Elementary: Rename elm_object_label_* -> elm_object_text_part_*.
[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 until
13  *             the toggle is released by the cursor (assuming it has been
14  *             triggered 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 static void _on_focus_hook(void *data, Evas_Object *obj);
39 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
40                              Evas_Callback_Type type, void *event_info);
41
42 static const char SIG_CHANGED[] = "changed";
43 static const Evas_Smart_Cb_Description _signals[] = {
44        {SIG_CHANGED, ""},
45        {NULL, NULL}
46 };
47
48 static Eina_Bool
49 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
50 {
51    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
52    Evas_Event_Key_Down *ev = event_info;
53    Widget_Data *wd = elm_widget_data_get(obj);
54    if (!wd) return EINA_FALSE;
55    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
56    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
57    if ((strcmp(ev->keyname, "Return")) &&
58        (strcmp(ev->keyname, "KP_Enter")) &&
59        (strcmp(ev->keyname, "space")))
60      return EINA_FALSE;
61    elm_toggle_state_set(obj, !wd->state);
62    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
63    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
64    return EINA_TRUE;
65 }
66
67 static void
68 _del_hook(Evas_Object *obj)
69 {
70    Widget_Data *wd = elm_widget_data_get(obj);
71    if (!wd) return;
72    if (wd->label) eina_stringshare_del(wd->label);
73    if (wd->ontext) eina_stringshare_del(wd->ontext);
74    if (wd->offtext) eina_stringshare_del(wd->offtext);
75    free(wd);
76 }
77
78 static void
79 _disable_hook(Evas_Object *obj)
80 {
81    Widget_Data *wd = elm_widget_data_get(obj);
82    if (!wd) return;
83    if (elm_widget_disabled_get(obj))
84      edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
85    else
86      edje_object_signal_emit(wd->tgl, "elm,state,enabled", "elm");
87 }
88
89 static void
90 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
91 {
92    Widget_Data *wd = elm_widget_data_get(obj);
93    if (!wd) return;
94    if (elm_widget_focus_get(obj))
95      {
96         edje_object_signal_emit(wd->tgl, "elm,action,focus", "elm");
97         evas_object_focus_set(wd->tgl, EINA_TRUE);
98      }
99    else
100      {
101         edje_object_signal_emit(wd->tgl, "elm,action,unfocus", "elm");
102         evas_object_focus_set(wd->tgl, EINA_FALSE);
103      }
104 }
105
106 static void
107 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
108 {
109    Widget_Data *wd = elm_widget_data_get(obj);
110    if (!wd) return;
111    edje_object_mirrored_set(wd->tgl, rtl);
112 }
113
114 static void
115 _theme_hook(Evas_Object *obj)
116 {
117    Widget_Data *wd = elm_widget_data_get(obj);
118    if (!wd) return;
119    _elm_widget_mirrored_reload(obj);
120    _mirrored_set(obj, elm_widget_mirrored_get(obj));
121    _elm_theme_object_set(obj, wd->tgl, "toggle", "base", elm_widget_style_get(obj));
122    if (wd->icon)
123      edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
124    else
125      edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
126    if (wd->state)
127      edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
128    else
129      edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
130    if (wd->label)
131      edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
132    else
133      edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
134    edje_object_part_text_set(wd->tgl, "elm.text", wd->label);
135    edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
136    edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
137    if (elm_widget_disabled_get(obj))
138      edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
139    edje_object_message_signal_process(wd->tgl);
140    edje_object_scale_set(wd->tgl, elm_widget_scale_get(obj) * _elm_config->scale);
141    _sizing_eval(obj);
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;
149
150    if (!wd) return;
151    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
152    edje_object_size_min_restricted_calc(wd->tgl, &minw, &minh, minw, minh);
153    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
154    evas_object_size_hint_min_set(obj, minw, minh);
155    evas_object_size_hint_max_set(obj, -1, -1);
156 }
157
158 static void
159 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
160 {
161    Widget_Data *wd = elm_widget_data_get(data);
162    if (!wd) return;
163    if (obj != wd->icon) return;
164    _sizing_eval(data);
165 }
166
167 static void
168 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
169 {
170    Widget_Data *wd = elm_widget_data_get(obj);
171    Evas_Object *sub = event_info;
172    if (!wd) return;
173    if (sub == wd->icon)
174      {
175         edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
176         evas_object_event_callback_del_full
177            (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
178         wd->icon = NULL;
179         edje_object_message_signal_process(wd->tgl);
180         _sizing_eval(obj);
181      }
182 }
183
184 static void
185 _signal_toggle_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
186 {
187    Widget_Data *wd = elm_widget_data_get(data);
188    if (!wd) return;
189    wd->state = 0;
190    if (wd->statep) *wd->statep = wd->state;
191    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
192 }
193
194 static void
195 _signal_toggle_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
196 {
197    Widget_Data *wd = elm_widget_data_get(data);
198    if (!wd) return;
199    wd->state = 1;
200    if (wd->statep) *wd->statep = wd->state;
201    evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
202 }
203
204 static void
205 _elm_toggle_label_set(Evas_Object *obj, const char *item, const char *label)
206 {
207    ELM_CHECK_WIDTYPE(obj, widtype);
208    Widget_Data *wd = elm_widget_data_get(obj);
209    if (item) return;
210    if (!wd) return;
211    eina_stringshare_replace(&wd->label, label);
212    if (label)
213      edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
214    else
215      edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
216    edje_object_message_signal_process(wd->tgl);
217    edje_object_part_text_set(wd->tgl, "elm.text", label);
218    _sizing_eval(obj);
219 }
220
221 static const char *
222 _elm_toggle_label_get(const Evas_Object *obj, const char *item)
223 {
224    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
225    Widget_Data *wd = elm_widget_data_get(obj);
226    if (item) return NULL;
227    if (!wd) return NULL;
228    return wd->label;
229 }
230
231 /**
232  * Add a toggle to @p parent.
233  *
234  * @param parent The parent object
235  *
236  * @return The toggle object
237  *
238  * @ingroup Toggle
239  */
240 EAPI Evas_Object *
241 elm_toggle_add(Evas_Object *parent)
242 {
243    Evas_Object *obj;
244    Evas *e;
245    Widget_Data *wd;
246
247    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
248
249    ELM_SET_WIDTYPE(widtype, "toggle");
250    elm_widget_type_set(obj, "toggle");
251    elm_widget_sub_object_add(parent, obj);
252    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
253    elm_widget_data_set(obj, wd);
254    elm_widget_del_hook_set(obj, _del_hook);
255    elm_widget_theme_hook_set(obj, _theme_hook);
256    elm_widget_disable_hook_set(obj, _disable_hook);
257    elm_widget_can_focus_set(obj, EINA_TRUE);
258    elm_widget_event_hook_set(obj, _event_hook);
259    elm_widget_text_set_hook_set(obj, _elm_toggle_label_set);
260    elm_widget_text_get_hook_set(obj, _elm_toggle_label_get);
261
262    wd->tgl = edje_object_add(e);
263    _mirrored_set(obj, elm_widget_mirrored_get(obj));
264    _elm_theme_object_set(obj, wd->tgl, "toggle", "base", "default");
265    wd->ontext = eina_stringshare_add("ON");
266    wd->offtext = eina_stringshare_add("OFF");
267    edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,on", "",
268                                    _signal_toggle_on, obj);
269    edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,off", "",
270                                    _signal_toggle_off, obj);
271    elm_widget_resize_object_set(obj, wd->tgl);
272    edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
273    edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
274
275    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
276    edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
277
278    _sizing_eval(obj);
279
280    // TODO: convert Elementary to subclassing of Evas_Smart_Class
281    // TODO: and save some bytes, making descriptions per-class and not instance!
282    evas_object_smart_callbacks_descriptions_set(obj, _signals);
283    return obj;
284 }
285
286 /**
287  * Sets the label to be displayed with the toggle.
288  *
289  * @param obj The toggle object
290  * @param label The label to be displayed
291  *
292  * @ingroup Toggle
293  * @deprecate use elm_object_text_* instead.
294  */
295 EAPI void
296 elm_toggle_label_set(Evas_Object *obj, const char *label)
297 {
298    _elm_toggle_label_set(obj, NULL, label);
299 }
300
301 /**
302  * Gets the label of the toggle
303  *
304  * @param obj  toggleeee object
305  * @return The label of the toggle
306  *
307  * @ingroup Toggle
308  * @deprecate use elm_object_text_* instead.
309  */
310 EAPI const char *
311 elm_toggle_label_get(const Evas_Object *obj)
312 {
313    return _elm_toggle_label_get(obj, NULL);
314 }
315
316 /**
317  * Set the icon used for the toggle
318  *
319  * Once the icon object is set, a previously set one will be deleted
320  * If you want to keep that old content object, use the
321  * elm_toggle_icon_unset() function.
322  *
323  * @param obj The toggle object
324  * @param icon The icon object for the button
325  *
326  * @ingroup Toggle
327  */
328 EAPI void
329 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
330 {
331    ELM_CHECK_WIDTYPE(obj, widtype);
332    Widget_Data *wd = elm_widget_data_get(obj);
333    if (!wd) return;
334    if (wd->icon == icon) return;
335    if (wd->icon) evas_object_del(wd->icon);
336    wd->icon = icon;
337    if (icon)
338      {
339         elm_widget_sub_object_add(obj, icon);
340         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
341                                        _changed_size_hints, obj);
342         edje_object_part_swallow(wd->tgl, "elm.swallow.content", icon);
343         edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
344         edje_object_message_signal_process(wd->tgl);
345      }
346    _sizing_eval(obj);
347 }
348
349 /**
350  * Get the icon used for the toggle
351  *
352  * Return the icon object which is set for this widget.
353  *
354  * @param obj The toggle object
355  * @return The icon object that is being used
356  *
357  * @ingroup Toggle
358  */
359 EAPI Evas_Object *
360 elm_toggle_icon_get(const Evas_Object *obj)
361 {
362    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
363    Widget_Data *wd = elm_widget_data_get(obj);
364    if (!wd) return NULL;
365    return wd->icon;
366 }
367
368 /**
369  * Unset the icon used for the toggle
370  *
371  * Unparent and return the icon object which was set for this widget.
372  *
373  * @param obj The toggle object
374  * @return The icon object that was being used
375  *
376  * @ingroup Toggle
377  */
378 EAPI Evas_Object *
379 elm_toggle_icon_unset(Evas_Object *obj)
380 {
381    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
382    Widget_Data *wd = elm_widget_data_get(obj);
383    if (!wd) return NULL;
384    if (!wd->icon) return NULL;
385    Evas_Object *icon = wd->icon;
386    elm_widget_sub_object_del(obj, wd->icon);
387    edje_object_part_unswallow(wd->tgl, wd->icon);
388    wd->icon = NULL;
389    return icon;
390 }
391
392 /**
393  * Sets the labels to be associated with the on and off states of the toggle.
394  *
395  * @param obj The toggle object
396  * @param onlabel The label displayed when the toggle is in the "on" state
397  * @param offlabel The label displayed when the toggle is in the "off" state
398  *
399  * @ingroup Toggle
400  */
401 EAPI void
402 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
403 {
404    ELM_CHECK_WIDTYPE(obj, widtype);
405    Widget_Data *wd = elm_widget_data_get(obj);
406    if (!wd) return;
407    eina_stringshare_replace(&wd->ontext, onlabel);
408    eina_stringshare_replace(&wd->offtext, offlabel);
409    edje_object_part_text_set(wd->tgl, "elm.ontext", onlabel);
410    edje_object_part_text_set(wd->tgl, "elm.offtext", offlabel);
411    _sizing_eval(obj);
412 }
413
414
415 /**
416  * Gets the labels associated with the on and off states of the toggle.
417  *
418  * @param obj The toggle object
419  * @param onlabel A char** to place the onlabel of @p obj into
420  * @param offlabel A char** to place the offlabel of @p obj into
421  *
422  * @ingroup Toggle
423  */
424 EAPI void
425 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
426 {
427    if (onlabel) *onlabel = NULL;
428    if (offlabel) *offlabel = NULL;
429    ELM_CHECK_WIDTYPE(obj, widtype);
430    Widget_Data *wd = elm_widget_data_get(obj);
431    if (!wd) return;
432    if (onlabel) *onlabel = wd->ontext;
433    if (offlabel) *offlabel = wd->offtext;
434 }
435
436 /**
437  * Sets the state of the toggle to @p state.
438  *
439  * @param obj The toggle object
440  * @param state The state of @p obj
441  *
442  * @ingroup Toggle
443  */
444 EAPI void
445 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
446 {
447    ELM_CHECK_WIDTYPE(obj, widtype);
448    Widget_Data *wd = elm_widget_data_get(obj);
449    if (!wd) return;
450    if (state != wd->state)
451      {
452         wd->state = state;
453         if (wd->statep) *wd->statep = wd->state;
454         if (wd->state)
455           edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
456         else
457           edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
458      }
459 }
460
461 /**
462  * Gets the state of the toggle to @p state.
463  *
464  * @param obj The toggle object
465  * @return The state of @p obj
466  *
467  * @ingroup Toggle
468  */
469 EAPI Eina_Bool
470 elm_toggle_state_get(const Evas_Object *obj)
471 {
472    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
473    Widget_Data *wd = elm_widget_data_get(obj);
474    if (!wd) return EINA_FALSE;
475    return wd->state;
476 }
477
478 /**
479  * Sets the state pointer of the toggle to @p statep.
480  *
481  * @param obj The toggle object
482  * @param statep The state pointer of @p obj
483  *
484  * @ingroup Toggle
485  */
486 EAPI void
487 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
488 {
489    ELM_CHECK_WIDTYPE(obj, widtype);
490    Widget_Data *wd = elm_widget_data_get(obj);
491    if (!wd) return;
492    if (statep)
493      {
494         wd->statep = statep;
495         if (*wd->statep != wd->state)
496           {
497              wd->state = *wd->statep;
498              if (wd->state)
499                edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
500              else
501                edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
502           }
503      }
504    else
505      wd->statep = NULL;
506 }