elementary/check - content_set/get/unset
[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    edje_object_part_unswallow(wd->chk, wd->icon);
273    wd->icon = NULL;
274    return icon;
275 }
276
277 static void
278 _activate(Evas_Object *obj)
279 {
280    Widget_Data *wd = elm_widget_data_get(obj);
281    if (!wd) return;
282    wd->state = !wd->state;
283    if (wd->statep) *wd->statep = wd->state;
284    if (wd->state)
285      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
286    else
287      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
288    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
289 }
290
291 static void
292 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
293 {
294    ELM_CHECK_WIDTYPE(obj, widtype);
295    Widget_Data *wd = elm_widget_data_get(obj);
296    if (item && strcmp(item, "default")) return;
297    if (!wd) return;
298    eina_stringshare_replace(&wd->label, label);
299    if (label)
300      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
301    else
302      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
303    edje_object_message_signal_process(wd->chk);
304    edje_object_part_text_set(wd->chk, "elm.text", label);
305    _sizing_eval(obj);
306 }
307
308 static const char *
309 _elm_check_label_get(const Evas_Object *obj, const char *item)
310 {
311    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
312    Widget_Data *wd = elm_widget_data_get(obj);
313    if (item && strcmp(item, "default")) return NULL;
314    if (!wd) return NULL;
315    return wd->label;
316 }
317
318 EAPI Evas_Object *
319 elm_check_add(Evas_Object *parent)
320 {
321    Evas_Object *obj;
322    Evas *e;
323    Widget_Data *wd;
324
325    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
326
327    ELM_SET_WIDTYPE(widtype, "check");
328    elm_widget_type_set(obj, "check");
329    elm_widget_sub_object_add(parent, obj);
330    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
331    elm_widget_data_set(obj, wd);
332    elm_widget_del_hook_set(obj, _del_hook);
333    elm_widget_theme_hook_set(obj, _theme_hook);
334    elm_widget_disable_hook_set(obj, _disable_hook);
335    elm_widget_can_focus_set(obj, EINA_TRUE);
336    elm_widget_activate_hook_set(obj, _activate_hook);
337    elm_widget_event_hook_set(obj, _event_hook);
338    elm_widget_text_set_hook_set(obj, _elm_check_label_set);
339    elm_widget_text_get_hook_set(obj, _elm_check_label_get);
340    elm_widget_content_set_hook_set(obj, _content_set_hook);
341    elm_widget_content_get_hook_set(obj, _content_get_hook);
342    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
343
344    wd->chk = edje_object_add(e);
345    _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
346    edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
347                                    _signal_check_on, obj);
348    edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
349                                    _signal_check_off, obj);
350    edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
351                                    _signal_check_toggle, obj);
352    elm_widget_resize_object_set(obj, wd->chk);
353
354    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
355
356    _mirrored_set(obj, elm_widget_mirrored_get(obj));
357    _sizing_eval(obj);
358
359    // TODO: convert Elementary to subclassing of Evas_Smart_Class
360    // TODO: and save some bytes, making descriptions per-class and not instance!
361    evas_object_smart_callbacks_descriptions_set(obj, _signals);
362    return obj;
363 }
364
365 EAPI void
366 elm_check_label_set(Evas_Object *obj, const char *label)
367 {
368    _elm_check_label_set(obj, NULL, label);
369 }
370
371 EAPI const char *
372 elm_check_label_get(const Evas_Object *obj)
373 {
374    return _elm_check_label_get(obj, NULL);
375 }
376
377 EAPI void
378 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
379 {
380    _content_set_hook(obj, NULL, icon);
381 }
382
383 EAPI Evas_Object *
384 elm_check_icon_get(const Evas_Object *obj)
385 {
386    return _content_get_hook(obj, NULL);
387 }
388
389 EAPI Evas_Object *
390 elm_check_icon_unset(Evas_Object *obj)
391 {
392    return _content_unset_hook(obj, NULL);
393 }
394
395 EAPI void
396 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
397 {
398    ELM_CHECK_WIDTYPE(obj, widtype);
399    Widget_Data *wd = elm_widget_data_get(obj);
400    if (!wd) return;
401    if (state != wd->state)
402      {
403         wd->state = state;
404         if (wd->statep) *wd->statep = wd->state;
405         if (wd->state)
406           edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
407         else
408           edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
409      }
410    edje_object_message_signal_process(wd->chk);
411 }
412
413 EAPI Eina_Bool
414 elm_check_state_get(const Evas_Object *obj)
415 {
416    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
417    Widget_Data *wd = elm_widget_data_get(obj);
418    if (!wd) return EINA_FALSE;
419    return wd->state;
420 }
421
422 EAPI void
423 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
424 {
425    ELM_CHECK_WIDTYPE(obj, widtype);
426    Widget_Data *wd = elm_widget_data_get(obj);
427    if (!wd) return;
428    if (statep)
429      {
430         wd->statep = statep;
431         if (*wd->statep != wd->state)
432           {
433              wd->state = *wd->statep;
434              if (wd->state)
435                edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
436              else
437                edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
438           }
439      }
440    else
441      wd->statep = NULL;
442 }