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 "gtkprivate.h"
38 #include "gtktypebuiltins.h"
40 #include "a11y/gtkscrolledwindowaccessible.h"
43 * SECTION:gtkscrolledwindow
44 * @Short_description: Adds scrollbars to its child widget
45 * @Title: GtkScrolledWindow
46 * @See_also: #GtkScrollable, #GtkViewport, #GtkAdjustment
48 * #GtkScrolledWindow is a #GtkBin subclass: it's a container
49 * the accepts a single child widget. #GtkScrolledWindow adds scrollbars
50 * to the child widget and optionally draws a beveled frame around the
53 * The scrolled window can work in two ways. Some widgets have native
54 * scrolling support; these widgets implement the #GtkScrollable interface.
55 * Widgets with native scroll support include #GtkTreeView, #GtkTextView,
58 * For widgets that lack native scrolling support, the #GtkViewport
59 * widget acts as an adaptor class, implementing scrollability for child
60 * widgets that lack their own scrolling capabilities. Use #GtkViewport
61 * to scroll child widgets such as #GtkGrid, #GtkBox, and so on.
63 * If a widget has native scrolling abilities, it can be added to the
64 * #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
65 * must first add the widget to a #GtkViewport, then add the #GtkViewport
66 * to the scrolled window. The convenience function
67 * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
68 * ignore the presence of the viewport.
70 * The position of the scrollbars is controlled by the scroll
71 * adjustments. See #GtkAdjustment for the fields in an adjustment - for
72 * #GtkScrollbar, used by #GtkScrolledWindow, the "value" field
73 * represents the position of the scrollbar, which must be between the
74 * "lower" field and "upper - page_size." The "page_size" field
75 * represents the size of the visible scrollable area. The
76 * "step_increment" and "page_increment" fields are used when the user
77 * asks to step down (using the small stepper arrows) or page down (using
78 * for example the PageDown key).
80 * If a #GtkScrolledWindow doesn't behave quite as you would like, or
81 * doesn't have exactly the right layout, it's very possible to set up
82 * your own scrolling with #GtkScrollbar and for example a #GtkGrid.
86 /* scrolled window policy and size requisition handling:
88 * gtk size requisition works as follows:
89 * a widget upon size-request reports the width and height that it finds
90 * to be best suited to display its contents, including children.
91 * the width and/or height reported from a widget upon size requisition
92 * may be overidden by the user by specifying a width and/or height
93 * other than 0 through gtk_widget_set_size_request().
95 * a scrolled window needs (for implementing all three policy types) to
96 * request its width and height based on two different rationales.
97 * 1) the user wants the scrolled window to just fit into the space
98 * that it gets allocated for a specifc dimension.
99 * 1.1) this does not apply if the user specified a concrete value
100 * value for that specific dimension by either specifying usize for the
101 * scrolled window or for its child.
102 * 2) the user wants the scrolled window to take as much space up as
103 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
105 * also, kinda obvious:
106 * 3) a user would certainly not have choosen a scrolled window as a container
107 * for the child, if the resulting allocation takes up more space than the
108 * child would have allocated without the scrolled window.
111 * A) from 1) follows: the scrolled window shouldn't request more space for a
112 * specifc dimension than is required at minimum.
113 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
114 * window (done automatically) or by usize of the child (needs to be checked).
115 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
117 * D) from 3) follows: the scrolled window child's minimum width and minimum height
118 * under A) at least correspond to the space taken up by its scrollbars.
121 #define DEFAULT_SCROLLBAR_SPACING 3
123 struct _GtkScrolledWindowPrivate
125 GtkWidget *hscrollbar;
126 GtkWidget *vscrollbar;
128 GtkCornerType real_window_placement;
131 guint window_placement_set : 1;
132 guint hscrollbar_policy : 2;
133 guint vscrollbar_policy : 2;
134 guint hscrollbar_visible : 1;
135 guint vscrollbar_visible : 1;
136 guint window_placement : 2;
137 guint focus_out : 1; /* Flag used by ::move-focus-out implementation */
139 gint min_content_width;
140 gint min_content_height;
148 PROP_HSCROLLBAR_POLICY,
149 PROP_VSCROLLBAR_POLICY,
150 PROP_WINDOW_PLACEMENT,
151 PROP_WINDOW_PLACEMENT_SET,
153 PROP_MIN_CONTENT_WIDTH,
154 PROP_MIN_CONTENT_HEIGHT
165 static void gtk_scrolled_window_set_property (GObject *object,
169 static void gtk_scrolled_window_get_property (GObject *object,
174 static void gtk_scrolled_window_destroy (GtkWidget *widget);
175 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
176 GdkScreen *previous_screen);
177 static gboolean gtk_scrolled_window_draw (GtkWidget *widget,
179 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
180 GtkAllocation *allocation);
181 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
182 GdkEventScroll *event);
183 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
184 GtkDirectionType direction);
185 static void gtk_scrolled_window_add (GtkContainer *container,
187 static void gtk_scrolled_window_remove (GtkContainer *container,
189 static void gtk_scrolled_window_forall (GtkContainer *container,
190 gboolean include_internals,
191 GtkCallback callback,
192 gpointer callback_data);
193 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
194 GtkScrollType scroll,
195 gboolean horizontal);
196 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
197 GtkDirectionType direction_type);
199 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
200 GtkAllocation *allocation);
201 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
204 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
206 static void gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
209 static void gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
212 static void gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *layout,
214 gint *minimum_height,
215 gint *natural_height);
216 static void gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *layout,
218 gint *minimum_height,
219 gint *natural_height);
221 static guint signals[LAST_SIGNAL] = {0};
223 G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
227 add_scroll_binding (GtkBindingSet *binding_set,
229 GdkModifierType mask,
230 GtkScrollType scroll,
233 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
235 gtk_binding_entry_add_signal (binding_set, keyval, mask,
237 GTK_TYPE_SCROLL_TYPE, scroll,
238 G_TYPE_BOOLEAN, horizontal);
239 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
241 GTK_TYPE_SCROLL_TYPE, scroll,
242 G_TYPE_BOOLEAN, horizontal);
246 add_tab_bindings (GtkBindingSet *binding_set,
247 GdkModifierType modifiers,
248 GtkDirectionType direction)
250 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
252 GTK_TYPE_DIRECTION_TYPE, direction);
253 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
255 GTK_TYPE_DIRECTION_TYPE, direction);
259 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
261 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
262 GtkWidgetClass *widget_class;
263 GtkContainerClass *container_class;
264 GtkBindingSet *binding_set;
266 widget_class = (GtkWidgetClass*) class;
267 container_class = (GtkContainerClass*) class;
269 gobject_class->set_property = gtk_scrolled_window_set_property;
270 gobject_class->get_property = gtk_scrolled_window_get_property;
272 widget_class->destroy = gtk_scrolled_window_destroy;
273 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
274 widget_class->draw = gtk_scrolled_window_draw;
275 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
276 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
277 widget_class->focus = gtk_scrolled_window_focus;
278 widget_class->get_preferred_width = gtk_scrolled_window_get_preferred_width;
279 widget_class->get_preferred_height = gtk_scrolled_window_get_preferred_height;
280 widget_class->get_preferred_height_for_width = gtk_scrolled_window_get_preferred_height_for_width;
281 widget_class->get_preferred_width_for_height = gtk_scrolled_window_get_preferred_width_for_height;
283 container_class->add = gtk_scrolled_window_add;
284 container_class->remove = gtk_scrolled_window_remove;
285 container_class->forall = gtk_scrolled_window_forall;
286 gtk_container_class_handle_border_width (container_class);
288 class->scrollbar_spacing = -1;
290 class->scroll_child = gtk_scrolled_window_scroll_child;
291 class->move_focus_out = gtk_scrolled_window_move_focus_out;
293 g_object_class_install_property (gobject_class,
295 g_param_spec_object ("hadjustment",
296 P_("Horizontal Adjustment"),
297 P_("The GtkAdjustment for the horizontal position"),
299 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
300 g_object_class_install_property (gobject_class,
302 g_param_spec_object ("vadjustment",
303 P_("Vertical Adjustment"),
304 P_("The GtkAdjustment for the vertical position"),
306 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
307 g_object_class_install_property (gobject_class,
308 PROP_HSCROLLBAR_POLICY,
309 g_param_spec_enum ("hscrollbar-policy",
310 P_("Horizontal Scrollbar Policy"),
311 P_("When the horizontal scrollbar is displayed"),
312 GTK_TYPE_POLICY_TYPE,
313 GTK_POLICY_AUTOMATIC,
314 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
315 g_object_class_install_property (gobject_class,
316 PROP_VSCROLLBAR_POLICY,
317 g_param_spec_enum ("vscrollbar-policy",
318 P_("Vertical Scrollbar Policy"),
319 P_("When the vertical scrollbar is displayed"),
320 GTK_TYPE_POLICY_TYPE,
321 GTK_POLICY_AUTOMATIC,
322 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
324 g_object_class_install_property (gobject_class,
325 PROP_WINDOW_PLACEMENT,
326 g_param_spec_enum ("window-placement",
327 P_("Window Placement"),
328 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
329 GTK_TYPE_CORNER_TYPE,
331 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
334 * GtkScrolledWindow:window-placement-set:
336 * Whether "window-placement" should be used to determine the location
337 * of the contents with respect to the scrollbars. Otherwise, the
338 * "gtk-scrolled-window-placement" setting is used.
342 g_object_class_install_property (gobject_class,
343 PROP_WINDOW_PLACEMENT_SET,
344 g_param_spec_boolean ("window-placement-set",
345 P_("Window Placement Set"),
346 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
348 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
349 g_object_class_install_property (gobject_class,
351 g_param_spec_enum ("shadow-type",
353 P_("Style of bevel around the contents"),
354 GTK_TYPE_SHADOW_TYPE,
356 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
359 * GtkScrolledWindow:scrollbars-within-bevel:
361 * Whether to place scrollbars within the scrolled window's bevel.
365 gtk_widget_class_install_style_property (widget_class,
366 g_param_spec_boolean ("scrollbars-within-bevel",
367 P_("Scrollbars within bevel"),
368 P_("Place scrollbars within the scrolled window's bevel"),
370 GTK_PARAM_READABLE));
372 gtk_widget_class_install_style_property (widget_class,
373 g_param_spec_int ("scrollbar-spacing",
374 P_("Scrollbar spacing"),
375 P_("Number of pixels between the scrollbars and the scrolled window"),
378 DEFAULT_SCROLLBAR_SPACING,
379 GTK_PARAM_READABLE));
382 * GtkScrolledWindow:min-content-width:
384 * The minimum content width of @scrolled_window, or -1 if not set.
388 g_object_class_install_property (gobject_class,
389 PROP_MIN_CONTENT_WIDTH,
390 g_param_spec_int ("min-content-width",
391 P_("Minimum Content Width"),
392 P_("The minimum width that the scrolled window will allocate to its content"),
394 GTK_PARAM_READWRITE));
397 * GtkScrolledWindow:min-content-height:
399 * The minimum content height of @scrolled_window, or -1 if not set.
403 g_object_class_install_property (gobject_class,
404 PROP_MIN_CONTENT_HEIGHT,
405 g_param_spec_int ("min-content-height",
406 P_("Minimum Content Height"),
407 P_("The minimum height that the scrolled window will allocate to its content"),
409 GTK_PARAM_READWRITE));
411 * GtkScrolledWindow::scroll-child:
412 * @scrolled_window: a #GtkScrolledWindow
413 * @scroll: a #GtkScrollType describing how much to scroll
414 * @horizontal: whether the keybinding scrolls the child
415 * horizontally or not
417 * The ::scroll-child signal is a
418 * <link linkend="keybinding-signals">keybinding signal</link>
419 * which gets emitted when a keybinding that scrolls is pressed.
420 * The horizontal or vertical adjustment is updated which triggers a
421 * signal that the scrolled windows child may listen to and scroll itself.
423 signals[SCROLL_CHILD] =
424 g_signal_new (I_("scroll-child"),
425 G_TYPE_FROM_CLASS (gobject_class),
426 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
427 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
429 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
431 GTK_TYPE_SCROLL_TYPE,
435 * GtkScrolledWindow::move-focus-out:
436 * @scrolled_window: a #GtkScrolledWindow
437 * @direction_type: either %GTK_DIR_TAB_FORWARD or
438 * %GTK_DIR_TAB_BACKWARD
440 * The ::move-focus-out signal is a
441 * <link linkend="keybinding-signals">keybinding signal</link>
442 * which gets emitted when focus is moved away from the scrolled
443 * window by a keybinding.
444 * The #GtkWidget::move-focus signal is emitted with @direction_type
445 * on this scrolled windows toplevel parent in the container hierarchy.
446 * The default bindings for this signal are
447 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap></keycombo>
449 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap><keycap>Shift</keycap></keycombo>.
451 signals[MOVE_FOCUS_OUT] =
452 g_signal_new (I_("move-focus-out"),
453 G_TYPE_FROM_CLASS (gobject_class),
454 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
455 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
457 _gtk_marshal_VOID__ENUM,
459 GTK_TYPE_DIRECTION_TYPE);
461 binding_set = gtk_binding_set_by_class (class);
463 add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
464 add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
465 add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
466 add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
468 add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
469 add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
470 add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
471 add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
473 add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
474 add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
475 add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
476 add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
478 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
479 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
481 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
483 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCROLLED_WINDOW_ACCESSIBLE);
487 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
489 GtkScrolledWindowPrivate *priv;
491 scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
492 GTK_TYPE_SCROLLED_WINDOW,
493 GtkScrolledWindowPrivate);
495 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
496 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
498 priv->hscrollbar = NULL;
499 priv->vscrollbar = NULL;
500 priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
501 priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
502 priv->hscrollbar_visible = FALSE;
503 priv->vscrollbar_visible = FALSE;
504 priv->focus_out = FALSE;
505 priv->window_placement = GTK_CORNER_TOP_LEFT;
506 gtk_scrolled_window_update_real_placement (scrolled_window);
507 priv->min_content_width = -1;
508 priv->min_content_height = -1;
512 * gtk_scrolled_window_new:
513 * @hadjustment: (allow-none): horizontal adjustment
514 * @vadjustment: (allow-none): vertical adjustment
516 * Creates a new scrolled window.
518 * The two arguments are the scrolled window's adjustments; these will be
519 * shared with the scrollbars and the child widget to keep the bars in sync
520 * with the child. Usually you want to pass %NULL for the adjustments, which
521 * will cause the scrolled window to create them for you.
523 * Returns: a new scrolled window
526 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
527 GtkAdjustment *vadjustment)
529 GtkWidget *scrolled_window;
532 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
535 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
537 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
538 "hadjustment", hadjustment,
539 "vadjustment", vadjustment,
542 return scrolled_window;
546 * gtk_scrolled_window_set_hadjustment:
547 * @scrolled_window: a #GtkScrolledWindow
548 * @hadjustment: horizontal scroll adjustment
550 * Sets the #GtkAdjustment for the horizontal scrollbar.
553 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
554 GtkAdjustment *hadjustment)
556 GtkScrolledWindowPrivate *priv;
560 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
562 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
564 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
566 bin = GTK_BIN (scrolled_window);
567 priv = scrolled_window->priv;
569 if (!priv->hscrollbar)
571 gtk_widget_push_composite_child ();
572 priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
573 gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
574 gtk_widget_pop_composite_child ();
576 gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
577 g_object_ref (priv->hscrollbar);
578 gtk_widget_show (priv->hscrollbar);
582 GtkAdjustment *old_adjustment;
584 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
585 if (old_adjustment == hadjustment)
588 g_signal_handlers_disconnect_by_func (old_adjustment,
589 gtk_scrolled_window_adjustment_changed,
591 gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
594 hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
595 g_signal_connect (hadjustment,
597 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
599 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
601 child = gtk_bin_get_child (bin);
602 if (GTK_IS_SCROLLABLE (child))
603 gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
605 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
609 * gtk_scrolled_window_set_vadjustment:
610 * @scrolled_window: a #GtkScrolledWindow
611 * @vadjustment: vertical scroll adjustment
613 * Sets the #GtkAdjustment for the vertical scrollbar.
616 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
617 GtkAdjustment *vadjustment)
619 GtkScrolledWindowPrivate *priv;
623 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
625 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
627 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
629 bin = GTK_BIN (scrolled_window);
630 priv = scrolled_window->priv;
632 if (!priv->vscrollbar)
634 gtk_widget_push_composite_child ();
635 priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
636 gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
637 gtk_widget_pop_composite_child ();
639 gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
640 g_object_ref (priv->vscrollbar);
641 gtk_widget_show (priv->vscrollbar);
645 GtkAdjustment *old_adjustment;
647 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
648 if (old_adjustment == vadjustment)
651 g_signal_handlers_disconnect_by_func (old_adjustment,
652 gtk_scrolled_window_adjustment_changed,
654 gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
657 vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
658 g_signal_connect (vadjustment,
660 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
662 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
664 child = gtk_bin_get_child (bin);
665 if (GTK_IS_SCROLLABLE (child))
666 gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
668 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
672 * gtk_scrolled_window_get_hadjustment:
673 * @scrolled_window: a #GtkScrolledWindow
675 * Returns the horizontal scrollbar's adjustment, used to connect the
676 * horizontal scrollbar to the child widget's horizontal scroll
679 * Returns: (transfer none): the horizontal #GtkAdjustment
682 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
684 GtkScrolledWindowPrivate *priv;
686 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
688 priv = scrolled_window->priv;
690 return (priv->hscrollbar ?
691 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
696 * gtk_scrolled_window_get_vadjustment:
697 * @scrolled_window: a #GtkScrolledWindow
699 * Returns the vertical scrollbar's adjustment, used to connect the
700 * vertical scrollbar to the child widget's vertical scroll functionality.
702 * Returns: (transfer none): the vertical #GtkAdjustment
705 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
707 GtkScrolledWindowPrivate *priv;
709 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
711 priv = scrolled_window->priv;
713 return (priv->vscrollbar ?
714 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
719 * gtk_scrolled_window_get_hscrollbar:
720 * @scrolled_window: a #GtkScrolledWindow
722 * Returns the horizontal scrollbar of @scrolled_window.
724 * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
725 * or %NULL if it does not have one.
730 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
732 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
734 return scrolled_window->priv->hscrollbar;
738 * gtk_scrolled_window_get_vscrollbar:
739 * @scrolled_window: a #GtkScrolledWindow
741 * Returns the vertical scrollbar of @scrolled_window.
743 * Returns: (transfer none): the vertical scrollbar of the scrolled window,
744 * or %NULL if it does not have one.
749 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
751 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
753 return scrolled_window->priv->vscrollbar;
757 * gtk_scrolled_window_set_policy:
758 * @scrolled_window: a #GtkScrolledWindow
759 * @hscrollbar_policy: policy for horizontal bar
760 * @vscrollbar_policy: policy for vertical bar
762 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
764 * The policy determines when the scrollbar should appear; it is a value
765 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
766 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
767 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
768 * if needed (that is, if the slider part of the bar would be smaller
769 * than the trough - the display is larger than the page size).
772 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
773 GtkPolicyType hscrollbar_policy,
774 GtkPolicyType vscrollbar_policy)
776 GtkScrolledWindowPrivate *priv;
777 GObject *object = G_OBJECT (scrolled_window);
779 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
781 priv = scrolled_window->priv;
783 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
784 (priv->vscrollbar_policy != vscrollbar_policy))
786 priv->hscrollbar_policy = hscrollbar_policy;
787 priv->vscrollbar_policy = vscrollbar_policy;
789 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
791 g_object_freeze_notify (object);
792 g_object_notify (object, "hscrollbar-policy");
793 g_object_notify (object, "vscrollbar-policy");
794 g_object_thaw_notify (object);
799 * gtk_scrolled_window_get_policy:
800 * @scrolled_window: a #GtkScrolledWindow
801 * @hscrollbar_policy: (out) (allow-none): location to store the policy
802 * for the horizontal scrollbar, or %NULL.
803 * @vscrollbar_policy: (out) (allow-none): location to store the policy
804 * for the vertical scrollbar, or %NULL.
806 * Retrieves the current policy values for the horizontal and vertical
807 * scrollbars. See gtk_scrolled_window_set_policy().
810 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
811 GtkPolicyType *hscrollbar_policy,
812 GtkPolicyType *vscrollbar_policy)
814 GtkScrolledWindowPrivate *priv;
816 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
818 priv = scrolled_window->priv;
820 if (hscrollbar_policy)
821 *hscrollbar_policy = priv->hscrollbar_policy;
822 if (vscrollbar_policy)
823 *vscrollbar_policy = priv->vscrollbar_policy;
827 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
829 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
830 GtkSettings *settings;
832 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
834 if (priv->window_placement_set || settings == NULL)
835 priv->real_window_placement = priv->window_placement;
837 g_object_get (settings,
838 "gtk-scrolled-window-placement",
839 &priv->real_window_placement,
844 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
845 GtkCornerType window_placement)
847 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
849 if (priv->window_placement != window_placement)
851 priv->window_placement = window_placement;
853 gtk_scrolled_window_update_real_placement (scrolled_window);
854 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
856 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
861 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
862 gboolean placement_set,
863 gboolean emit_resize)
865 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
867 if (priv->window_placement_set != placement_set)
869 priv->window_placement_set = placement_set;
871 gtk_scrolled_window_update_real_placement (scrolled_window);
873 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
875 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
880 * gtk_scrolled_window_set_placement:
881 * @scrolled_window: a #GtkScrolledWindow
882 * @window_placement: position of the child window
884 * Sets the placement of the contents with respect to the scrollbars
885 * for the scrolled window.
887 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
888 * in the top left, with the scrollbars underneath and to the right.
889 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
890 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
892 * See also gtk_scrolled_window_get_placement() and
893 * gtk_scrolled_window_unset_placement().
896 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
897 GtkCornerType window_placement)
899 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
901 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
902 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
906 * gtk_scrolled_window_get_placement:
907 * @scrolled_window: a #GtkScrolledWindow
909 * Gets the placement of the contents with respect to the scrollbars
910 * for the scrolled window. See gtk_scrolled_window_set_placement().
912 * Return value: the current placement value.
914 * See also gtk_scrolled_window_set_placement() and
915 * gtk_scrolled_window_unset_placement().
918 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
920 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
922 return scrolled_window->priv->window_placement;
926 * gtk_scrolled_window_unset_placement:
927 * @scrolled_window: a #GtkScrolledWindow
929 * Unsets the placement of the contents with respect to the scrollbars
930 * for the scrolled window. If no window placement is set for a scrolled
931 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
933 * See also gtk_scrolled_window_set_placement() and
934 * gtk_scrolled_window_get_placement().
939 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
941 GtkScrolledWindowPrivate *priv;
943 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
945 priv = scrolled_window->priv;
947 if (priv->window_placement_set)
949 priv->window_placement_set = FALSE;
951 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
953 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
958 * gtk_scrolled_window_set_shadow_type:
959 * @scrolled_window: a #GtkScrolledWindow
960 * @type: kind of shadow to draw around scrolled window contents
962 * Changes the type of shadow drawn around the contents of
967 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
970 GtkScrolledWindowPrivate *priv;
972 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
973 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
975 priv = scrolled_window->priv;
977 if (priv->shadow_type != type)
979 priv->shadow_type = type;
981 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
982 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
984 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
986 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
991 * gtk_scrolled_window_get_shadow_type:
992 * @scrolled_window: a #GtkScrolledWindow
994 * Gets the shadow type of the scrolled window. See
995 * gtk_scrolled_window_set_shadow_type().
997 * Return value: the current shadow type
1000 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
1002 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
1004 return scrolled_window->priv->shadow_type;
1008 gtk_scrolled_window_destroy (GtkWidget *widget)
1010 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1011 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1013 if (priv->hscrollbar)
1015 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1016 gtk_scrolled_window_adjustment_changed,
1018 gtk_widget_unparent (priv->hscrollbar);
1019 gtk_widget_destroy (priv->hscrollbar);
1020 g_object_unref (priv->hscrollbar);
1021 priv->hscrollbar = NULL;
1023 if (priv->vscrollbar)
1025 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1026 gtk_scrolled_window_adjustment_changed,
1028 gtk_widget_unparent (priv->vscrollbar);
1029 gtk_widget_destroy (priv->vscrollbar);
1030 g_object_unref (priv->vscrollbar);
1031 priv->vscrollbar = NULL;
1034 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1038 gtk_scrolled_window_set_property (GObject *object,
1040 const GValue *value,
1043 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1044 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1048 case PROP_HADJUSTMENT:
1049 gtk_scrolled_window_set_hadjustment (scrolled_window,
1050 g_value_get_object (value));
1052 case PROP_VADJUSTMENT:
1053 gtk_scrolled_window_set_vadjustment (scrolled_window,
1054 g_value_get_object (value));
1056 case PROP_HSCROLLBAR_POLICY:
1057 gtk_scrolled_window_set_policy (scrolled_window,
1058 g_value_get_enum (value),
1059 priv->vscrollbar_policy);
1061 case PROP_VSCROLLBAR_POLICY:
1062 gtk_scrolled_window_set_policy (scrolled_window,
1063 priv->hscrollbar_policy,
1064 g_value_get_enum (value));
1066 case PROP_WINDOW_PLACEMENT:
1067 gtk_scrolled_window_set_placement_internal (scrolled_window,
1068 g_value_get_enum (value));
1070 case PROP_WINDOW_PLACEMENT_SET:
1071 gtk_scrolled_window_set_placement_set (scrolled_window,
1072 g_value_get_boolean (value),
1075 case PROP_SHADOW_TYPE:
1076 gtk_scrolled_window_set_shadow_type (scrolled_window,
1077 g_value_get_enum (value));
1079 case PROP_MIN_CONTENT_WIDTH:
1080 gtk_scrolled_window_set_min_content_width (scrolled_window,
1081 g_value_get_int (value));
1083 case PROP_MIN_CONTENT_HEIGHT:
1084 gtk_scrolled_window_set_min_content_height (scrolled_window,
1085 g_value_get_int (value));
1088 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1094 gtk_scrolled_window_get_property (GObject *object,
1099 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1100 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1104 case PROP_HADJUSTMENT:
1105 g_value_set_object (value,
1106 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1108 case PROP_VADJUSTMENT:
1109 g_value_set_object (value,
1110 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1112 case PROP_WINDOW_PLACEMENT:
1113 g_value_set_enum (value, priv->window_placement);
1115 case PROP_WINDOW_PLACEMENT_SET:
1116 g_value_set_boolean (value, priv->window_placement_set);
1118 case PROP_SHADOW_TYPE:
1119 g_value_set_enum (value, priv->shadow_type);
1121 case PROP_HSCROLLBAR_POLICY:
1122 g_value_set_enum (value, priv->hscrollbar_policy);
1124 case PROP_VSCROLLBAR_POLICY:
1125 g_value_set_enum (value, priv->vscrollbar_policy);
1127 case PROP_MIN_CONTENT_WIDTH:
1128 g_value_set_int (value, priv->min_content_width);
1130 case PROP_MIN_CONTENT_HEIGHT:
1131 g_value_set_int (value, priv->min_content_height);
1134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1140 traverse_container (GtkWidget *widget,
1143 if (GTK_IS_SCROLLED_WINDOW (widget))
1145 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1146 gtk_widget_queue_resize (widget);
1148 else if (GTK_IS_CONTAINER (widget))
1149 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1153 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1157 list = gtk_window_list_toplevels ();
1159 for (l = list; l; l = l->next)
1160 gtk_container_forall (GTK_CONTAINER (l->data),
1161 traverse_container, NULL);
1167 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1168 GdkScreen *previous_screen)
1170 GtkSettings *settings;
1171 guint window_placement_connection;
1173 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1175 if (!gtk_widget_has_screen (widget))
1178 settings = gtk_widget_get_settings (widget);
1180 window_placement_connection =
1181 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1182 "gtk-scrolled-window-connection"));
1184 if (window_placement_connection)
1187 window_placement_connection =
1188 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1189 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1190 g_object_set_data (G_OBJECT (settings),
1191 I_("gtk-scrolled-window-connection"),
1192 GUINT_TO_POINTER (window_placement_connection));
1196 gtk_scrolled_window_draw (GtkWidget *widget,
1199 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1200 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1202 if (priv->shadow_type != GTK_SHADOW_NONE)
1204 GtkAllocation relative_allocation;
1205 GtkStyleContext *context;
1206 gboolean scrollbars_within_bevel;
1208 context = gtk_widget_get_style_context (widget);
1210 gtk_style_context_save (context);
1211 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1213 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1215 if (!scrollbars_within_bevel)
1217 GtkStateFlags state;
1218 GtkBorder padding, border;
1220 state = gtk_widget_get_state_flags (widget);
1221 gtk_style_context_get_padding (context, state, &padding);
1222 gtk_style_context_get_border (context, state, &border);
1224 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1226 relative_allocation.x -= padding.left + border.left;
1227 relative_allocation.y -= padding.top + border.top;
1228 relative_allocation.width += padding.left + padding.right + border.left + border.right;
1229 relative_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1233 relative_allocation.x = 0;
1234 relative_allocation.y = 0;
1235 relative_allocation.width = gtk_widget_get_allocated_width (widget);
1236 relative_allocation.height = gtk_widget_get_allocated_height (widget);
1239 gtk_render_frame (context, cr,
1240 relative_allocation.x,
1241 relative_allocation.y,
1242 relative_allocation.width,
1243 relative_allocation.height);
1245 gtk_style_context_restore (context);
1248 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1254 gtk_scrolled_window_forall (GtkContainer *container,
1255 gboolean include_internals,
1256 GtkCallback callback,
1257 gpointer callback_data)
1259 GtkScrolledWindowPrivate *priv;
1260 GtkScrolledWindow *scrolled_window;
1262 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1263 g_return_if_fail (callback != NULL);
1265 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1269 if (include_internals)
1271 scrolled_window = GTK_SCROLLED_WINDOW (container);
1272 priv = scrolled_window->priv;
1274 if (priv->vscrollbar)
1275 callback (priv->vscrollbar, callback_data);
1276 if (priv->hscrollbar)
1277 callback (priv->hscrollbar, callback_data);
1282 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1283 GtkScrollType scroll,
1284 gboolean horizontal)
1286 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1287 GtkAdjustment *adjustment = NULL;
1291 case GTK_SCROLL_STEP_UP:
1292 scroll = GTK_SCROLL_STEP_BACKWARD;
1295 case GTK_SCROLL_STEP_DOWN:
1296 scroll = GTK_SCROLL_STEP_FORWARD;
1299 case GTK_SCROLL_STEP_LEFT:
1300 scroll = GTK_SCROLL_STEP_BACKWARD;
1303 case GTK_SCROLL_STEP_RIGHT:
1304 scroll = GTK_SCROLL_STEP_FORWARD;
1307 case GTK_SCROLL_PAGE_UP:
1308 scroll = GTK_SCROLL_PAGE_BACKWARD;
1311 case GTK_SCROLL_PAGE_DOWN:
1312 scroll = GTK_SCROLL_PAGE_FORWARD;
1315 case GTK_SCROLL_PAGE_LEFT:
1316 scroll = GTK_SCROLL_STEP_BACKWARD;
1319 case GTK_SCROLL_PAGE_RIGHT:
1320 scroll = GTK_SCROLL_STEP_FORWARD;
1323 case GTK_SCROLL_STEP_BACKWARD:
1324 case GTK_SCROLL_STEP_FORWARD:
1325 case GTK_SCROLL_PAGE_BACKWARD:
1326 case GTK_SCROLL_PAGE_FORWARD:
1327 case GTK_SCROLL_START:
1328 case GTK_SCROLL_END:
1331 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1335 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1336 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1341 if (priv->hscrollbar)
1342 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1346 if (priv->vscrollbar)
1347 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1352 gdouble value = gtk_adjustment_get_value (adjustment);
1356 case GTK_SCROLL_STEP_FORWARD:
1357 value += gtk_adjustment_get_step_increment (adjustment);
1359 case GTK_SCROLL_STEP_BACKWARD:
1360 value -= gtk_adjustment_get_step_increment (adjustment);
1362 case GTK_SCROLL_PAGE_FORWARD:
1363 value += gtk_adjustment_get_page_increment (adjustment);
1365 case GTK_SCROLL_PAGE_BACKWARD:
1366 value -= gtk_adjustment_get_page_increment (adjustment);
1368 case GTK_SCROLL_START:
1369 value = gtk_adjustment_get_lower (adjustment);
1371 case GTK_SCROLL_END:
1372 value = gtk_adjustment_get_upper (adjustment);
1375 g_assert_not_reached ();
1379 gtk_adjustment_set_value (adjustment, value);
1388 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1389 GtkDirectionType direction_type)
1391 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1392 GtkWidget *toplevel;
1394 /* Focus out of the scrolled window entirely. We do this by setting
1395 * a flag, then propagating the focus motion to the notebook.
1397 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1398 if (!gtk_widget_is_toplevel (toplevel))
1401 g_object_ref (scrolled_window);
1403 priv->focus_out = TRUE;
1404 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1405 priv->focus_out = FALSE;
1407 g_object_unref (scrolled_window);
1411 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1412 GtkAllocation *allocation)
1414 GtkAllocation widget_allocation;
1415 GtkScrolledWindow *scrolled_window;
1416 GtkScrolledWindowPrivate *priv;
1421 g_return_if_fail (widget != NULL);
1422 g_return_if_fail (allocation != NULL);
1424 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1425 priv = scrolled_window->priv;
1427 /* Get possible scrollbar dimensions */
1428 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1429 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1430 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1432 /* Subtract some things from our available allocation size */
1436 if (priv->shadow_type != GTK_SHADOW_NONE)
1438 GtkStyleContext *context;
1439 GtkStateFlags state;
1440 GtkBorder padding, border;
1442 context = gtk_widget_get_style_context (widget);
1443 state = gtk_widget_get_state_flags (widget);
1445 gtk_style_context_save (context);
1446 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1448 gtk_style_context_get_border (context, state, &border);
1449 gtk_style_context_get_padding (context, state, &padding);
1451 allocation->x += padding.left + border.left;
1452 allocation->y += padding.top + border.top;
1454 gtk_style_context_restore (context);
1457 gtk_widget_get_allocation (widget, &widget_allocation);
1458 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1459 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1461 if (priv->vscrollbar_visible)
1465 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1468 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1469 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1471 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1472 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1473 allocation->x += (sb_width + sb_spacing);
1475 allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1477 if (priv->hscrollbar_visible)
1480 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1481 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1482 allocation->y += (sb_height + sb_spacing);
1484 allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1489 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1490 GtkAllocation *relative_allocation)
1492 GtkWidget *widget = GTK_WIDGET (swindow), *child;
1493 GtkAllocation allocation;
1494 GtkAllocation child_allocation;
1496 child = gtk_bin_get_child (GTK_BIN (widget));
1498 gtk_widget_get_allocation (widget, &allocation);
1500 gtk_scrolled_window_relative_allocation (widget, relative_allocation);
1501 child_allocation.x = relative_allocation->x + allocation.x;
1502 child_allocation.y = relative_allocation->y + allocation.y;
1503 child_allocation.width = relative_allocation->width;
1504 child_allocation.height = relative_allocation->height;
1506 gtk_widget_size_allocate (child, &child_allocation);
1510 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1511 GtkAllocation *allocation)
1513 GtkScrolledWindow *scrolled_window;
1514 GtkScrolledWindowPrivate *priv;
1515 GtkStyleContext *context;
1516 GtkStateFlags state;
1517 GtkBorder padding, border;
1519 GtkAllocation relative_allocation;
1520 GtkAllocation child_allocation;
1522 gboolean scrollbars_within_bevel;
1527 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1528 g_return_if_fail (allocation != NULL);
1530 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1531 bin = GTK_BIN (scrolled_window);
1532 priv = scrolled_window->priv;
1534 /* Get possible scrollbar dimensions */
1535 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1536 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1537 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1539 context = gtk_widget_get_style_context (widget);
1540 state = gtk_widget_get_state_flags (widget);
1542 gtk_style_context_save (context);
1543 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1545 gtk_style_context_get_padding (context, state, &padding);
1546 gtk_style_context_get_border (context, state, &border);
1548 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1550 gtk_widget_set_allocation (widget, allocation);
1552 gtk_style_context_restore (context);
1554 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1555 priv->hscrollbar_visible = TRUE;
1556 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1557 priv->hscrollbar_visible = FALSE;
1558 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1559 priv->vscrollbar_visible = TRUE;
1560 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1561 priv->vscrollbar_visible = FALSE;
1563 child = gtk_bin_get_child (bin);
1564 if (child && gtk_widget_get_visible (child))
1566 gint child_scroll_width;
1567 gint child_scroll_height;
1568 gboolean previous_hvis;
1569 gboolean previous_vvis;
1572 /* Determine scrollbar visibility first via hfw apis */
1573 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1575 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1576 gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
1578 gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
1580 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1582 /* First try without a vertical scrollbar if the content will fit the height
1583 * given the extra width of the scrollbar */
1584 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1585 gtk_widget_get_preferred_height_for_width (child,
1586 MAX (allocation->width, child_scroll_width),
1587 &child_scroll_height, NULL);
1589 gtk_widget_get_preferred_height_for_width (child,
1590 MAX (allocation->width, child_scroll_width),
1591 NULL, &child_scroll_height);
1593 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1595 /* Does the content height fit the allocation height ? */
1596 priv->vscrollbar_visible = child_scroll_height > allocation->height;
1598 /* Does the content width fit the allocation with minus a possible scrollbar ? */
1599 priv->hscrollbar_visible =
1600 child_scroll_width > allocation->width -
1601 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1603 /* Now that we've guessed the hscrollbar, does the content height fit
1604 * the possible new allocation height ? */
1605 priv->vscrollbar_visible =
1606 child_scroll_height > allocation->height -
1607 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1609 /* Now that we've guessed the vscrollbar, does the content width fit
1610 * the possible new allocation width ? */
1611 priv->hscrollbar_visible =
1612 child_scroll_width > allocation->width -
1613 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1615 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1617 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1618 priv->vscrollbar_visible = child_scroll_height > allocation->height -
1619 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1622 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1624 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1626 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1627 priv->hscrollbar_visible =
1628 child_scroll_width > allocation->width -
1629 (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1631 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1634 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1636 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1637 gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1639 gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1641 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1643 /* First try without a horizontal scrollbar if the content will fit the width
1644 * given the extra height of the scrollbar */
1645 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1646 gtk_widget_get_preferred_width_for_height (child,
1647 MAX (allocation->height, child_scroll_height),
1648 &child_scroll_width, NULL);
1650 gtk_widget_get_preferred_width_for_height (child,
1651 MAX (allocation->height, child_scroll_height),
1652 NULL, &child_scroll_width);
1654 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1656 /* Does the content width fit the allocation width ? */
1657 priv->hscrollbar_visible = child_scroll_width > allocation->width;
1659 /* Does the content height fit the allocation with minus a possible scrollbar ? */
1660 priv->vscrollbar_visible =
1661 child_scroll_height > allocation->height -
1662 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1664 /* Now that we've guessed the vscrollbar, does the content width fit
1665 * the possible new allocation width ? */
1666 priv->hscrollbar_visible =
1667 child_scroll_width > allocation->width -
1668 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1670 /* Now that we've guessed the hscrollbar, does the content height fit
1671 * the possible new allocation height ? */
1672 priv->vscrollbar_visible =
1673 child_scroll_height > allocation->height -
1674 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1676 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1678 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1679 priv->hscrollbar_visible = child_scroll_width > allocation->width -
1680 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1683 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1685 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1687 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1688 priv->vscrollbar_visible =
1689 child_scroll_height > allocation->height -
1690 (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1692 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1696 /* Now after guessing scrollbar visibility; fall back on the allocation loop which
1697 * observes the adjustments to detect scrollbar visibility and also avoids
1698 * infinite recursion
1702 previous_hvis = priv->hscrollbar_visible;
1703 previous_vvis = priv->vscrollbar_visible;
1704 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1706 /* Explicitly force scrollbar visibility checks.
1708 * Since we make a guess above, the child might not decide to update the adjustments
1709 * if they logically did not change since the last configuration
1711 if (priv->hscrollbar)
1712 gtk_scrolled_window_adjustment_changed
1713 (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)), scrolled_window);
1715 if (priv->vscrollbar)
1716 gtk_scrolled_window_adjustment_changed
1717 (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)), scrolled_window);
1719 /* If, after the first iteration, the hscrollbar and the
1720 * vscrollbar flip visiblity... or if one of the scrollbars flip
1721 * on each itteration indefinitly/infinitely, then we just need both
1725 previous_hvis != priv->hscrollbar_visible &&
1726 previous_vvis != priv->vscrollbar_visible) || count > 3)
1728 priv->hscrollbar_visible = TRUE;
1729 priv->vscrollbar_visible = TRUE;
1731 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1738 while (previous_hvis != priv->hscrollbar_visible ||
1739 previous_vvis != priv->vscrollbar_visible);
1743 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1744 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1745 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1748 if (priv->hscrollbar_visible)
1750 if (!gtk_widget_get_visible (priv->hscrollbar))
1751 gtk_widget_show (priv->hscrollbar);
1753 child_allocation.x = relative_allocation.x;
1754 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1755 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1756 child_allocation.y = (relative_allocation.y +
1757 relative_allocation.height +
1759 (priv->shadow_type == GTK_SHADOW_NONE ?
1760 0 : padding.top + border.top));
1762 child_allocation.y = 0;
1764 child_allocation.width = relative_allocation.width;
1765 child_allocation.height = sb_height;
1766 child_allocation.x += allocation->x;
1767 child_allocation.y += allocation->y;
1769 if (priv->shadow_type != GTK_SHADOW_NONE)
1771 if (!scrollbars_within_bevel)
1773 child_allocation.x -= padding.left + border.left;
1774 child_allocation.width += padding.left + padding.right + border.left + border.right;
1776 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1777 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1779 child_allocation.y -= padding.top + border.top;
1783 child_allocation.y += padding.top + border.top;
1787 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1789 else if (gtk_widget_get_visible (priv->hscrollbar))
1790 gtk_widget_hide (priv->hscrollbar);
1792 if (priv->vscrollbar_visible)
1794 if (!gtk_widget_get_visible (priv->vscrollbar))
1795 gtk_widget_show (priv->vscrollbar);
1797 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1798 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1799 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1800 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1801 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1802 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1803 child_allocation.x = (relative_allocation.x +
1804 relative_allocation.width +
1806 (priv->shadow_type == GTK_SHADOW_NONE ?
1807 0 : padding.left + border.left));
1809 child_allocation.x = 0;
1811 child_allocation.y = relative_allocation.y;
1812 child_allocation.width = sb_width;
1813 child_allocation.height = relative_allocation.height;
1814 child_allocation.x += allocation->x;
1815 child_allocation.y += allocation->y;
1817 if (priv->shadow_type != GTK_SHADOW_NONE)
1819 if (!scrollbars_within_bevel)
1821 child_allocation.y -= padding.top + border.top;
1822 child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1824 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1825 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1827 child_allocation.x -= padding.left + border.left;
1831 child_allocation.x += padding.left + border.left;
1835 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1837 else if (gtk_widget_get_visible (priv->vscrollbar))
1838 gtk_widget_hide (priv->vscrollbar);
1842 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1843 GdkEventScroll *event)
1845 GtkScrolledWindowPrivate *priv;
1846 GtkScrolledWindow *scrolled_window;
1849 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1850 g_return_val_if_fail (event != NULL, FALSE);
1852 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1853 priv = scrolled_window->priv;
1855 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1856 range = priv->vscrollbar;
1858 range = priv->hscrollbar;
1860 if (range && gtk_widget_get_visible (range))
1862 GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (range));
1865 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1867 gtk_adjustment_set_value (adjustment, gtk_adjustment_get_value (adjustment) + delta);
1876 gtk_scrolled_window_focus (GtkWidget *widget,
1877 GtkDirectionType direction)
1879 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1880 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1882 gboolean had_focus_child;
1884 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1886 if (priv->focus_out)
1888 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1892 if (gtk_widget_is_focus (widget))
1895 /* We only put the scrolled window itself in the focus chain if it
1896 * isn't possible to focus any children.
1898 child = gtk_bin_get_child (GTK_BIN (widget));
1901 if (gtk_widget_child_focus (child, direction))
1905 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1907 gtk_widget_grab_focus (widget);
1915 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1918 GtkScrolledWindowPrivate *priv;
1919 GtkScrolledWindow *scrolled_window;
1921 g_return_if_fail (adjustment != NULL);
1922 g_return_if_fail (data != NULL);
1924 scrolled_window = GTK_SCROLLED_WINDOW (data);
1925 priv = scrolled_window->priv;
1927 if (priv->hscrollbar &&
1928 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1930 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1934 visible = priv->hscrollbar_visible;
1935 priv->hscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1936 gtk_adjustment_get_page_size (adjustment));
1938 if (priv->hscrollbar_visible != visible)
1939 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1942 else if (priv->vscrollbar &&
1943 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1945 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1949 visible = priv->vscrollbar_visible;
1950 priv->vscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1951 gtk_adjustment_get_page_size (adjustment));
1953 if (priv->vscrollbar_visible != visible)
1954 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1960 gtk_scrolled_window_add (GtkContainer *container,
1963 GtkScrolledWindowPrivate *priv;
1964 GtkScrolledWindow *scrolled_window;
1966 GtkWidget *child_widget;
1967 GtkAdjustment *hadj, *vadj;
1969 bin = GTK_BIN (container);
1970 child_widget = gtk_bin_get_child (bin);
1971 g_return_if_fail (child_widget == NULL);
1973 scrolled_window = GTK_SCROLLED_WINDOW (container);
1974 priv = scrolled_window->priv;
1976 _gtk_bin_set_child (bin, child);
1977 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1979 hadj = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1980 vadj = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1982 if (GTK_IS_SCROLLABLE (child))
1983 g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1985 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1986 "use gtk_scrolled_window_add_with_viewport() instead");
1990 gtk_scrolled_window_remove (GtkContainer *container,
1993 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1994 g_return_if_fail (child != NULL);
1995 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1997 g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
1999 /* chain parent class handler to remove child */
2000 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
2004 * gtk_scrolled_window_add_with_viewport:
2005 * @scrolled_window: a #GtkScrolledWindow
2006 * @child: the widget you want to scroll
2008 * Used to add children without native scrolling capabilities. This
2009 * is simply a convenience function; it is equivalent to adding the
2010 * unscrollable child to a viewport, then adding the viewport to the
2011 * scrolled window. If a child has native scrolling, use
2012 * gtk_container_add() instead of this function.
2014 * The viewport scrolls the child by moving its #GdkWindow, and takes
2015 * the size of the child to be the size of its toplevel #GdkWindow.
2016 * This will be very wrong for most widgets that support native scrolling;
2017 * for example, if you add a widget such as #GtkTreeView with a viewport,
2018 * the whole widget will scroll, including the column headings. Thus,
2019 * widgets with native scrolling support should not be used with the
2020 * #GtkViewport proxy.
2022 * A widget supports scrolling natively if it implements the
2023 * #GtkScrollable interface.
2026 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
2030 GtkWidget *viewport;
2031 GtkWidget *child_widget;
2033 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2034 g_return_if_fail (GTK_IS_WIDGET (child));
2035 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
2037 bin = GTK_BIN (scrolled_window);
2038 child_widget = gtk_bin_get_child (bin);
2042 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2043 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2045 viewport = child_widget;
2050 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2051 gtk_scrolled_window_get_vadjustment (scrolled_window));
2052 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2055 gtk_widget_show (viewport);
2056 gtk_container_add (GTK_CONTAINER (viewport), child);
2060 * _gtk_scrolled_window_get_spacing:
2061 * @scrolled_window: a scrolled window
2063 * Gets the spacing between the scrolled window's scrollbars and
2064 * the scrolled widget. Used by GtkCombo
2066 * Return value: the spacing, in pixels.
2069 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2071 GtkScrolledWindowClass *class;
2073 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2075 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2077 if (class->scrollbar_spacing >= 0)
2078 return class->scrollbar_spacing;
2081 gint scrollbar_spacing;
2083 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2084 "scrollbar-spacing", &scrollbar_spacing,
2087 return scrollbar_spacing;
2093 gtk_scrolled_window_get_preferred_size (GtkWidget *widget,
2094 GtkOrientation orientation,
2098 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2099 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2100 GtkBin *bin = GTK_BIN (scrolled_window);
2103 gint scrollbar_spacing;
2104 GtkRequisition hscrollbar_requisition;
2105 GtkRequisition vscrollbar_requisition;
2106 GtkRequisition minimum_req, natural_req;
2108 gint min_child_size, nat_child_size;
2110 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2114 minimum_req.width = 0;
2115 minimum_req.height = 0;
2116 natural_req.width = 0;
2117 natural_req.height = 0;
2119 gtk_widget_get_preferred_size (priv->hscrollbar,
2120 &hscrollbar_requisition, NULL);
2121 gtk_widget_get_preferred_size (priv->vscrollbar,
2122 &vscrollbar_requisition, NULL);
2124 child = gtk_bin_get_child (bin);
2125 if (child && gtk_widget_get_visible (child))
2127 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2129 gtk_widget_get_preferred_width (child,
2133 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2135 minimum_req.width += min_child_size;
2136 natural_req.width += nat_child_size;
2140 gint min_content_width = priv->min_content_width;
2142 if (min_content_width >= 0)
2144 minimum_req.width = MAX (minimum_req.width, min_content_width);
2145 natural_req.width = MAX (natural_req.width, min_content_width);
2150 minimum_req.width += vscrollbar_requisition.width;
2151 natural_req.width += vscrollbar_requisition.width;
2155 else /* GTK_ORIENTATION_VERTICAL */
2157 gtk_widget_get_preferred_height (child,
2161 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2163 minimum_req.height += min_child_size;
2164 natural_req.height += nat_child_size;
2168 gint min_content_height = priv->min_content_height;
2170 if (min_content_height >= 0)
2172 minimum_req.height = MAX (minimum_req.height, min_content_height);
2173 natural_req.height = MAX (natural_req.height, min_content_height);
2178 minimum_req.height += vscrollbar_requisition.height;
2179 natural_req.height += vscrollbar_requisition.height;
2185 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2186 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2188 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2189 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2190 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2191 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2194 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2195 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2197 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2198 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2199 if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2200 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2203 minimum_req.width += MAX (0, extra_width);
2204 minimum_req.height += MAX (0, extra_height);
2205 natural_req.width += MAX (0, extra_width);
2206 natural_req.height += MAX (0, extra_height);
2208 if (priv->shadow_type != GTK_SHADOW_NONE)
2210 GtkStyleContext *context;
2211 GtkStateFlags state;
2212 GtkBorder padding, border;
2214 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
2215 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
2217 gtk_style_context_save (context);
2218 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
2220 gtk_style_context_get_padding (context, state, &padding);
2221 gtk_style_context_get_border (context, state, &border);
2223 minimum_req.width += padding.left + padding.right + border.left + border.right;
2224 minimum_req.height += padding.top + padding.bottom + border.top + border.bottom;
2225 natural_req.width += padding.left + padding.right + border.left + border.right;
2226 natural_req.height += padding.top + padding.bottom + border.top + border.bottom;
2228 gtk_style_context_restore (context);
2231 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2234 *minimum_size = minimum_req.width;
2236 *natural_size = natural_req.width;
2241 *minimum_size = minimum_req.height;
2243 *natural_size = natural_req.height;
2248 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2252 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2256 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2260 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2264 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2266 gint *minimum_height,
2267 gint *natural_height)
2269 g_return_if_fail (GTK_IS_WIDGET (widget));
2271 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2275 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2277 gint *minimum_width,
2278 gint *natural_width)
2280 g_return_if_fail (GTK_IS_WIDGET (widget));
2282 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2286 * gtk_scrolled_window_get_min_content_width:
2287 * @scrolled_window: a #GtkScrolledWindow
2289 * Gets the minimum content width of @scrolled_window, or -1 if not set.
2291 * Returns: the minimum content width
2296 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2298 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2300 return scrolled_window->priv->min_content_width;
2304 * gtk_scrolled_window_set_min_content_width:
2305 * @scrolled_window: a #GtkScrolledWindow
2306 * @width: the minimal content width
2308 * Sets the minimum width that @scrolled_window should keep visible.
2309 * Note that this can and (usually will) be smaller than the minimum
2310 * size of the content.
2315 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2318 GtkScrolledWindowPrivate *priv;
2320 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2322 priv = scrolled_window->priv;
2324 if (priv->min_content_width != width)
2326 priv->min_content_width = width;
2328 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2330 g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2335 * gtk_scrolled_window_get_min_content_height:
2336 * @scrolled_window: a #GtkScrolledWindow
2338 * Gets the minimal content height of @scrolled_window, or -1 if not set.
2340 * Returns: the minimal content height
2345 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2347 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2349 return scrolled_window->priv->min_content_height;
2353 * gtk_scrolled_window_set_min_content_height:
2354 * @scrolled_window: a #GtkScrolledWindow
2355 * @height: the minimal content height
2357 * Sets the minimum height that @scrolled_window should keep visible.
2358 * Note that this can and (usually will) be smaller than the minimum
2359 * size of the content.
2364 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2367 GtkScrolledWindowPrivate *priv;
2369 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2371 priv = scrolled_window->priv;
2373 if (priv->min_content_height != height)
2375 priv->min_content_height = height;
2377 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2379 g_object_notify (G_OBJECT (scrolled_window), "min-content-height");