svn update: 48945 (latest:48959)
[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         _sizing_eval(obj);
129      }
130 }
131
132 static void
133 _signal_toggle_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
134 {
135    Widget_Data *wd = elm_widget_data_get(data);
136    if (!wd) return;
137    wd->state = 0;
138    if (wd->statep) *wd->statep = wd->state;
139    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
140 }
141
142 static void
143 _signal_toggle_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
144 {
145    Widget_Data *wd = elm_widget_data_get(data);
146    if (!wd) return;
147    wd->state = 1;
148    if (wd->statep) *wd->statep = wd->state;
149    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
150 }
151
152 /**
153  * Add a toggle to @p parent.
154  *
155  * @param parent The parent object
156  *
157  * @return The toggle object
158  *
159  * @ingroup Toggle
160  */
161 EAPI Evas_Object *
162 elm_toggle_add(Evas_Object *parent)
163 {
164    Evas_Object *obj;
165    Evas *e;
166    Widget_Data *wd;
167
168    wd = ELM_NEW(Widget_Data);
169    e = evas_object_evas_get(parent);
170    obj = elm_widget_add(e);
171    ELM_SET_WIDTYPE(widtype, "toggle");
172    elm_widget_type_set(obj, "toggle");
173    elm_widget_sub_object_add(parent, obj);
174    elm_widget_data_set(obj, wd);
175    elm_widget_del_hook_set(obj, _del_hook);
176    elm_widget_theme_hook_set(obj, _theme_hook);
177    elm_widget_disable_hook_set(obj, _disable_hook);
178
179    wd->tgl = edje_object_add(e);
180    _elm_theme_object_set(obj, wd->tgl, "toggle", "base", "default");
181    wd->ontext = eina_stringshare_add("ON");
182    wd->offtext = eina_stringshare_add("OFF");
183    edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,on", "",
184                                    _signal_toggle_on, obj);
185    edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,off", "",
186                                    _signal_toggle_off, obj);
187    elm_widget_resize_object_set(obj, wd->tgl);
188    edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
189    edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
190
191    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
192
193    _sizing_eval(obj);
194
195    // TODO: convert Elementary to subclassing of Evas_Smart_Class
196    // TODO: and save some bytes, making descriptions per-class and not instance!
197    evas_object_smart_callbacks_descriptions_set(obj, _signals);
198    return obj;
199 }
200
201 /**
202  * Sets the label to be displayed with the toggle.
203  *
204  * @param obj The toggle object
205  * @param label The label to be displayed
206  *
207  * @ingroup Toggle
208  */
209 EAPI void
210 elm_toggle_label_set(Evas_Object *obj, const char *label)
211 {
212    ELM_CHECK_WIDTYPE(obj, widtype);
213    Widget_Data *wd = elm_widget_data_get(obj);
214    if (!wd) return;
215    eina_stringshare_replace(&wd->label, label);
216    if (label)
217      edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
218    else
219      edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
220    edje_object_message_signal_process(wd->tgl);
221    edje_object_part_text_set(wd->tgl, "elm.text", label);
222    _sizing_eval(obj);
223 }
224
225 /**
226  * Gets the label of the toggle
227  *
228  * @param obj The toggle object
229  * @return The label of the toggle
230  *
231  * @ingroup Toggle
232  */
233 EAPI const char *
234 elm_toggle_label_get(const Evas_Object *obj)
235 {
236    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
237    Widget_Data *wd = elm_widget_data_get(obj);
238    if (!wd) return NULL;
239    return wd->label;
240 }
241
242 /**
243  * Sets the icon to be displayed with the toggle.
244  *
245  * @param obj The toggle object
246  * @param icon The icon object to be displayed
247  *
248  * @ingroup Toggle
249  */
250 EAPI void
251 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
252 {
253    ELM_CHECK_WIDTYPE(obj, widtype);
254    Widget_Data *wd = elm_widget_data_get(obj);
255    if (!wd) return;
256    if ((wd->icon != icon) && (wd->icon))
257      elm_widget_sub_object_del(obj, wd->icon);
258    wd->icon = icon;
259    if (!icon) return;
260    elm_widget_sub_object_add(obj, icon);
261    evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
262                                   _changed_size_hints, obj);
263    edje_object_part_swallow(wd->tgl, "elm.swallow.content", icon);
264    edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
265    _sizing_eval(obj);
266 }
267
268 /**
269  * Gets the icon of the toggle
270  *
271  * @param obj The toggle object
272  * @return The icon object
273  *
274  * @ingroup Toggle
275  */
276 EAPI Evas_Object *
277 elm_toggle_icon_get(const Evas_Object *obj)
278 {
279    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
280    Widget_Data *wd = elm_widget_data_get(obj);
281    if (!wd) return NULL;
282    return wd->icon;
283 }
284
285 /**
286  * Sets the labels to be associated with the on and off states of the toggle.
287  *
288  * @param obj The toggle object
289  * @param onlabel The label displayed when the toggle is in the "on" state
290  * @param offlabel The label displayed when the toggle is in the "off" state
291  *
292  * @ingroup Toggle
293  */
294 EAPI void
295 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
296 {
297    ELM_CHECK_WIDTYPE(obj, widtype);
298    Widget_Data *wd = elm_widget_data_get(obj);
299    if (!wd) return;
300    eina_stringshare_replace(&wd->ontext, onlabel);
301    eina_stringshare_replace(&wd->offtext, offlabel);
302    edje_object_part_text_set(wd->tgl, "elm.ontext", onlabel);
303    edje_object_part_text_set(wd->tgl, "elm.offtext", offlabel);
304    _sizing_eval(obj);
305 }
306
307
308 /**
309  * Gets the labels associated with the on and off states of the toggle.
310  *
311  * @param obj The toggle object
312  * @param onlabel A char** to place the onlabel of @p obj into
313  * @param offlabel A char** to place the offlabel of @p obj into
314  *
315  * @ingroup Toggle
316  */
317 EAPI void
318 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
319 {
320    if (onlabel) *onlabel = NULL;
321    if (offlabel) *offlabel = NULL;
322    ELM_CHECK_WIDTYPE(obj, widtype);
323    Widget_Data *wd = elm_widget_data_get(obj);
324    if (!wd) return;
325    if (onlabel) *onlabel = wd->ontext;
326    if (offlabel) *offlabel = wd->offtext;
327 }
328
329 /**
330  * Sets the state of the toggle to @p state.
331  *
332  * @param obj The toggle object
333  * @param state The state of @p obj
334  *
335  * @ingroup Toggle
336  */
337 EAPI void
338 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
339 {
340    ELM_CHECK_WIDTYPE(obj, widtype);
341    Widget_Data *wd = elm_widget_data_get(obj);
342    if (!wd) return;
343    if (state != wd->state)
344      {
345         wd->state = state;
346         if (wd->statep) *wd->statep = wd->state;
347         if (wd->state)
348           edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
349         else
350           edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
351      }
352 }
353
354 /**
355  * Gets the state of the toggle to @p state.
356  *
357  * @param obj The toggle object
358  * @return The state of @p obj
359  *
360  * @ingroup Toggle
361  */
362 EAPI Eina_Bool
363 elm_toggle_state_get(const Evas_Object *obj)
364 {
365    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
366    Widget_Data *wd = elm_widget_data_get(obj);
367    if (!wd) return EINA_FALSE;
368    return wd->state;
369 }
370
371 /**
372  * Sets the state pointer of the toggle to @p statep.
373  *
374  * @param obj The toggle object
375  * @param statep The state pointer of @p obj
376  *
377  * @ingroup Toggle
378  */
379 EAPI void
380 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
381 {
382    ELM_CHECK_WIDTYPE(obj, widtype);
383    Widget_Data *wd = elm_widget_data_get(obj);
384    if (!wd) return;
385    if (statep)
386      {
387         wd->statep = statep;
388         if (*wd->statep != wd->state)
389           {
390              wd->state = *wd->statep;
391              if (wd->state)
392                edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
393              else
394                edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
395           }
396      }
397    else
398      wd->statep = NULL;
399 }