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