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