home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcltk805.zip / tcl805s.zip / tk8.0.5 / os2 / tkOS2Region.c < prev    next >
C/C++ Source or Header  |  2000-01-01  |  7KB  |  280 lines

  1. /* 
  2.  * tkOS2Region.c --
  3.  *
  4.  *    Tk Region emulation code.
  5.  *
  6.  * Copyright (c) 1996-2000 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 VERBOSE
  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 VERBOSE
  71.     printf("TkDestroyRegion %x\n", r);
  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.  
  101.     hps = WinGetPS(HWND_DESKTOP);
  102.     rc = GpiQueryRegionBox(hps, (HRGN)r, &rect);
  103.     WinReleasePS(hps);
  104.     if (rc == RGN_ERROR) {
  105. #ifdef VERBOSE
  106.         printf("TkClipBox: GpiQueryRegionBox %x returns RGN_ERROR\n", r);
  107. #endif
  108.         return;
  109.     }
  110. #ifdef VERBOSE
  111.     printf("TkClipBox: GpiQueryRegionBox %x %s\n", r,
  112.            rc == RGN_NULL ? "RGN_NULL" :
  113.            (rc == RGN_RECT ? "RGN_RECT" :
  114.            (rc == RGN_COMPLEX ? "RGN_COMPLEX" :
  115.            "RGN_ERROR")));
  116. #endif
  117.     rect_return->x = (short) rect.xLeft;
  118.     rect_return->width = (short) rect.xRight - rect.xLeft;
  119.     /* Return the Y coordinate that X expects (top) */
  120.     rect_return->y = (short) yScreen - rect.yTop;
  121.     /* PM coordinates are just reversed, translate */
  122.     rect_return->height = (short) rect.yTop - rect.yBottom;
  123. #ifdef VERBOSE
  124.     printf("          x %d y %d w %d h %d\n", rect_return->x, rect_return->y,
  125.            rect_return->width, rect_return->height);
  126. #endif
  127. }
  128.  
  129. /*
  130.  *----------------------------------------------------------------------
  131.  *
  132.  * TkIntersectRegion --
  133.  *
  134.  *    Compute the intersection of two regions.
  135.  *
  136.  * Results:
  137.  *    Returns the result in the dr_return region.
  138.  *
  139.  * Side effects:
  140.  *    None.
  141.  *
  142.  *----------------------------------------------------------------------
  143.  */
  144.  
  145. void
  146. TkIntersectRegion(sra, srb, dr_return)
  147.     TkRegion sra;
  148.     TkRegion srb;
  149.     TkRegion dr_return;
  150. {
  151.     HPS hps= WinGetPS(HWND_DESKTOP);
  152.     rc = GpiCombineRegion(hps, (HRGN)dr_return, (HRGN)sra, (HRGN)srb, CRGN_AND);
  153. #ifdef VERBOSE
  154.     printf("TkIntersectRegion %x %x %s\n", sra, srb,
  155.            rc == RGN_NULL ? "RGN_NULL" :
  156.            (rc == RGN_RECT ? "RGN_RECT" :
  157.            (rc == RGN_COMPLEX ? "RGN_COMPLEX" : "RGN_ERROR")));
  158. #endif
  159.     WinReleasePS(hps);
  160. }
  161.  
  162. /*
  163.  *----------------------------------------------------------------------
  164.  *
  165.  * TkUnionRectWithRegion --
  166.  *
  167.  *    Create the union of a source region and a rectangle.
  168.  *
  169.  * Results:
  170.  *    Returns the result in the dr_return region.
  171.  *
  172.  * Side effects:
  173.  *    None.
  174.  *
  175.  *----------------------------------------------------------------------
  176.  */
  177.  
  178. void
  179. TkUnionRectWithRegion(rectangle, src_region, dest_region_return)
  180.     XRectangle* rectangle;
  181.     TkRegion src_region;
  182.     TkRegion dest_region_return;
  183. {
  184.     HRGN rectRgn;
  185.     HPS hps;
  186.     RECTL rect;
  187.  
  188.     hps= WinGetScreenPS(HWND_DESKTOP);
  189.     rect.xLeft = rectangle->x;
  190.     rect.xRight = rectangle->x + rectangle->width;
  191.     /* Translate coordinates to PM */
  192.     rect.yTop = yScreen - rectangle->y;
  193.     rect.yBottom = rect.yTop - rectangle->height;
  194. #ifdef VERBOSE
  195.     printf("TkUnionRectWithRegion Xrect (%d,%d) %dx%d, src %x (PM %dx%d)\n",
  196.            rectangle->x, rectangle->y, rectangle->width, rectangle->height,
  197.            src_region, rect.xLeft, rect.yBottom);
  198. #endif
  199.     rectRgn = GpiCreateRegion(hps, 1, &rect);
  200.     rc = GpiCombineRegion(hps, (HRGN) dest_region_return, (HRGN) src_region,
  201.                           rectRgn, CRGN_OR);
  202. #ifdef VERBOSE
  203.     printf("    GpiCombineRegion %s\n", rc == RGN_NULL ? "RGN_NULL" :
  204.                                        (rc == RGN_RECT ? "RGN_RECT" :
  205.                                        (rc == RGN_COMPLEX ? "RGN_COMPLEX" :
  206.                                         "RGN_ERROR")));
  207. #endif
  208.     GpiDestroyRegion(hps, rectRgn);
  209.     WinReleasePS(hps);
  210. }
  211.  
  212. /*
  213.  *----------------------------------------------------------------------
  214.  *
  215.  * TkRectInRegion --
  216.  *
  217.  *    Test whether a given rectangle overlaps with a region.
  218.  *
  219.  * Results:
  220.  *    Returns RectanglePart, RectangleIn or RectangleOut.
  221.  *
  222.  * Side effects:
  223.  *    None.
  224.  *
  225.  *----------------------------------------------------------------------
  226.  */
  227.  
  228. int
  229. TkRectInRegion(r, x, y, width, height)
  230.     TkRegion r;
  231.     int x;
  232.     int y;
  233.     unsigned int width;
  234.     unsigned int height;
  235. {
  236.     RECTL rect;
  237.     LONG in;
  238.     HPS hps;
  239.  
  240.     /* Translate coordinates to PM */
  241.     rect.yTop = yScreen - y;
  242.     rect.xLeft = x;
  243.     rect.yBottom = rect.yTop - height;
  244.     rect.xRight = x+width;
  245.     hps= WinGetPS(HWND_DESKTOP);
  246.     in = GpiRectInRegion(hps, (HRGN)r, &rect);
  247.     WinReleasePS(hps);
  248.     if (in == RRGN_INSIDE) {
  249.         /* all in the region */
  250. #ifdef VERBOSE
  251.         printf("TkRectInRegion r %x (%d,%d) %dx%d RRGN_INSIDE (PM %d,%d)\n", r,
  252.            x, y, width, height, rect.xLeft, rect.yBottom);
  253. #endif
  254.         return RectangleIn;
  255.     } else if (in == RRGN_PARTIAL) {
  256.         /* partly in the region */
  257. #ifdef VERBOSE
  258.         printf("TkRectInRegion r %x (%d,%d) %dx%d RRGN_PARTIAL (PM %d,%d)\n", r,
  259.            x, y, width, height, rect.xLeft, rect.yBottom);
  260. #endif
  261.         return RectanglePart;
  262.     } else {
  263.         /* not in region or error */
  264. #ifdef VERBOSE
  265.         if (in == RRGN_OUTSIDE) {
  266.            printf("TkRectInRegion r %x (%d,%d) %dx%d RRGN_OUTSIDE (PM %d,%d)\n",
  267.               r, x, y, width, height, rect.xLeft, rect.yBottom);
  268.     } else if (in == RRGN_ERROR) {
  269.            printf("TkRectInRegion r %x (%d,%d) %dx%d ERROR %x (PM %d,%d)\n",
  270.               r, x, y, width, height, WinGetLastError(TclOS2GetHAB()),
  271.                   rect.xLeft, rect.yBottom);
  272.         } else {
  273.            printf("TkRectInRegion r %x (%d,%d) %dx%d UNKNOWN (PM %d,%d)\n",
  274.               r, x, y, width, height, rect.xLeft, rect.yBottom);
  275.     }
  276. #endif
  277.         return RectangleOut;
  278.     }
  279. }
  280.