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");
46 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
50 _theme_hook(Evas_Object *obj)
52 Widget_Data *wd = elm_widget_data_get(obj);
54 _elm_theme_set(wd->btn, "button", "base", elm_widget_style_get(obj));
56 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
58 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
60 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
62 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
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);
72 _disable_hook(Evas_Object *obj)
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");
78 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
82 _sizing_eval(Evas_Object *obj)
84 Widget_Data *wd = elm_widget_data_get(obj);
85 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
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);
96 _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info)
98 Widget_Data *wd = elm_widget_data_get(data);
100 if (obj != wd->icon) return;
105 _sub_del(void *data, Evas_Object *obj, void *event_info)
107 Widget_Data *wd = elm_widget_data_get(obj);
108 Evas_Object *sub = event_info;
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);
116 edje_object_message_signal_process(wd->btn);
122 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
124 Widget_Data *wd = elm_widget_data_get(data);
126 evas_object_smart_callback_call(data, "clicked", NULL);
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
137 elm_button_add(Evas_Object *parent)
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 );
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);
161 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
168 * Set the label used in the button
170 * @param obj The button object
171 * @param label The text will be written on the button
176 elm_button_label_set(Evas_Object *obj, const char *label)
178 Widget_Data *wd = elm_widget_data_get(obj);
182 if (wd->label) eina_stringshare_del(wd->label);
185 wd->label = eina_stringshare_add(label);
186 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
191 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
193 edje_object_message_signal_process(wd->btn);
194 edje_object_part_text_set(wd->btn, "elm.text", label);
199 elm_button_label_get(Evas_Object *obj)
201 Widget_Data *wd = elm_widget_data_get(obj);
202 if (!wd) return NULL;
207 * Set the icon used for the button
209 * @param obj The button object
210 * @param icon The image for the button
215 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
217 Widget_Data *wd = elm_widget_data_get(obj);
219 if ((wd->icon != icon) && (wd->icon))
220 elm_widget_sub_object_del(obj, wd->icon);
221 if ((icon) && (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);
237 elm_button_icon_get(Evas_Object *obj)
239 Widget_Data *wd = elm_widget_data_get(obj);
240 if (!wd) return NULL;
245 * Set the button style
247 * @param obj The button object
248 * @param style The style for the button
250 * DEPRECATED. use elm_object_style_set() instead
255 elm_button_style_set(Evas_Object *obj, const char *style)
257 elm_widget_style_set(obj, style);