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 * "unpressed" - when the button is unpressed (released)
18 typedef struct _Widget_Data Widget_Data;
30 Evas_Object *btn, *icon;
37 const char *statelabel[4];
41 static const char *widtype = NULL;
42 static void _del_hook(Evas_Object *obj);
43 static void _theme_hook(Evas_Object *obj);
44 static void _disable_hook(Evas_Object *obj);
45 static void _sizing_eval(Evas_Object *obj);
46 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
47 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
48 static void _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
49 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
50 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
51 static void _on_focus_hook(void *data, Evas_Object *obj);
52 static void _activate(Evas_Object *obj);
53 static void _activate_hook(Evas_Object *obj);
54 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src,
55 Evas_Callback_Type type, void *event_info);
57 static void _set_label(Evas_Object *obj, const char *label);
58 static void _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source);
60 static const char SIG_CLICKED[] = "clicked";
61 static const char SIG_REPEATED[] = "repeated";
62 static const char SIG_UNPRESSED[] = "unpressed";
63 static const Evas_Smart_Cb_Description _signals[] = {
71 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
73 if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
74 Evas_Event_Key_Down *ev = event_info;
75 Widget_Data *wd = elm_widget_data_get(obj);
76 if (!wd) return EINA_FALSE;
77 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
78 if (elm_widget_disabled_get(obj)) return EINA_FALSE;
79 if ((strcmp(ev->keyname, "Return")) &&
80 (strcmp(ev->keyname, "KP_Enter")) &&
81 (strcmp(ev->keyname, "space")))
84 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
85 edje_object_signal_emit(wd->btn, "elm,anim,activate", "elm");
90 _del_hook(Evas_Object *obj)
92 Widget_Data *wd = elm_widget_data_get(obj);
94 if (wd->label) eina_stringshare_del(wd->label);
95 if (wd->statelabel[DEFAULT]) eina_stringshare_del(wd->statelabel[DEFAULT]);
96 if (wd->statelabel[HIGHLIGHTED]) eina_stringshare_del(wd->statelabel[HIGHLIGHTED]);
97 if (wd->statelabel[FOCUSED]) eina_stringshare_del(wd->statelabel[FOCUSED]);
98 if (wd->statelabel[DISABLED]) eina_stringshare_del(wd->statelabel[DISABLED]);
103 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
105 Widget_Data *wd = elm_widget_data_get(obj);
107 if (elm_widget_focus_get(obj))
109 if (wd->statelabel[FOCUSED])
111 _set_label(obj, wd->statelabel[FOCUSED]);
113 edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
114 evas_object_focus_set(wd->btn, EINA_TRUE);
118 if (wd->statelabel[DEFAULT])
119 _set_label(obj, wd->statelabel[DEFAULT]);
122 _set_label(obj, wd->label);
124 edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
125 evas_object_focus_set(wd->btn, EINA_FALSE);
130 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
132 Widget_Data *wd = elm_widget_data_get(obj);
134 edje_object_mirrored_set(wd->btn, rtl);
138 _theme_hook(Evas_Object *obj)
140 Widget_Data *wd = elm_widget_data_get(obj);
143 _elm_widget_mirrored_reload(obj);
144 _mirrored_set(obj, elm_widget_mirrored_get(obj));
145 _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
146 if (elm_object_disabled_get(obj))
147 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
149 edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
151 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
153 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
155 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
157 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
158 edje_object_part_text_set(wd->btn, "elm.text", wd->label);
159 edje_object_message_signal_process(wd->btn);
160 edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
161 str = edje_object_data_get(wd->btn, "focus_highlight");
162 if ((str) && (!strcmp(str, "on")))
163 elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
165 elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
170 _disable_hook(Evas_Object *obj)
172 Widget_Data *wd = elm_widget_data_get(obj);
174 if (elm_widget_disabled_get(obj))
176 if (wd->statelabel[DISABLED] )
178 _set_label(obj, wd->statelabel[DISABLED]);
180 edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
184 if (wd->statelabel[DEFAULT])
185 _set_label(obj, wd->statelabel[DEFAULT]);
188 _set_label(obj, wd->label);
190 edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
195 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
197 Widget_Data *wd = elm_widget_data_get(obj);
199 edje_object_signal_emit(wd->btn, emission, source);
203 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
205 Widget_Data *wd = elm_widget_data_get(obj);
207 edje_object_signal_callback_add(wd->btn, emission, source, func_cb, data);
211 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
213 Widget_Data *wd = elm_widget_data_get(obj);
214 edje_object_signal_callback_del_full(wd->btn, emission, source, func_cb,
219 _sizing_eval(Evas_Object *obj)
221 Widget_Data *wd = elm_widget_data_get(obj);
222 Evas_Coord minw = -1, minh = -1;
226 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
227 edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
228 elm_coords_finger_size_adjust(1, &minw, 1, &minh);
229 //Commenting to sync with open source and able to resize based on text change
230 evas_object_size_hint_min_get(obj, &w, &h);
231 //if (w > minw) minw = w;
232 if (h > minh) minh = h;
234 evas_object_size_hint_min_set(obj, minw, minh);
238 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
240 Widget_Data *wd = elm_widget_data_get(data);
242 if (obj != wd->icon) return;
247 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
249 Widget_Data *wd = elm_widget_data_get(obj);
250 Evas_Object *sub = event_info;
254 edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
255 evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
256 _changed_size_hints, obj);
258 edje_object_message_signal_process(wd->btn);
264 _activate(Evas_Object *obj)
266 Widget_Data *wd = elm_widget_data_get(obj);
270 ecore_timer_del(wd->timer);
273 wd->repeating = EINA_FALSE;
274 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
278 _activate_hook(Evas_Object *obj)
284 _signal_clicked(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
290 _autorepeat_send(void *data)
292 Widget_Data *wd = elm_widget_data_get(data);
293 if (!wd) return ECORE_CALLBACK_CANCEL;
295 evas_object_smart_callback_call(data, SIG_REPEATED, NULL);
299 return ECORE_CALLBACK_CANCEL;
302 return ECORE_CALLBACK_RENEW;
306 _autorepeat_initial_send(void *data)
308 Widget_Data *wd = elm_widget_data_get(data);
309 if (!wd) return ECORE_CALLBACK_CANCEL;
311 if (wd->timer) ecore_timer_del(wd->timer);
312 wd->repeating = EINA_TRUE;
313 _autorepeat_send(data);
314 wd->timer = ecore_timer_add(wd->ar_interval, _autorepeat_send, data);
316 return ECORE_CALLBACK_CANCEL;
320 _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
322 Widget_Data *wd = elm_widget_data_get(data);
325 if (wd->statelabel[HIGHLIGHTED])
327 _set_label(data, wd->statelabel[HIGHLIGHTED]);
329 if ((wd->autorepeat) && (!wd->repeating))
331 if (wd->ar_threshold <= 0.0)
332 _autorepeat_initial_send(data); /* call immediately */
334 wd->timer = ecore_timer_add(wd->ar_threshold, _autorepeat_initial_send, data);
339 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
341 Widget_Data *wd = elm_widget_data_get(data);
343 if (wd->statelabel[DEFAULT])
344 _set_label(data, wd->statelabel[DEFAULT]);
347 _set_label(data, wd->label);
352 ecore_timer_del(wd->timer);
355 wd->repeating = EINA_FALSE;
356 evas_object_smart_callback_call(data, SIG_UNPRESSED, NULL);
360 _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source)
362 Widget_Data *wd = elm_widget_data_get(data);
364 if (wd->statelabel[DEFAULT])
365 _set_label(data, wd->statelabel[DEFAULT]);
368 _set_label(data, wd->label);
372 * Add a new button to the parent
373 * @param[in] parent The parent object
374 * @return The new object or NULL if it cannot be created
379 elm_button_add(Evas_Object *parent)
385 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
387 ELM_SET_WIDTYPE(widtype, "button");
388 elm_widget_type_set(obj, "button");
389 elm_widget_sub_object_add(parent, obj);
390 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
391 elm_widget_data_set(obj, wd);
392 elm_widget_del_hook_set(obj, _del_hook);
393 elm_widget_theme_hook_set(obj, _theme_hook);
394 elm_widget_disable_hook_set(obj, _disable_hook);
395 elm_widget_can_focus_set(obj, EINA_TRUE);
396 elm_widget_activate_hook_set(obj, _activate_hook);
397 elm_widget_event_hook_set(obj, _event_hook);
398 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
399 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
400 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
402 wd->btn = edje_object_add(e);
403 _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
404 wd->statetype[DEFAULT] = 0;
405 wd->statetype[HIGHLIGHTED] = 0;
406 wd->statetype[FOCUSED] = 0;
407 wd->statetype[DISABLED] = 0;
408 wd->statelabel[DEFAULT] = 0;
409 wd->statelabel[HIGHLIGHTED] = 0;
410 wd->statelabel[FOCUSED] = 0;
411 wd->statelabel[DISABLED] = 0;
412 edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
413 _signal_clicked, obj);
414 edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
415 _signal_pressed, obj);
416 edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
417 _signal_unpressed, obj);
418 edje_object_signal_callback_add(wd->btn, "elm,action,default,text,set", "",
419 _signal_default_text_set, obj);
420 elm_widget_resize_object_set(obj, wd->btn);
422 evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
426 // TODO: convert Elementary to subclassing of Evas_Smart_Class
427 // TODO: and save some bytes, making descriptions per-class and not instance!
428 evas_object_smart_callbacks_descriptions_set(obj, _signals);
433 * Set the label used in the button
435 * @param[in] obj The button object
436 * @param[in] label The text will be written on the button
441 elm_button_label_set(Evas_Object *obj, const char *label)
443 ELM_CHECK_WIDTYPE(obj, widtype);
444 Widget_Data *wd = elm_widget_data_get(obj);
446 eina_stringshare_replace(&wd->label, label);
448 edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
450 edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
451 edje_object_message_signal_process(wd->btn);
452 edje_object_part_text_set(wd->btn, "elm.text", label);
457 _set_label(Evas_Object *obj, const char *label)
459 Widget_Data *wd = elm_widget_data_get(obj);
461 edje_object_part_text_set(wd->btn, "elm.text", label);
465 * Set the label for each state of button
467 * @param[in] obj The button object
468 * @param[in] label The text will be written on the button
469 * @param[in] state The state of button
474 elm_button_label_set_for_state(Evas_Object *obj, const char *label, UIControlState state)
476 Widget_Data *wd = elm_widget_data_get(obj);
479 if (label == NULL) return;
481 if (state == UIControlStateDefault)
483 wd->statetype[DEFAULT] = UIControlStateDefault;
484 eina_stringshare_replace(&wd->statelabel[DEFAULT], label);
487 if (state == UIControlStateHighlighted)
489 wd->statetype[HIGHLIGHTED] = UIControlStateHighlighted;
490 eina_stringshare_replace(&wd->statelabel[HIGHLIGHTED], label);
493 if (state == UIControlStateFocused)
495 wd->statetype[FOCUSED] = UIControlStateFocused;
496 eina_stringshare_replace(&wd->statelabel[FOCUSED], label);
499 if (state == UIControlStateDisabled)
501 wd->statetype[DISABLED] = UIControlStateDisabled;
502 eina_stringshare_replace(&wd->statelabel[DISABLED], label);
508 * Get the label of button
510 * @param[in] obj The button object
511 * @return The title of button
516 elm_button_label_get(const Evas_Object *obj)
518 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
519 Widget_Data *wd = elm_widget_data_get(obj);
520 if (!wd) return NULL;
524 * Get the label of button for each state
526 * @param[in] obj The button object
527 * @param[in] state The state of button
528 * @return The title of button for state
533 elm_button_label_get_for_state(const Evas_Object *obj, UIControlState state)
535 Widget_Data *wd = elm_widget_data_get(obj);
536 if (!wd) return NULL;
538 if (state == UIControlStateDefault)
539 return wd->statelabel[DEFAULT];
540 else if (state == UIControlStateHighlighted)
541 return wd->statelabel[HIGHLIGHTED];
542 else if (state == UIControlStateFocused)
543 return wd->statelabel[FOCUSED];
544 else if (state == UIControlStateDisabled)
545 return wd->statelabel[DISABLED];
551 * Set the icon used for the button
553 * Once the icon object is set, a previously set one will be deleted
554 * If you want to keep that old content object, use the
555 * elm_button_icon_unset() function.
557 * @param[in] obj The button object
558 * @param[in] icon The icon object for the button
563 elm_button_icon_set(Evas_Object *obj, Evas_Object *icon)
565 ELM_CHECK_WIDTYPE(obj, widtype);
566 Widget_Data *wd = elm_widget_data_get(obj);
568 if (wd->icon == icon) return;
569 if (wd->icon) evas_object_del(wd->icon);
573 elm_widget_sub_object_add(obj, icon);
574 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
575 _changed_size_hints, obj);
576 edje_object_part_swallow(wd->btn, "elm.swallow.content", icon);
577 edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
578 edje_object_message_signal_process(wd->btn);
584 * Get the icon used for the button
586 * Return the icon object which is set for this widget.
588 * @param[in] obj The button object
589 * @return The icon object that is being used
594 elm_button_icon_get(const Evas_Object *obj)
596 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
597 Widget_Data *wd = elm_widget_data_get(obj);
598 if (!wd) return NULL;
603 * Unset the icon used for the button
605 * Unparent and return the icon object which was set for this widget.
607 * @param[in] obj The button object
608 * @return The icon object that was being used
613 elm_button_icon_unset(Evas_Object *obj)
615 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
616 Widget_Data *wd = elm_widget_data_get(obj);
617 if (!wd) return NULL;
618 if (!wd->icon) return NULL;
619 Evas_Object *icon = wd->icon;
620 elm_widget_sub_object_del(obj, wd->icon);
621 edje_object_part_unswallow(wd->btn, wd->icon);
627 * Turn on/off the autorepeat event generated when the user keeps pressing on the button
629 * @param[in] obj The button object
630 * @param[in] on A bool to turn on/off the event
635 elm_button_autorepeat_set(Evas_Object *obj, Eina_Bool on)
637 ELM_CHECK_WIDTYPE(obj, widtype);
638 Widget_Data *wd = elm_widget_data_get(obj);
642 ecore_timer_del(wd->timer);
646 wd->repeating = EINA_FALSE;
650 * Get if autorepeat event is on
652 * @param[in] obj The button object
653 * @return If autorepeat is on
658 elm_button_autorepeat_get(const Evas_Object *obj)
660 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
661 Widget_Data *wd = elm_widget_data_get(obj);
662 if (!wd) return EINA_FALSE;
663 return wd->autorepeat;
667 * Set the initial timeout before the autorepeat event is generated
669 * @param[in] obj The button object
670 * @param[in] t Timeout
675 elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
677 ELM_CHECK_WIDTYPE(obj, widtype);
678 Widget_Data *wd = elm_widget_data_get(obj);
680 if (wd->ar_threshold == t) return;
683 ecore_timer_del(wd->timer);
686 wd->ar_threshold = t;
690 * Get the initial timeout before the autorepeat event is generated
692 * @param[in] obj The button object
698 elm_button_autorepeat_initial_timeout_get(const Evas_Object *obj)
700 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
701 Widget_Data *wd = elm_widget_data_get(obj);
703 return wd->ar_threshold;
707 * Set the interval between each generated autorepeat event
709 * @param[in] obj The button object
710 * @param[in] t Interval
715 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
717 ELM_CHECK_WIDTYPE(obj, widtype);
718 Widget_Data *wd = elm_widget_data_get(obj);
720 if (wd->ar_interval == t) return;
723 if ((wd->repeating) && (wd->timer)) ecore_timer_interval_set(wd->timer, t);
727 * Get the interval between each generated autorepeat event
729 * @param[in] obj The button object
735 elm_button_autorepeat_gap_timeout_get(const Evas_Object *obj)
737 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
738 Widget_Data *wd = elm_widget_data_get(obj);
740 return wd->ar_interval;