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(obj, 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_OR_RETURN(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))
83 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
88 /* FIXME: replicated from elm_layout just because button's icon spot
89 * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
90 * can changed the theme API */
92 _icon_signal_emit(Evas_Object *obj)
96 snprintf(buf, sizeof(buf), "elm,state,icon,%s",
97 elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
99 elm_layout_signal_emit(obj, buf, "elm");
100 edje_object_message_signal_process(elm_layout_edje_get(obj));
101 _elm_button_smart_sizing_eval(obj);
104 // FIXME: There are applications which do not use elm_win as top widget.
105 // This is workaround! Those could not use focus!
106 static Eina_Bool _focus_enabled(Evas_Object *obj)
108 if (!elm_widget_focus_get(obj)) return EINA_FALSE;
110 const Evas_Object *win = elm_widget_top_get(obj);
111 const char *type = evas_object_type_get(win);
113 if (type && !strcmp(type, "elm_win"))
115 return elm_win_focus_highlight_enabled_get(win);
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 _on_clicked_signal(void *data,
167 Evas_Object *obj __UNUSED__,
168 const char *emission __UNUSED__,
169 const char *source __UNUSED__)
175 _autorepeat_send(void *data)
177 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
179 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
183 return ECORE_CALLBACK_CANCEL;
186 return ECORE_CALLBACK_RENEW;
190 _autorepeat_initial_send(void *data)
192 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
194 if (sd->timer) ecore_timer_del(sd->timer);
195 sd->repeating = EINA_TRUE;
196 _autorepeat_send(data);
197 sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
199 return ECORE_CALLBACK_CANCEL;
203 _multi_up_cb(void *data __UNUSED__,
204 Evas *evas __UNUSED__,
206 void *event_info __UNUSED__)
208 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
210 if (sd->multi_down == 0)
212 //Emitting signal to inform edje about last multi up event.
213 elm_layout_signal_emit(obj, "elm,action,multi,up", "elm");
218 _multi_down_cb(void *data __UNUSED__,
219 Evas *evas __UNUSED__,
221 void *event_info __UNUSED__)
223 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
224 //Emitting signal to inform edje about multi down event.
225 elm_layout_signal_emit(obj, "elm,action,multi,down", "elm");
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);
236 if ((sd->autorepeat) && (!sd->repeating))
238 if (sd->ar_threshold <= 0.0)
239 _autorepeat_initial_send(data); /* call immediately */
242 if (sd->timer) ecore_timer_del(sd->timer);
243 sd->timer = ecore_timer_add
244 (sd->ar_threshold, _autorepeat_initial_send, data);
248 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
252 _on_unpressed_signal(void *data,
253 Evas_Object *obj __UNUSED__,
254 const char *emission __UNUSED__,
255 const char *source __UNUSED__)
257 ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
261 ecore_timer_del(sd->timer);
264 sd->repeating = EINA_FALSE;
265 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
269 _elm_button_smart_event(Evas_Object *obj,
270 Evas_Object *src __UNUSED__,
271 Evas_Callback_Type type,
274 Evas_Event_Key_Down *ev = event_info;
276 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
278 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
279 if (!_focus_enabled(obj)) return EINA_FALSE;
280 if ((strcmp(ev->keyname, "Return")) &&
281 (strcmp(ev->keyname, "KP_Enter")))
284 if (type == EVAS_CALLBACK_KEY_DOWN)
286 elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
287 elm_layout_signal_emit(obj, "elm,action,pressed", "elm");
288 _on_pressed_signal(obj, NULL, NULL, NULL);
290 else if (type == EVAS_CALLBACK_KEY_UP)
292 elm_layout_signal_emit(obj, "elm,action,unpressed", "elm");
293 _on_unpressed_signal(obj, NULL, NULL, NULL);
296 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
301 _access_info_cb(void *data __UNUSED__, Evas_Object *obj)
303 const char *txt = elm_widget_access_info_get(obj);
305 if (!txt) txt = elm_layout_text_get(obj, NULL);
306 if (txt) return strdup(txt);
312 _access_state_cb(void *data __UNUSED__, Evas_Object *obj)
314 if (elm_widget_disabled_get(obj))
315 return strdup(E_("State: Disabled"));
321 _elm_button_smart_add(Evas_Object *obj)
323 EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
325 ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
329 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
331 ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
333 ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
334 ELM_WIDGET_CLASS(sc)->theme = _elm_button_smart_theme;
335 ELM_WIDGET_CLASS(sc)->sub_object_del = _elm_button_smart_sub_object_del;
337 /* not a 'focus chain manager' */
338 ELM_WIDGET_CLASS(sc)->focus_next = NULL;
339 ELM_WIDGET_CLASS(sc)->focus_direction_manager_is = NULL;
340 ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
342 ELM_CONTAINER_CLASS(sc)->content_set = _elm_button_smart_content_set;
344 ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
345 ELM_WIDGET_CLASS(sc)->activate = _elm_button_smart_activate;
347 ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
348 ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
350 sc->admits_autorepeat = EINA_TRUE;
353 EAPI const Elm_Button_Smart_Class *
354 elm_button_smart_class_get(void)
356 static Elm_Button_Smart_Class _sc =
357 ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(ELM_BUTTON_SMART_NAME);
358 static const Elm_Button_Smart_Class *class = NULL;
359 Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
361 if (class) return class;
363 _elm_button_smart_set(&_sc);
364 esc->callbacks = _smart_callbacks;
371 elm_button_add(Evas_Object *parent)
375 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
377 obj = elm_widget_add(_elm_button_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);
383 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, NULL);
386 elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
388 edje_object_signal_callback_add
389 (ELM_WIDGET_DATA(sd)->resize_obj, "elm,action,click", "",
390 _on_clicked_signal, obj);
391 edje_object_signal_callback_add
392 (ELM_WIDGET_DATA(sd)->resize_obj, "elm,action,press", "",
393 _on_pressed_signal, obj);
394 edje_object_signal_callback_add
395 (ELM_WIDGET_DATA(sd)->resize_obj, "elm,action,unpress", "",
396 _on_unpressed_signal, obj);
398 /*Registering Multi down/up events to ignore mouse down/up events untill all
399 * multi down/up events are released.*/
400 evas_object_event_callback_add(obj, EVAS_CALLBACK_MULTI_DOWN, (Evas_Object_Event_Cb)_multi_down_cb, NULL);
401 evas_object_event_callback_add(obj, EVAS_CALLBACK_MULTI_UP, (Evas_Object_Event_Cb)_multi_up_cb, NULL);
403 _elm_access_object_register(obj, ELM_WIDGET_DATA(sd)->resize_obj);
405 (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
406 _elm_access_callback_set
407 (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
408 _elm_access_callback_set
409 (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, sd);
411 elm_widget_can_focus_set(obj, EINA_TRUE);
413 //Tizen Only: This should be removed when eo is applied.
414 ELM_WIDGET_DATA_GET(obj, wsd);
415 wsd->on_create = EINA_FALSE;
421 elm_button_autorepeat_set(Evas_Object *obj,
424 ELM_BUTTON_CHECK(obj);
425 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
429 ecore_timer_del(sd->timer);
433 sd->repeating = EINA_FALSE;
436 #define _AR_CAPABLE(_sd) \
437 (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
440 elm_button_autorepeat_get(const Evas_Object *obj)
442 ELM_BUTTON_CHECK(obj) EINA_FALSE;
443 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
445 return _AR_CAPABLE(sd) & sd->autorepeat;
449 elm_button_autorepeat_initial_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_threshold == t) return;
464 ecore_timer_del(sd->timer);
467 sd->ar_threshold = t;
471 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
473 ELM_BUTTON_CHECK(obj) 0.0;
474 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
476 if (!_AR_CAPABLE(sd)) return 0.0;
478 return sd->ar_threshold;
482 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
485 ELM_BUTTON_CHECK(obj);
486 ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
488 if (!_AR_CAPABLE(sd))
490 ERR("this widget does not support auto repetition of clicks.");
494 if (sd->ar_interval == t) return;
497 if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
501 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
503 ELM_BUTTON_CHECK(obj) 0.0;
504 ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
506 return sd->ar_interval;