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