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