]> Pileus Git - ~andy/gtk/blob - gtk/gtkviewport.c
The first part of the fix for #114351 (see also gdk-pixbuf/ChangeLog and
[~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 "gtkviewport.h"
28 #include "gtkintl.h"
29 #include "gtkmarshalers.h"
30
31 enum {
32   PROP_0,
33   PROP_HADJUSTMENT,
34   PROP_VADJUSTMENT,
35   PROP_SHADOW_TYPE
36 };
37
38
39 static void gtk_viewport_class_init               (GtkViewportClass *klass);
40 static void gtk_viewport_init                     (GtkViewport      *viewport);
41 static void gtk_viewport_finalize                 (GObject          *object);
42 static void gtk_viewport_destroy                  (GtkObject        *object);
43 static void gtk_viewport_set_property             (GObject         *object,
44                                                    guint            prop_id,
45                                                    const GValue    *value,
46                                                    GParamSpec      *pspec);
47 static void gtk_viewport_get_property             (GObject         *object,
48                                                    guint            prop_id,
49                                                    GValue          *value,
50                                                    GParamSpec      *pspec);
51 static void gtk_viewport_set_scroll_adjustments   (GtkViewport      *viewport,
52                                                    GtkAdjustment    *hadjustment,
53                                                    GtkAdjustment    *vadjustment);
54 static void gtk_viewport_realize                  (GtkWidget        *widget);
55 static void gtk_viewport_unrealize                (GtkWidget        *widget);
56 static void gtk_viewport_paint                    (GtkWidget        *widget,
57                                                    GdkRectangle     *area);
58 static gint gtk_viewport_expose                   (GtkWidget        *widget,
59                                                    GdkEventExpose   *event);
60 static void gtk_viewport_add                      (GtkContainer     *container,
61                                                    GtkWidget        *widget);
62 static void gtk_viewport_size_request             (GtkWidget        *widget,
63                                                    GtkRequisition   *requisition);
64 static void gtk_viewport_size_allocate            (GtkWidget        *widget,
65                                                    GtkAllocation    *allocation);
66 static void gtk_viewport_adjustment_value_changed (GtkAdjustment    *adjustment,
67                                                    gpointer          data);
68 static void gtk_viewport_style_set                (GtkWidget *widget,
69                                                    GtkStyle  *previous_style);
70
71 static GtkBinClass *parent_class;
72
73 GType
74 gtk_viewport_get_type (void)
75 {
76   static GType viewport_type = 0;
77
78   if (!viewport_type)
79     {
80       static const GTypeInfo viewport_info =
81       {
82         sizeof (GtkViewportClass),
83         NULL,           /* base_init */
84         NULL,           /* base_finalize */
85         (GClassInitFunc) gtk_viewport_class_init,
86         NULL,           /* class_finalize */
87         NULL,           /* class_data */
88         sizeof (GtkViewport),
89         0,              /* n_preallocs */
90         (GInstanceInitFunc) gtk_viewport_init,
91       };
92
93       viewport_type = g_type_register_static (GTK_TYPE_BIN, "GtkViewport",
94                                               &viewport_info, 0);
95     }
96
97   return viewport_type;
98 }
99
100 static void
101 gtk_viewport_class_init (GtkViewportClass *class)
102 {
103   GtkObjectClass *object_class;
104   GObjectClass   *gobject_class;
105   GtkWidgetClass *widget_class;
106   GtkContainerClass *container_class;
107
108   object_class = (GtkObjectClass*) class;
109   gobject_class = G_OBJECT_CLASS (class);
110   widget_class = (GtkWidgetClass*) class;
111   container_class = (GtkContainerClass*) class;
112
113   parent_class = g_type_class_peek_parent (class);
114
115   gobject_class->finalize = gtk_viewport_finalize;
116   gobject_class->set_property = gtk_viewport_set_property;
117   gobject_class->get_property = gtk_viewport_get_property;
118   object_class->destroy = gtk_viewport_destroy;
119   
120   widget_class->realize = gtk_viewport_realize;
121   widget_class->unrealize = gtk_viewport_unrealize;
122   widget_class->expose_event = gtk_viewport_expose;
123   widget_class->size_request = gtk_viewport_size_request;
124   widget_class->size_allocate = gtk_viewport_size_allocate;
125   widget_class->style_set = gtk_viewport_style_set;
126   
127   container_class->add = gtk_viewport_add;
128
129   class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;
130
131   g_object_class_install_property (gobject_class,
132                                    PROP_HADJUSTMENT,
133                                    g_param_spec_object ("hadjustment",
134                                                         P_("Horizontal adjustment"),
135                                                         P_("The GtkAdjustment that determines the values of the horizontal position for this viewport"),
136                                                         GTK_TYPE_ADJUSTMENT,
137                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
138
139   g_object_class_install_property (gobject_class,
140                                    PROP_VADJUSTMENT,
141                                    g_param_spec_object ("vadjustment",
142                                                         P_("Vertical adjustment"),
143                                                         P_("The GtkAdjustment that determines the values of the vertical position for this viewport"),
144                                                         GTK_TYPE_ADJUSTMENT,
145                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
146
147   g_object_class_install_property (gobject_class,
148                                    PROP_SHADOW_TYPE,
149                                    g_param_spec_enum ("shadow_type",
150                                                       P_("Shadow type"),
151                                                       P_("Determines how the shadowed box around the viewport is drawn"),
152                                                       GTK_TYPE_SHADOW_TYPE,
153                                                       GTK_SHADOW_IN,
154                                                       G_PARAM_READWRITE));
155
156   widget_class->set_scroll_adjustments_signal =
157     g_signal_new ("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_UNSET_FLAGS (viewport, GTK_NO_WINDOW);
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 = gtk_widget_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 (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 (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_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_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
465   *adjustmentp = adjustment;
466   g_object_ref (adjustment);
467   gtk_object_sink (GTK_OBJECT (adjustment));
468
469   if (orientation == GTK_ORIENTATION_HORIZONTAL)
470     viewport_set_hadjustment_values (viewport, &value_changed);
471   else
472     viewport_set_vadjustment_values (viewport, &value_changed);
473
474   g_signal_connect (adjustment, "value_changed",
475                     G_CALLBACK (gtk_viewport_adjustment_value_changed),
476                     viewport);
477
478   gtk_adjustment_changed (adjustment);
479   
480   if (value_changed)
481     gtk_adjustment_value_changed (adjustment);
482   else
483     gtk_viewport_adjustment_value_changed (adjustment, viewport);
484 }
485
486 /**
487  * gtk_viewport_set_hadjustment:
488  * @viewport: a #GtkViewport.
489  * @adjustment: a #GtkAdjustment.
490  * 
491  * Sets the horizontal adjustment of the viewport.
492  **/
493 void
494 gtk_viewport_set_hadjustment (GtkViewport   *viewport,
495                               GtkAdjustment *adjustment)
496 {
497   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
498   if (adjustment)
499     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
500
501   viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
502
503   g_object_notify (G_OBJECT (viewport), "hadjustment");
504 }
505
506 /**
507  * gtk_viewport_set_vadjustment:
508  * @viewport: a #GtkViewport.
509  * @adjustment: a #GtkAdjustment.
510  * 
511  * Sets the vertical adjustment of the viewport.
512  **/
513 void
514 gtk_viewport_set_vadjustment (GtkViewport   *viewport,
515                               GtkAdjustment *adjustment)
516 {
517   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
518   if (adjustment)
519     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
520
521   viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
522
523   g_object_notify (G_OBJECT (viewport), "vadjustment");
524 }
525
526 static void
527 gtk_viewport_set_scroll_adjustments (GtkViewport      *viewport,
528                                      GtkAdjustment    *hadjustment,
529                                      GtkAdjustment    *vadjustment)
530 {
531   gtk_viewport_set_hadjustment (viewport, hadjustment);
532   gtk_viewport_set_vadjustment (viewport, vadjustment);
533 }
534
535 /** 
536  * gtk_viewport_set_shadow_type:
537  * @viewport: a #GtkViewport.
538  * @type: the new shadow type.
539  *
540  * Sets the shadow type of the viewport.
541  **/ 
542 void
543 gtk_viewport_set_shadow_type (GtkViewport   *viewport,
544                               GtkShadowType  type)
545 {
546   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
547
548   if ((GtkShadowType) viewport->shadow_type != type)
549     {
550       viewport->shadow_type = type;
551
552       if (GTK_WIDGET_VISIBLE (viewport))
553         {
554           gtk_widget_size_allocate (GTK_WIDGET (viewport), &(GTK_WIDGET (viewport)->allocation));
555           gtk_widget_queue_draw (GTK_WIDGET (viewport));
556         }
557
558       g_object_notify (G_OBJECT (viewport), "shadow_type");
559     }
560 }
561
562 /**
563  * gtk_viewport_get_shadow_type:
564  * @viewport: a #GtkViewport
565  *
566  * Gets the shadow type of the #GtkViewport. See
567  * gtk_viewport_set_shadow_type().
568  
569  * Return value: the shadow type 
570  **/
571 GtkShadowType
572 gtk_viewport_get_shadow_type (GtkViewport *viewport)
573 {
574   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
575
576   return viewport->shadow_type;
577 }
578
579 static void
580 gtk_viewport_realize (GtkWidget *widget)
581 {
582   GtkViewport *viewport = GTK_VIEWPORT (widget);
583   GtkBin *bin = GTK_BIN (widget);
584   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
585   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
586   gint border_width = GTK_CONTAINER (widget)->border_width;
587   
588   GtkAllocation view_allocation;
589   GdkWindowAttr attributes;
590   gint attributes_mask;
591   gint event_mask;
592
593   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
594
595   attributes.x = widget->allocation.x + border_width;
596   attributes.y = widget->allocation.y + border_width;
597   attributes.width = widget->allocation.width - border_width * 2;
598   attributes.height = widget->allocation.height - border_width * 2;
599   attributes.window_type = GDK_WINDOW_CHILD;
600   attributes.wclass = GDK_INPUT_OUTPUT;
601   attributes.visual = gtk_widget_get_visual (widget);
602   attributes.colormap = gtk_widget_get_colormap (widget);
603
604   event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
605   /* We select on button_press_mask so that button 4-5 scrolls are trapped.
606    */
607   attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
608
609   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
610
611   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
612                                    &attributes, attributes_mask);
613   gdk_window_set_user_data (widget->window, viewport);
614
615   viewport_get_view_allocation (viewport, &view_allocation);
616   
617   attributes.x = view_allocation.x;
618   attributes.y = view_allocation.y;
619   attributes.width = view_allocation.width;
620   attributes.height = view_allocation.height;
621   attributes.event_mask = 0;
622
623   viewport->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
624   gdk_window_set_user_data (viewport->view_window, viewport);
625
626   attributes.x = - hadjustment->value;
627   attributes.y = - vadjustment->value;
628   attributes.width = hadjustment->upper;
629   attributes.height = vadjustment->upper;
630   
631   attributes.event_mask = event_mask;
632
633   viewport->bin_window = gdk_window_new (viewport->view_window, &attributes, attributes_mask);
634   gdk_window_set_user_data (viewport->bin_window, viewport);
635
636   if (bin->child)
637     gtk_widget_set_parent_window (bin->child, viewport->bin_window);
638
639   widget->style = gtk_style_attach (widget->style, widget->window);
640   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
641   gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
642
643   /* Call paint here to allow a theme to set the background without flashing
644    */
645   gtk_paint_flat_box(widget->style, viewport->bin_window, GTK_STATE_NORMAL,
646                      GTK_SHADOW_NONE,
647                      NULL, widget, "viewportbin",
648                      0, 0, -1, -1);
649    
650   gdk_window_show (viewport->bin_window);
651   gdk_window_show (viewport->view_window);
652 }
653
654 static void
655 gtk_viewport_unrealize (GtkWidget *widget)
656 {
657   GtkViewport *viewport = GTK_VIEWPORT (widget);
658
659   gdk_window_set_user_data (viewport->view_window, NULL);
660   gdk_window_destroy (viewport->view_window);
661   viewport->view_window = NULL;
662
663   gdk_window_set_user_data (viewport->bin_window, NULL);
664   gdk_window_destroy (viewport->bin_window);
665   viewport->bin_window = NULL;
666
667   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
668     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
669 }
670
671 static void
672 gtk_viewport_paint (GtkWidget    *widget,
673                     GdkRectangle *area)
674 {
675   GtkViewport *viewport;
676
677   g_return_if_fail (GTK_IS_VIEWPORT (widget));
678   g_return_if_fail (area != NULL);
679
680   if (GTK_WIDGET_DRAWABLE (widget))
681     {
682       viewport = GTK_VIEWPORT (widget);
683
684       gtk_paint_shadow (widget->style, widget->window,
685                         GTK_STATE_NORMAL, viewport->shadow_type,
686                         NULL, widget, "viewport",
687                         0, 0, -1, -1);
688     }
689 }
690
691 static gint
692 gtk_viewport_expose (GtkWidget      *widget,
693                      GdkEventExpose *event)
694 {
695   GtkViewport *viewport;
696   GtkBin *bin;
697
698   if (GTK_WIDGET_DRAWABLE (widget))
699     {
700       viewport = GTK_VIEWPORT (widget);
701       bin = GTK_BIN (widget);
702
703       if (event->window == widget->window)
704         gtk_viewport_paint (widget, &event->area);
705       else if (event->window == viewport->bin_window)
706         {
707           gtk_paint_flat_box(widget->style, viewport->bin_window, 
708                              GTK_STATE_NORMAL, GTK_SHADOW_NONE,
709                              &event->area, widget, "viewportbin",
710                              0, 0, -1, -1);
711           
712           (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
713         }
714     }
715
716   return FALSE;
717 }
718
719 static void
720 gtk_viewport_add (GtkContainer *container,
721                   GtkWidget    *child)
722 {
723   GtkBin *bin;
724
725   g_return_if_fail (GTK_IS_WIDGET (child));
726
727   bin = GTK_BIN (container);
728   g_return_if_fail (bin->child == NULL);
729
730   gtk_widget_set_parent_window (child, GTK_VIEWPORT (bin)->bin_window);
731
732   GTK_CONTAINER_CLASS (parent_class)->add (container, child);
733 }
734
735 static void
736 gtk_viewport_size_request (GtkWidget      *widget,
737                            GtkRequisition *requisition)
738 {
739   GtkViewport *viewport;
740   GtkBin *bin;
741   GtkRequisition child_requisition;
742
743   viewport = GTK_VIEWPORT (widget);
744   bin = GTK_BIN (widget);
745
746   requisition->width = (GTK_CONTAINER (widget)->border_width +
747                         GTK_WIDGET (widget)->style->xthickness) * 2;
748
749   requisition->height = (GTK_CONTAINER (widget)->border_width * 2 +
750                          GTK_WIDGET (widget)->style->ythickness) * 2;
751
752   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
753     {
754       gtk_widget_size_request (bin->child, &child_requisition);
755       requisition->width += child_requisition.width;
756       requisition->height += child_requisition.height;
757     }
758 }
759
760 static void
761 gtk_viewport_size_allocate (GtkWidget     *widget,
762                             GtkAllocation *allocation)
763 {
764   GtkViewport *viewport = GTK_VIEWPORT (widget);
765   GtkBin *bin = GTK_BIN (widget);
766   gint border_width = GTK_CONTAINER (widget)->border_width;
767   gboolean hadjustment_value_changed, vadjustment_value_changed;
768
769   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
770   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
771
772   /* If our size changed, and we have a shadow, queue a redraw on widget->window to
773    * redraw the shadow correctly.
774    */
775   if (GTK_WIDGET_MAPPED (widget) &&
776       viewport->shadow_type != GTK_SHADOW_NONE &&
777       (widget->allocation.width != allocation->width ||
778        widget->allocation.height != allocation->height))
779     gdk_window_invalidate_rect (widget->window, NULL, FALSE);
780   
781   widget->allocation = *allocation;
782
783   viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
784   viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
785
786   if (GTK_WIDGET_REALIZED (widget))
787     {
788       GtkAllocation view_allocation;
789       
790       viewport_get_view_allocation (viewport, &view_allocation);
791   
792       gdk_window_move_resize (widget->window,
793                               allocation->x + border_width,
794                               allocation->y + border_width,
795                               allocation->width - border_width * 2,
796                               allocation->height - border_width * 2);
797
798       gdk_window_move_resize (viewport->view_window,
799                               view_allocation.x,
800                               view_allocation.y,
801                               view_allocation.width,
802                               view_allocation.height);
803     }
804
805   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
806     {
807       GtkAllocation child_allocation;
808       
809       child_allocation.x = 0;
810       child_allocation.y = 0;
811       child_allocation.width = hadjustment->upper;
812       child_allocation.height = vadjustment->upper;
813
814       if (GTK_WIDGET_REALIZED (widget))
815         gdk_window_move_resize (viewport->bin_window,
816                                 - hadjustment->value,
817                                 - vadjustment->value,
818                                 child_allocation.width,
819                                 child_allocation.height);
820
821       gtk_widget_size_allocate (bin->child, &child_allocation);
822     }
823
824   gtk_adjustment_changed (hadjustment);
825   gtk_adjustment_changed (vadjustment);
826   if (hadjustment_value_changed)
827     gtk_adjustment_value_changed (hadjustment);
828   if (vadjustment_value_changed)
829     gtk_adjustment_value_changed (vadjustment);
830 }
831
832 static void
833 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
834                                        gpointer       data)
835 {
836   GtkViewport *viewport;
837   GtkBin *bin;
838
839   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
840   g_return_if_fail (GTK_IS_VIEWPORT (data));
841
842   viewport = GTK_VIEWPORT (data);
843   bin = GTK_BIN (data);
844
845   if (bin->child && GTK_WIDGET_VISIBLE (bin->child)  &&
846       GTK_WIDGET_REALIZED (viewport))
847     {
848       GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
849       GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
850       gint old_x, old_y;
851       gint new_x, new_y;
852
853       gdk_window_get_position (viewport->bin_window, &old_x, &old_y);
854       new_x = - hadjustment->value;
855       new_y = - vadjustment->value;
856
857       if (new_x != old_x || new_y != old_y)
858         {
859           gdk_window_move (viewport->bin_window, new_x, new_y);
860           gdk_window_process_updates (viewport->bin_window, TRUE);
861         }
862     }
863 }
864
865 static void
866 gtk_viewport_style_set (GtkWidget *widget,
867                         GtkStyle  *previous_style)
868 {
869    GtkViewport *viewport;
870    
871    if (GTK_WIDGET_REALIZED (widget) &&
872        !GTK_WIDGET_NO_WINDOW (widget))
873      {
874         viewport = GTK_VIEWPORT (widget);
875         
876         gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
877         gtk_style_set_background (widget->style, widget->window, widget->state);
878      }
879 }