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