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