]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
Deprecation cleanup
[~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 <string.h>
28 #include "gtkalignment.h"
29 #include "gtkbutton.h"
30 #include "gtklabel.h"
31 #include "gtkmain.h"
32 #include "gtkmarshalers.h"
33 #include "gtksignal.h"
34 #include "gtkimage.h"
35 #include "gtkhbox.h"
36 #include "gtkstock.h"
37 #include "gtkiconfactory.h"
38 #include "gtkintl.h"
39
40 #define CHILD_SPACING     1
41
42 static GtkBorder default_default_border = { 1, 1, 1, 1 };
43 static GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
44
45 /* Time out before giving up on getting a key release when animatng
46  * the close button.
47  */
48 #define ACTIVATE_TIMEOUT 250
49
50 enum {
51   PRESSED,
52   RELEASED,
53   CLICKED,
54   ENTER,
55   LEAVE,
56   ACTIVATE,
57   LAST_SIGNAL
58 };
59
60 enum {
61   PROP_0,
62   PROP_LABEL,
63   PROP_RELIEF,
64   PROP_USE_UNDERLINE,
65   PROP_USE_STOCK
66 };
67
68 static void gtk_button_class_init     (GtkButtonClass   *klass);
69 static void gtk_button_init           (GtkButton        *button);
70 static void gtk_button_destroy        (GtkObject        *object);
71 static void gtk_button_set_property   (GObject         *object,
72                                        guint            prop_id,
73                                        const GValue    *value,
74                                        GParamSpec      *pspec);
75 static void gtk_button_get_property   (GObject         *object,
76                                        guint            prop_id,
77                                        GValue          *value,
78                                        GParamSpec      *pspec);
79 static void gtk_button_realize        (GtkWidget        *widget);
80 static void gtk_button_unrealize      (GtkWidget        *widget);
81 static void gtk_button_map            (GtkWidget        *widget);
82 static void gtk_button_unmap          (GtkWidget        *widget);
83 static void gtk_button_size_request   (GtkWidget        *widget,
84                                        GtkRequisition   *requisition);
85 static void gtk_button_size_allocate  (GtkWidget        *widget,
86                                        GtkAllocation    *allocation);
87 static gint gtk_button_expose         (GtkWidget        *widget,
88                                        GdkEventExpose   *event);
89 static gint gtk_button_button_press   (GtkWidget        *widget,
90                                        GdkEventButton   *event);
91 static gint gtk_button_button_release (GtkWidget        *widget,
92                                        GdkEventButton   *event);
93 static gint gtk_button_key_release    (GtkWidget        *widget,
94                                        GdkEventKey      *event);
95 static gint gtk_button_enter_notify   (GtkWidget        *widget,
96                                        GdkEventCrossing *event);
97 static gint gtk_button_leave_notify   (GtkWidget        *widget,
98                                        GdkEventCrossing *event);
99 static void gtk_real_button_pressed   (GtkButton        *button);
100 static void gtk_real_button_released  (GtkButton        *button);
101 static void gtk_real_button_activate (GtkButton         *button);
102 static void gtk_button_update_state   (GtkButton        *button);
103 static GtkType gtk_button_child_type  (GtkContainer     *container);
104 static void gtk_button_finish_activate (GtkButton *button,
105                                         gboolean   do_it);
106
107 static GObject* gtk_button_constructor     (GType                  type,
108                                             guint                  n_construct_properties,
109                                             GObjectConstructParam *construct_params);
110 static void     gtk_button_construct_child (GtkButton             *button);
111
112
113 static GtkBinClass *parent_class = NULL;
114 static guint button_signals[LAST_SIGNAL] = { 0 };
115
116
117 GtkType
118 gtk_button_get_type (void)
119 {
120   static GtkType button_type = 0;
121
122   if (!button_type)
123     {
124       static const GTypeInfo button_info =
125       {
126         sizeof (GtkButtonClass),
127         NULL,           /* base_init */
128         NULL,           /* base_finalize */
129         (GClassInitFunc) gtk_button_class_init,
130         NULL,           /* class_finalize */
131         NULL,           /* class_data */
132         sizeof (GtkButton),
133         16,             /* n_preallocs */
134         (GInstanceInitFunc) gtk_button_init,
135       };
136
137       button_type = g_type_register_static (GTK_TYPE_BIN, "GtkButton", &button_info, 0);
138     }
139
140   return button_type;
141 }
142
143 static void
144 gtk_button_class_init (GtkButtonClass *klass)
145 {
146   GObjectClass *g_object_class;
147   GtkObjectClass *object_class;
148   GtkWidgetClass *widget_class;
149   GtkContainerClass *container_class;
150
151   g_object_class = G_OBJECT_CLASS (klass);
152   object_class = (GtkObjectClass*) klass;
153   widget_class = (GtkWidgetClass*) klass;
154   container_class = (GtkContainerClass*) klass;
155   
156   parent_class = g_type_class_peek_parent (klass);
157
158   g_object_class->constructor = gtk_button_constructor;
159   g_object_class->set_property = gtk_button_set_property;
160   g_object_class->get_property = gtk_button_get_property;
161
162   object_class->destroy = gtk_button_destroy;
163
164   widget_class->realize = gtk_button_realize;
165   widget_class->unrealize = gtk_button_unrealize;
166   widget_class->map = gtk_button_map;
167   widget_class->unmap = gtk_button_unmap;
168   widget_class->size_request = gtk_button_size_request;
169   widget_class->size_allocate = gtk_button_size_allocate;
170   widget_class->expose_event = gtk_button_expose;
171   widget_class->button_press_event = gtk_button_button_press;
172   widget_class->button_release_event = gtk_button_button_release;
173   widget_class->key_release_event = gtk_button_key_release;
174   widget_class->enter_notify_event = gtk_button_enter_notify;
175   widget_class->leave_notify_event = gtk_button_leave_notify;
176
177   container_class->child_type = gtk_button_child_type;
178
179   klass->pressed = gtk_real_button_pressed;
180   klass->released = gtk_real_button_released;
181   klass->clicked = NULL;
182   klass->enter = gtk_button_update_state;
183   klass->leave = gtk_button_update_state;
184   klass->activate = gtk_real_button_activate;
185
186   g_object_class_install_property (G_OBJECT_CLASS(object_class),
187                                    PROP_LABEL,
188                                    g_param_spec_string ("label",
189                                                         _("Label"),
190                                                         _("Text of the label widget inside the button, if the button contains a label widget"),
191                                                         NULL,
192                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
193   
194   g_object_class_install_property (G_OBJECT_CLASS(object_class),
195                                    PROP_USE_UNDERLINE,
196                                    g_param_spec_boolean ("use_underline",
197                                                          _("Use underline"),
198                                                          _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
199                                                         FALSE,
200                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
201   
202   g_object_class_install_property (G_OBJECT_CLASS(object_class),
203                                    PROP_USE_STOCK,
204                                    g_param_spec_boolean ("use_stock",
205                                                          _("Use stock"),
206                                                          _("If set, the label is used to pick a stock item instead of being displayed"),
207                                                         FALSE,
208                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
209   
210   g_object_class_install_property (G_OBJECT_CLASS(object_class),
211                                    PROP_RELIEF,
212                                    g_param_spec_enum ("relief",
213                                                       _("Border relief"),
214                                                       _("The border relief style"),
215                                                       GTK_TYPE_RELIEF_STYLE,
216                                                       GTK_RELIEF_NORMAL,
217                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
218
219   button_signals[PRESSED] =
220     gtk_signal_new ("pressed",
221                     GTK_RUN_FIRST,
222                     GTK_CLASS_TYPE (object_class),
223                     GTK_SIGNAL_OFFSET (GtkButtonClass, pressed),
224                     _gtk_marshal_VOID__VOID,
225                     GTK_TYPE_NONE, 0);
226   button_signals[RELEASED] =
227     gtk_signal_new ("released",
228                     GTK_RUN_FIRST,
229                     GTK_CLASS_TYPE (object_class),
230                     GTK_SIGNAL_OFFSET (GtkButtonClass, released),
231                     _gtk_marshal_VOID__VOID,
232                     GTK_TYPE_NONE, 0);
233   button_signals[CLICKED] =
234     gtk_signal_new ("clicked",
235                     GTK_RUN_FIRST | GTK_RUN_ACTION,
236                     GTK_CLASS_TYPE (object_class),
237                     GTK_SIGNAL_OFFSET (GtkButtonClass, clicked),
238                     _gtk_marshal_VOID__VOID,
239                     GTK_TYPE_NONE, 0);
240   button_signals[ENTER] =
241     gtk_signal_new ("enter",
242                     GTK_RUN_FIRST,
243                     GTK_CLASS_TYPE (object_class),
244                     GTK_SIGNAL_OFFSET (GtkButtonClass, enter),
245                     _gtk_marshal_VOID__VOID,
246                     GTK_TYPE_NONE, 0);
247   button_signals[LEAVE] =
248     gtk_signal_new ("leave",
249                     GTK_RUN_FIRST,
250                     GTK_CLASS_TYPE (object_class),
251                     GTK_SIGNAL_OFFSET (GtkButtonClass, leave),
252                     _gtk_marshal_VOID__VOID,
253                     GTK_TYPE_NONE, 0);
254   button_signals[ACTIVATE] =
255     gtk_signal_new ("activate",
256                     GTK_RUN_FIRST | GTK_RUN_ACTION,
257                     GTK_CLASS_TYPE (object_class),
258                     GTK_SIGNAL_OFFSET (GtkButtonClass, activate),
259                     _gtk_marshal_VOID__VOID,
260                     GTK_TYPE_NONE, 0);
261   widget_class->activate_signal = button_signals[ACTIVATE];
262
263   gtk_widget_class_install_style_property (widget_class,
264                                            g_param_spec_boxed ("default_border",
265                                                                _("Default Spacing"),
266                                                                _("Extra space to add for CAN_DEFAULT buttons"),
267                                                                GTK_TYPE_BORDER,
268                                                                G_PARAM_READABLE));
269
270   gtk_widget_class_install_style_property (widget_class,
271                                            g_param_spec_boxed ("default_outside_border",
272                                                                _("Default Outside Spacing"),
273                                                                _("Extra space to add for CAN_DEFAULT buttons that is always drawn outside the border"),
274                                                                GTK_TYPE_BORDER,
275                                                                G_PARAM_READABLE));
276   gtk_widget_class_install_style_property (widget_class,
277                                            g_param_spec_int ("child_displacement_x",
278                                                              _("Child X Displacement"),
279                                                              _("How far in the x direction to move the child when the button is depressed"),
280                                                              G_MININT,
281                                                              G_MAXINT,
282                                                              0,
283                                                              G_PARAM_READABLE));
284   gtk_widget_class_install_style_property (widget_class,
285                                            g_param_spec_int ("child_displacement_y",
286                                                              _("Child Y Displacement"),
287                                                              _("How far in the y direction to move the child when the button is depressed"),
288                                                              G_MININT,
289                                                              G_MAXINT,
290                                                              0,
291                                                              G_PARAM_READABLE));
292 }
293
294 static void
295 gtk_button_init (GtkButton *button)
296 {
297   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
298   GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);
299
300   button->label_text = NULL;
301   
302   button->constructed = FALSE;
303   button->in_button = FALSE;
304   button->button_down = FALSE;
305   button->relief = GTK_RELIEF_NORMAL;
306   button->use_stock = FALSE;
307   button->use_underline = FALSE;
308   button->depressed = FALSE;
309   button->depress_on_activate = TRUE;
310 }
311
312 static void
313 gtk_button_destroy (GtkObject *object)
314 {
315   GtkButton *button = GTK_BUTTON (object);
316   
317   if (button->label_text)
318     {
319       g_free (button->label_text);
320       button->label_text = NULL;
321     }
322   
323   (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
324 }
325
326 static GObject*
327 gtk_button_constructor (GType                  type,
328                         guint                  n_construct_properties,
329                         GObjectConstructParam *construct_params)
330 {
331   GObject *object;
332   GtkButton *button;
333
334   object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
335                                                            n_construct_properties,
336                                                            construct_params);
337
338   button = GTK_BUTTON (object);
339   button->constructed = TRUE;
340
341   if (button->label_text != NULL)
342     gtk_button_construct_child (button);
343   
344   return object;
345 }
346
347
348 static GtkType
349 gtk_button_child_type  (GtkContainer     *container)
350 {
351   if (!GTK_BIN (container)->child)
352     return GTK_TYPE_WIDGET;
353   else
354     return GTK_TYPE_NONE;
355 }
356
357 static void
358 gtk_button_set_property (GObject         *object,
359                          guint            prop_id,
360                          const GValue    *value,
361                          GParamSpec      *pspec)
362 {
363   GtkButton *button;
364
365   button = GTK_BUTTON (object);
366
367   switch (prop_id)
368     {
369     case PROP_LABEL:
370       gtk_button_set_label (button, g_value_get_string (value));
371       break;
372     case PROP_RELIEF:
373       gtk_button_set_relief (button, g_value_get_enum (value));
374       break;
375     case PROP_USE_UNDERLINE:
376       gtk_button_set_use_underline (button, g_value_get_boolean (value));
377       break;
378     case PROP_USE_STOCK:
379       gtk_button_set_use_stock (button, g_value_get_boolean (value));
380       break;
381     default:
382       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
383       break;
384     }
385 }
386
387 static void
388 gtk_button_get_property (GObject         *object,
389                          guint            prop_id,
390                          GValue          *value,
391                          GParamSpec      *pspec)
392 {
393   GtkButton *button;
394
395   button = GTK_BUTTON (object);
396
397   switch (prop_id)
398     {
399     case PROP_LABEL:
400       g_value_set_string (value, button->label_text);
401       break;
402     case PROP_RELIEF:
403       g_value_set_enum (value, gtk_button_get_relief (button));
404       break;
405     case PROP_USE_UNDERLINE:
406       g_value_set_boolean (value, button->use_underline);
407       break;
408     case PROP_USE_STOCK:
409       g_value_set_boolean (value, button->use_stock);
410       break;
411     default:
412       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
413       break;
414     }
415 }
416
417 GtkWidget*
418 gtk_button_new (void)
419 {
420   return GTK_WIDGET (gtk_type_new (gtk_button_get_type ()));
421 }
422
423 static void
424 gtk_button_construct_child (GtkButton *button)
425 {
426   GtkStockItem item;
427   GtkWidget *label;
428   GtkWidget *image;
429   GtkWidget *hbox;
430   GtkWidget *align;
431
432   if (!button->constructed)
433     return;
434   
435   if (button->label_text == NULL)
436     return;
437
438   if (GTK_BIN (button)->child)
439     gtk_container_remove (GTK_CONTAINER (button),
440                           GTK_BIN (button)->child);
441
442   
443   if (button->use_stock &&
444       gtk_stock_lookup (button->label_text, &item))
445     {
446       label = gtk_label_new_with_mnemonic (item.label);
447
448       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
449       
450       image = gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON);
451       hbox = gtk_hbox_new (FALSE, 2);
452
453       align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
454       
455       gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
456       gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
457       
458       gtk_container_add (GTK_CONTAINER (button), align);
459       gtk_container_add (GTK_CONTAINER (align), hbox);
460       gtk_widget_show_all (align);
461
462       return;
463     }
464
465   if (button->use_underline)
466     {
467       label = gtk_label_new_with_mnemonic (button->label_text);
468       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
469     }
470   else
471     label = gtk_label_new (button->label_text);
472   
473   gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
474
475   gtk_widget_show (label);
476   gtk_container_add (GTK_CONTAINER (button), label);
477 }
478
479
480 GtkWidget*
481 gtk_button_new_with_label (const gchar *label)
482 {
483   return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
484 }
485
486 /**
487  * gtk_button_new_from_stock:
488  * @stock_id: the name of the stock item 
489  *
490  * Creates a new #GtkButton containing the image and text from a stock item.
491  * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
492  * #GTK_STOCK_APPLY.
493  *
494  * If @stock_id is unknown, then it will be treated as a mnemonic
495  * label (as for gtk_button_new_with_mnemonic()).
496  *
497  * Returns: a new #GtkButton
498  **/
499 GtkWidget*
500 gtk_button_new_from_stock (const gchar *stock_id)
501 {
502   return g_object_new (GTK_TYPE_BUTTON,
503                        "label", stock_id,
504                        "use_stock", TRUE,
505                        "use_underline", TRUE,
506                        NULL);
507 }
508
509 /**
510  * gtk_button_new_with_mnemonic:
511  * @label: The text of the button, with an underscore in front of the
512  *         mnemonic character
513  * @returns: a new #GtkButton
514  *
515  * Creates a new #GtkButton containing a label.
516  * If characters in @label are preceded by an underscore, they are underlined.
517  * If you need a literal underscore character in a label, use '__' (two 
518  * underscores). The first underlined character represents a keyboard 
519  * accelerator called a mnemonic.
520  * Pressing Alt and that key activates the button.
521  **/
522 GtkWidget*
523 gtk_button_new_with_mnemonic (const gchar *label)
524 {
525   return g_object_new (GTK_TYPE_BUTTON, "label", label, "use_underline", TRUE,  NULL);
526 }
527
528 void
529 gtk_button_pressed (GtkButton *button)
530 {
531   g_return_if_fail (GTK_IS_BUTTON (button));
532
533   gtk_signal_emit (GTK_OBJECT (button), button_signals[PRESSED]);
534 }
535
536 void
537 gtk_button_released (GtkButton *button)
538 {
539   g_return_if_fail (GTK_IS_BUTTON (button));
540
541   gtk_signal_emit (GTK_OBJECT (button), button_signals[RELEASED]);
542 }
543
544 void
545 gtk_button_clicked (GtkButton *button)
546 {
547   g_return_if_fail (GTK_IS_BUTTON (button));
548
549   gtk_signal_emit (GTK_OBJECT (button), button_signals[CLICKED]);
550 }
551
552 void
553 gtk_button_enter (GtkButton *button)
554 {
555   g_return_if_fail (GTK_IS_BUTTON (button));
556
557   gtk_signal_emit (GTK_OBJECT (button), button_signals[ENTER]);
558 }
559
560 void
561 gtk_button_leave (GtkButton *button)
562 {
563   g_return_if_fail (GTK_IS_BUTTON (button));
564
565   gtk_signal_emit (GTK_OBJECT (button), button_signals[LEAVE]);
566 }
567
568 void
569 gtk_button_set_relief (GtkButton *button,
570                        GtkReliefStyle newrelief)
571 {
572   g_return_if_fail (GTK_IS_BUTTON (button));
573
574   if (newrelief != button->relief) 
575     {
576        button->relief = newrelief;
577        g_object_notify (G_OBJECT (button), "relief");
578        gtk_widget_queue_draw (GTK_WIDGET (button));
579     }
580 }
581
582 GtkReliefStyle
583 gtk_button_get_relief (GtkButton *button)
584 {
585   g_return_val_if_fail (button != NULL, GTK_RELIEF_NORMAL);
586   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
587
588   return button->relief;
589 }
590
591 static void
592 gtk_button_realize (GtkWidget *widget)
593 {
594   GtkButton *button;
595   GdkWindowAttr attributes;
596   gint attributes_mask;
597   gint border_width;
598
599   button = GTK_BUTTON (widget);
600   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
601
602   border_width = GTK_CONTAINER (widget)->border_width;
603
604   attributes.window_type = GDK_WINDOW_CHILD;
605   attributes.x = widget->allocation.x + border_width;
606   attributes.y = widget->allocation.y + border_width;
607   attributes.width = widget->allocation.width - border_width * 2;
608   attributes.height = widget->allocation.height - border_width * 2;
609   attributes.wclass = GDK_INPUT_ONLY;
610   attributes.event_mask = gtk_widget_get_events (widget);
611   attributes.event_mask |= (GDK_EXPOSURE_MASK |
612                             GDK_BUTTON_PRESS_MASK |
613                             GDK_BUTTON_RELEASE_MASK |
614                             GDK_ENTER_NOTIFY_MASK |
615                             GDK_LEAVE_NOTIFY_MASK);
616
617   attributes_mask = GDK_WA_X | GDK_WA_Y;
618
619   widget->window = gtk_widget_get_parent_window (widget);
620   gdk_window_ref (widget->window);
621   
622   button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
623                                          &attributes, attributes_mask);
624   gdk_window_set_user_data (button->event_window, button);
625
626   widget->style = gtk_style_attach (widget->style, widget->window);
627 }
628
629 static void
630 gtk_button_unrealize (GtkWidget *widget)
631 {
632   GtkButton *button = GTK_BUTTON (widget);
633
634   if (button->activate_timeout)
635     gtk_button_finish_activate (button, FALSE);
636
637   if (button->event_window)
638     {
639       gdk_window_set_user_data (button->event_window, NULL);
640       gdk_window_destroy (button->event_window);
641       button->event_window = NULL;
642     }
643   
644   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
645 }
646
647 static void
648 gtk_button_map (GtkWidget *widget)
649 {
650   GtkButton *button = GTK_BUTTON (widget);
651   
652   g_return_if_fail (GTK_IS_BUTTON (widget));
653
654   GTK_WIDGET_CLASS (parent_class)->map (widget);
655
656   if (button->event_window)
657     gdk_window_show (button->event_window);
658 }
659
660 static void
661 gtk_button_unmap (GtkWidget *widget)
662 {
663   GtkButton *button = GTK_BUTTON (widget);
664     
665   g_return_if_fail (GTK_IS_BUTTON (widget));
666
667   if (button->event_window)
668     gdk_window_hide (button->event_window);
669
670   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
671 }
672
673 static void
674 gtk_button_get_props (GtkButton *button,
675                       GtkBorder *default_border,
676                       GtkBorder *default_outside_border,
677                       gboolean  *interior_focus)
678 {
679   GtkWidget *widget =  GTK_WIDGET (button);
680   GtkBorder *tmp_border;
681
682   if (default_border)
683     {
684       gtk_widget_style_get (widget, "default_border", &tmp_border, NULL);
685
686       if (tmp_border)
687         {
688           *default_border = *tmp_border;
689           g_free (tmp_border);
690         }
691       else
692         *default_border = default_default_border;
693     }
694
695   if (default_outside_border)
696     {
697       gtk_widget_style_get (widget, "default_outside_border", &tmp_border, NULL);
698
699       if (tmp_border)
700         {
701           *default_outside_border = *tmp_border;
702           g_free (tmp_border);
703         }
704       else
705         *default_outside_border = default_default_outside_border;
706     }
707
708   if (interior_focus)
709     gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
710 }
711         
712 static void
713 gtk_button_size_request (GtkWidget      *widget,
714                          GtkRequisition *requisition)
715 {
716   GtkButton *button = GTK_BUTTON (widget);
717   GtkBorder default_border;
718   gboolean interior_focus;
719   gint focus_width;
720   gint focus_pad;
721
722   gtk_button_get_props (button, &default_border, NULL, &interior_focus);
723   gtk_widget_style_get (GTK_WIDGET (widget),
724                         "focus-line-width", &focus_width,
725                         "focus-padding", &focus_pad,
726                         NULL);
727  
728   requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
729                         GTK_WIDGET (widget)->style->xthickness) * 2;
730   requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
731                          GTK_WIDGET (widget)->style->ythickness) * 2;
732
733   if (GTK_WIDGET_CAN_DEFAULT (widget))
734     {
735       requisition->width += default_border.left + default_border.right;
736       requisition->height += default_border.top + default_border.bottom;
737     }
738
739   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
740     {
741       GtkRequisition child_requisition;
742
743       gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
744
745       requisition->width += child_requisition.width;
746       requisition->height += child_requisition.height;
747     }
748   
749   requisition->width += 2 * (focus_width + focus_pad);
750   requisition->height += 2 * (focus_width + focus_pad);
751 }
752
753 static void
754 gtk_button_size_allocate (GtkWidget     *widget,
755                           GtkAllocation *allocation)
756 {
757   GtkButton *button = GTK_BUTTON (widget);
758   GtkAllocation child_allocation;
759
760   gint border_width = GTK_CONTAINER (widget)->border_width;
761   gint xthickness = GTK_WIDGET (widget)->style->xthickness;
762   gint ythickness = GTK_WIDGET (widget)->style->ythickness;
763   GtkBorder default_border;
764
765   gtk_button_get_props (button, &default_border, NULL, NULL);
766                             
767   widget->allocation = *allocation;
768
769   if (GTK_WIDGET_REALIZED (widget))
770     gdk_window_move_resize (button->event_window,
771                             widget->allocation.x + border_width,
772                             widget->allocation.y + border_width,
773                             widget->allocation.width - border_width * 2,
774                             widget->allocation.height - border_width * 2);
775
776   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
777     {
778       child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
779       child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
780       
781       child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
782                                     border_width * 2);
783       child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
784                                      border_width * 2);
785
786       if (GTK_WIDGET_CAN_DEFAULT (button))
787         {
788           child_allocation.x += default_border.left;
789           child_allocation.y += default_border.top;
790           child_allocation.width =  MAX (1, child_allocation.width - default_border.left - default_border.right);
791           child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
792         }
793
794       if (button->depressed)
795         {
796           gint child_displacement_x;
797           gint child_displacement_y;
798           
799           gtk_widget_style_get (widget,
800                                 "child_displacement_x", &child_displacement_x, 
801                                 "child_displacement_y", &child_displacement_y,
802                                 NULL);
803           child_allocation.x += child_displacement_x;
804           child_allocation.y += child_displacement_y;
805         }
806
807       gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
808     }
809 }
810
811 void
812 _gtk_button_paint (GtkButton    *button,
813                    GdkRectangle *area,
814                    GtkStateType  state_type,
815                    GtkShadowType shadow_type,
816                    const gchar  *main_detail,
817                    const gchar  *default_detail)
818 {
819   GtkWidget *widget;
820   gint width, height;
821   gint x, y;
822   gint border_width;
823   GtkBorder default_border;
824   GtkBorder default_outside_border;
825   gboolean interior_focus;
826   gint focus_width;
827   gint focus_pad;
828    
829   if (GTK_WIDGET_DRAWABLE (button))
830     {
831       widget = GTK_WIDGET (button);
832       border_width = GTK_CONTAINER (widget)->border_width;
833
834       gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
835       gtk_widget_style_get (GTK_WIDGET (widget),
836                             "focus-line-width", &focus_width,
837                             "focus-padding", &focus_pad,
838                             NULL); 
839         
840       x = widget->allocation.x + border_width;
841       y = widget->allocation.y + border_width;
842       width = widget->allocation.width - border_width * 2;
843       height = widget->allocation.height - border_width * 2;
844
845       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
846           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
847         {
848           gtk_paint_box (widget->style, widget->window,
849                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
850                          area, widget, "buttondefault",
851                          x, y, width, height);
852
853           x += default_border.left;
854           y += default_border.top;
855           width -= default_border.left + default_border.right;
856           height -= default_border.top + default_border.bottom;
857         }
858       else if (GTK_WIDGET_CAN_DEFAULT (widget))
859         {
860           x += default_outside_border.left;
861           y += default_outside_border.top;
862           width -= default_outside_border.left + default_outside_border.right;
863           height -= default_outside_border.top + default_outside_border.bottom;
864         }
865        
866       if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
867         {
868           x += focus_width + focus_pad;
869           y += focus_width + focus_pad;
870           width -= 2 * (focus_width + focus_pad);
871           height -= 2 * (focus_width + focus_pad);
872         }
873
874       if ((button->relief != GTK_RELIEF_NONE) ||
875           ((GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL) &&
876            (GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)))
877         gtk_paint_box (widget->style, widget->window,
878                        state_type,
879                        shadow_type, area, widget, "button",
880                        x, y, width, height);
881        
882       if (GTK_WIDGET_HAS_FOCUS (widget))
883         {
884           if (interior_focus)
885             {
886               x += widget->style->xthickness + focus_pad;
887               y += widget->style->ythickness + focus_pad;
888               width -= 2 * (widget->style->xthickness + focus_pad);
889               height -=  2 * (widget->style->xthickness + focus_pad);
890             }
891           else
892             {
893               x -= focus_width + focus_pad;
894               y -= focus_width + focus_pad;
895               width += 2 * (focus_width + focus_pad);
896               height += 2 * (focus_width + focus_pad);
897             }
898
899           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
900                            area, widget, "button",
901                            x, y, width, height);
902         }
903     }
904 }
905
906 static gboolean
907 gtk_button_expose (GtkWidget      *widget,
908                    GdkEventExpose *event)
909 {
910   if (GTK_WIDGET_DRAWABLE (widget))
911     {
912       GtkButton *button = GTK_BUTTON (widget);
913       
914       _gtk_button_paint (button, &event->area,
915                          GTK_WIDGET_STATE (widget),
916                          button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
917                          "button", "buttondefault");
918       
919       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
920     }
921   
922   return FALSE;
923 }
924
925 static gboolean
926 gtk_button_button_press (GtkWidget      *widget,
927                          GdkEventButton *event)
928 {
929   GtkButton *button;
930
931   if (event->type == GDK_BUTTON_PRESS)
932     {
933       button = GTK_BUTTON (widget);
934
935       if (!GTK_WIDGET_HAS_FOCUS (widget))
936         gtk_widget_grab_focus (widget);
937
938       if (event->button == 1)
939         gtk_button_pressed (button);
940     }
941
942   return TRUE;
943 }
944
945 static gboolean
946 gtk_button_button_release (GtkWidget      *widget,
947                            GdkEventButton *event)
948 {
949   GtkButton *button;
950
951   if (event->button == 1)
952     {
953       button = GTK_BUTTON (widget);
954       gtk_button_released (button);
955     }
956
957   return TRUE;
958 }
959
960 static gboolean
961 gtk_button_key_release (GtkWidget   *widget,
962                         GdkEventKey *event)
963 {
964   GtkButton *button = GTK_BUTTON (widget);
965
966   if (button->activate_timeout)
967     {
968       gtk_button_finish_activate (button, TRUE);
969       return TRUE;
970     }
971   else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
972     return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
973   else
974     return FALSE;
975 }
976
977 static gboolean
978 gtk_button_enter_notify (GtkWidget        *widget,
979                          GdkEventCrossing *event)
980 {
981   GtkButton *button;
982   GtkWidget *event_widget;
983
984   button = GTK_BUTTON (widget);
985   event_widget = gtk_get_event_widget ((GdkEvent*) event);
986
987   if ((event_widget == widget) &&
988       (event->detail != GDK_NOTIFY_INFERIOR))
989     {
990       button->in_button = TRUE;
991       gtk_button_enter (button);
992     }
993
994   return FALSE;
995 }
996
997 static gboolean
998 gtk_button_leave_notify (GtkWidget        *widget,
999                          GdkEventCrossing *event)
1000 {
1001   GtkButton *button;
1002   GtkWidget *event_widget;
1003
1004   button = GTK_BUTTON (widget);
1005   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1006
1007   if ((event_widget == widget) &&
1008       (event->detail != GDK_NOTIFY_INFERIOR))
1009     {
1010       button->in_button = FALSE;
1011       gtk_button_leave (button);
1012     }
1013
1014   return FALSE;
1015 }
1016
1017 static void
1018 gtk_real_button_pressed (GtkButton *button)
1019 {
1020   if (button->activate_timeout)
1021     return;
1022   
1023   button->button_down = TRUE;
1024   gtk_button_update_state (button);
1025 }
1026
1027 static void
1028 gtk_real_button_released (GtkButton *button)
1029 {
1030   if (button->button_down)
1031     {
1032       button->button_down = FALSE;
1033
1034       if (button->activate_timeout)
1035         return;
1036       
1037       if (button->in_button)
1038         gtk_button_clicked (button);
1039
1040       gtk_button_update_state (button);
1041     }
1042 }
1043
1044 static gboolean
1045 button_activate_timeout (gpointer data)
1046 {
1047   GDK_THREADS_ENTER ();
1048   
1049   gtk_button_finish_activate (data, TRUE);
1050
1051   GDK_THREADS_LEAVE ();
1052
1053   return FALSE;
1054 }
1055
1056 static void
1057 gtk_real_button_activate (GtkButton *button)
1058 {
1059   GtkWidget *widget = GTK_WIDGET (button);
1060   
1061   if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1062     {
1063       if (gdk_keyboard_grab (button->event_window, TRUE,
1064                              gtk_get_current_event_time ()) == 0)
1065         {
1066           gtk_grab_add (widget);
1067           
1068           button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1069                                                     button_activate_timeout,
1070                                                     button);
1071           button->button_down = TRUE;
1072           gtk_button_update_state (button);
1073           gtk_widget_queue_draw (GTK_WIDGET (button));
1074         }
1075     }
1076 }
1077
1078 static void
1079 gtk_button_finish_activate (GtkButton *button,
1080                             gboolean   do_it)
1081 {
1082   GtkWidget *widget = GTK_WIDGET (button);
1083   
1084   g_source_remove (button->activate_timeout);
1085   button->activate_timeout = 0;
1086
1087   gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1088                                gtk_get_current_event_time ());
1089   gtk_grab_remove (widget);
1090
1091   button->button_down = FALSE;
1092
1093   gtk_button_update_state (button);
1094   gtk_widget_queue_draw (GTK_WIDGET (button));
1095
1096   if (do_it)
1097     gtk_button_clicked (button);
1098 }
1099
1100 /**
1101  * gtk_button_set_label:
1102  * @button: a #GtkButton
1103  * @label: a string
1104  *
1105  * Sets the text of the label of the button to @str. This text is
1106  * also used to select the stock item if gtk_button_set_use_stock()
1107  * is used.
1108  *
1109  * This will also clear any previously set labels.
1110  **/
1111 void
1112 gtk_button_set_label (GtkButton   *button,
1113                       const gchar *label)
1114 {
1115   g_return_if_fail (GTK_IS_BUTTON (button));
1116   
1117   g_free (button->label_text);
1118   button->label_text = g_strdup (label);
1119   
1120   gtk_button_construct_child (button);
1121   
1122   g_object_notify (G_OBJECT (button), "label");
1123 }
1124
1125 /**
1126  * gtk_button_get_label:
1127  * @button: a #GtkButton
1128  *
1129  * Fetches the text from the label of the button, as set by
1130  * gtk_button_set_label(). If the label text has not 
1131  * been set the return value will be %NULL. This will be the 
1132  * case if you create an empty button with gtk_button_new() to 
1133  * use as a container.
1134  *
1135  * Return value: The text of the label widget. This string is owned
1136  * by the widget and must not be modified or freed.
1137  **/
1138 G_CONST_RETURN gchar *
1139 gtk_button_get_label (GtkButton *button)
1140 {
1141   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1142   
1143   return button->label_text;
1144 }
1145
1146 /**
1147  * gtk_button_set_use_underline:
1148  * @button: a #GtkButton
1149  * @use_underline: %TRUE if underlines in the text indicate mnemonics
1150  *
1151  * If true, an underline in the text of the button label indicates
1152  * the next character should be used for the mnemonic accelerator key.
1153  */
1154 void
1155 gtk_button_set_use_underline (GtkButton *button,
1156                               gboolean   use_underline)
1157 {
1158   g_return_if_fail (GTK_IS_BUTTON (button));
1159
1160   use_underline = use_underline != FALSE;
1161
1162   if (use_underline != button->use_underline)
1163     {
1164       button->use_underline = use_underline;
1165   
1166       gtk_button_construct_child (button);
1167       
1168       g_object_notify (G_OBJECT (button), "use_underline");
1169     }
1170 }
1171
1172 /**
1173  * gtk_button_get_use_underline:
1174  * @button: a #GtkButton
1175  *
1176  * Returns whether an embedded underline in the button label indicates a
1177  * mnemonic. See gtk_button_set_use_underline ().
1178  *
1179  * Return value: %TRUE if an embedded underline in the button label
1180  *               indicates the mnemonic accelerator keys.
1181  **/
1182 gboolean
1183 gtk_button_get_use_underline (GtkButton *button)
1184 {
1185   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1186   
1187   return button->use_underline;
1188 }
1189
1190 /**
1191  * gtk_button_set_use_stock:
1192  * @button: a #GtkButton
1193  * @use_stock: %TRUE if the button should use a stock item
1194  *
1195  * If true, the label set on the button is used as a
1196  * stock id to select the stock item for the button.
1197  */
1198 void
1199 gtk_button_set_use_stock (GtkButton *button,
1200                           gboolean   use_stock)
1201 {
1202   g_return_if_fail (GTK_IS_BUTTON (button));
1203
1204   use_stock = use_stock != FALSE;
1205
1206   if (use_stock != button->use_stock)
1207     {
1208       button->use_stock = use_stock;
1209   
1210       gtk_button_construct_child (button);
1211       
1212       g_object_notify (G_OBJECT (button), "use_stock");
1213     }
1214 }
1215
1216 /**
1217  * gtk_button_get_use_stock:
1218  * @button: a #GtkButton
1219  *
1220  * Returns whether the button label is a stock item.
1221  *
1222  * Return value: %TRUE if the button label is used to
1223  *               select a stock item instead of being
1224  *               used directly as the label text.
1225  */
1226 gboolean
1227 gtk_button_get_use_stock (GtkButton *button)
1228 {
1229   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1230   
1231   return button->use_stock;
1232 }
1233
1234 /**
1235  * _gtk_button_set_depressed:
1236  * @button: a #GtkButton
1237  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1238  * 
1239  * Sets whether the button is currently drawn as down or not. This is 
1240  * purely a visual setting, and is meant only for use by derived widgets
1241  * such as #GtkToggleButton.
1242  **/
1243 void
1244 _gtk_button_set_depressed (GtkButton *button,
1245                            gboolean   depressed)
1246 {
1247   GtkWidget *widget = GTK_WIDGET (button);
1248
1249   depressed = depressed != FALSE;
1250
1251   if (depressed != button->depressed)
1252     {
1253       button->depressed = depressed;
1254       gtk_widget_queue_resize (widget);
1255     }
1256 }
1257
1258 static void
1259 gtk_button_update_state (GtkButton *button)
1260 {
1261   gboolean depressed;
1262   GtkStateType new_state;
1263
1264   if (button->activate_timeout)
1265     depressed = button->depress_on_activate;
1266   else
1267     depressed = button->in_button && button->button_down;
1268
1269   if (button->in_button && (!button->button_down || !depressed))
1270     new_state = GTK_STATE_PRELIGHT;
1271   else
1272     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1273
1274   _gtk_button_set_depressed (button, depressed); 
1275   gtk_widget_set_state (GTK_WIDGET (button), new_state);
1276 }