Add trailing '\n's to ok() calls.
[wine.git] / objects / clipping.c
blobde1584572c908a30553b37b3bd473c0057bf08a5
1 /*
2 * DC clipping functions
4 * Copyright 1993 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "wownt32.h"
27 #include "wine/winuser16.h"
28 #include "gdi.h"
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
35 /***********************************************************************
36 * CLIPPING_UpdateGCRegion
38 * Update the GC clip region when the ClipRgn or VisRgn have changed.
40 void CLIPPING_UpdateGCRegion( DC * dc )
42 if (!dc->hGCClipRgn) dc->hGCClipRgn = CreateRectRgn( 0, 0, 0, 0 );
44 if (!dc->hVisRgn)
46 ERR("hVisRgn is zero. Please report this.\n" );
47 exit(1);
50 if (dc->flags & DC_DIRTY) ERR( "DC is dirty. Please report this.\n" );
52 if (!dc->hClipRgn)
53 CombineRgn( dc->hGCClipRgn, dc->hVisRgn, 0, RGN_COPY );
54 else
55 CombineRgn(dc->hGCClipRgn, dc->hClipRgn, dc->hVisRgn, RGN_AND);
56 if (dc->funcs->pSetDeviceClipping)
57 dc->funcs->pSetDeviceClipping( dc->physDev, dc->hGCClipRgn );
61 /***********************************************************************
62 * SelectClipRgn (GDI32.@)
64 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
66 return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
70 /******************************************************************************
71 * ExtSelectClipRgn [GDI32.@]
73 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
75 INT retval;
76 RECT rect;
77 DC * dc = DC_GetDCUpdate( hdc );
78 if (!dc) return ERROR;
80 TRACE("%p %p %d\n", hdc, hrgn, fnMode );
82 if (dc->funcs->pExtSelectClipRgn)
84 retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
85 GDI_ReleaseObj( hdc );
86 return retval;
89 if (!hrgn)
91 if (fnMode == RGN_COPY)
93 if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
94 dc->hClipRgn = 0;
96 else
98 FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
99 GDI_ReleaseObj( hdc );
100 return ERROR;
103 else
105 if (!dc->hClipRgn)
107 RECT rect;
108 GetRgnBox( dc->hVisRgn, &rect );
109 dc->hClipRgn = CreateRectRgnIndirect( &rect );
112 if(fnMode == RGN_COPY)
113 CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
114 else
115 CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
118 CLIPPING_UpdateGCRegion( dc );
119 GDI_ReleaseObj( hdc );
121 return GetClipBox(hdc, &rect);
124 /***********************************************************************
125 * SelectVisRgn (GDI.105)
127 INT16 WINAPI SelectVisRgn16( HDC16 hdc16, HRGN16 hrgn )
129 int retval;
130 HDC hdc = HDC_32( hdc16 );
131 DC * dc;
133 if (!hrgn) return ERROR;
134 if (!(dc = DC_GetDCPtr( hdc ))) return ERROR;
136 TRACE("%p %04x\n", hdc, hrgn );
138 dc->flags &= ~DC_DIRTY;
140 retval = CombineRgn( dc->hVisRgn, HRGN_32(hrgn), 0, RGN_COPY );
141 CLIPPING_UpdateGCRegion( dc );
142 GDI_ReleaseObj( hdc );
143 return retval;
147 /***********************************************************************
148 * OffsetClipRgn (GDI32.@)
150 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
152 INT ret = SIMPLEREGION;
153 DC *dc = DC_GetDCUpdate( hdc );
154 if (!dc) return ERROR;
156 TRACE("%p %d,%d\n", hdc, x, y );
158 if(dc->funcs->pOffsetClipRgn)
159 ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
160 else if (dc->hClipRgn) {
161 ret = OffsetRgn( dc->hClipRgn, MulDiv( x, dc->vportExtX, dc->wndExtX ),
162 MulDiv( y, dc->vportExtY, dc->wndExtY ) );
163 CLIPPING_UpdateGCRegion( dc );
165 GDI_ReleaseObj( hdc );
166 return ret;
170 /***********************************************************************
171 * OffsetVisRgn (GDI.102)
173 INT16 WINAPI OffsetVisRgn16( HDC16 hdc16, INT16 x, INT16 y )
175 INT16 retval;
176 HDC hdc = HDC_32( hdc16 );
177 DC * dc = DC_GetDCUpdate( hdc );
178 if (!dc) return ERROR;
179 TRACE("%p %d,%d\n", hdc, x, y );
180 retval = OffsetRgn( dc->hVisRgn, x, y );
181 CLIPPING_UpdateGCRegion( dc );
182 GDI_ReleaseObj( hdc );
183 return retval;
187 /***********************************************************************
188 * ExcludeClipRect (GDI32.@)
190 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
191 INT right, INT bottom )
193 HRGN newRgn;
194 INT ret;
195 DC *dc = DC_GetDCUpdate( hdc );
196 if (!dc) return ERROR;
198 TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
200 if(dc->funcs->pExcludeClipRect)
201 ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
202 else
204 POINT pt[2];
206 pt[0].x = left;
207 pt[0].y = top;
208 pt[1].x = right;
209 pt[1].y = bottom;
210 LPtoDP( hdc, pt, 2 );
211 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
212 else
214 if (!dc->hClipRgn)
216 dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
217 CombineRgn( dc->hClipRgn, dc->hVisRgn, 0, RGN_COPY );
219 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
220 DeleteObject( newRgn );
222 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
224 GDI_ReleaseObj( hdc );
225 return ret;
229 /***********************************************************************
230 * IntersectClipRect (GDI32.@)
232 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
234 INT ret;
235 DC *dc = DC_GetDCUpdate( hdc );
236 if (!dc) return ERROR;
238 TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
240 if(dc->funcs->pIntersectClipRect)
241 ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
242 else
244 POINT pt[2];
246 pt[0].x = left;
247 pt[0].y = top;
248 pt[1].x = right;
249 pt[1].y = bottom;
251 LPtoDP( hdc, pt, 2 );
253 if (!dc->hClipRgn)
255 dc->hClipRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y );
256 ret = SIMPLEREGION;
258 else
260 HRGN newRgn;
262 if (!(newRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
263 else
265 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
266 DeleteObject( newRgn );
269 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
271 GDI_ReleaseObj( hdc );
272 return ret;
276 /***********************************************************************
277 * ExcludeVisRect (GDI.73)
279 INT16 WINAPI ExcludeVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
281 HRGN tempRgn;
282 INT16 ret;
283 POINT pt[2];
284 HDC hdc = HDC_32( hdc16 );
285 DC * dc = DC_GetDCUpdate( hdc );
286 if (!dc) return ERROR;
288 pt[0].x = left;
289 pt[0].y = top;
290 pt[1].x = right;
291 pt[1].y = bottom;
293 LPtoDP( hdc, pt, 2 );
295 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
297 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
298 else
300 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_DIFF );
301 DeleteObject( tempRgn );
303 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
304 GDI_ReleaseObj( hdc );
305 return ret;
309 /***********************************************************************
310 * IntersectVisRect (GDI.98)
312 INT16 WINAPI IntersectVisRect16( HDC16 hdc16, INT16 left, INT16 top, INT16 right, INT16 bottom )
314 HRGN tempRgn;
315 INT16 ret;
316 POINT pt[2];
317 HDC hdc = HDC_32( hdc16 );
318 DC * dc = DC_GetDCUpdate( hdc );
319 if (!dc) return ERROR;
321 pt[0].x = left;
322 pt[0].y = top;
323 pt[1].x = right;
324 pt[1].y = bottom;
326 LPtoDP( hdc, pt, 2 );
328 TRACE("%p %ld,%ld - %ld,%ld\n", hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
331 if (!(tempRgn = CreateRectRgn( pt[0].x, pt[0].y, pt[1].x, pt[1].y ))) ret = ERROR;
332 else
334 ret = CombineRgn( dc->hVisRgn, dc->hVisRgn, tempRgn, RGN_AND );
335 DeleteObject( tempRgn );
337 if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
338 GDI_ReleaseObj( hdc );
339 return ret;
343 /***********************************************************************
344 * PtVisible (GDI32.@)
346 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
348 BOOL ret = FALSE;
349 DC *dc = DC_GetDCUpdate( hdc );
351 TRACE("%p %d,%d\n", hdc, x, y );
352 if (!dc) return FALSE;
353 if (dc->hGCClipRgn)
355 POINT pt;
357 pt.x = x;
358 pt.y = y;
359 LPtoDP( hdc, &pt, 1 );
360 ret = PtInRegion( dc->hGCClipRgn, pt.x, pt.y );
362 GDI_ReleaseObj( hdc );
363 return ret;
367 /***********************************************************************
368 * RectVisible (GDI32.@)
370 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
372 BOOL ret = FALSE;
373 DC *dc = DC_GetDCUpdate( hdc );
374 if (!dc) return FALSE;
375 TRACE("%p %ld,%ldx%ld,%ld\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
376 if (dc->hGCClipRgn)
378 POINT pt[2];
379 RECT tmpRect;
381 pt[0].x = rect->left;
382 pt[0].y = rect->top;
383 pt[1].x = rect->right;
384 pt[1].y = rect->bottom;
385 LPtoDP( hdc, pt, 2 );
386 tmpRect.left = pt[0].x;
387 tmpRect.top = pt[0].y;
388 tmpRect.right = pt[1].x;
389 tmpRect.bottom = pt[1].y;
390 ret = RectInRegion( dc->hGCClipRgn, &tmpRect );
392 GDI_ReleaseObj( hdc );
393 return ret;
397 /***********************************************************************
398 * GetClipBox (GDI32.@)
400 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
402 INT ret;
403 DC *dc = DC_GetDCUpdate( hdc );
404 if (!dc) return ERROR;
405 ret = GetRgnBox( dc->hGCClipRgn, rect );
406 DPtoLP( hdc, (LPPOINT)rect, 2 );
407 GDI_ReleaseObj( hdc );
408 return ret;
412 /***********************************************************************
413 * GetClipRgn (GDI32.@)
415 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
417 INT ret = -1;
418 DC * dc;
419 if (hRgn && (dc = DC_GetDCPtr( hdc )))
421 if( dc->hClipRgn )
423 if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR ) ret = 1;
425 else ret = 0;
426 GDI_ReleaseObj( hdc );
428 return ret;
431 /***********************************************************************
432 * SaveVisRgn (GDI.129)
434 HRGN16 WINAPI SaveVisRgn16( HDC16 hdc16 )
436 HRGN copy;
437 GDIOBJHDR *obj, *copyObj;
438 HDC hdc = HDC_32( hdc16 );
439 DC *dc = DC_GetDCUpdate( hdc );
441 if (!dc) return 0;
442 TRACE("%p\n", hdc );
444 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC )))
446 GDI_ReleaseObj( hdc );
447 return 0;
449 if (!(copy = CreateRectRgn( 0, 0, 0, 0 )))
451 GDI_ReleaseObj( dc->hVisRgn );
452 GDI_ReleaseObj( hdc );
453 return 0;
455 CombineRgn( copy, dc->hVisRgn, 0, RGN_COPY );
456 if (!(copyObj = GDI_GetObjPtr( copy, REGION_MAGIC )))
458 DeleteObject( copy );
459 GDI_ReleaseObj( dc->hVisRgn );
460 GDI_ReleaseObj( hdc );
461 return 0;
463 copyObj->hNext = obj->hNext;
464 obj->hNext = HRGN_16(copy);
465 GDI_ReleaseObj( copy );
466 GDI_ReleaseObj( dc->hVisRgn );
467 GDI_ReleaseObj( hdc );
468 return HRGN_16(copy);
472 /***********************************************************************
473 * RestoreVisRgn (GDI.130)
475 INT16 WINAPI RestoreVisRgn16( HDC16 hdc16 )
477 HRGN saved;
478 GDIOBJHDR *obj, *savedObj;
479 HDC hdc = HDC_32( hdc16 );
480 DC *dc = DC_GetDCPtr( hdc );
481 INT16 ret = ERROR;
483 if (!dc) return ERROR;
485 TRACE("%p\n", hdc );
487 if (!(obj = GDI_GetObjPtr( dc->hVisRgn, REGION_MAGIC ))) goto done;
488 saved = HRGN_32(obj->hNext);
490 if ((savedObj = GDI_GetObjPtr( saved, REGION_MAGIC )))
492 ret = CombineRgn( dc->hVisRgn, saved, 0, RGN_COPY );
493 obj->hNext = savedObj->hNext;
494 GDI_ReleaseObj( saved );
495 DeleteObject( saved );
496 dc->flags &= ~DC_DIRTY;
497 CLIPPING_UpdateGCRegion( dc );
499 GDI_ReleaseObj( dc->hVisRgn );
500 done:
501 GDI_ReleaseObj( hdc );
502 return ret;
506 /***********************************************************************
507 * GetRandomRgn [GDI32.@]
509 * NOTES
510 * This function is documented in MSDN online for the case of
511 * dwCode == SYSRGN (4).
513 * For dwCode == 1 it should return the clip region
514 * 2 " " " the meta region
515 * 3 " " " the intersection of the clip with
516 * the meta region (== 'Rao' region).
518 * See http://www.codeproject.com/gdi/cliprgnguide.asp
520 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, DWORD dwCode)
522 switch (dwCode)
524 case SYSRGN: /* == 4 */
526 DC *dc = DC_GetDCPtr (hDC);
527 if (!dc) return -1;
529 CombineRgn (hRgn, dc->hVisRgn, 0, RGN_COPY);
530 GDI_ReleaseObj( hDC );
532 * On Windows NT/2000,
533 * the region returned is in screen coordinates.
534 * On Windows 95/98,
535 * the region returned is in window coordinates
537 if (!(GetVersion() & 0x80000000))
539 POINT org;
540 GetDCOrgEx(hDC, &org);
541 OffsetRgn(hRgn, org.x, org.y);
543 return 1;
546 case 1: /* clip region */
547 return GetClipRgn (hDC, hRgn);
549 default:
550 WARN("Unknown dwCode %ld\n", dwCode);
551 return -1;
554 return -1;
558 /***********************************************************************
559 * GetMetaRgn (GDI32.@)
561 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
563 FIXME( "stub\n" );
565 return 0;
569 /***********************************************************************
570 * SetMetaRgn (GDI32.@)
572 INT WINAPI SetMetaRgn( HDC hdc )
574 FIXME( "stub\n" );
576 return ERROR;