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