2 #include <Elementary.h>
6 * @defgroup Button Button
8 * This is a push-button. Press it and run some function. It can contain
9 * a simple label and icon object.
12 typedef struct _Widget_Data Widget_Data;
16 Evas_Object *btn, *icon;
25 static const char *widtype = NULL;
26 static void _del_hook(Evas_Object *obj);
27 static void _theme_hook(Evas_Object *obj);
28 static void _disable_hook(Evas_Object *obj);
29 static void _sizing_eval(Evas_Object *obj);
30 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
31 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
32 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
33 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
34 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
35 static void _on_focus_hook(void *data, Evas_Object *obj);
37 static const char SIG_CLICKED[] = "clicked";
38 static const char SIG_REPEATED[] = "repeated";
39 static const char SIG_UNPRESSED[] = "unpressed";
40 static const Evas_Smart_Cb_Description _signals[] = {
48 _del_hook(Evas_Object *obj)
50 Widget_Data *wd = elm_widget_data_get(obj);
52 if (wd->label) eina_stringshare_del(wd->label);
57 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
59 Widget_Data *wd = elm_widget_data_get(obj);
61 if (elm_widget_focus_get(obj))
63 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
64 evas_object_focus_set(wd->btn, 1);
68 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
69 evas_object_focus_set(wd->btn, 0);
74 _theme_hook(Evas_Object *obj)
76 Widget_Data *wd = elm_widget_data_get(obj);
78 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
80 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
82 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
84 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
86 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
88 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
89 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
90 edje_object_message_signal_process(wd->btn);
91 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
96 _disable_hook(Evas_Object *obj)
98 Widget_Data *wd = elm_widget_data_get(obj);
100 if (elm_widget_disabled_get(obj))
101 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
103 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
107 _sizing_eval(Evas_Object *obj)
109 Widget_Data *wd = elm_widget_data_get(obj);
110 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
113 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
114 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
115 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
116 evas_object_size_hint_min_set(obj, minw, minh);
117 evas_object_size_hint_max_set(obj, maxw, maxh);
121 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
123 Widget_Data *wd = elm_widget_data_get(data);
125 if (obj != wd->icon) return;
130 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
132 Widget_Data *wd = elm_widget_data_get(obj);
133 Evas_Object *sub = event_info;
137 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
138 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
139 _changed_size_hints, obj);
141 edje_object_message_signal_process(wd->btn);
147 _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
149 Widget_Data *wd = elm_widget_data_get(data);
153 ecore_timer_del(wd->timer);
156 wd->repeating = EINA_FALSE;
157 evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
158 _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
162 _autorepeat_send(void *data)
164 Widget_Data *wd = elm_widget_data_get(data);
165 if (!wd) return ECORE_CALLBACK_CANCEL;
167 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
171 return ECORE_CALLBACK_CANCEL;
174 return ECORE_CALLBACK_RENEW;
178 _autorepeat_initial_send(void *data)
180 Widget_Data *wd = elm_widget_data_get(data);
181 if (!wd) return ECORE_CALLBACK_CANCEL;
183 if (wd->timer) ecore_timer_del(wd->timer);
184 wd->repeating = EINA_TRUE;
185 _autorepeat_send(data);
186 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
188 return ECORE_CALLBACK_CANCEL;
192 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
194 Widget_Data *wd = elm_widget_data_get(data);
197 if (wd->autorepeat && !wd->repeating)
199 if (wd->ar_threshold <= 0.0)
200 _autorepeat_initial_send(data); /* call immediately */
202 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
207 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
209 Widget_Data *wd = elm_widget_data_get(data);
214 ecore_timer_del(wd->timer);
217 wd->repeating = EINA_FALSE;
218 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
222 * Add a new button to the parent
223 * @param parent The parent object
224 * @return The new object or NULL if it cannot be created
229 elm_button_add(Evas_Object *parent)
235 wd = ELM_NEW(Widget_Data);
236 e = evas_object_evas_get(parent);
237 obj = elm_widget_add(e);
238 ELM_SET_WIDTYPE(widtype, "button");
239 elm_widget_type_set(obj, "button");
240 elm_widget_sub_object_add(parent, obj);
241 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
242 elm_widget_data_set(obj, wd);
243 elm_widget_del_hook_set(obj, _del_hook);
244 elm_widget_theme_hook_set(obj, _theme_hook);
245 elm_widget_disable_hook_set(obj, _disable_hook);
246 elm_widget_can_focus_set(obj, 1 );
248 wd->btn = edje_object_add(e);
249 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
250 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
251 _signal_clicked, obj);
252 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
253 _signal_pressed, obj);
254 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
255 _signal_unpressed, obj);
256 elm_widget_resize_object_set(obj, wd->btn);
258 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
262 // TODO: convert Elementary to subclassing of Evas_Smart_Class
263 // TODO: and save some bytes, making descriptions per-class and not instance!
264 evas_object_smart_callbacks_descriptions_set(obj, _signals);
269 * Set the label used in the button
271 * @param obj The button object
272 * @param label The text will be written on the button
277 elm_button_label_set(Evas_Object *obj, const char *label)
279 ELM_CHECK_WIDTYPE(obj, widtype);
280 Widget_Data *wd = elm_widget_data_get(obj);
282 eina_stringshare_replace(&wd->label, label);
284 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
286 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
287 edje_object_message_signal_process(wd->btn);
288 edje_object_part_text_set(wd->btn, "elm.text", label);
293 elm_button_label_get(const Evas_Object *obj)
295 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
296 Widget_Data *wd = elm_widget_data_get(obj);
297 if (!wd) return NULL;
302 * Set the icon used for the button
304 * Once the icon object is set, a previously set one will be deleted
306 * @param obj The button object
307 * @param icon The image for the button
312 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
314 ELM_CHECK_WIDTYPE(obj, widtype);
315 Widget_Data *wd = elm_widget_data_get(obj);
317 if (wd->icon == icon) return;
318 if (wd->icon) evas_object_del(wd->icon);
322 elm_widget_sub_object_add(obj, icon);
323 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
324 _changed_size_hints, obj);
325 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
326 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
327 edje_object_message_signal_process(wd->btn);
333 * Get the icon used for the button
335 * @param obj The button object
336 * @return The image for the button
341 elm_button_icon_get(const Evas_Object *obj)
343 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
344 Widget_Data *wd = elm_widget_data_get(obj);
345 if (!wd) return NULL;
350 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
352 * @param obj The button object
353 * @param on A bool to turn on/off the event
358 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
360 ELM_CHECK_WIDTYPE(obj, widtype);
361 Widget_Data *wd = elm_widget_data_get(obj);
365 ecore_timer_del(wd->timer);
369 wd->repeating = EINA_FALSE;
373 * Set the initial timeout before the autorepeat event is generated
375 * @param obj The button object
381 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
383 ELM_CHECK_WIDTYPE(obj, widtype);
384 Widget_Data *wd = elm_widget_data_get(obj);
386 if (wd->ar_threshold == t) return;
389 ecore_timer_del(wd->timer);
392 wd->ar_threshold = t;
396 * Set the interval between each generated autorepeat event
398 * @param obj The button object
404 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
406 ELM_CHECK_WIDTYPE(obj, widtype);
407 Widget_Data *wd = elm_widget_data_get(obj);
409 if (wd->ar_interval == t) return;
412 if (wd->repeating && wd->timer) ecore_timer_interval_set(wd->timer, t);