home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xaw / Box.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-17  |  17.9 KB  |  579 lines

  1. /* $XConsortium: Box.c,v 1.45 90/12/31 10:22:09 gildea Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27.  
  28. /* 
  29.  * Box.c - Box composite widget
  30.  * 
  31.  */
  32.  
  33. #include    <X11/IntrinsicP.h>
  34. #include    <X11/StringDefs.h>
  35. #include    <X11/Xmu/Misc.h>
  36. #include    <X11/Xaw/XawInit.h>
  37. #include    <X11/Xaw/BoxP.h>
  38.  
  39. /****************************************************************
  40.  *
  41.  * Box Resources
  42.  *
  43.  ****************************************************************/
  44.  
  45. static XtResource resources[] = {
  46.     { XtNhSpace, XtCHSpace, XtRDimension, sizeof(Dimension),
  47.         XtOffsetOf(BoxRec, box.h_space),
  48.         XtRImmediate, (XtPointer)4 },
  49.     { XtNvSpace, XtCVSpace, XtRDimension, sizeof(Dimension),
  50.         XtOffsetOf(BoxRec, box.v_space),
  51.         XtRImmediate, (XtPointer)4 },
  52.     { XtNorientation, XtCOrientation, XtROrientation, sizeof(XtOrientation),
  53.         XtOffsetOf(BoxRec, box.orientation),
  54.         XtRImmediate, (XtPointer)XtorientVertical },
  55. };
  56.  
  57. /****************************************************************
  58.  *
  59.  * Full class record constant
  60.  *
  61.  ****************************************************************/
  62.  
  63. static void ClassInitialize();
  64. static void Initialize();
  65. static void Realize();
  66. static void Resize();
  67. static Boolean SetValues();
  68. static XtGeometryResult GeometryManager();
  69. static void ChangeManaged();
  70. static XtGeometryResult PreferredSize();
  71.  
  72. BoxClassRec boxClassRec = {
  73.   {
  74. /* core_class fields      */
  75.     /* superclass         */    (WidgetClass) &compositeClassRec,
  76.     /* class_name         */    "Box",
  77.     /* widget_size        */    sizeof(BoxRec),
  78.     /* class_initialize   */    ClassInitialize,
  79.     /* class_part_init    */    NULL,
  80.     /* class_inited       */    FALSE,
  81.     /* initialize         */    Initialize,
  82.     /* initialize_hook    */    NULL,
  83.     /* realize            */    Realize,
  84.     /* actions            */    NULL,
  85.     /* num_actions      */    0,
  86.     /* resources          */    resources,
  87.     /* num_resources      */    XtNumber(resources),
  88.     /* xrm_class          */    NULLQUARK,
  89.     /* compress_motion      */    TRUE,
  90.     /* compress_exposure  */    TRUE,
  91.     /* compress_enterleave*/    TRUE,
  92.     /* visible_interest   */    FALSE,
  93.     /* destroy            */    NULL,
  94.     /* resize             */    Resize,
  95.     /* expose             */    NULL,
  96.     /* set_values         */    SetValues,
  97.     /* set_values_hook    */    NULL,
  98.     /* set_values_almost  */    XtInheritSetValuesAlmost,
  99.     /* get_values_hook    */    NULL,
  100.     /* accept_focus       */    NULL,
  101.     /* version            */    XtVersion,
  102.     /* callback_private   */    NULL,
  103.     /* tm_table           */    NULL,
  104.     /* query_geometry     */    PreferredSize,
  105.     /* display_accelerator*/    XtInheritDisplayAccelerator,
  106.     /* extension          */    NULL
  107.   },{
  108. /* composite_class fields */
  109.     /* geometry_manager   */    GeometryManager,
  110.     /* change_managed     */    ChangeManaged,
  111.     /* insert_child      */    XtInheritInsertChild,
  112.     /* delete_child      */    XtInheritDeleteChild,
  113.     /* extension          */    NULL
  114.   },{
  115. /* Box class fields */
  116.     /* empty          */    0,
  117.   }
  118. };
  119.  
  120. WidgetClass boxWidgetClass = (WidgetClass)&boxClassRec;
  121.  
  122.  
  123. /****************************************************************
  124.  *
  125.  * Private Routines
  126.  *
  127.  ****************************************************************/
  128.  
  129. /*
  130.  *
  131.  * Do a layout, either actually assigning positions, or just calculating size.
  132.  * Returns minimum width and height that will preserve the same layout.
  133.  *
  134.  */
  135.  
  136. static DoLayout(bbw, width, height, reply_width, reply_height, position)
  137.     BoxWidget    bbw;
  138.     Dimension    width, height;
  139.     Dimension    *reply_width, *reply_height; /* bounding box */
  140.     Boolean    position;    /* actually reposition the windows? */
  141. {
  142.     Boolean vbox = (bbw->box.orientation == XtorientVertical);
  143.     Cardinal  i;
  144.     Dimension w, h;    /* Width and height needed for box         */
  145.     Dimension lw, lh;    /* Width and height needed for current line     */
  146.     Dimension bw, bh;    /* Width and height needed for current widget     */
  147.     Dimension h_space;  /* Local copy of bbw->box.h_space         */
  148.     register Widget widget;    /* Current widget             */
  149.     int num_mapped_children = 0;
  150.  
  151.     /* Box width and height */
  152.     h_space = bbw->box.h_space;
  153.     w = h_space;
  154.     h = bbw->box.v_space;
  155.    
  156.     /* Line width and height */
  157.     lh = 0;
  158.     lw = h_space;
  159.   
  160.     for (i = 0; i < bbw->composite.num_children; i++) {
  161.     widget = bbw->composite.children[i];
  162.     if (widget->core.managed) {
  163.         if (widget->core.mapped_when_managed) num_mapped_children++;
  164.         /* Compute widget width */
  165.         bw = widget->core.width + 2*widget->core.border_width + h_space;
  166.         if ((Dimension)(lw + bw) > width) {
  167.         if (lw > h_space) {
  168.             /* At least one widget on this line, and
  169.              * can't fit any more.  Start new line if vbox.
  170.              */
  171.             AssignMax(w, lw);
  172.             if (vbox) {
  173.             h += lh + bbw->box.v_space;
  174.             lh = 0;
  175.             lw = h_space;
  176.             }
  177.         }
  178.         else if (!position) {
  179.             /* too narrow for this widget; we'll assume we can grow */
  180.             DoLayout(bbw, lw + bw, height, reply_width,
  181.                  reply_height, position);
  182.             return;
  183.         }
  184.         }
  185.         if (position && (lw != widget->core.x || h != widget->core.y)) {
  186.         /* It would be nice to use window gravity, but there isn't
  187.          * sufficient fine-grain control to nicely handle all
  188.          * situations (e.g. when only the height changes --
  189.          * a common case).  Explicit unmapping is a cheap hack
  190.          * to speed things up & avoid the visual jitter as
  191.          * things slide around.
  192.          *
  193.          * %%% perhaps there should be a client resource to
  194.          * control this.  If so, we'll have to optimize to
  195.          * perform the moves from the correct end so we don't
  196.          * force extra exposures as children occlude each other.
  197.          */
  198.         if (XtIsRealized(widget) && widget->core.mapped_when_managed)
  199.             XUnmapWindow( XtDisplay(widget), XtWindow(widget) );
  200.         XtMoveWidget(widget, (int)lw, (int)h);
  201.         }
  202.         lw += bw;
  203.         bh = widget->core.height + 2*widget->core.border_width;
  204.         AssignMax(lh, bh);
  205.     } /* if managed */
  206.     } /* for */
  207.  
  208.     if (!vbox && width && lw > width && lh < height) {
  209.     /* reduce width if too wide and height not filled */
  210.     Dimension sw = lw, sh = lh;
  211.     Dimension width_needed;
  212.     XtOrientation orientation = bbw->box.orientation;
  213.     bbw->box.orientation = XtorientVertical;
  214.     while (sh < height && sw > width) {
  215.         width_needed = sw;
  216.         DoLayout(bbw, sw-1, height, &sw, &sh, False);
  217.     }
  218.     if (sh < height) width_needed = sw;
  219.     if (width_needed != lw) {
  220.         DoLayout(bbw,width_needed,height,reply_width,reply_height,position);
  221.         bbw->box.orientation = orientation;
  222.         return;
  223.     }
  224.     bbw->box.orientation = orientation;
  225.     }
  226.  
  227.     if (position && XtIsRealized((Widget)bbw)) {
  228.     if (bbw->composite.num_children == num_mapped_children)
  229.         XMapSubwindows( XtDisplay((Widget)bbw), XtWindow((Widget)bbw) );
  230.     else {
  231.         int i = bbw->composite.num_children;
  232.         register Widget *childP = bbw->composite.children;
  233.         for (; i > 0; childP++, i--)
  234.         if (XtIsRealized(*childP) && XtIsManaged(*childP) &&
  235.             (*childP)->core.mapped_when_managed)
  236.             XtMapWidget(*childP);
  237.     }
  238.     }
  239.  
  240.     /* Finish off last line */
  241.     if (lw > h_space) {
  242.     AssignMax(w, lw);
  243.         h += lh + bbw->box.v_space;
  244.     }
  245.  
  246.     *reply_width = Max(w, 1);
  247.     *reply_height = Max(h, 1);
  248. }
  249.  
  250. /*
  251.  *
  252.  * Calculate preferred size, given constraining box, caching it in the widget.
  253.  *
  254.  */
  255.  
  256. static XtGeometryResult PreferredSize(widget, constraint, preferred)
  257.     Widget widget;
  258.     XtWidgetGeometry *constraint, *preferred;
  259. {
  260.     BoxWidget w = (BoxWidget)widget;
  261.     Dimension width /*, height */;
  262.     Dimension preferred_width = w->box.preferred_width;
  263.     Dimension preferred_height = w->box.preferred_height;
  264.  
  265.     constraint->request_mode &= CWWidth | CWHeight;
  266.  
  267.     if (constraint->request_mode == 0)
  268.     /* parent isn't going to change w or h, so nothing to re-compute */
  269.     return XtGeometryYes;
  270.  
  271.     if (constraint->request_mode == w->box.last_query_mode &&
  272.     (!(constraint->request_mode & CWWidth) ||
  273.      constraint->width == w->box.last_query_width) &&
  274.     (!(constraint->request_mode & CWHeight) ||
  275.      constraint->height == w->box.last_query_height)) {
  276.     /* same query; current preferences are still valid */
  277.     preferred->request_mode = CWWidth | CWHeight;
  278.     preferred->width = preferred_width;
  279.     preferred->height = preferred_height;
  280.     if (constraint->request_mode == (CWWidth | CWHeight) &&
  281.         constraint->width == preferred_width &&
  282.         constraint->height == preferred_height)
  283.         return XtGeometryYes;
  284.     else
  285.         return XtGeometryAlmost;
  286.     }
  287.     
  288.     /* else gotta do it the long way...
  289.        I have a preference for tall and narrow, so if my width is
  290.        constrained, I'll accept it; otherwise, I'll compute the minimum
  291.        width that will fit me within the height constraint */
  292.  
  293.     w->box.last_query_mode = constraint->request_mode;
  294.     w->box.last_query_width = constraint->width;
  295.     w->box.last_query_height= constraint->height;
  296.  
  297.     if (constraint->request_mode & CWWidth)
  298.     width = constraint->width;
  299.     else /* if (constraint->request_mode & CWHeight) */ {
  300.      /* let's see if I can become any narrower */
  301.     width = 0;
  302.     constraint->width = 65535;
  303.     }
  304.  
  305.     /* height is currently ignored by DoLayout.
  306.        height = (constraint->request_mode & CWHeight) ? constraint->height
  307.                : *preferred_height;
  308.      */
  309.     DoLayout(w, width, (Dimension)0,
  310.          &preferred_width, &preferred_height, FALSE);
  311.  
  312.     if (constraint->request_mode & CWHeight &&
  313.     preferred_height > constraint->height) {
  314.     /* find minimum width for this height */
  315.     if (preferred_width > constraint->width) {
  316.         /* punt; over-constrained */
  317.     }
  318.     else {
  319.         width = preferred_width;
  320.         do { /* find some width big enough to stay within this height */
  321.         width *= 2;
  322.         if (width > constraint->width) width = constraint->width;
  323.         DoLayout(w, width, 0, &preferred_width, &preferred_height, FALSE);
  324.         } while (preferred_height > constraint->height &&
  325.              width < constraint->width);
  326.         if (width != constraint->width) {
  327.         do { /* find minimum width */
  328.             width = preferred_width;
  329.             DoLayout(w, preferred_width-1, 0,
  330.                  &preferred_width, &preferred_height, FALSE);
  331.         } while (preferred_height < constraint->height);
  332.         /* one last time */
  333.         DoLayout(w, width, 0, &preferred_width, &preferred_height, FALSE);
  334.         }
  335.     }
  336.     }
  337.  
  338.     preferred->request_mode = CWWidth | CWHeight;
  339.     preferred->width = w->box.preferred_width = preferred_width;
  340.     preferred->height = w->box.preferred_height = preferred_height;
  341.  
  342.     if (constraint->request_mode == (CWWidth|CWHeight)
  343.     && constraint->width == preferred_width
  344.     && constraint->height == preferred_height)
  345.     return XtGeometryYes;
  346.     else
  347.     return XtGeometryAlmost;
  348.  
  349. }
  350.  
  351. /*
  352.  *
  353.  * Actually layout the box
  354.  *
  355.  */
  356.  
  357. static void Resize(w)
  358.     Widget    w;
  359. {
  360.     Dimension junk;
  361.  
  362.     DoLayout((BoxWidget)w, w->core.width, w->core.height, &junk, &junk, TRUE);
  363.  
  364. } /* Resize */
  365.  
  366. /*
  367.  *
  368.  * Try to do a new layout within the current width and height;
  369.  * if that fails try to resize and do it within the box returne
  370.  * by PreferredSize.
  371.  *
  372.  * TryNewLayout just says if it's possible, and doesn't actually move the kids
  373.  */
  374.  
  375. static Boolean TryNewLayout(bbw)
  376.     BoxWidget    bbw;
  377. {
  378.     Dimension     preferred_width, preferred_height;
  379.     Dimension    proposed_width, proposed_height;
  380.     int        iterations;
  381.  
  382.     DoLayout( bbw, bbw->core.width, bbw->core.height,
  383.           &preferred_width, &preferred_height, FALSE );
  384.  
  385.     /* at this point, preferred_width is guaranteed to not be greater
  386.        than bbw->core.width unless some child is larger, so there's no
  387.        point in re-computing another layout */
  388.  
  389.     if ((bbw->core.width == preferred_width) &&
  390.     (bbw->core.height == preferred_height)) {
  391.         /* Same size */
  392.     return (TRUE);
  393.     }
  394.  
  395.     /* let's see if our parent will go for a new size. */
  396.     iterations = 0;
  397.     proposed_width = preferred_width;
  398.     proposed_height = preferred_height;
  399.     do {
  400.     switch (XtMakeResizeRequest((Widget)bbw,proposed_width,proposed_height,
  401.                      &proposed_width, &proposed_height))
  402.     {
  403.         case XtGeometryYes:
  404.         return (TRUE);
  405.  
  406.         case XtGeometryNo:
  407.         if (iterations > 0)
  408.             /* protect from malicious parents who change their minds */
  409.             DoLayout( bbw, bbw->core.width, bbw->core.height,
  410.                   &preferred_width, &preferred_height, FALSE );
  411.         if ((preferred_width <= bbw->core.width) &&
  412.             (preferred_height <= bbw->core.height))
  413.             return (TRUE);
  414.         else
  415.             return (FALSE);
  416.  
  417.         case XtGeometryAlmost:
  418.         if (proposed_height >= preferred_height &&
  419.             proposed_width >= preferred_width) {
  420.  
  421.             /*
  422.              * Take it, and assume the parent knows what it is doing.
  423.              *
  424.              * The parent must accept this since it was returned in
  425.              * almost.
  426.              *
  427.              */
  428.             (void) XtMakeResizeRequest( (Widget)bbw,
  429.                        proposed_width, proposed_height,
  430.                        &proposed_width, &proposed_height);
  431.             return(TRUE);
  432.         }
  433.         else if (proposed_width != preferred_width) {
  434.             /* recalc bounding box; height might change */
  435.             DoLayout(bbw, proposed_width, 0,
  436.                  &preferred_width, &preferred_height, FALSE);
  437.             proposed_height = preferred_height;
  438.         }
  439.         else { /* proposed_height != preferred_height */
  440.             XtWidgetGeometry constraints, reply;
  441.             constraints.request_mode = CWHeight;
  442.             constraints.height = proposed_height;
  443.             (void)PreferredSize((Widget)bbw, &constraints, &reply);
  444.             proposed_width = preferred_width;
  445.         }
  446.     }
  447.     iterations++;
  448.     } while (iterations < 10);
  449.     return (FALSE);
  450. }
  451.  
  452. /*
  453.  *
  454.  * Geometry Manager
  455.  *
  456.  * 'reply' is unused; we say only yeay or nay, never almost.
  457.  *
  458.  */
  459.  
  460. /*ARGSUSED*/
  461. static XtGeometryResult GeometryManager(w, request, reply)
  462.     Widget        w;
  463.     XtWidgetGeometry    *request;
  464.     XtWidgetGeometry    *reply;    /* RETURN */
  465.  
  466. {
  467.     Dimension    width, height, borderWidth;
  468.     BoxWidget bbw;
  469.  
  470.     /* Position request always denied */
  471.     if ((request->request_mode & CWX && request->x != w->core.x) ||
  472.     (request->request_mode & CWY && request->y != w->core.y))
  473.         return (XtGeometryNo);
  474.  
  475.     /* Size changes must see if the new size can be accomodated */
  476.     if (request->request_mode & (CWWidth | CWHeight | CWBorderWidth)) {
  477.  
  478.     /* Make all three fields in the request valid */
  479.     if ((request->request_mode & CWWidth) == 0)
  480.         request->width = w->core.width;
  481.     if ((request->request_mode & CWHeight) == 0)
  482.         request->height = w->core.height;
  483.         if ((request->request_mode & CWBorderWidth) == 0)
  484.         request->border_width = w->core.border_width;
  485.  
  486.     /* Save current size and set to new size */
  487.     width = w->core.width;
  488.     height = w->core.height;
  489.     borderWidth = w->core.border_width;
  490.     w->core.width = request->width;
  491.     w->core.height = request->height;
  492.     w->core.border_width = request->border_width;
  493.  
  494.     /* Decide if new layout works: (1) new widget is smaller,
  495.        (2) new widget fits in existing Box, (3) Box can be
  496.        expanded to allow new widget to fit */
  497.  
  498.     bbw = (BoxWidget) w->core.parent;
  499.  
  500. /* whenever a child changes his geometry, we attempt to
  501.  * change ours to be the minimum enclosing size...
  502.     if (((request->width + request->border_width <= width + borderWidth) &&
  503.         (request->height + request->border_width <= height + borderWidth))
  504.     || bbw->box.preferred_width < bbw->core.width
  505.     || bbw->box.preferred_height < bbw->core.height
  506.     || TryNewLayout(bbw)) {
  507.  */
  508.     if (TryNewLayout(bbw)) {
  509.         /* Fits in existing or new space, relayout */
  510.         (*XtClass((Widget)bbw)->core_class.resize)((Widget)bbw);
  511.         return (XtGeometryYes);
  512.     } else {
  513.         /* Cannot satisfy request, change back to original geometry */
  514.         w->core.width = width;
  515.         w->core.height = height;
  516.         w->core.border_width = borderWidth;
  517.         return (XtGeometryNo);
  518.     }
  519.     }; /* if any size changes requested */
  520.  
  521.     /* Any stacking changes don't make a difference, so allow if that's all */
  522.     return (XtGeometryYes);
  523. }
  524.  
  525. static void ChangeManaged(w)
  526.     Widget w;
  527. {
  528.     /* Reconfigure the box */
  529.     (void) TryNewLayout((BoxWidget)w);
  530.     Resize(w);
  531. }
  532.  
  533. static void ClassInitialize()
  534. {
  535.     XawInitializeWidgetSet();
  536.     XtAddConverter( XtRString, XtROrientation, XmuCvtStringToOrientation,
  537.             NULL, (Cardinal)0 );
  538. }
  539.  
  540. /* ARGSUSED */
  541. static void Initialize(request, new)
  542.     Widget request, new;
  543. {
  544.     BoxWidget newbbw = (BoxWidget)new;
  545.  
  546.     newbbw->box.last_query_mode = CWWidth | CWHeight;
  547.     newbbw->box.last_query_width = newbbw->box.last_query_height = 0;
  548.     newbbw->box.preferred_width = Max(newbbw->box.h_space, 1);
  549.     newbbw->box.preferred_height = Max(newbbw->box.v_space, 1);
  550.  
  551.     if (newbbw->core.width == 0)
  552.         newbbw->core.width = newbbw->box.preferred_width;
  553.  
  554.     if (newbbw->core.height == 0)
  555.     newbbw->core.height = newbbw->box.preferred_height;
  556.  
  557. } /* Initialize */
  558.  
  559. static void Realize(w, valueMask, attributes)
  560.     register Widget w;
  561.     Mask *valueMask;
  562.     XSetWindowAttributes *attributes;
  563. {
  564.     attributes->bit_gravity = NorthWestGravity;
  565.     *valueMask |= CWBitGravity;
  566.  
  567.     XtCreateWindow( w, (unsigned)InputOutput, (Visual *)CopyFromParent,
  568.             *valueMask, attributes);
  569. } /* Realize */
  570.  
  571. /* ARGSUSED */
  572. static Boolean SetValues(current, request, new)
  573.     Widget current, request, new;
  574. {
  575.    /* need to relayout if h_space or v_space change */
  576.  
  577.     return False;
  578. }
  579.