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