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