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