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