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 __UNUSED__, Evas_Object *content)
238 ELM_CHECK_WIDTYPE(obj, widtype);
239 Widget_Data *wd = elm_widget_data_get(obj);
241 if (wd->icon == content) return;
242 if (wd->icon) evas_object_del(wd->icon);
246 elm_widget_sub_object_add(obj, content);
247 evas_object_event_callback_add(content,
248 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
249 _changed_size_hints, obj);
250 edje_object_part_swallow(wd->chk, "elm.swallow.content", content);
251 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
252 edje_object_message_signal_process(wd->chk);
258 _content_get_hook(const Evas_Object *obj, const char *part __UNUSED__)
260 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
261 Widget_Data *wd = elm_widget_data_get(obj);
262 if (!wd) return NULL;
267 _content_unset_hook(Evas_Object *obj, const char *part __UNUSED__)
269 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
270 Widget_Data *wd = elm_widget_data_get(obj);
271 if (!wd) return NULL;
272 if (!wd->icon) return NULL;
273 Evas_Object *icon = wd->icon;
274 elm_widget_sub_object_del(obj, wd->icon);
275 evas_object_event_callback_del_full(wd->icon,
276 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
277 _changed_size_hints, obj);
278 edje_object_part_unswallow(wd->chk, wd->icon);
284 _activate(Evas_Object *obj)
286 Widget_Data *wd = elm_widget_data_get(obj);
288 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
289 (_elm_access_2nd_click_timeout(obj)))
291 wd->state = !wd->state;
292 if (wd->statep) *wd->statep = wd->state;
295 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
296 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
300 _elm_access_say(E_("State: On"));
303 _elm_access_say(E_("State: On"));
308 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
309 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
313 _elm_access_say(E_("State: Off"));
316 _elm_access_say(E_("State: Off"));
319 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
324 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
326 ELM_CHECK_WIDTYPE(obj, widtype);
327 Widget_Data *wd = elm_widget_data_get(obj);
329 if ((!item) || (!strcmp(item, "default")))
331 eina_stringshare_replace(&wd->label, label);
333 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
335 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
336 edje_object_message_signal_process(wd->chk);
337 edje_object_part_text_set(wd->chk, "elm.text", label);
339 else if ((item) && (!strcmp(item, "on")))
341 eina_stringshare_replace(&wd->ontext, label);
342 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
344 else if ((item) && (!strcmp(item, "off")))
346 eina_stringshare_replace(&wd->offtext, label);
347 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
353 _elm_check_label_get(const Evas_Object *obj, const char *item)
355 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
356 Widget_Data *wd = elm_widget_data_get(obj);
357 if (!wd) return NULL;
358 if ((!item) || (!strcmp(item, "default")))
360 else if ((item) && (!strcmp(item, "on")))
362 else if ((item) && (!strcmp(item, "off")))
368 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
370 const char *txt = elm_widget_access_info_get(obj);
371 if (!txt) txt = _elm_check_label_get(obj, NULL);
372 if (txt) return strdup(txt);
377 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
379 Evas_Object *o = data;
380 Widget_Data *wd = elm_widget_data_get(o);
381 if (!wd) return NULL;
382 if (elm_widget_disabled_get(obj))
383 return strdup(E_("State: Disabled"));
390 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
394 return strdup(E_("State: On"));
400 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
403 return strdup(E_("State: Off"));
407 elm_check_add(Evas_Object *parent)
413 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
415 ELM_SET_WIDTYPE(widtype, "check");
416 elm_widget_type_set(obj, "check");
417 elm_widget_sub_object_add(parent, obj);
418 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
419 elm_widget_data_set(obj, wd);
420 elm_widget_del_hook_set(obj, _del_hook);
421 elm_widget_theme_hook_set(obj, _theme_hook);
422 elm_widget_disable_hook_set(obj, _disable_hook);
423 elm_widget_can_focus_set(obj, EINA_TRUE);
424 elm_widget_activate_hook_set(obj, _activate_hook);
425 elm_widget_event_hook_set(obj, _event_hook);
426 elm_widget_text_set_hook_set(obj, _elm_check_label_set);
427 elm_widget_text_get_hook_set(obj, _elm_check_label_get);
428 elm_widget_content_set_hook_set(obj, _content_set_hook);
429 elm_widget_content_get_hook_set(obj, _content_get_hook);
430 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
432 wd->chk = edje_object_add(e);
433 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
434 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
435 _signal_check_on, obj);
436 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
437 _signal_check_off, obj);
438 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
439 _signal_check_toggle, obj);
440 elm_widget_resize_object_set(obj, wd->chk);
442 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
444 _mirrored_set(obj, elm_widget_mirrored_get(obj));
447 // TODO: convert Elementary to subclassing of Evas_Smart_Class
448 // TODO: and save some bytes, making descriptions per-class and not instance!
449 evas_object_smart_callbacks_descriptions_set(obj, _signals);
451 _elm_access_object_register(obj, wd->chk);
452 _elm_access_text_set(_elm_access_object_get(obj),
453 ELM_ACCESS_TYPE, E_("Check"));
454 _elm_access_callback_set(_elm_access_object_get(obj),
455 ELM_ACCESS_INFO, _access_info_cb, obj);
456 _elm_access_callback_set(_elm_access_object_get(obj),
457 ELM_ACCESS_STATE, _access_state_cb, obj);
462 elm_check_label_set(Evas_Object *obj, const char *label)
464 _elm_check_label_set(obj, NULL, label);
468 elm_check_label_get(const Evas_Object *obj)
470 return _elm_check_label_get(obj, NULL);
474 elm_check_states_labels_set(Evas_Object *obj, const char *ontext, const char *offtext)
476 _elm_check_label_set(obj, "on", ontext);
477 _elm_check_label_set(obj, "off", offtext);
481 elm_check_states_labels_get(const Evas_Object *obj, const char **ontext, const char **offtext)
483 if (ontext) *ontext = _elm_check_label_get(obj, "on");
484 if (offtext) *offtext = _elm_check_label_get(obj, "off");
488 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
490 _content_set_hook(obj, NULL, icon);
494 elm_check_icon_get(const Evas_Object *obj)
496 return _content_get_hook(obj, NULL);
500 elm_check_icon_unset(Evas_Object *obj)
502 return _content_unset_hook(obj, NULL);
506 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
508 ELM_CHECK_WIDTYPE(obj, widtype);
509 Widget_Data *wd = elm_widget_data_get(obj);
511 if (state != wd->state)
514 if (wd->statep) *wd->statep = wd->state;
516 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
518 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
520 edje_object_message_signal_process(wd->chk);
524 elm_check_state_get(const Evas_Object *obj)
526 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
527 Widget_Data *wd = elm_widget_data_get(obj);
528 if (!wd) return EINA_FALSE;
533 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
535 ELM_CHECK_WIDTYPE(obj, widtype);
536 Widget_Data *wd = elm_widget_data_get(obj);
541 if (*wd->statep != wd->state)
543 wd->state = *wd->statep;
545 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
547 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");