aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/efl_ui_text_factory_images.c
blob: 38529e43a98775adddf6b8aad2aa9b1efeecd6bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>
#include <Elementary_Cursor.h>
#include "elm_priv.h"

#define MY_CLASS EFL_UI_TEXT_FACTORY_IMAGES_CLASS

typedef struct _Efl_Ui_Text_Factory_Images_Data Efl_Ui_Text_Factory_Images_Data;

struct _Efl_Ui_Text_Factory_Images_Data
{
   const char *name;
   Eina_Hash  *hash;
};

typedef struct
{
   Eina_File  *file;
   const char *key;
} File_Entry;

static void
_entry_free_cb(void *data)
{
   File_Entry *e = data;
   eina_file_close(e->file);
   eina_stringshare_del(e->key);
   free(e);
}

EOLIAN static Eo *
_efl_ui_text_factory_images_efl_object_constructor(Eo *obj,
     Efl_Ui_Text_Factory_Images_Data *pd EINA_UNUSED)
{
   obj = efl_constructor(efl_super(obj, MY_CLASS));
   pd->hash = eina_hash_string_superfast_new(_entry_free_cb);
   return obj;
}

EOLIAN static void
_efl_ui_text_factory_images_efl_object_destructor(Eo *obj,
     Efl_Ui_Text_Factory_Images_Data *pd EINA_UNUSED)
{
   eina_hash_free(pd->hash);
   efl_destructor(efl_super(obj, MY_CLASS));
}

EOLIAN static Efl_Canvas_Object *
_efl_ui_text_factory_images_efl_canvas_text_factory_create(Eo *obj EINA_UNUSED,
      Efl_Ui_Text_Factory_Images_Data *pd EINA_UNUSED,
      Efl_Canvas_Object *object,
      const char *key)
{
   Efl_Canvas_Object *o;
   File_Entry *e;

   o = efl_add(EFL_CANVAS_IMAGE_CLASS, object);
   e = eina_hash_find(pd->hash, key);
   if (e)
     {
        efl_file_key_set(o, e->key);
        if (efl_file_mmap_set(o, e->file)) goto error;
     }
   else
     {
        if (efl_file_set(o, key)) goto error;
     }
   if (efl_file_load(o)) goto error;

   return o;

error:
   efl_del(o);
   return NULL;
}

EOLIAN static Eina_Bool
_efl_ui_text_factory_images_matches_add(Eo *obj EINA_UNUSED,
      Efl_Ui_Text_Factory_Images_Data *pd,
      const char *name, const char *file, const char *key)
{
   File_Entry *e;
   Eina_File *f = eina_file_open(file, EINA_FALSE);

   if (!f) return EINA_FALSE;

   e = malloc(sizeof(*e));
   e->file = f;
   e->key = eina_stringshare_add(key);

   if (!eina_hash_add(pd->hash, name, e))
     {
        ERR("Failed to add file path %s to key %s\n", file, key);
        eina_file_close(f);
        free(e);
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

EOLIAN static Eina_Bool
_efl_ui_text_factory_images_matches_del(Eo *obj EINA_UNUSED,
      Efl_Ui_Text_Factory_Images_Data *pd,
      const char *name)
{
   return eina_hash_del(pd->hash, name, NULL);
}

EOLIAN static Eina_Bool
_efl_ui_text_factory_images_matches_mmap_add(Eo *obj EINA_UNUSED,
      Efl_Ui_Text_Factory_Images_Data *pd,
      const char *name, const Eina_File *file, const char *key)
{
   File_Entry *e;
   Eina_File *f;

   if (!file) return EINA_FALSE;

   f = eina_file_dup(file);
   e = malloc(sizeof(*e));
   e->file = f;
   e->key = eina_stringshare_add(key);

   if (!eina_hash_add(pd->hash, name, e))
     {
        ERR("Failed to add Eina_File %p to key %s\n", file, key);
        eina_file_close(f);
        free(e);
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

EOLIAN static Eina_Bool
_efl_ui_text_factory_images_matches_mmap_del(Eo *obj EINA_UNUSED,
      Efl_Ui_Text_Factory_Images_Data *pd,
      const char *name)
{
   return eina_hash_del(pd->hash, name, NULL);
}

#include "efl_ui_text_factory_images.eo.c"