1 #include <Elementary.h>
3 #include "elm_widget_button.h"
4 #include "elm_widget_layout.h"
6 EAPI Eo_Op ELM_OBJ_BUTTON_BASE_ID = EO_NOOP;
8 #define MY_CLASS ELM_OBJ_BUTTON_CLASS
10 #define MY_CLASS_NAME "elm_button"
12 static const char SIG_CLICKED[] = "clicked";
13 static const char SIG_REPEATED[] = "repeated";
14 static const char SIG_PRESSED[] = "pressed";
15 static const char SIG_UNPRESSED[] = "unpressed";
17 static const Elm_Layout_Part_Alias_Description _content_aliases[] =
19 {"icon", "elm.swallow.content"},
23 static const Elm_Layout_Part_Alias_Description _text_aliases[] =
25 {"default", "elm.text"},
29 /* smart callbacks coming from elm button objects (besides the ones
30 * coming from elm layout): */
31 static const Evas_Smart_Cb_Description _smart_callbacks[] = {
40 _activate(Evas_Object *obj)
42 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
46 ecore_timer_del(sd->timer);
50 sd->repeating = EINA_FALSE;
52 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
53 (_elm_access_2nd_click_timeout(obj)))
55 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
56 _elm_access_say(E_("Clicked"));
57 if (!elm_widget_disabled_get(obj) &&
58 !evas_object_freeze_events_get(obj))
59 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
64 _elm_button_smart_sizing_eval(Eo *obj, void *_pd EINA_UNUSED, va_list *list EINA_UNUSED)
66 Elm_Widget_Smart_Data *wd = eo_data_get(obj, ELM_OBJ_WIDGET_CLASS);
67 Evas_Coord minw = -1, minh = -1;
69 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
70 edje_object_size_min_restricted_calc
71 (wd->resize_obj, &minw, &minh, minw, minh);
72 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
73 evas_object_size_hint_min_set(obj, minw, minh);
77 _elm_button_smart_activate(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
79 Elm_Activate act = va_arg(*list, Elm_Activate);
80 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
81 if (ret) *ret = EINA_FALSE;
83 if (act != ELM_ACTIVATE_DEFAULT) return;
85 if (!elm_widget_disabled_get(obj) &&
86 !evas_object_freeze_events_get(obj))
88 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
89 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
92 if (ret) *ret = EINA_TRUE;
95 /* FIXME: replicated from elm_layout just because button's icon spot
96 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
97 * can changed the theme API */
99 _icon_signal_emit(Evas_Object *obj)
103 snprintf(buf, sizeof(buf), "elm,state,icon,%s",
104 elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
106 elm_layout_signal_emit(obj, buf, "elm");
107 edje_object_message_signal_process(elm_layout_edje_get(obj));
108 eo_do(obj, elm_obj_layout_sizing_eval());
111 /* FIXME: replicated from elm_layout just because button's icon spot
112 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
113 * can changed the theme API */
115 _elm_button_smart_theme(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
117 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
118 if (ret) *ret = EINA_FALSE;
119 Eina_Bool int_ret = EINA_FALSE;
121 eo_do_super(obj, MY_CLASS, elm_wdg_theme(&int_ret));
122 if (!int_ret) return;
123 _icon_signal_emit(obj);
125 if (ret) *ret = EINA_TRUE;
128 /* FIXME: replicated from elm_layout just because button's icon spot
129 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
130 * can changed the theme API */
132 _elm_button_smart_sub_object_del(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
134 Evas_Object *sobj = va_arg(*list, Evas_Object *);
135 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
136 if (ret) *ret = EINA_FALSE;
137 Eina_Bool int_ret = EINA_FALSE;
139 eo_do_super(obj, MY_CLASS, elm_wdg_sub_object_del(sobj, &int_ret));
140 if (!int_ret) return;
142 _icon_signal_emit(obj);
144 if (ret) *ret = EINA_TRUE;
147 /* FIXME: replicated from elm_layout just because button's icon spot
148 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
149 * can changed the theme API */
151 _elm_button_smart_content_set(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
153 const char *part = va_arg(*list, const char *);
154 Evas_Object *content = va_arg(*list, Evas_Object *);
155 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
156 if (ret) *ret = EINA_FALSE;
157 Eina_Bool int_ret = EINA_FALSE;
159 eo_do_super(obj, MY_CLASS, elm_obj_container_content_set(part, content, &int_ret));
160 if (!int_ret) return;
162 _icon_signal_emit(obj);
164 if (ret) *ret = EINA_TRUE;
168 _elm_button_smart_event(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
170 Evas_Object *src = va_arg(*list, Evas_Object *);
172 Evas_Callback_Type type = va_arg(*list, Evas_Callback_Type);
173 void *event_info = va_arg(*list, void *);
174 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
175 Evas_Event_Key_Down *ev = event_info;
176 if (ret) *ret = EINA_FALSE;
178 if (elm_widget_disabled_get(obj)) return;
180 if (type != EVAS_CALLBACK_KEY_DOWN) return;
181 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
183 if ((strcmp(ev->keyname, "Return")) &&
184 (strcmp(ev->keyname, "KP_Enter")) &&
185 (strcmp(ev->keyname, "space")))
189 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
190 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
192 if (ret) *ret = EINA_TRUE;
196 _on_clicked_signal(void *data,
197 Evas_Object *obj __UNUSED__,
198 const char *emission __UNUSED__,
199 const char *source __UNUSED__)
205 _autorepeat_send(void *data)
207 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
209 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
213 return ECORE_CALLBACK_CANCEL;
216 return ECORE_CALLBACK_RENEW;
220 _autorepeat_initial_send(void *data)
222 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
224 if (sd->timer) ecore_timer_del(sd->timer);
225 sd->repeating = EINA_TRUE;
226 _autorepeat_send(data);
227 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
229 return ECORE_CALLBACK_CANCEL;
233 _on_pressed_signal(void *data,
234 Evas_Object *obj __UNUSED__,
235 const char *emission __UNUSED__,
236 const char *source __UNUSED__)
238 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
240 if ((sd->autorepeat) && (!sd->repeating))
242 if (sd->ar_threshold <= 0.0)
243 _autorepeat_initial_send(data); /* call immediately */
245 sd->timer = ecore_timer_add
246 (sd->ar_threshold, _autorepeat_initial_send, data);
249 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
253 _on_unpressed_signal(void *data,
254 Evas_Object *obj __UNUSED__,
255 const char *emission __UNUSED__,
256 const char *source __UNUSED__)
258 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
262 ecore_timer_del(sd->timer);
265 sd->repeating = EINA_FALSE;
266 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
270 _access_info_cb(void *data __UNUSED__, Evas_Object *obj)
272 const char *txt = elm_widget_access_info_get(obj);
274 if (!txt) txt = elm_layout_text_get(obj, NULL);
275 if (txt) return strdup(txt);
281 _access_state_cb(void *data __UNUSED__, Evas_Object *obj)
283 if (elm_widget_disabled_get(obj))
284 return strdup(E_("State: Disabled"));
290 _elm_button_smart_add(Eo *obj, void *_pd, va_list *list EINA_UNUSED)
292 Elm_Button_Smart_Data *priv = _pd;
293 Elm_Widget_Smart_Data *wd = eo_data_get(obj, ELM_OBJ_WIDGET_CLASS);
295 eo_do_super(obj, MY_CLASS, evas_obj_smart_add());
297 Evas_Object *parent = eo_parent_get(obj);
298 if (!elm_widget_sub_object_add(parent, obj))
299 ERR("could not add %p as sub object of %p", obj, parent);
301 edje_object_signal_callback_add
302 (wd->resize_obj, "elm,action,click", "",
303 _on_clicked_signal, obj);
304 edje_object_signal_callback_add
305 (wd->resize_obj, "elm,action,press", "",
306 _on_pressed_signal, obj);
307 edje_object_signal_callback_add
308 (wd->resize_obj, "elm,action,unpress", "",
309 _on_unpressed_signal, obj);
311 _elm_access_object_register(obj, wd->resize_obj);
313 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
314 _elm_access_callback_set
315 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
316 _elm_access_callback_set
317 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
319 elm_widget_can_focus_set(obj, EINA_TRUE);
320 if (!elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj)))
321 CRITICAL("Failed to set layout!");
325 _elm_button_smart_content_aliases_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
327 const Elm_Layout_Part_Alias_Description **aliases = va_arg(*list, const Elm_Layout_Part_Alias_Description **);
328 *aliases = _content_aliases;
332 _elm_button_smart_text_aliases_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
334 const Elm_Layout_Part_Alias_Description **aliases = va_arg(*list, const Elm_Layout_Part_Alias_Description **);
335 *aliases = _text_aliases;
339 elm_button_add(Evas_Object *parent)
341 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
342 Evas_Object *obj = eo_add(MY_CLASS, parent);
348 _constructor(Eo *obj, void *_pd EINA_UNUSED, va_list *list EINA_UNUSED)
350 eo_do_super(obj, MY_CLASS, eo_constructor());
352 evas_obj_type_set(MY_CLASS_NAME),
353 evas_obj_smart_callbacks_descriptions_set(_smart_callbacks, NULL));
357 elm_button_autorepeat_set(Evas_Object *obj,
360 ELM_BUTTON_CHECK(obj);
361 eo_do(obj, elm_obj_button_autorepeat_set(on));
365 _autorepeat_set(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
367 Eina_Bool on = va_arg(*list, int);
368 Elm_Button_Smart_Data *sd = _pd;
372 ecore_timer_del(sd->timer);
376 sd->repeating = EINA_FALSE;
379 #define _AR_CAPABLE(obj) \
380 (_elm_button_admits_autorepeat_get(obj))
383 _elm_button_admits_autorepeat_get(const Evas_Object *obj)
385 Eina_Bool ret = EINA_FALSE;
386 eo_do((Eo *) obj, elm_obj_button_admits_autorepeat_get(&ret));
391 _admits_autorepeat_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
393 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
398 elm_button_autorepeat_get(const Evas_Object *obj)
400 ELM_BUTTON_CHECK(obj) EINA_FALSE;
401 Eina_Bool ret = EINA_FALSE;
402 eo_do((Eo *) obj, elm_obj_button_autorepeat_get(&ret));
407 _autorepeat_get(Eo *obj, void *_pd, va_list *list)
409 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
410 Elm_Button_Smart_Data *sd = _pd;
411 *ret = _AR_CAPABLE(obj) & sd->autorepeat;
415 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
418 ELM_BUTTON_CHECK(obj);
419 eo_do(obj, elm_obj_button_autorepeat_initial_timeout_set(t));
423 _autorepeat_initial_timeout_set(Eo *obj, void *_pd, va_list *list)
425 double t = va_arg(*list, double);
426 Elm_Button_Smart_Data *sd = _pd;
428 if (!_AR_CAPABLE(obj))
430 ERR("this widget does not support auto repetition of clicks.");
434 if (sd->ar_threshold == t) return;
437 ecore_timer_del(sd->timer);
440 sd->ar_threshold = t;
444 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
446 ELM_BUTTON_CHECK(obj) 0.0;
448 eo_do((Eo *) obj, elm_obj_button_autorepeat_initial_timeout_get(&ret));
453 _autorepeat_initial_timeout_get(Eo *obj, void *_pd, va_list *list)
455 double *ret = va_arg(*list, double *);
456 Elm_Button_Smart_Data *sd = _pd;
458 if (!_AR_CAPABLE(obj))
461 *ret = sd->ar_threshold;
465 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
468 ELM_BUTTON_CHECK(obj);
469 eo_do(obj, elm_obj_button_autorepeat_gap_timeout_set(t));
473 _autorepeat_gap_timeout_set(Eo *obj, void *_pd, va_list *list)
475 double t = va_arg(*list, double);
476 Elm_Button_Smart_Data *sd = _pd;
478 if (!_AR_CAPABLE(obj))
480 ERR("this widget does not support auto repetition of clicks.");
484 if (sd->ar_interval == t) return;
487 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
491 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
493 ELM_BUTTON_CHECK(obj) 0.0;
495 eo_do((Eo *) obj, elm_obj_button_autorepeat_gap_timeout_get(&ret));
500 _autorepeat_gap_timeout_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
502 double *ret = va_arg(*list, double *);
503 Elm_Button_Smart_Data *sd = _pd;
505 *ret = sd->ar_interval;
509 _elm_button_smart_focus_next_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
511 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
516 _elm_button_smart_focus_direction_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
518 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
523 _class_constructor(Eo_Class *klass)
525 const Eo_Op_Func_Description func_desc[] = {
526 EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor),
528 EO_OP_FUNC(EVAS_OBJ_SMART_ID(EVAS_OBJ_SMART_SUB_ID_ADD), _elm_button_smart_add),
530 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_THEME), _elm_button_smart_theme),
531 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_EVENT), _elm_button_smart_event),
532 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_SUB_OBJECT_DEL), _elm_button_smart_sub_object_del),
533 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_ACTIVATE), _elm_button_smart_activate),
534 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_FOCUS_NEXT_MANAGER_IS), _elm_button_smart_focus_next_manager_is),
535 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_FOCUS_DIRECTION_MANAGER_IS), _elm_button_smart_focus_direction_manager_is),
537 EO_OP_FUNC(ELM_OBJ_CONTAINER_ID(ELM_OBJ_CONTAINER_SUB_ID_CONTENT_SET), _elm_button_smart_content_set),
539 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_SIZING_EVAL), _elm_button_smart_sizing_eval),
540 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_CONTENT_ALIASES_GET), _elm_button_smart_content_aliases_get),
541 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_TEXT_ALIASES_GET), _elm_button_smart_text_aliases_get),
543 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_ADMITS_AUTOREPEAT_GET), _admits_autorepeat_get),
544 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_SET), _autorepeat_set),
545 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GET), _autorepeat_get),
546 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_SET), _autorepeat_initial_timeout_set),
547 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_GET), _autorepeat_initial_timeout_get),
548 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_SET), _autorepeat_gap_timeout_set),
549 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_GET), _autorepeat_gap_timeout_get),
552 eo_class_funcs_set(klass, func_desc);
554 evas_smart_legacy_type_register(MY_CLASS_NAME, klass);
557 static const Eo_Op_Description op_desc[] = {
558 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_ADMITS_AUTOREPEAT_GET, "Get whether auto-repetition is implemented or not"),
559 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_SET, "Turn on/off the autorepeat event generated when the button is kept pressed."),
560 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GET, "Get whether the autorepeat feature is enabled."),
561 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_SET, "Set the initial timeout before the autorepeat event is generated."),
562 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_GET, "Get the initial timeout before the autorepeat event is generated."),
563 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_SET, "Set the interval between each generated autorepeat event."),
564 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_GET, "Get the interval between each generated autorepeat event."),
565 EO_OP_DESCRIPTION_SENTINEL
568 static const Eo_Class_Description class_desc = {
571 EO_CLASS_TYPE_REGULAR,
572 EO_CLASS_DESCRIPTION_OPS(&ELM_OBJ_BUTTON_BASE_ID, op_desc, ELM_OBJ_BUTTON_SUB_ID_LAST),
574 sizeof(Elm_Button_Smart_Data),
579 EO_DEFINE_CLASS(elm_obj_button_class_get, &class_desc, ELM_OBJ_LAYOUT_CLASS, EVAS_SMART_CLICKABLE_INTERFACE, NULL);