aboutsummaryrefslogtreecommitdiffstats
path: root/src/edje_external/elementary/elm.c
blob: 744d2891c5f8538b95ec2f608c4b6dcd3947187c (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include "Elementary.h"
#include "private.h"

int _elm_ext_log_dom = -1;

static int init_count = 0;

void
external_elm_init(void)
{
   int argc = 0;
   char **argv = NULL;

   init_count++;
   DBG("elm_real_init\n");
   if (init_count > 1) return;
   ecore_app_args_get(&argc, &argv);
   elm_init(argc, argv);
}

static void
external_elm_shutdown(void)
{
   init_count--;
   DBG("elm_real_shutdown\n");
   if (init_count > 0) return;
   elm_shutdown();
}

void
external_signal(void *data EINA_UNUSED, Evas_Object *obj, const char *sig,
                const char *source)
{
   char *_signal = strdup(sig);
   char *p = _signal;
   Evas_Object *content;
   Edje_External_Type *type;

   if (!p) goto on_error;

   while ((*p!='\0') && (*p!=']'))
     p++;


   if ((*p=='\0') || (*(p+1)!=':'))
     {
        ERR("Invalid External Signal received: '%s' '%s'", sig, source);
        goto on_error;
     }

   *p = '\0';
   p+=2; //jump ']' and ':'

   type = evas_object_data_get(obj, "Edje_External_Type");
   if (!type)
     {
        ERR("no external type for object %p", obj);
        goto on_error;
     }
   if (!type->content_get)
     {
        ERR("external type '%s' from module '%s' does not provide content_get()",
            type->module_name, type->module);
        goto on_error;
     }

   content = type->content_get(type->data, obj, _signal);
   if (content)
     edje_object_signal_emit(content, sig + (p - _signal), source);

on_error:
   free(_signal);
   return;
}

const char *
external_translate(void *data EINA_UNUSED, const char *orig)
{
   // in future, mark all params as translatable and use dgettext()
   // with "elementary" text domain here.
   return orig;
}

typedef struct {
   const char *emission;
   const char *source;
   Evas_Object *edje;
} Elm_External_Signals_Proxy_Context;

static void
_external_signal_proxy_free_cb(void *data, Evas *e EINA_UNUSED,
                               Evas_Object *obj EINA_UNUSED,
                               void *event_info EINA_UNUSED)
{
   Elm_External_Signals_Proxy_Context *ctxt = data;
   external_elm_shutdown();
   free(ctxt);
}

static void
_external_signal_proxy_cb(void *data, Evas_Object *obj EINA_UNUSED,
                          void *event_info EINA_UNUSED)
{
   Elm_External_Signals_Proxy_Context *ctxt = data;
   // TODO: Is it worth to check Evas_Smart_Cb_Description and do something
   // TODO: with event_info given its description?
   edje_object_signal_emit(ctxt->edje, ctxt->emission, ctxt->source);
}

Eina_Bool
external_common_param_get(void *data EINA_UNUSED, const Evas_Object *obj,
                          Edje_External_Param *param)
{
   if (!strcmp(param->name, "style"))
     {
        if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
          {
             param->s = elm_object_style_get(obj);
             return EINA_TRUE;
          }
     }
   else if (!strcmp(param->name, "disabled"))
     {
        if (param->type == EDJE_EXTERNAL_PARAM_TYPE_BOOL)
          {
             param->i = elm_object_disabled_get(obj);
             return EINA_TRUE;
          }
     }
   return EINA_FALSE;
}

Eina_Bool
external_common_param_set(void *data EINA_UNUSED, Evas_Object *obj,
                          const Edje_External_Param *param)
{
   if (!strcmp(param->name, "style"))
     {
         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_STRING)
           {
              elm_object_style_set(obj, param->s);
              return EINA_TRUE;
           }
     }
   else if (!strcmp(param->name, "disabled"))
     {
         if (param->type == EDJE_EXTERNAL_PARAM_TYPE_BOOL)
           {
              elm_object_disabled_set(obj, param->i);
              return EINA_TRUE;
           }
     }
   return EINA_FALSE;
}

void
external_signals_proxy(Evas_Object *obj, Evas_Object *edje, const char *part_name)
{
   const Evas_Smart_Cb_Description **cls_descs, **inst_descs;
   unsigned int cls_count, inst_count, total;
   Elm_External_Signals_Proxy_Context *ctxt;

   evas_object_smart_callbacks_descriptions_get
     (obj, &cls_descs, &cls_count, &inst_descs, &inst_count);

   total = cls_count + inst_count;
   if (!total) return;
   ctxt = malloc(sizeof(Elm_External_Signals_Proxy_Context) * total);
   if (!ctxt) return;
   evas_object_event_callback_add
     (obj, EVAS_CALLBACK_FREE, _external_signal_proxy_free_cb, ctxt);

   for (; cls_count > 0; cls_count--,  cls_descs++, ctxt++)
     {
        const Evas_Smart_Cb_Description *d = *cls_descs;
        ctxt->emission = d->name;
        ctxt->source = part_name;
        ctxt->edje = edje;
        evas_object_smart_callback_add
           (obj, d->name, _external_signal_proxy_cb, ctxt);
     }

   for (; inst_count > 0; inst_count--,  inst_descs++, ctxt++)
     {
        const Evas_Smart_Cb_Description *d = *inst_descs;
        ctxt->emission = d->name;
        ctxt->source = part_name;
        ctxt->edje = edje;
        evas_object_smart_callback_add
           (obj, d->name, _external_signal_proxy_cb, ctxt);
     }
}

