1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2001. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 * @Short_description: A widget that creates a signal when clicked on
32 * The #GtkButton widget is generally used to attach a function to that is
33 * called when the button is pressed. The various signals and how to use them
36 * The #GtkButton widget can hold any valid child widget. That is it can hold
37 * most any other standard #GtkWidget. The most commonly used child is the
43 #include "gtkbutton.h"
44 #include "gtkbuttonprivate.h"
47 #include "gtkalignment.h"
50 #include "gtkmarshalers.h"
55 #include "gtkiconfactory.h"
56 #include "gtkactivatable.h"
57 #include "gtksizerequest.h"
58 #include "gtktypebuiltins.h"
59 #include "gtkprivate.h"
63 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
64 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
65 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
67 /* Time out before giving up on getting a key release when animating
70 #define ACTIVATE_TIMEOUT 250
95 /* activatable properties */
96 PROP_ACTIVATABLE_RELATED_ACTION,
97 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
101 static void gtk_button_destroy (GtkWidget *widget);
102 static void gtk_button_dispose (GObject *object);
103 static void gtk_button_set_property (GObject *object,
107 static void gtk_button_get_property (GObject *object,
111 static void gtk_button_screen_changed (GtkWidget *widget,
112 GdkScreen *previous_screen);
113 static void gtk_button_realize (GtkWidget * widget);
114 static void gtk_button_unrealize (GtkWidget * widget);
115 static void gtk_button_map (GtkWidget * widget);
116 static void gtk_button_unmap (GtkWidget * widget);
117 static void gtk_button_style_updated (GtkWidget * widget);
118 static void gtk_button_size_allocate (GtkWidget * widget,
119 GtkAllocation * allocation);
120 static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
121 static gint gtk_button_button_press (GtkWidget * widget,
122 GdkEventButton * event);
123 static gint gtk_button_button_release (GtkWidget * widget,
124 GdkEventButton * event);
125 static gint gtk_button_grab_broken (GtkWidget * widget,
126 GdkEventGrabBroken * event);
127 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
128 static gint gtk_button_enter_notify (GtkWidget * widget,
129 GdkEventCrossing * event);
130 static gint gtk_button_leave_notify (GtkWidget * widget,
131 GdkEventCrossing * event);
132 static void gtk_real_button_pressed (GtkButton * button);
133 static void gtk_real_button_released (GtkButton * button);
134 static void gtk_real_button_clicked (GtkButton * button);
135 static void gtk_real_button_activate (GtkButton *button);
136 static void gtk_button_update_state (GtkButton *button);
137 static void gtk_button_add (GtkContainer *container,
139 static GType gtk_button_child_type (GtkContainer *container);
140 static void gtk_button_finish_activate (GtkButton *button,
143 static GObject* gtk_button_constructor (GType type,
144 guint n_construct_properties,
145 GObjectConstructParam *construct_params);
146 static void gtk_button_construct_child (GtkButton *button);
147 static void gtk_button_state_changed (GtkWidget *widget,
148 GtkStateType previous_state);
149 static void gtk_button_grab_notify (GtkWidget *widget,
150 gboolean was_grabbed);
153 static void gtk_button_activatable_interface_init(GtkActivatableIface *iface);
154 static void gtk_button_update (GtkActivatable *activatable,
156 const gchar *property_name);
157 static void gtk_button_sync_action_properties (GtkActivatable *activatable,
159 static void gtk_button_set_related_action (GtkButton *button,
161 static void gtk_button_set_use_action_appearance (GtkButton *button,
162 gboolean use_appearance);
164 static void gtk_button_get_preferred_width (GtkWidget *widget,
167 static void gtk_button_get_preferred_height (GtkWidget *widget,
171 static guint button_signals[LAST_SIGNAL] = { 0 };
173 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
174 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
175 gtk_button_activatable_interface_init))
178 gtk_button_class_init (GtkButtonClass *klass)
180 GObjectClass *gobject_class;
181 GtkWidgetClass *widget_class;
182 GtkContainerClass *container_class;
184 gobject_class = G_OBJECT_CLASS (klass);
185 widget_class = (GtkWidgetClass*) klass;
186 container_class = (GtkContainerClass*) klass;
188 gobject_class->constructor = gtk_button_constructor;
189 gobject_class->dispose = gtk_button_dispose;
190 gobject_class->set_property = gtk_button_set_property;
191 gobject_class->get_property = gtk_button_get_property;
193 widget_class->get_preferred_width = gtk_button_get_preferred_width;
194 widget_class->get_preferred_height = gtk_button_get_preferred_height;
195 widget_class->destroy = gtk_button_destroy;
196 widget_class->screen_changed = gtk_button_screen_changed;
197 widget_class->realize = gtk_button_realize;
198 widget_class->unrealize = gtk_button_unrealize;
199 widget_class->map = gtk_button_map;
200 widget_class->unmap = gtk_button_unmap;
201 widget_class->style_updated = gtk_button_style_updated;
202 widget_class->size_allocate = gtk_button_size_allocate;
203 widget_class->draw = gtk_button_draw;
204 widget_class->button_press_event = gtk_button_button_press;
205 widget_class->button_release_event = gtk_button_button_release;
206 widget_class->grab_broken_event = gtk_button_grab_broken;
207 widget_class->key_release_event = gtk_button_key_release;
208 widget_class->enter_notify_event = gtk_button_enter_notify;
209 widget_class->leave_notify_event = gtk_button_leave_notify;
210 widget_class->state_changed = gtk_button_state_changed;
211 widget_class->grab_notify = gtk_button_grab_notify;
213 container_class->child_type = gtk_button_child_type;
214 container_class->add = gtk_button_add;
215 gtk_container_class_handle_border_width (container_class);
217 klass->pressed = gtk_real_button_pressed;
218 klass->released = gtk_real_button_released;
219 klass->clicked = NULL;
220 klass->enter = gtk_button_update_state;
221 klass->leave = gtk_button_update_state;
222 klass->activate = gtk_real_button_activate;
224 g_object_class_install_property (gobject_class,
226 g_param_spec_string ("label",
228 P_("Text of the label widget inside the button, if the button contains a label widget"),
230 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
232 g_object_class_install_property (gobject_class,
234 g_param_spec_boolean ("use-underline",
236 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
238 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
240 g_object_class_install_property (gobject_class,
242 g_param_spec_boolean ("use-stock",
244 P_("If set, the label is used to pick a stock item instead of being displayed"),
246 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
248 g_object_class_install_property (gobject_class,
250 g_param_spec_boolean ("focus-on-click",
251 P_("Focus on click"),
252 P_("Whether the button grabs focus when it is clicked with the mouse"),
254 GTK_PARAM_READWRITE));
256 g_object_class_install_property (gobject_class,
258 g_param_spec_enum ("relief",
260 P_("The border relief style"),
261 GTK_TYPE_RELIEF_STYLE,
263 GTK_PARAM_READWRITE));
268 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
269 * can be used to control it's horizontal alignment. 0.0 is left aligned,
270 * 1.0 is right aligned.
274 g_object_class_install_property (gobject_class,
276 g_param_spec_float("xalign",
277 P_("Horizontal alignment for child"),
278 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
282 GTK_PARAM_READWRITE));
287 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
288 * can be used to control it's vertical alignment. 0.0 is top aligned,
289 * 1.0 is bottom aligned.
293 g_object_class_install_property (gobject_class,
295 g_param_spec_float("yalign",
296 P_("Vertical alignment for child"),
297 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
301 GTK_PARAM_READWRITE));
306 * The child widget to appear next to the button text.
310 g_object_class_install_property (gobject_class,
312 g_param_spec_object ("image",
314 P_("Child widget to appear next to the button text"),
316 GTK_PARAM_READWRITE));
319 * GtkButton:image-position:
321 * The position of the image relative to the text inside the button.
325 g_object_class_install_property (gobject_class,
327 g_param_spec_enum ("image-position",
328 P_("Image position"),
329 P_("The position of the image relative to the text"),
330 GTK_TYPE_POSITION_TYPE,
332 GTK_PARAM_READWRITE));
334 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
335 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
338 * GtkButton::pressed:
339 * @button: the object that received the signal
341 * Emitted when the button is pressed.
343 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
345 button_signals[PRESSED] =
346 g_signal_new (I_("pressed"),
347 G_OBJECT_CLASS_TYPE (gobject_class),
349 G_STRUCT_OFFSET (GtkButtonClass, pressed),
351 _gtk_marshal_VOID__VOID,
355 * GtkButton::released:
356 * @button: the object that received the signal
358 * Emitted when the button is released.
360 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
362 button_signals[RELEASED] =
363 g_signal_new (I_("released"),
364 G_OBJECT_CLASS_TYPE (gobject_class),
366 G_STRUCT_OFFSET (GtkButtonClass, released),
368 _gtk_marshal_VOID__VOID,
372 * GtkButton::clicked:
373 * @button: the object that received the signal
375 * Emitted when the button has been activated (pressed and released).
377 button_signals[CLICKED] =
378 g_signal_new (I_("clicked"),
379 G_OBJECT_CLASS_TYPE (gobject_class),
380 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
381 G_STRUCT_OFFSET (GtkButtonClass, clicked),
383 _gtk_marshal_VOID__VOID,
388 * @button: the object that received the signal
390 * Emitted when the pointer enters the button.
392 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
394 button_signals[ENTER] =
395 g_signal_new (I_("enter"),
396 G_OBJECT_CLASS_TYPE (gobject_class),
398 G_STRUCT_OFFSET (GtkButtonClass, enter),
400 _gtk_marshal_VOID__VOID,
405 * @button: the object that received the signal
407 * Emitted when the pointer leaves the button.
409 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
411 button_signals[LEAVE] =
412 g_signal_new (I_("leave"),
413 G_OBJECT_CLASS_TYPE (gobject_class),
415 G_STRUCT_OFFSET (GtkButtonClass, leave),
417 _gtk_marshal_VOID__VOID,
421 * GtkButton::activate:
422 * @widget: the object which received the signal.
424 * The ::activate signal on GtkButton is an action signal and
425 * emitting it causes the button to animate press then release.
426 * Applications should never connect to this signal, but use the
427 * #GtkButton::clicked signal.
429 button_signals[ACTIVATE] =
430 g_signal_new (I_("activate"),
431 G_OBJECT_CLASS_TYPE (gobject_class),
432 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
433 G_STRUCT_OFFSET (GtkButtonClass, activate),
435 _gtk_marshal_VOID__VOID,
437 widget_class->activate_signal = button_signals[ACTIVATE];
440 * GtkButton:default-border:
442 * The "default-border" style property defines the extra space to add
443 * around a button that can become the default widget of its window.
444 * For more information about default widgets, see gtk_widget_grab_default().
447 gtk_widget_class_install_style_property (widget_class,
448 g_param_spec_boxed ("default-border",
449 P_("Default Spacing"),
450 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
452 GTK_PARAM_READABLE));
455 * GtkButton:default-outside-border:
457 * The "default-outside-border" style property defines the extra outside
458 * space to add around a button that can become the default widget of its
459 * window. Extra outside space is always drawn outside the button border.
460 * For more information about default widgets, see gtk_widget_grab_default().
462 gtk_widget_class_install_style_property (widget_class,
463 g_param_spec_boxed ("default-outside-border",
464 P_("Default Outside Spacing"),
465 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
467 GTK_PARAM_READABLE));
468 gtk_widget_class_install_style_property (widget_class,
469 g_param_spec_int ("child-displacement-x",
470 P_("Child X Displacement"),
471 P_("How far in the x direction to move the child when the button is depressed"),
475 GTK_PARAM_READABLE));
476 gtk_widget_class_install_style_property (widget_class,
477 g_param_spec_int ("child-displacement-y",
478 P_("Child Y Displacement"),
479 P_("How far in the y direction to move the child when the button is depressed"),
483 GTK_PARAM_READABLE));
486 * GtkButton:displace-focus:
488 * Whether the child_displacement_x/child_displacement_y properties
489 * should also affect the focus rectangle.
493 gtk_widget_class_install_style_property (widget_class,
494 g_param_spec_boolean ("displace-focus",
495 P_("Displace focus"),
496 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
498 GTK_PARAM_READABLE));
501 * GtkButton:inner-border:
503 * Sets the border between the button edges and child.
507 gtk_widget_class_install_style_property (widget_class,
508 g_param_spec_boxed ("inner-border",
510 P_("Border between button edges and child."),
512 GTK_PARAM_READABLE));
515 * GtkButton::image-spacing:
517 * Spacing in pixels between the image and label.
521 gtk_widget_class_install_style_property (widget_class,
522 g_param_spec_int ("image-spacing",
524 P_("Spacing in pixels between the image and label"),
528 GTK_PARAM_READABLE));
530 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
534 gtk_button_init (GtkButton *button)
536 GtkButtonPrivate *priv;
537 GtkStyleContext *context;
539 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
544 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
545 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
546 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
548 priv->label_text = NULL;
550 priv->constructed = FALSE;
551 priv->in_button = FALSE;
552 priv->button_down = FALSE;
553 priv->relief = GTK_RELIEF_NORMAL;
554 priv->use_stock = FALSE;
555 priv->use_underline = FALSE;
556 priv->depressed = FALSE;
557 priv->depress_on_activate = TRUE;
558 priv->focus_on_click = TRUE;
563 priv->image_is_stock = TRUE;
564 priv->image_position = GTK_POS_LEFT;
565 priv->use_action_appearance = TRUE;
567 context = gtk_widget_get_style_context (GTK_WIDGET (button));
568 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
572 gtk_button_destroy (GtkWidget *widget)
574 GtkButton *button = GTK_BUTTON (widget);
575 GtkButtonPrivate *priv = button->priv;
577 if (priv->label_text)
579 g_free (priv->label_text);
580 priv->label_text = NULL;
583 GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
587 gtk_button_constructor (GType type,
588 guint n_construct_properties,
589 GObjectConstructParam *construct_params)
593 GtkButtonPrivate *priv;
595 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
596 n_construct_properties,
599 button = GTK_BUTTON (object);
602 priv->constructed = TRUE;
604 if (priv->label_text != NULL)
605 gtk_button_construct_child (button);
612 gtk_button_child_type (GtkContainer *container)
614 if (!gtk_bin_get_child (GTK_BIN (container)))
615 return GTK_TYPE_WIDGET;
621 maybe_set_alignment (GtkButton *button,
624 GtkButtonPrivate *priv = button->priv;
626 if (GTK_IS_MISC (widget))
628 GtkMisc *misc = GTK_MISC (widget);
631 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
633 else if (GTK_IS_ALIGNMENT (widget))
635 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
636 gfloat xscale, yscale;
638 g_object_get (alignment,
644 gtk_alignment_set (alignment,
645 priv->xalign, priv->yalign,
651 gtk_button_add (GtkContainer *container,
654 maybe_set_alignment (GTK_BUTTON (container), widget);
656 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
660 gtk_button_dispose (GObject *object)
662 GtkButton *button = GTK_BUTTON (object);
663 GtkButtonPrivate *priv = button->priv;
667 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
670 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
674 gtk_button_set_property (GObject *object,
679 GtkButton *button = GTK_BUTTON (object);
680 GtkButtonPrivate *priv = button->priv;
685 gtk_button_set_label (button, g_value_get_string (value));
688 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
691 gtk_button_set_relief (button, g_value_get_enum (value));
693 case PROP_USE_UNDERLINE:
694 gtk_button_set_use_underline (button, g_value_get_boolean (value));
697 gtk_button_set_use_stock (button, g_value_get_boolean (value));
699 case PROP_FOCUS_ON_CLICK:
700 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
703 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
706 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
708 case PROP_IMAGE_POSITION:
709 gtk_button_set_image_position (button, g_value_get_enum (value));
711 case PROP_ACTIVATABLE_RELATED_ACTION:
712 gtk_button_set_related_action (button, g_value_get_object (value));
714 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
715 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
718 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
724 gtk_button_get_property (GObject *object,
729 GtkButton *button = GTK_BUTTON (object);
730 GtkButtonPrivate *priv = button->priv;
735 g_value_set_string (value, priv->label_text);
738 g_value_set_object (value, (GObject *)priv->image);
741 g_value_set_enum (value, priv->relief);
743 case PROP_USE_UNDERLINE:
744 g_value_set_boolean (value, priv->use_underline);
747 g_value_set_boolean (value, priv->use_stock);
749 case PROP_FOCUS_ON_CLICK:
750 g_value_set_boolean (value, priv->focus_on_click);
753 g_value_set_float (value, priv->xalign);
756 g_value_set_float (value, priv->yalign);
758 case PROP_IMAGE_POSITION:
759 g_value_set_enum (value, priv->image_position);
761 case PROP_ACTIVATABLE_RELATED_ACTION:
762 g_value_set_object (value, priv->action);
764 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
765 g_value_set_boolean (value, priv->use_action_appearance);
768 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
774 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
776 iface->update = gtk_button_update;
777 iface->sync_action_properties = gtk_button_sync_action_properties;
781 activatable_update_stock_id (GtkButton *button,
784 if (!gtk_button_get_use_stock (button))
787 gtk_button_set_label (button, gtk_action_get_stock_id (action));
791 activatable_update_short_label (GtkButton *button,
797 if (gtk_button_get_use_stock (button))
800 image = gtk_button_get_image (button);
802 /* Dont touch custom child... */
803 child = gtk_bin_get_child (GTK_BIN (button));
804 if (GTK_IS_IMAGE (image) ||
806 GTK_IS_LABEL (child))
808 gtk_button_set_label (button, gtk_action_get_short_label (action));
809 gtk_button_set_use_underline (button, TRUE);
814 activatable_update_icon_name (GtkButton *button,
819 if (gtk_button_get_use_stock (button))
822 image = gtk_button_get_image (button);
824 if (GTK_IS_IMAGE (image) &&
825 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
826 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
827 gtk_image_set_from_icon_name (GTK_IMAGE (image),
828 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
832 activatable_update_gicon (GtkButton *button,
835 GtkWidget *image = gtk_button_get_image (button);
836 GIcon *icon = gtk_action_get_gicon (action);
838 if (GTK_IS_IMAGE (image) &&
839 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
840 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
841 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
845 gtk_button_update (GtkActivatable *activatable,
847 const gchar *property_name)
849 GtkButton *button = GTK_BUTTON (activatable);
850 GtkButtonPrivate *priv = button->priv;
852 if (strcmp (property_name, "visible") == 0)
854 if (gtk_action_is_visible (action))
855 gtk_widget_show (GTK_WIDGET (activatable));
857 gtk_widget_hide (GTK_WIDGET (activatable));
859 else if (strcmp (property_name, "sensitive") == 0)
860 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
862 if (!priv->use_action_appearance)
865 if (strcmp (property_name, "stock-id") == 0)
866 activatable_update_stock_id (GTK_BUTTON (activatable), action);
867 else if (strcmp (property_name, "gicon") == 0)
868 activatable_update_gicon (GTK_BUTTON (activatable), action);
869 else if (strcmp (property_name, "short-label") == 0)
870 activatable_update_short_label (GTK_BUTTON (activatable), action);
871 else if (strcmp (property_name, "icon-name") == 0)
872 activatable_update_icon_name (GTK_BUTTON (activatable), action);
876 gtk_button_sync_action_properties (GtkActivatable *activatable,
879 GtkButton *button = GTK_BUTTON (activatable);
880 GtkButtonPrivate *priv = button->priv;
885 if (gtk_action_is_visible (action))
886 gtk_widget_show (GTK_WIDGET (activatable));
888 gtk_widget_hide (GTK_WIDGET (activatable));
890 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
892 if (priv->use_action_appearance)
894 activatable_update_stock_id (GTK_BUTTON (activatable), action);
895 activatable_update_short_label (GTK_BUTTON (activatable), action);
896 activatable_update_gicon (GTK_BUTTON (activatable), action);
897 activatable_update_icon_name (GTK_BUTTON (activatable), action);
902 gtk_button_set_related_action (GtkButton *button,
905 GtkButtonPrivate *priv = button->priv;
907 if (priv->action == action)
910 /* This should be a default handler, but for compatibility reasons
911 * we need to support derived classes that don't chain up their
914 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
916 g_signal_connect_after (button, "clicked",
917 G_CALLBACK (gtk_real_button_clicked), NULL);
919 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
921 priv->action = action;
925 gtk_button_set_use_action_appearance (GtkButton *button,
926 gboolean use_appearance)
928 GtkButtonPrivate *priv = button->priv;
930 if (priv->use_action_appearance != use_appearance)
932 priv->use_action_appearance = use_appearance;
934 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
941 * Creates a new #GtkButton widget. To add a child widget to the button,
942 * use gtk_container_add().
944 * Returns: The newly created #GtkButton widget.
947 gtk_button_new (void)
949 return g_object_new (GTK_TYPE_BUTTON, NULL);
953 show_image (GtkButton *button)
955 GtkButtonPrivate *priv = button->priv;
958 if (priv->label_text)
960 GtkSettings *settings;
962 settings = gtk_widget_get_settings (GTK_WIDGET (button));
963 g_object_get (settings, "gtk-button-images", &show, NULL);
972 gtk_button_construct_child (GtkButton *button)
974 GtkButtonPrivate *priv = button->priv;
975 GtkStyleContext *context;
981 GtkWidget *image = NULL;
982 gchar *label_text = NULL;
985 if (!priv->constructed)
988 if (!priv->label_text && !priv->image)
991 context = gtk_widget_get_style_context (GTK_WIDGET (button));
993 gtk_style_context_get_style (context,
994 "image-spacing", &image_spacing,
997 if (priv->image && !priv->image_is_stock)
1001 image = g_object_ref (priv->image);
1003 parent = gtk_widget_get_parent (image);
1005 gtk_container_remove (GTK_CONTAINER (parent), image);
1010 child = gtk_bin_get_child (GTK_BIN (button));
1012 gtk_container_remove (GTK_CONTAINER (button), child);
1014 if (priv->use_stock &&
1016 gtk_stock_lookup (priv->label_text, &item))
1019 image = g_object_ref (gtk_image_new_from_stock (priv->label_text, GTK_ICON_SIZE_BUTTON));
1021 label_text = item.label;
1024 label_text = priv->label_text;
1028 priv->image = image;
1029 g_object_set (priv->image,
1030 "visible", show_image (button),
1031 "no-show-all", TRUE,
1034 if (priv->image_position == GTK_POS_LEFT ||
1035 priv->image_position == GTK_POS_RIGHT)
1036 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1038 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1040 if (priv->align_set)
1041 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1043 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1045 if (priv->image_position == GTK_POS_LEFT ||
1046 priv->image_position == GTK_POS_TOP)
1047 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1049 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1053 if (priv->use_underline || priv->use_stock)
1055 label = gtk_label_new_with_mnemonic (label_text);
1056 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1057 GTK_WIDGET (button));
1060 label = gtk_label_new (label_text);
1062 if (priv->image_position == GTK_POS_RIGHT ||
1063 priv->image_position == GTK_POS_BOTTOM)
1064 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1066 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1069 gtk_container_add (GTK_CONTAINER (button), align);
1070 gtk_container_add (GTK_CONTAINER (align), box);
1071 gtk_widget_show_all (align);
1073 g_object_unref (image);
1078 if (priv->use_underline || priv->use_stock)
1080 label = gtk_label_new_with_mnemonic (priv->label_text);
1081 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1084 label = gtk_label_new (priv->label_text);
1086 if (priv->align_set)
1087 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1089 gtk_widget_show (label);
1090 gtk_container_add (GTK_CONTAINER (button), label);
1095 * gtk_button_new_with_label:
1096 * @label: The text you want the #GtkLabel to hold.
1098 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1101 * Returns: The newly created #GtkButton widget.
1104 gtk_button_new_with_label (const gchar *label)
1106 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1110 * gtk_button_new_from_stock:
1111 * @stock_id: the name of the stock item
1113 * Creates a new #GtkButton containing the image and text from a stock item.
1114 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1117 * If @stock_id is unknown, then it will be treated as a mnemonic
1118 * label (as for gtk_button_new_with_mnemonic()).
1120 * Returns: a new #GtkButton
1123 gtk_button_new_from_stock (const gchar *stock_id)
1125 return g_object_new (GTK_TYPE_BUTTON,
1128 "use-underline", TRUE,
1133 * gtk_button_new_with_mnemonic:
1134 * @label: The text of the button, with an underscore in front of the
1135 * mnemonic character
1136 * @returns: a new #GtkButton
1138 * Creates a new #GtkButton containing a label.
1139 * If characters in @label are preceded by an underscore, they are underlined.
1140 * If you need a literal underscore character in a label, use '__' (two
1141 * underscores). The first underlined character represents a keyboard
1142 * accelerator called a mnemonic.
1143 * Pressing Alt and that key activates the button.
1146 gtk_button_new_with_mnemonic (const gchar *label)
1148 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1152 * gtk_button_pressed:
1153 * @button: The #GtkButton you want to send the signal to.
1155 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1157 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1160 gtk_button_pressed (GtkButton *button)
1162 g_return_if_fail (GTK_IS_BUTTON (button));
1164 g_signal_emit (button, button_signals[PRESSED], 0);
1168 * gtk_button_released:
1169 * @button: The #GtkButton you want to send the signal to.
1171 * Emits a #GtkButton::released signal to the given #GtkButton.
1173 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1176 gtk_button_released (GtkButton *button)
1178 g_return_if_fail (GTK_IS_BUTTON (button));
1180 g_signal_emit (button, button_signals[RELEASED], 0);
1184 * gtk_button_clicked:
1185 * @button: The #GtkButton you want to send the signal to.
1187 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1190 gtk_button_clicked (GtkButton *button)
1192 g_return_if_fail (GTK_IS_BUTTON (button));
1194 g_signal_emit (button, button_signals[CLICKED], 0);
1199 * @button: The #GtkButton you want to send the signal to.
1201 * Emits a #GtkButton::enter signal to the given #GtkButton.
1203 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1206 gtk_button_enter (GtkButton *button)
1208 g_return_if_fail (GTK_IS_BUTTON (button));
1210 g_signal_emit (button, button_signals[ENTER], 0);
1215 * @button: The #GtkButton you want to send the signal to.
1217 * Emits a #GtkButton::leave signal to the given #GtkButton.
1219 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1222 gtk_button_leave (GtkButton *button)
1224 g_return_if_fail (GTK_IS_BUTTON (button));
1226 g_signal_emit (button, button_signals[LEAVE], 0);
1230 * gtk_button_set_relief:
1231 * @button: The #GtkButton you want to set relief styles of.
1232 * @newstyle: The GtkReliefStyle as described above.
1234 * Sets the relief style of the edges of the given #GtkButton widget.
1235 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1236 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1238 * <!-- FIXME: put pictures of each style -->
1241 gtk_button_set_relief (GtkButton *button,
1242 GtkReliefStyle newrelief)
1244 GtkButtonPrivate *priv;
1246 g_return_if_fail (GTK_IS_BUTTON (button));
1248 priv = button->priv;
1250 if (newrelief != priv->relief)
1252 priv->relief = newrelief;
1253 g_object_notify (G_OBJECT (button), "relief");
1254 gtk_widget_queue_draw (GTK_WIDGET (button));
1259 * gtk_button_get_relief:
1260 * @button: The #GtkButton you want the #GtkReliefStyle from.
1262 * Returns the current relief style of the given #GtkButton.
1264 * Returns: The current #GtkReliefStyle
1267 gtk_button_get_relief (GtkButton *button)
1269 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1271 return button->priv->relief;
1275 gtk_button_realize (GtkWidget *widget)
1277 GtkButton *button = GTK_BUTTON (widget);
1278 GtkButtonPrivate *priv = button->priv;
1279 GtkAllocation allocation;
1281 GdkWindowAttr attributes;
1282 gint attributes_mask;
1284 gtk_widget_get_allocation (widget, &allocation);
1286 gtk_widget_set_realized (widget, TRUE);
1288 attributes.window_type = GDK_WINDOW_CHILD;
1289 attributes.x = allocation.x;
1290 attributes.y = allocation.y;
1291 attributes.width = allocation.width;
1292 attributes.height = allocation.height;
1293 attributes.wclass = GDK_INPUT_ONLY;
1294 attributes.event_mask = gtk_widget_get_events (widget);
1295 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1296 GDK_BUTTON_RELEASE_MASK |
1297 GDK_ENTER_NOTIFY_MASK |
1298 GDK_LEAVE_NOTIFY_MASK);
1300 attributes_mask = GDK_WA_X | GDK_WA_Y;
1302 window = gtk_widget_get_parent_window (widget);
1303 gtk_widget_set_window (widget, window);
1304 g_object_ref (window);
1306 priv->event_window = gdk_window_new (window,
1307 &attributes, attributes_mask);
1308 gdk_window_set_user_data (priv->event_window, button);
1312 gtk_button_unrealize (GtkWidget *widget)
1314 GtkButton *button = GTK_BUTTON (widget);
1315 GtkButtonPrivate *priv = button->priv;
1317 if (priv->activate_timeout)
1318 gtk_button_finish_activate (button, FALSE);
1320 if (priv->event_window)
1322 gdk_window_set_user_data (priv->event_window, NULL);
1323 gdk_window_destroy (priv->event_window);
1324 priv->event_window = NULL;
1327 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1331 gtk_button_map (GtkWidget *widget)
1333 GtkButton *button = GTK_BUTTON (widget);
1334 GtkButtonPrivate *priv = button->priv;
1336 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1338 if (priv->event_window)
1339 gdk_window_show (priv->event_window);
1343 gtk_button_unmap (GtkWidget *widget)
1345 GtkButton *button = GTK_BUTTON (widget);
1346 GtkButtonPrivate *priv = button->priv;
1348 if (priv->event_window)
1349 gdk_window_hide (priv->event_window);
1351 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1355 gtk_button_update_image_spacing (GtkButton *button,
1356 GtkStyleContext *context)
1358 GtkButtonPrivate *priv = button->priv;
1362 /* Keep in sync with gtk_button_construct_child,
1363 * we only want to update the spacing if the box
1364 * was constructed there.
1366 if (!priv->constructed || !priv->image)
1369 child = gtk_bin_get_child (GTK_BIN (button));
1370 if (GTK_IS_ALIGNMENT (child))
1372 child = gtk_bin_get_child (GTK_BIN (child));
1373 if (GTK_IS_BOX (child))
1375 gtk_style_context_get_style (context,
1376 "image-spacing", &spacing,
1379 gtk_box_set_spacing (GTK_BOX (child), spacing);
1385 gtk_button_style_updated (GtkWidget *widget)
1387 GtkStyleContext *context;
1389 context = gtk_widget_get_style_context (widget);
1391 gtk_button_update_image_spacing (GTK_BUTTON (widget), context);
1395 gtk_button_get_props (GtkButton *button,
1396 GtkBorder *default_border,
1397 GtkBorder *default_outside_border,
1398 GtkBorder *inner_border,
1399 gboolean *interior_focus)
1401 GtkStyleContext *context;
1402 GtkBorder *tmp_border;
1404 context = gtk_widget_get_style_context (GTK_WIDGET (button));
1408 gtk_style_context_get_style (context,
1409 "default-border", &tmp_border,
1414 *default_border = *tmp_border;
1415 gtk_border_free (tmp_border);
1418 *default_border = default_default_border;
1421 if (default_outside_border)
1423 gtk_style_context_get_style (context,
1424 "default-outside-border", &tmp_border,
1429 *default_outside_border = *tmp_border;
1430 gtk_border_free (tmp_border);
1433 *default_outside_border = default_default_outside_border;
1438 gtk_style_context_get_style (context,
1439 "inner-border", &tmp_border,
1444 *inner_border = *tmp_border;
1445 gtk_border_free (tmp_border);
1448 *inner_border = default_inner_border;
1453 gtk_style_context_get_style (context,
1454 "interior-focus", interior_focus,
1460 gtk_button_size_allocate (GtkWidget *widget,
1461 GtkAllocation *allocation)
1463 GtkButton *button = GTK_BUTTON (widget);
1464 GtkButtonPrivate *priv = button->priv;
1465 GtkAllocation child_allocation;
1466 GtkStyleContext *context;
1467 GtkStateFlags state;
1469 GtkBorder default_border;
1470 GtkBorder inner_border;
1475 context = gtk_widget_get_style_context (widget);
1476 state = gtk_widget_get_state_flags (widget);
1478 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1479 gtk_style_context_get_style (context,
1480 "focus-line-width", &focus_width,
1481 "focus-padding", &focus_pad,
1484 gtk_style_context_get (context, state,
1485 "border-width", &border,
1488 gtk_widget_set_allocation (widget, allocation);
1490 if (gtk_widget_get_realized (widget))
1491 gdk_window_move_resize (priv->event_window,
1495 allocation->height);
1497 child = gtk_bin_get_child (GTK_BIN (button));
1498 if (child && gtk_widget_get_visible (child))
1500 child_allocation.x = allocation->x + inner_border.left + border->left;
1501 child_allocation.y = allocation->y + inner_border.top + border->top;
1503 child_allocation.width =
1505 (border->left + border->right) -
1509 child_allocation.height =
1510 allocation->height -
1511 (border->top + border->bottom) -
1513 inner_border.bottom;
1515 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1517 child_allocation.x += default_border.left;
1518 child_allocation.y += default_border.top;
1519 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1520 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1523 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1525 child_allocation.x += focus_width + focus_pad;
1526 child_allocation.y += focus_width + focus_pad;
1527 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1528 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1531 if (priv->depressed)
1533 gint child_displacement_x;
1534 gint child_displacement_y;
1536 gtk_style_context_get_style (context,
1537 "child-displacement-x", &child_displacement_x,
1538 "child-displacement-y", &child_displacement_y,
1540 child_allocation.x += child_displacement_x;
1541 child_allocation.y += child_displacement_y;
1544 child_allocation.width = MAX (1, child_allocation.width);
1545 child_allocation.height = MAX (1, child_allocation.height);
1547 gtk_widget_size_allocate (child, &child_allocation);
1550 gtk_border_free (border);
1554 _gtk_button_paint (GtkButton *button,
1558 GtkStateType state_type,
1559 GtkShadowType shadow_type,
1560 const gchar *main_detail,
1561 const gchar *default_detail)
1563 GtkButtonPrivate *priv = button->priv;
1566 GtkBorder default_border;
1567 GtkBorder default_outside_border;
1568 gboolean interior_focus;
1571 GtkAllocation allocation;
1573 GtkStyleContext *context;
1574 GtkStateFlags state;
1576 widget = GTK_WIDGET (button);
1578 context = gtk_widget_get_style_context (widget);
1579 state = gtk_widget_get_state_flags (widget);
1581 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1582 gtk_style_context_get_style (context,
1583 "focus-line-width", &focus_width,
1584 "focus-padding", &focus_pad,
1587 gtk_widget_get_allocation (widget, &allocation);
1588 window = gtk_widget_get_window (widget);
1593 if (gtk_widget_has_default (widget) &&
1594 priv->relief == GTK_RELIEF_NORMAL)
1596 x += default_border.left;
1597 y += default_border.top;
1598 width -= default_border.left + default_border.right;
1599 height -= default_border.top + default_border.bottom;
1601 gtk_style_context_add_class (context, GTK_STYLE_CLASS_DEFAULT);
1603 else if (gtk_widget_get_can_default (widget))
1605 x += default_outside_border.left;
1606 y += default_outside_border.top;
1607 width -= default_outside_border.left + default_outside_border.right;
1608 height -= default_outside_border.top + default_outside_border.bottom;
1611 if (!interior_focus && gtk_widget_has_focus (widget))
1613 x += focus_width + focus_pad;
1614 y += focus_width + focus_pad;
1615 width -= 2 * (focus_width + focus_pad);
1616 height -= 2 * (focus_width + focus_pad);
1619 state = gtk_widget_get_state_flags (widget);
1620 gtk_style_context_set_state (context, state);
1622 if (priv->relief != GTK_RELIEF_NONE || priv->depressed ||
1623 state & GTK_STATE_FLAG_PRELIGHT)
1625 gtk_render_background (context, cr,
1626 x, y, width, height);
1627 gtk_render_frame (context, cr,
1628 x, y, width, height);
1631 if (gtk_widget_has_focus (widget))
1633 gint child_displacement_x;
1634 gint child_displacement_y;
1635 gboolean displace_focus;
1638 gtk_style_context_get_style (context,
1639 "child-displacement-y", &child_displacement_y,
1640 "child-displacement-x", &child_displacement_x,
1641 "displace-focus", &displace_focus,
1644 gtk_style_context_get (context, state,
1645 "border-width", &border,
1650 x += border->left + focus_pad;
1651 y += border->top + focus_pad;
1652 width -= (2 * focus_pad) + border->left + border->right;
1653 height -= (2 * focus_pad) + border->top + border->bottom;
1657 x -= focus_width + focus_pad;
1658 y -= focus_width + focus_pad;
1659 width += 2 * (focus_width + focus_pad);
1660 height += 2 * (focus_width + focus_pad);
1663 if (priv->depressed && displace_focus)
1665 x += child_displacement_x;
1666 y += child_displacement_y;
1669 gtk_render_focus (context, cr,
1670 x, y, width, height);
1672 gtk_border_free (border);
1677 gtk_button_draw (GtkWidget *widget,
1680 GtkButton *button = GTK_BUTTON (widget);
1681 GtkButtonPrivate *priv = button->priv;
1683 _gtk_button_paint (button, cr,
1684 gtk_widget_get_allocated_width (widget),
1685 gtk_widget_get_allocated_height (widget),
1686 gtk_widget_get_state (widget),
1687 priv->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1688 "button", "buttondefault");
1690 GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1696 gtk_button_button_press (GtkWidget *widget,
1697 GdkEventButton *event)
1700 GtkButtonPrivate *priv;
1702 if (event->type == GDK_BUTTON_PRESS)
1704 button = GTK_BUTTON (widget);
1705 priv = button->priv;
1707 if (priv->focus_on_click && !gtk_widget_has_focus (widget))
1708 gtk_widget_grab_focus (widget);
1710 if (event->button == 1)
1711 gtk_button_pressed (button);
1718 gtk_button_button_release (GtkWidget *widget,
1719 GdkEventButton *event)
1723 if (event->button == 1)
1725 button = GTK_BUTTON (widget);
1726 gtk_button_released (button);
1733 gtk_button_grab_broken (GtkWidget *widget,
1734 GdkEventGrabBroken *event)
1736 GtkButton *button = GTK_BUTTON (widget);
1737 GtkButtonPrivate *priv = button->priv;
1740 /* Simulate a button release without the pointer in the button */
1741 if (priv->button_down)
1743 save_in = priv->in_button;
1744 priv->in_button = FALSE;
1745 gtk_button_released (button);
1746 if (save_in != priv->in_button)
1748 priv->in_button = save_in;
1749 gtk_button_update_state (button);
1757 gtk_button_key_release (GtkWidget *widget,
1760 GtkButton *button = GTK_BUTTON (widget);
1761 GtkButtonPrivate *priv = button->priv;
1763 if (priv->activate_timeout)
1765 gtk_button_finish_activate (button, TRUE);
1768 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1769 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1775 gtk_button_enter_notify (GtkWidget *widget,
1776 GdkEventCrossing *event)
1778 GtkButton *button = GTK_BUTTON (widget);
1779 GtkButtonPrivate *priv = button->priv;
1781 if ((event->window == button->priv->event_window) &&
1782 (event->detail != GDK_NOTIFY_INFERIOR))
1784 priv->in_button = TRUE;
1785 gtk_button_enter (button);
1792 gtk_button_leave_notify (GtkWidget *widget,
1793 GdkEventCrossing *event)
1795 GtkButton *button = GTK_BUTTON (widget);
1796 GtkButtonPrivate *priv = button->priv;
1798 if ((event->window == button->priv->event_window) &&
1799 (event->detail != GDK_NOTIFY_INFERIOR) &&
1800 (gtk_widget_get_sensitive (widget)))
1802 priv->in_button = FALSE;
1803 gtk_button_leave (button);
1810 gtk_real_button_pressed (GtkButton *button)
1812 GtkButtonPrivate *priv = button->priv;
1814 if (priv->activate_timeout)
1817 priv->button_down = TRUE;
1818 gtk_button_update_state (button);
1822 gtk_real_button_released (GtkButton *button)
1824 GtkButtonPrivate *priv = button->priv;
1826 if (priv->button_down)
1828 priv->button_down = FALSE;
1830 if (priv->activate_timeout)
1833 if (priv->in_button)
1834 gtk_button_clicked (button);
1836 gtk_button_update_state (button);
1841 gtk_real_button_clicked (GtkButton *button)
1843 GtkButtonPrivate *priv = button->priv;
1846 gtk_action_activate (priv->action);
1850 button_activate_timeout (gpointer data)
1852 gtk_button_finish_activate (data, TRUE);
1858 gtk_real_button_activate (GtkButton *button)
1860 GtkWidget *widget = GTK_WIDGET (button);
1861 GtkButtonPrivate *priv = button->priv;
1865 device = gtk_get_current_event_device ();
1867 if (device && gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
1868 device = gdk_device_get_associated_device (device);
1870 g_return_if_fail (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD);
1872 if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
1874 time = gtk_get_current_event_time ();
1876 if (gdk_device_grab (device, priv->event_window,
1877 GDK_OWNERSHIP_WINDOW, TRUE,
1878 GDK_KEY_PRESS | GDK_KEY_RELEASE,
1879 NULL, time) == GDK_GRAB_SUCCESS)
1881 gtk_device_grab_add (widget, device, TRUE);
1882 priv->grab_keyboard = device;
1883 priv->grab_time = time;
1886 priv->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1887 button_activate_timeout,
1889 priv->button_down = TRUE;
1890 gtk_button_update_state (button);
1891 gtk_widget_queue_draw (GTK_WIDGET (button));
1896 gtk_button_finish_activate (GtkButton *button,
1899 GtkWidget *widget = GTK_WIDGET (button);
1900 GtkButtonPrivate *priv = button->priv;
1902 g_source_remove (priv->activate_timeout);
1903 priv->activate_timeout = 0;
1905 if (priv->grab_keyboard)
1907 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1908 gtk_device_grab_remove (widget, priv->grab_keyboard);
1909 priv->grab_keyboard = NULL;
1912 priv->button_down = FALSE;
1914 gtk_button_update_state (button);
1915 gtk_widget_queue_draw (GTK_WIDGET (button));
1918 gtk_button_clicked (button);
1923 gtk_button_get_size (GtkWidget *widget,
1924 GtkOrientation orientation,
1928 GtkButton *button = GTK_BUTTON (widget);
1929 GtkStyleContext *context;
1930 GtkStateFlags state;
1932 GtkBorder default_border;
1933 GtkBorder inner_border;
1937 gint minimum, natural;
1939 context = gtk_widget_get_style_context (widget);
1940 state = gtk_widget_get_state_flags (widget);
1942 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1943 gtk_style_context_get_style (context,
1944 "focus-line-width", &focus_width,
1945 "focus-padding", &focus_pad,
1948 gtk_style_context_get (context, state,
1949 "border-width", &border,
1952 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1954 minimum = (border->left + border->right +
1955 inner_border.left + inner_border.right);
1957 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1958 minimum += default_border.left + default_border.right;
1962 minimum = (border->top + border->bottom +
1963 inner_border.top + inner_border.bottom);
1965 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1966 minimum += default_border.top + default_border.bottom;
1969 minimum += 2 * (focus_width + focus_pad);
1972 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1973 gtk_widget_get_visible (child))
1975 gint child_min, child_nat;
1977 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1978 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
1980 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1982 minimum += child_min;
1983 natural += child_nat;
1987 *minimum_size = minimum;
1990 *natural_size = natural;
1992 gtk_border_free (border);
1996 gtk_button_get_preferred_width (GtkWidget *widget,
2000 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2004 gtk_button_get_preferred_height (GtkWidget *widget,
2008 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2012 * gtk_button_set_label:
2013 * @button: a #GtkButton
2016 * Sets the text of the label of the button to @str. This text is
2017 * also used to select the stock item if gtk_button_set_use_stock()
2020 * This will also clear any previously set labels.
2023 gtk_button_set_label (GtkButton *button,
2026 GtkButtonPrivate *priv;
2029 g_return_if_fail (GTK_IS_BUTTON (button));
2031 priv = button->priv;
2033 new_label = g_strdup (label);
2034 g_free (priv->label_text);
2035 priv->label_text = new_label;
2037 gtk_button_construct_child (button);
2039 g_object_notify (G_OBJECT (button), "label");
2043 * gtk_button_get_label:
2044 * @button: a #GtkButton
2046 * Fetches the text from the label of the button, as set by
2047 * gtk_button_set_label(). If the label text has not
2048 * been set the return value will be %NULL. This will be the
2049 * case if you create an empty button with gtk_button_new() to
2050 * use as a container.
2052 * Return value: The text of the label widget. This string is owned
2053 * by the widget and must not be modified or freed.
2055 G_CONST_RETURN gchar *
2056 gtk_button_get_label (GtkButton *button)
2058 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2060 return button->priv->label_text;
2064 * gtk_button_set_use_underline:
2065 * @button: a #GtkButton
2066 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2068 * If true, an underline in the text of the button label indicates
2069 * the next character should be used for the mnemonic accelerator key.
2072 gtk_button_set_use_underline (GtkButton *button,
2073 gboolean use_underline)
2075 GtkButtonPrivate *priv;
2077 g_return_if_fail (GTK_IS_BUTTON (button));
2079 priv = button->priv;
2081 use_underline = use_underline != FALSE;
2083 if (use_underline != priv->use_underline)
2085 priv->use_underline = use_underline;
2087 gtk_button_construct_child (button);
2089 g_object_notify (G_OBJECT (button), "use-underline");
2094 * gtk_button_get_use_underline:
2095 * @button: a #GtkButton
2097 * Returns whether an embedded underline in the button label indicates a
2098 * mnemonic. See gtk_button_set_use_underline ().
2100 * Return value: %TRUE if an embedded underline in the button label
2101 * indicates the mnemonic accelerator keys.
2104 gtk_button_get_use_underline (GtkButton *button)
2106 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2108 return button->priv->use_underline;
2112 * gtk_button_set_use_stock:
2113 * @button: a #GtkButton
2114 * @use_stock: %TRUE if the button should use a stock item
2116 * If %TRUE, the label set on the button is used as a
2117 * stock id to select the stock item for the button.
2120 gtk_button_set_use_stock (GtkButton *button,
2123 GtkButtonPrivate *priv;
2125 g_return_if_fail (GTK_IS_BUTTON (button));
2127 priv = button->priv;
2129 use_stock = use_stock != FALSE;
2131 if (use_stock != priv->use_stock)
2133 priv->use_stock = use_stock;
2135 gtk_button_construct_child (button);
2137 g_object_notify (G_OBJECT (button), "use-stock");
2142 * gtk_button_get_use_stock:
2143 * @button: a #GtkButton
2145 * Returns whether the button label is a stock item.
2147 * Return value: %TRUE if the button label is used to
2148 * select a stock item instead of being
2149 * used directly as the label text.
2152 gtk_button_get_use_stock (GtkButton *button)
2154 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2156 return button->priv->use_stock;
2160 * gtk_button_set_focus_on_click:
2161 * @button: a #GtkButton
2162 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2164 * Sets whether the button will grab focus when it is clicked with the mouse.
2165 * Making mouse clicks not grab focus is useful in places like toolbars where
2166 * you don't want the keyboard focus removed from the main area of the
2172 gtk_button_set_focus_on_click (GtkButton *button,
2173 gboolean focus_on_click)
2175 GtkButtonPrivate *priv;
2177 g_return_if_fail (GTK_IS_BUTTON (button));
2179 priv = button->priv;
2181 focus_on_click = focus_on_click != FALSE;
2183 if (priv->focus_on_click != focus_on_click)
2185 priv->focus_on_click = focus_on_click;
2187 g_object_notify (G_OBJECT (button), "focus-on-click");
2192 * gtk_button_get_focus_on_click:
2193 * @button: a #GtkButton
2195 * Returns whether the button grabs focus when it is clicked with the mouse.
2196 * See gtk_button_set_focus_on_click().
2198 * Return value: %TRUE if the button grabs focus when it is clicked with
2204 gtk_button_get_focus_on_click (GtkButton *button)
2206 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2208 return button->priv->focus_on_click;
2212 * gtk_button_set_alignment:
2213 * @button: a #GtkButton
2214 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2215 * 1.0 is right aligned
2216 * @yalign: the vertical position of the child, 0.0 is top aligned,
2217 * 1.0 is bottom aligned
2219 * Sets the alignment of the child. This property has no effect unless
2220 * the child is a #GtkMisc or a #GtkAlignment.
2225 gtk_button_set_alignment (GtkButton *button,
2229 GtkButtonPrivate *priv;
2231 g_return_if_fail (GTK_IS_BUTTON (button));
2233 priv = button->priv;
2235 priv->xalign = xalign;
2236 priv->yalign = yalign;
2237 priv->align_set = 1;
2239 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2241 g_object_freeze_notify (G_OBJECT (button));
2242 g_object_notify (G_OBJECT (button), "xalign");
2243 g_object_notify (G_OBJECT (button), "yalign");
2244 g_object_thaw_notify (G_OBJECT (button));
2248 * gtk_button_get_alignment:
2249 * @button: a #GtkButton
2250 * @xalign: return location for horizontal alignment
2251 * @yalign: return location for vertical alignment
2253 * Gets the alignment of the child in the button.
2258 gtk_button_get_alignment (GtkButton *button,
2262 GtkButtonPrivate *priv;
2264 g_return_if_fail (GTK_IS_BUTTON (button));
2266 priv = button->priv;
2269 *xalign = priv->xalign;
2272 *yalign = priv->yalign;
2276 * _gtk_button_set_depressed:
2277 * @button: a #GtkButton
2278 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2280 * Sets whether the button is currently drawn as down or not. This is
2281 * purely a visual setting, and is meant only for use by derived widgets
2282 * such as #GtkToggleButton.
2285 _gtk_button_set_depressed (GtkButton *button,
2288 GtkWidget *widget = GTK_WIDGET (button);
2289 GtkButtonPrivate *priv = button->priv;
2291 depressed = depressed != FALSE;
2293 if (depressed != priv->depressed)
2295 priv->depressed = depressed;
2296 gtk_widget_queue_resize (widget);
2301 gtk_button_update_state (GtkButton *button)
2303 GtkButtonPrivate *priv = button->priv;
2304 GtkStateFlags new_state = 0;
2307 if (priv->activate_timeout)
2308 depressed = priv->depress_on_activate;
2310 depressed = priv->in_button && priv->button_down;
2312 if (priv->in_button)
2313 new_state |= GTK_STATE_FLAG_PRELIGHT;
2315 if (priv->button_down || depressed)
2316 new_state |= GTK_STATE_FLAG_ACTIVE;
2318 _gtk_button_set_depressed (button, depressed);
2319 gtk_widget_set_state_flags (GTK_WIDGET (button), new_state, TRUE);
2323 show_image_change_notify (GtkButton *button)
2325 GtkButtonPrivate *priv = button->priv;
2329 if (show_image (button))
2330 gtk_widget_show (priv->image);
2332 gtk_widget_hide (priv->image);
2337 traverse_container (GtkWidget *widget,
2340 if (GTK_IS_BUTTON (widget))
2341 show_image_change_notify (GTK_BUTTON (widget));
2342 else if (GTK_IS_CONTAINER (widget))
2343 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2347 gtk_button_setting_changed (GtkSettings *settings)
2351 list = gtk_window_list_toplevels ();
2353 for (l = list; l; l = l->next)
2354 gtk_container_forall (GTK_CONTAINER (l->data),
2355 traverse_container, NULL);
2362 gtk_button_screen_changed (GtkWidget *widget,
2363 GdkScreen *previous_screen)
2366 GtkButtonPrivate *priv;
2367 GtkSettings *settings;
2368 gulong show_image_connection;
2370 if (!gtk_widget_has_screen (widget))
2373 button = GTK_BUTTON (widget);
2374 priv = button->priv;
2376 /* If the button is being pressed while the screen changes the
2377 release might never occur, so we reset the state. */
2378 if (priv->button_down)
2380 priv->button_down = FALSE;
2381 gtk_button_update_state (button);
2384 settings = gtk_widget_get_settings (widget);
2386 show_image_connection =
2387 g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2388 NULL, gtk_button_setting_changed, NULL);
2390 if (show_image_connection)
2393 g_signal_connect (settings, "notify::gtk-button-images",
2394 G_CALLBACK (gtk_button_setting_changed), NULL);
2396 show_image_change_notify (button);
2400 gtk_button_state_changed (GtkWidget *widget,
2401 GtkStateType previous_state)
2403 GtkButton *button = GTK_BUTTON (widget);
2404 GtkButtonPrivate *priv = button->priv;
2406 if (!gtk_widget_is_sensitive (widget))
2408 priv->in_button = FALSE;
2409 gtk_real_button_released (button);
2414 gtk_button_grab_notify (GtkWidget *widget,
2415 gboolean was_grabbed)
2417 GtkButton *button = GTK_BUTTON (widget);
2418 GtkButtonPrivate *priv = button->priv;
2421 if (priv->activate_timeout &&
2422 priv->grab_keyboard &&
2423 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2424 gtk_button_finish_activate (button, FALSE);
2428 save_in = priv->in_button;
2429 priv->in_button = FALSE;
2430 gtk_real_button_released (button);
2431 if (save_in != priv->in_button)
2433 priv->in_button = save_in;
2434 gtk_button_update_state (button);
2440 * gtk_button_set_image:
2441 * @button: a #GtkButton
2442 * @image: a widget to set as the image for the button
2444 * Set the image of @button to the given widget. Note that
2445 * it depends on the #GtkSettings:gtk-button-images setting whether the
2446 * image will be displayed or not, you don't have to call
2447 * gtk_widget_show() on @image yourself.
2452 gtk_button_set_image (GtkButton *button,
2455 GtkButtonPrivate *priv;
2458 g_return_if_fail (GTK_IS_BUTTON (button));
2459 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2461 priv = button->priv;
2465 parent = gtk_widget_get_parent (priv->image);
2467 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2470 priv->image = image;
2471 priv->image_is_stock = (image == NULL);
2473 gtk_button_construct_child (button);
2475 g_object_notify (G_OBJECT (button), "image");
2479 * gtk_button_get_image:
2480 * @button: a #GtkButton
2482 * Gets the widget that is currenty set as the image of @button.
2483 * This may have been explicitly set by gtk_button_set_image()
2484 * or constructed by gtk_button_new_from_stock().
2486 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2491 gtk_button_get_image (GtkButton *button)
2493 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2495 return button->priv->image;
2499 * gtk_button_set_image_position:
2500 * @button: a #GtkButton
2501 * @position: the position
2503 * Sets the position of the image relative to the text
2504 * inside the button.
2509 gtk_button_set_image_position (GtkButton *button,
2510 GtkPositionType position)
2512 GtkButtonPrivate *priv;
2514 g_return_if_fail (GTK_IS_BUTTON (button));
2515 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2517 priv = button->priv;
2519 if (priv->image_position != position)
2521 priv->image_position = position;
2523 gtk_button_construct_child (button);
2525 g_object_notify (G_OBJECT (button), "image-position");
2530 * gtk_button_get_image_position:
2531 * @button: a #GtkButton
2533 * Gets the position of the image relative to the text
2534 * inside the button.
2536 * Return value: the position
2541 gtk_button_get_image_position (GtkButton *button)
2543 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2545 return button->priv->image_position;
2550 * gtk_button_get_event_window:
2551 * @button: a #GtkButton
2553 * Returns the button's event window if it is realized, %NULL otherwise.
2554 * This function should be rarely needed.
2556 * Return value: (transfer none): @button's event window.
2561 gtk_button_get_event_window (GtkButton *button)
2563 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2565 return button->priv->event_window;