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 (GtkObject *object);
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_expose (GtkWidget * widget, GdkEventExpose * event);
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_size_request_init (GtkSizeRequestIface *iface);
176 static void gtk_button_get_width (GtkSizeRequest *widget,
179 static void gtk_button_get_height (GtkSizeRequest *widget,
183 static guint button_signals[LAST_SIGNAL] = { 0 };
185 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
186 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
187 gtk_button_activatable_interface_init)
188 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
189 gtk_button_size_request_init))
192 gtk_button_class_init (GtkButtonClass *klass)
194 GObjectClass *gobject_class;
195 GtkObjectClass *object_class;
196 GtkWidgetClass *widget_class;
197 GtkContainerClass *container_class;
199 gobject_class = G_OBJECT_CLASS (klass);
200 object_class = (GtkObjectClass*) klass;
201 widget_class = (GtkWidgetClass*) klass;
202 container_class = (GtkContainerClass*) klass;
204 gobject_class->constructor = gtk_button_constructor;
205 gobject_class->dispose = gtk_button_dispose;
206 gobject_class->set_property = gtk_button_set_property;
207 gobject_class->get_property = gtk_button_get_property;
209 object_class->destroy = gtk_button_destroy;
211 widget_class->screen_changed = gtk_button_screen_changed;
212 widget_class->realize = gtk_button_realize;
213 widget_class->unrealize = gtk_button_unrealize;
214 widget_class->map = gtk_button_map;
215 widget_class->unmap = gtk_button_unmap;
216 widget_class->style_set = gtk_button_style_set;
217 widget_class->size_allocate = gtk_button_size_allocate;
218 widget_class->expose_event = gtk_button_expose;
219 widget_class->button_press_event = gtk_button_button_press;
220 widget_class->button_release_event = gtk_button_button_release;
221 widget_class->grab_broken_event = gtk_button_grab_broken;
222 widget_class->key_release_event = gtk_button_key_release;
223 widget_class->enter_notify_event = gtk_button_enter_notify;
224 widget_class->leave_notify_event = gtk_button_leave_notify;
225 widget_class->state_changed = gtk_button_state_changed;
226 widget_class->grab_notify = gtk_button_grab_notify;
228 container_class->child_type = gtk_button_child_type;
229 container_class->add = gtk_button_add;
230 gtk_container_class_handle_border_width (container_class);
232 klass->pressed = gtk_real_button_pressed;
233 klass->released = gtk_real_button_released;
234 klass->clicked = NULL;
235 klass->enter = gtk_button_update_state;
236 klass->leave = gtk_button_update_state;
237 klass->activate = gtk_real_button_activate;
239 g_object_class_install_property (gobject_class,
241 g_param_spec_string ("label",
243 P_("Text of the label widget inside the button, if the button contains a label widget"),
245 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
247 g_object_class_install_property (gobject_class,
249 g_param_spec_boolean ("use-underline",
251 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
253 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
255 g_object_class_install_property (gobject_class,
257 g_param_spec_boolean ("use-stock",
259 P_("If set, the label is used to pick a stock item instead of being displayed"),
261 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
263 g_object_class_install_property (gobject_class,
265 g_param_spec_boolean ("focus-on-click",
266 P_("Focus on click"),
267 P_("Whether the button grabs focus when it is clicked with the mouse"),
269 GTK_PARAM_READWRITE));
271 g_object_class_install_property (gobject_class,
273 g_param_spec_enum ("relief",
275 P_("The border relief style"),
276 GTK_TYPE_RELIEF_STYLE,
278 GTK_PARAM_READWRITE));
283 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
284 * can be used to control it's horizontal alignment. 0.0 is left aligned,
285 * 1.0 is right aligned.
289 g_object_class_install_property (gobject_class,
291 g_param_spec_float("xalign",
292 P_("Horizontal alignment for child"),
293 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
297 GTK_PARAM_READWRITE));
302 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
303 * can be used to control it's vertical alignment. 0.0 is top aligned,
304 * 1.0 is bottom aligned.
308 g_object_class_install_property (gobject_class,
310 g_param_spec_float("yalign",
311 P_("Vertical alignment for child"),
312 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
316 GTK_PARAM_READWRITE));
321 * The child widget to appear next to the button text.
325 g_object_class_install_property (gobject_class,
327 g_param_spec_object ("image",
329 P_("Child widget to appear next to the button text"),
331 GTK_PARAM_READWRITE));
334 * GtkButton:image-position:
336 * The position of the image relative to the text inside the button.
340 g_object_class_install_property (gobject_class,
342 g_param_spec_enum ("image-position",
343 P_("Image position"),
344 P_("The position of the image relative to the text"),
345 GTK_TYPE_POSITION_TYPE,
347 GTK_PARAM_READWRITE));
349 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
350 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
353 * GtkButton::pressed:
354 * @button: the object that received the signal
356 * Emitted when the button is pressed.
358 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
360 button_signals[PRESSED] =
361 g_signal_new (I_("pressed"),
362 G_OBJECT_CLASS_TYPE (object_class),
364 G_STRUCT_OFFSET (GtkButtonClass, pressed),
366 _gtk_marshal_VOID__VOID,
370 * GtkButton::released:
371 * @button: the object that received the signal
373 * Emitted when the button is released.
375 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
377 button_signals[RELEASED] =
378 g_signal_new (I_("released"),
379 G_OBJECT_CLASS_TYPE (object_class),
381 G_STRUCT_OFFSET (GtkButtonClass, released),
383 _gtk_marshal_VOID__VOID,
387 * GtkButton::clicked:
388 * @button: the object that received the signal
390 * Emitted when the button has been activated (pressed and released).
392 button_signals[CLICKED] =
393 g_signal_new (I_("clicked"),
394 G_OBJECT_CLASS_TYPE (object_class),
395 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
396 G_STRUCT_OFFSET (GtkButtonClass, clicked),
398 _gtk_marshal_VOID__VOID,
403 * @button: the object that received the signal
405 * Emitted when the pointer enters the button.
407 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
409 button_signals[ENTER] =
410 g_signal_new (I_("enter"),
411 G_OBJECT_CLASS_TYPE (object_class),
413 G_STRUCT_OFFSET (GtkButtonClass, enter),
415 _gtk_marshal_VOID__VOID,
420 * @button: the object that received the signal
422 * Emitted when the pointer leaves the button.
424 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
426 button_signals[LEAVE] =
427 g_signal_new (I_("leave"),
428 G_OBJECT_CLASS_TYPE (object_class),
430 G_STRUCT_OFFSET (GtkButtonClass, leave),
432 _gtk_marshal_VOID__VOID,
436 * GtkButton::activate:
437 * @widget: the object which received the signal.
439 * The ::activate signal on GtkButton is an action signal and
440 * emitting it causes the button to animate press then release.
441 * Applications should never connect to this signal, but use the
442 * #GtkButton::clicked signal.
444 button_signals[ACTIVATE] =
445 g_signal_new (I_("activate"),
446 G_OBJECT_CLASS_TYPE (object_class),
447 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
448 G_STRUCT_OFFSET (GtkButtonClass, activate),
450 _gtk_marshal_VOID__VOID,
452 widget_class->activate_signal = button_signals[ACTIVATE];
455 * GtkButton:default-border:
457 * The "default-border" style property defines the extra space to add
458 * around a button that can become the default widget of its window.
459 * 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-border",
464 P_("Default Spacing"),
465 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
467 GTK_PARAM_READABLE));
470 * GtkButton:default-outside-border:
472 * The "default-outside-border" style property defines the extra outside
473 * space to add around a button that can become the default widget of its
474 * window. Extra outside space is always drawn outside the button border.
475 * For more information about default widgets, see gtk_widget_grab_default().
477 gtk_widget_class_install_style_property (widget_class,
478 g_param_spec_boxed ("default-outside-border",
479 P_("Default Outside Spacing"),
480 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
482 GTK_PARAM_READABLE));
483 gtk_widget_class_install_style_property (widget_class,
484 g_param_spec_int ("child-displacement-x",
485 P_("Child X Displacement"),
486 P_("How far in the x direction to move the child when the button is depressed"),
490 GTK_PARAM_READABLE));
491 gtk_widget_class_install_style_property (widget_class,
492 g_param_spec_int ("child-displacement-y",
493 P_("Child Y Displacement"),
494 P_("How far in the y direction to move the child when the button is depressed"),
498 GTK_PARAM_READABLE));
501 * GtkButton:displace-focus:
503 * Whether the child_displacement_x/child_displacement_y properties
504 * should also affect the focus rectangle.
508 gtk_widget_class_install_style_property (widget_class,
509 g_param_spec_boolean ("displace-focus",
510 P_("Displace focus"),
511 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
513 GTK_PARAM_READABLE));
516 * GtkButton:inner-border:
518 * Sets the border between the button edges and child.
522 gtk_widget_class_install_style_property (widget_class,
523 g_param_spec_boxed ("inner-border",
525 P_("Border between button edges and child."),
527 GTK_PARAM_READABLE));
530 * GtkButton::image-spacing:
532 * Spacing in pixels between the image and label.
536 gtk_widget_class_install_style_property (widget_class,
537 g_param_spec_int ("image-spacing",
539 P_("Spacing in pixels between the image and label"),
543 GTK_PARAM_READABLE));
546 * GtkSettings::gtk-button-images:
548 * Whether images should be shown on buttons
552 gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
553 P_("Show button images"),
554 P_("Whether images should be shown on buttons"),
556 GTK_PARAM_READWRITE));
558 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
562 gtk_button_init (GtkButton *button)
564 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
566 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
567 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
568 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
570 button->label_text = NULL;
572 button->constructed = FALSE;
573 button->in_button = FALSE;
574 button->button_down = FALSE;
575 button->relief = GTK_RELIEF_NORMAL;
576 button->use_stock = FALSE;
577 button->use_underline = FALSE;
578 button->depressed = FALSE;
579 button->depress_on_activate = TRUE;
580 button->focus_on_click = TRUE;
585 priv->image_is_stock = TRUE;
586 priv->image_position = GTK_POS_LEFT;
587 priv->use_action_appearance = TRUE;
591 gtk_button_destroy (GtkObject *object)
593 GtkButton *button = GTK_BUTTON (object);
595 if (button->label_text)
597 g_free (button->label_text);
598 button->label_text = NULL;
601 GTK_OBJECT_CLASS (gtk_button_parent_class)->destroy (object);
605 gtk_button_constructor (GType type,
606 guint n_construct_properties,
607 GObjectConstructParam *construct_params)
612 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
613 n_construct_properties,
616 button = GTK_BUTTON (object);
617 button->constructed = TRUE;
619 if (button->label_text != NULL)
620 gtk_button_construct_child (button);
627 gtk_button_child_type (GtkContainer *container)
629 if (!gtk_bin_get_child (GTK_BIN (container)))
630 return GTK_TYPE_WIDGET;
636 maybe_set_alignment (GtkButton *button,
639 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
641 if (GTK_IS_MISC (widget))
643 GtkMisc *misc = GTK_MISC (widget);
646 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
648 else if (GTK_IS_ALIGNMENT (widget))
650 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
651 gfloat xscale, yscale;
653 g_object_get (alignment,
659 gtk_alignment_set (alignment,
660 priv->xalign, priv->yalign,
666 gtk_button_add (GtkContainer *container,
669 maybe_set_alignment (GTK_BUTTON (container), widget);
671 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
675 gtk_button_dispose (GObject *object)
677 GtkButton *button = GTK_BUTTON (object);
678 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
682 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
685 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
689 gtk_button_set_property (GObject *object,
694 GtkButton *button = GTK_BUTTON (object);
695 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
700 gtk_button_set_label (button, g_value_get_string (value));
703 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
706 gtk_button_set_relief (button, g_value_get_enum (value));
708 case PROP_USE_UNDERLINE:
709 gtk_button_set_use_underline (button, g_value_get_boolean (value));
712 gtk_button_set_use_stock (button, g_value_get_boolean (value));
714 case PROP_FOCUS_ON_CLICK:
715 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
718 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
721 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
723 case PROP_IMAGE_POSITION:
724 gtk_button_set_image_position (button, g_value_get_enum (value));
726 case PROP_ACTIVATABLE_RELATED_ACTION:
727 gtk_button_set_related_action (button, g_value_get_object (value));
729 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
730 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
733 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
739 gtk_button_get_property (GObject *object,
744 GtkButton *button = GTK_BUTTON (object);
745 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
750 g_value_set_string (value, button->label_text);
753 g_value_set_object (value, (GObject *)priv->image);
756 g_value_set_enum (value, gtk_button_get_relief (button));
758 case PROP_USE_UNDERLINE:
759 g_value_set_boolean (value, button->use_underline);
762 g_value_set_boolean (value, button->use_stock);
764 case PROP_FOCUS_ON_CLICK:
765 g_value_set_boolean (value, button->focus_on_click);
768 g_value_set_float (value, priv->xalign);
771 g_value_set_float (value, priv->yalign);
773 case PROP_IMAGE_POSITION:
774 g_value_set_enum (value, priv->image_position);
776 case PROP_ACTIVATABLE_RELATED_ACTION:
777 g_value_set_object (value, priv->action);
779 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
780 g_value_set_boolean (value, priv->use_action_appearance);
783 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
789 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
791 iface->update = gtk_button_update;
792 iface->sync_action_properties = gtk_button_sync_action_properties;
796 activatable_update_stock_id (GtkButton *button,
799 if (!gtk_button_get_use_stock (button))
802 gtk_button_set_label (button, gtk_action_get_stock_id (action));
806 activatable_update_short_label (GtkButton *button,
812 if (gtk_button_get_use_stock (button))
815 image = gtk_button_get_image (button);
817 /* Dont touch custom child... */
818 child = gtk_bin_get_child (GTK_BIN (button));
819 if (GTK_IS_IMAGE (image) ||
821 GTK_IS_LABEL (child))
823 gtk_button_set_label (button, gtk_action_get_short_label (action));
824 gtk_button_set_use_underline (button, TRUE);
829 activatable_update_icon_name (GtkButton *button,
834 if (gtk_button_get_use_stock (button))
837 image = gtk_button_get_image (button);
839 if (GTK_IS_IMAGE (image) &&
840 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
841 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
842 gtk_image_set_from_icon_name (GTK_IMAGE (image),
843 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
847 activatable_update_gicon (GtkButton *button,
850 GtkWidget *image = gtk_button_get_image (button);
851 GIcon *icon = gtk_action_get_gicon (action);
853 if (GTK_IS_IMAGE (image) &&
854 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
855 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
856 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
860 gtk_button_update (GtkActivatable *activatable,
862 const gchar *property_name)
864 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
866 if (strcmp (property_name, "visible") == 0)
868 if (gtk_action_is_visible (action))
869 gtk_widget_show (GTK_WIDGET (activatable));
871 gtk_widget_hide (GTK_WIDGET (activatable));
873 else if (strcmp (property_name, "sensitive") == 0)
874 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
876 if (!priv->use_action_appearance)
879 if (strcmp (property_name, "stock-id") == 0)
880 activatable_update_stock_id (GTK_BUTTON (activatable), action);
881 else if (strcmp (property_name, "gicon") == 0)
882 activatable_update_gicon (GTK_BUTTON (activatable), action);
883 else if (strcmp (property_name, "short-label") == 0)
884 activatable_update_short_label (GTK_BUTTON (activatable), action);
885 else if (strcmp (property_name, "icon-name") == 0)
886 activatable_update_icon_name (GTK_BUTTON (activatable), action);
890 gtk_button_sync_action_properties (GtkActivatable *activatable,
893 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
898 if (gtk_action_is_visible (action))
899 gtk_widget_show (GTK_WIDGET (activatable));
901 gtk_widget_hide (GTK_WIDGET (activatable));
903 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
905 if (priv->use_action_appearance)
907 activatable_update_stock_id (GTK_BUTTON (activatable), action);
908 activatable_update_short_label (GTK_BUTTON (activatable), action);
909 activatable_update_gicon (GTK_BUTTON (activatable), action);
910 activatable_update_icon_name (GTK_BUTTON (activatable), action);
915 gtk_button_set_related_action (GtkButton *button,
918 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
920 if (priv->action == action)
923 /* This should be a default handler, but for compatibility reasons
924 * we need to support derived classes that don't chain up their
927 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
929 g_signal_connect_after (button, "clicked",
930 G_CALLBACK (gtk_real_button_clicked), NULL);
932 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
934 priv->action = action;
938 gtk_button_set_use_action_appearance (GtkButton *button,
939 gboolean use_appearance)
941 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
943 if (priv->use_action_appearance != use_appearance)
945 priv->use_action_appearance = use_appearance;
947 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
954 * Creates a new #GtkButton widget. To add a child widget to the button,
955 * use gtk_container_add().
957 * Returns: The newly created #GtkButton widget.
960 gtk_button_new (void)
962 return g_object_new (GTK_TYPE_BUTTON, NULL);
966 show_image (GtkButton *button)
970 if (button->label_text)
972 GtkSettings *settings;
974 settings = gtk_widget_get_settings (GTK_WIDGET (button));
975 g_object_get (settings, "gtk-button-images", &show, NULL);
984 gtk_button_construct_child (GtkButton *button)
986 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
992 GtkWidget *image = NULL;
993 gchar *label_text = NULL;
996 if (!button->constructed)
999 if (!button->label_text && !priv->image)
1002 gtk_widget_style_get (GTK_WIDGET (button),
1003 "image-spacing", &image_spacing,
1006 if (priv->image && !priv->image_is_stock)
1010 image = g_object_ref (priv->image);
1012 parent = gtk_widget_get_parent (image);
1014 gtk_container_remove (GTK_CONTAINER (parent), image);
1019 child = gtk_bin_get_child (GTK_BIN (button));
1021 gtk_container_remove (GTK_CONTAINER (button), child);
1023 if (button->use_stock &&
1024 button->label_text &&
1025 gtk_stock_lookup (button->label_text, &item))
1028 image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
1030 label_text = item.label;
1033 label_text = button->label_text;
1037 priv->image = image;
1038 g_object_set (priv->image,
1039 "visible", show_image (button),
1040 "no-show-all", TRUE,
1043 if (priv->image_position == GTK_POS_LEFT ||
1044 priv->image_position == GTK_POS_RIGHT)
1045 box = gtk_hbox_new (FALSE, image_spacing);
1047 box = gtk_vbox_new (FALSE, image_spacing);
1049 if (priv->align_set)
1050 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1052 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1054 if (priv->image_position == GTK_POS_LEFT ||
1055 priv->image_position == GTK_POS_TOP)
1056 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1058 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1062 if (button->use_underline || button->use_stock)
1064 label = gtk_label_new_with_mnemonic (label_text);
1065 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1066 GTK_WIDGET (button));
1069 label = gtk_label_new (label_text);
1071 if (priv->image_position == GTK_POS_RIGHT ||
1072 priv->image_position == GTK_POS_BOTTOM)
1073 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1075 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1078 gtk_container_add (GTK_CONTAINER (button), align);
1079 gtk_container_add (GTK_CONTAINER (align), box);
1080 gtk_widget_show_all (align);
1082 g_object_unref (image);
1087 if (button->use_underline || button->use_stock)
1089 label = gtk_label_new_with_mnemonic (button->label_text);
1090 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1093 label = gtk_label_new (button->label_text);
1095 if (priv->align_set)
1096 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1098 gtk_widget_show (label);
1099 gtk_container_add (GTK_CONTAINER (button), label);
1104 * gtk_button_new_with_label:
1105 * @label: The text you want the #GtkLabel to hold.
1107 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1110 * Returns: The newly created #GtkButton widget.
1113 gtk_button_new_with_label (const gchar *label)
1115 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1119 * gtk_button_new_from_stock:
1120 * @stock_id: the name of the stock item
1122 * Creates a new #GtkButton containing the image and text from a stock item.
1123 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1126 * If @stock_id is unknown, then it will be treated as a mnemonic
1127 * label (as for gtk_button_new_with_mnemonic()).
1129 * Returns: a new #GtkButton
1132 gtk_button_new_from_stock (const gchar *stock_id)
1134 return g_object_new (GTK_TYPE_BUTTON,
1137 "use-underline", TRUE,
1142 * gtk_button_new_with_mnemonic:
1143 * @label: The text of the button, with an underscore in front of the
1144 * mnemonic character
1145 * @returns: a new #GtkButton
1147 * Creates a new #GtkButton containing a label.
1148 * If characters in @label are preceded by an underscore, they are underlined.
1149 * If you need a literal underscore character in a label, use '__' (two
1150 * underscores). The first underlined character represents a keyboard
1151 * accelerator called a mnemonic.
1152 * Pressing Alt and that key activates the button.
1155 gtk_button_new_with_mnemonic (const gchar *label)
1157 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1161 * gtk_button_pressed:
1162 * @button: The #GtkButton you want to send the signal to.
1164 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1166 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1169 gtk_button_pressed (GtkButton *button)
1171 g_return_if_fail (GTK_IS_BUTTON (button));
1174 g_signal_emit (button, button_signals[PRESSED], 0);
1178 * gtk_button_released:
1179 * @button: The #GtkButton you want to send the signal to.
1181 * Emits a #GtkButton::released signal to the given #GtkButton.
1183 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1186 gtk_button_released (GtkButton *button)
1188 g_return_if_fail (GTK_IS_BUTTON (button));
1190 g_signal_emit (button, button_signals[RELEASED], 0);
1194 * gtk_button_clicked:
1195 * @button: The #GtkButton you want to send the signal to.
1197 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1200 gtk_button_clicked (GtkButton *button)
1202 g_return_if_fail (GTK_IS_BUTTON (button));
1204 g_signal_emit (button, button_signals[CLICKED], 0);
1209 * @button: The #GtkButton you want to send the signal to.
1211 * Emits a #GtkButton::enter signal to the given #GtkButton.
1213 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1216 gtk_button_enter (GtkButton *button)
1218 g_return_if_fail (GTK_IS_BUTTON (button));
1220 g_signal_emit (button, button_signals[ENTER], 0);
1225 * @button: The #GtkButton you want to send the signal to.
1227 * Emits a #GtkButton::leave signal to the given #GtkButton.
1229 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1232 gtk_button_leave (GtkButton *button)
1234 g_return_if_fail (GTK_IS_BUTTON (button));
1236 g_signal_emit (button, button_signals[LEAVE], 0);
1240 * gtk_button_set_relief:
1241 * @button: The #GtkButton you want to set relief styles of.
1242 * @newstyle: The GtkReliefStyle as described above.
1244 * Sets the relief style of the edges of the given #GtkButton widget.
1245 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1246 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1248 * <!-- FIXME: put pictures of each style -->
1251 gtk_button_set_relief (GtkButton *button,
1252 GtkReliefStyle newrelief)
1254 g_return_if_fail (GTK_IS_BUTTON (button));
1256 if (newrelief != button->relief)
1258 button->relief = newrelief;
1259 g_object_notify (G_OBJECT (button), "relief");
1260 gtk_widget_queue_draw (GTK_WIDGET (button));
1265 * gtk_button_get_relief:
1266 * @button: The #GtkButton you want the #GtkReliefStyle from.
1268 * Returns the current relief style of the given #GtkButton.
1270 * Returns: The current #GtkReliefStyle
1273 gtk_button_get_relief (GtkButton *button)
1275 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1277 return button->relief;
1281 gtk_button_realize (GtkWidget *widget)
1283 GtkButton *button = GTK_BUTTON (widget);
1284 GtkAllocation allocation;
1286 GdkWindowAttr attributes;
1287 gint attributes_mask;
1289 gtk_widget_get_allocation (widget, &allocation);
1291 gtk_widget_set_realized (widget, TRUE);
1293 attributes.window_type = GDK_WINDOW_CHILD;
1294 attributes.x = allocation.x;
1295 attributes.y = allocation.y;
1296 attributes.width = allocation.width;
1297 attributes.height = allocation.height;
1298 attributes.wclass = GDK_INPUT_ONLY;
1299 attributes.event_mask = gtk_widget_get_events (widget);
1300 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1301 GDK_BUTTON_RELEASE_MASK |
1302 GDK_ENTER_NOTIFY_MASK |
1303 GDK_LEAVE_NOTIFY_MASK);
1305 attributes_mask = GDK_WA_X | GDK_WA_Y;
1307 window = gtk_widget_get_parent_window (widget);
1308 gtk_widget_set_window (widget, window);
1309 g_object_ref (window);
1311 button->event_window = gdk_window_new (window,
1312 &attributes, attributes_mask);
1313 gdk_window_set_user_data (button->event_window, button);
1315 gtk_widget_style_attach (widget);
1319 gtk_button_unrealize (GtkWidget *widget)
1321 GtkButton *button = GTK_BUTTON (widget);
1323 if (button->activate_timeout)
1324 gtk_button_finish_activate (button, FALSE);
1326 if (button->event_window)
1328 gdk_window_set_user_data (button->event_window, NULL);
1329 gdk_window_destroy (button->event_window);
1330 button->event_window = NULL;
1333 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1337 gtk_button_map (GtkWidget *widget)
1339 GtkButton *button = GTK_BUTTON (widget);
1341 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1343 if (button->event_window)
1344 gdk_window_show (button->event_window);
1348 gtk_button_unmap (GtkWidget *widget)
1350 GtkButton *button = GTK_BUTTON (widget);
1352 if (button->event_window)
1353 gdk_window_hide (button->event_window);
1355 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1359 gtk_button_update_image_spacing (GtkButton *button)
1361 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1365 /* Keep in sync with gtk_button_construct_child,
1366 * we only want to update the spacing if the box
1367 * was constructed there.
1369 if (!button->constructed || !priv->image)
1372 child = gtk_bin_get_child (GTK_BIN (button));
1373 if (GTK_IS_ALIGNMENT (child))
1375 child = gtk_bin_get_child (GTK_BIN (child));
1376 if (GTK_IS_BOX (child))
1378 gtk_widget_style_get (GTK_WIDGET (button),
1379 "image-spacing", &spacing,
1382 gtk_box_set_spacing (GTK_BOX (child), spacing);
1388 gtk_button_style_set (GtkWidget *widget,
1389 GtkStyle *prev_style)
1391 gtk_button_update_image_spacing (GTK_BUTTON (widget));
1395 gtk_button_get_props (GtkButton *button,
1396 GtkBorder *default_border,
1397 GtkBorder *default_outside_border,
1398 GtkBorder *inner_border,
1399 gboolean *interior_focus)
1401 GtkWidget *widget = GTK_WIDGET (button);
1402 GtkBorder *tmp_border;
1406 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1410 *default_border = *tmp_border;
1411 gtk_border_free (tmp_border);
1414 *default_border = default_default_border;
1417 if (default_outside_border)
1419 gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1423 *default_outside_border = *tmp_border;
1424 gtk_border_free (tmp_border);
1427 *default_outside_border = default_default_outside_border;
1432 gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1436 *inner_border = *tmp_border;
1437 gtk_border_free (tmp_border);
1440 *inner_border = default_inner_border;
1444 gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1448 gtk_button_size_allocate (GtkWidget *widget,
1449 GtkAllocation *allocation)
1451 GtkButton *button = GTK_BUTTON (widget);
1452 GtkAllocation child_allocation;
1456 gint xthickness, ythickness;
1457 GtkBorder default_border;
1458 GtkBorder inner_border;
1462 style = gtk_widget_get_style (widget);
1463 xthickness = style->xthickness;
1464 ythickness = style->ythickness;
1466 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1467 gtk_widget_style_get (GTK_WIDGET (widget),
1468 "focus-line-width", &focus_width,
1469 "focus-padding", &focus_pad,
1472 gtk_widget_set_allocation (widget, allocation);
1474 if (gtk_widget_get_realized (widget))
1475 gdk_window_move_resize (button->event_window,
1479 allocation->height);
1481 child = gtk_bin_get_child (GTK_BIN (button));
1482 if (child && gtk_widget_get_visible (child))
1484 child_allocation.x = allocation->x + inner_border.left + xthickness;
1485 child_allocation.y = allocation->y + inner_border.top + ythickness;
1487 child_allocation.width =
1493 child_allocation.height =
1494 allocation->height -
1497 inner_border.bottom;
1499 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1501 child_allocation.x += default_border.left;
1502 child_allocation.y += default_border.top;
1503 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1504 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1507 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1509 child_allocation.x += focus_width + focus_pad;
1510 child_allocation.y += focus_width + focus_pad;
1511 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1512 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1515 if (button->depressed)
1517 gint child_displacement_x;
1518 gint child_displacement_y;
1520 gtk_widget_style_get (widget,
1521 "child-displacement-x", &child_displacement_x,
1522 "child-displacement-y", &child_displacement_y,
1524 child_allocation.x += child_displacement_x;
1525 child_allocation.y += child_displacement_y;
1528 child_allocation.width = MAX (1, child_allocation.width);
1529 child_allocation.height = MAX (1, child_allocation.height);
1531 gtk_widget_size_allocate (child, &child_allocation);
1536 _gtk_button_paint (GtkButton *button,
1537 const GdkRectangle *area,
1538 GtkStateType state_type,
1539 GtkShadowType shadow_type,
1540 const gchar *main_detail,
1541 const gchar *default_detail)
1546 GtkBorder default_border;
1547 GtkBorder default_outside_border;
1548 gboolean interior_focus;
1551 GtkAllocation allocation;
1555 widget = GTK_WIDGET (button);
1557 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1558 gtk_widget_style_get (widget,
1559 "focus-line-width", &focus_width,
1560 "focus-padding", &focus_pad,
1563 gtk_widget_get_allocation (widget, &allocation);
1564 style = gtk_widget_get_style (widget);
1565 window = gtk_widget_get_window (widget);
1569 width = allocation.width;
1570 height = allocation.height;
1572 if (gtk_widget_has_default (widget) &&
1573 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1575 gtk_paint_box (style, window,
1576 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1577 area, widget, "buttondefault",
1578 x, y, width, height);
1580 x += default_border.left;
1581 y += default_border.top;
1582 width -= default_border.left + default_border.right;
1583 height -= default_border.top + default_border.bottom;
1585 else if (gtk_widget_get_can_default (widget))
1587 x += default_outside_border.left;
1588 y += default_outside_border.top;
1589 width -= default_outside_border.left + default_outside_border.right;
1590 height -= default_outside_border.top + default_outside_border.bottom;
1593 if (!interior_focus && gtk_widget_has_focus (widget))
1595 x += focus_width + focus_pad;
1596 y += focus_width + focus_pad;
1597 width -= 2 * (focus_width + focus_pad);
1598 height -= 2 * (focus_width + focus_pad);
1601 if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1602 gtk_widget_get_state(widget) == GTK_STATE_PRELIGHT)
1603 gtk_paint_box (style, window,
1605 shadow_type, area, widget, "button",
1606 x, y, width, height);
1608 if (gtk_widget_has_focus (widget))
1610 gint child_displacement_x;
1611 gint child_displacement_y;
1612 gboolean displace_focus;
1614 gtk_widget_style_get (widget,
1615 "child-displacement-y", &child_displacement_y,
1616 "child-displacement-x", &child_displacement_x,
1617 "displace-focus", &displace_focus,
1622 x += style->xthickness + focus_pad;
1623 y += style->ythickness + focus_pad;
1624 width -= 2 * (style->xthickness + focus_pad);
1625 height -= 2 * (style->ythickness + focus_pad);
1629 x -= focus_width + focus_pad;
1630 y -= focus_width + focus_pad;
1631 width += 2 * (focus_width + focus_pad);
1632 height += 2 * (focus_width + focus_pad);
1635 if (button->depressed && displace_focus)
1637 x += child_displacement_x;
1638 y += child_displacement_y;
1641 gtk_paint_focus (style, window,
1642 gtk_widget_get_state (widget),
1643 area, widget, "button",
1644 x, y, width, height);
1649 gtk_button_expose (GtkWidget *widget,
1650 GdkEventExpose *event)
1652 if (gtk_widget_is_drawable (widget))
1654 GtkButton *button = GTK_BUTTON (widget);
1656 _gtk_button_paint (button, &event->area,
1657 gtk_widget_get_state (widget),
1658 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1659 "button", "buttondefault");
1661 GTK_WIDGET_CLASS (gtk_button_parent_class)->expose_event (widget, event);
1668 gtk_button_button_press (GtkWidget *widget,
1669 GdkEventButton *event)
1673 if (event->type == GDK_BUTTON_PRESS)
1675 button = GTK_BUTTON (widget);
1677 if (button->focus_on_click && !gtk_widget_has_focus (widget))
1678 gtk_widget_grab_focus (widget);
1680 if (event->button == 1)
1681 gtk_button_pressed (button);
1688 gtk_button_button_release (GtkWidget *widget,
1689 GdkEventButton *event)
1693 if (event->button == 1)
1695 button = GTK_BUTTON (widget);
1696 gtk_button_released (button);
1703 gtk_button_grab_broken (GtkWidget *widget,
1704 GdkEventGrabBroken *event)
1706 GtkButton *button = GTK_BUTTON (widget);
1709 /* Simulate a button release without the pointer in the button */
1710 if (button->button_down)
1712 save_in = button->in_button;
1713 button->in_button = FALSE;
1714 gtk_button_released (button);
1715 if (save_in != button->in_button)
1717 button->in_button = save_in;
1718 gtk_button_update_state (button);
1726 gtk_button_key_release (GtkWidget *widget,
1729 GtkButton *button = GTK_BUTTON (widget);
1731 if (button->activate_timeout)
1733 gtk_button_finish_activate (button, TRUE);
1736 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1737 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1743 gtk_button_enter_notify (GtkWidget *widget,
1744 GdkEventCrossing *event)
1747 GtkWidget *event_widget;
1749 button = GTK_BUTTON (widget);
1750 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1752 if ((event_widget == widget) &&
1753 (event->detail != GDK_NOTIFY_INFERIOR))
1755 button->in_button = TRUE;
1756 gtk_button_enter (button);
1763 gtk_button_leave_notify (GtkWidget *widget,
1764 GdkEventCrossing *event)
1767 GtkWidget *event_widget;
1769 button = GTK_BUTTON (widget);
1770 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1772 if ((event_widget == widget) &&
1773 (event->detail != GDK_NOTIFY_INFERIOR) &&
1774 (gtk_widget_get_sensitive (event_widget)))
1776 button->in_button = FALSE;
1777 gtk_button_leave (button);
1784 gtk_real_button_pressed (GtkButton *button)
1786 if (button->activate_timeout)
1789 button->button_down = TRUE;
1790 gtk_button_update_state (button);
1794 gtk_real_button_released (GtkButton *button)
1796 if (button->button_down)
1798 button->button_down = FALSE;
1800 if (button->activate_timeout)
1803 if (button->in_button)
1804 gtk_button_clicked (button);
1806 gtk_button_update_state (button);
1811 gtk_real_button_clicked (GtkButton *button)
1813 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1816 gtk_action_activate (priv->action);
1820 button_activate_timeout (gpointer data)
1822 gtk_button_finish_activate (data, TRUE);
1828 gtk_real_button_activate (GtkButton *button)
1830 GtkWidget *widget = GTK_WIDGET (button);
1831 GtkButtonPrivate *priv;
1835 priv = GTK_BUTTON_GET_PRIVATE (button);
1836 device = gtk_get_current_event_device ();
1838 if (device && device->source != GDK_SOURCE_KEYBOARD)
1839 device = gdk_device_get_associated_device (device);
1841 g_return_if_fail (device && device->source == GDK_SOURCE_KEYBOARD);
1843 if (gtk_widget_get_realized (widget) && !button->activate_timeout)
1845 time = gtk_get_current_event_time ();
1847 if (gdk_device_grab (device, button->event_window,
1848 GDK_OWNERSHIP_WINDOW, TRUE,
1849 GDK_KEY_PRESS | GDK_KEY_RELEASE,
1850 NULL, time) == GDK_GRAB_SUCCESS)
1852 gtk_device_grab_add (widget, device, TRUE);
1853 priv->grab_keyboard = device;
1854 priv->grab_time = time;
1857 button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1858 button_activate_timeout,
1860 button->button_down = TRUE;
1861 gtk_button_update_state (button);
1862 gtk_widget_queue_draw (GTK_WIDGET (button));
1867 gtk_button_finish_activate (GtkButton *button,
1870 GtkWidget *widget = GTK_WIDGET (button);
1871 GtkButtonPrivate *priv;
1873 priv = GTK_BUTTON_GET_PRIVATE (button);
1875 g_source_remove (button->activate_timeout);
1876 button->activate_timeout = 0;
1878 if (priv->grab_keyboard)
1880 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1881 gtk_device_grab_remove (widget, priv->grab_keyboard);
1882 priv->grab_keyboard = NULL;
1885 button->button_down = FALSE;
1887 gtk_button_update_state (button);
1888 gtk_widget_queue_draw (GTK_WIDGET (button));
1891 gtk_button_clicked (button);
1896 gtk_button_size_request_init (GtkSizeRequestIface *iface)
1898 iface->get_width = gtk_button_get_width;
1899 iface->get_height = gtk_button_get_height;
1903 gtk_button_get_size (GtkSizeRequest *widget,
1904 GtkOrientation orientation,
1908 GtkButton *button = GTK_BUTTON (widget);
1911 GtkBorder default_border;
1912 GtkBorder inner_border;
1915 gint minimum, natural;
1917 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1918 gtk_widget_style_get (GTK_WIDGET (widget),
1919 "focus-line-width", &focus_width,
1920 "focus-padding", &focus_pad,
1923 style = gtk_widget_get_style (GTK_WIDGET (widget));
1925 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1927 minimum = (style->xthickness * 2 +
1928 inner_border.left + inner_border.right);
1930 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1931 minimum += default_border.left + default_border.right;
1935 minimum = (style->ythickness * 2 +
1936 inner_border.top + inner_border.bottom);
1938 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1939 minimum += default_border.top + default_border.bottom;
1942 minimum += 2 * (focus_width + focus_pad);
1945 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1946 gtk_widget_get_visible (child))
1948 gint child_min, child_nat;
1950 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1951 gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
1952 &child_min, &child_nat);
1954 gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
1955 &child_min, &child_nat);
1957 minimum += child_min;
1958 natural += child_nat;
1962 *minimum_size = minimum;
1965 *natural_size = natural;
1969 gtk_button_get_width (GtkSizeRequest *widget,
1973 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1977 gtk_button_get_height (GtkSizeRequest *widget,
1981 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1985 * gtk_button_set_label:
1986 * @button: a #GtkButton
1989 * Sets the text of the label of the button to @str. This text is
1990 * also used to select the stock item if gtk_button_set_use_stock()
1993 * This will also clear any previously set labels.
1996 gtk_button_set_label (GtkButton *button,
2001 g_return_if_fail (GTK_IS_BUTTON (button));
2003 new_label = g_strdup (label);
2004 g_free (button->label_text);
2005 button->label_text = new_label;
2007 gtk_button_construct_child (button);
2009 g_object_notify (G_OBJECT (button), "label");
2013 * gtk_button_get_label:
2014 * @button: a #GtkButton
2016 * Fetches the text from the label of the button, as set by
2017 * gtk_button_set_label(). If the label text has not
2018 * been set the return value will be %NULL. This will be the
2019 * case if you create an empty button with gtk_button_new() to
2020 * use as a container.
2022 * Return value: The text of the label widget. This string is owned
2023 * by the widget and must not be modified or freed.
2025 G_CONST_RETURN gchar *
2026 gtk_button_get_label (GtkButton *button)
2028 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2030 return button->label_text;
2034 * gtk_button_set_use_underline:
2035 * @button: a #GtkButton
2036 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2038 * If true, an underline in the text of the button label indicates
2039 * the next character should be used for the mnemonic accelerator key.
2042 gtk_button_set_use_underline (GtkButton *button,
2043 gboolean use_underline)
2045 g_return_if_fail (GTK_IS_BUTTON (button));
2047 use_underline = use_underline != FALSE;
2049 if (use_underline != button->use_underline)
2051 button->use_underline = use_underline;
2053 gtk_button_construct_child (button);
2055 g_object_notify (G_OBJECT (button), "use-underline");
2060 * gtk_button_get_use_underline:
2061 * @button: a #GtkButton
2063 * Returns whether an embedded underline in the button label indicates a
2064 * mnemonic. See gtk_button_set_use_underline ().
2066 * Return value: %TRUE if an embedded underline in the button label
2067 * indicates the mnemonic accelerator keys.
2070 gtk_button_get_use_underline (GtkButton *button)
2072 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2074 return button->use_underline;
2078 * gtk_button_set_use_stock:
2079 * @button: a #GtkButton
2080 * @use_stock: %TRUE if the button should use a stock item
2082 * If %TRUE, the label set on the button is used as a
2083 * stock id to select the stock item for the button.
2086 gtk_button_set_use_stock (GtkButton *button,
2089 g_return_if_fail (GTK_IS_BUTTON (button));
2091 use_stock = use_stock != FALSE;
2093 if (use_stock != button->use_stock)
2095 button->use_stock = use_stock;
2097 gtk_button_construct_child (button);
2099 g_object_notify (G_OBJECT (button), "use-stock");
2104 * gtk_button_get_use_stock:
2105 * @button: a #GtkButton
2107 * Returns whether the button label is a stock item.
2109 * Return value: %TRUE if the button label is used to
2110 * select a stock item instead of being
2111 * used directly as the label text.
2114 gtk_button_get_use_stock (GtkButton *button)
2116 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2118 return button->use_stock;
2122 * gtk_button_set_focus_on_click:
2123 * @button: a #GtkButton
2124 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2126 * Sets whether the button will grab focus when it is clicked with the mouse.
2127 * Making mouse clicks not grab focus is useful in places like toolbars where
2128 * you don't want the keyboard focus removed from the main area of the
2134 gtk_button_set_focus_on_click (GtkButton *button,
2135 gboolean focus_on_click)
2137 g_return_if_fail (GTK_IS_BUTTON (button));
2139 focus_on_click = focus_on_click != FALSE;
2141 if (button->focus_on_click != focus_on_click)
2143 button->focus_on_click = focus_on_click;
2145 g_object_notify (G_OBJECT (button), "focus-on-click");
2150 * gtk_button_get_focus_on_click:
2151 * @button: a #GtkButton
2153 * Returns whether the button grabs focus when it is clicked with the mouse.
2154 * See gtk_button_set_focus_on_click().
2156 * Return value: %TRUE if the button grabs focus when it is clicked with
2162 gtk_button_get_focus_on_click (GtkButton *button)
2164 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2166 return button->focus_on_click;
2170 * gtk_button_set_alignment:
2171 * @button: a #GtkButton
2172 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2173 * 1.0 is right aligned
2174 * @yalign: the vertical position of the child, 0.0 is top aligned,
2175 * 1.0 is bottom aligned
2177 * Sets the alignment of the child. This property has no effect unless
2178 * the child is a #GtkMisc or a #GtkAligment.
2183 gtk_button_set_alignment (GtkButton *button,
2187 GtkButtonPrivate *priv;
2189 g_return_if_fail (GTK_IS_BUTTON (button));
2191 priv = GTK_BUTTON_GET_PRIVATE (button);
2193 priv->xalign = xalign;
2194 priv->yalign = yalign;
2195 priv->align_set = 1;
2197 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2199 g_object_freeze_notify (G_OBJECT (button));
2200 g_object_notify (G_OBJECT (button), "xalign");
2201 g_object_notify (G_OBJECT (button), "yalign");
2202 g_object_thaw_notify (G_OBJECT (button));
2206 * gtk_button_get_alignment:
2207 * @button: a #GtkButton
2208 * @xalign: return location for horizontal alignment
2209 * @yalign: return location for vertical alignment
2211 * Gets the alignment of the child in the button.
2216 gtk_button_get_alignment (GtkButton *button,
2220 GtkButtonPrivate *priv;
2222 g_return_if_fail (GTK_IS_BUTTON (button));
2224 priv = GTK_BUTTON_GET_PRIVATE (button);
2227 *xalign = priv->xalign;
2230 *yalign = priv->yalign;
2234 * _gtk_button_set_depressed:
2235 * @button: a #GtkButton
2236 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2238 * Sets whether the button is currently drawn as down or not. This is
2239 * purely a visual setting, and is meant only for use by derived widgets
2240 * such as #GtkToggleButton.
2243 _gtk_button_set_depressed (GtkButton *button,
2246 GtkWidget *widget = GTK_WIDGET (button);
2248 depressed = depressed != FALSE;
2250 if (depressed != button->depressed)
2252 button->depressed = depressed;
2253 gtk_widget_queue_resize (widget);
2258 gtk_button_update_state (GtkButton *button)
2261 GtkStateType new_state;
2263 if (button->activate_timeout)
2264 depressed = button->depress_on_activate;
2266 depressed = button->in_button && button->button_down;
2268 if (button->in_button && (!button->button_down || !depressed))
2269 new_state = GTK_STATE_PRELIGHT;
2271 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2273 _gtk_button_set_depressed (button, depressed);
2274 gtk_widget_set_state (GTK_WIDGET (button), new_state);
2278 show_image_change_notify (GtkButton *button)
2280 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2284 if (show_image (button))
2285 gtk_widget_show (priv->image);
2287 gtk_widget_hide (priv->image);
2292 traverse_container (GtkWidget *widget,
2295 if (GTK_IS_BUTTON (widget))
2296 show_image_change_notify (GTK_BUTTON (widget));
2297 else if (GTK_IS_CONTAINER (widget))
2298 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2302 gtk_button_setting_changed (GtkSettings *settings)
2306 list = gtk_window_list_toplevels ();
2308 for (l = list; l; l = l->next)
2309 gtk_container_forall (GTK_CONTAINER (l->data),
2310 traverse_container, NULL);
2317 gtk_button_screen_changed (GtkWidget *widget,
2318 GdkScreen *previous_screen)
2321 GtkSettings *settings;
2322 guint show_image_connection;
2324 if (!gtk_widget_has_screen (widget))
2327 button = GTK_BUTTON (widget);
2329 /* If the button is being pressed while the screen changes the
2330 release might never occur, so we reset the state. */
2331 if (button->button_down)
2333 button->button_down = FALSE;
2334 gtk_button_update_state (button);
2337 settings = gtk_widget_get_settings (widget);
2339 show_image_connection =
2340 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
2341 "gtk-button-connection"));
2343 if (show_image_connection)
2346 show_image_connection =
2347 g_signal_connect (settings, "notify::gtk-button-images",
2348 G_CALLBACK (gtk_button_setting_changed), NULL);
2349 g_object_set_data (G_OBJECT (settings),
2350 I_("gtk-button-connection"),
2351 GUINT_TO_POINTER (show_image_connection));
2353 show_image_change_notify (button);
2357 gtk_button_state_changed (GtkWidget *widget,
2358 GtkStateType previous_state)
2360 GtkButton *button = GTK_BUTTON (widget);
2362 if (!gtk_widget_is_sensitive (widget))
2364 button->in_button = FALSE;
2365 gtk_real_button_released (button);
2370 gtk_button_grab_notify (GtkWidget *widget,
2371 gboolean was_grabbed)
2373 GtkButton *button = GTK_BUTTON (widget);
2374 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2377 if (button->activate_timeout &&
2378 priv->grab_keyboard &&
2379 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2380 gtk_button_finish_activate (button, FALSE);
2384 save_in = button->in_button;
2385 button->in_button = FALSE;
2386 gtk_real_button_released (button);
2387 if (save_in != button->in_button)
2389 button->in_button = save_in;
2390 gtk_button_update_state (button);
2396 * gtk_button_set_image:
2397 * @button: a #GtkButton
2398 * @image: a widget to set as the image for the button
2400 * Set the image of @button to the given widget. Note that
2401 * it depends on the #GtkSettings:gtk-button-images setting whether the
2402 * image will be displayed or not, you don't have to call
2403 * gtk_widget_show() on @image yourself.
2408 gtk_button_set_image (GtkButton *button,
2411 GtkButtonPrivate *priv;
2414 g_return_if_fail (GTK_IS_BUTTON (button));
2415 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2417 priv = GTK_BUTTON_GET_PRIVATE (button);
2421 parent = gtk_widget_get_parent (priv->image);
2423 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2426 priv->image = image;
2427 priv->image_is_stock = (image == NULL);
2429 gtk_button_construct_child (button);
2431 g_object_notify (G_OBJECT (button), "image");
2435 * gtk_button_get_image:
2436 * @button: a #GtkButton
2438 * Gets the widget that is currenty set as the image of @button.
2439 * This may have been explicitly set by gtk_button_set_image()
2440 * or constructed by gtk_button_new_from_stock().
2442 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2447 gtk_button_get_image (GtkButton *button)
2449 GtkButtonPrivate *priv;
2451 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2453 priv = GTK_BUTTON_GET_PRIVATE (button);
2459 * gtk_button_set_image_position:
2460 * @button: a #GtkButton
2461 * @position: the position
2463 * Sets the position of the image relative to the text
2464 * inside the button.
2469 gtk_button_set_image_position (GtkButton *button,
2470 GtkPositionType position)
2473 GtkButtonPrivate *priv;
2475 g_return_if_fail (GTK_IS_BUTTON (button));
2476 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2478 priv = GTK_BUTTON_GET_PRIVATE (button);
2480 if (priv->image_position != position)
2482 priv->image_position = position;
2484 gtk_button_construct_child (button);
2486 g_object_notify (G_OBJECT (button), "image-position");
2491 * gtk_button_get_image_position:
2492 * @button: a #GtkButton
2494 * Gets the position of the image relative to the text
2495 * inside the button.
2497 * Return value: the position
2502 gtk_button_get_image_position (GtkButton *button)
2504 GtkButtonPrivate *priv;
2506 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2508 priv = GTK_BUTTON_GET_PRIVATE (button);
2510 return priv->image_position;
2515 * gtk_button_get_event_window:
2516 * @button: a #GtkButton
2518 * Returns the button's event window if it is realized, %NULL otherwise.
2519 * This function should be rarely needed.
2521 * Return value: (transfer none): @button's event window.
2526 gtk_button_get_event_window (GtkButton *button)
2528 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2530 return button->event_window;