1 #include <Elementary.h>
3 #include "elm_widget_button.h"
5 static const char BUTTON_SMART_NAME[] = "elm_button";
7 static const char SIG_CLICKED[] = "clicked";
8 static const char SIG_REPEATED[] = "repeated";
9 static const char SIG_PRESSED[] = "pressed";
10 static const char SIG_UNPRESSED[] = "unpressed";
12 #define ELM_BUTTON_DATA_GET(o, sd) \
13 Elm_Button_Smart_Data * sd = evas_object_smart_data_get(o)
15 #define ELM_BUTTON_DATA_GET_OR_RETURN(o, ptr) \
16 ELM_BUTTON_DATA_GET(o, ptr); \
19 CRITICAL("No widget data for object %p (%s)", \
20 o, evas_object_type_get(o)); \
24 #define ELM_BUTTON_DATA_GET_OR_RETURN_VAL(o, ptr, val) \
25 ELM_BUTTON_DATA_GET(o, ptr); \
28 CRITICAL("No widget data for object %p (%s)", \
29 o, evas_object_type_get(o)); \
33 #define ELM_BUTTON_CHECK(obj) \
34 if (!obj || !elm_widget_type_check((obj), BUTTON_SMART_NAME, __func__)) \
37 static const Elm_Layout_Part_Alias_Description _content_aliases[] =
39 {"icon", "elm.swallow.icon"},
43 static const Elm_Layout_Part_Alias_Description _text_aliases[] =
45 {"default", "elm.text"},
49 /* no *direct* instantiation of this class, so far */
50 __UNUSED__ static Evas_Smart *_elm_button_smart_class_new(void);
52 /* smart callbacks coming from elm button objects (besides the ones
53 * coming from elm layout): */
54 static const Evas_Smart_Cb_Description _smart_callbacks[] = {
62 /* Inheriting from elm_layout. Besides, we need no more than what is
64 EVAS_SMART_SUBCLASS_NEW
65 (BUTTON_SMART_NAME, _elm_button, Elm_Button_Smart_Class,
66 Elm_Layout_Smart_Class, elm_layout_smart_class_get, _smart_callbacks);
69 _activate(Evas_Object *obj)
71 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
75 ecore_timer_del(sd->timer);
79 sd->repeating = EINA_FALSE;
81 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
82 (_elm_access_2nd_click_timeout(obj)))
84 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
85 _elm_access_say(E_("Clicked"));
86 if (!elm_widget_disabled_get(obj) &&
87 !evas_object_freeze_events_get(obj))
88 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
93 _elm_button_smart_sizing_eval(Evas_Object *obj)
95 Evas_Coord minw = -1, minh = -1;
97 ELM_BUTTON_DATA_GET(obj, sd);
99 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
100 edje_object_size_min_restricted_calc
101 (ELM_WIDGET_DATA(sd)->resize_obj, &minw, &minh, minw, minh);
102 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
103 evas_object_size_hint_min_set(obj, minw, minh);
107 _elm_button_smart_event(Evas_Object *obj,
108 Evas_Object *src __UNUSED__,
109 Evas_Callback_Type type,
112 Evas_Event_Key_Down *ev = event_info;
114 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
116 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
117 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
119 if ((strcmp(ev->keyname, "Return")) &&
120 (strcmp(ev->keyname, "KP_Enter")) &&
121 (strcmp(ev->keyname, "space")))
125 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
126 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
132 _on_clicked_signal(void *data,
133 Evas_Object *obj __UNUSED__,
134 const char *emission __UNUSED__,
135 const char *source __UNUSED__)
141 _autorepeat_send(void *data)
143 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
145 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
149 return ECORE_CALLBACK_CANCEL;
152 return ECORE_CALLBACK_RENEW;
156 _autorepeat_initial_send(void *data)
158 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
160 if (sd->timer) ecore_timer_del(sd->timer);
161 sd->repeating = EINA_TRUE;
162 _autorepeat_send(data);
163 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
165 return ECORE_CALLBACK_CANCEL;
169 _on_pressed_signal(void *data,
170 Evas_Object *obj __UNUSED__,
171 const char *emission __UNUSED__,
172 const char *source __UNUSED__)
174 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
176 if ((sd->autorepeat) && (!sd->repeating))
178 if (sd->ar_threshold <= 0.0)
179 _autorepeat_initial_send(data); /* call immediately */
181 sd->timer = ecore_timer_add
182 (sd->ar_threshold, _autorepeat_initial_send, data);
185 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
189 _on_unpressed_signal(void *data,
190 Evas_Object *obj __UNUSED__,
191 const char *emission __UNUSED__,
192 const char *source __UNUSED__)
194 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
198 ecore_timer_del(sd->timer);
201 sd->repeating = EINA_FALSE;
202 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
206 _access_info_cb(void *data __UNUSED__,
208 Elm_Widget_Item *item __UNUSED__)
210 const char *txt = elm_widget_access_info_get(obj);
212 if (!txt) txt = elm_layout_text_get(obj, NULL);
213 if (txt) return strdup(txt);
219 _access_state_cb(void *data __UNUSED__,
221 Elm_Widget_Item *item __UNUSED__)
223 if (elm_widget_disabled_get(obj))
224 return strdup(E_("State: Disabled"));
230 _elm_button_smart_add(Evas_Object *obj)
232 EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
234 ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
236 edje_object_signal_callback_add
237 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,click", "",
238 _on_clicked_signal, obj);
239 edje_object_signal_callback_add
240 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,press", "",
241 _on_pressed_signal, obj);
242 edje_object_signal_callback_add
243 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,unpress", "",
244 _on_unpressed_signal, obj);
246 _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
248 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
249 _elm_access_callback_set
250 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
251 _elm_access_callback_set
252 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
254 elm_widget_can_focus_set(obj, EINA_TRUE);
256 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
260 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
262 ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
264 ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
265 ELM_WIDGET_CLASS(sc)->focus_next = NULL; /* not 'focus chain manager' */
267 ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
269 ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
270 ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
273 EAPI const Elm_Button_Smart_Class *
274 elm_button_smart_class_get(void)
276 static Elm_Button_Smart_Class _sc =
277 ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(BUTTON_SMART_NAME);
278 static const Elm_Button_Smart_Class *class = NULL;
280 if (class) return class;
282 _elm_button_smart_set(&_sc);
288 /* new class here just to take advantage of
289 * ELM_BUTTON_SMART_CLASS_INIT() macro, which sets auto-repeat capability */
290 EVAS_SMART_SUBCLASS_NEW
291 (BUTTON_SMART_NAME, _elm_button_widget, Elm_Button_Smart_Class,
292 Elm_Button_Smart_Class, elm_button_smart_class_get, NULL);
295 _elm_button_widget_smart_set_user(Elm_Button_Smart_Class *sc __UNUSED__)
297 /* NOP: declared because it's obligatory for Evas macro */
301 elm_button_add(Evas_Object *parent)
306 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
308 e = evas_object_evas_get(parent);
311 obj = evas_object_smart_add(e, _elm_button_widget_smart_class_new());
313 if (!elm_widget_sub_object_add(parent, obj))
314 ERR("could not add %p as sub object of %p", obj, parent);
320 elm_button_autorepeat_set(Evas_Object *obj,
323 ELM_BUTTON_CHECK(obj);
324 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
328 ecore_timer_del(sd->timer);
332 sd->repeating = EINA_FALSE;
335 #define _AR_CAPABLE(_sd) \
336 (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
339 elm_button_autorepeat_get(const Evas_Object *obj)
341 ELM_BUTTON_CHECK(obj) EINA_FALSE;
342 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
344 return _AR_CAPABLE(sd) & sd->autorepeat;
348 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
351 ELM_BUTTON_CHECK(obj);
352 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
354 if (!_AR_CAPABLE(sd))
356 ERR("this widget does not support auto repetition of clicks.");
360 if (sd->ar_threshold == t) return;
363 ecore_timer_del(sd->timer);
366 sd->ar_threshold = t;
370 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
372 ELM_BUTTON_CHECK(obj) 0.0;
373 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
375 if (!_AR_CAPABLE(sd)) return 0.0;
377 return sd->ar_threshold;
381 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
384 ELM_BUTTON_CHECK(obj);
385 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
387 if (!_AR_CAPABLE(sd))
389 ERR("this widget does not support auto repetition of clicks.");
393 if (sd->ar_interval == t) return;
396 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
400 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
402 ELM_BUTTON_CHECK(obj) 0.0;
403 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
405 return sd->ar_interval;