Merge branch 'master' of 165.213.180.234:slp/pkgs/e/elementary
[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 _on_focus_hook(void *data, 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
45 static const char SIG_CHANGED[] = "changed";
46 static const char SIG_UNFOCUSED[] = "unfocused";
47 static const Evas_Smart_Cb_Description _signals[] = {
48   {SIG_CHANGED, ""},
49   {SIG_UNFOCUSED, ""},
50   {NULL, NULL}
51 };
52
53 static void
54 _del_hook(Evas_Object *obj)
55 {
56    Widget_Data *wd = elm_widget_data_get(obj);
57    if (!wd) return;
58    if (wd->label) eina_stringshare_del(wd->label);
59    free(wd);
60 }
61
62 static void
63 _theme_hook(Evas_Object *obj)
64 {
65    Widget_Data *wd = elm_widget_data_get(obj);
66    if (!wd) return;
67    _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
68    if (wd->icon)
69      edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
70    else
71      edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
72    if (wd->state)
73      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
74    else
75      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
76    if (wd->label)
77      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
78    else
79      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
80    edje_object_part_text_set(wd->chk, "elm.text", wd->label);
81    edje_object_message_signal_process(wd->chk);
82    edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
83    _sizing_eval(obj);
84 }
85
86 static void
87 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
88 {
89    Widget_Data *wd = elm_widget_data_get(obj);
90    if (!wd) return;
91    if (elm_widget_focus_get(obj))
92      {
93
94         evas_object_focus_set(wd->chk, EINA_TRUE);
95      }
96    else
97      {
98         evas_object_smart_callback_call(obj, SIG_UNFOCUSED, NULL);
99         evas_object_focus_set(wd->chk, EINA_FALSE);
100      }
101 }
102
103 static void
104 _disable_hook(Evas_Object *obj)
105 {
106    Widget_Data *wd = elm_widget_data_get(obj);
107    if (!wd) return;
108    if (elm_widget_disabled_get(obj))
109      edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
110    else
111      edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
112 }
113
114 static void
115 _sizing_eval(Evas_Object *obj)
116 {
117    Widget_Data *wd = elm_widget_data_get(obj);
118    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
119    if (!wd) return;
120    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
121    edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
122    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
123    evas_object_size_hint_min_set(obj, minw, minh);
124    evas_object_size_hint_max_set(obj, maxw, maxh);
125 }
126
127 static void
128 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
129 {
130    Widget_Data *wd = elm_widget_data_get(data);
131    if (!wd) return;
132    if (obj != wd->icon) return;
133    Evas_Coord mw, mh;
134    evas_object_size_hint_min_get(obj, &mw, &mh);
135    _sizing_eval(data);
136 }
137
138 static void
139 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
140 {
141    Widget_Data *wd = elm_widget_data_get(obj);
142    Evas_Object *sub = event_info;
143    if (!wd) return;
144    if (sub == wd->icon)
145      {
146         edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
147         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
148                                        _changed_size_hints, obj);
149         wd->icon = NULL;
150         _sizing_eval(obj);
151         edje_object_message_signal_process(wd->chk);
152      }
153 }
154
155 static void
156 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
157 {
158    Widget_Data *wd = elm_widget_data_get(data);
159    if (!wd) return;
160    wd->state = EINA_FALSE;
161    if (wd->statep) *wd->statep = wd->state;
162    edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
163    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
164 }
165
166 static void
167 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
168 {
169    Widget_Data *wd = elm_widget_data_get(data);
170    if (!wd) return;
171    wd->state = EINA_TRUE;
172    if (wd->statep) *wd->statep = wd->state;
173    edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
174    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
175 }
176
177 static void
178 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
179 {
180    Widget_Data *wd = elm_widget_data_get(data);
181    if (!wd) return;
182    wd->state = !wd->state;
183    if (wd->statep) *wd->statep = wd->state;
184    if (wd->state)
185      edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
186    else
187      edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
188    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
189 }
190
191 /**
192  * Add a new Check object
193  *
194  * @param[in] parent The parent object
195  * @return The new object or NULL if it cannot be created
196  *
197  * @ingroup Check
198  */
199 EAPI Evas_Object *
200 elm_check_add(Evas_Object *parent)
201 {
202    Evas_Object *obj;
203    Evas *e;
204    Widget_Data *wd;
205
206    wd = ELM_NEW(Widget_Data);
207    e = evas_object_evas_get(parent);
208    obj = elm_widget_add(e);
209    ELM_SET_WIDTYPE(widtype, "check");
210    elm_widget_type_set(obj, "check");
211    elm_widget_can_focus_set(obj, EINA_TRUE);
212    elm_widget_sub_object_add(parent, obj);
213    elm_widget_data_set(obj, wd);
214    elm_widget_del_hook_set(obj, _del_hook);
215    elm_widget_theme_hook_set(obj, _theme_hook);
216    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL );
217    elm_widget_disable_hook_set(obj, _disable_hook);
218
219    wd->chk = edje_object_add(e);
220    _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
221    edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
222                                    _signal_check_on, obj);
223    edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
224                                    _signal_check_off, obj);
225    edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
226                                    _signal_check_toggle, obj);
227    elm_widget_resize_object_set(obj, wd->chk);
228
229    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
230
231    _sizing_eval(obj);
232
233    // TODO: convert Elementary to subclassing of Evas_Smart_Class
234    // TODO: and save some bytes, making descriptions per-class and not instance!
235    evas_object_smart_callbacks_descriptions_set(obj, _signals);
236    return obj;
237 }
238
239 /**
240  * Set the text label of the check object
241  *
242  * @param[in] obj The check object
243  * @param[in] label The text label string in UTF-8
244  *
245  * @ingroup Check
246  */
247 EAPI void
248 elm_check_label_set(Evas_Object *obj, const char *label)
249 {
250    ELM_CHECK_WIDTYPE(obj, widtype);
251    Widget_Data *wd = elm_widget_data_get(obj);
252    if (!wd) return;
253    eina_stringshare_replace(&wd->label, label);
254    if (label)
255      edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
256    else
257      edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
258    edje_object_message_signal_process(wd->chk);
259    edje_object_part_text_set(wd->chk, "elm.text", label);
260    _sizing_eval(obj);
261 }
262
263 /**
264  * Get the text label of the check object
265  *
266  * @param[in] obj The check object
267  * @return The text label string in UTF-8
268  *
269  * @ingroup Check
270  */
271 EAPI const char *
272 elm_check_label_get(const Evas_Object *obj)
273 {
274    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
275    Widget_Data *wd = elm_widget_data_get(obj);
276    if (!wd) return NULL;
277    return wd->label;
278 }
279
280 /**
281  * Set the icon object of the check object
282  *
283  * Once the icon object is set, a previously set one will be deleted.
284  *
285  * @param[in] obj The check object
286  * @param[in] icon The icon object
287  *
288  * @ingroup Check
289  */
290 EAPI void
291 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
292 {
293    ELM_CHECK_WIDTYPE(obj, widtype);
294    Widget_Data *wd = elm_widget_data_get(obj);
295    if (!wd) return;
296    if (wd->icon == icon) return;
297    if (wd->icon) evas_object_del(wd->icon);
298    wd->icon = icon;
299    if (icon)
300      {
301         elm_widget_sub_object_add(obj, icon);
302         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
303         _changed_size_hints, obj);
304         edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
305         edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
306         edje_object_message_signal_process(wd->chk);
307      }
308    _sizing_eval(obj);
309 }
310
311 /**
312  * Get the icon object of the check object
313  *
314  * @param[in] obj The check object
315  * @return The icon object
316  *
317  * @ingroup Check
318  */
319 EAPI Evas_Object *
320 elm_check_icon_get(const Evas_Object *obj)
321 {
322    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
323    Widget_Data *wd = elm_widget_data_get(obj);
324    if (!wd) return NULL;
325    return wd->icon;
326 }
327
328 /**
329  * Set the on/off state of the check object
330  *
331  * This sets the state of the check and will also set the value if pointed to
332  * to the state supplied, but will not call any callbacks.
333  *
334  * @param[in] obj The check object
335  * @param[in] state The state to use (1 == on, 0 == off)
336  *
337  * @ingroup Check
338  */
339 EAPI void
340 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
341 {
342    ELM_CHECK_WIDTYPE(obj, widtype);
343    Widget_Data *wd = elm_widget_data_get(obj);
344    if (!wd) return;
345    if (state != wd->state)
346      {
347         wd->state = state;
348         if (wd->statep) *wd->statep = wd->state;
349         if (wd->state)
350           edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
351         else
352           edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
353      }
354 }
355
356 /**
357  * Get the state of the check object
358  *
359  * @param[in] obj The check object
360  * @return The boolean state
361  *
362  * @ingroup Check
363  */
364 EAPI Eina_Bool
365 elm_check_state_get(const Evas_Object *obj)
366 {
367    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
368    Widget_Data *wd = elm_widget_data_get(obj);
369    if (!wd) return EINA_FALSE;
370    return wd->state;
371 }
372
373 /**
374  * Set a convenience pointer to a boolean to change
375  *
376  * This sets a pointer to a boolean, that, in addition to the check objects
377  * state will also be modified directly. To stop setting the object pointed
378  * to simply use NULL as the statep parameter. If statep is not NULL, then
379  * when this is called, the check objects state will also be modified to
380  * reflect the value of the boolean statep points to, just like calling
381  * elm_check_state_set().
382  *
383  * @param[in] obj The check object
384  * @param[in] statep Pointer to the boolean to modify
385  *
386  * @ingroup Check
387  */
388 EAPI void
389 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
390 {
391    ELM_CHECK_WIDTYPE(obj, widtype);
392    Widget_Data *wd = elm_widget_data_get(obj);
393    if (!wd) return;
394    if (statep)
395      {
396         wd->statep = statep;
397         if (*wd->statep != wd->state)
398           {
399              wd->state = *wd->statep;
400              if (wd->state)
401                edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
402              else
403                edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
404           }
405      }
406    else
407      wd->statep = NULL;
408 }