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