home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 08 / dflat5 / rect.c < prev    next >
Text File  |  1991-06-25  |  3KB  |  133 lines

  1. /* ------------- rect.c --------------- */
  2.  
  3. #include <dos.h>
  4. #include "dflat.h"
  5.  
  6. /*
  7.  * Produce the vector end points produced by the overlap
  8.  * of two other vectors
  9.  */
  10. static void subVector(int *v1, int *v2,
  11.                         int t1, int t2, int o1, int o2)
  12. {
  13.     *v1 = *v2 = -1;
  14.     if (within(o1, t1, t2))    {
  15.         *v1 = o1;
  16.         if (within(o2, t1, t2))
  17.             *v2 = o2;
  18.         else
  19.             *v2 = t2;
  20.     }
  21.     else if (within(o2, t1, t2))    {
  22.         *v2 = o2;
  23.         if (within(o1, t1, t2))
  24.             *v1 = o1;
  25.         else
  26.             *v1 = t1;
  27.     }
  28.     else if (within(t1, o1, o2))    {
  29.         *v1 = t1;
  30.         if (within(t2, o1, o2))
  31.             *v2 = t2;
  32.         else
  33.             *v2 = o2;
  34.     }
  35.     else if (within(t2, o1, o2))    {
  36.         *v2 = t2;
  37.         if (within(t1, o1, o2))
  38.             *v1 = t1;
  39.         else
  40.             *v1 = o1;
  41.     }
  42. }
  43.  
  44. /*
  45.  * Return the rectangle produced by the overlap
  46.  * of two other rectangles
  47.  */
  48. RECT subRectangle(RECT r1, RECT r2)
  49. {
  50.     RECT r = {0,0,0,0};
  51.     subVector((int *) &RectLeft(r), (int *) &RectRight(r),
  52.         RectLeft(r1), RectRight(r1),
  53.         RectLeft(r2), RectRight(r2));
  54.     subVector((int *) &RectTop(r), (int *) &RectBottom(r),
  55.         RectTop(r1), RectBottom(r1),
  56.         RectTop(r2), RectBottom(r2));
  57.     if (RectRight(r) == -1 || RectTop(r) == -1)
  58.         RectRight(r) =
  59.         RectLeft(r) =
  60.         RectTop(r) =
  61.         RectBottom(r) = 0;
  62.     return r;
  63. }
  64.  
  65. /*
  66.  * Assumes that r1 is a proper subset of r2
  67.  * Returns rectangle that is relative position of r1 within r2
  68.  */
  69. RECT RelativeRectangle(RECT r1, RECT r2)
  70. {
  71.     RECT rc;
  72.     RectLeft(rc) = RectLeft(r1) - RectLeft(r2);
  73.     RectTop(rc) = RectTop(r1) - RectTop(r2);
  74.     RectBottom(rc) = RectTop(rc) + RectHeight(r1) - 1;
  75.     RectRight(rc) = RectLeft(rc) + RectWidth(r1) - 1;
  76.     return rc;
  77. }
  78.  
  79. /* ---------- return the client rectangle of a window ------ */
  80. RECT ClientRect(void *wnd)
  81. {
  82.     RECT rc;
  83.  
  84.     RectLeft(rc) = GetClientLeft((WINDOW)wnd);
  85.     RectTop(rc) = GetClientTop((WINDOW)wnd);
  86.     RectRight(rc) = GetClientRight((WINDOW)wnd);
  87.     RectBottom(rc) = GetClientBottom((WINDOW)wnd);
  88.     return rc;
  89. }
  90.  
  91. RECT RelativeClientRect(void *wnd)
  92. {
  93.     RECT rc;
  94.     rc = ClientRect((WINDOW)wnd);
  95.     return RelativeWindowRect(wnd, rc);
  96. }
  97.  
  98. RECT RelativeWindowRect(void *wnd, RECT rc)
  99. {
  100.     RectLeft(rc) -= GetLeft((WINDOW)wnd);
  101.     RectRight(rc) -= GetLeft((WINDOW)wnd);
  102.     RectTop(rc) -= GetTop((WINDOW)wnd);
  103.     RectBottom(rc) -= GetTop((WINDOW)wnd);
  104.     return rc;
  105. }
  106.  
  107. /*
  108.  * Initialize a rectangle
  109.  */
  110. RECT SetRect(int lf, int tp, int rt, int bt)
  111. {
  112.     RECT rc;
  113.     rc.lf = lf;
  114.     rc.tp = tp;
  115.     rc.rt = rt;
  116.     rc.bt = bt;
  117.     return rc;
  118. }
  119.  
  120. RECT ClipRectangle(void *wnd, RECT rc)
  121. {
  122.     RECT sr;
  123.     RectLeft(sr) = RectTop(sr) = 0;
  124.     RectRight(sr) = SCREENWIDTH-1;
  125.     RectBottom(sr) = SCREENHEIGHT-1;
  126.     if (!TestAttribute((WINDOW)wnd, NOCLIP))
  127.         while ((wnd = GetParent((WINDOW)wnd)) != NULLWND)
  128.             rc = subRectangle(rc, ClientRect(wnd));
  129.     return subRectangle(rc, sr);
  130. }
  131.  
  132.  
  133.