1 #include <Elementary.h>
5 * @defgroup Button Button
7 * This is a push-button. Press it and run some function. It can contain
8 * a simple label and icon object.
11 typedef struct _Widget_Data Widget_Data;
15 Evas_Object *btn, *icon;
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);
29 _del_hook(Evas_Object *obj)
31 Widget_Data *wd = elm_widget_data_get(obj);
33 if (wd->label) eina_stringshare_del(wd->label);
38 _on_focus_hook(void *data, Evas_Object *obj)
40 Widget_Data *wd = elm_widget_data_get(obj);
41 Evas_Object *top = elm_widget_top_get(obj);
43 if (elm_widget_focus_get(obj)) {
44 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
47 elm_win_keyboard_mode_set( top, ELM_WIN_KEYBOARD_ON );
51 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
54 elm_win_keyboard_mode_set(top, ELM_WIN_KEYBOARD_OFF);
63 _theme_hook(Evas_Object *obj)
65 Widget_Data *wd = elm_widget_data_get(obj);
67 _elm_theme_set(wd->btn, "button", "base", elm_widget_style_get(obj));
69 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
71 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
73 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
75 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
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);
85 _disable_hook(Evas_Object *obj)
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");
91 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
95 _sizing_eval(Evas_Object *obj)
97 Widget_Data *wd = elm_widget_data_get(obj);
98 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
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);
109 _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info)
111 Widget_Data *wd = elm_widget_data_get(data);
113 if (obj != wd->icon) return;
118 _sub_del(void *data, Evas_Object *obj, void *event_info)
120 Widget_Data *wd = elm_widget_data_get(obj);
121 Evas_Object *sub = event_info;
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);
129 edje_object_message_signal_process(wd->btn);
135 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
137 Widget_Data *wd = elm_widget_data_get(data);
139 evas_object_smart_callback_call(data, "clicked", NULL);
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
150 elm_button_add(Evas_Object *parent)
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 );
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);
174 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
181 * Set the label used in the button
183 * @param obj The button object
184 * @param label The text will be written on the button
189 elm_button_label_set(Evas_Object *obj, const char *label)
191 Widget_Data *wd = elm_widget_data_get(obj);
195 if (wd->label) eina_stringshare_del(wd->label);
198 wd->label = eina_stringshare_add(label);
199 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
204 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
206 edje_object_message_signal_process(wd->btn);
207 edje_object_part_text_set(wd->btn, "elm.text", label);
212 elm_button_label_get(Evas_Object *obj)
214 Widget_Data *wd = elm_widget_data_get(obj);
215 if (!wd) return NULL;
220 * Set the icon used for the button
222 * @param obj The button object
223 * @param icon The image for the button
228 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
230 Widget_Data *wd = elm_widget_data_get(obj);
232 if ((wd->icon != icon) && (wd->icon))
233 elm_widget_sub_object_del(obj, wd->icon);
234 if ((icon) && (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);
250 elm_button_icon_get(Evas_Object *obj)
252 Widget_Data *wd = elm_widget_data_get(obj);
253 if (!wd) return NULL;
258 * Set the button style
260 * @param obj The button object
261 * @param style The style for the button
263 * DEPRECATED. use elm_object_style_set() instead
268 elm_button_style_set(Evas_Object *obj, const char *style)
270 elm_widget_style_set(obj, style);