Viewport.c 31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
/* $Xorg: Viewport.c,v 1.4 2001/02/09 02:03:47 xorgcvs Exp $ */

/***********************************************************

Copyright 1987, 1988, 1994, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/
/* $XFree86: xc/lib/Xaw/Viewport.c,v 1.10 2001/08/23 00:03:20 dawes Exp $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xmu/Misc.h>
#include <X11/Xaw/Scrollbar.h>
#include <X11/Xaw/ViewportP.h>
#include <X11/Xaw/XawInit.h>
#include "Private.h"

/*
 * Class Methods
 */
static Boolean Layout(FormWidget, unsigned int, unsigned int, Bool);
static void XawViewportChangeManaged(Widget);
static void XawViewportInitialize(Widget, Widget, ArgList, Cardinal*);
static void
XawViewportConstraintInitialize(Widget, Widget, ArgList, Cardinal*);
static XtGeometryResult XawViewportGeometryManager(Widget, XtWidgetGeometry*,
						   XtWidgetGeometry*);
static XtGeometryResult XawViewportQueryGeometry(Widget,
						 XtWidgetGeometry*,
						 XtWidgetGeometry*);
static void XawViewportRealize(Widget, XtValueMask*, XSetWindowAttributes*);
static void XawViewportResize(Widget);
static Boolean XawViewportSetValues(Widget, Widget, Widget,
				    ArgList, Cardinal*);

/*
 * Prototypes
 */
static void ComputeLayout(Widget, Bool, Bool);
static void ComputeWithForceBars(Widget, Bool, XtWidgetGeometry*,
				 int*, int*);
static Widget CreateScrollbar(ViewportWidget, Bool);
static XtGeometryResult GeometryRequestPlusScrollbar(ViewportWidget, Bool,
						     XtWidgetGeometry*,
						     XtWidgetGeometry*);
static Bool GetGeometry(Widget, unsigned int, unsigned int);
static void MoveChild(ViewportWidget, int, int);
static XtGeometryResult QueryGeometry(ViewportWidget, XtWidgetGeometry*,
				      XtWidgetGeometry*);
static void RedrawThumbs(ViewportWidget);
static void ScrollUpDownProc(Widget, XtPointer, XtPointer);
static void SendReport(ViewportWidget, unsigned int);
static void SetBar(Widget, int, unsigned int, unsigned int);
static XtGeometryResult TestSmaller(ViewportWidget, XtWidgetGeometry*,
				    XtWidgetGeometry*);
static void ThumbProc(Widget, XtPointer, XtPointer);

/*
 * Initialization
 */
#define offset(field) XtOffsetOf(ViewportRec, viewport.field)
static XtResource resources[] = {
  {
    XtNforceBars,
    XtCBoolean,
    XtRBoolean,
    sizeof(Boolean),
    offset(forcebars),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNallowHoriz,
    XtCBoolean,
    XtRBoolean,
    sizeof(Boolean),
    offset(allowhoriz),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNallowVert,
    XtCBoolean,
    XtRBoolean,
    sizeof(Boolean),
    offset(allowvert),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNuseBottom,
    XtCBoolean,
    XtRBoolean,
    sizeof(Boolean),
    offset(usebottom),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNuseRight,
    XtCBoolean,
    XtRBoolean,
    sizeof(Boolean),
    offset(useright),
    XtRImmediate,
    (XtPointer)False
  },
  {
    XtNreportCallback,
    XtCReportCallback,
    XtRCallback,
    sizeof(XtPointer),
    offset(report_callbacks),
    XtRImmediate,
    NULL
  },
};
#undef offset

