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/.
31 #include "gtkbindings.h"
32 #include "gtkmarshalers.h"
33 #include "gtkscrollable.h"
34 #include "gtkscrollbar.h"
35 #include "gtkscrolledwindow.h"
36 #include "gtkwindow.h"
37 #include "gtkviewport.h"
38 #include "gtkprivate.h"
39 #include "gtktypebuiltins.h"
41 #include "a11y/gtkscrolledwindowaccessible.h"
44 * SECTION:gtkscrolledwindow
45 * @Short_description: Adds scrollbars to its child widget
46 * @Title: GtkScrolledWindow
47 * @See_also: #GtkScrollable, #GtkViewport, #GtkAdjustment
49 * #GtkScrolledWindow is a #GtkBin subclass: it's a container
50 * the accepts a single child widget. #GtkScrolledWindow adds scrollbars
51 * to the child widget and optionally draws a beveled frame around the
54 * The scrolled window can work in two ways. Some widgets have native
55 * scrolling support; these widgets implement the #GtkScrollable interface.
56 * Widgets with native scroll support include #GtkTreeView, #GtkTextView,
59 * For widgets that lack native scrolling support, the #GtkViewport
60 * widget acts as an adaptor class, implementing scrollability for child
61 * widgets that lack their own scrolling capabilities. Use #GtkViewport
62 * to scroll child widgets such as #GtkGrid, #GtkBox, and so on.
64 * If a widget has native scrolling abilities, it can be added to the
65 * #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
66 * must first add the widget to a #GtkViewport, then add the #GtkViewport
67 * to the scrolled window. The convenience function
68 * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
69 * ignore the presence of the viewport.
71 * The position of the scrollbars is controlled by the scroll
72 * adjustments. See #GtkAdjustment for the fields in an adjustment - for
73 * #GtkScrollbar, used by #GtkScrolledWindow, the "value" field
74 * represents the position of the scrollbar, which must be between the
75 * "lower" field and "upper - page_size." The "page_size" field
76 * represents the size of the visible scrollable area. The
77 * "step_increment" and "page_increment" fields are used when the user
78 * asks to step down (using the small stepper arrows) or page down (using
79 * for example the PageDown key).
81 * If a #GtkScrolledWindow doesn't behave quite as you would like, or
82 * doesn't have exactly the right layout, it's very possible to set up
83 * your own scrolling with #GtkScrollbar and for example a #GtkGrid.
87 /* scrolled window policy and size requisition handling:
89 * gtk size requisition works as follows:
90 * a widget upon size-request reports the width and height that it finds
91 * to be best suited to display its contents, including children.
92 * the width and/or height reported from a widget upon size requisition
93 * may be overidden by the user by specifying a width and/or height
94 * other than 0 through gtk_widget_set_size_request().
96 * a scrolled window needs (for implementing all three policy types) to
97 * request its width and height based on two different rationales.
98 * 1) the user wants the scrolled window to just fit into the space
99 * that it gets allocated for a specifc dimension.
100 * 1.1) this does not apply if the user specified a concrete value
101 * value for that specific dimension by either specifying usize for the
102 * scrolled window or for its child.
103 * 2) the user wants the scrolled window to take as much space up as
104 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
106 * also, kinda obvious:
107 * 3) a user would certainly not have choosen a scrolled window as a container
108 * for the child, if the resulting allocation takes up more space than the
109 * child would have allocated without the scrolled window.
112 * A) from 1) follows: the scrolled window shouldn't request more space for a
113 * specifc dimension than is required at minimum.
114 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
115 * window (done automatically) or by usize of the child (needs to be checked).
116 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
118 * D) from 3) follows: the scrolled window child's minimum width and minimum height
119 * under A) at least correspond to the space taken up by its scrollbars.
122 #define DEFAULT_SCROLLBAR_SPACING 3
124 struct _GtkScrolledWindowPrivate
126 GtkWidget *hscrollbar;
127 GtkWidget *vscrollbar;
129 GtkCornerType real_window_placement;
132 guint window_placement_set : 1;
133 guint hscrollbar_policy : 2;
134 guint vscrollbar_policy : 2;
135 guint hscrollbar_visible : 1;
136 guint vscrollbar_visible : 1;
137 guint window_placement : 2;
138 guint focus_out : 1; /* Flag used by ::move-focus-out implementation */
140 gint min_content_width;
141 gint min_content_height;
149 PROP_HSCROLLBAR_POLICY,
150 PROP_VSCROLLBAR_POLICY,
151 PROP_WINDOW_PLACEMENT,
152 PROP_WINDOW_PLACEMENT_SET,
154 PROP_MIN_CONTENT_WIDTH,
155 PROP_MIN_CONTENT_HEIGHT
166 static void gtk_scrolled_window_set_property (GObject *object,
170 static void gtk_scrolled_window_get_property (GObject *object,
175 static void gtk_scrolled_window_destroy (GtkWidget *widget);
176 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
177 GdkScreen *previous_screen);
178 static gboolean gtk_scrolled_window_draw (GtkWidget *widget,
180 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
181 GtkAllocation *allocation);
182 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
183 GdkEventScroll *event);
184 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
185 GtkDirectionType direction);
186 static void gtk_scrolled_window_add (GtkContainer *container,
188 static void gtk_scrolled_window_remove (GtkContainer *container,
190 static void gtk_scrolled_window_forall (GtkContainer *container,
191 gboolean include_internals,
192 GtkCallback callback,
193 gpointer callback_data);
194 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
195 GtkScrollType scroll,
196 gboolean horizontal);
197 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
198 GtkDirectionType direction_type);
200 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
201 GtkAllocation *allocation);
202 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
205 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
207 static void gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
210 static void gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
213 static void gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *layout,
215 gint *minimum_height,
216 gint *natural_height);
217 static void gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *layout,
219 gint *minimum_height,
220 gint *natural_height);
222 static guint signals[LAST_SIGNAL] = {0};
224 G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
228 add_scroll_binding (GtkBindingSet *binding_set,
230 GdkModifierType mask,
231 GtkScrollType scroll,
234 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
236 gtk_binding_entry_add_signal (binding_set, keyval, mask,
238 GTK_TYPE_SCROLL_TYPE, scroll,
239 G_TYPE_BOOLEAN, horizontal);
240 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
242 GTK_TYPE_SCROLL_TYPE, scroll,
243 G_TYPE_BOOLEAN, horizontal);
247 add_tab_bindings (GtkBindingSet *binding_set,
248 GdkModifierType modifiers,
249 GtkDirectionType direction)
251 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
253 GTK_TYPE_DIRECTION_TYPE, direction);
254 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
256 GTK_TYPE_DIRECTION_TYPE, direction);
260 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
262 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
263 GtkWidgetClass *widget_class;
264 GtkContainerClass *container_class;
265 GtkBindingSet *binding_set;
267 widget_class = (GtkWidgetClass*) class;
268 container_class = (GtkContainerClass*) class;
270 gobject_class->set_property = gtk_scrolled_window_set_property;
271 gobject_class->get_property = gtk_scrolled_window_get_property;
273 widget_class->destroy = gtk_scrolled_window_destroy;
274 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
275 widget_class->draw = gtk_scrolled_window_draw;
276 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
277 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
278 widget_class->focus = gtk_scrolled_window_focus;
279 widget_class->get_preferred_width = gtk_scrolled_window_get_preferred_width;
280 widget_class->get_preferred_height = gtk_scrolled_window_get_preferred_height;
281 widget_class->get_preferred_height_for_width = gtk_scrolled_window_get_preferred_height_for_width;
282 widget_class->get_preferred_width_for_height = gtk_scrolled_window_get_preferred_width_for_height;
284 container_class->add = gtk_scrolled_window_add;
285 container_class->remove = gtk_scrolled_window_remove;
286 container_class->forall = gtk_scrolled_window_forall;
287 gtk_container_class_handle_border_width (container_class);
289 class->scrollbar_spacing = -1;
291 class->scroll_child = gtk_scrolled_window_scroll_child;
292 class->move_focus_out = gtk_scrolled_window_move_focus_out;
294 g_object_class_install_property (gobject_class,
296 g_param_spec_object ("hadjustment",
297 P_("Horizontal Adjustment"),
298 P_("The GtkAdjustment for the horizontal position"),
300 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
301 g_object_class_install_property (gobject_class,
303 g_param_spec_object ("vadjustment",
304 P_("Vertical Adjustment"),
305 P_("The GtkAdjustment for the vertical position"),
307 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
308 g_object_class_install_property (gobject_class,
309 PROP_HSCROLLBAR_POLICY,
310 g_param_spec_enum ("hscrollbar-policy",
311 P_("Horizontal Scrollbar Policy"),
312 P_("When the horizontal scrollbar is displayed"),
313 GTK_TYPE_POLICY_TYPE,
314 GTK_POLICY_AUTOMATIC,
315 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
316 g_object_class_install_property (gobject_class,
317 PROP_VSCROLLBAR_POLICY,
318 g_param_spec_enum ("vscrollbar-policy",
319 P_("Vertical Scrollbar Policy"),
320 P_("When the vertical scrollbar is displayed"),
321 GTK_TYPE_POLICY_TYPE,
322 GTK_POLICY_AUTOMATIC,
323 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
325 g_object_class_install_property (gobject_class,
326 PROP_WINDOW_PLACEMENT,
327 g_param_spec_enum ("window-placement",
328 P_("Window Placement"),
329 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
330 GTK_TYPE_CORNER_TYPE,
332 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
335 * GtkScrolledWindow:window-placement-set:
337 * Whether "window-placement" should be used to determine the location
338 * of the contents with respect to the scrollbars. Otherwise, the
339 * "gtk-scrolled-window-placement" setting is used.
343 g_object_class_install_property (gobject_class,
344 PROP_WINDOW_PLACEMENT_SET,
345 g_param_spec_boolean ("window-placement-set",
346 P_("Window Placement Set"),
347 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
349 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
350 g_object_class_install_property (gobject_class,
352 g_param_spec_enum ("shadow-type",
354 P_("Style of bevel around the contents"),
355 GTK_TYPE_SHADOW_TYPE,
357 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
360 * GtkScrolledWindow:scrollbars-within-bevel:
362 * Whether to place scrollbars within the scrolled window's bevel.
366 gtk_widget_class_install_style_property (widget_class,
367 g_param_spec_boolean ("scrollbars-within-bevel",
368 P_("Scrollbars within bevel"),
369 P_("Place scrollbars within the scrolled window's bevel"),
371 GTK_PARAM_READABLE));
373 gtk_widget_class_install_style_property (widget_class,
374 g_param_spec_int ("scrollbar-spacing",
375 P_("Scrollbar spacing"),
376 P_("Number of pixels between the scrollbars and the scrolled window"),
379 DEFAULT_SCROLLBAR_SPACING,
380 GTK_PARAM_READABLE));
383 * GtkScrolledWindow:min-content-width:
385 * The minimum content width of @scrolled_window, or -1 if not set.
389 g_object_class_install_property (gobject_class,
390 PROP_MIN_CONTENT_WIDTH,
391 g_param_spec_int ("min-content-width",
392 P_("Minimum Content Width"),
393 P_("The minimum width that the scrolled window will allocate to its content"),
395 GTK_PARAM_READWRITE));
398 * GtkScrolledWindow:min-content-height:
400 * The minimum content height of @scrolled_window, or -1 if not set.
404 g_object_class_install_property (gobject_class,
405 PROP_MIN_CONTENT_HEIGHT,
406 g_param_spec_int ("min-content-height",
407 P_("Minimum Content Height"),
408 P_("The minimum height that the scrolled window will allocate to its content"),
410 GTK_PARAM_READWRITE));
412 * GtkScrolledWindow::scroll-child:
413 * @scrolled_window: a #GtkScrolledWindow
414 * @scroll: a #GtkScrollType describing how much to scroll
415 * @horizontal: whether the keybinding scrolls the child
416 * horizontally or not
418 * The ::scroll-child signal is a
419 * <link linkend="keybinding-signals">keybinding signal</link>
420 * which gets emitted when a keybinding that scrolls is pressed.
421 * The horizontal or vertical adjustment is updated which triggers a
422 * signal that the scrolled windows child may listen to and scroll itself.
424 signals[SCROLL_CHILD] =
425 g_signal_new (I_("scroll-child"),
426 G_TYPE_FROM_CLASS (gobject_class),
427 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
428 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
430 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
432 GTK_TYPE_SCROLL_TYPE,
436 * GtkScrolledWindow::move-focus-out:
437 * @scrolled_window: a #GtkScrolledWindow
438 * @direction_type: either %GTK_DIR_TAB_FORWARD or
439 * %GTK_DIR_TAB_BACKWARD
441 * The ::move-focus-out signal is a
442 * <link linkend="keybinding-signals">keybinding signal</link>
443 * which gets emitted when focus is moved away from the scrolled
444 * window by a keybinding.
445 * The #GtkWidget::move-focus signal is emitted with @direction_type
446 * on this scrolled windows toplevel parent in the container hierarchy.
447 * The default bindings for this signal are
448 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap></keycombo>
450 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap><keycap>Shift</keycap></keycombo>.
452 signals[MOVE_FOCUS_OUT] =
453 g_signal_new (I_("move-focus-out"),
454 G_TYPE_FROM_CLASS (gobject_class),
455 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
456 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
458 _gtk_marshal_VOID__ENUM,
460 GTK_TYPE_DIRECTION_TYPE);
462 binding_set = gtk_binding_set_by_class (class);
464 add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
465 add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
466 add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
467 add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
469 add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
470 add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
471 add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
472 add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
474 add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
475 add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
476 add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
477 add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
479 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
480 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
482 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
484 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCROLLED_WINDOW_ACCESSIBLE);
488 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
490 GtkScrolledWindowPrivate *priv;
492 scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
493 GTK_TYPE_SCROLLED_WINDOW,
494 GtkScrolledWindowPrivate);
496 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
497 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
499 priv->hscrollbar = NULL;
500 priv->vscrollbar = NULL;
501 priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
502 priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
503 priv->hscrollbar_visible = FALSE;
504 priv->vscrollbar_visible = FALSE;
505 priv->focus_out = FALSE;
506 priv->window_placement = GTK_CORNER_TOP_LEFT;
507 gtk_scrolled_window_update_real_placement (scrolled_window);
508 priv->min_content_width = -1;
509 priv->min_content_height = -1;
513 * gtk_scrolled_window_new:
514 * @hadjustment: (allow-none): horizontal adjustment
515 * @vadjustment: (allow-none): vertical adjustment
517 * Creates a new scrolled window.
519 * The two arguments are the scrolled window's adjustments; these will be
520 * shared with the scrollbars and the child widget to keep the bars in sync
521 * with the child. Usually you want to pass %NULL for the adjustments, which
522 * will cause the scrolled window to create them for you.
524 * Returns: a new scrolled window
527 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
528 GtkAdjustment *vadjustment)
530 GtkWidget *scrolled_window;
533 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
536 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
538 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
539 "hadjustment", hadjustment,
540 "vadjustment", vadjustment,
543 return scrolled_window;
547 * gtk_scrolled_window_set_hadjustment:
548 * @scrolled_window: a #GtkScrolledWindow
549 * @hadjustment: horizontal scroll adjustment
551 * Sets the #GtkAdjustment for the horizontal scrollbar.
554 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
555 GtkAdjustment *hadjustment)
557 GtkScrolledWindowPrivate *priv;
561 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
563 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
565 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
567 bin = GTK_BIN (scrolled_window);
568 priv = scrolled_window->priv;
570 if (!priv->hscrollbar)
572 gtk_widget_push_composite_child ();
573 priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
574 gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
575 gtk_widget_pop_composite_child ();
577 gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
578 g_object_ref (priv->hscrollbar);
579 gtk_widget_show (priv->hscrollbar);
583 GtkAdjustment *old_adjustment;
585 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
586 if (old_adjustment == hadjustment)
589 g_signal_handlers_disconnect_by_func (old_adjustment,
590 gtk_scrolled_window_adjustment_changed,
592 gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
595 hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
596 g_signal_connect (hadjustment,
598 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
600 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
602 child = gtk_bin_get_child (bin);
603 if (GTK_IS_SCROLLABLE (child))
604 gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
606 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
610 * gtk_scrolled_window_set_vadjustment:
611 * @scrolled_window: a #GtkScrolledWindow
612 * @vadjustment: vertical scroll adjustment
614 * Sets the #GtkAdjustment for the vertical scrollbar.
617 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
618 GtkAdjustment *vadjustment)
620 GtkScrolledWindowPrivate *priv;
624 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
626 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
628 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
630 bin = GTK_BIN (scrolled_window);
631 priv = scrolled_window->priv;
633 if (!priv->vscrollbar)
635 gtk_widget_push_composite_child ();
636 priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
637 gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
638 gtk_widget_pop_composite_child ();
640 gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
641 g_object_ref (priv->vscrollbar);
642 gtk_widget_show (priv->vscrollbar);
646 GtkAdjustment *old_adjustment;
648 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
649 if (old_adjustment == vadjustment)
652 g_signal_handlers_disconnect_by_func (old_adjustment,
653 gtk_scrolled_window_adjustment_changed,
655 gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
658 vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
659 g_signal_connect (vadjustment,
661 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
663 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
665 child = gtk_bin_get_child (bin);
666 if (GTK_IS_SCROLLABLE (child))
667 gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
669 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
673 * gtk_scrolled_window_get_hadjustment:
674 * @scrolled_window: a #GtkScrolledWindow
676 * Returns the horizontal scrollbar's adjustment, used to connect the
677 * horizontal scrollbar to the child widget's horizontal scroll
680 * Returns: (transfer none): the horizontal #GtkAdjustment
683 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
685 GtkScrolledWindowPrivate *priv;
687 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
689 priv = scrolled_window->priv;
691 return (priv->hscrollbar ?
692 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
697 * gtk_scrolled_window_get_vadjustment:
698 * @scrolled_window: a #GtkScrolledWindow
700 * Returns the vertical scrollbar's adjustment, used to connect the
701 * vertical scrollbar to the child widget's vertical scroll functionality.
703 * Returns: (transfer none): the vertical #GtkAdjustment
706 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
708 GtkScrolledWindowPrivate *priv;
710 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
712 priv = scrolled_window->priv;
714 return (priv->vscrollbar ?
715 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
720 * gtk_scrolled_window_get_hscrollbar:
721 * @scrolled_window: a #GtkScrolledWindow
723 * Returns the horizontal scrollbar of @scrolled_window.
725 * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
726 * or %NULL if it does not have one.
731 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
733 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
735 return scrolled_window->priv->hscrollbar;
739 * gtk_scrolled_window_get_vscrollbar:
740 * @scrolled_window: a #GtkScrolledWindow
742 * Returns the vertical scrollbar of @scrolled_window.
744 * Returns: (transfer none): the vertical scrollbar of the scrolled window,
745 * or %NULL if it does not have one.
750 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
752 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
754 return scrolled_window->priv->vscrollbar;
758 * gtk_scrolled_window_set_policy:
759 * @scrolled_window: a #GtkScrolledWindow
760 * @hscrollbar_policy: policy for horizontal bar
761 * @vscrollbar_policy: policy for vertical bar
763 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
765 * The policy determines when the scrollbar should appear; it is a value
766 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
767 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
768 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
769 * if needed (that is, if the slider part of the bar would be smaller
770 * than the trough - the display is larger than the page size).
773 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
774 GtkPolicyType hscrollbar_policy,
775 GtkPolicyType vscrollbar_policy)
777 GtkScrolledWindowPrivate *priv;
778 GObject *object = G_OBJECT (scrolled_window);
780 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
782 priv = scrolled_window->priv;
784 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
785 (priv->vscrollbar_policy != vscrollbar_policy))
787 priv->hscrollbar_policy = hscrollbar_policy;
788 priv->vscrollbar_policy = vscrollbar_policy;
790 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
792 g_object_freeze_notify (object);
793 g_object_notify (object, "hscrollbar-policy");
794 g_object_notify (object, "vscrollbar-policy");
795 g_object_thaw_notify (object);
800 * gtk_scrolled_window_get_policy:
801 * @scrolled_window: a #GtkScrolledWindow
802 * @hscrollbar_policy: (out) (allow-none): location to store the policy
803 * for the horizontal scrollbar, or %NULL.
804 * @vscrollbar_policy: (out) (allow-none): location to store the policy
805 * for the vertical scrollbar, or %NULL.
807 * Retrieves the current policy values for the horizontal and vertical
808 * scrollbars. See gtk_scrolled_window_set_policy().
811 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
812 GtkPolicyType *hscrollbar_policy,
813 GtkPolicyType *vscrollbar_policy)
815 GtkScrolledWindowPrivate *priv;
817 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
819 priv = scrolled_window->priv;
821 if (hscrollbar_policy)
822 *hscrollbar_policy = priv->hscrollbar_policy;
823 if (vscrollbar_policy)
824 *vscrollbar_policy = priv->vscrollbar_policy;
828 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
830 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
831 GtkSettings *settings;
833 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
835 if (priv->window_placement_set || settings == NULL)
836 priv->real_window_placement = priv->window_placement;
838 g_object_get (settings,
839 "gtk-scrolled-window-placement",
840 &priv->real_window_placement,
845 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
846 GtkCornerType window_placement)
848 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
850 if (priv->window_placement != window_placement)
852 priv->window_placement = window_placement;
854 gtk_scrolled_window_update_real_placement (scrolled_window);
855 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
857 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
862 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
863 gboolean placement_set,
864 gboolean emit_resize)
866 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
868 if (priv->window_placement_set != placement_set)
870 priv->window_placement_set = placement_set;
872 gtk_scrolled_window_update_real_placement (scrolled_window);
874 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
876 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
881 * gtk_scrolled_window_set_placement:
882 * @scrolled_window: a #GtkScrolledWindow
883 * @window_placement: position of the child window
885 * Sets the placement of the contents with respect to the scrollbars
886 * for the scrolled window.
888 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
889 * in the top left, with the scrollbars underneath and to the right.
890 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
891 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
893 * See also gtk_scrolled_window_get_placement() and
894 * gtk_scrolled_window_unset_placement().
897 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
898 GtkCornerType window_placement)
900 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
902 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
903 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
907 * gtk_scrolled_window_get_placement:
908 * @scrolled_window: a #GtkScrolledWindow
910 * Gets the placement of the contents with respect to the scrollbars
911 * for the scrolled window. See gtk_scrolled_window_set_placement().
913 * Return value: the current placement value.
915 * See also gtk_scrolled_window_set_placement() and
916 * gtk_scrolled_window_unset_placement().
919 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
921 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
923 return scrolled_window->priv->window_placement;
927 * gtk_scrolled_window_unset_placement:
928 * @scrolled_window: a #GtkScrolledWindow
930 * Unsets the placement of the contents with respect to the scrollbars
931 * for the scrolled window. If no window placement is set for a scrolled
932 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
934 * See also gtk_scrolled_window_set_placement() and
935 * gtk_scrolled_window_get_placement().
940 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
942 GtkScrolledWindowPrivate *priv;
944 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
946 priv = scrolled_window->priv;
948 if (priv->window_placement_set)
950 priv->window_placement_set = FALSE;
952 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
954 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
959 * gtk_scrolled_window_set_shadow_type:
960 * @scrolled_window: a #GtkScrolledWindow
961 * @type: kind of shadow to draw around scrolled window contents
963 * Changes the type of shadow drawn around the contents of
968 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
971 GtkScrolledWindowPrivate *priv;
973 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
974 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
976 priv = scrolled_window->priv;
978 if (priv->shadow_type != type)
980 priv->shadow_type = type;
982 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
983 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
985 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
987 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
992 * gtk_scrolled_window_get_shadow_type:
993 * @scrolled_window: a #GtkScrolledWindow
995 * Gets the shadow type of the scrolled window. See
996 * gtk_scrolled_window_set_shadow_type().
998 * Return value: the current shadow type
1001 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
1003 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
1005 return scrolled_window->priv->shadow_type;
1009 gtk_scrolled_window_destroy (GtkWidget *widget)
1011 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1012 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1014 if (priv->hscrollbar)
1016 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1017 gtk_scrolled_window_adjustment_changed,
1019 gtk_widget_unparent (priv->hscrollbar);
1020 gtk_widget_destroy (priv->hscrollbar);
1021 g_object_unref (priv->hscrollbar);
1022 priv->hscrollbar = NULL;
1024 if (priv->vscrollbar)
1026 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1027 gtk_scrolled_window_adjustment_changed,
1029 gtk_widget_unparent (priv->vscrollbar);
1030 gtk_widget_destroy (priv->vscrollbar);
1031 g_object_unref (priv->vscrollbar);
1032 priv->vscrollbar = NULL;
1035 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1039 gtk_scrolled_window_set_property (GObject *object,
1041 const GValue *value,
1044 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1045 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1049 case PROP_HADJUSTMENT:
1050 gtk_scrolled_window_set_hadjustment (scrolled_window,
1051 g_value_get_object (value));
1053 case PROP_VADJUSTMENT:
1054 gtk_scrolled_window_set_vadjustment (scrolled_window,
1055 g_value_get_object (value));
1057 case PROP_HSCROLLBAR_POLICY:
1058 gtk_scrolled_window_set_policy (scrolled_window,
1059 g_value_get_enum (value),
1060 priv->vscrollbar_policy);
1062 case PROP_VSCROLLBAR_POLICY:
1063 gtk_scrolled_window_set_policy (scrolled_window,
1064 priv->hscrollbar_policy,
1065 g_value_get_enum (value));
1067 case PROP_WINDOW_PLACEMENT:
1068 gtk_scrolled_window_set_placement_internal (scrolled_window,
1069 g_value_get_enum (value));
1071 case PROP_WINDOW_PLACEMENT_SET:
1072 gtk_scrolled_window_set_placement_set (scrolled_window,
1073 g_value_get_boolean (value),
1076 case PROP_SHADOW_TYPE:
1077 gtk_scrolled_window_set_shadow_type (scrolled_window,
1078 g_value_get_enum (value));
1080 case PROP_MIN_CONTENT_WIDTH:
1081 gtk_scrolled_window_set_min_content_width (scrolled_window,
1082 g_value_get_int (value));
1084 case PROP_MIN_CONTENT_HEIGHT:
1085 gtk_scrolled_window_set_min_content_height (scrolled_window,
1086 g_value_get_int (value));
1089 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1095 gtk_scrolled_window_get_property (GObject *object,
1100 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1101 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1105 case PROP_HADJUSTMENT:
1106 g_value_set_object (value,
1107 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1109 case PROP_VADJUSTMENT:
1110 g_value_set_object (value,
1111 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1113 case PROP_WINDOW_PLACEMENT:
1114 g_value_set_enum (value, priv->window_placement);
1116 case PROP_WINDOW_PLACEMENT_SET:
1117 g_value_set_boolean (value, priv->window_placement_set);
1119 case PROP_SHADOW_TYPE:
1120 g_value_set_enum (value, priv->shadow_type);
1122 case PROP_HSCROLLBAR_POLICY:
1123 g_value_set_enum (value, priv->hscrollbar_policy);
1125 case PROP_VSCROLLBAR_POLICY:
1126 g_value_set_enum (value, priv->vscrollbar_policy);
1128 case PROP_MIN_CONTENT_WIDTH:
1129 g_value_set_int (value, priv->min_content_width);
1131 case PROP_MIN_CONTENT_HEIGHT:
1132 g_value_set_int (value, priv->min_content_height);
1135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1141 traverse_container (GtkWidget *widget,
1144 if (GTK_IS_SCROLLED_WINDOW (widget))
1146 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1147 gtk_widget_queue_resize (widget);
1149 else if (GTK_IS_CONTAINER (widget))
1150 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1154 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1158 list = gtk_window_list_toplevels ();
1160 for (l = list; l; l = l->next)
1161 gtk_container_forall (GTK_CONTAINER (l->data),
1162 traverse_container, NULL);
1168 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1169 GdkScreen *previous_screen)
1171 GtkSettings *settings;
1172 guint window_placement_connection;
1174 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1176 if (!gtk_widget_has_screen (widget))
1179 settings = gtk_widget_get_settings (widget);
1181 window_placement_connection =
1182 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1183 "gtk-scrolled-window-connection"));
1185 if (window_placement_connection)
1188 window_placement_connection =
1189 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1190 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1191 g_object_set_data (G_OBJECT (settings),
1192 I_("gtk-scrolled-window-connection"),
1193 GUINT_TO_POINTER (window_placement_connection));
1197 gtk_scrolled_window_draw (GtkWidget *widget,
1200 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1201 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1203 if (priv->shadow_type != GTK_SHADOW_NONE)
1205 GtkAllocation relative_allocation;
1206 GtkStyleContext *context;
1207 gboolean scrollbars_within_bevel;
1209 context = gtk_widget_get_style_context (widget);
1211 gtk_style_context_save (context);
1212 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1214 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1216 if (!scrollbars_within_bevel)
1218 GtkStateFlags state;
1219 GtkBorder padding, border;
1221 state = gtk_widget_get_state_flags (widget);
1222 gtk_style_context_get_padding (context, state, &padding);
1223 gtk_style_context_get_border (context, state, &border);
1225 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1227 relative_allocation.x -= padding.left + border.left;
1228 relative_allocation.y -= padding.top + border.top;
1229 relative_allocation.width += padding.left + padding.right + border.left + border.right;
1230 relative_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1234 relative_allocation.x = 0;
1235 relative_allocation.y = 0;
1236 relative_allocation.width = gtk_widget_get_allocated_width (widget);
1237 relative_allocation.height = gtk_widget_get_allocated_height (widget);
1240 gtk_render_frame (context, cr,
1241 relative_allocation.x,
1242 relative_allocation.y,
1243 relative_allocation.width,
1244 relative_allocation.height);
1246 gtk_style_context_restore (context);
1249 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1255 gtk_scrolled_window_forall (GtkContainer *container,
1256 gboolean include_internals,
1257 GtkCallback callback,
1258 gpointer callback_data)
1260 GtkScrolledWindowPrivate *priv;
1261 GtkScrolledWindow *scrolled_window;
1263 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1264 g_return_if_fail (callback != NULL);
1266 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1270 if (include_internals)
1272 scrolled_window = GTK_SCROLLED_WINDOW (container);
1273 priv = scrolled_window->priv;
1275 if (priv->vscrollbar)
1276 callback (priv->vscrollbar, callback_data);
1277 if (priv->hscrollbar)
1278 callback (priv->hscrollbar, callback_data);
1283 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1284 GtkScrollType scroll,
1285 gboolean horizontal)
1287 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1288 GtkAdjustment *adjustment = NULL;
1292 case GTK_SCROLL_STEP_UP:
1293 scroll = GTK_SCROLL_STEP_BACKWARD;
1296 case GTK_SCROLL_STEP_DOWN:
1297 scroll = GTK_SCROLL_STEP_FORWARD;
1300 case GTK_SCROLL_STEP_LEFT:
1301 scroll = GTK_SCROLL_STEP_BACKWARD;
1304 case GTK_SCROLL_STEP_RIGHT:
1305 scroll = GTK_SCROLL_STEP_FORWARD;
1308 case GTK_SCROLL_PAGE_UP:
1309 scroll = GTK_SCROLL_PAGE_BACKWARD;
1312 case GTK_SCROLL_PAGE_DOWN:
1313 scroll = GTK_SCROLL_PAGE_FORWARD;
1316 case GTK_SCROLL_PAGE_LEFT:
1317 scroll = GTK_SCROLL_STEP_BACKWARD;
1320 case GTK_SCROLL_PAGE_RIGHT:
1321 scroll = GTK_SCROLL_STEP_FORWARD;
1324 case GTK_SCROLL_STEP_BACKWARD:
1325 case GTK_SCROLL_STEP_FORWARD:
1326 case GTK_SCROLL_PAGE_BACKWARD:
1327 case GTK_SCROLL_PAGE_FORWARD:
1328 case GTK_SCROLL_START:
1329 case GTK_SCROLL_END:
1332 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1336 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1337 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1342 if (priv->hscrollbar)
1343 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1347 if (priv->vscrollbar)
1348 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1353 gdouble value = gtk_adjustment_get_value (adjustment);
1357 case GTK_SCROLL_STEP_FORWARD:
1358 value += gtk_adjustment_get_step_increment (adjustment);
1360 case GTK_SCROLL_STEP_BACKWARD:
1361 value -= gtk_adjustment_get_step_increment (adjustment);
1363 case GTK_SCROLL_PAGE_FORWARD:
1364 value += gtk_adjustment_get_page_increment (adjustment);
1366 case GTK_SCROLL_PAGE_BACKWARD:
1367 value -= gtk_adjustment_get_page_increment (adjustment);
1369 case GTK_SCROLL_START:
1370 value = gtk_adjustment_get_lower (adjustment);
1372 case GTK_SCROLL_END:
1373 value = gtk_adjustment_get_upper (adjustment);
1376 g_assert_not_reached ();
1380 gtk_adjustment_set_value (adjustment, value);
1389 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1390 GtkDirectionType direction_type)
1392 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1393 GtkWidget *toplevel;
1395 /* Focus out of the scrolled window entirely. We do this by setting
1396 * a flag, then propagating the focus motion to the notebook.
1398 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1399 if (!gtk_widget_is_toplevel (toplevel))
1402 g_object_ref (scrolled_window);
1404 priv->focus_out = TRUE;
1405 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1406 priv->focus_out = FALSE;
1408 g_object_unref (scrolled_window);
1412 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1413 GtkAllocation *allocation)
1415 GtkAllocation widget_allocation;
1416 GtkScrolledWindow *scrolled_window;
1417 GtkScrolledWindowPrivate *priv;
1422 g_return_if_fail (widget != NULL);
1423 g_return_if_fail (allocation != NULL);
1425 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1426 priv = scrolled_window->priv;
1428 /* Get possible scrollbar dimensions */
1429 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1430 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1431 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1433 /* Subtract some things from our available allocation size */
1437 if (priv->shadow_type != GTK_SHADOW_NONE)
1439 GtkStyleContext *context;
1440 GtkStateFlags state;
1441 GtkBorder padding, border;
1443 context = gtk_widget_get_style_context (widget);
1444 state = gtk_widget_get_state_flags (widget);
1446 gtk_style_context_save (context);
1447 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1449 gtk_style_context_get_border (context, state, &border);
1450 gtk_style_context_get_padding (context, state, &padding);
1452 allocation->x += padding.left + border.left;
1453 allocation->y += padding.top + border.top;
1455 gtk_style_context_restore (context);
1458 gtk_widget_get_allocation (widget, &widget_allocation);
1459 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1460 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1462 if (priv->vscrollbar_visible)
1466 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1469 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1470 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1472 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1473 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1474 allocation->x += (sb_width + sb_spacing);
1476 allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1478 if (priv->hscrollbar_visible)
1481 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1482 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1483 allocation->y += (sb_height + sb_spacing);
1485 allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1490 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1491 GtkAllocation *relative_allocation)
1493 GtkWidget *widget = GTK_WIDGET (swindow), *child;
1494 GtkAllocation allocation;
1495 GtkAllocation child_allocation;
1497 child = gtk_bin_get_child (GTK_BIN (widget));
1499 gtk_widget_get_allocation (widget, &allocation);
1501 gtk_scrolled_window_relative_allocation (widget, relative_allocation);
1502 child_allocation.x = relative_allocation->x + allocation.x;
1503 child_allocation.y = relative_allocation->y + allocation.y;
1504 child_allocation.width = relative_allocation->width;
1505 child_allocation.height = relative_allocation->height;
1507 gtk_widget_size_allocate (child, &child_allocation);
1511 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1512 GtkAllocation *allocation)
1514 GtkScrolledWindow *scrolled_window;
1515 GtkScrolledWindowPrivate *priv;
1516 GtkStyleContext *context;
1517 GtkStateFlags state;
1518 GtkBorder padding, border;
1520 GtkAllocation relative_allocation;
1521 GtkAllocation child_allocation;
1523 gboolean scrollbars_within_bevel;
1528 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1529 g_return_if_fail (allocation != NULL);
1531 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1532 bin = GTK_BIN (scrolled_window);
1533 priv = scrolled_window->priv;
1535 /* Get possible scrollbar dimensions */
1536 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1537 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1538 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1540 context = gtk_widget_get_style_context (widget);
1541 state = gtk_widget_get_state_flags (widget);
1543 gtk_style_context_save (context);
1544 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1546 gtk_style_context_get_padding (context, state, &padding);
1547 gtk_style_context_get_border (context, state, &border);
1549 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1551 gtk_widget_set_allocation (widget, allocation);
1553 gtk_style_context_restore (context);
1555 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1556 priv->hscrollbar_visible = TRUE;
1557 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1558 priv->hscrollbar_visible = FALSE;
1559 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1560 priv->vscrollbar_visible = TRUE;
1561 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1562 priv->vscrollbar_visible = FALSE;
1564 child = gtk_bin_get_child (bin);
1565 if (child && gtk_widget_get_visible (child))
1567 gint child_scroll_width;
1568 gint child_scroll_height;
1569 gboolean previous_hvis;
1570 gboolean previous_vvis;
1573 /* Determine scrollbar visibility first via hfw apis */
1574 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1576 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1577 gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
1579 gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
1581 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1583 /* First try without a vertical scrollbar if the content will fit the height
1584 * given the extra width of the scrollbar */
1585 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1586 gtk_widget_get_preferred_height_for_width (child,
1587 MAX (allocation->width, child_scroll_width),
1588 &child_scroll_height, NULL);
1590 gtk_widget_get_preferred_height_for_width (child,
1591 MAX (allocation->width, child_scroll_width),
1592 NULL, &child_scroll_height);
1594 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1596 /* Does the content height fit the allocation height ? */
1597 priv->vscrollbar_visible = child_scroll_height > allocation->height;
1599 /* Does the content width fit the allocation with minus a possible scrollbar ? */
1600 priv->hscrollbar_visible =
1601 child_scroll_width > allocation->width -
1602 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1604 /* Now that we've guessed the hscrollbar, does the content height fit
1605 * the possible new allocation height ? */
1606 priv->vscrollbar_visible =
1607 child_scroll_height > allocation->height -
1608 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1610 /* Now that we've guessed the vscrollbar, does the content width fit
1611 * the possible new allocation width ? */
1612 priv->hscrollbar_visible =
1613 child_scroll_width > allocation->width -
1614 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1616 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1618 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1619 priv->vscrollbar_visible = child_scroll_height > allocation->height -
1620 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1623 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1625 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1627 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1628 priv->hscrollbar_visible =
1629 child_scroll_width > allocation->width -
1630 (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1632 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1635 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1637 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1638 gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1640 gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1642 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1644 /* First try without a horizontal scrollbar if the content will fit the width
1645 * given the extra height of the scrollbar */
1646 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1647 gtk_widget_get_preferred_width_for_height (child,
1648 MAX (allocation->height, child_scroll_height),
1649 &child_scroll_width, NULL);
1651 gtk_widget_get_preferred_width_for_height (child,
1652 MAX (allocation->height, child_scroll_height),
1653 NULL, &child_scroll_width);
1655 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1657 /* Does the content width fit the allocation width ? */
1658 priv->hscrollbar_visible = child_scroll_width > allocation->width;
1660 /* Does the content height fit the allocation with minus a possible scrollbar ? */
1661 priv->vscrollbar_visible =
1662 child_scroll_height > allocation->height -
1663 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1665 /* Now that we've guessed the vscrollbar, does the content width fit
1666 * the possible new allocation width ? */
1667 priv->hscrollbar_visible =
1668 child_scroll_width > allocation->width -
1669 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1671 /* Now that we've guessed the hscrollbar, does the content height fit
1672 * the possible new allocation height ? */
1673 priv->vscrollbar_visible =
1674 child_scroll_height > allocation->height -
1675 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1677 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1679 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1680 priv->hscrollbar_visible = child_scroll_width > allocation->width -
1681 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1684 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1686 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1688 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1689 priv->vscrollbar_visible =
1690 child_scroll_height > allocation->height -
1691 (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1693 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1697 /* Now after guessing scrollbar visibility; fall back on the allocation loop which
1698 * observes the adjustments to detect scrollbar visibility and also avoids
1699 * infinite recursion
1703 previous_hvis = priv->hscrollbar_visible;
1704 previous_vvis = priv->vscrollbar_visible;
1705 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1707 /* Explicitly force scrollbar visibility checks.
1709 * Since we make a guess above, the child might not decide to update the adjustments
1710 * if they logically did not change since the last configuration
1712 if (priv->hscrollbar)
1713 gtk_scrolled_window_adjustment_changed
1714 (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)), scrolled_window);
1716 if (priv->vscrollbar)
1717 gtk_scrolled_window_adjustment_changed
1718 (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)), scrolled_window);
1720 /* If, after the first iteration, the hscrollbar and the
1721 * vscrollbar flip visiblity... or if one of the scrollbars flip
1722 * on each itteration indefinitly/infinitely, then we just need both
1726 previous_hvis != priv->hscrollbar_visible &&
1727 previous_vvis != priv->vscrollbar_visible) || count > 3)
1729 priv->hscrollbar_visible = TRUE;
1730 priv->vscrollbar_visible = TRUE;
1732 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1739 while (previous_hvis != priv->hscrollbar_visible ||
1740 previous_vvis != priv->vscrollbar_visible);
1744 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1745 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1746 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1749 if (priv->hscrollbar_visible)
1751 if (!gtk_widget_get_visible (priv->hscrollbar))
1752 gtk_widget_show (priv->hscrollbar);
1754 child_allocation.x = relative_allocation.x;
1755 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1756 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1757 child_allocation.y = (relative_allocation.y +
1758 relative_allocation.height +
1760 (priv->shadow_type == GTK_SHADOW_NONE ?
1761 0 : padding.top + border.top));
1763 child_allocation.y = 0;
1765 child_allocation.width = relative_allocation.width;
1766 child_allocation.height = sb_height;
1767 child_allocation.x += allocation->x;
1768 child_allocation.y += allocation->y;
1770 if (priv->shadow_type != GTK_SHADOW_NONE)
1772 if (!scrollbars_within_bevel)
1774 child_allocation.x -= padding.left + border.left;
1775 child_allocation.width += padding.left + padding.right + border.left + border.right;
1777 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1778 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1780 child_allocation.y -= padding.top + border.top;
1784 child_allocation.y += padding.top + border.top;
1788 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1790 else if (gtk_widget_get_visible (priv->hscrollbar))
1791 gtk_widget_hide (priv->hscrollbar);
1793 if (priv->vscrollbar_visible)
1795 if (!gtk_widget_get_visible (priv->vscrollbar))
1796 gtk_widget_show (priv->vscrollbar);
1798 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1799 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1800 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1801 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1802 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1803 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1804 child_allocation.x = (relative_allocation.x +
1805 relative_allocation.width +
1807 (priv->shadow_type == GTK_SHADOW_NONE ?
1808 0 : padding.left + border.left));
1810 child_allocation.x = 0;
1812 child_allocation.y = relative_allocation.y;
1813 child_allocation.width = sb_width;
1814 child_allocation.height = relative_allocation.height;
1815 child_allocation.x += allocation->x;
1816 child_allocation.y += allocation->y;
1818 if (priv->shadow_type != GTK_SHADOW_NONE)
1820 if (!scrollbars_within_bevel)
1822 child_allocation.y -= padding.top + border.top;
1823 child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1825 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1826 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1828 child_allocation.x -= padding.left + border.left;
1832 child_allocation.x += padding.left + border.left;
1836 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1838 else if (gtk_widget_get_visible (priv->vscrollbar))
1839 gtk_widget_hide (priv->vscrollbar);
1843 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1844 GdkEventScroll *event)
1846 GtkScrolledWindowPrivate *priv;
1847 GtkScrolledWindow *scrolled_window;
1850 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1851 g_return_val_if_fail (event != NULL, FALSE);
1853 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1854 priv = scrolled_window->priv;
1856 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1857 range = priv->vscrollbar;
1859 range = priv->hscrollbar;
1861 if (range && gtk_widget_get_visible (range))
1863 GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (range));
1866 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1868 gtk_adjustment_set_value (adjustment, gtk_adjustment_get_value (adjustment) + delta);
1877 gtk_scrolled_window_focus (GtkWidget *widget,
1878 GtkDirectionType direction)
1880 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1881 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1883 gboolean had_focus_child;
1885 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1887 if (priv->focus_out)
1889 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1893 if (gtk_widget_is_focus (widget))
1896 /* We only put the scrolled window itself in the focus chain if it
1897 * isn't possible to focus any children.
1899 child = gtk_bin_get_child (GTK_BIN (widget));
1902 if (gtk_widget_child_focus (child, direction))
1906 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1908 gtk_widget_grab_focus (widget);
1916 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1919 GtkScrolledWindowPrivate *priv;
1920 GtkScrolledWindow *scrolled_window;
1922 g_return_if_fail (adjustment != NULL);
1923 g_return_if_fail (data != NULL);
1925 scrolled_window = GTK_SCROLLED_WINDOW (data);
1926 priv = scrolled_window->priv;
1928 if (priv->hscrollbar &&
1929 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1931 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1935 visible = priv->hscrollbar_visible;
1936 priv->hscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1937 gtk_adjustment_get_page_size (adjustment));
1939 if (priv->hscrollbar_visible != visible)
1940 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1943 else if (priv->vscrollbar &&
1944 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1946 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1950 visible = priv->vscrollbar_visible;
1951 priv->vscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1952 gtk_adjustment_get_page_size (adjustment));
1954 if (priv->vscrollbar_visible != visible)
1955 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1961 gtk_scrolled_window_add (GtkContainer *container,
1964 GtkScrolledWindowPrivate *priv;
1965 GtkScrolledWindow *scrolled_window;
1967 GtkWidget *child_widget;
1968 GtkAdjustment *hadj, *vadj;
1970 bin = GTK_BIN (container);
1971 child_widget = gtk_bin_get_child (bin);
1972 g_return_if_fail (child_widget == NULL);
1974 scrolled_window = GTK_SCROLLED_WINDOW (container);
1975 priv = scrolled_window->priv;
1977 _gtk_bin_set_child (bin, child);
1978 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1980 hadj = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1981 vadj = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1983 if (GTK_IS_SCROLLABLE (child))
1984 g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1986 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1987 "use gtk_scrolled_window_add_with_viewport() instead");
1991 gtk_scrolled_window_remove (GtkContainer *container,
1994 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1995 g_return_if_fail (child != NULL);
1996 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1998 g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
2000 /* chain parent class handler to remove child */
2001 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
2005 * gtk_scrolled_window_add_with_viewport:
2006 * @scrolled_window: a #GtkScrolledWindow
2007 * @child: the widget you want to scroll
2009 * Used to add children without native scrolling capabilities. This
2010 * is simply a convenience function; it is equivalent to adding the
2011 * unscrollable child to a viewport, then adding the viewport to the
2012 * scrolled window. If a child has native scrolling, use
2013 * gtk_container_add() instead of this function.
2015 * The viewport scrolls the child by moving its #GdkWindow, and takes
2016 * the size of the child to be the size of its toplevel #GdkWindow.
2017 * This will be very wrong for most widgets that support native scrolling;
2018 * for example, if you add a widget such as #GtkTreeView with a viewport,
2019 * the whole widget will scroll, including the column headings. Thus,
2020 * widgets with native scrolling support should not be used with the
2021 * #GtkViewport proxy.
2023 * A widget supports scrolling natively if it implements the
2024 * #GtkScrollable interface.
2027 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
2031 GtkWidget *viewport;
2032 GtkWidget *child_widget;
2034 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2035 g_return_if_fail (GTK_IS_WIDGET (child));
2036 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
2038 bin = GTK_BIN (scrolled_window);
2039 child_widget = gtk_bin_get_child (bin);
2043 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2044 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2046 viewport = child_widget;
2051 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2052 gtk_scrolled_window_get_vadjustment (scrolled_window));
2053 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2056 gtk_widget_show (viewport);
2057 gtk_container_add (GTK_CONTAINER (viewport), child);
2061 * _gtk_scrolled_window_get_spacing:
2062 * @scrolled_window: a scrolled window
2064 * Gets the spacing between the scrolled window's scrollbars and
2065 * the scrolled widget. Used by GtkCombo
2067 * Return value: the spacing, in pixels.
2070 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2072 GtkScrolledWindowClass *class;
2074 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2076 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2078 if (class->scrollbar_spacing >= 0)
2079 return class->scrollbar_spacing;
2082 gint scrollbar_spacing;
2084 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2085 "scrollbar-spacing", &scrollbar_spacing,
2088 return scrollbar_spacing;
2094 gtk_scrolled_window_get_preferred_size (GtkWidget *widget,
2095 GtkOrientation orientation,
2099 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2100 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2101 GtkBin *bin = GTK_BIN (scrolled_window);
2104 gint scrollbar_spacing;
2105 GtkRequisition hscrollbar_requisition;
2106 GtkRequisition vscrollbar_requisition;
2107 GtkRequisition minimum_req, natural_req;
2109 gint min_child_size, nat_child_size;
2111 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2115 minimum_req.width = 0;
2116 minimum_req.height = 0;
2117 natural_req.width = 0;
2118 natural_req.height = 0;
2120 gtk_widget_get_preferred_size (priv->hscrollbar,
2121 &hscrollbar_requisition, NULL);
2122 gtk_widget_get_preferred_size (priv->vscrollbar,
2123 &vscrollbar_requisition, NULL);
2125 child = gtk_bin_get_child (bin);
2126 if (child && gtk_widget_get_visible (child))
2128 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2130 gtk_widget_get_preferred_width (child,
2134 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2136 minimum_req.width += min_child_size;
2137 natural_req.width += nat_child_size;
2141 gint min_content_width = priv->min_content_width;
2143 if (min_content_width >= 0)
2145 minimum_req.width = MAX (minimum_req.width, min_content_width);
2146 natural_req.width = MAX (natural_req.width, min_content_width);
2151 minimum_req.width += vscrollbar_requisition.width;
2152 natural_req.width += vscrollbar_requisition.width;
2156 else /* GTK_ORIENTATION_VERTICAL */
2158 gtk_widget_get_preferred_height (child,
2162 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2164 minimum_req.height += min_child_size;
2165 natural_req.height += nat_child_size;
2169 gint min_content_height = priv->min_content_height;
2171 if (min_content_height >= 0)
2173 minimum_req.height = MAX (minimum_req.height, min_content_height);
2174 natural_req.height = MAX (natural_req.height, min_content_height);
2179 minimum_req.height += vscrollbar_requisition.height;
2180 natural_req.height += vscrollbar_requisition.height;
2186 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2187 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2189 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2190 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2191 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2192 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2195 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2196 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2198 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2199 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2200 if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2201 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2204 minimum_req.width += MAX (0, extra_width);
2205 minimum_req.height += MAX (0, extra_height);
2206 natural_req.width += MAX (0, extra_width);
2207 natural_req.height += MAX (0, extra_height);
2209 if (priv->shadow_type != GTK_SHADOW_NONE)
2211 GtkStyleContext *context;
2212 GtkStateFlags state;
2213 GtkBorder padding, border;
2215 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
2216 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
2218 gtk_style_context_save (context);
2219 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
2221 gtk_style_context_get_padding (context, state, &padding);
2222 gtk_style_context_get_border (context, state, &border);
2224 minimum_req.width += padding.left + padding.right + border.left + border.right;
2225 minimum_req.height += padding.top + padding.bottom + border.top + border.bottom;
2226 natural_req.width += padding.left + padding.right + border.left + border.right;
2227 natural_req.height += padding.top + padding.bottom + border.top + border.bottom;
2229 gtk_style_context_restore (context);
2232 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2235 *minimum_size = minimum_req.width;
2237 *natural_size = natural_req.width;
2242 *minimum_size = minimum_req.height;
2244 *natural_size = natural_req.height;
2249 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2253 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2257 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2261 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2265 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2267 gint *minimum_height,
2268 gint *natural_height)
2270 g_return_if_fail (GTK_IS_WIDGET (widget));
2272 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2276 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2278 gint *minimum_width,
2279 gint *natural_width)
2281 g_return_if_fail (GTK_IS_WIDGET (widget));
2283 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2287 * gtk_scrolled_window_get_min_content_width:
2288 * @scrolled_window: a #GtkScrolledWindow
2290 * Gets the minimum content width of @scrolled_window, or -1 if not set.
2292 * Returns: the minimum content width
2297 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2299 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2301 return scrolled_window->priv->min_content_width;
2305 * gtk_scrolled_window_set_min_content_width:
2306 * @scrolled_window: a #GtkScrolledWindow
2307 * @width: the minimal content width
2309 * Sets the minimum width that @scrolled_window should keep visible.
2310 * Note that this can and (usually will) be smaller than the minimum
2311 * size of the content.
2316 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2319 GtkScrolledWindowPrivate *priv;
2321 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2323 priv = scrolled_window->priv;
2325 if (priv->min_content_width != width)
2327 priv->min_content_width = width;
2329 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2331 g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2336 * gtk_scrolled_window_get_min_content_height:
2337 * @scrolled_window: a #GtkScrolledWindow
2339 * Gets the minimal content height of @scrolled_window, or -1 if not set.
2341 * Returns: the minimal content height
2346 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2348 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2350 return scrolled_window->priv->min_content_height;
2354 * gtk_scrolled_window_set_min_content_height:
2355 * @scrolled_window: a #GtkScrolledWindow
2356 * @height: the minimal content height
2358 * Sets the minimum height that @scrolled_window should keep visible.
2359 * Note that this can and (usually will) be smaller than the minimum
2360 * size of the content.
2365 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2368 GtkScrolledWindowPrivate *priv;
2370 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2372 priv = scrolled_window->priv;
2374 if (priv->min_content_height != height)
2376 priv->min_content_height = height;
2378 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2380 g_object_notify (G_OBJECT (scrolled_window), "min-content-height");