Form.c 29.6 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 1103 1104 1105 1106 1107 1108 1109 1110
/* $Xorg: Form.c,v 1.4 2001/02/09 02:03:43 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/Form.c,v 1.20 2001/02/05 22:38:04 paulo Exp $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xmu/CharSet.h>
#include <X11/Xmu/Converters.h>
#include <X11/Xaw/FormP.h>
#include <X11/Xaw/XawInit.h>
#include "Private.h"

/*
 * Class Methods
 */
static void XawFormChangeManaged(Widget);
static void XawFormClassInitialize(void);
static void XawFormClassPartInitialize(WidgetClass);
static void XawFormConstraintInitialize(Widget, Widget, ArgList, Cardinal*);
static Boolean XawFormConstraintSetValues(Widget, Widget, Widget,
					  ArgList, Cardinal*);
static XtGeometryResult XawFormGeometryManager(Widget, XtWidgetGeometry*,
					       XtWidgetGeometry*);
static void XawFormInitialize(Widget, Widget, ArgList, Cardinal*);
#ifndef OLDXAW
static void XawFormRealize(Widget, Mask*, XSetWindowAttributes*);
static void XawFormRedisplay(Widget, XEvent*, Region);
#endif
static XtGeometryResult XawFormQueryGeometry(Widget, XtWidgetGeometry*,
					     XtWidgetGeometry*);
static void XawFormResize(Widget);
static Boolean XawFormSetValues(Widget, Widget, Widget,	ArgList, Cardinal*);
static Boolean Layout(FormWidget, unsigned int, unsigned int, Bool);

/*
 * Prototypes
 */
static void _CvtStringToEdgeType(XrmValuePtr, Cardinal*,
				 XrmValuePtr, XrmValuePtr);
static Bool ChangeFormGeometry(Widget, Bool, unsigned int, unsigned int,
			       Dimension*, Dimension*);
Boolean CvtEdgeTypeToString(Display*, XrmValuePtr, Cardinal*,
			    XrmValuePtr, XrmValuePtr, XtPointer*);
static void LayoutChild(Widget);
static int TransformCoord(int, unsigned int, unsigned int, XtEdgeType);
static void ResizeChildren(Widget);

/*
 * Initialization
 */
#ifndef OLDXAW
static XtActionsRec actions[] = {
  {"set-values", XawSetValuesAction},
  {"get-values", XawGetValuesAction},
  {"declare",    XawDeclareAction},
  {"call-proc",  XawCallProcAction},
};
#endif

static XrmQuark	QchainLeft, QchainRight, QchainTop, QchainBottom, Qrubber;

#define default_value	-99999
#define Offset(field) XtOffsetOf(FormRec, form.field)
static XtResource resources[] = {
  {
    XtNdefaultDistance,
    XtCThickness,
    XtRInt,
    sizeof(int),
    Offset(default_spacing),
    XtRImmediate,
    (XtPointer)4
  },
#ifndef OLDXAW
  {
    XawNdisplayList,
    XawCDisplayList,
    XawRDisplayList,
    sizeof(XawDisplayList*),
    Offset(display_list),
    XtRImmediate,
    NULL
  },
#endif
};
#undef Offset

#define defEdge		XtRubber

#define Offset(field) XtOffsetOf(FormConstraintsRec, form.field)
static XtResource formConstraintResources[] = {
  {
    XtNtop,
    XtCEdge,
    XtREdgeType,
    sizeof(XtEdgeType),
    Offset(top),
    XtRImmediate,
    (XtPointer)defEdge
  },
  {
    XtNbottom,
    XtCEdge,
    XtREdgeType,
    sizeof(XtEdgeType),
    Offset(bottom),
    XtRImmediate,
    (XtPointer)defEdge
  },
  {
    XtNleft,
    XtCEdge,
    XtREdgeType,
    sizeof(XtEdgeType),
    Offset(left),
    XtRImmediate,
    (XtPointer)defEdge
  },
  {
    XtNright,
    XtCEdge,
    XtREdgeType,
    sizeof(XtEdgeType),
    Offset(right),
    XtRImmediate,
    (XtPointer)defEdge
  },
  {
    XtNhorizDistance,
    XtCThickness,
    XtRInt,
    sizeof(int),
    Offset(dx),
    XtRImmediate,
    (XtPointer)default_value
  },
  {
    XtNfromHoriz,
    XtCWidget,
    XtRWidget,
    sizeof(Widget),
    Offset(horiz_base),
    XtRWidget,
    NULL
  },
  {
    XtNvertDistance,
    XtCThickness,
    XtRInt,
    sizeof(int),
    Offset(dy),
    XtRImmediate,
    (XtPointer)default_value
  },
  {
    XtNfromVert,
    XtCWidget,
    XtRWidget,
    sizeof(Widget),
    Offset(vert_base),
    XtRWidget,
    NULL
  },
  {
    XtNresizable,
    XtCBoolean,
    XtRBoolean,
    sizeof(Boolean),
    Offset(allow_resize),
    XtRImmediate,
    (XtPointer)False
  },
};
#undef Offset

