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, elm_wdg_theme(&int_ret));
122 if (!int_ret) return;
124 _icon_signal_emit(obj);
126 if (ret) *ret = EINA_TRUE;
129 /* FIXME: replicated from elm_layout just because button's icon spot
130 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
131 * can changed the theme API */
133 _elm_button_smart_sub_object_del(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
135 Evas_Object *sobj = va_arg(*list, Evas_Object *);
136 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
137 if (ret) *ret = EINA_FALSE;
138 Eina_Bool int_ret = EINA_FALSE;
140 eo_do_super(obj, elm_wdg_sub_object_del(sobj, &int_ret));
141 if (!int_ret) return;
143 _icon_signal_emit(obj);
145 if (ret) *ret = EINA_TRUE;
148 /* FIXME: replicated from elm_layout just because button's icon spot
149 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
150 * can changed the theme API */
152 _elm_button_smart_content_set(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
154 const char *part = va_arg(*list, const char *);
155 Evas_Object *content = va_arg(*list, Evas_Object *);
156 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
157 if (ret) *ret = EINA_FALSE;
158 Eina_Bool int_ret = EINA_FALSE;
160 eo_do_super(obj, elm_obj_container_content_set(part, content, &int_ret));
161 if (!int_ret) return;
163 _icon_signal_emit(obj);
165 if (ret) *ret = EINA_TRUE;
169 _elm_button_smart_event(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
171 Evas_Object *src = va_arg(*list, Evas_Object *);
173 Evas_Callback_Type type = va_arg(*list, Evas_Callback_Type);
174 void *event_info = va_arg(*list, void *);
175 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
176 Evas_Event_Key_Down *ev = event_info;
177 if (ret) *ret = EINA_FALSE;
179 if (elm_widget_disabled_get(obj)) return;
181 if (type != EVAS_CALLBACK_KEY_DOWN) return;
182 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
184 if ((strcmp(ev->keyname, "Return")) &&
185 (strcmp(ev->keyname, "KP_Enter")) &&
186 (strcmp(ev->keyname, "space")))
190 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
191 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
193 if (ret) *ret = EINA_TRUE;
197 _on_clicked_signal(void *data,
198 Evas_Object *obj __UNUSED__,
199 const char *emission __UNUSED__,
200 const char *source __UNUSED__)
206 _autorepeat_send(void *data)
208 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
210 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
214 return ECORE_CALLBACK_CANCEL;
217 return ECORE_CALLBACK_RENEW;
221 _autorepeat_initial_send(void *data)
223 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
225 if (sd->timer) ecore_timer_del(sd->timer);
226 sd->repeating = EINA_TRUE;
227 _autorepeat_send(data);
228 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
230 return ECORE_CALLBACK_CANCEL;
234 _on_pressed_signal(void *data,
235 Evas_Object *obj __UNUSED__,
236 const char *emission __UNUSED__,
237 const char *source __UNUSED__)
239 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
241 if ((sd->autorepeat) && (!sd->repeating))
243 if (sd->ar_threshold <= 0.0)
244 _autorepeat_initial_send(data); /* call immediately */
246 sd->timer = ecore_timer_add
247 (sd->ar_threshold, _autorepeat_initial_send, data);
250 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
254 _on_unpressed_signal(void *data,
255 Evas_Object *obj __UNUSED__,
256 const char *emission __UNUSED__,
257 const char *source __UNUSED__)
259 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
263 ecore_timer_del(sd->timer);
266 sd->repeating = EINA_FALSE;
267 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
271 _access_info_cb(void *data __UNUSED__, Evas_Object *obj)
273 const char *txt = elm_widget_access_info_get(obj);
275 if (!txt) txt = elm_layout_text_get(obj, NULL);
276 if (txt) return strdup(txt);
282 _access_state_cb(void *data __UNUSED__, Evas_Object *obj)
284 if (elm_widget_disabled_get(obj))
285 return strdup(E_("State: Disabled"));
291 _elm_button_smart_add(Eo *obj, void *_pd, va_list *list EINA_UNUSED)
293 Elm_Button_Smart_Data *priv = _pd;
294 Elm_Widget_Smart_Data *wd = eo_data_get(obj, ELM_OBJ_WIDGET_CLASS);
296 eo_do_super(obj, evas_obj_smart_add());
298 edje_object_signal_callback_add
299 (wd->resize_obj, "elm,action,click", "",
300 _on_clicked_signal, obj);
301 edje_object_signal_callback_add
302 (wd->resize_obj, "elm,action,press", "",
303 _on_pressed_signal, obj);
304 edje_object_signal_callback_add
305 (wd->resize_obj, "elm,action,unpress", "",
306 _on_unpressed_signal, obj);
308 _elm_access_object_register(obj, wd->resize_obj);
310 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
311 _elm_access_callback_set
312 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
313 _elm_access_callback_set
314 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
316 elm_widget_can_focus_set(obj, EINA_TRUE);
318 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
322 _elm_button_smart_content_aliases_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
324 const Elm_Layout_Part_Alias_Description **aliases = va_arg(*list, const Elm_Layout_Part_Alias_Description **);
325 *aliases = _content_aliases;
329 _elm_button_smart_text_aliases_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
331 const Elm_Layout_Part_Alias_Description **aliases = va_arg(*list, const Elm_Layout_Part_Alias_Description **);
332 *aliases = _text_aliases;
336 elm_button_add(Evas_Object *parent)
338 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
339 Evas_Object *obj = eo_add(MY_CLASS, parent);
345 _constructor(Eo *obj, void *_pd EINA_UNUSED, va_list *list EINA_UNUSED)
347 eo_do_super(obj, eo_constructor());
349 evas_obj_type_set(MY_CLASS_NAME),
350 evas_obj_smart_callbacks_descriptions_set(_smart_callbacks, NULL));
352 Evas_Object *parent = eo_parent_get(obj);
353 if (!elm_widget_sub_object_add(parent, obj))
354 ERR("could not add %p as sub object of %p", obj, parent);
358 elm_button_autorepeat_set(Evas_Object *obj,
361 ELM_BUTTON_CHECK(obj);
362 eo_do(obj, elm_obj_button_autorepeat_set(on));
366 _autorepeat_set(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
368 Eina_Bool on = va_arg(*list, int);
369 Elm_Button_Smart_Data *sd = _pd;
373 ecore_timer_del(sd->timer);
377 sd->repeating = EINA_FALSE;
380 #define _AR_CAPABLE(obj) \
381 (_elm_button_admits_autorepeat_get(obj))
384 _elm_button_admits_autorepeat_get(const Evas_Object *obj)
386 Eina_Bool ret = EINA_FALSE;
387 eo_do((Eo *) obj, elm_obj_button_admits_autorepeat_get(&ret));
392 _admits_autorepeat_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
394 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
399 elm_button_autorepeat_get(const Evas_Object *obj)
401 ELM_BUTTON_CHECK(obj) EINA_FALSE;
402 Eina_Bool ret = EINA_FALSE;
403 eo_do((Eo *) obj, elm_obj_button_autorepeat_get(&ret));
408 _autorepeat_get(Eo *obj, void *_pd, va_list *list)
410 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
411 Elm_Button_Smart_Data *sd = _pd;
412 *ret = _AR_CAPABLE(obj) & sd->autorepeat;
416 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
419 ELM_BUTTON_CHECK(obj);
420 eo_do(obj, elm_obj_button_autorepeat_initial_timeout_set(t));
424 _autorepeat_initial_timeout_set(Eo *obj, void *_pd, va_list *list)
426 double t = va_arg(*list, double);
427 Elm_Button_Smart_Data *sd = _pd;
429 if (!_AR_CAPABLE(obj))
431 ERR("this widget does not support auto repetition of clicks.");
435 if (sd->ar_threshold == t) return;
438 ecore_timer_del(sd->timer);
441 sd->ar_threshold = t;
445 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
447 ELM_BUTTON_CHECK(obj) 0.0;
449 eo_do((Eo *) obj, elm_obj_button_autorepeat_initial_timeout_get(&ret));
454 _autorepeat_initial_timeout_get(Eo *obj, void *_pd, va_list *list)
456 double *ret = va_arg(*list, double *);
457 Elm_Button_Smart_Data *sd = _pd;
459 if (!_AR_CAPABLE(obj))
462 *ret = sd->ar_threshold;
466 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
469 ELM_BUTTON_CHECK(obj);
470 eo_do(obj, elm_obj_button_autorepeat_gap_timeout_set(t));
474 _autorepeat_gap_timeout_set(Eo *obj, void *_pd, va_list *list)
476 double t = va_arg(*list, double);
477 Elm_Button_Smart_Data *sd = _pd;
479 if (!_AR_CAPABLE(obj))
481 ERR("this widget does not support auto repetition of clicks.");
485 if (sd->ar_interval == t) return;
488 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
492 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
494 ELM_BUTTON_CHECK(obj) 0.0;
496 eo_do((Eo *) obj, elm_obj_button_autorepeat_gap_timeout_get(&ret));
501 _autorepeat_gap_timeout_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
503 double *ret = va_arg(*list, double *);
504 Elm_Button_Smart_Data *sd = _pd;
506 *ret = sd->ar_interval;
510 _elm_button_smart_focus_next_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
512 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
517 _elm_button_smart_focus_direction_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
519 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
524 _class_constructor(Eo_Class *klass)
526 const Eo_Op_Func_Description func_desc[] = {
527 EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor),
529 EO_OP_FUNC(EVAS_OBJ_SMART_ID(EVAS_OBJ_SMART_SUB_ID_ADD), _elm_button_smart_add),
531 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_THEME), _elm_button_smart_theme),
532 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_EVENT), _elm_button_smart_event),
533 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_SUB_OBJECT_DEL), _elm_button_smart_sub_object_del),
534 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_ACTIVATE), _elm_button_smart_activate),
535 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_FOCUS_NEXT_MANAGER_IS), _elm_button_smart_focus_next_manager_is),
536 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_FOCUS_DIRECTION_MANAGER_IS), _elm_button_smart_focus_direction_manager_is),
538 EO_OP_FUNC(ELM_OBJ_CONTAINER_ID(ELM_OBJ_CONTAINER_SUB_ID_CONTENT_SET), _elm_button_smart_content_set),
540 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_SIZING_EVAL), _elm_button_smart_sizing_eval),
541 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_CONTENT_ALIASES_GET), _elm_button_smart_content_aliases_get),
542 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_TEXT_ALIASES_GET), _elm_button_smart_text_aliases_get),
544 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_ADMITS_AUTOREPEAT_GET), _admits_autorepeat_get),
545 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_SET), _autorepeat_set),
546 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GET), _autorepeat_get),
547 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_SET), _autorepeat_initial_timeout_set),
548 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_GET), _autorepeat_initial_timeout_get),
549 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_SET), _autorepeat_gap_timeout_set),
550 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_GET), _autorepeat_gap_timeout_get),
553 eo_class_funcs_set(klass, func_desc);
556 static const Eo_Op_Description op_desc[] = {
557 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_ADMITS_AUTOREPEAT_GET, "Get whether auto-repetition is implemented or not"),
558 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_SET, "Turn on/off the autorepeat event generated when the button is kept pressed."),
559 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GET, "Get whether the autorepeat feature is enabled."),
560 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_SET, "Set the initial timeout before the autorepeat event is generated."),
561 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_GET, "Get the initial timeout before the autorepeat event is generated."),
562 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_SET, "Set the interval between each generated autorepeat event."),
563 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_GET, "Get the interval between each generated autorepeat event."),
564 EO_OP_DESCRIPTION_SENTINEL
567 static const Eo_Class_Description class_desc = {
570 EO_CLASS_TYPE_REGULAR,
571 EO_CLASS_DESCRIPTION_OPS(&ELM_OBJ_BUTTON_BASE_ID, op_desc, ELM_OBJ_BUTTON_SUB_ID_LAST),
573 sizeof(Elm_Button_Smart_Data),
578 EO_DEFINE_CLASS(elm_obj_button_class_get, &class_desc, ELM_OBJ_LAYOUT_CLASS, EVAS_SMART_CLICKABLE_INTERFACE, NULL);