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