From: Kim Shinwoo <kimcinoo.efl@gmail.com>
[platform/upstream/elementary.git] / src / lib / elm_button.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "elm_widget_button.h"
4
5 EAPI const char ELM_BUTTON_SMART_NAME[] = "elm_button";
6
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";
11
12 static const Elm_Layout_Part_Alias_Description _content_aliases[] =
13 {
14    {"icon", "elm.swallow.content"},
15    {NULL, NULL}
16 };
17
18 static const Elm_Layout_Part_Alias_Description _text_aliases[] =
19 {
20    {"default", "elm.text"},
21    {NULL, NULL}
22 };
23
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[] = {
27    {SIG_CLICKED, ""},
28    {SIG_REPEATED, ""},
29    {SIG_PRESSED, ""},
30    {SIG_UNPRESSED, ""},
31    {NULL, NULL}
32 };
33
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);
37
38 static void
39 _activate(Evas_Object *obj)
40 {
41    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
42
43    if (sd->timer)
44      {
45         ecore_timer_del(sd->timer);
46         sd->timer = NULL;
47      }
48
49    sd->repeating = EINA_FALSE;
50
51    if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
52        (_elm_access_2nd_click_timeout(obj)))
53      {
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);
59      }
60 }
61
62 static void
63 _elm_button_smart_sizing_eval(Evas_Object *obj)
64 {
65    Evas_Coord minw = -1, minh = -1;
66
67    ELM_BUTTON_DATA_GET(obj, sd);
68
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);
74 }
75
76 static Eina_Bool
77 _elm_button_smart_activate(Evas_Object *obj, Elm_Activate act)
78 {
79    if (act != ELM_ACTIVATE_DEFAULT) return EINA_FALSE;
80
81    evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
82    elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
83
84    return EINA_TRUE;
85 }
86
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 */
90 static void
91 _icon_signal_emit(Evas_Object *obj)
92 {
93    char buf[64];
94
95    snprintf(buf, sizeof(buf), "elm,state,icon,%s",
96             elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
97
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);
101 }
102
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 */
106 static Eina_Bool
107 _elm_button_smart_theme(Evas_Object *obj)
108 {
109    if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->theme(obj)) return EINA_FALSE;
110
111    _icon_signal_emit(obj);
112
113    return EINA_TRUE;
114 }
115
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 */
119 static Eina_Bool
120 _elm_button_smart_sub_object_del(Evas_Object *obj,
121                                  Evas_Object *sobj)
122 {
123    if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->sub_object_del(obj, sobj))
124      return EINA_FALSE;
125
126    _icon_signal_emit(obj);
127
128    return EINA_TRUE;
129 }
130
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 */
134 static Eina_Bool
135 _elm_button_smart_content_set(Evas_Object *obj,
136                               const char *part,
137                               Evas_Object *content)
138 {
139    if (!ELM_CONTAINER_CLASS(_elm_button_parent_sc)->content_set
140          (obj, part, content))
141      return EINA_FALSE;
142
143    _icon_signal_emit(obj);
144
145    return EINA_TRUE;
146 }
147
148 static Eina_Bool
149 _elm_button_smart_event(Evas_Object *obj,
150                         Evas_Object *src __UNUSED__,
151                         Evas_Callback_Type type,
152                         void *event_info)
153 {
154    Evas_Event_Key_Down *ev = event_info;
155
156    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
157
158    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
159    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
160
161    if ((strcmp(ev->keyname, "Return")) &&
162        (strcmp(ev->keyname, "KP_Enter")) &&
163        (strcmp(ev->keyname, "space")))
164      return EINA_FALSE;
165
166    _activate(obj);
167    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
168    elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
169
170    return EINA_TRUE;
171 }
172
173 static void
174 _on_clicked_signal(void *data,
175                    Evas_Object *obj __UNUSED__,
176                    const char *emission __UNUSED__,
177                    const char *source __UNUSED__)
178 {
179    _activate(data);
180 }
181
182 static Eina_Bool
183 _autorepeat_send(void *data)
184 {
185    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
186
187    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
188    if (!sd->repeating)
189      {
190         sd->timer = NULL;
191         return ECORE_CALLBACK_CANCEL;
192      }
193
194    return ECORE_CALLBACK_RENEW;
195 }
196
197 static Eina_Bool
198 _autorepeat_initial_send(void *data)
199 {
200    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
201
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);
206
207    return ECORE_CALLBACK_CANCEL;
208 }
209
210 static void
211 _on_pressed_signal(void *data,
212                    Evas_Object *obj __UNUSED__,
213                    const char *emission __UNUSED__,
214                    const char *source __UNUSED__)
215 {
216    ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
217
218    if ((sd->autorepeat) && (!sd->repeating))
219      {
220         if (sd->ar_threshold <= 0.0)
221           _autorepeat_initial_send(data);  /* call immediately */
222         else
223           sd->timer = ecore_timer_add
224               (sd->ar_threshold, _autorepeat_initial_send, data);
225      }
226
227    evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
228 }
229
230 static void
231 _on_unpressed_signal(void *data,
232                      Evas_Object *obj __UNUSED__,
233                      const char *emission __UNUSED__,
234                      const char *source __UNUSED__)
235 {
236    ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
237
238    if (sd->timer)
239      {
240         ecore_timer_del(sd->timer);
241         sd->timer = NULL;
242      }
243    sd->repeating = EINA_FALSE;
244    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
245 }
246
247 static char *
248 _access_info_cb(void *data __UNUSED__,
249                 Evas_Object *obj,
250                 Elm_Widget_Item *item __UNUSED__)
251 {
252    const char *txt = elm_widget_access_info_get(obj);
253
254    if (!txt) txt = elm_layout_text_get(obj, NULL);
255    if (txt) return strdup(txt);
256
257    return NULL;
258 }
259
260 static char *
261 _access_state_cb(void *data __UNUSED__,
262                  Evas_Object *obj,
263                  Elm_Widget_Item *item __UNUSED__)
264 {
265    if (elm_widget_disabled_get(obj))
266      return strdup(E_("State: Disabled"));
267
268    return NULL;
269 }
270
271 static void
272 _elm_button_smart_add(Evas_Object *obj)
273 {
274    EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
275
276    ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
277
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);
287
288    _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
289    _elm_access_text_set
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);
295
296    elm_widget_can_focus_set(obj, EINA_TRUE);
297
298    elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
299 }
300
301 static void
302 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
303 {
304    ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
305
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;
309
310    /* not a 'focus chain manager' */
311    ELM_WIDGET_CLASS(sc)->focus_next = NULL;
312    ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
313
314    ELM_CONTAINER_CLASS(sc)->content_set = _elm_button_smart_content_set;
315
316    ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
317    ELM_WIDGET_CLASS(sc)->activate = _elm_button_smart_activate;
318
319    ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
320    ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
321
322    sc->admits_autorepeat = EINA_TRUE;
323 }
324
325 EAPI const Elm_Button_Smart_Class *
326 elm_button_smart_class_get(void)
327 {
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;
332
333    if (class) return class;
334
335    _elm_button_smart_set(&_sc);
336    esc->callbacks = _smart_callbacks;
337    class = &_sc;
338
339    return class;
340 }
341
342 EAPI Evas_Object *
343 elm_button_add(Evas_Object *parent)
344 {
345    Evas_Object *obj;
346
347    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
348
349    obj = elm_widget_add(_elm_button_smart_class_new(), parent);
350    if (!obj) return NULL;
351
352    if (!elm_widget_sub_object_add(parent, obj))
353      ERR("could not add %p as sub object of %p", obj, parent);
354
355    return obj;
356 }
357
358 EAPI void
359 elm_button_autorepeat_set(Evas_Object *obj,
360                           Eina_Bool on)
361 {
362    ELM_BUTTON_CHECK(obj);
363    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
364
365    if (sd->timer)
366      {
367         ecore_timer_del(sd->timer);
368         sd->timer = NULL;
369      }
370    sd->autorepeat = on;
371    sd->repeating = EINA_FALSE;
372 }
373
374 #define _AR_CAPABLE(_sd) \
375   (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
376
377 EAPI Eina_Bool
378 elm_button_autorepeat_get(const Evas_Object *obj)
379 {
380    ELM_BUTTON_CHECK(obj) EINA_FALSE;
381    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
382
383    return _AR_CAPABLE(sd) & sd->autorepeat;
384 }
385
386 EAPI void
387 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
388                                           double t)
389 {
390    ELM_BUTTON_CHECK(obj);
391    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
392
393    if (!_AR_CAPABLE(sd))
394      {
395         ERR("this widget does not support auto repetition of clicks.");
396         return;
397      }
398
399    if (sd->ar_threshold == t) return;
400    if (sd->timer)
401      {
402         ecore_timer_del(sd->timer);
403         sd->timer = NULL;
404      }
405    sd->ar_threshold = t;
406 }
407
408 EAPI double
409 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
410 {
411    ELM_BUTTON_CHECK(obj) 0.0;
412    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
413
414    if (!_AR_CAPABLE(sd)) return 0.0;
415
416    return sd->ar_threshold;
417 }
418
419 EAPI void
420 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
421                                       double t)
422 {
423    ELM_BUTTON_CHECK(obj);
424    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
425
426    if (!_AR_CAPABLE(sd))
427      {
428         ERR("this widget does not support auto repetition of clicks.");
429         return;
430      }
431
432    if (sd->ar_interval == t) return;
433
434    sd->ar_interval = t;
435    if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
436 }
437
438 EAPI double
439 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
440 {
441    ELM_BUTTON_CHECK(obj) 0.0;
442    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
443
444    return sd->ar_interval;
445 }