1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2001. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * @Short_description: A widget that emits a signal when clicked on
30 * The #GtkButton widget is generally used to trigger a callback function that is
31 * called when the button is pressed. The various signals and how to use them
34 * The #GtkButton widget can hold any valid child widget. That is, it can hold
35 * almost any other standard #GtkWidget. The most commonly used child is the
41 #include "gtkbutton.h"
42 #include "gtkbuttonprivate.h"
45 #include "gtkalignment.h"
48 #include "gtkmarshalers.h"
52 #include "gtkiconfactory.h"
53 #include "gtkactivatable.h"
54 #include "gtksizerequest.h"
55 #include "gtktypebuiltins.h"
56 #include "gtkprivate.h"
58 #include "a11y/gtkbuttonaccessible.h"
59 #include "gtkapplicationprivate.h"
60 #include "gtkactionhelper.h"
62 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
63 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
65 /* Time out before giving up on getting a key release when animating
68 #define ACTIVATE_TIMEOUT 250
94 PROP_ALWAYS_SHOW_IMAGE,
96 /* activatable properties */
97 PROP_ACTIVATABLE_RELATED_ACTION,
98 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
102 static void gtk_button_destroy (GtkWidget *widget);
103 static void gtk_button_dispose (GObject *object);
104 static void gtk_button_set_property (GObject *object,
108 static void gtk_button_get_property (GObject *object,
112 static void gtk_button_screen_changed (GtkWidget *widget,
113 GdkScreen *previous_screen);
114 static void gtk_button_realize (GtkWidget * widget);
115 static void gtk_button_unrealize (GtkWidget * widget);
116 static void gtk_button_map (GtkWidget * widget);
117 static void gtk_button_unmap (GtkWidget * widget);
118 static void gtk_button_style_updated (GtkWidget * widget);
119 static void gtk_button_size_allocate (GtkWidget * widget,
120 GtkAllocation * allocation);
121 static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
122 static gint gtk_button_button_press (GtkWidget * widget,
123 GdkEventButton * event);
124 static gint gtk_button_button_release (GtkWidget * widget,
125 GdkEventButton * event);
126 static gboolean gtk_button_touch (GtkWidget *widget,
127 GdkEventTouch *event);
128 static gint gtk_button_grab_broken (GtkWidget * widget,
129 GdkEventGrabBroken * event);
130 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
131 static gint gtk_button_enter_notify (GtkWidget * widget,
132 GdkEventCrossing * event);
133 static gint gtk_button_leave_notify (GtkWidget * widget,
134 GdkEventCrossing * event);
135 static void gtk_real_button_pressed (GtkButton * button);
136 static void gtk_real_button_released (GtkButton * button);
137 static void gtk_real_button_clicked (GtkButton * button);
138 static void gtk_real_button_activate (GtkButton *button);
139 static void gtk_button_update_state (GtkButton *button);
140 static void gtk_button_add (GtkContainer *container,
142 static GType gtk_button_child_type (GtkContainer *container);
143 static void gtk_button_finish_activate (GtkButton *button,
146 static GObject* gtk_button_constructor (GType type,
147 guint n_construct_properties,
148 GObjectConstructParam *construct_params);
149 static void gtk_button_construct_child (GtkButton *button);
150 static void gtk_button_state_changed (GtkWidget *widget,
151 GtkStateType previous_state);
152 static void gtk_button_grab_notify (GtkWidget *widget,
153 gboolean was_grabbed);
155 static void gtk_button_actionable_iface_init (GtkActionableInterface *iface);
156 static void gtk_button_activatable_interface_init(GtkActivatableIface *iface);
157 static void gtk_button_update (GtkActivatable *activatable,
159 const gchar *property_name);
160 static void gtk_button_sync_action_properties (GtkActivatable *activatable,
162 static void gtk_button_set_related_action (GtkButton *button,
164 static void gtk_button_set_use_action_appearance (GtkButton *button,
165 gboolean use_appearance);
167 static void gtk_button_get_preferred_width (GtkWidget *widget,
170 static void gtk_button_get_preferred_height (GtkWidget *widget,
174 static guint button_signals[LAST_SIGNAL] = { 0 };
176 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
177 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE, gtk_button_actionable_iface_init)
178 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
179 gtk_button_activatable_interface_init))
182 gtk_button_class_init (GtkButtonClass *klass)
184 GObjectClass *gobject_class;
185 GtkWidgetClass *widget_class;
186 GtkContainerClass *container_class;
188 gobject_class = G_OBJECT_CLASS (klass);
189 widget_class = (GtkWidgetClass*) klass;
190 container_class = (GtkContainerClass*) klass;
192 gobject_class->constructor = gtk_button_constructor;
193 gobject_class->dispose = gtk_button_dispose;
194 gobject_class->set_property = gtk_button_set_property;
195 gobject_class->get_property = gtk_button_get_property;
197 widget_class->get_preferred_width = gtk_button_get_preferred_width;
198 widget_class->get_preferred_height = gtk_button_get_preferred_height;
199 widget_class->destroy = gtk_button_destroy;
200 widget_class->screen_changed = gtk_button_screen_changed;
201 widget_class->realize = gtk_button_realize;
202 widget_class->unrealize = gtk_button_unrealize;
203 widget_class->map = gtk_button_map;
204 widget_class->unmap = gtk_button_unmap;
205 widget_class->style_updated = gtk_button_style_updated;
206 widget_class->size_allocate = gtk_button_size_allocate;
207 widget_class->draw = gtk_button_draw;
208 widget_class->button_press_event = gtk_button_button_press;
209 widget_class->button_release_event = gtk_button_button_release;
210 widget_class->touch_event = gtk_button_touch;
211 widget_class->grab_broken_event = gtk_button_grab_broken;
212 widget_class->key_release_event = gtk_button_key_release;
213 widget_class->enter_notify_event = gtk_button_enter_notify;
214 widget_class->leave_notify_event = gtk_button_leave_notify;
215 widget_class->state_changed = gtk_button_state_changed;
216 widget_class->grab_notify = gtk_button_grab_notify;
218 container_class->child_type = gtk_button_child_type;
219 container_class->add = gtk_button_add;
220 gtk_container_class_handle_border_width (container_class);
222 klass->pressed = gtk_real_button_pressed;
223 klass->released = gtk_real_button_released;
224 klass->clicked = NULL;
225 klass->enter = gtk_button_update_state;
226 klass->leave = gtk_button_update_state;
227 klass->activate = gtk_real_button_activate;
229 g_object_class_install_property (gobject_class,
231 g_param_spec_string ("label",
233 P_("Text of the label widget inside the button, if the button contains a label widget"),
235 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_boolean ("use-underline",
241 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
243 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
245 g_object_class_install_property (gobject_class,
247 g_param_spec_boolean ("use-stock",
249 P_("If set, the label is used to pick a stock item instead of being displayed"),
251 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
253 g_object_class_install_property (gobject_class,
255 g_param_spec_boolean ("focus-on-click",
256 P_("Focus on click"),
257 P_("Whether the button grabs focus when it is clicked with the mouse"),
259 GTK_PARAM_READWRITE));
261 g_object_class_install_property (gobject_class,
263 g_param_spec_enum ("relief",
265 P_("The border relief style"),
266 GTK_TYPE_RELIEF_STYLE,
268 GTK_PARAM_READWRITE));
273 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
274 * can be used to control its horizontal alignment. 0.0 is left aligned,
275 * 1.0 is right aligned.
279 g_object_class_install_property (gobject_class,
281 g_param_spec_float("xalign",
282 P_("Horizontal alignment for child"),
283 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
287 GTK_PARAM_READWRITE));
292 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
293 * can be used to control its vertical alignment. 0.0 is top aligned,
294 * 1.0 is bottom aligned.
298 g_object_class_install_property (gobject_class,
300 g_param_spec_float("yalign",
301 P_("Vertical alignment for child"),
302 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
306 GTK_PARAM_READWRITE));
311 * The child widget to appear next to the button text.
315 g_object_class_install_property (gobject_class,
317 g_param_spec_object ("image",
319 P_("Child widget to appear next to the button text"),
321 GTK_PARAM_READWRITE));
324 * GtkButton:image-position:
326 * The position of the image relative to the text inside the button.
330 g_object_class_install_property (gobject_class,
332 g_param_spec_enum ("image-position",
333 P_("Image position"),
334 P_("The position of the image relative to the text"),
335 GTK_TYPE_POSITION_TYPE,
337 GTK_PARAM_READWRITE));
340 * GtkButton:always-show-image:
342 * If %TRUE, the button will ignore the #GtkSettings:gtk-button-images
343 * setting and always show the image, if available.
345 * Use this property if the button would be useless or hard to use
350 g_object_class_install_property (gobject_class,
351 PROP_ALWAYS_SHOW_IMAGE,
352 g_param_spec_boolean ("always-show-image",
353 P_("Always show image"),
354 P_("Whether the image will always be shown"),
356 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
358 g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
359 g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
361 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
362 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
365 * GtkButton::pressed:
366 * @button: the object that received the signal
368 * Emitted when the button is pressed.
370 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
372 button_signals[PRESSED] =
373 g_signal_new (I_("pressed"),
374 G_OBJECT_CLASS_TYPE (gobject_class),
376 G_STRUCT_OFFSET (GtkButtonClass, pressed),
378 _gtk_marshal_VOID__VOID,
382 * GtkButton::released:
383 * @button: the object that received the signal
385 * Emitted when the button is released.
387 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
389 button_signals[RELEASED] =
390 g_signal_new (I_("released"),
391 G_OBJECT_CLASS_TYPE (gobject_class),
393 G_STRUCT_OFFSET (GtkButtonClass, released),
395 _gtk_marshal_VOID__VOID,
399 * GtkButton::clicked:
400 * @button: the object that received the signal
402 * Emitted when the button has been activated (pressed and released).
404 button_signals[CLICKED] =
405 g_signal_new (I_("clicked"),
406 G_OBJECT_CLASS_TYPE (gobject_class),
407 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
408 G_STRUCT_OFFSET (GtkButtonClass, clicked),
410 _gtk_marshal_VOID__VOID,
415 * @button: the object that received the signal
417 * Emitted when the pointer enters the button.
419 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
421 button_signals[ENTER] =
422 g_signal_new (I_("enter"),
423 G_OBJECT_CLASS_TYPE (gobject_class),
425 G_STRUCT_OFFSET (GtkButtonClass, enter),
427 _gtk_marshal_VOID__VOID,
432 * @button: the object that received the signal
434 * Emitted when the pointer leaves the button.
436 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
438 button_signals[LEAVE] =
439 g_signal_new (I_("leave"),
440 G_OBJECT_CLASS_TYPE (gobject_class),
442 G_STRUCT_OFFSET (GtkButtonClass, leave),
444 _gtk_marshal_VOID__VOID,
448 * GtkButton::activate:
449 * @widget: the object which received the signal.
451 * The ::activate signal on GtkButton is an action signal and
452 * emitting it causes the button to animate press then release.
453 * Applications should never connect to this signal, but use the
454 * #GtkButton::clicked signal.
456 button_signals[ACTIVATE] =
457 g_signal_new (I_("activate"),
458 G_OBJECT_CLASS_TYPE (gobject_class),
459 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
460 G_STRUCT_OFFSET (GtkButtonClass, activate),
462 _gtk_marshal_VOID__VOID,
464 widget_class->activate_signal = button_signals[ACTIVATE];
467 * GtkButton:default-border:
469 * The "default-border" style property defines the extra space to add
470 * around a button that can become the default widget of its window.
471 * For more information about default widgets, see gtk_widget_grab_default().
474 gtk_widget_class_install_style_property (widget_class,
475 g_param_spec_boxed ("default-border",
476 P_("Default Spacing"),
477 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
479 GTK_PARAM_READABLE));
482 * GtkButton:default-outside-border:
484 * The "default-outside-border" style property defines the extra outside
485 * space to add around a button that can become the default widget of its
486 * window. Extra outside space is always drawn outside the button border.
487 * For more information about default widgets, see gtk_widget_grab_default().
489 gtk_widget_class_install_style_property (widget_class,
490 g_param_spec_boxed ("default-outside-border",
491 P_("Default Outside Spacing"),
492 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
494 GTK_PARAM_READABLE));
495 gtk_widget_class_install_style_property (widget_class,
496 g_param_spec_int ("child-displacement-x",
497 P_("Child X Displacement"),
498 P_("How far in the x direction to move the child when the button is depressed"),
502 GTK_PARAM_READABLE));
503 gtk_widget_class_install_style_property (widget_class,
504 g_param_spec_int ("child-displacement-y",
505 P_("Child Y Displacement"),
506 P_("How far in the y direction to move the child when the button is depressed"),
510 GTK_PARAM_READABLE));
513 * GtkButton:displace-focus:
515 * Whether the child_displacement_x/child_displacement_y properties
516 * should also affect the focus rectangle.
520 gtk_widget_class_install_style_property (widget_class,
521 g_param_spec_boolean ("displace-focus",
522 P_("Displace focus"),
523 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
525 GTK_PARAM_READABLE));
528 * GtkButton:inner-border:
530 * Sets the border between the button edges and child.
534 * Deprecated: 3.4: Use the standard border and padding CSS properties;
535 * the value of this style property is ignored.
537 gtk_widget_class_install_style_property (widget_class,
538 g_param_spec_boxed ("inner-border",
540 P_("Border between button edges and child."),
542 GTK_PARAM_READABLE));
545 * GtkButton::image-spacing:
547 * Spacing in pixels between the image and label.
551 gtk_widget_class_install_style_property (widget_class,
552 g_param_spec_int ("image-spacing",
554 P_("Spacing in pixels between the image and label"),
558 GTK_PARAM_READABLE));
560 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
562 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_BUTTON_ACCESSIBLE);
566 gtk_button_init (GtkButton *button)
568 GtkButtonPrivate *priv;
569 GtkStyleContext *context;
571 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
576 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
577 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
578 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
580 priv->label_text = NULL;
582 priv->constructed = FALSE;
583 priv->in_button = FALSE;
584 priv->button_down = FALSE;
585 priv->relief = GTK_RELIEF_NORMAL;
586 priv->use_stock = FALSE;
587 priv->use_underline = FALSE;
588 priv->depressed = FALSE;
589 priv->depress_on_activate = TRUE;
590 priv->focus_on_click = TRUE;
595 priv->image_is_stock = TRUE;
596 priv->image_position = GTK_POS_LEFT;
597 priv->use_action_appearance = TRUE;
599 context = gtk_widget_get_style_context (GTK_WIDGET (button));
600 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
604 gtk_button_destroy (GtkWidget *widget)
606 GtkButton *button = GTK_BUTTON (widget);
607 GtkButtonPrivate *priv = button->priv;
609 if (priv->label_text)
611 g_free (priv->label_text);
612 priv->label_text = NULL;
615 GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
619 gtk_button_constructor (GType type,
620 guint n_construct_properties,
621 GObjectConstructParam *construct_params)
625 GtkButtonPrivate *priv;
627 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
628 n_construct_properties,
631 button = GTK_BUTTON (object);
634 priv->constructed = TRUE;
636 if (priv->label_text != NULL)
637 gtk_button_construct_child (button);
644 gtk_button_child_type (GtkContainer *container)
646 if (!gtk_bin_get_child (GTK_BIN (container)))
647 return GTK_TYPE_WIDGET;
653 maybe_set_alignment (GtkButton *button,
656 GtkButtonPrivate *priv = button->priv;
658 if (GTK_IS_MISC (widget))
660 GtkMisc *misc = GTK_MISC (widget);
663 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
665 else if (GTK_IS_ALIGNMENT (widget))
667 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
668 gfloat xscale, yscale;
670 g_object_get (alignment,
676 gtk_alignment_set (alignment,
677 priv->xalign, priv->yalign,
683 gtk_button_add (GtkContainer *container,
686 maybe_set_alignment (GTK_BUTTON (container), widget);
688 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
692 gtk_button_dispose (GObject *object)
694 GtkButton *button = GTK_BUTTON (object);
695 GtkButtonPrivate *priv = button->priv;
697 g_clear_object (&priv->action_helper);
701 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
704 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
708 gtk_button_set_action_name (GtkActionable *actionable,
709 const gchar *action_name)
711 GtkButton *button = GTK_BUTTON (actionable);
713 g_return_if_fail (button->priv->action == NULL);
715 if (!button->priv->action_helper)
716 button->priv->action_helper = gtk_action_helper_new (actionable);
718 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
720 g_signal_connect_after (button, "clicked", G_CALLBACK (gtk_real_button_clicked), NULL);
722 gtk_action_helper_set_action_name (button->priv->action_helper, action_name);
726 gtk_button_set_action_target_value (GtkActionable *actionable,
727 GVariant *action_target)
729 GtkButton *button = GTK_BUTTON (actionable);
731 if (!button->priv->action_helper)
732 button->priv->action_helper = gtk_action_helper_new (actionable);
734 gtk_action_helper_set_action_target_value (button->priv->action_helper, action_target);
738 gtk_button_set_property (GObject *object,
743 GtkButton *button = GTK_BUTTON (object);
744 GtkButtonPrivate *priv = button->priv;
749 gtk_button_set_label (button, g_value_get_string (value));
752 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
754 case PROP_ALWAYS_SHOW_IMAGE:
755 gtk_button_set_always_show_image (button, g_value_get_boolean (value));
758 gtk_button_set_relief (button, g_value_get_enum (value));
760 case PROP_USE_UNDERLINE:
761 gtk_button_set_use_underline (button, g_value_get_boolean (value));
764 gtk_button_set_use_stock (button, g_value_get_boolean (value));
766 case PROP_FOCUS_ON_CLICK:
767 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
770 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
773 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
775 case PROP_IMAGE_POSITION:
776 gtk_button_set_image_position (button, g_value_get_enum (value));
778 case PROP_ACTIVATABLE_RELATED_ACTION:
779 gtk_button_set_related_action (button, g_value_get_object (value));
781 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
782 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
784 case PROP_ACTION_NAME:
785 gtk_button_set_action_name (GTK_ACTIONABLE (button), g_value_get_string (value));
787 case PROP_ACTION_TARGET:
788 gtk_button_set_action_target_value (GTK_ACTIONABLE (button), g_value_get_variant (value));
791 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
797 gtk_button_get_property (GObject *object,
802 GtkButton *button = GTK_BUTTON (object);
803 GtkButtonPrivate *priv = button->priv;
808 g_value_set_string (value, priv->label_text);
811 g_value_set_object (value, (GObject *)priv->image);
813 case PROP_ALWAYS_SHOW_IMAGE:
814 g_value_set_boolean (value, gtk_button_get_always_show_image (button));
817 g_value_set_enum (value, priv->relief);
819 case PROP_USE_UNDERLINE:
820 g_value_set_boolean (value, priv->use_underline);
823 g_value_set_boolean (value, priv->use_stock);
825 case PROP_FOCUS_ON_CLICK:
826 g_value_set_boolean (value, priv->focus_on_click);
829 g_value_set_float (value, priv->xalign);
832 g_value_set_float (value, priv->yalign);
834 case PROP_IMAGE_POSITION:
835 g_value_set_enum (value, priv->image_position);
837 case PROP_ACTIVATABLE_RELATED_ACTION:
838 g_value_set_object (value, priv->action);
840 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
841 g_value_set_boolean (value, priv->use_action_appearance);
843 case PROP_ACTION_NAME:
844 g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
846 case PROP_ACTION_TARGET:
847 g_value_set_variant (value, gtk_action_helper_get_action_target_value (priv->action_helper));
850 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
856 gtk_button_get_action_name (GtkActionable *actionable)
858 GtkButton *button = GTK_BUTTON (actionable);
860 return gtk_action_helper_get_action_name (button->priv->action_helper);
864 gtk_button_get_action_target_value (GtkActionable *actionable)
866 GtkButton *button = GTK_BUTTON (actionable);
868 return gtk_action_helper_get_action_target_value (button->priv->action_helper);
872 gtk_button_actionable_iface_init (GtkActionableInterface *iface)
874 iface->get_action_name = gtk_button_get_action_name;
875 iface->set_action_name = gtk_button_set_action_name;
876 iface->get_action_target_value = gtk_button_get_action_target_value;
877 iface->set_action_target_value = gtk_button_set_action_target_value;
881 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
883 iface->update = gtk_button_update;
884 iface->sync_action_properties = gtk_button_sync_action_properties;
888 activatable_update_stock_id (GtkButton *button,
891 if (!gtk_button_get_use_stock (button))
894 gtk_button_set_label (button, gtk_action_get_stock_id (action));
898 activatable_update_short_label (GtkButton *button,
904 if (gtk_button_get_use_stock (button))
907 image = gtk_button_get_image (button);
909 /* Dont touch custom child... */
910 child = gtk_bin_get_child (GTK_BIN (button));
911 if (GTK_IS_IMAGE (image) ||
913 GTK_IS_LABEL (child))
915 gtk_button_set_label (button, gtk_action_get_short_label (action));
916 gtk_button_set_use_underline (button, TRUE);
921 activatable_update_icon_name (GtkButton *button,
926 if (gtk_button_get_use_stock (button))
929 image = gtk_button_get_image (button);
931 if (GTK_IS_IMAGE (image) &&
932 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
933 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
934 gtk_image_set_from_icon_name (GTK_IMAGE (image),
935 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
939 activatable_update_gicon (GtkButton *button,
942 GtkWidget *image = gtk_button_get_image (button);
943 GIcon *icon = gtk_action_get_gicon (action);
945 if (GTK_IS_IMAGE (image) &&
946 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
947 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
948 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
952 gtk_button_update (GtkActivatable *activatable,
954 const gchar *property_name)
956 GtkButton *button = GTK_BUTTON (activatable);
957 GtkButtonPrivate *priv = button->priv;
959 if (strcmp (property_name, "visible") == 0)
961 if (gtk_action_is_visible (action))
962 gtk_widget_show (GTK_WIDGET (activatable));
964 gtk_widget_hide (GTK_WIDGET (activatable));
966 else if (strcmp (property_name, "sensitive") == 0)
967 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
969 if (!priv->use_action_appearance)
972 if (strcmp (property_name, "stock-id") == 0)
973 activatable_update_stock_id (GTK_BUTTON (activatable), action);
974 else if (strcmp (property_name, "gicon") == 0)
975 activatable_update_gicon (GTK_BUTTON (activatable), action);
976 else if (strcmp (property_name, "short-label") == 0)
977 activatable_update_short_label (GTK_BUTTON (activatable), action);
978 else if (strcmp (property_name, "icon-name") == 0)
979 activatable_update_icon_name (GTK_BUTTON (activatable), action);
983 gtk_button_sync_action_properties (GtkActivatable *activatable,
986 GtkButton *button = GTK_BUTTON (activatable);
987 GtkButtonPrivate *priv = button->priv;
992 if (gtk_action_is_visible (action))
993 gtk_widget_show (GTK_WIDGET (activatable));
995 gtk_widget_hide (GTK_WIDGET (activatable));
997 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
999 if (priv->use_action_appearance)
1001 activatable_update_stock_id (GTK_BUTTON (activatable), action);
1002 activatable_update_short_label (GTK_BUTTON (activatable), action);
1003 activatable_update_gicon (GTK_BUTTON (activatable), action);
1004 activatable_update_icon_name (GTK_BUTTON (activatable), action);
1007 gtk_button_set_always_show_image (button,
1008 gtk_action_get_always_show_image (action));
1012 gtk_button_set_related_action (GtkButton *button,
1015 GtkButtonPrivate *priv = button->priv;
1017 g_return_if_fail (gtk_action_helper_get_action_name (button->priv->action_helper) == NULL);
1019 if (priv->action == action)
1022 /* This should be a default handler, but for compatibility reasons
1023 * we need to support derived classes that don't chain up their
1026 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
1028 g_signal_connect_after (button, "clicked",
1029 G_CALLBACK (gtk_real_button_clicked), NULL);
1031 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
1033 priv->action = action;
1037 gtk_button_set_use_action_appearance (GtkButton *button,
1038 gboolean use_appearance)
1040 GtkButtonPrivate *priv = button->priv;
1042 if (priv->use_action_appearance != use_appearance)
1044 priv->use_action_appearance = use_appearance;
1046 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
1053 * Creates a new #GtkButton widget. To add a child widget to the button,
1054 * use gtk_container_add().
1056 * Returns: The newly created #GtkButton widget.
1059 gtk_button_new (void)
1061 return g_object_new (GTK_TYPE_BUTTON, NULL);
1065 show_image (GtkButton *button)
1067 GtkButtonPrivate *priv = button->priv;
1070 if (priv->label_text && !priv->always_show_image)
1072 GtkSettings *settings;
1074 settings = gtk_widget_get_settings (GTK_WIDGET (button));
1075 g_object_get (settings, "gtk-button-images", &show, NULL);
1085 gtk_button_construct_child (GtkButton *button)
1087 GtkButtonPrivate *priv = button->priv;
1088 GtkStyleContext *context;
1094 GtkWidget *image = NULL;
1095 gchar *label_text = NULL;
1098 if (!priv->constructed)
1101 if (!priv->label_text && !priv->image)
1104 context = gtk_widget_get_style_context (GTK_WIDGET (button));
1106 gtk_style_context_get_style (context,
1107 "image-spacing", &image_spacing,
1110 if (priv->image && !priv->image_is_stock)
1114 image = g_object_ref (priv->image);
1116 parent = gtk_widget_get_parent (image);
1118 gtk_container_remove (GTK_CONTAINER (parent), image);
1123 child = gtk_bin_get_child (GTK_BIN (button));
1125 gtk_container_remove (GTK_CONTAINER (button), child);
1127 if (priv->use_stock &&
1129 gtk_stock_lookup (priv->label_text, &item))
1132 image = g_object_ref (gtk_image_new_from_stock (priv->label_text, GTK_ICON_SIZE_BUTTON));
1134 label_text = item.label;
1137 label_text = priv->label_text;
1141 priv->image = image;
1142 g_object_set (priv->image,
1143 "visible", show_image (button),
1144 "no-show-all", TRUE,
1147 if (priv->image_position == GTK_POS_LEFT ||
1148 priv->image_position == GTK_POS_RIGHT)
1149 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1151 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1153 if (priv->align_set)
1154 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1156 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1158 if (priv->image_position == GTK_POS_LEFT ||
1159 priv->image_position == GTK_POS_TOP)
1160 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1162 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1166 if (priv->use_underline || priv->use_stock)
1168 label = gtk_label_new_with_mnemonic (label_text);
1169 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1170 GTK_WIDGET (button));
1173 label = gtk_label_new (label_text);
1175 if (priv->image_position == GTK_POS_RIGHT ||
1176 priv->image_position == GTK_POS_BOTTOM)
1177 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1179 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1182 gtk_container_add (GTK_CONTAINER (button), align);
1183 gtk_container_add (GTK_CONTAINER (align), box);
1184 gtk_widget_show_all (align);
1186 g_object_unref (image);
1191 if (priv->use_underline || priv->use_stock)
1193 label = gtk_label_new_with_mnemonic (priv->label_text);
1194 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1197 label = gtk_label_new (priv->label_text);
1199 if (priv->align_set)
1200 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1202 gtk_widget_show (label);
1203 gtk_container_add (GTK_CONTAINER (button), label);
1208 * gtk_button_new_with_label:
1209 * @label: The text you want the #GtkLabel to hold.
1211 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1214 * Returns: The newly created #GtkButton widget.
1217 gtk_button_new_with_label (const gchar *label)
1219 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1223 * gtk_button_new_from_stock:
1224 * @stock_id: the name of the stock item
1226 * Creates a new #GtkButton containing the image and text from a stock item.
1227 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1230 * If @stock_id is unknown, then it will be treated as a mnemonic
1231 * label (as for gtk_button_new_with_mnemonic()).
1233 * Returns: a new #GtkButton
1236 gtk_button_new_from_stock (const gchar *stock_id)
1238 return g_object_new (GTK_TYPE_BUTTON,
1241 "use-underline", TRUE,
1246 * gtk_button_new_with_mnemonic:
1247 * @label: The text of the button, with an underscore in front of the
1248 * mnemonic character
1250 * Creates a new #GtkButton containing a label.
1251 * If characters in @label are preceded by an underscore, they are underlined.
1252 * If you need a literal underscore character in a label, use '__' (two
1253 * underscores). The first underlined character represents a keyboard
1254 * accelerator called a mnemonic.
1255 * Pressing Alt and that key activates the button.
1257 * Returns: a new #GtkButton
1260 gtk_button_new_with_mnemonic (const gchar *label)
1262 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1266 * gtk_button_pressed:
1267 * @button: The #GtkButton you want to send the signal to.
1269 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1271 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1274 gtk_button_pressed (GtkButton *button)
1276 g_return_if_fail (GTK_IS_BUTTON (button));
1278 g_signal_emit (button, button_signals[PRESSED], 0);
1282 * gtk_button_released:
1283 * @button: The #GtkButton you want to send the signal to.
1285 * Emits a #GtkButton::released signal to the given #GtkButton.
1287 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1290 gtk_button_released (GtkButton *button)
1292 g_return_if_fail (GTK_IS_BUTTON (button));
1294 g_signal_emit (button, button_signals[RELEASED], 0);
1298 * gtk_button_clicked:
1299 * @button: The #GtkButton you want to send the signal to.
1301 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1304 gtk_button_clicked (GtkButton *button)
1306 g_return_if_fail (GTK_IS_BUTTON (button));
1308 g_signal_emit (button, button_signals[CLICKED], 0);
1313 * @button: The #GtkButton you want to send the signal to.
1315 * Emits a #GtkButton::enter signal to the given #GtkButton.
1317 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1320 gtk_button_enter (GtkButton *button)
1322 g_return_if_fail (GTK_IS_BUTTON (button));
1324 g_signal_emit (button, button_signals[ENTER], 0);
1329 * @button: The #GtkButton you want to send the signal to.
1331 * Emits a #GtkButton::leave signal to the given #GtkButton.
1333 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1336 gtk_button_leave (GtkButton *button)
1338 g_return_if_fail (GTK_IS_BUTTON (button));
1340 g_signal_emit (button, button_signals[LEAVE], 0);
1344 * gtk_button_set_relief:
1345 * @button: The #GtkButton you want to set relief styles of.
1346 * @newstyle: The GtkReliefStyle as described above.
1348 * Sets the relief style of the edges of the given #GtkButton widget.
1349 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1350 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1352 * <!-- FIXME: put pictures of each style -->
1355 gtk_button_set_relief (GtkButton *button,
1356 GtkReliefStyle newrelief)
1358 GtkButtonPrivate *priv;
1360 g_return_if_fail (GTK_IS_BUTTON (button));
1362 priv = button->priv;
1364 if (newrelief != priv->relief)
1366 priv->relief = newrelief;
1367 g_object_notify (G_OBJECT (button), "relief");
1368 gtk_widget_queue_draw (GTK_WIDGET (button));
1373 * gtk_button_get_relief:
1374 * @button: The #GtkButton you want the #GtkReliefStyle from.
1376 * Returns the current relief style of the given #GtkButton.
1378 * Returns: The current #GtkReliefStyle
1381 gtk_button_get_relief (GtkButton *button)
1383 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1385 return button->priv->relief;
1389 gtk_button_realize (GtkWidget *widget)
1391 GtkButton *button = GTK_BUTTON (widget);
1392 GtkButtonPrivate *priv = button->priv;
1393 GtkAllocation allocation;
1395 GdkWindowAttr attributes;
1396 gint attributes_mask;
1398 gtk_widget_get_allocation (widget, &allocation);
1400 gtk_widget_set_realized (widget, TRUE);
1402 attributes.window_type = GDK_WINDOW_CHILD;
1403 attributes.x = allocation.x;
1404 attributes.y = allocation.y;
1405 attributes.width = allocation.width;
1406 attributes.height = allocation.height;
1407 attributes.wclass = GDK_INPUT_ONLY;
1408 attributes.event_mask = gtk_widget_get_events (widget);
1409 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1410 GDK_BUTTON_RELEASE_MASK |
1412 GDK_ENTER_NOTIFY_MASK |
1413 GDK_LEAVE_NOTIFY_MASK);
1415 attributes_mask = GDK_WA_X | GDK_WA_Y;
1417 window = gtk_widget_get_parent_window (widget);
1418 gtk_widget_set_window (widget, window);
1419 g_object_ref (window);
1421 priv->event_window = gdk_window_new (window,
1422 &attributes, attributes_mask);
1423 gtk_widget_register_window (widget, priv->event_window);
1427 gtk_button_unrealize (GtkWidget *widget)
1429 GtkButton *button = GTK_BUTTON (widget);
1430 GtkButtonPrivate *priv = button->priv;
1432 if (priv->activate_timeout)
1433 gtk_button_finish_activate (button, FALSE);
1435 if (priv->event_window)
1437 gtk_widget_unregister_window (widget, priv->event_window);
1438 gdk_window_destroy (priv->event_window);
1439 priv->event_window = NULL;
1442 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1446 gtk_button_map (GtkWidget *widget)
1448 GtkButton *button = GTK_BUTTON (widget);
1449 GtkButtonPrivate *priv = button->priv;
1451 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1453 if (priv->event_window)
1454 gdk_window_show (priv->event_window);
1458 gtk_button_unmap (GtkWidget *widget)
1460 GtkButton *button = GTK_BUTTON (widget);
1461 GtkButtonPrivate *priv = button->priv;
1463 if (priv->event_window)
1465 gdk_window_hide (priv->event_window);
1466 priv->in_button = FALSE;
1469 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1473 gtk_button_update_image_spacing (GtkButton *button,
1474 GtkStyleContext *context)
1476 GtkButtonPrivate *priv = button->priv;
1480 /* Keep in sync with gtk_button_construct_child,
1481 * we only want to update the spacing if the box
1482 * was constructed there.
1484 if (!priv->constructed || !priv->image)
1487 child = gtk_bin_get_child (GTK_BIN (button));
1488 if (GTK_IS_ALIGNMENT (child))
1490 child = gtk_bin_get_child (GTK_BIN (child));
1491 if (GTK_IS_BOX (child))
1493 gtk_style_context_get_style (context,
1494 "image-spacing", &spacing,
1497 gtk_box_set_spacing (GTK_BOX (child), spacing);
1503 gtk_button_style_updated (GtkWidget *widget)
1505 GtkStyleContext *context;
1507 GTK_WIDGET_CLASS (gtk_button_parent_class)->style_updated (widget);
1509 context = gtk_widget_get_style_context (widget);
1511 gtk_button_update_image_spacing (GTK_BUTTON (widget), context);
1515 gtk_button_get_props (GtkButton *button,
1516 GtkBorder *default_border,
1517 GtkBorder *default_outside_border,
1520 gboolean *interior_focus)
1522 GtkStyleContext *context;
1523 GtkStateFlags state;
1524 GtkBorder *tmp_border;
1526 context = gtk_widget_get_style_context (GTK_WIDGET (button));
1527 state = gtk_style_context_get_state (context);
1531 gtk_style_context_get_style (context,
1532 "default-border", &tmp_border,
1537 *default_border = *tmp_border;
1538 gtk_border_free (tmp_border);
1541 *default_border = default_default_border;
1544 if (default_outside_border)
1546 gtk_style_context_get_style (context,
1547 "default-outside-border", &tmp_border,
1552 *default_outside_border = *tmp_border;
1553 gtk_border_free (tmp_border);
1556 *default_outside_border = default_default_outside_border;
1561 gtk_style_context_get_style (context,
1562 "interior-focus", interior_focus,
1567 gtk_style_context_get_padding (context, state, padding);
1570 gtk_style_context_get_border (context, state, border);
1574 gtk_button_size_allocate (GtkWidget *widget,
1575 GtkAllocation *allocation)
1577 GtkButton *button = GTK_BUTTON (widget);
1578 GtkButtonPrivate *priv = button->priv;
1579 GtkAllocation child_allocation;
1580 GtkStyleContext *context;
1582 GtkBorder default_border;
1588 context = gtk_widget_get_style_context (widget);
1590 gtk_button_get_props (button, &default_border, NULL,
1591 &padding, &border, NULL);
1592 gtk_style_context_get_style (context,
1593 "focus-line-width", &focus_width,
1594 "focus-padding", &focus_pad,
1597 gtk_widget_set_allocation (widget, allocation);
1599 if (gtk_widget_get_realized (widget))
1600 gdk_window_move_resize (priv->event_window,
1604 allocation->height);
1606 child = gtk_bin_get_child (GTK_BIN (button));
1607 if (child && gtk_widget_get_visible (child))
1609 child_allocation.x = allocation->x + padding.left + border.left;
1610 child_allocation.y = allocation->y + padding.top + border.top;
1612 child_allocation.width =
1614 (padding.left + padding.right) -
1615 (border.left + border.right);
1617 child_allocation.height =
1618 allocation->height -
1619 (padding.top + padding.bottom) -
1620 (border.top + border.bottom);
1622 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1624 child_allocation.x += default_border.left;
1625 child_allocation.y += default_border.top;
1626 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1627 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1630 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1632 child_allocation.x += focus_width + focus_pad;
1633 child_allocation.y += focus_width + focus_pad;
1634 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1635 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1638 if (priv->depressed)
1640 gint child_displacement_x;
1641 gint child_displacement_y;
1643 gtk_style_context_get_style (context,
1644 "child-displacement-x", &child_displacement_x,
1645 "child-displacement-y", &child_displacement_y,
1647 child_allocation.x += child_displacement_x;
1648 child_allocation.y += child_displacement_y;
1651 child_allocation.width = MAX (1, child_allocation.width);
1652 child_allocation.height = MAX (1, child_allocation.height);
1654 gtk_widget_size_allocate (child, &child_allocation);
1659 gtk_button_draw (GtkWidget *widget,
1662 GtkButton *button = GTK_BUTTON (widget);
1663 GtkButtonPrivate *priv = button->priv;
1665 GtkBorder default_border;
1666 GtkBorder default_outside_border;
1667 gboolean interior_focus;
1670 GtkAllocation allocation;
1671 GtkStyleContext *context;
1672 GtkStateFlags state;
1673 gboolean draw_focus;
1676 context = gtk_widget_get_style_context (widget);
1677 state = gtk_style_context_get_state (context);
1679 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, NULL, &interior_focus);
1680 gtk_style_context_get_style (context,
1681 "focus-line-width", &focus_width,
1682 "focus-padding", &focus_pad,
1685 gtk_widget_get_allocation (widget, &allocation);
1689 width = allocation.width;
1690 height = allocation.height;
1692 if (gtk_widget_has_default (widget) &&
1693 priv->relief == GTK_RELIEF_NORMAL)
1695 x += default_border.left;
1696 y += default_border.top;
1697 width -= default_border.left + default_border.right;
1698 height -= default_border.top + default_border.bottom;
1700 else if (gtk_widget_get_can_default (widget))
1702 x += default_outside_border.left;
1703 y += default_outside_border.top;
1704 width -= default_outside_border.left + default_outside_border.right;
1705 height -= default_outside_border.top + default_outside_border.bottom;
1708 draw_focus = gtk_widget_has_visible_focus (widget);
1711 if (!interior_focus && draw_focus)
1713 x += focus_width + focus_pad;
1714 y += focus_width + focus_pad;
1715 width -= 2 * (focus_width + focus_pad);
1716 height -= 2 * (focus_width + focus_pad);
1719 if (priv->relief != GTK_RELIEF_NONE || priv->depressed ||
1720 state & GTK_STATE_FLAG_PRELIGHT)
1722 gtk_render_background (context, cr,
1723 x, y, width, height);
1724 gtk_render_frame (context, cr,
1725 x, y, width, height);
1730 gint child_displacement_x;
1731 gint child_displacement_y;
1732 gboolean displace_focus;
1735 gtk_style_context_get_style (context,
1736 "child-displacement-y", &child_displacement_y,
1737 "child-displacement-x", &child_displacement_x,
1738 "displace-focus", &displace_focus,
1740 gtk_style_context_get_border (context, state, &border);
1744 x += border.left + focus_pad;
1745 y += border.top + focus_pad;
1746 width -= (2 * focus_pad) + border.left + border.right;
1747 height -= (2 * focus_pad) + border.top + border.bottom;
1751 x -= focus_width + focus_pad;
1752 y -= focus_width + focus_pad;
1753 width += 2 * (focus_width + focus_pad);
1754 height += 2 * (focus_width + focus_pad);
1757 if (priv->depressed && displace_focus)
1759 x += child_displacement_x;
1760 y += child_displacement_y;
1763 gtk_render_focus (context, cr, x, y, width, height);
1766 GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1772 gtk_button_button_press (GtkWidget *widget,
1773 GdkEventButton *event)
1776 GtkButtonPrivate *priv;
1778 if (event->type == GDK_BUTTON_PRESS)
1780 button = GTK_BUTTON (widget);
1781 priv = button->priv;
1783 if (priv->focus_on_click && !gtk_widget_has_focus (widget))
1784 gtk_widget_grab_focus (widget);
1786 if (event->button == GDK_BUTTON_PRIMARY)
1787 g_signal_emit (button, button_signals[PRESSED], 0);
1794 gtk_button_button_release (GtkWidget *widget,
1795 GdkEventButton *event)
1799 if (event->button == GDK_BUTTON_PRIMARY)
1801 button = GTK_BUTTON (widget);
1802 g_signal_emit (button, button_signals[RELEASED], 0);
1809 gtk_button_touch (GtkWidget *widget,
1810 GdkEventTouch *event)
1812 GtkButton *button = GTK_BUTTON (widget);
1813 GtkButtonPrivate *priv = button->priv;
1815 if (event->type == GDK_TOUCH_BEGIN)
1817 if (priv->focus_on_click && !gtk_widget_has_focus (widget))
1818 gtk_widget_grab_focus (widget);
1820 g_signal_emit (button, button_signals[PRESSED], 0);
1822 else if (event->type == GDK_TOUCH_END)
1824 g_signal_emit (button, button_signals[RELEASED], 0);
1831 gtk_button_grab_broken (GtkWidget *widget,
1832 GdkEventGrabBroken *event)
1834 GtkButton *button = GTK_BUTTON (widget);
1835 GtkButtonPrivate *priv = button->priv;
1838 /* Simulate a button release without the pointer in the button */
1839 if (priv->button_down)
1841 save_in = priv->in_button;
1842 priv->in_button = FALSE;
1843 g_signal_emit (button, button_signals[RELEASED], 0);
1844 if (save_in != priv->in_button)
1846 priv->in_button = save_in;
1847 gtk_button_update_state (button);
1855 gtk_button_key_release (GtkWidget *widget,
1858 GtkButton *button = GTK_BUTTON (widget);
1859 GtkButtonPrivate *priv = button->priv;
1861 if (priv->activate_timeout)
1863 gtk_button_finish_activate (button, TRUE);
1866 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1867 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1873 gtk_button_enter_notify (GtkWidget *widget,
1874 GdkEventCrossing *event)
1876 GtkButton *button = GTK_BUTTON (widget);
1877 GtkButtonPrivate *priv = button->priv;
1879 if ((event->window == button->priv->event_window) &&
1880 (event->detail != GDK_NOTIFY_INFERIOR))
1882 priv->in_button = TRUE;
1883 g_signal_emit (button, button_signals[ENTER], 0);
1890 gtk_button_leave_notify (GtkWidget *widget,
1891 GdkEventCrossing *event)
1893 GtkButton *button = GTK_BUTTON (widget);
1894 GtkButtonPrivate *priv = button->priv;
1896 if ((event->window == button->priv->event_window) &&
1897 (event->detail != GDK_NOTIFY_INFERIOR) &&
1898 (gtk_widget_get_sensitive (widget)))
1900 priv->in_button = FALSE;
1901 g_signal_emit (button, button_signals[LEAVE], 0);
1908 gtk_real_button_pressed (GtkButton *button)
1910 GtkButtonPrivate *priv = button->priv;
1912 if (priv->activate_timeout)
1915 priv->button_down = TRUE;
1916 gtk_button_update_state (button);
1920 touch_release_in_button (GtkButton *button)
1922 GtkButtonPrivate *priv;
1927 priv = button->priv;
1928 event = gtk_get_current_event ();
1933 if (event->type != GDK_TOUCH_END ||
1934 event->touch.window != priv->event_window)
1936 gdk_event_free (event);
1940 gdk_event_get_coords (event, &x, &y);
1941 width = gdk_window_get_width (priv->event_window);
1942 height = gdk_window_get_height (priv->event_window);
1944 gdk_event_free (event);
1946 if (x >= 0 && x <= width &&
1947 y >= 0 && y <= height)
1954 gtk_real_button_released (GtkButton *button)
1956 GtkButtonPrivate *priv = button->priv;
1958 if (priv->button_down)
1960 priv->button_down = FALSE;
1962 if (priv->activate_timeout)
1965 if (priv->in_button ||
1966 touch_release_in_button (button))
1967 gtk_button_clicked (button);
1969 gtk_button_update_state (button);
1974 gtk_real_button_clicked (GtkButton *button)
1976 GtkButtonPrivate *priv = button->priv;
1978 if (priv->action_helper)
1979 gtk_action_helper_activate (priv->action_helper);
1982 gtk_action_activate (priv->action);
1986 button_activate_timeout (gpointer data)
1988 gtk_button_finish_activate (data, TRUE);
1994 gtk_real_button_activate (GtkButton *button)
1996 GtkWidget *widget = GTK_WIDGET (button);
1997 GtkButtonPrivate *priv = button->priv;
2001 device = gtk_get_current_event_device ();
2003 if (device && gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
2004 device = gdk_device_get_associated_device (device);
2006 if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
2008 time = gtk_get_current_event_time ();
2010 /* bgo#626336 - Only grab if we have a device (from an event), not if we
2011 * were activated programmatically when no event is available.
2013 if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
2015 if (gdk_device_grab (device, priv->event_window,
2016 GDK_OWNERSHIP_WINDOW, TRUE,
2017 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
2018 NULL, time) == GDK_GRAB_SUCCESS)
2020 gtk_device_grab_add (widget, device, TRUE);
2021 priv->grab_keyboard = device;
2022 priv->grab_time = time;
2026 priv->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
2027 button_activate_timeout,
2029 priv->button_down = TRUE;
2030 gtk_button_update_state (button);
2031 gtk_widget_queue_draw (GTK_WIDGET (button));
2036 gtk_button_finish_activate (GtkButton *button,
2039 GtkWidget *widget = GTK_WIDGET (button);
2040 GtkButtonPrivate *priv = button->priv;
2042 g_source_remove (priv->activate_timeout);
2043 priv->activate_timeout = 0;
2045 if (priv->grab_keyboard)
2047 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
2048 gtk_device_grab_remove (widget, priv->grab_keyboard);
2049 priv->grab_keyboard = NULL;
2052 priv->button_down = FALSE;
2054 gtk_button_update_state (button);
2055 gtk_widget_queue_draw (GTK_WIDGET (button));
2058 gtk_button_clicked (button);
2063 gtk_button_get_size (GtkWidget *widget,
2064 GtkOrientation orientation,
2068 GtkButton *button = GTK_BUTTON (widget);
2069 GtkStyleContext *context;
2071 GtkBorder default_border;
2076 gint minimum, natural;
2078 context = gtk_widget_get_style_context (widget);
2080 gtk_button_get_props (button, &default_border, NULL,
2081 &padding, &border, NULL);
2082 gtk_style_context_get_style (context,
2083 "focus-line-width", &focus_width,
2084 "focus-padding", &focus_pad,
2087 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2089 minimum = padding.left + padding.right +
2090 border.left + border.right;
2092 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
2093 minimum += default_border.left + default_border.right;
2097 minimum = padding.top + padding.bottom +
2098 border.top + border.bottom;
2100 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
2101 minimum += default_border.top + default_border.bottom;
2104 minimum += 2 * (focus_width + focus_pad);
2107 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
2108 gtk_widget_get_visible (child))
2110 gint child_min, child_nat;
2112 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2113 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
2115 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
2117 minimum += child_min;
2118 natural += child_nat;
2122 *minimum_size = minimum;
2125 *natural_size = natural;
2129 gtk_button_get_preferred_width (GtkWidget *widget,
2133 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2137 gtk_button_get_preferred_height (GtkWidget *widget,
2141 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2145 * gtk_button_set_label:
2146 * @button: a #GtkButton
2149 * Sets the text of the label of the button to @str. This text is
2150 * also used to select the stock item if gtk_button_set_use_stock()
2153 * This will also clear any previously set labels.
2156 gtk_button_set_label (GtkButton *button,
2159 GtkButtonPrivate *priv;
2162 g_return_if_fail (GTK_IS_BUTTON (button));
2164 priv = button->priv;
2166 new_label = g_strdup (label);
2167 g_free (priv->label_text);
2168 priv->label_text = new_label;
2170 gtk_button_construct_child (button);
2172 g_object_notify (G_OBJECT (button), "label");
2176 * gtk_button_get_label:
2177 * @button: a #GtkButton
2179 * Fetches the text from the label of the button, as set by
2180 * gtk_button_set_label(). If the label text has not
2181 * been set the return value will be %NULL. This will be the
2182 * case if you create an empty button with gtk_button_new() to
2183 * use as a container.
2185 * Return value: The text of the label widget. This string is owned
2186 * by the widget and must not be modified or freed.
2189 gtk_button_get_label (GtkButton *button)
2191 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2193 return button->priv->label_text;
2197 * gtk_button_set_use_underline:
2198 * @button: a #GtkButton
2199 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2201 * If true, an underline in the text of the button label indicates
2202 * the next character should be used for the mnemonic accelerator key.
2205 gtk_button_set_use_underline (GtkButton *button,
2206 gboolean use_underline)
2208 GtkButtonPrivate *priv;
2210 g_return_if_fail (GTK_IS_BUTTON (button));
2212 priv = button->priv;
2214 use_underline = use_underline != FALSE;
2216 if (use_underline != priv->use_underline)
2218 priv->use_underline = use_underline;
2220 gtk_button_construct_child (button);
2222 g_object_notify (G_OBJECT (button), "use-underline");
2227 * gtk_button_get_use_underline:
2228 * @button: a #GtkButton
2230 * Returns whether an embedded underline in the button label indicates a
2231 * mnemonic. See gtk_button_set_use_underline ().
2233 * Return value: %TRUE if an embedded underline in the button label
2234 * indicates the mnemonic accelerator keys.
2237 gtk_button_get_use_underline (GtkButton *button)
2239 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2241 return button->priv->use_underline;
2245 * gtk_button_set_use_stock:
2246 * @button: a #GtkButton
2247 * @use_stock: %TRUE if the button should use a stock item
2249 * If %TRUE, the label set on the button is used as a
2250 * stock id to select the stock item for the button.
2253 gtk_button_set_use_stock (GtkButton *button,
2256 GtkButtonPrivate *priv;
2258 g_return_if_fail (GTK_IS_BUTTON (button));
2260 priv = button->priv;
2262 use_stock = use_stock != FALSE;
2264 if (use_stock != priv->use_stock)
2266 priv->use_stock = use_stock;
2268 gtk_button_construct_child (button);
2270 g_object_notify (G_OBJECT (button), "use-stock");
2275 * gtk_button_get_use_stock:
2276 * @button: a #GtkButton
2278 * Returns whether the button label is a stock item.
2280 * Return value: %TRUE if the button label is used to
2281 * select a stock item instead of being
2282 * used directly as the label text.
2285 gtk_button_get_use_stock (GtkButton *button)
2287 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2289 return button->priv->use_stock;
2293 * gtk_button_set_focus_on_click:
2294 * @button: a #GtkButton
2295 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2297 * Sets whether the button will grab focus when it is clicked with the mouse.
2298 * Making mouse clicks not grab focus is useful in places like toolbars where
2299 * you don't want the keyboard focus removed from the main area of the
2305 gtk_button_set_focus_on_click (GtkButton *button,
2306 gboolean focus_on_click)
2308 GtkButtonPrivate *priv;
2310 g_return_if_fail (GTK_IS_BUTTON (button));
2312 priv = button->priv;
2314 focus_on_click = focus_on_click != FALSE;
2316 if (priv->focus_on_click != focus_on_click)
2318 priv->focus_on_click = focus_on_click;
2320 g_object_notify (G_OBJECT (button), "focus-on-click");
2325 * gtk_button_get_focus_on_click:
2326 * @button: a #GtkButton
2328 * Returns whether the button grabs focus when it is clicked with the mouse.
2329 * See gtk_button_set_focus_on_click().
2331 * Return value: %TRUE if the button grabs focus when it is clicked with
2337 gtk_button_get_focus_on_click (GtkButton *button)
2339 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2341 return button->priv->focus_on_click;
2345 * gtk_button_set_alignment:
2346 * @button: a #GtkButton
2347 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2348 * 1.0 is right aligned
2349 * @yalign: the vertical position of the child, 0.0 is top aligned,
2350 * 1.0 is bottom aligned
2352 * Sets the alignment of the child. This property has no effect unless
2353 * the child is a #GtkMisc or a #GtkAlignment.
2358 gtk_button_set_alignment (GtkButton *button,
2362 GtkButtonPrivate *priv;
2364 g_return_if_fail (GTK_IS_BUTTON (button));
2366 priv = button->priv;
2368 priv->xalign = xalign;
2369 priv->yalign = yalign;
2370 priv->align_set = 1;
2372 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2374 g_object_freeze_notify (G_OBJECT (button));
2375 g_object_notify (G_OBJECT (button), "xalign");
2376 g_object_notify (G_OBJECT (button), "yalign");
2377 g_object_thaw_notify (G_OBJECT (button));
2381 * gtk_button_get_alignment:
2382 * @button: a #GtkButton
2383 * @xalign: (out): return location for horizontal alignment
2384 * @yalign: (out): return location for vertical alignment
2386 * Gets the alignment of the child in the button.
2391 gtk_button_get_alignment (GtkButton *button,
2395 GtkButtonPrivate *priv;
2397 g_return_if_fail (GTK_IS_BUTTON (button));
2399 priv = button->priv;
2402 *xalign = priv->xalign;
2405 *yalign = priv->yalign;
2409 * _gtk_button_set_depressed:
2410 * @button: a #GtkButton
2411 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2413 * Sets whether the button is currently drawn as down or not. This is
2414 * purely a visual setting, and is meant only for use by derived widgets
2415 * such as #GtkToggleButton.
2418 _gtk_button_set_depressed (GtkButton *button,
2421 GtkWidget *widget = GTK_WIDGET (button);
2422 GtkButtonPrivate *priv = button->priv;
2424 depressed = depressed != FALSE;
2426 if (depressed != priv->depressed)
2428 priv->depressed = depressed;
2429 gtk_widget_queue_resize (widget);
2434 gtk_button_update_state (GtkButton *button)
2436 GtkButtonPrivate *priv = button->priv;
2437 GtkStateFlags new_state;
2440 if (priv->activate_timeout)
2441 depressed = priv->depress_on_activate;
2443 depressed = priv->in_button && priv->button_down;
2445 new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) &
2446 ~(GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_ACTIVE);
2448 if (priv->in_button)
2449 new_state |= GTK_STATE_FLAG_PRELIGHT;
2452 new_state |= GTK_STATE_FLAG_ACTIVE;
2454 _gtk_button_set_depressed (button, depressed);
2455 gtk_widget_set_state_flags (GTK_WIDGET (button), new_state, TRUE);
2459 show_image_change_notify (GtkButton *button)
2461 GtkButtonPrivate *priv = button->priv;
2465 if (show_image (button))
2466 gtk_widget_show (priv->image);
2468 gtk_widget_hide (priv->image);
2473 traverse_container (GtkWidget *widget,
2476 if (GTK_IS_BUTTON (widget))
2477 show_image_change_notify (GTK_BUTTON (widget));
2478 else if (GTK_IS_CONTAINER (widget))
2479 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2483 gtk_button_setting_changed (GtkSettings *settings)
2487 list = gtk_window_list_toplevels ();
2489 for (l = list; l; l = l->next)
2490 gtk_container_forall (GTK_CONTAINER (l->data),
2491 traverse_container, NULL);
2498 gtk_button_screen_changed (GtkWidget *widget,
2499 GdkScreen *previous_screen)
2502 GtkButtonPrivate *priv;
2503 GtkSettings *settings;
2504 gulong show_image_connection;
2506 if (!gtk_widget_has_screen (widget))
2509 button = GTK_BUTTON (widget);
2510 priv = button->priv;
2512 /* If the button is being pressed while the screen changes the
2513 release might never occur, so we reset the state. */
2514 if (priv->button_down)
2516 priv->button_down = FALSE;
2517 gtk_button_update_state (button);
2520 settings = gtk_widget_get_settings (widget);
2522 show_image_connection =
2523 g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2524 NULL, gtk_button_setting_changed, NULL);
2526 if (show_image_connection)
2529 g_signal_connect (settings, "notify::gtk-button-images",
2530 G_CALLBACK (gtk_button_setting_changed), NULL);
2532 show_image_change_notify (button);
2536 gtk_button_state_changed (GtkWidget *widget,
2537 GtkStateType previous_state)
2539 GtkButton *button = GTK_BUTTON (widget);
2540 GtkButtonPrivate *priv = button->priv;
2542 if (!gtk_widget_is_sensitive (widget))
2544 priv->in_button = FALSE;
2545 gtk_real_button_released (button);
2550 gtk_button_grab_notify (GtkWidget *widget,
2551 gboolean was_grabbed)
2553 GtkButton *button = GTK_BUTTON (widget);
2554 GtkButtonPrivate *priv = button->priv;
2557 if (priv->activate_timeout &&
2558 priv->grab_keyboard &&
2559 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2560 gtk_button_finish_activate (button, FALSE);
2564 save_in = priv->in_button;
2565 priv->in_button = FALSE;
2566 gtk_real_button_released (button);
2567 if (save_in != priv->in_button)
2569 priv->in_button = save_in;
2570 gtk_button_update_state (button);
2576 * gtk_button_set_image:
2577 * @button: a #GtkButton
2578 * @image: a widget to set as the image for the button
2580 * Set the image of @button to the given widget. Note that
2581 * it depends on the #GtkSettings:gtk-button-images setting whether the
2582 * image will be displayed or not, you don't have to call
2583 * gtk_widget_show() on @image yourself.
2588 gtk_button_set_image (GtkButton *button,
2591 GtkButtonPrivate *priv;
2594 g_return_if_fail (GTK_IS_BUTTON (button));
2595 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2597 priv = button->priv;
2601 parent = gtk_widget_get_parent (priv->image);
2603 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2606 priv->image = image;
2607 priv->image_is_stock = (image == NULL);
2609 gtk_button_construct_child (button);
2611 g_object_notify (G_OBJECT (button), "image");
2615 * gtk_button_get_image:
2616 * @button: a #GtkButton
2618 * Gets the widget that is currenty set as the image of @button.
2619 * This may have been explicitly set by gtk_button_set_image()
2620 * or constructed by gtk_button_new_from_stock().
2622 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2627 gtk_button_get_image (GtkButton *button)
2629 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2631 return button->priv->image;
2635 * gtk_button_set_image_position:
2636 * @button: a #GtkButton
2637 * @position: the position
2639 * Sets the position of the image relative to the text
2640 * inside the button.
2645 gtk_button_set_image_position (GtkButton *button,
2646 GtkPositionType position)
2648 GtkButtonPrivate *priv;
2650 g_return_if_fail (GTK_IS_BUTTON (button));
2651 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2653 priv = button->priv;
2655 if (priv->image_position != position)
2657 priv->image_position = position;
2659 gtk_button_construct_child (button);
2661 g_object_notify (G_OBJECT (button), "image-position");
2666 * gtk_button_get_image_position:
2667 * @button: a #GtkButton
2669 * Gets the position of the image relative to the text
2670 * inside the button.
2672 * Return value: the position
2677 gtk_button_get_image_position (GtkButton *button)
2679 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2681 return button->priv->image_position;
2685 * gtk_button_set_always_show_image:
2686 * @button: a #GtkButton
2687 * @always_show: %TRUE if the menuitem should always show the image
2689 * If %TRUE, the button will ignore the #GtkSettings:gtk-button-images
2690 * setting and always show the image, if available.
2692 * Use this property if the button would be useless or hard to use
2693 * without the image.
2698 gtk_button_set_always_show_image (GtkButton *button,
2699 gboolean always_show)
2701 GtkButtonPrivate *priv;
2703 g_return_if_fail (GTK_IS_BUTTON (button));
2705 priv = button->priv;
2707 if (priv->always_show_image != always_show)
2709 priv->always_show_image = always_show;
2713 if (show_image (button))
2714 gtk_widget_show (priv->image);
2716 gtk_widget_hide (priv->image);
2719 g_object_notify (G_OBJECT (button), "always-show-image");
2724 * gtk_button_get_always_show_image:
2725 * @button: a #GtkButton
2727 * Returns whether the button will ignore the #GtkSettings:gtk-button-images
2728 * setting and always show the image, if available.
2730 * Returns: %TRUE if the button will always show the image
2735 gtk_button_get_always_show_image (GtkButton *button)
2737 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2739 return button->priv->always_show_image;
2743 * gtk_button_get_event_window:
2744 * @button: a #GtkButton
2746 * Returns the button's event window if it is realized, %NULL otherwise.
2747 * This function should be rarely needed.
2749 * Return value: (transfer none): @button's event window.
2754 gtk_button_get_event_window (GtkButton *button)
2756 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2758 return button->priv->event_window;