]> Pileus Git - ~andy/gtk/blob - gtk/gtkviewport.c
viewport: adjustments are never NULL
[~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       gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
179       break;
180     case PROP_VADJUSTMENT:
181       gtk_viewport_set_vadjustment (viewport, 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_get_padding (context, state, &padding);
332   gtk_style_context_get_border (context, state, &border);
333
334   if (priv->shadow_type != GTK_SHADOW_NONE)
335     {
336       view_allocation->x = border.left;
337       view_allocation->y = border.top;
338     }
339
340   view_allocation->x += padding.left;
341   view_allocation->y += padding.right;
342   view_allocation->width = MAX (1, allocation.width - padding.left - padding.right - border_width * 2);
343   view_allocation->height = MAX (1, allocation.height - padding.top - padding.bottom - border_width * 2);
344
345   if (priv->shadow_type != GTK_SHADOW_NONE)
346     {
347       view_allocation->width = MAX (1, view_allocation->width - border.left - border.right);
348       view_allocation->height = MAX (1, view_allocation->height - border.top - border.bottom);
349     }
350 }
351
352 /**
353  * gtk_viewport_get_hadjustment:
354  * @viewport: a #GtkViewport.
355  *
356  * Returns the horizontal adjustment of the viewport.
357  *
358  * Return value: (transfer none): the horizontal adjustment of @viewport.
359  *
360  * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
361  **/
362 GtkAdjustment*
363 gtk_viewport_get_hadjustment (GtkViewport *viewport)
364 {
365   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
366
367   return viewport->priv->hadjustment;
368 }
369
370 /**
371  * gtk_viewport_get_vadjustment:
372  * @viewport: a #GtkViewport.
373  * 
374  * Returns the vertical adjustment of the viewport.
375  *
376  * Return value: (transfer none): the vertical adjustment of @viewport.
377  *
378  * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
379  **/
380 GtkAdjustment*
381 gtk_viewport_get_vadjustment (GtkViewport *viewport)
382 {
383   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
384
385   return viewport->priv->vadjustment;
386 }
387
388 static void
389 viewport_set_hadjustment_values (GtkViewport *viewport)
390 {
391   GtkBin *bin = GTK_BIN (viewport);
392   GtkAllocation view_allocation;
393   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
394   GtkWidget *child;
395   gdouble upper, value;
396   
397   viewport_get_view_allocation (viewport, &view_allocation);  
398
399   child = gtk_bin_get_child (bin);
400   if (child && gtk_widget_get_visible (child))
401     {
402       gint minimum_width, natural_width;
403       gint scroll_height;
404       
405       if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
406         gtk_widget_get_preferred_height (child, &scroll_height, NULL);
407       else
408         gtk_widget_get_preferred_height (child, NULL, &scroll_height);
409
410       gtk_widget_get_preferred_width_for_height (child,
411                                                  MAX (view_allocation.height, scroll_height),
412                                                  &minimum_width,
413                                                  &natural_width);
414
415       if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
416         upper = MAX (minimum_width, view_allocation.width);
417       else
418         upper = MAX (natural_width, view_allocation.width);
419     }
420   else
421     upper = view_allocation.width;
422
423   value = gtk_adjustment_get_value (hadjustment);
424   /* We clamp to the left in RTL mode */
425   if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
426     {
427       gdouble dist = gtk_adjustment_get_upper (hadjustment)
428                      - value
429                      - gtk_adjustment_get_page_size (hadjustment);
430       value = upper - dist - view_allocation.width;
431     }
432
433   gtk_adjustment_configure (hadjustment,
434                             value,
435                             0,
436                             upper,
437                             view_allocation.width * 0.1,
438                             view_allocation.width * 0.9,
439                             view_allocation.width);
440 }
441
442 static void
443 viewport_set_vadjustment_values (GtkViewport *viewport)
444 {
445   GtkBin *bin = GTK_BIN (viewport);
446   GtkAllocation view_allocation;
447   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
448   GtkWidget *child;
449   gdouble upper;
450
451   viewport_get_view_allocation (viewport, &view_allocation);  
452
453   child = gtk_bin_get_child (bin);
454   if (child && gtk_widget_get_visible (child))
455     {
456       gint minimum_height, natural_height;
457       gint scroll_width;
458
459       if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
460         gtk_widget_get_preferred_width (child, &scroll_width, NULL);
461       else
462         gtk_widget_get_preferred_width (child, NULL, &scroll_width);
463
464       gtk_widget_get_preferred_height_for_width (child,
465                                                  MAX (view_allocation.width, scroll_width),
466                                                  &minimum_height,
467                                                  &natural_height);
468
469       if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
470         upper = MAX (minimum_height, view_allocation.height);
471       else
472         upper = MAX (natural_height, view_allocation.height);
473     }
474   else
475     upper = view_allocation.height;
476
477   gtk_adjustment_configure (vadjustment,
478                             gtk_adjustment_get_value (vadjustment),
479                             0,
480                             upper,
481                             view_allocation.height * 0.1,
482                             view_allocation.height * 0.9,
483                             view_allocation.height);
484 }
485
486 static void
487 viewport_set_adjustment (GtkViewport    *viewport,
488                          GtkOrientation  orientation,
489                          GtkAdjustment  *adjustment)
490 {
491   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
492
493   if (adjustment && adjustment == *adjustmentp)
494     return;
495
496   if (!adjustment)
497     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
498   viewport_disconnect_adjustment (viewport, orientation);
499   *adjustmentp = adjustment;
500   g_object_ref_sink (adjustment);
501
502   if (orientation == GTK_ORIENTATION_HORIZONTAL)
503     viewport_set_hadjustment_values (viewport);
504   else
505     viewport_set_vadjustment_values (viewport);
506
507   g_signal_connect (adjustment, "value-changed",
508                     G_CALLBACK (gtk_viewport_adjustment_value_changed),
509                     viewport);
510
511   gtk_viewport_adjustment_value_changed (adjustment, viewport);
512 }
513
514 /**
515  * gtk_viewport_set_hadjustment:
516  * @viewport: a #GtkViewport.
517  * @adjustment: (allow-none): a #GtkAdjustment.
518  *
519  * Sets the horizontal adjustment of the viewport.
520  *
521  * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
522  **/
523 void
524 gtk_viewport_set_hadjustment (GtkViewport   *viewport,
525                               GtkAdjustment *adjustment)
526 {
527   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
528   if (adjustment)
529     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
530
531   viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
532
533   g_object_notify (G_OBJECT (viewport), "hadjustment");
534 }
535
536 /**
537  * gtk_viewport_set_vadjustment:
538  * @viewport: a #GtkViewport.
539  * @adjustment: (allow-none): a #GtkAdjustment.
540  *
541  * Sets the vertical adjustment of the viewport.
542  *
543  * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
544  **/
545 void
546 gtk_viewport_set_vadjustment (GtkViewport   *viewport,
547                               GtkAdjustment *adjustment)
548 {
549   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
550   if (adjustment)
551     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
552
553   viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
554
555   g_object_notify (G_OBJECT (viewport), "vadjustment");
556 }
557
558 /** 
559  * gtk_viewport_set_shadow_type:
560  * @viewport: a #GtkViewport.
561  * @type: the new shadow type.
562  *
563  * Sets the shadow type of the viewport.
564  **/ 
565 void
566 gtk_viewport_set_shadow_type (GtkViewport   *viewport,
567                               GtkShadowType  type)
568 {
569   GtkViewportPrivate *priv;
570   GtkAllocation allocation;
571   GtkWidget *widget;
572
573   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
574
575   widget = GTK_WIDGET (viewport);
576   priv = viewport->priv;
577
578   if ((GtkShadowType) priv->shadow_type != type)
579     {
580       priv->shadow_type = type;
581
582       if (gtk_widget_get_visible (widget))
583         {
584           gtk_widget_get_allocation (widget, &allocation);
585           gtk_widget_size_allocate (widget, &allocation);
586           gtk_widget_set_allocation (widget, &allocation);
587           gtk_widget_queue_draw (widget);
588         }
589
590       g_object_notify (G_OBJECT (viewport), "shadow-type");
591     }
592 }
593
594 /**
595  * gtk_viewport_get_shadow_type:
596  * @viewport: a #GtkViewport
597  *
598  * Gets the shadow type of the #GtkViewport. See
599  * gtk_viewport_set_shadow_type().
600  *
601  * Return value: the shadow type 
602  **/
603 GtkShadowType
604 gtk_viewport_get_shadow_type (GtkViewport *viewport)
605 {
606   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
607
608   return viewport->priv->shadow_type;
609 }
610
611 /**
612  * gtk_viewport_get_bin_window:
613  * @viewport: a #GtkViewport
614  *
615  * Gets the bin window of the #GtkViewport.
616  *
617  * Return value: (transfer none): a #GdkWindow
618  *
619  * Since: 2.20
620  **/
621 GdkWindow*
622 gtk_viewport_get_bin_window (GtkViewport *viewport)
623 {
624   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
625
626   return viewport->priv->bin_window;
627 }
628
629 /**
630  * gtk_viewport_get_view_window:
631  * @viewport: a #GtkViewport
632  *
633  * Gets the view window of the #GtkViewport.
634  *
635  * Return value: (transfer none): a #GdkWindow
636  *
637  * Since: 2.22
638  **/
639 GdkWindow*
640 gtk_viewport_get_view_window (GtkViewport *viewport)
641 {
642   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
643
644   return viewport->priv->view_window;
645 }
646
647 static void
648 gtk_viewport_realize (GtkWidget *widget)
649 {
650   GtkViewport *viewport = GTK_VIEWPORT (widget);
651   GtkViewportPrivate *priv = viewport->priv;
652   GtkBin *bin = GTK_BIN (widget);
653   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
654   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
655   GtkAllocation allocation;
656   GtkAllocation view_allocation;
657   GtkStyleContext *context;
658   GtkWidget *child;
659   GdkWindow *window;
660   GdkWindowAttr attributes;
661   gint attributes_mask;
662   gint event_mask;
663   guint border_width;
664
665   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
666
667   gtk_widget_set_realized (widget, TRUE);
668
669   gtk_widget_get_allocation (widget, &allocation);
670
671   attributes.x = allocation.x + border_width;
672   attributes.y = allocation.y + border_width;
673   attributes.width = allocation.width - border_width * 2;
674   attributes.height = allocation.height - border_width * 2;
675   attributes.window_type = GDK_WINDOW_CHILD;
676   attributes.wclass = GDK_INPUT_OUTPUT;
677   attributes.visual = gtk_widget_get_visual (widget);
678
679   event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
680   /* We select on button_press_mask so that button 4-5 scrolls are trapped.
681    */
682   attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
683
684   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
685
686   window = gdk_window_new (gtk_widget_get_parent_window (widget),
687                            &attributes, attributes_mask);
688   gtk_widget_set_window (widget, window);
689   gdk_window_set_user_data (window, viewport);
690
691   viewport_get_view_allocation (viewport, &view_allocation);
692   
693   attributes.x = view_allocation.x;
694   attributes.y = view_allocation.y;
695   attributes.width = view_allocation.width;
696   attributes.height = view_allocation.height;
697   attributes.event_mask = 0;
698
699   priv->view_window = gdk_window_new (window,
700                                       &attributes, attributes_mask);
701   gdk_window_set_user_data (priv->view_window, viewport);
702
703   attributes.x = - gtk_adjustment_get_value (hadjustment);
704   attributes.y = - gtk_adjustment_get_value (vadjustment);
705   attributes.width = gtk_adjustment_get_upper (hadjustment);
706   attributes.height = gtk_adjustment_get_upper (vadjustment);
707   
708   attributes.event_mask = event_mask;
709
710   priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
711   gdk_window_set_user_data (priv->bin_window, viewport);
712
713   child = gtk_bin_get_child (bin);
714   if (child)
715     gtk_widget_set_parent_window (child, priv->bin_window);
716
717   context = gtk_widget_get_style_context (widget);
718   gtk_style_context_set_background (context, window);
719   gtk_style_context_set_background (context, priv->bin_window);
720
721   gdk_window_show (priv->bin_window);
722   gdk_window_show (priv->view_window);
723 }
724
725 static void
726 gtk_viewport_unrealize (GtkWidget *widget)
727 {
728   GtkViewport *viewport = GTK_VIEWPORT (widget);
729   GtkViewportPrivate *priv = viewport->priv;
730
731   gdk_window_set_user_data (priv->view_window, NULL);
732   gdk_window_destroy (priv->view_window);
733   priv->view_window = NULL;
734
735   gdk_window_set_user_data (priv->bin_window, NULL);
736   gdk_window_destroy (priv->bin_window);
737   priv->bin_window = NULL;
738
739   GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
740 }
741
742 static gint
743 gtk_viewport_draw (GtkWidget *widget,
744                    cairo_t   *cr)
745 {
746   GtkViewport *viewport = GTK_VIEWPORT (widget);
747   GtkViewportPrivate *priv = viewport->priv;
748   GtkStyleContext *context;
749   int x, y;
750
751   context = gtk_widget_get_style_context (widget);
752
753   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
754     {
755       gtk_style_context_save (context);
756       gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
757
758       gtk_render_frame (context, cr, 0, 0,
759                         gdk_window_get_width (gtk_widget_get_window (widget)),
760                         gdk_window_get_height (gtk_widget_get_window (widget)));
761
762       gtk_style_context_restore (context);
763     }
764   
765   if (gtk_cairo_should_draw_window (cr, priv->view_window))
766     {
767       /* This is a cute hack to ensure the contents of bin_window are
768        * restricted to where they are visible. We only need to do this
769        * clipping when called via gtk_widget_draw() and not in expose
770        * events. And when that happens every window (including this one)
771        * should be drawn.
772        */
773       gdk_window_get_position (priv->view_window, &x, &y);
774       cairo_rectangle (cr, x, y, 
775                        gdk_window_get_width (priv->view_window),
776                        gdk_window_get_height (priv->view_window));
777       cairo_clip (cr);
778     }
779
780   if (gtk_cairo_should_draw_window (cr, priv->bin_window))
781     {
782       gdk_window_get_position (priv->bin_window, &x, &y);
783       gtk_render_background (context, cr, x, y,
784                              gdk_window_get_width (priv->bin_window),
785                              gdk_window_get_height (priv->bin_window));
786
787       GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
788     }
789
790   return FALSE;
791 }
792
793 static void
794 gtk_viewport_add (GtkContainer *container,
795                   GtkWidget    *child)
796 {
797   GtkBin *bin = GTK_BIN (container);
798   GtkViewport *viewport = GTK_VIEWPORT (bin);
799   GtkViewportPrivate *priv = viewport->priv;
800
801   g_return_if_fail (gtk_bin_get_child (bin) == NULL);
802
803   gtk_widget_set_parent_window (child, priv->bin_window);
804
805   GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
806 }
807
808 static void
809 gtk_viewport_size_allocate (GtkWidget     *widget,
810                             GtkAllocation *allocation)
811 {
812   GtkAllocation widget_allocation;
813   GtkViewport *viewport = GTK_VIEWPORT (widget);
814   GtkViewportPrivate *priv = viewport->priv;
815   GtkBin *bin = GTK_BIN (widget);
816   guint border_width;
817   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
818   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
819   GtkAllocation child_allocation;
820   GtkWidget *child;
821
822   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
823
824   /* If our size changed, and we have a shadow, queue a redraw on widget->window to
825    * redraw the shadow correctly.
826    */
827   gtk_widget_get_allocation (widget, &widget_allocation);
828   if (gtk_widget_get_mapped (widget) &&
829       priv->shadow_type != GTK_SHADOW_NONE &&
830       (widget_allocation.width != allocation->width ||
831        widget_allocation.height != allocation->height))
832     gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
833
834   gtk_widget_set_allocation (widget, allocation);
835
836   g_object_freeze_notify (G_OBJECT (hadjustment));
837   g_object_freeze_notify (G_OBJECT (vadjustment));
838
839   viewport_set_hadjustment_values (viewport);
840   viewport_set_vadjustment_values (viewport);
841   
842   child_allocation.x = 0;
843   child_allocation.y = 0;
844   child_allocation.width = gtk_adjustment_get_upper (hadjustment);
845   child_allocation.height = gtk_adjustment_get_upper (vadjustment);
846   if (gtk_widget_get_realized (widget))
847     {
848       GtkAllocation view_allocation;
849
850       gdk_window_move_resize (gtk_widget_get_window (widget),
851                               allocation->x + border_width,
852                               allocation->y + border_width,
853                               allocation->width - border_width * 2,
854                               allocation->height - border_width * 2);
855       
856       viewport_get_view_allocation (viewport, &view_allocation);
857       gdk_window_move_resize (priv->view_window,
858                               view_allocation.x,
859                               view_allocation.y,
860                               view_allocation.width,
861                               view_allocation.height);
862       gdk_window_move_resize (priv->bin_window,
863                               - gtk_adjustment_get_value (hadjustment),
864                               - gtk_adjustment_get_value (vadjustment),
865                               child_allocation.width,
866                               child_allocation.height);
867     }
868
869   child = gtk_bin_get_child (bin);
870   if (child && gtk_widget_get_visible (child))
871     gtk_widget_size_allocate (child, &child_allocation);
872
873   g_object_thaw_notify (G_OBJECT (hadjustment));
874   g_object_thaw_notify (G_OBJECT (vadjustment));
875 }
876
877 static void
878 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
879                                        gpointer       data)
880 {
881   GtkViewport *viewport = GTK_VIEWPORT (data);
882   GtkViewportPrivate *priv = viewport->priv;
883   GtkBin *bin = GTK_BIN (data);
884   GtkWidget *child;
885
886   child = gtk_bin_get_child (bin);
887   if (child && gtk_widget_get_visible (child) &&
888       gtk_widget_get_realized (GTK_WIDGET (viewport)))
889     {
890       GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
891       GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
892       gint old_x, old_y;
893       gint new_x, new_y;
894
895       gdk_window_get_position (priv->bin_window, &old_x, &old_y);
896       new_x = - gtk_adjustment_get_value (hadjustment);
897       new_y = - gtk_adjustment_get_value (vadjustment);
898
899       if (new_x != old_x || new_y != old_y)
900         {
901           gdk_window_move (priv->bin_window, new_x, new_y);
902           gdk_window_process_updates (priv->bin_window, TRUE);
903         }
904     }
905 }
906
907 static void
908 gtk_viewport_style_updated (GtkWidget *widget)
909 {
910    GTK_WIDGET_CLASS (gtk_viewport_parent_class)->style_updated (widget);
911
912    if (gtk_widget_get_realized (widget) &&
913        gtk_widget_get_has_window (widget))
914      {
915         GtkStyleContext *context;
916         GtkViewport *viewport = GTK_VIEWPORT (widget);
917         GtkViewportPrivate *priv = viewport->priv;
918
919         context = gtk_widget_get_style_context (widget);
920         gtk_style_context_set_background (context, priv->bin_window);
921         gtk_style_context_set_background (context, gtk_widget_get_window (widget));
922      }
923 }
924
925
926 static void
927 gtk_viewport_get_preferred_size (GtkWidget      *widget,
928                                  GtkOrientation  orientation,
929                                  gint           *minimum_size,
930                                  gint           *natural_size)
931 {
932   GtkViewport *viewport = GTK_VIEWPORT (widget);
933   GtkViewportPrivate *priv = viewport->priv;
934   GtkStyleContext *context;
935   GtkStateFlags state;
936   GtkBorder padding, border;
937   GtkWidget *child;
938   gint       child_min, child_nat;
939   gint       minimum, natural;
940
941   child = gtk_bin_get_child (GTK_BIN (widget));
942
943   /* XXX This should probably be (border_width * 2); but GTK+ has
944    * been doing this with a single border for a while now...
945    */
946   minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
947
948   context = gtk_widget_get_style_context (GTK_WIDGET (widget));
949   state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
950   gtk_style_context_get_padding (context, state, &padding);
951
952   if (priv->shadow_type != GTK_SHADOW_NONE)
953     {
954       gtk_style_context_get_border (context, state, &border);
955
956       if (orientation == GTK_ORIENTATION_HORIZONTAL)
957         minimum += border.left + border.right;
958       else
959         minimum += border.top + border.bottom;
960     }
961
962   if (orientation == GTK_ORIENTATION_HORIZONTAL)
963     minimum += padding.left + padding.right;
964   else
965     minimum += padding.top + padding.bottom;
966
967   natural = minimum;
968
969   if (child && gtk_widget_get_visible (child))
970     {
971       if (orientation == GTK_ORIENTATION_HORIZONTAL)
972         gtk_widget_get_preferred_width (child, &child_min, &child_nat);
973       else
974         gtk_widget_get_preferred_height (child, &child_min, &child_nat);
975
976       minimum += child_min;
977       natural += child_nat;
978     }
979
980   if (minimum_size)
981     *minimum_size = minimum;
982
983   if (natural_size)
984     *natural_size = natural;
985 }
986
987 static void
988 gtk_viewport_get_preferred_width (GtkWidget *widget,
989                                   gint      *minimum_size,
990                                   gint      *natural_size)
991 {
992   gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
993 }
994
995 static void
996 gtk_viewport_get_preferred_height (GtkWidget *widget,
997                                    gint      *minimum_size,
998                                    gint      *natural_size)
999 {
1000   gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1001 }