]> Pileus Git - ~andy/gtk/blob - gtk/gtkviewport.c
viewport: Don't paint stuff in realize handler
[~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 "gtksizerequest.h"
30 #include "gtkintl.h"
31 #include "gtkmarshalers.h"
32 #include "gtkprivate.h"
33
34
35 /**
36  * SECTION:gtkviewport
37  * @Short_description: An adapter which makes widgets scrollable
38  * @Title: GtkViewport
39  * @See_also:#GtkScrolledWindow, #GtkAdjustment
40  *
41  * The #GtkViewport widget acts as an adaptor class, implementing
42  * scrollability for child widgets that lack their own scrolling
43  * capabilities. Use #GtkViewport to scroll child widgets such as
44  * #GtkTable, #GtkBox, and so on.
45  *
46  * If a widget has native scrolling abilities, such as #GtkTextView,
47  * #GtkTreeView or #GtkIconview, it can be added to a #GtkScrolledWindow
48  * with gtk_container_add(). If a widget does not, you must first add the
49  * widget to a #GtkViewport, then add the viewport to the scrolled window.
50  * The convenience function gtk_scrolled_window_add_with_viewport() does
51  * exactly this, so you can ignore the presence of the viewport.
52  *
53  * The #GtkViewport will start scrolling content only if allocated less
54  * than the child widget's minimum size in a given orientation.
55  */
56
57 struct _GtkViewportPrivate
58 {
59   GtkAdjustment  *hadjustment;
60   GtkAdjustment  *vadjustment;
61   GtkShadowType   shadow_type;
62
63   GdkWindow      *bin_window;
64   GdkWindow      *view_window;
65 };
66
67 enum {
68   PROP_0,
69   PROP_HADJUSTMENT,
70   PROP_VADJUSTMENT,
71   PROP_SHADOW_TYPE
72 };
73
74
75 static void gtk_viewport_finalize                 (GObject          *object);
76 static void gtk_viewport_destroy                  (GtkObject        *object);
77 static void gtk_viewport_set_property             (GObject         *object,
78                                                    guint            prop_id,
79                                                    const GValue    *value,
80                                                    GParamSpec      *pspec);
81 static void gtk_viewport_get_property             (GObject         *object,
82                                                    guint            prop_id,
83                                                    GValue          *value,
84                                                    GParamSpec      *pspec);
85 static void gtk_viewport_set_scroll_adjustments   (GtkViewport      *viewport,
86                                                    GtkAdjustment    *hadjustment,
87                                                    GtkAdjustment    *vadjustment);
88 static void gtk_viewport_realize                  (GtkWidget        *widget);
89 static void gtk_viewport_unrealize                (GtkWidget        *widget);
90 static void gtk_viewport_paint                    (GtkWidget        *widget,
91                                                    GdkRectangle     *area);
92 static gint gtk_viewport_expose                   (GtkWidget        *widget,
93                                                    GdkEventExpose   *event);
94 static void gtk_viewport_add                      (GtkContainer     *container,
95                                                    GtkWidget        *widget);
96 static void gtk_viewport_size_allocate            (GtkWidget        *widget,
97                                                    GtkAllocation    *allocation);
98 static void gtk_viewport_adjustment_value_changed (GtkAdjustment    *adjustment,
99                                                    gpointer          data);
100 static void gtk_viewport_style_set                (GtkWidget *widget,
101                                                    GtkStyle  *previous_style);
102
103 static void gtk_viewport_size_request_init        (GtkSizeRequestIface *iface);
104 static void gtk_viewport_get_width                (GtkSizeRequest       *widget,
105                                                    gint                 *minimum_size,
106                                                    gint                 *natural_size);
107 static void gtk_viewport_get_height               (GtkSizeRequest       *widget,
108                                                    gint                 *minimum_size,
109                                                    gint                 *natural_size);
110
111
112 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
113                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
114                                                 gtk_viewport_size_request_init))
115
116 static void
117 gtk_viewport_class_init (GtkViewportClass *class)
118 {
119   GtkObjectClass *object_class;
120   GObjectClass   *gobject_class;
121   GtkWidgetClass *widget_class;
122   GtkContainerClass *container_class;
123
124   object_class = (GtkObjectClass*) class;
125   gobject_class = G_OBJECT_CLASS (class);
126   widget_class = (GtkWidgetClass*) class;
127   container_class = (GtkContainerClass*) class;
128
129   gobject_class->finalize = gtk_viewport_finalize;
130   gobject_class->set_property = gtk_viewport_set_property;
131   gobject_class->get_property = gtk_viewport_get_property;
132   object_class->destroy = gtk_viewport_destroy;
133   
134   widget_class->realize = gtk_viewport_realize;
135   widget_class->unrealize = gtk_viewport_unrealize;
136   widget_class->expose_event = gtk_viewport_expose;
137   widget_class->size_allocate = gtk_viewport_size_allocate;
138   widget_class->style_set = gtk_viewport_style_set;
139   
140   container_class->add = gtk_viewport_add;
141
142   class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;
143
144   g_object_class_install_property (gobject_class,
145                                    PROP_HADJUSTMENT,
146                                    g_param_spec_object ("hadjustment",
147                                                         P_("Horizontal adjustment"),
148                                                         P_("The GtkAdjustment that determines the values of the horizontal position for this viewport"),
149                                                         GTK_TYPE_ADJUSTMENT,
150                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
151
152   g_object_class_install_property (gobject_class,
153                                    PROP_VADJUSTMENT,
154                                    g_param_spec_object ("vadjustment",
155                                                         P_("Vertical adjustment"),
156                                                         P_("The GtkAdjustment that determines the values of the vertical position for this viewport"),
157                                                         GTK_TYPE_ADJUSTMENT,
158                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
159
160   g_object_class_install_property (gobject_class,
161                                    PROP_SHADOW_TYPE,
162                                    g_param_spec_enum ("shadow-type",
163                                                       P_("Shadow type"),
164                                                       P_("Determines how the shadowed box around the viewport is drawn"),
165                                                       GTK_TYPE_SHADOW_TYPE,
166                                                       GTK_SHADOW_IN,
167                                                       GTK_PARAM_READWRITE));
168
169   /**
170    * GtkViewport::set-scroll-adjustments
171    * @horizontal: the horizontal #GtkAdjustment
172    * @vertical: the vertical #GtkAdjustment
173    *
174    * Set the scroll adjustments for the viewport. Usually scrolled containers
175    * like #GtkScrolledWindow will emit this signal to connect two instances
176    * of #GtkScrollbar to the scroll directions of the #GtkViewport.
177    */
178   widget_class->set_scroll_adjustments_signal =
179     g_signal_new (I_("set-scroll-adjustments"),
180                   G_OBJECT_CLASS_TYPE (gobject_class),
181                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
182                   G_STRUCT_OFFSET (GtkViewportClass, set_scroll_adjustments),
183                   NULL, NULL,
184                   _gtk_marshal_VOID__OBJECT_OBJECT,
185                   G_TYPE_NONE, 2,
186                   GTK_TYPE_ADJUSTMENT,
187                   GTK_TYPE_ADJUSTMENT);
188
189   g_type_class_add_private (class, sizeof (GtkViewportPrivate));
190 }
191
192 static void
193 gtk_viewport_set_property (GObject         *object,
194                            guint            prop_id,
195                            const GValue    *value,
196                            GParamSpec      *pspec)
197 {
198   GtkViewport *viewport;
199
200   viewport = GTK_VIEWPORT (object);
201
202   switch (prop_id)
203     {
204     case PROP_HADJUSTMENT:
205       gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
206       break;
207     case PROP_VADJUSTMENT:
208       gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
209       break;
210     case PROP_SHADOW_TYPE:
211       gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
212       break;
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216     }
217 }
218
219 static void
220 gtk_viewport_get_property (GObject         *object,
221                            guint            prop_id,
222                            GValue          *value,
223                            GParamSpec      *pspec)
224 {
225   GtkViewport *viewport = GTK_VIEWPORT (object);
226   GtkViewportPrivate *priv = viewport->priv;
227
228   switch (prop_id)
229     {
230     case PROP_HADJUSTMENT:
231       g_value_set_object (value, priv->hadjustment);
232       break;
233     case PROP_VADJUSTMENT:
234       g_value_set_object (value, priv->vadjustment);
235       break;
236     case PROP_SHADOW_TYPE:
237       g_value_set_enum (value, priv->shadow_type);
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242     }
243 }
244
245 static void
246 gtk_viewport_init (GtkViewport *viewport)
247 {
248   GtkViewportPrivate *priv;
249
250   viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
251                                                 GTK_TYPE_VIEWPORT,
252                                                 GtkViewportPrivate);
253   priv = viewport->priv;
254
255   gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
256
257   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
258   gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
259
260   priv->shadow_type = GTK_SHADOW_IN;
261   priv->view_window = NULL;
262   priv->bin_window = NULL;
263   priv->hadjustment = NULL;
264   priv->vadjustment = NULL;
265 }
266
267 /**
268  * gtk_viewport_new:
269  * @hadjustment: horizontal adjustment.
270  * @vadjustment: vertical adjustment.
271  * @returns: a new #GtkViewport.
272  *
273  * Creates a new #GtkViewport with the given adjustments.
274  *
275  **/
276 GtkWidget*
277 gtk_viewport_new (GtkAdjustment *hadjustment,
278                   GtkAdjustment *vadjustment)
279 {
280   GtkWidget *viewport;
281
282   viewport = g_object_new (GTK_TYPE_VIEWPORT,
283                              "hadjustment", hadjustment,
284                              "vadjustment", vadjustment,
285                              NULL);
286
287   return viewport;
288 }
289
290 #define ADJUSTMENT_POINTER(viewport, orientation)         \
291   (((orientation) == GTK_ORIENTATION_HORIZONTAL) ?        \
292      &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
293
294 static void
295 viewport_disconnect_adjustment (GtkViewport    *viewport,
296                                 GtkOrientation  orientation)
297 {
298   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
299
300   if (*adjustmentp)
301     {
302       g_signal_handlers_disconnect_by_func (*adjustmentp,
303                                             gtk_viewport_adjustment_value_changed,
304                                             viewport);
305       g_object_unref (*adjustmentp);
306       *adjustmentp = NULL;
307     }
308 }
309
310 static void
311 gtk_viewport_finalize (GObject *object)
312 {
313   GtkViewport *viewport = GTK_VIEWPORT (object);
314
315   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
316   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
317
318   G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
319 }
320
321 static void
322 gtk_viewport_destroy (GtkObject *object)
323 {
324   GtkViewport *viewport = GTK_VIEWPORT (object);
325
326   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
327   viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
328
329   GTK_OBJECT_CLASS (gtk_viewport_parent_class)->destroy (object);
330 }
331
332 /**
333  * gtk_viewport_get_hadjustment:
334  * @viewport: a #GtkViewport.
335  *
336  * Returns the horizontal adjustment of the viewport.
337  *
338  * Return value: (transfer none): the horizontal adjustment of @viewport.
339  **/
340 GtkAdjustment*
341 gtk_viewport_get_hadjustment (GtkViewport *viewport)
342 {
343   GtkViewportPrivate *priv;
344
345   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
346
347   priv = viewport->priv;
348
349   if (!priv->hadjustment)
350     gtk_viewport_set_hadjustment (viewport, NULL);
351
352   return priv->hadjustment;
353 }
354
355 /**
356  * gtk_viewport_get_vadjustment:
357  * @viewport: a #GtkViewport.
358  * 
359  * Returns the vertical adjustment of the viewport.
360  *
361  * Return value: (transfer none): the vertical adjustment of @viewport.
362  **/
363 GtkAdjustment*
364 gtk_viewport_get_vadjustment (GtkViewport *viewport)
365 {
366   GtkViewportPrivate *priv;
367
368   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
369
370   priv = viewport->priv;
371
372   if (!priv->vadjustment)
373     gtk_viewport_set_vadjustment (viewport, NULL);
374
375   return priv->vadjustment;
376 }
377
378 static void
379 viewport_get_view_allocation (GtkViewport   *viewport,
380                               GtkAllocation *view_allocation)
381 {
382   GtkViewportPrivate *priv = viewport->priv;
383   GtkStyle *style;
384   GtkWidget *widget = GTK_WIDGET (viewport);
385   GtkAllocation allocation;
386   guint border_width;
387
388   gtk_widget_get_allocation (widget, &allocation);
389   border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
390
391   view_allocation->x = 0;
392   view_allocation->y = 0;
393
394   if (priv->shadow_type != GTK_SHADOW_NONE)
395     {
396       style = gtk_widget_get_style (widget);
397       view_allocation->x = style->xthickness;
398       view_allocation->y = style->ythickness;
399     }
400
401   view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
402   view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
403 }
404
405 static void
406 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
407                              gboolean      *value_changed)
408 {
409   gdouble value = adjustment->value;
410   
411   value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
412   if (value != adjustment->value)
413     {
414       adjustment->value = value;
415       if (value_changed)
416         *value_changed = TRUE;
417     }
418   else if (value_changed)
419     *value_changed = FALSE;
420 }
421
422 static void
423 viewport_set_hadjustment_values (GtkViewport *viewport,
424                                  gboolean    *value_changed)
425 {
426   GtkBin *bin = GTK_BIN (viewport);
427   GtkAllocation view_allocation;
428   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
429   GtkWidget *child;
430   gdouble old_page_size;
431   gdouble old_upper;
432   gdouble old_value;
433   
434   viewport_get_view_allocation (viewport, &view_allocation);  
435
436   old_page_size = hadjustment->page_size;
437   old_upper = hadjustment->upper;
438   old_value = hadjustment->value;
439   hadjustment->page_size = view_allocation.width;
440   hadjustment->step_increment = view_allocation.width * 0.1;
441   hadjustment->page_increment = view_allocation.width * 0.9;
442   
443   hadjustment->lower = 0;
444
445   child = gtk_bin_get_child (bin);
446   if (child && gtk_widget_get_visible (child))
447     {
448       gint minimum_width;
449
450       gtk_size_request_get_width_for_height (GTK_SIZE_REQUEST (child),
451                                              view_allocation.height,
452                                              &minimum_width,
453                                              NULL);
454       hadjustment->upper = MAX (minimum_width, view_allocation.width);
455     }
456   else
457     hadjustment->upper = view_allocation.width;
458
459   if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL) 
460     {
461       gdouble dist = old_upper - (old_value + old_page_size);
462       hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
463       viewport_reclamp_adjustment (hadjustment, value_changed);
464       *value_changed = (old_value != hadjustment->value);
465     }
466   else
467     viewport_reclamp_adjustment (hadjustment, value_changed);
468 }
469
470 static void
471 viewport_set_vadjustment_values (GtkViewport *viewport,
472                                  gboolean    *value_changed)
473 {
474   GtkBin *bin = GTK_BIN (viewport);
475   GtkAllocation view_allocation;
476   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
477   GtkWidget *child;
478
479   viewport_get_view_allocation (viewport, &view_allocation);  
480
481   vadjustment->page_size = view_allocation.height;
482   vadjustment->step_increment = view_allocation.height * 0.1;
483   vadjustment->page_increment = view_allocation.height * 0.9;
484   
485   vadjustment->lower = 0;
486
487   child = gtk_bin_get_child (bin);
488   if (child && gtk_widget_get_visible (child))
489     {
490       gint minimum_height;
491
492       gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child),
493                                              view_allocation.width,
494                                              &minimum_height,
495                                              NULL);
496
497       vadjustment->upper = MAX (minimum_height, view_allocation.height);
498     }
499   else
500     vadjustment->upper = view_allocation.height;
501
502   viewport_reclamp_adjustment (vadjustment, value_changed);
503 }
504
505 static void
506 viewport_set_adjustment (GtkViewport    *viewport,
507                          GtkOrientation  orientation,
508                          GtkAdjustment  *adjustment)
509 {
510   GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
511   gboolean value_changed;
512
513   if (adjustment && adjustment == *adjustmentp)
514     return;
515
516   if (!adjustment)
517     adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
518                                                      0.0, 0.0, 0.0));
519   viewport_disconnect_adjustment (viewport, orientation);
520   *adjustmentp = adjustment;
521   g_object_ref_sink (adjustment);
522
523   if (orientation == GTK_ORIENTATION_HORIZONTAL)
524     viewport_set_hadjustment_values (viewport, &value_changed);
525   else
526     viewport_set_vadjustment_values (viewport, &value_changed);
527
528   g_signal_connect (adjustment, "value-changed",
529                     G_CALLBACK (gtk_viewport_adjustment_value_changed),
530                     viewport);
531
532   gtk_adjustment_changed (adjustment);
533   
534   if (value_changed)
535     gtk_adjustment_value_changed (adjustment);
536   else
537     gtk_viewport_adjustment_value_changed (adjustment, viewport);
538 }
539
540 /**
541  * gtk_viewport_set_hadjustment:
542  * @viewport: a #GtkViewport.
543  * @adjustment: (allow-none): a #GtkAdjustment.
544  *
545  * Sets the horizontal adjustment of the viewport.
546  **/
547 void
548 gtk_viewport_set_hadjustment (GtkViewport   *viewport,
549                               GtkAdjustment *adjustment)
550 {
551   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
552   if (adjustment)
553     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
554
555   viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
556
557   g_object_notify (G_OBJECT (viewport), "hadjustment");
558 }
559
560 /**
561  * gtk_viewport_set_vadjustment:
562  * @viewport: a #GtkViewport.
563  * @adjustment: (allow-none): a #GtkAdjustment.
564  *
565  * Sets the vertical adjustment of the viewport.
566  **/
567 void
568 gtk_viewport_set_vadjustment (GtkViewport   *viewport,
569                               GtkAdjustment *adjustment)
570 {
571   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
572   if (adjustment)
573     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
574
575   viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
576
577   g_object_notify (G_OBJECT (viewport), "vadjustment");
578 }
579
580 static void
581 gtk_viewport_set_scroll_adjustments (GtkViewport      *viewport,
582                                      GtkAdjustment    *hadjustment,
583                                      GtkAdjustment    *vadjustment)
584 {
585   gtk_viewport_set_hadjustment (viewport, hadjustment);
586   gtk_viewport_set_vadjustment (viewport, vadjustment);
587 }
588
589 /** 
590  * gtk_viewport_set_shadow_type:
591  * @viewport: a #GtkViewport.
592  * @type: the new shadow type.
593  *
594  * Sets the shadow type of the viewport.
595  **/ 
596 void
597 gtk_viewport_set_shadow_type (GtkViewport   *viewport,
598                               GtkShadowType  type)
599 {
600   GtkViewportPrivate *priv;
601   GtkAllocation allocation;
602   GtkWidget *widget;
603
604   g_return_if_fail (GTK_IS_VIEWPORT (viewport));
605
606   widget = GTK_WIDGET (viewport);
607   priv = viewport->priv;
608
609   if ((GtkShadowType) priv->shadow_type != type)
610     {
611       priv->shadow_type = type;
612
613       if (gtk_widget_get_visible (widget))
614         {
615           gtk_widget_get_allocation (widget, &allocation);
616           gtk_widget_size_allocate (widget, &allocation);
617           gtk_widget_set_allocation (widget, &allocation);
618           gtk_widget_queue_draw (widget);
619         }
620
621       g_object_notify (G_OBJECT (viewport), "shadow-type");
622     }
623 }
624
625 /**
626  * gtk_viewport_get_shadow_type:
627  * @viewport: a #GtkViewport
628  *
629  * Gets the shadow type of the #GtkViewport. See
630  * gtk_viewport_set_shadow_type().
631  *
632  * Return value: the shadow type 
633  **/
634 GtkShadowType
635 gtk_viewport_get_shadow_type (GtkViewport *viewport)
636 {
637   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
638
639   return viewport->priv->shadow_type;
640 }
641
642 /**
643  * gtk_viewport_get_bin_window:
644  * @viewport: a #GtkViewport
645  *
646  * Gets the bin window of the #GtkViewport.
647  *
648  * Return value: (transfer none): a #GdkWindow
649  *
650  * Since: 2.20
651  **/
652 GdkWindow*
653 gtk_viewport_get_bin_window (GtkViewport *viewport)
654 {
655   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
656
657   return viewport->priv->bin_window;
658 }
659
660 /**
661  * gtk_viewport_get_view_window:
662  * @viewport: a #GtkViewport
663  *
664  * Gets the view window of the #GtkViewport.
665  *
666  * Return value: (transfer none): a #GdkWindow
667  *
668  * Since: 2.22
669  **/
670 GdkWindow*
671 gtk_viewport_get_view_window (GtkViewport *viewport)
672 {
673   g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
674
675   return viewport->priv->view_window;
676 }
677
678 static void
679 gtk_viewport_realize (GtkWidget *widget)
680 {
681   GtkViewport *viewport = GTK_VIEWPORT (widget);
682   GtkViewportPrivate *priv = viewport->priv;
683   GtkBin *bin = GTK_BIN (widget);
684   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
685   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
686   GtkAllocation allocation;
687   GtkAllocation view_allocation;
688   GtkStyle *style;
689   GtkWidget *child;
690   GdkWindow *window;
691   GdkWindowAttr attributes;
692   gint attributes_mask;
693   gint event_mask;
694   guint border_width;
695
696   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
697
698   gtk_widget_set_realized (widget, TRUE);
699
700   gtk_widget_get_allocation (widget, &allocation);
701
702   attributes.x = allocation.x + border_width;
703   attributes.y = allocation.y + border_width;
704   attributes.width = allocation.width - border_width * 2;
705   attributes.height = allocation.height - border_width * 2;
706   attributes.window_type = GDK_WINDOW_CHILD;
707   attributes.wclass = GDK_INPUT_OUTPUT;
708   attributes.visual = gtk_widget_get_visual (widget);
709
710   event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
711   /* We select on button_press_mask so that button 4-5 scrolls are trapped.
712    */
713   attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
714
715   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
716
717   window = gdk_window_new (gtk_widget_get_parent_window (widget),
718                            &attributes, attributes_mask);
719   gtk_widget_set_window (widget, window);
720   gdk_window_set_user_data (window, viewport);
721
722   viewport_get_view_allocation (viewport, &view_allocation);
723   
724   attributes.x = view_allocation.x;
725   attributes.y = view_allocation.y;
726   attributes.width = view_allocation.width;
727   attributes.height = view_allocation.height;
728   attributes.event_mask = 0;
729
730   priv->view_window = gdk_window_new (window,
731                                       &attributes, attributes_mask);
732   gdk_window_set_user_data (priv->view_window, viewport);
733
734   attributes.x = - hadjustment->value;
735   attributes.y = - vadjustment->value;
736   attributes.width = hadjustment->upper;
737   attributes.height = vadjustment->upper;
738   
739   attributes.event_mask = event_mask;
740
741   priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
742   gdk_window_set_user_data (priv->bin_window, viewport);
743
744   child = gtk_bin_get_child (bin);
745   if (child)
746     gtk_widget_set_parent_window (child, priv->bin_window);
747
748   gtk_widget_style_attach (widget);
749   style = gtk_widget_get_style (widget);
750   gtk_style_set_background (style, window, GTK_STATE_NORMAL);
751   gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
752
753   gdk_window_show (priv->bin_window);
754   gdk_window_show (priv->view_window);
755 }
756
757 static void
758 gtk_viewport_unrealize (GtkWidget *widget)
759 {
760   GtkViewport *viewport = GTK_VIEWPORT (widget);
761   GtkViewportPrivate *priv = viewport->priv;
762
763   gdk_window_set_user_data (priv->view_window, NULL);
764   gdk_window_destroy (priv->view_window);
765   priv->view_window = NULL;
766
767   gdk_window_set_user_data (priv->bin_window, NULL);
768   gdk_window_destroy (priv->bin_window);
769   priv->bin_window = NULL;
770
771   GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
772 }
773
774 static void
775 gtk_viewport_paint (GtkWidget    *widget,
776                     GdkRectangle *area)
777 {
778   if (gtk_widget_is_drawable (widget))
779     {
780       GtkViewport *viewport = GTK_VIEWPORT (widget);
781       GtkViewportPrivate *priv = viewport->priv;
782
783       gtk_paint_shadow (gtk_widget_get_style (widget),
784                         gtk_widget_get_window (widget),
785                         GTK_STATE_NORMAL, priv->shadow_type,
786                         area, widget, "viewport",
787                         0, 0, -1, -1);
788     }
789 }
790
791 static gint
792 gtk_viewport_expose (GtkWidget      *widget,
793                      GdkEventExpose *event)
794 {
795   GtkViewport *viewport;
796
797   if (gtk_widget_is_drawable (widget))
798     {
799       viewport = GTK_VIEWPORT (widget);
800       GtkViewportPrivate *priv = viewport->priv;
801
802       if (event->window == gtk_widget_get_window (widget))
803         gtk_viewport_paint (widget, &event->area);
804       else if (event->window == priv->bin_window)
805         {
806           gtk_paint_flat_box(gtk_widget_get_style (widget), priv->bin_window,
807                              GTK_STATE_NORMAL, GTK_SHADOW_NONE,
808                              &event->area, widget, "viewportbin",
809                              0, 0, -1, -1);
810
811           GTK_WIDGET_CLASS (gtk_viewport_parent_class)->expose_event (widget, event);
812         }
813     }
814
815   return FALSE;
816 }
817
818 static void
819 gtk_viewport_add (GtkContainer *container,
820                   GtkWidget    *child)
821 {
822   GtkBin *bin = GTK_BIN (container);
823   GtkViewport *viewport = GTK_VIEWPORT (bin);
824   GtkViewportPrivate *priv = viewport->priv;
825
826   g_return_if_fail (gtk_bin_get_child (bin) == NULL);
827
828   gtk_widget_set_parent_window (child, priv->bin_window);
829
830   GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
831 }
832
833 static void
834 gtk_viewport_size_allocate (GtkWidget     *widget,
835                             GtkAllocation *allocation)
836 {
837   GtkAllocation widget_allocation;
838   GtkViewport *viewport = GTK_VIEWPORT (widget);
839   GtkViewportPrivate *priv = viewport->priv;
840   GtkBin *bin = GTK_BIN (widget);
841   guint border_width;
842   gboolean hadjustment_value_changed, vadjustment_value_changed;
843   GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
844   GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
845   GtkAllocation child_allocation;
846   GtkWidget *child;
847
848   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
849
850   /* If our size changed, and we have a shadow, queue a redraw on widget->window to
851    * redraw the shadow correctly.
852    */
853   gtk_widget_get_allocation (widget, &widget_allocation);
854   if (gtk_widget_get_mapped (widget) &&
855       priv->shadow_type != GTK_SHADOW_NONE &&
856       (widget_allocation.width != allocation->width ||
857        widget_allocation.height != allocation->height))
858     gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
859
860   gtk_widget_set_allocation (widget, allocation);
861
862   viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
863   viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
864   
865   child_allocation.x = 0;
866   child_allocation.y = 0;
867   child_allocation.width = hadjustment->upper;
868   child_allocation.height = vadjustment->upper;
869   if (gtk_widget_get_realized (widget))
870     {
871       GtkAllocation view_allocation;
872
873       gdk_window_move_resize (gtk_widget_get_window (widget),
874                               allocation->x + border_width,
875                               allocation->y + border_width,
876                               allocation->width - border_width * 2,
877                               allocation->height - border_width * 2);
878       
879       viewport_get_view_allocation (viewport, &view_allocation);
880       gdk_window_move_resize (priv->view_window,
881                               view_allocation.x,
882                               view_allocation.y,
883                               view_allocation.width,
884                               view_allocation.height);
885       gdk_window_move_resize (priv->bin_window,
886                               - hadjustment->value,
887                               - vadjustment->value,
888                               child_allocation.width,
889                               child_allocation.height);
890     }
891
892   child = gtk_bin_get_child (bin);
893   if (child && gtk_widget_get_visible (child))
894     gtk_widget_size_allocate (child, &child_allocation);
895
896   gtk_adjustment_changed (hadjustment);
897   gtk_adjustment_changed (vadjustment);
898   if (hadjustment_value_changed)
899     gtk_adjustment_value_changed (hadjustment);
900   if (vadjustment_value_changed)
901     gtk_adjustment_value_changed (vadjustment);
902 }
903
904 static void
905 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
906                                        gpointer       data)
907 {
908   GtkViewport *viewport = GTK_VIEWPORT (data);
909   GtkViewportPrivate *priv = viewport->priv;
910   GtkBin *bin = GTK_BIN (data);
911   GtkWidget *child;
912
913   child = gtk_bin_get_child (bin);
914   if (child && gtk_widget_get_visible (child) &&
915       gtk_widget_get_realized (GTK_WIDGET (viewport)))
916     {
917       GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
918       GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
919       gint old_x, old_y;
920       gint new_x, new_y;
921
922       gdk_window_get_position (priv->bin_window, &old_x, &old_y);
923       new_x = - hadjustment->value;
924       new_y = - vadjustment->value;
925
926       if (new_x != old_x || new_y != old_y)
927         {
928           gdk_window_move (priv->bin_window, new_x, new_y);
929           gdk_window_process_updates (priv->bin_window, TRUE);
930         }
931     }
932 }
933
934 static void
935 gtk_viewport_style_set (GtkWidget *widget,
936                         GtkStyle  *previous_style)
937 {
938    if (gtk_widget_get_realized (widget) &&
939        gtk_widget_get_has_window (widget))
940      {
941         GtkStyle *style;
942         GtkViewport *viewport = GTK_VIEWPORT (widget);
943         GtkViewportPrivate *priv = viewport->priv;
944
945         style = gtk_widget_get_style (widget);
946         gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
947         gtk_style_set_background (style,
948                                   gtk_widget_get_window (widget),
949                                   gtk_widget_get_state (widget));
950      }
951 }
952
953
954 static void
955 gtk_viewport_size_request_init (GtkSizeRequestIface *iface)
956 {
957   iface->get_width  = gtk_viewport_get_width;
958   iface->get_height = gtk_viewport_get_height;
959 }
960
961 static void
962 gtk_viewport_get_size (GtkSizeRequest *widget,
963                        GtkOrientation  orientation,
964                        gint           *minimum_size,
965                        gint           *natural_size)
966 {
967   GtkViewport *viewport = GTK_VIEWPORT (widget);
968   GtkViewportPrivate *priv = viewport->priv;
969   GtkStyle  *style;
970   GtkWidget *child;
971   gint       child_min, child_nat;
972   gint       minimum, natural;
973
974   child = gtk_bin_get_child (GTK_BIN (widget));
975
976   /* XXX This should probably be (border_width * 2); but GTK+ has
977    * been doing this with a single border for a while now...
978    */
979   minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
980
981   if (priv->shadow_type != GTK_SHADOW_NONE)
982     {
983       style = gtk_widget_get_style (GTK_WIDGET (widget));
984       if (orientation == GTK_ORIENTATION_HORIZONTAL)
985           minimum += 2 * style->xthickness;
986       else
987           minimum += 2 * style->ythickness;
988     }
989
990   natural = minimum;
991
992   if (child && gtk_widget_get_visible (child))
993     {
994       if (orientation == GTK_ORIENTATION_HORIZONTAL)
995         gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
996       else
997         gtk_size_request_get_height (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
998
999       minimum += child_min;
1000       natural += child_nat;
1001     }
1002
1003   if (minimum_size)
1004     *minimum_size = minimum;
1005
1006   if (natural_size)
1007     *natural_size = natural;
1008 }
1009
1010 static void
1011 gtk_viewport_get_width (GtkSizeRequest *widget,
1012                         gint           *minimum_size,
1013                         gint           *natural_size)
1014 {
1015   gtk_viewport_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1016 }
1017
1018 static void
1019 gtk_viewport_get_height (GtkSizeRequest *widget,
1020                          gint           *minimum_size,
1021                          gint           *natural_size)
1022 {
1023   gtk_viewport_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1024 }