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/.
28 #include "gtkviewport.h"
30 #include "gtkmarshalers.h"
31 #include "gtktypeutils.h"
32 #include "gtkprivate.h"
37 * @Short_description: An adapter which makes widgets scrollable
39 * @See_also:#GtkScrolledWindow, #GtkAdjustment
41 * The #GtkViewport widget acts as an adaptor class, implementing
42 * scrollability for child widgets that lack their own scrolling
43 * capabilities. Use #GtkViewport to scroll child widgets such as
44 * #GtkTable, #GtkBox, and so on.
46 * If a widget has native scrolling abilities, such as #GtkTextView,
47 * #GtkTreeView or #GtkIconview, it can be added to a #GtkScrolledWindow
48 * with gtk_container_add(). If a widget does not, you must first add the
49 * widget to a #GtkViewport, then add the viewport to the scrolled window.
50 * The convenience function gtk_scrolled_window_add_with_viewport() does
51 * exactly this, so you can ignore the presence of the viewport.
53 * The #GtkViewport will start scrolling content only if allocated less
54 * than the child widget's minimum size in a given orientation.
57 struct _GtkViewportPrivate
59 GtkAdjustment *hadjustment;
60 GtkAdjustment *vadjustment;
61 GtkShadowType shadow_type;
63 GdkWindow *bin_window;
64 GdkWindow *view_window;
75 static void gtk_viewport_finalize (GObject *object);
76 static void gtk_viewport_set_property (GObject *object,
80 static void gtk_viewport_get_property (GObject *object,
84 static void gtk_viewport_set_scroll_adjustments (GtkViewport *viewport,
85 GtkAdjustment *hadjustment,
86 GtkAdjustment *vadjustment);
87 static void gtk_viewport_destroy (GtkWidget *widget);
88 static void gtk_viewport_realize (GtkWidget *widget);
89 static void gtk_viewport_unrealize (GtkWidget *widget);
90 static gint gtk_viewport_draw (GtkWidget *widget,
92 static void gtk_viewport_add (GtkContainer *container,
94 static void gtk_viewport_size_allocate (GtkWidget *widget,
95 GtkAllocation *allocation);
96 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
98 static void gtk_viewport_style_set (GtkWidget *widget,
99 GtkStyle *previous_style);
101 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
104 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
109 G_DEFINE_TYPE (GtkViewport, gtk_viewport, GTK_TYPE_BIN)
112 gtk_viewport_class_init (GtkViewportClass *class)
114 GObjectClass *gobject_class;
115 GtkWidgetClass *widget_class;
116 GtkContainerClass *container_class;
118 gobject_class = G_OBJECT_CLASS (class);
119 widget_class = (GtkWidgetClass*) class;
120 container_class = (GtkContainerClass*) class;
122 gobject_class->finalize = gtk_viewport_finalize;
123 gobject_class->set_property = gtk_viewport_set_property;
124 gobject_class->get_property = gtk_viewport_get_property;
126 widget_class->destroy = gtk_viewport_destroy;
127 widget_class->realize = gtk_viewport_realize;
128 widget_class->unrealize = gtk_viewport_unrealize;
129 widget_class->draw = gtk_viewport_draw;
130 widget_class->size_allocate = gtk_viewport_size_allocate;
131 widget_class->style_set = gtk_viewport_style_set;
132 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
133 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
135 container_class->add = gtk_viewport_add;
137 class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;
139 g_object_class_install_property (gobject_class,
141 g_param_spec_object ("hadjustment",
142 P_("Horizontal adjustment"),
143 P_("The GtkAdjustment that determines the values of the horizontal position for this viewport"),
145 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
147 g_object_class_install_property (gobject_class,
149 g_param_spec_object ("vadjustment",
150 P_("Vertical adjustment"),
151 P_("The GtkAdjustment that determines the values of the vertical position for this viewport"),
153 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
155 g_object_class_install_property (gobject_class,
157 g_param_spec_enum ("shadow-type",
159 P_("Determines how the shadowed box around the viewport is drawn"),
160 GTK_TYPE_SHADOW_TYPE,
162 GTK_PARAM_READWRITE));
165 * GtkViewport::set-scroll-adjustments
166 * @horizontal: the horizontal #GtkAdjustment
167 * @vertical: the vertical #GtkAdjustment
169 * Set the scroll adjustments for the viewport. Usually scrolled containers
170 * like #GtkScrolledWindow will emit this signal to connect two instances
171 * of #GtkScrollbar to the scroll directions of the #GtkViewport.
173 widget_class->set_scroll_adjustments_signal =
174 g_signal_new (I_("set-scroll-adjustments"),
175 G_OBJECT_CLASS_TYPE (gobject_class),
176 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
177 G_STRUCT_OFFSET (GtkViewportClass, set_scroll_adjustments),
179 _gtk_marshal_VOID__OBJECT_OBJECT,
182 GTK_TYPE_ADJUSTMENT);
184 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
188 gtk_viewport_set_property (GObject *object,
193 GtkViewport *viewport;
195 viewport = GTK_VIEWPORT (object);
199 case PROP_HADJUSTMENT:
200 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
202 case PROP_VADJUSTMENT:
203 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
205 case PROP_SHADOW_TYPE:
206 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gtk_viewport_get_property (GObject *object,
220 GtkViewport *viewport = GTK_VIEWPORT (object);
221 GtkViewportPrivate *priv = viewport->priv;
225 case PROP_HADJUSTMENT:
226 g_value_set_object (value, priv->hadjustment);
228 case PROP_VADJUSTMENT:
229 g_value_set_object (value, priv->vadjustment);
231 case PROP_SHADOW_TYPE:
232 g_value_set_enum (value, priv->shadow_type);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 gtk_viewport_init (GtkViewport *viewport)
243 GtkViewportPrivate *priv;
245 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
248 priv = viewport->priv;
250 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
252 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
253 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
255 priv->shadow_type = GTK_SHADOW_IN;
256 priv->view_window = NULL;
257 priv->bin_window = NULL;
258 priv->hadjustment = NULL;
259 priv->vadjustment = NULL;
264 * @hadjustment: horizontal adjustment.
265 * @vadjustment: vertical adjustment.
266 * @returns: a new #GtkViewport.
268 * Creates a new #GtkViewport with the given adjustments.
272 gtk_viewport_new (GtkAdjustment *hadjustment,
273 GtkAdjustment *vadjustment)
277 viewport = g_object_new (GTK_TYPE_VIEWPORT,
278 "hadjustment", hadjustment,
279 "vadjustment", vadjustment,
285 #define ADJUSTMENT_POINTER(viewport, orientation) \
286 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
287 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
290 viewport_disconnect_adjustment (GtkViewport *viewport,
291 GtkOrientation orientation)
293 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
297 g_signal_handlers_disconnect_by_func (*adjustmentp,
298 gtk_viewport_adjustment_value_changed,
300 g_object_unref (*adjustmentp);
306 gtk_viewport_finalize (GObject *object)
308 GtkViewport *viewport = GTK_VIEWPORT (object);
310 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
311 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
313 G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
317 gtk_viewport_destroy (GtkWidget *widget)
319 GtkViewport *viewport = GTK_VIEWPORT (widget);
321 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
322 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
324 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
328 * gtk_viewport_get_hadjustment:
329 * @viewport: a #GtkViewport.
331 * Returns the horizontal adjustment of the viewport.
333 * Return value: (transfer none): the horizontal adjustment of @viewport.
336 gtk_viewport_get_hadjustment (GtkViewport *viewport)
338 GtkViewportPrivate *priv;
340 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
342 priv = viewport->priv;
344 if (!priv->hadjustment)
345 gtk_viewport_set_hadjustment (viewport, NULL);
347 return priv->hadjustment;
351 * gtk_viewport_get_vadjustment:
352 * @viewport: a #GtkViewport.
354 * Returns the vertical adjustment of the viewport.
356 * Return value: (transfer none): the vertical adjustment of @viewport.
359 gtk_viewport_get_vadjustment (GtkViewport *viewport)
361 GtkViewportPrivate *priv;
363 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
365 priv = viewport->priv;
367 if (!priv->vadjustment)
368 gtk_viewport_set_vadjustment (viewport, NULL);
370 return priv->vadjustment;
374 viewport_get_view_allocation (GtkViewport *viewport,
375 GtkAllocation *view_allocation)
377 GtkViewportPrivate *priv = viewport->priv;
379 GtkWidget *widget = GTK_WIDGET (viewport);
380 GtkAllocation allocation;
383 gtk_widget_get_allocation (widget, &allocation);
384 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
386 view_allocation->x = 0;
387 view_allocation->y = 0;
389 if (priv->shadow_type != GTK_SHADOW_NONE)
391 style = gtk_widget_get_style (widget);
392 view_allocation->x = style->xthickness;
393 view_allocation->y = style->ythickness;
396 view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
397 view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
401 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
402 gboolean *value_changed)
404 gdouble value = adjustment->value;
406 value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
407 if (value != adjustment->value)
409 adjustment->value = value;
411 *value_changed = TRUE;
413 else if (value_changed)
414 *value_changed = FALSE;
418 viewport_set_hadjustment_values (GtkViewport *viewport,
419 gboolean *value_changed)
421 GtkBin *bin = GTK_BIN (viewport);
422 GtkAllocation view_allocation;
423 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
425 gdouble old_page_size;
429 viewport_get_view_allocation (viewport, &view_allocation);
431 old_page_size = hadjustment->page_size;
432 old_upper = hadjustment->upper;
433 old_value = hadjustment->value;
434 hadjustment->page_size = view_allocation.width;
435 hadjustment->step_increment = view_allocation.width * 0.1;
436 hadjustment->page_increment = view_allocation.width * 0.9;
438 hadjustment->lower = 0;
440 child = gtk_bin_get_child (bin);
441 if (child && gtk_widget_get_visible (child))
445 gtk_widget_get_preferred_width_for_height (child,
446 view_allocation.height,
449 hadjustment->upper = MAX (minimum_width, view_allocation.width);
452 hadjustment->upper = view_allocation.width;
454 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
456 gdouble dist = old_upper - (old_value + old_page_size);
457 hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
458 viewport_reclamp_adjustment (hadjustment, value_changed);
459 *value_changed = (old_value != hadjustment->value);
462 viewport_reclamp_adjustment (hadjustment, value_changed);
466 viewport_set_vadjustment_values (GtkViewport *viewport,
467 gboolean *value_changed)
469 GtkBin *bin = GTK_BIN (viewport);
470 GtkAllocation view_allocation;
471 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
474 viewport_get_view_allocation (viewport, &view_allocation);
476 vadjustment->page_size = view_allocation.height;
477 vadjustment->step_increment = view_allocation.height * 0.1;
478 vadjustment->page_increment = view_allocation.height * 0.9;
480 vadjustment->lower = 0;
482 child = gtk_bin_get_child (bin);
483 if (child && gtk_widget_get_visible (child))
487 gtk_widget_get_preferred_height_for_width (child,
488 view_allocation.width,
492 vadjustment->upper = MAX (minimum_height, view_allocation.height);
495 vadjustment->upper = view_allocation.height;
497 viewport_reclamp_adjustment (vadjustment, value_changed);
501 viewport_set_adjustment (GtkViewport *viewport,
502 GtkOrientation orientation,
503 GtkAdjustment *adjustment)
505 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
506 gboolean value_changed;
508 if (adjustment && adjustment == *adjustmentp)
512 adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
514 viewport_disconnect_adjustment (viewport, orientation);
515 *adjustmentp = adjustment;
516 g_object_ref_sink (adjustment);
518 if (orientation == GTK_ORIENTATION_HORIZONTAL)
519 viewport_set_hadjustment_values (viewport, &value_changed);
521 viewport_set_vadjustment_values (viewport, &value_changed);
523 g_signal_connect (adjustment, "value-changed",
524 G_CALLBACK (gtk_viewport_adjustment_value_changed),
527 gtk_adjustment_changed (adjustment);
530 gtk_adjustment_value_changed (adjustment);
532 gtk_viewport_adjustment_value_changed (adjustment, viewport);
536 * gtk_viewport_set_hadjustment:
537 * @viewport: a #GtkViewport.
538 * @adjustment: (allow-none): a #GtkAdjustment.
540 * Sets the horizontal adjustment of the viewport.
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.
563 gtk_viewport_set_vadjustment (GtkViewport *viewport,
564 GtkAdjustment *adjustment)
566 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
568 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
570 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
572 g_object_notify (G_OBJECT (viewport), "vadjustment");
576 gtk_viewport_set_scroll_adjustments (GtkViewport *viewport,
577 GtkAdjustment *hadjustment,
578 GtkAdjustment *vadjustment)
580 gtk_viewport_set_hadjustment (viewport, hadjustment);
581 gtk_viewport_set_vadjustment (viewport, vadjustment);
585 * gtk_viewport_set_shadow_type:
586 * @viewport: a #GtkViewport.
587 * @type: the new shadow type.
589 * Sets the shadow type of the viewport.
592 gtk_viewport_set_shadow_type (GtkViewport *viewport,
595 GtkViewportPrivate *priv;
596 GtkAllocation allocation;
599 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
601 widget = GTK_WIDGET (viewport);
602 priv = viewport->priv;
604 if ((GtkShadowType) priv->shadow_type != type)
606 priv->shadow_type = type;
608 if (gtk_widget_get_visible (widget))
610 gtk_widget_get_allocation (widget, &allocation);
611 gtk_widget_size_allocate (widget, &allocation);
612 gtk_widget_set_allocation (widget, &allocation);
613 gtk_widget_queue_draw (widget);
616 g_object_notify (G_OBJECT (viewport), "shadow-type");
621 * gtk_viewport_get_shadow_type:
622 * @viewport: a #GtkViewport
624 * Gets the shadow type of the #GtkViewport. See
625 * gtk_viewport_set_shadow_type().
627 * Return value: the shadow type
630 gtk_viewport_get_shadow_type (GtkViewport *viewport)
632 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
634 return viewport->priv->shadow_type;
638 * gtk_viewport_get_bin_window:
639 * @viewport: a #GtkViewport
641 * Gets the bin window of the #GtkViewport.
643 * Return value: (transfer none): a #GdkWindow
648 gtk_viewport_get_bin_window (GtkViewport *viewport)
650 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
652 return viewport->priv->bin_window;
656 * gtk_viewport_get_view_window:
657 * @viewport: a #GtkViewport
659 * Gets the view window of the #GtkViewport.
661 * Return value: (transfer none): a #GdkWindow
666 gtk_viewport_get_view_window (GtkViewport *viewport)
668 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
670 return viewport->priv->view_window;
674 gtk_viewport_realize (GtkWidget *widget)
676 GtkViewport *viewport = GTK_VIEWPORT (widget);
677 GtkViewportPrivate *priv = viewport->priv;
678 GtkBin *bin = GTK_BIN (widget);
679 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
680 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
681 GtkAllocation allocation;
682 GtkAllocation view_allocation;
686 GdkWindowAttr attributes;
687 gint attributes_mask;
691 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
693 gtk_widget_set_realized (widget, TRUE);
695 gtk_widget_get_allocation (widget, &allocation);
697 attributes.x = allocation.x + border_width;
698 attributes.y = allocation.y + border_width;
699 attributes.width = allocation.width - border_width * 2;
700 attributes.height = allocation.height - border_width * 2;
701 attributes.window_type = GDK_WINDOW_CHILD;
702 attributes.wclass = GDK_INPUT_OUTPUT;
703 attributes.visual = gtk_widget_get_visual (widget);
705 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
706 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
708 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
710 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
712 window = gdk_window_new (gtk_widget_get_parent_window (widget),
713 &attributes, attributes_mask);
714 gtk_widget_set_window (widget, window);
715 gdk_window_set_user_data (window, viewport);
717 viewport_get_view_allocation (viewport, &view_allocation);
719 attributes.x = view_allocation.x;
720 attributes.y = view_allocation.y;
721 attributes.width = view_allocation.width;
722 attributes.height = view_allocation.height;
723 attributes.event_mask = 0;
725 priv->view_window = gdk_window_new (window,
726 &attributes, attributes_mask);
727 gdk_window_set_user_data (priv->view_window, viewport);
729 attributes.x = - hadjustment->value;
730 attributes.y = - vadjustment->value;
731 attributes.width = hadjustment->upper;
732 attributes.height = vadjustment->upper;
734 attributes.event_mask = event_mask;
736 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
737 gdk_window_set_user_data (priv->bin_window, viewport);
739 child = gtk_bin_get_child (bin);
741 gtk_widget_set_parent_window (child, priv->bin_window);
743 gtk_widget_style_attach (widget);
744 style = gtk_widget_get_style (widget);
745 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
746 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
748 gdk_window_show (priv->bin_window);
749 gdk_window_show (priv->view_window);
753 gtk_viewport_unrealize (GtkWidget *widget)
755 GtkViewport *viewport = GTK_VIEWPORT (widget);
756 GtkViewportPrivate *priv = viewport->priv;
758 gdk_window_set_user_data (priv->view_window, NULL);
759 gdk_window_destroy (priv->view_window);
760 priv->view_window = NULL;
762 gdk_window_set_user_data (priv->bin_window, NULL);
763 gdk_window_destroy (priv->bin_window);
764 priv->bin_window = NULL;
766 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
770 gtk_viewport_draw (GtkWidget *widget,
773 GtkViewport *viewport = GTK_VIEWPORT (widget);
774 GtkViewportPrivate *priv = viewport->priv;
777 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
779 gtk_paint_shadow (gtk_widget_get_style (widget),
781 GTK_STATE_NORMAL, priv->shadow_type,
784 gdk_window_get_width (gtk_widget_get_window (widget)),
785 gdk_window_get_height (gtk_widget_get_window (widget)));
788 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
790 gdk_window_get_position (priv->bin_window, &x, &y);
791 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
792 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
793 widget, "viewportbin",
795 gdk_window_get_width (priv->bin_window),
796 gdk_window_get_height (priv->bin_window));
798 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
805 gtk_viewport_add (GtkContainer *container,
808 GtkBin *bin = GTK_BIN (container);
809 GtkViewport *viewport = GTK_VIEWPORT (bin);
810 GtkViewportPrivate *priv = viewport->priv;
812 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
814 gtk_widget_set_parent_window (child, priv->bin_window);
816 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
820 gtk_viewport_size_allocate (GtkWidget *widget,
821 GtkAllocation *allocation)
823 GtkAllocation widget_allocation;
824 GtkViewport *viewport = GTK_VIEWPORT (widget);
825 GtkViewportPrivate *priv = viewport->priv;
826 GtkBin *bin = GTK_BIN (widget);
828 gboolean hadjustment_value_changed, vadjustment_value_changed;
829 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
830 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
831 GtkAllocation child_allocation;
834 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
836 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
837 * redraw the shadow correctly.
839 gtk_widget_get_allocation (widget, &widget_allocation);
840 if (gtk_widget_get_mapped (widget) &&
841 priv->shadow_type != GTK_SHADOW_NONE &&
842 (widget_allocation.width != allocation->width ||
843 widget_allocation.height != allocation->height))
844 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
846 gtk_widget_set_allocation (widget, allocation);
848 viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
849 viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
851 child_allocation.x = 0;
852 child_allocation.y = 0;
853 child_allocation.width = hadjustment->upper;
854 child_allocation.height = vadjustment->upper;
855 if (gtk_widget_get_realized (widget))
857 GtkAllocation view_allocation;
859 gdk_window_move_resize (gtk_widget_get_window (widget),
860 allocation->x + border_width,
861 allocation->y + border_width,
862 allocation->width - border_width * 2,
863 allocation->height - border_width * 2);
865 viewport_get_view_allocation (viewport, &view_allocation);
866 gdk_window_move_resize (priv->view_window,
869 view_allocation.width,
870 view_allocation.height);
871 gdk_window_move_resize (priv->bin_window,
872 - hadjustment->value,
873 - vadjustment->value,
874 child_allocation.width,
875 child_allocation.height);
878 child = gtk_bin_get_child (bin);
879 if (child && gtk_widget_get_visible (child))
880 gtk_widget_size_allocate (child, &child_allocation);
882 gtk_adjustment_changed (hadjustment);
883 gtk_adjustment_changed (vadjustment);
884 if (hadjustment_value_changed)
885 gtk_adjustment_value_changed (hadjustment);
886 if (vadjustment_value_changed)
887 gtk_adjustment_value_changed (vadjustment);
891 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
894 GtkViewport *viewport = GTK_VIEWPORT (data);
895 GtkViewportPrivate *priv = viewport->priv;
896 GtkBin *bin = GTK_BIN (data);
899 child = gtk_bin_get_child (bin);
900 if (child && gtk_widget_get_visible (child) &&
901 gtk_widget_get_realized (GTK_WIDGET (viewport)))
903 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
904 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
908 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
909 new_x = - hadjustment->value;
910 new_y = - vadjustment->value;
912 if (new_x != old_x || new_y != old_y)
914 gdk_window_move (priv->bin_window, new_x, new_y);
915 gdk_window_process_updates (priv->bin_window, TRUE);
921 gtk_viewport_style_set (GtkWidget *widget,
922 GtkStyle *previous_style)
924 if (gtk_widget_get_realized (widget) &&
925 gtk_widget_get_has_window (widget))
928 GtkViewport *viewport = GTK_VIEWPORT (widget);
929 GtkViewportPrivate *priv = viewport->priv;
931 style = gtk_widget_get_style (widget);
932 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
933 gtk_style_set_background (style,
934 gtk_widget_get_window (widget),
935 gtk_widget_get_state (widget));
941 gtk_viewport_get_preferred_size (GtkWidget *widget,
942 GtkOrientation orientation,
946 GtkViewport *viewport = GTK_VIEWPORT (widget);
947 GtkViewportPrivate *priv = viewport->priv;
950 gint child_min, child_nat;
951 gint minimum, natural;
953 child = gtk_bin_get_child (GTK_BIN (widget));
955 /* XXX This should probably be (border_width * 2); but GTK+ has
956 * been doing this with a single border for a while now...
958 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
960 if (priv->shadow_type != GTK_SHADOW_NONE)
962 style = gtk_widget_get_style (GTK_WIDGET (widget));
963 if (orientation == GTK_ORIENTATION_HORIZONTAL)
964 minimum += 2 * style->xthickness;
966 minimum += 2 * style->ythickness;
971 if (child && gtk_widget_get_visible (child))
973 if (orientation == GTK_ORIENTATION_HORIZONTAL)
974 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
976 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
978 minimum += child_min;
979 natural += child_nat;
983 *minimum_size = minimum;
986 *natural_size = natural;
990 gtk_viewport_get_preferred_width (GtkWidget *widget,
994 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
998 gtk_viewport_get_preferred_height (GtkWidget *widget,
1002 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);