#define Superclass	(&formClassRec)
ViewportClassRec viewportClassRec = {
  /* core */
  {
    (WidgetClass)Superclass,		/* superclass */
    "Viewport",				/* class_name */
    sizeof(ViewportRec),		/* widget_size */
    XawInitializeWidgetSet,		/* class_initialize */
    NULL,				/* class_part_init */
    False,				/* class_inited */
    XawViewportInitialize,		/* initialize */
    NULL,				/* initialize_hook */
    XawViewportRealize,			/* realize */
    NULL,				/* actions */
    0,					/* num_actions */
    resources,				/* resources */
    XtNumber(resources),		/* num_resources */
    NULLQUARK,				/* xrm_class */
    True,				/* compress_motion */
    True,				/* compress_exposure */
    True,				/* compress_enterleave */
    False,				/* visible_interest */
    NULL,				/* destroy */
    XawViewportResize,			/* resize */
    XtInheritExpose,			/* expose */
    XawViewportSetValues,		/* set_values */
    NULL,				/* set_values_hook */
    XtInheritSetValuesAlmost,		/* set_values_almost */
    NULL,				/* get_values_hook */
    NULL,				/* accept_focus */
    XtVersion,				/* version */
    NULL,				/* callback_private */
    NULL,				/* tm_table */
    XawViewportQueryGeometry,		/* query_geometry */
    XtInheritDisplayAccelerator,	/* display_accelerator */
    NULL,				/* extension */
  },
  /* composite */
  {
    XawViewportGeometryManager,		/* geometry_manager */
    XawViewportChangeManaged,		/* change_managed */
    XtInheritInsertChild,		/* insert_child */
    XtInheritDeleteChild,		/* delete_child */
    NULL,				/* extension */
  },
  /* constraint */
  {
    NULL,				/* subresourses */
    0,					/* subresource_count */
    sizeof(ViewportConstraintsRec),	/* constraint_size */
    XawViewportConstraintInitialize,	/* initialize */
    NULL,				/* destroy */
    NULL,				/* set_values */
    NULL,				/* extension */
  },
  /* form */
  {
    Layout,				/* layout */
  },
  /* viewport */
  {
    NULL,				/* extension */
  },
};

WidgetClass viewportWidgetClass = (WidgetClass)&viewportClassRec;

/*
 * Implementation
 */
static Widget
CreateScrollbar(ViewportWidget w, Bool horizontal)
{
    static Arg barArgs[] = {
	{XtNorientation,	    0},
	{XtNlength,		    0},
	{XtNleft,		    0},
	{XtNright,		    0},
	{XtNtop,		    0},
	{XtNbottom,		    0},
	{XtNmappedWhenManaged,	    False},
    };
    Widget clip = w->viewport.clip;
    ViewportConstraints constraints =
	(ViewportConstraints)clip->core.constraints;
    Widget bar;

    XtSetArg(barArgs[0], XtNorientation,
	   horizontal ? XtorientHorizontal : XtorientVertical);
    XtSetArg(barArgs[1], XtNlength,
	   horizontal ? XtWidth(clip) : XtHeight(clip));
    XtSetArg(barArgs[2], XtNleft,
	   !horizontal && w->viewport.useright ? XtChainRight : XtChainLeft);
    XtSetArg(barArgs[3], XtNright,
	   !horizontal && !w->viewport.useright ? XtChainLeft : XtChainRight);
    XtSetArg(barArgs[4], XtNtop,
	   horizontal && w->viewport.usebottom ? XtChainBottom: XtChainTop);
    XtSetArg(barArgs[5], XtNbottom,
	   horizontal && !w->viewport.usebottom ? XtChainTop: XtChainBottom);

    bar = XtCreateWidget(horizontal ? "horizontal" : "vertical",
			 scrollbarWidgetClass, (Widget)w,
			 barArgs, XtNumber(barArgs));
    XtAddCallback(bar, XtNscrollProc, ScrollUpDownProc, (XtPointer)w);
    XtAddCallback(bar, XtNjumpProc, ThumbProc, (XtPointer)w);

    if (horizontal) {
	w->viewport.horiz_bar = bar;
	constraints->form.vert_base = bar;
    }
    else {
	w->viewport.vert_bar = bar;
	constraints->form.horiz_base = bar;
    }

    XtManageChild(bar);

    return (bar);
}

