Merge branch 'master' into svn_merge
[framework/uifw/elementary.git] / src / lib / elm_toggle.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Toggle Toggle
6  * @ingroup Elementary
7  *
8  * A toggle is a slider which can be used to toggle between
9  * two values.  It has two states: on and off.
10  *
11  * Signals that you can add callbacks for are:
12  *
13  * "changed" - Whenever the toggle value has been changed.  Is not called until
14  *             the toggle is released by the cursor (assuming it has been
15  *             triggered by the cursor in the first place).
16  */
17
18 typedef struct _Widget_Data Widget_Data;
19
20 struct _Widget_Data
21 {
22    Evas_Object *tgl;
23    Evas_Object *icon;
24    Eina_Bool state;
25    Eina_Bool *statep;
26    const char *label;
27    const char *ontext, *offtext;
28 };
29
30 static const char *widtype = NULL;
31 static void _del_hook(Evas_Object *obj);
32 static void _disable_hook(Evas_Object *obj);
33 static void _theme_hook(Evas_Object *obj);
34 static void _sizing_eval(Evas_Object *obj);
35 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
36 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
37 static void _signal_toggle_off(void *data, Evas_Object *obj, const char *emission, const char *source);
38 static void _signal_toggle_on(void *data, Evas_Object *obj, const char *emission, const char *source);
39 static void _on_focus_hook(void *data, Evas_Object *obj);
40 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
41                              Evas_Callback_Type type, void *event_info);
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 Eina_Bool
50 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
51 {
52    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
53    Evas_Event_Key_Down *ev = event_info;
54    Widget_Data *wd = elm_widget_data_get(obj);
55    if (!wd) return EINA_FALSE;
56    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
57    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
58    if ((strcmp(ev->keyname, "Return")) &&
59        (strcmp(ev->keyname, "KP_Enter")) &&
60        (strcmp(ev->keyname, "space")))
61      return EINA_FALSE;
62    elm_toggle_state_set(obj, !wd->state);
63    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
64    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
65    return EINA_TRUE;
66 }
67
68 static void
69 _del_hook(Evas_Object *obj)
70 {
71    Widget_Data *wd = elm_widget_data_get(obj);
72    if (!wd) return;
73    if (wd->label) eina_stringshare_del(wd->label);
74    if (wd->ontext) eina_stringshare_del(wd->ontext);
75    if (wd->offtext) eina_stringshare_del(wd->offtext);
76    free(wd);
77 }
78
79 static void
80 _disable_hook(Evas_Object *obj)
81 {
82    Widget_Data *wd = elm_widget_data_get(obj);
83    if (!wd) return;
84    if (elm_widget_disabled_get(obj))
85      edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
86    else
87      edje_object_signal_emit(wd->tgl, "elm,state,enabled", "elm");
88 }
89
90 static void
91 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
92 {
93    Widget_Data *wd = elm_widget_data_get(obj);
94    if (!wd) return;
95    if (elm_widget_focus_get(obj))
96      {
97         edje_object_signal_emit(wd->tgl, "elm,action,focus", "elm");
98         evas_object_focus_set(wd->tgl, EINA_TRUE);
99      }
100    else
101      {
102         edje_object_signal_emit(wd->tgl, "elm,action,unfocus", "elm");
103         evas_object_focus_set(wd->tgl, EINA_FALSE);
104      }
105 }
106
107 static void
108 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
109 {
110    Widget_Data *wd = elm_widget_data_get(obj);
111    if (!wd) return;
112    edje_object_mirrored_set(wd->tgl, rtl);
113 }
114
115 static void
116 _theme_hook(Evas_Object *obj)
117 {
118    Widget_Data *wd = elm_widget_data_get(obj);
119    if (!wd) return;
120    _elm_widget_mirrored_reload(obj);
121    _mirrored_set(obj, elm_widget_mirrored_get(obj));
122    _elm_theme_object_set(obj, wd->tgl, "toggle", "base", elm_widget_style_get(obj));
123    if (wd->icon)
124      edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
125    else
126      edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
127    if (wd->state)
128      edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
129    else
130      edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
131    if (wd->label)
132      edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
133    else
134      edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
135    edje_object_part_text_set(wd->tgl, "elm.text", wd->label);
136    edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
137    edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
138    if (elm_widget_disabled_get(obj))
139      edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
140    edje_object_message_signal_process(wd->tgl);
141    edje_object_scale_set(wd->tgl, elm_widget_scale_get(obj) * _elm_config->scale);
142    _sizing_eval(obj);
143 }
144
145 static void
146 _sizing_eval(Evas_Object *obj)
147 {
148    Widget_Data *wd = elm_widget_data_get(obj);
149    Evas_Coord minw = -1, minh = -1;
150
151    if (!wd) return;
152    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
153    edje_object_size_min_restricted_calc(wd->tgl, &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, -1, -1);
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    _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->tgl, "elm,state,icon,hidden", "elm");
177         evas_object_event_callback_del_full
178            (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
179         wd->icon = NULL;
180         edje_object_message_signal_process(wd->tgl);
181         _sizing_eval(obj);
182      }
183 }
184
185 static void
186 _signal_toggle_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 = 0;
191    if (wd->statep) *wd->statep = wd->state;
192    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
193 }
194
195 static void
196 _signal_toggle_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
197 {
198    Widget_Data *wd = elm_widget_data_get(data);
199    if (!wd) return;
200    wd->state = 1;
201    if (wd->statep) *wd->statep = wd->state;
202    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
203 }
204
205 static void
206 _elm_toggle_label_set(Evas_Object *obj, const char *item, const char *label)
207 {
208    ELM_CHECK_WIDTYPE(obj, widtype);
209    Widget_Data *wd = elm_widget_data_get(obj);
210    if (item && strcmp(item, "default")) return;
211    if (!wd) return;
212    eina_stringshare_replace(&wd->label, label);
213    if (label)
214      edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
215    else
216      edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
217    edje_object_message_signal_process(wd->tgl);
218    edje_object_part_text_set(wd->tgl, "elm.text", label);
219    _sizing_eval(obj);
220 }
221
222 static const char *
223 _elm_toggle_label_get(const Evas_Object *obj, const char *item)
224 {
225    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
226    Widget_Data *wd = elm_widget_data_get(obj);
227    if (item && strcmp(item, "default")) return NULL;
228    if (!wd) return NULL;
229    return wd->label;
230 }
231
232 /**
233  * Add a toggle to @p parent.
234  *
235  * @param parent The parent object
236  *
237  * @return The toggle object
238  *
239  * @ingroup Toggle
240  */
241 EAPI Evas_Object *
242 elm_toggle_add(Evas_Object *parent)
243 {
244    Evas_Object *obj;
245    Evas *e;
246    Widget_Data *wd;
247
248    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
249
250    ELM_SET_WIDTYPE(widtype, "toggle");
251    elm_widget_type_set(obj, "toggle");
252    elm_widget_sub_object_add(parent, obj);
253    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
254    elm_widget_data_set(obj, wd);
255    elm_widget_del_hook_set(obj, _del_hook);
256    elm_widget_theme_hook_set(obj, _theme_hook);
257    elm_widget_disable_hook_set(obj, _disable_hook);
258    elm_widget_can_focus_set(obj, EINA_TRUE);
259    elm_widget_event_hook_set(obj, _event_hook);
260    elm_widget_text_set_hook_set(obj, _elm_toggle_label_set);
261    elm_widget_text_get_hook_set(obj, _elm_toggle_label_get);
262
263    wd->tgl = edje_object_add(e);
264    _mirrored_set(obj, elm_widget_mirrored_get(obj));
265    _elm_theme_object_set(obj, wd->tgl, "toggle", "base", "default");
266    wd->ontext = eina_stringshare_add("ON");
267    wd->offtext = eina_stringshare_add("OFF");
268    edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,on", "",
269                                    _signal_toggle_on, obj);
270    edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,off", "",
271                                    _signal_toggle_off, obj);
272    elm_widget_resize_object_set(obj, wd->tgl);
273    edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
274    edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
275
276    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
277    edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
278
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  * Sets the label to be displayed with the toggle.
289  *
290  * @param obj The toggle object
291  * @param label The label to be displayed
292  *
293  * @ingroup Toggle
294  * @deprecate use elm_object_text_* instead.
295  */
296 EAPI void
297 elm_toggle_label_set(Evas_Object *obj, const char *label)
298 {
299    _elm_toggle_label_set(obj, NULL, label);
300 }
301
302 /**
303  * Gets the label of the toggle
304  *
305  * @param obj  toggleeee object
306  * @return The label of the toggle
307  *
308  * @ingroup Toggle
309  * @deprecate use elm_object_text_* instead.
310  */
311 EAPI const char *
312 elm_toggle_label_get(const Evas_Object *obj)
313 {
314    return _elm_toggle_label_get(obj, NULL);
315 }
316
317 /**
318  * Set the icon used for the toggle
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_toggle_icon_unset() function.
323  *
324  * @param obj The toggle object
325  * @param icon The icon object for the button
326  *
327  * @ingroup Toggle
328  */
329 EAPI void
330 elm_toggle_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->tgl, "elm.swallow.content", icon);
344         edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
345         edje_object_message_signal_process(wd->tgl);
346      }
347    _sizing_eval(obj);
348 }
349
350 /**
351  * Get the icon used for the toggle
352  *
353  * Return the icon object which is set for this widget.
354  *
355  * @param obj The toggle object
356  * @return The icon object that is being used
357  *
358  * @ingroup Toggle
359  */
360 EAPI Evas_Object *
361 elm_toggle_icon_get(const Evas_Object *obj)
362 {
363    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
364    Widget_Data *wd = elm_widget_data_get(obj);
365    if (!wd) return NULL;
366    return wd->icon;
367 }
368
369 /**
370  * Unset the icon used for the toggle
371  *
372  * Unparent and return the icon object which was set for this widget.
373  *
374  * @param obj The toggle object
375  * @return The icon object that was being used
376  *
377  * @ingroup Toggle
378  */
379 EAPI Evas_Object *
380 elm_toggle_icon_unset(Evas_Object *obj)
381 {
382    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
383    Widget_Data *wd = elm_widget_data_get(obj);
384    if (!wd) return NULL;
385    if (!wd->icon) return NULL;
386    Evas_Object *icon = wd->icon;
387    elm_widget_sub_object_del(obj, wd->icon);
388    edje_object_part_unswallow(wd->tgl, wd->icon);
389    wd->icon = NULL;
390    return icon;
391 }
392
393 /**
394  * Sets the labels to be associated with the on and off states of the toggle.
395  *
396  * @param obj The toggle object
397  * @param onlabel The label displayed when the toggle is in the "on" state
398  * @param offlabel The label displayed when the toggle is in the "off" state
399  *
400  * @ingroup Toggle
401  */
402 EAPI void
403 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
404 {
405    ELM_CHECK_WIDTYPE(obj, widtype);
406    Widget_Data *wd = elm_widget_data_get(obj);
407    if (!wd) return;
408    eina_stringshare_replace(&wd->ontext, onlabel);
409    eina_stringshare_replace(&wd->offtext, offlabel);
410    edje_object_part_text_set(wd->tgl, "elm.ontext", onlabel);
411    edje_object_part_text_set(wd->tgl, "elm.offtext", offlabel);
412    _sizing_eval(obj);
413 }
414
415
416 /**
417  * Gets the labels associated with the on and off states of the toggle.
418  *
419  * @param obj The toggle object
420  * @param onlabel A char** to place the onlabel of @p obj into
421  * @param offlabel A char** to place the offlabel of @p obj into
422  *
423  * @ingroup Toggle
424  */
425 EAPI void
426 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
427 {
428    if (onlabel) *onlabel = NULL;
429    if (offlabel) *offlabel = NULL;
430    ELM_CHECK_WIDTYPE(obj, widtype);
431    Widget_Data *wd = elm_widget_data_get(obj);
432    if (!wd) return;
433    if (onlabel) *onlabel = wd->ontext;
434    if (offlabel) *offlabel = wd->offtext;
435 }
436
437 /**
438  * Sets the state of the toggle to @p state.
439  *
440  * @param obj The toggle object
441  * @param state The state of @p obj
442  *
443  * @ingroup Toggle
444  */
445 EAPI void
446 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
447 {
448    ELM_CHECK_WIDTYPE(obj, widtype);
449    Widget_Data *wd = elm_widget_data_get(obj);
450    if (!wd) return;
451    if (state != wd->state)
452      {
453         wd->state = state;
454         if (wd->statep) *wd->statep = wd->state;
455         if (wd->state)
456           edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
457         else
458           edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
459      }
460 }
461
462 /**
463  * Gets the state of the toggle to @p state.
464  *
465  * @param obj The toggle object
466  * @return The state of @p obj
467  *
468  * @ingroup Toggle
469  */
470 EAPI Eina_Bool
471 elm_toggle_state_get(const Evas_Object *obj)
472 {
473    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
474    Widget_Data *wd = elm_widget_data_get(obj);
475    if (!wd) return EINA_FALSE;
476    return wd->state;
477 }
478
479 /**
480  * Sets the state pointer of the toggle to @p statep.
481  *
482  * @param obj The toggle object
483  * @param statep The state pointer of @p obj
484  *
485  * @ingroup Toggle
486  */
487 EAPI void
488 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
489 {
490    ELM_CHECK_WIDTYPE(obj, widtype);
491    Widget_Data *wd = elm_widget_data_get(obj);
492    if (!wd) return;
493    if (statep)
494      {
495         wd->statep = statep;
496         if (*wd->statep != wd->state)
497           {
498              wd->state = *wd->statep;
499              if (wd->state)
500                edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
501              else
502                edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
503           }
504      }
505    else
506      wd->statep = NULL;
507 }