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"
29 #include "gtksizerequest.h"
31 #include "gtkmarshalers.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.
54 struct _GtkViewportPrivate
56 GtkAdjustment *hadjustment;
57 GtkAdjustment *vadjustment;
58 GtkShadowType shadow_type;
60 GdkWindow *bin_window;
61 GdkWindow *view_window;
72 static void gtk_viewport_finalize (GObject *object);
73 static void gtk_viewport_destroy (GtkObject *object);
74 static void gtk_viewport_set_property (GObject *object,
78 static void gtk_viewport_get_property (GObject *object,
82 static void gtk_viewport_set_scroll_adjustments (GtkViewport *viewport,
83 GtkAdjustment *hadjustment,
84 GtkAdjustment *vadjustment);
85 static void gtk_viewport_realize (GtkWidget *widget);
86 static void gtk_viewport_unrealize (GtkWidget *widget);
87 static void gtk_viewport_paint (GtkWidget *widget,
89 static gint gtk_viewport_expose (GtkWidget *widget,
90 GdkEventExpose *event);
91 static void gtk_viewport_add (GtkContainer *container,
93 static void gtk_viewport_size_allocate (GtkWidget *widget,
94 GtkAllocation *allocation);
95 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
97 static void gtk_viewport_style_set (GtkWidget *widget,
98 GtkStyle *previous_style);
100 static void gtk_viewport_size_request_init (GtkSizeRequestIface *iface);
101 static void gtk_viewport_get_width (GtkSizeRequest *widget,
104 static void gtk_viewport_get_height (GtkSizeRequest *widget,
109 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
110 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
111 gtk_viewport_size_request_init))
114 gtk_viewport_class_init (GtkViewportClass *class)
116 GtkObjectClass *object_class;
117 GObjectClass *gobject_class;
118 GtkWidgetClass *widget_class;
119 GtkContainerClass *container_class;
121 object_class = (GtkObjectClass*) class;
122 gobject_class = G_OBJECT_CLASS (class);
123 widget_class = (GtkWidgetClass*) class;
124 container_class = (GtkContainerClass*) class;
126 gobject_class->finalize = gtk_viewport_finalize;
127 gobject_class->set_property = gtk_viewport_set_property;
128 gobject_class->get_property = gtk_viewport_get_property;
129 object_class->destroy = gtk_viewport_destroy;
131 widget_class->realize = gtk_viewport_realize;
132 widget_class->unrealize = gtk_viewport_unrealize;
133 widget_class->expose_event = gtk_viewport_expose;
134 widget_class->size_allocate = gtk_viewport_size_allocate;
135 widget_class->style_set = gtk_viewport_style_set;
137 container_class->add = gtk_viewport_add;
139 class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;
141 g_object_class_install_property (gobject_class,
143 g_param_spec_object ("hadjustment",
144 P_("Horizontal adjustment"),
145 P_("The GtkAdjustment that determines the values of the horizontal position for this viewport"),
147 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
149 g_object_class_install_property (gobject_class,
151 g_param_spec_object ("vadjustment",
152 P_("Vertical adjustment"),
153 P_("The GtkAdjustment that determines the values of the vertical position for this viewport"),
155 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
157 g_object_class_install_property (gobject_class,
159 g_param_spec_enum ("shadow-type",
161 P_("Determines how the shadowed box around the viewport is drawn"),
162 GTK_TYPE_SHADOW_TYPE,
164 GTK_PARAM_READWRITE));
167 * GtkViewport::set-scroll-adjustments
168 * @horizontal: the horizontal #GtkAdjustment
169 * @vertical: the vertical #GtkAdjustment
171 * Set the scroll adjustments for the viewport. Usually scrolled containers
172 * like #GtkScrolledWindow will emit this signal to connect two instances
173 * of #GtkScrollbar to the scroll directions of the #GtkViewport.
175 widget_class->set_scroll_adjustments_signal =
176 g_signal_new (I_("set-scroll-adjustments"),
177 G_OBJECT_CLASS_TYPE (gobject_class),
178 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
179 G_STRUCT_OFFSET (GtkViewportClass, set_scroll_adjustments),
181 _gtk_marshal_VOID__OBJECT_OBJECT,
184 GTK_TYPE_ADJUSTMENT);
186 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
190 gtk_viewport_set_property (GObject *object,
195 GtkViewport *viewport;
197 viewport = GTK_VIEWPORT (object);
201 case PROP_HADJUSTMENT:
202 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
204 case PROP_VADJUSTMENT:
205 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
207 case PROP_SHADOW_TYPE:
208 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 gtk_viewport_get_property (GObject *object,
222 GtkViewport *viewport = GTK_VIEWPORT (object);
223 GtkViewportPrivate *priv = viewport->priv;
227 case PROP_HADJUSTMENT:
228 g_value_set_object (value, priv->hadjustment);
230 case PROP_VADJUSTMENT:
231 g_value_set_object (value, priv->vadjustment);
233 case PROP_SHADOW_TYPE:
234 g_value_set_enum (value, priv->shadow_type);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 gtk_viewport_init (GtkViewport *viewport)
245 GtkViewportPrivate *priv;
247 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
250 priv = viewport->priv;
252 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
254 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
255 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
257 priv->shadow_type = GTK_SHADOW_IN;
258 priv->view_window = NULL;
259 priv->bin_window = NULL;
260 priv->hadjustment = NULL;
261 priv->vadjustment = NULL;
266 * @hadjustment: horizontal adjustment.
267 * @vadjustment: vertical adjustment.
268 * @returns: a new #GtkViewport.
270 * Creates a new #GtkViewport with the given adjustments.
274 gtk_viewport_new (GtkAdjustment *hadjustment,
275 GtkAdjustment *vadjustment)
279 viewport = g_object_new (GTK_TYPE_VIEWPORT,
280 "hadjustment", hadjustment,
281 "vadjustment", vadjustment,
287 #define ADJUSTMENT_POINTER(viewport, orientation) \
288 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
289 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
292 viewport_disconnect_adjustment (GtkViewport *viewport,
293 GtkOrientation orientation)
295 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
299 g_signal_handlers_disconnect_by_func (*adjustmentp,
300 gtk_viewport_adjustment_value_changed,
302 g_object_unref (*adjustmentp);
308 gtk_viewport_finalize (GObject *object)
310 GtkViewport *viewport = GTK_VIEWPORT (object);
312 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
313 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
315 G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
319 gtk_viewport_destroy (GtkObject *object)
321 GtkViewport *viewport = GTK_VIEWPORT (object);
323 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
324 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
326 GTK_OBJECT_CLASS (gtk_viewport_parent_class)->destroy (object);
330 * gtk_viewport_get_hadjustment:
331 * @viewport: a #GtkViewport.
333 * Returns the horizontal adjustment of the viewport.
335 * Return value: the horizontal adjustment of @viewport.
338 gtk_viewport_get_hadjustment (GtkViewport *viewport)
340 GtkViewportPrivate *priv;
342 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
344 priv = viewport->priv;
346 if (!priv->hadjustment)
347 gtk_viewport_set_hadjustment (viewport, NULL);
349 return priv->hadjustment;
353 * gtk_viewport_get_vadjustment:
354 * @viewport: a #GtkViewport.
356 * Returns the vertical adjustment of the viewport.
358 * Return value: the vertical adjustment of @viewport.
361 gtk_viewport_get_vadjustment (GtkViewport *viewport)
363 GtkViewportPrivate *priv;
365 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
367 priv = viewport->priv;
369 if (!priv->vadjustment)
370 gtk_viewport_set_vadjustment (viewport, NULL);
372 return priv->vadjustment;
376 viewport_get_view_allocation (GtkViewport *viewport,
377 GtkAllocation *view_allocation)
379 GtkViewportPrivate *priv = viewport->priv;
381 GtkWidget *widget = GTK_WIDGET (viewport);
382 GtkAllocation allocation;
385 gtk_widget_get_allocation (widget, &allocation);
386 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
388 view_allocation->x = 0;
389 view_allocation->y = 0;
391 if (priv->shadow_type != GTK_SHADOW_NONE)
393 style = gtk_widget_get_style (widget);
394 view_allocation->x = style->xthickness;
395 view_allocation->y = style->ythickness;
398 view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
399 view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
403 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
404 gboolean *value_changed)
406 gdouble value = adjustment->value;
408 value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
409 if (value != adjustment->value)
411 adjustment->value = value;
413 *value_changed = TRUE;
415 else if (value_changed)
416 *value_changed = FALSE;
420 viewport_set_hadjustment_values (GtkViewport *viewport,
421 gboolean *value_changed)
423 GtkBin *bin = GTK_BIN (viewport);
424 GtkAllocation view_allocation;
425 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
427 gdouble old_page_size;
431 viewport_get_view_allocation (viewport, &view_allocation);
433 old_page_size = hadjustment->page_size;
434 old_upper = hadjustment->upper;
435 old_value = hadjustment->value;
436 hadjustment->page_size = view_allocation.width;
437 hadjustment->step_increment = view_allocation.width * 0.1;
438 hadjustment->page_increment = view_allocation.width * 0.9;
440 hadjustment->lower = 0;
442 child = gtk_bin_get_child (bin);
443 if (child && gtk_widget_get_visible (child))
445 GtkRequisition child_requisition;
447 gtk_size_request_get_size (GTK_SIZE_REQUEST (child),
448 &child_requisition, NULL);
449 hadjustment->upper = MAX (child_requisition.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_size_request_get_height_for_width (GTK_SIZE_REQUEST (child),
488 view_allocation.width,
491 vadjustment->upper = MAX (natural_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 (gtk_adjustment_new (0.0, 0.0, 0.0,
513 viewport_disconnect_adjustment (viewport, orientation);
514 *adjustmentp = adjustment;
515 g_object_ref_sink (adjustment);
517 if (orientation == GTK_ORIENTATION_HORIZONTAL)
518 viewport_set_hadjustment_values (viewport, &value_changed);
520 viewport_set_vadjustment_values (viewport, &value_changed);
522 g_signal_connect (adjustment, "value-changed",
523 G_CALLBACK (gtk_viewport_adjustment_value_changed),
526 gtk_adjustment_changed (adjustment);
529 gtk_adjustment_value_changed (adjustment);
531 gtk_viewport_adjustment_value_changed (adjustment, viewport);
535 * gtk_viewport_set_hadjustment:
536 * @viewport: a #GtkViewport.
537 * @adjustment: (allow-none): a #GtkAdjustment.
539 * Sets the horizontal adjustment of the viewport.
542 gtk_viewport_set_hadjustment (GtkViewport *viewport,
543 GtkAdjustment *adjustment)
545 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
547 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
549 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
551 g_object_notify (G_OBJECT (viewport), "hadjustment");
555 * gtk_viewport_set_vadjustment:
556 * @viewport: a #GtkViewport.
557 * @adjustment: (allow-none): a #GtkAdjustment.
559 * Sets the vertical adjustment of the viewport.
562 gtk_viewport_set_vadjustment (GtkViewport *viewport,
563 GtkAdjustment *adjustment)
565 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
567 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
569 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
571 g_object_notify (G_OBJECT (viewport), "vadjustment");
575 gtk_viewport_set_scroll_adjustments (GtkViewport *viewport,
576 GtkAdjustment *hadjustment,
577 GtkAdjustment *vadjustment)
579 gtk_viewport_set_hadjustment (viewport, hadjustment);
580 gtk_viewport_set_vadjustment (viewport, vadjustment);
584 * gtk_viewport_set_shadow_type:
585 * @viewport: a #GtkViewport.
586 * @type: the new shadow type.
588 * Sets the shadow type of the viewport.
591 gtk_viewport_set_shadow_type (GtkViewport *viewport,
594 GtkViewportPrivate *priv;
595 GtkAllocation allocation;
598 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
600 widget = GTK_WIDGET (viewport);
601 priv = viewport->priv;
603 if ((GtkShadowType) priv->shadow_type != type)
605 priv->shadow_type = type;
607 if (gtk_widget_get_visible (widget))
609 gtk_widget_get_allocation (widget, &allocation);
610 gtk_widget_size_allocate (widget, &allocation);
611 gtk_widget_set_allocation (widget, &allocation);
612 gtk_widget_queue_draw (widget);
615 g_object_notify (G_OBJECT (viewport), "shadow-type");
620 * gtk_viewport_get_shadow_type:
621 * @viewport: a #GtkViewport
623 * Gets the shadow type of the #GtkViewport. See
624 * gtk_viewport_set_shadow_type().
626 * Return value: the shadow type
629 gtk_viewport_get_shadow_type (GtkViewport *viewport)
631 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
633 return viewport->priv->shadow_type;
637 * gtk_viewport_get_bin_window:
638 * @viewport: a #GtkViewport
640 * Gets the bin window of the #GtkViewport.
642 * Return value: a #GdkWindow
647 gtk_viewport_get_bin_window (GtkViewport *viewport)
649 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
651 return viewport->priv->bin_window;
655 * gtk_viewport_get_view_window:
656 * @viewport: a #GtkViewport
658 * Gets the view window of the #GtkViewport.
660 * Return value: (transfer none): a #GdkWindow
665 gtk_viewport_get_view_window (GtkViewport *viewport)
667 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
669 return viewport->priv->view_window;
673 gtk_viewport_realize (GtkWidget *widget)
675 GtkViewport *viewport = GTK_VIEWPORT (widget);
676 GtkViewportPrivate *priv = viewport->priv;
677 GtkBin *bin = GTK_BIN (widget);
678 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
679 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
680 GtkAllocation allocation;
681 GtkAllocation view_allocation;
685 GdkWindowAttr attributes;
686 gint attributes_mask;
690 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
692 gtk_widget_set_realized (widget, TRUE);
694 gtk_widget_get_allocation (widget, &allocation);
696 attributes.x = allocation.x + border_width;
697 attributes.y = allocation.y + border_width;
698 attributes.width = allocation.width - border_width * 2;
699 attributes.height = allocation.height - border_width * 2;
700 attributes.window_type = GDK_WINDOW_CHILD;
701 attributes.wclass = GDK_INPUT_OUTPUT;
702 attributes.visual = gtk_widget_get_visual (widget);
703 attributes.colormap = gtk_widget_get_colormap (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 | GDK_WA_COLORMAP;
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 gdk_window_set_back_pixmap (priv->view_window, NULL, FALSE);
731 attributes.x = - hadjustment->value;
732 attributes.y = - vadjustment->value;
733 attributes.width = hadjustment->upper;
734 attributes.height = vadjustment->upper;
736 attributes.event_mask = event_mask;
738 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
739 gdk_window_set_user_data (priv->bin_window, viewport);
741 child = gtk_bin_get_child (bin);
743 gtk_widget_set_parent_window (child, priv->bin_window);
745 gtk_widget_style_attach (widget);
746 style = gtk_widget_get_style (widget);
747 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
748 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
750 /* Call paint here to allow a theme to set the background without flashing
752 gtk_paint_flat_box(style, priv->bin_window, GTK_STATE_NORMAL,
754 NULL, widget, "viewportbin",
757 gdk_window_show (priv->bin_window);
758 gdk_window_show (priv->view_window);
762 gtk_viewport_unrealize (GtkWidget *widget)
764 GtkViewport *viewport = GTK_VIEWPORT (widget);
765 GtkViewportPrivate *priv = viewport->priv;
767 gdk_window_set_user_data (priv->view_window, NULL);
768 gdk_window_destroy (priv->view_window);
769 priv->view_window = NULL;
771 gdk_window_set_user_data (priv->bin_window, NULL);
772 gdk_window_destroy (priv->bin_window);
773 priv->bin_window = NULL;
775 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
779 gtk_viewport_paint (GtkWidget *widget,
782 if (gtk_widget_is_drawable (widget))
784 GtkViewport *viewport = GTK_VIEWPORT (widget);
785 GtkViewportPrivate *priv = viewport->priv;
787 gtk_paint_shadow (gtk_widget_get_style (widget),
788 gtk_widget_get_window (widget),
789 GTK_STATE_NORMAL, priv->shadow_type,
790 area, widget, "viewport",
796 gtk_viewport_expose (GtkWidget *widget,
797 GdkEventExpose *event)
799 GtkViewport *viewport;
801 if (gtk_widget_is_drawable (widget))
803 viewport = GTK_VIEWPORT (widget);
804 GtkViewportPrivate *priv = viewport->priv;
806 if (event->window == gtk_widget_get_window (widget))
807 gtk_viewport_paint (widget, &event->area);
808 else if (event->window == priv->bin_window)
810 gtk_paint_flat_box(gtk_widget_get_style (widget), priv->bin_window,
811 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
812 &event->area, widget, "viewportbin",
815 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->expose_event (widget, event);
823 gtk_viewport_add (GtkContainer *container,
826 GtkBin *bin = GTK_BIN (container);
827 GtkViewport *viewport = GTK_VIEWPORT (bin);
828 GtkViewportPrivate *priv = viewport->priv;
830 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
832 gtk_widget_set_parent_window (child, priv->bin_window);
834 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
838 gtk_viewport_size_allocate (GtkWidget *widget,
839 GtkAllocation *allocation)
841 GtkAllocation widget_allocation;
842 GtkViewport *viewport = GTK_VIEWPORT (widget);
843 GtkViewportPrivate *priv = viewport->priv;
844 GtkBin *bin = GTK_BIN (widget);
846 gboolean hadjustment_value_changed, vadjustment_value_changed;
847 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
848 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
849 GtkAllocation child_allocation;
852 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
854 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
855 * redraw the shadow correctly.
857 gtk_widget_get_allocation (widget, &widget_allocation);
858 if (gtk_widget_get_mapped (widget) &&
859 priv->shadow_type != GTK_SHADOW_NONE &&
860 (widget_allocation.width != allocation->width ||
861 widget_allocation.height != allocation->height))
862 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
864 gtk_widget_set_allocation (widget, allocation);
866 viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
867 viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
869 child_allocation.x = 0;
870 child_allocation.y = 0;
871 child_allocation.width = hadjustment->upper;
872 child_allocation.height = vadjustment->upper;
873 if (gtk_widget_get_realized (widget))
875 GtkAllocation view_allocation;
877 gdk_window_move_resize (gtk_widget_get_window (widget),
878 allocation->x + border_width,
879 allocation->y + border_width,
880 allocation->width - border_width * 2,
881 allocation->height - border_width * 2);
883 viewport_get_view_allocation (viewport, &view_allocation);
884 gdk_window_move_resize (priv->view_window,
887 view_allocation.width,
888 view_allocation.height);
889 gdk_window_move_resize (priv->bin_window,
890 - hadjustment->value,
891 - vadjustment->value,
892 child_allocation.width,
893 child_allocation.height);
896 child = gtk_bin_get_child (bin);
897 if (child && gtk_widget_get_visible (child))
898 gtk_widget_size_allocate (child, &child_allocation);
900 gtk_adjustment_changed (hadjustment);
901 gtk_adjustment_changed (vadjustment);
902 if (hadjustment_value_changed)
903 gtk_adjustment_value_changed (hadjustment);
904 if (vadjustment_value_changed)
905 gtk_adjustment_value_changed (vadjustment);
909 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
912 GtkViewport *viewport = GTK_VIEWPORT (data);
913 GtkViewportPrivate *priv = viewport->priv;
914 GtkBin *bin = GTK_BIN (data);
917 child = gtk_bin_get_child (bin);
918 if (child && gtk_widget_get_visible (child) &&
919 gtk_widget_get_realized (GTK_WIDGET (viewport)))
921 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
922 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
926 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
927 new_x = - hadjustment->value;
928 new_y = - vadjustment->value;
930 if (new_x != old_x || new_y != old_y)
932 gdk_window_move (priv->bin_window, new_x, new_y);
933 gdk_window_process_updates (priv->bin_window, TRUE);
939 gtk_viewport_style_set (GtkWidget *widget,
940 GtkStyle *previous_style)
942 if (gtk_widget_get_realized (widget) &&
943 gtk_widget_get_has_window (widget))
946 GtkViewport *viewport = GTK_VIEWPORT (widget);
947 GtkViewportPrivate *priv = viewport->priv;
949 style = gtk_widget_get_style (widget);
950 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
951 gtk_style_set_background (style,
952 gtk_widget_get_window (widget),
953 gtk_widget_get_state (widget));
959 gtk_viewport_size_request_init (GtkSizeRequestIface *iface)
961 iface->get_width = gtk_viewport_get_width;
962 iface->get_height = gtk_viewport_get_height;
966 gtk_viewport_get_size (GtkSizeRequest *widget,
967 GtkOrientation orientation,
971 GtkViewport *viewport = GTK_VIEWPORT (widget);
972 GtkViewportPrivate *priv = viewport->priv;
975 gint child_min, child_nat;
976 gint minimum, natural;
978 child = gtk_bin_get_child (GTK_BIN (widget));
980 /* XXX This should probably be (border_width * 2); but GTK+ has
981 * been doing this with a single border for a while now...
983 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
985 if (priv->shadow_type != GTK_SHADOW_NONE)
987 style = gtk_widget_get_style (GTK_WIDGET (widget));
988 if (orientation == GTK_ORIENTATION_HORIZONTAL)
989 minimum += 2 * style->xthickness;
991 minimum += 2 * style->ythickness;
996 if (child && gtk_widget_get_visible (child))
998 if (orientation == GTK_ORIENTATION_HORIZONTAL)
999 gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
1001 gtk_size_request_get_height (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
1003 minimum += child_min;
1004 natural += child_nat;
1008 *minimum_size = minimum;
1011 *natural_size = natural;
1015 gtk_viewport_get_width (GtkSizeRequest *widget,
1019 gtk_viewport_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1023 gtk_viewport_get_height (GtkSizeRequest *widget,
1027 gtk_viewport_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);