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 * The convenience function gtk_scrolled_window_add_with_viewport() does
53 * exactly this, so you can ignore the presence of the viewport.
55 * The #GtkViewport will start scrolling content only if allocated less
56 * than the child widget's minimum size in a given orientation.
59 struct _GtkViewportPrivate
61 GtkAdjustment *hadjustment;
62 GtkAdjustment *vadjustment;
63 GtkShadowType shadow_type;
65 GdkWindow *bin_window;
66 GdkWindow *view_window;
68 /* GtkScrollablePolicy needs to be checked when
69 * driving the scrollable adjustment values */
70 guint hscroll_policy : 1;
71 guint vscroll_policy : 1;
84 static void gtk_viewport_set_property (GObject *object,
88 static void gtk_viewport_get_property (GObject *object,
92 static void gtk_viewport_destroy (GtkWidget *widget);
93 static void gtk_viewport_realize (GtkWidget *widget);
94 static void gtk_viewport_unrealize (GtkWidget *widget);
95 static gint gtk_viewport_draw (GtkWidget *widget,
97 static void gtk_viewport_add (GtkContainer *container,
99 static void gtk_viewport_size_allocate (GtkWidget *widget,
100 GtkAllocation *allocation);
101 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
103 static void gtk_viewport_style_updated (GtkWidget *widget);
105 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
108 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
112 static void viewport_set_adjustment (GtkViewport *viewport,
113 GtkOrientation orientation,
114 GtkAdjustment *adjustment);
116 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
117 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
120 gtk_viewport_class_init (GtkViewportClass *class)
122 GObjectClass *gobject_class;
123 GtkWidgetClass *widget_class;
124 GtkContainerClass *container_class;
126 gobject_class = G_OBJECT_CLASS (class);
127 widget_class = (GtkWidgetClass*) class;
128 container_class = (GtkContainerClass*) class;
130 gobject_class->set_property = gtk_viewport_set_property;
131 gobject_class->get_property = gtk_viewport_get_property;
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;
142 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_VIEWPORT);
144 container_class->add = gtk_viewport_add;
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");
152 g_object_class_install_property (gobject_class,
154 g_param_spec_enum ("shadow-type",
156 P_("Determines how the shadowed box around the viewport is drawn"),
157 GTK_TYPE_SHADOW_TYPE,
159 GTK_PARAM_READWRITE));
161 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
165 gtk_viewport_set_property (GObject *object,
170 GtkViewport *viewport;
172 viewport = GTK_VIEWPORT (object);
176 case PROP_HADJUSTMENT:
177 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, g_value_get_object (value));
179 case PROP_VADJUSTMENT:
180 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, g_value_get_object (value));
182 case PROP_HSCROLL_POLICY:
183 viewport->priv->hscroll_policy = g_value_get_enum (value);
184 gtk_widget_queue_resize (GTK_WIDGET (viewport));
186 case PROP_VSCROLL_POLICY:
187 viewport->priv->vscroll_policy = g_value_get_enum (value);
188 gtk_widget_queue_resize (GTK_WIDGET (viewport));
190 case PROP_SHADOW_TYPE:
191 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gtk_viewport_get_property (GObject *object,
205 GtkViewport *viewport = GTK_VIEWPORT (object);
206 GtkViewportPrivate *priv = viewport->priv;
210 case PROP_HADJUSTMENT:
211 g_value_set_object (value, priv->hadjustment);
213 case PROP_VADJUSTMENT:
214 g_value_set_object (value, priv->vadjustment);
216 case PROP_HSCROLL_POLICY:
217 g_value_set_enum (value, priv->hscroll_policy);
219 case PROP_VSCROLL_POLICY:
220 g_value_set_enum (value, priv->vscroll_policy);
222 case PROP_SHADOW_TYPE:
223 g_value_set_enum (value, priv->shadow_type);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 gtk_viewport_init (GtkViewport *viewport)
234 GtkViewportPrivate *priv;
236 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
239 priv = viewport->priv;
241 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
243 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
245 priv->shadow_type = GTK_SHADOW_IN;
246 priv->view_window = NULL;
247 priv->bin_window = NULL;
248 priv->hadjustment = NULL;
249 priv->vadjustment = NULL;
251 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, NULL);
252 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, NULL);
257 * @hadjustment: horizontal adjustment
258 * @vadjustment: vertical adjustment
260 * Creates a new #GtkViewport with the given adjustments.
262 * Returns: a new #GtkViewport
265 gtk_viewport_new (GtkAdjustment *hadjustment,
266 GtkAdjustment *vadjustment)
270 viewport = g_object_new (GTK_TYPE_VIEWPORT,
271 "hadjustment", hadjustment,
272 "vadjustment", vadjustment,
278 #define ADJUSTMENT_POINTER(viewport, orientation) \
279 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
280 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
283 viewport_disconnect_adjustment (GtkViewport *viewport,
284 GtkOrientation orientation)
286 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
290 g_signal_handlers_disconnect_by_func (*adjustmentp,
291 gtk_viewport_adjustment_value_changed,
293 g_object_unref (*adjustmentp);
299 gtk_viewport_destroy (GtkWidget *widget)
301 GtkViewport *viewport = GTK_VIEWPORT (widget);
303 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
304 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
306 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
310 viewport_get_view_allocation (GtkViewport *viewport,
311 GtkAllocation *view_allocation)
313 GtkViewportPrivate *priv = viewport->priv;
314 GtkWidget *widget = GTK_WIDGET (viewport);
315 GtkAllocation allocation;
316 GtkStyleContext *context;
318 GtkBorder padding, border;
321 gtk_widget_get_allocation (widget, &allocation);
322 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
324 view_allocation->x = 0;
325 view_allocation->y = 0;
327 context = gtk_widget_get_style_context (widget);
328 state = gtk_widget_get_state_flags (widget);
329 gtk_style_context_save (context);
330 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
332 gtk_style_context_get_padding (context, state, &padding);
333 gtk_style_context_get_border (context, state, &border);
335 gtk_style_context_restore (context);
337 if (priv->shadow_type != GTK_SHADOW_NONE)
339 view_allocation->x = border.left;
340 view_allocation->y = border.top;
343 view_allocation->x += padding.left;
344 view_allocation->y += padding.top;
345 view_allocation->width = MAX (1, allocation.width - padding.left - padding.right - border_width * 2);
346 view_allocation->height = MAX (1, allocation.height - padding.top - padding.bottom - border_width * 2);
348 if (priv->shadow_type != GTK_SHADOW_NONE)
350 view_allocation->width = MAX (1, view_allocation->width - border.left - border.right);
351 view_allocation->height = MAX (1, view_allocation->height - border.top - border.bottom);
356 * gtk_viewport_get_hadjustment:
357 * @viewport: a #GtkViewport.
359 * Returns the horizontal adjustment of the viewport.
361 * Return value: (transfer none): the horizontal adjustment of @viewport.
363 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
366 gtk_viewport_get_hadjustment (GtkViewport *viewport)
368 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
370 return viewport->priv->hadjustment;
374 * gtk_viewport_get_vadjustment:
375 * @viewport: a #GtkViewport.
377 * Returns the vertical adjustment of the viewport.
379 * Return value: (transfer none): the vertical adjustment of @viewport.
381 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
384 gtk_viewport_get_vadjustment (GtkViewport *viewport)
386 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
388 return viewport->priv->vadjustment;
392 viewport_set_hadjustment_values (GtkViewport *viewport)
394 GtkBin *bin = GTK_BIN (viewport);
395 GtkAllocation view_allocation;
396 GtkAdjustment *hadjustment = viewport->priv->hadjustment;
398 gdouble upper, value;
400 viewport_get_view_allocation (viewport, &view_allocation);
402 child = gtk_bin_get_child (bin);
403 if (child && gtk_widget_get_visible (child))
405 gint minimum_width, natural_width;
408 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
409 gtk_widget_get_preferred_height (child, &scroll_height, NULL);
411 gtk_widget_get_preferred_height (child, NULL, &scroll_height);
413 gtk_widget_get_preferred_width_for_height (child,
414 MAX (view_allocation.height, scroll_height),
418 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
419 upper = MAX (minimum_width, view_allocation.width);
421 upper = MAX (natural_width, view_allocation.width);
424 upper = view_allocation.width;
426 value = gtk_adjustment_get_value (hadjustment);
427 /* We clamp to the left in RTL mode */
428 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
430 gdouble dist = gtk_adjustment_get_upper (hadjustment)
432 - gtk_adjustment_get_page_size (hadjustment);
433 value = upper - dist - view_allocation.width;
436 gtk_adjustment_configure (hadjustment,
440 view_allocation.width * 0.1,
441 view_allocation.width * 0.9,
442 view_allocation.width);
446 viewport_set_vadjustment_values (GtkViewport *viewport)
448 GtkBin *bin = GTK_BIN (viewport);
449 GtkAllocation view_allocation;
450 GtkAdjustment *vadjustment = viewport->priv->vadjustment;
454 viewport_get_view_allocation (viewport, &view_allocation);
456 child = gtk_bin_get_child (bin);
457 if (child && gtk_widget_get_visible (child))
459 gint minimum_height, natural_height;
462 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
463 gtk_widget_get_preferred_width (child, &scroll_width, NULL);
465 gtk_widget_get_preferred_width (child, NULL, &scroll_width);
467 gtk_widget_get_preferred_height_for_width (child,
468 MAX (view_allocation.width, scroll_width),
472 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
473 upper = MAX (minimum_height, view_allocation.height);
475 upper = MAX (natural_height, view_allocation.height);
478 upper = view_allocation.height;
480 gtk_adjustment_configure (vadjustment,
481 gtk_adjustment_get_value (vadjustment),
484 view_allocation.height * 0.1,
485 view_allocation.height * 0.9,
486 view_allocation.height);
490 viewport_set_adjustment (GtkViewport *viewport,
491 GtkOrientation orientation,
492 GtkAdjustment *adjustment)
494 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
496 if (adjustment && adjustment == *adjustmentp)
500 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
501 viewport_disconnect_adjustment (viewport, orientation);
502 *adjustmentp = adjustment;
503 g_object_ref_sink (adjustment);
505 if (orientation == GTK_ORIENTATION_HORIZONTAL)
506 viewport_set_hadjustment_values (viewport);
508 viewport_set_vadjustment_values (viewport);
510 g_signal_connect (adjustment, "value-changed",
511 G_CALLBACK (gtk_viewport_adjustment_value_changed),
514 gtk_viewport_adjustment_value_changed (adjustment, viewport);
518 * gtk_viewport_set_hadjustment:
519 * @viewport: a #GtkViewport.
520 * @adjustment: (allow-none): a #GtkAdjustment.
522 * Sets the horizontal adjustment of the viewport.
524 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
527 gtk_viewport_set_hadjustment (GtkViewport *viewport,
528 GtkAdjustment *adjustment)
530 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
532 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
534 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
536 g_object_notify (G_OBJECT (viewport), "hadjustment");
540 * gtk_viewport_set_vadjustment:
541 * @viewport: a #GtkViewport.
542 * @adjustment: (allow-none): a #GtkAdjustment.
544 * Sets the vertical adjustment of the viewport.
546 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
549 gtk_viewport_set_vadjustment (GtkViewport *viewport,
550 GtkAdjustment *adjustment)
552 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
554 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
556 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
558 g_object_notify (G_OBJECT (viewport), "vadjustment");
562 * gtk_viewport_set_shadow_type:
563 * @viewport: a #GtkViewport.
564 * @type: the new shadow type.
566 * Sets the shadow type of the viewport.
569 gtk_viewport_set_shadow_type (GtkViewport *viewport,
572 GtkViewportPrivate *priv;
573 GtkAllocation allocation;
576 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
578 widget = GTK_WIDGET (viewport);
579 priv = viewport->priv;
581 if ((GtkShadowType) priv->shadow_type != type)
583 priv->shadow_type = type;
585 if (gtk_widget_get_visible (widget))
587 gtk_widget_get_allocation (widget, &allocation);
588 gtk_widget_size_allocate (widget, &allocation);
589 gtk_widget_set_allocation (widget, &allocation);
590 gtk_widget_queue_draw (widget);
593 g_object_notify (G_OBJECT (viewport), "shadow-type");
598 * gtk_viewport_get_shadow_type:
599 * @viewport: a #GtkViewport
601 * Gets the shadow type of the #GtkViewport. See
602 * gtk_viewport_set_shadow_type().
604 * Return value: the shadow type
607 gtk_viewport_get_shadow_type (GtkViewport *viewport)
609 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
611 return viewport->priv->shadow_type;
615 * gtk_viewport_get_bin_window:
616 * @viewport: a #GtkViewport
618 * Gets the bin window of the #GtkViewport.
620 * Return value: (transfer none): a #GdkWindow
625 gtk_viewport_get_bin_window (GtkViewport *viewport)
627 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
629 return viewport->priv->bin_window;
633 * gtk_viewport_get_view_window:
634 * @viewport: a #GtkViewport
636 * Gets the view window of the #GtkViewport.
638 * Return value: (transfer none): a #GdkWindow
643 gtk_viewport_get_view_window (GtkViewport *viewport)
645 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
647 return viewport->priv->view_window;
651 gtk_viewport_realize (GtkWidget *widget)
653 GtkViewport *viewport = GTK_VIEWPORT (widget);
654 GtkViewportPrivate *priv = viewport->priv;
655 GtkBin *bin = GTK_BIN (widget);
656 GtkAdjustment *hadjustment = priv->hadjustment;
657 GtkAdjustment *vadjustment = priv->vadjustment;
658 GtkAllocation allocation;
659 GtkAllocation view_allocation;
660 GtkStyleContext *context;
663 GdkWindowAttr attributes;
664 gint attributes_mask;
668 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
670 gtk_widget_set_realized (widget, TRUE);
672 gtk_widget_get_allocation (widget, &allocation);
674 attributes.x = allocation.x + border_width;
675 attributes.y = allocation.y + border_width;
676 attributes.width = allocation.width - border_width * 2;
677 attributes.height = allocation.height - border_width * 2;
678 attributes.window_type = GDK_WINDOW_CHILD;
679 attributes.wclass = GDK_INPUT_OUTPUT;
680 attributes.visual = gtk_widget_get_visual (widget);
682 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
684 attributes.event_mask = event_mask | GDK_SCROLL_MASK | GDK_TOUCH_MASK | GDK_SMOOTH_SCROLL_MASK;
686 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
688 window = gdk_window_new (gtk_widget_get_parent_window (widget),
689 &attributes, attributes_mask);
690 gtk_widget_set_window (widget, window);
691 gdk_window_set_user_data (window, viewport);
693 viewport_get_view_allocation (viewport, &view_allocation);
695 attributes.x = view_allocation.x;
696 attributes.y = view_allocation.y;
697 attributes.width = view_allocation.width;
698 attributes.height = view_allocation.height;
699 attributes.event_mask = 0;
701 priv->view_window = gdk_window_new (window,
702 &attributes, attributes_mask);
703 gdk_window_set_user_data (priv->view_window, viewport);
705 attributes.x = - gtk_adjustment_get_value (hadjustment);
706 attributes.y = - gtk_adjustment_get_value (vadjustment);
707 attributes.width = gtk_adjustment_get_upper (hadjustment);
708 attributes.height = gtk_adjustment_get_upper (vadjustment);
710 attributes.event_mask = event_mask;
712 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
713 gdk_window_set_user_data (priv->bin_window, viewport);
715 child = gtk_bin_get_child (bin);
717 gtk_widget_set_parent_window (child, priv->bin_window);
719 context = gtk_widget_get_style_context (widget);
720 gtk_style_context_set_background (context, window);
721 gtk_style_context_set_background (context, priv->bin_window);
723 gdk_window_show (priv->bin_window);
724 gdk_window_show (priv->view_window);
728 gtk_viewport_unrealize (GtkWidget *widget)
730 GtkViewport *viewport = GTK_VIEWPORT (widget);
731 GtkViewportPrivate *priv = viewport->priv;
733 gdk_window_set_user_data (priv->view_window, NULL);
734 gdk_window_destroy (priv->view_window);
735 priv->view_window = NULL;
737 gdk_window_set_user_data (priv->bin_window, NULL);
738 gdk_window_destroy (priv->bin_window);
739 priv->bin_window = NULL;
741 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
745 gtk_viewport_draw (GtkWidget *widget,
748 GtkViewport *viewport = GTK_VIEWPORT (widget);
749 GtkViewportPrivate *priv = viewport->priv;
750 GtkStyleContext *context;
753 context = gtk_widget_get_style_context (widget);
755 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)) &&
756 priv->shadow_type != GTK_SHADOW_NONE)
758 gtk_style_context_save (context);
759 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
761 gtk_render_frame (context, cr, 0, 0,
762 gdk_window_get_width (gtk_widget_get_window (widget)),
763 gdk_window_get_height (gtk_widget_get_window (widget)));
765 gtk_style_context_restore (context);
768 if (gtk_cairo_should_draw_window (cr, priv->view_window))
770 /* This is a cute hack to ensure the contents of bin_window are
771 * restricted to where they are visible. We only need to do this
772 * clipping when called via gtk_widget_draw() and not in expose
773 * events. And when that happens every window (including this one)
776 gdk_window_get_position (priv->view_window, &x, &y);
777 cairo_rectangle (cr, x, y,
778 gdk_window_get_width (priv->view_window),
779 gdk_window_get_height (priv->view_window));
783 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
785 gdk_window_get_position (priv->bin_window, &x, &y);
786 gtk_render_background (context, cr, x, y,
787 gdk_window_get_width (priv->bin_window),
788 gdk_window_get_height (priv->bin_window));
790 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
797 gtk_viewport_add (GtkContainer *container,
800 GtkBin *bin = GTK_BIN (container);
801 GtkViewport *viewport = GTK_VIEWPORT (bin);
802 GtkViewportPrivate *priv = viewport->priv;
804 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
806 gtk_widget_set_parent_window (child, priv->bin_window);
808 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
812 gtk_viewport_size_allocate (GtkWidget *widget,
813 GtkAllocation *allocation)
815 GtkAllocation widget_allocation;
816 GtkViewport *viewport = GTK_VIEWPORT (widget);
817 GtkViewportPrivate *priv = viewport->priv;
818 GtkBin *bin = GTK_BIN (widget);
820 GtkAdjustment *hadjustment = priv->hadjustment;
821 GtkAdjustment *vadjustment = priv->vadjustment;
822 GtkAllocation child_allocation;
825 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
827 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
828 * redraw the shadow correctly.
830 gtk_widget_get_allocation (widget, &widget_allocation);
831 if (gtk_widget_get_mapped (widget) &&
832 priv->shadow_type != GTK_SHADOW_NONE &&
833 (widget_allocation.width != allocation->width ||
834 widget_allocation.height != allocation->height))
835 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
837 gtk_widget_set_allocation (widget, allocation);
839 g_object_freeze_notify (G_OBJECT (hadjustment));
840 g_object_freeze_notify (G_OBJECT (vadjustment));
842 viewport_set_hadjustment_values (viewport);
843 viewport_set_vadjustment_values (viewport);
845 child_allocation.x = 0;
846 child_allocation.y = 0;
847 child_allocation.width = gtk_adjustment_get_upper (hadjustment);
848 child_allocation.height = gtk_adjustment_get_upper (vadjustment);
849 if (gtk_widget_get_realized (widget))
851 GtkAllocation view_allocation;
853 gdk_window_move_resize (gtk_widget_get_window (widget),
854 allocation->x + border_width,
855 allocation->y + border_width,
856 allocation->width - border_width * 2,
857 allocation->height - border_width * 2);
859 viewport_get_view_allocation (viewport, &view_allocation);
860 gdk_window_move_resize (priv->view_window,
863 view_allocation.width,
864 view_allocation.height);
865 gdk_window_move_resize (priv->bin_window,
866 - gtk_adjustment_get_value (hadjustment),
867 - gtk_adjustment_get_value (vadjustment),
868 child_allocation.width,
869 child_allocation.height);
872 child = gtk_bin_get_child (bin);
873 if (child && gtk_widget_get_visible (child))
874 gtk_widget_size_allocate (child, &child_allocation);
876 g_object_thaw_notify (G_OBJECT (hadjustment));
877 g_object_thaw_notify (G_OBJECT (vadjustment));
881 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
884 GtkViewport *viewport = GTK_VIEWPORT (data);
885 GtkViewportPrivate *priv = viewport->priv;
886 GtkBin *bin = GTK_BIN (data);
889 child = gtk_bin_get_child (bin);
890 if (child && gtk_widget_get_visible (child) &&
891 gtk_widget_get_realized (GTK_WIDGET (viewport)))
893 GtkAdjustment *hadjustment = priv->hadjustment;
894 GtkAdjustment *vadjustment = priv->vadjustment;
898 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
899 new_x = - gtk_adjustment_get_value (hadjustment);
900 new_y = - gtk_adjustment_get_value (vadjustment);
902 if (new_x != old_x || new_y != old_y)
903 gdk_window_move (priv->bin_window, new_x, new_y);
908 gtk_viewport_style_updated (GtkWidget *widget)
910 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->style_updated (widget);
912 if (gtk_widget_get_realized (widget) &&
913 gtk_widget_get_has_window (widget))
915 GtkStyleContext *context;
916 GtkViewport *viewport = GTK_VIEWPORT (widget);
917 GtkViewportPrivate *priv = viewport->priv;
919 context = gtk_widget_get_style_context (widget);
920 gtk_style_context_set_background (context, priv->bin_window);
921 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
927 gtk_viewport_get_preferred_size (GtkWidget *widget,
928 GtkOrientation orientation,
932 GtkViewport *viewport = GTK_VIEWPORT (widget);
933 GtkViewportPrivate *priv = viewport->priv;
934 GtkStyleContext *context;
936 GtkBorder padding, border;
938 gint child_min, child_nat;
939 gint minimum, natural;
941 child = gtk_bin_get_child (GTK_BIN (widget));
943 /* XXX This should probably be (border_width * 2); but GTK+ has
944 * been doing this with a single border for a while now...
946 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
948 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
949 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
950 gtk_style_context_get_padding (context, state, &padding);
952 if (priv->shadow_type != GTK_SHADOW_NONE)
954 gtk_style_context_get_border (context, state, &border);
956 if (orientation == GTK_ORIENTATION_HORIZONTAL)
957 minimum += border.left + border.right;
959 minimum += border.top + border.bottom;
962 if (orientation == GTK_ORIENTATION_HORIZONTAL)
963 minimum += padding.left + padding.right;
965 minimum += padding.top + padding.bottom;
969 if (child && gtk_widget_get_visible (child))
971 if (orientation == GTK_ORIENTATION_HORIZONTAL)
972 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
974 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
976 minimum += child_min;
977 natural += child_nat;
981 *minimum_size = minimum;
984 *natural_size = natural;
988 gtk_viewport_get_preferred_width (GtkWidget *widget,
992 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
996 gtk_viewport_get_preferred_height (GtkWidget *widget,
1000 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);