home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tkisrc04.zip / tk / os2 / tkOS2Region.c < prev    next >
C/C++ Source or Header  |  1998-08-07  |  6KB  |  250 lines

  1. /* 
  2.  * tkOS2Region.c --
  3.  *
  4.  *    Tk Region emulation code.
  5.  *
  6.  * Copyright (c) 1996-1997 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. #ifdef DEBUG
  44.     printf("TkCreateRegion region %x, hps %x\n", region, hps);
  45. #endif
  46.     return (TkRegion) region;
  47. }
  48.  
  49. /*
  50.  *----------------------------------------------------------------------
  51.  *
  52.  * TkDestroyRegion --
  53.  *
  54.  *    Destroy the specified region.
  55.  *
  56.  * Results:
  57.  *    None.
  58.  *
  59.  * Side effects:
  60.  *    Frees the storage associated with the specified region.
  61.  *
  62.  *----------------------------------------------------------------------
  63.  */
  64.  
  65. void
  66. TkDestroyRegion(r)
  67.     TkRegion r;
  68. {
  69.     HPS hps= WinGetPS(HWND_DESKTOP);
  70. #ifdef DEBUG
  71.     printf("TkDestroyRegion\n");
  72. #endif
  73.     GpiDestroyRegion(hps, (HRGN) r);
  74.     WinReleasePS(hps);
  75. }
  76.  
  77. /*
  78.  *----------------------------------------------------------------------
  79.  *
  80.  * TkClipBox --
  81.  *
  82.  *    Computes the bounding box of a region.
  83.  *
  84.  * Results:
  85.  *    Sets rect_return to the bounding box of the region.
  86.  *
  87.  * Side effects:
  88.  *    None.
  89.  *
  90.  *----------------------------------------------------------------------
  91.  */
  92.  
  93. void
  94. TkClipBox(r, rect_return)
  95.     TkRegion r;
  96.     XRectangle* rect_return;
  97. {
  98.     RECTL rect;
  99.     HPS hps;
  100.     LONG ret;
  101.  
  102.     hps = WinGetPS(HWND_DESKTOP);
  103.     ret = GpiQueryRegionBox(hps, (HRGN)r, &rect);
  104.     WinReleasePS(hps);
  105.     if (ret == RGN_ERROR) {
  106. #ifdef DEBUG
  107.     printf("TkClipBox: GpiQueryRegionBox returns RGN_ERROR\n");
  108. #endif
  109.         return;
  110.     }
  111. #ifdef DEBUG
  112.     printf("TkClipBox: GpiQueryRegionBox %x returns %x\n", r, ret);
  113. #endif
  114.     rect_return->x = rect.xLeft;
  115.     rect_return->width = rect.xRight - rect.xLeft;
  116.     /* Return the Y coordinate that X expects (top) */
  117.     rect_return->y = yScreen - rect.yTop;
  118.     /* PM coordinates are just reversed, translate */
  119.     rect_return->height = rect.yTop - rect.yBottom;
  120. #ifdef DEBUG
  121.     printf("          x %d y %d w %d h %d\n", rect_return->x, rect_return->y,
  122.            rect_return->width, rect_return->height);
  123. #endif
  124. }
  125.  
  126. /*
  127.  *----------------------------------------------------------------------
  128.  *
  129.  * TkIntersectRegion --
  130.  *
  131.  *    Compute the intersection of two regions.
  132.  *
  133.  * Results:
  134.  *    Returns the result in the dr_return region.
  135.  *
  136.  * Side effects:
  137.  *    None.
  138.  *
  139.  *----------------------------------------------------------------------
  140.  */
  141.  
  142. void
  143. TkIntersectRegion(sra, srb, dr_return)
  144.     TkRegion sra;
  145.     TkRegion srb;
  146.     TkRegion dr_return;
  147. {
  148.     HPS hps= WinGetPS(HWND_DESKTOP);
  149. #ifdef DEBUG
  150.     printf("TkIntersectRegion\n");
  151. #endif
  152.     GpiCombineRegion(hps, (HRGN) dr_return, (HRGN) sra, (HRGN) srb, CRGN_AND);
  153.     WinReleasePS(hps);
  154. }
  155.  
  156. /*
  157.  *----------------------------------------------------------------------
  158.  *
  159.  * TkUnionRectWithRegion --
  160.  *
  161.  *    Create the union of a source region and a rectangle.
  162.  *
  163.  * Results:
  164.  *    Returns the result in the dr_return region.
  165.  *
  166.  * Side effects:
  167.  *    None.
  168.  *
  169.  *----------------------------------------------------------------------
  170.  */
  171.  
  172. void
  173. TkUnionRectWithRegion(rectangle, src_region, dest_region_return)
  174.     XRectangle* rectangle;
  175.     TkRegion src_region;
  176.     TkRegion dest_region_return;
  177. {
  178.     HRGN rectRgn;
  179.     HPS hps;
  180.     RECTL rect;
  181.  
  182. #ifdef DEBUG
  183.     printf("TkUnionRectWithRegion Xrect: x %x, y %x, w %x, h %x\n",
  184.            rectangle->x, rectangle->y, rectangle->width, rectangle->height);
  185. #endif
  186.     hps= WinGetPS(HWND_DESKTOP);
  187.     rect.xLeft = rectangle->x;
  188.     rect.xRight = rectangle->x + rectangle->width;
  189.     /* Translate coordinates to PM */
  190.     rect.yTop = yScreen - rectangle->y;
  191.     rect.yBottom = rect.yTop - rectangle->height;
  192.     rectRgn = GpiCreateRegion(hps, 1, &rect);
  193.     GpiCombineRegion(hps, (HRGN) dest_region_return, (HRGN) src_region, rectRgn,
  194.                      CRGN_OR);
  195.     GpiDestroyRegion(hps, rectRgn);
  196.     WinReleasePS(hps);
  197. }
  198.  
  199. /*
  200.  *----------------------------------------------------------------------
  201.  *
  202.  * TkRectInRegion --
  203.  *
  204.  *    Test whether a given rectangle overlaps with a region.
  205.  *
  206.  * Results:
  207.  *    Returns RectanglePart, RectangleIn or RectangleOut.
  208.  *
  209.  * Side effects:
  210.  *    None.
  211.  *
  212.  *----------------------------------------------------------------------
  213.  */
  214.  
  215. int
  216. TkRectInRegion(r, x, y, width, height)
  217.     TkRegion r;
  218.     int x;
  219.     int y;
  220.     unsigned int width;
  221.     unsigned int height;
  222. {
  223.     RECTL rect;
  224.     LONG in;
  225.     HPS hps;
  226.  
  227. #ifdef DEBUG
  228.     printf("TkRectInRegion\n");
  229. #endif
  230.  
  231.     /* Translate coordinates to PM */
  232.     rect.yTop = yScreen - y;
  233.     rect.xLeft = x;
  234.     rect.yBottom = rect.yTop - height;
  235.     rect.xRight = x+width;
  236.     hps= WinGetPS(HWND_DESKTOP);
  237.     in = GpiRectInRegion(hps, (HRGN)r, &rect);
  238.     WinReleasePS(hps);
  239.     if (in == RRGN_INSIDE) {
  240.         /* all in the region */
  241.         return RectangleIn;
  242.     } else if (in == RRGN_PARTIAL) {
  243.         /* partly in the region */
  244.         return RectanglePart;
  245.     } else {
  246.         /* not in region or error */
  247.         return RectangleOut;
  248.     }
  249. }
  250.