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 "gtkviewport.h"
42 #include "a11y/gtkscrolledwindowaccessible.h"
45 * SECTION:gtkscrolledwindow
46 * @Short_description: Adds scrollbars to its child widget
47 * @Title: GtkScrolledWindow
48 * @See_also: #GtkScrollable, #GtkViewport, #GtkAdjustment
50 * #GtkScrolledWindow is a #GtkBin subclass: it's a container
51 * the accepts a single child widget. #GtkScrolledWindow adds scrollbars
52 * to the child widget and optionally draws a beveled frame around the
55 * The scrolled window can work in two ways. Some widgets have native
56 * scrolling support; these widgets implement the #GtkScrollable interface.
57 * Widgets with native scroll support include #GtkTreeView, #GtkTextView,
60 * For widgets that lack native scrolling support, the #GtkViewport
61 * widget acts as an adaptor class, implementing scrollability for child
62 * widgets that lack their own scrolling capabilities. Use #GtkViewport
63 * to scroll child widgets such as #GtkGrid, #GtkBox, and so on.
65 * If a widget has native scrolling abilities, it can be added to the
66 * #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
67 * must first add the widget to a #GtkViewport, then add the #GtkViewport
68 * to the scrolled window. The convenience function
69 * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
70 * ignore the presence of the viewport.
72 * The position of the scrollbars is controlled by the scroll
73 * adjustments. See #GtkAdjustment for the fields in an adjustment - for
74 * #GtkScrollbar, used by #GtkScrolledWindow, the "value" field
75 * represents the position of the scrollbar, which must be between the
76 * "lower" field and "upper - page_size." The "page_size" field
77 * represents the size of the visible scrollable area. The
78 * "step_increment" and "page_increment" fields are used when the user
79 * asks to step down (using the small stepper arrows) or page down (using
80 * for example the PageDown key).
82 * If a #GtkScrolledWindow doesn't behave quite as you would like, or
83 * doesn't have exactly the right layout, it's very possible to set up
84 * your own scrolling with #GtkScrollbar and for example a #GtkGrid.
88 /* scrolled window policy and size requisition handling:
90 * gtk size requisition works as follows:
91 * a widget upon size-request reports the width and height that it finds
92 * to be best suited to display its contents, including children.
93 * the width and/or height reported from a widget upon size requisition
94 * may be overidden by the user by specifying a width and/or height
95 * other than 0 through gtk_widget_set_size_request().
97 * a scrolled window needs (for implementing all three policy types) to
98 * request its width and height based on two different rationales.
99 * 1) the user wants the scrolled window to just fit into the space
100 * that it gets allocated for a specifc dimension.
101 * 1.1) this does not apply if the user specified a concrete value
102 * value for that specific dimension by either specifying usize for the
103 * scrolled window or for its child.
104 * 2) the user wants the scrolled window to take as much space up as
105 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
107 * also, kinda obvious:
108 * 3) a user would certainly not have choosen a scrolled window as a container
109 * for the child, if the resulting allocation takes up more space than the
110 * child would have allocated without the scrolled window.
113 * A) from 1) follows: the scrolled window shouldn't request more space for a
114 * specifc dimension than is required at minimum.
115 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
116 * window (done automatically) or by usize of the child (needs to be checked).
117 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
119 * D) from 3) follows: the scrolled window child's minimum width and minimum height
120 * under A) at least correspond to the space taken up by its scrollbars.
123 #define DEFAULT_SCROLLBAR_SPACING 3
125 struct _GtkScrolledWindowPrivate
127 GtkWidget *hscrollbar;
128 GtkWidget *vscrollbar;
130 GtkCornerType real_window_placement;
133 guint window_placement_set : 1;
134 guint hscrollbar_policy : 2;
135 guint vscrollbar_policy : 2;
136 guint hscrollbar_visible : 1;
137 guint vscrollbar_visible : 1;
138 guint window_placement : 2;
139 guint focus_out : 1; /* Flag used by ::move-focus-out implementation */
141 gint min_content_width;
142 gint min_content_height;
150 PROP_HSCROLLBAR_POLICY,
151 PROP_VSCROLLBAR_POLICY,
152 PROP_WINDOW_PLACEMENT,
153 PROP_WINDOW_PLACEMENT_SET,
155 PROP_MIN_CONTENT_WIDTH,
156 PROP_MIN_CONTENT_HEIGHT
167 static void gtk_scrolled_window_set_property (GObject *object,
171 static void gtk_scrolled_window_get_property (GObject *object,
176 static void gtk_scrolled_window_destroy (GtkWidget *widget);
177 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
178 GdkScreen *previous_screen);
179 static gboolean gtk_scrolled_window_draw (GtkWidget *widget,
181 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
182 GtkAllocation *allocation);
183 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
184 GdkEventScroll *event);
185 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
186 GtkDirectionType direction);
187 static void gtk_scrolled_window_add (GtkContainer *container,
189 static void gtk_scrolled_window_remove (GtkContainer *container,
191 static void gtk_scrolled_window_forall (GtkContainer *container,
192 gboolean include_internals,
193 GtkCallback callback,
194 gpointer callback_data);
195 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
196 GtkScrollType scroll,
197 gboolean horizontal);
198 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
199 GtkDirectionType direction_type);
201 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
202 GtkAllocation *allocation);
203 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
206 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
208 static void gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
211 static void gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
214 static void gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *layout,
216 gint *minimum_height,
217 gint *natural_height);
218 static void gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *layout,
220 gint *minimum_height,
221 gint *natural_height);
223 static guint signals[LAST_SIGNAL] = {0};
225 G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
229 add_scroll_binding (GtkBindingSet *binding_set,
231 GdkModifierType mask,
232 GtkScrollType scroll,
235 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
237 gtk_binding_entry_add_signal (binding_set, keyval, mask,
239 GTK_TYPE_SCROLL_TYPE, scroll,
240 G_TYPE_BOOLEAN, horizontal);
241 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
243 GTK_TYPE_SCROLL_TYPE, scroll,
244 G_TYPE_BOOLEAN, horizontal);
248 add_tab_bindings (GtkBindingSet *binding_set,
249 GdkModifierType modifiers,
250 GtkDirectionType direction)
252 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
254 GTK_TYPE_DIRECTION_TYPE, direction);
255 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
257 GTK_TYPE_DIRECTION_TYPE, direction);
261 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
263 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
264 GtkWidgetClass *widget_class;
265 GtkContainerClass *container_class;
266 GtkBindingSet *binding_set;
268 widget_class = (GtkWidgetClass*) class;
269 container_class = (GtkContainerClass*) class;
271 gobject_class->set_property = gtk_scrolled_window_set_property;
272 gobject_class->get_property = gtk_scrolled_window_get_property;
274 widget_class->destroy = gtk_scrolled_window_destroy;
275 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
276 widget_class->draw = gtk_scrolled_window_draw;
277 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
278 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
279 widget_class->focus = gtk_scrolled_window_focus;
280 widget_class->get_preferred_width = gtk_scrolled_window_get_preferred_width;
281 widget_class->get_preferred_height = gtk_scrolled_window_get_preferred_height;
282 widget_class->get_preferred_height_for_width = gtk_scrolled_window_get_preferred_height_for_width;
283 widget_class->get_preferred_width_for_height = gtk_scrolled_window_get_preferred_width_for_height;
285 container_class->add = gtk_scrolled_window_add;
286 container_class->remove = gtk_scrolled_window_remove;
287 container_class->forall = gtk_scrolled_window_forall;
288 gtk_container_class_handle_border_width (container_class);
290 class->scrollbar_spacing = -1;
292 class->scroll_child = gtk_scrolled_window_scroll_child;
293 class->move_focus_out = gtk_scrolled_window_move_focus_out;
295 g_object_class_install_property (gobject_class,
297 g_param_spec_object ("hadjustment",
298 P_("Horizontal Adjustment"),
299 P_("The GtkAdjustment for the horizontal position"),
301 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
302 g_object_class_install_property (gobject_class,
304 g_param_spec_object ("vadjustment",
305 P_("Vertical Adjustment"),
306 P_("The GtkAdjustment for the vertical position"),
308 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
309 g_object_class_install_property (gobject_class,
310 PROP_HSCROLLBAR_POLICY,
311 g_param_spec_enum ("hscrollbar-policy",
312 P_("Horizontal Scrollbar Policy"),
313 P_("When the horizontal scrollbar is displayed"),
314 GTK_TYPE_POLICY_TYPE,
315 GTK_POLICY_AUTOMATIC,
316 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
317 g_object_class_install_property (gobject_class,
318 PROP_VSCROLLBAR_POLICY,
319 g_param_spec_enum ("vscrollbar-policy",
320 P_("Vertical Scrollbar Policy"),
321 P_("When the vertical scrollbar is displayed"),
322 GTK_TYPE_POLICY_TYPE,
323 GTK_POLICY_AUTOMATIC,
324 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
326 g_object_class_install_property (gobject_class,
327 PROP_WINDOW_PLACEMENT,
328 g_param_spec_enum ("window-placement",
329 P_("Window Placement"),
330 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
331 GTK_TYPE_CORNER_TYPE,
333 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
336 * GtkScrolledWindow:window-placement-set:
338 * Whether "window-placement" should be used to determine the location
339 * of the contents with respect to the scrollbars. Otherwise, the
340 * "gtk-scrolled-window-placement" setting is used.
344 g_object_class_install_property (gobject_class,
345 PROP_WINDOW_PLACEMENT_SET,
346 g_param_spec_boolean ("window-placement-set",
347 P_("Window Placement Set"),
348 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
350 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
351 g_object_class_install_property (gobject_class,
353 g_param_spec_enum ("shadow-type",
355 P_("Style of bevel around the contents"),
356 GTK_TYPE_SHADOW_TYPE,
358 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
361 * GtkScrolledWindow:scrollbars-within-bevel:
363 * Whether to place scrollbars within the scrolled window's bevel.
367 gtk_widget_class_install_style_property (widget_class,
368 g_param_spec_boolean ("scrollbars-within-bevel",
369 P_("Scrollbars within bevel"),
370 P_("Place scrollbars within the scrolled window's bevel"),
372 GTK_PARAM_READABLE));
374 gtk_widget_class_install_style_property (widget_class,
375 g_param_spec_int ("scrollbar-spacing",
376 P_("Scrollbar spacing"),
377 P_("Number of pixels between the scrollbars and the scrolled window"),
380 DEFAULT_SCROLLBAR_SPACING,
381 GTK_PARAM_READABLE));
384 * GtkScrolledWindow:min-content-width:
386 * The minimum content width of @scrolled_window, or -1 if not set.
390 g_object_class_install_property (gobject_class,
391 PROP_MIN_CONTENT_WIDTH,
392 g_param_spec_int ("min-content-width",
393 P_("Minimum Content Width"),
394 P_("The minimum width that the scrolled window will allocate to its content"),
396 GTK_PARAM_READWRITE));
399 * GtkScrolledWindow:min-content-height:
401 * The minimum content height of @scrolled_window, or -1 if not set.
405 g_object_class_install_property (gobject_class,
406 PROP_MIN_CONTENT_HEIGHT,
407 g_param_spec_int ("min-content-height",
408 P_("Minimum Content Height"),
409 P_("The minimum height that the scrolled window will allocate to its content"),
411 GTK_PARAM_READWRITE));
413 * GtkScrolledWindow::scroll-child:
414 * @scrolled_window: a #GtkScrolledWindow
415 * @scroll: a #GtkScrollType describing how much to scroll
416 * @horizontal: whether the keybinding scrolls the child
417 * horizontally or not
419 * The ::scroll-child signal is a
420 * <link linkend="keybinding-signals">keybinding signal</link>
421 * which gets emitted when a keybinding that scrolls is pressed.
422 * The horizontal or vertical adjustment is updated which triggers a
423 * signal that the scrolled windows child may listen to and scroll itself.
425 signals[SCROLL_CHILD] =
426 g_signal_new (I_("scroll-child"),
427 G_TYPE_FROM_CLASS (gobject_class),
428 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
429 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
431 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
433 GTK_TYPE_SCROLL_TYPE,
437 * GtkScrolledWindow::move-focus-out:
438 * @scrolled_window: a #GtkScrolledWindow
439 * @direction_type: either %GTK_DIR_TAB_FORWARD or
440 * %GTK_DIR_TAB_BACKWARD
442 * The ::move-focus-out signal is a
443 * <link linkend="keybinding-signals">keybinding signal</link>
444 * which gets emitted when focus is moved away from the scrolled
445 * window by a keybinding.
446 * The #GtkWidget::move-focus signal is emitted with @direction_type
447 * on this scrolled windows toplevel parent in the container hierarchy.
448 * The default bindings for this signal are
449 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap></keycombo>
451 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap><keycap>Shift</keycap></keycombo>.
453 signals[MOVE_FOCUS_OUT] =
454 g_signal_new (I_("move-focus-out"),
455 G_TYPE_FROM_CLASS (gobject_class),
456 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
457 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
459 _gtk_marshal_VOID__ENUM,
461 GTK_TYPE_DIRECTION_TYPE);
463 binding_set = gtk_binding_set_by_class (class);
465 add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
466 add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
467 add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
468 add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
470 add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
471 add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
472 add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
473 add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
475 add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
476 add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
477 add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
478 add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
480 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
481 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
483 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
485 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCROLLED_WINDOW_ACCESSIBLE);
489 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
491 GtkScrolledWindowPrivate *priv;
493 scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
494 GTK_TYPE_SCROLLED_WINDOW,
495 GtkScrolledWindowPrivate);
497 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
498 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
500 priv->hscrollbar = NULL;
501 priv->vscrollbar = NULL;
502 priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
503 priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
504 priv->hscrollbar_visible = FALSE;
505 priv->vscrollbar_visible = FALSE;
506 priv->focus_out = FALSE;
507 priv->window_placement = GTK_CORNER_TOP_LEFT;
508 gtk_scrolled_window_update_real_placement (scrolled_window);
509 priv->min_content_width = -1;
510 priv->min_content_height = -1;
514 * gtk_scrolled_window_new:
515 * @hadjustment: (allow-none): horizontal adjustment
516 * @vadjustment: (allow-none): vertical adjustment
518 * Creates a new scrolled window.
520 * The two arguments are the scrolled window's adjustments; these will be
521 * shared with the scrollbars and the child widget to keep the bars in sync
522 * with the child. Usually you want to pass %NULL for the adjustments, which
523 * will cause the scrolled window to create them for you.
525 * Returns: a new scrolled window
528 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
529 GtkAdjustment *vadjustment)
531 GtkWidget *scrolled_window;
534 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
537 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
539 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
540 "hadjustment", hadjustment,
541 "vadjustment", vadjustment,
544 return scrolled_window;
548 * gtk_scrolled_window_set_hadjustment:
549 * @scrolled_window: a #GtkScrolledWindow
550 * @hadjustment: horizontal scroll adjustment
552 * Sets the #GtkAdjustment for the horizontal scrollbar.
555 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
556 GtkAdjustment *hadjustment)
558 GtkScrolledWindowPrivate *priv;
562 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
564 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
566 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
568 bin = GTK_BIN (scrolled_window);
569 priv = scrolled_window->priv;
571 if (!priv->hscrollbar)
573 gtk_widget_push_composite_child ();
574 priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
575 gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
576 gtk_widget_pop_composite_child ();
578 gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
579 g_object_ref (priv->hscrollbar);
580 gtk_widget_show (priv->hscrollbar);
584 GtkAdjustment *old_adjustment;
586 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
587 if (old_adjustment == hadjustment)
590 g_signal_handlers_disconnect_by_func (old_adjustment,
591 gtk_scrolled_window_adjustment_changed,
593 gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
596 hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
597 g_signal_connect (hadjustment,
599 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
601 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
603 child = gtk_bin_get_child (bin);
604 if (GTK_IS_SCROLLABLE (child))
605 gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
607 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
611 * gtk_scrolled_window_set_vadjustment:
612 * @scrolled_window: a #GtkScrolledWindow
613 * @vadjustment: vertical scroll adjustment
615 * Sets the #GtkAdjustment for the vertical scrollbar.
618 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
619 GtkAdjustment *vadjustment)
621 GtkScrolledWindowPrivate *priv;
625 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
627 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
629 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
631 bin = GTK_BIN (scrolled_window);
632 priv = scrolled_window->priv;
634 if (!priv->vscrollbar)
636 gtk_widget_push_composite_child ();
637 priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
638 gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
639 gtk_widget_pop_composite_child ();
641 gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
642 g_object_ref (priv->vscrollbar);
643 gtk_widget_show (priv->vscrollbar);
647 GtkAdjustment *old_adjustment;
649 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
650 if (old_adjustment == vadjustment)
653 g_signal_handlers_disconnect_by_func (old_adjustment,
654 gtk_scrolled_window_adjustment_changed,
656 gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
659 vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
660 g_signal_connect (vadjustment,
662 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
664 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
666 child = gtk_bin_get_child (bin);
667 if (GTK_IS_SCROLLABLE (child))
668 gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
670 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
674 * gtk_scrolled_window_get_hadjustment:
675 * @scrolled_window: a #GtkScrolledWindow
677 * Returns the horizontal scrollbar's adjustment, used to connect the
678 * horizontal scrollbar to the child widget's horizontal scroll
681 * Returns: (transfer none): the horizontal #GtkAdjustment
684 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
686 GtkScrolledWindowPrivate *priv;
688 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
690 priv = scrolled_window->priv;
692 return (priv->hscrollbar ?
693 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
698 * gtk_scrolled_window_get_vadjustment:
699 * @scrolled_window: a #GtkScrolledWindow
701 * Returns the vertical scrollbar's adjustment, used to connect the
702 * vertical scrollbar to the child widget's vertical scroll functionality.
704 * Returns: (transfer none): the vertical #GtkAdjustment
707 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
709 GtkScrolledWindowPrivate *priv;
711 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
713 priv = scrolled_window->priv;
715 return (priv->vscrollbar ?
716 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
721 * gtk_scrolled_window_get_hscrollbar:
722 * @scrolled_window: a #GtkScrolledWindow
724 * Returns the horizontal scrollbar of @scrolled_window.
726 * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
727 * or %NULL if it does not have one.
732 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
734 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
736 return scrolled_window->priv->hscrollbar;
740 * gtk_scrolled_window_get_vscrollbar:
741 * @scrolled_window: a #GtkScrolledWindow
743 * Returns the vertical scrollbar of @scrolled_window.
745 * Returns: (transfer none): the vertical scrollbar of the scrolled window,
746 * or %NULL if it does not have one.
751 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
753 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
755 return scrolled_window->priv->vscrollbar;
759 * gtk_scrolled_window_set_policy:
760 * @scrolled_window: a #GtkScrolledWindow
761 * @hscrollbar_policy: policy for horizontal bar
762 * @vscrollbar_policy: policy for vertical bar
764 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
766 * The policy determines when the scrollbar should appear; it is a value
767 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
768 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
769 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
770 * if needed (that is, if the slider part of the bar would be smaller
771 * than the trough - the display is larger than the page size).
774 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
775 GtkPolicyType hscrollbar_policy,
776 GtkPolicyType vscrollbar_policy)
778 GtkScrolledWindowPrivate *priv;
779 GObject *object = G_OBJECT (scrolled_window);
781 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
783 priv = scrolled_window->priv;
785 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
786 (priv->vscrollbar_policy != vscrollbar_policy))
788 priv->hscrollbar_policy = hscrollbar_policy;
789 priv->vscrollbar_policy = vscrollbar_policy;
791 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
793 g_object_freeze_notify (object);
794 g_object_notify (object, "hscrollbar-policy");
795 g_object_notify (object, "vscrollbar-policy");
796 g_object_thaw_notify (object);
801 * gtk_scrolled_window_get_policy:
802 * @scrolled_window: a #GtkScrolledWindow
803 * @hscrollbar_policy: (out) (allow-none): location to store the policy
804 * for the horizontal scrollbar, or %NULL.
805 * @vscrollbar_policy: (out) (allow-none): location to store the policy
806 * for the vertical scrollbar, or %NULL.
808 * Retrieves the current policy values for the horizontal and vertical
809 * scrollbars. See gtk_scrolled_window_set_policy().
812 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
813 GtkPolicyType *hscrollbar_policy,
814 GtkPolicyType *vscrollbar_policy)
816 GtkScrolledWindowPrivate *priv;
818 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
820 priv = scrolled_window->priv;
822 if (hscrollbar_policy)
823 *hscrollbar_policy = priv->hscrollbar_policy;
824 if (vscrollbar_policy)
825 *vscrollbar_policy = priv->vscrollbar_policy;
829 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
831 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
832 GtkSettings *settings;
834 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
836 if (priv->window_placement_set || settings == NULL)
837 priv->real_window_placement = priv->window_placement;
839 g_object_get (settings,
840 "gtk-scrolled-window-placement",
841 &priv->real_window_placement,
846 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
847 GtkCornerType window_placement)
849 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
851 if (priv->window_placement != window_placement)
853 priv->window_placement = window_placement;
855 gtk_scrolled_window_update_real_placement (scrolled_window);
856 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
858 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
863 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
864 gboolean placement_set,
865 gboolean emit_resize)
867 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
869 if (priv->window_placement_set != placement_set)
871 priv->window_placement_set = placement_set;
873 gtk_scrolled_window_update_real_placement (scrolled_window);
875 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
877 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
882 * gtk_scrolled_window_set_placement:
883 * @scrolled_window: a #GtkScrolledWindow
884 * @window_placement: position of the child window
886 * Sets the placement of the contents with respect to the scrollbars
887 * for the scrolled window.
889 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
890 * in the top left, with the scrollbars underneath and to the right.
891 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
892 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
894 * See also gtk_scrolled_window_get_placement() and
895 * gtk_scrolled_window_unset_placement().
898 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
899 GtkCornerType window_placement)
901 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
903 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
904 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
908 * gtk_scrolled_window_get_placement:
909 * @scrolled_window: a #GtkScrolledWindow
911 * Gets the placement of the contents with respect to the scrollbars
912 * for the scrolled window. See gtk_scrolled_window_set_placement().
914 * Return value: the current placement value.
916 * See also gtk_scrolled_window_set_placement() and
917 * gtk_scrolled_window_unset_placement().
920 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
922 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
924 return scrolled_window->priv->window_placement;
928 * gtk_scrolled_window_unset_placement:
929 * @scrolled_window: a #GtkScrolledWindow
931 * Unsets the placement of the contents with respect to the scrollbars
932 * for the scrolled window. If no window placement is set for a scrolled
933 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
935 * See also gtk_scrolled_window_set_placement() and
936 * gtk_scrolled_window_get_placement().
941 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
943 GtkScrolledWindowPrivate *priv;
945 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
947 priv = scrolled_window->priv;
949 if (priv->window_placement_set)
951 priv->window_placement_set = FALSE;
953 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
955 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
960 * gtk_scrolled_window_set_shadow_type:
961 * @scrolled_window: a #GtkScrolledWindow
962 * @type: kind of shadow to draw around scrolled window contents
964 * Changes the type of shadow drawn around the contents of
969 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
972 GtkScrolledWindowPrivate *priv;
974 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
975 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
977 priv = scrolled_window->priv;
979 if (priv->shadow_type != type)
981 priv->shadow_type = type;
983 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
984 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
986 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
988 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
993 * gtk_scrolled_window_get_shadow_type:
994 * @scrolled_window: a #GtkScrolledWindow
996 * Gets the shadow type of the scrolled window. See
997 * gtk_scrolled_window_set_shadow_type().
999 * Return value: the current shadow type
1002 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
1004 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
1006 return scrolled_window->priv->shadow_type;
1010 gtk_scrolled_window_destroy (GtkWidget *widget)
1012 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1013 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1015 if (priv->hscrollbar)
1017 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1018 gtk_scrolled_window_adjustment_changed,
1020 gtk_widget_unparent (priv->hscrollbar);
1021 gtk_widget_destroy (priv->hscrollbar);
1022 g_object_unref (priv->hscrollbar);
1023 priv->hscrollbar = NULL;
1025 if (priv->vscrollbar)
1027 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1028 gtk_scrolled_window_adjustment_changed,
1030 gtk_widget_unparent (priv->vscrollbar);
1031 gtk_widget_destroy (priv->vscrollbar);
1032 g_object_unref (priv->vscrollbar);
1033 priv->vscrollbar = NULL;
1036 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1040 gtk_scrolled_window_set_property (GObject *object,
1042 const GValue *value,
1045 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1046 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1050 case PROP_HADJUSTMENT:
1051 gtk_scrolled_window_set_hadjustment (scrolled_window,
1052 g_value_get_object (value));
1054 case PROP_VADJUSTMENT:
1055 gtk_scrolled_window_set_vadjustment (scrolled_window,
1056 g_value_get_object (value));
1058 case PROP_HSCROLLBAR_POLICY:
1059 gtk_scrolled_window_set_policy (scrolled_window,
1060 g_value_get_enum (value),
1061 priv->vscrollbar_policy);
1063 case PROP_VSCROLLBAR_POLICY:
1064 gtk_scrolled_window_set_policy (scrolled_window,
1065 priv->hscrollbar_policy,
1066 g_value_get_enum (value));
1068 case PROP_WINDOW_PLACEMENT:
1069 gtk_scrolled_window_set_placement_internal (scrolled_window,
1070 g_value_get_enum (value));
1072 case PROP_WINDOW_PLACEMENT_SET:
1073 gtk_scrolled_window_set_placement_set (scrolled_window,
1074 g_value_get_boolean (value),
1077 case PROP_SHADOW_TYPE:
1078 gtk_scrolled_window_set_shadow_type (scrolled_window,
1079 g_value_get_enum (value));
1081 case PROP_MIN_CONTENT_WIDTH:
1082 gtk_scrolled_window_set_min_content_width (scrolled_window,
1083 g_value_get_int (value));
1085 case PROP_MIN_CONTENT_HEIGHT:
1086 gtk_scrolled_window_set_min_content_height (scrolled_window,
1087 g_value_get_int (value));
1090 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1096 gtk_scrolled_window_get_property (GObject *object,
1101 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1102 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1106 case PROP_HADJUSTMENT:
1107 g_value_set_object (value,
1108 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1110 case PROP_VADJUSTMENT:
1111 g_value_set_object (value,
1112 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1114 case PROP_WINDOW_PLACEMENT:
1115 g_value_set_enum (value, priv->window_placement);
1117 case PROP_WINDOW_PLACEMENT_SET:
1118 g_value_set_boolean (value, priv->window_placement_set);
1120 case PROP_SHADOW_TYPE:
1121 g_value_set_enum (value, priv->shadow_type);
1123 case PROP_HSCROLLBAR_POLICY:
1124 g_value_set_enum (value, priv->hscrollbar_policy);
1126 case PROP_VSCROLLBAR_POLICY:
1127 g_value_set_enum (value, priv->vscrollbar_policy);
1129 case PROP_MIN_CONTENT_WIDTH:
1130 g_value_set_int (value, priv->min_content_width);
1132 case PROP_MIN_CONTENT_HEIGHT:
1133 g_value_set_int (value, priv->min_content_height);
1136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1142 traverse_container (GtkWidget *widget,
1145 if (GTK_IS_SCROLLED_WINDOW (widget))
1147 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1148 gtk_widget_queue_resize (widget);
1150 else if (GTK_IS_CONTAINER (widget))
1151 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1155 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1159 list = gtk_window_list_toplevels ();
1161 for (l = list; l; l = l->next)
1162 gtk_container_forall (GTK_CONTAINER (l->data),
1163 traverse_container, NULL);
1169 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1170 GdkScreen *previous_screen)
1172 GtkSettings *settings;
1173 guint window_placement_connection;
1175 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1177 if (!gtk_widget_has_screen (widget))
1180 settings = gtk_widget_get_settings (widget);
1182 window_placement_connection =
1183 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1184 "gtk-scrolled-window-connection"));
1186 if (window_placement_connection)
1189 window_placement_connection =
1190 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1191 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1192 g_object_set_data (G_OBJECT (settings),
1193 I_("gtk-scrolled-window-connection"),
1194 GUINT_TO_POINTER (window_placement_connection));
1198 gtk_scrolled_window_draw (GtkWidget *widget,
1201 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1202 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1203 GtkStyleContext *context;
1205 context = gtk_widget_get_style_context (widget);
1207 gtk_render_background (context, cr, 0, 0,
1208 gtk_widget_get_allocated_width (widget),
1209 gtk_widget_get_allocated_height (widget));
1211 if (priv->shadow_type != GTK_SHADOW_NONE)
1213 GtkAllocation relative_allocation;
1214 gboolean scrollbars_within_bevel;
1216 gtk_style_context_save (context);
1217 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1219 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1221 if (!scrollbars_within_bevel)
1223 GtkStateFlags state;
1224 GtkBorder padding, border;
1226 state = gtk_widget_get_state_flags (widget);
1227 gtk_style_context_get_padding (context, state, &padding);
1228 gtk_style_context_get_border (context, state, &border);
1230 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1232 relative_allocation.x -= padding.left + border.left;
1233 relative_allocation.y -= padding.top + border.top;
1234 relative_allocation.width += padding.left + padding.right + border.left + border.right;
1235 relative_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1239 relative_allocation.x = 0;
1240 relative_allocation.y = 0;
1241 relative_allocation.width = gtk_widget_get_allocated_width (widget);
1242 relative_allocation.height = gtk_widget_get_allocated_height (widget);
1245 gtk_render_frame (context, cr,
1246 relative_allocation.x,
1247 relative_allocation.y,
1248 relative_allocation.width,
1249 relative_allocation.height);
1251 gtk_style_context_restore (context);
1254 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1260 gtk_scrolled_window_forall (GtkContainer *container,
1261 gboolean include_internals,
1262 GtkCallback callback,
1263 gpointer callback_data)
1265 GtkScrolledWindowPrivate *priv;
1266 GtkScrolledWindow *scrolled_window;
1268 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1269 g_return_if_fail (callback != NULL);
1271 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1275 if (include_internals)
1277 scrolled_window = GTK_SCROLLED_WINDOW (container);
1278 priv = scrolled_window->priv;
1280 if (priv->vscrollbar)
1281 callback (priv->vscrollbar, callback_data);
1282 if (priv->hscrollbar)
1283 callback (priv->hscrollbar, callback_data);
1288 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1289 GtkScrollType scroll,
1290 gboolean horizontal)
1292 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1293 GtkAdjustment *adjustment = NULL;
1297 case GTK_SCROLL_STEP_UP:
1298 scroll = GTK_SCROLL_STEP_BACKWARD;
1301 case GTK_SCROLL_STEP_DOWN:
1302 scroll = GTK_SCROLL_STEP_FORWARD;
1305 case GTK_SCROLL_STEP_LEFT:
1306 scroll = GTK_SCROLL_STEP_BACKWARD;
1309 case GTK_SCROLL_STEP_RIGHT:
1310 scroll = GTK_SCROLL_STEP_FORWARD;
1313 case GTK_SCROLL_PAGE_UP:
1314 scroll = GTK_SCROLL_PAGE_BACKWARD;
1317 case GTK_SCROLL_PAGE_DOWN:
1318 scroll = GTK_SCROLL_PAGE_FORWARD;
1321 case GTK_SCROLL_PAGE_LEFT:
1322 scroll = GTK_SCROLL_STEP_BACKWARD;
1325 case GTK_SCROLL_PAGE_RIGHT:
1326 scroll = GTK_SCROLL_STEP_FORWARD;
1329 case GTK_SCROLL_STEP_BACKWARD:
1330 case GTK_SCROLL_STEP_FORWARD:
1331 case GTK_SCROLL_PAGE_BACKWARD:
1332 case GTK_SCROLL_PAGE_FORWARD:
1333 case GTK_SCROLL_START:
1334 case GTK_SCROLL_END:
1337 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1341 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1342 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1347 if (priv->hscrollbar)
1348 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1352 if (priv->vscrollbar)
1353 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1358 gdouble value = gtk_adjustment_get_value (adjustment);
1362 case GTK_SCROLL_STEP_FORWARD:
1363 value += gtk_adjustment_get_step_increment (adjustment);
1365 case GTK_SCROLL_STEP_BACKWARD:
1366 value -= gtk_adjustment_get_step_increment (adjustment);
1368 case GTK_SCROLL_PAGE_FORWARD:
1369 value += gtk_adjustment_get_page_increment (adjustment);
1371 case GTK_SCROLL_PAGE_BACKWARD:
1372 value -= gtk_adjustment_get_page_increment (adjustment);
1374 case GTK_SCROLL_START:
1375 value = gtk_adjustment_get_lower (adjustment);
1377 case GTK_SCROLL_END:
1378 value = gtk_adjustment_get_upper (adjustment);
1381 g_assert_not_reached ();
1385 gtk_adjustment_set_value (adjustment, value);
1394 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1395 GtkDirectionType direction_type)
1397 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1398 GtkWidget *toplevel;
1400 /* Focus out of the scrolled window entirely. We do this by setting
1401 * a flag, then propagating the focus motion to the notebook.
1403 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1404 if (!gtk_widget_is_toplevel (toplevel))
1407 g_object_ref (scrolled_window);
1409 priv->focus_out = TRUE;
1410 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1411 priv->focus_out = FALSE;
1413 g_object_unref (scrolled_window);
1417 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1418 GtkAllocation *allocation)
1420 GtkAllocation widget_allocation;
1421 GtkScrolledWindow *scrolled_window;
1422 GtkScrolledWindowPrivate *priv;
1427 g_return_if_fail (widget != NULL);
1428 g_return_if_fail (allocation != NULL);
1430 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1431 priv = scrolled_window->priv;
1433 /* Get possible scrollbar dimensions */
1434 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1435 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1436 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1438 /* Subtract some things from our available allocation size */
1442 if (priv->shadow_type != GTK_SHADOW_NONE)
1444 GtkStyleContext *context;
1445 GtkStateFlags state;
1446 GtkBorder padding, border;
1448 context = gtk_widget_get_style_context (widget);
1449 state = gtk_widget_get_state_flags (widget);
1451 gtk_style_context_save (context);
1452 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1454 gtk_style_context_get_border (context, state, &border);
1455 gtk_style_context_get_padding (context, state, &padding);
1457 allocation->x += padding.left + border.left;
1458 allocation->y += padding.top + border.top;
1460 gtk_style_context_restore (context);
1463 gtk_widget_get_allocation (widget, &widget_allocation);
1464 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1465 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1467 if (priv->vscrollbar_visible)
1471 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1474 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1475 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1477 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1478 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1479 allocation->x += (sb_width + sb_spacing);
1481 allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1483 if (priv->hscrollbar_visible)
1486 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1487 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1488 allocation->y += (sb_height + sb_spacing);
1490 allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1495 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1496 GtkAllocation *relative_allocation)
1498 GtkWidget *widget = GTK_WIDGET (swindow), *child;
1499 GtkAllocation allocation;
1500 GtkAllocation child_allocation;
1502 child = gtk_bin_get_child (GTK_BIN (widget));
1504 gtk_widget_get_allocation (widget, &allocation);
1506 gtk_scrolled_window_relative_allocation (widget, relative_allocation);
1507 child_allocation.x = relative_allocation->x + allocation.x;
1508 child_allocation.y = relative_allocation->y + allocation.y;
1509 child_allocation.width = relative_allocation->width;
1510 child_allocation.height = relative_allocation->height;
1512 gtk_widget_size_allocate (child, &child_allocation);
1516 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1517 GtkAllocation *allocation)
1519 GtkScrolledWindow *scrolled_window;
1520 GtkScrolledWindowPrivate *priv;
1521 GtkStyleContext *context;
1522 GtkStateFlags state;
1523 GtkBorder padding, border;
1525 GtkAllocation relative_allocation;
1526 GtkAllocation child_allocation;
1528 gboolean scrollbars_within_bevel;
1533 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1534 g_return_if_fail (allocation != NULL);
1536 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1537 bin = GTK_BIN (scrolled_window);
1538 priv = scrolled_window->priv;
1540 /* Get possible scrollbar dimensions */
1541 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1542 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1543 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1545 context = gtk_widget_get_style_context (widget);
1546 state = gtk_widget_get_state_flags (widget);
1548 gtk_style_context_save (context);
1549 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1551 gtk_style_context_get_padding (context, state, &padding);
1552 gtk_style_context_get_border (context, state, &border);
1554 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1556 gtk_widget_set_allocation (widget, allocation);
1558 gtk_style_context_restore (context);
1560 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1561 priv->hscrollbar_visible = TRUE;
1562 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1563 priv->hscrollbar_visible = FALSE;
1564 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1565 priv->vscrollbar_visible = TRUE;
1566 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1567 priv->vscrollbar_visible = FALSE;
1569 child = gtk_bin_get_child (bin);
1570 if (child && gtk_widget_get_visible (child))
1572 gint child_scroll_width;
1573 gint child_scroll_height;
1574 gboolean previous_hvis;
1575 gboolean previous_vvis;
1578 /* Determine scrollbar visibility first via hfw apis */
1579 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1581 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1582 gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
1584 gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
1586 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1588 /* First try without a vertical scrollbar if the content will fit the height
1589 * given the extra width of the scrollbar */
1590 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1591 gtk_widget_get_preferred_height_for_width (child,
1592 MAX (allocation->width, child_scroll_width),
1593 &child_scroll_height, NULL);
1595 gtk_widget_get_preferred_height_for_width (child,
1596 MAX (allocation->width, child_scroll_width),
1597 NULL, &child_scroll_height);
1599 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1601 /* Does the content height fit the allocation height ? */
1602 priv->vscrollbar_visible = child_scroll_height > allocation->height;
1604 /* Does the content width fit the allocation with minus a possible scrollbar ? */
1605 priv->hscrollbar_visible =
1606 child_scroll_width > allocation->width -
1607 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1609 /* Now that we've guessed the hscrollbar, does the content height fit
1610 * the possible new allocation height ? */
1611 priv->vscrollbar_visible =
1612 child_scroll_height > allocation->height -
1613 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1615 /* Now that we've guessed the vscrollbar, does the content width fit
1616 * the possible new allocation width ? */
1617 priv->hscrollbar_visible =
1618 child_scroll_width > allocation->width -
1619 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1621 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1623 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1624 priv->vscrollbar_visible = child_scroll_height > allocation->height -
1625 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1628 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1630 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1632 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1633 priv->hscrollbar_visible =
1634 child_scroll_width > allocation->width -
1635 (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1637 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1640 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1642 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1643 gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1645 gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1647 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1649 /* First try without a horizontal scrollbar if the content will fit the width
1650 * given the extra height of the scrollbar */
1651 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1652 gtk_widget_get_preferred_width_for_height (child,
1653 MAX (allocation->height, child_scroll_height),
1654 &child_scroll_width, NULL);
1656 gtk_widget_get_preferred_width_for_height (child,
1657 MAX (allocation->height, child_scroll_height),
1658 NULL, &child_scroll_width);
1660 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1662 /* Does the content width fit the allocation width ? */
1663 priv->hscrollbar_visible = child_scroll_width > allocation->width;
1665 /* Does the content height fit the allocation with minus a possible scrollbar ? */
1666 priv->vscrollbar_visible =
1667 child_scroll_height > allocation->height -
1668 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1670 /* Now that we've guessed the vscrollbar, does the content width fit
1671 * the possible new allocation width ? */
1672 priv->hscrollbar_visible =
1673 child_scroll_width > allocation->width -
1674 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1676 /* Now that we've guessed the hscrollbar, does the content height fit
1677 * the possible new allocation height ? */
1678 priv->vscrollbar_visible =
1679 child_scroll_height > allocation->height -
1680 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1682 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1684 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1685 priv->hscrollbar_visible = child_scroll_width > allocation->width -
1686 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1689 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1691 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1693 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1694 priv->vscrollbar_visible =
1695 child_scroll_height > allocation->height -
1696 (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1698 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1702 /* Now after guessing scrollbar visibility; fall back on the allocation loop which
1703 * observes the adjustments to detect scrollbar visibility and also avoids
1704 * infinite recursion
1708 previous_hvis = priv->hscrollbar_visible;
1709 previous_vvis = priv->vscrollbar_visible;
1710 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1712 /* Explicitly force scrollbar visibility checks.
1714 * Since we make a guess above, the child might not decide to update the adjustments
1715 * if they logically did not change since the last configuration
1717 if (priv->hscrollbar)
1718 gtk_scrolled_window_adjustment_changed
1719 (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)), scrolled_window);
1721 if (priv->vscrollbar)
1722 gtk_scrolled_window_adjustment_changed
1723 (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)), scrolled_window);
1725 /* If, after the first iteration, the hscrollbar and the
1726 * vscrollbar flip visiblity... or if one of the scrollbars flip
1727 * on each itteration indefinitly/infinitely, then we just need both
1731 previous_hvis != priv->hscrollbar_visible &&
1732 previous_vvis != priv->vscrollbar_visible) || count > 3)
1734 priv->hscrollbar_visible = TRUE;
1735 priv->vscrollbar_visible = TRUE;
1737 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1744 while (previous_hvis != priv->hscrollbar_visible ||
1745 previous_vvis != priv->vscrollbar_visible);
1749 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1750 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1751 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1754 if (priv->hscrollbar_visible)
1756 if (!gtk_widget_get_visible (priv->hscrollbar))
1757 gtk_widget_show (priv->hscrollbar);
1759 child_allocation.x = relative_allocation.x;
1760 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1761 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1762 child_allocation.y = (relative_allocation.y +
1763 relative_allocation.height +
1765 (priv->shadow_type == GTK_SHADOW_NONE ?
1766 0 : padding.top + border.top));
1768 child_allocation.y = 0;
1770 child_allocation.width = relative_allocation.width;
1771 child_allocation.height = sb_height;
1772 child_allocation.x += allocation->x;
1773 child_allocation.y += allocation->y;
1775 if (priv->shadow_type != GTK_SHADOW_NONE)
1777 if (!scrollbars_within_bevel)
1779 child_allocation.x -= padding.left + border.left;
1780 child_allocation.width += padding.left + padding.right + border.left + border.right;
1782 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1783 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1785 child_allocation.y -= padding.top + border.top;
1789 child_allocation.y += padding.top + border.top;
1793 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1795 else if (gtk_widget_get_visible (priv->hscrollbar))
1796 gtk_widget_hide (priv->hscrollbar);
1798 if (priv->vscrollbar_visible)
1800 if (!gtk_widget_get_visible (priv->vscrollbar))
1801 gtk_widget_show (priv->vscrollbar);
1803 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1804 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1805 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1806 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1807 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1808 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1809 child_allocation.x = (relative_allocation.x +
1810 relative_allocation.width +
1812 (priv->shadow_type == GTK_SHADOW_NONE ?
1813 0 : padding.left + border.left));
1815 child_allocation.x = 0;
1817 child_allocation.y = relative_allocation.y;
1818 child_allocation.width = sb_width;
1819 child_allocation.height = relative_allocation.height;
1820 child_allocation.x += allocation->x;
1821 child_allocation.y += allocation->y;
1823 if (priv->shadow_type != GTK_SHADOW_NONE)
1825 if (!scrollbars_within_bevel)
1827 child_allocation.y -= padding.top + border.top;
1828 child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1830 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1831 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1833 child_allocation.x -= padding.left + border.left;
1837 child_allocation.x += padding.left + border.left;
1841 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1843 else if (gtk_widget_get_visible (priv->vscrollbar))
1844 gtk_widget_hide (priv->vscrollbar);
1848 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1849 GdkEventScroll *event)
1851 GtkScrolledWindowPrivate *priv;
1852 GtkScrolledWindow *scrolled_window;
1855 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1856 g_return_val_if_fail (event != NULL, FALSE);
1858 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1859 priv = scrolled_window->priv;
1861 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1862 range = priv->vscrollbar;
1864 range = priv->hscrollbar;
1866 if (range && gtk_widget_get_visible (range))
1868 GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (range));
1871 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1873 gtk_adjustment_set_value (adjustment, gtk_adjustment_get_value (adjustment) + delta);
1882 gtk_scrolled_window_focus (GtkWidget *widget,
1883 GtkDirectionType direction)
1885 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1886 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1888 gboolean had_focus_child;
1890 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1892 if (priv->focus_out)
1894 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1898 if (gtk_widget_is_focus (widget))
1901 /* We only put the scrolled window itself in the focus chain if it
1902 * isn't possible to focus any children.
1904 child = gtk_bin_get_child (GTK_BIN (widget));
1907 if (gtk_widget_child_focus (child, direction))
1911 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1913 gtk_widget_grab_focus (widget);
1921 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1924 GtkScrolledWindowPrivate *priv;
1925 GtkScrolledWindow *scrolled_window;
1927 g_return_if_fail (adjustment != NULL);
1928 g_return_if_fail (data != NULL);
1930 scrolled_window = GTK_SCROLLED_WINDOW (data);
1931 priv = scrolled_window->priv;
1933 if (priv->hscrollbar &&
1934 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1936 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1940 visible = priv->hscrollbar_visible;
1941 priv->hscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1942 gtk_adjustment_get_page_size (adjustment));
1944 if (priv->hscrollbar_visible != visible)
1945 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1948 else if (priv->vscrollbar &&
1949 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1951 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1955 visible = priv->vscrollbar_visible;
1956 priv->vscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1957 gtk_adjustment_get_page_size (adjustment));
1959 if (priv->vscrollbar_visible != visible)
1960 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1966 gtk_scrolled_window_add (GtkContainer *container,
1969 GtkScrolledWindowPrivate *priv;
1970 GtkScrolledWindow *scrolled_window;
1972 GtkWidget *child_widget;
1973 GtkAdjustment *hadj, *vadj;
1975 bin = GTK_BIN (container);
1976 child_widget = gtk_bin_get_child (bin);
1977 g_return_if_fail (child_widget == NULL);
1979 scrolled_window = GTK_SCROLLED_WINDOW (container);
1980 priv = scrolled_window->priv;
1982 _gtk_bin_set_child (bin, child);
1983 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1985 hadj = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1986 vadj = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1988 if (GTK_IS_SCROLLABLE (child))
1989 g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1991 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1992 "use gtk_scrolled_window_add_with_viewport() instead");
1996 gtk_scrolled_window_remove (GtkContainer *container,
1999 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
2000 g_return_if_fail (child != NULL);
2001 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
2003 g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
2005 /* chain parent class handler to remove child */
2006 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
2010 * gtk_scrolled_window_add_with_viewport:
2011 * @scrolled_window: a #GtkScrolledWindow
2012 * @child: the widget you want to scroll
2014 * Used to add children without native scrolling capabilities. This
2015 * is simply a convenience function; it is equivalent to adding the
2016 * unscrollable child to a viewport, then adding the viewport to the
2017 * scrolled window. If a child has native scrolling, use
2018 * gtk_container_add() instead of this function.
2020 * The viewport scrolls the child by moving its #GdkWindow, and takes
2021 * the size of the child to be the size of its toplevel #GdkWindow.
2022 * This will be very wrong for most widgets that support native scrolling;
2023 * for example, if you add a widget such as #GtkTreeView with a viewport,
2024 * the whole widget will scroll, including the column headings. Thus,
2025 * widgets with native scrolling support should not be used with the
2026 * #GtkViewport proxy.
2028 * A widget supports scrolling natively if it implements the
2029 * #GtkScrollable interface.
2032 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
2036 GtkWidget *viewport;
2037 GtkWidget *child_widget;
2039 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2040 g_return_if_fail (GTK_IS_WIDGET (child));
2041 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
2043 bin = GTK_BIN (scrolled_window);
2044 child_widget = gtk_bin_get_child (bin);
2048 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2049 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2051 viewport = child_widget;
2056 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2057 gtk_scrolled_window_get_vadjustment (scrolled_window));
2058 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2061 gtk_widget_show (viewport);
2062 gtk_container_add (GTK_CONTAINER (viewport), child);
2066 * _gtk_scrolled_window_get_spacing:
2067 * @scrolled_window: a scrolled window
2069 * Gets the spacing between the scrolled window's scrollbars and
2070 * the scrolled widget. Used by GtkCombo
2072 * Return value: the spacing, in pixels.
2075 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2077 GtkScrolledWindowClass *class;
2079 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2081 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2083 if (class->scrollbar_spacing >= 0)
2084 return class->scrollbar_spacing;
2087 gint scrollbar_spacing;
2089 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2090 "scrollbar-spacing", &scrollbar_spacing,
2093 return scrollbar_spacing;
2099 gtk_scrolled_window_get_preferred_size (GtkWidget *widget,
2100 GtkOrientation orientation,
2104 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2105 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2106 GtkBin *bin = GTK_BIN (scrolled_window);
2109 gint scrollbar_spacing;
2110 GtkRequisition hscrollbar_requisition;
2111 GtkRequisition vscrollbar_requisition;
2112 GtkRequisition minimum_req, natural_req;
2114 gint min_child_size, nat_child_size;
2116 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2120 minimum_req.width = 0;
2121 minimum_req.height = 0;
2122 natural_req.width = 0;
2123 natural_req.height = 0;
2125 gtk_widget_get_preferred_size (priv->hscrollbar,
2126 &hscrollbar_requisition, NULL);
2127 gtk_widget_get_preferred_size (priv->vscrollbar,
2128 &vscrollbar_requisition, NULL);
2130 child = gtk_bin_get_child (bin);
2131 if (child && gtk_widget_get_visible (child))
2133 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2135 gtk_widget_get_preferred_width (child,
2139 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2141 minimum_req.width += min_child_size;
2142 natural_req.width += nat_child_size;
2146 gint min_content_width = priv->min_content_width;
2148 if (min_content_width >= 0)
2150 minimum_req.width = MAX (minimum_req.width, min_content_width);
2151 natural_req.width = MAX (natural_req.width, min_content_width);
2156 minimum_req.width += vscrollbar_requisition.width;
2157 natural_req.width += vscrollbar_requisition.width;
2161 else /* GTK_ORIENTATION_VERTICAL */
2163 gtk_widget_get_preferred_height (child,
2167 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2169 minimum_req.height += min_child_size;
2170 natural_req.height += nat_child_size;
2174 gint min_content_height = priv->min_content_height;
2176 if (min_content_height >= 0)
2178 minimum_req.height = MAX (minimum_req.height, min_content_height);
2179 natural_req.height = MAX (natural_req.height, min_content_height);
2184 minimum_req.height += vscrollbar_requisition.height;
2185 natural_req.height += vscrollbar_requisition.height;
2191 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2192 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2194 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2195 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2196 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2197 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2200 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2201 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2203 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2204 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2205 if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2206 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2209 minimum_req.width += MAX (0, extra_width);
2210 minimum_req.height += MAX (0, extra_height);
2211 natural_req.width += MAX (0, extra_width);
2212 natural_req.height += MAX (0, extra_height);
2214 if (priv->shadow_type != GTK_SHADOW_NONE)
2216 GtkStyleContext *context;
2217 GtkStateFlags state;
2218 GtkBorder padding, border;
2220 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
2221 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
2223 gtk_style_context_save (context);
2224 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
2226 gtk_style_context_get_padding (context, state, &padding);
2227 gtk_style_context_get_border (context, state, &border);
2229 minimum_req.width += padding.left + padding.right + border.left + border.right;
2230 minimum_req.height += padding.top + padding.bottom + border.top + border.bottom;
2231 natural_req.width += padding.left + padding.right + border.left + border.right;
2232 natural_req.height += padding.top + padding.bottom + border.top + border.bottom;
2234 gtk_style_context_restore (context);
2237 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2240 *minimum_size = minimum_req.width;
2242 *natural_size = natural_req.width;
2247 *minimum_size = minimum_req.height;
2249 *natural_size = natural_req.height;
2254 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2258 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2262 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2266 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2270 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2272 gint *minimum_height,
2273 gint *natural_height)
2275 g_return_if_fail (GTK_IS_WIDGET (widget));
2277 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2281 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2283 gint *minimum_width,
2284 gint *natural_width)
2286 g_return_if_fail (GTK_IS_WIDGET (widget));
2288 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2292 * gtk_scrolled_window_get_min_content_width:
2293 * @scrolled_window: a #GtkScrolledWindow
2295 * Gets the minimum content width of @scrolled_window, or -1 if not set.
2297 * Returns: the minimum content width
2302 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2304 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2306 return scrolled_window->priv->min_content_width;
2310 * gtk_scrolled_window_set_min_content_width:
2311 * @scrolled_window: a #GtkScrolledWindow
2312 * @width: the minimal content width
2314 * Sets the minimum width that @scrolled_window should keep visible.
2315 * Note that this can and (usually will) be smaller than the minimum
2316 * size of the content.
2321 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2324 GtkScrolledWindowPrivate *priv;
2326 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2328 priv = scrolled_window->priv;
2330 if (priv->min_content_width != width)
2332 priv->min_content_width = width;
2334 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2336 g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2341 * gtk_scrolled_window_get_min_content_height:
2342 * @scrolled_window: a #GtkScrolledWindow
2344 * Gets the minimal content height of @scrolled_window, or -1 if not set.
2346 * Returns: the minimal content height
2351 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2353 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2355 return scrolled_window->priv->min_content_height;
2359 * gtk_scrolled_window_set_min_content_height:
2360 * @scrolled_window: a #GtkScrolledWindow
2361 * @height: the minimal content height
2363 * Sets the minimum height that @scrolled_window should keep visible.
2364 * Note that this can and (usually will) be smaller than the minimum
2365 * size of the content.
2370 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2373 GtkScrolledWindowPrivate *priv;
2375 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2377 priv = scrolled_window->priv;
2379 if (priv->min_content_height != height)
2381 priv->min_content_height = height;
2383 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2385 g_object_notify (G_OBJECT (scrolled_window), "min-content-height");