elementary - modified content_set/get/unset part names to be simple names.
[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, Evas_Object *content)
211 {
212    ELM_CHECK_WIDTYPE(obj, widtype);
213    Widget_Data *wd;
214
215    if (strcmp(part, "icon")) return;
216    wd = elm_widget_data_get(obj);
217    if (!wd) return;
218    if (wd->icon == content) return;
219    if (wd->icon) evas_object_del(wd->icon);
220    wd->icon = content;
221    if (content)
222      {
223         elm_widget_sub_object_add(obj, content);
224         evas_object_event_callback_add(content,
225                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
226                                        _changed_size_hints, obj);
227         edje_object_part_swallow(wd->chk, "elm.swallow.content", content);
228         edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
229         edje_object_message_signal_process(wd->chk);
230      }
231    _sizing_eval(obj);
232 }
233
234 static Evas_Object *
235 _content_get_hook(const Evas_Object *obj, const char *part)
236 {
237    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
238    Widget_Data *wd;
239
240    if (strcmp(part, "icon")) return NULL;
241    wd = elm_widget_data_get(obj);
242    if (!wd) return NULL;
243    return wd->icon;
244 }
245
246 static Evas_Object *
247 _content_unset_hook(Evas_Object *obj, const char *part)
248 {
249    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
250    Widget_Data *wd;
251
252    if (strcmp(part, "icon")) return NULL;
253    wd = elm_widget_data_get(obj);
254    if (!wd) return NULL;
255    if (!wd->icon) return NULL;
256    Evas_Object *icon = wd->icon;
257    elm_widget_sub_object_del(obj, wd->icon);
258    evas_object_event_callback_del_full(wd->icon,
259                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
260                                        _changed_size_hints, obj);
261    edje_object_part_unswallow(wd->chk, wd->icon);
262    wd->icon = NULL;
263    return icon;
264 }
265
266 static void
267 _activate(Evas_Object *obj)
268 {
269    Widget_Data *wd = elm_widget_data_get(obj);
270    if (!wd) return;
271    if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
272        (_elm_access_2nd_click_timeout(obj)))
273      {
274         wd->state = !wd->state;
275         if (wd->statep) *wd->statep = wd->state;
276         if (wd->state)
277           {
278              edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
279              if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
280                {
281                   if (!wd->ontext)
282                     {
283                        _elm_access_say(E_("State: On"));
284                     }
285                   else
286                      _elm_access_say(E_("State: On"));
287                }
288           }
289         else
290           {
291              edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
292              if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
293                {
294                   if (!wd->offtext)
295                     {
296                        _elm_access_say(E_("State: Off"));
297                     }
298                   else
299                      _elm_access_say(E_("State: Off"));
300                }
301           }
302         evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
303      }
304 }
305
306 static void
307 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
308 {
309    ELM_CHECK_WIDTYPE(obj, widtype);
310    Widget_Data *wd = elm_widget_data_get(obj);
311    if (!wd) return;
312    if ((!item) || (!strcmp(item, "default")))
313      {
314         eina_stringshare_replace(&wd->label, label);
315         if (label)
316            edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
317         else
318            edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
319         edje_object_message_signal_process(wd->chk);
320         edje_object_part_text_set(wd->chk, "elm.text", label);
321      }
322    else if ((item) && (!strcmp(item, "on")))
323      {
324         eina_stringshare_replace(&wd->ontext, label);
325         edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
326      }
327    else if ((item) && (!strcmp(item, "off")))
328      {
329         eina_stringshare_replace(&wd->offtext, label);
330         edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
331      }
332    _sizing_eval(obj);
333 }
334
335 static const char *
336 _elm_check_label_get(const Evas_Object *obj, const char *item)
337 {
338    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
339    Widget_Data *wd = elm_widget_data_get(obj);
340    if (!wd) return NULL;
341    if ((!item) || (!strcmp(item, "default")))
342       return wd->label;
343    else if ((item) && (!strcmp(item, "on")))
344       return wd->ontext;
345    else if ((item) && (!strcmp(item, "off")))
346       return wd->offtext;
347    return NULL;
348 }
349
350 static char *
351 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
352 {
353    const char *txt = elm_widget_access_info_get(obj);
354    if (!txt) txt = _elm_check_label_get(obj, NULL);
355    if (txt) return strdup(txt);
356    return NULL;
357 }
358
359 static char *
360 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
361 {
362    Evas_Object *o = data;
363    Widget_Data *wd = elm_widget_data_get(o);
364    if (!wd) return NULL;
365    if (elm_widget_disabled_get(obj))
366      return strdup(E_("State: Disabled"));
367    if (wd->state)
368      {
369         if (wd->ontext)
370           {
371              char buf[1024];
372
373              snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
374              return strdup(buf);
375           }
376         else
377            return strdup(E_("State: On"));
378      }
379    if (wd->offtext)
380      {
381         char buf[1024];
382
383         snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
384         return strdup(buf);
385      }
386    return strdup(E_("State: Off"));
387 }
388
389 EAPI Evas_Object *
390 elm_check_add(Evas_Object *parent)
391 {
392    Evas_Object *obj;
393    Evas *e;
394    Widget_Data *wd;
395
396    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
397
398    ELM_SET_WIDTYPE(widtype, "check");
399    elm_widget_type_set(obj, "check");
400    elm_widget_sub_object_add(parent, obj);
401    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
402    elm_widget_data_set(obj, wd);
403    elm_widget_del_hook_set(obj, _del_hook);
404    elm_widget_theme_hook_set(obj, _theme_hook);
405    elm_widget_disable_hook_set(obj, _disable_hook);
406    elm_widget_can_focus_set(obj, EINA_TRUE);
407    elm_widget_activate_hook_set(obj, _activate_hook);
408    elm_widget_event_hook_set(obj, _event_hook);
409    elm_widget_text_set_hook_set(obj, _elm_check_label_set);
410    elm_widget_text_get_hook_set(obj, _elm_check_label_get);
411    elm_widget_content_set_hook_set(obj, _content_set_hook);
412    elm_widget_content_get_hook_set(obj, _content_get_hook);
413    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
414
415    wd->chk = edje_object_add(e);
416    _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
417    edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
418                                    _signal_check_on, obj);
419    edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
420                                    _signal_check_off, obj);
421    edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
422                                    _signal_check_toggle, obj);
423    elm_widget_resize_object_set(obj, wd->chk);
424
425    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
426
427    _mirrored_set(obj, elm_widget_mirrored_get(obj));
428    _sizing_eval(obj);
429
430    // TODO: convert Elementary to subclassing of Evas_Smart_Class
431    // TODO: and save some bytes, making descriptions per-class and not instance!
432    evas_object_smart_callbacks_descriptions_set(obj, _signals);
433
434    _elm_access_object_register(obj, wd->chk);
435    _elm_access_text_set(_elm_access_object_get(obj),
436                         ELM_ACCESS_TYPE, E_("Check"));
437    _elm_access_callback_set(_elm_access_object_get(obj),
438                             ELM_ACCESS_INFO, _access_info_cb, obj);
439    _elm_access_callback_set(_elm_access_object_get(obj),
440                             ELM_ACCESS_STATE, _access_state_cb, obj);
441    return obj;
442 }
443
444 EAPI void
445 elm_check_label_set(Evas_Object *obj, const char *label)
446 {
447    _elm_check_label_set(obj, NULL, label);
448 }
449
450 EAPI const char *
451 elm_check_label_get(const Evas_Object *obj)
452 {
453    return _elm_check_label_get(obj, NULL);
454 }
455
456 EAPI void
457 elm_check_states_labels_set(Evas_Object *obj, const char *ontext, const char *offtext)
458 {
459    _elm_check_label_set(obj, "on", ontext);
460    _elm_check_label_set(obj, "off", offtext);
461 }
462
463 EAPI void
464 elm_check_states_labels_get(const Evas_Object *obj, const char **ontext, const char **offtext)
465 {
466    if (ontext) *ontext = _elm_check_label_get(obj, "on");
467    if (offtext) *offtext = _elm_check_label_get(obj, "off");
468 }
469
470 EAPI void
471 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
472 {
473    _content_set_hook(obj, "icon", icon);
474 }
475
476 EAPI Evas_Object *
477 elm_check_icon_get(const Evas_Object *obj)
478 {
479    return _content_get_hook(obj, "icon");
480 }
481
482 EAPI Evas_Object *
483 elm_check_icon_unset(Evas_Object *obj)
484 {
485    return _content_unset_hook(obj, "icon");
486 }
487
488 EAPI void
489 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
490 {
491    ELM_CHECK_WIDTYPE(obj, widtype);
492    Widget_Data *wd = elm_widget_data_get(obj);
493    if (!wd) return;
494    if (state != wd->state)
495      {
496         wd->state = state;
497         if (wd->statep) *wd->statep = wd->state;
498         if (wd->state)
499           edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
500         else
501           edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
502      }
503    edje_object_message_signal_process(wd->chk);
504 }
505
506 EAPI Eina_Bool
507 elm_check_state_get(const Evas_Object *obj)
508 {
509    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
510    Widget_Data *wd = elm_widget_data_get(obj);
511    if (!wd) return EINA_FALSE;
512    return wd->state;
513 }
514
515 EAPI void
516 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
517 {
518    ELM_CHECK_WIDTYPE(obj, widtype);
519    Widget_Data *wd = elm_widget_data_get(obj);
520    if (!wd) return;
521    if (statep)
522      {
523         wd->statep = statep;
524         if (*wd->statep != wd->state)
525           {
526              wd->state = *wd->statep;
527              if (wd->state)
528                edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
529              else
530                edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
531           }
532      }
533    else
534      wd->statep = NULL;
535 }