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