FormClassRec formClassRec = {
  /* core */
  {
    (WidgetClass)&constraintClassRec,	/* superclass */
    "Form",				/* class_name */
    sizeof(FormRec),			/* widget_size */
    XawFormClassInitialize,		/* class_initialize */
    XawFormClassPartInitialize,		/* class_part_init */
    False,				/* class_inited */
    XawFormInitialize,			/* initialize */
    NULL,				/* initialize_hook */
#ifndef OLDXAW
    XawFormRealize,			/* realize */
    actions,				/* actions */
    XtNumber(actions),			/* num_actions */
#else
    XtInheritRealize,			/* realize */
    NULL,				/* actions */
    0,					/* num_actions */
#endif
    resources,				/* resources */
    XtNumber(resources),		/* num_resources */
    NULLQUARK,				/* xrm_class */
    True,				/* compress_motion */
    True,				/* compress_exposure */
    True,				/* compress_enterleave */
    False,				/* visible_interest */
    NULL,				/* destroy */
    XawFormResize,			/* resize */
#ifndef OLDXAW
    XawFormRedisplay,			/* expose */
#else
    XtInheritExpose,			/* expose */
#endif
    XawFormSetValues,			/* 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 */
    XawFormQueryGeometry,		/* query_geometry */
    XtInheritDisplayAccelerator,	/* display_accelerator */
    NULL,				/* extension */
  },
  /* composite */
  {
    XawFormGeometryManager,		/* geometry_manager */
    XawFormChangeManaged,		/* change_managed */
    XtInheritInsertChild,		/* insert_child */
    XtInheritDeleteChild,		/* delete_child */
    NULL,				/* extension */
  },
  /* constraint */
  {
    formConstraintResources,		/* subresourses */
    XtNumber(formConstraintResources),	/* subresource_count */
    sizeof(FormConstraintsRec),		/* constraint_size */
    XawFormConstraintInitialize,	/* initialize */
    NULL,				/* destroy */
    XawFormConstraintSetValues,		/* set_values */
    NULL,				/* extension */
  },
  /* form */
  {
    Layout,				/* layout */
  },
};

WidgetClass formWidgetClass = (WidgetClass)&formClassRec;

/*
 * Implementation
 */
#ifndef OLDXAW
static void
XawFormRealize(Widget w, Mask *mask, XSetWindowAttributes *attr)
{
    XawPixmap *pixmap;

    (*formWidgetClass->core_class.superclass->core_class.realize)(w, mask, attr);

    if (w->core.background_pixmap > XtUnspecifiedPixmap) {
	pixmap = XawPixmapFromXPixmap(w->core.background_pixmap, XtScreen(w),
				      w->core.colormap, w->core.depth);
	if (pixmap && pixmap->mask)
	    XawReshapeWidget(w, pixmap);
    }
}

static void
XawFormRedisplay(Widget w, XEvent *event, Region region)
{
    FormWidget xaw = (FormWidget)w;

    if (xaw->form.display_list)
	XawRunDisplayList(w, xaw->form.display_list, event, region);
}
#endif

