1 #include <Elementary.h>
5 * @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.
11 * Signals that you can add callbacks for are:
13 * "clicked" - the user clicked the button
14 * "repeated" - the user pressed the button without releasing it
15 * "pressed" - when the button is pressed
16 * "unpressed" - when the button is unpressed (released)
19 typedef struct _Widget_Data Widget_Data;
31 Evas_Object *btn, *icon;
38 const char *statelabel[4];
42 static const char *widtype = NULL;
43 static void _del_hook(Evas_Object *obj);
44 static void _theme_hook(Evas_Object *obj);
45 static void _disable_hook(Evas_Object *obj);
46 static void _sizing_eval(Evas_Object *obj);
47 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
48 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
49 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
50 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
51 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
52 static void _on_focus_hook(void *data, Evas_Object *obj);
53 static void _activate(Evas_Object *obj);
54 static void _activate_hook(Evas_Object *obj);
55 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
56 Evas_Callback_Type type, void *event_info);
58 static void _set_label(Evas_Object *obj, const char *label);
59 static void _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source);
61 static const char SIG_CLICKED[] = "clicked";
62 static const char SIG_REPEATED[] = "repeated";
63 static const char SIG_PRESSED[] = "pressed";
64 static const char SIG_UNPRESSED[] = "unpressed";
65 static const Evas_Smart_Cb_Description _signals[] = {
74 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
76 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
77 Evas_Event_Key_Down *ev = event_info;
78 Widget_Data *wd = elm_widget_data_get(obj);
79 if (!wd) return EINA_FALSE;
80 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
81 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
82 if ((strcmp(ev->keyname, "Return")) &&
83 (strcmp(ev->keyname, "KP_Enter")) &&
84 (strcmp(ev->keyname, "space")))
87 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
88 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
93 _del_hook(Evas_Object *obj)
95 Widget_Data *wd = elm_widget_data_get(obj);
97 if (wd->label) eina_stringshare_del(wd->label);
98 if (wd->statelabel[DEFAULT]) eina_stringshare_del(wd->statelabel[DEFAULT]);
99 if (wd->statelabel[HIGHLIGHTED]) eina_stringshare_del(wd->statelabel[HIGHLIGHTED]);
100 if (wd->statelabel[FOCUSED]) eina_stringshare_del(wd->statelabel[FOCUSED]);
101 if (wd->statelabel[DISABLED]) eina_stringshare_del(wd->statelabel[DISABLED]);
106 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
108 Widget_Data *wd = elm_widget_data_get(obj);
110 if (elm_widget_focus_get(obj))
112 if (wd->statelabel[FOCUSED])
114 _set_label(obj, wd->statelabel[FOCUSED]);
116 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
117 evas_object_focus_set(wd->btn, EINA_TRUE);
121 if (wd->statelabel[DEFAULT])
122 _set_label(obj, wd->statelabel[DEFAULT]);
125 _set_label(obj, wd->label);
127 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
128 evas_object_focus_set(wd->btn, EINA_FALSE);
133 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
135 Widget_Data *wd = elm_widget_data_get(obj);
137 edje_object_mirrored_set(wd->btn, rtl);
141 _theme_hook(Evas_Object *obj)
143 Widget_Data *wd = elm_widget_data_get(obj);
146 _elm_widget_mirrored_reload(obj);
147 _mirrored_set(obj, elm_widget_mirrored_get(obj));
148 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
149 if (elm_object_disabled_get(obj))
150 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
152 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
154 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
156 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
158 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
160 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
161 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
162 edje_object_message_signal_process(wd->btn);
163 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
164 str = edje_object_data_get(wd->btn, "focus_highlight");
165 if ((str) && (!strcmp(str, "on")))
166 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
168 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
173 _disable_hook(Evas_Object *obj)
175 Widget_Data *wd = elm_widget_data_get(obj);
177 if (elm_widget_disabled_get(obj))
179 if (wd->statelabel[DISABLED] )
181 _set_label(obj, wd->statelabel[DISABLED]);
183 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
187 if (wd->statelabel[DEFAULT])
188 _set_label(obj, wd->statelabel[DEFAULT]);
191 _set_label(obj, wd->label);
193 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
198 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
200 Widget_Data *wd = elm_widget_data_get(obj);
202 edje_object_signal_emit(wd->btn, emission, source);
206 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
208 Widget_Data *wd = elm_widget_data_get(obj);
210 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
214 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
216 Widget_Data *wd = elm_widget_data_get(obj);
217 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
222 _sizing_eval(Evas_Object *obj)
224 Widget_Data *wd = elm_widget_data_get(obj);
225 Evas_Coord minw = -1, minh = -1;
229 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
230 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
231 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
232 //Commenting to sync with open source and able to resize based on text change
233 evas_object_size_hint_min_get(obj, &w, &h);
234 //if (w > minw) minw = w;
235 if (h > minh) minh = h;
237 evas_object_size_hint_min_set(obj, minw, minh);
241 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
243 Widget_Data *wd = elm_widget_data_get(data);
245 if (obj != wd->icon) return;
250 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
252 Widget_Data *wd = elm_widget_data_get(obj);
253 Evas_Object *sub = event_info;
257 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
258 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
259 _changed_size_hints, obj);
261 edje_object_message_signal_process(wd->btn);
267 _activate(Evas_Object *obj)
269 Widget_Data *wd = elm_widget_data_get(obj);
273 ecore_timer_del(wd->timer);
276 wd->repeating = EINA_FALSE;
277 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
281 _activate_hook(Evas_Object *obj)
287 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
293 _autorepeat_send(void *data)
295 Widget_Data *wd = elm_widget_data_get(data);
296 if (!wd) return ECORE_CALLBACK_CANCEL;
298 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
302 return ECORE_CALLBACK_CANCEL;
305 return ECORE_CALLBACK_RENEW;
309 _autorepeat_initial_send(void *data)
311 Widget_Data *wd = elm_widget_data_get(data);
312 if (!wd) return ECORE_CALLBACK_CANCEL;
314 if (wd->timer) ecore_timer_del(wd->timer);
315 wd->repeating = EINA_TRUE;
316 _autorepeat_send(data);
317 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
319 return ECORE_CALLBACK_CANCEL;
323 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
325 Widget_Data *wd = elm_widget_data_get(data);
328 if (wd->statelabel[HIGHLIGHTED])
330 _set_label(data, wd->statelabel[HIGHLIGHTED]);
332 if ((wd->autorepeat) && (!wd->repeating))
334 if (wd->ar_threshold <= 0.0)
335 _autorepeat_initial_send(data); /* call immediately */
337 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
340 evas_object_smart_callback_call(data, SIG_PRESSED, NULL);
344 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
346 Widget_Data *wd = elm_widget_data_get(data);
348 if (wd->statelabel[DEFAULT])
349 _set_label(data, wd->statelabel[DEFAULT]);
352 _set_label(data, wd->label);
357 ecore_timer_del(wd->timer);
360 wd->repeating = EINA_FALSE;
361 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
365 _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source)
367 Widget_Data *wd = elm_widget_data_get(data);
369 if (wd->statelabel[DEFAULT])
370 _set_label(data, wd->statelabel[DEFAULT]);
373 _set_label(data, wd->label);
377 * Add a new button to the parent
378 * @param[in] parent The parent object
379 * @return The new object or NULL if it cannot be created
384 elm_button_add(Evas_Object *parent)
390 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
392 ELM_SET_WIDTYPE(widtype, "button");
393 elm_widget_type_set(obj, "button");
394 elm_widget_sub_object_add(parent, obj);
395 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
396 elm_widget_data_set(obj, wd);
397 elm_widget_del_hook_set(obj, _del_hook);
398 elm_widget_theme_hook_set(obj, _theme_hook);
399 elm_widget_disable_hook_set(obj, _disable_hook);
400 elm_widget_can_focus_set(obj, EINA_TRUE);
401 elm_widget_activate_hook_set(obj, _activate_hook);
402 elm_widget_event_hook_set(obj, _event_hook);
403 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
404 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
405 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
407 wd->btn = edje_object_add(e);
408 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
409 wd->statetype[DEFAULT] = 0;
410 wd->statetype[HIGHLIGHTED] = 0;
411 wd->statetype[FOCUSED] = 0;
412 wd->statetype[DISABLED] = 0;
413 wd->statelabel[DEFAULT] = 0;
414 wd->statelabel[HIGHLIGHTED] = 0;
415 wd->statelabel[FOCUSED] = 0;
416 wd->statelabel[DISABLED] = 0;
417 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
418 _signal_clicked, obj);
419 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
420 _signal_pressed, obj);
421 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
422 _signal_unpressed, obj);
423 edje_object_signal_callback_add(wd->btn, "elm,action,default,text,set", "",
424 _signal_default_text_set, obj);
425 elm_widget_resize_object_set(obj, wd->btn);
427 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
431 // TODO: convert Elementary to subclassing of Evas_Smart_Class
432 // TODO: and save some bytes, making descriptions per-class and not instance!
433 evas_object_smart_callbacks_descriptions_set(obj, _signals);
438 * Set the label used in the button
440 * @param[in] obj The button object
441 * @param[in] label The text will be written on the button
446 elm_button_label_set(Evas_Object *obj, const char *label)
448 ELM_CHECK_WIDTYPE(obj, widtype);
449 Widget_Data *wd = elm_widget_data_get(obj);
451 eina_stringshare_replace(&wd->label, label);
453 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
455 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
456 edje_object_message_signal_process(wd->btn);
457 edje_object_part_text_set(wd->btn, "elm.text", label);
462 _set_label(Evas_Object *obj, const char *label)
464 Widget_Data *wd = elm_widget_data_get(obj);
466 edje_object_part_text_set(wd->btn, "elm.text", label);
470 * Set the label for each state of button
472 * @param[in] obj The button object
473 * @param[in] label The text will be written on the button
474 * @param[in] state The state of button
479 elm_button_label_set_for_state(Evas_Object *obj, const char *label, UIControlState state)
481 Widget_Data *wd = elm_widget_data_get(obj);
484 if (label == NULL) return;
486 if (state == UIControlStateDefault)
488 wd->statetype[DEFAULT] = UIControlStateDefault;
489 eina_stringshare_replace(&wd->statelabel[DEFAULT], label);
492 if (state == UIControlStateHighlighted)
494 wd->statetype[HIGHLIGHTED] = UIControlStateHighlighted;
495 eina_stringshare_replace(&wd->statelabel[HIGHLIGHTED], label);
498 if (state == UIControlStateFocused)
500 wd->statetype[FOCUSED] = UIControlStateFocused;
501 eina_stringshare_replace(&wd->statelabel[FOCUSED], label);
504 if (state == UIControlStateDisabled)
506 wd->statetype[DISABLED] = UIControlStateDisabled;
507 eina_stringshare_replace(&wd->statelabel[DISABLED], label);
513 * Get the label of button
515 * @param[in] obj The button object
516 * @return The title of button
521 elm_button_label_get(const Evas_Object *obj)
523 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
524 Widget_Data *wd = elm_widget_data_get(obj);
525 if (!wd) return NULL;
529 * Get the label of button for each state
531 * @param[in] obj The button object
532 * @param[in] state The state of button
533 * @return The title of button for state
538 elm_button_label_get_for_state(const Evas_Object *obj, UIControlState state)
540 Widget_Data *wd = elm_widget_data_get(obj);
541 if (!wd) return NULL;
543 if (state == UIControlStateDefault)
544 return wd->statelabel[DEFAULT];
545 else if (state == UIControlStateHighlighted)
546 return wd->statelabel[HIGHLIGHTED];
547 else if (state == UIControlStateFocused)
548 return wd->statelabel[FOCUSED];
549 else if (state == UIControlStateDisabled)
550 return wd->statelabel[DISABLED];
556 * Set the icon used for the button
558 * Once the icon object is set, a previously set one will be deleted
559 * If you want to keep that old content object, use the
560 * elm_button_icon_unset() function.
562 * @param[in] obj The button object
563 * @param[in] icon The icon object for the button
568 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
570 ELM_CHECK_WIDTYPE(obj, widtype);
571 Widget_Data *wd = elm_widget_data_get(obj);
573 if (wd->icon == icon) return;
574 if (wd->icon) evas_object_del(wd->icon);
578 elm_widget_sub_object_add(obj, icon);
579 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
580 _changed_size_hints, obj);
581 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
582 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
583 edje_object_message_signal_process(wd->btn);
589 * Get the icon used for the button
591 * Return the icon object which is set for this widget.
593 * @param[in] obj The button object
594 * @return The icon object that is being used
599 elm_button_icon_get(const Evas_Object *obj)
601 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
602 Widget_Data *wd = elm_widget_data_get(obj);
603 if (!wd) return NULL;
608 * Unset the icon used for the button
610 * Unparent and return the icon object which was set for this widget.
612 * @param[in] obj The button object
613 * @return The icon object that was being used
618 elm_button_icon_unset(Evas_Object *obj)
620 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
621 Widget_Data *wd = elm_widget_data_get(obj);
622 if (!wd) return NULL;
623 if (!wd->icon) return NULL;
624 Evas_Object *icon = wd->icon;
625 elm_widget_sub_object_del(obj, wd->icon);
626 edje_object_part_unswallow(wd->btn, wd->icon);
632 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
634 * @param[in] obj The button object
635 * @param[in] on A bool to turn on/off the event
640 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
642 ELM_CHECK_WIDTYPE(obj, widtype);
643 Widget_Data *wd = elm_widget_data_get(obj);
647 ecore_timer_del(wd->timer);
651 wd->repeating = EINA_FALSE;
655 * Get if autorepeat event is on
657 * @param[in] obj The button object
658 * @return If autorepeat is on
663 elm_button_autorepeat_get(const Evas_Object *obj)
665 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
666 Widget_Data *wd = elm_widget_data_get(obj);
667 if (!wd) return EINA_FALSE;
668 return wd->autorepeat;
672 * Set the initial timeout before the autorepeat event is generated
674 * @param[in] obj The button object
675 * @param[in] t Timeout
680 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
682 ELM_CHECK_WIDTYPE(obj, widtype);
683 Widget_Data *wd = elm_widget_data_get(obj);
685 if (wd->ar_threshold == t) return;
688 ecore_timer_del(wd->timer);
691 wd->ar_threshold = t;
695 * Get the initial timeout before the autorepeat event is generated
697 * @param[in] obj The button object
703 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
705 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
706 Widget_Data *wd = elm_widget_data_get(obj);
708 return wd->ar_threshold;
712 * Set the interval between each generated autorepeat event
714 * @param[in] obj The button object
715 * @param[in] t Interval
720 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
722 ELM_CHECK_WIDTYPE(obj, widtype);
723 Widget_Data *wd = elm_widget_data_get(obj);
725 if (wd->ar_interval == t) return;
728 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
732 * Get the interval between each generated autorepeat event
734 * @param[in] obj The button object
740 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
742 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
743 Widget_Data *wd = elm_widget_data_get(obj);
745 return wd->ar_interval;