/*ARGSUSED*/
static void
XawViewportInitialize(Widget request, Widget cnew,
		      ArgList args, Cardinal *num_args)
{
    ViewportWidget w = (ViewportWidget)cnew;
    static Arg clip_args[8];
    Cardinal arg_cnt;
    Widget h_bar, v_bar;
    Dimension clip_height, clip_width;

    w->form.default_spacing = 0; /* Reset the default spacing to 0 pixels */

    /*
     * Initialize all widget pointers to NULL
     */
    w->viewport.child = NULL;
    w->viewport.horiz_bar = w->viewport.vert_bar = NULL;

    /*
     * Create Clip Widget
     */
    arg_cnt = 0;
    XtSetArg(clip_args[arg_cnt], XtNbackgroundPixmap, None);	arg_cnt++;
    XtSetArg(clip_args[arg_cnt], XtNborderWidth, 0);		arg_cnt++;
    XtSetArg(clip_args[arg_cnt], XtNleft, XtChainLeft);		arg_cnt++;
    XtSetArg(clip_args[arg_cnt], XtNright, XtChainRight);	arg_cnt++;
    XtSetArg(clip_args[arg_cnt], XtNtop, XtChainTop);		arg_cnt++;
    XtSetArg(clip_args[arg_cnt], XtNbottom, XtChainBottom);	arg_cnt++;
    XtSetArg(clip_args[arg_cnt], XtNwidth, XtWidth(w));		arg_cnt++;
    XtSetArg(clip_args[arg_cnt], XtNheight, XtHeight(w));	arg_cnt++;

    w->viewport.clip = XtCreateManagedWidget("clip", widgetClass, cnew,
					     clip_args, arg_cnt);

    if (!w->viewport.forcebars) 
	return;		 /* If we are not forcing the bars then we are done */

    if (w->viewport.allowhoriz) 
	(void)CreateScrollbar(w, True);
    if (w->viewport.allowvert) 
	(void)CreateScrollbar(w, False);

    h_bar = w->viewport.horiz_bar;
    v_bar = w->viewport.vert_bar;

    /*
     * Set the clip widget to the correct height
     */
    clip_width = XtWidth(w);
    clip_height = XtHeight(w);

    if (h_bar != NULL &&  XtWidth(w) > XtWidth(h_bar) + XtBorderWidth(h_bar))
	clip_width -= XtWidth(h_bar) + XtBorderWidth(h_bar);

    if (v_bar != NULL && XtHeight(w) > XtHeight(v_bar) + XtBorderWidth(v_bar))
	clip_height -= XtHeight(v_bar) + XtBorderWidth(v_bar);

    arg_cnt = 0;
    XtSetArg(clip_args[arg_cnt], XtNwidth, clip_width); arg_cnt++;
    XtSetArg(clip_args[arg_cnt], XtNheight, clip_height); arg_cnt++;
    XtSetValues(w->viewport.clip, clip_args, arg_cnt);
}

/*ARGSUSED*/
static void
XawViewportConstraintInitialize(Widget request, Widget cnew,
				ArgList args, Cardinal *num_args)
{
    ((ViewportConstraints)cnew->core.constraints)->viewport.reparented = False;
}

static void
XawViewportRealize(Widget widget, XtValueMask *value_mask,
		   XSetWindowAttributes *attributes)
{
    ViewportWidget w = (ViewportWidget)widget;
    Widget child = w->viewport.child;
    Widget clip = w->viewport.clip;

    *value_mask |= CWBitGravity;
    attributes->bit_gravity = NorthWestGravity;
    (*Superclass->core_class.realize)(widget, value_mask, attributes);

    (*w->core.widget_class->core_class.resize)(widget);	/* turn on bars */

    if (child != NULL) {
	XtMoveWidget(child, 0, 0);
	XtRealizeWidget(clip);
	XtRealizeWidget(child);
	XReparentWindow(XtDisplay(w), XtWindow(child), XtWindow(clip), 0, 0);
	XtMapWidget(child);
    }
}

/*ARGSUSED*/
static Boolean
XawViewportSetValues(Widget current, Widget request, Widget cnew,
		     ArgList args, Cardinal *num_args)
{
    ViewportWidget w = (ViewportWidget)cnew;
    ViewportWidget cw = (ViewportWidget)current;

    if (w->viewport.forcebars != cw->viewport.forcebars
	|| w->viewport.allowvert != cw->viewport.allowvert
	|| w->viewport.allowhoriz != cw->viewport.allowhoriz
	|| w->viewport.useright != cw->viewport.useright
	|| w->viewport.usebottom != cw->viewport.usebottom)
	(*w->core.widget_class->core_class.resize)(cnew); /* Recompute layout */

    return (False);
}