/*ARGSUSED*/
static void
_CvtStringToEdgeType(XrmValuePtr args, Cardinal *num_args,
		     XrmValuePtr fromVal, XrmValuePtr toVal)
{
    static XtEdgeType edgeType;
    XrmQuark q;
    char name[12];

    XmuNCopyISOLatin1Lowered(name, (char*)fromVal->addr, sizeof(name));
    q = XrmStringToQuark(name);

    if (q == QchainLeft)
	edgeType = XtChainLeft;
    else if (q == QchainRight)
	edgeType = XtChainRight;
    else if (q == QchainTop)
	edgeType = XtChainTop;
    else if (q == QchainBottom)
	edgeType = XtChainBottom;
    else if (q == Qrubber)
	edgeType = XtRubber;
    else {
	XtStringConversionWarning(fromVal->addr, XtREdgeType);
	toVal->size = 0;
	toVal->addr = NULL;
	return;
    }

    toVal->size = sizeof(XtEdgeType);
    toVal->addr = (XPointer)&edgeType;
}

/*ARGSUSED*/
Boolean
CvtEdgeTypeToString(Display *dpy, XrmValuePtr args, Cardinal *num_args,
		    XrmValuePtr fromVal, XrmValuePtr toVal, XtPointer *data)
{
    static String buffer;
    Cardinal size;

    switch (*(XtEdgeType *)fromVal->addr) {
	case XtChainLeft:
	    buffer = XtEchainLeft;
	    break;
	case XtChainRight:
	    buffer = XtEchainRight;
	    break;
	case XtChainTop:
	    buffer = XtEchainTop;
	    break;
	case XtChainBottom:
	    buffer = XtEchainBottom;
	    break;
	case XtRubber:
	    buffer = XtErubber;
	    break;
	default:
	    XawTypeToStringWarning(dpy, XtREdgeType);
	    toVal->addr = NULL;
	    toVal->size = 0;
	    return (False);
    }

    size = strlen(buffer) + 1;
    if (toVal->addr != NULL) {
	if (toVal->size < size)	{
	    toVal->size = size;
	    return (False);
	}
	strcpy((char *)toVal->addr, buffer);
    }
    else
	toVal->addr = (XPointer)buffer;
    toVal->size = sizeof(String);

    return (True);
}

static void
XawFormClassInitialize(void)
{
    static XtConvertArgRec parentCvtArgs[] = {
	{XtBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.parent),
	 sizeof(Widget)}
    };

    char name[12];

    XawInitializeWidgetSet();
    XmuNCopyISOLatin1Lowered(name, XtEchainLeft, sizeof(name));
    QchainLeft = XrmStringToQuark(name);
    XmuNCopyISOLatin1Lowered(name, XtEchainRight, sizeof(name));
    QchainRight = XrmStringToQuark(name);
    XmuNCopyISOLatin1Lowered(name, XtEchainTop, sizeof(name));
    QchainTop = XrmStringToQuark(name);
    XmuNCopyISOLatin1Lowered(name, XtEchainBottom, sizeof(name));
    QchainBottom = XrmStringToQuark(name);
    XmuNCopyISOLatin1Lowered(name, XtErubber, sizeof(name));
    Qrubber = XrmStringToQuark(name);

    XtAddConverter(XtRString, XtREdgeType, _CvtStringToEdgeType, NULL, 0);
    XtSetTypeConverter(XtREdgeType, XtRString, CvtEdgeTypeToString,
		       NULL, 0, XtCacheNone, NULL);
    XtSetTypeConverter(XtRString, XtRWidget, XmuNewCvtStringToWidget,
		       parentCvtArgs, XtNumber(parentCvtArgs), XtCacheNone,
		       NULL);
    XtSetTypeConverter(XtRWidget, XtRString, XmuCvtWidgetToString,
		       NULL, 0, XtCacheNone, NULL);
}

static void
XawFormClassPartInitialize(WidgetClass cclass)
{
    FormWidgetClass c = (FormWidgetClass)cclass;
    FormWidgetClass super = (FormWidgetClass)c->core_class.superclass;

    if (c->form_class.layout == XtInheritLayout)
	c->form_class.layout = super->form_class.layout;
}

