1 #include <Elementary.h>
5 * @defgroup Toggle Toggle
8 * A toggle is a slider which can be used to toggle between
9 * two values. It has two states: on and off.
11 * Signals that you can add callbacks for are:
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).
18 typedef struct _Widget_Data Widget_Data;
27 const char *ontext, *offtext;
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);
43 static const char SIG_CHANGED[] = "changed";
44 static const Evas_Smart_Cb_Description _signals[] = {
50 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
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")))
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;
69 _del_hook(Evas_Object *obj)
71 Widget_Data *wd = elm_widget_data_get(obj);
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);
80 _disable_hook(Evas_Object *obj)
82 Widget_Data *wd = elm_widget_data_get(obj);
84 if (elm_widget_disabled_get(obj))
85 edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
87 edje_object_signal_emit(wd->tgl, "elm,state,enabled", "elm");
91 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
93 Widget_Data *wd = elm_widget_data_get(obj);
95 if (elm_widget_focus_get(obj))
97 edje_object_signal_emit(wd->tgl, "elm,action,focus", "elm");
98 evas_object_focus_set(wd->tgl, EINA_TRUE);
102 edje_object_signal_emit(wd->tgl, "elm,action,unfocus", "elm");
103 evas_object_focus_set(wd->tgl, EINA_FALSE);
108 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
110 Widget_Data *wd = elm_widget_data_get(obj);
112 edje_object_mirrored_set(wd->tgl, rtl);
116 _theme_hook(Evas_Object *obj)
118 Widget_Data *wd = elm_widget_data_get(obj);
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));
124 edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
126 edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
128 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
130 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
132 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
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);
146 _sizing_eval(Evas_Object *obj)
148 Widget_Data *wd = elm_widget_data_get(obj);
149 Evas_Coord minw = -1, minh = -1;
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);
160 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
162 Widget_Data *wd = elm_widget_data_get(data);
164 if (obj != wd->icon) return;
169 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
171 Widget_Data *wd = elm_widget_data_get(obj);
172 Evas_Object *sub = event_info;
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);
180 edje_object_message_signal_process(wd->tgl);
186 _signal_toggle_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
188 Widget_Data *wd = elm_widget_data_get(data);
191 if (wd->statep) *wd->statep = wd->state;
192 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
196 _signal_toggle_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
198 Widget_Data *wd = elm_widget_data_get(data);
201 if (wd->statep) *wd->statep = wd->state;
202 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
206 _elm_toggle_label_set(Evas_Object *obj, const char *item, const char *label)
208 ELM_CHECK_WIDTYPE(obj, widtype);
209 Widget_Data *wd = elm_widget_data_get(obj);
210 if (item && strcmp(item, "default")) return;
212 eina_stringshare_replace(&wd->label, label);
214 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
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);
223 _elm_toggle_label_get(const Evas_Object *obj, const char *item)
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;
233 * Add a toggle to @p parent.
235 * @param parent The parent object
237 * @return The toggle object
242 elm_toggle_add(Evas_Object *parent)
248 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
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);
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);
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");
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);
288 * Sets the label to be displayed with the toggle.
290 * @param obj The toggle object
291 * @param label The label to be displayed
294 * @deprecate use elm_object_text_* instead.
297 elm_toggle_label_set(Evas_Object *obj, const char *label)
299 _elm_toggle_label_set(obj, NULL, label);
303 * Gets the label of the toggle
305 * @param obj toggleeee object
306 * @return The label of the toggle
309 * @deprecate use elm_object_text_* instead.
312 elm_toggle_label_get(const Evas_Object *obj)
314 return _elm_toggle_label_get(obj, NULL);
318 * Set the icon used for the toggle
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.
324 * @param obj The toggle object
325 * @param icon The icon object for the button
330 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
332 ELM_CHECK_WIDTYPE(obj, widtype);
333 Widget_Data *wd = elm_widget_data_get(obj);
335 if (wd->icon == icon) return;
336 if (wd->icon) evas_object_del(wd->icon);
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);
351 * Get the icon used for the toggle
353 * Return the icon object which is set for this widget.
355 * @param obj The toggle object
356 * @return The icon object that is being used
361 elm_toggle_icon_get(const Evas_Object *obj)
363 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
364 Widget_Data *wd = elm_widget_data_get(obj);
365 if (!wd) return NULL;
370 * Unset the icon used for the toggle
372 * Unparent and return the icon object which was set for this widget.
374 * @param obj The toggle object
375 * @return The icon object that was being used
380 elm_toggle_icon_unset(Evas_Object *obj)
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);
394 * Sets the labels to be associated with the on and off states of the toggle.
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
403 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
405 ELM_CHECK_WIDTYPE(obj, widtype);
406 Widget_Data *wd = elm_widget_data_get(obj);
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);
417 * Gets the labels associated with the on and off states of the toggle.
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
426 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
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);
433 if (onlabel) *onlabel = wd->ontext;
434 if (offlabel) *offlabel = wd->offtext;
438 * Sets the state of the toggle to @p state.
440 * @param obj The toggle object
441 * @param state The state of @p obj
446 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
448 ELM_CHECK_WIDTYPE(obj, widtype);
449 Widget_Data *wd = elm_widget_data_get(obj);
451 if (state != wd->state)
454 if (wd->statep) *wd->statep = wd->state;
456 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
458 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
463 * Gets the state of the toggle to @p state.
465 * @param obj The toggle object
466 * @return The state of @p obj
471 elm_toggle_state_get(const Evas_Object *obj)
473 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
474 Widget_Data *wd = elm_widget_data_get(obj);
475 if (!wd) return EINA_FALSE;
480 * Sets the state pointer of the toggle to @p statep.
482 * @param obj The toggle object
483 * @param statep The state pointer of @p obj
488 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
490 ELM_CHECK_WIDTYPE(obj, widtype);
491 Widget_Data *wd = elm_widget_data_get(obj);
496 if (*wd->statep != wd->state)
498 wd->state = *wd->statep;
500 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
502 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");