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 * #GtkGrid, #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_set_property (GObject *object,
89 static void gtk_viewport_get_property (GObject *object,
93 static void gtk_viewport_destroy (GtkWidget *widget);
94 static void gtk_viewport_realize (GtkWidget *widget);
95 static void gtk_viewport_unrealize (GtkWidget *widget);
96 static gint gtk_viewport_draw (GtkWidget *widget,
98 static void gtk_viewport_add (GtkContainer *container,
100 static void gtk_viewport_size_allocate (GtkWidget *widget,
101 GtkAllocation *allocation);
102 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
104 static void gtk_viewport_style_updated (GtkWidget *widget);
106 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
109 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
113 static void viewport_set_adjustment (GtkViewport *viewport,
114 GtkOrientation orientation,
115 GtkAdjustment *adjustment);
117 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
118 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
121 gtk_viewport_class_init (GtkViewportClass *class)
123 GObjectClass *gobject_class;
124 GtkWidgetClass *widget_class;
125 GtkContainerClass *container_class;
127 gobject_class = G_OBJECT_CLASS (class);
128 widget_class = (GtkWidgetClass*) class;
129 container_class = (GtkContainerClass*) class;
131 gobject_class->set_property = gtk_viewport_set_property;
132 gobject_class->get_property = gtk_viewport_get_property;
134 widget_class->destroy = gtk_viewport_destroy;
135 widget_class->realize = gtk_viewport_realize;
136 widget_class->unrealize = gtk_viewport_unrealize;
137 widget_class->draw = gtk_viewport_draw;
138 widget_class->size_allocate = gtk_viewport_size_allocate;
139 widget_class->style_updated = gtk_viewport_style_updated;
140 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
141 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
143 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_VIEWPORT);
145 container_class->add = gtk_viewport_add;
147 /* GtkScrollable implementation */
148 g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment");
149 g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment");
150 g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
151 g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
153 g_object_class_install_property (gobject_class,
155 g_param_spec_enum ("shadow-type",
157 P_("Determines how the shadowed box around the viewport is drawn"),
158 GTK_TYPE_SHADOW_TYPE,
160 GTK_PARAM_READWRITE));
162 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
166 gtk_viewport_set_property (GObject *object,
171 GtkViewport *viewport;
173 viewport = GTK_VIEWPORT (object);
177 case PROP_HADJUSTMENT:
178 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
180 case PROP_VADJUSTMENT:
181 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
183 case PROP_HSCROLL_POLICY:
184 viewport->priv->hscroll_policy = g_value_get_enum (value);
185 gtk_widget_queue_resize (GTK_WIDGET (viewport));
187 case PROP_VSCROLL_POLICY:
188 viewport->priv->vscroll_policy = g_value_get_enum (value);
189 gtk_widget_queue_resize (GTK_WIDGET (viewport));
191 case PROP_SHADOW_TYPE:
192 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 gtk_viewport_get_property (GObject *object,
206 GtkViewport *viewport = GTK_VIEWPORT (object);
207 GtkViewportPrivate *priv = viewport->priv;
211 case PROP_HADJUSTMENT:
212 g_value_set_object (value, priv->hadjustment);
214 case PROP_VADJUSTMENT:
215 g_value_set_object (value, priv->vadjustment);
217 case PROP_HSCROLL_POLICY:
218 g_value_set_enum (value, priv->hscroll_policy);
220 case PROP_VSCROLL_POLICY:
221 g_value_set_enum (value, priv->vscroll_policy);
223 case PROP_SHADOW_TYPE:
224 g_value_set_enum (value, priv->shadow_type);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 gtk_viewport_init (GtkViewport *viewport)
235 GtkViewportPrivate *priv;
237 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
240 priv = viewport->priv;
242 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
244 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
245 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
247 priv->shadow_type = GTK_SHADOW_IN;
248 priv->view_window = NULL;
249 priv->bin_window = NULL;
250 priv->hadjustment = NULL;
251 priv->vadjustment = NULL;
253 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, NULL);
254 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, NULL);
259 * @hadjustment: horizontal adjustment
260 * @vadjustment: vertical adjustment
262 * Creates a new #GtkViewport with the given adjustments.
264 * Returns: a new #GtkViewport
267 gtk_viewport_new (GtkAdjustment *hadjustment,
268 GtkAdjustment *vadjustment)
272 viewport = g_object_new (GTK_TYPE_VIEWPORT,
273 "hadjustment", hadjustment,
274 "vadjustment", vadjustment,
280 #define ADJUSTMENT_POINTER(viewport, orientation) \
281 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
282 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
285 viewport_disconnect_adjustment (GtkViewport *viewport,
286 GtkOrientation orientation)
288 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
292 g_signal_handlers_disconnect_by_func (*adjustmentp,
293 gtk_viewport_adjustment_value_changed,
295 g_object_unref (*adjustmentp);
301 gtk_viewport_destroy (GtkWidget *widget)
303 GtkViewport *viewport = GTK_VIEWPORT (widget);
305 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
306 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
308 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
312 viewport_get_view_allocation (GtkViewport *viewport,
313 GtkAllocation *view_allocation)
315 GtkViewportPrivate *priv = viewport->priv;
316 GtkWidget *widget = GTK_WIDGET (viewport);
317 GtkAllocation allocation;
318 GtkStyleContext *context;
320 GtkBorder padding, border;
323 gtk_widget_get_allocation (widget, &allocation);
324 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
326 view_allocation->x = 0;
327 view_allocation->y = 0;
329 context = gtk_widget_get_style_context (widget);
330 state = gtk_widget_get_state_flags (widget);
331 gtk_style_context_get_padding (context, state, &padding);
332 gtk_style_context_get_border (context, state, &border);
334 if (priv->shadow_type != GTK_SHADOW_NONE)
336 view_allocation->x = border.left;
337 view_allocation->y = border.top;
340 view_allocation->x += padding.left;
341 view_allocation->y += padding.right;
342 view_allocation->width = MAX (1, allocation.width - padding.left - padding.right - border_width * 2);
343 view_allocation->height = MAX (1, allocation.height - padding.top - padding.bottom - border_width * 2);
345 if (priv->shadow_type != GTK_SHADOW_NONE)
347 view_allocation->width = MAX (1, view_allocation->width - border.left - border.right);
348 view_allocation->height = MAX (1, view_allocation->height - border.top - border.bottom);
353 * gtk_viewport_get_hadjustment:
354 * @viewport: a #GtkViewport.
356 * Returns the horizontal adjustment of the viewport.
358 * Return value: (transfer none): the horizontal adjustment of @viewport.
360 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
363 gtk_viewport_get_hadjustment (GtkViewport *viewport)
365 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
367 return viewport->priv->hadjustment;
371 * gtk_viewport_get_vadjustment:
372 * @viewport: a #GtkViewport.
374 * Returns the vertical adjustment of the viewport.
376 * Return value: (transfer none): the vertical adjustment of @viewport.
378 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
381 gtk_viewport_get_vadjustment (GtkViewport *viewport)
383 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
385 return viewport->priv->vadjustment;
389 viewport_set_hadjustment_values (GtkViewport *viewport)
391 GtkBin *bin = GTK_BIN (viewport);
392 GtkAllocation view_allocation;
393 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
395 gdouble upper, value;
397 viewport_get_view_allocation (viewport, &view_allocation);
399 child = gtk_bin_get_child (bin);
400 if (child && gtk_widget_get_visible (child))
402 gint minimum_width, natural_width;
405 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
406 gtk_widget_get_preferred_height (child, &scroll_height, NULL);
408 gtk_widget_get_preferred_height (child, NULL, &scroll_height);
410 gtk_widget_get_preferred_width_for_height (child,
411 MAX (view_allocation.height, scroll_height),
415 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
416 upper = MAX (minimum_width, view_allocation.width);
418 upper = MAX (natural_width, view_allocation.width);
421 upper = view_allocation.width;
423 value = gtk_adjustment_get_value (hadjustment);
424 /* We clamp to the left in RTL mode */
425 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
427 gdouble dist = gtk_adjustment_get_upper (hadjustment)
429 - gtk_adjustment_get_page_size (hadjustment);
430 value = upper - dist - view_allocation.width;
433 gtk_adjustment_configure (hadjustment,
437 view_allocation.width * 0.1,
438 view_allocation.width * 0.9,
439 view_allocation.width);
443 viewport_set_vadjustment_values (GtkViewport *viewport)
445 GtkBin *bin = GTK_BIN (viewport);
446 GtkAllocation view_allocation;
447 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
451 viewport_get_view_allocation (viewport, &view_allocation);
453 child = gtk_bin_get_child (bin);
454 if (child && gtk_widget_get_visible (child))
456 gint minimum_height, natural_height;
459 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
460 gtk_widget_get_preferred_width (child, &scroll_width, NULL);
462 gtk_widget_get_preferred_width (child, NULL, &scroll_width);
464 gtk_widget_get_preferred_height_for_width (child,
465 MAX (view_allocation.width, scroll_width),
469 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
470 upper = MAX (minimum_height, view_allocation.height);
472 upper = MAX (natural_height, view_allocation.height);
475 upper = view_allocation.height;
477 gtk_adjustment_configure (vadjustment,
478 gtk_adjustment_get_value (vadjustment),
481 view_allocation.height * 0.1,
482 view_allocation.height * 0.9,
483 view_allocation.height);
487 viewport_set_adjustment (GtkViewport *viewport,
488 GtkOrientation orientation,
489 GtkAdjustment *adjustment)
491 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
493 if (adjustment && adjustment == *adjustmentp)
497 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
498 viewport_disconnect_adjustment (viewport, orientation);
499 *adjustmentp = adjustment;
500 g_object_ref_sink (adjustment);
502 if (orientation == GTK_ORIENTATION_HORIZONTAL)
503 viewport_set_hadjustment_values (viewport);
505 viewport_set_vadjustment_values (viewport);
507 g_signal_connect (adjustment, "value-changed",
508 G_CALLBACK (gtk_viewport_adjustment_value_changed),
511 gtk_viewport_adjustment_value_changed (adjustment, viewport);
515 * gtk_viewport_set_hadjustment:
516 * @viewport: a #GtkViewport.
517 * @adjustment: (allow-none): a #GtkAdjustment.
519 * Sets the horizontal adjustment of the viewport.
521 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
524 gtk_viewport_set_hadjustment (GtkViewport *viewport,
525 GtkAdjustment *adjustment)
527 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
529 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
531 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
533 g_object_notify (G_OBJECT (viewport), "hadjustment");
537 * gtk_viewport_set_vadjustment:
538 * @viewport: a #GtkViewport.
539 * @adjustment: (allow-none): a #GtkAdjustment.
541 * Sets the vertical adjustment of the viewport.
543 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
546 gtk_viewport_set_vadjustment (GtkViewport *viewport,
547 GtkAdjustment *adjustment)
549 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
551 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
553 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
555 g_object_notify (G_OBJECT (viewport), "vadjustment");
559 * gtk_viewport_set_shadow_type:
560 * @viewport: a #GtkViewport.
561 * @type: the new shadow type.
563 * Sets the shadow type of the viewport.
566 gtk_viewport_set_shadow_type (GtkViewport *viewport,
569 GtkViewportPrivate *priv;
570 GtkAllocation allocation;
573 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
575 widget = GTK_WIDGET (viewport);
576 priv = viewport->priv;
578 if ((GtkShadowType) priv->shadow_type != type)
580 priv->shadow_type = type;
582 if (gtk_widget_get_visible (widget))
584 gtk_widget_get_allocation (widget, &allocation);
585 gtk_widget_size_allocate (widget, &allocation);
586 gtk_widget_set_allocation (widget, &allocation);
587 gtk_widget_queue_draw (widget);
590 g_object_notify (G_OBJECT (viewport), "shadow-type");
595 * gtk_viewport_get_shadow_type:
596 * @viewport: a #GtkViewport
598 * Gets the shadow type of the #GtkViewport. See
599 * gtk_viewport_set_shadow_type().
601 * Return value: the shadow type
604 gtk_viewport_get_shadow_type (GtkViewport *viewport)
606 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
608 return viewport->priv->shadow_type;
612 * gtk_viewport_get_bin_window:
613 * @viewport: a #GtkViewport
615 * Gets the bin window of the #GtkViewport.
617 * Return value: (transfer none): a #GdkWindow
622 gtk_viewport_get_bin_window (GtkViewport *viewport)
624 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
626 return viewport->priv->bin_window;
630 * gtk_viewport_get_view_window:
631 * @viewport: a #GtkViewport
633 * Gets the view window of the #GtkViewport.
635 * Return value: (transfer none): a #GdkWindow
640 gtk_viewport_get_view_window (GtkViewport *viewport)
642 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
644 return viewport->priv->view_window;
648 gtk_viewport_realize (GtkWidget *widget)
650 GtkViewport *viewport = GTK_VIEWPORT (widget);
651 GtkViewportPrivate *priv = viewport->priv;
652 GtkBin *bin = GTK_BIN (widget);
653 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
654 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
655 GtkAllocation allocation;
656 GtkAllocation view_allocation;
657 GtkStyleContext *context;
660 GdkWindowAttr attributes;
661 gint attributes_mask;
665 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
667 gtk_widget_set_realized (widget, TRUE);
669 gtk_widget_get_allocation (widget, &allocation);
671 attributes.x = allocation.x + border_width;
672 attributes.y = allocation.y + border_width;
673 attributes.width = allocation.width - border_width * 2;
674 attributes.height = allocation.height - border_width * 2;
675 attributes.window_type = GDK_WINDOW_CHILD;
676 attributes.wclass = GDK_INPUT_OUTPUT;
677 attributes.visual = gtk_widget_get_visual (widget);
679 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
680 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
682 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
684 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
686 window = gdk_window_new (gtk_widget_get_parent_window (widget),
687 &attributes, attributes_mask);
688 gtk_widget_set_window (widget, window);
689 gdk_window_set_user_data (window, viewport);
691 viewport_get_view_allocation (viewport, &view_allocation);
693 attributes.x = view_allocation.x;
694 attributes.y = view_allocation.y;
695 attributes.width = view_allocation.width;
696 attributes.height = view_allocation.height;
697 attributes.event_mask = 0;
699 priv->view_window = gdk_window_new (window,
700 &attributes, attributes_mask);
701 gdk_window_set_user_data (priv->view_window, viewport);
703 attributes.x = - gtk_adjustment_get_value (hadjustment);
704 attributes.y = - gtk_adjustment_get_value (vadjustment);
705 attributes.width = gtk_adjustment_get_upper (hadjustment);
706 attributes.height = gtk_adjustment_get_upper (vadjustment);
708 attributes.event_mask = event_mask;
710 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
711 gdk_window_set_user_data (priv->bin_window, viewport);
713 child = gtk_bin_get_child (bin);
715 gtk_widget_set_parent_window (child, priv->bin_window);
717 context = gtk_widget_get_style_context (widget);
718 gtk_style_context_set_background (context, window);
719 gtk_style_context_set_background (context, priv->bin_window);
721 gdk_window_show (priv->bin_window);
722 gdk_window_show (priv->view_window);
726 gtk_viewport_unrealize (GtkWidget *widget)
728 GtkViewport *viewport = GTK_VIEWPORT (widget);
729 GtkViewportPrivate *priv = viewport->priv;
731 gdk_window_set_user_data (priv->view_window, NULL);
732 gdk_window_destroy (priv->view_window);
733 priv->view_window = NULL;
735 gdk_window_set_user_data (priv->bin_window, NULL);
736 gdk_window_destroy (priv->bin_window);
737 priv->bin_window = NULL;
739 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
743 gtk_viewport_draw (GtkWidget *widget,
746 GtkViewport *viewport = GTK_VIEWPORT (widget);
747 GtkViewportPrivate *priv = viewport->priv;
748 GtkStyleContext *context;
751 context = gtk_widget_get_style_context (widget);
753 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
755 gtk_style_context_save (context);
756 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
758 gtk_render_frame (context, cr, 0, 0,
759 gdk_window_get_width (gtk_widget_get_window (widget)),
760 gdk_window_get_height (gtk_widget_get_window (widget)));
762 gtk_style_context_restore (context);
765 if (gtk_cairo_should_draw_window (cr, priv->view_window))
767 /* This is a cute hack to ensure the contents of bin_window are
768 * restricted to where they are visible. We only need to do this
769 * clipping when called via gtk_widget_draw() and not in expose
770 * events. And when that happens every window (including this one)
773 gdk_window_get_position (priv->view_window, &x, &y);
774 cairo_rectangle (cr, x, y,
775 gdk_window_get_width (priv->view_window),
776 gdk_window_get_height (priv->view_window));
780 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
782 gdk_window_get_position (priv->bin_window, &x, &y);
783 gtk_render_background (context, cr, x, y,
784 gdk_window_get_width (priv->bin_window),
785 gdk_window_get_height (priv->bin_window));
787 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
794 gtk_viewport_add (GtkContainer *container,
797 GtkBin *bin = GTK_BIN (container);
798 GtkViewport *viewport = GTK_VIEWPORT (bin);
799 GtkViewportPrivate *priv = viewport->priv;
801 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
803 gtk_widget_set_parent_window (child, priv->bin_window);
805 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
809 gtk_viewport_size_allocate (GtkWidget *widget,
810 GtkAllocation *allocation)
812 GtkAllocation widget_allocation;
813 GtkViewport *viewport = GTK_VIEWPORT (widget);
814 GtkViewportPrivate *priv = viewport->priv;
815 GtkBin *bin = GTK_BIN (widget);
817 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
818 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
819 GtkAllocation child_allocation;
822 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
824 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
825 * redraw the shadow correctly.
827 gtk_widget_get_allocation (widget, &widget_allocation);
828 if (gtk_widget_get_mapped (widget) &&
829 priv->shadow_type != GTK_SHADOW_NONE &&
830 (widget_allocation.width != allocation->width ||
831 widget_allocation.height != allocation->height))
832 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
834 gtk_widget_set_allocation (widget, allocation);
836 g_object_freeze_notify (G_OBJECT (hadjustment));
837 g_object_freeze_notify (G_OBJECT (vadjustment));
839 viewport_set_hadjustment_values (viewport);
840 viewport_set_vadjustment_values (viewport);
842 child_allocation.x = 0;
843 child_allocation.y = 0;
844 child_allocation.width = gtk_adjustment_get_upper (hadjustment);
845 child_allocation.height = gtk_adjustment_get_upper (vadjustment);
846 if (gtk_widget_get_realized (widget))
848 GtkAllocation view_allocation;
850 gdk_window_move_resize (gtk_widget_get_window (widget),
851 allocation->x + border_width,
852 allocation->y + border_width,
853 allocation->width - border_width * 2,
854 allocation->height - border_width * 2);
856 viewport_get_view_allocation (viewport, &view_allocation);
857 gdk_window_move_resize (priv->view_window,
860 view_allocation.width,
861 view_allocation.height);
862 gdk_window_move_resize (priv->bin_window,
863 - gtk_adjustment_get_value (hadjustment),
864 - gtk_adjustment_get_value (vadjustment),
865 child_allocation.width,
866 child_allocation.height);
869 child = gtk_bin_get_child (bin);
870 if (child && gtk_widget_get_visible (child))
871 gtk_widget_size_allocate (child, &child_allocation);
873 g_object_thaw_notify (G_OBJECT (hadjustment));
874 g_object_thaw_notify (G_OBJECT (vadjustment));
878 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
881 GtkViewport *viewport = GTK_VIEWPORT (data);
882 GtkViewportPrivate *priv = viewport->priv;
883 GtkBin *bin = GTK_BIN (data);
886 child = gtk_bin_get_child (bin);
887 if (child && gtk_widget_get_visible (child) &&
888 gtk_widget_get_realized (GTK_WIDGET (viewport)))
890 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
891 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
895 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
896 new_x = - gtk_adjustment_get_value (hadjustment);
897 new_y = - gtk_adjustment_get_value (vadjustment);
899 if (new_x != old_x || new_y != old_y)
901 gdk_window_move (priv->bin_window, new_x, new_y);
902 gdk_window_process_updates (priv->bin_window, TRUE);
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);