1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2001. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 * @Short_description: A widget that creates a signal when clicked on
32 * The #GtkButton widget is generally used to attach a function to that is
33 * called when the button is pressed. The various signals and how to use them
36 * The #GtkButton widget can hold any valid child widget. That is it can hold
37 * most any other standard #GtkWidget. The most commonly used child is the
43 #include "gtkalignment.h"
44 #include "gtkbutton.h"
47 #include "gtkmarshalers.h"
52 #include "gtkiconfactory.h"
53 #include "gtkactivatable.h"
54 #include "gtksizerequest.h"
55 #include "gtkprivate.h"
59 struct _GtkButtonPrivate
62 GtkPositionType image_position;
65 GdkDevice *grab_keyboard;
66 GdkWindow *event_window;
73 guint activate_timeout;
77 guint button_down : 1;
78 guint constructed : 1;
80 guint depress_on_activate : 1;
81 guint focus_on_click : 1;
82 guint image_is_stock : 1;
85 guint use_action_appearance : 1;
87 guint use_underline : 1;
91 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
92 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
93 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
95 /* Time out before giving up on getting a key release when animating
98 #define ACTIVATE_TIMEOUT 250
123 /* activatable properties */
124 PROP_ACTIVATABLE_RELATED_ACTION,
125 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
129 static void gtk_button_destroy (GtkWidget *widget);
130 static void gtk_button_dispose (GObject *object);
131 static void gtk_button_set_property (GObject *object,
135 static void gtk_button_get_property (GObject *object,
139 static void gtk_button_screen_changed (GtkWidget *widget,
140 GdkScreen *previous_screen);
141 static void gtk_button_realize (GtkWidget * widget);
142 static void gtk_button_unrealize (GtkWidget * widget);
143 static void gtk_button_map (GtkWidget * widget);
144 static void gtk_button_unmap (GtkWidget * widget);
145 static void gtk_button_style_set (GtkWidget * widget, GtkStyle * prev_style);
146 static void gtk_button_size_allocate (GtkWidget * widget,
147 GtkAllocation * allocation);
148 static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
149 static gint gtk_button_button_press (GtkWidget * widget,
150 GdkEventButton * event);
151 static gint gtk_button_button_release (GtkWidget * widget,
152 GdkEventButton * event);
153 static gint gtk_button_grab_broken (GtkWidget * widget,
154 GdkEventGrabBroken * event);
155 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
156 static gint gtk_button_enter_notify (GtkWidget * widget,
157 GdkEventCrossing * event);
158 static gint gtk_button_leave_notify (GtkWidget * widget,
159 GdkEventCrossing * event);
160 static void gtk_real_button_pressed (GtkButton * button);
161 static void gtk_real_button_released (GtkButton * button);
162 static void gtk_real_button_clicked (GtkButton * button);
163 static void gtk_real_button_activate (GtkButton *button);
164 static void gtk_button_update_state (GtkButton *button);
165 static void gtk_button_add (GtkContainer *container,
167 static GType gtk_button_child_type (GtkContainer *container);
168 static void gtk_button_finish_activate (GtkButton *button,
171 static GObject* gtk_button_constructor (GType type,
172 guint n_construct_properties,
173 GObjectConstructParam *construct_params);
174 static void gtk_button_construct_child (GtkButton *button);
175 static void gtk_button_state_changed (GtkWidget *widget,
176 GtkStateType previous_state);
177 static void gtk_button_grab_notify (GtkWidget *widget,
178 gboolean was_grabbed);
181 static void gtk_button_activatable_interface_init(GtkActivatableIface *iface);
182 static void gtk_button_update (GtkActivatable *activatable,
184 const gchar *property_name);
185 static void gtk_button_sync_action_properties (GtkActivatable *activatable,
187 static void gtk_button_set_related_action (GtkButton *button,
189 static void gtk_button_set_use_action_appearance (GtkButton *button,
190 gboolean use_appearance);
192 static void gtk_button_get_preferred_width (GtkWidget *widget,
195 static void gtk_button_get_preferred_height (GtkWidget *widget,
199 static guint button_signals[LAST_SIGNAL] = { 0 };
201 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
202 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
203 gtk_button_activatable_interface_init))
206 gtk_button_class_init (GtkButtonClass *klass)
208 GObjectClass *gobject_class;
209 GtkWidgetClass *widget_class;
210 GtkContainerClass *container_class;
212 gobject_class = G_OBJECT_CLASS (klass);
213 widget_class = (GtkWidgetClass*) klass;
214 container_class = (GtkContainerClass*) klass;
216 gobject_class->constructor = gtk_button_constructor;
217 gobject_class->dispose = gtk_button_dispose;
218 gobject_class->set_property = gtk_button_set_property;
219 gobject_class->get_property = gtk_button_get_property;
221 widget_class->get_preferred_width = gtk_button_get_preferred_width;
222 widget_class->get_preferred_height = gtk_button_get_preferred_height;
223 widget_class->destroy = gtk_button_destroy;
224 widget_class->screen_changed = gtk_button_screen_changed;
225 widget_class->realize = gtk_button_realize;
226 widget_class->unrealize = gtk_button_unrealize;
227 widget_class->map = gtk_button_map;
228 widget_class->unmap = gtk_button_unmap;
229 widget_class->style_set = gtk_button_style_set;
230 widget_class->size_allocate = gtk_button_size_allocate;
231 widget_class->draw = gtk_button_draw;
232 widget_class->button_press_event = gtk_button_button_press;
233 widget_class->button_release_event = gtk_button_button_release;
234 widget_class->grab_broken_event = gtk_button_grab_broken;
235 widget_class->key_release_event = gtk_button_key_release;
236 widget_class->enter_notify_event = gtk_button_enter_notify;
237 widget_class->leave_notify_event = gtk_button_leave_notify;
238 widget_class->state_changed = gtk_button_state_changed;
239 widget_class->grab_notify = gtk_button_grab_notify;
241 container_class->child_type = gtk_button_child_type;
242 container_class->add = gtk_button_add;
243 gtk_container_class_handle_border_width (container_class);
245 klass->pressed = gtk_real_button_pressed;
246 klass->released = gtk_real_button_released;
247 klass->clicked = NULL;
248 klass->enter = gtk_button_update_state;
249 klass->leave = gtk_button_update_state;
250 klass->activate = gtk_real_button_activate;
252 g_object_class_install_property (gobject_class,
254 g_param_spec_string ("label",
256 P_("Text of the label widget inside the button, if the button contains a label widget"),
258 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
260 g_object_class_install_property (gobject_class,
262 g_param_spec_boolean ("use-underline",
264 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
266 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
268 g_object_class_install_property (gobject_class,
270 g_param_spec_boolean ("use-stock",
272 P_("If set, the label is used to pick a stock item instead of being displayed"),
274 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
276 g_object_class_install_property (gobject_class,
278 g_param_spec_boolean ("focus-on-click",
279 P_("Focus on click"),
280 P_("Whether the button grabs focus when it is clicked with the mouse"),
282 GTK_PARAM_READWRITE));
284 g_object_class_install_property (gobject_class,
286 g_param_spec_enum ("relief",
288 P_("The border relief style"),
289 GTK_TYPE_RELIEF_STYLE,
291 GTK_PARAM_READWRITE));
296 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
297 * can be used to control it's horizontal alignment. 0.0 is left aligned,
298 * 1.0 is right aligned.
302 g_object_class_install_property (gobject_class,
304 g_param_spec_float("xalign",
305 P_("Horizontal alignment for child"),
306 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
310 GTK_PARAM_READWRITE));
315 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
316 * can be used to control it's vertical alignment. 0.0 is top aligned,
317 * 1.0 is bottom aligned.
321 g_object_class_install_property (gobject_class,
323 g_param_spec_float("yalign",
324 P_("Vertical alignment for child"),
325 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
329 GTK_PARAM_READWRITE));
334 * The child widget to appear next to the button text.
338 g_object_class_install_property (gobject_class,
340 g_param_spec_object ("image",
342 P_("Child widget to appear next to the button text"),
344 GTK_PARAM_READWRITE));
347 * GtkButton:image-position:
349 * The position of the image relative to the text inside the button.
353 g_object_class_install_property (gobject_class,
355 g_param_spec_enum ("image-position",
356 P_("Image position"),
357 P_("The position of the image relative to the text"),
358 GTK_TYPE_POSITION_TYPE,
360 GTK_PARAM_READWRITE));
362 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
363 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
366 * GtkButton::pressed:
367 * @button: the object that received the signal
369 * Emitted when the button is pressed.
371 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
373 button_signals[PRESSED] =
374 g_signal_new (I_("pressed"),
375 G_OBJECT_CLASS_TYPE (gobject_class),
377 G_STRUCT_OFFSET (GtkButtonClass, pressed),
379 _gtk_marshal_VOID__VOID,
383 * GtkButton::released:
384 * @button: the object that received the signal
386 * Emitted when the button is released.
388 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
390 button_signals[RELEASED] =
391 g_signal_new (I_("released"),
392 G_OBJECT_CLASS_TYPE (gobject_class),
394 G_STRUCT_OFFSET (GtkButtonClass, released),
396 _gtk_marshal_VOID__VOID,
400 * GtkButton::clicked:
401 * @button: the object that received the signal
403 * Emitted when the button has been activated (pressed and released).
405 button_signals[CLICKED] =
406 g_signal_new (I_("clicked"),
407 G_OBJECT_CLASS_TYPE (gobject_class),
408 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
409 G_STRUCT_OFFSET (GtkButtonClass, clicked),
411 _gtk_marshal_VOID__VOID,
416 * @button: the object that received the signal
418 * Emitted when the pointer enters the button.
420 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
422 button_signals[ENTER] =
423 g_signal_new (I_("enter"),
424 G_OBJECT_CLASS_TYPE (gobject_class),
426 G_STRUCT_OFFSET (GtkButtonClass, enter),
428 _gtk_marshal_VOID__VOID,
433 * @button: the object that received the signal
435 * Emitted when the pointer leaves the button.
437 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
439 button_signals[LEAVE] =
440 g_signal_new (I_("leave"),
441 G_OBJECT_CLASS_TYPE (gobject_class),
443 G_STRUCT_OFFSET (GtkButtonClass, leave),
445 _gtk_marshal_VOID__VOID,
449 * GtkButton::activate:
450 * @widget: the object which received the signal.
452 * The ::activate signal on GtkButton is an action signal and
453 * emitting it causes the button to animate press then release.
454 * Applications should never connect to this signal, but use the
455 * #GtkButton::clicked signal.
457 button_signals[ACTIVATE] =
458 g_signal_new (I_("activate"),
459 G_OBJECT_CLASS_TYPE (gobject_class),
460 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
461 G_STRUCT_OFFSET (GtkButtonClass, activate),
463 _gtk_marshal_VOID__VOID,
465 widget_class->activate_signal = button_signals[ACTIVATE];
468 * GtkButton:default-border:
470 * The "default-border" style property defines the extra space to add
471 * around a button that can become the default widget of its window.
472 * For more information about default widgets, see gtk_widget_grab_default().
475 gtk_widget_class_install_style_property (widget_class,
476 g_param_spec_boxed ("default-border",
477 P_("Default Spacing"),
478 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
480 GTK_PARAM_READABLE));
483 * GtkButton:default-outside-border:
485 * The "default-outside-border" style property defines the extra outside
486 * space to add around a button that can become the default widget of its
487 * window. Extra outside space is always drawn outside the button border.
488 * For more information about default widgets, see gtk_widget_grab_default().
490 gtk_widget_class_install_style_property (widget_class,
491 g_param_spec_boxed ("default-outside-border",
492 P_("Default Outside Spacing"),
493 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
495 GTK_PARAM_READABLE));
496 gtk_widget_class_install_style_property (widget_class,
497 g_param_spec_int ("child-displacement-x",
498 P_("Child X Displacement"),
499 P_("How far in the x direction to move the child when the button is depressed"),
503 GTK_PARAM_READABLE));
504 gtk_widget_class_install_style_property (widget_class,
505 g_param_spec_int ("child-displacement-y",
506 P_("Child Y Displacement"),
507 P_("How far in the y direction to move the child when the button is depressed"),
511 GTK_PARAM_READABLE));
514 * GtkButton:displace-focus:
516 * Whether the child_displacement_x/child_displacement_y properties
517 * should also affect the focus rectangle.
521 gtk_widget_class_install_style_property (widget_class,
522 g_param_spec_boolean ("displace-focus",
523 P_("Displace focus"),
524 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
526 GTK_PARAM_READABLE));
529 * GtkButton:inner-border:
531 * Sets the border between the button edges and child.
535 gtk_widget_class_install_style_property (widget_class,
536 g_param_spec_boxed ("inner-border",
538 P_("Border between button edges and child."),
540 GTK_PARAM_READABLE));
543 * GtkButton::image-spacing:
545 * Spacing in pixels between the image and label.
549 gtk_widget_class_install_style_property (widget_class,
550 g_param_spec_int ("image-spacing",
552 P_("Spacing in pixels between the image and label"),
556 GTK_PARAM_READABLE));
558 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
562 gtk_button_init (GtkButton *button)
564 GtkButtonPrivate *priv;
566 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
571 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
572 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
573 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
575 priv->label_text = NULL;
577 priv->constructed = FALSE;
578 priv->in_button = FALSE;
579 priv->button_down = FALSE;
580 priv->relief = GTK_RELIEF_NORMAL;
581 priv->use_stock = FALSE;
582 priv->use_underline = FALSE;
583 priv->depressed = FALSE;
584 priv->depress_on_activate = TRUE;
585 priv->focus_on_click = TRUE;
590 priv->image_is_stock = TRUE;
591 priv->image_position = GTK_POS_LEFT;
592 priv->use_action_appearance = TRUE;
596 gtk_button_destroy (GtkWidget *widget)
598 GtkButton *button = GTK_BUTTON (widget);
599 GtkButtonPrivate *priv = button->priv;
601 if (priv->label_text)
603 g_free (priv->label_text);
604 priv->label_text = NULL;
607 GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
611 gtk_button_constructor (GType type,
612 guint n_construct_properties,
613 GObjectConstructParam *construct_params)
617 GtkButtonPrivate *priv;
619 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
620 n_construct_properties,
623 button = GTK_BUTTON (object);
626 priv->constructed = TRUE;
628 if (priv->label_text != NULL)
629 gtk_button_construct_child (button);
636 gtk_button_child_type (GtkContainer *container)
638 if (!gtk_bin_get_child (GTK_BIN (container)))
639 return GTK_TYPE_WIDGET;
645 maybe_set_alignment (GtkButton *button,
648 GtkButtonPrivate *priv = button->priv;
650 if (GTK_IS_MISC (widget))
652 GtkMisc *misc = GTK_MISC (widget);
655 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
657 else if (GTK_IS_ALIGNMENT (widget))
659 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
660 gfloat xscale, yscale;
662 g_object_get (alignment,
668 gtk_alignment_set (alignment,
669 priv->xalign, priv->yalign,
675 gtk_button_add (GtkContainer *container,
678 maybe_set_alignment (GTK_BUTTON (container), widget);
680 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
684 gtk_button_dispose (GObject *object)
686 GtkButton *button = GTK_BUTTON (object);
687 GtkButtonPrivate *priv = button->priv;
691 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
694 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
698 gtk_button_set_property (GObject *object,
703 GtkButton *button = GTK_BUTTON (object);
704 GtkButtonPrivate *priv = button->priv;
709 gtk_button_set_label (button, g_value_get_string (value));
712 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
715 gtk_button_set_relief (button, g_value_get_enum (value));
717 case PROP_USE_UNDERLINE:
718 gtk_button_set_use_underline (button, g_value_get_boolean (value));
721 gtk_button_set_use_stock (button, g_value_get_boolean (value));
723 case PROP_FOCUS_ON_CLICK:
724 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
727 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
730 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
732 case PROP_IMAGE_POSITION:
733 gtk_button_set_image_position (button, g_value_get_enum (value));
735 case PROP_ACTIVATABLE_RELATED_ACTION:
736 gtk_button_set_related_action (button, g_value_get_object (value));
738 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
739 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
742 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
748 gtk_button_get_property (GObject *object,
753 GtkButton *button = GTK_BUTTON (object);
754 GtkButtonPrivate *priv = button->priv;
759 g_value_set_string (value, priv->label_text);
762 g_value_set_object (value, (GObject *)priv->image);
765 g_value_set_enum (value, priv->relief);
767 case PROP_USE_UNDERLINE:
768 g_value_set_boolean (value, priv->use_underline);
771 g_value_set_boolean (value, priv->use_stock);
773 case PROP_FOCUS_ON_CLICK:
774 g_value_set_boolean (value, priv->focus_on_click);
777 g_value_set_float (value, priv->xalign);
780 g_value_set_float (value, priv->yalign);
782 case PROP_IMAGE_POSITION:
783 g_value_set_enum (value, priv->image_position);
785 case PROP_ACTIVATABLE_RELATED_ACTION:
786 g_value_set_object (value, priv->action);
788 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
789 g_value_set_boolean (value, priv->use_action_appearance);
792 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
798 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
800 iface->update = gtk_button_update;
801 iface->sync_action_properties = gtk_button_sync_action_properties;
805 activatable_update_stock_id (GtkButton *button,
808 if (!gtk_button_get_use_stock (button))
811 gtk_button_set_label (button, gtk_action_get_stock_id (action));
815 activatable_update_short_label (GtkButton *button,
821 if (gtk_button_get_use_stock (button))
824 image = gtk_button_get_image (button);
826 /* Dont touch custom child... */
827 child = gtk_bin_get_child (GTK_BIN (button));
828 if (GTK_IS_IMAGE (image) ||
830 GTK_IS_LABEL (child))
832 gtk_button_set_label (button, gtk_action_get_short_label (action));
833 gtk_button_set_use_underline (button, TRUE);
838 activatable_update_icon_name (GtkButton *button,
843 if (gtk_button_get_use_stock (button))
846 image = gtk_button_get_image (button);
848 if (GTK_IS_IMAGE (image) &&
849 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
850 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
851 gtk_image_set_from_icon_name (GTK_IMAGE (image),
852 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
856 activatable_update_gicon (GtkButton *button,
859 GtkWidget *image = gtk_button_get_image (button);
860 GIcon *icon = gtk_action_get_gicon (action);
862 if (GTK_IS_IMAGE (image) &&
863 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
864 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
865 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
869 gtk_button_update (GtkActivatable *activatable,
871 const gchar *property_name)
873 GtkButton *button = GTK_BUTTON (activatable);
874 GtkButtonPrivate *priv = button->priv;
876 if (strcmp (property_name, "visible") == 0)
878 if (gtk_action_is_visible (action))
879 gtk_widget_show (GTK_WIDGET (activatable));
881 gtk_widget_hide (GTK_WIDGET (activatable));
883 else if (strcmp (property_name, "sensitive") == 0)
884 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
886 if (!priv->use_action_appearance)
889 if (strcmp (property_name, "stock-id") == 0)
890 activatable_update_stock_id (GTK_BUTTON (activatable), action);
891 else if (strcmp (property_name, "gicon") == 0)
892 activatable_update_gicon (GTK_BUTTON (activatable), action);
893 else if (strcmp (property_name, "short-label") == 0)
894 activatable_update_short_label (GTK_BUTTON (activatable), action);
895 else if (strcmp (property_name, "icon-name") == 0)
896 activatable_update_icon_name (GTK_BUTTON (activatable), action);
900 gtk_button_sync_action_properties (GtkActivatable *activatable,
903 GtkButton *button = GTK_BUTTON (activatable);
904 GtkButtonPrivate *priv = button->priv;
909 if (gtk_action_is_visible (action))
910 gtk_widget_show (GTK_WIDGET (activatable));
912 gtk_widget_hide (GTK_WIDGET (activatable));
914 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
916 if (priv->use_action_appearance)
918 activatable_update_stock_id (GTK_BUTTON (activatable), action);
919 activatable_update_short_label (GTK_BUTTON (activatable), action);
920 activatable_update_gicon (GTK_BUTTON (activatable), action);
921 activatable_update_icon_name (GTK_BUTTON (activatable), action);
926 gtk_button_set_related_action (GtkButton *button,
929 GtkButtonPrivate *priv = button->priv;
931 if (priv->action == action)
934 /* This should be a default handler, but for compatibility reasons
935 * we need to support derived classes that don't chain up their
938 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
940 g_signal_connect_after (button, "clicked",
941 G_CALLBACK (gtk_real_button_clicked), NULL);
943 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
945 priv->action = action;
949 gtk_button_set_use_action_appearance (GtkButton *button,
950 gboolean use_appearance)
952 GtkButtonPrivate *priv = button->priv;
954 if (priv->use_action_appearance != use_appearance)
956 priv->use_action_appearance = use_appearance;
958 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
965 * Creates a new #GtkButton widget. To add a child widget to the button,
966 * use gtk_container_add().
968 * Returns: The newly created #GtkButton widget.
971 gtk_button_new (void)
973 return g_object_new (GTK_TYPE_BUTTON, NULL);
977 show_image (GtkButton *button)
979 GtkButtonPrivate *priv = button->priv;
982 if (priv->label_text)
984 GtkSettings *settings;
986 settings = gtk_widget_get_settings (GTK_WIDGET (button));
987 g_object_get (settings, "gtk-button-images", &show, NULL);
996 gtk_button_construct_child (GtkButton *button)
998 GtkButtonPrivate *priv = button->priv;
1004 GtkWidget *image = NULL;
1005 gchar *label_text = NULL;
1008 if (!priv->constructed)
1011 if (!priv->label_text && !priv->image)
1014 gtk_widget_style_get (GTK_WIDGET (button),
1015 "image-spacing", &image_spacing,
1018 if (priv->image && !priv->image_is_stock)
1022 image = g_object_ref (priv->image);
1024 parent = gtk_widget_get_parent (image);
1026 gtk_container_remove (GTK_CONTAINER (parent), image);
1031 child = gtk_bin_get_child (GTK_BIN (button));
1033 gtk_container_remove (GTK_CONTAINER (button), child);
1035 if (priv->use_stock &&
1037 gtk_stock_lookup (priv->label_text, &item))
1040 image = g_object_ref (gtk_image_new_from_stock (priv->label_text, GTK_ICON_SIZE_BUTTON));
1042 label_text = item.label;
1045 label_text = priv->label_text;
1049 priv->image = image;
1050 g_object_set (priv->image,
1051 "visible", show_image (button),
1052 "no-show-all", TRUE,
1055 if (priv->image_position == GTK_POS_LEFT ||
1056 priv->image_position == GTK_POS_RIGHT)
1057 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1059 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1061 if (priv->align_set)
1062 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1064 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1066 if (priv->image_position == GTK_POS_LEFT ||
1067 priv->image_position == GTK_POS_TOP)
1068 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1070 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1074 if (priv->use_underline || priv->use_stock)
1076 label = gtk_label_new_with_mnemonic (label_text);
1077 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1078 GTK_WIDGET (button));
1081 label = gtk_label_new (label_text);
1083 if (priv->image_position == GTK_POS_RIGHT ||
1084 priv->image_position == GTK_POS_BOTTOM)
1085 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1087 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1090 gtk_container_add (GTK_CONTAINER (button), align);
1091 gtk_container_add (GTK_CONTAINER (align), box);
1092 gtk_widget_show_all (align);
1094 g_object_unref (image);
1099 if (priv->use_underline || priv->use_stock)
1101 label = gtk_label_new_with_mnemonic (priv->label_text);
1102 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1105 label = gtk_label_new (priv->label_text);
1107 if (priv->align_set)
1108 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1110 gtk_widget_show (label);
1111 gtk_container_add (GTK_CONTAINER (button), label);
1116 * gtk_button_new_with_label:
1117 * @label: The text you want the #GtkLabel to hold.
1119 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1122 * Returns: The newly created #GtkButton widget.
1125 gtk_button_new_with_label (const gchar *label)
1127 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1131 * gtk_button_new_from_stock:
1132 * @stock_id: the name of the stock item
1134 * Creates a new #GtkButton containing the image and text from a stock item.
1135 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1138 * If @stock_id is unknown, then it will be treated as a mnemonic
1139 * label (as for gtk_button_new_with_mnemonic()).
1141 * Returns: a new #GtkButton
1144 gtk_button_new_from_stock (const gchar *stock_id)
1146 return g_object_new (GTK_TYPE_BUTTON,
1149 "use-underline", TRUE,
1154 * gtk_button_new_with_mnemonic:
1155 * @label: The text of the button, with an underscore in front of the
1156 * mnemonic character
1157 * @returns: a new #GtkButton
1159 * Creates a new #GtkButton containing a label.
1160 * If characters in @label are preceded by an underscore, they are underlined.
1161 * If you need a literal underscore character in a label, use '__' (two
1162 * underscores). The first underlined character represents a keyboard
1163 * accelerator called a mnemonic.
1164 * Pressing Alt and that key activates the button.
1167 gtk_button_new_with_mnemonic (const gchar *label)
1169 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1173 * gtk_button_pressed:
1174 * @button: The #GtkButton you want to send the signal to.
1176 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1178 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1181 gtk_button_pressed (GtkButton *button)
1183 g_return_if_fail (GTK_IS_BUTTON (button));
1185 g_signal_emit (button, button_signals[PRESSED], 0);
1189 * gtk_button_released:
1190 * @button: The #GtkButton you want to send the signal to.
1192 * Emits a #GtkButton::released signal to the given #GtkButton.
1194 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1197 gtk_button_released (GtkButton *button)
1199 g_return_if_fail (GTK_IS_BUTTON (button));
1201 g_signal_emit (button, button_signals[RELEASED], 0);
1205 * gtk_button_clicked:
1206 * @button: The #GtkButton you want to send the signal to.
1208 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1211 gtk_button_clicked (GtkButton *button)
1213 g_return_if_fail (GTK_IS_BUTTON (button));
1215 g_signal_emit (button, button_signals[CLICKED], 0);
1220 * @button: The #GtkButton you want to send the signal to.
1222 * Emits a #GtkButton::enter signal to the given #GtkButton.
1224 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1227 gtk_button_enter (GtkButton *button)
1229 g_return_if_fail (GTK_IS_BUTTON (button));
1231 g_signal_emit (button, button_signals[ENTER], 0);
1236 * @button: The #GtkButton you want to send the signal to.
1238 * Emits a #GtkButton::leave signal to the given #GtkButton.
1240 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1243 gtk_button_leave (GtkButton *button)
1245 g_return_if_fail (GTK_IS_BUTTON (button));
1247 g_signal_emit (button, button_signals[LEAVE], 0);
1251 * gtk_button_set_relief:
1252 * @button: The #GtkButton you want to set relief styles of.
1253 * @newstyle: The GtkReliefStyle as described above.
1255 * Sets the relief style of the edges of the given #GtkButton widget.
1256 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1257 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1259 * <!-- FIXME: put pictures of each style -->
1262 gtk_button_set_relief (GtkButton *button,
1263 GtkReliefStyle newrelief)
1265 GtkButtonPrivate *priv;
1267 g_return_if_fail (GTK_IS_BUTTON (button));
1269 priv = button->priv;
1271 if (newrelief != priv->relief)
1273 priv->relief = newrelief;
1274 g_object_notify (G_OBJECT (button), "relief");
1275 gtk_widget_queue_draw (GTK_WIDGET (button));
1280 * gtk_button_get_relief:
1281 * @button: The #GtkButton you want the #GtkReliefStyle from.
1283 * Returns the current relief style of the given #GtkButton.
1285 * Returns: The current #GtkReliefStyle
1288 gtk_button_get_relief (GtkButton *button)
1290 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1292 return button->priv->relief;
1296 gtk_button_realize (GtkWidget *widget)
1298 GtkButton *button = GTK_BUTTON (widget);
1299 GtkButtonPrivate *priv = button->priv;
1300 GtkAllocation allocation;
1302 GdkWindowAttr attributes;
1303 gint attributes_mask;
1305 gtk_widget_get_allocation (widget, &allocation);
1307 gtk_widget_set_realized (widget, TRUE);
1309 attributes.window_type = GDK_WINDOW_CHILD;
1310 attributes.x = allocation.x;
1311 attributes.y = allocation.y;
1312 attributes.width = allocation.width;
1313 attributes.height = allocation.height;
1314 attributes.wclass = GDK_INPUT_ONLY;
1315 attributes.event_mask = gtk_widget_get_events (widget);
1316 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1317 GDK_BUTTON_RELEASE_MASK |
1318 GDK_ENTER_NOTIFY_MASK |
1319 GDK_LEAVE_NOTIFY_MASK);
1321 attributes_mask = GDK_WA_X | GDK_WA_Y;
1323 window = gtk_widget_get_parent_window (widget);
1324 gtk_widget_set_window (widget, window);
1325 g_object_ref (window);
1327 priv->event_window = gdk_window_new (window,
1328 &attributes, attributes_mask);
1329 gdk_window_set_user_data (priv->event_window, button);
1331 gtk_widget_style_attach (widget);
1335 gtk_button_unrealize (GtkWidget *widget)
1337 GtkButton *button = GTK_BUTTON (widget);
1338 GtkButtonPrivate *priv = button->priv;
1340 if (priv->activate_timeout)
1341 gtk_button_finish_activate (button, FALSE);
1343 if (priv->event_window)
1345 gdk_window_set_user_data (priv->event_window, NULL);
1346 gdk_window_destroy (priv->event_window);
1347 priv->event_window = NULL;
1350 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1354 gtk_button_map (GtkWidget *widget)
1356 GtkButton *button = GTK_BUTTON (widget);
1357 GtkButtonPrivate *priv = button->priv;
1359 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1361 if (priv->event_window)
1362 gdk_window_show (priv->event_window);
1366 gtk_button_unmap (GtkWidget *widget)
1368 GtkButton *button = GTK_BUTTON (widget);
1369 GtkButtonPrivate *priv = button->priv;
1371 if (priv->event_window)
1372 gdk_window_hide (priv->event_window);
1374 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1378 gtk_button_update_image_spacing (GtkButton *button)
1380 GtkButtonPrivate *priv = button->priv;
1384 /* Keep in sync with gtk_button_construct_child,
1385 * we only want to update the spacing if the box
1386 * was constructed there.
1388 if (!priv->constructed || !priv->image)
1391 child = gtk_bin_get_child (GTK_BIN (button));
1392 if (GTK_IS_ALIGNMENT (child))
1394 child = gtk_bin_get_child (GTK_BIN (child));
1395 if (GTK_IS_BOX (child))
1397 gtk_widget_style_get (GTK_WIDGET (button),
1398 "image-spacing", &spacing,
1401 gtk_box_set_spacing (GTK_BOX (child), spacing);
1407 gtk_button_style_set (GtkWidget *widget,
1408 GtkStyle *prev_style)
1410 gtk_button_update_image_spacing (GTK_BUTTON (widget));
1414 gtk_button_get_props (GtkButton *button,
1415 GtkBorder *default_border,
1416 GtkBorder *default_outside_border,
1417 GtkBorder *inner_border,
1418 gboolean *interior_focus)
1420 GtkWidget *widget = GTK_WIDGET (button);
1421 GtkBorder *tmp_border;
1425 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1429 *default_border = *tmp_border;
1430 gtk_border_free (tmp_border);
1433 *default_border = default_default_border;
1436 if (default_outside_border)
1438 gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1442 *default_outside_border = *tmp_border;
1443 gtk_border_free (tmp_border);
1446 *default_outside_border = default_default_outside_border;
1451 gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1455 *inner_border = *tmp_border;
1456 gtk_border_free (tmp_border);
1459 *inner_border = default_inner_border;
1463 gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1467 gtk_button_size_allocate (GtkWidget *widget,
1468 GtkAllocation *allocation)
1470 GtkButton *button = GTK_BUTTON (widget);
1471 GtkButtonPrivate *priv = button->priv;
1472 GtkAllocation child_allocation;
1476 gint xthickness, ythickness;
1477 GtkBorder default_border;
1478 GtkBorder inner_border;
1482 style = gtk_widget_get_style (widget);
1483 xthickness = style->xthickness;
1484 ythickness = style->ythickness;
1486 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1487 gtk_widget_style_get (GTK_WIDGET (widget),
1488 "focus-line-width", &focus_width,
1489 "focus-padding", &focus_pad,
1492 gtk_widget_set_allocation (widget, allocation);
1494 if (gtk_widget_get_realized (widget))
1495 gdk_window_move_resize (priv->event_window,
1499 allocation->height);
1501 child = gtk_bin_get_child (GTK_BIN (button));
1502 if (child && gtk_widget_get_visible (child))
1504 child_allocation.x = allocation->x + inner_border.left + xthickness;
1505 child_allocation.y = allocation->y + inner_border.top + ythickness;
1507 child_allocation.width =
1513 child_allocation.height =
1514 allocation->height -
1517 inner_border.bottom;
1519 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1521 child_allocation.x += default_border.left;
1522 child_allocation.y += default_border.top;
1523 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1524 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1527 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1529 child_allocation.x += focus_width + focus_pad;
1530 child_allocation.y += focus_width + focus_pad;
1531 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1532 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1535 if (priv->depressed)
1537 gint child_displacement_x;
1538 gint child_displacement_y;
1540 gtk_widget_style_get (widget,
1541 "child-displacement-x", &child_displacement_x,
1542 "child-displacement-y", &child_displacement_y,
1544 child_allocation.x += child_displacement_x;
1545 child_allocation.y += child_displacement_y;
1548 child_allocation.width = MAX (1, child_allocation.width);
1549 child_allocation.height = MAX (1, child_allocation.height);
1551 gtk_widget_size_allocate (child, &child_allocation);
1556 _gtk_button_paint (GtkButton *button,
1560 GtkStateType state_type,
1561 GtkShadowType shadow_type,
1562 const gchar *main_detail,
1563 const gchar *default_detail)
1565 GtkButtonPrivate *priv = button->priv;
1568 GtkBorder default_border;
1569 GtkBorder default_outside_border;
1570 gboolean interior_focus;
1573 GtkAllocation allocation;
1577 widget = GTK_WIDGET (button);
1579 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1580 gtk_widget_style_get (widget,
1581 "focus-line-width", &focus_width,
1582 "focus-padding", &focus_pad,
1585 gtk_widget_get_allocation (widget, &allocation);
1586 style = gtk_widget_get_style (widget);
1587 window = gtk_widget_get_window (widget);
1592 if (gtk_widget_has_default (widget) &&
1593 priv->relief == GTK_RELIEF_NORMAL)
1595 gtk_paint_box (style, cr,
1596 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1597 widget, "buttondefault",
1598 x, y, width, height);
1600 x += default_border.left;
1601 y += default_border.top;
1602 width -= default_border.left + default_border.right;
1603 height -= default_border.top + default_border.bottom;
1605 else if (gtk_widget_get_can_default (widget))
1607 x += default_outside_border.left;
1608 y += default_outside_border.top;
1609 width -= default_outside_border.left + default_outside_border.right;
1610 height -= default_outside_border.top + default_outside_border.bottom;
1613 if (!interior_focus && gtk_widget_has_focus (widget))
1615 x += focus_width + focus_pad;
1616 y += focus_width + focus_pad;
1617 width -= 2 * (focus_width + focus_pad);
1618 height -= 2 * (focus_width + focus_pad);
1621 if (priv->relief != GTK_RELIEF_NONE || priv->depressed ||
1622 gtk_widget_get_state(widget) == GTK_STATE_PRELIGHT)
1623 gtk_paint_box (style, cr,
1625 shadow_type, widget, "button",
1626 x, y, width, height);
1628 if (gtk_widget_has_focus (widget))
1630 gint child_displacement_x;
1631 gint child_displacement_y;
1632 gboolean displace_focus;
1634 gtk_widget_style_get (widget,
1635 "child-displacement-y", &child_displacement_y,
1636 "child-displacement-x", &child_displacement_x,
1637 "displace-focus", &displace_focus,
1642 x += style->xthickness + focus_pad;
1643 y += style->ythickness + focus_pad;
1644 width -= 2 * (style->xthickness + focus_pad);
1645 height -= 2 * (style->ythickness + focus_pad);
1649 x -= focus_width + focus_pad;
1650 y -= focus_width + focus_pad;
1651 width += 2 * (focus_width + focus_pad);
1652 height += 2 * (focus_width + focus_pad);
1655 if (priv->depressed && displace_focus)
1657 x += child_displacement_x;
1658 y += child_displacement_y;
1661 gtk_paint_focus (style, cr,
1662 gtk_widget_get_state (widget),
1664 x, y, width, height);
1669 gtk_button_draw (GtkWidget *widget,
1672 GtkButton *button = GTK_BUTTON (widget);
1673 GtkButtonPrivate *priv = button->priv;
1675 _gtk_button_paint (button, cr,
1676 gtk_widget_get_allocated_width (widget),
1677 gtk_widget_get_allocated_height (widget),
1678 gtk_widget_get_state (widget),
1679 priv->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1680 "button", "buttondefault");
1682 GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1688 gtk_button_button_press (GtkWidget *widget,
1689 GdkEventButton *event)
1692 GtkButtonPrivate *priv;
1694 if (event->type == GDK_BUTTON_PRESS)
1696 button = GTK_BUTTON (widget);
1697 priv = button->priv;
1699 if (priv->focus_on_click && !gtk_widget_has_focus (widget))
1700 gtk_widget_grab_focus (widget);
1702 if (event->button == 1)
1703 gtk_button_pressed (button);
1710 gtk_button_button_release (GtkWidget *widget,
1711 GdkEventButton *event)
1715 if (event->button == 1)
1717 button = GTK_BUTTON (widget);
1718 gtk_button_released (button);
1725 gtk_button_grab_broken (GtkWidget *widget,
1726 GdkEventGrabBroken *event)
1728 GtkButton *button = GTK_BUTTON (widget);
1729 GtkButtonPrivate *priv = button->priv;
1732 /* Simulate a button release without the pointer in the button */
1733 if (priv->button_down)
1735 save_in = priv->in_button;
1736 priv->in_button = FALSE;
1737 gtk_button_released (button);
1738 if (save_in != priv->in_button)
1740 priv->in_button = save_in;
1741 gtk_button_update_state (button);
1749 gtk_button_key_release (GtkWidget *widget,
1752 GtkButton *button = GTK_BUTTON (widget);
1753 GtkButtonPrivate *priv = button->priv;
1755 if (priv->activate_timeout)
1757 gtk_button_finish_activate (button, TRUE);
1760 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1761 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1767 gtk_button_enter_notify (GtkWidget *widget,
1768 GdkEventCrossing *event)
1770 GtkButton *button = GTK_BUTTON (widget);
1771 GtkButtonPrivate *priv = button->priv;
1773 if ((event->window == button->priv->event_window) &&
1774 (event->detail != GDK_NOTIFY_INFERIOR))
1776 priv->in_button = TRUE;
1777 gtk_button_enter (button);
1784 gtk_button_leave_notify (GtkWidget *widget,
1785 GdkEventCrossing *event)
1787 GtkButton *button = GTK_BUTTON (widget);
1788 GtkButtonPrivate *priv = button->priv;
1790 if ((event->window == button->priv->event_window) &&
1791 (event->detail != GDK_NOTIFY_INFERIOR) &&
1792 (gtk_widget_get_sensitive (widget)))
1794 priv->in_button = FALSE;
1795 gtk_button_leave (button);
1802 gtk_real_button_pressed (GtkButton *button)
1804 GtkButtonPrivate *priv = button->priv;
1806 if (priv->activate_timeout)
1809 priv->button_down = TRUE;
1810 gtk_button_update_state (button);
1814 gtk_real_button_released (GtkButton *button)
1816 GtkButtonPrivate *priv = button->priv;
1818 if (priv->button_down)
1820 priv->button_down = FALSE;
1822 if (priv->activate_timeout)
1825 if (priv->in_button)
1826 gtk_button_clicked (button);
1828 gtk_button_update_state (button);
1833 gtk_real_button_clicked (GtkButton *button)
1835 GtkButtonPrivate *priv = button->priv;
1838 gtk_action_activate (priv->action);
1842 button_activate_timeout (gpointer data)
1844 gtk_button_finish_activate (data, TRUE);
1850 gtk_real_button_activate (GtkButton *button)
1852 GtkWidget *widget = GTK_WIDGET (button);
1853 GtkButtonPrivate *priv = button->priv;
1857 device = gtk_get_current_event_device ();
1859 if (device && device->source != GDK_SOURCE_KEYBOARD)
1860 device = gdk_device_get_associated_device (device);
1862 g_return_if_fail (device && device->source == GDK_SOURCE_KEYBOARD);
1864 if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
1866 time = gtk_get_current_event_time ();
1868 if (gdk_device_grab (device, priv->event_window,
1869 GDK_OWNERSHIP_WINDOW, TRUE,
1870 GDK_KEY_PRESS | GDK_KEY_RELEASE,
1871 NULL, time) == GDK_GRAB_SUCCESS)
1873 gtk_device_grab_add (widget, device, TRUE);
1874 priv->grab_keyboard = device;
1875 priv->grab_time = time;
1878 priv->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1879 button_activate_timeout,
1881 priv->button_down = TRUE;
1882 gtk_button_update_state (button);
1883 gtk_widget_queue_draw (GTK_WIDGET (button));
1888 gtk_button_finish_activate (GtkButton *button,
1891 GtkWidget *widget = GTK_WIDGET (button);
1892 GtkButtonPrivate *priv = button->priv;
1894 g_source_remove (priv->activate_timeout);
1895 priv->activate_timeout = 0;
1897 if (priv->grab_keyboard)
1899 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1900 gtk_device_grab_remove (widget, priv->grab_keyboard);
1901 priv->grab_keyboard = NULL;
1904 priv->button_down = FALSE;
1906 gtk_button_update_state (button);
1907 gtk_widget_queue_draw (GTK_WIDGET (button));
1910 gtk_button_clicked (button);
1915 gtk_button_get_size (GtkWidget *widget,
1916 GtkOrientation orientation,
1920 GtkButton *button = GTK_BUTTON (widget);
1923 GtkBorder default_border;
1924 GtkBorder inner_border;
1927 gint minimum, natural;
1929 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1930 gtk_widget_style_get (GTK_WIDGET (widget),
1931 "focus-line-width", &focus_width,
1932 "focus-padding", &focus_pad,
1935 style = gtk_widget_get_style (GTK_WIDGET (widget));
1937 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1939 minimum = (style->xthickness * 2 +
1940 inner_border.left + inner_border.right);
1942 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1943 minimum += default_border.left + default_border.right;
1947 minimum = (style->ythickness * 2 +
1948 inner_border.top + inner_border.bottom);
1950 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1951 minimum += default_border.top + default_border.bottom;
1954 minimum += 2 * (focus_width + focus_pad);
1957 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1958 gtk_widget_get_visible (child))
1960 gint child_min, child_nat;
1962 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1963 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
1965 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1967 minimum += child_min;
1968 natural += child_nat;
1972 *minimum_size = minimum;
1975 *natural_size = natural;
1979 gtk_button_get_preferred_width (GtkWidget *widget,
1983 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1987 gtk_button_get_preferred_height (GtkWidget *widget,
1991 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1995 * gtk_button_set_label:
1996 * @button: a #GtkButton
1999 * Sets the text of the label of the button to @str. This text is
2000 * also used to select the stock item if gtk_button_set_use_stock()
2003 * This will also clear any previously set labels.
2006 gtk_button_set_label (GtkButton *button,
2009 GtkButtonPrivate *priv;
2012 g_return_if_fail (GTK_IS_BUTTON (button));
2014 priv = button->priv;
2016 new_label = g_strdup (label);
2017 g_free (priv->label_text);
2018 priv->label_text = new_label;
2020 gtk_button_construct_child (button);
2022 g_object_notify (G_OBJECT (button), "label");
2026 * gtk_button_get_label:
2027 * @button: a #GtkButton
2029 * Fetches the text from the label of the button, as set by
2030 * gtk_button_set_label(). If the label text has not
2031 * been set the return value will be %NULL. This will be the
2032 * case if you create an empty button with gtk_button_new() to
2033 * use as a container.
2035 * Return value: The text of the label widget. This string is owned
2036 * by the widget and must not be modified or freed.
2038 G_CONST_RETURN gchar *
2039 gtk_button_get_label (GtkButton *button)
2041 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2043 return button->priv->label_text;
2047 * gtk_button_set_use_underline:
2048 * @button: a #GtkButton
2049 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2051 * If true, an underline in the text of the button label indicates
2052 * the next character should be used for the mnemonic accelerator key.
2055 gtk_button_set_use_underline (GtkButton *button,
2056 gboolean use_underline)
2058 GtkButtonPrivate *priv;
2060 g_return_if_fail (GTK_IS_BUTTON (button));
2062 priv = button->priv;
2064 use_underline = use_underline != FALSE;
2066 if (use_underline != priv->use_underline)
2068 priv->use_underline = use_underline;
2070 gtk_button_construct_child (button);
2072 g_object_notify (G_OBJECT (button), "use-underline");
2077 * gtk_button_get_use_underline:
2078 * @button: a #GtkButton
2080 * Returns whether an embedded underline in the button label indicates a
2081 * mnemonic. See gtk_button_set_use_underline ().
2083 * Return value: %TRUE if an embedded underline in the button label
2084 * indicates the mnemonic accelerator keys.
2087 gtk_button_get_use_underline (GtkButton *button)
2089 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2091 return button->priv->use_underline;
2095 * gtk_button_set_use_stock:
2096 * @button: a #GtkButton
2097 * @use_stock: %TRUE if the button should use a stock item
2099 * If %TRUE, the label set on the button is used as a
2100 * stock id to select the stock item for the button.
2103 gtk_button_set_use_stock (GtkButton *button,
2106 GtkButtonPrivate *priv;
2108 g_return_if_fail (GTK_IS_BUTTON (button));
2110 priv = button->priv;
2112 use_stock = use_stock != FALSE;
2114 if (use_stock != priv->use_stock)
2116 priv->use_stock = use_stock;
2118 gtk_button_construct_child (button);
2120 g_object_notify (G_OBJECT (button), "use-stock");
2125 * gtk_button_get_use_stock:
2126 * @button: a #GtkButton
2128 * Returns whether the button label is a stock item.
2130 * Return value: %TRUE if the button label is used to
2131 * select a stock item instead of being
2132 * used directly as the label text.
2135 gtk_button_get_use_stock (GtkButton *button)
2137 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2139 return button->priv->use_stock;
2143 * gtk_button_set_focus_on_click:
2144 * @button: a #GtkButton
2145 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2147 * Sets whether the button will grab focus when it is clicked with the mouse.
2148 * Making mouse clicks not grab focus is useful in places like toolbars where
2149 * you don't want the keyboard focus removed from the main area of the
2155 gtk_button_set_focus_on_click (GtkButton *button,
2156 gboolean focus_on_click)
2158 GtkButtonPrivate *priv;
2160 g_return_if_fail (GTK_IS_BUTTON (button));
2162 priv = button->priv;
2164 focus_on_click = focus_on_click != FALSE;
2166 if (priv->focus_on_click != focus_on_click)
2168 priv->focus_on_click = focus_on_click;
2170 g_object_notify (G_OBJECT (button), "focus-on-click");
2175 * gtk_button_get_focus_on_click:
2176 * @button: a #GtkButton
2178 * Returns whether the button grabs focus when it is clicked with the mouse.
2179 * See gtk_button_set_focus_on_click().
2181 * Return value: %TRUE if the button grabs focus when it is clicked with
2187 gtk_button_get_focus_on_click (GtkButton *button)
2189 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2191 return button->priv->focus_on_click;
2195 * gtk_button_set_alignment:
2196 * @button: a #GtkButton
2197 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2198 * 1.0 is right aligned
2199 * @yalign: the vertical position of the child, 0.0 is top aligned,
2200 * 1.0 is bottom aligned
2202 * Sets the alignment of the child. This property has no effect unless
2203 * the child is a #GtkMisc or a #GtkAlignment.
2208 gtk_button_set_alignment (GtkButton *button,
2212 GtkButtonPrivate *priv;
2214 g_return_if_fail (GTK_IS_BUTTON (button));
2216 priv = button->priv;
2218 priv->xalign = xalign;
2219 priv->yalign = yalign;
2220 priv->align_set = 1;
2222 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2224 g_object_freeze_notify (G_OBJECT (button));
2225 g_object_notify (G_OBJECT (button), "xalign");
2226 g_object_notify (G_OBJECT (button), "yalign");
2227 g_object_thaw_notify (G_OBJECT (button));
2231 * gtk_button_get_alignment:
2232 * @button: a #GtkButton
2233 * @xalign: return location for horizontal alignment
2234 * @yalign: return location for vertical alignment
2236 * Gets the alignment of the child in the button.
2241 gtk_button_get_alignment (GtkButton *button,
2245 GtkButtonPrivate *priv;
2247 g_return_if_fail (GTK_IS_BUTTON (button));
2249 priv = button->priv;
2252 *xalign = priv->xalign;
2255 *yalign = priv->yalign;
2259 * _gtk_button_set_depressed:
2260 * @button: a #GtkButton
2261 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2263 * Sets whether the button is currently drawn as down or not. This is
2264 * purely a visual setting, and is meant only for use by derived widgets
2265 * such as #GtkToggleButton.
2268 _gtk_button_set_depressed (GtkButton *button,
2271 GtkWidget *widget = GTK_WIDGET (button);
2272 GtkButtonPrivate *priv = button->priv;
2274 depressed = depressed != FALSE;
2276 if (depressed != priv->depressed)
2278 priv->depressed = depressed;
2279 gtk_widget_queue_resize (widget);
2284 gtk_button_update_state (GtkButton *button)
2286 GtkButtonPrivate *priv = button->priv;
2287 GtkStateType new_state;
2290 if (priv->activate_timeout)
2291 depressed = priv->depress_on_activate;
2293 depressed = priv->in_button && priv->button_down;
2295 if (priv->in_button && (!priv->button_down || !depressed))
2296 new_state = GTK_STATE_PRELIGHT;
2298 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2300 _gtk_button_set_depressed (button, depressed);
2301 gtk_widget_set_state (GTK_WIDGET (button), new_state);
2305 show_image_change_notify (GtkButton *button)
2307 GtkButtonPrivate *priv = button->priv;
2311 if (show_image (button))
2312 gtk_widget_show (priv->image);
2314 gtk_widget_hide (priv->image);
2319 traverse_container (GtkWidget *widget,
2322 if (GTK_IS_BUTTON (widget))
2323 show_image_change_notify (GTK_BUTTON (widget));
2324 else if (GTK_IS_CONTAINER (widget))
2325 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2329 gtk_button_setting_changed (GtkSettings *settings)
2333 list = gtk_window_list_toplevels ();
2335 for (l = list; l; l = l->next)
2336 gtk_container_forall (GTK_CONTAINER (l->data),
2337 traverse_container, NULL);
2344 gtk_button_screen_changed (GtkWidget *widget,
2345 GdkScreen *previous_screen)
2348 GtkButtonPrivate *priv;
2349 GtkSettings *settings;
2350 gulong show_image_connection;
2352 if (!gtk_widget_has_screen (widget))
2355 button = GTK_BUTTON (widget);
2356 priv = button->priv;
2358 /* If the button is being pressed while the screen changes the
2359 release might never occur, so we reset the state. */
2360 if (priv->button_down)
2362 priv->button_down = FALSE;
2363 gtk_button_update_state (button);
2366 settings = gtk_widget_get_settings (widget);
2368 show_image_connection =
2369 g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2370 NULL, gtk_button_setting_changed, NULL);
2372 if (show_image_connection)
2375 g_signal_connect (settings, "notify::gtk-button-images",
2376 G_CALLBACK (gtk_button_setting_changed), NULL);
2378 show_image_change_notify (button);
2382 gtk_button_state_changed (GtkWidget *widget,
2383 GtkStateType previous_state)
2385 GtkButton *button = GTK_BUTTON (widget);
2386 GtkButtonPrivate *priv = button->priv;
2388 if (!gtk_widget_is_sensitive (widget))
2390 priv->in_button = FALSE;
2391 gtk_real_button_released (button);
2396 gtk_button_grab_notify (GtkWidget *widget,
2397 gboolean was_grabbed)
2399 GtkButton *button = GTK_BUTTON (widget);
2400 GtkButtonPrivate *priv = button->priv;
2403 if (priv->activate_timeout &&
2404 priv->grab_keyboard &&
2405 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2406 gtk_button_finish_activate (button, FALSE);
2410 save_in = priv->in_button;
2411 priv->in_button = FALSE;
2412 gtk_real_button_released (button);
2413 if (save_in != priv->in_button)
2415 priv->in_button = save_in;
2416 gtk_button_update_state (button);
2422 * gtk_button_set_image:
2423 * @button: a #GtkButton
2424 * @image: a widget to set as the image for the button
2426 * Set the image of @button to the given widget. Note that
2427 * it depends on the #GtkSettings:gtk-button-images setting whether the
2428 * image will be displayed or not, you don't have to call
2429 * gtk_widget_show() on @image yourself.
2434 gtk_button_set_image (GtkButton *button,
2437 GtkButtonPrivate *priv;
2440 g_return_if_fail (GTK_IS_BUTTON (button));
2441 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2443 priv = button->priv;
2447 parent = gtk_widget_get_parent (priv->image);
2449 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2452 priv->image = image;
2453 priv->image_is_stock = (image == NULL);
2455 gtk_button_construct_child (button);
2457 g_object_notify (G_OBJECT (button), "image");
2461 * gtk_button_get_image:
2462 * @button: a #GtkButton
2464 * Gets the widget that is currenty set as the image of @button.
2465 * This may have been explicitly set by gtk_button_set_image()
2466 * or constructed by gtk_button_new_from_stock().
2468 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2473 gtk_button_get_image (GtkButton *button)
2475 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2477 return button->priv->image;
2481 * gtk_button_set_image_position:
2482 * @button: a #GtkButton
2483 * @position: the position
2485 * Sets the position of the image relative to the text
2486 * inside the button.
2491 gtk_button_set_image_position (GtkButton *button,
2492 GtkPositionType position)
2494 GtkButtonPrivate *priv;
2496 g_return_if_fail (GTK_IS_BUTTON (button));
2497 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2499 priv = button->priv;
2501 if (priv->image_position != position)
2503 priv->image_position = position;
2505 gtk_button_construct_child (button);
2507 g_object_notify (G_OBJECT (button), "image-position");
2512 * gtk_button_get_image_position:
2513 * @button: a #GtkButton
2515 * Gets the position of the image relative to the text
2516 * inside the button.
2518 * Return value: the position
2523 gtk_button_get_image_position (GtkButton *button)
2525 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2527 return button->priv->image_position;
2532 * gtk_button_get_event_window:
2533 * @button: a #GtkButton
2535 * Returns the button's event window if it is realized, %NULL otherwise.
2536 * This function should be rarely needed.
2538 * Return value: (transfer none): @button's event window.
2543 gtk_button_get_event_window (GtkButton *button)
2545 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2547 return button->priv->event_window;