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