Fix: _event_hook must return EINA_FALSE if widget is disable
[platform/upstream/elementary.git] / src / lib / elm_button.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Button Button
6  *
7  * This is a push-button. Press it and run some function. It can contain
8  * a simple label and icon object.
9  */
10
11 typedef struct _Widget_Data Widget_Data;
12
13 struct _Widget_Data
14 {
15    Evas_Object *btn, *icon;
16    const char *label;
17    Eina_Bool autorepeat;
18    Eina_Bool repeating;
19    double ar_threshold;
20    double ar_interval;
21    Ecore_Timer *timer;
22 };
23
24 static const char *widtype = NULL;
25 static void _del_hook(Evas_Object *obj);
26 static void _theme_hook(Evas_Object *obj);
27 static void _disable_hook(Evas_Object *obj);
28 static void _sizing_eval(Evas_Object *obj);
29 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
30 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
31 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
32 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
33 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
34 static void _on_focus_hook(void *data, Evas_Object *obj);
35 static void _activate(Evas_Object *obj);
36 static void _activate_hook(Evas_Object *obj);
37 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
38                              Evas_Callback_Type type, void *event_info);
39
40 static const char SIG_CLICKED[] = "clicked";
41 static const char SIG_REPEATED[] = "repeated";
42 static const char SIG_UNPRESSED[] = "unpressed";
43 static const Evas_Smart_Cb_Description _signals[] = {
44   {SIG_CLICKED, ""},
45   {SIG_REPEATED, ""},
46   {SIG_UNPRESSED, ""},
47   {NULL, NULL}
48 };
49
50 static Eina_Bool
51 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
52 {
53    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
54    Evas_Event_Key_Down *ev = event_info;
55    Widget_Data *wd = elm_widget_data_get(obj);
56    if (!wd) return EINA_FALSE;
57    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
58    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
59    if (strcmp(ev->keyname, "Return") && strcmp(ev->keyname, "space"))
60      return EINA_FALSE;
61    _activate(obj);
62    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
63    edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
64    return EINA_TRUE;
65 }
66
67 static void
68 _del_hook(Evas_Object *obj)
69 {
70    Widget_Data *wd = elm_widget_data_get(obj);
71    if (!wd) return;
72    if (wd->label) eina_stringshare_del(wd->label);
73    free(wd);
74 }
75
76 static void
77 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
78 {
79    Widget_Data *wd = elm_widget_data_get(obj);
80    if (!wd) return;
81    if (elm_widget_focus_get(obj))
82      {
83         edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
84         evas_object_focus_set(wd->btn, EINA_TRUE);
85      }
86    else
87      {
88         edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
89         evas_object_focus_set(wd->btn, EINA_FALSE);
90      }
91 }
92
93 static void
94 _theme_hook(Evas_Object *obj)
95 {
96    Widget_Data *wd = elm_widget_data_get(obj);
97    if (!wd) return;
98    _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
99    if (wd->icon)
100      edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
101    if (wd->label)
102      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
103    else
104      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
105    if (wd->icon)
106      edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
107    else
108      edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
109    edje_object_part_text_set(wd->btn, "elm.text", wd->label);
110    edje_object_message_signal_process(wd->btn);
111    edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
112    _sizing_eval(obj);
113 }
114
115 static void
116 _disable_hook(Evas_Object *obj)
117 {
118    Widget_Data *wd = elm_widget_data_get(obj);
119    if (!wd) return;
120    if (elm_widget_disabled_get(obj))
121      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
122    else
123      edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
124 }
125
126 static void
127 _sizing_eval(Evas_Object *obj)
128 {
129    Widget_Data *wd = elm_widget_data_get(obj);
130    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
131
132    if (!wd) return;
133    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
134    edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
135    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
136    evas_object_size_hint_min_set(obj, minw, minh);
137    evas_object_size_hint_max_set(obj, maxw, maxh);
138 }
139
140 static void
141 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
142 {
143    Widget_Data *wd = elm_widget_data_get(data);
144    if (!wd) return;
145    if (obj != wd->icon) return;
146    _sizing_eval(data);
147 }
148
149 static void
150 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
151 {
152    Widget_Data *wd = elm_widget_data_get(obj);
153    Evas_Object *sub = event_info;
154    if (!wd) return;
155    if (sub == wd->icon)
156      {
157         edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
158         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
159                                        _changed_size_hints, obj);
160         wd->icon = NULL;
161         edje_object_message_signal_process(wd->btn);
162         _sizing_eval(obj);
163      }
164 }
165
166 static void
167 _activate(Evas_Object *obj)
168 {
169    Widget_Data *wd = elm_widget_data_get(obj);
170    if (!wd) return;
171    if (wd->timer)
172      {
173         ecore_timer_del(wd->timer);
174         wd->timer = NULL;
175      }
176    wd->repeating = EINA_FALSE;
177    evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
178    _signal_unpressed(obj, wd->btn, NULL, NULL); /* safe guard when the theme does not emit the 'unpress' signal */
179 }
180
181 static void
182 _activate_hook(Evas_Object *obj)
183 {
184    _activate(obj);
185 }
186
187 static void
188 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
189 {
190    _activate(data);
191 }
192
193 static Eina_Bool
194 _autorepeat_send(void *data)
195 {
196    Widget_Data *wd = elm_widget_data_get(data);
197    if (!wd) return ECORE_CALLBACK_CANCEL;
198
199    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
200    if (!wd->repeating)
201      {
202         wd->timer = NULL;
203         return ECORE_CALLBACK_CANCEL;
204      }
205
206    return ECORE_CALLBACK_RENEW;
207 }
208
209 static Eina_Bool
210 _autorepeat_initial_send(void *data)
211 {
212    Widget_Data *wd = elm_widget_data_get(data);
213    if (!wd) return ECORE_CALLBACK_CANCEL;
214
215    if (wd->timer) ecore_timer_del(wd->timer);
216    wd->repeating = EINA_TRUE;
217    _autorepeat_send(data);
218    wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
219
220    return ECORE_CALLBACK_CANCEL;
221 }
222
223 static void
224 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
225 {
226    Widget_Data *wd = elm_widget_data_get(data);
227    if (!wd) return;
228
229    if (wd->autorepeat && !wd->repeating)
230      {
231         if (wd->ar_threshold <= 0.0)
232           _autorepeat_initial_send(data); /* call immediately */
233         else
234           wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
235      }
236 }
237
238 static void
239 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
240 {
241    Widget_Data *wd = elm_widget_data_get(data);
242    if (!wd) return;
243
244    if (wd->timer)
245      {
246         ecore_timer_del(wd->timer);
247         wd->timer = NULL;
248      }
249    wd->repeating = EINA_FALSE;
250    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
251 }
252
253 /**
254  * Add a new button to the parent
255  * @param parent The parent object
256  * @return The new object or NULL if it cannot be created
257  *
258  * @ingroup Button
259  */
260 EAPI Evas_Object *
261 elm_button_add(Evas_Object *parent)
262 {
263    Evas_Object *obj;
264    Evas *e;
265    Widget_Data *wd;
266
267    wd = ELM_NEW(Widget_Data);
268    e = evas_object_evas_get(parent);
269    obj = elm_widget_add(e);
270    ELM_SET_WIDTYPE(widtype, "button");
271    elm_widget_type_set(obj, "button");
272    elm_widget_sub_object_add(parent, obj);
273    elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
274    elm_widget_data_set(obj, wd);
275    elm_widget_del_hook_set(obj, _del_hook);
276    elm_widget_theme_hook_set(obj, _theme_hook);
277    elm_widget_disable_hook_set(obj, _disable_hook);
278    elm_widget_can_focus_set(obj, 1);                 
279    elm_widget_activate_hook_set(obj, _activate_hook);
280    elm_widget_event_hook_set(obj, _event_hook);
281
282    wd->btn = edje_object_add(e);
283    _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
284    edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
285                                    _signal_clicked, obj);
286    edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
287                                    _signal_pressed, obj);
288    edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
289                                    _signal_unpressed, obj);
290    elm_widget_resize_object_set(obj, wd->btn);
291
292    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
293
294    _sizing_eval(obj);
295
296    // TODO: convert Elementary to subclassing of Evas_Smart_Class
297    // TODO: and save some bytes, making descriptions per-class and not instance!
298    evas_object_smart_callbacks_descriptions_set(obj, _signals);
299    return obj;
300 }
301
302 /**
303  * Set the label used in the button
304  *
305  * @param obj The button object
306  * @param label The text will be written on the button
307  *
308  * @ingroup Button
309  */
310 EAPI void
311 elm_button_label_set(Evas_Object *obj, const char *label)
312 {
313    ELM_CHECK_WIDTYPE(obj, widtype);
314    Widget_Data *wd = elm_widget_data_get(obj);
315    if (!wd) return;
316    eina_stringshare_replace(&wd->label, label);
317    if (label)
318      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
319    else
320      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
321    edje_object_message_signal_process(wd->btn);
322    edje_object_part_text_set(wd->btn, "elm.text", label);
323    _sizing_eval(obj);
324 }
325
326 EAPI const char *
327 elm_button_label_get(const Evas_Object *obj)
328 {
329    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
330    Widget_Data *wd = elm_widget_data_get(obj);
331    if (!wd) return NULL;
332    return wd->label;
333 }
334
335 /**
336  * Set the icon used for the button
337  *
338  * Once the icon object is set, a previously set one will be deleted
339  *
340  * @param obj The button object
341  * @param icon The image for the button
342  *
343  * @ingroup Button
344  */
345 EAPI void
346 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
347 {
348    ELM_CHECK_WIDTYPE(obj, widtype);
349    Widget_Data *wd = elm_widget_data_get(obj);
350    if (!wd) return;
351    if (wd->icon == icon) return;
352    if (wd->icon) evas_object_del(wd->icon);
353    wd->icon = icon;
354    if (icon)
355      {
356         elm_widget_sub_object_add(obj, icon);
357         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
358                                        _changed_size_hints, obj);
359         edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
360         edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
361         edje_object_message_signal_process(wd->btn);
362      }
363    _sizing_eval(obj);
364 }
365
366 /**
367  * Get the icon used for the button
368  *
369  * @param obj The button object
370  * @return The image for the button
371  *
372  * @ingroup Button
373  */
374 EAPI Evas_Object *
375 elm_button_icon_get(const Evas_Object *obj)
376 {
377    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
378    Widget_Data *wd = elm_widget_data_get(obj);
379    if (!wd) return NULL;
380    return wd->icon;
381 }
382
383 /**
384  * Turn on/off the autorepeat event generated when the user keeps pressing on the button
385  *
386  * @param obj The button object
387  * @param on  A bool to turn on/off the event
388  *
389  * @ingroup Button
390  */
391 EAPI void
392 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
393 {
394    ELM_CHECK_WIDTYPE(obj, widtype);
395    Widget_Data *wd = elm_widget_data_get(obj);
396    if (!wd) return;
397    if (wd->timer)
398      {
399         ecore_timer_del(wd->timer);
400         wd->timer = NULL;
401      }
402    wd->autorepeat = on;
403    wd->repeating = EINA_FALSE;
404 }
405
406 /**
407  * Set the initial timeout before the autorepeat event is generated
408  *
409  * @param obj The button object
410  * @param t   Timeout
411  *
412  * @ingroup Button
413  */
414 EAPI void
415 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
416 {
417    ELM_CHECK_WIDTYPE(obj, widtype);
418    Widget_Data *wd = elm_widget_data_get(obj);
419    if (!wd) return;
420    if (wd->ar_threshold == t) return;
421    if (wd->timer)
422      {
423         ecore_timer_del(wd->timer);
424         wd->timer = NULL;
425      }
426    wd->ar_threshold = t;
427 }
428
429 /**
430  * Set the interval between each generated autorepeat event
431  *
432  * @param obj The button object
433  * @param t   Interval
434  *
435  * @ingroup Button
436  */
437 EAPI void         
438 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
439 {
440    ELM_CHECK_WIDTYPE(obj, widtype);
441    Widget_Data *wd = elm_widget_data_get(obj);
442    if (!wd) return;
443    if (wd->ar_interval == t) return;
444
445    wd->ar_interval = t;
446    if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);
447 }