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