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