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