void
external_common_params_parse(void *mem, void *data EINA_UNUSED,
                             Evas_Object *obj EINA_UNUSED,
                             const Eina_List *params)
{
   Elm_Params *p;
   const Eina_List *l;
   Edje_External_Param *param;

   p = mem;
   EINA_LIST_FOREACH(params, l, param)
     {
        if (!strcmp(param->name, "style"))
          p->style = eina_stringshare_add(param->s);
        else if (!strcmp(param->name, "disabled"))
          {
             p->disabled = param->i;
             p->disabled_exists = EINA_TRUE;
          }
     }
}

void
external_common_state_set(void *data EINA_UNUSED, Evas_Object *obj,
                          const void *from_params, const void *to_params,
                          float pos EINA_UNUSED)
{
   const Elm_Params *p;
   if (to_params) p = to_params;
   else if (from_params) p = from_params;
   else return;

   if (p->style)
      elm_object_style_set(obj, p->style);
   if (p->disabled_exists)
     elm_object_disabled_set(obj, p->disabled);
}

Evas_Object *
external_common_param_icon_get(Evas_Object *obj, const Edje_External_Param *p)
{
   Evas_Object *edje, *parent_widget, *icon;
   const char *file;

   if ((!p) || (!p->s) || (p->type != EDJE_EXTERNAL_PARAM_TYPE_STRING))
       return NULL;

   edje = evas_object_smart_parent_get(obj);
   edje_object_file_get(edje, &file, NULL);

   parent_widget = elm_widget_parent_widget_get(obj);
   if (!parent_widget)
     parent_widget = edje;
   icon = elm_icon_add(parent_widget);

   if ((edje_file_group_exists(file, p->s)) &&
       (elm_image_file_set(icon, file, p->s)))
     return icon;
   if (elm_icon_standard_set(icon, p->s))
     return icon;

   ERR("Failed to set icon: '%s'", p->s);
   evas_object_del(icon);
   return NULL;
}

void
external_common_icon_param_parse(Evas_Object **icon, Evas_Object *obj,
                                 const Eina_List *params)
{
   Edje_External_Param *p = edje_external_param_find(params, "icon");
   *icon = external_common_param_icon_get(obj, p);
}

Evas_Object *
external_common_param_elm_layout_get(Evas_Object *obj,
                                     const Edje_External_Param *p)
{
   Evas_Object *edje, *parent_widget, *ret;
   const char *file;

   if ((!p) || (!p->s) || (p->type != EDJE_EXTERNAL_PARAM_TYPE_STRING))
       return NULL;

   edje = evas_object_smart_parent_get(obj);
   edje_object_file_get(edje, &file, NULL);

   parent_widget = elm_widget_parent_widget_get(obj);
   if (parent_widget)
     {
        ret = elm_layout_add(parent_widget);
        if (elm_layout_file_set(ret, file, p->s))
          return ret;
     }
   else
     {
        ret = edje_object_add(evas_object_evas_get(edje));
        if (edje_object_file_set(ret, file, p->s))
          return ret;
     }
   evas_object_del(ret);
   return NULL;
}

void
external_common_params_free(void *params)
{
   Elm_Params *p = params;
   if (p->style)
     eina_stringshare_del(p->style);
}

#define DEFINE_TYPE(type_name) \
  extern const Edje_External_Type external_##type_name##_type;
#include "modules.inc"
#undef DEFINE_TYPE

static Edje_External_Type_Info elm_external_types[] =
{
#define DEFINE_TYPE(type_name)              \
  { "elm/"#type_name, &external_##type_name##_type },
#include "modules.inc"
#undef DEFINE_TYPE
   { NULL, NULL }
};

static Eina_Bool
elm_mod_init(void)
{
   _elm_ext_log_dom = eina_log_domain_register("elm-externals",
                                               EINA_COLOR_LIGHTBLUE);
   edje_external_type_array_register(elm_external_types);
   return EINA_TRUE;
}

static void
elm_mod_shutdown(void)
{
   edje_external_type_array_unregister(elm_external_types);
   if (_elm_ext_log_dom >= 0) eina_log_domain_unregister(_elm_ext_log_dom);
   _elm_ext_log_dom = -1;
}

EINA_MODULE_INIT(elm_mod_init);
EINA_MODULE_SHUTDOWN(elm_mod_shutdown);