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");
105 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
106 edje_object_signal_emit(wd->chk, "elm,state,toggle,on", "elm");
110 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
111 edje_object_signal_emit(wd->chk, "elm,state,toggle,off", "elm");
114 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
116 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
117 edje_object_part_text_set(wd->chk, "elm.text", wd->label);
118 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
119 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
120 if (elm_widget_disabled_get(obj))
121 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
122 edje_object_message_signal_process(wd->chk);
123 edje_object_scale_set(wd->chk, elm_widget_scale_get(obj) * _elm_config->scale);
128 _disable_hook(Evas_Object *obj)
130 Widget_Data *wd = elm_widget_data_get(obj);
132 if (elm_widget_disabled_get(obj))
133 edje_object_signal_emit(wd->chk, "elm,state,disabled", "elm");
135 edje_object_signal_emit(wd->chk, "elm,state,enabled", "elm");
139 _sizing_eval(Evas_Object *obj)
141 Widget_Data *wd = elm_widget_data_get(obj);
142 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
144 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
145 edje_object_size_min_restricted_calc(wd->chk, &minw, &minh, minw, minh);
146 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
147 evas_object_size_hint_min_set(obj, minw, minh);
148 evas_object_size_hint_max_set(obj, maxw, maxh);
152 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
154 Widget_Data *wd = elm_widget_data_get(data);
156 if (obj != wd->icon) return;
161 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
163 Widget_Data *wd = elm_widget_data_get(obj);
164 Evas_Object *sub = event_info;
168 edje_object_signal_emit(wd->chk, "elm,state,icon,hidden", "elm");
169 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
170 _changed_size_hints, obj);
173 edje_object_message_signal_process(wd->chk);
178 _signal_check_off(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
180 Widget_Data *wd = elm_widget_data_get(data);
182 wd->state = EINA_FALSE;
183 if (wd->statep) *wd->statep = wd->state;
184 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
185 edje_object_signal_emit(wd->chk, "elm,state,toggle,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 edje_object_signal_emit(wd->chk, "elm,state,toggle,on", "elm");
198 evas_object_smart_callback_call(data, SIG_CHANGED, NULL);
202 _signal_check_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
208 _activate_hook(Evas_Object *obj)
214 _activate(Evas_Object *obj)
216 Widget_Data *wd = elm_widget_data_get(obj);
218 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
219 (_elm_access_2nd_click_timeout(obj)))
221 wd->state = !wd->state;
222 if (wd->statep) *wd->statep = wd->state;
225 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
226 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
230 _elm_access_say(E_("State: On"));
233 _elm_access_say(E_("State: On"));
238 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
239 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
243 _elm_access_say(E_("State: Off"));
246 _elm_access_say(E_("State: Off"));
249 evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
254 _elm_check_label_set(Evas_Object *obj, const char *item, const char *label)
256 ELM_CHECK_WIDTYPE(obj, widtype);
257 Widget_Data *wd = elm_widget_data_get(obj);
259 if ((!item) || (!strcmp(item, "default")))
261 eina_stringshare_replace(&wd->label, label);
263 edje_object_signal_emit(wd->chk, "elm,state,text,visible", "elm");
265 edje_object_signal_emit(wd->chk, "elm,state,text,hidden", "elm");
266 edje_object_message_signal_process(wd->chk);
267 edje_object_part_text_set(wd->chk, "elm.text", label);
269 else if ((item) && (!strcmp(item, "on")))
271 eina_stringshare_replace(&wd->ontext, label);
272 edje_object_part_text_set(wd->chk, "elm.ontext", wd->ontext);
274 else if ((item) && (!strcmp(item, "off")))
276 eina_stringshare_replace(&wd->offtext, label);
277 edje_object_part_text_set(wd->chk, "elm.offtext", wd->offtext);
283 _elm_check_label_get(const Evas_Object *obj, const char *item)
285 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
286 Widget_Data *wd = elm_widget_data_get(obj);
287 if (!wd) return NULL;
288 if ((!item) || (!strcmp(item, "default")))
290 else if ((item) && (!strcmp(item, "on")))
292 else if ((item) && (!strcmp(item, "off")))
298 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
300 const char *txt = elm_widget_access_info_get(obj);
301 if (!txt) txt = _elm_check_label_get(obj, NULL);
302 if (txt) return strdup(txt);
307 _access_state_cb(void *data, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
309 Evas_Object *o = data;
310 Widget_Data *wd = elm_widget_data_get(o);
311 if (!wd) return NULL;
312 if (elm_widget_disabled_get(obj))
313 return strdup(E_("State: Disabled"));
320 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->ontext);
324 return strdup(E_("State: On"));
330 snprintf(buf, sizeof(buf), "%s: %s", E_("State"), wd->offtext);
333 return strdup(E_("State: Off"));
337 elm_check_add(Evas_Object *parent)
343 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
345 ELM_SET_WIDTYPE(widtype, "check");
346 elm_widget_type_set(obj, "check");
347 elm_widget_sub_object_add(parent, obj);
348 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
349 elm_widget_data_set(obj, wd);
350 elm_widget_del_hook_set(obj, _del_hook);
351 elm_widget_theme_hook_set(obj, _theme_hook);
352 elm_widget_disable_hook_set(obj, _disable_hook);
353 elm_widget_can_focus_set(obj, EINA_TRUE);
354 elm_widget_activate_hook_set(obj, _activate_hook);
355 elm_widget_event_hook_set(obj, _event_hook);
356 elm_widget_text_set_hook_set(obj, _elm_check_label_set);
357 elm_widget_text_get_hook_set(obj, _elm_check_label_get);
359 wd->chk = edje_object_add(e);
360 _elm_theme_object_set(obj, wd->chk, "check", "base", "default");
361 edje_object_signal_callback_add(wd->chk, "elm,action,check,on", "",
362 _signal_check_on, obj);
363 edje_object_signal_callback_add(wd->chk, "elm,action,check,off", "",
364 _signal_check_off, obj);
365 edje_object_signal_callback_add(wd->chk, "elm,action,check,toggle", "",
366 _signal_check_toggle, obj);
367 elm_widget_resize_object_set(obj, wd->chk);
369 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
371 _mirrored_set(obj, elm_widget_mirrored_get(obj));
374 // TODO: convert Elementary to subclassing of Evas_Smart_Class
375 // TODO: and save some bytes, making descriptions per-class and not instance!
376 evas_object_smart_callbacks_descriptions_set(obj, _signals);
378 _elm_access_object_register(obj, wd->chk);
379 _elm_access_text_set(_elm_access_object_get(obj),
380 ELM_ACCESS_TYPE, E_("Check"));
381 _elm_access_callback_set(_elm_access_object_get(obj),
382 ELM_ACCESS_INFO, _access_info_cb, obj);
383 _elm_access_callback_set(_elm_access_object_get(obj),
384 ELM_ACCESS_STATE, _access_state_cb, obj);
389 elm_check_label_set(Evas_Object *obj, const char *label)
391 _elm_check_label_set(obj, NULL, label);
395 elm_check_label_get(const Evas_Object *obj)
397 return _elm_check_label_get(obj, NULL);
401 elm_check_states_labels_set(Evas_Object *obj, const char *ontext, const char *offtext)
403 _elm_check_label_set(obj, "on", ontext);
404 _elm_check_label_set(obj, "off", offtext);
408 elm_check_states_labels_get(const Evas_Object *obj, const char **ontext, const char **offtext)
410 if (ontext) *ontext = _elm_check_label_get(obj, "on");
411 if (offtext) *offtext = _elm_check_label_get(obj, "off");
415 elm_check_icon_set(Evas_Object *obj, Evas_Object *icon)
417 ELM_CHECK_WIDTYPE(obj, widtype);
418 Widget_Data *wd = elm_widget_data_get(obj);
420 if (wd->icon == icon) return;
421 if (wd->icon) evas_object_del(wd->icon);
425 elm_widget_sub_object_add(obj, icon);
426 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
427 _changed_size_hints, obj);
428 edje_object_part_swallow(wd->chk, "elm.swallow.content", icon);
429 edje_object_signal_emit(wd->chk, "elm,state,icon,visible", "elm");
430 edje_object_message_signal_process(wd->chk);
436 elm_check_icon_get(const Evas_Object *obj)
438 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
439 Widget_Data *wd = elm_widget_data_get(obj);
440 if (!wd) return NULL;
445 elm_check_icon_unset(Evas_Object *obj)
447 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
448 Widget_Data *wd = elm_widget_data_get(obj);
449 if (!wd) return NULL;
450 if (!wd->icon) return NULL;
451 Evas_Object *icon = wd->icon;
452 elm_widget_sub_object_del(obj, wd->icon);
453 edje_object_part_unswallow(wd->chk, wd->icon);
459 elm_check_state_set(Evas_Object *obj, Eina_Bool state)
461 ELM_CHECK_WIDTYPE(obj, widtype);
462 Widget_Data *wd = elm_widget_data_get(obj);
464 if (state != wd->state)
467 if (wd->statep) *wd->statep = wd->state;
470 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
471 edje_object_signal_emit(wd->chk, "elm,state,toggle,on", "elm");
475 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
476 edje_object_signal_emit(wd->chk, "elm,state,toggle,off", "elm");
479 edje_object_message_signal_process(wd->chk);
483 elm_check_state_get(const Evas_Object *obj)
485 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
486 Widget_Data *wd = elm_widget_data_get(obj);
487 if (!wd) return EINA_FALSE;
492 elm_check_state_pointer_set(Evas_Object *obj, Eina_Bool *statep)
494 ELM_CHECK_WIDTYPE(obj, widtype);
495 Widget_Data *wd = elm_widget_data_get(obj);
500 if (*wd->statep != wd->state)
502 wd->state = *wd->statep;
505 edje_object_signal_emit(wd->chk, "elm,state,check,on", "elm");
506 edje_object_signal_emit(wd->chk, "elm,state,toggle,on", "elm");
510 edje_object_signal_emit(wd->chk, "elm,state,check,off", "elm");
511 edje_object_signal_emit(wd->chk, "elm,state,toggle,off", "elm");