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);
1063 border_width = gtk_container_get_border_width (container);
1065 relative_allocation.x = border_width;
1066 relative_allocation.y = border_width;
1067 relative_allocation.width = widget->allocation.width - 2 * border_width;
1068 relative_allocation.height = widget->allocation.height - 2 * border_width;
1071 gtk_paint_shadow (widget->style, widget->window,
1072 GTK_STATE_NORMAL, scrolled_window->shadow_type,
1073 area, widget, "scrolled_window",
1074 widget->allocation.x + relative_allocation.x,
1075 widget->allocation.y + relative_allocation.y,
1076 relative_allocation.width,
1077 relative_allocation.height);
1082 gtk_scrolled_window_expose (GtkWidget *widget,
1083 GdkEventExpose *event)
1085 if (gtk_widget_is_drawable (widget))
1087 gtk_scrolled_window_paint (widget, &event->area);
1089 GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->expose_event (widget, event);
1096 gtk_scrolled_window_forall (GtkContainer *container,
1097 gboolean include_internals,
1098 GtkCallback callback,
1099 gpointer callback_data)
1101 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1102 g_return_if_fail (callback != NULL);
1104 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->forall (container,
1108 if (include_internals)
1110 GtkScrolledWindow *scrolled_window;
1112 scrolled_window = GTK_SCROLLED_WINDOW (container);
1114 if (scrolled_window->vscrollbar)
1115 callback (scrolled_window->vscrollbar, callback_data);
1116 if (scrolled_window->hscrollbar)
1117 callback (scrolled_window->hscrollbar, callback_data);
1122 gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
1123 GtkScrollType scroll,
1124 gboolean horizontal)
1126 GtkAdjustment *adjustment = NULL;
1130 case GTK_SCROLL_STEP_UP:
1131 scroll = GTK_SCROLL_STEP_BACKWARD;
1134 case GTK_SCROLL_STEP_DOWN:
1135 scroll = GTK_SCROLL_STEP_FORWARD;
1138 case GTK_SCROLL_STEP_LEFT:
1139 scroll = GTK_SCROLL_STEP_BACKWARD;
1142 case GTK_SCROLL_STEP_RIGHT:
1143 scroll = GTK_SCROLL_STEP_FORWARD;
1146 case GTK_SCROLL_PAGE_UP:
1147 scroll = GTK_SCROLL_PAGE_BACKWARD;
1150 case GTK_SCROLL_PAGE_DOWN:
1151 scroll = GTK_SCROLL_PAGE_FORWARD;
1154 case GTK_SCROLL_PAGE_LEFT:
1155 scroll = GTK_SCROLL_STEP_BACKWARD;
1158 case GTK_SCROLL_PAGE_RIGHT:
1159 scroll = GTK_SCROLL_STEP_FORWARD;
1162 case GTK_SCROLL_STEP_BACKWARD:
1163 case GTK_SCROLL_STEP_FORWARD:
1164 case GTK_SCROLL_PAGE_BACKWARD:
1165 case GTK_SCROLL_PAGE_FORWARD:
1166 case GTK_SCROLL_START:
1167 case GTK_SCROLL_END:
1170 g_warning ("Invalid scroll type %u for GtkScrolledWindow::scroll-child", scroll);
1174 if ((horizontal && (!scrolled_window->hscrollbar || !scrolled_window->hscrollbar_visible)) ||
1175 (!horizontal && (!scrolled_window->vscrollbar || !scrolled_window->vscrollbar_visible)))
1180 if (scrolled_window->hscrollbar)
1181 adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
1185 if (scrolled_window->vscrollbar)
1186 adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
1191 gdouble value = adjustment->value;
1195 case GTK_SCROLL_STEP_FORWARD:
1196 value += adjustment->step_increment;
1198 case GTK_SCROLL_STEP_BACKWARD:
1199 value -= adjustment->step_increment;
1201 case GTK_SCROLL_PAGE_FORWARD:
1202 value += adjustment->page_increment;
1204 case GTK_SCROLL_PAGE_BACKWARD:
1205 value -= adjustment->page_increment;
1207 case GTK_SCROLL_START:
1208 value = adjustment->lower;
1210 case GTK_SCROLL_END:
1211 value = adjustment->upper;
1214 g_assert_not_reached ();
1218 gtk_adjustment_set_value (adjustment, value);
1227 gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
1228 GtkDirectionType direction_type)
1230 GtkWidget *toplevel;
1232 /* Focus out of the scrolled window entirely. We do this by setting
1233 * a flag, then propagating the focus motion to the notebook.
1235 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
1236 if (!gtk_widget_is_toplevel (toplevel))
1239 g_object_ref (scrolled_window);
1241 scrolled_window->focus_out = TRUE;
1242 g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1243 scrolled_window->focus_out = FALSE;
1245 g_object_unref (scrolled_window);
1249 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
1250 GtkAllocation *allocation)
1252 GtkScrolledWindow *scrolled_window;
1253 GtkScrolledWindowPrivate *priv;
1254 gint scrollbar_spacing;
1257 g_return_if_fail (widget != NULL);
1258 g_return_if_fail (allocation != NULL);
1260 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1261 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1263 priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
1265 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1266 allocation->x = border_width;
1267 allocation->y = border_width;
1269 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1271 allocation->x += widget->style->xthickness;
1272 allocation->y += widget->style->ythickness;
1275 allocation->width = MAX (1, (gint)widget->allocation.width - allocation->x * 2);
1276 allocation->height = MAX (1, (gint)widget->allocation.height - allocation->y * 2);
1278 if (scrolled_window->vscrollbar_visible)
1280 GtkRequisition vscrollbar_requisition;
1283 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
1284 &vscrollbar_requisition);
1285 is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1288 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1289 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1291 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1292 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1293 allocation->x += (vscrollbar_requisition.width + scrollbar_spacing);
1295 allocation->width = MAX (1, allocation->width - (vscrollbar_requisition.width + scrollbar_spacing));
1297 if (scrolled_window->hscrollbar_visible)
1299 GtkRequisition hscrollbar_requisition;
1300 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
1301 &hscrollbar_requisition);
1303 if (priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT ||
1304 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)
1305 allocation->y += (hscrollbar_requisition.height + scrollbar_spacing);
1307 allocation->height = MAX (1, allocation->height - (hscrollbar_requisition.height + scrollbar_spacing));
1312 gtk_scrolled_window_size_allocate (GtkWidget *widget,
1313 GtkAllocation *allocation)
1315 GtkScrolledWindow *scrolled_window;
1316 GtkScrolledWindowPrivate *priv;
1318 GtkAllocation relative_allocation;
1319 GtkAllocation child_allocation;
1320 gboolean scrollbars_within_bevel;
1321 gint scrollbar_spacing;
1324 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
1325 g_return_if_fail (allocation != NULL);
1327 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1328 bin = GTK_BIN (scrolled_window);
1330 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1331 gtk_widget_style_get (widget, "scrollbars-within-bevel", &scrollbars_within_bevel, NULL);
1333 priv = GTK_SCROLLED_WINDOW_GET_PRIVATE (scrolled_window);
1335 border_width = gtk_container_get_border_width (GTK_CONTAINER (scrolled_window));
1336 widget->allocation = *allocation;
1338 if (scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1339 scrolled_window->hscrollbar_visible = TRUE;
1340 else if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
1341 scrolled_window->hscrollbar_visible = FALSE;
1342 if (scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1343 scrolled_window->vscrollbar_visible = TRUE;
1344 else if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
1345 scrolled_window->vscrollbar_visible = FALSE;
1347 if (bin->child && gtk_widget_get_visible (bin->child))
1349 gboolean previous_hvis;
1350 gboolean previous_vvis;
1355 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1357 child_allocation.x = relative_allocation.x + allocation->x;
1358 child_allocation.y = relative_allocation.y + allocation->y;
1359 child_allocation.width = relative_allocation.width;
1360 child_allocation.height = relative_allocation.height;
1362 previous_hvis = scrolled_window->hscrollbar_visible;
1363 previous_vvis = scrolled_window->vscrollbar_visible;
1365 gtk_widget_size_allocate (bin->child, &child_allocation);
1367 /* If, after the first iteration, the hscrollbar and the
1368 * vscrollbar flip visiblity, then we need both.
1371 previous_hvis != scrolled_window->hscrollbar_visible &&
1372 previous_vvis != scrolled_window->vscrollbar_visible)
1374 scrolled_window->hscrollbar_visible = TRUE;
1375 scrolled_window->vscrollbar_visible = TRUE;
1377 /* a new resize is already queued at this point,
1378 * so we will immediatedly get reinvoked
1385 while (previous_hvis != scrolled_window->hscrollbar_visible ||
1386 previous_vvis != scrolled_window->vscrollbar_visible);
1390 scrolled_window->hscrollbar_visible = scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS;
1391 scrolled_window->vscrollbar_visible = scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS;
1392 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
1395 if (scrolled_window->hscrollbar_visible)
1397 GtkRequisition hscrollbar_requisition;
1398 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
1399 &hscrollbar_requisition);
1401 if (!gtk_widget_get_visible (scrolled_window->hscrollbar))
1402 gtk_widget_show (scrolled_window->hscrollbar);
1404 child_allocation.x = relative_allocation.x;
1405 if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1406 priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
1407 child_allocation.y = (relative_allocation.y +
1408 relative_allocation.height +
1410 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
1411 0 : widget->style->ythickness));
1413 child_allocation.y = border_width;
1415 child_allocation.width = relative_allocation.width;
1416 child_allocation.height = hscrollbar_requisition.height;
1417 child_allocation.x += allocation->x;
1418 child_allocation.y += allocation->y;
1420 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1422 if (!scrollbars_within_bevel)
1424 child_allocation.x -= widget->style->xthickness;
1425 child_allocation.width += 2 * widget->style->xthickness;
1427 else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
1428 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1430 child_allocation.y -= widget->style->ythickness;
1434 child_allocation.y += widget->style->ythickness;
1438 gtk_widget_size_allocate (scrolled_window->hscrollbar, &child_allocation);
1440 else if (gtk_widget_get_visible (scrolled_window->hscrollbar))
1441 gtk_widget_hide (scrolled_window->hscrollbar);
1443 if (scrolled_window->vscrollbar_visible)
1445 GtkRequisition vscrollbar_requisition;
1446 if (!gtk_widget_get_visible (scrolled_window->vscrollbar))
1447 gtk_widget_show (scrolled_window->vscrollbar);
1449 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
1450 &vscrollbar_requisition);
1452 if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
1453 (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
1454 priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
1455 (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
1456 (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
1457 priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
1458 child_allocation.x = (relative_allocation.x +
1459 relative_allocation.width +
1461 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
1462 0 : widget->style->xthickness));
1464 child_allocation.x = border_width;
1466 child_allocation.y = relative_allocation.y;
1467 child_allocation.width = vscrollbar_requisition.width;
1468 child_allocation.height = relative_allocation.height;
1469 child_allocation.x += allocation->x;
1470 child_allocation.y += allocation->y;
1472 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1474 if (!scrollbars_within_bevel)
1476 child_allocation.y -= widget->style->ythickness;
1477 child_allocation.height += 2 * widget->style->ythickness;
1479 else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
1480 GTK_CORNER_TOP_LEFT == priv->real_window_placement)
1482 child_allocation.x -= widget->style->xthickness;
1486 child_allocation.x += widget->style->xthickness;
1490 gtk_widget_size_allocate (scrolled_window->vscrollbar, &child_allocation);
1492 else if (gtk_widget_get_visible (scrolled_window->vscrollbar))
1493 gtk_widget_hide (scrolled_window->vscrollbar);
1497 gtk_scrolled_window_scroll_event (GtkWidget *widget,
1498 GdkEventScroll *event)
1502 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
1503 g_return_val_if_fail (event != NULL, FALSE);
1505 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
1506 range = GTK_SCROLLED_WINDOW (widget)->vscrollbar;
1508 range = GTK_SCROLLED_WINDOW (widget)->hscrollbar;
1510 if (range && gtk_widget_get_visible (range))
1512 GtkAdjustment *adj = GTK_RANGE (range)->adjustment;
1515 delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
1517 gtk_adjustment_set_value (adj, adj->value + delta);
1526 gtk_scrolled_window_focus (GtkWidget *widget,
1527 GtkDirectionType direction)
1529 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
1530 gboolean had_focus_child;
1532 had_focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL;
1534 if (scrolled_window->focus_out)
1536 scrolled_window->focus_out = FALSE; /* Clear this to catch the wrap-around case */
1540 if (gtk_widget_is_focus (widget))
1543 /* We only put the scrolled window itself in the focus chain if it
1544 * isn't possible to focus any children.
1546 if (GTK_BIN (widget)->child)
1548 if (gtk_widget_child_focus (GTK_BIN (widget)->child, direction))
1552 if (!had_focus_child && gtk_widget_get_can_focus (widget))
1554 gtk_widget_grab_focus (widget);
1562 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
1565 GtkScrolledWindow *scrolled_win;
1567 g_return_if_fail (adjustment != NULL);
1568 g_return_if_fail (data != NULL);
1570 scrolled_win = GTK_SCROLLED_WINDOW (data);
1572 if (scrolled_win->hscrollbar &&
1573 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->hscrollbar)))
1575 if (scrolled_win->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
1579 visible = scrolled_win->hscrollbar_visible;
1580 scrolled_win->hscrollbar_visible = (adjustment->upper - adjustment->lower >
1581 adjustment->page_size);
1582 if (scrolled_win->hscrollbar_visible != visible)
1583 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1586 else if (scrolled_win->vscrollbar &&
1587 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->vscrollbar)))
1589 if (scrolled_win->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
1593 visible = scrolled_win->vscrollbar_visible;
1594 scrolled_win->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1595 adjustment->page_size);
1596 if (scrolled_win->vscrollbar_visible != visible)
1597 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1603 gtk_scrolled_window_add (GtkContainer *container,
1606 GtkScrolledWindow *scrolled_window;
1609 bin = GTK_BIN (container);
1610 g_return_if_fail (bin->child == NULL);
1612 scrolled_window = GTK_SCROLLED_WINDOW (container);
1615 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1617 /* this is a temporary message */
1618 if (!gtk_widget_set_scroll_adjustments (child,
1619 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
1620 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar))))
1621 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1622 "use gtk_scrolled_window_add_with_viewport() instead");
1626 gtk_scrolled_window_remove (GtkContainer *container,
1629 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1630 g_return_if_fail (child != NULL);
1631 g_return_if_fail (GTK_BIN (container)->child == child);
1633 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1635 /* chain parent class handler to remove child */
1636 GTK_CONTAINER_CLASS (gtk_scrolled_window_parent_class)->remove (container, child);
1640 * gtk_scrolled_window_add_with_viewport:
1641 * @scrolled_window: a #GtkScrolledWindow
1642 * @child: the widget you want to scroll
1644 * Used to add children without native scrolling capabilities. This
1645 * is simply a convenience function; it is equivalent to adding the
1646 * unscrollable child to a viewport, then adding the viewport to the
1647 * scrolled window. If a child has native scrolling, use
1648 * gtk_container_add() instead of this function.
1650 * The viewport scrolls the child by moving its #GdkWindow, and takes
1651 * the size of the child to be the size of its toplevel #GdkWindow.
1652 * This will be very wrong for most widgets that support native scrolling;
1653 * for example, if you add a widget such as #GtkTreeView with a viewport,
1654 * the whole widget will scroll, including the column headings. Thus,
1655 * widgets with native scrolling support should not be used with the
1656 * #GtkViewport proxy.
1658 * A widget supports scrolling natively if the
1659 * set_scroll_adjustments_signal field in #GtkWidgetClass is non-zero,
1660 * i.e. has been filled in with a valid signal identifier.
1663 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1667 GtkWidget *viewport;
1669 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1670 g_return_if_fail (GTK_IS_WIDGET (child));
1671 g_return_if_fail (child->parent == NULL);
1673 bin = GTK_BIN (scrolled_window);
1675 if (bin->child != NULL)
1677 g_return_if_fail (GTK_IS_VIEWPORT (bin->child));
1678 g_return_if_fail (GTK_BIN (bin->child)->child == NULL);
1680 viewport = bin->child;
1685 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1686 gtk_scrolled_window_get_vadjustment (scrolled_window));
1687 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1690 gtk_widget_show (viewport);
1691 gtk_container_add (GTK_CONTAINER (viewport), child);
1695 * _gtk_scrolled_window_get_spacing:
1696 * @scrolled_window: a scrolled window
1698 * Gets the spacing between the scrolled window's scrollbars and
1699 * the scrolled widget. Used by GtkCombo
1701 * Return value: the spacing, in pixels.
1704 _gtk_scrolled_window_get_scrollbar_spacing (GtkScrolledWindow *scrolled_window)
1706 GtkScrolledWindowClass *class;
1708 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), 0);
1710 class = GTK_SCROLLED_WINDOW_GET_CLASS (scrolled_window);
1712 if (class->scrollbar_spacing >= 0)
1713 return class->scrollbar_spacing;
1716 gint scrollbar_spacing;
1718 gtk_widget_style_get (GTK_WIDGET (scrolled_window),
1719 "scrollbar-spacing", &scrollbar_spacing,
1722 return scrollbar_spacing;
1728 gtk_scrolled_window_size_request_init (GtkSizeRequestIface *iface)
1730 iface->get_width = gtk_scrolled_window_get_width;
1731 iface->get_height = gtk_scrolled_window_get_height;
1732 iface->get_height_for_width = gtk_scrolled_window_get_height_for_width;
1733 iface->get_width_for_height = gtk_scrolled_window_get_width_for_height;
1737 gtk_scrolled_window_get_size (GtkSizeRequest *widget,
1738 GtkOrientation orientation,
1742 GtkScrolledWindow *scrolled_window;
1746 gint scrollbar_spacing;
1747 GtkRequisition hscrollbar_requisition;
1748 GtkRequisition vscrollbar_requisition;
1749 GtkRequisition minimum_req, natural_req;
1750 gint min_child_size, nat_child_size;
1753 scrolled_window = GTK_SCROLLED_WINDOW (widget);
1754 bin = GTK_BIN (scrolled_window);
1756 scrollbar_spacing = _gtk_scrolled_window_get_scrollbar_spacing (scrolled_window);
1760 minimum_req.width = 0;
1761 minimum_req.height = 0;
1762 natural_req.width = 0;
1763 natural_req.height = 0;
1765 gtk_widget_size_request (scrolled_window->hscrollbar,
1766 &hscrollbar_requisition);
1767 gtk_widget_size_request (scrolled_window->vscrollbar,
1768 &vscrollbar_requisition);
1770 if (bin->child && gtk_widget_get_visible (bin->child))
1772 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1774 gtk_size_request_get_width (GTK_SIZE_REQUEST (bin->child),
1778 if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
1780 minimum_req.width += min_child_size;
1781 natural_req.width += nat_child_size;
1785 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (bin->child, FALSE);
1787 if (aux_info && aux_info->width > 0)
1789 minimum_req.width += aux_info->width;
1790 natural_req.width += aux_info->width;
1795 minimum_req.width += vscrollbar_requisition.width;
1796 natural_req.width += vscrollbar_requisition.width;
1800 else /* GTK_ORIENTATION_VERTICAL */
1802 gtk_size_request_get_height (GTK_SIZE_REQUEST (bin->child),
1806 if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
1808 minimum_req.height += min_child_size;
1809 natural_req.height += nat_child_size;
1813 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (bin->child, FALSE);
1815 if (aux_info && aux_info->height > 0)
1817 minimum_req.height += aux_info->height;
1818 natural_req.height += aux_info->height;
1823 minimum_req.height += hscrollbar_requisition.height;
1824 natural_req.height += hscrollbar_requisition.height;
1830 if (scrolled_window->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1831 scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1833 minimum_req.width = MAX (minimum_req.width, hscrollbar_requisition.width);
1834 natural_req.width = MAX (natural_req.width, hscrollbar_requisition.width);
1835 if (!extra_height || scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
1836 extra_height = scrollbar_spacing + hscrollbar_requisition.height;
1839 if (scrolled_window->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
1840 scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1842 minimum_req.height = MAX (minimum_req.height, vscrollbar_requisition.height);
1843 natural_req.height = MAX (natural_req.height, vscrollbar_requisition.height);
1844 if (!extra_height || scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
1845 extra_width = scrollbar_spacing + vscrollbar_requisition.width;
1848 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1849 minimum_req.width += border_width * 2 + MAX (0, extra_width);
1850 minimum_req.height += border_width * 2 + MAX (0, extra_height);
1851 natural_req.width += border_width * 2 + MAX (0, extra_width);
1852 natural_req.height += border_width * 2 + MAX (0, extra_height);
1854 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
1856 minimum_req.width += 2 * GTK_WIDGET (widget)->style->xthickness;
1857 minimum_req.height += 2 * GTK_WIDGET (widget)->style->ythickness;
1858 natural_req.width += 2 * GTK_WIDGET (widget)->style->xthickness;
1859 natural_req.height += 2 * GTK_WIDGET (widget)->style->ythickness;
1862 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1865 *minimum_size = minimum_req.width;
1867 *natural_size = natural_req.width;
1872 *minimum_size = minimum_req.height;
1874 *natural_size = natural_req.height;
1879 gtk_scrolled_window_get_width (GtkSizeRequest *widget,
1883 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1887 gtk_scrolled_window_get_height (GtkSizeRequest *widget,
1891 gtk_scrolled_window_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1895 gtk_scrolled_window_get_height_for_width (GtkSizeRequest *widget,
1897 gint *minimum_height,
1898 gint *natural_height)
1900 g_return_if_fail (GTK_IS_WIDGET (widget));
1902 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height (widget, minimum_height, natural_height);
1906 gtk_scrolled_window_get_width_for_height (GtkSizeRequest *widget,
1908 gint *minimum_width,
1909 gint *natural_width)
1911 g_return_if_fail (GTK_IS_WIDGET (widget));
1913 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, minimum_width, natural_width);