]> Pileus Git - ~andy/gtk/blob - gtk/gtkviewport.c
deprecated floating/sink API and implemented it in terms of the GObject
[~andy/gtk] / gtk / gtkviewport.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 <config.h>
28 #include "gtkviewport.h"
29 #include "gtkintl.h"
30 #include "gtkmarshalers.h"
31 #include "gtkprivate.h"
32 #include "gtkalias.h"
33
34 enum {
35   PROP_0,
36   PROP_HADJUSTMENT,
37   PROP_VADJUSTMENT,
38   PROP_SHADOW_TYPE
39 };
40
41
42 static void gtk_viewport_class_init               (GtkViewportClass *klass);
43 static void gtk_viewport_init                     (GtkViewport      *viewport);
44 static void gtk_viewport_finalize                 (GObject          *object);
45 static void gtk_viewport_destroy                  (GtkObject        *object);
46 static void gtk_viewport_set_property             (GObject         *object,
47                                                    guint            prop_id,
48                                                    const GValue    *value,
49                                                    GParamSpec      *pspec);
50 static void gtk_viewport_get_property             (GObject         *object,
51                                                    guint            prop_id,
52                                                    GValue          *value,
53                                                    GParamSpec      *pspec);
54 static void gtk_viewport_set_scroll_adjustments   (GtkViewport      *viewport,
55                                                    GtkAdjustment    *hadjustment,
56                                                    GtkAdjustment    *vadjustment);
57 static void gtk_viewport_realize                  (GtkWidget        *widget);
58 static void gtk_viewport_unrealize                (GtkWidget        *widget);
59 static void gtk_viewport_paint                    (GtkWidget        *widget,
60                                                    GdkRectangle     *area);
61 static gint gtk_viewport_expose                   (GtkWidget        *widget,
62                                                    GdkEventExpose   *event);
63 static void gtk_viewport_add                      (GtkContainer     *container,
64                                                    GtkWidget        *widget);
65 static void gtk_viewport_size_request             (GtkWidget        *widget,
66                                                    GtkRequisition   *requisition);
67 static void gtk_viewport_size_allocate            (GtkWidget        *widget,
68                                                    GtkAllocation    *allocation);
69 static void gtk_viewport_adjustment_value_changed (GtkAdjustment    *adjustment,
70                                                    gpointer          data);
71 static void gtk_viewport_style_set                (GtkWidget *widget,
72                                                    GtkStyle  *previous_style);
73
74 static GtkBinClass *parent_class;
75
76 GType
77 gtk_viewport_get_type (void)
78 {
79   static GType viewport_type = 0;
80
81   if (!viewport_type)
82     {
83       static const GTypeInfo viewport_info =
84       {
85         sizeof (GtkViewportClass),
86         NULL,           /* base_init */
87         NULL,           /* base_finalize */
88         (GClassInitFunc) gtk_viewport_class_init,
89         NULL,           /* class_finalize */
90         NULL,           /* class_data */
91         sizeof (GtkViewport),
92         0,              /* n_preallocs */
93         (GInstanceInitFunc) gtk_viewport_init,
94       };
95
96       viewport_type = g_type_register_static (GTK_TYPE_BIN, I_("GtkViewport"),
97                                               &viewport_info, 0);
98     }
99
100   return viewport_type;
101 }
102
103 static void
104 gtk_viewport_class_init (GtkViewportClass *class)
105 {
106   GtkObjectClass *object_class;
107   GObjectClass   *gobject_class;
108   GtkWidgetClass *widget_class;
109   GtkContainerClass *container_class;
110
111   object_class = (GtkObjectClass*) class;
112   gobject_class = G_OBJECT_CLASS (class);
113   widget_class = (GtkWidgetClass*) class;
114   container_class = (GtkContainerClass*) class;
115
116   parent_class = g_type_class_peek_parent (class);
117
118   gobject_class->finalize = gtk_viewport_finalize;
119   gobject_class->set_property = gtk_viewport_set_property;
120   gobject_class->get_property = gtk_viewport_get_property;
121   object_class->destroy = gtk_viewport_destroy;
122   
123   widget_class->realize = gtk_viewport_realize;
124   widget_class->unrealize = gtk_viewport_unrealize;
125   widget_class->expose_event = gtk_viewport_expose;
126   widget_class->size_request = gtk_viewport_size_request;
127   widget_class->size_allocate = gtk_viewport_size_allocate;
128   widget_class->style_set = gtk_viewport_style_set;
129   
130   container_class->add = gtk_viewport_add;
131
132   class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;
133
134   g_object_class_install_property (gobject_class,
135                                    PROP_HADJUSTMENT,
136                                    g_param_spec_object ("hadjustment",
137                                                         P_("Horizontal adjustment"),
138                                                         P_("The GtkAdjustment that determines the values of the horizontal position for this viewport"),
139                                                         GTK_TYPE_ADJUSTMENT,
140                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
141
142   g_object_class_install_property (gobject_class,
143                                    PROP_VADJUSTMENT,
144                                    g_param_spec_object ("vadjustment",
145                                                         P_("Vertical adjustment"),
146                                                         P_("The GtkAdjustment that determines the values of the vertical position for this viewport"),
147                                                         GTK_TYPE_ADJUSTMENT,
148                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
149
150   g_object_class_install_property (gobject_class,
151                                    PROP_SHADOW_TYPE,
152                                    g_param_spec_enum ("shadow-type",
153                                                       P_("Shadow type"),
154                                                       P_("Determines how the shadowed box around the viewport is drawn"),
155                                                       GTK_TYPE_SHADOW_TYPE,
156                                                       GTK_SHADOW_IN,
157                                                       GTK_PARAM_READWRITE));
158
159   widget_class->set_scroll_adjustments_signal =
160     g_signal_new (I_("set_scroll_adjustments"),
161                   G_OBJECT_CLASS_TYPE (gobject_class),
162                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
163                   G_STRUCT_OFFSET (GtkViewportClass, set_scroll_adjustments),
164                   NULL, NULL,
165                   _gtk_marshal_VOID__OBJECT_OBJECT,
166                   G_TYPE_NONE, 2,
167                   GTK_TYPE_ADJUSTMENT,
168                   GTK_TYPE_ADJUSTMENT);
169 }
170
171 static void
172 gtk_viewport_set_property (GObject         *object,
173                            guint            prop_id,
174                            const GValue    *value,
175                            GParamSpec      *pspec)
176 {
177   GtkViewport *viewport;
178
179   viewport = GTK_VIEWPORT (object);
180
181   switch (prop_id)
182     {
183     case PROP_HADJUSTMENT:
184       gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
185       break;
186     case PROP_VADJUSTMENT:
187       gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
188       break;
189     case PROP_SHADOW_TYPE:
190       gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195     }
196 }
197
198 static void
199 gtk_viewport_get_property (GObject         *object,
200                            guint            prop_id,
201                            GValue          *value,
202                            GParamSpec      *pspec)
203 {
204   GtkViewport *viewport;
205
206   viewport = GTK_VIEWPORT (object);
207
208   switch (prop_id)
209     {
210     case PROP_HADJUSTMENT:
211       g_value_set_object (value, viewport->hadjustment);
212       break;
213     case PROP_VADJUSTMENT:
214       g_value_set_object (value, viewport->vadjustment);
215       break;
216     case PROP_SHADOW_TYPE:
217       g_value_set_enum (value, viewport->shadow_type);
218       break;
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222     }
223 }
224
225 static void
226 gtk_viewport_init (GtkViewport *viewport)
227 {
228   GTK_WIDGET_UNSET_FLAGS (viewport, GTK_NO_WINDOW);
229
230   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
231   gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
232   
233   viewport->shadow_type = GTK_SHADOW_IN;
234   viewport->view_window = NULL;
235   viewport->bin_window = NULL;
236   viewport->hadjustment = NULL;
237   viewport->vadjustment = NULL;
238 }
239
240 /**
241  * gtk_viewport_new:
242  * @hadjustment: horizontal adjustment.
243  * @vadjustment: vertical adjustment.
244  * @returns: a new #GtkViewport.
245  *
246  * Creates a new #GtkViewport with the given adjustments.
247  *
248  **/
249 GtkWidget*
250 gtk_viewport_new (GtkAdjustment *hadjustment,
251                   GtkAdjustment *vadjustment)
252 {
253   GtkWidget *viewport;
254
255   viewport = gtk_widget_new (GTK_TYPE_VIEWPORT,
256                              "hadjustment", hadjustment,
257                              "vadjustment", vadjustment,
258                              NULL);
259
260   return viewport;
261 }
262
263 #define ADJUSTMENT_POINTER(viewport, orientation)         \
264   (((orientation) == GTK_ORIENTATION_HORIZONTAL) ?        \
265      &(viewport)->hadjustment : &(viewport)->vadjustment)
266
267 static void
268 viewport_disconnect_adjustment (GtkViewport    *viewport,
269                                 GtkOrientation  orientation)
270 {
271   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
272
273   if (*adjustmentp)
274     {
275       g_signal_handlers_disconnect_by_func (*adjustmentp,
276                                             gtk_viewport_adjustment_value_changed,
277                                             viewport);
278       g_object_unref (*adjustmentp);
279       *adjustmentp = NULL;
280     }
281 }
282
283 static void
284 gtk_viewport_finalize (GObject *object)
285 {
286   GtkViewport *viewport = GTK_VIEWPORT (object);
287
288   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
289   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
290
291   G_OBJECT_CLASS (parent_class)->finalize (object);
292 }
293
294 static void
295 gtk_viewport_destroy (GtkObject *object)
296 {
297   GtkViewport *viewport = GTK_VIEWPORT (object);
298
299   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
300   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
301
302   GTK_OBJECT_CLASS (parent_class)->destroy (object);
303 }
304
305 /**
306  * gtk_viewport_get_hadjustment:
307  * @viewport: a #GtkViewport.
308  * 
309  * Returns the horizontal adjustment of the viewport.
310  *
311  * Return value: the horizontal adjustment of @viewport.
312  **/
313 GtkAdjustment*
314 gtk_viewport_get_hadjustment (GtkViewport *viewport)
315 {
316   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
317
318   if (!viewport->hadjustment)
319     gtk_viewport_set_hadjustment (viewport, NULL);
320
321   return viewport->hadjustment;
322 }
323
324 /**
325  * gtk_viewport_get_vadjustment:
326  * @viewport: a #GtkViewport.
327  * 
328  * Returns the vertical adjustment of the viewport.
329  *
330  * Return value: the vertical adjustment of @viewport.
331  **/
332 GtkAdjustment*
333 gtk_viewport_get_vadjustment (GtkViewport *viewport)
334 {
335   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
336
337   if (!viewport->vadjustment)
338     gtk_viewport_set_vadjustment (viewport, NULL);
339
340   return viewport->vadjustment;
341 }
342
343 static void
344 viewport_get_view_allocation (GtkViewport   *viewport,
345                               GtkAllocation *view_allocation)
346 {
347   GtkWidget *widget = GTK_WIDGET (viewport);
348   GtkAllocation *allocation = &widget->allocation;
349   gint border_width = GTK_CONTAINER (viewport)->border_width;
350   
351   view_allocation->x = 0;
352   view_allocation->y = 0;
353
354   if (viewport->shadow_type != GTK_SHADOW_NONE)
355     {
356       view_allocation->x = widget->style->xthickness;
357       view_allocation->y = widget->style->ythickness;
358     }
359
360   view_allocation->width = MAX (1, allocation->width - view_allocation->x * 2 - border_width * 2);
361   view_allocation->height = MAX (1, allocation->height - view_allocation->y * 2 - border_width * 2);
362 }
363
364 static void
365 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
366                              gboolean      *value_changed)
367 {
368   gdouble value = adjustment->value;
369   
370   value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
371   if (value != adjustment->value)
372     {
373       adjustment->value = value;
374       if (value_changed)
375         *value_changed = TRUE;
376     }
377   else if (value_changed)
378     *value_changed = FALSE;
379 }
380
381 static void
382 viewport_set_hadjustment_values (GtkViewport *viewport,
383                                  gboolean    *value_changed)
384 {
385   GtkBin *bin = GTK_BIN (viewport);
386   GtkAllocation view_allocation;
387   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
388   gdouble old_page_size;
389   gdouble old_upper;
390   gdouble old_value;
391   
392   viewport_get_view_allocation (viewport, &view_allocation);  
393
394   old_page_size = hadjustment->page_size;
395   old_upper = hadjustment->upper;
396   old_value = hadjustment->value;
397   hadjustment->page_size = view_allocation.width;
398   hadjustment->step_increment = view_allocation.width * 0.1;
399   hadjustment->page_increment = view_allocation.width * 0.9;
400   
401   hadjustment->lower = 0;
402
403   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
404     {
405       GtkRequisition child_requisition;
406       
407       gtk_widget_get_child_requisition (bin->child, &child_requisition);
408       hadjustment->upper = MAX (child_requisition.width, view_allocation.width);
409     }
410   else
411     hadjustment->upper = view_allocation.width;
412
413   if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL) 
414     {
415       gdouble dist = old_upper - (old_value + old_page_size);
416       hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
417       viewport_reclamp_adjustment (hadjustment, value_changed);
418       *value_changed = (old_value != hadjustment->value);
419     }
420   else
421     viewport_reclamp_adjustment (hadjustment, value_changed);
422 }
423
424 static void
425 viewport_set_vadjustment_values (GtkViewport *viewport,
426                                  gboolean    *value_changed)
427 {
428   GtkBin *bin = GTK_BIN (viewport);
429   GtkAllocation view_allocation;
430   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
431
432   viewport_get_view_allocation (viewport, &view_allocation);  
433
434   vadjustment->page_size = view_allocation.height;
435   vadjustment->step_increment = view_allocation.height * 0.1;
436   vadjustment->page_increment = view_allocation.height * 0.9;
437   
438   vadjustment->lower = 0;
439
440   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
441     {
442       GtkRequisition child_requisition;
443       
444       gtk_widget_get_child_requisition (bin->child, &child_requisition);
445       vadjustment->upper = MAX (child_requisition.height, view_allocation.height);
446     }
447   else
448     vadjustment->upper = view_allocation.height;
449
450   viewport_reclamp_adjustment (vadjustment, value_changed);
451 }
452
453 static void
454 viewport_set_adjustment (GtkViewport    *viewport,
455                          GtkOrientation  orientation,
456                          GtkAdjustment  *adjustment)
457 {
458   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
459   gboolean value_changed;
460
461   if (adjustment && adjustment == *adjustmentp)
462     return;
463
464   if (!adjustment)
465     adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
466                                                      0.0, 0.0, 0.0));
467   if (*adjustmentp)
468     g_object_unref (*adjustmentp);
469   *adjustmentp = adjustment;
470   g_object_ref_sink (adjustment);
471
472   if (orientation == GTK_ORIENTATION_HORIZONTAL)
473     viewport_set_hadjustment_values (viewport, &value_changed);
474   else
475     viewport_set_vadjustment_values (viewport, &value_changed);
476
477   g_signal_connect (adjustment, "value_changed",
478                     G_CALLBACK (gtk_viewport_adjustment_value_changed),
479                     viewport);
480
481   gtk_adjustment_changed (adjustment);
482   
483   if (value_changed)
484     gtk_adjustment_value_changed (adjustment);
485   else
486     gtk_viewport_adjustment_value_changed (adjustment, viewport);
487 }
488
489 /**
490  * gtk_viewport_set_hadjustment:
491  * @viewport: a #GtkViewport.
492  * @adjustment: a #GtkAdjustment.
493  * 
494  * Sets the horizontal adjustment of the viewport.
495  **/
496 void
497 gtk_viewport_set_hadjustment (GtkViewport   *viewport,
498                               GtkAdjustment *adjustment)
499 {
500   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
501   if (adjustment)
502     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
503
504   viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
505
506   g_object_notify (G_OBJECT (viewport), "hadjustment");
507 }
508
509 /**
510  * gtk_viewport_set_vadjustment:
511  * @viewport: a #GtkViewport.
512  * @adjustment: a #GtkAdjustment.
513  * 
514  * Sets the vertical adjustment of the viewport.
515  **/
516 void
517 gtk_viewport_set_vadjustment (GtkViewport   *viewport,
518                               GtkAdjustment *adjustment)
519 {
520   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
521   if (adjustment)
522     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
523
524   viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
525
526   g_object_notify (G_OBJECT (viewport), "vadjustment");
527 }
528
529 static void
530 gtk_viewport_set_scroll_adjustments (GtkViewport      *viewport,
531                                      GtkAdjustment    *hadjustment,
532                                      GtkAdjustment    *vadjustment)
533 {
534   gtk_viewport_set_hadjustment (viewport, hadjustment);
535   gtk_viewport_set_vadjustment (viewport, vadjustment);
536 }
537
538 /** 
539  * gtk_viewport_set_shadow_type:
540  * @viewport: a #GtkViewport.
541  * @type: the new shadow type.
542  *
543  * Sets the shadow type of the viewport.
544  **/ 
545 void
546 gtk_viewport_set_shadow_type (GtkViewport   *viewport,
547                               GtkShadowType  type)
548 {
549   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
550
551   if ((GtkShadowType) viewport->shadow_type != type)
552     {
553       viewport->shadow_type = type;
554
555       if (GTK_WIDGET_VISIBLE (viewport))
556         {
557           gtk_widget_size_allocate (GTK_WIDGET (viewport), &(GTK_WIDGET (viewport)->allocation));
558           gtk_widget_queue_draw (GTK_WIDGET (viewport));
559         }
560
561       g_object_notify (G_OBJECT (viewport), "shadow-type");
562     }
563 }
564
565 /**
566  * gtk_viewport_get_shadow_type:
567  * @viewport: a #GtkViewport
568  *
569  * Gets the shadow type of the #GtkViewport. See
570  * gtk_viewport_set_shadow_type().
571  
572  * Return value: the shadow type 
573  **/
574 GtkShadowType
575 gtk_viewport_get_shadow_type (GtkViewport *viewport)
576 {
577   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
578
579   return viewport->shadow_type;
580 }
581
582 static void
583 gtk_viewport_realize (GtkWidget *widget)
584 {
585   GtkViewport *viewport = GTK_VIEWPORT (widget);
586   GtkBin *bin = GTK_BIN (widget);
587   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
588   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
589   gint border_width = GTK_CONTAINER (widget)->border_width;
590   
591   GtkAllocation view_allocation;
592   GdkWindowAttr attributes;
593   gint attributes_mask;
594   gint event_mask;
595
596   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
597
598   attributes.x = widget->allocation.x + border_width;
599   attributes.y = widget->allocation.y + border_width;
600   attributes.width = widget->allocation.width - border_width * 2;
601   attributes.height = widget->allocation.height - border_width * 2;
602   attributes.window_type = GDK_WINDOW_CHILD;
603   attributes.wclass = GDK_INPUT_OUTPUT;
604   attributes.visual = gtk_widget_get_visual (widget);
605   attributes.colormap = gtk_widget_get_colormap (widget);
606
607   event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
608   /* We select on button_press_mask so that button 4-5 scrolls are trapped.
609    */
610   attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
611
612   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
613
614   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
615                                    &attributes, attributes_mask);
616   gdk_window_set_user_data (widget->window, viewport);
617
618   viewport_get_view_allocation (viewport, &view_allocation);
619   
620   attributes.x = view_allocation.x;
621   attributes.y = view_allocation.y;
622   attributes.width = view_allocation.width;
623   attributes.height = view_allocation.height;
624   attributes.event_mask = 0;
625
626   viewport->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
627   gdk_window_set_user_data (viewport->view_window, viewport);
628
629   gdk_window_set_back_pixmap (viewport->view_window, NULL, FALSE);
630   
631   attributes.x = - hadjustment->value;
632   attributes.y = - vadjustment->value;
633   attributes.width = hadjustment->upper;
634   attributes.height = vadjustment->upper;
635   
636   attributes.event_mask = event_mask;
637
638   viewport->bin_window = gdk_window_new (viewport->view_window, &attributes, attributes_mask);
639   gdk_window_set_user_data (viewport->bin_window, viewport);
640
641   if (bin->child)
642     gtk_widget_set_parent_window (bin->child, viewport->bin_window);
643
644   widget->style = gtk_style_attach (widget->style, widget->window);
645   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
646   gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
647
648   /* Call paint here to allow a theme to set the background without flashing
649    */
650   gtk_paint_flat_box(widget->style, viewport->bin_window, GTK_STATE_NORMAL,
651                      GTK_SHADOW_NONE,
652                      NULL, widget, "viewportbin",
653                      0, 0, -1, -1);
654    
655   gdk_window_show (viewport->bin_window);
656   gdk_window_show (viewport->view_window);
657 }
658
659 static void
660 gtk_viewport_unrealize (GtkWidget *widget)
661 {
662   GtkViewport *viewport = GTK_VIEWPORT (widget);
663
664   gdk_window_set_user_data (viewport->view_window, NULL);
665   gdk_window_destroy (viewport->view_window);
666   viewport->view_window = NULL;
667
668   gdk_window_set_user_data (viewport->bin_window, NULL);
669   gdk_window_destroy (viewport->bin_window);
670   viewport->bin_window = NULL;
671
672   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
673     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
674 }
675
676 static void
677 gtk_viewport_paint (GtkWidget    *widget,
678                     GdkRectangle *area)
679 {
680   GtkViewport *viewport;
681
682   g_return_if_fail (GTK_IS_VIEWPORT (widget));
683   g_return_if_fail (area != NULL);
684
685   if (GTK_WIDGET_DRAWABLE (widget))
686     {
687       viewport = GTK_VIEWPORT (widget);
688
689       gtk_paint_shadow (widget->style, widget->window,
690                         GTK_STATE_NORMAL, viewport->shadow_type,
691                         NULL, widget, "viewport",
692                         0, 0, -1, -1);
693     }
694 }
695
696 static gint
697 gtk_viewport_expose (GtkWidget      *widget,
698                      GdkEventExpose *event)
699 {
700   GtkViewport *viewport;
701
702   if (GTK_WIDGET_DRAWABLE (widget))
703     {
704       viewport = GTK_VIEWPORT (widget);
705
706       if (event->window == widget->window)
707         gtk_viewport_paint (widget, &event->area);
708       else if (event->window == viewport->bin_window)
709         {
710           gtk_paint_flat_box(widget->style, viewport->bin_window, 
711                              GTK_STATE_NORMAL, GTK_SHADOW_NONE,
712                              &event->area, widget, "viewportbin",
713                              0, 0, -1, -1);
714           
715           (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
716         }
717     }
718
719   return FALSE;
720 }
721
722 static void
723 gtk_viewport_add (GtkContainer *container,
724                   GtkWidget    *child)
725 {
726   GtkBin *bin;
727
728   g_return_if_fail (GTK_IS_WIDGET (child));
729
730   bin = GTK_BIN (container);
731   g_return_if_fail (bin->child == NULL);
732
733   gtk_widget_set_parent_window (child, GTK_VIEWPORT (bin)->bin_window);
734
735   GTK_CONTAINER_CLASS (parent_class)->add (container, child);
736 }
737
738 static void
739 gtk_viewport_size_request (GtkWidget      *widget,
740                            GtkRequisition *requisition)
741 {
742   GtkBin *bin;
743   GtkRequisition child_requisition;
744
745   bin = GTK_BIN (widget);
746
747   requisition->width = (GTK_CONTAINER (widget)->border_width +
748                         GTK_WIDGET (widget)->style->xthickness) * 2;
749
750   requisition->height = (GTK_CONTAINER (widget)->border_width * 2 +
751                          GTK_WIDGET (widget)->style->ythickness) * 2;
752
753   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
754     {
755       gtk_widget_size_request (bin->child, &child_requisition);
756       requisition->width += child_requisition.width;
757       requisition->height += child_requisition.height;
758     }
759 }
760
761 static void
762 gtk_viewport_size_allocate (GtkWidget     *widget,
763                             GtkAllocation *allocation)
764 {
765   GtkViewport *viewport = GTK_VIEWPORT (widget);
766   GtkBin *bin = GTK_BIN (widget);
767   gint border_width = GTK_CONTAINER (widget)->border_width;
768   gboolean hadjustment_value_changed, vadjustment_value_changed;
769
770   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
771   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
772
773   /* If our size changed, and we have a shadow, queue a redraw on widget->window to
774    * redraw the shadow correctly.
775    */
776   if (GTK_WIDGET_MAPPED (widget) &&
777       viewport->shadow_type != GTK_SHADOW_NONE &&
778       (widget->allocation.width != allocation->width ||
779        widget->allocation.height != allocation->height))
780     gdk_window_invalidate_rect (widget->window, NULL, FALSE);
781   
782   widget->allocation = *allocation;
783
784   viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
785   viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
786
787   if (GTK_WIDGET_REALIZED (widget))
788     {
789       GtkAllocation view_allocation;
790       
791       viewport_get_view_allocation (viewport, &view_allocation);
792   
793       gdk_window_move_resize (widget->window,
794                               allocation->x + border_width,
795                               allocation->y + border_width,
796                               allocation->width - border_width * 2,
797                               allocation->height - border_width * 2);
798
799       gdk_window_move_resize (viewport->view_window,
800                               view_allocation.x,
801                               view_allocation.y,
802                               view_allocation.width,
803                               view_allocation.height);
804     }
805
806   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
807     {
808       GtkAllocation child_allocation;
809       
810       child_allocation.x = 0;
811       child_allocation.y = 0;
812       child_allocation.width = hadjustment->upper;
813       child_allocation.height = vadjustment->upper;
814
815       if (GTK_WIDGET_REALIZED (widget))
816         gdk_window_move_resize (viewport->bin_window,
817                                 - hadjustment->value,
818                                 - vadjustment->value,
819                                 child_allocation.width,
820                                 child_allocation.height);
821
822       gtk_widget_size_allocate (bin->child, &child_allocation);
823     }
824
825   gtk_adjustment_changed (hadjustment);
826   gtk_adjustment_changed (vadjustment);
827   if (hadjustment_value_changed)
828     gtk_adjustment_value_changed (hadjustment);
829   if (vadjustment_value_changed)
830     gtk_adjustment_value_changed (vadjustment);
831 }
832
833 static void
834 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
835                                        gpointer       data)
836 {
837   GtkViewport *viewport;
838   GtkBin *bin;
839
840   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
841   g_return_if_fail (GTK_IS_VIEWPORT (data));
842
843   viewport = GTK_VIEWPORT (data);
844   bin = GTK_BIN (data);
845
846   if (bin->child && GTK_WIDGET_VISIBLE (bin->child)  &&
847       GTK_WIDGET_REALIZED (viewport))
848     {
849       GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
850       GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
851       gint old_x, old_y;
852       gint new_x, new_y;
853
854       gdk_window_get_position (viewport->bin_window, &old_x, &old_y);
855       new_x = - hadjustment->value;
856       new_y = - vadjustment->value;
857
858       if (new_x != old_x || new_y != old_y)
859         {
860           gdk_window_move (viewport->bin_window, new_x, new_y);
861           gdk_window_process_updates (viewport->bin_window, TRUE);
862         }
863     }
864 }
865
866 static void
867 gtk_viewport_style_set (GtkWidget *widget,
868                         GtkStyle  *previous_style)
869 {
870    GtkViewport *viewport;
871    
872    if (GTK_WIDGET_REALIZED (widget) &&
873        !GTK_WIDGET_NO_WINDOW (widget))
874      {
875         viewport = GTK_VIEWPORT (widget);
876         
877         gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
878         gtk_style_set_background (widget->style, widget->window, widget->state);
879      }
880 }
881
882 #define __GTK_VIEWPORT_C__
883 #include "gtkaliasdef.c"