Release 950901
[wine] / objects / clipping.c
1 /*
2  * DC clipping functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  */
6
7 #include <stdio.h>
8 #include "region.h"
9 #include "metafile.h"
10 #include "stddebug.h"
11 /* #define DEBUG_CLIPPING */
12 #include "debug.h"
13
14 /***********************************************************************
15  *           CLIPPING_SetDeviceClipping
16  *
17  * Set the clip region of the physical device.
18  */
19 static void CLIPPING_SetDeviceClipping( DC * dc )
20 {
21     RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr(dc->w.hGCClipRgn, REGION_MAGIC);
22     if (!obj)
23     {
24         fprintf( stderr, "SetDeviceClipping: Rgn is 0. Please report this.\n");
25         exit(1);
26     }
27     if (obj->xrgn)
28     {
29         XSetRegion( display, dc->u.x.gc, obj->xrgn );
30         XSetClipOrigin( display, dc->u.x.gc, dc->w.DCOrgX, dc->w.DCOrgY );
31     }
32     else  /* Clip everything */
33     {
34         XSetClipRectangles( display, dc->u.x.gc, 0, 0, NULL, 0, 0 );
35     }
36 }
37
38
39 /***********************************************************************
40  *           CLIPPING_UpdateGCRegion
41  *
42  * Update the GC clip region when the ClipRgn or VisRgn have changed.
43  */
44 void CLIPPING_UpdateGCRegion( DC * dc )
45 {
46     if (!dc->w.hGCClipRgn) dc->w.hGCClipRgn = CreateRectRgn( 0, 0, 0, 0 );
47
48     if (!dc->w.hVisRgn)
49     {
50         fprintf( stderr, "UpdateGCRegion: hVisRgn is zero. Please report this.\n" );
51         exit(1);
52     }
53     if (!dc->w.hClipRgn)
54         CombineRgn( dc->w.hGCClipRgn, dc->w.hVisRgn, 0, RGN_COPY );
55     else
56         CombineRgn( dc->w.hGCClipRgn, dc->w.hClipRgn, dc->w.hVisRgn, RGN_AND );
57     CLIPPING_SetDeviceClipping( dc );
58 }
59
60
61 /***********************************************************************
62  *           SelectClipRgn    (GDI.44)
63  */
64 int SelectClipRgn( HDC hdc, HRGN hrgn )
65 {
66     int retval;
67     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
68     if (!dc) return ERROR;
69
70     dprintf_clipping(stddeb, "SelectClipRgn: %x %x\n", hdc, hrgn );
71
72     if (hrgn)
73     {
74         if (!dc->w.hClipRgn) dc->w.hClipRgn = CreateRectRgn(0,0,0,0);
75         retval = CombineRgn( dc->w.hClipRgn, hrgn, 0, RGN_COPY );
76     }
77     else
78     {
79         if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
80         dc->w.hClipRgn = 0;
81         retval = SIMPLEREGION; /* Clip region == whole DC */
82     }
83     CLIPPING_UpdateGCRegion( dc );
84     return retval;
85 }
86
87
88 /***********************************************************************
89  *           SelectVisRgn    (GDI.105)
90  */
91 int SelectVisRgn( HDC hdc, HRGN hrgn )
92 {
93     int retval;
94     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
95     if (!dc || !hrgn) return ERROR;
96
97     dprintf_clipping(stddeb, "SelectVisRgn: %x %x\n", hdc, hrgn );
98
99     retval = CombineRgn( dc->w.hVisRgn, hrgn, 0, RGN_COPY );
100     CLIPPING_UpdateGCRegion( dc );
101     return retval;
102 }
103
104
105 /***********************************************************************
106  *           OffsetClipRgn    (GDI.32)
107  */
108 int OffsetClipRgn( HDC hdc, short x, short y )
109 {
110     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
111     if (!dc) 
112     {
113         dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
114         if (!dc) return ERROR;
115         MF_MetaParam2(dc, META_OFFSETCLIPRGN, x, y);
116         return NULLREGION;   /* ?? */
117     }
118
119     dprintf_clipping(stddeb, "OffsetClipRgn: %x %d,%d\n", hdc, x, y );
120
121     if (dc->w.hClipRgn)
122     {
123         int retval = OffsetRgn( dc->w.hClipRgn, XLPTODP(dc,x), YLPTODP(dc,y) );
124         CLIPPING_UpdateGCRegion( dc );
125         return retval;
126     }
127     else return SIMPLEREGION; /* Clip region == client area */
128 }
129
130
131 /***********************************************************************
132  *           OffsetVisRgn    (GDI.102)
133  */
134 int OffsetVisRgn( HDC hdc, short x, short y )
135 {
136     int retval;
137     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
138     if (!dc) return ERROR;    
139     dprintf_clipping(stddeb, "OffsetVisRgn: %x %d,%d\n", hdc, x, y );
140     retval = OffsetRgn( dc->w.hVisRgn, x, y );
141     CLIPPING_UpdateGCRegion( dc );
142     return retval;
143 }
144
145
146 /***********************************************************************
147  *           CLIPPING_IntersectClipRect
148  *
149  * Helper function for {Intersect,Exclude}ClipRect
150  */
151 static int CLIPPING_IntersectClipRect( DC * dc, short left, short top,
152                                        short right, short bottom, BOOL exclude)
153 {
154     HRGN tempRgn, newRgn;
155     int ret;
156
157     left   = XLPTODP( dc, left );
158     right  = XLPTODP( dc, right );
159     top    = YLPTODP( dc, top );
160     bottom = YLPTODP( dc, bottom );
161
162     if (!(newRgn = CreateRectRgn( 0, 0, 0, 0 ))) return ERROR;
163     if (!(tempRgn = CreateRectRgn( left, top, right, bottom )))
164     {
165         DeleteObject( newRgn );
166         return ERROR;
167     }
168     ret = CombineRgn( newRgn, dc->w.hClipRgn ? dc->w.hClipRgn : dc->w.hVisRgn,
169                       tempRgn, exclude ? RGN_DIFF : RGN_AND);
170     DeleteObject( tempRgn );
171
172     if (ret != ERROR)
173     {
174         if (dc->w.hClipRgn) DeleteObject( dc->w.hClipRgn );
175         dc->w.hClipRgn = newRgn;    
176         CLIPPING_UpdateGCRegion( dc );
177     }
178     else DeleteObject( newRgn );
179     return ret;
180 }
181
182
183 /***********************************************************************
184  *           ExcludeClipRect    (GDI.21)
185  */
186 int ExcludeClipRect( HDC hdc, short left, short top,
187                      short right, short bottom )
188 {
189     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
190     if (!dc) 
191     {
192         dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
193         if (!dc) return ERROR;
194         MF_MetaParam4(dc, META_EXCLUDECLIPRECT, left, top, right, bottom);
195         return NULLREGION;   /* ?? */
196     }
197
198     dprintf_clipping(stddeb, "ExcludeClipRect: %x %dx%d,%dx%d\n",
199             hdc, left, top, right, bottom );
200     return CLIPPING_IntersectClipRect( dc, left, top, right, bottom, TRUE );
201 }
202
203
204 /***********************************************************************
205  *           IntersectClipRect    (GDI.22)
206  */
207 int IntersectClipRect( HDC hdc, short left, short top,
208                        short right, short bottom )
209 {
210     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
211     if (!dc) 
212     {
213         dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
214         if (!dc) return ERROR;
215         MF_MetaParam4(dc, META_INTERSECTCLIPRECT, left, top, right, bottom);
216         return NULLREGION;   /* ?? */
217     }
218
219     dprintf_clipping(stddeb, "IntersectClipRect: %x %dx%d,%dx%d\n",
220             hdc, left, top, right, bottom );
221     return CLIPPING_IntersectClipRect( dc, left, top, right, bottom, FALSE );
222 }
223
224
225 /***********************************************************************
226  *           CLIPPING_IntersectVisRect
227  *
228  * Helper function for {Intersect,Exclude}VisRect
229  */
230 static int CLIPPING_IntersectVisRect( DC * dc, short left, short top,
231                                       short right, short bottom, BOOL exclude )
232 {
233     HRGN tempRgn, newRgn;
234     int ret;
235
236     left   = XLPTODP( dc, left );
237     right  = XLPTODP( dc, right );
238     top    = YLPTODP( dc, top );
239     bottom = YLPTODP( dc, bottom );
240
241     if (!(newRgn = CreateRectRgn( 0, 0, 0, 0 ))) return ERROR;
242     if (!(tempRgn = CreateRectRgn( left, top, right, bottom )))
243     {
244         DeleteObject( newRgn );
245         return ERROR;
246     }
247     ret = CombineRgn( newRgn, dc->w.hVisRgn, tempRgn,
248                       exclude ? RGN_DIFF : RGN_AND);
249     DeleteObject( tempRgn );
250
251     if (ret != ERROR)
252     {
253         RGNOBJ *newObj  = (RGNOBJ*)GDI_GetObjPtr( newRgn, REGION_MAGIC);
254         RGNOBJ *prevObj = (RGNOBJ*)GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC);
255         if (newObj && prevObj) newObj->header.hNext = prevObj->header.hNext;
256         DeleteObject( dc->w.hVisRgn );
257         dc->w.hVisRgn = newRgn;    
258         CLIPPING_UpdateGCRegion( dc );
259     }
260     else DeleteObject( newRgn );
261     return ret;
262 }
263
264
265 /***********************************************************************
266  *           ExcludeVisRect    (GDI.73)
267  */
268 int ExcludeVisRect( HDC hdc, short left, short top, short right, short bottom )
269 {
270     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
271     if (!dc) return ERROR;    
272     dprintf_clipping(stddeb, "ExcludeVisRect: %x %dx%d,%dx%d\n",
273             hdc, left, top, right, bottom );
274     return CLIPPING_IntersectVisRect( dc, left, top, right, bottom, TRUE );
275 }
276
277
278 /***********************************************************************
279  *           IntersectVisRect    (GDI.98)
280  */
281 int IntersectVisRect( HDC hdc, short left, short top,
282                       short right, short bottom )
283 {
284     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
285     if (!dc) return ERROR;    
286     dprintf_clipping(stddeb, "IntersectVisRect: %x %dx%d,%dx%d\n",
287             hdc, left, top, right, bottom );
288     return CLIPPING_IntersectVisRect( dc, left, top, right, bottom, FALSE );
289 }
290
291
292 /***********************************************************************
293  *           PtVisible    (GDI.103)
294  */
295 BOOL PtVisible( HDC hdc, short x, short y )
296 {
297     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
298     if (!dc) return ERROR;    
299
300     dprintf_clipping(stddeb, "PtVisible: %x %d,%d\n", hdc, x, y );
301     if (!dc->w.hGCClipRgn) return FALSE;
302     return PtInRegion( dc->w.hGCClipRgn, XLPTODP(dc,x), YLPTODP(dc,y) );
303 }
304
305
306 /***********************************************************************
307  *           RectVisible    (GDI.104)
308  */
309 BOOL RectVisible( HDC hdc, LPRECT rect )
310 {
311     RECT tmpRect;
312     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
313     if (!dc) return FALSE;
314     dprintf_clipping(stddeb,"RectVisible: %x %d,%dx%d,%d\n",
315                      hdc, rect->left, rect->top, rect->right, rect->bottom );
316     if (!dc->w.hGCClipRgn) return FALSE;
317     /* copy rectangle to avoid overwriting by LPtoDP */
318     tmpRect = *rect;
319     LPtoDP( hdc, (LPPOINT)&tmpRect, 2 );
320     return RectInRegion( dc->w.hGCClipRgn, &tmpRect );
321 }
322
323
324 /***********************************************************************
325  *           GetClipBox    (GDI.77)
326  */
327 int GetClipBox( HDC hdc, LPRECT rect )
328 {
329     int ret;
330     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
331     if (!dc) return ERROR;    
332     dprintf_clipping(stddeb, "GetClipBox: %x %p\n", hdc, rect );
333     ret = GetRgnBox( dc->w.hGCClipRgn, rect );
334     DPtoLP( hdc, (LPPOINT)rect, 2 );
335     return ret;
336 }
337
338
339 /***********************************************************************
340  *           SaveVisRgn    (GDI.129)
341  */
342 HRGN SaveVisRgn( HDC hdc )
343 {
344     HRGN copy;
345     RGNOBJ *obj, *copyObj;
346     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
347     if (!dc) return 0;
348     dprintf_clipping(stddeb, "SaveVisRgn: %x\n", hdc );
349     if (!dc->w.hVisRgn)
350     {
351         fprintf( stderr, "SaveVisRgn: hVisRgn is zero. Please report this.\n" );
352         exit(1);
353     }
354     if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
355         return 0;
356     if (!(copy = CreateRectRgn( 0, 0, 0, 0 ))) return 0;
357     CombineRgn( copy, dc->w.hVisRgn, 0, RGN_COPY );
358     if (!(copyObj = (RGNOBJ *) GDI_GetObjPtr( copy, REGION_MAGIC )))
359         return 0;
360     copyObj->header.hNext = obj->header.hNext;
361     obj->header.hNext = copy;
362     return copy;
363 }
364
365
366 /***********************************************************************
367  *           RestoreVisRgn    (GDI.130)
368  */
369 int RestoreVisRgn( HDC hdc )
370 {
371     HRGN saved;
372     RGNOBJ *obj, *savedObj;
373     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
374     if (!dc || !dc->w.hVisRgn) return ERROR;    
375     dprintf_clipping(stddeb, "RestoreVisRgn: %x\n", hdc );
376     if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
377         return ERROR;
378     if (!(saved = obj->header.hNext)) return ERROR;
379     if (!(savedObj = (RGNOBJ *) GDI_GetObjPtr( saved, REGION_MAGIC )))
380         return ERROR;
381     DeleteObject( dc->w.hVisRgn );
382     dc->w.hVisRgn = saved;
383     CLIPPING_UpdateGCRegion( dc );
384     return savedObj->xrgn ? COMPLEXREGION : NULLREGION;
385 }