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 unsigned int counter = 0;
103 unsigned int length = 0;
104 const char *str = NULL;
106 char buffer[PATH_MAX]={'\0',};
107 char s1[PATH_MAX] = {'\0',};
108 Widget_Data *wd = elm_widget_data_get(obj);
110 _elm_widget_mirrored_reload(obj);
111 _mirrored_set(obj, elm_widget_mirrored_get(obj));
112 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
114 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
116 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
118 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
120 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
122 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
124 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
125 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
126 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
127 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
128 if (elm_widget_disabled_get(obj))
129 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
130 edje_object_message_signal_process(wd->chk);
131 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
132 //introduced internationalization of additional text parts used in style
135 // s1 is used to store part name while buffer is used to store the part's value string
136 snprintf(labels,sizeof(labels),"label_%d",i++);
137 str = edje_object_data_get(wd->chk,labels);
139 length = strlen(str);
140 while ((str[counter]!= ' ') && (counter < length))
142 if (counter == length)
144 strncpy(s1, str, counter);
146 strncpy(buffer, str + counter, sizeof(buffer));
147 edje_object_part_text_set(wd->chk, s1, E_(buffer));
153 _disable_hook(Evas_Object *obj)
155 Widget_Data *wd = elm_widget_data_get(obj);
157 if (elm_widget_disabled_get(obj))
158 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
160 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
164 _sizing_eval(Evas_Object *obj)
166 Widget_Data *wd = elm_widget_data_get(obj);
167 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
169 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
170 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
171 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
172 evas_object_size_hint_min_set(obj, minw, minh);
173 evas_object_size_hint_max_set(obj, maxw, maxh);
177 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
179 Widget_Data *wd = elm_widget_data_get(data);
181 if (obj != wd->icon) return;
186 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
188 Widget_Data *wd = elm_widget_data_get(obj);
189 Evas_Object *sub = event_info;
193 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
194 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
195 _changed_size_hints, obj);
198 edje_object_message_signal_process(wd->chk);
203 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
205 Widget_Data *wd = elm_widget_data_get(data);
207 wd->state = EINA_FALSE;
208 if (wd->statep) *wd->statep = wd->state;
209 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
210 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
214 _signal_check_on(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_TRUE;
219 if (wd->statep) *wd->statep = wd->state;
220 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
221 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
225 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
231 _activate_hook(Evas_Object *obj)
237 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
239 Widget_Data *wd = elm_widget_data_get(obj);
242 edje_object_signal_emit(wd->chk, emission, source);
246 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
248 Widget_Data *wd = elm_widget_data_get(obj);
250 edje_object_signal_callback_add(wd->chk, emission, source, func_cb, data);
254 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
256 Widget_Data *wd = elm_widget_data_get(obj);
257 edje_object_signal_callback_del_full(wd->chk, emission, source, func_cb,
262 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
264 ELM_CHECK_WIDTYPE(obj, widtype);
267 if (part && strcmp(part, "icon")) return;
268 wd = elm_widget_data_get(obj);
270 if (wd->icon == content) return;
271 if (wd->icon) evas_object_del(wd->icon);
275 elm_widget_sub_object_add(obj, content);
276 evas_object_event_callback_add(content,
277 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
278 _changed_size_hints, obj);
279 edje_object_part_swallow(wd->chk, "elm.swallow.content", content);
280 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
281 edje_object_message_signal_process(wd->chk);
287 _content_get_hook(const Evas_Object *obj, const char *part)
289 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
292 if (part && strcmp(part, "icon")) return NULL;
293 wd = elm_widget_data_get(obj);
294 if (!wd) return NULL;
299 _content_unset_hook(Evas_Object *obj, const char *part)
301 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
304 if (part && strcmp(part, "icon")) return NULL;
305 wd = elm_widget_data_get(obj);
306 if (!wd) return NULL;
307 if (!wd->icon) return NULL;
308 Evas_Object *icon = wd->icon;
309 elm_widget_sub_object_del(obj, wd->icon);
310 edje_object_part_unswallow(wd->chk, icon);
315 _activate(Evas_Object *obj)
317 Widget_Data *wd = elm_widget_data_get(obj);
319 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
320 (_elm_access_2nd_click_timeout(obj)))
322 wd->state = !wd->state;
323 if (wd->statep) *wd->statep = wd->state;
326 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
327 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
331 _elm_access_say(E_("State: On"));
334 _elm_access_say(E_("State: On"));
339 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
340 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
344 _elm_access_say(E_("State: Off"));
347 _elm_access_say(E_("State: Off"));
350 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
355 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
357 ELM_CHECK_WIDTYPE(obj, widtype);
358 Widget_Data *wd = elm_widget_data_get(obj);
360 if ((!item) || (!strcmp(item, "default")))
362 eina_stringshare_replace(&wd->label, label);
364 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
366 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
367 edje_object_message_signal_process(wd->chk);
368 edje_object_part_text_set(wd->chk, "elm.text", label);
370 else if ((item) && (!strcmp(item, "on")))
372 eina_stringshare_replace(&wd->ontext, label);
373 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
375 else if ((item) && (!strcmp(item, "off")))
377 eina_stringshare_replace(&wd->offtext, label);
378 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
384 _elm_check_label_get(const Evas_Object *obj, const char *item)
386 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
387 Widget_Data *wd = elm_widget_data_get(obj);
388 if (!wd) return NULL;
389 if ((!item) || (!strcmp(item, "default")))
391 else if ((item) && (!strcmp(item, "on")))
393 else if ((item) && (!strcmp(item, "off")))
399 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
401 const char *txt = elm_widget_access_info_get(obj);
402 if (!txt) txt = _elm_check_label_get(obj, NULL);
403 if (txt) return strdup(txt);
408 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
410 Evas_Object *o = data;
411 Widget_Data *wd = elm_widget_data_get(o);
412 if (!wd) return NULL;
413 if (elm_widget_disabled_get(obj))
414 return strdup(E_("State: Disabled"));
421 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
425 return strdup(E_("State: On"));
431 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
434 return strdup(E_("State: Off"));
438 elm_check_add(Evas_Object *parent)
444 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
446 ELM_SET_WIDTYPE(widtype, "check");
447 elm_widget_type_set(obj, "check");
448 elm_widget_sub_object_add(parent, obj);
449 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
450 elm_widget_data_set(obj, wd);
451 elm_widget_del_hook_set(obj, _del_hook);
452 elm_widget_theme_hook_set(obj, _theme_hook);
453 elm_widget_disable_hook_set(obj, _disable_hook);
454 elm_widget_can_focus_set(obj, EINA_TRUE);
455 elm_widget_activate_hook_set(obj, _activate_hook);
456 elm_widget_event_hook_set(obj, _event_hook);
457 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
458 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
459 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
460 elm_widget_text_set_hook_set(obj, _elm_check_label_set);
461 elm_widget_text_get_hook_set(obj, _elm_check_label_get);
462 elm_widget_content_set_hook_set(obj, _content_set_hook);
463 elm_widget_content_get_hook_set(obj, _content_get_hook);
464 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
466 wd->chk = edje_object_add(e);
467 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
468 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
469 _signal_check_on, obj);
470 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
471 _signal_check_off, obj);
472 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
473 _signal_check_toggle, obj);
474 elm_widget_resize_object_set(obj, wd->chk);
476 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
478 _mirrored_set(obj, elm_widget_mirrored_get(obj));
481 // TODO: convert Elementary to subclassing of Evas_Smart_Class
482 // TODO: and save some bytes, making descriptions per-class and not instance!
483 evas_object_smart_callbacks_descriptions_set(obj, _signals);
485 _elm_access_object_register(obj, wd->chk);
486 _elm_access_text_set(_elm_access_object_get(obj),
487 ELM_ACCESS_TYPE, E_("Check"));
488 _elm_access_callback_set(_elm_access_object_get(obj),
489 ELM_ACCESS_INFO, _access_info_cb, obj);
490 _elm_access_callback_set(_elm_access_object_get(obj),
491 ELM_ACCESS_STATE, _access_state_cb, obj);
496 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
498 ELM_CHECK_WIDTYPE(obj, widtype);
499 Widget_Data *wd = elm_widget_data_get(obj);
501 if (state != wd->state)
504 if (wd->statep) *wd->statep = wd->state;
506 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
508 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
510 edje_object_message_signal_process(wd->chk);
514 elm_check_state_get(const Evas_Object *obj)
516 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
517 Widget_Data *wd = elm_widget_data_get(obj);
518 if (!wd) return EINA_FALSE;
523 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
525 ELM_CHECK_WIDTYPE(obj, widtype);
526 Widget_Data *wd = elm_widget_data_get(obj);
531 if (*wd->statep != wd->state)
533 wd->state = *wd->statep;
535 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
537 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");