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");
118 edje_object_message_signal_process(elm_layout_edje_get(obj));
119 _elm_button_smart_sizing_eval(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_theme(Evas_Object *obj)
128 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->theme(obj)) return EINA_FALSE;
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_sub_object_del(Evas_Object *obj,
142 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->sub_object_del(obj, sobj))
145 _icon_signal_emit(obj);
150 /* FIXME: replicated from elm_layout just because button's icon spot
151 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
152 * can changed the theme API */
154 _elm_button_smart_content_set(Evas_Object *obj,
156 Evas_Object *content)
158 if (!ELM_CONTAINER_CLASS(_elm_button_parent_sc)->content_set
159 (obj, part, content))
162 _icon_signal_emit(obj);
168 _elm_button_smart_event(Evas_Object *obj,
169 Evas_Object *src __UNUSED__,
170 Evas_Callback_Type type,
173 Evas_Event_Key_Down *ev = event_info;
175 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
177 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
178 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
180 if ((strcmp(ev->keyname, "Return")) &&
181 (strcmp(ev->keyname, "KP_Enter")) &&
182 (strcmp(ev->keyname, "space")))
186 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
187 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
193 _on_clicked_signal(void *data,
194 Evas_Object *obj __UNUSED__,
195 const char *emission __UNUSED__,
196 const char *source __UNUSED__)
202 _autorepeat_send(void *data)
204 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
206 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
210 return ECORE_CALLBACK_CANCEL;
213 return ECORE_CALLBACK_RENEW;
217 _autorepeat_initial_send(void *data)
219 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
221 if (sd->timer) ecore_timer_del(sd->timer);
222 sd->repeating = EINA_TRUE;
223 _autorepeat_send(data);
224 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
226 return ECORE_CALLBACK_CANCEL;
230 _on_pressed_signal(void *data,
231 Evas_Object *obj __UNUSED__,
232 const char *emission __UNUSED__,
233 const char *source __UNUSED__)
235 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
237 if ((sd->autorepeat) && (!sd->repeating))
239 if (sd->ar_threshold <= 0.0)
240 _autorepeat_initial_send(data); /* call immediately */
242 sd->timer = ecore_timer_add
243 (sd->ar_threshold, _autorepeat_initial_send, data);
246 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
250 _on_unpressed_signal(void *data,
251 Evas_Object *obj __UNUSED__,
252 const char *emission __UNUSED__,
253 const char *source __UNUSED__)
255 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
259 ecore_timer_del(sd->timer);
262 sd->repeating = EINA_FALSE;
263 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
267 _access_info_cb(void *data __UNUSED__,
269 Elm_Widget_Item *item __UNUSED__)
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__,
282 Elm_Widget_Item *item __UNUSED__)
284 if (elm_widget_disabled_get(obj))
285 return strdup(E_("State: Disabled"));
291 _elm_button_smart_add(Evas_Object *obj)
293 EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
295 ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
297 edje_object_signal_callback_add
298 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,click", "",
299 _on_clicked_signal, obj);
300 edje_object_signal_callback_add
301 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,press", "",
302 _on_pressed_signal, obj);
303 edje_object_signal_callback_add
304 (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,unpress", "",
305 _on_unpressed_signal, obj);
307 _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
309 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
310 _elm_access_callback_set
311 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
312 _elm_access_callback_set
313 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
315 elm_widget_can_focus_set(obj, EINA_TRUE);
317 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
321 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
323 ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
325 ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
326 ELM_WIDGET_CLASS(sc)->theme = _elm_button_smart_theme;
327 ELM_WIDGET_CLASS(sc)->sub_object_del = _elm_button_smart_sub_object_del;
329 /* not a 'focus chain manager' */
330 ELM_WIDGET_CLASS(sc)->focus_next = NULL;
331 ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
333 ELM_CONTAINER_CLASS(sc)->content_set = _elm_button_smart_content_set;
335 ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
337 ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
338 ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
341 EAPI const Elm_Button_Smart_Class *
342 elm_button_smart_class_get(void)
344 static Elm_Button_Smart_Class _sc =
345 ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(BUTTON_SMART_NAME);
346 static const Elm_Button_Smart_Class *class = NULL;
347 Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
349 if (class) return class;
351 _elm_button_smart_set(&_sc);
352 esc->callbacks = _smart_callbacks;
358 /* new class here just to take advantage of
359 * ELM_BUTTON_SMART_CLASS_INIT() macro, which sets auto-repeat capability */
360 EVAS_SMART_SUBCLASS_NEW
361 (BUTTON_SMART_NAME, _elm_button_widget, Elm_Button_Smart_Class,
362 Elm_Button_Smart_Class, elm_button_smart_class_get, NULL);
365 _elm_button_widget_smart_set_user(Elm_Button_Smart_Class *sc __UNUSED__)
367 /* NOP: declared because it's obligatory for Evas macro */
371 elm_button_add(Evas_Object *parent)
375 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
377 obj = elm_widget_add(_elm_button_widget_smart_class_new(), parent);
378 if (!obj) return NULL;
380 if (!elm_widget_sub_object_add(parent, obj))
381 ERR("could not add %p as sub object of %p", obj, parent);
387 elm_button_autorepeat_set(Evas_Object *obj,
390 ELM_BUTTON_CHECK(obj);
391 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
395 ecore_timer_del(sd->timer);
399 sd->repeating = EINA_FALSE;
402 #define _AR_CAPABLE(_sd) \
403 (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
406 elm_button_autorepeat_get(const Evas_Object *obj)
408 ELM_BUTTON_CHECK(obj) EINA_FALSE;
409 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
411 return _AR_CAPABLE(sd) & sd->autorepeat;
415 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
418 ELM_BUTTON_CHECK(obj);
419 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
421 if (!_AR_CAPABLE(sd))
423 ERR("this widget does not support auto repetition of clicks.");
427 if (sd->ar_threshold == t) return;
430 ecore_timer_del(sd->timer);
433 sd->ar_threshold = t;
437 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
439 ELM_BUTTON_CHECK(obj) 0.0;
440 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
442 if (!_AR_CAPABLE(sd)) return 0.0;
444 return sd->ar_threshold;
448 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
451 ELM_BUTTON_CHECK(obj);
452 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
454 if (!_AR_CAPABLE(sd))
456 ERR("this widget does not support auto repetition of clicks.");
460 if (sd->ar_interval == t) return;
463 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
467 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
469 ELM_BUTTON_CHECK(obj) 0.0;
470 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
472 return sd->ar_interval;