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: 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
608 * Returns: the vertical #GtkAdjustment
611 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
613 GtkScrolledWindowPrivate *priv;
615 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
617 priv = scrolled_window->priv;
619 return (priv->vscrollbar ?
620 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)) :
625 * gtk_scrolled_window_get_hscrollbar:
626 * @scrolled_window: a #GtkScrolledWindow
628 * Returns the horizontal scrollbar of @scrolled_window.
630 * Returns: the horizontal scrollbar of the scrolled window, or
631 * %NULL if it does not have one.
636 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
638 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
640 return scrolled_window->priv->hscrollbar;
644 * gtk_scrolled_window_get_vscrollbar:
645 * @scrolled_window: a #GtkScrolledWindow
647 * Returns the vertical scrollbar of @scrolled_window.
649 * Returns: the vertical scrollbar of the scrolled window, or
650 * %NULL if it does not have one.
655 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
657 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
659 return scrolled_window->priv->vscrollbar;
663 * gtk_scrolled_window_set_policy:
664 * @scrolled_window: a #GtkScrolledWindow
665 * @hscrollbar_policy: policy for horizontal bar
666 * @vscrollbar_policy: policy for vertical bar
668 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
670 * The policy determines when the scrollbar should appear; it is a value
671 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
672 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
673 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
674 * if needed (that is, if the slider part of the bar would be smaller
675 * than the trough - the display is larger than the page size).
678 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
679 GtkPolicyType hscrollbar_policy,
680 GtkPolicyType vscrollbar_policy)
682 GtkScrolledWindowPrivate *priv;
683 GObject *object = G_OBJECT (scrolled_window);
685 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
687 priv = scrolled_window->priv;
689 if ((priv->hscrollbar_policy != hscrollbar_policy) ||
690 (priv->vscrollbar_policy != vscrollbar_policy))
692 priv->hscrollbar_policy = hscrollbar_policy;
693 priv->vscrollbar_policy = vscrollbar_policy;
695 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
697 g_object_freeze_notify (object);
698 g_object_notify (object, "hscrollbar-policy");
699 g_object_notify (object, "vscrollbar-policy");
700 g_object_thaw_notify (object);
705 * gtk_scrolled_window_get_policy:
706 * @scrolled_window: a #GtkScrolledWindow
707 * @hscrollbar_policy: location to store the policy for the horizontal
708 * scrollbar, or %NULL.
709 * @vscrollbar_policy: location to store the policy for the vertical
710 * scrollbar, or %NULL.
712 * Retrieves the current policy values for the horizontal and vertical
713 * scrollbars. See gtk_scrolled_window_set_policy().
716 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
717 GtkPolicyType *hscrollbar_policy,
718 GtkPolicyType *vscrollbar_policy)
720 GtkScrolledWindowPrivate *priv;
722 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
724 priv = scrolled_window->priv;
726 if (hscrollbar_policy)
727 *hscrollbar_policy = priv->hscrollbar_policy;
728 if (vscrollbar_policy)
729 *vscrollbar_policy = priv->vscrollbar_policy;
733 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
735 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
736 GtkSettings *settings;
738 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
740 if (priv->window_placement_set || settings == NULL)
741 priv->real_window_placement = priv->window_placement;
743 g_object_get (settings,
744 "gtk-scrolled-window-placement",
745 &priv->real_window_placement,
750 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
751 GtkCornerType window_placement)
753 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
755 if (priv->window_placement != window_placement)
757 priv->window_placement = window_placement;
759 gtk_scrolled_window_update_real_placement (scrolled_window);
760 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
762 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
767 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
768 gboolean placement_set,
769 gboolean emit_resize)
771 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
773 if (priv->window_placement_set != placement_set)
775 priv->window_placement_set = placement_set;
777 gtk_scrolled_window_update_real_placement (scrolled_window);
779 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
781 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
786 * gtk_scrolled_window_set_placement:
787 * @scrolled_window: a #GtkScrolledWindow
788 * @window_placement: position of the child window
790 * Sets the placement of the contents with respect to the scrollbars
791 * for the scrolled window.
793 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
794 * in the top left, with the scrollbars underneath and to the right.
795 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
796 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
798 * See also gtk_scrolled_window_get_placement() and
799 * gtk_scrolled_window_unset_placement().
802 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
803 GtkCornerType window_placement)
805 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
807 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
808 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
812 * gtk_scrolled_window_get_placement:
813 * @scrolled_window: a #GtkScrolledWindow
815 * Gets the placement of the contents with respect to the scrollbars
816 * for the scrolled window. See gtk_scrolled_window_set_placement().
818 * Return value: the current placement value.
820 * See also gtk_scrolled_window_set_placement() and
821 * gtk_scrolled_window_unset_placement().
824 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
826 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
828 return scrolled_window->priv->window_placement;
832 * gtk_scrolled_window_unset_placement:
833 * @scrolled_window: a #GtkScrolledWindow
835 * Unsets the placement of the contents with respect to the scrollbars
836 * for the scrolled window. If no window placement is set for a scrolled
837 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
839 * See also gtk_scrolled_window_set_placement() and
840 * gtk_scrolled_window_get_placement().
845 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
847 GtkScrolledWindowPrivate *priv;
849 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
851 priv = scrolled_window->priv;
853 if (priv->window_placement_set)
855 priv->window_placement_set = FALSE;
857 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
859 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
864 * gtk_scrolled_window_set_shadow_type:
865 * @scrolled_window: a #GtkScrolledWindow
866 * @type: kind of shadow to draw around scrolled window contents
868 * Changes the type of shadow drawn around the contents of
873 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
876 GtkScrolledWindowPrivate *priv;
878 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
879 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
881 priv = scrolled_window->priv;
883 if (priv->shadow_type != type)
885 priv->shadow_type = type;
887 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
888 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
890 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
892 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
897 * gtk_scrolled_window_get_shadow_type:
898 * @scrolled_window: a #GtkScrolledWindow
900 * Gets the shadow type of the scrolled window. See
901 * gtk_scrolled_window_set_shadow_type().
903 * Return value: the current shadow type
906 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
908 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
910 return scrolled_window->priv->shadow_type;
914 gtk_scrolled_window_destroy (GtkObject *object)
916 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
917 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
919 if (priv->hscrollbar)
921 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
922 gtk_scrolled_window_adjustment_changed,
924 gtk_widget_unparent (priv->hscrollbar);
925 gtk_widget_destroy (priv->hscrollbar);
926 g_object_unref (priv->hscrollbar);
927 priv->hscrollbar = NULL;
929 if (priv->vscrollbar)
931 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)),
932 gtk_scrolled_window_adjustment_changed,
934 gtk_widget_unparent (priv->vscrollbar);
935 gtk_widget_destroy (priv->vscrollbar);
936 g_object_unref (priv->vscrollbar);
937 priv->vscrollbar = NULL;
940 GTK_OBJECT_CLASS (gtk_scrolled_window_parent_class)->destroy (object);
944 gtk_scrolled_window_set_property (GObject *object,
949 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
950 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
954 case PROP_HADJUSTMENT:
955 gtk_scrolled_window_set_hadjustment (scrolled_window,
956 g_value_get_object (value));
958 case PROP_VADJUSTMENT:
959 gtk_scrolled_window_set_vadjustment (scrolled_window,
960 g_value_get_object (value));
962 case PROP_HSCROLLBAR_POLICY:
963 gtk_scrolled_window_set_policy (scrolled_window,
964 g_value_get_enum (value),
965 priv->vscrollbar_policy);
967 case PROP_VSCROLLBAR_POLICY:
968 gtk_scrolled_window_set_policy (scrolled_window,
969 priv->hscrollbar_policy,
970 g_value_get_enum (value));
972 case PROP_WINDOW_PLACEMENT:
973 gtk_scrolled_window_set_placement_internal (scrolled_window,
974 g_value_get_enum (value));
976 case PROP_WINDOW_PLACEMENT_SET:
977 gtk_scrolled_window_set_placement_set (scrolled_window,
978 g_value_get_boolean (value),
981 case PROP_SHADOW_TYPE:
982 gtk_scrolled_window_set_shadow_type (scrolled_window,
983 g_value_get_enum (value));
986 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
992 gtk_scrolled_window_get_property (GObject *object,
997 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
998 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1002 case PROP_HADJUSTMENT:
1003 g_value_set_object (value,
1004 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
1006 case PROP_VADJUSTMENT:
1007 g_value_set_object (value,
1008 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
1010 case PROP_HSCROLLBAR_POLICY:
1011 g_value_set_enum (value, priv->hscrollbar_policy);
1013 case PROP_VSCROLLBAR_POLICY:
1014 g_value_set_enum (value, priv->vscrollbar_policy);
1016 case PROP_WINDOW_PLACEMENT:
1017 g_value_set_enum (value, priv->window_placement);
1019 case PROP_WINDOW_PLACEMENT_SET:
1020 g_value_set_boolean (value, priv->window_placement_set);
1022 case PROP_SHADOW_TYPE:
1023 g_value_set_enum (value, priv->shadow_type);
1026 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1032 traverse_container (GtkWidget *widget,
1035 if (GTK_IS_SCROLLED_WINDOW (widget))
1037 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1038 gtk_widget_queue_resize (widget);
1040 else if (GTK_IS_CONTAINER (widget))
1041 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1045 gtk_scrolled_window_settings_changed (GtkSettings *settings)
1049 list = gtk_window_list_toplevels ();
1051 for (l = list; l; l = l->next)
1052 gtk_container_forall (GTK_CONTAINER (l->data),
1053 traverse_container, NULL);
1059 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1060 GdkScreen *previous_screen)
1062 GtkSettings *settings;
1063 guint window_placement_connection;
1065 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1067 if (!gtk_widget_has_screen (widget))
1070 settings = gtk_widget_get_settings (widget);
1072 window_placement_connection =
1073 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1074 "gtk-scrolled-window-connection"));
1076 if (window_placement_connection)
1079 window_placement_connection =
1080 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1081 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1082 g_object_set_data (G_OBJECT (settings),
1083 I_("gtk-scrolled-window-connection"),
1084 GUINT_TO_POINTER (window_placement_connection));
1088 gtk_scrolled_window_paint (GtkWidget *widget,
1091 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1092 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1094 if (priv->shadow_type != GTK_SHADOW_NONE)
1096 GtkAllocation allocation;
1097 GtkAllocation relative_allocation;
1099 gboolean scrollbars_within_bevel;
1101 style = gtk_widget_get_style (widget);
1102 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1104 gtk_widget_get_allocation (widget, &allocation);
1106 if (!scrollbars_within_bevel)
1108 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1110 relative_allocation.x -= style->xthickness;
1111 relative_allocation.y -= style->ythickness;
1112 relative_allocation.width += 2 * style->xthickness;
1113 relative_allocation.height += 2 * style->ythickness;
1117 GtkContainer *container = GTK_CONTAINER (widget);
1120 border_width = gtk_container_get_border_width (container);
1122 relative_allocation.x = border_width;
1123 relative_allocation.y = border_width;
1124 relative_allocation.width = allocation.width - 2 * border_width;
1125 relative_allocation.height = allocation.height - 2 * border_width;
1128 gtk_paint_shadow (style,
1129 gtk_widget_get_window (widget),
1130 GTK_STATE_NORMAL, priv->shadow_type,
1131 area, widget, "scrolled_window",
1132 allocation.x + relative_allocation.x,
1133 allocation.y + relative_allocation.y,
1134 relative_allocation.width,
1135 relative_allocation.height);
1140 gtk_scrolled_window_expose (GtkWidget *widget,
1141 GdkEventExpose *event)
1143 if (gtk_widget_is_drawable (widget))
1145 gtk_scrolled_window_paint (widget, &event->area);
1147 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->expose_event (widget, event);
1154 gtk_scrolled_window_forall (GtkContainer *container,
1155 gboolean include_internals,
1156 GtkCallback callback,
1157 gpointer callback_data)
1159 GtkScrolledWindowPrivate *priv;
1160 GtkScrolledWindow *scrolled_window;
1162 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1163 g_return_if_fail (callback != NULL);
1165 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1169 if (include_internals)
1171 scrolled_window = GTK_SCROLLED_WINDOW (container);
1172 priv = scrolled_window->priv;
1174 if (priv->vscrollbar)
1175 callback (priv->vscrollbar, callback_data);
1176 if (priv->hscrollbar)
1177 callback (priv->hscrollbar, callback_data);
1182 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1183 GtkScrollType scroll,
1184 gboolean horizontal)
1186 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1187 GtkAdjustment *adjustment = NULL;
1191 case GTK_SCROLL_STEP_UP:
1192 scroll = GTK_SCROLL_STEP_BACKWARD;
1195 case GTK_SCROLL_STEP_DOWN:
1196 scroll = GTK_SCROLL_STEP_FORWARD;
1199 case GTK_SCROLL_STEP_LEFT:
1200 scroll = GTK_SCROLL_STEP_BACKWARD;
1203 case GTK_SCROLL_STEP_RIGHT:
1204 scroll = GTK_SCROLL_STEP_FORWARD;
1207 case GTK_SCROLL_PAGE_UP:
1208 scroll = GTK_SCROLL_PAGE_BACKWARD;
1211 case GTK_SCROLL_PAGE_DOWN:
1212 scroll = GTK_SCROLL_PAGE_FORWARD;
1215 case GTK_SCROLL_PAGE_LEFT:
1216 scroll = GTK_SCROLL_STEP_BACKWARD;
1219 case GTK_SCROLL_PAGE_RIGHT:
1220 scroll = GTK_SCROLL_STEP_FORWARD;
1223 case GTK_SCROLL_STEP_BACKWARD:
1224 case GTK_SCROLL_STEP_FORWARD:
1225 case GTK_SCROLL_PAGE_BACKWARD:
1226 case GTK_SCROLL_PAGE_FORWARD:
1227 case GTK_SCROLL_START:
1228 case GTK_SCROLL_END:
1231 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1235 if ((horizontal && (!priv->hscrollbar || !priv->hscrollbar_visible)) ||
1236 (!horizontal && (!priv->vscrollbar || !priv->vscrollbar_visible)))
1241 if (priv->hscrollbar)
1242 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
1246 if (priv->vscrollbar)
1247 adjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
1252 gdouble value = adjustment->value;
1256 case GTK_SCROLL_STEP_FORWARD:
1257 value += adjustment->step_increment;
1259 case GTK_SCROLL_STEP_BACKWARD:
1260 value -= adjustment->step_increment;
1262 case GTK_SCROLL_PAGE_FORWARD:
1263 value += adjustment->page_increment;
1265 case GTK_SCROLL_PAGE_BACKWARD:
1266 value -= adjustment->page_increment;
1268 case GTK_SCROLL_START:
1269 value = adjustment->lower;
1271 case GTK_SCROLL_END:
1272 value = adjustment->upper;
1275 g_assert_not_reached ();
1279 gtk_adjustment_set_value (adjustment, value);
1288 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1289 GtkDirectionType direction_type)
1291 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1292 GtkWidget *toplevel;
1294 /* Focus out of the scrolled window entirely. We do this by setting
1295 * a flag, then propagating the focus motion to the notebook.
1297 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1298 if (!gtk_widget_is_toplevel (toplevel))
1301 g_object_ref (scrolled_window);
1303 priv->focus_out = TRUE;
1304 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1305 priv->focus_out = FALSE;
1307 g_object_unref (scrolled_window);
1311 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1312 GtkAllocation *allocation)
1314 GtkAllocation widget_allocation;
1315 GtkScrolledWindow *scrolled_window;
1316 GtkScrolledWindowPrivate *priv;
1318 gint scrollbar_spacing;
1321 g_return_if_fail (widget != NULL);
1322 g_return_if_fail (allocation != NULL);
1324 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1325 priv = scrolled_window->priv;
1327 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1329 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1330 allocation->x = border_width;
1331 allocation->y = border_width;
1333 if (priv->shadow_type != GTK_SHADOW_NONE)
1335 style = gtk_widget_get_style (widget);
1336 allocation->x += style->xthickness;
1337 allocation->y += style->ythickness;
1340 gtk_widget_get_allocation (widget, &widget_allocation);
1341 allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
1342 allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
1344 if (priv->vscrollbar_visible)
1346 GtkRequisition vscrollbar_requisition;
1349 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->vscrollbar),
1350 &vscrollbar_requisition, NULL);
1351 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1354 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1355 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1357 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1358 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1359 allocation->x += (vscrollbar_requisition.width + scrollbar_spacing);
1361 allocation->width = MAX (1, allocation->width - (vscrollbar_requisition.width + scrollbar_spacing));
1363 if (priv->hscrollbar_visible)
1365 GtkRequisition hscrollbar_requisition;
1367 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->hscrollbar),
1368 &hscrollbar_requisition, NULL);
1370 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1371 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1372 allocation->y += (hscrollbar_requisition.height + scrollbar_spacing);
1374 allocation->height = MAX (1, allocation->height - (hscrollbar_requisition.height + scrollbar_spacing));
1379 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1380 GtkAllocation *allocation)
1382 GtkScrolledWindow *scrolled_window;
1383 GtkScrolledWindowPrivate *priv;
1386 GtkAllocation relative_allocation;
1387 GtkAllocation child_allocation;
1389 gboolean scrollbars_within_bevel;
1390 gint scrollbar_spacing;
1393 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1394 g_return_if_fail (allocation != NULL);
1396 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1397 bin = GTK_BIN (scrolled_window);
1398 priv = scrolled_window->priv;
1400 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1402 style = gtk_widget_get_style (widget);
1403 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1405 border_width = gtk_container_get_border_width (GTK_CONTAINER (scrolled_window));
1407 gtk_widget_set_allocation (widget, allocation);
1409 if (priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1410 priv->hscrollbar_visible = TRUE;
1411 else if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1412 priv->hscrollbar_visible = FALSE;
1413 if (priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1414 priv->vscrollbar_visible = TRUE;
1415 else if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1416 priv->vscrollbar_visible = FALSE;
1418 child = gtk_bin_get_child (bin);
1419 if (child && gtk_widget_get_visible (child))
1421 gboolean previous_hvis;
1422 gboolean previous_vvis;
1427 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1429 child_allocation.x = relative_allocation.x + allocation->x;
1430 child_allocation.y = relative_allocation.y + allocation->y;
1431 child_allocation.width = relative_allocation.width;
1432 child_allocation.height = relative_allocation.height;
1434 previous_hvis = priv->hscrollbar_visible;
1435 previous_vvis = priv->vscrollbar_visible;
1437 gtk_widget_size_allocate (child, &child_allocation);
1439 /* If, after the first iteration, the hscrollbar and the
1440 * vscrollbar flip visiblity, then we need both.
1443 previous_hvis != priv->hscrollbar_visible &&
1444 previous_vvis != priv->vscrollbar_visible)
1446 priv->hscrollbar_visible = TRUE;
1447 priv->vscrollbar_visible = TRUE;
1449 /* a new resize is already queued at this point,
1450 * so we will immediatedly get reinvoked
1457 while (previous_hvis != priv->hscrollbar_visible ||
1458 previous_vvis != priv->vscrollbar_visible);
1462 priv->hscrollbar_visible = priv->hscrollbar_policy == GTK_POLICY_ALWAYS;
1463 priv->vscrollbar_visible = priv->vscrollbar_policy == GTK_POLICY_ALWAYS;
1464 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1467 if (priv->hscrollbar_visible)
1469 GtkRequisition hscrollbar_requisition;
1471 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->hscrollbar),
1472 &hscrollbar_requisition, NULL);
1474 if (!gtk_widget_get_visible (priv->hscrollbar))
1475 gtk_widget_show (priv->hscrollbar);
1477 child_allocation.x = relative_allocation.x;
1478 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1479 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1480 child_allocation.y = (relative_allocation.y +
1481 relative_allocation.height +
1483 (priv->shadow_type == GTK_SHADOW_NONE ?
1484 0 : style->ythickness));
1486 child_allocation.y = border_width;
1488 child_allocation.width = relative_allocation.width;
1489 child_allocation.height = hscrollbar_requisition.height;
1490 child_allocation.x += allocation->x;
1491 child_allocation.y += allocation->y;
1493 if (priv->shadow_type != GTK_SHADOW_NONE)
1495 if (!scrollbars_within_bevel)
1497 child_allocation.x -= style->xthickness;
1498 child_allocation.width += 2 * style->xthickness;
1500 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1501 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1503 child_allocation.y -= style->ythickness;
1507 child_allocation.y += style->ythickness;
1511 gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
1513 else if (gtk_widget_get_visible (priv->hscrollbar))
1514 gtk_widget_hide (priv->hscrollbar);
1516 if (priv->vscrollbar_visible)
1518 GtkRequisition vscrollbar_requisition;
1519 if (!gtk_widget_get_visible (priv->vscrollbar))
1520 gtk_widget_show (priv->vscrollbar);
1522 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->vscrollbar),
1523 &vscrollbar_requisition, NULL);
1525 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1526 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1527 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1528 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1529 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1530 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1531 child_allocation.x = (relative_allocation.x +
1532 relative_allocation.width +
1534 (priv->shadow_type == GTK_SHADOW_NONE ?
1535 0 : style->xthickness));
1537 child_allocation.x = border_width;
1539 child_allocation.y = relative_allocation.y;
1540 child_allocation.width = vscrollbar_requisition.width;
1541 child_allocation.height = relative_allocation.height;
1542 child_allocation.x += allocation->x;
1543 child_allocation.y += allocation->y;
1545 if (priv->shadow_type != GTK_SHADOW_NONE)
1547 if (!scrollbars_within_bevel)
1549 child_allocation.y -= style->ythickness;
1550 child_allocation.height += 2 * style->ythickness;
1552 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1553 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1555 child_allocation.x -= style->xthickness;
1559 child_allocation.x += style->xthickness;
1563 gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
1565 else if (gtk_widget_get_visible (priv->vscrollbar))
1566 gtk_widget_hide (priv->vscrollbar);
1570 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1571 GdkEventScroll *event)
1573 GtkScrolledWindowPrivate *priv;
1574 GtkScrolledWindow *scrolled_window;
1577 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1578 g_return_val_if_fail (event != NULL, FALSE);
1580 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1581 priv = scrolled_window->priv;
1583 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1584 range = priv->vscrollbar;
1586 range = priv->hscrollbar;
1588 if (range && gtk_widget_get_visible (range))
1590 GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE (range));
1593 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1595 gtk_adjustment_set_value (adj, adj->value + delta);
1604 gtk_scrolled_window_focus (GtkWidget *widget,
1605 GtkDirectionType direction)
1607 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1608 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1610 gboolean had_focus_child;
1612 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1614 if (priv->focus_out)
1616 priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1620 if (gtk_widget_is_focus (widget))
1623 /* We only put the scrolled window itself in the focus chain if it
1624 * isn't possible to focus any children.
1626 child = gtk_bin_get_child (GTK_BIN (widget));
1629 if (gtk_widget_child_focus (child, direction))
1633 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1635 gtk_widget_grab_focus (widget);
1643 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1646 GtkScrolledWindowPrivate *priv;;
1647 GtkScrolledWindow *scrolled_window;
1649 g_return_if_fail (adjustment != NULL);
1650 g_return_if_fail (data != NULL);
1652 scrolled_window = GTK_SCROLLED_WINDOW (data);
1653 priv = scrolled_window->priv;
1655 if (priv->hscrollbar &&
1656 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)))
1658 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1662 visible = priv->hscrollbar_visible;
1663 priv->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1664 adjustment->page_size);
1665 if (priv->hscrollbar_visible != visible)
1666 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1669 else if (priv->vscrollbar &&
1670 adjustment == gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar)))
1672 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1676 visible = priv->vscrollbar_visible;
1677 priv->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1678 adjustment->page_size);
1679 if (priv->vscrollbar_visible != visible)
1680 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
1686 gtk_scrolled_window_add (GtkContainer *container,
1689 GtkScrolledWindowPrivate *priv;
1690 GtkScrolledWindow *scrolled_window;
1692 GtkWidget *child_widget;
1694 bin = GTK_BIN (container);
1695 child_widget = gtk_bin_get_child (bin);
1696 g_return_if_fail (child_widget == NULL);
1698 scrolled_window = GTK_SCROLLED_WINDOW (container);
1699 priv = scrolled_window->priv;
1701 _gtk_bin_set_child (bin, child);
1702 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1704 /* this is a temporary message */
1705 if (!gtk_widget_set_scroll_adjustments (child,
1706 gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar)),
1707 gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar))))
1708 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1709 "use gtk_scrolled_window_add_with_viewport() instead");
1713 gtk_scrolled_window_remove (GtkContainer *container,
1716 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1717 g_return_if_fail (child != NULL);
1718 g_return_if_fail (gtk_bin_get_child (GTK_BIN (container)) == child);
1720 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1722 /* chain parent class handler to remove child */
1723 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1727 * gtk_scrolled_window_add_with_viewport:
1728 * @scrolled_window: a #GtkScrolledWindow
1729 * @child: the widget you want to scroll
1731 * Used to add children without native scrolling capabilities. This
1732 * is simply a convenience function; it is equivalent to adding the
1733 * unscrollable child to a viewport, then adding the viewport to the
1734 * scrolled window. If a child has native scrolling, use
1735 * gtk_container_add() instead of this function.
1737 * The viewport scrolls the child by moving its #GdkWindow, and takes
1738 * the size of the child to be the size of its toplevel #GdkWindow.
1739 * This will be very wrong for most widgets that support native scrolling;
1740 * for example, if you add a widget such as #GtkTreeView with a viewport,
1741 * the whole widget will scroll, including the column headings. Thus,
1742 * widgets with native scrolling support should not be used with the
1743 * #GtkViewport proxy.
1745 * A widget supports scrolling natively if the
1746 * set_scroll_adjustments_signal field in #GtkWidgetClass is non-zero,
1747 * i.e. has been filled in with a valid signal identifier.
1750 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1754 GtkWidget *viewport;
1755 GtkWidget *child_widget;
1757 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1758 g_return_if_fail (GTK_IS_WIDGET (child));
1759 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
1761 bin = GTK_BIN (scrolled_window);
1762 child_widget = gtk_bin_get_child (bin);
1766 g_return_if_fail (GTK_IS_VIEWPORT (child_widget));
1767 g_return_if_fail (gtk_bin_get_child (GTK_BIN (child_widget)) == NULL);
1769 viewport = child_widget;
1774 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1775 gtk_scrolled_window_get_vadjustment (scrolled_window));
1776 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1779 gtk_widget_show (viewport);
1780 gtk_container_add (GTK_CONTAINER (viewport), child);
1784 * _gtk_scrolled_window_get_spacing:
1785 * @scrolled_window: a scrolled window
1787 * Gets the spacing between the scrolled window's scrollbars and
1788 * the scrolled widget. Used by GtkCombo
1790 * Return value: the spacing, in pixels.
1793 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
1795 GtkScrolledWindowClass *class;
1797 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
1799 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
1801 if (class->scrollbar_spacing >= 0)
1802 return class->scrollbar_spacing;
1805 gint scrollbar_spacing;
1807 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
1808 "scrollbar-spacing", &scrollbar_spacing,
1811 return scrollbar_spacing;
1817 gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface)
1819 iface->get_width = gtk_scrolled_window_get_width;
1820 iface->get_height = gtk_scrolled_window_get_height;
1821 iface->get_height_for_width = gtk_scrolled_window_get_height_for_width;
1822 iface->get_width_for_height = gtk_scrolled_window_get_width_for_height;
1826 gtk_scrolled_window_get_size (GtkSizeRequest *widget,
1827 GtkOrientation orientation,
1831 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1832 GtkScrolledWindowPrivate *priv = scrolled_window->priv;
1833 GtkBin *bin = GTK_BIN (scrolled_window);
1836 gint scrollbar_spacing;
1837 GtkRequisition hscrollbar_requisition;
1838 GtkRequisition vscrollbar_requisition;
1839 GtkRequisition minimum_req, natural_req;
1842 gint min_child_size, nat_child_size;
1845 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1849 minimum_req.width = 0;
1850 minimum_req.height = 0;
1851 natural_req.width = 0;
1852 natural_req.height = 0;
1854 gtk_widget_size_request (priv->hscrollbar,
1855 &hscrollbar_requisition);
1856 gtk_widget_size_request (priv->vscrollbar,
1857 &vscrollbar_requisition);
1859 child = gtk_bin_get_child (bin);
1860 if (child && gtk_widget_get_visible (child))
1862 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1864 gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
1868 if (priv->hscrollbar_policy == GTK_POLICY_NEVER)
1870 minimum_req.width += min_child_size;
1871 natural_req.width += nat_child_size;
1875 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1877 if (aux_info && aux_info->width > 0)
1879 minimum_req.width += aux_info->width;
1880 natural_req.width += aux_info->width;
1885 minimum_req.width += vscrollbar_requisition.width;
1886 natural_req.width += vscrollbar_requisition.width;
1890 else /* GTK_ORIENTATION_VERTICAL */
1892 gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
1896 if (priv->vscrollbar_policy == GTK_POLICY_NEVER)
1898 minimum_req.height += min_child_size;
1899 natural_req.height += nat_child_size;
1903 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (child, FALSE);
1905 if (aux_info && aux_info->height > 0)
1907 minimum_req.height += aux_info->height;
1908 natural_req.height += aux_info->height;
1913 minimum_req.height += hscrollbar_requisition.height;
1914 natural_req.height += hscrollbar_requisition.height;
1920 if (priv->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1921 priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1923 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
1924 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
1925 if (!extra_height || priv->hscrollbar_policy == GTK_POLICY_ALWAYS)
1926 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
1929 if (priv->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1930 priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1932 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
1933 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
1934 if (!extra_height || priv->vscrollbar_policy == GTK_POLICY_ALWAYS)
1935 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
1938 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1939 minimum_req.width += border_width * 2 + MAX (0, extra_width);
1940 minimum_req.height += border_width * 2 + MAX (0, extra_height);
1941 natural_req.width += border_width * 2 + MAX (0, extra_width);
1942 natural_req.height += border_width * 2 + MAX (0, extra_height);
1944 if (priv->shadow_type != GTK_SHADOW_NONE)
1946 style = gtk_widget_get_style (GTK_WIDGET (widget));
1947 minimum_req.width += 2 * style->xthickness;
1948 minimum_req.height += 2 * style->ythickness;
1949 natural_req.width += 2 * style->xthickness;
1950 natural_req.height += 2 * style->ythickness;
1953 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1956 *minimum_size = minimum_req.width;
1958 *natural_size = natural_req.width;
1963 *minimum_size = minimum_req.height;
1965 *natural_size = natural_req.height;
1970 gtk_scrolled_window_get_width (GtkSizeRequest *widget,
1974 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1978 gtk_scrolled_window_get_height (GtkSizeRequest *widget,
1982 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1986 gtk_scrolled_window_get_height_for_width (GtkSizeRequest *widget,
1988 gint *minimum_height,
1989 gint *natural_height)
1991 g_return_if_fail (GTK_IS_WIDGET (widget));
1993 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
1997 gtk_scrolled_window_get_width_for_height (GtkSizeRequest *widget,
1999 gint *minimum_width,
2000 gint *natural_width)
2002 g_return_if_fail (GTK_IS_WIDGET (widget));
2004 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);