1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkviewport.h"
32 #include "gtkmarshalers.h"
33 #include "gtkscrollable.h"
34 #include "gtktypebuiltins.h"
35 #include "gtkprivate.h"
40 * @Short_description: An adapter which makes widgets scrollable
42 * @See_also:#GtkScrolledWindow, #GtkAdjustment
44 * The #GtkViewport widget acts as an adaptor class, implementing
45 * scrollability for child widgets that lack their own scrolling
46 * capabilities. Use #GtkViewport to scroll child widgets such as
47 * #GtkTable, #GtkBox, and so on.
49 * If a widget has native scrolling abilities, such as #GtkTextView,
50 * #GtkTreeView or #GtkIconview, it can be added to a #GtkScrolledWindow
51 * with gtk_container_add(). If a widget does not, you must first add the
52 * widget to a #GtkViewport, then add the viewport to the scrolled window.
53 * The convenience function gtk_scrolled_window_add_with_viewport() does
54 * exactly this, so you can ignore the presence of the viewport.
56 * The #GtkViewport will start scrolling content only if allocated less
57 * than the child widget's minimum size in a given orientation.
60 struct _GtkViewportPrivate
62 GtkAdjustment *hadjustment;
63 GtkAdjustment *vadjustment;
64 GtkShadowType shadow_type;
66 GdkWindow *bin_window;
67 GdkWindow *view_window;
69 /* GtkScrollablePolicy needs to be checked when
70 * driving the scrollable adjustment values */
71 guint hscroll_policy : 1;
72 guint vscroll_policy : 1;
85 static void gtk_viewport_finalize (GObject *object);
86 static void gtk_viewport_set_property (GObject *object,
90 static void gtk_viewport_get_property (GObject *object,
94 static void gtk_viewport_destroy (GtkWidget *widget);
95 static void gtk_viewport_realize (GtkWidget *widget);
96 static void gtk_viewport_unrealize (GtkWidget *widget);
97 static gint gtk_viewport_draw (GtkWidget *widget,
99 static void gtk_viewport_add (GtkContainer *container,
101 static void gtk_viewport_size_allocate (GtkWidget *widget,
102 GtkAllocation *allocation);
103 static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
105 static void gtk_viewport_style_updated (GtkWidget *widget);
107 static void gtk_viewport_get_preferred_width (GtkWidget *widget,
110 static void gtk_viewport_get_preferred_height (GtkWidget *widget,
115 G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
116 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
119 gtk_viewport_class_init (GtkViewportClass *class)
121 GObjectClass *gobject_class;
122 GtkWidgetClass *widget_class;
123 GtkContainerClass *container_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;
133 widget_class->destroy = gtk_viewport_destroy;
134 widget_class->realize = gtk_viewport_realize;
135 widget_class->unrealize = gtk_viewport_unrealize;
136 widget_class->draw = gtk_viewport_draw;
137 widget_class->size_allocate = gtk_viewport_size_allocate;
138 widget_class->style_updated = gtk_viewport_style_updated;
139 widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
140 widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
142 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_VIEWPORT);
144 container_class->add = gtk_viewport_add;
146 /* GtkScrollable implementation */
147 g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment");
148 g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment");
149 g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
150 g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
152 g_object_class_install_property (gobject_class,
154 g_param_spec_enum ("shadow-type",
156 P_("Determines how the shadowed box around the viewport is drawn"),
157 GTK_TYPE_SHADOW_TYPE,
159 GTK_PARAM_READWRITE));
161 g_type_class_add_private (class, sizeof (GtkViewportPrivate));
165 gtk_viewport_set_property (GObject *object,
170 GtkViewport *viewport;
172 viewport = GTK_VIEWPORT (object);
176 case PROP_HADJUSTMENT:
177 gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
179 case PROP_VADJUSTMENT:
180 gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
182 case PROP_HSCROLL_POLICY:
183 viewport->priv->hscroll_policy = g_value_get_enum (value);
184 gtk_widget_queue_resize (GTK_WIDGET (viewport));
186 case PROP_VSCROLL_POLICY:
187 viewport->priv->vscroll_policy = g_value_get_enum (value);
188 gtk_widget_queue_resize (GTK_WIDGET (viewport));
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_HSCROLL_POLICY:
217 g_value_set_enum (value, priv->hscroll_policy);
219 case PROP_VSCROLL_POLICY:
220 g_value_set_enum (value, priv->vscroll_policy);
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;
255 * @hadjustment: horizontal adjustment.
256 * @vadjustment: vertical adjustment.
257 * @returns: a new #GtkViewport.
259 * Creates a new #GtkViewport with the given adjustments.
263 gtk_viewport_new (GtkAdjustment *hadjustment,
264 GtkAdjustment *vadjustment)
268 viewport = g_object_new (GTK_TYPE_VIEWPORT,
269 "hadjustment", hadjustment,
270 "vadjustment", vadjustment,
276 #define ADJUSTMENT_POINTER(viewport, orientation) \
277 (((orientation) == GTK_ORIENTATION_HORIZONTAL) ? \
278 &(viewport)->priv->hadjustment : &(viewport)->priv->vadjustment)
281 viewport_disconnect_adjustment (GtkViewport *viewport,
282 GtkOrientation orientation)
284 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
288 g_signal_handlers_disconnect_by_func (*adjustmentp,
289 gtk_viewport_adjustment_value_changed,
291 g_object_unref (*adjustmentp);
297 gtk_viewport_finalize (GObject *object)
299 GtkViewport *viewport = GTK_VIEWPORT (object);
301 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
302 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
304 G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
308 gtk_viewport_destroy (GtkWidget *widget)
310 GtkViewport *viewport = GTK_VIEWPORT (widget);
312 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
313 viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
315 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->destroy (widget);
319 viewport_get_view_allocation (GtkViewport *viewport,
320 GtkAllocation *view_allocation)
322 GtkViewportPrivate *priv = viewport->priv;
323 GtkWidget *widget = GTK_WIDGET (viewport);
324 GtkAllocation allocation;
325 GtkStyleContext *context;
327 GtkBorder padding, border;
330 gtk_widget_get_allocation (widget, &allocation);
331 border_width = gtk_container_get_border_width (GTK_CONTAINER (viewport));
333 view_allocation->x = 0;
334 view_allocation->y = 0;
336 context = gtk_widget_get_style_context (widget);
337 state = gtk_widget_get_state_flags (widget);
338 gtk_style_context_get_padding (context, state, &padding);
339 gtk_style_context_get_border (context, state, &border);
341 if (priv->shadow_type != GTK_SHADOW_NONE)
343 view_allocation->x = border.left;
344 view_allocation->y = border.top;
347 view_allocation->x += padding.left;
348 view_allocation->y += padding.right;
349 view_allocation->width = MAX (1, allocation.width - padding.left - padding.right - border_width * 2);
350 view_allocation->height = MAX (1, allocation.height - padding.top - padding.bottom - border_width * 2);
352 if (priv->shadow_type != GTK_SHADOW_NONE)
354 view_allocation->width = MAX (1, view_allocation->width - border.left - border.right);
355 view_allocation->height = MAX (1, view_allocation->height - border.top - border.bottom);
360 * gtk_viewport_get_hadjustment:
361 * @viewport: a #GtkViewport.
363 * Returns the horizontal adjustment of the viewport.
365 * Return value: (transfer none): the horizontal adjustment of @viewport.
367 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
370 gtk_viewport_get_hadjustment (GtkViewport *viewport)
372 GtkViewportPrivate *priv;
374 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
376 priv = viewport->priv;
378 if (!priv->hadjustment)
379 gtk_viewport_set_hadjustment (viewport, NULL);
381 return priv->hadjustment;
385 * gtk_viewport_get_vadjustment:
386 * @viewport: a #GtkViewport.
388 * Returns the vertical adjustment of the viewport.
390 * Return value: (transfer none): the vertical adjustment of @viewport.
392 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
395 gtk_viewport_get_vadjustment (GtkViewport *viewport)
397 GtkViewportPrivate *priv;
399 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
401 priv = viewport->priv;
403 if (!priv->vadjustment)
404 gtk_viewport_set_vadjustment (viewport, NULL);
406 return priv->vadjustment;
410 viewport_set_hadjustment_values (GtkViewport *viewport)
412 GtkBin *bin = GTK_BIN (viewport);
413 GtkAllocation view_allocation;
414 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
416 gdouble upper, value;
418 viewport_get_view_allocation (viewport, &view_allocation);
420 child = gtk_bin_get_child (bin);
421 if (child && gtk_widget_get_visible (child))
423 gint minimum_width, natural_width;
426 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
427 gtk_widget_get_preferred_height (child, &scroll_height, NULL);
429 gtk_widget_get_preferred_height (child, NULL, &scroll_height);
431 gtk_widget_get_preferred_width_for_height (child,
432 MAX (view_allocation.height, scroll_height),
436 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
437 upper = MAX (minimum_width, view_allocation.width);
439 upper = MAX (natural_width, view_allocation.width);
442 upper = view_allocation.width;
444 value = gtk_adjustment_get_value (hadjustment);
445 /* We clamp to the left in RTL mode */
446 if (gtk_widget_get_direction (GTK_WIDGET (viewport)) == GTK_TEXT_DIR_RTL)
448 gdouble dist = gtk_adjustment_get_upper (hadjustment)
450 - gtk_adjustment_get_page_size (hadjustment);
451 value = upper - dist - view_allocation.width;
454 gtk_adjustment_configure (hadjustment,
458 view_allocation.width * 0.1,
459 view_allocation.width * 0.9,
460 view_allocation.width);
464 viewport_set_vadjustment_values (GtkViewport *viewport)
466 GtkBin *bin = GTK_BIN (viewport);
467 GtkAllocation view_allocation;
468 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
472 viewport_get_view_allocation (viewport, &view_allocation);
474 child = gtk_bin_get_child (bin);
475 if (child && gtk_widget_get_visible (child))
477 gint minimum_height, natural_height;
480 if (viewport->priv->hscroll_policy == GTK_SCROLL_MINIMUM)
481 gtk_widget_get_preferred_width (child, &scroll_width, NULL);
483 gtk_widget_get_preferred_width (child, NULL, &scroll_width);
485 gtk_widget_get_preferred_height_for_width (child,
486 MAX (view_allocation.width, scroll_width),
490 if (viewport->priv->vscroll_policy == GTK_SCROLL_MINIMUM)
491 upper = MAX (minimum_height, view_allocation.height);
493 upper = MAX (natural_height, view_allocation.height);
496 upper = view_allocation.height;
498 gtk_adjustment_configure (vadjustment,
499 gtk_adjustment_get_value (vadjustment),
502 view_allocation.height * 0.1,
503 view_allocation.height * 0.9,
504 view_allocation.height);
508 viewport_set_adjustment (GtkViewport *viewport,
509 GtkOrientation orientation,
510 GtkAdjustment *adjustment)
512 GtkAdjustment **adjustmentp = ADJUSTMENT_POINTER (viewport, orientation);
514 if (adjustment && adjustment == *adjustmentp)
518 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 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);
526 viewport_set_vadjustment_values (viewport);
528 g_signal_connect (adjustment, "value-changed",
529 G_CALLBACK (gtk_viewport_adjustment_value_changed),
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.
542 * Deprecated: 3.0: Use gtk_scrollable_set_hadjustment()
545 gtk_viewport_set_hadjustment (GtkViewport *viewport,
546 GtkAdjustment *adjustment)
548 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
550 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
552 viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, adjustment);
554 g_object_notify (G_OBJECT (viewport), "hadjustment");
558 * gtk_viewport_set_vadjustment:
559 * @viewport: a #GtkViewport.
560 * @adjustment: (allow-none): a #GtkAdjustment.
562 * Sets the vertical adjustment of the viewport.
564 * Deprecated: 3.0: Use gtk_scrollable_set_vadjustment()
567 gtk_viewport_set_vadjustment (GtkViewport *viewport,
568 GtkAdjustment *adjustment)
570 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
572 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
574 viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, adjustment);
576 g_object_notify (G_OBJECT (viewport), "vadjustment");
580 * gtk_viewport_set_shadow_type:
581 * @viewport: a #GtkViewport.
582 * @type: the new shadow type.
584 * Sets the shadow type of the viewport.
587 gtk_viewport_set_shadow_type (GtkViewport *viewport,
590 GtkViewportPrivate *priv;
591 GtkAllocation allocation;
594 g_return_if_fail (GTK_IS_VIEWPORT (viewport));
596 widget = GTK_WIDGET (viewport);
597 priv = viewport->priv;
599 if ((GtkShadowType) priv->shadow_type != type)
601 priv->shadow_type = type;
603 if (gtk_widget_get_visible (widget))
605 gtk_widget_get_allocation (widget, &allocation);
606 gtk_widget_size_allocate (widget, &allocation);
607 gtk_widget_set_allocation (widget, &allocation);
608 gtk_widget_queue_draw (widget);
611 g_object_notify (G_OBJECT (viewport), "shadow-type");
616 * gtk_viewport_get_shadow_type:
617 * @viewport: a #GtkViewport
619 * Gets the shadow type of the #GtkViewport. See
620 * gtk_viewport_set_shadow_type().
622 * Return value: the shadow type
625 gtk_viewport_get_shadow_type (GtkViewport *viewport)
627 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), GTK_SHADOW_NONE);
629 return viewport->priv->shadow_type;
633 * gtk_viewport_get_bin_window:
634 * @viewport: a #GtkViewport
636 * Gets the bin window of the #GtkViewport.
638 * Return value: (transfer none): a #GdkWindow
643 gtk_viewport_get_bin_window (GtkViewport *viewport)
645 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
647 return viewport->priv->bin_window;
651 * gtk_viewport_get_view_window:
652 * @viewport: a #GtkViewport
654 * Gets the view window of the #GtkViewport.
656 * Return value: (transfer none): a #GdkWindow
661 gtk_viewport_get_view_window (GtkViewport *viewport)
663 g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
665 return viewport->priv->view_window;
669 gtk_viewport_realize (GtkWidget *widget)
671 GtkViewport *viewport = GTK_VIEWPORT (widget);
672 GtkViewportPrivate *priv = viewport->priv;
673 GtkBin *bin = GTK_BIN (widget);
674 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
675 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
676 GtkAllocation allocation;
677 GtkAllocation view_allocation;
678 GtkStyleContext *context;
681 GdkWindowAttr attributes;
682 gint attributes_mask;
686 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
688 gtk_widget_set_realized (widget, TRUE);
690 gtk_widget_get_allocation (widget, &allocation);
692 attributes.x = allocation.x + border_width;
693 attributes.y = allocation.y + border_width;
694 attributes.width = allocation.width - border_width * 2;
695 attributes.height = allocation.height - border_width * 2;
696 attributes.window_type = GDK_WINDOW_CHILD;
697 attributes.wclass = GDK_INPUT_OUTPUT;
698 attributes.visual = gtk_widget_get_visual (widget);
700 event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
701 /* We select on button_press_mask so that button 4-5 scrolls are trapped.
703 attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
705 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
707 window = gdk_window_new (gtk_widget_get_parent_window (widget),
708 &attributes, attributes_mask);
709 gtk_widget_set_window (widget, window);
710 gdk_window_set_user_data (window, viewport);
712 viewport_get_view_allocation (viewport, &view_allocation);
714 attributes.x = view_allocation.x;
715 attributes.y = view_allocation.y;
716 attributes.width = view_allocation.width;
717 attributes.height = view_allocation.height;
718 attributes.event_mask = 0;
720 priv->view_window = gdk_window_new (window,
721 &attributes, attributes_mask);
722 gdk_window_set_user_data (priv->view_window, viewport);
724 attributes.x = - gtk_adjustment_get_value (hadjustment);
725 attributes.y = - gtk_adjustment_get_value (vadjustment);
726 attributes.width = gtk_adjustment_get_upper (hadjustment);
727 attributes.height = gtk_adjustment_get_upper (vadjustment);
729 attributes.event_mask = event_mask;
731 priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
732 gdk_window_set_user_data (priv->bin_window, viewport);
734 child = gtk_bin_get_child (bin);
736 gtk_widget_set_parent_window (child, priv->bin_window);
738 context = gtk_widget_get_style_context (widget);
739 gtk_style_context_set_background (context, window);
740 gtk_style_context_set_background (context, priv->bin_window);
742 gdk_window_show (priv->bin_window);
743 gdk_window_show (priv->view_window);
747 gtk_viewport_unrealize (GtkWidget *widget)
749 GtkViewport *viewport = GTK_VIEWPORT (widget);
750 GtkViewportPrivate *priv = viewport->priv;
752 gdk_window_set_user_data (priv->view_window, NULL);
753 gdk_window_destroy (priv->view_window);
754 priv->view_window = NULL;
756 gdk_window_set_user_data (priv->bin_window, NULL);
757 gdk_window_destroy (priv->bin_window);
758 priv->bin_window = NULL;
760 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->unrealize (widget);
764 gtk_viewport_draw (GtkWidget *widget,
767 GtkViewport *viewport = GTK_VIEWPORT (widget);
768 GtkViewportPrivate *priv = viewport->priv;
769 GtkStyleContext *context;
772 context = gtk_widget_get_style_context (widget);
774 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
776 gtk_style_context_save (context);
777 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
779 gtk_render_frame (context, cr, 0, 0,
780 gdk_window_get_width (gtk_widget_get_window (widget)),
781 gdk_window_get_height (gtk_widget_get_window (widget)));
783 gtk_style_context_restore (context);
786 if (gtk_cairo_should_draw_window (cr, priv->view_window))
788 /* This is a cute hack to ensure the contents of bin_window are
789 * restricted to where they are visible. We only need to do this
790 * clipping when called via gtk_widget_draw() and not in expose
791 * events. And when that happens every window (including this one)
794 gdk_window_get_position (priv->view_window, &x, &y);
795 cairo_rectangle (cr, x, y,
796 gdk_window_get_width (priv->view_window),
797 gdk_window_get_height (priv->view_window));
801 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
803 gdk_window_get_position (priv->bin_window, &x, &y);
804 gtk_render_background (context, cr, x, y,
805 gdk_window_get_width (priv->bin_window),
806 gdk_window_get_height (priv->bin_window));
808 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
815 gtk_viewport_add (GtkContainer *container,
818 GtkBin *bin = GTK_BIN (container);
819 GtkViewport *viewport = GTK_VIEWPORT (bin);
820 GtkViewportPrivate *priv = viewport->priv;
822 g_return_if_fail (gtk_bin_get_child (bin) == NULL);
824 gtk_widget_set_parent_window (child, priv->bin_window);
826 GTK_CONTAINER_CLASS (gtk_viewport_parent_class)->add (container, child);
830 gtk_viewport_size_allocate (GtkWidget *widget,
831 GtkAllocation *allocation)
833 GtkAllocation widget_allocation;
834 GtkViewport *viewport = GTK_VIEWPORT (widget);
835 GtkViewportPrivate *priv = viewport->priv;
836 GtkBin *bin = GTK_BIN (widget);
838 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
839 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
840 GtkAllocation child_allocation;
843 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
845 /* If our size changed, and we have a shadow, queue a redraw on widget->window to
846 * redraw the shadow correctly.
848 gtk_widget_get_allocation (widget, &widget_allocation);
849 if (gtk_widget_get_mapped (widget) &&
850 priv->shadow_type != GTK_SHADOW_NONE &&
851 (widget_allocation.width != allocation->width ||
852 widget_allocation.height != allocation->height))
853 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
855 gtk_widget_set_allocation (widget, allocation);
857 g_object_freeze_notify (G_OBJECT (hadjustment));
858 g_object_freeze_notify (G_OBJECT (vadjustment));
860 viewport_set_hadjustment_values (viewport);
861 viewport_set_vadjustment_values (viewport);
863 child_allocation.x = 0;
864 child_allocation.y = 0;
865 child_allocation.width = gtk_adjustment_get_upper (hadjustment);
866 child_allocation.height = gtk_adjustment_get_upper (vadjustment);
867 if (gtk_widget_get_realized (widget))
869 GtkAllocation view_allocation;
871 gdk_window_move_resize (gtk_widget_get_window (widget),
872 allocation->x + border_width,
873 allocation->y + border_width,
874 allocation->width - border_width * 2,
875 allocation->height - border_width * 2);
877 viewport_get_view_allocation (viewport, &view_allocation);
878 gdk_window_move_resize (priv->view_window,
881 view_allocation.width,
882 view_allocation.height);
883 gdk_window_move_resize (priv->bin_window,
884 - gtk_adjustment_get_value (hadjustment),
885 - gtk_adjustment_get_value (vadjustment),
886 child_allocation.width,
887 child_allocation.height);
890 child = gtk_bin_get_child (bin);
891 if (child && gtk_widget_get_visible (child))
892 gtk_widget_size_allocate (child, &child_allocation);
894 g_object_thaw_notify (G_OBJECT (hadjustment));
895 g_object_thaw_notify (G_OBJECT (vadjustment));
899 gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
902 GtkViewport *viewport = GTK_VIEWPORT (data);
903 GtkViewportPrivate *priv = viewport->priv;
904 GtkBin *bin = GTK_BIN (data);
907 child = gtk_bin_get_child (bin);
908 if (child && gtk_widget_get_visible (child) &&
909 gtk_widget_get_realized (GTK_WIDGET (viewport)))
911 GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
912 GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
916 gdk_window_get_position (priv->bin_window, &old_x, &old_y);
917 new_x = - gtk_adjustment_get_value (hadjustment);
918 new_y = - gtk_adjustment_get_value (vadjustment);
920 if (new_x != old_x || new_y != old_y)
922 gdk_window_move (priv->bin_window, new_x, new_y);
923 gdk_window_process_updates (priv->bin_window, TRUE);
929 gtk_viewport_style_updated (GtkWidget *widget)
931 GTK_WIDGET_CLASS (gtk_viewport_parent_class)->style_updated (widget);
933 if (gtk_widget_get_realized (widget) &&
934 gtk_widget_get_has_window (widget))
936 GtkStyleContext *context;
937 GtkViewport *viewport = GTK_VIEWPORT (widget);
938 GtkViewportPrivate *priv = viewport->priv;
940 context = gtk_widget_get_style_context (widget);
941 gtk_style_context_set_background (context, priv->bin_window);
942 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
948 gtk_viewport_get_preferred_size (GtkWidget *widget,
949 GtkOrientation orientation,
953 GtkViewport *viewport = GTK_VIEWPORT (widget);
954 GtkViewportPrivate *priv = viewport->priv;
955 GtkStyleContext *context;
957 GtkBorder padding, border;
959 gint child_min, child_nat;
960 gint minimum, natural;
962 child = gtk_bin_get_child (GTK_BIN (widget));
964 /* XXX This should probably be (border_width * 2); but GTK+ has
965 * been doing this with a single border for a while now...
967 minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
969 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
970 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
971 gtk_style_context_get_padding (context, state, &padding);
973 if (priv->shadow_type != GTK_SHADOW_NONE)
975 gtk_style_context_get_border (context, state, &border);
977 if (orientation == GTK_ORIENTATION_HORIZONTAL)
978 minimum += border.left + border.right;
980 minimum += border.top + border.bottom;
983 if (orientation == GTK_ORIENTATION_HORIZONTAL)
984 minimum += padding.left + padding.right;
986 minimum += padding.top + padding.bottom;
990 if (child && gtk_widget_get_visible (child))
992 if (orientation == GTK_ORIENTATION_HORIZONTAL)
993 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
995 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
997 minimum += child_min;
998 natural += child_nat;
1002 *minimum_size = minimum;
1005 *natural_size = natural;
1009 gtk_viewport_get_preferred_width (GtkWidget *widget,
1013 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1017 gtk_viewport_get_preferred_height (GtkWidget *widget,
1021 gtk_viewport_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);