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