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