static void
XawViewportChangeManaged(Widget widget)
{
    ViewportWidget w = (ViewportWidget)widget;
    int num_children = w->composite.num_children;
    Widget child, *childP;
    int i;

    child = NULL;
    for (childP = w->composite.children,
	 i = 0; i < num_children;
	 childP++, i++) {
	if (XtIsManaged(*childP)
	    && *childP != w->viewport.clip
	    && *childP != w->viewport.horiz_bar
	    && *childP != w->viewport.vert_bar)	{
	    child = *childP;
	    break;
	}
    }

    if (child != w->viewport.child) {
	w->viewport.child = child;
	if (child != NULL) {
	    XtResizeWidget(child, XtWidth(child), XtHeight(child), 0);
	    if (XtIsRealized(widget)) {
		ViewportConstraints constraints =
		    (ViewportConstraints)child->core.constraints;
		if (!XtIsRealized(child)) {
		    Window window = XtWindow(w);

		    XtMoveWidget(child, 0, 0);
		    w->core.window = XtWindow(w->viewport.clip);
		    XtRealizeWidget(child);
		    w->core.window = window;
		    constraints->viewport.reparented = True;
		}
		else if (!constraints->viewport.reparented) {
		    XReparentWindow(XtDisplay(w), XtWindow(child),
				    XtWindow(w->viewport.clip), 0, 0);
		    constraints->viewport.reparented = True;
		    if (child->core.mapped_when_managed)
		    XtMapWidget(child);
		}
	    }
	    GetGeometry(widget, XtWidth(child), XtHeight(child));
	    (*((ViewportWidgetClass)w->core.widget_class)->form_class.layout)
	    ((FormWidget)w, XtWidth(w), XtHeight(w), True /* True? */);
	}
    }

#ifdef notdef
    (*Superclass->composite_class.change_managed)(widget);
#endif
}

static void
SetBar(Widget w, int top, unsigned int length, unsigned int total)
{
    XawScrollbarSetThumb(w, (float)top / (float)total,
			 (float)length / (float)total);
}

static void
RedrawThumbs(ViewportWidget w)
{
    Widget child = w->viewport.child;
    Widget clip = w->viewport.clip;

    if (w->viewport.horiz_bar != NULL)
	SetBar(w->viewport.horiz_bar, -(int)XtX(child),
	       XtWidth(clip), XtWidth(child));

    if (w->viewport.vert_bar != NULL)
	SetBar(w->viewport.vert_bar, -(int)XtY(child),
	       XtHeight(clip), XtHeight(child));
}

static void
SendReport(ViewportWidget w, unsigned int changed)
{
    XawPannerReport rep;

    if (w->viewport.report_callbacks) {
	Widget child = w->viewport.child;
	Widget clip = w->viewport.clip;

	rep.changed = changed;
	rep.slider_x = -XtX(child);	/* child is canvas */
	rep.slider_y = -XtY(child);	/* clip is slider */
	rep.slider_width = XtWidth(clip);
	rep.slider_height = XtHeight(clip);
	rep.canvas_width = XtWidth(child);
	rep.canvas_height = XtHeight(child);
	XtCallCallbackList((Widget)w, w->viewport.report_callbacks,
			   (XtPointer)&rep);
    }
}

static void
MoveChild(ViewportWidget w, int x, int y)
{
    Widget child = w->viewport.child;
    Widget clip = w->viewport.clip;

    /* make sure we never move past right/bottom borders */
    if (-x + (int)XtWidth(clip) > XtWidth(child))
	x = -(int)(XtWidth(child) - XtWidth(clip));

    if (-y + (int)XtHeight(clip) > XtHeight(child))
	y = -(int)(XtHeight(child) - XtHeight(clip));

    /* make sure we never move past left/top borders */
    if (x >= 0)
	x = 0;
    if (y >= 0)
	y = 0;

    XtMoveWidget(child, x, y);
    SendReport(w, (XawPRSliderX | XawPRSliderY));

    RedrawThumbs(w);
}

