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