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