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