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 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
82 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
87 /* FIXME: replicated from elm_layout just because button's icon spot
88 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
89 * can changed the theme API */
91 _icon_signal_emit(Evas_Object *obj)
95 snprintf(buf, sizeof(buf), "elm,state,icon,%s",
96 elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
98 elm_layout_signal_emit(obj, buf, "elm");
99 edje_object_message_signal_process(elm_layout_edje_get(obj));
100 _elm_button_smart_sizing_eval(obj);
103 /* FIXME: replicated from elm_layout just because button's icon spot
104 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
105 * can changed the theme API */
107 _elm_button_smart_theme(Evas_Object *obj)
109 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->theme(obj)) return EINA_FALSE;
111 _icon_signal_emit(obj);
116 /* FIXME: replicated from elm_layout just because button's icon spot
117 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
118 * can changed the theme API */
120 _elm_button_smart_sub_object_del(Evas_Object *obj,
123 if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->sub_object_del(obj, sobj))
126 _icon_signal_emit(obj);
131 /* FIXME: replicated from elm_layout just because button's icon spot
132 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
133 * can changed the theme API */
135 _elm_button_smart_content_set(Evas_Object *obj,
137 Evas_Object *content)
139 if (!ELM_CONTAINER_CLASS(_elm_button_parent_sc)->content_set
140 (obj, part, content))
143 _icon_signal_emit(obj);
149 _elm_button_smart_event(Evas_Object *obj,
150 Evas_Object *src __UNUSED__,
151 Evas_Callback_Type type,
154 Evas_Event_Key_Down *ev = event_info;
156 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
158 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
159 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
161 if ((strcmp(ev->keyname, "Return")) &&
162 (strcmp(ev->keyname, "KP_Enter")) &&
163 (strcmp(ev->keyname, "space")))
167 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
168 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
174 _on_clicked_signal(void *data,
175 Evas_Object *obj __UNUSED__,
176 const char *emission __UNUSED__,
177 const char *source __UNUSED__)
183 _autorepeat_send(void *data)
185 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
187 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
191 return ECORE_CALLBACK_CANCEL;
194 return ECORE_CALLBACK_RENEW;
198 _autorepeat_initial_send(void *data)
200 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
202 if (sd->timer) ecore_timer_del(sd->timer);
203 sd->repeating = EINA_TRUE;
204 _autorepeat_send(data);
205 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
207 return ECORE_CALLBACK_CANCEL;
211 _on_pressed_signal(void *data,
212 Evas_Object *obj __UNUSED__,
213 const char *emission __UNUSED__,
214 const char *source __UNUSED__)
216 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
218 if ((sd->autorepeat) && (!sd->repeating))
220 if (sd->ar_threshold <= 0.0)
221 _autorepeat_initial_send(data); /* call immediately */
223 sd->timer = ecore_timer_add
224 (sd->ar_threshold, _autorepeat_initial_send, data);
227 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
231 _on_unpressed_signal(void *data,
232 Evas_Object *obj __UNUSED__,
233 const char *emission __UNUSED__,
234 const char *source __UNUSED__)
236 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
240 ecore_timer_del(sd->timer);
243 sd->repeating = EINA_FALSE;
244 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
248 _access_info_cb(void *data __UNUSED__,
250 Elm_Widget_Item *item __UNUSED__)
252 const char *txt = elm_widget_access_info_get(obj);
254 if (!txt) txt = elm_layout_text_get(obj, NULL);
255 if (txt) return strdup(txt);
261 _access_state_cb(void *data __UNUSED__,
263 Elm_Widget_Item *item __UNUSED__)
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);
359 elm_button_autorepeat_set(Evas_Object *obj,
362 ELM_BUTTON_CHECK(obj);
363 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
367 ecore_timer_del(sd->timer);
371 sd->repeating = EINA_FALSE;
374 #define _AR_CAPABLE(_sd) \
375 (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
378 elm_button_autorepeat_get(const Evas_Object *obj)
380 ELM_BUTTON_CHECK(obj) EINA_FALSE;
381 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
383 return _AR_CAPABLE(sd) & sd->autorepeat;
387 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
390 ELM_BUTTON_CHECK(obj);
391 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
393 if (!_AR_CAPABLE(sd))
395 ERR("this widget does not support auto repetition of clicks.");
399 if (sd->ar_threshold == t) return;
402 ecore_timer_del(sd->timer);
405 sd->ar_threshold = t;
409 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
411 ELM_BUTTON_CHECK(obj) 0.0;
412 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
414 if (!_AR_CAPABLE(sd)) return 0.0;
416 return sd->ar_threshold;
420 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
423 ELM_BUTTON_CHECK(obj);
424 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
426 if (!_AR_CAPABLE(sd))
428 ERR("this widget does not support auto repetition of clicks.");
432 if (sd->ar_interval == t) return;
435 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
439 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
441 ELM_BUTTON_CHECK(obj) 0.0;
442 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
444 return sd->ar_interval;