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