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