1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtkviewport.h"
29 #include "gtkadjustment.h"
31 #include "gtkmarshalers.h"
32 #include "gtkprivate.h"
33 #include "gtkscrollable.h"
34 #include "gtktypebuiltins.h"
39 * @Short_description: An adapter which makes widgets scrollable
41 * @See_also:#GtkScrolledWindow, #GtkAdjustment
43 * The #GtkViewport widget acts as an adaptor class, implementing
44 * scrollability for child widgets that lack their own scrolling
45 * capabilities. Use #GtkViewport to scroll child widgets such as
46 * #GtkGrid, #GtkBox, and so on.
48 * If a widget has native scrolling abilities, such as #GtkTextView,
49 * #GtkTreeView or #GtkIconView, it can be added to a #GtkScrolledWindow
50 * with gtk_container_add(). If a widget does not, you must first add the
51 * widget to a #GtkViewport, then add the viewport to the scrolled window.
52 * gtk_container_add() does this automatically if a child that does not
53 * implement #GtkScrollable is added to a #GtkScrolledWindow, so you can
54 * ignore the presence of the viewport.
56 * The #GtkViewport will start scrolling content only if allocated less
57 * than the child widget's minimum size in a given orientation.
60 struct _GtkViewportPrivate
62 GtkAdjustment *hadjustment;
63 GtkAdjustment *vadjustment;
64 GtkShadowType shadow_type;
66 GdkWindow *bin_window;
67 GdkWindow *view_window;
69 /* GtkScrollablePolicy needs to be checked when
70 * driving the scrollable adjustment values */
71 guint hscroll_policy : 1;
72 guint vscroll_policy : 1;
85 static void gtk_viewport_set_property (GObject *object,
89 static void gtk_viewport_get_property (GObject *object,
93 static void gtk_viewport_destroy (GtkWidget *widget);
94 static void gtk_viewport_realize (GtkWidget *widget);
95 static void gtk_viewport_unrealize (GtkWidget *widget);
96 static gint gtk_viewport_draw (GtkWidget *widget,
98 static void gtk_viewport_add (GtkContainer *container,
100 static void gtk_viewport_size_allocate (GtkWidget *widget,
101 GtkAllocation *allocation);
102 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
104 static void gtk_viewport_style_updated (GtkWidget *widget);
106 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
109 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
113 static void viewport_set_adjustment (GtkViewport *viewport,
114 GtkOrientation orientation,
115 GtkAdjustment *adjustment);
117 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
118 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
121 gtk_viewport_class_init (GtkViewportClass *class)
123 GObjectClass *gobject_class;
124 GtkWidgetClass *widget_class;
125 GtkContainerClass *container_class;
127 gobject_class = G_OBJECT_CLASS (class);
128 widget_class = (GtkWidgetClass*) class;
129 container_class = (GtkContainerClass*) class;
131 gobject_class->set_property = gtk_viewport_set_property;
132 gobject_class->get_property = gtk_viewport_get_property;
134 widget_class->destroy = gtk_viewport_destroy;
135 widget_class->realize = gtk_viewport_realize;
136 widget_class->unrealize = gtk_viewport_unrealize;
137 widget_class->draw = gtk_viewport_draw;
138 widget_class->size_allocate = gtk_viewport_size_allocate;
139 widget_class->style_updated = gtk_viewport_style_updated;
140 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
141 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
143 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_VIEWPORT);
145 container_class->add = gtk_viewport_add;
147 /* GtkScrollable implementation */
148 g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment");
149 g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment");
150 g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
151 g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
153 g_object_class_install_property (gobject_class,
155 g_param_spec_enum ("shadow-type",
157 P_("Determines how the shadowed box around the viewport is drawn"),
158 GTK_TYPE_SHADOW_TYPE,
160 GTK_PARAM_READWRITE));
162 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
166 gtk_viewport_set_property (GObject *object,
171 GtkViewport *viewport;
173 viewport = GTK_VIEWPORT (object);
177 case PROP_HADJUSTMENT:
178 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, g_value_get_object (value));
180 case PROP_VADJUSTMENT:
181 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, g_value_get_object (value));
183 case PROP_HSCROLL_POLICY:
184 viewport->priv->hscroll_policy = g_value_get_enum (value);
185 gtk_widget_queue_resize (GTK_WIDGET (viewport));
187 case PROP_VSCROLL_POLICY:
188 viewport->priv->vscroll_policy = g_value_get_enum (value);
189 gtk_widget_queue_resize (GTK_WIDGET (viewport));
191 case PROP_SHADOW_TYPE:
192 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 gtk_viewport_get_property (GObject *object,
206 GtkViewport *viewport = GTK_VIEWPORT (object);
207 GtkViewportPrivate *priv = viewport->priv;
211 case PROP_HADJUSTMENT:
212 g_value_set_object (value, priv->hadjustment);
214 case PROP_VADJUSTMENT:
215 g_value_set_object (value, priv->vadjustment);
217 case PROP_HSCROLL_POLICY:
218 g_value_set_enum (value, priv->hscroll_policy);
220 case PROP_VSCROLL_POLICY:
221 g_value_set_enum (value, priv->vscroll_policy);
223 case PROP_SHADOW_TYPE:
224 g_value_set_enum (value, priv->shadow_type);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 gtk_viewport_init (GtkViewport *viewport)
235 GtkViewportPrivate *priv;
237 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
240 priv = viewport->priv;
242 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
244 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
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;
252 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, NULL);
253 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, NULL);
258 * @hadjustment: (allow-none): horizontal adjustment
259 * @vadjustment: (allow-none): vertical adjustment
261 * Creates a new #GtkViewport with the given adjustments, or with default
262 * adjustments if none are given.
264 * Returns: a new #GtkViewport
267 gtk_viewport_new (GtkAdjustment *hadjustment,
268 GtkAdjustment *vadjustment)
272 viewport = g_object_new (GTK_TYPE_VIEWPORT,
273 "hadjustment", hadjustment,
274 "vadjustment", vadjustment,
280 #define ADJUSTMENT_POINTER(viewport, orientation) \
281 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
282 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
285 viewport_disconnect_adjustment (GtkViewport *viewport,
286 GtkOrientation orientation)
288 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
292 g_signal_handlers_disconnect_by_func (*adjustmentp,
293 gtk_viewport_adjustment_value_changed,
295 g_object_unref (*adjustmentp);
301 gtk_viewport_destroy (GtkWidget *widget)
303 GtkViewport *viewport = GTK_VIEWPORT (widget);
305 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
306 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
308 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
312 viewport_get_view_allocation (GtkViewport *viewport,
313 GtkAllocation *view_allocation)
315 GtkViewportPrivate *priv = viewport->priv;
316 GtkWidget *widget = GTK_WIDGET (viewport);
317 GtkAllocation allocation;
318 GtkStyleContext *context;
320 GtkBorder padding, border;
323 gtk_widget_get_allocation (widget, &allocation);
324 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
326 view_allocation->x = 0;
327 view_allocation->y = 0;
329 context = gtk_widget_get_style_context (widget);
330 state = gtk_widget_get_state_flags (widget);
331 gtk_style_context_save (context);
332 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
334 gtk_style_context_get_padding (context, state, &padding);
335 gtk_style_context_get_border (context, state, &border);
337 gtk_style_context_restore (context);
339 if (priv->shadow_type != GTK_SHADOW_NONE)
341 view_allocation->x = border.left;
342 view_allocation->y = border.top;
345 view_allocation->x += padding.left;
346 view_allocation->y += padding.top;
347 view_allocation->width = MAX (1, allocation.width - padding.left - padding.right - border_width * 2);
348 view_allocation->height = MAX (1, allocation.height - padding.top - padding.bottom - border_width * 2);
350 if (priv->shadow_type != GTK_SHADOW_NONE)
352 view_allocation->width = MAX (1, view_allocation->width - border.left - border.right);
353 view_allocation->height = MAX (1, view_allocation->height - border.top - border.bottom);
358 * gtk_viewport_get_hadjustment:
359 * @viewport: a #GtkViewport.
361 * Returns the horizontal adjustment of the viewport.
363 * Return value: (transfer none): the horizontal adjustment of @viewport.
365 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
368 gtk_viewport_get_hadjustment (GtkViewport *viewport)
370 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
372 return viewport->priv->hadjustment;
376 * gtk_viewport_get_vadjustment:
377 * @viewport: a #GtkViewport.
379 * Returns the vertical adjustment of the viewport.
381 * Return value: (transfer none): the vertical adjustment of @viewport.
383 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
386 gtk_viewport_get_vadjustment (GtkViewport *viewport)
388 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
390 return viewport->priv->vadjustment;
394 viewport_set_hadjustment_values (GtkViewport *viewport)
396 GtkBin *bin = GTK_BIN (viewport);
397 GtkAllocation view_allocation;
398 GtkAdjustment *hadjustment = viewport->priv->hadjustment;
400 gdouble upper, value;
402 viewport_get_view_allocation (viewport, &view_allocation);
404 child = gtk_bin_get_child (bin);
405 if (child && gtk_widget_get_visible (child))
407 gint minimum_width, natural_width;
410 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
411 gtk_widget_get_preferred_height (child, &scroll_height, NULL);
413 gtk_widget_get_preferred_height (child, NULL, &scroll_height);
415 gtk_widget_get_preferred_width_for_height (child,
416 MAX (view_allocation.height, scroll_height),
420 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
421 upper = MAX (minimum_width, view_allocation.width);
423 upper = MAX (natural_width, view_allocation.width);
426 upper = view_allocation.width;
428 value = gtk_adjustment_get_value (hadjustment);
429 /* We clamp to the left in RTL mode */
430 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
432 gdouble dist = gtk_adjustment_get_upper (hadjustment)
434 - gtk_adjustment_get_page_size (hadjustment);
435 value = upper - dist - view_allocation.width;
438 gtk_adjustment_configure (hadjustment,
442 view_allocation.width * 0.1,
443 view_allocation.width * 0.9,
444 view_allocation.width);
448 viewport_set_vadjustment_values (GtkViewport *viewport)
450 GtkBin *bin = GTK_BIN (viewport);
451 GtkAllocation view_allocation;
452 GtkAdjustment *vadjustment = viewport->priv->vadjustment;
456 viewport_get_view_allocation (viewport, &view_allocation);
458 child = gtk_bin_get_child (bin);
459 if (child && gtk_widget_get_visible (child))
461 gint minimum_height, natural_height;
464 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
465 gtk_widget_get_preferred_width (child, &scroll_width, NULL);
467 gtk_widget_get_preferred_width (child, NULL, &scroll_width);
469 gtk_widget_get_preferred_height_for_width (child,
470 MAX (view_allocation.width, scroll_width),
474 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
475 upper = MAX (minimum_height, view_allocation.height);
477 upper = MAX (natural_height, view_allocation.height);
480 upper = view_allocation.height;
482 gtk_adjustment_configure (vadjustment,
483 gtk_adjustment_get_value (vadjustment),
486 view_allocation.height * 0.1,
487 view_allocation.height * 0.9,
488 view_allocation.height);
492 viewport_set_adjustment (GtkViewport *viewport,
493 GtkOrientation orientation,
494 GtkAdjustment *adjustment)
496 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
498 if (adjustment && adjustment == *adjustmentp)
502 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
503 viewport_disconnect_adjustment (viewport, orientation);
504 *adjustmentp = adjustment;
505 g_object_ref_sink (adjustment);
507 if (orientation == GTK_ORIENTATION_HORIZONTAL)
508 viewport_set_hadjustment_values (viewport);
510 viewport_set_vadjustment_values (viewport);
512 g_signal_connect (adjustment, "value-changed",
513 G_CALLBACK (gtk_viewport_adjustment_value_changed),
516 gtk_viewport_adjustment_value_changed (adjustment, viewport);
520 * gtk_viewport_set_hadjustment:
521 * @viewport: a #GtkViewport.
522 * @adjustment: (allow-none): a #GtkAdjustment.
524 * Sets the horizontal adjustment of the viewport.
526 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
529 gtk_viewport_set_hadjustment (GtkViewport *viewport,
530 GtkAdjustment *adjustment)
532 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
534 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
536 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
538 g_object_notify (G_OBJECT (viewport), "hadjustment");
542 * gtk_viewport_set_vadjustment:
543 * @viewport: a #GtkViewport.
544 * @adjustment: (allow-none): a #GtkAdjustment.
546 * Sets the vertical adjustment of the viewport.
548 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
551 gtk_viewport_set_vadjustment (GtkViewport *viewport,
552 GtkAdjustment *adjustment)
554 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
556 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
558 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
560 g_object_notify (G_OBJECT (viewport), "vadjustment");
564 * gtk_viewport_set_shadow_type:
565 * @viewport: a #GtkViewport.
566 * @type: the new shadow type.
568 * Sets the shadow type of the viewport.
571 gtk_viewport_set_shadow_type (GtkViewport *viewport,
574 GtkViewportPrivate *priv;
575 GtkAllocation allocation;
578 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
580 widget = GTK_WIDGET (viewport);
581 priv = viewport->priv;
583 if ((GtkShadowType) priv->shadow_type != type)
585 priv->shadow_type = type;
587 if (gtk_widget_get_visible (widget))
589 gtk_widget_get_allocation (widget, &allocation);
590 gtk_widget_size_allocate (widget, &allocation);
591 gtk_widget_set_allocation (widget, &allocation);
592 gtk_widget_queue_draw (widget);
595 g_object_notify (G_OBJECT (viewport), "shadow-type");
600 * gtk_viewport_get_shadow_type:
601 * @viewport: a #GtkViewport
603 * Gets the shadow type of the #GtkViewport. See
604 * gtk_viewport_set_shadow_type().
606 * Return value: the shadow type
609 gtk_viewport_get_shadow_type (GtkViewport *viewport)
611 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
613 return viewport->priv->shadow_type;
617 * gtk_viewport_get_bin_window:
618 * @viewport: a #GtkViewport
620 * Gets the bin window of the #GtkViewport.
622 * Return value: (transfer none): a #GdkWindow
627 gtk_viewport_get_bin_window (GtkViewport *viewport)
629 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
631 return viewport->priv->bin_window;
635 * gtk_viewport_get_view_window:
636 * @viewport: a #GtkViewport
638 * Gets the view window of the #GtkViewport.
640 * Return value: (transfer none): a #GdkWindow
645 gtk_viewport_get_view_window (GtkViewport *viewport)
647 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
649 return viewport->priv->view_window;
653 gtk_viewport_realize (GtkWidget *widget)
655 GtkViewport *viewport = GTK_VIEWPORT (widget);
656 GtkViewportPrivate *priv = viewport->priv;
657 GtkBin *bin = GTK_BIN (widget);
658 GtkAdjustment *hadjustment = priv->hadjustment;
659 GtkAdjustment *vadjustment = priv->vadjustment;
660 GtkAllocation allocation;
661 GtkAllocation view_allocation;
662 GtkStyleContext *context;
665 GdkWindowAttr attributes;
666 gint attributes_mask;
670 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
672 gtk_widget_set_realized (widget, TRUE);
674 gtk_widget_get_allocation (widget, &allocation);
676 attributes.x = allocation.x + border_width;
677 attributes.y = allocation.y + border_width;
678 attributes.width = allocation.width - border_width * 2;
679 attributes.height = allocation.height - border_width * 2;
680 attributes.window_type = GDK_WINDOW_CHILD;
681 attributes.wclass = GDK_INPUT_OUTPUT;
682 attributes.visual = gtk_widget_get_visual (widget);
684 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
686 attributes.event_mask = event_mask | GDK_SCROLL_MASK | GDK_TOUCH_MASK | GDK_SMOOTH_SCROLL_MASK;
688 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
690 window = gdk_window_new (gtk_widget_get_parent_window (widget),
691 &attributes, attributes_mask);
692 gtk_widget_set_window (widget, window);
693 gtk_widget_register_window (widget, window);
695 viewport_get_view_allocation (viewport, &view_allocation);
697 attributes.x = view_allocation.x;
698 attributes.y = view_allocation.y;
699 attributes.width = view_allocation.width;
700 attributes.height = view_allocation.height;
701 attributes.event_mask = 0;
703 priv->view_window = gdk_window_new (window,
704 &attributes, attributes_mask);
705 gtk_widget_register_window (widget, priv->view_window);
707 attributes.x = - gtk_adjustment_get_value (hadjustment);
708 attributes.y = - gtk_adjustment_get_value (vadjustment);
709 attributes.width = gtk_adjustment_get_upper (hadjustment);
710 attributes.height = gtk_adjustment_get_upper (vadjustment);
712 attributes.event_mask = event_mask;
714 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
715 gtk_widget_register_window (widget, priv->bin_window);
717 child = gtk_bin_get_child (bin);
719 gtk_widget_set_parent_window (child, priv->bin_window);
721 context = gtk_widget_get_style_context (widget);
722 gtk_style_context_set_background (context, window);
723 gtk_style_context_set_background (context, priv->bin_window);
725 gdk_window_show (priv->bin_window);
726 gdk_window_show (priv->view_window);
730 gtk_viewport_unrealize (GtkWidget *widget)
732 GtkViewport *viewport = GTK_VIEWPORT (widget);
733 GtkViewportPrivate *priv = viewport->priv;
735 gtk_widget_unregister_window (widget, priv->view_window);
736 gdk_window_destroy (priv->view_window);
737 priv->view_window = NULL;
739 gtk_widget_unregister_window (widget, priv->bin_window);
740 gdk_window_destroy (priv->bin_window);
741 priv->bin_window = NULL;
743 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
747 gtk_viewport_draw (GtkWidget *widget,
750 GtkViewport *viewport = GTK_VIEWPORT (widget);
751 GtkViewportPrivate *priv = viewport->priv;
752 GtkStyleContext *context;
755 context = gtk_widget_get_style_context (widget);
757 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)) &&
758 priv->shadow_type != GTK_SHADOW_NONE)
760 gtk_style_context_save (context);
761 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
763 gtk_render_frame (context, cr, 0, 0,
764 gdk_window_get_width (gtk_widget_get_window (widget)),
765 gdk_window_get_height (gtk_widget_get_window (widget)));
767 gtk_style_context_restore (context);
770 if (gtk_cairo_should_draw_window (cr, priv->view_window))
772 /* This is a cute hack to ensure the contents of bin_window are
773 * restricted to where they are visible. We only need to do this
774 * clipping when called via gtk_widget_draw() and not in expose
775 * events. And when that happens every window (including this one)
778 gdk_window_get_position (priv->view_window, &x, &y);
779 cairo_rectangle (cr, x, y,
780 gdk_window_get_width (priv->view_window),
781 gdk_window_get_height (priv->view_window));
785 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
787 gdk_window_get_position (priv->bin_window, &x, &y);
788 gtk_render_background (context, cr, x, y,
789 gdk_window_get_width (priv->bin_window),
790 gdk_window_get_height (priv->bin_window));
792 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
799 gtk_viewport_add (GtkContainer *container,
802 GtkBin *bin = GTK_BIN (container);
803 GtkViewport *viewport = GTK_VIEWPORT (bin);
804 GtkViewportPrivate *priv = viewport->priv;
806 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
808 gtk_widget_set_parent_window (child, priv->bin_window);
810 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
814 gtk_viewport_size_allocate (GtkWidget *widget,
815 GtkAllocation *allocation)
817 GtkAllocation widget_allocation;
818 GtkViewport *viewport = GTK_VIEWPORT (widget);
819 GtkViewportPrivate *priv = viewport->priv;
820 GtkBin *bin = GTK_BIN (widget);
822 GtkAdjustment *hadjustment = priv->hadjustment;
823 GtkAdjustment *vadjustment = priv->vadjustment;
824 GtkAllocation child_allocation;
827 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
829 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
830 * redraw the shadow correctly.
832 gtk_widget_get_allocation (widget, &widget_allocation);
833 if (gtk_widget_get_mapped (widget) &&
834 priv->shadow_type != GTK_SHADOW_NONE &&
835 (widget_allocation.width != allocation->width ||
836 widget_allocation.height != allocation->height))
837 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
839 gtk_widget_set_allocation (widget, allocation);
841 g_object_freeze_notify (G_OBJECT (hadjustment));
842 g_object_freeze_notify (G_OBJECT (vadjustment));
844 viewport_set_hadjustment_values (viewport);
845 viewport_set_vadjustment_values (viewport);
847 child_allocation.x = 0;
848 child_allocation.y = 0;
849 child_allocation.width = gtk_adjustment_get_upper (hadjustment);
850 child_allocation.height = gtk_adjustment_get_upper (vadjustment);
851 if (gtk_widget_get_realized (widget))
853 GtkAllocation view_allocation;
855 gdk_window_move_resize (gtk_widget_get_window (widget),
856 allocation->x + border_width,
857 allocation->y + border_width,
858 allocation->width - border_width * 2,
859 allocation->height - border_width * 2);
861 viewport_get_view_allocation (viewport, &view_allocation);
862 gdk_window_move_resize (priv->view_window,
865 view_allocation.width,
866 view_allocation.height);
867 gdk_window_move_resize (priv->bin_window,
868 - gtk_adjustment_get_value (hadjustment),
869 - gtk_adjustment_get_value (vadjustment),
870 child_allocation.width,
871 child_allocation.height);
874 child = gtk_bin_get_child (bin);
875 if (child && gtk_widget_get_visible (child))
876 gtk_widget_size_allocate (child, &child_allocation);
878 g_object_thaw_notify (G_OBJECT (hadjustment));
879 g_object_thaw_notify (G_OBJECT (vadjustment));
883 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
886 GtkViewport *viewport = GTK_VIEWPORT (data);
887 GtkViewportPrivate *priv = viewport->priv;
888 GtkBin *bin = GTK_BIN (data);
891 child = gtk_bin_get_child (bin);
892 if (child && gtk_widget_get_visible (child) &&
893 gtk_widget_get_realized (GTK_WIDGET (viewport)))
895 GtkAdjustment *hadjustment = priv->hadjustment;
896 GtkAdjustment *vadjustment = priv->vadjustment;
900 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
901 new_x = - gtk_adjustment_get_value (hadjustment);
902 new_y = - gtk_adjustment_get_value (vadjustment);
904 if (new_x != old_x || new_y != old_y)
905 gdk_window_move (priv->bin_window, new_x, new_y);
910 gtk_viewport_style_updated (GtkWidget *widget)
912 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->style_updated (widget);
914 if (gtk_widget_get_realized (widget) &&
915 gtk_widget_get_has_window (widget))
917 GtkStyleContext *context;
918 GtkViewport *viewport = GTK_VIEWPORT (widget);
919 GtkViewportPrivate *priv = viewport->priv;
921 context = gtk_widget_get_style_context (widget);
922 gtk_style_context_set_background (context, priv->bin_window);
923 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
929 gtk_viewport_get_preferred_size (GtkWidget *widget,
930 GtkOrientation orientation,
934 GtkViewport *viewport = GTK_VIEWPORT (widget);
935 GtkViewportPrivate *priv = viewport->priv;
936 GtkStyleContext *context;
938 GtkBorder padding, border;
940 gint child_min, child_nat;
941 gint minimum, natural;
943 child = gtk_bin_get_child (GTK_BIN (widget));
945 /* XXX This should probably be (border_width * 2); but GTK+ has
946 * been doing this with a single border for a while now...
948 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
950 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
951 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
952 gtk_style_context_get_padding (context, state, &padding);
954 if (priv->shadow_type != GTK_SHADOW_NONE)
956 gtk_style_context_get_border (context, state, &border);
958 if (orientation == GTK_ORIENTATION_HORIZONTAL)
959 minimum += border.left + border.right;
961 minimum += border.top + border.bottom;
964 if (orientation == GTK_ORIENTATION_HORIZONTAL)
965 minimum += padding.left + padding.right;
967 minimum += padding.top + padding.bottom;
971 if (child && gtk_widget_get_visible (child))
973 if (orientation == GTK_ORIENTATION_HORIZONTAL)
974 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
976 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
978 minimum += child_min;
979 natural += child_nat;
983 *minimum_size = minimum;
986 *natural_size = natural;
990 gtk_viewport_get_preferred_width (GtkWidget *widget,
994 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
998 gtk_viewport_get_preferred_height (GtkWidget *widget,
1002 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);