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