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 "gtkscrollable.h"
33 #include "gtkprivate.h"
38 * @Short_description: An adapter which makes widgets scrollable
40 * @See_also:#GtkScrolledWindow, #GtkAdjustment
42 * The #GtkViewport widget acts as an adaptor class, implementing
43 * scrollability for child widgets that lack their own scrolling
44 * capabilities. Use #GtkViewport to scroll child widgets such as
45 * #GtkTable, #GtkBox, and so on.
47 * If a widget has native scrolling abilities, such as #GtkTextView,
48 * #GtkTreeView or #GtkIconview, it can be added to a #GtkScrolledWindow
49 * with gtk_container_add(). If a widget does not, you must first add the
50 * widget to a #GtkViewport, then add the viewport to the scrolled window.
51 * The convenience function gtk_scrolled_window_add_with_viewport() does
52 * exactly this, so you can ignore the presence of the viewport.
54 * The #GtkViewport will start scrolling content only if allocated less
55 * than the child widget's minimum size in a given orientation.
58 struct _GtkViewportPrivate
60 GtkAdjustment *hadjustment;
61 GtkAdjustment *vadjustment;
62 gint min_display_width;
63 gint min_display_height;
64 GtkShadowType shadow_type;
66 GdkWindow *bin_window;
67 GdkWindow *view_window;
74 PROP_MIN_DISPLAY_WIDTH,
75 PROP_MIN_DISPLAY_HEIGHT,
80 static void gtk_viewport_finalize (GObject *object);
81 static void gtk_viewport_set_property (GObject *object,
85 static void gtk_viewport_get_property (GObject *object,
89 static void gtk_viewport_destroy (GtkWidget *widget);
90 static void gtk_viewport_realize (GtkWidget *widget);
91 static void gtk_viewport_unrealize (GtkWidget *widget);
92 static gint gtk_viewport_draw (GtkWidget *widget,
94 static void gtk_viewport_add (GtkContainer *container,
96 static void gtk_viewport_size_allocate (GtkWidget *widget,
97 GtkAllocation *allocation);
98 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
100 static void gtk_viewport_style_set (GtkWidget *widget,
101 GtkStyle *previous_style);
103 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
106 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
111 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
112 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
115 gtk_viewport_class_init (GtkViewportClass *class)
117 GObjectClass *gobject_class;
118 GtkWidgetClass *widget_class;
119 GtkContainerClass *container_class;
121 gobject_class = G_OBJECT_CLASS (class);
122 widget_class = (GtkWidgetClass*) class;
123 container_class = (GtkContainerClass*) class;
125 gobject_class->finalize = gtk_viewport_finalize;
126 gobject_class->set_property = gtk_viewport_set_property;
127 gobject_class->get_property = gtk_viewport_get_property;
129 widget_class->destroy = gtk_viewport_destroy;
130 widget_class->realize = gtk_viewport_realize;
131 widget_class->unrealize = gtk_viewport_unrealize;
132 widget_class->draw = gtk_viewport_draw;
133 widget_class->size_allocate = gtk_viewport_size_allocate;
134 widget_class->style_set = gtk_viewport_style_set;
135 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
136 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
138 container_class->add = gtk_viewport_add;
140 /* GtkScrollable implementation */
141 g_object_class_override_property (gobject_class,
144 g_object_class_override_property (gobject_class,
147 g_object_class_override_property (gobject_class,
148 PROP_MIN_DISPLAY_WIDTH,
149 "min-display-width");
150 g_object_class_override_property (gobject_class,
151 PROP_MIN_DISPLAY_HEIGHT,
152 "min-display-height");
154 g_object_class_install_property (gobject_class,
156 g_param_spec_enum ("shadow-type",
158 P_("Determines how the shadowed box around the viewport is drawn"),
159 GTK_TYPE_SHADOW_TYPE,
161 GTK_PARAM_READWRITE));
163 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
167 gtk_viewport_set_property (GObject *object,
172 GtkViewport *viewport;
174 viewport = GTK_VIEWPORT (object);
178 case PROP_HADJUSTMENT:
179 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
181 case PROP_VADJUSTMENT:
182 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
184 case PROP_MIN_DISPLAY_WIDTH:
185 viewport->priv->min_display_width = g_value_get_int (value);
187 case PROP_MIN_DISPLAY_HEIGHT:
188 viewport->priv->min_display_height = g_value_get_int (value);
190 case PROP_SHADOW_TYPE:
191 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gtk_viewport_get_property (GObject *object,
205 GtkViewport *viewport = GTK_VIEWPORT (object);
206 GtkViewportPrivate *priv = viewport->priv;
210 case PROP_HADJUSTMENT:
211 g_value_set_object (value, priv->hadjustment);
213 case PROP_VADJUSTMENT:
214 g_value_set_object (value, priv->vadjustment);
216 case PROP_MIN_DISPLAY_WIDTH:
217 g_value_set_int (value, priv->min_display_width);
219 case PROP_MIN_DISPLAY_HEIGHT:
220 g_value_set_int (value, priv->min_display_height);
222 case PROP_SHADOW_TYPE:
223 g_value_set_enum (value, priv->shadow_type);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 gtk_viewport_init (GtkViewport *viewport)
234 GtkViewportPrivate *priv;
236 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
239 priv = viewport->priv;
241 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
243 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
244 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
246 priv->shadow_type = GTK_SHADOW_IN;
247 priv->view_window = NULL;
248 priv->bin_window = NULL;
249 priv->hadjustment = NULL;
250 priv->vadjustment = NULL;
251 priv->min_display_width = -1;
252 priv->min_display_height = -1;
257 * @hadjustment: horizontal adjustment.
258 * @vadjustment: vertical adjustment.
259 * @returns: a new #GtkViewport.
261 * Creates a new #GtkViewport with the given adjustments.
265 gtk_viewport_new (GtkAdjustment *hadjustment,
266 GtkAdjustment *vadjustment)
270 viewport = g_object_new (GTK_TYPE_VIEWPORT,
271 "hadjustment", hadjustment,
272 "vadjustment", vadjustment,
278 #define ADJUSTMENT_POINTER(viewport, orientation) \
279 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
280 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
283 viewport_disconnect_adjustment (GtkViewport *viewport,
284 GtkOrientation orientation)
286 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
290 g_signal_handlers_disconnect_by_func (*adjustmentp,
291 gtk_viewport_adjustment_value_changed,
293 g_object_unref (*adjustmentp);
299 gtk_viewport_finalize (GObject *object)
301 GtkViewport *viewport = GTK_VIEWPORT (object);
303 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
304 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
306 G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
310 gtk_viewport_destroy (GtkWidget *widget)
312 GtkViewport *viewport = GTK_VIEWPORT (widget);
314 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
315 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
317 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
321 * gtk_viewport_get_hadjustment:
322 * @viewport: a #GtkViewport.
324 * Returns the horizontal adjustment of the viewport.
326 * Return value: (transfer none): the horizontal adjustment of @viewport.
328 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
331 gtk_viewport_get_hadjustment (GtkViewport *viewport)
333 GtkViewportPrivate *priv;
335 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
337 priv = viewport->priv;
339 if (!priv->hadjustment)
340 gtk_viewport_set_hadjustment (viewport, NULL);
342 return priv->hadjustment;
346 * gtk_viewport_get_vadjustment:
347 * @viewport: a #GtkViewport.
349 * Returns the vertical adjustment of the viewport.
351 * Return value: (transfer none): the vertical adjustment of @viewport.
353 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
356 gtk_viewport_get_vadjustment (GtkViewport *viewport)
358 GtkViewportPrivate *priv;
360 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
362 priv = viewport->priv;
364 if (!priv->vadjustment)
365 gtk_viewport_set_vadjustment (viewport, NULL);
367 return priv->vadjustment;
371 viewport_get_view_allocation (GtkViewport *viewport,
372 GtkAllocation *view_allocation)
374 GtkViewportPrivate *priv = viewport->priv;
376 GtkWidget *widget = GTK_WIDGET (viewport);
377 GtkAllocation allocation;
380 gtk_widget_get_allocation (widget, &allocation);
381 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
383 view_allocation->x = 0;
384 view_allocation->y = 0;
386 if (priv->shadow_type != GTK_SHADOW_NONE)
388 style = gtk_widget_get_style (widget);
389 view_allocation->x = style->xthickness;
390 view_allocation->y = style->ythickness;
393 view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
394 view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
398 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
399 gboolean *value_changed)
401 gdouble value = adjustment->value;
403 value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
404 if (value != adjustment->value)
406 adjustment->value = value;
408 *value_changed = TRUE;
410 else if (value_changed)
411 *value_changed = FALSE;
415 viewport_set_hadjustment_values (GtkViewport *viewport,
416 gboolean *value_changed)
418 GtkBin *bin = GTK_BIN (viewport);
419 GtkAllocation view_allocation;
420 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
422 gdouble old_page_size;
426 viewport_get_view_allocation (viewport, &view_allocation);
428 old_page_size = hadjustment->page_size;
429 old_upper = hadjustment->upper;
430 old_value = hadjustment->value;
431 hadjustment->page_size = view_allocation.width;
432 hadjustment->step_increment = view_allocation.width * 0.1;
433 hadjustment->page_increment = view_allocation.width * 0.9;
435 hadjustment->lower = 0;
437 child = gtk_bin_get_child (bin);
438 if (child && gtk_widget_get_visible (child))
442 gtk_widget_get_preferred_width_for_height (child,
443 view_allocation.height,
446 hadjustment->upper = MAX (minimum_width, view_allocation.width);
449 hadjustment->upper = view_allocation.width;
451 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
453 gdouble dist = old_upper - (old_value + old_page_size);
454 hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
455 viewport_reclamp_adjustment (hadjustment, value_changed);
456 *value_changed = (old_value != hadjustment->value);
459 viewport_reclamp_adjustment (hadjustment, value_changed);
463 viewport_set_vadjustment_values (GtkViewport *viewport,
464 gboolean *value_changed)
466 GtkBin *bin = GTK_BIN (viewport);
467 GtkAllocation view_allocation;
468 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
471 viewport_get_view_allocation (viewport, &view_allocation);
473 vadjustment->page_size = view_allocation.height;
474 vadjustment->step_increment = view_allocation.height * 0.1;
475 vadjustment->page_increment = view_allocation.height * 0.9;
477 vadjustment->lower = 0;
479 child = gtk_bin_get_child (bin);
480 if (child && gtk_widget_get_visible (child))
484 gtk_widget_get_preferred_height_for_width (child,
485 view_allocation.width,
489 vadjustment->upper = MAX (minimum_height, view_allocation.height);
492 vadjustment->upper = view_allocation.height;
494 viewport_reclamp_adjustment (vadjustment, value_changed);
498 viewport_set_adjustment (GtkViewport *viewport,
499 GtkOrientation orientation,
500 GtkAdjustment *adjustment)
502 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
503 gboolean value_changed;
505 if (adjustment && adjustment == *adjustmentp)
509 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
510 viewport_disconnect_adjustment (viewport, orientation);
511 *adjustmentp = adjustment;
512 g_object_ref_sink (adjustment);
514 if (orientation == GTK_ORIENTATION_HORIZONTAL)
515 viewport_set_hadjustment_values (viewport, &value_changed);
517 viewport_set_vadjustment_values (viewport, &value_changed);
519 g_signal_connect (adjustment, "value-changed",
520 G_CALLBACK (gtk_viewport_adjustment_value_changed),
523 gtk_adjustment_changed (adjustment);
526 gtk_adjustment_value_changed (adjustment);
528 gtk_viewport_adjustment_value_changed (adjustment, viewport);
532 * gtk_viewport_set_hadjustment:
533 * @viewport: a #GtkViewport.
534 * @adjustment: (allow-none): a #GtkAdjustment.
536 * Sets the horizontal adjustment of the viewport.
538 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
541 gtk_viewport_set_hadjustment (GtkViewport *viewport,
542 GtkAdjustment *adjustment)
544 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
546 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
548 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
550 g_object_notify (G_OBJECT (viewport), "hadjustment");
554 * gtk_viewport_set_vadjustment:
555 * @viewport: a #GtkViewport.
556 * @adjustment: (allow-none): a #GtkAdjustment.
558 * Sets the vertical adjustment of the viewport.
560 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
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_shadow_type:
577 * @viewport: a #GtkViewport.
578 * @type: the new shadow type.
580 * Sets the shadow type of the viewport.
583 gtk_viewport_set_shadow_type (GtkViewport *viewport,
586 GtkViewportPrivate *priv;
587 GtkAllocation allocation;
590 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
592 widget = GTK_WIDGET (viewport);
593 priv = viewport->priv;
595 if ((GtkShadowType) priv->shadow_type != type)
597 priv->shadow_type = type;
599 if (gtk_widget_get_visible (widget))
601 gtk_widget_get_allocation (widget, &allocation);
602 gtk_widget_size_allocate (widget, &allocation);
603 gtk_widget_set_allocation (widget, &allocation);
604 gtk_widget_queue_draw (widget);
607 g_object_notify (G_OBJECT (viewport), "shadow-type");
612 * gtk_viewport_get_shadow_type:
613 * @viewport: a #GtkViewport
615 * Gets the shadow type of the #GtkViewport. See
616 * gtk_viewport_set_shadow_type().
618 * Return value: the shadow type
621 gtk_viewport_get_shadow_type (GtkViewport *viewport)
623 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
625 return viewport->priv->shadow_type;
629 * gtk_viewport_get_bin_window:
630 * @viewport: a #GtkViewport
632 * Gets the bin window of the #GtkViewport.
634 * Return value: (transfer none): a #GdkWindow
639 gtk_viewport_get_bin_window (GtkViewport *viewport)
641 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
643 return viewport->priv->bin_window;
647 * gtk_viewport_get_view_window:
648 * @viewport: a #GtkViewport
650 * Gets the view window of the #GtkViewport.
652 * Return value: (transfer none): a #GdkWindow
657 gtk_viewport_get_view_window (GtkViewport *viewport)
659 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
661 return viewport->priv->view_window;
665 gtk_viewport_realize (GtkWidget *widget)
667 GtkViewport *viewport = GTK_VIEWPORT (widget);
668 GtkViewportPrivate *priv = viewport->priv;
669 GtkBin *bin = GTK_BIN (widget);
670 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
671 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
672 GtkAllocation allocation;
673 GtkAllocation view_allocation;
677 GdkWindowAttr attributes;
678 gint attributes_mask;
682 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
684 gtk_widget_set_realized (widget, TRUE);
686 gtk_widget_get_allocation (widget, &allocation);
688 attributes.x = allocation.x + border_width;
689 attributes.y = allocation.y + border_width;
690 attributes.width = allocation.width - border_width * 2;
691 attributes.height = allocation.height - border_width * 2;
692 attributes.window_type = GDK_WINDOW_CHILD;
693 attributes.wclass = GDK_INPUT_OUTPUT;
694 attributes.visual = gtk_widget_get_visual (widget);
696 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
697 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
699 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
701 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
703 window = gdk_window_new (gtk_widget_get_parent_window (widget),
704 &attributes, attributes_mask);
705 gtk_widget_set_window (widget, window);
706 gdk_window_set_user_data (window, viewport);
708 viewport_get_view_allocation (viewport, &view_allocation);
710 attributes.x = view_allocation.x;
711 attributes.y = view_allocation.y;
712 attributes.width = view_allocation.width;
713 attributes.height = view_allocation.height;
714 attributes.event_mask = 0;
716 priv->view_window = gdk_window_new (window,
717 &attributes, attributes_mask);
718 gdk_window_set_user_data (priv->view_window, viewport);
720 attributes.x = - hadjustment->value;
721 attributes.y = - vadjustment->value;
722 attributes.width = hadjustment->upper;
723 attributes.height = vadjustment->upper;
725 attributes.event_mask = event_mask;
727 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
728 gdk_window_set_user_data (priv->bin_window, viewport);
730 child = gtk_bin_get_child (bin);
732 gtk_widget_set_parent_window (child, priv->bin_window);
734 gtk_widget_style_attach (widget);
735 style = gtk_widget_get_style (widget);
736 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
737 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
739 gdk_window_show (priv->bin_window);
740 gdk_window_show (priv->view_window);
744 gtk_viewport_unrealize (GtkWidget *widget)
746 GtkViewport *viewport = GTK_VIEWPORT (widget);
747 GtkViewportPrivate *priv = viewport->priv;
749 gdk_window_set_user_data (priv->view_window, NULL);
750 gdk_window_destroy (priv->view_window);
751 priv->view_window = NULL;
753 gdk_window_set_user_data (priv->bin_window, NULL);
754 gdk_window_destroy (priv->bin_window);
755 priv->bin_window = NULL;
757 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
761 gtk_viewport_draw (GtkWidget *widget,
764 GtkViewport *viewport = GTK_VIEWPORT (widget);
765 GtkViewportPrivate *priv = viewport->priv;
768 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
770 gtk_paint_shadow (gtk_widget_get_style (widget),
772 GTK_STATE_NORMAL, priv->shadow_type,
775 gdk_window_get_width (gtk_widget_get_window (widget)),
776 gdk_window_get_height (gtk_widget_get_window (widget)));
779 if (gtk_cairo_should_draw_window (cr, priv->view_window))
781 /* This is a cute hack to ensure the contents of bin_window are
782 * restricted to where they are visible. We only need to do this
783 * clipping when called via gtk_widget_draw() and not in expose
784 * events. And when that happens every window (including this one)
787 gdk_window_get_position (priv->view_window, &x, &y);
788 cairo_rectangle (cr, x, y,
789 gdk_window_get_width (priv->view_window),
790 gdk_window_get_height (priv->view_window));
794 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
796 gdk_window_get_position (priv->bin_window, &x, &y);
797 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
798 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
799 widget, "viewportbin",
801 gdk_window_get_width (priv->bin_window),
802 gdk_window_get_height (priv->bin_window));
804 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
811 gtk_viewport_add (GtkContainer *container,
814 GtkBin *bin = GTK_BIN (container);
815 GtkViewport *viewport = GTK_VIEWPORT (bin);
816 GtkViewportPrivate *priv = viewport->priv;
818 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
820 gtk_widget_set_parent_window (child, priv->bin_window);
822 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
826 gtk_viewport_size_allocate (GtkWidget *widget,
827 GtkAllocation *allocation)
829 GtkAllocation widget_allocation;
830 GtkViewport *viewport = GTK_VIEWPORT (widget);
831 GtkViewportPrivate *priv = viewport->priv;
832 GtkBin *bin = GTK_BIN (widget);
834 gboolean hadjustment_value_changed, vadjustment_value_changed;
835 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
836 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
837 GtkAllocation child_allocation;
840 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
842 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
843 * redraw the shadow correctly.
845 gtk_widget_get_allocation (widget, &widget_allocation);
846 if (gtk_widget_get_mapped (widget) &&
847 priv->shadow_type != GTK_SHADOW_NONE &&
848 (widget_allocation.width != allocation->width ||
849 widget_allocation.height != allocation->height))
850 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
852 gtk_widget_set_allocation (widget, allocation);
854 viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
855 viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
857 child_allocation.x = 0;
858 child_allocation.y = 0;
859 child_allocation.width = hadjustment->upper;
860 child_allocation.height = vadjustment->upper;
861 if (gtk_widget_get_realized (widget))
863 GtkAllocation view_allocation;
865 gdk_window_move_resize (gtk_widget_get_window (widget),
866 allocation->x + border_width,
867 allocation->y + border_width,
868 allocation->width - border_width * 2,
869 allocation->height - border_width * 2);
871 viewport_get_view_allocation (viewport, &view_allocation);
872 gdk_window_move_resize (priv->view_window,
875 view_allocation.width,
876 view_allocation.height);
877 gdk_window_move_resize (priv->bin_window,
878 - hadjustment->value,
879 - vadjustment->value,
880 child_allocation.width,
881 child_allocation.height);
884 child = gtk_bin_get_child (bin);
885 if (child && gtk_widget_get_visible (child))
886 gtk_widget_size_allocate (child, &child_allocation);
888 gtk_adjustment_changed (hadjustment);
889 gtk_adjustment_changed (vadjustment);
890 if (hadjustment_value_changed)
891 gtk_adjustment_value_changed (hadjustment);
892 if (vadjustment_value_changed)
893 gtk_adjustment_value_changed (vadjustment);
897 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
900 GtkViewport *viewport = GTK_VIEWPORT (data);
901 GtkViewportPrivate *priv = viewport->priv;
902 GtkBin *bin = GTK_BIN (data);
905 child = gtk_bin_get_child (bin);
906 if (child && gtk_widget_get_visible (child) &&
907 gtk_widget_get_realized (GTK_WIDGET (viewport)))
909 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
910 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
914 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
915 new_x = - hadjustment->value;
916 new_y = - vadjustment->value;
918 if (new_x != old_x || new_y != old_y)
920 gdk_window_move (priv->bin_window, new_x, new_y);
921 gdk_window_process_updates (priv->bin_window, TRUE);
927 gtk_viewport_style_set (GtkWidget *widget,
928 GtkStyle *previous_style)
930 if (gtk_widget_get_realized (widget) &&
931 gtk_widget_get_has_window (widget))
934 GtkViewport *viewport = GTK_VIEWPORT (widget);
935 GtkViewportPrivate *priv = viewport->priv;
937 style = gtk_widget_get_style (widget);
938 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
939 gtk_style_set_background (style,
940 gtk_widget_get_window (widget),
941 gtk_widget_get_state (widget));
947 gtk_viewport_get_preferred_size (GtkWidget *widget,
948 GtkOrientation orientation,
952 GtkViewport *viewport = GTK_VIEWPORT (widget);
953 GtkViewportPrivate *priv = viewport->priv;
956 gint child_min, child_nat;
957 gint minimum, natural;
959 child = gtk_bin_get_child (GTK_BIN (widget));
961 /* XXX This should probably be (border_width * 2); but GTK+ has
962 * been doing this with a single border for a while now...
964 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
966 if (priv->shadow_type != GTK_SHADOW_NONE)
968 style = gtk_widget_get_style (GTK_WIDGET (widget));
969 if (orientation == GTK_ORIENTATION_HORIZONTAL)
970 minimum += 2 * style->xthickness;
972 minimum += 2 * style->ythickness;
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);