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