]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
Move GtkSizeRequest into GtkWidget
[~andy/gtk] / gtk / gtkbutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /**
28  * SECTION:gtkbutton
29  * @Short_description: A widget that creates a signal when clicked on
30  * @Title: GtkButton
31  *
32  * The #GtkButton widget is generally used to attach a function to that is
33  * called when the button is pressed.  The various signals and how to use them
34  * are outlined below.
35  *
36  * The #GtkButton widget can hold any valid child widget.  That is it can hold
37  * most any other standard #GtkWidget.  The most commonly used child is the
38  * #GtkLabel.
39  */
40
41 #include "config.h"
42 #include <string.h>
43 #include "gtkalignment.h"
44 #include "gtkbutton.h"
45 #include "gtklabel.h"
46 #include "gtkmain.h"
47 #include "gtkmarshalers.h"
48 #include "gtkimage.h"
49 #include "gtkhbox.h"
50 #include "gtkvbox.h"
51 #include "gtkstock.h"
52 #include "gtkiconfactory.h"
53 #include "gtkactivatable.h"
54 #include "gtksizerequest.h"
55 #include "gtkprivate.h"
56 #include "gtkintl.h"
57
58
59 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_draw (GtkWidget * widget, cairo_t *cr);
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_get_preferred_width       (GtkWidget           *widget,
176                                                   gint                *minimum_size,
177                                                   gint                *natural_size);
178 static void gtk_button_get_preferred_height      (GtkWidget           *widget,
179                                                   gint                *minimum_size,
180                                                   gint                *natural_size);
181   
182 static guint button_signals[LAST_SIGNAL] = { 0 };
183
184 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
185                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
186                                                 gtk_button_activatable_interface_init))
187
188 static void
189 gtk_button_class_init (GtkButtonClass *klass)
190 {
191   GObjectClass *gobject_class;
192   GtkObjectClass *object_class;
193   GtkWidgetClass *widget_class;
194   GtkContainerClass *container_class;
195
196   gobject_class = G_OBJECT_CLASS (klass);
197   object_class = (GtkObjectClass*) klass;
198   widget_class = (GtkWidgetClass*) klass;
199   container_class = (GtkContainerClass*) klass;
200   
201   gobject_class->constructor  = gtk_button_constructor;
202   gobject_class->dispose      = gtk_button_dispose;
203   gobject_class->set_property = gtk_button_set_property;
204   gobject_class->get_property = gtk_button_get_property;
205
206   object_class->destroy = gtk_button_destroy;
207
208   widget_class->get_preferred_width  = gtk_button_get_preferred_width;
209   widget_class->get_preferred_height = gtk_button_get_preferred_height;
210   widget_class->screen_changed = gtk_button_screen_changed;
211   widget_class->realize = gtk_button_realize;
212   widget_class->unrealize = gtk_button_unrealize;
213   widget_class->map = gtk_button_map;
214   widget_class->unmap = gtk_button_unmap;
215   widget_class->style_set = gtk_button_style_set;
216   widget_class->size_allocate = gtk_button_size_allocate;
217   widget_class->draw = gtk_button_draw;
218   widget_class->button_press_event = gtk_button_button_press;
219   widget_class->button_release_event = gtk_button_button_release;
220   widget_class->grab_broken_event = gtk_button_grab_broken;
221   widget_class->key_release_event = gtk_button_key_release;
222   widget_class->enter_notify_event = gtk_button_enter_notify;
223   widget_class->leave_notify_event = gtk_button_leave_notify;
224   widget_class->state_changed = gtk_button_state_changed;
225   widget_class->grab_notify = gtk_button_grab_notify;
226
227   container_class->child_type = gtk_button_child_type;
228   container_class->add = gtk_button_add;
229   gtk_container_class_handle_border_width (container_class);
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_get_child (GTK_BIN (container)))
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       gfloat xscale, yscale;
651
652       g_object_get (alignment,
653                     "xscale", &xscale,
654                     "yscale", &yscale,
655                     NULL);
656
657       if (priv->align_set)
658         gtk_alignment_set (alignment,
659                            priv->xalign, priv->yalign,
660                            xscale, yscale);
661     }
662 }
663
664 static void
665 gtk_button_add (GtkContainer *container,
666                 GtkWidget    *widget)
667 {
668   maybe_set_alignment (GTK_BUTTON (container), widget);
669
670   GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
671 }
672
673 static void 
674 gtk_button_dispose (GObject *object)
675 {
676   GtkButton *button = GTK_BUTTON (object);
677   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
678
679   if (priv->action)
680     {
681       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
682       priv->action = NULL;
683     }
684   G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
685 }
686
687 static void
688 gtk_button_set_property (GObject         *object,
689                          guint            prop_id,
690                          const GValue    *value,
691                          GParamSpec      *pspec)
692 {
693   GtkButton *button = GTK_BUTTON (object);
694   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
695
696   switch (prop_id)
697     {
698     case PROP_LABEL:
699       gtk_button_set_label (button, g_value_get_string (value));
700       break;
701     case PROP_IMAGE:
702       gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
703       break;
704     case PROP_RELIEF:
705       gtk_button_set_relief (button, g_value_get_enum (value));
706       break;
707     case PROP_USE_UNDERLINE:
708       gtk_button_set_use_underline (button, g_value_get_boolean (value));
709       break;
710     case PROP_USE_STOCK:
711       gtk_button_set_use_stock (button, g_value_get_boolean (value));
712       break;
713     case PROP_FOCUS_ON_CLICK:
714       gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
715       break;
716     case PROP_XALIGN:
717       gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
718       break;
719     case PROP_YALIGN:
720       gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
721       break;
722     case PROP_IMAGE_POSITION:
723       gtk_button_set_image_position (button, g_value_get_enum (value));
724       break;
725     case PROP_ACTIVATABLE_RELATED_ACTION:
726       gtk_button_set_related_action (button, g_value_get_object (value));
727       break;
728     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
729       gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
730       break;
731     default:
732       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
733       break;
734     }
735 }
736
737 static void
738 gtk_button_get_property (GObject         *object,
739                          guint            prop_id,
740                          GValue          *value,
741                          GParamSpec      *pspec)
742 {
743   GtkButton *button = GTK_BUTTON (object);
744   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
745
746   switch (prop_id)
747     {
748     case PROP_LABEL:
749       g_value_set_string (value, button->label_text);
750       break;
751     case PROP_IMAGE:
752       g_value_set_object (value, (GObject *)priv->image);
753       break;
754     case PROP_RELIEF:
755       g_value_set_enum (value, gtk_button_get_relief (button));
756       break;
757     case PROP_USE_UNDERLINE:
758       g_value_set_boolean (value, button->use_underline);
759       break;
760     case PROP_USE_STOCK:
761       g_value_set_boolean (value, button->use_stock);
762       break;
763     case PROP_FOCUS_ON_CLICK:
764       g_value_set_boolean (value, button->focus_on_click);
765       break;
766     case PROP_XALIGN:
767       g_value_set_float (value, priv->xalign);
768       break;
769     case PROP_YALIGN:
770       g_value_set_float (value, priv->yalign);
771       break;
772     case PROP_IMAGE_POSITION:
773       g_value_set_enum (value, priv->image_position);
774       break;
775     case PROP_ACTIVATABLE_RELATED_ACTION:
776       g_value_set_object (value, priv->action);
777       break;
778     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
779       g_value_set_boolean (value, priv->use_action_appearance);
780       break;
781     default:
782       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
783       break;
784     }
785 }
786
787 static void 
788 gtk_button_activatable_interface_init (GtkActivatableIface  *iface)
789 {
790   iface->update = gtk_button_update;
791   iface->sync_action_properties = gtk_button_sync_action_properties;
792 }
793
794 static void
795 activatable_update_stock_id (GtkButton *button,
796                              GtkAction *action)
797 {
798   if (!gtk_button_get_use_stock (button))
799     return;
800
801   gtk_button_set_label (button, gtk_action_get_stock_id (action));
802 }
803
804 static void
805 activatable_update_short_label (GtkButton *button,
806                                 GtkAction *action)
807 {
808   GtkWidget *child;
809   GtkWidget *image;
810
811   if (gtk_button_get_use_stock (button))
812     return;
813
814   image = gtk_button_get_image (button);
815
816   /* Dont touch custom child... */
817   child = gtk_bin_get_child (GTK_BIN (button));
818   if (GTK_IS_IMAGE (image) ||
819       child == NULL ||
820       GTK_IS_LABEL (child))
821     {
822       gtk_button_set_label (button, gtk_action_get_short_label (action));
823       gtk_button_set_use_underline (button, TRUE);
824     }
825 }
826
827 static void
828 activatable_update_icon_name (GtkButton *button,
829                               GtkAction *action)
830 {
831   GtkWidget *image;
832               
833   if (gtk_button_get_use_stock (button))
834     return;
835
836   image = gtk_button_get_image (button);
837
838   if (GTK_IS_IMAGE (image) &&
839       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
840        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
841     gtk_image_set_from_icon_name (GTK_IMAGE (image),
842                                   gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
843 }
844
845 static void
846 activatable_update_gicon (GtkButton *button,
847                           GtkAction *action)
848 {
849   GtkWidget *image = gtk_button_get_image (button);
850   GIcon *icon = gtk_action_get_gicon (action);
851   
852   if (GTK_IS_IMAGE (image) &&
853       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
854        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
855     gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
856 }
857
858 static void 
859 gtk_button_update (GtkActivatable *activatable,
860                    GtkAction      *action,
861                    const gchar    *property_name)
862 {
863   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
864
865   if (strcmp (property_name, "visible") == 0)
866     {
867       if (gtk_action_is_visible (action))
868         gtk_widget_show (GTK_WIDGET (activatable));
869       else
870         gtk_widget_hide (GTK_WIDGET (activatable));
871     }
872   else if (strcmp (property_name, "sensitive") == 0)
873     gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
874
875   if (!priv->use_action_appearance)
876     return;
877
878   if (strcmp (property_name, "stock-id") == 0)
879     activatable_update_stock_id (GTK_BUTTON (activatable), action);
880   else if (strcmp (property_name, "gicon") == 0)
881     activatable_update_gicon (GTK_BUTTON (activatable), action);
882   else if (strcmp (property_name, "short-label") == 0)
883     activatable_update_short_label (GTK_BUTTON (activatable), action);
884   else if (strcmp (property_name, "icon-name") == 0)
885     activatable_update_icon_name (GTK_BUTTON (activatable), action);
886 }
887
888 static void
889 gtk_button_sync_action_properties (GtkActivatable *activatable,
890                                    GtkAction      *action)
891 {
892   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
893
894   if (!action)
895     return;
896
897   if (gtk_action_is_visible (action))
898     gtk_widget_show (GTK_WIDGET (activatable));
899   else
900     gtk_widget_hide (GTK_WIDGET (activatable));
901   
902   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
903   
904   if (priv->use_action_appearance)
905     {
906       activatable_update_stock_id (GTK_BUTTON (activatable), action);
907       activatable_update_short_label (GTK_BUTTON (activatable), action);
908       activatable_update_gicon (GTK_BUTTON (activatable), action);
909       activatable_update_icon_name (GTK_BUTTON (activatable), action);
910     }
911 }
912
913 static void
914 gtk_button_set_related_action (GtkButton *button,
915                                GtkAction *action)
916 {
917   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
918
919   if (priv->action == action)
920     return;
921
922   /* This should be a default handler, but for compatibility reasons
923    * we need to support derived classes that don't chain up their
924    * clicked handler.
925    */
926   g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
927   if (action)
928     g_signal_connect_after (button, "clicked",
929                             G_CALLBACK (gtk_real_button_clicked), NULL);
930
931   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
932
933   priv->action = action;
934 }
935
936 static void
937 gtk_button_set_use_action_appearance (GtkButton *button,
938                                       gboolean   use_appearance)
939 {
940   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
941
942   if (priv->use_action_appearance != use_appearance)
943     {
944       priv->use_action_appearance = use_appearance;
945
946       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
947     }
948 }
949
950 /**
951  * gtk_button_new:
952  *
953  * Creates a new #GtkButton widget. To add a child widget to the button,
954  * use gtk_container_add().
955  *
956  * Returns: The newly created #GtkButton widget.
957  */
958 GtkWidget*
959 gtk_button_new (void)
960 {
961   return g_object_new (GTK_TYPE_BUTTON, NULL);
962 }
963
964 static gboolean
965 show_image (GtkButton *button)
966 {
967   gboolean show;
968   
969   if (button->label_text)
970     {
971       GtkSettings *settings;
972
973       settings = gtk_widget_get_settings (GTK_WIDGET (button));        
974       g_object_get (settings, "gtk-button-images", &show, NULL);
975     }
976   else
977     show = TRUE;
978
979   return show;
980 }
981
982 static void
983 gtk_button_construct_child (GtkButton *button)
984 {
985   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
986   GtkStockItem item;
987   GtkWidget *child;
988   GtkWidget *label;
989   GtkWidget *box;
990   GtkWidget *align;
991   GtkWidget *image = NULL;
992   gchar *label_text = NULL;
993   gint image_spacing;
994
995   if (!button->constructed)
996     return;
997
998   if (!button->label_text && !priv->image)
999     return;
1000
1001   gtk_widget_style_get (GTK_WIDGET (button),
1002                         "image-spacing", &image_spacing,
1003                         NULL);
1004
1005   if (priv->image && !priv->image_is_stock)
1006     {
1007       GtkWidget *parent;
1008
1009       image = g_object_ref (priv->image);
1010
1011       parent = gtk_widget_get_parent (image);
1012       if (parent)
1013         gtk_container_remove (GTK_CONTAINER (parent), image);
1014     }
1015
1016   priv->image = NULL;
1017
1018   child = gtk_bin_get_child (GTK_BIN (button));
1019   if (child)
1020     gtk_container_remove (GTK_CONTAINER (button), child);
1021
1022   if (button->use_stock &&
1023       button->label_text &&
1024       gtk_stock_lookup (button->label_text, &item))
1025     {
1026       if (!image)
1027         image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
1028
1029       label_text = item.label;
1030     }
1031   else
1032     label_text = button->label_text;
1033
1034   if (image)
1035     {
1036       priv->image = image;
1037       g_object_set (priv->image,
1038                     "visible", show_image (button),
1039                     "no-show-all", TRUE,
1040                     NULL);
1041
1042       if (priv->image_position == GTK_POS_LEFT ||
1043           priv->image_position == GTK_POS_RIGHT)
1044         box = gtk_hbox_new (FALSE, image_spacing);
1045       else
1046         box = gtk_vbox_new (FALSE, image_spacing);
1047
1048       if (priv->align_set)
1049         align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1050       else
1051         align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1052
1053       if (priv->image_position == GTK_POS_LEFT ||
1054           priv->image_position == GTK_POS_TOP)
1055         gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1056       else
1057         gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1058
1059       if (label_text)
1060         {
1061           if (button->use_underline || button->use_stock)
1062             {
1063               label = gtk_label_new_with_mnemonic (label_text);
1064               gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1065                                              GTK_WIDGET (button));
1066             }
1067           else
1068             label = gtk_label_new (label_text);
1069
1070           if (priv->image_position == GTK_POS_RIGHT ||
1071               priv->image_position == GTK_POS_BOTTOM)
1072             gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1073           else
1074             gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1075         }
1076
1077       gtk_container_add (GTK_CONTAINER (button), align);
1078       gtk_container_add (GTK_CONTAINER (align), box);
1079       gtk_widget_show_all (align);
1080
1081       g_object_unref (image);
1082
1083       return;
1084     }
1085
1086   if (button->use_underline || button->use_stock)
1087     {
1088       label = gtk_label_new_with_mnemonic (button->label_text);
1089       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1090     }
1091   else
1092     label = gtk_label_new (button->label_text);
1093
1094   if (priv->align_set)
1095     gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1096
1097   gtk_widget_show (label);
1098   gtk_container_add (GTK_CONTAINER (button), label);
1099 }
1100
1101
1102 /**
1103  * gtk_button_new_with_label:
1104  * @label: The text you want the #GtkLabel to hold.
1105  *
1106  * Creates a #GtkButton widget with a #GtkLabel child containing the given
1107  * text.
1108  *
1109  * Returns: The newly created #GtkButton widget.
1110  */
1111 GtkWidget*
1112 gtk_button_new_with_label (const gchar *label)
1113 {
1114   return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1115 }
1116
1117 /**
1118  * gtk_button_new_from_stock:
1119  * @stock_id: the name of the stock item 
1120  *
1121  * Creates a new #GtkButton containing the image and text from a stock item.
1122  * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1123  * #GTK_STOCK_APPLY.
1124  *
1125  * If @stock_id is unknown, then it will be treated as a mnemonic
1126  * label (as for gtk_button_new_with_mnemonic()).
1127  *
1128  * Returns: a new #GtkButton
1129  **/
1130 GtkWidget*
1131 gtk_button_new_from_stock (const gchar *stock_id)
1132 {
1133   return g_object_new (GTK_TYPE_BUTTON,
1134                        "label", stock_id,
1135                        "use-stock", TRUE,
1136                        "use-underline", TRUE,
1137                        NULL);
1138 }
1139
1140 /**
1141  * gtk_button_new_with_mnemonic:
1142  * @label: The text of the button, with an underscore in front of the
1143  *         mnemonic character
1144  * @returns: a new #GtkButton
1145  *
1146  * Creates a new #GtkButton containing a label.
1147  * If characters in @label are preceded by an underscore, they are underlined.
1148  * If you need a literal underscore character in a label, use '__' (two 
1149  * underscores). The first underlined character represents a keyboard 
1150  * accelerator called a mnemonic.
1151  * Pressing Alt and that key activates the button.
1152  **/
1153 GtkWidget*
1154 gtk_button_new_with_mnemonic (const gchar *label)
1155 {
1156   return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE,  NULL);
1157 }
1158
1159 /**
1160  * gtk_button_pressed:
1161  * @button: The #GtkButton you want to send the signal to.
1162  *
1163  * Emits a #GtkButton::pressed signal to the given #GtkButton.
1164  *
1165  * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1166  */
1167 void
1168 gtk_button_pressed (GtkButton *button)
1169 {
1170   g_return_if_fail (GTK_IS_BUTTON (button));
1171
1172   
1173   g_signal_emit (button, button_signals[PRESSED], 0);
1174 }
1175
1176 /**
1177  * gtk_button_released:
1178  * @button: The #GtkButton you want to send the signal to.
1179  *
1180  * Emits a #GtkButton::released signal to the given #GtkButton.
1181  *
1182  * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1183  */
1184 void
1185 gtk_button_released (GtkButton *button)
1186 {
1187   g_return_if_fail (GTK_IS_BUTTON (button));
1188
1189   g_signal_emit (button, button_signals[RELEASED], 0);
1190 }
1191
1192 /**
1193  * gtk_button_clicked:
1194  * @button: The #GtkButton you want to send the signal to.
1195  *
1196  * Emits a #GtkButton::clicked signal to the given #GtkButton.
1197  */
1198 void
1199 gtk_button_clicked (GtkButton *button)
1200 {
1201   g_return_if_fail (GTK_IS_BUTTON (button));
1202
1203   g_signal_emit (button, button_signals[CLICKED], 0);
1204 }
1205
1206 /**
1207  * gtk_button_enter:
1208  * @button: The #GtkButton you want to send the signal to.
1209  *
1210  * Emits a #GtkButton::enter signal to the given #GtkButton.
1211  *
1212  * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1213  */
1214 void
1215 gtk_button_enter (GtkButton *button)
1216 {
1217   g_return_if_fail (GTK_IS_BUTTON (button));
1218
1219   g_signal_emit (button, button_signals[ENTER], 0);
1220 }
1221
1222 /**
1223  * gtk_button_leave:
1224  * @button: The #GtkButton you want to send the signal to.
1225  *
1226  * Emits a #GtkButton::leave signal to the given #GtkButton.
1227  *
1228  * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1229  */
1230 void
1231 gtk_button_leave (GtkButton *button)
1232 {
1233   g_return_if_fail (GTK_IS_BUTTON (button));
1234
1235   g_signal_emit (button, button_signals[LEAVE], 0);
1236 }
1237
1238 /**
1239  * gtk_button_set_relief:
1240  * @button: The #GtkButton you want to set relief styles of.
1241  * @newstyle: The GtkReliefStyle as described above.
1242  *
1243  * Sets the relief style of the edges of the given #GtkButton widget.
1244  * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1245  * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1246  *
1247  * <!-- FIXME: put pictures of each style -->
1248  */
1249 void
1250 gtk_button_set_relief (GtkButton *button,
1251                        GtkReliefStyle newrelief)
1252 {
1253   g_return_if_fail (GTK_IS_BUTTON (button));
1254
1255   if (newrelief != button->relief) 
1256     {
1257        button->relief = newrelief;
1258        g_object_notify (G_OBJECT (button), "relief");
1259        gtk_widget_queue_draw (GTK_WIDGET (button));
1260     }
1261 }
1262
1263 /**
1264  * gtk_button_get_relief:
1265  * @button: The #GtkButton you want the #GtkReliefStyle from.
1266  *
1267  * Returns the current relief style of the given #GtkButton.
1268  *
1269  * Returns: The current #GtkReliefStyle
1270  */
1271 GtkReliefStyle
1272 gtk_button_get_relief (GtkButton *button)
1273 {
1274   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1275
1276   return button->relief;
1277 }
1278
1279 static void
1280 gtk_button_realize (GtkWidget *widget)
1281 {
1282   GtkButton *button = GTK_BUTTON (widget);
1283   GtkAllocation allocation;
1284   GdkWindow *window;
1285   GdkWindowAttr attributes;
1286   gint attributes_mask;
1287
1288   gtk_widget_get_allocation (widget, &allocation);
1289
1290   gtk_widget_set_realized (widget, TRUE);
1291
1292   attributes.window_type = GDK_WINDOW_CHILD;
1293   attributes.x = allocation.x;
1294   attributes.y = allocation.y;
1295   attributes.width = allocation.width;
1296   attributes.height = allocation.height;
1297   attributes.wclass = GDK_INPUT_ONLY;
1298   attributes.event_mask = gtk_widget_get_events (widget);
1299   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1300                             GDK_BUTTON_RELEASE_MASK |
1301                             GDK_ENTER_NOTIFY_MASK |
1302                             GDK_LEAVE_NOTIFY_MASK);
1303
1304   attributes_mask = GDK_WA_X | GDK_WA_Y;
1305
1306   window = gtk_widget_get_parent_window (widget);
1307   gtk_widget_set_window (widget, window);
1308   g_object_ref (window);
1309
1310   button->event_window = gdk_window_new (window,
1311                                          &attributes, attributes_mask);
1312   gdk_window_set_user_data (button->event_window, button);
1313
1314   gtk_widget_style_attach (widget);
1315 }
1316
1317 static void
1318 gtk_button_unrealize (GtkWidget *widget)
1319 {
1320   GtkButton *button = GTK_BUTTON (widget);
1321
1322   if (button->activate_timeout)
1323     gtk_button_finish_activate (button, FALSE);
1324
1325   if (button->event_window)
1326     {
1327       gdk_window_set_user_data (button->event_window, NULL);
1328       gdk_window_destroy (button->event_window);
1329       button->event_window = NULL;
1330     }
1331   
1332   GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1333 }
1334
1335 static void
1336 gtk_button_map (GtkWidget *widget)
1337 {
1338   GtkButton *button = GTK_BUTTON (widget);
1339   
1340   GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1341
1342   if (button->event_window)
1343     gdk_window_show (button->event_window);
1344 }
1345
1346 static void
1347 gtk_button_unmap (GtkWidget *widget)
1348 {
1349   GtkButton *button = GTK_BUTTON (widget);
1350     
1351   if (button->event_window)
1352     gdk_window_hide (button->event_window);
1353
1354   GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1355 }
1356
1357 static void
1358 gtk_button_update_image_spacing (GtkButton *button)
1359 {
1360   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1361   GtkWidget *child; 
1362   gint spacing;
1363
1364   /* Keep in sync with gtk_button_construct_child,
1365    * we only want to update the spacing if the box 
1366    * was constructed there.
1367    */
1368   if (!button->constructed || !priv->image)
1369     return;
1370
1371   child = gtk_bin_get_child (GTK_BIN (button));
1372   if (GTK_IS_ALIGNMENT (child))
1373     {
1374       child = gtk_bin_get_child (GTK_BIN (child));
1375       if (GTK_IS_BOX (child))
1376         {
1377           gtk_widget_style_get (GTK_WIDGET (button),
1378                                 "image-spacing", &spacing,
1379                                 NULL);
1380
1381           gtk_box_set_spacing (GTK_BOX (child), spacing);
1382         }
1383     }   
1384 }
1385
1386 static void
1387 gtk_button_style_set (GtkWidget *widget,
1388                       GtkStyle  *prev_style)
1389 {
1390   gtk_button_update_image_spacing (GTK_BUTTON (widget));
1391 }
1392
1393 static void
1394 gtk_button_get_props (GtkButton *button,
1395                       GtkBorder *default_border,
1396                       GtkBorder *default_outside_border,
1397                       GtkBorder *inner_border,
1398                       gboolean  *interior_focus)
1399 {
1400   GtkWidget *widget =  GTK_WIDGET (button);
1401   GtkBorder *tmp_border;
1402
1403   if (default_border)
1404     {
1405       gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1406
1407       if (tmp_border)
1408         {
1409           *default_border = *tmp_border;
1410           gtk_border_free (tmp_border);
1411         }
1412       else
1413         *default_border = default_default_border;
1414     }
1415
1416   if (default_outside_border)
1417     {
1418       gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1419
1420       if (tmp_border)
1421         {
1422           *default_outside_border = *tmp_border;
1423           gtk_border_free (tmp_border);
1424         }
1425       else
1426         *default_outside_border = default_default_outside_border;
1427     }
1428
1429   if (inner_border)
1430     {
1431       gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1432
1433       if (tmp_border)
1434         {
1435           *inner_border = *tmp_border;
1436           gtk_border_free (tmp_border);
1437         }
1438       else
1439         *inner_border = default_inner_border;
1440     }
1441
1442   if (interior_focus)
1443     gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1444 }
1445
1446 static void
1447 gtk_button_size_allocate (GtkWidget     *widget,
1448                           GtkAllocation *allocation)
1449 {
1450   GtkButton *button = GTK_BUTTON (widget);
1451   GtkAllocation child_allocation;
1452   GtkStyle *style;
1453   GtkWidget *child;
1454
1455   gint xthickness, ythickness;
1456   GtkBorder default_border;
1457   GtkBorder inner_border;
1458   gint focus_width;
1459   gint focus_pad;
1460
1461   style = gtk_widget_get_style (widget);
1462   xthickness = style->xthickness;
1463   ythickness = style->ythickness;
1464
1465   gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1466   gtk_widget_style_get (GTK_WIDGET (widget),
1467                         "focus-line-width", &focus_width,
1468                         "focus-padding", &focus_pad,
1469                         NULL);
1470
1471   gtk_widget_set_allocation (widget, allocation);
1472
1473   if (gtk_widget_get_realized (widget))
1474     gdk_window_move_resize (button->event_window,
1475                             allocation->x,
1476                             allocation->y,
1477                             allocation->width,
1478                             allocation->height);
1479
1480   child = gtk_bin_get_child (GTK_BIN (button));
1481   if (child && gtk_widget_get_visible (child))
1482     {
1483       child_allocation.x = allocation->x + inner_border.left + xthickness;
1484       child_allocation.y = allocation->y + inner_border.top + ythickness;
1485
1486       child_allocation.width =
1487         allocation->width -
1488         xthickness * 2 -
1489         inner_border.left -
1490         inner_border.right;
1491
1492       child_allocation.height = 
1493         allocation->height -
1494         ythickness * 2 -
1495         inner_border.top -
1496         inner_border.bottom;
1497
1498       if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1499         {
1500           child_allocation.x += default_border.left;
1501           child_allocation.y += default_border.top;
1502           child_allocation.width =  child_allocation.width - default_border.left - default_border.right;
1503           child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1504         }
1505
1506       if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1507         {
1508           child_allocation.x += focus_width + focus_pad;
1509           child_allocation.y += focus_width + focus_pad;
1510           child_allocation.width =  child_allocation.width - (focus_width + focus_pad) * 2;
1511           child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1512         }
1513
1514       if (button->depressed)
1515         {
1516           gint child_displacement_x;
1517           gint child_displacement_y;
1518           
1519           gtk_widget_style_get (widget,
1520                                 "child-displacement-x", &child_displacement_x, 
1521                                 "child-displacement-y", &child_displacement_y,
1522                                 NULL);
1523           child_allocation.x += child_displacement_x;
1524           child_allocation.y += child_displacement_y;
1525         }
1526
1527       child_allocation.width  = MAX (1, child_allocation.width);
1528       child_allocation.height = MAX (1, child_allocation.height);
1529
1530       gtk_widget_size_allocate (child, &child_allocation);
1531     }
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   GtkWidget *widget;
1545   gint x, y;
1546   GtkBorder default_border;
1547   GtkBorder default_outside_border;
1548   gboolean interior_focus;
1549   gint focus_width;
1550   gint focus_pad;
1551   GtkAllocation allocation;
1552   GdkWindow *window;
1553   GtkStyle *style;
1554
1555   widget = GTK_WIDGET (button);
1556
1557   gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1558   gtk_widget_style_get (widget,
1559                         "focus-line-width", &focus_width,
1560                         "focus-padding", &focus_pad,
1561                         NULL); 
1562
1563   gtk_widget_get_allocation (widget, &allocation);
1564   style = gtk_widget_get_style (widget);
1565   window = gtk_widget_get_window (widget);
1566
1567   x = 0;
1568   y = 0;
1569
1570   if (gtk_widget_has_default (widget) &&
1571       GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1572     {
1573       gtk_paint_box (style, cr,
1574                      GTK_STATE_NORMAL, GTK_SHADOW_IN,
1575                      widget, "buttondefault",
1576                      x, y, width, height);
1577
1578       x += default_border.left;
1579       y += default_border.top;
1580       width -= default_border.left + default_border.right;
1581       height -= default_border.top + default_border.bottom;
1582     }
1583   else if (gtk_widget_get_can_default (widget))
1584     {
1585       x += default_outside_border.left;
1586       y += default_outside_border.top;
1587       width -= default_outside_border.left + default_outside_border.right;
1588       height -= default_outside_border.top + default_outside_border.bottom;
1589     }
1590    
1591   if (!interior_focus && gtk_widget_has_focus (widget))
1592     {
1593       x += focus_width + focus_pad;
1594       y += focus_width + focus_pad;
1595       width -= 2 * (focus_width + focus_pad);
1596       height -= 2 * (focus_width + focus_pad);
1597     }
1598
1599   if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1600       gtk_widget_get_state(widget) == GTK_STATE_PRELIGHT)
1601     gtk_paint_box (style, cr,
1602                    state_type,
1603                    shadow_type, widget, "button",
1604                    x, y, width, height);
1605    
1606   if (gtk_widget_has_focus (widget))
1607     {
1608       gint child_displacement_x;
1609       gint child_displacement_y;
1610       gboolean displace_focus;
1611       
1612       gtk_widget_style_get (widget,
1613                             "child-displacement-y", &child_displacement_y,
1614                             "child-displacement-x", &child_displacement_x,
1615                             "displace-focus", &displace_focus,
1616                             NULL);
1617
1618       if (interior_focus)
1619         {
1620           x += style->xthickness + focus_pad;
1621           y += style->ythickness + focus_pad;
1622           width -= 2 * (style->xthickness + focus_pad);
1623           height -=  2 * (style->ythickness + focus_pad);
1624         }
1625       else
1626         {
1627           x -= focus_width + focus_pad;
1628           y -= focus_width + focus_pad;
1629           width += 2 * (focus_width + focus_pad);
1630           height += 2 * (focus_width + focus_pad);
1631         }
1632
1633       if (button->depressed && displace_focus)
1634         {
1635           x += child_displacement_x;
1636           y += child_displacement_y;
1637         }
1638
1639       gtk_paint_focus (style, cr,
1640                        gtk_widget_get_state (widget),
1641                        widget, "button",
1642                        x, y, width, height);
1643     }
1644 }
1645
1646 static gboolean
1647 gtk_button_draw (GtkWidget *widget,
1648                  cairo_t   *cr)
1649 {
1650   GtkButton *button = GTK_BUTTON (widget);
1651   
1652   _gtk_button_paint (button, cr, 
1653                      gtk_widget_get_allocated_width (widget),
1654                      gtk_widget_get_allocated_height (widget),
1655                      gtk_widget_get_state (widget),
1656                      button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1657                      "button", "buttondefault");
1658
1659   GTK_WIDGET_CLASS (gtk_button_parent_class)->draw (widget, cr);
1660
1661   return FALSE;
1662 }
1663
1664 static gboolean
1665 gtk_button_button_press (GtkWidget      *widget,
1666                          GdkEventButton *event)
1667 {
1668   GtkButton *button;
1669
1670   if (event->type == GDK_BUTTON_PRESS)
1671     {
1672       button = GTK_BUTTON (widget);
1673
1674       if (button->focus_on_click && !gtk_widget_has_focus (widget))
1675         gtk_widget_grab_focus (widget);
1676
1677       if (event->button == 1)
1678         gtk_button_pressed (button);
1679     }
1680
1681   return TRUE;
1682 }
1683
1684 static gboolean
1685 gtk_button_button_release (GtkWidget      *widget,
1686                            GdkEventButton *event)
1687 {
1688   GtkButton *button;
1689
1690   if (event->button == 1)
1691     {
1692       button = GTK_BUTTON (widget);
1693       gtk_button_released (button);
1694     }
1695
1696   return TRUE;
1697 }
1698
1699 static gboolean
1700 gtk_button_grab_broken (GtkWidget          *widget,
1701                         GdkEventGrabBroken *event)
1702 {
1703   GtkButton *button = GTK_BUTTON (widget);
1704   gboolean save_in;
1705   
1706   /* Simulate a button release without the pointer in the button */
1707   if (button->button_down)
1708     {
1709       save_in = button->in_button;
1710       button->in_button = FALSE;
1711       gtk_button_released (button);
1712       if (save_in != button->in_button)
1713         {
1714           button->in_button = save_in;
1715           gtk_button_update_state (button);
1716         }
1717     }
1718
1719   return TRUE;
1720 }
1721
1722 static gboolean
1723 gtk_button_key_release (GtkWidget   *widget,
1724                         GdkEventKey *event)
1725 {
1726   GtkButton *button = GTK_BUTTON (widget);
1727
1728   if (button->activate_timeout)
1729     {
1730       gtk_button_finish_activate (button, TRUE);
1731       return TRUE;
1732     }
1733   else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1734     return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1735   else
1736     return FALSE;
1737 }
1738
1739 static gboolean
1740 gtk_button_enter_notify (GtkWidget        *widget,
1741                          GdkEventCrossing *event)
1742 {
1743   GtkButton *button;
1744   GtkWidget *event_widget;
1745
1746   button = GTK_BUTTON (widget);
1747   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1748
1749   if ((event_widget == widget) &&
1750       (event->detail != GDK_NOTIFY_INFERIOR))
1751     {
1752       button->in_button = TRUE;
1753       gtk_button_enter (button);
1754     }
1755
1756   return FALSE;
1757 }
1758
1759 static gboolean
1760 gtk_button_leave_notify (GtkWidget        *widget,
1761                          GdkEventCrossing *event)
1762 {
1763   GtkButton *button;
1764   GtkWidget *event_widget;
1765
1766   button = GTK_BUTTON (widget);
1767   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1768
1769   if ((event_widget == widget) &&
1770       (event->detail != GDK_NOTIFY_INFERIOR) &&
1771       (gtk_widget_get_sensitive (event_widget)))
1772     {
1773       button->in_button = FALSE;
1774       gtk_button_leave (button);
1775     }
1776
1777   return FALSE;
1778 }
1779
1780 static void
1781 gtk_real_button_pressed (GtkButton *button)
1782 {
1783   if (button->activate_timeout)
1784     return;
1785   
1786   button->button_down = TRUE;
1787   gtk_button_update_state (button);
1788 }
1789
1790 static void
1791 gtk_real_button_released (GtkButton *button)
1792 {
1793   if (button->button_down)
1794     {
1795       button->button_down = FALSE;
1796
1797       if (button->activate_timeout)
1798         return;
1799       
1800       if (button->in_button)
1801         gtk_button_clicked (button);
1802
1803       gtk_button_update_state (button);
1804     }
1805 }
1806
1807 static void 
1808 gtk_real_button_clicked (GtkButton *button)
1809 {
1810   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1811
1812   if (priv->action)
1813     gtk_action_activate (priv->action);
1814 }
1815
1816 static gboolean
1817 button_activate_timeout (gpointer data)
1818 {
1819   gtk_button_finish_activate (data, TRUE);
1820
1821   return FALSE;
1822 }
1823
1824 static void
1825 gtk_real_button_activate (GtkButton *button)
1826 {
1827   GtkWidget *widget = GTK_WIDGET (button);
1828   GtkButtonPrivate *priv;
1829   GdkDevice *device;
1830   guint32 time;
1831
1832   priv = GTK_BUTTON_GET_PRIVATE (button);
1833   device = gtk_get_current_event_device ();
1834
1835   if (device && device->source != GDK_SOURCE_KEYBOARD)
1836     device = gdk_device_get_associated_device (device);
1837
1838   g_return_if_fail (device && device->source == GDK_SOURCE_KEYBOARD);
1839
1840   if (gtk_widget_get_realized (widget) && !button->activate_timeout)
1841     {
1842       time = gtk_get_current_event_time ();
1843
1844       if (gdk_device_grab (device, button->event_window,
1845                            GDK_OWNERSHIP_WINDOW, TRUE,
1846                            GDK_KEY_PRESS | GDK_KEY_RELEASE,
1847                            NULL, time) == GDK_GRAB_SUCCESS)
1848         {
1849           gtk_device_grab_add (widget, device, TRUE);
1850           priv->grab_keyboard = device;
1851           priv->grab_time = time;
1852         }
1853
1854       button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1855                                                 button_activate_timeout,
1856                                                 button);
1857       button->button_down = TRUE;
1858       gtk_button_update_state (button);
1859       gtk_widget_queue_draw (GTK_WIDGET (button));
1860     }
1861 }
1862
1863 static void
1864 gtk_button_finish_activate (GtkButton *button,
1865                             gboolean   do_it)
1866 {
1867   GtkWidget *widget = GTK_WIDGET (button);
1868   GtkButtonPrivate *priv;
1869   
1870   priv = GTK_BUTTON_GET_PRIVATE (button);
1871
1872   g_source_remove (button->activate_timeout);
1873   button->activate_timeout = 0;
1874
1875   if (priv->grab_keyboard)
1876     {
1877       gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1878       gtk_device_grab_remove (widget, priv->grab_keyboard);
1879       priv->grab_keyboard = NULL;
1880     }
1881
1882   button->button_down = FALSE;
1883
1884   gtk_button_update_state (button);
1885   gtk_widget_queue_draw (GTK_WIDGET (button));
1886
1887   if (do_it)
1888     gtk_button_clicked (button);
1889 }
1890
1891
1892 static void
1893 gtk_button_get_size (GtkWidget      *widget,
1894                      GtkOrientation  orientation,
1895                      gint           *minimum_size,
1896                      gint           *natural_size)
1897 {
1898   GtkButton *button = GTK_BUTTON (widget);
1899   GtkStyle *style;
1900   GtkWidget *child;
1901   GtkBorder default_border;
1902   GtkBorder inner_border;
1903   gint focus_width;
1904   gint focus_pad;
1905   gint minimum, natural;
1906
1907   gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1908   gtk_widget_style_get (GTK_WIDGET (widget),
1909                         "focus-line-width", &focus_width,
1910                         "focus-padding", &focus_pad,
1911                         NULL);
1912
1913   style = gtk_widget_get_style (GTK_WIDGET (widget));
1914
1915   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1916     {
1917       minimum = (style->xthickness * 2 +
1918                  inner_border.left + inner_border.right);
1919
1920       if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1921         minimum += default_border.left + default_border.right;
1922     }
1923   else
1924     {
1925       minimum = (style->ythickness * 2 +
1926                  inner_border.top + inner_border.bottom);
1927
1928       if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1929         minimum += default_border.top + default_border.bottom;
1930     }  
1931
1932   minimum += 2 * (focus_width + focus_pad);
1933   natural = minimum;
1934
1935   if ((child = gtk_bin_get_child (GTK_BIN (button))) && 
1936       gtk_widget_get_visible (child))
1937     {
1938       gint child_min, child_nat;
1939
1940       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1941         gtk_widget_get_preferred_width (child, &child_min, &child_nat);
1942       else
1943         gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1944
1945       minimum += child_min;
1946       natural += child_nat;
1947     }
1948
1949   if (minimum_size)
1950     *minimum_size = minimum;
1951
1952   if (natural_size)
1953     *natural_size = natural;
1954 }
1955
1956 static void 
1957 gtk_button_get_preferred_width (GtkWidget *widget,
1958                                 gint      *minimum_size,
1959                                 gint      *natural_size)
1960 {
1961   gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1962 }
1963
1964 static void 
1965 gtk_button_get_preferred_height (GtkWidget *widget,
1966                                  gint      *minimum_size,
1967                                  gint      *natural_size)
1968 {
1969   gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1970 }
1971
1972 /**
1973  * gtk_button_set_label:
1974  * @button: a #GtkButton
1975  * @label: a string
1976  *
1977  * Sets the text of the label of the button to @str. This text is
1978  * also used to select the stock item if gtk_button_set_use_stock()
1979  * is used.
1980  *
1981  * This will also clear any previously set labels.
1982  **/
1983 void
1984 gtk_button_set_label (GtkButton   *button,
1985                       const gchar *label)
1986 {
1987   gchar *new_label;
1988   
1989   g_return_if_fail (GTK_IS_BUTTON (button));
1990
1991   new_label = g_strdup (label);
1992   g_free (button->label_text);
1993   button->label_text = new_label;
1994   
1995   gtk_button_construct_child (button);
1996   
1997   g_object_notify (G_OBJECT (button), "label");
1998 }
1999
2000 /**
2001  * gtk_button_get_label:
2002  * @button: a #GtkButton
2003  *
2004  * Fetches the text from the label of the button, as set by
2005  * gtk_button_set_label(). If the label text has not 
2006  * been set the return value will be %NULL. This will be the 
2007  * case if you create an empty button with gtk_button_new() to 
2008  * use as a container.
2009  *
2010  * Return value: The text of the label widget. This string is owned
2011  * by the widget and must not be modified or freed.
2012  **/
2013 G_CONST_RETURN gchar *
2014 gtk_button_get_label (GtkButton *button)
2015 {
2016   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2017   
2018   return button->label_text;
2019 }
2020
2021 /**
2022  * gtk_button_set_use_underline:
2023  * @button: a #GtkButton
2024  * @use_underline: %TRUE if underlines in the text indicate mnemonics
2025  *
2026  * If true, an underline in the text of the button label indicates
2027  * the next character should be used for the mnemonic accelerator key.
2028  */
2029 void
2030 gtk_button_set_use_underline (GtkButton *button,
2031                               gboolean   use_underline)
2032 {
2033   g_return_if_fail (GTK_IS_BUTTON (button));
2034
2035   use_underline = use_underline != FALSE;
2036
2037   if (use_underline != button->use_underline)
2038     {
2039       button->use_underline = use_underline;
2040   
2041       gtk_button_construct_child (button);
2042       
2043       g_object_notify (G_OBJECT (button), "use-underline");
2044     }
2045 }
2046
2047 /**
2048  * gtk_button_get_use_underline:
2049  * @button: a #GtkButton
2050  *
2051  * Returns whether an embedded underline in the button label indicates a
2052  * mnemonic. See gtk_button_set_use_underline ().
2053  *
2054  * Return value: %TRUE if an embedded underline in the button label
2055  *               indicates the mnemonic accelerator keys.
2056  **/
2057 gboolean
2058 gtk_button_get_use_underline (GtkButton *button)
2059 {
2060   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2061   
2062   return button->use_underline;
2063 }
2064
2065 /**
2066  * gtk_button_set_use_stock:
2067  * @button: a #GtkButton
2068  * @use_stock: %TRUE if the button should use a stock item
2069  *
2070  * If %TRUE, the label set on the button is used as a
2071  * stock id to select the stock item for the button.
2072  */
2073 void
2074 gtk_button_set_use_stock (GtkButton *button,
2075                           gboolean   use_stock)
2076 {
2077   g_return_if_fail (GTK_IS_BUTTON (button));
2078
2079   use_stock = use_stock != FALSE;
2080
2081   if (use_stock != button->use_stock)
2082     {
2083       button->use_stock = use_stock;
2084   
2085       gtk_button_construct_child (button);
2086       
2087       g_object_notify (G_OBJECT (button), "use-stock");
2088     }
2089 }
2090
2091 /**
2092  * gtk_button_get_use_stock:
2093  * @button: a #GtkButton
2094  *
2095  * Returns whether the button label is a stock item.
2096  *
2097  * Return value: %TRUE if the button label is used to
2098  *               select a stock item instead of being
2099  *               used directly as the label text.
2100  */
2101 gboolean
2102 gtk_button_get_use_stock (GtkButton *button)
2103 {
2104   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2105   
2106   return button->use_stock;
2107 }
2108
2109 /**
2110  * gtk_button_set_focus_on_click:
2111  * @button: a #GtkButton
2112  * @focus_on_click: whether the button grabs focus when clicked with the mouse
2113  *
2114  * Sets whether the button will grab focus when it is clicked with the mouse.
2115  * Making mouse clicks not grab focus is useful in places like toolbars where
2116  * you don't want the keyboard focus removed from the main area of the
2117  * application.
2118  *
2119  * Since: 2.4
2120  **/
2121 void
2122 gtk_button_set_focus_on_click (GtkButton *button,
2123                                gboolean   focus_on_click)
2124 {
2125   g_return_if_fail (GTK_IS_BUTTON (button));
2126   
2127   focus_on_click = focus_on_click != FALSE;
2128
2129   if (button->focus_on_click != focus_on_click)
2130     {
2131       button->focus_on_click = focus_on_click;
2132       
2133       g_object_notify (G_OBJECT (button), "focus-on-click");
2134     }
2135 }
2136
2137 /**
2138  * gtk_button_get_focus_on_click:
2139  * @button: a #GtkButton
2140  *
2141  * Returns whether the button grabs focus when it is clicked with the mouse.
2142  * See gtk_button_set_focus_on_click().
2143  *
2144  * Return value: %TRUE if the button grabs focus when it is clicked with
2145  *               the mouse.
2146  *
2147  * Since: 2.4
2148  **/
2149 gboolean
2150 gtk_button_get_focus_on_click (GtkButton *button)
2151 {
2152   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2153   
2154   return button->focus_on_click;
2155 }
2156
2157 /**
2158  * gtk_button_set_alignment:
2159  * @button: a #GtkButton
2160  * @xalign: the horizontal position of the child, 0.0 is left aligned, 
2161  *   1.0 is right aligned
2162  * @yalign: the vertical position of the child, 0.0 is top aligned, 
2163  *   1.0 is bottom aligned
2164  *
2165  * Sets the alignment of the child. This property has no effect unless 
2166  * the child is a #GtkMisc or a #GtkAligment.
2167  *
2168  * Since: 2.4
2169  */
2170 void
2171 gtk_button_set_alignment (GtkButton *button,
2172                           gfloat     xalign,
2173                           gfloat     yalign)
2174 {
2175   GtkButtonPrivate *priv;
2176
2177   g_return_if_fail (GTK_IS_BUTTON (button));
2178   
2179   priv = GTK_BUTTON_GET_PRIVATE (button);
2180
2181   priv->xalign = xalign;
2182   priv->yalign = yalign;
2183   priv->align_set = 1;
2184
2185   maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2186
2187   g_object_freeze_notify (G_OBJECT (button));
2188   g_object_notify (G_OBJECT (button), "xalign");
2189   g_object_notify (G_OBJECT (button), "yalign");
2190   g_object_thaw_notify (G_OBJECT (button));
2191 }
2192
2193 /**
2194  * gtk_button_get_alignment:
2195  * @button: a #GtkButton
2196  * @xalign: return location for horizontal alignment
2197  * @yalign: return location for vertical alignment
2198  *
2199  * Gets the alignment of the child in the button.
2200  *
2201  * Since: 2.4
2202  */
2203 void
2204 gtk_button_get_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 = GTK_BUTTON_GET_PRIVATE (button);
2213  
2214   if (xalign) 
2215     *xalign = priv->xalign;
2216
2217   if (yalign)
2218     *yalign = priv->yalign;
2219 }
2220
2221 /**
2222  * _gtk_button_set_depressed:
2223  * @button: a #GtkButton
2224  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2225  *
2226  * Sets whether the button is currently drawn as down or not. This is 
2227  * purely a visual setting, and is meant only for use by derived widgets
2228  * such as #GtkToggleButton.
2229  **/
2230 void
2231 _gtk_button_set_depressed (GtkButton *button,
2232                            gboolean   depressed)
2233 {
2234   GtkWidget *widget = GTK_WIDGET (button);
2235
2236   depressed = depressed != FALSE;
2237
2238   if (depressed != button->depressed)
2239     {
2240       button->depressed = depressed;
2241       gtk_widget_queue_resize (widget);
2242     }
2243 }
2244
2245 static void
2246 gtk_button_update_state (GtkButton *button)
2247 {
2248   gboolean depressed;
2249   GtkStateType new_state;
2250
2251   if (button->activate_timeout)
2252     depressed = button->depress_on_activate;
2253   else
2254     depressed = button->in_button && button->button_down;
2255
2256   if (button->in_button && (!button->button_down || !depressed))
2257     new_state = GTK_STATE_PRELIGHT;
2258   else
2259     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2260
2261   _gtk_button_set_depressed (button, depressed); 
2262   gtk_widget_set_state (GTK_WIDGET (button), new_state);
2263 }
2264
2265 static void 
2266 show_image_change_notify (GtkButton *button)
2267 {
2268   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2269
2270   if (priv->image) 
2271     {
2272       if (show_image (button))
2273         gtk_widget_show (priv->image);
2274       else
2275         gtk_widget_hide (priv->image);
2276     }
2277 }
2278
2279 static void
2280 traverse_container (GtkWidget *widget,
2281                     gpointer   data)
2282 {
2283   if (GTK_IS_BUTTON (widget))
2284     show_image_change_notify (GTK_BUTTON (widget));
2285   else if (GTK_IS_CONTAINER (widget))
2286     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2287 }
2288
2289 static void
2290 gtk_button_setting_changed (GtkSettings *settings)
2291 {
2292   GList *list, *l;
2293
2294   list = gtk_window_list_toplevels ();
2295
2296   for (l = list; l; l = l->next)
2297     gtk_container_forall (GTK_CONTAINER (l->data), 
2298                           traverse_container, NULL);
2299
2300   g_list_free (list);
2301 }
2302
2303
2304 static void
2305 gtk_button_screen_changed (GtkWidget *widget,
2306                            GdkScreen *previous_screen)
2307 {
2308   GtkButton *button;
2309   GtkSettings *settings;
2310   guint show_image_connection;
2311
2312   if (!gtk_widget_has_screen (widget))
2313     return;
2314
2315   button = GTK_BUTTON (widget);
2316
2317   /* If the button is being pressed while the screen changes the
2318     release might never occur, so we reset the state. */
2319   if (button->button_down)
2320     {
2321       button->button_down = FALSE;
2322       gtk_button_update_state (button);
2323     }
2324
2325   settings = gtk_widget_get_settings (widget);
2326
2327   show_image_connection = 
2328     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
2329                                          "gtk-button-connection"));
2330   
2331   if (show_image_connection)
2332     return;
2333
2334   show_image_connection =
2335     g_signal_connect (settings, "notify::gtk-button-images",
2336                       G_CALLBACK (gtk_button_setting_changed), NULL);
2337   g_object_set_data (G_OBJECT (settings), 
2338                      I_("gtk-button-connection"),
2339                      GUINT_TO_POINTER (show_image_connection));
2340
2341   show_image_change_notify (button);
2342 }
2343
2344 static void
2345 gtk_button_state_changed (GtkWidget    *widget,
2346                           GtkStateType  previous_state)
2347 {
2348   GtkButton *button = GTK_BUTTON (widget);
2349
2350   if (!gtk_widget_is_sensitive (widget))
2351     {
2352       button->in_button = FALSE;
2353       gtk_real_button_released (button);
2354     }
2355 }
2356
2357 static void
2358 gtk_button_grab_notify (GtkWidget *widget,
2359                         gboolean   was_grabbed)
2360 {
2361   GtkButton *button = GTK_BUTTON (widget);
2362   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2363   gboolean save_in;
2364
2365   if (button->activate_timeout &&
2366       priv->grab_keyboard &&
2367       gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2368     gtk_button_finish_activate (button, FALSE);
2369
2370   if (!was_grabbed)
2371     {
2372       save_in = button->in_button;
2373       button->in_button = FALSE; 
2374       gtk_real_button_released (button);
2375       if (save_in != button->in_button)
2376         {
2377           button->in_button = save_in;
2378           gtk_button_update_state (button);
2379         }
2380     }
2381 }
2382
2383 /**
2384  * gtk_button_set_image:
2385  * @button: a #GtkButton
2386  * @image: a widget to set as the image for the button
2387  *
2388  * Set the image of @button to the given widget. Note that
2389  * it depends on the #GtkSettings:gtk-button-images setting whether the
2390  * image will be displayed or not, you don't have to call
2391  * gtk_widget_show() on @image yourself.
2392  *
2393  * Since: 2.6
2394  */ 
2395 void
2396 gtk_button_set_image (GtkButton *button,
2397                       GtkWidget *image)
2398 {
2399   GtkButtonPrivate *priv;
2400   GtkWidget *parent;
2401
2402   g_return_if_fail (GTK_IS_BUTTON (button));
2403   g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2404
2405   priv = GTK_BUTTON_GET_PRIVATE (button);
2406
2407   if (priv->image)
2408     {
2409       parent = gtk_widget_get_parent (priv->image);
2410       if (parent)
2411         gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2412     }
2413
2414   priv->image = image;
2415   priv->image_is_stock = (image == NULL);
2416
2417   gtk_button_construct_child (button);
2418
2419   g_object_notify (G_OBJECT (button), "image");
2420 }
2421
2422 /**
2423  * gtk_button_get_image:
2424  * @button: a #GtkButton
2425  *
2426  * Gets the widget that is currenty set as the image of @button.
2427  * This may have been explicitly set by gtk_button_set_image()
2428  * or constructed by gtk_button_new_from_stock().
2429  *
2430  * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2431  *
2432  * Since: 2.6
2433  */
2434 GtkWidget *
2435 gtk_button_get_image (GtkButton *button)
2436 {
2437   GtkButtonPrivate *priv;
2438
2439   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2440
2441   priv = GTK_BUTTON_GET_PRIVATE (button);
2442   
2443   return priv->image;
2444 }
2445
2446 /**
2447  * gtk_button_set_image_position:
2448  * @button: a #GtkButton
2449  * @position: the position
2450  *
2451  * Sets the position of the image relative to the text 
2452  * inside the button.
2453  *
2454  * Since: 2.10
2455  */ 
2456 void
2457 gtk_button_set_image_position (GtkButton       *button,
2458                                GtkPositionType  position)
2459 {
2460
2461   GtkButtonPrivate *priv;
2462
2463   g_return_if_fail (GTK_IS_BUTTON (button));
2464   g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2465   
2466   priv = GTK_BUTTON_GET_PRIVATE (button);
2467
2468   if (priv->image_position != position)
2469     {
2470       priv->image_position = position;
2471
2472       gtk_button_construct_child (button);
2473
2474       g_object_notify (G_OBJECT (button), "image-position");
2475     }
2476 }
2477
2478 /**
2479  * gtk_button_get_image_position:
2480  * @button: a #GtkButton
2481  *
2482  * Gets the position of the image relative to the text 
2483  * inside the button.
2484  *
2485  * Return value: the position
2486  *
2487  * Since: 2.10
2488  */
2489 GtkPositionType
2490 gtk_button_get_image_position (GtkButton *button)
2491 {
2492   GtkButtonPrivate *priv;
2493
2494   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2495
2496   priv = GTK_BUTTON_GET_PRIVATE (button);
2497   
2498   return priv->image_position;
2499 }
2500
2501
2502 /**
2503  * gtk_button_get_event_window:
2504  * @button: a #GtkButton
2505  *
2506  * Returns the button's event window if it is realized, %NULL otherwise.
2507  * This function should be rarely needed.
2508  *
2509  * Return value: (transfer none): @button's event window.
2510  *
2511  * Since: 2.22
2512  */
2513 GdkWindow*
2514 gtk_button_get_event_window (GtkButton *button)
2515 {
2516   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2517
2518   return button->event_window;
2519 }