]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
Make string setters safe for self-assignment. (#122745, Soeren Sandmann)
[~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_BUTTON_PRESS_MASK |
634                             GDK_BUTTON_RELEASE_MASK |
635                             GDK_ENTER_NOTIFY_MASK |
636                             GDK_LEAVE_NOTIFY_MASK);
637
638   attributes_mask = GDK_WA_X | GDK_WA_Y;
639
640   widget->window = gtk_widget_get_parent_window (widget);
641   g_object_ref (widget->window);
642   
643   button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
644                                          &attributes, attributes_mask);
645   gdk_window_set_user_data (button->event_window, button);
646
647   widget->style = gtk_style_attach (widget->style, widget->window);
648 }
649
650 static void
651 gtk_button_unrealize (GtkWidget *widget)
652 {
653   GtkButton *button = GTK_BUTTON (widget);
654
655   if (button->activate_timeout)
656     gtk_button_finish_activate (button, FALSE);
657
658   if (button->event_window)
659     {
660       gdk_window_set_user_data (button->event_window, NULL);
661       gdk_window_destroy (button->event_window);
662       button->event_window = NULL;
663     }
664   
665   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
666 }
667
668 static void
669 gtk_button_map (GtkWidget *widget)
670 {
671   GtkButton *button = GTK_BUTTON (widget);
672   
673   g_return_if_fail (GTK_IS_BUTTON (widget));
674
675   GTK_WIDGET_CLASS (parent_class)->map (widget);
676
677   if (button->event_window)
678     gdk_window_show (button->event_window);
679 }
680
681 static void
682 gtk_button_unmap (GtkWidget *widget)
683 {
684   GtkButton *button = GTK_BUTTON (widget);
685     
686   g_return_if_fail (GTK_IS_BUTTON (widget));
687
688   if (button->event_window)
689     gdk_window_hide (button->event_window);
690
691   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
692 }
693
694 static void
695 gtk_button_get_props (GtkButton *button,
696                       GtkBorder *default_border,
697                       GtkBorder *default_outside_border,
698                       gboolean  *interior_focus)
699 {
700   GtkWidget *widget =  GTK_WIDGET (button);
701   GtkBorder *tmp_border;
702
703   if (default_border)
704     {
705       gtk_widget_style_get (widget, "default_border", &tmp_border, NULL);
706
707       if (tmp_border)
708         {
709           *default_border = *tmp_border;
710           g_free (tmp_border);
711         }
712       else
713         *default_border = default_default_border;
714     }
715
716   if (default_outside_border)
717     {
718       gtk_widget_style_get (widget, "default_outside_border", &tmp_border, NULL);
719
720       if (tmp_border)
721         {
722           *default_outside_border = *tmp_border;
723           g_free (tmp_border);
724         }
725       else
726         *default_outside_border = default_default_outside_border;
727     }
728
729   if (interior_focus)
730     gtk_widget_style_get (widget, "interior_focus", interior_focus, NULL);
731 }
732         
733 static void
734 gtk_button_size_request (GtkWidget      *widget,
735                          GtkRequisition *requisition)
736 {
737   GtkButton *button = GTK_BUTTON (widget);
738   GtkBorder default_border;
739   gboolean interior_focus;
740   gint focus_width;
741   gint focus_pad;
742
743   gtk_button_get_props (button, &default_border, NULL, &interior_focus);
744   gtk_widget_style_get (GTK_WIDGET (widget),
745                         "focus-line-width", &focus_width,
746                         "focus-padding", &focus_pad,
747                         NULL);
748  
749   requisition->width = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
750                         GTK_WIDGET (widget)->style->xthickness) * 2;
751   requisition->height = (GTK_CONTAINER (widget)->border_width + CHILD_SPACING +
752                          GTK_WIDGET (widget)->style->ythickness) * 2;
753
754   if (GTK_WIDGET_CAN_DEFAULT (widget))
755     {
756       requisition->width += default_border.left + default_border.right;
757       requisition->height += default_border.top + default_border.bottom;
758     }
759
760   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
761     {
762       GtkRequisition child_requisition;
763
764       gtk_widget_size_request (GTK_BIN (button)->child, &child_requisition);
765
766       requisition->width += child_requisition.width;
767       requisition->height += child_requisition.height;
768     }
769   
770   requisition->width += 2 * (focus_width + focus_pad);
771   requisition->height += 2 * (focus_width + focus_pad);
772 }
773
774 static void
775 gtk_button_size_allocate (GtkWidget     *widget,
776                           GtkAllocation *allocation)
777 {
778   GtkButton *button = GTK_BUTTON (widget);
779   GtkAllocation child_allocation;
780
781   gint border_width = GTK_CONTAINER (widget)->border_width;
782   gint xthickness = GTK_WIDGET (widget)->style->xthickness;
783   gint ythickness = GTK_WIDGET (widget)->style->ythickness;
784   GtkBorder default_border;
785
786   gtk_button_get_props (button, &default_border, NULL, NULL);
787                             
788   widget->allocation = *allocation;
789
790   if (GTK_WIDGET_REALIZED (widget))
791     gdk_window_move_resize (button->event_window,
792                             widget->allocation.x + border_width,
793                             widget->allocation.y + border_width,
794                             widget->allocation.width - border_width * 2,
795                             widget->allocation.height - border_width * 2);
796
797   if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
798     {
799       child_allocation.x = widget->allocation.x + border_width + CHILD_SPACING + xthickness;
800       child_allocation.y = widget->allocation.y + border_width + CHILD_SPACING + ythickness;
801       
802       child_allocation.width = MAX (1, widget->allocation.width - (CHILD_SPACING + xthickness) * 2 -
803                                     border_width * 2);
804       child_allocation.height = MAX (1, widget->allocation.height - (CHILD_SPACING + ythickness) * 2 -
805                                      border_width * 2);
806
807       if (GTK_WIDGET_CAN_DEFAULT (button))
808         {
809           child_allocation.x += default_border.left;
810           child_allocation.y += default_border.top;
811           child_allocation.width =  MAX (1, child_allocation.width - default_border.left - default_border.right);
812           child_allocation.height = MAX (1, child_allocation.height - default_border.top - default_border.bottom);
813         }
814
815       if (button->depressed)
816         {
817           gint child_displacement_x;
818           gint child_displacement_y;
819           
820           gtk_widget_style_get (widget,
821                                 "child_displacement_x", &child_displacement_x, 
822                                 "child_displacement_y", &child_displacement_y,
823                                 NULL);
824           child_allocation.x += child_displacement_x;
825           child_allocation.y += child_displacement_y;
826         }
827
828       gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
829     }
830 }
831
832 void
833 _gtk_button_paint (GtkButton    *button,
834                    GdkRectangle *area,
835                    GtkStateType  state_type,
836                    GtkShadowType shadow_type,
837                    const gchar  *main_detail,
838                    const gchar  *default_detail)
839 {
840   GtkWidget *widget;
841   gint width, height;
842   gint x, y;
843   gint border_width;
844   GtkBorder default_border;
845   GtkBorder default_outside_border;
846   gboolean interior_focus;
847   gint focus_width;
848   gint focus_pad;
849    
850   if (GTK_WIDGET_DRAWABLE (button))
851     {
852       widget = GTK_WIDGET (button);
853       border_width = GTK_CONTAINER (widget)->border_width;
854
855       gtk_button_get_props (button, &default_border, &default_outside_border, &interior_focus);
856       gtk_widget_style_get (GTK_WIDGET (widget),
857                             "focus-line-width", &focus_width,
858                             "focus-padding", &focus_pad,
859                             NULL); 
860         
861       x = widget->allocation.x + border_width;
862       y = widget->allocation.y + border_width;
863       width = widget->allocation.width - border_width * 2;
864       height = widget->allocation.height - border_width * 2;
865
866       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
867           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
868         {
869           gtk_paint_box (widget->style, widget->window,
870                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
871                          area, widget, "buttondefault",
872                          x, y, width, height);
873
874           x += default_border.left;
875           y += default_border.top;
876           width -= default_border.left + default_border.right;
877           height -= default_border.top + default_border.bottom;
878         }
879       else if (GTK_WIDGET_CAN_DEFAULT (widget))
880         {
881           x += default_outside_border.left;
882           y += default_outside_border.top;
883           width -= default_outside_border.left + default_outside_border.right;
884           height -= default_outside_border.top + default_outside_border.bottom;
885         }
886        
887       if (!interior_focus && GTK_WIDGET_HAS_FOCUS (widget))
888         {
889           x += focus_width + focus_pad;
890           y += focus_width + focus_pad;
891           width -= 2 * (focus_width + focus_pad);
892           height -= 2 * (focus_width + focus_pad);
893         }
894
895       if ((button->relief != GTK_RELIEF_NONE) ||
896           ((GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL) &&
897            (GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)))
898         gtk_paint_box (widget->style, widget->window,
899                        state_type,
900                        shadow_type, area, widget, "button",
901                        x, y, width, height);
902        
903       if (GTK_WIDGET_HAS_FOCUS (widget))
904         {
905           if (interior_focus)
906             {
907               x += widget->style->xthickness + focus_pad;
908               y += widget->style->ythickness + focus_pad;
909               width -= 2 * (widget->style->xthickness + focus_pad);
910               height -=  2 * (widget->style->xthickness + focus_pad);
911             }
912           else
913             {
914               x -= focus_width + focus_pad;
915               y -= focus_width + focus_pad;
916               width += 2 * (focus_width + focus_pad);
917               height += 2 * (focus_width + focus_pad);
918             }
919
920           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
921                            area, widget, "button",
922                            x, y, width, height);
923         }
924     }
925 }
926
927 static gboolean
928 gtk_button_expose (GtkWidget      *widget,
929                    GdkEventExpose *event)
930 {
931   if (GTK_WIDGET_DRAWABLE (widget))
932     {
933       GtkButton *button = GTK_BUTTON (widget);
934       
935       _gtk_button_paint (button, &event->area,
936                          GTK_WIDGET_STATE (widget),
937                          button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
938                          "button", "buttondefault");
939       
940       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
941     }
942   
943   return FALSE;
944 }
945
946 static gboolean
947 gtk_button_button_press (GtkWidget      *widget,
948                          GdkEventButton *event)
949 {
950   GtkButton *button;
951
952   if (event->type == GDK_BUTTON_PRESS)
953     {
954       button = GTK_BUTTON (widget);
955
956       if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget))
957         gtk_widget_grab_focus (widget);
958
959       if (event->button == 1)
960         gtk_button_pressed (button);
961     }
962
963   return TRUE;
964 }
965
966 static gboolean
967 gtk_button_button_release (GtkWidget      *widget,
968                            GdkEventButton *event)
969 {
970   GtkButton *button;
971
972   if (event->button == 1)
973     {
974       button = GTK_BUTTON (widget);
975       gtk_button_released (button);
976     }
977
978   return TRUE;
979 }
980
981 static gboolean
982 gtk_button_key_release (GtkWidget   *widget,
983                         GdkEventKey *event)
984 {
985   GtkButton *button = GTK_BUTTON (widget);
986
987   if (button->activate_timeout)
988     {
989       gtk_button_finish_activate (button, TRUE);
990       return TRUE;
991     }
992   else if (GTK_WIDGET_CLASS (parent_class)->key_release_event)
993     return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
994   else
995     return FALSE;
996 }
997
998 static gboolean
999 gtk_button_enter_notify (GtkWidget        *widget,
1000                          GdkEventCrossing *event)
1001 {
1002   GtkButton *button;
1003   GtkWidget *event_widget;
1004
1005   button = GTK_BUTTON (widget);
1006   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1007
1008   if ((event_widget == widget) &&
1009       (event->detail != GDK_NOTIFY_INFERIOR))
1010     {
1011       button->in_button = TRUE;
1012       gtk_button_enter (button);
1013     }
1014
1015   return FALSE;
1016 }
1017
1018 static gboolean
1019 gtk_button_leave_notify (GtkWidget        *widget,
1020                          GdkEventCrossing *event)
1021 {
1022   GtkButton *button;
1023   GtkWidget *event_widget;
1024
1025   button = GTK_BUTTON (widget);
1026   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1027
1028   if ((event_widget == widget) &&
1029       (event->detail != GDK_NOTIFY_INFERIOR))
1030     {
1031       button->in_button = FALSE;
1032       gtk_button_leave (button);
1033     }
1034
1035   return FALSE;
1036 }
1037
1038 static void
1039 gtk_real_button_pressed (GtkButton *button)
1040 {
1041   if (button->activate_timeout)
1042     return;
1043   
1044   button->button_down = TRUE;
1045   gtk_button_update_state (button);
1046 }
1047
1048 static void
1049 gtk_real_button_released (GtkButton *button)
1050 {
1051   if (button->button_down)
1052     {
1053       button->button_down = FALSE;
1054
1055       if (button->activate_timeout)
1056         return;
1057       
1058       if (button->in_button)
1059         gtk_button_clicked (button);
1060
1061       gtk_button_update_state (button);
1062     }
1063 }
1064
1065 static gboolean
1066 button_activate_timeout (gpointer data)
1067 {
1068   GDK_THREADS_ENTER ();
1069   
1070   gtk_button_finish_activate (data, TRUE);
1071
1072   GDK_THREADS_LEAVE ();
1073
1074   return FALSE;
1075 }
1076
1077 static void
1078 gtk_real_button_activate (GtkButton *button)
1079 {
1080   GtkWidget *widget = GTK_WIDGET (button);
1081   
1082   if (GTK_WIDGET_REALIZED (button) && !button->activate_timeout)
1083     {
1084       if (gdk_keyboard_grab (button->event_window, TRUE,
1085                              gtk_get_current_event_time ()) == 0)
1086         {
1087           gtk_grab_add (widget);
1088           
1089           button->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT,
1090                                                     button_activate_timeout,
1091                                                     button);
1092           button->button_down = TRUE;
1093           gtk_button_update_state (button);
1094           gtk_widget_queue_draw (GTK_WIDGET (button));
1095         }
1096     }
1097 }
1098
1099 static void
1100 gtk_button_finish_activate (GtkButton *button,
1101                             gboolean   do_it)
1102 {
1103   GtkWidget *widget = GTK_WIDGET (button);
1104   
1105   g_source_remove (button->activate_timeout);
1106   button->activate_timeout = 0;
1107
1108   gdk_display_keyboard_ungrab (gtk_widget_get_display (widget),
1109                                gtk_get_current_event_time ());
1110   gtk_grab_remove (widget);
1111
1112   button->button_down = FALSE;
1113
1114   gtk_button_update_state (button);
1115   gtk_widget_queue_draw (GTK_WIDGET (button));
1116
1117   if (do_it)
1118     gtk_button_clicked (button);
1119 }
1120
1121 /**
1122  * gtk_button_set_label:
1123  * @button: a #GtkButton
1124  * @label: a string
1125  *
1126  * Sets the text of the label of the button to @str. This text is
1127  * also used to select the stock item if gtk_button_set_use_stock()
1128  * is used.
1129  *
1130  * This will also clear any previously set labels.
1131  **/
1132 void
1133 gtk_button_set_label (GtkButton   *button,
1134                       const gchar *label)
1135 {
1136   gchar *new_label;
1137   
1138   g_return_if_fail (GTK_IS_BUTTON (button));
1139
1140   new_label = g_strdup (label);
1141   g_free (button->label_text);
1142   button->label_text = new_label;
1143   
1144   gtk_button_construct_child (button);
1145   
1146   g_object_notify (G_OBJECT (button), "label");
1147 }
1148
1149 /**
1150  * gtk_button_get_label:
1151  * @button: a #GtkButton
1152  *
1153  * Fetches the text from the label of the button, as set by
1154  * gtk_button_set_label(). If the label text has not 
1155  * been set the return value will be %NULL. This will be the 
1156  * case if you create an empty button with gtk_button_new() to 
1157  * use as a container.
1158  *
1159  * Return value: The text of the label widget. This string is owned
1160  * by the widget and must not be modified or freed.
1161  **/
1162 G_CONST_RETURN gchar *
1163 gtk_button_get_label (GtkButton *button)
1164 {
1165   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
1166   
1167   return button->label_text;
1168 }
1169
1170 /**
1171  * gtk_button_set_use_underline:
1172  * @button: a #GtkButton
1173  * @use_underline: %TRUE if underlines in the text indicate mnemonics
1174  *
1175  * If true, an underline in the text of the button label indicates
1176  * the next character should be used for the mnemonic accelerator key.
1177  */
1178 void
1179 gtk_button_set_use_underline (GtkButton *button,
1180                               gboolean   use_underline)
1181 {
1182   g_return_if_fail (GTK_IS_BUTTON (button));
1183
1184   use_underline = use_underline != FALSE;
1185
1186   if (use_underline != button->use_underline)
1187     {
1188       button->use_underline = use_underline;
1189   
1190       gtk_button_construct_child (button);
1191       
1192       g_object_notify (G_OBJECT (button), "use_underline");
1193     }
1194 }
1195
1196 /**
1197  * gtk_button_get_use_underline:
1198  * @button: a #GtkButton
1199  *
1200  * Returns whether an embedded underline in the button label indicates a
1201  * mnemonic. See gtk_button_set_use_underline ().
1202  *
1203  * Return value: %TRUE if an embedded underline in the button label
1204  *               indicates the mnemonic accelerator keys.
1205  **/
1206 gboolean
1207 gtk_button_get_use_underline (GtkButton *button)
1208 {
1209   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1210   
1211   return button->use_underline;
1212 }
1213
1214 /**
1215  * gtk_button_set_use_stock:
1216  * @button: a #GtkButton
1217  * @use_stock: %TRUE if the button should use a stock item
1218  *
1219  * If true, the label set on the button is used as a
1220  * stock id to select the stock item for the button.
1221  */
1222 void
1223 gtk_button_set_use_stock (GtkButton *button,
1224                           gboolean   use_stock)
1225 {
1226   g_return_if_fail (GTK_IS_BUTTON (button));
1227
1228   use_stock = use_stock != FALSE;
1229
1230   if (use_stock != button->use_stock)
1231     {
1232       button->use_stock = use_stock;
1233   
1234       gtk_button_construct_child (button);
1235       
1236       g_object_notify (G_OBJECT (button), "use_stock");
1237     }
1238 }
1239
1240 /**
1241  * gtk_button_get_use_stock:
1242  * @button: a #GtkButton
1243  *
1244  * Returns whether the button label is a stock item.
1245  *
1246  * Return value: %TRUE if the button label is used to
1247  *               select a stock item instead of being
1248  *               used directly as the label text.
1249  */
1250 gboolean
1251 gtk_button_get_use_stock (GtkButton *button)
1252 {
1253   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1254   
1255   return button->use_stock;
1256 }
1257
1258 /**
1259  * gtk_button_set_focus_on_click:
1260  * @button: a #GtkButton
1261  * @focus_on_click: whether the mouse grabs focus when clicked with the mouse
1262  * 
1263  * Sets whether the button will grab focus when it is clicked with the mouse.
1264  * Making mouse clicks not grab focus is useful in places like toolbars where
1265  * you don't want the keyboard focus removed from the main area of the
1266  * application.
1267  *
1268  * Since: 2.4
1269  **/
1270 void
1271 gtk_button_set_focus_on_click (GtkButton *button,
1272                                gboolean   focus_on_click)
1273 {
1274   g_return_if_fail (GTK_IS_BUTTON (button));
1275   
1276   focus_on_click = focus_on_click != FALSE;
1277
1278   if (button->focus_on_click != focus_on_click)
1279     {
1280       button->focus_on_click = focus_on_click;
1281       
1282       g_object_notify (G_OBJECT (button), "focus_on_click");
1283     }
1284 }
1285
1286 /**
1287  * gtk_button_get_focus_on_click:
1288  * @button: a #GtkButton
1289  * 
1290  * Returns whether the button grabs focus when it is clicked with the mouse.
1291  * See gtk_button_set_focus_on_click().
1292  *
1293  * Return value: %TRUE if the button grabs focus when it is clicked with
1294  *               the mouse.
1295  *
1296  * Since: 2.4
1297  **/
1298 gboolean
1299 gtk_button_get_focus_on_click (GtkButton *button)
1300 {
1301   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
1302   
1303   return button->focus_on_click;
1304 }
1305
1306 /**
1307  * _gtk_button_set_depressed:
1308  * @button: a #GtkButton
1309  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
1310  * 
1311  * Sets whether the button is currently drawn as down or not. This is 
1312  * purely a visual setting, and is meant only for use by derived widgets
1313  * such as #GtkToggleButton.
1314  **/
1315 void
1316 _gtk_button_set_depressed (GtkButton *button,
1317                            gboolean   depressed)
1318 {
1319   GtkWidget *widget = GTK_WIDGET (button);
1320
1321   depressed = depressed != FALSE;
1322
1323   if (depressed != button->depressed)
1324     {
1325       button->depressed = depressed;
1326       gtk_widget_queue_resize (widget);
1327     }
1328 }
1329
1330 static void
1331 gtk_button_update_state (GtkButton *button)
1332 {
1333   gboolean depressed;
1334   GtkStateType new_state;
1335
1336   if (button->activate_timeout)
1337     depressed = button->depress_on_activate;
1338   else
1339     depressed = button->in_button && button->button_down;
1340
1341   if (button->in_button && (!button->button_down || !depressed))
1342     new_state = GTK_STATE_PRELIGHT;
1343   else
1344     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
1345
1346   _gtk_button_set_depressed (button, depressed); 
1347   gtk_widget_set_state (GTK_WIDGET (button), new_state);
1348 }