1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
8 Evas_Object *btn, *icon;
13 Eina_Bool autorepeat : 1;
14 Eina_Bool repeating : 1;
15 Eina_Bool delete_me : 1;
18 static const char *widtype = NULL;
19 static void _del_hook(Evas_Object *obj);
20 static void _del_pre_hook(Evas_Object *obj);
21 static void _theme_hook(Evas_Object *obj);
22 static void _disable_hook(Evas_Object *obj);
23 static void _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
24 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
25 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
26 static void _sizing_eval(Evas_Object *obj);
27 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
28 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
29 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
30 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
31 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
32 static void _on_focus_hook(void *data, Evas_Object *obj);
33 static void _activate(Evas_Object *obj);
34 static void _activate_hook(Evas_Object *obj);
35 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
36 Evas_Callback_Type type, void *event_info);
38 static const char SIG_CLICKED[] = "clicked";
39 static const char SIG_REPEATED[] = "repeated";
40 static const char SIG_PRESSED[] = "pressed";
41 static const char SIG_UNPRESSED[] = "unpressed";
42 static const Evas_Smart_Cb_Description _signals[] = {
51 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
53 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
54 Evas_Event_Key_Down *ev = event_info;
55 Widget_Data *wd = elm_widget_data_get(obj);
56 if (!wd) return EINA_FALSE;
57 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
58 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
59 if ((strcmp(ev->keyname, "Return")) &&
60 (strcmp(ev->keyname, "KP_Enter")) &&
61 (strcmp(ev->keyname, "space")))
64 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
65 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
70 _del_pre_hook(Evas_Object *obj)
72 Widget_Data *wd = elm_widget_data_get(obj);
74 wd->delete_me = EINA_TRUE;
78 _del_hook(Evas_Object *obj)
80 Widget_Data *wd = elm_widget_data_get(obj);
82 if (wd->label) eina_stringshare_del(wd->label);
87 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
89 Widget_Data *wd = elm_widget_data_get(obj);
91 if (elm_widget_focus_get(obj))
93 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
94 evas_object_focus_set(wd->btn, EINA_TRUE);
98 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
99 evas_object_focus_set(wd->btn, EINA_FALSE);
104 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
106 Widget_Data *wd = elm_widget_data_get(obj);
108 edje_object_mirrored_set(wd->btn, rtl);
112 _theme_hook(Evas_Object *obj)
114 Widget_Data *wd = elm_widget_data_get(obj);
117 _elm_widget_mirrored_reload(obj);
118 _mirrored_set(obj, elm_widget_mirrored_get(obj));
119 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
121 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
123 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
125 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
127 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
129 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
130 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
131 if (elm_object_disabled_get(obj))
132 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
133 edje_object_message_signal_process(wd->btn);
134 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
135 str = edje_object_data_get(wd->btn, "focus_highlight");
136 if ((str) && (!strcmp(str, "on")))
137 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
139 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
144 _disable_hook(Evas_Object *obj)
146 Widget_Data *wd = elm_widget_data_get(obj);
148 if (elm_widget_disabled_get(obj))
149 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
151 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
155 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
157 Widget_Data *wd = elm_widget_data_get(obj);
159 edje_object_signal_emit(wd->btn, emission, source);
163 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
165 Widget_Data *wd = elm_widget_data_get(obj);
167 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
171 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
173 Widget_Data *wd = elm_widget_data_get(obj);
174 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
179 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
181 ELM_CHECK_WIDTYPE(obj, widtype);
182 Widget_Data *wd = elm_widget_data_get(obj);
184 if (!part || strcmp(part, "icon")) return;
185 if (wd->icon == content) return;
186 if (wd->icon) evas_object_del(wd->icon);
190 elm_widget_sub_object_add(obj, content);
191 evas_object_event_callback_add(content,
192 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
193 _changed_size_hints, obj);
194 edje_object_part_swallow(wd->btn, "elm.swallow.content", content);
195 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
196 edje_object_message_signal_process(wd->btn);
202 _content_get_hook(const Evas_Object *obj, const char *part)
204 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
207 if (!part || strcmp(part, "icon")) return NULL;
208 wd = elm_widget_data_get(obj);
209 if (!wd) return NULL;
214 _content_unset_hook(Evas_Object *obj, const char *part)
216 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
219 if (!part || strcmp(part, "icon")) return NULL;
220 wd = elm_widget_data_get(obj);
221 if (!wd) return NULL;
222 if (!wd->icon) return NULL;
223 Evas_Object *icon = wd->icon;
224 elm_widget_sub_object_del(obj, wd->icon);
225 evas_object_event_callback_del_full(wd->icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
226 _changed_size_hints, obj);
227 edje_object_part_unswallow(wd->btn, wd->icon);
233 _sizing_eval(Evas_Object *obj)
235 Widget_Data *wd = elm_widget_data_get(obj);
236 Evas_Coord minw = -1, minh = -1;
239 if (wd->delete_me) return;
240 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
241 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
242 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
243 evas_object_size_hint_min_set(obj, minw, minh);
247 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
249 Widget_Data *wd = elm_widget_data_get(data);
251 if (obj != wd->icon) return;
256 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
258 Widget_Data *wd = elm_widget_data_get(obj);
259 Evas_Object *sub = event_info;
263 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
264 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
265 _changed_size_hints, obj);
267 edje_object_message_signal_process(wd->btn);
273 _activate(Evas_Object *obj)
275 Widget_Data *wd = elm_widget_data_get(obj);
279 ecore_timer_del(wd->timer);
282 wd->repeating = EINA_FALSE;
283 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
284 (_elm_access_2nd_click_timeout(obj)))
286 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
287 _elm_access_say(E_("Clicked"));
288 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
293 _activate_hook(Evas_Object *obj)
299 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
305 _autorepeat_send(void *data)
307 Widget_Data *wd = elm_widget_data_get(data);
308 if (!wd) return ECORE_CALLBACK_CANCEL;
310 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
314 return ECORE_CALLBACK_CANCEL;
317 return ECORE_CALLBACK_RENEW;
321 _autorepeat_initial_send(void *data)
323 Widget_Data *wd = elm_widget_data_get(data);
324 if (!wd) return ECORE_CALLBACK_CANCEL;
326 if (wd->timer) ecore_timer_del(wd->timer);
327 wd->repeating = EINA_TRUE;
328 _autorepeat_send(data);
329 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
331 return ECORE_CALLBACK_CANCEL;
335 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
337 Widget_Data *wd = elm_widget_data_get(data);
340 if ((wd->autorepeat) && (!wd->repeating))
342 if (wd->ar_threshold <= 0.0)
343 _autorepeat_initial_send(data); /* call immediately */
345 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
348 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
352 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
354 Widget_Data *wd = elm_widget_data_get(data);
359 ecore_timer_del(wd->timer);
362 wd->repeating = EINA_FALSE;
363 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
367 _elm_button_label_set(Evas_Object *obj, const char *item, const char *label)
369 ELM_CHECK_WIDTYPE(obj, widtype);
370 Widget_Data *wd = elm_widget_data_get(obj);
371 if (item && strcmp(item, "default")) return;
373 eina_stringshare_replace(&wd->label, label);
375 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
377 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
378 edje_object_message_signal_process(wd->btn);
379 edje_object_part_text_set(wd->btn, "elm.text", label);
384 _elm_button_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 (item && strcmp(item, "default")) return NULL;
389 if (!wd) return NULL;
394 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
396 const char *txt = elm_widget_access_info_get(obj);
397 if (!txt) txt = _elm_button_label_get(obj, NULL);
398 if (txt) return strdup(txt);
403 _access_state_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
405 if (elm_widget_disabled_get(obj))
406 return strdup(E_("State: Disabled"));
411 elm_button_add(Evas_Object *parent)
417 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
419 ELM_SET_WIDTYPE(widtype, "button");
420 elm_widget_type_set(obj, "button");
421 elm_widget_sub_object_add(parent, obj);
422 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
423 elm_widget_data_set(obj, wd);
424 elm_widget_del_hook_set(obj, _del_hook);
425 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
426 elm_widget_theme_hook_set(obj, _theme_hook);
427 elm_widget_disable_hook_set(obj, _disable_hook);
428 elm_widget_can_focus_set(obj, EINA_TRUE);
429 elm_widget_activate_hook_set(obj, _activate_hook);
430 elm_widget_event_hook_set(obj, _event_hook);
431 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
432 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
433 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
434 elm_widget_text_set_hook_set(obj, _elm_button_label_set);
435 elm_widget_text_get_hook_set(obj, _elm_button_label_get);
436 elm_widget_content_set_hook_set(obj, _content_set_hook);
437 elm_widget_content_get_hook_set(obj, _content_get_hook);
438 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
440 wd->btn = edje_object_add(e);
441 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
442 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
443 _signal_clicked, obj);
444 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
445 _signal_pressed, obj);
446 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
447 _signal_unpressed, obj);
448 elm_widget_resize_object_set(obj, wd->btn);
450 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
454 // TODO: convert Elementary to subclassing of Evas_Smart_Class
455 // TODO: and save some bytes, making descriptions per-class and not instance!
456 evas_object_smart_callbacks_descriptions_set(obj, _signals);
458 _elm_access_object_register(obj, wd->btn);
459 _elm_access_text_set(_elm_access_object_get(obj),
460 ELM_ACCESS_TYPE, E_("Button"));
461 _elm_access_callback_set(_elm_access_object_get(obj),
462 ELM_ACCESS_INFO, _access_info_cb, obj);
463 _elm_access_callback_set(_elm_access_object_get(obj),
464 ELM_ACCESS_STATE, _access_state_cb, obj);
469 elm_button_label_set(Evas_Object *obj, const char *label)
471 _elm_button_label_set(obj, NULL, label);
475 elm_button_label_get(const Evas_Object *obj)
477 return _elm_button_label_get(obj, NULL);
482 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
484 _content_set_hook(obj, "icon", icon);
488 elm_button_icon_get(const Evas_Object *obj)
490 return _content_get_hook(obj, "icon");
494 elm_button_icon_unset(Evas_Object *obj)
496 return _content_unset_hook(obj, "icon");
500 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
502 ELM_CHECK_WIDTYPE(obj, widtype);
503 Widget_Data *wd = elm_widget_data_get(obj);
507 ecore_timer_del(wd->timer);
511 wd->repeating = EINA_FALSE;
515 elm_button_autorepeat_get(const Evas_Object *obj)
517 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
518 Widget_Data *wd = elm_widget_data_get(obj);
519 if (!wd) return EINA_FALSE;
520 return wd->autorepeat;
524 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
526 ELM_CHECK_WIDTYPE(obj, widtype);
527 Widget_Data *wd = elm_widget_data_get(obj);
529 if (wd->ar_threshold == t) return;
532 ecore_timer_del(wd->timer);
535 wd->ar_threshold = t;
539 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
541 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
542 Widget_Data *wd = elm_widget_data_get(obj);
544 return wd->ar_threshold;
548 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
550 ELM_CHECK_WIDTYPE(obj, widtype);
551 Widget_Data *wd = elm_widget_data_get(obj);
553 if (wd->ar_interval == t) return;
556 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
560 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
562 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
563 Widget_Data *wd = elm_widget_data_get(obj);
565 return wd->ar_interval;