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