3a7b479f49cea30c2a5d16e5f37d0c57ce4a0add
[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
14  * until the toggle is released by the cursor (assuming it has been triggered
15  * 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
40 static const char SIG_CHANGED[] = "changed";
41 static const Evas_Smart_Cb_Description _signals[] = {
42   {SIG_CHANGED, ""},
43   {NULL, NULL}
44 };
45
46 static void
47 _del_hook(Evas_Object *obj)
48 {
49    Widget_Data *wd = elm_widget_data_get(obj);
50    if (!wd) return;
51    if (wd->label) eina_stringshare_del(wd->label);
52    if (wd->ontext) eina_stringshare_del(wd->ontext);
53    if (wd->offtext) eina_stringshare_del(wd->offtext);
54    free(wd);
55 }
56
57 static void
58 _disable_hook(Evas_Object *obj)
59 {
60    Widget_Data *wd = elm_widget_data_get(obj);
61    if (!wd) return;
62    if (elm_widget_disabled_get(obj))
63      edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
64    else
65      edje_object_signal_emit(wd->tgl, "elm,state,enabled", "elm");
66 }
67
68 static void
69 _theme_hook(Evas_Object *obj)
70 {
71    Widget_Data *wd = elm_widget_data_get(obj);
72    if (!wd) return;
73    _elm_theme_object_set(obj, wd->tgl, "toggle", "base", elm_widget_style_get(obj));
74    if (wd->icon)
75      edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
76    else
77      edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
78    if (wd->state)
79      edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
80    else
81      edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
82    if (wd->label)
83      edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
84    else
85      edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
86    edje_object_part_text_set(wd->tgl, "elm.text", wd->label);
87    edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
88    edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
89    edje_object_message_signal_process(wd->tgl);
90    edje_object_scale_set(wd->tgl, elm_widget_scale_get(obj) * _elm_config->scale);
91    _sizing_eval(obj);
92 }
93
94 static void
95 _sizing_eval(Evas_Object *obj)
96 {
97    Widget_Data *wd = elm_widget_data_get(obj);
98    Evas_Coord minw = -1, minh = -1;
99
100    if (!wd) return;
101    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
102    edje_object_size_min_restricted_calc(wd->tgl, &minw, &minh, minw, minh);
103    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
104    evas_object_size_hint_min_set(obj, minw, minh);
105    evas_object_size_hint_max_set(obj, -1, -1);
106 }
107
108 static void
109 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
110 {
111    Widget_Data *wd = elm_widget_data_get(data);
112    if (!wd) return;
113    if (obj != wd->icon) return;
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->tgl, "elm,state,icon,hidden", "elm");
126         evas_object_event_callback_del_full
127           (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
128         wd->icon = NULL;
129         edje_object_message_signal_process(wd->tgl);
130         _sizing_eval(obj);
131      }
132 }
133
134 static void
135 _signal_toggle_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
136 {
137    Widget_Data *wd = elm_widget_data_get(data);
138    if (!wd) return;
139    wd->state = 0;
140    if (wd->statep) *wd->statep = wd->state;
141    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
142 }
143
144 static void
145 _signal_toggle_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 = 1;
150    if (wd->statep) *wd->statep = wd->state;
151    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
152 }
153
154 /**
155  * Add a toggle to @p parent.
156  *
157  * @param[in] parent The parent object
158  *
159  * @return The toggle object
160  *
161  * @ingroup Toggle
162  */
163 EAPI Evas_Object *
164 elm_toggle_add(Evas_Object *parent)
165 {
166    Evas_Object *obj;
167    Evas *e;
168    Widget_Data *wd;
169
170    wd = ELM_NEW(Widget_Data);
171    e = evas_object_evas_get(parent);
172    obj = elm_widget_add(e);
173    ELM_SET_WIDTYPE(widtype, "toggle");
174    elm_widget_type_set(obj, "toggle");
175    elm_widget_sub_object_add(parent, obj);
176    elm_widget_data_set(obj, wd);
177    elm_widget_del_hook_set(obj, _del_hook);
178    elm_widget_theme_hook_set(obj, _theme_hook);
179    elm_widget_disable_hook_set(obj, _disable_hook);
180
181    wd->tgl = edje_object_add(e);
182    _elm_theme_object_set(obj, wd->tgl, "toggle", "base", "default");
183    wd->ontext = eina_stringshare_add("ON");
184    wd->offtext = eina_stringshare_add("OFF");
185    edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,on", "",
186                                    _signal_toggle_on, obj);
187    edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,off", "",
188                                    _signal_toggle_off, obj);
189    elm_widget_resize_object_set(obj, wd->tgl);
190    edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
191    edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
192
193    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
194
195    _sizing_eval(obj);
196
197    // TODO: convert Elementary to subclassing of Evas_Smart_Class
198    // TODO: and save some bytes, making descriptions per-class and not instance!
199    evas_object_smart_callbacks_descriptions_set(obj, _signals);
200    return obj;
201 }
202
203 /**
204  * Sets the label to be displayed with the toggle.
205  *
206  * @param[in] obj The toggle object
207  * @param[in] label The label to be displayed
208  *
209  * @ingroup Toggle
210  */
211 EAPI void
212 elm_toggle_label_set(Evas_Object *obj, const char *label)
213 {
214    ELM_CHECK_WIDTYPE(obj, widtype);
215    Widget_Data *wd = elm_widget_data_get(obj);
216    if (!wd) return;
217    eina_stringshare_replace(&wd->label, label);
218    if (label)
219      edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
220    else
221      edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
222    edje_object_message_signal_process(wd->tgl);
223    edje_object_part_text_set(wd->tgl, "elm.text", label);
224    _sizing_eval(obj);
225 }
226
227 /**
228  * Gets the label of the toggle
229  *
230  * @param[in] obj The toggle object
231  * @return The label of the toggle
232  *
233  * @ingroup Toggle
234  */
235 EAPI const char *
236 elm_toggle_label_get(const Evas_Object *obj)
237 {
238    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
239    Widget_Data *wd = elm_widget_data_get(obj);
240    if (!wd) return NULL;
241    return wd->label;
242 }
243
244 /**
245  * Sets the icon to be displayed with the toggle.
246  *
247  * Once the icon object is set, a previously set one will be deleted.
248  *
249  * @param[in] obj The toggle object
250  * @param[in] icon The icon object to be displayed
251  *
252  * @ingroup Toggle
253  */
254 EAPI void
255 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
256 {
257    ELM_CHECK_WIDTYPE(obj, widtype);
258    Widget_Data *wd = elm_widget_data_get(obj);
259    if (!wd) return;
260    if (wd->icon == icon) return;
261    if (wd->icon) evas_object_del(wd->icon);
262    wd->icon = icon;
263    if (icon)
264      {
265         elm_widget_sub_object_add(obj, icon);
266         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
267               _changed_size_hints, obj);
268         edje_object_part_swallow(wd->tgl, "elm.swallow.content", icon);
269         edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
270         edje_object_message_signal_process(wd->tgl);
271      }
272    _sizing_eval(obj);
273 }
274
275 /**
276  * Gets the icon of the toggle
277  *
278  * @param[in] obj The toggle object
279  * @return The icon object
280  *
281  * @ingroup Toggle
282  */
283 EAPI Evas_Object *
284 elm_toggle_icon_get(const Evas_Object *obj)
285 {
286    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
287    Widget_Data *wd = elm_widget_data_get(obj);
288    if (!wd) return NULL;
289    return wd->icon;
290 }
291
292 /**
293  * Sets the labels to be associated with the on and off states of the toggle.
294  *
295  * @param[in] obj The toggle object
296  * @param[in] onlabel The label displayed when the toggle is in the "on" state
297  * @param[in] offlabel The label displayed when the toggle is in the "off" state
298  *
299  * @ingroup Toggle
300  */
301 EAPI void
302 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
303 {
304    ELM_CHECK_WIDTYPE(obj, widtype);
305    Widget_Data *wd = elm_widget_data_get(obj);
306    if (!wd) return;
307    eina_stringshare_replace(&wd->ontext, onlabel);
308    eina_stringshare_replace(&wd->offtext, offlabel);
309    edje_object_part_text_set(wd->tgl, "elm.ontext", onlabel);
310    edje_object_part_text_set(wd->tgl, "elm.offtext", offlabel);
311    _sizing_eval(obj);
312 }
313
314
315 /**
316  * Gets the labels associated with the on and off states of the toggle.
317  *
318  * @param[in] obj The toggle object
319  * @param[in] onlabel A char** to place the onlabel of @p obj into
320  * @param[in] offlabel A char** to place the offlabel of @p obj into
321  *
322  * @ingroup Toggle
323  */
324 EAPI void
325 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
326 {
327    if (onlabel) *onlabel = NULL;
328    if (offlabel) *offlabel = NULL;
329    ELM_CHECK_WIDTYPE(obj, widtype);
330    Widget_Data *wd = elm_widget_data_get(obj);
331    if (!wd) return;
332    if (onlabel) *onlabel = wd->ontext;
333    if (offlabel) *offlabel = wd->offtext;
334 }
335
336 /**
337  * Sets the state of the toggle to @p state.
338  *
339  * @param[in] obj The toggle object
340  * @param[in] state The state of @p obj
341  *
342  * @ingroup Toggle
343  */
344 EAPI void
345 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
346 {
347    ELM_CHECK_WIDTYPE(obj, widtype);
348    Widget_Data *wd = elm_widget_data_get(obj);
349    if (!wd) return;
350    if (state != wd->state)
351      {
352         wd->state = state;
353         if (wd->statep) *wd->statep = wd->state;
354         if (wd->state)
355           edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
356         else
357           edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
358      }
359 }
360
361 /**
362  * Gets the state of the toggle to @p state.
363  *
364  * @param[in] obj The toggle object
365  * @return The state of @p obj
366  *
367  * @ingroup Toggle
368  */
369 EAPI Eina_Bool
370 elm_toggle_state_get(const Evas_Object *obj)
371 {
372    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
373    Widget_Data *wd = elm_widget_data_get(obj);
374    if (!wd) return EINA_FALSE;
375    return wd->state;
376 }
377
378 /**
379  * Sets the state pointer of the toggle to @p statep.
380  *
381  * @param[in] obj The toggle object
382  * @param[in] statep The state pointer of @p obj
383  *
384  * @ingroup Toggle
385  */
386 EAPI void
387 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
388 {
389    ELM_CHECK_WIDTYPE(obj, widtype);
390    Widget_Data *wd = elm_widget_data_get(obj);
391    if (!wd) return;
392    if (statep)
393      {
394         wd->statep = statep;
395         if (*wd->statep != wd->state)
396           {
397              wd->state = *wd->statep;
398              if (wd->state)
399                edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
400              else
401                edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
402           }
403      }
404    else
405      wd->statep = NULL;
406 }