1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
29 #include <gdk/gdkkeysyms.h>
30 #include "gtkbindings.h"
31 #include "gtksizerequest.h"
32 #include "gtkmarshalers.h"
33 #include "gtkscrolledwindow.h"
34 #include "gtkwindow.h"
35 #include "gtkprivate.h"
38 /* scrolled window policy and size requisition handling:
40 * gtk size requisition works as follows:
41 * a widget upon size-request reports the width and height that it finds
42 * to be best suited to display its contents, including children.
43 * the width and/or height reported from a widget upon size requisition
44 * may be overidden by the user by specifying a width and/or height
45 * other than 0 through gtk_widget_set_size_request().
47 * a scrolled window needs (for implementing all three policy types) to
48 * request its width and height based on two different rationales.
49 * 1) the user wants the scrolled window to just fit into the space
50 * that it gets allocated for a specifc dimension.
51 * 1.1) this does not apply if the user specified a concrete value
52 * value for that specific dimension by either specifying usize for the
53 * scrolled window or for its child.
54 * 2) the user wants the scrolled window to take as much space up as
55 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
57 * also, kinda obvious:
58 * 3) a user would certainly not have choosen a scrolled window as a container
59 * for the child, if the resulting allocation takes up more space than the
60 * child would have allocated without the scrolled window.
63 * A) from 1) follows: the scrolled window shouldn't request more space for a
64 * specifc dimension than is required at minimum.
65 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
66 * window (done automatically) or by usize of the child (needs to be checked).
67 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
69 * D) from 3) follows: the scrolled window child's minimum width and minimum height
70 * under A) at least correspond to the space taken up by its scrollbars.
73 #define DEFAULT_SCROLLBAR_SPACING 3
76 gboolean window_placement_set;
77 GtkCornerType real_window_placement;
78 } GtkScrolledWindowPrivate;
80 #define GTK_SCROLLED_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCROLLED_WINDOW, GtkScrolledWindowPrivate))
86 PROP_HSCROLLBAR_POLICY,
87 PROP_VSCROLLBAR_POLICY,
88 PROP_WINDOW_PLACEMENT,
89 PROP_WINDOW_PLACEMENT_SET,
101 static void gtk_scrolled_window_destroy (GtkObject *object);
102 static void gtk_scrolled_window_set_property (GObject *object,
106 static void gtk_scrolled_window_get_property (GObject *object,
111 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
112 GdkScreen *previous_screen);
113 static gboolean gtk_scrolled_window_expose (GtkWidget *widget,
114 GdkEventExpose *event);
115 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
116 GtkAllocation *allocation);
117 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
118 GdkEventScroll *event);
119 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
120 GtkDirectionType direction);
121 static void gtk_scrolled_window_add (GtkContainer *container,
123 static void gtk_scrolled_window_remove (GtkContainer *container,
125 static void gtk_scrolled_window_forall (GtkContainer *container,
126 gboolean include_internals,
127 GtkCallback callback,
128 gpointer callback_data);
129 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
130 GtkScrollType scroll,
131 gboolean horizontal);
132 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
133 GtkDirectionType direction_type);
135 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
136 GtkAllocation *allocation);
137 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
140 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
142 static void gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface);
143 static void gtk_scrolled_window_get_width (GtkSizeRequest *widget,
146 static void gtk_scrolled_window_get_height (GtkSizeRequest *widget,
149 static void gtk_scrolled_window_get_height_for_width (GtkSizeRequest *layout,
151 gint *minimum_height,
152 gint *natural_height);
153 static void gtk_scrolled_window_get_width_for_height (GtkSizeRequest *layout,
155 gint *minimum_height,
156 gint *natural_height);
158 static guint signals[LAST_SIGNAL] = {0};
160 G_DEFINE_TYPE_WITH_CODE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN,
161 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
162 gtk_scrolled_window_size_request_init))
166 add_scroll_binding (GtkBindingSet *binding_set,
168 GdkModifierType mask,
169 GtkScrollType scroll,
172 guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
174 gtk_binding_entry_add_signal (binding_set, keyval, mask,
176 GTK_TYPE_SCROLL_TYPE, scroll,
177 G_TYPE_BOOLEAN, horizontal);
178 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
180 GTK_TYPE_SCROLL_TYPE, scroll,
181 G_TYPE_BOOLEAN, horizontal);
185 add_tab_bindings (GtkBindingSet *binding_set,
186 GdkModifierType modifiers,
187 GtkDirectionType direction)
189 gtk_binding_entry_add_signal (binding_set, GDK_Tab, modifiers,
191 GTK_TYPE_DIRECTION_TYPE, direction);
192 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, modifiers,
194 GTK_TYPE_DIRECTION_TYPE, direction);
198 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
200 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
201 GtkObjectClass *object_class;
202 GtkWidgetClass *widget_class;
203 GtkContainerClass *container_class;
204 GtkBindingSet *binding_set;
206 object_class = (GtkObjectClass*) class;
207 widget_class = (GtkWidgetClass*) class;
208 container_class = (GtkContainerClass*) class;
210 gobject_class->set_property = gtk_scrolled_window_set_property;
211 gobject_class->get_property = gtk_scrolled_window_get_property;
213 object_class->destroy = gtk_scrolled_window_destroy;
215 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
216 widget_class->expose_event = gtk_scrolled_window_expose;
217 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
218 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
219 widget_class->focus = gtk_scrolled_window_focus;
221 container_class->add = gtk_scrolled_window_add;
222 container_class->remove = gtk_scrolled_window_remove;
223 container_class->forall = gtk_scrolled_window_forall;
225 class->scrollbar_spacing = -1;
227 class->scroll_child = gtk_scrolled_window_scroll_child;
228 class->move_focus_out = gtk_scrolled_window_move_focus_out;
230 g_object_class_install_property (gobject_class,
232 g_param_spec_object ("hadjustment",
233 P_("Horizontal Adjustment"),
234 P_("The GtkAdjustment for the horizontal position"),
236 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_object ("vadjustment",
240 P_("Vertical Adjustment"),
241 P_("The GtkAdjustment for the vertical position"),
243 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
244 g_object_class_install_property (gobject_class,
245 PROP_HSCROLLBAR_POLICY,
246 g_param_spec_enum ("hscrollbar-policy",
247 P_("Horizontal Scrollbar Policy"),
248 P_("When the horizontal scrollbar is displayed"),
249 GTK_TYPE_POLICY_TYPE,
251 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
252 g_object_class_install_property (gobject_class,
253 PROP_VSCROLLBAR_POLICY,
254 g_param_spec_enum ("vscrollbar-policy",
255 P_("Vertical Scrollbar Policy"),
256 P_("When the vertical scrollbar is displayed"),
257 GTK_TYPE_POLICY_TYPE,
259 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
261 g_object_class_install_property (gobject_class,
262 PROP_WINDOW_PLACEMENT,
263 g_param_spec_enum ("window-placement",
264 P_("Window Placement"),
265 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
266 GTK_TYPE_CORNER_TYPE,
268 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
271 * GtkScrolledWindow:window-placement-set:
273 * Whether "window-placement" should be used to determine the location
274 * of the contents with respect to the scrollbars. Otherwise, the
275 * "gtk-scrolled-window-placement" setting is used.
279 g_object_class_install_property (gobject_class,
280 PROP_WINDOW_PLACEMENT_SET,
281 g_param_spec_boolean ("window-placement-set",
282 P_("Window Placement Set"),
283 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
285 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
286 g_object_class_install_property (gobject_class,
288 g_param_spec_enum ("shadow-type",
290 P_("Style of bevel around the contents"),
291 GTK_TYPE_SHADOW_TYPE,
293 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
296 * GtkScrolledWindow:scrollbars-within-bevel:
298 * Whether to place scrollbars within the scrolled window's bevel.
302 gtk_widget_class_install_style_property (widget_class,
303 g_param_spec_boolean ("scrollbars-within-bevel",
304 P_("Scrollbars within bevel"),
305 P_("Place scrollbars within the scrolled window's bevel"),
307 GTK_PARAM_READABLE));
309 gtk_widget_class_install_style_property (widget_class,
310 g_param_spec_int ("scrollbar-spacing",
311 P_("Scrollbar spacing"),
312 P_("Number of pixels between the scrollbars and the scrolled window"),
315 DEFAULT_SCROLLBAR_SPACING,
316 GTK_PARAM_READABLE));
319 * GtkSettings:gtk-scrolled-window-placement:
321 * Where the contents of scrolled windows are located with respect to the
322 * scrollbars, if not overridden by the scrolled window's own placement.
326 gtk_settings_install_property (g_param_spec_enum ("gtk-scrolled-window-placement",
327 P_("Scrolled Window Placement"),
328 P_("Where the contents of scrolled windows are located with respect to the scrollbars, if not overridden by the scrolled window's own placement."),
329 GTK_TYPE_CORNER_TYPE,
331 G_PARAM_READABLE | G_PARAM_WRITABLE));
334 signals[SCROLL_CHILD] =
335 g_signal_new (I_("scroll-child"),
336 G_TYPE_FROM_CLASS (object_class),
337 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
338 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
340 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
342 GTK_TYPE_SCROLL_TYPE,
344 signals[MOVE_FOCUS_OUT] =
345 g_signal_new (I_("move-focus-out"),
346 G_TYPE_FROM_CLASS (object_class),
347 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
348 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
350 _gtk_marshal_VOID__ENUM,
352 GTK_TYPE_DIRECTION_TYPE);
354 binding_set = gtk_binding_set_by_class (class);
356 add_scroll_binding (binding_set, GDK_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
357 add_scroll_binding (binding_set, GDK_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
358 add_scroll_binding (binding_set, GDK_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
359 add_scroll_binding (binding_set, GDK_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
361 add_scroll_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
362 add_scroll_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
363 add_scroll_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
364 add_scroll_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
366 add_scroll_binding (binding_set, GDK_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
367 add_scroll_binding (binding_set, GDK_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
368 add_scroll_binding (binding_set, GDK_Home, 0, GTK_SCROLL_START, FALSE);
369 add_scroll_binding (binding_set, GDK_End, 0, GTK_SCROLL_END, FALSE);
371 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
372 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
374 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
378 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
380 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
381 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
383 scrolled_window->hscrollbar = NULL;
384 scrolled_window->vscrollbar = NULL;
385 scrolled_window->hscrollbar_policy = GTK_POLICY_ALWAYS;
386 scrolled_window->vscrollbar_policy = GTK_POLICY_ALWAYS;
387 scrolled_window->hscrollbar_visible = FALSE;
388 scrolled_window->vscrollbar_visible = FALSE;
389 scrolled_window->focus_out = FALSE;
390 scrolled_window->window_placement = GTK_CORNER_TOP_LEFT;
391 gtk_scrolled_window_update_real_placement (scrolled_window);
395 * gtk_scrolled_window_new:
396 * @hadjustment: (allow-none): horizontal adjustment
397 * @vadjustment: (allow-none): vertical adjustment
399 * Creates a new scrolled window.
401 * The two arguments are the scrolled window's adjustments; these will be
402 * shared with the scrollbars and the child widget to keep the bars in sync
403 * with the child. Usually you want to pass %NULL for the adjustments, which
404 * will cause the scrolled window to create them for you.
406 * Returns: a new scrolled window
409 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
410 GtkAdjustment *vadjustment)
412 GtkWidget *scrolled_window;
415 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
418 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
420 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
421 "hadjustment", hadjustment,
422 "vadjustment", vadjustment,
425 return scrolled_window;
429 * gtk_scrolled_window_set_hadjustment:
430 * @scrolled_window: a #GtkScrolledWindow
431 * @hadjustment: horizontal scroll adjustment
433 * Sets the #GtkAdjustment for the horizontal scrollbar.
436 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
437 GtkAdjustment *hadjustment)
442 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
444 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
446 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
448 bin = GTK_BIN (scrolled_window);
450 if (!scrolled_window->hscrollbar)
452 gtk_widget_push_composite_child ();
453 scrolled_window->hscrollbar = gtk_hscrollbar_new (hadjustment);
454 gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
455 gtk_widget_pop_composite_child ();
457 gtk_widget_set_parent (scrolled_window->hscrollbar, GTK_WIDGET (scrolled_window));
458 g_object_ref (scrolled_window->hscrollbar);
459 gtk_widget_show (scrolled_window->hscrollbar);
463 GtkAdjustment *old_adjustment;
465 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
466 if (old_adjustment == hadjustment)
469 g_signal_handlers_disconnect_by_func (old_adjustment,
470 gtk_scrolled_window_adjustment_changed,
472 gtk_range_set_adjustment (GTK_RANGE (scrolled_window->hscrollbar),
475 hadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
476 g_signal_connect (hadjustment,
478 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
480 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
482 child = gtk_bin_get_child (bin);
484 gtk_widget_set_scroll_adjustments (child,
485 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
486 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
488 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
492 * gtk_scrolled_window_set_vadjustment:
493 * @scrolled_window: a #GtkScrolledWindow
494 * @vadjustment: vertical scroll adjustment
496 * Sets the #GtkAdjustment for the vertical scrollbar.
499 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
500 GtkAdjustment *vadjustment)
505 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
507 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
509 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
511 bin = GTK_BIN (scrolled_window);
513 if (!scrolled_window->vscrollbar)
515 gtk_widget_push_composite_child ();
516 scrolled_window->vscrollbar = gtk_vscrollbar_new (vadjustment);
517 gtk_widget_set_composite_name (scrolled_window->vscrollbar, "vscrollbar");
518 gtk_widget_pop_composite_child ();
520 gtk_widget_set_parent (scrolled_window->vscrollbar, GTK_WIDGET (scrolled_window));
521 g_object_ref (scrolled_window->vscrollbar);
522 gtk_widget_show (scrolled_window->vscrollbar);
526 GtkAdjustment *old_adjustment;
528 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
529 if (old_adjustment == vadjustment)
532 g_signal_handlers_disconnect_by_func (old_adjustment,
533 gtk_scrolled_window_adjustment_changed,
535 gtk_range_set_adjustment (GTK_RANGE (scrolled_window->vscrollbar),
538 vadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
539 g_signal_connect (vadjustment,
541 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
543 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
545 child = gtk_bin_get_child (bin);
547 gtk_widget_set_scroll_adjustments (child,
548 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
549 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
551 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
555 * gtk_scrolled_window_get_hadjustment:
556 * @scrolled_window: a #GtkScrolledWindow
558 * Returns the horizontal scrollbar's adjustment, used to connect the
559 * horizontal scrollbar to the child widget's horizontal scroll
562 * Returns: the horizontal #GtkAdjustment
565 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
567 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
569 return (scrolled_window->hscrollbar ?
570 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)) :
575 * gtk_scrolled_window_get_vadjustment:
576 * @scrolled_window: a #GtkScrolledWindow
578 * Returns the vertical scrollbar's adjustment, used to connect the
579 * vertical scrollbar to the child widget's vertical scroll
582 * Returns: the vertical #GtkAdjustment
585 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
587 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
589 return (scrolled_window->vscrollbar ?
590 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)) :
595 * gtk_scrolled_window_get_hscrollbar:
596 * @scrolled_window: a #GtkScrolledWindow
598 * Returns the horizontal scrollbar of @scrolled_window.
600 * Returns: the horizontal scrollbar of the scrolled window, or
601 * %NULL if it does not have one.
606 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
608 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
610 return scrolled_window->hscrollbar;
614 * gtk_scrolled_window_get_vscrollbar:
615 * @scrolled_window: a #GtkScrolledWindow
617 * Returns the vertical scrollbar of @scrolled_window.
619 * Returns: the vertical scrollbar of the scrolled window, or
620 * %NULL if it does not have one.
625 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
627 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
629 return scrolled_window->vscrollbar;
633 * gtk_scrolled_window_set_policy:
634 * @scrolled_window: a #GtkScrolledWindow
635 * @hscrollbar_policy: policy for horizontal bar
636 * @vscrollbar_policy: policy for vertical bar
638 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
640 * The policy determines when the scrollbar should appear; it is a value
641 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
642 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
643 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
644 * if needed (that is, if the slider part of the bar would be smaller
645 * than the trough - the display is larger than the page size).
648 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
649 GtkPolicyType hscrollbar_policy,
650 GtkPolicyType vscrollbar_policy)
652 GObject *object = G_OBJECT (scrolled_window);
654 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
656 if ((scrolled_window->hscrollbar_policy != hscrollbar_policy) ||
657 (scrolled_window->vscrollbar_policy != vscrollbar_policy))
659 scrolled_window->hscrollbar_policy = hscrollbar_policy;
660 scrolled_window->vscrollbar_policy = vscrollbar_policy;
662 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
664 g_object_freeze_notify (object);
665 g_object_notify (object, "hscrollbar-policy");
666 g_object_notify (object, "vscrollbar-policy");
667 g_object_thaw_notify (object);
672 * gtk_scrolled_window_get_policy:
673 * @scrolled_window: a #GtkScrolledWindow
674 * @hscrollbar_policy: location to store the policy for the horizontal
675 * scrollbar, or %NULL.
676 * @vscrollbar_policy: location to store the policy for the vertical
677 * scrollbar, or %NULL.
679 * Retrieves the current policy values for the horizontal and vertical
680 * scrollbars. See gtk_scrolled_window_set_policy().
683 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
684 GtkPolicyType *hscrollbar_policy,
685 GtkPolicyType *vscrollbar_policy)
687 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
689 if (hscrollbar_policy)
690 *hscrollbar_policy = scrolled_window->hscrollbar_policy;
691 if (vscrollbar_policy)
692 *vscrollbar_policy = scrolled_window->vscrollbar_policy;
696 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
698 GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
699 GtkSettings *settings;
701 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
703 if (priv->window_placement_set || settings == NULL)
704 priv->real_window_placement = scrolled_window->window_placement;
706 g_object_get (settings,
707 "gtk-scrolled-window-placement",
708 &priv->real_window_placement,
713 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
714 GtkCornerType window_placement)
716 if (scrolled_window->window_placement != window_placement)
718 scrolled_window->window_placement = window_placement;
720 gtk_scrolled_window_update_real_placement (scrolled_window);
721 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
723 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
728 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
729 gboolean placement_set,
730 gboolean emit_resize)
732 GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
734 if (priv->window_placement_set != placement_set)
736 priv->window_placement_set = placement_set;
738 gtk_scrolled_window_update_real_placement (scrolled_window);
740 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
742 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
747 * gtk_scrolled_window_set_placement:
748 * @scrolled_window: a #GtkScrolledWindow
749 * @window_placement: position of the child window
751 * Sets the placement of the contents with respect to the scrollbars
752 * for the scrolled window.
754 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
755 * in the top left, with the scrollbars underneath and to the right.
756 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
757 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
759 * See also gtk_scrolled_window_get_placement() and
760 * gtk_scrolled_window_unset_placement().
763 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
764 GtkCornerType window_placement)
766 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
768 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
769 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
773 * gtk_scrolled_window_get_placement:
774 * @scrolled_window: a #GtkScrolledWindow
776 * Gets the placement of the contents with respect to the scrollbars
777 * for the scrolled window. See gtk_scrolled_window_set_placement().
779 * Return value: the current placement value.
781 * See also gtk_scrolled_window_set_placement() and
782 * gtk_scrolled_window_unset_placement().
785 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
787 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
789 return scrolled_window->window_placement;
793 * gtk_scrolled_window_unset_placement:
794 * @scrolled_window: a #GtkScrolledWindow
796 * Unsets the placement of the contents with respect to the scrollbars
797 * for the scrolled window. If no window placement is set for a scrolled
798 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
800 * See also gtk_scrolled_window_set_placement() and
801 * gtk_scrolled_window_get_placement().
806 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
808 GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
810 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
812 if (priv->window_placement_set)
814 priv->window_placement_set = FALSE;
816 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
818 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
823 * gtk_scrolled_window_set_shadow_type:
824 * @scrolled_window: a #GtkScrolledWindow
825 * @type: kind of shadow to draw around scrolled window contents
827 * Changes the type of shadow drawn around the contents of
832 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
835 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
836 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
838 if (scrolled_window->shadow_type != type)
840 scrolled_window->shadow_type = type;
842 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
843 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
845 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
847 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
852 * gtk_scrolled_window_get_shadow_type:
853 * @scrolled_window: a #GtkScrolledWindow
855 * Gets the shadow type of the scrolled window. See
856 * gtk_scrolled_window_set_shadow_type().
858 * Return value: the current shadow type
861 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
863 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
865 return scrolled_window->shadow_type;
869 gtk_scrolled_window_destroy (GtkObject *object)
871 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
873 if (scrolled_window->hscrollbar)
875 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
876 gtk_scrolled_window_adjustment_changed,
878 gtk_widget_unparent (scrolled_window->hscrollbar);
879 gtk_widget_destroy (scrolled_window->hscrollbar);
880 g_object_unref (scrolled_window->hscrollbar);
881 scrolled_window->hscrollbar = NULL;
883 if (scrolled_window->vscrollbar)
885 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)),
886 gtk_scrolled_window_adjustment_changed,
888 gtk_widget_unparent (scrolled_window->vscrollbar);
889 gtk_widget_destroy (scrolled_window->vscrollbar);
890 g_object_unref (scrolled_window->vscrollbar);
891 scrolled_window->vscrollbar = NULL;
894 GTK_OBJECT_CLASS (gtk_scrolled_window_parent_class)->destroy (object);
898 gtk_scrolled_window_set_property (GObject *object,
903 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
907 case PROP_HADJUSTMENT:
908 gtk_scrolled_window_set_hadjustment (scrolled_window,
909 g_value_get_object (value));
911 case PROP_VADJUSTMENT:
912 gtk_scrolled_window_set_vadjustment (scrolled_window,
913 g_value_get_object (value));
915 case PROP_HSCROLLBAR_POLICY:
916 gtk_scrolled_window_set_policy (scrolled_window,
917 g_value_get_enum (value),
918 scrolled_window->vscrollbar_policy);
920 case PROP_VSCROLLBAR_POLICY:
921 gtk_scrolled_window_set_policy (scrolled_window,
922 scrolled_window->hscrollbar_policy,
923 g_value_get_enum (value));
925 case PROP_WINDOW_PLACEMENT:
926 gtk_scrolled_window_set_placement_internal (scrolled_window,
927 g_value_get_enum (value));
929 case PROP_WINDOW_PLACEMENT_SET:
930 gtk_scrolled_window_set_placement_set (scrolled_window,
931 g_value_get_boolean (value),
934 case PROP_SHADOW_TYPE:
935 gtk_scrolled_window_set_shadow_type (scrolled_window,
936 g_value_get_enum (value));
939 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
945 gtk_scrolled_window_get_property (GObject *object,
950 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
951 GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
955 case PROP_HADJUSTMENT:
956 g_value_set_object (value,
957 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
959 case PROP_VADJUSTMENT:
960 g_value_set_object (value,
961 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
963 case PROP_HSCROLLBAR_POLICY:
964 g_value_set_enum (value, scrolled_window->hscrollbar_policy);
966 case PROP_VSCROLLBAR_POLICY:
967 g_value_set_enum (value, scrolled_window->vscrollbar_policy);
969 case PROP_WINDOW_PLACEMENT:
970 g_value_set_enum (value, scrolled_window->window_placement);
972 case PROP_WINDOW_PLACEMENT_SET:
973 g_value_set_boolean (value, priv->window_placement_set);
975 case PROP_SHADOW_TYPE:
976 g_value_set_enum (value, scrolled_window->shadow_type);
979 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
985 traverse_container (GtkWidget *widget,
988 if (GTK_IS_SCROLLED_WINDOW (widget))
990 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
991 gtk_widget_queue_resize (widget);
993 else if (GTK_IS_CONTAINER (widget))
994 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
998 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1002 list = gtk_window_list_toplevels ();
1004 for (l = list; l; l = l->next)
1005 gtk_container_forall (GTK_CONTAINER (l->data),
1006 traverse_container, NULL);
1012 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1013 GdkScreen *previous_screen)
1015 GtkSettings *settings;
1016 guint window_placement_connection;
1018 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1020 if (!gtk_widget_has_screen (widget))
1023 settings = gtk_widget_get_settings (widget);
1025 window_placement_connection =
1026 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1027 "gtk-scrolled-window-connection"));
1029 if (window_placement_connection)
1032 window_placement_connection =
1033 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1034 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1035 g_object_set_data (G_OBJECT (settings),
1036 I_("gtk-scrolled-window-connection"),
1037 GUINT_TO_POINTER (window_placement_connection));
1041 gtk_scrolled_window_paint (GtkWidget *widget,
1044 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1046 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1048 GtkAllocation allocation;
1049 GtkAllocation relative_allocation;
1051 gboolean scrollbars_within_bevel;
1053 style = gtk_widget_get_style (widget);
1054 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1056 gtk_widget_get_allocation (widget, &allocation);
1058 if (!scrollbars_within_bevel)
1060 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1062 relative_allocation.x -= style->xthickness;
1063 relative_allocation.y -= style->ythickness;
1064 relative_allocation.width += 2 * style->xthickness;
1065 relative_allocation.height += 2 * style->ythickness;
1069 GtkContainer *container = GTK_CONTAINER (widget);
1072 border_width = gtk_container_get_border_width (container);
1074 relative_allocation.x = border_width;
1075 relative_allocation.y = border_width;
1076 relative_allocation.width = allocation.width - 2 * border_width;
1077 relative_allocation.height = allocation.height - 2 * border_width;
1080 gtk_paint_shadow (style,
1081 gtk_widget_get_window (widget),
1082 GTK_STATE_NORMAL, scrolled_window->shadow_type,
1083 area, widget, "scrolled_window",
1084 allocation.x + relative_allocation.x,
1085 allocation.y + relative_allocation.y,
1086 relative_allocation.width,
1087 relative_allocation.height);
1092 gtk_scrolled_window_expose (GtkWidget *widget,
1093 GdkEventExpose *event)
1095 if (gtk_widget_is_drawable (widget))
1097 gtk_scrolled_window_paint (widget, &event->area);
1099 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->expose_event (widget, event);
1106 gtk_scrolled_window_forall (GtkContainer *container,
1107 gboolean include_internals,
1108 GtkCallback callback,
1109 gpointer callback_data)
1111 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1112 g_return_if_fail (callback != NULL);
1114 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1118 if (include_internals)
1120 GtkScrolledWindow *scrolled_window;
1122 scrolled_window = GTK_SCROLLED_WINDOW (container);
1124 if (scrolled_window->vscrollbar)
1125 callback (scrolled_window->vscrollbar, callback_data);
1126 if (scrolled_window->hscrollbar)
1127 callback (scrolled_window->hscrollbar, callback_data);
1132 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1133 GtkScrollType scroll,
1134 gboolean horizontal)
1136 GtkAdjustment *adjustment = NULL;
1140 case GTK_SCROLL_STEP_UP:
1141 scroll = GTK_SCROLL_STEP_BACKWARD;
1144 case GTK_SCROLL_STEP_DOWN:
1145 scroll = GTK_SCROLL_STEP_FORWARD;
1148 case GTK_SCROLL_STEP_LEFT:
1149 scroll = GTK_SCROLL_STEP_BACKWARD;
1152 case GTK_SCROLL_STEP_RIGHT:
1153 scroll = GTK_SCROLL_STEP_FORWARD;
1156 case GTK_SCROLL_PAGE_UP:
1157 scroll = GTK_SCROLL_PAGE_BACKWARD;
1160 case GTK_SCROLL_PAGE_DOWN:
1161 scroll = GTK_SCROLL_PAGE_FORWARD;
1164 case GTK_SCROLL_PAGE_LEFT:
1165 scroll = GTK_SCROLL_STEP_BACKWARD;
1168 case GTK_SCROLL_PAGE_RIGHT:
1169 scroll = GTK_SCROLL_STEP_FORWARD;
1172 case GTK_SCROLL_STEP_BACKWARD:
1173 case GTK_SCROLL_STEP_FORWARD:
1174 case GTK_SCROLL_PAGE_BACKWARD:
1175 case GTK_SCROLL_PAGE_FORWARD:
1176 case GTK_SCROLL_START:
1177 case GTK_SCROLL_END:
1180 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1184 if ((horizontal && (!scrolled_window->hscrollbar || !scrolled_window->hscrollbar_visible)) ||
1185 (!horizontal && (!scrolled_window->vscrollbar || !scrolled_window->vscrollbar_visible)))
1190 if (scrolled_window->hscrollbar)
1191 adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
1195 if (scrolled_window->vscrollbar)
1196 adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
1201 gdouble value = adjustment->value;
1205 case GTK_SCROLL_STEP_FORWARD:
1206 value += adjustment->step_increment;
1208 case GTK_SCROLL_STEP_BACKWARD:
1209 value -= adjustment->step_increment;
1211 case GTK_SCROLL_PAGE_FORWARD:
1212 value += adjustment->page_increment;
1214 case GTK_SCROLL_PAGE_BACKWARD:
1215 value -= adjustment->page_increment;
1217 case GTK_SCROLL_START:
1218 value = adjustment->lower;
1220 case GTK_SCROLL_END:
1221 value = adjustment->upper;
1224 g_assert_not_reached ();
1228 gtk_adjustment_set_value (adjustment, value);
1237 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1238 GtkDirectionType direction_type)
1240 GtkWidget *toplevel;
1242 /* Focus out of the scrolled window entirely. We do this by setting
1243 * a flag, then propagating the focus motion to the notebook.
1245 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1246 if (!gtk_widget_is_toplevel (toplevel))
1249 g_object_ref (scrolled_window);
1251 scrolled_window->focus_out = TRUE;
1252 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1253 scrolled_window->focus_out = FALSE;
1255 g_object_unref (scrolled_window);
1259 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1260 GtkAllocation *allocation)
1262 GtkAllocation widget_allocation;
1263 GtkScrolledWindow *scrolled_window;
1264 GtkScrolledWindowPrivate *priv;
1266 gint scrollbar_spacing;
1269 g_return_if_fail (widget != NULL);
1270 g_return_if_fail (allocation != NULL);
1272 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1273 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1275 priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
1277 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1278 allocation->x = border_width;
1279 allocation->y = border_width;
1281 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1283 style = gtk_widget_get_style (widget);
1284 allocation->x += style->xthickness;
1285 allocation->y += style->ythickness;
1288 gtk_widget_get_allocation (widget, &widget_allocation);
1289 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1290 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1292 if (scrolled_window->vscrollbar_visible)
1294 GtkRequisition vscrollbar_requisition;
1297 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
1298 &vscrollbar_requisition);
1299 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1302 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1303 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1305 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1306 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1307 allocation->x += (vscrollbar_requisition.width + scrollbar_spacing);
1309 allocation->width = MAX (1, allocation->width - (vscrollbar_requisition.width + scrollbar_spacing));
1311 if (scrolled_window->hscrollbar_visible)
1313 GtkRequisition hscrollbar_requisition;
1314 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
1315 &hscrollbar_requisition);
1317 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1318 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1319 allocation->y += (hscrollbar_requisition.height + scrollbar_spacing);
1321 allocation->height = MAX (1, allocation->height - (hscrollbar_requisition.height + scrollbar_spacing));
1326 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1327 GtkAllocation *allocation)
1329 GtkScrolledWindow *scrolled_window;
1330 GtkScrolledWindowPrivate *priv;
1333 GtkAllocation relative_allocation;
1334 GtkAllocation child_allocation;
1336 gboolean scrollbars_within_bevel;
1337 gint scrollbar_spacing;
1340 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1341 g_return_if_fail (allocation != NULL);
1343 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1344 bin = GTK_BIN (scrolled_window);
1346 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1348 style = gtk_widget_get_style (widget);
1349 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1351 priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
1353 border_width = gtk_container_get_border_width (GTK_CONTAINER (scrolled_window));
1355 gtk_widget_set_allocation (widget, allocation);
1357 if (scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1358 scrolled_window->hscrollbar_visible = TRUE;
1359 else if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
1360 scrolled_window->hscrollbar_visible = FALSE;
1361 if (scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1362 scrolled_window->vscrollbar_visible = TRUE;
1363 else if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
1364 scrolled_window->vscrollbar_visible = FALSE;
1366 child = gtk_bin_get_child (bin);
1367 if (child && gtk_widget_get_visible (child))
1369 gboolean previous_hvis;
1370 gboolean previous_vvis;
1375 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1377 child_allocation.x = relative_allocation.x + allocation->x;
1378 child_allocation.y = relative_allocation.y + allocation->y;
1379 child_allocation.width = relative_allocation.width;
1380 child_allocation.height = relative_allocation.height;
1382 previous_hvis = scrolled_window->hscrollbar_visible;
1383 previous_vvis = scrolled_window->vscrollbar_visible;
1385 gtk_widget_size_allocate (child, &child_allocation);
1387 /* If, after the first iteration, the hscrollbar and the
1388 * vscrollbar flip visiblity, then we need both.
1391 previous_hvis != scrolled_window->hscrollbar_visible &&
1392 previous_vvis != scrolled_window->vscrollbar_visible)
1394 scrolled_window->hscrollbar_visible = TRUE;
1395 scrolled_window->vscrollbar_visible = TRUE;
1397 /* a new resize is already queued at this point,
1398 * so we will immediatedly get reinvoked
1405 while (previous_hvis != scrolled_window->hscrollbar_visible ||
1406 previous_vvis != scrolled_window->vscrollbar_visible);
1410 scrolled_window->hscrollbar_visible = scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS;
1411 scrolled_window->vscrollbar_visible = scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS;
1412 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1415 if (scrolled_window->hscrollbar_visible)
1417 GtkRequisition hscrollbar_requisition;
1418 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
1419 &hscrollbar_requisition);
1421 if (!gtk_widget_get_visible (scrolled_window->hscrollbar))
1422 gtk_widget_show (scrolled_window->hscrollbar);
1424 child_allocation.x = relative_allocation.x;
1425 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1426 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1427 child_allocation.y = (relative_allocation.y +
1428 relative_allocation.height +
1430 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
1431 0 : style->ythickness));
1433 child_allocation.y = border_width;
1435 child_allocation.width = relative_allocation.width;
1436 child_allocation.height = hscrollbar_requisition.height;
1437 child_allocation.x += allocation->x;
1438 child_allocation.y += allocation->y;
1440 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1442 if (!scrollbars_within_bevel)
1444 child_allocation.x -= style->xthickness;
1445 child_allocation.width += 2 * style->xthickness;
1447 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1448 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1450 child_allocation.y -= style->ythickness;
1454 child_allocation.y += style->ythickness;
1458 gtk_widget_size_allocate (scrolled_window->hscrollbar, &child_allocation);
1460 else if (gtk_widget_get_visible (scrolled_window->hscrollbar))
1461 gtk_widget_hide (scrolled_window->hscrollbar);
1463 if (scrolled_window->vscrollbar_visible)
1465 GtkRequisition vscrollbar_requisition;
1466 if (!gtk_widget_get_visible (scrolled_window->vscrollbar))
1467 gtk_widget_show (scrolled_window->vscrollbar);
1469 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
1470 &vscrollbar_requisition);
1472 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1473 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1474 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1475 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1476 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1477 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1478 child_allocation.x = (relative_allocation.x +
1479 relative_allocation.width +
1481 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
1482 0 : style->xthickness));
1484 child_allocation.x = border_width;
1486 child_allocation.y = relative_allocation.y;
1487 child_allocation.width = vscrollbar_requisition.width;
1488 child_allocation.height = relative_allocation.height;
1489 child_allocation.x += allocation->x;
1490 child_allocation.y += allocation->y;
1492 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1494 if (!scrollbars_within_bevel)
1496 child_allocation.y -= style->ythickness;
1497 child_allocation.height += 2 * style->ythickness;
1499 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1500 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1502 child_allocation.x -= style->xthickness;
1506 child_allocation.x += style->xthickness;
1510 gtk_widget_size_allocate (scrolled_window->vscrollbar, &child_allocation);
1512 else if (gtk_widget_get_visible (scrolled_window->vscrollbar))
1513 gtk_widget_hide (scrolled_window->vscrollbar);
1517 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1518 GdkEventScroll *event)
1522 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1523 g_return_val_if_fail (event != NULL, FALSE);
1525 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1526 range = GTK_SCROLLED_WINDOW (widget)->vscrollbar;
1528 range = GTK_SCROLLED_WINDOW (widget)->hscrollbar;
1530 if (range && gtk_widget_get_visible (range))
1532 GtkAdjustment *adj = GTK_RANGE (range)->adjustment;
1535 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1537 gtk_adjustment_set_value (adj, adj->value + delta);
1546 gtk_scrolled_window_focus (GtkWidget *widget,
1547 GtkDirectionType direction)
1549 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1551 gboolean had_focus_child;
1553 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1555 if (scrolled_window->focus_out)
1557 scrolled_window->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1561 if (gtk_widget_is_focus (widget))
1564 /* We only put the scrolled window itself in the focus chain if it
1565 * isn't possible to focus any children.
1567 child = gtk_bin_get_child (GTK_BIN (widget));
1570 if (gtk_widget_child_focus (child, direction))
1574 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1576 gtk_widget_grab_focus (widget);
1584 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1587 GtkScrolledWindow *scrolled_win;
1589 g_return_if_fail (adjustment != NULL);
1590 g_return_if_fail (data != NULL);
1592 scrolled_win = GTK_SCROLLED_WINDOW (data);
1594 if (scrolled_win->hscrollbar &&
1595 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->hscrollbar)))
1597 if (scrolled_win->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1601 visible = scrolled_win->hscrollbar_visible;
1602 scrolled_win->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1603 adjustment->page_size);
1604 if (scrolled_win->hscrollbar_visible != visible)
1605 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1608 else if (scrolled_win->vscrollbar &&
1609 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->vscrollbar)))
1611 if (scrolled_win->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1615 visible = scrolled_win->vscrollbar_visible;
1616 scrolled_win->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1617 adjustment->page_size);
1618 if (scrolled_win->vscrollbar_visible != visible)
1619 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1625 gtk_scrolled_window_add (GtkContainer *container,
1628 GtkScrolledWindow *scrolled_window;
1630 GtkWidget *child_widget;
1632 bin = GTK_BIN (container);
1633 child_widget = gtk_bin_get_child (bin);
1634 g_return_if_fail (child_widget == NULL);
1636 scrolled_window = GTK_SCROLLED_WINDOW (container);
1638 _gtk_bin_set_child (bin, child);
1639 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1641 /* this is a temporary message */
1642 if (!gtk_widget_set_scroll_adjustments (child,
1643 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
1644 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar))))
1645 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1646 "use gtk_scrolled_window_add_with_viewport() instead");
1650 gtk_scrolled_window_remove (GtkContainer *container,
1653 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1654 g_return_if_fail (child != NULL);
1655 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1657 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1659 /* chain parent class handler to remove child */
1660 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1664 * gtk_scrolled_window_add_with_viewport:
1665 * @scrolled_window: a #GtkScrolledWindow
1666 * @child: the widget you want to scroll
1668 * Used to add children without native scrolling capabilities. This
1669 * is simply a convenience function; it is equivalent to adding the
1670 * unscrollable child to a viewport, then adding the viewport to the
1671 * scrolled window. If a child has native scrolling, use
1672 * gtk_container_add() instead of this function.
1674 * The viewport scrolls the child by moving its #GdkWindow, and takes
1675 * the size of the child to be the size of its toplevel #GdkWindow.
1676 * This will be very wrong for most widgets that support native scrolling;
1677 * for example, if you add a widget such as #GtkTreeView with a viewport,
1678 * the whole widget will scroll, including the column headings. Thus,
1679 * widgets with native scrolling support should not be used with the
1680 * #GtkViewport proxy.
1682 * A widget supports scrolling natively if the
1683 * set_scroll_adjustments_signal field in #GtkWidgetClass is non-zero,
1684 * i.e. has been filled in with a valid signal identifier.
1687 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1691 GtkWidget *viewport;
1692 GtkWidget *child_widget;
1694 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1695 g_return_if_fail (GTK_IS_WIDGET (child));
1696 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1698 bin = GTK_BIN (scrolled_window);
1699 child_widget = gtk_bin_get_child (bin);
1703 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
1704 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
1706 viewport = child_widget;
1711 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1712 gtk_scrolled_window_get_vadjustment (scrolled_window));
1713 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1716 gtk_widget_show (viewport);
1717 gtk_container_add (GTK_CONTAINER (viewport), child);
1721 * _gtk_scrolled_window_get_spacing:
1722 * @scrolled_window: a scrolled window
1724 * Gets the spacing between the scrolled window's scrollbars and
1725 * the scrolled widget. Used by GtkCombo
1727 * Return value: the spacing, in pixels.
1730 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
1732 GtkScrolledWindowClass *class;
1734 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
1736 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
1738 if (class->scrollbar_spacing >= 0)
1739 return class->scrollbar_spacing;
1742 gint scrollbar_spacing;
1744 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
1745 "scrollbar-spacing", &scrollbar_spacing,
1748 return scrollbar_spacing;
1754 gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface)
1756 iface->get_width = gtk_scrolled_window_get_width;
1757 iface->get_height = gtk_scrolled_window_get_height;
1758 iface->get_height_for_width = gtk_scrolled_window_get_height_for_width;
1759 iface->get_width_for_height = gtk_scrolled_window_get_width_for_height;
1763 gtk_scrolled_window_get_size (GtkSizeRequest *widget,
1764 GtkOrientation orientation,
1768 GtkScrolledWindow *scrolled_window;
1772 gint scrollbar_spacing;
1773 GtkRequisition hscrollbar_requisition;
1774 GtkRequisition vscrollbar_requisition;
1775 GtkRequisition minimum_req, natural_req;
1778 gint min_child_size, nat_child_size;
1781 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1782 bin = GTK_BIN (scrolled_window);
1784 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1788 minimum_req.width = 0;
1789 minimum_req.height = 0;
1790 natural_req.width = 0;
1791 natural_req.height = 0;
1793 gtk_widget_size_request (scrolled_window->hscrollbar,
1794 &hscrollbar_requisition);
1795 gtk_widget_size_request (scrolled_window->vscrollbar,
1796 &vscrollbar_requisition);
1798 child = gtk_bin_get_child (bin);
1799 if (child && gtk_widget_get_visible (child))
1801 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1803 gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
1807 if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
1809 minimum_req.width += min_child_size;
1810 natural_req.width += nat_child_size;
1814 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1816 if (aux_info && aux_info->width > 0)
1818 minimum_req.width += aux_info->width;
1819 natural_req.width += aux_info->width;
1824 minimum_req.width += vscrollbar_requisition.width;
1825 natural_req.width += vscrollbar_requisition.width;
1829 else /* GTK_ORIENTATION_VERTICAL */
1831 gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
1835 if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
1837 minimum_req.height += min_child_size;
1838 natural_req.height += nat_child_size;
1842 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1844 if (aux_info && aux_info->height > 0)
1846 minimum_req.height += aux_info->height;
1847 natural_req.height += aux_info->height;
1852 minimum_req.height += hscrollbar_requisition.height;
1853 natural_req.height += hscrollbar_requisition.height;
1859 if (scrolled_window->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1860 scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1862 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
1863 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
1864 if (!extra_height || scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1865 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
1868 if (scrolled_window->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1869 scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1871 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
1872 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
1873 if (!extra_height || scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1874 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
1877 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1878 minimum_req.width += border_width * 2 + MAX (0, extra_width);
1879 minimum_req.height += border_width * 2 + MAX (0, extra_height);
1880 natural_req.width += border_width * 2 + MAX (0, extra_width);
1881 natural_req.height += border_width * 2 + MAX (0, extra_height);
1883 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1885 style = gtk_widget_get_style (GTK_WIDGET (widget));
1886 minimum_req.width += 2 * style->xthickness;
1887 minimum_req.height += 2 * style->ythickness;
1888 natural_req.width += 2 * style->xthickness;
1889 natural_req.height += 2 * style->ythickness;
1892 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1895 *minimum_size = minimum_req.width;
1897 *natural_size = natural_req.width;
1902 *minimum_size = minimum_req.height;
1904 *natural_size = natural_req.height;
1909 gtk_scrolled_window_get_width (GtkSizeRequest *widget,
1913 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1917 gtk_scrolled_window_get_height (GtkSizeRequest *widget,
1921 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1925 gtk_scrolled_window_get_height_for_width (GtkSizeRequest *widget,
1927 gint *minimum_height,
1928 gint *natural_height)
1930 g_return_if_fail (GTK_IS_WIDGET (widget));
1932 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
1936 gtk_scrolled_window_get_width_for_height (GtkSizeRequest *widget,
1938 gint *minimum_width,
1939 gint *natural_width)
1941 g_return_if_fail (GTK_IS_WIDGET (widget));
1943 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);