/*ARGSUSED*/
static void
XawFormInitialize(Widget request, Widget cnew,
		  ArgList args, Cardinal *num_args)
{
    FormWidget fw = (FormWidget)cnew;

    fw->form.old_width = fw->form.old_height = 0;
    fw->form.no_refigure = False;
    fw->form.needs_relayout = False;
    fw->form.resize_in_layout = True;
    fw->form.resize_is_no_op = False;
}

/*
 * Function:
 *	ChangeFormGeometry
 *
 * Parameters:
 *	w	   - Form widget
 *	query_only - is only a query?
 *	width	   - new width and height
 *	height	   - ""
 *	ret_width  - actual size the form is allowed to resize to (return)
 *	ret_height - ""
 *
 * Description:
 *	Ask the parent to change the form widget's geometry.
 *
 * Returns:
 *	True of children may always be resized
 */
static Bool
ChangeFormGeometry(Widget w, Bool query_only,
		   unsigned int width, unsigned int height,
		   Dimension *ret_width, Dimension *ret_height)
{
    FormWidget fw = (FormWidget)w;
    Boolean always_resize_children;
    XtGeometryResult result;
    XtWidgetGeometry request, return_request;

    /*
     * If we are already at the desired size then there is no need
     * to ask our parent of we can change size
     */
    if (width == XtWidth(fw) && height == XtHeight(fw))
	return (True);

    request.width = width;
    request.height = height;
    request.request_mode = CWWidth | CWHeight;
    if (query_only)
	request.request_mode |= XtCWQueryOnly;

    /*
     * Do no invoke the resize rules if our size changes here
     */
    fw->form.resize_is_no_op = True;

    result = XtMakeGeometryRequest(w, &request, &return_request);
    if (result == XtGeometryAlmost) {
	request = return_request;
	(void)XtMakeGeometryRequest(w, &request, &return_request);
	always_resize_children = False;
    }
    else
	always_resize_children = result == XtGeometryYes;

    fw->form.resize_is_no_op = False;

    if (ret_width != NULL)
	*ret_width = request.width;
    if (ret_height != NULL)
	*ret_height = request.height;

    return (always_resize_children);
}

/*
 * Function:
 *	Layout
 *
 * Parameters:
 *	fw	       - Form widget
 *	width	       - unused
 *	height	       - ""
 *	force_relayout - will force the children to be moved, even if some
 *			 go past the edge of the form
 *
 * Description:
 *	Moves all the children around.
 *
 * Returns:
 *	  True if the children are allowed to move from their
 *	  current locations to the new ones.
 */
/*ARGSUSED*/
static Boolean
Layout(FormWidget fw, unsigned int width, unsigned int height,
       Bool force_relayout)
{
    int num_children = fw->composite.num_children;
    WidgetList children = fw->composite.children;
    Widget *childP;
    Dimension maxx, maxy;
    Boolean ret_val;

    for (childP = children; childP - children < num_children; childP++) {
	FormConstraints form = (FormConstraints)(*childP)->core.constraints;
	form->form.layout_state = LayoutPending;
    }

    maxx = maxy = 1;
    for (childP = children; childP - children < num_children; childP++) {
	if (XtIsManaged(*childP)) {
	    FormConstraints form;
	    Position x, y;

	    form = (FormConstraints)(*childP)->core.constraints;

	    LayoutChild(*childP);

	    x = form->form.new_x + XtWidth(*childP)
		+ (XtBorderWidth(*childP) << 1);
	    if (x > (int)maxx)
		maxx = x;

	    y = form->form.new_y + XtHeight(*childP)
		+ (XtBorderWidth(*childP) << 1);
	    if (y > (int)maxy)
		maxy = y;
	}
    }

    fw->form.preferred_width = (maxx += fw->form.default_spacing);
    fw->form.preferred_height = (maxy += fw->form.default_spacing);

    if (fw->form.resize_in_layout) {
	Boolean always_resize_children;

	always_resize_children =
	    ChangeFormGeometry((Widget)fw, False, maxx, maxy, NULL, NULL);

#ifdef OLDXAW
	fw->form.old_width  = fw->core.width;
	fw->form.old_height = fw->core.height;
#endif

	if (force_relayout)
	    ret_val = True;
	else
	    ret_val = always_resize_children ||
			(XtWidth(fw) >= maxx && XtHeight(fw) >= maxy);

	if (ret_val)
	    ResizeChildren((Widget)fw);
    }
    else
	ret_val = False;

    fw->form.needs_relayout = False;

    return (ret_val);
}

