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 "gtkactionable.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
95 /* activatable properties */
96 PROP_ACTIVATABLE_RELATED_ACTION,
97 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
101 static void gtk_button_destroy (GtkWidget *widget);
102 static void gtk_button_dispose (GObject *object);
103 static void gtk_button_set_property (GObject *object,
107 static void gtk_button_get_property (GObject *object,
111 static void gtk_button_screen_changed (GtkWidget *widget,
112 GdkScreen *previous_screen);
113 static void gtk_button_realize (GtkWidget * widget);
114 static void gtk_button_unrealize (GtkWidget * widget);
115 static void gtk_button_map (GtkWidget * widget);
116 static void gtk_button_unmap (GtkWidget * widget);
117 static void gtk_button_style_updated (GtkWidget * widget);
118 static void gtk_button_size_allocate (GtkWidget * widget,
119 GtkAllocation * allocation);
120 static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
121 static gint gtk_button_button_press (GtkWidget * widget,
122 GdkEventButton * event);
123 static gint gtk_button_button_release (GtkWidget * widget,
124 GdkEventButton * event);
125 static gint gtk_button_grab_broken (GtkWidget * widget,
126 GdkEventGrabBroken * event);
127 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
128 static gint gtk_button_enter_notify (GtkWidget * widget,
129 GdkEventCrossing * event);
130 static gint gtk_button_leave_notify (GtkWidget * widget,
131 GdkEventCrossing * event);
132 static void gtk_real_button_pressed (GtkButton * button);
133 static void gtk_real_button_released (GtkButton * button);
134 static void gtk_real_button_clicked (GtkButton * button);
135 static void gtk_real_button_activate (GtkButton *button);
136 static void gtk_button_update_state (GtkButton *button);
137 static void gtk_button_add (GtkContainer *container,
139 static GType gtk_button_child_type (GtkContainer *container);
140 static void gtk_button_finish_activate (GtkButton *button,
143 static GObject* gtk_button_constructor (GType type,
144 guint n_construct_properties,
145 GObjectConstructParam *construct_params);
146 static void gtk_button_construct_child (GtkButton *button);
147 static void gtk_button_state_changed (GtkWidget *widget,
148 GtkStateType previous_state);
149 static void gtk_button_grab_notify (GtkWidget *widget,
150 gboolean was_grabbed);
151 static void gtk_button_hierarchy_changed (GtkWidget *widget,
152 GtkWidget *previous_toplevel);
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->grab_broken_event = gtk_button_grab_broken;
211 widget_class->key_release_event = gtk_button_key_release;
212 widget_class->enter_notify_event = gtk_button_enter_notify;
213 widget_class->leave_notify_event = gtk_button_leave_notify;
214 widget_class->state_changed = gtk_button_state_changed;
215 widget_class->grab_notify = gtk_button_grab_notify;
216 widget_class->hierarchy_changed = gtk_button_hierarchy_changed;
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));
339 g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
340 g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
342 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
343 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
346 * GtkButton::pressed:
347 * @button: the object that received the signal
349 * Emitted when the button is pressed.
351 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
353 button_signals[PRESSED] =
354 g_signal_new (I_("pressed"),
355 G_OBJECT_CLASS_TYPE (gobject_class),
357 G_STRUCT_OFFSET (GtkButtonClass, pressed),
359 _gtk_marshal_VOID__VOID,
363 * GtkButton::released:
364 * @button: the object that received the signal
366 * Emitted when the button is released.
368 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
370 button_signals[RELEASED] =
371 g_signal_new (I_("released"),
372 G_OBJECT_CLASS_TYPE (gobject_class),
374 G_STRUCT_OFFSET (GtkButtonClass, released),
376 _gtk_marshal_VOID__VOID,
380 * GtkButton::clicked:
381 * @button: the object that received the signal
383 * Emitted when the button has been activated (pressed and released).
385 button_signals[CLICKED] =
386 g_signal_new (I_("clicked"),
387 G_OBJECT_CLASS_TYPE (gobject_class),
388 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
389 G_STRUCT_OFFSET (GtkButtonClass, clicked),
391 _gtk_marshal_VOID__VOID,
396 * @button: the object that received the signal
398 * Emitted when the pointer enters the button.
400 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
402 button_signals[ENTER] =
403 g_signal_new (I_("enter"),
404 G_OBJECT_CLASS_TYPE (gobject_class),
406 G_STRUCT_OFFSET (GtkButtonClass, enter),
408 _gtk_marshal_VOID__VOID,
413 * @button: the object that received the signal
415 * Emitted when the pointer leaves the button.
417 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
419 button_signals[LEAVE] =
420 g_signal_new (I_("leave"),
421 G_OBJECT_CLASS_TYPE (gobject_class),
423 G_STRUCT_OFFSET (GtkButtonClass, leave),
425 _gtk_marshal_VOID__VOID,
429 * GtkButton::activate:
430 * @widget: the object which received the signal.
432 * The ::activate signal on GtkButton is an action signal and
433 * emitting it causes the button to animate press then release.
434 * Applications should never connect to this signal, but use the
435 * #GtkButton::clicked signal.
437 button_signals[ACTIVATE] =
438 g_signal_new (I_("activate"),
439 G_OBJECT_CLASS_TYPE (gobject_class),
440 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
441 G_STRUCT_OFFSET (GtkButtonClass, activate),
443 _gtk_marshal_VOID__VOID,
445 widget_class->activate_signal = button_signals[ACTIVATE];
448 * GtkButton:default-border:
450 * The "default-border" style property defines the extra space to add
451 * around a button that can become the default widget of its window.
452 * For more information about default widgets, see gtk_widget_grab_default().
455 gtk_widget_class_install_style_property (widget_class,
456 g_param_spec_boxed ("default-border",
457 P_("Default Spacing"),
458 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
460 GTK_PARAM_READABLE));
463 * GtkButton:default-outside-border:
465 * The "default-outside-border" style property defines the extra outside
466 * space to add around a button that can become the default widget of its
467 * window. Extra outside space is always drawn outside the button border.
468 * For more information about default widgets, see gtk_widget_grab_default().
470 gtk_widget_class_install_style_property (widget_class,
471 g_param_spec_boxed ("default-outside-border",
472 P_("Default Outside Spacing"),
473 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
475 GTK_PARAM_READABLE));
476 gtk_widget_class_install_style_property (widget_class,
477 g_param_spec_int ("child-displacement-x",
478 P_("Child X Displacement"),
479 P_("How far in the x direction to move the child when the button is depressed"),
483 GTK_PARAM_READABLE));
484 gtk_widget_class_install_style_property (widget_class,
485 g_param_spec_int ("child-displacement-y",
486 P_("Child Y Displacement"),
487 P_("How far in the y direction to move the child when the button is depressed"),
491 GTK_PARAM_READABLE));
494 * GtkButton:displace-focus:
496 * Whether the child_displacement_x/child_displacement_y properties
497 * should also affect the focus rectangle.
501 gtk_widget_class_install_style_property (widget_class,
502 g_param_spec_boolean ("displace-focus",
503 P_("Displace focus"),
504 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
506 GTK_PARAM_READABLE));
509 * GtkButton:inner-border:
511 * Sets the border between the button edges and child.
515 * Deprecated: 3.4: Use the standard border and padding CSS properties;
516 * the value of this style property is ignored.
518 gtk_widget_class_install_style_property (widget_class,
519 g_param_spec_boxed ("inner-border",
521 P_("Border between button edges and child."),
523 GTK_PARAM_READABLE));
526 * GtkButton::image-spacing:
528 * Spacing in pixels between the image and label.
532 gtk_widget_class_install_style_property (widget_class,
533 g_param_spec_int ("image-spacing",
535 P_("Spacing in pixels between the image and label"),
539 GTK_PARAM_READABLE));
541 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
543 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_BUTTON_ACCESSIBLE);
547 gtk_button_init (GtkButton *button)
549 GtkButtonPrivate *priv;
550 GtkStyleContext *context;
552 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
557 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
558 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
559 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
561 priv->label_text = NULL;
563 priv->constructed = FALSE;
564 priv->in_button = FALSE;
565 priv->button_down = FALSE;
566 priv->relief = GTK_RELIEF_NORMAL;
567 priv->use_stock = FALSE;
568 priv->use_underline = FALSE;
569 priv->depressed = FALSE;
570 priv->depress_on_activate = TRUE;
571 priv->focus_on_click = TRUE;
576 priv->image_is_stock = TRUE;
577 priv->image_position = GTK_POS_LEFT;
578 priv->use_action_appearance = TRUE;
580 context = gtk_widget_get_style_context (GTK_WIDGET (button));
581 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
585 gtk_button_destroy (GtkWidget *widget)
587 GtkButton *button = GTK_BUTTON (widget);
588 GtkButtonPrivate *priv = button->priv;
590 if (priv->label_text)
592 g_free (priv->label_text);
593 priv->label_text = NULL;
596 if (priv->action_name)
598 g_free (priv->action_name);
599 priv->action_name = NULL;
602 GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
606 gtk_button_constructor (GType type,
607 guint n_construct_properties,
608 GObjectConstructParam *construct_params)
612 GtkButtonPrivate *priv;
614 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
615 n_construct_properties,
618 button = GTK_BUTTON (object);
621 priv->constructed = TRUE;
623 if (priv->label_text != NULL)
624 gtk_button_construct_child (button);
631 gtk_button_child_type (GtkContainer *container)
633 if (!gtk_bin_get_child (GTK_BIN (container)))
634 return GTK_TYPE_WIDGET;
640 maybe_set_alignment (GtkButton *button,
643 GtkButtonPrivate *priv = button->priv;
645 if (GTK_IS_MISC (widget))
647 GtkMisc *misc = GTK_MISC (widget);
650 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
652 else if (GTK_IS_ALIGNMENT (widget))
654 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
655 gfloat xscale, yscale;
657 g_object_get (alignment,
663 gtk_alignment_set (alignment,
664 priv->xalign, priv->yalign,
670 gtk_button_add (GtkContainer *container,
673 maybe_set_alignment (GTK_BUTTON (container), widget);
675 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
679 gtk_button_dispose (GObject *object)
681 GtkButton *button = GTK_BUTTON (object);
682 GtkButtonPrivate *priv = button->priv;
684 g_clear_object (&priv->action_observer);
688 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
691 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
695 gtk_button_update_action_observer (GtkButton *button)
699 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
701 /* we are the only owner so this will clear all the signals */
702 g_clear_object (&button->priv->action_observer);
704 window = gtk_widget_get_toplevel (GTK_WIDGET (button));
706 if (GTK_IS_APPLICATION_WINDOW (window) && button->priv->action_name)
708 GSimpleActionObserver *observer;
710 observer = gtk_application_window_create_observer (GTK_APPLICATION_WINDOW (window),
711 button->priv->action_name,
712 button->priv->action_target);
714 _gtk_button_set_depressed (button, g_simple_action_observer_get_active (observer));
716 if (g_object_class_find_property (G_OBJECT_GET_CLASS (button), "active"))
717 g_object_bind_property (observer, "active", button, "active", G_BINDING_SYNC_CREATE);
718 g_object_bind_property (observer, "enabled", button, "sensitive", G_BINDING_SYNC_CREATE);
720 button->priv->action_observer = observer;
722 g_signal_connect_after (button, "clicked", G_CALLBACK (gtk_real_button_clicked), NULL);
727 gtk_button_set_action_name (GtkActionable *actionable,
728 const gchar *action_name)
730 GtkButton *button = GTK_BUTTON (actionable);
732 g_return_if_fail (GTK_IS_BUTTON (button));
733 g_return_if_fail (button->priv->action == NULL);
735 g_free (button->priv->action_name);
736 button->priv->action_name = g_strdup (action_name);
738 gtk_button_update_action_observer (button);
740 g_object_notify (G_OBJECT (button), "action-name");
744 gtk_button_set_action_target_value (GtkActionable *actionable,
745 GVariant *action_target)
747 GtkButton *button = GTK_BUTTON (actionable);
749 g_return_if_fail (GTK_IS_BUTTON (button));
751 if (action_target != button->priv->action_target &&
752 (!action_target || !button->priv->action_target ||
753 !g_variant_equal (action_target, button->priv->action_target)))
755 if (button->priv->action_target)
756 g_variant_unref (button->priv->action_target);
758 button->priv->action_target = NULL;
761 button->priv->action_target = g_variant_ref_sink (action_target);
763 gtk_button_update_action_observer (button);
765 g_object_notify (G_OBJECT (button), "action-target");
770 gtk_button_set_property (GObject *object,
775 GtkButton *button = GTK_BUTTON (object);
776 GtkButtonPrivate *priv = button->priv;
781 gtk_button_set_label (button, g_value_get_string (value));
784 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
787 gtk_button_set_relief (button, g_value_get_enum (value));
789 case PROP_USE_UNDERLINE:
790 gtk_button_set_use_underline (button, g_value_get_boolean (value));
793 gtk_button_set_use_stock (button, g_value_get_boolean (value));
795 case PROP_FOCUS_ON_CLICK:
796 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
799 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
802 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
804 case PROP_IMAGE_POSITION:
805 gtk_button_set_image_position (button, g_value_get_enum (value));
807 case PROP_ACTIVATABLE_RELATED_ACTION:
808 gtk_button_set_related_action (button, g_value_get_object (value));
810 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
811 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
813 case PROP_ACTION_NAME:
814 gtk_button_set_action_name (GTK_ACTIONABLE (button), g_value_get_string (value));
816 case PROP_ACTION_TARGET:
817 gtk_button_set_action_target_value (GTK_ACTIONABLE (button), g_value_get_variant (value));
820 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
826 gtk_button_get_property (GObject *object,
831 GtkButton *button = GTK_BUTTON (object);
832 GtkButtonPrivate *priv = button->priv;
837 g_value_set_string (value, priv->label_text);
840 g_value_set_object (value, (GObject *)priv->image);
843 g_value_set_enum (value, priv->relief);
845 case PROP_USE_UNDERLINE:
846 g_value_set_boolean (value, priv->use_underline);
849 g_value_set_boolean (value, priv->use_stock);
851 case PROP_FOCUS_ON_CLICK:
852 g_value_set_boolean (value, priv->focus_on_click);
855 g_value_set_float (value, priv->xalign);
858 g_value_set_float (value, priv->yalign);
860 case PROP_IMAGE_POSITION:
861 g_value_set_enum (value, priv->image_position);
863 case PROP_ACTIVATABLE_RELATED_ACTION:
864 g_value_set_object (value, priv->action);
866 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
867 g_value_set_boolean (value, priv->use_action_appearance);
869 case PROP_ACTION_NAME:
870 g_value_set_string (value, priv->action_name);
872 case PROP_ACTION_TARGET:
873 g_value_set_variant (value, priv->action_target);
876 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
882 gtk_button_get_action_name (GtkActionable *actionable)
884 GtkButton *button = GTK_BUTTON (actionable);
886 return button->priv->action_name;
890 gtk_button_get_action_target_value (GtkActionable *actionable)
892 GtkButton *button = GTK_BUTTON (actionable);
894 return button->priv->action_target;
898 gtk_button_actionable_iface_init (GtkActionableInterface *iface)
900 iface->get_action_name = gtk_button_get_action_name;
901 iface->set_action_name = gtk_button_set_action_name;
902 iface->get_action_target_value = gtk_button_get_action_target_value;
903 iface->set_action_target_value = gtk_button_set_action_target_value;
907 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
909 iface->update = gtk_button_update;
910 iface->sync_action_properties = gtk_button_sync_action_properties;
914 activatable_update_stock_id (GtkButton *button,
917 if (!gtk_button_get_use_stock (button))
920 gtk_button_set_label (button, gtk_action_get_stock_id (action));
924 activatable_update_short_label (GtkButton *button,
930 if (gtk_button_get_use_stock (button))
933 image = gtk_button_get_image (button);
935 /* Dont touch custom child... */
936 child = gtk_bin_get_child (GTK_BIN (button));
937 if (GTK_IS_IMAGE (image) ||
939 GTK_IS_LABEL (child))
941 gtk_button_set_label (button, gtk_action_get_short_label (action));
942 gtk_button_set_use_underline (button, TRUE);
947 activatable_update_icon_name (GtkButton *button,
952 if (gtk_button_get_use_stock (button))
955 image = gtk_button_get_image (button);
957 if (GTK_IS_IMAGE (image) &&
958 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
959 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
960 gtk_image_set_from_icon_name (GTK_IMAGE (image),
961 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
965 activatable_update_gicon (GtkButton *button,
968 GtkWidget *image = gtk_button_get_image (button);
969 GIcon *icon = gtk_action_get_gicon (action);
971 if (GTK_IS_IMAGE (image) &&
972 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
973 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
974 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
978 gtk_button_update (GtkActivatable *activatable,
980 const gchar *property_name)
982 GtkButton *button = GTK_BUTTON (activatable);
983 GtkButtonPrivate *priv = button->priv;
985 if (strcmp (property_name, "visible") == 0)
987 if (gtk_action_is_visible (action))
988 gtk_widget_show (GTK_WIDGET (activatable));
990 gtk_widget_hide (GTK_WIDGET (activatable));
992 else if (strcmp (property_name, "sensitive") == 0)
993 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
995 if (!priv->use_action_appearance)
998 if (strcmp (property_name, "stock-id") == 0)
999 activatable_update_stock_id (GTK_BUTTON (activatable), action);
1000 else if (strcmp (property_name, "gicon") == 0)
1001 activatable_update_gicon (GTK_BUTTON (activatable), action);
1002 else if (strcmp (property_name, "short-label") == 0)
1003 activatable_update_short_label (GTK_BUTTON (activatable), action);
1004 else if (strcmp (property_name, "icon-name") == 0)
1005 activatable_update_icon_name (GTK_BUTTON (activatable), action);
1009 gtk_button_sync_action_properties (GtkActivatable *activatable,
1012 GtkButton *button = GTK_BUTTON (activatable);
1013 GtkButtonPrivate *priv = button->priv;
1018 if (gtk_action_is_visible (action))
1019 gtk_widget_show (GTK_WIDGET (activatable));
1021 gtk_widget_hide (GTK_WIDGET (activatable));
1023 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1025 if (priv->use_action_appearance)
1027 activatable_update_stock_id (GTK_BUTTON (activatable), action);
1028 activatable_update_short_label (GTK_BUTTON (activatable), action);
1029 activatable_update_gicon (GTK_BUTTON (activatable), action);
1030 activatable_update_icon_name (GTK_BUTTON (activatable), action);
1035 gtk_button_set_related_action (GtkButton *button,
1038 GtkButtonPrivate *priv = button->priv;
1040 g_return_if_fail (button->priv->action_name == NULL);
1042 if (priv->action == action)
1045 /* This should be a default handler, but for compatibility reasons
1046 * we need to support derived classes that don't chain up their
1049 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
1051 g_signal_connect_after (button, "clicked",
1052 G_CALLBACK (gtk_real_button_clicked), NULL);
1054 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
1056 priv->action = action;
1060 gtk_button_set_use_action_appearance (GtkButton *button,
1061 gboolean use_appearance)
1063 GtkButtonPrivate *priv = button->priv;
1065 if (priv->use_action_appearance != use_appearance)
1067 priv->use_action_appearance = use_appearance;
1069 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
1076 * Creates a new #GtkButton widget. To add a child widget to the button,
1077 * use gtk_container_add().
1079 * Returns: The newly created #GtkButton widget.
1082 gtk_button_new (void)
1084 return g_object_new (GTK_TYPE_BUTTON, NULL);
1088 show_image (GtkButton *button)
1090 GtkButtonPrivate *priv = button->priv;
1093 if (priv->label_text)
1095 GtkSettings *settings;
1097 settings = gtk_widget_get_settings (GTK_WIDGET (button));
1098 g_object_get (settings, "gtk-button-images", &show, NULL);
1107 gtk_button_construct_child (GtkButton *button)
1109 GtkButtonPrivate *priv = button->priv;
1110 GtkStyleContext *context;
1116 GtkWidget *image = NULL;
1117 gchar *label_text = NULL;
1120 if (!priv->constructed)
1123 if (!priv->label_text && !priv->image)
1126 context = gtk_widget_get_style_context (GTK_WIDGET (button));
1128 gtk_style_context_get_style (context,
1129 "image-spacing", &image_spacing,
1132 if (priv->image && !priv->image_is_stock)
1136 image = g_object_ref (priv->image);
1138 parent = gtk_widget_get_parent (image);
1140 gtk_container_remove (GTK_CONTAINER (parent), image);
1145 child = gtk_bin_get_child (GTK_BIN (button));
1147 gtk_container_remove (GTK_CONTAINER (button), child);
1149 if (priv->use_stock &&
1151 gtk_stock_lookup (priv->label_text, &item))
1154 image = g_object_ref (gtk_image_new_from_stock (priv->label_text, GTK_ICON_SIZE_BUTTON));
1156 label_text = item.label;
1159 label_text = priv->label_text;
1163 priv->image = image;
1164 g_object_set (priv->image,
1165 "visible", show_image (button),
1166 "no-show-all", TRUE,
1169 if (priv->image_position == GTK_POS_LEFT ||
1170 priv->image_position == GTK_POS_RIGHT)
1171 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1173 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1175 if (priv->align_set)
1176 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1178 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1180 if (priv->image_position == GTK_POS_LEFT ||
1181 priv->image_position == GTK_POS_TOP)
1182 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1184 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1188 if (priv->use_underline || priv->use_stock)
1190 label = gtk_label_new_with_mnemonic (label_text);
1191 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1192 GTK_WIDGET (button));
1195 label = gtk_label_new (label_text);
1197 if (priv->image_position == GTK_POS_RIGHT ||
1198 priv->image_position == GTK_POS_BOTTOM)
1199 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1201 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1204 gtk_container_add (GTK_CONTAINER (button), align);
1205 gtk_container_add (GTK_CONTAINER (align), box);
1206 gtk_widget_show_all (align);
1208 g_object_unref (image);
1213 if (priv->use_underline || priv->use_stock)
1215 label = gtk_label_new_with_mnemonic (priv->label_text);
1216 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1219 label = gtk_label_new (priv->label_text);
1221 if (priv->align_set)
1222 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1224 gtk_widget_show (label);
1225 gtk_container_add (GTK_CONTAINER (button), label);
1230 * gtk_button_new_with_label:
1231 * @label: The text you want the #GtkLabel to hold.
1233 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1236 * Returns: The newly created #GtkButton widget.
1239 gtk_button_new_with_label (const gchar *label)
1241 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1245 * gtk_button_new_from_stock:
1246 * @stock_id: the name of the stock item
1248 * Creates a new #GtkButton containing the image and text from a stock item.
1249 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1252 * If @stock_id is unknown, then it will be treated as a mnemonic
1253 * label (as for gtk_button_new_with_mnemonic()).
1255 * Returns: a new #GtkButton
1258 gtk_button_new_from_stock (const gchar *stock_id)
1260 return g_object_new (GTK_TYPE_BUTTON,
1263 "use-underline", TRUE,
1268 * gtk_button_new_with_mnemonic:
1269 * @label: The text of the button, with an underscore in front of the
1270 * mnemonic character
1272 * Creates a new #GtkButton containing a label.
1273 * If characters in @label are preceded by an underscore, they are underlined.
1274 * If you need a literal underscore character in a label, use '__' (two
1275 * underscores). The first underlined character represents a keyboard
1276 * accelerator called a mnemonic.
1277 * Pressing Alt and that key activates the button.
1279 * Returns: a new #GtkButton
1282 gtk_button_new_with_mnemonic (const gchar *label)
1284 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1288 * gtk_button_pressed:
1289 * @button: The #GtkButton you want to send the signal to.
1291 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1293 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1296 gtk_button_pressed (GtkButton *button)
1298 g_return_if_fail (GTK_IS_BUTTON (button));
1300 g_signal_emit (button, button_signals[PRESSED], 0);
1304 * gtk_button_released:
1305 * @button: The #GtkButton you want to send the signal to.
1307 * Emits a #GtkButton::released signal to the given #GtkButton.
1309 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1312 gtk_button_released (GtkButton *button)
1314 g_return_if_fail (GTK_IS_BUTTON (button));
1316 g_signal_emit (button, button_signals[RELEASED], 0);
1320 * gtk_button_clicked:
1321 * @button: The #GtkButton you want to send the signal to.
1323 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1326 gtk_button_clicked (GtkButton *button)
1328 g_return_if_fail (GTK_IS_BUTTON (button));
1330 g_signal_emit (button, button_signals[CLICKED], 0);
1335 * @button: The #GtkButton you want to send the signal to.
1337 * Emits a #GtkButton::enter signal to the given #GtkButton.
1339 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1342 gtk_button_enter (GtkButton *button)
1344 g_return_if_fail (GTK_IS_BUTTON (button));
1346 g_signal_emit (button, button_signals[ENTER], 0);
1351 * @button: The #GtkButton you want to send the signal to.
1353 * Emits a #GtkButton::leave signal to the given #GtkButton.
1355 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1358 gtk_button_leave (GtkButton *button)
1360 g_return_if_fail (GTK_IS_BUTTON (button));
1362 g_signal_emit (button, button_signals[LEAVE], 0);
1366 * gtk_button_set_relief:
1367 * @button: The #GtkButton you want to set relief styles of.
1368 * @newstyle: The GtkReliefStyle as described above.
1370 * Sets the relief style of the edges of the given #GtkButton widget.
1371 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1372 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1374 * <!-- FIXME: put pictures of each style -->
1377 gtk_button_set_relief (GtkButton *button,
1378 GtkReliefStyle newrelief)
1380 GtkButtonPrivate *priv;
1382 g_return_if_fail (GTK_IS_BUTTON (button));
1384 priv = button->priv;
1386 if (newrelief != priv->relief)
1388 priv->relief = newrelief;
1389 g_object_notify (G_OBJECT (button), "relief");
1390 gtk_widget_queue_draw (GTK_WIDGET (button));
1395 * gtk_button_get_relief:
1396 * @button: The #GtkButton you want the #GtkReliefStyle from.
1398 * Returns the current relief style of the given #GtkButton.
1400 * Returns: The current #GtkReliefStyle
1403 gtk_button_get_relief (GtkButton *button)
1405 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1407 return button->priv->relief;
1411 gtk_button_realize (GtkWidget *widget)
1413 GtkButton *button = GTK_BUTTON (widget);
1414 GtkButtonPrivate *priv = button->priv;
1415 GtkAllocation allocation;
1417 GdkWindowAttr attributes;
1418 gint attributes_mask;
1420 gtk_widget_get_allocation (widget, &allocation);
1422 gtk_widget_set_realized (widget, TRUE);
1424 attributes.window_type = GDK_WINDOW_CHILD;
1425 attributes.x = allocation.x;
1426 attributes.y = allocation.y;
1427 attributes.width = allocation.width;
1428 attributes.height = allocation.height;
1429 attributes.wclass = GDK_INPUT_ONLY;
1430 attributes.event_mask = gtk_widget_get_events (widget);
1431 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1432 GDK_BUTTON_RELEASE_MASK |
1433 GDK_ENTER_NOTIFY_MASK |
1434 GDK_LEAVE_NOTIFY_MASK);
1436 attributes_mask = GDK_WA_X | GDK_WA_Y;
1438 window = gtk_widget_get_parent_window (widget);
1439 gtk_widget_set_window (widget, window);
1440 g_object_ref (window);
1442 priv->event_window = gdk_window_new (window,
1443 &attributes, attributes_mask);
1444 gdk_window_set_user_data (priv->event_window, button);
1448 gtk_button_unrealize (GtkWidget *widget)
1450 GtkButton *button = GTK_BUTTON (widget);
1451 GtkButtonPrivate *priv = button->priv;
1453 if (priv->activate_timeout)
1454 gtk_button_finish_activate (button, FALSE);
1456 if (priv->event_window)
1458 gdk_window_set_user_data (priv->event_window, NULL);
1459 gdk_window_destroy (priv->event_window);
1460 priv->event_window = NULL;
1463 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1467 gtk_button_map (GtkWidget *widget)
1469 GtkButton *button = GTK_BUTTON (widget);
1470 GtkButtonPrivate *priv = button->priv;
1472 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1474 if (priv->event_window)
1475 gdk_window_show (priv->event_window);
1479 gtk_button_unmap (GtkWidget *widget)
1481 GtkButton *button = GTK_BUTTON (widget);
1482 GtkButtonPrivate *priv = button->priv;
1484 if (priv->event_window)
1485 gdk_window_hide (priv->event_window);
1487 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1491 gtk_button_update_image_spacing (GtkButton *button,
1492 GtkStyleContext *context)
1494 GtkButtonPrivate *priv = button->priv;
1498 /* Keep in sync with gtk_button_construct_child,
1499 * we only want to update the spacing if the box
1500 * was constructed there.
1502 if (!priv->constructed || !priv->image)
1505 child = gtk_bin_get_child (GTK_BIN (button));
1506 if (GTK_IS_ALIGNMENT (child))
1508 child = gtk_bin_get_child (GTK_BIN (child));
1509 if (GTK_IS_BOX (child))
1511 gtk_style_context_get_style (context,
1512 "image-spacing", &spacing,
1515 gtk_box_set_spacing (GTK_BOX (child), spacing);
1521 gtk_button_style_updated (GtkWidget *widget)
1523 GtkStyleContext *context;
1525 GTK_WIDGET_CLASS (gtk_button_parent_class)->style_updated (widget);
1527 context = gtk_widget_get_style_context (widget);
1529 gtk_button_update_image_spacing (GTK_BUTTON (widget), context);
1533 gtk_button_get_props (GtkButton *button,
1534 GtkBorder *default_border,
1535 GtkBorder *default_outside_border,
1538 gboolean *interior_focus)
1540 GtkStyleContext *context;
1541 GtkStateFlags state;
1542 GtkBorder *tmp_border;
1544 context = gtk_widget_get_style_context (GTK_WIDGET (button));
1545 state = gtk_style_context_get_state (context);
1549 gtk_style_context_get_style (context,
1550 "default-border", &tmp_border,
1555 *default_border = *tmp_border;
1556 gtk_border_free (tmp_border);
1559 *default_border = default_default_border;
1562 if (default_outside_border)
1564 gtk_style_context_get_style (context,
1565 "default-outside-border", &tmp_border,
1570 *default_outside_border = *tmp_border;
1571 gtk_border_free (tmp_border);
1574 *default_outside_border = default_default_outside_border;
1579 gtk_style_context_get_style (context,
1580 "interior-focus", interior_focus,
1585 gtk_style_context_get_padding (context, state, padding);
1588 gtk_style_context_get_border (context, state, border);
1592 gtk_button_size_allocate (GtkWidget *widget,
1593 GtkAllocation *allocation)
1595 GtkButton *button = GTK_BUTTON (widget);
1596 GtkButtonPrivate *priv = button->priv;
1597 GtkAllocation child_allocation;
1598 GtkStyleContext *context;
1600 GtkBorder default_border;
1606 context = gtk_widget_get_style_context (widget);
1608 gtk_button_get_props (button, &default_border, NULL,
1609 &padding, &border, NULL);
1610 gtk_style_context_get_style (context,
1611 "focus-line-width", &focus_width,
1612 "focus-padding", &focus_pad,
1615 gtk_widget_set_allocation (widget, allocation);
1617 if (gtk_widget_get_realized (widget))
1618 gdk_window_move_resize (priv->event_window,
1622 allocation->height);
1624 child = gtk_bin_get_child (GTK_BIN (button));
1625 if (child && gtk_widget_get_visible (child))
1627 child_allocation.x = allocation->x + padding.left + border.left;
1628 child_allocation.y = allocation->y + padding.top + border.top;
1630 child_allocation.width =
1632 (padding.left + padding.right) -
1633 (border.left + border.right);
1635 child_allocation.height =
1636 allocation->height -
1637 (padding.top + padding.bottom) -
1638 (border.top + border.bottom);
1640 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1642 child_allocation.x += default_border.left;
1643 child_allocation.y += default_border.top;
1644 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1645 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1648 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1650 child_allocation.x += focus_width + focus_pad;
1651 child_allocation.y += focus_width + focus_pad;
1652 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1653 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1656 if (priv->depressed)
1658 gint child_displacement_x;
1659 gint child_displacement_y;
1661 gtk_style_context_get_style (context,
1662 "child-displacement-x", &child_displacement_x,
1663 "child-displacement-y", &child_displacement_y,
1665 child_allocation.x += child_displacement_x;
1666 child_allocation.y += child_displacement_y;
1669 child_allocation.width = MAX (1, child_allocation.width);
1670 child_allocation.height = MAX (1, child_allocation.height);
1672 gtk_widget_size_allocate (child, &child_allocation);
1677 _gtk_button_paint (GtkButton *button,
1681 GtkStateFlags state)
1683 GtkButtonPrivate *priv = button->priv;
1686 GtkBorder default_border;
1687 GtkBorder default_outside_border;
1688 gboolean interior_focus;
1691 GtkAllocation allocation;
1692 GtkStyleContext *context;
1693 gboolean draw_focus;
1695 widget = GTK_WIDGET (button);
1696 context = gtk_widget_get_style_context (widget);
1698 gtk_style_context_save (context);
1699 gtk_style_context_set_state (context, state);
1701 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, NULL, &interior_focus);
1702 gtk_style_context_get_style (context,
1703 "focus-line-width", &focus_width,
1704 "focus-padding", &focus_pad,
1707 gtk_widget_get_allocation (widget, &allocation);
1712 if (gtk_widget_has_default (widget) &&
1713 priv->relief == GTK_RELIEF_NORMAL)
1715 x += default_border.left;
1716 y += default_border.top;
1717 width -= default_border.left + default_border.right;
1718 height -= default_border.top + default_border.bottom;
1720 gtk_style_context_add_class (context, GTK_STYLE_CLASS_DEFAULT);
1722 else if (gtk_widget_get_can_default (widget))
1724 x += default_outside_border.left;
1725 y += default_outside_border.top;
1726 width -= default_outside_border.left + default_outside_border.right;
1727 height -= default_outside_border.top + default_outside_border.bottom;
1730 draw_focus = gtk_widget_has_visible_focus (widget);
1733 if (!interior_focus && draw_focus)
1735 x += focus_width + focus_pad;
1736 y += focus_width + focus_pad;
1737 width -= 2 * (focus_width + focus_pad);
1738 height -= 2 * (focus_width + focus_pad);
1741 if (priv->relief != GTK_RELIEF_NONE || priv->depressed ||
1742 state & GTK_STATE_FLAG_PRELIGHT)
1744 gtk_render_background (context, cr,
1745 x, y, width, height);
1746 gtk_render_frame (context, cr,
1747 x, y, width, height);
1752 gint child_displacement_x;
1753 gint child_displacement_y;
1754 gboolean displace_focus;
1757 gtk_style_context_get_style (context,
1758 "child-displacement-y", &child_displacement_y,
1759 "child-displacement-x", &child_displacement_x,
1760 "displace-focus", &displace_focus,
1762 gtk_style_context_get_border (context, state, &border);
1766 x += border.left + focus_pad;
1767 y += border.top + focus_pad;
1768 width -= (2 * focus_pad) + border.left + border.right;
1769 height -= (2 * focus_pad) + border.top + border.bottom;
1773 x -= focus_width + focus_pad;
1774 y -= focus_width + focus_pad;
1775 width += 2 * (focus_width + focus_pad);
1776 height += 2 * (focus_width + focus_pad);
1779 if (priv->depressed && displace_focus)
1781 x += child_displacement_x;
1782 y += child_displacement_y;
1785 gtk_render_focus (context, cr, x, y, width, height);
1788 gtk_style_context_restore (context);
1792 gtk_button_draw (GtkWidget *widget,
1795 GtkButton *button = GTK_BUTTON (widget);
1797 _gtk_button_paint (button, cr,
1798 gtk_widget_get_allocated_width (widget),
1799 gtk_widget_get_allocated_height (widget),
1800 gtk_widget_get_state_flags (widget));
1802 GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1808 gtk_button_button_press (GtkWidget *widget,
1809 GdkEventButton *event)
1812 GtkButtonPrivate *priv;
1814 if (event->type == GDK_BUTTON_PRESS)
1816 button = GTK_BUTTON (widget);
1817 priv = button->priv;
1819 if (priv->focus_on_click && !gtk_widget_has_focus (widget))
1820 gtk_widget_grab_focus (widget);
1822 if (event->button == GDK_BUTTON_PRIMARY)
1823 g_signal_emit (button, button_signals[PRESSED], 0);
1830 gtk_button_button_release (GtkWidget *widget,
1831 GdkEventButton *event)
1835 if (event->button == GDK_BUTTON_PRIMARY)
1837 button = GTK_BUTTON (widget);
1838 g_signal_emit (button, button_signals[RELEASED], 0);
1845 gtk_button_grab_broken (GtkWidget *widget,
1846 GdkEventGrabBroken *event)
1848 GtkButton *button = GTK_BUTTON (widget);
1849 GtkButtonPrivate *priv = button->priv;
1852 /* Simulate a button release without the pointer in the button */
1853 if (priv->button_down)
1855 save_in = priv->in_button;
1856 priv->in_button = FALSE;
1857 g_signal_emit (button, button_signals[RELEASED], 0);
1858 if (save_in != priv->in_button)
1860 priv->in_button = save_in;
1861 gtk_button_update_state (button);
1869 gtk_button_key_release (GtkWidget *widget,
1872 GtkButton *button = GTK_BUTTON (widget);
1873 GtkButtonPrivate *priv = button->priv;
1875 if (priv->activate_timeout)
1877 gtk_button_finish_activate (button, TRUE);
1880 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1881 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1887 gtk_button_enter_notify (GtkWidget *widget,
1888 GdkEventCrossing *event)
1890 GtkButton *button = GTK_BUTTON (widget);
1891 GtkButtonPrivate *priv = button->priv;
1893 if ((event->window == button->priv->event_window) &&
1894 (event->detail != GDK_NOTIFY_INFERIOR))
1896 priv->in_button = TRUE;
1897 g_signal_emit (button, button_signals[ENTER], 0);
1904 gtk_button_leave_notify (GtkWidget *widget,
1905 GdkEventCrossing *event)
1907 GtkButton *button = GTK_BUTTON (widget);
1908 GtkButtonPrivate *priv = button->priv;
1910 if ((event->window == button->priv->event_window) &&
1911 (event->detail != GDK_NOTIFY_INFERIOR) &&
1912 (gtk_widget_get_sensitive (widget)))
1914 priv->in_button = FALSE;
1915 g_signal_emit (button, button_signals[LEAVE], 0);
1922 gtk_real_button_pressed (GtkButton *button)
1924 GtkButtonPrivate *priv = button->priv;
1926 if (priv->activate_timeout)
1929 priv->button_down = TRUE;
1930 gtk_button_update_state (button);
1934 gtk_real_button_released (GtkButton *button)
1936 GtkButtonPrivate *priv = button->priv;
1938 if (priv->button_down)
1940 priv->button_down = FALSE;
1942 if (priv->activate_timeout)
1945 if (priv->in_button)
1946 gtk_button_clicked (button);
1948 gtk_button_update_state (button);
1953 gtk_real_button_clicked (GtkButton *button)
1955 GtkButtonPrivate *priv = button->priv;
1957 if (priv->action_observer)
1958 g_simple_action_observer_activate (priv->action_observer);
1961 gtk_action_activate (priv->action);
1965 button_activate_timeout (gpointer data)
1967 gtk_button_finish_activate (data, TRUE);
1973 gtk_real_button_activate (GtkButton *button)
1975 GtkWidget *widget = GTK_WIDGET (button);
1976 GtkButtonPrivate *priv = button->priv;
1980 device = gtk_get_current_event_device ();
1982 if (device && gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
1983 device = gdk_device_get_associated_device (device);
1985 if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
1987 time = gtk_get_current_event_time ();
1989 /* bgo#626336 - Only grab if we have a device (from an event), not if we
1990 * were activated programmatically when no event is available.
1992 if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1994 if (gdk_device_grab (device, priv->event_window,
1995 GDK_OWNERSHIP_WINDOW, TRUE,
1996 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1997 NULL, time) == GDK_GRAB_SUCCESS)
1999 gtk_device_grab_add (widget, device, TRUE);
2000 priv->grab_keyboard = device;
2001 priv->grab_time = time;
2005 priv->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
2006 button_activate_timeout,
2008 priv->button_down = TRUE;
2009 gtk_button_update_state (button);
2010 gtk_widget_queue_draw (GTK_WIDGET (button));
2015 gtk_button_finish_activate (GtkButton *button,
2018 GtkWidget *widget = GTK_WIDGET (button);
2019 GtkButtonPrivate *priv = button->priv;
2021 g_source_remove (priv->activate_timeout);
2022 priv->activate_timeout = 0;
2024 if (priv->grab_keyboard)
2026 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
2027 gtk_device_grab_remove (widget, priv->grab_keyboard);
2028 priv->grab_keyboard = NULL;
2031 priv->button_down = FALSE;
2033 gtk_button_update_state (button);
2034 gtk_widget_queue_draw (GTK_WIDGET (button));
2037 gtk_button_clicked (button);
2042 gtk_button_get_size (GtkWidget *widget,
2043 GtkOrientation orientation,
2047 GtkButton *button = GTK_BUTTON (widget);
2048 GtkStyleContext *context;
2050 GtkBorder default_border;
2055 gint minimum, natural;
2057 context = gtk_widget_get_style_context (widget);
2059 gtk_button_get_props (button, &default_border, NULL,
2060 &padding, &border, NULL);
2061 gtk_style_context_get_style (context,
2062 "focus-line-width", &focus_width,
2063 "focus-padding", &focus_pad,
2066 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2068 minimum = padding.left + padding.right +
2069 border.left + border.right;
2071 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
2072 minimum += default_border.left + default_border.right;
2076 minimum = padding.top + padding.bottom +
2077 border.top + border.bottom;
2079 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
2080 minimum += default_border.top + default_border.bottom;
2083 minimum += 2 * (focus_width + focus_pad);
2086 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
2087 gtk_widget_get_visible (child))
2089 gint child_min, child_nat;
2091 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2092 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
2094 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
2096 minimum += child_min;
2097 natural += child_nat;
2101 *minimum_size = minimum;
2104 *natural_size = natural;
2108 gtk_button_get_preferred_width (GtkWidget *widget,
2112 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2116 gtk_button_get_preferred_height (GtkWidget *widget,
2120 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2124 * gtk_button_set_label:
2125 * @button: a #GtkButton
2128 * Sets the text of the label of the button to @str. This text is
2129 * also used to select the stock item if gtk_button_set_use_stock()
2132 * This will also clear any previously set labels.
2135 gtk_button_set_label (GtkButton *button,
2138 GtkButtonPrivate *priv;
2141 g_return_if_fail (GTK_IS_BUTTON (button));
2143 priv = button->priv;
2145 new_label = g_strdup (label);
2146 g_free (priv->label_text);
2147 priv->label_text = new_label;
2149 gtk_button_construct_child (button);
2151 g_object_notify (G_OBJECT (button), "label");
2155 * gtk_button_get_label:
2156 * @button: a #GtkButton
2158 * Fetches the text from the label of the button, as set by
2159 * gtk_button_set_label(). If the label text has not
2160 * been set the return value will be %NULL. This will be the
2161 * case if you create an empty button with gtk_button_new() to
2162 * use as a container.
2164 * Return value: The text of the label widget. This string is owned
2165 * by the widget and must not be modified or freed.
2168 gtk_button_get_label (GtkButton *button)
2170 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2172 return button->priv->label_text;
2176 * gtk_button_set_use_underline:
2177 * @button: a #GtkButton
2178 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2180 * If true, an underline in the text of the button label indicates
2181 * the next character should be used for the mnemonic accelerator key.
2184 gtk_button_set_use_underline (GtkButton *button,
2185 gboolean use_underline)
2187 GtkButtonPrivate *priv;
2189 g_return_if_fail (GTK_IS_BUTTON (button));
2191 priv = button->priv;
2193 use_underline = use_underline != FALSE;
2195 if (use_underline != priv->use_underline)
2197 priv->use_underline = use_underline;
2199 gtk_button_construct_child (button);
2201 g_object_notify (G_OBJECT (button), "use-underline");
2206 * gtk_button_get_use_underline:
2207 * @button: a #GtkButton
2209 * Returns whether an embedded underline in the button label indicates a
2210 * mnemonic. See gtk_button_set_use_underline ().
2212 * Return value: %TRUE if an embedded underline in the button label
2213 * indicates the mnemonic accelerator keys.
2216 gtk_button_get_use_underline (GtkButton *button)
2218 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2220 return button->priv->use_underline;
2224 * gtk_button_set_use_stock:
2225 * @button: a #GtkButton
2226 * @use_stock: %TRUE if the button should use a stock item
2228 * If %TRUE, the label set on the button is used as a
2229 * stock id to select the stock item for the button.
2232 gtk_button_set_use_stock (GtkButton *button,
2235 GtkButtonPrivate *priv;
2237 g_return_if_fail (GTK_IS_BUTTON (button));
2239 priv = button->priv;
2241 use_stock = use_stock != FALSE;
2243 if (use_stock != priv->use_stock)
2245 priv->use_stock = use_stock;
2247 gtk_button_construct_child (button);
2249 g_object_notify (G_OBJECT (button), "use-stock");
2254 * gtk_button_get_use_stock:
2255 * @button: a #GtkButton
2257 * Returns whether the button label is a stock item.
2259 * Return value: %TRUE if the button label is used to
2260 * select a stock item instead of being
2261 * used directly as the label text.
2264 gtk_button_get_use_stock (GtkButton *button)
2266 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2268 return button->priv->use_stock;
2272 * gtk_button_set_focus_on_click:
2273 * @button: a #GtkButton
2274 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2276 * Sets whether the button will grab focus when it is clicked with the mouse.
2277 * Making mouse clicks not grab focus is useful in places like toolbars where
2278 * you don't want the keyboard focus removed from the main area of the
2284 gtk_button_set_focus_on_click (GtkButton *button,
2285 gboolean focus_on_click)
2287 GtkButtonPrivate *priv;
2289 g_return_if_fail (GTK_IS_BUTTON (button));
2291 priv = button->priv;
2293 focus_on_click = focus_on_click != FALSE;
2295 if (priv->focus_on_click != focus_on_click)
2297 priv->focus_on_click = focus_on_click;
2299 g_object_notify (G_OBJECT (button), "focus-on-click");
2304 * gtk_button_get_focus_on_click:
2305 * @button: a #GtkButton
2307 * Returns whether the button grabs focus when it is clicked with the mouse.
2308 * See gtk_button_set_focus_on_click().
2310 * Return value: %TRUE if the button grabs focus when it is clicked with
2316 gtk_button_get_focus_on_click (GtkButton *button)
2318 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2320 return button->priv->focus_on_click;
2324 * gtk_button_set_alignment:
2325 * @button: a #GtkButton
2326 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2327 * 1.0 is right aligned
2328 * @yalign: the vertical position of the child, 0.0 is top aligned,
2329 * 1.0 is bottom aligned
2331 * Sets the alignment of the child. This property has no effect unless
2332 * the child is a #GtkMisc or a #GtkAlignment.
2337 gtk_button_set_alignment (GtkButton *button,
2341 GtkButtonPrivate *priv;
2343 g_return_if_fail (GTK_IS_BUTTON (button));
2345 priv = button->priv;
2347 priv->xalign = xalign;
2348 priv->yalign = yalign;
2349 priv->align_set = 1;
2351 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2353 g_object_freeze_notify (G_OBJECT (button));
2354 g_object_notify (G_OBJECT (button), "xalign");
2355 g_object_notify (G_OBJECT (button), "yalign");
2356 g_object_thaw_notify (G_OBJECT (button));
2360 * gtk_button_get_alignment:
2361 * @button: a #GtkButton
2362 * @xalign: (out): return location for horizontal alignment
2363 * @yalign: (out): return location for vertical alignment
2365 * Gets the alignment of the child in the button.
2370 gtk_button_get_alignment (GtkButton *button,
2374 GtkButtonPrivate *priv;
2376 g_return_if_fail (GTK_IS_BUTTON (button));
2378 priv = button->priv;
2381 *xalign = priv->xalign;
2384 *yalign = priv->yalign;
2388 * _gtk_button_set_depressed:
2389 * @button: a #GtkButton
2390 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2392 * Sets whether the button is currently drawn as down or not. This is
2393 * purely a visual setting, and is meant only for use by derived widgets
2394 * such as #GtkToggleButton.
2397 _gtk_button_set_depressed (GtkButton *button,
2400 GtkWidget *widget = GTK_WIDGET (button);
2401 GtkButtonPrivate *priv = button->priv;
2403 depressed = depressed != FALSE;
2405 if (depressed != priv->depressed)
2407 priv->depressed = depressed;
2408 gtk_widget_queue_resize (widget);
2413 gtk_button_update_state (GtkButton *button)
2415 GtkButtonPrivate *priv = button->priv;
2416 GtkStateFlags new_state;
2419 if (priv->activate_timeout)
2420 depressed = priv->depress_on_activate;
2422 depressed = priv->in_button && priv->button_down;
2424 new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) &
2425 ~(GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_ACTIVE);
2427 if (priv->in_button)
2428 new_state |= GTK_STATE_FLAG_PRELIGHT;
2431 new_state |= GTK_STATE_FLAG_ACTIVE;
2433 _gtk_button_set_depressed (button, depressed);
2434 gtk_widget_set_state_flags (GTK_WIDGET (button), new_state, TRUE);
2438 show_image_change_notify (GtkButton *button)
2440 GtkButtonPrivate *priv = button->priv;
2444 if (show_image (button))
2445 gtk_widget_show (priv->image);
2447 gtk_widget_hide (priv->image);
2452 traverse_container (GtkWidget *widget,
2455 if (GTK_IS_BUTTON (widget))
2456 show_image_change_notify (GTK_BUTTON (widget));
2457 else if (GTK_IS_CONTAINER (widget))
2458 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2462 gtk_button_setting_changed (GtkSettings *settings)
2466 list = gtk_window_list_toplevels ();
2468 for (l = list; l; l = l->next)
2469 gtk_container_forall (GTK_CONTAINER (l->data),
2470 traverse_container, NULL);
2477 gtk_button_screen_changed (GtkWidget *widget,
2478 GdkScreen *previous_screen)
2481 GtkButtonPrivate *priv;
2482 GtkSettings *settings;
2483 gulong show_image_connection;
2485 if (!gtk_widget_has_screen (widget))
2488 button = GTK_BUTTON (widget);
2489 priv = button->priv;
2491 /* If the button is being pressed while the screen changes the
2492 release might never occur, so we reset the state. */
2493 if (priv->button_down)
2495 priv->button_down = FALSE;
2496 gtk_button_update_state (button);
2499 settings = gtk_widget_get_settings (widget);
2501 show_image_connection =
2502 g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2503 NULL, gtk_button_setting_changed, NULL);
2505 if (show_image_connection)
2508 g_signal_connect (settings, "notify::gtk-button-images",
2509 G_CALLBACK (gtk_button_setting_changed), NULL);
2511 show_image_change_notify (button);
2515 gtk_button_state_changed (GtkWidget *widget,
2516 GtkStateType previous_state)
2518 GtkButton *button = GTK_BUTTON (widget);
2519 GtkButtonPrivate *priv = button->priv;
2521 if (!gtk_widget_is_sensitive (widget))
2523 priv->in_button = FALSE;
2524 gtk_real_button_released (button);
2529 gtk_button_grab_notify (GtkWidget *widget,
2530 gboolean was_grabbed)
2532 GtkButton *button = GTK_BUTTON (widget);
2533 GtkButtonPrivate *priv = button->priv;
2536 if (priv->activate_timeout &&
2537 priv->grab_keyboard &&
2538 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2539 gtk_button_finish_activate (button, FALSE);
2543 save_in = priv->in_button;
2544 priv->in_button = FALSE;
2545 gtk_real_button_released (button);
2546 if (save_in != priv->in_button)
2548 priv->in_button = save_in;
2549 gtk_button_update_state (button);
2555 gtk_button_hierarchy_changed (GtkWidget *widget,
2556 GtkWidget *previous_toplevel)
2558 GtkButton *button = GTK_BUTTON (widget);
2559 GtkWidgetClass *parent_class;
2561 parent_class = GTK_WIDGET_CLASS (gtk_button_parent_class);
2562 if (parent_class->hierarchy_changed)
2563 parent_class->hierarchy_changed (widget, previous_toplevel);
2565 if (button->priv->action_name)
2567 GtkWidget *toplevel;
2569 toplevel = gtk_widget_get_toplevel (widget);
2571 if (toplevel != previous_toplevel)
2572 gtk_button_update_action_observer (button);
2577 * gtk_button_set_image:
2578 * @button: a #GtkButton
2579 * @image: a widget to set as the image for the button
2581 * Set the image of @button to the given widget. Note that
2582 * it depends on the #GtkSettings:gtk-button-images setting whether the
2583 * image will be displayed or not, you don't have to call
2584 * gtk_widget_show() on @image yourself.
2589 gtk_button_set_image (GtkButton *button,
2592 GtkButtonPrivate *priv;
2595 g_return_if_fail (GTK_IS_BUTTON (button));
2596 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2598 priv = button->priv;
2602 parent = gtk_widget_get_parent (priv->image);
2604 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2607 priv->image = image;
2608 priv->image_is_stock = (image == NULL);
2610 gtk_button_construct_child (button);
2612 g_object_notify (G_OBJECT (button), "image");
2616 * gtk_button_get_image:
2617 * @button: a #GtkButton
2619 * Gets the widget that is currenty set as the image of @button.
2620 * This may have been explicitly set by gtk_button_set_image()
2621 * or constructed by gtk_button_new_from_stock().
2623 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2628 gtk_button_get_image (GtkButton *button)
2630 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2632 return button->priv->image;
2636 * gtk_button_set_image_position:
2637 * @button: a #GtkButton
2638 * @position: the position
2640 * Sets the position of the image relative to the text
2641 * inside the button.
2646 gtk_button_set_image_position (GtkButton *button,
2647 GtkPositionType position)
2649 GtkButtonPrivate *priv;
2651 g_return_if_fail (GTK_IS_BUTTON (button));
2652 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2654 priv = button->priv;
2656 if (priv->image_position != position)
2658 priv->image_position = position;
2660 gtk_button_construct_child (button);
2662 g_object_notify (G_OBJECT (button), "image-position");
2667 * gtk_button_get_image_position:
2668 * @button: a #GtkButton
2670 * Gets the position of the image relative to the text
2671 * inside the button.
2673 * Return value: the position
2678 gtk_button_get_image_position (GtkButton *button)
2680 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2682 return button->priv->image_position;
2686 * gtk_button_get_event_window:
2687 * @button: a #GtkButton
2689 * Returns the button's event window if it is realized, %NULL otherwise.
2690 * This function should be rarely needed.
2692 * Return value: (transfer none): @button's event window.
2697 gtk_button_get_event_window (GtkButton *button)
2699 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2701 return button->priv->event_window;