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