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