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"
37 #include "gtktypebuiltins.h"
42 * SECTION:gtkscrolledwindow
43 * @Short_description: Adds scrollbars to its child widget
44 * @Title: GtkScrolledWindow
45 * @See_also: #GtkScrollable, #GtkViewport, #GtkAdjustment
47 * #GtkScrolledWindow is a #GtkBin subclass: it's a container
48 * the accepts a single child widget. #GtkScrolledWindow adds scrollbars
49 * to the child widget and optionally draws a beveled frame around the
52 * The scrolled window can work in two ways. Some widgets have native
53 * scrolling support; these widgets implement the #GtkScrollable interface.
54 * Widgets with native scroll support include #GtkTreeView, #GtkTextView,
57 * For widgets that lack native scrolling support, the #GtkViewport
58 * widget acts as an adaptor class, implementing scrollability for child
59 * widgets that lack their own scrolling capabilities. Use #GtkViewport
60 * to scroll child widgets such as #GtkTable, #GtkBox, and so on.
62 * If a widget has native scrolling abilities, it can be added to the
63 * #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
64 * must first add the widget to a #GtkViewport, then add the #GtkViewport
65 * to the scrolled window. The convenience function
66 * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
67 * ignore the presence of the viewport.
69 * The position of the scrollbars is controlled by the scroll
70 * adjustments. See #GtkAdjustment for the fields in an adjustment - for
71 * #GtkScrollbar, used by #GtkScrolledWindow, the "value" field
72 * represents the position of the scrollbar, which must be between the
73 * "lower" field and "upper - page_size." The "page_size" field
74 * represents the size of the visible scrollable area. The
75 * "step_increment" and "page_increment" fields are used when the user
76 * asks to step down (using the small stepper arrows) or page down (using
77 * for example the PageDown key).
79 * If a #GtkScrolledWindow doesn't behave quite as you would like, or
80 * doesn't have exactly the right layout, it's very possible to set up
81 * your own scrolling with #GtkScrollbar and for example a #GtkTable.
85 /* scrolled window policy and size requisition handling:
87 * gtk size requisition works as follows:
88 * a widget upon size-request reports the width and height that it finds
89 * to be best suited to display its contents, including children.
90 * the width and/or height reported from a widget upon size requisition
91 * may be overidden by the user by specifying a width and/or height
92 * other than 0 through gtk_widget_set_size_request().
94 * a scrolled window needs (for implementing all three policy types) to
95 * request its width and height based on two different rationales.
96 * 1) the user wants the scrolled window to just fit into the space
97 * that it gets allocated for a specifc dimension.
98 * 1.1) this does not apply if the user specified a concrete value
99 * value for that specific dimension by either specifying usize for the
100 * scrolled window or for its child.
101 * 2) the user wants the scrolled window to take as much space up as
102 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
104 * also, kinda obvious:
105 * 3) a user would certainly not have choosen a scrolled window as a container
106 * for the child, if the resulting allocation takes up more space than the
107 * child would have allocated without the scrolled window.
110 * A) from 1) follows: the scrolled window shouldn't request more space for a
111 * specifc dimension than is required at minimum.
112 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
113 * window (done automatically) or by usize of the child (needs to be checked).
114 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
116 * D) from 3) follows: the scrolled window child's minimum width and minimum height
117 * under A) at least correspond to the space taken up by its scrollbars.
120 #define DEFAULT_SCROLLBAR_SPACING 3
122 struct _GtkScrolledWindowPrivate
124 GtkCornerType real_window_placement;
125 GtkWidget *hscrollbar;
126 GtkWidget *vscrollbar;
128 gboolean window_placement_set;
132 guint hscrollbar_policy : 2;
133 guint vscrollbar_policy : 2;
134 guint hscrollbar_visible : 1;
135 guint vscrollbar_visible : 1;
136 guint window_placement : 2;
137 guint focus_out : 1; /* Flag used by ::move-focus-out implementation */
139 gint min_content_width;
140 gint min_content_height;
148 PROP_HSCROLLBAR_POLICY,
149 PROP_VSCROLLBAR_POLICY,
150 PROP_WINDOW_PLACEMENT,
151 PROP_WINDOW_PLACEMENT_SET,
153 PROP_MIN_CONTENT_WIDTH,
154 PROP_MIN_CONTENT_HEIGHT
165 static void gtk_scrolled_window_set_property (GObject *object,
169 static void gtk_scrolled_window_get_property (GObject *object,
174 static void gtk_scrolled_window_destroy (GtkWidget *widget);
175 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
176 GdkScreen *previous_screen);
177 static gboolean gtk_scrolled_window_draw (GtkWidget *widget,
179 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
180 GtkAllocation *allocation);
181 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
182 GdkEventScroll *event);
183 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
184 GtkDirectionType direction);
185 static void gtk_scrolled_window_add (GtkContainer *container,
187 static void gtk_scrolled_window_remove (GtkContainer *container,
189 static void gtk_scrolled_window_forall (GtkContainer *container,
190 gboolean include_internals,
191 GtkCallback callback,
192 gpointer callback_data);
193 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
194 GtkScrollType scroll,
195 gboolean horizontal);
196 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
197 GtkDirectionType direction_type);
199 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
200 GtkAllocation *allocation);
201 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
204 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
206 static void gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
209 static void gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
212 static void gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *layout,
214 gint *minimum_height,
215 gint *natural_height);
216 static void gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *layout,
218 gint *minimum_height,
219 gint *natural_height);
221 static guint signals[LAST_SIGNAL] = {0};
223 G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
227 add_scroll_binding (GtkBindingSet *binding_set,
229 GdkModifierType mask,
230 GtkScrollType scroll,
233 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
235 gtk_binding_entry_add_signal (binding_set, keyval, mask,
237 GTK_TYPE_SCROLL_TYPE, scroll,
238 G_TYPE_BOOLEAN, horizontal);
239 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
241 GTK_TYPE_SCROLL_TYPE, scroll,
242 G_TYPE_BOOLEAN, horizontal);
246 add_tab_bindings (GtkBindingSet *binding_set,
247 GdkModifierType modifiers,
248 GtkDirectionType direction)
250 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
252 GTK_TYPE_DIRECTION_TYPE, direction);
253 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
255 GTK_TYPE_DIRECTION_TYPE, direction);
259 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
261 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
262 GtkWidgetClass *widget_class;
263 GtkContainerClass *container_class;
264 GtkBindingSet *binding_set;
266 widget_class = (GtkWidgetClass*) class;
267 container_class = (GtkContainerClass*) class;
269 gobject_class->set_property = gtk_scrolled_window_set_property;
270 gobject_class->get_property = gtk_scrolled_window_get_property;
272 widget_class->destroy = gtk_scrolled_window_destroy;
273 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
274 widget_class->draw = gtk_scrolled_window_draw;
275 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
276 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
277 widget_class->focus = gtk_scrolled_window_focus;
278 widget_class->get_preferred_width = gtk_scrolled_window_get_preferred_width;
279 widget_class->get_preferred_height = gtk_scrolled_window_get_preferred_height;
280 widget_class->get_preferred_height_for_width = gtk_scrolled_window_get_preferred_height_for_width;
281 widget_class->get_preferred_width_for_height = gtk_scrolled_window_get_preferred_width_for_height;
283 container_class->add = gtk_scrolled_window_add;
284 container_class->remove = gtk_scrolled_window_remove;
285 container_class->forall = gtk_scrolled_window_forall;
286 gtk_container_class_handle_border_width (container_class);
288 class->scrollbar_spacing = -1;
290 class->scroll_child = gtk_scrolled_window_scroll_child;
291 class->move_focus_out = gtk_scrolled_window_move_focus_out;
293 g_object_class_install_property (gobject_class,
295 g_param_spec_object ("hadjustment",
296 P_("Horizontal Adjustment"),
297 P_("The GtkAdjustment for the horizontal position"),
299 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
300 g_object_class_install_property (gobject_class,
302 g_param_spec_object ("vadjustment",
303 P_("Vertical Adjustment"),
304 P_("The GtkAdjustment for the vertical position"),
306 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
307 g_object_class_install_property (gobject_class,
308 PROP_HSCROLLBAR_POLICY,
309 g_param_spec_enum ("hscrollbar-policy",
310 P_("Horizontal Scrollbar Policy"),
311 P_("When the horizontal scrollbar is displayed"),
312 GTK_TYPE_POLICY_TYPE,
313 GTK_POLICY_AUTOMATIC,
314 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
315 g_object_class_install_property (gobject_class,
316 PROP_VSCROLLBAR_POLICY,
317 g_param_spec_enum ("vscrollbar-policy",
318 P_("Vertical Scrollbar Policy"),
319 P_("When the vertical scrollbar is displayed"),
320 GTK_TYPE_POLICY_TYPE,
321 GTK_POLICY_AUTOMATIC,
322 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
324 g_object_class_install_property (gobject_class,
325 PROP_WINDOW_PLACEMENT,
326 g_param_spec_enum ("window-placement",
327 P_("Window Placement"),
328 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
329 GTK_TYPE_CORNER_TYPE,
331 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
334 * GtkScrolledWindow:window-placement-set:
336 * Whether "window-placement" should be used to determine the location
337 * of the contents with respect to the scrollbars. Otherwise, the
338 * "gtk-scrolled-window-placement" setting is used.
342 g_object_class_install_property (gobject_class,
343 PROP_WINDOW_PLACEMENT_SET,
344 g_param_spec_boolean ("window-placement-set",
345 P_("Window Placement Set"),
346 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
348 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
349 g_object_class_install_property (gobject_class,
351 g_param_spec_enum ("shadow-type",
353 P_("Style of bevel around the contents"),
354 GTK_TYPE_SHADOW_TYPE,
356 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
359 * GtkScrolledWindow:scrollbars-within-bevel:
361 * Whether to place scrollbars within the scrolled window's bevel.
365 gtk_widget_class_install_style_property (widget_class,
366 g_param_spec_boolean ("scrollbars-within-bevel",
367 P_("Scrollbars within bevel"),
368 P_("Place scrollbars within the scrolled window's bevel"),
370 GTK_PARAM_READABLE));
372 gtk_widget_class_install_style_property (widget_class,
373 g_param_spec_int ("scrollbar-spacing",
374 P_("Scrollbar spacing"),
375 P_("Number of pixels between the scrollbars and the scrolled window"),
378 DEFAULT_SCROLLBAR_SPACING,
379 GTK_PARAM_READABLE));
381 g_object_class_install_property (gobject_class,
382 PROP_MIN_CONTENT_WIDTH,
383 g_param_spec_int ("min-content-width",
384 P_("Minimum Content Width"),
385 P_("The minimum width that the scrolled window will allocate to its content"),
387 GTK_PARAM_READWRITE));
388 g_object_class_install_property (gobject_class,
389 PROP_MIN_CONTENT_HEIGHT,
390 g_param_spec_int ("min-content-height",
391 P_("Minimum Content Height"),
392 P_("The minimum height that the scrolled window will allocate to its content"),
394 GTK_PARAM_READWRITE));
396 * GtkScrolledWindow::scroll-child:
397 * @scrolled_window: a #GtkScrolledWindow
398 * @scroll: a #GtkScrollType describing how much to scroll
399 * @horizontal: whether the keybinding scrolls the child
400 * horizontally or not
402 * The ::scroll-child signal is a
403 * <link linkend="keybinding-signals">keybinding signal</link>
404 * which gets emitted when a keybinding that scrolls is pressed.
405 * The horizontal or vertical adjustment is updated which triggers a
406 * signal that the scrolled windows child may listen to and scroll itself.
408 signals[SCROLL_CHILD] =
409 g_signal_new (I_("scroll-child"),
410 G_TYPE_FROM_CLASS (gobject_class),
411 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
412 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
414 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
416 GTK_TYPE_SCROLL_TYPE,
420 * GtkScrolledWindow::move-focus-out:
421 * @scrolled_window: a #GtkScrolledWindow
422 * @direction_type: either %GTK_DIR_TAB_FORWARD or
423 * %GTK_DIR_TAB_BACKWARD
425 * The ::move-focus-out signal is a
426 * <link linkend="keybinding-signals">keybinding signal</link>
427 * which gets emitted when focus is moved away from the scrolled
428 * window by a keybinding.
429 * The #GtkWidget::move-focus signal is emitted with @direction_type
430 * on this scrolled windows toplevel parent in the container hierarchy.
431 * The default bindings for this signal are
432 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap></keycombo>
434 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap><keycap>Shift</keycap></keycombo>.
436 signals[MOVE_FOCUS_OUT] =
437 g_signal_new (I_("move-focus-out"),
438 G_TYPE_FROM_CLASS (gobject_class),
439 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
440 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
442 _gtk_marshal_VOID__ENUM,
444 GTK_TYPE_DIRECTION_TYPE);
446 binding_set = gtk_binding_set_by_class (class);
448 add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
449 add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
450 add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
451 add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
453 add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
454 add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
455 add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
456 add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
458 add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
459 add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
460 add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
461 add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
463 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
464 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
466 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
470 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
472 GtkScrolledWindowPrivate *priv;
474 scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
475 GTK_TYPE_SCROLLED_WINDOW,
476 GtkScrolledWindowPrivate);
478 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
479 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
481 priv->hscrollbar = NULL;
482 priv->vscrollbar = NULL;
483 priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
484 priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
485 priv->hscrollbar_visible = FALSE;
486 priv->vscrollbar_visible = FALSE;
487 priv->focus_out = FALSE;
488 priv->window_placement = GTK_CORNER_TOP_LEFT;
489 gtk_scrolled_window_update_real_placement (scrolled_window);
490 priv->min_content_width = -1;
491 priv->min_content_height = -1;
495 * gtk_scrolled_window_new:
496 * @hadjustment: (allow-none): horizontal adjustment
497 * @vadjustment: (allow-none): vertical adjustment
499 * Creates a new scrolled window.
501 * The two arguments are the scrolled window's adjustments; these will be
502 * shared with the scrollbars and the child widget to keep the bars in sync
503 * with the child. Usually you want to pass %NULL for the adjustments, which
504 * will cause the scrolled window to create them for you.
506 * Returns: a new scrolled window
509 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
510 GtkAdjustment *vadjustment)
512 GtkWidget *scrolled_window;
515 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
518 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
520 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
521 "hadjustment", hadjustment,
522 "vadjustment", vadjustment,
525 return scrolled_window;
529 * gtk_scrolled_window_set_hadjustment:
530 * @scrolled_window: a #GtkScrolledWindow
531 * @hadjustment: horizontal scroll adjustment
533 * Sets the #GtkAdjustment for the horizontal scrollbar.
536 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
537 GtkAdjustment *hadjustment)
539 GtkScrolledWindowPrivate *priv;
543 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
545 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
547 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
549 bin = GTK_BIN (scrolled_window);
550 priv = scrolled_window->priv;
552 if (!priv->hscrollbar)
554 gtk_widget_push_composite_child ();
555 priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
556 gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
557 gtk_widget_pop_composite_child ();
559 gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
560 g_object_ref (priv->hscrollbar);
561 gtk_widget_show (priv->hscrollbar);
565 GtkAdjustment *old_adjustment;
567 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
568 if (old_adjustment == hadjustment)
571 g_signal_handlers_disconnect_by_func (old_adjustment,
572 gtk_scrolled_window_adjustment_changed,
574 gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
577 hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
578 g_signal_connect (hadjustment,
580 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
582 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
584 child = gtk_bin_get_child (bin);
585 if (GTK_IS_SCROLLABLE (child))
586 gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
588 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
592 * gtk_scrolled_window_set_vadjustment:
593 * @scrolled_window: a #GtkScrolledWindow
594 * @vadjustment: vertical scroll adjustment
596 * Sets the #GtkAdjustment for the vertical scrollbar.
599 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
600 GtkAdjustment *vadjustment)
602 GtkScrolledWindowPrivate *priv;
606 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
608 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
610 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
612 bin = GTK_BIN (scrolled_window);
613 priv = scrolled_window->priv;
615 if (!priv->vscrollbar)
617 gtk_widget_push_composite_child ();
618 priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
619 gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
620 gtk_widget_pop_composite_child ();
622 gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
623 g_object_ref (priv->vscrollbar);
624 gtk_widget_show (priv->vscrollbar);
628 GtkAdjustment *old_adjustment;
630 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
631 if (old_adjustment == vadjustment)
634 g_signal_handlers_disconnect_by_func (old_adjustment,
635 gtk_scrolled_window_adjustment_changed,
637 gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
640 vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
641 g_signal_connect (vadjustment,
643 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
645 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
647 child = gtk_bin_get_child (bin);
648 if (GTK_IS_SCROLLABLE (child))
649 gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
651 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
655 * gtk_scrolled_window_get_hadjustment:
656 * @scrolled_window: a #GtkScrolledWindow
658 * Returns the horizontal scrollbar's adjustment, used to connect the
659 * horizontal scrollbar to the child widget's horizontal scroll
662 * Returns: (transfer none): the horizontal #GtkAdjustment
665 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
667 GtkScrolledWindowPrivate *priv;
669 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
671 priv = scrolled_window->priv;
673 return (priv->hscrollbar ?
674 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
679 * gtk_scrolled_window_get_vadjustment:
680 * @scrolled_window: a #GtkScrolledWindow
682 * Returns the vertical scrollbar's adjustment, used to connect the
683 * vertical scrollbar to the child widget's vertical scroll functionality.
685 * Returns: (transfer none): the vertical #GtkAdjustment
688 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
690 GtkScrolledWindowPrivate *priv;
692 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
694 priv = scrolled_window->priv;
696 return (priv->vscrollbar ?
697 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
702 * gtk_scrolled_window_get_hscrollbar:
703 * @scrolled_window: a #GtkScrolledWindow
705 * Returns the horizontal scrollbar of @scrolled_window.
707 * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
708 * or %NULL if it does not have one.
713 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
715 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
717 return scrolled_window->priv->hscrollbar;
721 * gtk_scrolled_window_get_vscrollbar:
722 * @scrolled_window: a #GtkScrolledWindow
724 * Returns the vertical scrollbar of @scrolled_window.
726 * Returns: (transfer none): the vertical scrollbar of the scrolled window,
727 * or %NULL if it does not have one.
732 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
734 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
736 return scrolled_window->priv->vscrollbar;
740 * gtk_scrolled_window_set_policy:
741 * @scrolled_window: a #GtkScrolledWindow
742 * @hscrollbar_policy: policy for horizontal bar
743 * @vscrollbar_policy: policy for vertical bar
745 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
747 * The policy determines when the scrollbar should appear; it is a value
748 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
749 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
750 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
751 * if needed (that is, if the slider part of the bar would be smaller
752 * than the trough - the display is larger than the page size).
755 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
756 GtkPolicyType hscrollbar_policy,
757 GtkPolicyType vscrollbar_policy)
759 GtkScrolledWindowPrivate *priv;
760 GObject *object = G_OBJECT (scrolled_window);
762 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
764 priv = scrolled_window->priv;
766 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
767 (priv->vscrollbar_policy != vscrollbar_policy))
769 priv->hscrollbar_policy = hscrollbar_policy;
770 priv->vscrollbar_policy = vscrollbar_policy;
772 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
774 g_object_freeze_notify (object);
775 g_object_notify (object, "hscrollbar-policy");
776 g_object_notify (object, "vscrollbar-policy");
777 g_object_thaw_notify (object);
782 * gtk_scrolled_window_get_policy:
783 * @scrolled_window: a #GtkScrolledWindow
784 * @hscrollbar_policy: location to store the policy for the horizontal
785 * scrollbar, or %NULL.
786 * @vscrollbar_policy: location to store the policy for the vertical
787 * scrollbar, or %NULL.
789 * Retrieves the current policy values for the horizontal and vertical
790 * scrollbars. See gtk_scrolled_window_set_policy().
793 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
794 GtkPolicyType *hscrollbar_policy,
795 GtkPolicyType *vscrollbar_policy)
797 GtkScrolledWindowPrivate *priv;
799 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
801 priv = scrolled_window->priv;
803 if (hscrollbar_policy)
804 *hscrollbar_policy = priv->hscrollbar_policy;
805 if (vscrollbar_policy)
806 *vscrollbar_policy = priv->vscrollbar_policy;
810 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
812 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
813 GtkSettings *settings;
815 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
817 if (priv->window_placement_set || settings == NULL)
818 priv->real_window_placement = priv->window_placement;
820 g_object_get (settings,
821 "gtk-scrolled-window-placement",
822 &priv->real_window_placement,
827 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
828 GtkCornerType window_placement)
830 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
832 if (priv->window_placement != window_placement)
834 priv->window_placement = window_placement;
836 gtk_scrolled_window_update_real_placement (scrolled_window);
837 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
839 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
844 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
845 gboolean placement_set,
846 gboolean emit_resize)
848 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
850 if (priv->window_placement_set != placement_set)
852 priv->window_placement_set = placement_set;
854 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-set");
863 * gtk_scrolled_window_set_placement:
864 * @scrolled_window: a #GtkScrolledWindow
865 * @window_placement: position of the child window
867 * Sets the placement of the contents with respect to the scrollbars
868 * for the scrolled window.
870 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
871 * in the top left, with the scrollbars underneath and to the right.
872 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
873 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
875 * See also gtk_scrolled_window_get_placement() and
876 * gtk_scrolled_window_unset_placement().
879 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
880 GtkCornerType window_placement)
882 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
884 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
885 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
889 * gtk_scrolled_window_get_placement:
890 * @scrolled_window: a #GtkScrolledWindow
892 * Gets the placement of the contents with respect to the scrollbars
893 * for the scrolled window. See gtk_scrolled_window_set_placement().
895 * Return value: the current placement value.
897 * See also gtk_scrolled_window_set_placement() and
898 * gtk_scrolled_window_unset_placement().
901 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
903 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
905 return scrolled_window->priv->window_placement;
909 * gtk_scrolled_window_unset_placement:
910 * @scrolled_window: a #GtkScrolledWindow
912 * Unsets the placement of the contents with respect to the scrollbars
913 * for the scrolled window. If no window placement is set for a scrolled
914 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
916 * See also gtk_scrolled_window_set_placement() and
917 * gtk_scrolled_window_get_placement().
922 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
924 GtkScrolledWindowPrivate *priv;
926 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
928 priv = scrolled_window->priv;
930 if (priv->window_placement_set)
932 priv->window_placement_set = FALSE;
934 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
936 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
941 * gtk_scrolled_window_set_shadow_type:
942 * @scrolled_window: a #GtkScrolledWindow
943 * @type: kind of shadow to draw around scrolled window contents
945 * Changes the type of shadow drawn around the contents of
950 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
953 GtkScrolledWindowPrivate *priv;
955 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
956 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
958 priv = scrolled_window->priv;
960 if (priv->shadow_type != type)
962 priv->shadow_type = type;
964 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
965 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
967 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
969 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
974 * gtk_scrolled_window_get_shadow_type:
975 * @scrolled_window: a #GtkScrolledWindow
977 * Gets the shadow type of the scrolled window. See
978 * gtk_scrolled_window_set_shadow_type().
980 * Return value: the current shadow type
983 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
985 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
987 return scrolled_window->priv->shadow_type;
991 gtk_scrolled_window_destroy (GtkWidget *widget)
993 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
994 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
996 if (priv->hscrollbar)
998 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
999 gtk_scrolled_window_adjustment_changed,
1001 gtk_widget_unparent (priv->hscrollbar);
1002 gtk_widget_destroy (priv->hscrollbar);
1003 g_object_unref (priv->hscrollbar);
1004 priv->hscrollbar = NULL;
1006 if (priv->vscrollbar)
1008 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1009 gtk_scrolled_window_adjustment_changed,
1011 gtk_widget_unparent (priv->vscrollbar);
1012 gtk_widget_destroy (priv->vscrollbar);
1013 g_object_unref (priv->vscrollbar);
1014 priv->vscrollbar = NULL;
1017 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1021 gtk_scrolled_window_set_property (GObject *object,
1023 const GValue *value,
1026 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1027 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1031 case PROP_HADJUSTMENT:
1032 gtk_scrolled_window_set_hadjustment (scrolled_window,
1033 g_value_get_object (value));
1035 case PROP_VADJUSTMENT:
1036 gtk_scrolled_window_set_vadjustment (scrolled_window,
1037 g_value_get_object (value));
1039 case PROP_HSCROLLBAR_POLICY:
1040 gtk_scrolled_window_set_policy (scrolled_window,
1041 g_value_get_enum (value),
1042 priv->vscrollbar_policy);
1044 case PROP_VSCROLLBAR_POLICY:
1045 gtk_scrolled_window_set_policy (scrolled_window,
1046 priv->hscrollbar_policy,
1047 g_value_get_enum (value));
1049 case PROP_WINDOW_PLACEMENT:
1050 gtk_scrolled_window_set_placement_internal (scrolled_window,
1051 g_value_get_enum (value));
1053 case PROP_WINDOW_PLACEMENT_SET:
1054 gtk_scrolled_window_set_placement_set (scrolled_window,
1055 g_value_get_boolean (value),
1058 case PROP_SHADOW_TYPE:
1059 gtk_scrolled_window_set_shadow_type (scrolled_window,
1060 g_value_get_enum (value));
1062 case PROP_MIN_CONTENT_WIDTH:
1063 gtk_scrolled_window_set_min_content_width (scrolled_window,
1064 g_value_get_int (value));
1066 case PROP_MIN_CONTENT_HEIGHT:
1067 gtk_scrolled_window_set_min_content_height (scrolled_window,
1068 g_value_get_int (value));
1071 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1077 gtk_scrolled_window_get_property (GObject *object,
1082 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1083 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1087 case PROP_HADJUSTMENT:
1088 g_value_set_object (value,
1089 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1091 case PROP_VADJUSTMENT:
1092 g_value_set_object (value,
1093 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1095 case PROP_WINDOW_PLACEMENT:
1096 g_value_set_enum (value, priv->window_placement);
1098 case PROP_WINDOW_PLACEMENT_SET:
1099 g_value_set_boolean (value, priv->window_placement_set);
1101 case PROP_SHADOW_TYPE:
1102 g_value_set_enum (value, priv->shadow_type);
1104 case PROP_HSCROLLBAR_POLICY:
1105 g_value_set_enum (value, priv->hscrollbar_policy);
1107 case PROP_VSCROLLBAR_POLICY:
1108 g_value_set_enum (value, priv->vscrollbar_policy);
1110 case PROP_MIN_CONTENT_WIDTH:
1111 g_value_set_int (value, priv->min_content_width);
1113 case PROP_MIN_CONTENT_HEIGHT:
1114 g_value_set_int (value, priv->min_content_height);
1117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1123 traverse_container (GtkWidget *widget,
1126 if (GTK_IS_SCROLLED_WINDOW (widget))
1128 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1129 gtk_widget_queue_resize (widget);
1131 else if (GTK_IS_CONTAINER (widget))
1132 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1136 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1140 list = gtk_window_list_toplevels ();
1142 for (l = list; l; l = l->next)
1143 gtk_container_forall (GTK_CONTAINER (l->data),
1144 traverse_container, NULL);
1150 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1151 GdkScreen *previous_screen)
1153 GtkSettings *settings;
1154 guint window_placement_connection;
1156 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1158 if (!gtk_widget_has_screen (widget))
1161 settings = gtk_widget_get_settings (widget);
1163 window_placement_connection =
1164 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1165 "gtk-scrolled-window-connection"));
1167 if (window_placement_connection)
1170 window_placement_connection =
1171 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1172 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1173 g_object_set_data (G_OBJECT (settings),
1174 I_("gtk-scrolled-window-connection"),
1175 GUINT_TO_POINTER (window_placement_connection));
1179 gtk_scrolled_window_draw (GtkWidget *widget,
1182 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1183 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1185 if (priv->shadow_type != GTK_SHADOW_NONE)
1187 GtkAllocation relative_allocation;
1188 GtkStyleContext *context;
1189 gboolean scrollbars_within_bevel;
1191 context = gtk_widget_get_style_context (widget);
1193 gtk_style_context_save (context);
1194 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1196 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1198 if (!scrollbars_within_bevel)
1200 GtkStateFlags state;
1201 GtkBorder padding, border;
1203 state = gtk_widget_get_state_flags (widget);
1204 gtk_style_context_get_padding (context, state, &padding);
1205 gtk_style_context_get_border (context, state, &border);
1207 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1209 relative_allocation.x -= padding.left + border.left;
1210 relative_allocation.y -= padding.top + border.top;
1211 relative_allocation.width += padding.left + padding.right + border.left + border.right;
1212 relative_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1216 relative_allocation.x = 0;
1217 relative_allocation.y = 0;
1218 relative_allocation.width = gtk_widget_get_allocated_width (widget);
1219 relative_allocation.height = gtk_widget_get_allocated_height (widget);
1222 gtk_render_frame (context, cr,
1223 relative_allocation.x,
1224 relative_allocation.y,
1225 relative_allocation.width,
1226 relative_allocation.height);
1228 gtk_style_context_restore (context);
1231 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1237 gtk_scrolled_window_forall (GtkContainer *container,
1238 gboolean include_internals,
1239 GtkCallback callback,
1240 gpointer callback_data)
1242 GtkScrolledWindowPrivate *priv;
1243 GtkScrolledWindow *scrolled_window;
1245 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1246 g_return_if_fail (callback != NULL);
1248 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1252 if (include_internals)
1254 scrolled_window = GTK_SCROLLED_WINDOW (container);
1255 priv = scrolled_window->priv;
1257 if (priv->vscrollbar)
1258 callback (priv->vscrollbar, callback_data);
1259 if (priv->hscrollbar)
1260 callback (priv->hscrollbar, callback_data);
1265 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1266 GtkScrollType scroll,
1267 gboolean horizontal)
1269 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1270 GtkAdjustment *adjustment = NULL;
1274 case GTK_SCROLL_STEP_UP:
1275 scroll = GTK_SCROLL_STEP_BACKWARD;
1278 case GTK_SCROLL_STEP_DOWN:
1279 scroll = GTK_SCROLL_STEP_FORWARD;
1282 case GTK_SCROLL_STEP_LEFT:
1283 scroll = GTK_SCROLL_STEP_BACKWARD;
1286 case GTK_SCROLL_STEP_RIGHT:
1287 scroll = GTK_SCROLL_STEP_FORWARD;
1290 case GTK_SCROLL_PAGE_UP:
1291 scroll = GTK_SCROLL_PAGE_BACKWARD;
1294 case GTK_SCROLL_PAGE_DOWN:
1295 scroll = GTK_SCROLL_PAGE_FORWARD;
1298 case GTK_SCROLL_PAGE_LEFT:
1299 scroll = GTK_SCROLL_STEP_BACKWARD;
1302 case GTK_SCROLL_PAGE_RIGHT:
1303 scroll = GTK_SCROLL_STEP_FORWARD;
1306 case GTK_SCROLL_STEP_BACKWARD:
1307 case GTK_SCROLL_STEP_FORWARD:
1308 case GTK_SCROLL_PAGE_BACKWARD:
1309 case GTK_SCROLL_PAGE_FORWARD:
1310 case GTK_SCROLL_START:
1311 case GTK_SCROLL_END:
1314 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1318 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1319 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1324 if (priv->hscrollbar)
1325 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1329 if (priv->vscrollbar)
1330 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1335 gdouble value = gtk_adjustment_get_value (adjustment);
1339 case GTK_SCROLL_STEP_FORWARD:
1340 value += gtk_adjustment_get_step_increment (adjustment);
1342 case GTK_SCROLL_STEP_BACKWARD:
1343 value -= gtk_adjustment_get_step_increment (adjustment);
1345 case GTK_SCROLL_PAGE_FORWARD:
1346 value += gtk_adjustment_get_page_increment (adjustment);
1348 case GTK_SCROLL_PAGE_BACKWARD:
1349 value -= gtk_adjustment_get_page_increment (adjustment);
1351 case GTK_SCROLL_START:
1352 value = gtk_adjustment_get_lower (adjustment);
1354 case GTK_SCROLL_END:
1355 value = gtk_adjustment_get_upper (adjustment);
1358 g_assert_not_reached ();
1362 gtk_adjustment_set_value (adjustment, value);
1371 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1372 GtkDirectionType direction_type)
1374 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1375 GtkWidget *toplevel;
1377 /* Focus out of the scrolled window entirely. We do this by setting
1378 * a flag, then propagating the focus motion to the notebook.
1380 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1381 if (!gtk_widget_is_toplevel (toplevel))
1384 g_object_ref (scrolled_window);
1386 priv->focus_out = TRUE;
1387 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1388 priv->focus_out = FALSE;
1390 g_object_unref (scrolled_window);
1394 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1395 GtkAllocation *allocation)
1397 GtkAllocation widget_allocation;
1398 GtkScrolledWindow *scrolled_window;
1399 GtkScrolledWindowPrivate *priv;
1404 g_return_if_fail (widget != NULL);
1405 g_return_if_fail (allocation != NULL);
1407 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1408 priv = scrolled_window->priv;
1410 /* Get possible scrollbar dimensions */
1411 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1412 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1413 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1415 /* Subtract some things from our available allocation size */
1419 if (priv->shadow_type != GTK_SHADOW_NONE)
1421 GtkStyleContext *context;
1422 GtkStateFlags state;
1423 GtkBorder padding, border;
1425 context = gtk_widget_get_style_context (widget);
1426 state = gtk_widget_get_state_flags (widget);
1428 gtk_style_context_save (context);
1429 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1431 gtk_style_context_get_border (context, state, &border);
1432 gtk_style_context_get_padding (context, state, &padding);
1434 allocation->x += padding.left + border.left;
1435 allocation->y += padding.top + border.top;
1437 gtk_style_context_restore (context);
1440 gtk_widget_get_allocation (widget, &widget_allocation);
1441 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1442 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1444 if (priv->vscrollbar_visible)
1448 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1451 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1452 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1454 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1455 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1456 allocation->x += (sb_width + sb_spacing);
1458 allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1460 if (priv->hscrollbar_visible)
1463 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1464 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1465 allocation->y += (sb_height + sb_spacing);
1467 allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1472 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1473 GtkAllocation *relative_allocation)
1475 GtkWidget *widget = GTK_WIDGET (swindow), *child;
1476 GtkAllocation allocation;
1477 GtkAllocation child_allocation;
1479 child = gtk_bin_get_child (GTK_BIN (widget));
1481 gtk_widget_get_allocation (widget, &allocation);
1483 gtk_scrolled_window_relative_allocation (widget, relative_allocation);
1484 child_allocation.x = relative_allocation->x + allocation.x;
1485 child_allocation.y = relative_allocation->y + allocation.y;
1486 child_allocation.width = relative_allocation->width;
1487 child_allocation.height = relative_allocation->height;
1489 gtk_widget_size_allocate (child, &child_allocation);
1493 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1494 GtkAllocation *allocation)
1496 GtkScrolledWindow *scrolled_window;
1497 GtkScrolledWindowPrivate *priv;
1498 GtkStyleContext *context;
1499 GtkStateFlags state;
1500 GtkBorder padding, border;
1502 GtkAllocation relative_allocation;
1503 GtkAllocation child_allocation;
1505 gboolean scrollbars_within_bevel;
1510 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1511 g_return_if_fail (allocation != NULL);
1513 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1514 bin = GTK_BIN (scrolled_window);
1515 priv = scrolled_window->priv;
1517 /* Get possible scrollbar dimensions */
1518 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1519 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1520 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1522 context = gtk_widget_get_style_context (widget);
1523 state = gtk_widget_get_state_flags (widget);
1525 gtk_style_context_save (context);
1526 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1528 gtk_style_context_get_padding (context, state, &padding);
1529 gtk_style_context_get_border (context, state, &border);
1531 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1533 gtk_widget_set_allocation (widget, allocation);
1535 gtk_style_context_restore (context);
1537 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1538 priv->hscrollbar_visible = TRUE;
1539 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1540 priv->hscrollbar_visible = FALSE;
1541 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1542 priv->vscrollbar_visible = TRUE;
1543 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1544 priv->vscrollbar_visible = FALSE;
1546 child = gtk_bin_get_child (bin);
1547 if (child && gtk_widget_get_visible (child))
1549 gint child_scroll_width;
1550 gint child_scroll_height;
1551 gboolean previous_hvis;
1552 gboolean previous_vvis;
1555 /* Determine scrollbar visibility first via hfw apis */
1556 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1558 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1559 gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
1561 gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
1563 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1565 /* First try without a vertical scrollbar if the content will fit the height
1566 * given the extra width of the scrollbar */
1567 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1568 gtk_widget_get_preferred_height_for_width (child,
1569 MAX (allocation->width, child_scroll_width),
1570 &child_scroll_height, NULL);
1572 gtk_widget_get_preferred_height_for_width (child,
1573 MAX (allocation->width, child_scroll_width),
1574 NULL, &child_scroll_height);
1576 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1578 /* Does the content height fit the allocation height ? */
1579 priv->vscrollbar_visible = child_scroll_height > allocation->height;
1581 /* Does the content width fit the allocation with minus a possible scrollbar ? */
1582 priv->hscrollbar_visible =
1583 child_scroll_width > allocation->width -
1584 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1586 /* Now that we've guessed the hscrollbar, does the content height fit
1587 * the possible new allocation height ? */
1588 priv->vscrollbar_visible =
1589 child_scroll_height > allocation->height -
1590 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1592 /* Now that we've guessed the vscrollbar, does the content width fit
1593 * the possible new allocation width ? */
1594 priv->hscrollbar_visible =
1595 child_scroll_width > allocation->width -
1596 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1598 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1600 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1601 priv->vscrollbar_visible = child_scroll_height > allocation->height -
1602 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1605 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1607 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1609 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1610 priv->hscrollbar_visible =
1611 child_scroll_width > allocation->width -
1612 (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1614 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1617 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1619 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1620 gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1622 gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1624 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1626 /* First try without a horizontal scrollbar if the content will fit the width
1627 * given the extra height of the scrollbar */
1628 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1629 gtk_widget_get_preferred_width_for_height (child,
1630 MAX (allocation->height, child_scroll_height),
1631 &child_scroll_width, NULL);
1633 gtk_widget_get_preferred_width_for_height (child,
1634 MAX (allocation->height, child_scroll_height),
1635 NULL, &child_scroll_width);
1637 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1639 /* Does the content width fit the allocation width ? */
1640 priv->hscrollbar_visible = child_scroll_width > allocation->width;
1642 /* Does the content height fit the allocation with minus a possible scrollbar ? */
1643 priv->vscrollbar_visible =
1644 child_scroll_height > allocation->height -
1645 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1647 /* Now that we've guessed the vscrollbar, does the content width fit
1648 * the possible new allocation width ? */
1649 priv->hscrollbar_visible =
1650 child_scroll_width > allocation->width -
1651 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1653 /* Now that we've guessed the hscrollbar, does the content height fit
1654 * the possible new allocation height ? */
1655 priv->vscrollbar_visible =
1656 child_scroll_height > allocation->height -
1657 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1659 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1661 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1662 priv->hscrollbar_visible = child_scroll_width > allocation->width -
1663 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1666 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1668 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1670 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1671 priv->vscrollbar_visible =
1672 child_scroll_height > allocation->height -
1673 (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1675 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1679 /* Now after guessing scrollbar visibility; fall back on the allocation loop which
1680 * observes the adjustments to detect scrollbar visibility and also avoids
1681 * infinite recursion
1685 previous_hvis = priv->hscrollbar_visible;
1686 previous_vvis = priv->vscrollbar_visible;
1687 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1689 /* Explicitly force scrollbar visibility checks.
1691 * Since we make a guess above, the child might not decide to update the adjustments
1692 * if they logically did not change since the last configuration
1694 if (priv->hscrollbar)
1695 gtk_scrolled_window_adjustment_changed
1696 (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)), scrolled_window);
1698 if (priv->vscrollbar)
1699 gtk_scrolled_window_adjustment_changed
1700 (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)), scrolled_window);
1702 /* If, after the first iteration, the hscrollbar and the
1703 * vscrollbar flip visiblity... or if one of the scrollbars flip
1704 * on each itteration indefinitly/infinitely, then we just need both
1708 previous_hvis != priv->hscrollbar_visible &&
1709 previous_vvis != priv->vscrollbar_visible) || count > 3)
1711 priv->hscrollbar_visible = TRUE;
1712 priv->vscrollbar_visible = TRUE;
1714 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1721 while (previous_hvis != priv->hscrollbar_visible ||
1722 previous_vvis != priv->vscrollbar_visible);
1726 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1727 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1728 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1731 if (priv->hscrollbar_visible)
1733 if (!gtk_widget_get_visible (priv->hscrollbar))
1734 gtk_widget_show (priv->hscrollbar);
1736 child_allocation.x = relative_allocation.x;
1737 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1738 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1739 child_allocation.y = (relative_allocation.y +
1740 relative_allocation.height +
1742 (priv->shadow_type == GTK_SHADOW_NONE ?
1743 0 : padding.top + border.top));
1745 child_allocation.y = 0;
1747 child_allocation.width = relative_allocation.width;
1748 child_allocation.height = sb_height;
1749 child_allocation.x += allocation->x;
1750 child_allocation.y += allocation->y;
1752 if (priv->shadow_type != GTK_SHADOW_NONE)
1754 if (!scrollbars_within_bevel)
1756 child_allocation.x -= padding.left + border.left;
1757 child_allocation.width += padding.left + padding.right + border.left + border.right;
1759 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1760 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1762 child_allocation.y -= padding.top + border.top;
1766 child_allocation.y += padding.top + border.top;
1770 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1772 else if (gtk_widget_get_visible (priv->hscrollbar))
1773 gtk_widget_hide (priv->hscrollbar);
1775 if (priv->vscrollbar_visible)
1777 if (!gtk_widget_get_visible (priv->vscrollbar))
1778 gtk_widget_show (priv->vscrollbar);
1780 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1781 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1782 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1783 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1784 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1785 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1786 child_allocation.x = (relative_allocation.x +
1787 relative_allocation.width +
1789 (priv->shadow_type == GTK_SHADOW_NONE ?
1790 0 : padding.left + border.left));
1792 child_allocation.x = 0;
1794 child_allocation.y = relative_allocation.y;
1795 child_allocation.width = sb_width;
1796 child_allocation.height = relative_allocation.height;
1797 child_allocation.x += allocation->x;
1798 child_allocation.y += allocation->y;
1800 if (priv->shadow_type != GTK_SHADOW_NONE)
1802 if (!scrollbars_within_bevel)
1804 child_allocation.y -= padding.top + border.top;
1805 child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1807 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1808 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1810 child_allocation.x -= padding.left + border.left;
1814 child_allocation.x += padding.left + border.left;
1818 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1820 else if (gtk_widget_get_visible (priv->vscrollbar))
1821 gtk_widget_hide (priv->vscrollbar);
1825 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1826 GdkEventScroll *event)
1828 GtkScrolledWindowPrivate *priv;
1829 GtkScrolledWindow *scrolled_window;
1832 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1833 g_return_val_if_fail (event != NULL, FALSE);
1835 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1836 priv = scrolled_window->priv;
1838 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1839 range = priv->vscrollbar;
1841 range = priv->hscrollbar;
1843 if (range && gtk_widget_get_visible (range))
1845 GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (range));
1848 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1850 gtk_adjustment_set_value (adjustment, gtk_adjustment_get_value (adjustment) + delta);
1859 gtk_scrolled_window_focus (GtkWidget *widget,
1860 GtkDirectionType direction)
1862 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1863 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1865 gboolean had_focus_child;
1867 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1869 if (priv->focus_out)
1871 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1875 if (gtk_widget_is_focus (widget))
1878 /* We only put the scrolled window itself in the focus chain if it
1879 * isn't possible to focus any children.
1881 child = gtk_bin_get_child (GTK_BIN (widget));
1884 if (gtk_widget_child_focus (child, direction))
1888 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1890 gtk_widget_grab_focus (widget);
1898 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1901 GtkScrolledWindowPrivate *priv;
1902 GtkScrolledWindow *scrolled_window;
1904 g_return_if_fail (adjustment != NULL);
1905 g_return_if_fail (data != NULL);
1907 scrolled_window = GTK_SCROLLED_WINDOW (data);
1908 priv = scrolled_window->priv;
1910 if (priv->hscrollbar &&
1911 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1913 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1917 visible = priv->hscrollbar_visible;
1918 priv->hscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1919 gtk_adjustment_get_page_size (adjustment));
1921 if (priv->hscrollbar_visible != visible)
1922 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1925 else if (priv->vscrollbar &&
1926 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1928 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1932 visible = priv->vscrollbar_visible;
1933 priv->vscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1934 gtk_adjustment_get_page_size (adjustment));
1936 if (priv->vscrollbar_visible != visible)
1937 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1943 gtk_scrolled_window_add (GtkContainer *container,
1946 GtkScrolledWindowPrivate *priv;
1947 GtkScrolledWindow *scrolled_window;
1949 GtkWidget *child_widget;
1950 GtkAdjustment *hadj, *vadj;
1952 bin = GTK_BIN (container);
1953 child_widget = gtk_bin_get_child (bin);
1954 g_return_if_fail (child_widget == NULL);
1956 scrolled_window = GTK_SCROLLED_WINDOW (container);
1957 priv = scrolled_window->priv;
1959 _gtk_bin_set_child (bin, child);
1960 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1962 hadj = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->priv->hscrollbar));
1963 vadj = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->priv->vscrollbar));
1965 if (GTK_IS_SCROLLABLE (child))
1966 g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1968 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1969 "use gtk_scrolled_window_add_with_viewport() instead");
1973 gtk_scrolled_window_remove (GtkContainer *container,
1976 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1977 g_return_if_fail (child != NULL);
1978 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1980 g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
1982 /* chain parent class handler to remove child */
1983 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1987 * gtk_scrolled_window_add_with_viewport:
1988 * @scrolled_window: a #GtkScrolledWindow
1989 * @child: the widget you want to scroll
1991 * Used to add children without native scrolling capabilities. This
1992 * is simply a convenience function; it is equivalent to adding the
1993 * unscrollable child to a viewport, then adding the viewport to the
1994 * scrolled window. If a child has native scrolling, use
1995 * gtk_container_add() instead of this function.
1997 * The viewport scrolls the child by moving its #GdkWindow, and takes
1998 * the size of the child to be the size of its toplevel #GdkWindow.
1999 * This will be very wrong for most widgets that support native scrolling;
2000 * for example, if you add a widget such as #GtkTreeView with a viewport,
2001 * the whole widget will scroll, including the column headings. Thus,
2002 * widgets with native scrolling support should not be used with the
2003 * #GtkViewport proxy.
2005 * A widget supports scrolling natively if it implements the
2006 * #GtkScrollable interface.
2009 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
2013 GtkWidget *viewport;
2014 GtkWidget *child_widget;
2016 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2017 g_return_if_fail (GTK_IS_WIDGET (child));
2018 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
2020 bin = GTK_BIN (scrolled_window);
2021 child_widget = gtk_bin_get_child (bin);
2025 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2026 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2028 viewport = child_widget;
2033 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2034 gtk_scrolled_window_get_vadjustment (scrolled_window));
2035 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2038 gtk_widget_show (viewport);
2039 gtk_container_add (GTK_CONTAINER (viewport), child);
2043 * _gtk_scrolled_window_get_spacing:
2044 * @scrolled_window: a scrolled window
2046 * Gets the spacing between the scrolled window's scrollbars and
2047 * the scrolled widget. Used by GtkCombo
2049 * Return value: the spacing, in pixels.
2052 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2054 GtkScrolledWindowClass *class;
2056 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2058 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2060 if (class->scrollbar_spacing >= 0)
2061 return class->scrollbar_spacing;
2064 gint scrollbar_spacing;
2066 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2067 "scrollbar-spacing", &scrollbar_spacing,
2070 return scrollbar_spacing;
2076 gtk_scrolled_window_get_preferred_size (GtkWidget *widget,
2077 GtkOrientation orientation,
2081 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2082 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2083 GtkBin *bin = GTK_BIN (scrolled_window);
2086 gint scrollbar_spacing;
2087 GtkRequisition hscrollbar_requisition;
2088 GtkRequisition vscrollbar_requisition;
2089 GtkRequisition minimum_req, natural_req;
2091 gint min_child_size, nat_child_size;
2093 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2097 minimum_req.width = 0;
2098 minimum_req.height = 0;
2099 natural_req.width = 0;
2100 natural_req.height = 0;
2102 gtk_widget_get_preferred_size (priv->hscrollbar,
2103 &hscrollbar_requisition, NULL);
2104 gtk_widget_get_preferred_size (priv->vscrollbar,
2105 &vscrollbar_requisition, NULL);
2107 child = gtk_bin_get_child (bin);
2108 if (child && gtk_widget_get_visible (child))
2110 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2112 gtk_widget_get_preferred_width (child,
2116 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2118 minimum_req.width += min_child_size;
2119 natural_req.width += nat_child_size;
2123 gint min_content_width = priv->min_content_width;
2125 if (min_content_width >= 0)
2127 minimum_req.width = MAX (minimum_req.width, min_content_width);
2128 natural_req.width = MAX (natural_req.width, min_content_width);
2133 minimum_req.width += vscrollbar_requisition.width;
2134 natural_req.width += vscrollbar_requisition.width;
2138 else /* GTK_ORIENTATION_VERTICAL */
2140 gtk_widget_get_preferred_height (child,
2144 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2146 minimum_req.height += min_child_size;
2147 natural_req.height += nat_child_size;
2151 gint min_content_height = priv->min_content_height;
2153 if (min_content_height >= 0)
2155 minimum_req.height = MAX (minimum_req.height, min_content_height);
2156 natural_req.height = MAX (natural_req.height, min_content_height);
2161 minimum_req.height += vscrollbar_requisition.height;
2162 natural_req.height += vscrollbar_requisition.height;
2168 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2169 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2171 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2172 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2173 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2174 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2177 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2178 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2180 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2181 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2182 if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2183 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2186 minimum_req.width += MAX (0, extra_width);
2187 minimum_req.height += MAX (0, extra_height);
2188 natural_req.width += MAX (0, extra_width);
2189 natural_req.height += MAX (0, extra_height);
2191 if (priv->shadow_type != GTK_SHADOW_NONE)
2193 GtkStyleContext *context;
2194 GtkStateFlags state;
2195 GtkBorder padding, border;
2197 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
2198 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
2200 gtk_style_context_save (context);
2201 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
2203 gtk_style_context_get_padding (context, state, &padding);
2204 gtk_style_context_get_border (context, state, &border);
2206 minimum_req.width += padding.left + padding.right + border.left + border.right;
2207 minimum_req.height += padding.top + padding.bottom + border.top + border.bottom;
2208 natural_req.width += padding.left + padding.right + border.left + border.right;
2209 natural_req.height += padding.top + padding.bottom + border.top + border.bottom;
2211 gtk_style_context_restore (context);
2214 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2217 *minimum_size = minimum_req.width;
2219 *natural_size = natural_req.width;
2224 *minimum_size = minimum_req.height;
2226 *natural_size = natural_req.height;
2231 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2235 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2239 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2243 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2247 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2249 gint *minimum_height,
2250 gint *natural_height)
2252 g_return_if_fail (GTK_IS_WIDGET (widget));
2254 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2258 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2260 gint *minimum_width,
2261 gint *natural_width)
2263 g_return_if_fail (GTK_IS_WIDGET (widget));
2265 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2269 * gtk_scrolled_window_get_min_content_width:
2270 * @scrolled_window: a #GtkScrolledWindow
2272 * Gets the minimum content width of @scrolled_window, or -1 if not set.
2274 * Returns: the minimum content width
2279 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2281 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2283 return scrolled_window->priv->min_content_width;
2287 * gtk_scrolled_window_set_min_content_width:
2288 * @scrolled_window: a #GtkScrolledWindow
2289 * @width: the minimal content width
2291 * Sets the minimum width that @scrolled_window should keep visible.
2292 * Note that this can and (usually will) be smaller than the minimum
2293 * size of the content.
2298 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2301 GtkScrolledWindowPrivate *priv;
2303 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2305 priv = scrolled_window->priv;
2307 if (priv->min_content_width != width)
2309 priv->min_content_width = width;
2311 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2313 g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2318 * gtk_scrolled_window_get_min_content_height:
2319 * @scrolled_window: a #GtkScrolledWindow
2321 * Gets the minimal content height of @scrolled_window, or -1 if not set.
2323 * Returns: the minimal content height
2328 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2330 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2332 return scrolled_window->priv->min_content_height;
2336 * gtk_scrolled_window_set_min_content_height:
2337 * @scrolled_window: a #GtkScrolledWindow
2338 * @height: the minimal content height
2340 * Sets the minimum height that @scrolled_window should keep visible.
2341 * Note that this can and (usually will) be smaller than the minimum
2342 * size of the content.
2347 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2350 GtkScrolledWindowPrivate *priv;
2352 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2354 priv = scrolled_window->priv;
2356 if (priv->min_content_height != height)
2358 priv->min_content_height = height;
2360 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2362 g_object_notify (G_OBJECT (scrolled_window), "min-content-height");