1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include <gdk/gdkkeysyms.h>
30 #include "gtkbindings.h"
31 #include "gtksizerequest.h"
32 #include "gtkmarshalers.h"
33 #include "gtkscrolledwindow.h"
34 #include "gtkwindow.h"
35 #include "gtkprivate.h"
38 /* scrolled window policy and size requisition handling:
40 * gtk size requisition works as follows:
41 * a widget upon size-request reports the width and height that it finds
42 * to be best suited to display its contents, including children.
43 * the width and/or height reported from a widget upon size requisition
44 * may be overidden by the user by specifying a width and/or height
45 * other than 0 through gtk_widget_set_size_request().
47 * a scrolled window needs (for implementing all three policy types) to
48 * request its width and height based on two different rationales.
49 * 1) the user wants the scrolled window to just fit into the space
50 * that it gets allocated for a specifc dimension.
51 * 1.1) this does not apply if the user specified a concrete value
52 * value for that specific dimension by either specifying usize for the
53 * scrolled window or for its child.
54 * 2) the user wants the scrolled window to take as much space up as
55 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
57 * also, kinda obvious:
58 * 3) a user would certainly not have choosen a scrolled window as a container
59 * for the child, if the resulting allocation takes up more space than the
60 * child would have allocated without the scrolled window.
63 * A) from 1) follows: the scrolled window shouldn't request more space for a
64 * specifc dimension than is required at minimum.
65 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
66 * window (done automatically) or by usize of the child (needs to be checked).
67 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
69 * D) from 3) follows: the scrolled window child's minimum width and minimum height
70 * under A) at least correspond to the space taken up by its scrollbars.
73 #define DEFAULT_SCROLLBAR_SPACING 3
76 gboolean window_placement_set;
77 GtkCornerType real_window_placement;
78 } GtkScrolledWindowPrivate;
80 #define GTK_SCROLLED_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCROLLED_WINDOW, GtkScrolledWindowPrivate))
86 PROP_HSCROLLBAR_POLICY,
87 PROP_VSCROLLBAR_POLICY,
88 PROP_WINDOW_PLACEMENT,
89 PROP_WINDOW_PLACEMENT_SET,
101 static void gtk_scrolled_window_destroy (GtkObject *object);
102 static void gtk_scrolled_window_set_property (GObject *object,
106 static void gtk_scrolled_window_get_property (GObject *object,
111 static void gtk_scrolled_window_screen_changed (GtkWidget *widget,
112 GdkScreen *previous_screen);
113 static gboolean gtk_scrolled_window_expose (GtkWidget *widget,
114 GdkEventExpose *event);
115 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
116 GtkAllocation *allocation);
117 static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
118 GdkEventScroll *event);
119 static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
120 GtkDirectionType direction);
121 static void gtk_scrolled_window_add (GtkContainer *container,
123 static void gtk_scrolled_window_remove (GtkContainer *container,
125 static void gtk_scrolled_window_forall (GtkContainer *container,
126 gboolean include_internals,
127 GtkCallback callback,
128 gpointer callback_data);
129 static gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
130 GtkScrollType scroll,
131 gboolean horizontal);
132 static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
133 GtkDirectionType direction_type);
135 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
136 GtkAllocation *allocation);
137 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
140 static void gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window);
142 static void gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface);
143 static void gtk_scrolled_window_get_width (GtkSizeRequest *widget,
146 static void gtk_scrolled_window_get_height (GtkSizeRequest *widget,
149 static void gtk_scrolled_window_get_height_for_width (GtkSizeRequest *layout,
151 gint *minimum_height,
152 gint *natural_height);
153 static void gtk_scrolled_window_get_width_for_height (GtkSizeRequest *layout,
155 gint *minimum_height,
156 gint *natural_height);
158 static guint signals[LAST_SIGNAL] = {0};
160 G_DEFINE_TYPE_WITH_CODE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN,
161 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
162 gtk_scrolled_window_size_request_init))
166 add_scroll_binding (GtkBindingSet *binding_set,
168 GdkModifierType mask,
169 GtkScrollType scroll,
172 guint keypad_keyval = keyval - GDK_Left + GDK_KP_Left;
174 gtk_binding_entry_add_signal (binding_set, keyval, mask,
176 GTK_TYPE_SCROLL_TYPE, scroll,
177 G_TYPE_BOOLEAN, horizontal);
178 gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
180 GTK_TYPE_SCROLL_TYPE, scroll,
181 G_TYPE_BOOLEAN, horizontal);
185 add_tab_bindings (GtkBindingSet *binding_set,
186 GdkModifierType modifiers,
187 GtkDirectionType direction)
189 gtk_binding_entry_add_signal (binding_set, GDK_Tab, modifiers,
191 GTK_TYPE_DIRECTION_TYPE, direction);
192 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, modifiers,
194 GTK_TYPE_DIRECTION_TYPE, direction);
198 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
200 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
201 GtkObjectClass *object_class;
202 GtkWidgetClass *widget_class;
203 GtkContainerClass *container_class;
204 GtkBindingSet *binding_set;
206 object_class = (GtkObjectClass*) class;
207 widget_class = (GtkWidgetClass*) class;
208 container_class = (GtkContainerClass*) class;
210 gobject_class->set_property = gtk_scrolled_window_set_property;
211 gobject_class->get_property = gtk_scrolled_window_get_property;
213 object_class->destroy = gtk_scrolled_window_destroy;
215 widget_class->screen_changed = gtk_scrolled_window_screen_changed;
216 widget_class->expose_event = gtk_scrolled_window_expose;
217 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
218 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
219 widget_class->focus = gtk_scrolled_window_focus;
221 container_class->add = gtk_scrolled_window_add;
222 container_class->remove = gtk_scrolled_window_remove;
223 container_class->forall = gtk_scrolled_window_forall;
225 class->scrollbar_spacing = -1;
227 class->scroll_child = gtk_scrolled_window_scroll_child;
228 class->move_focus_out = gtk_scrolled_window_move_focus_out;
230 g_object_class_install_property (gobject_class,
232 g_param_spec_object ("hadjustment",
233 P_("Horizontal Adjustment"),
234 P_("The GtkAdjustment for the horizontal position"),
236 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_object ("vadjustment",
240 P_("Vertical Adjustment"),
241 P_("The GtkAdjustment for the vertical position"),
243 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
244 g_object_class_install_property (gobject_class,
245 PROP_HSCROLLBAR_POLICY,
246 g_param_spec_enum ("hscrollbar-policy",
247 P_("Horizontal Scrollbar Policy"),
248 P_("When the horizontal scrollbar is displayed"),
249 GTK_TYPE_POLICY_TYPE,
251 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
252 g_object_class_install_property (gobject_class,
253 PROP_VSCROLLBAR_POLICY,
254 g_param_spec_enum ("vscrollbar-policy",
255 P_("Vertical Scrollbar Policy"),
256 P_("When the vertical scrollbar is displayed"),
257 GTK_TYPE_POLICY_TYPE,
259 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
261 g_object_class_install_property (gobject_class,
262 PROP_WINDOW_PLACEMENT,
263 g_param_spec_enum ("window-placement",
264 P_("Window Placement"),
265 P_("Where the contents are located with respect to the scrollbars. This property only takes effect if \"window-placement-set\" is TRUE."),
266 GTK_TYPE_CORNER_TYPE,
268 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
271 * GtkScrolledWindow:window-placement-set:
273 * Whether "window-placement" should be used to determine the location
274 * of the contents with respect to the scrollbars. Otherwise, the
275 * "gtk-scrolled-window-placement" setting is used.
279 g_object_class_install_property (gobject_class,
280 PROP_WINDOW_PLACEMENT_SET,
281 g_param_spec_boolean ("window-placement-set",
282 P_("Window Placement Set"),
283 P_("Whether \"window-placement\" should be used to determine the location of the contents with respect to the scrollbars."),
285 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
286 g_object_class_install_property (gobject_class,
288 g_param_spec_enum ("shadow-type",
290 P_("Style of bevel around the contents"),
291 GTK_TYPE_SHADOW_TYPE,
293 GTK_PARAM_READABLE | GTK_PARAM_WRITABLE));
296 * GtkScrolledWindow:scrollbars-within-bevel:
298 * Whether to place scrollbars within the scrolled window's bevel.
302 gtk_widget_class_install_style_property (widget_class,
303 g_param_spec_boolean ("scrollbars-within-bevel",
304 P_("Scrollbars within bevel"),
305 P_("Place scrollbars within the scrolled window's bevel"),
307 GTK_PARAM_READABLE));
309 gtk_widget_class_install_style_property (widget_class,
310 g_param_spec_int ("scrollbar-spacing",
311 P_("Scrollbar spacing"),
312 P_("Number of pixels between the scrollbars and the scrolled window"),
315 DEFAULT_SCROLLBAR_SPACING,
316 GTK_PARAM_READABLE));
319 * GtkSettings:gtk-scrolled-window-placement:
321 * Where the contents of scrolled windows are located with respect to the
322 * scrollbars, if not overridden by the scrolled window's own placement.
326 gtk_settings_install_property (g_param_spec_enum ("gtk-scrolled-window-placement",
327 P_("Scrolled Window Placement"),
328 P_("Where the contents of scrolled windows are located with respect to the scrollbars, if not overridden by the scrolled window's own placement."),
329 GTK_TYPE_CORNER_TYPE,
331 G_PARAM_READABLE | G_PARAM_WRITABLE));
334 signals[SCROLL_CHILD] =
335 g_signal_new (I_("scroll-child"),
336 G_TYPE_FROM_CLASS (object_class),
337 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
338 G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
340 _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
342 GTK_TYPE_SCROLL_TYPE,
344 signals[MOVE_FOCUS_OUT] =
345 g_signal_new (I_("move-focus-out"),
346 G_TYPE_FROM_CLASS (object_class),
347 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
348 G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
350 _gtk_marshal_VOID__ENUM,
352 GTK_TYPE_DIRECTION_TYPE);
354 binding_set = gtk_binding_set_by_class (class);
356 add_scroll_binding (binding_set, GDK_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
357 add_scroll_binding (binding_set, GDK_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
358 add_scroll_binding (binding_set, GDK_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
359 add_scroll_binding (binding_set, GDK_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
361 add_scroll_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
362 add_scroll_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
363 add_scroll_binding (binding_set, GDK_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
364 add_scroll_binding (binding_set, GDK_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
366 add_scroll_binding (binding_set, GDK_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
367 add_scroll_binding (binding_set, GDK_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
368 add_scroll_binding (binding_set, GDK_Home, 0, GTK_SCROLL_START, FALSE);
369 add_scroll_binding (binding_set, GDK_End, 0, GTK_SCROLL_END, FALSE);
371 add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
372 add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
374 g_type_class_add_private (class, sizeof (GtkScrolledWindowPrivate));
378 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
380 gtk_widget_set_has_window (GTK_WIDGET (scrolled_window), FALSE);
381 gtk_widget_set_can_focus (GTK_WIDGET (scrolled_window), TRUE);
383 scrolled_window->hscrollbar = NULL;
384 scrolled_window->vscrollbar = NULL;
385 scrolled_window->hscrollbar_policy = GTK_POLICY_ALWAYS;
386 scrolled_window->vscrollbar_policy = GTK_POLICY_ALWAYS;
387 scrolled_window->hscrollbar_visible = FALSE;
388 scrolled_window->vscrollbar_visible = FALSE;
389 scrolled_window->focus_out = FALSE;
390 scrolled_window->window_placement = GTK_CORNER_TOP_LEFT;
391 gtk_scrolled_window_update_real_placement (scrolled_window);
395 * gtk_scrolled_window_new:
396 * @hadjustment: (allow-none): horizontal adjustment
397 * @vadjustment: (allow-none): vertical adjustment
399 * Creates a new scrolled window.
401 * The two arguments are the scrolled window's adjustments; these will be
402 * shared with the scrollbars and the child widget to keep the bars in sync
403 * with the child. Usually you want to pass %NULL for the adjustments, which
404 * will cause the scrolled window to create them for you.
406 * Returns: a new scrolled window
409 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
410 GtkAdjustment *vadjustment)
412 GtkWidget *scrolled_window;
415 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
418 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
420 scrolled_window = g_object_new (GTK_TYPE_SCROLLED_WINDOW,
421 "hadjustment", hadjustment,
422 "vadjustment", vadjustment,
425 return scrolled_window;
429 * gtk_scrolled_window_set_hadjustment:
430 * @scrolled_window: a #GtkScrolledWindow
431 * @hadjustment: horizontal scroll adjustment
433 * Sets the #GtkAdjustment for the horizontal scrollbar.
436 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
437 GtkAdjustment *hadjustment)
441 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
443 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
445 hadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
447 bin = GTK_BIN (scrolled_window);
449 if (!scrolled_window->hscrollbar)
451 gtk_widget_push_composite_child ();
452 scrolled_window->hscrollbar = gtk_hscrollbar_new (hadjustment);
453 gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
454 gtk_widget_pop_composite_child ();
456 gtk_widget_set_parent (scrolled_window->hscrollbar, GTK_WIDGET (scrolled_window));
457 g_object_ref (scrolled_window->hscrollbar);
458 gtk_widget_show (scrolled_window->hscrollbar);
462 GtkAdjustment *old_adjustment;
464 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
465 if (old_adjustment == hadjustment)
468 g_signal_handlers_disconnect_by_func (old_adjustment,
469 gtk_scrolled_window_adjustment_changed,
471 gtk_range_set_adjustment (GTK_RANGE (scrolled_window->hscrollbar),
474 hadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
475 g_signal_connect (hadjustment,
477 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
479 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
482 gtk_widget_set_scroll_adjustments (bin->child,
483 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
484 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
486 g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
490 * gtk_scrolled_window_set_vadjustment:
491 * @scrolled_window: a #GtkScrolledWindow
492 * @vadjustment: vertical scroll adjustment
494 * Sets the #GtkAdjustment for the vertical scrollbar.
497 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
498 GtkAdjustment *vadjustment)
502 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
504 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
506 vadjustment = (GtkAdjustment*) g_object_new (GTK_TYPE_ADJUSTMENT, NULL);
508 bin = GTK_BIN (scrolled_window);
510 if (!scrolled_window->vscrollbar)
512 gtk_widget_push_composite_child ();
513 scrolled_window->vscrollbar = gtk_vscrollbar_new (vadjustment);
514 gtk_widget_set_composite_name (scrolled_window->vscrollbar, "vscrollbar");
515 gtk_widget_pop_composite_child ();
517 gtk_widget_set_parent (scrolled_window->vscrollbar, GTK_WIDGET (scrolled_window));
518 g_object_ref (scrolled_window->vscrollbar);
519 gtk_widget_show (scrolled_window->vscrollbar);
523 GtkAdjustment *old_adjustment;
525 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
526 if (old_adjustment == vadjustment)
529 g_signal_handlers_disconnect_by_func (old_adjustment,
530 gtk_scrolled_window_adjustment_changed,
532 gtk_range_set_adjustment (GTK_RANGE (scrolled_window->vscrollbar),
535 vadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
536 g_signal_connect (vadjustment,
538 G_CALLBACK (gtk_scrolled_window_adjustment_changed),
540 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
543 gtk_widget_set_scroll_adjustments (bin->child,
544 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
545 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
547 g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
551 * gtk_scrolled_window_get_hadjustment:
552 * @scrolled_window: a #GtkScrolledWindow
554 * Returns the horizontal scrollbar's adjustment, used to connect the
555 * horizontal scrollbar to the child widget's horizontal scroll
558 * Returns: the horizontal #GtkAdjustment
561 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
563 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
565 return (scrolled_window->hscrollbar ?
566 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)) :
571 * gtk_scrolled_window_get_vadjustment:
572 * @scrolled_window: a #GtkScrolledWindow
574 * Returns the vertical scrollbar's adjustment, used to connect the
575 * vertical scrollbar to the child widget's vertical scroll
578 * Returns: the vertical #GtkAdjustment
581 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
583 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
585 return (scrolled_window->vscrollbar ?
586 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)) :
591 * gtk_scrolled_window_get_hscrollbar:
592 * @scrolled_window: a #GtkScrolledWindow
594 * Returns the horizontal scrollbar of @scrolled_window.
596 * Returns: the horizontal scrollbar of the scrolled window, or
597 * %NULL if it does not have one.
602 gtk_scrolled_window_get_hscrollbar (GtkScrolledWindow *scrolled_window)
604 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
606 return scrolled_window->hscrollbar;
610 * gtk_scrolled_window_get_vscrollbar:
611 * @scrolled_window: a #GtkScrolledWindow
613 * Returns the vertical scrollbar of @scrolled_window.
615 * Returns: the vertical scrollbar of the scrolled window, or
616 * %NULL if it does not have one.
621 gtk_scrolled_window_get_vscrollbar (GtkScrolledWindow *scrolled_window)
623 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
625 return scrolled_window->vscrollbar;
629 * gtk_scrolled_window_set_policy:
630 * @scrolled_window: a #GtkScrolledWindow
631 * @hscrollbar_policy: policy for horizontal bar
632 * @vscrollbar_policy: policy for vertical bar
634 * Sets the scrollbar policy for the horizontal and vertical scrollbars.
636 * The policy determines when the scrollbar should appear; it is a value
637 * from the #GtkPolicyType enumeration. If %GTK_POLICY_ALWAYS, the
638 * scrollbar is always present; if %GTK_POLICY_NEVER, the scrollbar is
639 * never present; if %GTK_POLICY_AUTOMATIC, the scrollbar is present only
640 * if needed (that is, if the slider part of the bar would be smaller
641 * than the trough - the display is larger than the page size).
644 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
645 GtkPolicyType hscrollbar_policy,
646 GtkPolicyType vscrollbar_policy)
648 GObject *object = G_OBJECT (scrolled_window);
650 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
652 if ((scrolled_window->hscrollbar_policy != hscrollbar_policy) ||
653 (scrolled_window->vscrollbar_policy != vscrollbar_policy))
655 scrolled_window->hscrollbar_policy = hscrollbar_policy;
656 scrolled_window->vscrollbar_policy = vscrollbar_policy;
658 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
660 g_object_freeze_notify (object);
661 g_object_notify (object, "hscrollbar-policy");
662 g_object_notify (object, "vscrollbar-policy");
663 g_object_thaw_notify (object);
668 * gtk_scrolled_window_get_policy:
669 * @scrolled_window: a #GtkScrolledWindow
670 * @hscrollbar_policy: location to store the policy for the horizontal
671 * scrollbar, or %NULL.
672 * @vscrollbar_policy: location to store the policy for the vertical
673 * scrollbar, or %NULL.
675 * Retrieves the current policy values for the horizontal and vertical
676 * scrollbars. See gtk_scrolled_window_set_policy().
679 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
680 GtkPolicyType *hscrollbar_policy,
681 GtkPolicyType *vscrollbar_policy)
683 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
685 if (hscrollbar_policy)
686 *hscrollbar_policy = scrolled_window->hscrollbar_policy;
687 if (vscrollbar_policy)
688 *vscrollbar_policy = scrolled_window->vscrollbar_policy;
692 gtk_scrolled_window_update_real_placement (GtkScrolledWindow *scrolled_window)
694 GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
695 GtkSettings *settings;
697 settings = gtk_widget_get_settings (GTK_WIDGET (scrolled_window));
699 if (priv->window_placement_set || settings == NULL)
700 priv->real_window_placement = scrolled_window->window_placement;
702 g_object_get (settings,
703 "gtk-scrolled-window-placement",
704 &priv->real_window_placement,
709 gtk_scrolled_window_set_placement_internal (GtkScrolledWindow *scrolled_window,
710 GtkCornerType window_placement)
712 if (scrolled_window->window_placement != window_placement)
714 scrolled_window->window_placement = window_placement;
716 gtk_scrolled_window_update_real_placement (scrolled_window);
717 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
719 g_object_notify (G_OBJECT (scrolled_window), "window-placement");
724 gtk_scrolled_window_set_placement_set (GtkScrolledWindow *scrolled_window,
725 gboolean placement_set,
726 gboolean emit_resize)
728 GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
730 if (priv->window_placement_set != placement_set)
732 priv->window_placement_set = placement_set;
734 gtk_scrolled_window_update_real_placement (scrolled_window);
736 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
738 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
743 * gtk_scrolled_window_set_placement:
744 * @scrolled_window: a #GtkScrolledWindow
745 * @window_placement: position of the child window
747 * Sets the placement of the contents with respect to the scrollbars
748 * for the scrolled window.
750 * The default is %GTK_CORNER_TOP_LEFT, meaning the child is
751 * in the top left, with the scrollbars underneath and to the right.
752 * Other values in #GtkCornerType are %GTK_CORNER_TOP_RIGHT,
753 * %GTK_CORNER_BOTTOM_LEFT, and %GTK_CORNER_BOTTOM_RIGHT.
755 * See also gtk_scrolled_window_get_placement() and
756 * gtk_scrolled_window_unset_placement().
759 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
760 GtkCornerType window_placement)
762 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
764 gtk_scrolled_window_set_placement_set (scrolled_window, TRUE, FALSE);
765 gtk_scrolled_window_set_placement_internal (scrolled_window, window_placement);
769 * gtk_scrolled_window_get_placement:
770 * @scrolled_window: a #GtkScrolledWindow
772 * Gets the placement of the contents with respect to the scrollbars
773 * for the scrolled window. See gtk_scrolled_window_set_placement().
775 * Return value: the current placement value.
777 * See also gtk_scrolled_window_set_placement() and
778 * gtk_scrolled_window_unset_placement().
781 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
783 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
785 return scrolled_window->window_placement;
789 * gtk_scrolled_window_unset_placement:
790 * @scrolled_window: a #GtkScrolledWindow
792 * Unsets the placement of the contents with respect to the scrollbars
793 * for the scrolled window. If no window placement is set for a scrolled
794 * window, it obeys the "gtk-scrolled-window-placement" XSETTING.
796 * See also gtk_scrolled_window_set_placement() and
797 * gtk_scrolled_window_get_placement().
802 gtk_scrolled_window_unset_placement (GtkScrolledWindow *scrolled_window)
804 GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
806 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
808 if (priv->window_placement_set)
810 priv->window_placement_set = FALSE;
812 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
814 g_object_notify (G_OBJECT (scrolled_window), "window-placement-set");
819 * gtk_scrolled_window_set_shadow_type:
820 * @scrolled_window: a #GtkScrolledWindow
821 * @type: kind of shadow to draw around scrolled window contents
823 * Changes the type of shadow drawn around the contents of
828 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
831 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
832 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
834 if (scrolled_window->shadow_type != type)
836 scrolled_window->shadow_type = type;
838 if (gtk_widget_is_drawable (GTK_WIDGET (scrolled_window)))
839 gtk_widget_queue_draw (GTK_WIDGET (scrolled_window));
841 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
843 g_object_notify (G_OBJECT (scrolled_window), "shadow-type");
848 * gtk_scrolled_window_get_shadow_type:
849 * @scrolled_window: a #GtkScrolledWindow
851 * Gets the shadow type of the scrolled window. See
852 * gtk_scrolled_window_set_shadow_type().
854 * Return value: the current shadow type
857 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
859 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
861 return scrolled_window->shadow_type;
865 gtk_scrolled_window_destroy (GtkObject *object)
867 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
869 if (scrolled_window->hscrollbar)
871 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
872 gtk_scrolled_window_adjustment_changed,
874 gtk_widget_unparent (scrolled_window->hscrollbar);
875 gtk_widget_destroy (scrolled_window->hscrollbar);
876 g_object_unref (scrolled_window->hscrollbar);
877 scrolled_window->hscrollbar = NULL;
879 if (scrolled_window->vscrollbar)
881 g_signal_handlers_disconnect_by_func (gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)),
882 gtk_scrolled_window_adjustment_changed,
884 gtk_widget_unparent (scrolled_window->vscrollbar);
885 gtk_widget_destroy (scrolled_window->vscrollbar);
886 g_object_unref (scrolled_window->vscrollbar);
887 scrolled_window->vscrollbar = NULL;
890 GTK_OBJECT_CLASS (gtk_scrolled_window_parent_class)->destroy (object);
894 gtk_scrolled_window_set_property (GObject *object,
899 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
903 case PROP_HADJUSTMENT:
904 gtk_scrolled_window_set_hadjustment (scrolled_window,
905 g_value_get_object (value));
907 case PROP_VADJUSTMENT:
908 gtk_scrolled_window_set_vadjustment (scrolled_window,
909 g_value_get_object (value));
911 case PROP_HSCROLLBAR_POLICY:
912 gtk_scrolled_window_set_policy (scrolled_window,
913 g_value_get_enum (value),
914 scrolled_window->vscrollbar_policy);
916 case PROP_VSCROLLBAR_POLICY:
917 gtk_scrolled_window_set_policy (scrolled_window,
918 scrolled_window->hscrollbar_policy,
919 g_value_get_enum (value));
921 case PROP_WINDOW_PLACEMENT:
922 gtk_scrolled_window_set_placement_internal (scrolled_window,
923 g_value_get_enum (value));
925 case PROP_WINDOW_PLACEMENT_SET:
926 gtk_scrolled_window_set_placement_set (scrolled_window,
927 g_value_get_boolean (value),
930 case PROP_SHADOW_TYPE:
931 gtk_scrolled_window_set_shadow_type (scrolled_window,
932 g_value_get_enum (value));
935 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
941 gtk_scrolled_window_get_property (GObject *object,
946 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
947 GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
951 case PROP_HADJUSTMENT:
952 g_value_set_object (value,
953 G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
955 case PROP_VADJUSTMENT:
956 g_value_set_object (value,
957 G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
959 case PROP_HSCROLLBAR_POLICY:
960 g_value_set_enum (value, scrolled_window->hscrollbar_policy);
962 case PROP_VSCROLLBAR_POLICY:
963 g_value_set_enum (value, scrolled_window->vscrollbar_policy);
965 case PROP_WINDOW_PLACEMENT:
966 g_value_set_enum (value, scrolled_window->window_placement);
968 case PROP_WINDOW_PLACEMENT_SET:
969 g_value_set_boolean (value, priv->window_placement_set);
971 case PROP_SHADOW_TYPE:
972 g_value_set_enum (value, scrolled_window->shadow_type);
975 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
981 traverse_container (GtkWidget *widget,
984 if (GTK_IS_SCROLLED_WINDOW (widget))
986 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
987 gtk_widget_queue_resize (widget);
989 else if (GTK_IS_CONTAINER (widget))
990 gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
994 gtk_scrolled_window_settings_changed (GtkSettings *settings)
998 list = gtk_window_list_toplevels ();
1000 for (l = list; l; l = l->next)
1001 gtk_container_forall (GTK_CONTAINER (l->data),
1002 traverse_container, NULL);
1008 gtk_scrolled_window_screen_changed (GtkWidget *widget,
1009 GdkScreen *previous_screen)
1011 GtkSettings *settings;
1012 guint window_placement_connection;
1014 gtk_scrolled_window_update_real_placement (GTK_SCROLLED_WINDOW (widget));
1016 if (!gtk_widget_has_screen (widget))
1019 settings = gtk_widget_get_settings (widget);
1021 window_placement_connection =
1022 GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings),
1023 "gtk-scrolled-window-connection"));
1025 if (window_placement_connection)
1028 window_placement_connection =
1029 g_signal_connect (settings, "notify::gtk-scrolled-window-placement",
1030 G_CALLBACK (gtk_scrolled_window_settings_changed), NULL);
1031 g_object_set_data (G_OBJECT (settings),
1032 I_("gtk-scrolled-window-connection"),
1033 GUINT_TO_POINTER (window_placement_connection));
1037 gtk_scrolled_window_paint (GtkWidget *widget,
1040 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1042 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1044 GtkAllocation relative_allocation;
1045 gboolean scrollbars_within_bevel;
1047 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1049 if (!scrollbars_within_bevel)
1051 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1053 relative_allocation.x -= widget->style->xthickness;
1054 relative_allocation.y -= widget->style->ythickness;
1055 relative_allocation.width += 2 * widget->style->xthickness;
1056 relative_allocation.height += 2 * widget->style->ythickness;
1060 GtkContainer *container = GTK_CONTAINER (widget);
1062 relative_allocation.x = container->border_width;
1063 relative_allocation.y = container->border_width;
1064 relative_allocation.width = widget->allocation.width - 2 * container->border_width;
1065 relative_allocation.height = widget->allocation.height - 2 * container->border_width;
1068 gtk_paint_shadow (widget->style, widget->window,
1069 GTK_STATE_NORMAL, scrolled_window->shadow_type,
1070 area, widget, "scrolled_window",
1071 widget->allocation.x + relative_allocation.x,
1072 widget->allocation.y + relative_allocation.y,
1073 relative_allocation.width,
1074 relative_allocation.height);
1079 gtk_scrolled_window_expose (GtkWidget *widget,
1080 GdkEventExpose *event)
1082 if (gtk_widget_is_drawable (widget))
1084 gtk_scrolled_window_paint (widget, &event->area);
1086 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->expose_event (widget, event);
1093 gtk_scrolled_window_forall (GtkContainer *container,
1094 gboolean include_internals,
1095 GtkCallback callback,
1096 gpointer callback_data)
1098 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1099 g_return_if_fail (callback != NULL);
1101 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1105 if (include_internals)
1107 GtkScrolledWindow *scrolled_window;
1109 scrolled_window = GTK_SCROLLED_WINDOW (container);
1111 if (scrolled_window->vscrollbar)
1112 callback (scrolled_window->vscrollbar, callback_data);
1113 if (scrolled_window->hscrollbar)
1114 callback (scrolled_window->hscrollbar, callback_data);
1119 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1120 GtkScrollType scroll,
1121 gboolean horizontal)
1123 GtkAdjustment *adjustment = NULL;
1127 case GTK_SCROLL_STEP_UP:
1128 scroll = GTK_SCROLL_STEP_BACKWARD;
1131 case GTK_SCROLL_STEP_DOWN:
1132 scroll = GTK_SCROLL_STEP_FORWARD;
1135 case GTK_SCROLL_STEP_LEFT:
1136 scroll = GTK_SCROLL_STEP_BACKWARD;
1139 case GTK_SCROLL_STEP_RIGHT:
1140 scroll = GTK_SCROLL_STEP_FORWARD;
1143 case GTK_SCROLL_PAGE_UP:
1144 scroll = GTK_SCROLL_PAGE_BACKWARD;
1147 case GTK_SCROLL_PAGE_DOWN:
1148 scroll = GTK_SCROLL_PAGE_FORWARD;
1151 case GTK_SCROLL_PAGE_LEFT:
1152 scroll = GTK_SCROLL_STEP_BACKWARD;
1155 case GTK_SCROLL_PAGE_RIGHT:
1156 scroll = GTK_SCROLL_STEP_FORWARD;
1159 case GTK_SCROLL_STEP_BACKWARD:
1160 case GTK_SCROLL_STEP_FORWARD:
1161 case GTK_SCROLL_PAGE_BACKWARD:
1162 case GTK_SCROLL_PAGE_FORWARD:
1163 case GTK_SCROLL_START:
1164 case GTK_SCROLL_END:
1167 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1171 if ((horizontal && (!scrolled_window->hscrollbar || !scrolled_window->hscrollbar_visible)) ||
1172 (!horizontal && (!scrolled_window->vscrollbar || !scrolled_window->vscrollbar_visible)))
1177 if (scrolled_window->hscrollbar)
1178 adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
1182 if (scrolled_window->vscrollbar)
1183 adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
1188 gdouble value = adjustment->value;
1192 case GTK_SCROLL_STEP_FORWARD:
1193 value += adjustment->step_increment;
1195 case GTK_SCROLL_STEP_BACKWARD:
1196 value -= adjustment->step_increment;
1198 case GTK_SCROLL_PAGE_FORWARD:
1199 value += adjustment->page_increment;
1201 case GTK_SCROLL_PAGE_BACKWARD:
1202 value -= adjustment->page_increment;
1204 case GTK_SCROLL_START:
1205 value = adjustment->lower;
1207 case GTK_SCROLL_END:
1208 value = adjustment->upper;
1211 g_assert_not_reached ();
1215 gtk_adjustment_set_value (adjustment, value);
1224 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1225 GtkDirectionType direction_type)
1227 GtkWidget *toplevel;
1229 /* Focus out of the scrolled window entirely. We do this by setting
1230 * a flag, then propagating the focus motion to the notebook.
1232 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1233 if (!gtk_widget_is_toplevel (toplevel))
1236 g_object_ref (scrolled_window);
1238 scrolled_window->focus_out = TRUE;
1239 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1240 scrolled_window->focus_out = FALSE;
1242 g_object_unref (scrolled_window);
1246 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1247 GtkAllocation *allocation)
1249 GtkScrolledWindow *scrolled_window;
1250 GtkScrolledWindowPrivate *priv;
1251 gint scrollbar_spacing;
1253 g_return_if_fail (widget != NULL);
1254 g_return_if_fail (allocation != NULL);
1256 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1257 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1259 priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
1261 allocation->x = GTK_CONTAINER (widget)->border_width;
1262 allocation->y = GTK_CONTAINER (widget)->border_width;
1264 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1266 allocation->x += widget->style->xthickness;
1267 allocation->y += widget->style->ythickness;
1270 allocation->width = MAX (1, (gint)widget->allocation.width - allocation->x * 2);
1271 allocation->height = MAX (1, (gint)widget->allocation.height - allocation->y * 2);
1273 if (scrolled_window->vscrollbar_visible)
1275 GtkRequisition vscrollbar_requisition;
1278 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
1279 &vscrollbar_requisition);
1280 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1283 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1284 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1286 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1287 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1288 allocation->x += (vscrollbar_requisition.width + scrollbar_spacing);
1290 allocation->width = MAX (1, allocation->width - (vscrollbar_requisition.width + scrollbar_spacing));
1292 if (scrolled_window->hscrollbar_visible)
1294 GtkRequisition hscrollbar_requisition;
1295 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
1296 &hscrollbar_requisition);
1298 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1299 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1300 allocation->y += (hscrollbar_requisition.height + scrollbar_spacing);
1302 allocation->height = MAX (1, allocation->height - (hscrollbar_requisition.height + scrollbar_spacing));
1307 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1308 GtkAllocation *allocation)
1310 GtkScrolledWindow *scrolled_window;
1311 GtkScrolledWindowPrivate *priv;
1313 GtkAllocation relative_allocation;
1314 GtkAllocation child_allocation;
1315 gboolean scrollbars_within_bevel;
1316 gint scrollbar_spacing;
1318 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1319 g_return_if_fail (allocation != NULL);
1321 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1322 bin = GTK_BIN (scrolled_window);
1324 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1325 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1327 priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
1329 widget->allocation = *allocation;
1331 if (scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1332 scrolled_window->hscrollbar_visible = TRUE;
1333 else if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
1334 scrolled_window->hscrollbar_visible = FALSE;
1335 if (scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1336 scrolled_window->vscrollbar_visible = TRUE;
1337 else if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
1338 scrolled_window->vscrollbar_visible = FALSE;
1340 if (bin->child && gtk_widget_get_visible (bin->child))
1342 gboolean previous_hvis;
1343 gboolean previous_vvis;
1348 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1350 child_allocation.x = relative_allocation.x + allocation->x;
1351 child_allocation.y = relative_allocation.y + allocation->y;
1352 child_allocation.width = relative_allocation.width;
1353 child_allocation.height = relative_allocation.height;
1355 previous_hvis = scrolled_window->hscrollbar_visible;
1356 previous_vvis = scrolled_window->vscrollbar_visible;
1358 gtk_widget_size_allocate (bin->child, &child_allocation);
1360 /* If, after the first iteration, the hscrollbar and the
1361 * vscrollbar flip visiblity, then we need both.
1364 previous_hvis != scrolled_window->hscrollbar_visible &&
1365 previous_vvis != scrolled_window->vscrollbar_visible)
1367 scrolled_window->hscrollbar_visible = TRUE;
1368 scrolled_window->vscrollbar_visible = TRUE;
1370 /* a new resize is already queued at this point,
1371 * so we will immediatedly get reinvoked
1378 while (previous_hvis != scrolled_window->hscrollbar_visible ||
1379 previous_vvis != scrolled_window->vscrollbar_visible);
1383 scrolled_window->hscrollbar_visible = scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS;
1384 scrolled_window->vscrollbar_visible = scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS;
1385 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1388 if (scrolled_window->hscrollbar_visible)
1390 GtkRequisition hscrollbar_requisition;
1391 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
1392 &hscrollbar_requisition);
1394 if (!gtk_widget_get_visible (scrolled_window->hscrollbar))
1395 gtk_widget_show (scrolled_window->hscrollbar);
1397 child_allocation.x = relative_allocation.x;
1398 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1399 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1400 child_allocation.y = (relative_allocation.y +
1401 relative_allocation.height +
1403 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
1404 0 : widget->style->ythickness));
1406 child_allocation.y = GTK_CONTAINER (scrolled_window)->border_width;
1408 child_allocation.width = relative_allocation.width;
1409 child_allocation.height = hscrollbar_requisition.height;
1410 child_allocation.x += allocation->x;
1411 child_allocation.y += allocation->y;
1413 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1415 if (!scrollbars_within_bevel)
1417 child_allocation.x -= widget->style->xthickness;
1418 child_allocation.width += 2 * widget->style->xthickness;
1420 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1421 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1423 child_allocation.y -= widget->style->ythickness;
1427 child_allocation.y += widget->style->ythickness;
1431 gtk_widget_size_allocate (scrolled_window->hscrollbar, &child_allocation);
1433 else if (gtk_widget_get_visible (scrolled_window->hscrollbar))
1434 gtk_widget_hide (scrolled_window->hscrollbar);
1436 if (scrolled_window->vscrollbar_visible)
1438 GtkRequisition vscrollbar_requisition;
1439 if (!gtk_widget_get_visible (scrolled_window->vscrollbar))
1440 gtk_widget_show (scrolled_window->vscrollbar);
1442 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
1443 &vscrollbar_requisition);
1445 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1446 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1447 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1448 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1449 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1450 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1451 child_allocation.x = (relative_allocation.x +
1452 relative_allocation.width +
1454 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
1455 0 : widget->style->xthickness));
1457 child_allocation.x = GTK_CONTAINER (scrolled_window)->border_width;
1459 child_allocation.y = relative_allocation.y;
1460 child_allocation.width = vscrollbar_requisition.width;
1461 child_allocation.height = relative_allocation.height;
1462 child_allocation.x += allocation->x;
1463 child_allocation.y += allocation->y;
1465 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1467 if (!scrollbars_within_bevel)
1469 child_allocation.y -= widget->style->ythickness;
1470 child_allocation.height += 2 * widget->style->ythickness;
1472 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1473 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1475 child_allocation.x -= widget->style->xthickness;
1479 child_allocation.x += widget->style->xthickness;
1483 gtk_widget_size_allocate (scrolled_window->vscrollbar, &child_allocation);
1485 else if (gtk_widget_get_visible (scrolled_window->vscrollbar))
1486 gtk_widget_hide (scrolled_window->vscrollbar);
1490 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1491 GdkEventScroll *event)
1495 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1496 g_return_val_if_fail (event != NULL, FALSE);
1498 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1499 range = GTK_SCROLLED_WINDOW (widget)->vscrollbar;
1501 range = GTK_SCROLLED_WINDOW (widget)->hscrollbar;
1503 if (range && gtk_widget_get_visible (range))
1505 GtkAdjustment *adj = GTK_RANGE (range)->adjustment;
1508 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1510 gtk_adjustment_set_value (adj, adj->value + delta);
1519 gtk_scrolled_window_focus (GtkWidget *widget,
1520 GtkDirectionType direction)
1522 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1523 gboolean had_focus_child = GTK_CONTAINER (widget)->focus_child != NULL;
1525 if (scrolled_window->focus_out)
1527 scrolled_window->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1531 if (gtk_widget_is_focus (widget))
1534 /* We only put the scrolled window itself in the focus chain if it
1535 * isn't possible to focus any children.
1537 if (GTK_BIN (widget)->child)
1539 if (gtk_widget_child_focus (GTK_BIN (widget)->child, direction))
1543 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1545 gtk_widget_grab_focus (widget);
1553 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1556 GtkScrolledWindow *scrolled_win;
1558 g_return_if_fail (adjustment != NULL);
1559 g_return_if_fail (data != NULL);
1561 scrolled_win = GTK_SCROLLED_WINDOW (data);
1563 if (scrolled_win->hscrollbar &&
1564 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->hscrollbar)))
1566 if (scrolled_win->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1570 visible = scrolled_win->hscrollbar_visible;
1571 scrolled_win->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1572 adjustment->page_size);
1573 if (scrolled_win->hscrollbar_visible != visible)
1574 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1577 else if (scrolled_win->vscrollbar &&
1578 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->vscrollbar)))
1580 if (scrolled_win->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1584 visible = scrolled_win->vscrollbar_visible;
1585 scrolled_win->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1586 adjustment->page_size);
1587 if (scrolled_win->vscrollbar_visible != visible)
1588 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1594 gtk_scrolled_window_add (GtkContainer *container,
1597 GtkScrolledWindow *scrolled_window;
1600 bin = GTK_BIN (container);
1601 g_return_if_fail (bin->child == NULL);
1603 scrolled_window = GTK_SCROLLED_WINDOW (container);
1606 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1608 /* this is a temporary message */
1609 if (!gtk_widget_set_scroll_adjustments (child,
1610 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
1611 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar))))
1612 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1613 "use gtk_scrolled_window_add_with_viewport() instead");
1617 gtk_scrolled_window_remove (GtkContainer *container,
1620 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1621 g_return_if_fail (child != NULL);
1622 g_return_if_fail (GTK_BIN (container)->child == child);
1624 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1626 /* chain parent class handler to remove child */
1627 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1631 * gtk_scrolled_window_add_with_viewport:
1632 * @scrolled_window: a #GtkScrolledWindow
1633 * @child: the widget you want to scroll
1635 * Used to add children without native scrolling capabilities. This
1636 * is simply a convenience function; it is equivalent to adding the
1637 * unscrollable child to a viewport, then adding the viewport to the
1638 * scrolled window. If a child has native scrolling, use
1639 * gtk_container_add() instead of this function.
1641 * The viewport scrolls the child by moving its #GdkWindow, and takes
1642 * the size of the child to be the size of its toplevel #GdkWindow.
1643 * This will be very wrong for most widgets that support native scrolling;
1644 * for example, if you add a widget such as #GtkTreeView with a viewport,
1645 * the whole widget will scroll, including the column headings. Thus,
1646 * widgets with native scrolling support should not be used with the
1647 * #GtkViewport proxy.
1649 * A widget supports scrolling natively if the
1650 * set_scroll_adjustments_signal field in #GtkWidgetClass is non-zero,
1651 * i.e. has been filled in with a valid signal identifier.
1654 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1658 GtkWidget *viewport;
1660 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1661 g_return_if_fail (GTK_IS_WIDGET (child));
1662 g_return_if_fail (child->parent == NULL);
1664 bin = GTK_BIN (scrolled_window);
1666 if (bin->child != NULL)
1668 g_return_if_fail (GTK_IS_VIEWPORT (bin->child));
1669 g_return_if_fail (GTK_BIN (bin->child)->child == NULL);
1671 viewport = bin->child;
1676 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1677 gtk_scrolled_window_get_vadjustment (scrolled_window));
1678 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1681 gtk_widget_show (viewport);
1682 gtk_container_add (GTK_CONTAINER (viewport), child);
1686 * _gtk_scrolled_window_get_spacing:
1687 * @scrolled_window: a scrolled window
1689 * Gets the spacing between the scrolled window's scrollbars and
1690 * the scrolled widget. Used by GtkCombo
1692 * Return value: the spacing, in pixels.
1695 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
1697 GtkScrolledWindowClass *class;
1699 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
1701 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
1703 if (class->scrollbar_spacing >= 0)
1704 return class->scrollbar_spacing;
1707 gint scrollbar_spacing;
1709 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
1710 "scrollbar-spacing", &scrollbar_spacing,
1713 return scrollbar_spacing;
1719 gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface)
1721 iface->get_width = gtk_scrolled_window_get_width;
1722 iface->get_height = gtk_scrolled_window_get_height;
1723 iface->get_height_for_width = gtk_scrolled_window_get_height_for_width;
1724 iface->get_width_for_height = gtk_scrolled_window_get_width_for_height;
1728 gtk_scrolled_window_get_size (GtkSizeRequest *widget,
1729 GtkOrientation orientation,
1733 GtkScrolledWindow *scrolled_window;
1737 gint scrollbar_spacing;
1738 GtkRequisition hscrollbar_requisition;
1739 GtkRequisition vscrollbar_requisition;
1740 GtkRequisition minimum_req, natural_req;
1741 gint min_child_size, nat_child_size;
1743 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1744 bin = GTK_BIN (scrolled_window);
1746 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1750 minimum_req.width = 0;
1751 minimum_req.height = 0;
1752 natural_req.width = 0;
1753 natural_req.height = 0;
1755 gtk_widget_size_request (scrolled_window->hscrollbar,
1756 &hscrollbar_requisition);
1757 gtk_widget_size_request (scrolled_window->vscrollbar,
1758 &vscrollbar_requisition);
1760 if (bin->child && gtk_widget_get_visible (bin->child))
1762 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1764 gtk_size_request_get_width (GTK_SIZE_REQUEST (bin->child),
1768 if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
1770 minimum_req.width += min_child_size;
1771 natural_req.width += nat_child_size;
1775 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (bin->child, FALSE);
1777 if (aux_info && aux_info->width > 0)
1779 minimum_req.width += aux_info->width;
1780 natural_req.width += aux_info->width;
1785 minimum_req.width += vscrollbar_requisition.width;
1786 natural_req.width += vscrollbar_requisition.width;
1790 else /* GTK_ORIENTATION_VERTICAL */
1792 gtk_size_request_get_height (GTK_SIZE_REQUEST (bin->child),
1796 if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
1798 minimum_req.height += min_child_size;
1799 natural_req.height += nat_child_size;
1803 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (bin->child, FALSE);
1805 if (aux_info && aux_info->height > 0)
1807 minimum_req.height += aux_info->height;
1808 natural_req.height += aux_info->height;
1813 minimum_req.height += hscrollbar_requisition.height;
1814 natural_req.height += hscrollbar_requisition.height;
1820 if (scrolled_window->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1821 scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1823 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
1824 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
1825 if (!extra_height || scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1826 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
1829 if (scrolled_window->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1830 scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1832 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
1833 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
1834 if (!extra_height || scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1835 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
1838 minimum_req.width += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_width);
1839 minimum_req.height += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_height);
1840 natural_req.width += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_width);
1841 natural_req.height += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_height);
1843 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1845 minimum_req.width += 2 * GTK_WIDGET (widget)->style->xthickness;
1846 minimum_req.height += 2 * GTK_WIDGET (widget)->style->ythickness;
1847 natural_req.width += 2 * GTK_WIDGET (widget)->style->xthickness;
1848 natural_req.height += 2 * GTK_WIDGET (widget)->style->ythickness;
1851 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1854 *minimum_size = minimum_req.width;
1856 *natural_size = natural_req.width;
1861 *minimum_size = minimum_req.height;
1863 *natural_size = natural_req.height;
1868 gtk_scrolled_window_get_width (GtkSizeRequest *widget,
1872 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1876 gtk_scrolled_window_get_height (GtkSizeRequest *widget,
1880 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1884 gtk_scrolled_window_get_height_for_width (GtkSizeRequest *widget,
1886 gint *minimum_height,
1887 gint *natural_height)
1889 g_return_if_fail (GTK_IS_WIDGET (widget));
1891 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
1895 gtk_scrolled_window_get_width_for_height (GtkSizeRequest *widget,
1897 gint *minimum_width,
1898 gint *natural_width)
1900 g_return_if_fail (GTK_IS_WIDGET (widget));
1902 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);