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_update(void *data)
181 Widget_Data *wd = elm_widget_data_get(data);
184 elm_widget_sub_object_add(data, wd->icon);
185 evas_object_event_callback_add(wd->icon,
186 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
187 _changed_size_hints, data);
188 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
189 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
190 edje_object_message_signal_process(wd->btn);
195 _content_set_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
197 Widget_Data *wd = elm_widget_data_get(data);
198 Evas_Object *old_icon = edje_object_part_swallow_get(wd->btn, "elm.swallow.content");
199 if (wd->icon != old_icon)
201 evas_object_event_callback_del_full(old_icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
202 _changed_size_hints, obj);
203 edje_object_part_unswallow(wd->btn, old_icon);
204 elm_widget_sub_object_del(data, old_icon);
205 evas_object_del(old_icon);
207 _content_set_update(data);
208 edje_object_signal_callback_del(obj, emission, source, _content_set_cb);
209 edje_object_signal_emit(wd->btn, "elm,state,icon,reset", "elm");
213 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
216 ELM_CHECK_WIDTYPE(obj, widtype);
217 Widget_Data *wd = elm_widget_data_get(obj);
219 if (part && strcmp(part, "icon")) return;
220 if (wd->icon == content) return;
223 s = edje_object_data_get(wd->btn, "transition_animation_on");
224 if ((s) && (atoi(s)))
226 edje_object_part_swallow(wd->btn, "elm.swallow.content_new", wd->icon);
227 edje_object_signal_emit(wd->btn, "elm,state,icon_set", "elm");
228 edje_object_signal_callback_add(wd->btn,
229 "elm,state,icon_set,done", "elm",
230 _content_set_cb, obj);
233 _content_set_update(obj);
239 _content_get_hook(const Evas_Object *obj, const char *part)
241 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
244 if (part && strcmp(part, "icon")) return NULL;
245 wd = elm_widget_data_get(obj);
246 if (!wd) return NULL;
251 _content_unset_hook(Evas_Object *obj, const char *part)
253 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
256 if (part && strcmp(part, "icon")) return NULL;
257 wd = elm_widget_data_get(obj);
258 if (!wd) return NULL;
259 if (!wd->icon) return NULL;
260 Evas_Object *icon = wd->icon;
261 elm_widget_sub_object_del(obj, wd->icon);
262 evas_object_event_callback_del_full(wd->icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
263 _changed_size_hints, obj);
264 edje_object_part_unswallow(wd->btn, wd->icon);
270 _sizing_eval(Evas_Object *obj)
272 Widget_Data *wd = elm_widget_data_get(obj);
273 Evas_Coord minw = -1, minh = -1;
276 if (wd->delete_me) return;
277 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
278 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
279 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
280 evas_object_size_hint_min_set(obj, minw, minh);
284 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
286 Widget_Data *wd = elm_widget_data_get(data);
288 if (obj != wd->icon) return;
293 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
295 Widget_Data *wd = elm_widget_data_get(obj);
296 Evas_Object *sub = event_info;
300 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
301 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
302 _changed_size_hints, obj);
304 edje_object_message_signal_process(wd->btn);
310 _activate(Evas_Object *obj)
312 Widget_Data *wd = elm_widget_data_get(obj);
316 ecore_timer_del(wd->timer);
319 wd->repeating = EINA_FALSE;
320 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
321 (_elm_access_2nd_click_timeout(obj)))
323 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
324 _elm_access_say(E_("Clicked"));
325 if (!elm_widget_disabled_get(obj) &&
326 !evas_object_freeze_events_get(obj))
327 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
332 _activate_hook(Evas_Object *obj)
338 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
344 _autorepeat_send(void *data)
346 Widget_Data *wd = elm_widget_data_get(data);
347 if (!wd) return ECORE_CALLBACK_CANCEL;
349 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
353 return ECORE_CALLBACK_CANCEL;
356 return ECORE_CALLBACK_RENEW;
360 _autorepeat_initial_send(void *data)
362 Widget_Data *wd = elm_widget_data_get(data);
363 if (!wd) return ECORE_CALLBACK_CANCEL;
365 if (wd->timer) ecore_timer_del(wd->timer);
366 wd->repeating = EINA_TRUE;
367 _autorepeat_send(data);
368 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
370 return ECORE_CALLBACK_CANCEL;
374 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
376 Widget_Data *wd = elm_widget_data_get(data);
379 if ((wd->autorepeat) && (!wd->repeating))
381 if (wd->ar_threshold <= 0.0)
382 _autorepeat_initial_send(data); /* call immediately */
384 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
387 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
391 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
393 Widget_Data *wd = elm_widget_data_get(data);
398 ecore_timer_del(wd->timer);
401 wd->repeating = EINA_FALSE;
402 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
406 _elm_button_label_update(void *data)
408 Widget_Data *wd = elm_widget_data_get(data);
411 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
413 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
414 edje_object_message_signal_process(wd->btn);
415 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
420 _elm_button_label_set_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
422 Widget_Data *wd = elm_widget_data_get(data);
423 _elm_button_label_update(data);
424 edje_object_signal_callback_del(obj, emission, source,
425 _elm_button_label_set_cb);
426 edje_object_signal_emit(wd->btn, "elm,state,label,reset", "elm");
430 _elm_button_label_set(Evas_Object *obj, const char *item, const char *label)
433 ELM_CHECK_WIDTYPE(obj, widtype);
434 Widget_Data *wd = elm_widget_data_get(obj);
435 if (item && strcmp(item, "default")) return;
437 if ((label) && ((wd->label) && (!strcmp(label, wd->label)))) return;
438 eina_stringshare_replace(&wd->label, label);
439 s = edje_object_data_get(wd->btn, "transition_animation_on");
440 if ((s) && (atoi(s)))
442 edje_object_part_text_set(wd->btn, "elm.text_new", wd->label);
443 edje_object_signal_emit(wd->btn, "elm,state,label_set", "elm");
444 edje_object_signal_callback_add(wd->btn,
445 "elm,state,label_set,done", "elm",
446 _elm_button_label_set_cb, obj);
449 _elm_button_label_update(obj);
453 _elm_button_label_get(const Evas_Object *obj, const char *item)
455 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
456 Widget_Data *wd = elm_widget_data_get(obj);
457 if (item && strcmp(item, "default")) return NULL;
458 if (!wd) return NULL;
463 _access_info_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
465 const char *txt = elm_widget_access_info_get(obj);
466 if (!txt) txt = _elm_button_label_get(obj, NULL);
467 if (txt) return strdup(txt);
472 _access_state_cb(void *data __UNUSED__, Evas_Object *obj, Elm_Widget_Item *item __UNUSED__)
474 if (elm_widget_disabled_get(obj))
475 return strdup(E_("State: Disabled"));
480 elm_button_add(Evas_Object *parent)
486 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
488 ELM_SET_WIDTYPE(widtype, "button");
489 elm_widget_type_set(obj, "button");
490 elm_widget_sub_object_add(parent, obj);
491 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
492 elm_widget_data_set(obj, wd);
493 elm_widget_del_hook_set(obj, _del_hook);
494 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
495 elm_widget_theme_hook_set(obj, _theme_hook);
496 elm_widget_disable_hook_set(obj, _disable_hook);
497 elm_widget_can_focus_set(obj, EINA_TRUE);
498 elm_widget_activate_hook_set(obj, _activate_hook);
499 elm_widget_event_hook_set(obj, _event_hook);
500 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
501 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
502 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
503 elm_widget_text_set_hook_set(obj, _elm_button_label_set);
504 elm_widget_text_get_hook_set(obj, _elm_button_label_get);
505 elm_widget_content_set_hook_set(obj, _content_set_hook);
506 elm_widget_content_get_hook_set(obj, _content_get_hook);
507 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
509 wd->btn = edje_object_add(e);
510 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
511 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
512 _signal_clicked, obj);
513 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
514 _signal_pressed, obj);
515 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
516 _signal_unpressed, obj);
517 elm_widget_resize_object_set(obj, wd->btn);
519 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
523 // TODO: convert Elementary to subclassing of Evas_Smart_Class
524 // TODO: and save some bytes, making descriptions per-class and not instance!
525 evas_object_smart_callbacks_descriptions_set(obj, _signals);
527 _elm_access_object_register(obj, wd->btn);
528 _elm_access_text_set(_elm_access_object_get(obj),
529 ELM_ACCESS_TYPE, E_("Button"));
530 _elm_access_callback_set(_elm_access_object_get(obj),
531 ELM_ACCESS_INFO, _access_info_cb, obj);
532 _elm_access_callback_set(_elm_access_object_get(obj),
533 ELM_ACCESS_STATE, _access_state_cb, obj);
538 elm_button_label_set(Evas_Object *obj, const char *label)
540 _elm_button_label_set(obj, NULL, label);
544 elm_button_label_get(const Evas_Object *obj)
546 return _elm_button_label_get(obj, NULL);
551 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
553 _content_set_hook(obj, "icon", icon);
557 elm_button_icon_get(const Evas_Object *obj)
559 return _content_get_hook(obj, "icon");
563 elm_button_icon_unset(Evas_Object *obj)
565 return _content_unset_hook(obj, "icon");
569 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
571 ELM_CHECK_WIDTYPE(obj, widtype);
572 Widget_Data *wd = elm_widget_data_get(obj);
576 ecore_timer_del(wd->timer);
580 wd->repeating = EINA_FALSE;
584 elm_button_autorepeat_get(const Evas_Object *obj)
586 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
587 Widget_Data *wd = elm_widget_data_get(obj);
588 if (!wd) return EINA_FALSE;
589 return wd->autorepeat;
593 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
595 ELM_CHECK_WIDTYPE(obj, widtype);
596 Widget_Data *wd = elm_widget_data_get(obj);
598 if (wd->ar_threshold == t) return;
601 ecore_timer_del(wd->timer);
604 wd->ar_threshold = t;
608 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
610 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
611 Widget_Data *wd = elm_widget_data_get(obj);
613 return wd->ar_threshold;
617 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
619 ELM_CHECK_WIDTYPE(obj, widtype);
620 Widget_Data *wd = elm_widget_data_get(obj);
622 if (wd->ar_interval == t) return;
625 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
629 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
631 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
632 Widget_Data *wd = elm_widget_data_get(obj);
634 return wd->ar_interval;