1/*
2 * tkWinRegion.c --
3 *
4 *	Tk Region emulation code.
5 *
6 * Copyright (c) 1995 Sun Microsystems, Inc.
7 *
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id: tkWinRegion.c,v 1.3 2002/06/14 13:35:49 dkf Exp $
12 */
13
14#include "tkWinInt.h"
15
16
17/*
18 *----------------------------------------------------------------------
19 *
20 * TkCreateRegion --
21 *
22 *	Construct an empty region.
23 *
24 * Results:
25 *	Returns a new region handle.
26 *
27 * Side effects:
28 *	None.
29 *
30 *----------------------------------------------------------------------
31 */
32
33TkRegion
34TkCreateRegion()
35{
36    RECT rect;
37    memset(&rect, 0, sizeof(RECT));
38    return (TkRegion) CreateRectRgnIndirect(&rect);
39}
40
41/*
42 *----------------------------------------------------------------------
43 *
44 * TkDestroyRegion --
45 *
46 *	Destroy the specified region.
47 *
48 * Results:
49 *	None.
50 *
51 * Side effects:
52 *	Frees the storage associated with the specified region.
53 *
54 *----------------------------------------------------------------------
55 */
56
57void
58TkDestroyRegion(r)
59    TkRegion r;
60{
61    DeleteObject((HRGN) r);
62}
63
64/*
65 *----------------------------------------------------------------------
66 *
67 * TkClipBox --
68 *
69 *	Computes the bounding box of a region.
70 *
71 * Results:
72 *	Sets rect_return to the bounding box of the region.
73 *
74 * Side effects:
75 *	None.
76 *
77 *----------------------------------------------------------------------
78 */
79
80void
81TkClipBox(r, rect_return)
82    TkRegion r;
83    XRectangle* rect_return;
84{
85    RECT rect;
86    GetRgnBox((HRGN)r, &rect);
87    rect_return->x = (short) rect.left;
88    rect_return->y = (short) rect.top;
89    rect_return->width = (short) (rect.right - rect.left);
90    rect_return->height = (short) (rect.bottom - rect.top);
91}
92
93/*
94 *----------------------------------------------------------------------
95 *
96 * TkIntersectRegion --
97 *
98 *	Compute the intersection of two regions.
99 *
100 * Results:
101 *	Returns the result in the dr_return region.
102 *
103 * Side effects:
104 *	None.
105 *
106 *----------------------------------------------------------------------
107 */
108
109void
110TkIntersectRegion(sra, srb, dr_return)
111    TkRegion sra;
112    TkRegion srb;
113    TkRegion dr_return;
114{
115    CombineRgn((HRGN) dr_return, (HRGN) sra, (HRGN) srb, RGN_AND);
116}
117
118/*
119 *----------------------------------------------------------------------
120 *
121 * TkUnionRectWithRegion --
122 *
123 *	Create the union of a source region and a rectangle.
124 *
125 * Results:
126 *	Returns the result in the dr_return region.
127 *
128 * Side effects:
129 *	None.
130 *
131 *----------------------------------------------------------------------
132 */
133
134void
135TkUnionRectWithRegion(rectangle, src_region, dest_region_return)
136    XRectangle* rectangle;
137    TkRegion src_region;
138    TkRegion dest_region_return;
139{
140    HRGN rectRgn = CreateRectRgn(rectangle->x, rectangle->y,
141	    rectangle->x + rectangle->width, rectangle->y + rectangle->height);
142    CombineRgn((HRGN) dest_region_return, (HRGN) src_region,
143	    (HRGN) rectRgn, RGN_OR);
144    DeleteObject(rectRgn);
145}
146
147/*
148 *----------------------------------------------------------------------
149 *
150 * TkRectInRegion --
151 *
152 *	Test whether a given rectangle overlaps with a region.
153 *
154 * Results:
155 *	Returns RectanglePart or RectangleOut.  Note that this is
156 *	not a complete implementation since it doesn't test for
157 *	RectangleIn.
158 *
159 * Side effects:
160 *	None.
161 *
162 *----------------------------------------------------------------------
163 */
164
165int
166TkRectInRegion(r, x, y, width, height)
167    TkRegion r;
168    int x;
169    int y;
170    unsigned int width;
171    unsigned int height;
172{
173    RECT rect;
174    rect.top = y;
175    rect.left = x;
176    rect.bottom = y+height;
177    rect.right = x+width;
178    return RectInRegion((HRGN)r, &rect) ? RectanglePart : RectangleOut;
179}
180
181/*
182 *----------------------------------------------------------------------
183 *
184 * TkSubtractRegion --
185 *
186 *	Compute the set-difference of two regions.
187 *
188 * Results:
189 *	Returns the result in the dr_return region.
190 *
191 * Side effects:
192 *	None.
193 *
194 *----------------------------------------------------------------------
195 */
196
197void
198TkSubtractRegion(sra, srb, dr_return)
199    TkRegion sra;
200    TkRegion srb;
201    TkRegion dr_return;
202{
203    CombineRgn((HRGN) dr_return, (HRGN) sra, (HRGN) srb, RGN_DIFF);
204}
205