]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
Really free the list. (#158422, Morten Welinder)
[~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 #include <config.h>
28 #include <string.h>
29 #include "gtkalias.h"
30 #include "gtkalignment.h"
31 #include "gtkbutton.h"
32 #include "gtklabel.h"
33 #include "gtkmain.h"
34 #include "gtkmarshalers.h"
35 #include "gtkimage.h"
36 #include "gtkhbox.h"
37 #include "gtkstock.h"
38 #include "gtkiconfactory.h"
39 #include "gtkintl.h"
40
41 #define CHILD_SPACING     1
42
43 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
44 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
45
46 /* Time out before giving up on getting a key release when animating
47  * the close button.
48  */
49 #define ACTIVATE_TIMEOUT 250
50
51 enum {
52   PRESSED,
53   RELEASED,
54   CLICKED,
55   ENTER,
56   LEAVE,
57   ACTIVATE,
58   LAST_SIGNAL
59 };
60
61 enum {
62   PROP_0,
63   PROP_LABEL,
64   PROP_IMAGE,
65   PROP_RELIEF,
66   PROP_USE_UNDERLINE,
67   PROP_USE_STOCK,
68   PROP_FOCUS_ON_CLICK,
69   PROP_XALIGN,
70   PROP_YALIGN,
71 };
72
73 #define GTK_BUTTON_GET_PRIVATE(o)       (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_BUTTON, GtkButtonPrivate))
74 typedef struct _GtkButtonPrivate GtkButtonPrivate;
75
76 struct _GtkButtonPrivate
77 {
78   gfloat       xalign;
79   gfloat       yalign;
80   GtkWidget   *image;
81   guint        align_set : 1;
82   guint        image_is_stock : 1;
83 };
84
85 static void gtk_button_class_init     (GtkButtonClass   *klass);
86 static void gtk_button_init           (GtkButton        *button);
87 static void gtk_button_destroy        (GtkObject        *object);
88 static void gtk_button_set_property   (GObject         *object,
89                                        guint            prop_id,
90                                        const GValue    *value,
91                                        GParamSpec      *pspec);
92 static void gtk_button_get_property   (GObject         *object,
93                                        guint            prop_id,
94                                        GValue          *value,
95                                        GParamSpec      *pspec);
96 static void gtk_button_screen_changed (GtkWidget        *widget,
97                                        GdkScreen        *previous_screen);
98 static void gtk_button_realize        (GtkWidget        *widget);
99 static void gtk_button_unrealize      (GtkWidget        *widget);
100 static void gtk_button_map            (GtkWidget        *widget);
101 static void gtk_button_unmap          (GtkWidget        *widget);
102 static void gtk_button_size_request   (GtkWidget        *widget,
103                                        GtkRequisition   *requisition);
104 static void gtk_button_size_allocate  (GtkWidget        *widget,
105                                        GtkAllocation    *allocation);
106 static gint gtk_button_expose         (GtkWidget        *widget,
107                                        GdkEventExpose   *event);
108 static gint gtk_button_button_press   (GtkWidget        *widget,
109                                        GdkEventButton   *event);
110 static gint gtk_button_button_release (GtkWidget        *widget,
111                                        GdkEventButton   *event);
112 static gint gtk_button_key_release    (GtkWidget        *widget,
113                                        GdkEventKey      *event);
114 static gint gtk_button_enter_notify   (GtkWidget        *widget,
115                                        GdkEventCrossing *event);
116 static gint gtk_button_leave_notify   (GtkWidget        *widget,
117                                        GdkEventCrossing *event);
118 static void gtk_real_button_pressed   (GtkButton        *button);
119 static void gtk_real_button_released  (GtkButton        *button);
120 static void gtk_real_button_activate  (GtkButton         *button);
121 static void gtk_button_update_state   (GtkButton        *button);
122 static void gtk_button_add            (GtkContainer   *container,
123                                        GtkWidget      *widget);
124 static GType gtk_button_child_type    (GtkContainer     *container);
125 static void gtk_button_finish_activate (GtkButton *button,
126                                         gboolean   do_it);
127
128 static GObject* gtk_button_constructor (GType                  type,
129                                         guint                  n_construct_properties,
130                                         GObjectConstructParam *construct_params);
131 static void gtk_button_construct_child (GtkButton             *button);
132 static void gtk_button_state_changed   (GtkWidget             *widget,
133                                         GtkStateType           previous_state);
134 static void gtk_button_grab_notify     (GtkWidget             *widget,
135                                         gboolean               was_grabbed);
136
137
138
139 static GtkBinClass *parent_class = NULL;
140 static guint button_signals[LAST_SIGNAL] = { 0 };
141
142
143 GType
144 gtk_button_get_type (void)
145 {
146   static GType button_type = 0;
147
148   if (!button_type)
149     {
150       static const GTypeInfo button_info =
151       {
152         sizeof (GtkButtonClass),
153         NULL,           /* base_init */
154         NULL,           /* base_finalize */
155         (GClassInitFunc) gtk_button_class_init,
156         NULL,           /* class_finalize */
157         NULL,           /* class_data */
158         sizeof (GtkButton),
159         16,             /* n_preallocs */
160         (GInstanceInitFunc) gtk_button_init,
161       };
162
163       button_type = g_type_register_static (GTK_TYPE_BIN, "GtkButton",
164                                             &button_info, 0);
165     }
166
167   return button_type;
168 }
169
170 static void
171 gtk_button_class_init (GtkButtonClass *klass)
172 {
173   GObjectClass *gobject_class;
174   GtkObjectClass *object_class;
175   GtkWidgetClass *widget_class;
176   GtkContainerClass *container_class;
177
178   gobject_class = G_OBJECT_CLASS (klass);
179   object_class = (GtkObjectClass*) klass;
180   widget_class = (GtkWidgetClass*) klass;
181   container_class = (GtkContainerClass*) klass;
182   
183   parent_class = g_type_class_peek_parent (klass);
184
185   gobject_class->constructor = gtk_button_constructor;
186   gobject_class->set_property = gtk_button_set_property;
187   gobject_class->get_property = gtk_button_get_property;
188
189   object_class->destroy = gtk_button_destroy;
190
191   widget_class->screen_changed = gtk_button_screen_changed;
192   widget_class->realize = gtk_button_realize;
193   widget_class->unrealize = gtk_button_unrealize;
194   widget_class->map = gtk_button_map;
195   widget_class->unmap = gtk_button_unmap;
196   widget_class->size_request = gtk_button_size_request;
197   widget_class->size_allocate = gtk_button_size_allocate;
198   widget_class->expose_event = gtk_button_expose;
199   widget_class->button_press_event = gtk_button_button_press;
200   widget_class->button_release_event = gtk_button_button_release;
201   widget_class->key_release_event = gtk_button_key_release;
202   widget_class->enter_notify_event = gtk_button_enter_notify;
203   widget_class->leave_notify_event = gtk_button_leave_notify;
204   widget_class->state_changed = gtk_button_state_changed;
205   widget_class->grab_notify = gtk_button_grab_notify;
206
207   container_class->child_type = gtk_button_child_type;
208   container_class->add = gtk_button_add;
209
210   klass->pressed = gtk_real_button_pressed;
211   klass->released = gtk_real_button_released;
212   klass->clicked = NULL;
213   klass->enter = gtk_button_update_state;
214   klass->leave = gtk_button_update_state;
215   klass->activate = gtk_real_button_activate;
216
217   g_object_class_install_property (gobject_class,
218                                    PROP_LABEL,
219                                    g_param_spec_string ("label",
220                                                         P_("Label"),
221                                                         P_("Text of the label widget inside the button, if the button contains a label widget"),
222                                                         NULL,
223                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
224   
225   g_object_class_install_property (gobject_class,
226                                    PROP_USE_UNDERLINE,
227                                    g_param_spec_boolean ("use_underline",
228                                                          P_("Use underline"),
229                                                          P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
230                                                         FALSE,
231                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
232   
233   g_object_class_install_property (gobject_class,
234                                    PROP_USE_STOCK,
235                                    g_param_spec_boolean ("use_stock",
236                                                          P_("Use stock"),
237                                                          P_("If set, the label is used to pick a stock item instead of being displayed"),
238                                                         FALSE,
239                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
240   
241   g_object_class_install_property (gobject_class,
242                                    PROP_FOCUS_ON_CLICK,
243                                    g_param_spec_boolean ("focus_on_click",
244                                                          P_("Focus on click"),
245                                                          P_("Whether the button grabs focus when it is clicked with the mouse"),
246                                                          TRUE,
247                                                          G_PARAM_READWRITE));
248   
249   g_object_class_install_property (gobject_class,
250                                    PROP_RELIEF,
251                                    g_param_spec_enum ("relief",
252                                                       P_("Border relief"),
253                                                       P_("The border relief style"),
254                                                       GTK_TYPE_RELIEF_STYLE,
255                                                       GTK_RELIEF_NORMAL,
256                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
257   
258   /**
259    * GtkButton:xalign:
260    *
261    * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
262    * can be used to control it's horizontal alignment. 0.0 is left aligned, 
263    * 1.0 is right aligned.
264    * 
265    * Since: 2.4
266    */
267   g_object_class_install_property (gobject_class,
268                                    PROP_XALIGN,
269                                    g_param_spec_float("xalign",
270                                                       P_("Horizontal alignment for child"),
271                                                       P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
272                                                       0.0,
273                                                       1.0,
274                                                       0.5,
275                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
276
277   /**
278    * GtkButton:yalign:
279    *
280    * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
281    * can be used to control it's vertical alignment. 0.0 is top aligned, 
282    * 1.0 is bottom aligned.
283    * 
284    * Since: 2.4
285    */
286   g_object_class_install_property (gobject_class,
287                                    PROP_YALIGN,
288                                    g_param_spec_float("yalign",
289                                                       P_("Vertical alignment for child"),
290                                                       P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
291                                                       0.0,
292                                                       1.0,
293                                                       0.5,
294                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
295
296   /**
297    * GtkButton::image:
298    * 
299    * The child widget to appear next to the button text.
300    * 
301    * Since: 2.6
302    */
303   g_object_class_install_property (gobject_class,
304                                    PROP_IMAGE,
305                                    g_param_spec_object ("image",
306                                                         P_("Image widget"),
307                                                         P_("Child widget to appear next to the button text"),
308                                                         GTK_TYPE_WIDGET,
309                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
310
311   button_signals[PRESSED] =
312     g_signal_new ("pressed",
313                   G_OBJECT_CLASS_TYPE (object_class),
314                   G_SIGNAL_RUN_FIRST,
315                   G_STRUCT_OFFSET (GtkButtonClass, pressed),
316                   NULL, NULL,
317                   _gtk_marshal_VOID__VOID,
318                   G_TYPE_NONE, 0);
319   button_signals[RELEASED] =
320     g_signal_new ("released",
321                   G_OBJECT_CLASS_TYPE (object_class),
322                   G_SIGNAL_RUN_FIRST,
323                   G_STRUCT_OFFSET (GtkButtonClass, released),
324                   NULL, NULL,
325                   _gtk_marshal_VOID__VOID,
326                   G_TYPE_NONE, 0);
327   button_signals[CLICKED] =
328     g_signal_new ("clicked",
329                   G_OBJECT_CLASS_TYPE (object_class),
330                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
331                   G_STRUCT_OFFSET (GtkButtonClass, clicked),
332                   NULL, NULL,
333                   _gtk_marshal_VOID__VOID,
334                   G_TYPE_NONE, 0);
335   button_signals[ENTER] =
336     g_signal_new ("enter",
337                   G_OBJECT_CLASS_TYPE (object_class),
338                   G_SIGNAL_RUN_FIRST,
339                   G_STRUCT_OFFSET (GtkButtonClass, enter),
340                   NULL, NULL,
341                   _gtk_marshal_VOID__VOID,
342                   G_TYPE_NONE, 0);
343   button_signals[LEAVE] =
344     g_signal_new ("leave",
345                   G_OBJECT_CLASS_TYPE (object_class),
346                   G_SIGNAL_RUN_FIRST,
347                   G_STRUCT_OFFSET (GtkButtonClass, leave),
348                   NULL, NULL,
349                   _gtk_marshal_VOID__VOID,
350                   G_TYPE_NONE, 0);
351
352   /**
353    * GtkButton::activate:
354    * @widget: the object which received the signal.
355    *
356    * The "activate" signal on GtkButton is an action signal and
357    * emitting it causes the button to animate press then release. 
358    * Applications should never connect to this signal, but use the
359    * "clicked" signal.
360    */
361   button_signals[ACTIVATE] =
362     g_signal_new ("activate",
363                   G_OBJECT_CLASS_TYPE (object_class),
364                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
365                   G_STRUCT_OFFSET (GtkButtonClass, activate),
366                   NULL, NULL,
367                   _gtk_marshal_VOID__VOID,
368                   G_TYPE_NONE, 0);
369   widget_class->activate_signal = button_signals[ACTIVATE];
370
371   gtk_widget_class_install_style_property (widget_class,
372                                            g_param_spec_boxed ("default_border",
373                                                                P_("Default Spacing"),
374                                                                P_("Extra space to add for CAN_DEFAULT buttons"),
375                                                                GTK_TYPE_BORDER,
376                                                                G_PARAM_READABLE));
377
378   gtk_widget_class_install_style_property (widget_class,
379                                            g_param_spec_boxed ("default_outside_border",
380                                                                P_("Default Outside Spacing"),
381                                                                P_("Extra space to add for CAN_DEFAULT buttons that is always drawn outside the border"),
382                                                                GTK_TYPE_BORDER,
383                                                                G_PARAM_READABLE));
384   gtk_widget_class_install_style_property (widget_class,
385                                            g_param_spec_int ("child_displacement_x",
386                                                              P_("Child X Displacement"),
387                                                              P_("How far in the x direction to move the child when the button is depressed"),
388                                                              G_MININT,
389                                                              G_MAXINT,
390                                                              0,
391                                                              G_PARAM_READABLE));
392   gtk_widget_class_install_style_property (widget_class,
393                                            g_param_spec_int ("child_displacement_y",
394                                                              P_("Child Y Displacement"),
395                                                              P_("How far in the y direction to move the child when the button is depressed"),
396                                                              G_MININT,
397                                                              G_MAXINT,
398                                                              0,
399                                                              G_PARAM_READABLE));
400
401   /**
402    * GtkButton:displace-focus:
403    *
404    * Whether the child_displacement_x/child_displacement_y properties should also 
405    * affect the focus rectangle.
406    *
407    * Since: 2.6
408    */
409   gtk_widget_class_install_style_property (widget_class,
410                                            g_param_spec_boolean ("displace-focus",
411                                                                  P_("Displace focus"),
412                                                                  P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
413                                                        FALSE,
414                                                        G_PARAM_READABLE));
415
416   gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
417                                                        P_("Show button images"),
418                                                        P_("Whether stock icons should be shown in buttons"),
419                                                        TRUE,
420                                                        G_PARAM_READWRITE));
421   
422   g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));  
423 }
424
425 static void
426 gtk_button_init (GtkButton *button)
427 {
428   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
429
430   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
431   GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);
432
433   button->label_text = NULL;
434   
435   button->constructed = FALSE;
436   button->in_button = FALSE;
437   button->button_down = FALSE;
438   button->relief = GTK_RELIEF_NORMAL;
439   button->use_stock = FALSE;
440   button->use_underline = FALSE;
441   button->depressed = FALSE;
442   button->depress_on_activate = TRUE;
443   button->focus_on_click = TRUE;
444
445   priv->xalign = 0.5;
446   priv->yalign = 0.5;
447   priv->align_set = 0;
448 }
449
450 static void
451 gtk_button_destroy (GtkObject *object)
452 {
453   GtkButton *button = GTK_BUTTON (object);
454   
455   if (button->label_text)
456     {
457       g_free (button->label_text);
458       button->label_text = NULL;
459     }
460   
461   (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
462 }
463
464 static GObject*
465 gtk_button_constructor (GType                  type,
466                         guint                  n_construct_properties,
467                         GObjectConstructParam *construct_params)
468 {
469   GObject *object;
470   GtkButton *button;
471
472   object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
473                                                            n_construct_properties,
474                                                            construct_params);
475
476   button = GTK_BUTTON (object);
477   button->constructed = TRUE;
478
479   if (button->label_text != NULL)
480     gtk_button_construct_child (button);
481   
482   return object;
483 }
484
485
486 static GType
487 gtk_button_child_type  (GtkContainer     *container)
488 {
489   if (!GTK_BIN (container)->child)
490     return GTK_TYPE_WIDGET;
491   else
492     return G_TYPE_NONE;
493 }
494
495 static void
496 maybe_set_alignment (GtkButton *button,
497                      GtkWidget *widget)
498 {
499   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
500
501   if (GTK_IS_MISC (widget))
502     {
503       GtkMisc *misc = GTK_MISC (widget);
504       
505       if (priv->align_set)
506         gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
507     }
508   else if (GTK_IS_ALIGNMENT (widget))
509     {
510       GtkAlignment *alignment = GTK_ALIGNMENT (widget);
511
512       if (priv->align_set)
513         gtk_alignment_set (alignment, priv->xalign, priv->yalign, 
514                            alignment->xscale, alignment->yscale);
515     }
516 }
517
518 static void
519 gtk_button_add (GtkContainer *container,
520                 GtkWidget    *widget)
521 {
522   maybe_set_alignment (GTK_BUTTON (container), widget);
523
524   GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
525 }
526
527 static void
528 gtk_button_set_property (GObject         *object,
529                          guint            prop_id,
530                          const GValue    *value,
531                          GParamSpec      *pspec)
532 {
533   GtkButton *button = GTK_BUTTON (object);
534   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
535
536   switch (prop_id)
537     {
538     case PROP_LABEL:
539       gtk_button_set_label (button, g_value_get_string (value));
540       break;
541     case PROP_IMAGE:
542       gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
543       break;
544     case PROP_RELIEF:
545       gtk_button_set_relief (button, g_value_get_enum (value));
546       break;
547     case PROP_USE_UNDERLINE:
548       gtk_button_set_use_underline (button, g_value_get_boolean (value));
549       break;
550     case PROP_USE_STOCK:
551       gtk_button_set_use_stock (button, g_value_get_boolean (value));
552       break;
553     case PROP_FOCUS_ON_CLICK:
554       gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
555       break;
556     case PROP_XALIGN:
557       gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
558       break;
559     case PROP_YALIGN:
560       gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
561       break;
562     default:
563       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
564       break;
565     }
566 }
567
568 static void
569 gtk_button_get_property (GObject         *object,
570                          guint            prop_id,
571                          GValue          *value,
572                          GParamSpec      *pspec)
573 {
574   GtkButton *button = GTK_BUTTON (object);
575   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
576
577   switch (prop_id)
578     {
579     case PROP_LABEL:
580       g_value_set_string (value, button->label_text);
581       break;
582     case PROP_IMAGE:
583       g_value_set_object (value, (GObject *)priv->image);
584       break;
585     case PROP_RELIEF:
586       g_value_set_enum (value, gtk_button_get_relief (button));
587       break;
588     case PROP_USE_UNDERLINE:
589       g_value_set_boolean (value, button->use_underline);
590       break;
591     case PROP_USE_STOCK:
592       g_value_set_boolean (value, button->use_stock);
593       break;
594     case PROP_FOCUS_ON_CLICK:
595       g_value_set_boolean (value, button->focus_on_click);
596       break;
597     case PROP_XALIGN:
598       g_value_set_float (value, priv->xalign);
599       break;
600     case PROP_YALIGN:
601       g_value_set_float (value, priv->yalign);
602       break;
603     default:
604       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605       break;
606     }
607 }
608
609 GtkWidget*
610 gtk_button_new (void)
611 {
612   return g_object_new (GTK_TYPE_BUTTON, NULL);
613 }
614
615 static gboolean
616 show_image (GtkButton *button)
617 {
618   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (button));  
619   gboolean show;
620
621   g_object_get (settings, "gtk-button-images", &show, NULL);
622
623   return show;
624 }
625
626 static void
627 gtk_button_construct_child (GtkButton *button)
628 {
629   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
630   GtkStockItem item;
631   GtkWidget *label;
632   GtkWidget *hbox;
633   GtkWidget *align;
634   GtkWidget *image = NULL;
635   gchar *label_text = NULL;
636   
637   if (!button->constructed)
638     return;
639   
640   if (button->label_text == NULL)
641     return;
642
643   if (GTK_BIN (button)->child)
644     {
645       if (priv->image && !priv->image_is_stock)
646         image = g_object_ref (priv->image);
647
648       gtk_container_remove (GTK_CONTAINER (button),
649                             GTK_BIN (button)->child);
650   
651       priv->image = NULL;
652     }
653   
654   if (button->use_stock &&
655       gtk_stock_lookup (button->label_text, &item))
656     {
657       if (!image)
658         image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
659
660       label_text = item.label;
661     }
662   else
663     label_text = button->label_text;
664
665   if (image)
666     {
667       label = gtk_label_new_with_mnemonic (label_text);
668       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
669       
670       priv->image = image;
671
672       g_object_set (priv->image, 
673                     "visible", show_image (button),
674                     "no_show_all", TRUE,
675                     NULL);
676       hbox = gtk_hbox_new (FALSE, 2);
677
678       if (priv->align_set)
679         align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
680       else
681         align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
682         
683       gtk_box_pack_start (GTK_BOX (hbox), priv->image, FALSE, FALSE, 0);
684       gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
685       
686       gtk_container_add (GTK_CONTAINER (button), align);
687       gtk_container_add (GTK_CONTAINER (align), hbox);
688       gtk_widget_show_all (align);
689
690       g_object_unref (image);
691
692       return;
693     }
694   
695  if (button->use_underline)
696     {
697       label = gtk_label_new_with_mnemonic (button->label_text);
698       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
699     }
700   else
701     label = gtk_label_new (button->label_text);
702   
703   if (priv->align_set)
704     gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
705
706   gtk_widget_show (label);
707   gtk_container_add (GTK_CONTAINER (button), label);
708 }
709
710
711 GtkWidget*
712 gtk_button_new_with_label (const gchar *label)
713 {
714   return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
715 }
716
717 /**
718  * gtk_button_new_from_stock:
719  * @stock_id: the name of the stock item 
720  *
721  * Creates a new #GtkButton containing the image and text from a stock item.
722  * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
723  * #GTK_STOCK_APPLY.
724  *
725  * If @stock_id is unknown, then it will be treated as a mnemonic
726  * label (as for gtk_button_new_with_mnemonic()).
727  *
728  * Returns: a new #GtkButton
729  **/
730 GtkWidget*
731 gtk_button_new_from_stock (const gchar *stock_id)
732 {
733   return g_object_new (GTK_TYPE_BUTTON,
734                        "label", stock_id,
735                        "use_stock", TRUE,
736                        "use_underline", TRUE,
737                        NULL);
738 }
739
740 /**
741  * gtk_button_new_with_mnemonic:
742  * @label: The text of the button, with an underscore in front of the
743  *         mnemonic character
744  * @returns: a new #GtkButton
745  *
746  * Creates a new #GtkButton containing a label.
747  * If characters in @label are preceded by an underscore, they are underlined.
748  * If you need a literal underscore character in a label, use '__' (two 
749  * underscores). The first underlined character represents a keyboard 
750  * accelerator called a mnemonic.
751  * Pressing Alt and that key activates the button.
752  **/
753 GtkWidget*
754 gtk_button_new_with_mnemonic (const gchar *label)
755 {
756   return g_object_new (GTK_TYPE_BUTTON, "label", label, "use_underline", TRUE,  NULL);
757 }
758
759 void
760 gtk_button_pressed (GtkButton *button)
761 {
762   g_return_if_fail (GTK_IS_BUTTON (button));
763
764   
765   g_signal_emit (button, button_signals[PRESSED], 0);
766 }
767
768 void
769 gtk_button_released (GtkButton *button)
770 {
771   g_return_if_fail (GTK_IS_BUTTON (button));
772
773   g_signal_emit (button, button_signals[RELEASED], 0);
774 }
775
776 void
777 gtk_button_clicked (GtkButton *button)
778 {
779   g_return_if_fail (GTK_IS_BUTTON (button));
780
781   g_signal_emit (button, button_signals[CLICKED], 0);
782 }
783
784 void
785 gtk_button_enter (GtkButton *button)
786 {
787   g_return_if_fail (GTK_IS_BUTTON (button));
788
789   g_signal_emit (button, button_signals[ENTER], 0);
790 }
791
792 void
793 gtk_button_leave (GtkButton *button)
794 {
795   g_return_if_fail (GTK_IS_BUTTON (button));
796
797   g_signal_emit (button, button_signals[LEAVE], 0);
798 }
799
800 void
801 gtk_button_set_relief (GtkButton *button,
802                        GtkReliefStyle newrelief)
803 {
804   g_return_if_fail (GTK_IS_BUTTON (button));
805
806   if (newrelief != button->relief) 
807     {
808        button->relief = newrelief;
809        g_object_notify (G_OBJECT (button), "relief");
810        gtk_widget_queue_draw (GTK_WIDGET (button));
811     }
812 }
813
814 GtkReliefStyle
815 gtk_button_get_relief (GtkButton *button)
816 {
817   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
818
819   return button->relief;
820 }
821
822 static void
823 gtk_button_realize (GtkWidget *widget)
824 {
825   GtkButton *button;
826   GdkWindowAttr attributes;
827   gint attributes_mask;
828   gint border_width;
829
830   button = GTK_BUTTON (widget);
831   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
832
833   border_width = GTK_CONTAINER (widget)->border_width;
834
835   attributes.window_type = GDK_WINDOW_CHILD;
836   attributes.x = widget->allocation.x + border_width;
837   attributes.y = widget->allocation.y + border_width;
838   attributes.width = widget->allocation.width - border_width * 2;
839   attributes.height = widget->allocation.height - border_width * 2;
840   attributes.wclass = GDK_INPUT_ONLY;
841   attributes.event_mask = gtk_widget_get_events (widget);
842   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
843                             GDK_BUTTON_RELEASE_MASK |
844                             GDK_ENTER_NOTIFY_MASK |
845                             GDK_LEAVE_NOTIFY_MASK);
846
847   attributes_mask = GDK_WA_X | GDK_WA_Y;
848
849   widget->window = gtk_widget_get_parent_window (widget);
850   g_object_ref (widget->window);
851   
852   button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
853                                          &attributes, attributes_mask);
854   gdk_window_set_user_data (button->event_window, button);
855
856   widget->style = gtk_style_attach (widget->style, widget->window);
857 }
858
859 static void
860 gtk_button_unrealize (GtkWidget *widget)
861 {
862   GtkButton *button = GTK_BUTTON (widget);
863
864   if (button->activate_timeout)
865     gtk_button_finish_activate (button, FALSE);
866
867   if (button->event_window)
868     {
869       gdk_window_set_user_data (button->event_window, NULL);
870       gdk_window_destroy (button->event_window);
871       button->event_window = NULL;
872     }
873   
874   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
875 }
876
877 static void
878 gtk_button_map (GtkWidget *widget)
879 {
880   GtkButton *button = GTK_BUTTON (widget);
881   
882   GTK_WIDGET_CLASS (parent_class)->map (widget);
883
884   if (button->event_window)
885     gdk_window_show (button->event_window);
886 }
887
888 static void
889 gtk_button_unmap (GtkWidget *widget)
890 {
891   GtkButton *button = GTK_BUTTON (widget);
892     
893   if (button->event_window)
894     gdk_window_hide (button->event_window);
895
896   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
897 }
898
899 static void
900 gtk_button_get_props (GtkButton *button,
901                       GtkBorder *default_border,
902                       GtkBorder *default_outside_border,
903                       gboolean  *interior_focus)
904 {
905   GtkWidget *widget =  GTK_WIDGET (button);
906   GtkBorder *tmp_border;
907
908   if (default_border)
909     {
910       gtk_widget_style_get (widget, "default_border", &tmp_border, NULL);
911
912       if (tmp_border)
913         {
914           *default_border = *tmp_border;
915           g_free (tmp_border);
916         }
917       else
918         *default_border = default_default_border;
919     }
920
921   if (default_outside_border)
922     {
923       gtk_widget_style_get (widget, "default_outside_border", &tmp_border, NULL);
924
925       if (tmp_border)
926         {
927           *default_outside_border = *tmp_border;
928           g_free (tmp_border);
929         }
930       else
931         *default_outside_border = default_default_outside_border;
932     }
933
934   if (interior_focus)
935     gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
936 }
937         
938 static void
939 gtk_button_size_request (GtkWidget      *widget,
940                          GtkRequisition *requisition)
941 {
942   GtkButton *button = GTK_BUTTON (widget);
943   GtkBorder default_border;
944   gint focus_width;
945   gint focus_pad;
946
947   gtk_button_get_props (button, &default_border, NULL, NULL);
948   gtk_widget_style_get (GTK_WIDGET (widget),
949                         "focus-line-width", &focus_width,
950                         "focus-padding", &focus_pad,
951                         NULL);
952  
953   requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
954                         GTK_WIDGET (widget)->style->xthickness) * 2;
955   requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
956                          GTK_WIDGET (widget)->style->ythickness) * 2;
957
958   if (GTK_WIDGET_CAN_DEFAULT (widget))
959     {
960       requisition->width += default_border.left + default_border.right;
961       requisition->height += default_border.top + default_border.bottom;
962     }
963
964   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
965     {
966       GtkRequisition child_requisition;
967
968       gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
969
970       requisition->width += child_requisition.width;
971       requisition->height += child_requisition.height;
972     }
973   
974   requisition->width += 2 * (focus_width + focus_pad);
975   requisition->height += 2 * (focus_width + focus_pad);
976 }
977
978 static void
979 gtk_button_size_allocate (GtkWidget     *widget,
980                           GtkAllocation *allocation)
981 {
982   GtkButton *button = GTK_BUTTON (widget);
983   GtkAllocation child_allocation;
984
985   gint border_width = GTK_CONTAINER (widget)->border_width;
986   gint xthickness = GTK_WIDGET (widget)->style->xthickness;
987   gint ythickness = GTK_WIDGET (widget)->style->ythickness;
988   GtkBorder default_border;
989   gint focus_width;
990   gint focus_pad;
991
992   gtk_button_get_props (button, &default_border, NULL, NULL);
993   gtk_widget_style_get (GTK_WIDGET (widget),
994                         "focus-line-width", &focus_width,
995                         "focus-padding", &focus_pad,
996                         NULL);
997  
998                             
999   widget->allocation = *allocation;
1000
1001   if (GTK_WIDGET_REALIZED (widget))
1002     gdk_window_move_resize (button->event_window,
1003                             widget->allocation.x + border_width,
1004                             widget->allocation.y + border_width,
1005                             widget->allocation.width - border_width * 2,
1006                             widget->allocation.height - border_width * 2);
1007
1008   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
1009     {
1010       child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
1011       child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
1012       
1013       child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
1014                                     border_width * 2);
1015       child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
1016                                      border_width * 2);
1017
1018       if (GTK_WIDGET_CAN_DEFAULT (button))
1019         {
1020           child_allocation.x += default_border.left;
1021           child_allocation.y += default_border.top;
1022           child_allocation.width =  MAX (1, child_allocation.width - default_border.left - default_border.right);
1023           child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
1024         }
1025
1026       if (GTK_WIDGET_CAN_FOCUS (button))
1027         {
1028           child_allocation.x += focus_width + focus_pad;
1029           child_allocation.y += focus_width + focus_pad;
1030           child_allocation.width =  MAX (1, child_allocation.width - (focus_width + focus_pad) * 2);
1031           child_allocation.height = MAX (1, child_allocation.height - (focus_width + focus_pad) * 2);
1032         }
1033
1034       if (button->depressed)
1035         {
1036           gint child_displacement_x;
1037           gint child_displacement_y;
1038           
1039           gtk_widget_style_get (widget,
1040                                 "child_displacement_x", &child_displacement_x, 
1041                                 "child_displacement_y", &child_displacement_y,
1042                                 NULL);
1043           child_allocation.x += child_displacement_x;
1044           child_allocation.y += child_displacement_y;
1045         }
1046
1047       gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
1048     }
1049 }
1050
1051 void
1052 _gtk_button_paint (GtkButton    *button,
1053                    GdkRectangle *area,
1054                    GtkStateType  state_type,
1055                    GtkShadowType shadow_type,
1056                    const gchar  *main_detail,
1057                    const gchar  *default_detail)
1058 {
1059   GtkWidget *widget;
1060   gint width, height;
1061   gint x, y;
1062   gint border_width;
1063   GtkBorder default_border;
1064   GtkBorder default_outside_border;
1065   gboolean interior_focus;
1066   gint focus_width;
1067   gint focus_pad;
1068    
1069   if (GTK_WIDGET_DRAWABLE (button))
1070     {
1071       widget = GTK_WIDGET (button);
1072       border_width = GTK_CONTAINER (widget)->border_width;
1073
1074       gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
1075       gtk_widget_style_get (GTK_WIDGET (widget),
1076                             "focus-line-width", &focus_width,
1077                             "focus-padding", &focus_pad,
1078                             NULL); 
1079         
1080       x = widget->allocation.x + border_width;
1081       y = widget->allocation.y + border_width;
1082       width = widget->allocation.width - border_width * 2;
1083       height = widget->allocation.height - border_width * 2;
1084
1085       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
1086           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1087         {
1088           gtk_paint_box (widget->style, widget->window,
1089                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
1090                          area, widget, "buttondefault",
1091                          x, y, width, height);
1092
1093           x += default_border.left;
1094           y += default_border.top;
1095           width -= default_border.left + default_border.right;
1096           height -= default_border.top + default_border.bottom;
1097         }
1098       else if (GTK_WIDGET_CAN_DEFAULT (widget))
1099         {
1100           x += default_outside_border.left;
1101           y += default_outside_border.top;
1102           width -= default_outside_border.left + default_outside_border.right;
1103           height -= default_outside_border.top + default_outside_border.bottom;
1104         }
1105        
1106       if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
1107         {
1108           x += focus_width + focus_pad;
1109           y += focus_width + focus_pad;
1110           width -= 2 * (focus_width + focus_pad);
1111           height -= 2 * (focus_width + focus_pad);
1112         }
1113
1114       if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1115           GTK_WIDGET_STATE(widget) == GTK_STATE_PRELIGHT)
1116         gtk_paint_box (widget->style, widget->window,
1117                        state_type,
1118                        shadow_type, area, widget, "button",
1119                        x, y, width, height);
1120        
1121       if (GTK_WIDGET_HAS_FOCUS (widget))
1122         {
1123           gint child_displacement_x;
1124           gint child_displacement_y;
1125           gboolean displace_focus;
1126           
1127           gtk_widget_style_get (GTK_WIDGET (widget),
1128                                 "child_displacement_y", &child_displacement_y,
1129                                 "child_displacement_x", &child_displacement_x,
1130                                 "displace_focus", &displace_focus,
1131                                 NULL);
1132
1133           if (interior_focus)
1134             {
1135               x += widget->style->xthickness + focus_pad;
1136               y += widget->style->ythickness + focus_pad;
1137               width -= 2 * (widget->style->xthickness + focus_pad);
1138               height -=  2 * (widget->style->ythickness + focus_pad);
1139             }
1140           else
1141             {
1142               x -= focus_width + focus_pad;
1143               y -= focus_width + focus_pad;
1144               width += 2 * (focus_width + focus_pad);
1145               height += 2 * (focus_width + focus_pad);
1146             }
1147
1148           if (button->depressed && displace_focus)
1149             {
1150               x += child_displacement_x;
1151               y += child_displacement_y;
1152             }
1153
1154           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1155                            area, widget, "button",
1156                            x, y, width, height);
1157         }
1158     }
1159 }
1160
1161 static gboolean
1162 gtk_button_expose (GtkWidget      *widget,
1163                    GdkEventExpose *event)
1164 {
1165   if (GTK_WIDGET_DRAWABLE (widget))
1166     {
1167       GtkButton *button = GTK_BUTTON (widget);
1168       
1169       _gtk_button_paint (button, &event->area,
1170                          GTK_WIDGET_STATE (widget),
1171                          button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1172                          "button", "buttondefault");
1173       
1174       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1175     }
1176   
1177   return FALSE;
1178 }
1179
1180 static gboolean
1181 gtk_button_button_press (GtkWidget      *widget,
1182                          GdkEventButton *event)
1183 {
1184   GtkButton *button;
1185
1186   if (event->type == GDK_BUTTON_PRESS)
1187     {
1188       button = GTK_BUTTON (widget);
1189
1190       if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget))
1191         gtk_widget_grab_focus (widget);
1192
1193       if (event->button == 1)
1194         gtk_button_pressed (button);
1195     }
1196
1197   return TRUE;
1198 }
1199
1200 static gboolean
1201 gtk_button_button_release (GtkWidget      *widget,
1202                            GdkEventButton *event)
1203 {
1204   GtkButton *button;
1205
1206   if (event->button == 1)
1207     {
1208       button = GTK_BUTTON (widget);
1209       gtk_button_released (button);
1210     }
1211
1212   return TRUE;
1213 }
1214
1215 static gboolean
1216 gtk_button_key_release (GtkWidget   *widget,
1217                         GdkEventKey *event)
1218 {
1219   GtkButton *button = GTK_BUTTON (widget);
1220
1221   if (button->activate_timeout)
1222     {
1223       gtk_button_finish_activate (button, TRUE);
1224       return TRUE;
1225     }
1226   else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
1227     return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1228   else
1229     return FALSE;
1230 }
1231
1232 static gboolean
1233 gtk_button_enter_notify (GtkWidget        *widget,
1234                          GdkEventCrossing *event)
1235 {
1236   GtkButton *button;
1237   GtkWidget *event_widget;
1238
1239   button = GTK_BUTTON (widget);
1240   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1241
1242   if ((event_widget == widget) &&
1243       (event->detail != GDK_NOTIFY_INFERIOR))
1244     {
1245       button->in_button = TRUE;
1246       gtk_button_enter (button);
1247     }
1248
1249   return FALSE;
1250 }
1251
1252 static gboolean
1253 gtk_button_leave_notify (GtkWidget        *widget,
1254                          GdkEventCrossing *event)
1255 {
1256   GtkButton *button;
1257   GtkWidget *event_widget;
1258
1259   button = GTK_BUTTON (widget);
1260   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1261
1262   if ((event_widget == widget) &&
1263       (event->detail != GDK_NOTIFY_INFERIOR))
1264     {
1265       button->in_button = FALSE;
1266       gtk_button_leave (button);
1267     }
1268
1269   return FALSE;
1270 }
1271
1272 static void
1273 gtk_real_button_pressed (GtkButton *button)
1274 {
1275   if (button->activate_timeout)
1276     return;
1277   
1278   button->button_down = TRUE;
1279   gtk_button_update_state (button);
1280 }
1281
1282 static void
1283 gtk_real_button_released (GtkButton *button)
1284 {
1285   if (button->button_down)
1286     {
1287       button->button_down = FALSE;
1288
1289       if (button->activate_timeout)
1290         return;
1291       
1292       if (button->in_button)
1293         gtk_button_clicked (button);
1294
1295       gtk_button_update_state (button);
1296     }
1297 }
1298
1299 static gboolean
1300 button_activate_timeout (gpointer data)
1301 {
1302   GDK_THREADS_ENTER ();
1303   
1304   gtk_button_finish_activate (data, TRUE);
1305
1306   GDK_THREADS_LEAVE ();
1307
1308   return FALSE;
1309 }
1310
1311 static void
1312 gtk_real_button_activate (GtkButton *button)
1313 {
1314   GtkWidget *widget = GTK_WIDGET (button);
1315   
1316   if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1317     {
1318       if (gdk_keyboard_grab (button->event_window, TRUE,
1319                              gtk_get_current_event_time ()) == 0)
1320         {
1321           gtk_grab_add (widget);
1322           
1323           button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1324                                                     button_activate_timeout,
1325                                                     button);
1326           button->button_down = TRUE;
1327           gtk_button_update_state (button);
1328           gtk_widget_queue_draw (GTK_WIDGET (button));
1329         }
1330     }
1331 }
1332
1333 static void
1334 gtk_button_finish_activate (GtkButton *button,
1335                             gboolean   do_it)
1336 {
1337   GtkWidget *widget = GTK_WIDGET (button);
1338   
1339   g_source_remove (button->activate_timeout);
1340   button->activate_timeout = 0;
1341
1342   gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1343                                gtk_get_current_event_time ());
1344   gtk_grab_remove (widget);
1345
1346   button->button_down = FALSE;
1347
1348   gtk_button_update_state (button);
1349   gtk_widget_queue_draw (GTK_WIDGET (button));
1350
1351   if (do_it)
1352     gtk_button_clicked (button);
1353 }
1354
1355 /**
1356  * gtk_button_set_label:
1357  * @button: a #GtkButton
1358  * @label: a string
1359  *
1360  * Sets the text of the label of the button to @str. This text is
1361  * also used to select the stock item if gtk_button_set_use_stock()
1362  * is used.
1363  *
1364  * This will also clear any previously set labels.
1365  **/
1366 void
1367 gtk_button_set_label (GtkButton   *button,
1368                       const gchar *label)
1369 {
1370   gchar *new_label;
1371   
1372   g_return_if_fail (GTK_IS_BUTTON (button));
1373
1374   new_label = g_strdup (label);
1375   g_free (button->label_text);
1376   button->label_text = new_label;
1377   
1378   gtk_button_construct_child (button);
1379   
1380   g_object_notify (G_OBJECT (button), "label");
1381 }
1382
1383 /**
1384  * gtk_button_get_label:
1385  * @button: a #GtkButton
1386  *
1387  * Fetches the text from the label of the button, as set by
1388  * gtk_button_set_label(). If the label text has not 
1389  * been set the return value will be %NULL. This will be the 
1390  * case if you create an empty button with gtk_button_new() to 
1391  * use as a container.
1392  *
1393  * Return value: The text of the label widget. This string is owned
1394  * by the widget and must not be modified or freed.
1395  **/
1396 G_CONST_RETURN gchar *
1397 gtk_button_get_label (GtkButton *button)
1398 {
1399   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1400   
1401   return button->label_text;
1402 }
1403
1404 /**
1405  * gtk_button_set_use_underline:
1406  * @button: a #GtkButton
1407  * @use_underline: %TRUE if underlines in the text indicate mnemonics
1408  *
1409  * If true, an underline in the text of the button label indicates
1410  * the next character should be used for the mnemonic accelerator key.
1411  */
1412 void
1413 gtk_button_set_use_underline (GtkButton *button,
1414                               gboolean   use_underline)
1415 {
1416   g_return_if_fail (GTK_IS_BUTTON (button));
1417
1418   use_underline = use_underline != FALSE;
1419
1420   if (use_underline != button->use_underline)
1421     {
1422       button->use_underline = use_underline;
1423   
1424       gtk_button_construct_child (button);
1425       
1426       g_object_notify (G_OBJECT (button), "use_underline");
1427     }
1428 }
1429
1430 /**
1431  * gtk_button_get_use_underline:
1432  * @button: a #GtkButton
1433  *
1434  * Returns whether an embedded underline in the button label indicates a
1435  * mnemonic. See gtk_button_set_use_underline ().
1436  *
1437  * Return value: %TRUE if an embedded underline in the button label
1438  *               indicates the mnemonic accelerator keys.
1439  **/
1440 gboolean
1441 gtk_button_get_use_underline (GtkButton *button)
1442 {
1443   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1444   
1445   return button->use_underline;
1446 }
1447
1448 /**
1449  * gtk_button_set_use_stock:
1450  * @button: a #GtkButton
1451  * @use_stock: %TRUE if the button should use a stock item
1452  *
1453  * If true, the label set on the button is used as a
1454  * stock id to select the stock item for the button.
1455  */
1456 void
1457 gtk_button_set_use_stock (GtkButton *button,
1458                           gboolean   use_stock)
1459 {
1460   g_return_if_fail (GTK_IS_BUTTON (button));
1461
1462   use_stock = use_stock != FALSE;
1463
1464   if (use_stock != button->use_stock)
1465     {
1466       button->use_stock = use_stock;
1467   
1468       gtk_button_construct_child (button);
1469       
1470       g_object_notify (G_OBJECT (button), "use_stock");
1471     }
1472 }
1473
1474 /**
1475  * gtk_button_get_use_stock:
1476  * @button: a #GtkButton
1477  *
1478  * Returns whether the button label is a stock item.
1479  *
1480  * Return value: %TRUE if the button label is used to
1481  *               select a stock item instead of being
1482  *               used directly as the label text.
1483  */
1484 gboolean
1485 gtk_button_get_use_stock (GtkButton *button)
1486 {
1487   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1488   
1489   return button->use_stock;
1490 }
1491
1492 /**
1493  * gtk_button_set_focus_on_click:
1494  * @button: a #GtkButton
1495  * @focus_on_click: whether the button grabs focus when clicked with the mouse
1496  * 
1497  * Sets whether the button will grab focus when it is clicked with the mouse.
1498  * Making mouse clicks not grab focus is useful in places like toolbars where
1499  * you don't want the keyboard focus removed from the main area of the
1500  * application.
1501  *
1502  * Since: 2.4
1503  **/
1504 void
1505 gtk_button_set_focus_on_click (GtkButton *button,
1506                                gboolean   focus_on_click)
1507 {
1508   g_return_if_fail (GTK_IS_BUTTON (button));
1509   
1510   focus_on_click = focus_on_click != FALSE;
1511
1512   if (button->focus_on_click != focus_on_click)
1513     {
1514       button->focus_on_click = focus_on_click;
1515       
1516       g_object_notify (G_OBJECT (button), "focus_on_click");
1517     }
1518 }
1519
1520 /**
1521  * gtk_button_get_focus_on_click:
1522  * @button: a #GtkButton
1523  * 
1524  * Returns whether the button grabs focus when it is clicked with the mouse.
1525  * See gtk_button_set_focus_on_click().
1526  *
1527  * Return value: %TRUE if the button grabs focus when it is clicked with
1528  *               the mouse.
1529  *
1530  * Since: 2.4
1531  **/
1532 gboolean
1533 gtk_button_get_focus_on_click (GtkButton *button)
1534 {
1535   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1536   
1537   return button->focus_on_click;
1538 }
1539
1540 /**
1541  * gtk_button_set_alignment:
1542  * @button: a #GtkButton
1543  * @xalign: the horizontal position of the child, 0.0 is left aligned, 
1544  *   1.0 is right aligned
1545  * @yalign: the vertical position of the child, 0.0 is top aligned, 
1546  *   1.0 is bottom aligned
1547  *
1548  * Sets the alignment of the child. This property has no effect unless 
1549  * the child is a #GtkMisc or a #GtkAligment.
1550  *
1551  * Since: 2.4
1552  */
1553 void
1554 gtk_button_set_alignment (GtkButton *button,
1555                           gfloat     xalign,
1556                           gfloat     yalign)
1557 {
1558   GtkButtonPrivate *priv;
1559
1560   g_return_if_fail (GTK_IS_BUTTON (button));
1561   
1562   priv = GTK_BUTTON_GET_PRIVATE (button);
1563
1564   priv->xalign = xalign;
1565   priv->yalign = yalign;
1566   priv->align_set = 1;
1567
1568   maybe_set_alignment (button, GTK_BIN (button)->child);
1569
1570   g_object_freeze_notify (G_OBJECT (button));
1571   g_object_notify (G_OBJECT (button), "xalign");
1572   g_object_notify (G_OBJECT (button), "yalign");
1573   g_object_thaw_notify (G_OBJECT (button));
1574 }
1575
1576 /**
1577  * gtk_button_get_alignment:
1578  * @button: a #GtkButton
1579  * @xalign: return location for horizontal alignment
1580  * @yalign: return location for vertical alignment
1581  *
1582  * Gets the alignment of the child in the button.
1583  *
1584  * Since: 2.4
1585  */
1586 void
1587 gtk_button_get_alignment (GtkButton *button,
1588                           gfloat    *xalign,
1589                           gfloat    *yalign)
1590 {
1591   GtkButtonPrivate *priv;
1592
1593   g_return_if_fail (GTK_IS_BUTTON (button));
1594   
1595   priv = GTK_BUTTON_GET_PRIVATE (button);
1596  
1597   if (xalign) 
1598     *xalign = priv->xalign;
1599
1600   if (yalign)
1601     *yalign = priv->yalign;
1602 }
1603
1604 /**
1605  * _gtk_button_set_depressed:
1606  * @button: a #GtkButton
1607  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1608  * 
1609  * Sets whether the button is currently drawn as down or not. This is 
1610  * purely a visual setting, and is meant only for use by derived widgets
1611  * such as #GtkToggleButton.
1612  **/
1613 void
1614 _gtk_button_set_depressed (GtkButton *button,
1615                            gboolean   depressed)
1616 {
1617   GtkWidget *widget = GTK_WIDGET (button);
1618
1619   depressed = depressed != FALSE;
1620
1621   if (depressed != button->depressed)
1622     {
1623       button->depressed = depressed;
1624       gtk_widget_queue_resize (widget);
1625     }
1626 }
1627
1628 static void
1629 gtk_button_update_state (GtkButton *button)
1630 {
1631   gboolean depressed;
1632   GtkStateType new_state;
1633
1634   if (button->activate_timeout)
1635     depressed = button->depress_on_activate;
1636   else
1637     depressed = button->in_button && button->button_down;
1638
1639   if (button->in_button && (!button->button_down || !depressed))
1640     new_state = GTK_STATE_PRELIGHT;
1641   else
1642     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1643
1644   _gtk_button_set_depressed (button, depressed); 
1645   gtk_widget_set_state (GTK_WIDGET (button), new_state);
1646 }
1647
1648 static void 
1649 show_image_change_notify (GtkButton *button)
1650 {
1651   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1652
1653   if (priv->image) 
1654     {
1655       if (show_image (button))
1656         gtk_widget_show (priv->image);
1657       else
1658         gtk_widget_hide (priv->image);
1659     }
1660 }
1661
1662 static void
1663 traverse_container (GtkWidget *widget,
1664                     gpointer   data)
1665 {
1666   if (GTK_IS_BUTTON (widget))
1667     show_image_change_notify (GTK_BUTTON (widget));
1668   else if (GTK_IS_CONTAINER (widget))
1669     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1670 }
1671
1672 static void
1673 gtk_button_setting_changed (GtkSettings *settings)
1674 {
1675   GList *list, *l;
1676
1677   list = gtk_window_list_toplevels ();
1678
1679   for (l = list; l; l = l->next)
1680     gtk_container_forall (GTK_CONTAINER (l->data), 
1681                           traverse_container, NULL);
1682
1683   g_list_free (list);
1684 }
1685
1686
1687 static void
1688 gtk_button_screen_changed (GtkWidget *widget,
1689                            GdkScreen *previous_screen)
1690 {
1691   GtkSettings *settings;
1692   guint show_image_connection;
1693
1694   if (!gtk_widget_has_screen (widget))
1695     return;
1696
1697   settings = gtk_widget_get_settings (widget);
1698
1699   show_image_connection = 
1700     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
1701                                          "gtk-button-connection"));
1702   
1703   if (show_image_connection)
1704     return;
1705
1706   show_image_connection =
1707     g_signal_connect (settings, "notify::gtk-button-images",
1708                       G_CALLBACK (gtk_button_setting_changed), 0);
1709   g_object_set_data (G_OBJECT (settings), 
1710                      "gtk-button-connection",
1711                      GUINT_TO_POINTER (show_image_connection));
1712
1713   show_image_change_notify (GTK_BUTTON (widget));
1714 }
1715
1716 static void
1717 gtk_button_state_changed (GtkWidget    *widget,
1718                           GtkStateType  previous_state)
1719 {
1720   GtkButton *button = GTK_BUTTON (widget);
1721
1722   if (!GTK_WIDGET_IS_SENSITIVE (widget))
1723     {
1724       button->in_button = FALSE;
1725       gtk_real_button_released (button);
1726     }
1727 }
1728
1729 static void
1730 gtk_button_grab_notify (GtkWidget *widget,
1731                         gboolean   was_grabbed)
1732 {
1733   GtkButton *button = GTK_BUTTON (widget);
1734
1735   if (!was_grabbed)
1736     {
1737       button->in_button = FALSE;
1738       gtk_real_button_released (button);
1739     }
1740 }
1741
1742 /**
1743  * gtk_button_set_image:
1744  * @button: a #GtkButton
1745  * @image: a widget to set as the image for the button
1746  *
1747  * Set the image of @button to the given widget. Note that
1748  * it depends on the show-button-images setting whether the
1749  * image will be displayed or not.
1750  *
1751  * Since: 2.6
1752  */ 
1753 void
1754 gtk_button_set_image (GtkButton *button,
1755                       GtkWidget *image)
1756 {
1757   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1758
1759   priv->image = image;
1760   priv->image_is_stock = FALSE;
1761
1762   gtk_button_construct_child (button);
1763
1764   g_object_notify (G_OBJECT (button), "image");
1765 }
1766
1767 /**
1768  * gtk_button_get_image:
1769  * @button: a #GtkButton
1770  *
1771  * Gets the widget that is currenty set as the image of @button.
1772  * This may have been explicitly set by gtk_button_set_image()
1773  * or constructed by gtk_button_new_from_stock().
1774  *
1775  * Since: 2.6
1776  */
1777 GtkWidget *
1778 gtk_button_get_image (GtkButton *button)
1779 {
1780   GtkButtonPrivate *priv;
1781
1782   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1783
1784   priv = GTK_BUTTON_GET_PRIVATE (button);
1785   
1786   return priv->image;
1787 }
1788   
1789