1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2001. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkalignment.h"
30 #include "gtkbutton.h"
33 #include "gtkmarshalers.h"
38 #include "gtkiconfactory.h"
39 #include "gtkactivatable.h"
40 #include "gtkprivate.h"
44 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
45 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
46 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
48 /* Time out before giving up on getting a key release when animating
51 #define ACTIVATE_TIMEOUT 250
75 /* activatable properties */
76 PROP_ACTIVATABLE_RELATED_ACTION,
77 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
80 #define GTK_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_BUTTON, GtkButtonPrivate))
81 typedef struct _GtkButtonPrivate GtkButtonPrivate;
83 struct _GtkButtonPrivate
89 guint image_is_stock : 1;
91 guint use_action_appearance : 1;
93 GtkPositionType image_position;
97 static void gtk_button_destroy (GtkObject *object);
98 static void gtk_button_dispose (GObject *object);
99 static void gtk_button_set_property (GObject *object,
103 static void gtk_button_get_property (GObject *object,
107 static void gtk_button_screen_changed (GtkWidget *widget,
108 GdkScreen *previous_screen);
109 static void gtk_button_realize (GtkWidget * widget);
110 static void gtk_button_unrealize (GtkWidget * widget);
111 static void gtk_button_map (GtkWidget * widget);
112 static void gtk_button_unmap (GtkWidget * widget);
113 static void gtk_button_style_set (GtkWidget * widget, GtkStyle * prev_style);
114 static void gtk_button_size_request (GtkWidget * widget,
115 GtkRequisition * requisition);
116 static void gtk_button_size_allocate (GtkWidget * widget,
117 GtkAllocation * allocation);
118 static gint gtk_button_expose (GtkWidget * widget, GdkEventExpose * event);
119 static gint gtk_button_button_press (GtkWidget * widget,
120 GdkEventButton * event);
121 static gint gtk_button_button_release (GtkWidget * widget,
122 GdkEventButton * event);
123 static gint gtk_button_grab_broken (GtkWidget * widget,
124 GdkEventGrabBroken * event);
125 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
126 static gint gtk_button_enter_notify (GtkWidget * widget,
127 GdkEventCrossing * event);
128 static gint gtk_button_leave_notify (GtkWidget * widget,
129 GdkEventCrossing * event);
130 static void gtk_real_button_pressed (GtkButton * button);
131 static void gtk_real_button_released (GtkButton * button);
132 static void gtk_real_button_clicked (GtkButton * button);
133 static void gtk_real_button_activate (GtkButton *button);
134 static void gtk_button_update_state (GtkButton *button);
135 static void gtk_button_add (GtkContainer *container,
137 static GType gtk_button_child_type (GtkContainer *container);
138 static void gtk_button_finish_activate (GtkButton *button,
141 static GObject* gtk_button_constructor (GType type,
142 guint n_construct_properties,
143 GObjectConstructParam *construct_params);
144 static void gtk_button_construct_child (GtkButton *button);
145 static void gtk_button_state_changed (GtkWidget *widget,
146 GtkStateType previous_state);
147 static void gtk_button_grab_notify (GtkWidget *widget,
148 gboolean was_grabbed);
151 static void gtk_button_activatable_interface_init (GtkActivatableIface *iface);
152 static void gtk_button_update (GtkActivatable *activatable,
154 const gchar *property_name);
155 static void gtk_button_sync_action_properties (GtkActivatable *activatable,
157 static void gtk_button_set_related_action (GtkButton *button,
159 static void gtk_button_set_use_action_appearance (GtkButton *button,
160 gboolean use_appearance);
162 static guint button_signals[LAST_SIGNAL] = { 0 };
164 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
165 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
166 gtk_button_activatable_interface_init))
169 gtk_button_class_init (GtkButtonClass *klass)
171 GObjectClass *gobject_class;
172 GtkObjectClass *object_class;
173 GtkWidgetClass *widget_class;
174 GtkContainerClass *container_class;
176 gobject_class = G_OBJECT_CLASS (klass);
177 object_class = (GtkObjectClass*) klass;
178 widget_class = (GtkWidgetClass*) klass;
179 container_class = (GtkContainerClass*) klass;
181 gobject_class->constructor = gtk_button_constructor;
182 gobject_class->dispose = gtk_button_dispose;
183 gobject_class->set_property = gtk_button_set_property;
184 gobject_class->get_property = gtk_button_get_property;
186 object_class->destroy = gtk_button_destroy;
188 widget_class->screen_changed = gtk_button_screen_changed;
189 widget_class->realize = gtk_button_realize;
190 widget_class->unrealize = gtk_button_unrealize;
191 widget_class->map = gtk_button_map;
192 widget_class->unmap = gtk_button_unmap;
193 widget_class->style_set = gtk_button_style_set;
194 widget_class->size_request = gtk_button_size_request;
195 widget_class->size_allocate = gtk_button_size_allocate;
196 widget_class->expose_event = gtk_button_expose;
197 widget_class->button_press_event = gtk_button_button_press;
198 widget_class->button_release_event = gtk_button_button_release;
199 widget_class->grab_broken_event = gtk_button_grab_broken;
200 widget_class->key_release_event = gtk_button_key_release;
201 widget_class->enter_notify_event = gtk_button_enter_notify;
202 widget_class->leave_notify_event = gtk_button_leave_notify;
203 widget_class->state_changed = gtk_button_state_changed;
204 widget_class->grab_notify = gtk_button_grab_notify;
206 container_class->child_type = gtk_button_child_type;
207 container_class->add = gtk_button_add;
209 klass->pressed = gtk_real_button_pressed;
210 klass->released = gtk_real_button_released;
211 klass->clicked = NULL;
212 klass->enter = gtk_button_update_state;
213 klass->leave = gtk_button_update_state;
214 klass->activate = gtk_real_button_activate;
216 g_object_class_install_property (gobject_class,
218 g_param_spec_string ("label",
220 P_("Text of the label widget inside the button, if the button contains a label widget"),
222 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
224 g_object_class_install_property (gobject_class,
226 g_param_spec_boolean ("use-underline",
228 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
230 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
232 g_object_class_install_property (gobject_class,
234 g_param_spec_boolean ("use-stock",
236 P_("If set, the label is used to pick a stock item instead of being displayed"),
238 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
240 g_object_class_install_property (gobject_class,
242 g_param_spec_boolean ("focus-on-click",
243 P_("Focus on click"),
244 P_("Whether the button grabs focus when it is clicked with the mouse"),
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_enum ("relief",
252 P_("The border relief style"),
253 GTK_TYPE_RELIEF_STYLE,
255 GTK_PARAM_READWRITE));
260 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
261 * can be used to control it's horizontal alignment. 0.0 is left aligned,
262 * 1.0 is right aligned.
266 g_object_class_install_property (gobject_class,
268 g_param_spec_float("xalign",
269 P_("Horizontal alignment for child"),
270 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
274 GTK_PARAM_READWRITE));
279 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
280 * can be used to control it's vertical alignment. 0.0 is top aligned,
281 * 1.0 is bottom aligned.
285 g_object_class_install_property (gobject_class,
287 g_param_spec_float("yalign",
288 P_("Vertical alignment for child"),
289 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
293 GTK_PARAM_READWRITE));
298 * The child widget to appear next to the button text.
302 g_object_class_install_property (gobject_class,
304 g_param_spec_object ("image",
306 P_("Child widget to appear next to the button text"),
308 GTK_PARAM_READWRITE));
311 * GtkButton:image-position:
313 * The position of the image relative to the text inside the button.
317 g_object_class_install_property (gobject_class,
319 g_param_spec_enum ("image-position",
320 P_("Image position"),
321 P_("The position of the image relative to the text"),
322 GTK_TYPE_POSITION_TYPE,
324 GTK_PARAM_READWRITE));
326 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
327 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
330 * GtkButton::pressed:
331 * @button: the object that received the signal
333 * Emitted when the button is pressed.
335 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
337 button_signals[PRESSED] =
338 g_signal_new (I_("pressed"),
339 G_OBJECT_CLASS_TYPE (object_class),
341 G_STRUCT_OFFSET (GtkButtonClass, pressed),
343 _gtk_marshal_VOID__VOID,
347 * GtkButton::released:
348 * @button: the object that received the signal
350 * Emitted when the button is released.
352 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
354 button_signals[RELEASED] =
355 g_signal_new (I_("released"),
356 G_OBJECT_CLASS_TYPE (object_class),
358 G_STRUCT_OFFSET (GtkButtonClass, released),
360 _gtk_marshal_VOID__VOID,
364 * GtkButton::clicked:
365 * @button: the object that received the signal
367 * Emitted when the button has been activated (pressed and released).
369 button_signals[CLICKED] =
370 g_signal_new (I_("clicked"),
371 G_OBJECT_CLASS_TYPE (object_class),
372 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
373 G_STRUCT_OFFSET (GtkButtonClass, clicked),
375 _gtk_marshal_VOID__VOID,
380 * @button: the object that received the signal
382 * Emitted when the pointer enters the button.
384 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
386 button_signals[ENTER] =
387 g_signal_new (I_("enter"),
388 G_OBJECT_CLASS_TYPE (object_class),
390 G_STRUCT_OFFSET (GtkButtonClass, enter),
392 _gtk_marshal_VOID__VOID,
397 * @button: the object that received the signal
399 * Emitted when the pointer leaves the button.
401 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
403 button_signals[LEAVE] =
404 g_signal_new (I_("leave"),
405 G_OBJECT_CLASS_TYPE (object_class),
407 G_STRUCT_OFFSET (GtkButtonClass, leave),
409 _gtk_marshal_VOID__VOID,
413 * GtkButton::activate:
414 * @widget: the object which received the signal.
416 * The ::activate signal on GtkButton is an action signal and
417 * emitting it causes the button to animate press then release.
418 * Applications should never connect to this signal, but use the
419 * #GtkButton::clicked signal.
421 button_signals[ACTIVATE] =
422 g_signal_new (I_("activate"),
423 G_OBJECT_CLASS_TYPE (object_class),
424 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
425 G_STRUCT_OFFSET (GtkButtonClass, activate),
427 _gtk_marshal_VOID__VOID,
429 widget_class->activate_signal = button_signals[ACTIVATE];
432 * GtkButton:default-border:
434 * The "default-border" style property defines the extra space to add
435 * around a button that can become the default widget of its window.
436 * For more information about default widgets, see gtk_widget_grab_default().
439 gtk_widget_class_install_style_property (widget_class,
440 g_param_spec_boxed ("default-border",
441 P_("Default Spacing"),
442 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
444 GTK_PARAM_READABLE));
447 * GtkButton:default-outside-border:
449 * The "default-outside-border" style property defines the extra outside
450 * space to add around a button that can become the default widget of its
451 * window. Extra outside space is always drawn outside the button border.
452 * For more information about default widgets, see gtk_widget_grab_default().
454 gtk_widget_class_install_style_property (widget_class,
455 g_param_spec_boxed ("default-outside-border",
456 P_("Default Outside Spacing"),
457 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
459 GTK_PARAM_READABLE));
460 gtk_widget_class_install_style_property (widget_class,
461 g_param_spec_int ("child-displacement-x",
462 P_("Child X Displacement"),
463 P_("How far in the x direction to move the child when the button is depressed"),
467 GTK_PARAM_READABLE));
468 gtk_widget_class_install_style_property (widget_class,
469 g_param_spec_int ("child-displacement-y",
470 P_("Child Y Displacement"),
471 P_("How far in the y direction to move the child when the button is depressed"),
475 GTK_PARAM_READABLE));
478 * GtkButton:displace-focus:
480 * Whether the child_displacement_x/child_displacement_y properties
481 * should also affect the focus rectangle.
485 gtk_widget_class_install_style_property (widget_class,
486 g_param_spec_boolean ("displace-focus",
487 P_("Displace focus"),
488 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
490 GTK_PARAM_READABLE));
493 * GtkButton:inner-border:
495 * Sets the border between the button edges and child.
499 gtk_widget_class_install_style_property (widget_class,
500 g_param_spec_boxed ("inner-border",
502 P_("Border between button edges and child."),
504 GTK_PARAM_READABLE));
507 * GtkButton::image-spacing:
509 * Spacing in pixels between the image and label.
513 gtk_widget_class_install_style_property (widget_class,
514 g_param_spec_int ("image-spacing",
516 P_("Spacing in pixels between the image and label"),
520 GTK_PARAM_READABLE));
523 * GtkSettings::gtk-button-images:
525 * Whether images should be shown on buttons
529 gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
530 P_("Show button images"),
531 P_("Whether images should be shown on buttons"),
533 GTK_PARAM_READWRITE));
535 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
539 gtk_button_init (GtkButton *button)
541 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
543 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
544 GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);
546 button->label_text = NULL;
548 button->constructed = FALSE;
549 button->in_button = FALSE;
550 button->button_down = FALSE;
551 button->relief = GTK_RELIEF_NORMAL;
552 button->use_stock = FALSE;
553 button->use_underline = FALSE;
554 button->depressed = FALSE;
555 button->depress_on_activate = TRUE;
556 button->focus_on_click = TRUE;
561 priv->image_is_stock = TRUE;
562 priv->image_position = GTK_POS_LEFT;
563 priv->use_action_appearance = TRUE;
567 gtk_button_destroy (GtkObject *object)
569 GtkButton *button = GTK_BUTTON (object);
571 if (button->label_text)
573 g_free (button->label_text);
574 button->label_text = NULL;
577 GTK_OBJECT_CLASS (gtk_button_parent_class)->destroy (object);
581 gtk_button_constructor (GType type,
582 guint n_construct_properties,
583 GObjectConstructParam *construct_params)
588 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
589 n_construct_properties,
592 button = GTK_BUTTON (object);
593 button->constructed = TRUE;
595 if (button->label_text != NULL)
596 gtk_button_construct_child (button);
603 gtk_button_child_type (GtkContainer *container)
605 if (!GTK_BIN (container)->child)
606 return GTK_TYPE_WIDGET;
612 maybe_set_alignment (GtkButton *button,
615 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
617 if (GTK_IS_MISC (widget))
619 GtkMisc *misc = GTK_MISC (widget);
622 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
624 else if (GTK_IS_ALIGNMENT (widget))
626 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
629 gtk_alignment_set (alignment, priv->xalign, priv->yalign,
630 alignment->xscale, alignment->yscale);
635 gtk_button_add (GtkContainer *container,
638 maybe_set_alignment (GTK_BUTTON (container), widget);
640 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
644 gtk_button_dispose (GObject *object)
646 GtkButton *button = GTK_BUTTON (object);
647 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
651 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
654 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
658 gtk_button_set_property (GObject *object,
663 GtkButton *button = GTK_BUTTON (object);
664 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
669 gtk_button_set_label (button, g_value_get_string (value));
672 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
675 gtk_button_set_relief (button, g_value_get_enum (value));
677 case PROP_USE_UNDERLINE:
678 gtk_button_set_use_underline (button, g_value_get_boolean (value));
681 gtk_button_set_use_stock (button, g_value_get_boolean (value));
683 case PROP_FOCUS_ON_CLICK:
684 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
687 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
690 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
692 case PROP_IMAGE_POSITION:
693 gtk_button_set_image_position (button, g_value_get_enum (value));
695 case PROP_ACTIVATABLE_RELATED_ACTION:
696 gtk_button_set_related_action (button, g_value_get_object (value));
698 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
699 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
702 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
708 gtk_button_get_property (GObject *object,
713 GtkButton *button = GTK_BUTTON (object);
714 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
719 g_value_set_string (value, button->label_text);
722 g_value_set_object (value, (GObject *)priv->image);
725 g_value_set_enum (value, gtk_button_get_relief (button));
727 case PROP_USE_UNDERLINE:
728 g_value_set_boolean (value, button->use_underline);
731 g_value_set_boolean (value, button->use_stock);
733 case PROP_FOCUS_ON_CLICK:
734 g_value_set_boolean (value, button->focus_on_click);
737 g_value_set_float (value, priv->xalign);
740 g_value_set_float (value, priv->yalign);
742 case PROP_IMAGE_POSITION:
743 g_value_set_enum (value, priv->image_position);
745 case PROP_ACTIVATABLE_RELATED_ACTION:
746 g_value_set_object (value, priv->action);
748 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
749 g_value_set_boolean (value, priv->use_action_appearance);
752 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
758 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
760 iface->update = gtk_button_update;
761 iface->sync_action_properties = gtk_button_sync_action_properties;
765 activatable_update_stock_id (GtkButton *button,
768 if (!gtk_button_get_use_stock (button))
771 gtk_button_set_label (button, gtk_action_get_stock_id (action));
775 activatable_update_short_label (GtkButton *button,
780 if (gtk_button_get_use_stock (button))
783 image = gtk_button_get_image (button);
785 /* Dont touch custom child... */
786 if (GTK_IS_IMAGE (image) ||
787 GTK_BIN (button)->child == NULL ||
788 GTK_IS_LABEL (GTK_BIN (button)->child))
790 gtk_button_set_label (button, gtk_action_get_short_label (action));
791 gtk_button_set_use_underline (button, TRUE);
796 activatable_update_icon_name (GtkButton *button,
801 if (gtk_button_get_use_stock (button))
804 image = gtk_button_get_image (button);
806 if (GTK_IS_IMAGE (image) &&
807 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
808 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
809 gtk_image_set_from_icon_name (GTK_IMAGE (image),
810 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
814 activatable_update_gicon (GtkButton *button,
817 GtkWidget *image = gtk_button_get_image (button);
818 GIcon *icon = gtk_action_get_gicon (action);
820 if (GTK_IS_IMAGE (image) &&
821 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
822 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
823 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
827 gtk_button_update (GtkActivatable *activatable,
829 const gchar *property_name)
831 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
833 if (strcmp (property_name, "visible") == 0)
835 if (gtk_action_is_visible (action))
836 gtk_widget_show (GTK_WIDGET (activatable));
838 gtk_widget_hide (GTK_WIDGET (activatable));
840 else if (strcmp (property_name, "sensitive") == 0)
841 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
843 if (!priv->use_action_appearance)
846 if (strcmp (property_name, "stock-id") == 0)
847 activatable_update_stock_id (GTK_BUTTON (activatable), action);
848 else if (strcmp (property_name, "gicon") == 0)
849 activatable_update_gicon (GTK_BUTTON (activatable), action);
850 else if (strcmp (property_name, "short-label") == 0)
851 activatable_update_short_label (GTK_BUTTON (activatable), action);
852 else if (strcmp (property_name, "icon-name") == 0)
853 activatable_update_icon_name (GTK_BUTTON (activatable), action);
857 gtk_button_sync_action_properties (GtkActivatable *activatable,
860 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
865 if (gtk_action_is_visible (action))
866 gtk_widget_show (GTK_WIDGET (activatable));
868 gtk_widget_hide (GTK_WIDGET (activatable));
870 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
872 if (priv->use_action_appearance)
874 activatable_update_stock_id (GTK_BUTTON (activatable), action);
875 activatable_update_short_label (GTK_BUTTON (activatable), action);
876 activatable_update_gicon (GTK_BUTTON (activatable), action);
877 activatable_update_icon_name (GTK_BUTTON (activatable), action);
882 gtk_button_set_related_action (GtkButton *button,
885 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
887 if (priv->action == action)
890 /* This should be a default handler, but for compatibility reasons
891 * we need to support derived classes that don't chain up their
894 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
896 g_signal_connect_after (button, "clicked",
897 G_CALLBACK (gtk_real_button_clicked), NULL);
899 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
901 priv->action = action;
905 gtk_button_set_use_action_appearance (GtkButton *button,
906 gboolean use_appearance)
908 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
910 if (priv->use_action_appearance != use_appearance)
912 priv->use_action_appearance = use_appearance;
914 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
919 gtk_button_new (void)
921 return g_object_new (GTK_TYPE_BUTTON, NULL);
925 show_image (GtkButton *button)
929 if (button->label_text)
931 GtkSettings *settings;
933 settings = gtk_widget_get_settings (GTK_WIDGET (button));
934 g_object_get (settings, "gtk-button-images", &show, NULL);
943 gtk_button_construct_child (GtkButton *button)
945 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
950 GtkWidget *image = NULL;
951 gchar *label_text = NULL;
954 if (!button->constructed)
957 if (!button->label_text && !priv->image)
960 gtk_widget_style_get (GTK_WIDGET (button),
961 "image-spacing", &image_spacing,
964 if (priv->image && !priv->image_is_stock)
966 image = g_object_ref (priv->image);
968 gtk_container_remove (GTK_CONTAINER (image->parent), image);
973 if (GTK_BIN (button)->child)
974 gtk_container_remove (GTK_CONTAINER (button),
975 GTK_BIN (button)->child);
977 if (button->use_stock &&
978 button->label_text &&
979 gtk_stock_lookup (button->label_text, &item))
982 image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
984 label_text = item.label;
987 label_text = button->label_text;
992 g_object_set (priv->image,
993 "visible", show_image (button),
997 if (priv->image_position == GTK_POS_LEFT ||
998 priv->image_position == GTK_POS_RIGHT)
999 box = gtk_hbox_new (FALSE, image_spacing);
1001 box = gtk_vbox_new (FALSE, image_spacing);
1003 if (priv->align_set)
1004 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1006 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1008 if (priv->image_position == GTK_POS_LEFT ||
1009 priv->image_position == GTK_POS_TOP)
1010 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1012 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1016 if (button->use_underline || button->use_stock)
1018 label = gtk_label_new_with_mnemonic (label_text);
1019 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1020 GTK_WIDGET (button));
1023 label = gtk_label_new (label_text);
1025 if (priv->image_position == GTK_POS_RIGHT ||
1026 priv->image_position == GTK_POS_BOTTOM)
1027 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1029 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1032 gtk_container_add (GTK_CONTAINER (button), align);
1033 gtk_container_add (GTK_CONTAINER (align), box);
1034 gtk_widget_show_all (align);
1036 g_object_unref (image);
1041 if (button->use_underline || button->use_stock)
1043 label = gtk_label_new_with_mnemonic (button->label_text);
1044 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1047 label = gtk_label_new (button->label_text);
1049 if (priv->align_set)
1050 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1052 gtk_widget_show (label);
1053 gtk_container_add (GTK_CONTAINER (button), label);
1058 gtk_button_new_with_label (const gchar *label)
1060 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1064 * gtk_button_new_from_stock:
1065 * @stock_id: the name of the stock item
1067 * Creates a new #GtkButton containing the image and text from a stock item.
1068 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1071 * If @stock_id is unknown, then it will be treated as a mnemonic
1072 * label (as for gtk_button_new_with_mnemonic()).
1074 * Returns: a new #GtkButton
1077 gtk_button_new_from_stock (const gchar *stock_id)
1079 return g_object_new (GTK_TYPE_BUTTON,
1082 "use-underline", TRUE,
1087 * gtk_button_new_with_mnemonic:
1088 * @label: The text of the button, with an underscore in front of the
1089 * mnemonic character
1090 * @returns: a new #GtkButton
1092 * Creates a new #GtkButton containing a label.
1093 * If characters in @label are preceded by an underscore, they are underlined.
1094 * If you need a literal underscore character in a label, use '__' (two
1095 * underscores). The first underlined character represents a keyboard
1096 * accelerator called a mnemonic.
1097 * Pressing Alt and that key activates the button.
1100 gtk_button_new_with_mnemonic (const gchar *label)
1102 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1106 gtk_button_pressed (GtkButton *button)
1108 g_return_if_fail (GTK_IS_BUTTON (button));
1111 g_signal_emit (button, button_signals[PRESSED], 0);
1115 gtk_button_released (GtkButton *button)
1117 g_return_if_fail (GTK_IS_BUTTON (button));
1119 g_signal_emit (button, button_signals[RELEASED], 0);
1123 gtk_button_clicked (GtkButton *button)
1125 g_return_if_fail (GTK_IS_BUTTON (button));
1127 g_signal_emit (button, button_signals[CLICKED], 0);
1131 gtk_button_enter (GtkButton *button)
1133 g_return_if_fail (GTK_IS_BUTTON (button));
1135 g_signal_emit (button, button_signals[ENTER], 0);
1139 gtk_button_leave (GtkButton *button)
1141 g_return_if_fail (GTK_IS_BUTTON (button));
1143 g_signal_emit (button, button_signals[LEAVE], 0);
1147 gtk_button_set_relief (GtkButton *button,
1148 GtkReliefStyle newrelief)
1150 g_return_if_fail (GTK_IS_BUTTON (button));
1152 if (newrelief != button->relief)
1154 button->relief = newrelief;
1155 g_object_notify (G_OBJECT (button), "relief");
1156 gtk_widget_queue_draw (GTK_WIDGET (button));
1161 gtk_button_get_relief (GtkButton *button)
1163 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1165 return button->relief;
1169 gtk_button_realize (GtkWidget *widget)
1172 GdkWindowAttr attributes;
1173 gint attributes_mask;
1176 button = GTK_BUTTON (widget);
1177 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1179 border_width = GTK_CONTAINER (widget)->border_width;
1181 attributes.window_type = GDK_WINDOW_CHILD;
1182 attributes.x = widget->allocation.x + border_width;
1183 attributes.y = widget->allocation.y + border_width;
1184 attributes.width = widget->allocation.width - border_width * 2;
1185 attributes.height = widget->allocation.height - border_width * 2;
1186 attributes.wclass = GDK_INPUT_ONLY;
1187 attributes.event_mask = gtk_widget_get_events (widget);
1188 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1189 GDK_BUTTON_RELEASE_MASK |
1190 GDK_ENTER_NOTIFY_MASK |
1191 GDK_LEAVE_NOTIFY_MASK);
1193 attributes_mask = GDK_WA_X | GDK_WA_Y;
1195 widget->window = gtk_widget_get_parent_window (widget);
1196 g_object_ref (widget->window);
1198 button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1199 &attributes, attributes_mask);
1200 gdk_window_set_user_data (button->event_window, button);
1202 widget->style = gtk_style_attach (widget->style, widget->window);
1206 gtk_button_unrealize (GtkWidget *widget)
1208 GtkButton *button = GTK_BUTTON (widget);
1210 if (button->activate_timeout)
1211 gtk_button_finish_activate (button, FALSE);
1213 if (button->event_window)
1215 gdk_window_set_user_data (button->event_window, NULL);
1216 gdk_window_destroy (button->event_window);
1217 button->event_window = NULL;
1220 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1224 gtk_button_map (GtkWidget *widget)
1226 GtkButton *button = GTK_BUTTON (widget);
1228 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1230 if (button->event_window)
1231 gdk_window_show (button->event_window);
1235 gtk_button_unmap (GtkWidget *widget)
1237 GtkButton *button = GTK_BUTTON (widget);
1239 if (button->event_window)
1240 gdk_window_hide (button->event_window);
1242 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1246 gtk_button_update_image_spacing (GtkButton *button)
1248 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1252 /* Keep in sync with gtk_button_construct_child,
1253 * we only want to update the spacing if the box
1254 * was constructed there.
1256 if (!button->constructed || !priv->image)
1259 child = GTK_BIN (button)->child;
1260 if (GTK_IS_ALIGNMENT (child))
1262 child = GTK_BIN (child)->child;
1263 if (GTK_IS_BOX (child))
1265 gtk_widget_style_get (GTK_WIDGET (button),
1266 "image-spacing", &spacing,
1269 gtk_box_set_spacing (GTK_BOX (child), spacing);
1275 gtk_button_style_set (GtkWidget *widget,
1276 GtkStyle *prev_style)
1278 gtk_button_update_image_spacing (GTK_BUTTON (widget));
1282 gtk_button_get_props (GtkButton *button,
1283 GtkBorder *default_border,
1284 GtkBorder *default_outside_border,
1285 GtkBorder *inner_border,
1286 gboolean *interior_focus)
1288 GtkWidget *widget = GTK_WIDGET (button);
1289 GtkBorder *tmp_border;
1293 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1297 *default_border = *tmp_border;
1298 gtk_border_free (tmp_border);
1301 *default_border = default_default_border;
1304 if (default_outside_border)
1306 gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1310 *default_outside_border = *tmp_border;
1311 gtk_border_free (tmp_border);
1314 *default_outside_border = default_default_outside_border;
1319 gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1323 *inner_border = *tmp_border;
1324 gtk_border_free (tmp_border);
1327 *inner_border = default_inner_border;
1331 gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1335 gtk_button_size_request (GtkWidget *widget,
1336 GtkRequisition *requisition)
1338 GtkButton *button = GTK_BUTTON (widget);
1339 GtkBorder default_border;
1340 GtkBorder inner_border;
1344 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1345 gtk_widget_style_get (GTK_WIDGET (widget),
1346 "focus-line-width", &focus_width,
1347 "focus-padding", &focus_pad,
1350 requisition->width = ((GTK_CONTAINER (widget)->border_width +
1351 GTK_WIDGET (widget)->style->xthickness) * 2 +
1352 inner_border.left + inner_border.right);
1353 requisition->height = ((GTK_CONTAINER (widget)->border_width +
1354 GTK_WIDGET (widget)->style->ythickness) * 2 +
1355 inner_border.top + inner_border.bottom);
1357 if (gtk_widget_get_can_default (widget))
1359 requisition->width += default_border.left + default_border.right;
1360 requisition->height += default_border.top + default_border.bottom;
1363 if (GTK_BIN (button)->child && gtk_widget_get_visible (GTK_BIN (button)->child))
1365 GtkRequisition child_requisition;
1367 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
1369 requisition->width += child_requisition.width;
1370 requisition->height += child_requisition.height;
1373 requisition->width += 2 * (focus_width + focus_pad);
1374 requisition->height += 2 * (focus_width + focus_pad);
1378 gtk_button_size_allocate (GtkWidget *widget,
1379 GtkAllocation *allocation)
1381 GtkButton *button = GTK_BUTTON (widget);
1382 GtkAllocation child_allocation;
1384 gint border_width = GTK_CONTAINER (widget)->border_width;
1385 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
1386 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
1387 GtkBorder default_border;
1388 GtkBorder inner_border;
1392 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1393 gtk_widget_style_get (GTK_WIDGET (widget),
1394 "focus-line-width", &focus_width,
1395 "focus-padding", &focus_pad,
1399 widget->allocation = *allocation;
1401 if (GTK_WIDGET_REALIZED (widget))
1402 gdk_window_move_resize (button->event_window,
1403 widget->allocation.x + border_width,
1404 widget->allocation.y + border_width,
1405 widget->allocation.width - border_width * 2,
1406 widget->allocation.height - border_width * 2);
1408 if (GTK_BIN (button)->child && gtk_widget_get_visible (GTK_BIN (button)->child))
1410 child_allocation.x = widget->allocation.x + border_width + inner_border.left + xthickness;
1411 child_allocation.y = widget->allocation.y + border_width + inner_border.top + ythickness;
1413 child_allocation.width = MAX (1, widget->allocation.width -
1416 inner_border.right -
1418 child_allocation.height = MAX (1, widget->allocation.height -
1421 inner_border.bottom -
1424 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1426 child_allocation.x += default_border.left;
1427 child_allocation.y += default_border.top;
1428 child_allocation.width = MAX (1, child_allocation.width - default_border.left - default_border.right);
1429 child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
1432 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1434 child_allocation.x += focus_width + focus_pad;
1435 child_allocation.y += focus_width + focus_pad;
1436 child_allocation.width = MAX (1, child_allocation.width - (focus_width + focus_pad) * 2);
1437 child_allocation.height = MAX (1, child_allocation.height - (focus_width + focus_pad) * 2);
1440 if (button->depressed)
1442 gint child_displacement_x;
1443 gint child_displacement_y;
1445 gtk_widget_style_get (widget,
1446 "child-displacement-x", &child_displacement_x,
1447 "child-displacement-y", &child_displacement_y,
1449 child_allocation.x += child_displacement_x;
1450 child_allocation.y += child_displacement_y;
1453 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
1458 _gtk_button_paint (GtkButton *button,
1459 const GdkRectangle *area,
1460 GtkStateType state_type,
1461 GtkShadowType shadow_type,
1462 const gchar *main_detail,
1463 const gchar *default_detail)
1469 GtkBorder default_border;
1470 GtkBorder default_outside_border;
1471 gboolean interior_focus;
1475 widget = GTK_WIDGET (button);
1477 if (gtk_widget_is_drawable (widget))
1479 border_width = GTK_CONTAINER (widget)->border_width;
1481 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1482 gtk_widget_style_get (widget,
1483 "focus-line-width", &focus_width,
1484 "focus-padding", &focus_pad,
1487 x = widget->allocation.x + border_width;
1488 y = widget->allocation.y + border_width;
1489 width = widget->allocation.width - border_width * 2;
1490 height = widget->allocation.height - border_width * 2;
1492 if (gtk_widget_has_default (widget) &&
1493 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1495 gtk_paint_box (widget->style, widget->window,
1496 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1497 area, widget, "buttondefault",
1498 x, y, width, height);
1500 x += default_border.left;
1501 y += default_border.top;
1502 width -= default_border.left + default_border.right;
1503 height -= default_border.top + default_border.bottom;
1505 else if (gtk_widget_get_can_default (widget))
1507 x += default_outside_border.left;
1508 y += default_outside_border.top;
1509 width -= default_outside_border.left + default_outside_border.right;
1510 height -= default_outside_border.top + default_outside_border.bottom;
1513 if (!interior_focus && gtk_widget_has_focus (widget))
1515 x += focus_width + focus_pad;
1516 y += focus_width + focus_pad;
1517 width -= 2 * (focus_width + focus_pad);
1518 height -= 2 * (focus_width + focus_pad);
1521 if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1522 GTK_WIDGET_STATE(widget) == GTK_STATE_PRELIGHT)
1523 gtk_paint_box (widget->style, widget->window,
1525 shadow_type, area, widget, "button",
1526 x, y, width, height);
1528 if (gtk_widget_has_focus (widget))
1530 gint child_displacement_x;
1531 gint child_displacement_y;
1532 gboolean displace_focus;
1534 gtk_widget_style_get (widget,
1535 "child-displacement-y", &child_displacement_y,
1536 "child-displacement-x", &child_displacement_x,
1537 "displace-focus", &displace_focus,
1542 x += widget->style->xthickness + focus_pad;
1543 y += widget->style->ythickness + focus_pad;
1544 width -= 2 * (widget->style->xthickness + focus_pad);
1545 height -= 2 * (widget->style->ythickness + focus_pad);
1549 x -= focus_width + focus_pad;
1550 y -= focus_width + focus_pad;
1551 width += 2 * (focus_width + focus_pad);
1552 height += 2 * (focus_width + focus_pad);
1555 if (button->depressed && displace_focus)
1557 x += child_displacement_x;
1558 y += child_displacement_y;
1561 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1562 area, widget, "button",
1563 x, y, width, height);
1569 gtk_button_expose (GtkWidget *widget,
1570 GdkEventExpose *event)
1572 if (gtk_widget_is_drawable (widget))
1574 GtkButton *button = GTK_BUTTON (widget);
1576 _gtk_button_paint (button, &event->area,
1577 GTK_WIDGET_STATE (widget),
1578 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1579 "button", "buttondefault");
1581 GTK_WIDGET_CLASS (gtk_button_parent_class)->expose_event (widget, event);
1588 gtk_button_button_press (GtkWidget *widget,
1589 GdkEventButton *event)
1593 if (event->type == GDK_BUTTON_PRESS)
1595 button = GTK_BUTTON (widget);
1597 if (button->focus_on_click && !gtk_widget_has_focus (widget))
1598 gtk_widget_grab_focus (widget);
1600 if (event->button == 1)
1601 gtk_button_pressed (button);
1608 gtk_button_button_release (GtkWidget *widget,
1609 GdkEventButton *event)
1613 if (event->button == 1)
1615 button = GTK_BUTTON (widget);
1616 gtk_button_released (button);
1623 gtk_button_grab_broken (GtkWidget *widget,
1624 GdkEventGrabBroken *event)
1626 GtkButton *button = GTK_BUTTON (widget);
1629 /* Simulate a button release without the pointer in the button */
1630 if (button->button_down)
1632 save_in = button->in_button;
1633 button->in_button = FALSE;
1634 gtk_button_released (button);
1635 if (save_in != button->in_button)
1637 button->in_button = save_in;
1638 gtk_button_update_state (button);
1646 gtk_button_key_release (GtkWidget *widget,
1649 GtkButton *button = GTK_BUTTON (widget);
1651 if (button->activate_timeout)
1653 gtk_button_finish_activate (button, TRUE);
1656 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1657 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1663 gtk_button_enter_notify (GtkWidget *widget,
1664 GdkEventCrossing *event)
1667 GtkWidget *event_widget;
1669 button = GTK_BUTTON (widget);
1670 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1672 if ((event_widget == widget) &&
1673 (event->detail != GDK_NOTIFY_INFERIOR))
1675 button->in_button = TRUE;
1676 gtk_button_enter (button);
1683 gtk_button_leave_notify (GtkWidget *widget,
1684 GdkEventCrossing *event)
1687 GtkWidget *event_widget;
1689 button = GTK_BUTTON (widget);
1690 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1692 if ((event_widget == widget) &&
1693 (event->detail != GDK_NOTIFY_INFERIOR) &&
1694 (gtk_widget_get_sensitive (event_widget)))
1696 button->in_button = FALSE;
1697 gtk_button_leave (button);
1704 gtk_real_button_pressed (GtkButton *button)
1706 if (button->activate_timeout)
1709 button->button_down = TRUE;
1710 gtk_button_update_state (button);
1714 gtk_real_button_released (GtkButton *button)
1716 if (button->button_down)
1718 button->button_down = FALSE;
1720 if (button->activate_timeout)
1723 if (button->in_button)
1724 gtk_button_clicked (button);
1726 gtk_button_update_state (button);
1731 gtk_real_button_clicked (GtkButton *button)
1733 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1736 gtk_action_activate (priv->action);
1740 button_activate_timeout (gpointer data)
1742 gtk_button_finish_activate (data, TRUE);
1748 gtk_real_button_activate (GtkButton *button)
1750 GtkWidget *widget = GTK_WIDGET (button);
1751 GtkButtonPrivate *priv;
1754 priv = GTK_BUTTON_GET_PRIVATE (button);
1756 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1758 time = gtk_get_current_event_time ();
1759 if (gdk_keyboard_grab (button->event_window, TRUE, time) ==
1762 priv->has_grab = TRUE;
1763 priv->grab_time = time;
1766 gtk_grab_add (widget);
1768 button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1769 button_activate_timeout,
1771 button->button_down = TRUE;
1772 gtk_button_update_state (button);
1773 gtk_widget_queue_draw (GTK_WIDGET (button));
1778 gtk_button_finish_activate (GtkButton *button,
1781 GtkWidget *widget = GTK_WIDGET (button);
1782 GtkButtonPrivate *priv;
1784 priv = GTK_BUTTON_GET_PRIVATE (button);
1786 g_source_remove (button->activate_timeout);
1787 button->activate_timeout = 0;
1791 gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1794 gtk_grab_remove (widget);
1796 button->button_down = FALSE;
1798 gtk_button_update_state (button);
1799 gtk_widget_queue_draw (GTK_WIDGET (button));
1802 gtk_button_clicked (button);
1806 * gtk_button_set_label:
1807 * @button: a #GtkButton
1810 * Sets the text of the label of the button to @str. This text is
1811 * also used to select the stock item if gtk_button_set_use_stock()
1814 * This will also clear any previously set labels.
1817 gtk_button_set_label (GtkButton *button,
1822 g_return_if_fail (GTK_IS_BUTTON (button));
1824 new_label = g_strdup (label);
1825 g_free (button->label_text);
1826 button->label_text = new_label;
1828 gtk_button_construct_child (button);
1830 g_object_notify (G_OBJECT (button), "label");
1834 * gtk_button_get_label:
1835 * @button: a #GtkButton
1837 * Fetches the text from the label of the button, as set by
1838 * gtk_button_set_label(). If the label text has not
1839 * been set the return value will be %NULL. This will be the
1840 * case if you create an empty button with gtk_button_new() to
1841 * use as a container.
1843 * Return value: The text of the label widget. This string is owned
1844 * by the widget and must not be modified or freed.
1846 G_CONST_RETURN gchar *
1847 gtk_button_get_label (GtkButton *button)
1849 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1851 return button->label_text;
1855 * gtk_button_set_use_underline:
1856 * @button: a #GtkButton
1857 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1859 * If true, an underline in the text of the button label indicates
1860 * the next character should be used for the mnemonic accelerator key.
1863 gtk_button_set_use_underline (GtkButton *button,
1864 gboolean use_underline)
1866 g_return_if_fail (GTK_IS_BUTTON (button));
1868 use_underline = use_underline != FALSE;
1870 if (use_underline != button->use_underline)
1872 button->use_underline = use_underline;
1874 gtk_button_construct_child (button);
1876 g_object_notify (G_OBJECT (button), "use-underline");
1881 * gtk_button_get_use_underline:
1882 * @button: a #GtkButton
1884 * Returns whether an embedded underline in the button label indicates a
1885 * mnemonic. See gtk_button_set_use_underline ().
1887 * Return value: %TRUE if an embedded underline in the button label
1888 * indicates the mnemonic accelerator keys.
1891 gtk_button_get_use_underline (GtkButton *button)
1893 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1895 return button->use_underline;
1899 * gtk_button_set_use_stock:
1900 * @button: a #GtkButton
1901 * @use_stock: %TRUE if the button should use a stock item
1903 * If %TRUE, the label set on the button is used as a
1904 * stock id to select the stock item for the button.
1907 gtk_button_set_use_stock (GtkButton *button,
1910 g_return_if_fail (GTK_IS_BUTTON (button));
1912 use_stock = use_stock != FALSE;
1914 if (use_stock != button->use_stock)
1916 button->use_stock = use_stock;
1918 gtk_button_construct_child (button);
1920 g_object_notify (G_OBJECT (button), "use-stock");
1925 * gtk_button_get_use_stock:
1926 * @button: a #GtkButton
1928 * Returns whether the button label is a stock item.
1930 * Return value: %TRUE if the button label is used to
1931 * select a stock item instead of being
1932 * used directly as the label text.
1935 gtk_button_get_use_stock (GtkButton *button)
1937 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1939 return button->use_stock;
1943 * gtk_button_set_focus_on_click:
1944 * @button: a #GtkButton
1945 * @focus_on_click: whether the button grabs focus when clicked with the mouse
1947 * Sets whether the button will grab focus when it is clicked with the mouse.
1948 * Making mouse clicks not grab focus is useful in places like toolbars where
1949 * you don't want the keyboard focus removed from the main area of the
1955 gtk_button_set_focus_on_click (GtkButton *button,
1956 gboolean focus_on_click)
1958 g_return_if_fail (GTK_IS_BUTTON (button));
1960 focus_on_click = focus_on_click != FALSE;
1962 if (button->focus_on_click != focus_on_click)
1964 button->focus_on_click = focus_on_click;
1966 g_object_notify (G_OBJECT (button), "focus-on-click");
1971 * gtk_button_get_focus_on_click:
1972 * @button: a #GtkButton
1974 * Returns whether the button grabs focus when it is clicked with the mouse.
1975 * See gtk_button_set_focus_on_click().
1977 * Return value: %TRUE if the button grabs focus when it is clicked with
1983 gtk_button_get_focus_on_click (GtkButton *button)
1985 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1987 return button->focus_on_click;
1991 * gtk_button_set_alignment:
1992 * @button: a #GtkButton
1993 * @xalign: the horizontal position of the child, 0.0 is left aligned,
1994 * 1.0 is right aligned
1995 * @yalign: the vertical position of the child, 0.0 is top aligned,
1996 * 1.0 is bottom aligned
1998 * Sets the alignment of the child. This property has no effect unless
1999 * the child is a #GtkMisc or a #GtkAligment.
2004 gtk_button_set_alignment (GtkButton *button,
2008 GtkButtonPrivate *priv;
2010 g_return_if_fail (GTK_IS_BUTTON (button));
2012 priv = GTK_BUTTON_GET_PRIVATE (button);
2014 priv->xalign = xalign;
2015 priv->yalign = yalign;
2016 priv->align_set = 1;
2018 maybe_set_alignment (button, GTK_BIN (button)->child);
2020 g_object_freeze_notify (G_OBJECT (button));
2021 g_object_notify (G_OBJECT (button), "xalign");
2022 g_object_notify (G_OBJECT (button), "yalign");
2023 g_object_thaw_notify (G_OBJECT (button));
2027 * gtk_button_get_alignment:
2028 * @button: a #GtkButton
2029 * @xalign: return location for horizontal alignment
2030 * @yalign: return location for vertical alignment
2032 * Gets the alignment of the child in the button.
2037 gtk_button_get_alignment (GtkButton *button,
2041 GtkButtonPrivate *priv;
2043 g_return_if_fail (GTK_IS_BUTTON (button));
2045 priv = GTK_BUTTON_GET_PRIVATE (button);
2048 *xalign = priv->xalign;
2051 *yalign = priv->yalign;
2055 * _gtk_button_set_depressed:
2056 * @button: a #GtkButton
2057 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2059 * Sets whether the button is currently drawn as down or not. This is
2060 * purely a visual setting, and is meant only for use by derived widgets
2061 * such as #GtkToggleButton.
2064 _gtk_button_set_depressed (GtkButton *button,
2067 GtkWidget *widget = GTK_WIDGET (button);
2069 depressed = depressed != FALSE;
2071 if (depressed != button->depressed)
2073 button->depressed = depressed;
2074 gtk_widget_queue_resize (widget);
2079 gtk_button_update_state (GtkButton *button)
2082 GtkStateType new_state;
2084 if (button->activate_timeout)
2085 depressed = button->depress_on_activate;
2087 depressed = button->in_button && button->button_down;
2089 if (button->in_button && (!button->button_down || !depressed))
2090 new_state = GTK_STATE_PRELIGHT;
2092 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2094 _gtk_button_set_depressed (button, depressed);
2095 gtk_widget_set_state (GTK_WIDGET (button), new_state);
2099 show_image_change_notify (GtkButton *button)
2101 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2105 if (show_image (button))
2106 gtk_widget_show (priv->image);
2108 gtk_widget_hide (priv->image);
2113 traverse_container (GtkWidget *widget,
2116 if (GTK_IS_BUTTON (widget))
2117 show_image_change_notify (GTK_BUTTON (widget));
2118 else if (GTK_IS_CONTAINER (widget))
2119 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2123 gtk_button_setting_changed (GtkSettings *settings)
2127 list = gtk_window_list_toplevels ();
2129 for (l = list; l; l = l->next)
2130 gtk_container_forall (GTK_CONTAINER (l->data),
2131 traverse_container, NULL);
2138 gtk_button_screen_changed (GtkWidget *widget,
2139 GdkScreen *previous_screen)
2142 GtkSettings *settings;
2143 guint show_image_connection;
2145 if (!gtk_widget_has_screen (widget))
2148 button = GTK_BUTTON (widget);
2150 /* If the button is being pressed while the screen changes the
2151 release might never occur, so we reset the state. */
2152 if (button->button_down)
2154 button->button_down = FALSE;
2155 gtk_button_update_state (button);
2158 settings = gtk_widget_get_settings (widget);
2160 show_image_connection =
2161 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
2162 "gtk-button-connection"));
2164 if (show_image_connection)
2167 show_image_connection =
2168 g_signal_connect (settings, "notify::gtk-button-images",
2169 G_CALLBACK (gtk_button_setting_changed), NULL);
2170 g_object_set_data (G_OBJECT (settings),
2171 I_("gtk-button-connection"),
2172 GUINT_TO_POINTER (show_image_connection));
2174 show_image_change_notify (button);
2178 gtk_button_state_changed (GtkWidget *widget,
2179 GtkStateType previous_state)
2181 GtkButton *button = GTK_BUTTON (widget);
2183 if (!gtk_widget_is_sensitive (widget))
2185 button->in_button = FALSE;
2186 gtk_real_button_released (button);
2191 gtk_button_grab_notify (GtkWidget *widget,
2192 gboolean was_grabbed)
2194 GtkButton *button = GTK_BUTTON (widget);
2199 save_in = button->in_button;
2200 button->in_button = FALSE;
2201 gtk_real_button_released (button);
2202 if (save_in != button->in_button)
2204 button->in_button = save_in;
2205 gtk_button_update_state (button);
2211 * gtk_button_set_image:
2212 * @button: a #GtkButton
2213 * @image: a widget to set as the image for the button
2215 * Set the image of @button to the given widget. Note that
2216 * it depends on the #GtkSettings:gtk-button-images setting whether the
2217 * image will be displayed or not, you don't have to call
2218 * gtk_widget_show() on @image yourself.
2223 gtk_button_set_image (GtkButton *button,
2226 GtkButtonPrivate *priv;
2228 g_return_if_fail (GTK_IS_BUTTON (button));
2229 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2231 priv = GTK_BUTTON_GET_PRIVATE (button);
2233 if (priv->image && priv->image->parent)
2234 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
2236 priv->image = image;
2237 priv->image_is_stock = (image == NULL);
2239 gtk_button_construct_child (button);
2241 g_object_notify (G_OBJECT (button), "image");
2245 * gtk_button_get_image:
2246 * @button: a #GtkButton
2248 * Gets the widget that is currenty set as the image of @button.
2249 * This may have been explicitly set by gtk_button_set_image()
2250 * or constructed by gtk_button_new_from_stock().
2252 * Return value: a #GtkWidget or %NULL in case there is no image
2257 gtk_button_get_image (GtkButton *button)
2259 GtkButtonPrivate *priv;
2261 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2263 priv = GTK_BUTTON_GET_PRIVATE (button);
2269 * gtk_button_set_image_position:
2270 * @button: a #GtkButton
2271 * @position: the position
2273 * Sets the position of the image relative to the text
2274 * inside the button.
2279 gtk_button_set_image_position (GtkButton *button,
2280 GtkPositionType position)
2283 GtkButtonPrivate *priv;
2285 g_return_if_fail (GTK_IS_BUTTON (button));
2286 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2288 priv = GTK_BUTTON_GET_PRIVATE (button);
2290 if (priv->image_position != position)
2292 priv->image_position = position;
2294 gtk_button_construct_child (button);
2296 g_object_notify (G_OBJECT (button), "image-position");
2301 * gtk_button_get_image_position:
2302 * @button: a #GtkButton
2304 * Gets the position of the image relative to the text
2305 * inside the button.
2307 * Return value: the position
2312 gtk_button_get_image_position (GtkButton *button)
2314 GtkButtonPrivate *priv;
2316 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2318 priv = GTK_BUTTON_GET_PRIVATE (button);
2320 return priv->image_position;
2324 #define __GTK_BUTTON_C__
2325 #include "gtkaliasdef.c"