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 GtkWidget *hscrollbar;
125 GtkWidget *vscrollbar;
127 GtkCornerType real_window_placement;
130 guint window_placement_set : 1;
131 guint hscrollbar_policy : 2;
132 guint vscrollbar_policy : 2;
133 guint hscrollbar_visible : 1;
134 guint vscrollbar_visible : 1;
135 guint window_placement : 2;
136 guint focus_out : 1; /* Flag used by ::move-focus-out implementation */
138 gint min_content_width;
139 gint min_content_height;
147 PROP_HSCROLLBAR_POLICY,
148 PROP_VSCROLLBAR_POLICY,
149 PROP_WINDOW_PLACEMENT,
150 PROP_WINDOW_PLACEMENT_SET,
152 PROP_MIN_CONTENT_WIDTH,
153 PROP_MIN_CONTENT_HEIGHT
164 static void gtk_scrolled_window_set_property (GObject *object,
168 static void gtk_scrolled_window_get_property (GObject *object,
173 static void gtk_scrolled_window_destroy (GtkWidget *widget);
174 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
175 GdkScreen *previous_screen);
176 static gboolean gtk_scrolled_window_draw (GtkWidget *widget,
178 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
179 GtkAllocation *allocation);
180 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
181 GdkEventScroll *event);
182 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
183 GtkDirectionType direction);
184 static void gtk_scrolled_window_add (GtkContainer *container,
186 static void gtk_scrolled_window_remove (GtkContainer *container,
188 static void gtk_scrolled_window_forall (GtkContainer *container,
189 gboolean include_internals,
190 GtkCallback callback,
191 gpointer callback_data);
192 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
193 GtkScrollType scroll,
194 gboolean horizontal);
195 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
196 GtkDirectionType direction_type);
198 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
199 GtkAllocation *allocation);
200 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
203 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
205 static void gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
208 static void gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
211 static void gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *layout,
213 gint *minimum_height,
214 gint *natural_height);
215 static void gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *layout,
217 gint *minimum_height,
218 gint *natural_height);
220 static guint signals[LAST_SIGNAL] = {0};
222 G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
226 add_scroll_binding (GtkBindingSet *binding_set,
228 GdkModifierType mask,
229 GtkScrollType scroll,
232 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
234 gtk_binding_entry_add_signal (binding_set, keyval, mask,
236 GTK_TYPE_SCROLL_TYPE, scroll,
237 G_TYPE_BOOLEAN, horizontal);
238 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
240 GTK_TYPE_SCROLL_TYPE, scroll,
241 G_TYPE_BOOLEAN, horizontal);
245 add_tab_bindings (GtkBindingSet *binding_set,
246 GdkModifierType modifiers,
247 GtkDirectionType direction)
249 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
251 GTK_TYPE_DIRECTION_TYPE, direction);
252 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
254 GTK_TYPE_DIRECTION_TYPE, direction);
258 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
260 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
261 GtkWidgetClass *widget_class;
262 GtkContainerClass *container_class;
263 GtkBindingSet *binding_set;
265 widget_class = (GtkWidgetClass*) class;
266 container_class = (GtkContainerClass*) class;
268 gobject_class->set_property = gtk_scrolled_window_set_property;
269 gobject_class->get_property = gtk_scrolled_window_get_property;
271 widget_class->destroy = gtk_scrolled_window_destroy;
272 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
273 widget_class->draw = gtk_scrolled_window_draw;
274 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
275 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
276 widget_class->focus = gtk_scrolled_window_focus;
277 widget_class->get_preferred_width = gtk_scrolled_window_get_preferred_width;
278 widget_class->get_preferred_height = gtk_scrolled_window_get_preferred_height;
279 widget_class->get_preferred_height_for_width = gtk_scrolled_window_get_preferred_height_for_width;
280 widget_class->get_preferred_width_for_height = gtk_scrolled_window_get_preferred_width_for_height;
282 container_class->add = gtk_scrolled_window_add;
283 container_class->remove = gtk_scrolled_window_remove;
284 container_class->forall = gtk_scrolled_window_forall;
285 gtk_container_class_handle_border_width (container_class);
287 class->scrollbar_spacing = -1;
289 class->scroll_child = gtk_scrolled_window_scroll_child;
290 class->move_focus_out = gtk_scrolled_window_move_focus_out;
292 g_object_class_install_property (gobject_class,
294 g_param_spec_object ("hadjustment",
295 P_("Horizontal Adjustment"),
296 P_("The GtkAdjustment for the horizontal position"),
298 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
299 g_object_class_install_property (gobject_class,
301 g_param_spec_object ("vadjustment",
302 P_("Vertical Adjustment"),
303 P_("The GtkAdjustment for the vertical position"),
305 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
306 g_object_class_install_property (gobject_class,
307 PROP_HSCROLLBAR_POLICY,
308 g_param_spec_enum ("hscrollbar-policy",
309 P_("Horizontal Scrollbar Policy"),
310 P_("When the horizontal scrollbar is displayed"),
311 GTK_TYPE_POLICY_TYPE,
312 GTK_POLICY_AUTOMATIC,
313 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
314 g_object_class_install_property (gobject_class,
315 PROP_VSCROLLBAR_POLICY,
316 g_param_spec_enum ("vscrollbar-policy",
317 P_("Vertical Scrollbar Policy"),
318 P_("When the vertical scrollbar is displayed"),
319 GTK_TYPE_POLICY_TYPE,
320 GTK_POLICY_AUTOMATIC,
321 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
323 g_object_class_install_property (gobject_class,
324 PROP_WINDOW_PLACEMENT,
325 g_param_spec_enum ("window-placement",
326 P_("Window Placement"),
327 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
328 GTK_TYPE_CORNER_TYPE,
330 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
333 * GtkScrolledWindow:window-placement-set:
335 * Whether "window-placement" should be used to determine the location
336 * of the contents with respect to the scrollbars. Otherwise, the
337 * "gtk-scrolled-window-placement" setting is used.
341 g_object_class_install_property (gobject_class,
342 PROP_WINDOW_PLACEMENT_SET,
343 g_param_spec_boolean ("window-placement-set",
344 P_("Window Placement Set"),
345 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
347 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
348 g_object_class_install_property (gobject_class,
350 g_param_spec_enum ("shadow-type",
352 P_("Style of bevel around the contents"),
353 GTK_TYPE_SHADOW_TYPE,
355 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
358 * GtkScrolledWindow:scrollbars-within-bevel:
360 * Whether to place scrollbars within the scrolled window's bevel.
364 gtk_widget_class_install_style_property (widget_class,
365 g_param_spec_boolean ("scrollbars-within-bevel",
366 P_("Scrollbars within bevel"),
367 P_("Place scrollbars within the scrolled window's bevel"),
369 GTK_PARAM_READABLE));
371 gtk_widget_class_install_style_property (widget_class,
372 g_param_spec_int ("scrollbar-spacing",
373 P_("Scrollbar spacing"),
374 P_("Number of pixels between the scrollbars and the scrolled window"),
377 DEFAULT_SCROLLBAR_SPACING,
378 GTK_PARAM_READABLE));
381 * GtkScrolledWindow:min-content-width:
383 * The minimum content width of @scrolled_window, or -1 if not set.
387 g_object_class_install_property (gobject_class,
388 PROP_MIN_CONTENT_WIDTH,
389 g_param_spec_int ("min-content-width",
390 P_("Minimum Content Width"),
391 P_("The minimum width that the scrolled window will allocate to its content"),
393 GTK_PARAM_READWRITE));
396 * GtkScrolledWindow:min-content-height:
398 * The minimum content height of @scrolled_window, or -1 if not set.
402 g_object_class_install_property (gobject_class,
403 PROP_MIN_CONTENT_HEIGHT,
404 g_param_spec_int ("min-content-height",
405 P_("Minimum Content Height"),
406 P_("The minimum height that the scrolled window will allocate to its content"),
408 GTK_PARAM_READWRITE));
410 * GtkScrolledWindow::scroll-child:
411 * @scrolled_window: a #GtkScrolledWindow
412 * @scroll: a #GtkScrollType describing how much to scroll
413 * @horizontal: whether the keybinding scrolls the child
414 * horizontally or not
416 * The ::scroll-child signal is a
417 * <link linkend="keybinding-signals">keybinding signal</link>
418 * which gets emitted when a keybinding that scrolls is pressed.
419 * The horizontal or vertical adjustment is updated which triggers a
420 * signal that the scrolled windows child may listen to and scroll itself.
422 signals[SCROLL_CHILD] =
423 g_signal_new (I_("scroll-child"),
424 G_TYPE_FROM_CLASS (gobject_class),
425 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
426 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
428 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
430 GTK_TYPE_SCROLL_TYPE,
434 * GtkScrolledWindow::move-focus-out:
435 * @scrolled_window: a #GtkScrolledWindow
436 * @direction_type: either %GTK_DIR_TAB_FORWARD or
437 * %GTK_DIR_TAB_BACKWARD
439 * The ::move-focus-out signal is a
440 * <link linkend="keybinding-signals">keybinding signal</link>
441 * which gets emitted when focus is moved away from the scrolled
442 * window by a keybinding.
443 * The #GtkWidget::move-focus signal is emitted with @direction_type
444 * on this scrolled windows toplevel parent in the container hierarchy.
445 * The default bindings for this signal are
446 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap></keycombo>
448 * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap><keycap>Shift</keycap></keycombo>.
450 signals[MOVE_FOCUS_OUT] =
451 g_signal_new (I_("move-focus-out"),
452 G_TYPE_FROM_CLASS (gobject_class),
453 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
454 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
456 _gtk_marshal_VOID__ENUM,
458 GTK_TYPE_DIRECTION_TYPE);
460 binding_set = gtk_binding_set_by_class (class);
462 add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
463 add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
464 add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
465 add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
467 add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
468 add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
469 add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
470 add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
472 add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
473 add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
474 add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
475 add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
477 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
478 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
480 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
484 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
486 GtkScrolledWindowPrivate *priv;
488 scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
489 GTK_TYPE_SCROLLED_WINDOW,
490 GtkScrolledWindowPrivate);
492 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
493 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
495 priv->hscrollbar = NULL;
496 priv->vscrollbar = NULL;
497 priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
498 priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
499 priv->hscrollbar_visible = FALSE;
500 priv->vscrollbar_visible = FALSE;
501 priv->focus_out = FALSE;
502 priv->window_placement = GTK_CORNER_TOP_LEFT;
503 gtk_scrolled_window_update_real_placement (scrolled_window);
504 priv->min_content_width = -1;
505 priv->min_content_height = -1;
509 * gtk_scrolled_window_new:
510 * @hadjustment: (allow-none): horizontal adjustment
511 * @vadjustment: (allow-none): vertical adjustment
513 * Creates a new scrolled window.
515 * The two arguments are the scrolled window's adjustments; these will be
516 * shared with the scrollbars and the child widget to keep the bars in sync
517 * with the child. Usually you want to pass %NULL for the adjustments, which
518 * will cause the scrolled window to create them for you.
520 * Returns: a new scrolled window
523 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
524 GtkAdjustment *vadjustment)
526 GtkWidget *scrolled_window;
529 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
532 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
534 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
535 "hadjustment", hadjustment,
536 "vadjustment", vadjustment,
539 return scrolled_window;
543 * gtk_scrolled_window_set_hadjustment:
544 * @scrolled_window: a #GtkScrolledWindow
545 * @hadjustment: horizontal scroll adjustment
547 * Sets the #GtkAdjustment for the horizontal scrollbar.
550 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
551 GtkAdjustment *hadjustment)
553 GtkScrolledWindowPrivate *priv;
557 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
559 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
561 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
563 bin = GTK_BIN (scrolled_window);
564 priv = scrolled_window->priv;
566 if (!priv->hscrollbar)
568 gtk_widget_push_composite_child ();
569 priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
570 gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
571 gtk_widget_pop_composite_child ();
573 gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
574 g_object_ref (priv->hscrollbar);
575 gtk_widget_show (priv->hscrollbar);
579 GtkAdjustment *old_adjustment;
581 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
582 if (old_adjustment == hadjustment)
585 g_signal_handlers_disconnect_by_func (old_adjustment,
586 gtk_scrolled_window_adjustment_changed,
588 gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
591 hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
592 g_signal_connect (hadjustment,
594 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
596 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
598 child = gtk_bin_get_child (bin);
599 if (GTK_IS_SCROLLABLE (child))
600 gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
602 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
606 * gtk_scrolled_window_set_vadjustment:
607 * @scrolled_window: a #GtkScrolledWindow
608 * @vadjustment: vertical scroll adjustment
610 * Sets the #GtkAdjustment for the vertical scrollbar.
613 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
614 GtkAdjustment *vadjustment)
616 GtkScrolledWindowPrivate *priv;
620 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
622 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
624 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
626 bin = GTK_BIN (scrolled_window);
627 priv = scrolled_window->priv;
629 if (!priv->vscrollbar)
631 gtk_widget_push_composite_child ();
632 priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
633 gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
634 gtk_widget_pop_composite_child ();
636 gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
637 g_object_ref (priv->vscrollbar);
638 gtk_widget_show (priv->vscrollbar);
642 GtkAdjustment *old_adjustment;
644 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
645 if (old_adjustment == vadjustment)
648 g_signal_handlers_disconnect_by_func (old_adjustment,
649 gtk_scrolled_window_adjustment_changed,
651 gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
654 vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
655 g_signal_connect (vadjustment,
657 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
659 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
661 child = gtk_bin_get_child (bin);
662 if (GTK_IS_SCROLLABLE (child))
663 gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
665 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
669 * gtk_scrolled_window_get_hadjustment:
670 * @scrolled_window: a #GtkScrolledWindow
672 * Returns the horizontal scrollbar's adjustment, used to connect the
673 * horizontal scrollbar to the child widget's horizontal scroll
676 * Returns: (transfer none): the horizontal #GtkAdjustment
679 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
681 GtkScrolledWindowPrivate *priv;
683 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
685 priv = scrolled_window->priv;
687 return (priv->hscrollbar ?
688 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
693 * gtk_scrolled_window_get_vadjustment:
694 * @scrolled_window: a #GtkScrolledWindow
696 * Returns the vertical scrollbar's adjustment, used to connect the
697 * vertical scrollbar to the child widget's vertical scroll functionality.
699 * Returns: (transfer none): the vertical #GtkAdjustment
702 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
704 GtkScrolledWindowPrivate *priv;
706 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
708 priv = scrolled_window->priv;
710 return (priv->vscrollbar ?
711 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
716 * gtk_scrolled_window_get_hscrollbar:
717 * @scrolled_window: a #GtkScrolledWindow
719 * Returns the horizontal scrollbar of @scrolled_window.
721 * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
722 * or %NULL if it does not have one.
727 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
729 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
731 return scrolled_window->priv->hscrollbar;
735 * gtk_scrolled_window_get_vscrollbar:
736 * @scrolled_window: a #GtkScrolledWindow
738 * Returns the vertical scrollbar of @scrolled_window.
740 * Returns: (transfer none): the vertical scrollbar of the scrolled window,
741 * or %NULL if it does not have one.
746 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
748 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
750 return scrolled_window->priv->vscrollbar;
754 * gtk_scrolled_window_set_policy:
755 * @scrolled_window: a #GtkScrolledWindow
756 * @hscrollbar_policy: policy for horizontal bar
757 * @vscrollbar_policy: policy for vertical bar
759 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
761 * The policy determines when the scrollbar should appear; it is a value
762 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
763 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
764 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
765 * if needed (that is, if the slider part of the bar would be smaller
766 * than the trough - the display is larger than the page size).
769 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
770 GtkPolicyType hscrollbar_policy,
771 GtkPolicyType vscrollbar_policy)
773 GtkScrolledWindowPrivate *priv;
774 GObject *object = G_OBJECT (scrolled_window);
776 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
778 priv = scrolled_window->priv;
780 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
781 (priv->vscrollbar_policy != vscrollbar_policy))
783 priv->hscrollbar_policy = hscrollbar_policy;
784 priv->vscrollbar_policy = vscrollbar_policy;
786 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
788 g_object_freeze_notify (object);
789 g_object_notify (object, "hscrollbar-policy");
790 g_object_notify (object, "vscrollbar-policy");
791 g_object_thaw_notify (object);
796 * gtk_scrolled_window_get_policy:
797 * @scrolled_window: a #GtkScrolledWindow
798 * @hscrollbar_policy: (out) (allow-none): location to store the policy
799 * for the horizontal scrollbar, or %NULL.
800 * @vscrollbar_policy: (out) (allow-none): location to store the policy
801 * for the vertical scrollbar, or %NULL.
803 * Retrieves the current policy values for the horizontal and vertical
804 * scrollbars. See gtk_scrolled_window_set_policy().
807 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
808 GtkPolicyType *hscrollbar_policy,
809 GtkPolicyType *vscrollbar_policy)
811 GtkScrolledWindowPrivate *priv;
813 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
815 priv = scrolled_window->priv;
817 if (hscrollbar_policy)
818 *hscrollbar_policy = priv->hscrollbar_policy;
819 if (vscrollbar_policy)
820 *vscrollbar_policy = priv->vscrollbar_policy;
824 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
826 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
827 GtkSettings *settings;
829 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
831 if (priv->window_placement_set || settings == NULL)
832 priv->real_window_placement = priv->window_placement;
834 g_object_get (settings,
835 "gtk-scrolled-window-placement",
836 &priv->real_window_placement,
841 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
842 GtkCornerType window_placement)
844 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
846 if (priv->window_placement != window_placement)
848 priv->window_placement = window_placement;
850 gtk_scrolled_window_update_real_placement (scrolled_window);
851 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
853 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
858 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
859 gboolean placement_set,
860 gboolean emit_resize)
862 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
864 if (priv->window_placement_set != placement_set)
866 priv->window_placement_set = placement_set;
868 gtk_scrolled_window_update_real_placement (scrolled_window);
870 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
872 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
877 * gtk_scrolled_window_set_placement:
878 * @scrolled_window: a #GtkScrolledWindow
879 * @window_placement: position of the child window
881 * Sets the placement of the contents with respect to the scrollbars
882 * for the scrolled window.
884 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
885 * in the top left, with the scrollbars underneath and to the right.
886 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
887 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
889 * See also gtk_scrolled_window_get_placement() and
890 * gtk_scrolled_window_unset_placement().
893 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
894 GtkCornerType window_placement)
896 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
898 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
899 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
903 * gtk_scrolled_window_get_placement:
904 * @scrolled_window: a #GtkScrolledWindow
906 * Gets the placement of the contents with respect to the scrollbars
907 * for the scrolled window. See gtk_scrolled_window_set_placement().
909 * Return value: the current placement value.
911 * See also gtk_scrolled_window_set_placement() and
912 * gtk_scrolled_window_unset_placement().
915 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
917 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
919 return scrolled_window->priv->window_placement;
923 * gtk_scrolled_window_unset_placement:
924 * @scrolled_window: a #GtkScrolledWindow
926 * Unsets the placement of the contents with respect to the scrollbars
927 * for the scrolled window. If no window placement is set for a scrolled
928 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
930 * See also gtk_scrolled_window_set_placement() and
931 * gtk_scrolled_window_get_placement().
936 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
938 GtkScrolledWindowPrivate *priv;
940 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
942 priv = scrolled_window->priv;
944 if (priv->window_placement_set)
946 priv->window_placement_set = FALSE;
948 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
950 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
955 * gtk_scrolled_window_set_shadow_type:
956 * @scrolled_window: a #GtkScrolledWindow
957 * @type: kind of shadow to draw around scrolled window contents
959 * Changes the type of shadow drawn around the contents of
964 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
967 GtkScrolledWindowPrivate *priv;
969 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
970 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
972 priv = scrolled_window->priv;
974 if (priv->shadow_type != type)
976 priv->shadow_type = type;
978 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
979 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
981 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
983 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
988 * gtk_scrolled_window_get_shadow_type:
989 * @scrolled_window: a #GtkScrolledWindow
991 * Gets the shadow type of the scrolled window. See
992 * gtk_scrolled_window_set_shadow_type().
994 * Return value: the current shadow type
997 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
999 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
1001 return scrolled_window->priv->shadow_type;
1005 gtk_scrolled_window_destroy (GtkWidget *widget)
1007 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1008 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1010 if (priv->hscrollbar)
1012 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1013 gtk_scrolled_window_adjustment_changed,
1015 gtk_widget_unparent (priv->hscrollbar);
1016 gtk_widget_destroy (priv->hscrollbar);
1017 g_object_unref (priv->hscrollbar);
1018 priv->hscrollbar = NULL;
1020 if (priv->vscrollbar)
1022 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1023 gtk_scrolled_window_adjustment_changed,
1025 gtk_widget_unparent (priv->vscrollbar);
1026 gtk_widget_destroy (priv->vscrollbar);
1027 g_object_unref (priv->vscrollbar);
1028 priv->vscrollbar = NULL;
1031 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1035 gtk_scrolled_window_set_property (GObject *object,
1037 const GValue *value,
1040 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1041 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1045 case PROP_HADJUSTMENT:
1046 gtk_scrolled_window_set_hadjustment (scrolled_window,
1047 g_value_get_object (value));
1049 case PROP_VADJUSTMENT:
1050 gtk_scrolled_window_set_vadjustment (scrolled_window,
1051 g_value_get_object (value));
1053 case PROP_HSCROLLBAR_POLICY:
1054 gtk_scrolled_window_set_policy (scrolled_window,
1055 g_value_get_enum (value),
1056 priv->vscrollbar_policy);
1058 case PROP_VSCROLLBAR_POLICY:
1059 gtk_scrolled_window_set_policy (scrolled_window,
1060 priv->hscrollbar_policy,
1061 g_value_get_enum (value));
1063 case PROP_WINDOW_PLACEMENT:
1064 gtk_scrolled_window_set_placement_internal (scrolled_window,
1065 g_value_get_enum (value));
1067 case PROP_WINDOW_PLACEMENT_SET:
1068 gtk_scrolled_window_set_placement_set (scrolled_window,
1069 g_value_get_boolean (value),
1072 case PROP_SHADOW_TYPE:
1073 gtk_scrolled_window_set_shadow_type (scrolled_window,
1074 g_value_get_enum (value));
1076 case PROP_MIN_CONTENT_WIDTH:
1077 gtk_scrolled_window_set_min_content_width (scrolled_window,
1078 g_value_get_int (value));
1080 case PROP_MIN_CONTENT_HEIGHT:
1081 gtk_scrolled_window_set_min_content_height (scrolled_window,
1082 g_value_get_int (value));
1085 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1091 gtk_scrolled_window_get_property (GObject *object,
1096 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1097 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1101 case PROP_HADJUSTMENT:
1102 g_value_set_object (value,
1103 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1105 case PROP_VADJUSTMENT:
1106 g_value_set_object (value,
1107 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1109 case PROP_WINDOW_PLACEMENT:
1110 g_value_set_enum (value, priv->window_placement);
1112 case PROP_WINDOW_PLACEMENT_SET:
1113 g_value_set_boolean (value, priv->window_placement_set);
1115 case PROP_SHADOW_TYPE:
1116 g_value_set_enum (value, priv->shadow_type);
1118 case PROP_HSCROLLBAR_POLICY:
1119 g_value_set_enum (value, priv->hscrollbar_policy);
1121 case PROP_VSCROLLBAR_POLICY:
1122 g_value_set_enum (value, priv->vscrollbar_policy);
1124 case PROP_MIN_CONTENT_WIDTH:
1125 g_value_set_int (value, priv->min_content_width);
1127 case PROP_MIN_CONTENT_HEIGHT:
1128 g_value_set_int (value, priv->min_content_height);
1131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1137 traverse_container (GtkWidget *widget,
1140 if (GTK_IS_SCROLLED_WINDOW (widget))
1142 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1143 gtk_widget_queue_resize (widget);
1145 else if (GTK_IS_CONTAINER (widget))
1146 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1150 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1154 list = gtk_window_list_toplevels ();
1156 for (l = list; l; l = l->next)
1157 gtk_container_forall (GTK_CONTAINER (l->data),
1158 traverse_container, NULL);
1164 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1165 GdkScreen *previous_screen)
1167 GtkSettings *settings;
1168 guint window_placement_connection;
1170 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1172 if (!gtk_widget_has_screen (widget))
1175 settings = gtk_widget_get_settings (widget);
1177 window_placement_connection =
1178 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1179 "gtk-scrolled-window-connection"));
1181 if (window_placement_connection)
1184 window_placement_connection =
1185 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1186 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1187 g_object_set_data (G_OBJECT (settings),
1188 I_("gtk-scrolled-window-connection"),
1189 GUINT_TO_POINTER (window_placement_connection));
1193 gtk_scrolled_window_draw (GtkWidget *widget,
1196 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1197 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1199 if (priv->shadow_type != GTK_SHADOW_NONE)
1201 GtkAllocation relative_allocation;
1202 GtkStyleContext *context;
1203 gboolean scrollbars_within_bevel;
1205 context = gtk_widget_get_style_context (widget);
1207 gtk_style_context_save (context);
1208 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1210 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1212 if (!scrollbars_within_bevel)
1214 GtkStateFlags state;
1215 GtkBorder padding, border;
1217 state = gtk_widget_get_state_flags (widget);
1218 gtk_style_context_get_padding (context, state, &padding);
1219 gtk_style_context_get_border (context, state, &border);
1221 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1223 relative_allocation.x -= padding.left + border.left;
1224 relative_allocation.y -= padding.top + border.top;
1225 relative_allocation.width += padding.left + padding.right + border.left + border.right;
1226 relative_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1230 relative_allocation.x = 0;
1231 relative_allocation.y = 0;
1232 relative_allocation.width = gtk_widget_get_allocated_width (widget);
1233 relative_allocation.height = gtk_widget_get_allocated_height (widget);
1236 gtk_render_frame (context, cr,
1237 relative_allocation.x,
1238 relative_allocation.y,
1239 relative_allocation.width,
1240 relative_allocation.height);
1242 gtk_style_context_restore (context);
1245 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1251 gtk_scrolled_window_forall (GtkContainer *container,
1252 gboolean include_internals,
1253 GtkCallback callback,
1254 gpointer callback_data)
1256 GtkScrolledWindowPrivate *priv;
1257 GtkScrolledWindow *scrolled_window;
1259 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1260 g_return_if_fail (callback != NULL);
1262 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1266 if (include_internals)
1268 scrolled_window = GTK_SCROLLED_WINDOW (container);
1269 priv = scrolled_window->priv;
1271 if (priv->vscrollbar)
1272 callback (priv->vscrollbar, callback_data);
1273 if (priv->hscrollbar)
1274 callback (priv->hscrollbar, callback_data);
1279 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1280 GtkScrollType scroll,
1281 gboolean horizontal)
1283 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1284 GtkAdjustment *adjustment = NULL;
1288 case GTK_SCROLL_STEP_UP:
1289 scroll = GTK_SCROLL_STEP_BACKWARD;
1292 case GTK_SCROLL_STEP_DOWN:
1293 scroll = GTK_SCROLL_STEP_FORWARD;
1296 case GTK_SCROLL_STEP_LEFT:
1297 scroll = GTK_SCROLL_STEP_BACKWARD;
1300 case GTK_SCROLL_STEP_RIGHT:
1301 scroll = GTK_SCROLL_STEP_FORWARD;
1304 case GTK_SCROLL_PAGE_UP:
1305 scroll = GTK_SCROLL_PAGE_BACKWARD;
1308 case GTK_SCROLL_PAGE_DOWN:
1309 scroll = GTK_SCROLL_PAGE_FORWARD;
1312 case GTK_SCROLL_PAGE_LEFT:
1313 scroll = GTK_SCROLL_STEP_BACKWARD;
1316 case GTK_SCROLL_PAGE_RIGHT:
1317 scroll = GTK_SCROLL_STEP_FORWARD;
1320 case GTK_SCROLL_STEP_BACKWARD:
1321 case GTK_SCROLL_STEP_FORWARD:
1322 case GTK_SCROLL_PAGE_BACKWARD:
1323 case GTK_SCROLL_PAGE_FORWARD:
1324 case GTK_SCROLL_START:
1325 case GTK_SCROLL_END:
1328 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1332 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1333 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1338 if (priv->hscrollbar)
1339 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1343 if (priv->vscrollbar)
1344 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1349 gdouble value = gtk_adjustment_get_value (adjustment);
1353 case GTK_SCROLL_STEP_FORWARD:
1354 value += gtk_adjustment_get_step_increment (adjustment);
1356 case GTK_SCROLL_STEP_BACKWARD:
1357 value -= gtk_adjustment_get_step_increment (adjustment);
1359 case GTK_SCROLL_PAGE_FORWARD:
1360 value += gtk_adjustment_get_page_increment (adjustment);
1362 case GTK_SCROLL_PAGE_BACKWARD:
1363 value -= gtk_adjustment_get_page_increment (adjustment);
1365 case GTK_SCROLL_START:
1366 value = gtk_adjustment_get_lower (adjustment);
1368 case GTK_SCROLL_END:
1369 value = gtk_adjustment_get_upper (adjustment);
1372 g_assert_not_reached ();
1376 gtk_adjustment_set_value (adjustment, value);
1385 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1386 GtkDirectionType direction_type)
1388 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1389 GtkWidget *toplevel;
1391 /* Focus out of the scrolled window entirely. We do this by setting
1392 * a flag, then propagating the focus motion to the notebook.
1394 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1395 if (!gtk_widget_is_toplevel (toplevel))
1398 g_object_ref (scrolled_window);
1400 priv->focus_out = TRUE;
1401 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1402 priv->focus_out = FALSE;
1404 g_object_unref (scrolled_window);
1408 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1409 GtkAllocation *allocation)
1411 GtkAllocation widget_allocation;
1412 GtkScrolledWindow *scrolled_window;
1413 GtkScrolledWindowPrivate *priv;
1418 g_return_if_fail (widget != NULL);
1419 g_return_if_fail (allocation != NULL);
1421 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1422 priv = scrolled_window->priv;
1424 /* Get possible scrollbar dimensions */
1425 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1426 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1427 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1429 /* Subtract some things from our available allocation size */
1433 if (priv->shadow_type != GTK_SHADOW_NONE)
1435 GtkStyleContext *context;
1436 GtkStateFlags state;
1437 GtkBorder padding, border;
1439 context = gtk_widget_get_style_context (widget);
1440 state = gtk_widget_get_state_flags (widget);
1442 gtk_style_context_save (context);
1443 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1445 gtk_style_context_get_border (context, state, &border);
1446 gtk_style_context_get_padding (context, state, &padding);
1448 allocation->x += padding.left + border.left;
1449 allocation->y += padding.top + border.top;
1451 gtk_style_context_restore (context);
1454 gtk_widget_get_allocation (widget, &widget_allocation);
1455 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1456 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1458 if (priv->vscrollbar_visible)
1462 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1465 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1466 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1468 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1469 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1470 allocation->x += (sb_width + sb_spacing);
1472 allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1474 if (priv->hscrollbar_visible)
1477 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1478 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1479 allocation->y += (sb_height + sb_spacing);
1481 allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1486 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1487 GtkAllocation *relative_allocation)
1489 GtkWidget *widget = GTK_WIDGET (swindow), *child;
1490 GtkAllocation allocation;
1491 GtkAllocation child_allocation;
1493 child = gtk_bin_get_child (GTK_BIN (widget));
1495 gtk_widget_get_allocation (widget, &allocation);
1497 gtk_scrolled_window_relative_allocation (widget, relative_allocation);
1498 child_allocation.x = relative_allocation->x + allocation.x;
1499 child_allocation.y = relative_allocation->y + allocation.y;
1500 child_allocation.width = relative_allocation->width;
1501 child_allocation.height = relative_allocation->height;
1503 gtk_widget_size_allocate (child, &child_allocation);
1507 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1508 GtkAllocation *allocation)
1510 GtkScrolledWindow *scrolled_window;
1511 GtkScrolledWindowPrivate *priv;
1512 GtkStyleContext *context;
1513 GtkStateFlags state;
1514 GtkBorder padding, border;
1516 GtkAllocation relative_allocation;
1517 GtkAllocation child_allocation;
1519 gboolean scrollbars_within_bevel;
1524 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1525 g_return_if_fail (allocation != NULL);
1527 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1528 bin = GTK_BIN (scrolled_window);
1529 priv = scrolled_window->priv;
1531 /* Get possible scrollbar dimensions */
1532 sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1533 gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1534 gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1536 context = gtk_widget_get_style_context (widget);
1537 state = gtk_widget_get_state_flags (widget);
1539 gtk_style_context_save (context);
1540 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1542 gtk_style_context_get_padding (context, state, &padding);
1543 gtk_style_context_get_border (context, state, &border);
1545 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1547 gtk_widget_set_allocation (widget, allocation);
1549 gtk_style_context_restore (context);
1551 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1552 priv->hscrollbar_visible = TRUE;
1553 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1554 priv->hscrollbar_visible = FALSE;
1555 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1556 priv->vscrollbar_visible = TRUE;
1557 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1558 priv->vscrollbar_visible = FALSE;
1560 child = gtk_bin_get_child (bin);
1561 if (child && gtk_widget_get_visible (child))
1563 gint child_scroll_width;
1564 gint child_scroll_height;
1565 gboolean previous_hvis;
1566 gboolean previous_vvis;
1569 /* Determine scrollbar visibility first via hfw apis */
1570 if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1572 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1573 gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
1575 gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
1577 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1579 /* First try without a vertical scrollbar if the content will fit the height
1580 * given the extra width of the scrollbar */
1581 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1582 gtk_widget_get_preferred_height_for_width (child,
1583 MAX (allocation->width, child_scroll_width),
1584 &child_scroll_height, NULL);
1586 gtk_widget_get_preferred_height_for_width (child,
1587 MAX (allocation->width, child_scroll_width),
1588 NULL, &child_scroll_height);
1590 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1592 /* Does the content height fit the allocation height ? */
1593 priv->vscrollbar_visible = child_scroll_height > allocation->height;
1595 /* Does the content width fit the allocation with minus a possible scrollbar ? */
1596 priv->hscrollbar_visible =
1597 child_scroll_width > allocation->width -
1598 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1600 /* Now that we've guessed the hscrollbar, does the content height fit
1601 * the possible new allocation height ? */
1602 priv->vscrollbar_visible =
1603 child_scroll_height > allocation->height -
1604 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1606 /* Now that we've guessed the vscrollbar, does the content width fit
1607 * the possible new allocation width ? */
1608 priv->hscrollbar_visible =
1609 child_scroll_width > allocation->width -
1610 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1612 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1614 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1615 priv->vscrollbar_visible = child_scroll_height > allocation->height -
1616 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1619 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1621 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1623 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1624 priv->hscrollbar_visible =
1625 child_scroll_width > allocation->width -
1626 (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1628 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1631 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1633 if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1634 gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1636 gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1638 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1640 /* First try without a horizontal scrollbar if the content will fit the width
1641 * given the extra height of the scrollbar */
1642 if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1643 gtk_widget_get_preferred_width_for_height (child,
1644 MAX (allocation->height, child_scroll_height),
1645 &child_scroll_width, NULL);
1647 gtk_widget_get_preferred_width_for_height (child,
1648 MAX (allocation->height, child_scroll_height),
1649 NULL, &child_scroll_width);
1651 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1653 /* Does the content width fit the allocation width ? */
1654 priv->hscrollbar_visible = child_scroll_width > allocation->width;
1656 /* Does the content height fit the allocation with minus a possible scrollbar ? */
1657 priv->vscrollbar_visible =
1658 child_scroll_height > allocation->height -
1659 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1661 /* Now that we've guessed the vscrollbar, does the content width fit
1662 * the possible new allocation width ? */
1663 priv->hscrollbar_visible =
1664 child_scroll_width > allocation->width -
1665 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1667 /* Now that we've guessed the hscrollbar, does the content height fit
1668 * the possible new allocation height ? */
1669 priv->vscrollbar_visible =
1670 child_scroll_height > allocation->height -
1671 (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1673 else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1675 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1676 priv->hscrollbar_visible = child_scroll_width > allocation->width -
1677 (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1680 else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1682 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1684 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1685 priv->vscrollbar_visible =
1686 child_scroll_height > allocation->height -
1687 (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1689 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1693 /* Now after guessing scrollbar visibility; fall back on the allocation loop which
1694 * observes the adjustments to detect scrollbar visibility and also avoids
1695 * infinite recursion
1699 previous_hvis = priv->hscrollbar_visible;
1700 previous_vvis = priv->vscrollbar_visible;
1701 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1703 /* Explicitly force scrollbar visibility checks.
1705 * Since we make a guess above, the child might not decide to update the adjustments
1706 * if they logically did not change since the last configuration
1708 if (priv->hscrollbar)
1709 gtk_scrolled_window_adjustment_changed
1710 (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)), scrolled_window);
1712 if (priv->vscrollbar)
1713 gtk_scrolled_window_adjustment_changed
1714 (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)), scrolled_window);
1716 /* If, after the first iteration, the hscrollbar and the
1717 * vscrollbar flip visiblity... or if one of the scrollbars flip
1718 * on each itteration indefinitly/infinitely, then we just need both
1722 previous_hvis != priv->hscrollbar_visible &&
1723 previous_vvis != priv->vscrollbar_visible) || count > 3)
1725 priv->hscrollbar_visible = TRUE;
1726 priv->vscrollbar_visible = TRUE;
1728 gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1735 while (previous_hvis != priv->hscrollbar_visible ||
1736 previous_vvis != priv->vscrollbar_visible);
1740 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1741 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1742 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1745 if (priv->hscrollbar_visible)
1747 if (!gtk_widget_get_visible (priv->hscrollbar))
1748 gtk_widget_show (priv->hscrollbar);
1750 child_allocation.x = relative_allocation.x;
1751 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1752 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1753 child_allocation.y = (relative_allocation.y +
1754 relative_allocation.height +
1756 (priv->shadow_type == GTK_SHADOW_NONE ?
1757 0 : padding.top + border.top));
1759 child_allocation.y = 0;
1761 child_allocation.width = relative_allocation.width;
1762 child_allocation.height = sb_height;
1763 child_allocation.x += allocation->x;
1764 child_allocation.y += allocation->y;
1766 if (priv->shadow_type != GTK_SHADOW_NONE)
1768 if (!scrollbars_within_bevel)
1770 child_allocation.x -= padding.left + border.left;
1771 child_allocation.width += padding.left + padding.right + border.left + border.right;
1773 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1774 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1776 child_allocation.y -= padding.top + border.top;
1780 child_allocation.y += padding.top + border.top;
1784 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1786 else if (gtk_widget_get_visible (priv->hscrollbar))
1787 gtk_widget_hide (priv->hscrollbar);
1789 if (priv->vscrollbar_visible)
1791 if (!gtk_widget_get_visible (priv->vscrollbar))
1792 gtk_widget_show (priv->vscrollbar);
1794 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1795 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1796 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1797 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1798 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1799 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1800 child_allocation.x = (relative_allocation.x +
1801 relative_allocation.width +
1803 (priv->shadow_type == GTK_SHADOW_NONE ?
1804 0 : padding.left + border.left));
1806 child_allocation.x = 0;
1808 child_allocation.y = relative_allocation.y;
1809 child_allocation.width = sb_width;
1810 child_allocation.height = relative_allocation.height;
1811 child_allocation.x += allocation->x;
1812 child_allocation.y += allocation->y;
1814 if (priv->shadow_type != GTK_SHADOW_NONE)
1816 if (!scrollbars_within_bevel)
1818 child_allocation.y -= padding.top + border.top;
1819 child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1821 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1822 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1824 child_allocation.x -= padding.left + border.left;
1828 child_allocation.x += padding.left + border.left;
1832 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1834 else if (gtk_widget_get_visible (priv->vscrollbar))
1835 gtk_widget_hide (priv->vscrollbar);
1839 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1840 GdkEventScroll *event)
1842 GtkScrolledWindowPrivate *priv;
1843 GtkScrolledWindow *scrolled_window;
1846 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1847 g_return_val_if_fail (event != NULL, FALSE);
1849 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1850 priv = scrolled_window->priv;
1852 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1853 range = priv->vscrollbar;
1855 range = priv->hscrollbar;
1857 if (range && gtk_widget_get_visible (range))
1859 GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (range));
1862 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1864 gtk_adjustment_set_value (adjustment, gtk_adjustment_get_value (adjustment) + delta);
1873 gtk_scrolled_window_focus (GtkWidget *widget,
1874 GtkDirectionType direction)
1876 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1877 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1879 gboolean had_focus_child;
1881 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1883 if (priv->focus_out)
1885 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1889 if (gtk_widget_is_focus (widget))
1892 /* We only put the scrolled window itself in the focus chain if it
1893 * isn't possible to focus any children.
1895 child = gtk_bin_get_child (GTK_BIN (widget));
1898 if (gtk_widget_child_focus (child, direction))
1902 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1904 gtk_widget_grab_focus (widget);
1912 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1915 GtkScrolledWindowPrivate *priv;
1916 GtkScrolledWindow *scrolled_window;
1918 g_return_if_fail (adjustment != NULL);
1919 g_return_if_fail (data != NULL);
1921 scrolled_window = GTK_SCROLLED_WINDOW (data);
1922 priv = scrolled_window->priv;
1924 if (priv->hscrollbar &&
1925 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1927 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1931 visible = priv->hscrollbar_visible;
1932 priv->hscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1933 gtk_adjustment_get_page_size (adjustment));
1935 if (priv->hscrollbar_visible != visible)
1936 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1939 else if (priv->vscrollbar &&
1940 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1942 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1946 visible = priv->vscrollbar_visible;
1947 priv->vscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1948 gtk_adjustment_get_page_size (adjustment));
1950 if (priv->vscrollbar_visible != visible)
1951 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1957 gtk_scrolled_window_add (GtkContainer *container,
1960 GtkScrolledWindowPrivate *priv;
1961 GtkScrolledWindow *scrolled_window;
1963 GtkWidget *child_widget;
1964 GtkAdjustment *hadj, *vadj;
1966 bin = GTK_BIN (container);
1967 child_widget = gtk_bin_get_child (bin);
1968 g_return_if_fail (child_widget == NULL);
1970 scrolled_window = GTK_SCROLLED_WINDOW (container);
1971 priv = scrolled_window->priv;
1973 _gtk_bin_set_child (bin, child);
1974 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1976 hadj = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1977 vadj = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1979 if (GTK_IS_SCROLLABLE (child))
1980 g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1982 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1983 "use gtk_scrolled_window_add_with_viewport() instead");
1987 gtk_scrolled_window_remove (GtkContainer *container,
1990 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1991 g_return_if_fail (child != NULL);
1992 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1994 g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
1996 /* chain parent class handler to remove child */
1997 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
2001 * gtk_scrolled_window_add_with_viewport:
2002 * @scrolled_window: a #GtkScrolledWindow
2003 * @child: the widget you want to scroll
2005 * Used to add children without native scrolling capabilities. This
2006 * is simply a convenience function; it is equivalent to adding the
2007 * unscrollable child to a viewport, then adding the viewport to the
2008 * scrolled window. If a child has native scrolling, use
2009 * gtk_container_add() instead of this function.
2011 * The viewport scrolls the child by moving its #GdkWindow, and takes
2012 * the size of the child to be the size of its toplevel #GdkWindow.
2013 * This will be very wrong for most widgets that support native scrolling;
2014 * for example, if you add a widget such as #GtkTreeView with a viewport,
2015 * the whole widget will scroll, including the column headings. Thus,
2016 * widgets with native scrolling support should not be used with the
2017 * #GtkViewport proxy.
2019 * A widget supports scrolling natively if it implements the
2020 * #GtkScrollable interface.
2023 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
2027 GtkWidget *viewport;
2028 GtkWidget *child_widget;
2030 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2031 g_return_if_fail (GTK_IS_WIDGET (child));
2032 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
2034 bin = GTK_BIN (scrolled_window);
2035 child_widget = gtk_bin_get_child (bin);
2039 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2040 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2042 viewport = child_widget;
2047 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2048 gtk_scrolled_window_get_vadjustment (scrolled_window));
2049 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2052 gtk_widget_show (viewport);
2053 gtk_container_add (GTK_CONTAINER (viewport), child);
2057 * _gtk_scrolled_window_get_spacing:
2058 * @scrolled_window: a scrolled window
2060 * Gets the spacing between the scrolled window's scrollbars and
2061 * the scrolled widget. Used by GtkCombo
2063 * Return value: the spacing, in pixels.
2066 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2068 GtkScrolledWindowClass *class;
2070 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2072 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2074 if (class->scrollbar_spacing >= 0)
2075 return class->scrollbar_spacing;
2078 gint scrollbar_spacing;
2080 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2081 "scrollbar-spacing", &scrollbar_spacing,
2084 return scrollbar_spacing;
2090 gtk_scrolled_window_get_preferred_size (GtkWidget *widget,
2091 GtkOrientation orientation,
2095 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2096 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2097 GtkBin *bin = GTK_BIN (scrolled_window);
2100 gint scrollbar_spacing;
2101 GtkRequisition hscrollbar_requisition;
2102 GtkRequisition vscrollbar_requisition;
2103 GtkRequisition minimum_req, natural_req;
2105 gint min_child_size, nat_child_size;
2107 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2111 minimum_req.width = 0;
2112 minimum_req.height = 0;
2113 natural_req.width = 0;
2114 natural_req.height = 0;
2116 gtk_widget_get_preferred_size (priv->hscrollbar,
2117 &hscrollbar_requisition, NULL);
2118 gtk_widget_get_preferred_size (priv->vscrollbar,
2119 &vscrollbar_requisition, NULL);
2121 child = gtk_bin_get_child (bin);
2122 if (child && gtk_widget_get_visible (child))
2124 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2126 gtk_widget_get_preferred_width (child,
2130 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2132 minimum_req.width += min_child_size;
2133 natural_req.width += nat_child_size;
2137 gint min_content_width = priv->min_content_width;
2139 if (min_content_width >= 0)
2141 minimum_req.width = MAX (minimum_req.width, min_content_width);
2142 natural_req.width = MAX (natural_req.width, min_content_width);
2147 minimum_req.width += vscrollbar_requisition.width;
2148 natural_req.width += vscrollbar_requisition.width;
2152 else /* GTK_ORIENTATION_VERTICAL */
2154 gtk_widget_get_preferred_height (child,
2158 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2160 minimum_req.height += min_child_size;
2161 natural_req.height += nat_child_size;
2165 gint min_content_height = priv->min_content_height;
2167 if (min_content_height >= 0)
2169 minimum_req.height = MAX (minimum_req.height, min_content_height);
2170 natural_req.height = MAX (natural_req.height, min_content_height);
2175 minimum_req.height += vscrollbar_requisition.height;
2176 natural_req.height += vscrollbar_requisition.height;
2182 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2183 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2185 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2186 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2187 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2188 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2191 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2192 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2194 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2195 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2196 if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2197 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2200 minimum_req.width += MAX (0, extra_width);
2201 minimum_req.height += MAX (0, extra_height);
2202 natural_req.width += MAX (0, extra_width);
2203 natural_req.height += MAX (0, extra_height);
2205 if (priv->shadow_type != GTK_SHADOW_NONE)
2207 GtkStyleContext *context;
2208 GtkStateFlags state;
2209 GtkBorder padding, border;
2211 context = gtk_widget_get_style_context (GTK_WIDGET (widget));
2212 state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
2214 gtk_style_context_save (context);
2215 gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
2217 gtk_style_context_get_padding (context, state, &padding);
2218 gtk_style_context_get_border (context, state, &border);
2220 minimum_req.width += padding.left + padding.right + border.left + border.right;
2221 minimum_req.height += padding.top + padding.bottom + border.top + border.bottom;
2222 natural_req.width += padding.left + padding.right + border.left + border.right;
2223 natural_req.height += padding.top + padding.bottom + border.top + border.bottom;
2225 gtk_style_context_restore (context);
2228 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2231 *minimum_size = minimum_req.width;
2233 *natural_size = natural_req.width;
2238 *minimum_size = minimum_req.height;
2240 *natural_size = natural_req.height;
2245 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2249 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2253 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2257 gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2261 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2263 gint *minimum_height,
2264 gint *natural_height)
2266 g_return_if_fail (GTK_IS_WIDGET (widget));
2268 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2272 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2274 gint *minimum_width,
2275 gint *natural_width)
2277 g_return_if_fail (GTK_IS_WIDGET (widget));
2279 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2283 * gtk_scrolled_window_get_min_content_width:
2284 * @scrolled_window: a #GtkScrolledWindow
2286 * Gets the minimum content width of @scrolled_window, or -1 if not set.
2288 * Returns: the minimum content width
2293 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2295 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2297 return scrolled_window->priv->min_content_width;
2301 * gtk_scrolled_window_set_min_content_width:
2302 * @scrolled_window: a #GtkScrolledWindow
2303 * @width: the minimal content width
2305 * Sets the minimum width that @scrolled_window should keep visible.
2306 * Note that this can and (usually will) be smaller than the minimum
2307 * size of the content.
2312 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2315 GtkScrolledWindowPrivate *priv;
2317 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2319 priv = scrolled_window->priv;
2321 if (priv->min_content_width != width)
2323 priv->min_content_width = width;
2325 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2327 g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2332 * gtk_scrolled_window_get_min_content_height:
2333 * @scrolled_window: a #GtkScrolledWindow
2335 * Gets the minimal content height of @scrolled_window, or -1 if not set.
2337 * Returns: the minimal content height
2342 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2344 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2346 return scrolled_window->priv->min_content_height;
2350 * gtk_scrolled_window_set_min_content_height:
2351 * @scrolled_window: a #GtkScrolledWindow
2352 * @height: the minimal content height
2354 * Sets the minimum height that @scrolled_window should keep visible.
2355 * Note that this can and (usually will) be smaller than the minimum
2356 * size of the content.
2361 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2364 GtkScrolledWindowPrivate *priv;
2366 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2368 priv = scrolled_window->priv;
2370 if (priv->min_content_height != height)
2372 priv->min_content_height = height;
2374 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2376 g_object_notify (G_OBJECT (scrolled_window), "min-content-height");