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