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);
37 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
38 Evas_Callback_Type type, void *event_info);
40 static const char SIG_CLICKED[] = "clicked";
41 static const char SIG_REPEATED[] = "repeated";
42 static const char SIG_UNPRESSED[] = "unpressed";
43 static const Evas_Smart_Cb_Description _signals[] = {
51 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
53 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
54 Evas_Event_Key_Down *ev = event_info;
55 Widget_Data *wd = elm_widget_data_get(obj);
56 if (!wd) return EINA_FALSE;
57 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
58 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
59 if ((strcmp(ev->keyname, "Return")) &&
60 (strcmp(ev->keyname, "KP_Enter")) &&
61 (strcmp(ev->keyname, "space")))
64 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
65 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
70 _del_hook(Evas_Object *obj)
72 Widget_Data *wd = elm_widget_data_get(obj);
74 if (wd->label) eina_stringshare_del(wd->label);
79 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
81 Widget_Data *wd = elm_widget_data_get(obj);
83 if (elm_widget_focus_get(obj))
85 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
86 evas_object_focus_set(wd->btn, EINA_TRUE);
90 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
91 evas_object_focus_set(wd->btn, EINA_FALSE);
96 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
98 Widget_Data *wd = elm_widget_data_get(obj);
100 edje_object_mirrored_set(wd->btn, rtl);
104 _theme_hook(Evas_Object *obj)
106 Widget_Data *wd = elm_widget_data_get(obj);
109 _elm_widget_mirrored_reload(obj);
110 _mirrored_set(obj, elm_widget_mirrored_get(obj));
111 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
113 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
115 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
117 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
119 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
121 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
122 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
123 if (elm_object_disabled_get(obj))
124 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
125 edje_object_message_signal_process(wd->btn);
126 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
127 str = edje_object_data_get(wd->btn, "focus_highlight");
128 if ((str) && (!strcmp(str, "on")))
129 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
131 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
136 _disable_hook(Evas_Object *obj)
138 Widget_Data *wd = elm_widget_data_get(obj);
140 if (elm_widget_disabled_get(obj))
141 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
143 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
147 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
149 Widget_Data *wd = elm_widget_data_get(obj);
151 edje_object_signal_emit(wd->btn, emission, source);
155 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
157 Widget_Data *wd = elm_widget_data_get(obj);
159 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
163 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
165 Widget_Data *wd = elm_widget_data_get(obj);
166 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
171 _sizing_eval(Evas_Object *obj)
173 Widget_Data *wd = elm_widget_data_get(obj);
174 Evas_Coord minw = -1, minh = -1;
177 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
178 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
179 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
180 evas_object_size_hint_min_set(obj, minw, minh);
184 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
186 Widget_Data *wd = elm_widget_data_get(data);
188 if (obj != wd->icon) return;
193 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
195 Widget_Data *wd = elm_widget_data_get(obj);
196 Evas_Object *sub = event_info;
200 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
201 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
202 _changed_size_hints, obj);
204 edje_object_message_signal_process(wd->btn);
210 _activate(Evas_Object *obj)
212 Widget_Data *wd = elm_widget_data_get(obj);
216 ecore_timer_del(wd->timer);
219 wd->repeating = EINA_FALSE;
220 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
224 _activate_hook(Evas_Object *obj)
230 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
236 _autorepeat_send(void *data)
238 Widget_Data *wd = elm_widget_data_get(data);
239 if (!wd) return ECORE_CALLBACK_CANCEL;
241 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
245 return ECORE_CALLBACK_CANCEL;
248 return ECORE_CALLBACK_RENEW;
252 _autorepeat_initial_send(void *data)
254 Widget_Data *wd = elm_widget_data_get(data);
255 if (!wd) return ECORE_CALLBACK_CANCEL;
257 if (wd->timer) ecore_timer_del(wd->timer);
258 wd->repeating = EINA_TRUE;
259 _autorepeat_send(data);
260 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
262 return ECORE_CALLBACK_CANCEL;
266 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
268 Widget_Data *wd = elm_widget_data_get(data);
271 if ((wd->autorepeat) && (!wd->repeating))
273 if (wd->ar_threshold <= 0.0)
274 _autorepeat_initial_send(data); /* call immediately */
276 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
281 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
283 Widget_Data *wd = elm_widget_data_get(data);
288 ecore_timer_del(wd->timer);
291 wd->repeating = EINA_FALSE;
292 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
296 * Add a new button to the parent
297 * @param parent The parent object
298 * @return The new object or NULL if it cannot be created
303 elm_button_add(Evas_Object *parent)
309 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
311 ELM_SET_WIDTYPE(widtype, "button");
312 elm_widget_type_set(obj, "button");
313 elm_widget_sub_object_add(parent, obj);
314 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
315 elm_widget_data_set(obj, wd);
316 elm_widget_del_hook_set(obj, _del_hook);
317 elm_widget_theme_hook_set(obj, _theme_hook);
318 elm_widget_disable_hook_set(obj, _disable_hook);
319 elm_widget_can_focus_set(obj, EINA_TRUE);
320 elm_widget_activate_hook_set(obj, _activate_hook);
321 elm_widget_event_hook_set(obj, _event_hook);
322 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
323 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
324 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
326 wd->btn = edje_object_add(e);
327 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
328 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
329 _signal_clicked, obj);
330 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
331 _signal_pressed, obj);
332 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
333 _signal_unpressed, obj);
334 elm_widget_resize_object_set(obj, wd->btn);
336 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
340 // TODO: convert Elementary to subclassing of Evas_Smart_Class
341 // TODO: and save some bytes, making descriptions per-class and not instance!
342 evas_object_smart_callbacks_descriptions_set(obj, _signals);
347 * Set the label used in the button
349 * @param obj The button object
350 * @param label The text will be written on the button
355 elm_button_label_set(Evas_Object *obj, const char *label)
357 ELM_CHECK_WIDTYPE(obj, widtype);
358 Widget_Data *wd = elm_widget_data_get(obj);
360 eina_stringshare_replace(&wd->label, label);
362 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
364 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
365 edje_object_message_signal_process(wd->btn);
366 edje_object_part_text_set(wd->btn, "elm.text", label);
371 elm_button_label_get(const Evas_Object *obj)
373 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
374 Widget_Data *wd = elm_widget_data_get(obj);
375 if (!wd) return NULL;
380 * Set the icon used for the button
382 * Once the icon object is set, a previously set one will be deleted
383 * If you want to keep that old content object, use the
384 * elm_button_icon_unset() function.
386 * @param obj The button object
387 * @param icon The icon object for the button
392 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
394 ELM_CHECK_WIDTYPE(obj, widtype);
395 Widget_Data *wd = elm_widget_data_get(obj);
397 if (wd->icon == icon) return;
398 if (wd->icon) evas_object_del(wd->icon);
402 elm_widget_sub_object_add(obj, icon);
403 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
404 _changed_size_hints, obj);
405 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
406 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
407 edje_object_message_signal_process(wd->btn);
413 * Get the icon used for the button
415 * Return the icon object which is set for this widget.
417 * @param obj The button object
418 * @return The icon object that is being used
423 elm_button_icon_get(const Evas_Object *obj)
425 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
426 Widget_Data *wd = elm_widget_data_get(obj);
427 if (!wd) return NULL;
432 * Unset the icon used for the button
434 * Unparent and return the icon object which was set for this widget.
436 * @param obj The button object
437 * @return The icon object that was being used
442 elm_button_icon_unset(Evas_Object *obj)
444 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
445 Widget_Data *wd = elm_widget_data_get(obj);
446 if (!wd) return NULL;
447 if (!wd->icon) return NULL;
448 Evas_Object *icon = wd->icon;
449 elm_widget_sub_object_del(obj, wd->icon);
450 edje_object_part_unswallow(wd->btn, wd->icon);
456 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
458 * @param obj The button object
459 * @param on A bool to turn on/off the event
464 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
466 ELM_CHECK_WIDTYPE(obj, widtype);
467 Widget_Data *wd = elm_widget_data_get(obj);
471 ecore_timer_del(wd->timer);
475 wd->repeating = EINA_FALSE;
479 * Get if autorepeat event is on
481 * @param obj The button object
482 * @return If autorepeat is on
487 elm_button_autorepeat_get(const Evas_Object *obj)
489 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
490 Widget_Data *wd = elm_widget_data_get(obj);
491 if (!wd) return EINA_FALSE;
492 return wd->autorepeat;
496 * Set the initial timeout before the autorepeat event is generated
498 * @param obj The button object
504 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
506 ELM_CHECK_WIDTYPE(obj, widtype);
507 Widget_Data *wd = elm_widget_data_get(obj);
509 if (wd->ar_threshold == t) return;
512 ecore_timer_del(wd->timer);
515 wd->ar_threshold = t;
519 * Get the initial timeout before the autorepeat event is generated
521 * @param obj The button object
527 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
529 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
530 Widget_Data *wd = elm_widget_data_get(obj);
532 return wd->ar_threshold;
536 * Set the interval between each generated autorepeat event
538 * @param obj The button object
544 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
546 ELM_CHECK_WIDTYPE(obj, widtype);
547 Widget_Data *wd = elm_widget_data_get(obj);
549 if (wd->ar_interval == t) return;
552 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
556 * Get the interval between each generated autorepeat event
558 * @param obj The button object
564 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
566 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
567 Widget_Data *wd = elm_widget_data_get(obj);
569 return wd->ar_interval;