]> Pileus Git - ~andy/gtk/blob - gtk/gtkviewport.c
Deprecate widget flag: GTK_WIDGET_MAPPED
[~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   /**
129    * GtkViewport::set-scroll-adjustments
130    * @horizontal: the horizontal #GtkAdjustment
131    * @vertical: the vertical #GtkAdjustment
132    *
133    * Set the scroll adjustments for the viewport. Usually scrolled containers
134    * like #GtkScrolledWindow will emit this signal to connect two instances
135    * of #GtkScrollbar to the scroll directions of the #GtkViewport.
136    */
137   widget_class->set_scroll_adjustments_signal =
138     g_signal_new (I_("set-scroll-adjustments"),
139                   G_OBJECT_CLASS_TYPE (gobject_class),
140                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
141                   G_STRUCT_OFFSET (GtkViewportClass, set_scroll_adjustments),
142                   NULL, NULL,
143                   _gtk_marshal_VOID__OBJECT_OBJECT,
144                   G_TYPE_NONE, 2,
145                   GTK_TYPE_ADJUSTMENT,
146                   GTK_TYPE_ADJUSTMENT);
147 }
148
149 static void
150 gtk_viewport_set_property (GObject         *object,
151                            guint            prop_id,
152                            const GValue    *value,
153                            GParamSpec      *pspec)
154 {
155   GtkViewport *viewport;
156
157   viewport = GTK_VIEWPORT (object);
158
159   switch (prop_id)
160     {
161     case PROP_HADJUSTMENT:
162       gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
163       break;
164     case PROP_VADJUSTMENT:
165       gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
166       break;
167     case PROP_SHADOW_TYPE:
168       gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
169       break;
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172       break;
173     }
174 }
175
176 static void
177 gtk_viewport_get_property (GObject         *object,
178                            guint            prop_id,
179                            GValue          *value,
180                            GParamSpec      *pspec)
181 {
182   GtkViewport *viewport;
183
184   viewport = GTK_VIEWPORT (object);
185
186   switch (prop_id)
187     {
188     case PROP_HADJUSTMENT:
189       g_value_set_object (value, viewport->hadjustment);
190       break;
191     case PROP_VADJUSTMENT:
192       g_value_set_object (value, viewport->vadjustment);
193       break;
194     case PROP_SHADOW_TYPE:
195       g_value_set_enum (value, viewport->shadow_type);
196       break;
197     default:
198       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199       break;
200     }
201 }
202
203 static void
204 gtk_viewport_init (GtkViewport *viewport)
205 {
206   GTK_WIDGET_UNSET_FLAGS (viewport, GTK_NO_WINDOW);
207
208   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
209   gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
210   
211   viewport->shadow_type = GTK_SHADOW_IN;
212   viewport->view_window = NULL;
213   viewport->bin_window = NULL;
214   viewport->hadjustment = NULL;
215   viewport->vadjustment = NULL;
216 }
217
218 /**
219  * gtk_viewport_new:
220  * @hadjustment: horizontal adjustment.
221  * @vadjustment: vertical adjustment.
222  * @returns: a new #GtkViewport.
223  *
224  * Creates a new #GtkViewport with the given adjustments.
225  *
226  **/
227 GtkWidget*
228 gtk_viewport_new (GtkAdjustment *hadjustment,
229                   GtkAdjustment *vadjustment)
230 {
231   GtkWidget *viewport;
232
233   viewport = g_object_new (GTK_TYPE_VIEWPORT,
234                              "hadjustment", hadjustment,
235                              "vadjustment", vadjustment,
236                              NULL);
237
238   return viewport;
239 }
240
241 #define ADJUSTMENT_POINTER(viewport, orientation)         \
242   (((orientation) == GTK_ORIENTATION_HORIZONTAL) ?        \
243      &(viewport)->hadjustment : &(viewport)->vadjustment)
244
245 static void
246 viewport_disconnect_adjustment (GtkViewport    *viewport,
247                                 GtkOrientation  orientation)
248 {
249   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
250
251   if (*adjustmentp)
252     {
253       g_signal_handlers_disconnect_by_func (*adjustmentp,
254                                             gtk_viewport_adjustment_value_changed,
255                                             viewport);
256       g_object_unref (*adjustmentp);
257       *adjustmentp = NULL;
258     }
259 }
260
261 static void
262 gtk_viewport_finalize (GObject *object)
263 {
264   GtkViewport *viewport = GTK_VIEWPORT (object);
265
266   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
267   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
268
269   G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
270 }
271
272 static void
273 gtk_viewport_destroy (GtkObject *object)
274 {
275   GtkViewport *viewport = GTK_VIEWPORT (object);
276
277   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
278   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
279
280   GTK_OBJECT_CLASS (gtk_viewport_parent_class)->destroy (object);
281 }
282
283 /**
284  * gtk_viewport_get_hadjustment:
285  * @viewport: a #GtkViewport.
286  * 
287  * Returns the horizontal adjustment of the viewport.
288  *
289  * Return value: the horizontal adjustment of @viewport.
290  **/
291 GtkAdjustment*
292 gtk_viewport_get_hadjustment (GtkViewport *viewport)
293 {
294   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
295
296   if (!viewport->hadjustment)
297     gtk_viewport_set_hadjustment (viewport, NULL);
298
299   return viewport->hadjustment;
300 }
301
302 /**
303  * gtk_viewport_get_vadjustment:
304  * @viewport: a #GtkViewport.
305  * 
306  * Returns the vertical adjustment of the viewport.
307  *
308  * Return value: the vertical adjustment of @viewport.
309  **/
310 GtkAdjustment*
311 gtk_viewport_get_vadjustment (GtkViewport *viewport)
312 {
313   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
314
315   if (!viewport->vadjustment)
316     gtk_viewport_set_vadjustment (viewport, NULL);
317
318   return viewport->vadjustment;
319 }
320
321 static void
322 viewport_get_view_allocation (GtkViewport   *viewport,
323                               GtkAllocation *view_allocation)
324 {
325   GtkWidget *widget = GTK_WIDGET (viewport);
326   GtkAllocation *allocation = &widget->allocation;
327   gint border_width = GTK_CONTAINER (viewport)->border_width;
328   
329   view_allocation->x = 0;
330   view_allocation->y = 0;
331
332   if (viewport->shadow_type != GTK_SHADOW_NONE)
333     {
334       view_allocation->x = widget->style->xthickness;
335       view_allocation->y = widget->style->ythickness;
336     }
337
338   view_allocation->width = MAX (1, allocation->width - view_allocation->x * 2 - border_width * 2);
339   view_allocation->height = MAX (1, allocation->height - view_allocation->y * 2 - border_width * 2);
340 }
341
342 static void
343 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
344                              gboolean      *value_changed)
345 {
346   gdouble value = adjustment->value;
347   
348   value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
349   if (value != adjustment->value)
350     {
351       adjustment->value = value;
352       if (value_changed)
353         *value_changed = TRUE;
354     }
355   else if (value_changed)
356     *value_changed = FALSE;
357 }
358
359 static void
360 viewport_set_hadjustment_values (GtkViewport *viewport,
361                                  gboolean    *value_changed)
362 {
363   GtkBin *bin = GTK_BIN (viewport);
364   GtkAllocation view_allocation;
365   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
366   gdouble old_page_size;
367   gdouble old_upper;
368   gdouble old_value;
369   
370   viewport_get_view_allocation (viewport, &view_allocation);  
371
372   old_page_size = hadjustment->page_size;
373   old_upper = hadjustment->upper;
374   old_value = hadjustment->value;
375   hadjustment->page_size = view_allocation.width;
376   hadjustment->step_increment = view_allocation.width * 0.1;
377   hadjustment->page_increment = view_allocation.width * 0.9;
378   
379   hadjustment->lower = 0;
380
381   if (bin->child && gtk_widget_get_visible (bin->child))
382     {
383       GtkRequisition child_requisition;
384       
385       gtk_widget_get_child_requisition (bin->child, &child_requisition);
386       hadjustment->upper = MAX (child_requisition.width, view_allocation.width);
387     }
388   else
389     hadjustment->upper = view_allocation.width;
390
391   if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL) 
392     {
393       gdouble dist = old_upper - (old_value + old_page_size);
394       hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
395       viewport_reclamp_adjustment (hadjustment, value_changed);
396       *value_changed = (old_value != hadjustment->value);
397     }
398   else
399     viewport_reclamp_adjustment (hadjustment, value_changed);
400 }
401
402 static void
403 viewport_set_vadjustment_values (GtkViewport *viewport,
404                                  gboolean    *value_changed)
405 {
406   GtkBin *bin = GTK_BIN (viewport);
407   GtkAllocation view_allocation;
408   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
409
410   viewport_get_view_allocation (viewport, &view_allocation);  
411
412   vadjustment->page_size = view_allocation.height;
413   vadjustment->step_increment = view_allocation.height * 0.1;
414   vadjustment->page_increment = view_allocation.height * 0.9;
415   
416   vadjustment->lower = 0;
417
418   if (bin->child && gtk_widget_get_visible (bin->child))
419     {
420       GtkRequisition child_requisition;
421       
422       gtk_widget_get_child_requisition (bin->child, &child_requisition);
423       vadjustment->upper = MAX (child_requisition.height, view_allocation.height);
424     }
425   else
426     vadjustment->upper = view_allocation.height;
427
428   viewport_reclamp_adjustment (vadjustment, value_changed);
429 }
430
431 static void
432 viewport_set_adjustment (GtkViewport    *viewport,
433                          GtkOrientation  orientation,
434                          GtkAdjustment  *adjustment)
435 {
436   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
437   gboolean value_changed;
438
439   if (adjustment && adjustment == *adjustmentp)
440     return;
441
442   if (!adjustment)
443     adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
444                                                      0.0, 0.0, 0.0));
445   viewport_disconnect_adjustment (viewport, orientation);
446   *adjustmentp = adjustment;
447   g_object_ref_sink (adjustment);
448
449   if (orientation == GTK_ORIENTATION_HORIZONTAL)
450     viewport_set_hadjustment_values (viewport, &value_changed);
451   else
452     viewport_set_vadjustment_values (viewport, &value_changed);
453
454   g_signal_connect (adjustment, "value-changed",
455                     G_CALLBACK (gtk_viewport_adjustment_value_changed),
456                     viewport);
457
458   gtk_adjustment_changed (adjustment);
459   
460   if (value_changed)
461     gtk_adjustment_value_changed (adjustment);
462   else
463     gtk_viewport_adjustment_value_changed (adjustment, viewport);
464 }
465
466 /**
467  * gtk_viewport_set_hadjustment:
468  * @viewport: a #GtkViewport.
469  * @adjustment: (allow-none): a #GtkAdjustment.
470  *
471  * Sets the horizontal adjustment of the viewport.
472  **/
473 void
474 gtk_viewport_set_hadjustment (GtkViewport   *viewport,
475                               GtkAdjustment *adjustment)
476 {
477   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
478   if (adjustment)
479     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
480
481   viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
482
483   g_object_notify (G_OBJECT (viewport), "hadjustment");
484 }
485
486 /**
487  * gtk_viewport_set_vadjustment:
488  * @viewport: a #GtkViewport.
489  * @adjustment: (allow-none): a #GtkAdjustment.
490  *
491  * Sets the vertical adjustment of the viewport.
492  **/
493 void
494 gtk_viewport_set_vadjustment (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_VERTICAL, adjustment);
502
503   g_object_notify (G_OBJECT (viewport), "vadjustment");
504 }
505
506 static void
507 gtk_viewport_set_scroll_adjustments (GtkViewport      *viewport,
508                                      GtkAdjustment    *hadjustment,
509                                      GtkAdjustment    *vadjustment)
510 {
511   gtk_viewport_set_hadjustment (viewport, hadjustment);
512   gtk_viewport_set_vadjustment (viewport, vadjustment);
513 }
514
515 /** 
516  * gtk_viewport_set_shadow_type:
517  * @viewport: a #GtkViewport.
518  * @type: the new shadow type.
519  *
520  * Sets the shadow type of the viewport.
521  **/ 
522 void
523 gtk_viewport_set_shadow_type (GtkViewport   *viewport,
524                               GtkShadowType  type)
525 {
526   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
527
528   if ((GtkShadowType) viewport->shadow_type != type)
529     {
530       viewport->shadow_type = type;
531
532       if (gtk_widget_get_visible (GTK_WIDGET (viewport)))
533         {
534           gtk_widget_size_allocate (GTK_WIDGET (viewport), &(GTK_WIDGET (viewport)->allocation));
535           gtk_widget_queue_draw (GTK_WIDGET (viewport));
536         }
537
538       g_object_notify (G_OBJECT (viewport), "shadow-type");
539     }
540 }
541
542 /**
543  * gtk_viewport_get_shadow_type:
544  * @viewport: a #GtkViewport
545  *
546  * Gets the shadow type of the #GtkViewport. See
547  * gtk_viewport_set_shadow_type().
548  
549  * Return value: the shadow type 
550  **/
551 GtkShadowType
552 gtk_viewport_get_shadow_type (GtkViewport *viewport)
553 {
554   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
555
556   return viewport->shadow_type;
557 }
558
559 /**
560  * gtk_viewport_get_bin_window:
561  * @viewport: a #GtkViewport
562  *
563  * Gets the bin window of the #GtkViewport.
564  *
565  * Return value: a #GdkWindow
566  *
567  * Since: 2.20
568  **/
569 GdkWindow*
570 gtk_viewport_get_bin_window (GtkViewport *viewport)
571 {
572   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
573
574   return viewport->bin_window;
575 }
576
577 static void
578 gtk_viewport_realize (GtkWidget *widget)
579 {
580   GtkViewport *viewport = GTK_VIEWPORT (widget);
581   GtkBin *bin = GTK_BIN (widget);
582   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
583   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
584   gint border_width = GTK_CONTAINER (widget)->border_width;
585   
586   GtkAllocation view_allocation;
587   GdkWindowAttr attributes;
588   gint attributes_mask;
589   gint event_mask;
590
591   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
592
593   attributes.x = widget->allocation.x + border_width;
594   attributes.y = widget->allocation.y + border_width;
595   attributes.width = widget->allocation.width - border_width * 2;
596   attributes.height = widget->allocation.height - border_width * 2;
597   attributes.window_type = GDK_WINDOW_CHILD;
598   attributes.wclass = GDK_INPUT_OUTPUT;
599   attributes.visual = gtk_widget_get_visual (widget);
600   attributes.colormap = gtk_widget_get_colormap (widget);
601
602   event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
603   /* We select on button_press_mask so that button 4-5 scrolls are trapped.
604    */
605   attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
606
607   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
608
609   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
610                                    &attributes, attributes_mask);
611   gdk_window_set_user_data (widget->window, viewport);
612
613   viewport_get_view_allocation (viewport, &view_allocation);
614   
615   attributes.x = view_allocation.x;
616   attributes.y = view_allocation.y;
617   attributes.width = view_allocation.width;
618   attributes.height = view_allocation.height;
619   attributes.event_mask = 0;
620
621   viewport->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
622   gdk_window_set_user_data (viewport->view_window, viewport);
623
624   gdk_window_set_back_pixmap (viewport->view_window, NULL, FALSE);
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   GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
668 }
669
670 static void
671 gtk_viewport_paint (GtkWidget    *widget,
672                     GdkRectangle *area)
673 {
674   if (gtk_widget_is_drawable (widget))
675     {
676       GtkViewport *viewport = GTK_VIEWPORT (widget);
677
678       gtk_paint_shadow (widget->style, widget->window,
679                         GTK_STATE_NORMAL, viewport->shadow_type,
680                         area, widget, "viewport",
681                         0, 0, -1, -1);
682     }
683 }
684
685 static gint
686 gtk_viewport_expose (GtkWidget      *widget,
687                      GdkEventExpose *event)
688 {
689   GtkViewport *viewport;
690
691   if (gtk_widget_is_drawable (widget))
692     {
693       viewport = GTK_VIEWPORT (widget);
694
695       if (event->window == widget->window)
696         gtk_viewport_paint (widget, &event->area);
697       else if (event->window == viewport->bin_window)
698         {
699           gtk_paint_flat_box(widget->style, viewport->bin_window, 
700                              GTK_STATE_NORMAL, GTK_SHADOW_NONE,
701                              &event->area, widget, "viewportbin",
702                              0, 0, -1, -1);
703
704           GTK_WIDGET_CLASS (gtk_viewport_parent_class)->expose_event (widget, event);
705         }
706     }
707
708   return FALSE;
709 }
710
711 static void
712 gtk_viewport_add (GtkContainer *container,
713                   GtkWidget    *child)
714 {
715   GtkBin *bin = GTK_BIN (container);
716
717   g_return_if_fail (bin->child == NULL);
718
719   gtk_widget_set_parent_window (child, GTK_VIEWPORT (bin)->bin_window);
720
721   GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
722 }
723
724 static void
725 gtk_viewport_size_request (GtkWidget      *widget,
726                            GtkRequisition *requisition)
727 {
728   GtkBin *bin = GTK_BIN (widget);
729   GtkRequisition child_requisition;
730
731   requisition->width = GTK_CONTAINER (widget)->border_width;
732
733   requisition->height = GTK_CONTAINER (widget)->border_width;
734
735   if (GTK_VIEWPORT (widget)->shadow_type != GTK_SHADOW_NONE)
736     {
737       requisition->width += 2 * widget->style->xthickness;
738       requisition->height += 2 * widget->style->ythickness;
739     }
740
741   if (bin->child && gtk_widget_get_visible (bin->child))
742     {
743       gtk_widget_size_request (bin->child, &child_requisition);
744       requisition->width += child_requisition.width;
745       requisition->height += child_requisition.height;
746     }
747 }
748
749 static void
750 gtk_viewport_size_allocate (GtkWidget     *widget,
751                             GtkAllocation *allocation)
752 {
753   GtkViewport *viewport = GTK_VIEWPORT (widget);
754   GtkBin *bin = GTK_BIN (widget);
755   gint border_width = GTK_CONTAINER (widget)->border_width;
756   gboolean hadjustment_value_changed, vadjustment_value_changed;
757   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
758   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
759   GtkAllocation child_allocation;
760
761   /* If our size changed, and we have a shadow, queue a redraw on widget->window to
762    * redraw the shadow correctly.
763    */
764   if (gtk_widget_get_mapped (widget) &&
765       viewport->shadow_type != GTK_SHADOW_NONE &&
766       (widget->allocation.width != allocation->width ||
767        widget->allocation.height != allocation->height))
768     gdk_window_invalidate_rect (widget->window, NULL, FALSE);
769   
770   widget->allocation = *allocation;
771   
772   viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
773   viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
774   
775   child_allocation.x = 0;
776   child_allocation.y = 0;
777   child_allocation.width = hadjustment->upper;
778   child_allocation.height = vadjustment->upper;
779   if (GTK_WIDGET_REALIZED (widget))
780     {
781       GtkAllocation view_allocation;
782       gdk_window_move_resize (widget->window,
783                               allocation->x + border_width,
784                               allocation->y + border_width,
785                               allocation->width - border_width * 2,
786                               allocation->height - border_width * 2);
787       
788       viewport_get_view_allocation (viewport, &view_allocation);
789       gdk_window_move_resize (viewport->view_window,
790                               view_allocation.x,
791                               view_allocation.y,
792                               view_allocation.width,
793                               view_allocation.height);
794       gdk_window_move_resize (viewport->bin_window,
795                               - hadjustment->value,
796                               - vadjustment->value,
797                               child_allocation.width,
798                               child_allocation.height);
799     }
800   if (bin->child && gtk_widget_get_visible (bin->child))
801     gtk_widget_size_allocate (bin->child, &child_allocation);
802
803   gtk_adjustment_changed (hadjustment);
804   gtk_adjustment_changed (vadjustment);
805   if (hadjustment_value_changed)
806     gtk_adjustment_value_changed (hadjustment);
807   if (vadjustment_value_changed)
808     gtk_adjustment_value_changed (vadjustment);
809 }
810
811 static void
812 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
813                                        gpointer       data)
814 {
815   GtkViewport *viewport = GTK_VIEWPORT (data);
816   GtkBin *bin = GTK_BIN (data);
817
818   if (bin->child && gtk_widget_get_visible (bin->child)  &&
819       GTK_WIDGET_REALIZED (viewport))
820     {
821       GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
822       GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
823       gint old_x, old_y;
824       gint new_x, new_y;
825
826       gdk_window_get_position (viewport->bin_window, &old_x, &old_y);
827       new_x = - hadjustment->value;
828       new_y = - vadjustment->value;
829
830       if (new_x != old_x || new_y != old_y)
831         {
832           gdk_window_move (viewport->bin_window, new_x, new_y);
833           gdk_window_process_updates (viewport->bin_window, TRUE);
834         }
835     }
836 }
837
838 static void
839 gtk_viewport_style_set (GtkWidget *widget,
840                         GtkStyle  *previous_style)
841 {
842    if (GTK_WIDGET_REALIZED (widget) &&
843        gtk_widget_get_has_window (widget))
844      {
845         GtkViewport *viewport = GTK_VIEWPORT (widget);
846
847         gtk_style_set_background (widget->style, viewport->bin_window, GTK_STATE_NORMAL);
848         gtk_style_set_background (widget->style, widget->window, widget->state);
849      }
850 }
851
852 #define __GTK_VIEWPORT_C__
853 #include "gtkaliasdef.c"