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.
10 * Signals that you can add callbacks for are:
12 * "clicked" - the user clicked the button
13 * "repeated" - the user pressed the button without releasing it
14 * "unpressed" - when the button is unpressed (released)
17 typedef struct _Widget_Data Widget_Data;
21 Evas_Object *btn, *icon;
30 static const char *widtype = NULL;
31 static void _del_hook(Evas_Object *obj);
32 static void _theme_hook(Evas_Object *obj);
33 static void _disable_hook(Evas_Object *obj);
34 static void _sizing_eval(Evas_Object *obj);
35 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
36 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
37 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
38 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
39 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
40 static void _on_focus_hook(void *data, Evas_Object *obj);
41 static void _activate(Evas_Object *obj);
42 static void _activate_hook(Evas_Object *obj);
43 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
44 Evas_Callback_Type type, void *event_info);
46 static const char SIG_CLICKED[] = "clicked";
47 static const char SIG_REPEATED[] = "repeated";
48 static const char SIG_UNPRESSED[] = "unpressed";
49 static const Evas_Smart_Cb_Description _signals[] = {
57 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
59 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
60 Evas_Event_Key_Down *ev = event_info;
61 Widget_Data *wd = elm_widget_data_get(obj);
62 if (!wd) return EINA_FALSE;
63 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
64 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
65 if ((strcmp(ev->keyname, "Return")) &&
66 (strcmp(ev->keyname, "KP_Enter")) &&
67 (strcmp(ev->keyname, "space")))
70 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
71 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
76 _del_hook(Evas_Object *obj)
78 Widget_Data *wd = elm_widget_data_get(obj);
80 if (wd->label) eina_stringshare_del(wd->label);
85 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
87 Widget_Data *wd = elm_widget_data_get(obj);
89 if (elm_widget_focus_get(obj))
91 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
92 evas_object_focus_set(wd->btn, EINA_TRUE);
96 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
97 evas_object_focus_set(wd->btn, EINA_FALSE);
102 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
104 Widget_Data *wd = elm_widget_data_get(obj);
106 edje_object_mirrored_set(wd->btn, rtl);
110 _theme_hook(Evas_Object *obj)
112 Widget_Data *wd = elm_widget_data_get(obj);
115 _elm_widget_mirrored_reload(obj);
116 _mirrored_set(obj, elm_widget_mirrored_get(obj));
117 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
119 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
121 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
123 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
125 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
127 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
128 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
129 if (elm_object_disabled_get(obj))
130 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
131 edje_object_message_signal_process(wd->btn);
132 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
133 str = edje_object_data_get(wd->btn, "focus_highlight");
134 if ((str) && (!strcmp(str, "on")))
135 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
137 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
142 _disable_hook(Evas_Object *obj)
144 Widget_Data *wd = elm_widget_data_get(obj);
146 if (elm_widget_disabled_get(obj))
147 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
149 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
153 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
155 Widget_Data *wd = elm_widget_data_get(obj);
157 edje_object_signal_emit(wd->btn, emission, source);
161 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
163 Widget_Data *wd = elm_widget_data_get(obj);
165 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
169 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
171 Widget_Data *wd = elm_widget_data_get(obj);
172 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
177 _sizing_eval(Evas_Object *obj)
179 Widget_Data *wd = elm_widget_data_get(obj);
180 Evas_Coord minw = -1, minh = -1;
183 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
184 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
185 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
186 evas_object_size_hint_min_set(obj, minw, minh);
190 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
192 Widget_Data *wd = elm_widget_data_get(data);
194 if (obj != wd->icon) return;
199 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
201 Widget_Data *wd = elm_widget_data_get(obj);
202 Evas_Object *sub = event_info;
206 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
207 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
208 _changed_size_hints, obj);
210 edje_object_message_signal_process(wd->btn);
216 _activate(Evas_Object *obj)
218 Widget_Data *wd = elm_widget_data_get(obj);
222 ecore_timer_del(wd->timer);
225 wd->repeating = EINA_FALSE;
226 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
230 _activate_hook(Evas_Object *obj)
236 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
242 _autorepeat_send(void *data)
244 Widget_Data *wd = elm_widget_data_get(data);
245 if (!wd) return ECORE_CALLBACK_CANCEL;
247 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
251 return ECORE_CALLBACK_CANCEL;
254 return ECORE_CALLBACK_RENEW;
258 _autorepeat_initial_send(void *data)
260 Widget_Data *wd = elm_widget_data_get(data);
261 if (!wd) return ECORE_CALLBACK_CANCEL;
263 if (wd->timer) ecore_timer_del(wd->timer);
264 wd->repeating = EINA_TRUE;
265 _autorepeat_send(data);
266 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
268 return ECORE_CALLBACK_CANCEL;
272 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
274 Widget_Data *wd = elm_widget_data_get(data);
277 if ((wd->autorepeat) && (!wd->repeating))
279 if (wd->ar_threshold <= 0.0)
280 _autorepeat_initial_send(data); /* call immediately */
282 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
287 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
289 Widget_Data *wd = elm_widget_data_get(data);
294 ecore_timer_del(wd->timer);
297 wd->repeating = EINA_FALSE;
298 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
302 * Add a new button to the parent
303 * @param parent The parent object
304 * @return The new object or NULL if it cannot be created
309 elm_button_add(Evas_Object *parent)
315 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
317 ELM_SET_WIDTYPE(widtype, "button");
318 elm_widget_type_set(obj, "button");
319 elm_widget_sub_object_add(parent, obj);
320 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
321 elm_widget_data_set(obj, wd);
322 elm_widget_del_hook_set(obj, _del_hook);
323 elm_widget_theme_hook_set(obj, _theme_hook);
324 elm_widget_disable_hook_set(obj, _disable_hook);
325 elm_widget_can_focus_set(obj, EINA_TRUE);
326 elm_widget_activate_hook_set(obj, _activate_hook);
327 elm_widget_event_hook_set(obj, _event_hook);
328 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
329 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
330 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
332 wd->btn = edje_object_add(e);
333 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
334 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
335 _signal_clicked, obj);
336 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
337 _signal_pressed, obj);
338 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
339 _signal_unpressed, obj);
340 elm_widget_resize_object_set(obj, wd->btn);
342 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
346 // TODO: convert Elementary to subclassing of Evas_Smart_Class
347 // TODO: and save some bytes, making descriptions per-class and not instance!
348 evas_object_smart_callbacks_descriptions_set(obj, _signals);
353 * Set the label used in the button
355 * @param obj The button object
356 * @param label The text will be written on the button
361 elm_button_label_set(Evas_Object *obj, const char *label)
363 ELM_CHECK_WIDTYPE(obj, widtype);
364 Widget_Data *wd = elm_widget_data_get(obj);
366 eina_stringshare_replace(&wd->label, label);
368 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
370 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
371 edje_object_message_signal_process(wd->btn);
372 edje_object_part_text_set(wd->btn, "elm.text", label);
377 elm_button_label_get(const Evas_Object *obj)
379 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
380 Widget_Data *wd = elm_widget_data_get(obj);
381 if (!wd) return NULL;
386 * Set the icon used for the button
388 * Once the icon object is set, a previously set one will be deleted
389 * If you want to keep that old content object, use the
390 * elm_button_icon_unset() function.
392 * @param obj The button object
393 * @param icon The icon object for the button
398 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
400 ELM_CHECK_WIDTYPE(obj, widtype);
401 Widget_Data *wd = elm_widget_data_get(obj);
403 if (wd->icon == icon) return;
404 if (wd->icon) evas_object_del(wd->icon);
408 elm_widget_sub_object_add(obj, icon);
409 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
410 _changed_size_hints, obj);
411 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
412 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
413 edje_object_message_signal_process(wd->btn);
419 * Get the icon used for the button
421 * Return the icon object which is set for this widget.
423 * @param obj The button object
424 * @return The icon object that is being used
429 elm_button_icon_get(const Evas_Object *obj)
431 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
432 Widget_Data *wd = elm_widget_data_get(obj);
433 if (!wd) return NULL;
438 * Unset the icon used for the button
440 * Unparent and return the icon object which was set for this widget.
442 * @param obj The button object
443 * @return The icon object that was being used
448 elm_button_icon_unset(Evas_Object *obj)
450 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
451 Widget_Data *wd = elm_widget_data_get(obj);
452 if (!wd) return NULL;
453 if (!wd->icon) return NULL;
454 Evas_Object *icon = wd->icon;
455 elm_widget_sub_object_del(obj, wd->icon);
456 edje_object_part_unswallow(wd->btn, wd->icon);
462 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
464 * @param obj The button object
465 * @param on A bool to turn on/off the event
470 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
472 ELM_CHECK_WIDTYPE(obj, widtype);
473 Widget_Data *wd = elm_widget_data_get(obj);
477 ecore_timer_del(wd->timer);
481 wd->repeating = EINA_FALSE;
485 * Get if autorepeat event is on
487 * @param obj The button object
488 * @return If autorepeat is on
493 elm_button_autorepeat_get(const Evas_Object *obj)
495 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
496 Widget_Data *wd = elm_widget_data_get(obj);
497 if (!wd) return EINA_FALSE;
498 return wd->autorepeat;
502 * Set the initial timeout before the autorepeat event is generated
504 * @param obj The button object
510 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
512 ELM_CHECK_WIDTYPE(obj, widtype);
513 Widget_Data *wd = elm_widget_data_get(obj);
515 if (wd->ar_threshold == t) return;
518 ecore_timer_del(wd->timer);
521 wd->ar_threshold = t;
525 * Get the initial timeout before the autorepeat event is generated
527 * @param obj The button object
533 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
535 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
536 Widget_Data *wd = elm_widget_data_get(obj);
538 return wd->ar_threshold;
542 * Set the interval between each generated autorepeat event
544 * @param obj The button object
550 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
552 ELM_CHECK_WIDTYPE(obj, widtype);
553 Widget_Data *wd = elm_widget_data_get(obj);
555 if (wd->ar_interval == t) return;
558 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
562 * Get the interval between each generated autorepeat event
564 * @param obj The button object
570 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
572 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
573 Widget_Data *wd = elm_widget_data_get(obj);
575 return wd->ar_interval;