/*
 * Function:
 *	ResizeChildren
 *
 * Parameters:
 *	w - form widget
 *
 * Description:
 *	Resizes all children to new_x and new_y.
 */
static void
ResizeChildren(Widget w)
{
    FormWidget fw = (FormWidget)w;
    int num_children = fw->composite.num_children;
    WidgetList children = fw->composite.children;
    Widget *childP;

    for (childP = children; childP - children < num_children; childP++) {
	FormConstraints form;
	Position x, y;

	if (!XtIsManaged(*childP))
	    continue;

	form = (FormConstraints)(*childP)->core.constraints;

	if (fw->form.old_width && fw->form.old_height) {
	    x = TransformCoord(form->form.new_x, fw->form.old_width,
			       XtWidth(fw), form->form.left);
	    y = TransformCoord(form->form.new_y, fw->form.old_height,
			       XtHeight(fw), form->form.top);
	}
	else {
	    x = form->form.new_x;
	    y = form->form.new_y;
	}

	if (fw->form.no_refigure) {
	   /*
	    * I am changing the widget wrapper w/o modifing the window.  This is
	    * risky, but I can get away with it since I am the parent of this
	    * widget, and he must ask me for any geometry changes
	    *
	    * The window will be updated when no_refigure is set back to False
	    */
	    XtX(*childP) = x;
	    XtY(*childP) = y;
	}
	else
	    XtMoveWidget(*childP, x, y);
    }
}

static void
LayoutChild(Widget w)
{
    FormConstraints form = (FormConstraints)w->core.constraints;
    Widget ref;

    switch (form->form.layout_state) {
	case LayoutPending:
	    form->form.layout_state = LayoutInProgress;
	    break;
	case LayoutDone:
	    return;
	case LayoutInProgress: {
	    String subs[2];
	    Cardinal num_subs = 2;
	    subs[0] = w->core.name;
	    subs[1] = w->core.parent->core.name;

	    XtAppWarningMsg(XtWidgetToApplicationContext(w),
			    "constraintLoop", "xawFormLayout", "XawToolkitError",
			    "constraint loop detected while laying out "
			    "child '%s' in FormWidget '%s'",
			    subs, &num_subs);
	}   return;
    }

    form->form.new_x = form->form.dx;
    form->form.new_y = form->form.dy;
    if ((ref = form->form.horiz_base) != NULL) {
	FormConstraints ref_form = (FormConstraints)ref->core.constraints;

	LayoutChild(ref);
	form->form.new_x += ref_form->form.new_x + XtWidth(ref) +
			    (XtBorderWidth(ref) << 1);
    }
    if ((ref = form->form.vert_base) != NULL) {
	FormConstraints ref_form = (FormConstraints)ref->core.constraints;

	LayoutChild(ref);
	form->form.new_y += ref_form->form.new_y + XtHeight(ref) +
			    (XtBorderWidth(ref) << 1);
    }

    form->form.layout_state = LayoutDone;
}

static int
TransformCoord(int loc, unsigned int old, unsigned int cnew, XtEdgeType type)
{
    if (type == XtRubber) {
	if ((int)old > 0)
	    loc = (int)(loc * ((double)cnew / (double)old));
    }
    else if (type == XtChainBottom || type == XtChainRight)
	loc += (int)cnew - (int)old;

    return (loc);
}

