1 #include <Elementary.h>
5 * @defgroup Check Check
7 * The check widget allows for toggling a value between true or false (1 or 0).
9 * Signals that you can add callbacks for are:
11 * changed - This is called whenever the user changes the state of one of the
14 * Check objects are a lot like radio objects in layout and functionality
15 * except they do not work as a group, but independently and only toggle the
16 * value of a boolean from false to true (0 or 1). elm_check_state_set() sets
17 * the boolean state (1 for true, 0 for false), and elm_check_state_get()
18 * returns the current state. For convenience, like the radio objects, you
19 * can set a pointer to a boolean directly with elm_check_state_pointer_set()
22 typedef struct _Widget_Data Widget_Data;
26 Evas_Object *chk, *icon;
32 static const char *widtype = NULL;
33 static void _del_hook(Evas_Object *obj);
34 static void _theme_hook(Evas_Object *obj);
35 static void _disable_hook(Evas_Object *obj);
36 static void _sizing_eval(Evas_Object *obj);
37 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
38 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
39 static void _signal_check_off(void *data, Evas_Object *obj, const char *emission, const char *source);
40 static void _signal_check_on(void *data, Evas_Object *obj, const char *emission, const char *source);
41 static void _signal_check_toggle(void *data, Evas_Object *obj, const char *emission, const char *source);
43 static const char SIG_CHANGED[] = "changed";
44 static const Evas_Smart_Cb_Description _signals[] = {
50 _del_hook(Evas_Object *obj)
52 Widget_Data *wd = elm_widget_data_get(obj);
54 if (wd->label) eina_stringshare_del(wd->label);
59 _theme_hook(Evas_Object *obj)
61 Widget_Data *wd = elm_widget_data_get(obj);
63 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
65 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
67 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
69 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
71 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
73 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
75 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
76 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
77 edje_object_message_signal_process(wd->chk);
78 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
83 _disable_hook(Evas_Object *obj)
85 Widget_Data *wd = elm_widget_data_get(obj);
87 if (elm_widget_disabled_get(obj))
88 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
90 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
94 _sizing_eval(Evas_Object *obj)
96 Widget_Data *wd = elm_widget_data_get(obj);
97 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
99 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
100 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
101 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
102 evas_object_size_hint_min_set(obj, minw, minh);
103 evas_object_size_hint_max_set(obj, maxw, maxh);
107 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
109 Widget_Data *wd = elm_widget_data_get(data);
111 if (obj != wd->icon) return;
113 evas_object_size_hint_min_get(obj, &mw, &mh);
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->chk, "elm,state,icon,hidden", "elm");
126 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
127 _changed_size_hints, obj);
134 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
136 Widget_Data *wd = elm_widget_data_get(data);
138 wd->state = EINA_FALSE;
139 if (wd->statep) *wd->statep = wd->state;
140 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
141 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
145 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
147 Widget_Data *wd = elm_widget_data_get(data);
149 wd->state = EINA_TRUE;
150 if (wd->statep) *wd->statep = wd->state;
151 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
152 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
156 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
158 Widget_Data *wd = elm_widget_data_get(data);
160 wd->state = !wd->state;
161 if (wd->statep) *wd->statep = wd->state;
163 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
165 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
166 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
170 * Add a new Check object
172 * @param parent The parent object
173 * @return The new object or NULL if it cannot be created
178 elm_check_add(Evas_Object *parent)
184 wd = ELM_NEW(Widget_Data);
185 e = evas_object_evas_get(parent);
186 obj = elm_widget_add(e);
187 ELM_SET_WIDTYPE(widtype, "check");
188 elm_widget_type_set(obj, "check");
189 elm_widget_sub_object_add(parent, obj);
190 elm_widget_data_set(obj, wd);
191 elm_widget_del_hook_set(obj, _del_hook);
192 elm_widget_theme_hook_set(obj, _theme_hook);
193 elm_widget_disable_hook_set(obj, _disable_hook);
195 wd->chk = edje_object_add(e);
196 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
197 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
198 _signal_check_on, obj);
199 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
200 _signal_check_off, obj);
201 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
202 _signal_check_toggle, obj);
203 elm_widget_resize_object_set(obj, wd->chk);
205 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
209 // TODO: convert Elementary to subclassing of Evas_Smart_Class
210 // TODO: and save some bytes, making descriptions per-class and not instance!
211 evas_object_smart_callbacks_descriptions_set(obj, _signals);
216 * Set the text label of the check object
218 * @param obj The check object
219 * @param label The text label string in UTF-8
224 elm_check_label_set(Evas_Object *obj, const char *label)
226 ELM_CHECK_WIDTYPE(obj, widtype);
227 Widget_Data *wd = elm_widget_data_get(obj);
229 eina_stringshare_replace(&wd->label, label);
231 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
233 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
234 edje_object_message_signal_process(wd->chk);
235 edje_object_part_text_set(wd->chk, "elm.text", label);
240 * Get the text label of the check object
242 * @param obj The check object
243 * @return The text label string in UTF-8
248 elm_check_label_get(const Evas_Object *obj)
250 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
251 Widget_Data *wd = elm_widget_data_get(obj);
252 if (!wd) return NULL;
257 * Set the icon object of the check object
259 * Once the icon object is set, it will become a child of the check object and
260 * be deleted when the check object is deleted. If another icon object is set
261 * then the previous one becomes orophaned and will no longer be deleted along
264 * @param obj The check object
265 * @param icon The icon object
270 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
272 ELM_CHECK_WIDTYPE(obj, widtype);
273 Widget_Data *wd = elm_widget_data_get(obj);
275 if ((wd->icon != icon) && (wd->icon))
276 elm_widget_sub_object_del(obj, wd->icon);
280 elm_widget_sub_object_add(obj, icon);
281 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
282 _changed_size_hints, obj);
283 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
284 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
290 * Get the icon object of the check object
292 * @param obj The check object
293 * @return The icon object
298 elm_check_icon_get(const Evas_Object *obj)
300 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
301 Widget_Data *wd = elm_widget_data_get(obj);
302 if (!wd) return NULL;
307 * Set the on/off state of the check object
309 * This sets the state of the check and will also set the value if pointed to
310 * to the state supplied, but will not call any callbacks.
312 * @param obj The check object
313 * @param state The state to use (1 == on, 0 == off)
318 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
320 ELM_CHECK_WIDTYPE(obj, widtype);
321 Widget_Data *wd = elm_widget_data_get(obj);
323 if (state != wd->state)
326 if (wd->statep) *wd->statep = wd->state;
328 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
330 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
335 * Get the state of the check object
337 * @param obj The check object
338 * @return The boolean state
343 elm_check_state_get(const Evas_Object *obj)
345 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
346 Widget_Data *wd = elm_widget_data_get(obj);
347 if (!wd) return EINA_FALSE;
352 * Set a convenience pointer to a boolean to change
354 * This sets a pointer to a boolean, that, in addition to the check objects
355 * state will also be modified directly. To stop setting the object pointed
356 * to simply use NULL as the statep parameter. If statep is not NULL, then
357 * when this is called, the check objects state will also be modified to
358 * reflect the value of the boolean statep points to, just like calling
359 * elm_check_state_set().
361 * @param obj The check object
362 * @param statep Pointer to the boolean to modify
367 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
369 ELM_CHECK_WIDTYPE(obj, widtype);
370 Widget_Data *wd = elm_widget_data_get(obj);
375 if (*wd->statep != wd->state)
377 wd->state = *wd->statep;
379 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
381 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");