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 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 _theme_hook(Evas_Object *obj)
101 Widget_Data *wd = elm_widget_data_get(obj);
103 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
105 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
107 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
109 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
111 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
113 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
115 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
116 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
117 if (elm_widget_disabled_get(obj))
118 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
119 edje_object_message_signal_process(wd->chk);
120 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
125 _disable_hook(Evas_Object *obj)
127 Widget_Data *wd = elm_widget_data_get(obj);
129 if (elm_widget_disabled_get(obj))
130 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
132 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
136 _sizing_eval(Evas_Object *obj)
138 Widget_Data *wd = elm_widget_data_get(obj);
139 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
141 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
142 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
143 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
144 evas_object_size_hint_min_set(obj, minw, minh);
145 evas_object_size_hint_max_set(obj, maxw, maxh);
149 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
151 Widget_Data *wd = elm_widget_data_get(data);
153 if (obj != wd->icon) return;
155 evas_object_size_hint_min_get(obj, &mw, &mh);
160 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
162 Widget_Data *wd = elm_widget_data_get(obj);
163 Evas_Object *sub = event_info;
167 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
168 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
169 _changed_size_hints, obj);
172 edje_object_message_signal_process(wd->chk);
177 _signal_check_off(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 = EINA_FALSE;
182 if (wd->statep) *wd->statep = wd->state;
183 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
184 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
188 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
190 Widget_Data *wd = elm_widget_data_get(data);
192 wd->state = EINA_TRUE;
193 if (wd->statep) *wd->statep = wd->state;
194 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
195 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
199 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
205 _activate_hook(Evas_Object *obj)
211 _activate(Evas_Object *obj)
213 Widget_Data *wd = elm_widget_data_get(obj);
215 wd->state = !wd->state;
216 if (wd->statep) *wd->statep = wd->state;
218 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
220 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
221 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
225 * Add a new Check object
227 * @param parent The parent object
228 * @return The new object or NULL if it cannot be created
233 elm_check_add(Evas_Object *parent)
239 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
241 wd = ELM_NEW(Widget_Data);
242 e = evas_object_evas_get(parent);
244 obj = elm_widget_add(e);
245 ELM_SET_WIDTYPE(widtype, "check");
246 elm_widget_type_set(obj, "check");
247 elm_widget_sub_object_add(parent, obj);
248 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
249 elm_widget_data_set(obj, wd);
250 elm_widget_del_hook_set(obj, _del_hook);
251 elm_widget_theme_hook_set(obj, _theme_hook);
252 elm_widget_disable_hook_set(obj, _disable_hook);
253 elm_widget_can_focus_set(obj, EINA_TRUE);
254 elm_widget_activate_hook_set(obj, _activate_hook);
255 elm_widget_event_hook_set(obj, _event_hook);
257 wd->chk = edje_object_add(e);
258 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
259 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
260 _signal_check_on, obj);
261 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
262 _signal_check_off, obj);
263 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
264 _signal_check_toggle, obj);
265 elm_widget_resize_object_set(obj, wd->chk);
267 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
271 // TODO: convert Elementary to subclassing of Evas_Smart_Class
272 // TODO: and save some bytes, making descriptions per-class and not instance!
273 evas_object_smart_callbacks_descriptions_set(obj, _signals);
278 * Set the text label of the check object
280 * @param obj The check object
281 * @param label The text label string in UTF-8
286 elm_check_label_set(Evas_Object *obj, const char *label)
288 ELM_CHECK_WIDTYPE(obj, widtype);
289 Widget_Data *wd = elm_widget_data_get(obj);
291 eina_stringshare_replace(&wd->label, label);
293 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
295 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
296 edje_object_message_signal_process(wd->chk);
297 edje_object_part_text_set(wd->chk, "elm.text", label);
302 * Get the text label of the check object
304 * @param obj The check object
305 * @return The text label string in UTF-8
310 elm_check_label_get(const Evas_Object *obj)
312 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
313 Widget_Data *wd = elm_widget_data_get(obj);
314 if (!wd) return NULL;
319 * Set the icon object of the check object
321 * Once the icon object is set, a previously set one will be deleted.
322 * If you want to keep that old content object, use the
323 * elm_check_icon_unset() function.
325 * @param obj The check object
326 * @param icon The icon object
331 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
333 ELM_CHECK_WIDTYPE(obj, widtype);
334 Widget_Data *wd = elm_widget_data_get(obj);
336 if (wd->icon == icon) return;
337 if (wd->icon) evas_object_del(wd->icon);
341 elm_widget_sub_object_add(obj, icon);
342 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
343 _changed_size_hints, obj);
344 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
345 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
346 edje_object_message_signal_process(wd->chk);
352 * Get the icon object of the check object
354 * @param obj The check object
355 * @return The icon object
360 elm_check_icon_get(const Evas_Object *obj)
362 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
363 Widget_Data *wd = elm_widget_data_get(obj);
364 if (!wd) return NULL;
369 * Unset the icon used for the check object
371 * Unparent and return the icon object which was set for this widget.
373 * @param obj The check object
374 * @return The icon object that was being used
379 elm_check_icon_unset(Evas_Object *obj)
381 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
382 Widget_Data *wd = elm_widget_data_get(obj);
383 if (!wd) return NULL;
384 if (!wd->icon) return NULL;
385 Evas_Object *icon = wd->icon;
386 elm_widget_sub_object_del(obj, wd->icon);
387 edje_object_part_unswallow(wd->chk, wd->icon);
393 * Set the on/off state of the check object
395 * This sets the state of the check and will also set the value if pointed to
396 * to the state supplied, but will not call any callbacks.
398 * @param obj The check object
399 * @param state The state to use (1 == on, 0 == off)
404 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
406 ELM_CHECK_WIDTYPE(obj, widtype);
407 Widget_Data *wd = elm_widget_data_get(obj);
409 if (state != wd->state)
412 if (wd->statep) *wd->statep = wd->state;
414 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
416 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
421 * Get the state of the check object
423 * @param obj The check object
424 * @return The boolean state
429 elm_check_state_get(const Evas_Object *obj)
431 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
432 Widget_Data *wd = elm_widget_data_get(obj);
433 if (!wd) return EINA_FALSE;
438 * Set a convenience pointer to a boolean to change
440 * This sets a pointer to a boolean, that, in addition to the check objects
441 * state will also be modified directly. To stop setting the object pointed
442 * to simply use NULL as the statep parameter. If statep is not NULL, then
443 * when this is called, the check objects state will also be modified to
444 * reflect the value of the boolean statep points to, just like calling
445 * elm_check_state_set().
447 * @param obj The check object
448 * @param statep Pointer to the boolean to modify
453 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
455 ELM_CHECK_WIDTYPE(obj, widtype);
456 Widget_Data *wd = elm_widget_data_get(obj);
461 if (*wd->statep != wd->state)
463 wd->state = *wd->statep;
465 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
467 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");