elementary : indentation and some small changes
[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         wd->state = !wd->state;
289         if (wd->statep) *wd->statep = wd->state;
290         if (wd->state)
291           {
292              edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
293              if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
294                 _elm_access_say(E_("State: On"));
295           }
296         else
297           {
298              edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
299              if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
300                 _elm_access_say(E_("State: Off"));
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 (item && strcmp(item, "default")) return;
312    if (!wd) return;
313    eina_stringshare_replace(&wd->label, label);
314    if (label)
315      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
316    else
317      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
318    edje_object_message_signal_process(wd->chk);
319    edje_object_part_text_set(wd->chk, "elm.text", label);
320    _sizing_eval(obj);
321 }
322
323 static const char *
324 _elm_check_label_get(const Evas_Object *obj, const char *item)
325 {
326    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
327    Widget_Data *wd = elm_widget_data_get(obj);
328    if (item && strcmp(item, "default")) return NULL;
329    if (!wd) return NULL;
330    return wd->label;
331 }
332
333 static char *
334 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
335 {
336    const char *txt = elm_widget_access_info_get(obj);
337    if (!txt) txt = _elm_check_label_get(obj, NULL);
338    if (txt) return strdup(txt);
339    return NULL;
340 }
341
342 static char *
343 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
344 {
345    Evas_Object *o = data;
346    Widget_Data *wd = elm_widget_data_get(o);
347    if (!wd) return NULL;
348    if (elm_widget_disabled_get(obj))
349      return strdup(E_("State: Disabled"));
350    if (wd->state)
351      return strdup(E_("State: On"));
352    return strdup(E_("State: Off"));
353 }
354
355 EAPI Evas_Object *
356 elm_check_add(Evas_Object *parent)
357 {
358    Evas_Object *obj;
359    Evas *e;
360    Widget_Data *wd;
361
362    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
363
364    ELM_SET_WIDTYPE(widtype, "check");
365    elm_widget_type_set(obj, "check");
366    elm_widget_sub_object_add(parent, obj);
367    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
368    elm_widget_data_set(obj, wd);
369    elm_widget_del_hook_set(obj, _del_hook);
370    elm_widget_theme_hook_set(obj, _theme_hook);
371    elm_widget_disable_hook_set(obj, _disable_hook);
372    elm_widget_can_focus_set(obj, EINA_TRUE);
373    elm_widget_activate_hook_set(obj, _activate_hook);
374    elm_widget_event_hook_set(obj, _event_hook);
375    elm_widget_text_set_hook_set(obj, _elm_check_label_set);
376    elm_widget_text_get_hook_set(obj, _elm_check_label_get);
377    elm_widget_content_set_hook_set(obj, _content_set_hook);
378    elm_widget_content_get_hook_set(obj, _content_get_hook);
379    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
380
381    wd->chk = edje_object_add(e);
382    _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
383    edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
384                                    _signal_check_on, obj);
385    edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
386                                    _signal_check_off, obj);
387    edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
388                                    _signal_check_toggle, obj);
389    elm_widget_resize_object_set(obj, wd->chk);
390
391    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
392
393    _mirrored_set(obj, elm_widget_mirrored_get(obj));
394    _sizing_eval(obj);
395
396    // TODO: convert Elementary to subclassing of Evas_Smart_Class
397    // TODO: and save some bytes, making descriptions per-class and not instance!
398    evas_object_smart_callbacks_descriptions_set(obj, _signals);
399
400    _elm_access_object_register(obj, wd->chk);
401    _elm_access_text_set(_elm_access_object_get(obj),
402                         ELM_ACCESS_TYPE, E_("Check"));
403    _elm_access_callback_set(_elm_access_object_get(obj),
404                             ELM_ACCESS_INFO, _access_info_cb, obj);
405    _elm_access_callback_set(_elm_access_object_get(obj),
406                             ELM_ACCESS_STATE, _access_state_cb, obj);
407    return obj;
408 }
409
410 EAPI void
411 elm_check_label_set(Evas_Object *obj, const char *label)
412 {
413    _elm_check_label_set(obj, NULL, label);
414 }
415
416 EAPI const char *
417 elm_check_label_get(const Evas_Object *obj)
418 {
419    return _elm_check_label_get(obj, NULL);
420 }
421
422 EAPI void
423 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
424 {
425    _content_set_hook(obj, NULL, icon);
426 }
427
428 EAPI Evas_Object *
429 elm_check_icon_get(const Evas_Object *obj)
430 {
431    return _content_get_hook(obj, NULL);
432 }
433
434 EAPI Evas_Object *
435 elm_check_icon_unset(Evas_Object *obj)
436 {
437    return _content_unset_hook(obj, NULL);
438 }
439
440 EAPI void
441 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
442 {
443    ELM_CHECK_WIDTYPE(obj, widtype);
444    Widget_Data *wd = elm_widget_data_get(obj);
445    if (!wd) return;
446    if (state != wd->state)
447      {
448         wd->state = state;
449         if (wd->statep) *wd->statep = wd->state;
450         if (wd->state)
451           edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
452         else
453           edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
454      }
455    edje_object_message_signal_process(wd->chk);
456 }
457
458 EAPI Eina_Bool
459 elm_check_state_get(const Evas_Object *obj)
460 {
461    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
462    Widget_Data *wd = elm_widget_data_get(obj);
463    if (!wd) return EINA_FALSE;
464    return wd->state;
465 }
466
467 EAPI void
468 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
469 {
470    ELM_CHECK_WIDTYPE(obj, widtype);
471    Widget_Data *wd = elm_widget_data_get(obj);
472    if (!wd) return;
473    if (statep)
474      {
475         wd->statep = statep;
476         if (*wd->statep != wd->state)
477           {
478              wd->state = *wd->statep;
479              if (wd->state)
480                edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
481              else
482                edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
483           }
484      }
485    else
486      wd->statep = NULL;
487 }