home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / l / libxtgeo.zip / libgeo17 / Xt / Geometry.c next >
C/C++ Source or Header  |  1992-10-06  |  16KB  |  547 lines

  1. /* $XConsortium: Geometry.c,v 1.55 92/02/11 17:13:18 converse 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. #include "IntrinsicI.h"
  28. #include "ShellP.h"
  29.  
  30. static void ClearRectObjAreas(r, old)
  31.     RectObj r;
  32.     XWindowChanges* old;
  33. {
  34.     Widget pw = _XtWindowedAncestor((Widget)r);
  35.     int bw2;
  36.  
  37.     bw2 = old->border_width << 1;
  38.     XClearArea( XtDisplay(pw), XtWindow(pw),
  39.         old->x, old->y,
  40.         old->width + bw2, old->height + bw2,
  41.         TRUE );
  42.  
  43.     bw2 = r->rectangle.border_width << 1;
  44.     XClearArea( XtDisplay(pw), XtWindow(pw),
  45.         (int)r->rectangle.x, (int)r->rectangle.y,
  46.         (unsigned int)(r->rectangle.width + bw2),
  47.             (unsigned int)(r->rectangle.height + bw2),
  48.         TRUE );
  49. }
  50.  
  51. /*
  52.  * Internal function used by XtMakeGeometryRequest and XtSetValues.
  53.  * Returns more data than the public interface.  Does not convert
  54.  * XtGeometryDone to XtGeometryYes.
  55.  *
  56.  * clear_rect_obj - *** RETURNED ***  
  57.  *            TRUE if the rect obj has been cleared, false otherwise.
  58.  */
  59.  
  60. XtGeometryResult 
  61. _XtMakeGeometryRequest (widget, request, reply, clear_rect_obj)
  62.     Widget widget;
  63.     XtWidgetGeometry *request, *reply;
  64.     Boolean * clear_rect_obj;
  65. {
  66.     XtWidgetGeometry    junk;
  67.     XtGeometryHandler manager;
  68.     XtGeometryResult returnCode;
  69.     Widget parent = widget->core.parent;
  70.     XtGeometryMask    changeMask;
  71.     Boolean managed, parentRealized, rgm = False;
  72.     XWindowChanges changes;
  73.  
  74.     *clear_rect_obj = FALSE;
  75.  
  76.     if (XtIsShell(widget)) {
  77.     ShellClassExtension ext;
  78.     for (ext = (ShellClassExtension)((ShellWidgetClass)XtClass(widget))
  79.            ->shell_class.extension;
  80.          ext != NULL && ext->record_type != NULLQUARK;
  81.          ext = (ShellClassExtension)ext->next_extension);
  82.  
  83.     if (ext != NULL) {
  84.         if (  ext->version == XtShellExtensionVersion
  85.           && ext->record_size == sizeof(ShellClassExtensionRec)) {
  86.         manager = ext->root_geometry_manager;
  87.         rgm = True;
  88.         } else {
  89.         String params[1];
  90.         Cardinal num_params = 1;
  91.         params[0] = XtClass(widget)->core_class.class_name;
  92.         XtAppErrorMsg(XtWidgetToApplicationContext(widget),
  93.              "invalidExtension", "xtMakeGeometryRequest",
  94.              XtCXtToolkitError,
  95.              "widget class %s has invalid ShellClassExtension record",
  96.              params, &num_params);
  97.         }
  98.     } else {
  99.         XtAppErrorMsg(XtWidgetToApplicationContext(widget),
  100.               "internalError", "xtMakeGeometryRequest",
  101.               XtCXtToolkitError,
  102.               "internal error; ShellClassExtension is NULL",
  103.               NULL, NULL);
  104.     }
  105.     managed = True;
  106.     parentRealized = TRUE;
  107.     } else if (parent == NULL) {
  108.     XtAppErrorMsg(XtWidgetToApplicationContext(widget),
  109.               "invalidParent","xtMakeGeometryRequest",XtCXtToolkitError,
  110.               "non-shell has no parent in XtMakeGeometryRequest",
  111.               (String *)NULL, (Cardinal *)NULL);
  112.     } else /* not shell */ {
  113.     managed = XtIsManaged(widget);
  114.  
  115.     if (XtIsComposite(parent)) {
  116.         parentRealized = XtIsRealized(parent);
  117.         manager = ((CompositeWidgetClass) (parent->core.widget_class))
  118.             ->composite_class.geometry_manager;
  119.     } else if (managed) {
  120.         /* Should never happen - XtManageChildren should have checked */
  121.         XtAppErrorMsg(XtWidgetToApplicationContext(widget),
  122.               "invalidParent", "xtMakeGeometryRequest",
  123.               XtCXtToolkitError,
  124.               "XtMakeGeometryRequest - parent not composite",
  125.               (String *)NULL, (Cardinal *)NULL);
  126.     } else {
  127.         /* no need to waste time checking if parent is actually realized
  128.          * at this point; since the child is unmanaged we need to perform
  129.          * the configure iff the child is realized, so we dummy the
  130.          * parentRealized checks below.
  131.          */
  132.         parentRealized = TRUE;
  133.     }
  134.     }
  135.  
  136.     if (managed && manager == (XtGeometryHandler) NULL) {
  137.     XtErrorMsg("invalidGeometryManager","xtMakeGeometryRequest",
  138.                  XtCXtToolkitError,
  139.                  "XtMakeGeometryRequest - parent has no geometry manager",
  140.                   (String *)NULL, (Cardinal *)NULL);
  141.     }
  142.  
  143.     if (widget->core.being_destroyed) return XtGeometryNo;
  144.  
  145.     /* see if requesting anything to change */
  146.     changeMask = 0;
  147.     if (request->request_mode & CWStackMode
  148.     && request->stack_mode != XtSMDontChange) {
  149.         changeMask |= CWStackMode;
  150.         if (request->request_mode & CWSibling) {
  151.         XtCheckSubclass(request->sibling, rectObjClass,
  152.                 "XtMakeGeometryRequest");
  153.         changeMask |= CWSibling;
  154.         }
  155.     }
  156.     if (request->request_mode & CWX
  157.     && widget->core.x != request->x) changeMask |= CWX;
  158.     if (request->request_mode & CWY
  159.     && widget->core.y != request->y) changeMask |= CWY;
  160.     if (request->request_mode & CWWidth
  161.     && widget->core.width != request->width) changeMask |= CWWidth;
  162.     if (request->request_mode & CWHeight
  163.     && widget->core.height != request->height) changeMask |= CWHeight;
  164.     if (request->request_mode & CWBorderWidth
  165.     && widget->core.border_width != request->border_width)
  166.     changeMask |= CWBorderWidth;
  167.     if (! changeMask) return XtGeometryYes;
  168.     changeMask |= (request->request_mode & XtCWQueryOnly);
  169.  
  170.     if ( !(changeMask & XtCWQueryOnly) && XtIsRealized(widget) ) {
  171.     /* keep record of the current geometry so we know what's changed */
  172.     changes.x = widget->core.x ;
  173.     changes.y = widget->core.y ;
  174.     changes.width = widget->core.width ;
  175.     changes.height = widget->core.height ;
  176.     changes.border_width = widget->core.border_width ;
  177.     }
  178.  
  179.     if (!managed || !parentRealized) {
  180.     /* Don't get parent's manager involved--assume the answer is yes */
  181.     if (changeMask & XtCWQueryOnly) {
  182.         /* He was just asking, don't change anything, just tell him yes */
  183.         return XtGeometryYes;
  184.     } else {
  185.         /* copy values from request to widget */
  186.         if (request->request_mode & CWX)
  187.         widget->core.x = request->x;
  188.         if (request->request_mode & CWY)
  189.         widget->core.y = request->y;
  190.         if (request->request_mode & CWWidth)
  191.         widget->core.width = request->width;
  192.         if (request->request_mode & CWHeight)
  193.         widget->core.height = request->height;
  194.         if (request->request_mode & CWBorderWidth)
  195.         widget->core.border_width = request->border_width;
  196.         if (!parentRealized) return XtGeometryYes;
  197.         else returnCode = XtGeometryYes;
  198.     }
  199.     } else {
  200.     /* go ask the widget's geometry manager */
  201.     if (reply == (XtWidgetGeometry *) NULL) {
  202.         returnCode = (*manager)(widget, request, &junk);
  203.     } else {
  204.         returnCode = (*manager)(widget, request, reply);
  205.     }
  206.     }
  207.  
  208.     /*
  209.      * If Unrealized, not a XtGeometryYes, or a query-only then we are done.
  210.      */
  211.  
  212.     if ((returnCode != XtGeometryYes) || 
  213.     (changeMask & XtCWQueryOnly) || !XtIsRealized(widget)) {
  214.  
  215.     return returnCode;
  216.     }
  217.  
  218.     if (XtIsWidget(widget)) {    /* reconfigure the window (if needed) */
  219.  
  220.     if (rgm) return returnCode;
  221.  
  222.     if (changes.x != widget->core.x) {
  223.          changeMask |= CWX;
  224.          changes.x = widget->core.x;
  225.      }
  226.      if (changes.y != widget->core.y) {
  227.          changeMask |= CWY;
  228.          changes.y = widget->core.y;
  229.      }
  230.      if (changes.width != widget->core.width) {
  231.          changeMask |= CWWidth;
  232.          changes.width = widget->core.width;
  233.      }
  234.      if (changes.height != widget->core.height) {
  235.          changeMask |= CWHeight;
  236.          changes.height = widget->core.height;
  237.      }
  238.      if (changes.border_width != widget->core.border_width) {
  239.          changeMask |= CWBorderWidth;
  240.          changes.border_width = widget->core.border_width;
  241.      }
  242.     if (changeMask & CWStackMode) {
  243.         changes.stack_mode = request->stack_mode;
  244.         if (changeMask & CWSibling)
  245.         changes.sibling = XtWindow(request->sibling);
  246.     }
  247.  
  248.     XConfigureWindow(XtDisplay(widget), XtWindow(widget),
  249.              changeMask, &changes);
  250.     }
  251.     else {            /* RectObj child of realized Widget */
  252.     *clear_rect_obj = TRUE;
  253.     ClearRectObjAreas((RectObj)widget, &changes);
  254.     }
  255.  
  256.     return returnCode;
  257. } /* _XtMakeGeometryRequest */
  258.  
  259.  
  260. /* Public routines */
  261.  
  262. XtGeometryResult XtMakeGeometryRequest (widget, request, reply)
  263.     Widget         widget;
  264.     XtWidgetGeometry *request, *reply;
  265. {
  266.     Boolean junk;
  267.     XtGeometryResult returnCode;
  268.  
  269.     returnCode = _XtMakeGeometryRequest(widget, request, reply, &junk);
  270.  
  271.     return ((returnCode == XtGeometryDone) ? XtGeometryYes : returnCode);
  272. }
  273.  
  274. #if NeedFunctionPrototypes
  275. XtGeometryResult XtMakeResizeRequest(
  276.     Widget    widget,
  277.     _XtDimension width,
  278.     _XtDimension height,
  279.     Dimension    *replyWidth,
  280.     Dimension    *replyHeight
  281.     )
  282. #else
  283. XtGeometryResult XtMakeResizeRequest
  284.     (widget, width, height, replyWidth, replyHeight)
  285.     Widget    widget;
  286.     Dimension    width, height;
  287.     Dimension    *replyWidth, *replyHeight;
  288. #endif
  289. {
  290.     XtWidgetGeometry request, reply;
  291.     XtGeometryResult r;
  292.  
  293.     request.request_mode = CWWidth | CWHeight;
  294.     request.width = width;
  295.     request.height = height;
  296.     r = XtMakeGeometryRequest(widget, &request, &reply);
  297.     if (replyWidth != NULL)
  298.     if (r == XtGeometryAlmost && reply.request_mode & CWWidth)
  299.         *replyWidth = reply.width;
  300.     else
  301.         *replyWidth = width;
  302.     if (replyHeight != NULL)
  303.     if (r == XtGeometryAlmost && reply.request_mode & CWHeight)
  304.         *replyHeight = reply.height;
  305.     else
  306.         *replyHeight = height;
  307.     return r;
  308. } /* XtMakeResizeRequest */
  309.  
  310. void XtResizeWindow(w)
  311.     Widget w;
  312. {
  313.     if (XtIsRealized(w)) {
  314.     XWindowChanges changes;
  315.     changes.width = w->core.width;
  316.     changes.height = w->core.height;
  317.     changes.border_width = w->core.border_width;
  318.     XConfigureWindow(XtDisplay(w), XtWindow(w),
  319.         (unsigned) CWWidth | CWHeight | CWBorderWidth, &changes);
  320.     }
  321. } /* XtResizeWindow */
  322.  
  323.  
  324. #if NeedFunctionPrototypes
  325. void XtResizeWidget(
  326.     Widget w,
  327.     _XtDimension width,
  328.     _XtDimension height,
  329.     _XtDimension borderWidth
  330.     )
  331. #else
  332. void XtResizeWidget(w, width, height, borderWidth)
  333.     Widget w;
  334.     Dimension width, height, borderWidth;
  335. #endif
  336. {
  337.     XWindowChanges changes;
  338.     Dimension old_width, old_height, old_borderWidth;
  339.     Cardinal mask = 0;
  340.  
  341.     if ((old_width = w->core.width) != width) {
  342.     changes.width = w->core.width = width;
  343.     mask |= CWWidth;
  344.     }
  345.  
  346.     if ((old_height = w->core.height) != height) {
  347.     changes.height = w->core.height = height;
  348.     mask |= CWHeight;
  349.     }
  350.  
  351.     if ((old_borderWidth = w->core.border_width) != borderWidth) {
  352.     changes.border_width = w->core.border_width = borderWidth;
  353.     mask |= CWBorderWidth;
  354.     }
  355.  
  356.     if (mask != 0) {
  357.     if (XtIsRealized(w)) {
  358.         if (XtIsWidget(w))
  359.         XConfigureWindow(XtDisplay(w), XtWindow(w), mask, &changes);
  360.         else {
  361.         Widget pw = _XtWindowedAncestor(w);
  362.         old_width += (old_borderWidth << 1);
  363.         old_height += (old_borderWidth << 1);
  364.         if ((Dimension)(width + (borderWidth << 1)) > old_width)
  365.             old_width = width + (borderWidth << 1);
  366.         if ((Dimension)(height + (borderWidth << 1)) > old_height)
  367.             old_height = height + (borderWidth << 1);
  368.         XClearArea( XtDisplay(pw), XtWindow(pw),
  369.                 (int)w->core.x, (int)w->core.y,
  370.                 (unsigned int)old_width, (unsigned int)old_height,
  371.                 TRUE );
  372.         }
  373.     }
  374.     if ((mask & (CWWidth | CWHeight)) &&
  375.           XtClass(w)->core_class.resize != (XtWidgetProc) NULL)
  376.         (*(w->core.widget_class->core_class.resize))(w);
  377.     }
  378. } /* XtResizeWidget */
  379.  
  380. #if NeedFunctionPrototypes
  381. void XtConfigureWidget(
  382.     Widget w,
  383.     _XtPosition x,
  384.     _XtPosition y,
  385.     _XtDimension width,
  386.     _XtDimension height,
  387.     _XtDimension borderWidth
  388.     )
  389. #else
  390. void XtConfigureWidget(w, x, y, width, height, borderWidth)
  391.     Widget w;
  392.     Position x, y;
  393.     Dimension width, height, borderWidth;
  394. #endif
  395. {
  396.     XWindowChanges changes, old;
  397.     Cardinal mask = 0;
  398.  
  399.     if ((old.x = w->core.x) != x) {
  400.     changes.x = w->core.x = x;
  401.     mask |= CWX;
  402.     }
  403.  
  404.     if ((old.y = w->core.y) != y) {
  405.     changes.y = w->core.y = y;
  406.     mask |= CWY;
  407.     }
  408.  
  409.     if ((old.width = w->core.width) != width) {
  410.     changes.width = w->core.width = width;
  411.     mask |= CWWidth;
  412.     }
  413.  
  414.     if ((old.height = w->core.height) != height) {
  415.     changes.height = w->core.height = height;
  416.     mask |= CWHeight;
  417.     }
  418.  
  419.     if ((old.border_width = w->core.border_width) != borderWidth) {
  420.     changes.border_width = w->core.border_width = borderWidth;
  421.     mask |= CWBorderWidth;
  422.     }
  423.  
  424.     if (mask != 0) {
  425.     if (XtIsRealized(w)) {
  426.         if (XtIsWidget(w))
  427.         XConfigureWindow(XtDisplay(w), XtWindow(w), mask, &changes);
  428.         else
  429.         ClearRectObjAreas((RectObj)w, &old);
  430.     }
  431.     if ((mask & (CWWidth | CWHeight)) &&
  432.           XtClass(w)->core_class.resize != (XtWidgetProc) NULL)
  433.         (*(w->core.widget_class->core_class.resize))(w);
  434.     }
  435. } /* XtConfigureWidget */
  436.  
  437. #if NeedFunctionPrototypes
  438. void XtMoveWidget(
  439.     Widget w,
  440.     _XtPosition x,
  441.     _XtPosition y
  442.     )
  443. #else
  444. void XtMoveWidget(w, x, y)
  445.     Widget w;
  446.     Position x, y;
  447. #endif
  448. {
  449.     if ((w->core.x != x) || (w->core.y != y)) {
  450.     XWindowChanges old;
  451.     old.x = w->core.x;
  452.     old.y = w->core.y;
  453.     w->core.x = x;
  454.     w->core.y = y;
  455.     if (XtIsRealized(w)) {
  456.         if (XtIsWidget(w))
  457.         XMoveWindow(XtDisplay(w), XtWindow(w), w->core.x, w->core.y);
  458.         else {
  459.         old.width = w->core.width;
  460.         old.height = w->core.height;
  461.         old.border_width = w->core.border_width;
  462.         ClearRectObjAreas((RectObj)w, &old);
  463.         }
  464.         }
  465.     }
  466. } /* XtMoveWidget */
  467.  
  468. #if NeedFunctionPrototypes
  469. void XtTranslateCoords(
  470.     register Widget w,
  471.     _XtPosition x,
  472.     _XtPosition y,
  473.     register Position *rootx,    /* return */
  474.     register Position *rooty    /* return */
  475.     )
  476. #else
  477. void XtTranslateCoords(w, x, y, rootx, rooty)
  478.     register Widget w;
  479.     Position x, y;
  480.     register Position *rootx, *rooty;    /* return */
  481. #endif
  482. {
  483.     Position garbagex, garbagey;
  484.     Widget passed = w;
  485.  
  486.     if (rootx == NULL) rootx = &garbagex;
  487.     if (rooty == NULL) rooty = &garbagey;
  488.  
  489.     *rootx = x;
  490.     *rooty = y;
  491.  
  492.     for (; w != NULL && ! XtIsShell(w); w = w->core.parent) {
  493.     *rootx += w->core.x + w->core.border_width;
  494.     *rooty += w->core.y + w->core.border_width;
  495.     }
  496.  
  497.     if (w == NULL)
  498.         XtAppWarningMsg(XtWidgetToApplicationContext(passed),
  499.         "invalidShell","xtTranslateCoords",XtCXtToolkitError,
  500.                 "Widget has no shell ancestor",
  501.         (String *)NULL, (Cardinal *)NULL);
  502.     else {
  503.     Position x, y;
  504.     extern void _XtShellGetCoordinates();
  505.     _XtShellGetCoordinates( w, &x, &y );
  506.     *rootx += x + w->core.border_width;
  507.     *rooty += y + w->core.border_width;
  508.     }
  509. }
  510.  
  511. XtGeometryResult XtQueryGeometry(widget, intended, reply)
  512.     Widget widget;
  513.     register XtWidgetGeometry *intended; /* parent's changes; may be NULL */
  514.     XtWidgetGeometry *reply;    /* child's preferred geometry; never NULL */
  515. {
  516.     XtWidgetGeometry null_intended;
  517.     XtGeometryHandler query = XtClass(widget)->core_class.query_geometry;
  518.     XtGeometryResult result;
  519.  
  520.     reply->request_mode = 0;
  521.     if (query != NULL) {
  522.     if (intended == NULL) {
  523.         null_intended.request_mode = 0;
  524.         intended = &null_intended;
  525.     }
  526.     result = (*query) (widget, intended, reply);
  527.     }
  528.     else {
  529.     result = XtGeometryYes;
  530.     }
  531.  
  532. #define FillIn(mask, field) \
  533.     if (!(reply->request_mode & mask)) reply->field = widget->core.field;
  534.  
  535.     FillIn(CWX, x);
  536.     FillIn(CWY, y);
  537.     FillIn(CWWidth, width);
  538.     FillIn(CWHeight, height);
  539.     FillIn(CWBorderWidth, border_width);
  540. #undef FillIn
  541.  
  542.     if (!reply->request_mode & CWStackMode) reply->stack_mode =
  543. XtSMDontChange;
  544.  
  545.     return result;
  546.   }
  547.