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 "gtkmarshalers.h"
30 #include "gtktypeutils.h"
31 #include "gtkscrollable.h"
32 #undef GTK_DISABLE_DEPRECATED
33 #include "gtkviewport.h"
34 #define GTK_DISABLE_DEPRECATED
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 gint min_display_width;
65 gint min_display_height;
66 GtkShadowType shadow_type;
68 GdkWindow *bin_window;
69 GdkWindow *view_window;
76 PROP_MIN_DISPLAY_WIDTH,
77 PROP_MIN_DISPLAY_HEIGHT,
82 static void gtk_viewport_finalize (GObject *object);
83 static void gtk_viewport_set_property (GObject *object,
87 static void gtk_viewport_get_property (GObject *object,
91 static void gtk_viewport_destroy (GtkWidget *widget);
92 static void gtk_viewport_realize (GtkWidget *widget);
93 static void gtk_viewport_unrealize (GtkWidget *widget);
94 static gint gtk_viewport_draw (GtkWidget *widget,
96 static void gtk_viewport_add (GtkContainer *container,
98 static void gtk_viewport_size_allocate (GtkWidget *widget,
99 GtkAllocation *allocation);
100 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
102 static void gtk_viewport_style_set (GtkWidget *widget,
103 GtkStyle *previous_style);
105 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
108 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
113 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
114 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
117 gtk_viewport_class_init (GtkViewportClass *class)
119 GObjectClass *gobject_class;
120 GtkWidgetClass *widget_class;
121 GtkContainerClass *container_class;
123 gobject_class = G_OBJECT_CLASS (class);
124 widget_class = (GtkWidgetClass*) class;
125 container_class = (GtkContainerClass*) class;
127 gobject_class->finalize = gtk_viewport_finalize;
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_set = gtk_viewport_style_set;
137 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
138 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
140 container_class->add = gtk_viewport_add;
142 /* GtkScrollable implementation */
143 g_object_class_override_property (gobject_class,
146 g_object_class_override_property (gobject_class,
149 g_object_class_override_property (gobject_class,
150 PROP_MIN_DISPLAY_WIDTH,
151 "min-display-width");
152 g_object_class_override_property (gobject_class,
153 PROP_MIN_DISPLAY_HEIGHT,
154 "min-display-height");
156 g_object_class_install_property (gobject_class,
158 g_param_spec_enum ("shadow-type",
160 P_("Determines how the shadowed box around the viewport is drawn"),
161 GTK_TYPE_SHADOW_TYPE,
163 GTK_PARAM_READWRITE));
165 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
169 gtk_viewport_set_property (GObject *object,
174 GtkViewport *viewport;
176 viewport = GTK_VIEWPORT (object);
180 case PROP_HADJUSTMENT:
181 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
183 case PROP_VADJUSTMENT:
184 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
186 case PROP_MIN_DISPLAY_WIDTH:
187 viewport->priv->min_display_width = g_value_get_int (value);
189 case PROP_MIN_DISPLAY_HEIGHT:
190 viewport->priv->min_display_height = g_value_get_int (value);
192 case PROP_SHADOW_TYPE:
193 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 gtk_viewport_get_property (GObject *object,
207 GtkViewport *viewport = GTK_VIEWPORT (object);
208 GtkViewportPrivate *priv = viewport->priv;
212 case PROP_HADJUSTMENT:
213 g_value_set_object (value, priv->hadjustment);
215 case PROP_VADJUSTMENT:
216 g_value_set_object (value, priv->vadjustment);
218 case PROP_MIN_DISPLAY_WIDTH:
219 g_value_set_int (value, priv->min_display_width);
221 case PROP_MIN_DISPLAY_HEIGHT:
222 g_value_set_int (value, priv->min_display_height);
224 case PROP_SHADOW_TYPE:
225 g_value_set_enum (value, priv->shadow_type);
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 gtk_viewport_init (GtkViewport *viewport)
236 GtkViewportPrivate *priv;
238 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
241 priv = viewport->priv;
243 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
245 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
246 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
248 priv->shadow_type = GTK_SHADOW_IN;
249 priv->view_window = NULL;
250 priv->bin_window = NULL;
251 priv->hadjustment = NULL;
252 priv->vadjustment = NULL;
253 priv->min_display_width = -1;
254 priv->min_display_height = -1;
259 * @hadjustment: horizontal adjustment.
260 * @vadjustment: vertical adjustment.
261 * @returns: a new #GtkViewport.
263 * Creates a new #GtkViewport with the given adjustments.
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_finalize (GObject *object)
303 GtkViewport *viewport = GTK_VIEWPORT (object);
305 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
306 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
308 G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
312 gtk_viewport_destroy (GtkWidget *widget)
314 GtkViewport *viewport = GTK_VIEWPORT (widget);
316 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
317 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
319 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
323 * gtk_viewport_get_hadjustment:
324 * @viewport: a #GtkViewport.
326 * Returns the horizontal adjustment of the viewport.
328 * Return value: (transfer none): the horizontal adjustment of @viewport.
330 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
333 gtk_viewport_get_hadjustment (GtkViewport *viewport)
335 GtkViewportPrivate *priv;
337 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
339 priv = viewport->priv;
341 if (!priv->hadjustment)
342 gtk_viewport_set_hadjustment (viewport, NULL);
344 return priv->hadjustment;
348 * gtk_viewport_get_vadjustment:
349 * @viewport: a #GtkViewport.
351 * Returns the vertical adjustment of the viewport.
353 * Return value: (transfer none): the vertical adjustment of @viewport.
355 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
358 gtk_viewport_get_vadjustment (GtkViewport *viewport)
360 GtkViewportPrivate *priv;
362 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
364 priv = viewport->priv;
366 if (!priv->vadjustment)
367 gtk_viewport_set_vadjustment (viewport, NULL);
369 return priv->vadjustment;
373 viewport_get_view_allocation (GtkViewport *viewport,
374 GtkAllocation *view_allocation)
376 GtkViewportPrivate *priv = viewport->priv;
378 GtkWidget *widget = GTK_WIDGET (viewport);
379 GtkAllocation allocation;
382 gtk_widget_get_allocation (widget, &allocation);
383 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
385 view_allocation->x = 0;
386 view_allocation->y = 0;
388 if (priv->shadow_type != GTK_SHADOW_NONE)
390 style = gtk_widget_get_style (widget);
391 view_allocation->x = style->xthickness;
392 view_allocation->y = style->ythickness;
395 view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
396 view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
400 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
401 gboolean *value_changed)
403 gdouble value = adjustment->value;
405 value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
406 if (value != adjustment->value)
408 adjustment->value = value;
410 *value_changed = TRUE;
412 else if (value_changed)
413 *value_changed = FALSE;
417 viewport_set_hadjustment_values (GtkViewport *viewport,
418 gboolean *value_changed)
420 GtkBin *bin = GTK_BIN (viewport);
421 GtkAllocation view_allocation;
422 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
424 gdouble old_page_size;
428 viewport_get_view_allocation (viewport, &view_allocation);
430 old_page_size = hadjustment->page_size;
431 old_upper = hadjustment->upper;
432 old_value = hadjustment->value;
433 hadjustment->page_size = view_allocation.width;
434 hadjustment->step_increment = view_allocation.width * 0.1;
435 hadjustment->page_increment = view_allocation.width * 0.9;
437 hadjustment->lower = 0;
439 child = gtk_bin_get_child (bin);
440 if (child && gtk_widget_get_visible (child))
444 gtk_widget_get_preferred_width_for_height (child,
445 view_allocation.height,
448 hadjustment->upper = MAX (minimum_width, view_allocation.width);
451 hadjustment->upper = view_allocation.width;
453 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
455 gdouble dist = old_upper - (old_value + old_page_size);
456 hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
457 viewport_reclamp_adjustment (hadjustment, value_changed);
458 *value_changed = (old_value != hadjustment->value);
461 viewport_reclamp_adjustment (hadjustment, value_changed);
465 viewport_set_vadjustment_values (GtkViewport *viewport,
466 gboolean *value_changed)
468 GtkBin *bin = GTK_BIN (viewport);
469 GtkAllocation view_allocation;
470 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
473 viewport_get_view_allocation (viewport, &view_allocation);
475 vadjustment->page_size = view_allocation.height;
476 vadjustment->step_increment = view_allocation.height * 0.1;
477 vadjustment->page_increment = view_allocation.height * 0.9;
479 vadjustment->lower = 0;
481 child = gtk_bin_get_child (bin);
482 if (child && gtk_widget_get_visible (child))
486 gtk_widget_get_preferred_height_for_width (child,
487 view_allocation.width,
491 vadjustment->upper = MAX (minimum_height, view_allocation.height);
494 vadjustment->upper = view_allocation.height;
496 viewport_reclamp_adjustment (vadjustment, value_changed);
500 viewport_set_adjustment (GtkViewport *viewport,
501 GtkOrientation orientation,
502 GtkAdjustment *adjustment)
504 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
505 gboolean value_changed;
507 if (adjustment && adjustment == *adjustmentp)
511 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
512 viewport_disconnect_adjustment (viewport, orientation);
513 *adjustmentp = adjustment;
514 g_object_ref_sink (adjustment);
516 if (orientation == GTK_ORIENTATION_HORIZONTAL)
517 viewport_set_hadjustment_values (viewport, &value_changed);
519 viewport_set_vadjustment_values (viewport, &value_changed);
521 g_signal_connect (adjustment, "value-changed",
522 G_CALLBACK (gtk_viewport_adjustment_value_changed),
525 gtk_adjustment_changed (adjustment);
528 gtk_adjustment_value_changed (adjustment);
530 gtk_viewport_adjustment_value_changed (adjustment, viewport);
534 * gtk_viewport_set_hadjustment:
535 * @viewport: a #GtkViewport.
536 * @adjustment: (allow-none): a #GtkAdjustment.
538 * Sets the horizontal adjustment of the viewport.
540 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
543 gtk_viewport_set_hadjustment (GtkViewport *viewport,
544 GtkAdjustment *adjustment)
546 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
548 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
550 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
552 g_object_notify (G_OBJECT (viewport), "hadjustment");
556 * gtk_viewport_set_vadjustment:
557 * @viewport: a #GtkViewport.
558 * @adjustment: (allow-none): a #GtkAdjustment.
560 * Sets the vertical adjustment of the viewport.
562 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
565 gtk_viewport_set_vadjustment (GtkViewport *viewport,
566 GtkAdjustment *adjustment)
568 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
570 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
572 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
574 g_object_notify (G_OBJECT (viewport), "vadjustment");
578 * gtk_viewport_set_shadow_type:
579 * @viewport: a #GtkViewport.
580 * @type: the new shadow type.
582 * Sets the shadow type of the viewport.
585 gtk_viewport_set_shadow_type (GtkViewport *viewport,
588 GtkViewportPrivate *priv;
589 GtkAllocation allocation;
592 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
594 widget = GTK_WIDGET (viewport);
595 priv = viewport->priv;
597 if ((GtkShadowType) priv->shadow_type != type)
599 priv->shadow_type = type;
601 if (gtk_widget_get_visible (widget))
603 gtk_widget_get_allocation (widget, &allocation);
604 gtk_widget_size_allocate (widget, &allocation);
605 gtk_widget_set_allocation (widget, &allocation);
606 gtk_widget_queue_draw (widget);
609 g_object_notify (G_OBJECT (viewport), "shadow-type");
614 * gtk_viewport_get_shadow_type:
615 * @viewport: a #GtkViewport
617 * Gets the shadow type of the #GtkViewport. See
618 * gtk_viewport_set_shadow_type().
620 * Return value: the shadow type
623 gtk_viewport_get_shadow_type (GtkViewport *viewport)
625 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
627 return viewport->priv->shadow_type;
631 * gtk_viewport_get_bin_window:
632 * @viewport: a #GtkViewport
634 * Gets the bin window of the #GtkViewport.
636 * Return value: (transfer none): a #GdkWindow
641 gtk_viewport_get_bin_window (GtkViewport *viewport)
643 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
645 return viewport->priv->bin_window;
649 * gtk_viewport_get_view_window:
650 * @viewport: a #GtkViewport
652 * Gets the view window of the #GtkViewport.
654 * Return value: (transfer none): a #GdkWindow
659 gtk_viewport_get_view_window (GtkViewport *viewport)
661 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
663 return viewport->priv->view_window;
667 gtk_viewport_realize (GtkWidget *widget)
669 GtkViewport *viewport = GTK_VIEWPORT (widget);
670 GtkViewportPrivate *priv = viewport->priv;
671 GtkBin *bin = GTK_BIN (widget);
672 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
673 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
674 GtkAllocation allocation;
675 GtkAllocation view_allocation;
679 GdkWindowAttr attributes;
680 gint attributes_mask;
684 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
686 gtk_widget_set_realized (widget, TRUE);
688 gtk_widget_get_allocation (widget, &allocation);
690 attributes.x = allocation.x + border_width;
691 attributes.y = allocation.y + border_width;
692 attributes.width = allocation.width - border_width * 2;
693 attributes.height = allocation.height - border_width * 2;
694 attributes.window_type = GDK_WINDOW_CHILD;
695 attributes.wclass = GDK_INPUT_OUTPUT;
696 attributes.visual = gtk_widget_get_visual (widget);
698 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
699 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
701 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
703 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
705 window = gdk_window_new (gtk_widget_get_parent_window (widget),
706 &attributes, attributes_mask);
707 gtk_widget_set_window (widget, window);
708 gdk_window_set_user_data (window, viewport);
710 viewport_get_view_allocation (viewport, &view_allocation);
712 attributes.x = view_allocation.x;
713 attributes.y = view_allocation.y;
714 attributes.width = view_allocation.width;
715 attributes.height = view_allocation.height;
716 attributes.event_mask = 0;
718 priv->view_window = gdk_window_new (window,
719 &attributes, attributes_mask);
720 gdk_window_set_user_data (priv->view_window, viewport);
722 attributes.x = - hadjustment->value;
723 attributes.y = - vadjustment->value;
724 attributes.width = hadjustment->upper;
725 attributes.height = vadjustment->upper;
727 attributes.event_mask = event_mask;
729 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
730 gdk_window_set_user_data (priv->bin_window, viewport);
732 child = gtk_bin_get_child (bin);
734 gtk_widget_set_parent_window (child, priv->bin_window);
736 gtk_widget_style_attach (widget);
737 style = gtk_widget_get_style (widget);
738 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
739 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
741 gdk_window_show (priv->bin_window);
742 gdk_window_show (priv->view_window);
746 gtk_viewport_unrealize (GtkWidget *widget)
748 GtkViewport *viewport = GTK_VIEWPORT (widget);
749 GtkViewportPrivate *priv = viewport->priv;
751 gdk_window_set_user_data (priv->view_window, NULL);
752 gdk_window_destroy (priv->view_window);
753 priv->view_window = NULL;
755 gdk_window_set_user_data (priv->bin_window, NULL);
756 gdk_window_destroy (priv->bin_window);
757 priv->bin_window = NULL;
759 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
763 gtk_viewport_draw (GtkWidget *widget,
766 GtkViewport *viewport = GTK_VIEWPORT (widget);
767 GtkViewportPrivate *priv = viewport->priv;
770 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
772 gtk_paint_shadow (gtk_widget_get_style (widget),
774 GTK_STATE_NORMAL, priv->shadow_type,
777 gdk_window_get_width (gtk_widget_get_window (widget)),
778 gdk_window_get_height (gtk_widget_get_window (widget)));
781 if (gtk_cairo_should_draw_window (cr, priv->view_window))
783 /* This is a cute hack to ensure the contents of bin_window are
784 * restricted to where they are visible. We only need to do this
785 * clipping when called via gtk_widget_draw() and not in expose
786 * events. And when that happens every window (including this one)
789 gdk_window_get_position (priv->view_window, &x, &y);
790 cairo_rectangle (cr, x, y,
791 gdk_window_get_width (priv->view_window),
792 gdk_window_get_height (priv->view_window));
796 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
798 gdk_window_get_position (priv->bin_window, &x, &y);
799 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
800 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
801 widget, "viewportbin",
803 gdk_window_get_width (priv->bin_window),
804 gdk_window_get_height (priv->bin_window));
806 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
813 gtk_viewport_add (GtkContainer *container,
816 GtkBin *bin = GTK_BIN (container);
817 GtkViewport *viewport = GTK_VIEWPORT (bin);
818 GtkViewportPrivate *priv = viewport->priv;
820 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
822 gtk_widget_set_parent_window (child, priv->bin_window);
824 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
828 gtk_viewport_size_allocate (GtkWidget *widget,
829 GtkAllocation *allocation)
831 GtkAllocation widget_allocation;
832 GtkViewport *viewport = GTK_VIEWPORT (widget);
833 GtkViewportPrivate *priv = viewport->priv;
834 GtkBin *bin = GTK_BIN (widget);
836 gboolean hadjustment_value_changed, vadjustment_value_changed;
837 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
838 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
839 GtkAllocation child_allocation;
842 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
844 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
845 * redraw the shadow correctly.
847 gtk_widget_get_allocation (widget, &widget_allocation);
848 if (gtk_widget_get_mapped (widget) &&
849 priv->shadow_type != GTK_SHADOW_NONE &&
850 (widget_allocation.width != allocation->width ||
851 widget_allocation.height != allocation->height))
852 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
854 gtk_widget_set_allocation (widget, allocation);
856 viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
857 viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
859 child_allocation.x = 0;
860 child_allocation.y = 0;
861 child_allocation.width = hadjustment->upper;
862 child_allocation.height = vadjustment->upper;
863 if (gtk_widget_get_realized (widget))
865 GtkAllocation view_allocation;
867 gdk_window_move_resize (gtk_widget_get_window (widget),
868 allocation->x + border_width,
869 allocation->y + border_width,
870 allocation->width - border_width * 2,
871 allocation->height - border_width * 2);
873 viewport_get_view_allocation (viewport, &view_allocation);
874 gdk_window_move_resize (priv->view_window,
877 view_allocation.width,
878 view_allocation.height);
879 gdk_window_move_resize (priv->bin_window,
880 - hadjustment->value,
881 - vadjustment->value,
882 child_allocation.width,
883 child_allocation.height);
886 child = gtk_bin_get_child (bin);
887 if (child && gtk_widget_get_visible (child))
888 gtk_widget_size_allocate (child, &child_allocation);
890 gtk_adjustment_changed (hadjustment);
891 gtk_adjustment_changed (vadjustment);
892 if (hadjustment_value_changed)
893 gtk_adjustment_value_changed (hadjustment);
894 if (vadjustment_value_changed)
895 gtk_adjustment_value_changed (vadjustment);
899 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
902 GtkViewport *viewport = GTK_VIEWPORT (data);
903 GtkViewportPrivate *priv = viewport->priv;
904 GtkBin *bin = GTK_BIN (data);
907 child = gtk_bin_get_child (bin);
908 if (child && gtk_widget_get_visible (child) &&
909 gtk_widget_get_realized (GTK_WIDGET (viewport)))
911 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
912 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
916 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
917 new_x = - hadjustment->value;
918 new_y = - vadjustment->value;
920 if (new_x != old_x || new_y != old_y)
922 gdk_window_move (priv->bin_window, new_x, new_y);
923 gdk_window_process_updates (priv->bin_window, TRUE);
929 gtk_viewport_style_set (GtkWidget *widget,
930 GtkStyle *previous_style)
932 if (gtk_widget_get_realized (widget) &&
933 gtk_widget_get_has_window (widget))
936 GtkViewport *viewport = GTK_VIEWPORT (widget);
937 GtkViewportPrivate *priv = viewport->priv;
939 style = gtk_widget_get_style (widget);
940 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
941 gtk_style_set_background (style,
942 gtk_widget_get_window (widget),
943 gtk_widget_get_state (widget));
949 gtk_viewport_get_preferred_size (GtkWidget *widget,
950 GtkOrientation orientation,
954 GtkViewport *viewport = GTK_VIEWPORT (widget);
955 GtkViewportPrivate *priv = viewport->priv;
958 gint child_min, child_nat;
959 gint minimum, natural;
961 child = gtk_bin_get_child (GTK_BIN (widget));
963 /* XXX This should probably be (border_width * 2); but GTK+ has
964 * been doing this with a single border for a while now...
966 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
968 if (priv->shadow_type != GTK_SHADOW_NONE)
970 style = gtk_widget_get_style (GTK_WIDGET (widget));
971 if (orientation == GTK_ORIENTATION_HORIZONTAL)
972 minimum += 2 * style->xthickness;
974 minimum += 2 * style->ythickness;
979 if (child && gtk_widget_get_visible (child))
981 if (orientation == GTK_ORIENTATION_HORIZONTAL)
982 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
984 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
986 minimum += child_min;
987 natural += child_nat;
991 *minimum_size = minimum;
994 *natural_size = natural;
998 gtk_viewport_get_preferred_width (GtkWidget *widget,
1002 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1006 gtk_viewport_get_preferred_height (GtkWidget *widget,
1010 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);