Elementary patch for supporting keypad
[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 static void _activate(Evas_Object *obj);
36 static void _activate_hook(Evas_Object *obj);
37 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
38                              Evas_Callback_Type type, void *event_info);
39
40 static const char SIG_CLICKED[] = "clicked";
41 static const char SIG_REPEATED[] = "repeated";
42 static const char SIG_UNPRESSED[] = "unpressed";
43 static const Evas_Smart_Cb_Description _signals[] = {
44   {SIG_CLICKED, ""},
45   {SIG_REPEATED, ""},
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_hook(Evas_Object *obj)
71 {
72    Widget_Data *wd = elm_widget_data_get(obj);
73    if (!wd) return;
74    if (wd->label) eina_stringshare_del(wd->label);
75    free(wd);
76 }
77
78 static void
79 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
80 {
81    Widget_Data *wd = elm_widget_data_get(obj);
82    if (!wd) return;
83    if (elm_widget_focus_get(obj))
84      {
85         edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
86         evas_object_focus_set(wd->btn, EINA_TRUE);
87      }
88    else
89      {
90         edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
91         evas_object_focus_set(wd->btn, EINA_FALSE);
92      }
93 }
94
95 static void
96 _theme_hook(Evas_Object *obj)
97 {
98    Widget_Data *wd = elm_widget_data_get(obj);
99    const char *str;
100    if (!wd) return;
101    _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
102    if (wd->icon)
103      edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
104    if (wd->label)
105      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
106    else
107      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
108    if (wd->icon)
109      edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
110    else
111      edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
112    edje_object_part_text_set(wd->btn, "elm.text", wd->label);
113    if (elm_object_disabled_get(obj))
114      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
115    edje_object_message_signal_process(wd->btn);
116    edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
117    str = edje_object_data_get(wd->btn, "focus_highlight");
118    if ((str) && (!strcmp(str, "on")))
119      elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
120    else
121      elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
122    _sizing_eval(obj);
123 }
124
125 static void
126 _disable_hook(Evas_Object *obj)
127 {
128    Widget_Data *wd = elm_widget_data_get(obj);
129    if (!wd) return;
130    if (elm_widget_disabled_get(obj))
131      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
132    else
133      edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
134 }
135
136 static void
137 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
138 {
139    Widget_Data *wd = elm_widget_data_get(obj);
140    if (!wd) return;
141    edje_object_signal_emit(wd->btn, emission, source);
142 }
143
144 static void
145 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
146 {
147    Widget_Data *wd = elm_widget_data_get(obj);
148    if (!wd) return;
149    edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
150 }
151
152 static void
153 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
154 {
155    Widget_Data *wd = elm_widget_data_get(obj);
156    edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
157                                         data);
158 }
159
160 static void
161 _sizing_eval(Evas_Object *obj)
162 {
163    Widget_Data *wd = elm_widget_data_get(obj);
164    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
165
166    if (!wd) return;
167    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
168    edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
169    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
170    evas_object_size_hint_min_set(obj, minw, minh);
171    evas_object_size_hint_max_set(obj, maxw, maxh);
172 }
173
174 static void
175 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
176 {
177    Widget_Data *wd = elm_widget_data_get(data);
178    if (!wd) return;
179    if (obj != wd->icon) return;
180    _sizing_eval(data);
181 }
182
183 static void
184 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
185 {
186    Widget_Data *wd = elm_widget_data_get(obj);
187    Evas_Object *sub = event_info;
188    if (!wd) return;
189    if (sub == wd->icon)
190      {
191         edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
192         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
193                                        _changed_size_hints, obj);
194         wd->icon = NULL;
195         edje_object_message_signal_process(wd->btn);
196         _sizing_eval(obj);
197      }
198 }
199
200 static void
201 _activate(Evas_Object *obj)
202 {
203    Widget_Data *wd = elm_widget_data_get(obj);
204    if (!wd) return;
205    if (wd->timer)
206      {
207         ecore_timer_del(wd->timer);
208         wd->timer = NULL;
209      }
210    wd->repeating = EINA_FALSE;
211    evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
212    _signal_unpressed(obj, wd->btn, NULL, NULL); /* safe guard when the theme does not emit the 'unpress' signal */
213 }
214
215 static void
216 _activate_hook(Evas_Object *obj)
217 {
218    _activate(obj);
219 }
220
221 static void
222 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
223 {
224    _activate(data);
225 }
226
227 static Eina_Bool
228 _autorepeat_send(void *data)
229 {
230    Widget_Data *wd = elm_widget_data_get(data);
231    if (!wd) return ECORE_CALLBACK_CANCEL;
232
233    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
234    if (!wd->repeating)
235      {
236         wd->timer = NULL;
237         return ECORE_CALLBACK_CANCEL;
238      }
239
240    return ECORE_CALLBACK_RENEW;
241 }
242
243 static Eina_Bool
244 _autorepeat_initial_send(void *data)
245 {
246    Widget_Data *wd = elm_widget_data_get(data);
247    if (!wd) return ECORE_CALLBACK_CANCEL;
248
249    if (wd->timer) ecore_timer_del(wd->timer);
250    wd->repeating = EINA_TRUE;
251    _autorepeat_send(data);
252    wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
253
254    return ECORE_CALLBACK_CANCEL;
255 }
256
257 static void
258 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
259 {
260    Widget_Data *wd = elm_widget_data_get(data);
261    if (!wd) return;
262
263    if ((wd->autorepeat) && (!wd->repeating))
264      {
265         if (wd->ar_threshold <= 0.0)
266           _autorepeat_initial_send(data); /* call immediately */
267         else
268           wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
269      }
270 }
271
272 static void
273 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
274 {
275    Widget_Data *wd = elm_widget_data_get(data);
276    if (!wd) return;
277
278    if (wd->timer)
279      {
280         ecore_timer_del(wd->timer);
281         wd->timer = NULL;
282      }
283    wd->repeating = EINA_FALSE;
284    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
285 }
286
287 /**
288  * Add a new button to the parent
289  * @param parent The parent object
290  * @return The new object or NULL if it cannot be created
291  *
292  * @ingroup Button
293  */
294 EAPI Evas_Object *
295 elm_button_add(Evas_Object *parent)
296 {
297    Evas_Object *obj;
298    Evas *e;
299    Widget_Data *wd;
300
301    wd = ELM_NEW(Widget_Data);
302    e = evas_object_evas_get(parent);
303    obj = elm_widget_add(e);
304    ELM_SET_WIDTYPE(widtype, "button");
305    elm_widget_type_set(obj, "button");
306    elm_widget_sub_object_add(parent, obj);
307    elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
308    elm_widget_data_set(obj, wd);
309    elm_widget_del_hook_set(obj, _del_hook);
310    elm_widget_theme_hook_set(obj, _theme_hook);
311    elm_widget_disable_hook_set(obj, _disable_hook);
312    elm_widget_can_focus_set(obj, EINA_TRUE);
313    elm_widget_activate_hook_set(obj, _activate_hook);
314    elm_widget_event_hook_set(obj, _event_hook);
315    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
316    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
317    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
318
319    wd->btn = edje_object_add(e);
320    _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
321    edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
322                                    _signal_clicked, obj);
323    edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
324                                    _signal_pressed, obj);
325    edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
326                                    _signal_unpressed, obj);
327    elm_widget_resize_object_set(obj, wd->btn);
328
329    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
330
331    _theme_hook(obj);
332
333    // TODO: convert Elementary to subclassing of Evas_Smart_Class
334    // TODO: and save some bytes, making descriptions per-class and not instance!
335    evas_object_smart_callbacks_descriptions_set(obj, _signals);
336    return obj;
337 }
338
339 /**
340  * Set the label used in the button
341  *
342  * @param obj The button object
343  * @param label The text will be written on the button
344  *
345  * @ingroup Button
346  */
347 EAPI void
348 elm_button_label_set(Evas_Object *obj, const char *label)
349 {
350    ELM_CHECK_WIDTYPE(obj, widtype);
351    Widget_Data *wd = elm_widget_data_get(obj);
352    if (!wd) return;
353    eina_stringshare_replace(&wd->label, label);
354    if (label)
355      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
356    else
357      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
358    edje_object_message_signal_process(wd->btn);
359    edje_object_part_text_set(wd->btn, "elm.text", label);
360    _sizing_eval(obj);
361 }
362
363 EAPI const char *
364 elm_button_label_get(const Evas_Object *obj)
365 {
366    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
367    Widget_Data *wd = elm_widget_data_get(obj);
368    if (!wd) return NULL;
369    return wd->label;
370 }
371
372 /**
373  * Set the icon used for the button
374  *
375  * Once the icon object is set, a previously set one will be deleted
376  * If you want to keep that old content object, use the
377  * elm_button_icon_unset() function.
378  *
379  * @param obj The button object
380  * @param icon The icon object for the button
381  *
382  * @ingroup Button
383  */
384 EAPI void
385 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
386 {
387    ELM_CHECK_WIDTYPE(obj, widtype);
388    Widget_Data *wd = elm_widget_data_get(obj);
389    if (!wd) return;
390    if (wd->icon == icon) return;
391    if (wd->icon) evas_object_del(wd->icon);
392    wd->icon = icon;
393    if (icon)
394      {
395         elm_widget_sub_object_add(obj, icon);
396         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
397                                        _changed_size_hints, obj);
398         edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
399         edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
400         edje_object_message_signal_process(wd->btn);
401      }
402    _sizing_eval(obj);
403 }
404
405 /**
406  * Get the icon used for the button
407  *
408  * Return the icon object which is set for this widget.
409  *
410  * @param obj The button object
411  * @return The icon object that is being used
412  *
413  * @ingroup Button
414  */
415 EAPI Evas_Object *
416 elm_button_icon_get(const Evas_Object *obj)
417 {
418    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
419    Widget_Data *wd = elm_widget_data_get(obj);
420    if (!wd) return NULL;
421    return wd->icon;
422 }
423
424 /**
425  * Unset the icon used for the button
426  *
427  * Unparent and return the icon object which was set for this widget.
428  *
429  * @param obj The button object
430  * @return The icon object that was being used
431  *
432  * @ingroup Button
433  */
434 EAPI Evas_Object *
435 elm_button_icon_unset(Evas_Object *obj)
436 {
437    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
438    Widget_Data *wd = elm_widget_data_get(obj);
439    if (!wd) return NULL;
440    if (!wd->icon) return NULL;
441    Evas_Object *icon = wd->icon;
442    elm_widget_sub_object_del(obj, wd->icon);
443    edje_object_part_unswallow(wd->btn, wd->icon);
444    wd->icon = NULL;
445    return icon;
446 }
447
448 /**
449  * Turn on/off the autorepeat event generated when the user keeps pressing on the button
450  *
451  * @param obj The button object
452  * @param on  A bool to turn on/off the event
453  *
454  * @ingroup Button
455  */
456 EAPI void
457 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
458 {
459    ELM_CHECK_WIDTYPE(obj, widtype);
460    Widget_Data *wd = elm_widget_data_get(obj);
461    if (!wd) return;
462    if (wd->timer)
463      {
464         ecore_timer_del(wd->timer);
465         wd->timer = NULL;
466      }
467    wd->autorepeat = on;
468    wd->repeating = EINA_FALSE;
469 }
470
471 /**
472  * Get if autorepeat event is on
473  *
474  * @param obj The button object
475  * @return If autorepeat is on
476  *
477  * @ingroup Button
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 /**
489  * Set the initial timeout before the autorepeat event is generated
490  *
491  * @param obj The button object
492  * @param t   Timeout
493  *
494  * @ingroup Button
495  */
496 EAPI void
497 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
498 {
499    ELM_CHECK_WIDTYPE(obj, widtype);
500    Widget_Data *wd = elm_widget_data_get(obj);
501    if (!wd) return;
502    if (wd->ar_threshold == t) return;
503    if (wd->timer)
504      {
505         ecore_timer_del(wd->timer);
506         wd->timer = NULL;
507      }
508    wd->ar_threshold = t;
509 }
510
511 /**
512  * Get the initial timeout before the autorepeat event is generated
513  *
514  * @param obj The button object
515  * @return Timeout
516  *
517  * @ingroup Button
518  */
519 EAPI double
520 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
521 {
522    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
523    Widget_Data *wd = elm_widget_data_get(obj);
524    if (!wd) return 0.0;
525    return wd->ar_threshold;
526 }
527
528 /**
529  * Set the interval between each generated autorepeat event
530  *
531  * @param obj The button object
532  * @param t   Interval
533  *
534  * @ingroup Button
535  */
536 EAPI void
537 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
538 {
539    ELM_CHECK_WIDTYPE(obj, widtype);
540    Widget_Data *wd = elm_widget_data_get(obj);
541    if (!wd) return;
542    if (wd->ar_interval == t) return;
543
544    wd->ar_interval = t;
545    if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
546 }
547
548 /**
549  * Get the interval between each generated autorepeat event
550  *
551  * @param obj The button object
552  * @return Interval
553  *
554  * @ingroup Button
555  */
556 EAPI double
557 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
558 {
559    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
560    Widget_Data *wd = elm_widget_data_get(obj);
561    if (!wd) return 0.0;
562    return wd->ar_interval;
563 }