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