[Elm] And now button also inherits from
[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 static const char 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 #define ELM_BUTTON_DATA_GET(o, sd) \
13   Elm_Button_Smart_Data * sd = evas_object_smart_data_get(o)
14
15 #define ELM_BUTTON_DATA_GET_OR_RETURN(o, ptr)        \
16   ELM_BUTTON_DATA_GET(o, ptr);                       \
17   if (!ptr)                                          \
18     {                                                \
19        CRITICAL("No widget data for object %p (%s)", \
20                 o, evas_object_type_get(o));         \
21        return;                                       \
22     }
23
24 #define ELM_BUTTON_DATA_GET_OR_RETURN_VAL(o, ptr, val) \
25   ELM_BUTTON_DATA_GET(o, ptr);                         \
26   if (!ptr)                                            \
27     {                                                  \
28        CRITICAL("No widget data for object %p (%s)",   \
29                 o, evas_object_type_get(o));           \
30        return val;                                     \
31     }
32
33 #define ELM_BUTTON_CHECK(obj)                                             \
34   if (!obj || !elm_widget_type_check((obj), BUTTON_SMART_NAME, __func__)) \
35     return
36
37 static const Elm_Layout_Part_Alias_Description _content_aliases[] =
38 {
39    {"icon", "elm.swallow.content"},
40    {NULL, NULL}
41 };
42
43 static const Elm_Layout_Part_Alias_Description _text_aliases[] =
44 {
45    {"default", "elm.text"},
46    {NULL, NULL}
47 };
48
49 /* no *direct* instantiation of this class, so far */
50 __UNUSED__ static Evas_Smart *_elm_button_smart_class_new(void);
51
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[] = {
55    {SIG_CLICKED, ""},
56    {SIG_REPEATED, ""},
57    {SIG_PRESSED, ""},
58    {SIG_UNPRESSED, ""},
59    {NULL, NULL}
60 };
61
62 /* Inheriting from elm_layout. Besides, we need no more than what is
63  * there */
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);
67
68 static void
69 _activate(Evas_Object *obj)
70 {
71    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
72
73    if (sd->timer)
74      {
75         ecore_timer_del(sd->timer);
76         sd->timer = NULL;
77      }
78
79    sd->repeating = EINA_FALSE;
80
81    if ((_elm_config->access_mode == ELM_ACCESS_MODE_OFF) ||
82        (_elm_access_2nd_click_timeout(obj)))
83      {
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);
89      }
90 }
91
92 static void
93 _elm_button_smart_sizing_eval(Evas_Object *obj)
94 {
95    Evas_Coord minw = -1, minh = -1;
96
97    ELM_BUTTON_DATA_GET(obj, sd);
98
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);
104 }
105
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 */
109 static void
110 _icon_signal_emit(Evas_Object *obj)
111 {
112    char buf[64];
113
114    snprintf(buf, sizeof(buf), "elm,state,icon,%s",
115             elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
116
117    elm_layout_signal_emit(obj, buf, "elm");
118 }
119
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 */
123 static Eina_Bool
124 _elm_button_smart_theme(Evas_Object *obj)
125 {
126    if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->theme(obj)) return EINA_FALSE;
127
128    _icon_signal_emit(obj);
129
130    return EINA_TRUE;
131 }
132
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 */
136 static Eina_Bool
137 _elm_button_smart_sub_object_del(Evas_Object *obj,
138                                  Evas_Object *sobj)
139 {
140    if (!ELM_WIDGET_CLASS(_elm_button_parent_sc)->sub_object_del(obj, sobj))
141      return EINA_FALSE;
142
143    _icon_signal_emit(obj);
144
145    return EINA_TRUE;
146 }
147
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 */
151 static Eina_Bool
152 _elm_button_smart_content_set(Evas_Object *obj,
153                               const char *part,
154                               Evas_Object *content)
155 {
156    if (!ELM_CONTAINER_CLASS(_elm_button_parent_sc)->content_set
157          (obj, part, content))
158      return EINA_FALSE;
159
160    _icon_signal_emit(obj);
161
162    return EINA_TRUE;
163 }
164
165 static Eina_Bool
166 _elm_button_smart_event(Evas_Object *obj,
167                         Evas_Object *src __UNUSED__,
168                         Evas_Callback_Type type,
169                         void *event_info)
170 {
171    Evas_Event_Key_Down *ev = event_info;
172
173    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
174
175    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
176    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
177
178    if ((strcmp(ev->keyname, "Return")) &&
179        (strcmp(ev->keyname, "KP_Enter")) &&
180        (strcmp(ev->keyname, "space")))
181      return EINA_FALSE;
182
183    _activate(obj);
184    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
185    elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
186
187    return EINA_TRUE;
188 }
189
190 static void
191 _on_clicked_signal(void *data,
192                    Evas_Object *obj __UNUSED__,
193                    const char *emission __UNUSED__,
194                    const char *source __UNUSED__)
195 {
196    _activate(data);
197 }
198
199 static Eina_Bool
200 _autorepeat_send(void *data)
201 {
202    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
203
204    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
205    if (!sd->repeating)
206      {
207         sd->timer = NULL;
208         return ECORE_CALLBACK_CANCEL;
209      }
210
211    return ECORE_CALLBACK_RENEW;
212 }
213
214 static Eina_Bool
215 _autorepeat_initial_send(void *data)
216 {
217    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
218
219    if (sd->timer) ecore_timer_del(sd->timer);
220    sd->repeating = EINA_TRUE;
221    _autorepeat_send(data);
222    sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
223
224    return ECORE_CALLBACK_CANCEL;
225 }
226
227 static void
228 _on_pressed_signal(void *data,
229                    Evas_Object *obj __UNUSED__,
230                    const char *emission __UNUSED__,
231                    const char *source __UNUSED__)
232 {
233    ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
234
235    if ((sd->autorepeat) && (!sd->repeating))
236      {
237         if (sd->ar_threshold <= 0.0)
238           _autorepeat_initial_send(data);  /* call immediately */
239         else
240           sd->timer = ecore_timer_add
241               (sd->ar_threshold, _autorepeat_initial_send, data);
242      }
243
244    evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
245 }
246
247 static void
248 _on_unpressed_signal(void *data,
249                      Evas_Object *obj __UNUSED__,
250                      const char *emission __UNUSED__,
251                      const char *source __UNUSED__)
252 {
253    ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
254
255    if (sd->timer)
256      {
257         ecore_timer_del(sd->timer);
258         sd->timer = NULL;
259      }
260    sd->repeating = EINA_FALSE;
261    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
262 }
263
264 static char *
265 _access_info_cb(void *data __UNUSED__,
266                 Evas_Object *obj,
267                 Elm_Widget_Item *item __UNUSED__)
268 {
269    const char *txt = elm_widget_access_info_get(obj);
270
271    if (!txt) txt = elm_layout_text_get(obj, NULL);
272    if (txt) return strdup(txt);
273
274    return NULL;
275 }
276
277 static char *
278 _access_state_cb(void *data __UNUSED__,
279                  Evas_Object *obj,
280                  Elm_Widget_Item *item __UNUSED__)
281 {
282    if (elm_widget_disabled_get(obj))
283      return strdup(E_("State: Disabled"));
284
285    return NULL;
286 }
287
288 static void
289 _elm_button_smart_add(Evas_Object *obj)
290 {
291    EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
292
293    ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
294
295    edje_object_signal_callback_add
296      (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,click", "",
297      _on_clicked_signal, obj);
298    edje_object_signal_callback_add
299      (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,press", "",
300      _on_pressed_signal, obj);
301    edje_object_signal_callback_add
302      (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,unpress", "",
303      _on_unpressed_signal, obj);
304
305    _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
306    _elm_access_text_set
307      (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
308    _elm_access_callback_set
309      (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
310    _elm_access_callback_set
311      (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
312
313    elm_widget_can_focus_set(obj, EINA_TRUE);
314
315    elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
316 }
317
318 static void
319 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
320 {
321    ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
322
323    ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
324    ELM_WIDGET_CLASS(sc)->theme = _elm_button_smart_theme;
325    ELM_WIDGET_CLASS(sc)->sub_object_del = _elm_button_smart_sub_object_del;
326
327    /* not a 'focus chain manager' */
328    ELM_WIDGET_CLASS(sc)->focus_next = NULL;
329    ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
330
331    ELM_CONTAINER_CLASS(sc)->content_set = _elm_button_smart_content_set;
332
333    ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
334
335    ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
336    ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
337 }
338
339 EAPI const Elm_Button_Smart_Class *
340 elm_button_smart_class_get(void)
341 {
342    static Elm_Button_Smart_Class _sc =
343      ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(BUTTON_SMART_NAME);
344    static const Elm_Button_Smart_Class *class = NULL;
345
346    if (class) return class;
347
348    _elm_button_smart_set(&_sc);
349    class = &_sc;
350
351    return class;
352 }
353
354 /* new class here just to take advantage of
355  * ELM_BUTTON_SMART_CLASS_INIT() macro, which sets auto-repeat capability */
356 EVAS_SMART_SUBCLASS_NEW
357   (BUTTON_SMART_NAME, _elm_button_widget, Elm_Button_Smart_Class,
358   Elm_Button_Smart_Class, elm_button_smart_class_get, NULL);
359
360 static void
361 _elm_button_widget_smart_set_user(Elm_Button_Smart_Class *sc __UNUSED__)
362 {
363    /* NOP: declared because it's obligatory for Evas macro */
364 }
365
366 EAPI Evas_Object *
367 elm_button_add(Evas_Object *parent)
368 {
369    Evas *e;
370    Evas_Object *obj;
371
372    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
373
374    e = evas_object_evas_get(parent);
375    if (!e) return NULL;
376
377    obj = evas_object_smart_add(e, _elm_button_widget_smart_class_new());
378
379    if (!elm_widget_sub_object_add(parent, obj))
380      ERR("could not add %p as sub object of %p", obj, parent);
381
382    return obj;
383 }
384
385 EAPI void
386 elm_button_autorepeat_set(Evas_Object *obj,
387                           Eina_Bool on)
388 {
389    ELM_BUTTON_CHECK(obj);
390    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
391
392    if (sd->timer)
393      {
394         ecore_timer_del(sd->timer);
395         sd->timer = NULL;
396      }
397    sd->autorepeat = on;
398    sd->repeating = EINA_FALSE;
399 }
400
401 #define _AR_CAPABLE(_sd) \
402   (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
403
404 EAPI Eina_Bool
405 elm_button_autorepeat_get(const Evas_Object *obj)
406 {
407    ELM_BUTTON_CHECK(obj) EINA_FALSE;
408    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
409
410    return _AR_CAPABLE(sd) & sd->autorepeat;
411 }
412
413 EAPI void
414 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
415                                           double t)
416 {
417    ELM_BUTTON_CHECK(obj);
418    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
419
420    if (!_AR_CAPABLE(sd))
421      {
422         ERR("this widget does not support auto repetition of clicks.");
423         return;
424      }
425
426    if (sd->ar_threshold == t) return;
427    if (sd->timer)
428      {
429         ecore_timer_del(sd->timer);
430         sd->timer = NULL;
431      }
432    sd->ar_threshold = t;
433 }
434
435 EAPI double
436 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
437 {
438    ELM_BUTTON_CHECK(obj) 0.0;
439    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
440
441    if (!_AR_CAPABLE(sd)) return 0.0;
442
443    return sd->ar_threshold;
444 }
445
446 EAPI void
447 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
448                                       double t)
449 {
450    ELM_BUTTON_CHECK(obj);
451    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
452
453    if (!_AR_CAPABLE(sd))
454      {
455         ERR("this widget does not support auto repetition of clicks.");
456         return;
457      }
458
459    if (sd->ar_interval == t) return;
460
461    sd->ar_interval = t;
462    if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
463 }
464
465 EAPI double
466 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
467 {
468    ELM_BUTTON_CHECK(obj) 0.0;
469    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
470
471    return sd->ar_interval;
472 }