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