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