shell32/tests: Add some tests related to the shellview created by ExplorerBrowser.
[wine] / dlls / gdi32 / clipping.c
1 /*
2  * DC clipping functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "gdi_private.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(clipping);
30
31
32 /***********************************************************************
33  *           get_clip_region
34  *
35  * Return the total clip region (if any).
36  */
37 static inline HRGN get_clip_region( DC * dc )
38 {
39     if (dc->hMetaClipRgn) return dc->hMetaClipRgn;
40     if (dc->hMetaRgn) return dc->hMetaRgn;
41     return dc->hClipRgn;
42 }
43
44
45 /***********************************************************************
46  *           get_clip_rect
47  *
48  * Compute a clip rectangle from its logical coordinates.
49  */
50 static inline RECT get_clip_rect( DC * dc, int left, int top, int right, int bottom )
51 {
52     RECT rect;
53
54     rect.left   = left;
55     rect.top    = top;
56     rect.right  = right;
57     rect.bottom = bottom;
58     LPtoDP( dc->hSelf, (POINT *)&rect, 2 );
59     if (dc->layout & LAYOUT_RTL)
60     {
61         int tmp = rect.left;
62         rect.left = rect.right + 1;
63         rect.right = tmp + 1;
64     }
65     return rect;
66 }
67
68 /***********************************************************************
69  *           CLIPPING_UpdateGCRegion
70  *
71  * Update the GC clip region when the ClipRgn or VisRgn have changed.
72  */
73 void CLIPPING_UpdateGCRegion( DC * dc )
74 {
75     HRGN clip_rgn;
76
77     /* update the intersection of meta and clip regions */
78     if (dc->hMetaRgn && dc->hClipRgn)
79     {
80         if (!dc->hMetaClipRgn) dc->hMetaClipRgn = CreateRectRgn( 0, 0, 0, 0 );
81         CombineRgn( dc->hMetaClipRgn, dc->hClipRgn, dc->hMetaRgn, RGN_AND );
82         clip_rgn = dc->hMetaClipRgn;
83     }
84     else  /* only one is set, no need for an intersection */
85     {
86         if (dc->hMetaClipRgn) DeleteObject( dc->hMetaClipRgn );
87         dc->hMetaClipRgn = 0;
88         clip_rgn = dc->hMetaRgn ? dc->hMetaRgn : dc->hClipRgn;
89     }
90
91     if (dc->funcs->pSetDeviceClipping)
92         dc->funcs->pSetDeviceClipping( dc->physDev, dc->hVisRgn, clip_rgn );
93 }
94
95 /***********************************************************************
96  *           create_default_clip_region
97  *
98  * Create a default clipping region when none already exists.
99  */
100 static inline void create_default_clip_region( DC * dc )
101 {
102     UINT width, height;
103
104     if (dc->header.type == OBJ_MEMDC)
105     {
106         BITMAP bitmap;
107
108         GetObjectW( dc->hBitmap, sizeof(bitmap), &bitmap );
109         width = bitmap.bmWidth;
110         height = bitmap.bmHeight;
111     }
112     else
113     {
114         width = GetDeviceCaps( dc->hSelf, DESKTOPHORZRES );
115         height = GetDeviceCaps( dc->hSelf, DESKTOPVERTRES );
116     }
117     dc->hClipRgn = CreateRectRgn( 0, 0, width, height );
118 }
119
120
121 /***********************************************************************
122  *           SelectClipRgn    (GDI32.@)
123  */
124 INT WINAPI SelectClipRgn( HDC hdc, HRGN hrgn )
125 {
126     return ExtSelectClipRgn( hdc, hrgn, RGN_COPY );
127 }
128
129
130 /******************************************************************************
131  *              ExtSelectClipRgn        [GDI32.@]
132  */
133 INT WINAPI ExtSelectClipRgn( HDC hdc, HRGN hrgn, INT fnMode )
134 {
135     INT retval;
136     RECT rect;
137     DC * dc = get_dc_ptr( hdc );
138     if (!dc) return ERROR;
139
140     TRACE("%p %p %d\n", hdc, hrgn, fnMode );
141
142     update_dc( dc );
143     if (dc->funcs->pExtSelectClipRgn)
144     {
145         retval = dc->funcs->pExtSelectClipRgn( dc->physDev, hrgn, fnMode );
146         release_dc_ptr( dc );
147         return retval;
148     }
149
150     if (!hrgn)
151     {
152         if (fnMode == RGN_COPY)
153         {
154             if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
155             dc->hClipRgn = 0;
156         }
157         else
158         {
159             FIXME("Unimplemented: hrgn NULL in mode: %d\n", fnMode);
160             release_dc_ptr( dc );
161             return ERROR;
162         }
163     }
164     else
165     {
166         HRGN mirrored = 0;
167
168         if (dc->layout & LAYOUT_RTL)
169         {
170             if (!(mirrored = CreateRectRgn( 0, 0, 0, 0 )))
171             {
172                 release_dc_ptr( dc );
173                 return ERROR;
174             }
175             mirror_region( mirrored, hrgn, dc->vis_rect.right - dc->vis_rect.left );
176             hrgn = mirrored;
177         }
178
179         if (!dc->hClipRgn)
180             create_default_clip_region( dc );
181
182         if(fnMode == RGN_COPY)
183             CombineRgn( dc->hClipRgn, hrgn, 0, fnMode );
184         else
185             CombineRgn( dc->hClipRgn, dc->hClipRgn, hrgn, fnMode);
186
187         if (mirrored) DeleteObject( mirrored );
188     }
189
190     CLIPPING_UpdateGCRegion( dc );
191     release_dc_ptr( dc );
192
193     return GetClipBox(hdc, &rect);
194 }
195
196 /***********************************************************************
197  *           __wine_set_visible_region   (GDI32.@)
198  */
199 void CDECL __wine_set_visible_region( HDC hdc, HRGN hrgn, const RECT *vis_rect )
200 {
201     DC * dc;
202
203     if (!(dc = get_dc_ptr( hdc ))) return;
204
205     TRACE( "%p %p %s\n", hdc, hrgn, wine_dbgstr_rect(vis_rect) );
206
207     /* map region to DC coordinates */
208     OffsetRgn( hrgn, -vis_rect->left, -vis_rect->top );
209
210     DeleteObject( dc->hVisRgn );
211     dc->dirty = 0;
212     dc->vis_rect = *vis_rect;
213     dc->hVisRgn = hrgn;
214     DC_UpdateXforms( dc );
215     CLIPPING_UpdateGCRegion( dc );
216     release_dc_ptr( dc );
217 }
218
219
220 /***********************************************************************
221  *           OffsetClipRgn    (GDI32.@)
222  */
223 INT WINAPI OffsetClipRgn( HDC hdc, INT x, INT y )
224 {
225     INT ret = SIMPLEREGION;
226     DC *dc = get_dc_ptr( hdc );
227     if (!dc) return ERROR;
228
229     TRACE("%p %d,%d\n", hdc, x, y );
230
231     update_dc( dc );
232     if(dc->funcs->pOffsetClipRgn)
233     {
234         ret = dc->funcs->pOffsetClipRgn( dc->physDev, x, y );
235         /* FIXME: ret is just a success flag, we should return a proper value */
236     }
237     else if (dc->hClipRgn) {
238         x = MulDiv( x, dc->vportExtX, dc->wndExtX );
239         y = MulDiv( y, dc->vportExtY, dc->wndExtY );
240         if (dc->layout & LAYOUT_RTL) x = -x;
241         ret = OffsetRgn( dc->hClipRgn, x, y );
242         CLIPPING_UpdateGCRegion( dc );
243     }
244     release_dc_ptr( dc );
245     return ret;
246 }
247
248
249 /***********************************************************************
250  *           ExcludeClipRect    (GDI32.@)
251  */
252 INT WINAPI ExcludeClipRect( HDC hdc, INT left, INT top,
253                                 INT right, INT bottom )
254 {
255     HRGN newRgn;
256     INT ret;
257     DC *dc = get_dc_ptr( hdc );
258     if (!dc) return ERROR;
259
260     TRACE("%p %dx%d,%dx%d\n", hdc, left, top, right, bottom );
261
262     update_dc( dc );
263     if(dc->funcs->pExcludeClipRect)
264     {
265         ret = dc->funcs->pExcludeClipRect( dc->physDev, left, top, right, bottom );
266         /* FIXME: ret is just a success flag, we should return a proper value */
267     }
268     else
269     {
270         RECT rect = get_clip_rect( dc, left, top, right, bottom );
271
272         if (!(newRgn = CreateRectRgnIndirect( &rect ))) ret = ERROR;
273         else
274         {
275             if (!dc->hClipRgn)
276                 create_default_clip_region( dc );
277             ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_DIFF );
278             DeleteObject( newRgn );
279         }
280         if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
281     }
282     release_dc_ptr( dc );
283     return ret;
284 }
285
286
287 /***********************************************************************
288  *           IntersectClipRect    (GDI32.@)
289  */
290 INT WINAPI IntersectClipRect( HDC hdc, INT left, INT top, INT right, INT bottom )
291 {
292     INT ret;
293     DC *dc = get_dc_ptr( hdc );
294     if (!dc) return ERROR;
295
296     TRACE("%p %d,%d - %d,%d\n", hdc, left, top, right, bottom );
297
298     update_dc( dc );
299     if(dc->funcs->pIntersectClipRect)
300     {
301         ret = dc->funcs->pIntersectClipRect( dc->physDev, left, top, right, bottom );
302         /* FIXME: ret is just a success flag, we should return a proper value */
303     }
304     else
305     {
306         RECT rect = get_clip_rect( dc, left, top, right, bottom );
307
308         if (!dc->hClipRgn)
309         {
310             dc->hClipRgn = CreateRectRgnIndirect( &rect );
311             ret = SIMPLEREGION;
312         }
313         else
314         {
315             HRGN newRgn;
316
317             if (!(newRgn = CreateRectRgnIndirect( &rect ))) ret = ERROR;
318             else
319             {
320                 ret = CombineRgn( dc->hClipRgn, dc->hClipRgn, newRgn, RGN_AND );
321                 DeleteObject( newRgn );
322             }
323         }
324         if (ret != ERROR) CLIPPING_UpdateGCRegion( dc );
325     }
326     release_dc_ptr( dc );
327     return ret;
328 }
329
330
331 /***********************************************************************
332  *           PtVisible    (GDI32.@)
333  */
334 BOOL WINAPI PtVisible( HDC hdc, INT x, INT y )
335 {
336     POINT pt;
337     BOOL ret;
338     HRGN clip;
339     DC *dc = get_dc_ptr( hdc );
340
341     TRACE("%p %d,%d\n", hdc, x, y );
342     if (!dc) return FALSE;
343
344     pt.x = x;
345     pt.y = y;
346     LPtoDP( hdc, &pt, 1 );
347     update_dc( dc );
348     ret = PtInRegion( dc->hVisRgn, pt.x, pt.y );
349     if (ret && (clip = get_clip_region(dc))) ret = PtInRegion( clip, pt.x, pt.y );
350     release_dc_ptr( dc );
351     return ret;
352 }
353
354
355 /***********************************************************************
356  *           RectVisible    (GDI32.@)
357  */
358 BOOL WINAPI RectVisible( HDC hdc, const RECT* rect )
359 {
360     RECT tmpRect;
361     BOOL ret;
362     HRGN clip;
363     DC *dc = get_dc_ptr( hdc );
364     if (!dc) return FALSE;
365     TRACE("%p %d,%dx%d,%d\n", hdc, rect->left, rect->top, rect->right, rect->bottom );
366
367     tmpRect = *rect;
368     LPtoDP( hdc, (POINT *)&tmpRect, 2 );
369
370     update_dc( dc );
371     if ((clip = get_clip_region(dc)))
372     {
373         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
374         CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
375         ret = RectInRegion( hrgn, &tmpRect );
376         DeleteObject( hrgn );
377     }
378     else ret = RectInRegion( dc->hVisRgn, &tmpRect );
379     release_dc_ptr( dc );
380     return ret;
381 }
382
383
384 /***********************************************************************
385  *           GetClipBox    (GDI32.@)
386  */
387 INT WINAPI GetClipBox( HDC hdc, LPRECT rect )
388 {
389     INT ret;
390     HRGN clip;
391     DC *dc = get_dc_ptr( hdc );
392     if (!dc) return ERROR;
393
394     update_dc( dc );
395     if ((clip = get_clip_region(dc)))
396     {
397         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
398         CombineRgn( hrgn, dc->hVisRgn, clip, RGN_AND );
399         ret = GetRgnBox( hrgn, rect );
400         DeleteObject( hrgn );
401     }
402     else ret = GetRgnBox( dc->hVisRgn, rect );
403     DPtoLP( hdc, (LPPOINT)rect, 2 );
404     release_dc_ptr( dc );
405     return ret;
406 }
407
408
409 /***********************************************************************
410  *           GetClipRgn  (GDI32.@)
411  */
412 INT WINAPI GetClipRgn( HDC hdc, HRGN hRgn )
413 {
414     INT ret = -1;
415     DC * dc;
416     if ((dc = get_dc_ptr( hdc )))
417     {
418       if( dc->hClipRgn )
419       {
420           if( CombineRgn(hRgn, dc->hClipRgn, 0, RGN_COPY) != ERROR )
421           {
422               ret = 1;
423               if (dc->layout & LAYOUT_RTL)
424                   mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
425           }
426       }
427       else ret = 0;
428       release_dc_ptr( dc );
429     }
430     return ret;
431 }
432
433
434 /***********************************************************************
435  *           GetMetaRgn    (GDI32.@)
436  */
437 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
438 {
439     INT ret = 0;
440     DC * dc = get_dc_ptr( hdc );
441
442     if (dc)
443     {
444         if (dc->hMetaRgn && CombineRgn( hRgn, dc->hMetaRgn, 0, RGN_COPY ) != ERROR)
445         {
446             ret = 1;
447             if (dc->layout & LAYOUT_RTL)
448                 mirror_region( hRgn, hRgn, dc->vis_rect.right - dc->vis_rect.left );
449         }
450         release_dc_ptr( dc );
451     }
452     return ret;
453 }
454
455
456 /***********************************************************************
457  * GetRandomRgn [GDI32.@]
458  *
459  * NOTES
460  *     This function is documented in MSDN online for the case of
461  *     iCode == SYSRGN (4).
462  *
463  *     For iCode == 1 it should return the clip region
464  *                  2 "    "       "   the meta region
465  *                  3 "    "       "   the intersection of the clip with
466  *                                     the meta region (== 'Rao' region).
467  *
468  *     See http://www.codeproject.com/gdi/cliprgnguide.asp
469  */
470 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, INT iCode)
471 {
472     HRGN rgn;
473     DC *dc = get_dc_ptr( hDC );
474
475     if (!dc) return -1;
476
477     switch (iCode)
478     {
479     case 1:
480         rgn = dc->hClipRgn;
481         break;
482     case 2:
483         rgn = dc->hMetaRgn;
484         break;
485     case 3:
486         rgn = dc->hMetaClipRgn;
487         if(!rgn) rgn = dc->hClipRgn;
488         if(!rgn) rgn = dc->hMetaRgn;
489         break;
490     case SYSRGN: /* == 4 */
491         update_dc( dc );
492         rgn = dc->hVisRgn;
493         break;
494     default:
495         WARN("Unknown code %d\n", iCode);
496         release_dc_ptr( dc );
497         return -1;
498     }
499     if (rgn) CombineRgn( hRgn, rgn, 0, RGN_COPY );
500     release_dc_ptr( dc );
501
502     /* On Windows NT/2000, the SYSRGN returned is in screen coordinates */
503     if (iCode == SYSRGN && !(GetVersion() & 0x80000000))
504         OffsetRgn( hRgn, dc->vis_rect.left, dc->vis_rect.top );
505
506     return (rgn != 0);
507 }
508
509
510 /***********************************************************************
511  *           SetMetaRgn    (GDI32.@)
512  */
513 INT WINAPI SetMetaRgn( HDC hdc )
514 {
515     INT ret;
516     RECT dummy;
517     DC *dc = get_dc_ptr( hdc );
518
519     if (!dc) return ERROR;
520
521     if (dc->hMetaClipRgn)
522     {
523         /* the intersection becomes the new meta region */
524         DeleteObject( dc->hMetaRgn );
525         DeleteObject( dc->hClipRgn );
526         dc->hMetaRgn = dc->hMetaClipRgn;
527         dc->hClipRgn = 0;
528         dc->hMetaClipRgn = 0;
529     }
530     else if (dc->hClipRgn)
531     {
532         dc->hMetaRgn = dc->hClipRgn;
533         dc->hClipRgn = 0;
534     }
535     /* else nothing to do */
536
537     /* Note: no need to call CLIPPING_UpdateGCRegion, the overall clip region hasn't changed */
538
539     ret = GetRgnBox( dc->hMetaRgn, &dummy );
540     release_dc_ptr( dc );
541     return ret;
542 }