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
75 struct _GtkScrolledWindowPrivate
77 GtkCornerType real_window_placement;
78 GtkWidget *hscrollbar;
79 GtkWidget *vscrollbar;
81 gboolean window_placement_set;
85 guint hscrollbar_policy : 2;
86 guint vscrollbar_policy : 2;
87 guint hscrollbar_visible : 1;
88 guint vscrollbar_visible : 1;
89 guint window_placement : 2;
90 guint focus_out : 1; /* Flag used by ::move-focus-out implementation */
98 PROP_HSCROLLBAR_POLICY,
99 PROP_VSCROLLBAR_POLICY,
100 PROP_WINDOW_PLACEMENT,
101 PROP_WINDOW_PLACEMENT_SET,
113 static void gtk_scrolled_window_destroy (GtkObject *object);
114 static void gtk_scrolled_window_set_property (GObject *object,
118 static void gtk_scrolled_window_get_property (GObject *object,
123 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
124 GdkScreen *previous_screen);
125 static gboolean gtk_scrolled_window_draw (GtkWidget *widget,
127 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
128 GtkAllocation *allocation);
129 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
130 GdkEventScroll *event);
131 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
132 GtkDirectionType direction);
133 static void gtk_scrolled_window_add (GtkContainer *container,
135 static void gtk_scrolled_window_remove (GtkContainer *container,
137 static void gtk_scrolled_window_forall (GtkContainer *container,
138 gboolean include_internals,
139 GtkCallback callback,
140 gpointer callback_data);
141 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
142 GtkScrollType scroll,
143 gboolean horizontal);
144 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
145 GtkDirectionType direction_type);
147 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
148 GtkAllocation *allocation);
149 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
152 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
154 static void gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface);
155 static void gtk_scrolled_window_get_width (GtkSizeRequest *widget,
158 static void gtk_scrolled_window_get_height (GtkSizeRequest *widget,
161 static void gtk_scrolled_window_get_height_for_width (GtkSizeRequest *layout,
163 gint *minimum_height,
164 gint *natural_height);
165 static void gtk_scrolled_window_get_width_for_height (GtkSizeRequest *layout,
167 gint *minimum_height,
168 gint *natural_height);
170 static guint signals[LAST_SIGNAL] = {0};
172 G_DEFINE_TYPE_WITH_CODE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN,
173 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
174 gtk_scrolled_window_size_request_init))
178 add_scroll_binding (GtkBindingSet *binding_set,
180 GdkModifierType mask,
181 GtkScrollType scroll,
184 guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
186 gtk_binding_entry_add_signal (binding_set, keyval, mask,
188 GTK_TYPE_SCROLL_TYPE, scroll,
189 G_TYPE_BOOLEAN, horizontal);
190 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
192 GTK_TYPE_SCROLL_TYPE, scroll,
193 G_TYPE_BOOLEAN, horizontal);
197 add_tab_bindings (GtkBindingSet *binding_set,
198 GdkModifierType modifiers,
199 GtkDirectionType direction)
201 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
203 GTK_TYPE_DIRECTION_TYPE, direction);
204 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
206 GTK_TYPE_DIRECTION_TYPE, direction);
210 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
212 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
213 GtkObjectClass *object_class;
214 GtkWidgetClass *widget_class;
215 GtkContainerClass *container_class;
216 GtkBindingSet *binding_set;
218 object_class = (GtkObjectClass*) class;
219 widget_class = (GtkWidgetClass*) class;
220 container_class = (GtkContainerClass*) class;
222 gobject_class->set_property = gtk_scrolled_window_set_property;
223 gobject_class->get_property = gtk_scrolled_window_get_property;
225 object_class->destroy = gtk_scrolled_window_destroy;
227 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
228 widget_class->draw = gtk_scrolled_window_draw;
229 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
230 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
231 widget_class->focus = gtk_scrolled_window_focus;
233 container_class->add = gtk_scrolled_window_add;
234 container_class->remove = gtk_scrolled_window_remove;
235 container_class->forall = gtk_scrolled_window_forall;
237 class->scrollbar_spacing = -1;
239 class->scroll_child = gtk_scrolled_window_scroll_child;
240 class->move_focus_out = gtk_scrolled_window_move_focus_out;
242 g_object_class_install_property (gobject_class,
244 g_param_spec_object ("hadjustment",
245 P_("Horizontal Adjustment"),
246 P_("The GtkAdjustment for the horizontal position"),
248 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
249 g_object_class_install_property (gobject_class,
251 g_param_spec_object ("vadjustment",
252 P_("Vertical Adjustment"),
253 P_("The GtkAdjustment for the vertical position"),
255 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
256 g_object_class_install_property (gobject_class,
257 PROP_HSCROLLBAR_POLICY,
258 g_param_spec_enum ("hscrollbar-policy",
259 P_("Horizontal Scrollbar Policy"),
260 P_("When the horizontal scrollbar is displayed"),
261 GTK_TYPE_POLICY_TYPE,
262 GTK_POLICY_AUTOMATIC,
263 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
264 g_object_class_install_property (gobject_class,
265 PROP_VSCROLLBAR_POLICY,
266 g_param_spec_enum ("vscrollbar-policy",
267 P_("Vertical Scrollbar Policy"),
268 P_("When the vertical scrollbar is displayed"),
269 GTK_TYPE_POLICY_TYPE,
270 GTK_POLICY_AUTOMATIC,
271 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
273 g_object_class_install_property (gobject_class,
274 PROP_WINDOW_PLACEMENT,
275 g_param_spec_enum ("window-placement",
276 P_("Window Placement"),
277 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
278 GTK_TYPE_CORNER_TYPE,
280 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
283 * GtkScrolledWindow:window-placement-set:
285 * Whether "window-placement" should be used to determine the location
286 * of the contents with respect to the scrollbars. Otherwise, the
287 * "gtk-scrolled-window-placement" setting is used.
291 g_object_class_install_property (gobject_class,
292 PROP_WINDOW_PLACEMENT_SET,
293 g_param_spec_boolean ("window-placement-set",
294 P_("Window Placement Set"),
295 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
297 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
298 g_object_class_install_property (gobject_class,
300 g_param_spec_enum ("shadow-type",
302 P_("Style of bevel around the contents"),
303 GTK_TYPE_SHADOW_TYPE,
305 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
308 * GtkScrolledWindow:scrollbars-within-bevel:
310 * Whether to place scrollbars within the scrolled window's bevel.
314 gtk_widget_class_install_style_property (widget_class,
315 g_param_spec_boolean ("scrollbars-within-bevel",
316 P_("Scrollbars within bevel"),
317 P_("Place scrollbars within the scrolled window's bevel"),
319 GTK_PARAM_READABLE));
321 gtk_widget_class_install_style_property (widget_class,
322 g_param_spec_int ("scrollbar-spacing",
323 P_("Scrollbar spacing"),
324 P_("Number of pixels between the scrollbars and the scrolled window"),
327 DEFAULT_SCROLLBAR_SPACING,
328 GTK_PARAM_READABLE));
331 * GtkSettings:gtk-scrolled-window-placement:
333 * Where the contents of scrolled windows are located with respect to the
334 * scrollbars, if not overridden by the scrolled window's own placement.
338 gtk_settings_install_property (g_param_spec_enum ("gtk-scrolled-window-placement",
339 P_("Scrolled Window Placement"),
340 P_("Where the contents of scrolled windows are located with respect to the scrollbars, if not overridden by the scrolled window's own placement."),
341 GTK_TYPE_CORNER_TYPE,
343 G_PARAM_READABLE | G_PARAM_WRITABLE));
346 signals[SCROLL_CHILD] =
347 g_signal_new (I_("scroll-child"),
348 G_TYPE_FROM_CLASS (object_class),
349 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
350 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
352 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
354 GTK_TYPE_SCROLL_TYPE,
356 signals[MOVE_FOCUS_OUT] =
357 g_signal_new (I_("move-focus-out"),
358 G_TYPE_FROM_CLASS (object_class),
359 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
360 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
362 _gtk_marshal_VOID__ENUM,
364 GTK_TYPE_DIRECTION_TYPE);
366 binding_set = gtk_binding_set_by_class (class);
368 add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
369 add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
370 add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
371 add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
373 add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
374 add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
375 add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
376 add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
378 add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
379 add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
380 add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
381 add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
383 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
384 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
386 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
390 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
392 GtkScrolledWindowPrivate *priv;
394 scrolled_window->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (scrolled_window,
395 GTK_TYPE_SCROLLED_WINDOW,
396 GtkScrolledWindowPrivate);
398 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
399 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
401 priv->hscrollbar = NULL;
402 priv->vscrollbar = NULL;
403 priv->hscrollbar_policy = GTK_POLICY_AUTOMATIC;
404 priv->vscrollbar_policy = GTK_POLICY_AUTOMATIC;
405 priv->hscrollbar_visible = FALSE;
406 priv->vscrollbar_visible = FALSE;
407 priv->focus_out = FALSE;
408 priv->window_placement = GTK_CORNER_TOP_LEFT;
409 gtk_scrolled_window_update_real_placement (scrolled_window);
413 * gtk_scrolled_window_new:
414 * @hadjustment: (allow-none): horizontal adjustment
415 * @vadjustment: (allow-none): vertical adjustment
417 * Creates a new scrolled window.
419 * The two arguments are the scrolled window's adjustments; these will be
420 * shared with the scrollbars and the child widget to keep the bars in sync
421 * with the child. Usually you want to pass %NULL for the adjustments, which
422 * will cause the scrolled window to create them for you.
424 * Returns: a new scrolled window
427 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
428 GtkAdjustment *vadjustment)
430 GtkWidget *scrolled_window;
433 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
436 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
438 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
439 "hadjustment", hadjustment,
440 "vadjustment", vadjustment,
443 return scrolled_window;
447 * gtk_scrolled_window_set_hadjustment:
448 * @scrolled_window: a #GtkScrolledWindow
449 * @hadjustment: horizontal scroll adjustment
451 * Sets the #GtkAdjustment for the horizontal scrollbar.
454 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
455 GtkAdjustment *hadjustment)
457 GtkScrolledWindowPrivate *priv;
461 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
463 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
465 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
467 bin = GTK_BIN (scrolled_window);
468 priv = scrolled_window->priv;
470 if (!priv->hscrollbar)
472 gtk_widget_push_composite_child ();
473 priv->hscrollbar = gtk_hscrollbar_new (hadjustment);
474 gtk_widget_set_composite_name (priv->hscrollbar, "hscrollbar");
475 gtk_widget_pop_composite_child ();
477 gtk_widget_set_parent (priv->hscrollbar, GTK_WIDGET (scrolled_window));
478 g_object_ref (priv->hscrollbar);
479 gtk_widget_show (priv->hscrollbar);
483 GtkAdjustment *old_adjustment;
485 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
486 if (old_adjustment == hadjustment)
489 g_signal_handlers_disconnect_by_func (old_adjustment,
490 gtk_scrolled_window_adjustment_changed,
492 gtk_range_set_adjustment (GTK_RANGE (priv->hscrollbar),
495 hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
496 g_signal_connect (hadjustment,
498 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
500 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
502 child = gtk_bin_get_child (bin);
504 gtk_widget_set_scroll_adjustments (child,
505 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
506 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)));
508 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
512 * gtk_scrolled_window_set_vadjustment:
513 * @scrolled_window: a #GtkScrolledWindow
514 * @vadjustment: vertical scroll adjustment
516 * Sets the #GtkAdjustment for the vertical scrollbar.
519 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
520 GtkAdjustment *vadjustment)
522 GtkScrolledWindowPrivate *priv;
526 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
528 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
530 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
532 bin = GTK_BIN (scrolled_window);
533 priv = scrolled_window->priv;
535 if (!priv->vscrollbar)
537 gtk_widget_push_composite_child ();
538 priv->vscrollbar = gtk_vscrollbar_new (vadjustment);
539 gtk_widget_set_composite_name (priv->vscrollbar, "vscrollbar");
540 gtk_widget_pop_composite_child ();
542 gtk_widget_set_parent (priv->vscrollbar, GTK_WIDGET (scrolled_window));
543 g_object_ref (priv->vscrollbar);
544 gtk_widget_show (priv->vscrollbar);
548 GtkAdjustment *old_adjustment;
550 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
551 if (old_adjustment == vadjustment)
554 g_signal_handlers_disconnect_by_func (old_adjustment,
555 gtk_scrolled_window_adjustment_changed,
557 gtk_range_set_adjustment (GTK_RANGE (priv->vscrollbar),
560 vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
561 g_signal_connect (vadjustment,
563 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
565 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
567 child = gtk_bin_get_child (bin);
569 gtk_widget_set_scroll_adjustments (child,
570 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
571 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)));
573 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
577 * gtk_scrolled_window_get_hadjustment:
578 * @scrolled_window: a #GtkScrolledWindow
580 * Returns the horizontal scrollbar's adjustment, used to connect the
581 * horizontal scrollbar to the child widget's horizontal scroll
584 * Returns: (transfer none): the horizontal #GtkAdjustment
587 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
589 GtkScrolledWindowPrivate *priv;
591 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
593 priv = scrolled_window->priv;
595 return (priv->hscrollbar ?
596 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)) :
601 * gtk_scrolled_window_get_vadjustment:
602 * @scrolled_window: a #GtkScrolledWindow
604 * Returns the vertical scrollbar's adjustment, used to connect the
605 * vertical scrollbar to the child widget's vertical scroll functionality.
607 * Returns: (transfer none): the vertical #GtkAdjustment
610 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
612 GtkScrolledWindowPrivate *priv;
614 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
616 priv = scrolled_window->priv;
618 return (priv->vscrollbar ?
619 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
624 * gtk_scrolled_window_get_hscrollbar:
625 * @scrolled_window: a #GtkScrolledWindow
627 * Returns the horizontal scrollbar of @scrolled_window.
629 * Returns: (transfer none): the horizontal scrollbar of the scrolled window,
630 * or %NULL if it does not have one.
635 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
637 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
639 return scrolled_window->priv->hscrollbar;
643 * gtk_scrolled_window_get_vscrollbar:
644 * @scrolled_window: a #GtkScrolledWindow
646 * Returns the vertical scrollbar of @scrolled_window.
648 * Returns: (transfer none): the vertical scrollbar of the scrolled window,
649 * or %NULL if it does not have one.
654 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
656 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
658 return scrolled_window->priv->vscrollbar;
662 * gtk_scrolled_window_set_policy:
663 * @scrolled_window: a #GtkScrolledWindow
664 * @hscrollbar_policy: policy for horizontal bar
665 * @vscrollbar_policy: policy for vertical bar
667 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
669 * The policy determines when the scrollbar should appear; it is a value
670 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
671 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
672 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
673 * if needed (that is, if the slider part of the bar would be smaller
674 * than the trough - the display is larger than the page size).
677 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
678 GtkPolicyType hscrollbar_policy,
679 GtkPolicyType vscrollbar_policy)
681 GtkScrolledWindowPrivate *priv;
682 GObject *object = G_OBJECT (scrolled_window);
684 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
686 priv = scrolled_window->priv;
688 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
689 (priv->vscrollbar_policy != vscrollbar_policy))
691 priv->hscrollbar_policy = hscrollbar_policy;
692 priv->vscrollbar_policy = vscrollbar_policy;
694 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
696 g_object_freeze_notify (object);
697 g_object_notify (object, "hscrollbar-policy");
698 g_object_notify (object, "vscrollbar-policy");
699 g_object_thaw_notify (object);
704 * gtk_scrolled_window_get_policy:
705 * @scrolled_window: a #GtkScrolledWindow
706 * @hscrollbar_policy: location to store the policy for the horizontal
707 * scrollbar, or %NULL.
708 * @vscrollbar_policy: location to store the policy for the vertical
709 * scrollbar, or %NULL.
711 * Retrieves the current policy values for the horizontal and vertical
712 * scrollbars. See gtk_scrolled_window_set_policy().
715 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
716 GtkPolicyType *hscrollbar_policy,
717 GtkPolicyType *vscrollbar_policy)
719 GtkScrolledWindowPrivate *priv;
721 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
723 priv = scrolled_window->priv;
725 if (hscrollbar_policy)
726 *hscrollbar_policy = priv->hscrollbar_policy;
727 if (vscrollbar_policy)
728 *vscrollbar_policy = priv->vscrollbar_policy;
732 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
734 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
735 GtkSettings *settings;
737 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
739 if (priv->window_placement_set || settings == NULL)
740 priv->real_window_placement = priv->window_placement;
742 g_object_get (settings,
743 "gtk-scrolled-window-placement",
744 &priv->real_window_placement,
749 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
750 GtkCornerType window_placement)
752 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
754 if (priv->window_placement != window_placement)
756 priv->window_placement = window_placement;
758 gtk_scrolled_window_update_real_placement (scrolled_window);
759 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
761 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
766 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
767 gboolean placement_set,
768 gboolean emit_resize)
770 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
772 if (priv->window_placement_set != placement_set)
774 priv->window_placement_set = placement_set;
776 gtk_scrolled_window_update_real_placement (scrolled_window);
778 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
780 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
785 * gtk_scrolled_window_set_placement:
786 * @scrolled_window: a #GtkScrolledWindow
787 * @window_placement: position of the child window
789 * Sets the placement of the contents with respect to the scrollbars
790 * for the scrolled window.
792 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
793 * in the top left, with the scrollbars underneath and to the right.
794 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
795 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
797 * See also gtk_scrolled_window_get_placement() and
798 * gtk_scrolled_window_unset_placement().
801 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
802 GtkCornerType window_placement)
804 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
806 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
807 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
811 * gtk_scrolled_window_get_placement:
812 * @scrolled_window: a #GtkScrolledWindow
814 * Gets the placement of the contents with respect to the scrollbars
815 * for the scrolled window. See gtk_scrolled_window_set_placement().
817 * Return value: the current placement value.
819 * See also gtk_scrolled_window_set_placement() and
820 * gtk_scrolled_window_unset_placement().
823 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
825 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
827 return scrolled_window->priv->window_placement;
831 * gtk_scrolled_window_unset_placement:
832 * @scrolled_window: a #GtkScrolledWindow
834 * Unsets the placement of the contents with respect to the scrollbars
835 * for the scrolled window. If no window placement is set for a scrolled
836 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
838 * See also gtk_scrolled_window_set_placement() and
839 * gtk_scrolled_window_get_placement().
844 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
846 GtkScrolledWindowPrivate *priv;
848 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
850 priv = scrolled_window->priv;
852 if (priv->window_placement_set)
854 priv->window_placement_set = FALSE;
856 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
858 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
863 * gtk_scrolled_window_set_shadow_type:
864 * @scrolled_window: a #GtkScrolledWindow
865 * @type: kind of shadow to draw around scrolled window contents
867 * Changes the type of shadow drawn around the contents of
872 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
875 GtkScrolledWindowPrivate *priv;
877 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
878 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
880 priv = scrolled_window->priv;
882 if (priv->shadow_type != type)
884 priv->shadow_type = type;
886 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
887 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
889 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
891 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
896 * gtk_scrolled_window_get_shadow_type:
897 * @scrolled_window: a #GtkScrolledWindow
899 * Gets the shadow type of the scrolled window. See
900 * gtk_scrolled_window_set_shadow_type().
902 * Return value: the current shadow type
905 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
907 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
909 return scrolled_window->priv->shadow_type;
913 gtk_scrolled_window_destroy (GtkObject *object)
915 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
916 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
918 if (priv->hscrollbar)
920 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
921 gtk_scrolled_window_adjustment_changed,
923 gtk_widget_unparent (priv->hscrollbar);
924 gtk_widget_destroy (priv->hscrollbar);
925 g_object_unref (priv->hscrollbar);
926 priv->hscrollbar = NULL;
928 if (priv->vscrollbar)
930 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
931 gtk_scrolled_window_adjustment_changed,
933 gtk_widget_unparent (priv->vscrollbar);
934 gtk_widget_destroy (priv->vscrollbar);
935 g_object_unref (priv->vscrollbar);
936 priv->vscrollbar = NULL;
939 GTK_OBJECT_CLASS (gtk_scrolled_window_parent_class)->destroy (object);
943 gtk_scrolled_window_set_property (GObject *object,
948 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
949 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
953 case PROP_HADJUSTMENT:
954 gtk_scrolled_window_set_hadjustment (scrolled_window,
955 g_value_get_object (value));
957 case PROP_VADJUSTMENT:
958 gtk_scrolled_window_set_vadjustment (scrolled_window,
959 g_value_get_object (value));
961 case PROP_HSCROLLBAR_POLICY:
962 gtk_scrolled_window_set_policy (scrolled_window,
963 g_value_get_enum (value),
964 priv->vscrollbar_policy);
966 case PROP_VSCROLLBAR_POLICY:
967 gtk_scrolled_window_set_policy (scrolled_window,
968 priv->hscrollbar_policy,
969 g_value_get_enum (value));
971 case PROP_WINDOW_PLACEMENT:
972 gtk_scrolled_window_set_placement_internal (scrolled_window,
973 g_value_get_enum (value));
975 case PROP_WINDOW_PLACEMENT_SET:
976 gtk_scrolled_window_set_placement_set (scrolled_window,
977 g_value_get_boolean (value),
980 case PROP_SHADOW_TYPE:
981 gtk_scrolled_window_set_shadow_type (scrolled_window,
982 g_value_get_enum (value));
985 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
991 gtk_scrolled_window_get_property (GObject *object,
996 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
997 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1001 case PROP_HADJUSTMENT:
1002 g_value_set_object (value,
1003 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1005 case PROP_VADJUSTMENT:
1006 g_value_set_object (value,
1007 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1009 case PROP_HSCROLLBAR_POLICY:
1010 g_value_set_enum (value, priv->hscrollbar_policy);
1012 case PROP_VSCROLLBAR_POLICY:
1013 g_value_set_enum (value, priv->vscrollbar_policy);
1015 case PROP_WINDOW_PLACEMENT:
1016 g_value_set_enum (value, priv->window_placement);
1018 case PROP_WINDOW_PLACEMENT_SET:
1019 g_value_set_boolean (value, priv->window_placement_set);
1021 case PROP_SHADOW_TYPE:
1022 g_value_set_enum (value, priv->shadow_type);
1025 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1031 traverse_container (GtkWidget *widget,
1034 if (GTK_IS_SCROLLED_WINDOW (widget))
1036 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1037 gtk_widget_queue_resize (widget);
1039 else if (GTK_IS_CONTAINER (widget))
1040 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1044 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1048 list = gtk_window_list_toplevels ();
1050 for (l = list; l; l = l->next)
1051 gtk_container_forall (GTK_CONTAINER (l->data),
1052 traverse_container, NULL);
1058 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1059 GdkScreen *previous_screen)
1061 GtkSettings *settings;
1062 guint window_placement_connection;
1064 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1066 if (!gtk_widget_has_screen (widget))
1069 settings = gtk_widget_get_settings (widget);
1071 window_placement_connection =
1072 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1073 "gtk-scrolled-window-connection"));
1075 if (window_placement_connection)
1078 window_placement_connection =
1079 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1080 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1081 g_object_set_data (G_OBJECT (settings),
1082 I_("gtk-scrolled-window-connection"),
1083 GUINT_TO_POINTER (window_placement_connection));
1087 gtk_scrolled_window_draw (GtkWidget *widget,
1090 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1091 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1093 if (priv->shadow_type != GTK_SHADOW_NONE)
1095 GtkAllocation relative_allocation;
1097 gboolean scrollbars_within_bevel;
1099 style = gtk_widget_get_style (widget);
1100 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1102 if (!scrollbars_within_bevel)
1104 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1106 relative_allocation.x -= style->xthickness;
1107 relative_allocation.y -= style->ythickness;
1108 relative_allocation.width += 2 * style->xthickness;
1109 relative_allocation.height += 2 * style->ythickness;
1113 GtkContainer *container = GTK_CONTAINER (widget);
1116 border_width = gtk_container_get_border_width (container);
1118 relative_allocation.x = border_width;
1119 relative_allocation.y = border_width;
1120 relative_allocation.width = gtk_widget_get_allocated_width (widget) - 2 * border_width;
1121 relative_allocation.height = gtk_widget_get_allocated_height (widget) - 2 * border_width;
1124 gtk_paint_shadow (style,
1126 GTK_STATE_NORMAL, priv->shadow_type,
1127 widget, "scrolled_window",
1128 relative_allocation.x,
1129 relative_allocation.y,
1130 relative_allocation.width,
1131 relative_allocation.height);
1134 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->draw (widget, cr);
1140 gtk_scrolled_window_forall (GtkContainer *container,
1141 gboolean include_internals,
1142 GtkCallback callback,
1143 gpointer callback_data)
1145 GtkScrolledWindowPrivate *priv;
1146 GtkScrolledWindow *scrolled_window;
1148 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1149 g_return_if_fail (callback != NULL);
1151 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1155 if (include_internals)
1157 scrolled_window = GTK_SCROLLED_WINDOW (container);
1158 priv = scrolled_window->priv;
1160 if (priv->vscrollbar)
1161 callback (priv->vscrollbar, callback_data);
1162 if (priv->hscrollbar)
1163 callback (priv->hscrollbar, callback_data);
1168 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1169 GtkScrollType scroll,
1170 gboolean horizontal)
1172 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1173 GtkAdjustment *adjustment = NULL;
1177 case GTK_SCROLL_STEP_UP:
1178 scroll = GTK_SCROLL_STEP_BACKWARD;
1181 case GTK_SCROLL_STEP_DOWN:
1182 scroll = GTK_SCROLL_STEP_FORWARD;
1185 case GTK_SCROLL_STEP_LEFT:
1186 scroll = GTK_SCROLL_STEP_BACKWARD;
1189 case GTK_SCROLL_STEP_RIGHT:
1190 scroll = GTK_SCROLL_STEP_FORWARD;
1193 case GTK_SCROLL_PAGE_UP:
1194 scroll = GTK_SCROLL_PAGE_BACKWARD;
1197 case GTK_SCROLL_PAGE_DOWN:
1198 scroll = GTK_SCROLL_PAGE_FORWARD;
1201 case GTK_SCROLL_PAGE_LEFT:
1202 scroll = GTK_SCROLL_STEP_BACKWARD;
1205 case GTK_SCROLL_PAGE_RIGHT:
1206 scroll = GTK_SCROLL_STEP_FORWARD;
1209 case GTK_SCROLL_STEP_BACKWARD:
1210 case GTK_SCROLL_STEP_FORWARD:
1211 case GTK_SCROLL_PAGE_BACKWARD:
1212 case GTK_SCROLL_PAGE_FORWARD:
1213 case GTK_SCROLL_START:
1214 case GTK_SCROLL_END:
1217 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1221 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1222 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1227 if (priv->hscrollbar)
1228 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1232 if (priv->vscrollbar)
1233 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1238 gdouble value = adjustment->value;
1242 case GTK_SCROLL_STEP_FORWARD:
1243 value += adjustment->step_increment;
1245 case GTK_SCROLL_STEP_BACKWARD:
1246 value -= adjustment->step_increment;
1248 case GTK_SCROLL_PAGE_FORWARD:
1249 value += adjustment->page_increment;
1251 case GTK_SCROLL_PAGE_BACKWARD:
1252 value -= adjustment->page_increment;
1254 case GTK_SCROLL_START:
1255 value = adjustment->lower;
1257 case GTK_SCROLL_END:
1258 value = adjustment->upper;
1261 g_assert_not_reached ();
1265 gtk_adjustment_set_value (adjustment, value);
1274 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1275 GtkDirectionType direction_type)
1277 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1278 GtkWidget *toplevel;
1280 /* Focus out of the scrolled window entirely. We do this by setting
1281 * a flag, then propagating the focus motion to the notebook.
1283 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1284 if (!gtk_widget_is_toplevel (toplevel))
1287 g_object_ref (scrolled_window);
1289 priv->focus_out = TRUE;
1290 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1291 priv->focus_out = FALSE;
1293 g_object_unref (scrolled_window);
1297 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1298 GtkAllocation *allocation)
1300 GtkAllocation widget_allocation;
1301 GtkScrolledWindow *scrolled_window;
1302 GtkScrolledWindowPrivate *priv;
1304 gint scrollbar_spacing;
1307 g_return_if_fail (widget != NULL);
1308 g_return_if_fail (allocation != NULL);
1310 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1311 priv = scrolled_window->priv;
1313 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1315 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1316 allocation->x = border_width;
1317 allocation->y = border_width;
1319 if (priv->shadow_type != GTK_SHADOW_NONE)
1321 style = gtk_widget_get_style (widget);
1322 allocation->x += style->xthickness;
1323 allocation->y += style->ythickness;
1326 gtk_widget_get_allocation (widget, &widget_allocation);
1327 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1328 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1330 if (priv->vscrollbar_visible)
1332 GtkRequisition vscrollbar_requisition;
1335 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->vscrollbar),
1336 &vscrollbar_requisition, NULL);
1337 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1340 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1341 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1343 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1344 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1345 allocation->x += (vscrollbar_requisition.width + scrollbar_spacing);
1347 allocation->width = MAX (1, allocation->width - (vscrollbar_requisition.width + scrollbar_spacing));
1349 if (priv->hscrollbar_visible)
1351 GtkRequisition hscrollbar_requisition;
1353 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->hscrollbar),
1354 &hscrollbar_requisition, NULL);
1356 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1357 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1358 allocation->y += (hscrollbar_requisition.height + scrollbar_spacing);
1360 allocation->height = MAX (1, allocation->height - (hscrollbar_requisition.height + scrollbar_spacing));
1365 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1366 GtkAllocation *allocation)
1368 GtkScrolledWindow *scrolled_window;
1369 GtkScrolledWindowPrivate *priv;
1372 GtkAllocation relative_allocation;
1373 GtkAllocation child_allocation;
1375 gboolean scrollbars_within_bevel;
1376 gint scrollbar_spacing;
1379 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1380 g_return_if_fail (allocation != NULL);
1382 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1383 bin = GTK_BIN (scrolled_window);
1384 priv = scrolled_window->priv;
1386 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1388 style = gtk_widget_get_style (widget);
1389 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1391 border_width = gtk_container_get_border_width (GTK_CONTAINER (scrolled_window));
1393 gtk_widget_set_allocation (widget, allocation);
1395 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1396 priv->hscrollbar_visible = TRUE;
1397 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1398 priv->hscrollbar_visible = FALSE;
1399 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1400 priv->vscrollbar_visible = TRUE;
1401 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1402 priv->vscrollbar_visible = FALSE;
1404 child = gtk_bin_get_child (bin);
1405 if (child && gtk_widget_get_visible (child))
1407 gboolean previous_hvis;
1408 gboolean previous_vvis;
1413 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1415 child_allocation.x = relative_allocation.x + allocation->x;
1416 child_allocation.y = relative_allocation.y + allocation->y;
1417 child_allocation.width = relative_allocation.width;
1418 child_allocation.height = relative_allocation.height;
1420 previous_hvis = priv->hscrollbar_visible;
1421 previous_vvis = priv->vscrollbar_visible;
1423 gtk_widget_size_allocate (child, &child_allocation);
1425 /* If, after the first iteration, the hscrollbar and the
1426 * vscrollbar flip visiblity, then we need both.
1429 previous_hvis != priv->hscrollbar_visible &&
1430 previous_vvis != priv->vscrollbar_visible)
1432 priv->hscrollbar_visible = TRUE;
1433 priv->vscrollbar_visible = TRUE;
1435 /* a new resize is already queued at this point,
1436 * so we will immediatedly get reinvoked
1443 while (previous_hvis != priv->hscrollbar_visible ||
1444 previous_vvis != priv->vscrollbar_visible);
1448 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1449 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1450 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1453 if (priv->hscrollbar_visible)
1455 GtkRequisition hscrollbar_requisition;
1457 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->hscrollbar),
1458 &hscrollbar_requisition, NULL);
1460 if (!gtk_widget_get_visible (priv->hscrollbar))
1461 gtk_widget_show (priv->hscrollbar);
1463 child_allocation.x = relative_allocation.x;
1464 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1465 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1466 child_allocation.y = (relative_allocation.y +
1467 relative_allocation.height +
1469 (priv->shadow_type == GTK_SHADOW_NONE ?
1470 0 : style->ythickness));
1472 child_allocation.y = border_width;
1474 child_allocation.width = relative_allocation.width;
1475 child_allocation.height = hscrollbar_requisition.height;
1476 child_allocation.x += allocation->x;
1477 child_allocation.y += allocation->y;
1479 if (priv->shadow_type != GTK_SHADOW_NONE)
1481 if (!scrollbars_within_bevel)
1483 child_allocation.x -= style->xthickness;
1484 child_allocation.width += 2 * style->xthickness;
1486 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1487 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1489 child_allocation.y -= style->ythickness;
1493 child_allocation.y += style->ythickness;
1497 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1499 else if (gtk_widget_get_visible (priv->hscrollbar))
1500 gtk_widget_hide (priv->hscrollbar);
1502 if (priv->vscrollbar_visible)
1504 GtkRequisition vscrollbar_requisition;
1505 if (!gtk_widget_get_visible (priv->vscrollbar))
1506 gtk_widget_show (priv->vscrollbar);
1508 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->vscrollbar),
1509 &vscrollbar_requisition, NULL);
1511 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1512 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1513 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1514 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1515 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1516 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1517 child_allocation.x = (relative_allocation.x +
1518 relative_allocation.width +
1520 (priv->shadow_type == GTK_SHADOW_NONE ?
1521 0 : style->xthickness));
1523 child_allocation.x = border_width;
1525 child_allocation.y = relative_allocation.y;
1526 child_allocation.width = vscrollbar_requisition.width;
1527 child_allocation.height = relative_allocation.height;
1528 child_allocation.x += allocation->x;
1529 child_allocation.y += allocation->y;
1531 if (priv->shadow_type != GTK_SHADOW_NONE)
1533 if (!scrollbars_within_bevel)
1535 child_allocation.y -= style->ythickness;
1536 child_allocation.height += 2 * style->ythickness;
1538 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1539 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1541 child_allocation.x -= style->xthickness;
1545 child_allocation.x += style->xthickness;
1549 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1551 else if (gtk_widget_get_visible (priv->vscrollbar))
1552 gtk_widget_hide (priv->vscrollbar);
1556 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1557 GdkEventScroll *event)
1559 GtkScrolledWindowPrivate *priv;
1560 GtkScrolledWindow *scrolled_window;
1563 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1564 g_return_val_if_fail (event != NULL, FALSE);
1566 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1567 priv = scrolled_window->priv;
1569 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1570 range = priv->vscrollbar;
1572 range = priv->hscrollbar;
1574 if (range && gtk_widget_get_visible (range))
1576 GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE (range));
1579 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1581 gtk_adjustment_set_value (adj, adj->value + delta);
1590 gtk_scrolled_window_focus (GtkWidget *widget,
1591 GtkDirectionType direction)
1593 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1594 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1596 gboolean had_focus_child;
1598 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1600 if (priv->focus_out)
1602 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1606 if (gtk_widget_is_focus (widget))
1609 /* We only put the scrolled window itself in the focus chain if it
1610 * isn't possible to focus any children.
1612 child = gtk_bin_get_child (GTK_BIN (widget));
1615 if (gtk_widget_child_focus (child, direction))
1619 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1621 gtk_widget_grab_focus (widget);
1629 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1632 GtkScrolledWindowPrivate *priv;;
1633 GtkScrolledWindow *scrolled_window;
1635 g_return_if_fail (adjustment != NULL);
1636 g_return_if_fail (data != NULL);
1638 scrolled_window = GTK_SCROLLED_WINDOW (data);
1639 priv = scrolled_window->priv;
1641 if (priv->hscrollbar &&
1642 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1644 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1648 visible = priv->hscrollbar_visible;
1649 priv->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1650 adjustment->page_size);
1651 if (priv->hscrollbar_visible != visible)
1652 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1655 else if (priv->vscrollbar &&
1656 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1658 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1662 visible = priv->vscrollbar_visible;
1663 priv->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1664 adjustment->page_size);
1665 if (priv->vscrollbar_visible != visible)
1666 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1672 gtk_scrolled_window_add (GtkContainer *container,
1675 GtkScrolledWindowPrivate *priv;
1676 GtkScrolledWindow *scrolled_window;
1678 GtkWidget *child_widget;
1680 bin = GTK_BIN (container);
1681 child_widget = gtk_bin_get_child (bin);
1682 g_return_if_fail (child_widget == NULL);
1684 scrolled_window = GTK_SCROLLED_WINDOW (container);
1685 priv = scrolled_window->priv;
1687 _gtk_bin_set_child (bin, child);
1688 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1690 /* this is a temporary message */
1691 if (!gtk_widget_set_scroll_adjustments (child,
1692 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1693 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar))))
1694 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1695 "use gtk_scrolled_window_add_with_viewport() instead");
1699 gtk_scrolled_window_remove (GtkContainer *container,
1702 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1703 g_return_if_fail (child != NULL);
1704 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1706 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1708 /* chain parent class handler to remove child */
1709 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1713 * gtk_scrolled_window_add_with_viewport:
1714 * @scrolled_window: a #GtkScrolledWindow
1715 * @child: the widget you want to scroll
1717 * Used to add children without native scrolling capabilities. This
1718 * is simply a convenience function; it is equivalent to adding the
1719 * unscrollable child to a viewport, then adding the viewport to the
1720 * scrolled window. If a child has native scrolling, use
1721 * gtk_container_add() instead of this function.
1723 * The viewport scrolls the child by moving its #GdkWindow, and takes
1724 * the size of the child to be the size of its toplevel #GdkWindow.
1725 * This will be very wrong for most widgets that support native scrolling;
1726 * for example, if you add a widget such as #GtkTreeView with a viewport,
1727 * the whole widget will scroll, including the column headings. Thus,
1728 * widgets with native scrolling support should not be used with the
1729 * #GtkViewport proxy.
1731 * A widget supports scrolling natively if the
1732 * set_scroll_adjustments_signal field in #GtkWidgetClass is non-zero,
1733 * i.e. has been filled in with a valid signal identifier.
1736 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1740 GtkWidget *viewport;
1741 GtkWidget *child_widget;
1743 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1744 g_return_if_fail (GTK_IS_WIDGET (child));
1745 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1747 bin = GTK_BIN (scrolled_window);
1748 child_widget = gtk_bin_get_child (bin);
1752 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
1753 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
1755 viewport = child_widget;
1760 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1761 gtk_scrolled_window_get_vadjustment (scrolled_window));
1762 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1765 gtk_widget_show (viewport);
1766 gtk_container_add (GTK_CONTAINER (viewport), child);
1770 * _gtk_scrolled_window_get_spacing:
1771 * @scrolled_window: a scrolled window
1773 * Gets the spacing between the scrolled window's scrollbars and
1774 * the scrolled widget. Used by GtkCombo
1776 * Return value: the spacing, in pixels.
1779 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
1781 GtkScrolledWindowClass *class;
1783 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
1785 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
1787 if (class->scrollbar_spacing >= 0)
1788 return class->scrollbar_spacing;
1791 gint scrollbar_spacing;
1793 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
1794 "scrollbar-spacing", &scrollbar_spacing,
1797 return scrollbar_spacing;
1803 gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface)
1805 iface->get_width = gtk_scrolled_window_get_width;
1806 iface->get_height = gtk_scrolled_window_get_height;
1807 iface->get_height_for_width = gtk_scrolled_window_get_height_for_width;
1808 iface->get_width_for_height = gtk_scrolled_window_get_width_for_height;
1812 gtk_scrolled_window_get_size (GtkSizeRequest *widget,
1813 GtkOrientation orientation,
1817 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1818 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1819 GtkBin *bin = GTK_BIN (scrolled_window);
1822 gint scrollbar_spacing;
1823 GtkRequisition hscrollbar_requisition;
1824 GtkRequisition vscrollbar_requisition;
1825 GtkRequisition minimum_req, natural_req;
1828 gint min_child_size, nat_child_size;
1831 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1835 minimum_req.width = 0;
1836 minimum_req.height = 0;
1837 natural_req.width = 0;
1838 natural_req.height = 0;
1840 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->hscrollbar),
1841 &hscrollbar_requisition, NULL);
1842 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->vscrollbar),
1843 &vscrollbar_requisition, NULL);
1845 child = gtk_bin_get_child (bin);
1846 if (child && gtk_widget_get_visible (child))
1848 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1850 gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
1854 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1856 minimum_req.width += min_child_size;
1857 natural_req.width += nat_child_size;
1861 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1863 if (aux_info && aux_info->width > 0)
1865 minimum_req.width += aux_info->width;
1866 natural_req.width += aux_info->width;
1871 minimum_req.width += vscrollbar_requisition.width;
1872 natural_req.width += vscrollbar_requisition.width;
1876 else /* GTK_ORIENTATION_VERTICAL */
1878 gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
1882 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1884 minimum_req.height += min_child_size;
1885 natural_req.height += nat_child_size;
1889 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1891 if (aux_info && aux_info->height > 0)
1893 minimum_req.height += aux_info->height;
1894 natural_req.height += aux_info->height;
1899 minimum_req.height += hscrollbar_requisition.height;
1900 natural_req.height += hscrollbar_requisition.height;
1906 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1907 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1909 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
1910 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
1911 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1912 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
1915 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1916 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1918 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
1919 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
1920 if (!extra_height || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1921 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
1924 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1925 minimum_req.width += border_width * 2 + MAX (0, extra_width);
1926 minimum_req.height += border_width * 2 + MAX (0, extra_height);
1927 natural_req.width += border_width * 2 + MAX (0, extra_width);
1928 natural_req.height += border_width * 2 + MAX (0, extra_height);
1930 if (priv->shadow_type != GTK_SHADOW_NONE)
1932 style = gtk_widget_get_style (GTK_WIDGET (widget));
1933 minimum_req.width += 2 * style->xthickness;
1934 minimum_req.height += 2 * style->ythickness;
1935 natural_req.width += 2 * style->xthickness;
1936 natural_req.height += 2 * style->ythickness;
1939 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1942 *minimum_size = minimum_req.width;
1944 *natural_size = natural_req.width;
1949 *minimum_size = minimum_req.height;
1951 *natural_size = natural_req.height;
1956 gtk_scrolled_window_get_width (GtkSizeRequest *widget,
1960 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1964 gtk_scrolled_window_get_height (GtkSizeRequest *widget,
1968 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1972 gtk_scrolled_window_get_height_for_width (GtkSizeRequest *widget,
1974 gint *minimum_height,
1975 gint *natural_height)
1977 g_return_if_fail (GTK_IS_WIDGET (widget));
1979 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
1983 gtk_scrolled_window_get_width_for_height (GtkSizeRequest *widget,
1985 gint *minimum_width,
1986 gint *natural_width)
1988 g_return_if_fail (GTK_IS_WIDGET (widget));
1990 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);