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