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 _on_focus_hook(void *data, Evas_Object *obj);
38 static void _sizing_eval(Evas_Object *obj);
39 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
40 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
41 static void _signal_check_off(void *data, Evas_Object *obj, const char *emission, const char *source);
42 static void _signal_check_on(void *data, Evas_Object *obj, const char *emission, const char *source);
43 static void _signal_check_toggle(void *data, Evas_Object *obj, const char *emission, const char *source);
45 static const char SIG_CHANGED[] = "changed";
46 static const char SIG_UNFOCUSED[] = "unfocused";
47 static const Evas_Smart_Cb_Description _signals[] = {
54 _del_hook(Evas_Object *obj)
56 Widget_Data *wd = elm_widget_data_get(obj);
58 if (wd->label) eina_stringshare_del(wd->label);
63 _theme_hook(Evas_Object *obj)
65 Widget_Data *wd = elm_widget_data_get(obj);
67 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
69 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
71 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
73 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
75 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
77 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
79 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
80 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
81 edje_object_message_signal_process(wd->chk);
82 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
87 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
89 Widget_Data *wd = elm_widget_data_get(obj);
91 if (elm_widget_focus_get(obj))
94 evas_object_focus_set(wd->chk, EINA_TRUE);
98 evas_object_smart_callback_call(obj, SIG_UNFOCUSED, NULL);
99 evas_object_focus_set(wd->chk, EINA_FALSE);
104 _disable_hook(Evas_Object *obj)
106 Widget_Data *wd = elm_widget_data_get(obj);
108 if (elm_widget_disabled_get(obj))
109 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
111 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
115 _sizing_eval(Evas_Object *obj)
117 Widget_Data *wd = elm_widget_data_get(obj);
118 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
120 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
121 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
122 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
123 evas_object_size_hint_min_set(obj, minw, minh);
124 evas_object_size_hint_max_set(obj, maxw, maxh);
128 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
130 Widget_Data *wd = elm_widget_data_get(data);
132 if (obj != wd->icon) return;
134 evas_object_size_hint_min_get(obj, &mw, &mh);
139 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
141 Widget_Data *wd = elm_widget_data_get(obj);
142 Evas_Object *sub = event_info;
146 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
147 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
148 _changed_size_hints, obj);
151 edje_object_message_signal_process(wd->chk);
156 _signal_check_off(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 = EINA_FALSE;
161 if (wd->statep) *wd->statep = wd->state;
162 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
163 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
167 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
169 Widget_Data *wd = elm_widget_data_get(data);
171 wd->state = EINA_TRUE;
172 if (wd->statep) *wd->statep = wd->state;
173 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
174 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
178 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
180 Widget_Data *wd = elm_widget_data_get(data);
182 wd->state = !wd->state;
183 if (wd->statep) *wd->statep = wd->state;
185 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
187 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
188 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
192 * Add a new Check object
194 * @param[in] parent The parent object
195 * @return The new object or NULL if it cannot be created
200 elm_check_add(Evas_Object *parent)
206 wd = ELM_NEW(Widget_Data);
207 e = evas_object_evas_get(parent);
208 obj = elm_widget_add(e);
209 ELM_SET_WIDTYPE(widtype, "check");
210 elm_widget_type_set(obj, "check");
211 elm_widget_can_focus_set(obj, EINA_TRUE);
212 elm_widget_sub_object_add(parent, obj);
213 elm_widget_data_set(obj, wd);
214 elm_widget_del_hook_set(obj, _del_hook);
215 elm_widget_theme_hook_set(obj, _theme_hook);
216 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL );
217 elm_widget_disable_hook_set(obj, _disable_hook);
219 wd->chk = edje_object_add(e);
220 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
221 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
222 _signal_check_on, obj);
223 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
224 _signal_check_off, obj);
225 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
226 _signal_check_toggle, obj);
227 elm_widget_resize_object_set(obj, wd->chk);
229 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
233 // TODO: convert Elementary to subclassing of Evas_Smart_Class
234 // TODO: and save some bytes, making descriptions per-class and not instance!
235 evas_object_smart_callbacks_descriptions_set(obj, _signals);
240 * Set the text label of the check object
242 * @param[in] obj The check object
243 * @param[in] label The text label string in UTF-8
248 elm_check_label_set(Evas_Object *obj, const char *label)
250 ELM_CHECK_WIDTYPE(obj, widtype);
251 Widget_Data *wd = elm_widget_data_get(obj);
253 eina_stringshare_replace(&wd->label, label);
255 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
257 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
258 edje_object_message_signal_process(wd->chk);
259 edje_object_part_text_set(wd->chk, "elm.text", label);
264 * Get the text label of the check object
266 * @param[in] obj The check object
267 * @return The text label string in UTF-8
272 elm_check_label_get(const Evas_Object *obj)
274 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
275 Widget_Data *wd = elm_widget_data_get(obj);
276 if (!wd) return NULL;
281 * Set the icon object of the check object
283 * Once the icon object is set, a previously set one will be deleted.
285 * @param[in] obj The check object
286 * @param[in] icon The icon object
291 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
293 ELM_CHECK_WIDTYPE(obj, widtype);
294 Widget_Data *wd = elm_widget_data_get(obj);
296 if (wd->icon == icon) return;
297 if (wd->icon) evas_object_del(wd->icon);
301 elm_widget_sub_object_add(obj, icon);
302 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
303 _changed_size_hints, obj);
304 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
305 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
306 edje_object_message_signal_process(wd->chk);
312 * Get the icon object of the check object
314 * @param[in] obj The check object
315 * @return The icon object
320 elm_check_icon_get(const Evas_Object *obj)
322 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
323 Widget_Data *wd = elm_widget_data_get(obj);
324 if (!wd) return NULL;
329 * Set the on/off state of the check object
331 * This sets the state of the check and will also set the value if pointed to
332 * to the state supplied, but will not call any callbacks.
334 * @param[in] obj The check object
335 * @param[in] state The state to use (1 == on, 0 == off)
340 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
342 ELM_CHECK_WIDTYPE(obj, widtype);
343 Widget_Data *wd = elm_widget_data_get(obj);
345 if (state != wd->state)
348 if (wd->statep) *wd->statep = wd->state;
350 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
352 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
357 * Get the state of the check object
359 * @param[in] obj The check object
360 * @return The boolean state
365 elm_check_state_get(const Evas_Object *obj)
367 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
368 Widget_Data *wd = elm_widget_data_get(obj);
369 if (!wd) return EINA_FALSE;
374 * Set a convenience pointer to a boolean to change
376 * This sets a pointer to a boolean, that, in addition to the check objects
377 * state will also be modified directly. To stop setting the object pointed
378 * to simply use NULL as the statep parameter. If statep is not NULL, then
379 * when this is called, the check objects state will also be modified to
380 * reflect the value of the boolean statep points to, just like calling
381 * elm_check_state_set().
383 * @param[in] obj The check object
384 * @param[in] statep Pointer to the boolean to modify
389 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
391 ELM_CHECK_WIDTYPE(obj, widtype);
392 Widget_Data *wd = elm_widget_data_get(obj);
397 if (*wd->statep != wd->state)
399 wd->state = *wd->statep;
401 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
403 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");