Box.c revision 7a84e134
17a84e134Smrg/* $Xorg: Box.c,v 1.4 2001/02/09 02:03:43 xorgcvs Exp $ */
27a84e134Smrg
37a84e134Smrg/***********************************************************
47a84e134Smrg
57a84e134SmrgCopyright 1987, 1988, 1994, 1998  The Open Group
67a84e134Smrg
77a84e134SmrgPermission to use, copy, modify, distribute, and sell this software and its
87a84e134Smrgdocumentation for any purpose is hereby granted without fee, provided that
97a84e134Smrgthe above copyright notice appear in all copies and that both that
107a84e134Smrgcopyright notice and this permission notice appear in supporting
117a84e134Smrgdocumentation.
127a84e134Smrg
137a84e134SmrgThe above copyright notice and this permission notice shall be included in
147a84e134Smrgall copies or substantial portions of the Software.
157a84e134Smrg
167a84e134SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177a84e134SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187a84e134SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
197a84e134SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
207a84e134SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
217a84e134SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
227a84e134Smrg
237a84e134SmrgExcept as contained in this notice, the name of The Open Group shall not be
247a84e134Smrgused in advertising or otherwise to promote the sale, use or other dealings
257a84e134Smrgin this Software without prior written authorization from The Open Group.
267a84e134Smrg
277a84e134Smrg
287a84e134SmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
297a84e134Smrg
307a84e134Smrg                        All Rights Reserved
317a84e134Smrg
327a84e134SmrgPermission to use, copy, modify, and distribute this software and its
337a84e134Smrgdocumentation for any purpose and without fee is hereby granted,
347a84e134Smrgprovided that the above copyright notice appear in all copies and that
357a84e134Smrgboth that copyright notice and this permission notice appear in
367a84e134Smrgsupporting documentation, and that the name of Digital not be
377a84e134Smrgused in advertising or publicity pertaining to distribution of the
387a84e134Smrgsoftware without specific, written prior permission.
397a84e134Smrg
407a84e134SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
417a84e134SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
427a84e134SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
437a84e134SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
447a84e134SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
457a84e134SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
467a84e134SmrgSOFTWARE.
477a84e134Smrg
487a84e134Smrg******************************************************************/
497a84e134Smrg/* $XFree86: xc/lib/Xaw/Box.c,v 1.14 2001/01/17 19:42:25 dawes Exp $ */
507a84e134Smrg
517a84e134Smrg#ifdef HAVE_CONFIG_H
527a84e134Smrg#include <config.h>
537a84e134Smrg#endif
547a84e134Smrg#include <X11/IntrinsicP.h>
557a84e134Smrg#include <X11/StringDefs.h>
567a84e134Smrg#include <X11/Xmu/Misc.h>
577a84e134Smrg#include <X11/Xaw/BoxP.h>
587a84e134Smrg#include <X11/Xaw/XawInit.h>
597a84e134Smrg#include "Private.h"
607a84e134Smrg
617a84e134Smrg/*
627a84e134Smrg * Class Methods
637a84e134Smrg */
647a84e134Smrgstatic void XawBoxChangeManaged(Widget);
657a84e134Smrgstatic void XawBoxClassInitialize(void);
667a84e134Smrg#ifndef OLDXAW
677a84e134Smrgstatic void XawBoxExpose(Widget, XEvent*, Region);
687a84e134Smrg#endif
697a84e134Smrgstatic XtGeometryResult XawBoxGeometryManager(Widget, XtWidgetGeometry*,
707a84e134Smrg					      XtWidgetGeometry*);
717a84e134Smrgstatic void XawBoxInitialize(Widget, Widget, ArgList, Cardinal*);
727a84e134Smrgstatic XtGeometryResult XawBoxQueryGeometry(Widget, XtWidgetGeometry*,
737a84e134Smrg					    XtWidgetGeometry*);
747a84e134Smrgstatic void XawBoxRealize(Widget, Mask*, XSetWindowAttributes*);
757a84e134Smrgstatic void XawBoxResize(Widget);
767a84e134Smrgstatic Boolean XawBoxSetValues(Widget, Widget, Widget,
777a84e134Smrg			       ArgList, Cardinal*);
787a84e134Smrg
797a84e134Smrg/*
807a84e134Smrg * Prototypes
817a84e134Smrg */
827a84e134Smrgstatic void DoLayout(BoxWidget, unsigned int, unsigned int,
837a84e134Smrg		     Dimension*, Dimension*, Bool);
847a84e134Smrgstatic Bool TryNewLayout(BoxWidget);
857a84e134Smrg
867a84e134Smrg/*
877a84e134Smrg * Initialization
887a84e134Smrg */
897a84e134Smrg#ifndef OLDXAW
907a84e134Smrgstatic XtActionsRec actions[] = {
917a84e134Smrg  {"set-values", XawSetValuesAction},
927a84e134Smrg  {"get-values", XawGetValuesAction},
937a84e134Smrg  {"declare",    XawDeclareAction},
947a84e134Smrg  {"call-proc",  XawCallProcAction},
957a84e134Smrg};
967a84e134Smrg#endif
977a84e134Smrg
987a84e134Smrgstatic XtResource resources[] = {
997a84e134Smrg  {
1007a84e134Smrg    XtNhSpace,
1017a84e134Smrg    XtCHSpace,
1027a84e134Smrg    XtRDimension,
1037a84e134Smrg    sizeof(Dimension),
1047a84e134Smrg    XtOffsetOf(BoxRec, box.h_space),
1057a84e134Smrg    XtRImmediate,
1067a84e134Smrg    (XtPointer)4
1077a84e134Smrg  },
1087a84e134Smrg  {
1097a84e134Smrg    XtNvSpace,
1107a84e134Smrg    XtCVSpace,
1117a84e134Smrg    XtRDimension,
1127a84e134Smrg    sizeof(Dimension),
1137a84e134Smrg    XtOffsetOf(BoxRec, box.v_space),
1147a84e134Smrg    XtRImmediate,
1157a84e134Smrg    (XtPointer)4
1167a84e134Smrg  },
1177a84e134Smrg  {
1187a84e134Smrg    XtNorientation,
1197a84e134Smrg    XtCOrientation,
1207a84e134Smrg    XtROrientation,
1217a84e134Smrg    sizeof(XtOrientation),
1227a84e134Smrg    XtOffsetOf(BoxRec, box.orientation),
1237a84e134Smrg    XtRImmediate,
1247a84e134Smrg    (XtPointer)XtorientVertical
1257a84e134Smrg  },
1267a84e134Smrg#ifndef OLDXAW
1277a84e134Smrg  {
1287a84e134Smrg    XawNdisplayList,
1297a84e134Smrg    XawCDisplayList,
1307a84e134Smrg    XawRDisplayList,
1317a84e134Smrg    sizeof(XawDisplayList*),
1327a84e134Smrg    XtOffsetOf(BoxRec, box.display_list),
1337a84e134Smrg    XtRImmediate,
1347a84e134Smrg    NULL
1357a84e134Smrg  },
1367a84e134Smrg#endif
1377a84e134Smrg};
1387a84e134Smrg
1397a84e134SmrgBoxClassRec boxClassRec = {
1407a84e134Smrg  /* core */
1417a84e134Smrg  {
1427a84e134Smrg    (WidgetClass)&compositeClassRec,	/* superclass */
1437a84e134Smrg    "Box",				/* class_name */
1447a84e134Smrg    sizeof(BoxRec),			/* widget_size */
1457a84e134Smrg    XawBoxClassInitialize,		/* class_initialize */
1467a84e134Smrg    NULL,				/* class_part_init */
1477a84e134Smrg    False,				/* class_inited */
1487a84e134Smrg    XawBoxInitialize,			/* initialize */
1497a84e134Smrg    NULL,				/* initialize_hook */
1507a84e134Smrg    XawBoxRealize,			/* realize */
1517a84e134Smrg#ifndef OLDXAW
1527a84e134Smrg    actions,				/* actions */
1537a84e134Smrg    XtNumber(actions),			/* num_actions */
1547a84e134Smrg#else
1557a84e134Smrg    NULL,				/* actions */
1567a84e134Smrg    0,					/* num_actions */
1577a84e134Smrg#endif
1587a84e134Smrg    resources,				/* resources */
1597a84e134Smrg    XtNumber(resources),		/* num_resources */
1607a84e134Smrg    NULLQUARK,				/* xrm_class */
1617a84e134Smrg    True,				/* compress_motion */
1627a84e134Smrg    True,				/* compress_exposure */
1637a84e134Smrg    True,				/* compress_enterleave */
1647a84e134Smrg    False,				/* visible_interest */
1657a84e134Smrg    NULL,				/* destroy */
1667a84e134Smrg    XawBoxResize,			/* resize */
1677a84e134Smrg#ifndef OLDXAW
1687a84e134Smrg    XawBoxExpose,			/* expose */
1697a84e134Smrg#else
1707a84e134Smrg    NULL,				/* expose */
1717a84e134Smrg#endif
1727a84e134Smrg    XawBoxSetValues,			/* set_values */
1737a84e134Smrg    NULL,				/* set_values_hook */
1747a84e134Smrg    XtInheritSetValuesAlmost,		/* set_values_almost */
1757a84e134Smrg    NULL,				/* get_values_hook */
1767a84e134Smrg    NULL,				/* accept_focus */
1777a84e134Smrg    XtVersion,				/* version */
1787a84e134Smrg    NULL,				/* callback_private */
1797a84e134Smrg    NULL,				/* tm_table */
1807a84e134Smrg    XawBoxQueryGeometry,		/* query_geometry */
1817a84e134Smrg    XtInheritDisplayAccelerator,	/* display_accelerator */
1827a84e134Smrg    NULL,				/* extension */
1837a84e134Smrg  },
1847a84e134Smrg  /* composite */
1857a84e134Smrg  {
1867a84e134Smrg    XawBoxGeometryManager,		/* geometry_manager */
1877a84e134Smrg    XawBoxChangeManaged,		/* change_managed */
1887a84e134Smrg    XtInheritInsertChild,		/* insert_child */
1897a84e134Smrg    XtInheritDeleteChild,		/* delete_child */
1907a84e134Smrg    NULL,				/* extension */
1917a84e134Smrg  },
1927a84e134Smrg  /* box */
1937a84e134Smrg  {
1947a84e134Smrg    NULL,				/* extension */
1957a84e134Smrg  },
1967a84e134Smrg};
1977a84e134Smrg
1987a84e134SmrgWidgetClass boxWidgetClass = (WidgetClass)&boxClassRec;
1997a84e134Smrg
2007a84e134Smrg/*
2017a84e134Smrg * Do a layout, either actually assigning positions, or just calculating size.
2027a84e134Smrg * Returns minimum width and height that will preserve the same layout.
2037a84e134Smrg */
2047a84e134Smrgstatic void
2057a84e134SmrgDoLayout(BoxWidget bbw, unsigned int width, unsigned int height,
2067a84e134Smrg	 Dimension *reply_width, Dimension *reply_height, Bool position)
2077a84e134Smrg{
2087a84e134Smrg    Boolean vbox = (bbw->box.orientation == XtorientVertical);
2097a84e134Smrg    Cardinal  i;
2107a84e134Smrg    Dimension w, h;	/* Width and height needed for box 		*/
2117a84e134Smrg    Dimension lw, lh;	/* Width and height needed for current line 	*/
2127a84e134Smrg    Dimension bw, bh;	/* Width and height needed for current widget 	*/
2137a84e134Smrg    Dimension h_space;  /* Local copy of bbw->box.h_space 		*/
2147a84e134Smrg    Widget widget;	/* Current widget	 			*/
2157a84e134Smrg    unsigned int num_mapped_children = 0;
2167a84e134Smrg
2177a84e134Smrg    /* Box width and height */
2187a84e134Smrg    h_space = bbw->box.h_space;
2197a84e134Smrg
2207a84e134Smrg    w = 0;
2217a84e134Smrg    for (i = 0; i < bbw->composite.num_children; i++) {
2227a84e134Smrg	if (XtIsManaged(bbw->composite.children[i])
2237a84e134Smrg	    && bbw->composite.children[i]->core.width > w)
2247a84e134Smrg	    w = bbw->composite.children[i]->core.width;
2257a84e134Smrg    }
2267a84e134Smrg    w += h_space;
2277a84e134Smrg    if (w > width)
2287a84e134Smrg	width = w;
2297a84e134Smrg    h = bbw->box.v_space;
2307a84e134Smrg
2317a84e134Smrg    /* Line width and height */
2327a84e134Smrg    lh = 0;
2337a84e134Smrg    lw = h_space;
2347a84e134Smrg
2357a84e134Smrg    for (i = 0; i < bbw->composite.num_children; i++) {
2367a84e134Smrg	widget = bbw->composite.children[i];
2377a84e134Smrg	if (widget->core.managed) {
2387a84e134Smrg	    if (widget->core.mapped_when_managed)
2397a84e134Smrg		num_mapped_children++;
2407a84e134Smrg	    /* Compute widget width */
2417a84e134Smrg	    bw = XtWidth(widget) + (XtBorderWidth(widget)<<1) + h_space;
2427a84e134Smrg	    if ((Dimension)(lw + bw) > width) {
2437a84e134Smrg		if (lw > h_space) {
2447a84e134Smrg		    /* At least one widget on this line, and
2457a84e134Smrg		     * can't fit any more.  Start new line if vbox
2467a84e134Smrg		     */
2477a84e134Smrg		    AssignMax(w, lw);
2487a84e134Smrg		    if (vbox) {
2497a84e134Smrg			h += lh + bbw->box.v_space;
2507a84e134Smrg			lh = 0;
2517a84e134Smrg			lw = h_space;
2527a84e134Smrg		    }
2537a84e134Smrg		}
2547a84e134Smrg		else if (!position) {
2557a84e134Smrg		    /* too narrow for this widget; we'll assume we can grow */
2567a84e134Smrg		    DoLayout(bbw, (unsigned)(lw + bw), height, reply_width,
2577a84e134Smrg			     reply_height, position);
2587a84e134Smrg		    return;
2597a84e134Smrg		}
2607a84e134Smrg	    }
2617a84e134Smrg	    if (position && (lw != XtX(widget) || h != XtY(widget))) {
2627a84e134Smrg		/* It would be nice to use window gravity, but there isn't
2637a84e134Smrg		 * sufficient fine-grain control to nicely handle all
2647a84e134Smrg		 * situations (e.g. when only the height changes --
2657a84e134Smrg		 * a common case).  Explicit unmapping is a cheap hack
2667a84e134Smrg		 * to speed things up & avoid the visual jitter as
2677a84e134Smrg		 * things slide around.
2687a84e134Smrg		 *
2697a84e134Smrg		 * %%% perhaps there should be a client resource to
2707a84e134Smrg		 * control this.  If so, we'll have to optimize to
2717a84e134Smrg		 * perform the moves from the correct end so we don't
2727a84e134Smrg		 * force extra exposures as children occlude each other.
2737a84e134Smrg		 */
2747a84e134Smrg		if (XtIsRealized(widget) && widget->core.mapped_when_managed)
2757a84e134Smrg		XUnmapWindow( XtDisplay(widget), XtWindow(widget));
2767a84e134Smrg		XtMoveWidget(widget, (int)lw, (int)h);
2777a84e134Smrg	    }
2787a84e134Smrg	    lw += bw;
2797a84e134Smrg	    bh = XtHeight(widget) + (XtBorderWidth(widget) << 1);
2807a84e134Smrg	    AssignMax(lh, bh);
2817a84e134Smrg	}
2827a84e134Smrg    }
2837a84e134Smrg
2847a84e134Smrg    if (!vbox && width && lw > width && lh < height) {
2857a84e134Smrg	/* reduce width if too wide and height not filled */
2867a84e134Smrg	Dimension sw = lw, sh = lh;
2877a84e134Smrg	Dimension width_needed = width;
2887a84e134Smrg	XtOrientation orientation = bbw->box.orientation;
2897a84e134Smrg
2907a84e134Smrg	bbw->box.orientation = XtorientVertical;
2917a84e134Smrg	while (sh < height && sw > width) {
2927a84e134Smrg	    width_needed = sw;
2937a84e134Smrg	    DoLayout(bbw, (unsigned)(sw-1), height, &sw, &sh, False);
2947a84e134Smrg	}
2957a84e134Smrg	if (sh < height)
2967a84e134Smrg	  width_needed = sw;
2977a84e134Smrg	if (width_needed != lw) {
2987a84e134Smrg	    DoLayout(bbw, width_needed, height,
2997a84e134Smrg		     reply_width, reply_height, position);
3007a84e134Smrg	    bbw->box.orientation = orientation;
3017a84e134Smrg	    return;
3027a84e134Smrg	}
3037a84e134Smrg	bbw->box.orientation = orientation;
3047a84e134Smrg    }
3057a84e134Smrg    if (vbox && (width < w || width < lw)) {
3067a84e134Smrg	AssignMax(w, lw);
3077a84e134Smrg	DoLayout(bbw, w, height, reply_width, reply_height, position);
3087a84e134Smrg	return;
3097a84e134Smrg    }
3107a84e134Smrg     if (position && XtIsRealized((Widget)bbw)) {
3117a84e134Smrg	if (bbw->composite.num_children == num_mapped_children)
3127a84e134Smrg	    XMapSubwindows(XtDisplay((Widget)bbw), XtWindow((Widget)bbw));
3137a84e134Smrg	else {
3147a84e134Smrg	    int ii = bbw->composite.num_children;
3157a84e134Smrg	    Widget *childP = bbw->composite.children;
3167a84e134Smrg
3177a84e134Smrg	    for (; ii > 0; childP++, ii--)
3187a84e134Smrg		if (XtIsRealized(*childP) && XtIsManaged(*childP)
3197a84e134Smrg		    && (*childP)->core.mapped_when_managed)
3207a84e134Smrg		    XtMapWidget(*childP);
3217a84e134Smrg	}
3227a84e134Smrg    }
3237a84e134Smrg
3247a84e134Smrg    /* Finish off last line */
3257a84e134Smrg    if (lw > h_space) {
3267a84e134Smrg	AssignMax(w, lw);
3277a84e134Smrg        h += lh + bbw->box.v_space;
3287a84e134Smrg    }
3297a84e134Smrg
3307a84e134Smrg    *reply_width = Max(w, 1);
3317a84e134Smrg    *reply_height = Max(h, 1);
3327a84e134Smrg}
3337a84e134Smrg
3347a84e134Smrg/*
3357a84e134Smrg * Calculate preferred size, given constraining box, caching it in the widget
3367a84e134Smrg */
3377a84e134Smrgstatic XtGeometryResult
3387a84e134SmrgXawBoxQueryGeometry(Widget widget, XtWidgetGeometry *constraint,
3397a84e134Smrg		    XtWidgetGeometry *preferred)
3407a84e134Smrg{
3417a84e134Smrg    BoxWidget w = (BoxWidget)widget;
3427a84e134Smrg    Dimension width;
3437a84e134Smrg    Dimension preferred_width = w->box.preferred_width;
3447a84e134Smrg    Dimension preferred_height = w->box.preferred_height;
3457a84e134Smrg
3467a84e134Smrg    constraint->request_mode &= CWWidth | CWHeight;
3477a84e134Smrg
3487a84e134Smrg    if (constraint->request_mode == 0)
3497a84e134Smrg	/* parent isn't going to change w or h, so nothing to re-compute */
3507a84e134Smrg    return (XtGeometryYes);
3517a84e134Smrg
3527a84e134Smrg    if (constraint->request_mode == w->box.last_query_mode
3537a84e134Smrg	&& (!(constraint->request_mode & CWWidth)
3547a84e134Smrg	  || constraint->width == w->box.last_query_width)
3557a84e134Smrg	&& (!(constraint->request_mode & CWHeight)
3567a84e134Smrg	  || constraint->height == w->box.last_query_height)) {
3577a84e134Smrg	/* same query; current preferences are still valid */
3587a84e134Smrg	preferred->request_mode = CWWidth | CWHeight;
3597a84e134Smrg	preferred->width = preferred_width;
3607a84e134Smrg	preferred->height = preferred_height;
3617a84e134Smrg	if (constraint->request_mode == (CWWidth | CWHeight)
3627a84e134Smrg	    && constraint->width == preferred_width
3637a84e134Smrg	    && constraint->height == preferred_height)
3647a84e134Smrg	    return (XtGeometryYes);
3657a84e134Smrg	else
3667a84e134Smrg	    return (XtGeometryAlmost);
3677a84e134Smrg    }
3687a84e134Smrg
3697a84e134Smrg    /* else gotta do it the long way...
3707a84e134Smrg       I have a preference for tall and narrow, so if my width is
3717a84e134Smrg       constrained, I'll accept it; otherwise, I'll compute the minimum
3727a84e134Smrg       width that will fit me within the height constraint */
3737a84e134Smrg
3747a84e134Smrg    w->box.last_query_mode = constraint->request_mode;
3757a84e134Smrg    w->box.last_query_width = constraint->width;
3767a84e134Smrg    w->box.last_query_height= constraint->height;
3777a84e134Smrg
3787a84e134Smrg    if (constraint->request_mode & CWWidth)
3797a84e134Smrg	width = constraint->width;
3807a84e134Smrg    else { /* if (constraint->request_mode & CWHeight) */
3817a84e134Smrg	   /* let's see if I can become any narrower */
3827a84e134Smrg	width = 0;
3837a84e134Smrg	constraint->width = 65535;
3847a84e134Smrg    }
3857a84e134Smrg
3867a84e134Smrg    /* height is currently ignored by DoLayout.
3877a84e134Smrg       height = (constraint->request_mode & CWHeight) ? constraint->height
3887a84e134Smrg		       : *preferred_height;
3897a84e134Smrg     */
3907a84e134Smrg    DoLayout(w, width, 0, &preferred_width, &preferred_height, False);
3917a84e134Smrg
3927a84e134Smrg    if (constraint->request_mode & CWHeight
3937a84e134Smrg	&& preferred_height > constraint->height) {
3947a84e134Smrg	/* find minimum width for this height */
3957a84e134Smrg	if (preferred_width <= constraint->width) {
3967a84e134Smrg	    width = preferred_width;
3977a84e134Smrg	    do { /* find some width big enough to stay within this height */
3987a84e134Smrg		width <<= 1;
3997a84e134Smrg		if (width > constraint->width)
4007a84e134Smrg		    width = constraint->width;
4017a84e134Smrg		DoLayout(w, width, 0, &preferred_width, &preferred_height, False);
4027a84e134Smrg	    } while (preferred_height > constraint->height
4037a84e134Smrg		     && width < constraint->width);
4047a84e134Smrg	    if (width != constraint->width) {
4057a84e134Smrg		do { /* find minimum width */
4067a84e134Smrg		    width = preferred_width;
4077a84e134Smrg		    DoLayout(w, (unsigned)(preferred_width - 1), 0,
4087a84e134Smrg			     &preferred_width, &preferred_height, False);
4097a84e134Smrg		} while (preferred_height < constraint->height);
4107a84e134Smrg		/* one last time */
4117a84e134Smrg		DoLayout(w, width, 0, &preferred_width, &preferred_height, False);
4127a84e134Smrg	    }
4137a84e134Smrg	}
4147a84e134Smrg    }
4157a84e134Smrg
4167a84e134Smrg    preferred->request_mode = CWWidth | CWHeight;
4177a84e134Smrg    preferred->width = w->box.preferred_width = preferred_width;
4187a84e134Smrg    preferred->height = w->box.preferred_height = preferred_height;
4197a84e134Smrg
4207a84e134Smrg    if (constraint->request_mode == (CWWidth|CWHeight)
4217a84e134Smrg	&& constraint->width == preferred_width
4227a84e134Smrg	&& constraint->height == preferred_height)
4237a84e134Smrg	return (XtGeometryYes);
4247a84e134Smrg
4257a84e134Smrg    return (XtGeometryAlmost);
4267a84e134Smrg}
4277a84e134Smrg
4287a84e134Smrg/*
4297a84e134Smrg * Actually layout the box
4307a84e134Smrg */
4317a84e134Smrgstatic void
4327a84e134SmrgXawBoxResize(Widget w)
4337a84e134Smrg{
4347a84e134Smrg    Dimension tmp;
4357a84e134Smrg
4367a84e134Smrg    DoLayout((BoxWidget)w, XtWidth(w), XtHeight(w), &tmp, &tmp, True);
4377a84e134Smrg}
4387a84e134Smrg
4397a84e134Smrg/*
4407a84e134Smrg * Try to do a new layout within the current width and height;
4417a84e134Smrg * if that fails try to resize and do it within the box returne
4427a84e134Smrg * by XawBoxQueryGeometry
4437a84e134Smrg *
4447a84e134Smrg * TryNewLayout just says if it's possible, and doesn't actually move the kids
4457a84e134Smrg */
4467a84e134Smrgstatic Bool
4477a84e134SmrgTryNewLayout(BoxWidget bbw)
4487a84e134Smrg{
4497a84e134Smrg    Dimension 	preferred_width, preferred_height;
4507a84e134Smrg    Dimension	proposed_width, proposed_height;
4517a84e134Smrg    int		iterations;
4527a84e134Smrg
4537a84e134Smrg    DoLayout(bbw, bbw->core.width, bbw->core.height,
4547a84e134Smrg	     &preferred_width, &preferred_height, False);
4557a84e134Smrg
4567a84e134Smrg    /* at this point, preferred_width is guaranteed to not be greater
4577a84e134Smrg       than bbw->core.width unless some child is larger, so there's no
4587a84e134Smrg       point in re-computing another layout */
4597a84e134Smrg
4607a84e134Smrg    if (XtWidth(bbw) == preferred_width && XtHeight(bbw) == preferred_height)
4617a84e134Smrg	return (True);
4627a84e134Smrg
4637a84e134Smrg    /* let's see if our parent will go for a new size */
4647a84e134Smrg    iterations = 0;
4657a84e134Smrg    proposed_width = preferred_width;
4667a84e134Smrg    proposed_height = preferred_height;
4677a84e134Smrg    do {
4687a84e134Smrg	switch (XtMakeResizeRequest((Widget)bbw,proposed_width,proposed_height,
4697a84e134Smrg				     &proposed_width, &proposed_height)) {
4707a84e134Smrg	    case XtGeometryYes:
4717a84e134Smrg		return (True);
4727a84e134Smrg	    case XtGeometryNo:
4737a84e134Smrg		if (iterations > 0)
4747a84e134Smrg		    /* protect from malicious parents who change their minds */
4757a84e134Smrg		    DoLayout(bbw, bbw->core.width, bbw->core.height,
4767a84e134Smrg			     &preferred_width, &preferred_height, False);
4777a84e134Smrg		if (preferred_width <= XtWidth(bbw)
4787a84e134Smrg		    && preferred_height <= XtHeight(bbw))
4797a84e134Smrg		    return (True);
4807a84e134Smrg		else
4817a84e134Smrg		    return (False);
4827a84e134Smrg	    case XtGeometryAlmost:
4837a84e134Smrg		if (proposed_height >= preferred_height &&
4847a84e134Smrg		    proposed_width >= preferred_width) {
4857a84e134Smrg		    /*
4867a84e134Smrg		     * Take it, and assume the parent knows what it is doing.
4877a84e134Smrg		     *
4887a84e134Smrg		     * The parent must accept this since it was returned in
4897a84e134Smrg		     * almost.
4907a84e134Smrg		     */
4917a84e134Smrg		    (void)XtMakeResizeRequest((Widget)bbw,
4927a84e134Smrg					       proposed_width, proposed_height,
4937a84e134Smrg					       &proposed_width, &proposed_height);
4947a84e134Smrg		    return (True);
4957a84e134Smrg		}
4967a84e134Smrg		else if (proposed_width != preferred_width) {
4977a84e134Smrg		    /* recalc bounding box; height might change */
4987a84e134Smrg		    DoLayout(bbw, proposed_width, 0,
4997a84e134Smrg			     &preferred_width, &preferred_height, False);
5007a84e134Smrg		    proposed_height = preferred_height;
5017a84e134Smrg		}
5027a84e134Smrg		else {	/* proposed_height != preferred_height */
5037a84e134Smrg		    XtWidgetGeometry constraints, reply;
5047a84e134Smrg
5057a84e134Smrg		    constraints.request_mode = CWHeight;
5067a84e134Smrg		    constraints.height = proposed_height;
5077a84e134Smrg		    (void)XawBoxQueryGeometry((Widget)bbw, &constraints, &reply);
5087a84e134Smrg		    proposed_width = preferred_width;
5097a84e134Smrg		}
5107a84e134Smrg		/*FALLTHROUGH*/
5117a84e134Smrg	    default:
5127a84e134Smrg		break;
5137a84e134Smrg	}
5147a84e134Smrg	iterations++;
5157a84e134Smrg    } while (iterations < 10);
5167a84e134Smrg
5177a84e134Smrg    return (False);
5187a84e134Smrg}
5197a84e134Smrg
5207a84e134Smrg/*
5217a84e134Smrg * Geometry Manager
5227a84e134Smrg *
5237a84e134Smrg * 'reply' is unused; we say only yeay or nay, never almost.
5247a84e134Smrg */
5257a84e134Smrg/*ARGSUSED*/
5267a84e134Smrgstatic XtGeometryResult
5277a84e134SmrgXawBoxGeometryManager(Widget w, XtWidgetGeometry *request,
5287a84e134Smrg		      XtWidgetGeometry *reply)
5297a84e134Smrg{
5307a84e134Smrg    Dimension	width, height, borderWidth;
5317a84e134Smrg    BoxWidget bbw;
5327a84e134Smrg
5337a84e134Smrg    /* Position request always denied */
5347a84e134Smrg    if (((request->request_mode & CWX) && request->x != XtX(w))
5357a84e134Smrg	|| ((request->request_mode & CWY) && request->y != XtY(w)))
5367a84e134Smrg        return (XtGeometryNo);
5377a84e134Smrg
5387a84e134Smrg    /* Size changes must see if the new size can be accomodated */
5397a84e134Smrg    if (request->request_mode & (CWWidth | CWHeight | CWBorderWidth)) {
5407a84e134Smrg	/* Make all three fields in the request valid */
5417a84e134Smrg	if ((request->request_mode & CWWidth) == 0)
5427a84e134Smrg	    request->width = XtWidth(w);
5437a84e134Smrg	if ((request->request_mode & CWHeight) == 0)
5447a84e134Smrg	    request->height = XtHeight(w);
5457a84e134Smrg        if ((request->request_mode & CWBorderWidth) == 0)
5467a84e134Smrg	    request->border_width = XtBorderWidth(w);
5477a84e134Smrg
5487a84e134Smrg	/* Save current size and set to new size */
5497a84e134Smrg      width = XtWidth(w);
5507a84e134Smrg      height = XtHeight(w);
5517a84e134Smrg      borderWidth = XtBorderWidth(w);
5527a84e134Smrg      XtWidth(w) = request->width;
5537a84e134Smrg      XtHeight(w) = request->height;
5547a84e134Smrg      XtBorderWidth(w) = request->border_width;
5557a84e134Smrg
5567a84e134Smrg      /* Decide if new layout works:
5577a84e134Smrg	 (1) new widget is smaller,
5587a84e134Smrg	 (2) new widget fits in existing Box,
5597a84e134Smrg	 (3) Box can be expanded to allow new widget to fit
5607a84e134Smrg      */
5617a84e134Smrg
5627a84e134Smrg	bbw = (BoxWidget) w->core.parent;
5637a84e134Smrg
5647a84e134Smrg	if (TryNewLayout(bbw)) {
5657a84e134Smrg	    /* Fits in existing or new space, relayout */
5667a84e134Smrg	    (*XtClass((Widget)bbw)->core_class.resize)((Widget)bbw);
5677a84e134Smrg	    return (XtGeometryYes);
5687a84e134Smrg	}
5697a84e134Smrg	else {
5707a84e134Smrg	    /* Cannot satisfy request, change back to original geometry */
5717a84e134Smrg	    XtWidth(w) = width;
5727a84e134Smrg	    XtHeight(w) = height;
5737a84e134Smrg	    XtBorderWidth(w) = borderWidth;
5747a84e134Smrg	    return (XtGeometryNo);
5757a84e134Smrg	}
5767a84e134Smrg    }
5777a84e134Smrg
5787a84e134Smrg    /* Any stacking changes don't make a difference, so allow if that's all */
5797a84e134Smrg    return (XtGeometryYes);
5807a84e134Smrg}
5817a84e134Smrg
5827a84e134Smrgstatic void
5837a84e134SmrgXawBoxChangeManaged(Widget w)
5847a84e134Smrg{
5857a84e134Smrg    /* Reconfigure the box */
5867a84e134Smrg    (void)TryNewLayout((BoxWidget)w);
5877a84e134Smrg    XawBoxResize(w);
5887a84e134Smrg}
5897a84e134Smrg
5907a84e134Smrgstatic void
5917a84e134SmrgXawBoxClassInitialize(void)
5927a84e134Smrg{
5937a84e134Smrg    XawInitializeWidgetSet();
5947a84e134Smrg    XtAddConverter(XtRString, XtROrientation, XmuCvtStringToOrientation,
5957a84e134Smrg		   NULL, 0);
5967a84e134Smrg    XtSetTypeConverter(XtROrientation, XtRString, XmuCvtOrientationToString,
5977a84e134Smrg		       NULL, 0, XtCacheNone, NULL);
5987a84e134Smrg}
5997a84e134Smrg
6007a84e134Smrg/*ARGSUSED*/
6017a84e134Smrgstatic void
6027a84e134SmrgXawBoxInitialize(Widget request, Widget cnew,
6037a84e134Smrg		 ArgList args, Cardinal *num_args)
6047a84e134Smrg{
6057a84e134Smrg    BoxWidget newbbw = (BoxWidget)cnew;
6067a84e134Smrg
6077a84e134Smrg    newbbw->box.last_query_mode = CWWidth | CWHeight;
6087a84e134Smrg    newbbw->box.last_query_width = newbbw->box.last_query_height = 0;
6097a84e134Smrg    newbbw->box.preferred_width = Max(newbbw->box.h_space, 1);
6107a84e134Smrg    newbbw->box.preferred_height = Max(newbbw->box.v_space, 1);
6117a84e134Smrg
6127a84e134Smrg    if (XtWidth(newbbw) == 0)
6137a84e134Smrg	XtWidth(newbbw) = newbbw->box.preferred_width;
6147a84e134Smrg
6157a84e134Smrg    if (XtHeight(newbbw) == 0)
6167a84e134Smrg	XtHeight(newbbw) = newbbw->box.preferred_height;
6177a84e134Smrg}
6187a84e134Smrg
6197a84e134Smrgstatic void
6207a84e134SmrgXawBoxRealize(Widget w, Mask *valueMask, XSetWindowAttributes *attributes)
6217a84e134Smrg{
6227a84e134Smrg#ifndef OLDXAW
6237a84e134Smrg    XawPixmap *pixmap;
6247a84e134Smrg#endif
6257a84e134Smrg
6267a84e134Smrg    XtCreateWindow(w, InputOutput, (Visual *)CopyFromParent,
6277a84e134Smrg		   *valueMask, attributes);
6287a84e134Smrg
6297a84e134Smrg#ifndef OLDXAW
6307a84e134Smrg    if (w->core.background_pixmap > XtUnspecifiedPixmap) {
6317a84e134Smrg	pixmap = XawPixmapFromXPixmap(w->core.background_pixmap, XtScreen(w),
6327a84e134Smrg				      w->core.colormap, w->core.depth);
6337a84e134Smrg	if (pixmap && pixmap->mask)
6347a84e134Smrg	    XawReshapeWidget(w, pixmap);
6357a84e134Smrg    }
6367a84e134Smrg#endif
6377a84e134Smrg}
6387a84e134Smrg
6397a84e134Smrg/*ARGSUSED*/
6407a84e134Smrgstatic Boolean
6417a84e134SmrgXawBoxSetValues(Widget current, Widget request, Widget cnew,
6427a84e134Smrg		ArgList args, Cardinal *num_args)
6437a84e134Smrg{
6447a84e134Smrg     /* need to relayout if h_space or v_space change */
6457a84e134Smrg#ifndef OLDXAW
6467a84e134Smrg    BoxWidget b_old = (BoxWidget)current;
6477a84e134Smrg    BoxWidget b_new = (BoxWidget)cnew;
6487a84e134Smrg
6497a84e134Smrg    if (b_old->core.background_pixmap != b_new->core.background_pixmap) {
6507a84e134Smrg	XawPixmap *opix, *npix;
6517a84e134Smrg
6527a84e134Smrg	opix = XawPixmapFromXPixmap(b_old->core.background_pixmap,
6537a84e134Smrg				    XtScreen(b_old), b_old->core.colormap,
6547a84e134Smrg				    b_old->core.depth);
6557a84e134Smrg	npix = XawPixmapFromXPixmap(b_new->core.background_pixmap,
6567a84e134Smrg				    XtScreen(b_new), b_new->core.colormap,
6577a84e134Smrg				    b_new->core.depth);
6587a84e134Smrg	if ((npix && npix->mask) || (opix && opix->mask))
6597a84e134Smrg	    XawReshapeWidget(cnew, npix);
6607a84e134Smrg    }
6617a84e134Smrg#endif /* OLDXAW */
6627a84e134Smrg
6637a84e134Smrg  return (False);
6647a84e134Smrg}
6657a84e134Smrg
6667a84e134Smrg#ifndef OLDXAW
6677a84e134Smrgstatic void
6687a84e134SmrgXawBoxExpose(Widget w, XEvent *event, Region region)
6697a84e134Smrg{
6707a84e134Smrg    BoxWidget xaw = (BoxWidget)w;
6717a84e134Smrg
6727a84e134Smrg    if (xaw->box.display_list)
6737a84e134Smrg	XawRunDisplayList(w, xaw->box.display_list, event, region);
6747a84e134Smrg}
6757a84e134Smrg#endif /* OLDXAW */
676