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/.
27 #include "gtkscrolledwindow.h"
28 #include "gtksignal.h"
31 /* scrolled window policy and size requisition handling:
33 * gtk size requisition works as follows:
34 * a widget upon size-request reports the width and height that it finds
35 * to be best suited to display its contents, including children.
36 * the width and/or height reported from a widget upon size requisition
37 * may be overidden by the user by specifying a width and/or height
38 * other than 0 through gtk_widget_set_usize().
40 * a scrolled window needs (for imlementing all three policy types) to
41 * request its width and height based on two different rationales.
42 * 1) the user wants the scrolled window to just fit into the space
43 * that it gets allocated for a specifc dimension.
44 * 1.1) this does not apply if the user specified a concrete value
45 * value for that specific dimension by either specifying usize for the
46 * scrolled window or for its child.
47 * 2) the user wants the scrolled window to take as much space up as
48 * is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
50 * also, kinda obvious:
51 * 3) a user would certainly not have choosen a scrolled window as a container
52 * for the child, if the resulting allocation takes up more space than the
53 * child would have allocated without the scrolled window.
56 * A) from 1) follows: the scrolled window shouldn't request more space for a
57 * specifc dimension than is required at minimum.
58 * B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
59 * window (done automatically) or by usize of the child (needs to be checked).
60 * C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
62 * D) from 3) follows: the scrolled window child's minimum width and minimum height
63 * under A) at least correspond to the space taken up by its scrollbars.
66 #define SCROLLBAR_SPACING(w) (GTK_SCROLLED_WINDOW_GET_CLASS (w)->scrollbar_spacing)
68 #define DEFAULT_SCROLLBAR_SPACING 3
74 ARG_HSCROLLBAR_POLICY,
75 ARG_VSCROLLBAR_POLICY,
81 static void gtk_scrolled_window_class_init (GtkScrolledWindowClass *klass);
82 static void gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window);
83 static void gtk_scrolled_window_set_arg (GtkObject *object,
86 static void gtk_scrolled_window_get_arg (GtkObject *object,
89 static void gtk_scrolled_window_destroy (GtkObject *object);
90 static void gtk_scrolled_window_finalize (GObject *object);
91 static gint gtk_scrolled_window_expose (GtkWidget *widget,
92 GdkEventExpose *event);
93 static void gtk_scrolled_window_size_request (GtkWidget *widget,
94 GtkRequisition *requisition);
95 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
96 GtkAllocation *allocation);
97 static gint gtk_scrolled_window_scroll_event (GtkWidget *widget,
98 GdkEventScroll *event);
99 static void gtk_scrolled_window_add (GtkContainer *container,
101 static void gtk_scrolled_window_remove (GtkContainer *container,
103 static void gtk_scrolled_window_forall (GtkContainer *container,
104 gboolean include_internals,
105 GtkCallback callback,
106 gpointer callback_data);
107 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
108 GtkAllocation *allocation);
109 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
112 static GtkContainerClass *parent_class = NULL;
116 gtk_scrolled_window_get_type (void)
118 static GtkType scrolled_window_type = 0;
120 if (!scrolled_window_type)
122 static const GtkTypeInfo scrolled_window_info =
125 sizeof (GtkScrolledWindow),
126 sizeof (GtkScrolledWindowClass),
127 (GtkClassInitFunc) gtk_scrolled_window_class_init,
128 (GtkObjectInitFunc) gtk_scrolled_window_init,
129 /* reserved_1 */ NULL,
130 /* reserved_2 */ NULL,
131 (GtkClassInitFunc) NULL,
134 scrolled_window_type = gtk_type_unique (GTK_TYPE_BIN, &scrolled_window_info);
137 return scrolled_window_type;
141 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
143 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
144 GtkObjectClass *object_class;
145 GtkWidgetClass *widget_class;
146 GtkContainerClass *container_class;
148 object_class = (GtkObjectClass*) class;
149 widget_class = (GtkWidgetClass*) class;
150 container_class = (GtkContainerClass*) class;
151 parent_class = gtk_type_class (GTK_TYPE_BIN);
153 gobject_class->finalize = gtk_scrolled_window_finalize;
155 object_class->set_arg = gtk_scrolled_window_set_arg;
156 object_class->get_arg = gtk_scrolled_window_get_arg;
157 object_class->destroy = gtk_scrolled_window_destroy;
159 widget_class->expose_event = gtk_scrolled_window_expose;
160 widget_class->size_request = gtk_scrolled_window_size_request;
161 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
162 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
164 container_class->add = gtk_scrolled_window_add;
165 container_class->remove = gtk_scrolled_window_remove;
166 container_class->forall = gtk_scrolled_window_forall;
168 class->scrollbar_spacing = DEFAULT_SCROLLBAR_SPACING;
170 gtk_object_add_arg_type ("GtkScrolledWindow::hadjustment",
172 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
174 gtk_object_add_arg_type ("GtkScrolledWindow::vadjustment",
176 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
178 gtk_object_add_arg_type ("GtkScrolledWindow::hscrollbar_policy",
179 GTK_TYPE_POLICY_TYPE,
181 ARG_HSCROLLBAR_POLICY);
182 gtk_object_add_arg_type ("GtkScrolledWindow::vscrollbar_policy",
183 GTK_TYPE_POLICY_TYPE,
185 ARG_VSCROLLBAR_POLICY);
186 gtk_object_add_arg_type ("GtkScrolledWindow::window_placement",
187 GTK_TYPE_CORNER_TYPE,
189 ARG_WINDOW_PLACEMENT);
190 gtk_object_add_arg_type ("GtkScrolledWindow::shadow",
191 GTK_TYPE_SHADOW_TYPE,
197 gtk_scrolled_window_set_arg (GtkObject *object,
201 GtkScrolledWindow *scrolled_window;
203 scrolled_window = GTK_SCROLLED_WINDOW (object);
207 case ARG_HADJUSTMENT:
208 gtk_scrolled_window_set_hadjustment (scrolled_window, GTK_VALUE_POINTER (*arg));
210 case ARG_VADJUSTMENT:
211 gtk_scrolled_window_set_vadjustment (scrolled_window, GTK_VALUE_POINTER (*arg));
213 case ARG_HSCROLLBAR_POLICY:
214 gtk_scrolled_window_set_policy (scrolled_window,
215 GTK_VALUE_ENUM (*arg),
216 scrolled_window->vscrollbar_policy);
218 case ARG_VSCROLLBAR_POLICY:
219 gtk_scrolled_window_set_policy (scrolled_window,
220 scrolled_window->hscrollbar_policy,
221 GTK_VALUE_ENUM (*arg));
223 case ARG_WINDOW_PLACEMENT:
224 gtk_scrolled_window_set_placement (scrolled_window,
225 GTK_VALUE_ENUM (*arg));
228 gtk_scrolled_window_set_shadow_type (scrolled_window,
229 GTK_VALUE_ENUM (*arg));
237 gtk_scrolled_window_get_arg (GtkObject *object,
241 GtkScrolledWindow *scrolled_window;
243 scrolled_window = GTK_SCROLLED_WINDOW (object);
247 case ARG_HADJUSTMENT:
248 GTK_VALUE_POINTER (*arg) = gtk_scrolled_window_get_hadjustment (scrolled_window);
250 case ARG_VADJUSTMENT:
251 GTK_VALUE_POINTER (*arg) = gtk_scrolled_window_get_vadjustment (scrolled_window);
253 case ARG_HSCROLLBAR_POLICY:
254 GTK_VALUE_ENUM (*arg) = scrolled_window->hscrollbar_policy;
256 case ARG_VSCROLLBAR_POLICY:
257 GTK_VALUE_ENUM (*arg) = scrolled_window->vscrollbar_policy;
259 case ARG_WINDOW_PLACEMENT:
260 GTK_VALUE_ENUM (*arg) = scrolled_window->window_placement;
263 GTK_VALUE_ENUM (*arg) = scrolled_window->shadow_type;
266 arg->type = GTK_TYPE_INVALID;
272 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
274 GTK_WIDGET_SET_FLAGS (scrolled_window, GTK_NO_WINDOW);
276 gtk_container_set_resize_mode (GTK_CONTAINER (scrolled_window), GTK_RESIZE_QUEUE);
278 scrolled_window->hscrollbar = NULL;
279 scrolled_window->vscrollbar = NULL;
280 scrolled_window->hscrollbar_policy = GTK_POLICY_ALWAYS;
281 scrolled_window->vscrollbar_policy = GTK_POLICY_ALWAYS;
282 scrolled_window->hscrollbar_visible = FALSE;
283 scrolled_window->vscrollbar_visible = FALSE;
284 scrolled_window->window_placement = GTK_CORNER_TOP_LEFT;
289 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
290 GtkAdjustment *vadjustment)
292 GtkWidget *scrolled_window;
295 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
298 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
300 scrolled_window = gtk_widget_new (GTK_TYPE_SCROLLED_WINDOW,
301 "hadjustment", hadjustment,
302 "vadjustment", vadjustment,
305 return scrolled_window;
309 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
310 GtkAdjustment *hadjustment)
314 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
316 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
318 hadjustment = (GtkAdjustment*) gtk_object_new (GTK_TYPE_ADJUSTMENT, NULL);
320 bin = GTK_BIN (scrolled_window);
322 if (!scrolled_window->hscrollbar)
324 gtk_widget_push_composite_child ();
325 scrolled_window->hscrollbar = gtk_hscrollbar_new (hadjustment);
326 gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
327 gtk_widget_pop_composite_child ();
329 gtk_widget_set_parent (scrolled_window->hscrollbar, GTK_WIDGET (scrolled_window));
330 gtk_widget_ref (scrolled_window->hscrollbar);
331 gtk_widget_show (scrolled_window->hscrollbar);
335 GtkAdjustment *old_adjustment;
337 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
338 if (old_adjustment == hadjustment)
341 gtk_signal_disconnect_by_func (GTK_OBJECT (old_adjustment),
342 GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
344 gtk_range_set_adjustment (GTK_RANGE (scrolled_window->hscrollbar),
347 hadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
348 gtk_signal_connect (GTK_OBJECT (hadjustment),
350 GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
352 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
355 gtk_widget_set_scroll_adjustments (bin->child,
356 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
357 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
361 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
362 GtkAdjustment *vadjustment)
366 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
368 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
370 vadjustment = (GtkAdjustment*) gtk_object_new (GTK_TYPE_ADJUSTMENT, NULL);
372 bin = GTK_BIN (scrolled_window);
374 if (!scrolled_window->vscrollbar)
376 gtk_widget_push_composite_child ();
377 scrolled_window->vscrollbar = gtk_vscrollbar_new (vadjustment);
378 gtk_widget_set_composite_name (scrolled_window->vscrollbar, "vscrollbar");
379 gtk_widget_pop_composite_child ();
381 gtk_widget_set_parent (scrolled_window->vscrollbar, GTK_WIDGET (scrolled_window));
382 gtk_widget_ref (scrolled_window->vscrollbar);
383 gtk_widget_show (scrolled_window->vscrollbar);
387 GtkAdjustment *old_adjustment;
389 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
390 if (old_adjustment == vadjustment)
393 gtk_signal_disconnect_by_func (GTK_OBJECT (old_adjustment),
394 GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
396 gtk_range_set_adjustment (GTK_RANGE (scrolled_window->vscrollbar),
399 vadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
400 gtk_signal_connect (GTK_OBJECT (vadjustment),
402 GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
404 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
407 gtk_widget_set_scroll_adjustments (bin->child,
408 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
409 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
413 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
415 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
417 return (scrolled_window->hscrollbar ?
418 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)) :
423 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
425 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
427 return (scrolled_window->vscrollbar ?
428 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)) :
433 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
434 GtkPolicyType hscrollbar_policy,
435 GtkPolicyType vscrollbar_policy)
437 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
439 if ((scrolled_window->hscrollbar_policy != hscrollbar_policy) ||
440 (scrolled_window->vscrollbar_policy != vscrollbar_policy))
442 scrolled_window->hscrollbar_policy = hscrollbar_policy;
443 scrolled_window->vscrollbar_policy = vscrollbar_policy;
445 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
450 * gtk_scrolled_window_get_policy:
451 * @scrolled_window: a #GtkScrolledWindow
452 * @hscrollbar_policy: location to store the policy for the horizontal scrollbar, or %NULL.
453 * @vscrollbar_policy: location to store the policy for the horizontal scrollbar, or %NULL.
455 * Retrieves the current policy values for the horizontal and vertical
456 * scrollbars. See gtk_scrolled_window_set_policy().
459 gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
460 GtkPolicyType *hscrollbar_policy,
461 GtkPolicyType *vscrollbar_policy)
463 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
465 if (hscrollbar_policy)
466 *hscrollbar_policy = scrolled_window->hscrollbar_policy;
467 if (vscrollbar_policy)
468 *vscrollbar_policy = scrolled_window->vscrollbar_policy;
472 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
473 GtkCornerType window_placement)
475 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
477 if (scrolled_window->window_placement != window_placement)
479 scrolled_window->window_placement = window_placement;
481 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
486 * gtk_scrolled_window_get_placement:
487 * @scrolled_window: a #GtkScrolledWindow
489 * Gets the placement of the scrollbars for the scrolled window. See
490 * gtk_scrolled_window_set_placement().
492 * Return value: the current placement value.
495 gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
497 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
499 return scrolled_window->window_placement;
503 * gtk_scrolled_window_set_shadow_type:
504 * @scrolled_window: a #GtkScrolledWindow
505 * @type: kind of shadow to draw around scrolled window contents
507 * Changes the type of shadow drawn around the contents of
512 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
515 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
516 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
518 if (scrolled_window->shadow_type != type)
520 scrolled_window->shadow_type = type;
522 if (GTK_WIDGET_DRAWABLE (scrolled_window))
523 gtk_widget_queue_clear (GTK_WIDGET (scrolled_window));
525 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
530 * gtk_scrolled_window_get_shadow_type:
531 * @scrolled_window: a #GtkScrolledWindow
533 * Gets the shadow type of the scrolled window. See
534 * gtk_scrolled_window_set_shadow_type().
536 * Return value: the current shadow type
539 gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
541 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
543 return scrolled_window->shadow_type;
547 gtk_scrolled_window_destroy (GtkObject *object)
549 GtkScrolledWindow *scrolled_window;
551 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (object));
553 scrolled_window = GTK_SCROLLED_WINDOW (object);
555 gtk_widget_unparent (scrolled_window->hscrollbar);
556 gtk_widget_unparent (scrolled_window->vscrollbar);
557 gtk_widget_destroy (scrolled_window->hscrollbar);
558 gtk_widget_destroy (scrolled_window->vscrollbar);
560 GTK_OBJECT_CLASS (parent_class)->destroy (object);
564 gtk_scrolled_window_finalize (GObject *object)
566 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
568 gtk_widget_unref (scrolled_window->hscrollbar);
569 gtk_widget_unref (scrolled_window->vscrollbar);
571 G_OBJECT_CLASS (parent_class)->finalize (object);
575 gtk_scrolled_window_paint (GtkWidget *widget,
578 GtkAllocation relative_allocation;
579 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
581 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
583 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
585 relative_allocation.x -= widget->style->xthickness;
586 relative_allocation.y -= widget->style->ythickness;
587 relative_allocation.width += 2 * widget->style->xthickness;
588 relative_allocation.height += 2 * widget->style->ythickness;
590 gtk_paint_shadow (widget->style, widget->window,
591 GTK_STATE_NORMAL, scrolled_window->shadow_type,
592 area, widget, "scrolled_window",
593 widget->allocation.x + relative_allocation.x,
594 widget->allocation.y + relative_allocation.y,
595 relative_allocation.width,
596 relative_allocation.height);
601 gtk_scrolled_window_expose (GtkWidget *widget,
602 GdkEventExpose *event)
604 if (GTK_WIDGET_DRAWABLE (widget))
606 gtk_scrolled_window_paint (widget, &event->area);
608 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
615 gtk_scrolled_window_forall (GtkContainer *container,
616 gboolean include_internals,
617 GtkCallback callback,
618 gpointer callback_data)
620 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
621 g_return_if_fail (callback != NULL);
623 GTK_CONTAINER_CLASS (parent_class)->forall (container,
627 if (include_internals)
629 GtkScrolledWindow *scrolled_window;
631 scrolled_window = GTK_SCROLLED_WINDOW (container);
633 if (scrolled_window->vscrollbar)
634 callback (scrolled_window->vscrollbar, callback_data);
635 if (scrolled_window->hscrollbar)
636 callback (scrolled_window->hscrollbar, callback_data);
641 gtk_scrolled_window_size_request (GtkWidget *widget,
642 GtkRequisition *requisition)
644 GtkScrolledWindow *scrolled_window;
648 GtkRequisition hscrollbar_requisition;
649 GtkRequisition vscrollbar_requisition;
650 GtkRequisition child_requisition;
652 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
653 g_return_if_fail (requisition != NULL);
655 scrolled_window = GTK_SCROLLED_WINDOW (widget);
656 bin = GTK_BIN (scrolled_window);
660 requisition->width = 0;
661 requisition->height = 0;
663 gtk_widget_size_request (scrolled_window->hscrollbar,
664 &hscrollbar_requisition);
665 gtk_widget_size_request (scrolled_window->vscrollbar,
666 &vscrollbar_requisition);
668 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
670 gtk_widget_size_request (bin->child, &child_requisition);
672 if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
673 requisition->width += child_requisition.width;
676 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (bin->child, FALSE);
678 if (aux_info && aux_info->width > 0)
680 requisition->width += aux_info->width;
684 requisition->width += vscrollbar_requisition.width;
687 if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
688 requisition->height += child_requisition.height;
691 GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (bin->child, FALSE);
693 if (aux_info && aux_info->height > 0)
695 requisition->height += aux_info->height;
699 requisition->height += hscrollbar_requisition.height;
703 if (scrolled_window->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
704 scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
706 requisition->width = MAX (requisition->width, hscrollbar_requisition.width);
707 if (!extra_height || scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
708 extra_height = SCROLLBAR_SPACING (scrolled_window) + hscrollbar_requisition.height;
711 if (scrolled_window->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
712 scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
714 requisition->height = MAX (requisition->height, vscrollbar_requisition.height);
715 if (!extra_height || scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
716 extra_width = SCROLLBAR_SPACING (scrolled_window) + vscrollbar_requisition.width;
719 requisition->width += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_width);
720 requisition->height += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_height);
722 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
724 requisition->width += 2 * widget->style->xthickness;
725 requisition->height += 2 * widget->style->ythickness;
730 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
731 GtkAllocation *allocation)
733 GtkScrolledWindow *scrolled_window;
735 g_return_if_fail (widget != NULL);
736 g_return_if_fail (allocation != NULL);
738 scrolled_window = GTK_SCROLLED_WINDOW (widget);
740 allocation->x = GTK_CONTAINER (widget)->border_width;
741 allocation->y = GTK_CONTAINER (widget)->border_width;
743 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
745 allocation->x += widget->style->xthickness;
746 allocation->y += widget->style->ythickness;
749 allocation->width = MAX (1, (gint)widget->allocation.width - allocation->x * 2);
750 allocation->height = MAX (1, (gint)widget->allocation.height - allocation->y * 2);
752 if (scrolled_window->vscrollbar_visible)
754 GtkRequisition vscrollbar_requisition;
755 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
756 &vscrollbar_requisition);
758 if (scrolled_window->window_placement == GTK_CORNER_TOP_RIGHT ||
759 scrolled_window->window_placement == GTK_CORNER_BOTTOM_RIGHT)
760 allocation->x += (vscrollbar_requisition.width +
761 SCROLLBAR_SPACING (scrolled_window));
763 allocation->width = MAX (1, (gint)allocation->width -
764 ((gint)vscrollbar_requisition.width +
765 (gint)SCROLLBAR_SPACING (scrolled_window)));
767 if (scrolled_window->hscrollbar_visible)
769 GtkRequisition hscrollbar_requisition;
770 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
771 &hscrollbar_requisition);
773 if (scrolled_window->window_placement == GTK_CORNER_BOTTOM_LEFT ||
774 scrolled_window->window_placement == GTK_CORNER_BOTTOM_RIGHT)
775 allocation->y += (hscrollbar_requisition.height +
776 SCROLLBAR_SPACING (scrolled_window));
778 allocation->height = MAX (1, (gint)allocation->height -
779 ((gint)hscrollbar_requisition.height +
780 (gint)SCROLLBAR_SPACING (scrolled_window)));
785 gtk_scrolled_window_size_allocate (GtkWidget *widget,
786 GtkAllocation *allocation)
788 GtkScrolledWindow *scrolled_window;
790 GtkAllocation relative_allocation;
791 GtkAllocation child_allocation;
793 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
794 g_return_if_fail (allocation != NULL);
796 scrolled_window = GTK_SCROLLED_WINDOW (widget);
797 bin = GTK_BIN (scrolled_window);
799 widget->allocation = *allocation;
801 if (scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
802 scrolled_window->hscrollbar_visible = TRUE;
803 else if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
804 scrolled_window->hscrollbar_visible = FALSE;
805 if (scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
806 scrolled_window->vscrollbar_visible = TRUE;
807 else if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
808 scrolled_window->vscrollbar_visible = FALSE;
810 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
812 gboolean previous_hvis;
813 gboolean previous_vvis;
818 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
820 child_allocation.x = relative_allocation.x + allocation->x;
821 child_allocation.y = relative_allocation.y + allocation->y;
822 child_allocation.width = relative_allocation.width;
823 child_allocation.height = relative_allocation.height;
825 previous_hvis = scrolled_window->hscrollbar_visible;
826 previous_vvis = scrolled_window->vscrollbar_visible;
828 gtk_widget_size_allocate (bin->child, &child_allocation);
830 /* If, after the first iteration, the hscrollbar and the
831 * vscrollbar flip visiblity, then we need both.
834 previous_hvis != scrolled_window->hscrollbar_visible &&
835 previous_vvis != scrolled_window->vscrollbar_visible)
837 scrolled_window->hscrollbar_visible = TRUE;
838 scrolled_window->vscrollbar_visible = TRUE;
840 /* a new resize is already queued at this point,
841 * so we will immediatedly get reinvoked
848 while (previous_hvis != scrolled_window->hscrollbar_visible ||
849 previous_vvis != scrolled_window->vscrollbar_visible);
852 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
854 if (scrolled_window->hscrollbar_visible)
856 GtkRequisition hscrollbar_requisition;
857 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
858 &hscrollbar_requisition);
860 if (!GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
861 gtk_widget_show (scrolled_window->hscrollbar);
863 child_allocation.x = relative_allocation.x;
864 if (scrolled_window->window_placement == GTK_CORNER_TOP_LEFT ||
865 scrolled_window->window_placement == GTK_CORNER_TOP_RIGHT)
866 child_allocation.y = (relative_allocation.y +
867 relative_allocation.height +
868 SCROLLBAR_SPACING (scrolled_window) +
869 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
870 0 : widget->style->ythickness));
872 child_allocation.y = GTK_CONTAINER (scrolled_window)->border_width;
874 child_allocation.width = relative_allocation.width;
875 child_allocation.height = hscrollbar_requisition.height;
876 child_allocation.x += allocation->x;
877 child_allocation.y += allocation->y;
879 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
881 child_allocation.x -= widget->style->xthickness;
882 child_allocation.width += 2 * widget->style->xthickness;
885 gtk_widget_size_allocate (scrolled_window->hscrollbar, &child_allocation);
887 else if (GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
888 gtk_widget_hide (scrolled_window->hscrollbar);
890 if (scrolled_window->vscrollbar_visible)
892 GtkRequisition vscrollbar_requisition;
893 if (!GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
894 gtk_widget_show (scrolled_window->vscrollbar);
896 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
897 &vscrollbar_requisition);
899 if (scrolled_window->window_placement == GTK_CORNER_TOP_LEFT ||
900 scrolled_window->window_placement == GTK_CORNER_BOTTOM_LEFT)
901 child_allocation.x = (relative_allocation.x +
902 relative_allocation.width +
903 SCROLLBAR_SPACING (scrolled_window) +
904 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
905 0 : widget->style->xthickness));
907 child_allocation.x = GTK_CONTAINER (scrolled_window)->border_width;
909 child_allocation.y = relative_allocation.y;
910 child_allocation.width = vscrollbar_requisition.width;
911 child_allocation.height = relative_allocation.height;
912 child_allocation.x += allocation->x;
913 child_allocation.y += allocation->y;
915 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
917 child_allocation.y -= widget->style->ythickness;
918 child_allocation.height += 2 * widget->style->ythickness;
921 gtk_widget_size_allocate (scrolled_window->vscrollbar, &child_allocation);
923 else if (GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
924 gtk_widget_hide (scrolled_window->vscrollbar);
928 gtk_scrolled_window_scroll_event (GtkWidget *widget,
929 GdkEventScroll *event)
933 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
934 g_return_val_if_fail (event != NULL, FALSE);
936 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
937 range = GTK_SCROLLED_WINDOW (widget)->vscrollbar;
939 range = GTK_SCROLLED_WINDOW (widget)->hscrollbar;
941 if (range && GTK_WIDGET_VISIBLE (range))
943 GtkAdjustment *adj = GTK_RANGE (range)->adjustment;
946 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_LEFT)
947 new_value = adj->value - adj->page_increment / 2;
949 new_value = adj->value + adj->page_increment / 2;
951 new_value = CLAMP (new_value, adj->lower, adj->upper - adj->page_size);
952 gtk_adjustment_set_value (adj, new_value);
961 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
964 GtkScrolledWindow *scrolled_win;
966 g_return_if_fail (adjustment != NULL);
967 g_return_if_fail (data != NULL);
969 scrolled_win = GTK_SCROLLED_WINDOW (data);
971 if (scrolled_win->hscrollbar &&
972 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->hscrollbar)))
974 if (scrolled_win->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
978 visible = scrolled_win->hscrollbar_visible;
979 scrolled_win->hscrollbar_visible = (adjustment->upper - adjustment->lower >
980 adjustment->page_size);
981 if (scrolled_win->hscrollbar_visible != visible)
982 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
985 else if (scrolled_win->vscrollbar &&
986 adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->vscrollbar)))
988 if (scrolled_win->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
992 visible = scrolled_win->vscrollbar_visible;
993 scrolled_win->vscrollbar_visible = (adjustment->upper - adjustment->lower >
994 adjustment->page_size);
995 if (scrolled_win->vscrollbar_visible != visible)
996 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1002 gtk_scrolled_window_add (GtkContainer *container,
1005 GtkScrolledWindow *scrolled_window;
1008 bin = GTK_BIN (container);
1009 g_return_if_fail (bin->child == NULL);
1011 scrolled_window = GTK_SCROLLED_WINDOW (container);
1014 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1016 /* this is a temporary message */
1017 if (!gtk_widget_set_scroll_adjustments (child,
1018 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
1019 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar))))
1020 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1021 "use gtk_scrolled_window_add_with_viewport() instead");
1025 gtk_scrolled_window_remove (GtkContainer *container,
1028 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1029 g_return_if_fail (child != NULL);
1030 g_return_if_fail (GTK_BIN (container)->child == child);
1032 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1034 /* chain parent class handler to remove child */
1035 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
1039 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1043 GtkWidget *viewport;
1045 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1046 g_return_if_fail (GTK_IS_WIDGET (child));
1047 g_return_if_fail (child->parent == NULL);
1049 bin = GTK_BIN (scrolled_window);
1051 if (bin->child != NULL)
1053 g_return_if_fail (GTK_IS_VIEWPORT (bin->child));
1054 g_return_if_fail (GTK_BIN (bin->child)->child == NULL);
1056 viewport = bin->child;
1061 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1062 gtk_scrolled_window_get_vadjustment (scrolled_window));
1063 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1066 gtk_widget_show (viewport);
1067 gtk_container_add (GTK_CONTAINER (viewport), child);