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