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);
40 static const char SIG_CHANGED[] = "changed";
41 static const Evas_Smart_Cb_Description _signals[] = {
47 _del_hook(Evas_Object *obj)
49 Widget_Data *wd = elm_widget_data_get(obj);
51 if (wd->label) eina_stringshare_del(wd->label);
52 if (wd->ontext) eina_stringshare_del(wd->ontext);
53 if (wd->offtext) eina_stringshare_del(wd->offtext);
58 _disable_hook(Evas_Object *obj)
60 Widget_Data *wd = elm_widget_data_get(obj);
62 if (elm_widget_disabled_get(obj))
63 edje_object_signal_emit(wd->tgl, "elm,state,disabled", "elm");
65 edje_object_signal_emit(wd->tgl, "elm,state,enabled", "elm");
69 _theme_hook(Evas_Object *obj)
71 Widget_Data *wd = elm_widget_data_get(obj);
73 _elm_theme_object_set(obj, wd->tgl, "toggle", "base", elm_widget_style_get(obj));
75 edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
77 edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
79 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
81 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
83 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
85 edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
86 edje_object_part_text_set(wd->tgl, "elm.text", wd->label);
87 edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
88 edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
89 edje_object_message_signal_process(wd->tgl);
90 edje_object_scale_set(wd->tgl, elm_widget_scale_get(obj) * _elm_config->scale);
95 _sizing_eval(Evas_Object *obj)
97 Widget_Data *wd = elm_widget_data_get(obj);
98 Evas_Coord minw = -1, minh = -1;
101 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
102 edje_object_size_min_restricted_calc(wd->tgl, &minw, &minh, minw, minh);
103 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
104 evas_object_size_hint_min_set(obj, minw, minh);
105 evas_object_size_hint_max_set(obj, -1, -1);
109 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
111 Widget_Data *wd = elm_widget_data_get(data);
113 if (obj != wd->icon) return;
118 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
120 Widget_Data *wd = elm_widget_data_get(obj);
121 Evas_Object *sub = event_info;
125 edje_object_signal_emit(wd->tgl, "elm,state,icon,hidden", "elm");
126 evas_object_event_callback_del_full
127 (sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints, obj);
134 _signal_toggle_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
136 Widget_Data *wd = elm_widget_data_get(data);
139 if (wd->statep) *wd->statep = wd->state;
140 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
144 _signal_toggle_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
146 Widget_Data *wd = elm_widget_data_get(data);
149 if (wd->statep) *wd->statep = wd->state;
150 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
154 * Add a toggle to @p parent.
156 * @param parent The parent object
158 * @return The toggle object
163 elm_toggle_add(Evas_Object *parent)
169 wd = ELM_NEW(Widget_Data);
170 e = evas_object_evas_get(parent);
171 obj = elm_widget_add(e);
172 ELM_SET_WIDTYPE(widtype, "toggle");
173 elm_widget_type_set(obj, "toggle");
174 elm_widget_sub_object_add(parent, obj);
175 elm_widget_data_set(obj, wd);
176 elm_widget_del_hook_set(obj, _del_hook);
177 elm_widget_theme_hook_set(obj, _theme_hook);
178 elm_widget_disable_hook_set(obj, _disable_hook);
180 wd->tgl = edje_object_add(e);
181 _elm_theme_object_set(obj, wd->tgl, "toggle", "base", "default");
182 wd->ontext = eina_stringshare_add("ON");
183 wd->offtext = eina_stringshare_add("OFF");
184 edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,on", "",
185 _signal_toggle_on, obj);
186 edje_object_signal_callback_add(wd->tgl, "elm,action,toggle,off", "",
187 _signal_toggle_off, obj);
188 elm_widget_resize_object_set(obj, wd->tgl);
189 edje_object_part_text_set(wd->tgl, "elm.ontext", wd->ontext);
190 edje_object_part_text_set(wd->tgl, "elm.offtext", wd->offtext);
192 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
196 // TODO: convert Elementary to subclassing of Evas_Smart_Class
197 // TODO: and save some bytes, making descriptions per-class and not instance!
198 evas_object_smart_callbacks_descriptions_set(obj, _signals);
203 * Sets the label to be displayed with the toggle.
205 * @param obj The toggle object
206 * @param label The label to be displayed
211 elm_toggle_label_set(Evas_Object *obj, const char *label)
213 ELM_CHECK_WIDTYPE(obj, widtype);
214 Widget_Data *wd = elm_widget_data_get(obj);
216 eina_stringshare_replace(&wd->label, label);
218 edje_object_signal_emit(wd->tgl, "elm,state,text,visible", "elm");
220 edje_object_signal_emit(wd->tgl, "elm,state,text,hidden", "elm");
221 edje_object_message_signal_process(wd->tgl);
222 edje_object_part_text_set(wd->tgl, "elm.text", label);
227 * Gets the label of the toggle
229 * @param obj The toggle object
230 * @return The label of the toggle
235 elm_toggle_label_get(const Evas_Object *obj)
237 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
238 Widget_Data *wd = elm_widget_data_get(obj);
239 if (!wd) return NULL;
244 * Sets the icon to be displayed with the toggle.
246 * @param obj The toggle object
247 * @param icon The icon object to be displayed
252 elm_toggle_icon_set(Evas_Object *obj, Evas_Object *icon)
254 ELM_CHECK_WIDTYPE(obj, widtype);
255 Widget_Data *wd = elm_widget_data_get(obj);
257 if ((wd->icon != icon) && (wd->icon))
258 elm_widget_sub_object_del(obj, wd->icon);
261 elm_widget_sub_object_add(obj, icon);
262 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
263 _changed_size_hints, obj);
264 edje_object_part_swallow(wd->tgl, "elm.swallow.content", icon);
265 edje_object_signal_emit(wd->tgl, "elm,state,icon,visible", "elm");
270 * Gets the icon of the toggle
272 * @param obj The toggle object
273 * @return The icon object
278 elm_toggle_icon_get(const Evas_Object *obj)
280 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
281 Widget_Data *wd = elm_widget_data_get(obj);
282 if (!wd) return NULL;
287 * Sets the labels to be associated with the on and off states of the toggle.
289 * @param obj The toggle object
290 * @param onlabel The label displayed when the toggle is in the "on" state
291 * @param offlabel The label displayed when the toggle is in the "off" state
296 elm_toggle_states_labels_set(Evas_Object *obj, const char *onlabel, const char *offlabel)
298 ELM_CHECK_WIDTYPE(obj, widtype);
299 Widget_Data *wd = elm_widget_data_get(obj);
301 eina_stringshare_replace(&wd->ontext, onlabel);
302 eina_stringshare_replace(&wd->offtext, offlabel);
303 edje_object_part_text_set(wd->tgl, "elm.ontext", onlabel);
304 edje_object_part_text_set(wd->tgl, "elm.offtext", offlabel);
310 * Gets the labels associated with the on and off states of the toggle.
312 * @param obj The toggle object
313 * @param onlabel A char** to place the onlabel of @p obj into
314 * @param offlabel A char** to place the offlabel of @p obj into
319 elm_toggle_states_labels_get(const Evas_Object *obj, const char **onlabel, const char **offlabel)
321 if (onlabel) *onlabel = NULL;
322 if (offlabel) *offlabel = NULL;
323 ELM_CHECK_WIDTYPE(obj, widtype);
324 Widget_Data *wd = elm_widget_data_get(obj);
326 if (onlabel) *onlabel = wd->ontext;
327 if (offlabel) *offlabel = wd->offtext;
331 * Sets the state of the toggle to @p state.
333 * @param obj The toggle object
334 * @param state The state of @p obj
339 elm_toggle_state_set(Evas_Object *obj, Eina_Bool state)
341 ELM_CHECK_WIDTYPE(obj, widtype);
342 Widget_Data *wd = elm_widget_data_get(obj);
344 if (state != wd->state)
347 if (wd->statep) *wd->statep = wd->state;
349 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
351 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");
356 * Gets the state of the toggle to @p state.
358 * @param obj The toggle object
359 * @return The state of @p obj
364 elm_toggle_state_get(const Evas_Object *obj)
366 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
367 Widget_Data *wd = elm_widget_data_get(obj);
368 if (!wd) return EINA_FALSE;
373 * Sets the state pointer of the toggle to @p statep.
375 * @param obj The toggle object
376 * @param statep The state pointer of @p obj
381 elm_toggle_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
383 ELM_CHECK_WIDTYPE(obj, widtype);
384 Widget_Data *wd = elm_widget_data_get(obj);
389 if (*wd->statep != wd->state)
391 wd->state = *wd->statep;
393 edje_object_signal_emit(wd->tgl, "elm,state,toggle,on", "elm");
395 edje_object_signal_emit(wd->tgl, "elm,state,toggle,off", "elm");