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"
61 #include "a11y/gtkbuttonaccessible.h"
64 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
65 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
66 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
68 /* Time out before giving up on getting a key release when animating
71 #define ACTIVATE_TIMEOUT 250
96 /* activatable properties */
97 PROP_ACTIVATABLE_RELATED_ACTION,
98 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
102 static void gtk_button_destroy (GtkWidget *widget);
103 static void gtk_button_dispose (GObject *object);
104 static void gtk_button_set_property (GObject *object,
108 static void gtk_button_get_property (GObject *object,
112 static void gtk_button_screen_changed (GtkWidget *widget,
113 GdkScreen *previous_screen);
114 static void gtk_button_realize (GtkWidget * widget);
115 static void gtk_button_unrealize (GtkWidget * widget);
116 static void gtk_button_map (GtkWidget * widget);
117 static void gtk_button_unmap (GtkWidget * widget);
118 static void gtk_button_style_updated (GtkWidget * widget);
119 static void gtk_button_size_allocate (GtkWidget * widget,
120 GtkAllocation * allocation);
121 static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
122 static gint gtk_button_button_press (GtkWidget * widget,
123 GdkEventButton * event);
124 static gint gtk_button_button_release (GtkWidget * widget,
125 GdkEventButton * event);
126 static gint gtk_button_grab_broken (GtkWidget * widget,
127 GdkEventGrabBroken * event);
128 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
129 static gint gtk_button_enter_notify (GtkWidget * widget,
130 GdkEventCrossing * event);
131 static gint gtk_button_leave_notify (GtkWidget * widget,
132 GdkEventCrossing * event);
133 static void gtk_real_button_pressed (GtkButton * button);
134 static void gtk_real_button_released (GtkButton * button);
135 static void gtk_real_button_clicked (GtkButton * button);
136 static void gtk_real_button_activate (GtkButton *button);
137 static void gtk_button_update_state (GtkButton *button);
138 static void gtk_button_add (GtkContainer *container,
140 static GType gtk_button_child_type (GtkContainer *container);
141 static void gtk_button_finish_activate (GtkButton *button,
144 static GObject* gtk_button_constructor (GType type,
145 guint n_construct_properties,
146 GObjectConstructParam *construct_params);
147 static void gtk_button_construct_child (GtkButton *button);
148 static void gtk_button_state_changed (GtkWidget *widget,
149 GtkStateType previous_state);
150 static void gtk_button_grab_notify (GtkWidget *widget,
151 gboolean was_grabbed);
154 static void gtk_button_activatable_interface_init(GtkActivatableIface *iface);
155 static void gtk_button_update (GtkActivatable *activatable,
157 const gchar *property_name);
158 static void gtk_button_sync_action_properties (GtkActivatable *activatable,
160 static void gtk_button_set_related_action (GtkButton *button,
162 static void gtk_button_set_use_action_appearance (GtkButton *button,
163 gboolean use_appearance);
165 static void gtk_button_get_preferred_width (GtkWidget *widget,
168 static void gtk_button_get_preferred_height (GtkWidget *widget,
172 static guint button_signals[LAST_SIGNAL] = { 0 };
174 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
175 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
176 gtk_button_activatable_interface_init))
179 gtk_button_class_init (GtkButtonClass *klass)
181 GObjectClass *gobject_class;
182 GtkWidgetClass *widget_class;
183 GtkContainerClass *container_class;
185 gobject_class = G_OBJECT_CLASS (klass);
186 widget_class = (GtkWidgetClass*) klass;
187 container_class = (GtkContainerClass*) klass;
189 gobject_class->constructor = gtk_button_constructor;
190 gobject_class->dispose = gtk_button_dispose;
191 gobject_class->set_property = gtk_button_set_property;
192 gobject_class->get_property = gtk_button_get_property;
194 widget_class->get_preferred_width = gtk_button_get_preferred_width;
195 widget_class->get_preferred_height = gtk_button_get_preferred_height;
196 widget_class->destroy = gtk_button_destroy;
197 widget_class->screen_changed = gtk_button_screen_changed;
198 widget_class->realize = gtk_button_realize;
199 widget_class->unrealize = gtk_button_unrealize;
200 widget_class->map = gtk_button_map;
201 widget_class->unmap = gtk_button_unmap;
202 widget_class->style_updated = gtk_button_style_updated;
203 widget_class->size_allocate = gtk_button_size_allocate;
204 widget_class->draw = gtk_button_draw;
205 widget_class->button_press_event = gtk_button_button_press;
206 widget_class->button_release_event = gtk_button_button_release;
207 widget_class->grab_broken_event = gtk_button_grab_broken;
208 widget_class->key_release_event = gtk_button_key_release;
209 widget_class->enter_notify_event = gtk_button_enter_notify;
210 widget_class->leave_notify_event = gtk_button_leave_notify;
211 widget_class->state_changed = gtk_button_state_changed;
212 widget_class->grab_notify = gtk_button_grab_notify;
214 container_class->child_type = gtk_button_child_type;
215 container_class->add = gtk_button_add;
216 gtk_container_class_handle_border_width (container_class);
218 klass->pressed = gtk_real_button_pressed;
219 klass->released = gtk_real_button_released;
220 klass->clicked = NULL;
221 klass->enter = gtk_button_update_state;
222 klass->leave = gtk_button_update_state;
223 klass->activate = gtk_real_button_activate;
225 g_object_class_install_property (gobject_class,
227 g_param_spec_string ("label",
229 P_("Text of the label widget inside the button, if the button contains a label widget"),
231 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
233 g_object_class_install_property (gobject_class,
235 g_param_spec_boolean ("use-underline",
237 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
239 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
241 g_object_class_install_property (gobject_class,
243 g_param_spec_boolean ("use-stock",
245 P_("If set, the label is used to pick a stock item instead of being displayed"),
247 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
249 g_object_class_install_property (gobject_class,
251 g_param_spec_boolean ("focus-on-click",
252 P_("Focus on click"),
253 P_("Whether the button grabs focus when it is clicked with the mouse"),
255 GTK_PARAM_READWRITE));
257 g_object_class_install_property (gobject_class,
259 g_param_spec_enum ("relief",
261 P_("The border relief style"),
262 GTK_TYPE_RELIEF_STYLE,
264 GTK_PARAM_READWRITE));
269 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
270 * can be used to control its horizontal alignment. 0.0 is left aligned,
271 * 1.0 is right aligned.
275 g_object_class_install_property (gobject_class,
277 g_param_spec_float("xalign",
278 P_("Horizontal alignment for child"),
279 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
283 GTK_PARAM_READWRITE));
288 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
289 * can be used to control its vertical alignment. 0.0 is top aligned,
290 * 1.0 is bottom aligned.
294 g_object_class_install_property (gobject_class,
296 g_param_spec_float("yalign",
297 P_("Vertical alignment for child"),
298 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
302 GTK_PARAM_READWRITE));
307 * The child widget to appear next to the button text.
311 g_object_class_install_property (gobject_class,
313 g_param_spec_object ("image",
315 P_("Child widget to appear next to the button text"),
317 GTK_PARAM_READWRITE));
320 * GtkButton:image-position:
322 * The position of the image relative to the text inside the button.
326 g_object_class_install_property (gobject_class,
328 g_param_spec_enum ("image-position",
329 P_("Image position"),
330 P_("The position of the image relative to the text"),
331 GTK_TYPE_POSITION_TYPE,
333 GTK_PARAM_READWRITE));
335 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
336 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
339 * GtkButton::pressed:
340 * @button: the object that received the signal
342 * Emitted when the button is pressed.
344 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
346 button_signals[PRESSED] =
347 g_signal_new (I_("pressed"),
348 G_OBJECT_CLASS_TYPE (gobject_class),
350 G_STRUCT_OFFSET (GtkButtonClass, pressed),
352 _gtk_marshal_VOID__VOID,
356 * GtkButton::released:
357 * @button: the object that received the signal
359 * Emitted when the button is released.
361 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
363 button_signals[RELEASED] =
364 g_signal_new (I_("released"),
365 G_OBJECT_CLASS_TYPE (gobject_class),
367 G_STRUCT_OFFSET (GtkButtonClass, released),
369 _gtk_marshal_VOID__VOID,
373 * GtkButton::clicked:
374 * @button: the object that received the signal
376 * Emitted when the button has been activated (pressed and released).
378 button_signals[CLICKED] =
379 g_signal_new (I_("clicked"),
380 G_OBJECT_CLASS_TYPE (gobject_class),
381 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
382 G_STRUCT_OFFSET (GtkButtonClass, clicked),
384 _gtk_marshal_VOID__VOID,
389 * @button: the object that received the signal
391 * Emitted when the pointer enters the button.
393 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
395 button_signals[ENTER] =
396 g_signal_new (I_("enter"),
397 G_OBJECT_CLASS_TYPE (gobject_class),
399 G_STRUCT_OFFSET (GtkButtonClass, enter),
401 _gtk_marshal_VOID__VOID,
406 * @button: the object that received the signal
408 * Emitted when the pointer leaves the button.
410 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
412 button_signals[LEAVE] =
413 g_signal_new (I_("leave"),
414 G_OBJECT_CLASS_TYPE (gobject_class),
416 G_STRUCT_OFFSET (GtkButtonClass, leave),
418 _gtk_marshal_VOID__VOID,
422 * GtkButton::activate:
423 * @widget: the object which received the signal.
425 * The ::activate signal on GtkButton is an action signal and
426 * emitting it causes the button to animate press then release.
427 * Applications should never connect to this signal, but use the
428 * #GtkButton::clicked signal.
430 button_signals[ACTIVATE] =
431 g_signal_new (I_("activate"),
432 G_OBJECT_CLASS_TYPE (gobject_class),
433 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
434 G_STRUCT_OFFSET (GtkButtonClass, activate),
436 _gtk_marshal_VOID__VOID,
438 widget_class->activate_signal = button_signals[ACTIVATE];
441 * GtkButton:default-border:
443 * The "default-border" style property defines the extra space to add
444 * around a button that can become the default widget of its window.
445 * For more information about default widgets, see gtk_widget_grab_default().
448 gtk_widget_class_install_style_property (widget_class,
449 g_param_spec_boxed ("default-border",
450 P_("Default Spacing"),
451 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
453 GTK_PARAM_READABLE));
456 * GtkButton:default-outside-border:
458 * The "default-outside-border" style property defines the extra outside
459 * space to add around a button that can become the default widget of its
460 * window. Extra outside space is always drawn outside the button border.
461 * For more information about default widgets, see gtk_widget_grab_default().
463 gtk_widget_class_install_style_property (widget_class,
464 g_param_spec_boxed ("default-outside-border",
465 P_("Default Outside Spacing"),
466 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
468 GTK_PARAM_READABLE));
469 gtk_widget_class_install_style_property (widget_class,
470 g_param_spec_int ("child-displacement-x",
471 P_("Child X Displacement"),
472 P_("How far in the x direction to move the child when the button is depressed"),
476 GTK_PARAM_READABLE));
477 gtk_widget_class_install_style_property (widget_class,
478 g_param_spec_int ("child-displacement-y",
479 P_("Child Y Displacement"),
480 P_("How far in the y direction to move the child when the button is depressed"),
484 GTK_PARAM_READABLE));
487 * GtkButton:displace-focus:
489 * Whether the child_displacement_x/child_displacement_y properties
490 * should also affect the focus rectangle.
494 gtk_widget_class_install_style_property (widget_class,
495 g_param_spec_boolean ("displace-focus",
496 P_("Displace focus"),
497 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
499 GTK_PARAM_READABLE));
502 * GtkButton:inner-border:
504 * Sets the border between the button edges and child.
508 gtk_widget_class_install_style_property (widget_class,
509 g_param_spec_boxed ("inner-border",
511 P_("Border between button edges and child."),
513 GTK_PARAM_READABLE));
516 * GtkButton::image-spacing:
518 * Spacing in pixels between the image and label.
522 gtk_widget_class_install_style_property (widget_class,
523 g_param_spec_int ("image-spacing",
525 P_("Spacing in pixels between the image and label"),
529 GTK_PARAM_READABLE));
531 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
533 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_BUTTON_ACCESSIBLE);
537 gtk_button_init (GtkButton *button)
539 GtkButtonPrivate *priv;
540 GtkStyleContext *context;
542 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
547 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
548 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
549 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
551 priv->label_text = NULL;
553 priv->constructed = FALSE;
554 priv->in_button = FALSE;
555 priv->button_down = FALSE;
556 priv->relief = GTK_RELIEF_NORMAL;
557 priv->use_stock = FALSE;
558 priv->use_underline = FALSE;
559 priv->depressed = FALSE;
560 priv->depress_on_activate = TRUE;
561 priv->focus_on_click = TRUE;
566 priv->image_is_stock = TRUE;
567 priv->image_position = GTK_POS_LEFT;
568 priv->use_action_appearance = TRUE;
570 context = gtk_widget_get_style_context (GTK_WIDGET (button));
571 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
575 gtk_button_destroy (GtkWidget *widget)
577 GtkButton *button = GTK_BUTTON (widget);
578 GtkButtonPrivate *priv = button->priv;
580 if (priv->label_text)
582 g_free (priv->label_text);
583 priv->label_text = NULL;
586 GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
590 gtk_button_constructor (GType type,
591 guint n_construct_properties,
592 GObjectConstructParam *construct_params)
596 GtkButtonPrivate *priv;
598 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
599 n_construct_properties,
602 button = GTK_BUTTON (object);
605 priv->constructed = TRUE;
607 if (priv->label_text != NULL)
608 gtk_button_construct_child (button);
615 gtk_button_child_type (GtkContainer *container)
617 if (!gtk_bin_get_child (GTK_BIN (container)))
618 return GTK_TYPE_WIDGET;
624 maybe_set_alignment (GtkButton *button,
627 GtkButtonPrivate *priv = button->priv;
629 if (GTK_IS_MISC (widget))
631 GtkMisc *misc = GTK_MISC (widget);
634 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
636 else if (GTK_IS_ALIGNMENT (widget))
638 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
639 gfloat xscale, yscale;
641 g_object_get (alignment,
647 gtk_alignment_set (alignment,
648 priv->xalign, priv->yalign,
654 gtk_button_add (GtkContainer *container,
657 maybe_set_alignment (GTK_BUTTON (container), widget);
659 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
663 gtk_button_dispose (GObject *object)
665 GtkButton *button = GTK_BUTTON (object);
666 GtkButtonPrivate *priv = button->priv;
670 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
673 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
677 gtk_button_set_property (GObject *object,
682 GtkButton *button = GTK_BUTTON (object);
683 GtkButtonPrivate *priv = button->priv;
688 gtk_button_set_label (button, g_value_get_string (value));
691 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
694 gtk_button_set_relief (button, g_value_get_enum (value));
696 case PROP_USE_UNDERLINE:
697 gtk_button_set_use_underline (button, g_value_get_boolean (value));
700 gtk_button_set_use_stock (button, g_value_get_boolean (value));
702 case PROP_FOCUS_ON_CLICK:
703 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
706 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
709 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
711 case PROP_IMAGE_POSITION:
712 gtk_button_set_image_position (button, g_value_get_enum (value));
714 case PROP_ACTIVATABLE_RELATED_ACTION:
715 gtk_button_set_related_action (button, g_value_get_object (value));
717 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
718 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
721 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
727 gtk_button_get_property (GObject *object,
732 GtkButton *button = GTK_BUTTON (object);
733 GtkButtonPrivate *priv = button->priv;
738 g_value_set_string (value, priv->label_text);
741 g_value_set_object (value, (GObject *)priv->image);
744 g_value_set_enum (value, priv->relief);
746 case PROP_USE_UNDERLINE:
747 g_value_set_boolean (value, priv->use_underline);
750 g_value_set_boolean (value, priv->use_stock);
752 case PROP_FOCUS_ON_CLICK:
753 g_value_set_boolean (value, priv->focus_on_click);
756 g_value_set_float (value, priv->xalign);
759 g_value_set_float (value, priv->yalign);
761 case PROP_IMAGE_POSITION:
762 g_value_set_enum (value, priv->image_position);
764 case PROP_ACTIVATABLE_RELATED_ACTION:
765 g_value_set_object (value, priv->action);
767 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
768 g_value_set_boolean (value, priv->use_action_appearance);
771 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
777 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
779 iface->update = gtk_button_update;
780 iface->sync_action_properties = gtk_button_sync_action_properties;
784 activatable_update_stock_id (GtkButton *button,
787 if (!gtk_button_get_use_stock (button))
790 gtk_button_set_label (button, gtk_action_get_stock_id (action));
794 activatable_update_short_label (GtkButton *button,
800 if (gtk_button_get_use_stock (button))
803 image = gtk_button_get_image (button);
805 /* Dont touch custom child... */
806 child = gtk_bin_get_child (GTK_BIN (button));
807 if (GTK_IS_IMAGE (image) ||
809 GTK_IS_LABEL (child))
811 gtk_button_set_label (button, gtk_action_get_short_label (action));
812 gtk_button_set_use_underline (button, TRUE);
817 activatable_update_icon_name (GtkButton *button,
822 if (gtk_button_get_use_stock (button))
825 image = gtk_button_get_image (button);
827 if (GTK_IS_IMAGE (image) &&
828 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
829 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
830 gtk_image_set_from_icon_name (GTK_IMAGE (image),
831 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
835 activatable_update_gicon (GtkButton *button,
838 GtkWidget *image = gtk_button_get_image (button);
839 GIcon *icon = gtk_action_get_gicon (action);
841 if (GTK_IS_IMAGE (image) &&
842 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
843 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
844 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
848 gtk_button_update (GtkActivatable *activatable,
850 const gchar *property_name)
852 GtkButton *button = GTK_BUTTON (activatable);
853 GtkButtonPrivate *priv = button->priv;
855 if (strcmp (property_name, "visible") == 0)
857 if (gtk_action_is_visible (action))
858 gtk_widget_show (GTK_WIDGET (activatable));
860 gtk_widget_hide (GTK_WIDGET (activatable));
862 else if (strcmp (property_name, "sensitive") == 0)
863 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
865 if (!priv->use_action_appearance)
868 if (strcmp (property_name, "stock-id") == 0)
869 activatable_update_stock_id (GTK_BUTTON (activatable), action);
870 else if (strcmp (property_name, "gicon") == 0)
871 activatable_update_gicon (GTK_BUTTON (activatable), action);
872 else if (strcmp (property_name, "short-label") == 0)
873 activatable_update_short_label (GTK_BUTTON (activatable), action);
874 else if (strcmp (property_name, "icon-name") == 0)
875 activatable_update_icon_name (GTK_BUTTON (activatable), action);
879 gtk_button_sync_action_properties (GtkActivatable *activatable,
882 GtkButton *button = GTK_BUTTON (activatable);
883 GtkButtonPrivate *priv = button->priv;
888 if (gtk_action_is_visible (action))
889 gtk_widget_show (GTK_WIDGET (activatable));
891 gtk_widget_hide (GTK_WIDGET (activatable));
893 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
895 if (priv->use_action_appearance)
897 activatable_update_stock_id (GTK_BUTTON (activatable), action);
898 activatable_update_short_label (GTK_BUTTON (activatable), action);
899 activatable_update_gicon (GTK_BUTTON (activatable), action);
900 activatable_update_icon_name (GTK_BUTTON (activatable), action);
905 gtk_button_set_related_action (GtkButton *button,
908 GtkButtonPrivate *priv = button->priv;
910 if (priv->action == action)
913 /* This should be a default handler, but for compatibility reasons
914 * we need to support derived classes that don't chain up their
917 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
919 g_signal_connect_after (button, "clicked",
920 G_CALLBACK (gtk_real_button_clicked), NULL);
922 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
924 priv->action = action;
928 gtk_button_set_use_action_appearance (GtkButton *button,
929 gboolean use_appearance)
931 GtkButtonPrivate *priv = button->priv;
933 if (priv->use_action_appearance != use_appearance)
935 priv->use_action_appearance = use_appearance;
937 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
944 * Creates a new #GtkButton widget. To add a child widget to the button,
945 * use gtk_container_add().
947 * Returns: The newly created #GtkButton widget.
950 gtk_button_new (void)
952 return g_object_new (GTK_TYPE_BUTTON, NULL);
956 show_image (GtkButton *button)
958 GtkButtonPrivate *priv = button->priv;
961 if (priv->label_text)
963 GtkSettings *settings;
965 settings = gtk_widget_get_settings (GTK_WIDGET (button));
966 g_object_get (settings, "gtk-button-images", &show, NULL);
975 gtk_button_construct_child (GtkButton *button)
977 GtkButtonPrivate *priv = button->priv;
978 GtkStyleContext *context;
984 GtkWidget *image = NULL;
985 gchar *label_text = NULL;
988 if (!priv->constructed)
991 if (!priv->label_text && !priv->image)
994 context = gtk_widget_get_style_context (GTK_WIDGET (button));
996 gtk_style_context_get_style (context,
997 "image-spacing", &image_spacing,
1000 if (priv->image && !priv->image_is_stock)
1004 image = g_object_ref (priv->image);
1006 parent = gtk_widget_get_parent (image);
1008 gtk_container_remove (GTK_CONTAINER (parent), image);
1013 child = gtk_bin_get_child (GTK_BIN (button));
1015 gtk_container_remove (GTK_CONTAINER (button), child);
1017 if (priv->use_stock &&
1019 gtk_stock_lookup (priv->label_text, &item))
1022 image = g_object_ref (gtk_image_new_from_stock (priv->label_text, GTK_ICON_SIZE_BUTTON));
1024 label_text = item.label;
1027 label_text = priv->label_text;
1031 priv->image = image;
1032 g_object_set (priv->image,
1033 "visible", show_image (button),
1034 "no-show-all", TRUE,
1037 if (priv->image_position == GTK_POS_LEFT ||
1038 priv->image_position == GTK_POS_RIGHT)
1039 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1041 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1043 if (priv->align_set)
1044 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1046 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1048 if (priv->image_position == GTK_POS_LEFT ||
1049 priv->image_position == GTK_POS_TOP)
1050 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1052 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1056 if (priv->use_underline || priv->use_stock)
1058 label = gtk_label_new_with_mnemonic (label_text);
1059 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1060 GTK_WIDGET (button));
1063 label = gtk_label_new (label_text);
1065 if (priv->image_position == GTK_POS_RIGHT ||
1066 priv->image_position == GTK_POS_BOTTOM)
1067 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1069 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1072 gtk_container_add (GTK_CONTAINER (button), align);
1073 gtk_container_add (GTK_CONTAINER (align), box);
1074 gtk_widget_show_all (align);
1076 g_object_unref (image);
1081 if (priv->use_underline || priv->use_stock)
1083 label = gtk_label_new_with_mnemonic (priv->label_text);
1084 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1087 label = gtk_label_new (priv->label_text);
1089 if (priv->align_set)
1090 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1092 gtk_widget_show (label);
1093 gtk_container_add (GTK_CONTAINER (button), label);
1098 * gtk_button_new_with_label:
1099 * @label: The text you want the #GtkLabel to hold.
1101 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1104 * Returns: The newly created #GtkButton widget.
1107 gtk_button_new_with_label (const gchar *label)
1109 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1113 * gtk_button_new_from_stock:
1114 * @stock_id: the name of the stock item
1116 * Creates a new #GtkButton containing the image and text from a stock item.
1117 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1120 * If @stock_id is unknown, then it will be treated as a mnemonic
1121 * label (as for gtk_button_new_with_mnemonic()).
1123 * Returns: a new #GtkButton
1126 gtk_button_new_from_stock (const gchar *stock_id)
1128 return g_object_new (GTK_TYPE_BUTTON,
1131 "use-underline", TRUE,
1136 * gtk_button_new_with_mnemonic:
1137 * @label: The text of the button, with an underscore in front of the
1138 * mnemonic character
1139 * @returns: a new #GtkButton
1141 * Creates a new #GtkButton containing a label.
1142 * If characters in @label are preceded by an underscore, they are underlined.
1143 * If you need a literal underscore character in a label, use '__' (two
1144 * underscores). The first underlined character represents a keyboard
1145 * accelerator called a mnemonic.
1146 * Pressing Alt and that key activates the button.
1149 gtk_button_new_with_mnemonic (const gchar *label)
1151 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1155 * gtk_button_pressed:
1156 * @button: The #GtkButton you want to send the signal to.
1158 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1160 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1163 gtk_button_pressed (GtkButton *button)
1165 g_return_if_fail (GTK_IS_BUTTON (button));
1167 g_signal_emit (button, button_signals[PRESSED], 0);
1171 * gtk_button_released:
1172 * @button: The #GtkButton you want to send the signal to.
1174 * Emits a #GtkButton::released signal to the given #GtkButton.
1176 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1179 gtk_button_released (GtkButton *button)
1181 g_return_if_fail (GTK_IS_BUTTON (button));
1183 g_signal_emit (button, button_signals[RELEASED], 0);
1187 * gtk_button_clicked:
1188 * @button: The #GtkButton you want to send the signal to.
1190 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1193 gtk_button_clicked (GtkButton *button)
1195 g_return_if_fail (GTK_IS_BUTTON (button));
1197 g_signal_emit (button, button_signals[CLICKED], 0);
1202 * @button: The #GtkButton you want to send the signal to.
1204 * Emits a #GtkButton::enter signal to the given #GtkButton.
1206 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1209 gtk_button_enter (GtkButton *button)
1211 g_return_if_fail (GTK_IS_BUTTON (button));
1213 g_signal_emit (button, button_signals[ENTER], 0);
1218 * @button: The #GtkButton you want to send the signal to.
1220 * Emits a #GtkButton::leave signal to the given #GtkButton.
1222 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1225 gtk_button_leave (GtkButton *button)
1227 g_return_if_fail (GTK_IS_BUTTON (button));
1229 g_signal_emit (button, button_signals[LEAVE], 0);
1233 * gtk_button_set_relief:
1234 * @button: The #GtkButton you want to set relief styles of.
1235 * @newstyle: The GtkReliefStyle as described above.
1237 * Sets the relief style of the edges of the given #GtkButton widget.
1238 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1239 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1241 * <!-- FIXME: put pictures of each style -->
1244 gtk_button_set_relief (GtkButton *button,
1245 GtkReliefStyle newrelief)
1247 GtkButtonPrivate *priv;
1249 g_return_if_fail (GTK_IS_BUTTON (button));
1251 priv = button->priv;
1253 if (newrelief != priv->relief)
1255 priv->relief = newrelief;
1256 g_object_notify (G_OBJECT (button), "relief");
1257 gtk_widget_queue_draw (GTK_WIDGET (button));
1262 * gtk_button_get_relief:
1263 * @button: The #GtkButton you want the #GtkReliefStyle from.
1265 * Returns the current relief style of the given #GtkButton.
1267 * Returns: The current #GtkReliefStyle
1270 gtk_button_get_relief (GtkButton *button)
1272 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1274 return button->priv->relief;
1278 gtk_button_realize (GtkWidget *widget)
1280 GtkButton *button = GTK_BUTTON (widget);
1281 GtkButtonPrivate *priv = button->priv;
1282 GtkAllocation allocation;
1284 GdkWindowAttr attributes;
1285 gint attributes_mask;
1287 gtk_widget_get_allocation (widget, &allocation);
1289 gtk_widget_set_realized (widget, TRUE);
1291 attributes.window_type = GDK_WINDOW_CHILD;
1292 attributes.x = allocation.x;
1293 attributes.y = allocation.y;
1294 attributes.width = allocation.width;
1295 attributes.height = allocation.height;
1296 attributes.wclass = GDK_INPUT_ONLY;
1297 attributes.event_mask = gtk_widget_get_events (widget);
1298 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1299 GDK_BUTTON_RELEASE_MASK |
1300 GDK_ENTER_NOTIFY_MASK |
1301 GDK_LEAVE_NOTIFY_MASK);
1303 attributes_mask = GDK_WA_X | GDK_WA_Y;
1305 window = gtk_widget_get_parent_window (widget);
1306 gtk_widget_set_window (widget, window);
1307 g_object_ref (window);
1309 priv->event_window = gdk_window_new (window,
1310 &attributes, attributes_mask);
1311 gdk_window_set_user_data (priv->event_window, button);
1315 gtk_button_unrealize (GtkWidget *widget)
1317 GtkButton *button = GTK_BUTTON (widget);
1318 GtkButtonPrivate *priv = button->priv;
1320 if (priv->activate_timeout)
1321 gtk_button_finish_activate (button, FALSE);
1323 if (priv->event_window)
1325 gdk_window_set_user_data (priv->event_window, NULL);
1326 gdk_window_destroy (priv->event_window);
1327 priv->event_window = NULL;
1330 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1334 gtk_button_map (GtkWidget *widget)
1336 GtkButton *button = GTK_BUTTON (widget);
1337 GtkButtonPrivate *priv = button->priv;
1339 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1341 if (priv->event_window)
1342 gdk_window_show (priv->event_window);
1346 gtk_button_unmap (GtkWidget *widget)
1348 GtkButton *button = GTK_BUTTON (widget);
1349 GtkButtonPrivate *priv = button->priv;
1351 if (priv->event_window)
1352 gdk_window_hide (priv->event_window);
1354 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1358 gtk_button_update_image_spacing (GtkButton *button,
1359 GtkStyleContext *context)
1361 GtkButtonPrivate *priv = button->priv;
1365 /* Keep in sync with gtk_button_construct_child,
1366 * we only want to update the spacing if the box
1367 * was constructed there.
1369 if (!priv->constructed || !priv->image)
1372 child = gtk_bin_get_child (GTK_BIN (button));
1373 if (GTK_IS_ALIGNMENT (child))
1375 child = gtk_bin_get_child (GTK_BIN (child));
1376 if (GTK_IS_BOX (child))
1378 gtk_style_context_get_style (context,
1379 "image-spacing", &spacing,
1382 gtk_box_set_spacing (GTK_BOX (child), spacing);
1388 gtk_button_style_updated (GtkWidget *widget)
1390 GtkStyleContext *context;
1392 GTK_WIDGET_CLASS (gtk_button_parent_class)->style_updated (widget);
1394 context = gtk_widget_get_style_context (widget);
1396 gtk_button_update_image_spacing (GTK_BUTTON (widget), context);
1400 gtk_button_get_props (GtkButton *button,
1401 GtkBorder *default_border,
1402 GtkBorder *default_outside_border,
1403 GtkBorder *inner_border,
1405 gboolean *interior_focus)
1407 GtkStyleContext *context;
1408 GtkStateFlags state;
1409 GtkBorder *tmp_border;
1411 context = gtk_widget_get_style_context (GTK_WIDGET (button));
1412 state = gtk_style_context_get_state (context);
1416 gtk_style_context_get_style (context,
1417 "default-border", &tmp_border,
1422 *default_border = *tmp_border;
1423 gtk_border_free (tmp_border);
1426 *default_border = default_default_border;
1429 if (default_outside_border)
1431 gtk_style_context_get_style (context,
1432 "default-outside-border", &tmp_border,
1437 *default_outside_border = *tmp_border;
1438 gtk_border_free (tmp_border);
1441 *default_outside_border = default_default_outside_border;
1446 gtk_style_context_get_style (context,
1447 "inner-border", &tmp_border,
1452 *inner_border = *tmp_border;
1453 gtk_border_free (tmp_border);
1456 *inner_border = default_inner_border;
1461 gtk_style_context_get_style (context,
1462 "interior-focus", interior_focus,
1467 gtk_style_context_get_padding (context, state, padding);
1471 gtk_button_size_allocate (GtkWidget *widget,
1472 GtkAllocation *allocation)
1474 GtkButton *button = GTK_BUTTON (widget);
1475 GtkButtonPrivate *priv = button->priv;
1476 GtkAllocation child_allocation;
1477 GtkStyleContext *context;
1479 GtkBorder default_border;
1480 GtkBorder inner_border;
1485 context = gtk_widget_get_style_context (widget);
1487 gtk_button_get_props (button, &default_border, NULL, &inner_border, &padding, NULL);
1488 gtk_style_context_get_style (context,
1489 "focus-line-width", &focus_width,
1490 "focus-padding", &focus_pad,
1493 gtk_widget_set_allocation (widget, allocation);
1495 if (gtk_widget_get_realized (widget))
1496 gdk_window_move_resize (priv->event_window,
1500 allocation->height);
1502 child = gtk_bin_get_child (GTK_BIN (button));
1503 if (child && gtk_widget_get_visible (child))
1505 child_allocation.x = allocation->x + inner_border.left + padding.left;
1506 child_allocation.y = allocation->y + inner_border.top + padding.top;
1508 child_allocation.width =
1510 (padding.left + padding.right) -
1514 child_allocation.height =
1515 allocation->height -
1516 (padding.top + padding.bottom) -
1518 inner_border.bottom;
1520 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1522 child_allocation.x += default_border.left;
1523 child_allocation.y += default_border.top;
1524 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1525 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1528 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1530 child_allocation.x += focus_width + focus_pad;
1531 child_allocation.y += focus_width + focus_pad;
1532 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1533 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1536 if (priv->depressed)
1538 gint child_displacement_x;
1539 gint child_displacement_y;
1541 gtk_style_context_get_style (context,
1542 "child-displacement-x", &child_displacement_x,
1543 "child-displacement-y", &child_displacement_y,
1545 child_allocation.x += child_displacement_x;
1546 child_allocation.y += child_displacement_y;
1549 child_allocation.width = MAX (1, child_allocation.width);
1550 child_allocation.height = MAX (1, child_allocation.height);
1552 gtk_widget_size_allocate (child, &child_allocation);
1557 _gtk_button_paint (GtkButton *button,
1561 GtkStateFlags state)
1563 GtkButtonPrivate *priv = button->priv;
1566 GtkBorder default_border;
1567 GtkBorder default_outside_border;
1568 gboolean interior_focus;
1571 GtkAllocation allocation;
1572 GtkStyleContext *context;
1573 gboolean draw_focus;
1575 widget = GTK_WIDGET (button);
1576 context = gtk_widget_get_style_context (widget);
1578 gtk_style_context_save (context);
1579 gtk_style_context_set_state (context, state);
1581 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, 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);
1592 if (gtk_widget_has_default (widget) &&
1593 priv->relief == GTK_RELIEF_NORMAL)
1595 x += default_border.left;
1596 y += default_border.top;
1597 width -= default_border.left + default_border.right;
1598 height -= default_border.top + default_border.bottom;
1600 gtk_style_context_add_class (context, GTK_STYLE_CLASS_DEFAULT);
1602 else if (gtk_widget_get_can_default (widget))
1604 x += default_outside_border.left;
1605 y += default_outside_border.top;
1606 width -= default_outside_border.left + default_outside_border.right;
1607 height -= default_outside_border.top + default_outside_border.bottom;
1610 draw_focus = gtk_widget_has_visible_focus (widget);
1613 if (!interior_focus && draw_focus)
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 state & GTK_STATE_FLAG_PRELIGHT)
1624 gtk_render_background (context, cr,
1625 x, y, width, height);
1626 gtk_render_frame (context, cr,
1627 x, y, width, height);
1632 gint child_displacement_x;
1633 gint child_displacement_y;
1634 gboolean displace_focus;
1637 gtk_style_context_get_style (context,
1638 "child-displacement-y", &child_displacement_y,
1639 "child-displacement-x", &child_displacement_x,
1640 "displace-focus", &displace_focus,
1642 gtk_style_context_get_border (context, state, &border);
1646 x += border.left + focus_pad;
1647 y += border.top + focus_pad;
1648 width -= (2 * focus_pad) + border.left + border.right;
1649 height -= (2 * focus_pad) + border.top + border.bottom;
1653 x -= focus_width + focus_pad;
1654 y -= focus_width + focus_pad;
1655 width += 2 * (focus_width + focus_pad);
1656 height += 2 * (focus_width + focus_pad);
1659 if (priv->depressed && displace_focus)
1661 x += child_displacement_x;
1662 y += child_displacement_y;
1665 gtk_render_focus (context, cr, x, y, width, height);
1668 gtk_style_context_restore (context);
1672 gtk_button_draw (GtkWidget *widget,
1675 GtkButton *button = GTK_BUTTON (widget);
1677 _gtk_button_paint (button, cr,
1678 gtk_widget_get_allocated_width (widget),
1679 gtk_widget_get_allocated_height (widget),
1680 gtk_widget_get_state_flags (widget));
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 && gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
1860 device = gdk_device_get_associated_device (device);
1862 if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
1864 time = gtk_get_current_event_time ();
1866 /* bgo#626336 - Only grab if we have a device (from an event), not if we
1867 * were activated programmatically when no event is available.
1869 if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1871 if (gdk_device_grab (device, priv->event_window,
1872 GDK_OWNERSHIP_WINDOW, TRUE,
1873 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1874 NULL, time) == GDK_GRAB_SUCCESS)
1876 gtk_device_grab_add (widget, device, TRUE);
1877 priv->grab_keyboard = device;
1878 priv->grab_time = time;
1882 priv->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1883 button_activate_timeout,
1885 priv->button_down = TRUE;
1886 gtk_button_update_state (button);
1887 gtk_widget_queue_draw (GTK_WIDGET (button));
1892 gtk_button_finish_activate (GtkButton *button,
1895 GtkWidget *widget = GTK_WIDGET (button);
1896 GtkButtonPrivate *priv = button->priv;
1898 g_source_remove (priv->activate_timeout);
1899 priv->activate_timeout = 0;
1901 if (priv->grab_keyboard)
1903 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1904 gtk_device_grab_remove (widget, priv->grab_keyboard);
1905 priv->grab_keyboard = NULL;
1908 priv->button_down = FALSE;
1910 gtk_button_update_state (button);
1911 gtk_widget_queue_draw (GTK_WIDGET (button));
1914 gtk_button_clicked (button);
1919 gtk_button_get_size (GtkWidget *widget,
1920 GtkOrientation orientation,
1924 GtkButton *button = GTK_BUTTON (widget);
1925 GtkStyleContext *context;
1927 GtkBorder default_border;
1928 GtkBorder inner_border;
1932 gint minimum, natural;
1934 context = gtk_widget_get_style_context (widget);
1936 gtk_button_get_props (button, &default_border, NULL, &inner_border, &padding, NULL);
1937 gtk_style_context_get_style (context,
1938 "focus-line-width", &focus_width,
1939 "focus-padding", &focus_pad,
1942 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1944 minimum = inner_border.left + inner_border.right + padding.left + padding.right;
1946 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1947 minimum += default_border.left + default_border.right;
1951 minimum = inner_border.top + inner_border.bottom + padding.top + padding.bottom;
1953 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1954 minimum += default_border.top + default_border.bottom;
1957 minimum += 2 * (focus_width + focus_pad);
1960 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1961 gtk_widget_get_visible (child))
1963 gint child_min, child_nat;
1965 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1966 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
1968 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1970 minimum += child_min;
1971 natural += child_nat;
1975 *minimum_size = minimum;
1978 *natural_size = natural;
1982 gtk_button_get_preferred_width (GtkWidget *widget,
1986 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1990 gtk_button_get_preferred_height (GtkWidget *widget,
1994 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1998 * gtk_button_set_label:
1999 * @button: a #GtkButton
2002 * Sets the text of the label of the button to @str. This text is
2003 * also used to select the stock item if gtk_button_set_use_stock()
2006 * This will also clear any previously set labels.
2009 gtk_button_set_label (GtkButton *button,
2012 GtkButtonPrivate *priv;
2015 g_return_if_fail (GTK_IS_BUTTON (button));
2017 priv = button->priv;
2019 new_label = g_strdup (label);
2020 g_free (priv->label_text);
2021 priv->label_text = new_label;
2023 gtk_button_construct_child (button);
2025 g_object_notify (G_OBJECT (button), "label");
2029 * gtk_button_get_label:
2030 * @button: a #GtkButton
2032 * Fetches the text from the label of the button, as set by
2033 * gtk_button_set_label(). If the label text has not
2034 * been set the return value will be %NULL. This will be the
2035 * case if you create an empty button with gtk_button_new() to
2036 * use as a container.
2038 * Return value: The text of the label widget. This string is owned
2039 * by the widget and must not be modified or freed.
2042 gtk_button_get_label (GtkButton *button)
2044 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2046 return button->priv->label_text;
2050 * gtk_button_set_use_underline:
2051 * @button: a #GtkButton
2052 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2054 * If true, an underline in the text of the button label indicates
2055 * the next character should be used for the mnemonic accelerator key.
2058 gtk_button_set_use_underline (GtkButton *button,
2059 gboolean use_underline)
2061 GtkButtonPrivate *priv;
2063 g_return_if_fail (GTK_IS_BUTTON (button));
2065 priv = button->priv;
2067 use_underline = use_underline != FALSE;
2069 if (use_underline != priv->use_underline)
2071 priv->use_underline = use_underline;
2073 gtk_button_construct_child (button);
2075 g_object_notify (G_OBJECT (button), "use-underline");
2080 * gtk_button_get_use_underline:
2081 * @button: a #GtkButton
2083 * Returns whether an embedded underline in the button label indicates a
2084 * mnemonic. See gtk_button_set_use_underline ().
2086 * Return value: %TRUE if an embedded underline in the button label
2087 * indicates the mnemonic accelerator keys.
2090 gtk_button_get_use_underline (GtkButton *button)
2092 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2094 return button->priv->use_underline;
2098 * gtk_button_set_use_stock:
2099 * @button: a #GtkButton
2100 * @use_stock: %TRUE if the button should use a stock item
2102 * If %TRUE, the label set on the button is used as a
2103 * stock id to select the stock item for the button.
2106 gtk_button_set_use_stock (GtkButton *button,
2109 GtkButtonPrivate *priv;
2111 g_return_if_fail (GTK_IS_BUTTON (button));
2113 priv = button->priv;
2115 use_stock = use_stock != FALSE;
2117 if (use_stock != priv->use_stock)
2119 priv->use_stock = use_stock;
2121 gtk_button_construct_child (button);
2123 g_object_notify (G_OBJECT (button), "use-stock");
2128 * gtk_button_get_use_stock:
2129 * @button: a #GtkButton
2131 * Returns whether the button label is a stock item.
2133 * Return value: %TRUE if the button label is used to
2134 * select a stock item instead of being
2135 * used directly as the label text.
2138 gtk_button_get_use_stock (GtkButton *button)
2140 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2142 return button->priv->use_stock;
2146 * gtk_button_set_focus_on_click:
2147 * @button: a #GtkButton
2148 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2150 * Sets whether the button will grab focus when it is clicked with the mouse.
2151 * Making mouse clicks not grab focus is useful in places like toolbars where
2152 * you don't want the keyboard focus removed from the main area of the
2158 gtk_button_set_focus_on_click (GtkButton *button,
2159 gboolean focus_on_click)
2161 GtkButtonPrivate *priv;
2163 g_return_if_fail (GTK_IS_BUTTON (button));
2165 priv = button->priv;
2167 focus_on_click = focus_on_click != FALSE;
2169 if (priv->focus_on_click != focus_on_click)
2171 priv->focus_on_click = focus_on_click;
2173 g_object_notify (G_OBJECT (button), "focus-on-click");
2178 * gtk_button_get_focus_on_click:
2179 * @button: a #GtkButton
2181 * Returns whether the button grabs focus when it is clicked with the mouse.
2182 * See gtk_button_set_focus_on_click().
2184 * Return value: %TRUE if the button grabs focus when it is clicked with
2190 gtk_button_get_focus_on_click (GtkButton *button)
2192 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2194 return button->priv->focus_on_click;
2198 * gtk_button_set_alignment:
2199 * @button: a #GtkButton
2200 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2201 * 1.0 is right aligned
2202 * @yalign: the vertical position of the child, 0.0 is top aligned,
2203 * 1.0 is bottom aligned
2205 * Sets the alignment of the child. This property has no effect unless
2206 * the child is a #GtkMisc or a #GtkAlignment.
2211 gtk_button_set_alignment (GtkButton *button,
2215 GtkButtonPrivate *priv;
2217 g_return_if_fail (GTK_IS_BUTTON (button));
2219 priv = button->priv;
2221 priv->xalign = xalign;
2222 priv->yalign = yalign;
2223 priv->align_set = 1;
2225 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2227 g_object_freeze_notify (G_OBJECT (button));
2228 g_object_notify (G_OBJECT (button), "xalign");
2229 g_object_notify (G_OBJECT (button), "yalign");
2230 g_object_thaw_notify (G_OBJECT (button));
2234 * gtk_button_get_alignment:
2235 * @button: a #GtkButton
2236 * @xalign: (out): return location for horizontal alignment
2237 * @yalign: (out): return location for vertical alignment
2239 * Gets the alignment of the child in the button.
2244 gtk_button_get_alignment (GtkButton *button,
2248 GtkButtonPrivate *priv;
2250 g_return_if_fail (GTK_IS_BUTTON (button));
2252 priv = button->priv;
2255 *xalign = priv->xalign;
2258 *yalign = priv->yalign;
2262 * _gtk_button_set_depressed:
2263 * @button: a #GtkButton
2264 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2266 * Sets whether the button is currently drawn as down or not. This is
2267 * purely a visual setting, and is meant only for use by derived widgets
2268 * such as #GtkToggleButton.
2271 _gtk_button_set_depressed (GtkButton *button,
2274 GtkWidget *widget = GTK_WIDGET (button);
2275 GtkButtonPrivate *priv = button->priv;
2277 depressed = depressed != FALSE;
2279 if (depressed != priv->depressed)
2281 priv->depressed = depressed;
2282 gtk_widget_queue_resize (widget);
2287 gtk_button_update_state (GtkButton *button)
2289 GtkButtonPrivate *priv = button->priv;
2290 GtkStateFlags new_state;
2293 if (priv->activate_timeout)
2294 depressed = priv->depress_on_activate;
2296 depressed = priv->in_button && priv->button_down;
2298 new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) &
2299 ~(GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_ACTIVE);
2301 if (priv->in_button)
2302 new_state |= GTK_STATE_FLAG_PRELIGHT;
2304 if (priv->button_down || depressed)
2305 new_state |= GTK_STATE_FLAG_ACTIVE;
2307 _gtk_button_set_depressed (button, depressed);
2308 gtk_widget_set_state_flags (GTK_WIDGET (button), new_state, TRUE);
2312 show_image_change_notify (GtkButton *button)
2314 GtkButtonPrivate *priv = button->priv;
2318 if (show_image (button))
2319 gtk_widget_show (priv->image);
2321 gtk_widget_hide (priv->image);
2326 traverse_container (GtkWidget *widget,
2329 if (GTK_IS_BUTTON (widget))
2330 show_image_change_notify (GTK_BUTTON (widget));
2331 else if (GTK_IS_CONTAINER (widget))
2332 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2336 gtk_button_setting_changed (GtkSettings *settings)
2340 list = gtk_window_list_toplevels ();
2342 for (l = list; l; l = l->next)
2343 gtk_container_forall (GTK_CONTAINER (l->data),
2344 traverse_container, NULL);
2351 gtk_button_screen_changed (GtkWidget *widget,
2352 GdkScreen *previous_screen)
2355 GtkButtonPrivate *priv;
2356 GtkSettings *settings;
2357 gulong show_image_connection;
2359 if (!gtk_widget_has_screen (widget))
2362 button = GTK_BUTTON (widget);
2363 priv = button->priv;
2365 /* If the button is being pressed while the screen changes the
2366 release might never occur, so we reset the state. */
2367 if (priv->button_down)
2369 priv->button_down = FALSE;
2370 gtk_button_update_state (button);
2373 settings = gtk_widget_get_settings (widget);
2375 show_image_connection =
2376 g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2377 NULL, gtk_button_setting_changed, NULL);
2379 if (show_image_connection)
2382 g_signal_connect (settings, "notify::gtk-button-images",
2383 G_CALLBACK (gtk_button_setting_changed), NULL);
2385 show_image_change_notify (button);
2389 gtk_button_state_changed (GtkWidget *widget,
2390 GtkStateType previous_state)
2392 GtkButton *button = GTK_BUTTON (widget);
2393 GtkButtonPrivate *priv = button->priv;
2395 if (!gtk_widget_is_sensitive (widget))
2397 priv->in_button = FALSE;
2398 gtk_real_button_released (button);
2403 gtk_button_grab_notify (GtkWidget *widget,
2404 gboolean was_grabbed)
2406 GtkButton *button = GTK_BUTTON (widget);
2407 GtkButtonPrivate *priv = button->priv;
2410 if (priv->activate_timeout &&
2411 priv->grab_keyboard &&
2412 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2413 gtk_button_finish_activate (button, FALSE);
2417 save_in = priv->in_button;
2418 priv->in_button = FALSE;
2419 gtk_real_button_released (button);
2420 if (save_in != priv->in_button)
2422 priv->in_button = save_in;
2423 gtk_button_update_state (button);
2429 * gtk_button_set_image:
2430 * @button: a #GtkButton
2431 * @image: a widget to set as the image for the button
2433 * Set the image of @button to the given widget. Note that
2434 * it depends on the #GtkSettings:gtk-button-images setting whether the
2435 * image will be displayed or not, you don't have to call
2436 * gtk_widget_show() on @image yourself.
2441 gtk_button_set_image (GtkButton *button,
2444 GtkButtonPrivate *priv;
2447 g_return_if_fail (GTK_IS_BUTTON (button));
2448 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2450 priv = button->priv;
2454 parent = gtk_widget_get_parent (priv->image);
2456 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2459 priv->image = image;
2460 priv->image_is_stock = (image == NULL);
2462 gtk_button_construct_child (button);
2464 g_object_notify (G_OBJECT (button), "image");
2468 * gtk_button_get_image:
2469 * @button: a #GtkButton
2471 * Gets the widget that is currenty set as the image of @button.
2472 * This may have been explicitly set by gtk_button_set_image()
2473 * or constructed by gtk_button_new_from_stock().
2475 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2480 gtk_button_get_image (GtkButton *button)
2482 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2484 return button->priv->image;
2488 * gtk_button_set_image_position:
2489 * @button: a #GtkButton
2490 * @position: the position
2492 * Sets the position of the image relative to the text
2493 * inside the button.
2498 gtk_button_set_image_position (GtkButton *button,
2499 GtkPositionType position)
2501 GtkButtonPrivate *priv;
2503 g_return_if_fail (GTK_IS_BUTTON (button));
2504 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2506 priv = button->priv;
2508 if (priv->image_position != position)
2510 priv->image_position = position;
2512 gtk_button_construct_child (button);
2514 g_object_notify (G_OBJECT (button), "image-position");
2519 * gtk_button_get_image_position:
2520 * @button: a #GtkButton
2522 * Gets the position of the image relative to the text
2523 * inside the button.
2525 * Return value: the position
2530 gtk_button_get_image_position (GtkButton *button)
2532 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2534 return button->priv->image_position;
2539 * gtk_button_get_event_window:
2540 * @button: a #GtkButton
2542 * Returns the button's event window if it is realized, %NULL otherwise.
2543 * This function should be rarely needed.
2545 * Return value: (transfer none): @button's event window.
2550 gtk_button_get_event_window (GtkButton *button)
2552 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2554 return button->priv->event_window;