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;
120 eo_do_super(obj, elm_wdg_theme(&int_ret));
121 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__, Evas_Object *obj)
271 const char *txt = elm_widget_access_info_get(obj);
273 if (!txt) txt = elm_layout_text_get(obj, NULL);
274 if (txt) return strdup(txt);
280 _access_state_cb(void *data __UNUSED__, Evas_Object *obj)
282 if (elm_widget_disabled_get(obj))
283 return strdup(E_("State: Disabled"));
289 _elm_button_smart_add(Eo *obj, void *_pd, va_list *list EINA_UNUSED)
291 Elm_Button_Smart_Data *priv = _pd;
292 Elm_Widget_Smart_Data *wd = eo_data_get(obj, ELM_OBJ_WIDGET_CLASS);
294 eo_do_super(obj, evas_obj_smart_add());
296 edje_object_signal_callback_add
297 (wd->resize_obj, "elm,action,click", "",
298 _on_clicked_signal, obj);
299 edje_object_signal_callback_add
300 (wd->resize_obj, "elm,action,press", "",
301 _on_pressed_signal, obj);
302 edje_object_signal_callback_add
303 (wd->resize_obj, "elm,action,unpress", "",
304 _on_unpressed_signal, obj);
306 _elm_access_object_register(obj, wd->resize_obj);
308 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
309 _elm_access_callback_set
310 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
311 _elm_access_callback_set
312 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
314 elm_widget_can_focus_set(obj, EINA_TRUE);
318 _elm_button_smart_content_aliases_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
320 const Elm_Layout_Part_Alias_Description **aliases = va_arg(*list, const Elm_Layout_Part_Alias_Description **);
321 *aliases = _content_aliases;
325 _elm_button_smart_text_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 = _text_aliases;
332 elm_button_add(Evas_Object *parent)
334 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
335 Evas_Object *obj = eo_add(MY_CLASS, parent);
341 _constructor(Eo *obj, void *_pd EINA_UNUSED, va_list *list EINA_UNUSED)
343 eo_do_super(obj, eo_constructor());
345 evas_obj_type_set(MY_CLASS_NAME),
346 evas_obj_smart_callbacks_descriptions_set(_smart_callbacks, NULL));
348 Evas_Object *parent = eo_parent_get(obj);
349 if (!elm_widget_sub_object_add(parent, obj))
350 ERR("could not add %p as sub object of %p", obj, parent);
352 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
356 elm_button_autorepeat_set(Evas_Object *obj,
359 ELM_BUTTON_CHECK(obj);
360 eo_do(obj, elm_obj_button_autorepeat_set(on));
364 _autorepeat_set(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
366 Eina_Bool on = va_arg(*list, int);
367 Elm_Button_Smart_Data *sd = _pd;
371 ecore_timer_del(sd->timer);
375 sd->repeating = EINA_FALSE;
378 #define _AR_CAPABLE(obj) \
379 (_elm_button_admits_autorepeat_get(obj))
382 _elm_button_admits_autorepeat_get(const Evas_Object *obj)
384 Eina_Bool ret = EINA_FALSE;
385 eo_do((Eo *) obj, elm_obj_button_admits_autorepeat_get(&ret));
390 _admits_autorepeat_get(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
392 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
397 elm_button_autorepeat_get(const Evas_Object *obj)
399 ELM_BUTTON_CHECK(obj) EINA_FALSE;
400 Eina_Bool ret = EINA_FALSE;
401 eo_do((Eo *) obj, elm_obj_button_autorepeat_get(&ret));
406 _autorepeat_get(Eo *obj, void *_pd, va_list *list)
408 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
409 Elm_Button_Smart_Data *sd = _pd;
410 *ret = _AR_CAPABLE(obj) & sd->autorepeat;
414 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
417 ELM_BUTTON_CHECK(obj);
418 eo_do(obj, elm_obj_button_autorepeat_initial_timeout_set(t));
422 _autorepeat_initial_timeout_set(Eo *obj, void *_pd, va_list *list)
424 double t = va_arg(*list, double);
425 Elm_Button_Smart_Data *sd = _pd;
427 if (!_AR_CAPABLE(obj))
429 ERR("this widget does not support auto repetition of clicks.");
433 if (sd->ar_threshold == t) return;
436 ecore_timer_del(sd->timer);
439 sd->ar_threshold = t;
443 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
445 ELM_BUTTON_CHECK(obj) 0.0;
447 eo_do((Eo *) obj, elm_obj_button_autorepeat_initial_timeout_get(&ret));
452 _autorepeat_initial_timeout_get(Eo *obj, void *_pd, va_list *list)
454 double *ret = va_arg(*list, double *);
455 Elm_Button_Smart_Data *sd = _pd;
457 if (!_AR_CAPABLE(obj))
460 *ret = sd->ar_threshold;
464 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
467 ELM_BUTTON_CHECK(obj);
468 eo_do(obj, elm_obj_button_autorepeat_gap_timeout_set(t));
472 _autorepeat_gap_timeout_set(Eo *obj, void *_pd, va_list *list)
474 double t = va_arg(*list, double);
475 Elm_Button_Smart_Data *sd = _pd;
477 if (!_AR_CAPABLE(obj))
479 ERR("this widget does not support auto repetition of clicks.");
483 if (sd->ar_interval == t) return;
486 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
490 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
492 ELM_BUTTON_CHECK(obj) 0.0;
494 eo_do((Eo *) obj, elm_obj_button_autorepeat_gap_timeout_get(&ret));
499 _autorepeat_gap_timeout_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
501 double *ret = va_arg(*list, double *);
502 Elm_Button_Smart_Data *sd = _pd;
504 *ret = sd->ar_interval;
508 _elm_button_smart_focus_next_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
510 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
515 _elm_button_smart_focus_direction_manager_is(Eo *obj EINA_UNUSED, void *_pd EINA_UNUSED, va_list *list)
517 Eina_Bool *ret = va_arg(*list, Eina_Bool *);
522 _class_constructor(Eo_Class *klass)
524 const Eo_Op_Func_Description func_desc[] = {
525 EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor),
527 EO_OP_FUNC(EVAS_OBJ_SMART_ID(EVAS_OBJ_SMART_SUB_ID_ADD), _elm_button_smart_add),
529 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_THEME), _elm_button_smart_theme),
530 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_EVENT), _elm_button_smart_event),
531 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_SUB_OBJECT_DEL), _elm_button_smart_sub_object_del),
532 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_ACTIVATE), _elm_button_smart_activate),
533 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_FOCUS_NEXT_MANAGER_IS), _elm_button_smart_focus_next_manager_is),
534 EO_OP_FUNC(ELM_WIDGET_ID(ELM_WIDGET_SUB_ID_FOCUS_DIRECTION_MANAGER_IS), _elm_button_smart_focus_direction_manager_is),
536 EO_OP_FUNC(ELM_OBJ_CONTAINER_ID(ELM_OBJ_CONTAINER_SUB_ID_CONTENT_SET), _elm_button_smart_content_set),
538 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_SIZING_EVAL), _elm_button_smart_sizing_eval),
539 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_CONTENT_ALIASES_GET), _elm_button_smart_content_aliases_get),
540 EO_OP_FUNC(ELM_OBJ_LAYOUT_ID(ELM_OBJ_LAYOUT_SUB_ID_TEXT_ALIASES_GET), _elm_button_smart_text_aliases_get),
542 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_ADMITS_AUTOREPEAT_GET), _admits_autorepeat_get),
543 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_SET), _autorepeat_set),
544 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GET), _autorepeat_get),
545 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_SET), _autorepeat_initial_timeout_set),
546 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_INITIAL_TIMEOUT_GET), _autorepeat_initial_timeout_get),
547 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_SET), _autorepeat_gap_timeout_set),
548 EO_OP_FUNC(ELM_OBJ_BUTTON_ID(ELM_OBJ_BUTTON_SUB_ID_AUTOREPEAT_GAP_TIMEOUT_GET), _autorepeat_gap_timeout_get),
551 eo_class_funcs_set(klass, func_desc);
553 evas_smart_legacy_type_register(MY_CLASS_NAME, klass);
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);