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 #include "gtkviewport.h"
32 #include "gtkmarshalers.h"
33 #include "gtkscrollable.h"
34 #include "gtktypebuiltins.h"
35 #include "gtkprivate.h"
40 * @Short_description: An adapter which makes widgets scrollable
42 * @See_also:#GtkScrolledWindow, #GtkAdjustment
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.
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.
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_finalize (GObject *object);
86 static void gtk_viewport_set_property (GObject *object,
90 static void gtk_viewport_get_property (GObject *object,
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,
99 static void gtk_viewport_add (GtkContainer *container,
101 static void gtk_viewport_size_allocate (GtkWidget *widget,
102 GtkAllocation *allocation);
103 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
105 static void gtk_viewport_style_set (GtkWidget *widget,
106 GtkStyle *previous_style);
108 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
111 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
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->finalize = gtk_viewport_finalize;
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_set = gtk_viewport_style_set;
140 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
141 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
143 container_class->add = gtk_viewport_add;
145 /* GtkScrollable implementation */
146 g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment");
147 g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment");
148 g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
149 g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
151 g_object_class_install_property (gobject_class,
153 g_param_spec_enum ("shadow-type",
155 P_("Determines how the shadowed box around the viewport is drawn"),
156 GTK_TYPE_SHADOW_TYPE,
158 GTK_PARAM_READWRITE));
160 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
164 gtk_viewport_set_property (GObject *object,
169 GtkViewport *viewport;
171 viewport = GTK_VIEWPORT (object);
175 case PROP_HADJUSTMENT:
176 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
178 case PROP_VADJUSTMENT:
179 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
181 case PROP_HSCROLL_POLICY:
182 viewport->priv->hscroll_policy = g_value_get_enum (value);
183 gtk_widget_queue_resize (GTK_WIDGET (viewport));
185 case PROP_VSCROLL_POLICY:
186 viewport->priv->vscroll_policy = g_value_get_enum (value);
187 gtk_widget_queue_resize (GTK_WIDGET (viewport));
189 case PROP_SHADOW_TYPE:
190 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gtk_viewport_get_property (GObject *object,
204 GtkViewport *viewport = GTK_VIEWPORT (object);
205 GtkViewportPrivate *priv = viewport->priv;
209 case PROP_HADJUSTMENT:
210 g_value_set_object (value, priv->hadjustment);
212 case PROP_VADJUSTMENT:
213 g_value_set_object (value, priv->vadjustment);
215 case PROP_HSCROLL_POLICY:
216 g_value_set_enum (value, priv->hscroll_policy);
218 case PROP_VSCROLL_POLICY:
219 g_value_set_enum (value, priv->vscroll_policy);
221 case PROP_SHADOW_TYPE:
222 g_value_set_enum (value, priv->shadow_type);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gtk_viewport_init (GtkViewport *viewport)
233 GtkViewportPrivate *priv;
235 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
238 priv = viewport->priv;
240 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
242 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
243 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
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;
254 * @hadjustment: horizontal adjustment.
255 * @vadjustment: vertical adjustment.
256 * @returns: a new #GtkViewport.
258 * Creates a new #GtkViewport with the given adjustments.
262 gtk_viewport_new (GtkAdjustment *hadjustment,
263 GtkAdjustment *vadjustment)
267 viewport = g_object_new (GTK_TYPE_VIEWPORT,
268 "hadjustment", hadjustment,
269 "vadjustment", vadjustment,
275 #define ADJUSTMENT_POINTER(viewport, orientation) \
276 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
277 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
280 viewport_disconnect_adjustment (GtkViewport *viewport,
281 GtkOrientation orientation)
283 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
287 g_signal_handlers_disconnect_by_func (*adjustmentp,
288 gtk_viewport_adjustment_value_changed,
290 g_object_unref (*adjustmentp);
296 gtk_viewport_finalize (GObject *object)
298 GtkViewport *viewport = GTK_VIEWPORT (object);
300 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
301 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
303 G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
307 gtk_viewport_destroy (GtkWidget *widget)
309 GtkViewport *viewport = GTK_VIEWPORT (widget);
311 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
312 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
314 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
318 viewport_get_view_allocation (GtkViewport *viewport,
319 GtkAllocation *view_allocation)
321 GtkViewportPrivate *priv = viewport->priv;
323 GtkWidget *widget = GTK_WIDGET (viewport);
324 GtkAllocation allocation;
327 gtk_widget_get_allocation (widget, &allocation);
328 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
330 view_allocation->x = 0;
331 view_allocation->y = 0;
333 if (priv->shadow_type != GTK_SHADOW_NONE)
335 style = gtk_widget_get_style (widget);
336 view_allocation->x = style->xthickness;
337 view_allocation->y = style->ythickness;
340 view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
341 view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
345 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
346 gboolean *value_changed)
348 gdouble value = adjustment->value;
350 value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
351 if (value != adjustment->value)
353 adjustment->value = value;
355 *value_changed = TRUE;
357 else if (value_changed)
358 *value_changed = FALSE;
362 * gtk_viewport_get_hadjustment:
363 * @viewport: a #GtkViewport.
365 * Returns the horizontal adjustment of the viewport.
367 * Return value: (transfer none): the horizontal adjustment of @viewport.
369 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
372 gtk_viewport_get_hadjustment (GtkViewport *viewport)
374 GtkViewportPrivate *priv;
376 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
378 priv = viewport->priv;
380 if (!priv->hadjustment)
381 gtk_viewport_set_hadjustment (viewport, NULL);
383 return priv->hadjustment;
387 * gtk_viewport_get_vadjustment:
388 * @viewport: a #GtkViewport.
390 * Returns the vertical adjustment of the viewport.
392 * Return value: (transfer none): the vertical adjustment of @viewport.
394 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
397 gtk_viewport_get_vadjustment (GtkViewport *viewport)
399 GtkViewportPrivate *priv;
401 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
403 priv = viewport->priv;
405 if (!priv->vadjustment)
406 gtk_viewport_set_vadjustment (viewport, NULL);
408 return priv->vadjustment;
412 viewport_set_hadjustment_values (GtkViewport *viewport,
413 gboolean *value_changed)
415 GtkBin *bin = GTK_BIN (viewport);
416 GtkAllocation view_allocation;
417 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
419 gdouble old_page_size;
423 viewport_get_view_allocation (viewport, &view_allocation);
425 old_page_size = hadjustment->page_size;
426 old_upper = hadjustment->upper;
427 old_value = hadjustment->value;
428 hadjustment->page_size = view_allocation.width;
429 hadjustment->step_increment = view_allocation.width * 0.1;
430 hadjustment->page_increment = view_allocation.width * 0.9;
432 hadjustment->lower = 0;
434 child = gtk_bin_get_child (bin);
435 if (child && gtk_widget_get_visible (child))
437 gint minimum_width, natural_width;
440 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
441 gtk_widget_get_preferred_height (child, &scroll_height, NULL);
443 gtk_widget_get_preferred_height (child, NULL, &scroll_height);
445 gtk_widget_get_preferred_width_for_height (child,
446 MAX (view_allocation.height, scroll_height),
450 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
451 hadjustment->upper = MAX (minimum_width, view_allocation.width);
453 hadjustment->upper = MAX (natural_width, view_allocation.width);
456 hadjustment->upper = view_allocation.width;
458 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
460 gdouble dist = old_upper - (old_value + old_page_size);
461 hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
462 viewport_reclamp_adjustment (hadjustment, value_changed);
463 *value_changed = (old_value != hadjustment->value);
466 viewport_reclamp_adjustment (hadjustment, value_changed);
470 viewport_set_vadjustment_values (GtkViewport *viewport,
471 gboolean *value_changed)
473 GtkBin *bin = GTK_BIN (viewport);
474 GtkAllocation view_allocation;
475 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
478 viewport_get_view_allocation (viewport, &view_allocation);
480 vadjustment->page_size = view_allocation.height;
481 vadjustment->step_increment = view_allocation.height * 0.1;
482 vadjustment->page_increment = view_allocation.height * 0.9;
484 vadjustment->lower = 0;
486 child = gtk_bin_get_child (bin);
487 if (child && gtk_widget_get_visible (child))
489 gint minimum_height, natural_height;
492 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
493 gtk_widget_get_preferred_width (child, &scroll_width, NULL);
495 gtk_widget_get_preferred_width (child, NULL, &scroll_width);
497 gtk_widget_get_preferred_height_for_width (child,
498 MAX (view_allocation.width, scroll_width),
502 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
503 vadjustment->upper = MAX (minimum_height, view_allocation.height);
505 vadjustment->upper = MAX (natural_height, view_allocation.height);
508 vadjustment->upper = view_allocation.height;
510 viewport_reclamp_adjustment (vadjustment, value_changed);
514 viewport_set_adjustment (GtkViewport *viewport,
515 GtkOrientation orientation,
516 GtkAdjustment *adjustment)
518 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
519 gboolean value_changed;
521 if (adjustment && adjustment == *adjustmentp)
525 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
526 viewport_disconnect_adjustment (viewport, orientation);
527 *adjustmentp = adjustment;
528 g_object_ref_sink (adjustment);
530 if (orientation == GTK_ORIENTATION_HORIZONTAL)
531 viewport_set_hadjustment_values (viewport, &value_changed);
533 viewport_set_vadjustment_values (viewport, &value_changed);
535 g_signal_connect (adjustment, "value-changed",
536 G_CALLBACK (gtk_viewport_adjustment_value_changed),
539 gtk_adjustment_changed (adjustment);
542 gtk_adjustment_value_changed (adjustment);
544 gtk_viewport_adjustment_value_changed (adjustment, viewport);
548 * gtk_viewport_set_hadjustment:
549 * @viewport: a #GtkViewport.
550 * @adjustment: (allow-none): a #GtkAdjustment.
552 * Sets the horizontal adjustment of the viewport.
554 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
557 gtk_viewport_set_hadjustment (GtkViewport *viewport,
558 GtkAdjustment *adjustment)
560 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
562 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
564 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
566 g_object_notify (G_OBJECT (viewport), "hadjustment");
570 * gtk_viewport_set_vadjustment:
571 * @viewport: a #GtkViewport.
572 * @adjustment: (allow-none): a #GtkAdjustment.
574 * Sets the vertical adjustment of the viewport.
576 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
579 gtk_viewport_set_vadjustment (GtkViewport *viewport,
580 GtkAdjustment *adjustment)
582 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
584 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
586 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
588 g_object_notify (G_OBJECT (viewport), "vadjustment");
592 * gtk_viewport_set_shadow_type:
593 * @viewport: a #GtkViewport.
594 * @type: the new shadow type.
596 * Sets the shadow type of the viewport.
599 gtk_viewport_set_shadow_type (GtkViewport *viewport,
602 GtkViewportPrivate *priv;
603 GtkAllocation allocation;
606 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
608 widget = GTK_WIDGET (viewport);
609 priv = viewport->priv;
611 if ((GtkShadowType) priv->shadow_type != type)
613 priv->shadow_type = type;
615 if (gtk_widget_get_visible (widget))
617 gtk_widget_get_allocation (widget, &allocation);
618 gtk_widget_size_allocate (widget, &allocation);
619 gtk_widget_set_allocation (widget, &allocation);
620 gtk_widget_queue_draw (widget);
623 g_object_notify (G_OBJECT (viewport), "shadow-type");
628 * gtk_viewport_get_shadow_type:
629 * @viewport: a #GtkViewport
631 * Gets the shadow type of the #GtkViewport. See
632 * gtk_viewport_set_shadow_type().
634 * Return value: the shadow type
637 gtk_viewport_get_shadow_type (GtkViewport *viewport)
639 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
641 return viewport->priv->shadow_type;
645 * gtk_viewport_get_bin_window:
646 * @viewport: a #GtkViewport
648 * Gets the bin window of the #GtkViewport.
650 * Return value: (transfer none): a #GdkWindow
655 gtk_viewport_get_bin_window (GtkViewport *viewport)
657 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
659 return viewport->priv->bin_window;
663 * gtk_viewport_get_view_window:
664 * @viewport: a #GtkViewport
666 * Gets the view window of the #GtkViewport.
668 * Return value: (transfer none): a #GdkWindow
673 gtk_viewport_get_view_window (GtkViewport *viewport)
675 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
677 return viewport->priv->view_window;
681 gtk_viewport_realize (GtkWidget *widget)
683 GtkViewport *viewport = GTK_VIEWPORT (widget);
684 GtkViewportPrivate *priv = viewport->priv;
685 GtkBin *bin = GTK_BIN (widget);
686 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
687 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
688 GtkAllocation allocation;
689 GtkAllocation view_allocation;
693 GdkWindowAttr attributes;
694 gint attributes_mask;
698 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
700 gtk_widget_set_realized (widget, TRUE);
702 gtk_widget_get_allocation (widget, &allocation);
704 attributes.x = allocation.x + border_width;
705 attributes.y = allocation.y + border_width;
706 attributes.width = allocation.width - border_width * 2;
707 attributes.height = allocation.height - border_width * 2;
708 attributes.window_type = GDK_WINDOW_CHILD;
709 attributes.wclass = GDK_INPUT_OUTPUT;
710 attributes.visual = gtk_widget_get_visual (widget);
712 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
713 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
715 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
717 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
719 window = gdk_window_new (gtk_widget_get_parent_window (widget),
720 &attributes, attributes_mask);
721 gtk_widget_set_window (widget, window);
722 gdk_window_set_user_data (window, viewport);
724 viewport_get_view_allocation (viewport, &view_allocation);
726 attributes.x = view_allocation.x;
727 attributes.y = view_allocation.y;
728 attributes.width = view_allocation.width;
729 attributes.height = view_allocation.height;
730 attributes.event_mask = 0;
732 priv->view_window = gdk_window_new (window,
733 &attributes, attributes_mask);
734 gdk_window_set_user_data (priv->view_window, viewport);
736 attributes.x = - hadjustment->value;
737 attributes.y = - vadjustment->value;
738 attributes.width = hadjustment->upper;
739 attributes.height = vadjustment->upper;
741 attributes.event_mask = event_mask;
743 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
744 gdk_window_set_user_data (priv->bin_window, viewport);
746 child = gtk_bin_get_child (bin);
748 gtk_widget_set_parent_window (child, priv->bin_window);
750 gtk_widget_style_attach (widget);
751 style = gtk_widget_get_style (widget);
752 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
753 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
755 gdk_window_show (priv->bin_window);
756 gdk_window_show (priv->view_window);
760 gtk_viewport_unrealize (GtkWidget *widget)
762 GtkViewport *viewport = GTK_VIEWPORT (widget);
763 GtkViewportPrivate *priv = viewport->priv;
765 gdk_window_set_user_data (priv->view_window, NULL);
766 gdk_window_destroy (priv->view_window);
767 priv->view_window = NULL;
769 gdk_window_set_user_data (priv->bin_window, NULL);
770 gdk_window_destroy (priv->bin_window);
771 priv->bin_window = NULL;
773 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
777 gtk_viewport_draw (GtkWidget *widget,
780 GtkViewport *viewport = GTK_VIEWPORT (widget);
781 GtkViewportPrivate *priv = viewport->priv;
784 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
786 gtk_paint_shadow (gtk_widget_get_style (widget),
788 GTK_STATE_NORMAL, priv->shadow_type,
791 gdk_window_get_width (gtk_widget_get_window (widget)),
792 gdk_window_get_height (gtk_widget_get_window (widget)));
795 if (gtk_cairo_should_draw_window (cr, priv->view_window))
797 /* This is a cute hack to ensure the contents of bin_window are
798 * restricted to where they are visible. We only need to do this
799 * clipping when called via gtk_widget_draw() and not in expose
800 * events. And when that happens every window (including this one)
803 gdk_window_get_position (priv->view_window, &x, &y);
804 cairo_rectangle (cr, x, y,
805 gdk_window_get_width (priv->view_window),
806 gdk_window_get_height (priv->view_window));
810 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
812 gdk_window_get_position (priv->bin_window, &x, &y);
813 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
814 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
815 widget, "viewportbin",
817 gdk_window_get_width (priv->bin_window),
818 gdk_window_get_height (priv->bin_window));
820 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
827 gtk_viewport_add (GtkContainer *container,
830 GtkBin *bin = GTK_BIN (container);
831 GtkViewport *viewport = GTK_VIEWPORT (bin);
832 GtkViewportPrivate *priv = viewport->priv;
834 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
836 gtk_widget_set_parent_window (child, priv->bin_window);
838 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
842 gtk_viewport_size_allocate (GtkWidget *widget,
843 GtkAllocation *allocation)
845 GtkAllocation widget_allocation;
846 GtkViewport *viewport = GTK_VIEWPORT (widget);
847 GtkViewportPrivate *priv = viewport->priv;
848 GtkBin *bin = GTK_BIN (widget);
850 gboolean hadjustment_value_changed, vadjustment_value_changed;
851 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
852 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
853 GtkAllocation child_allocation;
856 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
858 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
859 * redraw the shadow correctly.
861 gtk_widget_get_allocation (widget, &widget_allocation);
862 if (gtk_widget_get_mapped (widget) &&
863 priv->shadow_type != GTK_SHADOW_NONE &&
864 (widget_allocation.width != allocation->width ||
865 widget_allocation.height != allocation->height))
866 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
868 gtk_widget_set_allocation (widget, allocation);
870 viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
871 viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
873 child_allocation.x = 0;
874 child_allocation.y = 0;
875 child_allocation.width = hadjustment->upper;
876 child_allocation.height = vadjustment->upper;
877 if (gtk_widget_get_realized (widget))
879 GtkAllocation view_allocation;
881 gdk_window_move_resize (gtk_widget_get_window (widget),
882 allocation->x + border_width,
883 allocation->y + border_width,
884 allocation->width - border_width * 2,
885 allocation->height - border_width * 2);
887 viewport_get_view_allocation (viewport, &view_allocation);
888 gdk_window_move_resize (priv->view_window,
891 view_allocation.width,
892 view_allocation.height);
893 gdk_window_move_resize (priv->bin_window,
894 - hadjustment->value,
895 - vadjustment->value,
896 child_allocation.width,
897 child_allocation.height);
900 child = gtk_bin_get_child (bin);
901 if (child && gtk_widget_get_visible (child))
902 gtk_widget_size_allocate (child, &child_allocation);
904 gtk_adjustment_changed (hadjustment);
905 gtk_adjustment_changed (vadjustment);
906 if (hadjustment_value_changed)
907 gtk_adjustment_value_changed (hadjustment);
908 if (vadjustment_value_changed)
909 gtk_adjustment_value_changed (vadjustment);
913 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
916 GtkViewport *viewport = GTK_VIEWPORT (data);
917 GtkViewportPrivate *priv = viewport->priv;
918 GtkBin *bin = GTK_BIN (data);
921 child = gtk_bin_get_child (bin);
922 if (child && gtk_widget_get_visible (child) &&
923 gtk_widget_get_realized (GTK_WIDGET (viewport)))
925 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
926 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
930 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
931 new_x = - hadjustment->value;
932 new_y = - vadjustment->value;
934 if (new_x != old_x || new_y != old_y)
936 gdk_window_move (priv->bin_window, new_x, new_y);
937 gdk_window_process_updates (priv->bin_window, TRUE);
943 gtk_viewport_style_set (GtkWidget *widget,
944 GtkStyle *previous_style)
946 if (gtk_widget_get_realized (widget) &&
947 gtk_widget_get_has_window (widget))
950 GtkViewport *viewport = GTK_VIEWPORT (widget);
951 GtkViewportPrivate *priv = viewport->priv;
953 style = gtk_widget_get_style (widget);
954 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
955 gtk_style_set_background (style,
956 gtk_widget_get_window (widget),
957 gtk_widget_get_state (widget));
963 gtk_viewport_get_preferred_size (GtkWidget *widget,
964 GtkOrientation orientation,
968 GtkViewport *viewport = GTK_VIEWPORT (widget);
969 GtkViewportPrivate *priv = viewport->priv;
972 gint child_min, child_nat;
973 gint minimum, natural;
975 child = gtk_bin_get_child (GTK_BIN (widget));
977 /* XXX This should probably be (border_width * 2); but GTK+ has
978 * been doing this with a single border for a while now...
980 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
982 if (priv->shadow_type != GTK_SHADOW_NONE)
984 style = gtk_widget_get_style (GTK_WIDGET (widget));
985 if (orientation == GTK_ORIENTATION_HORIZONTAL)
986 minimum += 2 * style->xthickness;
988 minimum += 2 * style->ythickness;
993 if (child && gtk_widget_get_visible (child))
995 if (orientation == GTK_ORIENTATION_HORIZONTAL)
996 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
998 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
1000 minimum += child_min;
1001 natural += child_nat;
1005 *minimum_size = minimum;
1008 *natural_size = natural;
1012 gtk_viewport_get_preferred_width (GtkWidget *widget,
1016 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1020 gtk_viewport_get_preferred_height (GtkWidget *widget,
1024 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);