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/.
28 #include "gtkalignment.h"
29 #include "gtkbutton.h"
32 #include "gtkmarshalers.h"
36 #include "gtkiconfactory.h"
39 #define CHILD_SPACING 1
41 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
42 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
44 /* Time out before giving up on getting a key release when animatng
47 #define ACTIVATE_TIMEOUT 250
68 static void gtk_button_class_init (GtkButtonClass *klass);
69 static void gtk_button_init (GtkButton *button);
70 static void gtk_button_destroy (GtkObject *object);
71 static void gtk_button_set_property (GObject *object,
75 static void gtk_button_get_property (GObject *object,
79 static void gtk_button_realize (GtkWidget *widget);
80 static void gtk_button_unrealize (GtkWidget *widget);
81 static void gtk_button_map (GtkWidget *widget);
82 static void gtk_button_unmap (GtkWidget *widget);
83 static void gtk_button_size_request (GtkWidget *widget,
84 GtkRequisition *requisition);
85 static void gtk_button_size_allocate (GtkWidget *widget,
86 GtkAllocation *allocation);
87 static gint gtk_button_expose (GtkWidget *widget,
88 GdkEventExpose *event);
89 static gint gtk_button_button_press (GtkWidget *widget,
90 GdkEventButton *event);
91 static gint gtk_button_button_release (GtkWidget *widget,
92 GdkEventButton *event);
93 static gint gtk_button_key_release (GtkWidget *widget,
95 static gint gtk_button_enter_notify (GtkWidget *widget,
96 GdkEventCrossing *event);
97 static gint gtk_button_leave_notify (GtkWidget *widget,
98 GdkEventCrossing *event);
99 static void gtk_real_button_pressed (GtkButton *button);
100 static void gtk_real_button_released (GtkButton *button);
101 static void gtk_real_button_activate (GtkButton *button);
102 static void gtk_button_update_state (GtkButton *button);
103 static GType gtk_button_child_type (GtkContainer *container);
104 static void gtk_button_finish_activate (GtkButton *button,
107 static GObject* gtk_button_constructor (GType type,
108 guint n_construct_properties,
109 GObjectConstructParam *construct_params);
110 static void gtk_button_construct_child (GtkButton *button);
113 static GtkBinClass *parent_class = NULL;
114 static guint button_signals[LAST_SIGNAL] = { 0 };
118 gtk_button_get_type (void)
120 static GType button_type = 0;
124 static const GTypeInfo button_info =
126 sizeof (GtkButtonClass),
127 NULL, /* base_init */
128 NULL, /* base_finalize */
129 (GClassInitFunc) gtk_button_class_init,
130 NULL, /* class_finalize */
131 NULL, /* class_data */
133 16, /* n_preallocs */
134 (GInstanceInitFunc) gtk_button_init,
137 button_type = g_type_register_static (GTK_TYPE_BIN, "GtkButton",
145 gtk_button_class_init (GtkButtonClass *klass)
147 GObjectClass *gobject_class;
148 GtkObjectClass *object_class;
149 GtkWidgetClass *widget_class;
150 GtkContainerClass *container_class;
152 gobject_class = G_OBJECT_CLASS (klass);
153 object_class = (GtkObjectClass*) klass;
154 widget_class = (GtkWidgetClass*) klass;
155 container_class = (GtkContainerClass*) klass;
157 parent_class = g_type_class_peek_parent (klass);
159 gobject_class->constructor = gtk_button_constructor;
160 gobject_class->set_property = gtk_button_set_property;
161 gobject_class->get_property = gtk_button_get_property;
163 object_class->destroy = gtk_button_destroy;
165 widget_class->realize = gtk_button_realize;
166 widget_class->unrealize = gtk_button_unrealize;
167 widget_class->map = gtk_button_map;
168 widget_class->unmap = gtk_button_unmap;
169 widget_class->size_request = gtk_button_size_request;
170 widget_class->size_allocate = gtk_button_size_allocate;
171 widget_class->expose_event = gtk_button_expose;
172 widget_class->button_press_event = gtk_button_button_press;
173 widget_class->button_release_event = gtk_button_button_release;
174 widget_class->key_release_event = gtk_button_key_release;
175 widget_class->enter_notify_event = gtk_button_enter_notify;
176 widget_class->leave_notify_event = gtk_button_leave_notify;
178 container_class->child_type = gtk_button_child_type;
180 klass->pressed = gtk_real_button_pressed;
181 klass->released = gtk_real_button_released;
182 klass->clicked = NULL;
183 klass->enter = gtk_button_update_state;
184 klass->leave = gtk_button_update_state;
185 klass->activate = gtk_real_button_activate;
187 g_object_class_install_property (gobject_class,
189 g_param_spec_string ("label",
191 _("Text of the label widget inside the button, if the button contains a label widget"),
193 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
195 g_object_class_install_property (gobject_class,
197 g_param_spec_boolean ("use_underline",
199 _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
201 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
203 g_object_class_install_property (gobject_class,
205 g_param_spec_boolean ("use_stock",
207 _("If set, the label is used to pick a stock item instead of being displayed"),
209 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
211 g_object_class_install_property (gobject_class,
213 g_param_spec_boolean ("focus_on_click",
215 _("Whether the button grabs focus when it is clicked with the mouse"),
219 g_object_class_install_property (gobject_class,
221 g_param_spec_enum ("relief",
223 _("The border relief style"),
224 GTK_TYPE_RELIEF_STYLE,
226 G_PARAM_READABLE | G_PARAM_WRITABLE));
228 button_signals[PRESSED] =
229 g_signal_new ("pressed",
230 G_OBJECT_CLASS_TYPE (object_class),
232 G_STRUCT_OFFSET (GtkButtonClass, pressed),
234 _gtk_marshal_VOID__VOID,
236 button_signals[RELEASED] =
237 g_signal_new ("released",
238 G_OBJECT_CLASS_TYPE (object_class),
240 G_STRUCT_OFFSET (GtkButtonClass, released),
242 _gtk_marshal_VOID__VOID,
244 button_signals[CLICKED] =
245 g_signal_new ("clicked",
246 G_OBJECT_CLASS_TYPE (object_class),
247 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
248 G_STRUCT_OFFSET (GtkButtonClass, clicked),
250 _gtk_marshal_VOID__VOID,
252 button_signals[ENTER] =
253 g_signal_new ("enter",
254 G_OBJECT_CLASS_TYPE (object_class),
256 G_STRUCT_OFFSET (GtkButtonClass, enter),
258 _gtk_marshal_VOID__VOID,
260 button_signals[LEAVE] =
261 g_signal_new ("leave",
262 G_OBJECT_CLASS_TYPE (object_class),
264 G_STRUCT_OFFSET (GtkButtonClass, leave),
266 _gtk_marshal_VOID__VOID,
268 button_signals[ACTIVATE] =
269 g_signal_new ("activate",
270 G_OBJECT_CLASS_TYPE (object_class),
271 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
272 G_STRUCT_OFFSET (GtkButtonClass, activate),
274 _gtk_marshal_VOID__VOID,
276 widget_class->activate_signal = button_signals[ACTIVATE];
278 gtk_widget_class_install_style_property (widget_class,
279 g_param_spec_boxed ("default_border",
280 _("Default Spacing"),
281 _("Extra space to add for CAN_DEFAULT buttons"),
285 gtk_widget_class_install_style_property (widget_class,
286 g_param_spec_boxed ("default_outside_border",
287 _("Default Outside Spacing"),
288 _("Extra space to add for CAN_DEFAULT buttons that is always drawn outside the border"),
291 gtk_widget_class_install_style_property (widget_class,
292 g_param_spec_int ("child_displacement_x",
293 _("Child X Displacement"),
294 _("How far in the x direction to move the child when the button is depressed"),
299 gtk_widget_class_install_style_property (widget_class,
300 g_param_spec_int ("child_displacement_y",
301 _("Child Y Displacement"),
302 _("How far in the y direction to move the child when the button is depressed"),
310 gtk_button_init (GtkButton *button)
312 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
313 GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);
315 button->label_text = NULL;
317 button->constructed = FALSE;
318 button->in_button = FALSE;
319 button->button_down = FALSE;
320 button->relief = GTK_RELIEF_NORMAL;
321 button->use_stock = FALSE;
322 button->use_underline = FALSE;
323 button->depressed = FALSE;
324 button->depress_on_activate = TRUE;
325 button->focus_on_click = TRUE;
329 gtk_button_destroy (GtkObject *object)
331 GtkButton *button = GTK_BUTTON (object);
333 if (button->label_text)
335 g_free (button->label_text);
336 button->label_text = NULL;
339 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
343 gtk_button_constructor (GType type,
344 guint n_construct_properties,
345 GObjectConstructParam *construct_params)
350 object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
351 n_construct_properties,
354 button = GTK_BUTTON (object);
355 button->constructed = TRUE;
357 if (button->label_text != NULL)
358 gtk_button_construct_child (button);
365 gtk_button_child_type (GtkContainer *container)
367 if (!GTK_BIN (container)->child)
368 return GTK_TYPE_WIDGET;
374 gtk_button_set_property (GObject *object,
381 button = GTK_BUTTON (object);
386 gtk_button_set_label (button, g_value_get_string (value));
389 gtk_button_set_relief (button, g_value_get_enum (value));
391 case PROP_USE_UNDERLINE:
392 gtk_button_set_use_underline (button, g_value_get_boolean (value));
395 gtk_button_set_use_stock (button, g_value_get_boolean (value));
397 case PROP_FOCUS_ON_CLICK:
398 gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
401 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
407 gtk_button_get_property (GObject *object,
414 button = GTK_BUTTON (object);
419 g_value_set_string (value, button->label_text);
422 g_value_set_enum (value, gtk_button_get_relief (button));
424 case PROP_USE_UNDERLINE:
425 g_value_set_boolean (value, button->use_underline);
428 g_value_set_boolean (value, button->use_stock);
430 case PROP_FOCUS_ON_CLICK:
431 g_value_set_boolean (value, button->focus_on_click);
434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
440 gtk_button_new (void)
442 return g_object_new (GTK_TYPE_BUTTON, NULL);
446 gtk_button_construct_child (GtkButton *button)
454 if (!button->constructed)
457 if (button->label_text == NULL)
460 if (GTK_BIN (button)->child)
461 gtk_container_remove (GTK_CONTAINER (button),
462 GTK_BIN (button)->child);
465 if (button->use_stock &&
466 gtk_stock_lookup (button->label_text, &item))
468 label = gtk_label_new_with_mnemonic (item.label);
470 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
472 image = gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON);
473 hbox = gtk_hbox_new (FALSE, 2);
475 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
477 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
478 gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
480 gtk_container_add (GTK_CONTAINER (button), align);
481 gtk_container_add (GTK_CONTAINER (align), hbox);
482 gtk_widget_show_all (align);
487 if (button->use_underline)
489 label = gtk_label_new_with_mnemonic (button->label_text);
490 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
493 label = gtk_label_new (button->label_text);
495 gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
497 gtk_widget_show (label);
498 gtk_container_add (GTK_CONTAINER (button), label);
503 gtk_button_new_with_label (const gchar *label)
505 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
509 * gtk_button_new_from_stock:
510 * @stock_id: the name of the stock item
512 * Creates a new #GtkButton containing the image and text from a stock item.
513 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
516 * If @stock_id is unknown, then it will be treated as a mnemonic
517 * label (as for gtk_button_new_with_mnemonic()).
519 * Returns: a new #GtkButton
522 gtk_button_new_from_stock (const gchar *stock_id)
524 return g_object_new (GTK_TYPE_BUTTON,
527 "use_underline", TRUE,
532 * gtk_button_new_with_mnemonic:
533 * @label: The text of the button, with an underscore in front of the
535 * @returns: a new #GtkButton
537 * Creates a new #GtkButton containing a label.
538 * If characters in @label are preceded by an underscore, they are underlined.
539 * If you need a literal underscore character in a label, use '__' (two
540 * underscores). The first underlined character represents a keyboard
541 * accelerator called a mnemonic.
542 * Pressing Alt and that key activates the button.
545 gtk_button_new_with_mnemonic (const gchar *label)
547 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use_underline", TRUE, NULL);
551 gtk_button_pressed (GtkButton *button)
553 g_return_if_fail (GTK_IS_BUTTON (button));
555 g_signal_emit (button, button_signals[PRESSED], 0);
559 gtk_button_released (GtkButton *button)
561 g_return_if_fail (GTK_IS_BUTTON (button));
563 g_signal_emit (button, button_signals[RELEASED], 0);
567 gtk_button_clicked (GtkButton *button)
569 g_return_if_fail (GTK_IS_BUTTON (button));
571 g_signal_emit (button, button_signals[CLICKED], 0);
575 gtk_button_enter (GtkButton *button)
577 g_return_if_fail (GTK_IS_BUTTON (button));
579 g_signal_emit (button, button_signals[ENTER], 0);
583 gtk_button_leave (GtkButton *button)
585 g_return_if_fail (GTK_IS_BUTTON (button));
587 g_signal_emit (button, button_signals[LEAVE], 0);
591 gtk_button_set_relief (GtkButton *button,
592 GtkReliefStyle newrelief)
594 g_return_if_fail (GTK_IS_BUTTON (button));
596 if (newrelief != button->relief)
598 button->relief = newrelief;
599 g_object_notify (G_OBJECT (button), "relief");
600 gtk_widget_queue_draw (GTK_WIDGET (button));
605 gtk_button_get_relief (GtkButton *button)
607 g_return_val_if_fail (button != NULL, GTK_RELIEF_NORMAL);
608 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
610 return button->relief;
614 gtk_button_realize (GtkWidget *widget)
617 GdkWindowAttr attributes;
618 gint attributes_mask;
621 button = GTK_BUTTON (widget);
622 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
624 border_width = GTK_CONTAINER (widget)->border_width;
626 attributes.window_type = GDK_WINDOW_CHILD;
627 attributes.x = widget->allocation.x + border_width;
628 attributes.y = widget->allocation.y + border_width;
629 attributes.width = widget->allocation.width - border_width * 2;
630 attributes.height = widget->allocation.height - border_width * 2;
631 attributes.wclass = GDK_INPUT_ONLY;
632 attributes.event_mask = gtk_widget_get_events (widget);
633 attributes.event_mask |= (GDK_EXPOSURE_MASK |
634 GDK_BUTTON_PRESS_MASK |
635 GDK_BUTTON_RELEASE_MASK |
636 GDK_ENTER_NOTIFY_MASK |
637 GDK_LEAVE_NOTIFY_MASK);
639 attributes_mask = GDK_WA_X | GDK_WA_Y;
641 widget->window = gtk_widget_get_parent_window (widget);
642 g_object_ref (widget->window);
644 button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
645 &attributes, attributes_mask);
646 gdk_window_set_user_data (button->event_window, button);
648 widget->style = gtk_style_attach (widget->style, widget->window);
652 gtk_button_unrealize (GtkWidget *widget)
654 GtkButton *button = GTK_BUTTON (widget);
656 if (button->activate_timeout)
657 gtk_button_finish_activate (button, FALSE);
659 if (button->event_window)
661 gdk_window_set_user_data (button->event_window, NULL);
662 gdk_window_destroy (button->event_window);
663 button->event_window = NULL;
666 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
670 gtk_button_map (GtkWidget *widget)
672 GtkButton *button = GTK_BUTTON (widget);
674 g_return_if_fail (GTK_IS_BUTTON (widget));
676 GTK_WIDGET_CLASS (parent_class)->map (widget);
678 if (button->event_window)
679 gdk_window_show (button->event_window);
683 gtk_button_unmap (GtkWidget *widget)
685 GtkButton *button = GTK_BUTTON (widget);
687 g_return_if_fail (GTK_IS_BUTTON (widget));
689 if (button->event_window)
690 gdk_window_hide (button->event_window);
692 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
696 gtk_button_get_props (GtkButton *button,
697 GtkBorder *default_border,
698 GtkBorder *default_outside_border,
699 gboolean *interior_focus)
701 GtkWidget *widget = GTK_WIDGET (button);
702 GtkBorder *tmp_border;
706 gtk_widget_style_get (widget, "default_border", &tmp_border, NULL);
710 *default_border = *tmp_border;
714 *default_border = default_default_border;
717 if (default_outside_border)
719 gtk_widget_style_get (widget, "default_outside_border", &tmp_border, NULL);
723 *default_outside_border = *tmp_border;
727 *default_outside_border = default_default_outside_border;
731 gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
735 gtk_button_size_request (GtkWidget *widget,
736 GtkRequisition *requisition)
738 GtkButton *button = GTK_BUTTON (widget);
739 GtkBorder default_border;
740 gboolean interior_focus;
744 gtk_button_get_props (button, &default_border, NULL, &interior_focus);
745 gtk_widget_style_get (GTK_WIDGET (widget),
746 "focus-line-width", &focus_width,
747 "focus-padding", &focus_pad,
750 requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
751 GTK_WIDGET (widget)->style->xthickness) * 2;
752 requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
753 GTK_WIDGET (widget)->style->ythickness) * 2;
755 if (GTK_WIDGET_CAN_DEFAULT (widget))
757 requisition->width += default_border.left + default_border.right;
758 requisition->height += default_border.top + default_border.bottom;
761 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
763 GtkRequisition child_requisition;
765 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
767 requisition->width += child_requisition.width;
768 requisition->height += child_requisition.height;
771 requisition->width += 2 * (focus_width + focus_pad);
772 requisition->height += 2 * (focus_width + focus_pad);
776 gtk_button_size_allocate (GtkWidget *widget,
777 GtkAllocation *allocation)
779 GtkButton *button = GTK_BUTTON (widget);
780 GtkAllocation child_allocation;
782 gint border_width = GTK_CONTAINER (widget)->border_width;
783 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
784 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
785 GtkBorder default_border;
787 gtk_button_get_props (button, &default_border, NULL, NULL);
789 widget->allocation = *allocation;
791 if (GTK_WIDGET_REALIZED (widget))
792 gdk_window_move_resize (button->event_window,
793 widget->allocation.x + border_width,
794 widget->allocation.y + border_width,
795 widget->allocation.width - border_width * 2,
796 widget->allocation.height - border_width * 2);
798 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
800 child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
801 child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
803 child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
805 child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
808 if (GTK_WIDGET_CAN_DEFAULT (button))
810 child_allocation.x += default_border.left;
811 child_allocation.y += default_border.top;
812 child_allocation.width = MAX (1, child_allocation.width - default_border.left - default_border.right);
813 child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
816 if (button->depressed)
818 gint child_displacement_x;
819 gint child_displacement_y;
821 gtk_widget_style_get (widget,
822 "child_displacement_x", &child_displacement_x,
823 "child_displacement_y", &child_displacement_y,
825 child_allocation.x += child_displacement_x;
826 child_allocation.y += child_displacement_y;
829 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
834 _gtk_button_paint (GtkButton *button,
836 GtkStateType state_type,
837 GtkShadowType shadow_type,
838 const gchar *main_detail,
839 const gchar *default_detail)
845 GtkBorder default_border;
846 GtkBorder default_outside_border;
847 gboolean interior_focus;
851 if (GTK_WIDGET_DRAWABLE (button))
853 widget = GTK_WIDGET (button);
854 border_width = GTK_CONTAINER (widget)->border_width;
856 gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
857 gtk_widget_style_get (GTK_WIDGET (widget),
858 "focus-line-width", &focus_width,
859 "focus-padding", &focus_pad,
862 x = widget->allocation.x + border_width;
863 y = widget->allocation.y + border_width;
864 width = widget->allocation.width - border_width * 2;
865 height = widget->allocation.height - border_width * 2;
867 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
868 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
870 gtk_paint_box (widget->style, widget->window,
871 GTK_STATE_NORMAL, GTK_SHADOW_IN,
872 area, widget, "buttondefault",
873 x, y, width, height);
875 x += default_border.left;
876 y += default_border.top;
877 width -= default_border.left + default_border.right;
878 height -= default_border.top + default_border.bottom;
880 else if (GTK_WIDGET_CAN_DEFAULT (widget))
882 x += default_outside_border.left;
883 y += default_outside_border.top;
884 width -= default_outside_border.left + default_outside_border.right;
885 height -= default_outside_border.top + default_outside_border.bottom;
888 if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
890 x += focus_width + focus_pad;
891 y += focus_width + focus_pad;
892 width -= 2 * (focus_width + focus_pad);
893 height -= 2 * (focus_width + focus_pad);
896 if ((button->relief != GTK_RELIEF_NONE) ||
897 ((GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL) &&
898 (GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)))
899 gtk_paint_box (widget->style, widget->window,
901 shadow_type, area, widget, "button",
902 x, y, width, height);
904 if (GTK_WIDGET_HAS_FOCUS (widget))
908 x += widget->style->xthickness + focus_pad;
909 y += widget->style->ythickness + focus_pad;
910 width -= 2 * (widget->style->xthickness + focus_pad);
911 height -= 2 * (widget->style->xthickness + focus_pad);
915 x -= focus_width + focus_pad;
916 y -= focus_width + focus_pad;
917 width += 2 * (focus_width + focus_pad);
918 height += 2 * (focus_width + focus_pad);
921 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
922 area, widget, "button",
923 x, y, width, height);
929 gtk_button_expose (GtkWidget *widget,
930 GdkEventExpose *event)
932 if (GTK_WIDGET_DRAWABLE (widget))
934 GtkButton *button = GTK_BUTTON (widget);
936 _gtk_button_paint (button, &event->area,
937 GTK_WIDGET_STATE (widget),
938 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
939 "button", "buttondefault");
941 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
948 gtk_button_button_press (GtkWidget *widget,
949 GdkEventButton *event)
953 if (event->type == GDK_BUTTON_PRESS)
955 button = GTK_BUTTON (widget);
957 if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget))
958 gtk_widget_grab_focus (widget);
960 if (event->button == 1)
961 gtk_button_pressed (button);
968 gtk_button_button_release (GtkWidget *widget,
969 GdkEventButton *event)
973 if (event->button == 1)
975 button = GTK_BUTTON (widget);
976 gtk_button_released (button);
983 gtk_button_key_release (GtkWidget *widget,
986 GtkButton *button = GTK_BUTTON (widget);
988 if (button->activate_timeout)
990 gtk_button_finish_activate (button, TRUE);
993 else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
994 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1000 gtk_button_enter_notify (GtkWidget *widget,
1001 GdkEventCrossing *event)
1004 GtkWidget *event_widget;
1006 button = GTK_BUTTON (widget);
1007 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1009 if ((event_widget == widget) &&
1010 (event->detail != GDK_NOTIFY_INFERIOR))
1012 button->in_button = TRUE;
1013 gtk_button_enter (button);
1020 gtk_button_leave_notify (GtkWidget *widget,
1021 GdkEventCrossing *event)
1024 GtkWidget *event_widget;
1026 button = GTK_BUTTON (widget);
1027 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1029 if ((event_widget == widget) &&
1030 (event->detail != GDK_NOTIFY_INFERIOR))
1032 button->in_button = FALSE;
1033 gtk_button_leave (button);
1040 gtk_real_button_pressed (GtkButton *button)
1042 if (button->activate_timeout)
1045 button->button_down = TRUE;
1046 gtk_button_update_state (button);
1050 gtk_real_button_released (GtkButton *button)
1052 if (button->button_down)
1054 button->button_down = FALSE;
1056 if (button->activate_timeout)
1059 if (button->in_button)
1060 gtk_button_clicked (button);
1062 gtk_button_update_state (button);
1067 button_activate_timeout (gpointer data)
1069 GDK_THREADS_ENTER ();
1071 gtk_button_finish_activate (data, TRUE);
1073 GDK_THREADS_LEAVE ();
1079 gtk_real_button_activate (GtkButton *button)
1081 GtkWidget *widget = GTK_WIDGET (button);
1083 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1085 if (gdk_keyboard_grab (button->event_window, TRUE,
1086 gtk_get_current_event_time ()) == 0)
1088 gtk_grab_add (widget);
1090 button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1091 button_activate_timeout,
1093 button->button_down = TRUE;
1094 gtk_button_update_state (button);
1095 gtk_widget_queue_draw (GTK_WIDGET (button));
1101 gtk_button_finish_activate (GtkButton *button,
1104 GtkWidget *widget = GTK_WIDGET (button);
1106 g_source_remove (button->activate_timeout);
1107 button->activate_timeout = 0;
1109 gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1110 gtk_get_current_event_time ());
1111 gtk_grab_remove (widget);
1113 button->button_down = FALSE;
1115 gtk_button_update_state (button);
1116 gtk_widget_queue_draw (GTK_WIDGET (button));
1119 gtk_button_clicked (button);
1123 * gtk_button_set_label:
1124 * @button: a #GtkButton
1127 * Sets the text of the label of the button to @str. This text is
1128 * also used to select the stock item if gtk_button_set_use_stock()
1131 * This will also clear any previously set labels.
1134 gtk_button_set_label (GtkButton *button,
1137 g_return_if_fail (GTK_IS_BUTTON (button));
1139 g_free (button->label_text);
1140 button->label_text = g_strdup (label);
1142 gtk_button_construct_child (button);
1144 g_object_notify (G_OBJECT (button), "label");
1148 * gtk_button_get_label:
1149 * @button: a #GtkButton
1151 * Fetches the text from the label of the button, as set by
1152 * gtk_button_set_label(). If the label text has not
1153 * been set the return value will be %NULL. This will be the
1154 * case if you create an empty button with gtk_button_new() to
1155 * use as a container.
1157 * Return value: The text of the label widget. This string is owned
1158 * by the widget and must not be modified or freed.
1160 G_CONST_RETURN gchar *
1161 gtk_button_get_label (GtkButton *button)
1163 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1165 return button->label_text;
1169 * gtk_button_set_use_underline:
1170 * @button: a #GtkButton
1171 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1173 * If true, an underline in the text of the button label indicates
1174 * the next character should be used for the mnemonic accelerator key.
1177 gtk_button_set_use_underline (GtkButton *button,
1178 gboolean use_underline)
1180 g_return_if_fail (GTK_IS_BUTTON (button));
1182 use_underline = use_underline != FALSE;
1184 if (use_underline != button->use_underline)
1186 button->use_underline = use_underline;
1188 gtk_button_construct_child (button);
1190 g_object_notify (G_OBJECT (button), "use_underline");
1195 * gtk_button_get_use_underline:
1196 * @button: a #GtkButton
1198 * Returns whether an embedded underline in the button label indicates a
1199 * mnemonic. See gtk_button_set_use_underline ().
1201 * Return value: %TRUE if an embedded underline in the button label
1202 * indicates the mnemonic accelerator keys.
1205 gtk_button_get_use_underline (GtkButton *button)
1207 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1209 return button->use_underline;
1213 * gtk_button_set_use_stock:
1214 * @button: a #GtkButton
1215 * @use_stock: %TRUE if the button should use a stock item
1217 * If true, the label set on the button is used as a
1218 * stock id to select the stock item for the button.
1221 gtk_button_set_use_stock (GtkButton *button,
1224 g_return_if_fail (GTK_IS_BUTTON (button));
1226 use_stock = use_stock != FALSE;
1228 if (use_stock != button->use_stock)
1230 button->use_stock = use_stock;
1232 gtk_button_construct_child (button);
1234 g_object_notify (G_OBJECT (button), "use_stock");
1239 * gtk_button_get_use_stock:
1240 * @button: a #GtkButton
1242 * Returns whether the button label is a stock item.
1244 * Return value: %TRUE if the button label is used to
1245 * select a stock item instead of being
1246 * used directly as the label text.
1249 gtk_button_get_use_stock (GtkButton *button)
1251 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1253 return button->use_stock;
1257 * gtk_button_set_focus_on_click:
1258 * @button: a #GtkButton
1259 * @focus_on_click: whether the mouse grabs focus when clicked with the mouse
1261 * Sets whether the button will grab focus when it is clicked with the mouse.
1262 * Making mouse clicks not grab focus is useful in places like toolbars where
1263 * you don't want the keyboard focus removed from the main area of the
1267 gtk_button_set_focus_on_click (GtkButton *button,
1268 gboolean focus_on_click)
1270 g_return_if_fail (GTK_IS_BUTTON (button));
1272 focus_on_click = focus_on_click != FALSE;
1274 if (button->focus_on_click != focus_on_click)
1276 button->focus_on_click = focus_on_click;
1278 g_object_notify (G_OBJECT (button), "focus_on_click");
1283 * gtk_button_get_focus_on_click:
1284 * @button: a #GtkButton
1286 * Returns whether the button grabs focus when it is clicked with the mouse.
1287 * See gtk_button_set_focus_on_click().
1289 * Return value: %TRUE if the button grabs focus when it is clicked with
1293 gtk_button_get_focus_on_click (GtkButton *button)
1295 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1297 return button->focus_on_click;
1301 * _gtk_button_set_depressed:
1302 * @button: a #GtkButton
1303 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1305 * Sets whether the button is currently drawn as down or not. This is
1306 * purely a visual setting, and is meant only for use by derived widgets
1307 * such as #GtkToggleButton.
1310 _gtk_button_set_depressed (GtkButton *button,
1313 GtkWidget *widget = GTK_WIDGET (button);
1315 depressed = depressed != FALSE;
1317 if (depressed != button->depressed)
1319 button->depressed = depressed;
1320 gtk_widget_queue_resize (widget);
1325 gtk_button_update_state (GtkButton *button)
1328 GtkStateType new_state;
1330 if (button->activate_timeout)
1331 depressed = button->depress_on_activate;
1333 depressed = button->in_button && button->button_down;
1335 if (button->in_button && (!button->button_down || !depressed))
1336 new_state = GTK_STATE_PRELIGHT;
1338 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1340 _gtk_button_set_depressed (button, depressed);
1341 gtk_widget_set_state (GTK_WIDGET (button), new_state);