home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tk42r2s.zip / tk4.2 / os2 / tkOS2Region.c < prev    next >
C/C++ Source or Header  |  1999-07-27  |  5KB  |  222 lines

  1. /* 
  2.  * tkOS2Region.c --
  3.  *
  4.  *    Tk Region emulation code.
  5.  *
  6.  * Copyright (c) 1996-1998 Illya Vaes
  7.  * Copyright (c) 1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  */
  12.  
  13.  
  14. #include "tkOS2Int.h"
  15.  
  16.  
  17.  
  18. /*
  19.  *----------------------------------------------------------------------
  20.  *
  21.  * TkCreateRegion --
  22.  *
  23.  *    Construct an empty region.
  24.  *
  25.  * Results:
  26.  *    Returns a new region handle.
  27.  *
  28.  * Side effects:
  29.  *    None.
  30.  *
  31.  *----------------------------------------------------------------------
  32.  */
  33.  
  34. TkRegion
  35. TkCreateRegion()
  36. {
  37.     HRGN region;
  38.     HPS hps;
  39.  
  40.     hps= WinGetPS(HWND_DESKTOP);
  41.     region = GpiCreateRegion(hps, 0, NULL);
  42.     WinReleasePS(hps);
  43.     return (TkRegion) region;
  44. }
  45.  
  46. /*
  47.  *----------------------------------------------------------------------
  48.  *
  49.  * TkDestroyRegion --
  50.  *
  51.  *    Destroy the specified region.
  52.  *
  53.  * Results:
  54.  *    None.
  55.  *
  56.  * Side effects:
  57.  *    Frees the storage associated with the specified region.
  58.  *
  59.  *----------------------------------------------------------------------
  60.  */
  61.  
  62. void
  63. TkDestroyRegion(r)
  64.     TkRegion r;
  65. {
  66.     HPS hps= WinGetPS(HWND_DESKTOP);
  67.     GpiDestroyRegion(hps, (HRGN) r);
  68.     WinReleasePS(hps);
  69. }
  70.  
  71. /*
  72.  *----------------------------------------------------------------------
  73.  *
  74.  * TkClipBox --
  75.  *
  76.  *    Computes the bounding box of a region.
  77.  *
  78.  * Results:
  79.  *    Sets rect_return to the bounding box of the region.
  80.  *
  81.  * Side effects:
  82.  *    None.
  83.  *
  84.  *----------------------------------------------------------------------
  85.  */
  86.  
  87. void
  88. TkClipBox(r, rect_return)
  89.     TkRegion r;
  90.     XRectangle* rect_return;
  91. {
  92.     RECTL rect;
  93.     HPS hps;
  94.  
  95.     hps = WinGetPS(HWND_DESKTOP);
  96.     rc = GpiQueryRegionBox(hps, (HRGN)r, &rect);
  97.     WinReleasePS(hps);
  98.     if (rc == RGN_ERROR) {
  99.         return;
  100.     }
  101.     rect_return->x = rect.xLeft;
  102.     rect_return->width = rect.xRight - rect.xLeft;
  103.     /* Return the Y coordinate that X expects (top) */
  104.     rect_return->y = yScreen - rect.yTop;
  105.     /* PM coordinates are just reversed, translate */
  106.     rect_return->height = rect.yTop - rect.yBottom;
  107. }
  108.  
  109. /*
  110.  *----------------------------------------------------------------------
  111.  *
  112.  * TkIntersectRegion --
  113.  *
  114.  *    Compute the intersection of two regions.
  115.  *
  116.  * Results:
  117.  *    Returns the result in the dr_return region.
  118.  *
  119.  * Side effects:
  120.  *    None.
  121.  *
  122.  *----------------------------------------------------------------------
  123.  */
  124.  
  125. void
  126. TkIntersectRegion(sra, srb, dr_return)
  127.     TkRegion sra;
  128.     TkRegion srb;
  129.     TkRegion dr_return;
  130. {
  131.     HPS hps= WinGetPS(HWND_DESKTOP);
  132.     rc = GpiCombineRegion(hps, (HRGN) dr_return, (HRGN) sra, (HRGN) srb, CRGN_AND);
  133.     WinReleasePS(hps);
  134. }
  135.  
  136. /*
  137.  *----------------------------------------------------------------------
  138.  *
  139.  * TkUnionRectWithRegion --
  140.  *
  141.  *    Create the union of a source region and a rectangle.
  142.  *
  143.  * Results:
  144.  *    Returns the result in the dr_return region.
  145.  *
  146.  * Side effects:
  147.  *    None.
  148.  *
  149.  *----------------------------------------------------------------------
  150.  */
  151.  
  152. void
  153. TkUnionRectWithRegion(rectangle, src_region, dest_region_return)
  154.     XRectangle* rectangle;
  155.     TkRegion src_region;
  156.     TkRegion dest_region_return;
  157. {
  158.     HRGN rectRgn;
  159.     HPS hps;
  160.     RECTL rect;
  161.  
  162.     hps= WinGetPS(HWND_DESKTOP);
  163.     rect.xLeft = rectangle->x;
  164.     rect.xRight = rectangle->x + rectangle->width;
  165.     /* Translate coordinates to PM */
  166.     rect.yTop = yScreen - rectangle->y;
  167.     rect.yBottom = rect.yTop - rectangle->height;
  168.     rectRgn = GpiCreateRegion(hps, 1, &rect);
  169.     rc = GpiCombineRegion(hps, (HRGN) dest_region_return, (HRGN) src_region,
  170.                           rectRgn, CRGN_OR);
  171.     GpiDestroyRegion(hps, rectRgn);
  172.     WinReleasePS(hps);
  173. }
  174.  
  175. /*
  176.  *----------------------------------------------------------------------
  177.  *
  178.  * TkRectInRegion --
  179.  *
  180.  *    Test whether a given rectangle overlaps with a region.
  181.  *
  182.  * Results:
  183.  *    Returns RectanglePart, RectangleIn or RectangleOut.
  184.  *
  185.  * Side effects:
  186.  *    None.
  187.  *
  188.  *----------------------------------------------------------------------
  189.  */
  190.  
  191. int
  192. TkRectInRegion(r, x, y, width, height)
  193.     TkRegion r;
  194.     int x;
  195.     int y;
  196.     unsigned int width;
  197.     unsigned int height;
  198. {
  199.     RECTL rect;
  200.     LONG in;
  201.     HPS hps;
  202.  
  203.     /* Translate coordinates to PM */
  204.     rect.yTop = yScreen - y;
  205.     rect.xLeft = x;
  206.     rect.yBottom = rect.yTop - height;
  207.     rect.xRight = x+width;
  208.     hps= WinGetPS(HWND_DESKTOP);
  209.     in = GpiRectInRegion(hps, (HRGN)r, &rect);
  210.     WinReleasePS(hps);
  211.     if (in == RRGN_INSIDE) {
  212.         /* all in the region */
  213.         return RectangleIn;
  214.     } else if (in == RRGN_PARTIAL) {
  215.         /* partly in the region */
  216.         return RectanglePart;
  217.     } else {
  218.         /* not in region or error */
  219.         return RectangleOut;
  220.     }
  221. }
  222.