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 _elm_toggle_label_set(Evas_Object *obj, const char *item, const char *label)
207 ELM_CHECK_WIDTYPE(obj, widtype);
208 Widget_Data *wd = elm_widget_data_get(obj);
211 eina_stringshare_replace(&wd->label, label);
213 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
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);
222 _elm_toggle_label_get(const Evas_Object *obj, const char *item)
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;
232 * Add a toggle to @p parent.
234 * @param parent The parent object
236 * @return The toggle object
241 elm_toggle_add(Evas_Object *parent)
247 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
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);
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);
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");
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);
287 * Sets the label to be displayed with the toggle.
289 * @param obj The toggle object
290 * @param label The label to be displayed
293 * @deprecate use elm_object_text_* instead.
296 elm_toggle_label_set(Evas_Object *obj, const char *label)
298 _elm_toggle_label_set(obj, NULL, label);
302 * Gets the label of the toggle
304 * @param obj toggleeee object
305 * @return The label of the toggle
308 * @deprecate use elm_object_text_* instead.
311 elm_toggle_label_get(const Evas_Object *obj)
313 return _elm_toggle_label_get(obj, NULL);
317 * Set the icon used for the toggle
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.
323 * @param obj The toggle object
324 * @param icon The icon object for the button
329 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
331 ELM_CHECK_WIDTYPE(obj, widtype);
332 Widget_Data *wd = elm_widget_data_get(obj);
334 if (wd->icon == icon) return;
335 if (wd->icon) evas_object_del(wd->icon);
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);
350 * Get the icon used for the toggle
352 * Return the icon object which is set for this widget.
354 * @param obj The toggle object
355 * @return The icon object that is being used
360 elm_toggle_icon_get(const Evas_Object *obj)
362 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
363 Widget_Data *wd = elm_widget_data_get(obj);
364 if (!wd) return NULL;
369 * Unset the icon used for the toggle
371 * Unparent and return the icon object which was set for this widget.
373 * @param obj The toggle object
374 * @return The icon object that was being used
379 elm_toggle_icon_unset(Evas_Object *obj)
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);
393 * Sets the labels to be associated with the on and off states of the toggle.
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
402 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
404 ELM_CHECK_WIDTYPE(obj, widtype);
405 Widget_Data *wd = elm_widget_data_get(obj);
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);
416 * Gets the labels associated with the on and off states of the toggle.
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
425 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
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);
432 if (onlabel) *onlabel = wd->ontext;
433 if (offlabel) *offlabel = wd->offtext;
437 * Sets the state of the toggle to @p state.
439 * @param obj The toggle object
440 * @param state The state of @p obj
445 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
447 ELM_CHECK_WIDTYPE(obj, widtype);
448 Widget_Data *wd = elm_widget_data_get(obj);
450 if (state != wd->state)
453 if (wd->statep) *wd->statep = wd->state;
455 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
457 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
462 * Gets the state of the toggle to @p state.
464 * @param obj The toggle object
465 * @return The state of @p obj
470 elm_toggle_state_get(const Evas_Object *obj)
472 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
473 Widget_Data *wd = elm_widget_data_get(obj);
474 if (!wd) return EINA_FALSE;
479 * Sets the state pointer of the toggle to @p statep.
481 * @param obj The toggle object
482 * @param statep The state pointer of @p obj
487 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
489 ELM_CHECK_WIDTYPE(obj, widtype);
490 Widget_Data *wd = elm_widget_data_get(obj);
495 if (*wd->statep != wd->state)
497 wd->state = *wd->statep;
499 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
501 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");