]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
Allow unsetting the custom image by passing NULL. (gtk_button_init):
[~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   priv->image_is_stock = TRUE;
449 }
450
451 static void
452 gtk_button_destroy (GtkObject *object)
453 {
454   GtkButton *button = GTK_BUTTON (object);
455   
456   if (button->label_text)
457     {
458       g_free (button->label_text);
459       button->label_text = NULL;
460     }
461   
462   (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
463 }
464
465 static GObject*
466 gtk_button_constructor (GType                  type,
467                         guint                  n_construct_properties,
468                         GObjectConstructParam *construct_params)
469 {
470   GObject *object;
471   GtkButton *button;
472
473   object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
474                                                            n_construct_properties,
475                                                            construct_params);
476
477   button = GTK_BUTTON (object);
478   button->constructed = TRUE;
479
480   if (button->label_text != NULL)
481     gtk_button_construct_child (button);
482   
483   return object;
484 }
485
486
487 static GType
488 gtk_button_child_type  (GtkContainer     *container)
489 {
490   if (!GTK_BIN (container)->child)
491     return GTK_TYPE_WIDGET;
492   else
493     return G_TYPE_NONE;
494 }
495
496 static void
497 maybe_set_alignment (GtkButton *button,
498                      GtkWidget *widget)
499 {
500   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
501
502   if (GTK_IS_MISC (widget))
503     {
504       GtkMisc *misc = GTK_MISC (widget);
505       
506       if (priv->align_set)
507         gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
508     }
509   else if (GTK_IS_ALIGNMENT (widget))
510     {
511       GtkAlignment *alignment = GTK_ALIGNMENT (widget);
512
513       if (priv->align_set)
514         gtk_alignment_set (alignment, priv->xalign, priv->yalign, 
515                            alignment->xscale, alignment->yscale);
516     }
517 }
518
519 static void
520 gtk_button_add (GtkContainer *container,
521                 GtkWidget    *widget)
522 {
523   maybe_set_alignment (GTK_BUTTON (container), widget);
524
525   GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
526 }
527
528 static void
529 gtk_button_set_property (GObject         *object,
530                          guint            prop_id,
531                          const GValue    *value,
532                          GParamSpec      *pspec)
533 {
534   GtkButton *button = GTK_BUTTON (object);
535   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
536
537   switch (prop_id)
538     {
539     case PROP_LABEL:
540       gtk_button_set_label (button, g_value_get_string (value));
541       break;
542     case PROP_IMAGE:
543       gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
544       break;
545     case PROP_RELIEF:
546       gtk_button_set_relief (button, g_value_get_enum (value));
547       break;
548     case PROP_USE_UNDERLINE:
549       gtk_button_set_use_underline (button, g_value_get_boolean (value));
550       break;
551     case PROP_USE_STOCK:
552       gtk_button_set_use_stock (button, g_value_get_boolean (value));
553       break;
554     case PROP_FOCUS_ON_CLICK:
555       gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
556       break;
557     case PROP_XALIGN:
558       gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
559       break;
560     case PROP_YALIGN:
561       gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
562       break;
563     default:
564       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
565       break;
566     }
567 }
568
569 static void
570 gtk_button_get_property (GObject         *object,
571                          guint            prop_id,
572                          GValue          *value,
573                          GParamSpec      *pspec)
574 {
575   GtkButton *button = GTK_BUTTON (object);
576   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
577
578   switch (prop_id)
579     {
580     case PROP_LABEL:
581       g_value_set_string (value, button->label_text);
582       break;
583     case PROP_IMAGE:
584       g_value_set_object (value, (GObject *)priv->image);
585       break;
586     case PROP_RELIEF:
587       g_value_set_enum (value, gtk_button_get_relief (button));
588       break;
589     case PROP_USE_UNDERLINE:
590       g_value_set_boolean (value, button->use_underline);
591       break;
592     case PROP_USE_STOCK:
593       g_value_set_boolean (value, button->use_stock);
594       break;
595     case PROP_FOCUS_ON_CLICK:
596       g_value_set_boolean (value, button->focus_on_click);
597       break;
598     case PROP_XALIGN:
599       g_value_set_float (value, priv->xalign);
600       break;
601     case PROP_YALIGN:
602       g_value_set_float (value, priv->yalign);
603       break;
604     default:
605       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
606       break;
607     }
608 }
609
610 GtkWidget*
611 gtk_button_new (void)
612 {
613   return g_object_new (GTK_TYPE_BUTTON, NULL);
614 }
615
616 static gboolean
617 show_image (GtkButton *button)
618 {
619   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (button));  
620   gboolean show;
621
622   g_object_get (settings, "gtk-button-images", &show, NULL);
623
624   return show;
625 }
626
627 static void
628 gtk_button_construct_child (GtkButton *button)
629 {
630   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
631   GtkStockItem item;
632   GtkWidget *label;
633   GtkWidget *hbox;
634   GtkWidget *align;
635   GtkWidget *image = NULL;
636   gchar *label_text = NULL;
637   
638   if (!button->constructed)
639     return;
640   
641   if (button->label_text == NULL)
642     return;
643
644   if (GTK_BIN (button)->child)
645     {
646       if (priv->image && !priv->image_is_stock)
647         image = g_object_ref (priv->image);
648
649       gtk_container_remove (GTK_CONTAINER (button),
650                             GTK_BIN (button)->child);
651   
652       priv->image = NULL;
653     }
654   
655   if (button->use_stock &&
656       gtk_stock_lookup (button->label_text, &item))
657     {
658       if (!image)
659         image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
660
661       label_text = item.label;
662     }
663   else
664     label_text = button->label_text;
665
666   if (image)
667     {
668       label = gtk_label_new_with_mnemonic (label_text);
669       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
670       
671       priv->image = image;
672
673       g_object_set (priv->image, 
674                     "visible", show_image (button),
675                     "no_show_all", TRUE,
676                     NULL);
677       hbox = gtk_hbox_new (FALSE, 2);
678
679       if (priv->align_set)
680         align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
681       else
682         align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
683         
684       gtk_box_pack_start (GTK_BOX (hbox), priv->image, FALSE, FALSE, 0);
685       gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
686       
687       gtk_container_add (GTK_CONTAINER (button), align);
688       gtk_container_add (GTK_CONTAINER (align), hbox);
689       gtk_widget_show_all (align);
690
691       g_object_unref (image);
692
693       return;
694     }
695   
696  if (button->use_underline)
697     {
698       label = gtk_label_new_with_mnemonic (button->label_text);
699       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
700     }
701   else
702     label = gtk_label_new (button->label_text);
703   
704   if (priv->align_set)
705     gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
706
707   gtk_widget_show (label);
708   gtk_container_add (GTK_CONTAINER (button), label);
709 }
710
711
712 GtkWidget*
713 gtk_button_new_with_label (const gchar *label)
714 {
715   return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
716 }
717
718 /**
719  * gtk_button_new_from_stock:
720  * @stock_id: the name of the stock item 
721  *
722  * Creates a new #GtkButton containing the image and text from a stock item.
723  * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
724  * #GTK_STOCK_APPLY.
725  *
726  * If @stock_id is unknown, then it will be treated as a mnemonic
727  * label (as for gtk_button_new_with_mnemonic()).
728  *
729  * Returns: a new #GtkButton
730  **/
731 GtkWidget*
732 gtk_button_new_from_stock (const gchar *stock_id)
733 {
734   return g_object_new (GTK_TYPE_BUTTON,
735                        "label", stock_id,
736                        "use_stock", TRUE,
737                        "use_underline", TRUE,
738                        NULL);
739 }
740
741 /**
742  * gtk_button_new_with_mnemonic:
743  * @label: The text of the button, with an underscore in front of the
744  *         mnemonic character
745  * @returns: a new #GtkButton
746  *
747  * Creates a new #GtkButton containing a label.
748  * If characters in @label are preceded by an underscore, they are underlined.
749  * If you need a literal underscore character in a label, use '__' (two 
750  * underscores). The first underlined character represents a keyboard 
751  * accelerator called a mnemonic.
752  * Pressing Alt and that key activates the button.
753  **/
754 GtkWidget*
755 gtk_button_new_with_mnemonic (const gchar *label)
756 {
757   return g_object_new (GTK_TYPE_BUTTON, "label", label, "use_underline", TRUE,  NULL);
758 }
759
760 void
761 gtk_button_pressed (GtkButton *button)
762 {
763   g_return_if_fail (GTK_IS_BUTTON (button));
764
765   
766   g_signal_emit (button, button_signals[PRESSED], 0);
767 }
768
769 void
770 gtk_button_released (GtkButton *button)
771 {
772   g_return_if_fail (GTK_IS_BUTTON (button));
773
774   g_signal_emit (button, button_signals[RELEASED], 0);
775 }
776
777 void
778 gtk_button_clicked (GtkButton *button)
779 {
780   g_return_if_fail (GTK_IS_BUTTON (button));
781
782   g_signal_emit (button, button_signals[CLICKED], 0);
783 }
784
785 void
786 gtk_button_enter (GtkButton *button)
787 {
788   g_return_if_fail (GTK_IS_BUTTON (button));
789
790   g_signal_emit (button, button_signals[ENTER], 0);
791 }
792
793 void
794 gtk_button_leave (GtkButton *button)
795 {
796   g_return_if_fail (GTK_IS_BUTTON (button));
797
798   g_signal_emit (button, button_signals[LEAVE], 0);
799 }
800
801 void
802 gtk_button_set_relief (GtkButton *button,
803                        GtkReliefStyle newrelief)
804 {
805   g_return_if_fail (GTK_IS_BUTTON (button));
806
807   if (newrelief != button->relief) 
808     {
809        button->relief = newrelief;
810        g_object_notify (G_OBJECT (button), "relief");
811        gtk_widget_queue_draw (GTK_WIDGET (button));
812     }
813 }
814
815 GtkReliefStyle
816 gtk_button_get_relief (GtkButton *button)
817 {
818   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
819
820   return button->relief;
821 }
822
823 static void
824 gtk_button_realize (GtkWidget *widget)
825 {
826   GtkButton *button;
827   GdkWindowAttr attributes;
828   gint attributes_mask;
829   gint border_width;
830
831   button = GTK_BUTTON (widget);
832   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
833
834   border_width = GTK_CONTAINER (widget)->border_width;
835
836   attributes.window_type = GDK_WINDOW_CHILD;
837   attributes.x = widget->allocation.x + border_width;
838   attributes.y = widget->allocation.y + border_width;
839   attributes.width = widget->allocation.width - border_width * 2;
840   attributes.height = widget->allocation.height - border_width * 2;
841   attributes.wclass = GDK_INPUT_ONLY;
842   attributes.event_mask = gtk_widget_get_events (widget);
843   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
844                             GDK_BUTTON_RELEASE_MASK |
845                             GDK_ENTER_NOTIFY_MASK |
846                             GDK_LEAVE_NOTIFY_MASK);
847
848   attributes_mask = GDK_WA_X | GDK_WA_Y;
849
850   widget->window = gtk_widget_get_parent_window (widget);
851   g_object_ref (widget->window);
852   
853   button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
854                                          &attributes, attributes_mask);
855   gdk_window_set_user_data (button->event_window, button);
856
857   widget->style = gtk_style_attach (widget->style, widget->window);
858 }
859
860 static void
861 gtk_button_unrealize (GtkWidget *widget)
862 {
863   GtkButton *button = GTK_BUTTON (widget);
864
865   if (button->activate_timeout)
866     gtk_button_finish_activate (button, FALSE);
867
868   if (button->event_window)
869     {
870       gdk_window_set_user_data (button->event_window, NULL);
871       gdk_window_destroy (button->event_window);
872       button->event_window = NULL;
873     }
874   
875   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
876 }
877
878 static void
879 gtk_button_map (GtkWidget *widget)
880 {
881   GtkButton *button = GTK_BUTTON (widget);
882   
883   GTK_WIDGET_CLASS (parent_class)->map (widget);
884
885   if (button->event_window)
886     gdk_window_show (button->event_window);
887 }
888
889 static void
890 gtk_button_unmap (GtkWidget *widget)
891 {
892   GtkButton *button = GTK_BUTTON (widget);
893     
894   if (button->event_window)
895     gdk_window_hide (button->event_window);
896
897   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
898 }
899
900 static void
901 gtk_button_get_props (GtkButton *button,
902                       GtkBorder *default_border,
903                       GtkBorder *default_outside_border,
904                       gboolean  *interior_focus)
905 {
906   GtkWidget *widget =  GTK_WIDGET (button);
907   GtkBorder *tmp_border;
908
909   if (default_border)
910     {
911       gtk_widget_style_get (widget, "default_border", &tmp_border, NULL);
912
913       if (tmp_border)
914         {
915           *default_border = *tmp_border;
916           g_free (tmp_border);
917         }
918       else
919         *default_border = default_default_border;
920     }
921
922   if (default_outside_border)
923     {
924       gtk_widget_style_get (widget, "default_outside_border", &tmp_border, NULL);
925
926       if (tmp_border)
927         {
928           *default_outside_border = *tmp_border;
929           g_free (tmp_border);
930         }
931       else
932         *default_outside_border = default_default_outside_border;
933     }
934
935   if (interior_focus)
936     gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
937 }
938         
939 static void
940 gtk_button_size_request (GtkWidget      *widget,
941                          GtkRequisition *requisition)
942 {
943   GtkButton *button = GTK_BUTTON (widget);
944   GtkBorder default_border;
945   gint focus_width;
946   gint focus_pad;
947
948   gtk_button_get_props (button, &default_border, NULL, NULL);
949   gtk_widget_style_get (GTK_WIDGET (widget),
950                         "focus-line-width", &focus_width,
951                         "focus-padding", &focus_pad,
952                         NULL);
953  
954   requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
955                         GTK_WIDGET (widget)->style->xthickness) * 2;
956   requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
957                          GTK_WIDGET (widget)->style->ythickness) * 2;
958
959   if (GTK_WIDGET_CAN_DEFAULT (widget))
960     {
961       requisition->width += default_border.left + default_border.right;
962       requisition->height += default_border.top + default_border.bottom;
963     }
964
965   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
966     {
967       GtkRequisition child_requisition;
968
969       gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
970
971       requisition->width += child_requisition.width;
972       requisition->height += child_requisition.height;
973     }
974   
975   requisition->width += 2 * (focus_width + focus_pad);
976   requisition->height += 2 * (focus_width + focus_pad);
977 }
978
979 static void
980 gtk_button_size_allocate (GtkWidget     *widget,
981                           GtkAllocation *allocation)
982 {
983   GtkButton *button = GTK_BUTTON (widget);
984   GtkAllocation child_allocation;
985
986   gint border_width = GTK_CONTAINER (widget)->border_width;
987   gint xthickness = GTK_WIDGET (widget)->style->xthickness;
988   gint ythickness = GTK_WIDGET (widget)->style->ythickness;
989   GtkBorder default_border;
990   gint focus_width;
991   gint focus_pad;
992
993   gtk_button_get_props (button, &default_border, NULL, NULL);
994   gtk_widget_style_get (GTK_WIDGET (widget),
995                         "focus-line-width", &focus_width,
996                         "focus-padding", &focus_pad,
997                         NULL);
998  
999                             
1000   widget->allocation = *allocation;
1001
1002   if (GTK_WIDGET_REALIZED (widget))
1003     gdk_window_move_resize (button->event_window,
1004                             widget->allocation.x + border_width,
1005                             widget->allocation.y + border_width,
1006                             widget->allocation.width - border_width * 2,
1007                             widget->allocation.height - border_width * 2);
1008
1009   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
1010     {
1011       child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
1012       child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
1013       
1014       child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
1015                                     border_width * 2);
1016       child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
1017                                      border_width * 2);
1018
1019       if (GTK_WIDGET_CAN_DEFAULT (button))
1020         {
1021           child_allocation.x += default_border.left;
1022           child_allocation.y += default_border.top;
1023           child_allocation.width =  MAX (1, child_allocation.width - default_border.left - default_border.right);
1024           child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
1025         }
1026
1027       if (GTK_WIDGET_CAN_FOCUS (button))
1028         {
1029           child_allocation.x += focus_width + focus_pad;
1030           child_allocation.y += focus_width + focus_pad;
1031           child_allocation.width =  MAX (1, child_allocation.width - (focus_width + focus_pad) * 2);
1032           child_allocation.height = MAX (1, child_allocation.height - (focus_width + focus_pad) * 2);
1033         }
1034
1035       if (button->depressed)
1036         {
1037           gint child_displacement_x;
1038           gint child_displacement_y;
1039           
1040           gtk_widget_style_get (widget,
1041                                 "child_displacement_x", &child_displacement_x, 
1042                                 "child_displacement_y", &child_displacement_y,
1043                                 NULL);
1044           child_allocation.x += child_displacement_x;
1045           child_allocation.y += child_displacement_y;
1046         }
1047
1048       gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
1049     }
1050 }
1051
1052 void
1053 _gtk_button_paint (GtkButton    *button,
1054                    GdkRectangle *area,
1055                    GtkStateType  state_type,
1056                    GtkShadowType shadow_type,
1057                    const gchar  *main_detail,
1058                    const gchar  *default_detail)
1059 {
1060   GtkWidget *widget;
1061   gint width, height;
1062   gint x, y;
1063   gint border_width;
1064   GtkBorder default_border;
1065   GtkBorder default_outside_border;
1066   gboolean interior_focus;
1067   gint focus_width;
1068   gint focus_pad;
1069    
1070   if (GTK_WIDGET_DRAWABLE (button))
1071     {
1072       widget = GTK_WIDGET (button);
1073       border_width = GTK_CONTAINER (widget)->border_width;
1074
1075       gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
1076       gtk_widget_style_get (GTK_WIDGET (widget),
1077                             "focus-line-width", &focus_width,
1078                             "focus-padding", &focus_pad,
1079                             NULL); 
1080         
1081       x = widget->allocation.x + border_width;
1082       y = widget->allocation.y + border_width;
1083       width = widget->allocation.width - border_width * 2;
1084       height = widget->allocation.height - border_width * 2;
1085
1086       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
1087           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1088         {
1089           gtk_paint_box (widget->style, widget->window,
1090                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
1091                          area, widget, "buttondefault",
1092                          x, y, width, height);
1093
1094           x += default_border.left;
1095           y += default_border.top;
1096           width -= default_border.left + default_border.right;
1097           height -= default_border.top + default_border.bottom;
1098         }
1099       else if (GTK_WIDGET_CAN_DEFAULT (widget))
1100         {
1101           x += default_outside_border.left;
1102           y += default_outside_border.top;
1103           width -= default_outside_border.left + default_outside_border.right;
1104           height -= default_outside_border.top + default_outside_border.bottom;
1105         }
1106        
1107       if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
1108         {
1109           x += focus_width + focus_pad;
1110           y += focus_width + focus_pad;
1111           width -= 2 * (focus_width + focus_pad);
1112           height -= 2 * (focus_width + focus_pad);
1113         }
1114
1115       if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1116           GTK_WIDGET_STATE(widget) == GTK_STATE_PRELIGHT)
1117         gtk_paint_box (widget->style, widget->window,
1118                        state_type,
1119                        shadow_type, area, widget, "button",
1120                        x, y, width, height);
1121        
1122       if (GTK_WIDGET_HAS_FOCUS (widget))
1123         {
1124           gint child_displacement_x;
1125           gint child_displacement_y;
1126           gboolean displace_focus;
1127           
1128           gtk_widget_style_get (GTK_WIDGET (widget),
1129                                 "child_displacement_y", &child_displacement_y,
1130                                 "child_displacement_x", &child_displacement_x,
1131                                 "displace_focus", &displace_focus,
1132                                 NULL);
1133
1134           if (interior_focus)
1135             {
1136               x += widget->style->xthickness + focus_pad;
1137               y += widget->style->ythickness + focus_pad;
1138               width -= 2 * (widget->style->xthickness + focus_pad);
1139               height -=  2 * (widget->style->ythickness + focus_pad);
1140             }
1141           else
1142             {
1143               x -= focus_width + focus_pad;
1144               y -= focus_width + focus_pad;
1145               width += 2 * (focus_width + focus_pad);
1146               height += 2 * (focus_width + focus_pad);
1147             }
1148
1149           if (button->depressed && displace_focus)
1150             {
1151               x += child_displacement_x;
1152               y += child_displacement_y;
1153             }
1154
1155           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1156                            area, widget, "button",
1157                            x, y, width, height);
1158         }
1159     }
1160 }
1161
1162 static gboolean
1163 gtk_button_expose (GtkWidget      *widget,
1164                    GdkEventExpose *event)
1165 {
1166   if (GTK_WIDGET_DRAWABLE (widget))
1167     {
1168       GtkButton *button = GTK_BUTTON (widget);
1169       
1170       _gtk_button_paint (button, &event->area,
1171                          GTK_WIDGET_STATE (widget),
1172                          button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1173                          "button", "buttondefault");
1174       
1175       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1176     }
1177   
1178   return FALSE;
1179 }
1180
1181 static gboolean
1182 gtk_button_button_press (GtkWidget      *widget,
1183                          GdkEventButton *event)
1184 {
1185   GtkButton *button;
1186
1187   if (event->type == GDK_BUTTON_PRESS)
1188     {
1189       button = GTK_BUTTON (widget);
1190
1191       if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget))
1192         gtk_widget_grab_focus (widget);
1193
1194       if (event->button == 1)
1195         gtk_button_pressed (button);
1196     }
1197
1198   return TRUE;
1199 }
1200
1201 static gboolean
1202 gtk_button_button_release (GtkWidget      *widget,
1203                            GdkEventButton *event)
1204 {
1205   GtkButton *button;
1206
1207   if (event->button == 1)
1208     {
1209       button = GTK_BUTTON (widget);
1210       gtk_button_released (button);
1211     }
1212
1213   return TRUE;
1214 }
1215
1216 static gboolean
1217 gtk_button_key_release (GtkWidget   *widget,
1218                         GdkEventKey *event)
1219 {
1220   GtkButton *button = GTK_BUTTON (widget);
1221
1222   if (button->activate_timeout)
1223     {
1224       gtk_button_finish_activate (button, TRUE);
1225       return TRUE;
1226     }
1227   else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
1228     return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
1229   else
1230     return FALSE;
1231 }
1232
1233 static gboolean
1234 gtk_button_enter_notify (GtkWidget        *widget,
1235                          GdkEventCrossing *event)
1236 {
1237   GtkButton *button;
1238   GtkWidget *event_widget;
1239
1240   button = GTK_BUTTON (widget);
1241   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1242
1243   if ((event_widget == widget) &&
1244       (event->detail != GDK_NOTIFY_INFERIOR))
1245     {
1246       button->in_button = TRUE;
1247       gtk_button_enter (button);
1248     }
1249
1250   return FALSE;
1251 }
1252
1253 static gboolean
1254 gtk_button_leave_notify (GtkWidget        *widget,
1255                          GdkEventCrossing *event)
1256 {
1257   GtkButton *button;
1258   GtkWidget *event_widget;
1259
1260   button = GTK_BUTTON (widget);
1261   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1262
1263   if ((event_widget == widget) &&
1264       (event->detail != GDK_NOTIFY_INFERIOR))
1265     {
1266       button->in_button = FALSE;
1267       gtk_button_leave (button);
1268     }
1269
1270   return FALSE;
1271 }
1272
1273 static void
1274 gtk_real_button_pressed (GtkButton *button)
1275 {
1276   if (button->activate_timeout)
1277     return;
1278   
1279   button->button_down = TRUE;
1280   gtk_button_update_state (button);
1281 }
1282
1283 static void
1284 gtk_real_button_released (GtkButton *button)
1285 {
1286   if (button->button_down)
1287     {
1288       button->button_down = FALSE;
1289
1290       if (button->activate_timeout)
1291         return;
1292       
1293       if (button->in_button)
1294         gtk_button_clicked (button);
1295
1296       gtk_button_update_state (button);
1297     }
1298 }
1299
1300 static gboolean
1301 button_activate_timeout (gpointer data)
1302 {
1303   GDK_THREADS_ENTER ();
1304   
1305   gtk_button_finish_activate (data, TRUE);
1306
1307   GDK_THREADS_LEAVE ();
1308
1309   return FALSE;
1310 }
1311
1312 static void
1313 gtk_real_button_activate (GtkButton *button)
1314 {
1315   GtkWidget *widget = GTK_WIDGET (button);
1316   
1317   if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1318     {
1319       if (gdk_keyboard_grab (button->event_window, TRUE,
1320                              gtk_get_current_event_time ()) == 0)
1321         {
1322           gtk_grab_add (widget);
1323           
1324           button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1325                                                     button_activate_timeout,
1326                                                     button);
1327           button->button_down = TRUE;
1328           gtk_button_update_state (button);
1329           gtk_widget_queue_draw (GTK_WIDGET (button));
1330         }
1331     }
1332 }
1333
1334 static void
1335 gtk_button_finish_activate (GtkButton *button,
1336                             gboolean   do_it)
1337 {
1338   GtkWidget *widget = GTK_WIDGET (button);
1339   
1340   g_source_remove (button->activate_timeout);
1341   button->activate_timeout = 0;
1342
1343   gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1344                                gtk_get_current_event_time ());
1345   gtk_grab_remove (widget);
1346
1347   button->button_down = FALSE;
1348
1349   gtk_button_update_state (button);
1350   gtk_widget_queue_draw (GTK_WIDGET (button));
1351
1352   if (do_it)
1353     gtk_button_clicked (button);
1354 }
1355
1356 /**
1357  * gtk_button_set_label:
1358  * @button: a #GtkButton
1359  * @label: a string
1360  *
1361  * Sets the text of the label of the button to @str. This text is
1362  * also used to select the stock item if gtk_button_set_use_stock()
1363  * is used.
1364  *
1365  * This will also clear any previously set labels.
1366  **/
1367 void
1368 gtk_button_set_label (GtkButton   *button,
1369                       const gchar *label)
1370 {
1371   gchar *new_label;
1372   
1373   g_return_if_fail (GTK_IS_BUTTON (button));
1374
1375   new_label = g_strdup (label);
1376   g_free (button->label_text);
1377   button->label_text = new_label;
1378   
1379   gtk_button_construct_child (button);
1380   
1381   g_object_notify (G_OBJECT (button), "label");
1382 }
1383
1384 /**
1385  * gtk_button_get_label:
1386  * @button: a #GtkButton
1387  *
1388  * Fetches the text from the label of the button, as set by
1389  * gtk_button_set_label(). If the label text has not 
1390  * been set the return value will be %NULL. This will be the 
1391  * case if you create an empty button with gtk_button_new() to 
1392  * use as a container.
1393  *
1394  * Return value: The text of the label widget. This string is owned
1395  * by the widget and must not be modified or freed.
1396  **/
1397 G_CONST_RETURN gchar *
1398 gtk_button_get_label (GtkButton *button)
1399 {
1400   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1401   
1402   return button->label_text;
1403 }
1404
1405 /**
1406  * gtk_button_set_use_underline:
1407  * @button: a #GtkButton
1408  * @use_underline: %TRUE if underlines in the text indicate mnemonics
1409  *
1410  * If true, an underline in the text of the button label indicates
1411  * the next character should be used for the mnemonic accelerator key.
1412  */
1413 void
1414 gtk_button_set_use_underline (GtkButton *button,
1415                               gboolean   use_underline)
1416 {
1417   g_return_if_fail (GTK_IS_BUTTON (button));
1418
1419   use_underline = use_underline != FALSE;
1420
1421   if (use_underline != button->use_underline)
1422     {
1423       button->use_underline = use_underline;
1424   
1425       gtk_button_construct_child (button);
1426       
1427       g_object_notify (G_OBJECT (button), "use_underline");
1428     }
1429 }
1430
1431 /**
1432  * gtk_button_get_use_underline:
1433  * @button: a #GtkButton
1434  *
1435  * Returns whether an embedded underline in the button label indicates a
1436  * mnemonic. See gtk_button_set_use_underline ().
1437  *
1438  * Return value: %TRUE if an embedded underline in the button label
1439  *               indicates the mnemonic accelerator keys.
1440  **/
1441 gboolean
1442 gtk_button_get_use_underline (GtkButton *button)
1443 {
1444   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1445   
1446   return button->use_underline;
1447 }
1448
1449 /**
1450  * gtk_button_set_use_stock:
1451  * @button: a #GtkButton
1452  * @use_stock: %TRUE if the button should use a stock item
1453  *
1454  * If true, the label set on the button is used as a
1455  * stock id to select the stock item for the button.
1456  */
1457 void
1458 gtk_button_set_use_stock (GtkButton *button,
1459                           gboolean   use_stock)
1460 {
1461   g_return_if_fail (GTK_IS_BUTTON (button));
1462
1463   use_stock = use_stock != FALSE;
1464
1465   if (use_stock != button->use_stock)
1466     {
1467       button->use_stock = use_stock;
1468   
1469       gtk_button_construct_child (button);
1470       
1471       g_object_notify (G_OBJECT (button), "use_stock");
1472     }
1473 }
1474
1475 /**
1476  * gtk_button_get_use_stock:
1477  * @button: a #GtkButton
1478  *
1479  * Returns whether the button label is a stock item.
1480  *
1481  * Return value: %TRUE if the button label is used to
1482  *               select a stock item instead of being
1483  *               used directly as the label text.
1484  */
1485 gboolean
1486 gtk_button_get_use_stock (GtkButton *button)
1487 {
1488   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1489   
1490   return button->use_stock;
1491 }
1492
1493 /**
1494  * gtk_button_set_focus_on_click:
1495  * @button: a #GtkButton
1496  * @focus_on_click: whether the button grabs focus when clicked with the mouse
1497  * 
1498  * Sets whether the button will grab focus when it is clicked with the mouse.
1499  * Making mouse clicks not grab focus is useful in places like toolbars where
1500  * you don't want the keyboard focus removed from the main area of the
1501  * application.
1502  *
1503  * Since: 2.4
1504  **/
1505 void
1506 gtk_button_set_focus_on_click (GtkButton *button,
1507                                gboolean   focus_on_click)
1508 {
1509   g_return_if_fail (GTK_IS_BUTTON (button));
1510   
1511   focus_on_click = focus_on_click != FALSE;
1512
1513   if (button->focus_on_click != focus_on_click)
1514     {
1515       button->focus_on_click = focus_on_click;
1516       
1517       g_object_notify (G_OBJECT (button), "focus_on_click");
1518     }
1519 }
1520
1521 /**
1522  * gtk_button_get_focus_on_click:
1523  * @button: a #GtkButton
1524  * 
1525  * Returns whether the button grabs focus when it is clicked with the mouse.
1526  * See gtk_button_set_focus_on_click().
1527  *
1528  * Return value: %TRUE if the button grabs focus when it is clicked with
1529  *               the mouse.
1530  *
1531  * Since: 2.4
1532  **/
1533 gboolean
1534 gtk_button_get_focus_on_click (GtkButton *button)
1535 {
1536   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1537   
1538   return button->focus_on_click;
1539 }
1540
1541 /**
1542  * gtk_button_set_alignment:
1543  * @button: a #GtkButton
1544  * @xalign: the horizontal position of the child, 0.0 is left aligned, 
1545  *   1.0 is right aligned
1546  * @yalign: the vertical position of the child, 0.0 is top aligned, 
1547  *   1.0 is bottom aligned
1548  *
1549  * Sets the alignment of the child. This property has no effect unless 
1550  * the child is a #GtkMisc or a #GtkAligment.
1551  *
1552  * Since: 2.4
1553  */
1554 void
1555 gtk_button_set_alignment (GtkButton *button,
1556                           gfloat     xalign,
1557                           gfloat     yalign)
1558 {
1559   GtkButtonPrivate *priv;
1560
1561   g_return_if_fail (GTK_IS_BUTTON (button));
1562   
1563   priv = GTK_BUTTON_GET_PRIVATE (button);
1564
1565   priv->xalign = xalign;
1566   priv->yalign = yalign;
1567   priv->align_set = 1;
1568
1569   maybe_set_alignment (button, GTK_BIN (button)->child);
1570
1571   g_object_freeze_notify (G_OBJECT (button));
1572   g_object_notify (G_OBJECT (button), "xalign");
1573   g_object_notify (G_OBJECT (button), "yalign");
1574   g_object_thaw_notify (G_OBJECT (button));
1575 }
1576
1577 /**
1578  * gtk_button_get_alignment:
1579  * @button: a #GtkButton
1580  * @xalign: return location for horizontal alignment
1581  * @yalign: return location for vertical alignment
1582  *
1583  * Gets the alignment of the child in the button.
1584  *
1585  * Since: 2.4
1586  */
1587 void
1588 gtk_button_get_alignment (GtkButton *button,
1589                           gfloat    *xalign,
1590                           gfloat    *yalign)
1591 {
1592   GtkButtonPrivate *priv;
1593
1594   g_return_if_fail (GTK_IS_BUTTON (button));
1595   
1596   priv = GTK_BUTTON_GET_PRIVATE (button);
1597  
1598   if (xalign) 
1599     *xalign = priv->xalign;
1600
1601   if (yalign)
1602     *yalign = priv->yalign;
1603 }
1604
1605 /**
1606  * _gtk_button_set_depressed:
1607  * @button: a #GtkButton
1608  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1609  * 
1610  * Sets whether the button is currently drawn as down or not. This is 
1611  * purely a visual setting, and is meant only for use by derived widgets
1612  * such as #GtkToggleButton.
1613  **/
1614 void
1615 _gtk_button_set_depressed (GtkButton *button,
1616                            gboolean   depressed)
1617 {
1618   GtkWidget *widget = GTK_WIDGET (button);
1619
1620   depressed = depressed != FALSE;
1621
1622   if (depressed != button->depressed)
1623     {
1624       button->depressed = depressed;
1625       gtk_widget_queue_resize (widget);
1626     }
1627 }
1628
1629 static void
1630 gtk_button_update_state (GtkButton *button)
1631 {
1632   gboolean depressed;
1633   GtkStateType new_state;
1634
1635   if (button->activate_timeout)
1636     depressed = button->depress_on_activate;
1637   else
1638     depressed = button->in_button && button->button_down;
1639
1640   if (button->in_button && (!button->button_down || !depressed))
1641     new_state = GTK_STATE_PRELIGHT;
1642   else
1643     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1644
1645   _gtk_button_set_depressed (button, depressed); 
1646   gtk_widget_set_state (GTK_WIDGET (button), new_state);
1647 }
1648
1649 static void 
1650 show_image_change_notify (GtkButton *button)
1651 {
1652   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1653
1654   if (priv->image) 
1655     {
1656       if (show_image (button))
1657         gtk_widget_show (priv->image);
1658       else
1659         gtk_widget_hide (priv->image);
1660     }
1661 }
1662
1663 static void
1664 traverse_container (GtkWidget *widget,
1665                     gpointer   data)
1666 {
1667   if (GTK_IS_BUTTON (widget))
1668     show_image_change_notify (GTK_BUTTON (widget));
1669   else if (GTK_IS_CONTAINER (widget))
1670     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1671 }
1672
1673 static void
1674 gtk_button_setting_changed (GtkSettings *settings)
1675 {
1676   GList *list, *l;
1677
1678   list = gtk_window_list_toplevels ();
1679
1680   for (l = list; l; l = l->next)
1681     gtk_container_forall (GTK_CONTAINER (l->data), 
1682                           traverse_container, NULL);
1683
1684   g_list_free (list);
1685 }
1686
1687
1688 static void
1689 gtk_button_screen_changed (GtkWidget *widget,
1690                            GdkScreen *previous_screen)
1691 {
1692   GtkSettings *settings;
1693   guint show_image_connection;
1694
1695   if (!gtk_widget_has_screen (widget))
1696     return;
1697
1698   settings = gtk_widget_get_settings (widget);
1699
1700   show_image_connection = 
1701     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
1702                                          "gtk-button-connection"));
1703   
1704   if (show_image_connection)
1705     return;
1706
1707   show_image_connection =
1708     g_signal_connect (settings, "notify::gtk-button-images",
1709                       G_CALLBACK (gtk_button_setting_changed), 0);
1710   g_object_set_data (G_OBJECT (settings), 
1711                      "gtk-button-connection",
1712                      GUINT_TO_POINTER (show_image_connection));
1713
1714   show_image_change_notify (GTK_BUTTON (widget));
1715 }
1716
1717 static void
1718 gtk_button_state_changed (GtkWidget    *widget,
1719                           GtkStateType  previous_state)
1720 {
1721   GtkButton *button = GTK_BUTTON (widget);
1722
1723   if (!GTK_WIDGET_IS_SENSITIVE (widget))
1724     {
1725       button->in_button = FALSE;
1726       gtk_real_button_released (button);
1727     }
1728 }
1729
1730 static void
1731 gtk_button_grab_notify (GtkWidget *widget,
1732                         gboolean   was_grabbed)
1733 {
1734   GtkButton *button = GTK_BUTTON (widget);
1735
1736   if (!was_grabbed)
1737     {
1738       button->in_button = FALSE;
1739       gtk_real_button_released (button);
1740     }
1741 }
1742
1743 /**
1744  * gtk_button_set_image:
1745  * @button: a #GtkButton
1746  * @image: a widget to set as the image for the button
1747  *
1748  * Set the image of @button to the given widget. Note that
1749  * it depends on the show-button-images setting whether the
1750  * image will be displayed or not.
1751  *
1752  * Since: 2.6
1753  */ 
1754 void
1755 gtk_button_set_image (GtkButton *button,
1756                       GtkWidget *image)
1757 {
1758   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1759
1760   priv->image = image;
1761   priv->image_is_stock = (image == NULL);
1762
1763   gtk_button_construct_child (button);
1764
1765   g_object_notify (G_OBJECT (button), "image");
1766 }
1767
1768 /**
1769  * gtk_button_get_image:
1770  * @button: a #GtkButton
1771  *
1772  * Gets the widget that is currenty set as the image of @button.
1773  * This may have been explicitly set by gtk_button_set_image()
1774  * or constructed by gtk_button_new_from_stock().
1775  *
1776  * Since: 2.6
1777  */
1778 GtkWidget *
1779 gtk_button_get_image (GtkButton *button)
1780 {
1781   GtkButtonPrivate *priv;
1782
1783   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1784
1785   priv = GTK_BUTTON_GET_PRIVATE (button);
1786   
1787   return priv->image;
1788 }
1789   
1790