]> Pileus Git - ~andy/gtk/blob - gtk/gtkviewport.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkviewport.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include "config.h"
26
27 #include "gtkviewport.h"
28
29 #include "gtkadjustment.h"
30 #include "gtkintl.h"
31 #include "gtkmarshalers.h"
32 #include "gtkprivate.h"
33 #include "gtkscrollable.h"
34 #include "gtktypebuiltins.h"
35
36
37 /**
38  * SECTION:gtkviewport
39  * @Short_description: An adapter which makes widgets scrollable
40  * @Title: GtkViewport
41  * @See_also:#GtkScrolledWindow, #GtkAdjustment
42  *
43  * The #GtkViewport widget acts as an adaptor class, implementing
44  * scrollability for child widgets that lack their own scrolling
45  * capabilities. Use #GtkViewport to scroll child widgets such as
46  * #GtkGrid, #GtkBox, and so on.
47  *
48  * If a widget has native scrolling abilities, such as #GtkTextView,
49  * #GtkTreeView or #GtkIconView, it can be added to a #GtkScrolledWindow
50  * with gtk_container_add(). If a widget does not, you must first add the
51  * widget to a #GtkViewport, then add the viewport to the scrolled window.
52  * gtk_container_add() does this automatically if a child that does not
53  * implement #GtkScrollable is added to a #GtkScrolledWindow, so you can
54  * 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
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
252   viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, NULL);
253   viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, NULL);
254 }
255
256 /**
257  * gtk_viewport_new:
258  * @hadjustment: (allow-none): horizontal adjustment
259  * @vadjustment: (allow-none): vertical adjustment
260  *
261  * Creates a new #GtkViewport with the given adjustments, or with default
262  * adjustments if none are given.
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
686   attributes.event_mask = event_mask | GDK_SCROLL_MASK | GDK_TOUCH_MASK | GDK_SMOOTH_SCROLL_MASK;
687
688   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
689
690   window = gdk_window_new (gtk_widget_get_parent_window (widget),
691                            &attributes, attributes_mask);
692   gtk_widget_set_window (widget, window);
693   gtk_widget_register_window (widget, window);
694
695   viewport_get_view_allocation (viewport, &view_allocation);
696   
697   attributes.x = view_allocation.x;
698   attributes.y = view_allocation.y;
699   attributes.width = view_allocation.width;
700   attributes.height = view_allocation.height;
701   attributes.event_mask = 0;
702
703   priv->view_window = gdk_window_new (window,
704                                       &attributes, attributes_mask);
705   gtk_widget_register_window (widget, priv->view_window);
706
707   attributes.x = - gtk_adjustment_get_value (hadjustment);
708   attributes.y = - gtk_adjustment_get_value (vadjustment);
709   attributes.width = gtk_adjustment_get_upper (hadjustment);
710   attributes.height = gtk_adjustment_get_upper (vadjustment);
711   
712   attributes.event_mask = event_mask;
713
714   priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
715   gtk_widget_register_window (widget, priv->bin_window);
716
717   child = gtk_bin_get_child (bin);
718   if (child)
719     gtk_widget_set_parent_window (child, priv->bin_window);
720
721   context = gtk_widget_get_style_context (widget);
722   gtk_style_context_set_background (context, window);
723   gtk_style_context_set_background (context, priv->bin_window);
724
725   gdk_window_show (priv->bin_window);
726   gdk_window_show (priv->view_window);
727 }
728
729 static void
730 gtk_viewport_unrealize (GtkWidget *widget)
731 {
732   GtkViewport *viewport = GTK_VIEWPORT (widget);
733   GtkViewportPrivate *priv = viewport->priv;
734
735   gtk_widget_unregister_window (widget, priv->view_window);
736   gdk_window_destroy (priv->view_window);
737   priv->view_window = NULL;
738
739   gtk_widget_unregister_window (widget, priv->bin_window);
740   gdk_window_destroy (priv->bin_window);
741   priv->bin_window = NULL;
742
743   GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
744 }
745
746 static gint
747 gtk_viewport_draw (GtkWidget *widget,
748                    cairo_t   *cr)
749 {
750   GtkViewport *viewport = GTK_VIEWPORT (widget);
751   GtkViewportPrivate *priv = viewport->priv;
752   GtkStyleContext *context;
753   int x, y;
754
755   context = gtk_widget_get_style_context (widget);
756
757   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)) &&
758       priv->shadow_type != GTK_SHADOW_NONE)
759     {
760       gtk_style_context_save (context);
761       gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
762
763       gtk_render_frame (context, cr, 0, 0,
764                         gdk_window_get_width (gtk_widget_get_window (widget)),
765                         gdk_window_get_height (gtk_widget_get_window (widget)));
766
767       gtk_style_context_restore (context);
768     }
769   
770   if (gtk_cairo_should_draw_window (cr, priv->view_window))
771     {
772       /* This is a cute hack to ensure the contents of bin_window are
773        * restricted to where they are visible. We only need to do this
774        * clipping when called via gtk_widget_draw() and not in expose
775        * events. And when that happens every window (including this one)
776        * should be drawn.
777        */
778       gdk_window_get_position (priv->view_window, &x, &y);
779       cairo_rectangle (cr, x, y, 
780                        gdk_window_get_width (priv->view_window),
781                        gdk_window_get_height (priv->view_window));
782       cairo_clip (cr);
783     }
784
785   if (gtk_cairo_should_draw_window (cr, priv->bin_window))
786     {
787       gdk_window_get_position (priv->bin_window, &x, &y);
788       gtk_render_background (context, cr, x, y,
789                              gdk_window_get_width (priv->bin_window),
790                              gdk_window_get_height (priv->bin_window));
791
792       GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
793     }
794
795   return FALSE;
796 }
797
798 static void
799 gtk_viewport_add (GtkContainer *container,
800                   GtkWidget    *child)
801 {
802   GtkBin *bin = GTK_BIN (container);
803   GtkViewport *viewport = GTK_VIEWPORT (bin);
804   GtkViewportPrivate *priv = viewport->priv;
805
806   g_return_if_fail (gtk_bin_get_child (bin) == NULL);
807
808   gtk_widget_set_parent_window (child, priv->bin_window);
809
810   GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
811 }
812
813 static void
814 gtk_viewport_size_allocate (GtkWidget     *widget,
815                             GtkAllocation *allocation)
816 {
817   GtkAllocation widget_allocation;
818   GtkViewport *viewport = GTK_VIEWPORT (widget);
819   GtkViewportPrivate *priv = viewport->priv;
820   GtkBin *bin = GTK_BIN (widget);
821   guint border_width;
822   GtkAdjustment *hadjustment = priv->hadjustment;
823   GtkAdjustment *vadjustment = priv->vadjustment;
824   GtkAllocation child_allocation;
825   GtkWidget *child;
826
827   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
828
829   /* If our size changed, and we have a shadow, queue a redraw on widget->window to
830    * redraw the shadow correctly.
831    */
832   gtk_widget_get_allocation (widget, &widget_allocation);
833   if (gtk_widget_get_mapped (widget) &&
834       priv->shadow_type != GTK_SHADOW_NONE &&
835       (widget_allocation.width != allocation->width ||
836        widget_allocation.height != allocation->height))
837     gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
838
839   gtk_widget_set_allocation (widget, allocation);
840
841   g_object_freeze_notify (G_OBJECT (hadjustment));
842   g_object_freeze_notify (G_OBJECT (vadjustment));
843
844   viewport_set_hadjustment_values (viewport);
845   viewport_set_vadjustment_values (viewport);
846   
847   child_allocation.x = 0;
848   child_allocation.y = 0;
849   child_allocation.width = gtk_adjustment_get_upper (hadjustment);
850   child_allocation.height = gtk_adjustment_get_upper (vadjustment);
851   if (gtk_widget_get_realized (widget))
852     {
853       GtkAllocation view_allocation;
854
855       gdk_window_move_resize (gtk_widget_get_window (widget),
856                               allocation->x + border_width,
857                               allocation->y + border_width,
858                               allocation->width - border_width * 2,
859                               allocation->height - border_width * 2);
860       
861       viewport_get_view_allocation (viewport, &view_allocation);
862       gdk_window_move_resize (priv->view_window,
863                               view_allocation.x,
864                               view_allocation.y,
865                               view_allocation.width,
866                               view_allocation.height);
867       gdk_window_move_resize (priv->bin_window,
868                               - gtk_adjustment_get_value (hadjustment),
869                               - gtk_adjustment_get_value (vadjustment),
870                               child_allocation.width,
871                               child_allocation.height);
872     }
873
874   child = gtk_bin_get_child (bin);
875   if (child && gtk_widget_get_visible (child))
876     gtk_widget_size_allocate (child, &child_allocation);
877
878   g_object_thaw_notify (G_OBJECT (hadjustment));
879   g_object_thaw_notify (G_OBJECT (vadjustment));
880 }
881
882 static void
883 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
884                                        gpointer       data)
885 {
886   GtkViewport *viewport = GTK_VIEWPORT (data);
887   GtkViewportPrivate *priv = viewport->priv;
888   GtkBin *bin = GTK_BIN (data);
889   GtkWidget *child;
890
891   child = gtk_bin_get_child (bin);
892   if (child && gtk_widget_get_visible (child) &&
893       gtk_widget_get_realized (GTK_WIDGET (viewport)))
894     {
895       GtkAdjustment *hadjustment = priv->hadjustment;
896       GtkAdjustment *vadjustment = priv->vadjustment;
897       gint old_x, old_y;
898       gint new_x, new_y;
899
900       gdk_window_get_position (priv->bin_window, &old_x, &old_y);
901       new_x = - gtk_adjustment_get_value (hadjustment);
902       new_y = - gtk_adjustment_get_value (vadjustment);
903
904       if (new_x != old_x || new_y != old_y)
905         gdk_window_move (priv->bin_window, new_x, new_y);
906     }
907 }
908
909 static void
910 gtk_viewport_style_updated (GtkWidget *widget)
911 {
912    GTK_WIDGET_CLASS (gtk_viewport_parent_class)->style_updated (widget);
913
914    if (gtk_widget_get_realized (widget) &&
915        gtk_widget_get_has_window (widget))
916      {
917         GtkStyleContext *context;
918         GtkViewport *viewport = GTK_VIEWPORT (widget);
919         GtkViewportPrivate *priv = viewport->priv;
920
921         context = gtk_widget_get_style_context (widget);
922         gtk_style_context_set_background (context, priv->bin_window);
923         gtk_style_context_set_background (context, gtk_widget_get_window (widget));
924      }
925 }
926
927
928 static void
929 gtk_viewport_get_preferred_size (GtkWidget      *widget,
930                                  GtkOrientation  orientation,
931                                  gint           *minimum_size,
932                                  gint           *natural_size)
933 {
934   GtkViewport *viewport = GTK_VIEWPORT (widget);
935   GtkViewportPrivate *priv = viewport->priv;
936   GtkStyleContext *context;
937   GtkStateFlags state;
938   GtkBorder padding, border;
939   GtkWidget *child;
940   gint       child_min, child_nat;
941   gint       minimum, natural;
942
943   child = gtk_bin_get_child (GTK_BIN (widget));
944
945   /* XXX This should probably be (border_width * 2); but GTK+ has
946    * been doing this with a single border for a while now...
947    */
948   minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
949
950   context = gtk_widget_get_style_context (GTK_WIDGET (widget));
951   state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
952   gtk_style_context_get_padding (context, state, &padding);
953
954   if (priv->shadow_type != GTK_SHADOW_NONE)
955     {
956       gtk_style_context_get_border (context, state, &border);
957
958       if (orientation == GTK_ORIENTATION_HORIZONTAL)
959         minimum += border.left + border.right;
960       else
961         minimum += border.top + border.bottom;
962     }
963
964   if (orientation == GTK_ORIENTATION_HORIZONTAL)
965     minimum += padding.left + padding.right;
966   else
967     minimum += padding.top + padding.bottom;
968
969   natural = minimum;
970
971   if (child && gtk_widget_get_visible (child))
972     {
973       if (orientation == GTK_ORIENTATION_HORIZONTAL)
974         gtk_widget_get_preferred_width (child, &child_min, &child_nat);
975       else
976         gtk_widget_get_preferred_height (child, &child_min, &child_nat);
977
978       minimum += child_min;
979       natural += child_nat;
980     }
981
982   if (minimum_size)
983     *minimum_size = minimum;
984
985   if (natural_size)
986     *natural_size = natural;
987 }
988
989 static void
990 gtk_viewport_get_preferred_width (GtkWidget *widget,
991                                   gint      *minimum_size,
992                                   gint      *natural_size)
993 {
994   gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
995 }
996
997 static void
998 gtk_viewport_get_preferred_height (GtkWidget *widget,
999                                    gint      *minimum_size,
1000                                    gint      *natural_size)
1001 {
1002   gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1003 }