elm: Use appropriate add/del_full for evas event callbacks.
[framework/uifw/elementary.git] / src / lib / elm_check.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *chk, *icon;
9    Eina_Bool state;
10    Eina_Bool *statep;
11    const char *label;
12    const char *ontext, *offtext;
13 };
14
15 static const char *widtype = NULL;
16 static void _del_hook(Evas_Object *obj);
17 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
18 static void _theme_hook(Evas_Object *obj);
19 static void _disable_hook(Evas_Object *obj);
20 static void _sizing_eval(Evas_Object *obj);
21 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
22 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
23 static void _signal_check_off(void *data, Evas_Object *obj, const char *emission, const char *source);
24 static void _signal_check_on(void *data, Evas_Object *obj, const char *emission, const char *source);
25 static void _signal_check_toggle(void *data, Evas_Object *obj, const char *emission, const char *source);
26 static void _on_focus_hook(void *data, Evas_Object *obj);
27 static void _activate_hook(Evas_Object *obj);
28 static void _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
29 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
30 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
31
32 static void _activate(Evas_Object *obj);
33 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
34                              Evas_Callback_Type type, void *event_info);
35
36 static const char SIG_CHANGED[] = "changed";
37 static const Evas_Smart_Cb_Description _signals[] = {
38        {SIG_CHANGED, ""},
39        {NULL, NULL}
40 };
41
42 static Eina_Bool
43 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
44 {
45    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
46    Evas_Event_Key_Down *ev = event_info;
47    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
48    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
49    if ((strcmp(ev->keyname, "Return")) &&
50        (strcmp(ev->keyname, "KP_Enter")) &&
51        (strcmp(ev->keyname, "space")))
52      return EINA_FALSE;
53    _activate(obj);
54    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
55    return EINA_TRUE;
56 }
57
58
59 static void
60 _del_hook(Evas_Object *obj)
61 {
62    Widget_Data *wd = elm_widget_data_get(obj);
63    if (!wd) return;
64    if (wd->label) eina_stringshare_del(wd->label);
65    if (wd->ontext) eina_stringshare_del(wd->ontext);
66    if (wd->offtext) eina_stringshare_del(wd->offtext);
67    free(wd);
68 }
69
70 static void
71 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
72 {
73    Widget_Data *wd = elm_widget_data_get(obj);
74    if (!wd) return;
75    if (elm_widget_focus_get(obj))
76      {
77         edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
78         evas_object_focus_set(wd->chk, EINA_TRUE);
79      }
80    else
81      {
82         edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
83         evas_object_focus_set(wd->chk, EINA_FALSE);
84      }
85 }
86
87 static void
88 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
89 {
90    Widget_Data *wd = elm_widget_data_get(obj);
91    if (!wd) return;
92    edje_object_mirrored_set(wd->chk, rtl);
93 }
94
95 static void
96 _theme_hook(Evas_Object *obj)
97 {
98    Widget_Data *wd = elm_widget_data_get(obj);
99    if (!wd) return;
100    _elm_widget_mirrored_reload(obj);
101    _mirrored_set(obj, elm_widget_mirrored_get(obj));
102    _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
103    if (wd->icon)
104      edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
105    else
106      edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
107    if (wd->state)
108      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
109    else
110      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
111    if (wd->label)
112      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
113    else
114      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
115    edje_object_part_text_set(wd->chk, "elm.text", wd->label);
116    edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
117    edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
118    if (elm_widget_disabled_get(obj))
119      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
120    edje_object_message_signal_process(wd->chk);
121    edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
122    _sizing_eval(obj);
123 }
124
125 static void
126 _disable_hook(Evas_Object *obj)
127 {
128    Widget_Data *wd = elm_widget_data_get(obj);
129    if (!wd) return;
130    if (elm_widget_disabled_get(obj))
131      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
132    else
133      edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
134 }
135
136 static void
137 _sizing_eval(Evas_Object *obj)
138 {
139    Widget_Data *wd = elm_widget_data_get(obj);
140    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
141    if (!wd) return;
142    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
143    edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
144    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
145    evas_object_size_hint_min_set(obj, minw, minh);
146    evas_object_size_hint_max_set(obj, maxw, maxh);
147 }
148
149 static void
150 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
151 {
152    Widget_Data *wd = elm_widget_data_get(data);
153    if (!wd) return;
154    if (obj != wd->icon) return;
155    _sizing_eval(data);
156 }
157
158 static void
159 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
160 {
161    Widget_Data *wd = elm_widget_data_get(obj);
162    Evas_Object *sub = event_info;
163    if (!wd) return;
164    if (sub == wd->icon)
165      {
166         edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
167         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
168                                             _changed_size_hints, obj);
169         wd->icon = NULL;
170         _sizing_eval(obj);
171         edje_object_message_signal_process(wd->chk);
172      }
173 }
174
175 static void
176 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
177 {
178    Widget_Data *wd = elm_widget_data_get(data);
179    if (!wd) return;
180    wd->state = EINA_FALSE;
181    if (wd->statep) *wd->statep = wd->state;
182    edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
183    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
184 }
185
186 static void
187 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
188 {
189    Widget_Data *wd = elm_widget_data_get(data);
190    if (!wd) return;
191    wd->state = EINA_TRUE;
192    if (wd->statep) *wd->statep = wd->state;
193    edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
194    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
195 }
196
197 static void
198 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
199 {
200    _activate(data);
201 }
202
203 static void
204 _activate_hook(Evas_Object *obj)
205 {
206    _activate(obj);
207 }
208
209 static void
210 _content_set_hook(Evas_Object *obj, const char *part __UNUSED__, Evas_Object *content)
211 {
212    ELM_CHECK_WIDTYPE(obj, widtype);
213    Widget_Data *wd = elm_widget_data_get(obj);
214    if (!wd) return;
215    if (wd->icon == content) return;
216    if (wd->icon) evas_object_del(wd->icon);
217    wd->icon = content;
218    if (content)
219      {
220         elm_widget_sub_object_add(obj, content);
221         evas_object_event_callback_add(content,
222                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
223                                        _changed_size_hints, obj);
224         edje_object_part_swallow(wd->chk, "elm.swallow.content", content);
225         edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
226         edje_object_message_signal_process(wd->chk);
227      }
228    _sizing_eval(obj);
229 }
230
231 static Evas_Object *
232 _content_get_hook(const Evas_Object *obj, const char *part __UNUSED__)
233 {
234    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
235    Widget_Data *wd = elm_widget_data_get(obj);
236    if (!wd) return NULL;
237    return wd->icon;
238 }
239
240 static Evas_Object *
241 _content_unset_hook(Evas_Object *obj, const char *part __UNUSED__)
242 {
243    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
244    Widget_Data *wd = elm_widget_data_get(obj);
245    if (!wd) return NULL;
246    if (!wd->icon) return NULL;
247    Evas_Object *icon = wd->icon;
248    elm_widget_sub_object_del(obj, wd->icon);
249    evas_object_event_callback_del_full(wd->icon,
250                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
251                                        _changed_size_hints, obj);
252    edje_object_part_unswallow(wd->chk, wd->icon);
253    wd->icon = NULL;
254    return icon;
255 }
256
257 static void
258 _activate(Evas_Object *obj)
259 {
260    Widget_Data *wd = elm_widget_data_get(obj);
261    if (!wd) return;
262    if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
263        (_elm_access_2nd_click_timeout(obj)))
264      {
265         wd->state = !wd->state;
266         if (wd->statep) *wd->statep = wd->state;
267         if (wd->state)
268           {
269              edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
270              if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
271                {
272                   if (!wd->ontext)
273                     {
274                        _elm_access_say(E_("State: On"));
275                     }
276                   else
277                      _elm_access_say(E_("State: On"));
278                }
279           }
280         else
281           {
282              edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
283              if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
284                {
285                   if (!wd->offtext)
286                     {
287                        _elm_access_say(E_("State: Off"));
288                     }
289                   else
290                      _elm_access_say(E_("State: Off"));
291                }
292           }
293         evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
294      }
295 }
296
297 static void
298 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
299 {
300    ELM_CHECK_WIDTYPE(obj, widtype);
301    Widget_Data *wd = elm_widget_data_get(obj);
302    if (!wd) return;
303    if ((!item) || (!strcmp(item, "default")))
304      {
305         eina_stringshare_replace(&wd->label, label);
306         if (label)
307            edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
308         else
309            edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
310         edje_object_message_signal_process(wd->chk);
311         edje_object_part_text_set(wd->chk, "elm.text", label);
312      }
313    else if ((item) && (!strcmp(item, "on")))
314      {
315         eina_stringshare_replace(&wd->ontext, label);
316         edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
317      }
318    else if ((item) && (!strcmp(item, "off")))
319      {
320         eina_stringshare_replace(&wd->offtext, label);
321         edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
322      }
323    _sizing_eval(obj);
324 }
325
326 static const char *
327 _elm_check_label_get(const Evas_Object *obj, const char *item)
328 {
329    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
330    Widget_Data *wd = elm_widget_data_get(obj);
331    if (!wd) return NULL;
332    if ((!item) || (!strcmp(item, "default")))
333       return wd->label;
334    else if ((item) && (!strcmp(item, "on")))
335       return wd->ontext;
336    else if ((item) && (!strcmp(item, "off")))
337       return wd->offtext;
338    return NULL;
339 }
340
341 static char *
342 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
343 {
344    const char *txt = elm_widget_access_info_get(obj);
345    if (!txt) txt = _elm_check_label_get(obj, NULL);
346    if (txt) return strdup(txt);
347    return NULL;
348 }
349
350 static char *
351 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
352 {
353    Evas_Object *o = data;
354    Widget_Data *wd = elm_widget_data_get(o);
355    if (!wd) return NULL;
356    if (elm_widget_disabled_get(obj))
357      return strdup(E_("State: Disabled"));
358    if (wd->state)
359      {
360         if (wd->ontext)
361           {
362              char buf[1024];
363
364              snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
365              return strdup(buf);
366           }
367         else
368            return strdup(E_("State: On"));
369      }
370    if (wd->offtext)
371      {
372         char buf[1024];
373
374         snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
375         return strdup(buf);
376      }
377    return strdup(E_("State: Off"));
378 }
379
380 EAPI Evas_Object *
381 elm_check_add(Evas_Object *parent)
382 {
383    Evas_Object *obj;
384    Evas *e;
385    Widget_Data *wd;
386
387    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
388
389    ELM_SET_WIDTYPE(widtype, "check");
390    elm_widget_type_set(obj, "check");
391    elm_widget_sub_object_add(parent, obj);
392    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
393    elm_widget_data_set(obj, wd);
394    elm_widget_del_hook_set(obj, _del_hook);
395    elm_widget_theme_hook_set(obj, _theme_hook);
396    elm_widget_disable_hook_set(obj, _disable_hook);
397    elm_widget_can_focus_set(obj, EINA_TRUE);
398    elm_widget_activate_hook_set(obj, _activate_hook);
399    elm_widget_event_hook_set(obj, _event_hook);
400    elm_widget_text_set_hook_set(obj, _elm_check_label_set);
401    elm_widget_text_get_hook_set(obj, _elm_check_label_get);
402    elm_widget_content_set_hook_set(obj, _content_set_hook);
403    elm_widget_content_get_hook_set(obj, _content_get_hook);
404    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
405
406    wd->chk = edje_object_add(e);
407    _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
408    edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
409                                    _signal_check_on, obj);
410    edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
411                                    _signal_check_off, obj);
412    edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
413                                    _signal_check_toggle, obj);
414    elm_widget_resize_object_set(obj, wd->chk);
415
416    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
417
418    _mirrored_set(obj, elm_widget_mirrored_get(obj));
419    _sizing_eval(obj);
420
421    // TODO: convert Elementary to subclassing of Evas_Smart_Class
422    // TODO: and save some bytes, making descriptions per-class and not instance!
423    evas_object_smart_callbacks_descriptions_set(obj, _signals);
424
425    _elm_access_object_register(obj, wd->chk);
426    _elm_access_text_set(_elm_access_object_get(obj),
427                         ELM_ACCESS_TYPE, E_("Check"));
428    _elm_access_callback_set(_elm_access_object_get(obj),
429                             ELM_ACCESS_INFO, _access_info_cb, obj);
430    _elm_access_callback_set(_elm_access_object_get(obj),
431                             ELM_ACCESS_STATE, _access_state_cb, obj);
432    return obj;
433 }
434
435 EAPI void
436 elm_check_label_set(Evas_Object *obj, const char *label)
437 {
438    _elm_check_label_set(obj, NULL, label);
439 }
440
441 EAPI const char *
442 elm_check_label_get(const Evas_Object *obj)
443 {
444    return _elm_check_label_get(obj, NULL);
445 }
446
447 EAPI void
448 elm_check_states_labels_set(Evas_Object *obj, const char *ontext, const char *offtext)
449 {
450    _elm_check_label_set(obj, "on", ontext);
451    _elm_check_label_set(obj, "off", offtext);
452 }
453
454 EAPI void
455 elm_check_states_labels_get(const Evas_Object *obj, const char **ontext, const char **offtext)
456 {
457    if (ontext) *ontext = _elm_check_label_get(obj, "on");
458    if (offtext) *offtext = _elm_check_label_get(obj, "off");
459 }
460
461 EAPI void
462 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
463 {
464    _content_set_hook(obj, NULL, icon);
465 }
466
467 EAPI Evas_Object *
468 elm_check_icon_get(const Evas_Object *obj)
469 {
470    return _content_get_hook(obj, NULL);
471 }
472
473 EAPI Evas_Object *
474 elm_check_icon_unset(Evas_Object *obj)
475 {
476    return _content_unset_hook(obj, NULL);
477 }
478
479 EAPI void
480 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
481 {
482    ELM_CHECK_WIDTYPE(obj, widtype);
483    Widget_Data *wd = elm_widget_data_get(obj);
484    if (!wd) return;
485    if (state != wd->state)
486      {
487         wd->state = state;
488         if (wd->statep) *wd->statep = wd->state;
489         if (wd->state)
490           edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
491         else
492           edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
493      }
494    edje_object_message_signal_process(wd->chk);
495 }
496
497 EAPI Eina_Bool
498 elm_check_state_get(const Evas_Object *obj)
499 {
500    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
501    Widget_Data *wd = elm_widget_data_get(obj);
502    if (!wd) return EINA_FALSE;
503    return wd->state;
504 }
505
506 EAPI void
507 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
508 {
509    ELM_CHECK_WIDTYPE(obj, widtype);
510    Widget_Data *wd = elm_widget_data_get(obj);
511    if (!wd) return;
512    if (statep)
513      {
514         wd->statep = statep;
515         if (*wd->statep != wd->state)
516           {
517              wd->state = *wd->statep;
518              if (wd->state)
519                edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
520              else
521                edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
522           }
523      }
524    else
525      wd->statep = NULL;
526 }