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.content"},
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);
106 /* FIXME: replicated from elm_layout just because button's icon spot
107 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
108 * can changed the theme API */
110 _icon_signal_emit(Evas_Object *obj)
114 snprintf(buf, sizeof(buf), "elm,state,icon,%s",
115 elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
117 elm_layout_signal_emit(obj, buf, "elm");
120 /* FIXME: replicated from elm_layout just because button's icon spot
121 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
122 * can changed the theme API */
124 _elm_button_smart_theme(Evas_Object *obj)
126 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->theme(obj)) return EINA_FALSE;
128 _icon_signal_emit(obj);
133 /* FIXME: replicated from elm_layout just because button's icon spot
134 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
135 * can changed the theme API */
137 _elm_button_smart_sub_object_del(Evas_Object *obj,
140 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->sub_object_del(obj, sobj))
143 _icon_signal_emit(obj);
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(Evas_Object *obj,
154 Evas_Object *content)
156 if (!ELM_CONTAINER_CLASS(_elm_button_parent_sc)->content_set
157 (obj, part, content))
160 _icon_signal_emit(obj);
166 _elm_button_smart_event(Evas_Object *obj,
167 Evas_Object *src __UNUSED__,
168 Evas_Callback_Type type,
171 Evas_Event_Key_Down *ev = event_info;
173 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
175 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
176 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
178 if ((strcmp(ev->keyname, "Return")) &&
179 (strcmp(ev->keyname, "KP_Enter")) &&
180 (strcmp(ev->keyname, "space")))
184 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
185 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
191 _on_clicked_signal(void *data,
192 Evas_Object *obj __UNUSED__,
193 const char *emission __UNUSED__,
194 const char *source __UNUSED__)
200 _autorepeat_send(void *data)
202 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
204 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
208 return ECORE_CALLBACK_CANCEL;
211 return ECORE_CALLBACK_RENEW;
215 _autorepeat_initial_send(void *data)
217 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
219 if (sd->timer) ecore_timer_del(sd->timer);
220 sd->repeating = EINA_TRUE;
221 _autorepeat_send(data);
222 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
224 return ECORE_CALLBACK_CANCEL;
228 _on_pressed_signal(void *data,
229 Evas_Object *obj __UNUSED__,
230 const char *emission __UNUSED__,
231 const char *source __UNUSED__)
233 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
235 if ((sd->autorepeat) && (!sd->repeating))
237 if (sd->ar_threshold <= 0.0)
238 _autorepeat_initial_send(data); /* call immediately */
240 sd->timer = ecore_timer_add
241 (sd->ar_threshold, _autorepeat_initial_send, data);
244 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
248 _on_unpressed_signal(void *data,
249 Evas_Object *obj __UNUSED__,
250 const char *emission __UNUSED__,
251 const char *source __UNUSED__)
253 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
257 ecore_timer_del(sd->timer);
260 sd->repeating = EINA_FALSE;
261 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
265 _access_info_cb(void *data __UNUSED__,
267 Elm_Widget_Item *item __UNUSED__)
269 const char *txt = elm_widget_access_info_get(obj);
271 if (!txt) txt = elm_layout_text_get(obj, NULL);
272 if (txt) return strdup(txt);
278 _access_state_cb(void *data __UNUSED__,
280 Elm_Widget_Item *item __UNUSED__)
282 if (elm_widget_disabled_get(obj))
283 return strdup(E_("State: Disabled"));
289 _elm_button_smart_add(Evas_Object *obj)
291 EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
293 ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
295 edje_object_signal_callback_add
296 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,click", "",
297 _on_clicked_signal, obj);
298 edje_object_signal_callback_add
299 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,press", "",
300 _on_pressed_signal, obj);
301 edje_object_signal_callback_add
302 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,unpress", "",
303 _on_unpressed_signal, obj);
305 _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
307 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
308 _elm_access_callback_set
309 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
310 _elm_access_callback_set
311 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
313 elm_widget_can_focus_set(obj, EINA_TRUE);
315 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
319 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
321 ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
323 ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
324 ELM_WIDGET_CLASS(sc)->theme = _elm_button_smart_theme;
325 ELM_WIDGET_CLASS(sc)->sub_object_del = _elm_button_smart_sub_object_del;
327 /* not a 'focus chain manager' */
328 ELM_WIDGET_CLASS(sc)->focus_next = NULL;
329 ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
331 ELM_CONTAINER_CLASS(sc)->content_set = _elm_button_smart_content_set;
333 ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
335 ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
336 ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
339 EAPI const Elm_Button_Smart_Class *
340 elm_button_smart_class_get(void)
342 static Elm_Button_Smart_Class _sc =
343 ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(BUTTON_SMART_NAME);
344 static const Elm_Button_Smart_Class *class = NULL;
345 Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
347 if (class) return class;
349 _elm_button_smart_set(&_sc);
350 esc->callbacks = _smart_callbacks;
356 /* new class here just to take advantage of
357 * ELM_BUTTON_SMART_CLASS_INIT() macro, which sets auto-repeat capability */
358 EVAS_SMART_SUBCLASS_NEW
359 (BUTTON_SMART_NAME, _elm_button_widget, Elm_Button_Smart_Class,
360 Elm_Button_Smart_Class, elm_button_smart_class_get, NULL);
363 _elm_button_widget_smart_set_user(Elm_Button_Smart_Class *sc __UNUSED__)
365 /* NOP: declared because it's obligatory for Evas macro */
369 elm_button_add(Evas_Object *parent)
374 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
376 e = evas_object_evas_get(parent);
379 obj = evas_object_smart_add(e, _elm_button_widget_smart_class_new());
381 if (!elm_widget_sub_object_add(parent, obj))
382 ERR("could not add %p as sub object of %p", obj, parent);
388 elm_button_autorepeat_set(Evas_Object *obj,
391 ELM_BUTTON_CHECK(obj);
392 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
396 ecore_timer_del(sd->timer);
400 sd->repeating = EINA_FALSE;
403 #define _AR_CAPABLE(_sd) \
404 (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
407 elm_button_autorepeat_get(const Evas_Object *obj)
409 ELM_BUTTON_CHECK(obj) EINA_FALSE;
410 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
412 return _AR_CAPABLE(sd) & sd->autorepeat;
416 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
419 ELM_BUTTON_CHECK(obj);
420 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
422 if (!_AR_CAPABLE(sd))
424 ERR("this widget does not support auto repetition of clicks.");
428 if (sd->ar_threshold == t) return;
431 ecore_timer_del(sd->timer);
434 sd->ar_threshold = t;
438 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
440 ELM_BUTTON_CHECK(obj) 0.0;
441 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
443 if (!_AR_CAPABLE(sd)) return 0.0;
445 return sd->ar_threshold;
449 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
452 ELM_BUTTON_CHECK(obj);
453 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
455 if (!_AR_CAPABLE(sd))
457 ERR("this widget does not support auto repetition of clicks.");
461 if (sd->ar_interval == t) return;
464 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
468 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
470 ELM_BUTTON_CHECK(obj) 0.0;
471 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
473 return sd->ar_interval;