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-2000. 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"
37 #define CHILD_SPACING 1
38 #define DEFAULT_LEFT_POS 4
39 #define DEFAULT_TOP_POS 4
40 #define DEFAULT_SPACING 7
42 /* Time out before giving up on getting a key release when animatng
45 #define ACTIVATE_TIMEOUT 250
63 static void gtk_button_class_init (GtkButtonClass *klass);
64 static void gtk_button_init (GtkButton *button);
65 static void gtk_button_set_arg (GtkObject *object,
68 static void gtk_button_get_arg (GtkObject *object,
71 static void gtk_button_realize (GtkWidget *widget);
72 static void gtk_button_unrealize (GtkWidget *widget);
73 static void gtk_button_size_request (GtkWidget *widget,
74 GtkRequisition *requisition);
75 static void gtk_button_size_allocate (GtkWidget *widget,
76 GtkAllocation *allocation);
77 static void gtk_button_paint (GtkWidget *widget,
79 static gint gtk_button_expose (GtkWidget *widget,
80 GdkEventExpose *event);
81 static gint gtk_button_button_press (GtkWidget *widget,
82 GdkEventButton *event);
83 static gint gtk_button_button_release (GtkWidget *widget,
84 GdkEventButton *event);
85 static gint gtk_button_key_release (GtkWidget *widget,
87 static gint gtk_button_enter_notify (GtkWidget *widget,
88 GdkEventCrossing *event);
89 static gint gtk_button_leave_notify (GtkWidget *widget,
90 GdkEventCrossing *event);
91 static void gtk_button_add (GtkContainer *container,
93 static void gtk_button_remove (GtkContainer *container,
95 static void gtk_real_button_pressed (GtkButton *button);
96 static void gtk_real_button_released (GtkButton *button);
97 static void gtk_real_button_enter (GtkButton *button);
98 static void gtk_real_button_leave (GtkButton *button);
99 static void gtk_real_button_activate (GtkButton *button);
100 static GtkType gtk_button_child_type (GtkContainer *container);
102 static void gtk_button_finish_activate (GtkButton *button,
105 static GtkBinClass *parent_class = NULL;
106 static guint button_signals[LAST_SIGNAL] = { 0 };
110 gtk_button_get_type (void)
112 static GtkType button_type = 0;
116 static const GTypeInfo button_info =
118 sizeof (GtkButtonClass),
119 NULL, /* base_init */
120 NULL, /* base_finalize */
121 (GClassInitFunc) gtk_button_class_init,
122 NULL, /* class_finalize */
123 NULL, /* class_data */
125 16, /* n_preallocs */
126 (GInstanceInitFunc) gtk_button_init,
129 button_type = g_type_register_static (GTK_TYPE_BIN, "GtkButton", &button_info, 0);
136 gtk_button_class_init (GtkButtonClass *klass)
138 GtkObjectClass *object_class;
139 GtkWidgetClass *widget_class;
140 GtkContainerClass *container_class;
142 object_class = (GtkObjectClass*) klass;
143 widget_class = (GtkWidgetClass*) klass;
144 container_class = (GtkContainerClass*) klass;
146 parent_class = g_type_class_peek_parent (klass);
148 object_class->set_arg = gtk_button_set_arg;
149 object_class->get_arg = gtk_button_get_arg;
151 widget_class->realize = gtk_button_realize;
152 widget_class->unrealize = gtk_button_unrealize;
153 widget_class->size_request = gtk_button_size_request;
154 widget_class->size_allocate = gtk_button_size_allocate;
155 widget_class->expose_event = gtk_button_expose;
156 widget_class->button_press_event = gtk_button_button_press;
157 widget_class->button_release_event = gtk_button_button_release;
158 widget_class->key_release_event = gtk_button_key_release;
159 widget_class->enter_notify_event = gtk_button_enter_notify;
160 widget_class->leave_notify_event = gtk_button_leave_notify;
162 container_class->add = gtk_button_add;
163 container_class->remove = gtk_button_remove;
164 container_class->child_type = gtk_button_child_type;
166 klass->pressed = gtk_real_button_pressed;
167 klass->released = gtk_real_button_released;
168 klass->clicked = NULL;
169 klass->enter = gtk_real_button_enter;
170 klass->leave = gtk_real_button_leave;
171 klass->activate = gtk_real_button_activate;
173 gtk_object_add_arg_type ("GtkButton::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
174 gtk_object_add_arg_type ("GtkButton::relief", GTK_TYPE_RELIEF_STYLE, GTK_ARG_READWRITE, ARG_RELIEF);
176 button_signals[PRESSED] =
177 gtk_signal_new ("pressed",
179 GTK_CLASS_TYPE (object_class),
180 GTK_SIGNAL_OFFSET (GtkButtonClass, pressed),
181 gtk_marshal_VOID__VOID,
183 button_signals[RELEASED] =
184 gtk_signal_new ("released",
186 GTK_CLASS_TYPE (object_class),
187 GTK_SIGNAL_OFFSET (GtkButtonClass, released),
188 gtk_marshal_VOID__VOID,
190 button_signals[CLICKED] =
191 gtk_signal_new ("clicked",
192 GTK_RUN_FIRST | GTK_RUN_ACTION,
193 GTK_CLASS_TYPE (object_class),
194 GTK_SIGNAL_OFFSET (GtkButtonClass, clicked),
195 gtk_marshal_VOID__VOID,
197 button_signals[ENTER] =
198 gtk_signal_new ("enter",
200 GTK_CLASS_TYPE (object_class),
201 GTK_SIGNAL_OFFSET (GtkButtonClass, enter),
202 gtk_marshal_VOID__VOID,
204 button_signals[LEAVE] =
205 gtk_signal_new ("leave",
207 GTK_CLASS_TYPE (object_class),
208 GTK_SIGNAL_OFFSET (GtkButtonClass, leave),
209 gtk_marshal_VOID__VOID,
211 button_signals[ACTIVATE] =
212 gtk_signal_new ("activate",
214 GTK_CLASS_TYPE (object_class),
215 GTK_SIGNAL_OFFSET (GtkButtonClass, activate),
216 gtk_marshal_VOID__VOID,
218 widget_class->activate_signal = button_signals[ACTIVATE];
222 gtk_button_init (GtkButton *button)
224 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
225 GTK_WIDGET_UNSET_FLAGS (button, GTK_NO_WINDOW);
227 button->child = NULL;
228 button->in_button = FALSE;
229 button->button_down = FALSE;
230 button->relief = GTK_RELIEF_NORMAL;
234 gtk_button_child_type (GtkContainer *container)
236 if (!GTK_BIN (container)->child)
237 return GTK_TYPE_WIDGET;
239 return GTK_TYPE_NONE;
243 gtk_button_set_arg (GtkObject *object,
249 button = GTK_BUTTON (object);
256 child = GTK_BIN (button)->child;
258 child = gtk_widget_new (GTK_TYPE_LABEL,
262 if (GTK_IS_LABEL (child))
263 gtk_label_set_text (GTK_LABEL (child),
264 GTK_VALUE_STRING (*arg) ? GTK_VALUE_STRING (*arg) : "");
267 gtk_button_set_relief (button, GTK_VALUE_ENUM (*arg));
275 gtk_button_get_arg (GtkObject *object,
281 button = GTK_BUTTON (object);
286 if (GTK_BIN (button)->child && GTK_IS_LABEL (GTK_BIN (button)->child))
287 GTK_VALUE_STRING (*arg) = g_strdup (GTK_LABEL (GTK_BIN (button)->child)->label);
289 GTK_VALUE_STRING (*arg) = NULL;
292 GTK_VALUE_ENUM (*arg) = gtk_button_get_relief (button);
295 arg->type = GTK_TYPE_INVALID;
301 gtk_button_new (void)
303 return GTK_WIDGET (gtk_type_new (gtk_button_get_type ()));
307 gtk_button_new_with_label (const gchar *label)
310 GtkWidget *label_widget;
312 button = gtk_button_new ();
313 label_widget = gtk_label_new (label);
314 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
316 gtk_container_add (GTK_CONTAINER (button), label_widget);
317 gtk_widget_show (label_widget);
323 * gtk_button_new_from_stock:
324 * @stock_id: the name of the stock item
325 * @returns: a new #GtkButton
327 * Creates a new #GtkButton containing the image and text from a stock item.
328 * Some stock ids have preprocessor macros like #GTK_STOCK_BUTTON_OK and
329 * #GTK_STOCK_BUTTON_APPLY.
332 gtk_button_new_from_stock (const gchar *stock_id)
337 if (gtk_stock_lookup (stock_id, &item))
343 button = gtk_button_new ();
345 label = gtk_label_new_with_mnemonic (item.label);
347 gtk_label_set_mnemonic_widget (GTK_LABEL (label), button);
349 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
350 hbox = gtk_hbox_new (FALSE, 0);
352 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 2);
353 gtk_box_pack_end (GTK_BOX (hbox), label, TRUE, TRUE, 2);
355 gtk_container_add (GTK_CONTAINER (button), hbox);
356 gtk_widget_show_all (hbox);
360 button = gtk_button_new_with_mnemonic (stock_id);
367 * gtk_button_new_with_mnemonic:
368 * @label: The text of the button, with an underscore in front of the
370 * @returns: a new #GtkButton
372 * Creates a new #GtkButton containing a label.
373 * If characters in @label are preceded by an underscore, they are underlined
374 * indicating that they represent a keyboard accelerator called a mnemonic.
375 * Pressing Alt and that key activates the button.
378 gtk_button_new_with_mnemonic (const gchar *label)
381 GtkWidget *label_widget;
383 button = gtk_button_new ();
385 label_widget = gtk_label_new_with_mnemonic (label);
387 gtk_label_set_mnemonic_widget (GTK_LABEL (label_widget), button);
389 gtk_container_add (GTK_CONTAINER (button), label_widget);
390 gtk_widget_show (label_widget);
396 gtk_button_pressed (GtkButton *button)
398 g_return_if_fail (button != NULL);
399 g_return_if_fail (GTK_IS_BUTTON (button));
401 gtk_signal_emit (GTK_OBJECT (button), button_signals[PRESSED]);
405 gtk_button_released (GtkButton *button)
407 g_return_if_fail (button != NULL);
408 g_return_if_fail (GTK_IS_BUTTON (button));
410 gtk_signal_emit (GTK_OBJECT (button), button_signals[RELEASED]);
414 gtk_button_clicked (GtkButton *button)
416 g_return_if_fail (button != NULL);
417 g_return_if_fail (GTK_IS_BUTTON (button));
419 gtk_signal_emit (GTK_OBJECT (button), button_signals[CLICKED]);
423 gtk_button_enter (GtkButton *button)
425 g_return_if_fail (button != NULL);
426 g_return_if_fail (GTK_IS_BUTTON (button));
428 gtk_signal_emit (GTK_OBJECT (button), button_signals[ENTER]);
432 gtk_button_leave (GtkButton *button)
434 g_return_if_fail (button != NULL);
435 g_return_if_fail (GTK_IS_BUTTON (button));
437 gtk_signal_emit (GTK_OBJECT (button), button_signals[LEAVE]);
441 gtk_button_set_relief (GtkButton *button,
442 GtkReliefStyle newrelief)
444 g_return_if_fail (button != NULL);
445 g_return_if_fail (GTK_IS_BUTTON (button));
447 button->relief = newrelief;
448 gtk_widget_queue_draw (GTK_WIDGET (button));
452 gtk_button_get_relief (GtkButton *button)
454 g_return_val_if_fail (button != NULL, GTK_RELIEF_NORMAL);
455 g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
457 return button->relief;
461 gtk_button_realize (GtkWidget *widget)
464 GdkWindowAttr attributes;
465 gint attributes_mask;
468 g_return_if_fail (widget != NULL);
469 g_return_if_fail (GTK_IS_BUTTON (widget));
471 button = GTK_BUTTON (widget);
472 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
474 border_width = GTK_CONTAINER (widget)->border_width;
476 attributes.window_type = GDK_WINDOW_CHILD;
477 attributes.x = widget->allocation.x + border_width;
478 attributes.y = widget->allocation.y + border_width;
479 attributes.width = widget->allocation.width - border_width * 2;
480 attributes.height = widget->allocation.height - border_width * 2;
481 attributes.wclass = GDK_INPUT_OUTPUT;
482 attributes.visual = gtk_widget_get_visual (widget);
483 attributes.colormap = gtk_widget_get_colormap (widget);
484 attributes.event_mask = gtk_widget_get_events (widget);
485 attributes.event_mask |= (GDK_EXPOSURE_MASK |
486 GDK_BUTTON_PRESS_MASK |
487 GDK_BUTTON_RELEASE_MASK |
488 GDK_ENTER_NOTIFY_MASK |
489 GDK_LEAVE_NOTIFY_MASK);
491 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
493 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
494 gdk_window_set_user_data (widget->window, button);
496 widget->style = gtk_style_attach (widget->style, widget->window);
497 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
501 gtk_button_unrealize (GtkWidget *widget)
503 GtkButton *button = GTK_BUTTON (widget);
505 if (button->activate_timeout)
506 gtk_button_finish_activate (button, FALSE);
508 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
512 gtk_button_size_request (GtkWidget *widget,
513 GtkRequisition *requisition)
517 g_return_if_fail (widget != NULL);
518 g_return_if_fail (GTK_IS_BUTTON (widget));
519 g_return_if_fail (requisition != NULL);
521 button = GTK_BUTTON (widget);
523 requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
524 GTK_WIDGET (widget)->style->xthickness) * 2;
525 requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
526 GTK_WIDGET (widget)->style->ythickness) * 2;
528 if (GTK_WIDGET_CAN_DEFAULT (widget))
530 requisition->width += (GTK_WIDGET (widget)->style->xthickness * 2 +
532 requisition->height += (GTK_WIDGET (widget)->style->ythickness * 2 +
536 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
538 GtkRequisition child_requisition;
540 gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
542 requisition->width += child_requisition.width;
543 requisition->height += child_requisition.height;
548 gtk_button_size_allocate (GtkWidget *widget,
549 GtkAllocation *allocation)
552 GtkAllocation child_allocation;
555 g_return_if_fail (widget != NULL);
556 g_return_if_fail (GTK_IS_BUTTON (widget));
557 g_return_if_fail (allocation != NULL);
559 widget->allocation = *allocation;
560 border_width = GTK_CONTAINER (widget)->border_width;
562 if (GTK_WIDGET_REALIZED (widget))
563 gdk_window_move_resize (widget->window,
564 widget->allocation.x + border_width,
565 widget->allocation.y + border_width,
566 widget->allocation.width - border_width * 2,
567 widget->allocation.height - border_width * 2);
569 button = GTK_BUTTON (widget);
571 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
573 child_allocation.x = (CHILD_SPACING + GTK_WIDGET (widget)->style->xthickness);
574 child_allocation.y = (CHILD_SPACING + GTK_WIDGET (widget)->style->ythickness);
576 child_allocation.width = MAX (1, (gint)widget->allocation.width - child_allocation.x * 2 -
578 child_allocation.height = MAX (1, (gint)widget->allocation.height - child_allocation.y * 2 -
581 if (GTK_WIDGET_CAN_DEFAULT (button))
583 child_allocation.x += (GTK_WIDGET (widget)->style->xthickness +
585 child_allocation.y += (GTK_WIDGET (widget)->style->ythickness +
587 child_allocation.width = MAX (1, (gint)child_allocation.width -
588 (gint)(GTK_WIDGET (widget)->style->xthickness * 2 + DEFAULT_SPACING));
589 child_allocation.height = MAX (1, (gint)child_allocation.height -
590 (gint)(GTK_WIDGET (widget)->style->xthickness * 2 + DEFAULT_SPACING));
593 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
598 * +------------------------------------------------+
600 * | +------------------------------------------+ |
601 * | |\\\\\\\\\\\\\\\\DEFAULT\\\\\\\\\\\\\\\\\ | |
602 * | |\\+------------------------------------+ | |
603 * | |\\| | SPACING 3 | | | |
604 * | |\\| +--------------------------------+ | | |
605 * | |\\| |########## FOCUS ###############| | | |
606 * | |\\| |#+----------------------------+#| | | |
607 * | |\\| |#| RELIEF \|#| | | |
608 * | |\\| |#| +-----------------------+\|#| | | |
609 * | |\\|1|#| + THE TEXT +\|#|2| | |
610 * | |\\| |#| +-----------------------+\|#| | | |
611 * | |\\| |#| \\\\\ ythickness \\\\\\\\\\|#| | | |
612 * | |\\| |#+----------------------------+#| | | |
613 * | |\\| |########### 1 ##################| | | |
614 * | |\\| +--------------------------------+ | | |
615 * | |\\| | default spacing 4 | | | |
616 * | |\\+------------------------------------+ | |
617 * | |\ ythickness | |
618 * | +------------------------------------------+ |
620 * +------------------------------------------------+
624 gtk_button_paint (GtkWidget *widget,
628 GtkShadowType shadow_type;
632 if (GTK_WIDGET_DRAWABLE (widget))
634 button = GTK_BUTTON (widget);
638 width = widget->allocation.width - GTK_CONTAINER (widget)->border_width * 2;
639 height = widget->allocation.height - GTK_CONTAINER (widget)->border_width * 2;
641 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
642 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
644 if (GTK_WIDGET_HAS_DEFAULT (widget) &&
645 GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
647 gtk_paint_box (widget->style, widget->window,
648 GTK_STATE_NORMAL, GTK_SHADOW_IN,
649 area, widget, "buttondefault",
650 x, y, width, height);
653 if (GTK_WIDGET_CAN_DEFAULT (widget))
655 x += widget->style->xthickness;
656 y += widget->style->ythickness;
657 width -= 2 * x + DEFAULT_SPACING;
658 height -= 2 * y + DEFAULT_SPACING;
659 x += DEFAULT_LEFT_POS;
660 y += DEFAULT_TOP_POS;
663 if (GTK_WIDGET_HAS_FOCUS (widget))
671 if (GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE)
672 shadow_type = GTK_SHADOW_IN;
674 shadow_type = GTK_SHADOW_OUT;
676 if ((button->relief != GTK_RELIEF_NONE) ||
677 ((GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL) &&
678 (GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)))
679 gtk_paint_box (widget->style, widget->window,
680 GTK_WIDGET_STATE (widget),
681 shadow_type, area, widget, "button",
682 x, y, width, height);
684 if (GTK_WIDGET_HAS_FOCUS (widget))
691 gtk_paint_focus (widget->style, widget->window,
692 area, widget, "button",
693 x, y, width - 1, height - 1);
699 gtk_button_expose (GtkWidget *widget,
700 GdkEventExpose *event)
704 g_return_val_if_fail (widget != NULL, FALSE);
705 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
706 g_return_val_if_fail (event != NULL, FALSE);
708 if (GTK_WIDGET_DRAWABLE (widget))
710 bin = GTK_BIN (widget);
712 gtk_button_paint (widget, &event->area);
714 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
721 gtk_button_button_press (GtkWidget *widget,
722 GdkEventButton *event)
726 g_return_val_if_fail (widget != NULL, FALSE);
727 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
728 g_return_val_if_fail (event != NULL, FALSE);
730 if (event->type == GDK_BUTTON_PRESS)
732 button = GTK_BUTTON (widget);
734 if (!GTK_WIDGET_HAS_FOCUS (widget))
735 gtk_widget_grab_focus (widget);
737 if (event->button == 1)
738 gtk_button_pressed (button);
745 gtk_button_button_release (GtkWidget *widget,
746 GdkEventButton *event)
750 g_return_val_if_fail (widget != NULL, FALSE);
751 g_return_val_if_fail (GTK_IS_BUTTON (widget), FALSE);
752 g_return_val_if_fail (event != NULL, FALSE);
754 if (event->button == 1)
756 button = GTK_BUTTON (widget);
757 gtk_button_released (button);
764 gtk_button_key_release (GtkWidget *widget,
767 GtkButton *button = GTK_BUTTON (widget);
769 if (button->activate_timeout)
771 gtk_button_finish_activate (button, TRUE);
774 else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
775 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
781 gtk_button_enter_notify (GtkWidget *widget,
782 GdkEventCrossing *event)
785 GtkWidget *event_widget;
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 button = GTK_BUTTON (widget);
792 event_widget = gtk_get_event_widget ((GdkEvent*) event);
794 if ((event_widget == widget) &&
795 (event->detail != GDK_NOTIFY_INFERIOR))
797 button->in_button = TRUE;
798 gtk_button_enter (button);
805 gtk_button_leave_notify (GtkWidget *widget,
806 GdkEventCrossing *event)
809 GtkWidget *event_widget;
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 button = GTK_BUTTON (widget);
816 event_widget = gtk_get_event_widget ((GdkEvent*) event);
818 if ((event_widget == widget) &&
819 (event->detail != GDK_NOTIFY_INFERIOR))
821 button->in_button = FALSE;
822 gtk_button_leave (button);
829 gtk_button_add (GtkContainer *container,
832 g_return_if_fail (container != NULL);
833 g_return_if_fail (widget != NULL);
835 if (GTK_CONTAINER_CLASS (parent_class)->add)
836 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
838 GTK_BUTTON (container)->child = GTK_BIN (container)->child;
842 gtk_button_remove (GtkContainer *container,
845 g_return_if_fail (container != NULL);
846 g_return_if_fail (widget != NULL);
848 if (GTK_CONTAINER_CLASS (parent_class)->remove)
849 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
851 GTK_BUTTON (container)->child = GTK_BIN (container)->child;
855 gtk_real_button_pressed (GtkButton *button)
857 GtkStateType new_state;
859 g_return_if_fail (button != NULL);
860 g_return_if_fail (GTK_IS_BUTTON (button));
862 if (button->activate_timeout)
865 button->button_down = TRUE;
867 new_state = (button->in_button ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
869 if (GTK_WIDGET_STATE (button) != new_state)
871 gtk_widget_set_state (GTK_WIDGET (button), new_state);
872 gtk_widget_queue_draw (GTK_WIDGET (button));
877 gtk_real_button_released (GtkButton *button)
879 GtkStateType new_state;
881 g_return_if_fail (button != NULL);
882 g_return_if_fail (GTK_IS_BUTTON (button));
884 if (button->button_down)
886 button->button_down = FALSE;
888 if (button->activate_timeout)
891 if (button->in_button)
892 gtk_button_clicked (button);
894 new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
896 if (GTK_WIDGET_STATE (button) != new_state)
898 gtk_widget_set_state (GTK_WIDGET (button), new_state);
899 /* We _draw () instead of queue_draw so that if the operation
900 * blocks, the label doesn't vanish.
902 gtk_widget_draw (GTK_WIDGET (button), NULL);
908 gtk_real_button_enter (GtkButton *button)
910 GtkStateType new_state;
912 g_return_if_fail (button != NULL);
913 g_return_if_fail (GTK_IS_BUTTON (button));
915 new_state = (button->button_down ? GTK_STATE_ACTIVE : GTK_STATE_PRELIGHT);
917 if (button->activate_timeout)
920 if (GTK_WIDGET_STATE (button) != new_state)
922 gtk_widget_set_state (GTK_WIDGET (button), new_state);
923 gtk_widget_queue_draw (GTK_WIDGET (button));
928 gtk_real_button_leave (GtkButton *button)
930 g_return_if_fail (button != NULL);
931 g_return_if_fail (GTK_IS_BUTTON (button));
933 if (button->activate_timeout)
936 if (GTK_WIDGET_STATE (button) != GTK_STATE_NORMAL)
938 gtk_widget_set_state (GTK_WIDGET (button), GTK_STATE_NORMAL);
939 gtk_widget_queue_draw (GTK_WIDGET (button));
944 button_activate_timeout (gpointer data)
946 gtk_button_finish_activate (data, TRUE);
952 gtk_real_button_activate (GtkButton *button)
954 GtkWidget *widget = GTK_WIDGET (button);
956 g_return_if_fail (button != NULL);
957 g_return_if_fail (GTK_IS_BUTTON (button));
959 if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
961 if (gdk_keyboard_grab (widget->window, TRUE,
962 gtk_get_current_event_time ()) == 0)
964 gtk_grab_add (widget);
966 button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
967 button_activate_timeout,
969 button->button_down = TRUE;
970 gtk_widget_set_state (widget, GTK_STATE_ACTIVE);
976 gtk_button_finish_activate (GtkButton *button,
979 GtkWidget *widget = GTK_WIDGET (button);
981 g_source_remove (button->activate_timeout);
982 button->activate_timeout = 0;
984 gdk_keyboard_ungrab (gtk_get_current_event_time ());
985 gtk_grab_remove (widget);
987 button->button_down = FALSE;
988 gtk_widget_set_state (GTK_WIDGET (button),
989 button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
992 gtk_button_clicked (button);