[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.icon"},
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 static Eina_Bool
107 _elm_button_smart_event(Evas_Object *obj,
108                         Evas_Object *src __UNUSED__,
109                         Evas_Callback_Type type,
110                         void *event_info)
111 {
112    Evas_Event_Key_Down *ev = event_info;
113
114    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
115
116    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
117    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
118
119    if ((strcmp(ev->keyname, "Return")) &&
120        (strcmp(ev->keyname, "KP_Enter")) &&
121        (strcmp(ev->keyname, "space")))
122      return EINA_FALSE;
123
124    _activate(obj);
125    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
126    elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
127
128    return EINA_TRUE;
129 }
130
131 static void
132 _on_clicked_signal(void *data,
133                    Evas_Object *obj __UNUSED__,
134                    const char *emission __UNUSED__,
135                    const char *source __UNUSED__)
136 {
137    _activate(data);
138 }
139
140 static Eina_Bool
141 _autorepeat_send(void *data)
142 {
143    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
144
145    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
146    if (!sd->repeating)
147      {
148         sd->timer = NULL;
149         return ECORE_CALLBACK_CANCEL;
150      }
151
152    return ECORE_CALLBACK_RENEW;
153 }
154
155 static Eina_Bool
156 _autorepeat_initial_send(void *data)
157 {
158    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
159
160    if (sd->timer) ecore_timer_del(sd->timer);
161    sd->repeating = EINA_TRUE;
162    _autorepeat_send(data);
163    sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
164
165    return ECORE_CALLBACK_CANCEL;
166 }
167
168 static void
169 _on_pressed_signal(void *data,
170                    Evas_Object *obj __UNUSED__,
171                    const char *emission __UNUSED__,
172                    const char *source __UNUSED__)
173 {
174    ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
175
176    if ((sd->autorepeat) && (!sd->repeating))
177      {
178         if (sd->ar_threshold <= 0.0)
179           _autorepeat_initial_send(data);  /* call immediately */
180         else
181           sd->timer = ecore_timer_add
182               (sd->ar_threshold, _autorepeat_initial_send, data);
183      }
184
185    evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
186 }
187
188 static void
189 _on_unpressed_signal(void *data,
190                      Evas_Object *obj __UNUSED__,
191                      const char *emission __UNUSED__,
192                      const char *source __UNUSED__)
193 {
194    ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
195
196    if (sd->timer)
197      {
198         ecore_timer_del(sd->timer);
199         sd->timer = NULL;
200      }
201    sd->repeating = EINA_FALSE;
202    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
203 }
204
205 static char *
206 _access_info_cb(void *data __UNUSED__,
207                 Evas_Object *obj,
208                 Elm_Widget_Item *item __UNUSED__)
209 {
210    const char *txt = elm_widget_access_info_get(obj);
211
212    if (!txt) txt = elm_layout_text_get(obj, NULL);
213    if (txt) return strdup(txt);
214
215    return NULL;
216 }
217
218 static char *
219 _access_state_cb(void *data __UNUSED__,
220                  Evas_Object *obj,
221                  Elm_Widget_Item *item __UNUSED__)
222 {
223    if (elm_widget_disabled_get(obj))
224      return strdup(E_("State: Disabled"));
225
226    return NULL;
227 }
228
229 static void
230 _elm_button_smart_add(Evas_Object *obj)
231 {
232    EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
233
234    ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
235
236    edje_object_signal_callback_add
237      (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,click", "",
238      _on_clicked_signal, obj);
239    edje_object_signal_callback_add
240      (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,press", "",
241      _on_pressed_signal, obj);
242    edje_object_signal_callback_add
243      (ELM_WIDGET_DATA(priv)->resize_obj, "elm,action,unpress", "",
244      _on_unpressed_signal, obj);
245
246    _elm_access_object_register(obj, ELM_WIDGET_DATA(priv)->resize_obj);
247    _elm_access_text_set
248      (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
249    _elm_access_callback_set
250      (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
251    _elm_access_callback_set
252      (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, priv);
253
254    elm_widget_can_focus_set(obj, EINA_TRUE);
255
256    elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
257 }
258
259 static void
260 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
261 {
262    ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
263
264    ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
265    ELM_WIDGET_CLASS(sc)->focus_next = NULL; /* not 'focus chain manager' */
266
267    ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
268
269    ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
270    ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
271 }
272
273 EAPI const Elm_Button_Smart_Class *
274 elm_button_smart_class_get(void)
275 {
276    static Elm_Button_Smart_Class _sc =
277      ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(BUTTON_SMART_NAME);
278    static const Elm_Button_Smart_Class *class = NULL;
279
280    if (class) return class;
281
282    _elm_button_smart_set(&_sc);
283    class = &_sc;
284
285    return class;
286 }
287
288 /* new class here just to take advantage of
289  * ELM_BUTTON_SMART_CLASS_INIT() macro, which sets auto-repeat capability */
290 EVAS_SMART_SUBCLASS_NEW
291   (BUTTON_SMART_NAME, _elm_button_widget, Elm_Button_Smart_Class,
292   Elm_Button_Smart_Class, elm_button_smart_class_get, NULL);
293
294 static void
295 _elm_button_widget_smart_set_user(Elm_Button_Smart_Class *sc __UNUSED__)
296 {
297    /* NOP: declared because it's obligatory for Evas macro */
298 }
299
300 EAPI Evas_Object *
301 elm_button_add(Evas_Object *parent)
302 {
303    Evas *e;
304    Evas_Object *obj;
305
306    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
307
308    e = evas_object_evas_get(parent);
309    if (!e) return NULL;
310
311    obj = evas_object_smart_add(e, _elm_button_widget_smart_class_new());
312
313    if (!elm_widget_sub_object_add(parent, obj))
314      ERR("could not add %p as sub object of %p", obj, parent);
315
316    return obj;
317 }
318
319 EAPI void
320 elm_button_autorepeat_set(Evas_Object *obj,
321                           Eina_Bool on)
322 {
323    ELM_BUTTON_CHECK(obj);
324    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
325
326    if (sd->timer)
327      {
328         ecore_timer_del(sd->timer);
329         sd->timer = NULL;
330      }
331    sd->autorepeat = on;
332    sd->repeating = EINA_FALSE;
333 }
334
335 #define _AR_CAPABLE(_sd) \
336   (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
337
338 EAPI Eina_Bool
339 elm_button_autorepeat_get(const Evas_Object *obj)
340 {
341    ELM_BUTTON_CHECK(obj) EINA_FALSE;
342    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
343
344    return _AR_CAPABLE(sd) & sd->autorepeat;
345 }
346
347 EAPI void
348 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
349                                           double t)
350 {
351    ELM_BUTTON_CHECK(obj);
352    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
353
354    if (!_AR_CAPABLE(sd))
355      {
356         ERR("this widget does not support auto repetition of clicks.");
357         return;
358      }
359
360    if (sd->ar_threshold == t) return;
361    if (sd->timer)
362      {
363         ecore_timer_del(sd->timer);
364         sd->timer = NULL;
365      }
366    sd->ar_threshold = t;
367 }
368
369 EAPI double
370 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
371 {
372    ELM_BUTTON_CHECK(obj) 0.0;
373    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
374
375    if (!_AR_CAPABLE(sd)) return 0.0;
376
377    return sd->ar_threshold;
378 }
379
380 EAPI void
381 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
382                                       double t)
383 {
384    ELM_BUTTON_CHECK(obj);
385    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
386
387    if (!_AR_CAPABLE(sd))
388      {
389         ERR("this widget does not support auto repetition of clicks.");
390         return;
391      }
392
393    if (sd->ar_interval == t) return;
394
395    sd->ar_interval = t;
396    if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
397 }
398
399 EAPI double
400 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
401 {
402    ELM_BUTTON_CHECK(obj) 0.0;
403    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
404
405    return sd->ar_interval;
406 }