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);
42 static void _on_focus_hook(void *data, Evas_Object *obj);
43 static void _activate_hook(Evas_Object *obj);
44 static void _activate(Evas_Object *obj);
45 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
46 Evas_Callback_Type type, void *event_info);
48 static const char SIG_CHANGED[] = "changed";
49 static const Evas_Smart_Cb_Description _signals[] = {
55 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
57 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
58 Evas_Event_Key_Down *ev = event_info;
59 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
60 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
61 if ((strcmp(ev->keyname, "Return")) &&
62 (strcmp(ev->keyname, "KP_Enter")) &&
63 (strcmp(ev->keyname, "space")))
66 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
72 _del_hook(Evas_Object *obj)
74 Widget_Data *wd = elm_widget_data_get(obj);
76 if (wd->label) eina_stringshare_del(wd->label);
81 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
83 Widget_Data *wd = elm_widget_data_get(obj);
85 if (elm_widget_focus_get(obj))
87 edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
88 evas_object_focus_set(wd->chk, EINA_TRUE);
92 edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
93 evas_object_focus_set(wd->chk, EINA_FALSE);
98 _theme_hook(Evas_Object *obj)
100 Widget_Data *wd = elm_widget_data_get(obj);
102 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
104 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
106 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
108 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
110 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
112 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
114 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
115 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
116 if (elm_widget_disabled_get(obj))
117 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
118 edje_object_message_signal_process(wd->chk);
119 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
124 _disable_hook(Evas_Object *obj)
126 Widget_Data *wd = elm_widget_data_get(obj);
128 if (elm_widget_disabled_get(obj))
129 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
131 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
135 _sizing_eval(Evas_Object *obj)
137 Widget_Data *wd = elm_widget_data_get(obj);
138 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
140 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
141 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
142 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
143 evas_object_size_hint_min_set(obj, minw, minh);
144 evas_object_size_hint_max_set(obj, maxw, maxh);
148 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
150 Widget_Data *wd = elm_widget_data_get(data);
152 if (obj != wd->icon) return;
154 evas_object_size_hint_min_get(obj, &mw, &mh);
159 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
161 Widget_Data *wd = elm_widget_data_get(obj);
162 Evas_Object *sub = event_info;
166 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
167 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
168 _changed_size_hints, obj);
171 edje_object_message_signal_process(wd->chk);
176 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
178 Widget_Data *wd = elm_widget_data_get(data);
180 wd->state = EINA_FALSE;
181 if (wd->statep) *wd->statep = wd->state;
182 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
183 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
187 _signal_check_on(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_TRUE;
192 if (wd->statep) *wd->statep = wd->state;
193 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
194 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
198 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
204 _activate_hook(Evas_Object *obj)
210 _activate(Evas_Object *obj)
212 Widget_Data *wd = elm_widget_data_get(obj);
214 wd->state = !wd->state;
215 if (wd->statep) *wd->statep = wd->state;
217 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
219 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
220 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
224 * Add a new Check object
226 * @param parent The parent object
227 * @return The new object or NULL if it cannot be created
232 elm_check_add(Evas_Object *parent)
238 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
240 wd = ELM_NEW(Widget_Data);
241 e = evas_object_evas_get(parent);
243 obj = elm_widget_add(e);
244 ELM_SET_WIDTYPE(widtype, "check");
245 elm_widget_type_set(obj, "check");
246 elm_widget_sub_object_add(parent, obj);
247 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
248 elm_widget_data_set(obj, wd);
249 elm_widget_del_hook_set(obj, _del_hook);
250 elm_widget_theme_hook_set(obj, _theme_hook);
251 elm_widget_disable_hook_set(obj, _disable_hook);
252 elm_widget_can_focus_set(obj, EINA_TRUE);
253 elm_widget_activate_hook_set(obj, _activate_hook);
254 elm_widget_event_hook_set(obj, _event_hook);
256 wd->chk = edje_object_add(e);
257 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
258 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
259 _signal_check_on, obj);
260 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
261 _signal_check_off, obj);
262 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
263 _signal_check_toggle, obj);
264 elm_widget_resize_object_set(obj, wd->chk);
266 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
270 // TODO: convert Elementary to subclassing of Evas_Smart_Class
271 // TODO: and save some bytes, making descriptions per-class and not instance!
272 evas_object_smart_callbacks_descriptions_set(obj, _signals);
277 * Set the text label of the check object
279 * @param obj The check object
280 * @param label The text label string in UTF-8
285 elm_check_label_set(Evas_Object *obj, const char *label)
287 ELM_CHECK_WIDTYPE(obj, widtype);
288 Widget_Data *wd = elm_widget_data_get(obj);
290 eina_stringshare_replace(&wd->label, label);
292 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
294 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
295 edje_object_message_signal_process(wd->chk);
296 edje_object_part_text_set(wd->chk, "elm.text", label);
301 * Get the text label of the check object
303 * @param obj The check object
304 * @return The text label string in UTF-8
309 elm_check_label_get(const Evas_Object *obj)
311 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
312 Widget_Data *wd = elm_widget_data_get(obj);
313 if (!wd) return NULL;
318 * Set the icon object of the check object
320 * Once the icon object is set, a previously set one will be deleted.
321 * If you want to keep that old content object, use the
322 * elm_check_icon_unset() function.
324 * @param obj The check object
325 * @param icon The icon object
330 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
332 ELM_CHECK_WIDTYPE(obj, widtype);
333 Widget_Data *wd = elm_widget_data_get(obj);
335 if (wd->icon == icon) return;
336 if (wd->icon) evas_object_del(wd->icon);
340 elm_widget_sub_object_add(obj, icon);
341 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
342 _changed_size_hints, obj);
343 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
344 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
345 edje_object_message_signal_process(wd->chk);
351 * Get the icon object of the check object
353 * @param obj The check object
354 * @return The icon object
359 elm_check_icon_get(const Evas_Object *obj)
361 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
362 Widget_Data *wd = elm_widget_data_get(obj);
363 if (!wd) return NULL;
368 * Unset the icon used for the check object
370 * Unparent and return the icon object which was set for this widget.
372 * @param obj The check object
373 * @return The icon object that was being used
378 elm_check_icon_unset(Evas_Object *obj)
380 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
381 Widget_Data *wd = elm_widget_data_get(obj);
382 if (!wd) return NULL;
383 if (!wd->icon) return NULL;
384 Evas_Object *icon = wd->icon;
385 elm_widget_sub_object_del(obj, wd->icon);
386 edje_object_part_unswallow(wd->chk, wd->icon);
392 * Set the on/off state of the check object
394 * This sets the state of the check and will also set the value if pointed to
395 * to the state supplied, but will not call any callbacks.
397 * @param obj The check object
398 * @param state The state to use (1 == on, 0 == off)
403 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
405 ELM_CHECK_WIDTYPE(obj, widtype);
406 Widget_Data *wd = elm_widget_data_get(obj);
408 if (state != wd->state)
411 if (wd->statep) *wd->statep = wd->state;
413 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
415 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
420 * Get the state of the check object
422 * @param obj The check object
423 * @return The boolean state
428 elm_check_state_get(const Evas_Object *obj)
430 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
431 Widget_Data *wd = elm_widget_data_get(obj);
432 if (!wd) return EINA_FALSE;
437 * Set a convenience pointer to a boolean to change
439 * This sets a pointer to a boolean, that, in addition to the check objects
440 * state will also be modified directly. To stop setting the object pointed
441 * to simply use NULL as the statep parameter. If statep is not NULL, then
442 * when this is called, the check objects state will also be modified to
443 * reflect the value of the boolean statep points to, just like calling
444 * elm_check_state_set().
446 * @param obj The check object
447 * @param statep Pointer to the boolean to modify
452 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
454 ELM_CHECK_WIDTYPE(obj, widtype);
455 Widget_Data *wd = elm_widget_data_get(obj);
460 if (*wd->statep != wd->state)
462 wd->state = *wd->statep;
464 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
466 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");