Box.c revision 421c997b
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		*/
211421c997bSmrg    Widget widget;	/* Current widget				*/
2127a84e134Smrg    unsigned int num_mapped_children = 0;
213421c997bSmrg
2147a84e134Smrg    /* Box width and height */
2157a84e134Smrg    h_space = bbw->box.h_space;
2167a84e134Smrg
2177a84e134Smrg    w = 0;
2187a84e134Smrg    for (i = 0; i < bbw->composite.num_children; i++) {
2197a84e134Smrg	if (XtIsManaged(bbw->composite.children[i])
2207a84e134Smrg	    && bbw->composite.children[i]->core.width > w)
2217a84e134Smrg	    w = bbw->composite.children[i]->core.width;
2227a84e134Smrg    }
2237a84e134Smrg    w += h_space;
2247a84e134Smrg    if (w > width)
2257a84e134Smrg	width = w;
2267a84e134Smrg    h = bbw->box.v_space;
227421c997bSmrg
2287a84e134Smrg    /* Line width and height */
2297a84e134Smrg    lh = 0;
2307a84e134Smrg    lw = h_space;
231421c997bSmrg
2327a84e134Smrg    for (i = 0; i < bbw->composite.num_children; i++) {
2337a84e134Smrg	widget = bbw->composite.children[i];
2347a84e134Smrg	if (widget->core.managed) {
2357a84e134Smrg	    if (widget->core.mapped_when_managed)
2367a84e134Smrg		num_mapped_children++;
2377a84e134Smrg	    /* Compute widget width */
2387a84e134Smrg	    bw = XtWidth(widget) + (XtBorderWidth(widget)<<1) + h_space;
2397a84e134Smrg	    if ((Dimension)(lw + bw) > width) {
2407a84e134Smrg		if (lw > h_space) {
2417a84e134Smrg		    /* At least one widget on this line, and
2427a84e134Smrg		     * can't fit any more.  Start new line if vbox
2437a84e134Smrg		     */
2447a84e134Smrg		    AssignMax(w, lw);
2457a84e134Smrg		    if (vbox) {
2467a84e134Smrg			h += lh + bbw->box.v_space;
2477a84e134Smrg			lh = 0;
2487a84e134Smrg			lw = h_space;
2497a84e134Smrg		    }
2507a84e134Smrg		}
2517a84e134Smrg		else if (!position) {
2527a84e134Smrg		    /* too narrow for this widget; we'll assume we can grow */
2537a84e134Smrg		    DoLayout(bbw, (unsigned)(lw + bw), height, reply_width,
2547a84e134Smrg			     reply_height, position);
2557a84e134Smrg		    return;
2567a84e134Smrg		}
2577a84e134Smrg	    }
2587a84e134Smrg	    if (position && (lw != XtX(widget) || h != XtY(widget))) {
2597a84e134Smrg		/* It would be nice to use window gravity, but there isn't
2607a84e134Smrg		 * sufficient fine-grain control to nicely handle all
2617a84e134Smrg		 * situations (e.g. when only the height changes --
2627a84e134Smrg		 * a common case).  Explicit unmapping is a cheap hack
2637a84e134Smrg		 * to speed things up & avoid the visual jitter as
2647a84e134Smrg		 * things slide around.
2657a84e134Smrg		 *
2667a84e134Smrg		 * %%% perhaps there should be a client resource to
2677a84e134Smrg		 * control this.  If so, we'll have to optimize to
2687a84e134Smrg		 * perform the moves from the correct end so we don't
2697a84e134Smrg		 * force extra exposures as children occlude each other.
2707a84e134Smrg		 */
2717a84e134Smrg		if (XtIsRealized(widget) && widget->core.mapped_when_managed)
2727a84e134Smrg		XUnmapWindow( XtDisplay(widget), XtWindow(widget));
2737a84e134Smrg		XtMoveWidget(widget, (int)lw, (int)h);
2747a84e134Smrg	    }
2757a84e134Smrg	    lw += bw;
2767a84e134Smrg	    bh = XtHeight(widget) + (XtBorderWidth(widget) << 1);
2777a84e134Smrg	    AssignMax(lh, bh);
2787a84e134Smrg	}
2797a84e134Smrg    }
2807a84e134Smrg
2817a84e134Smrg    if (!vbox && width && lw > width && lh < height) {
2827a84e134Smrg	/* reduce width if too wide and height not filled */
2837a84e134Smrg	Dimension sw = lw, sh = lh;
2847a84e134Smrg	Dimension width_needed = width;
2857a84e134Smrg	XtOrientation orientation = bbw->box.orientation;
2867a84e134Smrg
2877a84e134Smrg	bbw->box.orientation = XtorientVertical;
2887a84e134Smrg	while (sh < height && sw > width) {
2897a84e134Smrg	    width_needed = sw;
2907a84e134Smrg	    DoLayout(bbw, (unsigned)(sw-1), height, &sw, &sh, False);
2917a84e134Smrg	}
2927a84e134Smrg	if (sh < height)
2937a84e134Smrg	  width_needed = sw;
2947a84e134Smrg	if (width_needed != lw) {
2957a84e134Smrg	    DoLayout(bbw, width_needed, height,
2967a84e134Smrg		     reply_width, reply_height, position);
2977a84e134Smrg	    bbw->box.orientation = orientation;
2987a84e134Smrg	    return;
2997a84e134Smrg	}
3007a84e134Smrg	bbw->box.orientation = orientation;
3017a84e134Smrg    }
3027a84e134Smrg    if (vbox && (width < w || width < lw)) {
3037a84e134Smrg	AssignMax(w, lw);
3047a84e134Smrg	DoLayout(bbw, w, height, reply_width, reply_height, position);
3057a84e134Smrg	return;
3067a84e134Smrg    }
3077a84e134Smrg     if (position && XtIsRealized((Widget)bbw)) {
3087a84e134Smrg	if (bbw->composite.num_children == num_mapped_children)
3097a84e134Smrg	    XMapSubwindows(XtDisplay((Widget)bbw), XtWindow((Widget)bbw));
3107a84e134Smrg	else {
3117a84e134Smrg	    int ii = bbw->composite.num_children;
3127a84e134Smrg	    Widget *childP = bbw->composite.children;
3137a84e134Smrg
3147a84e134Smrg	    for (; ii > 0; childP++, ii--)
3157a84e134Smrg		if (XtIsRealized(*childP) && XtIsManaged(*childP)
3167a84e134Smrg		    && (*childP)->core.mapped_when_managed)
3177a84e134Smrg		    XtMapWidget(*childP);
3187a84e134Smrg	}
3197a84e134Smrg    }
3207a84e134Smrg
3217a84e134Smrg    /* Finish off last line */
3227a84e134Smrg    if (lw > h_space) {
3237a84e134Smrg	AssignMax(w, lw);
3247a84e134Smrg        h += lh + bbw->box.v_space;
3257a84e134Smrg    }
3267a84e134Smrg
3277a84e134Smrg    *reply_width = Max(w, 1);
3287a84e134Smrg    *reply_height = Max(h, 1);
3297a84e134Smrg}
3307a84e134Smrg
3317a84e134Smrg/*
3327a84e134Smrg * Calculate preferred size, given constraining box, caching it in the widget
3337a84e134Smrg */
3347a84e134Smrgstatic XtGeometryResult
3357a84e134SmrgXawBoxQueryGeometry(Widget widget, XtWidgetGeometry *constraint,
3367a84e134Smrg		    XtWidgetGeometry *preferred)
3377a84e134Smrg{
3387a84e134Smrg    BoxWidget w = (BoxWidget)widget;
3397a84e134Smrg    Dimension width;
3407a84e134Smrg    Dimension preferred_width = w->box.preferred_width;
3417a84e134Smrg    Dimension preferred_height = w->box.preferred_height;
3427a84e134Smrg
3437a84e134Smrg    constraint->request_mode &= CWWidth | CWHeight;
3447a84e134Smrg
3457a84e134Smrg    if (constraint->request_mode == 0)
3467a84e134Smrg	/* parent isn't going to change w or h, so nothing to re-compute */
3477a84e134Smrg    return (XtGeometryYes);
3487a84e134Smrg
3497a84e134Smrg    if (constraint->request_mode == w->box.last_query_mode
3507a84e134Smrg	&& (!(constraint->request_mode & CWWidth)
3517a84e134Smrg	  || constraint->width == w->box.last_query_width)
3527a84e134Smrg	&& (!(constraint->request_mode & CWHeight)
3537a84e134Smrg	  || constraint->height == w->box.last_query_height)) {
3547a84e134Smrg	/* same query; current preferences are still valid */
3557a84e134Smrg	preferred->request_mode = CWWidth | CWHeight;
3567a84e134Smrg	preferred->width = preferred_width;
3577a84e134Smrg	preferred->height = preferred_height;
3587a84e134Smrg	if (constraint->request_mode == (CWWidth | CWHeight)
3597a84e134Smrg	    && constraint->width == preferred_width
3607a84e134Smrg	    && constraint->height == preferred_height)
3617a84e134Smrg	    return (XtGeometryYes);
3627a84e134Smrg	else
3637a84e134Smrg	    return (XtGeometryAlmost);
3647a84e134Smrg    }
365421c997bSmrg
3667a84e134Smrg    /* else gotta do it the long way...
3677a84e134Smrg       I have a preference for tall and narrow, so if my width is
3687a84e134Smrg       constrained, I'll accept it; otherwise, I'll compute the minimum
3697a84e134Smrg       width that will fit me within the height constraint */
3707a84e134Smrg
3717a84e134Smrg    w->box.last_query_mode = constraint->request_mode;
3727a84e134Smrg    w->box.last_query_width = constraint->width;
3737a84e134Smrg    w->box.last_query_height= constraint->height;
3747a84e134Smrg
3757a84e134Smrg    if (constraint->request_mode & CWWidth)
3767a84e134Smrg	width = constraint->width;
3777a84e134Smrg    else { /* if (constraint->request_mode & CWHeight) */
3787a84e134Smrg	   /* let's see if I can become any narrower */
3797a84e134Smrg	width = 0;
3807a84e134Smrg	constraint->width = 65535;
3817a84e134Smrg    }
3827a84e134Smrg
3837a84e134Smrg    /* height is currently ignored by DoLayout.
3847a84e134Smrg       height = (constraint->request_mode & CWHeight) ? constraint->height
3857a84e134Smrg		       : *preferred_height;
3867a84e134Smrg     */
3877a84e134Smrg    DoLayout(w, width, 0, &preferred_width, &preferred_height, False);
3887a84e134Smrg
3897a84e134Smrg    if (constraint->request_mode & CWHeight
3907a84e134Smrg	&& preferred_height > constraint->height) {
3917a84e134Smrg	/* find minimum width for this height */
3927a84e134Smrg	if (preferred_width <= constraint->width) {
3937a84e134Smrg	    width = preferred_width;
3947a84e134Smrg	    do { /* find some width big enough to stay within this height */
395421c997bSmrg		if (width > (constraint->width >> 1)) /* avoid short int overflow */
3967a84e134Smrg		    width = constraint->width;
397421c997bSmrg		else
398421c997bSmrg		    width <<= 1;
3997a84e134Smrg		DoLayout(w, width, 0, &preferred_width, &preferred_height, False);
4007a84e134Smrg	    } while (preferred_height > constraint->height
4017a84e134Smrg		     && width < constraint->width);
4027a84e134Smrg	    if (width != constraint->width) {
4037a84e134Smrg		do { /* find minimum width */
4047a84e134Smrg		    width = preferred_width;
4057a84e134Smrg		    DoLayout(w, (unsigned)(preferred_width - 1), 0,
4067a84e134Smrg			     &preferred_width, &preferred_height, False);
4077a84e134Smrg		} while (preferred_height < constraint->height);
4087a84e134Smrg		/* one last time */
4097a84e134Smrg		DoLayout(w, width, 0, &preferred_width, &preferred_height, False);
4107a84e134Smrg	    }
4117a84e134Smrg	}
4127a84e134Smrg    }
4137a84e134Smrg
4147a84e134Smrg    preferred->request_mode = CWWidth | CWHeight;
4157a84e134Smrg    preferred->width = w->box.preferred_width = preferred_width;
4167a84e134Smrg    preferred->height = w->box.preferred_height = preferred_height;
4177a84e134Smrg
4187a84e134Smrg    if (constraint->request_mode == (CWWidth|CWHeight)
4197a84e134Smrg	&& constraint->width == preferred_width
4207a84e134Smrg	&& constraint->height == preferred_height)
4217a84e134Smrg	return (XtGeometryYes);
4227a84e134Smrg
4237a84e134Smrg    return (XtGeometryAlmost);
4247a84e134Smrg}
4257a84e134Smrg
4267a84e134Smrg/*
4277a84e134Smrg * Actually layout the box
4287a84e134Smrg */
4297a84e134Smrgstatic void
4307a84e134SmrgXawBoxResize(Widget w)
4317a84e134Smrg{
4327a84e134Smrg    Dimension tmp;
4337a84e134Smrg
4347a84e134Smrg    DoLayout((BoxWidget)w, XtWidth(w), XtHeight(w), &tmp, &tmp, True);
4357a84e134Smrg}
4367a84e134Smrg
4377a84e134Smrg/*
4387a84e134Smrg * Try to do a new layout within the current width and height;
4397a84e134Smrg * if that fails try to resize and do it within the box returne
4407a84e134Smrg * by XawBoxQueryGeometry
4417a84e134Smrg *
4427a84e134Smrg * TryNewLayout just says if it's possible, and doesn't actually move the kids
4437a84e134Smrg */
4447a84e134Smrgstatic Bool
4457a84e134SmrgTryNewLayout(BoxWidget bbw)
4467a84e134Smrg{
4477a84e134Smrg    Dimension 	preferred_width, preferred_height;
4487a84e134Smrg    Dimension	proposed_width, proposed_height;
4497a84e134Smrg    int		iterations;
4507a84e134Smrg
4517a84e134Smrg    DoLayout(bbw, bbw->core.width, bbw->core.height,
4527a84e134Smrg	     &preferred_width, &preferred_height, False);
4537a84e134Smrg
4547a84e134Smrg    /* at this point, preferred_width is guaranteed to not be greater
4557a84e134Smrg       than bbw->core.width unless some child is larger, so there's no
4567a84e134Smrg       point in re-computing another layout */
4577a84e134Smrg
4587a84e134Smrg    if (XtWidth(bbw) == preferred_width && XtHeight(bbw) == preferred_height)
4597a84e134Smrg	return (True);
4607a84e134Smrg
4617a84e134Smrg    /* let's see if our parent will go for a new size */
4627a84e134Smrg    iterations = 0;
4637a84e134Smrg    proposed_width = preferred_width;
4647a84e134Smrg    proposed_height = preferred_height;
4657a84e134Smrg    do {
4667a84e134Smrg	switch (XtMakeResizeRequest((Widget)bbw,proposed_width,proposed_height,
4677a84e134Smrg				     &proposed_width, &proposed_height)) {
4687a84e134Smrg	    case XtGeometryYes:
4697a84e134Smrg		return (True);
4707a84e134Smrg	    case XtGeometryNo:
4717a84e134Smrg		if (iterations > 0)
4727a84e134Smrg		    /* protect from malicious parents who change their minds */
4737a84e134Smrg		    DoLayout(bbw, bbw->core.width, bbw->core.height,
4747a84e134Smrg			     &preferred_width, &preferred_height, False);
4757a84e134Smrg		if (preferred_width <= XtWidth(bbw)
4767a84e134Smrg		    && preferred_height <= XtHeight(bbw))
4777a84e134Smrg		    return (True);
4787a84e134Smrg		else
4797a84e134Smrg		    return (False);
4807a84e134Smrg	    case XtGeometryAlmost:
4817a84e134Smrg		if (proposed_height >= preferred_height &&
4827a84e134Smrg		    proposed_width >= preferred_width) {
4837a84e134Smrg		    /*
4847a84e134Smrg		     * Take it, and assume the parent knows what it is doing.
4857a84e134Smrg		     *
4867a84e134Smrg		     * The parent must accept this since it was returned in
4877a84e134Smrg		     * almost.
4887a84e134Smrg		     */
4897a84e134Smrg		    (void)XtMakeResizeRequest((Widget)bbw,
4907a84e134Smrg					       proposed_width, proposed_height,
4917a84e134Smrg					       &proposed_width, &proposed_height);
4927a84e134Smrg		    return (True);
4937a84e134Smrg		}
4947a84e134Smrg		else if (proposed_width != preferred_width) {
4957a84e134Smrg		    /* recalc bounding box; height might change */
4967a84e134Smrg		    DoLayout(bbw, proposed_width, 0,
4977a84e134Smrg			     &preferred_width, &preferred_height, False);
4987a84e134Smrg		    proposed_height = preferred_height;
4997a84e134Smrg		}
5007a84e134Smrg		else {	/* proposed_height != preferred_height */
5017a84e134Smrg		    XtWidgetGeometry constraints, reply;
5027a84e134Smrg
5037a84e134Smrg		    constraints.request_mode = CWHeight;
5047a84e134Smrg		    constraints.height = proposed_height;
5057a84e134Smrg		    (void)XawBoxQueryGeometry((Widget)bbw, &constraints, &reply);
5067a84e134Smrg		    proposed_width = preferred_width;
5077a84e134Smrg		}
5087a84e134Smrg		/*FALLTHROUGH*/
5097a84e134Smrg	    default:
5107a84e134Smrg		break;
5117a84e134Smrg	}
5127a84e134Smrg	iterations++;
5137a84e134Smrg    } while (iterations < 10);
5147a84e134Smrg
5157a84e134Smrg    return (False);
5167a84e134Smrg}
5177a84e134Smrg
5187a84e134Smrg/*
5197a84e134Smrg * Geometry Manager
5207a84e134Smrg *
5217a84e134Smrg * 'reply' is unused; we say only yeay or nay, never almost.
5227a84e134Smrg */
5237a84e134Smrg/*ARGSUSED*/
5247a84e134Smrgstatic XtGeometryResult
5257a84e134SmrgXawBoxGeometryManager(Widget w, XtWidgetGeometry *request,
5267a84e134Smrg		      XtWidgetGeometry *reply)
5277a84e134Smrg{
5287a84e134Smrg    Dimension	width, height, borderWidth;
5297a84e134Smrg    BoxWidget bbw;
5307a84e134Smrg
5317a84e134Smrg    /* Position request always denied */
5327a84e134Smrg    if (((request->request_mode & CWX) && request->x != XtX(w))
5337a84e134Smrg	|| ((request->request_mode & CWY) && request->y != XtY(w)))
5347a84e134Smrg        return (XtGeometryNo);
5357a84e134Smrg
5367a84e134Smrg    /* Size changes must see if the new size can be accomodated */
5377a84e134Smrg    if (request->request_mode & (CWWidth | CWHeight | CWBorderWidth)) {
5387a84e134Smrg	/* Make all three fields in the request valid */
5397a84e134Smrg	if ((request->request_mode & CWWidth) == 0)
5407a84e134Smrg	    request->width = XtWidth(w);
5417a84e134Smrg	if ((request->request_mode & CWHeight) == 0)
5427a84e134Smrg	    request->height = XtHeight(w);
5437a84e134Smrg        if ((request->request_mode & CWBorderWidth) == 0)
5447a84e134Smrg	    request->border_width = XtBorderWidth(w);
5457a84e134Smrg
5467a84e134Smrg	/* Save current size and set to new size */
5477a84e134Smrg      width = XtWidth(w);
5487a84e134Smrg      height = XtHeight(w);
5497a84e134Smrg      borderWidth = XtBorderWidth(w);
5507a84e134Smrg      XtWidth(w) = request->width;
5517a84e134Smrg      XtHeight(w) = request->height;
5527a84e134Smrg      XtBorderWidth(w) = request->border_width;
5537a84e134Smrg
5547a84e134Smrg      /* Decide if new layout works:
5557a84e134Smrg	 (1) new widget is smaller,
5567a84e134Smrg	 (2) new widget fits in existing Box,
5577a84e134Smrg	 (3) Box can be expanded to allow new widget to fit
5587a84e134Smrg      */
5597a84e134Smrg
5607a84e134Smrg	bbw = (BoxWidget) w->core.parent;
5617a84e134Smrg
5627a84e134Smrg	if (TryNewLayout(bbw)) {
5637a84e134Smrg	    /* Fits in existing or new space, relayout */
5647a84e134Smrg	    (*XtClass((Widget)bbw)->core_class.resize)((Widget)bbw);
5657a84e134Smrg	    return (XtGeometryYes);
5667a84e134Smrg	}
5677a84e134Smrg	else {
5687a84e134Smrg	    /* Cannot satisfy request, change back to original geometry */
5697a84e134Smrg	    XtWidth(w) = width;
5707a84e134Smrg	    XtHeight(w) = height;
5717a84e134Smrg	    XtBorderWidth(w) = borderWidth;
5727a84e134Smrg	    return (XtGeometryNo);
5737a84e134Smrg	}
5747a84e134Smrg    }
5757a84e134Smrg
5767a84e134Smrg    /* Any stacking changes don't make a difference, so allow if that's all */
5777a84e134Smrg    return (XtGeometryYes);
5787a84e134Smrg}
5797a84e134Smrg
5807a84e134Smrgstatic void
5817a84e134SmrgXawBoxChangeManaged(Widget w)
5827a84e134Smrg{
5837a84e134Smrg    /* Reconfigure the box */
5847a84e134Smrg    (void)TryNewLayout((BoxWidget)w);
5857a84e134Smrg    XawBoxResize(w);
5867a84e134Smrg}
5877a84e134Smrg
5887a84e134Smrgstatic void
5897a84e134SmrgXawBoxClassInitialize(void)
5907a84e134Smrg{
5917a84e134Smrg    XawInitializeWidgetSet();
5927a84e134Smrg    XtAddConverter(XtRString, XtROrientation, XmuCvtStringToOrientation,
5937a84e134Smrg		   NULL, 0);
5947a84e134Smrg    XtSetTypeConverter(XtROrientation, XtRString, XmuCvtOrientationToString,
5957a84e134Smrg		       NULL, 0, XtCacheNone, NULL);
5967a84e134Smrg}
5977a84e134Smrg
5987a84e134Smrg/*ARGSUSED*/
5997a84e134Smrgstatic void
6007a84e134SmrgXawBoxInitialize(Widget request, Widget cnew,
6017a84e134Smrg		 ArgList args, Cardinal *num_args)
6027a84e134Smrg{
6037a84e134Smrg    BoxWidget newbbw = (BoxWidget)cnew;
6047a84e134Smrg
6057a84e134Smrg    newbbw->box.last_query_mode = CWWidth | CWHeight;
6067a84e134Smrg    newbbw->box.last_query_width = newbbw->box.last_query_height = 0;
6077a84e134Smrg    newbbw->box.preferred_width = Max(newbbw->box.h_space, 1);
6087a84e134Smrg    newbbw->box.preferred_height = Max(newbbw->box.v_space, 1);
6097a84e134Smrg
6107a84e134Smrg    if (XtWidth(newbbw) == 0)
6117a84e134Smrg	XtWidth(newbbw) = newbbw->box.preferred_width;
6127a84e134Smrg
6137a84e134Smrg    if (XtHeight(newbbw) == 0)
6147a84e134Smrg	XtHeight(newbbw) = newbbw->box.preferred_height;
6157a84e134Smrg}
6167a84e134Smrg
6177a84e134Smrgstatic void
6187a84e134SmrgXawBoxRealize(Widget w, Mask *valueMask, XSetWindowAttributes *attributes)
6197a84e134Smrg{
6207a84e134Smrg#ifndef OLDXAW
6217a84e134Smrg    XawPixmap *pixmap;
6227a84e134Smrg#endif
6237a84e134Smrg
6247a84e134Smrg    XtCreateWindow(w, InputOutput, (Visual *)CopyFromParent,
6257a84e134Smrg		   *valueMask, attributes);
6267a84e134Smrg
6277a84e134Smrg#ifndef OLDXAW
6287a84e134Smrg    if (w->core.background_pixmap > XtUnspecifiedPixmap) {
6297a84e134Smrg	pixmap = XawPixmapFromXPixmap(w->core.background_pixmap, XtScreen(w),
6307a84e134Smrg				      w->core.colormap, w->core.depth);
6317a84e134Smrg	if (pixmap && pixmap->mask)
6327a84e134Smrg	    XawReshapeWidget(w, pixmap);
6337a84e134Smrg    }
6347a84e134Smrg#endif
6357a84e134Smrg}
6367a84e134Smrg
6377a84e134Smrg/*ARGSUSED*/
6387a84e134Smrgstatic Boolean
6397a84e134SmrgXawBoxSetValues(Widget current, Widget request, Widget cnew,
6407a84e134Smrg		ArgList args, Cardinal *num_args)
6417a84e134Smrg{
6427a84e134Smrg     /* need to relayout if h_space or v_space change */
6437a84e134Smrg#ifndef OLDXAW
6447a84e134Smrg    BoxWidget b_old = (BoxWidget)current;
6457a84e134Smrg    BoxWidget b_new = (BoxWidget)cnew;
6467a84e134Smrg
6477a84e134Smrg    if (b_old->core.background_pixmap != b_new->core.background_pixmap) {
6487a84e134Smrg	XawPixmap *opix, *npix;
6497a84e134Smrg
6507a84e134Smrg	opix = XawPixmapFromXPixmap(b_old->core.background_pixmap,
6517a84e134Smrg				    XtScreen(b_old), b_old->core.colormap,
6527a84e134Smrg				    b_old->core.depth);
6537a84e134Smrg	npix = XawPixmapFromXPixmap(b_new->core.background_pixmap,
6547a84e134Smrg				    XtScreen(b_new), b_new->core.colormap,
6557a84e134Smrg				    b_new->core.depth);
6567a84e134Smrg	if ((npix && npix->mask) || (opix && opix->mask))
6577a84e134Smrg	    XawReshapeWidget(cnew, npix);
6587a84e134Smrg    }
6597a84e134Smrg#endif /* OLDXAW */
6607a84e134Smrg
6617a84e134Smrg  return (False);
6627a84e134Smrg}
6637a84e134Smrg
6647a84e134Smrg#ifndef OLDXAW
6657a84e134Smrgstatic void
6667a84e134SmrgXawBoxExpose(Widget w, XEvent *event, Region region)
6677a84e134Smrg{
6687a84e134Smrg    BoxWidget xaw = (BoxWidget)w;
6697a84e134Smrg
6707a84e134Smrg    if (xaw->box.display_list)
6717a84e134Smrg	XawRunDisplayList(w, xaw->box.display_list, event, region);
6727a84e134Smrg}
6737a84e134Smrg#endif /* OLDXAW */
674