patch for unfocus for elm butotn from chun eon park
[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 };
18
19 static void _del_hook(Evas_Object *obj);
20 static void _theme_hook(Evas_Object *obj);
21 static void _disable_hook(Evas_Object *obj);
22 static void _sizing_eval(Evas_Object *obj);
23 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
24 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
25 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
26 static void _on_focus_hook(void *data, Evas_Object *obj);
27
28 static void
29 _del_hook(Evas_Object *obj)
30 {
31    Widget_Data *wd = elm_widget_data_get(obj);
32    if (!wd) return;
33    if (wd->label) eina_stringshare_del(wd->label);
34    free(wd);
35 }
36
37 static void
38 _on_focus_hook(void *data, Evas_Object *obj)
39 {
40         Widget_Data *wd = elm_widget_data_get(obj);
41         Evas_Object *top = elm_widget_top_get(obj);
42
43         if (elm_widget_focus_get(obj)) {
44                 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
45
46                 if( top ) {
47                         elm_win_keyboard_mode_set( top, ELM_WIN_KEYBOARD_ON );
48                 }
49
50         }else {
51                 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
52
53                 if ( top ) {
54                         elm_win_keyboard_mode_set(top, ELM_WIN_KEYBOARD_OFF);
55                 }
56         }
57         
58 }
59
60
61
62 static void
63 _theme_hook(Evas_Object *obj)
64 {
65    Widget_Data *wd = elm_widget_data_get(obj);
66    if (!wd) return;
67    _elm_theme_set(wd->btn, "button", "base", elm_widget_style_get(obj));
68    if (wd->icon)
69      edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
70    if (wd->label)
71      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
72    else
73      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
74    if (wd->icon)
75      edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
76    else
77      edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
78    edje_object_part_text_set(wd->btn, "elm.text", wd->label);
79    edje_object_message_signal_process(wd->btn);
80    edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
81    _sizing_eval(obj);
82 }
83
84 static void
85 _disable_hook(Evas_Object *obj)
86 {
87    Widget_Data *wd = elm_widget_data_get(obj);
88    if (elm_widget_disabled_get(obj))
89      edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
90    else
91      edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
92 }
93
94 static void
95 _sizing_eval(Evas_Object *obj)
96 {
97    Widget_Data *wd = elm_widget_data_get(obj);
98    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
99
100    if (!wd) return;
101    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
102    edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
103    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
104    evas_object_size_hint_min_set(obj, minw, minh);
105    evas_object_size_hint_max_set(obj, maxw, maxh);
106 }
107
108 static void
109 _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info)
110 {
111    Widget_Data *wd = elm_widget_data_get(data);
112    if (!wd) return;
113    if (obj != wd->icon) return;
114    _sizing_eval(data);
115 }
116
117 static void
118 _sub_del(void *data, Evas_Object *obj, void *event_info)
119 {
120    Widget_Data *wd = elm_widget_data_get(obj);
121    Evas_Object *sub = event_info;
122    if (!wd) return;
123    if (sub == wd->icon)
124      {
125         edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
126         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
127                                        _changed_size_hints, obj);
128         wd->icon = NULL;
129         edje_object_message_signal_process(wd->btn);
130         _sizing_eval(obj);
131      }
132 }
133
134 static void
135 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
136 {
137    Widget_Data *wd = elm_widget_data_get(data);
138    if (!wd) return;
139    evas_object_smart_callback_call(data, "clicked", NULL);
140 }
141
142 /**
143  * Add a new button to the parent
144  * @param parent The parent object
145  * @return The new object or NULL if it cannot be created
146  *
147  * @ingroup Button
148  */
149 EAPI Evas_Object *
150 elm_button_add(Evas_Object *parent)
151 {
152    Evas_Object *obj;
153    Evas *e;
154    Widget_Data *wd;
155
156    wd = ELM_NEW(Widget_Data);
157    e = evas_object_evas_get(parent);
158    obj = elm_widget_add(e);
159    elm_widget_type_set(obj, "button");
160    elm_widget_sub_object_add(parent, obj);
161    elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
162    elm_widget_data_set(obj, wd);
163    elm_widget_del_hook_set(obj, _del_hook);
164    elm_widget_theme_hook_set(obj, _theme_hook);
165    elm_widget_disable_hook_set(obj, _disable_hook);
166    elm_widget_can_focus_set( obj, 1 );                 
167
168    wd->btn = edje_object_add(e);
169    _elm_theme_set(wd->btn, "button", "base", "default");
170    edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
171                                    _signal_clicked, obj);
172    elm_widget_resize_object_set(obj, wd->btn);
173
174    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
175
176    _sizing_eval(obj);
177    return obj;
178 }
179
180 /**
181  * Set the label used in the button
182  *
183  * @param obj The button object
184  * @param label The text will be written on the button
185  *
186  * @ingroup Button
187  */
188 EAPI void
189 elm_button_label_set(Evas_Object *obj, const char *label)
190 {
191    Widget_Data *wd = elm_widget_data_get(obj);
192    Evas_Coord mw, mh;
193
194    if (!wd) return;
195    if (wd->label) eina_stringshare_del(wd->label);
196    if (label)
197      {
198         wd->label = eina_stringshare_add(label);
199         edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
200      }
201    else
202      {
203         wd->label = NULL;
204         edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
205      }
206    edje_object_message_signal_process(wd->btn);
207    edje_object_part_text_set(wd->btn, "elm.text", label);
208    _sizing_eval(obj);
209 }
210
211 EAPI const char*
212 elm_button_label_get(Evas_Object *obj)
213 {
214    Widget_Data *wd = elm_widget_data_get(obj);
215    if (!wd) return NULL;
216    return wd->label;
217 }
218
219 /**
220  * Set the icon used for the button
221  *
222  * @param obj The button object
223  * @param icon  The image for the button
224  *
225  * @ingroup Button
226  */
227 EAPI void
228 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
229 {
230    Widget_Data *wd = elm_widget_data_get(obj);
231    if (!wd) return;
232    if ((wd->icon != icon) && (wd->icon))
233      elm_widget_sub_object_del(obj, wd->icon);
234    if ((icon) && (wd->icon != icon))
235      {
236         wd->icon = icon;
237         elm_widget_sub_object_add(obj, icon);
238         evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
239                                        _changed_size_hints, obj);
240         edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
241         edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
242         edje_object_message_signal_process(wd->btn);
243         _sizing_eval(obj);
244      }
245    else
246      wd->icon = icon;
247 }
248
249 EAPI Evas_Object *
250 elm_button_icon_get(Evas_Object *obj)
251 {
252    Widget_Data *wd = elm_widget_data_get(obj);
253    if (!wd) return NULL;
254    return wd->icon;
255 }
256
257 /**
258  * Set the button style
259  *
260  * @param obj The button object
261  * @param style The style for the button
262  *
263  * DEPRECATED. use elm_object_style_set() instead
264  *
265  * @ingroup Button
266  */
267 EAPI void
268 elm_button_style_set(Evas_Object *obj, const char *style)
269 {
270    elm_widget_style_set(obj, style);
271 }