1 #include <Elementary.h>
7 * A toggle is a slider which can be used to toggle between
8 * two values. It has two states: on and off.
10 * Signals that you can add callbacks for are:
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).
17 typedef struct _Widget_Data Widget_Data;
26 const char *ontext, *offtext;
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);
42 static const char SIG_CHANGED[] = "changed";
43 static const Evas_Smart_Cb_Description _signals[] = {
49 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
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")))
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;
68 _del_hook(Evas_Object *obj)
70 Widget_Data *wd = elm_widget_data_get(obj);
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);
79 _disable_hook(Evas_Object *obj)
81 Widget_Data *wd = elm_widget_data_get(obj);
83 if (elm_widget_disabled_get(obj))
84 edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
86 edje_object_signal_emit(wd->tgl, "elm,state,enabled", "elm");
90 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
92 Widget_Data *wd = elm_widget_data_get(obj);
94 if (elm_widget_focus_get(obj))
96 edje_object_signal_emit(wd->tgl, "elm,action,focus", "elm");
97 evas_object_focus_set(wd->tgl, EINA_TRUE);
101 edje_object_signal_emit(wd->tgl, "elm,action,unfocus", "elm");
102 evas_object_focus_set(wd->tgl, EINA_FALSE);
107 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
109 Widget_Data *wd = elm_widget_data_get(obj);
111 edje_object_mirrored_set(wd->tgl, rtl);
115 _theme_hook(Evas_Object *obj)
117 Widget_Data *wd = elm_widget_data_get(obj);
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));
123 edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
125 edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
127 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
129 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
131 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
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);
145 _sizing_eval(Evas_Object *obj)
147 Widget_Data *wd = elm_widget_data_get(obj);
148 Evas_Coord minw = -1, minh = -1;
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);
159 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
161 Widget_Data *wd = elm_widget_data_get(data);
163 if (obj != wd->icon) return;
168 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
170 Widget_Data *wd = elm_widget_data_get(obj);
171 Evas_Object *sub = event_info;
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);
179 edje_object_message_signal_process(wd->tgl);
185 _signal_toggle_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
187 Widget_Data *wd = elm_widget_data_get(data);
190 if (wd->statep) *wd->statep = wd->state;
191 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
195 _signal_toggle_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
197 Widget_Data *wd = elm_widget_data_get(data);
200 if (wd->statep) *wd->statep = wd->state;
201 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
205 * Add a toggle to @p parent.
207 * @param parent The parent object
209 * @return The toggle object
214 elm_toggle_add(Evas_Object *parent)
220 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
222 ELM_SET_WIDTYPE(widtype, "toggle");
223 elm_widget_type_set(obj, "toggle");
224 elm_widget_sub_object_add(parent, obj);
225 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
226 elm_widget_data_set(obj, wd);
227 elm_widget_del_hook_set(obj, _del_hook);
228 elm_widget_theme_hook_set(obj, _theme_hook);
229 elm_widget_disable_hook_set(obj, _disable_hook);
230 elm_widget_can_focus_set(obj, EINA_TRUE);
231 elm_widget_event_hook_set(obj, _event_hook);
233 wd->tgl = edje_object_add(e);
234 _mirrored_set(obj, elm_widget_mirrored_get(obj));
235 _elm_theme_object_set(obj, wd->tgl, "toggle", "base", "default");
236 wd->ontext = eina_stringshare_add("ON");
237 wd->offtext = eina_stringshare_add("OFF");
238 edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,on", "",
239 _signal_toggle_on, obj);
240 edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,off", "",
241 _signal_toggle_off, obj);
242 elm_widget_resize_object_set(obj, wd->tgl);
243 edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
244 edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
246 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
247 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
251 // TODO: convert Elementary to subclassing of Evas_Smart_Class
252 // TODO: and save some bytes, making descriptions per-class and not instance!
253 evas_object_smart_callbacks_descriptions_set(obj, _signals);
258 * Sets the label to be displayed with the toggle.
260 * @param obj The toggle object
261 * @param label The label to be displayed
266 elm_toggle_label_set(Evas_Object *obj, const char *label)
268 ELM_CHECK_WIDTYPE(obj, widtype);
269 Widget_Data *wd = elm_widget_data_get(obj);
271 eina_stringshare_replace(&wd->label, label);
273 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
275 edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
276 edje_object_message_signal_process(wd->tgl);
277 edje_object_part_text_set(wd->tgl, "elm.text", label);
282 * Gets the label of the toggle
284 * @param obj toggleeee object
285 * @return The label of the toggle
290 elm_toggle_label_get(const Evas_Object *obj)
292 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
293 Widget_Data *wd = elm_widget_data_get(obj);
294 if (!wd) return NULL;
299 * Set the icon used for the toggle
301 * Once the icon object is set, a previously set one will be deleted
302 * If you want to keep that old content object, use the
303 * elm_toggle_icon_unset() function.
305 * @param obj The toggle object
306 * @param icon The icon object for the button
311 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
313 ELM_CHECK_WIDTYPE(obj, widtype);
314 Widget_Data *wd = elm_widget_data_get(obj);
316 if (wd->icon == icon) return;
317 if (wd->icon) evas_object_del(wd->icon);
321 elm_widget_sub_object_add(obj, icon);
322 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
323 _changed_size_hints, obj);
324 edje_object_part_swallow(wd->tgl, "elm.swallow.content", icon);
325 edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
326 edje_object_message_signal_process(wd->tgl);
332 * Get the icon used for the toggle
334 * Return the icon object which is set for this widget.
336 * @param obj The toggle object
337 * @return The icon object that is being used
342 elm_toggle_icon_get(const Evas_Object *obj)
344 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
345 Widget_Data *wd = elm_widget_data_get(obj);
346 if (!wd) return NULL;
351 * Unset the icon used for the toggle
353 * Unparent and return the icon object which was set for this widget.
355 * @param obj The toggle object
356 * @return The icon object that was being used
361 elm_toggle_icon_unset(Evas_Object *obj)
363 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
364 Widget_Data *wd = elm_widget_data_get(obj);
365 if (!wd) return NULL;
366 if (!wd->icon) return NULL;
367 Evas_Object *icon = wd->icon;
368 elm_widget_sub_object_del(obj, wd->icon);
369 edje_object_part_unswallow(wd->tgl, wd->icon);
375 * Sets the labels to be associated with the on and off states of the toggle.
377 * @param obj The toggle object
378 * @param onlabel The label displayed when the toggle is in the "on" state
379 * @param offlabel The label displayed when the toggle is in the "off" state
384 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
386 ELM_CHECK_WIDTYPE(obj, widtype);
387 Widget_Data *wd = elm_widget_data_get(obj);
389 eina_stringshare_replace(&wd->ontext, onlabel);
390 eina_stringshare_replace(&wd->offtext, offlabel);
391 edje_object_part_text_set(wd->tgl, "elm.ontext", onlabel);
392 edje_object_part_text_set(wd->tgl, "elm.offtext", offlabel);
398 * Gets the labels associated with the on and off states of the toggle.
400 * @param obj The toggle object
401 * @param onlabel A char** to place the onlabel of @p obj into
402 * @param offlabel A char** to place the offlabel of @p obj into
407 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
409 if (onlabel) *onlabel = NULL;
410 if (offlabel) *offlabel = NULL;
411 ELM_CHECK_WIDTYPE(obj, widtype);
412 Widget_Data *wd = elm_widget_data_get(obj);
414 if (onlabel) *onlabel = wd->ontext;
415 if (offlabel) *offlabel = wd->offtext;
419 * Sets the state of the toggle to @p state.
421 * @param obj The toggle object
422 * @param state The state of @p obj
427 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
429 ELM_CHECK_WIDTYPE(obj, widtype);
430 Widget_Data *wd = elm_widget_data_get(obj);
432 if (state != wd->state)
435 if (wd->statep) *wd->statep = wd->state;
437 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
439 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
444 * Gets the state of the toggle to @p state.
446 * @param obj The toggle object
447 * @return The state of @p obj
452 elm_toggle_state_get(const Evas_Object *obj)
454 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
455 Widget_Data *wd = elm_widget_data_get(obj);
456 if (!wd) return EINA_FALSE;
461 * Sets the state pointer of the toggle to @p statep.
463 * @param obj The toggle object
464 * @param statep The state pointer of @p obj
469 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
471 ELM_CHECK_WIDTYPE(obj, widtype);
472 Widget_Data *wd = elm_widget_data_get(obj);
477 if (*wd->statep != wd->state)
479 wd->state = *wd->statep;
481 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
483 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");