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 * Add a toggle to @p parent.
208 * @param parent The parent object
210 * @return The toggle object
215 elm_toggle_add(Evas_Object *parent)
221 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
223 ELM_SET_WIDTYPE(widtype, "toggle");
224 elm_widget_type_set(obj, "toggle");
225 elm_widget_sub_object_add(parent, obj);
226 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
227 elm_widget_data_set(obj, wd);
228 elm_widget_del_hook_set(obj, _del_hook);
229 elm_widget_theme_hook_set(obj, _theme_hook);
230 elm_widget_disable_hook_set(obj, _disable_hook);
231 elm_widget_can_focus_set(obj, EINA_TRUE);
232 elm_widget_event_hook_set(obj, _event_hook);
234 wd->tgl = edje_object_add(e);
235 _mirrored_set(obj, elm_widget_mirrored_get(obj));
236 _elm_theme_object_set(obj, wd->tgl, "toggle", "base", "default");
237 wd->ontext = eina_stringshare_add("ON");
238 wd->offtext = eina_stringshare_add("OFF");
239 edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,on", "",
240 _signal_toggle_on, obj);
241 edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,off", "",
242 _signal_toggle_off, obj);
243 elm_widget_resize_object_set(obj, wd->tgl);
244 edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
245 edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
247 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
248 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
252 // TODO: convert Elementary to subclassing of Evas_Smart_Class
253 // TODO: and save some bytes, making descriptions per-class and not instance!
254 evas_object_smart_callbacks_descriptions_set(obj, _signals);
259 * Sets the label to be displayed with the toggle.
261 * @param obj The toggle object
262 * @param label The label to be displayed
267 elm_toggle_label_set(Evas_Object *obj, const char *label)
269 ELM_CHECK_WIDTYPE(obj, widtype);
270 Widget_Data *wd = elm_widget_data_get(obj);
272 eina_stringshare_replace(&wd->label, label);
274 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
276 edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
277 edje_object_message_signal_process(wd->tgl);
278 edje_object_part_text_set(wd->tgl, "elm.text", label);
283 * Gets the label of the toggle
285 * @param obj toggleeee object
286 * @return The label of the toggle
291 elm_toggle_label_get(const Evas_Object *obj)
293 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
294 Widget_Data *wd = elm_widget_data_get(obj);
295 if (!wd) return NULL;
300 * Set the icon used for the toggle
302 * Once the icon object is set, a previously set one will be deleted
303 * If you want to keep that old content object, use the
304 * elm_toggle_icon_unset() function.
306 * @param obj The toggle object
307 * @param icon The icon object for the button
312 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
314 ELM_CHECK_WIDTYPE(obj, widtype);
315 Widget_Data *wd = elm_widget_data_get(obj);
317 if (wd->icon == icon) return;
318 if (wd->icon) evas_object_del(wd->icon);
322 elm_widget_sub_object_add(obj, icon);
323 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
324 _changed_size_hints, obj);
325 edje_object_part_swallow(wd->tgl, "elm.swallow.content", icon);
326 edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
327 edje_object_message_signal_process(wd->tgl);
333 * Get the icon used for the toggle
335 * Return the icon object which is set for this widget.
337 * @param obj The toggle object
338 * @return The icon object that is being used
343 elm_toggle_icon_get(const Evas_Object *obj)
345 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
346 Widget_Data *wd = elm_widget_data_get(obj);
347 if (!wd) return NULL;
352 * Unset the icon used for the toggle
354 * Unparent and return the icon object which was set for this widget.
356 * @param obj The toggle object
357 * @return The icon object that was being used
362 elm_toggle_icon_unset(Evas_Object *obj)
364 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
365 Widget_Data *wd = elm_widget_data_get(obj);
366 if (!wd) return NULL;
367 if (!wd->icon) return NULL;
368 Evas_Object *icon = wd->icon;
369 elm_widget_sub_object_del(obj, wd->icon);
370 edje_object_part_unswallow(wd->tgl, wd->icon);
376 * Sets the labels to be associated with the on and off states of the toggle.
378 * @param obj The toggle object
379 * @param onlabel The label displayed when the toggle is in the "on" state
380 * @param offlabel The label displayed when the toggle is in the "off" state
385 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
387 ELM_CHECK_WIDTYPE(obj, widtype);
388 Widget_Data *wd = elm_widget_data_get(obj);
390 eina_stringshare_replace(&wd->ontext, onlabel);
391 eina_stringshare_replace(&wd->offtext, offlabel);
392 edje_object_part_text_set(wd->tgl, "elm.ontext", onlabel);
393 edje_object_part_text_set(wd->tgl, "elm.offtext", offlabel);
399 * Gets the labels associated with the on and off states of the toggle.
401 * @param obj The toggle object
402 * @param onlabel A char** to place the onlabel of @p obj into
403 * @param offlabel A char** to place the offlabel of @p obj into
408 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
410 if (onlabel) *onlabel = NULL;
411 if (offlabel) *offlabel = NULL;
412 ELM_CHECK_WIDTYPE(obj, widtype);
413 Widget_Data *wd = elm_widget_data_get(obj);
415 if (onlabel) *onlabel = wd->ontext;
416 if (offlabel) *offlabel = wd->offtext;
420 * Sets the state of the toggle to @p state.
422 * @param obj The toggle object
423 * @param state The state of @p obj
428 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
430 ELM_CHECK_WIDTYPE(obj, widtype);
431 Widget_Data *wd = elm_widget_data_get(obj);
433 if (state != wd->state)
436 if (wd->statep) *wd->statep = wd->state;
438 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
440 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
445 * Gets the state of the toggle to @p state.
447 * @param obj The toggle object
448 * @return The state of @p obj
453 elm_toggle_state_get(const Evas_Object *obj)
455 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
456 Widget_Data *wd = elm_widget_data_get(obj);
457 if (!wd) return EINA_FALSE;
462 * Sets the state pointer of the toggle to @p statep.
464 * @param obj The toggle object
465 * @param statep The state pointer of @p obj
470 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
472 ELM_CHECK_WIDTYPE(obj, widtype);
473 Widget_Data *wd = elm_widget_data_get(obj);
478 if (*wd->statep != wd->state)
480 wd->state = *wd->statep;
482 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
484 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");