static void
ComputeLayout(Widget widget, Bool query, Bool destroy_scrollbars)
{
    ViewportWidget w = (ViewportWidget)widget;
    Widget child = w->viewport.child;
    Widget clip = w->viewport.clip;
    ViewportConstraints constraints =
	(ViewportConstraints)clip->core.constraints;
    Bool needshoriz, needsvert;
    int clip_width, clip_height;
    XtWidgetGeometry intended;

    if (child == NULL)
	return;

    clip_width = XtWidth(w);
    clip_height = XtHeight(w);
    intended.request_mode = CWBorderWidth;
    intended.border_width = 0;

    if (w->viewport.forcebars) {
	needsvert = w->viewport.allowvert;
	needshoriz = w->viewport.allowhoriz;
	ComputeWithForceBars(widget, query, &intended, 
			     &clip_width, &clip_height);
    }
    else {
	Dimension prev_width, prev_height;
	XtGeometryMask prev_mode;
	XtWidgetGeometry preferred;

	needshoriz = needsvert = False;

	/*
	 * intended.{width,height} caches the eventual child dimensions,
	 * but we don't set the mode bits until after we decide that the
	 * child's preferences are not acceptable
	 */
	if (!w->viewport.allowhoriz) 
	    intended.request_mode |= CWWidth;

	if (XtWidth(child) < clip_width)
	    intended.width = clip_width;
	else
	    intended.width = XtWidth(child);

	if (XtHeight(child) < clip_height)
	    intended.height = clip_height;
	else
	    intended.height = XtHeight(child);

	if (!w->viewport.allowvert) 
	    intended.request_mode |= CWHeight;

	if (!query) {
	    preferred.width = XtWidth(child);
	    preferred.height = XtHeight(child);
	}
	do { /* while intended != prev  */
	    if (query) {
		(void)XtQueryGeometry(child, &intended, &preferred);
		if (!(preferred.request_mode & CWWidth))
		    preferred.width = intended.width;
		if (!(preferred.request_mode & CWHeight))
		    preferred.height = intended.height;
	    }
	    prev_width = intended.width;
	    prev_height = intended.height;
	    prev_mode = intended.request_mode;
	    /*
	     * note that having once decided to turn on either bar
	     * we'll not change our mind until we're next resized,
	     * thus avoiding potential oscillations
	     */
#define CheckHoriz() \
	    if (w->viewport.allowhoriz &&				\
		preferred.width > clip_width) {				\
		if (!needshoriz) {					\
		    Widget bar;						\
									\
		    needshoriz = True;					\
		    if ((bar = w->viewport.horiz_bar) == NULL)		\
			bar = CreateScrollbar(w, True);			\
		    clip_height -= XtHeight(bar) + XtBorderWidth(bar);	\
		    if (clip_height < 1)				\
			clip_height = 1;				\
		}							\
		intended.width = preferred.width;			\
	    }

	    CheckHoriz();
	    if (w->viewport.allowvert && preferred.height > clip_height) {
		if (!needsvert) {
		    Widget bar;
		    needsvert = True;
		    if ((bar = w->viewport.vert_bar) == NULL)
			bar = CreateScrollbar(w, False);
		    clip_width -= XtWidth(bar) + XtBorderWidth(bar);
		    if (clip_width < 1)
			clip_width = 1;
		    CheckHoriz();
		}
		intended.height = preferred.height;
	    }
	    if (!w->viewport.allowhoriz || preferred.width < clip_width) {
		intended.width = clip_width;
		intended.request_mode |= CWWidth;
	    }
	    if (!w->viewport.allowvert || preferred.height < clip_height) {
		intended.height = clip_height;
		intended.request_mode |= CWHeight;
	    }
	} while (intended.request_mode != prev_mode
		 || (intended.request_mode & CWWidth
		     && intended.width != prev_width)
		 || (intended.request_mode & CWHeight
		     && intended.height != prev_height));
    }

    if (XtIsRealized(clip))
	XRaiseWindow(XtDisplay(clip), XtWindow(clip));

    XtMoveWidget(clip,
		 needsvert ? w->viewport.useright ? 0 :
		 XtWidth(w->viewport.vert_bar)
		 + XtBorderWidth(w->viewport.vert_bar) : 0,
		 needshoriz ? w->viewport.usebottom ? 0 :
		 XtHeight(w->viewport.horiz_bar)
		 + XtBorderWidth(w->viewport.horiz_bar) : 0);
    XtResizeWidget(clip, clip_width, clip_height, 0);
	
    if (w->viewport.horiz_bar != NULL) {
	Widget bar = w->viewport.horiz_bar;

	if (!needshoriz) {
	    constraints->form.vert_base = NULL;
	    if (destroy_scrollbars) {
		XtDestroyWidget(bar);
		w->viewport.horiz_bar = NULL;
	    }
	}
	else {
	    int bw = XtBorderWidth(bar);

	    XtResizeWidget(bar, clip_width, XtHeight(bar), bw);
	    XtMoveWidget(bar,
			 needsvert && !w->viewport.useright
			 ? XtWidth(w->viewport.vert_bar) : -bw,
			 w->viewport.usebottom
			 ? XtHeight(w) - XtHeight(bar) - bw : -bw);
	    XtSetMappedWhenManaged(bar, True);
	}
    }

    if (w->viewport.vert_bar != NULL) {
	Widget bar = w->viewport.vert_bar;

	if (!needsvert)	{
	    constraints->form.horiz_base = NULL;
	    if (destroy_scrollbars) {
		XtDestroyWidget(bar);
		w->viewport.vert_bar = NULL;
	    }
	}
	else {
	    int bw = bar->core.border_width;

	    XtResizeWidget(bar, XtWidth(bar), clip_height, bw);
	    XtMoveWidget(bar,
			w->viewport.useright
			? XtWidth(w) - XtWidth(bar) - bw : -bw,
			needshoriz && !w->viewport.usebottom
			? XtHeight(w->viewport.horiz_bar) : -bw);
	   XtSetMappedWhenManaged(bar, True);
	}
    }

    if (child != NULL) {
	XtResizeWidget(child, intended.width, intended.height, 0);
	MoveChild(w, needshoriz ? XtX(child) : 0,	needsvert ? XtY(child) : 0);
    }

    SendReport (w, XawPRAll);
}

