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
14 * until the toggle is released by the cursor (assuming it has been triggered
15 * 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 _theme_hook(Evas_Object *obj)
110 Widget_Data *wd = elm_widget_data_get(obj);
112 _elm_theme_object_set(obj, wd->tgl, "toggle", "base", elm_widget_style_get(obj));
114 edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
116 edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
118 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
120 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
122 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
124 edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
125 edje_object_part_text_set(wd->tgl, "elm.text", wd->label);
126 edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
127 edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
128 if (elm_widget_disabled_get(obj))
129 edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
130 edje_object_message_signal_process(wd->tgl);
131 edje_object_scale_set(wd->tgl, elm_widget_scale_get(obj) * _elm_config->scale);
136 _sizing_eval(Evas_Object *obj)
138 Widget_Data *wd = elm_widget_data_get(obj);
139 Evas_Coord minw = -1, minh = -1;
142 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
143 edje_object_size_min_restricted_calc(wd->tgl, &minw, &minh, minw, minh);
144 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
145 evas_object_size_hint_min_set(obj, minw, minh);
146 evas_object_size_hint_max_set(obj, -1, -1);
150 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
152 Widget_Data *wd = elm_widget_data_get(data);
154 if (obj != wd->icon) return;
159 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
161 Widget_Data *wd = elm_widget_data_get(obj);
162 Evas_Object *sub = event_info;
166 edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
167 evas_object_event_callback_del_full
168 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
170 edje_object_message_signal_process(wd->tgl);
176 _signal_toggle_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
178 Widget_Data *wd = elm_widget_data_get(data);
181 if (wd->statep) *wd->statep = wd->state;
182 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
186 _signal_toggle_on(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 * Add a toggle to @p parent.
198 * @param parent The parent object
200 * @return The toggle object
205 elm_toggle_add(Evas_Object *parent)
211 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
213 wd = ELM_NEW(Widget_Data);
214 e = evas_object_evas_get(parent);
216 obj = elm_widget_add(e);
217 ELM_SET_WIDTYPE(widtype, "toggle");
218 elm_widget_type_set(obj, "toggle");
219 elm_widget_sub_object_add(parent, obj);
220 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
221 elm_widget_data_set(obj, wd);
222 elm_widget_del_hook_set(obj, _del_hook);
223 elm_widget_theme_hook_set(obj, _theme_hook);
224 elm_widget_disable_hook_set(obj, _disable_hook);
225 elm_widget_can_focus_set(obj, EINA_TRUE);
226 elm_widget_event_hook_set(obj, _event_hook);
228 wd->tgl = edje_object_add(e);
229 _elm_theme_object_set(obj, wd->tgl, "toggle", "base", "default");
230 wd->ontext = eina_stringshare_add("ON");
231 wd->offtext = eina_stringshare_add("OFF");
232 edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,on", "",
233 _signal_toggle_on, obj);
234 edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,off", "",
235 _signal_toggle_off, obj);
236 elm_widget_resize_object_set(obj, wd->tgl);
237 edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
238 edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
240 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
244 // TODO: convert Elementary to subclassing of Evas_Smart_Class
245 // TODO: and save some bytes, making descriptions per-class and not instance!
246 evas_object_smart_callbacks_descriptions_set(obj, _signals);
251 * Sets the label to be displayed with the toggle.
253 * @param obj The toggle object
254 * @param label The label to be displayed
259 elm_toggle_label_set(Evas_Object *obj, const char *label)
261 ELM_CHECK_WIDTYPE(obj, widtype);
262 Widget_Data *wd = elm_widget_data_get(obj);
264 eina_stringshare_replace(&wd->label, label);
266 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
268 edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
269 edje_object_message_signal_process(wd->tgl);
270 edje_object_part_text_set(wd->tgl, "elm.text", label);
275 * Gets the label of the toggle
277 * @param obj toggleeee object
278 * @return The label of the toggle
283 elm_toggle_label_get(const Evas_Object *obj)
285 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
286 Widget_Data *wd = elm_widget_data_get(obj);
287 if (!wd) return NULL;
292 * Set the icon used for the toggle
294 * Once the icon object is set, a previously set one will be deleted
295 * If you want to keep that old content object, use the
296 * elm_toggle_icon_unset() function.
298 * @param obj The toggle object
299 * @param icon The icon object for the button
304 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
306 ELM_CHECK_WIDTYPE(obj, widtype);
307 Widget_Data *wd = elm_widget_data_get(obj);
309 if (wd->icon == icon) return;
310 if (wd->icon) evas_object_del(wd->icon);
314 elm_widget_sub_object_add(obj, icon);
315 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
316 _changed_size_hints, obj);
317 edje_object_part_swallow(wd->tgl, "elm.swallow.content", icon);
318 edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
319 edje_object_message_signal_process(wd->tgl);
325 * Get the icon used for the toggle
327 * Return the icon object which is set for this widget.
329 * @param obj The toggle object
330 * @return The icon object that is being used
335 elm_toggle_icon_get(const Evas_Object *obj)
337 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
338 Widget_Data *wd = elm_widget_data_get(obj);
339 if (!wd) return NULL;
344 * Unset the icon used for the toggle
346 * Unparent and return the icon object which was set for this widget.
348 * @param obj The toggle object
349 * @return The icon object that was being used
354 elm_toggle_icon_unset(Evas_Object *obj)
356 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
357 Widget_Data *wd = elm_widget_data_get(obj);
358 if (!wd) return NULL;
359 if (!wd->icon) return NULL;
360 Evas_Object *icon = wd->icon;
361 elm_widget_sub_object_del(obj, wd->icon);
362 edje_object_part_unswallow(wd->tgl, wd->icon);
368 * Sets the labels to be associated with the on and off states of the toggle.
370 * @param obj The toggle object
371 * @param onlabel The label displayed when the toggle is in the "on" state
372 * @param offlabel The label displayed when the toggle is in the "off" state
377 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
379 ELM_CHECK_WIDTYPE(obj, widtype);
380 Widget_Data *wd = elm_widget_data_get(obj);
382 eina_stringshare_replace(&wd->ontext, onlabel);
383 eina_stringshare_replace(&wd->offtext, offlabel);
384 edje_object_part_text_set(wd->tgl, "elm.ontext", onlabel);
385 edje_object_part_text_set(wd->tgl, "elm.offtext", offlabel);
391 * Gets the labels associated with the on and off states of the toggle.
393 * @param obj The toggle object
394 * @param onlabel A char** to place the onlabel of @p obj into
395 * @param offlabel A char** to place the offlabel of @p obj into
400 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
402 if (onlabel) *onlabel = NULL;
403 if (offlabel) *offlabel = NULL;
404 ELM_CHECK_WIDTYPE(obj, widtype);
405 Widget_Data *wd = elm_widget_data_get(obj);
407 if (onlabel) *onlabel = wd->ontext;
408 if (offlabel) *offlabel = wd->offtext;
412 * Sets the state of the toggle to @p state.
414 * @param obj The toggle object
415 * @param state The state of @p obj
420 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
422 ELM_CHECK_WIDTYPE(obj, widtype);
423 Widget_Data *wd = elm_widget_data_get(obj);
425 if (state != wd->state)
428 if (wd->statep) *wd->statep = wd->state;
430 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
432 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
437 * Gets the state of the toggle to @p state.
439 * @param obj The toggle object
440 * @return The state of @p obj
445 elm_toggle_state_get(const Evas_Object *obj)
447 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
448 Widget_Data *wd = elm_widget_data_get(obj);
449 if (!wd) return EINA_FALSE;
454 * Sets the state pointer of the toggle to @p statep.
456 * @param obj The toggle object
457 * @param statep The state pointer of @p obj
462 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
464 ELM_CHECK_WIDTYPE(obj, widtype);
465 Widget_Data *wd = elm_widget_data_get(obj);
470 if (*wd->statep != wd->state)
472 wd->state = *wd->statep;
474 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
476 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");