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