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