And let's not expose internal objects when setting signal callbacks on 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    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    const char *str;
98    if (!wd) return;
99    _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
100    if (wd->icon)
101      edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
102    if (wd->label)
103      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
104    else
105      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
106    if (wd->icon)
107      edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
108    else
109      edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
110    edje_object_part_text_set(wd->btn, "elm.text", wd->label);
111    if (elm_object_disabled_get(obj))
112      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
113    edje_object_message_signal_process(wd->btn);
114    edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
115    str = edje_object_data_get(wd->btn, "focus_highlight");
116    if ((str) && (!strcmp(str, "on")))
117      elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
118    else
119      elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
120    _sizing_eval(obj);
121 }
122
123 static void
124 _disable_hook(Evas_Object *obj)
125 {
126    Widget_Data *wd = elm_widget_data_get(obj);
127    if (!wd) return;
128    if (elm_widget_disabled_get(obj))
129      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
130    else
131      edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
132 }
133
134 static void
135 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
136 {
137    Widget_Data *wd = elm_widget_data_get(obj);
138    if (!wd) return;
139    edje_object_signal_emit(wd->btn, emission, source);
140 }
141
142 static void
143 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
144 {
145    Widget_Data *wd = elm_widget_data_get(obj);
146    if (!wd) return;
147    edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
148 }
149
150 static void
151 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
152 {
153    Widget_Data *wd = elm_widget_data_get(obj);
154    edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
155                                         data);
156 }
157
158 static void
159 _sizing_eval(Evas_Object *obj)
160 {
161    Widget_Data *wd = elm_widget_data_get(obj);
162    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
163
164    if (!wd) return;
165    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
166    edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
167    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
168    evas_object_size_hint_min_set(obj, minw, minh);
169    evas_object_size_hint_max_set(obj, maxw, maxh);
170 }
171
172 static void
173 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
174 {
175    Widget_Data *wd = elm_widget_data_get(data);
176    if (!wd) return;
177    if (obj != wd->icon) return;
178    _sizing_eval(data);
179 }
180
181 static void
182 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
183 {
184    Widget_Data *wd = elm_widget_data_get(obj);
185    Evas_Object *sub = event_info;
186    if (!wd) return;
187    if (sub == wd->icon)
188      {
189         edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
190         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
191                                        _changed_size_hints, obj);
192         wd->icon = NULL;
193         edje_object_message_signal_process(wd->btn);
194         _sizing_eval(obj);
195      }
196 }
197
198 static void
199 _activate(Evas_Object *obj)
200 {
201    Widget_Data *wd = elm_widget_data_get(obj);
202    if (!wd) return;
203    if (wd->timer)
204      {
205         ecore_timer_del(wd->timer);
206         wd->timer = NULL;
207      }
208    wd->repeating = EINA_FALSE;
209    evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
210    _signal_unpressed(obj, wd->btn, NULL, NULL); /* safe guard when the theme does not emit the 'unpress' signal */
211 }
212
213 static void
214 _activate_hook(Evas_Object *obj)
215 {
216    _activate(obj);
217 }
218
219 static void
220 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
221 {
222    _activate(data);
223 }
224
225 static Eina_Bool
226 _autorepeat_send(void *data)
227 {
228    Widget_Data *wd = elm_widget_data_get(data);
229    if (!wd) return ECORE_CALLBACK_CANCEL;
230
231    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
232    if (!wd->repeating)
233      {
234         wd->timer = NULL;
235         return ECORE_CALLBACK_CANCEL;
236      }
237
238    return ECORE_CALLBACK_RENEW;
239 }
240
241 static Eina_Bool
242 _autorepeat_initial_send(void *data)
243 {
244    Widget_Data *wd = elm_widget_data_get(data);
245    if (!wd) return ECORE_CALLBACK_CANCEL;
246
247    if (wd->timer) ecore_timer_del(wd->timer);
248    wd->repeating = EINA_TRUE;
249    _autorepeat_send(data);
250    wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
251
252    return ECORE_CALLBACK_CANCEL;
253 }
254
255 static void
256 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
257 {
258    Widget_Data *wd = elm_widget_data_get(data);
259    if (!wd) return;
260
261    if ((wd->autorepeat) && (!wd->repeating))
262      {
263         if (wd->ar_threshold <= 0.0)
264           _autorepeat_initial_send(data); /* call immediately */
265         else
266           wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
267      }
268 }
269
270 static void
271 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
272 {
273    Widget_Data *wd = elm_widget_data_get(data);
274    if (!wd) return;
275
276    if (wd->timer)
277      {
278         ecore_timer_del(wd->timer);
279         wd->timer = NULL;
280      }
281    wd->repeating = EINA_FALSE;
282    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
283 }
284
285 /**
286  * Add a new button to the parent
287  * @param parent The parent object
288  * @return The new object or NULL if it cannot be created
289  *
290  * @ingroup Button
291  */
292 EAPI Evas_Object *
293 elm_button_add(Evas_Object *parent)
294 {
295    Evas_Object *obj;
296    Evas *e;
297    Widget_Data *wd;
298
299    wd = ELM_NEW(Widget_Data);
300    e = evas_object_evas_get(parent);
301    obj = elm_widget_add(e);
302    ELM_SET_WIDTYPE(widtype, "button");
303    elm_widget_type_set(obj, "button");
304    elm_widget_sub_object_add(parent, obj);
305    elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
306    elm_widget_data_set(obj, wd);
307    elm_widget_del_hook_set(obj, _del_hook);
308    elm_widget_theme_hook_set(obj, _theme_hook);
309    elm_widget_disable_hook_set(obj, _disable_hook);
310    elm_widget_can_focus_set(obj, EINA_TRUE);
311    elm_widget_activate_hook_set(obj, _activate_hook);
312    elm_widget_event_hook_set(obj, _event_hook);
313    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
314    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
315    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
316
317    wd->btn = edje_object_add(e);
318    _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
319    edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
320                                    _signal_clicked, obj);
321    edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
322                                    _signal_pressed, obj);
323    edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
324                                    _signal_unpressed, obj);
325    elm_widget_resize_object_set(obj, wd->btn);
326
327    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
328
329    _theme_hook(obj);
330
331    // TODO: convert Elementary to subclassing of Evas_Smart_Class
332    // TODO: and save some bytes, making descriptions per-class and not instance!
333    evas_object_smart_callbacks_descriptions_set(obj, _signals);
334    return obj;
335 }
336
337 /**
338  * Set the label used in the button
339  *
340  * @param obj The button object
341  * @param label The text will be written on the button
342  *
343  * @ingroup Button
344  */
345 EAPI void
346 elm_button_label_set(Evas_Object *obj, const char *label)
347 {
348    ELM_CHECK_WIDTYPE(obj, widtype);
349    Widget_Data *wd = elm_widget_data_get(obj);
350    if (!wd) return;
351    eina_stringshare_replace(&wd->label, label);
352    if (label)
353      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
354    else
355      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
356    edje_object_message_signal_process(wd->btn);
357    edje_object_part_text_set(wd->btn, "elm.text", label);
358    _sizing_eval(obj);
359 }
360
361 EAPI const char *
362 elm_button_label_get(const Evas_Object *obj)
363 {
364    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
365    Widget_Data *wd = elm_widget_data_get(obj);
366    if (!wd) return NULL;
367    return wd->label;
368 }
369
370 /**
371  * Set the icon used for the button
372  *
373  * Once the icon object is set, a previously set one will be deleted
374  * If you want to keep that old content object, use the
375  * elm_button_icon_unset() function.
376  *
377  * @param obj The button object
378  * @param icon The icon object for the button
379  *
380  * @ingroup Button
381  */
382 EAPI void
383 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
384 {
385    ELM_CHECK_WIDTYPE(obj, widtype);
386    Widget_Data *wd = elm_widget_data_get(obj);
387    if (!wd) return;
388    if (wd->icon == icon) return;
389    if (wd->icon) evas_object_del(wd->icon);
390    wd->icon = icon;
391    if (icon)
392      {
393         elm_widget_sub_object_add(obj, icon);
394         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
395                                        _changed_size_hints, obj);
396         edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
397         edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
398         edje_object_message_signal_process(wd->btn);
399      }
400    _sizing_eval(obj);
401 }
402
403 /**
404  * Get the icon used for the button
405  *
406  * Return the icon object which is set for this widget.
407  *
408  * @param obj The button object
409  * @return The icon object that is being used
410  *
411  * @ingroup Button
412  */
413 EAPI Evas_Object *
414 elm_button_icon_get(const Evas_Object *obj)
415 {
416    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
417    Widget_Data *wd = elm_widget_data_get(obj);
418    if (!wd) return NULL;
419    return wd->icon;
420 }
421
422 /**
423  * Unset the icon used for the button
424  *
425  * Unparent and return the icon object which was set for this widget.
426  *
427  * @param obj The button object
428  * @return The icon object that was being used
429  *
430  * @ingroup Button
431  */
432 EAPI Evas_Object *
433 elm_button_icon_unset(Evas_Object *obj)
434 {
435    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
436    Widget_Data *wd = elm_widget_data_get(obj);
437    if (!wd) return NULL;
438    if (!wd->icon) return NULL;
439    Evas_Object *icon = wd->icon;
440    elm_widget_sub_object_del(obj, wd->icon);
441    edje_object_part_unswallow(wd->btn, wd->icon);
442    wd->icon = NULL;
443    return icon;
444 }
445
446 /**
447  * Turn on/off the autorepeat event generated when the user keeps pressing on the button
448  *
449  * @param obj The button object
450  * @param on  A bool to turn on/off the event
451  *
452  * @ingroup Button
453  */
454 EAPI void
455 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
456 {
457    ELM_CHECK_WIDTYPE(obj, widtype);
458    Widget_Data *wd = elm_widget_data_get(obj);
459    if (!wd) return;
460    if (wd->timer)
461      {
462         ecore_timer_del(wd->timer);
463         wd->timer = NULL;
464      }
465    wd->autorepeat = on;
466    wd->repeating = EINA_FALSE;
467 }
468
469 /**
470  * Get if autorepeat event is on
471  *
472  * @param obj The button object
473  * @return If autorepeat is on
474  *
475  * @ingroup Button
476  */
477 EAPI Eina_Bool
478 elm_button_autorepeat_get(const Evas_Object *obj)
479 {
480    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
481    Widget_Data *wd = elm_widget_data_get(obj);
482    if (!wd) return EINA_FALSE;
483    return wd->autorepeat;
484 }
485
486 /**
487  * Set the initial timeout before the autorepeat event is generated
488  *
489  * @param obj The button object
490  * @param t   Timeout
491  *
492  * @ingroup Button
493  */
494 EAPI void
495 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
496 {
497    ELM_CHECK_WIDTYPE(obj, widtype);
498    Widget_Data *wd = elm_widget_data_get(obj);
499    if (!wd) return;
500    if (wd->ar_threshold == t) return;
501    if (wd->timer)
502      {
503         ecore_timer_del(wd->timer);
504         wd->timer = NULL;
505      }
506    wd->ar_threshold = t;
507 }
508
509 /**
510  * Get the initial timeout before the autorepeat event is generated
511  *
512  * @param obj The button object
513  * @return Timeout
514  *
515  * @ingroup Button
516  */
517 EAPI double
518 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
519 {
520    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
521    Widget_Data *wd = elm_widget_data_get(obj);
522    if (!wd) return 0.0;
523    return wd->ar_threshold;
524 }
525
526 /**
527  * Set the interval between each generated autorepeat event
528  *
529  * @param obj The button object
530  * @param t   Interval
531  *
532  * @ingroup Button
533  */
534 EAPI void
535 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
536 {
537    ELM_CHECK_WIDTYPE(obj, widtype);
538    Widget_Data *wd = elm_widget_data_get(obj);
539    if (!wd) return;
540    if (wd->ar_interval == t) return;
541
542    wd->ar_interval = t;
543    if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
544 }
545
546 /**
547  * Get the interval between each generated autorepeat event
548  *
549  * @param obj The button object
550  * @return Interval
551  *
552  * @ingroup Button
553  */
554 EAPI double
555 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
556 {
557    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
558    Widget_Data *wd = elm_widget_data_get(obj);
559    if (!wd) return 0.0;
560    return wd->ar_interval;
561 }