Initialize Tizen 2.3
[framework/uifw/elementary.git] / wearable / 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(obj, 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_OR_RETURN(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    if (!elm_widget_disabled_get(obj) &&
82        !evas_object_freeze_events_get(obj))
83      evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
84
85    return EINA_TRUE;
86 }
87
88 /* FIXME: replicated from elm_layout just because button's icon spot
89  * is elm.swallow.content, not elm.swallow.icon. Fix that whenever we
90  * can changed the theme API */
91 static void
92 _icon_signal_emit(Evas_Object *obj)
93 {
94    char buf[64];
95
96    snprintf(buf, sizeof(buf), "elm,state,icon,%s",
97             elm_layout_content_get(obj, "icon") ? "visible" : "hidden");
98
99    elm_layout_signal_emit(obj, buf, "elm");
100    edje_object_message_signal_process(elm_layout_edje_get(obj));
101    _elm_button_smart_sizing_eval(obj);
102 }
103
104 // FIXME: There are applications which do not use elm_win as top widget.
105 // This is workaround! Those could not use focus!
106 static Eina_Bool _focus_enabled(Evas_Object *obj)
107 {
108    if (!elm_widget_focus_get(obj)) return EINA_FALSE;
109
110    const Evas_Object *win = elm_widget_top_get(obj);
111    const char *type = evas_object_type_get(win);
112
113    if (type && !strcmp(type, "elm_win"))
114      {
115         return elm_win_focus_highlight_enabled_get(win);
116      }
117    return EINA_FALSE;
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 void
166 _on_clicked_signal(void *data,
167                    Evas_Object *obj __UNUSED__,
168                    const char *emission __UNUSED__,
169                    const char *source __UNUSED__)
170 {
171    _activate(data);
172 }
173
174 static Eina_Bool
175 _autorepeat_send(void *data)
176 {
177    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
178
179    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
180    if (!sd->repeating)
181      {
182         sd->timer = NULL;
183         return ECORE_CALLBACK_CANCEL;
184      }
185
186    return ECORE_CALLBACK_RENEW;
187 }
188
189 static Eina_Bool
190 _autorepeat_initial_send(void *data)
191 {
192    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(data, sd, ECORE_CALLBACK_CANCEL);
193
194    if (sd->timer) ecore_timer_del(sd->timer);
195    sd->repeating = EINA_TRUE;
196    _autorepeat_send(data);
197    sd->timer = ecore_timer_add(sd->ar_interval, _autorepeat_send, data);
198
199    return ECORE_CALLBACK_CANCEL;
200 }
201
202 static void
203 _multi_up_cb(void *data __UNUSED__,
204              Evas *evas __UNUSED__,
205              Evas_Object *obj,
206              void *event_info __UNUSED__)
207 {
208    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
209    sd->multi_down--;
210    if (sd->multi_down == 0)
211      {
212         //Emitting signal to inform edje about last multi up event.
213         elm_layout_signal_emit(obj, "elm,action,multi,up", "elm");
214      }
215 }
216
217 static void
218 _multi_down_cb(void *data __UNUSED__,
219                Evas *evas __UNUSED__,
220                Evas_Object *obj,
221                void *event_info __UNUSED__)
222 {
223    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
224    //Emitting signal to inform edje about multi down event.
225    elm_layout_signal_emit(obj, "elm,action,multi,down", "elm");
226    sd->multi_down++;
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    if ((sd->autorepeat) && (!sd->repeating))
237      {
238         if (sd->ar_threshold <= 0.0)
239           _autorepeat_initial_send(data);  /* call immediately */
240         else
241           {
242              if (sd->timer) ecore_timer_del(sd->timer);
243              sd->timer = ecore_timer_add
244                 (sd->ar_threshold, _autorepeat_initial_send, data);
245           }
246      }
247
248    evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
249 }
250
251 static void
252 _on_unpressed_signal(void *data,
253                      Evas_Object *obj __UNUSED__,
254                      const char *emission __UNUSED__,
255                      const char *source __UNUSED__)
256 {
257    ELM_BUTTON_DATA_GET_OR_RETURN(data, sd);
258
259    if (sd->timer)
260      {
261         ecore_timer_del(sd->timer);
262         sd->timer = NULL;
263      }
264    sd->repeating = EINA_FALSE;
265    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
266 }
267
268 static Eina_Bool
269 _elm_button_smart_event(Evas_Object *obj,
270                         Evas_Object *src __UNUSED__,
271                         Evas_Callback_Type type,
272                         void *event_info)
273 {
274    Evas_Event_Key_Down *ev = event_info;
275
276    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
277
278    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
279    if (!_focus_enabled(obj)) return EINA_FALSE;
280    if ((strcmp(ev->keyname, "Return")) &&
281        (strcmp(ev->keyname, "KP_Enter")))
282      return EINA_FALSE;
283
284    if (type == EVAS_CALLBACK_KEY_DOWN)
285      {
286         elm_layout_signal_emit(obj, "elm,anim,activate", "elm");
287         elm_layout_signal_emit(obj, "elm,action,pressed", "elm");
288         _on_pressed_signal(obj, NULL, NULL, NULL);
289      }
290    else if (type == EVAS_CALLBACK_KEY_UP)
291      {
292         elm_layout_signal_emit(obj, "elm,action,unpressed", "elm");
293         _on_unpressed_signal(obj, NULL, NULL, NULL);
294         _activate(obj);
295      }
296    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
297    return EINA_TRUE;
298 }
299
300 static char *
301 _access_info_cb(void *data __UNUSED__, Evas_Object *obj)
302 {
303    const char *txt = elm_widget_access_info_get(obj);
304
305    if (!txt) txt = elm_layout_text_get(obj, NULL);
306    if (txt) return strdup(txt);
307
308    return NULL;
309 }
310
311 static char *
312 _access_state_cb(void *data __UNUSED__, Evas_Object *obj)
313 {
314    if (elm_widget_disabled_get(obj))
315      return strdup(E_("State: Disabled"));
316
317    return NULL;
318 }
319
320 static void
321 _elm_button_smart_add(Evas_Object *obj)
322 {
323    EVAS_SMART_DATA_ALLOC(obj, Elm_Button_Smart_Data);
324
325    ELM_WIDGET_CLASS(_elm_button_parent_sc)->base.add(obj);
326 }
327
328 static void
329 _elm_button_smart_set_user(Elm_Button_Smart_Class *sc)
330 {
331    ELM_WIDGET_CLASS(sc)->base.add = _elm_button_smart_add;
332
333    ELM_WIDGET_CLASS(sc)->event = _elm_button_smart_event;
334    ELM_WIDGET_CLASS(sc)->theme = _elm_button_smart_theme;
335    ELM_WIDGET_CLASS(sc)->sub_object_del = _elm_button_smart_sub_object_del;
336
337    /* not a 'focus chain manager' */
338    ELM_WIDGET_CLASS(sc)->focus_next = NULL;
339    ELM_WIDGET_CLASS(sc)->focus_direction_manager_is = NULL;
340    ELM_WIDGET_CLASS(sc)->focus_direction = NULL;
341
342    ELM_CONTAINER_CLASS(sc)->content_set = _elm_button_smart_content_set;
343
344    ELM_LAYOUT_CLASS(sc)->sizing_eval = _elm_button_smart_sizing_eval;
345    ELM_WIDGET_CLASS(sc)->activate = _elm_button_smart_activate;
346
347    ELM_LAYOUT_CLASS(sc)->content_aliases = _content_aliases;
348    ELM_LAYOUT_CLASS(sc)->text_aliases = _text_aliases;
349
350    sc->admits_autorepeat = EINA_TRUE;
351 }
352
353 EAPI const Elm_Button_Smart_Class *
354 elm_button_smart_class_get(void)
355 {
356    static Elm_Button_Smart_Class _sc =
357      ELM_BUTTON_SMART_CLASS_INIT_NAME_VERSION(ELM_BUTTON_SMART_NAME);
358    static const Elm_Button_Smart_Class *class = NULL;
359    Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
360
361    if (class) return class;
362
363    _elm_button_smart_set(&_sc);
364    esc->callbacks = _smart_callbacks;
365    class = &_sc;
366
367    return class;
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_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    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, NULL);
384    sd->multi_down = 0;
385
386    elm_layout_theme_set(obj, "button", "base", elm_widget_style_get(obj));
387
388    edje_object_signal_callback_add
389      (ELM_WIDGET_DATA(sd)->resize_obj, "elm,action,click", "",
390      _on_clicked_signal, obj);
391    edje_object_signal_callback_add
392      (ELM_WIDGET_DATA(sd)->resize_obj, "elm,action,press", "",
393      _on_pressed_signal, obj);
394    edje_object_signal_callback_add
395      (ELM_WIDGET_DATA(sd)->resize_obj, "elm,action,unpress", "",
396      _on_unpressed_signal, obj);
397
398    /*Registering Multi down/up events to ignore mouse down/up events untill all
399     * multi down/up events are released.*/
400    evas_object_event_callback_add(obj, EVAS_CALLBACK_MULTI_DOWN, (Evas_Object_Event_Cb)_multi_down_cb, NULL);
401    evas_object_event_callback_add(obj, EVAS_CALLBACK_MULTI_UP, (Evas_Object_Event_Cb)_multi_up_cb, NULL);
402
403    _elm_access_object_register(obj, ELM_WIDGET_DATA(sd)->resize_obj);
404    _elm_access_text_set
405      (_elm_access_object_get(obj), ELM_ACCESS_TYPE, E_("Button"));
406    _elm_access_callback_set
407      (_elm_access_object_get(obj), ELM_ACCESS_INFO, _access_info_cb, NULL);
408    _elm_access_callback_set
409      (_elm_access_object_get(obj), ELM_ACCESS_STATE, _access_state_cb, sd);
410
411    elm_widget_can_focus_set(obj, EINA_TRUE);
412
413    //Tizen Only: This should be removed when eo is applied.
414    ELM_WIDGET_DATA_GET(obj, wsd);
415    wsd->on_create = EINA_FALSE;
416
417    return obj;
418 }
419
420 EAPI void
421 elm_button_autorepeat_set(Evas_Object *obj,
422                           Eina_Bool on)
423 {
424    ELM_BUTTON_CHECK(obj);
425    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
426
427    if (sd->timer)
428      {
429         ecore_timer_del(sd->timer);
430         sd->timer = NULL;
431      }
432    sd->autorepeat = on;
433    sd->repeating = EINA_FALSE;
434 }
435
436 #define _AR_CAPABLE(_sd) \
437   (ELM_BUTTON_CLASS(ELM_WIDGET_DATA(_sd)->api)->admits_autorepeat)
438
439 EAPI Eina_Bool
440 elm_button_autorepeat_get(const Evas_Object *obj)
441 {
442    ELM_BUTTON_CHECK(obj) EINA_FALSE;
443    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
444
445    return _AR_CAPABLE(sd) & sd->autorepeat;
446 }
447
448 EAPI void
449 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj,
450                                           double t)
451 {
452    ELM_BUTTON_CHECK(obj);
453    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
454
455    if (!_AR_CAPABLE(sd))
456      {
457         ERR("this widget does not support auto repetition of clicks.");
458         return;
459      }
460
461    if (sd->ar_threshold == t) return;
462    if (sd->timer)
463      {
464         ecore_timer_del(sd->timer);
465         sd->timer = NULL;
466      }
467    sd->ar_threshold = t;
468 }
469
470 EAPI double
471 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
472 {
473    ELM_BUTTON_CHECK(obj) 0.0;
474    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
475
476    if (!_AR_CAPABLE(sd)) return 0.0;
477
478    return sd->ar_threshold;
479 }
480
481 EAPI void
482 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj,
483                                       double t)
484 {
485    ELM_BUTTON_CHECK(obj);
486    ELM_BUTTON_DATA_GET_OR_RETURN(obj, sd);
487
488    if (!_AR_CAPABLE(sd))
489      {
490         ERR("this widget does not support auto repetition of clicks.");
491         return;
492      }
493
494    if (sd->ar_interval == t) return;
495
496    sd->ar_interval = t;
497    if ((sd->repeating) && (sd->timer)) ecore_timer_interval_set(sd->timer, t);
498 }
499
500 EAPI double
501 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
502 {
503    ELM_BUTTON_CHECK(obj) 0.0;
504    ELM_BUTTON_DATA_GET_OR_RETURN_VAL(obj, sd, 0.0);
505
506    return sd->ar_interval;
507 }