source: trunk/src/gdi32/region.cpp@ 10442

Last change on this file since 10442 was 10442, checked in by sandervl, 22 years ago

Wrong GdiCopyClipRgn return value; Implemented SelectClipPath

File size: 69.3 KB
Line 
1/* $Id: region.cpp,v 1.38 2004-02-10 15:35:39 sandervl Exp $ */
2
3/*
4 * GDI32 region code
5 *
6 * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
7 * Copyright 1998 Patrick Haller
8 * Copyright 2002-2003 Innotek Systemberatung GmbH (sandervl@innotek.de)
9 *
10 * TODO: Metafile recording
11 * TODO: Do we need to translate & set the last error for Gpi operations?
12 *
13 *
14 * NOTE: In windows there is a clip and a visible region (just like in PM)
15 * We do not map the Win32 visible region to the PM visible region
16 * as that's rather annoying.
17 * Instead we use the intersection of the win32 visible and win32 clip
18 * region for the PM clip region.
19 * The original Win32 clip region is saved in pHps->hrgnWin32Clip.
20 * The Win32 visible region is saved in pHps->hrgnWinVis.
21 *
22 *
23 *
24 * Project Odin Software License can be found in LICENSE.TXT
25 *
26 */
27
28#define INCL_GPI
29#define INCL_WIN
30#include <os2wrap.h> //need odin wrappers
31
32#include <win32type.h>
33#include <win32api.h>
34#include <winconst.h>
35#include <stdlib.h>
36#include <stdarg.h>
37#include <string.h>
38#include <misc.h>
39#include <objhandle.h>
40#include <dcdata.h>
41#include <winuser32.h>
42#include "oslibgpi.h"
43#include <stats.h>
44#include "dibsect.h"
45#include <wingdi32.h>
46
47#define DBG_LOCALLOG DBG_region
48#include "dbglocal.h"
49
50//GPI and Windows use the same values
51#define GPI_TO_WIN_COMPLEXITY(a) a
52
53typedef enum
54{
55 AS_DEVICE,
56 AS_WORLD
57} InterpMode;
58
59#define MEM_HPS_MAX 768
60
61int WIN32API OffsetRgn( HRGN hrgn, int xOffset, int yOffset);
62
63static void convertDeviceRect(HWND hwnd, pDCData pHps, PRECTL pRectl, ULONG count);
64
65#define convertWinDeviceRectToPMDeviceRect(arg1, arg2, arg3) convertDeviceRect(arg1, arg2, arg3)
66#define convertPMDeviceRectToWinDeviceRect(arg1, arg2, arg3) convertDeviceRect(arg1, arg2, arg3)
67
68inline void convertDeviceRect(pDCData pHps, PRECTL pRectl, ULONG count)
69{
70 convertDeviceRect(0, pHps, pRectl, count);
71}
72inline void convertDeviceRect(HWND hwnd, PRECTL pRectl, ULONG count)
73{
74 convertDeviceRect(hwnd, 0, pRectl, count);
75}
76
77HPS hpsRegion = 0;
78
79
80#ifdef DEBUG
81//#define dprintfRegion(a,b,c) if(DbgEnabledLvl2GDI32[DBG_LOCALLOG] == 1) dprintfRegion1(a,b,c)
82//#define dprintfRegion(a,b,c) dprintfRegion1(a,b,c)
83
84void dprintfRegion(HPS hps, HRGN hrgnClip)
85{
86 RGNRECT rgnRect = {0, 16, 0, RECTDIR_LFRT_TOPBOT};
87 RECTL rectRegion[16];
88 APIRET rc;
89
90 dprintf(("dprintfRegion %x", hps));
91 rc = GpiQueryRegionRects(hps, hrgnClip, NULL, &rgnRect, &rectRegion[0]);
92 for(int i=0;i<rgnRect.crcReturned;i++) {
93 dprintf(("(%d,%d)(%d,%d)", rectRegion[i].xLeft, rectRegion[i].yBottom, rectRegion[i].xRight, rectRegion[i].yTop));
94 }
95}
96#else
97#define dprintfRegion(a,c)
98#endif
99
100//******************************************************************************
101//******************************************************************************
102BOOL InitRegionSpace()
103{
104 hpsRegion = WinGetScreenPS(HWND_DESKTOP);
105 return hpsRegion != 0;
106}
107//******************************************************************************
108//******************************************************************************
109void DestroyRegionSpace()
110{
111 if(hpsRegion) WinReleasePS(hpsRegion);
112 hpsRegion = 0;
113}
114//******************************************************************************
115//******************************************************************************
116LONG hdcHeight(HWND hwnd, pDCData pHps)
117{
118 if(hwnd == 0 && pHps != 0)
119 hwnd = pHps->hwnd;
120
121 if(hwnd != 0 || pHps == 0)
122 {
123 RECT rect;
124 RECTL rectl;
125 LONG y = 0;
126
127 if(pHps && pHps->isClient) //client area
128 {
129 if(GetClientRect(OS2ToWin32Handle(hwnd), &rect) == TRUE) {
130 y = rect.bottom - rect.top;
131 }
132 }
133 else
134 if(WinQueryWindowRect(hwnd, &rectl)) //whole window
135 y = rectl.yTop;
136
137 return y;
138 }
139 else
140 if(pHps->bitmapHandle)
141 {
142 return pHps->bitmapHeight;
143 }
144 else
145 if(pHps->isMetaPS)
146 {
147 return 0;
148 }
149 else
150 if(pHps->isPrinter)
151 {
152 return GetDeviceCaps(pHps->hps, VERTRES_W);
153 }
154 else
155 {
156 return MEM_HPS_MAX;
157 }
158}
159//******************************************************************************
160//******************************************************************************
161LONG hdcWidth(HWND hwnd, pDCData pHps)
162{
163 if(hwnd == 0 && pHps != 0)
164 hwnd = pHps->hwnd;
165
166 if(hwnd != 0 || pHps == 0)
167 {
168 RECT rect;
169 RECTL rectl;
170 LONG x = 0;
171
172 if(pHps && pHps->isClient) //client area
173 {
174 if(GetClientRect(OS2ToWin32Handle(hwnd), &rect) == TRUE) {
175 x = rect.right - rect.left;
176 }
177 }
178 else
179 if(WinQueryWindowRect(hwnd, &rectl)) //whole window
180 x = rectl.xRight;
181
182 return x;
183 }
184 else
185 if(pHps->bitmapHandle)
186 {
187 return pHps->bitmapWidth;
188 }
189 else
190 if(pHps->isMetaPS)
191 {
192 return 0;
193 }
194 else
195 if(pHps->isPrinter)
196 {
197 return GetDeviceCaps(pHps->hps, HORZRES_W);
198 }
199// else
200// DebugInt3();
201 return 0;
202}
203//******************************************************************************
204//******************************************************************************
205static void convertWinWorldRectToPMDeviceRect(pDCData pHps_Hdc, PRECTL pRects, UINT count = 1)
206{
207 PRECTL pRectl;
208 for (int x=0; x < count; x++)
209 {
210 pRectl = &pRects[x];
211
212 if(pRectl->yTop < pRectl->yBottom) {
213 ULONG temp = pRectl->yBottom;
214 pRectl->yBottom = pRectl->yTop;
215 pRectl->yTop = temp;
216 }
217 BOOL success = GpiConvert( pHps_Hdc->hps, CVTC_WORLD, CVTC_DEVICE, 2, (PPOINTL)pRectl);
218 if(!success)
219 {
220 break;
221 }
222 if(pRectl->yTop < pRectl->yBottom) {
223 ULONG temp = pRectl->yBottom;
224 pRectl->yBottom = pRectl->yTop;
225 pRectl->yTop = temp;
226 }
227 }
228}
229//******************************************************************************
230//******************************************************************************
231static void convertDeviceRect(HWND hwnd, pDCData pHps, PRECTL pRectl, ULONG count)
232{
233 long wHeight, wWidth;
234
235 if(hwnd == HWND_DESKTOP) {
236 wHeight = OSLibGetScreenHeight();
237 wWidth = OSLibGetScreenWidth();
238 }
239 else {
240 wHeight = hdcHeight(hwnd, pHps);
241 wWidth = hdcWidth(hwnd, pHps);
242 }
243
244 if(pHps)
245 {
246 wHeight += pHps->HPStoHDCInversionHeight;
247 }
248
249 if(hwnd || (pHps && pHps->hwnd)) {
250 for(int x=0; x < count; x++)
251 {
252 if(pRectl[x].xRight > wWidth) {
253 pRectl[x].xRight = wWidth;
254 }
255 if(pRectl[x].yTop > wHeight) {
256 pRectl[x].yTop = 0;
257 }
258 else pRectl[x].yTop = wHeight - pRectl[x].yTop;
259
260 pRectl[x].yBottom = wHeight - pRectl[x].yBottom;
261 }
262 }
263 else {
264 for(int x=0; x < count; x++)
265 {
266 pRectl[x].yTop = wHeight - pRectl[x].yTop;
267 pRectl[x].yBottom = wHeight - pRectl[x].yBottom;
268 }
269 }
270}
271//******************************************************************************
272//******************************************************************************
273static void convertPMDeviceRectToWinWorldRect(pDCData pHps_Hdc, PRECTL pRectl)
274{
275 GpiConvert(pHps_Hdc->hps, CVTC_DEVICE, CVTC_WORLD, 2, (PPOINTL)pRectl );
276}
277//******************************************************************************
278//******************************************************************************
279BOOL interpretRegionAs(pDCData pHps, HRGN hrgnDest, HRGN hrgnSrc, InterpMode mode)
280{
281 BOOL success = FALSE;
282 int x, i;
283 HPS hpsDest;
284
285 if(pHps)
286 {
287 if(!hrgnDest) {
288 hrgnDest = pHps->hrgnHDC;
289#ifdef DEBUG
290 if(hrgnDest == 0) {
291 DebugInt3();
292 }
293#endif
294 }
295 hpsDest = pHps->hps;
296 }
297 else {
298 DebugInt3();
299 return FALSE;
300 }
301
302 RGNRECT rgnRect;
303 rgnRect.ircStart = 1;
304 rgnRect.crc = 0;
305 rgnRect.ulDirection = RECTDIR_LFRT_TOPBOT;
306 success = GpiQueryRegionRects(hpsRegion, hrgnSrc, NULL, &rgnRect, NULL);
307 if(success)
308 {
309 if(rgnRect.crcReturned > 0)
310 {
311 rgnRect.crc = rgnRect.crcReturned;
312 PRECTL pRectl = new RECTL[rgnRect.crcReturned];
313 success = GpiQueryRegionRects(hpsRegion, hrgnSrc, NULL, &rgnRect, pRectl);
314 if(success)
315 {
316 if(mode == AS_DEVICE)
317 {
318 ULONG temp;
319 for(x=0; x < rgnRect.crcReturned; x++)
320 {
321 temp = pRectl[x].yBottom;
322 pRectl[x].yBottom = pRectl[x].yTop;
323 pRectl[x].yTop = temp;
324 }
325 convertWinDeviceRectToPMDeviceRect(pHps, pRectl, rgnRect.crcReturned);
326 }
327 else
328 {
329 GpiConvert(pHps->hps, CVTC_WORLD, CVTC_DEVICE, rgnRect.crcReturned<<1, (PPOINTL)pRectl);
330
331 ULONG temp;
332 for(i=0; i < rgnRect.crcReturned; i++)
333 {
334 if(pRectl[i].yTop < pRectl[i].yBottom)
335 {
336 temp = pRectl[i].yBottom + 1;
337 pRectl[i].yBottom = pRectl[i].yTop + 1;
338 pRectl[i].yTop = temp;
339 }
340 else
341 {
342 pRectl[i].yTop++;
343 pRectl[i].yBottom++;
344 }
345 }
346 }
347 success = GpiSetRegion(hpsDest, hrgnDest, rgnRect.crcReturned, pRectl);
348 }
349 delete[] pRectl;
350 }
351 else
352 {
353 success = GpiSetRegion(hpsDest, hrgnDest, 0, NULL);
354 }
355 }
356 return success;
357}
358//******************************************************************************
359//exported (used by user32)
360//******************************************************************************
361BOOL WIN32API setWinDeviceRegionFromPMDeviceRegion(HRGN winHrgn, HRGN pmHrgn, pDCData pHpsPmHrgn, HWND hwndPmHrgn)
362{
363 BOOL success;
364 int x;
365 BOOL mustReleaseScreenPS = FALSE;
366 HPS hps;
367
368 if(pHpsPmHrgn || hwndPmHrgn)
369 {
370 if(pHpsPmHrgn)
371 hps = pHpsPmHrgn->hps;
372 else
373 {
374 hps = WinGetScreenPS(HWND_DESKTOP);
375 mustReleaseScreenPS = TRUE;
376 }
377
378 RGNRECT rgnRect;
379 rgnRect.ircStart = 1;
380 rgnRect.crc = 0;
381 rgnRect.ulDirection = RECTDIR_LFRT_TOPBOT;
382 success = GpiQueryRegionRects(hps, pmHrgn, NULL, &rgnRect, NULL);
383 if(success )
384 {
385 if(rgnRect.crcReturned > 0)
386 {
387 rgnRect.crc = rgnRect.crcReturned;
388 PRECTL pRectl = new RECTL[rgnRect.crcReturned];
389 success = GpiQueryRegionRects(hps, pmHrgn, NULL, &rgnRect, pRectl);
390 if(success)
391 {
392 if(pHpsPmHrgn)
393 convertPMDeviceRectToWinDeviceRect(pHpsPmHrgn, pRectl, rgnRect.crcReturned);
394 else
395 convertPMDeviceRectToWinDeviceRect(hwndPmHrgn, pRectl, rgnRect.crcReturned);
396
397 for(x=0; x < rgnRect.crcReturned; x++)
398 {
399 ULONG temp = pRectl[x].yBottom;
400 pRectl[x].yBottom = pRectl[x].yTop;
401 pRectl[x].yTop = temp;
402 }
403
404 success = GpiSetRegion(hpsRegion, winHrgn, rgnRect.crcReturned, pRectl);
405 }
406 delete[] pRectl;
407 }
408 else
409 {
410 success = GpiSetRegion(hpsRegion, winHrgn, 0, NULL);
411 }
412 }
413 }
414 else
415 success = FALSE;
416
417 if(mustReleaseScreenPS)
418 WinReleasePS(hps);
419
420 return success;
421}
422//******************************************************************************
423// GdiSetVisRgn
424//
425// Change the Win32 visible region to the specified region
426//
427// Parameters:
428//
429// pDCData pHps - presentation space structure
430// HRGN hrgn - region handle (GPI)
431//
432//
433// Returns: - ERROR_W -> failure
434// - NULLREGION_W -> empty region
435// - SIMPLEREGION_W -> rectangular region
436// - COMPLEXREGION_W -> complex region
437//
438// NOTE: In windows there is a clip and a visible region (just like in PM)
439// We do not map the Win32 visible region to the PM visible region
440// as that's rather annoying.
441// Instead we use the intersection of the win32 visible and win32 clip
442// region for the PM clip region.
443// The Win32 clip region is saved in pHps->hrgnWin32Clip.
444// The Win32 visible region is saved in pHps->hrgnWinVis.
445//
446//******************************************************************************
447INT WIN32API GdiSetVisRgn(pDCData pHps, HRGN hrgn)
448{
449 HRGN hrgnNewClip = NULLHANDLE, hrgnOldClip = NULLHANDLE;
450 LONG lComplexity = RGN_NULL;
451 RECTL rectl = {0, 0, 1, 1};
452
453 if(pHps == NULL) {
454 DebugInt3();
455 return ERROR_W;
456 }
457
458 if(hrgn != NULLHANDLE)
459 {
460 hrgnNewClip = GpiCreateRegion(pHps->hps, 1, &rectl);
461 if(hrgnNewClip == NULLHANDLE) {
462 dprintf(("ERROR: GdiCombineVisRgn: GpiCreateRegion failed!!"));
463 DebugInt3();
464 goto failure;
465 }
466 //make a copy of the new visible region
467 lComplexity = GpiCombineRegion(pHps->hps, hrgnNewClip, hrgn, NULLHANDLE, CRGN_COPY);
468 }
469 else {
470 hrgnNewClip = NULLHANDLE;
471 }
472
473 //and set that as the GPI clip region
474 lComplexity = GpiSetClipRegion(pHps->hps, hrgnNewClip, &hrgnOldClip);
475 if (lComplexity != RGN_ERROR)
476 {
477 //SvL: Must check if origin changed here. Sometimes happens when
478 // window looses focus. (don't know why....)
479 checkOrigin(pHps);
480
481 if(hrgnOldClip) GpiDestroyRegion(pHps->hps, hrgnOldClip);
482
483 dprintf(("New visible region %x", hrgn));
484 pHps->hrgnWinVis = hrgn;
485 return GPI_TO_WIN_COMPLEXITY(lComplexity);
486 }
487failure:
488 if(hrgnNewClip) GpiDestroyRegion(pHps->hps, hrgnNewClip);
489
490 return ERROR_W;
491}
492//******************************************************************************
493// GdiCombineVisRgn
494//
495// Combine the specified region with the visible region according to the operation
496//
497// Parameters:
498//
499// pDCData pHps - presentation space structure
500// HRGN hrgn - region handle (Win32)
501// INT operation - combine operation (RGN_*)
502//
503//
504// Returns: - ERROR_W -> failure
505// - NULLREGION_W -> empty region
506// - SIMPLEREGION_W -> rectangular region
507// - COMPLEXREGION_W -> complex region
508//
509// NOTE: In windows there is a clip and a visible region (just like in PM)
510// We do not map the Win32 visible region to the PM visible region
511// as that's rather annoying.
512// Instead we use the intersection of the win32 visible and win32 clip
513// region for the PM clip region.
514// The Win32 clip region is saved in pHps->hrgnWin32Clip.
515// The Win32 visible region is saved in pHps->hrgnWinVis.
516//
517//******************************************************************************
518INT WIN32API GdiCombineVisRgn(pDCData pHps, HRGN hrgn, INT operation)
519{
520#ifdef DEBUG_LOGGING
521 HRGN hrgn1 = hrgn;
522#endif
523 LONG lComplexity = RGN_NULL;
524
525 if(pHps == NULL) {
526 DebugInt3();
527 return ERROR_W;
528 }
529
530 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
531 if(hrgn == HANDLE_OBJ_ERROR || !pHps) {
532 dprintf(("ERROR: GdiCombineVisRgn %x invalid handle", hrgn1));
533 return ERROR_W;
534 }
535 //interpretRegionAs converts the region and saves it in pHps->hrgnHDC
536 if(!interpretRegionAs(pHps, 0, hrgn, AS_DEVICE) )
537 {
538 dprintf(("ERROR: interpretRegionAs failed!!"));
539 return ERROR_W;
540 }
541
542 //If there is no visible region, then create one that covers the whole DC
543 if(pHps->hrgnWinVis == NULLHANDLE)
544 {
545 RECTL rectl = {0, 0, hdcWidth(0, pHps), hdcHeight(0, pHps)};
546 dprintf(("No visible region: create one (0,0)(%d,%d)", rectl.xRight, rectl.yTop));
547 pHps->hrgnWinVis = GpiCreateRegion(pHps->hps, 1, &rectl);
548 if(pHps->hrgnWinVis == NULLHANDLE) {
549 dprintf(("ERROR: GdiCombineVisRgn: GpiCreateRegion failed!!"));
550 DebugInt3();
551 goto failure;
552 }
553 }
554
555 LONG lMode;
556 switch (operation)
557 {
558 case RGN_AND_W : lMode = CRGN_AND ; break;
559 case RGN_DIFF_W : lMode = CRGN_DIFF; break;
560 default:
561 dprintf(("ERROR: GdiCombineVisRgn %d invalid parameter", operation));
562 DebugInt3();
563 goto failure;
564 }
565 lComplexity = GpiCombineRegion(pHps->hps, pHps->hrgnWinVis, pHps->hrgnWinVis, pHps->hrgnHDC, lMode);
566 if (lComplexity != RGN_ERROR)
567 {
568 return GdiSetVisRgn(pHps, pHps->hrgnWinVis);
569 }
570 dprintf(("ERROR: GdiCombineVisRgn: GpiCombineRegion failed"));
571 DebugInt3();
572
573failure:
574 return ERROR_W;
575}
576//******************************************************************************
577// GdiCombineVisRgnClipRgn
578//
579// Combine the specified clip region with the visible region according to the operation
580//
581// Parameters:
582//
583// pDCData pHps - presentation space structure
584// HRGN hrgn - region handle (GPI)
585// INT operation - combine operation (RGN_*)
586//
587//
588// Returns: - ERROR_W -> failure
589// - NULLREGION_W -> empty region
590// - SIMPLEREGION_W -> rectangular region
591// - COMPLEXREGION_W -> complex region
592//
593// NOTE: In windows there is a clip and a visible region (just like in PM)
594// We do not map the Win32 visible region to the PM visible region
595// as that's rather annoying.
596// Instead we use the intersection of the win32 visible and win32 clip
597// region for the PM clip region.
598// The Win32 clip region is saved in pHps->hrgnWin32Clip.
599// The Win32 visible region is saved in pHps->hrgnWinVis.
600//
601//******************************************************************************
602INT WIN32API GdiCombineVisRgnClipRgn(pDCData pHps, HRGN hrgn, INT operation)
603{
604 HRGN hrgnClip = NULL, hrgnOldClip, hrgnNewClip;
605 LONG lComplexity = RGN_NULL;
606
607 //Create a region that will be used for the new GPI clip region
608 RECTL rectl = {0, 0, 1, 1};
609 hrgnNewClip = GpiCreateRegion(pHps->hps, 1, &rectl);
610 if(hrgnNewClip == NULLHANDLE) {
611 dprintf(("ERROR: GdiCombineVisRgnClipRgn: GpiCreateRegion failed!!"));
612 DebugInt3();
613 return ERROR_W;
614 }
615
616 if(hrgn == NULLHANDLE)
617 {
618 if(pHps->hrgnWin32Clip != NULL)
619 {
620 //Only reset the path if both the visible and clip regions are zero
621 if(!pHps->hrgnWinVis) {
622 GpiSetClipPath(pHps->hps, 0, SCP_RESET);
623
624 GpiDestroyRegion(pHps->hps, hrgnNewClip);
625 hrgnNewClip = NULLHANDLE;
626 }
627 else
628 {//set PM clip region to Win32 visible region
629 lComplexity = GpiCombineRegion(pHps->hps, hrgnNewClip, pHps->hrgnWinVis, NULLHANDLE, CRGN_COPY);
630 if(lComplexity == RGN_ERROR) {
631 dprintf(("ERROR: GdiCombineVisRgnClipRgn: GpiSetClipRegion failed!!"));
632 DebugInt3();
633 goto failure;
634 }
635 }
636 dprintfRegion(pHps->hps, hrgnNewClip);
637 lComplexity = GpiSetClipRegion(pHps->hps, hrgnNewClip, &hrgnOldClip);
638 if(lComplexity == RGN_ERROR) {
639 dprintf(("ERROR: GdiCombineVisRgnClipRgn: GpiSetClipRegion failed!!"));
640 DebugInt3();
641 }
642
643 //SvL: Must check if origin changed here. Sometimes happens when
644 // window looses focus. (don't know why....)
645 checkOrigin(pHps);
646 if(hrgnOldClip) GpiDestroyRegion(pHps->hps, hrgnOldClip);
647 }
648 //else already NULL, so nothing to do.
649
650 pHps->hrgnWin32Clip = hrgn;
651 return NULLREGION_W;
652 }
653
654 LONG lMode;
655 switch (operation)
656 {
657 case RGN_AND_W : lMode = CRGN_AND ; break; //intersect clip & visible region
658 default:
659 dprintf(("ERROR: GdiCombineVisRgnClipRgn %d invalid parameter", operation));
660 DebugInt3();
661 goto failure;
662 }
663 // If there's no visible region (meaning entire DC is visible), then just set
664 // the GPI clip region to the win32 clip region
665 if (pHps->hrgnWinVis == NULLHANDLE)
666 {
667 lComplexity = GpiCombineRegion(pHps->hps, hrgnNewClip, hrgn, NULLHANDLE, CRGN_COPY);
668 }
669 else lComplexity = GpiCombineRegion(pHps->hps, hrgnNewClip, pHps->hrgnWinVis, hrgn, lMode);
670
671 if (lComplexity != RGN_ERROR)
672 {
673 dprintfRegion(pHps->hps, hrgnNewClip);
674 //And activate the new clip region
675 lComplexity = GpiSetClipRegion(pHps->hps, hrgnNewClip, &hrgnOldClip);
676 if (lComplexity == RGN_ERROR )
677 {
678 dprintf(("ERROR: GdiCombineVisRgnClipRgn: GpiSetClipRegion failed"));
679 DebugInt3();
680 goto failure;
681 }
682 //SvL: Must check if origin changed here. Sometimes happens when
683 // window looses focus. (don't know why....)
684 checkOrigin(pHps);
685
686 if(hrgnOldClip) GpiDestroyRegion(pHps->hps, hrgnOldClip);
687
688 pHps->hrgnWin32Clip = hrgn;
689
690 return GPI_TO_WIN_COMPLEXITY(lComplexity);
691 }
692 dprintf(("ERROR: GdiCombineVisRgnClipRgn: GpiCombineRegion failed"));
693 DebugInt3();
694
695failure:
696 if(hrgnNewClip) GpiDestroyRegion(pHps->hps, hrgnNewClip);
697
698 return ERROR_W;
699}
700//******************************************************************************
701// GdiCopyClipRgn
702//
703// Duplicate the GPI region
704//
705// Parameters:
706//
707// pDCData pHps - presentation space structure
708//
709// Returns: - NULL -> failure
710// - <> NULL -> GPI handle of copied region
711//
712//******************************************************************************
713HRGN GdiCopyClipRgn(pDCData pHps)
714{
715 HRGN hrgnNewClip;
716 LONG lComplexity;
717 RECTL rectl = {0, 0, 1, 1};
718
719 hrgnNewClip = GpiCreateRegion(pHps->hps, 1, &rectl);
720 if(hrgnNewClip == NULLHANDLE) {
721 dprintf(("ERROR: GdiCopyClipRgn: GpiCreateRegion failed!!"));
722 DebugInt3();
723 return NULLHANDLE;
724 }
725 lComplexity = GpiCombineRegion(pHps->hps, hrgnNewClip, pHps->hrgnWin32Clip, NULLHANDLE, CRGN_COPY);
726 if (lComplexity != RGN_ERROR)
727 {
728 return hrgnNewClip;
729 }
730 dprintf(("GpiCombineRegion %x %x %x failed!!", pHps->hps, hrgnNewClip, pHps->hrgnWin32Clip));
731 DebugInt3();
732 return NULLHANDLE;
733}
734//******************************************************************************
735// GdiDestroyRgn
736//
737// Destroy the GPI region
738//
739// Parameters:
740//
741// pDCData pHps - presentation space structure
742// HRGN hrgn - region handle (GPI)
743//
744// Returns: - FALSE -> failure
745// - TRUE -> success
746//
747//******************************************************************************
748BOOL GdiDestroyRgn(pDCData pHps, HRGN hrgn)
749{
750 return GpiDestroyRegion(pHps->hps, hrgn);
751}
752//******************************************************************************
753//******************************************************************************
754int WIN32API ExtSelectClipRgn(HDC hdc, HRGN hrgn, int mode)
755{
756#ifdef DEBUG_LOGGING
757 HRGN hrgn1 = hrgn;
758#endif
759
760 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
761 if (!pHps)
762 {
763 dprintf(("WARNING: ExtSelectRgn %x %x %d invalid hdc", hdc, hrgn, mode));
764 SetLastError(ERROR_INVALID_HANDLE_W);
765 return ERROR_W;
766 }
767
768 LONG lComplexity;
769 HRGN hrgnCurrent = NULLHANDLE, hrgnOld = NULLHANDLE;
770
771 if(!hrgn && mode != RGN_COPY_W)
772 {
773 SetLastError(ERROR_INVALID_PARAMETER_W);
774 dprintf(("WARNING: ExtSelectRgn %x %x %d invalid parameter", hdc, hrgn1, mode));
775 return ERROR_W;
776 }
777
778 LONG lMode;
779 switch (mode)
780 {
781 case RGN_AND_W : lMode = CRGN_AND ; break;
782 case RGN_COPY_W : lMode = CRGN_COPY; break;
783 case RGN_DIFF_W : lMode = CRGN_DIFF; break;
784 case RGN_OR_W : lMode = CRGN_OR ; break;
785 case RGN_XOR_W : lMode = CRGN_XOR ; break;
786 default:
787 {
788 SetLastError(ERROR_INVALID_PARAMETER_W);
789 dprintf(("WARNING: ExtSelectRgn %x %x %d invalid parameter", hdc, hrgn1, mode));
790 return ERROR_W;
791 }
792 }
793
794 if(hrgn)
795 {
796 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
797 if(hrgn == HANDLE_OBJ_ERROR) {
798 dprintf(("WARNING: ExtSelectRgn %x %x %d invalid region handle", hdc, hrgn1, mode));
799 SetLastError(ERROR_INVALID_HANDLE_W);
800 return 0;
801 }
802 dprintfRegion(hdc, hrgn);
803 }
804
805 //TODO: metafile recording
806 if(hrgn)
807 {
808 //interpretRegionAs converts the region and saves it in pHps->hrgnHDC
809 if(!interpretRegionAs(pHps, 0, hrgn, AS_DEVICE) )
810 {
811 dprintf(("interpretRegionAs failed!!"));
812 return ERROR_W;
813 }
814 }
815 else
816 {
817 //Intersect the Windows clip region with the current visible region
818 lComplexity = GdiCombineVisRgnClipRgn(pHps, NULLHANDLE, RGN_AND_W);
819
820 return NULLREGION_W;
821 }
822
823 // get current clipping region
824 hrgnOld = pHps->hrgnWin32Clip;
825 hrgnCurrent = hrgnOld;
826
827 if(hrgnCurrent == NULLHANDLE)
828 {//if none, then create one
829 lMode = CRGN_COPY;
830 RECTL rectl = {0, 0, 1, 1};
831 hrgnCurrent = GpiCreateRegion(pHps->hps, 1, &rectl);
832 }
833
834 HRGN hrgnSrc1;
835 HRGN hrgnSrc2;
836 if(lMode != CRGN_COPY)
837 {
838 hrgnSrc1 = hrgnCurrent;
839 hrgnSrc2 = pHps->hrgnHDC;
840 }
841 else
842 {
843 hrgnSrc1 = pHps->hrgnHDC;
844 hrgnSrc2 = NULLHANDLE;
845 }
846
847 lComplexity = GpiCombineRegion(pHps->hps, hrgnCurrent, hrgnSrc1, hrgnSrc2, lMode);
848 if (lComplexity != RGN_ERROR)
849 {
850 HRGN hrgnOld;
851
852 //Intersect the Windows clip region with the current visible region
853 lComplexity = GdiCombineVisRgnClipRgn(pHps, hrgnCurrent, RGN_AND_W);
854
855 SetLastError(ERROR_SUCCESS_W);
856
857 if (lComplexity != RGN_ERROR) {
858 return lComplexity;
859 }
860 }
861 dprintf(("GpiCombineRegion failed %x %x %x %d", hrgnCurrent, hrgnSrc1, hrgnSrc2, lMode));
862 GpiDestroyRegion(pHps->hps, hrgnCurrent); //delete newly created region
863
864 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO
865 return ERROR_W;
866}
867//******************************************************************************
868//******************************************************************************
869int WIN32API SelectClipRgn(HDC hdc, HRGN hrgn)
870{
871 return ExtSelectClipRgn(hdc, hrgn, RGN_COPY_W);
872}
873//******************************************************************************
874// The GetClipBox function retrieves the dimensions of the tightest bounding
875// rectangle that can be drawn around the current visible area on the device.
876// The visible area is defined by the current clipping region or clip path, as well
877// as any overlapping windows.
878//
879// NOTE: We simply call GpiQueryClipBox as it behaves just like GetClipBox
880//
881//******************************************************************************
882int WIN32API GetClipBox(HDC hdc, PRECT lpRect)
883{
884 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
885 RECTL rectl;
886 LONG lComplexity;
887 int rc;
888
889 if(!hdc || !lpRect || !pHps) {
890 dprintf(("GDI32: GetClipBox %x %x ERROR_INVALID_PARAMETER", hdc, lpRect));
891 SetLastError(ERROR_INVALID_PARAMETER_W);
892 return ERROR_W;
893 }
894 if(pHps->isPrinter)
895 {
896 //Some printers return the wrong clip box. Return the full page instead.
897 //(TODO: which is incorrect!)
898 lpRect->left = 0;
899 lpRect->top = 0;
900 lpRect->right = GetDeviceCaps( hdc, HORZRES_W);
901 lpRect->bottom = GetDeviceCaps( hdc, VERTRES_W);
902 includeBottomRightPoint(pHps, (PPOINTLOS2)&rectl);
903 convertPMDeviceRectToWinWorldRect(pHps, &rectl);
904
905 rc = SIMPLEREGION_W;
906 }
907 else {
908 lComplexity = GpiQueryClipBox(pHps->hps, &rectl);
909 if(lComplexity == RGN_ERROR)
910 {
911 rc = ERROR_W;
912 }
913 else
914 if(lComplexity == RGN_NULL)
915 {
916 memset(lpRect, 0, sizeof(*lpRect));
917 rc = NULLREGION_W;
918 }
919 else {
920#ifndef INVERT
921 //Convert coordinates from PM to win32
922 if (pHps->yInvert > 0) {
923 LONG temp = pHps->yInvert - rectl.yBottom;
924 rectl.yBottom = pHps->yInvert - rectl.yTop;
925 rectl.yTop = temp;
926 }
927#endif
928 //Convert including/including to including/excluding
929 includeBottomRightPoint(pHps, (PPOINTLOS2)&rectl);
930
931 lpRect->left = rectl.xLeft;
932 lpRect->right = rectl.xRight;
933
934 if(rectl.yBottom > rectl.yTop) {
935 lpRect->top = rectl.yTop;
936 lpRect->bottom = rectl.yBottom;
937 }
938 else {
939 lpRect->top = rectl.yBottom;
940 lpRect->bottom = rectl.yTop;
941 }
942
943 rc = GPI_TO_WIN_COMPLEXITY(lComplexity);
944 }
945 }
946// if(lpRect->left == 0 && lpRect->top == 0 && lpRect->right == 0 && lpRect->bottom == 0)
947// DebugInt3();
948 dprintf(("GDI32: GetClipBox of %X returned %d; (%d,%d)(%d,%d)", hdc, rc, lpRect->left, lpRect->top, lpRect->right, lpRect->bottom));
949 return rc;
950}
951//******************************************************************************
952//******************************************************************************
953int WIN32API GetClipRgn(HDC hdc, HRGN hrgn)
954{
955 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
956 BOOL success;
957 LONG lComplexity = RGN_RECT;
958 HRGN hrgnClip = NULL, hrgnTemp;
959#ifdef DEBUG_LOGGING
960 HRGN hrgn1 = hrgn;
961#endif
962
963 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
964 if(hrgn == HANDLE_OBJ_ERROR || !pHps) {
965 dprintf(("WARNING: GetClipRgn %x %x invalid handle", hdc, hrgn1));
966 SetLastError(ERROR_INVALID_HANDLE_W);
967 return 0;
968 }
969
970 if(pHps->hrgnWin32Clip) {
971 dprintfRegion(hdc, pHps->hrgnWin32Clip);
972 if(!setWinDeviceRegionFromPMDeviceRegion(hrgn, pHps->hrgnWin32Clip, pHps, NULL)) {
973 dprintf(("WARNING: GetClipRgn setWinDeviceRegionFromPMDeviceRegion failed!"));
974 GpiSetClipRegion(pHps->hps, hrgnClip, &hrgnTemp);
975 SetLastError(ERROR_INVALID_PARAMETER_W); //todo right errror
976 return 0;
977 }
978 }
979 else {
980 //no clip region; return NULL
981 lComplexity = RGN_NULL;
982 }
983
984 SetLastError(ERROR_SUCCESS_W);
985 if(lComplexity == RGN_NULL)
986 return 0;
987 else return 1;
988}
989//******************************************************************************
990// The GetRandomRgn function copies the system clipping region of a specified
991// device context to a specific region.
992// It returns the visible region of the specified device context in screen
993// coordinates (if it belongs to a window).
994//
995// VALUE = 1: This undocumented value will return the current Clip Region contained in the DC.
996// VALUE = 2: This undocumented value will return the current Meta Region contained in the DC.
997// VALUE = 3: This undocumented value will return the intersection of the Clip Region and Meta Region.
998// VALUE = 4, SYSRGN: The only value that is documented and defined for this function.
999// This value returns the System Region
1000//******************************************************************************
1001INT WIN32API GetRandomRgn(HDC hdc, HRGN hrgn, INT iNum)
1002{
1003 HWND hwnd;
1004 INT ret;
1005
1006 if(iNum != SYSRGN_W) {
1007 dprintf(("WARNING: GetRandomRgn: invalid parameter %x", iNum));
1008 SetLastError(ERROR_INVALID_PARAMETER_W);
1009 return ERROR_W;
1010 }
1011 ret = GetClipRgn(hdc, hrgn);
1012
1013 hwnd = WindowFromDC(hdc);
1014 if(hwnd) {
1015 POINT pt = {0,0};
1016 //map from client to desktop coordinates
1017 MapWindowPoints(hwnd, 0, &pt, 1);
1018
1019 OffsetRgn(hrgn, pt.x, pt.y);
1020 }
1021 return ret;
1022}
1023//******************************************************************************
1024//******************************************************************************
1025int WIN32API ExcludeClipRect(HDC hdc, int left, int top, int right, int bottom)
1026{
1027 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
1028 if(!pHps)
1029 {
1030 dprintf(("ERROR: ExcludeClipRgn %x (%d,%d)(%d,%d) invalid hdc", hdc, left, top, right, bottom));
1031 SetLastError(ERROR_INVALID_HANDLE_W);
1032 return ERROR_W;
1033 }
1034
1035 LONG lComplexity;
1036 RECTL rectl = { left, bottom, right, top };
1037
1038 excludeBottomRightPoint(pHps, (PPOINTLOS2)&rectl);
1039 if(rectl.yTop < rectl.yBottom) {
1040 ULONG temp = rectl.yBottom;
1041 rectl.yBottom = rectl.yTop;
1042 rectl.yTop = temp;
1043 }
1044
1045#ifndef INVERT
1046 if (pHps->yInvert > 0) {
1047 LONG temp = pHps->yInvert - rectl.yBottom;
1048 rectl.yBottom = pHps->yInvert - rectl.yTop;
1049 rectl.yTop = temp;
1050 }
1051#endif
1052
1053 dprintf(("ExcludeClipRgn %x (%d,%d)(%d,%d)", hdc, left, top, right, bottom));
1054
1055 lComplexity = GpiExcludeClipRectangle(pHps->hps, &rectl);
1056 if (lComplexity == RGN_ERROR) {
1057 dprintf(("ERROR: ExcludeClipRgn: GpiExcludeClipRectangle failed!!"));
1058 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: wrong error
1059 return ERROR_W;
1060 }
1061
1062 //Ok, success. Now update the cached win32 clip region
1063 if(pHps->hrgnWin32Clip != NULLHANDLE)
1064 {
1065 HRGN hrgnClipRect = GpiCreateRegion(pHps->hps, 1, &rectl);
1066 if(hrgnClipRect == NULLHANDLE) {
1067 dprintf(("ERROR: ExcludeClipRgn GpiCreateRegion failed!!"));
1068 DebugInt3();
1069 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: wrong error
1070 return ERROR_W;
1071 }
1072 //subtract rect region from clip region
1073 lComplexity = GpiCombineRegion(pHps->hps, pHps->hrgnWin32Clip, pHps->hrgnWin32Clip, hrgnClipRect, CRGN_DIFF);
1074 GpiDestroyRegion(pHps->hps, hrgnClipRect);
1075
1076 if (lComplexity == RGN_ERROR) {
1077 dprintf(("ERROR: ExcludeClipRgn GpiCombineRegion failed!!"));
1078 DebugInt3();
1079 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: wrong error
1080 return ERROR_W;
1081 }
1082 }
1083
1084 //todo metafile recording
1085
1086 SetLastError(ERROR_SUCCESS_W);
1087 return lComplexity;
1088}
1089//******************************************************************************
1090//******************************************************************************
1091int WIN32API IntersectClipRect(HDC hdc, int left, int top, int right, int bottom)
1092{
1093 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
1094 LONG lComplexity;
1095 BOOL success;
1096
1097 if(!pHps) {
1098 dprintf(("WARNING: IntersectClipRect %x (%d,%d)(%d,%d) invalid hdc", hdc, left, top, right, bottom));
1099 SetLastError(ERROR_INVALID_HANDLE_W);
1100 return ERROR_W;
1101 }
1102 RECTL rectl = { left, bottom, right, top };
1103
1104 excludeBottomRightPoint(pHps, (PPOINTLOS2)&rectl);
1105 if(rectl.yTop < rectl.yBottom) {
1106 ULONG temp = rectl.yBottom;
1107 rectl.yBottom = rectl.yTop;
1108 rectl.yTop = temp;
1109 }
1110
1111#ifndef INVERT
1112 //Convert coordinates from PM to win32
1113 if (pHps->yInvert > 0) {
1114 LONG temp = pHps->yInvert - rectl.yBottom;
1115 rectl.yBottom = pHps->yInvert - rectl.yTop;
1116 rectl.yTop = temp;
1117 }
1118#endif
1119
1120 dprintf(("IntersectClipRect %x (%d,%d)(%d,%d)", hdc, left, top, right, bottom));
1121 lComplexity = GpiIntersectClipRectangle(pHps->hps, &rectl);
1122 if (lComplexity == RGN_ERROR) {
1123 dprintf(("ERROR: IntersectClipRect: GpiIntersectClipRectangle failed!!"));
1124 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: wrong error
1125 return ERROR_W;
1126 }
1127
1128 //Ok, success. Now update the cached win32 clip region
1129 if(pHps->hrgnWin32Clip != NULLHANDLE)
1130 {
1131 HRGN hrgnClipRect = GpiCreateRegion(pHps->hps, 1, &rectl);
1132 if(hrgnClipRect == NULLHANDLE) {
1133 dprintf(("ERROR: IntersectClipRect GpiCreateRegion failed!!"));
1134 DebugInt3();
1135 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: wrong error
1136 return ERROR_W;
1137 }
1138 //intersect rect region with clip region
1139 lComplexity = GpiCombineRegion(pHps->hps, pHps->hrgnWin32Clip, pHps->hrgnWin32Clip, hrgnClipRect, CRGN_AND);
1140 GpiDestroyRegion(pHps->hps, hrgnClipRect);
1141
1142 if (lComplexity == RGN_ERROR) {
1143 dprintf(("ERROR: IntersectClipRect GpiCombineRegion failed!!"));
1144 DebugInt3();
1145 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: wrong error
1146 return ERROR_W;
1147 }
1148 }
1149
1150 //todo metafile recording
1151
1152 SetLastError(ERROR_SUCCESS_W);
1153 return lComplexity;
1154}
1155//******************************************************************************
1156//******************************************************************************
1157int WIN32API OffsetClipRgn(HDC hdc, int nXOffset, int nYOffset )
1158{
1159 BOOL success;
1160 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
1161 LONG lComplexity;
1162
1163 if(!pHps) {
1164 dprintf(("OffsetClipRgn %x (%d,%d) invalid hdc", hdc, nXOffset, nYOffset));
1165 SetLastError(ERROR_INVALID_HANDLE_W);
1166 return ERROR_W;
1167 }
1168
1169 dprintf(("OffsetClipRgn %x (%d,%d)", hdc, nXOffset, nYOffset));
1170 POINTL pointl = { nXOffset, nYOffset };
1171#ifndef INVERT
1172 if (pHps->yInvert > 0) {
1173 pointl.y = pHps->yInvert - pointl.y;
1174 }
1175#endif
1176
1177 if(pHps->hrgnWin32Clip == NULLHANDLE) {
1178 lComplexity = NULLREGION_W;
1179 }
1180 else {
1181 lComplexity = GpiOffsetRegion(pHps->hps, pHps->hrgnWin32Clip, &pointl);
1182 if (lComplexity == RGN_ERROR) {
1183 dprintf(("ERROR: OffsetClipRgn: GpiOffsetRegion failed!!"));
1184 SetLastError(ERROR_INVALID_PARAMETER_W); //TODO: wrong error
1185 return ERROR_W;
1186 }
1187
1188 //Intersect the Windows clip region with the current visible region
1189 lComplexity = GdiCombineVisRgnClipRgn(pHps, pHps->hrgnWin32Clip, RGN_AND_W);
1190
1191 lComplexity = GPI_TO_WIN_COMPLEXITY(lComplexity);
1192 }
1193
1194 //todo metafile recording
1195
1196 SetLastError(ERROR_SUCCESS_W);
1197 return lComplexity;
1198}
1199//******************************************************************************
1200//******************************************************************************
1201HRGN WIN32API CreatePolyPolygonRgn(const POINT *lppt, const int *pPolyCount,
1202 int nCount, int fnPolyFillMode)
1203{
1204 LONG flMode;
1205
1206 switch(fnPolyFillMode)
1207 {
1208 case ALTERNATE_W:
1209 flMode = POLYGON_ALTERNATE;
1210 break;
1211 case WINDING_W:
1212 flMode = POLYGON_WINDING;
1213 break;
1214 default:
1215 dprintf(("WARNING: CreatePolyPolygonRgn %x %x %d %d invalid parameter", lppt, pPolyCount, nCount, fnPolyFillMode));
1216 SetLastError(ERROR_INVALID_PARAMETER_W);
1217 return 0;
1218 }
1219
1220 if(nCount < 1)
1221 {
1222 dprintf(("WARNING: CreatePolyPolygonRgn %x %x %d %d invalid parameter", lppt, pPolyCount, nCount, fnPolyFillMode));
1223 SetLastError(ERROR_INVALID_PARAMETER_W);
1224 return 0;
1225 }
1226
1227 PPOLYGON pPolygon = new POLYGON[nCount];
1228 if(!pPolygon) {
1229 dprintf(("ERROR: CreatePolyPolygonRgn: out of memory!!"));
1230 SetLastError(ERROR_OUTOFMEMORY_W);
1231 return 0;
1232 }
1233 dprintf(("CreatePolyPolygonRgn %x %x %d %d", lppt, pPolyCount, nCount, fnPolyFillMode));
1234
1235 PPOINTL pPointl = (PPOINTL)lppt+1; // skip first point
1236 for (int x=0; x < nCount; x++) {
1237 pPolygon[x].ulPoints = (x == 0) ? pPolyCount[x] - 1 : pPolyCount[x];
1238 pPolygon[x].aPointl = pPointl;
1239 pPointl += pPolygon[x].ulPoints;
1240 }
1241 GpiMove(hpsRegion, (PPOINTL)lppt);
1242 HRGN hrgn = GpiCreatePolygonRegion(hpsRegion, nCount, pPolygon, POLYGON_BOUNDARY | flMode);
1243
1244 delete[] pPolygon;
1245
1246 if(!hrgn) {
1247 SetLastError(ERROR_INVALID_PARAMETER_W); //todo: not right
1248 return 0;
1249 }
1250
1251 if(ObjAllocateHandle(&hrgn, hrgn, HNDL_REGION) == FALSE) {
1252 GpiDestroyRegion(hpsRegion, hrgn);
1253 SetLastError(ERROR_OUTOFMEMORY_W);
1254 return 0;
1255 }
1256 STATS_CreatePolyPolygonRgn(hrgn, lppt, pPolyCount, nCount, fnPolyFillMode);
1257
1258 SetLastError(ERROR_SUCCESS_W);
1259 return hrgn;
1260}
1261//******************************************************************************
1262//******************************************************************************
1263HRGN WIN32API CreateRectRgn(int left, int top, int right, int bottom)
1264{
1265 HRGN hrgn;
1266 RECTL rectl = { left, top < bottom ? top : bottom, right, top < bottom ? bottom : top };
1267
1268 hrgn = GpiCreateRegion(hpsRegion, 1, &rectl);
1269 if(!hrgn) {
1270 dprintf(("WARNING: CreateRectRgn: GpiCreateRectRegion failed! %x", WinGetLastError(0)));
1271 SetLastError(ERROR_INVALID_PARAMETER_W); //todo: not right
1272 return 0;
1273 }
1274
1275 if(ObjAllocateHandle(&hrgn, hrgn, HNDL_REGION) == FALSE) {
1276 GpiDestroyRegion(hpsRegion, hrgn);
1277 SetLastError(ERROR_OUTOFMEMORY_W);
1278 return 0;
1279 }
1280 STATS_CreateRectRgn(hrgn, left, top, right, bottom);
1281
1282 dprintf(("CreateRectRegion (%d,%d)(%d,%d) returned %x", left, top, right, bottom, hrgn));
1283 SetLastError(ERROR_SUCCESS_W);
1284 return hrgn;
1285}
1286//******************************************************************************
1287//******************************************************************************
1288HRGN WIN32API CreateRectRgnIndirect( const RECT * lprc)
1289{
1290 return CreateRectRgn(lprc->left, lprc->top, lprc->right, lprc->bottom);
1291}
1292//******************************************************************************
1293//******************************************************************************
1294HRGN WIN32API CreateRoundRectRgn(int left, int top, int right, int bottom, int nWidthEllipse, int nHeightEllipse)
1295{
1296 HRGN hrgn;
1297 RECTL rectl = { left, top < bottom ? top : bottom, right, top < bottom ? bottom : top };
1298
1299 PPOINTL pPointl = (PPOINTL)&rectl;
1300
1301 GpiMove(hpsRegion, &pPointl[0]);
1302 hrgn = GpiCreateRoundRectRegion(hpsRegion, &pPointl[1], labs(nWidthEllipse), labs(nHeightEllipse));
1303
1304 if(!hrgn) {
1305 SetLastError(ERROR_INVALID_PARAMETER_W); //todo: not right
1306 dprintf(("WARNING: CreateRoundRectRgn: GpiCreateRoundRectRegion failed! %x", WinGetLastError(0)));
1307 return 0;
1308 }
1309
1310 if(ObjAllocateHandle(&hrgn, hrgn, HNDL_REGION) == FALSE) {
1311 GpiDestroyRegion(hpsRegion, hrgn);
1312 SetLastError(ERROR_OUTOFMEMORY_W);
1313 return 0;
1314 }
1315 STATS_CreateRoundRectRgn(hrgn, left, top, right, bottom, nWidthEllipse, nHeightEllipse);
1316
1317 dprintf(("CreateRoundRectRegion (%d,%d)(%d,%d) (%d,%d) returned %x", left, top, right, bottom, nWidthEllipse, nHeightEllipse, hrgn));
1318 SetLastError(ERROR_SUCCESS_W);
1319 return hrgn;
1320}
1321//******************************************************************************
1322//******************************************************************************
1323HRGN WIN32API ExtCreateRegion(const XFORM_W * pXform, DWORD count,
1324 const RGNDATA * pData)
1325{
1326 HRGN hrgn;
1327
1328 if(!pData || count < (sizeof(RGNDATAHEADER) + pData->rdh.nCount * sizeof(RECT))) {
1329 dprintf(("WARNING: ExtCreateRegion %x %d %x; invalid parameter", pXform, count, pData));
1330 SetLastError(ERROR_INVALID_PARAMETER_W);
1331 return 0;
1332 }
1333
1334 PRECTL pRectl = new RECTL[pData->rdh.nCount];
1335 PRECT pDataRects = (PRECT)pData->Buffer;
1336 for(int i=0; i < pData->rdh.nCount; i++) {
1337 MapWin32ToOS2Rect(pDataRects[i], pRectl[i]);
1338 }
1339
1340 BOOL doShear = pXform && (pXform->eM12 || pXform->eM21);
1341 HPS hpsTemp = NULLHANDLE;
1342
1343 dprintf(("ExtCreateRegion %x %d %x", pXform, count, pData));
1344 if(doShear) {
1345 hpsTemp = WinGetPS(HWND_DESKTOP);
1346 GpiBeginPath(hpsTemp, 1);
1347
1348 MATRIXLF matrixlf;
1349 matrixlf.fxM11 = pXform->eM11 * (float)0x10000;
1350 matrixlf.fxM12 = pXform->eM12 * (float)0x10000;
1351 matrixlf.lM13 = 0;
1352 matrixlf.fxM21 = pXform->eM21 * (float)0x10000;
1353 matrixlf.fxM22 = pXform->eM22 * (float)0x10000;
1354 matrixlf.lM23 = 0;
1355 matrixlf.lM31 = pXform->eDx;
1356 matrixlf.lM32 = pXform->eDy;
1357 matrixlf.lM33 = 1;
1358
1359 GpiSetModelTransformMatrix(hpsTemp, 9, &matrixlf, TRANSFORM_REPLACE);
1360 }
1361
1362 for(i=0; i < pData->rdh.nCount; i++)
1363 {
1364 LONG temp = pRectl[i].yTop;
1365 pRectl[i].yTop = pRectl[i].yBottom;
1366 pRectl[i].yBottom = temp;
1367
1368 if(pXform)
1369 {
1370 PPOINTL pPointl = ((PPOINTL)&pRectl[i]);
1371
1372 if (doShear) {
1373 GpiMove(hpsTemp, pPointl);
1374 GpiBox(hpsTemp, DRO_OUTLINE, pPointl+1, 0, 0);
1375 }
1376 else
1377 {
1378 for(int j=0; j < 2; j++) {
1379 pPointl[j].x = (pXform->eM11 * (float)pPointl[j].x) + (pXform->eM12 * (float)pPointl[j].y) + pXform->eDx;
1380 pPointl[j].y = (pXform->eM21 * (float)pPointl[j].x) + (pXform->eM22 * (float)pPointl[j].y) + pXform->eDy;
1381 }
1382
1383 PRECTL pRectlT = (PRECTL)pPointl;
1384 if (pRectlT->xLeft > pRectlT->xRight) {
1385 LONG temp = pRectlT->xLeft;
1386 pRectlT->xLeft = pRectlT->xRight;
1387 pRectlT->xRight = temp;
1388 }
1389 if (pRectlT->yBottom > pRectlT->yTop) {
1390 LONG temp = pRectlT->yBottom;
1391 pRectlT->yBottom = pRectlT->yTop;
1392 pRectlT->yTop = temp;
1393 }
1394 }
1395 }
1396 }
1397
1398 if(doShear) {
1399 GpiEndPath(hpsTemp);
1400 hrgn = GpiPathToRegion(hpsTemp, 1, FPATH_WINDING);
1401
1402 if(pRectl)
1403 delete[] pRectl;
1404
1405 if(hrgn == 0) {
1406 dprintf(("WARNING: ExtCreateRegion GpiCreateRegion failed! (%x)", WinGetLastError(0)));
1407 SetLastError(ERROR_INVALID_PARAMETER_W);
1408 return 0;
1409 }
1410 WinReleasePS(hpsTemp);
1411 }
1412 else {
1413 hrgn = GpiCreateRegion(hpsRegion, pData->rdh.nCount, pRectl);
1414 if(pRectl)
1415 delete[] pRectl;
1416
1417 if(hrgn == 0) {
1418 dprintf(("WARNING: ExtCreateRegion GpiCreateRegion failed! (%x)", WinGetLastError(0)));
1419 SetLastError(ERROR_INVALID_PARAMETER_W);
1420 return 0;
1421 }
1422 }
1423
1424 if(ObjAllocateHandle(&hrgn, hrgn, HNDL_REGION) == FALSE) {
1425 GpiDestroyRegion(hpsRegion, hrgn);
1426 SetLastError(ERROR_OUTOFMEMORY_W);
1427 return 0;
1428 }
1429 STATS_ExtCreateRegion(hrgn, (PVOID)pXform, count, pData);
1430 SetLastError(ERROR_SUCCESS_W);
1431 return hrgn;
1432}
1433//******************************************************************************
1434//******************************************************************************
1435HRGN WIN32API CreateEllipticRgn(int left, int top, int right, int bottom)
1436{
1437 HRGN hrgn;
1438
1439 RECTL rectl = { left,
1440 top < bottom ? top : bottom,
1441 right,
1442 top < bottom ? bottom : top };
1443
1444 dprintf(("CreateEllipticRgn (%d,%d)(%d,%d)", left, top, right, bottom));
1445 hrgn = GpiCreateEllipticRegion(hpsRegion, &rectl);
1446 if(hrgn == RGN_ERROR) {
1447 SetLastError(ERROR_INVALID_PARAMETER_W); //todo: not right
1448 dprintf(("WARNING: CreateEllipticRgn: GpiCreateEllipticRegion failed! %x", WinGetLastError(0)));
1449 return 0;
1450 }
1451
1452 if(ObjAllocateHandle(&hrgn, hrgn, HNDL_REGION) == FALSE) {
1453 GpiDestroyRegion(hpsRegion, hrgn);
1454 SetLastError(ERROR_OUTOFMEMORY_W);
1455 return 0;
1456 }
1457 STATS_CreateEllipticRgn(hrgn, left, top, right, bottom);
1458 SetLastError(ERROR_SUCCESS_W);
1459 return hrgn;
1460}
1461//******************************************************************************
1462//******************************************************************************
1463HRGN WIN32API CreateEllipticRgnIndirect(const RECT *pRect)
1464{
1465 return CreateEllipticRgn(pRect->left, pRect->top, pRect->right, pRect->bottom);
1466}
1467//******************************************************************************
1468//******************************************************************************
1469HRGN WIN32API CreatePolygonRgn(const POINT * lppt, int cPoints, int fnPolyFillMode)
1470{
1471 HRGN hrgn;
1472 LONG flMode;
1473
1474 if(!lppt || cPoints < 2) {
1475 dprintf(("WARNING: CreatePolygonRgn %x %d %d invalid parameter", lppt, cPoints, fnPolyFillMode));
1476 SetLastError(ERROR_INVALID_PARAMETER_W);
1477 return 0;
1478 }
1479
1480 switch (fnPolyFillMode)
1481 {
1482 case ALTERNATE_W :
1483 flMode = POLYGON_ALTERNATE;
1484 break;
1485 case WINDING_W :
1486 flMode = POLYGON_WINDING;
1487 break;
1488 default:
1489 dprintf(("WARNING: CreatePolygonRgn %x %d %d invalid parameter", lppt, cPoints, fnPolyFillMode));
1490 SetLastError(ERROR_INVALID_PARAMETER_W);
1491 return 0;
1492 }
1493 dprintf(("CreatePolygonRgn %x %d %d", lppt, cPoints, fnPolyFillMode));
1494
1495 POLYGON polygon;
1496 polygon.ulPoints = cPoints - 1;
1497 polygon.aPointl = (PPOINTL)(lppt + 1);
1498
1499#ifdef DEBUG
1500 for(int i=0;i<cPoints;i++) {
1501 dprintf(("Point %d: (%d,%d)", i, lppt[i].x, lppt[i].y));
1502 }
1503#endif
1504 GpiMove(hpsRegion, (PPOINTL)lppt);
1505 hrgn = GpiCreatePolygonRegion(hpsRegion, 1, &polygon, POLYGON_BOUNDARY | flMode);
1506 if(hrgn == RGN_ERROR) {
1507 SetLastError(ERROR_INVALID_PARAMETER_W); //todo: not right
1508 dprintf(("WARNING: CreatePolygonRgn: GpiCreatePolygonRegion failed! %x", WinGetLastError(0)));
1509 return 0;
1510 }
1511
1512 if(ObjAllocateHandle(&hrgn, hrgn, HNDL_REGION) == FALSE) {
1513 GpiDestroyRegion(hpsRegion, hrgn);
1514 SetLastError(ERROR_OUTOFMEMORY_W);
1515 return 0;
1516 }
1517 STATS_CreatePolygonRgn(hrgn, lppt, cPoints, fnPolyFillMode);
1518 SetLastError(ERROR_SUCCESS_W);
1519 return hrgn;
1520}
1521//******************************************************************************
1522//******************************************************************************
1523int WIN32API CombineRgn(HRGN hrgnDest, HRGN hrgnSrc1, HRGN hrgnSrc2, int combineMode)
1524{
1525 ULONG lComplexity;
1526 LONG mode;
1527#ifdef DEBUG_LOGGING
1528 HRGN hrgn1 = hrgnDest;
1529 HRGN hrgn2 = hrgnSrc1;
1530 HRGN hrgn3 = hrgnSrc2;
1531#endif
1532
1533 switch(combineMode) {
1534 case RGN_AND_W:
1535 mode = CRGN_AND;
1536 break;
1537 case RGN_COPY_W:
1538 mode = CRGN_COPY;
1539 break;
1540 case RGN_DIFF_W:
1541 mode = CRGN_DIFF;
1542 break;
1543 case RGN_OR_W:
1544 mode = CRGN_OR;
1545 break;
1546 case RGN_XOR_W:
1547 mode = CRGN_XOR;
1548 break;
1549 default:
1550 dprintf(("WARNING: CombineRgn %x %x %x %d; invalid mode!", hrgnDest, hrgnSrc1, hrgnSrc2, combineMode));
1551 SetLastError(ERROR_INVALID_PARAMETER_W);
1552 return ERROR_W;
1553 }
1554
1555 hrgnDest = ObjQueryHandleData(hrgnDest, HNDL_REGION);
1556 hrgnSrc1 = ObjQueryHandleData(hrgnSrc1, HNDL_REGION);
1557 hrgnSrc2 = ObjQueryHandleData(hrgnSrc2, HNDL_REGION);
1558 if(hrgnDest == HANDLE_OBJ_ERROR || hrgnSrc1 == HANDLE_OBJ_ERROR || (hrgnSrc2 == HANDLE_OBJ_ERROR && combineMode != RGN_COPY_W)) {
1559 dprintf(("WARNING: CombineRgn %x %x %x %d invalid region", hrgn1, hrgn2, hrgn3, combineMode));
1560 SetLastError(ERROR_INVALID_HANDLE_W);
1561 return ERROR_W;
1562 }
1563
1564 lComplexity = GpiCombineRegion(hpsRegion, hrgnDest, hrgnSrc1, hrgnSrc2, mode);
1565 if(lComplexity == RGN_ERROR) {
1566 dprintf(("WARNING: CombineRgn %x %x %x %d GpiCombineRegion failed (%x)", hrgn1, hrgn2, hrgn3, mode, WinGetLastError(0)));
1567 SetLastError(ERROR_INVALID_HANDLE_W);
1568 return ERROR_W;
1569 }
1570 SetLastError(ERROR_SUCCESS_W);
1571 return lComplexity;
1572}
1573//******************************************************************************
1574//******************************************************************************
1575BOOL WIN32API EqualRgn(HRGN hrgn1, HRGN hrgn2)
1576{
1577 LONG lEquality;
1578#ifdef DEBUG_LOGGING
1579 HRGN hrgnt1 = hrgn1;
1580 HRGN hrgnt2 = hrgn2;
1581#endif
1582
1583 hrgn1 = ObjQueryHandleData(hrgn1, HNDL_REGION);
1584 hrgn2 = ObjQueryHandleData(hrgn2, HNDL_REGION);
1585 if(hrgn1 == HANDLE_OBJ_ERROR || hrgn2 == HANDLE_OBJ_ERROR) {
1586 dprintf(("WARNING: EqualRgn %x %x invalid region", hrgnt1, hrgnt2));
1587 SetLastError(ERROR_INVALID_HANDLE_W);
1588 return FALSE;
1589 }
1590 lEquality = GpiEqualRegion(hpsRegion, hrgn1, hrgn2);
1591
1592 dprintf(("EqualRgn %x %x = %d", hrgnt1, hrgnt2, lEquality));
1593 SetLastError(ERROR_SUCCESS_W);
1594
1595 if(lEquality == EQRGN_EQUAL)
1596 return TRUE;
1597 else
1598 if(lEquality == EQRGN_NOTEQUAL)
1599 return FALSE;
1600 else {
1601 return FALSE;
1602 }
1603}
1604//******************************************************************************
1605//******************************************************************************
1606BOOL WIN32API SetRectRgn(HRGN hrgn, int left, int top, int right, int bottom)
1607{
1608 BOOL result = FALSE;
1609#ifdef DEBUG_LOGGING
1610 HRGN hrgn1 = hrgn;
1611#endif
1612
1613 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1614 if(hrgn == HANDLE_OBJ_ERROR) {
1615 dprintf(("WARNING: SetRectRgn %x (%d,%d)(%d,%d) invalid region handle", hrgn1, left, top, right, bottom));
1616 SetLastError(ERROR_INVALID_HANDLE_W);
1617 return 0;
1618 }
1619 RECTL rectl = { left, top, right, bottom }; //reversed y coordinates
1620 if(GpiSetRegion(hpsRegion, hrgn, 1, &rectl)) {
1621 dprintf(("SetRectRgn %x (%d,%d)(%d,%d)", hrgn1, left, top, right, bottom));
1622 return TRUE;
1623 }
1624 dprintf(("WARNING: SetRectRgn %x (%d,%d)(%d,%d) GpiSetRegion failed %x", hrgn1, left, top, right, bottom, WinGetLastError(0)));
1625 return FALSE;
1626}
1627//******************************************************************************
1628//NOTE: depends on hps inversion
1629//******************************************************************************
1630ULONG WIN32API GetRegionData( HRGN hrgn, ULONG count, PRGNDATA pData)
1631{
1632#ifdef DEBUG_LOGGING
1633 HRGN hrgn1 = hrgn;
1634#endif
1635
1636 if(!count && pData)
1637 {
1638 dprintf(("WARNING: GetRegionData %x %d %x; invalid parameter", hrgn1, count, pData));
1639 SetLastError(ERROR_INVALID_PARAMETER_W);
1640 return 0;
1641 }
1642 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1643 if(hrgn == HANDLE_OBJ_ERROR) {
1644 dprintf(("WARNING: GetRegionData %x %d %x; invalid region handle", hrgn1, count, pData));
1645 SetLastError(ERROR_INVALID_HANDLE_W);
1646 return 0;
1647 }
1648 RGNRECT rgnRect;
1649 rgnRect.ircStart = 1;
1650 rgnRect.crc = 0;
1651 rgnRect.ulDirection = RECTDIR_LFRT_TOPBOT;
1652 if(!GpiQueryRegionRects(hpsRegion, hrgn, NULL, &rgnRect, NULL))
1653 {
1654 dprintf(("WARNING: GetRegionData %x %d %x: GpiQueryRegionRects failed! (%x)", hrgn1, count, pData, WinGetLastError(0)));
1655 return 0;
1656 }
1657
1658 dprintf(("GetRegionData %x %d %x", hrgn1, count, pData));
1659
1660 ULONG bufSizeNeeded = rgnRect.crcReturned * sizeof(RECT) + sizeof (RGNDATAHEADER);
1661
1662 if(pData && (count >= (sizeof(RGNDATAHEADER) + rgnRect.crcReturned * sizeof(RECT))))
1663 {
1664 //we actually need to flip the top & bottom values, but as the layout of the PM RECTL and
1665 //Win32 RECT have exactly those two members reversed, we don't do this for the returned
1666 //rectangles (more efficient)
1667 PRECTL pRectl = (PRECTL)pData->Buffer;
1668 rgnRect.crc = rgnRect.crcReturned;
1669 if(!GpiQueryRegionRects(hpsRegion, hrgn, NULL, &rgnRect, pRectl))
1670 {
1671 dprintf(("WARNING: GetRegionData: GpiQueryRegionRects failed! (%x)", WinGetLastError(0)));
1672 return 0;
1673 }
1674
1675 RECTL boundRect;
1676 GpiQueryRegionBox(hpsRegion, hrgn, &boundRect); // no need to check rc
1677
1678 pData->rdh.dwSize = sizeof(pData->rdh);
1679 pData->rdh.iType = RDH_RECTANGLES_W; // one and only possible value
1680 pData->rdh.nCount = rgnRect.crcReturned;
1681 pData->rdh.nRgnSize = rgnRect.crcReturned * sizeof(RECT);
1682
1683 //flip top & bottom for bounding rectangle (not really necessary; but cleaner coding)
1684 LONG temp = boundRect.yTop;
1685 boundRect.yTop = boundRect.yBottom;
1686 boundRect.yBottom = temp;
1687 MapOS2ToWin32Rect(boundRect, pData->rdh.rcBound);
1688 }
1689
1690 //return size needed
1691 return bufSizeNeeded;
1692}
1693//******************************************************************************
1694//******************************************************************************
1695int WIN32API GetRgnBox( HRGN hrgn, PRECT pRect)
1696{
1697 BOOL success;
1698 LONG lComplexity;
1699#ifdef DEBUG_LOGGING
1700 HRGN hrgn1 = hrgn;
1701#endif
1702
1703 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1704 if(hrgn == HANDLE_OBJ_ERROR) {
1705 dprintf(("WARNING: GetRgnBox %x %x invalid region!", hrgn1, pRect));
1706 SetLastError(ERROR_INVALID_HANDLE_W);
1707 return ERROR_W;
1708 }
1709
1710 RECTL rectl;
1711 lComplexity = GpiQueryRegionBox(hpsRegion, hrgn, &rectl);
1712 if(lComplexity != RGN_ERROR)
1713 {
1714 //no conversion required, just flip top & bottom
1715 ULONG temp = rectl.yBottom;
1716 rectl.yBottom = rectl.yTop;
1717 rectl.yTop = temp;
1718
1719 MapOS2ToWin32Rect(rectl, *pRect);
1720 }
1721 else {
1722 lComplexity = ERROR_W;
1723 dprintf(("WARNING: GetRgnBox error in region!"));
1724 }
1725 dprintf(("GetRgnBox %x (%d,%d)(%d,%d)", hrgn1, pRect->left, pRect->top, pRect->right, pRect->bottom));
1726
1727 SetLastError(ERROR_SUCCESS_W);
1728 return lComplexity;
1729}
1730//******************************************************************************
1731//******************************************************************************
1732BOOL WIN32API InvertRgn( HDC hdc, HRGN hrgn)
1733{
1734#ifdef DEBUG_LOGGING
1735 HRGN hrgn1 = hrgn;
1736#endif
1737 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
1738
1739 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1740 if(!pHps || hrgn == HANDLE_OBJ_ERROR) {
1741 dprintf(("WARNING: InvertRgn %x %x invalid handle!", hdc, hrgn));
1742 SetLastError(ERROR_INVALID_HANDLE_W);
1743 return FALSE;
1744 }
1745 //todo metafile recording
1746
1747 dprintf(("InvertRgn %x %x", hdc, hrgn1));
1748
1749 interpretRegionAs(pHps, NULL, hrgn, AS_WORLD);
1750
1751 LONG lHits = GPI_ERROR;
1752 HRGN hrgnOld;
1753 LONG lComplexity ;
1754 RECTL boundingRect; // this gets a rectangle in world cordinates!
1755
1756 lComplexity = GpiQueryRegionBox(pHps->hps, pHps->hrgnHDC, &boundingRect);
1757 if(lComplexity != RGN_ERROR)
1758 {
1759 lComplexity = GpiSetClipRegion(pHps->hps, pHps->hrgnHDC, &hrgnOld);
1760 if(lComplexity != RGN_ERROR)
1761 {
1762 RECTL rectls[2];
1763 rectls[0] = boundingRect;
1764 rectls[1] = boundingRect;
1765 lHits = GpiBitBlt(pHps->hps, NULL, 3, (PPOINTL)rectls,
1766 ROP_DSTINVERT, 0);
1767
1768 /* Restore the old region */
1769 GpiSetClipRegion(pHps->hps, hrgnOld, &hrgnOld);
1770 }
1771 }
1772
1773 if(lHits == GPI_ERROR || lComplexity == RGN_ERROR)
1774 {
1775 dprintf(("WARNING: InvertRgn error during Gpi operation (%x) (%d,%d)", WinGetLastError(0), lHits, lComplexity));
1776 return FALSE;
1777 }
1778
1779 DIBSECTION_MARK_INVALID(hdc);
1780 return TRUE;
1781}
1782//******************************************************************************
1783//******************************************************************************
1784int WIN32API OffsetRgn( HRGN hrgn, int xOffset, int yOffset)
1785{
1786 LONG lComplexity;
1787#ifdef DEBUG_LOGGING
1788 HRGN hrgn1 = hrgn;
1789#endif
1790
1791 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1792 if(hrgn == HANDLE_OBJ_ERROR) {
1793 dprintf(("WARNING: OffsetRgn %x %d %d invalid handle!", hrgn, xOffset, yOffset));
1794 SetLastError(ERROR_INVALID_HANDLE_W);
1795 return ERROR_W;
1796 }
1797 dprintf(("OffsetRgn %x %d %d", hrgn1, xOffset, yOffset));
1798
1799 POINTL ptlOffset = {xOffset, yOffset};
1800 GpiOffsetRegion(hpsRegion, hrgn, &ptlOffset);
1801
1802 RECTL rectl[8];
1803 RGNRECT rgnRect;
1804 rgnRect.ircStart = 1;
1805 rgnRect.crc = 8;
1806 rgnRect.ulDirection = RECTDIR_LFRT_TOPBOT; // doesn't make a difference
1807 if(GpiQueryRegionRects(hpsRegion, hrgn, NULL, &rgnRect, &rectl[0]))
1808 {
1809 switch (rgnRect.crcReturned) {
1810 case 0:
1811 lComplexity = NULLREGION_W;
1812 break;
1813 case 1:
1814 lComplexity = SIMPLEREGION_W;
1815 break;
1816 default:
1817 lComplexity = COMPLEXREGION_W;
1818 break;
1819 }
1820 }
1821 else {
1822 lComplexity = ERROR_W;
1823 dprintf(("WARNING: OffsetRgn error in region! (%x)", WinGetLastError(0)));
1824 }
1825
1826 SetLastError(ERROR_SUCCESS_W);
1827
1828 return lComplexity;
1829}
1830//******************************************************************************
1831//******************************************************************************
1832BOOL WIN32API FrameRgn(HDC hdc, HRGN hrgn, HBRUSH hBrush, int width, int height)
1833{
1834 HBRUSH hbrushRestore = 0;
1835#ifdef DEBUG_LOGGING
1836 HRGN hrgn1 = hrgn;
1837#endif
1838 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
1839
1840 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1841 if(!pHps || hrgn == HANDLE_OBJ_ERROR) {
1842 dprintf(("WARNING: FrameRgn %x %x %x (%d,%d) invalid handle!", hdc, hrgn, hBrush, width, height));
1843 SetLastError(ERROR_INVALID_HANDLE_W);
1844 return FALSE;
1845 }
1846
1847 width = abs(width);
1848 height = abs(height);
1849
1850 if(pHps->lastBrushKey != (UINT)hBrush)
1851 {
1852 hbrushRestore = SelectObject(hdc, hBrush);
1853 if(!hbrushRestore)
1854 {
1855 dprintf(("WARNING: FrameRgn %x %x %x (%d,%d) invalid brush!", hdc, hrgn, hBrush, width, height));
1856 SetLastError(ERROR_INVALID_HANDLE_W);
1857 return FALSE;
1858 }
1859 }
1860 dprintf(("FrameRgn %x %x %x (%d,%d)", hdc, hrgn1, hBrush, width, height));
1861 interpretRegionAs(pHps, NULL, hrgn, AS_WORLD);
1862
1863 SIZEL thickness = { width, height };
1864 LONG lHits = GpiFrameRegion(pHps->hps, pHps->hrgnHDC, &thickness);
1865
1866 SetLastError(ERROR_SUCCESS_W);
1867
1868 // Restore the brush if necessary
1869 if(hbrushRestore)
1870 SelectObject(hdc, hbrushRestore);
1871
1872 if(lHits != GPI_ERROR)
1873 DIBSECTION_MARK_INVALID(hdc);
1874
1875 //todo metafile recording
1876 return (lHits != GPI_ERROR);
1877}
1878//******************************************************************************
1879//******************************************************************************
1880BOOL WIN32API FillRgn(HDC hdc, HRGN hrgn, HBRUSH hBrush)
1881{
1882 BOOL success;
1883 HBRUSH hbrushRestore = 0;
1884#ifdef DEBUG_LOGGING
1885 HRGN hrgn1 = hrgn;
1886#endif
1887
1888 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
1889
1890 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1891 if(!pHps || hrgn == HANDLE_OBJ_ERROR) {
1892 dprintf(("WARNING: FillRgn %x %x %x invalid handle!", hdc, hrgn1, hBrush));
1893 SetLastError(ERROR_INVALID_HANDLE_W);
1894 return FALSE;
1895 }
1896
1897 if(pHps->lastBrushKey != (UINT)hBrush)
1898 {
1899 hbrushRestore = SelectObject(hdc, hBrush);
1900 if (!hbrushRestore)
1901 {
1902 dprintf(("WARNING: FillRgn %x %x %x invalid brush!", hdc, hrgn1, hBrush));
1903 SetLastError(ERROR_INVALID_HANDLE_W);
1904 return FALSE;
1905 }
1906 }
1907 dprintf(("FillRgn %x %x %x", hdc, hrgn1, hBrush));
1908
1909 DIBSECTION_CHECK_IF_DIRTY(hdc);
1910 interpretRegionAs(pHps, NULL, hrgn, AS_WORLD);
1911
1912 dprintfRegion(pHps->hps, pHps->hrgnHDC);
1913
1914 success = GpiPaintRegion(pHps->hps, pHps->hrgnHDC);
1915
1916 //todo metafile recording
1917
1918 /* Restore the brush if necessary */
1919 if(hbrushRestore)
1920 SelectObject(hdc, hbrushRestore);
1921
1922 if(success)
1923 DIBSECTION_MARK_INVALID(hdc);
1924
1925 return(success);
1926}
1927//******************************************************************************
1928//******************************************************************************
1929BOOL WIN32API PaintRgn( HDC hdc, HRGN hrgn)
1930{
1931 return FillRgn(hdc, hrgn, (HBRUSH) GetCurrentObject(hdc, OBJ_BRUSH_W));
1932}
1933//******************************************************************************
1934//******************************************************************************
1935BOOL WIN32API PtInRegion( HRGN hrgn, int x, int y)
1936{
1937 BOOL success;
1938 LONG lInside;
1939#ifdef DEBUG_LOGGING
1940 HRGN hrgn1 = hrgn;
1941#endif
1942
1943 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1944 if(hrgn == HANDLE_OBJ_ERROR) {
1945 dprintf(("WARNING: PtInRgn %x (%d,%d) invalid region!", hrgn1, x, y));
1946 SetLastError(ERROR_INVALID_HANDLE_W);
1947 return FALSE;
1948 }
1949
1950 POINTL pointl = {x,y};
1951 lInside = GpiPtInRegion(hpsRegion, hrgn, &pointl);
1952 if(lInside == PRGN_ERROR) {
1953 success = FALSE;
1954 }
1955 else success = TRUE;
1956
1957 SetLastError(ERROR_SUCCESS_W);
1958
1959 dprintf(("PtInRgn %x (%d,%d) returned %d", hrgn1, x, y, (success && lInside == PRGN_INSIDE) ? 1 : 0));
1960
1961 if(success && lInside == PRGN_INSIDE)
1962 return TRUE;
1963 else
1964 return FALSE;
1965}
1966//******************************************************************************
1967//******************************************************************************
1968BOOL WIN32API RectInRegion( HRGN hrgn, const RECT * pRect)
1969{
1970 BOOL success;
1971 LONG lInside;
1972#ifdef DEBUG_LOGGING
1973 HRGN hrgn1 = hrgn;
1974#endif
1975
1976 if(!pRect) {
1977 dprintf(("WARNING: RectInRgn %x %x invalid parameter!", hrgn1, pRect));
1978 SetLastError(ERROR_INVALID_PARAMETER_W);
1979 return FALSE;
1980 }
1981 hrgn = ObjQueryHandleData(hrgn, HNDL_REGION);
1982 if(hrgn == HANDLE_OBJ_ERROR) {
1983 dprintf(("WARNING: RectInRgn %x %x invalid region", hrgn1, pRect));
1984 SetLastError(ERROR_INVALID_HANDLE_W);
1985 return FALSE;
1986 }
1987
1988 RECTL rectl;
1989 MapWin32ToOS2Rect(*pRect, rectl);
1990 //switch bottom & top
1991 UINT temp = rectl.yBottom;
1992 rectl.yBottom = rectl.yTop;
1993 rectl.yTop = temp;
1994
1995 lInside = GpiRectInRegion(hpsRegion, hrgn, &rectl);
1996 if(lInside == RRGN_ERROR) {
1997 success = FALSE;
1998 }
1999 else success = TRUE;
2000
2001 SetLastError(ERROR_SUCCESS_W);
2002
2003 dprintf(("RectInRgn %x %x returned %d", hrgn1, pRect, (success && (lInside == RRGN_INSIDE || lInside == RRGN_PARTIAL)) ? 1 : 0));
2004
2005 if(success && (lInside == RRGN_INSIDE || lInside == RRGN_PARTIAL))
2006 return TRUE;
2007 else
2008 return FALSE;
2009}
2010//******************************************************************************
2011//Returned region in device coordinates (undocumented behaviour)
2012//******************************************************************************
2013HRGN WIN32API PathToRegion( HDC hdc)
2014{
2015 pDCData pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
2016 if(!pHps)
2017 {
2018 dprintf(("WARNING: PathToRegion %x; invalid hdc!", hdc));
2019 SetLastError(ERROR_INVALID_HANDLE_W);
2020 return NULLHANDLE;
2021 }
2022
2023 HRGN hrgn, hrgnTemp, hrgnwin;
2024
2025 dprintf(("GDI32: PathToRegion %x", hdc));
2026
2027 hrgnTemp = GpiPathToRegion(pHps->hps, 1, (pHps->polyFillMode == ALTERNATE_W) ? FPATH_ALTERNATE : FPATH_WINDING);
2028 if(hrgnTemp == NULLHANDLE)
2029 goto error;
2030
2031 hrgnwin = CreateRectRgn(1, 1, 2, 2);
2032 hrgn = ObjQueryHandleData(hrgnwin, HNDL_REGION);
2033 if(hrgn == HANDLE_OBJ_ERROR) {
2034 dprintf(("WARNING: PathToRegion invalid region", hrgnwin));
2035 SetLastError(ERROR_INVALID_HANDLE_W);
2036 return NULLHANDLE;
2037 }
2038
2039 if(!setWinDeviceRegionFromPMDeviceRegion(hrgn, hrgnTemp, pHps, NULL))
2040 goto error;
2041
2042 GpiDestroyRegion(pHps->hps, hrgnTemp);
2043
2044 return hrgnwin;
2045
2046error:
2047 if(hrgnwin)
2048 DeleteObject(hrgnwin);
2049
2050 SetLastError(ERROR_INVALID_HANDLE_W); //todo right error
2051 return NULLHANDLE;
2052}
2053//******************************************************************************
2054//Selects the current path as a clipping region for a device context, combining
2055//any existing clipping region by using the specified mode
2056//TODO: Can be emulated with SelectClipRegion??
2057//******************************************************************************
2058BOOL WIN32API SelectClipPath(HDC hdc, int iMode)
2059{
2060 HRGN hrgn;
2061 BOOL ret = FALSE;
2062
2063 hrgn = PathToRegion(hdc);
2064 if(hrgn)
2065 {
2066 ret = ExtSelectClipRgn(hdc, hrgn, iMode) != ERROR_W;
2067 DeleteObject(hrgn);
2068 }
2069
2070 if(hrgn == NULLHANDLE || ret == FALSE) {
2071 dprintf(("GDI32: SelectClipPath FAILED %x %d", hrgn, ret));
2072 }
2073 return ret;
2074}
2075//******************************************************************************
2076//Needs wrapper as this file includes os2.h!!
2077//******************************************************************************
2078BOOL WIN32API OSLibDeleteRegion(HANDLE hRegion)
2079{
2080 if(GpiDestroyRegion(hpsRegion, hRegion) == FALSE) {
2081 dprintf(("WARNING: OSLibDeleteRegion %x; GpiDestroyRegion failed (%x)", hRegion, WinGetLastError(0)));
2082 }
2083 return (0);
2084}
2085/*****************************************************************************
2086 * Name : int GetMetaRgn
2087 * Purpose : The GetMetaRgn function retrieves the current metaregion for
2088 * the specified device context.
2089 * Parameters: HDC hdc handle of device context
2090 * HRGN hrgn handle of region
2091 * Variables :
2092 * Result : 0 / 1
2093 * Remark :
2094 * Status : UNTESTED STUB
2095 *
2096 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
2097 *****************************************************************************/
2098
2099int WIN32API GetMetaRgn( HDC hdc, HRGN hrgn)
2100{
2101 dprintf(("GDI32: GetMetaRgn(%08xh, %08xh) not implemented.\n",
2102 hdc,
2103 hrgn));
2104
2105 SetLastError(ERROR_SUCCESS_W);
2106 return (0);
2107}
2108/*****************************************************************************
2109 * Name : int SetMetaRgn
2110 * Purpose : The SetMetaRgn function intersects the current clipping region
2111 * for the specified device context with the current metaregion
2112 * and saves the combined region as the new metaregion for the
2113 * specified device context. The clipping region is reset to a null region.
2114 * Parameters: HDC hdc handle of device context
2115 * Variables :
2116 * Result : TRUE / FALSE
2117 * Remark :
2118 * Status : UNTESTED STUB
2119 *
2120 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
2121 *****************************************************************************/
2122
2123BOOL WIN32API SetMetaRgn( HDC hdc)
2124{
2125 dprintf(("GDI32: SetMetaRgn(%08xh) not implemented.\n",
2126 hdc));
2127
2128 SetLastError(ERROR_SUCCESS_W);
2129 return (NULLREGION_W);
2130}
2131//******************************************************************************
2132//******************************************************************************
Note: See TracBrowser for help on using the repository browser.