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