]> Pileus Git - ~andy/gtk/blob - gtk/gtkscrolledwindow.c
a86ffd966b15145ec67017a45d531e143650ceb2
[~andy/gtk] / gtk / gtkscrolledwindow.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #include "config.h"
28
29 #include <math.h>
30
31 #include "gtkbindings.h"
32 #include "gtkmarshalers.h"
33 #include "gtkscrollable.h"
34 #include "gtkscrollbar.h"
35 #include "gtkscrolledwindow.h"
36 #include "gtkwindow.h"
37 #include "gtkviewport.h"
38 #include "gtkprivate.h"
39 #include "gtktypebuiltins.h"
40 #include "gtkintl.h"
41 #include "gtkviewport.h"
42 #include "a11y/gtkscrolledwindowaccessible.h"
43
44 /**
45  * SECTION:gtkscrolledwindow
46  * @Short_description: Adds scrollbars to its child widget
47  * @Title: GtkScrolledWindow
48  * @See_also: #GtkScrollable, #GtkViewport, #GtkAdjustment
49  *
50  * #GtkScrolledWindow is a #GtkBin subclass: it's a container
51  * the accepts a single child widget. #GtkScrolledWindow adds scrollbars
52  * to the child widget and optionally draws a beveled frame around the
53  * child widget.
54  *
55  * The scrolled window can work in two ways. Some widgets have native
56  * scrolling support; these widgets implement the #GtkScrollable interface.
57  * Widgets with native scroll support include #GtkTreeView, #GtkTextView,
58  * and #GtkLayout.
59  *
60  * For widgets that lack native scrolling support, the #GtkViewport
61  * widget acts as an adaptor class, implementing scrollability for child
62  * widgets that lack their own scrolling capabilities. Use #GtkViewport
63  * to scroll child widgets such as #GtkGrid, #GtkBox, and so on.
64  *
65  * If a widget has native scrolling abilities, it can be added to the
66  * #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
67  * must first add the widget to a #GtkViewport, then add the #GtkViewport
68  * to the scrolled window. The convenience function
69  * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
70  * ignore the presence of the viewport.
71  *
72  * The position of the scrollbars is controlled by the scroll
73  * adjustments. See #GtkAdjustment for the fields in an adjustment - for
74  * #GtkScrollbar, used by #GtkScrolledWindow, the "value" field
75  * represents the position of the scrollbar, which must be between the
76  * "lower" field and "upper - page_size." The "page_size" field
77  * represents the size of the visible scrollable area. The
78  * "step_increment" and "page_increment" fields are used when the user
79  * asks to step down (using the small stepper arrows) or page down (using
80  * for example the PageDown key).
81  *
82  * If a #GtkScrolledWindow doesn't behave quite as you would like, or
83  * doesn't have exactly the right layout, it's very possible to set up
84  * your own scrolling with #GtkScrollbar and for example a #GtkGrid.
85  */
86
87
88 /* scrolled window policy and size requisition handling:
89  *
90  * gtk size requisition works as follows:
91  *   a widget upon size-request reports the width and height that it finds
92  *   to be best suited to display its contents, including children.
93  *   the width and/or height reported from a widget upon size requisition
94  *   may be overidden by the user by specifying a width and/or height
95  *   other than 0 through gtk_widget_set_size_request().
96  *
97  * a scrolled window needs (for implementing all three policy types) to
98  * request its width and height based on two different rationales.
99  * 1)   the user wants the scrolled window to just fit into the space
100  *      that it gets allocated for a specifc dimension.
101  * 1.1) this does not apply if the user specified a concrete value
102  *      value for that specific dimension by either specifying usize for the
103  *      scrolled window or for its child.
104  * 2)   the user wants the scrolled window to take as much space up as
105  *      is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
106  *
107  * also, kinda obvious:
108  * 3)   a user would certainly not have choosen a scrolled window as a container
109  *      for the child, if the resulting allocation takes up more space than the
110  *      child would have allocated without the scrolled window.
111  *
112  * conclusions:
113  * A) from 1) follows: the scrolled window shouldn't request more space for a
114  *    specifc dimension than is required at minimum.
115  * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
116  *    window (done automatically) or by usize of the child (needs to be checked).
117  * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
118  *    child's dimension.
119  * D) from 3) follows: the scrolled window child's minimum width and minimum height
120  *    under A) at least correspond to the space taken up by its scrollbars.
121  */
122
123 #define DEFAULT_SCROLLBAR_SPACING  3
124
125 struct _GtkScrolledWindowPrivate
126 {
127   GtkWidget     *hscrollbar;
128   GtkWidget     *vscrollbar;
129
130   GtkCornerType  real_window_placement;
131   guint16  shadow_type;
132
133   guint    window_placement_set   : 1;
134   guint    hscrollbar_policy      : 2;
135   guint    vscrollbar_policy      : 2;
136   guint    hscrollbar_visible     : 1;
137   guint    vscrollbar_visible     : 1;
138   guint    window_placement       : 2;
139   guint    focus_out              : 1; /* Flag used by ::move-focus-out implementation */
140
141   gint     min_content_width;
142   gint     min_content_height;
143 };
144
145
146 enum {
147   PROP_0,
148   PROP_HADJUSTMENT,
149   PROP_VADJUSTMENT,
150   PROP_HSCROLLBAR_POLICY,
151   PROP_VSCROLLBAR_POLICY,
152   PROP_WINDOW_PLACEMENT,
153   PROP_WINDOW_PLACEMENT_SET,
154   PROP_SHADOW_TYPE,
155   PROP_MIN_CONTENT_WIDTH,
156   PROP_MIN_CONTENT_HEIGHT
157 };
158
159 /* Signals */
160 enum
161 {
162   SCROLL_CHILD,
163   MOVE_FOCUS_OUT,
164   LAST_SIGNAL
165 };
166
167 static void     gtk_scrolled_window_set_property       (GObject           *object,
168                                                         guint              prop_id,
169                                                         const GValue      *value,
170                                                         GParamSpec        *pspec);
171 static void     gtk_scrolled_window_get_property       (GObject           *object,
172                                                         guint              prop_id,
173                                                         GValue            *value,
174                                                         GParamSpec        *pspec);
175
176 static void     gtk_scrolled_window_destroy            (GtkWidget         *widget);
177 static void     gtk_scrolled_window_screen_changed     (GtkWidget         *widget,
178                                                         GdkScreen         *previous_screen);
179 static gboolean gtk_scrolled_window_draw               (GtkWidget         *widget,
180                                                         cairo_t           *cr);
181 static void     gtk_scrolled_window_size_allocate      (GtkWidget         *widget,
182                                                         GtkAllocation     *allocation);
183 static gboolean gtk_scrolled_window_scroll_event       (GtkWidget         *widget,
184                                                         GdkEventScroll    *event);
185 static gboolean gtk_scrolled_window_focus              (GtkWidget         *widget,
186                                                         GtkDirectionType   direction);
187 static void     gtk_scrolled_window_add                (GtkContainer      *container,
188                                                         GtkWidget         *widget);
189 static void     gtk_scrolled_window_remove             (GtkContainer      *container,
190                                                         GtkWidget         *widget);
191 static void     gtk_scrolled_window_forall             (GtkContainer      *container,
192                                                         gboolean           include_internals,
193                                                         GtkCallback        callback,
194                                                         gpointer           callback_data);
195 static gboolean gtk_scrolled_window_scroll_child       (GtkScrolledWindow *scrolled_window,
196                                                         GtkScrollType      scroll,
197                                                         gboolean           horizontal);
198 static void     gtk_scrolled_window_move_focus_out     (GtkScrolledWindow *scrolled_window,
199                                                         GtkDirectionType   direction_type);
200
201 static void     gtk_scrolled_window_relative_allocation(GtkWidget         *widget,
202                                                         GtkAllocation     *allocation);
203 static void     gtk_scrolled_window_adjustment_changed (GtkAdjustment     *adjustment,
204                                                         gpointer           data);
205
206 static void  gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
207
208 static void  gtk_scrolled_window_get_preferred_width   (GtkWidget           *widget,
209                                                         gint                *minimum_size,
210                                                         gint                *natural_size);
211 static void  gtk_scrolled_window_get_preferred_height  (GtkWidget           *widget,
212                                                         gint                *minimum_size,
213                                                         gint                *natural_size);
214 static void  gtk_scrolled_window_get_preferred_height_for_width  (GtkWidget           *layout,
215                                                         gint                 width,
216                                                         gint                *minimum_height,
217                                                         gint                *natural_height);
218 static void  gtk_scrolled_window_get_preferred_width_for_height  (GtkWidget           *layout,
219                                                         gint                 width,
220                                                         gint                *minimum_height,
221                                                         gint                *natural_height);
222
223 static guint signals[LAST_SIGNAL] = {0};
224
225 G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
226
227
228 static void
229 add_scroll_binding (GtkBindingSet  *binding_set,
230                     guint           keyval,
231                     GdkModifierType mask,
232                     GtkScrollType   scroll,
233                     gboolean        horizontal)
234 {
235   guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
236   
237   gtk_binding_entry_add_signal (binding_set, keyval, mask,
238                                 "scroll-child", 2,
239                                 GTK_TYPE_SCROLL_TYPE, scroll,
240                                 G_TYPE_BOOLEAN, horizontal);
241   gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
242                                 "scroll-child", 2,
243                                 GTK_TYPE_SCROLL_TYPE, scroll,
244                                 G_TYPE_BOOLEAN, horizontal);
245 }
246
247 static void
248 add_tab_bindings (GtkBindingSet    *binding_set,
249                   GdkModifierType   modifiers,
250                   GtkDirectionType  direction)
251 {
252   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
253                                 "move-focus-out", 1,
254                                 GTK_TYPE_DIRECTION_TYPE, direction);
255   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
256                                 "move-focus-out", 1,
257                                 GTK_TYPE_DIRECTION_TYPE, direction);
258 }
259
260 static void
261 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
262 {
263   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
264   GtkWidgetClass *widget_class;
265   GtkContainerClass *container_class;
266   GtkBindingSet *binding_set;
267
268   widget_class = (GtkWidgetClass*) class;
269   container_class = (GtkContainerClass*) class;
270
271   gobject_class->set_property = gtk_scrolled_window_set_property;
272   gobject_class->get_property = gtk_scrolled_window_get_property;
273
274   widget_class->destroy = gtk_scrolled_window_destroy;
275   widget_class->screen_changed = gtk_scrolled_window_screen_changed;
276   widget_class->draw = gtk_scrolled_window_draw;
277   widget_class->size_allocate = gtk_scrolled_window_size_allocate;
278   widget_class->scroll_event = gtk_scrolled_window_scroll_event;
279   widget_class->focus = gtk_scrolled_window_focus;
280   widget_class->get_preferred_width = gtk_scrolled_window_get_preferred_width;
281   widget_class->get_preferred_height = gtk_scrolled_window_get_preferred_height;
282   widget_class->get_preferred_height_for_width = gtk_scrolled_window_get_preferred_height_for_width;
283   widget_class->get_preferred_width_for_height = gtk_scrolled_window_get_preferred_width_for_height;
284
285   container_class->add = gtk_scrolled_window_add;
286   container_class->remove = gtk_scrolled_window_remove;
287   container_class->forall = gtk_scrolled_window_forall;
288   gtk_container_class_handle_border_width (container_class);
289
290   class->scrollbar_spacing = -1;
291
292   class->scroll_child = gtk_scrolled_window_scroll_child;
293   class->move_focus_out = gtk_scrolled_window_move_focus_out;
294   
295   g_object_class_install_property (gobject_class,
296                                    PROP_HADJUSTMENT,
297                                    g_param_spec_object ("hadjustment",
298                                                         P_("Horizontal Adjustment"),
299                                                         P_("The GtkAdjustment for the horizontal position"),
300                                                         GTK_TYPE_ADJUSTMENT,
301                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
302   g_object_class_install_property (gobject_class,
303                                    PROP_VADJUSTMENT,
304                                    g_param_spec_object ("vadjustment",
305                                                         P_("Vertical Adjustment"),
306                                                         P_("The GtkAdjustment for the vertical position"),
307                                                         GTK_TYPE_ADJUSTMENT,
308                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
309   g_object_class_install_property (gobject_class,
310                                    PROP_HSCROLLBAR_POLICY,
311                                    g_param_spec_enum ("hscrollbar-policy",
312                                                       P_("Horizontal Scrollbar Policy"),
313                                                       P_("When the horizontal scrollbar is displayed"),
314                                                       GTK_TYPE_POLICY_TYPE,
315                                                       GTK_POLICY_AUTOMATIC,
316                                                       GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
317   g_object_class_install_property (gobject_class,
318                                    PROP_VSCROLLBAR_POLICY,
319                                    g_param_spec_enum ("vscrollbar-policy",
320                                                       P_("Vertical Scrollbar Policy"),
321                                                       P_("When the vertical scrollbar is displayed"),
322                                                       GTK_TYPE_POLICY_TYPE,
323                                                       GTK_POLICY_AUTOMATIC,
324                                                       GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
325
326   g_object_class_install_property (gobject_class,
327                                    PROP_WINDOW_PLACEMENT,
328                                    g_param_spec_enum ("window-placement",
329                                                       P_("Window Placement"),
330                                                       P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
331                                                       GTK_TYPE_CORNER_TYPE,
332                                                       GTK_CORNER_TOP_LEFT,
333                                                       GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
334   
335   /**
336    * GtkScrolledWindow:window-placement-set:
337    *
338    * Whether "window-placement" should be used to determine the location 
339    * of the contents with respect to the scrollbars. Otherwise, the 
340    * "gtk-scrolled-window-placement" setting is used.
341    *
342    * Since: 2.10
343    */
344   g_object_class_install_property (gobject_class,
345                                    PROP_WINDOW_PLACEMENT_SET,
346                                    g_param_spec_boolean ("window-placement-set",
347                                                          P_("Window Placement Set"),
348                                                          P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
349                                                          FALSE,
350                                                          GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
351   g_object_class_install_property (gobject_class,
352                                    PROP_SHADOW_TYPE,
353                                    g_param_spec_enum ("shadow-type",
354                                                       P_("Shadow Type"),
355                                                       P_("Style of bevel around the contents"),
356                                                       GTK_TYPE_SHADOW_TYPE,
357                                                       GTK_SHADOW_NONE,
358                                                       GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
359
360   /**
361    * GtkScrolledWindow:scrollbars-within-bevel:
362    *
363    * Whether to place scrollbars within the scrolled window's bevel.
364    *
365    * Since: 2.12
366    */
367   gtk_widget_class_install_style_property (widget_class,
368                                            g_param_spec_boolean ("scrollbars-within-bevel",
369                                                                  P_("Scrollbars within bevel"),
370                                                                  P_("Place scrollbars within the scrolled window's bevel"),
371                                                                  FALSE,
372                                                                  GTK_PARAM_READABLE));
373
374   gtk_widget_class_install_style_property (widget_class,
375                                            g_param_spec_int ("scrollbar-spacing",
376                                                              P_("Scrollbar spacing"),
377                                                              P_("Number of pixels between the scrollbars and the scrolled window"),
378                                                              0,
379                                                              G_MAXINT,
380                                                              DEFAULT_SCROLLBAR_SPACING,
381                                                              GTK_PARAM_READABLE));
382
383   /**
384    * GtkScrolledWindow:min-content-width:
385    *
386    * The minimum content width of @scrolled_window, or -1 if not set.
387    *
388    * Since: 3.0
389    */
390   g_object_class_install_property (gobject_class,
391                                    PROP_MIN_CONTENT_WIDTH,
392                                    g_param_spec_int ("min-content-width",
393                                                      P_("Minimum Content Width"),
394                                                      P_("The minimum width that the scrolled window will allocate to its content"),
395                                                      -1, G_MAXINT, -1,
396                                                      GTK_PARAM_READWRITE));
397
398   /**
399    * GtkScrolledWindow:min-content-height:
400    *
401    * The minimum content height of @scrolled_window, or -1 if not set.
402    *
403    * Since: 3.0
404    */
405   g_object_class_install_property (gobject_class,
406                                    PROP_MIN_CONTENT_HEIGHT,
407                                    g_param_spec_int ("min-content-height",
408                                                      P_("Minimum Content Height"),
409                                                      P_("The minimum height that the scrolled window will allocate to its content"),
410                                                      -1, G_MAXINT, -1,
411                                                      GTK_PARAM_READWRITE));
412   /**
413    * GtkScrolledWindow::scroll-child:
414    * @scrolled_window: a #GtkScrolledWindow
415    * @scroll: a #GtkScrollType describing how much to scroll
416    * @horizontal: whether the keybinding scrolls the child
417    *   horizontally or not
418    *
419    * The ::scroll-child signal is a
420    * <link linkend="keybinding-signals">keybinding signal</link>
421    * which gets emitted when a keybinding that scrolls is pressed.
422    * The horizontal or vertical adjustment is updated which triggers a
423    * signal that the scrolled windows child may listen to and scroll itself.
424    */
425   signals[SCROLL_CHILD] =
426     g_signal_new (I_("scroll-child"),
427                   G_TYPE_FROM_CLASS (gobject_class),
428                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
429                   G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
430                   NULL, NULL,
431                   _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
432                   G_TYPE_BOOLEAN, 2,
433                   GTK_TYPE_SCROLL_TYPE,
434                   G_TYPE_BOOLEAN);
435
436   /**
437    * GtkScrolledWindow::move-focus-out:
438    * @scrolled_window: a #GtkScrolledWindow
439    * @direction_type: either %GTK_DIR_TAB_FORWARD or
440    *   %GTK_DIR_TAB_BACKWARD
441    *
442    * The ::move-focus-out signal is a
443    * <link linkend="keybinding-signals">keybinding signal</link>
444    * which gets emitted when focus is moved away from the scrolled
445    * window by a keybinding.
446    * The #GtkWidget::move-focus signal is emitted with @direction_type
447    * on this scrolled windows toplevel parent in the container hierarchy.
448    * The default bindings for this signal are
449    * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap></keycombo>
450    * and
451    * <keycombo><keycap>Tab</keycap><keycap>Ctrl</keycap><keycap>Shift</keycap></keycombo>.
452    */
453   signals[MOVE_FOCUS_OUT] =
454     g_signal_new (I_("move-focus-out"),
455                   G_TYPE_FROM_CLASS (gobject_class),
456                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
457                   G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
458                   NULL, NULL,
459                   _gtk_marshal_VOID__ENUM,
460                   G_TYPE_NONE, 1,
461                   GTK_TYPE_DIRECTION_TYPE);
462   
463   binding_set = gtk_binding_set_by_class (class);
464
465   add_scroll_binding (binding_set, GDK_KEY_Left,  GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
466   add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD,  TRUE);
467   add_scroll_binding (binding_set, GDK_KEY_Up,    GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
468   add_scroll_binding (binding_set, GDK_KEY_Down,  GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD,  FALSE);
469
470   add_scroll_binding (binding_set, GDK_KEY_Page_Up,   GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
471   add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD,  TRUE);
472   add_scroll_binding (binding_set, GDK_KEY_Page_Up,   0,                GTK_SCROLL_PAGE_BACKWARD, FALSE);
473   add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0,                GTK_SCROLL_PAGE_FORWARD,  FALSE);
474
475   add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
476   add_scroll_binding (binding_set, GDK_KEY_End,  GDK_CONTROL_MASK, GTK_SCROLL_END,   TRUE);
477   add_scroll_binding (binding_set, GDK_KEY_Home, 0,                GTK_SCROLL_START, FALSE);
478   add_scroll_binding (binding_set, GDK_KEY_End,  0,                GTK_SCROLL_END,   FALSE);
479
480   add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
481   add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
482
483   g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
484
485   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCROLLED_WINDOW_ACCESSIBLE);
486 }
487
488 static void
489 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
490 {
491   GtkScrolledWindowPrivate *priv;
492
493   scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
494                                                               GTK_TYPE_SCROLLED_WINDOW,
495                                                               GtkScrolledWindowPrivate);
496
497   gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
498   gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
499
500   priv->hscrollbar = NULL;
501   priv->vscrollbar = NULL;
502   priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
503   priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
504   priv->hscrollbar_visible = FALSE;
505   priv->vscrollbar_visible = FALSE;
506   priv->focus_out = FALSE;
507   priv->window_placement = GTK_CORNER_TOP_LEFT;
508   gtk_scrolled_window_update_real_placement (scrolled_window);
509   priv->min_content_width = -1;
510   priv->min_content_height = -1;
511 }
512
513 /**
514  * gtk_scrolled_window_new:
515  * @hadjustment: (allow-none): horizontal adjustment
516  * @vadjustment: (allow-none): vertical adjustment
517  *
518  * Creates a new scrolled window.
519  *
520  * The two arguments are the scrolled window's adjustments; these will be
521  * shared with the scrollbars and the child widget to keep the bars in sync 
522  * with the child. Usually you want to pass %NULL for the adjustments, which 
523  * will cause the scrolled window to create them for you.
524  *
525  * Returns: a new scrolled window
526  */
527 GtkWidget*
528 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
529                          GtkAdjustment *vadjustment)
530 {
531   GtkWidget *scrolled_window;
532
533   if (hadjustment)
534     g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
535
536   if (vadjustment)
537     g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
538
539   scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
540                                     "hadjustment", hadjustment,
541                                     "vadjustment", vadjustment,
542                                     NULL);
543
544   return scrolled_window;
545 }
546
547 /**
548  * gtk_scrolled_window_set_hadjustment:
549  * @scrolled_window: a #GtkScrolledWindow
550  * @hadjustment: horizontal scroll adjustment
551  *
552  * Sets the #GtkAdjustment for the horizontal scrollbar.
553  */
554 void
555 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
556                                      GtkAdjustment     *hadjustment)
557 {
558   GtkScrolledWindowPrivate *priv;
559   GtkBin *bin;
560   GtkWidget *child;
561
562   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
563   if (hadjustment)
564     g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
565   else
566     hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
567
568   bin = GTK_BIN (scrolled_window);
569   priv = scrolled_window->priv;
570
571   if (!priv->hscrollbar)
572     {
573       gtk_widget_push_composite_child ();
574       priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
575       gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
576       gtk_widget_pop_composite_child ();
577
578       gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
579       g_object_ref (priv->hscrollbar);
580       gtk_widget_show (priv->hscrollbar);
581     }
582   else
583     {
584       GtkAdjustment *old_adjustment;
585
586       old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
587       if (old_adjustment == hadjustment)
588         return;
589
590       g_signal_handlers_disconnect_by_func (old_adjustment,
591                                             gtk_scrolled_window_adjustment_changed,
592                                             scrolled_window);
593       gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
594                                 hadjustment);
595     }
596   hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
597   g_signal_connect (hadjustment,
598                     "changed",
599                     G_CALLBACK (gtk_scrolled_window_adjustment_changed),
600                     scrolled_window);
601   gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
602
603   child = gtk_bin_get_child (bin);
604   if (GTK_IS_SCROLLABLE (child))
605     gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
606
607   g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
608 }
609
610 /**
611  * gtk_scrolled_window_set_vadjustment:
612  * @scrolled_window: a #GtkScrolledWindow
613  * @vadjustment: vertical scroll adjustment
614  *
615  * Sets the #GtkAdjustment for the vertical scrollbar.
616  */
617 void
618 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
619                                      GtkAdjustment     *vadjustment)
620 {
621   GtkScrolledWindowPrivate *priv;
622   GtkBin *bin;
623   GtkWidget *child;
624
625   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
626   if (vadjustment)
627     g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
628   else
629     vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
630
631   bin = GTK_BIN (scrolled_window);
632   priv = scrolled_window->priv;
633
634   if (!priv->vscrollbar)
635     {
636       gtk_widget_push_composite_child ();
637       priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
638       gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
639       gtk_widget_pop_composite_child ();
640
641       gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
642       g_object_ref (priv->vscrollbar);
643       gtk_widget_show (priv->vscrollbar);
644     }
645   else
646     {
647       GtkAdjustment *old_adjustment;
648       
649       old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
650       if (old_adjustment == vadjustment)
651         return;
652
653       g_signal_handlers_disconnect_by_func (old_adjustment,
654                                             gtk_scrolled_window_adjustment_changed,
655                                             scrolled_window);
656       gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
657                                 vadjustment);
658     }
659   vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
660   g_signal_connect (vadjustment,
661                     "changed",
662                     G_CALLBACK (gtk_scrolled_window_adjustment_changed),
663                     scrolled_window);
664   gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
665
666   child = gtk_bin_get_child (bin);
667   if (GTK_IS_SCROLLABLE (child))
668     gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
669
670   g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
671 }
672
673 /**
674  * gtk_scrolled_window_get_hadjustment:
675  * @scrolled_window: a #GtkScrolledWindow
676  *
677  * Returns the horizontal scrollbar's adjustment, used to connect the
678  * horizontal scrollbar to the child widget's horizontal scroll
679  * functionality.
680  *
681  * Returns: (transfer none): the horizontal #GtkAdjustment
682  */
683 GtkAdjustment*
684 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
685 {
686   GtkScrolledWindowPrivate *priv;
687
688   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
689
690   priv = scrolled_window->priv;
691
692   return (priv->hscrollbar ?
693           gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
694           NULL);
695 }
696
697 /**
698  * gtk_scrolled_window_get_vadjustment:
699  * @scrolled_window: a #GtkScrolledWindow
700  * 
701  * Returns the vertical scrollbar's adjustment, used to connect the
702  * vertical scrollbar to the child widget's vertical scroll functionality.
703  * 
704  * Returns: (transfer none): the vertical #GtkAdjustment
705  */
706 GtkAdjustment*
707 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
708 {
709   GtkScrolledWindowPrivate *priv;
710
711   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
712
713   priv = scrolled_window->priv;
714
715   return (priv->vscrollbar ?
716           gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
717           NULL);
718 }
719
720 /**
721  * gtk_scrolled_window_get_hscrollbar:
722  * @scrolled_window: a #GtkScrolledWindow
723  *
724  * Returns the horizontal scrollbar of @scrolled_window.
725  *
726  * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
727  *     or %NULL if it does not have one.
728  *
729  * Since: 2.8
730  */
731 GtkWidget*
732 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
733 {
734   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
735
736   return scrolled_window->priv->hscrollbar;
737 }
738
739 /**
740  * gtk_scrolled_window_get_vscrollbar:
741  * @scrolled_window: a #GtkScrolledWindow
742  * 
743  * Returns the vertical scrollbar of @scrolled_window.
744  *
745  * Returns: (transfer none): the vertical scrollbar of the scrolled window,
746  *     or %NULL if it does not have one.
747  *
748  * Since: 2.8
749  */
750 GtkWidget*
751 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
752 {
753   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
754
755   return scrolled_window->priv->vscrollbar;
756 }
757
758 /**
759  * gtk_scrolled_window_set_policy:
760  * @scrolled_window: a #GtkScrolledWindow
761  * @hscrollbar_policy: policy for horizontal bar
762  * @vscrollbar_policy: policy for vertical bar
763  * 
764  * Sets the scrollbar policy for the horizontal and vertical scrollbars.
765  *
766  * The policy determines when the scrollbar should appear; it is a value
767  * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
768  * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
769  * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
770  * if needed (that is, if the slider part of the bar would be smaller
771  * than the trough - the display is larger than the page size).
772  */
773 void
774 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
775                                 GtkPolicyType      hscrollbar_policy,
776                                 GtkPolicyType      vscrollbar_policy)
777 {
778   GtkScrolledWindowPrivate *priv;
779   GObject *object = G_OBJECT (scrolled_window);
780   
781   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
782
783   priv = scrolled_window->priv;
784
785   if ((priv->hscrollbar_policy != hscrollbar_policy) ||
786       (priv->vscrollbar_policy != vscrollbar_policy))
787     {
788       priv->hscrollbar_policy = hscrollbar_policy;
789       priv->vscrollbar_policy = vscrollbar_policy;
790
791       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
792
793       g_object_freeze_notify (object);
794       g_object_notify (object, "hscrollbar-policy");
795       g_object_notify (object, "vscrollbar-policy");
796       g_object_thaw_notify (object);
797     }
798 }
799
800 /**
801  * gtk_scrolled_window_get_policy:
802  * @scrolled_window: a #GtkScrolledWindow
803  * @hscrollbar_policy: (out) (allow-none): location to store the policy 
804  *     for the horizontal scrollbar, or %NULL.
805  * @vscrollbar_policy: (out) (allow-none): location to store the policy
806  *     for the vertical scrollbar, or %NULL.
807  * 
808  * Retrieves the current policy values for the horizontal and vertical
809  * scrollbars. See gtk_scrolled_window_set_policy().
810  */
811 void
812 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
813                                 GtkPolicyType     *hscrollbar_policy,
814                                 GtkPolicyType     *vscrollbar_policy)
815 {
816   GtkScrolledWindowPrivate *priv;
817
818   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
819
820   priv = scrolled_window->priv;
821
822   if (hscrollbar_policy)
823     *hscrollbar_policy = priv->hscrollbar_policy;
824   if (vscrollbar_policy)
825     *vscrollbar_policy = priv->vscrollbar_policy;
826 }
827
828 static void
829 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
830 {
831   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
832   GtkSettings *settings;
833
834   settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
835
836   if (priv->window_placement_set || settings == NULL)
837     priv->real_window_placement = priv->window_placement;
838   else
839     g_object_get (settings,
840                   "gtk-scrolled-window-placement",
841                   &priv->real_window_placement,
842                   NULL);
843 }
844
845 static void
846 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
847                                             GtkCornerType      window_placement)
848 {
849   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
850
851   if (priv->window_placement != window_placement)
852     {
853       priv->window_placement = window_placement;
854
855       gtk_scrolled_window_update_real_placement (scrolled_window);
856       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
857       
858       g_object_notify (G_OBJECT (scrolled_window), "window-placement");
859     }
860 }
861
862 static void
863 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
864                                        gboolean           placement_set,
865                                        gboolean           emit_resize)
866 {
867   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
868
869   if (priv->window_placement_set != placement_set)
870     {
871       priv->window_placement_set = placement_set;
872
873       gtk_scrolled_window_update_real_placement (scrolled_window);
874       if (emit_resize)
875         gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
876
877       g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
878     }
879 }
880
881 /**
882  * gtk_scrolled_window_set_placement:
883  * @scrolled_window: a #GtkScrolledWindow
884  * @window_placement: position of the child window
885  *
886  * Sets the placement of the contents with respect to the scrollbars
887  * for the scrolled window.
888  * 
889  * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
890  * in the top left, with the scrollbars underneath and to the right.
891  * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
892  * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
893  *
894  * See also gtk_scrolled_window_get_placement() and
895  * gtk_scrolled_window_unset_placement().
896  */
897 void
898 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
899                                    GtkCornerType      window_placement)
900 {
901   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
902
903   gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
904   gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
905 }
906
907 /**
908  * gtk_scrolled_window_get_placement:
909  * @scrolled_window: a #GtkScrolledWindow
910  *
911  * Gets the placement of the contents with respect to the scrollbars
912  * for the scrolled window. See gtk_scrolled_window_set_placement().
913  *
914  * Return value: the current placement value.
915  *
916  * See also gtk_scrolled_window_set_placement() and
917  * gtk_scrolled_window_unset_placement().
918  **/
919 GtkCornerType
920 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
921 {
922   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
923
924   return scrolled_window->priv->window_placement;
925 }
926
927 /**
928  * gtk_scrolled_window_unset_placement:
929  * @scrolled_window: a #GtkScrolledWindow
930  *
931  * Unsets the placement of the contents with respect to the scrollbars
932  * for the scrolled window. If no window placement is set for a scrolled
933  * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
934  *
935  * See also gtk_scrolled_window_set_placement() and
936  * gtk_scrolled_window_get_placement().
937  *
938  * Since: 2.10
939  **/
940 void
941 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
942 {
943   GtkScrolledWindowPrivate *priv;
944
945   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
946
947   priv = scrolled_window->priv;
948
949   if (priv->window_placement_set)
950     {
951       priv->window_placement_set = FALSE;
952
953       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
954
955       g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
956     }
957 }
958
959 /**
960  * gtk_scrolled_window_set_shadow_type:
961  * @scrolled_window: a #GtkScrolledWindow
962  * @type: kind of shadow to draw around scrolled window contents
963  *
964  * Changes the type of shadow drawn around the contents of
965  * @scrolled_window.
966  * 
967  **/
968 void
969 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
970                                      GtkShadowType      type)
971 {
972   GtkScrolledWindowPrivate *priv;
973
974   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
975   g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
976
977   priv = scrolled_window->priv;
978
979   if (priv->shadow_type != type)
980     {
981       priv->shadow_type = type;
982
983       if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
984         gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
985
986       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
987
988       g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
989     }
990 }
991
992 /**
993  * gtk_scrolled_window_get_shadow_type:
994  * @scrolled_window: a #GtkScrolledWindow
995  *
996  * Gets the shadow type of the scrolled window. See 
997  * gtk_scrolled_window_set_shadow_type().
998  *
999  * Return value: the current shadow type
1000  **/
1001 GtkShadowType
1002 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
1003 {
1004   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
1005
1006   return scrolled_window->priv->shadow_type;
1007 }
1008
1009 static void
1010 gtk_scrolled_window_destroy (GtkWidget *widget)
1011 {
1012   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1013   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1014
1015   if (priv->hscrollbar)
1016     {
1017       g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1018                                             gtk_scrolled_window_adjustment_changed,
1019                                             scrolled_window);
1020       gtk_widget_unparent (priv->hscrollbar);
1021       gtk_widget_destroy (priv->hscrollbar);
1022       g_object_unref (priv->hscrollbar);
1023       priv->hscrollbar = NULL;
1024     }
1025   if (priv->vscrollbar)
1026     {
1027       g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1028                                             gtk_scrolled_window_adjustment_changed,
1029                                             scrolled_window);
1030       gtk_widget_unparent (priv->vscrollbar);
1031       gtk_widget_destroy (priv->vscrollbar);
1032       g_object_unref (priv->vscrollbar);
1033       priv->vscrollbar = NULL;
1034     }
1035
1036   GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1037 }
1038
1039 static void
1040 gtk_scrolled_window_set_property (GObject      *object,
1041                                   guint         prop_id,
1042                                   const GValue *value,
1043                                   GParamSpec   *pspec)
1044 {
1045   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1046   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1047
1048   switch (prop_id)
1049     {
1050     case PROP_HADJUSTMENT:
1051       gtk_scrolled_window_set_hadjustment (scrolled_window,
1052                                            g_value_get_object (value));
1053       break;
1054     case PROP_VADJUSTMENT:
1055       gtk_scrolled_window_set_vadjustment (scrolled_window,
1056                                            g_value_get_object (value));
1057       break;
1058     case PROP_HSCROLLBAR_POLICY:
1059       gtk_scrolled_window_set_policy (scrolled_window,
1060                                       g_value_get_enum (value),
1061                                       priv->vscrollbar_policy);
1062       break;
1063     case PROP_VSCROLLBAR_POLICY:
1064       gtk_scrolled_window_set_policy (scrolled_window,
1065                                       priv->hscrollbar_policy,
1066                                       g_value_get_enum (value));
1067       break;
1068     case PROP_WINDOW_PLACEMENT:
1069       gtk_scrolled_window_set_placement_internal (scrolled_window,
1070                                                   g_value_get_enum (value));
1071       break;
1072     case PROP_WINDOW_PLACEMENT_SET:
1073       gtk_scrolled_window_set_placement_set (scrolled_window,
1074                                              g_value_get_boolean (value),
1075                                              TRUE);
1076       break;
1077     case PROP_SHADOW_TYPE:
1078       gtk_scrolled_window_set_shadow_type (scrolled_window,
1079                                            g_value_get_enum (value));
1080       break;
1081     case PROP_MIN_CONTENT_WIDTH:
1082       gtk_scrolled_window_set_min_content_width (scrolled_window,
1083                                                  g_value_get_int (value));
1084       break;
1085     case PROP_MIN_CONTENT_HEIGHT:
1086       gtk_scrolled_window_set_min_content_height (scrolled_window,
1087                                                   g_value_get_int (value));
1088       break;
1089     default:
1090       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1091       break;
1092     }
1093 }
1094
1095 static void
1096 gtk_scrolled_window_get_property (GObject    *object,
1097                                   guint       prop_id,
1098                                   GValue     *value,
1099                                   GParamSpec *pspec)
1100 {
1101   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1102   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1103
1104   switch (prop_id)
1105     {
1106     case PROP_HADJUSTMENT:
1107       g_value_set_object (value,
1108                           G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1109       break;
1110     case PROP_VADJUSTMENT:
1111       g_value_set_object (value,
1112                           G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1113       break;
1114     case PROP_WINDOW_PLACEMENT:
1115       g_value_set_enum (value, priv->window_placement);
1116       break;
1117     case PROP_WINDOW_PLACEMENT_SET:
1118       g_value_set_boolean (value, priv->window_placement_set);
1119       break;
1120     case PROP_SHADOW_TYPE:
1121       g_value_set_enum (value, priv->shadow_type);
1122       break;
1123     case PROP_HSCROLLBAR_POLICY:
1124       g_value_set_enum (value, priv->hscrollbar_policy);
1125       break;
1126     case PROP_VSCROLLBAR_POLICY:
1127       g_value_set_enum (value, priv->vscrollbar_policy);
1128       break;
1129     case PROP_MIN_CONTENT_WIDTH:
1130       g_value_set_int (value, priv->min_content_width);
1131       break;
1132     case PROP_MIN_CONTENT_HEIGHT:
1133       g_value_set_int (value, priv->min_content_height);
1134       break;
1135     default:
1136       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1137       break;
1138     }
1139 }
1140
1141 static void
1142 traverse_container (GtkWidget *widget,
1143                     gpointer   data)
1144 {
1145   if (GTK_IS_SCROLLED_WINDOW (widget))
1146     {
1147       gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1148       gtk_widget_queue_resize (widget);
1149     }
1150   else if (GTK_IS_CONTAINER (widget))
1151     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1152 }
1153
1154 static void
1155 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1156 {
1157   GList *list, *l;
1158
1159   list = gtk_window_list_toplevels ();
1160
1161   for (l = list; l; l = l->next)
1162     gtk_container_forall (GTK_CONTAINER (l->data), 
1163                           traverse_container, NULL);
1164
1165   g_list_free (list);
1166 }
1167
1168 static void
1169 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1170                                     GdkScreen *previous_screen)
1171 {
1172   GtkSettings *settings;
1173   guint window_placement_connection;
1174
1175   gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1176
1177   if (!gtk_widget_has_screen (widget))
1178     return;
1179
1180   settings = gtk_widget_get_settings (widget);
1181
1182   window_placement_connection = 
1183     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
1184                                          "gtk-scrolled-window-connection"));
1185   
1186   if (window_placement_connection)
1187     return;
1188
1189   window_placement_connection =
1190     g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1191                       G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1192   g_object_set_data (G_OBJECT (settings), 
1193                      I_("gtk-scrolled-window-connection"),
1194                      GUINT_TO_POINTER (window_placement_connection));
1195 }
1196
1197 static gboolean
1198 gtk_scrolled_window_draw (GtkWidget *widget,
1199                           cairo_t   *cr)
1200 {
1201   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1202   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1203   GtkStyleContext *context;
1204
1205   context = gtk_widget_get_style_context (widget);
1206
1207   gtk_render_background (context, cr, 0, 0,
1208                          gtk_widget_get_allocated_width (widget),
1209                          gtk_widget_get_allocated_height (widget));
1210
1211   if (priv->shadow_type != GTK_SHADOW_NONE)
1212     {
1213       GtkAllocation relative_allocation;
1214       gboolean scrollbars_within_bevel;
1215
1216       gtk_style_context_save (context);
1217       gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1218
1219       gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1220
1221       if (!scrollbars_within_bevel)
1222         {
1223           GtkStateFlags state;
1224           GtkBorder padding, border;
1225
1226           state = gtk_widget_get_state_flags (widget);
1227           gtk_style_context_get_padding (context, state, &padding);
1228           gtk_style_context_get_border (context, state, &border);
1229
1230           gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1231
1232           relative_allocation.x -= padding.left + border.left;
1233           relative_allocation.y -= padding.top + border.top;
1234           relative_allocation.width += padding.left + padding.right + border.left + border.right;
1235           relative_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1236         }
1237       else
1238         {
1239           relative_allocation.x = 0;
1240           relative_allocation.y = 0;
1241           relative_allocation.width = gtk_widget_get_allocated_width (widget);
1242           relative_allocation.height = gtk_widget_get_allocated_height (widget);
1243         }
1244
1245       gtk_render_frame (context, cr,
1246                         relative_allocation.x,
1247                         relative_allocation.y,
1248                         relative_allocation.width,
1249                         relative_allocation.height);
1250
1251       gtk_style_context_restore (context);
1252     }
1253
1254   GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1255
1256   return FALSE;
1257 }
1258
1259 static void
1260 gtk_scrolled_window_forall (GtkContainer *container,
1261                             gboolean      include_internals,
1262                             GtkCallback   callback,
1263                             gpointer      callback_data)
1264 {
1265   GtkScrolledWindowPrivate *priv;
1266   GtkScrolledWindow *scrolled_window;
1267
1268   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1269   g_return_if_fail (callback != NULL);
1270
1271   GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1272                                               include_internals,
1273                                               callback,
1274                                               callback_data);
1275   if (include_internals)
1276     {
1277       scrolled_window = GTK_SCROLLED_WINDOW (container);
1278       priv = scrolled_window->priv;
1279
1280       if (priv->vscrollbar)
1281         callback (priv->vscrollbar, callback_data);
1282       if (priv->hscrollbar)
1283         callback (priv->hscrollbar, callback_data);
1284     }
1285 }
1286
1287 static gboolean
1288 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1289                                   GtkScrollType      scroll,
1290                                   gboolean           horizontal)
1291 {
1292   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1293   GtkAdjustment *adjustment = NULL;
1294   
1295   switch (scroll)
1296     {
1297     case GTK_SCROLL_STEP_UP:
1298       scroll = GTK_SCROLL_STEP_BACKWARD;
1299       horizontal = FALSE;
1300       break;
1301     case GTK_SCROLL_STEP_DOWN:
1302       scroll = GTK_SCROLL_STEP_FORWARD;
1303       horizontal = FALSE;
1304       break;
1305     case GTK_SCROLL_STEP_LEFT:
1306       scroll = GTK_SCROLL_STEP_BACKWARD;
1307       horizontal = TRUE;
1308       break;
1309     case GTK_SCROLL_STEP_RIGHT:
1310       scroll = GTK_SCROLL_STEP_FORWARD;
1311       horizontal = TRUE;
1312       break;
1313     case GTK_SCROLL_PAGE_UP:
1314       scroll = GTK_SCROLL_PAGE_BACKWARD;
1315       horizontal = FALSE;
1316       break;
1317     case GTK_SCROLL_PAGE_DOWN:
1318       scroll = GTK_SCROLL_PAGE_FORWARD;
1319       horizontal = FALSE;
1320       break;
1321     case GTK_SCROLL_PAGE_LEFT:
1322       scroll = GTK_SCROLL_STEP_BACKWARD;
1323       horizontal = TRUE;
1324       break;
1325     case GTK_SCROLL_PAGE_RIGHT:
1326       scroll = GTK_SCROLL_STEP_FORWARD;
1327       horizontal = TRUE;
1328       break;
1329     case GTK_SCROLL_STEP_BACKWARD:
1330     case GTK_SCROLL_STEP_FORWARD:
1331     case GTK_SCROLL_PAGE_BACKWARD:
1332     case GTK_SCROLL_PAGE_FORWARD:
1333     case GTK_SCROLL_START:
1334     case GTK_SCROLL_END:
1335       break;
1336     default:
1337       g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1338       return FALSE;
1339     }
1340
1341   if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1342       (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1343     return FALSE;
1344
1345   if (horizontal)
1346     {
1347       if (priv->hscrollbar)
1348         adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1349     }
1350   else
1351     {
1352       if (priv->vscrollbar)
1353         adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1354     }
1355
1356   if (adjustment)
1357     {
1358       gdouble value = gtk_adjustment_get_value (adjustment);
1359       
1360       switch (scroll)
1361         {
1362         case GTK_SCROLL_STEP_FORWARD:
1363           value += gtk_adjustment_get_step_increment (adjustment);
1364           break;
1365         case GTK_SCROLL_STEP_BACKWARD:
1366           value -= gtk_adjustment_get_step_increment (adjustment);
1367           break;
1368         case GTK_SCROLL_PAGE_FORWARD:
1369           value += gtk_adjustment_get_page_increment (adjustment);
1370           break;
1371         case GTK_SCROLL_PAGE_BACKWARD:
1372           value -= gtk_adjustment_get_page_increment (adjustment);
1373           break;
1374         case GTK_SCROLL_START:
1375           value = gtk_adjustment_get_lower (adjustment);
1376           break;
1377         case GTK_SCROLL_END:
1378           value = gtk_adjustment_get_upper (adjustment);
1379           break;
1380         default:
1381           g_assert_not_reached ();
1382           break;
1383         }
1384
1385       gtk_adjustment_set_value (adjustment, value);
1386
1387       return TRUE;
1388     }
1389
1390   return FALSE;
1391 }
1392
1393 static void
1394 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1395                                     GtkDirectionType   direction_type)
1396 {
1397   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1398   GtkWidget *toplevel;
1399   
1400   /* Focus out of the scrolled window entirely. We do this by setting
1401    * a flag, then propagating the focus motion to the notebook.
1402    */
1403   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1404   if (!gtk_widget_is_toplevel (toplevel))
1405     return;
1406
1407   g_object_ref (scrolled_window);
1408
1409   priv->focus_out = TRUE;
1410   g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1411   priv->focus_out = FALSE;
1412
1413   g_object_unref (scrolled_window);
1414 }
1415
1416 static void
1417 gtk_scrolled_window_relative_allocation (GtkWidget     *widget,
1418                                          GtkAllocation *allocation)
1419 {
1420   GtkAllocation widget_allocation;
1421   GtkScrolledWindow *scrolled_window;
1422   GtkScrolledWindowPrivate *priv;
1423   gint sb_spacing;
1424   gint sb_width;
1425   gint sb_height;
1426
1427   g_return_if_fail (widget != NULL);
1428   g_return_if_fail (allocation != NULL);
1429
1430   scrolled_window = GTK_SCROLLED_WINDOW (widget);
1431   priv = scrolled_window->priv;
1432
1433   /* Get possible scrollbar dimensions */
1434   sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1435   gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1436   gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1437
1438   /* Subtract some things from our available allocation size */
1439   allocation->x = 0;
1440   allocation->y = 0;
1441
1442   if (priv->shadow_type != GTK_SHADOW_NONE)
1443     {
1444       GtkStyleContext *context;
1445       GtkStateFlags state;
1446       GtkBorder padding, border;
1447
1448       context = gtk_widget_get_style_context (widget);
1449       state = gtk_widget_get_state_flags (widget);
1450
1451       gtk_style_context_save (context);
1452       gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1453
1454       gtk_style_context_get_border (context, state, &border);
1455       gtk_style_context_get_padding (context, state, &padding);
1456
1457       allocation->x += padding.left + border.left;
1458       allocation->y += padding.top + border.top;
1459
1460       gtk_style_context_restore (context);
1461     }
1462
1463   gtk_widget_get_allocation (widget, &widget_allocation);
1464   allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1465   allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1466
1467   if (priv->vscrollbar_visible)
1468     {
1469       gboolean is_rtl;
1470
1471       is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1472   
1473       if ((!is_rtl && 
1474            (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1475             priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1476           (is_rtl && 
1477            (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1478             priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1479         allocation->x += (sb_width +  sb_spacing);
1480
1481       allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1482     }
1483   if (priv->hscrollbar_visible)
1484     {
1485
1486       if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1487           priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1488         allocation->y += (sb_height + sb_spacing);
1489
1490       allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1491     }
1492 }
1493
1494 static void
1495 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1496                                     GtkAllocation     *relative_allocation)
1497 {
1498   GtkWidget     *widget = GTK_WIDGET (swindow), *child;
1499   GtkAllocation  allocation;
1500   GtkAllocation  child_allocation;
1501
1502   child = gtk_bin_get_child (GTK_BIN (widget));
1503
1504   gtk_widget_get_allocation (widget, &allocation);
1505
1506   gtk_scrolled_window_relative_allocation (widget, relative_allocation);
1507   child_allocation.x = relative_allocation->x + allocation.x;
1508   child_allocation.y = relative_allocation->y + allocation.y;
1509   child_allocation.width = relative_allocation->width;
1510   child_allocation.height = relative_allocation->height;
1511
1512   gtk_widget_size_allocate (child, &child_allocation);
1513 }
1514
1515 static void
1516 gtk_scrolled_window_size_allocate (GtkWidget     *widget,
1517                                    GtkAllocation *allocation)
1518 {
1519   GtkScrolledWindow *scrolled_window;
1520   GtkScrolledWindowPrivate *priv;
1521   GtkStyleContext *context;
1522   GtkStateFlags state;
1523   GtkBorder padding, border;
1524   GtkBin *bin;
1525   GtkAllocation relative_allocation;
1526   GtkAllocation child_allocation;
1527   GtkWidget *child;
1528   gboolean scrollbars_within_bevel;
1529   gint sb_spacing;
1530   gint sb_width;
1531   gint sb_height;
1532  
1533   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1534   g_return_if_fail (allocation != NULL);
1535
1536   scrolled_window = GTK_SCROLLED_WINDOW (widget);
1537   bin = GTK_BIN (scrolled_window);
1538   priv = scrolled_window->priv;
1539
1540   /* Get possible scrollbar dimensions */
1541   sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1542   gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1543   gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1544
1545   context = gtk_widget_get_style_context (widget);
1546   state = gtk_widget_get_state_flags (widget);
1547
1548   gtk_style_context_save (context);
1549   gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
1550
1551   gtk_style_context_get_padding (context, state, &padding);
1552   gtk_style_context_get_border (context, state, &border);
1553
1554   gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1555
1556   gtk_widget_set_allocation (widget, allocation);
1557
1558   gtk_style_context_restore (context);
1559
1560   if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1561     priv->hscrollbar_visible = TRUE;
1562   else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1563     priv->hscrollbar_visible = FALSE;
1564   if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1565     priv->vscrollbar_visible = TRUE;
1566   else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1567     priv->vscrollbar_visible = FALSE;
1568
1569   child = gtk_bin_get_child (bin);
1570   if (child && gtk_widget_get_visible (child))
1571     {
1572       gint child_scroll_width;
1573       gint child_scroll_height;
1574       gboolean previous_hvis;
1575       gboolean previous_vvis;
1576       guint count = 0;
1577
1578       /* Determine scrollbar visibility first via hfw apis */
1579       if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1580         {
1581           if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1582             gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
1583           else
1584             gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
1585           
1586           if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1587             {
1588               /* First try without a vertical scrollbar if the content will fit the height
1589                * given the extra width of the scrollbar */
1590               if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1591                 gtk_widget_get_preferred_height_for_width (child, 
1592                                                            MAX (allocation->width, child_scroll_width), 
1593                                                            &child_scroll_height, NULL);
1594               else
1595                 gtk_widget_get_preferred_height_for_width (child,
1596                                                            MAX (allocation->width, child_scroll_width), 
1597                                                            NULL, &child_scroll_height);
1598               
1599               if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1600                 {
1601                   /* Does the content height fit the allocation height ? */
1602                   priv->vscrollbar_visible = child_scroll_height > allocation->height;
1603                   
1604                   /* Does the content width fit the allocation with minus a possible scrollbar ? */
1605                   priv->hscrollbar_visible = 
1606                     child_scroll_width > allocation->width - 
1607                     (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1608                   
1609                   /* Now that we've guessed the hscrollbar, does the content height fit
1610                    * the possible new allocation height ? */
1611                   priv->vscrollbar_visible = 
1612                     child_scroll_height > allocation->height - 
1613                     (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1614                   
1615                   /* Now that we've guessed the vscrollbar, does the content width fit
1616                    * the possible new allocation width ? */
1617                   priv->hscrollbar_visible = 
1618                     child_scroll_width > allocation->width - 
1619                     (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1620                 }
1621               else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1622                 {
1623                   priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1624                   priv->vscrollbar_visible = child_scroll_height > allocation->height - 
1625                     (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1626                 }
1627             }
1628           else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1629             {
1630               priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1631               
1632               if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1633                 priv->hscrollbar_visible = 
1634                   child_scroll_width > allocation->width - 
1635                   (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1636               else
1637                 priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1638             }
1639         } 
1640       else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1641         {
1642           if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1643             gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1644           else
1645             gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1646           
1647           if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1648             {
1649               /* First try without a horizontal scrollbar if the content will fit the width
1650                * given the extra height of the scrollbar */
1651               if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1652                 gtk_widget_get_preferred_width_for_height (child, 
1653                                                            MAX (allocation->height, child_scroll_height), 
1654                                                            &child_scroll_width, NULL);
1655               else
1656                 gtk_widget_get_preferred_width_for_height (child, 
1657                                                            MAX (allocation->height, child_scroll_height), 
1658                                                            NULL, &child_scroll_width);
1659               
1660               if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1661                 {
1662                   /* Does the content width fit the allocation width ? */
1663                   priv->hscrollbar_visible = child_scroll_width > allocation->width;
1664                   
1665                   /* Does the content height fit the allocation with minus a possible scrollbar ? */
1666                   priv->vscrollbar_visible = 
1667                     child_scroll_height > allocation->height - 
1668                     (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1669                   
1670                   /* Now that we've guessed the vscrollbar, does the content width fit
1671                    * the possible new allocation width ? */
1672                   priv->hscrollbar_visible = 
1673                     child_scroll_width > allocation->width - 
1674                     (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1675                   
1676                   /* Now that we've guessed the hscrollbar, does the content height fit
1677                    * the possible new allocation height ? */
1678                   priv->vscrollbar_visible = 
1679                     child_scroll_height > allocation->height - 
1680                     (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1681                 }
1682               else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1683                 {
1684                   priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1685                   priv->hscrollbar_visible = child_scroll_width > allocation->width - 
1686                     (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1687                 }
1688             }
1689           else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1690             {
1691               priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1692               
1693               if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1694                 priv->vscrollbar_visible = 
1695                   child_scroll_height > allocation->height - 
1696                   (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1697               else
1698                 priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1699             }
1700         }
1701
1702       /* Now after guessing scrollbar visibility; fall back on the allocation loop which 
1703        * observes the adjustments to detect scrollbar visibility and also avoids 
1704        * infinite recursion
1705        */
1706       do
1707         {
1708           previous_hvis = priv->hscrollbar_visible;
1709           previous_vvis = priv->vscrollbar_visible;
1710           gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1711
1712           /* Explicitly force scrollbar visibility checks.
1713            *
1714            * Since we make a guess above, the child might not decide to update the adjustments 
1715            * if they logically did not change since the last configuration
1716            */
1717           if (priv->hscrollbar)
1718             gtk_scrolled_window_adjustment_changed 
1719               (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)), scrolled_window);
1720
1721           if (priv->vscrollbar)
1722             gtk_scrolled_window_adjustment_changed 
1723               (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)), scrolled_window);
1724
1725           /* If, after the first iteration, the hscrollbar and the
1726            * vscrollbar flip visiblity... or if one of the scrollbars flip
1727            * on each itteration indefinitly/infinitely, then we just need both 
1728            * at this size.
1729            */
1730           if ((count &&
1731                previous_hvis != priv->hscrollbar_visible &&
1732                previous_vvis != priv->vscrollbar_visible) || count > 3)
1733             {
1734               priv->hscrollbar_visible = TRUE;
1735               priv->vscrollbar_visible = TRUE;
1736
1737               gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1738
1739               break;
1740             }
1741           
1742           count++;
1743         }
1744       while (previous_hvis != priv->hscrollbar_visible ||
1745              previous_vvis != priv->vscrollbar_visible);
1746     }
1747   else
1748     {
1749       priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1750       priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1751       gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1752     }
1753
1754   if (priv->hscrollbar_visible)
1755     {
1756       if (!gtk_widget_get_visible (priv->hscrollbar))
1757         gtk_widget_show (priv->hscrollbar);
1758
1759       child_allocation.x = relative_allocation.x;
1760       if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1761           priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1762         child_allocation.y = (relative_allocation.y +
1763                               relative_allocation.height +
1764                               sb_spacing +
1765                               (priv->shadow_type == GTK_SHADOW_NONE ?
1766                                0 : padding.top + border.top));
1767       else
1768         child_allocation.y = 0;
1769
1770       child_allocation.width = relative_allocation.width;
1771       child_allocation.height = sb_height;
1772       child_allocation.x += allocation->x;
1773       child_allocation.y += allocation->y;
1774
1775       if (priv->shadow_type != GTK_SHADOW_NONE)
1776         {
1777           if (!scrollbars_within_bevel)
1778             {
1779               child_allocation.x -= padding.left + border.left;
1780               child_allocation.width += padding.left + padding.right + border.left + border.right;
1781             }
1782           else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1783                    GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1784             {
1785               child_allocation.y -= padding.top + border.top;
1786             }
1787           else
1788             {
1789               child_allocation.y += padding.top + border.top;
1790             }
1791         }
1792
1793       gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1794     }
1795   else if (gtk_widget_get_visible (priv->hscrollbar))
1796     gtk_widget_hide (priv->hscrollbar);
1797
1798   if (priv->vscrollbar_visible)
1799     {
1800       if (!gtk_widget_get_visible (priv->vscrollbar))
1801         gtk_widget_show (priv->vscrollbar);
1802
1803       if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL && 
1804            (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1805             priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1806           (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR && 
1807            (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1808             priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1809         child_allocation.x = (relative_allocation.x +
1810                               relative_allocation.width +
1811                               sb_spacing +
1812                               (priv->shadow_type == GTK_SHADOW_NONE ?
1813                                0 : padding.left + border.left));
1814       else
1815         child_allocation.x = 0;
1816
1817       child_allocation.y = relative_allocation.y;
1818       child_allocation.width = sb_width;
1819       child_allocation.height = relative_allocation.height;
1820       child_allocation.x += allocation->x;
1821       child_allocation.y += allocation->y;
1822
1823       if (priv->shadow_type != GTK_SHADOW_NONE)
1824         {
1825           if (!scrollbars_within_bevel)
1826             {
1827               child_allocation.y -= padding.top + border.top;
1828               child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
1829             }
1830           else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1831                    GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1832             {
1833               child_allocation.x -= padding.left + border.left;
1834             }
1835           else
1836             {
1837               child_allocation.x += padding.left + border.left;
1838             }
1839         }
1840
1841       gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1842     }
1843   else if (gtk_widget_get_visible (priv->vscrollbar))
1844     gtk_widget_hide (priv->vscrollbar);
1845 }
1846
1847 static gboolean
1848 gtk_scrolled_window_scroll_event (GtkWidget      *widget,
1849                                   GdkEventScroll *event)
1850 {
1851   GtkScrolledWindowPrivate *priv;
1852   GtkScrolledWindow *scrolled_window;
1853   GtkWidget *range;
1854
1855   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1856   g_return_val_if_fail (event != NULL, FALSE);  
1857
1858   scrolled_window = GTK_SCROLLED_WINDOW (widget);
1859   priv = scrolled_window->priv;
1860
1861   if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1862     range = priv->vscrollbar;
1863   else
1864     range = priv->hscrollbar;
1865
1866   if (range && gtk_widget_get_visible (range))
1867     {
1868       GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (range));
1869       gdouble delta;
1870
1871       delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1872
1873       gtk_adjustment_set_value (adjustment, gtk_adjustment_get_value (adjustment) + delta);
1874
1875       return TRUE;
1876     }
1877
1878   return FALSE;
1879 }
1880
1881 static gboolean
1882 gtk_scrolled_window_focus (GtkWidget        *widget,
1883                            GtkDirectionType  direction)
1884 {
1885   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1886   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1887   GtkWidget *child;
1888   gboolean had_focus_child;
1889
1890   had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1891
1892   if (priv->focus_out)
1893     {
1894       priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1895       return FALSE;
1896     }
1897   
1898   if (gtk_widget_is_focus (widget))
1899     return FALSE;
1900
1901   /* We only put the scrolled window itself in the focus chain if it
1902    * isn't possible to focus any children.
1903    */
1904   child = gtk_bin_get_child (GTK_BIN (widget));
1905   if (child)
1906     {
1907       if (gtk_widget_child_focus (child, direction))
1908         return TRUE;
1909     }
1910
1911   if (!had_focus_child && gtk_widget_get_can_focus (widget))
1912     {
1913       gtk_widget_grab_focus (widget);
1914       return TRUE;
1915     }
1916   else
1917     return FALSE;
1918 }
1919
1920 static void
1921 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1922                                         gpointer       data)
1923 {
1924   GtkScrolledWindowPrivate *priv;
1925   GtkScrolledWindow *scrolled_window;
1926
1927   g_return_if_fail (adjustment != NULL);
1928   g_return_if_fail (data != NULL);
1929
1930   scrolled_window = GTK_SCROLLED_WINDOW (data);
1931   priv = scrolled_window->priv;
1932
1933   if (priv->hscrollbar &&
1934       adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1935     {
1936       if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1937         {
1938           gboolean visible;
1939
1940           visible = priv->hscrollbar_visible;
1941           priv->hscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1942                                       gtk_adjustment_get_page_size (adjustment));
1943
1944           if (priv->hscrollbar_visible != visible)
1945             gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1946         }
1947     }
1948   else if (priv->vscrollbar &&
1949            adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1950     {
1951       if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1952         {
1953           gboolean visible;
1954
1955           visible = priv->vscrollbar_visible;
1956           priv->vscrollbar_visible = (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment) >
1957                                       gtk_adjustment_get_page_size (adjustment));
1958
1959           if (priv->vscrollbar_visible != visible)
1960             gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1961         }
1962     }
1963 }
1964
1965 static void
1966 gtk_scrolled_window_add (GtkContainer *container,
1967                          GtkWidget    *child)
1968 {
1969   GtkScrolledWindowPrivate *priv;
1970   GtkScrolledWindow *scrolled_window;
1971   GtkBin *bin;
1972   GtkWidget *child_widget;
1973   GtkAdjustment *hadj, *vadj;
1974
1975   bin = GTK_BIN (container);
1976   child_widget = gtk_bin_get_child (bin);
1977   g_return_if_fail (child_widget == NULL);
1978
1979   scrolled_window = GTK_SCROLLED_WINDOW (container);
1980   priv = scrolled_window->priv;
1981
1982   _gtk_bin_set_child (bin, child);
1983   gtk_widget_set_parent (child, GTK_WIDGET (bin));
1984
1985   hadj = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1986   vadj = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1987
1988   if (GTK_IS_SCROLLABLE (child))
1989     g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1990   else
1991     g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1992                "use gtk_scrolled_window_add_with_viewport() instead");
1993 }
1994
1995 static void
1996 gtk_scrolled_window_remove (GtkContainer *container,
1997                             GtkWidget    *child)
1998 {
1999   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
2000   g_return_if_fail (child != NULL);
2001   g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
2002
2003   g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
2004
2005   /* chain parent class handler to remove child */
2006   GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
2007 }
2008
2009 /**
2010  * gtk_scrolled_window_add_with_viewport:
2011  * @scrolled_window: a #GtkScrolledWindow
2012  * @child: the widget you want to scroll
2013  *
2014  * Used to add children without native scrolling capabilities. This
2015  * is simply a convenience function; it is equivalent to adding the
2016  * unscrollable child to a viewport, then adding the viewport to the
2017  * scrolled window. If a child has native scrolling, use
2018  * gtk_container_add() instead of this function.
2019  *
2020  * The viewport scrolls the child by moving its #GdkWindow, and takes
2021  * the size of the child to be the size of its toplevel #GdkWindow. 
2022  * This will be very wrong for most widgets that support native scrolling;
2023  * for example, if you add a widget such as #GtkTreeView with a viewport,
2024  * the whole widget will scroll, including the column headings. Thus, 
2025  * widgets with native scrolling support should not be used with the 
2026  * #GtkViewport proxy.
2027  *
2028  * A widget supports scrolling natively if it implements the
2029  * #GtkScrollable interface.
2030  */
2031 void
2032 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
2033                                        GtkWidget         *child)
2034 {
2035   GtkBin *bin;
2036   GtkWidget *viewport;
2037   GtkWidget *child_widget;
2038
2039   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2040   g_return_if_fail (GTK_IS_WIDGET (child));
2041   g_return_if_fail (gtk_widget_get_parent (child) == NULL);
2042
2043   bin = GTK_BIN (scrolled_window);
2044   child_widget = gtk_bin_get_child (bin);
2045
2046   if (child_widget)
2047     {
2048       g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2049       g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2050
2051       viewport = child_widget;
2052     }
2053   else
2054     {
2055       viewport =
2056         gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2057                           gtk_scrolled_window_get_vadjustment (scrolled_window));
2058       gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2059     }
2060
2061   gtk_widget_show (viewport);
2062   gtk_container_add (GTK_CONTAINER (viewport), child);
2063 }
2064
2065 /*
2066  * _gtk_scrolled_window_get_spacing:
2067  * @scrolled_window: a scrolled window
2068  * 
2069  * Gets the spacing between the scrolled window's scrollbars and
2070  * the scrolled widget. Used by GtkCombo
2071  * 
2072  * Return value: the spacing, in pixels.
2073  */
2074 gint
2075 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2076 {
2077   GtkScrolledWindowClass *class;
2078     
2079   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2080
2081   class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2082
2083   if (class->scrollbar_spacing >= 0)
2084     return class->scrollbar_spacing;
2085   else
2086     {
2087       gint scrollbar_spacing;
2088       
2089       gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2090                             "scrollbar-spacing", &scrollbar_spacing,
2091                             NULL);
2092
2093       return scrollbar_spacing;
2094     }
2095 }
2096
2097
2098 static void
2099 gtk_scrolled_window_get_preferred_size (GtkWidget      *widget,
2100                                         GtkOrientation  orientation,
2101                                         gint           *minimum_size,
2102                                         gint           *natural_size)
2103 {
2104   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2105   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2106   GtkBin *bin = GTK_BIN (scrolled_window);
2107   gint extra_width;
2108   gint extra_height;
2109   gint scrollbar_spacing;
2110   GtkRequisition hscrollbar_requisition;
2111   GtkRequisition vscrollbar_requisition;
2112   GtkRequisition minimum_req, natural_req;
2113   GtkWidget *child;
2114   gint min_child_size, nat_child_size;
2115
2116   scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2117
2118   extra_width = 0;
2119   extra_height = 0;
2120   minimum_req.width = 0;
2121   minimum_req.height = 0;
2122   natural_req.width = 0;
2123   natural_req.height = 0;
2124
2125   gtk_widget_get_preferred_size (priv->hscrollbar,
2126                                  &hscrollbar_requisition, NULL);
2127   gtk_widget_get_preferred_size (priv->vscrollbar,
2128                                  &vscrollbar_requisition, NULL);
2129
2130   child = gtk_bin_get_child (bin);
2131   if (child && gtk_widget_get_visible (child))
2132     {
2133       if (orientation == GTK_ORIENTATION_HORIZONTAL)
2134         {
2135           gtk_widget_get_preferred_width (child,
2136                                           &min_child_size,
2137                                           &nat_child_size);
2138
2139           if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2140             {
2141               minimum_req.width += min_child_size;
2142               natural_req.width += nat_child_size;
2143             }
2144           else
2145             {
2146               gint min_content_width = priv->min_content_width;
2147
2148               if (min_content_width >= 0)
2149                 {
2150                   minimum_req.width = MAX (minimum_req.width, min_content_width);
2151                   natural_req.width = MAX (natural_req.width, min_content_width);
2152                   extra_width = -1;
2153                 }
2154               else
2155                 {
2156                   minimum_req.width += vscrollbar_requisition.width;
2157                   natural_req.width += vscrollbar_requisition.width;
2158                 }
2159             }
2160         }
2161       else /* GTK_ORIENTATION_VERTICAL */
2162         {
2163           gtk_widget_get_preferred_height (child,
2164                                            &min_child_size,
2165                                            &nat_child_size);
2166
2167           if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2168             {
2169               minimum_req.height += min_child_size;
2170               natural_req.height += nat_child_size;
2171             }
2172           else
2173             {
2174               gint min_content_height = priv->min_content_height;
2175
2176               if (min_content_height >= 0)
2177                 {
2178                   minimum_req.height = MAX (minimum_req.height, min_content_height);
2179                   natural_req.height = MAX (natural_req.height, min_content_height);
2180                   extra_height = -1;
2181                 }
2182               else
2183                 {
2184                   minimum_req.height += vscrollbar_requisition.height;
2185                   natural_req.height += vscrollbar_requisition.height;
2186                 }
2187             }
2188         }
2189     }
2190
2191   if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2192       priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2193     {
2194       minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2195       natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2196       if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2197         extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2198     }
2199
2200   if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2201       priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2202     {
2203       minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2204       natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2205       if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2206         extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2207     }
2208
2209   minimum_req.width  += MAX (0, extra_width);
2210   minimum_req.height += MAX (0, extra_height);
2211   natural_req.width  += MAX (0, extra_width);
2212   natural_req.height += MAX (0, extra_height);
2213
2214   if (priv->shadow_type != GTK_SHADOW_NONE)
2215     {
2216       GtkStyleContext *context;
2217       GtkStateFlags state;
2218       GtkBorder padding, border;
2219
2220       context = gtk_widget_get_style_context (GTK_WIDGET (widget));
2221       state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
2222
2223       gtk_style_context_save (context);
2224       gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
2225
2226       gtk_style_context_get_padding (context, state, &padding);
2227       gtk_style_context_get_border (context, state, &border);
2228
2229       minimum_req.width += padding.left + padding.right + border.left + border.right;
2230       minimum_req.height += padding.top + padding.bottom + border.top + border.bottom;
2231       natural_req.width += padding.left + padding.right + border.left + border.right;
2232       natural_req.height += padding.top + padding.bottom + border.top + border.bottom;
2233
2234       gtk_style_context_restore (context);
2235     }
2236
2237   if (orientation == GTK_ORIENTATION_HORIZONTAL)
2238     {
2239       if (minimum_size)
2240         *minimum_size = minimum_req.width;
2241       if (natural_size)
2242         *natural_size = natural_req.width;
2243     }
2244   else
2245     {
2246       if (minimum_size)
2247         *minimum_size = minimum_req.height;
2248       if (natural_size)
2249         *natural_size = natural_req.height;
2250     }
2251 }
2252
2253 static void     
2254 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2255                                          gint      *minimum_size,
2256                                          gint      *natural_size)
2257 {
2258   gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2259 }
2260
2261 static void
2262 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2263                                           gint      *minimum_size,
2264                                           gint      *natural_size)
2265 {  
2266   gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2267 }
2268
2269 static void
2270 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2271                                                     gint       width,
2272                                                     gint      *minimum_height,
2273                                                     gint      *natural_height)
2274 {
2275   g_return_if_fail (GTK_IS_WIDGET (widget));
2276
2277   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2278 }
2279
2280 static void
2281 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2282                                                     gint       height,
2283                                                     gint      *minimum_width,
2284                                                     gint      *natural_width)
2285 {
2286   g_return_if_fail (GTK_IS_WIDGET (widget));
2287
2288   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2289 }
2290
2291 /**
2292  * gtk_scrolled_window_get_min_content_width:
2293  * @scrolled_window: a #GtkScrolledWindow
2294  *
2295  * Gets the minimum content width of @scrolled_window, or -1 if not set.
2296  *
2297  * Returns: the minimum content width
2298  *
2299  * Since: 3.0
2300  */
2301 gint
2302 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2303 {
2304   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2305
2306   return scrolled_window->priv->min_content_width;
2307 }
2308
2309 /**
2310  * gtk_scrolled_window_set_min_content_width:
2311  * @scrolled_window: a #GtkScrolledWindow
2312  * @width: the minimal content width
2313  *
2314  * Sets the minimum width that @scrolled_window should keep visible.
2315  * Note that this can and (usually will) be smaller than the minimum
2316  * size of the content.
2317  *
2318  * Since: 3.0
2319  */
2320 void
2321 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2322                                            gint               width)
2323 {
2324   GtkScrolledWindowPrivate *priv;
2325
2326   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2327
2328   priv = scrolled_window->priv;
2329
2330   if (priv->min_content_width != width)
2331     {
2332       priv->min_content_width = width;
2333
2334       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2335
2336       g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2337     }
2338 }
2339
2340 /**
2341  * gtk_scrolled_window_get_min_content_height:
2342  * @scrolled_window: a #GtkScrolledWindow
2343  *
2344  * Gets the minimal content height of @scrolled_window, or -1 if not set.
2345  *
2346  * Returns: the minimal content height
2347  *
2348  * Since: 3.0
2349  */
2350 gint
2351 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2352 {
2353   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2354
2355   return scrolled_window->priv->min_content_height;
2356 }
2357
2358 /**
2359  * gtk_scrolled_window_set_min_content_height:
2360  * @scrolled_window: a #GtkScrolledWindow
2361  * @height: the minimal content height
2362  *
2363  * Sets the minimum height that @scrolled_window should keep visible.
2364  * Note that this can and (usually will) be smaller than the minimum
2365  * size of the content.
2366  *
2367  * Since: 3.0
2368  */
2369 void
2370 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2371                                             gint               height)
2372 {
2373   GtkScrolledWindowPrivate *priv;
2374
2375   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2376
2377   priv = scrolled_window->priv;
2378
2379   if (priv->min_content_height != height)
2380     {
2381       priv->min_content_height = height;
2382
2383       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2384
2385       g_object_notify (G_OBJECT (scrolled_window), "min-content-height");
2386     }
2387 }