3 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
5 #include <Elementary.h>
9 * @defgroup Button Button
11 * This is a push-button. Press it and run some function. It can contain
12 * a simple label and icon object.
15 typedef struct _Widget_Data Widget_Data;
19 Evas_Object *btn, *icon;
29 static const char *widtype = NULL;
30 static void _del_hook(Evas_Object *obj);
31 static void _theme_hook(Evas_Object *obj);
32 static void _disable_hook(Evas_Object *obj);
33 static void _sizing_eval(Evas_Object *obj);
34 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
35 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
36 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
37 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
38 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
39 static void _on_focus_hook(void *data, Evas_Object *obj);
42 _del_hook(Evas_Object *obj)
44 Widget_Data *wd = elm_widget_data_get(obj);
46 if (wd->label) eina_stringshare_del(wd->label);
51 _on_focus_hook(void *data, Evas_Object *obj)
53 Widget_Data *wd = elm_widget_data_get(obj);
55 if (elm_widget_focus_get(obj))
56 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
58 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
62 _theme_hook(Evas_Object *obj)
64 Widget_Data *wd = elm_widget_data_get(obj);
66 _elm_theme_set(wd->btn, "button", "base", elm_widget_style_get(obj));
68 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
70 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
72 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
74 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
76 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
77 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
78 edje_object_message_signal_process(wd->btn);
79 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
84 _disable_hook(Evas_Object *obj)
86 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);
140 _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
144 _autorepeat_send(void *data)
146 Widget_Data *wd = elm_widget_data_get(data);
147 if (!wd) return ECORE_CALLBACK_CANCEL;
149 evas_object_smart_callback_call(data, "repeated", NULL);
151 return ECORE_CALLBACK_RENEW;
155 _autorepeat_initial_send(void *data)
157 Widget_Data *wd = elm_widget_data_get(data);
158 if (!wd) return ECORE_CALLBACK_CANCEL;
160 _autorepeat_send(data);
161 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
164 return ECORE_CALLBACK_CANCEL;
168 _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source)
170 Widget_Data *wd = elm_widget_data_get(data);
175 if (wd->ar_threshold <= 0.0)
176 _autorepeat_initial_send(data); /* call immediately */
178 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
183 _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source)
185 Widget_Data *wd = elm_widget_data_get(data);
187 evas_object_smart_callback_call(data, "unpressed", NULL);
191 ecore_timer_del(wd->timer);
198 * Add a new button to the parent
199 * @param parent The parent object
200 * @return The new object or NULL if it cannot be created
205 elm_button_add(Evas_Object *parent)
211 wd = ELM_NEW(Widget_Data);
212 e = evas_object_evas_get(parent);
213 obj = elm_widget_add(e);
214 ELM_SET_WIDTYPE(widtype, "button");
215 elm_widget_type_set(obj, "button");
216 elm_widget_sub_object_add(parent, obj);
217 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
218 elm_widget_data_set(obj, wd);
219 elm_widget_del_hook_set(obj, _del_hook);
220 elm_widget_theme_hook_set(obj, _theme_hook);
221 elm_widget_disable_hook_set(obj, _disable_hook);
222 elm_widget_can_focus_set(obj, 1 );
224 wd->btn = edje_object_add(e);
225 _elm_theme_set(wd->btn, "button", "base", "default");
226 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
227 _signal_clicked, obj);
228 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
229 _signal_pressed, obj);
230 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
231 _signal_unpressed, obj);
232 elm_widget_resize_object_set(obj, wd->btn);
234 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
241 * Set the label used in the button
243 * @param obj The button object
244 * @param label The text will be written on the button
249 elm_button_label_set(Evas_Object *obj, const char *label)
251 ELM_CHECK_WIDTYPE(obj, widtype);
252 Widget_Data *wd = elm_widget_data_get(obj);
254 if (wd->label) eina_stringshare_del(wd->label);
257 wd->label = eina_stringshare_add(label);
258 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
263 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
265 edje_object_message_signal_process(wd->btn);
266 edje_object_part_text_set(wd->btn, "elm.text", label);
271 elm_button_label_get(Evas_Object *obj)
273 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
274 Widget_Data *wd = elm_widget_data_get(obj);
275 if (!wd) return NULL;
280 * Set the icon used for the button
282 * @param obj The button object
283 * @param icon The image for the button
288 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
290 ELM_CHECK_WIDTYPE(obj, widtype);
291 Widget_Data *wd = elm_widget_data_get(obj);
293 if ((wd->icon != icon) && (wd->icon))
294 elm_widget_sub_object_del(obj, wd->icon);
295 if ((icon) && (wd->icon != icon))
298 elm_widget_sub_object_add(obj, icon);
299 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
300 _changed_size_hints, obj);
301 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
302 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
303 edje_object_message_signal_process(wd->btn);
311 * Get the icon used for the button
313 * @param obj The button object
314 * @return The image for the button
319 elm_button_icon_get(Evas_Object *obj)
321 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
322 Widget_Data *wd = elm_widget_data_get(obj);
323 if (!wd) return NULL;
328 * Set the button style
330 * @param obj The button object
331 * @param style The style for the button
333 * DEPRECATED. use elm_object_style_set() instead
338 elm_button_style_set(Evas_Object *obj, const char *style)
340 elm_widget_style_set(obj, style);
344 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
346 * @param obj The button object
347 * @param on A bool to turn on/off the event
352 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
354 ELM_CHECK_WIDTYPE(obj, widtype);
355 Widget_Data *wd = elm_widget_data_get(obj);
359 ecore_timer_del(wd->timer);
366 * Set the initial timeout before the autorepeat event is generated
368 * @param obj The button object
374 elm_button_autorepeat_initital_timeout_set(Evas_Object *obj, double t)
376 ELM_CHECK_WIDTYPE(obj, widtype);
377 Widget_Data *wd = elm_widget_data_get(obj);
379 if (wd->ar_threshold == t) return;
382 ecore_timer_del(wd->timer);
385 wd->ar_threshold = t;
389 * Set the interval between each generated autorepeat event
391 * @param obj The button object
397 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
399 ELM_CHECK_WIDTYPE(obj, widtype);
400 Widget_Data *wd = elm_widget_data_get(obj);
402 if (wd->ar_interval == t) return;
405 ecore_timer_del(wd->timer);
410 wd->timer = ecore_timer_add(t, _autorepeat_send, obj);