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