Example for button and moving docs around
[framework/uifw/elementary.git] / src / lib / elm_button.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *btn, *icon;
9    const char *label;
10    Eina_Bool autorepeat;
11    Eina_Bool repeating;
12    double ar_threshold;
13    double ar_interval;
14    Ecore_Timer *timer;
15 };
16
17 static const char *widtype = NULL;
18 static void _del_hook(Evas_Object *obj);
19 static void _theme_hook(Evas_Object *obj);
20 static void _disable_hook(Evas_Object *obj);
21 static void _sizing_eval(Evas_Object *obj);
22 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
23 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
24 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
25 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
26 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
27 static void _on_focus_hook(void *data, Evas_Object *obj);
28 static void _activate(Evas_Object *obj);
29 static void _activate_hook(Evas_Object *obj);
30 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
31                              Evas_Callback_Type type, void *event_info);
32
33 static const char SIG_CLICKED[] = "clicked";
34 static const char SIG_REPEATED[] = "repeated";
35 static const char SIG_UNPRESSED[] = "unpressed";
36 static const Evas_Smart_Cb_Description _signals[] = {
37        {SIG_CLICKED, ""},
38        {SIG_REPEATED, ""},
39        {SIG_UNPRESSED, ""},
40        {NULL, NULL}
41 };
42
43 static Eina_Bool
44 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
45 {
46    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
47    Evas_Event_Key_Down *ev = event_info;
48    Widget_Data *wd = elm_widget_data_get(obj);
49    if (!wd) return EINA_FALSE;
50    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
51    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
52    if ((strcmp(ev->keyname, "Return")) &&
53        (strcmp(ev->keyname, "KP_Enter")) &&
54        (strcmp(ev->keyname, "space")))
55      return EINA_FALSE;
56    _activate(obj);
57    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
58    edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
59    return EINA_TRUE;
60 }
61
62 static void
63 _del_hook(Evas_Object *obj)
64 {
65    Widget_Data *wd = elm_widget_data_get(obj);
66    if (!wd) return;
67    if (wd->label) eina_stringshare_del(wd->label);
68    free(wd);
69 }
70
71 static void
72 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
73 {
74    Widget_Data *wd = elm_widget_data_get(obj);
75    if (!wd) return;
76    if (elm_widget_focus_get(obj))
77      {
78         edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
79         evas_object_focus_set(wd->btn, EINA_TRUE);
80      }
81    else
82      {
83         edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
84         evas_object_focus_set(wd->btn, EINA_FALSE);
85      }
86 }
87
88 static void
89 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
90 {
91    Widget_Data *wd = elm_widget_data_get(obj);
92    if (!wd) return;
93    edje_object_mirrored_set(wd->btn, rtl);
94 }
95
96 static void
97 _theme_hook(Evas_Object *obj)
98 {
99    Widget_Data *wd = elm_widget_data_get(obj);
100    const char *str;
101    if (!wd) return;
102    _elm_widget_mirrored_reload(obj);
103    _mirrored_set(obj, elm_widget_mirrored_get(obj));
104    _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
105    if (wd->icon)
106      edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
107    if (wd->label)
108      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
109    else
110      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
111    if (wd->icon)
112      edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
113    else
114      edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
115    edje_object_part_text_set(wd->btn, "elm.text", wd->label);
116    if (elm_object_disabled_get(obj))
117      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
118    edje_object_message_signal_process(wd->btn);
119    edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
120    str = edje_object_data_get(wd->btn, "focus_highlight");
121    if ((str) && (!strcmp(str, "on")))
122      elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
123    else
124      elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
125    _sizing_eval(obj);
126 }
127
128 static void
129 _disable_hook(Evas_Object *obj)
130 {
131    Widget_Data *wd = elm_widget_data_get(obj);
132    if (!wd) return;
133    if (elm_widget_disabled_get(obj))
134      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
135    else
136      edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
137 }
138
139 static void
140 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
141 {
142    Widget_Data *wd = elm_widget_data_get(obj);
143    if (!wd) return;
144    edje_object_signal_emit(wd->btn, emission, source);
145 }
146
147 static void
148 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
149 {
150    Widget_Data *wd = elm_widget_data_get(obj);
151    if (!wd) return;
152    edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
153 }
154
155 static void
156 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
157 {
158    Widget_Data *wd = elm_widget_data_get(obj);
159    edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
160                                         data);
161 }
162
163 static void
164 _sizing_eval(Evas_Object *obj)
165 {
166    Widget_Data *wd = elm_widget_data_get(obj);
167    Evas_Coord minw = -1, minh = -1;
168
169    if (!wd) return;
170    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
171    edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
172    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
173    evas_object_size_hint_min_set(obj, minw, minh);
174 }
175
176 static void
177 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
178 {
179    Widget_Data *wd = elm_widget_data_get(data);
180    if (!wd) return;
181    if (obj != wd->icon) return;
182    _sizing_eval(data);
183 }
184
185 static void
186 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
187 {
188    Widget_Data *wd = elm_widget_data_get(obj);
189    Evas_Object *sub = event_info;
190    if (!wd) return;
191    if (sub == wd->icon)
192      {
193         edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
194         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
195                                             _changed_size_hints, obj);
196         wd->icon = NULL;
197         edje_object_message_signal_process(wd->btn);
198         _sizing_eval(obj);
199      }
200 }
201
202 static void
203 _activate(Evas_Object *obj)
204 {
205    Widget_Data *wd = elm_widget_data_get(obj);
206    if (!wd) return;
207    if (wd->timer)
208      {
209         ecore_timer_del(wd->timer);
210         wd->timer = NULL;
211      }
212    wd->repeating = EINA_FALSE;
213    evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
214 }
215
216 static void
217 _activate_hook(Evas_Object *obj)
218 {
219    _activate(obj);
220 }
221
222 static void
223 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
224 {
225    _activate(data);
226 }
227
228 static Eina_Bool
229 _autorepeat_send(void *data)
230 {
231    Widget_Data *wd = elm_widget_data_get(data);
232    if (!wd) return ECORE_CALLBACK_CANCEL;
233
234    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
235    if (!wd->repeating)
236      {
237         wd->timer = NULL;
238         return ECORE_CALLBACK_CANCEL;
239      }
240
241    return ECORE_CALLBACK_RENEW;
242 }
243
244 static Eina_Bool
245 _autorepeat_initial_send(void *data)
246 {
247    Widget_Data *wd = elm_widget_data_get(data);
248    if (!wd) return ECORE_CALLBACK_CANCEL;
249
250    if (wd->timer) ecore_timer_del(wd->timer);
251    wd->repeating = EINA_TRUE;
252    _autorepeat_send(data);
253    wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
254
255    return ECORE_CALLBACK_CANCEL;
256 }
257
258 static void
259 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
260 {
261    Widget_Data *wd = elm_widget_data_get(data);
262    if (!wd) return;
263
264    if ((wd->autorepeat) && (!wd->repeating))
265      {
266         if (wd->ar_threshold <= 0.0)
267           _autorepeat_initial_send(data); /* call immediately */
268         else
269           wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
270      }
271 }
272
273 static void
274 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
275 {
276    Widget_Data *wd = elm_widget_data_get(data);
277    if (!wd) return;
278
279    if (wd->timer)
280      {
281         ecore_timer_del(wd->timer);
282         wd->timer = NULL;
283      }
284    wd->repeating = EINA_FALSE;
285    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
286 }
287
288 EAPI Evas_Object *
289 elm_button_add(Evas_Object *parent)
290 {
291    Evas_Object *obj;
292    Evas *e;
293    Widget_Data *wd;
294
295    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
296
297    ELM_SET_WIDTYPE(widtype, "button");
298    elm_widget_type_set(obj, "button");
299    elm_widget_sub_object_add(parent, obj);
300    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
301    elm_widget_data_set(obj, wd);
302    elm_widget_del_hook_set(obj, _del_hook);
303    elm_widget_theme_hook_set(obj, _theme_hook);
304    elm_widget_disable_hook_set(obj, _disable_hook);
305    elm_widget_can_focus_set(obj, EINA_TRUE);
306    elm_widget_activate_hook_set(obj, _activate_hook);
307    elm_widget_event_hook_set(obj, _event_hook);
308    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
309    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
310    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
311
312    wd->btn = edje_object_add(e);
313    _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
314    edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
315                                    _signal_clicked, obj);
316    edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
317                                    _signal_pressed, obj);
318    edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
319                                    _signal_unpressed, obj);
320    elm_widget_resize_object_set(obj, wd->btn);
321
322    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
323
324    _theme_hook(obj);
325
326    // TODO: convert Elementary to subclassing of Evas_Smart_Class
327    // TODO: and save some bytes, making descriptions per-class and not instance!
328    evas_object_smart_callbacks_descriptions_set(obj, _signals);
329    return obj;
330 }
331
332 EAPI void
333 elm_button_label_set(Evas_Object *obj, const char *label)
334 {
335    ELM_CHECK_WIDTYPE(obj, widtype);
336    Widget_Data *wd = elm_widget_data_get(obj);
337    if (!wd) return;
338    eina_stringshare_replace(&wd->label, label);
339    if (label)
340      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
341    else
342      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
343    edje_object_message_signal_process(wd->btn);
344    edje_object_part_text_set(wd->btn, "elm.text", label);
345    _sizing_eval(obj);
346 }
347
348 EAPI const char *
349 elm_button_label_get(const Evas_Object *obj)
350 {
351    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
352    Widget_Data *wd = elm_widget_data_get(obj);
353    if (!wd) return NULL;
354    return wd->label;
355 }
356
357 EAPI void
358 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
359 {
360    ELM_CHECK_WIDTYPE(obj, widtype);
361    Widget_Data *wd = elm_widget_data_get(obj);
362    if (!wd) return;
363    if (wd->icon == icon) return;
364    if (wd->icon) evas_object_del(wd->icon);
365    wd->icon = icon;
366    if (icon)
367      {
368         elm_widget_sub_object_add(obj, icon);
369         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
370                                        _changed_size_hints, obj);
371         edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
372         edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
373         edje_object_message_signal_process(wd->btn);
374      }
375    _sizing_eval(obj);
376 }
377
378 EAPI Evas_Object *
379 elm_button_icon_get(const Evas_Object *obj)
380 {
381    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
382    Widget_Data *wd = elm_widget_data_get(obj);
383    if (!wd) return NULL;
384    return wd->icon;
385 }
386
387 EAPI Evas_Object *
388 elm_button_icon_unset(Evas_Object *obj)
389 {
390    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
391    Widget_Data *wd = elm_widget_data_get(obj);
392    if (!wd) return NULL;
393    if (!wd->icon) return NULL;
394    Evas_Object *icon = wd->icon;
395    elm_widget_sub_object_del(obj, wd->icon);
396    edje_object_part_unswallow(wd->btn, wd->icon);
397    wd->icon = NULL;
398    return icon;
399 }
400
401 EAPI void
402 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
403 {
404    ELM_CHECK_WIDTYPE(obj, widtype);
405    Widget_Data *wd = elm_widget_data_get(obj);
406    if (!wd) return;
407    if (wd->timer)
408      {
409         ecore_timer_del(wd->timer);
410         wd->timer = NULL;
411      }
412    wd->autorepeat = on;
413    wd->repeating = EINA_FALSE;
414 }
415
416 EAPI Eina_Bool
417 elm_button_autorepeat_get(const Evas_Object *obj)
418 {
419    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
420    Widget_Data *wd = elm_widget_data_get(obj);
421    if (!wd) return EINA_FALSE;
422    return wd->autorepeat;
423 }
424
425 EAPI void
426 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
427 {
428    ELM_CHECK_WIDTYPE(obj, widtype);
429    Widget_Data *wd = elm_widget_data_get(obj);
430    if (!wd) return;
431    if (wd->ar_threshold == t) return;
432    if (wd->timer)
433      {
434         ecore_timer_del(wd->timer);
435         wd->timer = NULL;
436      }
437    wd->ar_threshold = t;
438 }
439
440 EAPI double
441 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
442 {
443    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
444    Widget_Data *wd = elm_widget_data_get(obj);
445    if (!wd) return 0.0;
446    return wd->ar_threshold;
447 }
448
449 EAPI void
450 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
451 {
452    ELM_CHECK_WIDTYPE(obj, widtype);
453    Widget_Data *wd = elm_widget_data_get(obj);
454    if (!wd) return;
455    if (wd->ar_interval == t) return;
456
457    wd->ar_interval = t;
458    if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
459 }
460
461 EAPI double
462 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
463 {
464    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
465    Widget_Data *wd = elm_widget_data_get(obj);
466    if (!wd) return 0.0;
467    return wd->ar_interval;
468 }