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 * gtk_viewport_get_hadjustment:
346 * @viewport: a #GtkViewport.
348 * Returns the horizontal adjustment of the viewport.
350 * Return value: (transfer none): the horizontal adjustment of @viewport.
352 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
355 gtk_viewport_get_hadjustment (GtkViewport *viewport)
357 GtkViewportPrivate *priv;
359 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
361 priv = viewport->priv;
363 if (!priv->hadjustment)
364 gtk_viewport_set_hadjustment (viewport, NULL);
366 return priv->hadjustment;
370 * gtk_viewport_get_vadjustment:
371 * @viewport: a #GtkViewport.
373 * Returns the vertical adjustment of the viewport.
375 * Return value: (transfer none): the vertical adjustment of @viewport.
377 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
380 gtk_viewport_get_vadjustment (GtkViewport *viewport)
382 GtkViewportPrivate *priv;
384 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
386 priv = viewport->priv;
388 if (!priv->vadjustment)
389 gtk_viewport_set_vadjustment (viewport, NULL);
391 return priv->vadjustment;
395 viewport_set_hadjustment_values (GtkViewport *viewport)
397 GtkBin *bin = GTK_BIN (viewport);
398 GtkAllocation view_allocation;
399 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
401 gdouble upper, value;
403 viewport_get_view_allocation (viewport, &view_allocation);
405 child = gtk_bin_get_child (bin);
406 if (child && gtk_widget_get_visible (child))
408 gint minimum_width, natural_width;
411 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
412 gtk_widget_get_preferred_height (child, &scroll_height, NULL);
414 gtk_widget_get_preferred_height (child, NULL, &scroll_height);
416 gtk_widget_get_preferred_width_for_height (child,
417 MAX (view_allocation.height, scroll_height),
421 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
422 upper = MAX (minimum_width, view_allocation.width);
424 upper = MAX (natural_width, view_allocation.width);
427 upper = view_allocation.width;
429 value = gtk_adjustment_get_value (hadjustment);
430 /* We clamp to the left in RTL mode */
431 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
433 gdouble dist = gtk_adjustment_get_upper (hadjustment)
435 - gtk_adjustment_get_page_size (hadjustment);
436 value = upper - dist - view_allocation.width;
439 gtk_adjustment_configure (hadjustment,
443 view_allocation.width * 0.1,
444 view_allocation.width * 0.9,
445 view_allocation.width);
449 viewport_set_vadjustment_values (GtkViewport *viewport)
451 GtkBin *bin = GTK_BIN (viewport);
452 GtkAllocation view_allocation;
453 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
457 viewport_get_view_allocation (viewport, &view_allocation);
459 child = gtk_bin_get_child (bin);
460 if (child && gtk_widget_get_visible (child))
462 gint minimum_height, natural_height;
465 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
466 gtk_widget_get_preferred_width (child, &scroll_width, NULL);
468 gtk_widget_get_preferred_width (child, NULL, &scroll_width);
470 gtk_widget_get_preferred_height_for_width (child,
471 MAX (view_allocation.width, scroll_width),
475 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
476 upper = MAX (minimum_height, view_allocation.height);
478 upper = MAX (natural_height, view_allocation.height);
481 upper = view_allocation.height;
483 gtk_adjustment_configure (vadjustment,
484 gtk_adjustment_get_value (vadjustment),
487 view_allocation.height * 0.1,
488 view_allocation.height * 0.9,
489 view_allocation.height);
493 viewport_set_adjustment (GtkViewport *viewport,
494 GtkOrientation orientation,
495 GtkAdjustment *adjustment)
497 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
499 if (adjustment && adjustment == *adjustmentp)
503 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
504 viewport_disconnect_adjustment (viewport, orientation);
505 *adjustmentp = adjustment;
506 g_object_ref_sink (adjustment);
508 if (orientation == GTK_ORIENTATION_HORIZONTAL)
509 viewport_set_hadjustment_values (viewport);
511 viewport_set_vadjustment_values (viewport);
513 g_signal_connect (adjustment, "value-changed",
514 G_CALLBACK (gtk_viewport_adjustment_value_changed),
517 gtk_viewport_adjustment_value_changed (adjustment, viewport);
521 * gtk_viewport_set_hadjustment:
522 * @viewport: a #GtkViewport.
523 * @adjustment: (allow-none): a #GtkAdjustment.
525 * Sets the horizontal adjustment of the viewport.
527 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
530 gtk_viewport_set_hadjustment (GtkViewport *viewport,
531 GtkAdjustment *adjustment)
533 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
535 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
537 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
539 g_object_notify (G_OBJECT (viewport), "hadjustment");
543 * gtk_viewport_set_vadjustment:
544 * @viewport: a #GtkViewport.
545 * @adjustment: (allow-none): a #GtkAdjustment.
547 * Sets the vertical adjustment of the viewport.
549 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
552 gtk_viewport_set_vadjustment (GtkViewport *viewport,
553 GtkAdjustment *adjustment)
555 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
557 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
559 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
561 g_object_notify (G_OBJECT (viewport), "vadjustment");
565 * gtk_viewport_set_shadow_type:
566 * @viewport: a #GtkViewport.
567 * @type: the new shadow type.
569 * Sets the shadow type of the viewport.
572 gtk_viewport_set_shadow_type (GtkViewport *viewport,
575 GtkViewportPrivate *priv;
576 GtkAllocation allocation;
579 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
581 widget = GTK_WIDGET (viewport);
582 priv = viewport->priv;
584 if ((GtkShadowType) priv->shadow_type != type)
586 priv->shadow_type = type;
588 if (gtk_widget_get_visible (widget))
590 gtk_widget_get_allocation (widget, &allocation);
591 gtk_widget_size_allocate (widget, &allocation);
592 gtk_widget_set_allocation (widget, &allocation);
593 gtk_widget_queue_draw (widget);
596 g_object_notify (G_OBJECT (viewport), "shadow-type");
601 * gtk_viewport_get_shadow_type:
602 * @viewport: a #GtkViewport
604 * Gets the shadow type of the #GtkViewport. See
605 * gtk_viewport_set_shadow_type().
607 * Return value: the shadow type
610 gtk_viewport_get_shadow_type (GtkViewport *viewport)
612 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
614 return viewport->priv->shadow_type;
618 * gtk_viewport_get_bin_window:
619 * @viewport: a #GtkViewport
621 * Gets the bin window of the #GtkViewport.
623 * Return value: (transfer none): a #GdkWindow
628 gtk_viewport_get_bin_window (GtkViewport *viewport)
630 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
632 return viewport->priv->bin_window;
636 * gtk_viewport_get_view_window:
637 * @viewport: a #GtkViewport
639 * Gets the view window of the #GtkViewport.
641 * Return value: (transfer none): a #GdkWindow
646 gtk_viewport_get_view_window (GtkViewport *viewport)
648 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
650 return viewport->priv->view_window;
654 gtk_viewport_realize (GtkWidget *widget)
656 GtkViewport *viewport = GTK_VIEWPORT (widget);
657 GtkViewportPrivate *priv = viewport->priv;
658 GtkBin *bin = GTK_BIN (widget);
659 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
660 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
661 GtkAllocation allocation;
662 GtkAllocation view_allocation;
666 GdkWindowAttr attributes;
667 gint attributes_mask;
671 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
673 gtk_widget_set_realized (widget, TRUE);
675 gtk_widget_get_allocation (widget, &allocation);
677 attributes.x = allocation.x + border_width;
678 attributes.y = allocation.y + border_width;
679 attributes.width = allocation.width - border_width * 2;
680 attributes.height = allocation.height - border_width * 2;
681 attributes.window_type = GDK_WINDOW_CHILD;
682 attributes.wclass = GDK_INPUT_OUTPUT;
683 attributes.visual = gtk_widget_get_visual (widget);
685 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
686 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
688 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
690 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
692 window = gdk_window_new (gtk_widget_get_parent_window (widget),
693 &attributes, attributes_mask);
694 gtk_widget_set_window (widget, window);
695 gdk_window_set_user_data (window, viewport);
697 viewport_get_view_allocation (viewport, &view_allocation);
699 attributes.x = view_allocation.x;
700 attributes.y = view_allocation.y;
701 attributes.width = view_allocation.width;
702 attributes.height = view_allocation.height;
703 attributes.event_mask = 0;
705 priv->view_window = gdk_window_new (window,
706 &attributes, attributes_mask);
707 gdk_window_set_user_data (priv->view_window, viewport);
709 attributes.x = - gtk_adjustment_get_value (hadjustment);
710 attributes.y = - gtk_adjustment_get_value (vadjustment);
711 attributes.width = gtk_adjustment_get_upper (hadjustment);
712 attributes.height = gtk_adjustment_get_upper (vadjustment);
714 attributes.event_mask = event_mask;
716 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
717 gdk_window_set_user_data (priv->bin_window, viewport);
719 child = gtk_bin_get_child (bin);
721 gtk_widget_set_parent_window (child, priv->bin_window);
723 gtk_widget_style_attach (widget);
724 style = gtk_widget_get_style (widget);
725 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
726 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
728 gdk_window_show (priv->bin_window);
729 gdk_window_show (priv->view_window);
733 gtk_viewport_unrealize (GtkWidget *widget)
735 GtkViewport *viewport = GTK_VIEWPORT (widget);
736 GtkViewportPrivate *priv = viewport->priv;
738 gdk_window_set_user_data (priv->view_window, NULL);
739 gdk_window_destroy (priv->view_window);
740 priv->view_window = NULL;
742 gdk_window_set_user_data (priv->bin_window, NULL);
743 gdk_window_destroy (priv->bin_window);
744 priv->bin_window = NULL;
746 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
750 gtk_viewport_draw (GtkWidget *widget,
753 GtkViewport *viewport = GTK_VIEWPORT (widget);
754 GtkViewportPrivate *priv = viewport->priv;
757 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
759 gtk_paint_shadow (gtk_widget_get_style (widget),
761 GTK_STATE_NORMAL, priv->shadow_type,
764 gdk_window_get_width (gtk_widget_get_window (widget)),
765 gdk_window_get_height (gtk_widget_get_window (widget)));
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_paint_flat_box (gtk_widget_get_style (widget), cr,
787 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
788 widget, "viewportbin",
790 gdk_window_get_width (priv->bin_window),
791 gdk_window_get_height (priv->bin_window));
793 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
800 gtk_viewport_add (GtkContainer *container,
803 GtkBin *bin = GTK_BIN (container);
804 GtkViewport *viewport = GTK_VIEWPORT (bin);
805 GtkViewportPrivate *priv = viewport->priv;
807 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
809 gtk_widget_set_parent_window (child, priv->bin_window);
811 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
815 gtk_viewport_size_allocate (GtkWidget *widget,
816 GtkAllocation *allocation)
818 GtkAllocation widget_allocation;
819 GtkViewport *viewport = GTK_VIEWPORT (widget);
820 GtkViewportPrivate *priv = viewport->priv;
821 GtkBin *bin = GTK_BIN (widget);
823 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
824 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
825 GtkAllocation child_allocation;
828 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
830 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
831 * redraw the shadow correctly.
833 gtk_widget_get_allocation (widget, &widget_allocation);
834 if (gtk_widget_get_mapped (widget) &&
835 priv->shadow_type != GTK_SHADOW_NONE &&
836 (widget_allocation.width != allocation->width ||
837 widget_allocation.height != allocation->height))
838 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
840 gtk_widget_set_allocation (widget, allocation);
842 g_object_freeze_notify (G_OBJECT (hadjustment));
843 g_object_freeze_notify (G_OBJECT (vadjustment));
845 viewport_set_hadjustment_values (viewport);
846 viewport_set_vadjustment_values (viewport);
848 child_allocation.x = 0;
849 child_allocation.y = 0;
850 child_allocation.width = gtk_adjustment_get_upper (hadjustment);
851 child_allocation.height = gtk_adjustment_get_upper (vadjustment);
852 if (gtk_widget_get_realized (widget))
854 GtkAllocation view_allocation;
856 gdk_window_move_resize (gtk_widget_get_window (widget),
857 allocation->x + border_width,
858 allocation->y + border_width,
859 allocation->width - border_width * 2,
860 allocation->height - border_width * 2);
862 viewport_get_view_allocation (viewport, &view_allocation);
863 gdk_window_move_resize (priv->view_window,
866 view_allocation.width,
867 view_allocation.height);
868 gdk_window_move_resize (priv->bin_window,
869 - gtk_adjustment_get_value (hadjustment),
870 - gtk_adjustment_get_value (vadjustment),
871 child_allocation.width,
872 child_allocation.height);
875 child = gtk_bin_get_child (bin);
876 if (child && gtk_widget_get_visible (child))
877 gtk_widget_size_allocate (child, &child_allocation);
879 g_object_thaw_notify (G_OBJECT (hadjustment));
880 g_object_thaw_notify (G_OBJECT (vadjustment));
884 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
887 GtkViewport *viewport = GTK_VIEWPORT (data);
888 GtkViewportPrivate *priv = viewport->priv;
889 GtkBin *bin = GTK_BIN (data);
892 child = gtk_bin_get_child (bin);
893 if (child && gtk_widget_get_visible (child) &&
894 gtk_widget_get_realized (GTK_WIDGET (viewport)))
896 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
897 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
901 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
902 new_x = - gtk_adjustment_get_value (hadjustment);
903 new_y = - gtk_adjustment_get_value (vadjustment);
905 if (new_x != old_x || new_y != old_y)
907 gdk_window_move (priv->bin_window, new_x, new_y);
908 gdk_window_process_updates (priv->bin_window, TRUE);
914 gtk_viewport_style_set (GtkWidget *widget,
915 GtkStyle *previous_style)
917 if (gtk_widget_get_realized (widget) &&
918 gtk_widget_get_has_window (widget))
921 GtkViewport *viewport = GTK_VIEWPORT (widget);
922 GtkViewportPrivate *priv = viewport->priv;
924 style = gtk_widget_get_style (widget);
925 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
926 gtk_style_set_background (style,
927 gtk_widget_get_window (widget),
928 gtk_widget_get_state (widget));
934 gtk_viewport_get_preferred_size (GtkWidget *widget,
935 GtkOrientation orientation,
939 GtkViewport *viewport = GTK_VIEWPORT (widget);
940 GtkViewportPrivate *priv = viewport->priv;
943 gint child_min, child_nat;
944 gint minimum, natural;
946 child = gtk_bin_get_child (GTK_BIN (widget));
948 /* XXX This should probably be (border_width * 2); but GTK+ has
949 * been doing this with a single border for a while now...
951 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
953 if (priv->shadow_type != GTK_SHADOW_NONE)
955 style = gtk_widget_get_style (GTK_WIDGET (widget));
956 if (orientation == GTK_ORIENTATION_HORIZONTAL)
957 minimum += 2 * style->xthickness;
959 minimum += 2 * style->ythickness;
964 if (child && gtk_widget_get_visible (child))
966 if (orientation == GTK_ORIENTATION_HORIZONTAL)
967 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
969 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
971 minimum += child_min;
972 natural += child_nat;
976 *minimum_size = minimum;
979 *natural_size = natural;
983 gtk_viewport_get_preferred_width (GtkWidget *widget,
987 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
991 gtk_viewport_get_preferred_height (GtkWidget *widget,
995 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);