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_BUTTON_PRESS_MASK |
634 GDK_BUTTON_RELEASE_MASK |
635 GDK_ENTER_NOTIFY_MASK |
636 GDK_LEAVE_NOTIFY_MASK);
638 attributes_mask = GDK_WA_X | GDK_WA_Y;
640 widget->window = gtk_widget_get_parent_window (widget);
641 g_object_ref (widget->window);
643 button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
644 &attributes, attributes_mask);
645 gdk_window_set_user_data (button->event_window, button);
647 widget->style = gtk_style_attach (widget->style, widget->window);
651 gtk_button_unrealize (GtkWidget *widget)
653 GtkButton *button = GTK_BUTTON (widget);
655 if (button->activate_timeout)
656 gtk_button_finish_activate (button, FALSE);
658 if (button->event_window)
660 gdk_window_set_user_data (button->event_window, NULL);
661 gdk_window_destroy (button->event_window);
662 button->event_window = NULL;
665 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
669 gtk_button_map (GtkWidget *widget)
671 GtkButton *button = GTK_BUTTON (widget);
673 g_return_if_fail (GTK_IS_BUTTON (widget));
675 GTK_WIDGET_CLASS (parent_class)->map (widget);
677 if (button->event_window)
678 gdk_window_show (button->event_window);
682 gtk_button_unmap (GtkWidget *widget)
684 GtkButton *button = GTK_BUTTON (widget);
686 g_return_if_fail (GTK_IS_BUTTON (widget));
688 if (button->event_window)
689 gdk_window_hide (button->event_window);
691 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
695 gtk_button_get_props (GtkButton *button,
696 GtkBorder *default_border,
697 GtkBorder *default_outside_border,
698 gboolean *interior_focus)
700 GtkWidget *widget = GTK_WIDGET (button);
701 GtkBorder *tmp_border;
705 gtk_widget_style_get (widget, "default_border", &tmp_border, NULL);
709 *default_border = *tmp_border;
713 *default_border = default_default_border;
716 if (default_outside_border)
718 gtk_widget_style_get (widget, "default_outside_border", &tmp_border, NULL);
722 *default_outside_border = *tmp_border;
726 *default_outside_border = default_default_outside_border;
730 gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
734 gtk_button_size_request (GtkWidget *widget,
735 GtkRequisition *requisition)
737 GtkButton *button = GTK_BUTTON (widget);
738 GtkBorder default_border;
739 gboolean interior_focus;
743 gtk_button_get_props (button, &default_border, NULL, &interior_focus);
744 gtk_widget_style_get (GTK_WIDGET (widget),
745 "focus-line-width", &focus_width,
746 "focus-padding", &focus_pad,
749 requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
750 GTK_WIDGET (widget)->style->xthickness) * 2;
751 requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
752 GTK_WIDGET (widget)->style->ythickness) * 2;
754 if (GTK_WIDGET_CAN_DEFAULT (widget))
756 requisition->width += default_border.left + default_border.right;
757 requisition->height += default_border.top + default_border.bottom;
760 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
762 GtkRequisition child_requisition;
764 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
766 requisition->width += child_requisition.width;
767 requisition->height += child_requisition.height;
770 requisition->width += 2 * (focus_width + focus_pad);
771 requisition->height += 2 * (focus_width + focus_pad);
775 gtk_button_size_allocate (GtkWidget *widget,
776 GtkAllocation *allocation)
778 GtkButton *button = GTK_BUTTON (widget);
779 GtkAllocation child_allocation;
781 gint border_width = GTK_CONTAINER (widget)->border_width;
782 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
783 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
784 GtkBorder default_border;
786 gtk_button_get_props (button, &default_border, NULL, NULL);
788 widget->allocation = *allocation;
790 if (GTK_WIDGET_REALIZED (widget))
791 gdk_window_move_resize (button->event_window,
792 widget->allocation.x + border_width,
793 widget->allocation.y + border_width,
794 widget->allocation.width - border_width * 2,
795 widget->allocation.height - border_width * 2);
797 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
799 child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
800 child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
802 child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
804 child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
807 if (GTK_WIDGET_CAN_DEFAULT (button))
809 child_allocation.x += default_border.left;
810 child_allocation.y += default_border.top;
811 child_allocation.width = MAX (1, child_allocation.width - default_border.left - default_border.right);
812 child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
815 if (button->depressed)
817 gint child_displacement_x;
818 gint child_displacement_y;
820 gtk_widget_style_get (widget,
821 "child_displacement_x", &child_displacement_x,
822 "child_displacement_y", &child_displacement_y,
824 child_allocation.x += child_displacement_x;
825 child_allocation.y += child_displacement_y;
828 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
833 _gtk_button_paint (GtkButton *button,
835 GtkStateType state_type,
836 GtkShadowType shadow_type,
837 const gchar *main_detail,
838 const gchar *default_detail)
844 GtkBorder default_border;
845 GtkBorder default_outside_border;
846 gboolean interior_focus;
850 if (GTK_WIDGET_DRAWABLE (button))
852 widget = GTK_WIDGET (button);
853 border_width = GTK_CONTAINER (widget)->border_width;
855 gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
856 gtk_widget_style_get (GTK_WIDGET (widget),
857 "focus-line-width", &focus_width,
858 "focus-padding", &focus_pad,
861 x = widget->allocation.x + border_width;
862 y = widget->allocation.y + border_width;
863 width = widget->allocation.width - border_width * 2;
864 height = widget->allocation.height - border_width * 2;
866 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
867 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
869 gtk_paint_box (widget->style, widget->window,
870 GTK_STATE_NORMAL, GTK_SHADOW_IN,
871 area, widget, "buttondefault",
872 x, y, width, height);
874 x += default_border.left;
875 y += default_border.top;
876 width -= default_border.left + default_border.right;
877 height -= default_border.top + default_border.bottom;
879 else if (GTK_WIDGET_CAN_DEFAULT (widget))
881 x += default_outside_border.left;
882 y += default_outside_border.top;
883 width -= default_outside_border.left + default_outside_border.right;
884 height -= default_outside_border.top + default_outside_border.bottom;
887 if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
889 x += focus_width + focus_pad;
890 y += focus_width + focus_pad;
891 width -= 2 * (focus_width + focus_pad);
892 height -= 2 * (focus_width + focus_pad);
895 if ((button->relief != GTK_RELIEF_NONE) ||
896 ((GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL) &&
897 (GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)))
898 gtk_paint_box (widget->style, widget->window,
900 shadow_type, area, widget, "button",
901 x, y, width, height);
903 if (GTK_WIDGET_HAS_FOCUS (widget))
907 x += widget->style->xthickness + focus_pad;
908 y += widget->style->ythickness + focus_pad;
909 width -= 2 * (widget->style->xthickness + focus_pad);
910 height -= 2 * (widget->style->xthickness + focus_pad);
914 x -= focus_width + focus_pad;
915 y -= focus_width + focus_pad;
916 width += 2 * (focus_width + focus_pad);
917 height += 2 * (focus_width + focus_pad);
920 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
921 area, widget, "button",
922 x, y, width, height);
928 gtk_button_expose (GtkWidget *widget,
929 GdkEventExpose *event)
931 if (GTK_WIDGET_DRAWABLE (widget))
933 GtkButton *button = GTK_BUTTON (widget);
935 _gtk_button_paint (button, &event->area,
936 GTK_WIDGET_STATE (widget),
937 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
938 "button", "buttondefault");
940 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
947 gtk_button_button_press (GtkWidget *widget,
948 GdkEventButton *event)
952 if (event->type == GDK_BUTTON_PRESS)
954 button = GTK_BUTTON (widget);
956 if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget))
957 gtk_widget_grab_focus (widget);
959 if (event->button == 1)
960 gtk_button_pressed (button);
967 gtk_button_button_release (GtkWidget *widget,
968 GdkEventButton *event)
972 if (event->button == 1)
974 button = GTK_BUTTON (widget);
975 gtk_button_released (button);
982 gtk_button_key_release (GtkWidget *widget,
985 GtkButton *button = GTK_BUTTON (widget);
987 if (button->activate_timeout)
989 gtk_button_finish_activate (button, TRUE);
992 else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
993 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
999 gtk_button_enter_notify (GtkWidget *widget,
1000 GdkEventCrossing *event)
1003 GtkWidget *event_widget;
1005 button = GTK_BUTTON (widget);
1006 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1008 if ((event_widget == widget) &&
1009 (event->detail != GDK_NOTIFY_INFERIOR))
1011 button->in_button = TRUE;
1012 gtk_button_enter (button);
1019 gtk_button_leave_notify (GtkWidget *widget,
1020 GdkEventCrossing *event)
1023 GtkWidget *event_widget;
1025 button = GTK_BUTTON (widget);
1026 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1028 if ((event_widget == widget) &&
1029 (event->detail != GDK_NOTIFY_INFERIOR))
1031 button->in_button = FALSE;
1032 gtk_button_leave (button);
1039 gtk_real_button_pressed (GtkButton *button)
1041 if (button->activate_timeout)
1044 button->button_down = TRUE;
1045 gtk_button_update_state (button);
1049 gtk_real_button_released (GtkButton *button)
1051 if (button->button_down)
1053 button->button_down = FALSE;
1055 if (button->activate_timeout)
1058 if (button->in_button)
1059 gtk_button_clicked (button);
1061 gtk_button_update_state (button);
1066 button_activate_timeout (gpointer data)
1068 GDK_THREADS_ENTER ();
1070 gtk_button_finish_activate (data, TRUE);
1072 GDK_THREADS_LEAVE ();
1078 gtk_real_button_activate (GtkButton *button)
1080 GtkWidget *widget = GTK_WIDGET (button);
1082 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1084 if (gdk_keyboard_grab (button->event_window, TRUE,
1085 gtk_get_current_event_time ()) == 0)
1087 gtk_grab_add (widget);
1089 button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1090 button_activate_timeout,
1092 button->button_down = TRUE;
1093 gtk_button_update_state (button);
1094 gtk_widget_queue_draw (GTK_WIDGET (button));
1100 gtk_button_finish_activate (GtkButton *button,
1103 GtkWidget *widget = GTK_WIDGET (button);
1105 g_source_remove (button->activate_timeout);
1106 button->activate_timeout = 0;
1108 gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1109 gtk_get_current_event_time ());
1110 gtk_grab_remove (widget);
1112 button->button_down = FALSE;
1114 gtk_button_update_state (button);
1115 gtk_widget_queue_draw (GTK_WIDGET (button));
1118 gtk_button_clicked (button);
1122 * gtk_button_set_label:
1123 * @button: a #GtkButton
1126 * Sets the text of the label of the button to @str. This text is
1127 * also used to select the stock item if gtk_button_set_use_stock()
1130 * This will also clear any previously set labels.
1133 gtk_button_set_label (GtkButton *button,
1138 g_return_if_fail (GTK_IS_BUTTON (button));
1140 new_label = g_strdup (label);
1141 g_free (button->label_text);
1142 button->label_text = new_label;
1144 gtk_button_construct_child (button);
1146 g_object_notify (G_OBJECT (button), "label");
1150 * gtk_button_get_label:
1151 * @button: a #GtkButton
1153 * Fetches the text from the label of the button, as set by
1154 * gtk_button_set_label(). If the label text has not
1155 * been set the return value will be %NULL. This will be the
1156 * case if you create an empty button with gtk_button_new() to
1157 * use as a container.
1159 * Return value: The text of the label widget. This string is owned
1160 * by the widget and must not be modified or freed.
1162 G_CONST_RETURN gchar *
1163 gtk_button_get_label (GtkButton *button)
1165 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1167 return button->label_text;
1171 * gtk_button_set_use_underline:
1172 * @button: a #GtkButton
1173 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1175 * If true, an underline in the text of the button label indicates
1176 * the next character should be used for the mnemonic accelerator key.
1179 gtk_button_set_use_underline (GtkButton *button,
1180 gboolean use_underline)
1182 g_return_if_fail (GTK_IS_BUTTON (button));
1184 use_underline = use_underline != FALSE;
1186 if (use_underline != button->use_underline)
1188 button->use_underline = use_underline;
1190 gtk_button_construct_child (button);
1192 g_object_notify (G_OBJECT (button), "use_underline");
1197 * gtk_button_get_use_underline:
1198 * @button: a #GtkButton
1200 * Returns whether an embedded underline in the button label indicates a
1201 * mnemonic. See gtk_button_set_use_underline ().
1203 * Return value: %TRUE if an embedded underline in the button label
1204 * indicates the mnemonic accelerator keys.
1207 gtk_button_get_use_underline (GtkButton *button)
1209 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1211 return button->use_underline;
1215 * gtk_button_set_use_stock:
1216 * @button: a #GtkButton
1217 * @use_stock: %TRUE if the button should use a stock item
1219 * If true, the label set on the button is used as a
1220 * stock id to select the stock item for the button.
1223 gtk_button_set_use_stock (GtkButton *button,
1226 g_return_if_fail (GTK_IS_BUTTON (button));
1228 use_stock = use_stock != FALSE;
1230 if (use_stock != button->use_stock)
1232 button->use_stock = use_stock;
1234 gtk_button_construct_child (button);
1236 g_object_notify (G_OBJECT (button), "use_stock");
1241 * gtk_button_get_use_stock:
1242 * @button: a #GtkButton
1244 * Returns whether the button label is a stock item.
1246 * Return value: %TRUE if the button label is used to
1247 * select a stock item instead of being
1248 * used directly as the label text.
1251 gtk_button_get_use_stock (GtkButton *button)
1253 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1255 return button->use_stock;
1259 * gtk_button_set_focus_on_click:
1260 * @button: a #GtkButton
1261 * @focus_on_click: whether the mouse grabs focus when clicked with the mouse
1263 * Sets whether the button will grab focus when it is clicked with the mouse.
1264 * Making mouse clicks not grab focus is useful in places like toolbars where
1265 * you don't want the keyboard focus removed from the main area of the
1271 gtk_button_set_focus_on_click (GtkButton *button,
1272 gboolean focus_on_click)
1274 g_return_if_fail (GTK_IS_BUTTON (button));
1276 focus_on_click = focus_on_click != FALSE;
1278 if (button->focus_on_click != focus_on_click)
1280 button->focus_on_click = focus_on_click;
1282 g_object_notify (G_OBJECT (button), "focus_on_click");
1287 * gtk_button_get_focus_on_click:
1288 * @button: a #GtkButton
1290 * Returns whether the button grabs focus when it is clicked with the mouse.
1291 * See gtk_button_set_focus_on_click().
1293 * Return value: %TRUE if the button grabs focus when it is clicked with
1299 gtk_button_get_focus_on_click (GtkButton *button)
1301 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1303 return button->focus_on_click;
1307 * _gtk_button_set_depressed:
1308 * @button: a #GtkButton
1309 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1311 * Sets whether the button is currently drawn as down or not. This is
1312 * purely a visual setting, and is meant only for use by derived widgets
1313 * such as #GtkToggleButton.
1316 _gtk_button_set_depressed (GtkButton *button,
1319 GtkWidget *widget = GTK_WIDGET (button);
1321 depressed = depressed != FALSE;
1323 if (depressed != button->depressed)
1325 button->depressed = depressed;
1326 gtk_widget_queue_resize (widget);
1331 gtk_button_update_state (GtkButton *button)
1334 GtkStateType new_state;
1336 if (button->activate_timeout)
1337 depressed = button->depress_on_activate;
1339 depressed = button->in_button && button->button_down;
1341 if (button->in_button && (!button->button_down || !depressed))
1342 new_state = GTK_STATE_PRELIGHT;
1344 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1346 _gtk_button_set_depressed (button, depressed);
1347 gtk_widget_set_state (GTK_WIDGET (button), new_state);