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