]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
button: add CSS borders to the size request
[~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 "gtkbox.h"
53 #include "gtkstock.h"
54 #include "gtkiconfactory.h"
55 #include "gtkactivatable.h"
56 #include "gtksizerequest.h"
57 #include "gtktypebuiltins.h"
58 #include "gtkprivate.h"
59 #include "gtkintl.h"
60 #include "a11y/gtkbuttonaccessible.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   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_BUTTON_ACCESSIBLE);
533 }
534
535 static void
536 gtk_button_init (GtkButton *button)
537 {
538   GtkButtonPrivate *priv;
539   GtkStyleContext *context;
540
541   button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
542                                               GTK_TYPE_BUTTON,
543                                               GtkButtonPrivate);
544   priv = button->priv;
545
546   gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
547   gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
548   gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
549
550   priv->label_text = NULL;
551
552   priv->constructed = FALSE;
553   priv->in_button = FALSE;
554   priv->button_down = FALSE;
555   priv->relief = GTK_RELIEF_NORMAL;
556   priv->use_stock = FALSE;
557   priv->use_underline = FALSE;
558   priv->depressed = FALSE;
559   priv->depress_on_activate = TRUE;
560   priv->focus_on_click = TRUE;
561
562   priv->xalign = 0.5;
563   priv->yalign = 0.5;
564   priv->align_set = 0;
565   priv->image_is_stock = TRUE;
566   priv->image_position = GTK_POS_LEFT;
567   priv->use_action_appearance = TRUE;
568
569   context = gtk_widget_get_style_context (GTK_WIDGET (button));
570   gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
571 }
572
573 static void
574 gtk_button_destroy (GtkWidget *widget)
575 {
576   GtkButton *button = GTK_BUTTON (widget);
577   GtkButtonPrivate *priv = button->priv;
578
579   if (priv->label_text)
580     {
581       g_free (priv->label_text);
582       priv->label_text = NULL;
583     }
584
585   GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
586 }
587
588 static GObject*
589 gtk_button_constructor (GType                  type,
590                         guint                  n_construct_properties,
591                         GObjectConstructParam *construct_params)
592 {
593   GObject *object;
594   GtkButton *button;
595   GtkButtonPrivate *priv;
596
597   object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
598                                                                   n_construct_properties,
599                                                                   construct_params);
600
601   button = GTK_BUTTON (object);
602   priv = button->priv;
603
604   priv->constructed = TRUE;
605
606   if (priv->label_text != NULL)
607     gtk_button_construct_child (button);
608   
609   return object;
610 }
611
612
613 static GType
614 gtk_button_child_type  (GtkContainer     *container)
615 {
616   if (!gtk_bin_get_child (GTK_BIN (container)))
617     return GTK_TYPE_WIDGET;
618   else
619     return G_TYPE_NONE;
620 }
621
622 static void
623 maybe_set_alignment (GtkButton *button,
624                      GtkWidget *widget)
625 {
626   GtkButtonPrivate *priv = button->priv;
627
628   if (GTK_IS_MISC (widget))
629     {
630       GtkMisc *misc = GTK_MISC (widget);
631       
632       if (priv->align_set)
633         gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
634     }
635   else if (GTK_IS_ALIGNMENT (widget))
636     {
637       GtkAlignment *alignment = GTK_ALIGNMENT (widget);
638       gfloat xscale, yscale;
639
640       g_object_get (alignment,
641                     "xscale", &xscale,
642                     "yscale", &yscale,
643                     NULL);
644
645       if (priv->align_set)
646         gtk_alignment_set (alignment,
647                            priv->xalign, priv->yalign,
648                            xscale, yscale);
649     }
650 }
651
652 static void
653 gtk_button_add (GtkContainer *container,
654                 GtkWidget    *widget)
655 {
656   maybe_set_alignment (GTK_BUTTON (container), widget);
657
658   GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
659 }
660
661 static void 
662 gtk_button_dispose (GObject *object)
663 {
664   GtkButton *button = GTK_BUTTON (object);
665   GtkButtonPrivate *priv = button->priv;
666
667   if (priv->action)
668     {
669       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
670       priv->action = NULL;
671     }
672   G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
673 }
674
675 static void
676 gtk_button_set_property (GObject         *object,
677                          guint            prop_id,
678                          const GValue    *value,
679                          GParamSpec      *pspec)
680 {
681   GtkButton *button = GTK_BUTTON (object);
682   GtkButtonPrivate *priv = button->priv;
683
684   switch (prop_id)
685     {
686     case PROP_LABEL:
687       gtk_button_set_label (button, g_value_get_string (value));
688       break;
689     case PROP_IMAGE:
690       gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
691       break;
692     case PROP_RELIEF:
693       gtk_button_set_relief (button, g_value_get_enum (value));
694       break;
695     case PROP_USE_UNDERLINE:
696       gtk_button_set_use_underline (button, g_value_get_boolean (value));
697       break;
698     case PROP_USE_STOCK:
699       gtk_button_set_use_stock (button, g_value_get_boolean (value));
700       break;
701     case PROP_FOCUS_ON_CLICK:
702       gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
703       break;
704     case PROP_XALIGN:
705       gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
706       break;
707     case PROP_YALIGN:
708       gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
709       break;
710     case PROP_IMAGE_POSITION:
711       gtk_button_set_image_position (button, g_value_get_enum (value));
712       break;
713     case PROP_ACTIVATABLE_RELATED_ACTION:
714       gtk_button_set_related_action (button, g_value_get_object (value));
715       break;
716     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
717       gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
718       break;
719     default:
720       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
721       break;
722     }
723 }
724
725 static void
726 gtk_button_get_property (GObject         *object,
727                          guint            prop_id,
728                          GValue          *value,
729                          GParamSpec      *pspec)
730 {
731   GtkButton *button = GTK_BUTTON (object);
732   GtkButtonPrivate *priv = button->priv;
733
734   switch (prop_id)
735     {
736     case PROP_LABEL:
737       g_value_set_string (value, priv->label_text);
738       break;
739     case PROP_IMAGE:
740       g_value_set_object (value, (GObject *)priv->image);
741       break;
742     case PROP_RELIEF:
743       g_value_set_enum (value, priv->relief);
744       break;
745     case PROP_USE_UNDERLINE:
746       g_value_set_boolean (value, priv->use_underline);
747       break;
748     case PROP_USE_STOCK:
749       g_value_set_boolean (value, priv->use_stock);
750       break;
751     case PROP_FOCUS_ON_CLICK:
752       g_value_set_boolean (value, priv->focus_on_click);
753       break;
754     case PROP_XALIGN:
755       g_value_set_float (value, priv->xalign);
756       break;
757     case PROP_YALIGN:
758       g_value_set_float (value, priv->yalign);
759       break;
760     case PROP_IMAGE_POSITION:
761       g_value_set_enum (value, priv->image_position);
762       break;
763     case PROP_ACTIVATABLE_RELATED_ACTION:
764       g_value_set_object (value, priv->action);
765       break;
766     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
767       g_value_set_boolean (value, priv->use_action_appearance);
768       break;
769     default:
770       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
771       break;
772     }
773 }
774
775 static void 
776 gtk_button_activatable_interface_init (GtkActivatableIface  *iface)
777 {
778   iface->update = gtk_button_update;
779   iface->sync_action_properties = gtk_button_sync_action_properties;
780 }
781
782 static void
783 activatable_update_stock_id (GtkButton *button,
784                              GtkAction *action)
785 {
786   if (!gtk_button_get_use_stock (button))
787     return;
788
789   gtk_button_set_label (button, gtk_action_get_stock_id (action));
790 }
791
792 static void
793 activatable_update_short_label (GtkButton *button,
794                                 GtkAction *action)
795 {
796   GtkWidget *child;
797   GtkWidget *image;
798
799   if (gtk_button_get_use_stock (button))
800     return;
801
802   image = gtk_button_get_image (button);
803
804   /* Dont touch custom child... */
805   child = gtk_bin_get_child (GTK_BIN (button));
806   if (GTK_IS_IMAGE (image) ||
807       child == NULL ||
808       GTK_IS_LABEL (child))
809     {
810       gtk_button_set_label (button, gtk_action_get_short_label (action));
811       gtk_button_set_use_underline (button, TRUE);
812     }
813 }
814
815 static void
816 activatable_update_icon_name (GtkButton *button,
817                               GtkAction *action)
818 {
819   GtkWidget *image;
820               
821   if (gtk_button_get_use_stock (button))
822     return;
823
824   image = gtk_button_get_image (button);
825
826   if (GTK_IS_IMAGE (image) &&
827       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
828        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
829     gtk_image_set_from_icon_name (GTK_IMAGE (image),
830                                   gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
831 }
832
833 static void
834 activatable_update_gicon (GtkButton *button,
835                           GtkAction *action)
836 {
837   GtkWidget *image = gtk_button_get_image (button);
838   GIcon *icon = gtk_action_get_gicon (action);
839   
840   if (GTK_IS_IMAGE (image) &&
841       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
842        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
843     gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
844 }
845
846 static void 
847 gtk_button_update (GtkActivatable *activatable,
848                    GtkAction      *action,
849                    const gchar    *property_name)
850 {
851   GtkButton *button = GTK_BUTTON (activatable);
852   GtkButtonPrivate *priv = button->priv;
853
854   if (strcmp (property_name, "visible") == 0)
855     {
856       if (gtk_action_is_visible (action))
857         gtk_widget_show (GTK_WIDGET (activatable));
858       else
859         gtk_widget_hide (GTK_WIDGET (activatable));
860     }
861   else if (strcmp (property_name, "sensitive") == 0)
862     gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
863
864   if (!priv->use_action_appearance)
865     return;
866
867   if (strcmp (property_name, "stock-id") == 0)
868     activatable_update_stock_id (GTK_BUTTON (activatable), action);
869   else if (strcmp (property_name, "gicon") == 0)
870     activatable_update_gicon (GTK_BUTTON (activatable), action);
871   else if (strcmp (property_name, "short-label") == 0)
872     activatable_update_short_label (GTK_BUTTON (activatable), action);
873   else if (strcmp (property_name, "icon-name") == 0)
874     activatable_update_icon_name (GTK_BUTTON (activatable), action);
875 }
876
877 static void
878 gtk_button_sync_action_properties (GtkActivatable *activatable,
879                                    GtkAction      *action)
880 {
881   GtkButton *button = GTK_BUTTON (activatable);
882   GtkButtonPrivate *priv = button->priv;
883
884   if (!action)
885     return;
886
887   if (gtk_action_is_visible (action))
888     gtk_widget_show (GTK_WIDGET (activatable));
889   else
890     gtk_widget_hide (GTK_WIDGET (activatable));
891   
892   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
893   
894   if (priv->use_action_appearance)
895     {
896       activatable_update_stock_id (GTK_BUTTON (activatable), action);
897       activatable_update_short_label (GTK_BUTTON (activatable), action);
898       activatable_update_gicon (GTK_BUTTON (activatable), action);
899       activatable_update_icon_name (GTK_BUTTON (activatable), action);
900     }
901 }
902
903 static void
904 gtk_button_set_related_action (GtkButton *button,
905                                GtkAction *action)
906 {
907   GtkButtonPrivate *priv = button->priv;
908
909   if (priv->action == action)
910     return;
911
912   /* This should be a default handler, but for compatibility reasons
913    * we need to support derived classes that don't chain up their
914    * clicked handler.
915    */
916   g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
917   if (action)
918     g_signal_connect_after (button, "clicked",
919                             G_CALLBACK (gtk_real_button_clicked), NULL);
920
921   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
922
923   priv->action = action;
924 }
925
926 static void
927 gtk_button_set_use_action_appearance (GtkButton *button,
928                                       gboolean   use_appearance)
929 {
930   GtkButtonPrivate *priv = button->priv;
931
932   if (priv->use_action_appearance != use_appearance)
933     {
934       priv->use_action_appearance = use_appearance;
935
936       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
937     }
938 }
939
940 /**
941  * gtk_button_new:
942  *
943  * Creates a new #GtkButton widget. To add a child widget to the button,
944  * use gtk_container_add().
945  *
946  * Returns: The newly created #GtkButton widget.
947  */
948 GtkWidget*
949 gtk_button_new (void)
950 {
951   return g_object_new (GTK_TYPE_BUTTON, NULL);
952 }
953
954 static gboolean
955 show_image (GtkButton *button)
956 {
957   GtkButtonPrivate *priv = button->priv;
958   gboolean show;
959
960   if (priv->label_text)
961     {
962       GtkSettings *settings;
963
964       settings = gtk_widget_get_settings (GTK_WIDGET (button));        
965       g_object_get (settings, "gtk-button-images", &show, NULL);
966     }
967   else
968     show = TRUE;
969
970   return show;
971 }
972
973 static void
974 gtk_button_construct_child (GtkButton *button)
975 {
976   GtkButtonPrivate *priv = button->priv;
977   GtkStyleContext *context;
978   GtkStockItem item;
979   GtkWidget *child;
980   GtkWidget *label;
981   GtkWidget *box;
982   GtkWidget *align;
983   GtkWidget *image = NULL;
984   gchar *label_text = NULL;
985   gint image_spacing;
986
987   if (!priv->constructed)
988     return;
989
990   if (!priv->label_text && !priv->image)
991     return;
992
993   context = gtk_widget_get_style_context (GTK_WIDGET (button));
994
995   gtk_style_context_get_style (context,
996                                "image-spacing", &image_spacing,
997                                NULL);
998
999   if (priv->image && !priv->image_is_stock)
1000     {
1001       GtkWidget *parent;
1002
1003       image = g_object_ref (priv->image);
1004
1005       parent = gtk_widget_get_parent (image);
1006       if (parent)
1007         gtk_container_remove (GTK_CONTAINER (parent), image);
1008     }
1009
1010   priv->image = NULL;
1011
1012   child = gtk_bin_get_child (GTK_BIN (button));
1013   if (child)
1014     gtk_container_remove (GTK_CONTAINER (button), child);
1015
1016   if (priv->use_stock &&
1017       priv->label_text &&
1018       gtk_stock_lookup (priv->label_text, &item))
1019     {
1020       if (!image)
1021         image = g_object_ref (gtk_image_new_from_stock (priv->label_text, GTK_ICON_SIZE_BUTTON));
1022
1023       label_text = item.label;
1024     }
1025   else
1026     label_text = priv->label_text;
1027
1028   if (image)
1029     {
1030       priv->image = image;
1031       g_object_set (priv->image,
1032                     "visible", show_image (button),
1033                     "no-show-all", TRUE,
1034                     NULL);
1035
1036       if (priv->image_position == GTK_POS_LEFT ||
1037           priv->image_position == GTK_POS_RIGHT)
1038         box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, image_spacing);
1039       else
1040         box = gtk_box_new (GTK_ORIENTATION_VERTICAL, image_spacing);
1041
1042       if (priv->align_set)
1043         align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1044       else
1045         align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1046
1047       if (priv->image_position == GTK_POS_LEFT ||
1048           priv->image_position == GTK_POS_TOP)
1049         gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1050       else
1051         gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1052
1053       if (label_text)
1054         {
1055           if (priv->use_underline || priv->use_stock)
1056             {
1057               label = gtk_label_new_with_mnemonic (label_text);
1058               gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1059                                              GTK_WIDGET (button));
1060             }
1061           else
1062             label = gtk_label_new (label_text);
1063
1064           if (priv->image_position == GTK_POS_RIGHT ||
1065               priv->image_position == GTK_POS_BOTTOM)
1066             gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1067           else
1068             gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1069         }
1070
1071       gtk_container_add (GTK_CONTAINER (button), align);
1072       gtk_container_add (GTK_CONTAINER (align), box);
1073       gtk_widget_show_all (align);
1074
1075       g_object_unref (image);
1076
1077       return;
1078     }
1079
1080   if (priv->use_underline || priv->use_stock)
1081     {
1082       label = gtk_label_new_with_mnemonic (priv->label_text);
1083       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1084     }
1085   else
1086     label = gtk_label_new (priv->label_text);
1087
1088   if (priv->align_set)
1089     gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1090
1091   gtk_widget_show (label);
1092   gtk_container_add (GTK_CONTAINER (button), label);
1093 }
1094
1095
1096 /**
1097  * gtk_button_new_with_label:
1098  * @label: The text you want the #GtkLabel to hold.
1099  *
1100  * Creates a #GtkButton widget with a #GtkLabel child containing the given
1101  * text.
1102  *
1103  * Returns: The newly created #GtkButton widget.
1104  */
1105 GtkWidget*
1106 gtk_button_new_with_label (const gchar *label)
1107 {
1108   return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1109 }
1110
1111 /**
1112  * gtk_button_new_from_stock:
1113  * @stock_id: the name of the stock item 
1114  *
1115  * Creates a new #GtkButton containing the image and text from a stock item.
1116  * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1117  * #GTK_STOCK_APPLY.
1118  *
1119  * If @stock_id is unknown, then it will be treated as a mnemonic
1120  * label (as for gtk_button_new_with_mnemonic()).
1121  *
1122  * Returns: a new #GtkButton
1123  **/
1124 GtkWidget*
1125 gtk_button_new_from_stock (const gchar *stock_id)
1126 {
1127   return g_object_new (GTK_TYPE_BUTTON,
1128                        "label", stock_id,
1129                        "use-stock", TRUE,
1130                        "use-underline", TRUE,
1131                        NULL);
1132 }
1133
1134 /**
1135  * gtk_button_new_with_mnemonic:
1136  * @label: The text of the button, with an underscore in front of the
1137  *         mnemonic character
1138  *
1139  * Creates a new #GtkButton containing a label.
1140  * If characters in @label are preceded by an underscore, they are underlined.
1141  * If you need a literal underscore character in a label, use '__' (two
1142  * underscores). The first underlined character represents a keyboard
1143  * accelerator called a mnemonic.
1144  * Pressing Alt and that key activates the button.
1145  *
1146  * Returns: a new #GtkButton
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                       GtkBorder *border,
1406                       gboolean  *interior_focus)
1407 {
1408   GtkStyleContext *context;
1409   GtkStateFlags state;
1410   GtkBorder *tmp_border;
1411
1412   context = gtk_widget_get_style_context (GTK_WIDGET (button));
1413   state = gtk_style_context_get_state (context);
1414
1415   if (default_border)
1416     {
1417       gtk_style_context_get_style (context,
1418                                    "default-border", &tmp_border,
1419                                    NULL);
1420
1421       if (tmp_border)
1422         {
1423           *default_border = *tmp_border;
1424           gtk_border_free (tmp_border);
1425         }
1426       else
1427         *default_border = default_default_border;
1428     }
1429
1430   if (default_outside_border)
1431     {
1432       gtk_style_context_get_style (context,
1433                                    "default-outside-border", &tmp_border,
1434                                    NULL);
1435
1436       if (tmp_border)
1437         {
1438           *default_outside_border = *tmp_border;
1439           gtk_border_free (tmp_border);
1440         }
1441       else
1442         *default_outside_border = default_default_outside_border;
1443     }
1444
1445   if (inner_border)
1446     {
1447       gtk_style_context_get_style (context,
1448                                    "inner-border", &tmp_border,
1449                                    NULL);
1450
1451       if (tmp_border)
1452         {
1453           *inner_border = *tmp_border;
1454           gtk_border_free (tmp_border);
1455         }
1456       else
1457         *inner_border = default_inner_border;
1458     }
1459
1460   if (interior_focus)
1461     {
1462       gtk_style_context_get_style (context,
1463                                    "interior-focus", interior_focus,
1464                                    NULL);
1465     }
1466
1467   if (padding)
1468     gtk_style_context_get_padding (context, state, padding);
1469
1470   if (border)
1471     gtk_style_context_get_border (context, state, border);
1472 }
1473
1474 static void
1475 gtk_button_size_allocate (GtkWidget     *widget,
1476                           GtkAllocation *allocation)
1477 {
1478   GtkButton *button = GTK_BUTTON (widget);
1479   GtkButtonPrivate *priv = button->priv;
1480   GtkAllocation child_allocation;
1481   GtkStyleContext *context;
1482   GtkWidget *child;
1483   GtkBorder default_border;
1484   GtkBorder inner_border;
1485   GtkBorder padding;
1486   GtkBorder border;
1487   gint focus_width;
1488   gint focus_pad;
1489
1490   context = gtk_widget_get_style_context (widget);
1491
1492   gtk_button_get_props (button, &default_border, NULL, &inner_border,
1493                         &padding, &border, NULL);
1494   gtk_style_context_get_style (context,
1495                               "focus-line-width", &focus_width,
1496                               "focus-padding", &focus_pad,
1497                               NULL);
1498
1499   gtk_widget_set_allocation (widget, allocation);
1500
1501   if (gtk_widget_get_realized (widget))
1502     gdk_window_move_resize (priv->event_window,
1503                             allocation->x,
1504                             allocation->y,
1505                             allocation->width,
1506                             allocation->height);
1507
1508   child = gtk_bin_get_child (GTK_BIN (button));
1509   if (child && gtk_widget_get_visible (child))
1510     {
1511       child_allocation.x = allocation->x + inner_border.left + padding.left + border.left;
1512       child_allocation.y = allocation->y + inner_border.top + padding.top + border.top;
1513
1514       child_allocation.width =
1515         allocation->width -
1516         (padding.left + padding.right) -
1517         (border.left + border.right) -
1518         (inner_border.left + inner_border.right);
1519
1520       child_allocation.height = 
1521         allocation->height -
1522         (padding.top + padding.bottom) -
1523         (border.top + border.bottom) -
1524         (inner_border.top + inner_border.bottom);
1525
1526       if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1527         {
1528           child_allocation.x += default_border.left;
1529           child_allocation.y += default_border.top;
1530           child_allocation.width =  child_allocation.width - default_border.left - default_border.right;
1531           child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1532         }
1533
1534       if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1535         {
1536           child_allocation.x += focus_width + focus_pad;
1537           child_allocation.y += focus_width + focus_pad;
1538           child_allocation.width =  child_allocation.width - (focus_width + focus_pad) * 2;
1539           child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1540         }
1541
1542       if (priv->depressed)
1543         {
1544           gint child_displacement_x;
1545           gint child_displacement_y;
1546
1547           gtk_style_context_get_style (context,
1548                                        "child-displacement-x", &child_displacement_x,
1549                                        "child-displacement-y", &child_displacement_y,
1550                                        NULL);
1551           child_allocation.x += child_displacement_x;
1552           child_allocation.y += child_displacement_y;
1553         }
1554
1555       child_allocation.width  = MAX (1, child_allocation.width);
1556       child_allocation.height = MAX (1, child_allocation.height);
1557
1558       gtk_widget_size_allocate (child, &child_allocation);
1559     }
1560 }
1561
1562 void
1563 _gtk_button_paint (GtkButton          *button,
1564                    cairo_t            *cr,
1565                    int                 width,
1566                    int                 height,
1567                    GtkStateFlags       state)
1568 {
1569   GtkButtonPrivate *priv = button->priv;
1570   GtkWidget *widget;
1571   gint x, y;
1572   GtkBorder default_border;
1573   GtkBorder default_outside_border;
1574   gboolean interior_focus;
1575   gint focus_width;
1576   gint focus_pad;
1577   GtkAllocation allocation;
1578   GtkStyleContext *context;
1579   gboolean draw_focus;
1580
1581   widget = GTK_WIDGET (button);
1582   context = gtk_widget_get_style_context (widget);
1583
1584   gtk_style_context_save (context);
1585
1586   gtk_button_get_props (button, &default_border, &default_outside_border, NULL, NULL, NULL, &interior_focus);
1587   gtk_style_context_get_style (context,
1588                                "focus-line-width", &focus_width,
1589                                "focus-padding", &focus_pad,
1590                                NULL);
1591
1592   gtk_widget_get_allocation (widget, &allocation);
1593
1594   x = 0;
1595   y = 0;
1596
1597   if (gtk_widget_has_default (widget) &&
1598       priv->relief == GTK_RELIEF_NORMAL)
1599     {
1600       x += default_border.left;
1601       y += default_border.top;
1602       width -= default_border.left + default_border.right;
1603       height -= default_border.top + default_border.bottom;
1604
1605       gtk_style_context_add_class (context, GTK_STYLE_CLASS_DEFAULT);
1606     }
1607   else if (gtk_widget_get_can_default (widget))
1608     {
1609       x += default_outside_border.left;
1610       y += default_outside_border.top;
1611       width -= default_outside_border.left + default_outside_border.right;
1612       height -= default_outside_border.top + default_outside_border.bottom;
1613     }
1614
1615   draw_focus = gtk_widget_has_visible_focus (widget);
1616
1617
1618   if (!interior_focus && draw_focus)
1619     {
1620       x += focus_width + focus_pad;
1621       y += focus_width + focus_pad;
1622       width -= 2 * (focus_width + focus_pad);
1623       height -= 2 * (focus_width + focus_pad);
1624     }
1625
1626   if (priv->relief != GTK_RELIEF_NONE || priv->depressed ||
1627       state & GTK_STATE_FLAG_PRELIGHT)
1628     {
1629       gtk_render_background (context, cr,
1630                              x, y, width, height);
1631       gtk_render_frame (context, cr,
1632                         x, y, width, height);
1633     }
1634
1635   if (draw_focus)
1636     {
1637       gint child_displacement_x;
1638       gint child_displacement_y;
1639       gboolean displace_focus;
1640       GtkBorder border;
1641
1642       gtk_style_context_get_style (context,
1643                                    "child-displacement-y", &child_displacement_y,
1644                                    "child-displacement-x", &child_displacement_x,
1645                                    "displace-focus", &displace_focus,
1646                                    NULL);
1647       gtk_style_context_get_border (context, state, &border);
1648
1649       if (interior_focus)
1650         {
1651           x += border.left + focus_pad;
1652           y += border.top + focus_pad;
1653           width -= (2 * focus_pad) + border.left + border.right;
1654           height -=  (2 * focus_pad) + border.top + border.bottom;
1655         }
1656       else
1657         {
1658           x -= focus_width + focus_pad;
1659           y -= focus_width + focus_pad;
1660           width += 2 * (focus_width + focus_pad);
1661           height += 2 * (focus_width + focus_pad);
1662         }
1663
1664       if (priv->depressed && displace_focus)
1665         {
1666           x += child_displacement_x;
1667           y += child_displacement_y;
1668         }
1669
1670       gtk_render_focus (context, cr, x, y, width, height);
1671     }
1672
1673   gtk_style_context_restore (context);
1674 }
1675
1676 static gboolean
1677 gtk_button_draw (GtkWidget *widget,
1678                  cairo_t   *cr)
1679 {
1680   GtkButton *button = GTK_BUTTON (widget);
1681
1682   _gtk_button_paint (button, cr, 
1683                      gtk_widget_get_allocated_width (widget),
1684                      gtk_widget_get_allocated_height (widget),
1685                      gtk_widget_get_state_flags (widget));
1686
1687   GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1688
1689   return FALSE;
1690 }
1691
1692 static gboolean
1693 gtk_button_button_press (GtkWidget      *widget,
1694                          GdkEventButton *event)
1695 {
1696   GtkButton *button;
1697   GtkButtonPrivate *priv;
1698
1699   if (event->type == GDK_BUTTON_PRESS)
1700     {
1701       button = GTK_BUTTON (widget);
1702       priv = button->priv;
1703
1704       if (priv->focus_on_click && !gtk_widget_has_focus (widget))
1705         gtk_widget_grab_focus (widget);
1706
1707       if (event->button == 1)
1708         g_signal_emit (button, button_signals[PRESSED], 0);
1709     }
1710
1711   return TRUE;
1712 }
1713
1714 static gboolean
1715 gtk_button_button_release (GtkWidget      *widget,
1716                            GdkEventButton *event)
1717 {
1718   GtkButton *button;
1719
1720   if (event->button == 1)
1721     {
1722       button = GTK_BUTTON (widget);
1723       g_signal_emit (button, button_signals[RELEASED], 0);
1724     }
1725
1726   return TRUE;
1727 }
1728
1729 static gboolean
1730 gtk_button_grab_broken (GtkWidget          *widget,
1731                         GdkEventGrabBroken *event)
1732 {
1733   GtkButton *button = GTK_BUTTON (widget);
1734   GtkButtonPrivate *priv = button->priv;
1735   gboolean save_in;
1736   
1737   /* Simulate a button release without the pointer in the button */
1738   if (priv->button_down)
1739     {
1740       save_in = priv->in_button;
1741       priv->in_button = FALSE;
1742       g_signal_emit (button, button_signals[RELEASED], 0);
1743       if (save_in != priv->in_button)
1744         {
1745           priv->in_button = save_in;
1746           gtk_button_update_state (button);
1747         }
1748     }
1749
1750   return TRUE;
1751 }
1752
1753 static gboolean
1754 gtk_button_key_release (GtkWidget   *widget,
1755                         GdkEventKey *event)
1756 {
1757   GtkButton *button = GTK_BUTTON (widget);
1758   GtkButtonPrivate *priv = button->priv;
1759
1760   if (priv->activate_timeout)
1761     {
1762       gtk_button_finish_activate (button, TRUE);
1763       return TRUE;
1764     }
1765   else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1766     return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1767   else
1768     return FALSE;
1769 }
1770
1771 static gboolean
1772 gtk_button_enter_notify (GtkWidget        *widget,
1773                          GdkEventCrossing *event)
1774 {
1775   GtkButton *button = GTK_BUTTON (widget);
1776   GtkButtonPrivate *priv = button->priv;
1777
1778   if ((event->window == button->priv->event_window) &&
1779       (event->detail != GDK_NOTIFY_INFERIOR))
1780     {
1781       priv->in_button = TRUE;
1782       g_signal_emit (button, button_signals[ENTER], 0);
1783     }
1784
1785   return FALSE;
1786 }
1787
1788 static gboolean
1789 gtk_button_leave_notify (GtkWidget        *widget,
1790                          GdkEventCrossing *event)
1791 {
1792   GtkButton *button = GTK_BUTTON (widget);
1793   GtkButtonPrivate *priv = button->priv;
1794
1795   if ((event->window == button->priv->event_window) &&
1796       (event->detail != GDK_NOTIFY_INFERIOR) &&
1797       (gtk_widget_get_sensitive (widget)))
1798     {
1799       priv->in_button = FALSE;
1800       g_signal_emit (button, button_signals[LEAVE], 0);
1801     }
1802
1803   return FALSE;
1804 }
1805
1806 static void
1807 gtk_real_button_pressed (GtkButton *button)
1808 {
1809   GtkButtonPrivate *priv = button->priv;
1810
1811   if (priv->activate_timeout)
1812     return;
1813
1814   priv->button_down = TRUE;
1815   gtk_button_update_state (button);
1816 }
1817
1818 static void
1819 gtk_real_button_released (GtkButton *button)
1820 {
1821   GtkButtonPrivate *priv = button->priv;
1822
1823   if (priv->button_down)
1824     {
1825       priv->button_down = FALSE;
1826
1827       if (priv->activate_timeout)
1828         return;
1829
1830       if (priv->in_button)
1831         gtk_button_clicked (button);
1832
1833       gtk_button_update_state (button);
1834     }
1835 }
1836
1837 static void 
1838 gtk_real_button_clicked (GtkButton *button)
1839 {
1840   GtkButtonPrivate *priv = button->priv;
1841
1842   if (priv->action)
1843     gtk_action_activate (priv->action);
1844 }
1845
1846 static gboolean
1847 button_activate_timeout (gpointer data)
1848 {
1849   gtk_button_finish_activate (data, TRUE);
1850
1851   return FALSE;
1852 }
1853
1854 static void
1855 gtk_real_button_activate (GtkButton *button)
1856 {
1857   GtkWidget *widget = GTK_WIDGET (button);
1858   GtkButtonPrivate *priv = button->priv;
1859   GdkDevice *device;
1860   guint32 time;
1861
1862   device = gtk_get_current_event_device ();
1863
1864   if (device && gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
1865     device = gdk_device_get_associated_device (device);
1866
1867   if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
1868     {
1869       time = gtk_get_current_event_time ();
1870
1871       /* bgo#626336 - Only grab if we have a device (from an event), not if we
1872        * were activated programmatically when no event is available.
1873        */
1874       if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1875         {
1876           if (gdk_device_grab (device, priv->event_window,
1877                                GDK_OWNERSHIP_WINDOW, TRUE,
1878                                GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1879                                NULL, time) == GDK_GRAB_SUCCESS)
1880             {
1881               gtk_device_grab_add (widget, device, TRUE);
1882               priv->grab_keyboard = device;
1883               priv->grab_time = time;
1884             }
1885         }
1886
1887       priv->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1888                                                 button_activate_timeout,
1889                                                 button);
1890       priv->button_down = TRUE;
1891       gtk_button_update_state (button);
1892       gtk_widget_queue_draw (GTK_WIDGET (button));
1893     }
1894 }
1895
1896 static void
1897 gtk_button_finish_activate (GtkButton *button,
1898                             gboolean   do_it)
1899 {
1900   GtkWidget *widget = GTK_WIDGET (button);
1901   GtkButtonPrivate *priv = button->priv;
1902
1903   g_source_remove (priv->activate_timeout);
1904   priv->activate_timeout = 0;
1905
1906   if (priv->grab_keyboard)
1907     {
1908       gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1909       gtk_device_grab_remove (widget, priv->grab_keyboard);
1910       priv->grab_keyboard = NULL;
1911     }
1912
1913   priv->button_down = FALSE;
1914
1915   gtk_button_update_state (button);
1916   gtk_widget_queue_draw (GTK_WIDGET (button));
1917
1918   if (do_it)
1919     gtk_button_clicked (button);
1920 }
1921
1922
1923 static void
1924 gtk_button_get_size (GtkWidget      *widget,
1925                      GtkOrientation  orientation,
1926                      gint           *minimum_size,
1927                      gint           *natural_size)
1928 {
1929   GtkButton *button = GTK_BUTTON (widget);
1930   GtkStyleContext *context;
1931   GtkWidget *child;
1932   GtkBorder default_border;
1933   GtkBorder inner_border;
1934   GtkBorder padding;
1935   GtkBorder border;
1936   gint focus_width;
1937   gint focus_pad;
1938   gint minimum, natural;
1939
1940   context = gtk_widget_get_style_context (widget);
1941
1942   gtk_button_get_props (button, &default_border, NULL, &inner_border,
1943                         &padding, &border, NULL);
1944   gtk_style_context_get_style (context,
1945                                "focus-line-width", &focus_width,
1946                                "focus-padding", &focus_pad,
1947                                NULL);
1948
1949   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1950     {
1951       minimum = inner_border.left + inner_border.right +
1952         padding.left + padding.right +
1953         border.left + border.right;
1954
1955       if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1956         minimum += default_border.left + default_border.right;
1957     }
1958   else
1959     {
1960       minimum = inner_border.top + inner_border.bottom +
1961         padding.top + padding.bottom +
1962         border.top + border.bottom;
1963
1964       if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1965         minimum += default_border.top + default_border.bottom;
1966     }  
1967
1968   minimum += 2 * (focus_width + focus_pad);
1969   natural = minimum;
1970
1971   if ((child = gtk_bin_get_child (GTK_BIN (button))) && 
1972       gtk_widget_get_visible (child))
1973     {
1974       gint child_min, child_nat;
1975
1976       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1977         gtk_widget_get_preferred_width (child, &child_min, &child_nat);
1978       else
1979         gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1980
1981       minimum += child_min;
1982       natural += child_nat;
1983     }
1984
1985   if (minimum_size)
1986     *minimum_size = minimum;
1987
1988   if (natural_size)
1989     *natural_size = natural;
1990 }
1991
1992 static void 
1993 gtk_button_get_preferred_width (GtkWidget *widget,
1994                                 gint      *minimum_size,
1995                                 gint      *natural_size)
1996 {
1997   gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1998 }
1999
2000 static void 
2001 gtk_button_get_preferred_height (GtkWidget *widget,
2002                                  gint      *minimum_size,
2003                                  gint      *natural_size)
2004 {
2005   gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2006 }
2007
2008 /**
2009  * gtk_button_set_label:
2010  * @button: a #GtkButton
2011  * @label: a string
2012  *
2013  * Sets the text of the label of the button to @str. This text is
2014  * also used to select the stock item if gtk_button_set_use_stock()
2015  * is used.
2016  *
2017  * This will also clear any previously set labels.
2018  **/
2019 void
2020 gtk_button_set_label (GtkButton   *button,
2021                       const gchar *label)
2022 {
2023   GtkButtonPrivate *priv;
2024   gchar *new_label;
2025
2026   g_return_if_fail (GTK_IS_BUTTON (button));
2027
2028   priv = button->priv;
2029
2030   new_label = g_strdup (label);
2031   g_free (priv->label_text);
2032   priv->label_text = new_label;
2033
2034   gtk_button_construct_child (button);
2035   
2036   g_object_notify (G_OBJECT (button), "label");
2037 }
2038
2039 /**
2040  * gtk_button_get_label:
2041  * @button: a #GtkButton
2042  *
2043  * Fetches the text from the label of the button, as set by
2044  * gtk_button_set_label(). If the label text has not 
2045  * been set the return value will be %NULL. This will be the 
2046  * case if you create an empty button with gtk_button_new() to 
2047  * use as a container.
2048  *
2049  * Return value: The text of the label widget. This string is owned
2050  * by the widget and must not be modified or freed.
2051  **/
2052 const gchar *
2053 gtk_button_get_label (GtkButton *button)
2054 {
2055   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2056
2057   return button->priv->label_text;
2058 }
2059
2060 /**
2061  * gtk_button_set_use_underline:
2062  * @button: a #GtkButton
2063  * @use_underline: %TRUE if underlines in the text indicate mnemonics
2064  *
2065  * If true, an underline in the text of the button label indicates
2066  * the next character should be used for the mnemonic accelerator key.
2067  */
2068 void
2069 gtk_button_set_use_underline (GtkButton *button,
2070                               gboolean   use_underline)
2071 {
2072   GtkButtonPrivate *priv;
2073
2074   g_return_if_fail (GTK_IS_BUTTON (button));
2075
2076   priv = button->priv;
2077
2078   use_underline = use_underline != FALSE;
2079
2080   if (use_underline != priv->use_underline)
2081     {
2082       priv->use_underline = use_underline;
2083
2084       gtk_button_construct_child (button);
2085       
2086       g_object_notify (G_OBJECT (button), "use-underline");
2087     }
2088 }
2089
2090 /**
2091  * gtk_button_get_use_underline:
2092  * @button: a #GtkButton
2093  *
2094  * Returns whether an embedded underline in the button label indicates a
2095  * mnemonic. See gtk_button_set_use_underline ().
2096  *
2097  * Return value: %TRUE if an embedded underline in the button label
2098  *               indicates the mnemonic accelerator keys.
2099  **/
2100 gboolean
2101 gtk_button_get_use_underline (GtkButton *button)
2102 {
2103   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2104
2105   return button->priv->use_underline;
2106 }
2107
2108 /**
2109  * gtk_button_set_use_stock:
2110  * @button: a #GtkButton
2111  * @use_stock: %TRUE if the button should use a stock item
2112  *
2113  * If %TRUE, the label set on the button is used as a
2114  * stock id to select the stock item for the button.
2115  */
2116 void
2117 gtk_button_set_use_stock (GtkButton *button,
2118                           gboolean   use_stock)
2119 {
2120   GtkButtonPrivate *priv;
2121
2122   g_return_if_fail (GTK_IS_BUTTON (button));
2123
2124   priv = button->priv;
2125
2126   use_stock = use_stock != FALSE;
2127
2128   if (use_stock != priv->use_stock)
2129     {
2130       priv->use_stock = use_stock;
2131
2132       gtk_button_construct_child (button);
2133       
2134       g_object_notify (G_OBJECT (button), "use-stock");
2135     }
2136 }
2137
2138 /**
2139  * gtk_button_get_use_stock:
2140  * @button: a #GtkButton
2141  *
2142  * Returns whether the button label is a stock item.
2143  *
2144  * Return value: %TRUE if the button label is used to
2145  *               select a stock item instead of being
2146  *               used directly as the label text.
2147  */
2148 gboolean
2149 gtk_button_get_use_stock (GtkButton *button)
2150 {
2151   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2152
2153   return button->priv->use_stock;
2154 }
2155
2156 /**
2157  * gtk_button_set_focus_on_click:
2158  * @button: a #GtkButton
2159  * @focus_on_click: whether the button grabs focus when clicked with the mouse
2160  *
2161  * Sets whether the button will grab focus when it is clicked with the mouse.
2162  * Making mouse clicks not grab focus is useful in places like toolbars where
2163  * you don't want the keyboard focus removed from the main area of the
2164  * application.
2165  *
2166  * Since: 2.4
2167  **/
2168 void
2169 gtk_button_set_focus_on_click (GtkButton *button,
2170                                gboolean   focus_on_click)
2171 {
2172   GtkButtonPrivate *priv;
2173
2174   g_return_if_fail (GTK_IS_BUTTON (button));
2175
2176   priv = button->priv;
2177
2178   focus_on_click = focus_on_click != FALSE;
2179
2180   if (priv->focus_on_click != focus_on_click)
2181     {
2182       priv->focus_on_click = focus_on_click;
2183       
2184       g_object_notify (G_OBJECT (button), "focus-on-click");
2185     }
2186 }
2187
2188 /**
2189  * gtk_button_get_focus_on_click:
2190  * @button: a #GtkButton
2191  *
2192  * Returns whether the button grabs focus when it is clicked with the mouse.
2193  * See gtk_button_set_focus_on_click().
2194  *
2195  * Return value: %TRUE if the button grabs focus when it is clicked with
2196  *               the mouse.
2197  *
2198  * Since: 2.4
2199  **/
2200 gboolean
2201 gtk_button_get_focus_on_click (GtkButton *button)
2202 {
2203   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2204   
2205   return button->priv->focus_on_click;
2206 }
2207
2208 /**
2209  * gtk_button_set_alignment:
2210  * @button: a #GtkButton
2211  * @xalign: the horizontal position of the child, 0.0 is left aligned, 
2212  *   1.0 is right aligned
2213  * @yalign: the vertical position of the child, 0.0 is top aligned, 
2214  *   1.0 is bottom aligned
2215  *
2216  * Sets the alignment of the child. This property has no effect unless 
2217  * the child is a #GtkMisc or a #GtkAlignment.
2218  *
2219  * Since: 2.4
2220  */
2221 void
2222 gtk_button_set_alignment (GtkButton *button,
2223                           gfloat     xalign,
2224                           gfloat     yalign)
2225 {
2226   GtkButtonPrivate *priv;
2227
2228   g_return_if_fail (GTK_IS_BUTTON (button));
2229   
2230   priv = button->priv;
2231
2232   priv->xalign = xalign;
2233   priv->yalign = yalign;
2234   priv->align_set = 1;
2235
2236   maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2237
2238   g_object_freeze_notify (G_OBJECT (button));
2239   g_object_notify (G_OBJECT (button), "xalign");
2240   g_object_notify (G_OBJECT (button), "yalign");
2241   g_object_thaw_notify (G_OBJECT (button));
2242 }
2243
2244 /**
2245  * gtk_button_get_alignment:
2246  * @button: a #GtkButton
2247  * @xalign: (out): return location for horizontal alignment
2248  * @yalign: (out): return location for vertical alignment
2249  *
2250  * Gets the alignment of the child in the button.
2251  *
2252  * Since: 2.4
2253  */
2254 void
2255 gtk_button_get_alignment (GtkButton *button,
2256                           gfloat    *xalign,
2257                           gfloat    *yalign)
2258 {
2259   GtkButtonPrivate *priv;
2260
2261   g_return_if_fail (GTK_IS_BUTTON (button));
2262   
2263   priv = button->priv;
2264  
2265   if (xalign) 
2266     *xalign = priv->xalign;
2267
2268   if (yalign)
2269     *yalign = priv->yalign;
2270 }
2271
2272 /**
2273  * _gtk_button_set_depressed:
2274  * @button: a #GtkButton
2275  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2276  *
2277  * Sets whether the button is currently drawn as down or not. This is 
2278  * purely a visual setting, and is meant only for use by derived widgets
2279  * such as #GtkToggleButton.
2280  **/
2281 void
2282 _gtk_button_set_depressed (GtkButton *button,
2283                            gboolean   depressed)
2284 {
2285   GtkWidget *widget = GTK_WIDGET (button);
2286   GtkButtonPrivate *priv = button->priv;
2287
2288   depressed = depressed != FALSE;
2289
2290   if (depressed != priv->depressed)
2291     {
2292       priv->depressed = depressed;
2293       gtk_widget_queue_resize (widget);
2294     }
2295 }
2296
2297 static void
2298 gtk_button_update_state (GtkButton *button)
2299 {
2300   GtkButtonPrivate *priv = button->priv;
2301   GtkStateFlags new_state;
2302   gboolean depressed;
2303
2304   if (priv->activate_timeout)
2305     depressed = priv->depress_on_activate;
2306   else
2307     depressed = priv->in_button && priv->button_down;
2308
2309   new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) &
2310     ~(GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_ACTIVE);
2311
2312   if (priv->in_button)
2313     new_state |= GTK_STATE_FLAG_PRELIGHT;
2314
2315   if (priv->button_down || depressed)
2316     new_state |= GTK_STATE_FLAG_ACTIVE;
2317
2318   _gtk_button_set_depressed (button, depressed);
2319   gtk_widget_set_state_flags (GTK_WIDGET (button), new_state, TRUE);
2320 }
2321
2322 static void 
2323 show_image_change_notify (GtkButton *button)
2324 {
2325   GtkButtonPrivate *priv = button->priv;
2326
2327   if (priv->image) 
2328     {
2329       if (show_image (button))
2330         gtk_widget_show (priv->image);
2331       else
2332         gtk_widget_hide (priv->image);
2333     }
2334 }
2335
2336 static void
2337 traverse_container (GtkWidget *widget,
2338                     gpointer   data)
2339 {
2340   if (GTK_IS_BUTTON (widget))
2341     show_image_change_notify (GTK_BUTTON (widget));
2342   else if (GTK_IS_CONTAINER (widget))
2343     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2344 }
2345
2346 static void
2347 gtk_button_setting_changed (GtkSettings *settings)
2348 {
2349   GList *list, *l;
2350
2351   list = gtk_window_list_toplevels ();
2352
2353   for (l = list; l; l = l->next)
2354     gtk_container_forall (GTK_CONTAINER (l->data), 
2355                           traverse_container, NULL);
2356
2357   g_list_free (list);
2358 }
2359
2360
2361 static void
2362 gtk_button_screen_changed (GtkWidget *widget,
2363                            GdkScreen *previous_screen)
2364 {
2365   GtkButton *button;
2366   GtkButtonPrivate *priv;
2367   GtkSettings *settings;
2368   gulong show_image_connection;
2369
2370   if (!gtk_widget_has_screen (widget))
2371     return;
2372
2373   button = GTK_BUTTON (widget);
2374   priv = button->priv;
2375
2376   /* If the button is being pressed while the screen changes the
2377     release might never occur, so we reset the state. */
2378   if (priv->button_down)
2379     {
2380       priv->button_down = FALSE;
2381       gtk_button_update_state (button);
2382     }
2383
2384   settings = gtk_widget_get_settings (widget);
2385
2386   show_image_connection = 
2387     g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
2388                            NULL, gtk_button_setting_changed, NULL);
2389   
2390   if (show_image_connection)
2391     return;
2392
2393   g_signal_connect (settings, "notify::gtk-button-images",
2394                     G_CALLBACK (gtk_button_setting_changed), NULL);
2395
2396   show_image_change_notify (button);
2397 }
2398
2399 static void
2400 gtk_button_state_changed (GtkWidget    *widget,
2401                           GtkStateType  previous_state)
2402 {
2403   GtkButton *button = GTK_BUTTON (widget);
2404   GtkButtonPrivate *priv = button->priv;
2405
2406   if (!gtk_widget_is_sensitive (widget))
2407     {
2408       priv->in_button = FALSE;
2409       gtk_real_button_released (button);
2410     }
2411 }
2412
2413 static void
2414 gtk_button_grab_notify (GtkWidget *widget,
2415                         gboolean   was_grabbed)
2416 {
2417   GtkButton *button = GTK_BUTTON (widget);
2418   GtkButtonPrivate *priv = button->priv;
2419   gboolean save_in;
2420
2421   if (priv->activate_timeout &&
2422       priv->grab_keyboard &&
2423       gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2424     gtk_button_finish_activate (button, FALSE);
2425
2426   if (!was_grabbed)
2427     {
2428       save_in = priv->in_button;
2429       priv->in_button = FALSE;
2430       gtk_real_button_released (button);
2431       if (save_in != priv->in_button)
2432         {
2433           priv->in_button = save_in;
2434           gtk_button_update_state (button);
2435         }
2436     }
2437 }
2438
2439 /**
2440  * gtk_button_set_image:
2441  * @button: a #GtkButton
2442  * @image: a widget to set as the image for the button
2443  *
2444  * Set the image of @button to the given widget. Note that
2445  * it depends on the #GtkSettings:gtk-button-images setting whether the
2446  * image will be displayed or not, you don't have to call
2447  * gtk_widget_show() on @image yourself.
2448  *
2449  * Since: 2.6
2450  */ 
2451 void
2452 gtk_button_set_image (GtkButton *button,
2453                       GtkWidget *image)
2454 {
2455   GtkButtonPrivate *priv;
2456   GtkWidget *parent;
2457
2458   g_return_if_fail (GTK_IS_BUTTON (button));
2459   g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2460
2461   priv = button->priv;
2462
2463   if (priv->image)
2464     {
2465       parent = gtk_widget_get_parent (priv->image);
2466       if (parent)
2467         gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2468     }
2469
2470   priv->image = image;
2471   priv->image_is_stock = (image == NULL);
2472
2473   gtk_button_construct_child (button);
2474
2475   g_object_notify (G_OBJECT (button), "image");
2476 }
2477
2478 /**
2479  * gtk_button_get_image:
2480  * @button: a #GtkButton
2481  *
2482  * Gets the widget that is currenty set as the image of @button.
2483  * This may have been explicitly set by gtk_button_set_image()
2484  * or constructed by gtk_button_new_from_stock().
2485  *
2486  * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2487  *
2488  * Since: 2.6
2489  */
2490 GtkWidget *
2491 gtk_button_get_image (GtkButton *button)
2492 {
2493   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2494   
2495   return button->priv->image;
2496 }
2497
2498 /**
2499  * gtk_button_set_image_position:
2500  * @button: a #GtkButton
2501  * @position: the position
2502  *
2503  * Sets the position of the image relative to the text 
2504  * inside the button.
2505  *
2506  * Since: 2.10
2507  */ 
2508 void
2509 gtk_button_set_image_position (GtkButton       *button,
2510                                GtkPositionType  position)
2511 {
2512   GtkButtonPrivate *priv;
2513
2514   g_return_if_fail (GTK_IS_BUTTON (button));
2515   g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2516   
2517   priv = button->priv;
2518
2519   if (priv->image_position != position)
2520     {
2521       priv->image_position = position;
2522
2523       gtk_button_construct_child (button);
2524
2525       g_object_notify (G_OBJECT (button), "image-position");
2526     }
2527 }
2528
2529 /**
2530  * gtk_button_get_image_position:
2531  * @button: a #GtkButton
2532  *
2533  * Gets the position of the image relative to the text 
2534  * inside the button.
2535  *
2536  * Return value: the position
2537  *
2538  * Since: 2.10
2539  */
2540 GtkPositionType
2541 gtk_button_get_image_position (GtkButton *button)
2542 {
2543   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2544   
2545   return button->priv->image_position;
2546 }
2547
2548
2549 /**
2550  * gtk_button_get_event_window:
2551  * @button: a #GtkButton
2552  *
2553  * Returns the button's event window if it is realized, %NULL otherwise.
2554  * This function should be rarely needed.
2555  *
2556  * Return value: (transfer none): @button's event window.
2557  *
2558  * Since: 2.22
2559  */
2560 GdkWindow*
2561 gtk_button_get_event_window (GtkButton *button)
2562 {
2563   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2564
2565   return button->priv->event_window;
2566 }