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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
29 #undef GTK_DISABLE_DEPRECATED
30 #include "gtkviewport.h"
31 #define GTK_DISABLE_DEPRECATED
33 #include "gtkmarshalers.h"
34 #include "gtktypeutils.h"
35 #include "gtkscrollable.h"
36 #include "gtkprivate.h"
41 * @Short_description: An adapter which makes widgets scrollable
43 * @See_also:#GtkScrolledWindow, #GtkAdjustment
45 * The #GtkViewport widget acts as an adaptor class, implementing
46 * scrollability for child widgets that lack their own scrolling
47 * capabilities. Use #GtkViewport to scroll child widgets such as
48 * #GtkTable, #GtkBox, and so on.
50 * If a widget has native scrolling abilities, such as #GtkTextView,
51 * #GtkTreeView or #GtkIconview, it can be added to a #GtkScrolledWindow
52 * with gtk_container_add(). If a widget does not, you must first add the
53 * widget to a #GtkViewport, then add the viewport to the scrolled window.
54 * The convenience function gtk_scrolled_window_add_with_viewport() does
55 * exactly this, so you can ignore the presence of the viewport.
57 * The #GtkViewport will start scrolling content only if allocated less
58 * than the child widget's minimum size in a given orientation.
61 struct _GtkViewportPrivate
63 GtkAdjustment *hadjustment;
64 GtkAdjustment *vadjustment;
65 GtkShadowType shadow_type;
67 GdkWindow *bin_window;
68 GdkWindow *view_window;
70 /* GtkScrollablePolicy needs to be checked when
71 * driving the scrollable adjustment values */
72 guint hscroll_policy : 1;
73 guint vscroll_policy : 1;
86 static void gtk_viewport_finalize (GObject *object);
87 static void gtk_viewport_set_property (GObject *object,
91 static void gtk_viewport_get_property (GObject *object,
95 static void gtk_viewport_destroy (GtkWidget *widget);
96 static void gtk_viewport_realize (GtkWidget *widget);
97 static void gtk_viewport_unrealize (GtkWidget *widget);
98 static gint gtk_viewport_draw (GtkWidget *widget,
100 static void gtk_viewport_add (GtkContainer *container,
102 static void gtk_viewport_size_allocate (GtkWidget *widget,
103 GtkAllocation *allocation);
104 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
106 static void gtk_viewport_style_set (GtkWidget *widget,
107 GtkStyle *previous_style);
109 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
112 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
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->finalize = gtk_viewport_finalize;
132 gobject_class->set_property = gtk_viewport_set_property;
133 gobject_class->get_property = gtk_viewport_get_property;
135 widget_class->destroy = gtk_viewport_destroy;
136 widget_class->realize = gtk_viewport_realize;
137 widget_class->unrealize = gtk_viewport_unrealize;
138 widget_class->draw = gtk_viewport_draw;
139 widget_class->size_allocate = gtk_viewport_size_allocate;
140 widget_class->style_set = gtk_viewport_style_set;
141 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
142 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
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 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
179 case PROP_VADJUSTMENT:
180 gtk_viewport_set_vadjustment (viewport, 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);
244 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
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;
255 * @hadjustment: horizontal adjustment.
256 * @vadjustment: vertical adjustment.
257 * @returns: a new #GtkViewport.
259 * Creates a new #GtkViewport with the given adjustments.
263 gtk_viewport_new (GtkAdjustment *hadjustment,
264 GtkAdjustment *vadjustment)
268 viewport = g_object_new (GTK_TYPE_VIEWPORT,
269 "hadjustment", hadjustment,
270 "vadjustment", vadjustment,
276 #define ADJUSTMENT_POINTER(viewport, orientation) \
277 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
278 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
281 viewport_disconnect_adjustment (GtkViewport *viewport,
282 GtkOrientation orientation)
284 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
288 g_signal_handlers_disconnect_by_func (*adjustmentp,
289 gtk_viewport_adjustment_value_changed,
291 g_object_unref (*adjustmentp);
297 gtk_viewport_finalize (GObject *object)
299 GtkViewport *viewport = GTK_VIEWPORT (object);
301 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
302 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
304 G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
308 gtk_viewport_destroy (GtkWidget *widget)
310 GtkViewport *viewport = GTK_VIEWPORT (widget);
312 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
313 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
315 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
319 viewport_get_view_allocation (GtkViewport *viewport,
320 GtkAllocation *view_allocation)
322 GtkViewportPrivate *priv = viewport->priv;
324 GtkWidget *widget = GTK_WIDGET (viewport);
325 GtkAllocation allocation;
328 gtk_widget_get_allocation (widget, &allocation);
329 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
331 view_allocation->x = 0;
332 view_allocation->y = 0;
334 if (priv->shadow_type != GTK_SHADOW_NONE)
336 style = gtk_widget_get_style (widget);
337 view_allocation->x = style->xthickness;
338 view_allocation->y = style->ythickness;
341 view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
342 view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
346 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
347 gboolean *value_changed)
349 gdouble value = adjustment->value;
351 value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
352 if (value != adjustment->value)
354 adjustment->value = value;
356 *value_changed = TRUE;
358 else if (value_changed)
359 *value_changed = FALSE;
363 * gtk_viewport_get_hadjustment:
364 * @viewport: a #GtkViewport.
366 * Returns the horizontal adjustment of the viewport.
368 * Return value: (transfer none): the horizontal adjustment of @viewport.
370 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
373 gtk_viewport_get_hadjustment (GtkViewport *viewport)
375 GtkViewportPrivate *priv;
377 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
379 priv = viewport->priv;
381 if (!priv->hadjustment)
382 gtk_viewport_set_hadjustment (viewport, NULL);
384 return priv->hadjustment;
388 * gtk_viewport_get_vadjustment:
389 * @viewport: a #GtkViewport.
391 * Returns the vertical adjustment of the viewport.
393 * Return value: (transfer none): the vertical adjustment of @viewport.
395 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
398 gtk_viewport_get_vadjustment (GtkViewport *viewport)
400 GtkViewportPrivate *priv;
402 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
404 priv = viewport->priv;
406 if (!priv->vadjustment)
407 gtk_viewport_set_vadjustment (viewport, NULL);
409 return priv->vadjustment;
413 viewport_set_hadjustment_values (GtkViewport *viewport,
414 gboolean *value_changed)
416 GtkBin *bin = GTK_BIN (viewport);
417 GtkAllocation view_allocation;
418 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
420 gdouble old_page_size;
424 viewport_get_view_allocation (viewport, &view_allocation);
426 old_page_size = hadjustment->page_size;
427 old_upper = hadjustment->upper;
428 old_value = hadjustment->value;
429 hadjustment->page_size = view_allocation.width;
430 hadjustment->step_increment = view_allocation.width * 0.1;
431 hadjustment->page_increment = view_allocation.width * 0.9;
433 hadjustment->lower = 0;
435 child = gtk_bin_get_child (bin);
436 if (child && gtk_widget_get_visible (child))
438 gint minimum_width, natural_width;
441 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
442 gtk_widget_get_preferred_height (child, &scroll_height, NULL);
444 gtk_widget_get_preferred_height (child, NULL, &scroll_height);
446 gtk_widget_get_preferred_width_for_height (child,
447 MAX (view_allocation.height, scroll_height),
451 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
452 hadjustment->upper = MAX (minimum_width, view_allocation.width);
454 hadjustment->upper = MAX (natural_width, view_allocation.width);
457 hadjustment->upper = view_allocation.width;
459 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
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);
467 viewport_reclamp_adjustment (hadjustment, value_changed);
471 viewport_set_vadjustment_values (GtkViewport *viewport,
472 gboolean *value_changed)
474 GtkBin *bin = GTK_BIN (viewport);
475 GtkAllocation view_allocation;
476 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
479 viewport_get_view_allocation (viewport, &view_allocation);
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;
485 vadjustment->lower = 0;
487 child = gtk_bin_get_child (bin);
488 if (child && gtk_widget_get_visible (child))
490 gint minimum_height, natural_height;
493 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
494 gtk_widget_get_preferred_width (child, &scroll_width, NULL);
496 gtk_widget_get_preferred_width (child, NULL, &scroll_width);
498 gtk_widget_get_preferred_height_for_width (child,
499 MAX (view_allocation.width, scroll_width),
503 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
504 vadjustment->upper = MAX (minimum_height, view_allocation.height);
506 vadjustment->upper = MAX (natural_height, view_allocation.height);
509 vadjustment->upper = view_allocation.height;
511 viewport_reclamp_adjustment (vadjustment, value_changed);
515 viewport_set_adjustment (GtkViewport *viewport,
516 GtkOrientation orientation,
517 GtkAdjustment *adjustment)
519 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
520 gboolean value_changed;
522 if (adjustment && adjustment == *adjustmentp)
526 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
527 viewport_disconnect_adjustment (viewport, orientation);
528 *adjustmentp = adjustment;
529 g_object_ref_sink (adjustment);
531 if (orientation == GTK_ORIENTATION_HORIZONTAL)
532 viewport_set_hadjustment_values (viewport, &value_changed);
534 viewport_set_vadjustment_values (viewport, &value_changed);
536 g_signal_connect (adjustment, "value-changed",
537 G_CALLBACK (gtk_viewport_adjustment_value_changed),
540 gtk_adjustment_changed (adjustment);
543 gtk_adjustment_value_changed (adjustment);
545 gtk_viewport_adjustment_value_changed (adjustment, viewport);
549 * gtk_viewport_set_hadjustment:
550 * @viewport: a #GtkViewport.
551 * @adjustment: (allow-none): a #GtkAdjustment.
553 * Sets the horizontal adjustment of the viewport.
555 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
558 gtk_viewport_set_hadjustment (GtkViewport *viewport,
559 GtkAdjustment *adjustment)
561 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
563 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
565 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
567 g_object_notify (G_OBJECT (viewport), "hadjustment");
571 * gtk_viewport_set_vadjustment:
572 * @viewport: a #GtkViewport.
573 * @adjustment: (allow-none): a #GtkAdjustment.
575 * Sets the vertical adjustment of the viewport.
577 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
580 gtk_viewport_set_vadjustment (GtkViewport *viewport,
581 GtkAdjustment *adjustment)
583 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
585 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
587 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
589 g_object_notify (G_OBJECT (viewport), "vadjustment");
593 * gtk_viewport_set_shadow_type:
594 * @viewport: a #GtkViewport.
595 * @type: the new shadow type.
597 * Sets the shadow type of the viewport.
600 gtk_viewport_set_shadow_type (GtkViewport *viewport,
603 GtkViewportPrivate *priv;
604 GtkAllocation allocation;
607 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
609 widget = GTK_WIDGET (viewport);
610 priv = viewport->priv;
612 if ((GtkShadowType) priv->shadow_type != type)
614 priv->shadow_type = type;
616 if (gtk_widget_get_visible (widget))
618 gtk_widget_get_allocation (widget, &allocation);
619 gtk_widget_size_allocate (widget, &allocation);
620 gtk_widget_set_allocation (widget, &allocation);
621 gtk_widget_queue_draw (widget);
624 g_object_notify (G_OBJECT (viewport), "shadow-type");
629 * gtk_viewport_get_shadow_type:
630 * @viewport: a #GtkViewport
632 * Gets the shadow type of the #GtkViewport. See
633 * gtk_viewport_set_shadow_type().
635 * Return value: the shadow type
638 gtk_viewport_get_shadow_type (GtkViewport *viewport)
640 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
642 return viewport->priv->shadow_type;
646 * gtk_viewport_get_bin_window:
647 * @viewport: a #GtkViewport
649 * Gets the bin window of the #GtkViewport.
651 * Return value: (transfer none): a #GdkWindow
656 gtk_viewport_get_bin_window (GtkViewport *viewport)
658 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
660 return viewport->priv->bin_window;
664 * gtk_viewport_get_view_window:
665 * @viewport: a #GtkViewport
667 * Gets the view window of the #GtkViewport.
669 * Return value: (transfer none): a #GdkWindow
674 gtk_viewport_get_view_window (GtkViewport *viewport)
676 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
678 return viewport->priv->view_window;
682 gtk_viewport_realize (GtkWidget *widget)
684 GtkViewport *viewport = GTK_VIEWPORT (widget);
685 GtkViewportPrivate *priv = viewport->priv;
686 GtkBin *bin = GTK_BIN (widget);
687 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
688 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
689 GtkAllocation allocation;
690 GtkAllocation view_allocation;
694 GdkWindowAttr attributes;
695 gint attributes_mask;
699 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
701 gtk_widget_set_realized (widget, TRUE);
703 gtk_widget_get_allocation (widget, &allocation);
705 attributes.x = allocation.x + border_width;
706 attributes.y = allocation.y + border_width;
707 attributes.width = allocation.width - border_width * 2;
708 attributes.height = allocation.height - border_width * 2;
709 attributes.window_type = GDK_WINDOW_CHILD;
710 attributes.wclass = GDK_INPUT_OUTPUT;
711 attributes.visual = gtk_widget_get_visual (widget);
713 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
714 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
716 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
718 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
720 window = gdk_window_new (gtk_widget_get_parent_window (widget),
721 &attributes, attributes_mask);
722 gtk_widget_set_window (widget, window);
723 gdk_window_set_user_data (window, viewport);
725 viewport_get_view_allocation (viewport, &view_allocation);
727 attributes.x = view_allocation.x;
728 attributes.y = view_allocation.y;
729 attributes.width = view_allocation.width;
730 attributes.height = view_allocation.height;
731 attributes.event_mask = 0;
733 priv->view_window = gdk_window_new (window,
734 &attributes, attributes_mask);
735 gdk_window_set_user_data (priv->view_window, viewport);
737 attributes.x = - hadjustment->value;
738 attributes.y = - vadjustment->value;
739 attributes.width = hadjustment->upper;
740 attributes.height = vadjustment->upper;
742 attributes.event_mask = event_mask;
744 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
745 gdk_window_set_user_data (priv->bin_window, viewport);
747 child = gtk_bin_get_child (bin);
749 gtk_widget_set_parent_window (child, priv->bin_window);
751 gtk_widget_style_attach (widget);
752 style = gtk_widget_get_style (widget);
753 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
754 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
756 gdk_window_show (priv->bin_window);
757 gdk_window_show (priv->view_window);
761 gtk_viewport_unrealize (GtkWidget *widget)
763 GtkViewport *viewport = GTK_VIEWPORT (widget);
764 GtkViewportPrivate *priv = viewport->priv;
766 gdk_window_set_user_data (priv->view_window, NULL);
767 gdk_window_destroy (priv->view_window);
768 priv->view_window = NULL;
770 gdk_window_set_user_data (priv->bin_window, NULL);
771 gdk_window_destroy (priv->bin_window);
772 priv->bin_window = NULL;
774 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
778 gtk_viewport_draw (GtkWidget *widget,
781 GtkViewport *viewport = GTK_VIEWPORT (widget);
782 GtkViewportPrivate *priv = viewport->priv;
785 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
787 gtk_paint_shadow (gtk_widget_get_style (widget),
789 GTK_STATE_NORMAL, priv->shadow_type,
792 gdk_window_get_width (gtk_widget_get_window (widget)),
793 gdk_window_get_height (gtk_widget_get_window (widget)));
796 if (gtk_cairo_should_draw_window (cr, priv->view_window))
798 /* This is a cute hack to ensure the contents of bin_window are
799 * restricted to where they are visible. We only need to do this
800 * clipping when called via gtk_widget_draw() and not in expose
801 * events. And when that happens every window (including this one)
804 gdk_window_get_position (priv->view_window, &x, &y);
805 cairo_rectangle (cr, x, y,
806 gdk_window_get_width (priv->view_window),
807 gdk_window_get_height (priv->view_window));
811 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
813 gdk_window_get_position (priv->bin_window, &x, &y);
814 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
815 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
816 widget, "viewportbin",
818 gdk_window_get_width (priv->bin_window),
819 gdk_window_get_height (priv->bin_window));
821 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
828 gtk_viewport_add (GtkContainer *container,
831 GtkBin *bin = GTK_BIN (container);
832 GtkViewport *viewport = GTK_VIEWPORT (bin);
833 GtkViewportPrivate *priv = viewport->priv;
835 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
837 gtk_widget_set_parent_window (child, priv->bin_window);
839 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
843 gtk_viewport_size_allocate (GtkWidget *widget,
844 GtkAllocation *allocation)
846 GtkAllocation widget_allocation;
847 GtkViewport *viewport = GTK_VIEWPORT (widget);
848 GtkViewportPrivate *priv = viewport->priv;
849 GtkBin *bin = GTK_BIN (widget);
851 gboolean hadjustment_value_changed, vadjustment_value_changed;
852 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
853 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
854 GtkAllocation child_allocation;
857 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
859 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
860 * redraw the shadow correctly.
862 gtk_widget_get_allocation (widget, &widget_allocation);
863 if (gtk_widget_get_mapped (widget) &&
864 priv->shadow_type != GTK_SHADOW_NONE &&
865 (widget_allocation.width != allocation->width ||
866 widget_allocation.height != allocation->height))
867 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
869 gtk_widget_set_allocation (widget, allocation);
871 viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
872 viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
874 child_allocation.x = 0;
875 child_allocation.y = 0;
876 child_allocation.width = hadjustment->upper;
877 child_allocation.height = vadjustment->upper;
878 if (gtk_widget_get_realized (widget))
880 GtkAllocation view_allocation;
882 gdk_window_move_resize (gtk_widget_get_window (widget),
883 allocation->x + border_width,
884 allocation->y + border_width,
885 allocation->width - border_width * 2,
886 allocation->height - border_width * 2);
888 viewport_get_view_allocation (viewport, &view_allocation);
889 gdk_window_move_resize (priv->view_window,
892 view_allocation.width,
893 view_allocation.height);
894 gdk_window_move_resize (priv->bin_window,
895 - hadjustment->value,
896 - vadjustment->value,
897 child_allocation.width,
898 child_allocation.height);
901 child = gtk_bin_get_child (bin);
902 if (child && gtk_widget_get_visible (child))
903 gtk_widget_size_allocate (child, &child_allocation);
905 gtk_adjustment_changed (hadjustment);
906 gtk_adjustment_changed (vadjustment);
907 if (hadjustment_value_changed)
908 gtk_adjustment_value_changed (hadjustment);
909 if (vadjustment_value_changed)
910 gtk_adjustment_value_changed (vadjustment);
914 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
917 GtkViewport *viewport = GTK_VIEWPORT (data);
918 GtkViewportPrivate *priv = viewport->priv;
919 GtkBin *bin = GTK_BIN (data);
922 child = gtk_bin_get_child (bin);
923 if (child && gtk_widget_get_visible (child) &&
924 gtk_widget_get_realized (GTK_WIDGET (viewport)))
926 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
927 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
931 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
932 new_x = - hadjustment->value;
933 new_y = - vadjustment->value;
935 if (new_x != old_x || new_y != old_y)
937 gdk_window_move (priv->bin_window, new_x, new_y);
938 gdk_window_process_updates (priv->bin_window, TRUE);
944 gtk_viewport_style_set (GtkWidget *widget,
945 GtkStyle *previous_style)
947 if (gtk_widget_get_realized (widget) &&
948 gtk_widget_get_has_window (widget))
951 GtkViewport *viewport = GTK_VIEWPORT (widget);
952 GtkViewportPrivate *priv = viewport->priv;
954 style = gtk_widget_get_style (widget);
955 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
956 gtk_style_set_background (style,
957 gtk_widget_get_window (widget),
958 gtk_widget_get_state (widget));
964 gtk_viewport_get_preferred_size (GtkWidget *widget,
965 GtkOrientation orientation,
969 GtkViewport *viewport = GTK_VIEWPORT (widget);
970 GtkViewportPrivate *priv = viewport->priv;
973 gint child_min, child_nat;
974 gint minimum, natural;
976 child = gtk_bin_get_child (GTK_BIN (widget));
978 /* XXX This should probably be (border_width * 2); but GTK+ has
979 * been doing this with a single border for a while now...
981 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
983 if (priv->shadow_type != GTK_SHADOW_NONE)
985 style = gtk_widget_get_style (GTK_WIDGET (widget));
986 if (orientation == GTK_ORIENTATION_HORIZONTAL)
987 minimum += 2 * style->xthickness;
989 minimum += 2 * style->ythickness;
994 if (child && gtk_widget_get_visible (child))
996 if (orientation == GTK_ORIENTATION_HORIZONTAL)
997 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
999 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1001 minimum += child_min;
1002 natural += child_nat;
1006 *minimum_size = minimum;
1009 *natural_size = natural;
1013 gtk_viewport_get_preferred_width (GtkWidget *widget,
1017 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1021 gtk_viewport_get_preferred_height (GtkWidget *widget,
1025 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);