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 _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
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 void _activate_hook(Evas_Object *obj);
45 static void _activate(Evas_Object *obj);
46 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
47 Evas_Callback_Type type, void *event_info);
49 static const char SIG_CHANGED[] = "changed";
50 static const Evas_Smart_Cb_Description _signals[] = {
56 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
58 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
59 Evas_Event_Key_Down *ev = event_info;
60 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
61 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
62 if ((strcmp(ev->keyname, "Return")) &&
63 (strcmp(ev->keyname, "KP_Enter")) &&
64 (strcmp(ev->keyname, "space")))
67 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
73 _del_hook(Evas_Object *obj)
75 Widget_Data *wd = elm_widget_data_get(obj);
77 if (wd->label) eina_stringshare_del(wd->label);
82 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
84 Widget_Data *wd = elm_widget_data_get(obj);
86 if (elm_widget_focus_get(obj))
88 edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
89 evas_object_focus_set(wd->chk, EINA_TRUE);
93 edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
94 evas_object_focus_set(wd->chk, EINA_FALSE);
99 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
101 Widget_Data *wd = elm_widget_data_get(obj);
103 edje_object_mirrored_set(wd->chk, rtl);
107 _theme_hook(Evas_Object *obj)
109 Widget_Data *wd = elm_widget_data_get(obj);
111 _elm_widget_mirrored_reload(obj);
112 _mirrored_set(obj, elm_widget_mirrored_get(obj));
113 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
115 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
117 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
119 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
121 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
123 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
125 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
126 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
127 if (elm_widget_disabled_get(obj))
128 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
129 edje_object_message_signal_process(wd->chk);
130 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
135 _disable_hook(Evas_Object *obj)
137 Widget_Data *wd = elm_widget_data_get(obj);
139 if (elm_widget_disabled_get(obj))
140 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
142 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
146 _sizing_eval(Evas_Object *obj)
148 Widget_Data *wd = elm_widget_data_get(obj);
149 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
151 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
152 edje_object_size_min_restricted_calc(wd->chk, &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, maxw, maxh);
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;
165 evas_object_size_hint_min_get(obj, &mw, &mh);
170 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
172 Widget_Data *wd = elm_widget_data_get(obj);
173 Evas_Object *sub = event_info;
177 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
178 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
179 _changed_size_hints, obj);
182 edje_object_message_signal_process(wd->chk);
187 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
189 Widget_Data *wd = elm_widget_data_get(data);
191 wd->state = EINA_FALSE;
192 if (wd->statep) *wd->statep = wd->state;
193 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
194 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
198 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
200 Widget_Data *wd = elm_widget_data_get(data);
202 wd->state = EINA_TRUE;
203 if (wd->statep) *wd->statep = wd->state;
204 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
205 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
209 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
215 _activate_hook(Evas_Object *obj)
221 _activate(Evas_Object *obj)
223 Widget_Data *wd = elm_widget_data_get(obj);
225 wd->state = !wd->state;
226 if (wd->statep) *wd->statep = wd->state;
228 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
230 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
231 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
235 * Add a new Check object
237 * @param parent The parent object
238 * @return The new object or NULL if it cannot be created
243 elm_check_add(Evas_Object *parent)
249 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
251 ELM_SET_WIDTYPE(widtype, "check");
252 elm_widget_type_set(obj, "check");
253 elm_widget_sub_object_add(parent, obj);
254 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
255 elm_widget_data_set(obj, wd);
256 elm_widget_del_hook_set(obj, _del_hook);
257 elm_widget_theme_hook_set(obj, _theme_hook);
258 elm_widget_disable_hook_set(obj, _disable_hook);
259 elm_widget_can_focus_set(obj, EINA_TRUE);
260 elm_widget_activate_hook_set(obj, _activate_hook);
261 elm_widget_event_hook_set(obj, _event_hook);
263 wd->chk = edje_object_add(e);
264 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
265 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
266 _signal_check_on, obj);
267 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
268 _signal_check_off, obj);
269 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
270 _signal_check_toggle, obj);
271 elm_widget_resize_object_set(obj, wd->chk);
273 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
275 _mirrored_set(obj, elm_widget_mirrored_get(obj));
278 // TODO: convert Elementary to subclassing of Evas_Smart_Class
279 // TODO: and save some bytes, making descriptions per-class and not instance!
280 evas_object_smart_callbacks_descriptions_set(obj, _signals);
285 * Set the text label of the check object
287 * @param obj The check object
288 * @param label The text label string in UTF-8
293 elm_check_label_set(Evas_Object *obj, const char *label)
295 ELM_CHECK_WIDTYPE(obj, widtype);
296 Widget_Data *wd = elm_widget_data_get(obj);
298 eina_stringshare_replace(&wd->label, label);
300 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
302 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
303 edje_object_message_signal_process(wd->chk);
304 edje_object_part_text_set(wd->chk, "elm.text", label);
309 * Get the text label of the check object
311 * @param obj The check object
312 * @return The text label string in UTF-8
317 elm_check_label_get(const Evas_Object *obj)
319 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
320 Widget_Data *wd = elm_widget_data_get(obj);
321 if (!wd) return NULL;
326 * Set the icon object of the check object
328 * Once the icon object is set, a previously set one will be deleted.
329 * If you want to keep that old content object, use the
330 * elm_check_icon_unset() function.
332 * @param obj The check object
333 * @param icon The icon object
338 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
340 ELM_CHECK_WIDTYPE(obj, widtype);
341 Widget_Data *wd = elm_widget_data_get(obj);
343 if (wd->icon == icon) return;
344 if (wd->icon) evas_object_del(wd->icon);
348 elm_widget_sub_object_add(obj, icon);
349 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
350 _changed_size_hints, obj);
351 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
352 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
353 edje_object_message_signal_process(wd->chk);
359 * Get the icon object of the check object
361 * @param obj The check object
362 * @return The icon object
367 elm_check_icon_get(const Evas_Object *obj)
369 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
370 Widget_Data *wd = elm_widget_data_get(obj);
371 if (!wd) return NULL;
376 * Unset the icon used for the check object
378 * Unparent and return the icon object which was set for this widget.
380 * @param obj The check object
381 * @return The icon object that was being used
386 elm_check_icon_unset(Evas_Object *obj)
388 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
389 Widget_Data *wd = elm_widget_data_get(obj);
390 if (!wd) return NULL;
391 if (!wd->icon) return NULL;
392 Evas_Object *icon = wd->icon;
393 elm_widget_sub_object_del(obj, wd->icon);
394 edje_object_part_unswallow(wd->chk, wd->icon);
400 * Set the on/off state of the check object
402 * This sets the state of the check and will also set the value if pointed to
403 * to the state supplied, but will not call any callbacks.
405 * @param obj The check object
406 * @param state The state to use (1 == on, 0 == off)
411 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
413 ELM_CHECK_WIDTYPE(obj, widtype);
414 Widget_Data *wd = elm_widget_data_get(obj);
416 if (state != wd->state)
419 if (wd->statep) *wd->statep = wd->state;
421 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
423 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
428 * Get the state of the check object
430 * @param obj The check object
431 * @return The boolean state
436 elm_check_state_get(const Evas_Object *obj)
438 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
439 Widget_Data *wd = elm_widget_data_get(obj);
440 if (!wd) return EINA_FALSE;
445 * Set a convenience pointer to a boolean to change
447 * This sets a pointer to a boolean, that, in addition to the check objects
448 * state will also be modified directly. To stop setting the object pointed
449 * to simply use NULL as the statep parameter. If statep is not NULL, then
450 * when this is called, the check objects state will also be modified to
451 * reflect the value of the boolean statep points to, just like calling
452 * elm_check_state_set().
454 * @param obj The check object
455 * @param statep Pointer to the boolean to modify
460 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
462 ELM_CHECK_WIDTYPE(obj, widtype);
463 Widget_Data *wd = elm_widget_data_get(obj);
468 if (*wd->statep != wd->state)
470 wd->state = *wd->statep;
472 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
474 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");