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