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;
231 klass->pressed = gtk_real_button_pressed;
232 klass->released = gtk_real_button_released;
233 klass->clicked = NULL;
234 klass->enter = gtk_button_update_state;
235 klass->leave = gtk_button_update_state;
236 klass->activate = gtk_real_button_activate;
238 g_object_class_install_property (gobject_class,
240 g_param_spec_string ("label",
242 P_("Text of the label widget inside the button, if the button contains a label widget"),
244 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
246 g_object_class_install_property (gobject_class,
248 g_param_spec_boolean ("use-underline",
250 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
252 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
254 g_object_class_install_property (gobject_class,
256 g_param_spec_boolean ("use-stock",
258 P_("If set, the label is used to pick a stock item instead of being displayed"),
260 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
262 g_object_class_install_property (gobject_class,
264 g_param_spec_boolean ("focus-on-click",
265 P_("Focus on click"),
266 P_("Whether the button grabs focus when it is clicked with the mouse"),
268 GTK_PARAM_READWRITE));
270 g_object_class_install_property (gobject_class,
272 g_param_spec_enum ("relief",
274 P_("The border relief style"),
275 GTK_TYPE_RELIEF_STYLE,
277 GTK_PARAM_READWRITE));
282 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
283 * can be used to control it's horizontal alignment. 0.0 is left aligned,
284 * 1.0 is right aligned.
288 g_object_class_install_property (gobject_class,
290 g_param_spec_float("xalign",
291 P_("Horizontal alignment for child"),
292 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
296 GTK_PARAM_READWRITE));
301 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
302 * can be used to control it's vertical alignment. 0.0 is top aligned,
303 * 1.0 is bottom aligned.
307 g_object_class_install_property (gobject_class,
309 g_param_spec_float("yalign",
310 P_("Vertical alignment for child"),
311 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
315 GTK_PARAM_READWRITE));
320 * The child widget to appear next to the button text.
324 g_object_class_install_property (gobject_class,
326 g_param_spec_object ("image",
328 P_("Child widget to appear next to the button text"),
330 GTK_PARAM_READWRITE));
333 * GtkButton:image-position:
335 * The position of the image relative to the text inside the button.
339 g_object_class_install_property (gobject_class,
341 g_param_spec_enum ("image-position",
342 P_("Image position"),
343 P_("The position of the image relative to the text"),
344 GTK_TYPE_POSITION_TYPE,
346 GTK_PARAM_READWRITE));
348 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
349 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
352 * GtkButton::pressed:
353 * @button: the object that received the signal
355 * Emitted when the button is pressed.
357 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
359 button_signals[PRESSED] =
360 g_signal_new (I_("pressed"),
361 G_OBJECT_CLASS_TYPE (object_class),
363 G_STRUCT_OFFSET (GtkButtonClass, pressed),
365 _gtk_marshal_VOID__VOID,
369 * GtkButton::released:
370 * @button: the object that received the signal
372 * Emitted when the button is released.
374 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
376 button_signals[RELEASED] =
377 g_signal_new (I_("released"),
378 G_OBJECT_CLASS_TYPE (object_class),
380 G_STRUCT_OFFSET (GtkButtonClass, released),
382 _gtk_marshal_VOID__VOID,
386 * GtkButton::clicked:
387 * @button: the object that received the signal
389 * Emitted when the button has been activated (pressed and released).
391 button_signals[CLICKED] =
392 g_signal_new (I_("clicked"),
393 G_OBJECT_CLASS_TYPE (object_class),
394 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
395 G_STRUCT_OFFSET (GtkButtonClass, clicked),
397 _gtk_marshal_VOID__VOID,
402 * @button: the object that received the signal
404 * Emitted when the pointer enters the button.
406 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
408 button_signals[ENTER] =
409 g_signal_new (I_("enter"),
410 G_OBJECT_CLASS_TYPE (object_class),
412 G_STRUCT_OFFSET (GtkButtonClass, enter),
414 _gtk_marshal_VOID__VOID,
419 * @button: the object that received the signal
421 * Emitted when the pointer leaves the button.
423 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
425 button_signals[LEAVE] =
426 g_signal_new (I_("leave"),
427 G_OBJECT_CLASS_TYPE (object_class),
429 G_STRUCT_OFFSET (GtkButtonClass, leave),
431 _gtk_marshal_VOID__VOID,
435 * GtkButton::activate:
436 * @widget: the object which received the signal.
438 * The ::activate signal on GtkButton is an action signal and
439 * emitting it causes the button to animate press then release.
440 * Applications should never connect to this signal, but use the
441 * #GtkButton::clicked signal.
443 button_signals[ACTIVATE] =
444 g_signal_new (I_("activate"),
445 G_OBJECT_CLASS_TYPE (object_class),
446 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
447 G_STRUCT_OFFSET (GtkButtonClass, activate),
449 _gtk_marshal_VOID__VOID,
451 widget_class->activate_signal = button_signals[ACTIVATE];
454 * GtkButton:default-border:
456 * The "default-border" style property defines the extra space to add
457 * around a button that can become the default widget of its window.
458 * For more information about default widgets, see gtk_widget_grab_default().
461 gtk_widget_class_install_style_property (widget_class,
462 g_param_spec_boxed ("default-border",
463 P_("Default Spacing"),
464 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
466 GTK_PARAM_READABLE));
469 * GtkButton:default-outside-border:
471 * The "default-outside-border" style property defines the extra outside
472 * space to add around a button that can become the default widget of its
473 * window. Extra outside space is always drawn outside the button border.
474 * For more information about default widgets, see gtk_widget_grab_default().
476 gtk_widget_class_install_style_property (widget_class,
477 g_param_spec_boxed ("default-outside-border",
478 P_("Default Outside Spacing"),
479 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
481 GTK_PARAM_READABLE));
482 gtk_widget_class_install_style_property (widget_class,
483 g_param_spec_int ("child-displacement-x",
484 P_("Child X Displacement"),
485 P_("How far in the x direction to move the child when the button is depressed"),
489 GTK_PARAM_READABLE));
490 gtk_widget_class_install_style_property (widget_class,
491 g_param_spec_int ("child-displacement-y",
492 P_("Child Y Displacement"),
493 P_("How far in the y direction to move the child when the button is depressed"),
497 GTK_PARAM_READABLE));
500 * GtkButton:displace-focus:
502 * Whether the child_displacement_x/child_displacement_y properties
503 * should also affect the focus rectangle.
507 gtk_widget_class_install_style_property (widget_class,
508 g_param_spec_boolean ("displace-focus",
509 P_("Displace focus"),
510 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
512 GTK_PARAM_READABLE));
515 * GtkButton:inner-border:
517 * Sets the border between the button edges and child.
521 gtk_widget_class_install_style_property (widget_class,
522 g_param_spec_boxed ("inner-border",
524 P_("Border between button edges and child."),
526 GTK_PARAM_READABLE));
529 * GtkButton::image-spacing:
531 * Spacing in pixels between the image and label.
535 gtk_widget_class_install_style_property (widget_class,
536 g_param_spec_int ("image-spacing",
538 P_("Spacing in pixels between the image and label"),
542 GTK_PARAM_READABLE));
545 * GtkSettings::gtk-button-images:
547 * Whether images should be shown on buttons
551 gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
552 P_("Show button images"),
553 P_("Whether images should be shown on buttons"),
555 GTK_PARAM_READWRITE));
557 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
561 gtk_button_init (GtkButton *button)
563 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
565 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
566 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
567 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
569 button->label_text = NULL;
571 button->constructed = FALSE;
572 button->in_button = FALSE;
573 button->button_down = FALSE;
574 button->relief = GTK_RELIEF_NORMAL;
575 button->use_stock = FALSE;
576 button->use_underline = FALSE;
577 button->depressed = FALSE;
578 button->depress_on_activate = TRUE;
579 button->focus_on_click = TRUE;
584 priv->image_is_stock = TRUE;
585 priv->image_position = GTK_POS_LEFT;
586 priv->use_action_appearance = TRUE;
590 gtk_button_destroy (GtkObject *object)
592 GtkButton *button = GTK_BUTTON (object);
594 if (button->label_text)
596 g_free (button->label_text);
597 button->label_text = NULL;
600 GTK_OBJECT_CLASS (gtk_button_parent_class)->destroy (object);
604 gtk_button_constructor (GType type,
605 guint n_construct_properties,
606 GObjectConstructParam *construct_params)
611 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
612 n_construct_properties,
615 button = GTK_BUTTON (object);
616 button->constructed = TRUE;
618 if (button->label_text != NULL)
619 gtk_button_construct_child (button);
626 gtk_button_child_type (GtkContainer *container)
628 if (!gtk_bin_get_child (GTK_BIN (container)))
629 return GTK_TYPE_WIDGET;
635 maybe_set_alignment (GtkButton *button,
638 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
640 if (GTK_IS_MISC (widget))
642 GtkMisc *misc = GTK_MISC (widget);
645 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
647 else if (GTK_IS_ALIGNMENT (widget))
649 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
650 gfloat xscale, yscale;
652 g_object_get (alignment,
658 gtk_alignment_set (alignment,
659 priv->xalign, priv->yalign,
665 gtk_button_add (GtkContainer *container,
668 maybe_set_alignment (GTK_BUTTON (container), widget);
670 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
674 gtk_button_dispose (GObject *object)
676 GtkButton *button = GTK_BUTTON (object);
677 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
681 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
684 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
688 gtk_button_set_property (GObject *object,
693 GtkButton *button = GTK_BUTTON (object);
694 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
699 gtk_button_set_label (button, g_value_get_string (value));
702 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
705 gtk_button_set_relief (button, g_value_get_enum (value));
707 case PROP_USE_UNDERLINE:
708 gtk_button_set_use_underline (button, g_value_get_boolean (value));
711 gtk_button_set_use_stock (button, g_value_get_boolean (value));
713 case PROP_FOCUS_ON_CLICK:
714 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
717 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
720 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
722 case PROP_IMAGE_POSITION:
723 gtk_button_set_image_position (button, g_value_get_enum (value));
725 case PROP_ACTIVATABLE_RELATED_ACTION:
726 gtk_button_set_related_action (button, g_value_get_object (value));
728 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
729 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
732 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
738 gtk_button_get_property (GObject *object,
743 GtkButton *button = GTK_BUTTON (object);
744 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
749 g_value_set_string (value, button->label_text);
752 g_value_set_object (value, (GObject *)priv->image);
755 g_value_set_enum (value, gtk_button_get_relief (button));
757 case PROP_USE_UNDERLINE:
758 g_value_set_boolean (value, button->use_underline);
761 g_value_set_boolean (value, button->use_stock);
763 case PROP_FOCUS_ON_CLICK:
764 g_value_set_boolean (value, button->focus_on_click);
767 g_value_set_float (value, priv->xalign);
770 g_value_set_float (value, priv->yalign);
772 case PROP_IMAGE_POSITION:
773 g_value_set_enum (value, priv->image_position);
775 case PROP_ACTIVATABLE_RELATED_ACTION:
776 g_value_set_object (value, priv->action);
778 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
779 g_value_set_boolean (value, priv->use_action_appearance);
782 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
788 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
790 iface->update = gtk_button_update;
791 iface->sync_action_properties = gtk_button_sync_action_properties;
795 activatable_update_stock_id (GtkButton *button,
798 if (!gtk_button_get_use_stock (button))
801 gtk_button_set_label (button, gtk_action_get_stock_id (action));
805 activatable_update_short_label (GtkButton *button,
811 if (gtk_button_get_use_stock (button))
814 image = gtk_button_get_image (button);
816 /* Dont touch custom child... */
817 child = gtk_bin_get_child (GTK_BIN (button));
818 if (GTK_IS_IMAGE (image) ||
820 GTK_IS_LABEL (child))
822 gtk_button_set_label (button, gtk_action_get_short_label (action));
823 gtk_button_set_use_underline (button, TRUE);
828 activatable_update_icon_name (GtkButton *button,
833 if (gtk_button_get_use_stock (button))
836 image = gtk_button_get_image (button);
838 if (GTK_IS_IMAGE (image) &&
839 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
840 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
841 gtk_image_set_from_icon_name (GTK_IMAGE (image),
842 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
846 activatable_update_gicon (GtkButton *button,
849 GtkWidget *image = gtk_button_get_image (button);
850 GIcon *icon = gtk_action_get_gicon (action);
852 if (GTK_IS_IMAGE (image) &&
853 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
854 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
855 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
859 gtk_button_update (GtkActivatable *activatable,
861 const gchar *property_name)
863 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
865 if (strcmp (property_name, "visible") == 0)
867 if (gtk_action_is_visible (action))
868 gtk_widget_show (GTK_WIDGET (activatable));
870 gtk_widget_hide (GTK_WIDGET (activatable));
872 else if (strcmp (property_name, "sensitive") == 0)
873 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
875 if (!priv->use_action_appearance)
878 if (strcmp (property_name, "stock-id") == 0)
879 activatable_update_stock_id (GTK_BUTTON (activatable), action);
880 else if (strcmp (property_name, "gicon") == 0)
881 activatable_update_gicon (GTK_BUTTON (activatable), action);
882 else if (strcmp (property_name, "short-label") == 0)
883 activatable_update_short_label (GTK_BUTTON (activatable), action);
884 else if (strcmp (property_name, "icon-name") == 0)
885 activatable_update_icon_name (GTK_BUTTON (activatable), action);
889 gtk_button_sync_action_properties (GtkActivatable *activatable,
892 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
897 if (gtk_action_is_visible (action))
898 gtk_widget_show (GTK_WIDGET (activatable));
900 gtk_widget_hide (GTK_WIDGET (activatable));
902 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
904 if (priv->use_action_appearance)
906 activatable_update_stock_id (GTK_BUTTON (activatable), action);
907 activatable_update_short_label (GTK_BUTTON (activatable), action);
908 activatable_update_gicon (GTK_BUTTON (activatable), action);
909 activatable_update_icon_name (GTK_BUTTON (activatable), action);
914 gtk_button_set_related_action (GtkButton *button,
917 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
919 if (priv->action == action)
922 /* This should be a default handler, but for compatibility reasons
923 * we need to support derived classes that don't chain up their
926 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
928 g_signal_connect_after (button, "clicked",
929 G_CALLBACK (gtk_real_button_clicked), NULL);
931 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
933 priv->action = action;
937 gtk_button_set_use_action_appearance (GtkButton *button,
938 gboolean use_appearance)
940 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
942 if (priv->use_action_appearance != use_appearance)
944 priv->use_action_appearance = use_appearance;
946 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
953 * Creates a new #GtkButton widget. To add a child widget to the button,
954 * use gtk_container_add().
956 * Returns: The newly created #GtkButton widget.
959 gtk_button_new (void)
961 return g_object_new (GTK_TYPE_BUTTON, NULL);
965 show_image (GtkButton *button)
969 if (button->label_text)
971 GtkSettings *settings;
973 settings = gtk_widget_get_settings (GTK_WIDGET (button));
974 g_object_get (settings, "gtk-button-images", &show, NULL);
983 gtk_button_construct_child (GtkButton *button)
985 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
991 GtkWidget *image = NULL;
992 gchar *label_text = NULL;
995 if (!button->constructed)
998 if (!button->label_text && !priv->image)
1001 gtk_widget_style_get (GTK_WIDGET (button),
1002 "image-spacing", &image_spacing,
1005 if (priv->image && !priv->image_is_stock)
1009 image = g_object_ref (priv->image);
1011 parent = gtk_widget_get_parent (image);
1013 gtk_container_remove (GTK_CONTAINER (parent), image);
1018 child = gtk_bin_get_child (GTK_BIN (button));
1020 gtk_container_remove (GTK_CONTAINER (button), child);
1022 if (button->use_stock &&
1023 button->label_text &&
1024 gtk_stock_lookup (button->label_text, &item))
1027 image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
1029 label_text = item.label;
1032 label_text = button->label_text;
1036 priv->image = image;
1037 g_object_set (priv->image,
1038 "visible", show_image (button),
1039 "no-show-all", TRUE,
1042 if (priv->image_position == GTK_POS_LEFT ||
1043 priv->image_position == GTK_POS_RIGHT)
1044 box = gtk_hbox_new (FALSE, image_spacing);
1046 box = gtk_vbox_new (FALSE, image_spacing);
1048 if (priv->align_set)
1049 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1051 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1053 if (priv->image_position == GTK_POS_LEFT ||
1054 priv->image_position == GTK_POS_TOP)
1055 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1057 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1061 if (button->use_underline || button->use_stock)
1063 label = gtk_label_new_with_mnemonic (label_text);
1064 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1065 GTK_WIDGET (button));
1068 label = gtk_label_new (label_text);
1070 if (priv->image_position == GTK_POS_RIGHT ||
1071 priv->image_position == GTK_POS_BOTTOM)
1072 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1074 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1077 gtk_container_add (GTK_CONTAINER (button), align);
1078 gtk_container_add (GTK_CONTAINER (align), box);
1079 gtk_widget_show_all (align);
1081 g_object_unref (image);
1086 if (button->use_underline || button->use_stock)
1088 label = gtk_label_new_with_mnemonic (button->label_text);
1089 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1092 label = gtk_label_new (button->label_text);
1094 if (priv->align_set)
1095 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1097 gtk_widget_show (label);
1098 gtk_container_add (GTK_CONTAINER (button), label);
1103 * gtk_button_new_with_label:
1104 * @label: The text you want the #GtkLabel to hold.
1106 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1109 * Returns: The newly created #GtkButton widget.
1112 gtk_button_new_with_label (const gchar *label)
1114 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1118 * gtk_button_new_from_stock:
1119 * @stock_id: the name of the stock item
1121 * Creates a new #GtkButton containing the image and text from a stock item.
1122 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1125 * If @stock_id is unknown, then it will be treated as a mnemonic
1126 * label (as for gtk_button_new_with_mnemonic()).
1128 * Returns: a new #GtkButton
1131 gtk_button_new_from_stock (const gchar *stock_id)
1133 return g_object_new (GTK_TYPE_BUTTON,
1136 "use-underline", TRUE,
1141 * gtk_button_new_with_mnemonic:
1142 * @label: The text of the button, with an underscore in front of the
1143 * mnemonic character
1144 * @returns: a new #GtkButton
1146 * Creates a new #GtkButton containing a label.
1147 * If characters in @label are preceded by an underscore, they are underlined.
1148 * If you need a literal underscore character in a label, use '__' (two
1149 * underscores). The first underlined character represents a keyboard
1150 * accelerator called a mnemonic.
1151 * Pressing Alt and that key activates the button.
1154 gtk_button_new_with_mnemonic (const gchar *label)
1156 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1160 * gtk_button_pressed:
1161 * @button: The #GtkButton you want to send the signal to.
1163 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1165 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1168 gtk_button_pressed (GtkButton *button)
1170 g_return_if_fail (GTK_IS_BUTTON (button));
1173 g_signal_emit (button, button_signals[PRESSED], 0);
1177 * gtk_button_released:
1178 * @button: The #GtkButton you want to send the signal to.
1180 * Emits a #GtkButton::released signal to the given #GtkButton.
1182 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1185 gtk_button_released (GtkButton *button)
1187 g_return_if_fail (GTK_IS_BUTTON (button));
1189 g_signal_emit (button, button_signals[RELEASED], 0);
1193 * gtk_button_clicked:
1194 * @button: The #GtkButton you want to send the signal to.
1196 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1199 gtk_button_clicked (GtkButton *button)
1201 g_return_if_fail (GTK_IS_BUTTON (button));
1203 g_signal_emit (button, button_signals[CLICKED], 0);
1208 * @button: The #GtkButton you want to send the signal to.
1210 * Emits a #GtkButton::enter signal to the given #GtkButton.
1212 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1215 gtk_button_enter (GtkButton *button)
1217 g_return_if_fail (GTK_IS_BUTTON (button));
1219 g_signal_emit (button, button_signals[ENTER], 0);
1224 * @button: The #GtkButton you want to send the signal to.
1226 * Emits a #GtkButton::leave signal to the given #GtkButton.
1228 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1231 gtk_button_leave (GtkButton *button)
1233 g_return_if_fail (GTK_IS_BUTTON (button));
1235 g_signal_emit (button, button_signals[LEAVE], 0);
1239 * gtk_button_set_relief:
1240 * @button: The #GtkButton you want to set relief styles of.
1241 * @newstyle: The GtkReliefStyle as described above.
1243 * Sets the relief style of the edges of the given #GtkButton widget.
1244 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1245 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1247 * <!-- FIXME: put pictures of each style -->
1250 gtk_button_set_relief (GtkButton *button,
1251 GtkReliefStyle newrelief)
1253 g_return_if_fail (GTK_IS_BUTTON (button));
1255 if (newrelief != button->relief)
1257 button->relief = newrelief;
1258 g_object_notify (G_OBJECT (button), "relief");
1259 gtk_widget_queue_draw (GTK_WIDGET (button));
1264 * gtk_button_get_relief:
1265 * @button: The #GtkButton you want the #GtkReliefStyle from.
1267 * Returns the current relief style of the given #GtkButton.
1269 * Returns: The current #GtkReliefStyle
1272 gtk_button_get_relief (GtkButton *button)
1274 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1276 return button->relief;
1280 gtk_button_realize (GtkWidget *widget)
1282 GtkButton *button = GTK_BUTTON (widget);
1283 GtkAllocation allocation;
1285 GdkWindowAttr attributes;
1286 gint attributes_mask;
1289 gtk_widget_get_allocation (widget, &allocation);
1291 gtk_widget_set_realized (widget, TRUE);
1293 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1295 attributes.window_type = GDK_WINDOW_CHILD;
1296 attributes.x = allocation.x + border_width;
1297 attributes.y = allocation.y + border_width;
1298 attributes.width = allocation.width - border_width * 2;
1299 attributes.height = allocation.height - border_width * 2;
1300 attributes.wclass = GDK_INPUT_ONLY;
1301 attributes.event_mask = gtk_widget_get_events (widget);
1302 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1303 GDK_BUTTON_RELEASE_MASK |
1304 GDK_ENTER_NOTIFY_MASK |
1305 GDK_LEAVE_NOTIFY_MASK);
1307 attributes_mask = GDK_WA_X | GDK_WA_Y;
1309 window = gtk_widget_get_parent_window (widget);
1310 gtk_widget_set_window (widget, window);
1311 g_object_ref (window);
1313 button->event_window = gdk_window_new (window,
1314 &attributes, attributes_mask);
1315 gdk_window_set_user_data (button->event_window, button);
1317 gtk_widget_style_attach (widget);
1321 gtk_button_unrealize (GtkWidget *widget)
1323 GtkButton *button = GTK_BUTTON (widget);
1325 if (button->activate_timeout)
1326 gtk_button_finish_activate (button, FALSE);
1328 if (button->event_window)
1330 gdk_window_set_user_data (button->event_window, NULL);
1331 gdk_window_destroy (button->event_window);
1332 button->event_window = NULL;
1335 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1339 gtk_button_map (GtkWidget *widget)
1341 GtkButton *button = GTK_BUTTON (widget);
1343 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1345 if (button->event_window)
1346 gdk_window_show (button->event_window);
1350 gtk_button_unmap (GtkWidget *widget)
1352 GtkButton *button = GTK_BUTTON (widget);
1354 if (button->event_window)
1355 gdk_window_hide (button->event_window);
1357 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1361 gtk_button_update_image_spacing (GtkButton *button)
1363 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1367 /* Keep in sync with gtk_button_construct_child,
1368 * we only want to update the spacing if the box
1369 * was constructed there.
1371 if (!button->constructed || !priv->image)
1374 child = gtk_bin_get_child (GTK_BIN (button));
1375 if (GTK_IS_ALIGNMENT (child))
1377 child = gtk_bin_get_child (GTK_BIN (child));
1378 if (GTK_IS_BOX (child))
1380 gtk_widget_style_get (GTK_WIDGET (button),
1381 "image-spacing", &spacing,
1384 gtk_box_set_spacing (GTK_BOX (child), spacing);
1390 gtk_button_style_set (GtkWidget *widget,
1391 GtkStyle *prev_style)
1393 gtk_button_update_image_spacing (GTK_BUTTON (widget));
1397 gtk_button_get_props (GtkButton *button,
1398 GtkBorder *default_border,
1399 GtkBorder *default_outside_border,
1400 GtkBorder *inner_border,
1401 gboolean *interior_focus)
1403 GtkWidget *widget = GTK_WIDGET (button);
1404 GtkBorder *tmp_border;
1408 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1412 *default_border = *tmp_border;
1413 gtk_border_free (tmp_border);
1416 *default_border = default_default_border;
1419 if (default_outside_border)
1421 gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1425 *default_outside_border = *tmp_border;
1426 gtk_border_free (tmp_border);
1429 *default_outside_border = default_default_outside_border;
1434 gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1438 *inner_border = *tmp_border;
1439 gtk_border_free (tmp_border);
1442 *inner_border = default_inner_border;
1446 gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1450 gtk_button_size_allocate (GtkWidget *widget,
1451 GtkAllocation *allocation)
1453 GtkButton *button = GTK_BUTTON (widget);
1454 GtkAllocation child_allocation;
1458 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1459 gint xthickness, ythickness;
1460 GtkBorder default_border;
1461 GtkBorder inner_border;
1465 style = gtk_widget_get_style (widget);
1466 xthickness = style->xthickness;
1467 ythickness = style->ythickness;
1469 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1470 gtk_widget_style_get (GTK_WIDGET (widget),
1471 "focus-line-width", &focus_width,
1472 "focus-padding", &focus_pad,
1475 gtk_widget_set_allocation (widget, allocation);
1477 if (gtk_widget_get_realized (widget))
1478 gdk_window_move_resize (button->event_window,
1479 allocation->x + border_width,
1480 allocation->y + border_width,
1481 allocation->width - border_width * 2,
1482 allocation->height - border_width * 2);
1484 child = gtk_bin_get_child (GTK_BIN (button));
1485 if (child && gtk_widget_get_visible (child))
1487 child_allocation.x = allocation->x + border_width + inner_border.left + xthickness;
1488 child_allocation.y = allocation->y + border_width + inner_border.top + ythickness;
1490 child_allocation.width =
1494 inner_border.right -
1497 child_allocation.height =
1498 allocation->height -
1501 inner_border.bottom -
1504 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1506 child_allocation.x += default_border.left;
1507 child_allocation.y += default_border.top;
1508 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1509 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1512 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1514 child_allocation.x += focus_width + focus_pad;
1515 child_allocation.y += focus_width + focus_pad;
1516 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1517 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1520 if (button->depressed)
1522 gint child_displacement_x;
1523 gint child_displacement_y;
1525 gtk_widget_style_get (widget,
1526 "child-displacement-x", &child_displacement_x,
1527 "child-displacement-y", &child_displacement_y,
1529 child_allocation.x += child_displacement_x;
1530 child_allocation.y += child_displacement_y;
1533 child_allocation.width = MAX (1, child_allocation.width);
1534 child_allocation.height = MAX (1, child_allocation.height);
1536 gtk_widget_size_allocate (child, &child_allocation);
1541 _gtk_button_paint (GtkButton *button,
1542 const GdkRectangle *area,
1543 GtkStateType state_type,
1544 GtkShadowType shadow_type,
1545 const gchar *main_detail,
1546 const gchar *default_detail)
1552 GtkBorder default_border;
1553 GtkBorder default_outside_border;
1554 gboolean interior_focus;
1558 widget = GTK_WIDGET (button);
1560 if (gtk_widget_is_drawable (widget))
1562 GtkAllocation allocation;
1566 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1568 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1569 gtk_widget_style_get (widget,
1570 "focus-line-width", &focus_width,
1571 "focus-padding", &focus_pad,
1574 gtk_widget_get_allocation (widget, &allocation);
1575 style = gtk_widget_get_style (widget);
1576 window = gtk_widget_get_window (widget);
1578 x = allocation.x + border_width;
1579 y = allocation.y + border_width;
1580 width = allocation.width - border_width * 2;
1581 height = allocation.height - border_width * 2;
1583 if (gtk_widget_has_default (widget) &&
1584 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1586 gtk_paint_box (style, window,
1587 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1588 area, widget, "buttondefault",
1589 x, y, width, height);
1591 x += default_border.left;
1592 y += default_border.top;
1593 width -= default_border.left + default_border.right;
1594 height -= default_border.top + default_border.bottom;
1596 else if (gtk_widget_get_can_default (widget))
1598 x += default_outside_border.left;
1599 y += default_outside_border.top;
1600 width -= default_outside_border.left + default_outside_border.right;
1601 height -= default_outside_border.top + default_outside_border.bottom;
1604 if (!interior_focus && gtk_widget_has_focus (widget))
1606 x += focus_width + focus_pad;
1607 y += focus_width + focus_pad;
1608 width -= 2 * (focus_width + focus_pad);
1609 height -= 2 * (focus_width + focus_pad);
1612 if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1613 gtk_widget_get_state(widget) == GTK_STATE_PRELIGHT)
1614 gtk_paint_box (style, window,
1616 shadow_type, area, widget, "button",
1617 x, y, width, height);
1619 if (gtk_widget_has_focus (widget))
1621 gint child_displacement_x;
1622 gint child_displacement_y;
1623 gboolean displace_focus;
1625 gtk_widget_style_get (widget,
1626 "child-displacement-y", &child_displacement_y,
1627 "child-displacement-x", &child_displacement_x,
1628 "displace-focus", &displace_focus,
1633 x += style->xthickness + focus_pad;
1634 y += style->ythickness + focus_pad;
1635 width -= 2 * (style->xthickness + focus_pad);
1636 height -= 2 * (style->ythickness + focus_pad);
1640 x -= focus_width + focus_pad;
1641 y -= focus_width + focus_pad;
1642 width += 2 * (focus_width + focus_pad);
1643 height += 2 * (focus_width + focus_pad);
1646 if (button->depressed && displace_focus)
1648 x += child_displacement_x;
1649 y += child_displacement_y;
1652 gtk_paint_focus (style, window,
1653 gtk_widget_get_state (widget),
1654 area, widget, "button",
1655 x, y, width, height);
1661 gtk_button_expose (GtkWidget *widget,
1662 GdkEventExpose *event)
1664 if (gtk_widget_is_drawable (widget))
1666 GtkButton *button = GTK_BUTTON (widget);
1668 _gtk_button_paint (button, &event->area,
1669 gtk_widget_get_state (widget),
1670 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1671 "button", "buttondefault");
1673 GTK_WIDGET_CLASS (gtk_button_parent_class)->expose_event (widget, event);
1680 gtk_button_button_press (GtkWidget *widget,
1681 GdkEventButton *event)
1685 if (event->type == GDK_BUTTON_PRESS)
1687 button = GTK_BUTTON (widget);
1689 if (button->focus_on_click && !gtk_widget_has_focus (widget))
1690 gtk_widget_grab_focus (widget);
1692 if (event->button == 1)
1693 gtk_button_pressed (button);
1700 gtk_button_button_release (GtkWidget *widget,
1701 GdkEventButton *event)
1705 if (event->button == 1)
1707 button = GTK_BUTTON (widget);
1708 gtk_button_released (button);
1715 gtk_button_grab_broken (GtkWidget *widget,
1716 GdkEventGrabBroken *event)
1718 GtkButton *button = GTK_BUTTON (widget);
1721 /* Simulate a button release without the pointer in the button */
1722 if (button->button_down)
1724 save_in = button->in_button;
1725 button->in_button = FALSE;
1726 gtk_button_released (button);
1727 if (save_in != button->in_button)
1729 button->in_button = save_in;
1730 gtk_button_update_state (button);
1738 gtk_button_key_release (GtkWidget *widget,
1741 GtkButton *button = GTK_BUTTON (widget);
1743 if (button->activate_timeout)
1745 gtk_button_finish_activate (button, TRUE);
1748 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1749 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1755 gtk_button_enter_notify (GtkWidget *widget,
1756 GdkEventCrossing *event)
1759 GtkWidget *event_widget;
1761 button = GTK_BUTTON (widget);
1762 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1764 if ((event_widget == widget) &&
1765 (event->detail != GDK_NOTIFY_INFERIOR))
1767 button->in_button = TRUE;
1768 gtk_button_enter (button);
1775 gtk_button_leave_notify (GtkWidget *widget,
1776 GdkEventCrossing *event)
1779 GtkWidget *event_widget;
1781 button = GTK_BUTTON (widget);
1782 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1784 if ((event_widget == widget) &&
1785 (event->detail != GDK_NOTIFY_INFERIOR) &&
1786 (gtk_widget_get_sensitive (event_widget)))
1788 button->in_button = FALSE;
1789 gtk_button_leave (button);
1796 gtk_real_button_pressed (GtkButton *button)
1798 if (button->activate_timeout)
1801 button->button_down = TRUE;
1802 gtk_button_update_state (button);
1806 gtk_real_button_released (GtkButton *button)
1808 if (button->button_down)
1810 button->button_down = FALSE;
1812 if (button->activate_timeout)
1815 if (button->in_button)
1816 gtk_button_clicked (button);
1818 gtk_button_update_state (button);
1823 gtk_real_button_clicked (GtkButton *button)
1825 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1828 gtk_action_activate (priv->action);
1832 button_activate_timeout (gpointer data)
1834 gtk_button_finish_activate (data, TRUE);
1840 gtk_real_button_activate (GtkButton *button)
1842 GtkWidget *widget = GTK_WIDGET (button);
1843 GtkButtonPrivate *priv;
1847 priv = GTK_BUTTON_GET_PRIVATE (button);
1848 device = gtk_get_current_event_device ();
1850 if (device && device->source != GDK_SOURCE_KEYBOARD)
1851 device = gdk_device_get_associated_device (device);
1853 g_return_if_fail (device && device->source == GDK_SOURCE_KEYBOARD);
1855 if (gtk_widget_get_realized (widget) && !button->activate_timeout)
1857 time = gtk_get_current_event_time ();
1859 if (gdk_device_grab (device, button->event_window,
1860 GDK_OWNERSHIP_WINDOW, TRUE,
1861 GDK_KEY_PRESS | GDK_KEY_RELEASE,
1862 NULL, time) == GDK_GRAB_SUCCESS)
1864 gtk_device_grab_add (widget, device, TRUE);
1865 priv->grab_keyboard = device;
1866 priv->grab_time = time;
1869 button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1870 button_activate_timeout,
1872 button->button_down = TRUE;
1873 gtk_button_update_state (button);
1874 gtk_widget_queue_draw (GTK_WIDGET (button));
1879 gtk_button_finish_activate (GtkButton *button,
1882 GtkWidget *widget = GTK_WIDGET (button);
1883 GtkButtonPrivate *priv;
1885 priv = GTK_BUTTON_GET_PRIVATE (button);
1887 g_source_remove (button->activate_timeout);
1888 button->activate_timeout = 0;
1890 if (priv->grab_keyboard)
1892 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1893 gtk_device_grab_remove (widget, priv->grab_keyboard);
1894 priv->grab_keyboard = NULL;
1897 button->button_down = FALSE;
1899 gtk_button_update_state (button);
1900 gtk_widget_queue_draw (GTK_WIDGET (button));
1903 gtk_button_clicked (button);
1908 gtk_button_size_request_init (GtkSizeRequestIface *iface)
1910 iface->get_width = gtk_button_get_width;
1911 iface->get_height = gtk_button_get_height;
1915 gtk_button_get_size (GtkSizeRequest *widget,
1916 GtkOrientation orientation,
1920 GtkButton *button = GTK_BUTTON (widget);
1923 GtkBorder default_border;
1924 GtkBorder inner_border;
1927 gint minimum, natural;
1930 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1931 gtk_widget_style_get (GTK_WIDGET (widget),
1932 "focus-line-width", &focus_width,
1933 "focus-padding", &focus_pad,
1936 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1937 style = gtk_widget_get_style (GTK_WIDGET (widget));
1939 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1941 minimum = ((border_width + style->xthickness) * 2 +
1942 inner_border.left + inner_border.right);
1944 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1945 minimum += default_border.left + default_border.right;
1949 minimum = ((border_width + style->ythickness) * 2 +
1950 inner_border.top + inner_border.bottom);
1952 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1953 minimum += default_border.top + default_border.bottom;
1956 minimum += 2 * (focus_width + focus_pad);
1959 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1960 gtk_widget_get_visible (child))
1962 gint child_min, child_nat;
1964 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1965 gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
1966 &child_min, &child_nat);
1968 gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
1969 &child_min, &child_nat);
1971 minimum += child_min;
1972 natural += child_nat;
1976 *minimum_size = minimum;
1979 *natural_size = natural;
1983 gtk_button_get_width (GtkSizeRequest *widget,
1987 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1991 gtk_button_get_height (GtkSizeRequest *widget,
1995 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1999 * gtk_button_set_label:
2000 * @button: a #GtkButton
2003 * Sets the text of the label of the button to @str. This text is
2004 * also used to select the stock item if gtk_button_set_use_stock()
2007 * This will also clear any previously set labels.
2010 gtk_button_set_label (GtkButton *button,
2015 g_return_if_fail (GTK_IS_BUTTON (button));
2017 new_label = g_strdup (label);
2018 g_free (button->label_text);
2019 button->label_text = new_label;
2021 gtk_button_construct_child (button);
2023 g_object_notify (G_OBJECT (button), "label");
2027 * gtk_button_get_label:
2028 * @button: a #GtkButton
2030 * Fetches the text from the label of the button, as set by
2031 * gtk_button_set_label(). If the label text has not
2032 * been set the return value will be %NULL. This will be the
2033 * case if you create an empty button with gtk_button_new() to
2034 * use as a container.
2036 * Return value: The text of the label widget. This string is owned
2037 * by the widget and must not be modified or freed.
2039 G_CONST_RETURN gchar *
2040 gtk_button_get_label (GtkButton *button)
2042 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2044 return button->label_text;
2048 * gtk_button_set_use_underline:
2049 * @button: a #GtkButton
2050 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2052 * If true, an underline in the text of the button label indicates
2053 * the next character should be used for the mnemonic accelerator key.
2056 gtk_button_set_use_underline (GtkButton *button,
2057 gboolean use_underline)
2059 g_return_if_fail (GTK_IS_BUTTON (button));
2061 use_underline = use_underline != FALSE;
2063 if (use_underline != button->use_underline)
2065 button->use_underline = use_underline;
2067 gtk_button_construct_child (button);
2069 g_object_notify (G_OBJECT (button), "use-underline");
2074 * gtk_button_get_use_underline:
2075 * @button: a #GtkButton
2077 * Returns whether an embedded underline in the button label indicates a
2078 * mnemonic. See gtk_button_set_use_underline ().
2080 * Return value: %TRUE if an embedded underline in the button label
2081 * indicates the mnemonic accelerator keys.
2084 gtk_button_get_use_underline (GtkButton *button)
2086 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2088 return button->use_underline;
2092 * gtk_button_set_use_stock:
2093 * @button: a #GtkButton
2094 * @use_stock: %TRUE if the button should use a stock item
2096 * If %TRUE, the label set on the button is used as a
2097 * stock id to select the stock item for the button.
2100 gtk_button_set_use_stock (GtkButton *button,
2103 g_return_if_fail (GTK_IS_BUTTON (button));
2105 use_stock = use_stock != FALSE;
2107 if (use_stock != button->use_stock)
2109 button->use_stock = use_stock;
2111 gtk_button_construct_child (button);
2113 g_object_notify (G_OBJECT (button), "use-stock");
2118 * gtk_button_get_use_stock:
2119 * @button: a #GtkButton
2121 * Returns whether the button label is a stock item.
2123 * Return value: %TRUE if the button label is used to
2124 * select a stock item instead of being
2125 * used directly as the label text.
2128 gtk_button_get_use_stock (GtkButton *button)
2130 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2132 return button->use_stock;
2136 * gtk_button_set_focus_on_click:
2137 * @button: a #GtkButton
2138 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2140 * Sets whether the button will grab focus when it is clicked with the mouse.
2141 * Making mouse clicks not grab focus is useful in places like toolbars where
2142 * you don't want the keyboard focus removed from the main area of the
2148 gtk_button_set_focus_on_click (GtkButton *button,
2149 gboolean focus_on_click)
2151 g_return_if_fail (GTK_IS_BUTTON (button));
2153 focus_on_click = focus_on_click != FALSE;
2155 if (button->focus_on_click != focus_on_click)
2157 button->focus_on_click = focus_on_click;
2159 g_object_notify (G_OBJECT (button), "focus-on-click");
2164 * gtk_button_get_focus_on_click:
2165 * @button: a #GtkButton
2167 * Returns whether the button grabs focus when it is clicked with the mouse.
2168 * See gtk_button_set_focus_on_click().
2170 * Return value: %TRUE if the button grabs focus when it is clicked with
2176 gtk_button_get_focus_on_click (GtkButton *button)
2178 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2180 return button->focus_on_click;
2184 * gtk_button_set_alignment:
2185 * @button: a #GtkButton
2186 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2187 * 1.0 is right aligned
2188 * @yalign: the vertical position of the child, 0.0 is top aligned,
2189 * 1.0 is bottom aligned
2191 * Sets the alignment of the child. This property has no effect unless
2192 * the child is a #GtkMisc or a #GtkAligment.
2197 gtk_button_set_alignment (GtkButton *button,
2201 GtkButtonPrivate *priv;
2203 g_return_if_fail (GTK_IS_BUTTON (button));
2205 priv = GTK_BUTTON_GET_PRIVATE (button);
2207 priv->xalign = xalign;
2208 priv->yalign = yalign;
2209 priv->align_set = 1;
2211 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2213 g_object_freeze_notify (G_OBJECT (button));
2214 g_object_notify (G_OBJECT (button), "xalign");
2215 g_object_notify (G_OBJECT (button), "yalign");
2216 g_object_thaw_notify (G_OBJECT (button));
2220 * gtk_button_get_alignment:
2221 * @button: a #GtkButton
2222 * @xalign: return location for horizontal alignment
2223 * @yalign: return location for vertical alignment
2225 * Gets the alignment of the child in the button.
2230 gtk_button_get_alignment (GtkButton *button,
2234 GtkButtonPrivate *priv;
2236 g_return_if_fail (GTK_IS_BUTTON (button));
2238 priv = GTK_BUTTON_GET_PRIVATE (button);
2241 *xalign = priv->xalign;
2244 *yalign = priv->yalign;
2248 * _gtk_button_set_depressed:
2249 * @button: a #GtkButton
2250 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2252 * Sets whether the button is currently drawn as down or not. This is
2253 * purely a visual setting, and is meant only for use by derived widgets
2254 * such as #GtkToggleButton.
2257 _gtk_button_set_depressed (GtkButton *button,
2260 GtkWidget *widget = GTK_WIDGET (button);
2262 depressed = depressed != FALSE;
2264 if (depressed != button->depressed)
2266 button->depressed = depressed;
2267 gtk_widget_queue_resize (widget);
2272 gtk_button_update_state (GtkButton *button)
2275 GtkStateType new_state;
2277 if (button->activate_timeout)
2278 depressed = button->depress_on_activate;
2280 depressed = button->in_button && button->button_down;
2282 if (button->in_button && (!button->button_down || !depressed))
2283 new_state = GTK_STATE_PRELIGHT;
2285 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2287 _gtk_button_set_depressed (button, depressed);
2288 gtk_widget_set_state (GTK_WIDGET (button), new_state);
2292 show_image_change_notify (GtkButton *button)
2294 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2298 if (show_image (button))
2299 gtk_widget_show (priv->image);
2301 gtk_widget_hide (priv->image);
2306 traverse_container (GtkWidget *widget,
2309 if (GTK_IS_BUTTON (widget))
2310 show_image_change_notify (GTK_BUTTON (widget));
2311 else if (GTK_IS_CONTAINER (widget))
2312 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2316 gtk_button_setting_changed (GtkSettings *settings)
2320 list = gtk_window_list_toplevels ();
2322 for (l = list; l; l = l->next)
2323 gtk_container_forall (GTK_CONTAINER (l->data),
2324 traverse_container, NULL);
2331 gtk_button_screen_changed (GtkWidget *widget,
2332 GdkScreen *previous_screen)
2335 GtkSettings *settings;
2336 guint show_image_connection;
2338 if (!gtk_widget_has_screen (widget))
2341 button = GTK_BUTTON (widget);
2343 /* If the button is being pressed while the screen changes the
2344 release might never occur, so we reset the state. */
2345 if (button->button_down)
2347 button->button_down = FALSE;
2348 gtk_button_update_state (button);
2351 settings = gtk_widget_get_settings (widget);
2353 show_image_connection =
2354 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
2355 "gtk-button-connection"));
2357 if (show_image_connection)
2360 show_image_connection =
2361 g_signal_connect (settings, "notify::gtk-button-images",
2362 G_CALLBACK (gtk_button_setting_changed), NULL);
2363 g_object_set_data (G_OBJECT (settings),
2364 I_("gtk-button-connection"),
2365 GUINT_TO_POINTER (show_image_connection));
2367 show_image_change_notify (button);
2371 gtk_button_state_changed (GtkWidget *widget,
2372 GtkStateType previous_state)
2374 GtkButton *button = GTK_BUTTON (widget);
2376 if (!gtk_widget_is_sensitive (widget))
2378 button->in_button = FALSE;
2379 gtk_real_button_released (button);
2384 gtk_button_grab_notify (GtkWidget *widget,
2385 gboolean was_grabbed)
2387 GtkButton *button = GTK_BUTTON (widget);
2388 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2391 if (button->activate_timeout &&
2392 priv->grab_keyboard &&
2393 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2394 gtk_button_finish_activate (button, FALSE);
2398 save_in = button->in_button;
2399 button->in_button = FALSE;
2400 gtk_real_button_released (button);
2401 if (save_in != button->in_button)
2403 button->in_button = save_in;
2404 gtk_button_update_state (button);
2410 * gtk_button_set_image:
2411 * @button: a #GtkButton
2412 * @image: a widget to set as the image for the button
2414 * Set the image of @button to the given widget. Note that
2415 * it depends on the #GtkSettings:gtk-button-images setting whether the
2416 * image will be displayed or not, you don't have to call
2417 * gtk_widget_show() on @image yourself.
2422 gtk_button_set_image (GtkButton *button,
2425 GtkButtonPrivate *priv;
2428 g_return_if_fail (GTK_IS_BUTTON (button));
2429 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2431 priv = GTK_BUTTON_GET_PRIVATE (button);
2435 parent = gtk_widget_get_parent (priv->image);
2437 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2440 priv->image = image;
2441 priv->image_is_stock = (image == NULL);
2443 gtk_button_construct_child (button);
2445 g_object_notify (G_OBJECT (button), "image");
2449 * gtk_button_get_image:
2450 * @button: a #GtkButton
2452 * Gets the widget that is currenty set as the image of @button.
2453 * This may have been explicitly set by gtk_button_set_image()
2454 * or constructed by gtk_button_new_from_stock().
2456 * Return value: a #GtkWidget or %NULL in case there is no image
2461 gtk_button_get_image (GtkButton *button)
2463 GtkButtonPrivate *priv;
2465 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2467 priv = GTK_BUTTON_GET_PRIVATE (button);
2473 * gtk_button_set_image_position:
2474 * @button: a #GtkButton
2475 * @position: the position
2477 * Sets the position of the image relative to the text
2478 * inside the button.
2483 gtk_button_set_image_position (GtkButton *button,
2484 GtkPositionType position)
2487 GtkButtonPrivate *priv;
2489 g_return_if_fail (GTK_IS_BUTTON (button));
2490 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2492 priv = GTK_BUTTON_GET_PRIVATE (button);
2494 if (priv->image_position != position)
2496 priv->image_position = position;
2498 gtk_button_construct_child (button);
2500 g_object_notify (G_OBJECT (button), "image-position");
2505 * gtk_button_get_image_position:
2506 * @button: a #GtkButton
2508 * Gets the position of the image relative to the text
2509 * inside the button.
2511 * Return value: the position
2516 gtk_button_get_image_position (GtkButton *button)
2518 GtkButtonPrivate *priv;
2520 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2522 priv = GTK_BUTTON_GET_PRIVATE (button);
2524 return priv->image_position;
2529 * gtk_button_get_event_window:
2530 * @button: a #GtkButton
2532 * Returns the button's event window if it is realized, %NULL otherwise.
2533 * This function should be rarely needed.
2535 * Return value: (transfer none): @button's event window.
2540 gtk_button_get_event_window (GtkButton *button)
2542 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2544 return button->event_window;