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 "gtkscrolledwindow.h"
35 #include "gtkwindow.h"
36 #include "gtkprivate.h"
41 * SECTION:gtkscrolledwindow
42 * @Short_description: Adds scrollbars to its child widget
43 * @Title: GtkScrolledWindow
44 * @See_also: #GtkScrollable, #GtkViewport, #GtkAdjustment
46 * #GtkScrolledWindow is a #GtkBin subclass: it's a container
47 * the accepts a single child widget. #GtkScrolledWindow adds scrollbars
48 * to the child widget and optionally draws a beveled frame around the
51 * The scrolled window can work in two ways. Some widgets have native
52 * scrolling support; these widgets implement the #GtkScrollable interface.
53 * Widgets with native scroll support include #GtkTreeView, #GtkTextView,
56 * For widgets that lack native scrolling support, the #GtkViewport
57 * widget acts as an adaptor class, implementing scrollability for child
58 * widgets that lack their own scrolling capabilities. Use #GtkViewport
59 * to scroll child widgets such as #GtkTable, #GtkBox, and so on.
61 * If a widget has native scrolling abilities, it can be added to the
62 * #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
63 * must first add the widget to a #GtkViewport, then add the #GtkViewport
64 * to the scrolled window. The convenience function
65 * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
66 * ignore the presence of the viewport.
68 * The position of the scrollbars is controlled by the scroll
69 * adjustments. See #GtkAdjustment for the fields in an adjustment - for
70 * #GtkScrollbar, used by #GtkScrolledWindow, the "value" field
71 * represents the position of the scrollbar, which must be between the
72 * "lower" field and "upper - page_size." The "page_size" field
73 * represents the size of the visible scrollable area. The
74 * "step_increment" and "page_increment" fields are used when the user
75 * asks to step down (using the small stepper arrows) or page down (using
76 * for example the PageDown key).
78 * If a #GtkScrolledWindow doesn't behave quite as you would like, or
79 * doesn't have exactly the right layout, it's very possible to set up
80 * your own scrolling with #GtkScrollbar and for example a #GtkTable.
84 /* scrolled window policy and size requisition handling:
86 * gtk size requisition works as follows:
87 * a widget upon size-request reports the width and height that it finds
88 * to be best suited to display its contents, including children.
89 * the width and/or height reported from a widget upon size requisition
90 * may be overidden by the user by specifying a width and/or height
91 * other than 0 through gtk_widget_set_size_request().
93 * a scrolled window needs (for implementing all three policy types) to
94 * request its width and height based on two different rationales.
95 * 1) the user wants the scrolled window to just fit into the space
96 * that it gets allocated for a specifc dimension.
97 * 1.1) this does not apply if the user specified a concrete value
98 * value for that specific dimension by either specifying usize for the
99 * scrolled window or for its child.
100 * 2) the user wants the scrolled window to take as much space up as
101 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
103 * also, kinda obvious:
104 * 3) a user would certainly not have choosen a scrolled window as a container
105 * for the child, if the resulting allocation takes up more space than the
106 * child would have allocated without the scrolled window.
109 * A) from 1) follows: the scrolled window shouldn't request more space for a
110 * specifc dimension than is required at minimum.
111 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
112 * window (done automatically) or by usize of the child (needs to be checked).
113 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
115 * D) from 3) follows: the scrolled window child's minimum width and minimum height
116 * under A) at least correspond to the space taken up by its scrollbars.
119 #define DEFAULT_SCROLLBAR_SPACING 3
121 struct _GtkScrolledWindowPrivate
123 GtkCornerType real_window_placement;
124 GtkWidget *hscrollbar;
125 GtkWidget *vscrollbar;
127 gboolean window_placement_set;
131 guint hscrollbar_policy : 2;
132 guint vscrollbar_policy : 2;
133 guint hscrollbar_visible : 1;
134 guint vscrollbar_visible : 1;
135 guint window_placement : 2;
136 guint focus_out : 1; /* Flag used by ::move-focus-out implementation */
138 gint min_content_width;
139 gint min_content_height;
147 PROP_HSCROLLBAR_POLICY,
148 PROP_VSCROLLBAR_POLICY,
149 PROP_WINDOW_PLACEMENT,
150 PROP_WINDOW_PLACEMENT_SET,
152 PROP_MIN_CONTENT_WIDTH,
153 PROP_MIN_CONTENT_HEIGHT
164 static void gtk_scrolled_window_set_property (GObject *object,
168 static void gtk_scrolled_window_get_property (GObject *object,
173 static void gtk_scrolled_window_destroy (GtkWidget *widget);
174 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
175 GdkScreen *previous_screen);
176 static gboolean gtk_scrolled_window_draw (GtkWidget *widget,
178 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
179 GtkAllocation *allocation);
180 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
181 GdkEventScroll *event);
182 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
183 GtkDirectionType direction);
184 static void gtk_scrolled_window_add (GtkContainer *container,
186 static void gtk_scrolled_window_remove (GtkContainer *container,
188 static void gtk_scrolled_window_forall (GtkContainer *container,
189 gboolean include_internals,
190 GtkCallback callback,
191 gpointer callback_data);
192 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
193 GtkScrollType scroll,
194 gboolean horizontal);
195 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
196 GtkDirectionType direction_type);
198 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
199 GtkAllocation *allocation);
200 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
203 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
205 static void gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
208 static void gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
211 static void gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *layout,
213 gint *minimum_height,
214 gint *natural_height);
215 static void gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *layout,
217 gint *minimum_height,
218 gint *natural_height);
220 static guint signals[LAST_SIGNAL] = {0};
222 G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
226 add_scroll_binding (GtkBindingSet *binding_set,
228 GdkModifierType mask,
229 GtkScrollType scroll,
232 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
234 gtk_binding_entry_add_signal (binding_set, keyval, mask,
236 GTK_TYPE_SCROLL_TYPE, scroll,
237 G_TYPE_BOOLEAN, horizontal);
238 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
240 GTK_TYPE_SCROLL_TYPE, scroll,
241 G_TYPE_BOOLEAN, horizontal);
245 add_tab_bindings (GtkBindingSet *binding_set,
246 GdkModifierType modifiers,
247 GtkDirectionType direction)
249 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
251 GTK_TYPE_DIRECTION_TYPE, direction);
252 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
254 GTK_TYPE_DIRECTION_TYPE, direction);
258 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
260 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
261 GtkWidgetClass *widget_class;
262 GtkContainerClass *container_class;
263 GtkBindingSet *binding_set;
265 widget_class = (GtkWidgetClass*) class;
266 container_class = (GtkContainerClass*) class;
268 gobject_class->set_property = gtk_scrolled_window_set_property;
269 gobject_class->get_property = gtk_scrolled_window_get_property;
271 widget_class->destroy = gtk_scrolled_window_destroy;
272 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
273 widget_class->draw = gtk_scrolled_window_draw;
274 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
275 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
276 widget_class->focus = gtk_scrolled_window_focus;
277 widget_class->get_preferred_width = gtk_scrolled_window_get_preferred_width;
278 widget_class->get_preferred_height = gtk_scrolled_window_get_preferred_height;
279 widget_class->get_preferred_height_for_width = gtk_scrolled_window_get_preferred_height_for_width;
280 widget_class->get_preferred_width_for_height = gtk_scrolled_window_get_preferred_width_for_height;
282 container_class->add = gtk_scrolled_window_add;
283 container_class->remove = gtk_scrolled_window_remove;
284 container_class->forall = gtk_scrolled_window_forall;
285 gtk_container_class_handle_border_width (container_class);
287 class->scrollbar_spacing = -1;
289 class->scroll_child = gtk_scrolled_window_scroll_child;
290 class->move_focus_out = gtk_scrolled_window_move_focus_out;
292 g_object_class_install_property (gobject_class,
294 g_param_spec_object ("hadjustment",
295 P_("Horizontal Adjustment"),
296 P_("The GtkAdjustment for the horizontal position"),
298 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
299 g_object_class_install_property (gobject_class,
301 g_param_spec_object ("vadjustment",
302 P_("Vertical Adjustment"),
303 P_("The GtkAdjustment for the vertical position"),
305 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
306 g_object_class_install_property (gobject_class,
307 PROP_HSCROLLBAR_POLICY,
308 g_param_spec_enum ("hscrollbar-policy",
309 P_("Horizontal Scrollbar Policy"),
310 P_("When the horizontal scrollbar is displayed"),
311 GTK_TYPE_POLICY_TYPE,
312 GTK_POLICY_AUTOMATIC,
313 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
314 g_object_class_install_property (gobject_class,
315 PROP_VSCROLLBAR_POLICY,
316 g_param_spec_enum ("vscrollbar-policy",
317 P_("Vertical Scrollbar Policy"),
318 P_("When the vertical scrollbar is displayed"),
319 GTK_TYPE_POLICY_TYPE,
320 GTK_POLICY_AUTOMATIC,
321 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
323 g_object_class_install_property (gobject_class,
324 PROP_WINDOW_PLACEMENT,
325 g_param_spec_enum ("window-placement",
326 P_("Window Placement"),
327 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
328 GTK_TYPE_CORNER_TYPE,
330 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
333 * GtkScrolledWindow:window-placement-set:
335 * Whether "window-placement" should be used to determine the location
336 * of the contents with respect to the scrollbars. Otherwise, the
337 * "gtk-scrolled-window-placement" setting is used.
341 g_object_class_install_property (gobject_class,
342 PROP_WINDOW_PLACEMENT_SET,
343 g_param_spec_boolean ("window-placement-set",
344 P_("Window Placement Set"),
345 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
347 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
348 g_object_class_install_property (gobject_class,
350 g_param_spec_enum ("shadow-type",
352 P_("Style of bevel around the contents"),
353 GTK_TYPE_SHADOW_TYPE,
355 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
358 * GtkScrolledWindow:scrollbars-within-bevel:
360 * Whether to place scrollbars within the scrolled window's bevel.
364 gtk_widget_class_install_style_property (widget_class,
365 g_param_spec_boolean ("scrollbars-within-bevel",
366 P_("Scrollbars within bevel"),
367 P_("Place scrollbars within the scrolled window's bevel"),
369 GTK_PARAM_READABLE));
371 gtk_widget_class_install_style_property (widget_class,
372 g_param_spec_int ("scrollbar-spacing",
373 P_("Scrollbar spacing"),
374 P_("Number of pixels between the scrollbars and the scrolled window"),
377 DEFAULT_SCROLLBAR_SPACING,
378 GTK_PARAM_READABLE));
380 g_object_class_install_property (gobject_class,
381 PROP_MIN_CONTENT_WIDTH,
382 g_param_spec_int ("min-content-width",
383 P_("Minimum Content Width"),
384 P_("The minimum width that the scrolled window will allocate to its content"),
386 GTK_PARAM_READWRITE));
387 g_object_class_install_property (gobject_class,
388 PROP_MIN_CONTENT_HEIGHT,
389 g_param_spec_int ("min-content-height",
390 P_("Minimum Content Height"),
391 P_("The minimum height that the scrolled window will allocate to its content"),
393 GTK_PARAM_READWRITE));
395 * GtkScrolledWindow::scroll-child:
396 * @scrolled_window: a #GtkScrolledWindow
397 * @scroll: a #GtkScrollType describing how much to scroll
398 * @horizontal: whether the keybinding scrolls the child
399 * horizontally or not
401 * The ::scroll-child signal is a
402 * <link linkend="keybinding-signals">keybinding signal</link>
403 * which gets emitted when a keybinding that scrolls is pressed.
404 * The horizontal or vertical adjustment is updated which triggers a
405 * signal that the scrolled windows child may listen to and scroll itself.
407 signals[SCROLL_CHILD] =
408 g_signal_new (I_("scroll-child"),
409 G_TYPE_FROM_CLASS (gobject_class),
410 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
411 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
413 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
415 GTK_TYPE_SCROLL_TYPE,
419 * GtkScrolledWindow::move-focus-out:
420 * @scrolled_window: a #GtkScrolledWindow
421 * @direction_type: either %GTK_DIR_TAB_FORWARD or
422 * %GTK_DIR_TAB_BACKWARD
424 * The ::move-focus-out signal is a
425 * <link linkend="keybinding-signals">keybinding signal</link>
426 * which gets emitted when focus is moved away from the scrolled
427 * window by a keybinding.
428 * The #GtkWidget::move-focus signal is emitted with @direction_type
429 * on this scrolled windows toplevel parent in the container hierarchy.
430 * The default bindings for this signal are
431 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap></keycombo>
433 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap><keycap>Shift</keycap></keycombo>.
435 signals[MOVE_FOCUS_OUT] =
436 g_signal_new (I_("move-focus-out"),
437 G_TYPE_FROM_CLASS (gobject_class),
438 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
439 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
441 _gtk_marshal_VOID__ENUM,
443 GTK_TYPE_DIRECTION_TYPE);
445 binding_set = gtk_binding_set_by_class (class);
447 add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
448 add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
449 add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
450 add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
452 add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
453 add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
454 add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
455 add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
457 add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
458 add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
459 add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
460 add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
462 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
463 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
465 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
469 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
471 GtkScrolledWindowPrivate *priv;
473 scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
474 GTK_TYPE_SCROLLED_WINDOW,
475 GtkScrolledWindowPrivate);
477 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
478 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
480 priv->hscrollbar = NULL;
481 priv->vscrollbar = NULL;
482 priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
483 priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
484 priv->hscrollbar_visible = FALSE;
485 priv->vscrollbar_visible = FALSE;
486 priv->focus_out = FALSE;
487 priv->window_placement = GTK_CORNER_TOP_LEFT;
488 gtk_scrolled_window_update_real_placement (scrolled_window);
489 priv->min_content_width = -1;
490 priv->min_content_height = -1;
494 * gtk_scrolled_window_new:
495 * @hadjustment: (allow-none): horizontal adjustment
496 * @vadjustment: (allow-none): vertical adjustment
498 * Creates a new scrolled window.
500 * The two arguments are the scrolled window's adjustments; these will be
501 * shared with the scrollbars and the child widget to keep the bars in sync
502 * with the child. Usually you want to pass %NULL for the adjustments, which
503 * will cause the scrolled window to create them for you.
505 * Returns: a new scrolled window
508 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
509 GtkAdjustment *vadjustment)
511 GtkWidget *scrolled_window;
514 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
517 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
519 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
520 "hadjustment", hadjustment,
521 "vadjustment", vadjustment,
524 return scrolled_window;
528 * gtk_scrolled_window_set_hadjustment:
529 * @scrolled_window: a #GtkScrolledWindow
530 * @hadjustment: horizontal scroll adjustment
532 * Sets the #GtkAdjustment for the horizontal scrollbar.
535 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
536 GtkAdjustment *hadjustment)
538 GtkScrolledWindowPrivate *priv;
542 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
544 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
546 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
548 bin = GTK_BIN (scrolled_window);
549 priv = scrolled_window->priv;
551 if (!priv->hscrollbar)
553 gtk_widget_push_composite_child ();
554 priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
555 gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
556 gtk_widget_pop_composite_child ();
558 gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
559 g_object_ref (priv->hscrollbar);
560 gtk_widget_show (priv->hscrollbar);
564 GtkAdjustment *old_adjustment;
566 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
567 if (old_adjustment == hadjustment)
570 g_signal_handlers_disconnect_by_func (old_adjustment,
571 gtk_scrolled_window_adjustment_changed,
573 gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
576 hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
577 g_signal_connect (hadjustment,
579 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
581 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
583 child = gtk_bin_get_child (bin);
584 if (GTK_IS_SCROLLABLE (child))
585 gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
587 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
591 * gtk_scrolled_window_set_vadjustment:
592 * @scrolled_window: a #GtkScrolledWindow
593 * @vadjustment: vertical scroll adjustment
595 * Sets the #GtkAdjustment for the vertical scrollbar.
598 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
599 GtkAdjustment *vadjustment)
601 GtkScrolledWindowPrivate *priv;
605 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
607 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
609 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
611 bin = GTK_BIN (scrolled_window);
612 priv = scrolled_window->priv;
614 if (!priv->vscrollbar)
616 gtk_widget_push_composite_child ();
617 priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
618 gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
619 gtk_widget_pop_composite_child ();
621 gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
622 g_object_ref (priv->vscrollbar);
623 gtk_widget_show (priv->vscrollbar);
627 GtkAdjustment *old_adjustment;
629 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
630 if (old_adjustment == vadjustment)
633 g_signal_handlers_disconnect_by_func (old_adjustment,
634 gtk_scrolled_window_adjustment_changed,
636 gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
639 vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
640 g_signal_connect (vadjustment,
642 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
644 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
646 child = gtk_bin_get_child (bin);
647 if (GTK_IS_SCROLLABLE (child))
648 gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
650 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
654 * gtk_scrolled_window_get_hadjustment:
655 * @scrolled_window: a #GtkScrolledWindow
657 * Returns the horizontal scrollbar's adjustment, used to connect the
658 * horizontal scrollbar to the child widget's horizontal scroll
661 * Returns: (transfer none): the horizontal #GtkAdjustment
664 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
666 GtkScrolledWindowPrivate *priv;
668 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
670 priv = scrolled_window->priv;
672 return (priv->hscrollbar ?
673 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
678 * gtk_scrolled_window_get_vadjustment:
679 * @scrolled_window: a #GtkScrolledWindow
681 * Returns the vertical scrollbar's adjustment, used to connect the
682 * vertical scrollbar to the child widget's vertical scroll functionality.
684 * Returns: (transfer none): the vertical #GtkAdjustment
687 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
689 GtkScrolledWindowPrivate *priv;
691 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
693 priv = scrolled_window->priv;
695 return (priv->vscrollbar ?
696 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
701 * gtk_scrolled_window_get_hscrollbar:
702 * @scrolled_window: a #GtkScrolledWindow
704 * Returns the horizontal scrollbar of @scrolled_window.
706 * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
707 * or %NULL if it does not have one.
712 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
714 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
716 return scrolled_window->priv->hscrollbar;
720 * gtk_scrolled_window_get_vscrollbar:
721 * @scrolled_window: a #GtkScrolledWindow
723 * Returns the vertical scrollbar of @scrolled_window.
725 * Returns: (transfer none): the vertical scrollbar of the scrolled window,
726 * or %NULL if it does not have one.
731 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
733 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
735 return scrolled_window->priv->vscrollbar;
739 * gtk_scrolled_window_set_policy:
740 * @scrolled_window: a #GtkScrolledWindow
741 * @hscrollbar_policy: policy for horizontal bar
742 * @vscrollbar_policy: policy for vertical bar
744 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
746 * The policy determines when the scrollbar should appear; it is a value
747 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
748 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
749 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
750 * if needed (that is, if the slider part of the bar would be smaller
751 * than the trough - the display is larger than the page size).
754 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
755 GtkPolicyType hscrollbar_policy,
756 GtkPolicyType vscrollbar_policy)
758 GtkScrolledWindowPrivate *priv;
759 GObject *object = G_OBJECT (scrolled_window);
761 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
763 priv = scrolled_window->priv;
765 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
766 (priv->vscrollbar_policy != vscrollbar_policy))
768 priv->hscrollbar_policy = hscrollbar_policy;
769 priv->vscrollbar_policy = vscrollbar_policy;
771 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
773 g_object_freeze_notify (object);
774 g_object_notify (object, "hscrollbar-policy");
775 g_object_notify (object, "vscrollbar-policy");
776 g_object_thaw_notify (object);
781 * gtk_scrolled_window_get_policy:
782 * @scrolled_window: a #GtkScrolledWindow
783 * @hscrollbar_policy: location to store the policy for the horizontal
784 * scrollbar, or %NULL.
785 * @vscrollbar_policy: location to store the policy for the vertical
786 * scrollbar, or %NULL.
788 * Retrieves the current policy values for the horizontal and vertical
789 * scrollbars. See gtk_scrolled_window_set_policy().
792 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
793 GtkPolicyType *hscrollbar_policy,
794 GtkPolicyType *vscrollbar_policy)
796 GtkScrolledWindowPrivate *priv;
798 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
800 priv = scrolled_window->priv;
802 if (hscrollbar_policy)
803 *hscrollbar_policy = priv->hscrollbar_policy;
804 if (vscrollbar_policy)
805 *vscrollbar_policy = priv->vscrollbar_policy;
809 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
811 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
812 GtkSettings *settings;
814 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
816 if (priv->window_placement_set || settings == NULL)
817 priv->real_window_placement = priv->window_placement;
819 g_object_get (settings,
820 "gtk-scrolled-window-placement",
821 &priv->real_window_placement,
826 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
827 GtkCornerType window_placement)
829 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
831 if (priv->window_placement != window_placement)
833 priv->window_placement = window_placement;
835 gtk_scrolled_window_update_real_placement (scrolled_window);
836 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
838 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
843 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
844 gboolean placement_set,
845 gboolean emit_resize)
847 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
849 if (priv->window_placement_set != placement_set)
851 priv->window_placement_set = placement_set;
853 gtk_scrolled_window_update_real_placement (scrolled_window);
855 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
857 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
862 * gtk_scrolled_window_set_placement:
863 * @scrolled_window: a #GtkScrolledWindow
864 * @window_placement: position of the child window
866 * Sets the placement of the contents with respect to the scrollbars
867 * for the scrolled window.
869 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
870 * in the top left, with the scrollbars underneath and to the right.
871 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
872 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
874 * See also gtk_scrolled_window_get_placement() and
875 * gtk_scrolled_window_unset_placement().
878 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
879 GtkCornerType window_placement)
881 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
883 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
884 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
888 * gtk_scrolled_window_get_placement:
889 * @scrolled_window: a #GtkScrolledWindow
891 * Gets the placement of the contents with respect to the scrollbars
892 * for the scrolled window. See gtk_scrolled_window_set_placement().
894 * Return value: the current placement value.
896 * See also gtk_scrolled_window_set_placement() and
897 * gtk_scrolled_window_unset_placement().
900 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
902 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
904 return scrolled_window->priv->window_placement;
908 * gtk_scrolled_window_unset_placement:
909 * @scrolled_window: a #GtkScrolledWindow
911 * Unsets the placement of the contents with respect to the scrollbars
912 * for the scrolled window. If no window placement is set for a scrolled
913 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
915 * See also gtk_scrolled_window_set_placement() and
916 * gtk_scrolled_window_get_placement().
921 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
923 GtkScrolledWindowPrivate *priv;
925 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
927 priv = scrolled_window->priv;
929 if (priv->window_placement_set)
931 priv->window_placement_set = FALSE;
933 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
935 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
940 * gtk_scrolled_window_set_shadow_type:
941 * @scrolled_window: a #GtkScrolledWindow
942 * @type: kind of shadow to draw around scrolled window contents
944 * Changes the type of shadow drawn around the contents of
949 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
952 GtkScrolledWindowPrivate *priv;
954 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
955 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
957 priv = scrolled_window->priv;
959 if (priv->shadow_type != type)
961 priv->shadow_type = type;
963 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
964 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
966 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
968 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
973 * gtk_scrolled_window_get_shadow_type:
974 * @scrolled_window: a #GtkScrolledWindow
976 * Gets the shadow type of the scrolled window. See
977 * gtk_scrolled_window_set_shadow_type().
979 * Return value: the current shadow type
982 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
984 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
986 return scrolled_window->priv->shadow_type;
990 gtk_scrolled_window_destroy (GtkWidget *widget)
992 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
993 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
995 if (priv->hscrollbar)
997 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
998 gtk_scrolled_window_adjustment_changed,
1000 gtk_widget_unparent (priv->hscrollbar);
1001 gtk_widget_destroy (priv->hscrollbar);
1002 g_object_unref (priv->hscrollbar);
1003 priv->hscrollbar = NULL;
1005 if (priv->vscrollbar)
1007 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1008 gtk_scrolled_window_adjustment_changed,
1010 gtk_widget_unparent (priv->vscrollbar);
1011 gtk_widget_destroy (priv->vscrollbar);
1012 g_object_unref (priv->vscrollbar);
1013 priv->vscrollbar = NULL;
1016 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1020 gtk_scrolled_window_set_property (GObject *object,
1022 const GValue *value,
1025 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1026 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1030 case PROP_HADJUSTMENT:
1031 gtk_scrolled_window_set_hadjustment (scrolled_window,
1032 g_value_get_object (value));
1034 case PROP_VADJUSTMENT:
1035 gtk_scrolled_window_set_vadjustment (scrolled_window,
1036 g_value_get_object (value));
1038 case PROP_HSCROLLBAR_POLICY:
1039 gtk_scrolled_window_set_policy (scrolled_window,
1040 g_value_get_enum (value),
1041 priv->vscrollbar_policy);
1043 case PROP_VSCROLLBAR_POLICY:
1044 gtk_scrolled_window_set_policy (scrolled_window,
1045 priv->hscrollbar_policy,
1046 g_value_get_enum (value));
1048 case PROP_WINDOW_PLACEMENT:
1049 gtk_scrolled_window_set_placement_internal (scrolled_window,
1050 g_value_get_enum (value));
1052 case PROP_WINDOW_PLACEMENT_SET:
1053 gtk_scrolled_window_set_placement_set (scrolled_window,
1054 g_value_get_boolean (value),
1057 case PROP_SHADOW_TYPE:
1058 gtk_scrolled_window_set_shadow_type (scrolled_window,
1059 g_value_get_enum (value));
1061 case PROP_MIN_CONTENT_WIDTH:
1062 gtk_scrolled_window_set_min_content_width (scrolled_window,
1063 g_value_get_int (value));
1065 case PROP_MIN_CONTENT_HEIGHT:
1066 gtk_scrolled_window_set_min_content_height (scrolled_window,
1067 g_value_get_int (value));
1070 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1076 gtk_scrolled_window_get_property (GObject *object,
1081 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1082 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1086 case PROP_HADJUSTMENT:
1087 g_value_set_object (value,
1088 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1090 case PROP_VADJUSTMENT:
1091 g_value_set_object (value,
1092 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1094 case PROP_WINDOW_PLACEMENT:
1095 g_value_set_enum (value, priv->window_placement);
1097 case PROP_WINDOW_PLACEMENT_SET:
1098 g_value_set_boolean (value, priv->window_placement_set);
1100 case PROP_SHADOW_TYPE:
1101 g_value_set_enum (value, priv->shadow_type);
1103 case PROP_HSCROLLBAR_POLICY:
1104 g_value_set_enum (value, priv->hscrollbar_policy);
1106 case PROP_VSCROLLBAR_POLICY:
1107 g_value_set_enum (value, priv->vscrollbar_policy);
1109 case PROP_MIN_CONTENT_WIDTH:
1110 g_value_set_int (value, priv->min_content_width);
1112 case PROP_MIN_CONTENT_HEIGHT:
1113 g_value_set_int (value, priv->min_content_height);
1116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1122 traverse_container (GtkWidget *widget,
1125 if (GTK_IS_SCROLLED_WINDOW (widget))
1127 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1128 gtk_widget_queue_resize (widget);
1130 else if (GTK_IS_CONTAINER (widget))
1131 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1135 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1139 list = gtk_window_list_toplevels ();
1141 for (l = list; l; l = l->next)
1142 gtk_container_forall (GTK_CONTAINER (l->data),
1143 traverse_container, NULL);
1149 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1150 GdkScreen *previous_screen)
1152 GtkSettings *settings;
1153 guint window_placement_connection;
1155 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1157 if (!gtk_widget_has_screen (widget))
1160 settings = gtk_widget_get_settings (widget);
1162 window_placement_connection =
1163 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1164 "gtk-scrolled-window-connection"));
1166 if (window_placement_connection)
1169 window_placement_connection =
1170 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1171 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1172 g_object_set_data (G_OBJECT (settings),
1173 I_("gtk-scrolled-window-connection"),
1174 GUINT_TO_POINTER (window_placement_connection));
1178 gtk_scrolled_window_draw (GtkWidget *widget,
1181 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1182 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1184 if (priv->shadow_type != GTK_SHADOW_NONE)
1186 GtkAllocation relative_allocation;
1187 GtkStyleContext *context;
1188 gboolean scrollbars_within_bevel;
1190 context = gtk_widget_get_style_context (widget);
1192 gtk_style_context_save (context);
1193 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1195 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1197 if (!scrollbars_within_bevel)
1199 GtkStateFlags state;
1200 GtkBorder padding, border;
1202 state = gtk_widget_get_state_flags (widget);
1203 gtk_style_context_get_padding (context, state, &padding);
1204 gtk_style_context_get_border (context, state, &border);
1206 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1208 relative_allocation.x -= padding.left + border.left;
1209 relative_allocation.y -= padding.top + border.top;
1210 relative_allocation.width += padding.left + padding.right + border.left + border.right;
1211 relative_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1215 relative_allocation.x = 0;
1216 relative_allocation.y = 0;
1217 relative_allocation.width = gtk_widget_get_allocated_width (widget);
1218 relative_allocation.height = gtk_widget_get_allocated_height (widget);
1221 gtk_render_frame (context, cr,
1222 relative_allocation.x,
1223 relative_allocation.y,
1224 relative_allocation.width,
1225 relative_allocation.height);
1227 gtk_style_context_restore (context);
1230 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1236 gtk_scrolled_window_forall (GtkContainer *container,
1237 gboolean include_internals,
1238 GtkCallback callback,
1239 gpointer callback_data)
1241 GtkScrolledWindowPrivate *priv;
1242 GtkScrolledWindow *scrolled_window;
1244 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1245 g_return_if_fail (callback != NULL);
1247 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1251 if (include_internals)
1253 scrolled_window = GTK_SCROLLED_WINDOW (container);
1254 priv = scrolled_window->priv;
1256 if (priv->vscrollbar)
1257 callback (priv->vscrollbar, callback_data);
1258 if (priv->hscrollbar)
1259 callback (priv->hscrollbar, callback_data);
1264 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1265 GtkScrollType scroll,
1266 gboolean horizontal)
1268 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1269 GtkAdjustment *adjustment = NULL;
1273 case GTK_SCROLL_STEP_UP:
1274 scroll = GTK_SCROLL_STEP_BACKWARD;
1277 case GTK_SCROLL_STEP_DOWN:
1278 scroll = GTK_SCROLL_STEP_FORWARD;
1281 case GTK_SCROLL_STEP_LEFT:
1282 scroll = GTK_SCROLL_STEP_BACKWARD;
1285 case GTK_SCROLL_STEP_RIGHT:
1286 scroll = GTK_SCROLL_STEP_FORWARD;
1289 case GTK_SCROLL_PAGE_UP:
1290 scroll = GTK_SCROLL_PAGE_BACKWARD;
1293 case GTK_SCROLL_PAGE_DOWN:
1294 scroll = GTK_SCROLL_PAGE_FORWARD;
1297 case GTK_SCROLL_PAGE_LEFT:
1298 scroll = GTK_SCROLL_STEP_BACKWARD;
1301 case GTK_SCROLL_PAGE_RIGHT:
1302 scroll = GTK_SCROLL_STEP_FORWARD;
1305 case GTK_SCROLL_STEP_BACKWARD:
1306 case GTK_SCROLL_STEP_FORWARD:
1307 case GTK_SCROLL_PAGE_BACKWARD:
1308 case GTK_SCROLL_PAGE_FORWARD:
1309 case GTK_SCROLL_START:
1310 case GTK_SCROLL_END:
1313 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1317 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1318 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1323 if (priv->hscrollbar)
1324 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1328 if (priv->vscrollbar)
1329 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1334 gdouble value = adjustment->value;
1338 case GTK_SCROLL_STEP_FORWARD:
1339 value += adjustment->step_increment;
1341 case GTK_SCROLL_STEP_BACKWARD:
1342 value -= adjustment->step_increment;
1344 case GTK_SCROLL_PAGE_FORWARD:
1345 value += adjustment->page_increment;
1347 case GTK_SCROLL_PAGE_BACKWARD:
1348 value -= adjustment->page_increment;
1350 case GTK_SCROLL_START:
1351 value = adjustment->lower;
1353 case GTK_SCROLL_END:
1354 value = adjustment->upper;
1357 g_assert_not_reached ();
1361 gtk_adjustment_set_value (adjustment, value);
1370 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1371 GtkDirectionType direction_type)
1373 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1374 GtkWidget *toplevel;
1376 /* Focus out of the scrolled window entirely. We do this by setting
1377 * a flag, then propagating the focus motion to the notebook.
1379 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1380 if (!gtk_widget_is_toplevel (toplevel))
1383 g_object_ref (scrolled_window);
1385 priv->focus_out = TRUE;
1386 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1387 priv->focus_out = FALSE;
1389 g_object_unref (scrolled_window);
1393 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1394 GtkAllocation *allocation)
1396 GtkAllocation widget_allocation;
1397 GtkScrolledWindow *scrolled_window;
1398 GtkScrolledWindowPrivate *priv;
1403 g_return_if_fail (widget != NULL);
1404 g_return_if_fail (allocation != NULL);
1406 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1407 priv = scrolled_window->priv;
1409 /* Get possible scrollbar dimensions */
1410 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1411 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1412 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1414 /* Subtract some things from our available allocation size */
1418 if (priv->shadow_type != GTK_SHADOW_NONE)
1420 GtkStyleContext *context;
1421 GtkStateFlags state;
1422 GtkBorder padding, border;
1424 context = gtk_widget_get_style_context (widget);
1425 state = gtk_widget_get_state_flags (widget);
1427 gtk_style_context_save (context);
1428 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1430 gtk_style_context_get_border (context, state, &border);
1431 gtk_style_context_get_padding (context, state, &padding);
1433 allocation->x += padding.left + border.left;
1434 allocation->y += padding.top + border.top;
1436 gtk_style_context_restore (context);
1439 gtk_widget_get_allocation (widget, &widget_allocation);
1440 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1441 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1443 if (priv->vscrollbar_visible)
1447 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1450 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1451 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1453 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1454 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1455 allocation->x += (sb_width + sb_spacing);
1457 allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1459 if (priv->hscrollbar_visible)
1462 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1463 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1464 allocation->y += (sb_height + sb_spacing);
1466 allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1471 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1472 GtkAllocation *relative_allocation)
1474 GtkWidget *widget = GTK_WIDGET (swindow), *child;
1475 GtkAllocation allocation;
1476 GtkAllocation child_allocation;
1478 child = gtk_bin_get_child (GTK_BIN (widget));
1480 gtk_widget_get_allocation (widget, &allocation);
1482 gtk_scrolled_window_relative_allocation (widget, relative_allocation);
1483 child_allocation.x = relative_allocation->x + allocation.x;
1484 child_allocation.y = relative_allocation->y + allocation.y;
1485 child_allocation.width = relative_allocation->width;
1486 child_allocation.height = relative_allocation->height;
1488 gtk_widget_size_allocate (child, &child_allocation);
1492 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1493 GtkAllocation *allocation)
1495 GtkScrolledWindow *scrolled_window;
1496 GtkScrolledWindowPrivate *priv;
1497 GtkStyleContext *context;
1498 GtkStateFlags state;
1499 GtkBorder padding, border;
1501 GtkAllocation relative_allocation;
1502 GtkAllocation child_allocation;
1504 gboolean scrollbars_within_bevel;
1509 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1510 g_return_if_fail (allocation != NULL);
1512 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1513 bin = GTK_BIN (scrolled_window);
1514 priv = scrolled_window->priv;
1516 /* Get possible scrollbar dimensions */
1517 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1518 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1519 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1521 context = gtk_widget_get_style_context (widget);
1522 state = gtk_widget_get_state_flags (widget);
1524 gtk_style_context_save (context);
1525 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1527 gtk_style_context_get_padding (context, state, &padding);
1528 gtk_style_context_get_border (context, state, &border);
1530 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1532 gtk_widget_set_allocation (widget, allocation);
1534 gtk_style_context_restore (context);
1536 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1537 priv->hscrollbar_visible = TRUE;
1538 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1539 priv->hscrollbar_visible = FALSE;
1540 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1541 priv->vscrollbar_visible = TRUE;
1542 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1543 priv->vscrollbar_visible = FALSE;
1545 child = gtk_bin_get_child (bin);
1546 if (child && gtk_widget_get_visible (child))
1548 gint child_scroll_width;
1549 gint child_scroll_height;
1550 gboolean previous_hvis;
1551 gboolean previous_vvis;
1554 /* Determine scrollbar visibility first via hfw apis */
1555 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1557 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1558 gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
1560 gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
1562 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1564 /* First try without a vertical scrollbar if the content will fit the height
1565 * given the extra width of the scrollbar */
1566 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1567 gtk_widget_get_preferred_height_for_width (child,
1568 MAX (allocation->width, child_scroll_width),
1569 &child_scroll_height, NULL);
1571 gtk_widget_get_preferred_height_for_width (child,
1572 MAX (allocation->width, child_scroll_width),
1573 NULL, &child_scroll_height);
1575 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1577 /* Does the content height fit the allocation height ? */
1578 priv->vscrollbar_visible = child_scroll_height > allocation->height;
1580 /* Does the content width fit the allocation with minus a possible scrollbar ? */
1581 priv->hscrollbar_visible =
1582 child_scroll_width > allocation->width -
1583 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1585 /* Now that we've guessed the hscrollbar, does the content height fit
1586 * the possible new allocation height ? */
1587 priv->vscrollbar_visible =
1588 child_scroll_height > allocation->height -
1589 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1591 /* Now that we've guessed the vscrollbar, does the content width fit
1592 * the possible new allocation width ? */
1593 priv->hscrollbar_visible =
1594 child_scroll_width > allocation->width -
1595 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1597 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1599 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1600 priv->vscrollbar_visible = child_scroll_height > allocation->height -
1601 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1604 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1606 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1608 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1609 priv->hscrollbar_visible =
1610 child_scroll_width > allocation->width -
1611 (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1613 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1616 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1618 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1619 gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1621 gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1623 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1625 /* First try without a horizontal scrollbar if the content will fit the width
1626 * given the extra height of the scrollbar */
1627 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1628 gtk_widget_get_preferred_width_for_height (child,
1629 MAX (allocation->height, child_scroll_height),
1630 &child_scroll_width, NULL);
1632 gtk_widget_get_preferred_width_for_height (child,
1633 MAX (allocation->height, child_scroll_height),
1634 NULL, &child_scroll_width);
1636 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1638 /* Does the content width fit the allocation width ? */
1639 priv->hscrollbar_visible = child_scroll_width > allocation->width;
1641 /* Does the content height fit the allocation with minus a possible scrollbar ? */
1642 priv->vscrollbar_visible =
1643 child_scroll_height > allocation->height -
1644 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1646 /* Now that we've guessed the vscrollbar, does the content width fit
1647 * the possible new allocation width ? */
1648 priv->hscrollbar_visible =
1649 child_scroll_width > allocation->width -
1650 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1652 /* Now that we've guessed the hscrollbar, does the content height fit
1653 * the possible new allocation height ? */
1654 priv->vscrollbar_visible =
1655 child_scroll_height > allocation->height -
1656 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1658 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1660 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1661 priv->hscrollbar_visible = child_scroll_width > allocation->width -
1662 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1665 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1667 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1669 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1670 priv->vscrollbar_visible =
1671 child_scroll_height > allocation->height -
1672 (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1674 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1678 /* Now after guessing scrollbar visibility; fall back on the allocation loop which
1679 * observes the adjustments to detect scrollbar visibility and also avoids
1680 * infinite recursion
1684 previous_hvis = priv->hscrollbar_visible;
1685 previous_vvis = priv->vscrollbar_visible;
1686 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1688 /* Explicitly force scrollbar visibility checks.
1690 * Since we make a guess above, the child might not decide to update the adjustments
1691 * if they logically did not change since the last configuration
1693 if (priv->hscrollbar)
1694 gtk_scrolled_window_adjustment_changed
1695 (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)), scrolled_window);
1697 if (priv->vscrollbar)
1698 gtk_scrolled_window_adjustment_changed
1699 (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)), scrolled_window);
1701 /* If, after the first iteration, the hscrollbar and the
1702 * vscrollbar flip visiblity... or if one of the scrollbars flip
1703 * on each itteration indefinitly/infinitely, then we just need both
1707 previous_hvis != priv->hscrollbar_visible &&
1708 previous_vvis != priv->vscrollbar_visible) || count > 3)
1710 priv->hscrollbar_visible = TRUE;
1711 priv->vscrollbar_visible = TRUE;
1713 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1720 while (previous_hvis != priv->hscrollbar_visible ||
1721 previous_vvis != priv->vscrollbar_visible);
1725 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1726 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1727 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1730 if (priv->hscrollbar_visible)
1732 if (!gtk_widget_get_visible (priv->hscrollbar))
1733 gtk_widget_show (priv->hscrollbar);
1735 child_allocation.x = relative_allocation.x;
1736 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1737 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1738 child_allocation.y = (relative_allocation.y +
1739 relative_allocation.height +
1741 (priv->shadow_type == GTK_SHADOW_NONE ?
1742 0 : padding.top + border.top));
1744 child_allocation.y = 0;
1746 child_allocation.width = relative_allocation.width;
1747 child_allocation.height = sb_height;
1748 child_allocation.x += allocation->x;
1749 child_allocation.y += allocation->y;
1751 if (priv->shadow_type != GTK_SHADOW_NONE)
1753 if (!scrollbars_within_bevel)
1755 child_allocation.x -= padding.left + border.left;
1756 child_allocation.width += padding.left + padding.right + border.left + border.right;
1758 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1759 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1761 child_allocation.y -= padding.top + border.top;
1765 child_allocation.y += padding.top + border.top;
1769 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1771 else if (gtk_widget_get_visible (priv->hscrollbar))
1772 gtk_widget_hide (priv->hscrollbar);
1774 if (priv->vscrollbar_visible)
1776 if (!gtk_widget_get_visible (priv->vscrollbar))
1777 gtk_widget_show (priv->vscrollbar);
1779 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1780 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1781 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1782 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1783 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1784 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1785 child_allocation.x = (relative_allocation.x +
1786 relative_allocation.width +
1788 (priv->shadow_type == GTK_SHADOW_NONE ?
1789 0 : padding.left + border.left));
1791 child_allocation.x = 0;
1793 child_allocation.y = relative_allocation.y;
1794 child_allocation.width = sb_width;
1795 child_allocation.height = relative_allocation.height;
1796 child_allocation.x += allocation->x;
1797 child_allocation.y += allocation->y;
1799 if (priv->shadow_type != GTK_SHADOW_NONE)
1801 if (!scrollbars_within_bevel)
1803 child_allocation.y -= padding.top + border.top;
1804 child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1806 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1807 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1809 child_allocation.x -= padding.left + border.left;
1813 child_allocation.x += padding.left + border.left;
1817 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1819 else if (gtk_widget_get_visible (priv->vscrollbar))
1820 gtk_widget_hide (priv->vscrollbar);
1824 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1825 GdkEventScroll *event)
1827 GtkScrolledWindowPrivate *priv;
1828 GtkScrolledWindow *scrolled_window;
1831 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1832 g_return_val_if_fail (event != NULL, FALSE);
1834 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1835 priv = scrolled_window->priv;
1837 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1838 range = priv->vscrollbar;
1840 range = priv->hscrollbar;
1842 if (range && gtk_widget_get_visible (range))
1844 GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE (range));
1847 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1849 gtk_adjustment_set_value (adj, adj->value + delta);
1858 gtk_scrolled_window_focus (GtkWidget *widget,
1859 GtkDirectionType direction)
1861 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1862 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1864 gboolean had_focus_child;
1866 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1868 if (priv->focus_out)
1870 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1874 if (gtk_widget_is_focus (widget))
1877 /* We only put the scrolled window itself in the focus chain if it
1878 * isn't possible to focus any children.
1880 child = gtk_bin_get_child (GTK_BIN (widget));
1883 if (gtk_widget_child_focus (child, direction))
1887 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1889 gtk_widget_grab_focus (widget);
1897 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1900 GtkScrolledWindowPrivate *priv;
1901 GtkScrolledWindow *scrolled_window;
1903 g_return_if_fail (adjustment != NULL);
1904 g_return_if_fail (data != NULL);
1906 scrolled_window = GTK_SCROLLED_WINDOW (data);
1907 priv = scrolled_window->priv;
1909 if (priv->hscrollbar &&
1910 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1912 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1916 visible = priv->hscrollbar_visible;
1917 priv->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1918 adjustment->page_size);
1920 if (priv->hscrollbar_visible != visible)
1921 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1924 else if (priv->vscrollbar &&
1925 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1927 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1931 visible = priv->vscrollbar_visible;
1932 priv->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1933 adjustment->page_size);
1935 if (priv->vscrollbar_visible != visible)
1936 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1942 gtk_scrolled_window_add (GtkContainer *container,
1945 GtkScrolledWindowPrivate *priv;
1946 GtkScrolledWindow *scrolled_window;
1948 GtkWidget *child_widget;
1949 GtkAdjustment *hadj, *vadj;
1951 bin = GTK_BIN (container);
1952 child_widget = gtk_bin_get_child (bin);
1953 g_return_if_fail (child_widget == NULL);
1955 scrolled_window = GTK_SCROLLED_WINDOW (container);
1956 priv = scrolled_window->priv;
1958 _gtk_bin_set_child (bin, child);
1959 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1961 hadj = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->priv->hscrollbar));
1962 vadj = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->priv->vscrollbar));
1964 if (GTK_IS_SCROLLABLE (child))
1965 g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1967 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1968 "use gtk_scrolled_window_add_with_viewport() instead");
1972 gtk_scrolled_window_remove (GtkContainer *container,
1975 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1976 g_return_if_fail (child != NULL);
1977 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1979 g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
1981 /* chain parent class handler to remove child */
1982 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1986 * gtk_scrolled_window_add_with_viewport:
1987 * @scrolled_window: a #GtkScrolledWindow
1988 * @child: the widget you want to scroll
1990 * Used to add children without native scrolling capabilities. This
1991 * is simply a convenience function; it is equivalent to adding the
1992 * unscrollable child to a viewport, then adding the viewport to the
1993 * scrolled window. If a child has native scrolling, use
1994 * gtk_container_add() instead of this function.
1996 * The viewport scrolls the child by moving its #GdkWindow, and takes
1997 * the size of the child to be the size of its toplevel #GdkWindow.
1998 * This will be very wrong for most widgets that support native scrolling;
1999 * for example, if you add a widget such as #GtkTreeView with a viewport,
2000 * the whole widget will scroll, including the column headings. Thus,
2001 * widgets with native scrolling support should not be used with the
2002 * #GtkViewport proxy.
2004 * A widget supports scrolling natively if it implements the
2005 * #GtkScrollable interface.
2008 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
2012 GtkWidget *viewport;
2013 GtkWidget *child_widget;
2015 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2016 g_return_if_fail (GTK_IS_WIDGET (child));
2017 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
2019 bin = GTK_BIN (scrolled_window);
2020 child_widget = gtk_bin_get_child (bin);
2024 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2025 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2027 viewport = child_widget;
2032 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2033 gtk_scrolled_window_get_vadjustment (scrolled_window));
2034 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2037 gtk_widget_show (viewport);
2038 gtk_container_add (GTK_CONTAINER (viewport), child);
2042 * _gtk_scrolled_window_get_spacing:
2043 * @scrolled_window: a scrolled window
2045 * Gets the spacing between the scrolled window's scrollbars and
2046 * the scrolled widget. Used by GtkCombo
2048 * Return value: the spacing, in pixels.
2051 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2053 GtkScrolledWindowClass *class;
2055 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2057 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2059 if (class->scrollbar_spacing >= 0)
2060 return class->scrollbar_spacing;
2063 gint scrollbar_spacing;
2065 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2066 "scrollbar-spacing", &scrollbar_spacing,
2069 return scrollbar_spacing;
2075 gtk_scrolled_window_get_preferred_size (GtkWidget *widget,
2076 GtkOrientation orientation,
2080 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2081 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2082 GtkBin *bin = GTK_BIN (scrolled_window);
2085 gint scrollbar_spacing;
2086 GtkRequisition hscrollbar_requisition;
2087 GtkRequisition vscrollbar_requisition;
2088 GtkRequisition minimum_req, natural_req;
2090 gint min_child_size, nat_child_size;
2092 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2096 minimum_req.width = 0;
2097 minimum_req.height = 0;
2098 natural_req.width = 0;
2099 natural_req.height = 0;
2101 gtk_widget_get_preferred_size (priv->hscrollbar,
2102 &hscrollbar_requisition, NULL);
2103 gtk_widget_get_preferred_size (priv->vscrollbar,
2104 &vscrollbar_requisition, NULL);
2106 child = gtk_bin_get_child (bin);
2107 if (child && gtk_widget_get_visible (child))
2109 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2111 gtk_widget_get_preferred_width (child,
2115 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2117 minimum_req.width += min_child_size;
2118 natural_req.width += nat_child_size;
2122 gint min_content_width = priv->min_content_width;
2124 if (min_content_width >= 0)
2126 minimum_req.width = MAX (minimum_req.width, min_content_width);
2127 natural_req.width = MAX (natural_req.width, min_content_width);
2132 minimum_req.width += vscrollbar_requisition.width;
2133 natural_req.width += vscrollbar_requisition.width;
2137 else /* GTK_ORIENTATION_VERTICAL */
2139 gtk_widget_get_preferred_height (child,
2143 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2145 minimum_req.height += min_child_size;
2146 natural_req.height += nat_child_size;
2150 gint min_content_height = priv->min_content_height;
2152 if (min_content_height >= 0)
2154 minimum_req.height = MAX (minimum_req.height, min_content_height);
2155 natural_req.height = MAX (natural_req.height, min_content_height);
2160 minimum_req.height += vscrollbar_requisition.height;
2161 natural_req.height += vscrollbar_requisition.height;
2167 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2168 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2170 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2171 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2172 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2173 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2176 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2177 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2179 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2180 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2181 if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2182 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2185 minimum_req.width += MAX (0, extra_width);
2186 minimum_req.height += MAX (0, extra_height);
2187 natural_req.width += MAX (0, extra_width);
2188 natural_req.height += MAX (0, extra_height);
2190 if (priv->shadow_type != GTK_SHADOW_NONE)
2192 GtkStyleContext *context;
2193 GtkStateFlags state;
2194 GtkBorder padding, border;
2196 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
2197 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
2199 gtk_style_context_save (context);
2200 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
2202 gtk_style_context_get_padding (context, state, &padding);
2203 gtk_style_context_get_border (context, state, &border);
2205 minimum_req.width += padding.left + padding.right + border.left + border.right;
2206 minimum_req.height += padding.top + padding.bottom + border.top + border.bottom;
2207 natural_req.width += padding.left + padding.right + border.left + border.right;
2208 natural_req.height += padding.top + padding.bottom + border.top + border.bottom;
2210 gtk_style_context_restore (context);
2213 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2216 *minimum_size = minimum_req.width;
2218 *natural_size = natural_req.width;
2223 *minimum_size = minimum_req.height;
2225 *natural_size = natural_req.height;
2230 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2234 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2238 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2242 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2246 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2248 gint *minimum_height,
2249 gint *natural_height)
2251 g_return_if_fail (GTK_IS_WIDGET (widget));
2253 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2257 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2259 gint *minimum_width,
2260 gint *natural_width)
2262 g_return_if_fail (GTK_IS_WIDGET (widget));
2264 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2268 * gtk_scrolled_window_get_min_content_width:
2269 * @scrolled_window: a #GtkScrolledWindow
2271 * Gets the minimum content width of @scrolled_window, or -1 if not set.
2273 * Returns: the minimum content width
2278 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2280 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2282 return scrolled_window->priv->min_content_width;
2286 * gtk_scrolled_window_set_min_content_width:
2287 * @scrolled_window: a #GtkScrolledWindow
2288 * @width: the minimal content width
2290 * Sets the minimum width that @scrolled_window should keep visible.
2291 * Note that this can and (usually will) be smaller than the minimum
2292 * size of the content.
2297 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2300 GtkScrolledWindowPrivate *priv;
2302 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2304 priv = scrolled_window->priv;
2306 if (priv->min_content_width != width)
2308 priv->min_content_width = width;
2310 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2312 g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2317 * gtk_scrolled_window_get_min_content_height:
2318 * @scrolled_window: a #GtkScrolledWindow
2320 * Gets the minimal content height of @scrolled_window, or -1 if not set.
2322 * Returns: the minimal content height
2327 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2329 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2331 return scrolled_window->priv->min_content_height;
2335 * gtk_scrolled_window_set_min_content_height:
2336 * @scrolled_window: a #GtkScrolledWindow
2337 * @height: the minimal content height
2339 * Sets the minimum height that @scrolled_window should keep visible.
2340 * Note that this can and (usually will) be smaller than the minimum
2341 * size of the content.
2346 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2349 GtkScrolledWindowPrivate *priv;
2351 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2353 priv = scrolled_window->priv;
2355 if (priv->min_content_height != height)
2357 priv->min_content_height = height;
2359 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2361 g_object_notify (G_OBJECT (scrolled_window), "min-content-height");