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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2001. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 * @Short_description: A widget that creates a signal when clicked on
32 * The #GtkButton widget is generally used to attach a function to that is
33 * called when the button is pressed. The various signals and how to use them
36 * The #GtkButton widget can hold any valid child widget. That is it can hold
37 * most any other standard #GtkWidget. The most commonly used child is the
43 #include "gtkalignment.h"
44 #include "gtkbutton.h"
47 #include "gtkmarshalers.h"
52 #include "gtkiconfactory.h"
53 #include "gtkactivatable.h"
54 #include "gtksizerequest.h"
55 #include "gtkprivate.h"
59 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
60 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
61 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
63 /* Time out before giving up on getting a key release when animating
66 #define ACTIVATE_TIMEOUT 250
90 /* activatable properties */
91 PROP_ACTIVATABLE_RELATED_ACTION,
92 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
95 #define GTK_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_BUTTON, GtkButtonPrivate))
96 typedef struct _GtkButtonPrivate GtkButtonPrivate;
98 struct _GtkButtonPrivate
104 guint image_is_stock : 1;
105 guint use_action_appearance : 1;
107 GdkDevice *grab_keyboard;
108 GtkPositionType image_position;
112 static void gtk_button_destroy (GtkWidget *widget);
113 static void gtk_button_dispose (GObject *object);
114 static void gtk_button_set_property (GObject *object,
118 static void gtk_button_get_property (GObject *object,
122 static void gtk_button_screen_changed (GtkWidget *widget,
123 GdkScreen *previous_screen);
124 static void gtk_button_realize (GtkWidget * widget);
125 static void gtk_button_unrealize (GtkWidget * widget);
126 static void gtk_button_map (GtkWidget * widget);
127 static void gtk_button_unmap (GtkWidget * widget);
128 static void gtk_button_style_set (GtkWidget * widget, GtkStyle * prev_style);
129 static void gtk_button_size_allocate (GtkWidget * widget,
130 GtkAllocation * allocation);
131 static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
132 static gint gtk_button_button_press (GtkWidget * widget,
133 GdkEventButton * event);
134 static gint gtk_button_button_release (GtkWidget * widget,
135 GdkEventButton * event);
136 static gint gtk_button_grab_broken (GtkWidget * widget,
137 GdkEventGrabBroken * event);
138 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
139 static gint gtk_button_enter_notify (GtkWidget * widget,
140 GdkEventCrossing * event);
141 static gint gtk_button_leave_notify (GtkWidget * widget,
142 GdkEventCrossing * event);
143 static void gtk_real_button_pressed (GtkButton * button);
144 static void gtk_real_button_released (GtkButton * button);
145 static void gtk_real_button_clicked (GtkButton * button);
146 static void gtk_real_button_activate (GtkButton *button);
147 static void gtk_button_update_state (GtkButton *button);
148 static void gtk_button_add (GtkContainer *container,
150 static GType gtk_button_child_type (GtkContainer *container);
151 static void gtk_button_finish_activate (GtkButton *button,
154 static GObject* gtk_button_constructor (GType type,
155 guint n_construct_properties,
156 GObjectConstructParam *construct_params);
157 static void gtk_button_construct_child (GtkButton *button);
158 static void gtk_button_state_changed (GtkWidget *widget,
159 GtkStateType previous_state);
160 static void gtk_button_grab_notify (GtkWidget *widget,
161 gboolean was_grabbed);
164 static void gtk_button_activatable_interface_init(GtkActivatableIface *iface);
165 static void gtk_button_update (GtkActivatable *activatable,
167 const gchar *property_name);
168 static void gtk_button_sync_action_properties (GtkActivatable *activatable,
170 static void gtk_button_set_related_action (GtkButton *button,
172 static void gtk_button_set_use_action_appearance (GtkButton *button,
173 gboolean use_appearance);
175 static void gtk_button_get_preferred_width (GtkWidget *widget,
178 static void gtk_button_get_preferred_height (GtkWidget *widget,
182 static guint button_signals[LAST_SIGNAL] = { 0 };
184 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
185 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
186 gtk_button_activatable_interface_init))
189 gtk_button_class_init (GtkButtonClass *klass)
191 GObjectClass *gobject_class;
192 GtkWidgetClass *widget_class;
193 GtkContainerClass *container_class;
195 gobject_class = G_OBJECT_CLASS (klass);
196 widget_class = (GtkWidgetClass*) klass;
197 container_class = (GtkContainerClass*) klass;
199 gobject_class->constructor = gtk_button_constructor;
200 gobject_class->dispose = gtk_button_dispose;
201 gobject_class->set_property = gtk_button_set_property;
202 gobject_class->get_property = gtk_button_get_property;
204 widget_class->get_preferred_width = gtk_button_get_preferred_width;
205 widget_class->get_preferred_height = gtk_button_get_preferred_height;
206 widget_class->destroy = gtk_button_destroy;
207 widget_class->screen_changed = gtk_button_screen_changed;
208 widget_class->realize = gtk_button_realize;
209 widget_class->unrealize = gtk_button_unrealize;
210 widget_class->map = gtk_button_map;
211 widget_class->unmap = gtk_button_unmap;
212 widget_class->style_set = gtk_button_style_set;
213 widget_class->size_allocate = gtk_button_size_allocate;
214 widget_class->draw = gtk_button_draw;
215 widget_class->button_press_event = gtk_button_button_press;
216 widget_class->button_release_event = gtk_button_button_release;
217 widget_class->grab_broken_event = gtk_button_grab_broken;
218 widget_class->key_release_event = gtk_button_key_release;
219 widget_class->enter_notify_event = gtk_button_enter_notify;
220 widget_class->leave_notify_event = gtk_button_leave_notify;
221 widget_class->state_changed = gtk_button_state_changed;
222 widget_class->grab_notify = gtk_button_grab_notify;
224 container_class->child_type = gtk_button_child_type;
225 container_class->add = gtk_button_add;
226 gtk_container_class_handle_border_width (container_class);
228 klass->pressed = gtk_real_button_pressed;
229 klass->released = gtk_real_button_released;
230 klass->clicked = NULL;
231 klass->enter = gtk_button_update_state;
232 klass->leave = gtk_button_update_state;
233 klass->activate = gtk_real_button_activate;
235 g_object_class_install_property (gobject_class,
237 g_param_spec_string ("label",
239 P_("Text of the label widget inside the button, if the button contains a label widget"),
241 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
243 g_object_class_install_property (gobject_class,
245 g_param_spec_boolean ("use-underline",
247 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
249 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
251 g_object_class_install_property (gobject_class,
253 g_param_spec_boolean ("use-stock",
255 P_("If set, the label is used to pick a stock item instead of being displayed"),
257 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
259 g_object_class_install_property (gobject_class,
261 g_param_spec_boolean ("focus-on-click",
262 P_("Focus on click"),
263 P_("Whether the button grabs focus when it is clicked with the mouse"),
265 GTK_PARAM_READWRITE));
267 g_object_class_install_property (gobject_class,
269 g_param_spec_enum ("relief",
271 P_("The border relief style"),
272 GTK_TYPE_RELIEF_STYLE,
274 GTK_PARAM_READWRITE));
279 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
280 * can be used to control it's horizontal alignment. 0.0 is left aligned,
281 * 1.0 is right aligned.
285 g_object_class_install_property (gobject_class,
287 g_param_spec_float("xalign",
288 P_("Horizontal alignment for child"),
289 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
293 GTK_PARAM_READWRITE));
298 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
299 * can be used to control it's vertical alignment. 0.0 is top aligned,
300 * 1.0 is bottom aligned.
304 g_object_class_install_property (gobject_class,
306 g_param_spec_float("yalign",
307 P_("Vertical alignment for child"),
308 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
312 GTK_PARAM_READWRITE));
317 * The child widget to appear next to the button text.
321 g_object_class_install_property (gobject_class,
323 g_param_spec_object ("image",
325 P_("Child widget to appear next to the button text"),
327 GTK_PARAM_READWRITE));
330 * GtkButton:image-position:
332 * The position of the image relative to the text inside the button.
336 g_object_class_install_property (gobject_class,
338 g_param_spec_enum ("image-position",
339 P_("Image position"),
340 P_("The position of the image relative to the text"),
341 GTK_TYPE_POSITION_TYPE,
343 GTK_PARAM_READWRITE));
345 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
346 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
349 * GtkButton::pressed:
350 * @button: the object that received the signal
352 * Emitted when the button is pressed.
354 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
356 button_signals[PRESSED] =
357 g_signal_new (I_("pressed"),
358 G_OBJECT_CLASS_TYPE (gobject_class),
360 G_STRUCT_OFFSET (GtkButtonClass, pressed),
362 _gtk_marshal_VOID__VOID,
366 * GtkButton::released:
367 * @button: the object that received the signal
369 * Emitted when the button is released.
371 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
373 button_signals[RELEASED] =
374 g_signal_new (I_("released"),
375 G_OBJECT_CLASS_TYPE (gobject_class),
377 G_STRUCT_OFFSET (GtkButtonClass, released),
379 _gtk_marshal_VOID__VOID,
383 * GtkButton::clicked:
384 * @button: the object that received the signal
386 * Emitted when the button has been activated (pressed and released).
388 button_signals[CLICKED] =
389 g_signal_new (I_("clicked"),
390 G_OBJECT_CLASS_TYPE (gobject_class),
391 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
392 G_STRUCT_OFFSET (GtkButtonClass, clicked),
394 _gtk_marshal_VOID__VOID,
399 * @button: the object that received the signal
401 * Emitted when the pointer enters the button.
403 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
405 button_signals[ENTER] =
406 g_signal_new (I_("enter"),
407 G_OBJECT_CLASS_TYPE (gobject_class),
409 G_STRUCT_OFFSET (GtkButtonClass, enter),
411 _gtk_marshal_VOID__VOID,
416 * @button: the object that received the signal
418 * Emitted when the pointer leaves the button.
420 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
422 button_signals[LEAVE] =
423 g_signal_new (I_("leave"),
424 G_OBJECT_CLASS_TYPE (gobject_class),
426 G_STRUCT_OFFSET (GtkButtonClass, leave),
428 _gtk_marshal_VOID__VOID,
432 * GtkButton::activate:
433 * @widget: the object which received the signal.
435 * The ::activate signal on GtkButton is an action signal and
436 * emitting it causes the button to animate press then release.
437 * Applications should never connect to this signal, but use the
438 * #GtkButton::clicked signal.
440 button_signals[ACTIVATE] =
441 g_signal_new (I_("activate"),
442 G_OBJECT_CLASS_TYPE (gobject_class),
443 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
444 G_STRUCT_OFFSET (GtkButtonClass, activate),
446 _gtk_marshal_VOID__VOID,
448 widget_class->activate_signal = button_signals[ACTIVATE];
451 * GtkButton:default-border:
453 * The "default-border" style property defines the extra space to add
454 * around a button that can become the default widget of its window.
455 * For more information about default widgets, see gtk_widget_grab_default().
458 gtk_widget_class_install_style_property (widget_class,
459 g_param_spec_boxed ("default-border",
460 P_("Default Spacing"),
461 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
463 GTK_PARAM_READABLE));
466 * GtkButton:default-outside-border:
468 * The "default-outside-border" style property defines the extra outside
469 * space to add around a button that can become the default widget of its
470 * window. Extra outside space is always drawn outside the button border.
471 * For more information about default widgets, see gtk_widget_grab_default().
473 gtk_widget_class_install_style_property (widget_class,
474 g_param_spec_boxed ("default-outside-border",
475 P_("Default Outside Spacing"),
476 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
478 GTK_PARAM_READABLE));
479 gtk_widget_class_install_style_property (widget_class,
480 g_param_spec_int ("child-displacement-x",
481 P_("Child X Displacement"),
482 P_("How far in the x direction to move the child when the button is depressed"),
486 GTK_PARAM_READABLE));
487 gtk_widget_class_install_style_property (widget_class,
488 g_param_spec_int ("child-displacement-y",
489 P_("Child Y Displacement"),
490 P_("How far in the y direction to move the child when the button is depressed"),
494 GTK_PARAM_READABLE));
497 * GtkButton:displace-focus:
499 * Whether the child_displacement_x/child_displacement_y properties
500 * should also affect the focus rectangle.
504 gtk_widget_class_install_style_property (widget_class,
505 g_param_spec_boolean ("displace-focus",
506 P_("Displace focus"),
507 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
509 GTK_PARAM_READABLE));
512 * GtkButton:inner-border:
514 * Sets the border between the button edges and child.
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));
545 gtk_button_init (GtkButton *button)
547 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
549 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
550 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
551 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
553 button->label_text = NULL;
555 button->constructed = FALSE;
556 button->in_button = FALSE;
557 button->button_down = FALSE;
558 button->relief = GTK_RELIEF_NORMAL;
559 button->use_stock = FALSE;
560 button->use_underline = FALSE;
561 button->depressed = FALSE;
562 button->depress_on_activate = TRUE;
563 button->focus_on_click = TRUE;
568 priv->image_is_stock = TRUE;
569 priv->image_position = GTK_POS_LEFT;
570 priv->use_action_appearance = TRUE;
574 gtk_button_destroy (GtkWidget *widget)
576 GtkButton *button = GTK_BUTTON (widget);
578 if (button->label_text)
580 g_free (button->label_text);
581 button->label_text = NULL;
584 GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
588 gtk_button_constructor (GType type,
589 guint n_construct_properties,
590 GObjectConstructParam *construct_params)
595 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
596 n_construct_properties,
599 button = GTK_BUTTON (object);
600 button->constructed = TRUE;
602 if (button->label_text != NULL)
603 gtk_button_construct_child (button);
610 gtk_button_child_type (GtkContainer *container)
612 if (!gtk_bin_get_child (GTK_BIN (container)))
613 return GTK_TYPE_WIDGET;
619 maybe_set_alignment (GtkButton *button,
622 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
624 if (GTK_IS_MISC (widget))
626 GtkMisc *misc = GTK_MISC (widget);
629 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
631 else if (GTK_IS_ALIGNMENT (widget))
633 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
634 gfloat xscale, yscale;
636 g_object_get (alignment,
642 gtk_alignment_set (alignment,
643 priv->xalign, priv->yalign,
649 gtk_button_add (GtkContainer *container,
652 maybe_set_alignment (GTK_BUTTON (container), widget);
654 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
658 gtk_button_dispose (GObject *object)
660 GtkButton *button = GTK_BUTTON (object);
661 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
665 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
668 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
672 gtk_button_set_property (GObject *object,
677 GtkButton *button = GTK_BUTTON (object);
678 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
683 gtk_button_set_label (button, g_value_get_string (value));
686 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
689 gtk_button_set_relief (button, g_value_get_enum (value));
691 case PROP_USE_UNDERLINE:
692 gtk_button_set_use_underline (button, g_value_get_boolean (value));
695 gtk_button_set_use_stock (button, g_value_get_boolean (value));
697 case PROP_FOCUS_ON_CLICK:
698 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
701 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
704 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
706 case PROP_IMAGE_POSITION:
707 gtk_button_set_image_position (button, g_value_get_enum (value));
709 case PROP_ACTIVATABLE_RELATED_ACTION:
710 gtk_button_set_related_action (button, g_value_get_object (value));
712 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
713 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
716 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
722 gtk_button_get_property (GObject *object,
727 GtkButton *button = GTK_BUTTON (object);
728 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
733 g_value_set_string (value, button->label_text);
736 g_value_set_object (value, (GObject *)priv->image);
739 g_value_set_enum (value, gtk_button_get_relief (button));
741 case PROP_USE_UNDERLINE:
742 g_value_set_boolean (value, button->use_underline);
745 g_value_set_boolean (value, button->use_stock);
747 case PROP_FOCUS_ON_CLICK:
748 g_value_set_boolean (value, button->focus_on_click);
751 g_value_set_float (value, priv->xalign);
754 g_value_set_float (value, priv->yalign);
756 case PROP_IMAGE_POSITION:
757 g_value_set_enum (value, priv->image_position);
759 case PROP_ACTIVATABLE_RELATED_ACTION:
760 g_value_set_object (value, priv->action);
762 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
763 g_value_set_boolean (value, priv->use_action_appearance);
766 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
772 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
774 iface->update = gtk_button_update;
775 iface->sync_action_properties = gtk_button_sync_action_properties;
779 activatable_update_stock_id (GtkButton *button,
782 if (!gtk_button_get_use_stock (button))
785 gtk_button_set_label (button, gtk_action_get_stock_id (action));
789 activatable_update_short_label (GtkButton *button,
795 if (gtk_button_get_use_stock (button))
798 image = gtk_button_get_image (button);
800 /* Dont touch custom child... */
801 child = gtk_bin_get_child (GTK_BIN (button));
802 if (GTK_IS_IMAGE (image) ||
804 GTK_IS_LABEL (child))
806 gtk_button_set_label (button, gtk_action_get_short_label (action));
807 gtk_button_set_use_underline (button, TRUE);
812 activatable_update_icon_name (GtkButton *button,
817 if (gtk_button_get_use_stock (button))
820 image = gtk_button_get_image (button);
822 if (GTK_IS_IMAGE (image) &&
823 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
824 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
825 gtk_image_set_from_icon_name (GTK_IMAGE (image),
826 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
830 activatable_update_gicon (GtkButton *button,
833 GtkWidget *image = gtk_button_get_image (button);
834 GIcon *icon = gtk_action_get_gicon (action);
836 if (GTK_IS_IMAGE (image) &&
837 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
838 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
839 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
843 gtk_button_update (GtkActivatable *activatable,
845 const gchar *property_name)
847 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
849 if (strcmp (property_name, "visible") == 0)
851 if (gtk_action_is_visible (action))
852 gtk_widget_show (GTK_WIDGET (activatable));
854 gtk_widget_hide (GTK_WIDGET (activatable));
856 else if (strcmp (property_name, "sensitive") == 0)
857 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
859 if (!priv->use_action_appearance)
862 if (strcmp (property_name, "stock-id") == 0)
863 activatable_update_stock_id (GTK_BUTTON (activatable), action);
864 else if (strcmp (property_name, "gicon") == 0)
865 activatable_update_gicon (GTK_BUTTON (activatable), action);
866 else if (strcmp (property_name, "short-label") == 0)
867 activatable_update_short_label (GTK_BUTTON (activatable), action);
868 else if (strcmp (property_name, "icon-name") == 0)
869 activatable_update_icon_name (GTK_BUTTON (activatable), action);
873 gtk_button_sync_action_properties (GtkActivatable *activatable,
876 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
881 if (gtk_action_is_visible (action))
882 gtk_widget_show (GTK_WIDGET (activatable));
884 gtk_widget_hide (GTK_WIDGET (activatable));
886 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
888 if (priv->use_action_appearance)
890 activatable_update_stock_id (GTK_BUTTON (activatable), action);
891 activatable_update_short_label (GTK_BUTTON (activatable), action);
892 activatable_update_gicon (GTK_BUTTON (activatable), action);
893 activatable_update_icon_name (GTK_BUTTON (activatable), action);
898 gtk_button_set_related_action (GtkButton *button,
901 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
903 if (priv->action == action)
906 /* This should be a default handler, but for compatibility reasons
907 * we need to support derived classes that don't chain up their
910 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
912 g_signal_connect_after (button, "clicked",
913 G_CALLBACK (gtk_real_button_clicked), NULL);
915 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
917 priv->action = action;
921 gtk_button_set_use_action_appearance (GtkButton *button,
922 gboolean use_appearance)
924 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
926 if (priv->use_action_appearance != use_appearance)
928 priv->use_action_appearance = use_appearance;
930 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
937 * Creates a new #GtkButton widget. To add a child widget to the button,
938 * use gtk_container_add().
940 * Returns: The newly created #GtkButton widget.
943 gtk_button_new (void)
945 return g_object_new (GTK_TYPE_BUTTON, NULL);
949 show_image (GtkButton *button)
953 if (button->label_text)
955 GtkSettings *settings;
957 settings = gtk_widget_get_settings (GTK_WIDGET (button));
958 g_object_get (settings, "gtk-button-images", &show, NULL);
967 gtk_button_construct_child (GtkButton *button)
969 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
975 GtkWidget *image = NULL;
976 gchar *label_text = NULL;
979 if (!button->constructed)
982 if (!button->label_text && !priv->image)
985 gtk_widget_style_get (GTK_WIDGET (button),
986 "image-spacing", &image_spacing,
989 if (priv->image && !priv->image_is_stock)
993 image = g_object_ref (priv->image);
995 parent = gtk_widget_get_parent (image);
997 gtk_container_remove (GTK_CONTAINER (parent), image);
1002 child = gtk_bin_get_child (GTK_BIN (button));
1004 gtk_container_remove (GTK_CONTAINER (button), child);
1006 if (button->use_stock &&
1007 button->label_text &&
1008 gtk_stock_lookup (button->label_text, &item))
1011 image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
1013 label_text = item.label;
1016 label_text = button->label_text;
1020 priv->image = image;
1021 g_object_set (priv->image,
1022 "visible", show_image (button),
1023 "no-show-all", TRUE,
1026 if (priv->image_position == GTK_POS_LEFT ||
1027 priv->image_position == GTK_POS_RIGHT)
1028 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1030 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1032 if (priv->align_set)
1033 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1035 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1037 if (priv->image_position == GTK_POS_LEFT ||
1038 priv->image_position == GTK_POS_TOP)
1039 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1041 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1045 if (button->use_underline || button->use_stock)
1047 label = gtk_label_new_with_mnemonic (label_text);
1048 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1049 GTK_WIDGET (button));
1052 label = gtk_label_new (label_text);
1054 if (priv->image_position == GTK_POS_RIGHT ||
1055 priv->image_position == GTK_POS_BOTTOM)
1056 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1058 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1061 gtk_container_add (GTK_CONTAINER (button), align);
1062 gtk_container_add (GTK_CONTAINER (align), box);
1063 gtk_widget_show_all (align);
1065 g_object_unref (image);
1070 if (button->use_underline || button->use_stock)
1072 label = gtk_label_new_with_mnemonic (button->label_text);
1073 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1076 label = gtk_label_new (button->label_text);
1078 if (priv->align_set)
1079 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1081 gtk_widget_show (label);
1082 gtk_container_add (GTK_CONTAINER (button), label);
1087 * gtk_button_new_with_label:
1088 * @label: The text you want the #GtkLabel to hold.
1090 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1093 * Returns: The newly created #GtkButton widget.
1096 gtk_button_new_with_label (const gchar *label)
1098 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1102 * gtk_button_new_from_stock:
1103 * @stock_id: the name of the stock item
1105 * Creates a new #GtkButton containing the image and text from a stock item.
1106 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1109 * If @stock_id is unknown, then it will be treated as a mnemonic
1110 * label (as for gtk_button_new_with_mnemonic()).
1112 * Returns: a new #GtkButton
1115 gtk_button_new_from_stock (const gchar *stock_id)
1117 return g_object_new (GTK_TYPE_BUTTON,
1120 "use-underline", TRUE,
1125 * gtk_button_new_with_mnemonic:
1126 * @label: The text of the button, with an underscore in front of the
1127 * mnemonic character
1128 * @returns: a new #GtkButton
1130 * Creates a new #GtkButton containing a label.
1131 * If characters in @label are preceded by an underscore, they are underlined.
1132 * If you need a literal underscore character in a label, use '__' (two
1133 * underscores). The first underlined character represents a keyboard
1134 * accelerator called a mnemonic.
1135 * Pressing Alt and that key activates the button.
1138 gtk_button_new_with_mnemonic (const gchar *label)
1140 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1144 * gtk_button_pressed:
1145 * @button: The #GtkButton you want to send the signal to.
1147 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1149 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1152 gtk_button_pressed (GtkButton *button)
1154 g_return_if_fail (GTK_IS_BUTTON (button));
1157 g_signal_emit (button, button_signals[PRESSED], 0);
1161 * gtk_button_released:
1162 * @button: The #GtkButton you want to send the signal to.
1164 * Emits a #GtkButton::released signal to the given #GtkButton.
1166 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1169 gtk_button_released (GtkButton *button)
1171 g_return_if_fail (GTK_IS_BUTTON (button));
1173 g_signal_emit (button, button_signals[RELEASED], 0);
1177 * gtk_button_clicked:
1178 * @button: The #GtkButton you want to send the signal to.
1180 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1183 gtk_button_clicked (GtkButton *button)
1185 g_return_if_fail (GTK_IS_BUTTON (button));
1187 g_signal_emit (button, button_signals[CLICKED], 0);
1192 * @button: The #GtkButton you want to send the signal to.
1194 * Emits a #GtkButton::enter signal to the given #GtkButton.
1196 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1199 gtk_button_enter (GtkButton *button)
1201 g_return_if_fail (GTK_IS_BUTTON (button));
1203 g_signal_emit (button, button_signals[ENTER], 0);
1208 * @button: The #GtkButton you want to send the signal to.
1210 * Emits a #GtkButton::leave signal to the given #GtkButton.
1212 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1215 gtk_button_leave (GtkButton *button)
1217 g_return_if_fail (GTK_IS_BUTTON (button));
1219 g_signal_emit (button, button_signals[LEAVE], 0);
1223 * gtk_button_set_relief:
1224 * @button: The #GtkButton you want to set relief styles of.
1225 * @newstyle: The GtkReliefStyle as described above.
1227 * Sets the relief style of the edges of the given #GtkButton widget.
1228 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1229 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1231 * <!-- FIXME: put pictures of each style -->
1234 gtk_button_set_relief (GtkButton *button,
1235 GtkReliefStyle newrelief)
1237 g_return_if_fail (GTK_IS_BUTTON (button));
1239 if (newrelief != button->relief)
1241 button->relief = newrelief;
1242 g_object_notify (G_OBJECT (button), "relief");
1243 gtk_widget_queue_draw (GTK_WIDGET (button));
1248 * gtk_button_get_relief:
1249 * @button: The #GtkButton you want the #GtkReliefStyle from.
1251 * Returns the current relief style of the given #GtkButton.
1253 * Returns: The current #GtkReliefStyle
1256 gtk_button_get_relief (GtkButton *button)
1258 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1260 return button->relief;
1264 gtk_button_realize (GtkWidget *widget)
1266 GtkButton *button = GTK_BUTTON (widget);
1267 GtkAllocation allocation;
1269 GdkWindowAttr attributes;
1270 gint attributes_mask;
1272 gtk_widget_get_allocation (widget, &allocation);
1274 gtk_widget_set_realized (widget, TRUE);
1276 attributes.window_type = GDK_WINDOW_CHILD;
1277 attributes.x = allocation.x;
1278 attributes.y = allocation.y;
1279 attributes.width = allocation.width;
1280 attributes.height = allocation.height;
1281 attributes.wclass = GDK_INPUT_ONLY;
1282 attributes.event_mask = gtk_widget_get_events (widget);
1283 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1284 GDK_BUTTON_RELEASE_MASK |
1285 GDK_ENTER_NOTIFY_MASK |
1286 GDK_LEAVE_NOTIFY_MASK);
1288 attributes_mask = GDK_WA_X | GDK_WA_Y;
1290 window = gtk_widget_get_parent_window (widget);
1291 gtk_widget_set_window (widget, window);
1292 g_object_ref (window);
1294 button->event_window = gdk_window_new (window,
1295 &attributes, attributes_mask);
1296 gdk_window_set_user_data (button->event_window, button);
1298 gtk_widget_style_attach (widget);
1302 gtk_button_unrealize (GtkWidget *widget)
1304 GtkButton *button = GTK_BUTTON (widget);
1306 if (button->activate_timeout)
1307 gtk_button_finish_activate (button, FALSE);
1309 if (button->event_window)
1311 gdk_window_set_user_data (button->event_window, NULL);
1312 gdk_window_destroy (button->event_window);
1313 button->event_window = NULL;
1316 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1320 gtk_button_map (GtkWidget *widget)
1322 GtkButton *button = GTK_BUTTON (widget);
1324 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1326 if (button->event_window)
1327 gdk_window_show (button->event_window);
1331 gtk_button_unmap (GtkWidget *widget)
1333 GtkButton *button = GTK_BUTTON (widget);
1335 if (button->event_window)
1336 gdk_window_hide (button->event_window);
1338 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1342 gtk_button_update_image_spacing (GtkButton *button)
1344 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1348 /* Keep in sync with gtk_button_construct_child,
1349 * we only want to update the spacing if the box
1350 * was constructed there.
1352 if (!button->constructed || !priv->image)
1355 child = gtk_bin_get_child (GTK_BIN (button));
1356 if (GTK_IS_ALIGNMENT (child))
1358 child = gtk_bin_get_child (GTK_BIN (child));
1359 if (GTK_IS_BOX (child))
1361 gtk_widget_style_get (GTK_WIDGET (button),
1362 "image-spacing", &spacing,
1365 gtk_box_set_spacing (GTK_BOX (child), spacing);
1371 gtk_button_style_set (GtkWidget *widget,
1372 GtkStyle *prev_style)
1374 gtk_button_update_image_spacing (GTK_BUTTON (widget));
1378 gtk_button_get_props (GtkButton *button,
1379 GtkBorder *default_border,
1380 GtkBorder *default_outside_border,
1381 GtkBorder *inner_border,
1382 gboolean *interior_focus)
1384 GtkWidget *widget = GTK_WIDGET (button);
1385 GtkBorder *tmp_border;
1389 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1393 *default_border = *tmp_border;
1394 gtk_border_free (tmp_border);
1397 *default_border = default_default_border;
1400 if (default_outside_border)
1402 gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1406 *default_outside_border = *tmp_border;
1407 gtk_border_free (tmp_border);
1410 *default_outside_border = default_default_outside_border;
1415 gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1419 *inner_border = *tmp_border;
1420 gtk_border_free (tmp_border);
1423 *inner_border = default_inner_border;
1427 gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1431 gtk_button_size_allocate (GtkWidget *widget,
1432 GtkAllocation *allocation)
1434 GtkButton *button = GTK_BUTTON (widget);
1435 GtkAllocation child_allocation;
1439 gint xthickness, ythickness;
1440 GtkBorder default_border;
1441 GtkBorder inner_border;
1445 style = gtk_widget_get_style (widget);
1446 xthickness = style->xthickness;
1447 ythickness = style->ythickness;
1449 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1450 gtk_widget_style_get (GTK_WIDGET (widget),
1451 "focus-line-width", &focus_width,
1452 "focus-padding", &focus_pad,
1455 gtk_widget_set_allocation (widget, allocation);
1457 if (gtk_widget_get_realized (widget))
1458 gdk_window_move_resize (button->event_window,
1462 allocation->height);
1464 child = gtk_bin_get_child (GTK_BIN (button));
1465 if (child && gtk_widget_get_visible (child))
1467 child_allocation.x = allocation->x + inner_border.left + xthickness;
1468 child_allocation.y = allocation->y + inner_border.top + ythickness;
1470 child_allocation.width =
1476 child_allocation.height =
1477 allocation->height -
1480 inner_border.bottom;
1482 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1484 child_allocation.x += default_border.left;
1485 child_allocation.y += default_border.top;
1486 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1487 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1490 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1492 child_allocation.x += focus_width + focus_pad;
1493 child_allocation.y += focus_width + focus_pad;
1494 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1495 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1498 if (button->depressed)
1500 gint child_displacement_x;
1501 gint child_displacement_y;
1503 gtk_widget_style_get (widget,
1504 "child-displacement-x", &child_displacement_x,
1505 "child-displacement-y", &child_displacement_y,
1507 child_allocation.x += child_displacement_x;
1508 child_allocation.y += child_displacement_y;
1511 child_allocation.width = MAX (1, child_allocation.width);
1512 child_allocation.height = MAX (1, child_allocation.height);
1514 gtk_widget_size_allocate (child, &child_allocation);
1519 _gtk_button_paint (GtkButton *button,
1523 GtkStateType state_type,
1524 GtkShadowType shadow_type,
1525 const gchar *main_detail,
1526 const gchar *default_detail)
1530 GtkBorder default_border;
1531 GtkBorder default_outside_border;
1532 gboolean interior_focus;
1535 GtkAllocation allocation;
1539 widget = GTK_WIDGET (button);
1541 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1542 gtk_widget_style_get (widget,
1543 "focus-line-width", &focus_width,
1544 "focus-padding", &focus_pad,
1547 gtk_widget_get_allocation (widget, &allocation);
1548 style = gtk_widget_get_style (widget);
1549 window = gtk_widget_get_window (widget);
1554 if (gtk_widget_has_default (widget) &&
1555 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1557 gtk_paint_box (style, cr,
1558 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1559 widget, "buttondefault",
1560 x, y, width, height);
1562 x += default_border.left;
1563 y += default_border.top;
1564 width -= default_border.left + default_border.right;
1565 height -= default_border.top + default_border.bottom;
1567 else if (gtk_widget_get_can_default (widget))
1569 x += default_outside_border.left;
1570 y += default_outside_border.top;
1571 width -= default_outside_border.left + default_outside_border.right;
1572 height -= default_outside_border.top + default_outside_border.bottom;
1575 if (!interior_focus && gtk_widget_has_focus (widget))
1577 x += focus_width + focus_pad;
1578 y += focus_width + focus_pad;
1579 width -= 2 * (focus_width + focus_pad);
1580 height -= 2 * (focus_width + focus_pad);
1583 if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1584 gtk_widget_get_state(widget) == GTK_STATE_PRELIGHT)
1585 gtk_paint_box (style, cr,
1587 shadow_type, widget, "button",
1588 x, y, width, height);
1590 if (gtk_widget_has_focus (widget))
1592 gint child_displacement_x;
1593 gint child_displacement_y;
1594 gboolean displace_focus;
1596 gtk_widget_style_get (widget,
1597 "child-displacement-y", &child_displacement_y,
1598 "child-displacement-x", &child_displacement_x,
1599 "displace-focus", &displace_focus,
1604 x += style->xthickness + focus_pad;
1605 y += style->ythickness + focus_pad;
1606 width -= 2 * (style->xthickness + focus_pad);
1607 height -= 2 * (style->ythickness + focus_pad);
1611 x -= focus_width + focus_pad;
1612 y -= focus_width + focus_pad;
1613 width += 2 * (focus_width + focus_pad);
1614 height += 2 * (focus_width + focus_pad);
1617 if (button->depressed && displace_focus)
1619 x += child_displacement_x;
1620 y += child_displacement_y;
1623 gtk_paint_focus (style, cr,
1624 gtk_widget_get_state (widget),
1626 x, y, width, height);
1631 gtk_button_draw (GtkWidget *widget,
1634 GtkButton *button = GTK_BUTTON (widget);
1636 _gtk_button_paint (button, cr,
1637 gtk_widget_get_allocated_width (widget),
1638 gtk_widget_get_allocated_height (widget),
1639 gtk_widget_get_state (widget),
1640 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1641 "button", "buttondefault");
1643 GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1649 gtk_button_button_press (GtkWidget *widget,
1650 GdkEventButton *event)
1654 if (event->type == GDK_BUTTON_PRESS)
1656 button = GTK_BUTTON (widget);
1658 if (button->focus_on_click && !gtk_widget_has_focus (widget))
1659 gtk_widget_grab_focus (widget);
1661 if (event->button == 1)
1662 gtk_button_pressed (button);
1669 gtk_button_button_release (GtkWidget *widget,
1670 GdkEventButton *event)
1674 if (event->button == 1)
1676 button = GTK_BUTTON (widget);
1677 gtk_button_released (button);
1684 gtk_button_grab_broken (GtkWidget *widget,
1685 GdkEventGrabBroken *event)
1687 GtkButton *button = GTK_BUTTON (widget);
1690 /* Simulate a button release without the pointer in the button */
1691 if (button->button_down)
1693 save_in = button->in_button;
1694 button->in_button = FALSE;
1695 gtk_button_released (button);
1696 if (save_in != button->in_button)
1698 button->in_button = save_in;
1699 gtk_button_update_state (button);
1707 gtk_button_key_release (GtkWidget *widget,
1710 GtkButton *button = GTK_BUTTON (widget);
1712 if (button->activate_timeout)
1714 gtk_button_finish_activate (button, TRUE);
1717 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1718 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1724 gtk_button_enter_notify (GtkWidget *widget,
1725 GdkEventCrossing *event)
1729 button = GTK_BUTTON (widget);
1731 if ((event->window == button->event_window) &&
1732 (event->detail != GDK_NOTIFY_INFERIOR))
1734 button->in_button = TRUE;
1735 gtk_button_enter (button);
1742 gtk_button_leave_notify (GtkWidget *widget,
1743 GdkEventCrossing *event)
1747 button = GTK_BUTTON (widget);
1749 if ((event->window == button->event_window) &&
1750 (event->detail != GDK_NOTIFY_INFERIOR) &&
1751 (gtk_widget_get_sensitive (widget)))
1753 button->in_button = FALSE;
1754 gtk_button_leave (button);
1761 gtk_real_button_pressed (GtkButton *button)
1763 if (button->activate_timeout)
1766 button->button_down = TRUE;
1767 gtk_button_update_state (button);
1771 gtk_real_button_released (GtkButton *button)
1773 if (button->button_down)
1775 button->button_down = FALSE;
1777 if (button->activate_timeout)
1780 if (button->in_button)
1781 gtk_button_clicked (button);
1783 gtk_button_update_state (button);
1788 gtk_real_button_clicked (GtkButton *button)
1790 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1793 gtk_action_activate (priv->action);
1797 button_activate_timeout (gpointer data)
1799 gtk_button_finish_activate (data, TRUE);
1805 gtk_real_button_activate (GtkButton *button)
1807 GtkWidget *widget = GTK_WIDGET (button);
1808 GtkButtonPrivate *priv;
1812 priv = GTK_BUTTON_GET_PRIVATE (button);
1813 device = gtk_get_current_event_device ();
1815 if (device && device->source != GDK_SOURCE_KEYBOARD)
1816 device = gdk_device_get_associated_device (device);
1818 g_return_if_fail (device && device->source == GDK_SOURCE_KEYBOARD);
1820 if (gtk_widget_get_realized (widget) && !button->activate_timeout)
1822 time = gtk_get_current_event_time ();
1824 if (gdk_device_grab (device, button->event_window,
1825 GDK_OWNERSHIP_WINDOW, TRUE,
1826 GDK_KEY_PRESS | GDK_KEY_RELEASE,
1827 NULL, time) == GDK_GRAB_SUCCESS)
1829 gtk_device_grab_add (widget, device, TRUE);
1830 priv->grab_keyboard = device;
1831 priv->grab_time = time;
1834 button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1835 button_activate_timeout,
1837 button->button_down = TRUE;
1838 gtk_button_update_state (button);
1839 gtk_widget_queue_draw (GTK_WIDGET (button));
1844 gtk_button_finish_activate (GtkButton *button,
1847 GtkWidget *widget = GTK_WIDGET (button);
1848 GtkButtonPrivate *priv;
1850 priv = GTK_BUTTON_GET_PRIVATE (button);
1852 g_source_remove (button->activate_timeout);
1853 button->activate_timeout = 0;
1855 if (priv->grab_keyboard)
1857 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1858 gtk_device_grab_remove (widget, priv->grab_keyboard);
1859 priv->grab_keyboard = NULL;
1862 button->button_down = FALSE;
1864 gtk_button_update_state (button);
1865 gtk_widget_queue_draw (GTK_WIDGET (button));
1868 gtk_button_clicked (button);
1873 gtk_button_get_size (GtkWidget *widget,
1874 GtkOrientation orientation,
1878 GtkButton *button = GTK_BUTTON (widget);
1881 GtkBorder default_border;
1882 GtkBorder inner_border;
1885 gint minimum, natural;
1887 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1888 gtk_widget_style_get (GTK_WIDGET (widget),
1889 "focus-line-width", &focus_width,
1890 "focus-padding", &focus_pad,
1893 style = gtk_widget_get_style (GTK_WIDGET (widget));
1895 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1897 minimum = (style->xthickness * 2 +
1898 inner_border.left + inner_border.right);
1900 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1901 minimum += default_border.left + default_border.right;
1905 minimum = (style->ythickness * 2 +
1906 inner_border.top + inner_border.bottom);
1908 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1909 minimum += default_border.top + default_border.bottom;
1912 minimum += 2 * (focus_width + focus_pad);
1915 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1916 gtk_widget_get_visible (child))
1918 gint child_min, child_nat;
1920 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1921 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
1923 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1925 minimum += child_min;
1926 natural += child_nat;
1930 *minimum_size = minimum;
1933 *natural_size = natural;
1937 gtk_button_get_preferred_width (GtkWidget *widget,
1941 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1945 gtk_button_get_preferred_height (GtkWidget *widget,
1949 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1953 * gtk_button_set_label:
1954 * @button: a #GtkButton
1957 * Sets the text of the label of the button to @str. This text is
1958 * also used to select the stock item if gtk_button_set_use_stock()
1961 * This will also clear any previously set labels.
1964 gtk_button_set_label (GtkButton *button,
1969 g_return_if_fail (GTK_IS_BUTTON (button));
1971 new_label = g_strdup (label);
1972 g_free (button->label_text);
1973 button->label_text = new_label;
1975 gtk_button_construct_child (button);
1977 g_object_notify (G_OBJECT (button), "label");
1981 * gtk_button_get_label:
1982 * @button: a #GtkButton
1984 * Fetches the text from the label of the button, as set by
1985 * gtk_button_set_label(). If the label text has not
1986 * been set the return value will be %NULL. This will be the
1987 * case if you create an empty button with gtk_button_new() to
1988 * use as a container.
1990 * Return value: The text of the label widget. This string is owned
1991 * by the widget and must not be modified or freed.
1993 G_CONST_RETURN gchar *
1994 gtk_button_get_label (GtkButton *button)
1996 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1998 return button->label_text;
2002 * gtk_button_set_use_underline:
2003 * @button: a #GtkButton
2004 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2006 * If true, an underline in the text of the button label indicates
2007 * the next character should be used for the mnemonic accelerator key.
2010 gtk_button_set_use_underline (GtkButton *button,
2011 gboolean use_underline)
2013 g_return_if_fail (GTK_IS_BUTTON (button));
2015 use_underline = use_underline != FALSE;
2017 if (use_underline != button->use_underline)
2019 button->use_underline = use_underline;
2021 gtk_button_construct_child (button);
2023 g_object_notify (G_OBJECT (button), "use-underline");
2028 * gtk_button_get_use_underline:
2029 * @button: a #GtkButton
2031 * Returns whether an embedded underline in the button label indicates a
2032 * mnemonic. See gtk_button_set_use_underline ().
2034 * Return value: %TRUE if an embedded underline in the button label
2035 * indicates the mnemonic accelerator keys.
2038 gtk_button_get_use_underline (GtkButton *button)
2040 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2042 return button->use_underline;
2046 * gtk_button_set_use_stock:
2047 * @button: a #GtkButton
2048 * @use_stock: %TRUE if the button should use a stock item
2050 * If %TRUE, the label set on the button is used as a
2051 * stock id to select the stock item for the button.
2054 gtk_button_set_use_stock (GtkButton *button,
2057 g_return_if_fail (GTK_IS_BUTTON (button));
2059 use_stock = use_stock != FALSE;
2061 if (use_stock != button->use_stock)
2063 button->use_stock = use_stock;
2065 gtk_button_construct_child (button);
2067 g_object_notify (G_OBJECT (button), "use-stock");
2072 * gtk_button_get_use_stock:
2073 * @button: a #GtkButton
2075 * Returns whether the button label is a stock item.
2077 * Return value: %TRUE if the button label is used to
2078 * select a stock item instead of being
2079 * used directly as the label text.
2082 gtk_button_get_use_stock (GtkButton *button)
2084 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2086 return button->use_stock;
2090 * gtk_button_set_focus_on_click:
2091 * @button: a #GtkButton
2092 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2094 * Sets whether the button will grab focus when it is clicked with the mouse.
2095 * Making mouse clicks not grab focus is useful in places like toolbars where
2096 * you don't want the keyboard focus removed from the main area of the
2102 gtk_button_set_focus_on_click (GtkButton *button,
2103 gboolean focus_on_click)
2105 g_return_if_fail (GTK_IS_BUTTON (button));
2107 focus_on_click = focus_on_click != FALSE;
2109 if (button->focus_on_click != focus_on_click)
2111 button->focus_on_click = focus_on_click;
2113 g_object_notify (G_OBJECT (button), "focus-on-click");
2118 * gtk_button_get_focus_on_click:
2119 * @button: a #GtkButton
2121 * Returns whether the button grabs focus when it is clicked with the mouse.
2122 * See gtk_button_set_focus_on_click().
2124 * Return value: %TRUE if the button grabs focus when it is clicked with
2130 gtk_button_get_focus_on_click (GtkButton *button)
2132 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2134 return button->focus_on_click;
2138 * gtk_button_set_alignment:
2139 * @button: a #GtkButton
2140 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2141 * 1.0 is right aligned
2142 * @yalign: the vertical position of the child, 0.0 is top aligned,
2143 * 1.0 is bottom aligned
2145 * Sets the alignment of the child. This property has no effect unless
2146 * the child is a #GtkMisc or a #GtkAlignment.
2151 gtk_button_set_alignment (GtkButton *button,
2155 GtkButtonPrivate *priv;
2157 g_return_if_fail (GTK_IS_BUTTON (button));
2159 priv = GTK_BUTTON_GET_PRIVATE (button);
2161 priv->xalign = xalign;
2162 priv->yalign = yalign;
2163 priv->align_set = 1;
2165 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2167 g_object_freeze_notify (G_OBJECT (button));
2168 g_object_notify (G_OBJECT (button), "xalign");
2169 g_object_notify (G_OBJECT (button), "yalign");
2170 g_object_thaw_notify (G_OBJECT (button));
2174 * gtk_button_get_alignment:
2175 * @button: a #GtkButton
2176 * @xalign: return location for horizontal alignment
2177 * @yalign: return location for vertical alignment
2179 * Gets the alignment of the child in the button.
2184 gtk_button_get_alignment (GtkButton *button,
2188 GtkButtonPrivate *priv;
2190 g_return_if_fail (GTK_IS_BUTTON (button));
2192 priv = GTK_BUTTON_GET_PRIVATE (button);
2195 *xalign = priv->xalign;
2198 *yalign = priv->yalign;
2202 * _gtk_button_set_depressed:
2203 * @button: a #GtkButton
2204 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2206 * Sets whether the button is currently drawn as down or not. This is
2207 * purely a visual setting, and is meant only for use by derived widgets
2208 * such as #GtkToggleButton.
2211 _gtk_button_set_depressed (GtkButton *button,
2214 GtkWidget *widget = GTK_WIDGET (button);
2216 depressed = depressed != FALSE;
2218 if (depressed != button->depressed)
2220 button->depressed = depressed;
2221 gtk_widget_queue_resize (widget);
2226 gtk_button_update_state (GtkButton *button)
2229 GtkStateType new_state;
2231 if (button->activate_timeout)
2232 depressed = button->depress_on_activate;
2234 depressed = button->in_button && button->button_down;
2236 if (button->in_button && (!button->button_down || !depressed))
2237 new_state = GTK_STATE_PRELIGHT;
2239 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2241 _gtk_button_set_depressed (button, depressed);
2242 gtk_widget_set_state (GTK_WIDGET (button), new_state);
2246 show_image_change_notify (GtkButton *button)
2248 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2252 if (show_image (button))
2253 gtk_widget_show (priv->image);
2255 gtk_widget_hide (priv->image);
2260 traverse_container (GtkWidget *widget,
2263 if (GTK_IS_BUTTON (widget))
2264 show_image_change_notify (GTK_BUTTON (widget));
2265 else if (GTK_IS_CONTAINER (widget))
2266 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2270 gtk_button_setting_changed (GtkSettings *settings)
2274 list = gtk_window_list_toplevels ();
2276 for (l = list; l; l = l->next)
2277 gtk_container_forall (GTK_CONTAINER (l->data),
2278 traverse_container, NULL);
2285 gtk_button_screen_changed (GtkWidget *widget,
2286 GdkScreen *previous_screen)
2289 GtkSettings *settings;
2290 gulong show_image_connection;
2292 if (!gtk_widget_has_screen (widget))
2295 button = GTK_BUTTON (widget);
2297 /* If the button is being pressed while the screen changes the
2298 release might never occur, so we reset the state. */
2299 if (button->button_down)
2301 button->button_down = FALSE;
2302 gtk_button_update_state (button);
2305 settings = gtk_widget_get_settings (widget);
2307 show_image_connection =
2308 g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2309 NULL, gtk_button_setting_changed, NULL);
2311 if (show_image_connection)
2314 g_signal_connect (settings, "notify::gtk-button-images",
2315 G_CALLBACK (gtk_button_setting_changed), NULL);
2317 show_image_change_notify (button);
2321 gtk_button_state_changed (GtkWidget *widget,
2322 GtkStateType previous_state)
2324 GtkButton *button = GTK_BUTTON (widget);
2326 if (!gtk_widget_is_sensitive (widget))
2328 button->in_button = FALSE;
2329 gtk_real_button_released (button);
2334 gtk_button_grab_notify (GtkWidget *widget,
2335 gboolean was_grabbed)
2337 GtkButton *button = GTK_BUTTON (widget);
2338 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2341 if (button->activate_timeout &&
2342 priv->grab_keyboard &&
2343 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2344 gtk_button_finish_activate (button, FALSE);
2348 save_in = button->in_button;
2349 button->in_button = FALSE;
2350 gtk_real_button_released (button);
2351 if (save_in != button->in_button)
2353 button->in_button = save_in;
2354 gtk_button_update_state (button);
2360 * gtk_button_set_image:
2361 * @button: a #GtkButton
2362 * @image: a widget to set as the image for the button
2364 * Set the image of @button to the given widget. Note that
2365 * it depends on the #GtkSettings:gtk-button-images setting whether the
2366 * image will be displayed or not, you don't have to call
2367 * gtk_widget_show() on @image yourself.
2372 gtk_button_set_image (GtkButton *button,
2375 GtkButtonPrivate *priv;
2378 g_return_if_fail (GTK_IS_BUTTON (button));
2379 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2381 priv = GTK_BUTTON_GET_PRIVATE (button);
2385 parent = gtk_widget_get_parent (priv->image);
2387 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2390 priv->image = image;
2391 priv->image_is_stock = (image == NULL);
2393 gtk_button_construct_child (button);
2395 g_object_notify (G_OBJECT (button), "image");
2399 * gtk_button_get_image:
2400 * @button: a #GtkButton
2402 * Gets the widget that is currenty set as the image of @button.
2403 * This may have been explicitly set by gtk_button_set_image()
2404 * or constructed by gtk_button_new_from_stock().
2406 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2411 gtk_button_get_image (GtkButton *button)
2413 GtkButtonPrivate *priv;
2415 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2417 priv = GTK_BUTTON_GET_PRIVATE (button);
2423 * gtk_button_set_image_position:
2424 * @button: a #GtkButton
2425 * @position: the position
2427 * Sets the position of the image relative to the text
2428 * inside the button.
2433 gtk_button_set_image_position (GtkButton *button,
2434 GtkPositionType position)
2437 GtkButtonPrivate *priv;
2439 g_return_if_fail (GTK_IS_BUTTON (button));
2440 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2442 priv = GTK_BUTTON_GET_PRIVATE (button);
2444 if (priv->image_position != position)
2446 priv->image_position = position;
2448 gtk_button_construct_child (button);
2450 g_object_notify (G_OBJECT (button), "image-position");
2455 * gtk_button_get_image_position:
2456 * @button: a #GtkButton
2458 * Gets the position of the image relative to the text
2459 * inside the button.
2461 * Return value: the position
2466 gtk_button_get_image_position (GtkButton *button)
2468 GtkButtonPrivate *priv;
2470 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2472 priv = GTK_BUTTON_GET_PRIVATE (button);
2474 return priv->image_position;
2479 * gtk_button_get_event_window:
2480 * @button: a #GtkButton
2482 * Returns the button's event window if it is realized, %NULL otherwise.
2483 * This function should be rarely needed.
2485 * Return value: (transfer none): @button's event window.
2490 gtk_button_get_event_window (GtkButton *button)
2492 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2494 return button->event_window;