d6a6fbd6e5ac36e303227aaa82a38fb55479b4fd
[framework/uifw/elementary.git] / src / lib / elm_button.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Button Button
6  * @ingroup Elementary
7  *
8  * This is a push-button. Press it and run some function. It can contain
9  * a simple label and icon object.
10  */
11
12 typedef struct _Widget_Data Widget_Data;
13
14 enum
15 {
16    DEFAULT = 0,
17    HIGHLIGHTED,
18    FOCUSED,
19    DISABLED,
20 };
21
22 struct _Widget_Data
23 {
24    Evas_Object *btn, *icon;
25    const char *label;
26    Eina_Bool autorepeat;
27    Eina_Bool repeating;
28    double ar_threshold;
29    double ar_interval;
30    Ecore_Timer *timer;
31    const char *statelabel[4];
32    int statetype[4];
33 };
34
35 static const char *widtype = NULL;
36 static void _del_hook(Evas_Object *obj);
37 static void _theme_hook(Evas_Object *obj);
38 static void _disable_hook(Evas_Object *obj);
39 static void _sizing_eval(Evas_Object *obj);
40 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
41 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
42 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
43 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
44 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
45 static void _on_focus_hook(void *data, Evas_Object *obj);
46 static void _set_label(Evas_Object *obj, const char *label);
47 static void _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source);
48
49 static const char SIG_CLICKED[] = "clicked";
50 static const char SIG_REPEATED[] = "repeated";
51 static const char SIG_UNPRESSED[] = "unpressed";
52 static const Evas_Smart_Cb_Description _signals[] = {
53   {SIG_CLICKED, ""},
54   {SIG_REPEATED, ""},
55   {SIG_UNPRESSED, ""},
56   {NULL, NULL}
57 };
58
59 static void
60 _del_hook(Evas_Object *obj)
61 {
62    Widget_Data *wd = elm_widget_data_get(obj);
63    if (!wd) return;
64    if (wd->label) eina_stringshare_del(wd->label);
65    if (wd->statelabel[DEFAULT]) eina_stringshare_del(wd->statelabel[DEFAULT]);
66    if (wd->statelabel[HIGHLIGHTED]) eina_stringshare_del(wd->statelabel[HIGHLIGHTED]);
67    if (wd->statelabel[FOCUSED]) eina_stringshare_del(wd->statelabel[FOCUSED]);
68    if (wd->statelabel[DISABLED]) eina_stringshare_del(wd->statelabel[DISABLED]);
69    free(wd);
70 }
71
72 static void
73 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
74 {
75    Widget_Data *wd = elm_widget_data_get(obj);
76    if (!wd) return;
77    if (elm_widget_focus_get(obj))
78      {
79         if(wd->statelabel[FOCUSED])
80           {
81              _set_label(obj, wd->statelabel[FOCUSED]);
82           }
83         edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
84         evas_object_focus_set(wd->btn, EINA_TRUE);
85      }
86    else
87      {
88         if(wd->statelabel[DEFAULT])
89           _set_label(obj, wd->statelabel[DEFAULT]);
90 #if 0
91         else
92           _set_label(obj, wd->label);
93 #endif
94         edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
95         evas_object_focus_set(wd->btn, EINA_FALSE);
96      }
97 }
98
99 static void
100 _theme_hook(Evas_Object *obj)
101 {
102    Widget_Data *wd = elm_widget_data_get(obj);
103    if (!wd) return;
104    _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
105
106    if (wd->icon)
107      edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
108    
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    
114    if (wd->icon)
115      edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
116    else
117      edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
118
119    edje_object_part_text_set(wd->btn, "elm.text", wd->label);
120    if (elm_object_disabled_get(obj))
121      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
122    edje_object_message_signal_process(wd->btn);
123    edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
124    _sizing_eval(obj);
125 }
126
127 static void
128 _disable_hook(Evas_Object *obj)
129 {
130    Widget_Data *wd = elm_widget_data_get(obj);
131    if (!wd) return;
132    if (elm_widget_disabled_get(obj))
133      {
134         if(wd->statelabel[DISABLED] )
135           {
136              _set_label(obj, wd->statelabel[DISABLED]);
137           }
138         edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
139      }
140    else
141      {
142         if(wd->statelabel[DEFAULT])
143           _set_label(obj, wd->statelabel[DEFAULT]);
144 #if 0
145         else
146           _set_label(obj, wd->label);
147 #endif
148         edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
149      }
150 }
151
152 static void
153 _sizing_eval(Evas_Object *obj)
154 {
155    Widget_Data *wd = elm_widget_data_get(obj);
156    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
157    Evas_Coord w, h;
158
159    if (!wd) return;
160    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
161    edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
162    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
163    //Commenting to sync with open source and able to resize based on text change
164    evas_object_size_hint_min_get(obj, &w, &h);
165    //if (w > minw) minw = w;
166    if (h > minh) minh = h;
167
168    evas_object_size_hint_min_set(obj, minw, minh);
169    //evas_object_size_hint_max_set(obj, maxw, maxh);
170 }
171
172 static void
173 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
174 {
175    Widget_Data *wd = elm_widget_data_get(data);
176    if (!wd) return;
177    if (obj != wd->icon) return;
178    _sizing_eval(data);
179 }
180
181 static void
182 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
183 {
184    Widget_Data *wd = elm_widget_data_get(obj);
185    Evas_Object *sub = event_info;
186    if (!wd) return;
187    if (sub == wd->icon)
188      {
189         edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
190         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
191                                        _changed_size_hints, obj);
192         wd->icon = NULL;
193         edje_object_message_signal_process(wd->btn);
194         _sizing_eval(obj);
195      }
196 }
197
198 static void
199 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
200 {
201    Widget_Data *wd = elm_widget_data_get(data);
202    if (!wd) return;
203    if (wd->timer)
204      {
205         ecore_timer_del(wd->timer);
206         wd->timer = NULL;
207      }
208    wd->repeating = EINA_FALSE;
209    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
210 #if 0
211    _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
212 #endif
213 }
214
215 static Eina_Bool
216 _autorepeat_send(void *data)
217 {
218    Widget_Data *wd = elm_widget_data_get(data);
219    if (!wd) return ECORE_CALLBACK_CANCEL;
220
221    evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
222    if (!wd->repeating)
223      {
224         wd->timer = NULL;
225         return ECORE_CALLBACK_CANCEL;
226      }
227
228    return ECORE_CALLBACK_RENEW;
229 }
230
231 static Eina_Bool
232 _autorepeat_initial_send(void *data)
233 {
234    Widget_Data *wd = elm_widget_data_get(data);
235    if (!wd) return ECORE_CALLBACK_CANCEL;
236
237    if (wd->timer) ecore_timer_del(wd->timer);
238    wd->repeating = EINA_TRUE;
239    _autorepeat_send(data);
240    wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
241
242    return ECORE_CALLBACK_CANCEL;
243 }
244
245 static void
246 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
247 {
248    Widget_Data *wd = elm_widget_data_get(data);
249    if (!wd) return;
250
251    if(wd->statelabel[HIGHLIGHTED])
252      {
253         _set_label(data, wd->statelabel[HIGHLIGHTED]);
254      }
255    if (wd->autorepeat && !wd->repeating)
256      {
257         if (wd->ar_threshold <= 0.0)
258           _autorepeat_initial_send(data); /* call immediately */
259         else
260           wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
261      }
262 }
263
264 static void
265 _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source)
266 {
267    Widget_Data *wd = elm_widget_data_get(data);
268    if (!wd) return;
269    if(wd->statelabel[DEFAULT])
270      _set_label(data, wd->statelabel[DEFAULT]);
271 #if 0
272    else
273      _set_label(data, wd->label);
274 #endif
275    return;
276 }
277
278 static void
279 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
280 {
281    Widget_Data *wd = elm_widget_data_get(data);
282    if (!wd) return;
283    if(wd->statelabel[DEFAULT])
284      _set_label(data, wd->statelabel[DEFAULT]);
285 #if 0
286    else
287      _set_label(data, wd->label);
288 #endif
289
290    if (wd->timer)
291      {
292         ecore_timer_del(wd->timer);
293         wd->timer = NULL;
294      }
295    wd->repeating = EINA_FALSE;
296    evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
297 }
298
299 /**
300  * Add a new button to the parent
301  * @param[in] parent The parent object
302  * @return The new object or NULL if it cannot be created
303  *
304  * @ingroup Button
305  */
306 EAPI Evas_Object *
307 elm_button_add(Evas_Object *parent)
308 {
309    Evas_Object *obj;
310    Evas *e;
311    Widget_Data *wd;
312
313    wd = ELM_NEW(Widget_Data);
314    e = evas_object_evas_get(parent);
315    obj = elm_widget_add(e);
316    ELM_SET_WIDTYPE(widtype, "button");
317    elm_widget_type_set(obj, "button");
318    elm_widget_can_focus_set(obj, EINA_TRUE);
319    elm_widget_sub_object_add(parent, obj);
320    elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
321    elm_widget_data_set(obj, wd);
322    elm_widget_del_hook_set(obj, _del_hook);
323    elm_widget_theme_hook_set(obj, _theme_hook);
324    elm_widget_disable_hook_set(obj, _disable_hook);
325
326    wd->btn = edje_object_add(e);
327    _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
328    wd->statetype[DEFAULT] = 0;
329    wd->statetype[HIGHLIGHTED] = 0;
330    wd->statetype[FOCUSED] = 0;
331    wd->statetype[DISABLED] = 0;
332    wd->statelabel[DEFAULT] = 0;
333    wd->statelabel[HIGHLIGHTED] = 0;
334    wd->statelabel[FOCUSED] = 0;
335    wd->statelabel[DISABLED] = 0;
336    edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
337                                    _signal_clicked, obj);
338    edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
339                                    _signal_pressed, obj);
340    edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
341                                    _signal_unpressed, obj);
342    edje_object_signal_callback_add(wd->btn, "elm,action,default,text,set", "",
343                 _signal_default_text_set, obj);
344    elm_widget_resize_object_set(obj, wd->btn);
345
346    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
347
348    _sizing_eval(obj);
349
350    // TODO: convert Elementary to subclassing of Evas_Smart_Class
351    // TODO: and save some bytes, making descriptions per-class and not instance!
352    evas_object_smart_callbacks_descriptions_set(obj, _signals);
353    return obj;
354 }
355
356 /**
357  * Set the label used in the button
358  *
359  * @param[in] obj The button object
360  * @param[in] label The text will be written on the button
361  *
362  * @ingroup Button
363  */
364 EAPI void
365 elm_button_label_set(Evas_Object *obj, const char *label)
366 {
367    ELM_CHECK_WIDTYPE(obj, widtype);
368    Widget_Data *wd = elm_widget_data_get(obj);
369    if (!wd) return;
370    eina_stringshare_replace(&wd->label, label);
371    if (label)
372      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
373    else
374      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
375    edje_object_message_signal_process(wd->btn);
376    edje_object_part_text_set(wd->btn, "elm.text", label);
377    _sizing_eval(obj);
378 }
379
380 static void
381 _set_label(Evas_Object *obj, const char *label)
382 {
383    Widget_Data *wd = elm_widget_data_get(obj);
384
385    edje_object_message_signal_process(wd->btn);
386    edje_object_part_text_set(wd->btn, "elm.text", label);
387    _sizing_eval(obj);
388 }
389 /**
390  * Set the label for each state of button
391  *
392  * @param[in] obj The button object
393  * @param[in] label The text will be written on the button
394  * @param[in] state The state of button
395  *
396  * @ingroup Button
397  */
398 EAPI void
399 elm_button_label_set_for_state(Evas_Object *obj, const char *label, UIControlState state)
400 {
401    Widget_Data *wd = elm_widget_data_get(obj);
402
403    if (!wd) return;
404    if(label == NULL) return;
405
406    if(state == UIControlStateDefault)
407      {
408         wd->statetype[DEFAULT] = UIControlStateDefault;
409         eina_stringshare_replace(&wd->statelabel[DEFAULT], label);
410         return;
411      }
412    if(state == UIControlStateHighlighted)
413      {
414         wd->statetype[HIGHLIGHTED] = UIControlStateHighlighted;
415         eina_stringshare_replace(&wd->statelabel[HIGHLIGHTED], label);
416         return;
417      }
418    if(state == UIControlStateFocused)
419      {
420         wd->statetype[FOCUSED] = UIControlStateFocused;
421         eina_stringshare_replace(&wd->statelabel[FOCUSED], label);
422         return;
423      }
424    if(state == UIControlStateDisabled)
425      {
426         wd->statetype[DISABLED] = UIControlStateDisabled;
427         eina_stringshare_replace(&wd->statelabel[DISABLED], label);
428         return;
429      }
430 }
431
432 /**
433  * Get the label of button
434  *
435  * @param[in] obj The button object
436  * @return The title of button
437  *
438  * @ingroup Button
439  */
440 EAPI const char *
441 elm_button_label_get(const Evas_Object *obj)
442 {
443    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
444    Widget_Data *wd = elm_widget_data_get(obj);
445    if (!wd) return NULL;
446    return wd->label;
447 }
448 /**
449  * Get the label of button for each state
450  *
451  * @param[in] obj The button object
452  * @param[in] state The state of button
453  * @return The title of button for state
454  *
455  * @ingroup Button
456  */
457 EAPI const char*
458 elm_button_label_get_for_state(const Evas_Object *obj, UIControlState state)
459 {
460    Widget_Data *wd = elm_widget_data_get(obj);
461    if (!wd) return NULL;
462
463    if(state == UIControlStateDefault)
464      return wd->statelabel[DEFAULT];
465    else if(state == UIControlStateHighlighted)
466      return wd->statelabel[HIGHLIGHTED];
467    else if(state == UIControlStateFocused)
468      return wd->statelabel[FOCUSED];
469    else if(state == UIControlStateDisabled)
470      return wd->statelabel[DISABLED];
471    else
472      return NULL;
473 }
474
475 /**
476  * Set the icon used for the button
477  *
478  * Once the icon object is set, a previously set one will be deleted
479  *
480  * @param[in] obj The button object
481  * @param[in] icon The image for the button
482  *
483  * @ingroup Button
484  */
485 EAPI void
486 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
487 {
488    ELM_CHECK_WIDTYPE(obj, widtype);
489    Widget_Data *wd = elm_widget_data_get(obj);
490    if (!wd) return;
491    if (wd->icon == icon) return;
492    if (wd->icon) evas_object_del(wd->icon);
493    wd->icon = icon;
494    if (icon)
495      {
496         elm_widget_sub_object_add(obj, icon);
497         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
498                                        _changed_size_hints, obj);
499         edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
500         edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
501         edje_object_message_signal_process(wd->btn);
502      }
503    _sizing_eval(obj);
504 }
505
506 /**
507  * Get the icon used for the button
508  *
509  * @param[in] obj The button object
510  * @return The image for the button
511  *
512  * @ingroup Button
513  */
514 EAPI Evas_Object *
515 elm_button_icon_get(const Evas_Object *obj)
516 {
517    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
518    Widget_Data *wd = elm_widget_data_get(obj);
519    if (!wd) return NULL;
520    return wd->icon;
521 }
522
523 /**
524  * Turn on/off the autorepeat event generated when the user keeps pressing on the button
525  *
526  * @param[in] obj The button object
527  * @param[in] on  A bool to turn on/off the event
528  *
529  * @ingroup Button
530  */
531 EAPI void
532 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
533 {
534    ELM_CHECK_WIDTYPE(obj, widtype);
535    Widget_Data *wd = elm_widget_data_get(obj);
536    if (!wd) return;
537    if (wd->timer)
538      {
539         ecore_timer_del(wd->timer);
540         wd->timer = NULL;
541      }
542    wd->autorepeat = on;
543    wd->repeating = EINA_FALSE;
544 }
545
546 /**
547  * Set the initial timeout before the autorepeat event is generated
548  *
549  * @param[in] obj The button object
550  * @param[in] t   Timeout
551  *
552  * @ingroup Button
553  */
554 EAPI void
555 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
556 {
557    ELM_CHECK_WIDTYPE(obj, widtype);
558    Widget_Data *wd = elm_widget_data_get(obj);
559    if (!wd) return;
560    if (wd->ar_threshold == t) return;
561    if (wd->timer)
562      {
563         ecore_timer_del(wd->timer);
564         wd->timer = NULL;
565      }
566    wd->ar_threshold = t;
567 }
568
569 /**
570  * Set the interval between each generated autorepeat event
571  *
572  * @param[in] obj The button object
573  * @param[in] t   Interval
574  *
575  * @ingroup Button
576  */
577 EAPI void
578 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
579 {
580    ELM_CHECK_WIDTYPE(obj, widtype);
581    Widget_Data *wd = elm_widget_data_get(obj);
582    if (!wd) return;
583    if (wd->ar_interval == t) return;
584
585    wd->ar_interval = t;
586    if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);
587 }