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 void _del_hook(Evas_Object *obj);
30 static void _theme_hook(Evas_Object *obj);
31 static void _disable_hook(Evas_Object *obj);
32 static void _sizing_eval(Evas_Object *obj);
33 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
34 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
35 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
36 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
37 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
38 static void _on_focus_hook(void *data, Evas_Object *obj);
41 _del_hook(Evas_Object *obj)
43 Widget_Data *wd = elm_widget_data_get(obj);
45 if (wd->label) eina_stringshare_del(wd->label);
50 _on_focus_hook(void *data, Evas_Object *obj)
52 Widget_Data *wd = elm_widget_data_get(obj);
53 Evas_Object *top = elm_widget_top_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);
87 if (elm_widget_disabled_get(obj))
88 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
90 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
94 _sizing_eval(Evas_Object *obj)
96 Widget_Data *wd = elm_widget_data_get(obj);
97 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
100 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
101 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
102 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
103 evas_object_size_hint_min_set(obj, minw, minh);
104 evas_object_size_hint_max_set(obj, maxw, maxh);
108 _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info)
110 Widget_Data *wd = elm_widget_data_get(data);
112 if (obj != wd->icon) return;
117 _sub_del(void *data, Evas_Object *obj, void *event_info)
119 Widget_Data *wd = elm_widget_data_get(obj);
120 Evas_Object *sub = event_info;
124 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
125 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
126 _changed_size_hints, obj);
128 edje_object_message_signal_process(wd->btn);
134 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
136 Widget_Data *wd = elm_widget_data_get(data);
138 evas_object_smart_callback_call(data, "clicked", NULL);
139 _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
143 _autorepeat_send(void *data)
145 Widget_Data *wd = elm_widget_data_get(data);
146 if (!wd) return ECORE_CALLBACK_CANCEL;
148 evas_object_smart_callback_call(data, "repeated", NULL);
150 return ECORE_CALLBACK_RENEW;
154 _autorepeat_initial_send(void *data)
156 Widget_Data *wd = elm_widget_data_get(data);
157 if (!wd) return ECORE_CALLBACK_CANCEL;
159 _autorepeat_send(data);
160 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
163 return ECORE_CALLBACK_CANCEL;
167 _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source)
169 Widget_Data *wd = elm_widget_data_get(data);
174 if (wd->ar_threshold <= 0.0)
175 _autorepeat_initial_send(data); /* call immediately */
177 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
182 _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source)
184 Widget_Data *wd = elm_widget_data_get(data);
186 evas_object_smart_callback_call(data, "unpressed", NULL);
190 ecore_timer_del(wd->timer);
197 * Add a new button to the parent
198 * @param parent The parent object
199 * @return The new object or NULL if it cannot be created
204 elm_button_add(Evas_Object *parent)
210 wd = ELM_NEW(Widget_Data);
211 e = evas_object_evas_get(parent);
212 obj = elm_widget_add(e);
213 elm_widget_type_set(obj, "button");
214 elm_widget_sub_object_add(parent, obj);
215 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
216 elm_widget_data_set(obj, wd);
217 elm_widget_del_hook_set(obj, _del_hook);
218 elm_widget_theme_hook_set(obj, _theme_hook);
219 elm_widget_disable_hook_set(obj, _disable_hook);
220 elm_widget_can_focus_set(obj, 1 );
222 wd->btn = edje_object_add(e);
223 _elm_theme_set(wd->btn, "button", "base", "default");
224 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
225 _signal_clicked, obj);
226 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
227 _signal_pressed, obj);
228 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
229 _signal_unpressed, obj);
230 elm_widget_resize_object_set(obj, wd->btn);
232 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
239 * Set the label used in the button
241 * @param obj The button object
242 * @param label The text will be written on the button
247 elm_button_label_set(Evas_Object *obj, const char *label)
249 Widget_Data *wd = elm_widget_data_get(obj);
253 if (wd->label) eina_stringshare_del(wd->label);
256 wd->label = eina_stringshare_add(label);
257 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
262 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
264 edje_object_message_signal_process(wd->btn);
265 edje_object_part_text_set(wd->btn, "elm.text", label);
270 elm_button_label_get(Evas_Object *obj)
272 Widget_Data *wd = elm_widget_data_get(obj);
273 if (!wd) return NULL;
278 * Set the icon used for the button
280 * @param obj The button object
281 * @param icon The image for the button
286 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
288 Widget_Data *wd = elm_widget_data_get(obj);
290 if ((wd->icon != icon) && (wd->icon))
291 elm_widget_sub_object_del(obj, wd->icon);
292 if ((icon) && (wd->icon != icon))
295 elm_widget_sub_object_add(obj, icon);
296 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
297 _changed_size_hints, obj);
298 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
299 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
300 edje_object_message_signal_process(wd->btn);
308 elm_button_icon_get(Evas_Object *obj)
310 Widget_Data *wd = elm_widget_data_get(obj);
311 if (!wd) return NULL;
316 * Set the button style
318 * @param obj The button object
319 * @param style The style for the button
321 * DEPRECATED. use elm_object_style_set() instead
326 elm_button_style_set(Evas_Object *obj, const char *style)
328 elm_widget_style_set(obj, style);
332 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
334 * @param obj The button object
335 * @param on A bool to turn on/off the event
340 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
342 Widget_Data *wd = elm_widget_data_get(obj);
346 ecore_timer_del(wd->timer);
353 * Set the initial timeout before the autorepeat event is generated
355 * @param obj The button object
361 elm_button_autorepeat_initital_timeout_set(Evas_Object *obj, double t)
363 Widget_Data *wd = elm_widget_data_get(obj);
366 if (wd->ar_threshold == t)
371 ecore_timer_del(wd->timer);
375 wd->ar_threshold = t;
379 * Set the interval between each generated autorepeat event
381 * @param obj The button object
387 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
389 Widget_Data *wd = elm_widget_data_get(obj);
392 if (wd->ar_interval == t)
397 ecore_timer_del(wd->timer);
404 wd->timer = ecore_timer_add(t, _autorepeat_send, obj);