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