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