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 EVAS_SMART_SUBCLASS_NEW
35 (ELM_BUTTON_SMART_NAME, _elm_button, Elm_Button_Smart_Class,
36 Elm_Layout_Smart_Class, elm_layout_smart_class_get, _smart_callbacks);
39 _activate(Evas_Object *obj)
41 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
45 ecore_timer_del(sd->timer);
49 sd->repeating = EINA_FALSE;
51 if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
52 (_elm_access_2nd_click_timeout(obj)))
54 if (_elm_config->access_mode != ELM_ACCESS_MODE_OFF)
55 _elm_access_say(E_("Clicked"));
56 if (!elm_widget_disabled_get(obj) &&
57 !evas_object_freeze_events_get(obj))
58 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
63 _elm_button_smart_sizing_eval(Evas_Object *obj)
65 Evas_Coord minw = -1, minh = -1;
67 ELM_BUTTON_DATA_GET(obj, sd);
69 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
70 edje_object_size_min_restricted_calc
71 (ELM_WIDGET_DATA(sd)->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(Evas_Object *obj, Elm_Activate act)
79 if (act != ELM_ACTIVATE_DEFAULT) return EINA_FALSE;
81 if (!elm_widget_disabled_get(obj) &&
82 !evas_object_freeze_events_get(obj))
84 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
85 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
91 /* FIXME: replicated from elm_layout just because button's icon spot
92 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
93 * can changed the theme API */
95 _icon_signal_emit(Evas_Object *obj)
99 snprintf(buf, sizeof(buf), "elm,state,icon,%s",
100 elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
102 elm_layout_signal_emit(obj, buf, "elm");
103 edje_object_message_signal_process(elm_layout_edje_get(obj));
104 _elm_button_smart_sizing_eval(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_theme(Evas_Object *obj)
113 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->theme(obj)) return EINA_FALSE;
115 _icon_signal_emit(obj);
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_sub_object_del(Evas_Object *obj,
127 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->sub_object_del(obj, sobj))
130 _icon_signal_emit(obj);
135 /* FIXME: replicated from elm_layout just because button's icon spot
136 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
137 * can changed the theme API */
139 _elm_button_smart_content_set(Evas_Object *obj,
141 Evas_Object *content)
143 if (!ELM_CONTAINER_CLASS(_elm_button_parent_sc)->content_set
144 (obj, part, content))
147 _icon_signal_emit(obj);
153 _elm_button_smart_event(Evas_Object *obj,
154 Evas_Object *src __UNUSED__,
155 Evas_Callback_Type type,
158 Evas_Event_Key_Down *ev = event_info;
160 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
162 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
163 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
165 if ((strcmp(ev->keyname, "Return")) &&
166 (strcmp(ev->keyname, "KP_Enter")) &&
167 (strcmp(ev->keyname, "space")))
171 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
172 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
178 _on_clicked_signal(void *data,
179 Evas_Object *obj __UNUSED__,
180 const char *emission __UNUSED__,
181 const char *source __UNUSED__)
187 _autorepeat_send(void *data)
189 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
191 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
195 return ECORE_CALLBACK_CANCEL;
198 return ECORE_CALLBACK_RENEW;
202 _autorepeat_initial_send(void *data)
204 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
206 if (sd->timer) ecore_timer_del(sd->timer);
207 sd->repeating = EINA_TRUE;
208 _autorepeat_send(data);
209 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
211 return ECORE_CALLBACK_CANCEL;
215 _on_pressed_signal(void *data,
216 Evas_Object *obj __UNUSED__,
217 const char *emission __UNUSED__,
218 const char *source __UNUSED__)
220 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
222 if ((sd->autorepeat) && (!sd->repeating))
224 if (sd->ar_threshold <= 0.0)
225 _autorepeat_initial_send(data); /* call immediately */
227 sd->timer = ecore_timer_add
228 (sd->ar_threshold, _autorepeat_initial_send, data);
231 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
235 _on_unpressed_signal(void *data,
236 Evas_Object *obj __UNUSED__,
237 const char *emission __UNUSED__,
238 const char *source __UNUSED__)
240 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
244 ecore_timer_del(sd->timer);
247 sd->repeating = EINA_FALSE;
248 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
252 _access_info_cb(void *data __UNUSED__, Evas_Object *obj)
254 const char *txt = elm_widget_access_info_get(obj);
256 if (!txt) txt = elm_layout_text_get(obj, NULL);
257 if (txt) return strdup(txt);
263 _access_state_cb(void *data __UNUSED__, Evas_Object *obj)
265 if (elm_widget_disabled_get(obj))
266 return strdup(E_("State: Disabled"));
272 _elm_button_smart_add(Evas_Object *obj)
274 EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
276 ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
278 edje_object_signal_callback_add
279 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,click", "",
280 _on_clicked_signal, obj);
281 edje_object_signal_callback_add
282 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,press", "",
283 _on_pressed_signal, obj);
284 edje_object_signal_callback_add
285 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,unpress", "",
286 _on_unpressed_signal, obj);
288 _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
290 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
291 _elm_access_callback_set
292 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
293 _elm_access_callback_set
294 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
296 elm_widget_can_focus_set(obj, EINA_TRUE);
298 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
302 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
304 ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
306 ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
307 ELM_WIDGET_CLASS(sc)->theme = _elm_button_smart_theme;
308 ELM_WIDGET_CLASS(sc)->sub_object_del = _elm_button_smart_sub_object_del;
310 /* not a 'focus chain manager' */
311 ELM_WIDGET_CLASS(sc)->focus_next = NULL;
312 ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
314 ELM_CONTAINER_CLASS(sc)->content_set = _elm_button_smart_content_set;
316 ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
317 ELM_WIDGET_CLASS(sc)->activate = _elm_button_smart_activate;
319 ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
320 ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
322 sc->admits_autorepeat = EINA_TRUE;
325 EAPI const Elm_Button_Smart_Class *
326 elm_button_smart_class_get(void)
328 static Elm_Button_Smart_Class _sc =
329 ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(ELM_BUTTON_SMART_NAME);
330 static const Elm_Button_Smart_Class *class = NULL;
331 Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
333 if (class) return class;
335 _elm_button_smart_set(&_sc);
336 esc->callbacks = _smart_callbacks;
343 elm_button_add(Evas_Object *parent)
347 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
349 obj = elm_widget_add(_elm_button_smart_class_new(), parent);
350 if (!obj) return NULL;
352 if (!elm_widget_sub_object_add(parent, obj))
353 ERR("could not add %p as sub object of %p", obj, parent);
355 _elm_widget_orient_signal_emit(obj);
361 elm_button_autorepeat_set(Evas_Object *obj,
364 ELM_BUTTON_CHECK(obj);
365 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
369 ecore_timer_del(sd->timer);
373 sd->repeating = EINA_FALSE;
376 #define _AR_CAPABLE(_sd) \
377 (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
380 elm_button_autorepeat_get(const Evas_Object *obj)
382 ELM_BUTTON_CHECK(obj) EINA_FALSE;
383 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
385 return _AR_CAPABLE(sd) & sd->autorepeat;
389 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
392 ELM_BUTTON_CHECK(obj);
393 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
395 if (!_AR_CAPABLE(sd))
397 ERR("this widget does not support auto repetition of clicks.");
401 if (sd->ar_threshold == t) return;
404 ecore_timer_del(sd->timer);
407 sd->ar_threshold = t;
411 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
413 ELM_BUTTON_CHECK(obj) 0.0;
414 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
416 if (!_AR_CAPABLE(sd)) return 0.0;
418 return sd->ar_threshold;
422 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
425 ELM_BUTTON_CHECK(obj);
426 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
428 if (!_AR_CAPABLE(sd))
430 ERR("this widget does not support auto repetition of clicks.");
434 if (sd->ar_interval == t) return;
437 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
441 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
443 ELM_BUTTON_CHECK(obj) 0.0;
444 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
446 return sd->ar_interval;