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"
54 #include "gtkiconfactory.h"
55 #include "gtkactivatable.h"
56 #include "gtksizerequest.h"
57 #include "gtktypebuiltins.h"
58 #include "gtkprivate.h"
60 #include "a11y/gtkbuttonaccessible.h"
63 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
64 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
66 /* Time out before giving up on getting a key release when animating
69 #define ACTIVATE_TIMEOUT 250
94 /* activatable properties */
95 PROP_ACTIVATABLE_RELATED_ACTION,
96 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
100 static void gtk_button_destroy (GtkWidget *widget);
101 static void gtk_button_dispose (GObject *object);
102 static void gtk_button_set_property (GObject *object,
106 static void gtk_button_get_property (GObject *object,
110 static void gtk_button_screen_changed (GtkWidget *widget,
111 GdkScreen *previous_screen);
112 static void gtk_button_realize (GtkWidget * widget);
113 static void gtk_button_unrealize (GtkWidget * widget);
114 static void gtk_button_map (GtkWidget * widget);
115 static void gtk_button_unmap (GtkWidget * widget);
116 static void gtk_button_style_updated (GtkWidget * widget);
117 static void gtk_button_size_allocate (GtkWidget * widget,
118 GtkAllocation * allocation);
119 static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
120 static gint gtk_button_button_press (GtkWidget * widget,
121 GdkEventButton * event);
122 static gint gtk_button_button_release (GtkWidget * widget,
123 GdkEventButton * event);
124 static gint gtk_button_grab_broken (GtkWidget * widget,
125 GdkEventGrabBroken * event);
126 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
127 static gint gtk_button_enter_notify (GtkWidget * widget,
128 GdkEventCrossing * event);
129 static gint gtk_button_leave_notify (GtkWidget * widget,
130 GdkEventCrossing * event);
131 static void gtk_real_button_pressed (GtkButton * button);
132 static void gtk_real_button_released (GtkButton * button);
133 static void gtk_real_button_clicked (GtkButton * button);
134 static void gtk_real_button_activate (GtkButton *button);
135 static void gtk_button_update_state (GtkButton *button);
136 static void gtk_button_add (GtkContainer *container,
138 static GType gtk_button_child_type (GtkContainer *container);
139 static void gtk_button_finish_activate (GtkButton *button,
142 static GObject* gtk_button_constructor (GType type,
143 guint n_construct_properties,
144 GObjectConstructParam *construct_params);
145 static void gtk_button_construct_child (GtkButton *button);
146 static void gtk_button_state_changed (GtkWidget *widget,
147 GtkStateType previous_state);
148 static void gtk_button_grab_notify (GtkWidget *widget,
149 gboolean was_grabbed);
152 static void gtk_button_activatable_interface_init(GtkActivatableIface *iface);
153 static void gtk_button_update (GtkActivatable *activatable,
155 const gchar *property_name);
156 static void gtk_button_sync_action_properties (GtkActivatable *activatable,
158 static void gtk_button_set_related_action (GtkButton *button,
160 static void gtk_button_set_use_action_appearance (GtkButton *button,
161 gboolean use_appearance);
163 static void gtk_button_get_preferred_width (GtkWidget *widget,
166 static void gtk_button_get_preferred_height (GtkWidget *widget,
170 static guint button_signals[LAST_SIGNAL] = { 0 };
172 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
173 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
174 gtk_button_activatable_interface_init))
177 gtk_button_class_init (GtkButtonClass *klass)
179 GObjectClass *gobject_class;
180 GtkWidgetClass *widget_class;
181 GtkContainerClass *container_class;
183 gobject_class = G_OBJECT_CLASS (klass);
184 widget_class = (GtkWidgetClass*) klass;
185 container_class = (GtkContainerClass*) klass;
187 gobject_class->constructor = gtk_button_constructor;
188 gobject_class->dispose = gtk_button_dispose;
189 gobject_class->set_property = gtk_button_set_property;
190 gobject_class->get_property = gtk_button_get_property;
192 widget_class->get_preferred_width = gtk_button_get_preferred_width;
193 widget_class->get_preferred_height = gtk_button_get_preferred_height;
194 widget_class->destroy = gtk_button_destroy;
195 widget_class->screen_changed = gtk_button_screen_changed;
196 widget_class->realize = gtk_button_realize;
197 widget_class->unrealize = gtk_button_unrealize;
198 widget_class->map = gtk_button_map;
199 widget_class->unmap = gtk_button_unmap;
200 widget_class->style_updated = gtk_button_style_updated;
201 widget_class->size_allocate = gtk_button_size_allocate;
202 widget_class->draw = gtk_button_draw;
203 widget_class->button_press_event = gtk_button_button_press;
204 widget_class->button_release_event = gtk_button_button_release;
205 widget_class->grab_broken_event = gtk_button_grab_broken;
206 widget_class->key_release_event = gtk_button_key_release;
207 widget_class->enter_notify_event = gtk_button_enter_notify;
208 widget_class->leave_notify_event = gtk_button_leave_notify;
209 widget_class->state_changed = gtk_button_state_changed;
210 widget_class->grab_notify = gtk_button_grab_notify;
212 container_class->child_type = gtk_button_child_type;
213 container_class->add = gtk_button_add;
214 gtk_container_class_handle_border_width (container_class);
216 klass->pressed = gtk_real_button_pressed;
217 klass->released = gtk_real_button_released;
218 klass->clicked = NULL;
219 klass->enter = gtk_button_update_state;
220 klass->leave = gtk_button_update_state;
221 klass->activate = gtk_real_button_activate;
223 g_object_class_install_property (gobject_class,
225 g_param_spec_string ("label",
227 P_("Text of the label widget inside the button, if the button contains a label widget"),
229 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
231 g_object_class_install_property (gobject_class,
233 g_param_spec_boolean ("use-underline",
235 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
237 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
239 g_object_class_install_property (gobject_class,
241 g_param_spec_boolean ("use-stock",
243 P_("If set, the label is used to pick a stock item instead of being displayed"),
245 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
247 g_object_class_install_property (gobject_class,
249 g_param_spec_boolean ("focus-on-click",
250 P_("Focus on click"),
251 P_("Whether the button grabs focus when it is clicked with the mouse"),
253 GTK_PARAM_READWRITE));
255 g_object_class_install_property (gobject_class,
257 g_param_spec_enum ("relief",
259 P_("The border relief style"),
260 GTK_TYPE_RELIEF_STYLE,
262 GTK_PARAM_READWRITE));
267 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
268 * can be used to control its horizontal alignment. 0.0 is left aligned,
269 * 1.0 is right aligned.
273 g_object_class_install_property (gobject_class,
275 g_param_spec_float("xalign",
276 P_("Horizontal alignment for child"),
277 P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
281 GTK_PARAM_READWRITE));
286 * If the child of the button is a #GtkMisc or #GtkAlignment, this property
287 * can be used to control its vertical alignment. 0.0 is top aligned,
288 * 1.0 is bottom aligned.
292 g_object_class_install_property (gobject_class,
294 g_param_spec_float("yalign",
295 P_("Vertical alignment for child"),
296 P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
300 GTK_PARAM_READWRITE));
305 * The child widget to appear next to the button text.
309 g_object_class_install_property (gobject_class,
311 g_param_spec_object ("image",
313 P_("Child widget to appear next to the button text"),
315 GTK_PARAM_READWRITE));
318 * GtkButton:image-position:
320 * The position of the image relative to the text inside the button.
324 g_object_class_install_property (gobject_class,
326 g_param_spec_enum ("image-position",
327 P_("Image position"),
328 P_("The position of the image relative to the text"),
329 GTK_TYPE_POSITION_TYPE,
331 GTK_PARAM_READWRITE));
333 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
334 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
337 * GtkButton::pressed:
338 * @button: the object that received the signal
340 * Emitted when the button is pressed.
342 * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
344 button_signals[PRESSED] =
345 g_signal_new (I_("pressed"),
346 G_OBJECT_CLASS_TYPE (gobject_class),
348 G_STRUCT_OFFSET (GtkButtonClass, pressed),
350 _gtk_marshal_VOID__VOID,
354 * GtkButton::released:
355 * @button: the object that received the signal
357 * Emitted when the button is released.
359 * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
361 button_signals[RELEASED] =
362 g_signal_new (I_("released"),
363 G_OBJECT_CLASS_TYPE (gobject_class),
365 G_STRUCT_OFFSET (GtkButtonClass, released),
367 _gtk_marshal_VOID__VOID,
371 * GtkButton::clicked:
372 * @button: the object that received the signal
374 * Emitted when the button has been activated (pressed and released).
376 button_signals[CLICKED] =
377 g_signal_new (I_("clicked"),
378 G_OBJECT_CLASS_TYPE (gobject_class),
379 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
380 G_STRUCT_OFFSET (GtkButtonClass, clicked),
382 _gtk_marshal_VOID__VOID,
387 * @button: the object that received the signal
389 * Emitted when the pointer enters the button.
391 * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
393 button_signals[ENTER] =
394 g_signal_new (I_("enter"),
395 G_OBJECT_CLASS_TYPE (gobject_class),
397 G_STRUCT_OFFSET (GtkButtonClass, enter),
399 _gtk_marshal_VOID__VOID,
404 * @button: the object that received the signal
406 * Emitted when the pointer leaves the button.
408 * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
410 button_signals[LEAVE] =
411 g_signal_new (I_("leave"),
412 G_OBJECT_CLASS_TYPE (gobject_class),
414 G_STRUCT_OFFSET (GtkButtonClass, leave),
416 _gtk_marshal_VOID__VOID,
420 * GtkButton::activate:
421 * @widget: the object which received the signal.
423 * The ::activate signal on GtkButton is an action signal and
424 * emitting it causes the button to animate press then release.
425 * Applications should never connect to this signal, but use the
426 * #GtkButton::clicked signal.
428 button_signals[ACTIVATE] =
429 g_signal_new (I_("activate"),
430 G_OBJECT_CLASS_TYPE (gobject_class),
431 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
432 G_STRUCT_OFFSET (GtkButtonClass, activate),
434 _gtk_marshal_VOID__VOID,
436 widget_class->activate_signal = button_signals[ACTIVATE];
439 * GtkButton:default-border:
441 * The "default-border" style property defines the extra space to add
442 * around a button that can become the default widget of its window.
443 * For more information about default widgets, see gtk_widget_grab_default().
446 gtk_widget_class_install_style_property (widget_class,
447 g_param_spec_boxed ("default-border",
448 P_("Default Spacing"),
449 P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
451 GTK_PARAM_READABLE));
454 * GtkButton:default-outside-border:
456 * The "default-outside-border" style property defines the extra outside
457 * space to add around a button that can become the default widget of its
458 * window. Extra outside space is always drawn outside the button border.
459 * For more information about default widgets, see gtk_widget_grab_default().
461 gtk_widget_class_install_style_property (widget_class,
462 g_param_spec_boxed ("default-outside-border",
463 P_("Default Outside Spacing"),
464 P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
466 GTK_PARAM_READABLE));
467 gtk_widget_class_install_style_property (widget_class,
468 g_param_spec_int ("child-displacement-x",
469 P_("Child X Displacement"),
470 P_("How far in the x direction to move the child when the button is depressed"),
474 GTK_PARAM_READABLE));
475 gtk_widget_class_install_style_property (widget_class,
476 g_param_spec_int ("child-displacement-y",
477 P_("Child Y Displacement"),
478 P_("How far in the y direction to move the child when the button is depressed"),
482 GTK_PARAM_READABLE));
485 * GtkButton:displace-focus:
487 * Whether the child_displacement_x/child_displacement_y properties
488 * should also affect the focus rectangle.
492 gtk_widget_class_install_style_property (widget_class,
493 g_param_spec_boolean ("displace-focus",
494 P_("Displace focus"),
495 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
497 GTK_PARAM_READABLE));
500 * GtkButton:inner-border:
502 * Sets the border between the button edges and child.
506 * Deprecated: 3.4: Use the standard border and padding CSS properties;
507 * the value of this style property is ignored.
509 gtk_widget_class_install_style_property (widget_class,
510 g_param_spec_boxed ("inner-border",
512 P_("Border between button edges and child."),
514 GTK_PARAM_READABLE));
517 * GtkButton::image-spacing:
519 * Spacing in pixels between the image and label.
523 gtk_widget_class_install_style_property (widget_class,
524 g_param_spec_int ("image-spacing",
526 P_("Spacing in pixels between the image and label"),
530 GTK_PARAM_READABLE));
532 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
534 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_BUTTON_ACCESSIBLE);
538 gtk_button_init (GtkButton *button)
540 GtkButtonPrivate *priv;
541 GtkStyleContext *context;
543 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
548 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
549 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
550 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
552 priv->label_text = NULL;
554 priv->constructed = FALSE;
555 priv->in_button = FALSE;
556 priv->button_down = FALSE;
557 priv->relief = GTK_RELIEF_NORMAL;
558 priv->use_stock = FALSE;
559 priv->use_underline = FALSE;
560 priv->depressed = FALSE;
561 priv->depress_on_activate = TRUE;
562 priv->focus_on_click = TRUE;
567 priv->image_is_stock = TRUE;
568 priv->image_position = GTK_POS_LEFT;
569 priv->use_action_appearance = TRUE;
571 context = gtk_widget_get_style_context (GTK_WIDGET (button));
572 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
576 gtk_button_destroy (GtkWidget *widget)
578 GtkButton *button = GTK_BUTTON (widget);
579 GtkButtonPrivate *priv = button->priv;
581 if (priv->label_text)
583 g_free (priv->label_text);
584 priv->label_text = NULL;
587 GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
591 gtk_button_constructor (GType type,
592 guint n_construct_properties,
593 GObjectConstructParam *construct_params)
597 GtkButtonPrivate *priv;
599 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
600 n_construct_properties,
603 button = GTK_BUTTON (object);
606 priv->constructed = TRUE;
608 if (priv->label_text != NULL)
609 gtk_button_construct_child (button);
616 gtk_button_child_type (GtkContainer *container)
618 if (!gtk_bin_get_child (GTK_BIN (container)))
619 return GTK_TYPE_WIDGET;
625 maybe_set_alignment (GtkButton *button,
628 GtkButtonPrivate *priv = button->priv;
630 if (GTK_IS_MISC (widget))
632 GtkMisc *misc = GTK_MISC (widget);
635 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
637 else if (GTK_IS_ALIGNMENT (widget))
639 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
640 gfloat xscale, yscale;
642 g_object_get (alignment,
648 gtk_alignment_set (alignment,
649 priv->xalign, priv->yalign,
655 gtk_button_add (GtkContainer *container,
658 maybe_set_alignment (GTK_BUTTON (container), widget);
660 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
664 gtk_button_dispose (GObject *object)
666 GtkButton *button = GTK_BUTTON (object);
667 GtkButtonPrivate *priv = button->priv;
671 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
674 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
678 gtk_button_set_property (GObject *object,
683 GtkButton *button = GTK_BUTTON (object);
684 GtkButtonPrivate *priv = button->priv;
689 gtk_button_set_label (button, g_value_get_string (value));
692 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
695 gtk_button_set_relief (button, g_value_get_enum (value));
697 case PROP_USE_UNDERLINE:
698 gtk_button_set_use_underline (button, g_value_get_boolean (value));
701 gtk_button_set_use_stock (button, g_value_get_boolean (value));
703 case PROP_FOCUS_ON_CLICK:
704 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
707 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
710 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
712 case PROP_IMAGE_POSITION:
713 gtk_button_set_image_position (button, g_value_get_enum (value));
715 case PROP_ACTIVATABLE_RELATED_ACTION:
716 gtk_button_set_related_action (button, g_value_get_object (value));
718 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
719 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
722 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
728 gtk_button_get_property (GObject *object,
733 GtkButton *button = GTK_BUTTON (object);
734 GtkButtonPrivate *priv = button->priv;
739 g_value_set_string (value, priv->label_text);
742 g_value_set_object (value, (GObject *)priv->image);
745 g_value_set_enum (value, priv->relief);
747 case PROP_USE_UNDERLINE:
748 g_value_set_boolean (value, priv->use_underline);
751 g_value_set_boolean (value, priv->use_stock);
753 case PROP_FOCUS_ON_CLICK:
754 g_value_set_boolean (value, priv->focus_on_click);
757 g_value_set_float (value, priv->xalign);
760 g_value_set_float (value, priv->yalign);
762 case PROP_IMAGE_POSITION:
763 g_value_set_enum (value, priv->image_position);
765 case PROP_ACTIVATABLE_RELATED_ACTION:
766 g_value_set_object (value, priv->action);
768 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
769 g_value_set_boolean (value, priv->use_action_appearance);
772 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
778 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
780 iface->update = gtk_button_update;
781 iface->sync_action_properties = gtk_button_sync_action_properties;
785 activatable_update_stock_id (GtkButton *button,
788 if (!gtk_button_get_use_stock (button))
791 gtk_button_set_label (button, gtk_action_get_stock_id (action));
795 activatable_update_short_label (GtkButton *button,
801 if (gtk_button_get_use_stock (button))
804 image = gtk_button_get_image (button);
806 /* Dont touch custom child... */
807 child = gtk_bin_get_child (GTK_BIN (button));
808 if (GTK_IS_IMAGE (image) ||
810 GTK_IS_LABEL (child))
812 gtk_button_set_label (button, gtk_action_get_short_label (action));
813 gtk_button_set_use_underline (button, TRUE);
818 activatable_update_icon_name (GtkButton *button,
823 if (gtk_button_get_use_stock (button))
826 image = gtk_button_get_image (button);
828 if (GTK_IS_IMAGE (image) &&
829 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
830 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
831 gtk_image_set_from_icon_name (GTK_IMAGE (image),
832 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
836 activatable_update_gicon (GtkButton *button,
839 GtkWidget *image = gtk_button_get_image (button);
840 GIcon *icon = gtk_action_get_gicon (action);
842 if (GTK_IS_IMAGE (image) &&
843 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
844 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
845 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
849 gtk_button_update (GtkActivatable *activatable,
851 const gchar *property_name)
853 GtkButton *button = GTK_BUTTON (activatable);
854 GtkButtonPrivate *priv = button->priv;
856 if (strcmp (property_name, "visible") == 0)
858 if (gtk_action_is_visible (action))
859 gtk_widget_show (GTK_WIDGET (activatable));
861 gtk_widget_hide (GTK_WIDGET (activatable));
863 else if (strcmp (property_name, "sensitive") == 0)
864 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
866 if (!priv->use_action_appearance)
869 if (strcmp (property_name, "stock-id") == 0)
870 activatable_update_stock_id (GTK_BUTTON (activatable), action);
871 else if (strcmp (property_name, "gicon") == 0)
872 activatable_update_gicon (GTK_BUTTON (activatable), action);
873 else if (strcmp (property_name, "short-label") == 0)
874 activatable_update_short_label (GTK_BUTTON (activatable), action);
875 else if (strcmp (property_name, "icon-name") == 0)
876 activatable_update_icon_name (GTK_BUTTON (activatable), action);
880 gtk_button_sync_action_properties (GtkActivatable *activatable,
883 GtkButton *button = GTK_BUTTON (activatable);
884 GtkButtonPrivate *priv = button->priv;
889 if (gtk_action_is_visible (action))
890 gtk_widget_show (GTK_WIDGET (activatable));
892 gtk_widget_hide (GTK_WIDGET (activatable));
894 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
896 if (priv->use_action_appearance)
898 activatable_update_stock_id (GTK_BUTTON (activatable), action);
899 activatable_update_short_label (GTK_BUTTON (activatable), action);
900 activatable_update_gicon (GTK_BUTTON (activatable), action);
901 activatable_update_icon_name (GTK_BUTTON (activatable), action);
906 gtk_button_set_related_action (GtkButton *button,
909 GtkButtonPrivate *priv = button->priv;
911 if (priv->action == action)
914 /* This should be a default handler, but for compatibility reasons
915 * we need to support derived classes that don't chain up their
918 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
920 g_signal_connect_after (button, "clicked",
921 G_CALLBACK (gtk_real_button_clicked), NULL);
923 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
925 priv->action = action;
929 gtk_button_set_use_action_appearance (GtkButton *button,
930 gboolean use_appearance)
932 GtkButtonPrivate *priv = button->priv;
934 if (priv->use_action_appearance != use_appearance)
936 priv->use_action_appearance = use_appearance;
938 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
945 * Creates a new #GtkButton widget. To add a child widget to the button,
946 * use gtk_container_add().
948 * Returns: The newly created #GtkButton widget.
951 gtk_button_new (void)
953 return g_object_new (GTK_TYPE_BUTTON, NULL);
957 show_image (GtkButton *button)
959 GtkButtonPrivate *priv = button->priv;
962 if (priv->label_text)
964 GtkSettings *settings;
966 settings = gtk_widget_get_settings (GTK_WIDGET (button));
967 g_object_get (settings, "gtk-button-images", &show, NULL);
976 gtk_button_construct_child (GtkButton *button)
978 GtkButtonPrivate *priv = button->priv;
979 GtkStyleContext *context;
985 GtkWidget *image = NULL;
986 gchar *label_text = NULL;
989 if (!priv->constructed)
992 if (!priv->label_text && !priv->image)
995 context = gtk_widget_get_style_context (GTK_WIDGET (button));
997 gtk_style_context_get_style (context,
998 "image-spacing", &image_spacing,
1001 if (priv->image && !priv->image_is_stock)
1005 image = g_object_ref (priv->image);
1007 parent = gtk_widget_get_parent (image);
1009 gtk_container_remove (GTK_CONTAINER (parent), image);
1014 child = gtk_bin_get_child (GTK_BIN (button));
1016 gtk_container_remove (GTK_CONTAINER (button), child);
1018 if (priv->use_stock &&
1020 gtk_stock_lookup (priv->label_text, &item))
1023 image = g_object_ref (gtk_image_new_from_stock (priv->label_text, GTK_ICON_SIZE_BUTTON));
1025 label_text = item.label;
1028 label_text = priv->label_text;
1032 priv->image = image;
1033 g_object_set (priv->image,
1034 "visible", show_image (button),
1035 "no-show-all", TRUE,
1038 if (priv->image_position == GTK_POS_LEFT ||
1039 priv->image_position == GTK_POS_RIGHT)
1040 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1042 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1044 if (priv->align_set)
1045 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1047 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1049 if (priv->image_position == GTK_POS_LEFT ||
1050 priv->image_position == GTK_POS_TOP)
1051 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1053 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1057 if (priv->use_underline || priv->use_stock)
1059 label = gtk_label_new_with_mnemonic (label_text);
1060 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1061 GTK_WIDGET (button));
1064 label = gtk_label_new (label_text);
1066 if (priv->image_position == GTK_POS_RIGHT ||
1067 priv->image_position == GTK_POS_BOTTOM)
1068 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1070 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1073 gtk_container_add (GTK_CONTAINER (button), align);
1074 gtk_container_add (GTK_CONTAINER (align), box);
1075 gtk_widget_show_all (align);
1077 g_object_unref (image);
1082 if (priv->use_underline || priv->use_stock)
1084 label = gtk_label_new_with_mnemonic (priv->label_text);
1085 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1088 label = gtk_label_new (priv->label_text);
1090 if (priv->align_set)
1091 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1093 gtk_widget_show (label);
1094 gtk_container_add (GTK_CONTAINER (button), label);
1099 * gtk_button_new_with_label:
1100 * @label: The text you want the #GtkLabel to hold.
1102 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1105 * Returns: The newly created #GtkButton widget.
1108 gtk_button_new_with_label (const gchar *label)
1110 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1114 * gtk_button_new_from_stock:
1115 * @stock_id: the name of the stock item
1117 * Creates a new #GtkButton containing the image and text from a stock item.
1118 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1121 * If @stock_id is unknown, then it will be treated as a mnemonic
1122 * label (as for gtk_button_new_with_mnemonic()).
1124 * Returns: a new #GtkButton
1127 gtk_button_new_from_stock (const gchar *stock_id)
1129 return g_object_new (GTK_TYPE_BUTTON,
1132 "use-underline", TRUE,
1137 * gtk_button_new_with_mnemonic:
1138 * @label: The text of the button, with an underscore in front of the
1139 * mnemonic character
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.
1148 * Returns: a new #GtkButton
1151 gtk_button_new_with_mnemonic (const gchar *label)
1153 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1157 * gtk_button_pressed:
1158 * @button: The #GtkButton you want to send the signal to.
1160 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1162 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1165 gtk_button_pressed (GtkButton *button)
1167 g_return_if_fail (GTK_IS_BUTTON (button));
1169 g_signal_emit (button, button_signals[PRESSED], 0);
1173 * gtk_button_released:
1174 * @button: The #GtkButton you want to send the signal to.
1176 * Emits a #GtkButton::released signal to the given #GtkButton.
1178 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1181 gtk_button_released (GtkButton *button)
1183 g_return_if_fail (GTK_IS_BUTTON (button));
1185 g_signal_emit (button, button_signals[RELEASED], 0);
1189 * gtk_button_clicked:
1190 * @button: The #GtkButton you want to send the signal to.
1192 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1195 gtk_button_clicked (GtkButton *button)
1197 g_return_if_fail (GTK_IS_BUTTON (button));
1199 g_signal_emit (button, button_signals[CLICKED], 0);
1204 * @button: The #GtkButton you want to send the signal to.
1206 * Emits a #GtkButton::enter signal to the given #GtkButton.
1208 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1211 gtk_button_enter (GtkButton *button)
1213 g_return_if_fail (GTK_IS_BUTTON (button));
1215 g_signal_emit (button, button_signals[ENTER], 0);
1220 * @button: The #GtkButton you want to send the signal to.
1222 * Emits a #GtkButton::leave signal to the given #GtkButton.
1224 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1227 gtk_button_leave (GtkButton *button)
1229 g_return_if_fail (GTK_IS_BUTTON (button));
1231 g_signal_emit (button, button_signals[LEAVE], 0);
1235 * gtk_button_set_relief:
1236 * @button: The #GtkButton you want to set relief styles of.
1237 * @newstyle: The GtkReliefStyle as described above.
1239 * Sets the relief style of the edges of the given #GtkButton widget.
1240 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1241 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1243 * <!-- FIXME: put pictures of each style -->
1246 gtk_button_set_relief (GtkButton *button,
1247 GtkReliefStyle newrelief)
1249 GtkButtonPrivate *priv;
1251 g_return_if_fail (GTK_IS_BUTTON (button));
1253 priv = button->priv;
1255 if (newrelief != priv->relief)
1257 priv->relief = newrelief;
1258 g_object_notify (G_OBJECT (button), "relief");
1259 gtk_widget_queue_draw (GTK_WIDGET (button));
1264 * gtk_button_get_relief:
1265 * @button: The #GtkButton you want the #GtkReliefStyle from.
1267 * Returns the current relief style of the given #GtkButton.
1269 * Returns: The current #GtkReliefStyle
1272 gtk_button_get_relief (GtkButton *button)
1274 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1276 return button->priv->relief;
1280 gtk_button_realize (GtkWidget *widget)
1282 GtkButton *button = GTK_BUTTON (widget);
1283 GtkButtonPrivate *priv = button->priv;
1284 GtkAllocation allocation;
1286 GdkWindowAttr attributes;
1287 gint attributes_mask;
1289 gtk_widget_get_allocation (widget, &allocation);
1291 gtk_widget_set_realized (widget, TRUE);
1293 attributes.window_type = GDK_WINDOW_CHILD;
1294 attributes.x = allocation.x;
1295 attributes.y = allocation.y;
1296 attributes.width = allocation.width;
1297 attributes.height = allocation.height;
1298 attributes.wclass = GDK_INPUT_ONLY;
1299 attributes.event_mask = gtk_widget_get_events (widget);
1300 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1301 GDK_BUTTON_RELEASE_MASK |
1302 GDK_ENTER_NOTIFY_MASK |
1303 GDK_LEAVE_NOTIFY_MASK);
1305 attributes_mask = GDK_WA_X | GDK_WA_Y;
1307 window = gtk_widget_get_parent_window (widget);
1308 gtk_widget_set_window (widget, window);
1309 g_object_ref (window);
1311 priv->event_window = gdk_window_new (window,
1312 &attributes, attributes_mask);
1313 gdk_window_set_user_data (priv->event_window, button);
1317 gtk_button_unrealize (GtkWidget *widget)
1319 GtkButton *button = GTK_BUTTON (widget);
1320 GtkButtonPrivate *priv = button->priv;
1322 if (priv->activate_timeout)
1323 gtk_button_finish_activate (button, FALSE);
1325 if (priv->event_window)
1327 gdk_window_set_user_data (priv->event_window, NULL);
1328 gdk_window_destroy (priv->event_window);
1329 priv->event_window = NULL;
1332 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1336 gtk_button_map (GtkWidget *widget)
1338 GtkButton *button = GTK_BUTTON (widget);
1339 GtkButtonPrivate *priv = button->priv;
1341 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1343 if (priv->event_window)
1344 gdk_window_show (priv->event_window);
1348 gtk_button_unmap (GtkWidget *widget)
1350 GtkButton *button = GTK_BUTTON (widget);
1351 GtkButtonPrivate *priv = button->priv;
1353 if (priv->event_window)
1354 gdk_window_hide (priv->event_window);
1356 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1360 gtk_button_update_image_spacing (GtkButton *button,
1361 GtkStyleContext *context)
1363 GtkButtonPrivate *priv = button->priv;
1367 /* Keep in sync with gtk_button_construct_child,
1368 * we only want to update the spacing if the box
1369 * was constructed there.
1371 if (!priv->constructed || !priv->image)
1374 child = gtk_bin_get_child (GTK_BIN (button));
1375 if (GTK_IS_ALIGNMENT (child))
1377 child = gtk_bin_get_child (GTK_BIN (child));
1378 if (GTK_IS_BOX (child))
1380 gtk_style_context_get_style (context,
1381 "image-spacing", &spacing,
1384 gtk_box_set_spacing (GTK_BOX (child), spacing);
1390 gtk_button_style_updated (GtkWidget *widget)
1392 GtkStyleContext *context;
1394 GTK_WIDGET_CLASS (gtk_button_parent_class)->style_updated (widget);
1396 context = gtk_widget_get_style_context (widget);
1398 gtk_button_update_image_spacing (GTK_BUTTON (widget), context);
1402 gtk_button_get_props (GtkButton *button,
1403 GtkBorder *default_border,
1404 GtkBorder *default_outside_border,
1407 gboolean *interior_focus)
1409 GtkStyleContext *context;
1410 GtkStateFlags state;
1411 GtkBorder *tmp_border;
1413 context = gtk_widget_get_style_context (GTK_WIDGET (button));
1414 state = gtk_style_context_get_state (context);
1418 gtk_style_context_get_style (context,
1419 "default-border", &tmp_border,
1424 *default_border = *tmp_border;
1425 gtk_border_free (tmp_border);
1428 *default_border = default_default_border;
1431 if (default_outside_border)
1433 gtk_style_context_get_style (context,
1434 "default-outside-border", &tmp_border,
1439 *default_outside_border = *tmp_border;
1440 gtk_border_free (tmp_border);
1443 *default_outside_border = default_default_outside_border;
1448 gtk_style_context_get_style (context,
1449 "interior-focus", interior_focus,
1454 gtk_style_context_get_padding (context, state, padding);
1457 gtk_style_context_get_border (context, state, border);
1461 gtk_button_size_allocate (GtkWidget *widget,
1462 GtkAllocation *allocation)
1464 GtkButton *button = GTK_BUTTON (widget);
1465 GtkButtonPrivate *priv = button->priv;
1466 GtkAllocation child_allocation;
1467 GtkStyleContext *context;
1469 GtkBorder default_border;
1475 context = gtk_widget_get_style_context (widget);
1477 gtk_button_get_props (button, &default_border, NULL,
1478 &padding, &border, NULL);
1479 gtk_style_context_get_style (context,
1480 "focus-line-width", &focus_width,
1481 "focus-padding", &focus_pad,
1484 gtk_widget_set_allocation (widget, allocation);
1486 if (gtk_widget_get_realized (widget))
1487 gdk_window_move_resize (priv->event_window,
1491 allocation->height);
1493 child = gtk_bin_get_child (GTK_BIN (button));
1494 if (child && gtk_widget_get_visible (child))
1496 child_allocation.x = allocation->x + padding.left + border.left;
1497 child_allocation.y = allocation->y + padding.top + border.top;
1499 child_allocation.width =
1501 (padding.left + padding.right) -
1502 (border.left + border.right);
1504 child_allocation.height =
1505 allocation->height -
1506 (padding.top + padding.bottom) -
1507 (border.top + border.bottom);
1509 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1511 child_allocation.x += default_border.left;
1512 child_allocation.y += default_border.top;
1513 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1514 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1517 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1519 child_allocation.x += focus_width + focus_pad;
1520 child_allocation.y += focus_width + focus_pad;
1521 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1522 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1525 if (priv->depressed)
1527 gint child_displacement_x;
1528 gint child_displacement_y;
1530 gtk_style_context_get_style (context,
1531 "child-displacement-x", &child_displacement_x,
1532 "child-displacement-y", &child_displacement_y,
1534 child_allocation.x += child_displacement_x;
1535 child_allocation.y += child_displacement_y;
1538 child_allocation.width = MAX (1, child_allocation.width);
1539 child_allocation.height = MAX (1, child_allocation.height);
1541 gtk_widget_size_allocate (child, &child_allocation);
1546 _gtk_button_paint (GtkButton *button,
1550 GtkStateFlags state)
1552 GtkButtonPrivate *priv = button->priv;
1555 GtkBorder default_border;
1556 GtkBorder default_outside_border;
1557 gboolean interior_focus;
1560 GtkAllocation allocation;
1561 GtkStyleContext *context;
1562 gboolean draw_focus;
1564 widget = GTK_WIDGET (button);
1565 context = gtk_widget_get_style_context (widget);
1567 gtk_style_context_save (context);
1569 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, NULL, &interior_focus);
1570 gtk_style_context_get_style (context,
1571 "focus-line-width", &focus_width,
1572 "focus-padding", &focus_pad,
1575 gtk_widget_get_allocation (widget, &allocation);
1580 if (gtk_widget_has_default (widget) &&
1581 priv->relief == GTK_RELIEF_NORMAL)
1583 x += default_border.left;
1584 y += default_border.top;
1585 width -= default_border.left + default_border.right;
1586 height -= default_border.top + default_border.bottom;
1588 gtk_style_context_add_class (context, GTK_STYLE_CLASS_DEFAULT);
1590 else if (gtk_widget_get_can_default (widget))
1592 x += default_outside_border.left;
1593 y += default_outside_border.top;
1594 width -= default_outside_border.left + default_outside_border.right;
1595 height -= default_outside_border.top + default_outside_border.bottom;
1598 draw_focus = gtk_widget_has_visible_focus (widget);
1601 if (!interior_focus && draw_focus)
1603 x += focus_width + focus_pad;
1604 y += focus_width + focus_pad;
1605 width -= 2 * (focus_width + focus_pad);
1606 height -= 2 * (focus_width + focus_pad);
1609 if (priv->relief != GTK_RELIEF_NONE || priv->depressed ||
1610 state & GTK_STATE_FLAG_PRELIGHT)
1612 gtk_render_background (context, cr,
1613 x, y, width, height);
1614 gtk_render_frame (context, cr,
1615 x, y, width, height);
1620 gint child_displacement_x;
1621 gint child_displacement_y;
1622 gboolean displace_focus;
1625 gtk_style_context_get_style (context,
1626 "child-displacement-y", &child_displacement_y,
1627 "child-displacement-x", &child_displacement_x,
1628 "displace-focus", &displace_focus,
1630 gtk_style_context_get_border (context, state, &border);
1634 x += border.left + focus_pad;
1635 y += border.top + focus_pad;
1636 width -= (2 * focus_pad) + border.left + border.right;
1637 height -= (2 * focus_pad) + border.top + border.bottom;
1641 x -= focus_width + focus_pad;
1642 y -= focus_width + focus_pad;
1643 width += 2 * (focus_width + focus_pad);
1644 height += 2 * (focus_width + focus_pad);
1647 if (priv->depressed && displace_focus)
1649 x += child_displacement_x;
1650 y += child_displacement_y;
1653 gtk_render_focus (context, cr, x, y, width, height);
1656 gtk_style_context_restore (context);
1660 gtk_button_draw (GtkWidget *widget,
1663 GtkButton *button = GTK_BUTTON (widget);
1665 _gtk_button_paint (button, cr,
1666 gtk_widget_get_allocated_width (widget),
1667 gtk_widget_get_allocated_height (widget),
1668 gtk_widget_get_state_flags (widget));
1670 GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1676 gtk_button_button_press (GtkWidget *widget,
1677 GdkEventButton *event)
1680 GtkButtonPrivate *priv;
1682 if (event->type == GDK_BUTTON_PRESS)
1684 button = GTK_BUTTON (widget);
1685 priv = button->priv;
1687 if (priv->focus_on_click && !gtk_widget_has_focus (widget))
1688 gtk_widget_grab_focus (widget);
1690 if (event->button == 1)
1691 g_signal_emit (button, button_signals[PRESSED], 0);
1698 gtk_button_button_release (GtkWidget *widget,
1699 GdkEventButton *event)
1703 if (event->button == 1)
1705 button = GTK_BUTTON (widget);
1706 g_signal_emit (button, button_signals[RELEASED], 0);
1713 gtk_button_grab_broken (GtkWidget *widget,
1714 GdkEventGrabBroken *event)
1716 GtkButton *button = GTK_BUTTON (widget);
1717 GtkButtonPrivate *priv = button->priv;
1720 /* Simulate a button release without the pointer in the button */
1721 if (priv->button_down)
1723 save_in = priv->in_button;
1724 priv->in_button = FALSE;
1725 g_signal_emit (button, button_signals[RELEASED], 0);
1726 if (save_in != priv->in_button)
1728 priv->in_button = save_in;
1729 gtk_button_update_state (button);
1737 gtk_button_key_release (GtkWidget *widget,
1740 GtkButton *button = GTK_BUTTON (widget);
1741 GtkButtonPrivate *priv = button->priv;
1743 if (priv->activate_timeout)
1745 gtk_button_finish_activate (button, TRUE);
1748 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1749 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1755 gtk_button_enter_notify (GtkWidget *widget,
1756 GdkEventCrossing *event)
1758 GtkButton *button = GTK_BUTTON (widget);
1759 GtkButtonPrivate *priv = button->priv;
1761 if ((event->window == button->priv->event_window) &&
1762 (event->detail != GDK_NOTIFY_INFERIOR))
1764 priv->in_button = TRUE;
1765 g_signal_emit (button, button_signals[ENTER], 0);
1772 gtk_button_leave_notify (GtkWidget *widget,
1773 GdkEventCrossing *event)
1775 GtkButton *button = GTK_BUTTON (widget);
1776 GtkButtonPrivate *priv = button->priv;
1778 if ((event->window == button->priv->event_window) &&
1779 (event->detail != GDK_NOTIFY_INFERIOR) &&
1780 (gtk_widget_get_sensitive (widget)))
1782 priv->in_button = FALSE;
1783 g_signal_emit (button, button_signals[LEAVE], 0);
1790 gtk_real_button_pressed (GtkButton *button)
1792 GtkButtonPrivate *priv = button->priv;
1794 if (priv->activate_timeout)
1797 priv->button_down = TRUE;
1798 gtk_button_update_state (button);
1802 gtk_real_button_released (GtkButton *button)
1804 GtkButtonPrivate *priv = button->priv;
1806 if (priv->button_down)
1808 priv->button_down = FALSE;
1810 if (priv->activate_timeout)
1813 if (priv->in_button)
1814 gtk_button_clicked (button);
1816 gtk_button_update_state (button);
1821 gtk_real_button_clicked (GtkButton *button)
1823 GtkButtonPrivate *priv = button->priv;
1826 gtk_action_activate (priv->action);
1830 button_activate_timeout (gpointer data)
1832 gtk_button_finish_activate (data, TRUE);
1838 gtk_real_button_activate (GtkButton *button)
1840 GtkWidget *widget = GTK_WIDGET (button);
1841 GtkButtonPrivate *priv = button->priv;
1845 device = gtk_get_current_event_device ();
1847 if (device && gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
1848 device = gdk_device_get_associated_device (device);
1850 if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
1852 time = gtk_get_current_event_time ();
1854 /* bgo#626336 - Only grab if we have a device (from an event), not if we
1855 * were activated programmatically when no event is available.
1857 if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1859 if (gdk_device_grab (device, priv->event_window,
1860 GDK_OWNERSHIP_WINDOW, TRUE,
1861 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1862 NULL, time) == GDK_GRAB_SUCCESS)
1864 gtk_device_grab_add (widget, device, TRUE);
1865 priv->grab_keyboard = device;
1866 priv->grab_time = time;
1870 priv->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1871 button_activate_timeout,
1873 priv->button_down = TRUE;
1874 gtk_button_update_state (button);
1875 gtk_widget_queue_draw (GTK_WIDGET (button));
1880 gtk_button_finish_activate (GtkButton *button,
1883 GtkWidget *widget = GTK_WIDGET (button);
1884 GtkButtonPrivate *priv = button->priv;
1886 g_source_remove (priv->activate_timeout);
1887 priv->activate_timeout = 0;
1889 if (priv->grab_keyboard)
1891 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1892 gtk_device_grab_remove (widget, priv->grab_keyboard);
1893 priv->grab_keyboard = NULL;
1896 priv->button_down = FALSE;
1898 gtk_button_update_state (button);
1899 gtk_widget_queue_draw (GTK_WIDGET (button));
1902 gtk_button_clicked (button);
1907 gtk_button_get_size (GtkWidget *widget,
1908 GtkOrientation orientation,
1912 GtkButton *button = GTK_BUTTON (widget);
1913 GtkStyleContext *context;
1915 GtkBorder default_border;
1920 gint minimum, natural;
1922 context = gtk_widget_get_style_context (widget);
1924 gtk_button_get_props (button, &default_border, NULL,
1925 &padding, &border, NULL);
1926 gtk_style_context_get_style (context,
1927 "focus-line-width", &focus_width,
1928 "focus-padding", &focus_pad,
1931 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1933 minimum = padding.left + padding.right +
1934 border.left + border.right;
1936 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1937 minimum += default_border.left + default_border.right;
1941 minimum = padding.top + padding.bottom +
1942 border.top + border.bottom;
1944 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1945 minimum += default_border.top + default_border.bottom;
1948 minimum += 2 * (focus_width + focus_pad);
1951 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1952 gtk_widget_get_visible (child))
1954 gint child_min, child_nat;
1956 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1957 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
1959 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1961 minimum += child_min;
1962 natural += child_nat;
1966 *minimum_size = minimum;
1969 *natural_size = natural;
1973 gtk_button_get_preferred_width (GtkWidget *widget,
1977 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1981 gtk_button_get_preferred_height (GtkWidget *widget,
1985 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1989 * gtk_button_set_label:
1990 * @button: a #GtkButton
1993 * Sets the text of the label of the button to @str. This text is
1994 * also used to select the stock item if gtk_button_set_use_stock()
1997 * This will also clear any previously set labels.
2000 gtk_button_set_label (GtkButton *button,
2003 GtkButtonPrivate *priv;
2006 g_return_if_fail (GTK_IS_BUTTON (button));
2008 priv = button->priv;
2010 new_label = g_strdup (label);
2011 g_free (priv->label_text);
2012 priv->label_text = new_label;
2014 gtk_button_construct_child (button);
2016 g_object_notify (G_OBJECT (button), "label");
2020 * gtk_button_get_label:
2021 * @button: a #GtkButton
2023 * Fetches the text from the label of the button, as set by
2024 * gtk_button_set_label(). If the label text has not
2025 * been set the return value will be %NULL. This will be the
2026 * case if you create an empty button with gtk_button_new() to
2027 * use as a container.
2029 * Return value: The text of the label widget. This string is owned
2030 * by the widget and must not be modified or freed.
2033 gtk_button_get_label (GtkButton *button)
2035 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2037 return button->priv->label_text;
2041 * gtk_button_set_use_underline:
2042 * @button: a #GtkButton
2043 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2045 * If true, an underline in the text of the button label indicates
2046 * the next character should be used for the mnemonic accelerator key.
2049 gtk_button_set_use_underline (GtkButton *button,
2050 gboolean use_underline)
2052 GtkButtonPrivate *priv;
2054 g_return_if_fail (GTK_IS_BUTTON (button));
2056 priv = button->priv;
2058 use_underline = use_underline != FALSE;
2060 if (use_underline != priv->use_underline)
2062 priv->use_underline = use_underline;
2064 gtk_button_construct_child (button);
2066 g_object_notify (G_OBJECT (button), "use-underline");
2071 * gtk_button_get_use_underline:
2072 * @button: a #GtkButton
2074 * Returns whether an embedded underline in the button label indicates a
2075 * mnemonic. See gtk_button_set_use_underline ().
2077 * Return value: %TRUE if an embedded underline in the button label
2078 * indicates the mnemonic accelerator keys.
2081 gtk_button_get_use_underline (GtkButton *button)
2083 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2085 return button->priv->use_underline;
2089 * gtk_button_set_use_stock:
2090 * @button: a #GtkButton
2091 * @use_stock: %TRUE if the button should use a stock item
2093 * If %TRUE, the label set on the button is used as a
2094 * stock id to select the stock item for the button.
2097 gtk_button_set_use_stock (GtkButton *button,
2100 GtkButtonPrivate *priv;
2102 g_return_if_fail (GTK_IS_BUTTON (button));
2104 priv = button->priv;
2106 use_stock = use_stock != FALSE;
2108 if (use_stock != priv->use_stock)
2110 priv->use_stock = use_stock;
2112 gtk_button_construct_child (button);
2114 g_object_notify (G_OBJECT (button), "use-stock");
2119 * gtk_button_get_use_stock:
2120 * @button: a #GtkButton
2122 * Returns whether the button label is a stock item.
2124 * Return value: %TRUE if the button label is used to
2125 * select a stock item instead of being
2126 * used directly as the label text.
2129 gtk_button_get_use_stock (GtkButton *button)
2131 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2133 return button->priv->use_stock;
2137 * gtk_button_set_focus_on_click:
2138 * @button: a #GtkButton
2139 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2141 * Sets whether the button will grab focus when it is clicked with the mouse.
2142 * Making mouse clicks not grab focus is useful in places like toolbars where
2143 * you don't want the keyboard focus removed from the main area of the
2149 gtk_button_set_focus_on_click (GtkButton *button,
2150 gboolean focus_on_click)
2152 GtkButtonPrivate *priv;
2154 g_return_if_fail (GTK_IS_BUTTON (button));
2156 priv = button->priv;
2158 focus_on_click = focus_on_click != FALSE;
2160 if (priv->focus_on_click != focus_on_click)
2162 priv->focus_on_click = focus_on_click;
2164 g_object_notify (G_OBJECT (button), "focus-on-click");
2169 * gtk_button_get_focus_on_click:
2170 * @button: a #GtkButton
2172 * Returns whether the button grabs focus when it is clicked with the mouse.
2173 * See gtk_button_set_focus_on_click().
2175 * Return value: %TRUE if the button grabs focus when it is clicked with
2181 gtk_button_get_focus_on_click (GtkButton *button)
2183 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2185 return button->priv->focus_on_click;
2189 * gtk_button_set_alignment:
2190 * @button: a #GtkButton
2191 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2192 * 1.0 is right aligned
2193 * @yalign: the vertical position of the child, 0.0 is top aligned,
2194 * 1.0 is bottom aligned
2196 * Sets the alignment of the child. This property has no effect unless
2197 * the child is a #GtkMisc or a #GtkAlignment.
2202 gtk_button_set_alignment (GtkButton *button,
2206 GtkButtonPrivate *priv;
2208 g_return_if_fail (GTK_IS_BUTTON (button));
2210 priv = button->priv;
2212 priv->xalign = xalign;
2213 priv->yalign = yalign;
2214 priv->align_set = 1;
2216 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2218 g_object_freeze_notify (G_OBJECT (button));
2219 g_object_notify (G_OBJECT (button), "xalign");
2220 g_object_notify (G_OBJECT (button), "yalign");
2221 g_object_thaw_notify (G_OBJECT (button));
2225 * gtk_button_get_alignment:
2226 * @button: a #GtkButton
2227 * @xalign: (out): return location for horizontal alignment
2228 * @yalign: (out): return location for vertical alignment
2230 * Gets the alignment of the child in the button.
2235 gtk_button_get_alignment (GtkButton *button,
2239 GtkButtonPrivate *priv;
2241 g_return_if_fail (GTK_IS_BUTTON (button));
2243 priv = button->priv;
2246 *xalign = priv->xalign;
2249 *yalign = priv->yalign;
2253 * _gtk_button_set_depressed:
2254 * @button: a #GtkButton
2255 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2257 * Sets whether the button is currently drawn as down or not. This is
2258 * purely a visual setting, and is meant only for use by derived widgets
2259 * such as #GtkToggleButton.
2262 _gtk_button_set_depressed (GtkButton *button,
2265 GtkWidget *widget = GTK_WIDGET (button);
2266 GtkButtonPrivate *priv = button->priv;
2268 depressed = depressed != FALSE;
2270 if (depressed != priv->depressed)
2272 priv->depressed = depressed;
2273 gtk_widget_queue_resize (widget);
2278 gtk_button_update_state (GtkButton *button)
2280 GtkButtonPrivate *priv = button->priv;
2281 GtkStateFlags new_state;
2284 if (priv->activate_timeout)
2285 depressed = priv->depress_on_activate;
2287 depressed = priv->in_button && priv->button_down;
2289 new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) &
2290 ~(GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_ACTIVE);
2292 if (priv->in_button)
2293 new_state |= GTK_STATE_FLAG_PRELIGHT;
2295 if (priv->button_down || depressed)
2296 new_state |= GTK_STATE_FLAG_ACTIVE;
2298 _gtk_button_set_depressed (button, depressed);
2299 gtk_widget_set_state_flags (GTK_WIDGET (button), new_state, TRUE);
2303 show_image_change_notify (GtkButton *button)
2305 GtkButtonPrivate *priv = button->priv;
2309 if (show_image (button))
2310 gtk_widget_show (priv->image);
2312 gtk_widget_hide (priv->image);
2317 traverse_container (GtkWidget *widget,
2320 if (GTK_IS_BUTTON (widget))
2321 show_image_change_notify (GTK_BUTTON (widget));
2322 else if (GTK_IS_CONTAINER (widget))
2323 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2327 gtk_button_setting_changed (GtkSettings *settings)
2331 list = gtk_window_list_toplevels ();
2333 for (l = list; l; l = l->next)
2334 gtk_container_forall (GTK_CONTAINER (l->data),
2335 traverse_container, NULL);
2342 gtk_button_screen_changed (GtkWidget *widget,
2343 GdkScreen *previous_screen)
2346 GtkButtonPrivate *priv;
2347 GtkSettings *settings;
2348 gulong show_image_connection;
2350 if (!gtk_widget_has_screen (widget))
2353 button = GTK_BUTTON (widget);
2354 priv = button->priv;
2356 /* If the button is being pressed while the screen changes the
2357 release might never occur, so we reset the state. */
2358 if (priv->button_down)
2360 priv->button_down = FALSE;
2361 gtk_button_update_state (button);
2364 settings = gtk_widget_get_settings (widget);
2366 show_image_connection =
2367 g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2368 NULL, gtk_button_setting_changed, NULL);
2370 if (show_image_connection)
2373 g_signal_connect (settings, "notify::gtk-button-images",
2374 G_CALLBACK (gtk_button_setting_changed), NULL);
2376 show_image_change_notify (button);
2380 gtk_button_state_changed (GtkWidget *widget,
2381 GtkStateType previous_state)
2383 GtkButton *button = GTK_BUTTON (widget);
2384 GtkButtonPrivate *priv = button->priv;
2386 if (!gtk_widget_is_sensitive (widget))
2388 priv->in_button = FALSE;
2389 gtk_real_button_released (button);
2394 gtk_button_grab_notify (GtkWidget *widget,
2395 gboolean was_grabbed)
2397 GtkButton *button = GTK_BUTTON (widget);
2398 GtkButtonPrivate *priv = button->priv;
2401 if (priv->activate_timeout &&
2402 priv->grab_keyboard &&
2403 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2404 gtk_button_finish_activate (button, FALSE);
2408 save_in = priv->in_button;
2409 priv->in_button = FALSE;
2410 gtk_real_button_released (button);
2411 if (save_in != priv->in_button)
2413 priv->in_button = save_in;
2414 gtk_button_update_state (button);
2420 * gtk_button_set_image:
2421 * @button: a #GtkButton
2422 * @image: a widget to set as the image for the button
2424 * Set the image of @button to the given widget. Note that
2425 * it depends on the #GtkSettings:gtk-button-images setting whether the
2426 * image will be displayed or not, you don't have to call
2427 * gtk_widget_show() on @image yourself.
2432 gtk_button_set_image (GtkButton *button,
2435 GtkButtonPrivate *priv;
2438 g_return_if_fail (GTK_IS_BUTTON (button));
2439 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2441 priv = button->priv;
2445 parent = gtk_widget_get_parent (priv->image);
2447 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2450 priv->image = image;
2451 priv->image_is_stock = (image == NULL);
2453 gtk_button_construct_child (button);
2455 g_object_notify (G_OBJECT (button), "image");
2459 * gtk_button_get_image:
2460 * @button: a #GtkButton
2462 * Gets the widget that is currenty set as the image of @button.
2463 * This may have been explicitly set by gtk_button_set_image()
2464 * or constructed by gtk_button_new_from_stock().
2466 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2471 gtk_button_get_image (GtkButton *button)
2473 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2475 return button->priv->image;
2479 * gtk_button_set_image_position:
2480 * @button: a #GtkButton
2481 * @position: the position
2483 * Sets the position of the image relative to the text
2484 * inside the button.
2489 gtk_button_set_image_position (GtkButton *button,
2490 GtkPositionType position)
2492 GtkButtonPrivate *priv;
2494 g_return_if_fail (GTK_IS_BUTTON (button));
2495 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2497 priv = button->priv;
2499 if (priv->image_position != position)
2501 priv->image_position = position;
2503 gtk_button_construct_child (button);
2505 g_object_notify (G_OBJECT (button), "image-position");
2510 * gtk_button_get_image_position:
2511 * @button: a #GtkButton
2513 * Gets the position of the image relative to the text
2514 * inside the button.
2516 * Return value: the position
2521 gtk_button_get_image_position (GtkButton *button)
2523 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2525 return button->priv->image_position;
2530 * gtk_button_get_event_window:
2531 * @button: a #GtkButton
2533 * Returns the button's event window if it is realized, %NULL otherwise.
2534 * This function should be rarely needed.
2536 * Return value: (transfer none): @button's event window.
2541 gtk_button_get_event_window (GtkButton *button)
2543 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2545 return button->priv->event_window;