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 _activate(Evas_Object *obj);
29 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
30 Evas_Callback_Type type, void *event_info);
32 static const char SIG_CHANGED[] = "changed";
33 static const Evas_Smart_Cb_Description _signals[] = {
39 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
41 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
42 Evas_Event_Key_Down *ev = event_info;
43 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
44 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
45 if ((strcmp(ev->keyname, "Return")) &&
46 (strcmp(ev->keyname, "KP_Enter")) &&
47 (strcmp(ev->keyname, "space")))
50 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
56 _del_hook(Evas_Object *obj)
58 Widget_Data *wd = elm_widget_data_get(obj);
60 if (wd->label) eina_stringshare_del(wd->label);
61 if (wd->ontext) eina_stringshare_del(wd->ontext);
62 if (wd->offtext) eina_stringshare_del(wd->offtext);
67 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
69 Widget_Data *wd = elm_widget_data_get(obj);
71 if (elm_widget_focus_get(obj))
73 edje_object_signal_emit(wd->chk, "elm,action,focus", "elm");
74 evas_object_focus_set(wd->chk, EINA_TRUE);
78 edje_object_signal_emit(wd->chk, "elm,action,unfocus", "elm");
79 evas_object_focus_set(wd->chk, EINA_FALSE);
84 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
86 Widget_Data *wd = elm_widget_data_get(obj);
88 edje_object_mirrored_set(wd->chk, rtl);
92 _theme_hook(Evas_Object *obj)
94 Widget_Data *wd = elm_widget_data_get(obj);
96 _elm_widget_mirrored_reload(obj);
97 _mirrored_set(obj, elm_widget_mirrored_get(obj));
98 _elm_theme_object_set(obj, wd->chk, "check", "base", elm_widget_style_get(obj));
100 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
102 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
104 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
106 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
108 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
110 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
111 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
112 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
113 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
114 if (elm_widget_disabled_get(obj))
115 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
116 edje_object_message_signal_process(wd->chk);
117 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
122 _disable_hook(Evas_Object *obj)
124 Widget_Data *wd = elm_widget_data_get(obj);
126 if (elm_widget_disabled_get(obj))
127 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
129 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
133 _sizing_eval(Evas_Object *obj)
135 Widget_Data *wd = elm_widget_data_get(obj);
136 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
138 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
139 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
140 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
141 evas_object_size_hint_min_set(obj, minw, minh);
142 evas_object_size_hint_max_set(obj, maxw, maxh);
146 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
148 Widget_Data *wd = elm_widget_data_get(data);
150 if (obj != wd->icon) return;
155 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
157 Widget_Data *wd = elm_widget_data_get(obj);
158 Evas_Object *sub = event_info;
162 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
163 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
164 _changed_size_hints, obj);
167 edje_object_message_signal_process(wd->chk);
172 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
174 Widget_Data *wd = elm_widget_data_get(data);
176 wd->state = EINA_FALSE;
177 if (wd->statep) *wd->statep = wd->state;
178 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
179 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
183 _signal_check_on(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
185 Widget_Data *wd = elm_widget_data_get(data);
187 wd->state = EINA_TRUE;
188 if (wd->statep) *wd->statep = wd->state;
189 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
190 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
194 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
200 _activate_hook(Evas_Object *obj)
206 _activate(Evas_Object *obj)
208 Widget_Data *wd = elm_widget_data_get(obj);
210 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
211 (_elm_access_2nd_click_timeout(obj)))
213 wd->state = !wd->state;
214 if (wd->statep) *wd->statep = wd->state;
217 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
218 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
222 _elm_access_say(E_("State: On"));
225 _elm_access_say(E_("State: On"));
230 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
231 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
235 _elm_access_say(E_("State: Off"));
238 _elm_access_say(E_("State: Off"));
241 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
246 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
248 ELM_CHECK_WIDTYPE(obj, widtype);
249 Widget_Data *wd = elm_widget_data_get(obj);
251 if ((!item) || (!strcmp(item, "default")))
253 eina_stringshare_replace(&wd->label, label);
255 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
257 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
258 edje_object_message_signal_process(wd->chk);
259 edje_object_part_text_set(wd->chk, "elm.text", label);
261 else if ((item) && (!strcmp(item, "on")))
263 eina_stringshare_replace(&wd->ontext, label);
264 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
266 else if ((item) && (!strcmp(item, "off")))
268 eina_stringshare_replace(&wd->offtext, label);
269 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
275 _elm_check_label_get(const Evas_Object *obj, const char *item)
277 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
278 Widget_Data *wd = elm_widget_data_get(obj);
279 if (!wd) return NULL;
280 if ((!item) || (!strcmp(item, "default")))
282 else if ((item) && (!strcmp(item, "on")))
284 else if ((item) && (!strcmp(item, "off")))
290 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
292 const char *txt = elm_widget_access_info_get(obj);
293 if (!txt) txt = _elm_check_label_get(obj, NULL);
294 if (txt) return strdup(txt);
299 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
301 Evas_Object *o = data;
302 Widget_Data *wd = elm_widget_data_get(o);
303 if (!wd) return NULL;
304 if (elm_widget_disabled_get(obj))
305 return strdup(E_("State: Disabled"));
312 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
316 return strdup(E_("State: On"));
322 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
325 return strdup(E_("State: Off"));
329 elm_check_add(Evas_Object *parent)
335 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
337 ELM_SET_WIDTYPE(widtype, "check");
338 elm_widget_type_set(obj, "check");
339 elm_widget_sub_object_add(parent, obj);
340 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
341 elm_widget_data_set(obj, wd);
342 elm_widget_del_hook_set(obj, _del_hook);
343 elm_widget_theme_hook_set(obj, _theme_hook);
344 elm_widget_disable_hook_set(obj, _disable_hook);
345 elm_widget_can_focus_set(obj, EINA_TRUE);
346 elm_widget_activate_hook_set(obj, _activate_hook);
347 elm_widget_event_hook_set(obj, _event_hook);
348 elm_widget_text_set_hook_set(obj, _elm_check_label_set);
349 elm_widget_text_get_hook_set(obj, _elm_check_label_get);
351 wd->chk = edje_object_add(e);
352 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
353 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
354 _signal_check_on, obj);
355 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
356 _signal_check_off, obj);
357 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
358 _signal_check_toggle, obj);
359 elm_widget_resize_object_set(obj, wd->chk);
361 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
363 _mirrored_set(obj, elm_widget_mirrored_get(obj));
366 // TODO: convert Elementary to subclassing of Evas_Smart_Class
367 // TODO: and save some bytes, making descriptions per-class and not instance!
368 evas_object_smart_callbacks_descriptions_set(obj, _signals);
370 _elm_access_object_register(obj, wd->chk);
371 _elm_access_text_set(_elm_access_object_get(obj),
372 ELM_ACCESS_TYPE, E_("Check"));
373 _elm_access_callback_set(_elm_access_object_get(obj),
374 ELM_ACCESS_INFO, _access_info_cb, obj);
375 _elm_access_callback_set(_elm_access_object_get(obj),
376 ELM_ACCESS_STATE, _access_state_cb, obj);
381 elm_check_label_set(Evas_Object *obj, const char *label)
383 _elm_check_label_set(obj, NULL, label);
387 elm_check_label_get(const Evas_Object *obj)
389 return _elm_check_label_get(obj, NULL);
393 elm_check_states_labels_set(Evas_Object *obj, const char *ontext, const char *offtext)
395 _elm_check_label_set(obj, "on", ontext);
396 _elm_check_label_set(obj, "off", offtext);
400 elm_check_states_labels_get(const Evas_Object *obj, const char **ontext, const char **offtext)
402 if (ontext) *ontext = _elm_check_label_get(obj, "on");
403 if (offtext) *offtext = _elm_check_label_get(obj, "off");
407 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
409 ELM_CHECK_WIDTYPE(obj, widtype);
410 Widget_Data *wd = elm_widget_data_get(obj);
412 if (wd->icon == icon) return;
413 if (wd->icon) evas_object_del(wd->icon);
417 elm_widget_sub_object_add(obj, icon);
418 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
419 _changed_size_hints, obj);
420 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
421 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
422 edje_object_message_signal_process(wd->chk);
428 elm_check_icon_get(const Evas_Object *obj)
430 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
431 Widget_Data *wd = elm_widget_data_get(obj);
432 if (!wd) return NULL;
437 elm_check_icon_unset(Evas_Object *obj)
439 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
440 Widget_Data *wd = elm_widget_data_get(obj);
441 if (!wd) return NULL;
442 if (!wd->icon) return NULL;
443 Evas_Object *icon = wd->icon;
444 elm_widget_sub_object_del(obj, wd->icon);
445 edje_object_part_unswallow(wd->chk, wd->icon);
451 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
453 ELM_CHECK_WIDTYPE(obj, widtype);
454 Widget_Data *wd = elm_widget_data_get(obj);
456 if (state != wd->state)
459 if (wd->statep) *wd->statep = wd->state;
461 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
463 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
465 edje_object_message_signal_process(wd->chk);
469 elm_check_state_get(const Evas_Object *obj)
471 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
472 Widget_Data *wd = elm_widget_data_get(obj);
473 if (!wd) return EINA_FALSE;
478 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
480 ELM_CHECK_WIDTYPE(obj, widtype);
481 Widget_Data *wd = elm_widget_data_get(obj);
486 if (*wd->statep != wd->state)
488 wd->state = *wd->statep;
490 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
492 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");