Elementary: documentation for checkbox.
[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 _activate(Evas_Object *obj);
28 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
29                              Evas_Callback_Type type, void *event_info);
30
31 static const char SIG_CHANGED[] = "changed";
32 static const Evas_Smart_Cb_Description _signals[] = {
33        {SIG_CHANGED, ""},
34        {NULL, NULL}
35 };
36
37 static Eina_Bool
38 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
39 {
40    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
41    Evas_Event_Key_Down *ev = event_info;
42    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
43    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
44    if ((strcmp(ev->keyname, "Return")) &&
45        (strcmp(ev->keyname, "KP_Enter")) &&
46        (strcmp(ev->keyname, "space")))
47      return EINA_FALSE;
48    _activate(obj);
49    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
50    return EINA_TRUE;
51 }
52
53
54 static void
55 _del_hook(Evas_Object *obj)
56 {
57    Widget_Data *wd = elm_widget_data_get(obj);
58    if (!wd) return;
59    if (wd->label) eina_stringshare_del(wd->label);
60    free(wd);
61 }
62
63 static void
64 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
65 {
66    Widget_Data *wd = elm_widget_data_get(obj);
67    if (!wd) return;
68    if (elm_widget_focus_get(obj))
69      {
70         edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
71         evas_object_focus_set(wd->chk, EINA_TRUE);
72      }
73    else
74      {
75         edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
76         evas_object_focus_set(wd->chk, EINA_FALSE);
77      }
78 }
79
80 static void
81 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
82 {
83    Widget_Data *wd = elm_widget_data_get(obj);
84    if (!wd) return;
85    edje_object_mirrored_set(wd->chk, rtl);
86 }
87
88 static void
89 _theme_hook(Evas_Object *obj)
90 {
91    Widget_Data *wd = elm_widget_data_get(obj);
92    if (!wd) return;
93    _elm_widget_mirrored_reload(obj);
94    _mirrored_set(obj, elm_widget_mirrored_get(obj));
95    _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
96    if (wd->icon)
97      edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
98    else
99      edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
100    if (wd->state)
101      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
102    else
103      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
104    if (wd->label)
105      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
106    else
107      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
108    edje_object_part_text_set(wd->chk, "elm.text", wd->label);
109    if (elm_widget_disabled_get(obj))
110      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
111    edje_object_message_signal_process(wd->chk);
112    edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
113    _sizing_eval(obj);
114 }
115
116 static void
117 _disable_hook(Evas_Object *obj)
118 {
119    Widget_Data *wd = elm_widget_data_get(obj);
120    if (!wd) return;
121    if (elm_widget_disabled_get(obj))
122      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
123    else
124      edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
125 }
126
127 static void
128 _sizing_eval(Evas_Object *obj)
129 {
130    Widget_Data *wd = elm_widget_data_get(obj);
131    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
132    if (!wd) return;
133    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
134    edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
135    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
136    evas_object_size_hint_min_set(obj, minw, minh);
137    evas_object_size_hint_max_set(obj, maxw, maxh);
138 }
139
140 static void
141 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
142 {
143    Widget_Data *wd = elm_widget_data_get(data);
144    if (!wd) return;
145    if (obj != wd->icon) return;
146    Evas_Coord mw, mh;
147    evas_object_size_hint_min_get(obj, &mw, &mh);
148    _sizing_eval(data);
149 }
150
151 static void
152 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
153 {
154    Widget_Data *wd = elm_widget_data_get(obj);
155    Evas_Object *sub = event_info;
156    if (!wd) return;
157    if (sub == wd->icon)
158      {
159         edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
160         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
161                                             _changed_size_hints, obj);
162         wd->icon = NULL;
163         _sizing_eval(obj);
164         edje_object_message_signal_process(wd->chk);
165      }
166 }
167
168 static void
169 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
170 {
171    Widget_Data *wd = elm_widget_data_get(data);
172    if (!wd) return;
173    wd->state = EINA_FALSE;
174    if (wd->statep) *wd->statep = wd->state;
175    edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
176    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
177 }
178
179 static void
180 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
181 {
182    Widget_Data *wd = elm_widget_data_get(data);
183    if (!wd) return;
184    wd->state = EINA_TRUE;
185    if (wd->statep) *wd->statep = wd->state;
186    edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
187    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
188 }
189
190 static void
191 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
192 {
193    _activate(data);
194 }
195
196 static void
197 _activate_hook(Evas_Object *obj)
198 {
199    _activate(obj);
200 }
201
202 static void
203 _activate(Evas_Object *obj)
204 {
205    Widget_Data *wd = elm_widget_data_get(obj);
206    if (!wd) return;
207    wd->state = !wd->state;
208    if (wd->statep) *wd->statep = wd->state;
209    if (wd->state)
210      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
211    else
212      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
213    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
214 }
215
216 static void
217 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
218 {
219    ELM_CHECK_WIDTYPE(obj, widtype);
220    Widget_Data *wd = elm_widget_data_get(obj);
221    if (item && strcmp(item, "default")) return;
222    if (!wd) return;
223    eina_stringshare_replace(&wd->label, label);
224    if (label)
225      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
226    else
227      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
228    edje_object_message_signal_process(wd->chk);
229    edje_object_part_text_set(wd->chk, "elm.text", label);
230    _sizing_eval(obj);
231 }
232
233 static const char *
234 _elm_check_label_get(const Evas_Object *obj, const char *item)
235 {
236    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
237    Widget_Data *wd = elm_widget_data_get(obj);
238    if (item && strcmp(item, "default")) return NULL;
239    if (!wd) return NULL;
240    return wd->label;
241 }
242
243 EAPI Evas_Object *
244 elm_check_add(Evas_Object *parent)
245 {
246    Evas_Object *obj;
247    Evas *e;
248    Widget_Data *wd;
249
250    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
251
252    ELM_SET_WIDTYPE(widtype, "check");
253    elm_widget_type_set(obj, "check");
254    elm_widget_sub_object_add(parent, obj);
255    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
256    elm_widget_data_set(obj, wd);
257    elm_widget_del_hook_set(obj, _del_hook);
258    elm_widget_theme_hook_set(obj, _theme_hook);
259    elm_widget_disable_hook_set(obj, _disable_hook);
260    elm_widget_can_focus_set(obj, EINA_TRUE);
261    elm_widget_activate_hook_set(obj, _activate_hook);
262    elm_widget_event_hook_set(obj, _event_hook);
263    elm_widget_text_set_hook_set(obj, _elm_check_label_set);
264    elm_widget_text_get_hook_set(obj, _elm_check_label_get);
265
266    wd->chk = edje_object_add(e);
267    _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
268    edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
269                                    _signal_check_on, obj);
270    edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
271                                    _signal_check_off, obj);
272    edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
273                                    _signal_check_toggle, obj);
274    elm_widget_resize_object_set(obj, wd->chk);
275
276    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
277
278    _mirrored_set(obj, elm_widget_mirrored_get(obj));
279    _sizing_eval(obj);
280
281    // TODO: convert Elementary to subclassing of Evas_Smart_Class
282    // TODO: and save some bytes, making descriptions per-class and not instance!
283    evas_object_smart_callbacks_descriptions_set(obj, _signals);
284    return obj;
285 }
286
287 EAPI void
288 elm_check_label_set(Evas_Object *obj, const char *label)
289 {
290    _elm_check_label_set(obj, NULL, label);
291 }
292
293 EAPI const char *
294 elm_check_label_get(const Evas_Object *obj)
295 {
296    return _elm_check_label_get(obj, NULL);
297 }
298
299 EAPI void
300 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
301 {
302    ELM_CHECK_WIDTYPE(obj, widtype);
303    Widget_Data *wd = elm_widget_data_get(obj);
304    if (!wd) return;
305    if (wd->icon == icon) return;
306    if (wd->icon) evas_object_del(wd->icon);
307    wd->icon = icon;
308    if (icon)
309      {
310         elm_widget_sub_object_add(obj, icon);
311         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
312                                        _changed_size_hints, obj);
313         edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
314         edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
315         edje_object_message_signal_process(wd->chk);
316      }
317    _sizing_eval(obj);
318 }
319
320 EAPI Evas_Object *
321 elm_check_icon_get(const Evas_Object *obj)
322 {
323    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
324    Widget_Data *wd = elm_widget_data_get(obj);
325    if (!wd) return NULL;
326    return wd->icon;
327 }
328
329 EAPI Evas_Object *
330 elm_check_icon_unset(Evas_Object *obj)
331 {
332    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
333    Widget_Data *wd = elm_widget_data_get(obj);
334    if (!wd) return NULL;
335    if (!wd->icon) return NULL;
336    Evas_Object *icon = wd->icon;
337    elm_widget_sub_object_del(obj, wd->icon);
338    edje_object_part_unswallow(wd->chk, wd->icon);
339    wd->icon = NULL;
340    return icon;
341 }
342
343 EAPI void
344 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
345 {
346    ELM_CHECK_WIDTYPE(obj, widtype);
347    Widget_Data *wd = elm_widget_data_get(obj);
348    if (!wd) return;
349    if (state != wd->state)
350      {
351         wd->state = state;
352         if (wd->statep) *wd->statep = wd->state;
353         if (wd->state)
354           edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
355         else
356           edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
357      }
358 }
359
360 EAPI Eina_Bool
361 elm_check_state_get(const Evas_Object *obj)
362 {
363    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
364    Widget_Data *wd = elm_widget_data_get(obj);
365    if (!wd) return EINA_FALSE;
366    return wd->state;
367 }
368
369 EAPI void
370 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
371 {
372    ELM_CHECK_WIDTYPE(obj, widtype);
373    Widget_Data *wd = elm_widget_data_get(obj);
374    if (!wd) return;
375    if (statep)
376      {
377         wd->statep = statep;
378         if (*wd->statep != wd->state)
379           {
380              wd->state = *wd->statep;
381              if (wd->state)
382                edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
383              else
384                edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
385           }
386      }
387    else
388      wd->statep = NULL;
389 }