]> Pileus Git - ~andy/gtk/blob - gtk/gtkscrolledwindow.c
Another attempt at scrolledwindow size allocation
[~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    trust_sb_visibility    : 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   priv->trust_sb_visibility = FALSE;
493 }
494
495 /**
496  * gtk_scrolled_window_new:
497  * @hadjustment: (allow-none): horizontal adjustment
498  * @vadjustment: (allow-none): vertical adjustment
499  *
500  * Creates a new scrolled window.
501  *
502  * The two arguments are the scrolled window's adjustments; these will be
503  * shared with the scrollbars and the child widget to keep the bars in sync 
504  * with the child. Usually you want to pass %NULL for the adjustments, which 
505  * will cause the scrolled window to create them for you.
506  *
507  * Returns: a new scrolled window
508  */
509 GtkWidget*
510 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
511                          GtkAdjustment *vadjustment)
512 {
513   GtkWidget *scrolled_window;
514
515   if (hadjustment)
516     g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
517
518   if (vadjustment)
519     g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
520
521   scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
522                                     "hadjustment", hadjustment,
523                                     "vadjustment", vadjustment,
524                                     NULL);
525
526   return scrolled_window;
527 }
528
529 /**
530  * gtk_scrolled_window_set_hadjustment:
531  * @scrolled_window: a #GtkScrolledWindow
532  * @hadjustment: horizontal scroll adjustment
533  *
534  * Sets the #GtkAdjustment for the horizontal scrollbar.
535  */
536 void
537 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
538                                      GtkAdjustment     *hadjustment)
539 {
540   GtkScrolledWindowPrivate *priv;
541   GtkBin *bin;
542   GtkWidget *child;
543
544   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
545   if (hadjustment)
546     g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
547   else
548     hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
549
550   bin = GTK_BIN (scrolled_window);
551   priv = scrolled_window->priv;
552
553   if (!priv->hscrollbar)
554     {
555       gtk_widget_push_composite_child ();
556       priv->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
557       gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
558       gtk_widget_pop_composite_child ();
559
560       gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
561       g_object_ref (priv->hscrollbar);
562       gtk_widget_show (priv->hscrollbar);
563     }
564   else
565     {
566       GtkAdjustment *old_adjustment;
567
568       old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
569       if (old_adjustment == hadjustment)
570         return;
571
572       g_signal_handlers_disconnect_by_func (old_adjustment,
573                                             gtk_scrolled_window_adjustment_changed,
574                                             scrolled_window);
575       gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
576                                 hadjustment);
577     }
578   hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
579   g_signal_connect (hadjustment,
580                     "changed",
581                     G_CALLBACK (gtk_scrolled_window_adjustment_changed),
582                     scrolled_window);
583   gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
584
585   child = gtk_bin_get_child (bin);
586   if (GTK_IS_SCROLLABLE (child))
587     gtk_scrollable_set_hadjustment (GTK_SCROLLABLE (child), hadjustment);
588
589   g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
590 }
591
592 /**
593  * gtk_scrolled_window_set_vadjustment:
594  * @scrolled_window: a #GtkScrolledWindow
595  * @vadjustment: vertical scroll adjustment
596  *
597  * Sets the #GtkAdjustment for the vertical scrollbar.
598  */
599 void
600 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
601                                      GtkAdjustment     *vadjustment)
602 {
603   GtkScrolledWindowPrivate *priv;
604   GtkBin *bin;
605   GtkWidget *child;
606
607   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
608   if (vadjustment)
609     g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
610   else
611     vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
612
613   bin = GTK_BIN (scrolled_window);
614   priv = scrolled_window->priv;
615
616   if (!priv->vscrollbar)
617     {
618       gtk_widget_push_composite_child ();
619       priv->vscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, vadjustment);
620       gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
621       gtk_widget_pop_composite_child ();
622
623       gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
624       g_object_ref (priv->vscrollbar);
625       gtk_widget_show (priv->vscrollbar);
626     }
627   else
628     {
629       GtkAdjustment *old_adjustment;
630       
631       old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
632       if (old_adjustment == vadjustment)
633         return;
634
635       g_signal_handlers_disconnect_by_func (old_adjustment,
636                                             gtk_scrolled_window_adjustment_changed,
637                                             scrolled_window);
638       gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
639                                 vadjustment);
640     }
641   vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
642   g_signal_connect (vadjustment,
643                     "changed",
644                     G_CALLBACK (gtk_scrolled_window_adjustment_changed),
645                     scrolled_window);
646   gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
647
648   child = gtk_bin_get_child (bin);
649   if (GTK_IS_SCROLLABLE (child))
650     gtk_scrollable_set_vadjustment (GTK_SCROLLABLE (child), vadjustment);
651
652   g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
653 }
654
655 /**
656  * gtk_scrolled_window_get_hadjustment:
657  * @scrolled_window: a #GtkScrolledWindow
658  *
659  * Returns the horizontal scrollbar's adjustment, used to connect the
660  * horizontal scrollbar to the child widget's horizontal scroll
661  * functionality.
662  *
663  * Returns: (transfer none): the horizontal #GtkAdjustment
664  */
665 GtkAdjustment*
666 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
667 {
668   GtkScrolledWindowPrivate *priv;
669
670   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
671
672   priv = scrolled_window->priv;
673
674   return (priv->hscrollbar ?
675           gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
676           NULL);
677 }
678
679 /**
680  * gtk_scrolled_window_get_vadjustment:
681  * @scrolled_window: a #GtkScrolledWindow
682  * 
683  * Returns the vertical scrollbar's adjustment, used to connect the
684  * vertical scrollbar to the child widget's vertical scroll functionality.
685  * 
686  * Returns: (transfer none): the vertical #GtkAdjustment
687  */
688 GtkAdjustment*
689 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
690 {
691   GtkScrolledWindowPrivate *priv;
692
693   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
694
695   priv = scrolled_window->priv;
696
697   return (priv->vscrollbar ?
698           gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
699           NULL);
700 }
701
702 /**
703  * gtk_scrolled_window_get_hscrollbar:
704  * @scrolled_window: a #GtkScrolledWindow
705  *
706  * Returns the horizontal scrollbar of @scrolled_window.
707  *
708  * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
709  *     or %NULL if it does not have one.
710  *
711  * Since: 2.8
712  */
713 GtkWidget*
714 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
715 {
716   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
717
718   return scrolled_window->priv->hscrollbar;
719 }
720
721 /**
722  * gtk_scrolled_window_get_vscrollbar:
723  * @scrolled_window: a #GtkScrolledWindow
724  * 
725  * Returns the vertical scrollbar of @scrolled_window.
726  *
727  * Returns: (transfer none): the vertical scrollbar of the scrolled window,
728  *     or %NULL if it does not have one.
729  *
730  * Since: 2.8
731  */
732 GtkWidget*
733 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
734 {
735   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
736
737   return scrolled_window->priv->vscrollbar;
738 }
739
740 /**
741  * gtk_scrolled_window_set_policy:
742  * @scrolled_window: a #GtkScrolledWindow
743  * @hscrollbar_policy: policy for horizontal bar
744  * @vscrollbar_policy: policy for vertical bar
745  * 
746  * Sets the scrollbar policy for the horizontal and vertical scrollbars.
747  *
748  * The policy determines when the scrollbar should appear; it is a value
749  * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
750  * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
751  * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
752  * if needed (that is, if the slider part of the bar would be smaller
753  * than the trough - the display is larger than the page size).
754  */
755 void
756 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
757                                 GtkPolicyType      hscrollbar_policy,
758                                 GtkPolicyType      vscrollbar_policy)
759 {
760   GtkScrolledWindowPrivate *priv;
761   GObject *object = G_OBJECT (scrolled_window);
762   
763   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
764
765   priv = scrolled_window->priv;
766
767   if ((priv->hscrollbar_policy != hscrollbar_policy) ||
768       (priv->vscrollbar_policy != vscrollbar_policy))
769     {
770       priv->hscrollbar_policy = hscrollbar_policy;
771       priv->vscrollbar_policy = vscrollbar_policy;
772
773       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
774
775       g_object_freeze_notify (object);
776       g_object_notify (object, "hscrollbar-policy");
777       g_object_notify (object, "vscrollbar-policy");
778       g_object_thaw_notify (object);
779     }
780 }
781
782 /**
783  * gtk_scrolled_window_get_policy:
784  * @scrolled_window: a #GtkScrolledWindow
785  * @hscrollbar_policy: location to store the policy for the horizontal 
786  *     scrollbar, or %NULL.
787  * @vscrollbar_policy: location to store the policy for the vertical
788  *     scrollbar, or %NULL.
789  * 
790  * Retrieves the current policy values for the horizontal and vertical
791  * scrollbars. See gtk_scrolled_window_set_policy().
792  */
793 void
794 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
795                                 GtkPolicyType     *hscrollbar_policy,
796                                 GtkPolicyType     *vscrollbar_policy)
797 {
798   GtkScrolledWindowPrivate *priv;
799
800   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
801
802   priv = scrolled_window->priv;
803
804   if (hscrollbar_policy)
805     *hscrollbar_policy = priv->hscrollbar_policy;
806   if (vscrollbar_policy)
807     *vscrollbar_policy = priv->vscrollbar_policy;
808 }
809
810 static void
811 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
812 {
813   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
814   GtkSettings *settings;
815
816   settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
817
818   if (priv->window_placement_set || settings == NULL)
819     priv->real_window_placement = priv->window_placement;
820   else
821     g_object_get (settings,
822                   "gtk-scrolled-window-placement",
823                   &priv->real_window_placement,
824                   NULL);
825 }
826
827 static void
828 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
829                                             GtkCornerType      window_placement)
830 {
831   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
832
833   if (priv->window_placement != window_placement)
834     {
835       priv->window_placement = window_placement;
836
837       gtk_scrolled_window_update_real_placement (scrolled_window);
838       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
839       
840       g_object_notify (G_OBJECT (scrolled_window), "window-placement");
841     }
842 }
843
844 static void
845 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
846                                        gboolean           placement_set,
847                                        gboolean           emit_resize)
848 {
849   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
850
851   if (priv->window_placement_set != placement_set)
852     {
853       priv->window_placement_set = placement_set;
854
855       gtk_scrolled_window_update_real_placement (scrolled_window);
856       if (emit_resize)
857         gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
858
859       g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
860     }
861 }
862
863 /**
864  * gtk_scrolled_window_set_placement:
865  * @scrolled_window: a #GtkScrolledWindow
866  * @window_placement: position of the child window
867  *
868  * Sets the placement of the contents with respect to the scrollbars
869  * for the scrolled window.
870  * 
871  * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
872  * in the top left, with the scrollbars underneath and to the right.
873  * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
874  * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
875  *
876  * See also gtk_scrolled_window_get_placement() and
877  * gtk_scrolled_window_unset_placement().
878  */
879 void
880 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
881                                    GtkCornerType      window_placement)
882 {
883   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
884
885   gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
886   gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
887 }
888
889 /**
890  * gtk_scrolled_window_get_placement:
891  * @scrolled_window: a #GtkScrolledWindow
892  *
893  * Gets the placement of the contents with respect to the scrollbars
894  * for the scrolled window. See gtk_scrolled_window_set_placement().
895  *
896  * Return value: the current placement value.
897  *
898  * See also gtk_scrolled_window_set_placement() and
899  * gtk_scrolled_window_unset_placement().
900  **/
901 GtkCornerType
902 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
903 {
904   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
905
906   return scrolled_window->priv->window_placement;
907 }
908
909 /**
910  * gtk_scrolled_window_unset_placement:
911  * @scrolled_window: a #GtkScrolledWindow
912  *
913  * Unsets the placement of the contents with respect to the scrollbars
914  * for the scrolled window. If no window placement is set for a scrolled
915  * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
916  *
917  * See also gtk_scrolled_window_set_placement() and
918  * gtk_scrolled_window_get_placement().
919  *
920  * Since: 2.10
921  **/
922 void
923 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
924 {
925   GtkScrolledWindowPrivate *priv;
926
927   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
928
929   priv = scrolled_window->priv;
930
931   if (priv->window_placement_set)
932     {
933       priv->window_placement_set = FALSE;
934
935       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
936
937       g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
938     }
939 }
940
941 /**
942  * gtk_scrolled_window_set_shadow_type:
943  * @scrolled_window: a #GtkScrolledWindow
944  * @type: kind of shadow to draw around scrolled window contents
945  *
946  * Changes the type of shadow drawn around the contents of
947  * @scrolled_window.
948  * 
949  **/
950 void
951 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
952                                      GtkShadowType      type)
953 {
954   GtkScrolledWindowPrivate *priv;
955
956   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
957   g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
958
959   priv = scrolled_window->priv;
960
961   if (priv->shadow_type != type)
962     {
963       priv->shadow_type = type;
964
965       if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
966         gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
967
968       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
969
970       g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
971     }
972 }
973
974 /**
975  * gtk_scrolled_window_get_shadow_type:
976  * @scrolled_window: a #GtkScrolledWindow
977  *
978  * Gets the shadow type of the scrolled window. See 
979  * gtk_scrolled_window_set_shadow_type().
980  *
981  * Return value: the current shadow type
982  **/
983 GtkShadowType
984 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
985 {
986   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
987
988   return scrolled_window->priv->shadow_type;
989 }
990
991 static void
992 gtk_scrolled_window_destroy (GtkWidget *widget)
993 {
994   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
995   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
996
997   if (priv->hscrollbar)
998     {
999       g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1000                                             gtk_scrolled_window_adjustment_changed,
1001                                             scrolled_window);
1002       gtk_widget_unparent (priv->hscrollbar);
1003       gtk_widget_destroy (priv->hscrollbar);
1004       g_object_unref (priv->hscrollbar);
1005       priv->hscrollbar = NULL;
1006     }
1007   if (priv->vscrollbar)
1008     {
1009       g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
1010                                             gtk_scrolled_window_adjustment_changed,
1011                                             scrolled_window);
1012       gtk_widget_unparent (priv->vscrollbar);
1013       gtk_widget_destroy (priv->vscrollbar);
1014       g_object_unref (priv->vscrollbar);
1015       priv->vscrollbar = NULL;
1016     }
1017
1018   GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->destroy (widget);
1019 }
1020
1021 static void
1022 gtk_scrolled_window_set_property (GObject      *object,
1023                                   guint         prop_id,
1024                                   const GValue *value,
1025                                   GParamSpec   *pspec)
1026 {
1027   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1028   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1029
1030   switch (prop_id)
1031     {
1032     case PROP_HADJUSTMENT:
1033       gtk_scrolled_window_set_hadjustment (scrolled_window,
1034                                            g_value_get_object (value));
1035       break;
1036     case PROP_VADJUSTMENT:
1037       gtk_scrolled_window_set_vadjustment (scrolled_window,
1038                                            g_value_get_object (value));
1039       break;
1040     case PROP_HSCROLLBAR_POLICY:
1041       gtk_scrolled_window_set_policy (scrolled_window,
1042                                       g_value_get_enum (value),
1043                                       priv->vscrollbar_policy);
1044       break;
1045     case PROP_VSCROLLBAR_POLICY:
1046       gtk_scrolled_window_set_policy (scrolled_window,
1047                                       priv->hscrollbar_policy,
1048                                       g_value_get_enum (value));
1049       break;
1050     case PROP_WINDOW_PLACEMENT:
1051       gtk_scrolled_window_set_placement_internal (scrolled_window,
1052                                                   g_value_get_enum (value));
1053       break;
1054     case PROP_WINDOW_PLACEMENT_SET:
1055       gtk_scrolled_window_set_placement_set (scrolled_window,
1056                                              g_value_get_boolean (value),
1057                                              TRUE);
1058       break;
1059     case PROP_SHADOW_TYPE:
1060       gtk_scrolled_window_set_shadow_type (scrolled_window,
1061                                            g_value_get_enum (value));
1062       break;
1063     case PROP_MIN_CONTENT_WIDTH:
1064       gtk_scrolled_window_set_min_content_width (scrolled_window,
1065                                                  g_value_get_int (value));
1066       break;
1067     case PROP_MIN_CONTENT_HEIGHT:
1068       gtk_scrolled_window_set_min_content_height (scrolled_window,
1069                                                   g_value_get_int (value));
1070       break;
1071     default:
1072       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1073       break;
1074     }
1075 }
1076
1077 static void
1078 gtk_scrolled_window_get_property (GObject    *object,
1079                                   guint       prop_id,
1080                                   GValue     *value,
1081                                   GParamSpec *pspec)
1082 {
1083   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
1084   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1085
1086   switch (prop_id)
1087     {
1088     case PROP_HADJUSTMENT:
1089       g_value_set_object (value,
1090                           G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1091       break;
1092     case PROP_VADJUSTMENT:
1093       g_value_set_object (value,
1094                           G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1095       break;
1096     case PROP_WINDOW_PLACEMENT:
1097       g_value_set_enum (value, priv->window_placement);
1098       break;
1099     case PROP_WINDOW_PLACEMENT_SET:
1100       g_value_set_boolean (value, priv->window_placement_set);
1101       break;
1102     case PROP_SHADOW_TYPE:
1103       g_value_set_enum (value, priv->shadow_type);
1104       break;
1105     case PROP_HSCROLLBAR_POLICY:
1106       g_value_set_enum (value, priv->hscrollbar_policy);
1107       break;
1108     case PROP_VSCROLLBAR_POLICY:
1109       g_value_set_enum (value, priv->vscrollbar_policy);
1110       break;
1111     case PROP_MIN_CONTENT_WIDTH:
1112       g_value_set_int (value, priv->min_content_width);
1113       break;
1114     case PROP_MIN_CONTENT_HEIGHT:
1115       g_value_set_int (value, priv->min_content_height);
1116       break;
1117     default:
1118       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1119       break;
1120     }
1121 }
1122
1123 static void
1124 traverse_container (GtkWidget *widget,
1125                     gpointer   data)
1126 {
1127   if (GTK_IS_SCROLLED_WINDOW (widget))
1128     {
1129       gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1130       gtk_widget_queue_resize (widget);
1131     }
1132   else if (GTK_IS_CONTAINER (widget))
1133     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1134 }
1135
1136 static void
1137 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1138 {
1139   GList *list, *l;
1140
1141   list = gtk_window_list_toplevels ();
1142
1143   for (l = list; l; l = l->next)
1144     gtk_container_forall (GTK_CONTAINER (l->data), 
1145                           traverse_container, NULL);
1146
1147   g_list_free (list);
1148 }
1149
1150 static void
1151 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1152                                     GdkScreen *previous_screen)
1153 {
1154   GtkSettings *settings;
1155   guint window_placement_connection;
1156
1157   gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1158
1159   if (!gtk_widget_has_screen (widget))
1160     return;
1161
1162   settings = gtk_widget_get_settings (widget);
1163
1164   window_placement_connection = 
1165     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
1166                                          "gtk-scrolled-window-connection"));
1167   
1168   if (window_placement_connection)
1169     return;
1170
1171   window_placement_connection =
1172     g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1173                       G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1174   g_object_set_data (G_OBJECT (settings), 
1175                      I_("gtk-scrolled-window-connection"),
1176                      GUINT_TO_POINTER (window_placement_connection));
1177 }
1178
1179 static gboolean
1180 gtk_scrolled_window_draw (GtkWidget *widget,
1181                           cairo_t   *cr)
1182 {
1183   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1184   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1185
1186   if (priv->shadow_type != GTK_SHADOW_NONE)
1187     {
1188       GtkAllocation relative_allocation;
1189       GtkStyle *style;
1190       gboolean scrollbars_within_bevel;
1191
1192       style = gtk_widget_get_style (widget);
1193       gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1194
1195       if (!scrollbars_within_bevel)
1196         {
1197           gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1198
1199           relative_allocation.x -= style->xthickness;
1200           relative_allocation.y -= style->ythickness;
1201           relative_allocation.width += 2 * style->xthickness;
1202           relative_allocation.height += 2 * style->ythickness;
1203         }
1204       else
1205         {
1206           relative_allocation.x = 0;
1207           relative_allocation.y = 0;
1208           relative_allocation.width = gtk_widget_get_allocated_width (widget);
1209           relative_allocation.height = gtk_widget_get_allocated_height (widget);
1210         }
1211
1212       gtk_paint_shadow (style,
1213                         cr,
1214                         GTK_STATE_NORMAL, priv->shadow_type,
1215                         widget, "scrolled_window",
1216                         relative_allocation.x,
1217                         relative_allocation.y,
1218                         relative_allocation.width,
1219                         relative_allocation.height);
1220     }
1221
1222   GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1223
1224   return FALSE;
1225 }
1226
1227 static void
1228 gtk_scrolled_window_forall (GtkContainer *container,
1229                             gboolean      include_internals,
1230                             GtkCallback   callback,
1231                             gpointer      callback_data)
1232 {
1233   GtkScrolledWindowPrivate *priv;
1234   GtkScrolledWindow *scrolled_window;
1235
1236   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1237   g_return_if_fail (callback != NULL);
1238
1239   GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1240                                               include_internals,
1241                                               callback,
1242                                               callback_data);
1243   if (include_internals)
1244     {
1245       scrolled_window = GTK_SCROLLED_WINDOW (container);
1246       priv = scrolled_window->priv;
1247
1248       if (priv->vscrollbar)
1249         callback (priv->vscrollbar, callback_data);
1250       if (priv->hscrollbar)
1251         callback (priv->hscrollbar, callback_data);
1252     }
1253 }
1254
1255 static gboolean
1256 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1257                                   GtkScrollType      scroll,
1258                                   gboolean           horizontal)
1259 {
1260   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1261   GtkAdjustment *adjustment = NULL;
1262   
1263   switch (scroll)
1264     {
1265     case GTK_SCROLL_STEP_UP:
1266       scroll = GTK_SCROLL_STEP_BACKWARD;
1267       horizontal = FALSE;
1268       break;
1269     case GTK_SCROLL_STEP_DOWN:
1270       scroll = GTK_SCROLL_STEP_FORWARD;
1271       horizontal = FALSE;
1272       break;
1273     case GTK_SCROLL_STEP_LEFT:
1274       scroll = GTK_SCROLL_STEP_BACKWARD;
1275       horizontal = TRUE;
1276       break;
1277     case GTK_SCROLL_STEP_RIGHT:
1278       scroll = GTK_SCROLL_STEP_FORWARD;
1279       horizontal = TRUE;
1280       break;
1281     case GTK_SCROLL_PAGE_UP:
1282       scroll = GTK_SCROLL_PAGE_BACKWARD;
1283       horizontal = FALSE;
1284       break;
1285     case GTK_SCROLL_PAGE_DOWN:
1286       scroll = GTK_SCROLL_PAGE_FORWARD;
1287       horizontal = FALSE;
1288       break;
1289     case GTK_SCROLL_PAGE_LEFT:
1290       scroll = GTK_SCROLL_STEP_BACKWARD;
1291       horizontal = TRUE;
1292       break;
1293     case GTK_SCROLL_PAGE_RIGHT:
1294       scroll = GTK_SCROLL_STEP_FORWARD;
1295       horizontal = TRUE;
1296       break;
1297     case GTK_SCROLL_STEP_BACKWARD:
1298     case GTK_SCROLL_STEP_FORWARD:
1299     case GTK_SCROLL_PAGE_BACKWARD:
1300     case GTK_SCROLL_PAGE_FORWARD:
1301     case GTK_SCROLL_START:
1302     case GTK_SCROLL_END:
1303       break;
1304     default:
1305       g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1306       return FALSE;
1307     }
1308
1309   if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1310       (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1311     return FALSE;
1312
1313   if (horizontal)
1314     {
1315       if (priv->hscrollbar)
1316         adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1317     }
1318   else
1319     {
1320       if (priv->vscrollbar)
1321         adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1322     }
1323
1324   if (adjustment)
1325     {
1326       gdouble value = adjustment->value;
1327       
1328       switch (scroll)
1329         {
1330         case GTK_SCROLL_STEP_FORWARD:
1331           value += adjustment->step_increment;
1332           break;
1333         case GTK_SCROLL_STEP_BACKWARD:
1334           value -= adjustment->step_increment;
1335           break;
1336         case GTK_SCROLL_PAGE_FORWARD:
1337           value += adjustment->page_increment;
1338           break;
1339         case GTK_SCROLL_PAGE_BACKWARD:
1340           value -= adjustment->page_increment;
1341           break;
1342         case GTK_SCROLL_START:
1343           value = adjustment->lower;
1344           break;
1345         case GTK_SCROLL_END:
1346           value = adjustment->upper;
1347           break;
1348         default:
1349           g_assert_not_reached ();
1350           break;
1351         }
1352
1353       gtk_adjustment_set_value (adjustment, value);
1354
1355       return TRUE;
1356     }
1357
1358   return FALSE;
1359 }
1360
1361 static void
1362 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1363                                     GtkDirectionType   direction_type)
1364 {
1365   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1366   GtkWidget *toplevel;
1367   
1368   /* Focus out of the scrolled window entirely. We do this by setting
1369    * a flag, then propagating the focus motion to the notebook.
1370    */
1371   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1372   if (!gtk_widget_is_toplevel (toplevel))
1373     return;
1374
1375   g_object_ref (scrolled_window);
1376
1377   priv->focus_out = TRUE;
1378   g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1379   priv->focus_out = FALSE;
1380
1381   g_object_unref (scrolled_window);
1382 }
1383
1384 static void
1385 gtk_scrolled_window_relative_allocation (GtkWidget     *widget,
1386                                          GtkAllocation *allocation)
1387 {
1388   GtkAllocation widget_allocation;
1389   GtkScrolledWindow *scrolled_window;
1390   GtkScrolledWindowPrivate *priv;
1391   GtkStyle *style;
1392   gint sb_spacing;
1393   gint sb_width;
1394   gint sb_height;
1395
1396   g_return_if_fail (widget != NULL);
1397   g_return_if_fail (allocation != NULL);
1398
1399   scrolled_window = GTK_SCROLLED_WINDOW (widget);
1400   priv = scrolled_window->priv;
1401
1402   /* Get possible scrollbar dimensions */
1403   sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1404   gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1405   gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1406
1407   /* Subtract some things from our available allocation size */
1408   allocation->x = 0;
1409   allocation->y = 0;
1410
1411   if (priv->shadow_type != GTK_SHADOW_NONE)
1412     {
1413       style = gtk_widget_get_style (widget);
1414       allocation->x += style->xthickness;
1415       allocation->y += style->ythickness;
1416     }
1417
1418   gtk_widget_get_allocation (widget, &widget_allocation);
1419   allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1420   allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1421
1422   if (priv->vscrollbar_visible)
1423     {
1424       gboolean is_rtl;
1425
1426       is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1427   
1428       if ((!is_rtl && 
1429            (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1430             priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1431           (is_rtl && 
1432            (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1433             priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1434         allocation->x += (sb_width +  sb_spacing);
1435
1436       allocation->width = MAX (1, allocation->width - (sb_width + sb_spacing));
1437     }
1438   if (priv->hscrollbar_visible)
1439     {
1440
1441       if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1442           priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1443         allocation->y += (sb_height + sb_spacing);
1444
1445       allocation->height = MAX (1, allocation->height - (sb_height + sb_spacing));
1446     }
1447 }
1448
1449 void
1450 gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
1451                                     GtkAllocation     *relative_allocation)
1452 {
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   gtk_widget_size_allocate (child, &child_allocation);
1468 }
1469
1470 static void
1471 gtk_scrolled_window_size_allocate (GtkWidget     *widget,
1472                                    GtkAllocation *allocation)
1473 {
1474   GtkScrolledWindow *scrolled_window;
1475   GtkScrolledWindowPrivate *priv;
1476   GtkStyle *style;
1477   GtkBin *bin;
1478   GtkAllocation relative_allocation;
1479   GtkAllocation child_allocation;
1480   GtkWidget *child;
1481   gboolean scrollbars_within_bevel;
1482   gint sb_spacing;
1483   gint sb_width;
1484   gint sb_height;
1485   guint count = 0;
1486
1487   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1488   g_return_if_fail (allocation != NULL);
1489
1490   scrolled_window = GTK_SCROLLED_WINDOW (widget);
1491   bin = GTK_BIN (scrolled_window);
1492   priv = scrolled_window->priv;
1493
1494   /* Get possible scrollbar dimensions */
1495   sb_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1496   gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
1497   gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
1498
1499   style = gtk_widget_get_style (widget);
1500   gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1501
1502   gtk_widget_set_allocation (widget, allocation);
1503
1504   if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1505     priv->hscrollbar_visible = TRUE;
1506   else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1507     priv->hscrollbar_visible = FALSE;
1508   if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1509     priv->vscrollbar_visible = TRUE;
1510   else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1511     priv->vscrollbar_visible = FALSE;
1512
1513   child = gtk_bin_get_child (bin);
1514   if (child && gtk_widget_get_visible (child))
1515     {
1516       gint child_scroll_width;
1517       gint child_scroll_height;
1518       gboolean previous_hvis;
1519       gboolean previous_vvis;
1520
1521       /* Determine scrollbar visibility first via hfw apis */
1522       if (!priv->trust_sb_visibility)
1523         {
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
1534                    * fit the height given the extra width of the scrollbar
1535                    */
1536                   if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1537                     gtk_widget_get_preferred_height_for_width (child,
1538                                                                MAX (allocation->width, child_scroll_width),
1539                                                                &child_scroll_height, NULL);
1540                   else
1541                     gtk_widget_get_preferred_height_for_width (child,
1542                                                                MAX (allocation->width, child_scroll_width),
1543                                                                NULL, &child_scroll_height);
1544
1545                   if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1546                     {
1547                       /* Does the content height fit the allocation height ? */
1548                       priv->vscrollbar_visible = child_scroll_height > allocation->height;
1549
1550                       /* Does the content width fit the allocation with minus a possible scrollbar ? */
1551                       priv->hscrollbar_visible =
1552                         child_scroll_width > allocation->width -
1553                         (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1554
1555                       /* Now that we've guessed the hscrollbar, does the
1556                        * content height fit the possible new allocation height ?
1557                        */
1558                       priv->vscrollbar_visible =
1559                         child_scroll_height > allocation->height -
1560                         (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1561
1562                       /* Now that we've guessed the vscrollbar, does the
1563                        * content width fit the possible new allocation width ?
1564                        */
1565                       priv->hscrollbar_visible =
1566                         child_scroll_width > allocation->width -
1567                         (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1568                     }
1569                   else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1570                     {
1571                       priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1572                       priv->vscrollbar_visible = child_scroll_height > allocation->height -
1573                         (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1574                     }
1575                 }
1576               else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1577                 {
1578                   priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1579
1580                   if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1581                     priv->hscrollbar_visible =
1582                       child_scroll_width > allocation->width -
1583                       (priv->vscrollbar_visible ? 0 : sb_width + sb_spacing);
1584                   else
1585                     priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1586                 }
1587             }
1588           else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1589             {
1590               if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1591                 gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
1592               else
1593                 gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
1594
1595               if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1596                 {
1597                   /* First try without a horizontal scrollbar if the content
1598                    * will fit the width given the extra height of the scrollbar
1599                    */
1600                   if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
1601                     gtk_widget_get_preferred_width_for_height (child,
1602                                                                MAX (allocation->height, child_scroll_height),
1603                                                                &child_scroll_width, NULL);
1604                   else
1605                     gtk_widget_get_preferred_width_for_height (child,
1606                                                                MAX (allocation->height, child_scroll_height),
1607                                                                NULL, &child_scroll_width);
1608
1609                   if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1610                     {
1611                       /* Does the content width fit the allocation width ? */
1612                       priv->hscrollbar_visible = child_scroll_width > allocation->width;
1613
1614                       /* Does the content height fit the allocation with minus a possible scrollbar ? */
1615                       priv->vscrollbar_visible =
1616                         child_scroll_height > allocation->height -
1617                         (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1618
1619                       /* Now that we've guessed the vscrollbar, does the content
1620                        * width fit the possible new allocation width ?
1621                        */
1622                       priv->hscrollbar_visible =
1623                         child_scroll_width > allocation->width -
1624                         (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1625
1626                       /* Now that we've guessed the hscrollbar, does the content
1627                        * height fit the possible new allocation height ?
1628                        */
1629                       priv->vscrollbar_visible =
1630                         child_scroll_height > allocation->height -
1631                         (priv->hscrollbar_visible ? sb_height + sb_spacing : 0);
1632                     }
1633                   else /* priv->vscrollbar_policy != GTK_POLICY_AUTOMATIC */
1634                     {
1635                       priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1636                       priv->hscrollbar_visible = child_scroll_width > allocation->width -
1637                         (priv->vscrollbar_visible ? sb_width + sb_spacing : 0);
1638                     }
1639                 }
1640               else /* priv->hscrollbar_policy != GTK_POLICY_AUTOMATIC */
1641                 {
1642                   priv->hscrollbar_visible = priv->hscrollbar_policy != GTK_POLICY_NEVER;
1643
1644                   if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1645                     priv->vscrollbar_visible =
1646                       child_scroll_height > allocation->height -
1647                       (priv->hscrollbar_visible ? 0 : sb_height + sb_spacing);
1648                   else
1649                     priv->vscrollbar_visible = priv->vscrollbar_policy != GTK_POLICY_NEVER;
1650                 }
1651             }
1652         }
1653
1654       /* Now after guessing scrollbar visibility; fall back on the allocation
1655        * loop which observes the adjustments to detect scrollbar visibility
1656        * and also avoids infinite recursion
1657        */
1658       do
1659         {
1660           previous_hvis = priv->hscrollbar_visible;
1661           previous_vvis = priv->vscrollbar_visible;
1662           gtk_scrolled_window_allocate_child (scrolled_window, &relative_allocation);
1663
1664           /* If, after the first iteration, the hscrollbar and the
1665            * vscrollbar flip visiblity, then we need both.
1666            */
1667           if (count &&
1668               previous_hvis != priv->hscrollbar_visible &&
1669               previous_vvis != priv->vscrollbar_visible)
1670             {
1671               priv->hscrollbar_visible = TRUE;
1672               priv->vscrollbar_visible = TRUE;
1673
1674               /* Make sure we dont do the height-for-width guess on the
1675                * next allocate.
1676                */
1677               priv->trust_sb_visibility = TRUE;
1678
1679               /* A queue resize is incoming so we will be immediately reinvoked */
1680               return;
1681             }
1682           
1683           count++;
1684         }
1685       while (previous_hvis != priv->hscrollbar_visible ||
1686              previous_vvis != priv->vscrollbar_visible);
1687     }
1688   else
1689     {
1690       priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1691       priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1692       gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1693     }
1694
1695   if (count > 1)
1696     /* If the scrollbars have flipped visibility, showing/hiding them will
1697      * cause a resize to be queued, we need to blindly trust those visibility
1698      * states on the next incoming allocate() and avoid guessing the visibility
1699      * with height-for-width apis.
1700      */
1701     priv->trust_sb_visibility = TRUE;
1702   else
1703     priv->trust_sb_visibility = FALSE;
1704
1705   if (priv->hscrollbar_visible)
1706     {
1707       if (!gtk_widget_get_visible (priv->hscrollbar))
1708         gtk_widget_show (priv->hscrollbar);
1709
1710       child_allocation.x = relative_allocation.x;
1711       if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1712           priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1713         child_allocation.y = (relative_allocation.y +
1714                               relative_allocation.height +
1715                               sb_spacing +
1716                               (priv->shadow_type == GTK_SHADOW_NONE ?
1717                                0 : style->ythickness));
1718       else
1719         child_allocation.y = 0;
1720
1721       child_allocation.width = relative_allocation.width;
1722       child_allocation.height = sb_height;
1723       child_allocation.x += allocation->x;
1724       child_allocation.y += allocation->y;
1725
1726       if (priv->shadow_type != GTK_SHADOW_NONE)
1727         {
1728           if (!scrollbars_within_bevel)
1729             {
1730               child_allocation.x -= style->xthickness;
1731               child_allocation.width += 2 * style->xthickness;
1732             }
1733           else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1734                    GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1735             {
1736               child_allocation.y -= style->ythickness;
1737             }
1738           else
1739             {
1740               child_allocation.y += style->ythickness;
1741             }
1742         }
1743
1744       gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1745     }
1746   else if (gtk_widget_get_visible (priv->hscrollbar))
1747     gtk_widget_hide (priv->hscrollbar);
1748
1749   if (priv->vscrollbar_visible)
1750     {
1751       if (!gtk_widget_get_visible (priv->vscrollbar))
1752         gtk_widget_show (priv->vscrollbar);
1753
1754       if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL && 
1755            (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1756             priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1757           (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR && 
1758            (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1759             priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1760         child_allocation.x = (relative_allocation.x +
1761                               relative_allocation.width +
1762                               sb_spacing +
1763                               (priv->shadow_type == GTK_SHADOW_NONE ?
1764                                0 : style->xthickness));
1765       else
1766         child_allocation.x = 0;
1767
1768       child_allocation.y = relative_allocation.y;
1769       child_allocation.width = sb_width;
1770       child_allocation.height = relative_allocation.height;
1771       child_allocation.x += allocation->x;
1772       child_allocation.y += allocation->y;
1773
1774       if (priv->shadow_type != GTK_SHADOW_NONE)
1775         {
1776           if (!scrollbars_within_bevel)
1777             {
1778               child_allocation.y -= style->ythickness;
1779               child_allocation.height += 2 * style->ythickness;
1780             }
1781           else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1782                    GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1783             {
1784               child_allocation.x -= style->xthickness;
1785             }
1786           else
1787             {
1788               child_allocation.x += style->xthickness;
1789             }
1790         }
1791
1792       gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1793     }
1794   else if (gtk_widget_get_visible (priv->vscrollbar))
1795     gtk_widget_hide (priv->vscrollbar);
1796 }
1797
1798 static gboolean
1799 gtk_scrolled_window_scroll_event (GtkWidget      *widget,
1800                                   GdkEventScroll *event)
1801 {
1802   GtkScrolledWindowPrivate *priv;
1803   GtkScrolledWindow *scrolled_window;
1804   GtkWidget *range;
1805
1806   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1807   g_return_val_if_fail (event != NULL, FALSE);  
1808
1809   scrolled_window = GTK_SCROLLED_WINDOW (widget);
1810   priv = scrolled_window->priv;
1811
1812   if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1813     range = priv->vscrollbar;
1814   else
1815     range = priv->hscrollbar;
1816
1817   if (range && gtk_widget_get_visible (range))
1818     {
1819       GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE (range));
1820       gdouble delta;
1821
1822       delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1823
1824       gtk_adjustment_set_value (adj, adj->value + delta);
1825
1826       return TRUE;
1827     }
1828
1829   return FALSE;
1830 }
1831
1832 static gboolean
1833 gtk_scrolled_window_focus (GtkWidget        *widget,
1834                            GtkDirectionType  direction)
1835 {
1836   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1837   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1838   GtkWidget *child;
1839   gboolean had_focus_child;
1840
1841   had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1842
1843   if (priv->focus_out)
1844     {
1845       priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1846       return FALSE;
1847     }
1848   
1849   if (gtk_widget_is_focus (widget))
1850     return FALSE;
1851
1852   /* We only put the scrolled window itself in the focus chain if it
1853    * isn't possible to focus any children.
1854    */
1855   child = gtk_bin_get_child (GTK_BIN (widget));
1856   if (child)
1857     {
1858       if (gtk_widget_child_focus (child, direction))
1859         return TRUE;
1860     }
1861
1862   if (!had_focus_child && gtk_widget_get_can_focus (widget))
1863     {
1864       gtk_widget_grab_focus (widget);
1865       return TRUE;
1866     }
1867   else
1868     return FALSE;
1869 }
1870
1871 static void
1872 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1873                                         gpointer       data)
1874 {
1875   GtkScrolledWindowPrivate *priv;;
1876   GtkScrolledWindow *scrolled_window;
1877
1878   g_return_if_fail (adjustment != NULL);
1879   g_return_if_fail (data != NULL);
1880
1881   scrolled_window = GTK_SCROLLED_WINDOW (data);
1882   priv = scrolled_window->priv;
1883
1884   if (priv->hscrollbar &&
1885       adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1886     {
1887       if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1888         {
1889           gboolean visible;
1890
1891           visible = priv->hscrollbar_visible;
1892           priv->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1893                                               adjustment->page_size);
1894           if (priv->hscrollbar_visible != visible)
1895             {
1896               priv->trust_sb_visibility = TRUE;
1897               gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1898             }
1899         }
1900     }
1901   else if (priv->vscrollbar &&
1902            adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1903     {
1904       if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1905         {
1906           gboolean visible;
1907
1908           visible = priv->vscrollbar_visible;
1909           priv->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1910                                               adjustment->page_size);
1911           if (priv->vscrollbar_visible != visible)
1912             {
1913               priv->trust_sb_visibility = TRUE;
1914               gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1915             }
1916         }
1917     }
1918 }
1919
1920 static void
1921 gtk_scrolled_window_add (GtkContainer *container,
1922                          GtkWidget    *child)
1923 {
1924   GtkScrolledWindowPrivate *priv;
1925   GtkScrolledWindow *scrolled_window;
1926   GtkBin *bin;
1927   GtkWidget *child_widget;
1928   GtkAdjustment *hadj, *vadj;
1929
1930   bin = GTK_BIN (container);
1931   child_widget = gtk_bin_get_child (bin);
1932   g_return_if_fail (child_widget == NULL);
1933
1934   scrolled_window = GTK_SCROLLED_WINDOW (container);
1935   priv = scrolled_window->priv;
1936
1937   _gtk_bin_set_child (bin, child);
1938   gtk_widget_set_parent (child, GTK_WIDGET (bin));
1939
1940   hadj = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->priv->hscrollbar));
1941   vadj = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->priv->vscrollbar));
1942
1943   if (GTK_IS_SCROLLABLE (child))
1944     g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
1945   else
1946     g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1947                "use gtk_scrolled_window_add_with_viewport() instead");
1948 }
1949
1950 static void
1951 gtk_scrolled_window_remove (GtkContainer *container,
1952                             GtkWidget    *child)
1953 {
1954   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1955   g_return_if_fail (child != NULL);
1956   g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1957
1958   g_object_set (child, "hadjustment", NULL, "vadjustment", NULL, NULL);
1959
1960   /* chain parent class handler to remove child */
1961   GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1962 }
1963
1964 /**
1965  * gtk_scrolled_window_add_with_viewport:
1966  * @scrolled_window: a #GtkScrolledWindow
1967  * @child: the widget you want to scroll
1968  *
1969  * Used to add children without native scrolling capabilities. This
1970  * is simply a convenience function; it is equivalent to adding the
1971  * unscrollable child to a viewport, then adding the viewport to the
1972  * scrolled window. If a child has native scrolling, use
1973  * gtk_container_add() instead of this function.
1974  *
1975  * The viewport scrolls the child by moving its #GdkWindow, and takes
1976  * the size of the child to be the size of its toplevel #GdkWindow. 
1977  * This will be very wrong for most widgets that support native scrolling;
1978  * for example, if you add a widget such as #GtkTreeView with a viewport,
1979  * the whole widget will scroll, including the column headings. Thus, 
1980  * widgets with native scrolling support should not be used with the 
1981  * #GtkViewport proxy.
1982  *
1983  * A widget supports scrolling natively if it implements the
1984  * #GtkScrollable interface.
1985  */
1986 void
1987 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1988                                        GtkWidget         *child)
1989 {
1990   GtkBin *bin;
1991   GtkWidget *viewport;
1992   GtkWidget *child_widget;
1993
1994   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1995   g_return_if_fail (GTK_IS_WIDGET (child));
1996   g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1997
1998   bin = GTK_BIN (scrolled_window);
1999   child_widget = gtk_bin_get_child (bin);
2000
2001   if (child_widget)
2002     {
2003       g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
2004       g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
2005
2006       viewport = child_widget;
2007     }
2008   else
2009     {
2010       viewport =
2011         gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
2012                           gtk_scrolled_window_get_vadjustment (scrolled_window));
2013       gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
2014     }
2015
2016   gtk_widget_show (viewport);
2017   gtk_container_add (GTK_CONTAINER (viewport), child);
2018 }
2019
2020 /*
2021  * _gtk_scrolled_window_get_spacing:
2022  * @scrolled_window: a scrolled window
2023  * 
2024  * Gets the spacing between the scrolled window's scrollbars and
2025  * the scrolled widget. Used by GtkCombo
2026  * 
2027  * Return value: the spacing, in pixels.
2028  */
2029 gint
2030 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
2031 {
2032   GtkScrolledWindowClass *class;
2033     
2034   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2035
2036   class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
2037
2038   if (class->scrollbar_spacing >= 0)
2039     return class->scrollbar_spacing;
2040   else
2041     {
2042       gint scrollbar_spacing;
2043       
2044       gtk_widget_style_get (GTK_WIDGET (scrolled_window),
2045                             "scrollbar-spacing", &scrollbar_spacing,
2046                             NULL);
2047
2048       return scrollbar_spacing;
2049     }
2050 }
2051
2052
2053 static void
2054 gtk_scrolled_window_get_preferred_size (GtkWidget      *widget,
2055                                         GtkOrientation  orientation,
2056                                         gint           *minimum_size,
2057                                         gint           *natural_size)
2058 {
2059   GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
2060   GtkScrolledWindowPrivate *priv = scrolled_window->priv;
2061   GtkBin *bin = GTK_BIN (scrolled_window);
2062   gint extra_width;
2063   gint extra_height;
2064   gint scrollbar_spacing;
2065   GtkRequisition hscrollbar_requisition;
2066   GtkRequisition vscrollbar_requisition;
2067   GtkRequisition minimum_req, natural_req;
2068   GtkStyle *style;
2069   GtkWidget *child;
2070   gint min_child_size, nat_child_size;
2071
2072   scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
2073
2074   extra_width = 0;
2075   extra_height = 0;
2076   minimum_req.width = 0;
2077   minimum_req.height = 0;
2078   natural_req.width = 0;
2079   natural_req.height = 0;
2080
2081   gtk_widget_get_preferred_size (priv->hscrollbar,
2082                                  &hscrollbar_requisition, NULL);
2083   gtk_widget_get_preferred_size (priv->vscrollbar,
2084                                  &vscrollbar_requisition, NULL);
2085
2086   child = gtk_bin_get_child (bin);
2087   if (child && gtk_widget_get_visible (child))
2088     {
2089       if (orientation == GTK_ORIENTATION_HORIZONTAL)
2090         {
2091           gtk_widget_get_preferred_width (child,
2092                                           &min_child_size,
2093                                           &nat_child_size);
2094
2095           if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
2096             {
2097               minimum_req.width += min_child_size;
2098               natural_req.width += nat_child_size;
2099             }
2100           else
2101             {
2102               gint min_content_width = priv->min_content_width;
2103
2104               if (min_content_width >= 0)
2105                 {
2106                   minimum_req.width = MAX (minimum_req.width, min_content_width);
2107                   natural_req.width = MAX (natural_req.width, min_content_width);
2108                   extra_width = -1;
2109                 }
2110               else
2111                 {
2112                   minimum_req.width += vscrollbar_requisition.width;
2113                   natural_req.width += vscrollbar_requisition.width;
2114                 }
2115             }
2116         }
2117       else /* GTK_ORIENTATION_VERTICAL */
2118         {
2119           gtk_widget_get_preferred_height (child,
2120                                            &min_child_size,
2121                                            &nat_child_size);
2122
2123           if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
2124             {
2125               minimum_req.height += min_child_size;
2126               natural_req.height += nat_child_size;
2127             }
2128           else
2129             {
2130               gint min_content_height = priv->min_content_height;
2131
2132               if (min_content_height >= 0)
2133                 {
2134                   minimum_req.height = MAX (minimum_req.height, min_content_height);
2135                   natural_req.height = MAX (natural_req.height, min_content_height);
2136                   extra_height = -1;
2137                 }
2138               else
2139                 {
2140                   minimum_req.height += vscrollbar_requisition.height;
2141                   natural_req.height += vscrollbar_requisition.height;
2142                 }
2143             }
2144         }
2145     }
2146
2147   if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2148       priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2149     {
2150       minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
2151       natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
2152       if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
2153         extra_height = scrollbar_spacing + hscrollbar_requisition.height;
2154     }
2155
2156   if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
2157       priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2158     {
2159       minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
2160       natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
2161       if (!extra_width || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
2162         extra_width = scrollbar_spacing + vscrollbar_requisition.width;
2163     }
2164
2165   minimum_req.width  += MAX (0, extra_width);
2166   minimum_req.height += MAX (0, extra_height);
2167   natural_req.width  += MAX (0, extra_width);
2168   natural_req.height += MAX (0, extra_height);
2169
2170   if (priv->shadow_type != GTK_SHADOW_NONE)
2171     {
2172       style = gtk_widget_get_style (GTK_WIDGET (widget));
2173       minimum_req.width += 2 * style->xthickness;
2174       minimum_req.height += 2 * style->ythickness;
2175       natural_req.width += 2 * style->xthickness;
2176       natural_req.height += 2 * style->ythickness;
2177     }
2178
2179   if (orientation == GTK_ORIENTATION_HORIZONTAL)
2180     {
2181       if (minimum_size)
2182         *minimum_size = minimum_req.width;
2183       if (natural_size)
2184         *natural_size = natural_req.width;
2185     }
2186   else
2187     {
2188       if (minimum_size)
2189         *minimum_size = minimum_req.height;
2190       if (natural_size)
2191         *natural_size = natural_req.height;
2192     }
2193 }
2194
2195 static void     
2196 gtk_scrolled_window_get_preferred_width (GtkWidget *widget,
2197                                          gint      *minimum_size,
2198                                          gint      *natural_size)
2199 {
2200   gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
2201 }
2202
2203 static void
2204 gtk_scrolled_window_get_preferred_height (GtkWidget *widget,
2205                                           gint      *minimum_size,
2206                                           gint      *natural_size)
2207 {  
2208   gtk_scrolled_window_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
2209 }
2210
2211 static void
2212 gtk_scrolled_window_get_preferred_height_for_width (GtkWidget *widget,
2213                                                     gint       width,
2214                                                     gint      *minimum_height,
2215                                                     gint      *natural_height)
2216 {
2217   g_return_if_fail (GTK_IS_WIDGET (widget));
2218
2219   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
2220 }
2221
2222 static void
2223 gtk_scrolled_window_get_preferred_width_for_height (GtkWidget *widget,
2224                                                     gint       height,
2225                                                     gint      *minimum_width,
2226                                                     gint      *natural_width)
2227 {
2228   g_return_if_fail (GTK_IS_WIDGET (widget));
2229
2230   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
2231 }
2232
2233 /**
2234  * gtk_scrolled_window_get_min_content_width:
2235  * @scrolled_window: a #GtkScrolledWindow
2236  *
2237  * Gets the minimum content width of @scrolled_window, or -1 if not set.
2238  *
2239  * Returns: the minimum content width
2240  *
2241  * Since: 3.0
2242  */
2243 gint
2244 gtk_scrolled_window_get_min_content_width (GtkScrolledWindow *scrolled_window)
2245 {
2246   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2247
2248   return scrolled_window->priv->min_content_width;
2249 }
2250
2251 /**
2252  * gtk_scrolled_window_set_min_content_width:
2253  * @scrolled_window: a #GtkScrolledWindow
2254  * @width: the minimal content width
2255  *
2256  * Sets the minimum width that @scrolled_window should keep visible.
2257  * Note that this can and (usually will) be smaller than the minimum
2258  * size of the content.
2259  *
2260  * Since: 3.0
2261  */
2262 void
2263 gtk_scrolled_window_set_min_content_width (GtkScrolledWindow *scrolled_window,
2264                                            gint               width)
2265 {
2266   GtkScrolledWindowPrivate *priv;
2267
2268   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2269
2270   priv = scrolled_window->priv;
2271
2272   if (priv->min_content_width != width)
2273     {
2274       priv->min_content_width = width;
2275
2276       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2277
2278       g_object_notify (G_OBJECT (scrolled_window), "min-content-width");
2279     }
2280 }
2281
2282 /**
2283  * gtk_scrolled_window_get_min_content_height:
2284  * @scrolled_window: a #GtkScrolledWindow
2285  *
2286  * Gets the minimal content height of @scrolled_window, or -1 if not set.
2287  *
2288  * Returns: the minimal content height
2289  *
2290  * Since: 3.0
2291  */
2292 gint
2293 gtk_scrolled_window_get_min_content_height (GtkScrolledWindow *scrolled_window)
2294 {
2295   g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
2296
2297   return scrolled_window->priv->min_content_height;
2298 }
2299
2300 /**
2301  * gtk_scrolled_window_set_min_content_height:
2302  * @scrolled_window: a #GtkScrolledWindow
2303  * @height: the minimal content height
2304  *
2305  * Sets the minimum height that @scrolled_window should keep visible.
2306  * Note that this can and (usually will) be smaller than the minimum
2307  * size of the content.
2308  *
2309  * Since: 3.0
2310  */
2311 void
2312 gtk_scrolled_window_set_min_content_height (GtkScrolledWindow *scrolled_window,
2313                                             gint               height)
2314 {
2315   GtkScrolledWindowPrivate *priv;
2316
2317   g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
2318
2319   priv = scrolled_window->priv;
2320
2321   if (priv->min_content_height != height)
2322     {
2323       priv->min_content_height = height;
2324
2325       gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
2326
2327       g_object_notify (G_OBJECT (scrolled_window), "min-content-height");
2328     }
2329 }