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 "gtkprivate.h"
62 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
63 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
64 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
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 it's 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 it's 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 gtk_widget_class_install_style_property (widget_class,
507 g_param_spec_boxed ("inner-border",
509 P_("Border between button edges and child."),
511 GTK_PARAM_READABLE));
514 * GtkButton::image-spacing:
516 * Spacing in pixels between the image and label.
520 gtk_widget_class_install_style_property (widget_class,
521 g_param_spec_int ("image-spacing",
523 P_("Spacing in pixels between the image and label"),
527 GTK_PARAM_READABLE));
529 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
533 gtk_button_init (GtkButton *button)
535 GtkButtonPrivate *priv;
536 GtkStyleContext *context;
538 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
543 gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
544 gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
545 gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
547 priv->label_text = NULL;
549 priv->constructed = FALSE;
550 priv->in_button = FALSE;
551 priv->button_down = FALSE;
552 priv->relief = GTK_RELIEF_NORMAL;
553 priv->use_stock = FALSE;
554 priv->use_underline = FALSE;
555 priv->depressed = FALSE;
556 priv->depress_on_activate = TRUE;
557 priv->focus_on_click = TRUE;
562 priv->image_is_stock = TRUE;
563 priv->image_position = GTK_POS_LEFT;
564 priv->use_action_appearance = TRUE;
566 context = gtk_widget_get_style_context (GTK_WIDGET (button));
567 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
571 gtk_button_destroy (GtkWidget *widget)
573 GtkButton *button = GTK_BUTTON (widget);
574 GtkButtonPrivate *priv = button->priv;
576 if (priv->label_text)
578 g_free (priv->label_text);
579 priv->label_text = NULL;
582 GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
586 gtk_button_constructor (GType type,
587 guint n_construct_properties,
588 GObjectConstructParam *construct_params)
592 GtkButtonPrivate *priv;
594 object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
595 n_construct_properties,
598 button = GTK_BUTTON (object);
601 priv->constructed = TRUE;
603 if (priv->label_text != NULL)
604 gtk_button_construct_child (button);
611 gtk_button_child_type (GtkContainer *container)
613 if (!gtk_bin_get_child (GTK_BIN (container)))
614 return GTK_TYPE_WIDGET;
620 maybe_set_alignment (GtkButton *button,
623 GtkButtonPrivate *priv = button->priv;
625 if (GTK_IS_MISC (widget))
627 GtkMisc *misc = GTK_MISC (widget);
630 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
632 else if (GTK_IS_ALIGNMENT (widget))
634 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
635 gfloat xscale, yscale;
637 g_object_get (alignment,
643 gtk_alignment_set (alignment,
644 priv->xalign, priv->yalign,
650 gtk_button_add (GtkContainer *container,
653 maybe_set_alignment (GTK_BUTTON (container), widget);
655 GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
659 gtk_button_dispose (GObject *object)
661 GtkButton *button = GTK_BUTTON (object);
662 GtkButtonPrivate *priv = button->priv;
666 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
669 G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
673 gtk_button_set_property (GObject *object,
678 GtkButton *button = GTK_BUTTON (object);
679 GtkButtonPrivate *priv = button->priv;
684 gtk_button_set_label (button, g_value_get_string (value));
687 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
690 gtk_button_set_relief (button, g_value_get_enum (value));
692 case PROP_USE_UNDERLINE:
693 gtk_button_set_use_underline (button, g_value_get_boolean (value));
696 gtk_button_set_use_stock (button, g_value_get_boolean (value));
698 case PROP_FOCUS_ON_CLICK:
699 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
702 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
705 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
707 case PROP_IMAGE_POSITION:
708 gtk_button_set_image_position (button, g_value_get_enum (value));
710 case PROP_ACTIVATABLE_RELATED_ACTION:
711 gtk_button_set_related_action (button, g_value_get_object (value));
713 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
714 gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
717 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
723 gtk_button_get_property (GObject *object,
728 GtkButton *button = GTK_BUTTON (object);
729 GtkButtonPrivate *priv = button->priv;
734 g_value_set_string (value, priv->label_text);
737 g_value_set_object (value, (GObject *)priv->image);
740 g_value_set_enum (value, priv->relief);
742 case PROP_USE_UNDERLINE:
743 g_value_set_boolean (value, priv->use_underline);
746 g_value_set_boolean (value, priv->use_stock);
748 case PROP_FOCUS_ON_CLICK:
749 g_value_set_boolean (value, priv->focus_on_click);
752 g_value_set_float (value, priv->xalign);
755 g_value_set_float (value, priv->yalign);
757 case PROP_IMAGE_POSITION:
758 g_value_set_enum (value, priv->image_position);
760 case PROP_ACTIVATABLE_RELATED_ACTION:
761 g_value_set_object (value, priv->action);
763 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
764 g_value_set_boolean (value, priv->use_action_appearance);
767 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
773 gtk_button_activatable_interface_init (GtkActivatableIface *iface)
775 iface->update = gtk_button_update;
776 iface->sync_action_properties = gtk_button_sync_action_properties;
780 activatable_update_stock_id (GtkButton *button,
783 if (!gtk_button_get_use_stock (button))
786 gtk_button_set_label (button, gtk_action_get_stock_id (action));
790 activatable_update_short_label (GtkButton *button,
796 if (gtk_button_get_use_stock (button))
799 image = gtk_button_get_image (button);
801 /* Dont touch custom child... */
802 child = gtk_bin_get_child (GTK_BIN (button));
803 if (GTK_IS_IMAGE (image) ||
805 GTK_IS_LABEL (child))
807 gtk_button_set_label (button, gtk_action_get_short_label (action));
808 gtk_button_set_use_underline (button, TRUE);
813 activatable_update_icon_name (GtkButton *button,
818 if (gtk_button_get_use_stock (button))
821 image = gtk_button_get_image (button);
823 if (GTK_IS_IMAGE (image) &&
824 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
825 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
826 gtk_image_set_from_icon_name (GTK_IMAGE (image),
827 gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
831 activatable_update_gicon (GtkButton *button,
834 GtkWidget *image = gtk_button_get_image (button);
835 GIcon *icon = gtk_action_get_gicon (action);
837 if (GTK_IS_IMAGE (image) &&
838 (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
839 gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
840 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
844 gtk_button_update (GtkActivatable *activatable,
846 const gchar *property_name)
848 GtkButton *button = GTK_BUTTON (activatable);
849 GtkButtonPrivate *priv = button->priv;
851 if (strcmp (property_name, "visible") == 0)
853 if (gtk_action_is_visible (action))
854 gtk_widget_show (GTK_WIDGET (activatable));
856 gtk_widget_hide (GTK_WIDGET (activatable));
858 else if (strcmp (property_name, "sensitive") == 0)
859 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
861 if (!priv->use_action_appearance)
864 if (strcmp (property_name, "stock-id") == 0)
865 activatable_update_stock_id (GTK_BUTTON (activatable), action);
866 else if (strcmp (property_name, "gicon") == 0)
867 activatable_update_gicon (GTK_BUTTON (activatable), action);
868 else if (strcmp (property_name, "short-label") == 0)
869 activatable_update_short_label (GTK_BUTTON (activatable), action);
870 else if (strcmp (property_name, "icon-name") == 0)
871 activatable_update_icon_name (GTK_BUTTON (activatable), action);
875 gtk_button_sync_action_properties (GtkActivatable *activatable,
878 GtkButton *button = GTK_BUTTON (activatable);
879 GtkButtonPrivate *priv = button->priv;
884 if (gtk_action_is_visible (action))
885 gtk_widget_show (GTK_WIDGET (activatable));
887 gtk_widget_hide (GTK_WIDGET (activatable));
889 gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
891 if (priv->use_action_appearance)
893 activatable_update_stock_id (GTK_BUTTON (activatable), action);
894 activatable_update_short_label (GTK_BUTTON (activatable), action);
895 activatable_update_gicon (GTK_BUTTON (activatable), action);
896 activatable_update_icon_name (GTK_BUTTON (activatable), action);
901 gtk_button_set_related_action (GtkButton *button,
904 GtkButtonPrivate *priv = button->priv;
906 if (priv->action == action)
909 /* This should be a default handler, but for compatibility reasons
910 * we need to support derived classes that don't chain up their
913 g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
915 g_signal_connect_after (button, "clicked",
916 G_CALLBACK (gtk_real_button_clicked), NULL);
918 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
920 priv->action = action;
924 gtk_button_set_use_action_appearance (GtkButton *button,
925 gboolean use_appearance)
927 GtkButtonPrivate *priv = button->priv;
929 if (priv->use_action_appearance != use_appearance)
931 priv->use_action_appearance = use_appearance;
933 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
940 * Creates a new #GtkButton widget. To add a child widget to the button,
941 * use gtk_container_add().
943 * Returns: The newly created #GtkButton widget.
946 gtk_button_new (void)
948 return g_object_new (GTK_TYPE_BUTTON, NULL);
952 show_image (GtkButton *button)
954 GtkButtonPrivate *priv = button->priv;
957 if (priv->label_text)
959 GtkSettings *settings;
961 settings = gtk_widget_get_settings (GTK_WIDGET (button));
962 g_object_get (settings, "gtk-button-images", &show, NULL);
971 gtk_button_construct_child (GtkButton *button)
973 GtkButtonPrivate *priv = button->priv;
974 GtkStyleContext *context;
980 GtkWidget *image = NULL;
981 gchar *label_text = NULL;
984 if (!priv->constructed)
987 if (!priv->label_text && !priv->image)
990 context = gtk_widget_get_style_context (GTK_WIDGET (button));
992 gtk_style_context_get_style (context,
993 "image-spacing", &image_spacing,
996 if (priv->image && !priv->image_is_stock)
1000 image = g_object_ref (priv->image);
1002 parent = gtk_widget_get_parent (image);
1004 gtk_container_remove (GTK_CONTAINER (parent), image);
1009 child = gtk_bin_get_child (GTK_BIN (button));
1011 gtk_container_remove (GTK_CONTAINER (button), child);
1013 if (priv->use_stock &&
1015 gtk_stock_lookup (priv->label_text, &item))
1018 image = g_object_ref (gtk_image_new_from_stock (priv->label_text, GTK_ICON_SIZE_BUTTON));
1020 label_text = item.label;
1023 label_text = priv->label_text;
1027 priv->image = image;
1028 g_object_set (priv->image,
1029 "visible", show_image (button),
1030 "no-show-all", TRUE,
1033 if (priv->image_position == GTK_POS_LEFT ||
1034 priv->image_position == GTK_POS_RIGHT)
1035 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1037 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1039 if (priv->align_set)
1040 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1042 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1044 if (priv->image_position == GTK_POS_LEFT ||
1045 priv->image_position == GTK_POS_TOP)
1046 gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1048 gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1052 if (priv->use_underline || priv->use_stock)
1054 label = gtk_label_new_with_mnemonic (label_text);
1055 gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1056 GTK_WIDGET (button));
1059 label = gtk_label_new (label_text);
1061 if (priv->image_position == GTK_POS_RIGHT ||
1062 priv->image_position == GTK_POS_BOTTOM)
1063 gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1065 gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1068 gtk_container_add (GTK_CONTAINER (button), align);
1069 gtk_container_add (GTK_CONTAINER (align), box);
1070 gtk_widget_show_all (align);
1072 g_object_unref (image);
1077 if (priv->use_underline || priv->use_stock)
1079 label = gtk_label_new_with_mnemonic (priv->label_text);
1080 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1083 label = gtk_label_new (priv->label_text);
1085 if (priv->align_set)
1086 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1088 gtk_widget_show (label);
1089 gtk_container_add (GTK_CONTAINER (button), label);
1094 * gtk_button_new_with_label:
1095 * @label: The text you want the #GtkLabel to hold.
1097 * Creates a #GtkButton widget with a #GtkLabel child containing the given
1100 * Returns: The newly created #GtkButton widget.
1103 gtk_button_new_with_label (const gchar *label)
1105 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1109 * gtk_button_new_from_stock:
1110 * @stock_id: the name of the stock item
1112 * Creates a new #GtkButton containing the image and text from a stock item.
1113 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1116 * If @stock_id is unknown, then it will be treated as a mnemonic
1117 * label (as for gtk_button_new_with_mnemonic()).
1119 * Returns: a new #GtkButton
1122 gtk_button_new_from_stock (const gchar *stock_id)
1124 return g_object_new (GTK_TYPE_BUTTON,
1127 "use-underline", TRUE,
1132 * gtk_button_new_with_mnemonic:
1133 * @label: The text of the button, with an underscore in front of the
1134 * mnemonic character
1135 * @returns: a new #GtkButton
1137 * Creates a new #GtkButton containing a label.
1138 * If characters in @label are preceded by an underscore, they are underlined.
1139 * If you need a literal underscore character in a label, use '__' (two
1140 * underscores). The first underlined character represents a keyboard
1141 * accelerator called a mnemonic.
1142 * Pressing Alt and that key activates the button.
1145 gtk_button_new_with_mnemonic (const gchar *label)
1147 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
1151 * gtk_button_pressed:
1152 * @button: The #GtkButton you want to send the signal to.
1154 * Emits a #GtkButton::pressed signal to the given #GtkButton.
1156 * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1159 gtk_button_pressed (GtkButton *button)
1161 g_return_if_fail (GTK_IS_BUTTON (button));
1163 g_signal_emit (button, button_signals[PRESSED], 0);
1167 * gtk_button_released:
1168 * @button: The #GtkButton you want to send the signal to.
1170 * Emits a #GtkButton::released signal to the given #GtkButton.
1172 * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1175 gtk_button_released (GtkButton *button)
1177 g_return_if_fail (GTK_IS_BUTTON (button));
1179 g_signal_emit (button, button_signals[RELEASED], 0);
1183 * gtk_button_clicked:
1184 * @button: The #GtkButton you want to send the signal to.
1186 * Emits a #GtkButton::clicked signal to the given #GtkButton.
1189 gtk_button_clicked (GtkButton *button)
1191 g_return_if_fail (GTK_IS_BUTTON (button));
1193 g_signal_emit (button, button_signals[CLICKED], 0);
1198 * @button: The #GtkButton you want to send the signal to.
1200 * Emits a #GtkButton::enter signal to the given #GtkButton.
1202 * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1205 gtk_button_enter (GtkButton *button)
1207 g_return_if_fail (GTK_IS_BUTTON (button));
1209 g_signal_emit (button, button_signals[ENTER], 0);
1214 * @button: The #GtkButton you want to send the signal to.
1216 * Emits a #GtkButton::leave signal to the given #GtkButton.
1218 * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1221 gtk_button_leave (GtkButton *button)
1223 g_return_if_fail (GTK_IS_BUTTON (button));
1225 g_signal_emit (button, button_signals[LEAVE], 0);
1229 * gtk_button_set_relief:
1230 * @button: The #GtkButton you want to set relief styles of.
1231 * @newstyle: The GtkReliefStyle as described above.
1233 * Sets the relief style of the edges of the given #GtkButton widget.
1234 * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1235 * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1237 * <!-- FIXME: put pictures of each style -->
1240 gtk_button_set_relief (GtkButton *button,
1241 GtkReliefStyle newrelief)
1243 GtkButtonPrivate *priv;
1245 g_return_if_fail (GTK_IS_BUTTON (button));
1247 priv = button->priv;
1249 if (newrelief != priv->relief)
1251 priv->relief = newrelief;
1252 g_object_notify (G_OBJECT (button), "relief");
1253 gtk_widget_queue_draw (GTK_WIDGET (button));
1258 * gtk_button_get_relief:
1259 * @button: The #GtkButton you want the #GtkReliefStyle from.
1261 * Returns the current relief style of the given #GtkButton.
1263 * Returns: The current #GtkReliefStyle
1266 gtk_button_get_relief (GtkButton *button)
1268 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1270 return button->priv->relief;
1274 gtk_button_realize (GtkWidget *widget)
1276 GtkButton *button = GTK_BUTTON (widget);
1277 GtkButtonPrivate *priv = button->priv;
1278 GtkAllocation allocation;
1280 GdkWindowAttr attributes;
1281 gint attributes_mask;
1283 gtk_widget_get_allocation (widget, &allocation);
1285 gtk_widget_set_realized (widget, TRUE);
1287 attributes.window_type = GDK_WINDOW_CHILD;
1288 attributes.x = allocation.x;
1289 attributes.y = allocation.y;
1290 attributes.width = allocation.width;
1291 attributes.height = allocation.height;
1292 attributes.wclass = GDK_INPUT_ONLY;
1293 attributes.event_mask = gtk_widget_get_events (widget);
1294 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1295 GDK_BUTTON_RELEASE_MASK |
1296 GDK_ENTER_NOTIFY_MASK |
1297 GDK_LEAVE_NOTIFY_MASK);
1299 attributes_mask = GDK_WA_X | GDK_WA_Y;
1301 window = gtk_widget_get_parent_window (widget);
1302 gtk_widget_set_window (widget, window);
1303 g_object_ref (window);
1305 priv->event_window = gdk_window_new (window,
1306 &attributes, attributes_mask);
1307 gdk_window_set_user_data (priv->event_window, button);
1311 gtk_button_unrealize (GtkWidget *widget)
1313 GtkButton *button = GTK_BUTTON (widget);
1314 GtkButtonPrivate *priv = button->priv;
1316 if (priv->activate_timeout)
1317 gtk_button_finish_activate (button, FALSE);
1319 if (priv->event_window)
1321 gdk_window_set_user_data (priv->event_window, NULL);
1322 gdk_window_destroy (priv->event_window);
1323 priv->event_window = NULL;
1326 GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1330 gtk_button_map (GtkWidget *widget)
1332 GtkButton *button = GTK_BUTTON (widget);
1333 GtkButtonPrivate *priv = button->priv;
1335 GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1337 if (priv->event_window)
1338 gdk_window_show (priv->event_window);
1342 gtk_button_unmap (GtkWidget *widget)
1344 GtkButton *button = GTK_BUTTON (widget);
1345 GtkButtonPrivate *priv = button->priv;
1347 if (priv->event_window)
1348 gdk_window_hide (priv->event_window);
1350 GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1354 gtk_button_update_image_spacing (GtkButton *button,
1355 GtkStyleContext *context)
1357 GtkButtonPrivate *priv = button->priv;
1361 /* Keep in sync with gtk_button_construct_child,
1362 * we only want to update the spacing if the box
1363 * was constructed there.
1365 if (!priv->constructed || !priv->image)
1368 child = gtk_bin_get_child (GTK_BIN (button));
1369 if (GTK_IS_ALIGNMENT (child))
1371 child = gtk_bin_get_child (GTK_BIN (child));
1372 if (GTK_IS_BOX (child))
1374 gtk_style_context_get_style (context,
1375 "image-spacing", &spacing,
1378 gtk_box_set_spacing (GTK_BOX (child), spacing);
1384 gtk_button_style_updated (GtkWidget *widget)
1386 GtkStyleContext *context;
1388 context = gtk_widget_get_style_context (widget);
1390 gtk_button_update_image_spacing (GTK_BUTTON (widget), context);
1394 gtk_button_get_props (GtkButton *button,
1395 GtkBorder *default_border,
1396 GtkBorder *default_outside_border,
1397 GtkBorder *inner_border,
1398 gboolean *interior_focus)
1400 GtkStyleContext *context;
1401 GtkBorder *tmp_border;
1403 context = gtk_widget_get_style_context (GTK_WIDGET (button));
1407 gtk_style_context_get_style (context,
1408 "default-border", &tmp_border,
1413 *default_border = *tmp_border;
1414 gtk_border_free (tmp_border);
1417 *default_border = default_default_border;
1420 if (default_outside_border)
1422 gtk_style_context_get_style (context,
1423 "default-outside-border", &tmp_border,
1428 *default_outside_border = *tmp_border;
1429 gtk_border_free (tmp_border);
1432 *default_outside_border = default_default_outside_border;
1437 gtk_style_context_get_style (context,
1438 "inner-border", &tmp_border,
1443 *inner_border = *tmp_border;
1444 gtk_border_free (tmp_border);
1447 *inner_border = default_inner_border;
1452 gtk_style_context_get_style (context,
1453 "interior-focus", interior_focus,
1459 gtk_button_size_allocate (GtkWidget *widget,
1460 GtkAllocation *allocation)
1462 GtkButton *button = GTK_BUTTON (widget);
1463 GtkButtonPrivate *priv = button->priv;
1464 GtkAllocation child_allocation;
1465 GtkStyleContext *context;
1466 GtkStateFlags state;
1468 GtkBorder default_border;
1469 GtkBorder inner_border;
1474 context = gtk_widget_get_style_context (widget);
1475 state = gtk_widget_get_state_flags (widget);
1477 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1478 gtk_style_context_get_style (context,
1479 "focus-line-width", &focus_width,
1480 "focus-padding", &focus_pad,
1483 gtk_style_context_get (context, state,
1484 "border-width", &border,
1487 gtk_widget_set_allocation (widget, allocation);
1489 if (gtk_widget_get_realized (widget))
1490 gdk_window_move_resize (priv->event_window,
1494 allocation->height);
1496 child = gtk_bin_get_child (GTK_BIN (button));
1497 if (child && gtk_widget_get_visible (child))
1499 child_allocation.x = allocation->x + inner_border.left + border->left;
1500 child_allocation.y = allocation->y + inner_border.top + border->top;
1502 child_allocation.width =
1504 (border->left + border->right) -
1508 child_allocation.height =
1509 allocation->height -
1510 (border->top + border->bottom) -
1512 inner_border.bottom;
1514 if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1516 child_allocation.x += default_border.left;
1517 child_allocation.y += default_border.top;
1518 child_allocation.width = child_allocation.width - default_border.left - default_border.right;
1519 child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1522 if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1524 child_allocation.x += focus_width + focus_pad;
1525 child_allocation.y += focus_width + focus_pad;
1526 child_allocation.width = child_allocation.width - (focus_width + focus_pad) * 2;
1527 child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1530 if (priv->depressed)
1532 gint child_displacement_x;
1533 gint child_displacement_y;
1535 gtk_style_context_get_style (context,
1536 "child-displacement-x", &child_displacement_x,
1537 "child-displacement-y", &child_displacement_y,
1539 child_allocation.x += child_displacement_x;
1540 child_allocation.y += child_displacement_y;
1543 child_allocation.width = MAX (1, child_allocation.width);
1544 child_allocation.height = MAX (1, child_allocation.height);
1546 gtk_widget_size_allocate (child, &child_allocation);
1549 gtk_border_free (border);
1553 _gtk_button_paint (GtkButton *button,
1557 GtkStateType state_type,
1558 GtkShadowType shadow_type,
1559 const gchar *main_detail,
1560 const gchar *default_detail)
1562 GtkButtonPrivate *priv = button->priv;
1565 GtkBorder default_border;
1566 GtkBorder default_outside_border;
1567 gboolean interior_focus;
1570 GtkAllocation allocation;
1572 GtkStyleContext *context;
1573 GtkStateFlags state;
1575 widget = GTK_WIDGET (button);
1577 context = gtk_widget_get_style_context (widget);
1578 state = gtk_widget_get_state_flags (widget);
1580 gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1581 gtk_style_context_get_style (context,
1582 "focus-line-width", &focus_width,
1583 "focus-padding", &focus_pad,
1586 gtk_widget_get_allocation (widget, &allocation);
1587 window = gtk_widget_get_window (widget);
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 if (!interior_focus && gtk_widget_has_focus (widget))
1612 x += focus_width + focus_pad;
1613 y += focus_width + focus_pad;
1614 width -= 2 * (focus_width + focus_pad);
1615 height -= 2 * (focus_width + focus_pad);
1618 state = gtk_widget_get_state_flags (widget);
1619 gtk_style_context_set_state (context, state);
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);
1630 if (gtk_widget_has_focus (widget))
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,
1643 gtk_style_context_get (context, state,
1644 "border-width", &border,
1649 x += border->left + focus_pad;
1650 y += border->top + focus_pad;
1651 width -= (2 * focus_pad) + border->left + border->right;
1652 height -= (2 * focus_pad) + border->top + border->bottom;
1656 x -= focus_width + focus_pad;
1657 y -= focus_width + focus_pad;
1658 width += 2 * (focus_width + focus_pad);
1659 height += 2 * (focus_width + focus_pad);
1662 if (priv->depressed && displace_focus)
1664 x += child_displacement_x;
1665 y += child_displacement_y;
1668 gtk_render_focus (context, cr,
1669 x, y, width, height);
1671 gtk_border_free (border);
1676 gtk_button_draw (GtkWidget *widget,
1679 GtkButton *button = GTK_BUTTON (widget);
1680 GtkButtonPrivate *priv = button->priv;
1682 _gtk_button_paint (button, cr,
1683 gtk_widget_get_allocated_width (widget),
1684 gtk_widget_get_allocated_height (widget),
1685 gtk_widget_get_state (widget),
1686 priv->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1687 "button", "buttondefault");
1689 GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1695 gtk_button_button_press (GtkWidget *widget,
1696 GdkEventButton *event)
1699 GtkButtonPrivate *priv;
1701 if (event->type == GDK_BUTTON_PRESS)
1703 button = GTK_BUTTON (widget);
1704 priv = button->priv;
1706 if (priv->focus_on_click && !gtk_widget_has_focus (widget))
1707 gtk_widget_grab_focus (widget);
1709 if (event->button == 1)
1710 gtk_button_pressed (button);
1717 gtk_button_button_release (GtkWidget *widget,
1718 GdkEventButton *event)
1722 if (event->button == 1)
1724 button = GTK_BUTTON (widget);
1725 gtk_button_released (button);
1732 gtk_button_grab_broken (GtkWidget *widget,
1733 GdkEventGrabBroken *event)
1735 GtkButton *button = GTK_BUTTON (widget);
1736 GtkButtonPrivate *priv = button->priv;
1739 /* Simulate a button release without the pointer in the button */
1740 if (priv->button_down)
1742 save_in = priv->in_button;
1743 priv->in_button = FALSE;
1744 gtk_button_released (button);
1745 if (save_in != priv->in_button)
1747 priv->in_button = save_in;
1748 gtk_button_update_state (button);
1756 gtk_button_key_release (GtkWidget *widget,
1759 GtkButton *button = GTK_BUTTON (widget);
1760 GtkButtonPrivate *priv = button->priv;
1762 if (priv->activate_timeout)
1764 gtk_button_finish_activate (button, TRUE);
1767 else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1768 return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1774 gtk_button_enter_notify (GtkWidget *widget,
1775 GdkEventCrossing *event)
1777 GtkButton *button = GTK_BUTTON (widget);
1778 GtkButtonPrivate *priv = button->priv;
1780 if ((event->window == button->priv->event_window) &&
1781 (event->detail != GDK_NOTIFY_INFERIOR))
1783 priv->in_button = TRUE;
1784 gtk_button_enter (button);
1791 gtk_button_leave_notify (GtkWidget *widget,
1792 GdkEventCrossing *event)
1794 GtkButton *button = GTK_BUTTON (widget);
1795 GtkButtonPrivate *priv = button->priv;
1797 if ((event->window == button->priv->event_window) &&
1798 (event->detail != GDK_NOTIFY_INFERIOR) &&
1799 (gtk_widget_get_sensitive (widget)))
1801 priv->in_button = FALSE;
1802 gtk_button_leave (button);
1809 gtk_real_button_pressed (GtkButton *button)
1811 GtkButtonPrivate *priv = button->priv;
1813 if (priv->activate_timeout)
1816 priv->button_down = TRUE;
1817 gtk_button_update_state (button);
1821 gtk_real_button_released (GtkButton *button)
1823 GtkButtonPrivate *priv = button->priv;
1825 if (priv->button_down)
1827 priv->button_down = FALSE;
1829 if (priv->activate_timeout)
1832 if (priv->in_button)
1833 gtk_button_clicked (button);
1835 gtk_button_update_state (button);
1840 gtk_real_button_clicked (GtkButton *button)
1842 GtkButtonPrivate *priv = button->priv;
1845 gtk_action_activate (priv->action);
1849 button_activate_timeout (gpointer data)
1851 gtk_button_finish_activate (data, TRUE);
1857 gtk_real_button_activate (GtkButton *button)
1859 GtkWidget *widget = GTK_WIDGET (button);
1860 GtkButtonPrivate *priv = button->priv;
1864 device = gtk_get_current_event_device ();
1866 if (device && gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
1867 device = gdk_device_get_associated_device (device);
1869 g_return_if_fail (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD);
1871 if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
1873 time = gtk_get_current_event_time ();
1875 if (gdk_device_grab (device, priv->event_window,
1876 GDK_OWNERSHIP_WINDOW, TRUE,
1877 GDK_KEY_PRESS | GDK_KEY_RELEASE,
1878 NULL, time) == GDK_GRAB_SUCCESS)
1880 gtk_device_grab_add (widget, device, TRUE);
1881 priv->grab_keyboard = device;
1882 priv->grab_time = time;
1885 priv->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1886 button_activate_timeout,
1888 priv->button_down = TRUE;
1889 gtk_button_update_state (button);
1890 gtk_widget_queue_draw (GTK_WIDGET (button));
1895 gtk_button_finish_activate (GtkButton *button,
1898 GtkWidget *widget = GTK_WIDGET (button);
1899 GtkButtonPrivate *priv = button->priv;
1901 g_source_remove (priv->activate_timeout);
1902 priv->activate_timeout = 0;
1904 if (priv->grab_keyboard)
1906 gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1907 gtk_device_grab_remove (widget, priv->grab_keyboard);
1908 priv->grab_keyboard = NULL;
1911 priv->button_down = FALSE;
1913 gtk_button_update_state (button);
1914 gtk_widget_queue_draw (GTK_WIDGET (button));
1917 gtk_button_clicked (button);
1922 gtk_button_get_size (GtkWidget *widget,
1923 GtkOrientation orientation,
1927 GtkButton *button = GTK_BUTTON (widget);
1928 GtkStyleContext *context;
1929 GtkStateFlags state;
1931 GtkBorder default_border;
1932 GtkBorder inner_border;
1936 gint minimum, natural;
1938 context = gtk_widget_get_style_context (widget);
1939 state = gtk_widget_get_state_flags (widget);
1941 gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1942 gtk_style_context_get_style (context,
1943 "focus-line-width", &focus_width,
1944 "focus-padding", &focus_pad,
1947 gtk_style_context_get (context, state,
1948 "border-width", &border,
1951 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1953 minimum = (border->left + border->right +
1954 inner_border.left + inner_border.right);
1956 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1957 minimum += default_border.left + default_border.right;
1961 minimum = (border->top + border->bottom +
1962 inner_border.top + inner_border.bottom);
1964 if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1965 minimum += default_border.top + default_border.bottom;
1968 minimum += 2 * (focus_width + focus_pad);
1971 if ((child = gtk_bin_get_child (GTK_BIN (button))) &&
1972 gtk_widget_get_visible (child))
1974 gint child_min, child_nat;
1976 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1977 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
1979 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1981 minimum += child_min;
1982 natural += child_nat;
1986 *minimum_size = minimum;
1989 *natural_size = natural;
1991 gtk_border_free (border);
1995 gtk_button_get_preferred_width (GtkWidget *widget,
1999 gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2003 gtk_button_get_preferred_height (GtkWidget *widget,
2007 gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2011 * gtk_button_set_label:
2012 * @button: a #GtkButton
2015 * Sets the text of the label of the button to @str. This text is
2016 * also used to select the stock item if gtk_button_set_use_stock()
2019 * This will also clear any previously set labels.
2022 gtk_button_set_label (GtkButton *button,
2025 GtkButtonPrivate *priv;
2028 g_return_if_fail (GTK_IS_BUTTON (button));
2030 priv = button->priv;
2032 new_label = g_strdup (label);
2033 g_free (priv->label_text);
2034 priv->label_text = new_label;
2036 gtk_button_construct_child (button);
2038 g_object_notify (G_OBJECT (button), "label");
2042 * gtk_button_get_label:
2043 * @button: a #GtkButton
2045 * Fetches the text from the label of the button, as set by
2046 * gtk_button_set_label(). If the label text has not
2047 * been set the return value will be %NULL. This will be the
2048 * case if you create an empty button with gtk_button_new() to
2049 * use as a container.
2051 * Return value: The text of the label widget. This string is owned
2052 * by the widget and must not be modified or freed.
2054 G_CONST_RETURN gchar *
2055 gtk_button_get_label (GtkButton *button)
2057 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2059 return button->priv->label_text;
2063 * gtk_button_set_use_underline:
2064 * @button: a #GtkButton
2065 * @use_underline: %TRUE if underlines in the text indicate mnemonics
2067 * If true, an underline in the text of the button label indicates
2068 * the next character should be used for the mnemonic accelerator key.
2071 gtk_button_set_use_underline (GtkButton *button,
2072 gboolean use_underline)
2074 GtkButtonPrivate *priv;
2076 g_return_if_fail (GTK_IS_BUTTON (button));
2078 priv = button->priv;
2080 use_underline = use_underline != FALSE;
2082 if (use_underline != priv->use_underline)
2084 priv->use_underline = use_underline;
2086 gtk_button_construct_child (button);
2088 g_object_notify (G_OBJECT (button), "use-underline");
2093 * gtk_button_get_use_underline:
2094 * @button: a #GtkButton
2096 * Returns whether an embedded underline in the button label indicates a
2097 * mnemonic. See gtk_button_set_use_underline ().
2099 * Return value: %TRUE if an embedded underline in the button label
2100 * indicates the mnemonic accelerator keys.
2103 gtk_button_get_use_underline (GtkButton *button)
2105 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2107 return button->priv->use_underline;
2111 * gtk_button_set_use_stock:
2112 * @button: a #GtkButton
2113 * @use_stock: %TRUE if the button should use a stock item
2115 * If %TRUE, the label set on the button is used as a
2116 * stock id to select the stock item for the button.
2119 gtk_button_set_use_stock (GtkButton *button,
2122 GtkButtonPrivate *priv;
2124 g_return_if_fail (GTK_IS_BUTTON (button));
2126 priv = button->priv;
2128 use_stock = use_stock != FALSE;
2130 if (use_stock != priv->use_stock)
2132 priv->use_stock = use_stock;
2134 gtk_button_construct_child (button);
2136 g_object_notify (G_OBJECT (button), "use-stock");
2141 * gtk_button_get_use_stock:
2142 * @button: a #GtkButton
2144 * Returns whether the button label is a stock item.
2146 * Return value: %TRUE if the button label is used to
2147 * select a stock item instead of being
2148 * used directly as the label text.
2151 gtk_button_get_use_stock (GtkButton *button)
2153 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2155 return button->priv->use_stock;
2159 * gtk_button_set_focus_on_click:
2160 * @button: a #GtkButton
2161 * @focus_on_click: whether the button grabs focus when clicked with the mouse
2163 * Sets whether the button will grab focus when it is clicked with the mouse.
2164 * Making mouse clicks not grab focus is useful in places like toolbars where
2165 * you don't want the keyboard focus removed from the main area of the
2171 gtk_button_set_focus_on_click (GtkButton *button,
2172 gboolean focus_on_click)
2174 GtkButtonPrivate *priv;
2176 g_return_if_fail (GTK_IS_BUTTON (button));
2178 priv = button->priv;
2180 focus_on_click = focus_on_click != FALSE;
2182 if (priv->focus_on_click != focus_on_click)
2184 priv->focus_on_click = focus_on_click;
2186 g_object_notify (G_OBJECT (button), "focus-on-click");
2191 * gtk_button_get_focus_on_click:
2192 * @button: a #GtkButton
2194 * Returns whether the button grabs focus when it is clicked with the mouse.
2195 * See gtk_button_set_focus_on_click().
2197 * Return value: %TRUE if the button grabs focus when it is clicked with
2203 gtk_button_get_focus_on_click (GtkButton *button)
2205 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2207 return button->priv->focus_on_click;
2211 * gtk_button_set_alignment:
2212 * @button: a #GtkButton
2213 * @xalign: the horizontal position of the child, 0.0 is left aligned,
2214 * 1.0 is right aligned
2215 * @yalign: the vertical position of the child, 0.0 is top aligned,
2216 * 1.0 is bottom aligned
2218 * Sets the alignment of the child. This property has no effect unless
2219 * the child is a #GtkMisc or a #GtkAlignment.
2224 gtk_button_set_alignment (GtkButton *button,
2228 GtkButtonPrivate *priv;
2230 g_return_if_fail (GTK_IS_BUTTON (button));
2232 priv = button->priv;
2234 priv->xalign = xalign;
2235 priv->yalign = yalign;
2236 priv->align_set = 1;
2238 maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2240 g_object_freeze_notify (G_OBJECT (button));
2241 g_object_notify (G_OBJECT (button), "xalign");
2242 g_object_notify (G_OBJECT (button), "yalign");
2243 g_object_thaw_notify (G_OBJECT (button));
2247 * gtk_button_get_alignment:
2248 * @button: a #GtkButton
2249 * @xalign: return location for horizontal alignment
2250 * @yalign: return location for vertical alignment
2252 * Gets the alignment of the child in the button.
2257 gtk_button_get_alignment (GtkButton *button,
2261 GtkButtonPrivate *priv;
2263 g_return_if_fail (GTK_IS_BUTTON (button));
2265 priv = button->priv;
2268 *xalign = priv->xalign;
2271 *yalign = priv->yalign;
2275 * _gtk_button_set_depressed:
2276 * @button: a #GtkButton
2277 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2279 * Sets whether the button is currently drawn as down or not. This is
2280 * purely a visual setting, and is meant only for use by derived widgets
2281 * such as #GtkToggleButton.
2284 _gtk_button_set_depressed (GtkButton *button,
2287 GtkWidget *widget = GTK_WIDGET (button);
2288 GtkButtonPrivate *priv = button->priv;
2290 depressed = depressed != FALSE;
2292 if (depressed != priv->depressed)
2294 priv->depressed = depressed;
2295 gtk_widget_queue_resize (widget);
2300 gtk_button_update_state (GtkButton *button)
2302 GtkButtonPrivate *priv = button->priv;
2303 GtkStateFlags new_state = 0;
2306 if (priv->activate_timeout)
2307 depressed = priv->depress_on_activate;
2309 depressed = priv->in_button && priv->button_down;
2311 if (priv->in_button)
2312 new_state |= GTK_STATE_FLAG_PRELIGHT;
2314 if (priv->button_down || depressed)
2315 new_state |= GTK_STATE_FLAG_ACTIVE;
2317 _gtk_button_set_depressed (button, depressed);
2318 gtk_widget_set_state_flags (GTK_WIDGET (button), new_state, TRUE);
2322 show_image_change_notify (GtkButton *button)
2324 GtkButtonPrivate *priv = button->priv;
2328 if (show_image (button))
2329 gtk_widget_show (priv->image);
2331 gtk_widget_hide (priv->image);
2336 traverse_container (GtkWidget *widget,
2339 if (GTK_IS_BUTTON (widget))
2340 show_image_change_notify (GTK_BUTTON (widget));
2341 else if (GTK_IS_CONTAINER (widget))
2342 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2346 gtk_button_setting_changed (GtkSettings *settings)
2350 list = gtk_window_list_toplevels ();
2352 for (l = list; l; l = l->next)
2353 gtk_container_forall (GTK_CONTAINER (l->data),
2354 traverse_container, NULL);
2361 gtk_button_screen_changed (GtkWidget *widget,
2362 GdkScreen *previous_screen)
2365 GtkButtonPrivate *priv;
2366 GtkSettings *settings;
2367 gulong show_image_connection;
2369 if (!gtk_widget_has_screen (widget))
2372 button = GTK_BUTTON (widget);
2373 priv = button->priv;
2375 /* If the button is being pressed while the screen changes the
2376 release might never occur, so we reset the state. */
2377 if (priv->button_down)
2379 priv->button_down = FALSE;
2380 gtk_button_update_state (button);
2383 settings = gtk_widget_get_settings (widget);
2385 show_image_connection =
2386 g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2387 NULL, gtk_button_setting_changed, NULL);
2389 if (show_image_connection)
2392 g_signal_connect (settings, "notify::gtk-button-images",
2393 G_CALLBACK (gtk_button_setting_changed), NULL);
2395 show_image_change_notify (button);
2399 gtk_button_state_changed (GtkWidget *widget,
2400 GtkStateType previous_state)
2402 GtkButton *button = GTK_BUTTON (widget);
2403 GtkButtonPrivate *priv = button->priv;
2405 if (!gtk_widget_is_sensitive (widget))
2407 priv->in_button = FALSE;
2408 gtk_real_button_released (button);
2413 gtk_button_grab_notify (GtkWidget *widget,
2414 gboolean was_grabbed)
2416 GtkButton *button = GTK_BUTTON (widget);
2417 GtkButtonPrivate *priv = button->priv;
2420 if (priv->activate_timeout &&
2421 priv->grab_keyboard &&
2422 gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2423 gtk_button_finish_activate (button, FALSE);
2427 save_in = priv->in_button;
2428 priv->in_button = FALSE;
2429 gtk_real_button_released (button);
2430 if (save_in != priv->in_button)
2432 priv->in_button = save_in;
2433 gtk_button_update_state (button);
2439 * gtk_button_set_image:
2440 * @button: a #GtkButton
2441 * @image: a widget to set as the image for the button
2443 * Set the image of @button to the given widget. Note that
2444 * it depends on the #GtkSettings:gtk-button-images setting whether the
2445 * image will be displayed or not, you don't have to call
2446 * gtk_widget_show() on @image yourself.
2451 gtk_button_set_image (GtkButton *button,
2454 GtkButtonPrivate *priv;
2457 g_return_if_fail (GTK_IS_BUTTON (button));
2458 g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2460 priv = button->priv;
2464 parent = gtk_widget_get_parent (priv->image);
2466 gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2469 priv->image = image;
2470 priv->image_is_stock = (image == NULL);
2472 gtk_button_construct_child (button);
2474 g_object_notify (G_OBJECT (button), "image");
2478 * gtk_button_get_image:
2479 * @button: a #GtkButton
2481 * Gets the widget that is currenty set as the image of @button.
2482 * This may have been explicitly set by gtk_button_set_image()
2483 * or constructed by gtk_button_new_from_stock().
2485 * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2490 gtk_button_get_image (GtkButton *button)
2492 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2494 return button->priv->image;
2498 * gtk_button_set_image_position:
2499 * @button: a #GtkButton
2500 * @position: the position
2502 * Sets the position of the image relative to the text
2503 * inside the button.
2508 gtk_button_set_image_position (GtkButton *button,
2509 GtkPositionType position)
2511 GtkButtonPrivate *priv;
2513 g_return_if_fail (GTK_IS_BUTTON (button));
2514 g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2516 priv = button->priv;
2518 if (priv->image_position != position)
2520 priv->image_position = position;
2522 gtk_button_construct_child (button);
2524 g_object_notify (G_OBJECT (button), "image-position");
2529 * gtk_button_get_image_position:
2530 * @button: a #GtkButton
2532 * Gets the position of the image relative to the text
2533 * inside the button.
2535 * Return value: the position
2540 gtk_button_get_image_position (GtkButton *button)
2542 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2544 return button->priv->image_position;
2549 * gtk_button_get_event_window:
2550 * @button: a #GtkButton
2552 * Returns the button's event window if it is realized, %NULL otherwise.
2553 * This function should be rarely needed.
2555 * Return value: (transfer none): @button's event window.
2560 gtk_button_get_event_window (GtkButton *button)
2562 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2564 return button->priv->event_window;