source: trunk/src/user32/dc.cpp@ 10367

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

Visible & Clip region changes

File size: 56.8 KB
Line 
1/* $Id: dc.cpp,v 1.122 2003-11-12 14:10:18 sandervl Exp $ */
2
3/*
4 * DC functions for USER32
5 *
6 * Project Odin Software License can be found in LICENSE.TXT
7 *
8 */
9
10/*****************************************************************************
11 * Includes *
12 *****************************************************************************/
13
14#include <odin.h>
15
16#define INCL_WIN
17#define INCL_GPI
18#define INCL_GREALL
19#define INCL_DEV
20#include <os2wrap.h>
21//#include <pmddi.h>
22#include <stdlib.h>
23
24#include <string.h>
25#include <win32type.h>
26#include <win32api.h>
27#include <winuser32.h>
28#include <winconst.h>
29#include <misc.h>
30#include <win32wbase.h>
31#include <math.h>
32#include <limits.h>
33#include "oslibwin.h"
34#include "oslibmsg.h"
35#include <dcdata.h>
36#include <codepage.h>
37#include <wingdi32.h>
38#include <stats.h>
39
40#define INCLUDED_BY_DC
41#include "dc.h"
42
43#define DBG_LOCALLOG DBG_dc
44#include "dbglocal.h"
45
46#ifndef DEVESC_SETPS
47 #define DEVESC_SETPS 49149L
48#endif
49
50#define FLOAT_TO_FIXED(x) ((FIXED) ((x) * 65536.0))
51#define MICRO_HPS_TO_HDC(x) ((x) & 0xFFFFFFFE)
52
53#define PMRECT_FROM_WINRECT( pmRect, winRect ) \
54{ \
55 (pmRect).xLeft = (winRect).left; \
56 (pmRect).yBottom = (winRect).bottom; \
57 (pmRect).xRight = (winRect).right; \
58 (pmRect).yTop = (winRect).top; \
59}
60
61#define WINRECT_FROM_PMRECT( winRect, pmRect ) \
62{ \
63 (winRect).left = (pmRect).xLeft; \
64 (winRect).top = (pmRect).yTop; \
65 (winRect).right = (pmRect).xRight; \
66 (winRect).bottom = (pmRect).yBottom; \
67}
68
69#define MEM_HPS_MAX 768
70
71const XFORM_W XFORMIdentity = { 1.0, 0.0, 0.0, 1.0, 0, 0 };
72const MATRIXLF matrixlfIdentity = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0};
73
74BOOL setPageXForm(Win32BaseWindow *wnd, pDCData pHps);
75BOOL changePageXForm(Win32BaseWindow *wnd, pDCData pHps, PPOINTL pValue, int x, int y, PPOINTL pPrev);
76LONG clientHeight(Win32BaseWindow *wnd, HWND hwnd, pDCData pHps);
77
78#ifdef DEBUG
79#define dprintfRegion(a,b,c) if(DbgEnabledLvl2USER32[DBG_LOCALLOG] == 1) dprintfRegion1(a,b,c)
80//#define dprintfRegion(a,b,c) dprintfRegion1(a,b,c)
81
82void dprintfRegion1(HPS hps, HWND hWnd, HRGN hrgnClip)
83{
84 RGNRECT rgnRect = {0, 16, 0, RECTDIR_LFRT_TOPBOT};
85 RECTL rectRegion[16];
86 APIRET rc;
87
88 dprintf(("dprintfRegion %x %x", hWnd, hps));
89 rc = GpiQueryRegionRects(hps, hrgnClip, NULL, &rgnRect, &rectRegion[0]);
90 for(int i=0;i<rgnRect.crcReturned;i++) {
91 dprintf(("(%d,%d)(%d,%d)", rectRegion[i].xLeft, rectRegion[i].yBottom, rectRegion[i].xRight, rectRegion[i].yTop));
92 }
93}
94#else
95#define dprintfRegion(a,b,c)
96#endif
97
98//******************************************************************************
99//******************************************************************************
100void WIN32API TestWideLine (pDCData pHps)
101{
102 const LOGPEN_W *pLogPen;
103
104 pHps->isWideLine = FALSE;
105 pLogPen = pHps->penIsExtPen ?
106 &(pHps->lastPenObject->ExtPen.logpen) :
107 &(pHps->lastPenObject->Pen.logpen);
108
109 if (((pLogPen->lopnStyle & PS_STYLE_MASK_W) != PS_NULL_W) &&
110 (pLogPen->lopnWidth.x > 0))
111 {
112 POINTL aptl[2] = { 0, 0, pLogPen->lopnWidth.x, pLogPen->lopnWidth.x };
113
114 GpiConvert(pHps->hps, CVTC_WORLD, CVTC_DEVICE, 2, aptl);
115
116 ULONG dx = abs(aptl[0].x - aptl[1].x);
117 ULONG dy = abs(aptl[0].y - aptl[1].y);
118
119 pHps->isWideLine = (dx > 1) || (dy > 1);
120 }
121}
122//******************************************************************************
123//******************************************************************************
124void WIN32API Calculate1PixelDelta(pDCData pHps)
125{
126 POINTL aptl[2] = {0, 0, 1, 1};
127
128 GpiConvert(pHps->hps, CVTC_DEVICE, CVTC_WORLD, 2, aptl);
129 pHps->worldYDeltaFor1Pixel = (int)(aptl[1].y - aptl[0].y);
130 pHps->worldXDeltaFor1Pixel = (int)(aptl[1].x - aptl[0].x); // 171182
131}
132//******************************************************************************
133//******************************************************************************
134int setMapMode(Win32BaseWindow *wnd, pDCData pHps, int mode)
135{
136 int prevMode = 0;
137 ULONG flOptions;
138
139 switch (mode)
140 {
141 case MM_HIENGLISH_W : flOptions = PU_HIENGLISH; break;
142 case MM_LOENGLISH_W : flOptions = PU_LOENGLISH; break;
143 case MM_HIMETRIC_W : flOptions = PU_HIMETRIC ; break;
144 case MM_LOMETRIC_W : flOptions = PU_LOMETRIC ; break;
145 case MM_TEXT_W : flOptions = PU_PELS ; break;
146 case MM_TWIPS_W : flOptions = PU_TWIPS ; break;
147 case MM_ANISOTROPIC_W: flOptions = PU_PELS ; break;
148 case MM_ISOTROPIC_W : flOptions = PU_LOMETRIC ; break;
149 default:
150 SetLastError(ERROR_INVALID_PARAMETER_W);
151 return FALSE;
152 }
153
154 prevMode = pHps->MapMode; /* store previous mode */
155 pHps->MapMode = mode;
156
157 if (mode == MM_TEXT_W)
158 {
159 pHps->viewportXExt =
160 pHps->viewportYExt = 1.0;
161 pHps->windowExt.cx =
162 pHps->windowExt.cy = 1;
163 }
164 else if (mode != MM_ANISOTROPIC_W)
165 {
166 RECTL rectl;
167 SIZEL sizel;
168 ULONG data[3];
169
170 data[0] = flOptions;
171 data[1] = data[2] = 0;
172
173 if (DevEscape(pHps->hdc ? pHps->hdc : pHps->hps, DEVESC_SETPS, 12, (PBYTE)data, 0, 0) == DEVESC_ERROR)
174 {
175 SetLastError(ERROR_INVALID_PARAMETER_W);
176 return 0;
177 }
178
179 GpiQueryPageViewport(pHps->hps, &rectl);
180 pHps->viewportXExt = (double)rectl.xRight;
181 pHps->viewportYExt = -(double)rectl.yTop;
182
183 GreGetPageUnits(pHps->hdc? pHps->hdc : pHps->hps, &sizel);
184 pHps->windowExt.cx = sizel.cx;
185 pHps->windowExt.cy = sizel.cy;
186
187 data[0] = PU_PELS;
188 DevEscape(pHps->hdc ? pHps->hdc : pHps->hps, DEVESC_SETPS, 12, (PBYTE)data, 0, 0);
189 }
190
191 if (((prevMode != MM_ISOTROPIC_W) && (prevMode != MM_ANISOTROPIC_W)) &&
192 ((mode == MM_ISOTROPIC_W) || (mode == MM_ANISOTROPIC_W)))
193 {
194 if (pHps->lWndXExtSave && pHps->lWndYExtSave)
195 {
196 changePageXForm (wnd, pHps, (PPOINTL)&pHps->windowExt,
197 pHps->lWndXExtSave, pHps->lWndYExtSave, NULL );
198 pHps->lWndXExtSave = pHps->lWndYExtSave = 0;
199 }
200 if (pHps->lVwpXExtSave && pHps->lVwpYExtSave)
201 {
202 changePageXForm (wnd, pHps, NULL,
203 pHps->lVwpXExtSave, pHps->lVwpYExtSave, NULL );
204 pHps->lVwpXExtSave = pHps->lVwpYExtSave = 0;
205 }
206 }
207
208 setPageXForm(wnd, pHps);
209
210 return prevMode;
211}
212//******************************************************************************
213//******************************************************************************
214BOOL setPageXForm(Win32BaseWindow *wnd, pDCData pHps)
215{
216 MATRIXLF mlf;
217 BOOL rc = TRUE;
218
219 pHps->height = clientHeight(wnd, 0, pHps) - 1;
220
221 double xScale = pHps->viewportXExt / (double)pHps->windowExt.cx;
222 double yScale = pHps->viewportYExt / (double)pHps->windowExt.cy;
223
224#if 0
225 mlf.fxM11 = FLOAT_TO_FIXED(1.0);
226 mlf.fxM12 = 0;
227 mlf.lM13 = 0;
228 mlf.fxM21 = 0;
229 mlf.fxM22 = FLOAT_TO_FIXED(1.0);
230 mlf.lM23 = 0;
231 mlf.lM31 = pHps->viewportOrg.x - (LONG)(pHps->windowOrg.x * xScale);
232 mlf.lM32 = pHps->viewportOrg.y - (LONG)(pHps->windowOrg.y * yScale);
233 mlf.lM33 = 1;
234
235//testestest
236 if(wnd && wnd->getWindowWidth() && wnd->getWindowHeight())
237 {
238 RECTL recttmp, rectviewold;
239
240 rc = GpiQueryPageViewport(pHps->hps, &rectviewold);
241 if(rc == 0) {
242 dprintf(("WARNING: GpiQueryPageViewport returned FALSE!!"));
243 }
244 recttmp.xLeft = 0;
245 recttmp.xRight = ((double)GetScreenWidth() / xScale);
246 recttmp.yBottom = 0;
247 recttmp.yTop = ((double)GetScreenHeight() / yScale);
248
249 if(recttmp.yTop != rectviewold.yTop ||
250 recttmp.xRight != rectviewold.xRight)
251 {
252 if(pHps->viewportYExt > pHps->windowExt.cy) {//scaling up means we have to invert the y values
253 recttmp.yBottom = GetScreenHeight() - recttmp.yTop;
254 recttmp.yTop = GetScreenHeight();
255 }
256 dprintf(("GpiSetPageViewport %x (%d,%d)(%d,%d) %f %f", pHps->hps, recttmp.xLeft, recttmp.yBottom, recttmp.xRight, recttmp.yTop, xScale, yScale));
257 rc = GpiSetPageViewport(pHps->hps, &recttmp);
258 if(rc == 0) {
259 dprintf(("WARNING: GpiSetPageViewport returned FALSE!!"));
260 }
261 }
262 }
263//testestest
264
265#else
266 mlf.fxM11 = FLOAT_TO_FIXED(xScale);
267 mlf.fxM12 = 0;
268 mlf.lM13 = 0;
269 mlf.fxM21 = 0;
270 mlf.fxM22 = FLOAT_TO_FIXED(yScale);
271 mlf.lM23 = 0;
272 mlf.lM31 = pHps->viewportOrg.x - (LONG)(pHps->windowOrg.x * xScale);
273 mlf.lM32 = pHps->viewportOrg.y - (LONG)(pHps->windowOrg.y * yScale);
274 mlf.lM33 = 1;
275#endif
276
277 pHps->isLeftLeft = mlf.fxM11 >= 0;
278 pHps->isTopTop = mlf.fxM22 >= 0;
279
280 BOOL bEnableYInversion = FALSE;
281 if ((mlf.fxM22 > 0) ||
282 ((pHps->graphicsMode == GM_ADVANCED_W) &&
283 ((pHps->MapMode == MM_ANISOTROPIC_W) ||
284 (pHps->MapMode == MM_ISOTROPIC_W))))
285 {
286 bEnableYInversion = TRUE;
287 }
288 else
289 {
290 bEnableYInversion = FALSE;
291 mlf.lM32 = pHps->HPStoHDCInversionHeight + pHps->height - mlf.lM32;
292 mlf.fxM22 = -mlf.fxM22;
293 }
294
295 if (!pHps->isMetaPS)
296// if ((!pHps->isMetaPS) ||
297// (pHps->pMetaFileObject && pHps->pMetaFileObject->isEnhanced()))
298 rc = GpiSetDefaultViewMatrix(pHps->hps, 8, &mlf, TRANSFORM_REPLACE);
299 if(rc == 0) {
300 dprintf(("WARNING: GpiSetDefaultViewMatrix returned FALSE!!"));
301 }
302#ifdef INVERT
303 if (bEnableYInversion)
304 GpiEnableYInversion(pHps->hps, pHps->height + pHps->HPStoHDCInversionHeight);
305 else
306 GpiEnableYInversion(pHps->hps, 0);
307#else
308 pHps->yInvert = 0;
309 if(bEnableYInversion)
310 {
311 pHps->yInvert4Enable = pHps->height + pHps->HPStoHDCInversionHeight;
312 if(pHps->isPrinter)
313 {
314 pHps->yInvert = pHps->yInvert4Enable
315 * (pHps->windowExt.cy / (double)pHps->viewportYExt);
316 } else {
317 pHps->yInvert = pHps->yInvert4Enable;
318 }
319 }
320#endif
321
322 TestWideLine(pHps);
323 Calculate1PixelDelta(pHps);
324 return rc;
325}
326//******************************************************************************
327//******************************************************************************
328BOOL changePageXForm(Win32BaseWindow *wnd, pDCData pHps, PPOINTL pValue, int x, int y, PPOINTL pPrev)
329{
330 BOOL result = FALSE;
331
332 if (pValue)
333 {
334 if (pPrev)
335 *pPrev = *pValue;
336
337 if ((pValue->x == x) && (pValue->y == y)) {
338 return TRUE;
339 }
340 pValue->x = x;
341 pValue->y = y;
342 }
343 else
344 {
345 if (pPrev)
346 {
347 pPrev->x = (int)pHps->viewportXExt;
348 pPrev->y = (int)pHps->viewportYExt;
349 }
350 pHps->viewportXExt = (double)x;
351 pHps->viewportYExt = (double)y;
352 }
353
354 if (pHps->MapMode == MM_ISOTROPIC_W)
355 {
356 double xExt = fabs(pHps->viewportXExt);
357 double yExt = fabs(pHps->viewportYExt);
358 double sf = fabs((double)pHps->windowExt.cx / pHps->windowExt.cy);
359
360 if (xExt > (yExt * sf))
361 {
362 xExt = yExt * sf;
363
364 if ((double)LONG_MAX <= xExt) return (result);
365
366 if (pHps->viewportXExt < 0.0)
367 pHps->viewportXExt = -xExt;
368 else
369 pHps->viewportXExt = xExt;
370 }
371 else
372 {
373 yExt = xExt / sf;
374
375 if ((double)LONG_MAX <= yExt) return (result);
376
377 if (pHps->viewportYExt < 0.0)
378 pHps->viewportYExt = -yExt;
379 else
380 pHps->viewportYExt = yExt;
381 }
382 }
383 result = setPageXForm(wnd, pHps);
384
385 return (result);
386}
387//******************************************************************************
388//******************************************************************************
389VOID removeClientArea(Win32BaseWindow *window, pDCData pHps)
390{
391 pHps->isClient = FALSE;
392 pHps->isClientArea = FALSE;
393}
394//******************************************************************************
395//******************************************************************************
396void selectClientArea(Win32BaseWindow *window, pDCData pHps)
397{
398 pHps->isClient = TRUE;
399 pHps->isClientArea = TRUE;
400}
401//******************************************************************************
402//******************************************************************************
403void selectClientArea(Win32BaseWindow *wnd, HDC hdc)
404{
405 pDCData pHps = (pDCData)GpiQueryDCData (wnd->getOwnDC());
406
407 if (pHps != NULLHANDLE)
408 selectClientArea(wnd, pHps);
409}
410//******************************************************************************
411//******************************************************************************
412LONG clientHeight(Win32BaseWindow *wnd, HWND hwnd, pDCData pHps)
413{
414 if ((hwnd == 0) && (pHps != 0))
415 hwnd = pHps->hwnd;
416
417 if ((hwnd != 0) || (pHps == 0))
418 {
419 if(wnd) {
420 if(pHps && !pHps->isClientArea) {
421 return (wnd->getWindowHeight());
422 }
423 else return (wnd->getClientHeight());
424 }
425 else return OSLibQueryScreenHeight();
426 }
427 else if (pHps->bitmapHandle)
428 {
429 return pHps->bitmapHeight;
430 }
431 else if (pHps->isMetaPS)
432 {
433 return 0;
434 }
435 else if (pHps->isPrinter)
436 {
437 return pHps->printPageHeight;
438 }
439 else
440 {
441 return MEM_HPS_MAX;
442 }
443}
444//******************************************************************************
445//******************************************************************************
446BOOL WIN32API changePageXForm(pDCData pHps, PPOINTL pValue, int x, int y, PPOINTL pPrev)
447{
448 Win32BaseWindow *wnd;
449
450 if(pHps->isClient) {
451 wnd = Win32BaseWindow::GetWindowFromOS2Handle(pHps->hwnd);
452 }
453 else wnd = Win32BaseWindow::GetWindowFromOS2FrameHandle(pHps->hwnd);
454 BOOL ret = changePageXForm(wnd, pHps, pValue, x, y, pPrev);
455 if(wnd) RELEASE_WNDOBJ(wnd);
456 return ret;
457}
458//******************************************************************************
459//******************************************************************************
460BOOL WIN32API setPageXForm(pDCData pHps)
461{
462 Win32BaseWindow *wnd;
463
464 if(pHps->isClient) {
465 wnd = Win32BaseWindow::GetWindowFromOS2Handle(pHps->hwnd);
466 }
467 else wnd = Win32BaseWindow::GetWindowFromOS2FrameHandle(pHps->hwnd);
468 BOOL ret = setPageXForm(wnd, pHps);
469 if(wnd) RELEASE_WNDOBJ(wnd);
470 return ret;
471}
472//******************************************************************************
473//******************************************************************************
474VOID WIN32API removeClientArea(pDCData pHps)
475{
476 Win32BaseWindow *wnd;
477
478 if(pHps->isClient) {
479 wnd = Win32BaseWindow::GetWindowFromOS2Handle(pHps->hwnd);
480 }
481 else wnd = Win32BaseWindow::GetWindowFromOS2FrameHandle(pHps->hwnd);
482 if(wnd) {
483 removeClientArea(wnd, pHps);
484 RELEASE_WNDOBJ(wnd);
485 }
486}
487//******************************************************************************
488//******************************************************************************
489VOID WIN32API selectClientArea(pDCData pHps)
490{
491 Win32BaseWindow *wnd;
492
493 if(pHps->isClient) {
494 wnd = Win32BaseWindow::GetWindowFromOS2Handle(pHps->hwnd);
495 }
496 else wnd = Win32BaseWindow::GetWindowFromOS2FrameHandle(pHps->hwnd);
497 if(wnd) {
498 selectClientArea(wnd, pHps);
499 RELEASE_WNDOBJ(wnd);
500 }
501}
502//******************************************************************************
503//******************************************************************************
504VOID WIN32API checkOrigin(pDCData pHps)
505{
506 Win32BaseWindow *wnd;
507
508 if(pHps->isClient) {
509 wnd = Win32BaseWindow::GetWindowFromOS2Handle(pHps->hwnd);
510 }
511 else wnd = Win32BaseWindow::GetWindowFromOS2FrameHandle(pHps->hwnd);
512 if(wnd) {
513 if(pHps->isClient)
514 selectClientArea(wnd, pHps);
515 RELEASE_WNDOBJ(wnd);
516 }
517}
518//******************************************************************************
519//******************************************************************************
520LONG WIN32API clientHeight(HWND hwnd, pDCData pHps)
521{
522 Win32BaseWindow *wnd;
523
524 if(!pHps) {
525 wnd = Win32BaseWindow::GetWindowFromOS2Handle(hwnd);
526 }
527 else
528 if(pHps->isClient) {
529 wnd = Win32BaseWindow::GetWindowFromOS2Handle(pHps->hwnd);
530 }
531 else wnd = Win32BaseWindow::GetWindowFromOS2FrameHandle(pHps->hwnd);
532 LONG ret = clientHeight(wnd, hwnd, pHps);
533 if(wnd) RELEASE_WNDOBJ(wnd);
534 return ret;
535}
536//******************************************************************************
537//******************************************************************************
538int WIN32API setMapModeDC(pDCData pHps, int mode)
539{
540 Win32BaseWindow *wnd;
541
542 if(pHps->isClient) {
543 wnd = Win32BaseWindow::GetWindowFromOS2Handle(pHps->hwnd);
544 }
545 else wnd = Win32BaseWindow::GetWindowFromOS2FrameHandle(pHps->hwnd);
546 int ret = setMapMode(wnd, pHps, mode);
547 if(wnd) RELEASE_WNDOBJ(wnd);
548 return ret;
549}
550//******************************************************************************
551//******************************************************************************
552BOOL isYup (pDCData pHps)
553{
554 if (((pHps->windowExt.cy < 0) && (pHps->viewportYExt > 0.0)) ||
555 ((pHps->windowExt.cy > 0) && (pHps->viewportYExt < 0.0)))
556 {
557 if ((pHps->graphicsMode == GM_COMPATIBLE_W) ||
558 ((pHps->graphicsMode == GM_ADVANCED_W) && (pHps->xform.eM22 >= 0.0)))
559 return TRUE;
560 }
561 else
562 {
563 if ((pHps->graphicsMode == GM_ADVANCED_W) && (pHps->xform.eM22 < 0.0))
564 return TRUE;
565 }
566 return FALSE;
567}
568//******************************************************************************
569//******************************************************************************
570INT revertDy (Win32BaseWindow *wnd, INT dy)
571{
572 //SvL: Hack for memory.exe (doesn't get repainted properly otherwise)
573// if (wnd->isOwnDC() && wnd->getOwnDC())
574 if (wnd->isOwnDC())
575 {
576 pDCData pHps = (pDCData)GpiQueryDCData (wnd->getOwnDC());
577
578 if (pHps != NULLHANDLE)
579 if (!isYup (pHps))
580 dy = -dy;
581 }
582 else
583 {
584 dy = -dy;
585 }
586 return (dy);
587}
588//******************************************************************************
589//******************************************************************************
590HDC sendEraseBkgnd (Win32BaseWindow *wnd)
591{
592 BOOL erased;
593 HWND hwnd;
594 HDC hdc;
595 HPS hps;
596 HRGN hrgnUpdate;
597 RECTL rectl = { 1, 1, 2, 2 };
598 pDCData pHps = NULLHANDLE;
599
600 hwnd = wnd->getOS2WindowHandle();
601 hps = WinGetPS(hwnd);
602
603 hrgnUpdate = GpiCreateRegion (hps, 1, &rectl);
604 WinQueryUpdateRegion (hwnd, hrgnUpdate);
605
606 hdc = HPSToHDC (hwnd, hps, NULL, NULL);
607
608 pHps = (pDCData)GpiQueryDCData(hps);
609 GdiSetVisRgn(pHps, hrgnUpdate);
610
611 erased = wnd->MsgEraseBackGround (hdc);
612
613 DeleteHDC (hdc);
614 WinReleasePS (hps);
615
616 return erased;
617}
618//******************************************************************************
619//******************************************************************************
620void releaseOwnDC (HDC hps)
621{
622 pDCData pHps = (pDCData)GpiQueryDCData ((HPS)hps);
623
624 dprintf2(("releaseOwnDC %x", hps));
625
626 if (pHps) {
627 if (pHps->hrgnHDC)
628 GpiDestroyRegion (pHps->hps, pHps->hrgnHDC);
629
630 GpiSetBitmap (pHps->hps, NULL);
631 O32_DeleteObject (pHps->nullBitmapHandle);
632 GpiDestroyPS(pHps->hps);
633
634 if (pHps->hdc)
635 DevCloseDC(pHps->hdc);
636 }
637}
638//******************************************************************************
639//******************************************************************************
640HDC WIN32API BeginPaint (HWND hWnd, PPAINTSTRUCT_W lpps)
641{
642 HWND hwnd = hWnd ? hWnd : HWND_DESKTOP;
643 pDCData pHps = NULLHANDLE;
644 HPS hPS_ownDC = NULLHANDLE, hpsPaint = 0;
645 RECTL rectl = {0, 0, 1, 1};
646 HRGN hrgnOldClip;
647 LONG lComplexity;
648 RECTL rectlClient;
649 RECTL rectlClip;
650 BOOL bIcon;
651
652 if(lpps == NULL) {
653 //BeginPaint does NOT change last error in this case
654 //(verified in NT4, SP6)
655 dprintf (("USER32: BeginPaint %x NULL PAINTSTRUCT pointer", hWnd));
656 return 0;
657 }
658 memset(lpps, 0, sizeof(*lpps));
659
660 Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
661 if(!wnd) {
662 dprintf (("USER32: BeginPaint %x %x: invalid window handle!!", hWnd, lpps));
663 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W); //(verified in NT4, SP6)
664 return (HDC)0;
665 }
666 bIcon = IsIconic(hwnd);
667
668 //check if the application previously didn't handle a WM_PAINT msg properly
669 wnd->checkForDirtyUpdateRegion();
670
671 HWND hwndClient = (bIcon) ? wnd->getOS2FrameWindowHandle() : wnd->getOS2WindowHandle();
672
673 if(hwnd != HWND_DESKTOP && !bIcon && wnd->isOwnDC())
674 {
675 hPS_ownDC = wnd->getOwnDC();
676 //SvL: Hack for memory.exe (doesn't get repainted properly otherwise)
677 if(hPS_ownDC) {
678 pHps = (pDCData)GpiQueryDCData(hPS_ownDC);
679 if (!pHps)
680 {
681 dprintf (("USER32: BeginPaint %x invalid parameter %x", hWnd, lpps));
682 RELEASE_WNDOBJ(wnd);
683 SetLastError(ERROR_INVALID_PARAMETER_W);
684 return (HDC)NULLHANDLE;
685 }
686 hpsPaint = hPS_ownDC;
687 }
688 }
689 if(!hpsPaint) {
690 hpsPaint = GetDCEx(hwnd, 0, (DCX_CACHE_W|DCX_USESTYLE_W | ((bIcon) ? DCX_WINDOW_W : 0)));
691 pHps = (pDCData)GpiQueryDCData(hpsPaint);
692 if (!pHps)
693 {
694 dprintf (("USER32: BeginPaint %x invalid parameter %x", hWnd, lpps));
695 RELEASE_WNDOBJ(wnd);
696 SetLastError(ERROR_INVALID_PARAMETER_W);
697 return (HDC)NULLHANDLE;
698 }
699 }
700
701 if(WinQueryUpdateRect(hwndClient, &rectl) == FALSE)
702 {
703 memset(&rectl, 0, sizeof(rectl));
704 dprintf (("USER32: WARNING: WinQueryUpdateRect failed (error or no update rectangle)!!"));
705
706 if(bIcon) {
707 //WinBeginPaint messes this up for icon windows; this isn't
708 //a good solution, but it does the job
709 //Use the entire frame window as clip region
710 rectl.xRight = wnd->getWindowWidth();
711 rectl.yTop = wnd->getWindowHeight();
712 }
713
714 //save old clip and visible regions (restored for CS_OWNDC windows in EndPaint)
715 dprintf(("Old visible region %x; old clip region %x", pHps->hrgnWinVis, pHps->hrgnWin32Clip));
716 wnd->SaveClipRegion(pHps->hrgnWin32Clip);
717 wnd->SaveVisRegion(pHps->hrgnWinVis);
718
719 //clear visible and clip regions
720 pHps->hrgnWin32Clip = NULLHANDLE;
721 pHps->hrgnWinVis = NULLHANDLE;
722
723 HRGN hrgnVis = GpiCreateRegion(pHps->hps, 1, &rectl);
724 GdiSetVisRgn(pHps, hrgnVis);
725
726 selectClientArea(wnd, pHps);
727
728 if(bIcon) {
729 lComplexity = RGN_RECT;
730 }
731 else lComplexity = RGN_NULL;
732 }
733 else {
734 rectlClip.yBottom = rectlClip.xLeft = 0;
735 rectlClip.yTop = rectlClip.xRight = 1;
736
737 //Query update region
738 HRGN hrgnVis = GpiCreateRegion(pHps->hps, 1, &rectlClip);
739 WinQueryUpdateRegion(hwndClient, hrgnVis);
740 WinValidateRegion(hwndClient, hrgnVis, FALSE);
741
742 dprintfRegion(pHps->hps, wnd->getWindowHandle(), hrgnVis);
743
744#ifdef DEBUG
745 //Note: GpiQueryClipBox returns logical points
746 lComplexity = GpiQueryClipBox(pHps->hps, &rectlClip);
747 dprintf(("ClipBox (%d): (%d,%d)(%d,%d)", lComplexity, rectlClip.xLeft, rectlClip.yBottom, rectlClip.xRight, rectlClip.yTop));
748#endif
749
750 //save old clip and visible regions (restored for CS_OWNDC windows in EndPaint)
751 dprintf(("Old visible region %x; old clip region %x", pHps->hrgnWinVis, pHps->hrgnWin32Clip));
752 wnd->SaveClipRegion(pHps->hrgnWin32Clip);
753 wnd->SaveVisRegion(pHps->hrgnWinVis);
754
755 //clear visible and clip regions
756 pHps->hrgnWin32Clip = NULLHANDLE;
757 pHps->hrgnWinVis = NULLHANDLE;
758
759 //set clip region
760 lComplexity = GdiSetVisRgn(pHps, hrgnVis);
761
762 if(lComplexity == NULLREGION_W) {
763 dprintf (("BeginPaint %x: EMPTY update rectangle (vis=%d/%d show=%d/%d", hWnd, WinIsWindowVisible(wnd->getOS2FrameWindowHandle()), WinIsWindowVisible(wnd->getOS2WindowHandle()), WinIsWindowShowing(wnd->getOS2FrameWindowHandle()), WinIsWindowShowing(wnd->getOS2WindowHandle())));
764 }
765 else dprintf (("BeginPaint %x: (vis=%d/%d show=%d/%d)", hWnd, WinIsWindowVisible(wnd->getOS2FrameWindowHandle()), WinIsWindowVisible(wnd->getOS2WindowHandle()), WinIsWindowShowing(wnd->getOS2FrameWindowHandle()), WinIsWindowShowing(wnd->getOS2WindowHandle())));
766
767 selectClientArea(wnd, pHps);
768
769#ifdef DEBUG
770 //Note: GpiQueryClipBox returns logical points
771 GpiQueryClipBox(pHps->hps, &rectlClip);
772 dprintf(("ClipBox (%d): (%d,%d)(%d,%d)", lComplexity, rectlClip.xLeft, rectlClip.yBottom, rectlClip.xRight, rectlClip.yTop));
773#endif
774 }
775
776 if(hPS_ownDC == 0)
777 setMapMode (wnd, pHps, MM_TEXT_W);
778 else
779 setPageXForm(wnd, pHps);
780
781 pHps->hdcType = TYPE_3;
782
783 HideCaret(hwnd);
784 WinShowTrackRect(wnd->getOS2WindowHandle(), FALSE);
785
786 if((wnd->needsEraseBkgnd() || wnd->hasPMUpdateRegionChanged()) && lComplexity != RGN_NULL) {
787 wnd->setEraseBkgnd(FALSE);
788 wnd->SetPMUpdateRegionChanged(FALSE);
789 lpps->fErase = (wnd->MsgEraseBackGround(pHps->hps) == 0);
790 }
791 else lpps->fErase = TRUE;
792
793 lpps->hdc = (HDC)pHps->hps;
794
795 if(lComplexity != RGN_NULL) {
796 long height = wnd->getClientHeight();
797 lpps->rcPaint.top = height - rectl.yTop;
798 lpps->rcPaint.left = rectl.xLeft;
799 lpps->rcPaint.bottom = height - rectl.yBottom;
800 lpps->rcPaint.right = rectl.xRight;
801 //BeginPaint must return logical points instead of device points.
802 //(not the same if e.g. app changes page viewport)
803 DPtoLP(lpps->hdc, (LPPOINT)&lpps->rcPaint, 2);
804 }
805 else {
806 lpps->rcPaint.bottom = lpps->rcPaint.top = 0;
807 lpps->rcPaint.right = lpps->rcPaint.left = 0;
808 }
809 RELEASE_WNDOBJ(wnd);
810
811 SetLastError(0);
812 dprintf(("USER32: BeginPaint %x -> hdc %x (%d,%d)(%d,%d)", hWnd, pHps->hps, lpps->rcPaint.left, lpps->rcPaint.top, lpps->rcPaint.right, lpps->rcPaint.bottom));
813 return (HDC)pHps->hps;
814}
815//******************************************************************************
816//******************************************************************************
817BOOL WIN32API EndPaint (HWND hWnd, const PAINTSTRUCT_W *pPaint)
818{
819 HWND hwnd = hWnd ? hWnd : HWND_DESKTOP;
820 pDCData pHps;
821
822 dprintf (("USER32: EndPaint(%x)", hwnd));
823
824 if (!pPaint || !pPaint->hdc )
825 return TRUE;
826
827 Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
828
829 if (!wnd) goto exit;
830
831 pHps = (pDCData)GpiQueryDCData((HPS)pPaint->hdc);
832 if (pHps && (pHps->hdcType == TYPE_3))
833 {
834 //restore previous visible and clip regions
835 pHps->hrgnWin32Clip = NULLHANDLE;
836 pHps->hrgnWinVis = NULLHANDLE;
837
838 GdiSetVisRgn(pHps, wnd->GetVisRegion());
839 GdiCombineVisRgnClipRgn(pHps, wnd->GetClipRegion(), RGN_AND_W);
840
841 wnd->SaveClipRegion(0);
842 wnd->SaveVisRegion(0);
843
844 pHps->hdcType = TYPE_1; //otherwise Open32's ReleaseDC fails
845 ReleaseDC(hwnd, pPaint->hdc);
846 }
847 else {
848 dprintf(("EndPaint: wrong hdc %x!!", pPaint->hdc));
849 }
850 wnd->setEraseBkgnd(TRUE);
851 ShowCaret(hwnd);
852 WinShowTrackRect(wnd->getOS2WindowHandle(), TRUE);
853
854exit:
855 if(wnd) RELEASE_WNDOBJ(wnd);
856 SetLastError(0);
857 return TRUE;
858}
859//******************************************************************************
860//******************************************************************************
861int WIN32API ReleaseDC (HWND hwnd, HDC hdc)
862{
863 BOOL isOwnDC = FALSE;
864 int rc;
865
866 HWND hwndDC = WindowFromDC(hdc);
867
868 if(hwndDC != hwnd) {
869 dprintf(("WARNING: ReleaseDC: wrong window handle specified %x -> %x", hwnd, hwndDC));
870 hwnd = hwndDC;
871 }
872
873 if (hwnd)
874 {
875 Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle (hwnd);
876 if(wnd == NULL) {
877 dprintf(("ERROR: ReleaseDC %x %x failed", hwnd, hdc));
878 return 0;
879 }
880 isOwnDC = wnd->isOwnDC() && (wnd->getOwnDC() == hdc);
881 if(!isOwnDC)
882 {
883 pDCData pHps = (pDCData)GpiQueryDCData((HPS)hdc);
884 if(pHps && pHps->psType == MICRO_CACHED) {
885 removeClientArea(wnd, pHps);
886 if(pHps->hrgnWinVis) {
887 GreDestroyRegion(pHps->hps, pHps->hrgnWinVis);
888 pHps->hrgnWinVis = 0;
889 }
890 if(pHps->hrgnWin32Clip) {
891 GreDestroyRegion(pHps->hps, pHps->hrgnWin32Clip);
892 pHps->hrgnWin32Clip = 0;
893 }
894 }
895 else {
896 dprintf(("ERROR: ReleaseDC: pHps == NULL!!"));
897 DebugInt3();
898 }
899 }
900 else {
901 dprintf2(("ReleaseDC: CS_OWNDC, not released"));
902 }
903 if(!isOwnDC && !wnd->isDesktopWindow()) {
904 //remove from list of open DCs for this window
905 wnd->removeOpenDC(hdc);
906 }
907 RELEASE_WNDOBJ(wnd);
908 }
909
910 if(isOwnDC) {
911 rc = TRUE;
912 }
913 else {
914 UnselectGDIObjects(hdc);
915 rc = O32_ReleaseDC (0, hdc);
916 STATS_ReleaseDC(hwnd, hdc);
917 }
918
919 dprintf(("ReleaseDC %x %x", hwnd, hdc));
920 return (rc);
921}
922//******************************************************************************
923// This implementation of GetDCEx supports:
924//
925// DCX_WINDOW
926// DCX_CACHE
927// DCX_EXCLUDERGN (complex regions allowed)
928// DCX_INTERSECTRGN (complex regions allowed)
929// DCX_USESTYLE
930// DCX_CLIPSIBLINGS
931// DCX_CLIPCHILDREN
932// DCX_PARENTCLIP
933//
934// Not supported:
935//
936// DCX_NORESETATTRS_W
937// DCX_INTERSECTUPDATE_W
938// DCX_LOCKWINDOWUPDATE_W
939// DCX_VALIDATE_W
940// DCX_EXCLUDEUPDATE_W
941//
942//TODO: WM_SETREDRAW affects drawingAllowed flag!!
943//******************************************************************************
944HDC WIN32API GetDCEx (HWND hwnd, HRGN hrgn, ULONG flags)
945{
946 Win32BaseWindow *wnd = NULL;
947 HWND hWindow;
948 BOOL success;
949 pDCData pHps = NULL;
950 HPS hps = NULLHANDLE;
951 BOOL drawingAllowed = TRUE;
952 BOOL isWindowOwnDC;
953 BOOL creatingOwnDC = FALSE;
954 PS_Type psType;
955
956 if(flags & (DCX_NORESETATTRS_W | DCX_INTERSECTUPDATE_W | DCX_LOCKWINDOWUPDATE_W | DCX_VALIDATE_W | DCX_EXCLUDEUPDATE_W)) {
957 dprintf(("ERROR: GetDCEx: unsupported flags %x!!", flags));
958 DebugInt3();
959 }
960
961 if(hwnd == 0) {
962 dprintf(("error: GetDCEx window %x not found", hwnd));
963 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
964 return 0;
965 }
966
967 if(hwnd)
968 {
969 wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
970 if(wnd == NULL) {
971 dprintf (("ERROR: User32: GetDCEx bad window handle %X!!!!!", hwnd));
972 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
973 return 0;
974 }
975 if(flags & DCX_WINDOW_W) {
976 hWindow = wnd->getOS2FrameWindowHandle();
977 }
978 else hWindow = wnd->getOS2WindowHandle();
979 }
980
981 isWindowOwnDC = (((hWindow == HWND_DESKTOP) ? FALSE : (wnd->isOwnDC()))
982 && !(flags & (DCX_CACHE_W|DCX_WINDOW_W)));
983
984 if(isWindowOwnDC) //own dc always for client area
985 {
986 hps = wnd->getOwnDC();
987 if (hps)
988 {
989 pDCData pHps = (pDCData)GpiQueryDCData (hps);
990 if (!pHps)
991 goto error;
992
993 selectClientArea(wnd, pHps);
994
995 //TODO: Is this always necessary??
996 setPageXForm (wnd, pHps);
997 pHps->hdcType = TYPE_1;
998
999 //TODO: intersect/exclude clip region?
1000 dprintf (("User32: GetDCEx hwnd %x (%x %x) -> wnd %x hdc %x", hwnd, hrgn, flags, wnd, hps));
1001
1002 RELEASE_WNDOBJ(wnd);
1003
1004 STATS_GetDCEx(hwnd, hps, hrgn, flags);
1005
1006 return (HDC)hps;
1007 }
1008 else creatingOwnDC = TRUE;
1009 }
1010
1011 if(isWindowOwnDC)
1012 {
1013 SIZEL sizel = {0,0};
1014 hps = GpiCreatePS (WinQueryAnchorBlock (hWindow),
1015 WinOpenWindowDC (hWindow),
1016 &sizel, PU_PELS | GPIT_MICRO | GPIA_ASSOC );
1017 psType = MICRO;
1018 // default cp is OS/2 one: set to windows default (ODIN.INI)
1019 GpiSetCp(hps, GetDisplayCodepage());
1020 }
1021 else
1022 {
1023 if (hWindow == HWND_DESKTOP) {
1024 hps = WinGetScreenPS (hWindow);
1025 }
1026 else {
1027 int clipstyle = 0;
1028 int clipwnd = HWND_TOP;
1029 if(flags & (DCX_USESTYLE_W))
1030 {
1031 int style = wnd->getStyle();
1032 if(style & WS_CLIPCHILDREN_W) {
1033 clipstyle |= PSF_CLIPCHILDREN;
1034 }
1035 if(style & WS_CLIPSIBLINGS_W) {
1036 clipstyle |= PSF_CLIPSIBLINGS;
1037 }
1038 if(wnd->fHasParentDC()) {
1039 clipstyle |= PSF_PARENTCLIP;
1040 }
1041 }
1042 if(flags & DCX_CLIPSIBLINGS_W) {
1043 clipstyle |= PSF_CLIPSIBLINGS;
1044 }
1045 if(flags & DCX_CLIPCHILDREN_W) {
1046 clipstyle |= PSF_CLIPCHILDREN;
1047 }
1048 if(flags & DCX_PARENTCLIP_W) {
1049 clipstyle |= PSF_PARENTCLIP;
1050 }
1051 //Always call WinGetClipPS; WinGetPS takes window & class style
1052 //into account
1053// if(clipstyle || !(flags & DCX_DEFAULTCLIP_W)) {
1054 dprintf2(("WinGetClipPS style %x", clipstyle));
1055 hps = WinGetClipPS(hWindow, clipwnd, clipstyle);
1056// }
1057// else hps = WinGetPS (hWindow);
1058
1059 // default cp is OS/2 one: set to windows default (ODIN.INI)
1060 GpiSetCp(hps, GetDisplayCodepage());
1061 }
1062 psType = MICRO_CACHED;
1063 }
1064
1065 if (!hps)
1066 goto error;
1067
1068 HPSToHDC (hWindow, hps, NULL, NULL);
1069 pHps = (pDCData)GpiQueryDCData (hps);
1070
1071 if(flags & DCX_WINDOW_W) {
1072 removeClientArea(wnd, pHps);
1073 }
1074 else selectClientArea(wnd, pHps);
1075
1076 setMapMode(wnd, pHps, MM_TEXT_W);
1077
1078 if ((flags & DCX_EXCLUDERGN_W) || (flags & DCX_INTERSECTRGN_W))
1079 {
1080 ULONG BytesNeeded;
1081 PRGNDATA RgnData;
1082 PRECT pr;
1083 int i;
1084 RECTL rectl;
1085
1086 if (!hrgn)
1087 goto error;
1088
1089 success = TRUE;
1090 if (flags & DCX_EXCLUDERGN_W)
1091 {//exclude specified region from visible region
1092 success = (GdiCombineVisRgn(pHps, hrgn, RGN_DIFF_W) != ERROR_W);
1093 }
1094 else //DCX_INTERSECTRGN_W
1095 {//intersect visible region with specified region
1096 success = (GdiCombineVisRgn(pHps, hrgn, RGN_AND_W) != ERROR_W);
1097 }
1098 if (!success)
1099 goto error;
1100 }
1101
1102 if (creatingOwnDC)
1103 wnd->setOwnDC ((HDC)hps);
1104
1105 pHps->psType = psType;
1106 pHps->hdcType = TYPE_1;
1107 //TODO: WM_SETREDRAW affects drawingAllowed flag!!
1108 GpiSetDrawControl (hps, DCTL_DISPLAY, drawingAllowed ? DCTL_ON : DCTL_OFF);
1109
1110 dprintf (("User32: GetDCEx hwnd %x (%x %x) -> hdc %x", hwnd, hrgn, flags, pHps->hps));
1111
1112 //add to list of open DCs for this window
1113 if(wnd->isDesktopWindow() == FALSE) {//not relevant for the desktop window
1114 wnd->addOpenDC((HDC)pHps->hps);
1115 }
1116
1117 RELEASE_WNDOBJ(wnd);
1118
1119
1120 STATS_GetDCEx(hwnd, pHps->hps, hrgn, flags);
1121 return (HDC)pHps->hps;
1122
1123error:
1124 /* Something went wrong; clean up
1125 */
1126 dprintf(("ERROR: GetDCEx hwnd %x (%x %x) FAILED!", hwnd, hrgn, flags));
1127 DebugInt3();
1128 if (pHps)
1129 {
1130 if (pHps->hps)
1131 {
1132 if(pHps->psType == MICRO_CACHED)
1133 WinReleasePS(pHps->hps);
1134 else
1135 GpiDestroyPS(pHps->hps);
1136 }
1137
1138 if (pHps->hdc) DevCloseDC(pHps->hdc);
1139 if (pHps->hrgnHDC) GpiDestroyRegion(pHps->hps, pHps->hrgnHDC);
1140
1141 O32_DeleteObject (pHps->nullBitmapHandle);
1142 }
1143 if(wnd) RELEASE_WNDOBJ(wnd);
1144 SetLastError(ERROR_INVALID_PARAMETER_W);
1145 return NULL;
1146}
1147//******************************************************************************
1148//******************************************************************************
1149HDC WIN32API GetDC (HWND hwnd)
1150{
1151 if(!hwnd)
1152 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE_W | DCX_WINDOW_W );
1153
1154 return GetDCEx (hwnd, NULL, DCX_USESTYLE_W);
1155}
1156//******************************************************************************
1157//******************************************************************************
1158HDC WIN32API GetWindowDC (HWND hwnd)
1159{
1160 if (!hwnd) hwnd = GetDesktopWindow();
1161 return GetDCEx (hwnd, NULL, DCX_WINDOW_W | DCX_USESTYLE_W);
1162}
1163//******************************************************************************
1164//Helper for RedrawWindow (RDW_ALLCHILDREN_W)
1165//******************************************************************************
1166LRESULT WIN32API RedrawChildEnumProc(HWND hwnd, LPARAM lParam)
1167{
1168 RedrawWindow(hwnd, NULL, 0, lParam);
1169 return TRUE;
1170}
1171//******************************************************************************
1172// This implementation of RedrawWindow supports
1173// RDW_ERASE
1174// RDW_NOERASE
1175// RDW_INTERNALPAINT
1176// RDW_NOINTERNALPAINT
1177// RDW_INVALIDATE
1178// RDW_VALIDATE
1179// RDW_ERASENOW
1180// RDW_UPDATENOW
1181// RDW_FRAME
1182//
1183//TODO: Works ok for RDW_FRAME??
1184// SDK docs say RDW_FRAME is only valid for RDW_INVALIDATE...
1185//******************************************************************************
1186BOOL WIN32API RedrawWindow(HWND hwnd, const RECT* pRect, HRGN hrgn, DWORD redraw)
1187{
1188 Win32BaseWindow *wnd;
1189
1190 if(pRect) {
1191 dprintf(("RedrawWindow %x (%d,%d)(%d,%d) %x %x", hwnd, pRect->left, pRect->top, pRect->right, pRect->bottom, hrgn, redraw));
1192 }
1193 else dprintf(("RedrawWindow %x %x %x %x", hwnd, pRect, hrgn, redraw));
1194
1195 if (hwnd == NULLHANDLE)
1196 {
1197 hwnd = HWND_DESKTOP;
1198 wnd = Win32BaseWindow::GetWindowFromOS2Handle(OSLIB_HWND_DESKTOP);
1199
1200 if (!wnd)
1201 {
1202 dprintf(("USER32:dc: RedrawWindow can't find desktop window %08xh\n",
1203 hwnd));
1204 SetLastError(ERROR_INVALID_PARAMETER_W);
1205 return FALSE;
1206 }
1207 }
1208 else
1209 {
1210 wnd = Win32BaseWindow::GetWindowFromHandle (hwnd);
1211
1212 if (!wnd)
1213 {
1214 dprintf(("USER32:dc: RedrawWindow can't find window %08xh\n",
1215 hwnd));
1216 SetLastError(ERROR_INVALID_PARAMETER_W);
1217 return FALSE;
1218 }
1219 if(redraw & RDW_FRAME_W) {
1220 hwnd = wnd->getOS2FrameWindowHandle();
1221 }
1222 else hwnd = wnd->getOS2WindowHandle();
1223 }
1224
1225 //check if the application previously didn't handle a WM_PAINT msg properly
1226 wnd->checkForDirtyUpdateRegion();
1227
1228 BOOL IncludeChildren = (redraw & RDW_ALLCHILDREN_W) ? TRUE : FALSE;
1229 BOOL success = TRUE;
1230 HPS hpsTemp = NULLHANDLE;
1231 HRGN hrgnTemp = NULLHANDLE;
1232 RECTL rectl;
1233
1234 if (hrgn)
1235 {
1236 ULONG BytesNeeded;
1237 PRGNDATA RgnData;
1238 PRECTL pr;
1239 int i;
1240 LONG height;
1241
1242 if(wnd) {
1243 height = (redraw & RDW_FRAME_W) ? wnd->getWindowHeight() : wnd->getClientHeight();
1244 }
1245 else height = OSLibQueryScreenHeight();
1246
1247 if (!hrgn)
1248 goto error;
1249
1250 BytesNeeded = GetRegionData (hrgn, 0, NULL);
1251 RgnData = (PRGNDATA)_alloca (BytesNeeded);
1252 if (RgnData == NULL)
1253 goto error;
1254 GetRegionData (hrgn, BytesNeeded, RgnData);
1255
1256 pr = (PRECTL)(RgnData->Buffer);
1257 for (i = RgnData->rdh.nCount; i > 0; i--, pr++) {
1258 LONG temp = pr->yTop;
1259 pr->yTop = height - pr->yBottom;
1260 pr->yBottom = height - temp;
1261 dprintf2(("RedrawWindow: region (%d,%d) (%d,%d)", pr->xLeft, pr->yBottom, pr->xRight, pr->yTop));
1262 }
1263
1264 hpsTemp = WinGetScreenPS (HWND_DESKTOP);
1265 hrgnTemp = GpiCreateRegion (hpsTemp, RgnData->rdh.nCount, (PRECTL)(RgnData->Buffer));
1266 if (!hrgnTemp) goto error;
1267 }
1268 else if (pRect)
1269 {
1270 if(wnd) {
1271 if(redraw & RDW_FRAME_W) {
1272 mapWin32ToOS2Rect(wnd->getWindowHeight(), (PRECT)pRect, (PRECTLOS2)&rectl);
1273 }
1274 else mapWin32ToOS2Rect(wnd->getClientHeight(), (PRECT)pRect, (PRECTLOS2)&rectl);
1275 }
1276 else mapWin32ToOS2Rect(OSLibQueryScreenHeight(), (PRECT)pRect, (PRECTLOS2)&rectl);
1277 }
1278
1279 if (redraw & RDW_INVALIDATE_W)
1280 {
1281 //TODO: SvL: pingpong.exe doesn't have RDW_NOERASE, but doesn't want WM_ERASEBKGND msgs
1282 if (redraw & RDW_ERASE_W) {
1283 wnd->setEraseBkgnd(TRUE);
1284 }
1285 else
1286 //Don't clear erase background flag if the window is
1287 //already (partly) invalidated
1288 if (!WinQueryUpdateRect (hwnd, NULL)) {
1289 dprintf(("RDW_INVALIDATE: no update rectangle, disable %x WM_ERASEBKGND", wnd->getWindowHandle()));
1290 wnd->setEraseBkgnd(FALSE);
1291 }
1292
1293 if (!pRect && !hrgn)
1294 success = WinInvalidateRect (hwnd, NULL, IncludeChildren);
1295 else
1296 if (hrgn) {
1297 success = WinInvalidateRegion (hwnd, hrgnTemp, IncludeChildren);
1298 if(IncludeChildren && WinQueryUpdateRect(hwnd, NULL) == FALSE) {
1299 dprintf(("WARNING: WinQueryUpdateRect %x region %x returned false even though we just invalidated part of a window!!!", hwnd, hrgnTemp));
1300 success = success = WinInvalidateRegion (hwnd, hrgnTemp, FALSE);
1301 }
1302 }
1303 else {
1304 dprintf2(("WinInvalidateRect (%d,%d)(%d,%d)", rectl.xLeft, rectl.yBottom, rectl.xRight, rectl.yTop));
1305 success = WinInvalidateRect (hwnd, &rectl, IncludeChildren);
1306//SvL: If all children are included, then WinInvalidateRect is called
1307// with fIncludeChildren=1 -> rect of hwnd isn't invalidated if child(ren)
1308// overlap(s) the specified rectangle completely (EVEN if window doesn't
1309// have WS_CLIPCHILREN!)
1310// -> example: XWing vs Tie Fighter install window
1311// WinInvalidateRect with fIncludeChildren=0 invalidates both the parent
1312// and child windows
1313//SvL: Can't always set fIncludeChildren to FALSE or else some controls in
1314// the RealPlayer setup application aren't redrawn when invalidating
1315// their parent
1316// SO: Check if IncludeChildren is set and part of the window is invalid
1317// If not -> call WinInvalidateRect with IncludeChildren=0 to force
1318// the window rectangle to be invalidated
1319//
1320 if(IncludeChildren && WinQueryUpdateRect(hwnd, NULL) == FALSE) {
1321 dprintf(("WARNING: WinQueryUpdateRect %x (%d,%d)(%d,%d) returned false even though we just invalidated part of a window!!!", hwnd, rectl.xLeft, rectl.yBottom, rectl.xRight, rectl.yTop));
1322 success = WinInvalidateRect (hwnd, &rectl, FALSE);
1323 }
1324 }
1325
1326 if (!success) goto error;
1327 }
1328 else if (redraw & RDW_VALIDATE_W)
1329 {
1330 if (redraw & RDW_NOERASE_W) {
1331 dprintf(("RDW_NOERASE: disable %x WM_ERASEBKGND", wnd->getWindowHandle()));
1332 wnd->setEraseBkgnd(FALSE);
1333 }
1334 if (WinQueryUpdateRect (hwnd, NULL))
1335 {
1336 if(!pRect && !hrgn) {
1337 success = WinValidateRect (hwnd, NULL, IncludeChildren);
1338 }
1339 else
1340 if(hrgn) {
1341 success = WinValidateRegion (hwnd, hrgnTemp, IncludeChildren);
1342 }
1343 else {
1344 success = WinValidateRect (hwnd, &rectl, IncludeChildren);
1345 }
1346 if(!success) goto error;
1347 }
1348 }
1349
1350 if(WinQueryUpdateRect(hwnd, &rectl))
1351 {
1352 dprintf2(("Update region (%d,%d)(%d,%d)", rectl.xLeft, rectl.yBottom, rectl.xRight, rectl.yTop));
1353 //TODO: Does this work if RDW_ALLCHILDREN is set??
1354 if(redraw & RDW_UPDATENOW_W)
1355 {
1356 dprintf(("RDW_UPDATENOW -> UpdateWindow"));
1357 UpdateWindow(wnd->getWindowHandle());
1358 }
1359 else
1360// if((redraw & RDW_ERASE_W) && (redraw & RDW_ERASENOW_W))
1361 if(redraw & RDW_ERASENOW_W && wnd->needsEraseBkgnd())
1362 wnd->setEraseBkgnd(sendEraseBkgnd(wnd) == 0);
1363// if(redraw & RDW_ALLCHILDREN_W) {
1364// EnumChildWindows(wnd->getWindowHandle(), RedrawChildEnumProc, redraw);
1365// }
1366 }
1367 else if((redraw & RDW_INTERNALPAINT_W) && !(redraw & RDW_INVALIDATE_W))
1368 {
1369 dprintf(("Manual redraw"));
1370 if(redraw & RDW_UPDATENOW_W) {
1371 wnd->MsgPaint(0, FALSE);
1372 }
1373 else PostMessageA(hwnd, WINWM_PAINT, 0, 0);
1374 }
1375
1376error:
1377 /* clean up */
1378 if (hrgnTemp)
1379 GpiDestroyRegion (hpsTemp, hrgnTemp);
1380
1381 if (hpsTemp)
1382 WinReleasePS (hpsTemp);
1383
1384 if (!success) {
1385 dprintf(("RedrawWindow failure!"));
1386 SetLastError(ERROR_INVALID_PARAMETER_W);
1387 }
1388 if(wnd) RELEASE_WNDOBJ(wnd);
1389 return (success);
1390}
1391//******************************************************************************
1392//******************************************************************************
1393BOOL WIN32API UpdateWindow (HWND hwnd)
1394{
1395 Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle (hwnd);
1396 RECTL rectl;
1397 BOOL rc;
1398
1399 if(!wnd) {
1400 dprintf (("ERROR: User32: UpdateWindow INVALID hwnd %x", hwnd));
1401 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
1402 return FALSE;
1403 }
1404
1405 //check if the application previously didn't handle a WM_PAINT msg properly
1406 wnd->checkForDirtyUpdateRegion();
1407
1408#ifdef DEBUG
1409 if(WinQueryUpdateRect(wnd->getOS2WindowHandle(), &rectl))
1410 {
1411 RECT rectUpdate;
1412 mapOS2ToWin32Rect(wnd->getClientHeight(), (PRECTLOS2)&rectl, &rectUpdate);
1413
1414 dprintf (("User32: UpdateWindow hwnd %x: update rectangle (%d,%d)(%d,%d)", hwnd, rectUpdate.left, rectUpdate.top, rectUpdate.right, rectUpdate.bottom));
1415 }
1416 else dprintf (("User32: UpdateWindow hwnd %x; EMPTY update rectangle (vis=%d/%d, show=%d/%d, vis parent=%d)", hwnd, WinIsWindowVisible(wnd->getOS2FrameWindowHandle()), WinIsWindowVisible(wnd->getOS2WindowHandle()), WinIsWindowShowing(wnd->getOS2FrameWindowHandle()), WinIsWindowShowing(wnd->getOS2WindowHandle()), IsWindowVisible(GetParent(hwnd))));
1417#endif
1418 //SvL: This doesn't work right (Wine uses RDW_NOCHILDREN_W -> doesn't work here)
1419 // Breaks vpbuddy
1420 //rc = RedrawWindow(hwnd, NULL, 0, RDW_UPDATENOW_W | RDW_ALLCHILDREN_W);
1421 // -> RDW_UPDATENOW causes WM_PAINT messages to be directy posted to window
1422 // handler; possibly bypassing queued WM_PAINT messages for parent window(s)
1423 // -> out of sync painting (i.e. parent paints over child)
1424
1425// if(!WinIsWindowShowing(wnd->getOS2FrameWindowHandle()) || !WinIsWindowShowing(wnd->getOS2WindowHandle())) {
1426// dprintf(("UpdateWindow: window not showing %d/%d", WinIsWindowShowing(wnd->getOS2FrameWindowHandle()), WinIsWindowShowing(wnd->getOS2WindowHandle()) ));
1427// RELEASE_WNDOBJ(wnd);
1428// return FALSE;
1429// }
1430 //Must use frame window here. If the frame window has a valid update region and we call
1431 //WinUpdateWindow for the client window, then no WM_PAINT messages will be sent.
1432 rc = WinUpdateWindow(wnd->getOS2FrameWindowHandle());
1433#ifdef DEBUG
1434 if(WinQueryUpdateRect(wnd->getOS2WindowHandle(), NULL))
1435 {
1436 //if parent has valid update region then WinUpdateWindow will still fail..
1437 //might be harmless and happen even in windows
1438 dprintf (("ERROR: User32: UpdateWindow didn't send WM_PAINT messages!!"));
1439 }
1440#endif
1441 RELEASE_WNDOBJ(wnd);
1442 return rc;
1443}
1444//******************************************************************************
1445//******************************************************************************
1446BOOL WIN32API ValidateRect( HWND hwnd, const RECT * lprc)
1447{
1448 if(lprc) {
1449 dprintf(("USER32: ValidateRect %x (%d,%d)(%d,%d)", hwnd, lprc->left, lprc->top, lprc->right, lprc->bottom));
1450 }
1451 else dprintf(("USER32: ValidateRect %x", hwnd));
1452
1453 return RedrawWindow( hwnd, lprc, 0, RDW_VALIDATE_W | RDW_NOCHILDREN_W | (hwnd==0 ? RDW_UPDATENOW_W : 0));
1454}
1455//******************************************************************************
1456//******************************************************************************
1457BOOL WIN32API ValidateRgn( HWND hwnd, HRGN hrgn)
1458{
1459 dprintf(("USER32: ValidateRgn %x %x", hwnd, hrgn));
1460 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE_W | RDW_NOCHILDREN_W | (hwnd==0 ? RDW_UPDATENOW_W : 0));
1461}
1462//******************************************************************************
1463//******************************************************************************
1464BOOL WIN32API InvalidateRect (HWND hwnd, const RECT *pRect, BOOL erase)
1465{
1466 BOOL result;
1467
1468 if(pRect) {
1469 dprintf(("InvalidateRect %x (%d,%d)(%d,%d) erase=%d", hwnd, pRect->left, pRect->top, pRect->right, pRect->bottom, erase));
1470 }
1471 else dprintf(("InvalidateRect %x NULL erase=%d", hwnd, erase));
1472#if 1
1473 result = RedrawWindow (hwnd, pRect, NULLHANDLE,
1474 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1475 (erase ? RDW_ERASE_W : 0) |
1476 (hwnd == NULLHANDLE ? RDW_UPDATENOW_W : 0));
1477#else
1478 result = RedrawWindow (hwnd, pRect, NULLHANDLE,
1479 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1480 (erase ? RDW_ERASE_W : RDW_NOERASE_W) |
1481 (hwnd == NULLHANDLE ? RDW_UPDATENOW_W : 0));
1482#endif
1483 return (result);
1484}
1485//******************************************************************************
1486//******************************************************************************
1487BOOL WIN32API InvalidateRgn (HWND hwnd, HRGN hrgn, BOOL erase)
1488{
1489 BOOL result;
1490
1491 dprintf(("InvalidateRgn %x %x erase=%d", hwnd, hrgn, erase));
1492#if 1
1493 result = RedrawWindow (hwnd, NULL, hrgn,
1494 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1495 (erase ? RDW_ERASE_W : 0) |
1496 (hwnd == NULLHANDLE ? RDW_UPDATENOW_W : 0));
1497#else
1498 result = RedrawWindow (hwnd, NULL, hrgn,
1499 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1500 (erase ? RDW_ERASE_W : RDW_NOERASE_W) |
1501 (hwnd == NULLHANDLE ? RDW_UPDATENOW_W : 0));
1502#endif
1503 return (result);
1504}
1505//******************************************************************************
1506//TODO: Change for client rectangle!!!!!
1507//******************************************************************************
1508BOOL setPMRgnIntoWinRgn (HPS hps, HRGN hrgnPM, HRGN hrgnWin, LONG height)
1509{
1510 BOOL rc;
1511 RGNRECT rgnRect;
1512 rgnRect.ircStart = 1;
1513 rgnRect.crc = 0;
1514 rgnRect.ulDirection = RECTDIR_LFRT_TOPBOT;
1515
1516 rc = GpiQueryRegionRects (hps, hrgnPM, NULL, &rgnRect, NULL);
1517
1518 if (rc && (rgnRect.crcReturned > 0))
1519 {
1520 PRECTL Rcls = new RECTL[rgnRect.crcReturned];
1521 PRECTL pRcl = Rcls;
1522
1523 if (Rcls != NULL)
1524 {
1525 rgnRect.crc = rgnRect.crcReturned;
1526 rc = GpiQueryRegionRects (hps, hrgnPM, NULL, &rgnRect, Rcls);
1527
1528 rc = SetRectRgn(hrgnWin, pRcl->xLeft, height - pRcl->yTop,
1529 pRcl->xRight, height - pRcl->yBottom);
1530
1531 if (rgnRect.crcReturned > 1)
1532 {
1533 int i;
1534 HRGN temp;
1535 temp = CreateRectRgn (0, 0, 1, 1);
1536
1537 for (i = 1, pRcl++; rc && (i < rgnRect.crcReturned); i++, pRcl++)
1538 {
1539 rc = SetRectRgn (temp, pRcl->xLeft, height - pRcl->yTop,
1540 pRcl->xRight, height - pRcl->yBottom);
1541 rc &= CombineRgn (hrgnWin, hrgnWin, temp, RGN_OR_W);
1542 }
1543 DeleteObject (temp);
1544 }
1545 delete[] Rcls;
1546 }
1547 else
1548 {
1549 rc = FALSE;
1550 }
1551 }
1552 else
1553 {
1554 rc = SetRectRgn (hrgnWin, 0, 0, 0, 0);
1555 }
1556
1557 return (rc);
1558}
1559//******************************************************************************
1560//Using WinScrollWindow to scroll child windows is better (smoother).
1561//******************************************************************************
1562INT WIN32API ScrollWindowEx(HWND hwnd, int dx, int dy, const RECT *pScroll, const RECT *pClip,
1563 HRGN hrgnUpdate, PRECT pRectUpdate, UINT scrollFlag)
1564{
1565 Win32BaseWindow *window;
1566 APIRET rc;
1567 RECTL scrollRect;
1568 RECTL clipRect;
1569 PRECTL pScrollOS2 = NULL;
1570 PRECTL pClipOS2 = NULL;
1571 ULONG scrollFlagsOS2 = 0;
1572 int orgdy = dy;
1573 int regionType = ERROR_W;
1574
1575 window = Win32BaseWindow::GetWindowFromHandle(hwnd);
1576 if(!window) {
1577 dprintf(("ScrollWindowEx, window %x not found", hwnd));
1578 return 0;
1579 }
1580
1581 dprintf(("ScrollWindowEx %x %d %d %x %x", hwnd, dx, dy, pScroll, pClip));
1582
1583 dy = revertDy (window, dy);
1584
1585 if (scrollFlag & SW_INVALIDATE_W) scrollFlagsOS2 |= SW_INVALIDATERGN;
1586 if (scrollFlag & SW_SCROLLCHILDREN_W) scrollFlagsOS2 |= SW_SCROLLCHILDREN;
1587
1588 if(pScroll) {
1589 mapWin32ToOS2Rect(window->getClientHeight(), (RECT *)pScroll, (PRECTLOS2)&scrollRect);
1590 pScrollOS2 = &scrollRect;
1591 }
1592
1593 if(pClip) {
1594 mapWin32ToOS2Rect(window->getClientHeight(), (RECT *)pClip, (PRECTLOS2)&clipRect);
1595 pClipOS2 = &clipRect;
1596 }
1597
1598 RECTL rectlUpdate;
1599 HRGN hrgn = NULLHANDLE;
1600 HPS hpsScreen = 0;
1601
1602 if(hrgnUpdate) {
1603 RECTL rectl = { 1, 1, 2, 2 };
1604
1605 hpsScreen = WinGetScreenPS (HWND_DESKTOP);
1606 hrgn = GpiCreateRegion(hpsScreen, 1, &rectl);
1607 }
1608
1609 if (scrollFlag & SW_SMOOTHSCROLL_W)
1610 {
1611 INT time = (scrollFlag >> 16) & 0xFFFF;
1612
1613 //CB: todo, scroll in several steps
1614 // is time in ms? time <-> iteration count?
1615 }
1616
1617 LONG lComplexity = WinScrollWindow(window->getOS2WindowHandle(), dx, dy,
1618 pScrollOS2, pClipOS2,
1619 hrgn, &rectlUpdate, scrollFlagsOS2);
1620 if (lComplexity == RGN_ERROR)
1621 {
1622 RELEASE_WNDOBJ(window);
1623 return ERROR_W;
1624 }
1625
1626 //Notify children that they have moved (according to the SDK docs,
1627 //they only receive a WM_MOVE message)
1628 //(PM only does this for windows with CS_MOVENOTIFY class)
1629 //TODO: Verify this (no WM_WINDOWPOSCHANGING/ED?)
1630 if (scrollFlag & SW_SCROLLCHILDREN_W)
1631 {
1632 HWND hwndChild;
1633 RECT rectChild, rc;
1634
1635 dprintf(("ScrollWindowEx: Scroll child windows"));
1636 GetClientRect(hwnd, &rc);
1637 if (pScroll) IntersectRect(&rc, &rc, pScroll);
1638
1639 hwndChild = GetWindow(hwnd, GW_CHILD_W);
1640
1641 while(hwndChild)
1642 {
1643 Win32BaseWindow *child;
1644
1645 child = Win32BaseWindow::GetWindowFromHandle(hwndChild);
1646 if(!child) {
1647 dprintf(("ERROR: ScrollWindowEx, child %x not found", hwnd));
1648 RELEASE_WNDOBJ(window);
1649 return 0;
1650 }
1651 rectChild = *child->getWindowRect();
1652 if(!pScroll || IntersectRect(&rectChild, &rectChild, &rc))
1653 {
1654 dprintf(("ScrollWindowEx: Scroll child window %x", hwndChild));
1655 child->ScrollWindow(dx, orgdy);
1656 }
1657 RELEASE_WNDOBJ(child);
1658 hwndChild = GetWindow(hwndChild, GW_HWNDNEXT_W);
1659 }
1660 dprintf(("***ScrollWindowEx: Scroll child windows DONE"));
1661 }
1662
1663 RECT winRectUpdate;
1664
1665 mapOS2ToWin32Rect(window->getClientHeight(), (PRECTLOS2)&rectlUpdate, &winRectUpdate);
1666
1667 if (pRectUpdate)
1668 *pRectUpdate = winRectUpdate;
1669
1670 if (hrgnUpdate) {
1671 rc = setPMRgnIntoWinRgn(hpsScreen, hrgn, hrgnUpdate, window->getClientHeight());
1672 GpiDestroyRegion(hpsScreen, hrgn);
1673 WinReleasePS(hpsScreen);
1674 }
1675
1676#if 0
1677 //SvL: WinScrollWindow already invalidates the area; no need to do it again
1678 //(call to invalidateRect was wrong; has to include erase flag)
1679 if ((scrollFlag & SW_INVALIDATE_W) &&
1680 ((lComplexity == RGN_RECT) || (lComplexity == RGN_COMPLEX)))
1681 {
1682 rc = RedrawWindow (hwnd, &winRectUpdate, NULLHANDLE,
1683 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1684 ((scrollFlag & SW_ERASE_W) ? RDW_ERASE_W : 0) |
1685 RDW_UPDATENOW_W);
1686
1687// rc = InvalidateRect (hwnd, &winRectUpdate, (scrollFlag & SW_ERASE_W) ? 1 : 0);
1688 if (rc == FALSE)
1689 {
1690 RELEASE_WNDOBJ(window);
1691 return (0);
1692 }
1693 }
1694#endif
1695
1696 switch (lComplexity)
1697 {
1698 case RGN_NULL:
1699 regionType = NULLREGION_W;
1700 break;
1701 case RGN_RECT:
1702 regionType = SIMPLEREGION_W;
1703 break;
1704 case RGN_COMPLEX:
1705 regionType = COMPLEXREGION_W;
1706 break;
1707 default:
1708 regionType = ERROR_W;
1709 break;
1710 }
1711
1712 RELEASE_WNDOBJ(window);
1713 return (regionType);
1714}
1715//******************************************************************************
1716//******************************************************************************
1717BOOL WIN32API ScrollWindow(HWND hwnd, int dx, int dy, const RECT *pScroll, const RECT *pClip)
1718{
1719 dprintf(("ScrollWindow %x %d %d %x %x", hwnd, dx, dy, pScroll, pClip));
1720 return (ERROR_W != ScrollWindowEx(hwnd, dx, dy, pScroll, pClip, 0, NULL,
1721 (pScroll ? 0 : SW_SCROLLCHILDREN_W) |
1722 SW_INVALIDATE_W ));
1723}
1724//******************************************************************************
1725//******************************************************************************
1726HWND WIN32API WindowFromDC(HDC hdc)
1727{
1728 pDCData pHps = (pDCData)GpiQueryDCData( (HPS)hdc );
1729
1730 dprintf2(("USER32: WindowFromDC %x", hdc));
1731 if ( pHps )
1732 return OS2ToWin32Handle(pHps->hwnd);
1733 else
1734 return 0;
1735}
1736//******************************************************************************
1737//******************************************************************************
1738
Note: See TracBrowser for help on using the repository browser.