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