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