8ad2215f3f889c13158314fd5e071c46391c54cf
[framework/uifw/elementary.git] / src / lib / elm_check.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Check Check
6  *
7  * The check widget allows for toggling a value between true or false (1 or 0).
8  *
9  * Check objects are a lot like radio objects in layout and functionality
10  * except they do not work as a group, but independently and only toggle the
11  * value of a boolean from false to true (0 or 1). elm_check_state_set() sets
12  * the boolean state (1 for true, 0 for false), and elm_check_state_get()
13  * returns the current state. For convenience, like the radio objects, you
14  * can set a pointer to a boolean directly with elm_check_state_pointer_set()
15  * for it to modify.
16  *
17  * Signals that you can add callbacks for are:
18  *
19  * "changed" - This is called whenever the user changes the state of one of the
20  *             check object.
21  */
22 typedef struct _Widget_Data Widget_Data;
23
24 struct _Widget_Data
25 {
26    Evas_Object *chk, *icon;
27    Eina_Bool state;
28    Eina_Bool *statep;
29    const char *label;
30 };
31
32 static const char *widtype = NULL;
33 static void _del_hook(Evas_Object *obj);
34 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
35 static void _theme_hook(Evas_Object *obj);
36 static void _disable_hook(Evas_Object *obj);
37 static void _sizing_eval(Evas_Object *obj);
38 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
39 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
40 static void _signal_check_off(void *data, Evas_Object *obj, const char *emission, const char *source);
41 static void _signal_check_on(void *data, Evas_Object *obj, const char *emission, const char *source);
42 static void _signal_check_toggle(void *data, Evas_Object *obj, const char *emission, const char *source);
43 static void _on_focus_hook(void *data, Evas_Object *obj);
44 static void _activate_hook(Evas_Object *obj);
45 static void _activate(Evas_Object *obj);
46 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
47                              Evas_Callback_Type type, void *event_info);
48
49 static const char SIG_CHANGED[] = "changed";
50 static const Evas_Smart_Cb_Description _signals[] = {
51        {SIG_CHANGED, ""},
52        {NULL, NULL}
53 };
54
55 static Eina_Bool
56 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
57 {
58    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
59    Evas_Event_Key_Down *ev = event_info;
60    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
61    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
62    if ((strcmp(ev->keyname, "Return")) &&
63        (strcmp(ev->keyname, "KP_Enter")) &&
64        (strcmp(ev->keyname, "space")))
65      return EINA_FALSE;
66    _activate(obj);
67    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
68    return EINA_TRUE;
69 }
70
71
72 static void
73 _del_hook(Evas_Object *obj)
74 {
75    Widget_Data *wd = elm_widget_data_get(obj);
76    if (!wd) return;
77    if (wd->label) eina_stringshare_del(wd->label);
78    free(wd);
79 }
80
81 static void
82 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
83 {
84    Widget_Data *wd = elm_widget_data_get(obj);
85    if (!wd) return;
86    if (elm_widget_focus_get(obj))
87      {
88         edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
89         evas_object_focus_set(wd->chk, EINA_TRUE);
90      }
91    else
92      {
93         edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
94         evas_object_focus_set(wd->chk, EINA_FALSE);
95      }
96 }
97
98 static void
99 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
100 {
101    Widget_Data *wd = elm_widget_data_get(obj);
102    if (!wd) return;
103    edje_object_mirrored_set(wd->chk, rtl);
104 }
105
106 static void
107 _theme_hook(Evas_Object *obj)
108 {
109    Widget_Data *wd = elm_widget_data_get(obj);
110    if (!wd) return;
111    _elm_widget_mirrored_reload(obj);
112    _mirrored_set(obj, elm_widget_mirrored_get(obj));
113    _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
114    if (wd->icon)
115      edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
116    else
117      edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
118    if (wd->state)
119      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
120    else
121      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
122    if (wd->label)
123      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
124    else
125      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
126    edje_object_part_text_set(wd->chk, "elm.text", wd->label);
127    if (elm_widget_disabled_get(obj))
128      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
129    edje_object_message_signal_process(wd->chk);
130    edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
131    _sizing_eval(obj);
132 }
133
134 static void
135 _disable_hook(Evas_Object *obj)
136 {
137    Widget_Data *wd = elm_widget_data_get(obj);
138    if (!wd) return;
139    if (elm_widget_disabled_get(obj))
140      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
141    else
142      edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
143 }
144
145 static void
146 _sizing_eval(Evas_Object *obj)
147 {
148    Widget_Data *wd = elm_widget_data_get(obj);
149    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
150    if (!wd) return;
151    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
152    edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
153    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
154    evas_object_size_hint_min_set(obj, minw, minh);
155    evas_object_size_hint_max_set(obj, maxw, maxh);
156 }
157
158 static void
159 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
160 {
161    Widget_Data *wd = elm_widget_data_get(data);
162    if (!wd) return;
163    if (obj != wd->icon) return;
164    Evas_Coord mw, mh;
165    evas_object_size_hint_min_get(obj, &mw, &mh);
166    _sizing_eval(data);
167 }
168
169 static void
170 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
171 {
172    Widget_Data *wd = elm_widget_data_get(obj);
173    Evas_Object *sub = event_info;
174    if (!wd) return;
175    if (sub == wd->icon)
176      {
177         edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
178         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
179                                             _changed_size_hints, obj);
180         wd->icon = NULL;
181         _sizing_eval(obj);
182         edje_object_message_signal_process(wd->chk);
183      }
184 }
185
186 static void
187 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
188 {
189    Widget_Data *wd = elm_widget_data_get(data);
190    if (!wd) return;
191    wd->state = EINA_FALSE;
192    if (wd->statep) *wd->statep = wd->state;
193    edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
194    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
195 }
196
197 static void
198 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
199 {
200    Widget_Data *wd = elm_widget_data_get(data);
201    if (!wd) return;
202    wd->state = EINA_TRUE;
203    if (wd->statep) *wd->statep = wd->state;
204    edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
205    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
206 }
207
208 static void
209 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
210 {
211    _activate(data);
212 }
213
214 static void
215 _activate_hook(Evas_Object *obj)
216 {
217    _activate(obj);
218 }
219
220 static void
221 _activate(Evas_Object *obj)
222 {
223    Widget_Data *wd = elm_widget_data_get(obj);
224    if (!wd) return;
225    wd->state = !wd->state;
226    if (wd->statep) *wd->statep = wd->state;
227    if (wd->state)
228      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
229    else
230      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
231    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
232 }
233
234 static void
235 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
236 {
237    ELM_CHECK_WIDTYPE(obj, widtype);
238    Widget_Data *wd = elm_widget_data_get(obj);
239    if (item) return;
240    if (!wd) return;
241    eina_stringshare_replace(&wd->label, label);
242    if (label)
243      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
244    else
245      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
246    edje_object_message_signal_process(wd->chk);
247    edje_object_part_text_set(wd->chk, "elm.text", label);
248    _sizing_eval(obj);
249 }
250
251 static const char *
252 _elm_check_label_get(const Evas_Object *obj, const char *item)
253 {
254    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
255    Widget_Data *wd = elm_widget_data_get(obj);
256    if (item) return NULL;
257    if (!wd) return NULL;
258    return wd->label;
259 }
260
261 /**
262  * Add a new Check object
263  *
264  * @param parent The parent object
265  * @return The new object or NULL if it cannot be created
266  *
267  * @ingroup Check
268  */
269 EAPI Evas_Object *
270 elm_check_add(Evas_Object *parent)
271 {
272    Evas_Object *obj;
273    Evas *e;
274    Widget_Data *wd;
275
276    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
277
278    ELM_SET_WIDTYPE(widtype, "check");
279    elm_widget_type_set(obj, "check");
280    elm_widget_sub_object_add(parent, obj);
281    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
282    elm_widget_data_set(obj, wd);
283    elm_widget_del_hook_set(obj, _del_hook);
284    elm_widget_theme_hook_set(obj, _theme_hook);
285    elm_widget_disable_hook_set(obj, _disable_hook);
286    elm_widget_can_focus_set(obj, EINA_TRUE);
287    elm_widget_activate_hook_set(obj, _activate_hook);
288    elm_widget_event_hook_set(obj, _event_hook);
289    elm_widget_label_set_hook_set(obj, _elm_check_label_set);
290    elm_widget_label_get_hook_set(obj, _elm_check_label_get);
291
292    wd->chk = edje_object_add(e);
293    _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
294    edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
295                                    _signal_check_on, obj);
296    edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
297                                    _signal_check_off, obj);
298    edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
299                                    _signal_check_toggle, obj);
300    elm_widget_resize_object_set(obj, wd->chk);
301
302    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
303
304    _mirrored_set(obj, elm_widget_mirrored_get(obj));
305    _sizing_eval(obj);
306
307    // TODO: convert Elementary to subclassing of Evas_Smart_Class
308    // TODO: and save some bytes, making descriptions per-class and not instance!
309    evas_object_smart_callbacks_descriptions_set(obj, _signals);
310    return obj;
311 }
312
313 /**
314  * Set the text label of the check object
315  *
316  * @param obj The check object
317  * @param label The text label string in UTF-8
318  *
319  * @ingroup Check
320  */
321 EAPI void
322 elm_check_label_set(Evas_Object *obj, const char *label)
323 {
324    _elm_check_label_set(obj, NULL, label);
325 }
326
327 /**
328  * Get the text label of the check object
329  *
330  * @param obj The check object
331  * @return The text label string in UTF-8
332  *
333  * @ingroup Check
334  */
335 EAPI const char *
336 elm_check_label_get(const Evas_Object *obj)
337 {
338    return _elm_check_label_get(obj, NULL);
339 }
340
341 /**
342  * Set the icon object of the check object
343  *
344  * Once the icon object is set, a previously set one will be deleted.
345  * If you want to keep that old content object, use the
346  * elm_check_icon_unset() function.
347  *
348  * @param obj The check object
349  * @param icon The icon object
350  *
351  * @ingroup Check
352  */
353 EAPI void
354 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
355 {
356    ELM_CHECK_WIDTYPE(obj, widtype);
357    Widget_Data *wd = elm_widget_data_get(obj);
358    if (!wd) return;
359    if (wd->icon == icon) return;
360    if (wd->icon) evas_object_del(wd->icon);
361    wd->icon = icon;
362    if (icon)
363      {
364         elm_widget_sub_object_add(obj, icon);
365         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
366                                        _changed_size_hints, obj);
367         edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
368         edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
369         edje_object_message_signal_process(wd->chk);
370      }
371    _sizing_eval(obj);
372 }
373
374 /**
375  * Get the icon object of the check object
376  *
377  * @param obj The check object
378  * @return The icon object
379  *
380  * @ingroup Check
381  */
382 EAPI Evas_Object *
383 elm_check_icon_get(const Evas_Object *obj)
384 {
385    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
386    Widget_Data *wd = elm_widget_data_get(obj);
387    if (!wd) return NULL;
388    return wd->icon;
389 }
390
391 /**
392  * Unset the icon used for the check object
393  *
394  * Unparent and return the icon object which was set for this widget.
395  *
396  * @param obj The check object
397  * @return The icon object that was being used
398  *
399  * @ingroup Check
400  */
401 EAPI Evas_Object *
402 elm_check_icon_unset(Evas_Object *obj)
403 {
404    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
405    Widget_Data *wd = elm_widget_data_get(obj);
406    if (!wd) return NULL;
407    if (!wd->icon) return NULL;
408    Evas_Object *icon = wd->icon;
409    elm_widget_sub_object_del(obj, wd->icon);
410    edje_object_part_unswallow(wd->chk, wd->icon);
411    wd->icon = NULL;
412    return icon;
413 }
414
415 /**
416  * Set the on/off state of the check object
417  *
418  * This sets the state of the check and will also set the value if pointed to
419  * to the state supplied, but will not call any callbacks.
420  *
421  * @param obj The check object
422  * @param state The state to use (1 == on, 0 == off)
423  *
424  * @ingroup Check
425  */
426 EAPI void
427 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
428 {
429    ELM_CHECK_WIDTYPE(obj, widtype);
430    Widget_Data *wd = elm_widget_data_get(obj);
431    if (!wd) return;
432    if (state != wd->state)
433      {
434         wd->state = state;
435         if (wd->statep) *wd->statep = wd->state;
436         if (wd->state)
437           edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
438         else
439           edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
440      }
441 }
442
443 /**
444  * Get the state of the check object
445  *
446  * @param obj The check object
447  * @return The boolean state
448  *
449  * @ingroup Check
450  */
451 EAPI Eina_Bool
452 elm_check_state_get(const Evas_Object *obj)
453 {
454    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
455    Widget_Data *wd = elm_widget_data_get(obj);
456    if (!wd) return EINA_FALSE;
457    return wd->state;
458 }
459
460 /**
461  * Set a convenience pointer to a boolean to change
462  *
463  * This sets a pointer to a boolean, that, in addition to the check objects
464  * state will also be modified directly. To stop setting the object pointed
465  * to simply use NULL as the statep parameter. If statep is not NULL, then
466  * when this is called, the check objects state will also be modified to
467  * reflect the value of the boolean statep points to, just like calling
468  * elm_check_state_set().
469  *
470  * @param obj The check object
471  * @param statep Pointer to the boolean to modify
472  *
473  * @ingroup Check
474  */
475 EAPI void
476 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
477 {
478    ELM_CHECK_WIDTYPE(obj, widtype);
479    Widget_Data *wd = elm_widget_data_get(obj);
480    if (!wd) return;
481    if (statep)
482      {
483         wd->statep = statep;
484         if (*wd->statep != wd->state)
485           {
486              wd->state = *wd->statep;
487              if (wd->state)
488                edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
489              else
490                edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
491           }
492      }
493    else
494      wd->statep = NULL;
495 }