]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
Remove creative formatting.
[~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 static void
206 gtk_toggle_button_set_property (GObject      *object,
207                                 guint         prop_id,
208                                 const GValue *value,
209                                 GParamSpec   *pspec)
210 {
211   GtkToggleButton *tb;
212
213   tb = GTK_TOGGLE_BUTTON (object);
214
215   switch (prop_id)
216     {
217     case PROP_ACTIVE:
218       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
219       break;
220     case PROP_INCONSISTENT:
221       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
222       break;
223     case PROP_DRAW_INDICATOR:
224       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
225       break;
226     default:
227       break;
228     }
229 }
230
231 static void
232 gtk_toggle_button_get_property (GObject      *object,
233                                 guint         prop_id,
234                                 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       g_value_set_boolean (value, tb->active);
245       break;
246     case PROP_INCONSISTENT:
247       g_value_set_boolean (value, tb->inconsistent);
248       break;
249     case PROP_DRAW_INDICATOR:
250       g_value_set_boolean (value, tb->draw_indicator);
251       break;
252     default:
253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254       break;
255     }
256 }
257
258 void
259 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
260                             gboolean         draw_indicator)
261 {
262   GtkWidget *widget;
263
264   g_return_if_fail (toggle_button != NULL);
265   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
266
267   widget = GTK_WIDGET (toggle_button);
268
269   draw_indicator = draw_indicator ? TRUE : FALSE;
270
271   if (toggle_button->draw_indicator != draw_indicator)
272     {
273       if (GTK_WIDGET_REALIZED (toggle_button))
274         {
275           gboolean visible = GTK_WIDGET_VISIBLE (toggle_button);
276
277           if (visible)
278             gtk_widget_hide (widget);
279
280           gtk_widget_unrealize (widget);
281           toggle_button->draw_indicator = draw_indicator;
282
283           if (toggle_button->draw_indicator)
284             GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW);
285           else
286             GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
287           
288           gtk_widget_realize (widget);
289
290           if (visible)
291             gtk_widget_show (widget);
292         }
293       else
294         {
295           toggle_button->draw_indicator = draw_indicator;
296
297           if (toggle_button->draw_indicator)
298             GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW);
299           else
300             GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW);
301         }
302
303       if (GTK_WIDGET_VISIBLE (toggle_button))
304         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
305
306       g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
307     }
308 }
309
310
311 void
312 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
313                               gboolean         is_active)
314 {
315   g_return_if_fail (toggle_button != NULL);
316   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
317
318   is_active = is_active != FALSE;
319
320   if (toggle_button->active != is_active)
321     gtk_button_clicked (GTK_BUTTON (toggle_button));
322 }
323
324
325 gboolean
326 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
327 {
328   g_return_val_if_fail (toggle_button != NULL, FALSE);
329   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
330
331   return (toggle_button->active) ? TRUE : FALSE;
332 }
333
334
335 void
336 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
337 {
338   g_return_if_fail (toggle_button != NULL);
339   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
340
341   gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]);
342 }
343
344 /**
345  * gtk_toggle_button_set_inconsistent:
346  * @toggle_button: a #GtkToggleButton
347  * @setting: %TRUE if state is inconsistent
348  *
349  * If the user has selected a range of elements (such as some text or
350  * spreadsheet cells) that are affected by a toggle button, and the
351  * current values in that range are inconsistent, you may want to
352  * display the toggle in an "in between" state. This function turns on
353  * "in between" display.  Normally you would turn off the inconsistent
354  * state again if the user toggles the toggle button. This has to be
355  * done manually, gtk_toggle_button_set_inconsistent() only affects
356  * visual appearance, it doesn't affect the semantics of the button.
357  * 
358  **/
359 void
360 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
361                                     gboolean         setting)
362 {
363   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
364   
365   setting = setting != FALSE;
366
367   if (setting != toggle_button->inconsistent)
368     {
369       toggle_button->inconsistent = setting;
370       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
371
372       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
373     }
374 }
375
376 /**
377  * gtk_toggle_button_get_inconsistent:
378  * @toggle_button: a #GtkToggleButton
379  * 
380  * Gets the value set by gtk_toggle_button_set_inconsistent().
381  * 
382  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
383  **/
384 gboolean
385 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
386 {
387   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
388
389   return toggle_button->inconsistent;
390 }
391
392 static void
393 gtk_toggle_button_paint (GtkWidget    *widget,
394                          GdkRectangle *area)
395 {
396   GtkButton *button;
397   GtkToggleButton *toggle_button;
398   GtkShadowType shadow_type;
399   GtkStateType state_type;
400   gint width, height;
401   gboolean interior_focus;
402   gint x, y;
403
404   button = GTK_BUTTON (widget);
405   toggle_button = GTK_TOGGLE_BUTTON (widget);
406
407   if (GTK_WIDGET_DRAWABLE (widget))
408     {
409       gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
410       
411       x = 0;
412       y = 0;
413       width = widget->allocation.width - GTK_CONTAINER (widget)->border_width * 2;
414       height = widget->allocation.height - GTK_CONTAINER (widget)->border_width * 2;
415
416       gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
417       gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
418
419       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
420           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
421         {
422           gtk_paint_box (widget->style, widget->window,
423                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
424                          area, widget, "togglebuttondefault",
425                          x, y, width, height);
426         }
427
428       if (GTK_WIDGET_CAN_DEFAULT (widget))
429         {
430           x += widget->style->xthickness;
431           y += widget->style->ythickness;
432           width -= 2 * x + DEFAULT_SPACING;
433           height -= 2 * y + DEFAULT_SPACING;
434           x += DEFAULT_LEFT_POS;
435           y += DEFAULT_TOP_POS;
436         }
437
438       if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
439         {
440           x += 1;
441           y += 1;
442           width -= 2;
443           height -= 2;
444         }
445
446       state_type = GTK_WIDGET_STATE (widget);
447       
448       if (toggle_button->inconsistent)
449         {
450           if (state_type == GTK_STATE_ACTIVE)
451             state_type = GTK_STATE_NORMAL;
452           shadow_type = GTK_SHADOW_ETCHED_IN;
453         }
454       else if ((GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE) ||
455           toggle_button->active)
456         shadow_type = GTK_SHADOW_IN;
457       else
458         shadow_type = GTK_SHADOW_OUT;
459
460       if (button->relief != GTK_RELIEF_NONE ||
461           (GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL &&
462            GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE))
463         gtk_paint_box (widget->style, widget->window,
464                        state_type,
465                        shadow_type, area, widget, "togglebutton",
466                        x, y, width, height);
467       
468       if (GTK_WIDGET_HAS_FOCUS (widget))
469         {
470           if (interior_focus)
471             {
472               x += widget->style->xthickness + 1;
473               y += widget->style->xthickness + 1;
474               width -= 2 * (widget->style->xthickness + 1);
475               height -= 2 * (widget->style->ythickness + 1);
476             }
477           else
478             {
479               x -= 1;
480               y -= 1;
481               width += 2;
482               height += 2;
483             }
484
485           gtk_paint_focus (widget->style, widget->window,
486                            area, widget, "togglebutton",
487                            x, y, width - 1, height - 1);
488         }
489     }
490 }
491
492 static void
493 gtk_toggle_button_size_allocate (GtkWidget     *widget,
494                                  GtkAllocation *allocation)
495 {
496   if (!GTK_WIDGET_NO_WINDOW (widget) &&
497       GTK_WIDGET_CLASS (parent_class)->size_allocate)
498     GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
499 }
500
501 static gint
502 gtk_toggle_button_expose (GtkWidget      *widget,
503                           GdkEventExpose *event)
504 {
505   if (GTK_WIDGET_DRAWABLE (widget))
506     {
507       GtkWidget *child = GTK_BIN (widget)->child;
508
509       gtk_toggle_button_paint (widget, &event->area);
510
511       if (child)
512         gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
513     }
514   
515   return TRUE;
516 }
517
518 static void
519 gtk_toggle_button_pressed (GtkButton *button)
520 {
521   GtkToggleButton *toggle_button;
522   GtkStateType new_state;
523
524   g_return_if_fail (button != NULL);
525   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
526
527   toggle_button = GTK_TOGGLE_BUTTON (button);
528
529   button->button_down = TRUE;
530
531   if (toggle_button->active)
532     new_state = (button->in_button ? GTK_STATE_NORMAL : GTK_STATE_ACTIVE);
533   else
534     new_state = (button->in_button ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
535
536   if (GTK_WIDGET_STATE (button) != new_state)
537     gtk_widget_set_state (GTK_WIDGET (button), new_state);
538 }
539
540 static void
541 gtk_toggle_button_released (GtkButton *button)
542 {
543   GtkToggleButton *toggle_button;
544   GtkStateType new_state;
545
546   g_return_if_fail (button != NULL);
547   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
548
549   if (button->button_down)
550     {
551       toggle_button = GTK_TOGGLE_BUTTON (button);
552
553       button->button_down = FALSE;
554
555       if (button->in_button)
556         {
557           gtk_button_clicked (button);
558         }
559       else
560         {
561           if (toggle_button->active)
562             new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
563           else
564             new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
565
566           if (GTK_WIDGET_STATE (button) != new_state)
567             gtk_widget_set_state (GTK_WIDGET (button), new_state);
568         }
569     }
570 }
571
572 static void
573 gtk_toggle_button_clicked (GtkButton *button)
574 {
575   GtkToggleButton *toggle_button;
576   GtkStateType new_state;
577
578   g_return_if_fail (button != NULL);
579   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
580
581   toggle_button = GTK_TOGGLE_BUTTON (button);
582   toggle_button->active = !toggle_button->active;
583
584   gtk_toggle_button_toggled (toggle_button);
585
586   if (toggle_button->active)
587     new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE);
588   else
589     new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL);
590
591   if (GTK_WIDGET_STATE (button) != new_state)
592     gtk_widget_set_state (GTK_WIDGET (button), new_state);
593   else
594     gtk_widget_queue_draw (GTK_WIDGET (button));
595
596   g_object_notify (G_OBJECT (toggle_button), "active");
597 }
598
599 static void
600 gtk_toggle_button_enter (GtkButton *button)
601 {
602   GtkToggleButton *toggle_button;
603   GtkStateType new_state;
604
605   g_return_if_fail (button != NULL);
606   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
607
608   toggle_button = GTK_TOGGLE_BUTTON (button);
609
610   if (toggle_button->active)
611     new_state = (button->button_down ? GTK_STATE_NORMAL : GTK_STATE_PRELIGHT);
612   else
613     new_state = (button->button_down ? GTK_STATE_ACTIVE : GTK_STATE_PRELIGHT);
614
615   if (GTK_WIDGET_STATE (button) != new_state)
616     gtk_widget_set_state (GTK_WIDGET (button), new_state);
617 }
618
619 static void
620 gtk_toggle_button_leave (GtkButton *button)
621 {
622   GtkToggleButton *toggle_button;
623   GtkStateType new_state;
624
625   g_return_if_fail (button != NULL);
626   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button));
627
628   toggle_button = GTK_TOGGLE_BUTTON (button);
629
630   new_state = (toggle_button->active ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL);
631
632   if (GTK_WIDGET_STATE (button) != new_state)
633     gtk_widget_set_state (GTK_WIDGET (button), new_state);
634 }
635
636 static void
637 gtk_toggle_button_realize (GtkWidget *widget)
638 {
639   GtkToggleButton *toggle_button;
640   GdkWindowAttr attributes;
641   gint attributes_mask;
642   gint border_width;
643   
644   g_return_if_fail (widget != NULL);
645   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
646   
647   toggle_button = GTK_TOGGLE_BUTTON (widget);
648   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
649   
650   border_width = GTK_CONTAINER (widget)->border_width;
651   
652   attributes.window_type = GDK_WINDOW_CHILD;
653   attributes.x = widget->allocation.x + border_width;
654   attributes.y = widget->allocation.y + border_width;
655   attributes.width = widget->allocation.width - border_width * 2;
656   attributes.height = widget->allocation.height - border_width * 2;
657   attributes.event_mask = gtk_widget_get_events (widget);
658   attributes.event_mask |= (GDK_EXPOSURE_MASK |
659                             GDK_BUTTON_PRESS_MASK |
660                             GDK_BUTTON_RELEASE_MASK |
661                             GDK_ENTER_NOTIFY_MASK |
662                             GDK_LEAVE_NOTIFY_MASK);
663
664   if (GTK_WIDGET_NO_WINDOW (widget))
665     {
666       attributes.wclass = GDK_INPUT_ONLY;
667       attributes_mask = GDK_WA_X | GDK_WA_Y;
668
669       widget->window = gtk_widget_get_parent_window (widget);
670       gdk_window_ref (widget->window);
671       
672       toggle_button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
673                                                     &attributes, attributes_mask);
674       gdk_window_set_user_data (toggle_button->event_window, toggle_button);
675     }
676   else
677     {
678       attributes.wclass = GDK_INPUT_OUTPUT;
679       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
680       attributes.visual = gtk_widget_get_visual (widget);
681       attributes.colormap = gtk_widget_get_colormap (widget);
682       widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
683                                        &attributes, attributes_mask);
684       gdk_window_set_user_data (widget->window, toggle_button);
685     }
686
687   widget->style = gtk_style_attach (widget->style, widget->window);
688
689   if (!GTK_WIDGET_NO_WINDOW (widget))
690     gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
691 }
692   
693 static void
694 gtk_toggle_button_unrealize (GtkWidget *widget)
695 {
696   GtkToggleButton *toggle_button;
697   
698   g_return_if_fail (widget != NULL);
699   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
700
701   toggle_button = GTK_TOGGLE_BUTTON (widget);
702   
703   if (GTK_WIDGET_NO_WINDOW (widget))
704     {
705       gdk_window_set_user_data (toggle_button->event_window, NULL);
706       gdk_window_destroy (toggle_button->event_window);
707       toggle_button->event_window = NULL;
708     }
709
710   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
711     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
712 }
713
714 static void
715 gtk_toggle_button_map (GtkWidget *widget)
716 {
717   g_return_if_fail (widget != NULL);
718   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
719
720   if (GTK_WIDGET_NO_WINDOW (widget))
721     gdk_window_show (GTK_TOGGLE_BUTTON (widget)->event_window);
722
723   GTK_WIDGET_CLASS (parent_class)->map (widget);
724 }
725
726 static void
727 gtk_toggle_button_unmap (GtkWidget *widget)
728 {
729   g_return_if_fail (widget != NULL);
730   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
731
732   if (GTK_WIDGET_NO_WINDOW (widget))
733     gdk_window_hide (GTK_TOGGLE_BUTTON (widget)->event_window);
734
735   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
736 }