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_expose (GtkWidget *widget,
126 GdkEventExpose *event);
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->expose_event = gtk_scrolled_window_expose;
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,
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,
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_ALWAYS;
404 priv->vscrollbar_policy = GTK_POLICY_ALWAYS;
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_paint (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 allocation;
1096 GtkAllocation relative_allocation;
1098 gboolean scrollbars_within_bevel;
1100 style = gtk_widget_get_style (widget);
1101 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1103 gtk_widget_get_allocation (widget, &allocation);
1105 if (!scrollbars_within_bevel)
1107 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1109 relative_allocation.x -= style->xthickness;
1110 relative_allocation.y -= style->ythickness;
1111 relative_allocation.width += 2 * style->xthickness;
1112 relative_allocation.height += 2 * style->ythickness;
1116 GtkContainer *container = GTK_CONTAINER (widget);
1119 border_width = gtk_container_get_border_width (container);
1121 relative_allocation.x = border_width;
1122 relative_allocation.y = border_width;
1123 relative_allocation.width = allocation.width - 2 * border_width;
1124 relative_allocation.height = allocation.height - 2 * border_width;
1127 gtk_paint_shadow (style,
1128 gtk_widget_get_window (widget),
1129 GTK_STATE_NORMAL, priv->shadow_type,
1130 area, widget, "scrolled_window",
1131 allocation.x + relative_allocation.x,
1132 allocation.y + relative_allocation.y,
1133 relative_allocation.width,
1134 relative_allocation.height);
1139 gtk_scrolled_window_expose (GtkWidget *widget,
1140 GdkEventExpose *event)
1142 if (gtk_widget_is_drawable (widget))
1144 gtk_scrolled_window_paint (widget, &event->area);
1146 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->expose_event (widget, event);
1153 gtk_scrolled_window_forall (GtkContainer *container,
1154 gboolean include_internals,
1155 GtkCallback callback,
1156 gpointer callback_data)
1158 GtkScrolledWindowPrivate *priv;
1159 GtkScrolledWindow *scrolled_window;
1161 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1162 g_return_if_fail (callback != NULL);
1164 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1168 if (include_internals)
1170 scrolled_window = GTK_SCROLLED_WINDOW (container);
1171 priv = scrolled_window->priv;
1173 if (priv->vscrollbar)
1174 callback (priv->vscrollbar, callback_data);
1175 if (priv->hscrollbar)
1176 callback (priv->hscrollbar, callback_data);
1181 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1182 GtkScrollType scroll,
1183 gboolean horizontal)
1185 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1186 GtkAdjustment *adjustment = NULL;
1190 case GTK_SCROLL_STEP_UP:
1191 scroll = GTK_SCROLL_STEP_BACKWARD;
1194 case GTK_SCROLL_STEP_DOWN:
1195 scroll = GTK_SCROLL_STEP_FORWARD;
1198 case GTK_SCROLL_STEP_LEFT:
1199 scroll = GTK_SCROLL_STEP_BACKWARD;
1202 case GTK_SCROLL_STEP_RIGHT:
1203 scroll = GTK_SCROLL_STEP_FORWARD;
1206 case GTK_SCROLL_PAGE_UP:
1207 scroll = GTK_SCROLL_PAGE_BACKWARD;
1210 case GTK_SCROLL_PAGE_DOWN:
1211 scroll = GTK_SCROLL_PAGE_FORWARD;
1214 case GTK_SCROLL_PAGE_LEFT:
1215 scroll = GTK_SCROLL_STEP_BACKWARD;
1218 case GTK_SCROLL_PAGE_RIGHT:
1219 scroll = GTK_SCROLL_STEP_FORWARD;
1222 case GTK_SCROLL_STEP_BACKWARD:
1223 case GTK_SCROLL_STEP_FORWARD:
1224 case GTK_SCROLL_PAGE_BACKWARD:
1225 case GTK_SCROLL_PAGE_FORWARD:
1226 case GTK_SCROLL_START:
1227 case GTK_SCROLL_END:
1230 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1234 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1235 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1240 if (priv->hscrollbar)
1241 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1245 if (priv->vscrollbar)
1246 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1251 gdouble value = adjustment->value;
1255 case GTK_SCROLL_STEP_FORWARD:
1256 value += adjustment->step_increment;
1258 case GTK_SCROLL_STEP_BACKWARD:
1259 value -= adjustment->step_increment;
1261 case GTK_SCROLL_PAGE_FORWARD:
1262 value += adjustment->page_increment;
1264 case GTK_SCROLL_PAGE_BACKWARD:
1265 value -= adjustment->page_increment;
1267 case GTK_SCROLL_START:
1268 value = adjustment->lower;
1270 case GTK_SCROLL_END:
1271 value = adjustment->upper;
1274 g_assert_not_reached ();
1278 gtk_adjustment_set_value (adjustment, value);
1287 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1288 GtkDirectionType direction_type)
1290 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1291 GtkWidget *toplevel;
1293 /* Focus out of the scrolled window entirely. We do this by setting
1294 * a flag, then propagating the focus motion to the notebook.
1296 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1297 if (!gtk_widget_is_toplevel (toplevel))
1300 g_object_ref (scrolled_window);
1302 priv->focus_out = TRUE;
1303 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1304 priv->focus_out = FALSE;
1306 g_object_unref (scrolled_window);
1310 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1311 GtkAllocation *allocation)
1313 GtkAllocation widget_allocation;
1314 GtkScrolledWindow *scrolled_window;
1315 GtkScrolledWindowPrivate *priv;
1317 gint scrollbar_spacing;
1320 g_return_if_fail (widget != NULL);
1321 g_return_if_fail (allocation != NULL);
1323 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1324 priv = scrolled_window->priv;
1326 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1328 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1329 allocation->x = border_width;
1330 allocation->y = border_width;
1332 if (priv->shadow_type != GTK_SHADOW_NONE)
1334 style = gtk_widget_get_style (widget);
1335 allocation->x += style->xthickness;
1336 allocation->y += style->ythickness;
1339 gtk_widget_get_allocation (widget, &widget_allocation);
1340 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1341 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1343 if (priv->vscrollbar_visible)
1345 GtkRequisition vscrollbar_requisition;
1348 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->vscrollbar),
1349 &vscrollbar_requisition, NULL);
1350 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1353 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1354 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1356 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1357 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1358 allocation->x += (vscrollbar_requisition.width + scrollbar_spacing);
1360 allocation->width = MAX (1, allocation->width - (vscrollbar_requisition.width + scrollbar_spacing));
1362 if (priv->hscrollbar_visible)
1364 GtkRequisition hscrollbar_requisition;
1366 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->hscrollbar),
1367 &hscrollbar_requisition, NULL);
1369 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1370 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1371 allocation->y += (hscrollbar_requisition.height + scrollbar_spacing);
1373 allocation->height = MAX (1, allocation->height - (hscrollbar_requisition.height + scrollbar_spacing));
1378 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1379 GtkAllocation *allocation)
1381 GtkScrolledWindow *scrolled_window;
1382 GtkScrolledWindowPrivate *priv;
1385 GtkAllocation relative_allocation;
1386 GtkAllocation child_allocation;
1388 gboolean scrollbars_within_bevel;
1389 gint scrollbar_spacing;
1392 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1393 g_return_if_fail (allocation != NULL);
1395 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1396 bin = GTK_BIN (scrolled_window);
1397 priv = scrolled_window->priv;
1399 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1401 style = gtk_widget_get_style (widget);
1402 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1404 border_width = gtk_container_get_border_width (GTK_CONTAINER (scrolled_window));
1406 gtk_widget_set_allocation (widget, allocation);
1408 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1409 priv->hscrollbar_visible = TRUE;
1410 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1411 priv->hscrollbar_visible = FALSE;
1412 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1413 priv->vscrollbar_visible = TRUE;
1414 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1415 priv->vscrollbar_visible = FALSE;
1417 child = gtk_bin_get_child (bin);
1418 if (child && gtk_widget_get_visible (child))
1420 gboolean previous_hvis;
1421 gboolean previous_vvis;
1426 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1428 child_allocation.x = relative_allocation.x + allocation->x;
1429 child_allocation.y = relative_allocation.y + allocation->y;
1430 child_allocation.width = relative_allocation.width;
1431 child_allocation.height = relative_allocation.height;
1433 previous_hvis = priv->hscrollbar_visible;
1434 previous_vvis = priv->vscrollbar_visible;
1436 gtk_widget_size_allocate (child, &child_allocation);
1438 /* If, after the first iteration, the hscrollbar and the
1439 * vscrollbar flip visiblity, then we need both.
1442 previous_hvis != priv->hscrollbar_visible &&
1443 previous_vvis != priv->vscrollbar_visible)
1445 priv->hscrollbar_visible = TRUE;
1446 priv->vscrollbar_visible = TRUE;
1448 /* a new resize is already queued at this point,
1449 * so we will immediatedly get reinvoked
1456 while (previous_hvis != priv->hscrollbar_visible ||
1457 previous_vvis != priv->vscrollbar_visible);
1461 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1462 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1463 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1466 if (priv->hscrollbar_visible)
1468 GtkRequisition hscrollbar_requisition;
1470 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->hscrollbar),
1471 &hscrollbar_requisition, NULL);
1473 if (!gtk_widget_get_visible (priv->hscrollbar))
1474 gtk_widget_show (priv->hscrollbar);
1476 child_allocation.x = relative_allocation.x;
1477 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1478 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1479 child_allocation.y = (relative_allocation.y +
1480 relative_allocation.height +
1482 (priv->shadow_type == GTK_SHADOW_NONE ?
1483 0 : style->ythickness));
1485 child_allocation.y = border_width;
1487 child_allocation.width = relative_allocation.width;
1488 child_allocation.height = hscrollbar_requisition.height;
1489 child_allocation.x += allocation->x;
1490 child_allocation.y += allocation->y;
1492 if (priv->shadow_type != GTK_SHADOW_NONE)
1494 if (!scrollbars_within_bevel)
1496 child_allocation.x -= style->xthickness;
1497 child_allocation.width += 2 * style->xthickness;
1499 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1500 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1502 child_allocation.y -= style->ythickness;
1506 child_allocation.y += style->ythickness;
1510 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1512 else if (gtk_widget_get_visible (priv->hscrollbar))
1513 gtk_widget_hide (priv->hscrollbar);
1515 if (priv->vscrollbar_visible)
1517 GtkRequisition vscrollbar_requisition;
1518 if (!gtk_widget_get_visible (priv->vscrollbar))
1519 gtk_widget_show (priv->vscrollbar);
1521 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->vscrollbar),
1522 &vscrollbar_requisition, NULL);
1524 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1525 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1526 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1527 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1528 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1529 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1530 child_allocation.x = (relative_allocation.x +
1531 relative_allocation.width +
1533 (priv->shadow_type == GTK_SHADOW_NONE ?
1534 0 : style->xthickness));
1536 child_allocation.x = border_width;
1538 child_allocation.y = relative_allocation.y;
1539 child_allocation.width = vscrollbar_requisition.width;
1540 child_allocation.height = relative_allocation.height;
1541 child_allocation.x += allocation->x;
1542 child_allocation.y += allocation->y;
1544 if (priv->shadow_type != GTK_SHADOW_NONE)
1546 if (!scrollbars_within_bevel)
1548 child_allocation.y -= style->ythickness;
1549 child_allocation.height += 2 * style->ythickness;
1551 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1552 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1554 child_allocation.x -= style->xthickness;
1558 child_allocation.x += style->xthickness;
1562 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1564 else if (gtk_widget_get_visible (priv->vscrollbar))
1565 gtk_widget_hide (priv->vscrollbar);
1569 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1570 GdkEventScroll *event)
1572 GtkScrolledWindowPrivate *priv;
1573 GtkScrolledWindow *scrolled_window;
1576 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1577 g_return_val_if_fail (event != NULL, FALSE);
1579 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1580 priv = scrolled_window->priv;
1582 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1583 range = priv->vscrollbar;
1585 range = priv->hscrollbar;
1587 if (range && gtk_widget_get_visible (range))
1589 GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE (range));
1592 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1594 gtk_adjustment_set_value (adj, adj->value + delta);
1603 gtk_scrolled_window_focus (GtkWidget *widget,
1604 GtkDirectionType direction)
1606 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1607 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1609 gboolean had_focus_child;
1611 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1613 if (priv->focus_out)
1615 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1619 if (gtk_widget_is_focus (widget))
1622 /* We only put the scrolled window itself in the focus chain if it
1623 * isn't possible to focus any children.
1625 child = gtk_bin_get_child (GTK_BIN (widget));
1628 if (gtk_widget_child_focus (child, direction))
1632 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1634 gtk_widget_grab_focus (widget);
1642 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1645 GtkScrolledWindowPrivate *priv;;
1646 GtkScrolledWindow *scrolled_window;
1648 g_return_if_fail (adjustment != NULL);
1649 g_return_if_fail (data != NULL);
1651 scrolled_window = GTK_SCROLLED_WINDOW (data);
1652 priv = scrolled_window->priv;
1654 if (priv->hscrollbar &&
1655 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1657 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1661 visible = priv->hscrollbar_visible;
1662 priv->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1663 adjustment->page_size);
1664 if (priv->hscrollbar_visible != visible)
1665 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1668 else if (priv->vscrollbar &&
1669 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1671 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1675 visible = priv->vscrollbar_visible;
1676 priv->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1677 adjustment->page_size);
1678 if (priv->vscrollbar_visible != visible)
1679 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1685 gtk_scrolled_window_add (GtkContainer *container,
1688 GtkScrolledWindowPrivate *priv;
1689 GtkScrolledWindow *scrolled_window;
1691 GtkWidget *child_widget;
1693 bin = GTK_BIN (container);
1694 child_widget = gtk_bin_get_child (bin);
1695 g_return_if_fail (child_widget == NULL);
1697 scrolled_window = GTK_SCROLLED_WINDOW (container);
1698 priv = scrolled_window->priv;
1700 _gtk_bin_set_child (bin, child);
1701 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1703 /* this is a temporary message */
1704 if (!gtk_widget_set_scroll_adjustments (child,
1705 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1706 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar))))
1707 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1708 "use gtk_scrolled_window_add_with_viewport() instead");
1712 gtk_scrolled_window_remove (GtkContainer *container,
1715 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1716 g_return_if_fail (child != NULL);
1717 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1719 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1721 /* chain parent class handler to remove child */
1722 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1726 * gtk_scrolled_window_add_with_viewport:
1727 * @scrolled_window: a #GtkScrolledWindow
1728 * @child: the widget you want to scroll
1730 * Used to add children without native scrolling capabilities. This
1731 * is simply a convenience function; it is equivalent to adding the
1732 * unscrollable child to a viewport, then adding the viewport to the
1733 * scrolled window. If a child has native scrolling, use
1734 * gtk_container_add() instead of this function.
1736 * The viewport scrolls the child by moving its #GdkWindow, and takes
1737 * the size of the child to be the size of its toplevel #GdkWindow.
1738 * This will be very wrong for most widgets that support native scrolling;
1739 * for example, if you add a widget such as #GtkTreeView with a viewport,
1740 * the whole widget will scroll, including the column headings. Thus,
1741 * widgets with native scrolling support should not be used with the
1742 * #GtkViewport proxy.
1744 * A widget supports scrolling natively if the
1745 * set_scroll_adjustments_signal field in #GtkWidgetClass is non-zero,
1746 * i.e. has been filled in with a valid signal identifier.
1749 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1753 GtkWidget *viewport;
1754 GtkWidget *child_widget;
1756 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1757 g_return_if_fail (GTK_IS_WIDGET (child));
1758 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1760 bin = GTK_BIN (scrolled_window);
1761 child_widget = gtk_bin_get_child (bin);
1765 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
1766 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
1768 viewport = child_widget;
1773 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1774 gtk_scrolled_window_get_vadjustment (scrolled_window));
1775 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1778 gtk_widget_show (viewport);
1779 gtk_container_add (GTK_CONTAINER (viewport), child);
1783 * _gtk_scrolled_window_get_spacing:
1784 * @scrolled_window: a scrolled window
1786 * Gets the spacing between the scrolled window's scrollbars and
1787 * the scrolled widget. Used by GtkCombo
1789 * Return value: the spacing, in pixels.
1792 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
1794 GtkScrolledWindowClass *class;
1796 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
1798 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
1800 if (class->scrollbar_spacing >= 0)
1801 return class->scrollbar_spacing;
1804 gint scrollbar_spacing;
1806 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
1807 "scrollbar-spacing", &scrollbar_spacing,
1810 return scrollbar_spacing;
1816 gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface)
1818 iface->get_width = gtk_scrolled_window_get_width;
1819 iface->get_height = gtk_scrolled_window_get_height;
1820 iface->get_height_for_width = gtk_scrolled_window_get_height_for_width;
1821 iface->get_width_for_height = gtk_scrolled_window_get_width_for_height;
1825 gtk_scrolled_window_get_size (GtkSizeRequest *widget,
1826 GtkOrientation orientation,
1830 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1831 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1832 GtkBin *bin = GTK_BIN (scrolled_window);
1835 gint scrollbar_spacing;
1836 GtkRequisition hscrollbar_requisition;
1837 GtkRequisition vscrollbar_requisition;
1838 GtkRequisition minimum_req, natural_req;
1841 gint min_child_size, nat_child_size;
1844 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1848 minimum_req.width = 0;
1849 minimum_req.height = 0;
1850 natural_req.width = 0;
1851 natural_req.height = 0;
1853 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->hscrollbar),
1854 &hscrollbar_requisition, NULL);
1855 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->vscrollbar),
1856 &vscrollbar_requisition, NULL);
1858 child = gtk_bin_get_child (bin);
1859 if (child && gtk_widget_get_visible (child))
1861 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1863 gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
1867 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1869 minimum_req.width += min_child_size;
1870 natural_req.width += nat_child_size;
1874 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1876 if (aux_info && aux_info->width > 0)
1878 minimum_req.width += aux_info->width;
1879 natural_req.width += aux_info->width;
1884 minimum_req.width += vscrollbar_requisition.width;
1885 natural_req.width += vscrollbar_requisition.width;
1889 else /* GTK_ORIENTATION_VERTICAL */
1891 gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
1895 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1897 minimum_req.height += min_child_size;
1898 natural_req.height += nat_child_size;
1902 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1904 if (aux_info && aux_info->height > 0)
1906 minimum_req.height += aux_info->height;
1907 natural_req.height += aux_info->height;
1912 minimum_req.height += hscrollbar_requisition.height;
1913 natural_req.height += hscrollbar_requisition.height;
1919 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1920 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1922 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
1923 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
1924 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1925 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
1928 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1929 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1931 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
1932 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
1933 if (!extra_height || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1934 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
1937 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1938 minimum_req.width += border_width * 2 + MAX (0, extra_width);
1939 minimum_req.height += border_width * 2 + MAX (0, extra_height);
1940 natural_req.width += border_width * 2 + MAX (0, extra_width);
1941 natural_req.height += border_width * 2 + MAX (0, extra_height);
1943 if (priv->shadow_type != GTK_SHADOW_NONE)
1945 style = gtk_widget_get_style (GTK_WIDGET (widget));
1946 minimum_req.width += 2 * style->xthickness;
1947 minimum_req.height += 2 * style->ythickness;
1948 natural_req.width += 2 * style->xthickness;
1949 natural_req.height += 2 * style->ythickness;
1952 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1955 *minimum_size = minimum_req.width;
1957 *natural_size = natural_req.width;
1962 *minimum_size = minimum_req.height;
1964 *natural_size = natural_req.height;
1969 gtk_scrolled_window_get_width (GtkSizeRequest *widget,
1973 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1977 gtk_scrolled_window_get_height (GtkSizeRequest *widget,
1981 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1985 gtk_scrolled_window_get_height_for_width (GtkSizeRequest *widget,
1987 gint *minimum_height,
1988 gint *natural_height)
1990 g_return_if_fail (GTK_IS_WIDGET (widget));
1992 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
1996 gtk_scrolled_window_get_width_for_height (GtkSizeRequest *widget,
1998 gint *minimum_width,
1999 gint *natural_width)
2001 g_return_if_fail (GTK_IS_WIDGET (widget));
2003 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);