static void
XawFormResize(Widget w)
{
    FormWidget fw = (FormWidget)w;
    WidgetList children = fw->composite.children;
    int num_children = fw->composite.num_children;
    Widget *childP;
    int x, y;
    int width, height;
    Boolean unmap = XtIsRealized(w) && w->core.mapped_when_managed &&
		    XtIsManaged(w);

    if (unmap)
	XtUnmapWidget(w);

    if (!fw->form.resize_is_no_op)
	for (childP = children; childP - children < num_children; childP++) {
	    FormConstraints form = (FormConstraints)(*childP)->core.constraints;

	    if (!XtIsManaged(*childP))
		continue;

#ifndef OLDXAW
	    x = TransformCoord(form->form.virtual_x, fw->form.old_width,
			      XtWidth(fw), form->form.left);
	    y = TransformCoord(form->form.virtual_y, fw->form.old_height,
			       XtHeight(fw), form->form.top);
	    width = TransformCoord(form->form.virtual_x +
				   form->form.virtual_width +
				   (XtBorderWidth(*childP) << 1),
				   fw->form.old_width, XtWidth(fw),
				   form->form.right) -
				   (x + (XtBorderWidth(*childP) << 1));
	    height = TransformCoord(form->form.virtual_y +
				    form->form.virtual_height +
				    (XtBorderWidth(*childP) << 1),
				    fw->form.old_height, XtHeight(fw),
				    form->form.bottom) -
				    (y + (XtBorderWidth(*childP) << 1));
#else
	    x = TransformCoord(XtX(*childP), fw->form.old_width,
			      XtWidth(fw), form->form.left);
	    y = TransformCoord(XtY(*childP), fw->form.old_height,
			       XtHeight(fw), form->form.top);
	    width = TransformCoord(XtX(*childP) + form->form.virtual_width +
				   (XtBorderWidth(*childP) << 1),
				   fw->form.old_width, XtWidth(fw),
				   form->form.right) -
				   (x + (XtBorderWidth(*childP) << 1));
	    height = TransformCoord(XtY(*childP) + form->form.virtual_height +
				    (XtBorderWidth(*childP) << 1),
				    fw->form.old_height, XtHeight(fw),
				    form->form.bottom) -
				    (y + (XtBorderWidth(*childP) << 1));
	    form->form.virtual_width = width;
	    form->form.virtual_height = height;
#endif

	    width = width < 1 ? 1 : width;
	    height = height < 1 ? 1 : height;

	    XtConfigureWidget(*childP, x, y, width, height,
			      XtBorderWidth(*childP));
	}

    if (unmap)
	XtMapWidget(w);

#ifdef OLDXAW
    fw->form.old_width = XtWidth(fw);
    fw->form.old_height = XtHeight(fw);
#endif
}

