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