]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
Patch from Matthias Clasen to remove remove all instances of
[~andy/gtk] / gtk / gtktogglebutton.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-2000.  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 "gtklabel.h"
28 #include "gtkmain.h"
29 #include "gtksignal.h"
30 #include "gtktogglebutton.h"
31 #include "gtkintl.h"
32
33 #define DEFAULT_LEFT_POS  4
34 #define DEFAULT_TOP_POS   4
35 #define DEFAULT_SPACING   7
36
37 enum {
38   TOGGLED,
39   LAST_SIGNAL
40 };
41
42 enum {
43   PROP_0,
44   PROP_ACTIVE,
45   PROP_INCONSISTENT,
46   PROP_DRAW_INDICATOR
47 };
48
49
50 static void gtk_toggle_button_class_init    (GtkToggleButtonClass *klass);
51 static void gtk_toggle_button_init          (GtkToggleButton      *toggle_button);
52 static void gtk_toggle_button_paint         (GtkWidget            *widget,
53                                              GdkRectangle         *area);
54 static void gtk_toggle_button_size_allocate (GtkWidget            *widget,
55                                              GtkAllocation        *allocation);
56 static gint gtk_toggle_button_expose        (GtkWidget            *widget,
57                                              GdkEventExpose       *event);
58 static void gtk_toggle_button_pressed       (GtkButton            *button);
59 static void gtk_toggle_button_released      (GtkButton            *button);
60 static void gtk_toggle_button_clicked       (GtkButton            *button);
61 static void gtk_toggle_button_enter         (GtkButton            *button);
62 static void gtk_toggle_button_leave         (GtkButton            *button);
63 static void gtk_toggle_button_set_property  (GObject              *object,
64                                              guint                 prop_id,
65                                              const GValue         *value,
66                                              GParamSpec           *pspec);
67 static void gtk_toggle_button_get_property  (GObject              *object,
68                                              guint                 prop_id,
69                                              GValue               *value,
70                                              GParamSpec           *pspec);
71 static void gtk_toggle_button_leave         (GtkButton            *button);
72 static void gtk_toggle_button_realize       (GtkWidget            *widget);
73 static void gtk_toggle_button_unrealize     (GtkWidget            *widget);
74 static void gtk_toggle_button_map           (GtkWidget            *widget);
75 static void gtk_toggle_button_unmap         (GtkWidget            *widget);
76
77 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
78 static GtkContainerClass *parent_class = NULL;
79
80 GtkType
81 gtk_toggle_button_get_type (void)
82 {
83   static GtkType toggle_button_type = 0;
84
85   if (!toggle_button_type)
86     {
87       static const GtkTypeInfo toggle_button_info =
88       {
89         "GtkToggleButton",
90         sizeof (GtkToggleButton),
91         sizeof (GtkToggleButtonClass),
92         (GtkClassInitFunc) gtk_toggle_button_class_init,
93         (GtkObjectInitFunc) gtk_toggle_button_init,
94         /* reserved_1 */ NULL,
95         /* reserved_2 */ NULL,
96         (GtkClassInitFunc) NULL,
97       };
98
99       toggle_button_type = gtk_type_unique (GTK_TYPE_BUTTON, &toggle_button_info);
100     }
101
102   return toggle_button_type;
103 }
104
105 static void
106 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
107 {
108   GtkObjectClass *object_class;
109   GObjectClass   *gobject_class;
110   GtkWidgetClass *widget_class;
111   GtkContainerClass *container_class;
112   GtkButtonClass *button_class;
113
114   object_class = (GtkObjectClass*) class;
115   gobject_class = G_OBJECT_CLASS (class);
116   widget_class = (GtkWidgetClass*) class;
117   container_class = (GtkContainerClass*) class;
118   button_class = (GtkButtonClass*) class;
119
120   parent_class = gtk_type_class (GTK_TYPE_BUTTON);
121
122
123   gobject_class->set_property = gtk_toggle_button_set_property;
124   gobject_class->get_property = gtk_toggle_button_get_property;
125
126   widget_class->size_allocate = gtk_toggle_button_size_allocate;
127   widget_class->expose_event = gtk_toggle_button_expose;
128   widget_class->realize = gtk_toggle_button_realize;
129   widget_class->unrealize = gtk_toggle_button_unrealize;
130   widget_class->map = gtk_toggle_button_map;
131   widget_class->unmap = gtk_toggle_button_unmap;
132
133   button_class->pressed = gtk_toggle_button_pressed;
134   button_class->released = gtk_toggle_button_released;
135   button_class->clicked = gtk_toggle_button_clicked;
136   button_class->enter = gtk_toggle_button_enter;
137   button_class->leave = gtk_toggle_button_leave;
138
139   class->toggled = NULL;
140
141   g_object_class_install_property (gobject_class,
142                                    PROP_ACTIVE,
143                                    g_param_spec_boolean ("active",
144                                                          _("Active"),
145                                                          _("If the toggle button should be pressed in or not"),
146                                                          FALSE,
147                                                          G_PARAM_READWRITE));
148
149   g_object_class_install_property (gobject_class,
150                                    PROP_INCONSISTENT,
151                                    g_param_spec_boolean ("inconsistent",
152                                                          _("Inconsistent"),
153                                                          _("If the toggle button is in an \"in between\" state."),
154                                                          FALSE,
155                                                          G_PARAM_READWRITE));
156
157   g_object_class_install_property (gobject_class,
158                                    PROP_DRAW_INDICATOR,
159                                    g_param_spec_boolean ("draw_indicator",
160                                                          _("Draw Indicator"),
161                                                          _("If the toggle part of the button is displayed"),
162                                                          FALSE,
163                                                          G_PARAM_READWRITE));
164
165   toggle_button_signals[TOGGLED] =
166     gtk_signal_new ("toggled",
167                     GTK_RUN_FIRST,
168                     GTK_CLASS_TYPE (object_class),
169                     GTK_SIGNAL_OFFSET (GtkToggleButtonClass, toggled),
170                     gtk_marshal_VOID__VOID,
171                     GTK_TYPE_NONE, 0);
172 }
173
174 static void
175 gtk_toggle_button_init (GtkToggleButton *toggle_button)
176 {
177   toggle_button->active = FALSE;
178   toggle_button->draw_indicator = FALSE;
179   GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
180 }
181
182
183 GtkWidget*
184 gtk_toggle_button_new (void)
185 {
186   return GTK_WIDGET (gtk_type_new (gtk_toggle_button_get_type ()));
187 }
188
189 GtkWidget*
190 gtk_toggle_button_new_with_label (const gchar *label)
191 {
192   GtkWidget *toggle_button;
193   GtkWidget *label_widget;
194
195   toggle_button = gtk_toggle_button_new ();
196   label_widget = gtk_label_new (label);
197   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
198
199   gtk_container_add (GTK_CONTAINER (toggle_button), label_widget);
200   gtk_widget_show (label_widget);
201
202   return toggle_button;
203 }
204
205 /**
206  * gtk_toggle_button_new_with_mnemonic:
207  * @label: the text of the button, with an underscore in front of the
208  *         mnemonic character
209  * @returns: a new #GtkToggleButton
210  *
211  * Creates a new #GtkToggleButton containing a label. The label
212  * will be created using gtk_label_new_with_mnemonic(), so underscores
213  * in @label indicate the mnemonic for the button.
214  **/
215 GtkWidget*
216 gtk_toggle_button_new_with_mnemonic (const gchar *label)
217 {
218   GtkWidget *toggle_button;
219   GtkWidget *label_widget;
220
221   toggle_button = gtk_toggle_button_new ();
222   label_widget = gtk_label_new_with_mnemonic (label);
223   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5);
224
225   gtk_container_add (GTK_CONTAINER (toggle_button), label_widget);
226   gtk_widget_show (label_widget);
227
228   return toggle_button;
229 }
230
231 static void
232 gtk_toggle_button_set_property (GObject      *object,
233                                 guint         prop_id,
234                                 const GValue *value,
235                                 GParamSpec   *pspec)
236 {
237   GtkToggleButton *tb;
238
239   tb = GTK_TOGGLE_BUTTON (object);
240
241   switch (prop_id)
242     {
243     case PROP_ACTIVE:
244       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
245       break;
246     case PROP_INCONSISTENT:
247       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
248       break;
249     case PROP_DRAW_INDICATOR:
250       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
251       break;
252     default:
253       break;
254     }
255 }
256
257 static void
258 gtk_toggle_button_get_property (GObject      *object,
259                                 guint         prop_id,
260                                 GValue       *value,
261                                 GParamSpec   *pspec)
262 {
263   GtkToggleButton *tb;
264
265   tb = GTK_TOGGLE_BUTTON (object);
266
267   switch (prop_id)
268     {
269     case PROP_ACTIVE:
270       g_value_set_boolean (value, tb->active);
271       break;
272     case PROP_INCONSISTENT:
273       g_value_set_boolean (value, tb->inconsistent);
274       break;
275     case PROP_DRAW_INDICATOR:
276       g_value_set_boolean (value, tb->draw_indicator);
277       break;
278     default:
279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280       break;
281     }
282 }
283
284 void
285 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
286                             gboolean         draw_indicator)
287 {
288   GtkWidget *widget;
289
290   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
291
292   widget = GTK_WIDGET (toggle_button);
293
294   draw_indicator = draw_indicator ? TRUE : FALSE;
295
296   if (toggle_button->draw_indicator != draw_indicator)
297     {
298       if (GTK_WIDGET_REALIZED (toggle_button))
299         {
300           gboolean visible = GTK_WIDGET_VISIBLE (toggle_button);
301
302           if (visible)
303             gtk_widget_hide (widget);
304
305           gtk_widget_unrealize (widget);
306           toggle_button->draw_indicator = draw_indicator;
307
308           if (toggle_button->draw_indicator)
309             GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW);
310           else
311             GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
312           
313           gtk_widget_realize (widget);
314
315           if (visible)
316             gtk_widget_show (widget);
317         }
318       else
319         {
320           toggle_button->draw_indicator = draw_indicator;
321
322           if (toggle_button->draw_indicator)
323             GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW);
324           else
325             GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
326         }
327
328       if (GTK_WIDGET_VISIBLE (toggle_button))
329         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
330
331       g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
332     }
333 }
334
335 /**
336  * gtk_toggle_button_get_mode:
337  * @toggle_button: a #GtkToggleButton
338  *
339  * Retrieves whether the button is displayed as a separate indicator
340  * and label. See gtk_toggle_button_set_mode().
341  *
342  * Return value: %TRUE if the togglebutton is drawn as a separate indicator
343  *   and label.
344  **/
345 gboolean
346 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
347 {
348   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
349
350   return toggle_button->draw_indicator;
351 }
352
353 void
354 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
355                               gboolean         is_active)
356 {
357   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
358
359   is_active = is_active != FALSE;
360
361   if (toggle_button->active != is_active)
362     gtk_button_clicked (GTK_BUTTON (toggle_button));
363 }
364
365
366 gboolean
367 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
368 {
369   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
370
371   return (toggle_button->active) ? TRUE : FALSE;
372 }
373
374
375 void
376 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
377 {
378   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
379
380   gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]);
381 }
382
383 /**
384  * gtk_toggle_button_set_inconsistent:
385  * @toggle_button: a #GtkToggleButton
386  * @setting: %TRUE if state is inconsistent
387  *
388  * If the user has selected a range of elements (such as some text or
389  * spreadsheet cells) that are affected by a toggle button, and the
390  * current values in that range are inconsistent, you may want to
391  * display the toggle in an "in between" state. This function turns on
392  * "in between" display.  Normally you would turn off the inconsistent
393  * state again if the user toggles the toggle button. This has to be
394  * done manually, gtk_toggle_button_set_inconsistent() only affects
395  * visual appearance, it doesn't affect the semantics of the button.
396  * 
397  **/
398 void
399 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
400                                     gboolean         setting)
401 {
402   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
403   
404   setting = setting != FALSE;
405
406   if (setting != toggle_button->inconsistent)
407     {
408       toggle_button->inconsistent = setting;
409       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
410
411       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
412     }
413 }
414
415 /**
416  * gtk_toggle_button_get_inconsistent:
417  * @toggle_button: a #GtkToggleButton
418  * 
419  * Gets the value set by gtk_toggle_button_set_inconsistent().
420  * 
421  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
422  **/
423 gboolean
424 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
425 {
426   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
427
428   return toggle_button->inconsistent;
429 }
430
431 static void
432 gtk_toggle_button_paint (GtkWidget    *widget,
433                          GdkRectangle *area)
434 {
435   GtkButton *button;
436   GtkToggleButton *toggle_button;
437   GtkShadowType shadow_type;
438   GtkStateType state_type;
439   gint width, height;
440   gboolean interior_focus;
441   gint x, y;
442
443   button = GTK_BUTTON (widget);
444   toggle_button = GTK_TOGGLE_BUTTON (widget);
445
446   if (GTK_WIDGET_DRAWABLE (widget))
447     {
448       gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
449       
450       x = 0;
451       y = 0;
452       width = widget->allocation.width - GTK_CONTAINER (widget)->border_width * 2;
453       height = widget->allocation.height - GTK_CONTAINER (widget)->border_width * 2;
454
455       gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
456       gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
457
458       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
459           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
460         {
461           gtk_paint_box (widget->style, widget->window,
462                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
463                          area, widget, "togglebuttondefault",
464                          x, y, width, height);
465         }
466
467       if (GTK_WIDGET_CAN_DEFAULT (widget))
468         {
469           x += widget->style->xthickness;
470           y += widget->style->ythickness;
471           width -= 2 * x + DEFAULT_SPACING;
472           height -= 2 * y + DEFAULT_SPACING;
473           x += DEFAULT_LEFT_POS;
474           y += DEFAULT_TOP_POS;
475         }
476
477       if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
478         {
479           x += 1;
480           y += 1;
481           width -= 2;
482           height -= 2;
483         }
484
485       state_type = GTK_WIDGET_STATE (widget);
486       
487       if (toggle_button->inconsistent)
488         {
489           if (state_type == GTK_STATE_ACTIVE)
490             state_type = GTK_STATE_NORMAL;
491           shadow_type = GTK_SHADOW_ETCHED_IN;
492         }
493       else if ((GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE) ||
494           toggle_button->active)
495         shadow_type = GTK_SHADOW_IN;
496       else
497         shadow_type = GTK_SHADOW_OUT;
498
499       if (button->relief != GTK_RELIEF_NONE ||
500           (GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL &&
501            GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE))
502         gtk_paint_box (widget->style, widget->window,
503                        state_type,
504                        shadow_type, area, widget, "togglebutton",
505                        x, y, width, height);
506       
507       if (GTK_WIDGET_HAS_FOCUS (widget))
508         {
509           if (interior_focus)
510             {
511               x += widget->style->xthickness + 1;
512               y += widget->style->xthickness + 1;
513               width -= 2 * (widget->style->xthickness + 1);
514               height -= 2 * (widget->style->ythickness + 1);
515             }
516           else
517             {
518               x -= 1;
519               y -= 1;
520               width += 2;
521               height += 2;
522             }
523
524           gtk_paint_focus (widget->style, widget->window,
525                            area, widget, "togglebutton",
526                            x, y, width - 1, height - 1);
527         }
528     }
529 }
530
531 static void
532 gtk_toggle_button_size_allocate (GtkWidget     *widget,
533                                  GtkAllocation *allocation)
534 {
535   if (!GTK_WIDGET_NO_WINDOW (widget) &&
536       GTK_WIDGET_CLASS (parent_class)->size_allocate)
537     GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
538 }
539
540 static gint
541 gtk_toggle_button_expose (GtkWidget      *widget,
542                           GdkEventExpose *event)
543 {
544   if (GTK_WIDGET_DRAWABLE (widget))
545     {
546       GtkWidget *child = GTK_BIN (widget)->child;
547
548       gtk_toggle_button_paint (widget, &event->area);
549
550       if (child)
551         gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
552     }
553   
554   return TRUE;
555 }
556
557 static void
558 gtk_toggle_button_pressed (GtkButton *button)
559 {
560   GtkToggleButton *toggle_button;
561   GtkStateType new_state;
562
563   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
564
565   toggle_button = GTK_TOGGLE_BUTTON (button);
566
567   button->button_down = TRUE;
568
569   if (toggle_button->active)
570     new_state = (button->in_button ? GTK_STATE_NORMAL : GTK_STATE_ACTIVE);
571   else
572     new_state = (button->in_button ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
573
574   if (GTK_WIDGET_STATE (button) != new_state)
575     gtk_widget_set_state (GTK_WIDGET (button), new_state);
576 }
577
578 static void
579 gtk_toggle_button_released (GtkButton *button)
580 {
581   GtkToggleButton *toggle_button;
582   GtkStateType new_state;
583
584   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
585
586   if (button->button_down)
587     {
588       toggle_button = GTK_TOGGLE_BUTTON (button);
589
590       button->button_down = FALSE;
591
592       if (button->in_button)
593         {
594           gtk_button_clicked (button);
595         }
596       else
597         {
598           if (toggle_button->active)
599             new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
600           else
601             new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
602
603           if (GTK_WIDGET_STATE (button) != new_state)
604             gtk_widget_set_state (GTK_WIDGET (button), new_state);
605         }
606     }
607 }
608
609 static void
610 gtk_toggle_button_clicked (GtkButton *button)
611 {
612   GtkToggleButton *toggle_button;
613   GtkStateType new_state;
614
615   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
616
617   toggle_button = GTK_TOGGLE_BUTTON (button);
618   toggle_button->active = !toggle_button->active;
619
620   gtk_toggle_button_toggled (toggle_button);
621
622   if (toggle_button->active)
623     new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
624   else
625     new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
626
627   if (GTK_WIDGET_STATE (button) != new_state)
628     gtk_widget_set_state (GTK_WIDGET (button), new_state);
629   else
630     gtk_widget_queue_draw (GTK_WIDGET (button));
631
632   g_object_notify (G_OBJECT (toggle_button), "active");
633 }
634
635 static void
636 gtk_toggle_button_enter (GtkButton *button)
637 {
638   GtkToggleButton *toggle_button;
639   GtkStateType new_state;
640
641   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
642
643   toggle_button = GTK_TOGGLE_BUTTON (button);
644
645   if (toggle_button->active)
646     new_state = (button->button_down ? GTK_STATE_NORMAL : GTK_STATE_PRELIGHT);
647   else
648     new_state = (button->button_down ? GTK_STATE_ACTIVE : GTK_STATE_PRELIGHT);
649
650   if (GTK_WIDGET_STATE (button) != new_state)
651     gtk_widget_set_state (GTK_WIDGET (button), new_state);
652 }
653
654 static void
655 gtk_toggle_button_leave (GtkButton *button)
656 {
657   GtkToggleButton *toggle_button;
658   GtkStateType new_state;
659
660   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
661
662   toggle_button = GTK_TOGGLE_BUTTON (button);
663
664   new_state = (toggle_button->active ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
665
666   if (GTK_WIDGET_STATE (button) != new_state)
667     gtk_widget_set_state (GTK_WIDGET (button), new_state);
668 }
669
670 static void
671 gtk_toggle_button_realize (GtkWidget *widget)
672 {
673   GtkToggleButton *toggle_button;
674   GdkWindowAttr attributes;
675   gint attributes_mask;
676   gint border_width;
677   
678   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
679   
680   toggle_button = GTK_TOGGLE_BUTTON (widget);
681   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
682   
683   border_width = GTK_CONTAINER (widget)->border_width;
684   
685   attributes.window_type = GDK_WINDOW_CHILD;
686   attributes.x = widget->allocation.x + border_width;
687   attributes.y = widget->allocation.y + border_width;
688   attributes.width = widget->allocation.width - border_width * 2;
689   attributes.height = widget->allocation.height - border_width * 2;
690   attributes.event_mask = gtk_widget_get_events (widget);
691   attributes.event_mask |= (GDK_EXPOSURE_MASK |
692                             GDK_BUTTON_PRESS_MASK |
693                             GDK_BUTTON_RELEASE_MASK |
694                             GDK_ENTER_NOTIFY_MASK |
695                             GDK_LEAVE_NOTIFY_MASK);
696
697   if (GTK_WIDGET_NO_WINDOW (widget))
698     {
699       attributes.wclass = GDK_INPUT_ONLY;
700       attributes_mask = GDK_WA_X | GDK_WA_Y;
701
702       widget->window = gtk_widget_get_parent_window (widget);
703       gdk_window_ref (widget->window);
704       
705       toggle_button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
706                                                     &attributes, attributes_mask);
707       gdk_window_set_user_data (toggle_button->event_window, toggle_button);
708     }
709   else
710     {
711       attributes.wclass = GDK_INPUT_OUTPUT;
712       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
713       attributes.visual = gtk_widget_get_visual (widget);
714       attributes.colormap = gtk_widget_get_colormap (widget);
715       widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
716                                        &attributes, attributes_mask);
717       gdk_window_set_user_data (widget->window, toggle_button);
718     }
719
720   widget->style = gtk_style_attach (widget->style, widget->window);
721
722   if (!GTK_WIDGET_NO_WINDOW (widget))
723     gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
724 }
725   
726 static void
727 gtk_toggle_button_unrealize (GtkWidget *widget)
728 {
729   GtkToggleButton *toggle_button;
730   
731   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
732
733   toggle_button = GTK_TOGGLE_BUTTON (widget);
734   
735   if (GTK_WIDGET_NO_WINDOW (widget))
736     {
737       gdk_window_set_user_data (toggle_button->event_window, NULL);
738       gdk_window_destroy (toggle_button->event_window);
739       toggle_button->event_window = NULL;
740     }
741
742   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
743     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
744 }
745
746 static void
747 gtk_toggle_button_map (GtkWidget *widget)
748 {
749   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
750
751   if (GTK_WIDGET_NO_WINDOW (widget))
752     gdk_window_show (GTK_TOGGLE_BUTTON (widget)->event_window);
753
754   GTK_WIDGET_CLASS (parent_class)->map (widget);
755 }
756
757 static void
758 gtk_toggle_button_unmap (GtkWidget *widget)
759 {
760   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
761
762   if (GTK_WIDGET_NO_WINDOW (widget))
763     gdk_window_hide (GTK_TOGGLE_BUTTON (widget)->event_window);
764
765   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
766 }