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.
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_destroy (GtkObject *object);
77 static void gtk_viewport_set_property (GObject *object,
81 static void gtk_viewport_get_property (GObject *object,
85 static void gtk_viewport_set_scroll_adjustments (GtkViewport *viewport,
86 GtkAdjustment *hadjustment,
87 GtkAdjustment *vadjustment);
88 static void gtk_viewport_realize (GtkWidget *widget);
89 static void gtk_viewport_unrealize (GtkWidget *widget);
90 static void gtk_viewport_paint (GtkWidget *widget,
92 static gint gtk_viewport_expose (GtkWidget *widget,
93 GdkEventExpose *event);
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_size_request_init (GtkSizeRequestIface *iface);
104 static void gtk_viewport_get_width (GtkSizeRequest *widget,
107 static void gtk_viewport_get_height (GtkSizeRequest *widget,
112 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
113 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
114 gtk_viewport_size_request_init))
117 gtk_viewport_class_init (GtkViewportClass *class)
119 GtkObjectClass *object_class;
120 GObjectClass *gobject_class;
121 GtkWidgetClass *widget_class;
122 GtkContainerClass *container_class;
124 object_class = (GtkObjectClass*) class;
125 gobject_class = G_OBJECT_CLASS (class);
126 widget_class = (GtkWidgetClass*) class;
127 container_class = (GtkContainerClass*) class;
129 gobject_class->finalize = gtk_viewport_finalize;
130 gobject_class->set_property = gtk_viewport_set_property;
131 gobject_class->get_property = gtk_viewport_get_property;
132 object_class->destroy = gtk_viewport_destroy;
134 widget_class->realize = gtk_viewport_realize;
135 widget_class->unrealize = gtk_viewport_unrealize;
136 widget_class->expose_event = gtk_viewport_expose;
137 widget_class->size_allocate = gtk_viewport_size_allocate;
138 widget_class->style_set = gtk_viewport_style_set;
140 container_class->add = gtk_viewport_add;
142 class->set_scroll_adjustments = gtk_viewport_set_scroll_adjustments;
144 g_object_class_install_property (gobject_class,
146 g_param_spec_object ("hadjustment",
147 P_("Horizontal adjustment"),
148 P_("The GtkAdjustment that determines the values of the horizontal position for this viewport"),
150 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
152 g_object_class_install_property (gobject_class,
154 g_param_spec_object ("vadjustment",
155 P_("Vertical adjustment"),
156 P_("The GtkAdjustment that determines the values of the vertical position for this viewport"),
158 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
160 g_object_class_install_property (gobject_class,
162 g_param_spec_enum ("shadow-type",
164 P_("Determines how the shadowed box around the viewport is drawn"),
165 GTK_TYPE_SHADOW_TYPE,
167 GTK_PARAM_READWRITE));
170 * GtkViewport::set-scroll-adjustments
171 * @horizontal: the horizontal #GtkAdjustment
172 * @vertical: the vertical #GtkAdjustment
174 * Set the scroll adjustments for the viewport. Usually scrolled containers
175 * like #GtkScrolledWindow will emit this signal to connect two instances
176 * of #GtkScrollbar to the scroll directions of the #GtkViewport.
178 widget_class->set_scroll_adjustments_signal =
179 g_signal_new (I_("set-scroll-adjustments"),
180 G_OBJECT_CLASS_TYPE (gobject_class),
181 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
182 G_STRUCT_OFFSET (GtkViewportClass, set_scroll_adjustments),
184 _gtk_marshal_VOID__OBJECT_OBJECT,
187 GTK_TYPE_ADJUSTMENT);
189 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
193 gtk_viewport_set_property (GObject *object,
198 GtkViewport *viewport;
200 viewport = GTK_VIEWPORT (object);
204 case PROP_HADJUSTMENT:
205 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
207 case PROP_VADJUSTMENT:
208 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
210 case PROP_SHADOW_TYPE:
211 gtk_viewport_set_shadow_type (viewport, g_value_get_enum (value));
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gtk_viewport_get_property (GObject *object,
225 GtkViewport *viewport = GTK_VIEWPORT (object);
226 GtkViewportPrivate *priv = viewport->priv;
230 case PROP_HADJUSTMENT:
231 g_value_set_object (value, priv->hadjustment);
233 case PROP_VADJUSTMENT:
234 g_value_set_object (value, priv->vadjustment);
236 case PROP_SHADOW_TYPE:
237 g_value_set_enum (value, priv->shadow_type);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 gtk_viewport_init (GtkViewport *viewport)
248 GtkViewportPrivate *priv;
250 viewport->priv = G_TYPE_INSTANCE_GET_PRIVATE (viewport,
253 priv = viewport->priv;
255 gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
257 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
258 gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
260 priv->shadow_type = GTK_SHADOW_IN;
261 priv->view_window = NULL;
262 priv->bin_window = NULL;
263 priv->hadjustment = NULL;
264 priv->vadjustment = NULL;
269 * @hadjustment: horizontal adjustment.
270 * @vadjustment: vertical adjustment.
271 * @returns: a new #GtkViewport.
273 * Creates a new #GtkViewport with the given adjustments.
277 gtk_viewport_new (GtkAdjustment *hadjustment,
278 GtkAdjustment *vadjustment)
282 viewport = g_object_new (GTK_TYPE_VIEWPORT,
283 "hadjustment", hadjustment,
284 "vadjustment", vadjustment,
290 #define ADJUSTMENT_POINTER(viewport, orientation) \
291 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
292 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
295 viewport_disconnect_adjustment (GtkViewport *viewport,
296 GtkOrientation orientation)
298 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
302 g_signal_handlers_disconnect_by_func (*adjustmentp,
303 gtk_viewport_adjustment_value_changed,
305 g_object_unref (*adjustmentp);
311 gtk_viewport_finalize (GObject *object)
313 GtkViewport *viewport = GTK_VIEWPORT (object);
315 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
316 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
318 G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
322 gtk_viewport_destroy (GtkObject *object)
324 GtkViewport *viewport = GTK_VIEWPORT (object);
326 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
327 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
329 GTK_OBJECT_CLASS (gtk_viewport_parent_class)->destroy (object);
333 * gtk_viewport_get_hadjustment:
334 * @viewport: a #GtkViewport.
336 * Returns the horizontal adjustment of the viewport.
338 * Return value: (transfer none): the horizontal adjustment of @viewport.
341 gtk_viewport_get_hadjustment (GtkViewport *viewport)
343 GtkViewportPrivate *priv;
345 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
347 priv = viewport->priv;
349 if (!priv->hadjustment)
350 gtk_viewport_set_hadjustment (viewport, NULL);
352 return priv->hadjustment;
356 * gtk_viewport_get_vadjustment:
357 * @viewport: a #GtkViewport.
359 * Returns the vertical adjustment of the viewport.
361 * Return value: (transfer none): the vertical adjustment of @viewport.
364 gtk_viewport_get_vadjustment (GtkViewport *viewport)
366 GtkViewportPrivate *priv;
368 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
370 priv = viewport->priv;
372 if (!priv->vadjustment)
373 gtk_viewport_set_vadjustment (viewport, NULL);
375 return priv->vadjustment;
379 viewport_get_view_allocation (GtkViewport *viewport,
380 GtkAllocation *view_allocation)
382 GtkViewportPrivate *priv = viewport->priv;
384 GtkWidget *widget = GTK_WIDGET (viewport);
385 GtkAllocation allocation;
388 gtk_widget_get_allocation (widget, &allocation);
389 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
391 view_allocation->x = 0;
392 view_allocation->y = 0;
394 if (priv->shadow_type != GTK_SHADOW_NONE)
396 style = gtk_widget_get_style (widget);
397 view_allocation->x = style->xthickness;
398 view_allocation->y = style->ythickness;
401 view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
402 view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
406 viewport_reclamp_adjustment (GtkAdjustment *adjustment,
407 gboolean *value_changed)
409 gdouble value = adjustment->value;
411 value = CLAMP (value, 0, adjustment->upper - adjustment->page_size);
412 if (value != adjustment->value)
414 adjustment->value = value;
416 *value_changed = TRUE;
418 else if (value_changed)
419 *value_changed = FALSE;
423 viewport_set_hadjustment_values (GtkViewport *viewport,
424 gboolean *value_changed)
426 GtkBin *bin = GTK_BIN (viewport);
427 GtkAllocation view_allocation;
428 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
430 gdouble old_page_size;
434 viewport_get_view_allocation (viewport, &view_allocation);
436 old_page_size = hadjustment->page_size;
437 old_upper = hadjustment->upper;
438 old_value = hadjustment->value;
439 hadjustment->page_size = view_allocation.width;
440 hadjustment->step_increment = view_allocation.width * 0.1;
441 hadjustment->page_increment = view_allocation.width * 0.9;
443 hadjustment->lower = 0;
445 child = gtk_bin_get_child (bin);
446 if (child && gtk_widget_get_visible (child))
450 gtk_size_request_get_width_for_height (GTK_SIZE_REQUEST (child),
451 view_allocation.height,
454 hadjustment->upper = MAX (minimum_width, view_allocation.width);
457 hadjustment->upper = view_allocation.width;
459 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
461 gdouble dist = old_upper - (old_value + old_page_size);
462 hadjustment->value = hadjustment->upper - dist - hadjustment->page_size;
463 viewport_reclamp_adjustment (hadjustment, value_changed);
464 *value_changed = (old_value != hadjustment->value);
467 viewport_reclamp_adjustment (hadjustment, value_changed);
471 viewport_set_vadjustment_values (GtkViewport *viewport,
472 gboolean *value_changed)
474 GtkBin *bin = GTK_BIN (viewport);
475 GtkAllocation view_allocation;
476 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
479 viewport_get_view_allocation (viewport, &view_allocation);
481 vadjustment->page_size = view_allocation.height;
482 vadjustment->step_increment = view_allocation.height * 0.1;
483 vadjustment->page_increment = view_allocation.height * 0.9;
485 vadjustment->lower = 0;
487 child = gtk_bin_get_child (bin);
488 if (child && gtk_widget_get_visible (child))
492 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child),
493 view_allocation.width,
497 vadjustment->upper = MAX (minimum_height, view_allocation.height);
500 vadjustment->upper = view_allocation.height;
502 viewport_reclamp_adjustment (vadjustment, value_changed);
506 viewport_set_adjustment (GtkViewport *viewport,
507 GtkOrientation orientation,
508 GtkAdjustment *adjustment)
510 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
511 gboolean value_changed;
513 if (adjustment && adjustment == *adjustmentp)
517 adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
519 viewport_disconnect_adjustment (viewport, orientation);
520 *adjustmentp = adjustment;
521 g_object_ref_sink (adjustment);
523 if (orientation == GTK_ORIENTATION_HORIZONTAL)
524 viewport_set_hadjustment_values (viewport, &value_changed);
526 viewport_set_vadjustment_values (viewport, &value_changed);
528 g_signal_connect (adjustment, "value-changed",
529 G_CALLBACK (gtk_viewport_adjustment_value_changed),
532 gtk_adjustment_changed (adjustment);
535 gtk_adjustment_value_changed (adjustment);
537 gtk_viewport_adjustment_value_changed (adjustment, viewport);
541 * gtk_viewport_set_hadjustment:
542 * @viewport: a #GtkViewport.
543 * @adjustment: (allow-none): a #GtkAdjustment.
545 * Sets the horizontal adjustment of the viewport.
548 gtk_viewport_set_hadjustment (GtkViewport *viewport,
549 GtkAdjustment *adjustment)
551 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
553 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
555 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
557 g_object_notify (G_OBJECT (viewport), "hadjustment");
561 * gtk_viewport_set_vadjustment:
562 * @viewport: a #GtkViewport.
563 * @adjustment: (allow-none): a #GtkAdjustment.
565 * Sets the vertical adjustment of the viewport.
568 gtk_viewport_set_vadjustment (GtkViewport *viewport,
569 GtkAdjustment *adjustment)
571 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
573 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
575 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
577 g_object_notify (G_OBJECT (viewport), "vadjustment");
581 gtk_viewport_set_scroll_adjustments (GtkViewport *viewport,
582 GtkAdjustment *hadjustment,
583 GtkAdjustment *vadjustment)
585 gtk_viewport_set_hadjustment (viewport, hadjustment);
586 gtk_viewport_set_vadjustment (viewport, vadjustment);
590 * gtk_viewport_set_shadow_type:
591 * @viewport: a #GtkViewport.
592 * @type: the new shadow type.
594 * Sets the shadow type of the viewport.
597 gtk_viewport_set_shadow_type (GtkViewport *viewport,
600 GtkViewportPrivate *priv;
601 GtkAllocation allocation;
604 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
606 widget = GTK_WIDGET (viewport);
607 priv = viewport->priv;
609 if ((GtkShadowType) priv->shadow_type != type)
611 priv->shadow_type = type;
613 if (gtk_widget_get_visible (widget))
615 gtk_widget_get_allocation (widget, &allocation);
616 gtk_widget_size_allocate (widget, &allocation);
617 gtk_widget_set_allocation (widget, &allocation);
618 gtk_widget_queue_draw (widget);
621 g_object_notify (G_OBJECT (viewport), "shadow-type");
626 * gtk_viewport_get_shadow_type:
627 * @viewport: a #GtkViewport
629 * Gets the shadow type of the #GtkViewport. See
630 * gtk_viewport_set_shadow_type().
632 * Return value: the shadow type
635 gtk_viewport_get_shadow_type (GtkViewport *viewport)
637 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
639 return viewport->priv->shadow_type;
643 * gtk_viewport_get_bin_window:
644 * @viewport: a #GtkViewport
646 * Gets the bin window of the #GtkViewport.
648 * Return value: (transfer none): a #GdkWindow
653 gtk_viewport_get_bin_window (GtkViewport *viewport)
655 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
657 return viewport->priv->bin_window;
661 * gtk_viewport_get_view_window:
662 * @viewport: a #GtkViewport
664 * Gets the view window of the #GtkViewport.
666 * Return value: (transfer none): a #GdkWindow
671 gtk_viewport_get_view_window (GtkViewport *viewport)
673 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
675 return viewport->priv->view_window;
679 gtk_viewport_realize (GtkWidget *widget)
681 GtkViewport *viewport = GTK_VIEWPORT (widget);
682 GtkViewportPrivate *priv = viewport->priv;
683 GtkBin *bin = GTK_BIN (widget);
684 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
685 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
686 GtkAllocation allocation;
687 GtkAllocation view_allocation;
691 GdkWindowAttr attributes;
692 gint attributes_mask;
696 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
698 gtk_widget_set_realized (widget, TRUE);
700 gtk_widget_get_allocation (widget, &allocation);
702 attributes.x = allocation.x + border_width;
703 attributes.y = allocation.y + border_width;
704 attributes.width = allocation.width - border_width * 2;
705 attributes.height = allocation.height - border_width * 2;
706 attributes.window_type = GDK_WINDOW_CHILD;
707 attributes.wclass = GDK_INPUT_OUTPUT;
708 attributes.visual = gtk_widget_get_visual (widget);
710 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
711 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
713 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
715 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
717 window = gdk_window_new (gtk_widget_get_parent_window (widget),
718 &attributes, attributes_mask);
719 gtk_widget_set_window (widget, window);
720 gdk_window_set_user_data (window, viewport);
722 viewport_get_view_allocation (viewport, &view_allocation);
724 attributes.x = view_allocation.x;
725 attributes.y = view_allocation.y;
726 attributes.width = view_allocation.width;
727 attributes.height = view_allocation.height;
728 attributes.event_mask = 0;
730 priv->view_window = gdk_window_new (window,
731 &attributes, attributes_mask);
732 gdk_window_set_user_data (priv->view_window, viewport);
734 attributes.x = - hadjustment->value;
735 attributes.y = - vadjustment->value;
736 attributes.width = hadjustment->upper;
737 attributes.height = vadjustment->upper;
739 attributes.event_mask = event_mask;
741 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
742 gdk_window_set_user_data (priv->bin_window, viewport);
744 child = gtk_bin_get_child (bin);
746 gtk_widget_set_parent_window (child, priv->bin_window);
748 gtk_widget_style_attach (widget);
749 style = gtk_widget_get_style (widget);
750 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
751 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
753 gdk_window_show (priv->bin_window);
754 gdk_window_show (priv->view_window);
758 gtk_viewport_unrealize (GtkWidget *widget)
760 GtkViewport *viewport = GTK_VIEWPORT (widget);
761 GtkViewportPrivate *priv = viewport->priv;
763 gdk_window_set_user_data (priv->view_window, NULL);
764 gdk_window_destroy (priv->view_window);
765 priv->view_window = NULL;
767 gdk_window_set_user_data (priv->bin_window, NULL);
768 gdk_window_destroy (priv->bin_window);
769 priv->bin_window = NULL;
771 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
775 gtk_viewport_paint (GtkWidget *widget,
778 if (gtk_widget_is_drawable (widget))
780 GtkViewport *viewport = GTK_VIEWPORT (widget);
781 GtkViewportPrivate *priv = viewport->priv;
783 gtk_paint_shadow (gtk_widget_get_style (widget),
784 gtk_widget_get_window (widget),
785 GTK_STATE_NORMAL, priv->shadow_type,
786 area, widget, "viewport",
792 gtk_viewport_expose (GtkWidget *widget,
793 GdkEventExpose *event)
795 GtkViewport *viewport;
797 if (gtk_widget_is_drawable (widget))
799 viewport = GTK_VIEWPORT (widget);
800 GtkViewportPrivate *priv = viewport->priv;
802 if (event->window == gtk_widget_get_window (widget))
803 gtk_viewport_paint (widget, &event->area);
804 else if (event->window == priv->bin_window)
806 gtk_paint_flat_box(gtk_widget_get_style (widget), priv->bin_window,
807 GTK_STATE_NORMAL, GTK_SHADOW_NONE,
808 &event->area, widget, "viewportbin",
811 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->expose_event (widget, event);
819 gtk_viewport_add (GtkContainer *container,
822 GtkBin *bin = GTK_BIN (container);
823 GtkViewport *viewport = GTK_VIEWPORT (bin);
824 GtkViewportPrivate *priv = viewport->priv;
826 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
828 gtk_widget_set_parent_window (child, priv->bin_window);
830 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
834 gtk_viewport_size_allocate (GtkWidget *widget,
835 GtkAllocation *allocation)
837 GtkAllocation widget_allocation;
838 GtkViewport *viewport = GTK_VIEWPORT (widget);
839 GtkViewportPrivate *priv = viewport->priv;
840 GtkBin *bin = GTK_BIN (widget);
842 gboolean hadjustment_value_changed, vadjustment_value_changed;
843 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
844 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
845 GtkAllocation child_allocation;
848 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
850 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
851 * redraw the shadow correctly.
853 gtk_widget_get_allocation (widget, &widget_allocation);
854 if (gtk_widget_get_mapped (widget) &&
855 priv->shadow_type != GTK_SHADOW_NONE &&
856 (widget_allocation.width != allocation->width ||
857 widget_allocation.height != allocation->height))
858 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
860 gtk_widget_set_allocation (widget, allocation);
862 viewport_set_hadjustment_values (viewport, &hadjustment_value_changed);
863 viewport_set_vadjustment_values (viewport, &vadjustment_value_changed);
865 child_allocation.x = 0;
866 child_allocation.y = 0;
867 child_allocation.width = hadjustment->upper;
868 child_allocation.height = vadjustment->upper;
869 if (gtk_widget_get_realized (widget))
871 GtkAllocation view_allocation;
873 gdk_window_move_resize (gtk_widget_get_window (widget),
874 allocation->x + border_width,
875 allocation->y + border_width,
876 allocation->width - border_width * 2,
877 allocation->height - border_width * 2);
879 viewport_get_view_allocation (viewport, &view_allocation);
880 gdk_window_move_resize (priv->view_window,
883 view_allocation.width,
884 view_allocation.height);
885 gdk_window_move_resize (priv->bin_window,
886 - hadjustment->value,
887 - vadjustment->value,
888 child_allocation.width,
889 child_allocation.height);
892 child = gtk_bin_get_child (bin);
893 if (child && gtk_widget_get_visible (child))
894 gtk_widget_size_allocate (child, &child_allocation);
896 gtk_adjustment_changed (hadjustment);
897 gtk_adjustment_changed (vadjustment);
898 if (hadjustment_value_changed)
899 gtk_adjustment_value_changed (hadjustment);
900 if (vadjustment_value_changed)
901 gtk_adjustment_value_changed (vadjustment);
905 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
908 GtkViewport *viewport = GTK_VIEWPORT (data);
909 GtkViewportPrivate *priv = viewport->priv;
910 GtkBin *bin = GTK_BIN (data);
913 child = gtk_bin_get_child (bin);
914 if (child && gtk_widget_get_visible (child) &&
915 gtk_widget_get_realized (GTK_WIDGET (viewport)))
917 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
918 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
922 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
923 new_x = - hadjustment->value;
924 new_y = - vadjustment->value;
926 if (new_x != old_x || new_y != old_y)
928 gdk_window_move (priv->bin_window, new_x, new_y);
929 gdk_window_process_updates (priv->bin_window, TRUE);
935 gtk_viewport_style_set (GtkWidget *widget,
936 GtkStyle *previous_style)
938 if (gtk_widget_get_realized (widget) &&
939 gtk_widget_get_has_window (widget))
942 GtkViewport *viewport = GTK_VIEWPORT (widget);
943 GtkViewportPrivate *priv = viewport->priv;
945 style = gtk_widget_get_style (widget);
946 gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
947 gtk_style_set_background (style,
948 gtk_widget_get_window (widget),
949 gtk_widget_get_state (widget));
955 gtk_viewport_size_request_init (GtkSizeRequestIface *iface)
957 iface->get_width = gtk_viewport_get_width;
958 iface->get_height = gtk_viewport_get_height;
962 gtk_viewport_get_size (GtkSizeRequest *widget,
963 GtkOrientation orientation,
967 GtkViewport *viewport = GTK_VIEWPORT (widget);
968 GtkViewportPrivate *priv = viewport->priv;
971 gint child_min, child_nat;
972 gint minimum, natural;
974 child = gtk_bin_get_child (GTK_BIN (widget));
976 /* XXX This should probably be (border_width * 2); but GTK+ has
977 * been doing this with a single border for a while now...
979 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
981 if (priv->shadow_type != GTK_SHADOW_NONE)
983 style = gtk_widget_get_style (GTK_WIDGET (widget));
984 if (orientation == GTK_ORIENTATION_HORIZONTAL)
985 minimum += 2 * style->xthickness;
987 minimum += 2 * style->ythickness;
992 if (child && gtk_widget_get_visible (child))
994 if (orientation == GTK_ORIENTATION_HORIZONTAL)
995 gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
997 gtk_size_request_get_height (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
999 minimum += child_min;
1000 natural += child_nat;
1004 *minimum_size = minimum;
1007 *natural_size = natural;
1011 gtk_viewport_get_width (GtkSizeRequest *widget,
1015 gtk_viewport_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1019 gtk_viewport_get_height (GtkSizeRequest *widget,
1023 gtk_viewport_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);