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 edje_object_part_unswallow(wd->chk, icon);
291 _activate(Evas_Object *obj)
293 Widget_Data *wd = elm_widget_data_get(obj);
295 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
296 (_elm_access_2nd_click_timeout(obj)))
298 wd->state = !wd->state;
299 if (wd->statep) *wd->statep = wd->state;
302 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
303 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
307 _elm_access_say(E_("State: On"));
310 _elm_access_say(E_("State: On"));
315 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
316 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
320 _elm_access_say(E_("State: Off"));
323 _elm_access_say(E_("State: Off"));
326 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
331 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
333 ELM_CHECK_WIDTYPE(obj, widtype);
334 Widget_Data *wd = elm_widget_data_get(obj);
336 if ((!item) || (!strcmp(item, "default")))
338 eina_stringshare_replace(&wd->label, label);
340 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
342 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
343 edje_object_message_signal_process(wd->chk);
344 edje_object_part_text_set(wd->chk, "elm.text", label);
346 else if ((item) && (!strcmp(item, "on")))
348 eina_stringshare_replace(&wd->ontext, label);
349 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
351 else if ((item) && (!strcmp(item, "off")))
353 eina_stringshare_replace(&wd->offtext, label);
354 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
360 _elm_check_label_get(const Evas_Object *obj, const char *item)
362 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
363 Widget_Data *wd = elm_widget_data_get(obj);
364 if (!wd) return NULL;
365 if ((!item) || (!strcmp(item, "default")))
367 else if ((item) && (!strcmp(item, "on")))
369 else if ((item) && (!strcmp(item, "off")))
375 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
377 const char *txt = elm_widget_access_info_get(obj);
378 if (!txt) txt = _elm_check_label_get(obj, NULL);
379 if (txt) return strdup(txt);
384 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
386 Evas_Object *o = data;
387 Widget_Data *wd = elm_widget_data_get(o);
388 if (!wd) return NULL;
389 if (elm_widget_disabled_get(obj))
390 return strdup(E_("State: Disabled"));
397 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
401 return strdup(E_("State: On"));
407 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
410 return strdup(E_("State: Off"));
414 elm_check_add(Evas_Object *parent)
420 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
422 ELM_SET_WIDTYPE(widtype, "check");
423 elm_widget_type_set(obj, "check");
424 elm_widget_sub_object_add(parent, obj);
425 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
426 elm_widget_data_set(obj, wd);
427 elm_widget_del_hook_set(obj, _del_hook);
428 elm_widget_theme_hook_set(obj, _theme_hook);
429 elm_widget_disable_hook_set(obj, _disable_hook);
430 elm_widget_can_focus_set(obj, EINA_TRUE);
431 elm_widget_activate_hook_set(obj, _activate_hook);
432 elm_widget_event_hook_set(obj, _event_hook);
433 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
434 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
435 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
436 elm_widget_text_set_hook_set(obj, _elm_check_label_set);
437 elm_widget_text_get_hook_set(obj, _elm_check_label_get);
438 elm_widget_content_set_hook_set(obj, _content_set_hook);
439 elm_widget_content_get_hook_set(obj, _content_get_hook);
440 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
442 wd->chk = edje_object_add(e);
443 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
444 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
445 _signal_check_on, obj);
446 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
447 _signal_check_off, obj);
448 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
449 _signal_check_toggle, obj);
450 elm_widget_resize_object_set(obj, wd->chk);
452 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
454 _mirrored_set(obj, elm_widget_mirrored_get(obj));
457 // TODO: convert Elementary to subclassing of Evas_Smart_Class
458 // TODO: and save some bytes, making descriptions per-class and not instance!
459 evas_object_smart_callbacks_descriptions_set(obj, _signals);
461 _elm_access_object_register(obj, wd->chk);
462 _elm_access_text_set(_elm_access_object_get(obj),
463 ELM_ACCESS_TYPE, E_("Check"));
464 _elm_access_callback_set(_elm_access_object_get(obj),
465 ELM_ACCESS_INFO, _access_info_cb, obj);
466 _elm_access_callback_set(_elm_access_object_get(obj),
467 ELM_ACCESS_STATE, _access_state_cb, obj);
472 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
474 ELM_CHECK_WIDTYPE(obj, widtype);
475 Widget_Data *wd = elm_widget_data_get(obj);
477 if (state != wd->state)
480 if (wd->statep) *wd->statep = wd->state;
482 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
484 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
486 edje_object_message_signal_process(wd->chk);
490 elm_check_state_get(const Evas_Object *obj)
492 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
493 Widget_Data *wd = elm_widget_data_get(obj);
494 if (!wd) return EINA_FALSE;
499 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
501 ELM_CHECK_WIDTYPE(obj, widtype);
502 Widget_Data *wd = elm_widget_data_get(obj);
507 if (*wd->statep != wd->state)
509 wd->state = *wd->statep;
511 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
513 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");