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 relative_allocation;
1049 gboolean scrollbars_within_bevel;
1051 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1053 if (!scrollbars_within_bevel)
1055 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1057 relative_allocation.x -= widget->style->xthickness;
1058 relative_allocation.y -= widget->style->ythickness;
1059 relative_allocation.width += 2 * widget->style->xthickness;
1060 relative_allocation.height += 2 * widget->style->ythickness;
1064 GtkContainer *container = GTK_CONTAINER (widget);
1067 border_width = gtk_container_get_border_width (container);
1069 relative_allocation.x = border_width;
1070 relative_allocation.y = border_width;
1071 relative_allocation.width = widget->allocation.width - 2 * border_width;
1072 relative_allocation.height = widget->allocation.height - 2 * border_width;
1075 gtk_paint_shadow (widget->style, widget->window,
1076 GTK_STATE_NORMAL, scrolled_window->shadow_type,
1077 area, widget, "scrolled_window",
1078 widget->allocation.x + relative_allocation.x,
1079 widget->allocation.y + relative_allocation.y,
1080 relative_allocation.width,
1081 relative_allocation.height);
1086 gtk_scrolled_window_expose (GtkWidget *widget,
1087 GdkEventExpose *event)
1089 if (gtk_widget_is_drawable (widget))
1091 gtk_scrolled_window_paint (widget, &event->area);
1093 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->expose_event (widget, event);
1100 gtk_scrolled_window_forall (GtkContainer *container,
1101 gboolean include_internals,
1102 GtkCallback callback,
1103 gpointer callback_data)
1105 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1106 g_return_if_fail (callback != NULL);
1108 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1112 if (include_internals)
1114 GtkScrolledWindow *scrolled_window;
1116 scrolled_window = GTK_SCROLLED_WINDOW (container);
1118 if (scrolled_window->vscrollbar)
1119 callback (scrolled_window->vscrollbar, callback_data);
1120 if (scrolled_window->hscrollbar)
1121 callback (scrolled_window->hscrollbar, callback_data);
1126 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1127 GtkScrollType scroll,
1128 gboolean horizontal)
1130 GtkAdjustment *adjustment = NULL;
1134 case GTK_SCROLL_STEP_UP:
1135 scroll = GTK_SCROLL_STEP_BACKWARD;
1138 case GTK_SCROLL_STEP_DOWN:
1139 scroll = GTK_SCROLL_STEP_FORWARD;
1142 case GTK_SCROLL_STEP_LEFT:
1143 scroll = GTK_SCROLL_STEP_BACKWARD;
1146 case GTK_SCROLL_STEP_RIGHT:
1147 scroll = GTK_SCROLL_STEP_FORWARD;
1150 case GTK_SCROLL_PAGE_UP:
1151 scroll = GTK_SCROLL_PAGE_BACKWARD;
1154 case GTK_SCROLL_PAGE_DOWN:
1155 scroll = GTK_SCROLL_PAGE_FORWARD;
1158 case GTK_SCROLL_PAGE_LEFT:
1159 scroll = GTK_SCROLL_STEP_BACKWARD;
1162 case GTK_SCROLL_PAGE_RIGHT:
1163 scroll = GTK_SCROLL_STEP_FORWARD;
1166 case GTK_SCROLL_STEP_BACKWARD:
1167 case GTK_SCROLL_STEP_FORWARD:
1168 case GTK_SCROLL_PAGE_BACKWARD:
1169 case GTK_SCROLL_PAGE_FORWARD:
1170 case GTK_SCROLL_START:
1171 case GTK_SCROLL_END:
1174 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1178 if ((horizontal && (!scrolled_window->hscrollbar || !scrolled_window->hscrollbar_visible)) ||
1179 (!horizontal && (!scrolled_window->vscrollbar || !scrolled_window->vscrollbar_visible)))
1184 if (scrolled_window->hscrollbar)
1185 adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
1189 if (scrolled_window->vscrollbar)
1190 adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
1195 gdouble value = adjustment->value;
1199 case GTK_SCROLL_STEP_FORWARD:
1200 value += adjustment->step_increment;
1202 case GTK_SCROLL_STEP_BACKWARD:
1203 value -= adjustment->step_increment;
1205 case GTK_SCROLL_PAGE_FORWARD:
1206 value += adjustment->page_increment;
1208 case GTK_SCROLL_PAGE_BACKWARD:
1209 value -= adjustment->page_increment;
1211 case GTK_SCROLL_START:
1212 value = adjustment->lower;
1214 case GTK_SCROLL_END:
1215 value = adjustment->upper;
1218 g_assert_not_reached ();
1222 gtk_adjustment_set_value (adjustment, value);
1231 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1232 GtkDirectionType direction_type)
1234 GtkWidget *toplevel;
1236 /* Focus out of the scrolled window entirely. We do this by setting
1237 * a flag, then propagating the focus motion to the notebook.
1239 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1240 if (!gtk_widget_is_toplevel (toplevel))
1243 g_object_ref (scrolled_window);
1245 scrolled_window->focus_out = TRUE;
1246 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1247 scrolled_window->focus_out = FALSE;
1249 g_object_unref (scrolled_window);
1253 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1254 GtkAllocation *allocation)
1256 GtkScrolledWindow *scrolled_window;
1257 GtkScrolledWindowPrivate *priv;
1258 gint scrollbar_spacing;
1261 g_return_if_fail (widget != NULL);
1262 g_return_if_fail (allocation != NULL);
1264 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1265 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1267 priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
1269 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1270 allocation->x = border_width;
1271 allocation->y = border_width;
1273 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1275 allocation->x += widget->style->xthickness;
1276 allocation->y += widget->style->ythickness;
1279 allocation->width = MAX (1, (gint)widget->allocation.width - allocation->x * 2);
1280 allocation->height = MAX (1, (gint)widget->allocation.height - allocation->y * 2);
1282 if (scrolled_window->vscrollbar_visible)
1284 GtkRequisition vscrollbar_requisition;
1287 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
1288 &vscrollbar_requisition);
1289 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1292 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1293 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1295 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1296 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1297 allocation->x += (vscrollbar_requisition.width + scrollbar_spacing);
1299 allocation->width = MAX (1, allocation->width - (vscrollbar_requisition.width + scrollbar_spacing));
1301 if (scrolled_window->hscrollbar_visible)
1303 GtkRequisition hscrollbar_requisition;
1304 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
1305 &hscrollbar_requisition);
1307 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1308 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1309 allocation->y += (hscrollbar_requisition.height + scrollbar_spacing);
1311 allocation->height = MAX (1, allocation->height - (hscrollbar_requisition.height + scrollbar_spacing));
1316 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1317 GtkAllocation *allocation)
1319 GtkScrolledWindow *scrolled_window;
1320 GtkScrolledWindowPrivate *priv;
1322 GtkAllocation relative_allocation;
1323 GtkAllocation child_allocation;
1325 gboolean scrollbars_within_bevel;
1326 gint scrollbar_spacing;
1329 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1330 g_return_if_fail (allocation != NULL);
1332 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1333 bin = GTK_BIN (scrolled_window);
1335 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1336 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1338 priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
1340 border_width = gtk_container_get_border_width (GTK_CONTAINER (scrolled_window));
1341 widget->allocation = *allocation;
1343 if (scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1344 scrolled_window->hscrollbar_visible = TRUE;
1345 else if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
1346 scrolled_window->hscrollbar_visible = FALSE;
1347 if (scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1348 scrolled_window->vscrollbar_visible = TRUE;
1349 else if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
1350 scrolled_window->vscrollbar_visible = FALSE;
1352 child = gtk_bin_get_child (bin);
1353 if (child && gtk_widget_get_visible (child))
1355 gboolean previous_hvis;
1356 gboolean previous_vvis;
1361 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1363 child_allocation.x = relative_allocation.x + allocation->x;
1364 child_allocation.y = relative_allocation.y + allocation->y;
1365 child_allocation.width = relative_allocation.width;
1366 child_allocation.height = relative_allocation.height;
1368 previous_hvis = scrolled_window->hscrollbar_visible;
1369 previous_vvis = scrolled_window->vscrollbar_visible;
1371 gtk_widget_size_allocate (child, &child_allocation);
1373 /* If, after the first iteration, the hscrollbar and the
1374 * vscrollbar flip visiblity, then we need both.
1377 previous_hvis != scrolled_window->hscrollbar_visible &&
1378 previous_vvis != scrolled_window->vscrollbar_visible)
1380 scrolled_window->hscrollbar_visible = TRUE;
1381 scrolled_window->vscrollbar_visible = TRUE;
1383 /* a new resize is already queued at this point,
1384 * so we will immediatedly get reinvoked
1391 while (previous_hvis != scrolled_window->hscrollbar_visible ||
1392 previous_vvis != scrolled_window->vscrollbar_visible);
1396 scrolled_window->hscrollbar_visible = scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS;
1397 scrolled_window->vscrollbar_visible = scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS;
1398 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1401 if (scrolled_window->hscrollbar_visible)
1403 GtkRequisition hscrollbar_requisition;
1404 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
1405 &hscrollbar_requisition);
1407 if (!gtk_widget_get_visible (scrolled_window->hscrollbar))
1408 gtk_widget_show (scrolled_window->hscrollbar);
1410 child_allocation.x = relative_allocation.x;
1411 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1412 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1413 child_allocation.y = (relative_allocation.y +
1414 relative_allocation.height +
1416 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
1417 0 : widget->style->ythickness));
1419 child_allocation.y = border_width;
1421 child_allocation.width = relative_allocation.width;
1422 child_allocation.height = hscrollbar_requisition.height;
1423 child_allocation.x += allocation->x;
1424 child_allocation.y += allocation->y;
1426 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1428 if (!scrollbars_within_bevel)
1430 child_allocation.x -= widget->style->xthickness;
1431 child_allocation.width += 2 * widget->style->xthickness;
1433 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1434 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1436 child_allocation.y -= widget->style->ythickness;
1440 child_allocation.y += widget->style->ythickness;
1444 gtk_widget_size_allocate (scrolled_window->hscrollbar, &child_allocation);
1446 else if (gtk_widget_get_visible (scrolled_window->hscrollbar))
1447 gtk_widget_hide (scrolled_window->hscrollbar);
1449 if (scrolled_window->vscrollbar_visible)
1451 GtkRequisition vscrollbar_requisition;
1452 if (!gtk_widget_get_visible (scrolled_window->vscrollbar))
1453 gtk_widget_show (scrolled_window->vscrollbar);
1455 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
1456 &vscrollbar_requisition);
1458 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1459 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1460 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1461 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1462 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1463 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1464 child_allocation.x = (relative_allocation.x +
1465 relative_allocation.width +
1467 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
1468 0 : widget->style->xthickness));
1470 child_allocation.x = border_width;
1472 child_allocation.y = relative_allocation.y;
1473 child_allocation.width = vscrollbar_requisition.width;
1474 child_allocation.height = relative_allocation.height;
1475 child_allocation.x += allocation->x;
1476 child_allocation.y += allocation->y;
1478 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1480 if (!scrollbars_within_bevel)
1482 child_allocation.y -= widget->style->ythickness;
1483 child_allocation.height += 2 * widget->style->ythickness;
1485 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1486 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1488 child_allocation.x -= widget->style->xthickness;
1492 child_allocation.x += widget->style->xthickness;
1496 gtk_widget_size_allocate (scrolled_window->vscrollbar, &child_allocation);
1498 else if (gtk_widget_get_visible (scrolled_window->vscrollbar))
1499 gtk_widget_hide (scrolled_window->vscrollbar);
1503 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1504 GdkEventScroll *event)
1508 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1509 g_return_val_if_fail (event != NULL, FALSE);
1511 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1512 range = GTK_SCROLLED_WINDOW (widget)->vscrollbar;
1514 range = GTK_SCROLLED_WINDOW (widget)->hscrollbar;
1516 if (range && gtk_widget_get_visible (range))
1518 GtkAdjustment *adj = GTK_RANGE (range)->adjustment;
1521 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1523 gtk_adjustment_set_value (adj, adj->value + delta);
1532 gtk_scrolled_window_focus (GtkWidget *widget,
1533 GtkDirectionType direction)
1535 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1537 gboolean had_focus_child;
1539 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1541 if (scrolled_window->focus_out)
1543 scrolled_window->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1547 if (gtk_widget_is_focus (widget))
1550 /* We only put the scrolled window itself in the focus chain if it
1551 * isn't possible to focus any children.
1553 child = gtk_bin_get_child (GTK_BIN (widget));
1556 if (gtk_widget_child_focus (child, direction))
1560 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1562 gtk_widget_grab_focus (widget);
1570 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1573 GtkScrolledWindow *scrolled_win;
1575 g_return_if_fail (adjustment != NULL);
1576 g_return_if_fail (data != NULL);
1578 scrolled_win = GTK_SCROLLED_WINDOW (data);
1580 if (scrolled_win->hscrollbar &&
1581 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->hscrollbar)))
1583 if (scrolled_win->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1587 visible = scrolled_win->hscrollbar_visible;
1588 scrolled_win->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1589 adjustment->page_size);
1590 if (scrolled_win->hscrollbar_visible != visible)
1591 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1594 else if (scrolled_win->vscrollbar &&
1595 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->vscrollbar)))
1597 if (scrolled_win->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1601 visible = scrolled_win->vscrollbar_visible;
1602 scrolled_win->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1603 adjustment->page_size);
1604 if (scrolled_win->vscrollbar_visible != visible)
1605 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1611 gtk_scrolled_window_add (GtkContainer *container,
1614 GtkScrolledWindow *scrolled_window;
1616 GtkWidget *child_widget;
1618 bin = GTK_BIN (container);
1619 child_widget = gtk_bin_get_child (bin);
1620 g_return_if_fail (child_widget == NULL);
1622 scrolled_window = GTK_SCROLLED_WINDOW (container);
1624 _gtk_bin_set_child (bin, child);
1625 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1627 /* this is a temporary message */
1628 if (!gtk_widget_set_scroll_adjustments (child,
1629 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
1630 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar))))
1631 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1632 "use gtk_scrolled_window_add_with_viewport() instead");
1636 gtk_scrolled_window_remove (GtkContainer *container,
1639 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1640 g_return_if_fail (child != NULL);
1641 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1643 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1645 /* chain parent class handler to remove child */
1646 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1650 * gtk_scrolled_window_add_with_viewport:
1651 * @scrolled_window: a #GtkScrolledWindow
1652 * @child: the widget you want to scroll
1654 * Used to add children without native scrolling capabilities. This
1655 * is simply a convenience function; it is equivalent to adding the
1656 * unscrollable child to a viewport, then adding the viewport to the
1657 * scrolled window. If a child has native scrolling, use
1658 * gtk_container_add() instead of this function.
1660 * The viewport scrolls the child by moving its #GdkWindow, and takes
1661 * the size of the child to be the size of its toplevel #GdkWindow.
1662 * This will be very wrong for most widgets that support native scrolling;
1663 * for example, if you add a widget such as #GtkTreeView with a viewport,
1664 * the whole widget will scroll, including the column headings. Thus,
1665 * widgets with native scrolling support should not be used with the
1666 * #GtkViewport proxy.
1668 * A widget supports scrolling natively if the
1669 * set_scroll_adjustments_signal field in #GtkWidgetClass is non-zero,
1670 * i.e. has been filled in with a valid signal identifier.
1673 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1677 GtkWidget *viewport;
1678 GtkWidget *child_widget;
1680 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1681 g_return_if_fail (GTK_IS_WIDGET (child));
1682 g_return_if_fail (child->parent == NULL);
1684 bin = GTK_BIN (scrolled_window);
1685 child_widget = gtk_bin_get_child (bin);
1689 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
1690 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
1692 viewport = child_widget;
1697 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1698 gtk_scrolled_window_get_vadjustment (scrolled_window));
1699 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1702 gtk_widget_show (viewport);
1703 gtk_container_add (GTK_CONTAINER (viewport), child);
1707 * _gtk_scrolled_window_get_spacing:
1708 * @scrolled_window: a scrolled window
1710 * Gets the spacing between the scrolled window's scrollbars and
1711 * the scrolled widget. Used by GtkCombo
1713 * Return value: the spacing, in pixels.
1716 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
1718 GtkScrolledWindowClass *class;
1720 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
1722 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
1724 if (class->scrollbar_spacing >= 0)
1725 return class->scrollbar_spacing;
1728 gint scrollbar_spacing;
1730 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
1731 "scrollbar-spacing", &scrollbar_spacing,
1734 return scrollbar_spacing;
1740 gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface)
1742 iface->get_width = gtk_scrolled_window_get_width;
1743 iface->get_height = gtk_scrolled_window_get_height;
1744 iface->get_height_for_width = gtk_scrolled_window_get_height_for_width;
1745 iface->get_width_for_height = gtk_scrolled_window_get_width_for_height;
1749 gtk_scrolled_window_get_size (GtkSizeRequest *widget,
1750 GtkOrientation orientation,
1754 GtkScrolledWindow *scrolled_window;
1758 gint scrollbar_spacing;
1759 GtkRequisition hscrollbar_requisition;
1760 GtkRequisition vscrollbar_requisition;
1761 GtkRequisition minimum_req, natural_req;
1763 gint min_child_size, nat_child_size;
1766 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1767 bin = GTK_BIN (scrolled_window);
1769 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1773 minimum_req.width = 0;
1774 minimum_req.height = 0;
1775 natural_req.width = 0;
1776 natural_req.height = 0;
1778 gtk_widget_size_request (scrolled_window->hscrollbar,
1779 &hscrollbar_requisition);
1780 gtk_widget_size_request (scrolled_window->vscrollbar,
1781 &vscrollbar_requisition);
1783 child = gtk_bin_get_child (bin);
1784 if (child && gtk_widget_get_visible (child))
1786 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1788 gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
1792 if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
1794 minimum_req.width += min_child_size;
1795 natural_req.width += nat_child_size;
1799 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1801 if (aux_info && aux_info->width > 0)
1803 minimum_req.width += aux_info->width;
1804 natural_req.width += aux_info->width;
1809 minimum_req.width += vscrollbar_requisition.width;
1810 natural_req.width += vscrollbar_requisition.width;
1814 else /* GTK_ORIENTATION_VERTICAL */
1816 gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
1820 if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
1822 minimum_req.height += min_child_size;
1823 natural_req.height += nat_child_size;
1827 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1829 if (aux_info && aux_info->height > 0)
1831 minimum_req.height += aux_info->height;
1832 natural_req.height += aux_info->height;
1837 minimum_req.height += hscrollbar_requisition.height;
1838 natural_req.height += hscrollbar_requisition.height;
1844 if (scrolled_window->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1845 scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1847 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
1848 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
1849 if (!extra_height || scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1850 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
1853 if (scrolled_window->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1854 scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1856 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
1857 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
1858 if (!extra_height || scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1859 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
1862 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1863 minimum_req.width += border_width * 2 + MAX (0, extra_width);
1864 minimum_req.height += border_width * 2 + MAX (0, extra_height);
1865 natural_req.width += border_width * 2 + MAX (0, extra_width);
1866 natural_req.height += border_width * 2 + MAX (0, extra_height);
1868 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1870 minimum_req.width += 2 * GTK_WIDGET (widget)->style->xthickness;
1871 minimum_req.height += 2 * GTK_WIDGET (widget)->style->ythickness;
1872 natural_req.width += 2 * GTK_WIDGET (widget)->style->xthickness;
1873 natural_req.height += 2 * GTK_WIDGET (widget)->style->ythickness;
1876 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1879 *minimum_size = minimum_req.width;
1881 *natural_size = natural_req.width;
1886 *minimum_size = minimum_req.height;
1888 *natural_size = natural_req.height;
1893 gtk_scrolled_window_get_width (GtkSizeRequest *widget,
1897 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1901 gtk_scrolled_window_get_height (GtkSizeRequest *widget,
1905 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1909 gtk_scrolled_window_get_height_for_width (GtkSizeRequest *widget,
1911 gint *minimum_height,
1912 gint *natural_height)
1914 g_return_if_fail (GTK_IS_WIDGET (widget));
1916 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
1920 gtk_scrolled_window_get_width_for_height (GtkSizeRequest *widget,
1922 gint *minimum_width,
1923 gint *natural_width)
1925 g_return_if_fail (GTK_IS_WIDGET (widget));
1927 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);