1 #include <Elementary.h>
5 * @defgroup Check Check
8 * The check widget allows for toggling a value between true or false (1 or 0).
10 * Signals that you can add callbacks for are:
12 * changed - This is called whenever the user changes the state of one of the
15 * Check objects are a lot like radio objects in layout and functionality
16 * except they do not work as a group, but independently and only toggle the
17 * value of a boolean from false to true (0 or 1). elm_check_state_set() sets
18 * the boolean state (1 for true, 0 for false), and elm_check_state_get()
19 * returns the current state. For convenience, like the radio objects, you
20 * can set a pointer to a boolean directly with elm_check_state_pointer_set()
23 typedef struct _Widget_Data Widget_Data;
27 Evas_Object *chk, *icon;
33 static const char *widtype = NULL;
34 static void _del_hook(Evas_Object *obj);
35 static void _theme_hook(Evas_Object *obj);
36 static void _disable_hook(Evas_Object *obj);
37 static void _sizing_eval(Evas_Object *obj);
38 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
39 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
40 static void _signal_check_off(void *data, Evas_Object *obj, const char *emission, const char *source);
41 static void _signal_check_on(void *data, Evas_Object *obj, const char *emission, const char *source);
42 static void _signal_check_toggle(void *data, Evas_Object *obj, const char *emission, const char *source);
43 static void _on_focus_hook(void *data, Evas_Object *obj);
44 static const char SIG_CHANGED[] = "changed";
45 static const Evas_Smart_Cb_Description _signals[] = {
51 _del_hook(Evas_Object *obj)
53 Widget_Data *wd = elm_widget_data_get(obj);
55 if (wd->label) eina_stringshare_del(wd->label);
60 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
62 Widget_Data *wd = elm_widget_data_get(obj);
64 if (elm_widget_focus_get(obj))
66 edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
67 evas_object_focus_set(wd->chk, EINA_TRUE);
71 edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
72 evas_object_focus_set(wd->chk, EINA_FALSE);
77 _theme_hook(Evas_Object *obj)
79 Widget_Data *wd = elm_widget_data_get(obj);
81 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
83 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
85 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
87 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
89 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
91 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
93 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
94 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
95 if (elm_widget_disabled_get(obj))
96 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
97 edje_object_message_signal_process(wd->chk);
98 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
103 _disable_hook(Evas_Object *obj)
105 Widget_Data *wd = elm_widget_data_get(obj);
107 if (elm_widget_disabled_get(obj))
108 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
110 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
114 _sizing_eval(Evas_Object *obj)
116 Widget_Data *wd = elm_widget_data_get(obj);
117 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
119 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
120 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
121 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
122 evas_object_size_hint_min_set(obj, minw, minh);
123 evas_object_size_hint_max_set(obj, maxw, maxh);
127 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
129 Widget_Data *wd = elm_widget_data_get(data);
131 if (obj != wd->icon) return;
133 evas_object_size_hint_min_get(obj, &mw, &mh);
138 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
140 Widget_Data *wd = elm_widget_data_get(obj);
141 Evas_Object *sub = event_info;
145 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
146 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
147 _changed_size_hints, obj);
150 edje_object_message_signal_process(wd->chk);
155 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
157 Widget_Data *wd = elm_widget_data_get(data);
159 wd->state = EINA_FALSE;
160 if (wd->statep) *wd->statep = wd->state;
161 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
162 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
166 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
168 Widget_Data *wd = elm_widget_data_get(data);
170 wd->state = EINA_TRUE;
171 if (wd->statep) *wd->statep = wd->state;
172 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
173 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
177 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
179 Widget_Data *wd = elm_widget_data_get(data);
181 wd->state = !wd->state;
182 if (wd->statep) *wd->statep = wd->state;
184 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
186 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
187 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
191 * Add a new Check object
193 * @param[in] parent The parent object
194 * @return The new object or NULL if it cannot be created
199 elm_check_add(Evas_Object *parent)
205 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
207 wd = ELM_NEW(Widget_Data);
208 e = evas_object_evas_get(parent);
209 obj = elm_widget_add(e);
210 ELM_SET_WIDTYPE(widtype, "check");
211 elm_widget_type_set(obj, "check");
212 elm_widget_can_focus_set(obj, EINA_TRUE);
213 elm_widget_sub_object_add(parent, obj);
214 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
215 elm_widget_data_set(obj, wd);
216 elm_widget_del_hook_set(obj, _del_hook);
217 elm_widget_theme_hook_set(obj, _theme_hook);
218 elm_widget_disable_hook_set(obj, _disable_hook);
220 wd->chk = edje_object_add(e);
221 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
222 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
223 _signal_check_on, obj);
224 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
225 _signal_check_off, obj);
226 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
227 _signal_check_toggle, obj);
228 elm_widget_resize_object_set(obj, wd->chk);
230 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
234 // TODO: convert Elementary to subclassing of Evas_Smart_Class
235 // TODO: and save some bytes, making descriptions per-class and not instance!
236 evas_object_smart_callbacks_descriptions_set(obj, _signals);
241 * Set the text label of the check object
243 * @param[in] obj The check object
244 * @param[in] label The text label string in UTF-8
249 elm_check_label_set(Evas_Object *obj, const char *label)
251 ELM_CHECK_WIDTYPE(obj, widtype);
252 Widget_Data *wd = elm_widget_data_get(obj);
254 eina_stringshare_replace(&wd->label, label);
256 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
258 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
259 edje_object_message_signal_process(wd->chk);
260 edje_object_part_text_set(wd->chk, "elm.text", label);
265 * Get the text label of the check object
267 * @param[in] obj The check object
268 * @return The text label string in UTF-8
273 elm_check_label_get(const Evas_Object *obj)
275 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
276 Widget_Data *wd = elm_widget_data_get(obj);
277 if (!wd) return NULL;
282 * Set the icon object of the check object
284 * Once the icon object is set, a previously set one will be deleted.
285 * If you want to keep that old content object, use the
286 * elm_check_icon_unset() function.
288 * @param[in] obj The check object
289 * @param[in] icon The icon object
294 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
296 ELM_CHECK_WIDTYPE(obj, widtype);
297 Widget_Data *wd = elm_widget_data_get(obj);
299 if (wd->icon == icon) return;
300 if (wd->icon) evas_object_del(wd->icon);
304 elm_widget_sub_object_add(obj, icon);
305 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
306 _changed_size_hints, obj);
307 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
308 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
309 edje_object_message_signal_process(wd->chk);
315 * Get the icon object of the check object
317 * @param[in] obj The check object
318 * @return The icon object
323 elm_check_icon_get(const Evas_Object *obj)
325 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
326 Widget_Data *wd = elm_widget_data_get(obj);
327 if (!wd) return NULL;
332 * Unset the icon used for the check object
334 * Unparent and return the icon object which was set for this widget.
336 * @param[in] obj The check object
337 * @return The icon object that was being used
342 elm_check_icon_unset(Evas_Object *obj)
344 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
345 Widget_Data *wd = elm_widget_data_get(obj);
346 if (!wd) return NULL;
347 if (!wd->icon) return NULL;
348 Evas_Object *icon = wd->icon;
349 elm_widget_sub_object_del(obj, wd->icon);
350 edje_object_part_unswallow(wd->chk, wd->icon);
356 * Set the on/off state of the check object
358 * This sets the state of the check and will also set the value if pointed to
359 * to the state supplied, but will not call any callbacks.
361 * @param[in] obj The check object
362 * @param[in] state The state to use (1 == on, 0 == off)
367 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
369 ELM_CHECK_WIDTYPE(obj, widtype);
370 Widget_Data *wd = elm_widget_data_get(obj);
372 if (state != wd->state)
375 if (wd->statep) *wd->statep = wd->state;
377 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
379 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
384 * Get the state of the check object
386 * @param[in] obj The check object
387 * @return The boolean state
392 elm_check_state_get(const Evas_Object *obj)
394 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
395 Widget_Data *wd = elm_widget_data_get(obj);
396 if (!wd) return EINA_FALSE;
401 * Set a convenience pointer to a boolean to change
403 * This sets a pointer to a boolean, that, in addition to the check objects
404 * state will also be modified directly. To stop setting the object pointed
405 * to simply use NULL as the statep parameter. If statep is not NULL, then
406 * when this is called, the check objects state will also be modified to
407 * reflect the value of the boolean statep points to, just like calling
408 * elm_check_state_set().
410 * @param[in] obj The check object
411 * @param[in] statep Pointer to the boolean to modify
416 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
418 ELM_CHECK_WIDTYPE(obj, widtype);
419 Widget_Data *wd = elm_widget_data_get(obj);
424 if (*wd->statep != wd->state)
426 wd->state = *wd->statep;
428 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
430 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");