1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
8 Evas_Object *chk, *icon;
12 const char *ontext, *offtext;
15 static const char *widtype = NULL;
16 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
17 Evas_Callback_Type type, void *event_info);
18 static void _del_hook(Evas_Object *obj);
19 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
20 static void _theme_hook(Evas_Object *obj);
21 static void _disable_hook(Evas_Object *obj);
22 static void _sizing_eval(Evas_Object *obj);
23 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj,
25 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
26 static void _signal_check_off(void *data, Evas_Object *obj,
27 const char *emission, const char *source);
28 static void _signal_check_on(void *data, Evas_Object *obj,
29 const char *emission, const char *source);
30 static void _signal_check_toggle(void *data, Evas_Object *obj,
31 const char *emission, const char *source);
32 static void _on_focus_hook(void *data, Evas_Object *obj);
33 static void _activate_hook(Evas_Object *obj);
34 static void _content_set_hook(Evas_Object *obj, const char *part,
35 Evas_Object *content);
36 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
37 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
38 static void _activate(Evas_Object *obj);
39 static const char SIG_CHANGED[] = "changed";
40 static const Evas_Smart_Cb_Description _signals[] = {
46 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__,
47 Evas_Callback_Type type, void *event_info)
49 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
50 Evas_Event_Key_Down *ev = event_info;
51 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
52 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
53 if ((strcmp(ev->keyname, "Return")) &&
54 (strcmp(ev->keyname, "KP_Enter")) &&
55 (strcmp(ev->keyname, "space")))
58 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
63 _del_hook(Evas_Object *obj)
65 Widget_Data *wd = elm_widget_data_get(obj);
67 if (wd->label) eina_stringshare_del(wd->label);
68 if (wd->ontext) eina_stringshare_del(wd->ontext);
69 if (wd->offtext) eina_stringshare_del(wd->offtext);
74 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
76 Widget_Data *wd = elm_widget_data_get(obj);
78 if (elm_widget_focus_get(obj))
80 edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
81 evas_object_focus_set(wd->chk, EINA_TRUE);
85 edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
86 evas_object_focus_set(wd->chk, EINA_FALSE);
91 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
93 Widget_Data *wd = elm_widget_data_get(obj);
95 edje_object_mirrored_set(wd->chk, rtl);
99 _theme_hook(Evas_Object *obj)
101 Widget_Data *wd = elm_widget_data_get(obj);
103 _elm_widget_mirrored_reload(obj);
104 _mirrored_set(obj, elm_widget_mirrored_get(obj));
105 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
107 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
109 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
111 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
113 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
115 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
117 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
118 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
119 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
120 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
121 if (elm_widget_disabled_get(obj))
122 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
123 edje_object_message_signal_process(wd->chk);
124 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
129 _disable_hook(Evas_Object *obj)
131 Widget_Data *wd = elm_widget_data_get(obj);
133 if (elm_widget_disabled_get(obj))
134 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
136 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
140 _sizing_eval(Evas_Object *obj)
142 Widget_Data *wd = elm_widget_data_get(obj);
143 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
145 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
146 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
147 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
148 evas_object_size_hint_min_set(obj, minw, minh);
149 evas_object_size_hint_max_set(obj, maxw, maxh);
153 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
155 Widget_Data *wd = elm_widget_data_get(data);
157 if (obj != wd->icon) return;
162 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
164 Widget_Data *wd = elm_widget_data_get(obj);
165 Evas_Object *sub = event_info;
169 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
170 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
171 _changed_size_hints, obj);
174 edje_object_message_signal_process(wd->chk);
179 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
181 Widget_Data *wd = elm_widget_data_get(data);
183 wd->state = EINA_FALSE;
184 if (wd->statep) *wd->statep = wd->state;
185 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
186 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
190 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
192 Widget_Data *wd = elm_widget_data_get(data);
194 wd->state = EINA_TRUE;
195 if (wd->statep) *wd->statep = wd->state;
196 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
197 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
201 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
207 _activate_hook(Evas_Object *obj)
213 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
215 ELM_CHECK_WIDTYPE(obj, widtype);
218 if (part && strcmp(part, "icon")) return;
219 wd = elm_widget_data_get(obj);
221 if (wd->icon == content) return;
222 if (wd->icon) evas_object_del(wd->icon);
226 elm_widget_sub_object_add(obj, content);
227 evas_object_event_callback_add(content,
228 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
229 _changed_size_hints, obj);
230 edje_object_part_swallow(wd->chk, "elm.swallow.content", content);
231 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
232 edje_object_message_signal_process(wd->chk);
238 _content_get_hook(const Evas_Object *obj, const char *part)
240 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
243 if (part && strcmp(part, "icon")) return NULL;
244 wd = elm_widget_data_get(obj);
245 if (!wd) return NULL;
250 _content_unset_hook(Evas_Object *obj, const char *part)
252 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
255 if (part && strcmp(part, "icon")) return NULL;
256 wd = elm_widget_data_get(obj);
257 if (!wd) return NULL;
258 if (!wd->icon) return NULL;
259 Evas_Object *icon = wd->icon;
260 elm_widget_sub_object_del(obj, wd->icon);
261 evas_object_event_callback_del_full(wd->icon,
262 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
263 _changed_size_hints, obj);
264 edje_object_part_unswallow(wd->chk, wd->icon);
270 _activate(Evas_Object *obj)
272 Widget_Data *wd = elm_widget_data_get(obj);
274 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
275 (_elm_access_2nd_click_timeout(obj)))
277 wd->state = !wd->state;
278 if (wd->statep) *wd->statep = wd->state;
281 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
282 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
286 _elm_access_say(E_("State: On"));
289 _elm_access_say(E_("State: On"));
294 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
295 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
299 _elm_access_say(E_("State: Off"));
302 _elm_access_say(E_("State: Off"));
305 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
310 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
312 ELM_CHECK_WIDTYPE(obj, widtype);
313 Widget_Data *wd = elm_widget_data_get(obj);
315 if ((!item) || (!strcmp(item, "default")))
317 eina_stringshare_replace(&wd->label, label);
319 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
321 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
322 edje_object_message_signal_process(wd->chk);
323 edje_object_part_text_set(wd->chk, "elm.text", label);
325 else if ((item) && (!strcmp(item, "on")))
327 eina_stringshare_replace(&wd->ontext, label);
328 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
330 else if ((item) && (!strcmp(item, "off")))
332 eina_stringshare_replace(&wd->offtext, label);
333 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
339 _elm_check_label_get(const Evas_Object *obj, const char *item)
341 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
342 Widget_Data *wd = elm_widget_data_get(obj);
343 if (!wd) return NULL;
344 if ((!item) || (!strcmp(item, "default")))
346 else if ((item) && (!strcmp(item, "on")))
348 else if ((item) && (!strcmp(item, "off")))
354 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
356 const char *txt = elm_widget_access_info_get(obj);
357 if (!txt) txt = _elm_check_label_get(obj, NULL);
358 if (txt) return strdup(txt);
363 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
365 Evas_Object *o = data;
366 Widget_Data *wd = elm_widget_data_get(o);
367 if (!wd) return NULL;
368 if (elm_widget_disabled_get(obj))
369 return strdup(E_("State: Disabled"));
376 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
380 return strdup(E_("State: On"));
386 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
389 return strdup(E_("State: Off"));
393 elm_check_add(Evas_Object *parent)
399 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
401 ELM_SET_WIDTYPE(widtype, "check");
402 elm_widget_type_set(obj, "check");
403 elm_widget_sub_object_add(parent, obj);
404 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
405 elm_widget_data_set(obj, wd);
406 elm_widget_del_hook_set(obj, _del_hook);
407 elm_widget_theme_hook_set(obj, _theme_hook);
408 elm_widget_disable_hook_set(obj, _disable_hook);
409 elm_widget_can_focus_set(obj, EINA_TRUE);
410 elm_widget_activate_hook_set(obj, _activate_hook);
411 elm_widget_event_hook_set(obj, _event_hook);
412 elm_widget_text_set_hook_set(obj, _elm_check_label_set);
413 elm_widget_text_get_hook_set(obj, _elm_check_label_get);
414 elm_widget_content_set_hook_set(obj, _content_set_hook);
415 elm_widget_content_get_hook_set(obj, _content_get_hook);
416 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
418 wd->chk = edje_object_add(e);
419 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
420 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
421 _signal_check_on, obj);
422 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
423 _signal_check_off, obj);
424 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
425 _signal_check_toggle, obj);
426 elm_widget_resize_object_set(obj, wd->chk);
428 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
430 _mirrored_set(obj, elm_widget_mirrored_get(obj));
433 // TODO: convert Elementary to subclassing of Evas_Smart_Class
434 // TODO: and save some bytes, making descriptions per-class and not instance!
435 evas_object_smart_callbacks_descriptions_set(obj, _signals);
437 _elm_access_object_register(obj, wd->chk);
438 _elm_access_text_set(_elm_access_object_get(obj),
439 ELM_ACCESS_TYPE, E_("Check"));
440 _elm_access_callback_set(_elm_access_object_get(obj),
441 ELM_ACCESS_INFO, _access_info_cb, obj);
442 _elm_access_callback_set(_elm_access_object_get(obj),
443 ELM_ACCESS_STATE, _access_state_cb, obj);
448 elm_check_states_labels_set(Evas_Object *obj, const char *ontext, const char *offtext)
450 _elm_check_label_set(obj, "on", ontext);
451 _elm_check_label_set(obj, "off", offtext);
455 elm_check_states_labels_get(const Evas_Object *obj, const char **ontext, const char **offtext)
457 if (ontext) *ontext = _elm_check_label_get(obj, "on");
458 if (offtext) *offtext = _elm_check_label_get(obj, "off");
462 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
464 _content_set_hook(obj, "icon", icon);
468 elm_check_icon_get(const Evas_Object *obj)
470 return _content_get_hook(obj, "icon");
474 elm_check_icon_unset(Evas_Object *obj)
476 return _content_unset_hook(obj, "icon");
480 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
482 ELM_CHECK_WIDTYPE(obj, widtype);
483 Widget_Data *wd = elm_widget_data_get(obj);
485 if (state != wd->state)
488 if (wd->statep) *wd->statep = wd->state;
490 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
492 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
494 edje_object_message_signal_process(wd->chk);
498 elm_check_state_get(const Evas_Object *obj)
500 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
501 Widget_Data *wd = elm_widget_data_get(obj);
502 if (!wd) return EINA_FALSE;
507 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
509 ELM_CHECK_WIDTYPE(obj, widtype);
510 Widget_Data *wd = elm_widget_data_get(obj);
515 if (*wd->statep != wd->state)
517 wd->state = *wd->statep;
519 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
521 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");