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