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