]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
Use GtkBin accessors
[~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 /**
28  * SECTION:gtkbutton
29  * @Short_description: A widget that creates a signal when clicked on
30  * @Title: GtkButton
31  *
32  * The #GtkButton widget is generally used to attach a function to that is
33  * called when the button is pressed.  The various signals and how to use them
34  * are outlined below.
35  *
36  * The #GtkButton widget can hold any valid child widget.  That is it can hold
37  * most any other standard #GtkWidget.  The most commonly used child is the
38  * #GtkLabel.
39  */
40
41 #include "config.h"
42 #include <string.h>
43 #include "gtkalignment.h"
44 #include "gtkbutton.h"
45 #include "gtklabel.h"
46 #include "gtkmain.h"
47 #include "gtkmarshalers.h"
48 #include "gtkimage.h"
49 #include "gtkhbox.h"
50 #include "gtkvbox.h"
51 #include "gtkstock.h"
52 #include "gtkiconfactory.h"
53 #include "gtkactivatable.h"
54 #include "gtksizerequest.h"
55 #include "gtkprivate.h"
56 #include "gtkintl.h"
57
58
59 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
60 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
61 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
62
63 /* Time out before giving up on getting a key release when animating
64  * the close button.
65  */
66 #define ACTIVATE_TIMEOUT 250
67
68 enum {
69   PRESSED,
70   RELEASED,
71   CLICKED,
72   ENTER,
73   LEAVE,
74   ACTIVATE,
75   LAST_SIGNAL
76 };
77
78 enum {
79   PROP_0,
80   PROP_LABEL,
81   PROP_IMAGE,
82   PROP_RELIEF,
83   PROP_USE_UNDERLINE,
84   PROP_USE_STOCK,
85   PROP_FOCUS_ON_CLICK,
86   PROP_XALIGN,
87   PROP_YALIGN,
88   PROP_IMAGE_POSITION,
89
90   /* activatable properties */
91   PROP_ACTIVATABLE_RELATED_ACTION,
92   PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
93 };
94
95 #define GTK_BUTTON_GET_PRIVATE(o)       (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_BUTTON, GtkButtonPrivate))
96 typedef struct _GtkButtonPrivate GtkButtonPrivate;
97
98 struct _GtkButtonPrivate
99 {
100   gfloat          xalign;
101   gfloat          yalign;
102   GtkWidget      *image;
103   guint           align_set             : 1;
104   guint           image_is_stock        : 1;
105   guint           use_action_appearance : 1;
106   guint32         grab_time;
107   GdkDevice      *grab_keyboard;
108   GtkPositionType image_position;
109   GtkAction      *action;
110 };
111
112 static void gtk_button_destroy        (GtkObject          *object);
113 static void gtk_button_dispose        (GObject            *object);
114 static void gtk_button_set_property   (GObject            *object,
115                                        guint               prop_id,
116                                        const GValue       *value,
117                                        GParamSpec         *pspec);
118 static void gtk_button_get_property   (GObject            *object,
119                                        guint               prop_id,
120                                        GValue             *value,
121                                        GParamSpec         *pspec);
122 static void gtk_button_screen_changed (GtkWidget          *widget,
123                                        GdkScreen          *previous_screen);
124 static void gtk_button_realize (GtkWidget * widget);
125 static void gtk_button_unrealize (GtkWidget * widget);
126 static void gtk_button_map (GtkWidget * widget);
127 static void gtk_button_unmap (GtkWidget * widget);
128 static void gtk_button_style_set (GtkWidget * widget, GtkStyle * prev_style);
129 static void gtk_button_size_allocate (GtkWidget * widget,
130                                       GtkAllocation * allocation);
131 static gint gtk_button_expose (GtkWidget * widget, GdkEventExpose * event);
132 static gint gtk_button_button_press (GtkWidget * widget,
133                                      GdkEventButton * event);
134 static gint gtk_button_button_release (GtkWidget * widget,
135                                        GdkEventButton * event);
136 static gint gtk_button_grab_broken (GtkWidget * widget,
137                                     GdkEventGrabBroken * event);
138 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
139 static gint gtk_button_enter_notify (GtkWidget * widget,
140                                      GdkEventCrossing * event);
141 static gint gtk_button_leave_notify (GtkWidget * widget,
142                                      GdkEventCrossing * event);
143 static void gtk_real_button_pressed (GtkButton * button);
144 static void gtk_real_button_released (GtkButton * button);
145 static void gtk_real_button_clicked (GtkButton * button);
146 static void gtk_real_button_activate  (GtkButton          *button);
147 static void gtk_button_update_state   (GtkButton          *button);
148 static void gtk_button_add            (GtkContainer       *container,
149                                        GtkWidget          *widget);
150 static GType gtk_button_child_type    (GtkContainer       *container);
151 static void gtk_button_finish_activate (GtkButton         *button,
152                                         gboolean           do_it);
153
154 static GObject* gtk_button_constructor (GType                  type,
155                                         guint                  n_construct_properties,
156                                         GObjectConstructParam *construct_params);
157 static void gtk_button_construct_child (GtkButton             *button);
158 static void gtk_button_state_changed   (GtkWidget             *widget,
159                                         GtkStateType           previous_state);
160 static void gtk_button_grab_notify     (GtkWidget             *widget,
161                                         gboolean               was_grabbed);
162
163
164 static void gtk_button_activatable_interface_init(GtkActivatableIface  *iface);
165 static void gtk_button_update                    (GtkActivatable       *activatable,
166                                                   GtkAction            *action,
167                                                   const gchar          *property_name);
168 static void gtk_button_sync_action_properties    (GtkActivatable       *activatable,
169                                                   GtkAction            *action);
170 static void gtk_button_set_related_action        (GtkButton            *button,
171                                                   GtkAction            *action);
172 static void gtk_button_set_use_action_appearance (GtkButton            *button,
173                                                   gboolean              use_appearance);
174
175 static void gtk_button_size_request_init         (GtkSizeRequestIface *iface);
176 static void gtk_button_get_width                 (GtkSizeRequest      *widget,
177                                                   gint                *minimum_size,
178                                                   gint                *natural_size);
179 static void gtk_button_get_height                (GtkSizeRequest      *widget,
180                                                   gint                *minimum_size,
181                                                   gint                *natural_size);
182   
183 static guint button_signals[LAST_SIGNAL] = { 0 };
184
185 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
186                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
187                                                 gtk_button_activatable_interface_init)
188                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
189                                                 gtk_button_size_request_init))
190
191 static void
192 gtk_button_class_init (GtkButtonClass *klass)
193 {
194   GObjectClass *gobject_class;
195   GtkObjectClass *object_class;
196   GtkWidgetClass *widget_class;
197   GtkContainerClass *container_class;
198
199   gobject_class = G_OBJECT_CLASS (klass);
200   object_class = (GtkObjectClass*) klass;
201   widget_class = (GtkWidgetClass*) klass;
202   container_class = (GtkContainerClass*) klass;
203   
204   gobject_class->constructor  = gtk_button_constructor;
205   gobject_class->dispose      = gtk_button_dispose;
206   gobject_class->set_property = gtk_button_set_property;
207   gobject_class->get_property = gtk_button_get_property;
208
209   object_class->destroy = gtk_button_destroy;
210
211   widget_class->screen_changed = gtk_button_screen_changed;
212   widget_class->realize = gtk_button_realize;
213   widget_class->unrealize = gtk_button_unrealize;
214   widget_class->map = gtk_button_map;
215   widget_class->unmap = gtk_button_unmap;
216   widget_class->style_set = gtk_button_style_set;
217   widget_class->size_allocate = gtk_button_size_allocate;
218   widget_class->expose_event = gtk_button_expose;
219   widget_class->button_press_event = gtk_button_button_press;
220   widget_class->button_release_event = gtk_button_button_release;
221   widget_class->grab_broken_event = gtk_button_grab_broken;
222   widget_class->key_release_event = gtk_button_key_release;
223   widget_class->enter_notify_event = gtk_button_enter_notify;
224   widget_class->leave_notify_event = gtk_button_leave_notify;
225   widget_class->state_changed = gtk_button_state_changed;
226   widget_class->grab_notify = gtk_button_grab_notify;
227
228   container_class->child_type = gtk_button_child_type;
229   container_class->add = gtk_button_add;
230
231   klass->pressed = gtk_real_button_pressed;
232   klass->released = gtk_real_button_released;
233   klass->clicked = NULL;
234   klass->enter = gtk_button_update_state;
235   klass->leave = gtk_button_update_state;
236   klass->activate = gtk_real_button_activate;
237
238   g_object_class_install_property (gobject_class,
239                                    PROP_LABEL,
240                                    g_param_spec_string ("label",
241                                                         P_("Label"),
242                                                         P_("Text of the label widget inside the button, if the button contains a label widget"),
243                                                         NULL,
244                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
245   
246   g_object_class_install_property (gobject_class,
247                                    PROP_USE_UNDERLINE,
248                                    g_param_spec_boolean ("use-underline",
249                                                          P_("Use underline"),
250                                                          P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
251                                                         FALSE,
252                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
253   
254   g_object_class_install_property (gobject_class,
255                                    PROP_USE_STOCK,
256                                    g_param_spec_boolean ("use-stock",
257                                                          P_("Use stock"),
258                                                          P_("If set, the label is used to pick a stock item instead of being displayed"),
259                                                         FALSE,
260                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
261   
262   g_object_class_install_property (gobject_class,
263                                    PROP_FOCUS_ON_CLICK,
264                                    g_param_spec_boolean ("focus-on-click",
265                                                          P_("Focus on click"),
266                                                          P_("Whether the button grabs focus when it is clicked with the mouse"),
267                                                          TRUE,
268                                                          GTK_PARAM_READWRITE));
269   
270   g_object_class_install_property (gobject_class,
271                                    PROP_RELIEF,
272                                    g_param_spec_enum ("relief",
273                                                       P_("Border relief"),
274                                                       P_("The border relief style"),
275                                                       GTK_TYPE_RELIEF_STYLE,
276                                                       GTK_RELIEF_NORMAL,
277                                                       GTK_PARAM_READWRITE));
278   
279   /**
280    * GtkButton:xalign:
281    *
282    * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
283    * can be used to control it's horizontal alignment. 0.0 is left aligned, 
284    * 1.0 is right aligned.
285    *
286    * Since: 2.4
287    */
288   g_object_class_install_property (gobject_class,
289                                    PROP_XALIGN,
290                                    g_param_spec_float("xalign",
291                                                       P_("Horizontal alignment for child"),
292                                                       P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
293                                                       0.0,
294                                                       1.0,
295                                                       0.5,
296                                                       GTK_PARAM_READWRITE));
297
298   /**
299    * GtkButton:yalign:
300    *
301    * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
302    * can be used to control it's vertical alignment. 0.0 is top aligned, 
303    * 1.0 is bottom aligned.
304    *
305    * Since: 2.4
306    */
307   g_object_class_install_property (gobject_class,
308                                    PROP_YALIGN,
309                                    g_param_spec_float("yalign",
310                                                       P_("Vertical alignment for child"),
311                                                       P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
312                                                       0.0,
313                                                       1.0,
314                                                       0.5,
315                                                       GTK_PARAM_READWRITE));
316
317   /**
318    * GtkButton::image:
319    *
320    * The child widget to appear next to the button text.
321    *
322    * Since: 2.6
323    */
324   g_object_class_install_property (gobject_class,
325                                    PROP_IMAGE,
326                                    g_param_spec_object ("image",
327                                                         P_("Image widget"),
328                                                         P_("Child widget to appear next to the button text"),
329                                                         GTK_TYPE_WIDGET,
330                                                         GTK_PARAM_READWRITE));
331
332   /**
333    * GtkButton:image-position:
334    *
335    * The position of the image relative to the text inside the button.
336    *
337    * Since: 2.10
338    */
339   g_object_class_install_property (gobject_class,
340                                    PROP_IMAGE_POSITION,
341                                    g_param_spec_enum ("image-position",
342                                             P_("Image position"),
343                                                       P_("The position of the image relative to the text"),
344                                                       GTK_TYPE_POSITION_TYPE,
345                                                       GTK_POS_LEFT,
346                                                       GTK_PARAM_READWRITE));
347
348   g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
349   g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
350
351   /**
352    * GtkButton::pressed:
353    * @button: the object that received the signal
354    *
355    * Emitted when the button is pressed.
356    *
357    * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
358    */ 
359   button_signals[PRESSED] =
360     g_signal_new (I_("pressed"),
361                   G_OBJECT_CLASS_TYPE (object_class),
362                   G_SIGNAL_RUN_FIRST,
363                   G_STRUCT_OFFSET (GtkButtonClass, pressed),
364                   NULL, NULL,
365                   _gtk_marshal_VOID__VOID,
366                   G_TYPE_NONE, 0);
367
368   /**
369    * GtkButton::released:
370    * @button: the object that received the signal
371    *
372    * Emitted when the button is released.
373    *
374    * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
375    */ 
376   button_signals[RELEASED] =
377     g_signal_new (I_("released"),
378                   G_OBJECT_CLASS_TYPE (object_class),
379                   G_SIGNAL_RUN_FIRST,
380                   G_STRUCT_OFFSET (GtkButtonClass, released),
381                   NULL, NULL,
382                   _gtk_marshal_VOID__VOID,
383                   G_TYPE_NONE, 0);
384
385   /**
386    * GtkButton::clicked:
387    * @button: the object that received the signal
388    *
389    * Emitted when the button has been activated (pressed and released).
390    */ 
391   button_signals[CLICKED] =
392     g_signal_new (I_("clicked"),
393                   G_OBJECT_CLASS_TYPE (object_class),
394                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
395                   G_STRUCT_OFFSET (GtkButtonClass, clicked),
396                   NULL, NULL,
397                   _gtk_marshal_VOID__VOID,
398                   G_TYPE_NONE, 0);
399
400   /**
401    * GtkButton::enter:
402    * @button: the object that received the signal
403    *
404    * Emitted when the pointer enters the button.
405    *
406    * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
407    */ 
408   button_signals[ENTER] =
409     g_signal_new (I_("enter"),
410                   G_OBJECT_CLASS_TYPE (object_class),
411                   G_SIGNAL_RUN_FIRST,
412                   G_STRUCT_OFFSET (GtkButtonClass, enter),
413                   NULL, NULL,
414                   _gtk_marshal_VOID__VOID,
415                   G_TYPE_NONE, 0);
416
417   /**
418    * GtkButton::leave:
419    * @button: the object that received the signal
420    *
421    * Emitted when the pointer leaves the button.
422    *
423    * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
424    */ 
425   button_signals[LEAVE] =
426     g_signal_new (I_("leave"),
427                   G_OBJECT_CLASS_TYPE (object_class),
428                   G_SIGNAL_RUN_FIRST,
429                   G_STRUCT_OFFSET (GtkButtonClass, leave),
430                   NULL, NULL,
431                   _gtk_marshal_VOID__VOID,
432                   G_TYPE_NONE, 0);
433
434   /**
435    * GtkButton::activate:
436    * @widget: the object which received the signal.
437    *
438    * The ::activate signal on GtkButton is an action signal and
439    * emitting it causes the button to animate press then release. 
440    * Applications should never connect to this signal, but use the
441    * #GtkButton::clicked signal.
442    */
443   button_signals[ACTIVATE] =
444     g_signal_new (I_("activate"),
445                   G_OBJECT_CLASS_TYPE (object_class),
446                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
447                   G_STRUCT_OFFSET (GtkButtonClass, activate),
448                   NULL, NULL,
449                   _gtk_marshal_VOID__VOID,
450                   G_TYPE_NONE, 0);
451   widget_class->activate_signal = button_signals[ACTIVATE];
452
453   /**
454    * GtkButton:default-border:
455    *
456    * The "default-border" style property defines the extra space to add
457    * around a button that can become the default widget of its window.
458    * For more information about default widgets, see gtk_widget_grab_default().
459    */
460
461   gtk_widget_class_install_style_property (widget_class,
462                                            g_param_spec_boxed ("default-border",
463                                                                P_("Default Spacing"),
464                                                                P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
465                                                                GTK_TYPE_BORDER,
466                                                                GTK_PARAM_READABLE));
467
468   /**
469    * GtkButton:default-outside-border:
470    *
471    * The "default-outside-border" style property defines the extra outside
472    * space to add around a button that can become the default widget of its
473    * window. Extra outside space is always drawn outside the button border.
474    * For more information about default widgets, see gtk_widget_grab_default().
475    */
476   gtk_widget_class_install_style_property (widget_class,
477                                            g_param_spec_boxed ("default-outside-border",
478                                                                P_("Default Outside Spacing"),
479                                                                P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
480                                                                GTK_TYPE_BORDER,
481                                                                GTK_PARAM_READABLE));
482   gtk_widget_class_install_style_property (widget_class,
483                                            g_param_spec_int ("child-displacement-x",
484                                                              P_("Child X Displacement"),
485                                                              P_("How far in the x direction to move the child when the button is depressed"),
486                                                              G_MININT,
487                                                              G_MAXINT,
488                                                              0,
489                                                              GTK_PARAM_READABLE));
490   gtk_widget_class_install_style_property (widget_class,
491                                            g_param_spec_int ("child-displacement-y",
492                                                              P_("Child Y Displacement"),
493                                                              P_("How far in the y direction to move the child when the button is depressed"),
494                                                              G_MININT,
495                                                              G_MAXINT,
496                                                              0,
497                                                              GTK_PARAM_READABLE));
498
499   /**
500    * GtkButton:displace-focus:
501    *
502    * Whether the child_displacement_x/child_displacement_y properties 
503    * should also affect the focus rectangle.
504    *
505    * Since: 2.6
506    */
507   gtk_widget_class_install_style_property (widget_class,
508                                            g_param_spec_boolean ("displace-focus",
509                                                                  P_("Displace focus"),
510                                                                  P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
511                                                                  FALSE,
512                                                                  GTK_PARAM_READABLE));
513
514   /**
515    * GtkButton:inner-border:
516    *
517    * Sets the border between the button edges and child.
518    *
519    * Since: 2.10
520    */
521   gtk_widget_class_install_style_property (widget_class,
522                                            g_param_spec_boxed ("inner-border",
523                                                                P_("Inner Border"),
524                                                                P_("Border between button edges and child."),
525                                                                GTK_TYPE_BORDER,
526                                                                GTK_PARAM_READABLE));
527
528   /**
529    * GtkButton::image-spacing:
530    *
531    * Spacing in pixels between the image and label.
532    *
533    * Since: 2.10
534    */
535   gtk_widget_class_install_style_property (widget_class,
536                                            g_param_spec_int ("image-spacing",
537                                                              P_("Image spacing"),
538                                                              P_("Spacing in pixels between the image and label"),
539                                                              0,
540                                                              G_MAXINT,
541                                                              2,
542                                                              GTK_PARAM_READABLE));
543
544   /**
545    * GtkSettings::gtk-button-images:
546    *
547    * Whether images should be shown on buttons
548    *
549    * Since: 2.4
550    */
551   gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
552                                                        P_("Show button images"),
553                                                        P_("Whether images should be shown on buttons"),
554                                                        TRUE,
555                                                        GTK_PARAM_READWRITE));
556
557   g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
558 }
559
560 static void
561 gtk_button_init (GtkButton *button)
562 {
563   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
564
565   gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
566   gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
567   gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
568
569   button->label_text = NULL;
570   
571   button->constructed = FALSE;
572   button->in_button = FALSE;
573   button->button_down = FALSE;
574   button->relief = GTK_RELIEF_NORMAL;
575   button->use_stock = FALSE;
576   button->use_underline = FALSE;
577   button->depressed = FALSE;
578   button->depress_on_activate = TRUE;
579   button->focus_on_click = TRUE;
580
581   priv->xalign = 0.5;
582   priv->yalign = 0.5;
583   priv->align_set = 0;
584   priv->image_is_stock = TRUE;
585   priv->image_position = GTK_POS_LEFT;
586   priv->use_action_appearance = TRUE;
587 }
588
589 static void
590 gtk_button_destroy (GtkObject *object)
591 {
592   GtkButton *button = GTK_BUTTON (object);
593   
594   if (button->label_text)
595     {
596       g_free (button->label_text);
597       button->label_text = NULL;
598     }
599
600   GTK_OBJECT_CLASS (gtk_button_parent_class)->destroy (object);
601 }
602
603 static GObject*
604 gtk_button_constructor (GType                  type,
605                         guint                  n_construct_properties,
606                         GObjectConstructParam *construct_params)
607 {
608   GObject *object;
609   GtkButton *button;
610
611   object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
612                                                                   n_construct_properties,
613                                                                   construct_params);
614
615   button = GTK_BUTTON (object);
616   button->constructed = TRUE;
617
618   if (button->label_text != NULL)
619     gtk_button_construct_child (button);
620   
621   return object;
622 }
623
624
625 static GType
626 gtk_button_child_type  (GtkContainer     *container)
627 {
628   if (!gtk_bin_get_child (GTK_BIN (container)))
629     return GTK_TYPE_WIDGET;
630   else
631     return G_TYPE_NONE;
632 }
633
634 static void
635 maybe_set_alignment (GtkButton *button,
636                      GtkWidget *widget)
637 {
638   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
639
640   if (GTK_IS_MISC (widget))
641     {
642       GtkMisc *misc = GTK_MISC (widget);
643       
644       if (priv->align_set)
645         gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
646     }
647   else if (GTK_IS_ALIGNMENT (widget))
648     {
649       GtkAlignment *alignment = GTK_ALIGNMENT (widget);
650
651       if (priv->align_set)
652         gtk_alignment_set (alignment, priv->xalign, priv->yalign, 
653                            alignment->xscale, alignment->yscale);
654     }
655 }
656
657 static void
658 gtk_button_add (GtkContainer *container,
659                 GtkWidget    *widget)
660 {
661   maybe_set_alignment (GTK_BUTTON (container), widget);
662
663   GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
664 }
665
666 static void 
667 gtk_button_dispose (GObject *object)
668 {
669   GtkButton *button = GTK_BUTTON (object);
670   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
671
672   if (priv->action)
673     {
674       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
675       priv->action = NULL;
676     }
677   G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
678 }
679
680 static void
681 gtk_button_set_property (GObject         *object,
682                          guint            prop_id,
683                          const GValue    *value,
684                          GParamSpec      *pspec)
685 {
686   GtkButton *button = GTK_BUTTON (object);
687   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
688
689   switch (prop_id)
690     {
691     case PROP_LABEL:
692       gtk_button_set_label (button, g_value_get_string (value));
693       break;
694     case PROP_IMAGE:
695       gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
696       break;
697     case PROP_RELIEF:
698       gtk_button_set_relief (button, g_value_get_enum (value));
699       break;
700     case PROP_USE_UNDERLINE:
701       gtk_button_set_use_underline (button, g_value_get_boolean (value));
702       break;
703     case PROP_USE_STOCK:
704       gtk_button_set_use_stock (button, g_value_get_boolean (value));
705       break;
706     case PROP_FOCUS_ON_CLICK:
707       gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
708       break;
709     case PROP_XALIGN:
710       gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
711       break;
712     case PROP_YALIGN:
713       gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
714       break;
715     case PROP_IMAGE_POSITION:
716       gtk_button_set_image_position (button, g_value_get_enum (value));
717       break;
718     case PROP_ACTIVATABLE_RELATED_ACTION:
719       gtk_button_set_related_action (button, g_value_get_object (value));
720       break;
721     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
722       gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
723       break;
724     default:
725       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
726       break;
727     }
728 }
729
730 static void
731 gtk_button_get_property (GObject         *object,
732                          guint            prop_id,
733                          GValue          *value,
734                          GParamSpec      *pspec)
735 {
736   GtkButton *button = GTK_BUTTON (object);
737   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
738
739   switch (prop_id)
740     {
741     case PROP_LABEL:
742       g_value_set_string (value, button->label_text);
743       break;
744     case PROP_IMAGE:
745       g_value_set_object (value, (GObject *)priv->image);
746       break;
747     case PROP_RELIEF:
748       g_value_set_enum (value, gtk_button_get_relief (button));
749       break;
750     case PROP_USE_UNDERLINE:
751       g_value_set_boolean (value, button->use_underline);
752       break;
753     case PROP_USE_STOCK:
754       g_value_set_boolean (value, button->use_stock);
755       break;
756     case PROP_FOCUS_ON_CLICK:
757       g_value_set_boolean (value, button->focus_on_click);
758       break;
759     case PROP_XALIGN:
760       g_value_set_float (value, priv->xalign);
761       break;
762     case PROP_YALIGN:
763       g_value_set_float (value, priv->yalign);
764       break;
765     case PROP_IMAGE_POSITION:
766       g_value_set_enum (value, priv->image_position);
767       break;
768     case PROP_ACTIVATABLE_RELATED_ACTION:
769       g_value_set_object (value, priv->action);
770       break;
771     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
772       g_value_set_boolean (value, priv->use_action_appearance);
773       break;
774     default:
775       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
776       break;
777     }
778 }
779
780 static void 
781 gtk_button_activatable_interface_init (GtkActivatableIface  *iface)
782 {
783   iface->update = gtk_button_update;
784   iface->sync_action_properties = gtk_button_sync_action_properties;
785 }
786
787 static void
788 activatable_update_stock_id (GtkButton *button,
789                              GtkAction *action)
790 {
791   if (!gtk_button_get_use_stock (button))
792     return;
793
794   gtk_button_set_label (button, gtk_action_get_stock_id (action));
795 }
796
797 static void
798 activatable_update_short_label (GtkButton *button,
799                                 GtkAction *action)
800 {
801   GtkWidget *child;
802   GtkWidget *image;
803
804   if (gtk_button_get_use_stock (button))
805     return;
806
807   image = gtk_button_get_image (button);
808
809   /* Dont touch custom child... */
810   child = gtk_bin_get_child (GTK_BIN (button));
811   if (GTK_IS_IMAGE (image) ||
812       child == NULL ||
813       GTK_IS_LABEL (child))
814     {
815       gtk_button_set_label (button, gtk_action_get_short_label (action));
816       gtk_button_set_use_underline (button, TRUE);
817     }
818 }
819
820 static void
821 activatable_update_icon_name (GtkButton *button,
822                               GtkAction *action)
823 {
824   GtkWidget *image;
825               
826   if (gtk_button_get_use_stock (button))
827     return;
828
829   image = gtk_button_get_image (button);
830
831   if (GTK_IS_IMAGE (image) &&
832       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
833        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
834     gtk_image_set_from_icon_name (GTK_IMAGE (image),
835                                   gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
836 }
837
838 static void
839 activatable_update_gicon (GtkButton *button,
840                           GtkAction *action)
841 {
842   GtkWidget *image = gtk_button_get_image (button);
843   GIcon *icon = gtk_action_get_gicon (action);
844   
845   if (GTK_IS_IMAGE (image) &&
846       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
847        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
848     gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
849 }
850
851 static void 
852 gtk_button_update (GtkActivatable *activatable,
853                    GtkAction      *action,
854                    const gchar    *property_name)
855 {
856   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
857
858   if (strcmp (property_name, "visible") == 0)
859     {
860       if (gtk_action_is_visible (action))
861         gtk_widget_show (GTK_WIDGET (activatable));
862       else
863         gtk_widget_hide (GTK_WIDGET (activatable));
864     }
865   else if (strcmp (property_name, "sensitive") == 0)
866     gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
867
868   if (!priv->use_action_appearance)
869     return;
870
871   if (strcmp (property_name, "stock-id") == 0)
872     activatable_update_stock_id (GTK_BUTTON (activatable), action);
873   else if (strcmp (property_name, "gicon") == 0)
874     activatable_update_gicon (GTK_BUTTON (activatable), action);
875   else if (strcmp (property_name, "short-label") == 0)
876     activatable_update_short_label (GTK_BUTTON (activatable), action);
877   else if (strcmp (property_name, "icon-name") == 0)
878     activatable_update_icon_name (GTK_BUTTON (activatable), action);
879 }
880
881 static void
882 gtk_button_sync_action_properties (GtkActivatable *activatable,
883                                    GtkAction      *action)
884 {
885   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
886
887   if (!action)
888     return;
889
890   if (gtk_action_is_visible (action))
891     gtk_widget_show (GTK_WIDGET (activatable));
892   else
893     gtk_widget_hide (GTK_WIDGET (activatable));
894   
895   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
896   
897   if (priv->use_action_appearance)
898     {
899       activatable_update_stock_id (GTK_BUTTON (activatable), action);
900       activatable_update_short_label (GTK_BUTTON (activatable), action);
901       activatable_update_gicon (GTK_BUTTON (activatable), action);
902       activatable_update_icon_name (GTK_BUTTON (activatable), action);
903     }
904 }
905
906 static void
907 gtk_button_set_related_action (GtkButton *button,
908                                GtkAction *action)
909 {
910   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
911
912   if (priv->action == action)
913     return;
914
915   /* This should be a default handler, but for compatibility reasons
916    * we need to support derived classes that don't chain up their
917    * clicked handler.
918    */
919   g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
920   if (action)
921     g_signal_connect_after (button, "clicked",
922                             G_CALLBACK (gtk_real_button_clicked), NULL);
923
924   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
925
926   priv->action = action;
927 }
928
929 static void
930 gtk_button_set_use_action_appearance (GtkButton *button,
931                                       gboolean   use_appearance)
932 {
933   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
934
935   if (priv->use_action_appearance != use_appearance)
936     {
937       priv->use_action_appearance = use_appearance;
938
939       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
940     }
941 }
942
943 /**
944  * gtk_button_new:
945  *
946  * Creates a new #GtkButton widget. To add a child widget to the button,
947  * use gtk_container_add().
948  *
949  * Returns: The newly created #GtkButton widget.
950  */
951 GtkWidget*
952 gtk_button_new (void)
953 {
954   return g_object_new (GTK_TYPE_BUTTON, NULL);
955 }
956
957 static gboolean
958 show_image (GtkButton *button)
959 {
960   gboolean show;
961   
962   if (button->label_text)
963     {
964       GtkSettings *settings;
965
966       settings = gtk_widget_get_settings (GTK_WIDGET (button));        
967       g_object_get (settings, "gtk-button-images", &show, NULL);
968     }
969   else
970     show = TRUE;
971
972   return show;
973 }
974
975 static void
976 gtk_button_construct_child (GtkButton *button)
977 {
978   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
979   GtkStockItem item;
980   GtkWidget *child;
981   GtkWidget *label;
982   GtkWidget *box;
983   GtkWidget *align;
984   GtkWidget *image = NULL;
985   gchar *label_text = NULL;
986   gint image_spacing;
987
988   if (!button->constructed)
989     return;
990
991   if (!button->label_text && !priv->image)
992     return;
993
994   gtk_widget_style_get (GTK_WIDGET (button),
995                         "image-spacing", &image_spacing,
996                         NULL);
997
998   if (priv->image && !priv->image_is_stock)
999     {
1000       image = g_object_ref (priv->image);
1001       if (image->parent)
1002         gtk_container_remove (GTK_CONTAINER (image->parent), image);
1003     }
1004
1005   priv->image = NULL;
1006
1007   child = gtk_bin_get_child (GTK_BIN (button));
1008   if (child)
1009     gtk_container_remove (GTK_CONTAINER (button), child);
1010
1011   if (button->use_stock &&
1012       button->label_text &&
1013       gtk_stock_lookup (button->label_text, &item))
1014     {
1015       if (!image)
1016         image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
1017
1018       label_text = item.label;
1019     }
1020   else
1021     label_text = button->label_text;
1022
1023   if (image)
1024     {
1025       priv->image = image;
1026       g_object_set (priv->image,
1027                     "visible", show_image (button),
1028                     "no-show-all", TRUE,
1029                     NULL);
1030
1031       if (priv->image_position == GTK_POS_LEFT ||
1032           priv->image_position == GTK_POS_RIGHT)
1033         box = gtk_hbox_new (FALSE, image_spacing);
1034       else
1035         box = gtk_vbox_new (FALSE, image_spacing);
1036
1037       if (priv->align_set)
1038         align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1039       else
1040         align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1041
1042       if (priv->image_position == GTK_POS_LEFT ||
1043           priv->image_position == GTK_POS_TOP)
1044         gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1045       else
1046         gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1047
1048       if (label_text)
1049         {
1050           if (button->use_underline || button->use_stock)
1051             {
1052               label = gtk_label_new_with_mnemonic (label_text);
1053               gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1054                                              GTK_WIDGET (button));
1055             }
1056           else
1057             label = gtk_label_new (label_text);
1058
1059           if (priv->image_position == GTK_POS_RIGHT ||
1060               priv->image_position == GTK_POS_BOTTOM)
1061             gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1062           else
1063             gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1064         }
1065
1066       gtk_container_add (GTK_CONTAINER (button), align);
1067       gtk_container_add (GTK_CONTAINER (align), box);
1068       gtk_widget_show_all (align);
1069
1070       g_object_unref (image);
1071
1072       return;
1073     }
1074
1075   if (button->use_underline || button->use_stock)
1076     {
1077       label = gtk_label_new_with_mnemonic (button->label_text);
1078       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1079     }
1080   else
1081     label = gtk_label_new (button->label_text);
1082
1083   if (priv->align_set)
1084     gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1085
1086   gtk_widget_show (label);
1087   gtk_container_add (GTK_CONTAINER (button), label);
1088 }
1089
1090
1091 /**
1092  * gtk_button_new_with_label:
1093  * @label: The text you want the #GtkLabel to hold.
1094  *
1095  * Creates a #GtkButton widget with a #GtkLabel child containing the given
1096  * text.
1097  *
1098  * Returns: The newly created #GtkButton widget.
1099  */
1100 GtkWidget*
1101 gtk_button_new_with_label (const gchar *label)
1102 {
1103   return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1104 }
1105
1106 /**
1107  * gtk_button_new_from_stock:
1108  * @stock_id: the name of the stock item 
1109  *
1110  * Creates a new #GtkButton containing the image and text from a stock item.
1111  * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1112  * #GTK_STOCK_APPLY.
1113  *
1114  * If @stock_id is unknown, then it will be treated as a mnemonic
1115  * label (as for gtk_button_new_with_mnemonic()).
1116  *
1117  * Returns: a new #GtkButton
1118  **/
1119 GtkWidget*
1120 gtk_button_new_from_stock (const gchar *stock_id)
1121 {
1122   return g_object_new (GTK_TYPE_BUTTON,
1123                        "label", stock_id,
1124                        "use-stock", TRUE,
1125                        "use-underline", TRUE,
1126                        NULL);
1127 }
1128
1129 /**
1130  * gtk_button_new_with_mnemonic:
1131  * @label: The text of the button, with an underscore in front of the
1132  *         mnemonic character
1133  * @returns: a new #GtkButton
1134  *
1135  * Creates a new #GtkButton containing a label.
1136  * If characters in @label are preceded by an underscore, they are underlined.
1137  * If you need a literal underscore character in a label, use '__' (two 
1138  * underscores). The first underlined character represents a keyboard 
1139  * accelerator called a mnemonic.
1140  * Pressing Alt and that key activates the button.
1141  **/
1142 GtkWidget*
1143 gtk_button_new_with_mnemonic (const gchar *label)
1144 {
1145   return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE,  NULL);
1146 }
1147
1148 /**
1149  * gtk_button_pressed:
1150  * @button: The #GtkButton you want to send the signal to.
1151  *
1152  * Emits a #GtkButton::pressed signal to the given #GtkButton.
1153  *
1154  * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1155  */
1156 void
1157 gtk_button_pressed (GtkButton *button)
1158 {
1159   g_return_if_fail (GTK_IS_BUTTON (button));
1160
1161   
1162   g_signal_emit (button, button_signals[PRESSED], 0);
1163 }
1164
1165 /**
1166  * gtk_button_released:
1167  * @button: The #GtkButton you want to send the signal to.
1168  *
1169  * Emits a #GtkButton::released signal to the given #GtkButton.
1170  *
1171  * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1172  */
1173 void
1174 gtk_button_released (GtkButton *button)
1175 {
1176   g_return_if_fail (GTK_IS_BUTTON (button));
1177
1178   g_signal_emit (button, button_signals[RELEASED], 0);
1179 }
1180
1181 /**
1182  * gtk_button_clicked:
1183  * @button: The #GtkButton you want to send the signal to.
1184  *
1185  * Emits a #GtkButton::clicked signal to the given #GtkButton.
1186  */
1187 void
1188 gtk_button_clicked (GtkButton *button)
1189 {
1190   g_return_if_fail (GTK_IS_BUTTON (button));
1191
1192   g_signal_emit (button, button_signals[CLICKED], 0);
1193 }
1194
1195 /**
1196  * gtk_button_enter:
1197  * @button: The #GtkButton you want to send the signal to.
1198  *
1199  * Emits a #GtkButton::enter signal to the given #GtkButton.
1200  *
1201  * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1202  */
1203 void
1204 gtk_button_enter (GtkButton *button)
1205 {
1206   g_return_if_fail (GTK_IS_BUTTON (button));
1207
1208   g_signal_emit (button, button_signals[ENTER], 0);
1209 }
1210
1211 /**
1212  * gtk_button_leave:
1213  * @button: The #GtkButton you want to send the signal to.
1214  *
1215  * Emits a #GtkButton::leave signal to the given #GtkButton.
1216  *
1217  * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1218  */
1219 void
1220 gtk_button_leave (GtkButton *button)
1221 {
1222   g_return_if_fail (GTK_IS_BUTTON (button));
1223
1224   g_signal_emit (button, button_signals[LEAVE], 0);
1225 }
1226
1227 /**
1228  * gtk_button_set_relief:
1229  * @button: The #GtkButton you want to set relief styles of.
1230  * @newstyle: The GtkReliefStyle as described above.
1231  *
1232  * Sets the relief style of the edges of the given #GtkButton widget.
1233  * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1234  * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1235  *
1236  * <!-- FIXME: put pictures of each style -->
1237  */
1238 void
1239 gtk_button_set_relief (GtkButton *button,
1240                        GtkReliefStyle newrelief)
1241 {
1242   g_return_if_fail (GTK_IS_BUTTON (button));
1243
1244   if (newrelief != button->relief) 
1245     {
1246        button->relief = newrelief;
1247        g_object_notify (G_OBJECT (button), "relief");
1248        gtk_widget_queue_draw (GTK_WIDGET (button));
1249     }
1250 }
1251
1252 /**
1253  * gtk_button_get_relief:
1254  * @button: The #GtkButton you want the #GtkReliefStyle from.
1255  *
1256  * Returns the current relief style of the given #GtkButton.
1257  *
1258  * Returns: The current #GtkReliefStyle
1259  */
1260 GtkReliefStyle
1261 gtk_button_get_relief (GtkButton *button)
1262 {
1263   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1264
1265   return button->relief;
1266 }
1267
1268 static void
1269 gtk_button_realize (GtkWidget *widget)
1270 {
1271   GtkButton *button;
1272   GdkWindowAttr attributes;
1273   gint attributes_mask;
1274   gint border_width;
1275
1276   button = GTK_BUTTON (widget);
1277   gtk_widget_set_realized (widget, TRUE);
1278
1279   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1280
1281   attributes.window_type = GDK_WINDOW_CHILD;
1282   attributes.x = widget->allocation.x + border_width;
1283   attributes.y = widget->allocation.y + border_width;
1284   attributes.width = widget->allocation.width - border_width * 2;
1285   attributes.height = widget->allocation.height - border_width * 2;
1286   attributes.wclass = GDK_INPUT_ONLY;
1287   attributes.event_mask = gtk_widget_get_events (widget);
1288   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1289                             GDK_BUTTON_RELEASE_MASK |
1290                             GDK_ENTER_NOTIFY_MASK |
1291                             GDK_LEAVE_NOTIFY_MASK);
1292
1293   attributes_mask = GDK_WA_X | GDK_WA_Y;
1294
1295   widget->window = gtk_widget_get_parent_window (widget);
1296   g_object_ref (widget->window);
1297   
1298   button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1299                                          &attributes, attributes_mask);
1300   gdk_window_set_user_data (button->event_window, button);
1301
1302   widget->style = gtk_style_attach (widget->style, widget->window);
1303 }
1304
1305 static void
1306 gtk_button_unrealize (GtkWidget *widget)
1307 {
1308   GtkButton *button = GTK_BUTTON (widget);
1309
1310   if (button->activate_timeout)
1311     gtk_button_finish_activate (button, FALSE);
1312
1313   if (button->event_window)
1314     {
1315       gdk_window_set_user_data (button->event_window, NULL);
1316       gdk_window_destroy (button->event_window);
1317       button->event_window = NULL;
1318     }
1319   
1320   GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1321 }
1322
1323 static void
1324 gtk_button_map (GtkWidget *widget)
1325 {
1326   GtkButton *button = GTK_BUTTON (widget);
1327   
1328   GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1329
1330   if (button->event_window)
1331     gdk_window_show (button->event_window);
1332 }
1333
1334 static void
1335 gtk_button_unmap (GtkWidget *widget)
1336 {
1337   GtkButton *button = GTK_BUTTON (widget);
1338     
1339   if (button->event_window)
1340     gdk_window_hide (button->event_window);
1341
1342   GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1343 }
1344
1345 static void
1346 gtk_button_update_image_spacing (GtkButton *button)
1347 {
1348   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1349   GtkWidget *child; 
1350   gint spacing;
1351
1352   /* Keep in sync with gtk_button_construct_child,
1353    * we only want to update the spacing if the box 
1354    * was constructed there.
1355    */
1356   if (!button->constructed || !priv->image)
1357     return;
1358
1359   child = gtk_bin_get_child (GTK_BIN (button));
1360   if (GTK_IS_ALIGNMENT (child))
1361     {
1362       child = gtk_bin_get_child (GTK_BIN (child));
1363       if (GTK_IS_BOX (child))
1364         {
1365           gtk_widget_style_get (GTK_WIDGET (button),
1366                                 "image-spacing", &spacing,
1367                                 NULL);
1368
1369           gtk_box_set_spacing (GTK_BOX (child), spacing);
1370         }
1371     }   
1372 }
1373
1374 static void
1375 gtk_button_style_set (GtkWidget *widget,
1376                       GtkStyle  *prev_style)
1377 {
1378   gtk_button_update_image_spacing (GTK_BUTTON (widget));
1379 }
1380
1381 static void
1382 gtk_button_get_props (GtkButton *button,
1383                       GtkBorder *default_border,
1384                       GtkBorder *default_outside_border,
1385                       GtkBorder *inner_border,
1386                       gboolean  *interior_focus)
1387 {
1388   GtkWidget *widget =  GTK_WIDGET (button);
1389   GtkBorder *tmp_border;
1390
1391   if (default_border)
1392     {
1393       gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1394
1395       if (tmp_border)
1396         {
1397           *default_border = *tmp_border;
1398           gtk_border_free (tmp_border);
1399         }
1400       else
1401         *default_border = default_default_border;
1402     }
1403
1404   if (default_outside_border)
1405     {
1406       gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1407
1408       if (tmp_border)
1409         {
1410           *default_outside_border = *tmp_border;
1411           gtk_border_free (tmp_border);
1412         }
1413       else
1414         *default_outside_border = default_default_outside_border;
1415     }
1416
1417   if (inner_border)
1418     {
1419       gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1420
1421       if (tmp_border)
1422         {
1423           *inner_border = *tmp_border;
1424           gtk_border_free (tmp_border);
1425         }
1426       else
1427         *inner_border = default_inner_border;
1428     }
1429
1430   if (interior_focus)
1431     gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1432 }
1433
1434 static void
1435 gtk_button_size_allocate (GtkWidget     *widget,
1436                           GtkAllocation *allocation)
1437 {
1438   GtkButton *button = GTK_BUTTON (widget);
1439   GtkAllocation child_allocation;
1440   GtkWidget *child;
1441
1442   guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1443   gint xthickness = GTK_WIDGET (widget)->style->xthickness;
1444   gint ythickness = GTK_WIDGET (widget)->style->ythickness;
1445   GtkBorder default_border;
1446   GtkBorder inner_border;
1447   gint focus_width;
1448   gint focus_pad;
1449
1450   gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1451   gtk_widget_style_get (GTK_WIDGET (widget),
1452                         "focus-line-width", &focus_width,
1453                         "focus-padding", &focus_pad,
1454                         NULL);
1455  
1456                             
1457   widget->allocation = *allocation;
1458
1459   if (gtk_widget_get_realized (widget))
1460     gdk_window_move_resize (button->event_window,
1461                             widget->allocation.x + border_width,
1462                             widget->allocation.y + border_width,
1463                             widget->allocation.width - border_width * 2,
1464                             widget->allocation.height - border_width * 2);
1465
1466   child = gtk_bin_get_child (GTK_BIN (button));
1467   if (child && gtk_widget_get_visible (child))
1468     {
1469       child_allocation.x = widget->allocation.x + border_width + inner_border.left + xthickness;
1470       child_allocation.y = widget->allocation.y + border_width + inner_border.top + ythickness;
1471       
1472       child_allocation.width = MAX (1, widget->allocation.width -
1473                                     xthickness * 2 -
1474                                     inner_border.left -
1475                                     inner_border.right -
1476                                     border_width * 2);
1477       child_allocation.height = MAX (1, widget->allocation.height -
1478                                      ythickness * 2 -
1479                                      inner_border.top -
1480                                      inner_border.bottom -
1481                                      border_width * 2);
1482
1483       if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1484         {
1485           child_allocation.x += default_border.left;
1486           child_allocation.y += default_border.top;
1487           child_allocation.width =  MAX (1, child_allocation.width - default_border.left - default_border.right);
1488           child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
1489         }
1490
1491       if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1492         {
1493           child_allocation.x += focus_width + focus_pad;
1494           child_allocation.y += focus_width + focus_pad;
1495           child_allocation.width =  MAX (1, child_allocation.width - (focus_width + focus_pad) * 2);
1496           child_allocation.height = MAX (1, child_allocation.height - (focus_width + focus_pad) * 2);
1497         }
1498
1499       if (button->depressed)
1500         {
1501           gint child_displacement_x;
1502           gint child_displacement_y;
1503           
1504           gtk_widget_style_get (widget,
1505                                 "child-displacement-x", &child_displacement_x, 
1506                                 "child-displacement-y", &child_displacement_y,
1507                                 NULL);
1508           child_allocation.x += child_displacement_x;
1509           child_allocation.y += child_displacement_y;
1510         }
1511
1512       gtk_widget_size_allocate (child, &child_allocation);
1513     }
1514 }
1515
1516 void
1517 _gtk_button_paint (GtkButton          *button,
1518                    const GdkRectangle *area,
1519                    GtkStateType        state_type,
1520                    GtkShadowType       shadow_type,
1521                    const gchar        *main_detail,
1522                    const gchar        *default_detail)
1523 {
1524   GtkWidget *widget;
1525   gint width, height;
1526   gint x, y;
1527   gint border_width;
1528   GtkBorder default_border;
1529   GtkBorder default_outside_border;
1530   gboolean interior_focus;
1531   gint focus_width;
1532   gint focus_pad;
1533
1534   widget = GTK_WIDGET (button);
1535
1536   if (gtk_widget_is_drawable (widget))
1537     {
1538       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1539
1540       gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1541       gtk_widget_style_get (widget,
1542                             "focus-line-width", &focus_width,
1543                             "focus-padding", &focus_pad,
1544                             NULL); 
1545         
1546       x = widget->allocation.x + border_width;
1547       y = widget->allocation.y + border_width;
1548       width = widget->allocation.width - border_width * 2;
1549       height = widget->allocation.height - border_width * 2;
1550
1551       if (gtk_widget_has_default (widget) &&
1552           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1553         {
1554           gtk_paint_box (widget->style, widget->window,
1555                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
1556                          area, widget, "buttondefault",
1557                          x, y, width, height);
1558
1559           x += default_border.left;
1560           y += default_border.top;
1561           width -= default_border.left + default_border.right;
1562           height -= default_border.top + default_border.bottom;
1563         }
1564       else if (gtk_widget_get_can_default (widget))
1565         {
1566           x += default_outside_border.left;
1567           y += default_outside_border.top;
1568           width -= default_outside_border.left + default_outside_border.right;
1569           height -= default_outside_border.top + default_outside_border.bottom;
1570         }
1571        
1572       if (!interior_focus && gtk_widget_has_focus (widget))
1573         {
1574           x += focus_width + focus_pad;
1575           y += focus_width + focus_pad;
1576           width -= 2 * (focus_width + focus_pad);
1577           height -= 2 * (focus_width + focus_pad);
1578         }
1579
1580       if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1581           gtk_widget_get_state(widget) == GTK_STATE_PRELIGHT)
1582         gtk_paint_box (widget->style, widget->window,
1583                        state_type,
1584                        shadow_type, area, widget, "button",
1585                        x, y, width, height);
1586        
1587       if (gtk_widget_has_focus (widget))
1588         {
1589           gint child_displacement_x;
1590           gint child_displacement_y;
1591           gboolean displace_focus;
1592           
1593           gtk_widget_style_get (widget,
1594                                 "child-displacement-y", &child_displacement_y,
1595                                 "child-displacement-x", &child_displacement_x,
1596                                 "displace-focus", &displace_focus,
1597                                 NULL);
1598
1599           if (interior_focus)
1600             {
1601               x += widget->style->xthickness + focus_pad;
1602               y += widget->style->ythickness + focus_pad;
1603               width -= 2 * (widget->style->xthickness + focus_pad);
1604               height -=  2 * (widget->style->ythickness + focus_pad);
1605             }
1606           else
1607             {
1608               x -= focus_width + focus_pad;
1609               y -= focus_width + focus_pad;
1610               width += 2 * (focus_width + focus_pad);
1611               height += 2 * (focus_width + focus_pad);
1612             }
1613
1614           if (button->depressed && displace_focus)
1615             {
1616               x += child_displacement_x;
1617               y += child_displacement_y;
1618             }
1619
1620           gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
1621                            area, widget, "button",
1622                            x, y, width, height);
1623         }
1624     }
1625 }
1626
1627 static gboolean
1628 gtk_button_expose (GtkWidget      *widget,
1629                    GdkEventExpose *event)
1630 {
1631   if (gtk_widget_is_drawable (widget))
1632     {
1633       GtkButton *button = GTK_BUTTON (widget);
1634       
1635       _gtk_button_paint (button, &event->area,
1636                          gtk_widget_get_state (widget),
1637                          button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1638                          "button", "buttondefault");
1639
1640       GTK_WIDGET_CLASS (gtk_button_parent_class)->expose_event (widget, event);
1641     }
1642
1643   return FALSE;
1644 }
1645
1646 static gboolean
1647 gtk_button_button_press (GtkWidget      *widget,
1648                          GdkEventButton *event)
1649 {
1650   GtkButton *button;
1651
1652   if (event->type == GDK_BUTTON_PRESS)
1653     {
1654       button = GTK_BUTTON (widget);
1655
1656       if (button->focus_on_click && !gtk_widget_has_focus (widget))
1657         gtk_widget_grab_focus (widget);
1658
1659       if (event->button == 1)
1660         gtk_button_pressed (button);
1661     }
1662
1663   return TRUE;
1664 }
1665
1666 static gboolean
1667 gtk_button_button_release (GtkWidget      *widget,
1668                            GdkEventButton *event)
1669 {
1670   GtkButton *button;
1671
1672   if (event->button == 1)
1673     {
1674       button = GTK_BUTTON (widget);
1675       gtk_button_released (button);
1676     }
1677
1678   return TRUE;
1679 }
1680
1681 static gboolean
1682 gtk_button_grab_broken (GtkWidget          *widget,
1683                         GdkEventGrabBroken *event)
1684 {
1685   GtkButton *button = GTK_BUTTON (widget);
1686   gboolean save_in;
1687   
1688   /* Simulate a button release without the pointer in the button */
1689   if (button->button_down)
1690     {
1691       save_in = button->in_button;
1692       button->in_button = FALSE;
1693       gtk_button_released (button);
1694       if (save_in != button->in_button)
1695         {
1696           button->in_button = save_in;
1697           gtk_button_update_state (button);
1698         }
1699     }
1700
1701   return TRUE;
1702 }
1703
1704 static gboolean
1705 gtk_button_key_release (GtkWidget   *widget,
1706                         GdkEventKey *event)
1707 {
1708   GtkButton *button = GTK_BUTTON (widget);
1709
1710   if (button->activate_timeout)
1711     {
1712       gtk_button_finish_activate (button, TRUE);
1713       return TRUE;
1714     }
1715   else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1716     return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1717   else
1718     return FALSE;
1719 }
1720
1721 static gboolean
1722 gtk_button_enter_notify (GtkWidget        *widget,
1723                          GdkEventCrossing *event)
1724 {
1725   GtkButton *button;
1726   GtkWidget *event_widget;
1727
1728   button = GTK_BUTTON (widget);
1729   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1730
1731   if ((event_widget == widget) &&
1732       (event->detail != GDK_NOTIFY_INFERIOR))
1733     {
1734       button->in_button = TRUE;
1735       gtk_button_enter (button);
1736     }
1737
1738   return FALSE;
1739 }
1740
1741 static gboolean
1742 gtk_button_leave_notify (GtkWidget        *widget,
1743                          GdkEventCrossing *event)
1744 {
1745   GtkButton *button;
1746   GtkWidget *event_widget;
1747
1748   button = GTK_BUTTON (widget);
1749   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1750
1751   if ((event_widget == widget) &&
1752       (event->detail != GDK_NOTIFY_INFERIOR) &&
1753       (gtk_widget_get_sensitive (event_widget)))
1754     {
1755       button->in_button = FALSE;
1756       gtk_button_leave (button);
1757     }
1758
1759   return FALSE;
1760 }
1761
1762 static void
1763 gtk_real_button_pressed (GtkButton *button)
1764 {
1765   if (button->activate_timeout)
1766     return;
1767   
1768   button->button_down = TRUE;
1769   gtk_button_update_state (button);
1770 }
1771
1772 static void
1773 gtk_real_button_released (GtkButton *button)
1774 {
1775   if (button->button_down)
1776     {
1777       button->button_down = FALSE;
1778
1779       if (button->activate_timeout)
1780         return;
1781       
1782       if (button->in_button)
1783         gtk_button_clicked (button);
1784
1785       gtk_button_update_state (button);
1786     }
1787 }
1788
1789 static void 
1790 gtk_real_button_clicked (GtkButton *button)
1791 {
1792   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1793
1794   if (priv->action)
1795     gtk_action_activate (priv->action);
1796 }
1797
1798 static gboolean
1799 button_activate_timeout (gpointer data)
1800 {
1801   gtk_button_finish_activate (data, TRUE);
1802
1803   return FALSE;
1804 }
1805
1806 static void
1807 gtk_real_button_activate (GtkButton *button)
1808 {
1809   GtkWidget *widget = GTK_WIDGET (button);
1810   GtkButtonPrivate *priv;
1811   GdkDevice *device;
1812   guint32 time;
1813
1814   priv = GTK_BUTTON_GET_PRIVATE (button);
1815   device = gtk_get_current_event_device ();
1816
1817   g_return_if_fail (device && device->source == GDK_SOURCE_KEYBOARD);
1818
1819   if (gtk_widget_get_realized (widget) && !button->activate_timeout)
1820     {
1821       time = gtk_get_current_event_time ();
1822
1823       if (gdk_device_grab (device, button->event_window,
1824                            GDK_OWNERSHIP_WINDOW, TRUE,
1825                            GDK_KEY_PRESS | GDK_KEY_RELEASE,
1826                            NULL, time) == GDK_GRAB_SUCCESS)
1827         {
1828           gtk_device_grab_add (widget, device, TRUE);
1829           priv->grab_keyboard = device;
1830           priv->grab_time = time;
1831         }
1832
1833       button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1834                                                 button_activate_timeout,
1835                                                 button);
1836       button->button_down = TRUE;
1837       gtk_button_update_state (button);
1838       gtk_widget_queue_draw (GTK_WIDGET (button));
1839     }
1840 }
1841
1842 static void
1843 gtk_button_finish_activate (GtkButton *button,
1844                             gboolean   do_it)
1845 {
1846   GtkWidget *widget = GTK_WIDGET (button);
1847   GtkButtonPrivate *priv;
1848   
1849   priv = GTK_BUTTON_GET_PRIVATE (button);
1850
1851   g_source_remove (button->activate_timeout);
1852   button->activate_timeout = 0;
1853
1854   if (priv->grab_keyboard)
1855     {
1856       gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1857       gtk_device_grab_remove (widget, priv->grab_keyboard);
1858       priv->grab_keyboard = NULL;
1859     }
1860
1861   button->button_down = FALSE;
1862
1863   gtk_button_update_state (button);
1864   gtk_widget_queue_draw (GTK_WIDGET (button));
1865
1866   if (do_it)
1867     gtk_button_clicked (button);
1868 }
1869
1870
1871 static void
1872 gtk_button_size_request_init (GtkSizeRequestIface *iface)
1873 {
1874   iface->get_width  = gtk_button_get_width;
1875   iface->get_height = gtk_button_get_height;
1876 }
1877
1878 static void
1879 gtk_button_get_size (GtkSizeRequest *widget,
1880                      GtkOrientation  orientation,
1881                      gint           *minimum_size,
1882                      gint           *natural_size)
1883 {
1884   GtkButton *button = GTK_BUTTON (widget);
1885   GtkWidget *child;
1886   GtkBorder default_border;
1887   GtkBorder inner_border;
1888   gint focus_width;
1889   gint focus_pad;
1890   gint minimum, natural;
1891   guint border_width;
1892
1893   gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1894   gtk_widget_style_get (GTK_WIDGET (widget),
1895                         "focus-line-width", &focus_width,
1896                         "focus-padding", &focus_pad,
1897                         NULL);
1898
1899   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1900   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1901     {
1902       minimum = ((border_width +
1903                   GTK_WIDGET (widget)->style->xthickness) * 2 +
1904                  inner_border.left + inner_border.right);
1905
1906       if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1907         minimum += default_border.left + default_border.right;
1908     }
1909   else
1910     {
1911       minimum = ((border_width +
1912                   GTK_WIDGET (widget)->style->ythickness) * 2 +
1913                  inner_border.top + inner_border.bottom);
1914
1915       if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1916         minimum += default_border.top + default_border.bottom;
1917     }  
1918
1919   minimum += 2 * (focus_width + focus_pad);
1920   natural = minimum;
1921
1922   if ((child = gtk_bin_get_child (GTK_BIN (button))) && 
1923       gtk_widget_get_visible (child))
1924     {
1925       gint child_min, child_nat;
1926
1927       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1928         gtk_size_request_get_width (GTK_SIZE_REQUEST (child), 
1929                                     &child_min, &child_nat);
1930       else
1931         gtk_size_request_get_height (GTK_SIZE_REQUEST (child), 
1932                                      &child_min, &child_nat);
1933
1934       minimum += child_min;
1935       natural += child_nat;
1936     }
1937
1938   if (minimum_size)
1939     *minimum_size = minimum;
1940
1941   if (natural_size)
1942     *natural_size = natural;
1943 }
1944
1945 static void 
1946 gtk_button_get_width (GtkSizeRequest      *widget,
1947                       gint                *minimum_size,
1948                       gint                *natural_size)
1949 {
1950   gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1951 }
1952
1953 static void 
1954 gtk_button_get_height (GtkSizeRequest      *widget,
1955                        gint                *minimum_size,
1956                        gint                *natural_size)
1957 {
1958   gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1959 }
1960
1961 /**
1962  * gtk_button_set_label:
1963  * @button: a #GtkButton
1964  * @label: a string
1965  *
1966  * Sets the text of the label of the button to @str. This text is
1967  * also used to select the stock item if gtk_button_set_use_stock()
1968  * is used.
1969  *
1970  * This will also clear any previously set labels.
1971  **/
1972 void
1973 gtk_button_set_label (GtkButton   *button,
1974                       const gchar *label)
1975 {
1976   gchar *new_label;
1977   
1978   g_return_if_fail (GTK_IS_BUTTON (button));
1979
1980   new_label = g_strdup (label);
1981   g_free (button->label_text);
1982   button->label_text = new_label;
1983   
1984   gtk_button_construct_child (button);
1985   
1986   g_object_notify (G_OBJECT (button), "label");
1987 }
1988
1989 /**
1990  * gtk_button_get_label:
1991  * @button: a #GtkButton
1992  *
1993  * Fetches the text from the label of the button, as set by
1994  * gtk_button_set_label(). If the label text has not 
1995  * been set the return value will be %NULL. This will be the 
1996  * case if you create an empty button with gtk_button_new() to 
1997  * use as a container.
1998  *
1999  * Return value: The text of the label widget. This string is owned
2000  * by the widget and must not be modified or freed.
2001  **/
2002 G_CONST_RETURN gchar *
2003 gtk_button_get_label (GtkButton *button)
2004 {
2005   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2006   
2007   return button->label_text;
2008 }
2009
2010 /**
2011  * gtk_button_set_use_underline:
2012  * @button: a #GtkButton
2013  * @use_underline: %TRUE if underlines in the text indicate mnemonics
2014  *
2015  * If true, an underline in the text of the button label indicates
2016  * the next character should be used for the mnemonic accelerator key.
2017  */
2018 void
2019 gtk_button_set_use_underline (GtkButton *button,
2020                               gboolean   use_underline)
2021 {
2022   g_return_if_fail (GTK_IS_BUTTON (button));
2023
2024   use_underline = use_underline != FALSE;
2025
2026   if (use_underline != button->use_underline)
2027     {
2028       button->use_underline = use_underline;
2029   
2030       gtk_button_construct_child (button);
2031       
2032       g_object_notify (G_OBJECT (button), "use-underline");
2033     }
2034 }
2035
2036 /**
2037  * gtk_button_get_use_underline:
2038  * @button: a #GtkButton
2039  *
2040  * Returns whether an embedded underline in the button label indicates a
2041  * mnemonic. See gtk_button_set_use_underline ().
2042  *
2043  * Return value: %TRUE if an embedded underline in the button label
2044  *               indicates the mnemonic accelerator keys.
2045  **/
2046 gboolean
2047 gtk_button_get_use_underline (GtkButton *button)
2048 {
2049   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2050   
2051   return button->use_underline;
2052 }
2053
2054 /**
2055  * gtk_button_set_use_stock:
2056  * @button: a #GtkButton
2057  * @use_stock: %TRUE if the button should use a stock item
2058  *
2059  * If %TRUE, the label set on the button is used as a
2060  * stock id to select the stock item for the button.
2061  */
2062 void
2063 gtk_button_set_use_stock (GtkButton *button,
2064                           gboolean   use_stock)
2065 {
2066   g_return_if_fail (GTK_IS_BUTTON (button));
2067
2068   use_stock = use_stock != FALSE;
2069
2070   if (use_stock != button->use_stock)
2071     {
2072       button->use_stock = use_stock;
2073   
2074       gtk_button_construct_child (button);
2075       
2076       g_object_notify (G_OBJECT (button), "use-stock");
2077     }
2078 }
2079
2080 /**
2081  * gtk_button_get_use_stock:
2082  * @button: a #GtkButton
2083  *
2084  * Returns whether the button label is a stock item.
2085  *
2086  * Return value: %TRUE if the button label is used to
2087  *               select a stock item instead of being
2088  *               used directly as the label text.
2089  */
2090 gboolean
2091 gtk_button_get_use_stock (GtkButton *button)
2092 {
2093   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2094   
2095   return button->use_stock;
2096 }
2097
2098 /**
2099  * gtk_button_set_focus_on_click:
2100  * @button: a #GtkButton
2101  * @focus_on_click: whether the button grabs focus when clicked with the mouse
2102  *
2103  * Sets whether the button will grab focus when it is clicked with the mouse.
2104  * Making mouse clicks not grab focus is useful in places like toolbars where
2105  * you don't want the keyboard focus removed from the main area of the
2106  * application.
2107  *
2108  * Since: 2.4
2109  **/
2110 void
2111 gtk_button_set_focus_on_click (GtkButton *button,
2112                                gboolean   focus_on_click)
2113 {
2114   g_return_if_fail (GTK_IS_BUTTON (button));
2115   
2116   focus_on_click = focus_on_click != FALSE;
2117
2118   if (button->focus_on_click != focus_on_click)
2119     {
2120       button->focus_on_click = focus_on_click;
2121       
2122       g_object_notify (G_OBJECT (button), "focus-on-click");
2123     }
2124 }
2125
2126 /**
2127  * gtk_button_get_focus_on_click:
2128  * @button: a #GtkButton
2129  *
2130  * Returns whether the button grabs focus when it is clicked with the mouse.
2131  * See gtk_button_set_focus_on_click().
2132  *
2133  * Return value: %TRUE if the button grabs focus when it is clicked with
2134  *               the mouse.
2135  *
2136  * Since: 2.4
2137  **/
2138 gboolean
2139 gtk_button_get_focus_on_click (GtkButton *button)
2140 {
2141   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2142   
2143   return button->focus_on_click;
2144 }
2145
2146 /**
2147  * gtk_button_set_alignment:
2148  * @button: a #GtkButton
2149  * @xalign: the horizontal position of the child, 0.0 is left aligned, 
2150  *   1.0 is right aligned
2151  * @yalign: the vertical position of the child, 0.0 is top aligned, 
2152  *   1.0 is bottom aligned
2153  *
2154  * Sets the alignment of the child. This property has no effect unless 
2155  * the child is a #GtkMisc or a #GtkAligment.
2156  *
2157  * Since: 2.4
2158  */
2159 void
2160 gtk_button_set_alignment (GtkButton *button,
2161                           gfloat     xalign,
2162                           gfloat     yalign)
2163 {
2164   GtkButtonPrivate *priv;
2165
2166   g_return_if_fail (GTK_IS_BUTTON (button));
2167   
2168   priv = GTK_BUTTON_GET_PRIVATE (button);
2169
2170   priv->xalign = xalign;
2171   priv->yalign = yalign;
2172   priv->align_set = 1;
2173
2174   maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2175
2176   g_object_freeze_notify (G_OBJECT (button));
2177   g_object_notify (G_OBJECT (button), "xalign");
2178   g_object_notify (G_OBJECT (button), "yalign");
2179   g_object_thaw_notify (G_OBJECT (button));
2180 }
2181
2182 /**
2183  * gtk_button_get_alignment:
2184  * @button: a #GtkButton
2185  * @xalign: return location for horizontal alignment
2186  * @yalign: return location for vertical alignment
2187  *
2188  * Gets the alignment of the child in the button.
2189  *
2190  * Since: 2.4
2191  */
2192 void
2193 gtk_button_get_alignment (GtkButton *button,
2194                           gfloat    *xalign,
2195                           gfloat    *yalign)
2196 {
2197   GtkButtonPrivate *priv;
2198
2199   g_return_if_fail (GTK_IS_BUTTON (button));
2200   
2201   priv = GTK_BUTTON_GET_PRIVATE (button);
2202  
2203   if (xalign) 
2204     *xalign = priv->xalign;
2205
2206   if (yalign)
2207     *yalign = priv->yalign;
2208 }
2209
2210 /**
2211  * _gtk_button_set_depressed:
2212  * @button: a #GtkButton
2213  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2214  *
2215  * Sets whether the button is currently drawn as down or not. This is 
2216  * purely a visual setting, and is meant only for use by derived widgets
2217  * such as #GtkToggleButton.
2218  **/
2219 void
2220 _gtk_button_set_depressed (GtkButton *button,
2221                            gboolean   depressed)
2222 {
2223   GtkWidget *widget = GTK_WIDGET (button);
2224
2225   depressed = depressed != FALSE;
2226
2227   if (depressed != button->depressed)
2228     {
2229       button->depressed = depressed;
2230       gtk_widget_queue_resize (widget);
2231     }
2232 }
2233
2234 static void
2235 gtk_button_update_state (GtkButton *button)
2236 {
2237   gboolean depressed;
2238   GtkStateType new_state;
2239
2240   if (button->activate_timeout)
2241     depressed = button->depress_on_activate;
2242   else
2243     depressed = button->in_button && button->button_down;
2244
2245   if (button->in_button && (!button->button_down || !depressed))
2246     new_state = GTK_STATE_PRELIGHT;
2247   else
2248     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2249
2250   _gtk_button_set_depressed (button, depressed); 
2251   gtk_widget_set_state (GTK_WIDGET (button), new_state);
2252 }
2253
2254 static void 
2255 show_image_change_notify (GtkButton *button)
2256 {
2257   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2258
2259   if (priv->image) 
2260     {
2261       if (show_image (button))
2262         gtk_widget_show (priv->image);
2263       else
2264         gtk_widget_hide (priv->image);
2265     }
2266 }
2267
2268 static void
2269 traverse_container (GtkWidget *widget,
2270                     gpointer   data)
2271 {
2272   if (GTK_IS_BUTTON (widget))
2273     show_image_change_notify (GTK_BUTTON (widget));
2274   else if (GTK_IS_CONTAINER (widget))
2275     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2276 }
2277
2278 static void
2279 gtk_button_setting_changed (GtkSettings *settings)
2280 {
2281   GList *list, *l;
2282
2283   list = gtk_window_list_toplevels ();
2284
2285   for (l = list; l; l = l->next)
2286     gtk_container_forall (GTK_CONTAINER (l->data), 
2287                           traverse_container, NULL);
2288
2289   g_list_free (list);
2290 }
2291
2292
2293 static void
2294 gtk_button_screen_changed (GtkWidget *widget,
2295                            GdkScreen *previous_screen)
2296 {
2297   GtkButton *button;
2298   GtkSettings *settings;
2299   guint show_image_connection;
2300
2301   if (!gtk_widget_has_screen (widget))
2302     return;
2303
2304   button = GTK_BUTTON (widget);
2305
2306   /* If the button is being pressed while the screen changes the
2307     release might never occur, so we reset the state. */
2308   if (button->button_down)
2309     {
2310       button->button_down = FALSE;
2311       gtk_button_update_state (button);
2312     }
2313
2314   settings = gtk_widget_get_settings (widget);
2315
2316   show_image_connection = 
2317     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
2318                                          "gtk-button-connection"));
2319   
2320   if (show_image_connection)
2321     return;
2322
2323   show_image_connection =
2324     g_signal_connect (settings, "notify::gtk-button-images",
2325                       G_CALLBACK (gtk_button_setting_changed), NULL);
2326   g_object_set_data (G_OBJECT (settings), 
2327                      I_("gtk-button-connection"),
2328                      GUINT_TO_POINTER (show_image_connection));
2329
2330   show_image_change_notify (button);
2331 }
2332
2333 static void
2334 gtk_button_state_changed (GtkWidget    *widget,
2335                           GtkStateType  previous_state)
2336 {
2337   GtkButton *button = GTK_BUTTON (widget);
2338
2339   if (!gtk_widget_is_sensitive (widget))
2340     {
2341       button->in_button = FALSE;
2342       gtk_real_button_released (button);
2343     }
2344 }
2345
2346 static void
2347 gtk_button_grab_notify (GtkWidget *widget,
2348                         gboolean   was_grabbed)
2349 {
2350   GtkButton *button = GTK_BUTTON (widget);
2351   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2352   gboolean save_in;
2353
2354   if (button->activate_timeout &&
2355       priv->grab_keyboard &&
2356       gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2357     gtk_button_finish_activate (button, FALSE);
2358
2359   if (!was_grabbed)
2360     {
2361       save_in = button->in_button;
2362       button->in_button = FALSE; 
2363       gtk_real_button_released (button);
2364       if (save_in != button->in_button)
2365         {
2366           button->in_button = save_in;
2367           gtk_button_update_state (button);
2368         }
2369     }
2370 }
2371
2372 /**
2373  * gtk_button_set_image:
2374  * @button: a #GtkButton
2375  * @image: a widget to set as the image for the button
2376  *
2377  * Set the image of @button to the given widget. Note that
2378  * it depends on the #GtkSettings:gtk-button-images setting whether the
2379  * image will be displayed or not, you don't have to call
2380  * gtk_widget_show() on @image yourself.
2381  *
2382  * Since: 2.6
2383  */ 
2384 void
2385 gtk_button_set_image (GtkButton *button,
2386                       GtkWidget *image)
2387 {
2388   GtkButtonPrivate *priv;
2389
2390   g_return_if_fail (GTK_IS_BUTTON (button));
2391   g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2392
2393   priv = GTK_BUTTON_GET_PRIVATE (button);
2394
2395   if (priv->image && priv->image->parent)
2396     gtk_container_remove (GTK_CONTAINER (priv->image->parent), priv->image);
2397
2398   priv->image = image;
2399   priv->image_is_stock = (image == NULL);
2400
2401   gtk_button_construct_child (button);
2402
2403   g_object_notify (G_OBJECT (button), "image");
2404 }
2405
2406 /**
2407  * gtk_button_get_image:
2408  * @button: a #GtkButton
2409  *
2410  * Gets the widget that is currenty set as the image of @button.
2411  * This may have been explicitly set by gtk_button_set_image()
2412  * or constructed by gtk_button_new_from_stock().
2413  *
2414  * Return value: a #GtkWidget or %NULL in case there is no image
2415  *
2416  * Since: 2.6
2417  */
2418 GtkWidget *
2419 gtk_button_get_image (GtkButton *button)
2420 {
2421   GtkButtonPrivate *priv;
2422
2423   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2424
2425   priv = GTK_BUTTON_GET_PRIVATE (button);
2426   
2427   return priv->image;
2428 }
2429
2430 /**
2431  * gtk_button_set_image_position:
2432  * @button: a #GtkButton
2433  * @position: the position
2434  *
2435  * Sets the position of the image relative to the text 
2436  * inside the button.
2437  *
2438  * Since: 2.10
2439  */ 
2440 void
2441 gtk_button_set_image_position (GtkButton       *button,
2442                                GtkPositionType  position)
2443 {
2444
2445   GtkButtonPrivate *priv;
2446
2447   g_return_if_fail (GTK_IS_BUTTON (button));
2448   g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2449   
2450   priv = GTK_BUTTON_GET_PRIVATE (button);
2451
2452   if (priv->image_position != position)
2453     {
2454       priv->image_position = position;
2455
2456       gtk_button_construct_child (button);
2457
2458       g_object_notify (G_OBJECT (button), "image-position");
2459     }
2460 }
2461
2462 /**
2463  * gtk_button_get_image_position:
2464  * @button: a #GtkButton
2465  *
2466  * Gets the position of the image relative to the text 
2467  * inside the button.
2468  *
2469  * Return value: the position
2470  *
2471  * Since: 2.10
2472  */
2473 GtkPositionType
2474 gtk_button_get_image_position (GtkButton *button)
2475 {
2476   GtkButtonPrivate *priv;
2477
2478   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2479
2480   priv = GTK_BUTTON_GET_PRIVATE (button);
2481   
2482   return priv->image_position;
2483 }
2484
2485
2486 /**
2487  * gtk_button_get_event_window:
2488  * @button: a #GtkButton
2489  *
2490  * Returns the button's event window if it is realized, %NULL otherwise.
2491  * This function should be rarely needed.
2492  *
2493  * Return value: (transfer none): @button's event window.
2494  *
2495  * Since: 2.22
2496  */
2497 GdkWindow*
2498 gtk_button_get_event_window (GtkButton *button)
2499 {
2500   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2501
2502   return button->event_window;
2503 }