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,
114 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
115 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
118 gtk_viewport_class_init (GtkViewportClass *class)
120 GObjectClass *gobject_class;
121 GtkWidgetClass *widget_class;
122 GtkContainerClass *container_class;
124 gobject_class = G_OBJECT_CLASS (class);
125 widget_class = (GtkWidgetClass*) class;
126 container_class = (GtkContainerClass*) class;
128 gobject_class->set_property = gtk_viewport_set_property;
129 gobject_class->get_property = gtk_viewport_get_property;
131 widget_class->destroy = gtk_viewport_destroy;
132 widget_class->realize = gtk_viewport_realize;
133 widget_class->unrealize = gtk_viewport_unrealize;
134 widget_class->draw = gtk_viewport_draw;
135 widget_class->size_allocate = gtk_viewport_size_allocate;
136 widget_class->style_updated = gtk_viewport_style_updated;
137 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
138 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
140 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_VIEWPORT);
142 container_class->add = gtk_viewport_add;
144 /* GtkScrollable implementation */
145 g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment");
146 g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment");
147 g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
148 g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
150 g_object_class_install_property (gobject_class,
152 g_param_spec_enum ("shadow-type",
154 P_("Determines how the shadowed box around the viewport is drawn"),
155 GTK_TYPE_SHADOW_TYPE,
157 GTK_PARAM_READWRITE));
159 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
163 gtk_viewport_set_property (GObject *object,
168 GtkViewport *viewport;
170 viewport = GTK_VIEWPORT (object);
174 case PROP_HADJUSTMENT:
175 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
177 case PROP_VADJUSTMENT:
178 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
180 case PROP_HSCROLL_POLICY:
181 viewport->priv->hscroll_policy = g_value_get_enum (value);
182 gtk_widget_queue_resize (GTK_WIDGET (viewport));
184 case PROP_VSCROLL_POLICY:
185 viewport->priv->vscroll_policy = g_value_get_enum (value);
186 gtk_widget_queue_resize (GTK_WIDGET (viewport));
188 case PROP_SHADOW_TYPE:
189 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 gtk_viewport_get_property (GObject *object,
203 GtkViewport *viewport = GTK_VIEWPORT (object);
204 GtkViewportPrivate *priv = viewport->priv;
208 case PROP_HADJUSTMENT:
209 g_value_set_object (value, priv->hadjustment);
211 case PROP_VADJUSTMENT:
212 g_value_set_object (value, priv->vadjustment);
214 case PROP_HSCROLL_POLICY:
215 g_value_set_enum (value, priv->hscroll_policy);
217 case PROP_VSCROLL_POLICY:
218 g_value_set_enum (value, priv->vscroll_policy);
220 case PROP_SHADOW_TYPE:
221 g_value_set_enum (value, priv->shadow_type);
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 gtk_viewport_init (GtkViewport *viewport)
232 GtkViewportPrivate *priv;
234 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
237 priv = viewport->priv;
239 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
241 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
242 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
244 priv->shadow_type = GTK_SHADOW_IN;
245 priv->view_window = NULL;
246 priv->bin_window = NULL;
247 priv->hadjustment = NULL;
248 priv->vadjustment = NULL;
253 * @hadjustment: horizontal adjustment
254 * @vadjustment: vertical adjustment
256 * Creates a new #GtkViewport with the given adjustments.
258 * Returns: a new #GtkViewport
261 gtk_viewport_new (GtkAdjustment *hadjustment,
262 GtkAdjustment *vadjustment)
266 viewport = g_object_new (GTK_TYPE_VIEWPORT,
267 "hadjustment", hadjustment,
268 "vadjustment", vadjustment,
274 #define ADJUSTMENT_POINTER(viewport, orientation) \
275 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
276 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
279 viewport_disconnect_adjustment (GtkViewport *viewport,
280 GtkOrientation orientation)
282 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
286 g_signal_handlers_disconnect_by_func (*adjustmentp,
287 gtk_viewport_adjustment_value_changed,
289 g_object_unref (*adjustmentp);
295 gtk_viewport_destroy (GtkWidget *widget)
297 GtkViewport *viewport = GTK_VIEWPORT (widget);
299 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
300 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
302 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
306 viewport_get_view_allocation (GtkViewport *viewport,
307 GtkAllocation *view_allocation)
309 GtkViewportPrivate *priv = viewport->priv;
310 GtkWidget *widget = GTK_WIDGET (viewport);
311 GtkAllocation allocation;
312 GtkStyleContext *context;
314 GtkBorder padding, border;
317 gtk_widget_get_allocation (widget, &allocation);
318 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
320 view_allocation->x = 0;
321 view_allocation->y = 0;
323 context = gtk_widget_get_style_context (widget);
324 state = gtk_widget_get_state_flags (widget);
325 gtk_style_context_get_padding (context, state, &padding);
326 gtk_style_context_get_border (context, state, &border);
328 if (priv->shadow_type != GTK_SHADOW_NONE)
330 view_allocation->x = border.left;
331 view_allocation->y = border.top;
334 view_allocation->x += padding.left;
335 view_allocation->y += padding.right;
336 view_allocation->width = MAX (1, allocation.width - padding.left - padding.right - border_width * 2);
337 view_allocation->height = MAX (1, allocation.height - padding.top - padding.bottom - border_width * 2);
339 if (priv->shadow_type != GTK_SHADOW_NONE)
341 view_allocation->width = MAX (1, view_allocation->width - border.left - border.right);
342 view_allocation->height = MAX (1, view_allocation->height - border.top - border.bottom);
347 * gtk_viewport_get_hadjustment:
348 * @viewport: a #GtkViewport.
350 * Returns the horizontal adjustment of the viewport.
352 * Return value: (transfer none): the horizontal adjustment of @viewport.
354 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
357 gtk_viewport_get_hadjustment (GtkViewport *viewport)
359 GtkViewportPrivate *priv;
361 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
363 priv = viewport->priv;
365 if (!priv->hadjustment)
366 gtk_viewport_set_hadjustment (viewport, NULL);
368 return priv->hadjustment;
372 * gtk_viewport_get_vadjustment:
373 * @viewport: a #GtkViewport.
375 * Returns the vertical adjustment of the viewport.
377 * Return value: (transfer none): the vertical adjustment of @viewport.
379 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
382 gtk_viewport_get_vadjustment (GtkViewport *viewport)
384 GtkViewportPrivate *priv;
386 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
388 priv = viewport->priv;
390 if (!priv->vadjustment)
391 gtk_viewport_set_vadjustment (viewport, NULL);
393 return priv->vadjustment;
397 viewport_set_hadjustment_values (GtkViewport *viewport)
399 GtkBin *bin = GTK_BIN (viewport);
400 GtkAllocation view_allocation;
401 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
403 gdouble upper, value;
405 viewport_get_view_allocation (viewport, &view_allocation);
407 child = gtk_bin_get_child (bin);
408 if (child && gtk_widget_get_visible (child))
410 gint minimum_width, natural_width;
413 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
414 gtk_widget_get_preferred_height (child, &scroll_height, NULL);
416 gtk_widget_get_preferred_height (child, NULL, &scroll_height);
418 gtk_widget_get_preferred_width_for_height (child,
419 MAX (view_allocation.height, scroll_height),
423 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
424 upper = MAX (minimum_width, view_allocation.width);
426 upper = MAX (natural_width, view_allocation.width);
429 upper = view_allocation.width;
431 value = gtk_adjustment_get_value (hadjustment);
432 /* We clamp to the left in RTL mode */
433 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
435 gdouble dist = gtk_adjustment_get_upper (hadjustment)
437 - gtk_adjustment_get_page_size (hadjustment);
438 value = upper - dist - view_allocation.width;
441 gtk_adjustment_configure (hadjustment,
445 view_allocation.width * 0.1,
446 view_allocation.width * 0.9,
447 view_allocation.width);
451 viewport_set_vadjustment_values (GtkViewport *viewport)
453 GtkBin *bin = GTK_BIN (viewport);
454 GtkAllocation view_allocation;
455 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
459 viewport_get_view_allocation (viewport, &view_allocation);
461 child = gtk_bin_get_child (bin);
462 if (child && gtk_widget_get_visible (child))
464 gint minimum_height, natural_height;
467 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
468 gtk_widget_get_preferred_width (child, &scroll_width, NULL);
470 gtk_widget_get_preferred_width (child, NULL, &scroll_width);
472 gtk_widget_get_preferred_height_for_width (child,
473 MAX (view_allocation.width, scroll_width),
477 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
478 upper = MAX (minimum_height, view_allocation.height);
480 upper = MAX (natural_height, view_allocation.height);
483 upper = view_allocation.height;
485 gtk_adjustment_configure (vadjustment,
486 gtk_adjustment_get_value (vadjustment),
489 view_allocation.height * 0.1,
490 view_allocation.height * 0.9,
491 view_allocation.height);
495 viewport_set_adjustment (GtkViewport *viewport,
496 GtkOrientation orientation,
497 GtkAdjustment *adjustment)
499 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
501 if (adjustment && adjustment == *adjustmentp)
505 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
506 viewport_disconnect_adjustment (viewport, orientation);
507 *adjustmentp = adjustment;
508 g_object_ref_sink (adjustment);
510 if (orientation == GTK_ORIENTATION_HORIZONTAL)
511 viewport_set_hadjustment_values (viewport);
513 viewport_set_vadjustment_values (viewport);
515 g_signal_connect (adjustment, "value-changed",
516 G_CALLBACK (gtk_viewport_adjustment_value_changed),
519 gtk_viewport_adjustment_value_changed (adjustment, viewport);
523 * gtk_viewport_set_hadjustment:
524 * @viewport: a #GtkViewport.
525 * @adjustment: (allow-none): a #GtkAdjustment.
527 * Sets the horizontal adjustment of the viewport.
529 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
532 gtk_viewport_set_hadjustment (GtkViewport *viewport,
533 GtkAdjustment *adjustment)
535 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
537 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
539 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
541 g_object_notify (G_OBJECT (viewport), "hadjustment");
545 * gtk_viewport_set_vadjustment:
546 * @viewport: a #GtkViewport.
547 * @adjustment: (allow-none): a #GtkAdjustment.
549 * Sets the vertical adjustment of the viewport.
551 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
554 gtk_viewport_set_vadjustment (GtkViewport *viewport,
555 GtkAdjustment *adjustment)
557 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
559 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
561 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
563 g_object_notify (G_OBJECT (viewport), "vadjustment");
567 * gtk_viewport_set_shadow_type:
568 * @viewport: a #GtkViewport.
569 * @type: the new shadow type.
571 * Sets the shadow type of the viewport.
574 gtk_viewport_set_shadow_type (GtkViewport *viewport,
577 GtkViewportPrivate *priv;
578 GtkAllocation allocation;
581 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
583 widget = GTK_WIDGET (viewport);
584 priv = viewport->priv;
586 if ((GtkShadowType) priv->shadow_type != type)
588 priv->shadow_type = type;
590 if (gtk_widget_get_visible (widget))
592 gtk_widget_get_allocation (widget, &allocation);
593 gtk_widget_size_allocate (widget, &allocation);
594 gtk_widget_set_allocation (widget, &allocation);
595 gtk_widget_queue_draw (widget);
598 g_object_notify (G_OBJECT (viewport), "shadow-type");
603 * gtk_viewport_get_shadow_type:
604 * @viewport: a #GtkViewport
606 * Gets the shadow type of the #GtkViewport. See
607 * gtk_viewport_set_shadow_type().
609 * Return value: the shadow type
612 gtk_viewport_get_shadow_type (GtkViewport *viewport)
614 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
616 return viewport->priv->shadow_type;
620 * gtk_viewport_get_bin_window:
621 * @viewport: a #GtkViewport
623 * Gets the bin window of the #GtkViewport.
625 * Return value: (transfer none): a #GdkWindow
630 gtk_viewport_get_bin_window (GtkViewport *viewport)
632 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
634 return viewport->priv->bin_window;
638 * gtk_viewport_get_view_window:
639 * @viewport: a #GtkViewport
641 * Gets the view window of the #GtkViewport.
643 * Return value: (transfer none): a #GdkWindow
648 gtk_viewport_get_view_window (GtkViewport *viewport)
650 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
652 return viewport->priv->view_window;
656 gtk_viewport_realize (GtkWidget *widget)
658 GtkViewport *viewport = GTK_VIEWPORT (widget);
659 GtkViewportPrivate *priv = viewport->priv;
660 GtkBin *bin = GTK_BIN (widget);
661 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
662 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
663 GtkAllocation allocation;
664 GtkAllocation view_allocation;
665 GtkStyleContext *context;
668 GdkWindowAttr attributes;
669 gint attributes_mask;
673 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
675 gtk_widget_set_realized (widget, TRUE);
677 gtk_widget_get_allocation (widget, &allocation);
679 attributes.x = allocation.x + border_width;
680 attributes.y = allocation.y + border_width;
681 attributes.width = allocation.width - border_width * 2;
682 attributes.height = allocation.height - border_width * 2;
683 attributes.window_type = GDK_WINDOW_CHILD;
684 attributes.wclass = GDK_INPUT_OUTPUT;
685 attributes.visual = gtk_widget_get_visual (widget);
687 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
688 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
690 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
692 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
694 window = gdk_window_new (gtk_widget_get_parent_window (widget),
695 &attributes, attributes_mask);
696 gtk_widget_set_window (widget, window);
697 gdk_window_set_user_data (window, viewport);
699 viewport_get_view_allocation (viewport, &view_allocation);
701 attributes.x = view_allocation.x;
702 attributes.y = view_allocation.y;
703 attributes.width = view_allocation.width;
704 attributes.height = view_allocation.height;
705 attributes.event_mask = 0;
707 priv->view_window = gdk_window_new (window,
708 &attributes, attributes_mask);
709 gdk_window_set_user_data (priv->view_window, viewport);
711 attributes.x = - gtk_adjustment_get_value (hadjustment);
712 attributes.y = - gtk_adjustment_get_value (vadjustment);
713 attributes.width = gtk_adjustment_get_upper (hadjustment);
714 attributes.height = gtk_adjustment_get_upper (vadjustment);
716 attributes.event_mask = event_mask;
718 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
719 gdk_window_set_user_data (priv->bin_window, viewport);
721 child = gtk_bin_get_child (bin);
723 gtk_widget_set_parent_window (child, priv->bin_window);
725 context = gtk_widget_get_style_context (widget);
726 gtk_style_context_set_background (context, window);
727 gtk_style_context_set_background (context, priv->bin_window);
729 gdk_window_show (priv->bin_window);
730 gdk_window_show (priv->view_window);
734 gtk_viewport_unrealize (GtkWidget *widget)
736 GtkViewport *viewport = GTK_VIEWPORT (widget);
737 GtkViewportPrivate *priv = viewport->priv;
739 gdk_window_set_user_data (priv->view_window, NULL);
740 gdk_window_destroy (priv->view_window);
741 priv->view_window = NULL;
743 gdk_window_set_user_data (priv->bin_window, NULL);
744 gdk_window_destroy (priv->bin_window);
745 priv->bin_window = NULL;
747 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
751 gtk_viewport_draw (GtkWidget *widget,
754 GtkViewport *viewport = GTK_VIEWPORT (widget);
755 GtkViewportPrivate *priv = viewport->priv;
756 GtkStyleContext *context;
759 context = gtk_widget_get_style_context (widget);
761 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
763 gtk_style_context_save (context);
764 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
766 gtk_render_frame (context, cr, 0, 0,
767 gdk_window_get_width (gtk_widget_get_window (widget)),
768 gdk_window_get_height (gtk_widget_get_window (widget)));
770 gtk_style_context_restore (context);
773 if (gtk_cairo_should_draw_window (cr, priv->view_window))
775 /* This is a cute hack to ensure the contents of bin_window are
776 * restricted to where they are visible. We only need to do this
777 * clipping when called via gtk_widget_draw() and not in expose
778 * events. And when that happens every window (including this one)
781 gdk_window_get_position (priv->view_window, &x, &y);
782 cairo_rectangle (cr, x, y,
783 gdk_window_get_width (priv->view_window),
784 gdk_window_get_height (priv->view_window));
788 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
790 gdk_window_get_position (priv->bin_window, &x, &y);
791 gtk_render_background (context, cr, x, y,
792 gdk_window_get_width (priv->bin_window),
793 gdk_window_get_height (priv->bin_window));
795 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
802 gtk_viewport_add (GtkContainer *container,
805 GtkBin *bin = GTK_BIN (container);
806 GtkViewport *viewport = GTK_VIEWPORT (bin);
807 GtkViewportPrivate *priv = viewport->priv;
809 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
811 gtk_widget_set_parent_window (child, priv->bin_window);
813 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
817 gtk_viewport_size_allocate (GtkWidget *widget,
818 GtkAllocation *allocation)
820 GtkAllocation widget_allocation;
821 GtkViewport *viewport = GTK_VIEWPORT (widget);
822 GtkViewportPrivate *priv = viewport->priv;
823 GtkBin *bin = GTK_BIN (widget);
825 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
826 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
827 GtkAllocation child_allocation;
830 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
832 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
833 * redraw the shadow correctly.
835 gtk_widget_get_allocation (widget, &widget_allocation);
836 if (gtk_widget_get_mapped (widget) &&
837 priv->shadow_type != GTK_SHADOW_NONE &&
838 (widget_allocation.width != allocation->width ||
839 widget_allocation.height != allocation->height))
840 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
842 gtk_widget_set_allocation (widget, allocation);
844 g_object_freeze_notify (G_OBJECT (hadjustment));
845 g_object_freeze_notify (G_OBJECT (vadjustment));
847 viewport_set_hadjustment_values (viewport);
848 viewport_set_vadjustment_values (viewport);
850 child_allocation.x = 0;
851 child_allocation.y = 0;
852 child_allocation.width = gtk_adjustment_get_upper (hadjustment);
853 child_allocation.height = gtk_adjustment_get_upper (vadjustment);
854 if (gtk_widget_get_realized (widget))
856 GtkAllocation view_allocation;
858 gdk_window_move_resize (gtk_widget_get_window (widget),
859 allocation->x + border_width,
860 allocation->y + border_width,
861 allocation->width - border_width * 2,
862 allocation->height - border_width * 2);
864 viewport_get_view_allocation (viewport, &view_allocation);
865 gdk_window_move_resize (priv->view_window,
868 view_allocation.width,
869 view_allocation.height);
870 gdk_window_move_resize (priv->bin_window,
871 - gtk_adjustment_get_value (hadjustment),
872 - gtk_adjustment_get_value (vadjustment),
873 child_allocation.width,
874 child_allocation.height);
877 child = gtk_bin_get_child (bin);
878 if (child && gtk_widget_get_visible (child))
879 gtk_widget_size_allocate (child, &child_allocation);
881 g_object_thaw_notify (G_OBJECT (hadjustment));
882 g_object_thaw_notify (G_OBJECT (vadjustment));
886 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
889 GtkViewport *viewport = GTK_VIEWPORT (data);
890 GtkViewportPrivate *priv = viewport->priv;
891 GtkBin *bin = GTK_BIN (data);
894 child = gtk_bin_get_child (bin);
895 if (child && gtk_widget_get_visible (child) &&
896 gtk_widget_get_realized (GTK_WIDGET (viewport)))
898 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
899 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
903 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
904 new_x = - gtk_adjustment_get_value (hadjustment);
905 new_y = - gtk_adjustment_get_value (vadjustment);
907 if (new_x != old_x || new_y != old_y)
909 gdk_window_move (priv->bin_window, new_x, new_y);
910 gdk_window_process_updates (priv->bin_window, TRUE);
916 gtk_viewport_style_updated (GtkWidget *widget)
918 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->style_updated (widget);
920 if (gtk_widget_get_realized (widget) &&
921 gtk_widget_get_has_window (widget))
923 GtkStyleContext *context;
924 GtkViewport *viewport = GTK_VIEWPORT (widget);
925 GtkViewportPrivate *priv = viewport->priv;
927 context = gtk_widget_get_style_context (widget);
928 gtk_style_context_set_background (context, priv->bin_window);
929 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
935 gtk_viewport_get_preferred_size (GtkWidget *widget,
936 GtkOrientation orientation,
940 GtkViewport *viewport = GTK_VIEWPORT (widget);
941 GtkViewportPrivate *priv = viewport->priv;
942 GtkStyleContext *context;
944 GtkBorder padding, border;
946 gint child_min, child_nat;
947 gint minimum, natural;
949 child = gtk_bin_get_child (GTK_BIN (widget));
951 /* XXX This should probably be (border_width * 2); but GTK+ has
952 * been doing this with a single border for a while now...
954 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
956 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
957 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
958 gtk_style_context_get_padding (context, state, &padding);
960 if (priv->shadow_type != GTK_SHADOW_NONE)
962 gtk_style_context_get_border (context, state, &border);
964 if (orientation == GTK_ORIENTATION_HORIZONTAL)
965 minimum += border.left + border.right;
967 minimum += border.top + border.bottom;
970 if (orientation == GTK_ORIENTATION_HORIZONTAL)
971 minimum += padding.left + padding.right;
973 minimum += padding.top + padding.bottom;
977 if (child && gtk_widget_get_visible (child))
979 if (orientation == GTK_ORIENTATION_HORIZONTAL)
980 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
982 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
984 minimum += child_min;
985 natural += child_nat;
989 *minimum_size = minimum;
992 *natural_size = natural;
996 gtk_viewport_get_preferred_width (GtkWidget *widget,
1000 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1004 gtk_viewport_get_preferred_height (GtkWidget *widget,
1008 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);