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