1 #include <Elementary.h>
3 #include "elm_widget_button.h"
4 #include "elm_widget_layout.h"
8 EAPI Eo_Op ELM_OBJ_BUTTON_BASE_ID = EO_NOOP;
10 #define MY_CLASS ELM_OBJ_BUTTON_CLASS
12 #define MY_CLASS_NAME "elm_button"
14 static const char SIG_CLICKED[] = "clicked";
15 static const char SIG_REPEATED[] = "repeated";
16 static const char SIG_PRESSED[] = "pressed";
17 static const char SIG_UNPRESSED[] = "unpressed";
19 static const Elm_Layout_Part_Alias_Description _content_aliases[] =
21 {"icon", "elm.swallow.content"},
25 static const Elm_Layout_Part_Alias_Description _text_aliases[] =
27 {"default", "elm.text"},
31 /* smart callbacks coming from elm button objects (besides the ones
32 * coming from elm layout): */
33 static const Evas_Smart_Cb_Description _smart_callbacks[] = {
42 _activate(Evas_Object *obj)
44 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
48 ecore_timer_del(sd->timer);
52 sd->repeating = EINA_FALSE;
54 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
55 (_elm_access_2nd_click_timeout(obj)))
57 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
58 _elm_access_say(E_("Clicked"));
59 if (!elm_widget_disabled_get(obj) &&
60 !evas_object_freeze_events_get(obj))
61 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
66 _elm_button_smart_sizing_eval(Eo *obj, void *_pd EINA_UNUSED, va_list *list EINA_UNUSED)
68 Elm_Widget_Smart_Data *wd = eo_data_get(obj, ELM_OBJ_WIDGET_CLASS);
69 Evas_Coord minw = -1, minh = -1;
71 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
72 edje_object_size_min_restricted_calc
73 (wd->resize_obj, &minw, &minh, minw, minh);
74 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
75 evas_object_size_hint_min_set(obj, minw, minh);
79 _elm_button_smart_activate(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
81 Elm_Activate act = va_arg(*list, Elm_Activate);
82 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
83 if (ret) *ret = EINA_FALSE;
85 if (act != ELM_ACTIVATE_DEFAULT) return;
87 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
88 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
90 if (ret) *ret = EINA_TRUE;
93 /* FIXME: replicated from elm_layout just because button's icon spot
94 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
95 * can changed the theme API */
97 _icon_signal_emit(Evas_Object *obj)
101 snprintf(buf, sizeof(buf), "elm,state,icon,%s",
102 elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
104 elm_layout_signal_emit(obj, buf, "elm");
105 edje_object_message_signal_process(elm_layout_edje_get(obj));
106 eo_do(obj, elm_obj_layout_sizing_eval());
109 /* FIXME: replicated from elm_layout just because button's icon spot
110 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
111 * can changed the theme API */
113 _elm_button_smart_theme(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
115 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
116 if (ret) *ret = EINA_FALSE;
117 Eina_Bool int_ret = EINA_FALSE;
119 eo_do_super(obj, elm_wdg_theme(&int_ret));
120 if (!int_ret) return;
122 _icon_signal_emit(obj);
124 if (ret) *ret = EINA_TRUE;
127 /* FIXME: replicated from elm_layout just because button's icon spot
128 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
129 * can changed the theme API */
131 _elm_button_smart_sub_object_del(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
133 Evas_Object *sobj = va_arg(*list, Evas_Object *);
134 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
135 if (ret) *ret = EINA_FALSE;
136 Eina_Bool int_ret = EINA_FALSE;
138 eo_do_super(obj, elm_wdg_sub_object_del(sobj, &int_ret));
139 if (!int_ret) return;
141 _icon_signal_emit(obj);
143 if (ret) *ret = EINA_TRUE;
146 /* FIXME: replicated from elm_layout just because button's icon spot
147 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
148 * can changed the theme API */
150 _elm_button_smart_content_set(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
152 const char *part = va_arg(*list, const char *);
153 Evas_Object *content = va_arg(*list, Evas_Object *);
154 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
155 if (ret) *ret = EINA_FALSE;
156 Eina_Bool int_ret = EINA_FALSE;
158 eo_do_super(obj, elm_obj_container_content_set(part, content, &int_ret));
159 if (!int_ret) return;
161 _icon_signal_emit(obj);
163 if (ret) *ret = EINA_TRUE;
167 _elm_button_smart_event(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
169 Evas_Object *src = va_arg(*list, Evas_Object *);
171 Evas_Callback_Type type = va_arg(*list, Evas_Callback_Type);
172 void *event_info = va_arg(*list, void *);
173 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
174 Evas_Event_Key_Down *ev = event_info;
175 if (ret) *ret = EINA_FALSE;
177 if (elm_widget_disabled_get(obj)) return;
179 if (type != EVAS_CALLBACK_KEY_DOWN) return;
180 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
182 if ((strcmp(ev->keyname, "Return")) &&
183 (strcmp(ev->keyname, "KP_Enter")) &&
184 (strcmp(ev->keyname, "space")))
188 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
189 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
191 if (ret) *ret = EINA_TRUE;
195 _on_clicked_signal(void *data,
196 Evas_Object *obj __UNUSED__,
197 const char *emission __UNUSED__,
198 const char *source __UNUSED__)
204 _autorepeat_send(void *data)
206 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
208 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
212 return ECORE_CALLBACK_CANCEL;
215 return ECORE_CALLBACK_RENEW;
219 _autorepeat_initial_send(void *data)
221 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
223 if (sd->timer) ecore_timer_del(sd->timer);
224 sd->repeating = EINA_TRUE;
225 _autorepeat_send(data);
226 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
228 return ECORE_CALLBACK_CANCEL;
232 _on_pressed_signal(void *data,
233 Evas_Object *obj __UNUSED__,
234 const char *emission __UNUSED__,
235 const char *source __UNUSED__)
237 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
239 if ((sd->autorepeat) && (!sd->repeating))
241 if (sd->ar_threshold <= 0.0)
242 _autorepeat_initial_send(data); /* call immediately */
244 sd->timer = ecore_timer_add
245 (sd->ar_threshold, _autorepeat_initial_send, data);
248 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
252 _on_unpressed_signal(void *data,
253 Evas_Object *obj __UNUSED__,
254 const char *emission __UNUSED__,
255 const char *source __UNUSED__)
257 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
261 ecore_timer_del(sd->timer);
264 sd->repeating = EINA_FALSE;
265 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
269 _access_info_cb(void *data __UNUSED__,
271 Elm_Widget_Item *item __UNUSED__)
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__,
284 Elm_Widget_Item *item __UNUSED__)
286 if (elm_widget_disabled_get(obj))
287 return strdup(E_("State: Disabled"));
293 _elm_button_smart_add(Eo *obj, void *_pd, va_list *list EINA_UNUSED)
295 Elm_Button_Smart_Data *priv = _pd;
296 Elm_Widget_Smart_Data *wd = eo_data_get(obj, ELM_OBJ_WIDGET_CLASS);
298 eo_do_super(obj, evas_obj_smart_add());
300 edje_object_signal_callback_add
301 (wd->resize_obj, "elm,action,click", "",
302 _on_clicked_signal, obj);
303 edje_object_signal_callback_add
304 (wd->resize_obj, "elm,action,press", "",
305 _on_pressed_signal, obj);
306 edje_object_signal_callback_add
307 (wd->resize_obj, "elm,action,unpress", "",
308 _on_unpressed_signal, obj);
310 _elm_access_object_register(obj, wd->resize_obj);
312 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
313 _elm_access_callback_set
314 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
315 _elm_access_callback_set
316 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
318 elm_widget_can_focus_set(obj, EINA_TRUE);
320 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
324 _elm_button_smart_content_aliases_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
326 const Elm_Layout_Part_Alias_Description **aliases = va_arg(*list, const Elm_Layout_Part_Alias_Description **);
327 *aliases = _content_aliases;
331 _elm_button_smart_text_aliases_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
333 const Elm_Layout_Part_Alias_Description **aliases = va_arg(*list, const Elm_Layout_Part_Alias_Description **);
334 *aliases = _text_aliases;
338 elm_button_add(Evas_Object *parent)
340 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
341 Evas_Object *obj = eo_add(MY_CLASS, parent);
347 _constructor(Eo *obj, void *_pd EINA_UNUSED, va_list *list EINA_UNUSED)
349 eo_do_super(obj, eo_constructor());
351 evas_obj_type_set(MY_CLASS_NAME),
352 evas_obj_smart_callbacks_descriptions_set(_smart_callbacks, NULL));
354 Evas_Object *parent = eo_parent_get(obj);
355 if (!elm_widget_sub_object_add(parent, obj))
356 ERR("could not add %p as sub object of %p", obj, parent);
360 elm_button_autorepeat_set(Evas_Object *obj,
363 ELM_BUTTON_CHECK(obj);
364 eo_do(obj, elm_obj_button_autorepeat_set(on));
368 _autorepeat_set(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
370 Eina_Bool on = va_arg(*list, int);
371 Elm_Button_Smart_Data *sd = _pd;
375 ecore_timer_del(sd->timer);
379 sd->repeating = EINA_FALSE;
382 #define _AR_CAPABLE(obj) \
383 (_elm_button_admits_autorepeat_get(obj))
386 _elm_button_admits_autorepeat_get(const Evas_Object *obj)
388 Eina_Bool ret = EINA_FALSE;
389 eo_do((Eo *) obj, elm_obj_button_admits_autorepeat_get(&ret));
394 _admits_autorepeat_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
396 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
401 elm_button_autorepeat_get(const Evas_Object *obj)
403 ELM_BUTTON_CHECK(obj) EINA_FALSE;
404 Eina_Bool ret = EINA_FALSE;
405 eo_do((Eo *) obj, elm_obj_button_autorepeat_get(&ret));
410 _autorepeat_get(Eo *obj, void *_pd, va_list *list)
412 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
413 Elm_Button_Smart_Data *sd = _pd;
414 *ret = _AR_CAPABLE(obj) & sd->autorepeat;
418 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
421 ELM_BUTTON_CHECK(obj);
422 eo_do(obj, elm_obj_button_autorepeat_initial_timeout_set(t));
426 _autorepeat_initial_timeout_set(Eo *obj, void *_pd, va_list *list)
428 double t = va_arg(*list, double);
429 Elm_Button_Smart_Data *sd = _pd;
431 if (!_AR_CAPABLE(obj))
433 ERR("this widget does not support auto repetition of clicks.");
437 if (sd->ar_threshold == t) return;
440 ecore_timer_del(sd->timer);
443 sd->ar_threshold = t;
447 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
449 ELM_BUTTON_CHECK(obj) 0.0;
451 eo_do((Eo *) obj, elm_obj_button_autorepeat_initial_timeout_get(&ret));
456 _autorepeat_initial_timeout_get(Eo *obj, void *_pd, va_list *list)
458 double *ret = va_arg(*list, double *);
459 Elm_Button_Smart_Data *sd = _pd;
461 if (!_AR_CAPABLE(obj))
464 *ret = sd->ar_threshold;
468 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
471 ELM_BUTTON_CHECK(obj);
472 eo_do(obj, elm_obj_button_autorepeat_gap_timeout_set(t));
476 _autorepeat_gap_timeout_set(Eo *obj, void *_pd, va_list *list)
478 double t = va_arg(*list, double);
479 Elm_Button_Smart_Data *sd = _pd;
481 if (!_AR_CAPABLE(obj))
483 ERR("this widget does not support auto repetition of clicks.");
487 if (sd->ar_interval == t) return;
490 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
494 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
496 ELM_BUTTON_CHECK(obj) 0.0;
498 eo_do((Eo *) obj, elm_obj_button_autorepeat_gap_timeout_get(&ret));
503 _autorepeat_gap_timeout_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
505 double *ret = va_arg(*list, double *);
506 Elm_Button_Smart_Data *sd = _pd;
508 *ret = sd->ar_interval;
512 _elm_button_smart_focus_next_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
514 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
519 _elm_button_smart_focus_direction_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
521 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
526 _class_constructor(Eo_Class *klass)
528 const Eo_Op_Func_Description func_desc[] = {
529 EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor),
531 EO_OP_FUNC(EVAS_OBJ_SMART_ID(EVAS_OBJ_SMART_SUB_ID_ADD), _elm_button_smart_add),
533 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_THEME), _elm_button_smart_theme),
534 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_EVENT), _elm_button_smart_event),
535 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_SUB_OBJECT_DEL), _elm_button_smart_sub_object_del),
536 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_ACTIVATE), _elm_button_smart_activate),
537 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_FOCUS_NEXT_MANAGER_IS), _elm_button_smart_focus_next_manager_is),
538 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_FOCUS_DIRECTION_MANAGER_IS), _elm_button_smart_focus_direction_manager_is),
540 EO_OP_FUNC(ELM_OBJ_CONTAINER_ID(ELM_OBJ_CONTAINER_SUB_ID_CONTENT_SET), _elm_button_smart_content_set),
542 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_SIZING_EVAL), _elm_button_smart_sizing_eval),
543 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_CONTENT_ALIASES_GET), _elm_button_smart_content_aliases_get),
544 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_TEXT_ALIASES_GET), _elm_button_smart_text_aliases_get),
546 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_ADMITS_AUTOREPEAT_GET), _admits_autorepeat_get),
547 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_SET), _autorepeat_set),
548 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GET), _autorepeat_get),
549 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_SET), _autorepeat_initial_timeout_set),
550 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_GET), _autorepeat_initial_timeout_get),
551 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_SET), _autorepeat_gap_timeout_set),
552 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_GET), _autorepeat_gap_timeout_get),
555 eo_class_funcs_set(klass, func_desc);
558 static const Eo_Op_Description op_desc[] = {
559 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_ADMITS_AUTOREPEAT_GET, "Get whether auto-repetition is implemented or not"),
560 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_SET, "Turn on/off the autorepeat event generated when the button is kept pressed."),
561 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GET, "Get whether the autorepeat feature is enabled."),
562 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_SET, "Set the initial timeout before the autorepeat event is generated."),
563 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_GET, "Get the initial timeout before the autorepeat event is generated."),
564 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_SET, "Set the interval between each generated autorepeat event."),
565 EO_OP_DESCRIPTION(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_GET, "Get the interval between each generated autorepeat event."),
566 EO_OP_DESCRIPTION_SENTINEL
569 static const Eo_Class_Description class_desc = {
572 EO_CLASS_TYPE_REGULAR,
573 EO_CLASS_DESCRIPTION_OPS(&ELM_OBJ_BUTTON_BASE_ID, op_desc, ELM_OBJ_BUTTON_SUB_ID_LAST),
575 sizeof(Elm_Button_Smart_Data),
580 EO_DEFINE_CLASS(elm_obj_button_class_get, &class_desc, ELM_OBJ_LAYOUT_CLASS, EVAS_SMART_CLICKABLE_INTERFACE, NULL);