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 * Check objects are a lot like radio objects in layout and functionality
11 * except they do not work as a group, but independently and only toggle the
12 * value of a boolean from false to true (0 or 1). elm_check_state_set() sets
13 * the boolean state (1 for true, 0 for false), and elm_check_state_get()
14 * returns the current state. For convenience, like the radio objects, you
15 * can set a pointer to a boolean directly with elm_check_state_pointer_set()
18 * Signals that you can add callbacks for are:
20 * "changed" - This is called whenever the user changes the state of one of the
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 _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
36 static void _theme_hook(Evas_Object *obj);
37 static void _disable_hook(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);
44 static void _on_focus_hook(void *data, Evas_Object *obj);
45 static void _activate_hook(Evas_Object *obj);
46 static void _activate(Evas_Object *obj);
47 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
48 Evas_Callback_Type type, void *event_info);
50 static const char SIG_CHANGED[] = "changed";
51 static const Evas_Smart_Cb_Description _signals[] = {
57 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
59 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
60 Evas_Event_Key_Down *ev = event_info;
61 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
62 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
63 if ((strcmp(ev->keyname, "Return")) &&
64 (strcmp(ev->keyname, "KP_Enter")) &&
65 (strcmp(ev->keyname, "space")))
68 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
74 _del_hook(Evas_Object *obj)
76 Widget_Data *wd = elm_widget_data_get(obj);
78 if (wd->label) eina_stringshare_del(wd->label);
83 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
85 Widget_Data *wd = elm_widget_data_get(obj);
87 if (elm_widget_focus_get(obj))
89 edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
90 evas_object_focus_set(wd->chk, EINA_TRUE);
94 edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
95 evas_object_focus_set(wd->chk, EINA_FALSE);
100 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
102 Widget_Data *wd = elm_widget_data_get(obj);
104 edje_object_mirrored_set(wd->chk, rtl);
108 _theme_hook(Evas_Object *obj)
110 unsigned int counter = 0;
112 unsigned int length = 0;
115 char buffer[PATH_MAX]={'\0',};
116 char s1[PATH_MAX] = {'\0',};
117 Widget_Data *wd = elm_widget_data_get(obj);
120 _elm_widget_mirrored_reload(obj);
121 _mirrored_set(obj, elm_widget_mirrored_get(obj));
122 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
124 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
126 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
128 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
130 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
132 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
134 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
135 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
136 if (elm_widget_disabled_get(obj))
137 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
138 edje_object_message_signal_process(wd->chk);
139 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
141 //introduced internationalization of additional text parts used in style
144 // s1 is used to store part name while buffer is used to store the part's value string
145 snprintf(labels,sizeof(labels),"label_%d",i++);
146 str = edje_object_data_get(wd->chk,labels);
148 length = strlen(str);
149 while ((str[counter]!= ' ') && (counter < length))
151 if (counter == length)
153 strncpy(s1, str, counter);
155 strncpy(buffer, str + counter, sizeof(buffer));
156 edje_object_part_text_set(wd->chk, s1, E_(buffer));
162 _disable_hook(Evas_Object *obj)
164 Widget_Data *wd = elm_widget_data_get(obj);
166 if (elm_widget_disabled_get(obj))
167 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
169 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
173 _sizing_eval(Evas_Object *obj)
175 Widget_Data *wd = elm_widget_data_get(obj);
176 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
178 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
179 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
180 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
181 evas_object_size_hint_min_set(obj, minw, minh);
182 evas_object_size_hint_max_set(obj, maxw, maxh);
186 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
188 Widget_Data *wd = elm_widget_data_get(data);
190 if (obj != wd->icon) return;
192 evas_object_size_hint_min_get(obj, &mw, &mh);
197 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
199 Widget_Data *wd = elm_widget_data_get(obj);
200 Evas_Object *sub = event_info;
204 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
205 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
206 _changed_size_hints, obj);
209 edje_object_message_signal_process(wd->chk);
214 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
216 Widget_Data *wd = elm_widget_data_get(data);
218 wd->state = EINA_FALSE;
219 if (wd->statep) *wd->statep = wd->state;
220 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
221 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
225 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
227 Widget_Data *wd = elm_widget_data_get(data);
229 wd->state = EINA_TRUE;
230 if (wd->statep) *wd->statep = wd->state;
231 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
232 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
236 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
242 _activate_hook(Evas_Object *obj)
248 _activate(Evas_Object *obj)
250 Widget_Data *wd = elm_widget_data_get(obj);
252 wd->state = !wd->state;
253 if (wd->statep) *wd->statep = wd->state;
255 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
257 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
258 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
262 * Add a new Check object
264 * @param parent The parent object
265 * @return The new object or NULL if it cannot be created
270 elm_check_add(Evas_Object *parent)
276 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
278 ELM_SET_WIDTYPE(widtype, "check");
279 elm_widget_type_set(obj, "check");
280 elm_widget_sub_object_add(parent, obj);
281 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
282 elm_widget_data_set(obj, wd);
283 elm_widget_del_hook_set(obj, _del_hook);
284 elm_widget_theme_hook_set(obj, _theme_hook);
285 elm_widget_disable_hook_set(obj, _disable_hook);
286 elm_widget_can_focus_set(obj, EINA_TRUE);
287 elm_widget_activate_hook_set(obj, _activate_hook);
288 elm_widget_event_hook_set(obj, _event_hook);
290 wd->chk = edje_object_add(e);
291 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
292 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
293 _signal_check_on, obj);
294 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
295 _signal_check_off, obj);
296 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
297 _signal_check_toggle, obj);
298 elm_widget_resize_object_set(obj, wd->chk);
300 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
302 _mirrored_set(obj, elm_widget_mirrored_get(obj));
305 // TODO: convert Elementary to subclassing of Evas_Smart_Class
306 // TODO: and save some bytes, making descriptions per-class and not instance!
307 evas_object_smart_callbacks_descriptions_set(obj, _signals);
312 * Set the text label of the check object
314 * @param obj The check object
315 * @param label The text label string in UTF-8
320 elm_check_label_set(Evas_Object *obj, const char *label)
322 ELM_CHECK_WIDTYPE(obj, widtype);
323 Widget_Data *wd = elm_widget_data_get(obj);
325 eina_stringshare_replace(&wd->label, label);
327 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
329 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
330 edje_object_message_signal_process(wd->chk);
331 edje_object_part_text_set(wd->chk, "elm.text", label);
336 * Get the text label of the check object
338 * @param obj The check object
339 * @return The text label string in UTF-8
344 elm_check_label_get(const Evas_Object *obj)
346 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
347 Widget_Data *wd = elm_widget_data_get(obj);
348 if (!wd) return NULL;
353 * Set the icon object of the check object
355 * Once the icon object is set, a previously set one will be deleted.
356 * If you want to keep that old content object, use the
357 * elm_check_icon_unset() function.
359 * @param obj The check object
360 * @param icon The icon object
365 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
367 ELM_CHECK_WIDTYPE(obj, widtype);
368 Widget_Data *wd = elm_widget_data_get(obj);
370 if (wd->icon == icon) return;
371 if (wd->icon) evas_object_del(wd->icon);
375 elm_widget_sub_object_add(obj, icon);
376 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
377 _changed_size_hints, obj);
378 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
379 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
380 edje_object_message_signal_process(wd->chk);
386 * Get the icon object of the check object
388 * @param obj The check object
389 * @return The icon object
394 elm_check_icon_get(const Evas_Object *obj)
396 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
397 Widget_Data *wd = elm_widget_data_get(obj);
398 if (!wd) return NULL;
403 * Unset the icon used for the check object
405 * Unparent and return the icon object which was set for this widget.
407 * @param obj The check object
408 * @return The icon object that was being used
413 elm_check_icon_unset(Evas_Object *obj)
415 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
416 Widget_Data *wd = elm_widget_data_get(obj);
417 if (!wd) return NULL;
418 if (!wd->icon) return NULL;
419 Evas_Object *icon = wd->icon;
420 elm_widget_sub_object_del(obj, wd->icon);
421 edje_object_part_unswallow(wd->chk, wd->icon);
427 * Set the on/off state of the check object
429 * This sets the state of the check and will also set the value if pointed to
430 * to the state supplied, but will not call any callbacks.
432 * @param obj The check object
433 * @param state The state to use (1 == on, 0 == off)
438 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
440 ELM_CHECK_WIDTYPE(obj, widtype);
441 Widget_Data *wd = elm_widget_data_get(obj);
443 if (state != wd->state)
446 if (wd->statep) *wd->statep = wd->state;
448 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
450 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
455 * Get the state of the check object
457 * @param obj The check object
458 * @return The boolean state
463 elm_check_state_get(const Evas_Object *obj)
465 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
466 Widget_Data *wd = elm_widget_data_get(obj);
467 if (!wd) return EINA_FALSE;
472 * Set a convenience pointer to a boolean to change
474 * This sets a pointer to a boolean, that, in addition to the check objects
475 * state will also be modified directly. To stop setting the object pointed
476 * to simply use NULL as the statep parameter. If statep is not NULL, then
477 * when this is called, the check objects state will also be modified to
478 * reflect the value of the boolean statep points to, just like calling
479 * elm_check_state_set().
481 * @param obj The check object
482 * @param statep Pointer to the boolean to modify
487 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
489 ELM_CHECK_WIDTYPE(obj, widtype);
490 Widget_Data *wd = elm_widget_data_get(obj);
495 if (*wd->statep != wd->state)
497 wd->state = *wd->statep;
499 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
501 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");