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