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