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