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
39 #define DEFAULT_SPACING 7
41 /* Time out before giving up on getting a key release when animatng
44 #define ACTIVATE_TIMEOUT 250
62 static void gtk_button_class_init (GtkButtonClass *klass);
63 static void gtk_button_init (GtkButton *button);
64 static void gtk_button_set_property (GObject *object,
68 static void gtk_button_get_property (GObject *object,
72 static void gtk_button_realize (GtkWidget *widget);
73 static void gtk_button_unrealize (GtkWidget *widget);
74 static void gtk_button_size_request (GtkWidget *widget,
75 GtkRequisition *requisition);
76 static void gtk_button_size_allocate (GtkWidget *widget,
77 GtkAllocation *allocation);
78 static void gtk_button_paint (GtkWidget *widget,
80 static gint gtk_button_expose (GtkWidget *widget,
81 GdkEventExpose *event);
82 static gint gtk_button_button_press (GtkWidget *widget,
83 GdkEventButton *event);
84 static gint gtk_button_button_release (GtkWidget *widget,
85 GdkEventButton *event);
86 static gint gtk_button_key_release (GtkWidget *widget,
88 static gint gtk_button_enter_notify (GtkWidget *widget,
89 GdkEventCrossing *event);
90 static gint gtk_button_leave_notify (GtkWidget *widget,
91 GdkEventCrossing *event);
92 static void gtk_button_add (GtkContainer *container,
94 static void gtk_button_remove (GtkContainer *container,
96 static void gtk_real_button_pressed (GtkButton *button);
97 static void gtk_real_button_released (GtkButton *button);
98 static void gtk_real_button_enter (GtkButton *button);
99 static void gtk_real_button_leave (GtkButton *button);
100 static void gtk_real_button_activate (GtkButton *button);
101 static GtkType gtk_button_child_type (GtkContainer *container);
103 static void gtk_button_finish_activate (GtkButton *button,
106 static GtkBinClass *parent_class = NULL;
107 static guint button_signals[LAST_SIGNAL] = { 0 };
111 gtk_button_get_type (void)
113 static GtkType button_type = 0;
117 static const GTypeInfo button_info =
119 sizeof (GtkButtonClass),
120 NULL, /* base_init */
121 NULL, /* base_finalize */
122 (GClassInitFunc) gtk_button_class_init,
123 NULL, /* class_finalize */
124 NULL, /* class_data */
126 16, /* n_preallocs */
127 (GInstanceInitFunc) gtk_button_init,
130 button_type = g_type_register_static (GTK_TYPE_BIN, "GtkButton", &button_info, 0);
137 gtk_button_class_init (GtkButtonClass *klass)
139 GtkObjectClass *object_class;
140 GtkWidgetClass *widget_class;
141 GtkContainerClass *container_class;
143 object_class = (GtkObjectClass*) klass;
144 widget_class = (GtkWidgetClass*) klass;
145 container_class = (GtkContainerClass*) klass;
147 parent_class = g_type_class_peek_parent (klass);
149 G_OBJECT_CLASS(object_class)->set_property = gtk_button_set_property;
150 G_OBJECT_CLASS(object_class)->get_property = gtk_button_get_property;
152 widget_class->realize = gtk_button_realize;
153 widget_class->unrealize = gtk_button_unrealize;
154 widget_class->size_request = gtk_button_size_request;
155 widget_class->size_allocate = gtk_button_size_allocate;
156 widget_class->expose_event = gtk_button_expose;
157 widget_class->button_press_event = gtk_button_button_press;
158 widget_class->button_release_event = gtk_button_button_release;
159 widget_class->key_release_event = gtk_button_key_release;
160 widget_class->enter_notify_event = gtk_button_enter_notify;
161 widget_class->leave_notify_event = gtk_button_leave_notify;
163 container_class->add = gtk_button_add;
164 container_class->remove = gtk_button_remove;
165 container_class->child_type = gtk_button_child_type;
167 klass->pressed = gtk_real_button_pressed;
168 klass->released = gtk_real_button_released;
169 klass->clicked = NULL;
170 klass->enter = gtk_real_button_enter;
171 klass->leave = gtk_real_button_leave;
172 klass->activate = gtk_real_button_activate;
174 g_object_class_install_property (G_OBJECT_CLASS(object_class),
176 g_param_spec_string ("label",
178 _("Text of the label widget inside the button, if the button contains a label widget."),
180 G_PARAM_READABLE | G_PARAM_WRITABLE));
182 g_object_class_install_property (G_OBJECT_CLASS(object_class),
184 g_param_spec_enum ("relief",
186 _("The border relief style."),
187 GTK_TYPE_RELIEF_STYLE,
189 G_PARAM_READABLE | G_PARAM_WRITABLE));
191 button_signals[PRESSED] =
192 gtk_signal_new ("pressed",
194 GTK_CLASS_TYPE (object_class),
195 GTK_SIGNAL_OFFSET (GtkButtonClass, pressed),
196 gtk_marshal_VOID__VOID,
198 button_signals[RELEASED] =
199 gtk_signal_new ("released",
201 GTK_CLASS_TYPE (object_class),
202 GTK_SIGNAL_OFFSET (GtkButtonClass, released),
203 gtk_marshal_VOID__VOID,
205 button_signals[CLICKED] =
206 gtk_signal_new ("clicked",
207 GTK_RUN_FIRST | GTK_RUN_ACTION,
208 GTK_CLASS_TYPE (object_class),
209 GTK_SIGNAL_OFFSET (GtkButtonClass, clicked),
210 gtk_marshal_VOID__VOID,
212 button_signals[ENTER] =
213 gtk_signal_new ("enter",
215 GTK_CLASS_TYPE (object_class),
216 GTK_SIGNAL_OFFSET (GtkButtonClass, enter),
217 gtk_marshal_VOID__VOID,
219 button_signals[LEAVE] =
220 gtk_signal_new ("leave",
222 GTK_CLASS_TYPE (object_class),
223 GTK_SIGNAL_OFFSET (GtkButtonClass, leave),
224 gtk_marshal_VOID__VOID,
226 button_signals[ACTIVATE] =
227 gtk_signal_new ("activate",
229 GTK_CLASS_TYPE (object_class),
230 GTK_SIGNAL_OFFSET (GtkButtonClass, activate),
231 gtk_marshal_VOID__VOID,
233 widget_class->activate_signal = button_signals[ACTIVATE];
235 gtk_widget_class_install_style_property (widget_class,
236 g_param_spec_int ("default_spacing",
237 _("Default Spacing"),
238 _("Extra space to add for CAN_DEFAULT buttons"),
246 gtk_button_init (GtkButton *button)
248 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
249 GTK_WIDGET_UNSET_FLAGS (button, GTK_NO_WINDOW);
251 button->child = NULL;
252 button->in_button = FALSE;
253 button->button_down = FALSE;
254 button->relief = GTK_RELIEF_NORMAL;
258 gtk_button_child_type (GtkContainer *container)
260 if (!GTK_BIN (container)->child)
261 return GTK_TYPE_WIDGET;
263 return GTK_TYPE_NONE;
267 gtk_button_set_property (GObject *object,
274 button = GTK_BUTTON (object);
281 child = GTK_BIN (button)->child;
283 child = gtk_widget_new (GTK_TYPE_LABEL,
287 if (GTK_IS_LABEL (child))
289 gtk_label_set_text (GTK_LABEL (child),
290 g_value_get_string (value) ? g_value_get_string (value) : "");
294 gtk_button_set_relief (button, g_value_get_enum (value));
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 gtk_button_get_property (GObject *object,
310 button = GTK_BUTTON (object);
315 if (GTK_BIN (button)->child && GTK_IS_LABEL (GTK_BIN (button)->child))
316 g_value_set_string (value, GTK_LABEL (GTK_BIN (button)->child)->label);
318 g_value_set_string (value, NULL);
321 g_value_set_enum (value, gtk_button_get_relief (button));
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 gtk_button_new (void)
332 return GTK_WIDGET (gtk_type_new (gtk_button_get_type ()));
336 gtk_button_new_with_label (const gchar *label)
339 GtkWidget *label_widget;
341 button = gtk_button_new ();
342 label_widget = gtk_label_new (label);
343 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
345 gtk_container_add (GTK_CONTAINER (button), label_widget);
346 gtk_widget_show (label_widget);
352 * gtk_button_new_from_stock:
353 * @stock_id: the name of the stock item
354 * @returns: a new #GtkButton
356 * Creates a new #GtkButton containing the image and text from a stock item.
357 * Some stock ids have preprocessor macros like #GTK_STOCK_BUTTON_OK and
358 * #GTK_STOCK_BUTTON_APPLY.
361 gtk_button_new_from_stock (const gchar *stock_id)
366 if (gtk_stock_lookup (stock_id, &item))
372 button = gtk_button_new ();
374 label = gtk_label_new_with_mnemonic (item.label);
376 gtk_label_set_mnemonic_widget (GTK_LABEL (label), button);
378 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
379 hbox = gtk_hbox_new (FALSE, 0);
381 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 2);
382 gtk_box_pack_end (GTK_BOX (hbox), label, TRUE, TRUE, 2);
384 gtk_container_add (GTK_CONTAINER (button), hbox);
385 gtk_widget_show_all (hbox);
389 button = gtk_button_new_with_mnemonic (stock_id);
396 * gtk_button_new_with_mnemonic:
397 * @label: The text of the button, with an underscore in front of the
399 * @returns: a new #GtkButton
401 * Creates a new #GtkButton containing a label.
402 * If characters in @label are preceded by an underscore, they are underlined
403 * indicating that they represent a keyboard accelerator called a mnemonic.
404 * Pressing Alt and that key activates the button.
407 gtk_button_new_with_mnemonic (const gchar *label)
410 GtkWidget *label_widget;
412 button = gtk_button_new ();
414 label_widget = gtk_label_new_with_mnemonic (label);
416 gtk_label_set_mnemonic_widget (GTK_LABEL (label_widget), button);
418 gtk_container_add (GTK_CONTAINER (button), label_widget);
419 gtk_widget_show (label_widget);
425 gtk_button_pressed (GtkButton *button)
427 g_return_if_fail (button != NULL);
428 g_return_if_fail (GTK_IS_BUTTON (button));
430 gtk_signal_emit (GTK_OBJECT (button), button_signals[PRESSED]);
434 gtk_button_released (GtkButton *button)
436 g_return_if_fail (button != NULL);
437 g_return_if_fail (GTK_IS_BUTTON (button));
439 gtk_signal_emit (GTK_OBJECT (button), button_signals[RELEASED]);
443 gtk_button_clicked (GtkButton *button)
445 g_return_if_fail (button != NULL);
446 g_return_if_fail (GTK_IS_BUTTON (button));
448 gtk_signal_emit (GTK_OBJECT (button), button_signals[CLICKED]);
452 gtk_button_enter (GtkButton *button)
454 g_return_if_fail (button != NULL);
455 g_return_if_fail (GTK_IS_BUTTON (button));
457 gtk_signal_emit (GTK_OBJECT (button), button_signals[ENTER]);
461 gtk_button_leave (GtkButton *button)
463 g_return_if_fail (button != NULL);
464 g_return_if_fail (GTK_IS_BUTTON (button));
466 gtk_signal_emit (GTK_OBJECT (button), button_signals[LEAVE]);
470 gtk_button_set_relief (GtkButton *button,
471 GtkReliefStyle newrelief)
473 g_return_if_fail (button != NULL);
474 g_return_if_fail (GTK_IS_BUTTON (button));
476 button->relief = newrelief;
477 g_object_notify(G_OBJECT(button), "relief");
478 gtk_widget_queue_draw (GTK_WIDGET (button));
482 gtk_button_get_relief (GtkButton *button)
484 g_return_val_if_fail (button != NULL, GTK_RELIEF_NORMAL);
485 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
487 return button->relief;
491 gtk_button_realize (GtkWidget *widget)
494 GdkWindowAttr attributes;
495 gint attributes_mask;
498 g_return_if_fail (widget != NULL);
499 g_return_if_fail (GTK_IS_BUTTON (widget));
501 button = GTK_BUTTON (widget);
502 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
504 border_width = GTK_CONTAINER (widget)->border_width;
506 attributes.window_type = GDK_WINDOW_CHILD;
507 attributes.x = widget->allocation.x + border_width;
508 attributes.y = widget->allocation.y + border_width;
509 attributes.width = widget->allocation.width - border_width * 2;
510 attributes.height = widget->allocation.height - border_width * 2;
511 attributes.wclass = GDK_INPUT_OUTPUT;
512 attributes.visual = gtk_widget_get_visual (widget);
513 attributes.colormap = gtk_widget_get_colormap (widget);
514 attributes.event_mask = gtk_widget_get_events (widget);
515 attributes.event_mask |= (GDK_EXPOSURE_MASK |
516 GDK_BUTTON_PRESS_MASK |
517 GDK_BUTTON_RELEASE_MASK |
518 GDK_ENTER_NOTIFY_MASK |
519 GDK_LEAVE_NOTIFY_MASK);
521 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
523 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
524 gdk_window_set_user_data (widget->window, button);
526 widget->style = gtk_style_attach (widget->style, widget->window);
527 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
531 gtk_button_unrealize (GtkWidget *widget)
533 GtkButton *button = GTK_BUTTON (widget);
535 if (button->activate_timeout)
536 gtk_button_finish_activate (button, FALSE);
538 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
542 gtk_button_get_props (GtkButton *button,
543 gint *default_spacing,
544 gboolean *interior_focus)
546 GtkWidget *widget = GTK_WIDGET (button);
549 gtk_widget_style_get (widget, "default_spacing", default_spacing, NULL);
552 gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
556 gtk_button_size_request (GtkWidget *widget,
557 GtkRequisition *requisition)
559 GtkButton *button = GTK_BUTTON (widget);
560 gint default_spacing;
561 gboolean interior_focus;
563 gtk_button_get_props (button, &default_spacing, &interior_focus);
565 requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
566 GTK_WIDGET (widget)->style->xthickness) * 2;
567 requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
568 GTK_WIDGET (widget)->style->ythickness) * 2;
570 if (GTK_WIDGET_CAN_DEFAULT (widget))
572 requisition->width += (GTK_WIDGET (widget)->style->xthickness * 2 +
574 requisition->height += (GTK_WIDGET (widget)->style->ythickness * 2 +
578 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
580 GtkRequisition child_requisition;
582 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
584 requisition->width += child_requisition.width;
585 requisition->height += child_requisition.height;
590 requisition->width += 2;
591 requisition->height += 2;
596 gtk_button_size_allocate (GtkWidget *widget,
597 GtkAllocation *allocation)
599 GtkButton *button = GTK_BUTTON (widget);
600 GtkAllocation child_allocation;
602 gint border_width = GTK_CONTAINER (widget)->border_width;
603 gint xthickness = GTK_WIDGET (widget)->style->xthickness;
604 gint ythickness = GTK_WIDGET (widget)->style->ythickness;
605 gint default_spacing;
607 gtk_button_get_props (button, &default_spacing, NULL);
609 widget->allocation = *allocation;
611 if (GTK_WIDGET_REALIZED (widget))
612 gdk_window_move_resize (widget->window,
613 widget->allocation.x + border_width,
614 widget->allocation.y + border_width,
615 widget->allocation.width - border_width * 2,
616 widget->allocation.height - border_width * 2);
618 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
620 child_allocation.x = (CHILD_SPACING + xthickness);
621 child_allocation.y = (CHILD_SPACING + ythickness);
623 child_allocation.width = MAX (1, (gint)widget->allocation.width - child_allocation.x * 2 -
625 child_allocation.height = MAX (1, (gint)widget->allocation.height - child_allocation.y * 2 -
628 if (GTK_WIDGET_CAN_DEFAULT (button))
630 child_allocation.x += (GTK_WIDGET (widget)->style->xthickness +
631 (1 + default_spacing) / 2);
632 child_allocation.y += (GTK_WIDGET (widget)->style->ythickness +
633 (1 + default_spacing) / 2);
634 child_allocation.width = MAX (1, (gint)child_allocation.width -
635 (gint)(GTK_WIDGET (widget)->style->xthickness * 2 + default_spacing));
636 child_allocation.height = MAX (1, (gint)child_allocation.height -
637 (gint)(GTK_WIDGET (widget)->style->xthickness * 2 + default_spacing));
640 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
645 * +------------------------------------------------+
647 * | +------------------------------------------+ |
648 * | |\\\\\\\\\\\\\\\\DEFAULT\\\\\\\\\\\\\\\\\ | |
649 * | |\\+------------------------------------+ | |
650 * | |\\| | SPACING 3 | | | |
651 * | |\\| +--------------------------------+ | | |
652 * | |\\| |########## FOCUS ###############| | | |
653 * | |\\| |#+----------------------------+#| | | |
654 * | |\\| |#| RELIEF \|#| | | |
655 * | |\\| |#| +-----------------------+\|#| | | |
656 * | |\\|1|#| + THE TEXT +\|#|2| | |
657 * | |\\| |#| +-----------------------+\|#| | | |
658 * | |\\| |#| \\\\\ ythickness \\\\\\\\\\|#| | | |
659 * | |\\| |#+----------------------------+#| | | |
660 * | |\\| |########### 1 ##################| | | |
661 * | |\\| +--------------------------------+ | | |
662 * | |\\| | default spacing 4 | | | |
663 * | |\\+------------------------------------+ | |
664 * | |\ ythickness | |
665 * | +------------------------------------------+ |
667 * +------------------------------------------------+
671 gtk_button_paint (GtkWidget *widget,
675 GtkShadowType shadow_type;
678 gint default_spacing;
679 gboolean interior_focus;
681 if (GTK_WIDGET_DRAWABLE (widget))
683 button = GTK_BUTTON (widget);
685 gtk_button_get_props (button, &default_spacing, &interior_focus);
689 width = widget->allocation.width - GTK_CONTAINER (widget)->border_width * 2;
690 height = widget->allocation.height - GTK_CONTAINER (widget)->border_width * 2;
692 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
693 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
695 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
696 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
698 gtk_paint_box (widget->style, widget->window,
699 GTK_STATE_NORMAL, GTK_SHADOW_IN,
700 area, widget, "buttondefault",
701 x, y, width, height);
704 if (GTK_WIDGET_CAN_DEFAULT (widget))
706 x += widget->style->xthickness;
707 y += widget->style->ythickness;
708 width -= 2 * x + default_spacing;
709 height -= 2 * y + default_spacing;
710 x += (1 + default_spacing) / 2;
711 y += (1 + default_spacing) / 2;
714 if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
722 if (GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE)
723 shadow_type = GTK_SHADOW_IN;
725 shadow_type = GTK_SHADOW_OUT;
727 if ((button->relief != GTK_RELIEF_NONE) ||
728 ((GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL) &&
729 (GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)))
730 gtk_paint_box (widget->style, widget->window,
731 GTK_WIDGET_STATE (widget),
732 shadow_type, area, widget, "button",
733 x, y, width, height);
735 if (GTK_WIDGET_HAS_FOCUS (widget))
739 x += widget->style->xthickness + 1;
740 y += widget->style->ythickness + 1;
741 width -= 2 * (widget->style->xthickness + 1);
742 height -= 2 * (widget->style->xthickness + 1);
752 gtk_paint_focus (widget->style, widget->window,
753 area, widget, "button",
754 x, y, width - 1, height - 1);
760 gtk_button_expose (GtkWidget *widget,
761 GdkEventExpose *event)
765 g_return_val_if_fail (widget != NULL, FALSE);
766 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
767 g_return_val_if_fail (event != NULL, FALSE);
769 if (GTK_WIDGET_DRAWABLE (widget))
771 bin = GTK_BIN (widget);
773 gtk_button_paint (widget, &event->area);
775 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
782 gtk_button_button_press (GtkWidget *widget,
783 GdkEventButton *event)
787 g_return_val_if_fail (widget != NULL, FALSE);
788 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
789 g_return_val_if_fail (event != NULL, FALSE);
791 if (event->type == GDK_BUTTON_PRESS)
793 button = GTK_BUTTON (widget);
795 if (!GTK_WIDGET_HAS_FOCUS (widget))
796 gtk_widget_grab_focus (widget);
798 if (event->button == 1)
799 gtk_button_pressed (button);
806 gtk_button_button_release (GtkWidget *widget,
807 GdkEventButton *event)
811 g_return_val_if_fail (widget != NULL, FALSE);
812 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
813 g_return_val_if_fail (event != NULL, FALSE);
815 if (event->button == 1)
817 button = GTK_BUTTON (widget);
818 gtk_button_released (button);
825 gtk_button_key_release (GtkWidget *widget,
828 GtkButton *button = GTK_BUTTON (widget);
830 if (button->activate_timeout)
832 gtk_button_finish_activate (button, TRUE);
835 else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
836 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
842 gtk_button_enter_notify (GtkWidget *widget,
843 GdkEventCrossing *event)
846 GtkWidget *event_widget;
848 g_return_val_if_fail (widget != NULL, FALSE);
849 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
850 g_return_val_if_fail (event != NULL, FALSE);
852 button = GTK_BUTTON (widget);
853 event_widget = gtk_get_event_widget ((GdkEvent*) event);
855 if ((event_widget == widget) &&
856 (event->detail != GDK_NOTIFY_INFERIOR))
858 button->in_button = TRUE;
859 gtk_button_enter (button);
866 gtk_button_leave_notify (GtkWidget *widget,
867 GdkEventCrossing *event)
870 GtkWidget *event_widget;
872 g_return_val_if_fail (widget != NULL, FALSE);
873 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
874 g_return_val_if_fail (event != NULL, FALSE);
876 button = GTK_BUTTON (widget);
877 event_widget = gtk_get_event_widget ((GdkEvent*) event);
879 if ((event_widget == widget) &&
880 (event->detail != GDK_NOTIFY_INFERIOR))
882 button->in_button = FALSE;
883 gtk_button_leave (button);
890 gtk_button_add (GtkContainer *container,
893 g_return_if_fail (container != NULL);
894 g_return_if_fail (widget != NULL);
896 if (GTK_CONTAINER_CLASS (parent_class)->add)
897 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
899 GTK_BUTTON (container)->child = GTK_BIN (container)->child;
903 gtk_button_remove (GtkContainer *container,
906 g_return_if_fail (container != NULL);
907 g_return_if_fail (widget != NULL);
909 if (GTK_CONTAINER_CLASS (parent_class)->remove)
910 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
912 GTK_BUTTON (container)->child = GTK_BIN (container)->child;
916 gtk_real_button_pressed (GtkButton *button)
918 GtkStateType new_state;
920 g_return_if_fail (button != NULL);
921 g_return_if_fail (GTK_IS_BUTTON (button));
923 if (button->activate_timeout)
926 button->button_down = TRUE;
928 new_state = (button->in_button ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
930 if (GTK_WIDGET_STATE (button) != new_state)
932 gtk_widget_set_state (GTK_WIDGET (button), new_state);
933 gtk_widget_queue_draw (GTK_WIDGET (button));
938 gtk_real_button_released (GtkButton *button)
940 GtkStateType new_state;
942 g_return_if_fail (button != NULL);
943 g_return_if_fail (GTK_IS_BUTTON (button));
945 if (button->button_down)
947 button->button_down = FALSE;
949 if (button->activate_timeout)
952 if (button->in_button)
953 gtk_button_clicked (button);
955 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
957 if (GTK_WIDGET_STATE (button) != new_state)
959 gtk_widget_set_state (GTK_WIDGET (button), new_state);
960 /* We _draw () instead of queue_draw so that if the operation
961 * blocks, the label doesn't vanish.
963 gtk_widget_draw (GTK_WIDGET (button), NULL);
969 gtk_real_button_enter (GtkButton *button)
971 GtkStateType new_state;
973 g_return_if_fail (button != NULL);
974 g_return_if_fail (GTK_IS_BUTTON (button));
976 new_state = (button->button_down ? GTK_STATE_ACTIVE : GTK_STATE_PRELIGHT);
978 if (button->activate_timeout)
981 if (GTK_WIDGET_STATE (button) != new_state)
983 gtk_widget_set_state (GTK_WIDGET (button), new_state);
984 gtk_widget_queue_draw (GTK_WIDGET (button));
989 gtk_real_button_leave (GtkButton *button)
991 g_return_if_fail (button != NULL);
992 g_return_if_fail (GTK_IS_BUTTON (button));
994 if (button->activate_timeout)
997 if (GTK_WIDGET_STATE (button) != GTK_STATE_NORMAL)
999 gtk_widget_set_state (GTK_WIDGET (button), GTK_STATE_NORMAL);
1000 gtk_widget_queue_draw (GTK_WIDGET (button));
1005 button_activate_timeout (gpointer data)
1007 gtk_button_finish_activate (data, TRUE);
1013 gtk_real_button_activate (GtkButton *button)
1015 GtkWidget *widget = GTK_WIDGET (button);
1017 g_return_if_fail (button != NULL);
1018 g_return_if_fail (GTK_IS_BUTTON (button));
1020 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1022 if (gdk_keyboard_grab (widget->window, TRUE,
1023 gtk_get_current_event_time ()) == 0)
1025 gtk_grab_add (widget);
1027 button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1028 button_activate_timeout,
1030 button->button_down = TRUE;
1031 gtk_widget_set_state (widget, GTK_STATE_ACTIVE);
1037 gtk_button_finish_activate (GtkButton *button,
1040 GtkWidget *widget = GTK_WIDGET (button);
1042 g_source_remove (button->activate_timeout);
1043 button->activate_timeout = 0;
1045 gdk_keyboard_ungrab (gtk_get_current_event_time ());
1046 gtk_grab_remove (widget);
1048 button->button_down = FALSE;
1049 gtk_widget_set_state (GTK_WIDGET (button),
1050 button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
1053 gtk_button_clicked (button);