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];
431 gtk_widget_class_install_style_property (widget_class,
432 g_param_spec_boxed ("default-border",
433 P_("Default Spacing"),
434 P_("Extra space to add for CAN_DEFAULT buttons"),
436 GTK_PARAM_READABLE));
438 gtk_widget_class_install_style_property (widget_class,
439 g_param_spec_boxed ("default-outside-border",
440 P_("Default Outside Spacing"),
441 P_("Extra space to add for CAN_DEFAULT buttons that is always drawn outside the border"),
443 GTK_PARAM_READABLE));
444 gtk_widget_class_install_style_property (widget_class,
445 g_param_spec_int ("child-displacement-x",
446 P_("Child X Displacement"),
447 P_("How far in the x direction to move the child when the button is depressed"),
451 GTK_PARAM_READABLE));
452 gtk_widget_class_install_style_property (widget_class,
453 g_param_spec_int ("child-displacement-y",
454 P_("Child Y Displacement"),
455 P_("How far in the y direction to move the child when the button is depressed"),
459 GTK_PARAM_READABLE));
462 * GtkButton:displace-focus:
464 * Whether the child_displacement_x/child_displacement_y properties
465 * should also affect the focus rectangle.
469 gtk_widget_class_install_style_property (widget_class,
470 g_param_spec_boolean ("displace-focus",
471 P_("Displace focus"),
472 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
474 GTK_PARAM_READABLE));
477 * GtkButton:inner-border:
479 * Sets the border between the button edges and child.
483 gtk_widget_class_install_style_property (widget_class,
484 g_param_spec_boxed ("inner-border",
486 P_("Border between button edges and child."),
488 GTK_PARAM_READABLE));
491 * GtkButton::image-spacing:
493 * Spacing in pixels between the image and label.
497 gtk_widget_class_install_style_property (widget_class,
498 g_param_spec_int ("image-spacing",
500 P_("Spacing in pixels between the image and label"),
504 GTK_PARAM_READABLE));
507 * GtkSettings::gtk-button-images:
509 * Whether images should be shown on buttons
513 gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
514 P_("Show button images"),
515 P_("Whether images should be shown on buttons"),
517 GTK_PARAM_READWRITE));
519 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
523 gtk_button_init (GtkButton *button)
525 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
527 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
528 GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);
530 button->label_text = NULL;
532 button->constructed = FALSE;
533 button->in_button = FALSE;
534 button->button_down = FALSE;
535 button->relief = GTK_RELIEF_NORMAL;
536 button->use_stock = FALSE;
537 button->use_underline = FALSE;
538 button->depressed = FALSE;
539 button->depress_on_activate = TRUE;
540 button->focus_on_click = TRUE;
545 priv->image_is_stock = TRUE;
546 priv->image_position = GTK_POS_LEFT;
547 priv->use_action_appearance = TRUE;
551 gtk_button_destroy (GtkObject *object)
553 GtkButton *button = GTK_BUTTON (object);
555 if (button->label_text)
557 g_free (button->label_text);
558 button->label_text = NULL;
561 GTK_OBJECT_CLASS (gtk_button_parent_class)->destroy (object);
565 gtk_button_constructor (GType type,
566 guint n_construct_properties,
567 GObjectConstructParam *construct_params)
572 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
573 n_construct_properties,
576 button = GTK_BUTTON (object);
577 button->constructed = TRUE;
579 if (button->label_text != NULL)
580 gtk_button_construct_child (button);
587 gtk_button_child_type (GtkContainer *container)
589 if (!GTK_BIN (container)->child)
590 return GTK_TYPE_WIDGET;
596 maybe_set_alignment (GtkButton *button,
599 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
601 if (GTK_IS_MISC (widget))
603 GtkMisc *misc = GTK_MISC (widget);
606 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
608 else if (GTK_IS_ALIGNMENT (widget))
610 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
613 gtk_alignment_set (alignment, priv->xalign, priv->yalign,
614 alignment->xscale, alignment->yscale);
619 gtk_button_add (GtkContainer *container,
622 maybe_set_alignment (GTK_BUTTON (container), widget);
624 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
628 gtk_button_dispose (GObject *object)
630 GtkButton *button = GTK_BUTTON (object);
631 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
635 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
638 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
642 gtk_button_set_property (GObject *object,
647 GtkButton *button = GTK_BUTTON (object);
648 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
653 gtk_button_set_label (button, g_value_get_string (value));
656 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
659 gtk_button_set_relief (button, g_value_get_enum (value));
661 case PROP_USE_UNDERLINE:
662 gtk_button_set_use_underline (button, g_value_get_boolean (value));
665 gtk_button_set_use_stock (button, g_value_get_boolean (value));
667 case PROP_FOCUS_ON_CLICK:
668 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
671 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
674 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
676 case PROP_IMAGE_POSITION:
677 gtk_button_set_image_position (button, g_value_get_enum (value));
679 case PROP_ACTIVATABLE_RELATED_ACTION:
680 gtk_button_set_related_action (button, g_value_get_object (value));
682 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
683 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
686 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
692 gtk_button_get_property (GObject *object,
697 GtkButton *button = GTK_BUTTON (object);
698 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
703 g_value_set_string (value, button->label_text);
706 g_value_set_object (value, (GObject *)priv->image);
709 g_value_set_enum (value, gtk_button_get_relief (button));
711 case PROP_USE_UNDERLINE:
712 g_value_set_boolean (value, button->use_underline);
715 g_value_set_boolean (value, button->use_stock);
717 case PROP_FOCUS_ON_CLICK:
718 g_value_set_boolean (value, button->focus_on_click);
721 g_value_set_float (value, priv->xalign);
724 g_value_set_float (value, priv->yalign);
726 case PROP_IMAGE_POSITION:
727 g_value_set_enum (value, priv->image_position);
729 case PROP_ACTIVATABLE_RELATED_ACTION:
730 g_value_set_object (value, priv->action);
732 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
733 g_value_set_boolean (value, priv->use_action_appearance);
736 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
742 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
744 iface->update = gtk_button_update;
745 iface->sync_action_properties = gtk_button_sync_action_properties;
749 activatable_update_stock_id (GtkButton *button,
752 if (!gtk_button_get_use_stock (button))
755 gtk_button_set_label (button, gtk_action_get_stock_id (action));
759 activatable_update_short_label (GtkButton *button,
764 if (gtk_button_get_use_stock (button))
767 image = gtk_button_get_image (button);
769 /* Dont touch custom child... */
770 if (GTK_IS_IMAGE (image) ||
771 GTK_BIN (button)->child == NULL ||
772 GTK_IS_LABEL (GTK_BIN (button)->child))
774 gtk_button_set_label (button, gtk_action_get_short_label (action));
775 gtk_button_set_use_underline (button, TRUE);
780 activatable_update_icon_name (GtkButton *button,
785 if (gtk_button_get_use_stock (button))
788 image = gtk_button_get_image (button);
790 if (GTK_IS_IMAGE (image) &&
791 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
792 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
793 gtk_image_set_from_icon_name (GTK_IMAGE (image),
794 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
798 activatable_update_gicon (GtkButton *button,
801 GtkWidget *image = gtk_button_get_image (button);
802 GIcon *icon = gtk_action_get_gicon (action);
804 if (GTK_IS_IMAGE (image) &&
805 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
806 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
807 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
811 gtk_button_update (GtkActivatable *activatable,
813 const gchar *property_name)
815 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
817 if (strcmp (property_name, "visible") == 0)
819 if (gtk_action_is_visible (action))
820 gtk_widget_show (GTK_WIDGET (activatable));
822 gtk_widget_hide (GTK_WIDGET (activatable));
824 else if (strcmp (property_name, "sensitive") == 0)
825 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
827 if (!priv->use_action_appearance)
830 if (strcmp (property_name, "stock-id") == 0)
831 activatable_update_stock_id (GTK_BUTTON (activatable), action);
832 else if (strcmp (property_name, "gicon") == 0)
833 activatable_update_gicon (GTK_BUTTON (activatable), action);
834 else if (strcmp (property_name, "short-label") == 0)
835 activatable_update_short_label (GTK_BUTTON (activatable), action);
836 else if (strcmp (property_name, "icon-name") == 0)
837 activatable_update_icon_name (GTK_BUTTON (activatable), action);
841 gtk_button_sync_action_properties (GtkActivatable *activatable,
844 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
849 if (gtk_action_is_visible (action))
850 gtk_widget_show (GTK_WIDGET (activatable));
852 gtk_widget_hide (GTK_WIDGET (activatable));
854 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
856 if (priv->use_action_appearance)
858 activatable_update_stock_id (GTK_BUTTON (activatable), action);
859 activatable_update_short_label (GTK_BUTTON (activatable), action);
860 activatable_update_gicon (GTK_BUTTON (activatable), action);
861 activatable_update_icon_name (GTK_BUTTON (activatable), action);
866 gtk_button_set_related_action (GtkButton *button,
869 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
871 if (priv->action == action)
874 /* This should be a default handler, but for compatibility reasons
875 * we need to support derived classes that don't chain up their
878 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
880 g_signal_connect_after (button, "clicked",
881 G_CALLBACK (gtk_real_button_clicked), NULL);
883 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
885 priv->action = action;
889 gtk_button_set_use_action_appearance (GtkButton *button,
890 gboolean use_appearance)
892 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
894 if (priv->use_action_appearance != use_appearance)
896 priv->use_action_appearance = use_appearance;
898 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
903 gtk_button_new (void)
905 return g_object_new (GTK_TYPE_BUTTON, NULL);
909 show_image (GtkButton *button)
913 if (button->label_text)
915 GtkSettings *settings;
917 settings = gtk_widget_get_settings (GTK_WIDGET (button));
918 g_object_get (settings, "gtk-button-images", &show, NULL);
927 gtk_button_construct_child (GtkButton *button)
929 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
934 GtkWidget *image = NULL;
935 gchar *label_text = NULL;
938 if (!button->constructed)
941 if (!button->label_text && !priv->image)
944 gtk_widget_style_get (GTK_WIDGET (button),
945 "image-spacing", &image_spacing,
948 if (priv->image && !priv->image_is_stock)
950 image = g_object_ref (priv->image);
952 gtk_container_remove (GTK_CONTAINER (image->parent), image);
957 if (GTK_BIN (button)->child)
958 gtk_container_remove (GTK_CONTAINER (button),
959 GTK_BIN (button)->child);
961 if (button->use_stock &&
962 button->label_text &&
963 gtk_stock_lookup (button->label_text, &item))
966 image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
968 label_text = item.label;
971 label_text = button->label_text;
976 g_object_set (priv->image,
977 "visible", show_image (button),
981 if (priv->image_position == GTK_POS_LEFT ||
982 priv->image_position == GTK_POS_RIGHT)
983 box = gtk_hbox_new (FALSE, image_spacing);
985 box = gtk_vbox_new (FALSE, image_spacing);
988 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
990 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
992 if (priv->image_position == GTK_POS_LEFT ||
993 priv->image_position == GTK_POS_TOP)
994 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
996 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1000 label = gtk_label_new_with_mnemonic (label_text);
1001 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1002 GTK_WIDGET (button));
1004 if (priv->image_position == GTK_POS_RIGHT ||
1005 priv->image_position == GTK_POS_BOTTOM)
1006 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1008 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1011 gtk_container_add (GTK_CONTAINER (button), align);
1012 gtk_container_add (GTK_CONTAINER (align), box);
1013 gtk_widget_show_all (align);
1015 g_object_unref (image);
1020 if (button->use_underline)
1022 label = gtk_label_new_with_mnemonic (button->label_text);
1023 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1026 label = gtk_label_new (button->label_text);
1028 if (priv->align_set)
1029 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1031 gtk_widget_show (label);
1032 gtk_container_add (GTK_CONTAINER (button), label);
1037 gtk_button_new_with_label (const gchar *label)
1039 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1043 * gtk_button_new_from_stock:
1044 * @stock_id: the name of the stock item
1046 * Creates a new #GtkButton containing the image and text from a stock item.
1047 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1050 * If @stock_id is unknown, then it will be treated as a mnemonic
1051 * label (as for gtk_button_new_with_mnemonic()).
1053 * Returns: a new #GtkButton
1056 gtk_button_new_from_stock (const gchar *stock_id)
1058 return g_object_new (GTK_TYPE_BUTTON,
1061 "use-underline", TRUE,
1066 * gtk_button_new_with_mnemonic:
1067 * @label: The text of the button, with an underscore in front of the
1068 * mnemonic character
1069 * @returns: a new #GtkButton
1071 * Creates a new #GtkButton containing a label.
1072 * If characters in @label are preceded by an underscore, they are underlined.
1073 * If you need a literal underscore character in a label, use '__' (two
1074 * underscores). The first underlined character represents a keyboard
1075 * accelerator called a mnemonic.
1076 * Pressing Alt and that key activates the button.
1079 gtk_button_new_with_mnemonic (const gchar *label)
1081 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1085 gtk_button_pressed (GtkButton *button)
1087 g_return_if_fail (GTK_IS_BUTTON (button));
1090 g_signal_emit (button, button_signals[PRESSED], 0);
1094 gtk_button_released (GtkButton *button)
1096 g_return_if_fail (GTK_IS_BUTTON (button));
1098 g_signal_emit (button, button_signals[RELEASED], 0);
1102 gtk_button_clicked (GtkButton *button)
1104 g_return_if_fail (GTK_IS_BUTTON (button));
1106 g_signal_emit (button, button_signals[CLICKED], 0);
1110 gtk_button_enter (GtkButton *button)
1112 g_return_if_fail (GTK_IS_BUTTON (button));
1114 g_signal_emit (button, button_signals[ENTER], 0);
1118 gtk_button_leave (GtkButton *button)
1120 g_return_if_fail (GTK_IS_BUTTON (button));
1122 g_signal_emit (button, button_signals[LEAVE], 0);
1126 gtk_button_set_relief (GtkButton *button,
1127 GtkReliefStyle newrelief)
1129 g_return_if_fail (GTK_IS_BUTTON (button));
1131 if (newrelief != button->relief)
1133 button->relief = newrelief;
1134 g_object_notify (G_OBJECT (button), "relief");
1135 gtk_widget_queue_draw (GTK_WIDGET (button));
1140 gtk_button_get_relief (GtkButton *button)
1142 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1144 return button->relief;
1148 gtk_button_realize (GtkWidget *widget)
1151 GdkWindowAttr attributes;
1152 gint attributes_mask;
1155 button = GTK_BUTTON (widget);
1156 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1158 border_width = GTK_CONTAINER (widget)->border_width;
1160 attributes.window_type = GDK_WINDOW_CHILD;
1161 attributes.x = widget->allocation.x + border_width;
1162 attributes.y = widget->allocation.y + border_width;
1163 attributes.width = widget->allocation.width - border_width * 2;
1164 attributes.height = widget->allocation.height - border_width * 2;
1165 attributes.wclass = GDK_INPUT_ONLY;
1166 attributes.event_mask = gtk_widget_get_events (widget);
1167 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1168 GDK_BUTTON_RELEASE_MASK |
1169 GDK_ENTER_NOTIFY_MASK |
1170 GDK_LEAVE_NOTIFY_MASK);
1172 attributes_mask = GDK_WA_X | GDK_WA_Y;
1174 widget->window = gtk_widget_get_parent_window (widget);
1175 g_object_ref (widget->window);
1177 button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1178 &attributes, attributes_mask);
1179 gdk_window_set_user_data (button->event_window, button);
1181 widget->style = gtk_style_attach (widget->style, widget->window);
1185 gtk_button_unrealize (GtkWidget *widget)
1187 GtkButton *button = GTK_BUTTON (widget);
1189 if (button->activate_timeout)
1190 gtk_button_finish_activate (button, FALSE);
1192 if (button->event_window)
1194 gdk_window_set_user_data (button->event_window, NULL);
1195 gdk_window_destroy (button->event_window);
1196 button->event_window = NULL;
1199 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1203 gtk_button_map (GtkWidget *widget)
1205 GtkButton *button = GTK_BUTTON (widget);
1207 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1209 if (button->event_window)
1210 gdk_window_show (button->event_window);
1214 gtk_button_unmap (GtkWidget *widget)
1216 GtkButton *button = GTK_BUTTON (widget);
1218 if (button->event_window)
1219 gdk_window_hide (button->event_window);
1221 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1225 gtk_button_update_image_spacing (GtkButton *button)
1227 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1231 /* Keep in sync with gtk_button_construct_child,
1232 * we only want to update the spacing if the box
1233 * was constructed there.
1235 if (!button->constructed || !priv->image)
1238 child = GTK_BIN (button)->child;
1239 if (GTK_IS_ALIGNMENT (child))
1241 child = GTK_BIN (child)->child;
1242 if (GTK_IS_BOX (child))
1244 gtk_widget_style_get (GTK_WIDGET (button),
1245 "image-spacing", &spacing,
1248 gtk_box_set_spacing (GTK_BOX (child), spacing);
1254 gtk_button_style_set (GtkWidget *widget,
1255 GtkStyle *prev_style)
1257 gtk_button_update_image_spacing (GTK_BUTTON (widget));
1261 gtk_button_get_props (GtkButton *button,
1262 GtkBorder *default_border,
1263 GtkBorder *default_outside_border,
1264 GtkBorder *inner_border,
1265 gboolean *interior_focus)
1267 GtkWidget *widget = GTK_WIDGET (button);
1268 GtkBorder *tmp_border;
1272 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1276 *default_border = *tmp_border;
1277 gtk_border_free (tmp_border);
1280 *default_border = default_default_border;
1283 if (default_outside_border)
1285 gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1289 *default_outside_border = *tmp_border;
1290 gtk_border_free (tmp_border);
1293 *default_outside_border = default_default_outside_border;
1298 gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1302 *inner_border = *tmp_border;
1303 gtk_border_free (tmp_border);
1306 *inner_border = default_inner_border;
1310 gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1314 gtk_button_size_request (GtkWidget *widget,
1315 GtkRequisition *requisition)
1317 GtkButton *button = GTK_BUTTON (widget);
1318 GtkBorder default_border;
1319 GtkBorder inner_border;
1323 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1324 gtk_widget_style_get (GTK_WIDGET (widget),
1325 "focus-line-width", &focus_width,
1326 "focus-padding", &focus_pad,
1329 requisition->width = ((GTK_CONTAINER (widget)->border_width +
1330 GTK_WIDGET (widget)->style->xthickness) * 2 +
1331 inner_border.left + inner_border.right);
1332 requisition->height = ((GTK_CONTAINER (widget)->border_width +
1333 GTK_WIDGET (widget)->style->ythickness) * 2 +
1334 inner_border.top + inner_border.bottom);
1336 if (GTK_WIDGET_CAN_DEFAULT (widget))
1338 requisition->width += default_border.left + default_border.right;
1339 requisition->height += default_border.top + default_border.bottom;
1342 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
1344 GtkRequisition child_requisition;
1346 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
1348 requisition->width += child_requisition.width;
1349 requisition->height += child_requisition.height;
1352 requisition->width += 2 * (focus_width + focus_pad);
1353 requisition->height += 2 * (focus_width + focus_pad);
1357 gtk_button_size_allocate (GtkWidget *widget,
1358 GtkAllocation *allocation)
1360 GtkButton *button = GTK_BUTTON (widget);
1361 GtkAllocation child_allocation;
1363 gint border_width = GTK_CONTAINER (widget)->border_width;
1364 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
1365 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
1366 GtkBorder default_border;
1367 GtkBorder inner_border;
1371 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1372 gtk_widget_style_get (GTK_WIDGET (widget),
1373 "focus-line-width", &focus_width,
1374 "focus-padding", &focus_pad,
1378 widget->allocation = *allocation;
1380 if (GTK_WIDGET_REALIZED (widget))
1381 gdk_window_move_resize (button->event_window,
1382 widget->allocation.x + border_width,
1383 widget->allocation.y + border_width,
1384 widget->allocation.width - border_width * 2,
1385 widget->allocation.height - border_width * 2);
1387 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
1389 child_allocation.x = widget->allocation.x + border_width + inner_border.left + xthickness;
1390 child_allocation.y = widget->allocation.y + border_width + inner_border.top + ythickness;
1392 child_allocation.width = MAX (1, widget->allocation.width -
1395 inner_border.right -
1397 child_allocation.height = MAX (1, widget->allocation.height -
1400 inner_border.bottom -
1403 if (GTK_WIDGET_CAN_DEFAULT (button))
1405 child_allocation.x += default_border.left;
1406 child_allocation.y += default_border.top;
1407 child_allocation.width = MAX (1, child_allocation.width - default_border.left - default_border.right);
1408 child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
1411 if (GTK_WIDGET_CAN_FOCUS (button))
1413 child_allocation.x += focus_width + focus_pad;
1414 child_allocation.y += focus_width + focus_pad;
1415 child_allocation.width = MAX (1, child_allocation.width - (focus_width + focus_pad) * 2);
1416 child_allocation.height = MAX (1, child_allocation.height - (focus_width + focus_pad) * 2);
1419 if (button->depressed)
1421 gint child_displacement_x;
1422 gint child_displacement_y;
1424 gtk_widget_style_get (widget,
1425 "child-displacement-x", &child_displacement_x,
1426 "child-displacement-y", &child_displacement_y,
1428 child_allocation.x += child_displacement_x;
1429 child_allocation.y += child_displacement_y;
1432 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
1437 _gtk_button_paint (GtkButton *button,
1438 const GdkRectangle *area,
1439 GtkStateType state_type,
1440 GtkShadowType shadow_type,
1441 const gchar *main_detail,
1442 const gchar *default_detail)
1448 GtkBorder default_border;
1449 GtkBorder default_outside_border;
1450 gboolean interior_focus;
1454 if (GTK_WIDGET_DRAWABLE (button))
1456 widget = GTK_WIDGET (button);
1457 border_width = GTK_CONTAINER (widget)->border_width;
1459 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1460 gtk_widget_style_get (GTK_WIDGET (widget),
1461 "focus-line-width", &focus_width,
1462 "focus-padding", &focus_pad,
1465 x = widget->allocation.x + border_width;
1466 y = widget->allocation.y + border_width;
1467 width = widget->allocation.width - border_width * 2;
1468 height = widget->allocation.height - border_width * 2;
1470 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
1471 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1473 gtk_paint_box (widget->style, widget->window,
1474 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1475 area, widget, "buttondefault",
1476 x, y, width, height);
1478 x += default_border.left;
1479 y += default_border.top;
1480 width -= default_border.left + default_border.right;
1481 height -= default_border.top + default_border.bottom;
1483 else if (GTK_WIDGET_CAN_DEFAULT (widget))
1485 x += default_outside_border.left;
1486 y += default_outside_border.top;
1487 width -= default_outside_border.left + default_outside_border.right;
1488 height -= default_outside_border.top + default_outside_border.bottom;
1491 if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
1493 x += focus_width + focus_pad;
1494 y += focus_width + focus_pad;
1495 width -= 2 * (focus_width + focus_pad);
1496 height -= 2 * (focus_width + focus_pad);
1499 if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1500 GTK_WIDGET_STATE(widget) == GTK_STATE_PRELIGHT)
1501 gtk_paint_box (widget->style, widget->window,
1503 shadow_type, area, widget, "button",
1504 x, y, width, height);
1506 if (GTK_WIDGET_HAS_FOCUS (widget))
1508 gint child_displacement_x;
1509 gint child_displacement_y;
1510 gboolean displace_focus;
1512 gtk_widget_style_get (GTK_WIDGET (widget),
1513 "child-displacement-y", &child_displacement_y,
1514 "child-displacement-x", &child_displacement_x,
1515 "displace-focus", &displace_focus,
1520 x += widget->style->xthickness + focus_pad;
1521 y += widget->style->ythickness + focus_pad;
1522 width -= 2 * (widget->style->xthickness + focus_pad);
1523 height -= 2 * (widget->style->ythickness + focus_pad);
1527 x -= focus_width + focus_pad;
1528 y -= focus_width + focus_pad;
1529 width += 2 * (focus_width + focus_pad);
1530 height += 2 * (focus_width + focus_pad);
1533 if (button->depressed && displace_focus)
1535 x += child_displacement_x;
1536 y += child_displacement_y;
1539 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1540 area, widget, "button",
1541 x, y, width, height);
1547 gtk_button_expose (GtkWidget *widget,
1548 GdkEventExpose *event)
1550 if (GTK_WIDGET_DRAWABLE (widget))
1552 GtkButton *button = GTK_BUTTON (widget);
1554 _gtk_button_paint (button, &event->area,
1555 GTK_WIDGET_STATE (widget),
1556 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1557 "button", "buttondefault");
1559 GTK_WIDGET_CLASS (gtk_button_parent_class)->expose_event (widget, event);
1566 gtk_button_button_press (GtkWidget *widget,
1567 GdkEventButton *event)
1571 if (event->type == GDK_BUTTON_PRESS)
1573 button = GTK_BUTTON (widget);
1575 if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget))
1576 gtk_widget_grab_focus (widget);
1578 if (event->button == 1)
1579 gtk_button_pressed (button);
1586 gtk_button_button_release (GtkWidget *widget,
1587 GdkEventButton *event)
1591 if (event->button == 1)
1593 button = GTK_BUTTON (widget);
1594 gtk_button_released (button);
1601 gtk_button_grab_broken (GtkWidget *widget,
1602 GdkEventGrabBroken *event)
1604 GtkButton *button = GTK_BUTTON (widget);
1607 /* Simulate a button release without the pointer in the button */
1608 if (button->button_down)
1610 save_in = button->in_button;
1611 button->in_button = FALSE;
1612 gtk_button_released (button);
1613 if (save_in != button->in_button)
1615 button->in_button = save_in;
1616 gtk_button_update_state (button);
1624 gtk_button_key_release (GtkWidget *widget,
1627 GtkButton *button = GTK_BUTTON (widget);
1629 if (button->activate_timeout)
1631 gtk_button_finish_activate (button, TRUE);
1634 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1635 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1641 gtk_button_enter_notify (GtkWidget *widget,
1642 GdkEventCrossing *event)
1645 GtkWidget *event_widget;
1647 button = GTK_BUTTON (widget);
1648 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1650 if ((event_widget == widget) &&
1651 (event->detail != GDK_NOTIFY_INFERIOR))
1653 button->in_button = TRUE;
1654 gtk_button_enter (button);
1661 gtk_button_leave_notify (GtkWidget *widget,
1662 GdkEventCrossing *event)
1665 GtkWidget *event_widget;
1667 button = GTK_BUTTON (widget);
1668 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1670 if ((event_widget == widget) &&
1671 (event->detail != GDK_NOTIFY_INFERIOR) &&
1672 (GTK_WIDGET_SENSITIVE (event_widget)))
1674 button->in_button = FALSE;
1675 gtk_button_leave (button);
1682 gtk_real_button_pressed (GtkButton *button)
1684 if (button->activate_timeout)
1687 button->button_down = TRUE;
1688 gtk_button_update_state (button);
1692 gtk_real_button_released (GtkButton *button)
1694 if (button->button_down)
1696 button->button_down = FALSE;
1698 if (button->activate_timeout)
1701 if (button->in_button)
1702 gtk_button_clicked (button);
1704 gtk_button_update_state (button);
1709 gtk_real_button_clicked (GtkButton *button)
1711 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1714 gtk_action_activate (priv->action);
1718 button_activate_timeout (gpointer data)
1720 gtk_button_finish_activate (data, TRUE);
1726 gtk_real_button_activate (GtkButton *button)
1728 GtkWidget *widget = GTK_WIDGET (button);
1729 GtkButtonPrivate *priv;
1732 priv = GTK_BUTTON_GET_PRIVATE (button);
1734 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1736 time = gtk_get_current_event_time ();
1737 if (gdk_keyboard_grab (button->event_window, TRUE, time) ==
1740 priv->has_grab = TRUE;
1741 priv->grab_time = time;
1744 gtk_grab_add (widget);
1746 button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1747 button_activate_timeout,
1749 button->button_down = TRUE;
1750 gtk_button_update_state (button);
1751 gtk_widget_queue_draw (GTK_WIDGET (button));
1756 gtk_button_finish_activate (GtkButton *button,
1759 GtkWidget *widget = GTK_WIDGET (button);
1760 GtkButtonPrivate *priv;
1762 priv = GTK_BUTTON_GET_PRIVATE (button);
1764 g_source_remove (button->activate_timeout);
1765 button->activate_timeout = 0;
1769 gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1772 gtk_grab_remove (widget);
1774 button->button_down = FALSE;
1776 gtk_button_update_state (button);
1777 gtk_widget_queue_draw (GTK_WIDGET (button));
1780 gtk_button_clicked (button);
1784 * gtk_button_set_label:
1785 * @button: a #GtkButton
1788 * Sets the text of the label of the button to @str. This text is
1789 * also used to select the stock item if gtk_button_set_use_stock()
1792 * This will also clear any previously set labels.
1795 gtk_button_set_label (GtkButton *button,
1800 g_return_if_fail (GTK_IS_BUTTON (button));
1802 new_label = g_strdup (label);
1803 g_free (button->label_text);
1804 button->label_text = new_label;
1806 gtk_button_construct_child (button);
1808 g_object_notify (G_OBJECT (button), "label");
1812 * gtk_button_get_label:
1813 * @button: a #GtkButton
1815 * Fetches the text from the label of the button, as set by
1816 * gtk_button_set_label(). If the label text has not
1817 * been set the return value will be %NULL. This will be the
1818 * case if you create an empty button with gtk_button_new() to
1819 * use as a container.
1821 * Return value: The text of the label widget. This string is owned
1822 * by the widget and must not be modified or freed.
1824 G_CONST_RETURN gchar *
1825 gtk_button_get_label (GtkButton *button)
1827 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1829 return button->label_text;
1833 * gtk_button_set_use_underline:
1834 * @button: a #GtkButton
1835 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1837 * If true, an underline in the text of the button label indicates
1838 * the next character should be used for the mnemonic accelerator key.
1841 gtk_button_set_use_underline (GtkButton *button,
1842 gboolean use_underline)
1844 g_return_if_fail (GTK_IS_BUTTON (button));
1846 use_underline = use_underline != FALSE;
1848 if (use_underline != button->use_underline)
1850 button->use_underline = use_underline;
1852 gtk_button_construct_child (button);
1854 g_object_notify (G_OBJECT (button), "use-underline");
1859 * gtk_button_get_use_underline:
1860 * @button: a #GtkButton
1862 * Returns whether an embedded underline in the button label indicates a
1863 * mnemonic. See gtk_button_set_use_underline ().
1865 * Return value: %TRUE if an embedded underline in the button label
1866 * indicates the mnemonic accelerator keys.
1869 gtk_button_get_use_underline (GtkButton *button)
1871 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1873 return button->use_underline;
1877 * gtk_button_set_use_stock:
1878 * @button: a #GtkButton
1879 * @use_stock: %TRUE if the button should use a stock item
1881 * If %TRUE, the label set on the button is used as a
1882 * stock id to select the stock item for the button.
1885 gtk_button_set_use_stock (GtkButton *button,
1888 g_return_if_fail (GTK_IS_BUTTON (button));
1890 use_stock = use_stock != FALSE;
1892 if (use_stock != button->use_stock)
1894 button->use_stock = use_stock;
1896 gtk_button_construct_child (button);
1898 g_object_notify (G_OBJECT (button), "use-stock");
1903 * gtk_button_get_use_stock:
1904 * @button: a #GtkButton
1906 * Returns whether the button label is a stock item.
1908 * Return value: %TRUE if the button label is used to
1909 * select a stock item instead of being
1910 * used directly as the label text.
1913 gtk_button_get_use_stock (GtkButton *button)
1915 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1917 return button->use_stock;
1921 * gtk_button_set_focus_on_click:
1922 * @button: a #GtkButton
1923 * @focus_on_click: whether the button grabs focus when clicked with the mouse
1925 * Sets whether the button will grab focus when it is clicked with the mouse.
1926 * Making mouse clicks not grab focus is useful in places like toolbars where
1927 * you don't want the keyboard focus removed from the main area of the
1933 gtk_button_set_focus_on_click (GtkButton *button,
1934 gboolean focus_on_click)
1936 g_return_if_fail (GTK_IS_BUTTON (button));
1938 focus_on_click = focus_on_click != FALSE;
1940 if (button->focus_on_click != focus_on_click)
1942 button->focus_on_click = focus_on_click;
1944 g_object_notify (G_OBJECT (button), "focus-on-click");
1949 * gtk_button_get_focus_on_click:
1950 * @button: a #GtkButton
1952 * Returns whether the button grabs focus when it is clicked with the mouse.
1953 * See gtk_button_set_focus_on_click().
1955 * Return value: %TRUE if the button grabs focus when it is clicked with
1961 gtk_button_get_focus_on_click (GtkButton *button)
1963 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1965 return button->focus_on_click;
1969 * gtk_button_set_alignment:
1970 * @button: a #GtkButton
1971 * @xalign: the horizontal position of the child, 0.0 is left aligned,
1972 * 1.0 is right aligned
1973 * @yalign: the vertical position of the child, 0.0 is top aligned,
1974 * 1.0 is bottom aligned
1976 * Sets the alignment of the child. This property has no effect unless
1977 * the child is a #GtkMisc or a #GtkAligment.
1982 gtk_button_set_alignment (GtkButton *button,
1986 GtkButtonPrivate *priv;
1988 g_return_if_fail (GTK_IS_BUTTON (button));
1990 priv = GTK_BUTTON_GET_PRIVATE (button);
1992 priv->xalign = xalign;
1993 priv->yalign = yalign;
1994 priv->align_set = 1;
1996 maybe_set_alignment (button, GTK_BIN (button)->child);
1998 g_object_freeze_notify (G_OBJECT (button));
1999 g_object_notify (G_OBJECT (button), "xalign");
2000 g_object_notify (G_OBJECT (button), "yalign");
2001 g_object_thaw_notify (G_OBJECT (button));
2005 * gtk_button_get_alignment:
2006 * @button: a #GtkButton
2007 * @xalign: return location for horizontal alignment
2008 * @yalign: return location for vertical alignment
2010 * Gets the alignment of the child in the button.
2015 gtk_button_get_alignment (GtkButton *button,
2019 GtkButtonPrivate *priv;
2021 g_return_if_fail (GTK_IS_BUTTON (button));
2023 priv = GTK_BUTTON_GET_PRIVATE (button);
2026 *xalign = priv->xalign;
2029 *yalign = priv->yalign;
2033 * _gtk_button_set_depressed:
2034 * @button: a #GtkButton
2035 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2037 * Sets whether the button is currently drawn as down or not. This is
2038 * purely a visual setting, and is meant only for use by derived widgets
2039 * such as #GtkToggleButton.
2042 _gtk_button_set_depressed (GtkButton *button,
2045 GtkWidget *widget = GTK_WIDGET (button);
2047 depressed = depressed != FALSE;
2049 if (depressed != button->depressed)
2051 button->depressed = depressed;
2052 gtk_widget_queue_resize (widget);
2057 gtk_button_update_state (GtkButton *button)
2060 GtkStateType new_state;
2062 if (button->activate_timeout)
2063 depressed = button->depress_on_activate;
2065 depressed = button->in_button && button->button_down;
2067 if (button->in_button && (!button->button_down || !depressed))
2068 new_state = GTK_STATE_PRELIGHT;
2070 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2072 _gtk_button_set_depressed (button, depressed);
2073 gtk_widget_set_state (GTK_WIDGET (button), new_state);
2077 show_image_change_notify (GtkButton *button)
2079 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2083 if (show_image (button))
2084 gtk_widget_show (priv->image);
2086 gtk_widget_hide (priv->image);
2091 traverse_container (GtkWidget *widget,
2094 if (GTK_IS_BUTTON (widget))
2095 show_image_change_notify (GTK_BUTTON (widget));
2096 else if (GTK_IS_CONTAINER (widget))
2097 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2101 gtk_button_setting_changed (GtkSettings *settings)
2105 list = gtk_window_list_toplevels ();
2107 for (l = list; l; l = l->next)
2108 gtk_container_forall (GTK_CONTAINER (l->data),
2109 traverse_container, NULL);
2116 gtk_button_screen_changed (GtkWidget *widget,
2117 GdkScreen *previous_screen)
2119 GtkSettings *settings;
2120 guint show_image_connection;
2122 if (!gtk_widget_has_screen (widget))
2125 settings = gtk_widget_get_settings (widget);
2127 show_image_connection =
2128 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
2129 "gtk-button-connection"));
2131 if (show_image_connection)
2134 show_image_connection =
2135 g_signal_connect (settings, "notify::gtk-button-images",
2136 G_CALLBACK (gtk_button_setting_changed), NULL);
2137 g_object_set_data (G_OBJECT (settings),
2138 I_("gtk-button-connection"),
2139 GUINT_TO_POINTER (show_image_connection));
2141 show_image_change_notify (GTK_BUTTON (widget));
2145 gtk_button_state_changed (GtkWidget *widget,
2146 GtkStateType previous_state)
2148 GtkButton *button = GTK_BUTTON (widget);
2150 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2152 button->in_button = FALSE;
2153 gtk_real_button_released (button);
2158 gtk_button_grab_notify (GtkWidget *widget,
2159 gboolean was_grabbed)
2161 GtkButton *button = GTK_BUTTON (widget);
2166 save_in = button->in_button;
2167 button->in_button = FALSE;
2168 gtk_real_button_released (button);
2169 if (save_in != button->in_button)
2171 button->in_button = save_in;
2172 gtk_button_update_state (button);
2178 * gtk_button_set_image:
2179 * @button: a #GtkButton
2180 * @image: a widget to set as the image for the button
2182 * Set the image of @button to the given widget. Note that
2183 * it depends on the #GtkSettings:gtk-button-images setting whether the
2184 * image will be displayed or not, you don't have to call
2185 * gtk_widget_show() on @image yourself.
2190 gtk_button_set_image (GtkButton *button,
2193 GtkButtonPrivate *priv;
2195 g_return_if_fail (GTK_IS_BUTTON (button));
2196 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2198 priv = GTK_BUTTON_GET_PRIVATE (button);
2200 if (priv->image && priv->image->parent)
2201 gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
2203 priv->image = image;
2204 priv->image_is_stock = (image == NULL);
2206 gtk_button_construct_child (button);
2208 g_object_notify (G_OBJECT (button), "image");
2212 * gtk_button_get_image:
2213 * @button: a #GtkButton
2215 * Gets the widget that is currenty set as the image of @button.
2216 * This may have been explicitly set by gtk_button_set_image()
2217 * or constructed by gtk_button_new_from_stock().
2219 * Return value: a #GtkWidget or %NULL in case there is no image
2224 gtk_button_get_image (GtkButton *button)
2226 GtkButtonPrivate *priv;
2228 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2230 priv = GTK_BUTTON_GET_PRIVATE (button);
2236 * gtk_button_set_image_position:
2237 * @button: a #GtkButton
2238 * @position: the position
2240 * Sets the position of the image relative to the text
2241 * inside the button.
2246 gtk_button_set_image_position (GtkButton *button,
2247 GtkPositionType position)
2250 GtkButtonPrivate *priv;
2252 g_return_if_fail (GTK_IS_BUTTON (button));
2253 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2255 priv = GTK_BUTTON_GET_PRIVATE (button);
2257 if (priv->image_position != position)
2259 priv->image_position = position;
2261 gtk_button_construct_child (button);
2263 g_object_notify (G_OBJECT (button), "image-position");
2268 * gtk_button_get_image_position:
2269 * @button: a #GtkButton
2271 * Gets the position of the image relative to the text
2272 * inside the button.
2274 * Return value: the position
2279 gtk_button_get_image_position (GtkButton *button)
2281 GtkButtonPrivate *priv;
2283 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2285 priv = GTK_BUTTON_GET_PRIVATE (button);
2287 return priv->image_position;
2291 #define __GTK_BUTTON_C__
2292 #include "gtkaliasdef.c"