]> Pileus Git - ~andy/gtk/blob - gtk/gtkviewport.c
[docs] Move documentation to inline comments: GtkViewport
[~andy/gtk] / gtk / gtkviewport.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28 #include "gtkviewport.h"
29 #include "gtkintl.h"
30 #include "gtkmarshalers.h"
31 #include "gtkprivate.h"
32 #include "gtkalias.h"
33
34 /**
35  * SECTION:gtkviewport
36  * @Short_description: An adapter which makes widgets scrollable
37  * @Title: GtkViewport
38  * @See_also:#GtkScrolledWindow, #GtkAdjustment
39  *
40  * The #GtkViewport widget acts as an adaptor class, implementing
41  * scrollability for child widgets that lack their own scrolling
42  * capabilities. Use #GtkViewport to scroll child widgets such as
43  * #GtkTable, #GtkBox, and so on.
44  *
45  * If a widget has native scrolling abilities, such as #GtkTextView,
46  * #GtkTreeView or #GtkIconview, it can be added to a #GtkScrolledWindow
47  * with gtk_container_add(). If a widget does not, you must first add the
48  * widget to a #GtkViewport, then add the viewport to the scrolled window.
49  * The convenience function gtk_scrolled_window_add_with_viewport() does
50  * exactly this, so you can ignore the presence of the viewport.
51  */
52
53 enum {
54   PROP_0,
55   PROP_HADJUSTMENT,
56   PROP_VADJUSTMENT,
57   PROP_SHADOW_TYPE
58 };
59
60
61 static void gtk_viewport_finalize                 (GObject          *object);
62 static void gtk_viewport_destroy                  (GtkObject        *object);
63 static void gtk_viewport_set_property             (GObject         *object,
64                                                    guint            prop_id,
65                                                    const GValue    *value,
66                                                    GParamSpec      *pspec);
67 static void gtk_viewport_get_property             (GObject         *object,
68                                                    guint            prop_id,
69                                                    GValue          *value,
70                                                    GParamSpec      *pspec);
71 static void gtk_viewport_set_scroll_adjustments   (GtkViewport      *viewport,
72                                                    GtkAdjustment    *hadjustment,
73                                                    GtkAdjustment    *vadjustment);
74 static void gtk_viewport_realize                  (GtkWidget        *widget);
75 static void gtk_viewport_unrealize                (GtkWidget        *widget);
76 static void gtk_viewport_paint                    (GtkWidget        *widget,
77                                                    GdkRectangle     *area);
78 static gint gtk_viewport_expose                   (GtkWidget        *widget,
79                                                    GdkEventExpose   *event);
80 static void gtk_viewport_add                      (GtkContainer     *container,
81                                                    GtkWidget        *widget);
82 static void gtk_viewport_size_request             (GtkWidget        *widget,
83                                                    GtkRequisition   *requisition);
84 static void gtk_viewport_size_allocate            (GtkWidget        *widget,
85                                                    GtkAllocation    *allocation);
86 static void gtk_viewport_adjustment_value_changed (GtkAdjustment    *adjustment,
87                                                    gpointer          data);
88 static void gtk_viewport_style_set                (GtkWidget *widget,
89                                                    GtkStyle  *previous_style);
90
91 G_DEFINE_TYPE (GtkViewport, gtk_viewport, GTK_TYPE_BIN)
92
93 static void
94 gtk_viewport_class_init (GtkViewportClass *class)
95 {
96   GtkObjectClass *object_class;
97   GObjectClass   *gobject_class;
98   GtkWidgetClass *widget_class;
99   GtkContainerClass *container_class;
100
101   object_class = (GtkObjectClass*) class;
102   gobject_class = G_OBJECT_CLASS (class);
103   widget_class = (GtkWidgetClass*) class;
104   container_class = (GtkContainerClass*) class;
105
106   gobject_class->finalize = gtk_viewport_finalize;
107   gobject_class->set_property = gtk_viewport_set_property;
108   gobject_class->get_property = gtk_viewport_get_property;
109   object_class->destroy = gtk_viewport_destroy;
110   
111   widget_class->realize = gtk_viewport_realize;
112   widget_class->unrealize = gtk_viewport_unrealize;
113   widget_class->expose_event = gtk_viewport_expose;
114   widget_class->size_request = gtk_viewport_size_request;
115   widget_class->size_allocate = gtk_viewport_size_allocate;
116   widget_class->style_set = gtk_viewport_style_set;
117   
118   container_class->add = gtk_viewport_add;
119
120   class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;
121
122   g_object_class_install_property (gobject_class,
123                                    PROP_HADJUSTMENT,
124                                    g_param_spec_object ("hadjustment",
125                                                         P_("Horizontal adjustment"),
126                                                         P_("The GtkAdjustment that determines the values of the horizontal position for this viewport"),
127                                                         GTK_TYPE_ADJUSTMENT,
128                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
129
130   g_object_class_install_property (gobject_class,
131                                    PROP_VADJUSTMENT,
132                                    g_param_spec_object ("vadjustment",
133                                                         P_("Vertical adjustment"),
134                                                         P_("The GtkAdjustment that determines the values of the vertical position for this viewport"),
135                                                         GTK_TYPE_ADJUSTMENT,
136                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
137
138   g_object_class_install_property (gobject_class,
139                                    PROP_SHADOW_TYPE,
140                                    g_param_spec_enum ("shadow-type",
141                                                       P_("Shadow type"),
142                                                       P_("Determines how the shadowed box around the viewport is drawn"),
143                                                       GTK_TYPE_SHADOW_TYPE,
144                                                       GTK_SHADOW_IN,
145                                                       GTK_PARAM_READWRITE));
146
147   /**
148    * GtkViewport::set-scroll-adjustments
149    * @horizontal: the horizontal #GtkAdjustment
150    * @vertical: the vertical #GtkAdjustment
151    *
152    * Set the scroll adjustments for the viewport. Usually scrolled containers
153    * like #GtkScrolledWindow will emit this signal to connect two instances
154    * of #GtkScrollbar to the scroll directions of the #GtkViewport.
155    */
156   widget_class->set_scroll_adjustments_signal =
157     g_signal_new (I_("set-scroll-adjustments"),
158                   G_OBJECT_CLASS_TYPE (gobject_class),
159                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
160                   G_STRUCT_OFFSET (GtkViewportClass, set_scroll_adjustments),
161                   NULL, NULL,
162                   _gtk_marshal_VOID__OBJECT_OBJECT,
163                   G_TYPE_NONE, 2,
164                   GTK_TYPE_ADJUSTMENT,
165                   GTK_TYPE_ADJUSTMENT);
166 }
167
168 static void
169 gtk_viewport_set_property (GObject         *object,
170                            guint            prop_id,
171                            const GValue    *value,
172                            GParamSpec      *pspec)
173 {
174   GtkViewport *viewport;
175
176   viewport = GTK_VIEWPORT (object);
177
178   switch (prop_id)
179     {
180     case PROP_HADJUSTMENT:
181       gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
182       break;
183     case PROP_VADJUSTMENT:
184       gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
185       break;
186     case PROP_SHADOW_TYPE:
187       gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
188       break;
189     default:
190       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191       break;
192     }
193 }
194
195 static void
196 gtk_viewport_get_property (GObject         *object,
197                            guint            prop_id,
198                            GValue          *value,
199                            GParamSpec      *pspec)
200 {
201   GtkViewport *viewport;
202
203   viewport = GTK_VIEWPORT (object);
204
205   switch (prop_id)
206     {
207     case PROP_HADJUSTMENT:
208       g_value_set_object (value, viewport->hadjustment);
209       break;
210     case PROP_VADJUSTMENT:
211       g_value_set_object (value, viewport->vadjustment);
212       break;
213     case PROP_SHADOW_TYPE:
214       g_value_set_enum (value, viewport->shadow_type);
215       break;
216     default:
217       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218       break;
219     }
220 }
221
222 static void
223 gtk_viewport_init (GtkViewport *viewport)
224 {
225   gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
226
227   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
228   gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
229   
230   viewport->shadow_type = GTK_SHADOW_IN;
231   viewport->view_window = NULL;
232   viewport->bin_window = NULL;
233   viewport->hadjustment = NULL;
234   viewport->vadjustment = NULL;
235 }
236
237 /**
238  * gtk_viewport_new:
239  * @hadjustment: horizontal adjustment.
240  * @vadjustment: vertical adjustment.
241  * @returns: a new #GtkViewport.
242  *
243  * Creates a new #GtkViewport with the given adjustments.
244  *
245  **/
246 GtkWidget*
247 gtk_viewport_new (GtkAdjustment *hadjustment,
248                   GtkAdjustment *vadjustment)
249 {
250   GtkWidget *viewport;
251
252   viewport = g_object_new (GTK_TYPE_VIEWPORT,
253                              "hadjustment", hadjustment,
254                              "vadjustment", vadjustment,
255                              NULL);
256
257   return viewport;
258 }
259
260 #define ADJUSTMENT_POINTER(viewport, orientation)         \
261   (((orientation) == GTK_ORIENTATION_HORIZONTAL) ?        \
262      &(viewport)->hadjustment : &(viewport)->vadjustment)
263
264 static void
265 viewport_disconnect_adjustment (GtkViewport    *viewport,
266                                 GtkOrientation  orientation)
267 {
268   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
269
270   if (*adjustmentp)
271     {
272       g_signal_handlers_disconnect_by_func (*adjustmentp,
273                                             gtk_viewport_adjustment_value_changed,
274                                             viewport);
275       g_object_unref (*adjustmentp);
276       *adjustmentp = NULL;
277     }
278 }
279
280 static void
281 gtk_viewport_finalize (GObject *object)
282 {
283   GtkViewport *viewport = GTK_VIEWPORT (object);
284
285   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
286   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
287
288   G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
289 }
290
291 static void
292 gtk_viewport_destroy (GtkObject *object)
293 {
294   GtkViewport *viewport = GTK_VIEWPORT (object);
295
296   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
297   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
298
299   GTK_OBJECT_CLASS (gtk_viewport_parent_class)->destroy (object);
300 }
301
302 /**
303  * gtk_viewport_get_hadjustment:
304  * @viewport: a #GtkViewport.
305  * 
306  * Returns the horizontal adjustment of the viewport.
307  *
308  * Return value: the horizontal adjustment of @viewport.
309  **/
310 GtkAdjustment*
311 gtk_viewport_get_hadjustment (GtkViewport *viewport)
312 {
313   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
314
315   if (!viewport->hadjustment)
316     gtk_viewport_set_hadjustment (viewport, NULL);
317
318   return viewport->hadjustment;
319 }
320
321 /**
322  * gtk_viewport_get_vadjustment:
323  * @viewport: a #GtkViewport.
324  * 
325  * Returns the vertical adjustment of the viewport.
326  *
327  * Return value: the vertical adjustment of @viewport.
328  **/
329 GtkAdjustment*
330 gtk_viewport_get_vadjustment (GtkViewport *viewport)
331 {
332   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
333
334   if (!viewport->vadjustment)
335     gtk_viewport_set_vadjustment (viewport, NULL);
336
337   return viewport->vadjustment;
338 }
339
340 static void
341 viewport_get_view_allocation (GtkViewport   *viewport,
342                               GtkAllocation *view_allocation)
343 {
344   GtkWidget *widget = GTK_WIDGET (viewport);
345   GtkAllocation *allocation = &widget->allocation;
346   gint border_width = GTK_CONTAINER (viewport)->border_width;
347   
348   view_allocation->x = 0;
349   view_allocation->y = 0;
350
351   if (viewport->shadow_type != GTK_SHADOW_NONE)
352     {
353       view_allocation->x = widget->style->xthickness;
354       view_allocation->y = widget->style->ythickness;
355     }
356
357   view_allocation->width = MAX (1, allocation->width - view_allocation->x * 2 - border_width * 2);
358   view_allocation->height = MAX (1, allocation->height - view_allocation->y * 2 - border_width * 2);
359 }
360
361 static void
362 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
363                              gboolean      *value_changed)
364 {
365   gdouble value = adjustment->value;
366   
367   value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
368   if (value != adjustment->value)
369     {
370       adjustment->value = value;
371       if (value_changed)
372         *value_changed = TRUE;
373     }
374   else if (value_changed)
375     *value_changed = FALSE;
376 }
377
378 static void
379 viewport_set_hadjustment_values (GtkViewport *viewport,
380                                  gboolean    *value_changed)
381 {
382   GtkBin *bin = GTK_BIN (viewport);
383   GtkAllocation view_allocation;
384   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
385   gdouble old_page_size;
386   gdouble old_upper;
387   gdouble old_value;
388   
389   viewport_get_view_allocation (viewport, &view_allocation);  
390
391   old_page_size = hadjustment->page_size;
392   old_upper = hadjustment->upper;
393   old_value = hadjustment->value;
394   hadjustment->page_size = view_allocation.width;
395   hadjustment->step_increment = view_allocation.width * 0.1;
396   hadjustment->page_increment = view_allocation.width * 0.9;
397   
398   hadjustment->lower = 0;
399
400   if (bin->child && gtk_widget_get_visible (bin->child))
401     {
402       GtkRequisition child_requisition;
403       
404       gtk_widget_get_child_requisition (bin->child, &child_requisition);
405       hadjustment->upper = MAX (child_requisition.width, view_allocation.width);
406     }
407   else
408     hadjustment->upper = view_allocation.width;
409
410   if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL) 
411     {
412       gdouble dist = old_upper - (old_value + old_page_size);
413       hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
414       viewport_reclamp_adjustment (hadjustment, value_changed);
415       *value_changed = (old_value != hadjustment->value);
416     }
417   else
418     viewport_reclamp_adjustment (hadjustment, value_changed);
419 }
420
421 static void
422 viewport_set_vadjustment_values (GtkViewport *viewport,
423                                  gboolean    *value_changed)
424 {
425   GtkBin *bin = GTK_BIN (viewport);
426   GtkAllocation view_allocation;
427   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
428
429   viewport_get_view_allocation (viewport, &view_allocation);  
430
431   vadjustment->page_size = view_allocation.height;
432   vadjustment->step_increment = view_allocation.height * 0.1;
433   vadjustment->page_increment = view_allocation.height * 0.9;
434   
435   vadjustment->lower = 0;
436
437   if (bin->child && gtk_widget_get_visible (bin->child))
438     {
439       GtkRequisition child_requisition;
440       
441       gtk_widget_get_child_requisition (bin->child, &child_requisition);
442       vadjustment->upper = MAX (child_requisition.height, view_allocation.height);
443     }
444   else
445     vadjustment->upper = view_allocation.height;
446
447   viewport_reclamp_adjustment (vadjustment, value_changed);
448 }
449
450 static void
451 viewport_set_adjustment (GtkViewport    *viewport,
452                          GtkOrientation  orientation,
453                          GtkAdjustment  *adjustment)
454 {
455   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
456   gboolean value_changed;
457
458   if (adjustment && adjustment == *adjustmentp)
459     return;
460
461   if (!adjustment)
462     adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
463                                                      0.0, 0.0, 0.0));
464   viewport_disconnect_adjustment (viewport, orientation);
465   *adjustmentp = adjustment;
466   g_object_ref_sink (adjustment);
467
468   if (orientation == GTK_ORIENTATION_HORIZONTAL)
469     viewport_set_hadjustment_values (viewport, &value_changed);
470   else
471     viewport_set_vadjustment_values (viewport, &value_changed);
472
473   g_signal_connect (adjustment, "value-changed",
474                     G_CALLBACK (gtk_viewport_adjustment_value_changed),
475                     viewport);
476
477   gtk_adjustment_changed (adjustment);
478   
479   if (value_changed)
480     gtk_adjustment_value_changed (adjustment);
481   else
482     gtk_viewport_adjustment_value_changed (adjustment, viewport);
483 }
484
485 /**
486  * gtk_viewport_set_hadjustment:
487  * @viewport: a #GtkViewport.
488  * @adjustment: (allow-none): a #GtkAdjustment.
489  *
490  * Sets the horizontal adjustment of the viewport.
491  **/
492 void
493 gtk_viewport_set_hadjustment (GtkViewport   *viewport,
494                               GtkAdjustment *adjustment)
495 {
496   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
497   if (adjustment)
498     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
499
500   viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
501
502   g_object_notify (G_OBJECT (viewport), "hadjustment");
503 }
504
505 /**
506  * gtk_viewport_set_vadjustment:
507  * @viewport: a #GtkViewport.
508  * @adjustment: (allow-none): a #GtkAdjustment.
509  *
510  * Sets the vertical adjustment of the viewport.
511  **/
512 void
513 gtk_viewport_set_vadjustment (GtkViewport   *viewport,
514                               GtkAdjustment *adjustment)
515 {
516   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
517   if (adjustment)
518     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
519
520   viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
521
522   g_object_notify (G_OBJECT (viewport), "vadjustment");
523 }
524
525 static void
526 gtk_viewport_set_scroll_adjustments (GtkViewport      *viewport,
527                                      GtkAdjustment    *hadjustment,
528                                      GtkAdjustment    *vadjustment)
529 {
530   gtk_viewport_set_hadjustment (viewport, hadjustment);
531   gtk_viewport_set_vadjustment (viewport, vadjustment);
532 }
533
534 /** 
535  * gtk_viewport_set_shadow_type:
536  * @viewport: a #GtkViewport.
537  * @type: the new shadow type.
538  *
539  * Sets the shadow type of the viewport.
540  **/ 
541 void
542 gtk_viewport_set_shadow_type (GtkViewport   *viewport,
543                               GtkShadowType  type)
544 {
545   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
546
547   if ((GtkShadowType) viewport->shadow_type != type)
548     {
549       viewport->shadow_type = type;
550
551       if (gtk_widget_get_visible (GTK_WIDGET (viewport)))
552         {
553           gtk_widget_size_allocate (GTK_WIDGET (viewport), &(GTK_WIDGET (viewport)->allocation));
554           gtk_widget_queue_draw (GTK_WIDGET (viewport));
555         }
556
557       g_object_notify (G_OBJECT (viewport), "shadow-type");
558     }
559 }
560
561 /**
562  * gtk_viewport_get_shadow_type:
563  * @viewport: a #GtkViewport
564  *
565  * Gets the shadow type of the #GtkViewport. See
566  * gtk_viewport_set_shadow_type().
567  
568  * Return value: the shadow type 
569  **/
570 GtkShadowType
571 gtk_viewport_get_shadow_type (GtkViewport *viewport)
572 {
573   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
574
575   return viewport->shadow_type;
576 }
577
578 /**
579  * gtk_viewport_get_bin_window:
580  * @viewport: a #GtkViewport
581  *
582  * Gets the bin window of the #GtkViewport.
583  *
584  * Return value: a #GdkWindow
585  *
586  * Since: 2.20
587  **/
588 GdkWindow*
589 gtk_viewport_get_bin_window (GtkViewport *viewport)
590 {
591   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
592
593   return viewport->bin_window;
594 }
595
596 static void
597 gtk_viewport_realize (GtkWidget *widget)
598 {
599   GtkViewport *viewport = GTK_VIEWPORT (widget);
600   GtkBin *bin = GTK_BIN (widget);
601   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
602   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
603   gint border_width = GTK_CONTAINER (widget)->border_width;
604   
605   GtkAllocation view_allocation;
606   GdkWindowAttr attributes;
607   gint attributes_mask;
608   gint event_mask;
609
610   gtk_widget_set_realized (widget, TRUE);
611
612   attributes.x = widget->allocation.x + border_width;
613   attributes.y = widget->allocation.y + border_width;
614   attributes.width = widget->allocation.width - border_width * 2;
615   attributes.height = widget->allocation.height - border_width * 2;
616   attributes.window_type = GDK_WINDOW_CHILD;
617   attributes.wclass = GDK_INPUT_OUTPUT;
618   attributes.visual = gtk_widget_get_visual (widget);
619   attributes.colormap = gtk_widget_get_colormap (widget);
620
621   event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
622   /* We select on button_press_mask so that button 4-5 scrolls are trapped.
623    */
624   attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
625
626   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
627
628   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
629                                    &attributes, attributes_mask);
630   gdk_window_set_user_data (widget->window, viewport);
631
632   viewport_get_view_allocation (viewport, &view_allocation);
633   
634   attributes.x = view_allocation.x;
635   attributes.y = view_allocation.y;
636   attributes.width = view_allocation.width;
637   attributes.height = view_allocation.height;
638   attributes.event_mask = 0;
639
640   viewport->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
641   gdk_window_set_user_data (viewport->view_window, viewport);
642
643   gdk_window_set_back_pixmap (viewport->view_window, NULL, FALSE);
644   
645   attributes.x = - hadjustment->value;
646   attributes.y = - vadjustment->value;
647   attributes.width = hadjustment->upper;
648   attributes.height = vadjustment->upper;
649   
650   attributes.event_mask = event_mask;
651
652   viewport->bin_window = gdk_window_new (viewport->view_window, &attributes, attributes_mask);
653   gdk_window_set_user_data (viewport->bin_window, viewport);
654
655   if (bin->child)
656     gtk_widget_set_parent_window (bin->child, viewport->bin_window);
657
658   widget->style = gtk_style_attach (widget->style, widget->window);
659   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
660   gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
661
662   /* Call paint here to allow a theme to set the background without flashing
663    */
664   gtk_paint_flat_box(widget->style, viewport->bin_window, GTK_STATE_NORMAL,
665                      GTK_SHADOW_NONE,
666                      NULL, widget, "viewportbin",
667                      0, 0, -1, -1);
668    
669   gdk_window_show (viewport->bin_window);
670   gdk_window_show (viewport->view_window);
671 }
672
673 static void
674 gtk_viewport_unrealize (GtkWidget *widget)
675 {
676   GtkViewport *viewport = GTK_VIEWPORT (widget);
677
678   gdk_window_set_user_data (viewport->view_window, NULL);
679   gdk_window_destroy (viewport->view_window);
680   viewport->view_window = NULL;
681
682   gdk_window_set_user_data (viewport->bin_window, NULL);
683   gdk_window_destroy (viewport->bin_window);
684   viewport->bin_window = NULL;
685
686   GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
687 }
688
689 static void
690 gtk_viewport_paint (GtkWidget    *widget,
691                     GdkRectangle *area)
692 {
693   if (gtk_widget_is_drawable (widget))
694     {
695       GtkViewport *viewport = GTK_VIEWPORT (widget);
696
697       gtk_paint_shadow (widget->style, widget->window,
698                         GTK_STATE_NORMAL, viewport->shadow_type,
699                         area, widget, "viewport",
700                         0, 0, -1, -1);
701     }
702 }
703
704 static gint
705 gtk_viewport_expose (GtkWidget      *widget,
706                      GdkEventExpose *event)
707 {
708   GtkViewport *viewport;
709
710   if (gtk_widget_is_drawable (widget))
711     {
712       viewport = GTK_VIEWPORT (widget);
713
714       if (event->window == widget->window)
715         gtk_viewport_paint (widget, &event->area);
716       else if (event->window == viewport->bin_window)
717         {
718           gtk_paint_flat_box(widget->style, viewport->bin_window, 
719                              GTK_STATE_NORMAL, GTK_SHADOW_NONE,
720                              &event->area, widget, "viewportbin",
721                              0, 0, -1, -1);
722
723           GTK_WIDGET_CLASS (gtk_viewport_parent_class)->expose_event (widget, event);
724         }
725     }
726
727   return FALSE;
728 }
729
730 static void
731 gtk_viewport_add (GtkContainer *container,
732                   GtkWidget    *child)
733 {
734   GtkBin *bin = GTK_BIN (container);
735
736   g_return_if_fail (bin->child == NULL);
737
738   gtk_widget_set_parent_window (child, GTK_VIEWPORT (bin)->bin_window);
739
740   GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
741 }
742
743 static void
744 gtk_viewport_size_request (GtkWidget      *widget,
745                            GtkRequisition *requisition)
746 {
747   GtkBin *bin = GTK_BIN (widget);
748   GtkRequisition child_requisition;
749
750   requisition->width = GTK_CONTAINER (widget)->border_width;
751
752   requisition->height = GTK_CONTAINER (widget)->border_width;
753
754   if (GTK_VIEWPORT (widget)->shadow_type != GTK_SHADOW_NONE)
755     {
756       requisition->width += 2 * widget->style->xthickness;
757       requisition->height += 2 * widget->style->ythickness;
758     }
759
760   if (bin->child && gtk_widget_get_visible (bin->child))
761     {
762       gtk_widget_size_request (bin->child, &child_requisition);
763       requisition->width += child_requisition.width;
764       requisition->height += child_requisition.height;
765     }
766 }
767
768 static void
769 gtk_viewport_size_allocate (GtkWidget     *widget,
770                             GtkAllocation *allocation)
771 {
772   GtkViewport *viewport = GTK_VIEWPORT (widget);
773   GtkBin *bin = GTK_BIN (widget);
774   gint border_width = GTK_CONTAINER (widget)->border_width;
775   gboolean hadjustment_value_changed, vadjustment_value_changed;
776   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
777   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
778   GtkAllocation child_allocation;
779
780   /* If our size changed, and we have a shadow, queue a redraw on widget->window to
781    * redraw the shadow correctly.
782    */
783   if (gtk_widget_get_mapped (widget) &&
784       viewport->shadow_type != GTK_SHADOW_NONE &&
785       (widget->allocation.width != allocation->width ||
786        widget->allocation.height != allocation->height))
787     gdk_window_invalidate_rect (widget->window, NULL, FALSE);
788   
789   widget->allocation = *allocation;
790   
791   viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
792   viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
793   
794   child_allocation.x = 0;
795   child_allocation.y = 0;
796   child_allocation.width = hadjustment->upper;
797   child_allocation.height = vadjustment->upper;
798   if (gtk_widget_get_realized (widget))
799     {
800       GtkAllocation view_allocation;
801       gdk_window_move_resize (widget->window,
802                               allocation->x + border_width,
803                               allocation->y + border_width,
804                               allocation->width - border_width * 2,
805                               allocation->height - border_width * 2);
806       
807       viewport_get_view_allocation (viewport, &view_allocation);
808       gdk_window_move_resize (viewport->view_window,
809                               view_allocation.x,
810                               view_allocation.y,
811                               view_allocation.width,
812                               view_allocation.height);
813       gdk_window_move_resize (viewport->bin_window,
814                               - hadjustment->value,
815                               - vadjustment->value,
816                               child_allocation.width,
817                               child_allocation.height);
818     }
819   if (bin->child && gtk_widget_get_visible (bin->child))
820     gtk_widget_size_allocate (bin->child, &child_allocation);
821
822   gtk_adjustment_changed (hadjustment);
823   gtk_adjustment_changed (vadjustment);
824   if (hadjustment_value_changed)
825     gtk_adjustment_value_changed (hadjustment);
826   if (vadjustment_value_changed)
827     gtk_adjustment_value_changed (vadjustment);
828 }
829
830 static void
831 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
832                                        gpointer       data)
833 {
834   GtkViewport *viewport = GTK_VIEWPORT (data);
835   GtkBin *bin = GTK_BIN (data);
836
837   if (bin->child && gtk_widget_get_visible (bin->child) &&
838       gtk_widget_get_realized (GTK_WIDGET (viewport)))
839     {
840       GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
841       GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
842       gint old_x, old_y;
843       gint new_x, new_y;
844
845       gdk_window_get_position (viewport->bin_window, &old_x, &old_y);
846       new_x = - hadjustment->value;
847       new_y = - vadjustment->value;
848
849       if (new_x != old_x || new_y != old_y)
850         {
851           gdk_window_move (viewport->bin_window, new_x, new_y);
852           gdk_window_process_updates (viewport->bin_window, TRUE);
853         }
854     }
855 }
856
857 static void
858 gtk_viewport_style_set (GtkWidget *widget,
859                         GtkStyle  *previous_style)
860 {
861    if (gtk_widget_get_realized (widget) &&
862        gtk_widget_get_has_window (widget))
863      {
864         GtkViewport *viewport = GTK_VIEWPORT (widget);
865
866         gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
867         gtk_style_set_background (widget->style, widget->window, widget->state);
868      }
869 }
870
871 #define __GTK_VIEWPORT_C__
872 #include "gtkaliasdef.c"