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 "gtkbutton.h"
31 #include "gtksignal.h"
35 #include "gtkiconfactory.h"
38 #define CHILD_SPACING 1
40 static GtkBorder default_default_border = { 1, 1, 1, 1 };
41 static GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
43 /* Time out before giving up on getting a key release when animatng
46 #define ACTIVATE_TIMEOUT 250
66 static void gtk_button_class_init (GtkButtonClass *klass);
67 static void gtk_button_init (GtkButton *button);
68 static void gtk_button_set_property (GObject *object,
72 static void gtk_button_get_property (GObject *object,
76 static void gtk_button_realize (GtkWidget *widget);
77 static void gtk_button_unrealize (GtkWidget *widget);
78 static void gtk_button_size_request (GtkWidget *widget,
79 GtkRequisition *requisition);
80 static void gtk_button_size_allocate (GtkWidget *widget,
81 GtkAllocation *allocation);
82 static void gtk_button_paint (GtkWidget *widget,
84 static gint gtk_button_expose (GtkWidget *widget,
85 GdkEventExpose *event);
86 static gint gtk_button_button_press (GtkWidget *widget,
87 GdkEventButton *event);
88 static gint gtk_button_button_release (GtkWidget *widget,
89 GdkEventButton *event);
90 static gint gtk_button_key_release (GtkWidget *widget,
92 static gint gtk_button_enter_notify (GtkWidget *widget,
93 GdkEventCrossing *event);
94 static gint gtk_button_leave_notify (GtkWidget *widget,
95 GdkEventCrossing *event);
96 static void gtk_real_button_pressed (GtkButton *button);
97 static void gtk_real_button_released (GtkButton *button);
98 static void gtk_real_button_activate (GtkButton *button);
99 static void gtk_button_update_state (GtkButton *button);
100 static GtkType gtk_button_child_type (GtkContainer *container);
101 static void gtk_button_finish_activate (GtkButton *button,
104 static GObject* gtk_button_constructor (GType type,
105 guint n_construct_properties,
106 GObjectConstructParam *construct_params);
107 static void gtk_button_construct_child (GtkButton *button);
110 static GtkBinClass *parent_class = NULL;
111 static guint button_signals[LAST_SIGNAL] = { 0 };
115 gtk_button_get_type (void)
117 static GtkType button_type = 0;
121 static const GTypeInfo button_info =
123 sizeof (GtkButtonClass),
124 NULL, /* base_init */
125 NULL, /* base_finalize */
126 (GClassInitFunc) gtk_button_class_init,
127 NULL, /* class_finalize */
128 NULL, /* class_data */
130 16, /* n_preallocs */
131 (GInstanceInitFunc) gtk_button_init,
134 button_type = g_type_register_static (GTK_TYPE_BIN, "GtkButton", &button_info, 0);
141 gtk_button_class_init (GtkButtonClass *klass)
143 GObjectClass *g_object_class;
144 GtkObjectClass *object_class;
145 GtkWidgetClass *widget_class;
146 GtkContainerClass *container_class;
148 g_object_class = G_OBJECT_CLASS (klass);
149 object_class = (GtkObjectClass*) klass;
150 widget_class = (GtkWidgetClass*) klass;
151 container_class = (GtkContainerClass*) klass;
153 parent_class = g_type_class_peek_parent (klass);
155 g_object_class->constructor = gtk_button_constructor;
156 g_object_class->set_property = gtk_button_set_property;
157 g_object_class->get_property = gtk_button_get_property;
159 widget_class->realize = gtk_button_realize;
160 widget_class->unrealize = gtk_button_unrealize;
161 widget_class->size_request = gtk_button_size_request;
162 widget_class->size_allocate = gtk_button_size_allocate;
163 widget_class->expose_event = gtk_button_expose;
164 widget_class->button_press_event = gtk_button_button_press;
165 widget_class->button_release_event = gtk_button_button_release;
166 widget_class->key_release_event = gtk_button_key_release;
167 widget_class->enter_notify_event = gtk_button_enter_notify;
168 widget_class->leave_notify_event = gtk_button_leave_notify;
170 container_class->child_type = gtk_button_child_type;
172 klass->pressed = gtk_real_button_pressed;
173 klass->released = gtk_real_button_released;
174 klass->clicked = NULL;
175 klass->enter = gtk_button_update_state;
176 klass->leave = gtk_button_update_state;
177 klass->activate = gtk_real_button_activate;
179 g_object_class_install_property (G_OBJECT_CLASS(object_class),
181 g_param_spec_string ("label",
183 _("Text of the label widget inside the button, if the button contains a label widget."),
185 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
187 g_object_class_install_property (G_OBJECT_CLASS(object_class),
189 g_param_spec_boolean ("use_underline",
191 _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
193 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
195 g_object_class_install_property (G_OBJECT_CLASS(object_class),
197 g_param_spec_boolean ("use_stock",
199 _("If set, the label is used to pick a stock item instead of being displayed"),
201 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
203 g_object_class_install_property (G_OBJECT_CLASS(object_class),
205 g_param_spec_enum ("relief",
207 _("The border relief style."),
208 GTK_TYPE_RELIEF_STYLE,
210 G_PARAM_READABLE | G_PARAM_WRITABLE));
212 button_signals[PRESSED] =
213 gtk_signal_new ("pressed",
215 GTK_CLASS_TYPE (object_class),
216 GTK_SIGNAL_OFFSET (GtkButtonClass, pressed),
217 gtk_marshal_VOID__VOID,
219 button_signals[RELEASED] =
220 gtk_signal_new ("released",
222 GTK_CLASS_TYPE (object_class),
223 GTK_SIGNAL_OFFSET (GtkButtonClass, released),
224 gtk_marshal_VOID__VOID,
226 button_signals[CLICKED] =
227 gtk_signal_new ("clicked",
228 GTK_RUN_FIRST | GTK_RUN_ACTION,
229 GTK_CLASS_TYPE (object_class),
230 GTK_SIGNAL_OFFSET (GtkButtonClass, clicked),
231 gtk_marshal_VOID__VOID,
233 button_signals[ENTER] =
234 gtk_signal_new ("enter",
236 GTK_CLASS_TYPE (object_class),
237 GTK_SIGNAL_OFFSET (GtkButtonClass, enter),
238 gtk_marshal_VOID__VOID,
240 button_signals[LEAVE] =
241 gtk_signal_new ("leave",
243 GTK_CLASS_TYPE (object_class),
244 GTK_SIGNAL_OFFSET (GtkButtonClass, leave),
245 gtk_marshal_VOID__VOID,
247 button_signals[ACTIVATE] =
248 gtk_signal_new ("activate",
250 GTK_CLASS_TYPE (object_class),
251 GTK_SIGNAL_OFFSET (GtkButtonClass, activate),
252 gtk_marshal_VOID__VOID,
254 widget_class->activate_signal = button_signals[ACTIVATE];
256 gtk_widget_class_install_style_property (widget_class,
257 g_param_spec_boxed ("default_border",
258 _("Default Spacing"),
259 _("Extra space to add for CAN_DEFAULT buttons"),
263 gtk_widget_class_install_style_property (widget_class,
264 g_param_spec_boxed ("default_outside_border",
265 _("Default Outside Spacing"),
266 _("Extra space to add for CAN_DEFAULT buttons that is always drawn outside the border"),
269 gtk_widget_class_install_style_property (widget_class,
270 g_param_spec_int ("child_displacement_x",
271 _("Child X Displacement"),
272 _("How far in the x direction to move the child when the button is depressed"),
277 gtk_widget_class_install_style_property (widget_class,
278 g_param_spec_int ("child_displacement_y",
279 _("Child Y Displacement"),
280 _("How far in the y direction to move the child when the button is depressed"),
288 gtk_button_init (GtkButton *button)
290 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
291 GTK_WIDGET_UNSET_FLAGS (button, GTK_NO_WINDOW);
293 button->label_text = NULL;
295 button->constructed = FALSE;
296 button->in_button = FALSE;
297 button->button_down = FALSE;
298 button->relief = GTK_RELIEF_NORMAL;
299 button->use_stock = FALSE;
300 button->use_underline = FALSE;
301 button->depressed = FALSE;
305 gtk_button_constructor (GType type,
306 guint n_construct_properties,
307 GObjectConstructParam *construct_params)
312 object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
313 n_construct_properties,
316 button = GTK_BUTTON (object);
317 button->constructed = TRUE;
319 if (button->label_text != NULL)
320 gtk_button_construct_child (button);
327 gtk_button_child_type (GtkContainer *container)
329 if (!GTK_BIN (container)->child)
330 return GTK_TYPE_WIDGET;
332 return GTK_TYPE_NONE;
336 gtk_button_set_property (GObject *object,
343 button = GTK_BUTTON (object);
348 gtk_button_set_label (button, g_value_get_string (value));
351 gtk_button_set_relief (button, g_value_get_enum (value));
353 case PROP_USE_UNDERLINE:
354 gtk_button_set_use_underline (button, g_value_get_boolean (value));
357 gtk_button_set_use_stock (button, g_value_get_boolean (value));
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gtk_button_get_property (GObject *object,
373 button = GTK_BUTTON (object);
378 g_value_set_string (value, button->label_text);
381 g_value_set_enum (value, gtk_button_get_relief (button));
383 case PROP_USE_UNDERLINE:
384 g_value_set_boolean (value, button->use_underline);
387 g_value_set_boolean (value, button->use_stock);
390 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
396 gtk_button_new (void)
398 return GTK_WIDGET (gtk_type_new (gtk_button_get_type ()));
402 gtk_button_construct_child (GtkButton *button)
409 if (!button->constructed)
412 if (button->label_text == NULL)
415 if (GTK_BIN (button)->child)
416 gtk_container_remove (GTK_CONTAINER (button),
417 GTK_BIN (button)->child);
420 if (button->use_stock &&
421 gtk_stock_lookup (button->label_text, &item))
423 label = gtk_label_new_with_mnemonic (item.label);
425 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
427 image = gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON);
428 hbox = gtk_hbox_new (FALSE, 1);
430 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
431 gtk_box_pack_end (GTK_BOX (hbox), label, TRUE, TRUE, 0);
433 gtk_container_add (GTK_CONTAINER (button), hbox);
434 gtk_widget_show_all (hbox);
439 if (button->use_underline)
441 label = gtk_label_new_with_mnemonic (button->label_text);
442 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
445 label = gtk_label_new (button->label_text);
447 gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
449 gtk_widget_show (label);
450 gtk_container_add (GTK_CONTAINER (button), label);
455 gtk_button_new_with_label (const gchar *label)
457 return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
461 * gtk_button_new_from_stock:
462 * @stock_id: the name of the stock item
463 * @returns: a new #GtkButton
465 * Creates a new #GtkButton containing the image and text from a stock item.
466 * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
470 gtk_button_new_from_stock (const gchar *stock_id)
472 return g_object_new (GTK_TYPE_BUTTON, "label", stock_id, "use_stock", TRUE, NULL);
476 * gtk_button_new_with_mnemonic:
477 * @label: The text of the button, with an underscore in front of the
479 * @returns: a new #GtkButton
481 * Creates a new #GtkButton containing a label.
482 * If characters in @label are preceded by an underscore, they are underlined
483 * indicating that they represent a keyboard accelerator called a mnemonic.
484 * Pressing Alt and that key activates the button.
487 gtk_button_new_with_mnemonic (const gchar *label)
489 return g_object_new (GTK_TYPE_BUTTON, "label", label, "use_underline", TRUE, NULL);
493 gtk_button_pressed (GtkButton *button)
495 g_return_if_fail (GTK_IS_BUTTON (button));
497 gtk_signal_emit (GTK_OBJECT (button), button_signals[PRESSED]);
501 gtk_button_released (GtkButton *button)
503 g_return_if_fail (GTK_IS_BUTTON (button));
505 gtk_signal_emit (GTK_OBJECT (button), button_signals[RELEASED]);
509 gtk_button_clicked (GtkButton *button)
511 g_return_if_fail (GTK_IS_BUTTON (button));
513 gtk_signal_emit (GTK_OBJECT (button), button_signals[CLICKED]);
517 gtk_button_enter (GtkButton *button)
519 g_return_if_fail (GTK_IS_BUTTON (button));
521 gtk_signal_emit (GTK_OBJECT (button), button_signals[ENTER]);
525 gtk_button_leave (GtkButton *button)
527 g_return_if_fail (GTK_IS_BUTTON (button));
529 gtk_signal_emit (GTK_OBJECT (button), button_signals[LEAVE]);
533 gtk_button_set_relief (GtkButton *button,
534 GtkReliefStyle newrelief)
536 g_return_if_fail (GTK_IS_BUTTON (button));
538 button->relief = newrelief;
539 g_object_notify(G_OBJECT(button), "relief");
540 gtk_widget_queue_draw (GTK_WIDGET (button));
544 gtk_button_get_relief (GtkButton *button)
546 g_return_val_if_fail (button != NULL, GTK_RELIEF_NORMAL);
547 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
549 return button->relief;
553 gtk_button_realize (GtkWidget *widget)
556 GdkWindowAttr attributes;
557 gint attributes_mask;
560 g_return_if_fail (GTK_IS_BUTTON (widget));
562 button = GTK_BUTTON (widget);
563 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
565 border_width = GTK_CONTAINER (widget)->border_width;
567 attributes.window_type = GDK_WINDOW_CHILD;
568 attributes.x = widget->allocation.x + border_width;
569 attributes.y = widget->allocation.y + border_width;
570 attributes.width = widget->allocation.width - border_width * 2;
571 attributes.height = widget->allocation.height - border_width * 2;
572 attributes.wclass = GDK_INPUT_OUTPUT;
573 attributes.visual = gtk_widget_get_visual (widget);
574 attributes.colormap = gtk_widget_get_colormap (widget);
575 attributes.event_mask = gtk_widget_get_events (widget);
576 attributes.event_mask |= (GDK_EXPOSURE_MASK |
577 GDK_BUTTON_PRESS_MASK |
578 GDK_BUTTON_RELEASE_MASK |
579 GDK_ENTER_NOTIFY_MASK |
580 GDK_LEAVE_NOTIFY_MASK);
582 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
584 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
585 gdk_window_set_user_data (widget->window, button);
587 widget->style = gtk_style_attach (widget->style, widget->window);
588 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
592 gtk_button_unrealize (GtkWidget *widget)
594 GtkButton *button = GTK_BUTTON (widget);
596 if (button->activate_timeout)
597 gtk_button_finish_activate (button, FALSE);
599 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
603 gtk_button_get_props (GtkButton *button,
604 GtkBorder *default_border,
605 GtkBorder *default_outside_border,
606 gboolean *interior_focus)
608 GtkWidget *widget = GTK_WIDGET (button);
609 GtkBorder *tmp_border;
613 gtk_widget_style_get (widget, "default_border", &tmp_border, NULL);
617 *default_border = *tmp_border;
621 *default_border = default_default_border;
624 if (default_outside_border)
626 gtk_widget_style_get (widget, "default_outside_border", &tmp_border, NULL);
630 *default_outside_border = *tmp_border;
634 *default_outside_border = default_default_outside_border;
638 gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
642 gtk_button_size_request (GtkWidget *widget,
643 GtkRequisition *requisition)
645 GtkButton *button = GTK_BUTTON (widget);
646 GtkBorder default_border;
647 gboolean interior_focus;
649 gtk_button_get_props (button, &default_border, NULL, &interior_focus);
651 requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
652 GTK_WIDGET (widget)->style->xthickness) * 2;
653 requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
654 GTK_WIDGET (widget)->style->ythickness) * 2;
656 if (GTK_WIDGET_CAN_DEFAULT (widget))
658 requisition->width += default_border.left + default_border.right;
659 requisition->height += default_border.top + default_border.bottom;
662 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
664 GtkRequisition child_requisition;
666 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
668 requisition->width += child_requisition.width;
669 requisition->height += child_requisition.height;
674 requisition->width += 2;
675 requisition->height += 2;
680 gtk_button_size_allocate (GtkWidget *widget,
681 GtkAllocation *allocation)
683 GtkButton *button = GTK_BUTTON (widget);
684 GtkAllocation child_allocation;
686 gint border_width = GTK_CONTAINER (widget)->border_width;
687 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
688 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
689 GtkBorder default_border;
691 gtk_button_get_props (button, &default_border, NULL, NULL);
693 widget->allocation = *allocation;
695 if (GTK_WIDGET_REALIZED (widget))
696 gdk_window_move_resize (widget->window,
697 widget->allocation.x + border_width,
698 widget->allocation.y + border_width,
699 widget->allocation.width - border_width * 2,
700 widget->allocation.height - border_width * 2);
702 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
704 child_allocation.x = (CHILD_SPACING + xthickness);
705 child_allocation.y = (CHILD_SPACING + ythickness);
707 child_allocation.width = MAX (1, (gint)widget->allocation.width - child_allocation.x * 2 -
709 child_allocation.height = MAX (1, (gint)widget->allocation.height - child_allocation.y * 2 -
712 if (GTK_WIDGET_CAN_DEFAULT (button))
714 child_allocation.x += default_border.left;
715 child_allocation.y += default_border.top;
716 child_allocation.width = MAX (1, child_allocation.width - default_border.left - default_border.right);
717 child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
720 if (button->depressed)
722 gint child_displacement_x;
723 gint child_displacement_y;
725 gtk_widget_style_get (widget,
726 "child_displacement_x", &child_displacement_x,
727 "child_displacement_y", &child_displacement_y,
729 child_allocation.x += child_displacement_x;
730 child_allocation.y += child_displacement_y;
733 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
738 * +------------------------------------------------+
740 * | +------------------------------------------+ |
741 * | |\\\\\\\\\\\\\\\\DEFAULT\\\\\\\\\\\\\\\\\ | |
742 * | |\\+------------------------------------+ | |
743 * | |\\| | SPACING 3 | | | |
744 * | |\\| +--------------------------------+ | | |
745 * | |\\| |########## FOCUS ###############| | | |
746 * | |\\| |#+----------------------------+#| | | |
747 * | |\\| |#| RELIEF \|#| | | |
748 * | |\\| |#| +-----------------------+\|#| | | |
749 * | |\\|1|#| + THE TEXT +\|#|2| | |
750 * | |\\| |#| +-----------------------+\|#| | | |
751 * | |\\| |#| \\\\\ ythickness \\\\\\\\\\|#| | | |
752 * | |\\| |#+----------------------------+#| | | |
753 * | |\\| |########### 1 ##################| | | |
754 * | |\\| +--------------------------------+ | | |
755 * | |\\| | default spacing 4 | | | |
756 * | |\\+------------------------------------+ | |
757 * | |\ ythickness | |
758 * | +------------------------------------------+ |
760 * +------------------------------------------------+
764 gtk_button_paint (GtkWidget *widget,
768 GtkShadowType shadow_type;
771 GtkBorder default_border;
772 GtkBorder default_outside_border;
773 gboolean interior_focus;
775 if (GTK_WIDGET_DRAWABLE (widget))
777 button = GTK_BUTTON (widget);
779 gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
783 width = widget->allocation.width - GTK_CONTAINER (widget)->border_width * 2;
784 height = widget->allocation.height - GTK_CONTAINER (widget)->border_width * 2;
786 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
787 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
789 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
790 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
792 gtk_paint_box (widget->style, widget->window,
793 GTK_STATE_NORMAL, GTK_SHADOW_IN,
794 area, widget, "buttondefault",
795 x, y, width, height);
797 x += default_border.left;
798 y += default_border.top;
799 width -= default_border.left + default_border.right;
800 height -= default_border.top + default_border.bottom;
802 else if (GTK_WIDGET_CAN_DEFAULT (widget))
804 x += default_outside_border.left;
805 y += default_outside_border.top;
806 width -= default_outside_border.left + default_outside_border.right;
807 height -= default_outside_border.top + default_outside_border.bottom;
810 if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
818 shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
820 if ((button->relief != GTK_RELIEF_NONE) ||
821 ((GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL) &&
822 (GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)))
823 gtk_paint_box (widget->style, widget->window,
824 GTK_WIDGET_STATE (widget),
825 shadow_type, area, widget, "button",
826 x, y, width, height);
828 if (GTK_WIDGET_HAS_FOCUS (widget))
832 x += widget->style->xthickness + 1;
833 y += widget->style->ythickness + 1;
834 width -= 2 * (widget->style->xthickness + 1);
835 height -= 2 * (widget->style->xthickness + 1);
845 gtk_paint_focus (widget->style, widget->window,
846 area, widget, "button",
847 x, y, width - 1, height - 1);
853 gtk_button_expose (GtkWidget *widget,
854 GdkEventExpose *event)
858 g_return_val_if_fail (widget != NULL, FALSE);
859 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
860 g_return_val_if_fail (event != NULL, FALSE);
862 if (GTK_WIDGET_DRAWABLE (widget))
864 bin = GTK_BIN (widget);
866 gtk_button_paint (widget, &event->area);
868 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
875 gtk_button_button_press (GtkWidget *widget,
876 GdkEventButton *event)
880 g_return_val_if_fail (widget != NULL, FALSE);
881 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
882 g_return_val_if_fail (event != NULL, FALSE);
884 if (event->type == GDK_BUTTON_PRESS)
886 button = GTK_BUTTON (widget);
888 if (!GTK_WIDGET_HAS_FOCUS (widget))
889 gtk_widget_grab_focus (widget);
891 if (event->button == 1)
892 gtk_button_pressed (button);
899 gtk_button_button_release (GtkWidget *widget,
900 GdkEventButton *event)
904 g_return_val_if_fail (widget != NULL, FALSE);
905 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
906 g_return_val_if_fail (event != NULL, FALSE);
908 if (event->button == 1)
910 button = GTK_BUTTON (widget);
911 gtk_button_released (button);
918 gtk_button_key_release (GtkWidget *widget,
921 GtkButton *button = GTK_BUTTON (widget);
923 if (button->activate_timeout)
925 gtk_button_finish_activate (button, TRUE);
928 else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
929 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
935 gtk_button_enter_notify (GtkWidget *widget,
936 GdkEventCrossing *event)
939 GtkWidget *event_widget;
941 g_return_val_if_fail (widget != NULL, FALSE);
942 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
943 g_return_val_if_fail (event != NULL, FALSE);
945 button = GTK_BUTTON (widget);
946 event_widget = gtk_get_event_widget ((GdkEvent*) event);
948 if ((event_widget == widget) &&
949 (event->detail != GDK_NOTIFY_INFERIOR))
951 button->in_button = TRUE;
952 gtk_button_enter (button);
959 gtk_button_leave_notify (GtkWidget *widget,
960 GdkEventCrossing *event)
963 GtkWidget *event_widget;
965 g_return_val_if_fail (widget != NULL, FALSE);
966 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
967 g_return_val_if_fail (event != NULL, FALSE);
969 button = GTK_BUTTON (widget);
970 event_widget = gtk_get_event_widget ((GdkEvent*) event);
972 if ((event_widget == widget) &&
973 (event->detail != GDK_NOTIFY_INFERIOR))
975 button->in_button = FALSE;
976 gtk_button_leave (button);
983 gtk_real_button_pressed (GtkButton *button)
985 if (button->activate_timeout)
988 button->button_down = TRUE;
989 gtk_button_update_state (button);
993 gtk_real_button_released (GtkButton *button)
995 if (button->button_down)
997 button->button_down = FALSE;
999 if (button->activate_timeout)
1002 if (button->in_button)
1003 gtk_button_clicked (button);
1005 gtk_button_update_state (button);
1010 button_activate_timeout (gpointer data)
1012 GDK_THREADS_ENTER ();
1014 gtk_button_finish_activate (data, TRUE);
1016 GDK_THREADS_LEAVE ();
1022 gtk_real_button_activate (GtkButton *button)
1024 GtkWidget *widget = GTK_WIDGET (button);
1026 g_return_if_fail (GTK_IS_BUTTON (button));
1028 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1030 if (gdk_keyboard_grab (widget->window, TRUE,
1031 gtk_get_current_event_time ()) == 0)
1033 gtk_grab_add (widget);
1035 button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1036 button_activate_timeout,
1038 button->button_down = TRUE;
1039 gtk_button_update_state (button);
1045 gtk_button_finish_activate (GtkButton *button,
1048 GtkWidget *widget = GTK_WIDGET (button);
1050 g_source_remove (button->activate_timeout);
1051 button->activate_timeout = 0;
1053 gdk_keyboard_ungrab (gtk_get_current_event_time ());
1054 gtk_grab_remove (widget);
1056 button->button_down = FALSE;
1057 gtk_button_update_state (button);
1060 gtk_button_clicked (button);
1064 * gtk_button_set_label:
1065 * @button: a #GtkButton
1068 * Sets the text of the label of the button to @str. This text is
1069 * also used to select the stock item if gtk_button_set_use_stock()
1072 * This will also clear any previously set labels.
1075 gtk_button_set_label (GtkButton *button,
1078 g_return_if_fail (GTK_IS_BUTTON (button));
1080 g_free (button->label_text);
1081 button->label_text = g_strdup (label);
1083 gtk_button_construct_child (button);
1085 g_object_notify (G_OBJECT (button), "label");
1089 * gtk_button_get_label:
1090 * @button: a #GtkButton
1092 * Fetches the text from the label of the button, as set by
1093 * gtk_button_set_label().
1095 * Return value: the text of the label widget. This string is
1096 * owned by the widget and must not be modified or freed.
1097 * If the label text has not been set the return value
1098 * will be NULL. This will be the case if you create an
1099 * empty button with gtk_button_new() to use as a container.
1101 G_CONST_RETURN gchar *
1102 gtk_button_get_label (GtkButton *button)
1104 g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1106 return button->label_text;
1110 * gtk_button_set_use_underline:
1111 * @button: a #GtkButton
1112 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1114 * If true, an underline in the text of the button label indicates
1115 * the next character should be used for the mnemonic accelerator key.
1118 gtk_button_set_use_underline (GtkButton *button,
1119 gboolean use_underline)
1121 g_return_if_fail (GTK_IS_BUTTON (button));
1123 use_underline = use_underline != FALSE;
1125 if (use_underline != button->use_underline)
1127 button->use_underline = use_underline;
1129 gtk_button_construct_child (button);
1131 g_object_notify (G_OBJECT (button), "use_underline");
1136 * gtk_button_get_use_underline:
1137 * @label: a #GtkButton
1139 * Returns whether an embedded underline in the button label indicates a
1140 * mnemonic. See gtk_button_set_use_underline ().
1142 * Return value: %TRUE if an embedded underline in the button label
1143 * indicates the mnemonic accelerator keys.
1146 gtk_button_get_use_underline (GtkButton *button)
1148 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1150 return button->use_underline;
1154 * gtk_button_set_use_stock:
1155 * @button: a #GtkButton
1156 * @use_stock: %TRUE if the button should use a stock item
1158 * If true, the label set on the button is used as a
1159 * stock id to select the stock item for the button.
1162 gtk_button_set_use_stock (GtkButton *button,
1165 g_return_if_fail (GTK_IS_BUTTON (button));
1167 use_stock = use_stock != FALSE;
1169 if (use_stock != button->use_stock)
1171 button->use_stock = use_stock;
1173 gtk_button_construct_child (button);
1175 g_object_notify (G_OBJECT (button), "use_stock");
1180 * gtk_button_get_use_stock:
1181 * @button: a #GtkButton
1183 * Returns whether the button label is a stock item.
1185 * Return value: %TRUE if the button label is used to
1186 * select a stock item instead of being
1187 * used directly as the label text.
1190 gtk_button_get_use_stock (GtkButton *button)
1192 g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1194 return button->use_stock;
1198 * _gtk_button_set_depressed:
1199 * @button: a #GtkButton
1200 * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1202 * Sets whether the button is currently drawn as down or not. This is
1203 * purely a visual setting, and is meant only for use by derived widgets
1204 * such as #GtkToggleButton.
1207 _gtk_button_set_depressed (GtkButton *button,
1210 GtkWidget *widget = GTK_WIDGET (button);
1212 depressed = depressed != FALSE;
1214 if (depressed != button->depressed)
1216 button->depressed = depressed;
1217 gtk_widget_queue_resize (widget);
1222 gtk_button_update_state (GtkButton *button)
1225 GtkStateType new_state;
1227 depressed = button->in_button && button->button_down;
1229 if (!button->button_down && button->in_button)
1230 new_state = GTK_STATE_PRELIGHT;
1232 new_state = depressed ? GTK_STATE_ACTIVE: GTK_STATE_NORMAL;
1234 _gtk_button_set_depressed (button, depressed);
1235 gtk_widget_set_state (GTK_WIDGET (button), new_state);