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/.
29 #include <gdk/gdkkeysyms.h>
30 #include "gtkbindings.h"
31 #include "gtkmarshalers.h"
32 #include "gtkscrollable.h"
33 #include "gtkscrolledwindow.h"
34 #include "gtkwindow.h"
35 #include "gtkprivate.h"
40 * SECTION:gtkscrolledwindow
41 * @Short_description: Adds scrollbars to its child widget
42 * @Title: GtkScrolledWindow
43 * @See_also: #GtkScrollable, #GtkViewport, #GtkAdjustment
45 * #GtkScrolledWindow is a #GtkBin subclass: it's a container
46 * the accepts a single child widget. #GtkScrolledWindow adds scrollbars
47 * to the child widget and optionally draws a beveled frame around the
50 * The scrolled window can work in two ways. Some widgets have native
51 * scrolling support; these widgets implement the #GtkScrollable interface.
52 * Widgets with native scroll support include #GtkTreeView, #GtkTextView,
55 * For widgets that lack native scrolling support, the #GtkViewport
56 * widget acts as an adaptor class, implementing scrollability for child
57 * widgets that lack their own scrolling capabilities. Use #GtkViewport
58 * to scroll child widgets such as #GtkTable, #GtkBox, and so on.
60 * If a widget has native scrolling abilities, it can be added to the
61 * #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
62 * must first add the widget to a #GtkViewport, then add the #GtkViewport
63 * to the scrolled window. The convenience function
64 * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
65 * ignore the presence of the viewport.
67 * The position of the scrollbars is controlled by the scroll
68 * adjustments. See #GtkAdjustment for the fields in an adjustment - for
69 * #GtkScrollbar, used by #GtkScrolledWindow, the "value" field
70 * represents the position of the scrollbar, which must be between the
71 * "lower" field and "upper - page_size." The "page_size" field
72 * represents the size of the visible scrollable area. The
73 * "step_increment" and "page_increment" fields are used when the user
74 * asks to step down (using the small stepper arrows) or page down (using
75 * for example the PageDown key).
77 * If a #GtkScrolledWindow doesn't behave quite as you would like, or
78 * doesn't have exactly the right layout, it's very possible to set up
79 * your own scrolling with #GtkScrollbar and for example a #GtkTable.
83 /* scrolled window policy and size requisition handling:
85 * gtk size requisition works as follows:
86 * a widget upon size-request reports the width and height that it finds
87 * to be best suited to display its contents, including children.
88 * the width and/or height reported from a widget upon size requisition
89 * may be overidden by the user by specifying a width and/or height
90 * other than 0 through gtk_widget_set_size_request().
92 * a scrolled window needs (for implementing all three policy types) to
93 * request its width and height based on two different rationales.
94 * 1) the user wants the scrolled window to just fit into the space
95 * that it gets allocated for a specifc dimension.
96 * 1.1) this does not apply if the user specified a concrete value
97 * value for that specific dimension by either specifying usize for the
98 * scrolled window or for its child.
99 * 2) the user wants the scrolled window to take as much space up as
100 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
102 * also, kinda obvious:
103 * 3) a user would certainly not have choosen a scrolled window as a container
104 * for the child, if the resulting allocation takes up more space than the
105 * child would have allocated without the scrolled window.
108 * A) from 1) follows: the scrolled window shouldn't request more space for a
109 * specifc dimension than is required at minimum.
110 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
111 * window (done automatically) or by usize of the child (needs to be checked).
112 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
114 * D) from 3) follows: the scrolled window child's minimum width and minimum height
115 * under A) at least correspond to the space taken up by its scrollbars.
118 #define DEFAULT_SCROLLBAR_SPACING 3
120 struct _GtkScrolledWindowPrivate
122 GtkCornerType real_window_placement;
123 GtkWidget *hscrollbar;
124 GtkWidget *vscrollbar;
126 gboolean window_placement_set;
130 guint hscrollbar_policy : 2;
131 guint vscrollbar_policy : 2;
132 guint hscrollbar_visible : 1;
133 guint vscrollbar_visible : 1;
134 guint window_placement : 2;
135 guint focus_out : 1; /* Flag used by ::move-focus-out implementation */
137 gint min_content_width;
138 gint min_content_height;
146 PROP_HSCROLLBAR_POLICY,
147 PROP_VSCROLLBAR_POLICY,
148 PROP_WINDOW_PLACEMENT,
149 PROP_WINDOW_PLACEMENT_SET,
151 PROP_MIN_CONTENT_WIDTH,
152 PROP_MIN_CONTENT_HEIGHT
163 static void gtk_scrolled_window_set_property (GObject *object,
167 static void gtk_scrolled_window_get_property (GObject *object,
172 static void gtk_scrolled_window_destroy (GtkWidget *widget);
173 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
174 GdkScreen *previous_screen);
175 static gboolean gtk_scrolled_window_draw (GtkWidget *widget,
177 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
178 GtkAllocation *allocation);
179 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
180 GdkEventScroll *event);
181 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
182 GtkDirectionType direction);
183 static void gtk_scrolled_window_add (GtkContainer *container,
185 static void gtk_scrolled_window_remove (GtkContainer *container,
187 static void gtk_scrolled_window_forall (GtkContainer *container,
188 gboolean include_internals,
189 GtkCallback callback,
190 gpointer callback_data);
191 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
192 GtkScrollType scroll,
193 gboolean horizontal);
194 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
195 GtkDirectionType direction_type);
197 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
198 GtkAllocation *allocation);
199 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
202 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
204 static void gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
207 static void gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
210 static void gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *layout,
212 gint *minimum_height,
213 gint *natural_height);
214 static void gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *layout,
216 gint *minimum_height,
217 gint *natural_height);
219 static guint signals[LAST_SIGNAL] = {0};
221 G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
225 add_scroll_binding (GtkBindingSet *binding_set,
227 GdkModifierType mask,
228 GtkScrollType scroll,
231 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
233 gtk_binding_entry_add_signal (binding_set, keyval, mask,
235 GTK_TYPE_SCROLL_TYPE, scroll,
236 G_TYPE_BOOLEAN, horizontal);
237 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
239 GTK_TYPE_SCROLL_TYPE, scroll,
240 G_TYPE_BOOLEAN, horizontal);
244 add_tab_bindings (GtkBindingSet *binding_set,
245 GdkModifierType modifiers,
246 GtkDirectionType direction)
248 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
250 GTK_TYPE_DIRECTION_TYPE, direction);
251 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
253 GTK_TYPE_DIRECTION_TYPE, direction);
257 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
259 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
260 GtkWidgetClass *widget_class;
261 GtkContainerClass *container_class;
262 GtkBindingSet *binding_set;
264 widget_class = (GtkWidgetClass*) class;
265 container_class = (GtkContainerClass*) class;
267 gobject_class->set_property = gtk_scrolled_window_set_property;
268 gobject_class->get_property = gtk_scrolled_window_get_property;
270 widget_class->destroy = gtk_scrolled_window_destroy;
271 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
272 widget_class->draw = gtk_scrolled_window_draw;
273 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
274 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
275 widget_class->focus = gtk_scrolled_window_focus;
276 widget_class->get_preferred_width = gtk_scrolled_window_get_preferred_width;
277 widget_class->get_preferred_height = gtk_scrolled_window_get_preferred_height;
278 widget_class->get_preferred_height_for_width = gtk_scrolled_window_get_preferred_height_for_width;
279 widget_class->get_preferred_width_for_height = gtk_scrolled_window_get_preferred_width_for_height;
281 container_class->add = gtk_scrolled_window_add;
282 container_class->remove = gtk_scrolled_window_remove;
283 container_class->forall = gtk_scrolled_window_forall;
284 gtk_container_class_handle_border_width (container_class);
286 class->scrollbar_spacing = -1;
288 class->scroll_child = gtk_scrolled_window_scroll_child;
289 class->move_focus_out = gtk_scrolled_window_move_focus_out;
291 g_object_class_install_property (gobject_class,
293 g_param_spec_object ("hadjustment",
294 P_("Horizontal Adjustment"),
295 P_("The GtkAdjustment for the horizontal position"),
297 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
298 g_object_class_install_property (gobject_class,
300 g_param_spec_object ("vadjustment",
301 P_("Vertical Adjustment"),
302 P_("The GtkAdjustment for the vertical position"),
304 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
305 g_object_class_install_property (gobject_class,
306 PROP_HSCROLLBAR_POLICY,
307 g_param_spec_enum ("hscrollbar-policy",
308 P_("Horizontal Scrollbar Policy"),
309 P_("When the horizontal scrollbar is displayed"),
310 GTK_TYPE_POLICY_TYPE,
311 GTK_POLICY_AUTOMATIC,
312 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
313 g_object_class_install_property (gobject_class,
314 PROP_VSCROLLBAR_POLICY,
315 g_param_spec_enum ("vscrollbar-policy",
316 P_("Vertical Scrollbar Policy"),
317 P_("When the vertical scrollbar is displayed"),
318 GTK_TYPE_POLICY_TYPE,
319 GTK_POLICY_AUTOMATIC,
320 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
322 g_object_class_install_property (gobject_class,
323 PROP_WINDOW_PLACEMENT,
324 g_param_spec_enum ("window-placement",
325 P_("Window Placement"),
326 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
327 GTK_TYPE_CORNER_TYPE,
329 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
332 * GtkScrolledWindow:window-placement-set:
334 * Whether "window-placement" should be used to determine the location
335 * of the contents with respect to the scrollbars. Otherwise, the
336 * "gtk-scrolled-window-placement" setting is used.
340 g_object_class_install_property (gobject_class,
341 PROP_WINDOW_PLACEMENT_SET,
342 g_param_spec_boolean ("window-placement-set",
343 P_("Window Placement Set"),
344 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
346 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
347 g_object_class_install_property (gobject_class,
349 g_param_spec_enum ("shadow-type",
351 P_("Style of bevel around the contents"),
352 GTK_TYPE_SHADOW_TYPE,
354 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
357 * GtkScrolledWindow:scrollbars-within-bevel:
359 * Whether to place scrollbars within the scrolled window's bevel.
363 gtk_widget_class_install_style_property (widget_class,
364 g_param_spec_boolean ("scrollbars-within-bevel",
365 P_("Scrollbars within bevel"),
366 P_("Place scrollbars within the scrolled window's bevel"),
368 GTK_PARAM_READABLE));
370 gtk_widget_class_install_style_property (widget_class,
371 g_param_spec_int ("scrollbar-spacing",
372 P_("Scrollbar spacing"),
373 P_("Number of pixels between the scrollbars and the scrolled window"),
376 DEFAULT_SCROLLBAR_SPACING,
377 GTK_PARAM_READABLE));
379 g_object_class_install_property (gobject_class,
380 PROP_MIN_CONTENT_WIDTH,
381 g_param_spec_int ("min-content-width",
382 P_("Minimum Content Width"),
383 P_("The minimum width that the scrolled window will allocate to its content"),
385 GTK_PARAM_READWRITE));
386 g_object_class_install_property (gobject_class,
387 PROP_MIN_CONTENT_HEIGHT,
388 g_param_spec_int ("min-content-height",
389 P_("Minimum Content Height"),
390 P_("The minimum height that the scrolled window will allocate to its content"),
392 GTK_PARAM_READWRITE));
394 * GtkScrolledWindow::scroll-child:
395 * @scrolled_window: a #GtkScrolledWindow
396 * @scroll: a #GtkScrollType describing how much to scroll
397 * @horizontal: whether the keybinding scrolls the child
398 * horizontally or not
400 * The ::scroll-child signal is a
401 * <link linkend="keybinding-signals">keybinding signal</link>
402 * which gets emitted when a keybinding that scrolls is pressed.
403 * The horizontal or vertical adjustment is updated which triggers a
404 * signal that the scrolled windows child may listen to and scroll itself.
406 signals[SCROLL_CHILD] =
407 g_signal_new (I_("scroll-child"),
408 G_TYPE_FROM_CLASS (gobject_class),
409 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
410 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
412 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
414 GTK_TYPE_SCROLL_TYPE,
418 * GtkScrolledWindow::move-focus-out:
419 * @scrolled_window: a #GtkScrolledWindow
420 * @direction_type: either %GTK_DIR_TAB_FORWARD or
421 * %GTK_DIR_TAB_BACKWARD
423 * The ::move-focus-out signal is a
424 * <link linkend="keybinding-signals">keybinding signal</link>
425 * which gets emitted when focus is moved away from the scrolled
426 * window by a keybinding.
427 * The #GtkWidget::move-focus signal is emitted with @direction_type
428 * on this scrolled windows toplevel parent in the container hierarchy.
429 * The default bindings for this signal are
430 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap></keycombo>
432 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap><keycap>Shift</keycap></keycombo>.
434 signals[MOVE_FOCUS_OUT] =
435 g_signal_new (I_("move-focus-out"),
436 G_TYPE_FROM_CLASS (gobject_class),
437 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
438 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
440 _gtk_marshal_VOID__ENUM,
442 GTK_TYPE_DIRECTION_TYPE);
444 binding_set = gtk_binding_set_by_class (class);
446 add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
447 add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
448 add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
449 add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
451 add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
452 add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
453 add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
454 add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
456 add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
457 add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
458 add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
459 add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
461 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
462 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
464 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
468 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
470 GtkScrolledWindowPrivate *priv;
472 scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
473 GTK_TYPE_SCROLLED_WINDOW,
474 GtkScrolledWindowPrivate);
476 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
477 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
479 priv->hscrollbar = NULL;
480 priv->vscrollbar = NULL;
481 priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
482 priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
483 priv->hscrollbar_visible = FALSE;
484 priv->vscrollbar_visible = FALSE;
485 priv->focus_out = FALSE;
486 priv->window_placement = GTK_CORNER_TOP_LEFT;
487 gtk_scrolled_window_update_real_placement (scrolled_window);
488 priv->min_content_width = -1;
489 priv->min_content_height = -1;
493 * gtk_scrolled_window_new:
494 * @hadjustment: (allow-none): horizontal adjustment
495 * @vadjustment: (allow-none): vertical adjustment
497 * Creates a new scrolled window.
499 * The two arguments are the scrolled window's adjustments; these will be
500 * shared with the scrollbars and the child widget to keep the bars in sync
501 * with the child. Usually you want to pass %NULL for the adjustments, which
502 * will cause the scrolled window to create them for you.
504 * Returns: a new scrolled window
507 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
508 GtkAdjustment *vadjustment)
510 GtkWidget *scrolled_window;
513 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
516 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
518 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
519 "hadjustment", hadjustment,
520 "vadjustment", vadjustment,
523 return scrolled_window;
527 * gtk_scrolled_window_set_hadjustment:
528 * @scrolled_window: a #GtkScrolledWindow
529 * @hadjustment: horizontal scroll adjustment
531 * Sets the #GtkAdjustment for the horizontal scrollbar.
534 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
535 GtkAdjustment *hadjustment)
537 GtkScrolledWindowPrivate *priv;
541 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
543 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
545 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
547 bin = GTK_BIN (scrolled_window);
548 priv = scrolled_window->priv;
550 if (!priv->hscrollbar)
552 gtk_widget_push_composite_child ();
553 priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
554 gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
555 gtk_widget_pop_composite_child ();
557 gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
558 g_object_ref (priv->hscrollbar);
559 gtk_widget_show (priv->hscrollbar);
563 GtkAdjustment *old_adjustment;
565 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
566 if (old_adjustment == hadjustment)
569 g_signal_handlers_disconnect_by_func (old_adjustment,
570 gtk_scrolled_window_adjustment_changed,
572 gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
575 hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
576 g_signal_connect (hadjustment,
578 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
580 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
582 child = gtk_bin_get_child (bin);
583 if (GTK_IS_SCROLLABLE (child))
584 gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
586 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
590 * gtk_scrolled_window_set_vadjustment:
591 * @scrolled_window: a #GtkScrolledWindow
592 * @vadjustment: vertical scroll adjustment
594 * Sets the #GtkAdjustment for the vertical scrollbar.
597 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
598 GtkAdjustment *vadjustment)
600 GtkScrolledWindowPrivate *priv;
604 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
606 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
608 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
610 bin = GTK_BIN (scrolled_window);
611 priv = scrolled_window->priv;
613 if (!priv->vscrollbar)
615 gtk_widget_push_composite_child ();
616 priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
617 gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
618 gtk_widget_pop_composite_child ();
620 gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
621 g_object_ref (priv->vscrollbar);
622 gtk_widget_show (priv->vscrollbar);
626 GtkAdjustment *old_adjustment;
628 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
629 if (old_adjustment == vadjustment)
632 g_signal_handlers_disconnect_by_func (old_adjustment,
633 gtk_scrolled_window_adjustment_changed,
635 gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
638 vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
639 g_signal_connect (vadjustment,
641 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
643 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
645 child = gtk_bin_get_child (bin);
646 if (GTK_IS_SCROLLABLE (child))
647 gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
649 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
653 * gtk_scrolled_window_get_hadjustment:
654 * @scrolled_window: a #GtkScrolledWindow
656 * Returns the horizontal scrollbar's adjustment, used to connect the
657 * horizontal scrollbar to the child widget's horizontal scroll
660 * Returns: (transfer none): the horizontal #GtkAdjustment
663 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
665 GtkScrolledWindowPrivate *priv;
667 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
669 priv = scrolled_window->priv;
671 return (priv->hscrollbar ?
672 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
677 * gtk_scrolled_window_get_vadjustment:
678 * @scrolled_window: a #GtkScrolledWindow
680 * Returns the vertical scrollbar's adjustment, used to connect the
681 * vertical scrollbar to the child widget's vertical scroll functionality.
683 * Returns: (transfer none): the vertical #GtkAdjustment
686 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
688 GtkScrolledWindowPrivate *priv;
690 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
692 priv = scrolled_window->priv;
694 return (priv->vscrollbar ?
695 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
700 * gtk_scrolled_window_get_hscrollbar:
701 * @scrolled_window: a #GtkScrolledWindow
703 * Returns the horizontal scrollbar of @scrolled_window.
705 * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
706 * or %NULL if it does not have one.
711 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
713 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
715 return scrolled_window->priv->hscrollbar;
719 * gtk_scrolled_window_get_vscrollbar:
720 * @scrolled_window: a #GtkScrolledWindow
722 * Returns the vertical scrollbar of @scrolled_window.
724 * Returns: (transfer none): the vertical scrollbar of the scrolled window,
725 * or %NULL if it does not have one.
730 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
732 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
734 return scrolled_window->priv->vscrollbar;
738 * gtk_scrolled_window_set_policy:
739 * @scrolled_window: a #GtkScrolledWindow
740 * @hscrollbar_policy: policy for horizontal bar
741 * @vscrollbar_policy: policy for vertical bar
743 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
745 * The policy determines when the scrollbar should appear; it is a value
746 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
747 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
748 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
749 * if needed (that is, if the slider part of the bar would be smaller
750 * than the trough - the display is larger than the page size).
753 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
754 GtkPolicyType hscrollbar_policy,
755 GtkPolicyType vscrollbar_policy)
757 GtkScrolledWindowPrivate *priv;
758 GObject *object = G_OBJECT (scrolled_window);
760 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
762 priv = scrolled_window->priv;
764 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
765 (priv->vscrollbar_policy != vscrollbar_policy))
767 priv->hscrollbar_policy = hscrollbar_policy;
768 priv->vscrollbar_policy = vscrollbar_policy;
770 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
772 g_object_freeze_notify (object);
773 g_object_notify (object, "hscrollbar-policy");
774 g_object_notify (object, "vscrollbar-policy");
775 g_object_thaw_notify (object);
780 * gtk_scrolled_window_get_policy:
781 * @scrolled_window: a #GtkScrolledWindow
782 * @hscrollbar_policy: location to store the policy for the horizontal
783 * scrollbar, or %NULL.
784 * @vscrollbar_policy: location to store the policy for the vertical
785 * scrollbar, or %NULL.
787 * Retrieves the current policy values for the horizontal and vertical
788 * scrollbars. See gtk_scrolled_window_set_policy().
791 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
792 GtkPolicyType *hscrollbar_policy,
793 GtkPolicyType *vscrollbar_policy)
795 GtkScrolledWindowPrivate *priv;
797 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
799 priv = scrolled_window->priv;
801 if (hscrollbar_policy)
802 *hscrollbar_policy = priv->hscrollbar_policy;
803 if (vscrollbar_policy)
804 *vscrollbar_policy = priv->vscrollbar_policy;
808 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
810 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
811 GtkSettings *settings;
813 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
815 if (priv->window_placement_set || settings == NULL)
816 priv->real_window_placement = priv->window_placement;
818 g_object_get (settings,
819 "gtk-scrolled-window-placement",
820 &priv->real_window_placement,
825 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
826 GtkCornerType window_placement)
828 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
830 if (priv->window_placement != window_placement)
832 priv->window_placement = window_placement;
834 gtk_scrolled_window_update_real_placement (scrolled_window);
835 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
837 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
842 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
843 gboolean placement_set,
844 gboolean emit_resize)
846 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
848 if (priv->window_placement_set != placement_set)
850 priv->window_placement_set = placement_set;
852 gtk_scrolled_window_update_real_placement (scrolled_window);
854 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
856 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
861 * gtk_scrolled_window_set_placement:
862 * @scrolled_window: a #GtkScrolledWindow
863 * @window_placement: position of the child window
865 * Sets the placement of the contents with respect to the scrollbars
866 * for the scrolled window.
868 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
869 * in the top left, with the scrollbars underneath and to the right.
870 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
871 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
873 * See also gtk_scrolled_window_get_placement() and
874 * gtk_scrolled_window_unset_placement().
877 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
878 GtkCornerType window_placement)
880 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
882 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
883 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
887 * gtk_scrolled_window_get_placement:
888 * @scrolled_window: a #GtkScrolledWindow
890 * Gets the placement of the contents with respect to the scrollbars
891 * for the scrolled window. See gtk_scrolled_window_set_placement().
893 * Return value: the current placement value.
895 * See also gtk_scrolled_window_set_placement() and
896 * gtk_scrolled_window_unset_placement().
899 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
901 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
903 return scrolled_window->priv->window_placement;
907 * gtk_scrolled_window_unset_placement:
908 * @scrolled_window: a #GtkScrolledWindow
910 * Unsets the placement of the contents with respect to the scrollbars
911 * for the scrolled window. If no window placement is set for a scrolled
912 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
914 * See also gtk_scrolled_window_set_placement() and
915 * gtk_scrolled_window_get_placement().
920 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
922 GtkScrolledWindowPrivate *priv;
924 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
926 priv = scrolled_window->priv;
928 if (priv->window_placement_set)
930 priv->window_placement_set = FALSE;
932 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
934 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
939 * gtk_scrolled_window_set_shadow_type:
940 * @scrolled_window: a #GtkScrolledWindow
941 * @type: kind of shadow to draw around scrolled window contents
943 * Changes the type of shadow drawn around the contents of
948 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
951 GtkScrolledWindowPrivate *priv;
953 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
954 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
956 priv = scrolled_window->priv;
958 if (priv->shadow_type != type)
960 priv->shadow_type = type;
962 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
963 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
965 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
967 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
972 * gtk_scrolled_window_get_shadow_type:
973 * @scrolled_window: a #GtkScrolledWindow
975 * Gets the shadow type of the scrolled window. See
976 * gtk_scrolled_window_set_shadow_type().
978 * Return value: the current shadow type
981 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
983 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
985 return scrolled_window->priv->shadow_type;
989 gtk_scrolled_window_destroy (GtkWidget *widget)
991 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
992 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
994 if (priv->hscrollbar)
996 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
997 gtk_scrolled_window_adjustment_changed,
999 gtk_widget_unparent (priv->hscrollbar);
1000 gtk_widget_destroy (priv->hscrollbar);
1001 g_object_unref (priv->hscrollbar);
1002 priv->hscrollbar = NULL;
1004 if (priv->vscrollbar)
1006 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1007 gtk_scrolled_window_adjustment_changed,
1009 gtk_widget_unparent (priv->vscrollbar);
1010 gtk_widget_destroy (priv->vscrollbar);
1011 g_object_unref (priv->vscrollbar);
1012 priv->vscrollbar = NULL;
1015 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1019 gtk_scrolled_window_set_property (GObject *object,
1021 const GValue *value,
1024 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1025 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1029 case PROP_HADJUSTMENT:
1030 gtk_scrolled_window_set_hadjustment (scrolled_window,
1031 g_value_get_object (value));
1033 case PROP_VADJUSTMENT:
1034 gtk_scrolled_window_set_vadjustment (scrolled_window,
1035 g_value_get_object (value));
1037 case PROP_HSCROLLBAR_POLICY:
1038 gtk_scrolled_window_set_policy (scrolled_window,
1039 g_value_get_enum (value),
1040 priv->vscrollbar_policy);
1042 case PROP_VSCROLLBAR_POLICY:
1043 gtk_scrolled_window_set_policy (scrolled_window,
1044 priv->hscrollbar_policy,
1045 g_value_get_enum (value));
1047 case PROP_WINDOW_PLACEMENT:
1048 gtk_scrolled_window_set_placement_internal (scrolled_window,
1049 g_value_get_enum (value));
1051 case PROP_WINDOW_PLACEMENT_SET:
1052 gtk_scrolled_window_set_placement_set (scrolled_window,
1053 g_value_get_boolean (value),
1056 case PROP_SHADOW_TYPE:
1057 gtk_scrolled_window_set_shadow_type (scrolled_window,
1058 g_value_get_enum (value));
1060 case PROP_MIN_CONTENT_WIDTH:
1061 gtk_scrolled_window_set_min_content_width (scrolled_window,
1062 g_value_get_int (value));
1064 case PROP_MIN_CONTENT_HEIGHT:
1065 gtk_scrolled_window_set_min_content_height (scrolled_window,
1066 g_value_get_int (value));
1069 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1075 gtk_scrolled_window_get_property (GObject *object,
1080 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1081 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1085 case PROP_HADJUSTMENT:
1086 g_value_set_object (value,
1087 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1089 case PROP_VADJUSTMENT:
1090 g_value_set_object (value,
1091 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1093 case PROP_WINDOW_PLACEMENT:
1094 g_value_set_enum (value, priv->window_placement);
1096 case PROP_WINDOW_PLACEMENT_SET:
1097 g_value_set_boolean (value, priv->window_placement_set);
1099 case PROP_SHADOW_TYPE:
1100 g_value_set_enum (value, priv->shadow_type);
1102 case PROP_HSCROLLBAR_POLICY:
1103 g_value_set_enum (value, priv->hscrollbar_policy);
1105 case PROP_VSCROLLBAR_POLICY:
1106 g_value_set_enum (value, priv->vscrollbar_policy);
1108 case PROP_MIN_CONTENT_WIDTH:
1109 g_value_set_int (value, priv->min_content_width);
1111 case PROP_MIN_CONTENT_HEIGHT:
1112 g_value_set_int (value, priv->min_content_height);
1115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1121 traverse_container (GtkWidget *widget,
1124 if (GTK_IS_SCROLLED_WINDOW (widget))
1126 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1127 gtk_widget_queue_resize (widget);
1129 else if (GTK_IS_CONTAINER (widget))
1130 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1134 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1138 list = gtk_window_list_toplevels ();
1140 for (l = list; l; l = l->next)
1141 gtk_container_forall (GTK_CONTAINER (l->data),
1142 traverse_container, NULL);
1148 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1149 GdkScreen *previous_screen)
1151 GtkSettings *settings;
1152 guint window_placement_connection;
1154 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1156 if (!gtk_widget_has_screen (widget))
1159 settings = gtk_widget_get_settings (widget);
1161 window_placement_connection =
1162 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1163 "gtk-scrolled-window-connection"));
1165 if (window_placement_connection)
1168 window_placement_connection =
1169 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1170 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1171 g_object_set_data (G_OBJECT (settings),
1172 I_("gtk-scrolled-window-connection"),
1173 GUINT_TO_POINTER (window_placement_connection));
1177 gtk_scrolled_window_draw (GtkWidget *widget,
1180 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1181 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1183 if (priv->shadow_type != GTK_SHADOW_NONE)
1185 GtkAllocation relative_allocation;
1186 GtkStyleContext *context;
1187 gboolean scrollbars_within_bevel;
1189 context = gtk_widget_get_style_context (widget);
1191 gtk_style_context_save (context);
1192 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1194 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1196 if (!scrollbars_within_bevel)
1198 GtkStateFlags state;
1199 GtkBorder padding, border;
1201 state = gtk_widget_get_state_flags (widget);
1202 gtk_style_context_get_padding (context, state, &padding);
1203 gtk_style_context_get_border (context, state, &border);
1205 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1207 relative_allocation.x -= padding.left + border.left;
1208 relative_allocation.y -= padding.top + border.top;
1209 relative_allocation.width += padding.left + padding.right + border.left + border.right;
1210 relative_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1214 relative_allocation.x = 0;
1215 relative_allocation.y = 0;
1216 relative_allocation.width = gtk_widget_get_allocated_width (widget);
1217 relative_allocation.height = gtk_widget_get_allocated_height (widget);
1220 gtk_render_frame (context, cr,
1221 relative_allocation.x,
1222 relative_allocation.y,
1223 relative_allocation.width,
1224 relative_allocation.height);
1226 gtk_style_context_restore (context);
1229 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1235 gtk_scrolled_window_forall (GtkContainer *container,
1236 gboolean include_internals,
1237 GtkCallback callback,
1238 gpointer callback_data)
1240 GtkScrolledWindowPrivate *priv;
1241 GtkScrolledWindow *scrolled_window;
1243 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1244 g_return_if_fail (callback != NULL);
1246 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1250 if (include_internals)
1252 scrolled_window = GTK_SCROLLED_WINDOW (container);
1253 priv = scrolled_window->priv;
1255 if (priv->vscrollbar)
1256 callback (priv->vscrollbar, callback_data);
1257 if (priv->hscrollbar)
1258 callback (priv->hscrollbar, callback_data);
1263 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1264 GtkScrollType scroll,
1265 gboolean horizontal)
1267 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1268 GtkAdjustment *adjustment = NULL;
1272 case GTK_SCROLL_STEP_UP:
1273 scroll = GTK_SCROLL_STEP_BACKWARD;
1276 case GTK_SCROLL_STEP_DOWN:
1277 scroll = GTK_SCROLL_STEP_FORWARD;
1280 case GTK_SCROLL_STEP_LEFT:
1281 scroll = GTK_SCROLL_STEP_BACKWARD;
1284 case GTK_SCROLL_STEP_RIGHT:
1285 scroll = GTK_SCROLL_STEP_FORWARD;
1288 case GTK_SCROLL_PAGE_UP:
1289 scroll = GTK_SCROLL_PAGE_BACKWARD;
1292 case GTK_SCROLL_PAGE_DOWN:
1293 scroll = GTK_SCROLL_PAGE_FORWARD;
1296 case GTK_SCROLL_PAGE_LEFT:
1297 scroll = GTK_SCROLL_STEP_BACKWARD;
1300 case GTK_SCROLL_PAGE_RIGHT:
1301 scroll = GTK_SCROLL_STEP_FORWARD;
1304 case GTK_SCROLL_STEP_BACKWARD:
1305 case GTK_SCROLL_STEP_FORWARD:
1306 case GTK_SCROLL_PAGE_BACKWARD:
1307 case GTK_SCROLL_PAGE_FORWARD:
1308 case GTK_SCROLL_START:
1309 case GTK_SCROLL_END:
1312 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1316 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1317 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1322 if (priv->hscrollbar)
1323 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1327 if (priv->vscrollbar)
1328 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1333 gdouble value = adjustment->value;
1337 case GTK_SCROLL_STEP_FORWARD:
1338 value += adjustment->step_increment;
1340 case GTK_SCROLL_STEP_BACKWARD:
1341 value -= adjustment->step_increment;
1343 case GTK_SCROLL_PAGE_FORWARD:
1344 value += adjustment->page_increment;
1346 case GTK_SCROLL_PAGE_BACKWARD:
1347 value -= adjustment->page_increment;
1349 case GTK_SCROLL_START:
1350 value = adjustment->lower;
1352 case GTK_SCROLL_END:
1353 value = adjustment->upper;
1356 g_assert_not_reached ();
1360 gtk_adjustment_set_value (adjustment, value);
1369 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1370 GtkDirectionType direction_type)
1372 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1373 GtkWidget *toplevel;
1375 /* Focus out of the scrolled window entirely. We do this by setting
1376 * a flag, then propagating the focus motion to the notebook.
1378 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1379 if (!gtk_widget_is_toplevel (toplevel))
1382 g_object_ref (scrolled_window);
1384 priv->focus_out = TRUE;
1385 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1386 priv->focus_out = FALSE;
1388 g_object_unref (scrolled_window);
1392 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1393 GtkAllocation *allocation)
1395 GtkAllocation widget_allocation;
1396 GtkScrolledWindow *scrolled_window;
1397 GtkScrolledWindowPrivate *priv;
1402 g_return_if_fail (widget != NULL);
1403 g_return_if_fail (allocation != NULL);
1405 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1406 priv = scrolled_window->priv;
1408 /* Get possible scrollbar dimensions */
1409 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1410 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1411 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1413 /* Subtract some things from our available allocation size */
1417 if (priv->shadow_type != GTK_SHADOW_NONE)
1419 GtkStyleContext *context;
1420 GtkStateFlags state;
1421 GtkBorder padding, border;
1423 context = gtk_widget_get_style_context (widget);
1424 state = gtk_widget_get_state_flags (widget);
1426 gtk_style_context_save (context);
1427 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1429 gtk_style_context_get_border (context, state, &border);
1430 gtk_style_context_get_padding (context, state, &padding);
1432 allocation->x += padding.left + border.left;
1433 allocation->y += padding.top + border.top;
1435 gtk_style_context_restore (context);
1438 gtk_widget_get_allocation (widget, &widget_allocation);
1439 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1440 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1442 if (priv->vscrollbar_visible)
1446 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1449 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1450 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1452 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1453 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1454 allocation->x += (sb_width + sb_spacing);
1456 allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1458 if (priv->hscrollbar_visible)
1461 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1462 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1463 allocation->y += (sb_height + sb_spacing);
1465 allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1470 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1471 GtkAllocation *relative_allocation)
1473 GtkWidget *widget = GTK_WIDGET (swindow), *child;
1474 GtkAllocation allocation;
1475 GtkAllocation child_allocation;
1477 child = gtk_bin_get_child (GTK_BIN (widget));
1479 gtk_widget_get_allocation (widget, &allocation);
1481 gtk_scrolled_window_relative_allocation (widget, relative_allocation);
1482 child_allocation.x = relative_allocation->x + allocation.x;
1483 child_allocation.y = relative_allocation->y + allocation.y;
1484 child_allocation.width = relative_allocation->width;
1485 child_allocation.height = relative_allocation->height;
1487 gtk_widget_size_allocate (child, &child_allocation);
1491 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1492 GtkAllocation *allocation)
1494 GtkScrolledWindow *scrolled_window;
1495 GtkScrolledWindowPrivate *priv;
1496 GtkStyleContext *context;
1497 GtkStateFlags state;
1498 GtkBorder padding, border;
1500 GtkAllocation relative_allocation;
1501 GtkAllocation child_allocation;
1503 gboolean scrollbars_within_bevel;
1508 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1509 g_return_if_fail (allocation != NULL);
1511 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1512 bin = GTK_BIN (scrolled_window);
1513 priv = scrolled_window->priv;
1515 /* Get possible scrollbar dimensions */
1516 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1517 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1518 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1520 context = gtk_widget_get_style_context (widget);
1521 state = gtk_widget_get_state_flags (widget);
1523 gtk_style_context_save (context);
1524 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1526 gtk_style_context_get_padding (context, state, &padding);
1527 gtk_style_context_get_border (context, state, &border);
1529 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1531 gtk_widget_set_allocation (widget, allocation);
1533 gtk_style_context_restore (context);
1535 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1536 priv->hscrollbar_visible = TRUE;
1537 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1538 priv->hscrollbar_visible = FALSE;
1539 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1540 priv->vscrollbar_visible = TRUE;
1541 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1542 priv->vscrollbar_visible = FALSE;
1544 child = gtk_bin_get_child (bin);
1545 if (child && gtk_widget_get_visible (child))
1547 gint child_scroll_width;
1548 gint child_scroll_height;
1549 gboolean previous_hvis;
1550 gboolean previous_vvis;
1553 /* Determine scrollbar visibility first via hfw apis */
1554 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1556 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1557 gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
1559 gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
1561 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1563 /* First try without a vertical scrollbar if the content will fit the height
1564 * given the extra width of the scrollbar */
1565 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1566 gtk_widget_get_preferred_height_for_width (child,
1567 MAX (allocation->width, child_scroll_width),
1568 &child_scroll_height, NULL);
1570 gtk_widget_get_preferred_height_for_width (child,
1571 MAX (allocation->width, child_scroll_width),
1572 NULL, &child_scroll_height);
1574 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1576 /* Does the content height fit the allocation height ? */
1577 priv->vscrollbar_visible = child_scroll_height > allocation->height;
1579 /* Does the content width fit the allocation with minus a possible scrollbar ? */
1580 priv->hscrollbar_visible =
1581 child_scroll_width > allocation->width -
1582 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1584 /* Now that we've guessed the hscrollbar, does the content height fit
1585 * the possible new allocation height ? */
1586 priv->vscrollbar_visible =
1587 child_scroll_height > allocation->height -
1588 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1590 /* Now that we've guessed the vscrollbar, does the content width fit
1591 * the possible new allocation width ? */
1592 priv->hscrollbar_visible =
1593 child_scroll_width > allocation->width -
1594 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1596 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1598 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1599 priv->vscrollbar_visible = child_scroll_height > allocation->height -
1600 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1603 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1605 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1607 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1608 priv->hscrollbar_visible =
1609 child_scroll_width > allocation->width -
1610 (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1612 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1615 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1617 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1618 gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1620 gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1622 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1624 /* First try without a horizontal scrollbar if the content will fit the width
1625 * given the extra height of the scrollbar */
1626 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1627 gtk_widget_get_preferred_width_for_height (child,
1628 MAX (allocation->height, child_scroll_height),
1629 &child_scroll_width, NULL);
1631 gtk_widget_get_preferred_width_for_height (child,
1632 MAX (allocation->height, child_scroll_height),
1633 NULL, &child_scroll_width);
1635 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1637 /* Does the content width fit the allocation width ? */
1638 priv->hscrollbar_visible = child_scroll_width > allocation->width;
1640 /* Does the content height fit the allocation with minus a possible scrollbar ? */
1641 priv->vscrollbar_visible =
1642 child_scroll_height > allocation->height -
1643 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1645 /* Now that we've guessed the vscrollbar, does the content width fit
1646 * the possible new allocation width ? */
1647 priv->hscrollbar_visible =
1648 child_scroll_width > allocation->width -
1649 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1651 /* Now that we've guessed the hscrollbar, does the content height fit
1652 * the possible new allocation height ? */
1653 priv->vscrollbar_visible =
1654 child_scroll_height > allocation->height -
1655 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1657 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1659 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1660 priv->hscrollbar_visible = child_scroll_width > allocation->width -
1661 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1664 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1666 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1668 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1669 priv->vscrollbar_visible =
1670 child_scroll_height > allocation->height -
1671 (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1673 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1677 /* Now after guessing scrollbar visibility; fall back on the allocation loop which
1678 * observes the adjustments to detect scrollbar visibility and also avoids
1679 * infinite recursion
1683 previous_hvis = priv->hscrollbar_visible;
1684 previous_vvis = priv->vscrollbar_visible;
1685 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1687 /* Explicitly force scrollbar visibility checks.
1689 * Since we make a guess above, the child might not decide to update the adjustments
1690 * if they logically did not change since the last configuration
1692 if (priv->hscrollbar)
1693 gtk_scrolled_window_adjustment_changed
1694 (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)), scrolled_window);
1696 if (priv->vscrollbar)
1697 gtk_scrolled_window_adjustment_changed
1698 (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)), scrolled_window);
1700 /* If, after the first iteration, the hscrollbar and the
1701 * vscrollbar flip visiblity... or if one of the scrollbars flip
1702 * on each itteration indefinitly/infinitely, then we just need both
1706 previous_hvis != priv->hscrollbar_visible &&
1707 previous_vvis != priv->vscrollbar_visible) || count > 3)
1709 priv->hscrollbar_visible = TRUE;
1710 priv->vscrollbar_visible = TRUE;
1712 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1719 while (previous_hvis != priv->hscrollbar_visible ||
1720 previous_vvis != priv->vscrollbar_visible);
1724 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1725 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1726 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1729 if (priv->hscrollbar_visible)
1731 if (!gtk_widget_get_visible (priv->hscrollbar))
1732 gtk_widget_show (priv->hscrollbar);
1734 child_allocation.x = relative_allocation.x;
1735 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1736 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1737 child_allocation.y = (relative_allocation.y +
1738 relative_allocation.height +
1740 (priv->shadow_type == GTK_SHADOW_NONE ?
1741 0 : padding.top + border.top));
1743 child_allocation.y = 0;
1745 child_allocation.width = relative_allocation.width;
1746 child_allocation.height = sb_height;
1747 child_allocation.x += allocation->x;
1748 child_allocation.y += allocation->y;
1750 if (priv->shadow_type != GTK_SHADOW_NONE)
1752 if (!scrollbars_within_bevel)
1754 child_allocation.x -= padding.left + border.left;
1755 child_allocation.width += padding.left + padding.right + border.left + border.right;
1757 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1758 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1760 child_allocation.y -= padding.top + border.top;
1764 child_allocation.y += padding.top + border.top;
1768 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1770 else if (gtk_widget_get_visible (priv->hscrollbar))
1771 gtk_widget_hide (priv->hscrollbar);
1773 if (priv->vscrollbar_visible)
1775 if (!gtk_widget_get_visible (priv->vscrollbar))
1776 gtk_widget_show (priv->vscrollbar);
1778 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1779 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1780 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1781 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1782 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1783 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1784 child_allocation.x = (relative_allocation.x +
1785 relative_allocation.width +
1787 (priv->shadow_type == GTK_SHADOW_NONE ?
1788 0 : padding.left + border.left));
1790 child_allocation.x = 0;
1792 child_allocation.y = relative_allocation.y;
1793 child_allocation.width = sb_width;
1794 child_allocation.height = relative_allocation.height;
1795 child_allocation.x += allocation->x;
1796 child_allocation.y += allocation->y;
1798 if (priv->shadow_type != GTK_SHADOW_NONE)
1800 if (!scrollbars_within_bevel)
1802 child_allocation.y -= padding.top + border.top;
1803 child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1805 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1806 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1808 child_allocation.x -= padding.left + border.left;
1812 child_allocation.x += padding.left + border.left;
1816 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1818 else if (gtk_widget_get_visible (priv->vscrollbar))
1819 gtk_widget_hide (priv->vscrollbar);
1823 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1824 GdkEventScroll *event)
1826 GtkScrolledWindowPrivate *priv;
1827 GtkScrolledWindow *scrolled_window;
1830 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1831 g_return_val_if_fail (event != NULL, FALSE);
1833 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1834 priv = scrolled_window->priv;
1836 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1837 range = priv->vscrollbar;
1839 range = priv->hscrollbar;
1841 if (range && gtk_widget_get_visible (range))
1843 GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE (range));
1846 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1848 gtk_adjustment_set_value (adj, adj->value + delta);
1857 gtk_scrolled_window_focus (GtkWidget *widget,
1858 GtkDirectionType direction)
1860 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1861 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1863 gboolean had_focus_child;
1865 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1867 if (priv->focus_out)
1869 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1873 if (gtk_widget_is_focus (widget))
1876 /* We only put the scrolled window itself in the focus chain if it
1877 * isn't possible to focus any children.
1879 child = gtk_bin_get_child (GTK_BIN (widget));
1882 if (gtk_widget_child_focus (child, direction))
1886 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1888 gtk_widget_grab_focus (widget);
1896 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1899 GtkScrolledWindowPrivate *priv;
1900 GtkScrolledWindow *scrolled_window;
1902 g_return_if_fail (adjustment != NULL);
1903 g_return_if_fail (data != NULL);
1905 scrolled_window = GTK_SCROLLED_WINDOW (data);
1906 priv = scrolled_window->priv;
1908 if (priv->hscrollbar &&
1909 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1911 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1915 visible = priv->hscrollbar_visible;
1916 priv->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1917 adjustment->page_size);
1919 if (priv->hscrollbar_visible != visible)
1920 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1923 else if (priv->vscrollbar &&
1924 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1926 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1930 visible = priv->vscrollbar_visible;
1931 priv->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1932 adjustment->page_size);
1934 if (priv->vscrollbar_visible != visible)
1935 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1941 gtk_scrolled_window_add (GtkContainer *container,
1944 GtkScrolledWindowPrivate *priv;
1945 GtkScrolledWindow *scrolled_window;
1947 GtkWidget *child_widget;
1948 GtkAdjustment *hadj, *vadj;
1950 bin = GTK_BIN (container);
1951 child_widget = gtk_bin_get_child (bin);
1952 g_return_if_fail (child_widget == NULL);
1954 scrolled_window = GTK_SCROLLED_WINDOW (container);
1955 priv = scrolled_window->priv;
1957 _gtk_bin_set_child (bin, child);
1958 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1960 hadj = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->priv->hscrollbar));
1961 vadj = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->priv->vscrollbar));
1963 if (GTK_IS_SCROLLABLE (child))
1964 g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1966 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1967 "use gtk_scrolled_window_add_with_viewport() instead");
1971 gtk_scrolled_window_remove (GtkContainer *container,
1974 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1975 g_return_if_fail (child != NULL);
1976 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1978 g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
1980 /* chain parent class handler to remove child */
1981 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1985 * gtk_scrolled_window_add_with_viewport:
1986 * @scrolled_window: a #GtkScrolledWindow
1987 * @child: the widget you want to scroll
1989 * Used to add children without native scrolling capabilities. This
1990 * is simply a convenience function; it is equivalent to adding the
1991 * unscrollable child to a viewport, then adding the viewport to the
1992 * scrolled window. If a child has native scrolling, use
1993 * gtk_container_add() instead of this function.
1995 * The viewport scrolls the child by moving its #GdkWindow, and takes
1996 * the size of the child to be the size of its toplevel #GdkWindow.
1997 * This will be very wrong for most widgets that support native scrolling;
1998 * for example, if you add a widget such as #GtkTreeView with a viewport,
1999 * the whole widget will scroll, including the column headings. Thus,
2000 * widgets with native scrolling support should not be used with the
2001 * #GtkViewport proxy.
2003 * A widget supports scrolling natively if it implements the
2004 * #GtkScrollable interface.
2007 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
2011 GtkWidget *viewport;
2012 GtkWidget *child_widget;
2014 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2015 g_return_if_fail (GTK_IS_WIDGET (child));
2016 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
2018 bin = GTK_BIN (scrolled_window);
2019 child_widget = gtk_bin_get_child (bin);
2023 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2024 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2026 viewport = child_widget;
2031 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2032 gtk_scrolled_window_get_vadjustment (scrolled_window));
2033 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2036 gtk_widget_show (viewport);
2037 gtk_container_add (GTK_CONTAINER (viewport), child);
2041 * _gtk_scrolled_window_get_spacing:
2042 * @scrolled_window: a scrolled window
2044 * Gets the spacing between the scrolled window's scrollbars and
2045 * the scrolled widget. Used by GtkCombo
2047 * Return value: the spacing, in pixels.
2050 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2052 GtkScrolledWindowClass *class;
2054 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2056 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2058 if (class->scrollbar_spacing >= 0)
2059 return class->scrollbar_spacing;
2062 gint scrollbar_spacing;
2064 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2065 "scrollbar-spacing", &scrollbar_spacing,
2068 return scrollbar_spacing;
2074 gtk_scrolled_window_get_preferred_size (GtkWidget *widget,
2075 GtkOrientation orientation,
2079 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2080 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2081 GtkBin *bin = GTK_BIN (scrolled_window);
2084 gint scrollbar_spacing;
2085 GtkRequisition hscrollbar_requisition;
2086 GtkRequisition vscrollbar_requisition;
2087 GtkRequisition minimum_req, natural_req;
2089 gint min_child_size, nat_child_size;
2091 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2095 minimum_req.width = 0;
2096 minimum_req.height = 0;
2097 natural_req.width = 0;
2098 natural_req.height = 0;
2100 gtk_widget_get_preferred_size (priv->hscrollbar,
2101 &hscrollbar_requisition, NULL);
2102 gtk_widget_get_preferred_size (priv->vscrollbar,
2103 &vscrollbar_requisition, NULL);
2105 child = gtk_bin_get_child (bin);
2106 if (child && gtk_widget_get_visible (child))
2108 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2110 gtk_widget_get_preferred_width (child,
2114 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2116 minimum_req.width += min_child_size;
2117 natural_req.width += nat_child_size;
2121 gint min_content_width = priv->min_content_width;
2123 if (min_content_width >= 0)
2125 minimum_req.width = MAX (minimum_req.width, min_content_width);
2126 natural_req.width = MAX (natural_req.width, min_content_width);
2131 minimum_req.width += vscrollbar_requisition.width;
2132 natural_req.width += vscrollbar_requisition.width;
2136 else /* GTK_ORIENTATION_VERTICAL */
2138 gtk_widget_get_preferred_height (child,
2142 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2144 minimum_req.height += min_child_size;
2145 natural_req.height += nat_child_size;
2149 gint min_content_height = priv->min_content_height;
2151 if (min_content_height >= 0)
2153 minimum_req.height = MAX (minimum_req.height, min_content_height);
2154 natural_req.height = MAX (natural_req.height, min_content_height);
2159 minimum_req.height += vscrollbar_requisition.height;
2160 natural_req.height += vscrollbar_requisition.height;
2166 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2167 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2169 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2170 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2171 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2172 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2175 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2176 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2178 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2179 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2180 if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2181 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2184 minimum_req.width += MAX (0, extra_width);
2185 minimum_req.height += MAX (0, extra_height);
2186 natural_req.width += MAX (0, extra_width);
2187 natural_req.height += MAX (0, extra_height);
2189 if (priv->shadow_type != GTK_SHADOW_NONE)
2191 GtkStyleContext *context;
2192 GtkStateFlags state;
2193 GtkBorder padding, border;
2195 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
2196 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
2198 gtk_style_context_save (context);
2199 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
2201 gtk_style_context_get_padding (context, state, &padding);
2202 gtk_style_context_get_border (context, state, &border);
2204 minimum_req.width += padding.left + padding.right + border.left + border.right;
2205 minimum_req.height += padding.top + padding.bottom + border.top + border.bottom;
2206 natural_req.width += padding.left + padding.right + border.left + border.right;
2207 natural_req.height += padding.top + padding.bottom + border.top + border.bottom;
2209 gtk_style_context_restore (context);
2212 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2215 *minimum_size = minimum_req.width;
2217 *natural_size = natural_req.width;
2222 *minimum_size = minimum_req.height;
2224 *natural_size = natural_req.height;
2229 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2233 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2237 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2241 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2245 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2247 gint *minimum_height,
2248 gint *natural_height)
2250 g_return_if_fail (GTK_IS_WIDGET (widget));
2252 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2256 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2258 gint *minimum_width,
2259 gint *natural_width)
2261 g_return_if_fail (GTK_IS_WIDGET (widget));
2263 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2267 * gtk_scrolled_window_get_min_content_width:
2268 * @scrolled_window: a #GtkScrolledWindow
2270 * Gets the minimum content width of @scrolled_window, or -1 if not set.
2272 * Returns: the minimum content width
2277 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2279 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2281 return scrolled_window->priv->min_content_width;
2285 * gtk_scrolled_window_set_min_content_width:
2286 * @scrolled_window: a #GtkScrolledWindow
2287 * @width: the minimal content width
2289 * Sets the minimum width that @scrolled_window should keep visible.
2290 * Note that this can and (usually will) be smaller than the minimum
2291 * size of the content.
2296 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2299 GtkScrolledWindowPrivate *priv;
2301 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2303 priv = scrolled_window->priv;
2305 if (priv->min_content_width != width)
2307 priv->min_content_width = width;
2309 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2311 g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2316 * gtk_scrolled_window_get_min_content_height:
2317 * @scrolled_window: a #GtkScrolledWindow
2319 * Gets the minimal content height of @scrolled_window, or -1 if not set.
2321 * Returns: the minimal content height
2326 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2328 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2330 return scrolled_window->priv->min_content_height;
2334 * gtk_scrolled_window_set_min_content_height:
2335 * @scrolled_window: a #GtkScrolledWindow
2336 * @height: the minimal content height
2338 * Sets the minimum height that @scrolled_window should keep visible.
2339 * Note that this can and (usually will) be smaller than the minimum
2340 * size of the content.
2345 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2348 GtkScrolledWindowPrivate *priv;
2350 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2352 priv = scrolled_window->priv;
2354 if (priv->min_content_height != height)
2356 priv->min_content_height = height;
2358 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2360 g_object_notify (G_OBJECT (scrolled_window), "min-content-height");