/*
 * Function:
 *	ComputeWithForceBars
 *
 * Parameters:
 *	widget	    - viewport widget
 *	query	    - whether or not to query the child
 *	intended    - cache of the childs height is stored here
 *		      (used and returned)
 *	clip_width  - size of clip window (used and returned)
 *	clip_height - ""
 *
 * Description:
 *	Computes the layout give forcebars is set.
 */
static void
ComputeWithForceBars(Widget widget, Bool query, XtWidgetGeometry *intended,
		     int *clip_width, int *clip_height)
{
    ViewportWidget w = (ViewportWidget)widget;
    Widget child = w->viewport.child;
    XtWidgetGeometry preferred;

    /*
     * If forcebars then needs = allows = has
     * Thus if needsvert is set it MUST have a scrollbar
     */
    if (w->viewport.allowvert) {
	if (w->viewport.vert_bar == NULL) 
	    w->viewport.vert_bar = CreateScrollbar(w, False);

	*clip_width -= XtWidth(w->viewport.vert_bar) +
		       XtBorderWidth(w->viewport.vert_bar);
    }

    if (w->viewport.allowhoriz) {
	if (w->viewport.horiz_bar == NULL) 
	    w->viewport.horiz_bar = CreateScrollbar(w, True);

	*clip_height -= XtHeight(w->viewport.horiz_bar) +
			XtBorderWidth(w->viewport.horiz_bar);
    }

    AssignMax(*clip_width, 1);
    AssignMax(*clip_height, 1);

    if (!w->viewport.allowvert) {
	intended->height = *clip_height;
	intended->request_mode = CWHeight;
    }
    if (!w->viewport.allowhoriz) {
	intended->width = *clip_width;
	intended->request_mode = CWWidth;
    }

    if (query) {
	if (w->viewport.allowvert || w->viewport.allowhoriz) {
	    XtQueryGeometry(child, intended, &preferred);
	  
	    if (!(intended->request_mode & CWWidth)) {
		if (preferred.request_mode & CWWidth)
		    intended->width = preferred.width;
		else
		    intended->width = XtWidth(child);
	    }

	    if (!(intended->request_mode & CWHeight)) {
		if (preferred.request_mode & CWHeight)
		    intended->height = preferred.height;
		else
		    intended->height = XtHeight(child);
	    }
	}
    }
    else {
	if (w->viewport.allowvert)
	    intended->height = XtHeight(child);
	if (w->viewport.allowhoriz)
	    intended->width = XtWidth(child);
    }

    if (*clip_width > (int)intended->width)
	intended->width = *clip_width;
    if (*clip_height > (int)intended->height)
	intended->height = *clip_height;
}

static void
XawViewportResize(Widget widget)
{
    ComputeLayout(widget, True, True);
}

/*ARGSUSED*/
static Boolean
Layout(FormWidget w, unsigned int width, unsigned int height, Bool force)
{
    ComputeLayout((Widget)w, True, True);
    w->form.preferred_width = XtWidth(w);
    w->form.preferred_height = XtHeight(w);

    return (False);
}

static void
ScrollUpDownProc(Widget widget, XtPointer closure, XtPointer call_data)
{
    ViewportWidget w = (ViewportWidget)closure;
    Widget child = w->viewport.child;
    int pix = (long)call_data;
    int x, y;

    if (child == NULL)
	return;

    x = XtX(child) - (widget == w->viewport.horiz_bar ? pix : 0);
    y = XtY(child) - (widget == w->viewport.vert_bar ? pix : 0);
    MoveChild(w, x, y);
}

