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