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 #include "gtkalignment.h"
30 #include "gtkbutton.h"
33 #include "gtkmarshalers.h"
37 #include "gtkiconfactory.h"
38 #include "gtkprivate.h"
42 #define CHILD_SPACING 1
44 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
45 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
47 /* Time out before giving up on getting a key release when animating
50 #define ACTIVATE_TIMEOUT 250
74 #define GTK_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_BUTTON, GtkButtonPrivate))
75 typedef struct _GtkButtonPrivate GtkButtonPrivate;
77 struct _GtkButtonPrivate
83 guint image_is_stock : 1;
88 static void gtk_button_class_init (GtkButtonClass *klass);
89 static void gtk_button_init (GtkButton *button);
90 static void gtk_button_destroy (GtkObject *object);
91 static void gtk_button_set_property (GObject *object,
95 static void gtk_button_get_property (GObject *object,
99 static void gtk_button_screen_changed (GtkWidget *widget,
100 GdkScreen *previous_screen);
101 static void gtk_button_realize (GtkWidget *widget);
102 static void gtk_button_unrealize (GtkWidget *widget);
103 static void gtk_button_map (GtkWidget *widget);
104 static void gtk_button_unmap (GtkWidget *widget);
105 static void gtk_button_size_request (GtkWidget *widget,
106 GtkRequisition *requisition);
107 static void gtk_button_size_allocate (GtkWidget *widget,
108 GtkAllocation *allocation);
109 static gint gtk_button_expose (GtkWidget *widget,
110 GdkEventExpose *event);
111 static gint gtk_button_button_press (GtkWidget *widget,
112 GdkEventButton *event);
113 static gint gtk_button_button_release (GtkWidget *widget,
114 GdkEventButton *event);
115 static gint gtk_button_grab_broken (GtkWidget *widget,
116 GdkEventGrabBroken *event);
117 static gint gtk_button_key_release (GtkWidget *widget,
119 static gint gtk_button_enter_notify (GtkWidget *widget,
120 GdkEventCrossing *event);
121 static gint gtk_button_leave_notify (GtkWidget *widget,
122 GdkEventCrossing *event);
123 static void gtk_real_button_pressed (GtkButton *button);
124 static void gtk_real_button_released (GtkButton *button);
125 static void gtk_real_button_activate (GtkButton *button);
126 static void gtk_button_update_state (GtkButton *button);
127 static void gtk_button_add (GtkContainer *container,
129 static GType gtk_button_child_type (GtkContainer *container);
130 static void gtk_button_finish_activate (GtkButton *button,
133 static GObject* gtk_button_constructor (GType type,
134 guint n_construct_properties,
135 GObjectConstructParam *construct_params);
136 static void gtk_button_construct_child (GtkButton *button);
137 static void gtk_button_state_changed (GtkWidget *widget,
138 GtkStateType previous_state);
139 static void gtk_button_grab_notify (GtkWidget *widget,
140 gboolean was_grabbed);
144 static GtkBinClass *parent_class = NULL;
145 static guint button_signals[LAST_SIGNAL] = { 0 };
149 gtk_button_get_type (void)
151 static GType button_type = 0;
155 static const GTypeInfo button_info =
157 sizeof (GtkButtonClass),
158 NULL, /* base_init */
159 NULL, /* base_finalize */
160 (GClassInitFunc) gtk_button_class_init,
161 NULL, /* class_finalize */
162 NULL, /* class_data */
164 16, /* n_preallocs */
165 (GInstanceInitFunc) gtk_button_init,
168 button_type = g_type_register_static (GTK_TYPE_BIN, "GtkButton",
176 gtk_button_class_init (GtkButtonClass *klass)
178 GObjectClass *gobject_class;
179 GtkObjectClass *object_class;
180 GtkWidgetClass *widget_class;
181 GtkContainerClass *container_class;
183 gobject_class = G_OBJECT_CLASS (klass);
184 object_class = (GtkObjectClass*) klass;
185 widget_class = (GtkWidgetClass*) klass;
186 container_class = (GtkContainerClass*) klass;
188 parent_class = g_type_class_peek_parent (klass);
190 gobject_class->constructor = gtk_button_constructor;
191 gobject_class->set_property = gtk_button_set_property;
192 gobject_class->get_property = gtk_button_get_property;
194 object_class->destroy = gtk_button_destroy;
196 widget_class->screen_changed = gtk_button_screen_changed;
197 widget_class->realize = gtk_button_realize;
198 widget_class->unrealize = gtk_button_unrealize;
199 widget_class->map = gtk_button_map;
200 widget_class->unmap = gtk_button_unmap;
201 widget_class->size_request = gtk_button_size_request;
202 widget_class->size_allocate = gtk_button_size_allocate;
203 widget_class->expose_event = gtk_button_expose;
204 widget_class->button_press_event = gtk_button_button_press;
205 widget_class->button_release_event = gtk_button_button_release;
206 widget_class->grab_broken_event = gtk_button_grab_broken;
207 widget_class->key_release_event = gtk_button_key_release;
208 widget_class->enter_notify_event = gtk_button_enter_notify;
209 widget_class->leave_notify_event = gtk_button_leave_notify;
210 widget_class->state_changed = gtk_button_state_changed;
211 widget_class->grab_notify = gtk_button_grab_notify;
213 container_class->child_type = gtk_button_child_type;
214 container_class->add = gtk_button_add;
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::pressed:
319 * @button: the object that received the signal
321 * Emitted when the button is pressed.
323 * @Deprecated: Use the GtkWidget::button-press-event signal.
325 button_signals[PRESSED] =
326 g_signal_new ("pressed",
327 G_OBJECT_CLASS_TYPE (object_class),
329 G_STRUCT_OFFSET (GtkButtonClass, pressed),
331 _gtk_marshal_VOID__VOID,
335 * GtkButton::released:
336 * @button: the object that received the signal
338 * Emitted when the button is released.
340 * @Deprecated: Use the GtkWidget::button-release-event signal.
342 button_signals[RELEASED] =
343 g_signal_new ("released",
344 G_OBJECT_CLASS_TYPE (object_class),
346 G_STRUCT_OFFSET (GtkButtonClass, released),
348 _gtk_marshal_VOID__VOID,
352 * GtkButton::clicked:
353 * @button: the object that received the signal
355 * Emitted when the button has been activated (pressed and released).
357 button_signals[CLICKED] =
358 g_signal_new ("clicked",
359 G_OBJECT_CLASS_TYPE (object_class),
360 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
361 G_STRUCT_OFFSET (GtkButtonClass, clicked),
363 _gtk_marshal_VOID__VOID,
368 * @button: the object that received the signal
370 * Emitted when the pointer enters the button.
372 * @Deprecated: Use the GtkWidget::enter-notify-event signal.
374 button_signals[ENTER] =
375 g_signal_new ("enter",
376 G_OBJECT_CLASS_TYPE (object_class),
378 G_STRUCT_OFFSET (GtkButtonClass, enter),
380 _gtk_marshal_VOID__VOID,
385 * @button: the object that received the signal
387 * Emitted when the pointer leaves the button.
389 * @Deprecated: Use the GtkWidget::leave-notify-event signal.
391 button_signals[LEAVE] =
392 g_signal_new ("leave",
393 G_OBJECT_CLASS_TYPE (object_class),
395 G_STRUCT_OFFSET (GtkButtonClass, leave),
397 _gtk_marshal_VOID__VOID,
401 * GtkButton::activate:
402 * @widget: the object which received the signal.
404 * The "activate" signal on GtkButton is an action signal and
405 * emitting it causes the button to animate press then release.
406 * Applications should never connect to this signal, but use the
409 button_signals[ACTIVATE] =
410 g_signal_new ("activate",
411 G_OBJECT_CLASS_TYPE (object_class),
412 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
413 G_STRUCT_OFFSET (GtkButtonClass, activate),
415 _gtk_marshal_VOID__VOID,
417 widget_class->activate_signal = button_signals[ACTIVATE];
419 gtk_widget_class_install_style_property (widget_class,
420 g_param_spec_boxed ("default-border",
421 P_("Default Spacing"),
422 P_("Extra space to add for CAN_DEFAULT buttons"),
424 GTK_PARAM_READABLE));
426 gtk_widget_class_install_style_property (widget_class,
427 g_param_spec_boxed ("default-outside-border",
428 P_("Default Outside Spacing"),
429 P_("Extra space to add for CAN_DEFAULT buttons that is always drawn outside the border"),
431 GTK_PARAM_READABLE));
432 gtk_widget_class_install_style_property (widget_class,
433 g_param_spec_int ("child-displacement-x",
434 P_("Child X Displacement"),
435 P_("How far in the x direction to move the child when the button is depressed"),
439 GTK_PARAM_READABLE));
440 gtk_widget_class_install_style_property (widget_class,
441 g_param_spec_int ("child-displacement-y",
442 P_("Child Y Displacement"),
443 P_("How far in the y direction to move the child when the button is depressed"),
447 GTK_PARAM_READABLE));
450 * GtkButton:displace-focus:
452 * Whether the child_displacement_x/child_displacement_y properties should also
453 * affect the focus rectangle.
457 gtk_widget_class_install_style_property (widget_class,
458 g_param_spec_boolean ("displace-focus",
459 P_("Displace focus"),
460 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
462 GTK_PARAM_READABLE));
464 gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
465 P_("Show button images"),
466 P_("Whether stock icons should be shown in buttons"),
468 GTK_PARAM_READWRITE));
470 g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
474 gtk_button_init (GtkButton *button)
476 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
478 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
479 GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);
481 button->label_text = NULL;
483 button->constructed = FALSE;
484 button->in_button = FALSE;
485 button->button_down = FALSE;
486 button->relief = GTK_RELIEF_NORMAL;
487 button->use_stock = FALSE;
488 button->use_underline = FALSE;
489 button->depressed = FALSE;
490 button->depress_on_activate = TRUE;
491 button->focus_on_click = TRUE;
496 priv->image_is_stock = TRUE;
500 gtk_button_destroy (GtkObject *object)
502 GtkButton *button = GTK_BUTTON (object);
504 if (button->label_text)
506 g_free (button->label_text);
507 button->label_text = NULL;
510 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
514 gtk_button_constructor (GType type,
515 guint n_construct_properties,
516 GObjectConstructParam *construct_params)
521 object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
522 n_construct_properties,
525 button = GTK_BUTTON (object);
526 button->constructed = TRUE;
528 if (button->label_text != NULL)
529 gtk_button_construct_child (button);
536 gtk_button_child_type (GtkContainer *container)
538 if (!GTK_BIN (container)->child)
539 return GTK_TYPE_WIDGET;
545 maybe_set_alignment (GtkButton *button,
548 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
550 if (GTK_IS_MISC (widget))
552 GtkMisc *misc = GTK_MISC (widget);
555 gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
557 else if (GTK_IS_ALIGNMENT (widget))
559 GtkAlignment *alignment = GTK_ALIGNMENT (widget);
562 gtk_alignment_set (alignment, priv->xalign, priv->yalign,
563 alignment->xscale, alignment->yscale);
568 gtk_button_add (GtkContainer *container,
571 maybe_set_alignment (GTK_BUTTON (container), widget);
573 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
577 gtk_button_set_property (GObject *object,
582 GtkButton *button = GTK_BUTTON (object);
583 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
588 gtk_button_set_label (button, g_value_get_string (value));
591 gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
594 gtk_button_set_relief (button, g_value_get_enum (value));
596 case PROP_USE_UNDERLINE:
597 gtk_button_set_use_underline (button, g_value_get_boolean (value));
600 gtk_button_set_use_stock (button, g_value_get_boolean (value));
602 case PROP_FOCUS_ON_CLICK:
603 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
606 gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
609 gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
612 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
618 gtk_button_get_property (GObject *object,
623 GtkButton *button = GTK_BUTTON (object);
624 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
629 g_value_set_string (value, button->label_text);
632 g_value_set_object (value, (GObject *)priv->image);
635 g_value_set_enum (value, gtk_button_get_relief (button));
637 case PROP_USE_UNDERLINE:
638 g_value_set_boolean (value, button->use_underline);
641 g_value_set_boolean (value, button->use_stock);
643 case PROP_FOCUS_ON_CLICK:
644 g_value_set_boolean (value, button->focus_on_click);
647 g_value_set_float (value, priv->xalign);
650 g_value_set_float (value, priv->yalign);
653 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
659 gtk_button_new (void)
661 return g_object_new (GTK_TYPE_BUTTON, NULL);
665 show_image (GtkButton *button)
667 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (button));
670 g_object_get (settings, "gtk-button-images", &show, NULL);
676 gtk_button_construct_child (GtkButton *button)
678 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
683 GtkWidget *image = NULL;
684 gchar *label_text = NULL;
686 if (!button->constructed)
689 if (button->label_text == NULL)
692 if (GTK_BIN (button)->child)
694 if (priv->image && !priv->image_is_stock)
696 image = g_object_ref (priv->image);
698 gtk_container_remove (GTK_CONTAINER (image->parent), image);
701 gtk_container_remove (GTK_CONTAINER (button),
702 GTK_BIN (button)->child);
707 if (button->use_stock &&
708 gtk_stock_lookup (button->label_text, &item))
711 image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
713 label_text = item.label;
716 label_text = button->label_text;
720 label = gtk_label_new_with_mnemonic (label_text);
721 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
725 g_object_set (priv->image,
726 "visible", show_image (button),
729 hbox = gtk_hbox_new (FALSE, 2);
732 align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
734 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
736 gtk_box_pack_start (GTK_BOX (hbox), priv->image, FALSE, FALSE, 0);
737 gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
739 gtk_container_add (GTK_CONTAINER (button), align);
740 gtk_container_add (GTK_CONTAINER (align), hbox);
741 gtk_widget_show_all (align);
743 g_object_unref (image);
748 if (button->use_underline)
750 label = gtk_label_new_with_mnemonic (button->label_text);
751 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
754 label = gtk_label_new (button->label_text);
757 gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
759 gtk_widget_show (label);
760 gtk_container_add (GTK_CONTAINER (button), label);
765 gtk_button_new_with_label (const gchar *label)
767 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
771 * gtk_button_new_from_stock:
772 * @stock_id: the name of the stock item
774 * Creates a new #GtkButton containing the image and text from a stock item.
775 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
778 * If @stock_id is unknown, then it will be treated as a mnemonic
779 * label (as for gtk_button_new_with_mnemonic()).
781 * Returns: a new #GtkButton
784 gtk_button_new_from_stock (const gchar *stock_id)
786 return g_object_new (GTK_TYPE_BUTTON,
789 "use-underline", TRUE,
794 * gtk_button_new_with_mnemonic:
795 * @label: The text of the button, with an underscore in front of the
797 * @returns: a new #GtkButton
799 * Creates a new #GtkButton containing a label.
800 * If characters in @label are preceded by an underscore, they are underlined.
801 * If you need a literal underscore character in a label, use '__' (two
802 * underscores). The first underlined character represents a keyboard
803 * accelerator called a mnemonic.
804 * Pressing Alt and that key activates the button.
807 gtk_button_new_with_mnemonic (const gchar *label)
809 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
813 gtk_button_pressed (GtkButton *button)
815 g_return_if_fail (GTK_IS_BUTTON (button));
818 g_signal_emit (button, button_signals[PRESSED], 0);
822 gtk_button_released (GtkButton *button)
824 g_return_if_fail (GTK_IS_BUTTON (button));
826 g_signal_emit (button, button_signals[RELEASED], 0);
830 gtk_button_clicked (GtkButton *button)
832 g_return_if_fail (GTK_IS_BUTTON (button));
834 g_signal_emit (button, button_signals[CLICKED], 0);
838 gtk_button_enter (GtkButton *button)
840 g_return_if_fail (GTK_IS_BUTTON (button));
842 g_signal_emit (button, button_signals[ENTER], 0);
846 gtk_button_leave (GtkButton *button)
848 g_return_if_fail (GTK_IS_BUTTON (button));
850 g_signal_emit (button, button_signals[LEAVE], 0);
854 gtk_button_set_relief (GtkButton *button,
855 GtkReliefStyle newrelief)
857 g_return_if_fail (GTK_IS_BUTTON (button));
859 if (newrelief != button->relief)
861 button->relief = newrelief;
862 g_object_notify (G_OBJECT (button), "relief");
863 gtk_widget_queue_draw (GTK_WIDGET (button));
868 gtk_button_get_relief (GtkButton *button)
870 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
872 return button->relief;
876 gtk_button_realize (GtkWidget *widget)
879 GdkWindowAttr attributes;
880 gint attributes_mask;
883 button = GTK_BUTTON (widget);
884 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
886 border_width = GTK_CONTAINER (widget)->border_width;
888 attributes.window_type = GDK_WINDOW_CHILD;
889 attributes.x = widget->allocation.x + border_width;
890 attributes.y = widget->allocation.y + border_width;
891 attributes.width = widget->allocation.width - border_width * 2;
892 attributes.height = widget->allocation.height - border_width * 2;
893 attributes.wclass = GDK_INPUT_ONLY;
894 attributes.event_mask = gtk_widget_get_events (widget);
895 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
896 GDK_BUTTON_RELEASE_MASK |
897 GDK_ENTER_NOTIFY_MASK |
898 GDK_LEAVE_NOTIFY_MASK);
900 attributes_mask = GDK_WA_X | GDK_WA_Y;
902 widget->window = gtk_widget_get_parent_window (widget);
903 g_object_ref (widget->window);
905 button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
906 &attributes, attributes_mask);
907 gdk_window_set_user_data (button->event_window, button);
909 widget->style = gtk_style_attach (widget->style, widget->window);
913 gtk_button_unrealize (GtkWidget *widget)
915 GtkButton *button = GTK_BUTTON (widget);
917 if (button->activate_timeout)
918 gtk_button_finish_activate (button, FALSE);
920 if (button->event_window)
922 gdk_window_set_user_data (button->event_window, NULL);
923 gdk_window_destroy (button->event_window);
924 button->event_window = NULL;
927 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
931 gtk_button_map (GtkWidget *widget)
933 GtkButton *button = GTK_BUTTON (widget);
935 GTK_WIDGET_CLASS (parent_class)->map (widget);
937 if (button->event_window)
938 gdk_window_show (button->event_window);
942 gtk_button_unmap (GtkWidget *widget)
944 GtkButton *button = GTK_BUTTON (widget);
946 if (button->event_window)
947 gdk_window_hide (button->event_window);
949 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
953 gtk_button_get_props (GtkButton *button,
954 GtkBorder *default_border,
955 GtkBorder *default_outside_border,
956 gboolean *interior_focus)
958 GtkWidget *widget = GTK_WIDGET (button);
959 GtkBorder *tmp_border;
963 gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
967 *default_border = *tmp_border;
971 *default_border = default_default_border;
974 if (default_outside_border)
976 gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
980 *default_outside_border = *tmp_border;
984 *default_outside_border = default_default_outside_border;
988 gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
992 gtk_button_size_request (GtkWidget *widget,
993 GtkRequisition *requisition)
995 GtkButton *button = GTK_BUTTON (widget);
996 GtkBorder default_border;
1000 gtk_button_get_props (button, &default_border, NULL, NULL);
1001 gtk_widget_style_get (GTK_WIDGET (widget),
1002 "focus-line-width", &focus_width,
1003 "focus-padding", &focus_pad,
1006 requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
1007 GTK_WIDGET (widget)->style->xthickness) * 2;
1008 requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
1009 GTK_WIDGET (widget)->style->ythickness) * 2;
1011 if (GTK_WIDGET_CAN_DEFAULT (widget))
1013 requisition->width += default_border.left + default_border.right;
1014 requisition->height += default_border.top + default_border.bottom;
1017 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
1019 GtkRequisition child_requisition;
1021 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
1023 requisition->width += child_requisition.width;
1024 requisition->height += child_requisition.height;
1027 requisition->width += 2 * (focus_width + focus_pad);
1028 requisition->height += 2 * (focus_width + focus_pad);
1032 gtk_button_size_allocate (GtkWidget *widget,
1033 GtkAllocation *allocation)
1035 GtkButton *button = GTK_BUTTON (widget);
1036 GtkAllocation child_allocation;
1038 gint border_width = GTK_CONTAINER (widget)->border_width;
1039 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
1040 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
1041 GtkBorder default_border;
1045 gtk_button_get_props (button, &default_border, NULL, NULL);
1046 gtk_widget_style_get (GTK_WIDGET (widget),
1047 "focus-line-width", &focus_width,
1048 "focus-padding", &focus_pad,
1052 widget->allocation = *allocation;
1054 if (GTK_WIDGET_REALIZED (widget))
1055 gdk_window_move_resize (button->event_window,
1056 widget->allocation.x + border_width,
1057 widget->allocation.y + border_width,
1058 widget->allocation.width - border_width * 2,
1059 widget->allocation.height - border_width * 2);
1061 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
1063 child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
1064 child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
1066 child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
1068 child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
1071 if (GTK_WIDGET_CAN_DEFAULT (button))
1073 child_allocation.x += default_border.left;
1074 child_allocation.y += default_border.top;
1075 child_allocation.width = MAX (1, child_allocation.width - default_border.left - default_border.right);
1076 child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
1079 if (GTK_WIDGET_CAN_FOCUS (button))
1081 child_allocation.x += focus_width + focus_pad;
1082 child_allocation.y += focus_width + focus_pad;
1083 child_allocation.width = MAX (1, child_allocation.width - (focus_width + focus_pad) * 2);
1084 child_allocation.height = MAX (1, child_allocation.height - (focus_width + focus_pad) * 2);
1087 if (button->depressed)
1089 gint child_displacement_x;
1090 gint child_displacement_y;
1092 gtk_widget_style_get (widget,
1093 "child-displacement-x", &child_displacement_x,
1094 "child-displacement-y", &child_displacement_y,
1096 child_allocation.x += child_displacement_x;
1097 child_allocation.y += child_displacement_y;
1100 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
1105 _gtk_button_paint (GtkButton *button,
1107 GtkStateType state_type,
1108 GtkShadowType shadow_type,
1109 const gchar *main_detail,
1110 const gchar *default_detail)
1116 GtkBorder default_border;
1117 GtkBorder default_outside_border;
1118 gboolean interior_focus;
1122 if (GTK_WIDGET_DRAWABLE (button))
1124 widget = GTK_WIDGET (button);
1125 border_width = GTK_CONTAINER (widget)->border_width;
1127 gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
1128 gtk_widget_style_get (GTK_WIDGET (widget),
1129 "focus-line-width", &focus_width,
1130 "focus-padding", &focus_pad,
1133 x = widget->allocation.x + border_width;
1134 y = widget->allocation.y + border_width;
1135 width = widget->allocation.width - border_width * 2;
1136 height = widget->allocation.height - border_width * 2;
1138 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
1139 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1141 gtk_paint_box (widget->style, widget->window,
1142 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1143 area, widget, "buttondefault",
1144 x, y, width, height);
1146 x += default_border.left;
1147 y += default_border.top;
1148 width -= default_border.left + default_border.right;
1149 height -= default_border.top + default_border.bottom;
1151 else if (GTK_WIDGET_CAN_DEFAULT (widget))
1153 x += default_outside_border.left;
1154 y += default_outside_border.top;
1155 width -= default_outside_border.left + default_outside_border.right;
1156 height -= default_outside_border.top + default_outside_border.bottom;
1159 if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
1161 x += focus_width + focus_pad;
1162 y += focus_width + focus_pad;
1163 width -= 2 * (focus_width + focus_pad);
1164 height -= 2 * (focus_width + focus_pad);
1167 if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1168 GTK_WIDGET_STATE(widget) == GTK_STATE_PRELIGHT)
1169 gtk_paint_box (widget->style, widget->window,
1171 shadow_type, area, widget, "button",
1172 x, y, width, height);
1174 if (GTK_WIDGET_HAS_FOCUS (widget))
1176 gint child_displacement_x;
1177 gint child_displacement_y;
1178 gboolean displace_focus;
1180 gtk_widget_style_get (GTK_WIDGET (widget),
1181 "child-displacement-y", &child_displacement_y,
1182 "child-displacement-x", &child_displacement_x,
1183 "displace-focus", &displace_focus,
1188 x += widget->style->xthickness + focus_pad;
1189 y += widget->style->ythickness + focus_pad;
1190 width -= 2 * (widget->style->xthickness + focus_pad);
1191 height -= 2 * (widget->style->ythickness + focus_pad);
1195 x -= focus_width + focus_pad;
1196 y -= focus_width + focus_pad;
1197 width += 2 * (focus_width + focus_pad);
1198 height += 2 * (focus_width + focus_pad);
1201 if (button->depressed && displace_focus)
1203 x += child_displacement_x;
1204 y += child_displacement_y;
1207 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1208 area, widget, "button",
1209 x, y, width, height);
1215 gtk_button_expose (GtkWidget *widget,
1216 GdkEventExpose *event)
1218 if (GTK_WIDGET_DRAWABLE (widget))
1220 GtkButton *button = GTK_BUTTON (widget);
1222 _gtk_button_paint (button, &event->area,
1223 GTK_WIDGET_STATE (widget),
1224 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1225 "button", "buttondefault");
1227 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1234 gtk_button_button_press (GtkWidget *widget,
1235 GdkEventButton *event)
1239 if (event->type == GDK_BUTTON_PRESS)
1241 button = GTK_BUTTON (widget);
1243 if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget))
1244 gtk_widget_grab_focus (widget);
1246 if (event->button == 1)
1247 gtk_button_pressed (button);
1254 gtk_button_button_release (GtkWidget *widget,
1255 GdkEventButton *event)
1259 if (event->button == 1)
1261 button = GTK_BUTTON (widget);
1262 gtk_button_released (button);
1269 gtk_button_grab_broken (GtkWidget *widget,
1270 GdkEventGrabBroken *event)
1272 GtkButton *button = GTK_BUTTON (widget);
1275 /* Simulate a button release without the pointer in the button */
1276 if (button->button_down)
1278 save_in = button->in_button;
1279 button->in_button = FALSE;
1280 gtk_button_released (button);
1281 if (save_in != button->in_button)
1283 button->in_button = save_in;
1284 gtk_button_update_state (button);
1292 gtk_button_key_release (GtkWidget *widget,
1295 GtkButton *button = GTK_BUTTON (widget);
1297 if (button->activate_timeout)
1299 gtk_button_finish_activate (button, TRUE);
1302 else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
1303 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1309 gtk_button_enter_notify (GtkWidget *widget,
1310 GdkEventCrossing *event)
1313 GtkWidget *event_widget;
1315 button = GTK_BUTTON (widget);
1316 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1318 if ((event_widget == widget) &&
1319 (event->detail != GDK_NOTIFY_INFERIOR))
1321 button->in_button = TRUE;
1322 gtk_button_enter (button);
1329 gtk_button_leave_notify (GtkWidget *widget,
1330 GdkEventCrossing *event)
1333 GtkWidget *event_widget;
1335 button = GTK_BUTTON (widget);
1336 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1338 if ((event_widget == widget) &&
1339 (event->detail != GDK_NOTIFY_INFERIOR))
1341 button->in_button = FALSE;
1342 gtk_button_leave (button);
1349 gtk_real_button_pressed (GtkButton *button)
1351 if (button->activate_timeout)
1354 button->button_down = TRUE;
1355 gtk_button_update_state (button);
1359 gtk_real_button_released (GtkButton *button)
1361 if (button->button_down)
1363 button->button_down = FALSE;
1365 if (button->activate_timeout)
1368 if (button->in_button)
1369 gtk_button_clicked (button);
1371 gtk_button_update_state (button);
1376 button_activate_timeout (gpointer data)
1378 GDK_THREADS_ENTER ();
1380 gtk_button_finish_activate (data, TRUE);
1382 GDK_THREADS_LEAVE ();
1388 gtk_real_button_activate (GtkButton *button)
1390 GtkWidget *widget = GTK_WIDGET (button);
1391 GtkButtonPrivate *priv;
1394 priv = GTK_BUTTON_GET_PRIVATE (button);
1396 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1398 time = gtk_get_current_event_time ();
1399 if (gdk_keyboard_grab (button->event_window, TRUE, time) ==
1402 priv->has_grab = TRUE;
1403 priv->grab_time = time;
1406 gtk_grab_add (widget);
1408 button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1409 button_activate_timeout,
1411 button->button_down = TRUE;
1412 gtk_button_update_state (button);
1413 gtk_widget_queue_draw (GTK_WIDGET (button));
1418 gtk_button_finish_activate (GtkButton *button,
1421 GtkWidget *widget = GTK_WIDGET (button);
1422 GtkButtonPrivate *priv;
1424 priv = GTK_BUTTON_GET_PRIVATE (button);
1426 g_source_remove (button->activate_timeout);
1427 button->activate_timeout = 0;
1431 gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1434 gtk_grab_remove (widget);
1436 button->button_down = FALSE;
1438 gtk_button_update_state (button);
1439 gtk_widget_queue_draw (GTK_WIDGET (button));
1442 gtk_button_clicked (button);
1446 * gtk_button_set_label:
1447 * @button: a #GtkButton
1450 * Sets the text of the label of the button to @str. This text is
1451 * also used to select the stock item if gtk_button_set_use_stock()
1454 * This will also clear any previously set labels.
1457 gtk_button_set_label (GtkButton *button,
1462 g_return_if_fail (GTK_IS_BUTTON (button));
1464 new_label = g_strdup (label);
1465 g_free (button->label_text);
1466 button->label_text = new_label;
1468 gtk_button_construct_child (button);
1470 g_object_notify (G_OBJECT (button), "label");
1474 * gtk_button_get_label:
1475 * @button: a #GtkButton
1477 * Fetches the text from the label of the button, as set by
1478 * gtk_button_set_label(). If the label text has not
1479 * been set the return value will be %NULL. This will be the
1480 * case if you create an empty button with gtk_button_new() to
1481 * use as a container.
1483 * Return value: The text of the label widget. This string is owned
1484 * by the widget and must not be modified or freed.
1486 G_CONST_RETURN gchar *
1487 gtk_button_get_label (GtkButton *button)
1489 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1491 return button->label_text;
1495 * gtk_button_set_use_underline:
1496 * @button: a #GtkButton
1497 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1499 * If true, an underline in the text of the button label indicates
1500 * the next character should be used for the mnemonic accelerator key.
1503 gtk_button_set_use_underline (GtkButton *button,
1504 gboolean use_underline)
1506 g_return_if_fail (GTK_IS_BUTTON (button));
1508 use_underline = use_underline != FALSE;
1510 if (use_underline != button->use_underline)
1512 button->use_underline = use_underline;
1514 gtk_button_construct_child (button);
1516 g_object_notify (G_OBJECT (button), "use-underline");
1521 * gtk_button_get_use_underline:
1522 * @button: a #GtkButton
1524 * Returns whether an embedded underline in the button label indicates a
1525 * mnemonic. See gtk_button_set_use_underline ().
1527 * Return value: %TRUE if an embedded underline in the button label
1528 * indicates the mnemonic accelerator keys.
1531 gtk_button_get_use_underline (GtkButton *button)
1533 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1535 return button->use_underline;
1539 * gtk_button_set_use_stock:
1540 * @button: a #GtkButton
1541 * @use_stock: %TRUE if the button should use a stock item
1543 * If true, the label set on the button is used as a
1544 * stock id to select the stock item for the button.
1547 gtk_button_set_use_stock (GtkButton *button,
1550 g_return_if_fail (GTK_IS_BUTTON (button));
1552 use_stock = use_stock != FALSE;
1554 if (use_stock != button->use_stock)
1556 button->use_stock = use_stock;
1558 gtk_button_construct_child (button);
1560 g_object_notify (G_OBJECT (button), "use-stock");
1565 * gtk_button_get_use_stock:
1566 * @button: a #GtkButton
1568 * Returns whether the button label is a stock item.
1570 * Return value: %TRUE if the button label is used to
1571 * select a stock item instead of being
1572 * used directly as the label text.
1575 gtk_button_get_use_stock (GtkButton *button)
1577 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1579 return button->use_stock;
1583 * gtk_button_set_focus_on_click:
1584 * @button: a #GtkButton
1585 * @focus_on_click: whether the button grabs focus when clicked with the mouse
1587 * Sets whether the button will grab focus when it is clicked with the mouse.
1588 * Making mouse clicks not grab focus is useful in places like toolbars where
1589 * you don't want the keyboard focus removed from the main area of the
1595 gtk_button_set_focus_on_click (GtkButton *button,
1596 gboolean focus_on_click)
1598 g_return_if_fail (GTK_IS_BUTTON (button));
1600 focus_on_click = focus_on_click != FALSE;
1602 if (button->focus_on_click != focus_on_click)
1604 button->focus_on_click = focus_on_click;
1606 g_object_notify (G_OBJECT (button), "focus-on-click");
1611 * gtk_button_get_focus_on_click:
1612 * @button: a #GtkButton
1614 * Returns whether the button grabs focus when it is clicked with the mouse.
1615 * See gtk_button_set_focus_on_click().
1617 * Return value: %TRUE if the button grabs focus when it is clicked with
1623 gtk_button_get_focus_on_click (GtkButton *button)
1625 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1627 return button->focus_on_click;
1631 * gtk_button_set_alignment:
1632 * @button: a #GtkButton
1633 * @xalign: the horizontal position of the child, 0.0 is left aligned,
1634 * 1.0 is right aligned
1635 * @yalign: the vertical position of the child, 0.0 is top aligned,
1636 * 1.0 is bottom aligned
1638 * Sets the alignment of the child. This property has no effect unless
1639 * the child is a #GtkMisc or a #GtkAligment.
1644 gtk_button_set_alignment (GtkButton *button,
1648 GtkButtonPrivate *priv;
1650 g_return_if_fail (GTK_IS_BUTTON (button));
1652 priv = GTK_BUTTON_GET_PRIVATE (button);
1654 priv->xalign = xalign;
1655 priv->yalign = yalign;
1656 priv->align_set = 1;
1658 maybe_set_alignment (button, GTK_BIN (button)->child);
1660 g_object_freeze_notify (G_OBJECT (button));
1661 g_object_notify (G_OBJECT (button), "xalign");
1662 g_object_notify (G_OBJECT (button), "yalign");
1663 g_object_thaw_notify (G_OBJECT (button));
1667 * gtk_button_get_alignment:
1668 * @button: a #GtkButton
1669 * @xalign: return location for horizontal alignment
1670 * @yalign: return location for vertical alignment
1672 * Gets the alignment of the child in the button.
1677 gtk_button_get_alignment (GtkButton *button,
1681 GtkButtonPrivate *priv;
1683 g_return_if_fail (GTK_IS_BUTTON (button));
1685 priv = GTK_BUTTON_GET_PRIVATE (button);
1688 *xalign = priv->xalign;
1691 *yalign = priv->yalign;
1695 * _gtk_button_set_depressed:
1696 * @button: a #GtkButton
1697 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1699 * Sets whether the button is currently drawn as down or not. This is
1700 * purely a visual setting, and is meant only for use by derived widgets
1701 * such as #GtkToggleButton.
1704 _gtk_button_set_depressed (GtkButton *button,
1707 GtkWidget *widget = GTK_WIDGET (button);
1709 depressed = depressed != FALSE;
1711 if (depressed != button->depressed)
1713 button->depressed = depressed;
1714 gtk_widget_queue_resize (widget);
1719 gtk_button_update_state (GtkButton *button)
1722 GtkStateType new_state;
1724 if (button->activate_timeout)
1725 depressed = button->depress_on_activate;
1727 depressed = button->in_button && button->button_down;
1729 if (button->in_button && (!button->button_down || !depressed))
1730 new_state = GTK_STATE_PRELIGHT;
1732 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1734 _gtk_button_set_depressed (button, depressed);
1735 gtk_widget_set_state (GTK_WIDGET (button), new_state);
1739 show_image_change_notify (GtkButton *button)
1741 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1745 if (show_image (button))
1746 gtk_widget_show (priv->image);
1748 gtk_widget_hide (priv->image);
1753 traverse_container (GtkWidget *widget,
1756 if (GTK_IS_BUTTON (widget))
1757 show_image_change_notify (GTK_BUTTON (widget));
1758 else if (GTK_IS_CONTAINER (widget))
1759 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1763 gtk_button_setting_changed (GtkSettings *settings)
1767 list = gtk_window_list_toplevels ();
1769 for (l = list; l; l = l->next)
1770 gtk_container_forall (GTK_CONTAINER (l->data),
1771 traverse_container, NULL);
1778 gtk_button_screen_changed (GtkWidget *widget,
1779 GdkScreen *previous_screen)
1781 GtkSettings *settings;
1782 guint show_image_connection;
1784 if (!gtk_widget_has_screen (widget))
1787 settings = gtk_widget_get_settings (widget);
1789 show_image_connection =
1790 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1791 "gtk-button-connection"));
1793 if (show_image_connection)
1796 show_image_connection =
1797 g_signal_connect (settings, "notify::gtk-button-images",
1798 G_CALLBACK (gtk_button_setting_changed), 0);
1799 g_object_set_data (G_OBJECT (settings),
1800 "gtk-button-connection",
1801 GUINT_TO_POINTER (show_image_connection));
1803 show_image_change_notify (GTK_BUTTON (widget));
1807 gtk_button_state_changed (GtkWidget *widget,
1808 GtkStateType previous_state)
1810 GtkButton *button = GTK_BUTTON (widget);
1812 if (!GTK_WIDGET_IS_SENSITIVE (widget))
1814 button->in_button = FALSE;
1815 gtk_real_button_released (button);
1820 gtk_button_grab_notify (GtkWidget *widget,
1821 gboolean was_grabbed)
1823 GtkButton *button = GTK_BUTTON (widget);
1827 button->in_button = FALSE;
1828 gtk_real_button_released (button);
1833 * gtk_button_set_image:
1834 * @button: a #GtkButton
1835 * @image: a widget to set as the image for the button
1837 * Set the image of @button to the given widget. Note that
1838 * it depends on the gtk-button-images setting whether the
1839 * image will be displayed or not, you don't have to call
1840 * gtk_widget_show() on @image yourself.
1845 gtk_button_set_image (GtkButton *button,
1848 GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1850 priv->image = image;
1851 priv->image_is_stock = (image == NULL);
1853 gtk_button_construct_child (button);
1855 g_object_notify (G_OBJECT (button), "image");
1859 * gtk_button_get_image:
1860 * @button: a #GtkButton
1862 * Gets the widget that is currenty set as the image of @button.
1863 * This may have been explicitly set by gtk_button_set_image()
1864 * or constructed by gtk_button_new_from_stock().
1866 * Return value: a #GtkWidget or %NULL in case there is no image
1871 gtk_button_get_image (GtkButton *button)
1873 GtkButtonPrivate *priv;
1875 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1877 priv = GTK_BUTTON_GET_PRIVATE (button);
1882 #define __GTK_BUTTON_C__
1883 #include "gtkaliasdef.c"