/*ARGSUSED*/
static void
ThumbProc(Widget widget, XtPointer closure, XtPointer call_data)
{
    ViewportWidget w = (ViewportWidget)closure;
    Widget child = w->viewport.child;
    float percent = *(float *)call_data;
    int x, y;

    if (child == NULL)
	return;

    if (widget == w->viewport.horiz_bar)
	x = -percent * XtWidth(child);
    else
	x = XtX(child);

    if (widget == w->viewport.vert_bar)
	y = -percent * XtHeight(child);
    else
	y = XtY(child);

    MoveChild(w, x, y);
}

static XtGeometryResult
TestSmaller(ViewportWidget w, XtWidgetGeometry *request,
	    XtWidgetGeometry *reply_return)
{
    if (request->width < XtWidth(w) || request->height < XtHeight(w))
	return (XtMakeGeometryRequest((Widget)w, request, reply_return));

    return (XtGeometryYes);
}

static XtGeometryResult
GeometryRequestPlusScrollbar(ViewportWidget w, Bool horizontal,
			     XtWidgetGeometry *request,
			     XtWidgetGeometry *reply_return)
{
    Widget sb;
    XtWidgetGeometry plusScrollbars;

    plusScrollbars = *request;
    if ((sb = w->viewport.horiz_bar) == NULL)
	sb = CreateScrollbar(w, horizontal);
    request->width += XtWidth(sb);
    request->height += XtHeight(sb);
    XtDestroyWidget(sb);
    return (XtMakeGeometryRequest((Widget)w, &plusScrollbars, reply_return));
}

#define WidthChange()	(request->width != XtWidth(w))
#define HeightChange()	(request->height != XtHeight(w))
static XtGeometryResult 
QueryGeometry(ViewportWidget w, XtWidgetGeometry *request,
	      XtWidgetGeometry *reply_return)
{	
    if (w->viewport.allowhoriz && w->viewport.allowvert) 
	return (TestSmaller(w, request, reply_return));

    else if (w->viewport.allowhoriz && !w->viewport.allowvert) {
	if (WidthChange() && !HeightChange())
	    return (TestSmaller(w, request, reply_return));
	else if (!WidthChange() && HeightChange())
	    return (XtMakeGeometryRequest((Widget)w, request, reply_return));
	else if (WidthChange() && HeightChange())
	    return (GeometryRequestPlusScrollbar(w, True, request, reply_return));
	else /* !WidthChange() && !HeightChange() */
	    return (XtGeometryYes);
    }
    else if (!w->viewport.allowhoriz && w->viewport.allowvert) {
	if (!WidthChange() && HeightChange())
	    return (TestSmaller(w, request, reply_return));
	else if (WidthChange() && !HeightChange())
	    return (XtMakeGeometryRequest((Widget)w, request, reply_return));
	else if (WidthChange() && HeightChange())
	    return (GeometryRequestPlusScrollbar(w, False, request, reply_return));
	else /* !WidthChange() && !HeightChange() */
	    return (XtGeometryYes);
    }
    else /* (!w->viewport.allowhoriz && !w->viewport.allowvert) */
	return (XtMakeGeometryRequest((Widget)w, request, reply_return));
}
#undef WidthChange
#undef HeightChange

