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 void gtk_scrolled_window_map (GtkWidget *widget);
92 static void gtk_scrolled_window_unmap (GtkWidget *widget);
93 static gint gtk_scrolled_window_expose (GtkWidget *widget,
94 GdkEventExpose *event);
95 static void gtk_scrolled_window_size_request (GtkWidget *widget,
96 GtkRequisition *requisition);
97 static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
98 GtkAllocation *allocation);
99 static gint gtk_scrolled_window_scroll_event (GtkWidget *widget,
100 GdkEventScroll *event);
101 static void gtk_scrolled_window_add (GtkContainer *container,
103 static void gtk_scrolled_window_remove (GtkContainer *container,
105 static void gtk_scrolled_window_forall (GtkContainer *container,
106 gboolean include_internals,
107 GtkCallback callback,
108 gpointer callback_data);
109 static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
110 GtkAllocation *allocation);
111 static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
114 static GtkContainerClass *parent_class = NULL;
118 gtk_scrolled_window_get_type (void)
120 static GtkType scrolled_window_type = 0;
122 if (!scrolled_window_type)
124 static const GtkTypeInfo scrolled_window_info =
127 sizeof (GtkScrolledWindow),
128 sizeof (GtkScrolledWindowClass),
129 (GtkClassInitFunc) gtk_scrolled_window_class_init,
130 (GtkObjectInitFunc) gtk_scrolled_window_init,
131 /* reserved_1 */ NULL,
132 /* reserved_2 */ NULL,
133 (GtkClassInitFunc) NULL,
136 scrolled_window_type = gtk_type_unique (GTK_TYPE_BIN, &scrolled_window_info);
139 return scrolled_window_type;
143 gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
145 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
146 GtkObjectClass *object_class;
147 GtkWidgetClass *widget_class;
148 GtkContainerClass *container_class;
150 object_class = (GtkObjectClass*) class;
151 widget_class = (GtkWidgetClass*) class;
152 container_class = (GtkContainerClass*) class;
153 parent_class = gtk_type_class (GTK_TYPE_BIN);
155 gobject_class->finalize = gtk_scrolled_window_finalize;
157 object_class->set_arg = gtk_scrolled_window_set_arg;
158 object_class->get_arg = gtk_scrolled_window_get_arg;
159 object_class->destroy = gtk_scrolled_window_destroy;
161 widget_class->map = gtk_scrolled_window_map;
162 widget_class->unmap = gtk_scrolled_window_unmap;
163 widget_class->expose_event = gtk_scrolled_window_expose;
164 widget_class->size_request = gtk_scrolled_window_size_request;
165 widget_class->size_allocate = gtk_scrolled_window_size_allocate;
166 widget_class->scroll_event = gtk_scrolled_window_scroll_event;
168 container_class->add = gtk_scrolled_window_add;
169 container_class->remove = gtk_scrolled_window_remove;
170 container_class->forall = gtk_scrolled_window_forall;
172 class->scrollbar_spacing = DEFAULT_SCROLLBAR_SPACING;
174 gtk_object_add_arg_type ("GtkScrolledWindow::hadjustment",
176 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
178 gtk_object_add_arg_type ("GtkScrolledWindow::vadjustment",
180 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
182 gtk_object_add_arg_type ("GtkScrolledWindow::hscrollbar_policy",
183 GTK_TYPE_POLICY_TYPE,
185 ARG_HSCROLLBAR_POLICY);
186 gtk_object_add_arg_type ("GtkScrolledWindow::vscrollbar_policy",
187 GTK_TYPE_POLICY_TYPE,
189 ARG_VSCROLLBAR_POLICY);
190 gtk_object_add_arg_type ("GtkScrolledWindow::window_placement",
191 GTK_TYPE_CORNER_TYPE,
193 ARG_WINDOW_PLACEMENT);
194 gtk_object_add_arg_type ("GtkScrolledWindow::shadow",
195 GTK_TYPE_SHADOW_TYPE,
201 gtk_scrolled_window_set_arg (GtkObject *object,
205 GtkScrolledWindow *scrolled_window;
207 scrolled_window = GTK_SCROLLED_WINDOW (object);
211 case ARG_HADJUSTMENT:
212 gtk_scrolled_window_set_hadjustment (scrolled_window, GTK_VALUE_POINTER (*arg));
214 case ARG_VADJUSTMENT:
215 gtk_scrolled_window_set_vadjustment (scrolled_window, GTK_VALUE_POINTER (*arg));
217 case ARG_HSCROLLBAR_POLICY:
218 gtk_scrolled_window_set_policy (scrolled_window,
219 GTK_VALUE_ENUM (*arg),
220 scrolled_window->vscrollbar_policy);
222 case ARG_VSCROLLBAR_POLICY:
223 gtk_scrolled_window_set_policy (scrolled_window,
224 scrolled_window->hscrollbar_policy,
225 GTK_VALUE_ENUM (*arg));
227 case ARG_WINDOW_PLACEMENT:
228 gtk_scrolled_window_set_placement (scrolled_window,
229 GTK_VALUE_ENUM (*arg));
232 gtk_scrolled_window_set_shadow_type (scrolled_window,
233 GTK_VALUE_ENUM (*arg));
241 gtk_scrolled_window_get_arg (GtkObject *object,
245 GtkScrolledWindow *scrolled_window;
247 scrolled_window = GTK_SCROLLED_WINDOW (object);
251 case ARG_HADJUSTMENT:
252 GTK_VALUE_POINTER (*arg) = gtk_scrolled_window_get_hadjustment (scrolled_window);
254 case ARG_VADJUSTMENT:
255 GTK_VALUE_POINTER (*arg) = gtk_scrolled_window_get_vadjustment (scrolled_window);
257 case ARG_HSCROLLBAR_POLICY:
258 GTK_VALUE_ENUM (*arg) = scrolled_window->hscrollbar_policy;
260 case ARG_VSCROLLBAR_POLICY:
261 GTK_VALUE_ENUM (*arg) = scrolled_window->vscrollbar_policy;
263 case ARG_WINDOW_PLACEMENT:
264 GTK_VALUE_ENUM (*arg) = scrolled_window->window_placement;
267 GTK_VALUE_ENUM (*arg) = scrolled_window->shadow_type;
270 arg->type = GTK_TYPE_INVALID;
276 gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
278 GTK_WIDGET_SET_FLAGS (scrolled_window, GTK_NO_WINDOW);
280 gtk_container_set_resize_mode (GTK_CONTAINER (scrolled_window), GTK_RESIZE_QUEUE);
282 scrolled_window->hscrollbar = NULL;
283 scrolled_window->vscrollbar = NULL;
284 scrolled_window->hscrollbar_policy = GTK_POLICY_ALWAYS;
285 scrolled_window->vscrollbar_policy = GTK_POLICY_ALWAYS;
286 scrolled_window->hscrollbar_visible = FALSE;
287 scrolled_window->vscrollbar_visible = FALSE;
288 scrolled_window->window_placement = GTK_CORNER_TOP_LEFT;
293 gtk_scrolled_window_new (GtkAdjustment *hadjustment,
294 GtkAdjustment *vadjustment)
296 GtkWidget *scrolled_window;
299 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
302 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
304 scrolled_window = gtk_widget_new (GTK_TYPE_SCROLLED_WINDOW,
305 "hadjustment", hadjustment,
306 "vadjustment", vadjustment,
309 return scrolled_window;
313 gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
314 GtkAdjustment *hadjustment)
318 g_return_if_fail (scrolled_window != NULL);
319 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
321 g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
323 hadjustment = (GtkAdjustment*) gtk_object_new (GTK_TYPE_ADJUSTMENT, NULL);
325 bin = GTK_BIN (scrolled_window);
327 if (!scrolled_window->hscrollbar)
329 gtk_widget_push_composite_child ();
330 scrolled_window->hscrollbar = gtk_hscrollbar_new (hadjustment);
331 gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
332 gtk_widget_pop_composite_child ();
334 gtk_widget_set_parent (scrolled_window->hscrollbar, GTK_WIDGET (scrolled_window));
335 gtk_widget_ref (scrolled_window->hscrollbar);
336 gtk_widget_show (scrolled_window->hscrollbar);
340 GtkAdjustment *old_adjustment;
342 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
343 if (old_adjustment == hadjustment)
346 gtk_signal_disconnect_by_func (GTK_OBJECT (old_adjustment),
347 GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
349 gtk_range_set_adjustment (GTK_RANGE (scrolled_window->hscrollbar),
352 hadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
353 gtk_signal_connect (GTK_OBJECT (hadjustment),
355 GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
357 gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
360 gtk_widget_set_scroll_adjustments (bin->child,
361 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
362 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
366 gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
367 GtkAdjustment *vadjustment)
371 g_return_if_fail (scrolled_window != NULL);
372 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
374 g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
376 vadjustment = (GtkAdjustment*) gtk_object_new (GTK_TYPE_ADJUSTMENT, NULL);
378 bin = GTK_BIN (scrolled_window);
380 if (!scrolled_window->vscrollbar)
382 gtk_widget_push_composite_child ();
383 scrolled_window->vscrollbar = gtk_vscrollbar_new (vadjustment);
384 gtk_widget_set_composite_name (scrolled_window->vscrollbar, "vscrollbar");
385 gtk_widget_pop_composite_child ();
387 gtk_widget_set_parent (scrolled_window->vscrollbar, GTK_WIDGET (scrolled_window));
388 gtk_widget_ref (scrolled_window->vscrollbar);
389 gtk_widget_show (scrolled_window->vscrollbar);
393 GtkAdjustment *old_adjustment;
395 old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
396 if (old_adjustment == vadjustment)
399 gtk_signal_disconnect_by_func (GTK_OBJECT (old_adjustment),
400 GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
402 gtk_range_set_adjustment (GTK_RANGE (scrolled_window->vscrollbar),
405 vadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
406 gtk_signal_connect (GTK_OBJECT (vadjustment),
408 GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
410 gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
413 gtk_widget_set_scroll_adjustments (bin->child,
414 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
415 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
419 gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
421 g_return_val_if_fail (scrolled_window != NULL, NULL);
422 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
424 return (scrolled_window->hscrollbar ?
425 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)) :
430 gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
432 g_return_val_if_fail (scrolled_window != NULL, NULL);
433 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
435 return (scrolled_window->vscrollbar ?
436 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)) :
441 gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
442 GtkPolicyType hscrollbar_policy,
443 GtkPolicyType vscrollbar_policy)
445 g_return_if_fail (scrolled_window != NULL);
446 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
448 if ((scrolled_window->hscrollbar_policy != hscrollbar_policy) ||
449 (scrolled_window->vscrollbar_policy != vscrollbar_policy))
451 scrolled_window->hscrollbar_policy = hscrollbar_policy;
452 scrolled_window->vscrollbar_policy = vscrollbar_policy;
454 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
459 gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
460 GtkCornerType window_placement)
462 g_return_if_fail (scrolled_window != NULL);
463 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
465 if (scrolled_window->window_placement != window_placement)
467 scrolled_window->window_placement = window_placement;
469 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
474 * gtk_scrolled_window_set_shadow_type:
475 * @scrolled_window: a #GtkScrolledWindow
476 * @type: kind of shadow to draw around scrolled window contents
478 * Changes the type of shadow drawn around the contents of
483 gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
486 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
487 g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
489 if (scrolled_window->shadow_type != type)
491 scrolled_window->shadow_type = type;
493 if (GTK_WIDGET_DRAWABLE (scrolled_window))
494 gtk_widget_queue_clear (GTK_WIDGET (scrolled_window));
496 gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
501 gtk_scrolled_window_destroy (GtkObject *object)
503 GtkScrolledWindow *scrolled_window;
505 g_return_if_fail (object != NULL);
506 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (object));
508 scrolled_window = GTK_SCROLLED_WINDOW (object);
510 gtk_widget_unparent (scrolled_window->hscrollbar);
511 gtk_widget_unparent (scrolled_window->vscrollbar);
512 gtk_widget_destroy (scrolled_window->hscrollbar);
513 gtk_widget_destroy (scrolled_window->vscrollbar);
515 GTK_OBJECT_CLASS (parent_class)->destroy (object);
519 gtk_scrolled_window_finalize (GObject *object)
521 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
523 gtk_widget_unref (scrolled_window->hscrollbar);
524 gtk_widget_unref (scrolled_window->vscrollbar);
526 G_OBJECT_CLASS (parent_class)->finalize (object);
530 gtk_scrolled_window_map (GtkWidget *widget)
532 GtkScrolledWindow *scrolled_window;
534 g_return_if_fail (widget != NULL);
535 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
537 scrolled_window = GTK_SCROLLED_WINDOW (widget);
539 /* chain parent class handler to map self and child */
540 GTK_WIDGET_CLASS (parent_class)->map (widget);
542 if (GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar) &&
543 !GTK_WIDGET_MAPPED (scrolled_window->hscrollbar))
544 gtk_widget_map (scrolled_window->hscrollbar);
546 if (GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar) &&
547 !GTK_WIDGET_MAPPED (scrolled_window->vscrollbar))
548 gtk_widget_map (scrolled_window->vscrollbar);
552 gtk_scrolled_window_unmap (GtkWidget *widget)
554 GtkScrolledWindow *scrolled_window;
556 g_return_if_fail (widget != NULL);
557 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
559 scrolled_window = GTK_SCROLLED_WINDOW (widget);
561 /* chain parent class handler to unmap self and child */
562 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
564 if (GTK_WIDGET_MAPPED (scrolled_window->hscrollbar))
565 gtk_widget_unmap (scrolled_window->hscrollbar);
567 if (GTK_WIDGET_MAPPED (scrolled_window->vscrollbar))
568 gtk_widget_unmap (scrolled_window->vscrollbar);
572 gtk_scrolled_window_paint (GtkWidget *widget,
575 GtkAllocation relative_allocation;
576 GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
578 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
580 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
582 relative_allocation.x -= widget->style->xthickness;
583 relative_allocation.y -= widget->style->ythickness;
584 relative_allocation.width += 2 * widget->style->xthickness;
585 relative_allocation.height += 2 * widget->style->ythickness;
587 gtk_paint_shadow (widget->style, widget->window,
588 GTK_STATE_NORMAL, scrolled_window->shadow_type,
589 area, widget, "scrolled_window",
590 widget->allocation.x + relative_allocation.x,
591 widget->allocation.y + relative_allocation.y,
592 relative_allocation.width,
593 relative_allocation.height);
598 gtk_scrolled_window_expose (GtkWidget *widget,
599 GdkEventExpose *event)
601 if (GTK_WIDGET_DRAWABLE (widget))
603 gtk_scrolled_window_paint (widget, &event->area);
605 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
612 gtk_scrolled_window_forall (GtkContainer *container,
613 gboolean include_internals,
614 GtkCallback callback,
615 gpointer callback_data)
617 g_return_if_fail (container != NULL);
618 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
619 g_return_if_fail (callback != NULL);
621 GTK_CONTAINER_CLASS (parent_class)->forall (container,
625 if (include_internals)
627 GtkScrolledWindow *scrolled_window;
629 scrolled_window = GTK_SCROLLED_WINDOW (container);
631 if (scrolled_window->vscrollbar)
632 callback (scrolled_window->vscrollbar, callback_data);
633 if (scrolled_window->hscrollbar)
634 callback (scrolled_window->hscrollbar, callback_data);
639 gtk_scrolled_window_size_request (GtkWidget *widget,
640 GtkRequisition *requisition)
642 GtkScrolledWindow *scrolled_window;
646 GtkRequisition hscrollbar_requisition;
647 GtkRequisition vscrollbar_requisition;
648 GtkRequisition child_requisition;
650 g_return_if_fail (widget != NULL);
651 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
652 g_return_if_fail (requisition != NULL);
654 scrolled_window = GTK_SCROLLED_WINDOW (widget);
655 bin = GTK_BIN (scrolled_window);
659 requisition->width = 0;
660 requisition->height = 0;
662 gtk_widget_size_request (scrolled_window->hscrollbar,
663 &hscrollbar_requisition);
664 gtk_widget_size_request (scrolled_window->vscrollbar,
665 &vscrollbar_requisition);
667 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
669 static guint quark_aux_info = 0;
672 quark_aux_info = g_quark_from_static_string ("gtk-aux-info");
674 gtk_widget_size_request (bin->child, &child_requisition);
676 if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
677 requisition->width += child_requisition.width;
680 GtkWidgetAuxInfo *aux_info;
682 aux_info = gtk_object_get_data_by_id (GTK_OBJECT (bin->child), quark_aux_info);
683 if (aux_info && aux_info->width > 0)
685 requisition->width += aux_info->width;
689 requisition->width += vscrollbar_requisition.width;
692 if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
693 requisition->height += child_requisition.height;
696 GtkWidgetAuxInfo *aux_info;
698 aux_info = gtk_object_get_data_by_id (GTK_OBJECT (bin->child), quark_aux_info);
699 if (aux_info && aux_info->height > 0)
701 requisition->height += aux_info->height;
705 requisition->height += hscrollbar_requisition.height;
709 if (scrolled_window->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
710 scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
712 requisition->width = MAX (requisition->width, hscrollbar_requisition.width);
713 if (!extra_height || scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
714 extra_height = SCROLLBAR_SPACING (scrolled_window) + hscrollbar_requisition.height;
717 if (scrolled_window->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
718 scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
720 requisition->height = MAX (requisition->height, vscrollbar_requisition.height);
721 if (!extra_height || scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
722 extra_width = SCROLLBAR_SPACING (scrolled_window) + vscrollbar_requisition.width;
725 requisition->width += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_width);
726 requisition->height += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_height);
728 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
730 requisition->width += 2 * widget->style->xthickness;
731 requisition->height += 2 * widget->style->ythickness;
736 gtk_scrolled_window_relative_allocation (GtkWidget *widget,
737 GtkAllocation *allocation)
739 GtkScrolledWindow *scrolled_window;
741 g_return_if_fail (widget != NULL);
742 g_return_if_fail (allocation != NULL);
744 scrolled_window = GTK_SCROLLED_WINDOW (widget);
746 allocation->x = GTK_CONTAINER (widget)->border_width;
747 allocation->y = GTK_CONTAINER (widget)->border_width;
749 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
751 allocation->x += widget->style->xthickness;
752 allocation->y += widget->style->ythickness;
755 allocation->width = MAX (1, (gint)widget->allocation.width - allocation->x * 2);
756 allocation->height = MAX (1, (gint)widget->allocation.height - allocation->y * 2);
758 if (scrolled_window->vscrollbar_visible)
760 GtkRequisition vscrollbar_requisition;
761 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
762 &vscrollbar_requisition);
764 if (scrolled_window->window_placement == GTK_CORNER_TOP_RIGHT ||
765 scrolled_window->window_placement == GTK_CORNER_BOTTOM_RIGHT)
766 allocation->x += (vscrollbar_requisition.width +
767 SCROLLBAR_SPACING (scrolled_window));
769 allocation->width = MAX (1, (gint)allocation->width -
770 ((gint)vscrollbar_requisition.width +
771 (gint)SCROLLBAR_SPACING (scrolled_window)));
773 if (scrolled_window->hscrollbar_visible)
775 GtkRequisition hscrollbar_requisition;
776 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
777 &hscrollbar_requisition);
779 if (scrolled_window->window_placement == GTK_CORNER_BOTTOM_LEFT ||
780 scrolled_window->window_placement == GTK_CORNER_BOTTOM_RIGHT)
781 allocation->y += (hscrollbar_requisition.height +
782 SCROLLBAR_SPACING (scrolled_window));
784 allocation->height = MAX (1, (gint)allocation->height -
785 ((gint)hscrollbar_requisition.height +
786 (gint)SCROLLBAR_SPACING (scrolled_window)));
791 gtk_scrolled_window_size_allocate (GtkWidget *widget,
792 GtkAllocation *allocation)
794 GtkScrolledWindow *scrolled_window;
796 GtkAllocation relative_allocation;
797 GtkAllocation child_allocation;
799 g_return_if_fail (widget != NULL);
800 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
801 g_return_if_fail (allocation != NULL);
803 scrolled_window = GTK_SCROLLED_WINDOW (widget);
804 bin = GTK_BIN (scrolled_window);
806 widget->allocation = *allocation;
808 if (scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
809 scrolled_window->hscrollbar_visible = TRUE;
810 else if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
811 scrolled_window->hscrollbar_visible = FALSE;
812 if (scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
813 scrolled_window->vscrollbar_visible = TRUE;
814 else if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
815 scrolled_window->vscrollbar_visible = FALSE;
817 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
819 gboolean previous_hvis;
820 gboolean previous_vvis;
825 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
827 child_allocation.x = relative_allocation.x + allocation->x;
828 child_allocation.y = relative_allocation.y + allocation->y;
829 child_allocation.width = relative_allocation.width;
830 child_allocation.height = relative_allocation.height;
832 previous_hvis = scrolled_window->hscrollbar_visible;
833 previous_vvis = scrolled_window->vscrollbar_visible;
835 gtk_widget_size_allocate (bin->child, &child_allocation);
837 /* If, after the first iteration, the hscrollbar and the
838 * vscrollbar flip visiblity, then we need both.
841 previous_hvis != scrolled_window->hscrollbar_visible &&
842 previous_vvis != scrolled_window->vscrollbar_visible)
844 scrolled_window->hscrollbar_visible = TRUE;
845 scrolled_window->vscrollbar_visible = TRUE;
847 /* a new resize is already queued at this point,
848 * so we will immediatedly get reinvoked
855 while (previous_hvis != scrolled_window->hscrollbar_visible ||
856 previous_vvis != scrolled_window->vscrollbar_visible);
859 gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
861 if (scrolled_window->hscrollbar_visible)
863 GtkRequisition hscrollbar_requisition;
864 gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
865 &hscrollbar_requisition);
867 if (!GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
868 gtk_widget_show (scrolled_window->hscrollbar);
870 child_allocation.x = relative_allocation.x;
871 if (scrolled_window->window_placement == GTK_CORNER_TOP_LEFT ||
872 scrolled_window->window_placement == GTK_CORNER_TOP_RIGHT)
873 child_allocation.y = (relative_allocation.y +
874 relative_allocation.height +
875 SCROLLBAR_SPACING (scrolled_window) +
876 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
877 0 : widget->style->ythickness));
879 child_allocation.y = GTK_CONTAINER (scrolled_window)->border_width;
881 child_allocation.width = relative_allocation.width;
882 child_allocation.height = hscrollbar_requisition.height;
883 child_allocation.x += allocation->x;
884 child_allocation.y += allocation->y;
886 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
888 child_allocation.x -= widget->style->xthickness;
889 child_allocation.width += 2 * widget->style->xthickness;
892 gtk_widget_size_allocate (scrolled_window->hscrollbar, &child_allocation);
894 else if (GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
895 gtk_widget_hide (scrolled_window->hscrollbar);
897 if (scrolled_window->vscrollbar_visible)
899 GtkRequisition vscrollbar_requisition;
900 if (!GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
901 gtk_widget_show (scrolled_window->vscrollbar);
903 gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
904 &vscrollbar_requisition);
906 if (scrolled_window->window_placement == GTK_CORNER_TOP_LEFT ||
907 scrolled_window->window_placement == GTK_CORNER_BOTTOM_LEFT)
908 child_allocation.x = (relative_allocation.x +
909 relative_allocation.width +
910 SCROLLBAR_SPACING (scrolled_window) +
911 (scrolled_window->shadow_type == GTK_SHADOW_NONE ?
912 0 : widget->style->xthickness));
914 child_allocation.x = GTK_CONTAINER (scrolled_window)->border_width;
916 child_allocation.y = relative_allocation.y;
917 child_allocation.width = vscrollbar_requisition.width;
918 child_allocation.height = relative_allocation.height;
919 child_allocation.x += allocation->x;
920 child_allocation.y += allocation->y;
922 if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
924 child_allocation.y -= widget->style->ythickness;
925 child_allocation.height += 2 * widget->style->ythickness;
928 gtk_widget_size_allocate (scrolled_window->vscrollbar, &child_allocation);
930 else if (GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
931 gtk_widget_hide (scrolled_window->vscrollbar);
935 gtk_scrolled_window_scroll_event (GtkWidget *widget,
936 GdkEventScroll *event)
940 g_return_val_if_fail (widget != NULL, FALSE);
941 g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
942 g_return_val_if_fail (event != NULL, FALSE);
944 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
945 range = GTK_SCROLLED_WINDOW (widget)->vscrollbar;
947 range = GTK_SCROLLED_WINDOW (widget)->hscrollbar;
949 if (range && GTK_WIDGET_VISIBLE (range))
951 GtkAdjustment *adj = GTK_RANGE (range)->adjustment;
954 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_LEFT)
955 new_value = adj->value - adj->page_increment / 2;
957 new_value = adj->value + adj->page_increment / 2;
959 new_value = CLAMP (new_value, adj->lower, adj->upper - adj->page_size);
960 gtk_adjustment_set_value (adj, new_value);
969 gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
972 GtkScrolledWindow *scrolled_win;
974 g_return_if_fail (adjustment != NULL);
975 g_return_if_fail (data != NULL);
977 scrolled_win = GTK_SCROLLED_WINDOW (data);
979 if (adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->hscrollbar)))
981 if (scrolled_win->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
985 visible = scrolled_win->hscrollbar_visible;
986 scrolled_win->hscrollbar_visible = (adjustment->upper - adjustment->lower >
987 adjustment->page_size);
988 if (scrolled_win->hscrollbar_visible != visible)
989 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
992 else if (adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->vscrollbar)))
994 if (scrolled_win->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
998 visible = scrolled_win->vscrollbar_visible;
999 scrolled_win->vscrollbar_visible = (adjustment->upper - adjustment->lower >
1000 adjustment->page_size);
1001 if (scrolled_win->vscrollbar_visible != visible)
1002 gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
1008 gtk_scrolled_window_add (GtkContainer *container,
1011 GtkScrolledWindow *scrolled_window;
1014 bin = GTK_BIN (container);
1015 g_return_if_fail (bin->child == NULL);
1017 scrolled_window = GTK_SCROLLED_WINDOW (container);
1020 gtk_widget_set_parent (child, GTK_WIDGET (bin));
1022 /* this is a temporary message */
1023 if (!gtk_widget_set_scroll_adjustments (child,
1024 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
1025 gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar))))
1026 g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
1027 "use gtk_scrolled_window_add_with_viewport() instead");
1029 if (GTK_WIDGET_REALIZED (child->parent))
1030 gtk_widget_realize (child);
1032 if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
1034 if (GTK_WIDGET_MAPPED (child->parent))
1035 gtk_widget_map (child);
1037 gtk_widget_queue_resize (child);
1042 gtk_scrolled_window_remove (GtkContainer *container,
1045 g_return_if_fail (container != NULL);
1046 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
1047 g_return_if_fail (child != NULL);
1048 g_return_if_fail (GTK_BIN (container)->child == child);
1050 gtk_widget_set_scroll_adjustments (child, NULL, NULL);
1052 /* chain parent class handler to remove child */
1053 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
1057 gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
1061 GtkWidget *viewport;
1063 g_return_if_fail (scrolled_window != NULL);
1064 g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
1065 g_return_if_fail (child != NULL);
1066 g_return_if_fail (GTK_IS_WIDGET (child));
1067 g_return_if_fail (child->parent == NULL);
1069 bin = GTK_BIN (scrolled_window);
1071 if (bin->child != NULL)
1073 g_return_if_fail (GTK_IS_VIEWPORT (bin->child));
1074 g_return_if_fail (GTK_BIN (bin->child)->child == NULL);
1076 viewport = bin->child;
1081 gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
1082 gtk_scrolled_window_get_vadjustment (scrolled_window));
1083 gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
1086 gtk_widget_show (viewport);
1087 gtk_container_add (GTK_CONTAINER (viewport), child);