/*ARGSUSED*/
static XtGeometryResult
XawFormGeometryManager(Widget w, XtWidgetGeometry *request,
		       XtWidgetGeometry *reply)
{
    Dimension old_width, old_height;
    FormWidget fw = (FormWidget)XtParent(w);
    FormConstraints form = (FormConstraints)w->core.constraints;
    XtWidgetGeometry allowed;
    XtGeometryResult ret_val;

    if ((request->request_mode & (unsigned)~(XtCWQueryOnly | CWWidth | CWHeight))
	|| !form->form.allow_resize) {
	/* If GeometryManager is invoked during a SetValues call on a child
	 * then it is necessary to compute a new layout if ConstraintSetValues
	 * allowed any constraint changes
	 */
	if (fw->form.needs_relayout)
	    (*((FormWidgetClass)fw->core.widget_class)->form_class.layout)
		(fw, 0, 0, True);
	return (XtGeometryNo);
    }

    if (request->request_mode & CWWidth)
	allowed.width = request->width;
    else
	allowed.width = XtWidth(w);

    if (request->request_mode & CWHeight)
	allowed.height = request->height;
    else
	allowed.height = XtHeight(w);

    if (allowed.width == XtWidth(w) && allowed.height == XtHeight(w)) {
	/* If GeometryManager is invoked during a SetValues call on a child
	 * then it is necessary to compute a new layout if ConstraintSetValues
	 * allowed any constraint changes
	 */
	if (fw->form.needs_relayout)
	    (*((FormWidgetClass)fw->core.widget_class)->form_class.layout)
		(fw, 0, 0, True);
	return (XtGeometryNo);
    }

    /*
     * Remember the old size, and then set the size to the requested size
     */
    old_width = XtWidth(w);
    old_height = XtHeight(w);
    XtWidth(w) = allowed.width;
    XtHeight(w) = allowed.height;

    if (request->request_mode & XtCWQueryOnly) {
	Boolean always_resize_children;
	Dimension ret_width, ret_height;

	fw->form.resize_in_layout = False;

	(*((FormWidgetClass)fw->core.widget_class)->form_class.layout)
	    (fw, XtWidth(w), XtHeight(w), False);

	/*
	 * Reset the size of this child back to what it used to be
	 */
	XtWidth(w) = old_width;
	XtHeight(w) = old_height;

	fw->form.resize_in_layout = True;

	always_resize_children = ChangeFormGeometry(w, True,
				   fw->form.preferred_width,
				   fw->form.preferred_height,
				   &ret_width, &ret_height);

	if (always_resize_children
	    || (ret_width >= fw->form.preferred_width
		&& ret_height >= fw->form.preferred_height))
	    ret_val = XtGeometryYes;
	else
	    ret_val = XtGeometryNo;
    }
    else {
	if ((*((FormWidgetClass)fw->core.widget_class)->form_class.layout)
		(fw, XtWidth(w), XtHeight(w), False)) {
	    Widget *childP;
	    int num_children = fw->composite.num_children;
	    WidgetList children = fw->composite.children;

	    if (fw->form.no_refigure) {
		/*
		 * I am changing the widget wrapper w/o modifing the window.
		 * This is risky, but I can get away with it since I am the
		 * parent of this widget, and he must ask me for any geometry
		 * changes
		 *
		 * The window will be updated when no_refigure is set back
		 * to False
		 */
		form->form.deferred_resize = True;
		ret_val = XtGeometryDone;
	    }
	    else
		ret_val = XtGeometryYes;

	    /*
	     * Resets everything.
	     */
	    fw->form.old_width = XtWidth(fw);
	    fw->form.old_height = XtHeight(fw);
	    for (childP = children; childP - children < num_children; childP++) {
		Widget nw = *childP;

		if (XtIsManaged(nw)) {
		    FormConstraints nform = (FormConstraints)nw->core.constraints;

#ifndef OLDXAW
		    nform->form.virtual_x = XtX(nw);
		    nform->form.virtual_y = XtY(nw);
#endif
		    nform->form.virtual_width = XtWidth(nw);
		    nform->form.virtual_height = XtHeight(nw);
		}
	    }
	}
	else {
	    XtWidth(w) = old_width;
	    XtHeight(w) = old_height;
	    ret_val = XtGeometryNo;
	}
    }

    return (ret_val);
}

/*ARGSUSED*/
static Boolean
XawFormSetValues(Widget current, Widget request, Widget cnew,
		 ArgList args, Cardinal *num_args)
{
#ifndef OLDXAW
    FormWidget f_old = (FormWidget)current;
    FormWidget f_new = (FormWidget)cnew;

    if (f_old->core.background_pixmap != f_new->core.background_pixmap) {
	XawPixmap *opix, *npix;

	opix = XawPixmapFromXPixmap(f_old->core.background_pixmap, XtScreen(f_old),
				    f_old->core.colormap, f_old->core.depth);
	npix = XawPixmapFromXPixmap(f_new->core.background_pixmap, XtScreen(f_new),
				    f_new->core.colormap, f_new->core.depth);
	if ((npix && npix->mask) || (opix && opix->mask))
	    XawReshapeWidget(cnew, npix);
    }
#endif /* OLDXAW */

    return (False);
}

/* ARGSUSED */
static void
XawFormConstraintInitialize(Widget request, Widget cnew,
			    ArgList args, Cardinal *num_args)
{
    FormConstraints form = (FormConstraints)cnew->core.constraints;
    FormWidget fw = (FormWidget)cnew->core.parent;

#ifndef OLDXAW
    form->form.virtual_x = XtX(cnew);
    form->form.virtual_y = XtY(cnew);
#endif
    form->form.virtual_width = XtWidth(cnew);
    form->form.virtual_height = XtHeight(cnew);

    if (form->form.dx == default_value)
	form->form.dx = fw->form.default_spacing;

    if (form->form.dy == default_value)
	form->form.dy = fw->form.default_spacing;

    form->form.deferred_resize = False;
}

