]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
Handle broken grabs.
[~andy/gtk] / gtk / gtkbutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <config.h>
28 #include <string.h>
29 #include "gtkalignment.h"
30 #include "gtkbutton.h"
31 #include "gtklabel.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkimage.h"
35 #include "gtkhbox.h"
36 #include "gtkstock.h"
37 #include "gtkiconfactory.h"
38 #include "gtkprivate.h"
39 #include "gtkintl.h"
40 #include "gtkalias.h"
41
42 #define CHILD_SPACING     1
43
44 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
45 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
46
47 /* Time out before giving up on getting a key release when animating
48  * the close button.
49  */
50 #define ACTIVATE_TIMEOUT 250
51
52 enum {
53   PRESSED,
54   RELEASED,
55   CLICKED,
56   ENTER,
57   LEAVE,
58   ACTIVATE,
59   LAST_SIGNAL
60 };
61
62 enum {
63   PROP_0,
64   PROP_LABEL,
65   PROP_IMAGE,
66   PROP_RELIEF,
67   PROP_USE_UNDERLINE,
68   PROP_USE_STOCK,
69   PROP_FOCUS_ON_CLICK,
70   PROP_XALIGN,
71   PROP_YALIGN,
72 };
73
74 #define GTK_BUTTON_GET_PRIVATE(o)       (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_BUTTON, GtkButtonPrivate))
75 typedef struct _GtkButtonPrivate GtkButtonPrivate;
76
77 struct _GtkButtonPrivate
78 {
79   gfloat       xalign;
80   gfloat       yalign;
81   GtkWidget   *image;
82   guint        align_set : 1;
83   guint        image_is_stock : 1;
84   guint        has_grab : 1;
85   guint32      grab_time;
86 };
87
88 static void gtk_button_class_init     (GtkButtonClass   *klass);
89 static void gtk_button_init           (GtkButton        *button);
90 static void gtk_button_destroy        (GtkObject        *object);
91 static void gtk_button_set_property   (GObject         *object,
92                                        guint            prop_id,
93                                        const GValue    *value,
94                                        GParamSpec      *pspec);
95 static void gtk_button_get_property   (GObject         *object,
96                                        guint            prop_id,
97                                        GValue          *value,
98                                        GParamSpec      *pspec);
99 static void gtk_button_screen_changed (GtkWidget        *widget,
100                                        GdkScreen        *previous_screen);
101 static void gtk_button_realize        (GtkWidget        *widget);
102 static void gtk_button_unrealize      (GtkWidget        *widget);
103 static void gtk_button_map            (GtkWidget        *widget);
104 static void gtk_button_unmap          (GtkWidget        *widget);
105 static void gtk_button_size_request   (GtkWidget        *widget,
106                                        GtkRequisition   *requisition);
107 static void gtk_button_size_allocate  (GtkWidget        *widget,
108                                        GtkAllocation    *allocation);
109 static gint gtk_button_expose         (GtkWidget        *widget,
110                                        GdkEventExpose   *event);
111 static gint gtk_button_button_press   (GtkWidget        *widget,
112                                        GdkEventButton   *event);
113 static gint gtk_button_button_release (GtkWidget        *widget,
114                                        GdkEventButton   *event);
115 static gint gtk_button_grab_broken    (GtkWidget        *widget,
116                                        GdkEventAny      *event);
117 static gint gtk_button_key_release    (GtkWidget        *widget,
118                                        GdkEventKey      *event);
119 static gint gtk_button_enter_notify   (GtkWidget        *widget,
120                                        GdkEventCrossing *event);
121 static gint gtk_button_leave_notify   (GtkWidget        *widget,
122                                        GdkEventCrossing *event);
123 static void gtk_real_button_pressed   (GtkButton        *button);
124 static void gtk_real_button_released  (GtkButton        *button);
125 static void gtk_real_button_activate  (GtkButton         *button);
126 static void gtk_button_update_state   (GtkButton        *button);
127 static void gtk_button_add            (GtkContainer   *container,
128                                        GtkWidget      *widget);
129 static GType gtk_button_child_type    (GtkContainer     *container);
130 static void gtk_button_finish_activate (GtkButton *button,
131                                         gboolean   do_it);
132
133 static GObject* gtk_button_constructor (GType                  type,
134                                         guint                  n_construct_properties,
135                                         GObjectConstructParam *construct_params);
136 static void gtk_button_construct_child (GtkButton             *button);
137 static void gtk_button_state_changed   (GtkWidget             *widget,
138                                         GtkStateType           previous_state);
139 static void gtk_button_grab_notify     (GtkWidget             *widget,
140                                         gboolean               was_grabbed);
141
142
143
144 static GtkBinClass *parent_class = NULL;
145 static guint button_signals[LAST_SIGNAL] = { 0 };
146
147
148 GType
149 gtk_button_get_type (void)
150 {
151   static GType button_type = 0;
152
153   if (!button_type)
154     {
155       static const GTypeInfo button_info =
156       {
157         sizeof (GtkButtonClass),
158         NULL,           /* base_init */
159         NULL,           /* base_finalize */
160         (GClassInitFunc) gtk_button_class_init,
161         NULL,           /* class_finalize */
162         NULL,           /* class_data */
163         sizeof (GtkButton),
164         16,             /* n_preallocs */
165         (GInstanceInitFunc) gtk_button_init,
166       };
167
168       button_type = g_type_register_static (GTK_TYPE_BIN, "GtkButton",
169                                             &button_info, 0);
170     }
171
172   return button_type;
173 }
174
175 static void
176 gtk_button_class_init (GtkButtonClass *klass)
177 {
178   GObjectClass *gobject_class;
179   GtkObjectClass *object_class;
180   GtkWidgetClass *widget_class;
181   GtkContainerClass *container_class;
182
183   gobject_class = G_OBJECT_CLASS (klass);
184   object_class = (GtkObjectClass*) klass;
185   widget_class = (GtkWidgetClass*) klass;
186   container_class = (GtkContainerClass*) klass;
187   
188   parent_class = g_type_class_peek_parent (klass);
189
190   gobject_class->constructor = gtk_button_constructor;
191   gobject_class->set_property = gtk_button_set_property;
192   gobject_class->get_property = gtk_button_get_property;
193
194   object_class->destroy = gtk_button_destroy;
195
196   widget_class->screen_changed = gtk_button_screen_changed;
197   widget_class->realize = gtk_button_realize;
198   widget_class->unrealize = gtk_button_unrealize;
199   widget_class->map = gtk_button_map;
200   widget_class->unmap = gtk_button_unmap;
201   widget_class->size_request = gtk_button_size_request;
202   widget_class->size_allocate = gtk_button_size_allocate;
203   widget_class->expose_event = gtk_button_expose;
204   widget_class->button_press_event = gtk_button_button_press;
205   widget_class->button_release_event = gtk_button_button_release;
206   widget_class->grab_broken_event = gtk_button_grab_broken;
207   widget_class->key_release_event = gtk_button_key_release;
208   widget_class->enter_notify_event = gtk_button_enter_notify;
209   widget_class->leave_notify_event = gtk_button_leave_notify;
210   widget_class->state_changed = gtk_button_state_changed;
211   widget_class->grab_notify = gtk_button_grab_notify;
212
213   container_class->child_type = gtk_button_child_type;
214   container_class->add = gtk_button_add;
215
216   klass->pressed = gtk_real_button_pressed;
217   klass->released = gtk_real_button_released;
218   klass->clicked = NULL;
219   klass->enter = gtk_button_update_state;
220   klass->leave = gtk_button_update_state;
221   klass->activate = gtk_real_button_activate;
222
223   g_object_class_install_property (gobject_class,
224                                    PROP_LABEL,
225                                    g_param_spec_string ("label",
226                                                         P_("Label"),
227                                                         P_("Text of the label widget inside the button, if the button contains a label widget"),
228                                                         NULL,
229                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
230   
231   g_object_class_install_property (gobject_class,
232                                    PROP_USE_UNDERLINE,
233                                    g_param_spec_boolean ("use-underline",
234                                                          P_("Use underline"),
235                                                          P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
236                                                         FALSE,
237                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
238   
239   g_object_class_install_property (gobject_class,
240                                    PROP_USE_STOCK,
241                                    g_param_spec_boolean ("use-stock",
242                                                          P_("Use stock"),
243                                                          P_("If set, the label is used to pick a stock item instead of being displayed"),
244                                                         FALSE,
245                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
246   
247   g_object_class_install_property (gobject_class,
248                                    PROP_FOCUS_ON_CLICK,
249                                    g_param_spec_boolean ("focus-on-click",
250                                                          P_("Focus on click"),
251                                                          P_("Whether the button grabs focus when it is clicked with the mouse"),
252                                                          TRUE,
253                                                          GTK_PARAM_READWRITE));
254   
255   g_object_class_install_property (gobject_class,
256                                    PROP_RELIEF,
257                                    g_param_spec_enum ("relief",
258                                                       P_("Border relief"),
259                                                       P_("The border relief style"),
260                                                       GTK_TYPE_RELIEF_STYLE,
261                                                       GTK_RELIEF_NORMAL,
262                                                       GTK_PARAM_READWRITE));
263   
264   /**
265    * GtkButton:xalign:
266    *
267    * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
268    * can be used to control it's horizontal alignment. 0.0 is left aligned, 
269    * 1.0 is right aligned.
270    * 
271    * Since: 2.4
272    */
273   g_object_class_install_property (gobject_class,
274                                    PROP_XALIGN,
275                                    g_param_spec_float("xalign",
276                                                       P_("Horizontal alignment for child"),
277                                                       P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
278                                                       0.0,
279                                                       1.0,
280                                                       0.5,
281                                                       GTK_PARAM_READWRITE));
282
283   /**
284    * GtkButton:yalign:
285    *
286    * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
287    * can be used to control it's vertical alignment. 0.0 is top aligned, 
288    * 1.0 is bottom aligned.
289    * 
290    * Since: 2.4
291    */
292   g_object_class_install_property (gobject_class,
293                                    PROP_YALIGN,
294                                    g_param_spec_float("yalign",
295                                                       P_("Vertical alignment for child"),
296                                                       P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
297                                                       0.0,
298                                                       1.0,
299                                                       0.5,
300                                                       GTK_PARAM_READWRITE));
301
302   /**
303    * GtkButton::image:
304    * 
305    * The child widget to appear next to the button text.
306    * 
307    * Since: 2.6
308    */
309   g_object_class_install_property (gobject_class,
310                                    PROP_IMAGE,
311                                    g_param_spec_object ("image",
312                                                         P_("Image widget"),
313                                                         P_("Child widget to appear next to the button text"),
314                                                         GTK_TYPE_WIDGET,
315                                                         GTK_PARAM_READWRITE));
316
317   /**
318    * GtkButton::pressed:
319    * @button: the object that received the signal
320    *
321    * Emitted when the button is pressed.
322    * 
323    * @Deprecated: Use the GtkWidget::button-press-event signal.
324    */ 
325   button_signals[PRESSED] =
326     g_signal_new ("pressed",
327                   G_OBJECT_CLASS_TYPE (object_class),
328                   G_SIGNAL_RUN_FIRST,
329                   G_STRUCT_OFFSET (GtkButtonClass, pressed),
330                   NULL, NULL,
331                   _gtk_marshal_VOID__VOID,
332                   G_TYPE_NONE, 0);
333
334   /**
335    * GtkButton::released:
336    * @button: the object that received the signal
337    *
338    * Emitted when the button is released.
339    * 
340    * @Deprecated: Use the GtkWidget::button-release-event signal.
341    */ 
342   button_signals[RELEASED] =
343     g_signal_new ("released",
344                   G_OBJECT_CLASS_TYPE (object_class),
345                   G_SIGNAL_RUN_FIRST,
346                   G_STRUCT_OFFSET (GtkButtonClass, released),
347                   NULL, NULL,
348                   _gtk_marshal_VOID__VOID,
349                   G_TYPE_NONE, 0);
350
351   /**
352    * GtkButton::clicked:
353    * @button: the object that received the signal
354    *
355    * Emitted when the button has been activated (pressed and released).
356    */ 
357   button_signals[CLICKED] =
358     g_signal_new ("clicked",
359                   G_OBJECT_CLASS_TYPE (object_class),
360                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
361                   G_STRUCT_OFFSET (GtkButtonClass, clicked),
362                   NULL, NULL,
363                   _gtk_marshal_VOID__VOID,
364                   G_TYPE_NONE, 0);
365
366   /**
367    * GtkButton::enter:
368    * @button: the object that received the signal
369    *
370    * Emitted when the pointer enters the button.
371    * 
372    * @Deprecated: Use the GtkWidget::enter-notify-event signal.
373    */ 
374   button_signals[ENTER] =
375     g_signal_new ("enter",
376                   G_OBJECT_CLASS_TYPE (object_class),
377                   G_SIGNAL_RUN_FIRST,
378                   G_STRUCT_OFFSET (GtkButtonClass, enter),
379                   NULL, NULL,
380                   _gtk_marshal_VOID__VOID,
381                   G_TYPE_NONE, 0);
382
383   /**
384    * GtkButton::leave:
385    * @button: the object that received the signal
386    *
387    * Emitted when the pointer leaves the button.
388    * 
389    * @Deprecated: Use the GtkWidget::leave-notify-event signal.
390    */ 
391   button_signals[LEAVE] =
392     g_signal_new ("leave",
393                   G_OBJECT_CLASS_TYPE (object_class),
394                   G_SIGNAL_RUN_FIRST,
395                   G_STRUCT_OFFSET (GtkButtonClass, leave),
396                   NULL, NULL,
397                   _gtk_marshal_VOID__VOID,
398                   G_TYPE_NONE, 0);
399
400   /**
401    * GtkButton::activate:
402    * @widget: the object which received the signal.
403    *
404    * The "activate" signal on GtkButton is an action signal and
405    * emitting it causes the button to animate press then release. 
406    * Applications should never connect to this signal, but use the
407    * "clicked" signal.
408    */
409   button_signals[ACTIVATE] =
410     g_signal_new ("activate",
411                   G_OBJECT_CLASS_TYPE (object_class),
412                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
413                   G_STRUCT_OFFSET (GtkButtonClass, activate),
414                   NULL, NULL,
415                   _gtk_marshal_VOID__VOID,
416                   G_TYPE_NONE, 0);
417   widget_class->activate_signal = button_signals[ACTIVATE];
418
419   gtk_widget_class_install_style_property (widget_class,
420                                            g_param_spec_boxed ("default-border",
421                                                                P_("Default Spacing"),
422                                                                P_("Extra space to add for CAN_DEFAULT buttons"),
423                                                                GTK_TYPE_BORDER,
424                                                                GTK_PARAM_READABLE));
425
426   gtk_widget_class_install_style_property (widget_class,
427                                            g_param_spec_boxed ("default-outside-border",
428                                                                P_("Default Outside Spacing"),
429                                                                P_("Extra space to add for CAN_DEFAULT buttons that is always drawn outside the border"),
430                                                                GTK_TYPE_BORDER,
431                                                                GTK_PARAM_READABLE));
432   gtk_widget_class_install_style_property (widget_class,
433                                            g_param_spec_int ("child-displacement-x",
434                                                              P_("Child X Displacement"),
435                                                              P_("How far in the x direction to move the child when the button is depressed"),
436                                                              G_MININT,
437                                                              G_MAXINT,
438                                                              0,
439                                                              GTK_PARAM_READABLE));
440   gtk_widget_class_install_style_property (widget_class,
441                                            g_param_spec_int ("child-displacement-y",
442                                                              P_("Child Y Displacement"),
443                                                              P_("How far in the y direction to move the child when the button is depressed"),
444                                                              G_MININT,
445                                                              G_MAXINT,
446                                                              0,
447                                                              GTK_PARAM_READABLE));
448
449   /**
450    * GtkButton:displace-focus:
451    *
452    * Whether the child_displacement_x/child_displacement_y properties should also 
453    * affect the focus rectangle.
454    *
455    * Since: 2.6
456    */
457   gtk_widget_class_install_style_property (widget_class,
458                                            g_param_spec_boolean ("displace-focus",
459                                                                  P_("Displace focus"),
460                                                                  P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
461                                                        FALSE,
462                                                        GTK_PARAM_READABLE));
463
464   gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
465                                                        P_("Show button images"),
466                                                        P_("Whether stock icons should be shown in buttons"),
467                                                        TRUE,
468                                                        GTK_PARAM_READWRITE));
469   
470   g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));  
471 }
472
473 static void
474 gtk_button_init (GtkButton *button)
475 {
476   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
477
478   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
479   GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);
480
481   button->label_text = NULL;
482   
483   button->constructed = FALSE;
484   button->in_button = FALSE;
485   button->button_down = FALSE;
486   button->relief = GTK_RELIEF_NORMAL;
487   button->use_stock = FALSE;
488   button->use_underline = FALSE;
489   button->depressed = FALSE;
490   button->depress_on_activate = TRUE;
491   button->focus_on_click = TRUE;
492
493   priv->xalign = 0.5;
494   priv->yalign = 0.5;
495   priv->align_set = 0;
496   priv->image_is_stock = TRUE;
497 }
498
499 static void
500 gtk_button_destroy (GtkObject *object)
501 {
502   GtkButton *button = GTK_BUTTON (object);
503   
504   if (button->label_text)
505     {
506       g_free (button->label_text);
507       button->label_text = NULL;
508     }
509   
510   (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
511 }
512
513 static GObject*
514 gtk_button_constructor (GType                  type,
515                         guint                  n_construct_properties,
516                         GObjectConstructParam *construct_params)
517 {
518   GObject *object;
519   GtkButton *button;
520
521   object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
522                                                            n_construct_properties,
523                                                            construct_params);
524
525   button = GTK_BUTTON (object);
526   button->constructed = TRUE;
527
528   if (button->label_text != NULL)
529     gtk_button_construct_child (button);
530   
531   return object;
532 }
533
534
535 static GType
536 gtk_button_child_type  (GtkContainer     *container)
537 {
538   if (!GTK_BIN (container)->child)
539     return GTK_TYPE_WIDGET;
540   else
541     return G_TYPE_NONE;
542 }
543
544 static void
545 maybe_set_alignment (GtkButton *button,
546                      GtkWidget *widget)
547 {
548   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
549
550   if (GTK_IS_MISC (widget))
551     {
552       GtkMisc *misc = GTK_MISC (widget);
553       
554       if (priv->align_set)
555         gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
556     }
557   else if (GTK_IS_ALIGNMENT (widget))
558     {
559       GtkAlignment *alignment = GTK_ALIGNMENT (widget);
560
561       if (priv->align_set)
562         gtk_alignment_set (alignment, priv->xalign, priv->yalign, 
563                            alignment->xscale, alignment->yscale);
564     }
565 }
566
567 static void
568 gtk_button_add (GtkContainer *container,
569                 GtkWidget    *widget)
570 {
571   maybe_set_alignment (GTK_BUTTON (container), widget);
572
573   GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
574 }
575
576 static void
577 gtk_button_set_property (GObject         *object,
578                          guint            prop_id,
579                          const GValue    *value,
580                          GParamSpec      *pspec)
581 {
582   GtkButton *button = GTK_BUTTON (object);
583   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
584
585   switch (prop_id)
586     {
587     case PROP_LABEL:
588       gtk_button_set_label (button, g_value_get_string (value));
589       break;
590     case PROP_IMAGE:
591       gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
592       break;
593     case PROP_RELIEF:
594       gtk_button_set_relief (button, g_value_get_enum (value));
595       break;
596     case PROP_USE_UNDERLINE:
597       gtk_button_set_use_underline (button, g_value_get_boolean (value));
598       break;
599     case PROP_USE_STOCK:
600       gtk_button_set_use_stock (button, g_value_get_boolean (value));
601       break;
602     case PROP_FOCUS_ON_CLICK:
603       gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
604       break;
605     case PROP_XALIGN:
606       gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
607       break;
608     case PROP_YALIGN:
609       gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
610       break;
611     default:
612       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
613       break;
614     }
615 }
616
617 static void
618 gtk_button_get_property (GObject         *object,
619                          guint            prop_id,
620                          GValue          *value,
621                          GParamSpec      *pspec)
622 {
623   GtkButton *button = GTK_BUTTON (object);
624   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
625
626   switch (prop_id)
627     {
628     case PROP_LABEL:
629       g_value_set_string (value, button->label_text);
630       break;
631     case PROP_IMAGE:
632       g_value_set_object (value, (GObject *)priv->image);
633       break;
634     case PROP_RELIEF:
635       g_value_set_enum (value, gtk_button_get_relief (button));
636       break;
637     case PROP_USE_UNDERLINE:
638       g_value_set_boolean (value, button->use_underline);
639       break;
640     case PROP_USE_STOCK:
641       g_value_set_boolean (value, button->use_stock);
642       break;
643     case PROP_FOCUS_ON_CLICK:
644       g_value_set_boolean (value, button->focus_on_click);
645       break;
646     case PROP_XALIGN:
647       g_value_set_float (value, priv->xalign);
648       break;
649     case PROP_YALIGN:
650       g_value_set_float (value, priv->yalign);
651       break;
652     default:
653       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
654       break;
655     }
656 }
657
658 GtkWidget*
659 gtk_button_new (void)
660 {
661   return g_object_new (GTK_TYPE_BUTTON, NULL);
662 }
663
664 static gboolean
665 show_image (GtkButton *button)
666 {
667   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (button));  
668   gboolean show;
669
670   g_object_get (settings, "gtk-button-images", &show, NULL);
671
672   return show;
673 }
674
675 static void
676 gtk_button_construct_child (GtkButton *button)
677 {
678   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
679   GtkStockItem item;
680   GtkWidget *label;
681   GtkWidget *hbox;
682   GtkWidget *align;
683   GtkWidget *image = NULL;
684   gchar *label_text = NULL;
685   
686   if (!button->constructed)
687     return;
688   
689   if (button->label_text == NULL)
690     return;
691
692   if (GTK_BIN (button)->child)
693     {
694       if (priv->image && !priv->image_is_stock)
695         {
696           image = g_object_ref (priv->image);
697           if (image->parent)
698             gtk_container_remove (GTK_CONTAINER (image->parent), image);
699         }
700
701       gtk_container_remove (GTK_CONTAINER (button),
702                             GTK_BIN (button)->child);
703   
704       priv->image = NULL;
705     }
706   
707   if (button->use_stock &&
708       gtk_stock_lookup (button->label_text, &item))
709     {
710       if (!image)
711         image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
712
713       label_text = item.label;
714     }
715   else
716     label_text = button->label_text;
717
718   if (image)
719     {
720       label = gtk_label_new_with_mnemonic (label_text);
721       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
722       
723       priv->image = image;
724
725       g_object_set (priv->image, 
726                     "visible", show_image (button),
727                     "no-show-all", TRUE,
728                     NULL);
729       hbox = gtk_hbox_new (FALSE, 2);
730
731       if (priv->align_set)
732         align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
733       else
734         align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
735         
736       gtk_box_pack_start (GTK_BOX (hbox), priv->image, FALSE, FALSE, 0);
737       gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
738       
739       gtk_container_add (GTK_CONTAINER (button), align);
740       gtk_container_add (GTK_CONTAINER (align), hbox);
741       gtk_widget_show_all (align);
742
743       g_object_unref (image);
744
745       return;
746     }
747   
748  if (button->use_underline)
749     {
750       label = gtk_label_new_with_mnemonic (button->label_text);
751       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
752     }
753   else
754     label = gtk_label_new (button->label_text);
755   
756   if (priv->align_set)
757     gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
758
759   gtk_widget_show (label);
760   gtk_container_add (GTK_CONTAINER (button), label);
761 }
762
763
764 GtkWidget*
765 gtk_button_new_with_label (const gchar *label)
766 {
767   return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
768 }
769
770 /**
771  * gtk_button_new_from_stock:
772  * @stock_id: the name of the stock item 
773  *
774  * Creates a new #GtkButton containing the image and text from a stock item.
775  * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
776  * #GTK_STOCK_APPLY.
777  *
778  * If @stock_id is unknown, then it will be treated as a mnemonic
779  * label (as for gtk_button_new_with_mnemonic()).
780  *
781  * Returns: a new #GtkButton
782  **/
783 GtkWidget*
784 gtk_button_new_from_stock (const gchar *stock_id)
785 {
786   return g_object_new (GTK_TYPE_BUTTON,
787                        "label", stock_id,
788                        "use-stock", TRUE,
789                        "use-underline", TRUE,
790                        NULL);
791 }
792
793 /**
794  * gtk_button_new_with_mnemonic:
795  * @label: The text of the button, with an underscore in front of the
796  *         mnemonic character
797  * @returns: a new #GtkButton
798  *
799  * Creates a new #GtkButton containing a label.
800  * If characters in @label are preceded by an underscore, they are underlined.
801  * If you need a literal underscore character in a label, use '__' (two 
802  * underscores). The first underlined character represents a keyboard 
803  * accelerator called a mnemonic.
804  * Pressing Alt and that key activates the button.
805  **/
806 GtkWidget*
807 gtk_button_new_with_mnemonic (const gchar *label)
808 {
809   return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE,  NULL);
810 }
811
812 void
813 gtk_button_pressed (GtkButton *button)
814 {
815   g_return_if_fail (GTK_IS_BUTTON (button));
816
817   
818   g_signal_emit (button, button_signals[PRESSED], 0);
819 }
820
821 void
822 gtk_button_released (GtkButton *button)
823 {
824   g_return_if_fail (GTK_IS_BUTTON (button));
825
826   g_signal_emit (button, button_signals[RELEASED], 0);
827 }
828
829 void
830 gtk_button_clicked (GtkButton *button)
831 {
832   g_return_if_fail (GTK_IS_BUTTON (button));
833
834   g_signal_emit (button, button_signals[CLICKED], 0);
835 }
836
837 void
838 gtk_button_enter (GtkButton *button)
839 {
840   g_return_if_fail (GTK_IS_BUTTON (button));
841
842   g_signal_emit (button, button_signals[ENTER], 0);
843 }
844
845 void
846 gtk_button_leave (GtkButton *button)
847 {
848   g_return_if_fail (GTK_IS_BUTTON (button));
849
850   g_signal_emit (button, button_signals[LEAVE], 0);
851 }
852
853 void
854 gtk_button_set_relief (GtkButton *button,
855                        GtkReliefStyle newrelief)
856 {
857   g_return_if_fail (GTK_IS_BUTTON (button));
858
859   if (newrelief != button->relief) 
860     {
861        button->relief = newrelief;
862        g_object_notify (G_OBJECT (button), "relief");
863        gtk_widget_queue_draw (GTK_WIDGET (button));
864     }
865 }
866
867 GtkReliefStyle
868 gtk_button_get_relief (GtkButton *button)
869 {
870   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
871
872   return button->relief;
873 }
874
875 static void
876 gtk_button_realize (GtkWidget *widget)
877 {
878   GtkButton *button;
879   GdkWindowAttr attributes;
880   gint attributes_mask;
881   gint border_width;
882
883   button = GTK_BUTTON (widget);
884   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
885
886   border_width = GTK_CONTAINER (widget)->border_width;
887
888   attributes.window_type = GDK_WINDOW_CHILD;
889   attributes.x = widget->allocation.x + border_width;
890   attributes.y = widget->allocation.y + border_width;
891   attributes.width = widget->allocation.width - border_width * 2;
892   attributes.height = widget->allocation.height - border_width * 2;
893   attributes.wclass = GDK_INPUT_ONLY;
894   attributes.event_mask = gtk_widget_get_events (widget);
895   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
896                             GDK_BUTTON_RELEASE_MASK |
897                             GDK_ENTER_NOTIFY_MASK |
898                             GDK_LEAVE_NOTIFY_MASK);
899
900   attributes_mask = GDK_WA_X | GDK_WA_Y;
901
902   widget->window = gtk_widget_get_parent_window (widget);
903   g_object_ref (widget->window);
904   
905   button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
906                                          &attributes, attributes_mask);
907   gdk_window_set_user_data (button->event_window, button);
908
909   widget->style = gtk_style_attach (widget->style, widget->window);
910 }
911
912 static void
913 gtk_button_unrealize (GtkWidget *widget)
914 {
915   GtkButton *button = GTK_BUTTON (widget);
916
917   if (button->activate_timeout)
918     gtk_button_finish_activate (button, FALSE);
919
920   if (button->event_window)
921     {
922       gdk_window_set_user_data (button->event_window, NULL);
923       gdk_window_destroy (button->event_window);
924       button->event_window = NULL;
925     }
926   
927   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
928 }
929
930 static void
931 gtk_button_map (GtkWidget *widget)
932 {
933   GtkButton *button = GTK_BUTTON (widget);
934   
935   GTK_WIDGET_CLASS (parent_class)->map (widget);
936
937   if (button->event_window)
938     gdk_window_show (button->event_window);
939 }
940
941 static void
942 gtk_button_unmap (GtkWidget *widget)
943 {
944   GtkButton *button = GTK_BUTTON (widget);
945     
946   if (button->event_window)
947     gdk_window_hide (button->event_window);
948
949   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
950 }
951
952 static void
953 gtk_button_get_props (GtkButton *button,
954                       GtkBorder *default_border,
955                       GtkBorder *default_outside_border,
956                       gboolean  *interior_focus)
957 {
958   GtkWidget *widget =  GTK_WIDGET (button);
959   GtkBorder *tmp_border;
960
961   if (default_border)
962     {
963       gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
964
965       if (tmp_border)
966         {
967           *default_border = *tmp_border;
968           g_free (tmp_border);
969         }
970       else
971         *default_border = default_default_border;
972     }
973
974   if (default_outside_border)
975     {
976       gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
977
978       if (tmp_border)
979         {
980           *default_outside_border = *tmp_border;
981           g_free (tmp_border);
982         }
983       else
984         *default_outside_border = default_default_outside_border;
985     }
986
987   if (interior_focus)
988     gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
989 }
990         
991 static void
992 gtk_button_size_request (GtkWidget      *widget,
993                          GtkRequisition *requisition)
994 {
995   GtkButton *button = GTK_BUTTON (widget);
996   GtkBorder default_border;
997   gint focus_width;
998   gint focus_pad;
999
1000   gtk_button_get_props (button, &default_border, NULL, NULL);
1001   gtk_widget_style_get (GTK_WIDGET (widget),
1002                         "focus-line-width", &focus_width,
1003                         "focus-padding", &focus_pad,
1004                         NULL);
1005  
1006   requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
1007                         GTK_WIDGET (widget)->style->xthickness) * 2;
1008   requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
1009                          GTK_WIDGET (widget)->style->ythickness) * 2;
1010
1011   if (GTK_WIDGET_CAN_DEFAULT (widget))
1012     {
1013       requisition->width += default_border.left + default_border.right;
1014       requisition->height += default_border.top + default_border.bottom;
1015     }
1016
1017   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
1018     {
1019       GtkRequisition child_requisition;
1020
1021       gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
1022
1023       requisition->width += child_requisition.width;
1024       requisition->height += child_requisition.height;
1025     }
1026   
1027   requisition->width += 2 * (focus_width + focus_pad);
1028   requisition->height += 2 * (focus_width + focus_pad);
1029 }
1030
1031 static void
1032 gtk_button_size_allocate (GtkWidget     *widget,
1033                           GtkAllocation *allocation)
1034 {
1035   GtkButton *button = GTK_BUTTON (widget);
1036   GtkAllocation child_allocation;
1037
1038   gint border_width = GTK_CONTAINER (widget)->border_width;
1039   gint xthickness = GTK_WIDGET (widget)->style->xthickness;
1040   gint ythickness = GTK_WIDGET (widget)->style->ythickness;
1041   GtkBorder default_border;
1042   gint focus_width;
1043   gint focus_pad;
1044
1045   gtk_button_get_props (button, &default_border, NULL, NULL);
1046   gtk_widget_style_get (GTK_WIDGET (widget),
1047                         "focus-line-width", &focus_width,
1048                         "focus-padding", &focus_pad,
1049                         NULL);
1050  
1051                             
1052   widget->allocation = *allocation;
1053
1054   if (GTK_WIDGET_REALIZED (widget))
1055     gdk_window_move_resize (button->event_window,
1056                             widget->allocation.x + border_width,
1057                             widget->allocation.y + border_width,
1058                             widget->allocation.width - border_width * 2,
1059                             widget->allocation.height - border_width * 2);
1060
1061   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
1062     {
1063       child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
1064       child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
1065       
1066       child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
1067                                     border_width * 2);
1068       child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
1069                                      border_width * 2);
1070
1071       if (GTK_WIDGET_CAN_DEFAULT (button))
1072         {
1073           child_allocation.x += default_border.left;
1074           child_allocation.y += default_border.top;
1075           child_allocation.width =  MAX (1, child_allocation.width - default_border.left - default_border.right);
1076           child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
1077         }
1078
1079       if (GTK_WIDGET_CAN_FOCUS (button))
1080         {
1081           child_allocation.x += focus_width + focus_pad;
1082           child_allocation.y += focus_width + focus_pad;
1083           child_allocation.width =  MAX (1, child_allocation.width - (focus_width + focus_pad) * 2);
1084           child_allocation.height = MAX (1, child_allocation.height - (focus_width + focus_pad) * 2);
1085         }
1086
1087       if (button->depressed)
1088         {
1089           gint child_displacement_x;
1090           gint child_displacement_y;
1091           
1092           gtk_widget_style_get (widget,
1093                                 "child-displacement-x", &child_displacement_x, 
1094                                 "child-displacement-y", &child_displacement_y,
1095                                 NULL);
1096           child_allocation.x += child_displacement_x;
1097           child_allocation.y += child_displacement_y;
1098         }
1099
1100       gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
1101     }
1102 }
1103
1104 void
1105 _gtk_button_paint (GtkButton    *button,
1106                    GdkRectangle *area,
1107                    GtkStateType  state_type,
1108                    GtkShadowType shadow_type,
1109                    const gchar  *main_detail,
1110                    const gchar  *default_detail)
1111 {
1112   GtkWidget *widget;
1113   gint width, height;
1114   gint x, y;
1115   gint border_width;
1116   GtkBorder default_border;
1117   GtkBorder default_outside_border;
1118   gboolean interior_focus;
1119   gint focus_width;
1120   gint focus_pad;
1121    
1122   if (GTK_WIDGET_DRAWABLE (button))
1123     {
1124       widget = GTK_WIDGET (button);
1125       border_width = GTK_CONTAINER (widget)->border_width;
1126
1127       gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
1128       gtk_widget_style_get (GTK_WIDGET (widget),
1129                             "focus-line-width", &focus_width,
1130                             "focus-padding", &focus_pad,
1131                             NULL); 
1132         
1133       x = widget->allocation.x + border_width;
1134       y = widget->allocation.y + border_width;
1135       width = widget->allocation.width - border_width * 2;
1136       height = widget->allocation.height - border_width * 2;
1137
1138       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
1139           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1140         {
1141           gtk_paint_box (widget->style, widget->window,
1142                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
1143                          area, widget, "buttondefault",
1144                          x, y, width, height);
1145
1146           x += default_border.left;
1147           y += default_border.top;
1148           width -= default_border.left + default_border.right;
1149           height -= default_border.top + default_border.bottom;
1150         }
1151       else if (GTK_WIDGET_CAN_DEFAULT (widget))
1152         {
1153           x += default_outside_border.left;
1154           y += default_outside_border.top;
1155           width -= default_outside_border.left + default_outside_border.right;
1156           height -= default_outside_border.top + default_outside_border.bottom;
1157         }
1158        
1159       if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
1160         {
1161           x += focus_width + focus_pad;
1162           y += focus_width + focus_pad;
1163           width -= 2 * (focus_width + focus_pad);
1164           height -= 2 * (focus_width + focus_pad);
1165         }
1166
1167       if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1168           GTK_WIDGET_STATE(widget) == GTK_STATE_PRELIGHT)
1169         gtk_paint_box (widget->style, widget->window,
1170                        state_type,
1171                        shadow_type, area, widget, "button",
1172                        x, y, width, height);
1173        
1174       if (GTK_WIDGET_HAS_FOCUS (widget))
1175         {
1176           gint child_displacement_x;
1177           gint child_displacement_y;
1178           gboolean displace_focus;
1179           
1180           gtk_widget_style_get (GTK_WIDGET (widget),
1181                                 "child-displacement-y", &child_displacement_y,
1182                                 "child-displacement-x", &child_displacement_x,
1183                                 "displace-focus", &displace_focus,
1184                                 NULL);
1185
1186           if (interior_focus)
1187             {
1188               x += widget->style->xthickness + focus_pad;
1189               y += widget->style->ythickness + focus_pad;
1190               width -= 2 * (widget->style->xthickness + focus_pad);
1191               height -=  2 * (widget->style->ythickness + focus_pad);
1192             }
1193           else
1194             {
1195               x -= focus_width + focus_pad;
1196               y -= focus_width + focus_pad;
1197               width += 2 * (focus_width + focus_pad);
1198               height += 2 * (focus_width + focus_pad);
1199             }
1200
1201           if (button->depressed && displace_focus)
1202             {
1203               x += child_displacement_x;
1204               y += child_displacement_y;
1205             }
1206
1207           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1208                            area, widget, "button",
1209                            x, y, width, height);
1210         }
1211     }
1212 }
1213
1214 static gboolean
1215 gtk_button_expose (GtkWidget      *widget,
1216                    GdkEventExpose *event)
1217 {
1218   if (GTK_WIDGET_DRAWABLE (widget))
1219     {
1220       GtkButton *button = GTK_BUTTON (widget);
1221       
1222       _gtk_button_paint (button, &event->area,
1223                          GTK_WIDGET_STATE (widget),
1224                          button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1225                          "button", "buttondefault");
1226       
1227       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1228     }
1229   
1230   return FALSE;
1231 }
1232
1233 static gboolean
1234 gtk_button_button_press (GtkWidget      *widget,
1235                          GdkEventButton *event)
1236 {
1237   GtkButton *button;
1238
1239   if (event->type == GDK_BUTTON_PRESS)
1240     {
1241       button = GTK_BUTTON (widget);
1242
1243       if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget))
1244         gtk_widget_grab_focus (widget);
1245
1246       if (event->button == 1)
1247         gtk_button_pressed (button);
1248     }
1249
1250   return TRUE;
1251 }
1252
1253 static gboolean
1254 gtk_button_button_release (GtkWidget      *widget,
1255                            GdkEventButton *event)
1256 {
1257   GtkButton *button;
1258
1259   if (event->button == 1)
1260     {
1261       button = GTK_BUTTON (widget);
1262       gtk_button_released (button);
1263     }
1264
1265   return TRUE;
1266 }
1267
1268 static gboolean
1269 gtk_button_grab_broken (GtkWidget   *widget,
1270                         GdkEventAny *event)
1271 {
1272   GtkButton *button = GTK_BUTTON (widget);
1273   gboolean save_in;
1274   
1275   /* Simulate a button release without the pointer in the button */
1276   if (button->button_down)
1277     {
1278       save_in = button->in_button;
1279       button->in_button = FALSE;
1280       gtk_button_released (button);
1281       if (save_in != button->in_button)
1282         {
1283           button->in_button = save_in;
1284           gtk_button_update_state (button);
1285         }
1286     }
1287
1288   return TRUE;
1289 }
1290
1291 static gboolean
1292 gtk_button_key_release (GtkWidget   *widget,
1293                         GdkEventKey *event)
1294 {
1295   GtkButton *button = GTK_BUTTON (widget);
1296
1297   if (button->activate_timeout)
1298     {
1299       gtk_button_finish_activate (button, TRUE);
1300       return TRUE;
1301     }
1302   else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
1303     return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1304   else
1305     return FALSE;
1306 }
1307
1308 static gboolean
1309 gtk_button_enter_notify (GtkWidget        *widget,
1310                          GdkEventCrossing *event)
1311 {
1312   GtkButton *button;
1313   GtkWidget *event_widget;
1314
1315   button = GTK_BUTTON (widget);
1316   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1317
1318   if ((event_widget == widget) &&
1319       (event->detail != GDK_NOTIFY_INFERIOR))
1320     {
1321       button->in_button = TRUE;
1322       gtk_button_enter (button);
1323     }
1324
1325   return FALSE;
1326 }
1327
1328 static gboolean
1329 gtk_button_leave_notify (GtkWidget        *widget,
1330                          GdkEventCrossing *event)
1331 {
1332   GtkButton *button;
1333   GtkWidget *event_widget;
1334
1335   button = GTK_BUTTON (widget);
1336   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1337
1338   if ((event_widget == widget) &&
1339       (event->detail != GDK_NOTIFY_INFERIOR))
1340     {
1341       button->in_button = FALSE;
1342       gtk_button_leave (button);
1343     }
1344
1345   return FALSE;
1346 }
1347
1348 static void
1349 gtk_real_button_pressed (GtkButton *button)
1350 {
1351   if (button->activate_timeout)
1352     return;
1353   
1354   button->button_down = TRUE;
1355   gtk_button_update_state (button);
1356 }
1357
1358 static void
1359 gtk_real_button_released (GtkButton *button)
1360 {
1361   if (button->button_down)
1362     {
1363       button->button_down = FALSE;
1364
1365       if (button->activate_timeout)
1366         return;
1367       
1368       if (button->in_button)
1369         gtk_button_clicked (button);
1370
1371       gtk_button_update_state (button);
1372     }
1373 }
1374
1375 static gboolean
1376 button_activate_timeout (gpointer data)
1377 {
1378   GDK_THREADS_ENTER ();
1379   
1380   gtk_button_finish_activate (data, TRUE);
1381
1382   GDK_THREADS_LEAVE ();
1383
1384   return FALSE;
1385 }
1386
1387 static void
1388 gtk_real_button_activate (GtkButton *button)
1389 {
1390   GtkWidget *widget = GTK_WIDGET (button);
1391   GtkButtonPrivate *priv;
1392   guint32 time;
1393
1394   priv = GTK_BUTTON_GET_PRIVATE (button);
1395
1396   if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1397     {
1398       time = gtk_get_current_event_time ();
1399       if (gdk_keyboard_grab (button->event_window, TRUE, time) == 
1400           GDK_GRAB_SUCCESS)
1401         {
1402           priv->has_grab = TRUE;
1403           priv->grab_time = time;
1404         }
1405
1406       gtk_grab_add (widget);
1407       
1408       button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1409                                                 button_activate_timeout,
1410                                                 button);
1411       button->button_down = TRUE;
1412       gtk_button_update_state (button);
1413       gtk_widget_queue_draw (GTK_WIDGET (button));
1414     }
1415 }
1416
1417 static void
1418 gtk_button_finish_activate (GtkButton *button,
1419                             gboolean   do_it)
1420 {
1421   GtkWidget *widget = GTK_WIDGET (button);
1422   GtkButtonPrivate *priv;
1423   
1424   priv = GTK_BUTTON_GET_PRIVATE (button);
1425
1426   g_source_remove (button->activate_timeout);
1427   button->activate_timeout = 0;
1428
1429   if (priv->has_grab)
1430     {
1431       gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1432                                    priv->grab_time);
1433     }
1434   gtk_grab_remove (widget);
1435
1436   button->button_down = FALSE;
1437
1438   gtk_button_update_state (button);
1439   gtk_widget_queue_draw (GTK_WIDGET (button));
1440
1441   if (do_it)
1442     gtk_button_clicked (button);
1443 }
1444
1445 /**
1446  * gtk_button_set_label:
1447  * @button: a #GtkButton
1448  * @label: a string
1449  *
1450  * Sets the text of the label of the button to @str. This text is
1451  * also used to select the stock item if gtk_button_set_use_stock()
1452  * is used.
1453  *
1454  * This will also clear any previously set labels.
1455  **/
1456 void
1457 gtk_button_set_label (GtkButton   *button,
1458                       const gchar *label)
1459 {
1460   gchar *new_label;
1461   
1462   g_return_if_fail (GTK_IS_BUTTON (button));
1463
1464   new_label = g_strdup (label);
1465   g_free (button->label_text);
1466   button->label_text = new_label;
1467   
1468   gtk_button_construct_child (button);
1469   
1470   g_object_notify (G_OBJECT (button), "label");
1471 }
1472
1473 /**
1474  * gtk_button_get_label:
1475  * @button: a #GtkButton
1476  *
1477  * Fetches the text from the label of the button, as set by
1478  * gtk_button_set_label(). If the label text has not 
1479  * been set the return value will be %NULL. This will be the 
1480  * case if you create an empty button with gtk_button_new() to 
1481  * use as a container.
1482  *
1483  * Return value: The text of the label widget. This string is owned
1484  * by the widget and must not be modified or freed.
1485  **/
1486 G_CONST_RETURN gchar *
1487 gtk_button_get_label (GtkButton *button)
1488 {
1489   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1490   
1491   return button->label_text;
1492 }
1493
1494 /**
1495  * gtk_button_set_use_underline:
1496  * @button: a #GtkButton
1497  * @use_underline: %TRUE if underlines in the text indicate mnemonics
1498  *
1499  * If true, an underline in the text of the button label indicates
1500  * the next character should be used for the mnemonic accelerator key.
1501  */
1502 void
1503 gtk_button_set_use_underline (GtkButton *button,
1504                               gboolean   use_underline)
1505 {
1506   g_return_if_fail (GTK_IS_BUTTON (button));
1507
1508   use_underline = use_underline != FALSE;
1509
1510   if (use_underline != button->use_underline)
1511     {
1512       button->use_underline = use_underline;
1513   
1514       gtk_button_construct_child (button);
1515       
1516       g_object_notify (G_OBJECT (button), "use-underline");
1517     }
1518 }
1519
1520 /**
1521  * gtk_button_get_use_underline:
1522  * @button: a #GtkButton
1523  *
1524  * Returns whether an embedded underline in the button label indicates a
1525  * mnemonic. See gtk_button_set_use_underline ().
1526  *
1527  * Return value: %TRUE if an embedded underline in the button label
1528  *               indicates the mnemonic accelerator keys.
1529  **/
1530 gboolean
1531 gtk_button_get_use_underline (GtkButton *button)
1532 {
1533   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1534   
1535   return button->use_underline;
1536 }
1537
1538 /**
1539  * gtk_button_set_use_stock:
1540  * @button: a #GtkButton
1541  * @use_stock: %TRUE if the button should use a stock item
1542  *
1543  * If true, the label set on the button is used as a
1544  * stock id to select the stock item for the button.
1545  */
1546 void
1547 gtk_button_set_use_stock (GtkButton *button,
1548                           gboolean   use_stock)
1549 {
1550   g_return_if_fail (GTK_IS_BUTTON (button));
1551
1552   use_stock = use_stock != FALSE;
1553
1554   if (use_stock != button->use_stock)
1555     {
1556       button->use_stock = use_stock;
1557   
1558       gtk_button_construct_child (button);
1559       
1560       g_object_notify (G_OBJECT (button), "use-stock");
1561     }
1562 }
1563
1564 /**
1565  * gtk_button_get_use_stock:
1566  * @button: a #GtkButton
1567  *
1568  * Returns whether the button label is a stock item.
1569  *
1570  * Return value: %TRUE if the button label is used to
1571  *               select a stock item instead of being
1572  *               used directly as the label text.
1573  */
1574 gboolean
1575 gtk_button_get_use_stock (GtkButton *button)
1576 {
1577   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1578   
1579   return button->use_stock;
1580 }
1581
1582 /**
1583  * gtk_button_set_focus_on_click:
1584  * @button: a #GtkButton
1585  * @focus_on_click: whether the button grabs focus when clicked with the mouse
1586  * 
1587  * Sets whether the button will grab focus when it is clicked with the mouse.
1588  * Making mouse clicks not grab focus is useful in places like toolbars where
1589  * you don't want the keyboard focus removed from the main area of the
1590  * application.
1591  *
1592  * Since: 2.4
1593  **/
1594 void
1595 gtk_button_set_focus_on_click (GtkButton *button,
1596                                gboolean   focus_on_click)
1597 {
1598   g_return_if_fail (GTK_IS_BUTTON (button));
1599   
1600   focus_on_click = focus_on_click != FALSE;
1601
1602   if (button->focus_on_click != focus_on_click)
1603     {
1604       button->focus_on_click = focus_on_click;
1605       
1606       g_object_notify (G_OBJECT (button), "focus-on-click");
1607     }
1608 }
1609
1610 /**
1611  * gtk_button_get_focus_on_click:
1612  * @button: a #GtkButton
1613  * 
1614  * Returns whether the button grabs focus when it is clicked with the mouse.
1615  * See gtk_button_set_focus_on_click().
1616  *
1617  * Return value: %TRUE if the button grabs focus when it is clicked with
1618  *               the mouse.
1619  *
1620  * Since: 2.4
1621  **/
1622 gboolean
1623 gtk_button_get_focus_on_click (GtkButton *button)
1624 {
1625   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1626   
1627   return button->focus_on_click;
1628 }
1629
1630 /**
1631  * gtk_button_set_alignment:
1632  * @button: a #GtkButton
1633  * @xalign: the horizontal position of the child, 0.0 is left aligned, 
1634  *   1.0 is right aligned
1635  * @yalign: the vertical position of the child, 0.0 is top aligned, 
1636  *   1.0 is bottom aligned
1637  *
1638  * Sets the alignment of the child. This property has no effect unless 
1639  * the child is a #GtkMisc or a #GtkAligment.
1640  *
1641  * Since: 2.4
1642  */
1643 void
1644 gtk_button_set_alignment (GtkButton *button,
1645                           gfloat     xalign,
1646                           gfloat     yalign)
1647 {
1648   GtkButtonPrivate *priv;
1649
1650   g_return_if_fail (GTK_IS_BUTTON (button));
1651   
1652   priv = GTK_BUTTON_GET_PRIVATE (button);
1653
1654   priv->xalign = xalign;
1655   priv->yalign = yalign;
1656   priv->align_set = 1;
1657
1658   maybe_set_alignment (button, GTK_BIN (button)->child);
1659
1660   g_object_freeze_notify (G_OBJECT (button));
1661   g_object_notify (G_OBJECT (button), "xalign");
1662   g_object_notify (G_OBJECT (button), "yalign");
1663   g_object_thaw_notify (G_OBJECT (button));
1664 }
1665
1666 /**
1667  * gtk_button_get_alignment:
1668  * @button: a #GtkButton
1669  * @xalign: return location for horizontal alignment
1670  * @yalign: return location for vertical alignment
1671  *
1672  * Gets the alignment of the child in the button.
1673  *
1674  * Since: 2.4
1675  */
1676 void
1677 gtk_button_get_alignment (GtkButton *button,
1678                           gfloat    *xalign,
1679                           gfloat    *yalign)
1680 {
1681   GtkButtonPrivate *priv;
1682
1683   g_return_if_fail (GTK_IS_BUTTON (button));
1684   
1685   priv = GTK_BUTTON_GET_PRIVATE (button);
1686  
1687   if (xalign) 
1688     *xalign = priv->xalign;
1689
1690   if (yalign)
1691     *yalign = priv->yalign;
1692 }
1693
1694 /**
1695  * _gtk_button_set_depressed:
1696  * @button: a #GtkButton
1697  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1698  * 
1699  * Sets whether the button is currently drawn as down or not. This is 
1700  * purely a visual setting, and is meant only for use by derived widgets
1701  * such as #GtkToggleButton.
1702  **/
1703 void
1704 _gtk_button_set_depressed (GtkButton *button,
1705                            gboolean   depressed)
1706 {
1707   GtkWidget *widget = GTK_WIDGET (button);
1708
1709   depressed = depressed != FALSE;
1710
1711   if (depressed != button->depressed)
1712     {
1713       button->depressed = depressed;
1714       gtk_widget_queue_resize (widget);
1715     }
1716 }
1717
1718 static void
1719 gtk_button_update_state (GtkButton *button)
1720 {
1721   gboolean depressed;
1722   GtkStateType new_state;
1723
1724   if (button->activate_timeout)
1725     depressed = button->depress_on_activate;
1726   else
1727     depressed = button->in_button && button->button_down;
1728
1729   if (button->in_button && (!button->button_down || !depressed))
1730     new_state = GTK_STATE_PRELIGHT;
1731   else
1732     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1733
1734   _gtk_button_set_depressed (button, depressed); 
1735   gtk_widget_set_state (GTK_WIDGET (button), new_state);
1736 }
1737
1738 static void 
1739 show_image_change_notify (GtkButton *button)
1740 {
1741   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1742
1743   if (priv->image) 
1744     {
1745       if (show_image (button))
1746         gtk_widget_show (priv->image);
1747       else
1748         gtk_widget_hide (priv->image);
1749     }
1750 }
1751
1752 static void
1753 traverse_container (GtkWidget *widget,
1754                     gpointer   data)
1755 {
1756   if (GTK_IS_BUTTON (widget))
1757     show_image_change_notify (GTK_BUTTON (widget));
1758   else if (GTK_IS_CONTAINER (widget))
1759     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1760 }
1761
1762 static void
1763 gtk_button_setting_changed (GtkSettings *settings)
1764 {
1765   GList *list, *l;
1766
1767   list = gtk_window_list_toplevels ();
1768
1769   for (l = list; l; l = l->next)
1770     gtk_container_forall (GTK_CONTAINER (l->data), 
1771                           traverse_container, NULL);
1772
1773   g_list_free (list);
1774 }
1775
1776
1777 static void
1778 gtk_button_screen_changed (GtkWidget *widget,
1779                            GdkScreen *previous_screen)
1780 {
1781   GtkSettings *settings;
1782   guint show_image_connection;
1783
1784   if (!gtk_widget_has_screen (widget))
1785     return;
1786
1787   settings = gtk_widget_get_settings (widget);
1788
1789   show_image_connection = 
1790     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
1791                                          "gtk-button-connection"));
1792   
1793   if (show_image_connection)
1794     return;
1795
1796   show_image_connection =
1797     g_signal_connect (settings, "notify::gtk-button-images",
1798                       G_CALLBACK (gtk_button_setting_changed), 0);
1799   g_object_set_data (G_OBJECT (settings), 
1800                      "gtk-button-connection",
1801                      GUINT_TO_POINTER (show_image_connection));
1802
1803   show_image_change_notify (GTK_BUTTON (widget));
1804 }
1805
1806 static void
1807 gtk_button_state_changed (GtkWidget    *widget,
1808                           GtkStateType  previous_state)
1809 {
1810   GtkButton *button = GTK_BUTTON (widget);
1811
1812   if (!GTK_WIDGET_IS_SENSITIVE (widget))
1813     {
1814       button->in_button = FALSE;
1815       gtk_real_button_released (button);
1816     }
1817 }
1818
1819 static void
1820 gtk_button_grab_notify (GtkWidget *widget,
1821                         gboolean   was_grabbed)
1822 {
1823   GtkButton *button = GTK_BUTTON (widget);
1824
1825   if (!was_grabbed)
1826     {
1827       button->in_button = FALSE;
1828       gtk_real_button_released (button);
1829     }
1830 }
1831
1832 /**
1833  * gtk_button_set_image:
1834  * @button: a #GtkButton
1835  * @image: a widget to set as the image for the button
1836  *
1837  * Set the image of @button to the given widget. Note that
1838  * it depends on the gtk-button-images setting whether the
1839  * image will be displayed or not, you don't have to call
1840  * gtk_widget_show() on @image yourself.
1841  *
1842  * Since: 2.6
1843  */ 
1844 void
1845 gtk_button_set_image (GtkButton *button,
1846                       GtkWidget *image)
1847 {
1848   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1849
1850   priv->image = image;
1851   priv->image_is_stock = (image == NULL);
1852
1853   gtk_button_construct_child (button);
1854
1855   g_object_notify (G_OBJECT (button), "image");
1856 }
1857
1858 /**
1859  * gtk_button_get_image:
1860  * @button: a #GtkButton
1861  *
1862  * Gets the widget that is currenty set as the image of @button.
1863  * This may have been explicitly set by gtk_button_set_image()
1864  * or constructed by gtk_button_new_from_stock().
1865  *
1866  * Return value: a #GtkWidget or %NULL in case there is no image
1867  *
1868  * Since: 2.6
1869  */
1870 GtkWidget *
1871 gtk_button_get_image (GtkButton *button)
1872 {
1873   GtkButtonPrivate *priv;
1874
1875   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1876
1877   priv = GTK_BUTTON_GET_PRIVATE (button);
1878   
1879   return priv->image;
1880 }
1881   
1882 #define __GTK_BUTTON_C__
1883 #include "gtkaliasdef.c"