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 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
215 Widget_Data *wd = elm_widget_data_get(obj);
218 edje_object_signal_emit(wd->chk, emission, source);
222 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
224 Widget_Data *wd = elm_widget_data_get(obj);
226 edje_object_signal_callback_add(wd->chk, emission, source, func_cb, data);
230 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
232 Widget_Data *wd = elm_widget_data_get(obj);
233 edje_object_signal_callback_del_full(wd->chk, emission, source, func_cb,
238 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
240 ELM_CHECK_WIDTYPE(obj, widtype);
243 if (part && strcmp(part, "icon")) return;
244 wd = elm_widget_data_get(obj);
246 if (wd->icon == content) return;
247 if (wd->icon) evas_object_del(wd->icon);
251 elm_widget_sub_object_add(obj, content);
252 evas_object_event_callback_add(content,
253 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
254 _changed_size_hints, obj);
255 edje_object_part_swallow(wd->chk, "elm.swallow.content", content);
256 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
257 edje_object_message_signal_process(wd->chk);
263 _content_get_hook(const Evas_Object *obj, const char *part)
265 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
268 if (part && strcmp(part, "icon")) return NULL;
269 wd = elm_widget_data_get(obj);
270 if (!wd) return NULL;
275 _content_unset_hook(Evas_Object *obj, const char *part)
277 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
280 if (part && strcmp(part, "icon")) return NULL;
281 wd = elm_widget_data_get(obj);
282 if (!wd) return NULL;
283 if (!wd->icon) return NULL;
284 Evas_Object *icon = wd->icon;
285 elm_widget_sub_object_del(obj, wd->icon);
286 evas_object_event_callback_del_full(wd->icon,
287 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
288 _changed_size_hints, obj);
289 edje_object_part_unswallow(wd->chk, wd->icon);
295 _activate(Evas_Object *obj)
297 Widget_Data *wd = elm_widget_data_get(obj);
299 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
300 (_elm_access_2nd_click_timeout(obj)))
302 wd->state = !wd->state;
303 if (wd->statep) *wd->statep = wd->state;
306 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
307 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
311 _elm_access_say(E_("State: On"));
314 _elm_access_say(E_("State: On"));
319 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
320 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
324 _elm_access_say(E_("State: Off"));
327 _elm_access_say(E_("State: Off"));
330 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
335 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
337 ELM_CHECK_WIDTYPE(obj, widtype);
338 Widget_Data *wd = elm_widget_data_get(obj);
340 if ((!item) || (!strcmp(item, "default")))
342 eina_stringshare_replace(&wd->label, label);
344 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
346 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
347 edje_object_message_signal_process(wd->chk);
348 edje_object_part_text_set(wd->chk, "elm.text", label);
350 else if ((item) && (!strcmp(item, "on")))
352 eina_stringshare_replace(&wd->ontext, label);
353 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
355 else if ((item) && (!strcmp(item, "off")))
357 eina_stringshare_replace(&wd->offtext, label);
358 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
364 _elm_check_label_get(const Evas_Object *obj, const char *item)
366 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
367 Widget_Data *wd = elm_widget_data_get(obj);
368 if (!wd) return NULL;
369 if ((!item) || (!strcmp(item, "default")))
371 else if ((item) && (!strcmp(item, "on")))
373 else if ((item) && (!strcmp(item, "off")))
379 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
381 const char *txt = elm_widget_access_info_get(obj);
382 if (!txt) txt = _elm_check_label_get(obj, NULL);
383 if (txt) return strdup(txt);
388 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
390 Evas_Object *o = data;
391 Widget_Data *wd = elm_widget_data_get(o);
392 if (!wd) return NULL;
393 if (elm_widget_disabled_get(obj))
394 return strdup(E_("State: Disabled"));
401 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
405 return strdup(E_("State: On"));
411 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
414 return strdup(E_("State: Off"));
418 elm_check_add(Evas_Object *parent)
424 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
426 ELM_SET_WIDTYPE(widtype, "check");
427 elm_widget_type_set(obj, "check");
428 elm_widget_sub_object_add(parent, obj);
429 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
430 elm_widget_data_set(obj, wd);
431 elm_widget_del_hook_set(obj, _del_hook);
432 elm_widget_theme_hook_set(obj, _theme_hook);
433 elm_widget_disable_hook_set(obj, _disable_hook);
434 elm_widget_can_focus_set(obj, EINA_TRUE);
435 elm_widget_activate_hook_set(obj, _activate_hook);
436 elm_widget_event_hook_set(obj, _event_hook);
437 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
438 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
439 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
440 elm_widget_text_set_hook_set(obj, _elm_check_label_set);
441 elm_widget_text_get_hook_set(obj, _elm_check_label_get);
442 elm_widget_content_set_hook_set(obj, _content_set_hook);
443 elm_widget_content_get_hook_set(obj, _content_get_hook);
444 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
446 wd->chk = edje_object_add(e);
447 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
448 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
449 _signal_check_on, obj);
450 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
451 _signal_check_off, obj);
452 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
453 _signal_check_toggle, obj);
454 elm_widget_resize_object_set(obj, wd->chk);
456 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
458 _mirrored_set(obj, elm_widget_mirrored_get(obj));
461 // TODO: convert Elementary to subclassing of Evas_Smart_Class
462 // TODO: and save some bytes, making descriptions per-class and not instance!
463 evas_object_smart_callbacks_descriptions_set(obj, _signals);
465 _elm_access_object_register(obj, wd->chk);
466 _elm_access_text_set(_elm_access_object_get(obj),
467 ELM_ACCESS_TYPE, E_("Check"));
468 _elm_access_callback_set(_elm_access_object_get(obj),
469 ELM_ACCESS_INFO, _access_info_cb, obj);
470 _elm_access_callback_set(_elm_access_object_get(obj),
471 ELM_ACCESS_STATE, _access_state_cb, obj);
476 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
478 ELM_CHECK_WIDTYPE(obj, widtype);
479 Widget_Data *wd = elm_widget_data_get(obj);
481 if (state != wd->state)
484 if (wd->statep) *wd->statep = wd->state;
486 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
488 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
490 edje_object_message_signal_process(wd->chk);
494 elm_check_state_get(const Evas_Object *obj)
496 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
497 Widget_Data *wd = elm_widget_data_get(obj);
498 if (!wd) return EINA_FALSE;
503 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
505 ELM_CHECK_WIDTYPE(obj, widtype);
506 Widget_Data *wd = elm_widget_data_get(obj);
511 if (*wd->statep != wd->state)
513 wd->state = *wd->statep;
515 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
517 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");