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;
24 static const char *widtype = NULL;
25 static void _del_hook(Evas_Object *obj);
26 static void _theme_hook(Evas_Object *obj);
27 static void _disable_hook(Evas_Object *obj);
28 static void _sizing_eval(Evas_Object *obj);
29 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
30 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
31 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
32 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
33 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
34 static void _on_focus_hook(void *data, Evas_Object *obj);
36 static const char SIG_CLICKED[] = "clicked";
37 static const char SIG_REPEATED[] = "repeated";
38 static const char SIG_UNPRESSED[] = "unpressed";
39 static const Evas_Smart_Cb_Description _signals[] = {
47 _del_hook(Evas_Object *obj)
49 Widget_Data *wd = elm_widget_data_get(obj);
51 if (wd->label) eina_stringshare_del(wd->label);
56 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
58 Widget_Data *wd = elm_widget_data_get(obj);
60 if (elm_widget_focus_get(obj))
62 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
63 evas_object_focus_set(wd->btn, EINA_TRUE);
67 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
68 evas_object_focus_set(wd->btn, EINA_FALSE);
73 _theme_hook(Evas_Object *obj)
75 Widget_Data *wd = elm_widget_data_get(obj);
77 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
79 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
81 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
83 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
85 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
87 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
88 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
89 edje_object_message_signal_process(wd->btn);
90 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
95 _disable_hook(Evas_Object *obj)
97 Widget_Data *wd = elm_widget_data_get(obj);
99 if (elm_widget_disabled_get(obj))
100 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
102 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
106 _sizing_eval(Evas_Object *obj)
108 Widget_Data *wd = elm_widget_data_get(obj);
109 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
112 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
113 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
114 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
115 evas_object_size_hint_min_set(obj, minw, minh);
116 evas_object_size_hint_max_set(obj, maxw, maxh);
120 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
122 Widget_Data *wd = elm_widget_data_get(data);
124 if (obj != wd->icon) return;
129 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
131 Widget_Data *wd = elm_widget_data_get(obj);
132 Evas_Object *sub = event_info;
136 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
137 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
138 _changed_size_hints, obj);
140 edje_object_message_signal_process(wd->btn);
146 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
148 Widget_Data *wd = elm_widget_data_get(data);
152 ecore_timer_del(wd->timer);
155 wd->repeating = EINA_FALSE;
156 evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
157 _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
161 _autorepeat_send(void *data)
163 Widget_Data *wd = elm_widget_data_get(data);
164 if (!wd) return ECORE_CALLBACK_CANCEL;
166 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
170 return ECORE_CALLBACK_CANCEL;
173 return ECORE_CALLBACK_RENEW;
177 _autorepeat_initial_send(void *data)
179 Widget_Data *wd = elm_widget_data_get(data);
180 if (!wd) return ECORE_CALLBACK_CANCEL;
182 if (wd->timer) ecore_timer_del(wd->timer);
183 wd->repeating = EINA_TRUE;
184 _autorepeat_send(data);
185 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
187 return ECORE_CALLBACK_CANCEL;
191 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
193 Widget_Data *wd = elm_widget_data_get(data);
196 if (wd->autorepeat && !wd->repeating)
198 if (wd->ar_threshold <= 0.0)
199 _autorepeat_initial_send(data); /* call immediately */
201 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
206 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
208 Widget_Data *wd = elm_widget_data_get(data);
213 ecore_timer_del(wd->timer);
216 wd->repeating = EINA_FALSE;
217 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
221 * Add a new button to the parent
222 * @param parent The parent object
223 * @return The new object or NULL if it cannot be created
228 elm_button_add(Evas_Object *parent)
234 wd = ELM_NEW(Widget_Data);
235 e = evas_object_evas_get(parent);
236 obj = elm_widget_add(e);
237 ELM_SET_WIDTYPE(widtype, "button");
238 elm_widget_type_set(obj, "button");
239 elm_widget_sub_object_add(parent, obj);
240 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
241 elm_widget_data_set(obj, wd);
242 elm_widget_del_hook_set(obj, _del_hook);
243 elm_widget_theme_hook_set(obj, _theme_hook);
244 elm_widget_disable_hook_set(obj, _disable_hook);
245 elm_widget_can_focus_set(obj, 1);
247 wd->btn = edje_object_add(e);
248 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
249 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
250 _signal_clicked, obj);
251 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
252 _signal_pressed, obj);
253 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
254 _signal_unpressed, obj);
255 elm_widget_resize_object_set(obj, wd->btn);
257 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
261 // TODO: convert Elementary to subclassing of Evas_Smart_Class
262 // TODO: and save some bytes, making descriptions per-class and not instance!
263 evas_object_smart_callbacks_descriptions_set(obj, _signals);
268 * Set the label used in the button
270 * @param obj The button object
271 * @param label The text will be written on the button
276 elm_button_label_set(Evas_Object *obj, const char *label)
278 ELM_CHECK_WIDTYPE(obj, widtype);
279 Widget_Data *wd = elm_widget_data_get(obj);
281 eina_stringshare_replace(&wd->label, label);
283 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
285 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
286 edje_object_message_signal_process(wd->btn);
287 edje_object_part_text_set(wd->btn, "elm.text", label);
292 elm_button_label_get(const Evas_Object *obj)
294 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
295 Widget_Data *wd = elm_widget_data_get(obj);
296 if (!wd) return NULL;
301 * Set the icon used for the button
303 * Once the icon object is set, a previously set one will be deleted
305 * @param obj The button object
306 * @param icon The image for the button
311 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
313 ELM_CHECK_WIDTYPE(obj, widtype);
314 Widget_Data *wd = elm_widget_data_get(obj);
316 if (wd->icon == icon) return;
317 if (wd->icon) evas_object_del(wd->icon);
321 elm_widget_sub_object_add(obj, icon);
322 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
323 _changed_size_hints, obj);
324 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
325 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
326 edje_object_message_signal_process(wd->btn);
332 * Get the icon used for the button
334 * @param obj The button object
335 * @return The image for the button
340 elm_button_icon_get(const Evas_Object *obj)
342 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
343 Widget_Data *wd = elm_widget_data_get(obj);
344 if (!wd) return NULL;
349 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
351 * @param obj The button object
352 * @param on A bool to turn on/off the event
357 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
359 ELM_CHECK_WIDTYPE(obj, widtype);
360 Widget_Data *wd = elm_widget_data_get(obj);
364 ecore_timer_del(wd->timer);
368 wd->repeating = EINA_FALSE;
372 * Set the initial timeout before the autorepeat event is generated
374 * @param obj The button object
380 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
382 ELM_CHECK_WIDTYPE(obj, widtype);
383 Widget_Data *wd = elm_widget_data_get(obj);
385 if (wd->ar_threshold == t) return;
388 ecore_timer_del(wd->timer);
391 wd->ar_threshold = t;
395 * Set the interval between each generated autorepeat event
397 * @param obj The button object
403 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
405 ELM_CHECK_WIDTYPE(obj, widtype);
406 Widget_Data *wd = elm_widget_data_get(obj);
408 if (wd->ar_interval == t) return;
411 if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);