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 void _del_hook(Evas_Object *obj);
17 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
18 static void _theme_hook(Evas_Object *obj);
19 static void _disable_hook(Evas_Object *obj);
20 static void _sizing_eval(Evas_Object *obj);
21 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
22 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
23 static void _signal_check_off(void *data, Evas_Object *obj, const char *emission, const char *source);
24 static void _signal_check_on(void *data, Evas_Object *obj, const char *emission, const char *source);
25 static void _signal_check_toggle(void *data, Evas_Object *obj, const char *emission, const char *source);
26 static void _on_focus_hook(void *data, Evas_Object *obj);
27 static void _activate_hook(Evas_Object *obj);
28 static void _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
29 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
30 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
32 static void _activate(Evas_Object *obj);
33 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
34 Evas_Callback_Type type, void *event_info);
36 static const char SIG_CHANGED[] = "changed";
37 static const Evas_Smart_Cb_Description _signals[] = {
43 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
45 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
46 Evas_Event_Key_Down *ev = event_info;
47 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
48 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
49 if ((strcmp(ev->keyname, "Return")) &&
50 (strcmp(ev->keyname, "KP_Enter")) &&
51 (strcmp(ev->keyname, "space")))
54 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
60 _del_hook(Evas_Object *obj)
62 Widget_Data *wd = elm_widget_data_get(obj);
64 if (wd->label) eina_stringshare_del(wd->label);
65 if (wd->ontext) eina_stringshare_del(wd->ontext);
66 if (wd->offtext) eina_stringshare_del(wd->offtext);
71 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
73 Widget_Data *wd = elm_widget_data_get(obj);
75 if (elm_widget_focus_get(obj))
77 edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
78 evas_object_focus_set(wd->chk, EINA_TRUE);
82 edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
83 evas_object_focus_set(wd->chk, EINA_FALSE);
88 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
90 Widget_Data *wd = elm_widget_data_get(obj);
92 edje_object_mirrored_set(wd->chk, rtl);
96 _theme_hook(Evas_Object *obj)
98 unsigned int counter = 0;
100 unsigned int length = 0;
101 const char *str = NULL;
103 char buffer[PATH_MAX]={'\0',};
104 char s1[PATH_MAX] = {'\0',};
105 Widget_Data *wd = elm_widget_data_get(obj);
108 _elm_widget_mirrored_reload(obj);
109 _mirrored_set(obj, elm_widget_mirrored_get(obj));
110 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
112 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
114 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
116 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
118 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
120 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
122 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
123 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
124 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
125 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
126 if (elm_widget_disabled_get(obj))
127 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
128 edje_object_message_signal_process(wd->chk);
129 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
131 //introduced internationalization of additional text parts used in style
134 // s1 is used to store part name while buffer is used to store the part's value string
135 snprintf(labels,sizeof(labels),"label_%d",i++);
136 str = edje_object_data_get(wd->chk,labels);
138 length = strlen(str);
139 while ((str[counter]!= ' ') && (counter < length))
141 if (counter == length)
143 strncpy(s1, str, counter);
145 strncpy(buffer, str + counter, sizeof(buffer));
146 edje_object_part_text_set(wd->chk, s1, E_(buffer));
152 _disable_hook(Evas_Object *obj)
154 Widget_Data *wd = elm_widget_data_get(obj);
156 if (elm_widget_disabled_get(obj))
157 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
159 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
163 _sizing_eval(Evas_Object *obj)
165 Widget_Data *wd = elm_widget_data_get(obj);
166 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
168 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
169 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
170 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
171 evas_object_size_hint_min_set(obj, minw, minh);
172 evas_object_size_hint_max_set(obj, maxw, maxh);
176 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
178 Widget_Data *wd = elm_widget_data_get(data);
180 if (obj != wd->icon) return;
185 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
187 Widget_Data *wd = elm_widget_data_get(obj);
188 Evas_Object *sub = event_info;
192 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
193 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
194 _changed_size_hints, obj);
197 edje_object_message_signal_process(wd->chk);
202 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
204 Widget_Data *wd = elm_widget_data_get(data);
206 wd->state = EINA_FALSE;
207 if (wd->statep) *wd->statep = wd->state;
208 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
209 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
213 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
215 Widget_Data *wd = elm_widget_data_get(data);
217 wd->state = EINA_TRUE;
218 if (wd->statep) *wd->statep = wd->state;
219 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
220 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
224 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
230 _activate_hook(Evas_Object *obj)
236 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
238 ELM_CHECK_WIDTYPE(obj, widtype);
241 if (part && strcmp(part, "icon")) return;
242 wd = elm_widget_data_get(obj);
244 if (wd->icon == content) return;
245 if (wd->icon) evas_object_del(wd->icon);
249 elm_widget_sub_object_add(obj, content);
250 evas_object_event_callback_add(content,
251 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
252 _changed_size_hints, obj);
253 edje_object_part_swallow(wd->chk, "elm.swallow.content", content);
254 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
255 edje_object_message_signal_process(wd->chk);
261 _content_get_hook(const Evas_Object *obj, const char *part)
263 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
266 if (part && strcmp(part, "icon")) return NULL;
267 wd = elm_widget_data_get(obj);
268 if (!wd) return NULL;
273 _content_unset_hook(Evas_Object *obj, const char *part)
275 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
278 if (part && strcmp(part, "icon")) return NULL;
279 wd = elm_widget_data_get(obj);
280 if (!wd) return NULL;
281 if (!wd->icon) return NULL;
282 Evas_Object *icon = wd->icon;
283 elm_widget_sub_object_del(obj, wd->icon);
284 evas_object_event_callback_del_full(wd->icon,
285 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
286 _changed_size_hints, obj);
287 edje_object_part_unswallow(wd->chk, wd->icon);
293 _activate(Evas_Object *obj)
295 Widget_Data *wd = elm_widget_data_get(obj);
297 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
298 (_elm_access_2nd_click_timeout(obj)))
300 wd->state = !wd->state;
301 if (wd->statep) *wd->statep = wd->state;
304 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
305 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
309 _elm_access_say(E_("State: On"));
312 _elm_access_say(E_("State: On"));
317 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
318 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
322 _elm_access_say(E_("State: Off"));
325 _elm_access_say(E_("State: Off"));
328 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
333 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
335 ELM_CHECK_WIDTYPE(obj, widtype);
336 Widget_Data *wd = elm_widget_data_get(obj);
338 if ((!item) || (!strcmp(item, "default")))
340 eina_stringshare_replace(&wd->label, label);
342 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
344 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
345 edje_object_message_signal_process(wd->chk);
346 edje_object_part_text_set(wd->chk, "elm.text", label);
348 else if ((item) && (!strcmp(item, "on")))
350 eina_stringshare_replace(&wd->ontext, label);
351 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
353 else if ((item) && (!strcmp(item, "off")))
355 eina_stringshare_replace(&wd->offtext, label);
356 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
362 _elm_check_label_get(const Evas_Object *obj, const char *item)
364 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
365 Widget_Data *wd = elm_widget_data_get(obj);
366 if (!wd) return NULL;
367 if ((!item) || (!strcmp(item, "default")))
369 else if ((item) && (!strcmp(item, "on")))
371 else if ((item) && (!strcmp(item, "off")))
377 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
379 const char *txt = elm_widget_access_info_get(obj);
380 if (!txt) txt = _elm_check_label_get(obj, NULL);
381 if (txt) return strdup(txt);
386 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
388 Evas_Object *o = data;
389 Widget_Data *wd = elm_widget_data_get(o);
390 if (!wd) return NULL;
391 if (elm_widget_disabled_get(obj))
392 return strdup(E_("State: Disabled"));
399 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
403 return strdup(E_("State: On"));
409 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
412 return strdup(E_("State: Off"));
416 elm_check_add(Evas_Object *parent)
422 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
424 ELM_SET_WIDTYPE(widtype, "check");
425 elm_widget_type_set(obj, "check");
426 elm_widget_sub_object_add(parent, obj);
427 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
428 elm_widget_data_set(obj, wd);
429 elm_widget_del_hook_set(obj, _del_hook);
430 elm_widget_theme_hook_set(obj, _theme_hook);
431 elm_widget_disable_hook_set(obj, _disable_hook);
432 elm_widget_can_focus_set(obj, EINA_TRUE);
433 elm_widget_activate_hook_set(obj, _activate_hook);
434 elm_widget_event_hook_set(obj, _event_hook);
435 elm_widget_text_set_hook_set(obj, _elm_check_label_set);
436 elm_widget_text_get_hook_set(obj, _elm_check_label_get);
437 elm_widget_content_set_hook_set(obj, _content_set_hook);
438 elm_widget_content_get_hook_set(obj, _content_get_hook);
439 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
441 wd->chk = edje_object_add(e);
442 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
443 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
444 _signal_check_on, obj);
445 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
446 _signal_check_off, obj);
447 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
448 _signal_check_toggle, obj);
449 elm_widget_resize_object_set(obj, wd->chk);
451 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
453 _mirrored_set(obj, elm_widget_mirrored_get(obj));
456 // TODO: convert Elementary to subclassing of Evas_Smart_Class
457 // TODO: and save some bytes, making descriptions per-class and not instance!
458 evas_object_smart_callbacks_descriptions_set(obj, _signals);
460 _elm_access_object_register(obj, wd->chk);
461 _elm_access_text_set(_elm_access_object_get(obj),
462 ELM_ACCESS_TYPE, E_("Check"));
463 _elm_access_callback_set(_elm_access_object_get(obj),
464 ELM_ACCESS_INFO, _access_info_cb, obj);
465 _elm_access_callback_set(_elm_access_object_get(obj),
466 ELM_ACCESS_STATE, _access_state_cb, obj);
471 elm_check_label_set(Evas_Object *obj, const char *label)
473 _elm_check_label_set(obj, NULL, label);
477 elm_check_label_get(const Evas_Object *obj)
479 return _elm_check_label_get(obj, NULL);
483 elm_check_states_labels_set(Evas_Object *obj, const char *ontext, const char *offtext)
485 _elm_check_label_set(obj, "on", ontext);
486 _elm_check_label_set(obj, "off", offtext);
490 elm_check_states_labels_get(const Evas_Object *obj, const char **ontext, const char **offtext)
492 if (ontext) *ontext = _elm_check_label_get(obj, "on");
493 if (offtext) *offtext = _elm_check_label_get(obj, "off");
497 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
499 _content_set_hook(obj, "icon", icon);
503 elm_check_icon_get(const Evas_Object *obj)
505 return _content_get_hook(obj, "icon");
509 elm_check_icon_unset(Evas_Object *obj)
511 return _content_unset_hook(obj, "icon");
515 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
517 ELM_CHECK_WIDTYPE(obj, widtype);
518 Widget_Data *wd = elm_widget_data_get(obj);
520 if (state != wd->state)
523 if (wd->statep) *wd->statep = wd->state;
525 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
527 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
529 edje_object_message_signal_process(wd->chk);
533 elm_check_state_get(const Evas_Object *obj)
535 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
536 Widget_Data *wd = elm_widget_data_get(obj);
537 if (!wd) return EINA_FALSE;
542 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
544 ELM_CHECK_WIDTYPE(obj, widtype);
545 Widget_Data *wd = elm_widget_data_get(obj);
550 if (*wd->statep != wd->state)
552 wd->state = *wd->statep;
554 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
556 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");