1 #include <Elementary.h>
3 #include "elm_widget_button.h"
5 EAPI const char ELM_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 static const Elm_Layout_Part_Alias_Description _content_aliases[] =
14 {"icon", "elm.swallow.content"},
18 static const Elm_Layout_Part_Alias_Description _text_aliases[] =
20 {"default", "elm.text"},
24 /* smart callbacks coming from elm button objects (besides the ones
25 * coming from elm layout): */
26 static const Evas_Smart_Cb_Description _smart_callbacks[] = {
34 /* Inheriting from elm_layout. Besides, we need no more than what is
36 EVAS_SMART_SUBCLASS_NEW
37 (ELM_BUTTON_SMART_NAME, _elm_button, Elm_Button_Smart_Class,
38 Elm_Layout_Smart_Class, elm_layout_smart_class_get, _smart_callbacks);
41 _activate(Evas_Object *obj)
43 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
47 ecore_timer_del(sd->timer);
51 sd->repeating = EINA_FALSE;
53 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
54 (_elm_access_2nd_click_timeout(obj)))
56 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
57 _elm_access_say(E_("Clicked"));
58 if (!elm_widget_disabled_get(obj) &&
59 !evas_object_freeze_events_get(obj))
60 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
65 _elm_button_smart_sizing_eval(Evas_Object *obj)
67 Evas_Coord minw = -1, minh = -1;
69 ELM_BUTTON_DATA_GET(obj, sd);
71 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
72 edje_object_size_min_restricted_calc
73 (ELM_WIDGET_DATA(sd)->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);
78 /* FIXME: replicated from elm_layout just because button's icon spot
79 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
80 * can changed the theme API */
82 _icon_signal_emit(Evas_Object *obj)
86 snprintf(buf, sizeof(buf), "elm,state,icon,%s",
87 elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
89 elm_layout_signal_emit(obj, buf, "elm");
90 edje_object_message_signal_process(elm_layout_edje_get(obj));
91 _elm_button_smart_sizing_eval(obj);
94 /* FIXME: replicated from elm_layout just because button's icon spot
95 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
96 * can changed the theme API */
98 _elm_button_smart_theme(Evas_Object *obj)
100 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->theme(obj)) return EINA_FALSE;
102 _icon_signal_emit(obj);
107 /* FIXME: replicated from elm_layout just because button's icon spot
108 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
109 * can changed the theme API */
111 _elm_button_smart_sub_object_del(Evas_Object *obj,
114 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->sub_object_del(obj, sobj))
117 _icon_signal_emit(obj);
122 /* FIXME: replicated from elm_layout just because button's icon spot
123 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
124 * can changed the theme API */
126 _elm_button_smart_content_set(Evas_Object *obj,
128 Evas_Object *content)
130 if (!ELM_CONTAINER_CLASS(_elm_button_parent_sc)->content_set
131 (obj, part, content))
134 _icon_signal_emit(obj);
140 _elm_button_smart_event(Evas_Object *obj,
141 Evas_Object *src __UNUSED__,
142 Evas_Callback_Type type,
145 Evas_Event_Key_Down *ev = event_info;
147 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
149 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
150 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
152 if ((strcmp(ev->keyname, "Return")) &&
153 (strcmp(ev->keyname, "KP_Enter")) &&
154 (strcmp(ev->keyname, "space")))
158 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
159 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
165 _on_clicked_signal(void *data,
166 Evas_Object *obj __UNUSED__,
167 const char *emission __UNUSED__,
168 const char *source __UNUSED__)
174 _autorepeat_send(void *data)
176 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
178 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
182 return ECORE_CALLBACK_CANCEL;
185 return ECORE_CALLBACK_RENEW;
189 _autorepeat_initial_send(void *data)
191 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
193 if (sd->timer) ecore_timer_del(sd->timer);
194 sd->repeating = EINA_TRUE;
195 _autorepeat_send(data);
196 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
198 return ECORE_CALLBACK_CANCEL;
202 _on_pressed_signal(void *data,
203 Evas_Object *obj __UNUSED__,
204 const char *emission __UNUSED__,
205 const char *source __UNUSED__)
207 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
209 if ((sd->autorepeat) && (!sd->repeating))
211 if (sd->ar_threshold <= 0.0)
212 _autorepeat_initial_send(data); /* call immediately */
214 sd->timer = ecore_timer_add
215 (sd->ar_threshold, _autorepeat_initial_send, data);
218 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
222 _on_unpressed_signal(void *data,
223 Evas_Object *obj __UNUSED__,
224 const char *emission __UNUSED__,
225 const char *source __UNUSED__)
227 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
231 ecore_timer_del(sd->timer);
234 sd->repeating = EINA_FALSE;
235 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
239 _access_info_cb(void *data __UNUSED__,
241 Elm_Widget_Item *item __UNUSED__)
243 const char *txt = elm_widget_access_info_get(obj);
245 if (!txt) txt = elm_layout_text_get(obj, NULL);
246 if (txt) return strdup(txt);
252 _access_state_cb(void *data __UNUSED__,
254 Elm_Widget_Item *item __UNUSED__)
256 if (elm_widget_disabled_get(obj))
257 return strdup(E_("State: Disabled"));
263 _elm_button_smart_add(Evas_Object *obj)
265 EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
267 ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
269 edje_object_signal_callback_add
270 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,click", "",
271 _on_clicked_signal, obj);
272 edje_object_signal_callback_add
273 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,press", "",
274 _on_pressed_signal, obj);
275 edje_object_signal_callback_add
276 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,unpress", "",
277 _on_unpressed_signal, obj);
279 _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
281 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
282 _elm_access_callback_set
283 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
284 _elm_access_callback_set
285 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
287 elm_widget_can_focus_set(obj, EINA_TRUE);
289 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
293 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
295 ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
297 ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
298 ELM_WIDGET_CLASS(sc)->theme = _elm_button_smart_theme;
299 ELM_WIDGET_CLASS(sc)->sub_object_del = _elm_button_smart_sub_object_del;
301 /* not a 'focus chain manager' */
302 ELM_WIDGET_CLASS(sc)->focus_next = NULL;
303 ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
305 ELM_CONTAINER_CLASS(sc)->content_set = _elm_button_smart_content_set;
307 ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
309 ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
310 ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
312 sc->admits_autorepeat = EINA_TRUE;
315 EAPI const Elm_Button_Smart_Class *
316 elm_button_smart_class_get(void)
318 static Elm_Button_Smart_Class _sc =
319 ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(ELM_BUTTON_SMART_NAME);
320 static const Elm_Button_Smart_Class *class = NULL;
321 Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
323 if (class) return class;
325 _elm_button_smart_set(&_sc);
326 esc->callbacks = _smart_callbacks;
333 elm_button_add(Evas_Object *parent)
337 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
339 obj = elm_widget_add(_elm_button_smart_class_new(), parent);
340 if (!obj) return NULL;
342 if (!elm_widget_sub_object_add(parent, obj))
343 ERR("could not add %p as sub object of %p", obj, parent);
349 elm_button_autorepeat_set(Evas_Object *obj,
352 ELM_BUTTON_CHECK(obj);
353 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
357 ecore_timer_del(sd->timer);
361 sd->repeating = EINA_FALSE;
364 #define _AR_CAPABLE(_sd) \
365 (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
368 elm_button_autorepeat_get(const Evas_Object *obj)
370 ELM_BUTTON_CHECK(obj) EINA_FALSE;
371 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
373 return _AR_CAPABLE(sd) & sd->autorepeat;
377 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
380 ELM_BUTTON_CHECK(obj);
381 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
383 if (!_AR_CAPABLE(sd))
385 ERR("this widget does not support auto repetition of clicks.");
389 if (sd->ar_threshold == t) return;
392 ecore_timer_del(sd->timer);
395 sd->ar_threshold = t;
399 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
401 ELM_BUTTON_CHECK(obj) 0.0;
402 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
404 if (!_AR_CAPABLE(sd)) return 0.0;
406 return sd->ar_threshold;
410 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
413 ELM_BUTTON_CHECK(obj);
414 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
416 if (!_AR_CAPABLE(sd))
418 ERR("this widget does not support auto repetition of clicks.");
422 if (sd->ar_interval == t) return;
425 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
429 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
431 ELM_BUTTON_CHECK(obj) 0.0;
432 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
434 return sd->ar_interval;