static XtGeometryResult
XawViewportGeometryManager(Widget child, XtWidgetGeometry *request,
			   XtWidgetGeometry *reply)
{
    ViewportWidget w = (ViewportWidget)child->core.parent;
    Bool rWidth = (request->request_mode & CWWidth) != 0;
    Bool rHeight = (request->request_mode & CWHeight) != 0;
    XtWidgetGeometry allowed;
    XtGeometryResult result;
    Bool reconfigured;
    Bool child_changed_size;
    unsigned int height_remaining;

    if (request->request_mode & XtCWQueryOnly)
	return (QueryGeometry(w, request, reply));

    if (child != w->viewport.child
	|| request->request_mode & ~(CWWidth | CWHeight | CWBorderWidth)
	|| ((request->request_mode & CWBorderWidth)
	    && request->border_width > 0))
	return (XtGeometryNo);

    allowed = *request;

    reconfigured = GetGeometry((Widget)w,
				rWidth ? request->width : XtWidth(w),
				rHeight ? request->height : XtHeight(w));

    child_changed_size = (rWidth && XtWidth(child) != request->width) ||
			 (rHeight && XtHeight(child) != request->height);

    height_remaining = XtHeight(w);
    if (rWidth && XtWidth(w) != request->width) {
	if (w->viewport.allowhoriz && request->width > XtWidth(w)) {
	    /* horizontal scrollbar will be needed so possibly reduce height */
	    Widget bar; 

	    if ((bar = w->viewport.horiz_bar) == NULL)
		bar = CreateScrollbar(w, True);
	    height_remaining -= XtHeight(bar) + XtBorderWidth(bar);
	    reconfigured = True;
	}
	else
	    allowed.width = XtWidth(w);
    }
    if (rHeight && height_remaining != request->height) {
	if (w->viewport.allowvert && request->height > height_remaining) {
	    /* vertical scrollbar will be needed, so possibly reduce width */
	    if (!w->viewport.allowhoriz || request->width < XtWidth(w)) {
		Widget bar;

		if ((bar = w->viewport.vert_bar) == NULL)
		    bar = CreateScrollbar(w, False);
		if (!rWidth) {
		    allowed.width = XtWidth(w);
		    allowed.request_mode |= CWWidth;
		}
		if (allowed.width  >  XtWidth(bar) + XtBorderWidth(bar))
		    allowed.width -= XtWidth(bar) + XtBorderWidth(bar);
		else
		    allowed.width = 1;
		reconfigured = True;
	    }
	}
	else
	    allowed.height = height_remaining;
    }

    if (allowed.width != request->width || allowed.height != request->height) {
	*reply = allowed;
	result = XtGeometryAlmost;
    }
    else {
	if (rWidth)
	    XtWidth(child) = request->width;
	if (rHeight)
	    XtHeight(child) = request->height;
	result = XtGeometryYes;
    }

    if (reconfigured || child_changed_size)
	ComputeLayout((Widget)w, False, result == XtGeometryYes);

    return (result);
}

static Bool
GetGeometry(Widget w, unsigned int width, unsigned int height)
{
    XtWidgetGeometry geometry, return_geom;
    XtGeometryResult result;

    if (width == XtWidth(w) && height == XtHeight(w))
	return (False);

    geometry.request_mode = CWWidth | CWHeight;
    geometry.width = width;
    geometry.height = height;

    if (XtIsRealized(w)) {
	if (((ViewportWidget)w)->viewport.allowhoriz && width > XtWidth(w))
	    geometry.width = XtWidth(w);
	if (((ViewportWidget)w)->viewport.allowvert && height > XtHeight(w))
	    geometry.height = XtHeight(w);
    }
    else {
	/* This is the Realize call; we'll inherit a w&h iff none currently */
	if (XtWidth(w) != 0) {
	    if (XtHeight(w) != 0)
		return (False);
	    geometry.width = XtWidth(w);
	}
	if (XtHeight(w) != 0)
	    geometry.height = XtHeight(w);
    }

    result = XtMakeGeometryRequest(w, &geometry, &return_geom);
    if (result == XtGeometryAlmost)
	result = XtMakeGeometryRequest(w, &return_geom, NULL);

    return (result == XtGeometryYes);
}

static XtGeometryResult
XawViewportQueryGeometry(Widget w, XtWidgetGeometry *constraints,
			 XtWidgetGeometry *reply)
{
    if (((ViewportWidget)w)->viewport.child != NULL)
	return (XtQueryGeometry(((ViewportWidget)w)->viewport.child,
				constraints, reply));

    return (XtGeometryYes);
}

void
XawViewportSetLocation
(
 Widget gw,
#if NeedWidePrototypes
 double xoff, double yoff
#else
 float xoff, float yoff
#endif
 )
{
    ViewportWidget w = (ViewportWidget)gw;
    Widget child = w->viewport.child;
    int x, y;

    if (xoff > 1.0)			/* scroll to right */
	x = XtWidth(child);
    else if (xoff < 0.0)		/* if the offset is < 0.0 nothing */ 
	x = XtX(child);
    else
	x = (float)XtWidth(child) * xoff;

    if (yoff > 1.0) 
	y = XtHeight(child);
    else if (yoff < 0.0)
	y = XtY(child);
    else
	y = (float)XtHeight(child) * yoff;

    MoveChild (w, -x, -y);
}

void
XawViewportSetCoordinates(Widget gw,
#if NeedWidePrototypes
	int x, int y
#else
	Position x, Position y
#endif
)
{
    ViewportWidget w = (ViewportWidget)gw;
    Widget child = w->viewport.child;

    if (x > XtWidth(child))
	x = XtWidth(child);
    else if (x < 0)
	x = XtX(child);

    if (y > XtHeight(child))
	y = XtHeight(child);
    else if (y < 0)
	y = XtY(child);

    MoveChild (w, -x, -y);
}