home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / fontutils-0.6-base.tgz / fontutils-0.6-base.tar / fsf / fontutils / widgets / Box.c < prev    next >
C/C++ Source or Header  |  1992-08-19  |  19KB  |  592 lines

  1. /* Box.c: implementation of the Athena Box widget.  The DoLayout
  2.    procedure has been modified from the distributed version.
  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 void
  137. DoLayout(bbw, width, height, reply_width, reply_height, position)
  138.     BoxWidget    bbw;
  139.     Dimension    width, height;
  140.     Dimension    *reply_width, *reply_height; /* bounding box */
  141.     Boolean    position;    /* actually reposition the windows? */
  142. {
  143.     Boolean vbox = (bbw->box.orientation == XtorientVertical);
  144.     Cardinal  i;
  145.     Dimension w, h;    /* Width and height needed for box         */
  146.     Dimension lw, lh;    /* Width and height needed for current line     */
  147.     Dimension bw, bh;    /* Width and height needed for current widget     */
  148.     Dimension h_space;  /* Local copy of bbw->box.h_space         */
  149.     register Widget widget;    /* Current widget             */
  150.     int num_mapped_children = 0;
  151.  
  152.     /* Box width and height */
  153.     h_space = bbw->box.h_space;
  154.     w = h_space;
  155.     h = bbw->box.v_space;
  156.    
  157.     /* Line width and height */
  158.     lh = 0;
  159.     lw = h_space;
  160.   
  161.     for (i = 0; i < bbw->composite.num_children; i++) {
  162.     widget = bbw->composite.children[i];
  163.     if (widget->core.managed) {
  164.         if (widget->core.mapped_when_managed) num_mapped_children++;
  165.         /* Compute widget width */
  166.         bw = widget->core.width + 2*widget->core.border_width + h_space;
  167.         if (lw + bw > width) {
  168.         if (lw > h_space) {
  169.             /* At least one widget on this line, and
  170.              * can't fit any more.  Start new line if vbox.
  171.              */
  172.             AssignMax(w, lw);
  173.             if (vbox) {
  174.             h += lh + bbw->box.v_space;
  175.             lh = 0;
  176.             lw = h_space;
  177.             }
  178.         }
  179.         else if (!position) {
  180.             /* too narrow for this widget; we'll assume we can grow */
  181.             DoLayout(bbw, lw + bw, height, reply_width,
  182.                  reply_height, position);
  183.             return;
  184.         }
  185.         }
  186.         if (position && ((Position) lw != widget->core.x
  187.                              || (Position) h != widget->core.y)) {
  188.         /* It would be nice to use window gravity, but there isn't
  189.          * sufficient fine-grain control to nicely handle all
  190.          * situations (e.g. when only the height changes --
  191.          * a common case).  Explicit unmapping is a cheap hack
  192.          * to speed things up & avoid the visual jitter as
  193.          * things slide around.
  194.          *
  195.          * %%% perhaps there should be a client resource to
  196.          * control this.  If so, we'll have to optimize to
  197.          * perform the moves from the correct end so we don't
  198.          * force extra exposures as children occlude each other.
  199.          */
  200.         if (XtIsRealized(widget))
  201.             XUnmapWindow( XtDisplay(widget), XtWindow(widget) );
  202.         XtMoveWidget(bbw->composite.children[i], (int)lw, (int)h);
  203.         }
  204.         lw += bw;
  205.         bh = widget->core.height + 2*widget->core.border_width;
  206.         AssignMax(lh, bh);
  207.     } /* if managed */
  208.     } /* for */
  209.  
  210.     if (!vbox && width && lw > width && lh < height) {
  211.     /* reduce width if too wide and height not filled */
  212.     Dimension sw = lw, sh = lh;
  213.     Dimension width_needed;
  214.     XtOrientation orientation = bbw->box.orientation;
  215.     bbw->box.orientation = XtorientVertical;
  216.     while (sh < height && sw > width) {
  217.         width_needed = sw;
  218.         DoLayout(bbw, sw-1, height, &sw, &sh, False);
  219.     }
  220.     if (sh < height) width_needed = sw;
  221.     if (width_needed != lw) {
  222.         DoLayout(bbw,width_needed,height,reply_width,reply_height,position);
  223.         bbw->box.orientation = orientation;
  224.         return;
  225.     }
  226.     bbw->box.orientation = orientation;
  227.     }
  228.  
  229.     if (position && XtIsRealized((Widget)bbw)) {
  230.     if (bbw->composite.num_children == num_mapped_children)
  231.         XMapSubwindows( XtDisplay((Widget)bbw), XtWindow((Widget)bbw) );
  232.     else {
  233.         int i = num_mapped_children;
  234.         register Widget *childP = bbw->composite.children;
  235.         for (; i > 0; childP++) {
  236.         if (XtIsManaged(*childP) &&
  237.             (*childP)->core.mapped_when_managed) {
  238.                     /* If we do not ensure the child is realized,
  239.                        XtMapWidget can fail; e.g., kern items in xbfe.  */
  240.                     if (!XtIsRealized (*childP))
  241.                       XtRealizeWidget (*childP);
  242.             XtMapWidget(*childP);
  243.             i--;
  244.         }
  245.         }
  246.     }
  247.     }
  248.  
  249.     /* Finish off last line */
  250.     if (lw > h_space) {
  251.     AssignMax(w, lw);
  252.         h += lh + bbw->box.v_space;
  253.     }
  254.  
  255.     *reply_width = Max(w, 1);
  256.     *reply_height = Max(h, 1);
  257. }
  258.  
  259. /*
  260.  *
  261.  * Calculate preferred size, given constraining box, caching it in the widget.
  262.  *
  263.  */
  264.  
  265. static XtGeometryResult PreferredSize(widget, constraint, preferred)
  266.     Widget widget;
  267.     XtWidgetGeometry *constraint, *preferred;
  268. {
  269.     BoxWidget w = (BoxWidget)widget;
  270.     Dimension width /*, height */;
  271.     Dimension preferred_width = w->box.preferred_width;
  272.     Dimension preferred_height = w->box.preferred_height;
  273.  
  274.     constraint->request_mode &= CWWidth | CWHeight;
  275.  
  276.     if (constraint->request_mode == 0)
  277.     /* parent isn't going to change w or h, so nothing to re-compute */
  278.     return XtGeometryYes;
  279.  
  280.     if (constraint->request_mode == w->box.last_query_mode &&
  281.     (!(constraint->request_mode & CWWidth) ||
  282.      constraint->width == w->box.last_query_width) &&
  283.     (!(constraint->request_mode & CWHeight) ||
  284.      constraint->height == w->box.last_query_height)) {
  285.     /* same query; current preferences are still valid */
  286.     preferred->request_mode = CWWidth | CWHeight;
  287.     preferred->width = preferred_width;
  288.     preferred->height = preferred_height;
  289.     if (constraint->request_mode == (CWWidth | CWHeight) &&
  290.         constraint->width == preferred_width &&
  291.         constraint->height == preferred_height)
  292.         return XtGeometryYes;
  293.     else
  294.         return XtGeometryAlmost;
  295.     }
  296.     
  297.     /* else gotta do it the long way...
  298.        I have a preference for tall and narrow, so if my width is
  299.        constrained, I'll accept it; otherwise, I'll compute the minimum
  300.        width that will fit me within the height constraint */
  301.  
  302.     w->box.last_query_mode = constraint->request_mode;
  303.     w->box.last_query_width = constraint->width;
  304.     w->box.last_query_height= constraint->height;
  305.  
  306.     if (constraint->request_mode & CWWidth)
  307.     width = constraint->width;
  308.     else /* if (constraint->request_mode & CWHeight) */ {
  309.      /* let's see if I can become any narrower */
  310.     width = 0;
  311.     constraint->width = 65535;
  312.     }
  313.  
  314.     /* height is currently ignored by DoLayout.
  315.        height = (constraint->request_mode & CWHeight) ? constraint->height
  316.                : *preferred_height;
  317.      */
  318.     DoLayout(w, width, (Dimension)0,
  319.          &preferred_width, &preferred_height, FALSE);
  320.  
  321.     if (constraint->request_mode & CWHeight &&
  322.     preferred_height > constraint->height) {
  323.     /* find minimum width for this height */
  324.     if (preferred_width > constraint->width) {
  325.         /* punt; over-constrained */
  326.     }
  327.     else {
  328.         width = preferred_width;
  329.         do { /* find some width big enough to stay within this height */
  330.         width *= 2;
  331.         if (width > constraint->width) width = constraint->width;
  332.         DoLayout(w, width, 0, &preferred_width, &preferred_height, FALSE);
  333.         } while (preferred_height > constraint->height &&
  334.              width < constraint->width);
  335.         if (width != constraint->width) {
  336.         do { /* find minimum width */
  337.             width = preferred_width;
  338.             DoLayout(w, preferred_width-1, 0,
  339.                  &preferred_width, &preferred_height, FALSE);
  340.         } while (preferred_height < constraint->height);
  341.         /* one last time */
  342.         DoLayout(w, width, 0, &preferred_width, &preferred_height, FALSE);
  343.         }
  344.     }
  345.     }
  346.  
  347.     preferred->request_mode = CWWidth | CWHeight;
  348.     preferred->width = w->box.preferred_width = preferred_width;
  349.     preferred->height = w->box.preferred_height = preferred_height;
  350.  
  351.     if (constraint->request_mode == (CWWidth|CWHeight)
  352.     && constraint->width == preferred_width
  353.     && constraint->height == preferred_height)
  354.     return XtGeometryYes;
  355.     else
  356.     return XtGeometryAlmost;
  357.  
  358. }
  359.  
  360. /*
  361.  *
  362.  * Actually layout the box
  363.  *
  364.  */
  365.  
  366. static void Resize(w)
  367.     Widget    w;
  368. {
  369.     Dimension junk;
  370.  
  371.     DoLayout((BoxWidget)w, w->core.width, w->core.height, &junk, &junk, TRUE);
  372.  
  373. } /* Resize */
  374.  
  375. /*
  376.  *
  377.  * Try to do a new layout within the current width and height;
  378.  * if that fails try to resize and do it within the box returne
  379.  * by PreferredSize.
  380.  *
  381.  * TryNewLayout just says if it's possible, and doesn't actually move the kids
  382.  */
  383.  
  384. static Boolean TryNewLayout(bbw)
  385.     BoxWidget    bbw;
  386. {
  387.     Dimension     preferred_width, preferred_height;
  388.     Dimension    proposed_width, proposed_height;
  389.     int        iterations;
  390.  
  391.     DoLayout( bbw, bbw->core.width, bbw->core.height,
  392.           &preferred_width, &preferred_height, FALSE );
  393.  
  394.     /* at this point, preferred_width is guaranteed to not be greater
  395.        than bbw->core.width unless some child is larger, so there's no
  396.        point in re-computing another layout */
  397.  
  398.     if ((bbw->core.width == preferred_width) &&
  399.     (bbw->core.height == preferred_height)) {
  400.         /* Same size */
  401.     return (TRUE);
  402.     }
  403.  
  404.     /* let's see if our parent will go for a new size. */
  405.     iterations = 0;
  406.     proposed_width = preferred_width;
  407.     proposed_height = preferred_height;
  408.     do {
  409.     switch (XtMakeResizeRequest((Widget)bbw,proposed_width,proposed_height,
  410.                      &proposed_width, &proposed_height))
  411.     {
  412.         case XtGeometryYes:
  413.         return (TRUE);
  414.  
  415.         case XtGeometryNo:
  416.         if (iterations > 0)
  417.             /* protect from malicious parents who change their minds */
  418.             DoLayout( bbw, bbw->core.width, bbw->core.height,
  419.                   &preferred_width, &preferred_height, FALSE );
  420.         if ((preferred_width <= bbw->core.width) &&
  421.             (preferred_height <= bbw->core.height))
  422.             return (TRUE);
  423.         else
  424.             return (FALSE);
  425.  
  426.             /* I don't know what to do here.  */
  427.             case XtGeometryDone:
  428.         return TRUE;
  429.                 
  430.             case XtGeometryAlmost:
  431.         if (proposed_height >= preferred_height &&
  432.             proposed_width >= preferred_width) {
  433.  
  434.             /*
  435.              * Take it, and assume the parent knows what it is doing.
  436.              *
  437.              * The parent must accept this since it was returned in
  438.              * almost.
  439.              *
  440.              */
  441.             (void) XtMakeResizeRequest( (Widget)bbw,
  442.                        proposed_width, proposed_height,
  443.                        &proposed_width, &proposed_height);
  444.             return(TRUE);
  445.         }
  446.         else if (proposed_width != preferred_width) {
  447.             /* recalc bounding box; height might change */
  448.             DoLayout(bbw, proposed_width, 0,
  449.                  &preferred_width, &preferred_height, FALSE);
  450.             proposed_height = preferred_height;
  451.         }
  452.         else { /* proposed_height != preferred_height */
  453.             XtWidgetGeometry constraints, reply;
  454.             constraints.request_mode = CWHeight;
  455.             constraints.height = proposed_height;
  456.             (void)PreferredSize((Widget)bbw, &constraints, &reply);
  457.             proposed_width = preferred_width;
  458.         }
  459.     }
  460.     iterations++;
  461.     } while (iterations < 10);
  462.     return (FALSE);
  463. }
  464.  
  465. /*
  466.  *
  467.  * Geometry Manager
  468.  *
  469.  * 'reply' is unused; we say only yeay or nay, never almost.
  470.  *
  471.  */
  472.  
  473. /*ARGSUSED*/
  474. static XtGeometryResult GeometryManager(w, request, reply)
  475.     Widget        w;
  476.     XtWidgetGeometry    *request;
  477.     XtWidgetGeometry    *reply;    /* RETURN */
  478.  
  479. {
  480.     Dimension    width, height, borderWidth;
  481.     BoxWidget bbw;
  482.  
  483.     /* Position request always denied */
  484.     if ((request->request_mode & CWX && request->x != w->core.x) ||
  485.     (request->request_mode & CWY && request->y != w->core.y))
  486.         return (XtGeometryNo);
  487.  
  488.     /* Size changes must see if the new size can be accomodated */
  489.     if (request->request_mode & (CWWidth | CWHeight | CWBorderWidth)) {
  490.  
  491.     /* Make all three fields in the request valid */
  492.     if ((request->request_mode & CWWidth) == 0)
  493.         request->width = w->core.width;
  494.     if ((request->request_mode & CWHeight) == 0)
  495.         request->height = w->core.height;
  496.         if ((request->request_mode & CWBorderWidth) == 0)
  497.         request->border_width = w->core.border_width;
  498.  
  499.     /* Save current size and set to new size */
  500.     width = w->core.width;
  501.     height = w->core.height;
  502.     borderWidth = w->core.border_width;
  503.     w->core.width = request->width;
  504.     w->core.height = request->height;
  505.     w->core.border_width = request->border_width;
  506.  
  507.     /* Decide if new layout works: (1) new widget is smaller,
  508.        (2) new widget fits in existing Box, (3) Box can be
  509.        expanded to allow new widget to fit */
  510.  
  511.     bbw = (BoxWidget) w->core.parent;
  512.  
  513. /* whenever a child changes his geometry, we attempt to
  514.  * change ours to be the minimum enclosing size...
  515.     if (((request->width + request->border_width <= width + borderWidth) &&
  516.         (request->height + request->border_width <= height + borderWidth))
  517.     || bbw->box.preferred_width < bbw->core.width
  518.     || bbw->box.preferred_height < bbw->core.height
  519.     || TryNewLayout(bbw)) {
  520.  */
  521.     if (TryNewLayout(bbw)) {
  522.         /* Fits in existing or new space, relayout */
  523.         (*XtClass((Widget)bbw)->core_class.resize)((Widget)bbw);
  524.         return (XtGeometryYes);
  525.     } else {
  526.         /* Cannot satisfy request, change back to original geometry */
  527.         w->core.width = width;
  528.         w->core.height = height;
  529.         w->core.border_width = borderWidth;
  530.         return (XtGeometryNo);
  531.     }
  532.     }; /* if any size changes requested */
  533.  
  534.     /* Any stacking changes don't make a difference, so allow if that's all */
  535.     return (XtGeometryYes);
  536. }
  537.  
  538. static void ChangeManaged(w)
  539.     Widget w;
  540. {
  541.     /* Reconfigure the box */
  542.     (void) TryNewLayout((BoxWidget)w);
  543.     Resize(w);
  544. }
  545.  
  546. static void ClassInitialize()
  547. {
  548.     XawInitializeWidgetSet();
  549.     XtAddConverter( XtRString, XtROrientation, XmuCvtStringToOrientation,
  550.             NULL, (Cardinal)0 );
  551. }
  552.  
  553. /* ARGSUSED */
  554. static void Initialize(request, new)
  555.     Widget request, new;
  556. {
  557.     BoxWidget newbbw = (BoxWidget)new;
  558.  
  559.     newbbw->box.last_query_mode = CWWidth | CWHeight;
  560.     newbbw->box.last_query_width = newbbw->box.last_query_height = 0;
  561.     newbbw->box.preferred_width = Max(newbbw->box.h_space, 1);
  562.     newbbw->box.preferred_height = Max(newbbw->box.v_space, 1);
  563.  
  564.     if (newbbw->core.width == 0)
  565.         newbbw->core.width = newbbw->box.preferred_width;
  566.  
  567.     if (newbbw->core.height == 0)
  568.     newbbw->core.height = newbbw->box.preferred_height;
  569.  
  570. } /* Initialize */
  571.  
  572. static void Realize(w, valueMask, attributes)
  573.     register Widget w;
  574.     Mask *valueMask;
  575.     XSetWindowAttributes *attributes;
  576. {
  577.     attributes->bit_gravity = NorthWestGravity;
  578.     *valueMask |= CWBitGravity;
  579.  
  580.     XtCreateWindow( w, (unsigned)InputOutput, (Visual *)CopyFromParent,
  581.             *valueMask, attributes);
  582. } /* Realize */
  583.  
  584. /* ARGSUSED */
  585. static Boolean SetValues(current, request, new)
  586.     Widget current, request, new;
  587. {
  588.    /* need to relayout if h_space or v_space change */
  589.  
  590.     return False;
  591. }
  592.