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);
35 static void _activate(Evas_Object *obj);
36 static void _activate_hook(Evas_Object *obj);
38 static const char SIG_CLICKED[] = "clicked";
39 static const char SIG_REPEATED[] = "repeated";
40 static const char SIG_UNPRESSED[] = "unpressed";
41 static const Evas_Smart_Cb_Description _signals[] = {
49 _del_hook(Evas_Object *obj)
51 Widget_Data *wd = elm_widget_data_get(obj);
53 if (wd->label) eina_stringshare_del(wd->label);
58 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
60 Widget_Data *wd = elm_widget_data_get(obj);
62 if (elm_widget_focus_get(obj))
64 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
65 evas_object_focus_set(wd->btn, EINA_TRUE);
69 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
70 evas_object_focus_set(wd->btn, EINA_FALSE);
75 _theme_hook(Evas_Object *obj)
77 Widget_Data *wd = elm_widget_data_get(obj);
79 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
81 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
83 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
85 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
87 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
89 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
90 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
91 edje_object_message_signal_process(wd->btn);
92 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
97 _disable_hook(Evas_Object *obj)
99 Widget_Data *wd = elm_widget_data_get(obj);
101 if (elm_widget_disabled_get(obj))
102 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
104 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
108 _sizing_eval(Evas_Object *obj)
110 Widget_Data *wd = elm_widget_data_get(obj);
111 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
114 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
115 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
116 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
117 evas_object_size_hint_min_set(obj, minw, minh);
118 evas_object_size_hint_max_set(obj, maxw, maxh);
122 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
124 Widget_Data *wd = elm_widget_data_get(data);
126 if (obj != wd->icon) return;
131 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
133 Widget_Data *wd = elm_widget_data_get(obj);
134 Evas_Object *sub = event_info;
138 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
139 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
140 _changed_size_hints, obj);
142 edje_object_message_signal_process(wd->btn);
148 _activate(Evas_Object *obj)
150 Widget_Data *wd = elm_widget_data_get(obj);
154 ecore_timer_del(wd->timer);
157 wd->repeating = EINA_FALSE;
158 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
159 _signal_unpressed(obj, wd->btn, NULL, NULL); /* safe guard when the theme does not emit the 'unpress' signal */
163 _activate_hook(Evas_Object *obj)
169 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
175 _autorepeat_send(void *data)
177 Widget_Data *wd = elm_widget_data_get(data);
178 if (!wd) return ECORE_CALLBACK_CANCEL;
180 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
184 return ECORE_CALLBACK_CANCEL;
187 return ECORE_CALLBACK_RENEW;
191 _autorepeat_initial_send(void *data)
193 Widget_Data *wd = elm_widget_data_get(data);
194 if (!wd) return ECORE_CALLBACK_CANCEL;
196 if (wd->timer) ecore_timer_del(wd->timer);
197 wd->repeating = EINA_TRUE;
198 _autorepeat_send(data);
199 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
201 return ECORE_CALLBACK_CANCEL;
205 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
207 Widget_Data *wd = elm_widget_data_get(data);
210 if (wd->autorepeat && !wd->repeating)
212 if (wd->ar_threshold <= 0.0)
213 _autorepeat_initial_send(data); /* call immediately */
215 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
220 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
222 Widget_Data *wd = elm_widget_data_get(data);
227 ecore_timer_del(wd->timer);
230 wd->repeating = EINA_FALSE;
231 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
235 * Add a new button to the parent
236 * @param parent The parent object
237 * @return The new object or NULL if it cannot be created
242 elm_button_add(Evas_Object *parent)
248 wd = ELM_NEW(Widget_Data);
249 e = evas_object_evas_get(parent);
250 obj = elm_widget_add(e);
251 ELM_SET_WIDTYPE(widtype, "button");
252 elm_widget_type_set(obj, "button");
253 elm_widget_sub_object_add(parent, obj);
254 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
255 elm_widget_data_set(obj, wd);
256 elm_widget_del_hook_set(obj, _del_hook);
257 elm_widget_theme_hook_set(obj, _theme_hook);
258 elm_widget_disable_hook_set(obj, _disable_hook);
259 elm_widget_can_focus_set(obj, 1);
260 elm_widget_activate_hook_set(obj, _activate_hook);
262 wd->btn = edje_object_add(e);
263 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
264 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
265 _signal_clicked, obj);
266 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
267 _signal_pressed, obj);
268 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
269 _signal_unpressed, obj);
270 elm_widget_resize_object_set(obj, wd->btn);
272 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
276 // TODO: convert Elementary to subclassing of Evas_Smart_Class
277 // TODO: and save some bytes, making descriptions per-class and not instance!
278 evas_object_smart_callbacks_descriptions_set(obj, _signals);
283 * Set the label used in the button
285 * @param obj The button object
286 * @param label The text will be written on the button
291 elm_button_label_set(Evas_Object *obj, const char *label)
293 ELM_CHECK_WIDTYPE(obj, widtype);
294 Widget_Data *wd = elm_widget_data_get(obj);
296 eina_stringshare_replace(&wd->label, label);
298 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
300 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
301 edje_object_message_signal_process(wd->btn);
302 edje_object_part_text_set(wd->btn, "elm.text", label);
307 elm_button_label_get(const Evas_Object *obj)
309 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
310 Widget_Data *wd = elm_widget_data_get(obj);
311 if (!wd) return NULL;
316 * Set the icon used for the button
318 * Once the icon object is set, a previously set one will be deleted
320 * @param obj The button object
321 * @param icon The image for the button
326 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
328 ELM_CHECK_WIDTYPE(obj, widtype);
329 Widget_Data *wd = elm_widget_data_get(obj);
331 if (wd->icon == icon) return;
332 if (wd->icon) evas_object_del(wd->icon);
336 elm_widget_sub_object_add(obj, icon);
337 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
338 _changed_size_hints, obj);
339 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
340 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
341 edje_object_message_signal_process(wd->btn);
347 * Get the icon used for the button
349 * @param obj The button object
350 * @return The image for the button
355 elm_button_icon_get(const Evas_Object *obj)
357 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
358 Widget_Data *wd = elm_widget_data_get(obj);
359 if (!wd) return NULL;
364 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
366 * @param obj The button object
367 * @param on A bool to turn on/off the event
372 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
374 ELM_CHECK_WIDTYPE(obj, widtype);
375 Widget_Data *wd = elm_widget_data_get(obj);
379 ecore_timer_del(wd->timer);
383 wd->repeating = EINA_FALSE;
387 * Set the initial timeout before the autorepeat event is generated
389 * @param obj The button object
395 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
397 ELM_CHECK_WIDTYPE(obj, widtype);
398 Widget_Data *wd = elm_widget_data_get(obj);
400 if (wd->ar_threshold == t) return;
403 ecore_timer_del(wd->timer);
406 wd->ar_threshold = t;
410 * Set the interval between each generated autorepeat event
412 * @param obj The button object
418 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
420 ELM_CHECK_WIDTYPE(obj, widtype);
421 Widget_Data *wd = elm_widget_data_get(obj);
423 if (wd->ar_interval == t) return;
426 if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);