[src/lib/elm_button.c] Fix for the Text updation in Button for various states.
[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_part_text_set(wd->btn, "elm.text", label);
386    _sizing_eval(obj);
387 }
388 /**
389  * Set the label for each state of button
390  *
391  * @param[in] obj The button object
392  * @param[in] label The text will be written on the button
393  * @param[in] state The state of button
394  *
395  * @ingroup Button
396  */
397 EAPI void
398 elm_button_label_set_for_state(Evas_Object *obj, const char *label, UIControlState state)
399 {
400    Widget_Data *wd = elm_widget_data_get(obj);
401
402    if (!wd) return;
403    if (label == NULL) return;
404
405    if (state == UIControlStateDefault)
406      {
407         wd->statetype[DEFAULT] = UIControlStateDefault;
408         eina_stringshare_replace(&wd->statelabel[DEFAULT], label);
409         return;
410      }
411    if (state == UIControlStateHighlighted)
412      {
413         wd->statetype[HIGHLIGHTED] = UIControlStateHighlighted;
414         eina_stringshare_replace(&wd->statelabel[HIGHLIGHTED], label);
415         return;
416      }
417    if (state == UIControlStateFocused)
418      {
419         wd->statetype[FOCUSED] = UIControlStateFocused;
420         eina_stringshare_replace(&wd->statelabel[FOCUSED], label);
421         return;
422      }
423    if (state == UIControlStateDisabled)
424      {
425         wd->statetype[DISABLED] = UIControlStateDisabled;
426         eina_stringshare_replace(&wd->statelabel[DISABLED], label);
427         return;
428      }
429 }
430
431 /**
432  * Get the label of button
433  *
434  * @param[in] obj The button object
435  * @return The title of button
436  *
437  * @ingroup Button
438  */
439 EAPI const char *
440 elm_button_label_get(const Evas_Object *obj)
441 {
442    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
443    Widget_Data *wd = elm_widget_data_get(obj);
444    if (!wd) return NULL;
445    return wd->label;
446 }
447 /**
448  * Get the label of button for each state
449  *
450  * @param[in] obj The button object
451  * @param[in] state The state of button
452  * @return The title of button for state
453  *
454  * @ingroup Button
455  */
456 EAPI const char*
457 elm_button_label_get_for_state(const Evas_Object *obj, UIControlState state)
458 {
459    Widget_Data *wd = elm_widget_data_get(obj);
460    if (!wd) return NULL;
461
462    if (state == UIControlStateDefault)
463      return wd->statelabel[DEFAULT];
464    else if (state == UIControlStateHighlighted)
465      return wd->statelabel[HIGHLIGHTED];
466    else if (state == UIControlStateFocused)
467      return wd->statelabel[FOCUSED];
468    else if (state == UIControlStateDisabled)
469      return wd->statelabel[DISABLED];
470    else
471      return NULL;
472 }
473
474 /**
475  * Set the icon used for the button
476  *
477  * Once the icon object is set, a previously set one will be deleted
478  *
479  * @param[in] obj The button object
480  * @param[in] icon The image for the button
481  *
482  * @ingroup Button
483  */
484 EAPI void
485 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
486 {
487    ELM_CHECK_WIDTYPE(obj, widtype);
488    Widget_Data *wd = elm_widget_data_get(obj);
489    if (!wd) return;
490    if (wd->icon == icon) return;
491    if (wd->icon) evas_object_del(wd->icon);
492    wd->icon = icon;
493    if (icon)
494      {
495         elm_widget_sub_object_add(obj, icon);
496         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
497                                        _changed_size_hints, obj);
498         edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
499         edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
500         edje_object_message_signal_process(wd->btn);
501      }
502    _sizing_eval(obj);
503 }
504
505 /**
506  * Get the icon used for the button
507  *
508  * @param[in] obj The button object
509  * @return The image for the button
510  *
511  * @ingroup Button
512  */
513 EAPI Evas_Object *
514 elm_button_icon_get(const Evas_Object *obj)
515 {
516    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
517    Widget_Data *wd = elm_widget_data_get(obj);
518    if (!wd) return NULL;
519    return wd->icon;
520 }
521
522 /**
523  * Turn on/off the autorepeat event generated when the user keeps pressing on the button
524  *
525  * @param[in] obj The button object
526  * @param[in] on  A bool to turn on/off the event
527  *
528  * @ingroup Button
529  */
530 EAPI void
531 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
532 {
533    ELM_CHECK_WIDTYPE(obj, widtype);
534    Widget_Data *wd = elm_widget_data_get(obj);
535    if (!wd) return;
536    if (wd->timer)
537      {
538         ecore_timer_del(wd->timer);
539         wd->timer = NULL;
540      }
541    wd->autorepeat = on;
542    wd->repeating = EINA_FALSE;
543 }
544
545 /**
546  * Set the initial timeout before the autorepeat event is generated
547  *
548  * @param[in] obj The button object
549  * @param[in] t   Timeout
550  *
551  * @ingroup Button
552  */
553 EAPI void
554 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
555 {
556    ELM_CHECK_WIDTYPE(obj, widtype);
557    Widget_Data *wd = elm_widget_data_get(obj);
558    if (!wd) return;
559    if (wd->ar_threshold == t) return;
560    if (wd->timer)
561      {
562         ecore_timer_del(wd->timer);
563         wd->timer = NULL;
564      }
565    wd->ar_threshold = t;
566 }
567
568 /**
569  * Set the interval between each generated autorepeat event
570  *
571  * @param[in] obj The button object
572  * @param[in] t   Interval
573  *
574  * @ingroup Button
575  */
576 EAPI void
577 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
578 {
579    ELM_CHECK_WIDTYPE(obj, widtype);
580    Widget_Data *wd = elm_widget_data_get(obj);
581    if (!wd) return;
582    if (wd->ar_interval == t) return;
583
584    wd->ar_interval = t;
585    if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);
586 }