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