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