/*ARGSUSED*/
static Boolean
XawFormConstraintSetValues(Widget current, Widget request, Widget cnew,
			   ArgList args, Cardinal *num_args)
{
    FormConstraints cfc = (FormConstraints)current->core.constraints;
    FormConstraints nfc = (FormConstraints)cnew->core.constraints;

    if (cfc->form.top != nfc->form.top || cfc->form.bottom != nfc->form.bottom
	|| cfc->form.left != nfc->form.left || cfc->form.right != nfc->form.right
	|| cfc->form.dx != nfc->form.dx || cfc->form.dy != nfc->form.dy
	|| cfc->form.horiz_base != nfc->form.horiz_base
	|| cfc->form.vert_base != nfc->form.vert_base) {
	FormWidget fp = (FormWidget)XtParent(cnew);

	/* If there are no subclass ConstraintSetValues procedures remaining
	 * to be invoked, and if there is no geometry request about to be
	 * made, then invoke the new layout now; else defer it
	 */
	if (XtClass(XtParent(cnew)) == formWidgetClass
	    && XtX(current) == XtX(cnew)
	    && XtY(current) == XtY(cnew)
	    && XtWidth(current) == XtWidth(cnew)
	    && XtHeight(current) == XtHeight(cnew)
	    && XtBorderWidth(current) == XtBorderWidth(cnew))
	    Layout(fp, 0, 0, True);
	else
	    fp->form.needs_relayout = True;
    }

    return (False);
}

static void
XawFormChangeManaged(Widget w)
{
    FormWidget fw = (FormWidget)w;
    FormConstraints form;
    WidgetList children, childP;
    int num_children = fw->composite.num_children;
    Widget child;

    (*((FormWidgetClass)w->core.widget_class)->form_class.layout)
	(fw, XtWidth(w), XtHeight(w), True);

    fw->form.old_width = XtWidth(w);
    fw->form.old_height = XtHeight(w);
    for (children = childP = fw->composite.children;
	 childP - children < num_children;
	 childP++) {
	child = *childP;
	if (!XtIsManaged(child))
	    continue;
	form = (FormConstraints)child->core.constraints;
#ifndef OLDXAW
	form->form.virtual_x = XtX(child);
	form->form.virtual_y = XtY(child);
#endif
	form->form.virtual_width = XtWidth(child);
	form->form.virtual_height = XtHeight(child);
    }
}

static XtGeometryResult
XawFormQueryGeometry(Widget widget, XtWidgetGeometry *request,
		     XtWidgetGeometry *reply)
{
    FormWidget w = (FormWidget)widget;

    reply->width = w->form.preferred_width;
    reply->height = w->form.preferred_height;
    reply->request_mode = CWWidth | CWHeight;

    if ((request->request_mode & (CWWidth | CWHeight)) == (CWWidth | CWHeight)
	&& request->width == reply->width
	&& request->height == reply->height)
	return (XtGeometryYes);
    else if (reply->width == XtWidth(w) && reply->height == XtHeight(w))
	return (XtGeometryNo);

    return (XtGeometryAlmost);
}

/*
 * Public routines
 */
/*
 * Set or reset figuring (ignored if not realized)
 */
void
XawFormDoLayout(Widget w,
#if NeedWidePrototypes
		Bool force
#else
		Boolean force
#endif
)
{
    Widget *childP;
    FormWidget fw = (FormWidget)w;
    int num_children = fw->composite.num_children;
    WidgetList children = fw->composite.children;

    if ((fw->form.no_refigure = !force) == True || !XtIsRealized(w))
	return;

    for (childP = children; childP - children < num_children; childP++) {
	Widget nw = *childP;

	if (XtIsManaged(nw)) {
	    FormConstraints form = (FormConstraints)nw->core.constraints;

	    /*
	     * Xt Configure widget is too smart, and optimizes out
	     * my changes
	     */
	    XMoveResizeWindow(XtDisplay(nw), XtWindow(nw),
			      XtX(nw), XtY(nw), XtWidth(nw), XtHeight(nw));

	    if (form)
	      if (form->form.deferred_resize &&
		XtClass(nw)->core_class.resize != NULL) {
		(*(XtClass(nw)->core_class.resize))(nw);
		form->form.deferred_resize = False;
	      }
	}
    }
}