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