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"
33 #include "gtksignal.h"
37 #include "gtkiconfactory.h"
40 #define CHILD_SPACING 1
42 static GtkBorder default_default_border = { 1, 1, 1, 1 };
43 static GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
45 /* Time out before giving up on getting a key release when animatng
48 #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 GtkType 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 GtkType 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", &button_info, 0);
144 gtk_button_class_init (GtkButtonClass *klass)
146 GObjectClass *g_object_class;
147 GtkObjectClass *object_class;
148 GtkWidgetClass *widget_class;
149 GtkContainerClass *container_class;
151 g_object_class = G_OBJECT_CLASS (klass);
152 object_class = (GtkObjectClass*) klass;
153 widget_class = (GtkWidgetClass*) klass;
154 container_class = (GtkContainerClass*) klass;
156 parent_class = g_type_class_peek_parent (klass);
158 g_object_class->constructor = gtk_button_constructor;
159 g_object_class->set_property = gtk_button_set_property;
160 g_object_class->get_property = gtk_button_get_property;
162 object_class->destroy = gtk_button_destroy;
164 widget_class->realize = gtk_button_realize;
165 widget_class->unrealize = gtk_button_unrealize;
166 widget_class->map = gtk_button_map;
167 widget_class->unmap = gtk_button_unmap;
168 widget_class->size_request = gtk_button_size_request;
169 widget_class->size_allocate = gtk_button_size_allocate;
170 widget_class->expose_event = gtk_button_expose;
171 widget_class->button_press_event = gtk_button_button_press;
172 widget_class->button_release_event = gtk_button_button_release;
173 widget_class->key_release_event = gtk_button_key_release;
174 widget_class->enter_notify_event = gtk_button_enter_notify;
175 widget_class->leave_notify_event = gtk_button_leave_notify;
177 container_class->child_type = gtk_button_child_type;
179 klass->pressed = gtk_real_button_pressed;
180 klass->released = gtk_real_button_released;
181 klass->clicked = NULL;
182 klass->enter = gtk_button_update_state;
183 klass->leave = gtk_button_update_state;
184 klass->activate = gtk_real_button_activate;
186 g_object_class_install_property (G_OBJECT_CLASS(object_class),
188 g_param_spec_string ("label",
190 _("Text of the label widget inside the button, if the button contains a label widget"),
192 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
194 g_object_class_install_property (G_OBJECT_CLASS(object_class),
196 g_param_spec_boolean ("use_underline",
198 _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
200 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
202 g_object_class_install_property (G_OBJECT_CLASS(object_class),
204 g_param_spec_boolean ("use_stock",
206 _("If set, the label is used to pick a stock item instead of being displayed"),
208 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
210 g_object_class_install_property (G_OBJECT_CLASS(object_class),
212 g_param_spec_enum ("relief",
214 _("The border relief style"),
215 GTK_TYPE_RELIEF_STYLE,
217 G_PARAM_READABLE | G_PARAM_WRITABLE));
219 button_signals[PRESSED] =
220 gtk_signal_new ("pressed",
222 GTK_CLASS_TYPE (object_class),
223 GTK_SIGNAL_OFFSET (GtkButtonClass, pressed),
224 _gtk_marshal_VOID__VOID,
226 button_signals[RELEASED] =
227 gtk_signal_new ("released",
229 GTK_CLASS_TYPE (object_class),
230 GTK_SIGNAL_OFFSET (GtkButtonClass, released),
231 _gtk_marshal_VOID__VOID,
233 button_signals[CLICKED] =
234 gtk_signal_new ("clicked",
235 GTK_RUN_FIRST | GTK_RUN_ACTION,
236 GTK_CLASS_TYPE (object_class),
237 GTK_SIGNAL_OFFSET (GtkButtonClass, clicked),
238 _gtk_marshal_VOID__VOID,
240 button_signals[ENTER] =
241 gtk_signal_new ("enter",
243 GTK_CLASS_TYPE (object_class),
244 GTK_SIGNAL_OFFSET (GtkButtonClass, enter),
245 _gtk_marshal_VOID__VOID,
247 button_signals[LEAVE] =
248 gtk_signal_new ("leave",
250 GTK_CLASS_TYPE (object_class),
251 GTK_SIGNAL_OFFSET (GtkButtonClass, leave),
252 _gtk_marshal_VOID__VOID,
254 button_signals[ACTIVATE] =
255 gtk_signal_new ("activate",
256 GTK_RUN_FIRST | GTK_RUN_ACTION,
257 GTK_CLASS_TYPE (object_class),
258 GTK_SIGNAL_OFFSET (GtkButtonClass, activate),
259 _gtk_marshal_VOID__VOID,
261 widget_class->activate_signal = button_signals[ACTIVATE];
263 gtk_widget_class_install_style_property (widget_class,
264 g_param_spec_boxed ("default_border",
265 _("Default Spacing"),
266 _("Extra space to add for CAN_DEFAULT buttons"),
270 gtk_widget_class_install_style_property (widget_class,
271 g_param_spec_boxed ("default_outside_border",
272 _("Default Outside Spacing"),
273 _("Extra space to add for CAN_DEFAULT buttons that is always drawn outside the border"),
276 gtk_widget_class_install_style_property (widget_class,
277 g_param_spec_int ("child_displacement_x",
278 _("Child X Displacement"),
279 _("How far in the x direction to move the child when the button is depressed"),
284 gtk_widget_class_install_style_property (widget_class,
285 g_param_spec_int ("child_displacement_y",
286 _("Child Y Displacement"),
287 _("How far in the y direction to move the child when the button is depressed"),
295 gtk_button_init (GtkButton *button)
297 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
298 GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);
300 button->label_text = NULL;
302 button->constructed = FALSE;
303 button->in_button = FALSE;
304 button->button_down = FALSE;
305 button->relief = GTK_RELIEF_NORMAL;
306 button->use_stock = FALSE;
307 button->use_underline = FALSE;
308 button->depressed = FALSE;
309 button->depress_on_activate = TRUE;
313 gtk_button_destroy (GtkObject *object)
315 GtkButton *button = GTK_BUTTON (object);
317 if (button->label_text)
319 g_free (button->label_text);
320 button->label_text = NULL;
323 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
327 gtk_button_constructor (GType type,
328 guint n_construct_properties,
329 GObjectConstructParam *construct_params)
334 object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
335 n_construct_properties,
338 button = GTK_BUTTON (object);
339 button->constructed = TRUE;
341 if (button->label_text != NULL)
342 gtk_button_construct_child (button);
349 gtk_button_child_type (GtkContainer *container)
351 if (!GTK_BIN (container)->child)
352 return GTK_TYPE_WIDGET;
354 return GTK_TYPE_NONE;
358 gtk_button_set_property (GObject *object,
365 button = GTK_BUTTON (object);
370 gtk_button_set_label (button, g_value_get_string (value));
373 gtk_button_set_relief (button, g_value_get_enum (value));
375 case PROP_USE_UNDERLINE:
376 gtk_button_set_use_underline (button, g_value_get_boolean (value));
379 gtk_button_set_use_stock (button, g_value_get_boolean (value));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388 gtk_button_get_property (GObject *object,
395 button = GTK_BUTTON (object);
400 g_value_set_string (value, button->label_text);
403 g_value_set_enum (value, gtk_button_get_relief (button));
405 case PROP_USE_UNDERLINE:
406 g_value_set_boolean (value, button->use_underline);
409 g_value_set_boolean (value, button->use_stock);
412 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
418 gtk_button_new (void)
420 return GTK_WIDGET (gtk_type_new (gtk_button_get_type ()));
424 gtk_button_construct_child (GtkButton *button)
432 if (!button->constructed)
435 if (button->label_text == NULL)
438 if (GTK_BIN (button)->child)
439 gtk_container_remove (GTK_CONTAINER (button),
440 GTK_BIN (button)->child);
443 if (button->use_stock &&
444 gtk_stock_lookup (button->label_text, &item))
446 label = gtk_label_new_with_mnemonic (item.label);
448 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
450 image = gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON);
451 hbox = gtk_hbox_new (FALSE, 2);
453 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
455 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
456 gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
458 gtk_container_add (GTK_CONTAINER (button), align);
459 gtk_container_add (GTK_CONTAINER (align), hbox);
460 gtk_widget_show_all (align);
465 if (button->use_underline)
467 label = gtk_label_new_with_mnemonic (button->label_text);
468 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
471 label = gtk_label_new (button->label_text);
473 gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
475 gtk_widget_show (label);
476 gtk_container_add (GTK_CONTAINER (button), label);
481 gtk_button_new_with_label (const gchar *label)
483 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
487 * gtk_button_new_from_stock:
488 * @stock_id: the name of the stock item
490 * Creates a new #GtkButton containing the image and text from a stock item.
491 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
494 * If @stock_id is unknown, then it will be treated as a mnemonic
495 * label (as for gtk_button_new_with_mnemonic()).
497 * Returns: a new #GtkButton
500 gtk_button_new_from_stock (const gchar *stock_id)
502 return g_object_new (GTK_TYPE_BUTTON,
505 "use_underline", TRUE,
510 * gtk_button_new_with_mnemonic:
511 * @label: The text of the button, with an underscore in front of the
513 * @returns: a new #GtkButton
515 * Creates a new #GtkButton containing a label.
516 * If characters in @label are preceded by an underscore, they are underlined.
517 * If you need a literal underscore character in a label, use '__' (two
518 * underscores). The first underlined character represents a keyboard
519 * accelerator called a mnemonic.
520 * Pressing Alt and that key activates the button.
523 gtk_button_new_with_mnemonic (const gchar *label)
525 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use_underline", TRUE, NULL);
529 gtk_button_pressed (GtkButton *button)
531 g_return_if_fail (GTK_IS_BUTTON (button));
533 gtk_signal_emit (GTK_OBJECT (button), button_signals[PRESSED]);
537 gtk_button_released (GtkButton *button)
539 g_return_if_fail (GTK_IS_BUTTON (button));
541 gtk_signal_emit (GTK_OBJECT (button), button_signals[RELEASED]);
545 gtk_button_clicked (GtkButton *button)
547 g_return_if_fail (GTK_IS_BUTTON (button));
549 gtk_signal_emit (GTK_OBJECT (button), button_signals[CLICKED]);
553 gtk_button_enter (GtkButton *button)
555 g_return_if_fail (GTK_IS_BUTTON (button));
557 gtk_signal_emit (GTK_OBJECT (button), button_signals[ENTER]);
561 gtk_button_leave (GtkButton *button)
563 g_return_if_fail (GTK_IS_BUTTON (button));
565 gtk_signal_emit (GTK_OBJECT (button), button_signals[LEAVE]);
569 gtk_button_set_relief (GtkButton *button,
570 GtkReliefStyle newrelief)
572 g_return_if_fail (GTK_IS_BUTTON (button));
574 if (newrelief != button->relief)
576 button->relief = newrelief;
577 g_object_notify (G_OBJECT (button), "relief");
578 gtk_widget_queue_draw (GTK_WIDGET (button));
583 gtk_button_get_relief (GtkButton *button)
585 g_return_val_if_fail (button != NULL, GTK_RELIEF_NORMAL);
586 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
588 return button->relief;
592 gtk_button_realize (GtkWidget *widget)
595 GdkWindowAttr attributes;
596 gint attributes_mask;
599 button = GTK_BUTTON (widget);
600 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
602 border_width = GTK_CONTAINER (widget)->border_width;
604 attributes.window_type = GDK_WINDOW_CHILD;
605 attributes.x = widget->allocation.x + border_width;
606 attributes.y = widget->allocation.y + border_width;
607 attributes.width = widget->allocation.width - border_width * 2;
608 attributes.height = widget->allocation.height - border_width * 2;
609 attributes.wclass = GDK_INPUT_ONLY;
610 attributes.event_mask = gtk_widget_get_events (widget);
611 attributes.event_mask |= (GDK_EXPOSURE_MASK |
612 GDK_BUTTON_PRESS_MASK |
613 GDK_BUTTON_RELEASE_MASK |
614 GDK_ENTER_NOTIFY_MASK |
615 GDK_LEAVE_NOTIFY_MASK);
617 attributes_mask = GDK_WA_X | GDK_WA_Y;
619 widget->window = gtk_widget_get_parent_window (widget);
620 gdk_window_ref (widget->window);
622 button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
623 &attributes, attributes_mask);
624 gdk_window_set_user_data (button->event_window, button);
626 widget->style = gtk_style_attach (widget->style, widget->window);
630 gtk_button_unrealize (GtkWidget *widget)
632 GtkButton *button = GTK_BUTTON (widget);
634 if (button->activate_timeout)
635 gtk_button_finish_activate (button, FALSE);
637 if (button->event_window)
639 gdk_window_set_user_data (button->event_window, NULL);
640 gdk_window_destroy (button->event_window);
641 button->event_window = NULL;
644 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
648 gtk_button_map (GtkWidget *widget)
650 GtkButton *button = GTK_BUTTON (widget);
652 g_return_if_fail (GTK_IS_BUTTON (widget));
654 GTK_WIDGET_CLASS (parent_class)->map (widget);
656 if (button->event_window)
657 gdk_window_show (button->event_window);
661 gtk_button_unmap (GtkWidget *widget)
663 GtkButton *button = GTK_BUTTON (widget);
665 g_return_if_fail (GTK_IS_BUTTON (widget));
667 if (button->event_window)
668 gdk_window_hide (button->event_window);
670 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
674 gtk_button_get_props (GtkButton *button,
675 GtkBorder *default_border,
676 GtkBorder *default_outside_border,
677 gboolean *interior_focus)
679 GtkWidget *widget = GTK_WIDGET (button);
680 GtkBorder *tmp_border;
684 gtk_widget_style_get (widget, "default_border", &tmp_border, NULL);
688 *default_border = *tmp_border;
692 *default_border = default_default_border;
695 if (default_outside_border)
697 gtk_widget_style_get (widget, "default_outside_border", &tmp_border, NULL);
701 *default_outside_border = *tmp_border;
705 *default_outside_border = default_default_outside_border;
709 gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
713 gtk_button_size_request (GtkWidget *widget,
714 GtkRequisition *requisition)
716 GtkButton *button = GTK_BUTTON (widget);
717 GtkBorder default_border;
718 gboolean interior_focus;
722 gtk_button_get_props (button, &default_border, NULL, &interior_focus);
723 gtk_widget_style_get (GTK_WIDGET (widget),
724 "focus-line-width", &focus_width,
725 "focus-padding", &focus_pad,
728 requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
729 GTK_WIDGET (widget)->style->xthickness) * 2;
730 requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
731 GTK_WIDGET (widget)->style->ythickness) * 2;
733 if (GTK_WIDGET_CAN_DEFAULT (widget))
735 requisition->width += default_border.left + default_border.right;
736 requisition->height += default_border.top + default_border.bottom;
739 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
741 GtkRequisition child_requisition;
743 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
745 requisition->width += child_requisition.width;
746 requisition->height += child_requisition.height;
749 requisition->width += 2 * (focus_width + focus_pad);
750 requisition->height += 2 * (focus_width + focus_pad);
754 gtk_button_size_allocate (GtkWidget *widget,
755 GtkAllocation *allocation)
757 GtkButton *button = GTK_BUTTON (widget);
758 GtkAllocation child_allocation;
760 gint border_width = GTK_CONTAINER (widget)->border_width;
761 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
762 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
763 GtkBorder default_border;
765 gtk_button_get_props (button, &default_border, NULL, NULL);
767 widget->allocation = *allocation;
769 if (GTK_WIDGET_REALIZED (widget))
770 gdk_window_move_resize (button->event_window,
771 widget->allocation.x + border_width,
772 widget->allocation.y + border_width,
773 widget->allocation.width - border_width * 2,
774 widget->allocation.height - border_width * 2);
776 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
778 child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
779 child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
781 child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
783 child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
786 if (GTK_WIDGET_CAN_DEFAULT (button))
788 child_allocation.x += default_border.left;
789 child_allocation.y += default_border.top;
790 child_allocation.width = MAX (1, child_allocation.width - default_border.left - default_border.right);
791 child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
794 if (button->depressed)
796 gint child_displacement_x;
797 gint child_displacement_y;
799 gtk_widget_style_get (widget,
800 "child_displacement_x", &child_displacement_x,
801 "child_displacement_y", &child_displacement_y,
803 child_allocation.x += child_displacement_x;
804 child_allocation.y += child_displacement_y;
807 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
812 _gtk_button_paint (GtkButton *button,
814 GtkStateType state_type,
815 GtkShadowType shadow_type,
816 const gchar *main_detail,
817 const gchar *default_detail)
823 GtkBorder default_border;
824 GtkBorder default_outside_border;
825 gboolean interior_focus;
829 if (GTK_WIDGET_DRAWABLE (button))
831 widget = GTK_WIDGET (button);
832 border_width = GTK_CONTAINER (widget)->border_width;
834 gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
835 gtk_widget_style_get (GTK_WIDGET (widget),
836 "focus-line-width", &focus_width,
837 "focus-padding", &focus_pad,
840 x = widget->allocation.x + border_width;
841 y = widget->allocation.y + border_width;
842 width = widget->allocation.width - border_width * 2;
843 height = widget->allocation.height - border_width * 2;
845 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
846 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
848 gtk_paint_box (widget->style, widget->window,
849 GTK_STATE_NORMAL, GTK_SHADOW_IN,
850 area, widget, "buttondefault",
851 x, y, width, height);
853 x += default_border.left;
854 y += default_border.top;
855 width -= default_border.left + default_border.right;
856 height -= default_border.top + default_border.bottom;
858 else if (GTK_WIDGET_CAN_DEFAULT (widget))
860 x += default_outside_border.left;
861 y += default_outside_border.top;
862 width -= default_outside_border.left + default_outside_border.right;
863 height -= default_outside_border.top + default_outside_border.bottom;
866 if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
868 x += focus_width + focus_pad;
869 y += focus_width + focus_pad;
870 width -= 2 * (focus_width + focus_pad);
871 height -= 2 * (focus_width + focus_pad);
874 if ((button->relief != GTK_RELIEF_NONE) ||
875 ((GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL) &&
876 (GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)))
877 gtk_paint_box (widget->style, widget->window,
879 shadow_type, area, widget, "button",
880 x, y, width, height);
882 if (GTK_WIDGET_HAS_FOCUS (widget))
886 x += widget->style->xthickness + focus_pad;
887 y += widget->style->ythickness + focus_pad;
888 width -= 2 * (widget->style->xthickness + focus_pad);
889 height -= 2 * (widget->style->xthickness + focus_pad);
893 x -= focus_width + focus_pad;
894 y -= focus_width + focus_pad;
895 width += 2 * (focus_width + focus_pad);
896 height += 2 * (focus_width + focus_pad);
899 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
900 area, widget, "button",
901 x, y, width, height);
907 gtk_button_expose (GtkWidget *widget,
908 GdkEventExpose *event)
910 if (GTK_WIDGET_DRAWABLE (widget))
912 GtkButton *button = GTK_BUTTON (widget);
914 _gtk_button_paint (button, &event->area,
915 GTK_WIDGET_STATE (widget),
916 button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
917 "button", "buttondefault");
919 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
926 gtk_button_button_press (GtkWidget *widget,
927 GdkEventButton *event)
931 if (event->type == GDK_BUTTON_PRESS)
933 button = GTK_BUTTON (widget);
935 if (!GTK_WIDGET_HAS_FOCUS (widget))
936 gtk_widget_grab_focus (widget);
938 if (event->button == 1)
939 gtk_button_pressed (button);
946 gtk_button_button_release (GtkWidget *widget,
947 GdkEventButton *event)
951 if (event->button == 1)
953 button = GTK_BUTTON (widget);
954 gtk_button_released (button);
961 gtk_button_key_release (GtkWidget *widget,
964 GtkButton *button = GTK_BUTTON (widget);
966 if (button->activate_timeout)
968 gtk_button_finish_activate (button, TRUE);
971 else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
972 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
978 gtk_button_enter_notify (GtkWidget *widget,
979 GdkEventCrossing *event)
982 GtkWidget *event_widget;
984 button = GTK_BUTTON (widget);
985 event_widget = gtk_get_event_widget ((GdkEvent*) event);
987 if ((event_widget == widget) &&
988 (event->detail != GDK_NOTIFY_INFERIOR))
990 button->in_button = TRUE;
991 gtk_button_enter (button);
998 gtk_button_leave_notify (GtkWidget *widget,
999 GdkEventCrossing *event)
1002 GtkWidget *event_widget;
1004 button = GTK_BUTTON (widget);
1005 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1007 if ((event_widget == widget) &&
1008 (event->detail != GDK_NOTIFY_INFERIOR))
1010 button->in_button = FALSE;
1011 gtk_button_leave (button);
1018 gtk_real_button_pressed (GtkButton *button)
1020 if (button->activate_timeout)
1023 button->button_down = TRUE;
1024 gtk_button_update_state (button);
1028 gtk_real_button_released (GtkButton *button)
1030 if (button->button_down)
1032 button->button_down = FALSE;
1034 if (button->activate_timeout)
1037 if (button->in_button)
1038 gtk_button_clicked (button);
1040 gtk_button_update_state (button);
1045 button_activate_timeout (gpointer data)
1047 GDK_THREADS_ENTER ();
1049 gtk_button_finish_activate (data, TRUE);
1051 GDK_THREADS_LEAVE ();
1057 gtk_real_button_activate (GtkButton *button)
1059 GtkWidget *widget = GTK_WIDGET (button);
1061 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1063 if (gdk_keyboard_grab (button->event_window, TRUE,
1064 gtk_get_current_event_time ()) == 0)
1066 gtk_grab_add (widget);
1068 button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1069 button_activate_timeout,
1071 button->button_down = TRUE;
1072 gtk_button_update_state (button);
1073 gtk_widget_queue_draw (GTK_WIDGET (button));
1079 gtk_button_finish_activate (GtkButton *button,
1082 GtkWidget *widget = GTK_WIDGET (button);
1084 g_source_remove (button->activate_timeout);
1085 button->activate_timeout = 0;
1087 gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1088 gtk_get_current_event_time ());
1089 gtk_grab_remove (widget);
1091 button->button_down = FALSE;
1093 gtk_button_update_state (button);
1094 gtk_widget_queue_draw (GTK_WIDGET (button));
1097 gtk_button_clicked (button);
1101 * gtk_button_set_label:
1102 * @button: a #GtkButton
1105 * Sets the text of the label of the button to @str. This text is
1106 * also used to select the stock item if gtk_button_set_use_stock()
1109 * This will also clear any previously set labels.
1112 gtk_button_set_label (GtkButton *button,
1115 g_return_if_fail (GTK_IS_BUTTON (button));
1117 g_free (button->label_text);
1118 button->label_text = g_strdup (label);
1120 gtk_button_construct_child (button);
1122 g_object_notify (G_OBJECT (button), "label");
1126 * gtk_button_get_label:
1127 * @button: a #GtkButton
1129 * Fetches the text from the label of the button, as set by
1130 * gtk_button_set_label(). If the label text has not
1131 * been set the return value will be %NULL. This will be the
1132 * case if you create an empty button with gtk_button_new() to
1133 * use as a container.
1135 * Return value: The text of the label widget. This string is owned
1136 * by the widget and must not be modified or freed.
1138 G_CONST_RETURN gchar *
1139 gtk_button_get_label (GtkButton *button)
1141 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1143 return button->label_text;
1147 * gtk_button_set_use_underline:
1148 * @button: a #GtkButton
1149 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1151 * If true, an underline in the text of the button label indicates
1152 * the next character should be used for the mnemonic accelerator key.
1155 gtk_button_set_use_underline (GtkButton *button,
1156 gboolean use_underline)
1158 g_return_if_fail (GTK_IS_BUTTON (button));
1160 use_underline = use_underline != FALSE;
1162 if (use_underline != button->use_underline)
1164 button->use_underline = use_underline;
1166 gtk_button_construct_child (button);
1168 g_object_notify (G_OBJECT (button), "use_underline");
1173 * gtk_button_get_use_underline:
1174 * @button: a #GtkButton
1176 * Returns whether an embedded underline in the button label indicates a
1177 * mnemonic. See gtk_button_set_use_underline ().
1179 * Return value: %TRUE if an embedded underline in the button label
1180 * indicates the mnemonic accelerator keys.
1183 gtk_button_get_use_underline (GtkButton *button)
1185 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1187 return button->use_underline;
1191 * gtk_button_set_use_stock:
1192 * @button: a #GtkButton
1193 * @use_stock: %TRUE if the button should use a stock item
1195 * If true, the label set on the button is used as a
1196 * stock id to select the stock item for the button.
1199 gtk_button_set_use_stock (GtkButton *button,
1202 g_return_if_fail (GTK_IS_BUTTON (button));
1204 use_stock = use_stock != FALSE;
1206 if (use_stock != button->use_stock)
1208 button->use_stock = use_stock;
1210 gtk_button_construct_child (button);
1212 g_object_notify (G_OBJECT (button), "use_stock");
1217 * gtk_button_get_use_stock:
1218 * @button: a #GtkButton
1220 * Returns whether the button label is a stock item.
1222 * Return value: %TRUE if the button label is used to
1223 * select a stock item instead of being
1224 * used directly as the label text.
1227 gtk_button_get_use_stock (GtkButton *button)
1229 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1231 return button->use_stock;
1235 * _gtk_button_set_depressed:
1236 * @button: a #GtkButton
1237 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1239 * Sets whether the button is currently drawn as down or not. This is
1240 * purely a visual setting, and is meant only for use by derived widgets
1241 * such as #GtkToggleButton.
1244 _gtk_button_set_depressed (GtkButton *button,
1247 GtkWidget *widget = GTK_WIDGET (button);
1249 depressed = depressed != FALSE;
1251 if (depressed != button->depressed)
1253 button->depressed = depressed;
1254 gtk_widget_queue_resize (widget);
1259 gtk_button_update_state (GtkButton *button)
1262 GtkStateType new_state;
1264 if (button->activate_timeout)
1265 depressed = button->depress_on_activate;
1267 depressed = button->in_button && button->button_down;
1269 if (button->in_button && (!button->button_down || !depressed))
1270 new_state = GTK_STATE_PRELIGHT;
1272 new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1274 _gtk_button_set_depressed (button, depressed);
1275 gtk_widget_set_state (GTK_WIDGET (button), new_state);