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