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