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 __UNUSED__, 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 __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
111 Widget_Data *wd = elm_widget_data_get(data);
113 if (obj != wd->icon) return;
118 _sub_del(void *data __UNUSED__, 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 __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
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 __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
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 eina_stringshare_replace(&wd->label, label);
256 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
258 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
259 edje_object_message_signal_process(wd->btn);
260 edje_object_part_text_set(wd->btn, "elm.text", label);
265 elm_button_label_get(Evas_Object *obj)
267 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
268 Widget_Data *wd = elm_widget_data_get(obj);
269 if (!wd) return NULL;
274 * Set the icon used for the button
276 * @param obj The button object
277 * @param icon The image for the button
282 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
284 ELM_CHECK_WIDTYPE(obj, widtype);
285 Widget_Data *wd = elm_widget_data_get(obj);
287 if ((wd->icon != icon) && (wd->icon))
288 elm_widget_sub_object_del(obj, wd->icon);
289 if ((icon) && (wd->icon != icon))
292 elm_widget_sub_object_add(obj, icon);
293 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
294 _changed_size_hints, obj);
295 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
296 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
297 edje_object_message_signal_process(wd->btn);
305 * Get the icon used for the button
307 * @param obj The button object
308 * @return The image for the button
313 elm_button_icon_get(Evas_Object *obj)
315 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
316 Widget_Data *wd = elm_widget_data_get(obj);
317 if (!wd) return NULL;
322 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
324 * @param obj The button object
325 * @param on A bool to turn on/off the event
330 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
332 ELM_CHECK_WIDTYPE(obj, widtype);
333 Widget_Data *wd = elm_widget_data_get(obj);
337 ecore_timer_del(wd->timer);
344 * Set the initial timeout before the autorepeat event is generated
346 * @param obj The button object
352 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
354 ELM_CHECK_WIDTYPE(obj, widtype);
355 Widget_Data *wd = elm_widget_data_get(obj);
357 if (wd->ar_threshold == t) return;
360 ecore_timer_del(wd->timer);
363 wd->ar_threshold = t;
367 * Set the interval between each generated autorepeat event
369 * @param obj The button object
375 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
377 ELM_CHECK_WIDTYPE(obj, widtype);
378 Widget_Data *wd = elm_widget_data_get(obj);
380 if (wd->ar_interval == t) return;
383 ecore_timer_del(wd->timer);
388 wd->timer = ecore_timer_add(t, _autorepeat_send, obj);