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 #include "gtkalignment.h"
30 #include "gtkbutton.h"
33 #include "gtkmarshalers.h"
38 #include "gtkiconfactory.h"
39 #include "gtkactivatable.h"
40 #include "gtkextendedlayout.h"
41 #include "gtkprivate.h"
45 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
46 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
47 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
49 /* Time out before giving up on getting a key release when animating
52 #define ACTIVATE_TIMEOUT 250
76 /* activatable properties */
77 PROP_ACTIVATABLE_RELATED_ACTION,
78 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
81 #define GTK_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_BUTTON, GtkButtonPrivate))
82 typedef struct _GtkButtonPrivate GtkButtonPrivate;
84 struct _GtkButtonPrivate
90 guint image_is_stock : 1;
91 guint use_action_appearance : 1;
93 GdkDevice *grab_keyboard;
94 GtkPositionType image_position;
98 static void gtk_button_destroy (GtkObject *object);
99 static void gtk_button_dispose (GObject *object);
100 static void gtk_button_set_property (GObject *object,
104 static void gtk_button_get_property (GObject *object,
108 static void gtk_button_screen_changed (GtkWidget *widget,
109 GdkScreen *previous_screen);
110 static void gtk_button_realize (GtkWidget * widget);
111 static void gtk_button_unrealize (GtkWidget * widget);
112 static void gtk_button_map (GtkWidget * widget);
113 static void gtk_button_unmap (GtkWidget * widget);
114 static void gtk_button_style_set (GtkWidget * widget, GtkStyle * prev_style);
115 static void gtk_button_size_allocate (GtkWidget * widget,
116 GtkAllocation * allocation);
117 static gint gtk_button_expose (GtkWidget * widget, GdkEventExpose * event);
118 static gint gtk_button_button_press (GtkWidget * widget,
119 GdkEventButton * event);
120 static gint gtk_button_button_release (GtkWidget * widget,
121 GdkEventButton * event);
122 static gint gtk_button_grab_broken (GtkWidget * widget,
123 GdkEventGrabBroken * event);
124 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
125 static gint gtk_button_enter_notify (GtkWidget * widget,
126 GdkEventCrossing * event);
127 static gint gtk_button_leave_notify (GtkWidget * widget,
128 GdkEventCrossing * event);
129 static void gtk_real_button_pressed (GtkButton * button);
130 static void gtk_real_button_released (GtkButton * button);
131 static void gtk_real_button_clicked (GtkButton * button);
132 static void gtk_real_button_activate (GtkButton *button);
133 static void gtk_button_update_state (GtkButton *button);
134 static void gtk_button_add (GtkContainer *container,
136 static GType gtk_button_child_type (GtkContainer *container);
137 static void gtk_button_finish_activate (GtkButton *button,
140 static GObject* gtk_button_constructor (GType type,
141 guint n_construct_properties,
142 GObjectConstructParam *construct_params);
143 static void gtk_button_construct_child (GtkButton *button);
144 static void gtk_button_state_changed (GtkWidget *widget,
145 GtkStateType previous_state);
146 static void gtk_button_grab_notify (GtkWidget *widget,
147 gboolean was_grabbed);
150 static void gtk_button_activatable_interface_init(GtkActivatableIface *iface);
151 static void gtk_button_update (GtkActivatable *activatable,
153 const gchar *property_name);
154 static void gtk_button_sync_action_properties (GtkActivatable *activatable,
156 static void gtk_button_set_related_action (GtkButton *button,
158 static void gtk_button_set_use_action_appearance (GtkButton *button,
159 gboolean use_appearance);
161 static void gtk_button_extended_layout_init (GtkExtendedLayoutIface *iface);
162 static void gtk_button_get_desired_width (GtkExtendedLayout *layout,
165 static void gtk_button_get_desired_height (GtkExtendedLayout *layout,
169 static guint button_signals[LAST_SIGNAL] = { 0 };
171 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
172 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
173 gtk_button_activatable_interface_init)
174 G_IMPLEMENT_INTERFACE (GTK_TYPE_EXTENDED_LAYOUT,
175 gtk_button_extended_layout_init))
178 gtk_button_class_init (GtkButtonClass *klass)
180 GObjectClass *gobject_class;
181 GtkObjectClass *object_class;
182 GtkWidgetClass *widget_class;
183 GtkContainerClass *container_class;
185 gobject_class = G_OBJECT_CLASS (klass);
186 object_class = (GtkObjectClass*) klass;
187 widget_class = (GtkWidgetClass*) klass;
188 container_class = (GtkContainerClass*) klass;
190 gobject_class->constructor = gtk_button_constructor;
191 gobject_class->dispose = gtk_button_dispose;
192 gobject_class->set_property = gtk_button_set_property;
193 gobject_class->get_property = gtk_button_get_property;
195 object_class->destroy = gtk_button_destroy;
197 widget_class->screen_changed = gtk_button_screen_changed;
198 widget_class->realize = gtk_button_realize;
199 widget_class->unrealize = gtk_button_unrealize;
200 widget_class->map = gtk_button_map;
201 widget_class->unmap = gtk_button_unmap;
202 widget_class->style_set = gtk_button_style_set;
203 widget_class->size_allocate = gtk_button_size_allocate;
204 widget_class->expose_event = gtk_button_expose;
205 widget_class->button_press_event = gtk_button_button_press;
206 widget_class->button_release_event = gtk_button_button_release;
207 widget_class->grab_broken_event = gtk_button_grab_broken;
208 widget_class->key_release_event = gtk_button_key_release;
209 widget_class->enter_notify_event = gtk_button_enter_notify;
210 widget_class->leave_notify_event = gtk_button_leave_notify;
211 widget_class->state_changed = gtk_button_state_changed;
212 widget_class->grab_notify = gtk_button_grab_notify;
214 container_class->child_type = gtk_button_child_type;
215 container_class->add = gtk_button_add;
217 klass->pressed = gtk_real_button_pressed;
218 klass->released = gtk_real_button_released;
219 klass->clicked = NULL;
220 klass->enter = gtk_button_update_state;
221 klass->leave = gtk_button_update_state;
222 klass->activate = gtk_real_button_activate;
224 g_object_class_install_property (gobject_class,
226 g_param_spec_string ("label",
228 P_("Text of the label widget inside the button, if the button contains a label widget"),
230 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
232 g_object_class_install_property (gobject_class,
234 g_param_spec_boolean ("use-underline",
236 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
238 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
240 g_object_class_install_property (gobject_class,
242 g_param_spec_boolean ("use-stock",
244 P_("If set, the label is used to pick a stock item instead of being displayed"),
246 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_boolean ("focus-on-click",
251 P_("Focus on click"),
252 P_("Whether the button grabs focus when it is clicked with the mouse"),
254 GTK_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class,
258 g_param_spec_enum ("relief",
260 P_("The border relief style"),
261 GTK_TYPE_RELIEF_STYLE,
263 GTK_PARAM_READWRITE));
268 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
269 * can be used to control it's horizontal alignment. 0.0 is left aligned,
270 * 1.0 is right aligned.
274 g_object_class_install_property (gobject_class,
276 g_param_spec_float("xalign",
277 P_("Horizontal alignment for child"),
278 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
282 GTK_PARAM_READWRITE));
287 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
288 * can be used to control it's vertical alignment. 0.0 is top aligned,
289 * 1.0 is bottom aligned.
293 g_object_class_install_property (gobject_class,
295 g_param_spec_float("yalign",
296 P_("Vertical alignment for child"),
297 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
301 GTK_PARAM_READWRITE));
306 * The child widget to appear next to the button text.
310 g_object_class_install_property (gobject_class,
312 g_param_spec_object ("image",
314 P_("Child widget to appear next to the button text"),
316 GTK_PARAM_READWRITE));
319 * GtkButton:image-position:
321 * The position of the image relative to the text inside the button.
325 g_object_class_install_property (gobject_class,
327 g_param_spec_enum ("image-position",
328 P_("Image position"),
329 P_("The position of the image relative to the text"),
330 GTK_TYPE_POSITION_TYPE,
332 GTK_PARAM_READWRITE));
334 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
335 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
338 * GtkButton::pressed:
339 * @button: the object that received the signal
341 * Emitted when the button is pressed.
343 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
345 button_signals[PRESSED] =
346 g_signal_new (I_("pressed"),
347 G_OBJECT_CLASS_TYPE (object_class),
349 G_STRUCT_OFFSET (GtkButtonClass, pressed),
351 _gtk_marshal_VOID__VOID,
355 * GtkButton::released:
356 * @button: the object that received the signal
358 * Emitted when the button is released.
360 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
362 button_signals[RELEASED] =
363 g_signal_new (I_("released"),
364 G_OBJECT_CLASS_TYPE (object_class),
366 G_STRUCT_OFFSET (GtkButtonClass, released),
368 _gtk_marshal_VOID__VOID,
372 * GtkButton::clicked:
373 * @button: the object that received the signal
375 * Emitted when the button has been activated (pressed and released).
377 button_signals[CLICKED] =
378 g_signal_new (I_("clicked"),
379 G_OBJECT_CLASS_TYPE (object_class),
380 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
381 G_STRUCT_OFFSET (GtkButtonClass, clicked),
383 _gtk_marshal_VOID__VOID,
388 * @button: the object that received the signal
390 * Emitted when the pointer enters the button.
392 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
394 button_signals[ENTER] =
395 g_signal_new (I_("enter"),
396 G_OBJECT_CLASS_TYPE (object_class),
398 G_STRUCT_OFFSET (GtkButtonClass, enter),
400 _gtk_marshal_VOID__VOID,
405 * @button: the object that received the signal
407 * Emitted when the pointer leaves the button.
409 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
411 button_signals[LEAVE] =
412 g_signal_new (I_("leave"),
413 G_OBJECT_CLASS_TYPE (object_class),
415 G_STRUCT_OFFSET (GtkButtonClass, leave),
417 _gtk_marshal_VOID__VOID,
421 * GtkButton::activate:
422 * @widget: the object which received the signal.
424 * The ::activate signal on GtkButton is an action signal and
425 * emitting it causes the button to animate press then release.
426 * Applications should never connect to this signal, but use the
427 * #GtkButton::clicked signal.
429 button_signals[ACTIVATE] =
430 g_signal_new (I_("activate"),
431 G_OBJECT_CLASS_TYPE (object_class),
432 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
433 G_STRUCT_OFFSET (GtkButtonClass, activate),
435 _gtk_marshal_VOID__VOID,
437 widget_class->activate_signal = button_signals[ACTIVATE];
440 * GtkButton:default-border:
442 * The "default-border" style property defines the extra space to add
443 * around a button that can become the default widget of its window.
444 * For more information about default widgets, see gtk_widget_grab_default().
447 gtk_widget_class_install_style_property (widget_class,
448 g_param_spec_boxed ("default-border",
449 P_("Default Spacing"),
450 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
452 GTK_PARAM_READABLE));
455 * GtkButton:default-outside-border:
457 * The "default-outside-border" style property defines the extra outside
458 * space to add around a button that can become the default widget of its
459 * window. Extra outside space is always drawn outside the button border.
460 * For more information about default widgets, see gtk_widget_grab_default().
462 gtk_widget_class_install_style_property (widget_class,
463 g_param_spec_boxed ("default-outside-border",
464 P_("Default Outside Spacing"),
465 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
467 GTK_PARAM_READABLE));
468 gtk_widget_class_install_style_property (widget_class,
469 g_param_spec_int ("child-displacement-x",
470 P_("Child X Displacement"),
471 P_("How far in the x direction to move the child when the button is depressed"),
475 GTK_PARAM_READABLE));
476 gtk_widget_class_install_style_property (widget_class,
477 g_param_spec_int ("child-displacement-y",
478 P_("Child Y Displacement"),
479 P_("How far in the y direction to move the child when the button is depressed"),
483 GTK_PARAM_READABLE));
486 * GtkButton:displace-focus:
488 * Whether the child_displacement_x/child_displacement_y properties
489 * should also affect the focus rectangle.
493 gtk_widget_class_install_style_property (widget_class,
494 g_param_spec_boolean ("displace-focus",
495 P_("Displace focus"),
496 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
498 GTK_PARAM_READABLE));
501 * GtkButton:inner-border:
503 * Sets the border between the button edges and child.
507 gtk_widget_class_install_style_property (widget_class,
508 g_param_spec_boxed ("inner-border",
510 P_("Border between button edges and child."),
512 GTK_PARAM_READABLE));
515 * GtkButton::image-spacing:
517 * Spacing in pixels between the image and label.
521 gtk_widget_class_install_style_property (widget_class,
522 g_param_spec_int ("image-spacing",
524 P_("Spacing in pixels between the image and label"),
528 GTK_PARAM_READABLE));
531 * GtkSettings::gtk-button-images:
533 * Whether images should be shown on buttons
537 gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
538 P_("Show button images"),
539 P_("Whether images should be shown on buttons"),
541 GTK_PARAM_READWRITE));
543 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
547 gtk_button_init (GtkButton *button)
549 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
551 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
552 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
553 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
555 button->label_text = NULL;
557 button->constructed = FALSE;
558 button->in_button = FALSE;
559 button->button_down = FALSE;
560 button->relief = GTK_RELIEF_NORMAL;
561 button->use_stock = FALSE;
562 button->use_underline = FALSE;
563 button->depressed = FALSE;
564 button->depress_on_activate = TRUE;
565 button->focus_on_click = TRUE;
570 priv->image_is_stock = TRUE;
571 priv->image_position = GTK_POS_LEFT;
572 priv->use_action_appearance = TRUE;
576 gtk_button_destroy (GtkObject *object)
578 GtkButton *button = GTK_BUTTON (object);
580 if (button->label_text)
582 g_free (button->label_text);
583 button->label_text = NULL;
586 GTK_OBJECT_CLASS (gtk_button_parent_class)->destroy (object);
590 gtk_button_constructor (GType type,
591 guint n_construct_properties,
592 GObjectConstructParam *construct_params)
597 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
598 n_construct_properties,
601 button = GTK_BUTTON (object);
602 button->constructed = TRUE;
604 if (button->label_text != NULL)
605 gtk_button_construct_child (button);
612 gtk_button_child_type (GtkContainer *container)
614 if (!GTK_BIN (container)->child)
615 return GTK_TYPE_WIDGET;
621 maybe_set_alignment (GtkButton *button,
624 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
626 if (GTK_IS_MISC (widget))
628 GtkMisc *misc = GTK_MISC (widget);
631 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
633 else if (GTK_IS_ALIGNMENT (widget))
635 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
638 gtk_alignment_set (alignment, priv->xalign, priv->yalign,
639 alignment->xscale, alignment->yscale);
644 gtk_button_add (GtkContainer *container,
647 maybe_set_alignment (GTK_BUTTON (container), widget);
649 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
653 gtk_button_dispose (GObject *object)
655 GtkButton *button = GTK_BUTTON (object);
656 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
660 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
663 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
667 gtk_button_set_property (GObject *object,
672 GtkButton *button = GTK_BUTTON (object);
673 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
678 gtk_button_set_label (button, g_value_get_string (value));
681 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
684 gtk_button_set_relief (button, g_value_get_enum (value));
686 case PROP_USE_UNDERLINE:
687 gtk_button_set_use_underline (button, g_value_get_boolean (value));
690 gtk_button_set_use_stock (button, g_value_get_boolean (value));
692 case PROP_FOCUS_ON_CLICK:
693 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
696 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
699 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
701 case PROP_IMAGE_POSITION:
702 gtk_button_set_image_position (button, g_value_get_enum (value));
704 case PROP_ACTIVATABLE_RELATED_ACTION:
705 gtk_button_set_related_action (button, g_value_get_object (value));
707 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
708 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
711 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
717 gtk_button_get_property (GObject *object,
722 GtkButton *button = GTK_BUTTON (object);
723 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
728 g_value_set_string (value, button->label_text);
731 g_value_set_object (value, (GObject *)priv->image);
734 g_value_set_enum (value, gtk_button_get_relief (button));
736 case PROP_USE_UNDERLINE:
737 g_value_set_boolean (value, button->use_underline);
740 g_value_set_boolean (value, button->use_stock);
742 case PROP_FOCUS_ON_CLICK:
743 g_value_set_boolean (value, button->focus_on_click);
746 g_value_set_float (value, priv->xalign);
749 g_value_set_float (value, priv->yalign);
751 case PROP_IMAGE_POSITION:
752 g_value_set_enum (value, priv->image_position);
754 case PROP_ACTIVATABLE_RELATED_ACTION:
755 g_value_set_object (value, priv->action);
757 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
758 g_value_set_boolean (value, priv->use_action_appearance);
761 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
767 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
769 iface->update = gtk_button_update;
770 iface->sync_action_properties = gtk_button_sync_action_properties;
774 activatable_update_stock_id (GtkButton *button,
777 if (!gtk_button_get_use_stock (button))
780 gtk_button_set_label (button, gtk_action_get_stock_id (action));
784 activatable_update_short_label (GtkButton *button,
789 if (gtk_button_get_use_stock (button))
792 image = gtk_button_get_image (button);
794 /* Dont touch custom child... */
795 if (GTK_IS_IMAGE (image) ||
796 GTK_BIN (button)->child == NULL ||
797 GTK_IS_LABEL (GTK_BIN (button)->child))
799 gtk_button_set_label (button, gtk_action_get_short_label (action));
800 gtk_button_set_use_underline (button, TRUE);
805 activatable_update_icon_name (GtkButton *button,
810 if (gtk_button_get_use_stock (button))
813 image = gtk_button_get_image (button);
815 if (GTK_IS_IMAGE (image) &&
816 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
817 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
818 gtk_image_set_from_icon_name (GTK_IMAGE (image),
819 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
823 activatable_update_gicon (GtkButton *button,
826 GtkWidget *image = gtk_button_get_image (button);
827 GIcon *icon = gtk_action_get_gicon (action);
829 if (GTK_IS_IMAGE (image) &&
830 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
831 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
832 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
836 gtk_button_update (GtkActivatable *activatable,
838 const gchar *property_name)
840 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
842 if (strcmp (property_name, "visible") == 0)
844 if (gtk_action_is_visible (action))
845 gtk_widget_show (GTK_WIDGET (activatable));
847 gtk_widget_hide (GTK_WIDGET (activatable));
849 else if (strcmp (property_name, "sensitive") == 0)
850 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
852 if (!priv->use_action_appearance)
855 if (strcmp (property_name, "stock-id") == 0)
856 activatable_update_stock_id (GTK_BUTTON (activatable), action);
857 else if (strcmp (property_name, "gicon") == 0)
858 activatable_update_gicon (GTK_BUTTON (activatable), action);
859 else if (strcmp (property_name, "short-label") == 0)
860 activatable_update_short_label (GTK_BUTTON (activatable), action);
861 else if (strcmp (property_name, "icon-name") == 0)
862 activatable_update_icon_name (GTK_BUTTON (activatable), action);
866 gtk_button_sync_action_properties (GtkActivatable *activatable,
869 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
874 if (gtk_action_is_visible (action))
875 gtk_widget_show (GTK_WIDGET (activatable));
877 gtk_widget_hide (GTK_WIDGET (activatable));
879 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
881 if (priv->use_action_appearance)
883 activatable_update_stock_id (GTK_BUTTON (activatable), action);
884 activatable_update_short_label (GTK_BUTTON (activatable), action);
885 activatable_update_gicon (GTK_BUTTON (activatable), action);
886 activatable_update_icon_name (GTK_BUTTON (activatable), action);
891 gtk_button_set_related_action (GtkButton *button,
894 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
896 if (priv->action == action)
899 /* This should be a default handler, but for compatibility reasons
900 * we need to support derived classes that don't chain up their
903 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
905 g_signal_connect_after (button, "clicked",
906 G_CALLBACK (gtk_real_button_clicked), NULL);
908 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
910 priv->action = action;
914 gtk_button_set_use_action_appearance (GtkButton *button,
915 gboolean use_appearance)
917 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
919 if (priv->use_action_appearance != use_appearance)
921 priv->use_action_appearance = use_appearance;
923 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
928 gtk_button_new (void)
930 return g_object_new (GTK_TYPE_BUTTON, NULL);
934 show_image (GtkButton *button)
938 if (button->label_text)
940 GtkSettings *settings;
942 settings = gtk_widget_get_settings (GTK_WIDGET (button));
943 g_object_get (settings, "gtk-button-images", &show, NULL);
952 gtk_button_construct_child (GtkButton *button)
954 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
959 GtkWidget *image = NULL;
960 gchar *label_text = NULL;
963 if (!button->constructed)
966 if (!button->label_text && !priv->image)
969 gtk_widget_style_get (GTK_WIDGET (button),
970 "image-spacing", &image_spacing,
973 if (priv->image && !priv->image_is_stock)
975 image = g_object_ref (priv->image);
977 gtk_container_remove (GTK_CONTAINER (image->parent), image);
982 if (GTK_BIN (button)->child)
983 gtk_container_remove (GTK_CONTAINER (button),
984 GTK_BIN (button)->child);
986 if (button->use_stock &&
987 button->label_text &&
988 gtk_stock_lookup (button->label_text, &item))
991 image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
993 label_text = item.label;
996 label_text = button->label_text;
1000 priv->image = image;
1001 g_object_set (priv->image,
1002 "visible", show_image (button),
1003 "no-show-all", TRUE,
1006 if (priv->image_position == GTK_POS_LEFT ||
1007 priv->image_position == GTK_POS_RIGHT)
1008 box = gtk_hbox_new (FALSE, image_spacing);
1010 box = gtk_vbox_new (FALSE, image_spacing);
1012 if (priv->align_set)
1013 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1015 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1017 if (priv->image_position == GTK_POS_LEFT ||
1018 priv->image_position == GTK_POS_TOP)
1019 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1021 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1025 if (button->use_underline || button->use_stock)
1027 label = gtk_label_new_with_mnemonic (label_text);
1028 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1029 GTK_WIDGET (button));
1032 label = gtk_label_new (label_text);
1034 if (priv->image_position == GTK_POS_RIGHT ||
1035 priv->image_position == GTK_POS_BOTTOM)
1036 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1038 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1041 gtk_container_add (GTK_CONTAINER (button), align);
1042 gtk_container_add (GTK_CONTAINER (align), box);
1043 gtk_widget_show_all (align);
1045 g_object_unref (image);
1050 if (button->use_underline || button->use_stock)
1052 label = gtk_label_new_with_mnemonic (button->label_text);
1053 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1056 label = gtk_label_new (button->label_text);
1058 if (priv->align_set)
1059 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1061 gtk_widget_show (label);
1062 gtk_container_add (GTK_CONTAINER (button), label);
1067 gtk_button_new_with_label (const gchar *label)
1069 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1073 * gtk_button_new_from_stock:
1074 * @stock_id: the name of the stock item
1076 * Creates a new #GtkButton containing the image and text from a stock item.
1077 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1080 * If @stock_id is unknown, then it will be treated as a mnemonic
1081 * label (as for gtk_button_new_with_mnemonic()).
1083 * Returns: a new #GtkButton
1086 gtk_button_new_from_stock (const gchar *stock_id)
1088 return g_object_new (GTK_TYPE_BUTTON,
1091 "use-underline", TRUE,
1096 * gtk_button_new_with_mnemonic:
1097 * @label: The text of the button, with an underscore in front of the
1098 * mnemonic character
1099 * @returns: a new #GtkButton
1101 * Creates a new #GtkButton containing a label.
1102 * If characters in @label are preceded by an underscore, they are underlined.
1103 * If you need a literal underscore character in a label, use '__' (two
1104 * underscores). The first underlined character represents a keyboard
1105 * accelerator called a mnemonic.
1106 * Pressing Alt and that key activates the button.
1109 gtk_button_new_with_mnemonic (const gchar *label)
1111 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1115 gtk_button_pressed (GtkButton *button)
1117 g_return_if_fail (GTK_IS_BUTTON (button));
1120 g_signal_emit (button, button_signals[PRESSED], 0);
1124 gtk_button_released (GtkButton *button)
1126 g_return_if_fail (GTK_IS_BUTTON (button));
1128 g_signal_emit (button, button_signals[RELEASED], 0);
1132 gtk_button_clicked (GtkButton *button)
1134 g_return_if_fail (GTK_IS_BUTTON (button));
1136 g_signal_emit (button, button_signals[CLICKED], 0);
1140 gtk_button_enter (GtkButton *button)
1142 g_return_if_fail (GTK_IS_BUTTON (button));
1144 g_signal_emit (button, button_signals[ENTER], 0);
1148 gtk_button_leave (GtkButton *button)
1150 g_return_if_fail (GTK_IS_BUTTON (button));
1152 g_signal_emit (button, button_signals[LEAVE], 0);
1156 gtk_button_set_relief (GtkButton *button,
1157 GtkReliefStyle newrelief)
1159 g_return_if_fail (GTK_IS_BUTTON (button));
1161 if (newrelief != button->relief)
1163 button->relief = newrelief;
1164 g_object_notify (G_OBJECT (button), "relief");
1165 gtk_widget_queue_draw (GTK_WIDGET (button));
1170 gtk_button_get_relief (GtkButton *button)
1172 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1174 return button->relief;
1178 gtk_button_realize (GtkWidget *widget)
1181 GdkWindowAttr attributes;
1182 gint attributes_mask;
1185 button = GTK_BUTTON (widget);
1186 gtk_widget_set_realized (widget, TRUE);
1188 border_width = GTK_CONTAINER (widget)->border_width;
1190 attributes.window_type = GDK_WINDOW_CHILD;
1191 attributes.x = widget->allocation.x + border_width;
1192 attributes.y = widget->allocation.y + border_width;
1193 attributes.width = widget->allocation.width - border_width * 2;
1194 attributes.height = widget->allocation.height - border_width * 2;
1195 attributes.wclass = GDK_INPUT_ONLY;
1196 attributes.event_mask = gtk_widget_get_events (widget);
1197 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1198 GDK_BUTTON_RELEASE_MASK |
1199 GDK_ENTER_NOTIFY_MASK |
1200 GDK_LEAVE_NOTIFY_MASK);
1202 attributes_mask = GDK_WA_X | GDK_WA_Y;
1204 widget->window = gtk_widget_get_parent_window (widget);
1205 g_object_ref (widget->window);
1207 button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1208 &attributes, attributes_mask);
1209 gdk_window_set_user_data (button->event_window, button);
1211 widget->style = gtk_style_attach (widget->style, widget->window);
1215 gtk_button_unrealize (GtkWidget *widget)
1217 GtkButton *button = GTK_BUTTON (widget);
1219 if (button->activate_timeout)
1220 gtk_button_finish_activate (button, FALSE);
1222 if (button->event_window)
1224 gdk_window_set_user_data (button->event_window, NULL);
1225 gdk_window_destroy (button->event_window);
1226 button->event_window = NULL;
1229 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1233 gtk_button_map (GtkWidget *widget)
1235 GtkButton *button = GTK_BUTTON (widget);
1237 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1239 if (button->event_window)
1240 gdk_window_show (button->event_window);
1244 gtk_button_unmap (GtkWidget *widget)
1246 GtkButton *button = GTK_BUTTON (widget);
1248 if (button->event_window)
1249 gdk_window_hide (button->event_window);
1251 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1255 gtk_button_update_image_spacing (GtkButton *button)
1257 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1261 /* Keep in sync with gtk_button_construct_child,
1262 * we only want to update the spacing if the box
1263 * was constructed there.
1265 if (!button->constructed || !priv->image)
1268 child = GTK_BIN (button)->child;
1269 if (GTK_IS_ALIGNMENT (child))
1271 child = GTK_BIN (child)->child;
1272 if (GTK_IS_BOX (child))
1274 gtk_widget_style_get (GTK_WIDGET (button),
1275 "image-spacing", &spacing,
1278 gtk_box_set_spacing (GTK_BOX (child), spacing);
1284 gtk_button_style_set (GtkWidget *widget,
1285 GtkStyle *prev_style)
1287 gtk_button_update_image_spacing (GTK_BUTTON (widget));
1291 gtk_button_get_props (GtkButton *button,
1292 GtkBorder *default_border,
1293 GtkBorder *default_outside_border,
1294 GtkBorder *inner_border,
1295 gboolean *interior_focus)
1297 GtkWidget *widget = GTK_WIDGET (button);
1298 GtkBorder *tmp_border;
1302 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1306 *default_border = *tmp_border;
1307 gtk_border_free (tmp_border);
1310 *default_border = default_default_border;
1313 if (default_outside_border)
1315 gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1319 *default_outside_border = *tmp_border;
1320 gtk_border_free (tmp_border);
1323 *default_outside_border = default_default_outside_border;
1328 gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1332 *inner_border = *tmp_border;
1333 gtk_border_free (tmp_border);
1336 *inner_border = default_inner_border;
1340 gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1344 gtk_button_size_allocate (GtkWidget *widget,
1345 GtkAllocation *allocation)
1347 GtkButton *button = GTK_BUTTON (widget);
1348 GtkAllocation child_allocation;
1350 gint border_width = GTK_CONTAINER (widget)->border_width;
1351 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
1352 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
1353 GtkBorder default_border;
1354 GtkBorder inner_border;
1358 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1359 gtk_widget_style_get (GTK_WIDGET (widget),
1360 "focus-line-width", &focus_width,
1361 "focus-padding", &focus_pad,
1365 widget->allocation = *allocation;
1367 if (gtk_widget_get_realized (widget))
1368 gdk_window_move_resize (button->event_window,
1369 widget->allocation.x + border_width,
1370 widget->allocation.y + border_width,
1371 widget->allocation.width - border_width * 2,
1372 widget->allocation.height - border_width * 2);
1374 if (GTK_BIN (button)->child && gtk_widget_get_visible (GTK_BIN (button)->child))
1376 child_allocation.x = widget->allocation.x + border_width + inner_border.left + xthickness;
1377 child_allocation.y = widget->allocation.y + border_width + inner_border.top + ythickness;
1379 child_allocation.width = MAX (1, widget->allocation.width -
1382 inner_border.right -
1384 child_allocation.height = MAX (1, widget->allocation.height -
1387 inner_border.bottom -
1390 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1392 child_allocation.x += default_border.left;
1393 child_allocation.y += default_border.top;
1394 child_allocation.width = MAX (1, child_allocation.width - default_border.left - default_border.right);
1395 child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
1398 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1400 child_allocation.x += focus_width + focus_pad;
1401 child_allocation.y += focus_width + focus_pad;
1402 child_allocation.width = MAX (1, child_allocation.width - (focus_width + focus_pad) * 2);
1403 child_allocation.height = MAX (1, child_allocation.height - (focus_width + focus_pad) * 2);
1406 if (button->depressed)
1408 gint child_displacement_x;
1409 gint child_displacement_y;
1411 gtk_widget_style_get (widget,
1412 "child-displacement-x", &child_displacement_x,
1413 "child-displacement-y", &child_displacement_y,
1415 child_allocation.x += child_displacement_x;
1416 child_allocation.y += child_displacement_y;
1419 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
1424 _gtk_button_paint (GtkButton *button,
1425 const GdkRectangle *area,
1426 GtkStateType state_type,
1427 GtkShadowType shadow_type,
1428 const gchar *main_detail,
1429 const gchar *default_detail)
1435 GtkBorder default_border;
1436 GtkBorder default_outside_border;
1437 gboolean interior_focus;
1441 widget = GTK_WIDGET (button);
1443 if (gtk_widget_is_drawable (widget))
1445 border_width = GTK_CONTAINER (widget)->border_width;
1447 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1448 gtk_widget_style_get (widget,
1449 "focus-line-width", &focus_width,
1450 "focus-padding", &focus_pad,
1453 x = widget->allocation.x + border_width;
1454 y = widget->allocation.y + border_width;
1455 width = widget->allocation.width - border_width * 2;
1456 height = widget->allocation.height - border_width * 2;
1458 if (gtk_widget_has_default (widget) &&
1459 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1461 gtk_paint_box (widget->style, widget->window,
1462 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1463 area, widget, "buttondefault",
1464 x, y, width, height);
1466 x += default_border.left;
1467 y += default_border.top;
1468 width -= default_border.left + default_border.right;
1469 height -= default_border.top + default_border.bottom;
1471 else if (gtk_widget_get_can_default (widget))
1473 x += default_outside_border.left;
1474 y += default_outside_border.top;
1475 width -= default_outside_border.left + default_outside_border.right;
1476 height -= default_outside_border.top + default_outside_border.bottom;
1479 if (!interior_focus && gtk_widget_has_focus (widget))
1481 x += focus_width + focus_pad;
1482 y += focus_width + focus_pad;
1483 width -= 2 * (focus_width + focus_pad);
1484 height -= 2 * (focus_width + focus_pad);
1487 if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1488 gtk_widget_get_state(widget) == GTK_STATE_PRELIGHT)
1489 gtk_paint_box (widget->style, widget->window,
1491 shadow_type, area, widget, "button",
1492 x, y, width, height);
1494 if (gtk_widget_has_focus (widget))
1496 gint child_displacement_x;
1497 gint child_displacement_y;
1498 gboolean displace_focus;
1500 gtk_widget_style_get (widget,
1501 "child-displacement-y", &child_displacement_y,
1502 "child-displacement-x", &child_displacement_x,
1503 "displace-focus", &displace_focus,
1508 x += widget->style->xthickness + focus_pad;
1509 y += widget->style->ythickness + focus_pad;
1510 width -= 2 * (widget->style->xthickness + focus_pad);
1511 height -= 2 * (widget->style->ythickness + focus_pad);
1515 x -= focus_width + focus_pad;
1516 y -= focus_width + focus_pad;
1517 width += 2 * (focus_width + focus_pad);
1518 height += 2 * (focus_width + focus_pad);
1521 if (button->depressed && displace_focus)
1523 x += child_displacement_x;
1524 y += child_displacement_y;
1527 gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
1528 area, widget, "button",
1529 x, y, width, height);
1535 gtk_button_expose (GtkWidget *widget,
1536 GdkEventExpose *event)
1538 if (gtk_widget_is_drawable (widget))
1540 GtkButton *button = GTK_BUTTON (widget);
1542 _gtk_button_paint (button, &event->area,
1543 gtk_widget_get_state (widget),
1544 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1545 "button", "buttondefault");
1547 GTK_WIDGET_CLASS (gtk_button_parent_class)->expose_event (widget, event);
1554 gtk_button_button_press (GtkWidget *widget,
1555 GdkEventButton *event)
1559 if (event->type == GDK_BUTTON_PRESS)
1561 button = GTK_BUTTON (widget);
1563 if (button->focus_on_click && !gtk_widget_has_focus (widget))
1564 gtk_widget_grab_focus (widget);
1566 if (event->button == 1)
1567 gtk_button_pressed (button);
1574 gtk_button_button_release (GtkWidget *widget,
1575 GdkEventButton *event)
1579 if (event->button == 1)
1581 button = GTK_BUTTON (widget);
1582 gtk_button_released (button);
1589 gtk_button_grab_broken (GtkWidget *widget,
1590 GdkEventGrabBroken *event)
1592 GtkButton *button = GTK_BUTTON (widget);
1595 /* Simulate a button release without the pointer in the button */
1596 if (button->button_down)
1598 save_in = button->in_button;
1599 button->in_button = FALSE;
1600 gtk_button_released (button);
1601 if (save_in != button->in_button)
1603 button->in_button = save_in;
1604 gtk_button_update_state (button);
1612 gtk_button_key_release (GtkWidget *widget,
1615 GtkButton *button = GTK_BUTTON (widget);
1617 if (button->activate_timeout)
1619 gtk_button_finish_activate (button, TRUE);
1622 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1623 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1629 gtk_button_enter_notify (GtkWidget *widget,
1630 GdkEventCrossing *event)
1633 GtkWidget *event_widget;
1635 button = GTK_BUTTON (widget);
1636 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1638 if ((event_widget == widget) &&
1639 (event->detail != GDK_NOTIFY_INFERIOR))
1641 button->in_button = TRUE;
1642 gtk_button_enter (button);
1649 gtk_button_leave_notify (GtkWidget *widget,
1650 GdkEventCrossing *event)
1653 GtkWidget *event_widget;
1655 button = GTK_BUTTON (widget);
1656 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1658 if ((event_widget == widget) &&
1659 (event->detail != GDK_NOTIFY_INFERIOR) &&
1660 (gtk_widget_get_sensitive (event_widget)))
1662 button->in_button = FALSE;
1663 gtk_button_leave (button);
1670 gtk_real_button_pressed (GtkButton *button)
1672 if (button->activate_timeout)
1675 button->button_down = TRUE;
1676 gtk_button_update_state (button);
1680 gtk_real_button_released (GtkButton *button)
1682 if (button->button_down)
1684 button->button_down = FALSE;
1686 if (button->activate_timeout)
1689 if (button->in_button)
1690 gtk_button_clicked (button);
1692 gtk_button_update_state (button);
1697 gtk_real_button_clicked (GtkButton *button)
1699 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1702 gtk_action_activate (priv->action);
1706 button_activate_timeout (gpointer data)
1708 gtk_button_finish_activate (data, TRUE);
1714 gtk_real_button_activate (GtkButton *button)
1716 GtkWidget *widget = GTK_WIDGET (button);
1717 GtkButtonPrivate *priv;
1721 priv = GTK_BUTTON_GET_PRIVATE (button);
1722 device = gtk_get_current_event_device ();
1724 g_return_if_fail (device && device->source == GDK_SOURCE_KEYBOARD);
1726 if (gtk_widget_get_realized (widget) && !button->activate_timeout)
1728 time = gtk_get_current_event_time ();
1730 if (gdk_device_grab (device, button->event_window,
1731 GDK_OWNERSHIP_WINDOW, TRUE,
1732 GDK_KEY_PRESS | GDK_KEY_RELEASE,
1733 NULL, time) == GDK_GRAB_SUCCESS)
1735 gtk_device_grab_add (widget, device, TRUE);
1736 priv->grab_keyboard = device;
1737 priv->grab_time = time;
1740 button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1741 button_activate_timeout,
1743 button->button_down = TRUE;
1744 gtk_button_update_state (button);
1745 gtk_widget_queue_draw (GTK_WIDGET (button));
1750 gtk_button_finish_activate (GtkButton *button,
1753 GtkWidget *widget = GTK_WIDGET (button);
1754 GtkButtonPrivate *priv;
1756 priv = GTK_BUTTON_GET_PRIVATE (button);
1758 g_source_remove (button->activate_timeout);
1759 button->activate_timeout = 0;
1761 if (priv->grab_keyboard)
1763 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1764 gtk_device_grab_remove (widget, priv->grab_keyboard);
1765 priv->grab_keyboard = NULL;
1768 button->button_down = FALSE;
1770 gtk_button_update_state (button);
1771 gtk_widget_queue_draw (GTK_WIDGET (button));
1774 gtk_button_clicked (button);
1779 gtk_button_extended_layout_init (GtkExtendedLayoutIface *iface)
1781 iface->get_desired_width = gtk_button_get_desired_width;
1782 iface->get_desired_height = gtk_button_get_desired_height;
1786 gtk_button_get_desired_size (GtkExtendedLayout *layout,
1787 GtkOrientation orientation,
1791 GtkButton *button = GTK_BUTTON (layout);
1793 GtkBorder default_border;
1794 GtkBorder inner_border;
1797 gint minimum, natural;
1799 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1800 gtk_widget_style_get (GTK_WIDGET (layout),
1801 "focus-line-width", &focus_width,
1802 "focus-padding", &focus_pad,
1805 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1807 minimum = ((GTK_CONTAINER (layout)->border_width +
1808 GTK_WIDGET (layout)->style->xthickness) * 2 +
1809 inner_border.left + inner_border.right);
1811 if (gtk_widget_get_can_default (GTK_WIDGET (layout)))
1812 minimum += default_border.left + default_border.right;
1816 minimum = ((GTK_CONTAINER (layout)->border_width +
1817 GTK_WIDGET (layout)->style->ythickness) * 2 +
1818 inner_border.top + inner_border.bottom);
1820 if (gtk_widget_get_can_default (GTK_WIDGET (layout)))
1821 minimum += default_border.top + default_border.bottom;
1824 minimum += 2 * (focus_width + focus_pad);
1827 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1828 gtk_widget_get_visible (child))
1830 gint child_min, child_nat;
1832 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1833 gtk_extended_layout_get_desired_width (GTK_EXTENDED_LAYOUT (child),
1834 &child_min, &child_nat);
1836 gtk_extended_layout_get_desired_height (GTK_EXTENDED_LAYOUT (child),
1837 &child_min, &child_nat);
1839 minimum += child_min;
1840 natural += child_nat;
1844 *minimum_size = minimum;
1847 *natural_size = natural;
1851 gtk_button_get_desired_width (GtkExtendedLayout *layout,
1855 gtk_button_get_desired_size (layout, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1859 gtk_button_get_desired_height (GtkExtendedLayout *layout,
1863 gtk_button_get_desired_size (layout, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1867 * gtk_button_set_label:
1868 * @button: a #GtkButton
1871 * Sets the text of the label of the button to @str. This text is
1872 * also used to select the stock item if gtk_button_set_use_stock()
1875 * This will also clear any previously set labels.
1878 gtk_button_set_label (GtkButton *button,
1883 g_return_if_fail (GTK_IS_BUTTON (button));
1885 new_label = g_strdup (label);
1886 g_free (button->label_text);
1887 button->label_text = new_label;
1889 gtk_button_construct_child (button);
1891 g_object_notify (G_OBJECT (button), "label");
1895 * gtk_button_get_label:
1896 * @button: a #GtkButton
1898 * Fetches the text from the label of the button, as set by
1899 * gtk_button_set_label(). If the label text has not
1900 * been set the return value will be %NULL. This will be the
1901 * case if you create an empty button with gtk_button_new() to
1902 * use as a container.
1904 * Return value: The text of the label widget. This string is owned
1905 * by the widget and must not be modified or freed.
1907 G_CONST_RETURN gchar *
1908 gtk_button_get_label (GtkButton *button)
1910 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1912 return button->label_text;
1916 * gtk_button_set_use_underline:
1917 * @button: a #GtkButton
1918 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1920 * If true, an underline in the text of the button label indicates
1921 * the next character should be used for the mnemonic accelerator key.
1924 gtk_button_set_use_underline (GtkButton *button,
1925 gboolean use_underline)
1927 g_return_if_fail (GTK_IS_BUTTON (button));
1929 use_underline = use_underline != FALSE;
1931 if (use_underline != button->use_underline)
1933 button->use_underline = use_underline;
1935 gtk_button_construct_child (button);
1937 g_object_notify (G_OBJECT (button), "use-underline");
1942 * gtk_button_get_use_underline:
1943 * @button: a #GtkButton
1945 * Returns whether an embedded underline in the button label indicates a
1946 * mnemonic. See gtk_button_set_use_underline ().
1948 * Return value: %TRUE if an embedded underline in the button label
1949 * indicates the mnemonic accelerator keys.
1952 gtk_button_get_use_underline (GtkButton *button)
1954 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1956 return button->use_underline;
1960 * gtk_button_set_use_stock:
1961 * @button: a #GtkButton
1962 * @use_stock: %TRUE if the button should use a stock item
1964 * If %TRUE, the label set on the button is used as a
1965 * stock id to select the stock item for the button.
1968 gtk_button_set_use_stock (GtkButton *button,
1971 g_return_if_fail (GTK_IS_BUTTON (button));
1973 use_stock = use_stock != FALSE;
1975 if (use_stock != button->use_stock)
1977 button->use_stock = use_stock;
1979 gtk_button_construct_child (button);
1981 g_object_notify (G_OBJECT (button), "use-stock");
1986 * gtk_button_get_use_stock:
1987 * @button: a #GtkButton
1989 * Returns whether the button label is a stock item.
1991 * Return value: %TRUE if the button label is used to
1992 * select a stock item instead of being
1993 * used directly as the label text.
1996 gtk_button_get_use_stock (GtkButton *button)
1998 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2000 return button->use_stock;
2004 * gtk_button_set_focus_on_click:
2005 * @button: a #GtkButton
2006 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2008 * Sets whether the button will grab focus when it is clicked with the mouse.
2009 * Making mouse clicks not grab focus is useful in places like toolbars where
2010 * you don't want the keyboard focus removed from the main area of the
2016 gtk_button_set_focus_on_click (GtkButton *button,
2017 gboolean focus_on_click)
2019 g_return_if_fail (GTK_IS_BUTTON (button));
2021 focus_on_click = focus_on_click != FALSE;
2023 if (button->focus_on_click != focus_on_click)
2025 button->focus_on_click = focus_on_click;
2027 g_object_notify (G_OBJECT (button), "focus-on-click");
2032 * gtk_button_get_focus_on_click:
2033 * @button: a #GtkButton
2035 * Returns whether the button grabs focus when it is clicked with the mouse.
2036 * See gtk_button_set_focus_on_click().
2038 * Return value: %TRUE if the button grabs focus when it is clicked with
2044 gtk_button_get_focus_on_click (GtkButton *button)
2046 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2048 return button->focus_on_click;
2052 * gtk_button_set_alignment:
2053 * @button: a #GtkButton
2054 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2055 * 1.0 is right aligned
2056 * @yalign: the vertical position of the child, 0.0 is top aligned,
2057 * 1.0 is bottom aligned
2059 * Sets the alignment of the child. This property has no effect unless
2060 * the child is a #GtkMisc or a #GtkAligment.
2065 gtk_button_set_alignment (GtkButton *button,
2069 GtkButtonPrivate *priv;
2071 g_return_if_fail (GTK_IS_BUTTON (button));
2073 priv = GTK_BUTTON_GET_PRIVATE (button);
2075 priv->xalign = xalign;
2076 priv->yalign = yalign;
2077 priv->align_set = 1;
2079 maybe_set_alignment (button, GTK_BIN (button)->child);
2081 g_object_freeze_notify (G_OBJECT (button));
2082 g_object_notify (G_OBJECT (button), "xalign");
2083 g_object_notify (G_OBJECT (button), "yalign");
2084 g_object_thaw_notify (G_OBJECT (button));
2088 * gtk_button_get_alignment:
2089 * @button: a #GtkButton
2090 * @xalign: return location for horizontal alignment
2091 * @yalign: return location for vertical alignment
2093 * Gets the alignment of the child in the button.
2098 gtk_button_get_alignment (GtkButton *button,
2102 GtkButtonPrivate *priv;
2104 g_return_if_fail (GTK_IS_BUTTON (button));
2106 priv = GTK_BUTTON_GET_PRIVATE (button);
2109 *xalign = priv->xalign;
2112 *yalign = priv->yalign;
2116 * _gtk_button_set_depressed:
2117 * @button: a #GtkButton
2118 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2120 * Sets whether the button is currently drawn as down or not. This is
2121 * purely a visual setting, and is meant only for use by derived widgets
2122 * such as #GtkToggleButton.
2125 _gtk_button_set_depressed (GtkButton *button,
2128 GtkWidget *widget = GTK_WIDGET (button);
2130 depressed = depressed != FALSE;
2132 if (depressed != button->depressed)
2134 button->depressed = depressed;
2135 gtk_widget_queue_resize (widget);
2140 gtk_button_update_state (GtkButton *button)
2143 GtkStateType new_state;
2145 if (button->activate_timeout)
2146 depressed = button->depress_on_activate;
2148 depressed = button->in_button && button->button_down;
2150 if (button->in_button && (!button->button_down || !depressed))
2151 new_state = GTK_STATE_PRELIGHT;
2153 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2155 _gtk_button_set_depressed (button, depressed);
2156 gtk_widget_set_state (GTK_WIDGET (button), new_state);
2160 show_image_change_notify (GtkButton *button)
2162 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2166 if (show_image (button))
2167 gtk_widget_show (priv->image);
2169 gtk_widget_hide (priv->image);
2174 traverse_container (GtkWidget *widget,
2177 if (GTK_IS_BUTTON (widget))
2178 show_image_change_notify (GTK_BUTTON (widget));
2179 else if (GTK_IS_CONTAINER (widget))
2180 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2184 gtk_button_setting_changed (GtkSettings *settings)
2188 list = gtk_window_list_toplevels ();
2190 for (l = list; l; l = l->next)
2191 gtk_container_forall (GTK_CONTAINER (l->data),
2192 traverse_container, NULL);
2199 gtk_button_screen_changed (GtkWidget *widget,
2200 GdkScreen *previous_screen)
2203 GtkSettings *settings;
2204 guint show_image_connection;
2206 if (!gtk_widget_has_screen (widget))
2209 button = GTK_BUTTON (widget);
2211 /* If the button is being pressed while the screen changes the
2212 release might never occur, so we reset the state. */
2213 if (button->button_down)
2215 button->button_down = FALSE;
2216 gtk_button_update_state (button);
2219 settings = gtk_widget_get_settings (widget);
2221 show_image_connection =
2222 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
2223 "gtk-button-connection"));
2225 if (show_image_connection)
2228 show_image_connection =
2229 g_signal_connect (settings, "notify::gtk-button-images",
2230 G_CALLBACK (gtk_button_setting_changed), NULL);
2231 g_object_set_data (G_OBJECT (settings),
2232 I_("gtk-button-connection"),
2233 GUINT_TO_POINTER (show_image_connection));
2235 show_image_change_notify (button);
2239 gtk_button_state_changed (GtkWidget *widget,
2240 GtkStateType previous_state)
2242 GtkButton *button = GTK_BUTTON (widget);
2244 if (!gtk_widget_is_sensitive (widget))
2246 button->in_button = FALSE;
2247 gtk_real_button_released (button);
2252 gtk_button_grab_notify (GtkWidget *widget,
2253 gboolean was_grabbed)
2255 GtkButton *button = GTK_BUTTON (widget);
2256 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2259 if (button->activate_timeout &&
2260 priv->grab_keyboard &&
2261 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2262 gtk_button_finish_activate (button, FALSE);
2266 save_in = button->in_button;
2267 button->in_button = FALSE;
2268 gtk_real_button_released (button);
2269 if (save_in != button->in_button)
2271 button->in_button = save_in;
2272 gtk_button_update_state (button);
2278 * gtk_button_set_image:
2279 * @button: a #GtkButton
2280 * @image: a widget to set as the image for the button
2282 * Set the image of @button to the given widget. Note that
2283 * it depends on the #GtkSettings:gtk-button-images setting whether the
2284 * image will be displayed or not, you don't have to call
2285 * gtk_widget_show() on @image yourself.
2290 gtk_button_set_image (GtkButton *button,
2293 GtkButtonPrivate *priv;
2295 g_return_if_fail (GTK_IS_BUTTON (button));
2296 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2298 priv = GTK_BUTTON_GET_PRIVATE (button);
2300 if (priv->image && priv->image->parent)
2301 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
2303 priv->image = image;
2304 priv->image_is_stock = (image == NULL);
2306 gtk_button_construct_child (button);
2308 g_object_notify (G_OBJECT (button), "image");
2312 * gtk_button_get_image:
2313 * @button: a #GtkButton
2315 * Gets the widget that is currenty set as the image of @button.
2316 * This may have been explicitly set by gtk_button_set_image()
2317 * or constructed by gtk_button_new_from_stock().
2319 * Return value: a #GtkWidget or %NULL in case there is no image
2324 gtk_button_get_image (GtkButton *button)
2326 GtkButtonPrivate *priv;
2328 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2330 priv = GTK_BUTTON_GET_PRIVATE (button);
2336 * gtk_button_set_image_position:
2337 * @button: a #GtkButton
2338 * @position: the position
2340 * Sets the position of the image relative to the text
2341 * inside the button.
2346 gtk_button_set_image_position (GtkButton *button,
2347 GtkPositionType position)
2350 GtkButtonPrivate *priv;
2352 g_return_if_fail (GTK_IS_BUTTON (button));
2353 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2355 priv = GTK_BUTTON_GET_PRIVATE (button);
2357 if (priv->image_position != position)
2359 priv->image_position = position;
2361 gtk_button_construct_child (button);
2363 g_object_notify (G_OBJECT (button), "image-position");
2368 * gtk_button_get_image_position:
2369 * @button: a #GtkButton
2371 * Gets the position of the image relative to the text
2372 * inside the button.
2374 * Return value: the position
2379 gtk_button_get_image_position (GtkButton *button)
2381 GtkButtonPrivate *priv;
2383 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2385 priv = GTK_BUTTON_GET_PRIVATE (button);
2387 return priv->image_position;
2391 #define __GTK_BUTTON_C__
2392 #include "gtkaliasdef.c"