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