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

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

ReleaseDC: always verify window associated with DC

File size: 56.8 KB
Line 
1/* $Id: dc.cpp,v 1.121 2003-05-15 13:12:52 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, hrgnOld, hrgnClip, hrgnCombined;
597 RECTL rectl = { 1, 1, 2, 2 };
598
599 hwnd = wnd->getOS2WindowHandle();
600 hps = WinGetPS(hwnd);
601
602 hrgnUpdate = GpiCreateRegion (hps, 1, &rectl);
603 WinQueryUpdateRegion (hwnd, hrgnUpdate);
604 hrgnClip = GpiQueryClipRegion (hps);
605
606 if (hrgnClip == NULLHANDLE)
607 {
608 GpiSetClipRegion (hps, hrgnUpdate, &hrgnOld);
609 }
610 else
611 {
612 hrgnCombined = GpiCreateRegion (hps, 1, &rectl);
613 GpiCombineRegion (hps, hrgnCombined, hrgnClip, hrgnUpdate, CRGN_AND);
614 GpiSetClipRegion (hps, hrgnCombined, &hrgnOld);
615 GpiDestroyRegion (hps, hrgnUpdate);
616 GpiDestroyRegion (hps, hrgnClip);
617 }
618 if (hrgnOld != NULLHANDLE)
619 GpiDestroyRegion (hps, hrgnOld);
620
621 hdc = HPSToHDC (hwnd, hps, NULL, NULL);
622
623 erased = wnd->MsgEraseBackGround (hdc);
624
625 DeleteHDC (hdc);
626 WinReleasePS (hps);
627
628 return erased;
629}
630//******************************************************************************
631//******************************************************************************
632void releaseOwnDC (HDC hps)
633{
634 pDCData pHps = (pDCData)GpiQueryDCData ((HPS)hps);
635
636 dprintf2(("releaseOwnDC %x", hps));
637
638 if (pHps) {
639 if (pHps->hrgnHDC)
640 GpiDestroyRegion (pHps->hps, pHps->hrgnHDC);
641
642 GpiSetBitmap (pHps->hps, NULL);
643 O32_DeleteObject (pHps->nullBitmapHandle);
644 GpiDestroyPS(pHps->hps);
645
646 if (pHps->hdc)
647 DevCloseDC(pHps->hdc);
648 }
649}
650//******************************************************************************
651//******************************************************************************
652HDC WIN32API BeginPaint (HWND hWnd, PPAINTSTRUCT_W lpps)
653{
654 HWND hwnd = hWnd ? hWnd : HWND_DESKTOP;
655 pDCData pHps = NULLHANDLE;
656 HPS hPS_ownDC = NULLHANDLE, hpsPaint = 0;
657 RECTL rectl = {0, 0, 1, 1};
658 HRGN hrgnOldClip;
659 LONG lComplexity;
660 RECTL rectlClient;
661 RECTL rectlClip;
662 BOOL bIcon;
663
664 if(lpps == NULL) {
665 //BeginPaint does NOT change last error in this case
666 //(verified in NT4, SP6)
667 dprintf (("USER32: BeginPaint %x NULL PAINTSTRUCT pointer", hWnd));
668 return 0;
669 }
670 memset(lpps, 0, sizeof(*lpps));
671
672 Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
673 if(!wnd) {
674 dprintf (("USER32: BeginPaint %x %x: invalid window handle!!", hWnd, lpps));
675 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W); //(verified in NT4, SP6)
676 return (HDC)0;
677 }
678// bIcon = (IsIconic(hwnd) && GetClassLongA(hWnd, GCL_HICON_W));
679 bIcon = IsIconic(hwnd);
680
681 //check if the application previously didn't handle a WM_PAINT msg properly
682 wnd->checkForDirtyUpdateRegion();
683
684 HWND hwndClient = (bIcon) ? wnd->getOS2FrameWindowHandle() : wnd->getOS2WindowHandle();
685
686 if(hwnd != HWND_DESKTOP && !bIcon && wnd->isOwnDC())
687 {
688 hPS_ownDC = wnd->getOwnDC();
689 //SvL: Hack for memory.exe (doesn't get repainted properly otherwise)
690 if(hPS_ownDC) {
691 pHps = (pDCData)GpiQueryDCData(hPS_ownDC);
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 hpsPaint = hPS_ownDC;
700 }
701 }
702 if(!hpsPaint) {
703 hpsPaint = GetDCEx(hwnd, 0, (DCX_CACHE_W|DCX_USESTYLE_W | ((bIcon) ? DCX_WINDOW_W : 0)));
704 pHps = (pDCData)GpiQueryDCData(hpsPaint);
705 if (!pHps)
706 {
707 dprintf (("USER32: BeginPaint %x invalid parameter %x", hWnd, lpps));
708 RELEASE_WNDOBJ(wnd);
709 SetLastError(ERROR_INVALID_PARAMETER_W);
710 return (HDC)NULLHANDLE;
711 }
712 }
713
714 if(WinQueryUpdateRect(hwndClient, &rectl) == FALSE)
715 {
716 memset(&rectl, 0, sizeof(rectl));
717 dprintf (("USER32: WARNING: WinQueryUpdateRect failed (error or no update rectangle)!!"));
718
719 if(bIcon) {
720 //WinBeginPaint messes this up for icon windows; this isn't
721 //a good solution, but it does the job
722 //Use the entire frame window as clip region
723 rectl.xRight = wnd->getWindowWidth();
724 rectl.yTop = wnd->getWindowHeight();
725 }
726
727 HRGN hrgnClip = GpiCreateRegion(pHps->hps, 1, &rectl);
728 GpiSetClipRegion(pHps->hps, hrgnClip, &hrgnOldClip);
729
730 selectClientArea(wnd, pHps);
731
732 //save old clip region (restored for CS_OWNDC windows in EndPaint)
733 wnd->SetClipRegion(hrgnOldClip);
734 if(bIcon) {
735 lComplexity = RGN_RECT;
736 }
737 else lComplexity = RGN_NULL;
738 }
739 else {
740 rectlClip.yBottom = rectlClip.xLeft = 0;
741 rectlClip.yTop = rectlClip.xRight = 1;
742
743 //Query update region
744 HRGN hrgnClip = GpiCreateRegion(pHps->hps, 1, &rectlClip);
745 WinQueryUpdateRegion(hwndClient, hrgnClip);
746 WinValidateRegion(hwndClient, hrgnClip, FALSE);
747
748 dprintfRegion(pHps->hps, wnd->getWindowHandle(), hrgnClip);
749
750#ifdef DEBUG
751 //Note: GpiQueryClipBox returns logical points
752 lComplexity = GpiQueryClipBox(pHps->hps, &rectlClip);
753 dprintf(("ClipBox (%d): (%d,%d)(%d,%d)", lComplexity, rectlClip.xLeft, rectlClip.yBottom, rectlClip.xRight, rectlClip.yTop));
754#endif
755
756 //set clip region
757 lComplexity = GpiSetClipRegion(pHps->hps, hrgnClip, &hrgnOldClip);
758
759 if(lComplexity == RGN_NULL) {
760 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())));
761 }
762 else dprintf (("BeginPaint %x: (vis=%d/%d show=%d/%d)", hWnd, WinIsWindowVisible(wnd->getOS2FrameWindowHandle()), WinIsWindowVisible(wnd->getOS2WindowHandle()), WinIsWindowShowing(wnd->getOS2FrameWindowHandle()), WinIsWindowShowing(wnd->getOS2WindowHandle())));
763
764 selectClientArea(wnd, pHps);
765
766#ifdef DEBUG
767 //Note: GpiQueryClipBox returns logical points
768 GpiQueryClipBox(pHps->hps, &rectlClip);
769 dprintf(("ClipBox (%d): (%d,%d)(%d,%d)", lComplexity, rectlClip.xLeft, rectlClip.yBottom, rectlClip.xRight, rectlClip.yTop));
770#endif
771 //save old clip region (restored for CS_OWNDC windows in EndPaint)
772 wnd->SetClipRegion(hrgnOldClip);
773 }
774
775 if(hPS_ownDC == 0)
776 setMapMode (wnd, pHps, MM_TEXT_W);
777 else
778 setPageXForm(wnd, pHps);
779
780 pHps->hdcType = TYPE_3;
781
782 HideCaret(hwnd);
783 WinShowTrackRect(wnd->getOS2WindowHandle(), FALSE);
784
785 if((wnd->needsEraseBkgnd() || wnd->IsVisibleRegionChanged()) && lComplexity != RGN_NULL) {
786 wnd->setEraseBkgnd(FALSE);
787 wnd->SetVisibleRegionChanged(FALSE);
788 lpps->fErase = (wnd->MsgEraseBackGround(pHps->hps) == 0);
789 }
790 else lpps->fErase = TRUE;
791
792 lpps->hdc = (HDC)pHps->hps;
793
794 if(lComplexity != RGN_NULL) {
795 long height = wnd->getClientHeight();
796 lpps->rcPaint.top = height - rectl.yTop;
797 lpps->rcPaint.left = rectl.xLeft;
798 lpps->rcPaint.bottom = height - rectl.yBottom;
799 lpps->rcPaint.right = rectl.xRight;
800 //BeginPaint must return logical points instead of device points.
801 //(not the same if e.g. app changes page viewport)
802 DPtoLP(lpps->hdc, (LPPOINT)&lpps->rcPaint, 2);
803 }
804 else {
805 lpps->rcPaint.bottom = lpps->rcPaint.top = 0;
806 lpps->rcPaint.right = lpps->rcPaint.left = 0;
807 }
808 RELEASE_WNDOBJ(wnd);
809
810 SetLastError(0);
811 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));
812 return (HDC)pHps->hps;
813}
814//******************************************************************************
815//******************************************************************************
816BOOL WIN32API EndPaint (HWND hWnd, const PAINTSTRUCT_W *pPaint)
817{
818 HWND hwnd = hWnd ? hWnd : HWND_DESKTOP;
819 HRGN hrgnOld;
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 GpiSetClipRegion(pHps->hps, wnd->GetClipRegion(), &hrgnOld);
835 wnd->SetClipRegion(0);
836 if(hrgnOld) {
837 GpiDestroyRegion(pHps->hps, hrgnOld);
838 }
839 pHps->hdcType = TYPE_1; //otherwise Open32's ReleaseDC fails
840 ReleaseDC(hwnd, pPaint->hdc);
841 }
842 else {
843 dprintf(("EndPaint: wrong hdc %x!!", pPaint->hdc));
844 }
845 wnd->setEraseBkgnd(TRUE);
846 ShowCaret(hwnd);
847 WinShowTrackRect(wnd->getOS2WindowHandle(), TRUE);
848
849exit:
850 if(wnd) RELEASE_WNDOBJ(wnd);
851 SetLastError(0);
852 return TRUE;
853}
854//******************************************************************************
855//******************************************************************************
856int WIN32API ReleaseDC (HWND hwnd, HDC hdc)
857{
858 BOOL isOwnDC = FALSE;
859 int rc;
860
861 HWND hwndDC = WindowFromDC(hdc);
862
863 if(hwndDC != hwnd) {
864 dprintf(("WARNING: ReleaseDC: wrong window handle specified %x -> %x", hwnd, hwndDC));
865 hwnd = hwndDC;
866 }
867
868 if (hwnd)
869 {
870 Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle (hwnd);
871 if(wnd == NULL) {
872 dprintf(("ERROR: ReleaseDC %x %x failed", hwnd, hdc));
873 return 0;
874 }
875 isOwnDC = wnd->isOwnDC() && (wnd->getOwnDC() == hdc);
876 if(!isOwnDC)
877 {
878 pDCData pHps = (pDCData)GpiQueryDCData((HPS)hdc);
879 if(pHps && pHps->psType == MICRO_CACHED) {
880 removeClientArea(wnd, pHps);
881 if(pHps->hrgnVis) {
882 GreDestroyRegion(pHps->hps, pHps->hrgnVis);
883 pHps->hrgnVis = 0;
884 }
885 }
886 else {
887 dprintf(("ERROR: ReleaseDC: pHps == NULL!!"));
888 DebugInt3();
889 }
890 }
891 else {
892 dprintf2(("ReleaseDC: CS_OWNDC, not released"));
893 }
894 if(!isOwnDC && !wnd->isDesktopWindow()) {
895 //remove from list of open DCs for this window
896 wnd->removeOpenDC(hdc);
897 }
898 RELEASE_WNDOBJ(wnd);
899 }
900
901 if(isOwnDC) {
902 rc = TRUE;
903 }
904 else {
905 UnselectGDIObjects(hdc);
906 rc = O32_ReleaseDC (0, hdc);
907 STATS_ReleaseDC(hwnd, hdc);
908 }
909
910 dprintf(("ReleaseDC %x %x", hwnd, hdc));
911 return (rc);
912}
913//******************************************************************************
914// This implementation of GetDCEx supports
915// DCX_WINDOW
916// DCX_CACHE
917// DCX_EXCLUDERGN (complex regions allowed)
918// DCX_INTERSECTRGN (complex regions allowed)
919// DCX_USESTYLE
920// DCX_CLIPSIBLINGS
921// DCX_CLIPCHILDREN
922// DCX_PARENTCLIP
923//
924//TODO: WM_SETREDRAW affects drawingAllowed flag!!
925//******************************************************************************
926HDC WIN32API GetDCEx (HWND hwnd, HRGN hrgn, ULONG flags)
927{
928 Win32BaseWindow *wnd = NULL;
929 HWND hWindow;
930 BOOL success;
931 pDCData pHps = NULL;
932 HPS hps = NULLHANDLE;
933 BOOL drawingAllowed = TRUE;
934 BOOL isWindowOwnDC;
935 BOOL creatingOwnDC = FALSE;
936 PS_Type psType;
937
938 if(hwnd == 0) {
939 dprintf(("error: GetDCEx window %x not found", hwnd));
940 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
941 return 0;
942 }
943
944 if(hwnd)
945 {
946 wnd = Win32BaseWindow::GetWindowFromHandle(hwnd);
947 if(wnd == NULL) {
948 dprintf (("ERROR: User32: GetDCEx bad window handle %X!!!!!", hwnd));
949 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
950 return 0;
951 }
952 if(flags & DCX_WINDOW_W) {
953 hWindow = wnd->getOS2FrameWindowHandle();
954 }
955 else hWindow = wnd->getOS2WindowHandle();
956 }
957
958 isWindowOwnDC = (((hWindow == HWND_DESKTOP) ? FALSE : (wnd->isOwnDC()))
959 && !(flags & (DCX_CACHE_W|DCX_WINDOW_W)));
960
961 if(isWindowOwnDC) //own dc always for client area
962 {
963 hps = wnd->getOwnDC();
964 if (hps)
965 {
966 pDCData pHps = (pDCData)GpiQueryDCData (hps);
967 if (!pHps)
968 goto error;
969
970 selectClientArea(wnd, pHps);
971
972 //TODO: Is this always necessary??
973 setPageXForm (wnd, pHps);
974 pHps->hdcType = TYPE_1;
975
976 //TODO: intersect/exclude clip region?
977 dprintf (("User32: GetDCEx hwnd %x (%x %x) -> wnd %x hdc %x", hwnd, hrgn, flags, wnd, hps));
978
979 RELEASE_WNDOBJ(wnd);
980
981 STATS_GetDCEx(hwnd, hps, hrgn, flags);
982
983 return (HDC)hps;
984 }
985 else creatingOwnDC = TRUE;
986 }
987
988 if(isWindowOwnDC)
989 {
990 SIZEL sizel = {0,0};
991 hps = GpiCreatePS (WinQueryAnchorBlock (hWindow),
992 WinOpenWindowDC (hWindow),
993 &sizel, PU_PELS | GPIT_MICRO | GPIA_ASSOC );
994 psType = MICRO;
995 // default cp is OS/2 one: set to windows default (ODIN.INI)
996 GpiSetCp(hps, GetDisplayCodepage());
997 }
998 else
999 {
1000 if (hWindow == HWND_DESKTOP) {
1001 hps = WinGetScreenPS (hWindow);
1002 }
1003 else {
1004 int clipstyle = 0;
1005 int clipwnd = HWND_TOP;
1006 if(flags & (DCX_USESTYLE_W))
1007 {
1008 int style = wnd->getStyle();
1009 if(style & WS_CLIPCHILDREN_W) {
1010 clipstyle |= PSF_CLIPCHILDREN;
1011 }
1012 if(style & WS_CLIPSIBLINGS_W) {
1013 clipstyle |= PSF_CLIPSIBLINGS;
1014 }
1015 if(wnd->fHasParentDC()) {
1016 clipstyle |= PSF_PARENTCLIP;
1017 }
1018 }
1019 if(flags & DCX_CLIPSIBLINGS_W) {
1020 clipstyle |= PSF_CLIPSIBLINGS;
1021 }
1022 if(flags & DCX_CLIPCHILDREN_W) {
1023 clipstyle |= PSF_CLIPCHILDREN;
1024 }
1025 if(flags & DCX_PARENTCLIP_W) {
1026 clipstyle |= PSF_PARENTCLIP;
1027 }
1028 //Always call WinGetClipPS; WinGetPS takes window & class style
1029 //into account
1030// if(clipstyle || !(flags & DCX_DEFAULTCLIP_W)) {
1031 dprintf2(("WinGetClipPS style %x", clipstyle));
1032 hps = WinGetClipPS(hWindow, clipwnd, clipstyle);
1033// }
1034// else hps = WinGetPS (hWindow);
1035
1036 // default cp is OS/2 one: set to windows default (ODIN.INI)
1037 GpiSetCp(hps, GetDisplayCodepage());
1038 }
1039 psType = MICRO_CACHED;
1040 }
1041
1042 if (!hps)
1043 goto error;
1044
1045 HPSToHDC (hWindow, hps, NULL, NULL);
1046 pHps = (pDCData)GpiQueryDCData (hps);
1047
1048 if(flags & DCX_WINDOW_W) {
1049 removeClientArea(wnd, pHps);
1050 }
1051 else selectClientArea(wnd, pHps);
1052
1053 setMapMode(wnd, pHps, MM_TEXT_W);
1054
1055 if ((flags & DCX_EXCLUDERGN_W) || (flags & DCX_INTERSECTRGN_W))
1056 {
1057 ULONG BytesNeeded;
1058 PRGNDATA RgnData;
1059 PRECT pr;
1060 int i;
1061 RECTL rectl;
1062
1063 if (!hrgn)
1064 goto error;
1065
1066 success = TRUE;
1067 if (flags & DCX_EXCLUDERGN_W)
1068 {
1069#if 0 //CB: todo
1070 long height;
1071
1072 BytesNeeded = GetRegionData (hrgn, 0, NULL);
1073 RgnData = (PRGNDATA_W)_alloca (BytesNeeded);
1074 if (RgnData == NULL)
1075 goto error;
1076 GetRegionData (hrgn, BytesNeeded, RgnData);
1077
1078 i = RgnData->rdh.nCount;
1079 pr = (PRECT)(RgnData->Buffer);
1080
1081 if (flags & DCX_WINDOW_W)
1082 height = wnd->getWindowHeight();
1083 else height = wnd->getClientHeight();
1084
1085 for (; (i > 0) && success; i--, pr++) {
1086 LONG y = pr->yBottom;
1087
1088 pr->yBottom = height - pr->yTop;
1089 pr->yTop = height - y;
1090 success &= GpiExcludeClipRectangle (pHps->hps, pr);
1091 }
1092#endif
1093 }
1094 else //DCX_INTERSECTRGN_W
1095 {
1096 //SvL: I'm getting paint problems when clipping a dc created in GetDCEx
1097 // with a region that covers the entire window (RealPlayer 7 Update 1)
1098 // Using SelectClipRgn here doesn't make any difference.
1099 if(ExtSelectClipRgn(pHps->hps, hrgn, RGN_AND_W) == ERROR_W) {
1100 dprintf(("ExtSelectClipRgn failed!!"));
1101 }
1102 }
1103 if (!success)
1104 goto error;
1105 }
1106
1107 if (creatingOwnDC)
1108 wnd->setOwnDC ((HDC)hps);
1109
1110 pHps->psType = psType;
1111 pHps->hdcType = TYPE_1;
1112 //TODO: WM_SETREDRAW affects drawingAllowed flag!!
1113 GpiSetDrawControl (hps, DCTL_DISPLAY, drawingAllowed ? DCTL_ON : DCTL_OFF);
1114
1115 dprintf (("User32: GetDCEx hwnd %x (%x %x) -> hdc %x", hwnd, hrgn, flags, pHps->hps));
1116
1117 //add to list of open DCs for this window
1118 if(wnd->isDesktopWindow() == FALSE) {//not relevant for the desktop window
1119 wnd->addOpenDC((HDC)pHps->hps);
1120 }
1121
1122 RELEASE_WNDOBJ(wnd);
1123
1124
1125 STATS_GetDCEx(hwnd, pHps->hps, hrgn, flags);
1126 return (HDC)pHps->hps;
1127
1128error:
1129 /* Something went wrong; clean up
1130 */
1131 dprintf(("ERROR: GetDCEx hwnd %x (%x %x) FAILED!", hwnd, hrgn, flags));
1132 DebugInt3();
1133 if (pHps)
1134 {
1135 if (pHps->hps)
1136 {
1137 if(pHps->psType == MICRO_CACHED)
1138 WinReleasePS(pHps->hps);
1139 else
1140 GpiDestroyPS(pHps->hps);
1141 }
1142
1143 if (pHps->hdc) DevCloseDC(pHps->hdc);
1144 if (pHps->hrgnHDC) GpiDestroyRegion(pHps->hps, pHps->hrgnHDC);
1145
1146 O32_DeleteObject (pHps->nullBitmapHandle);
1147 }
1148 if(wnd) RELEASE_WNDOBJ(wnd);
1149 SetLastError(ERROR_INVALID_PARAMETER_W);
1150 return NULL;
1151}
1152//******************************************************************************
1153//******************************************************************************
1154HDC WIN32API GetDC (HWND hwnd)
1155{
1156 if(!hwnd)
1157 return GetDCEx( GetDesktopWindow(), 0, DCX_CACHE_W | DCX_WINDOW_W );
1158
1159 return GetDCEx (hwnd, NULL, DCX_USESTYLE_W);
1160}
1161//******************************************************************************
1162//******************************************************************************
1163HDC WIN32API GetWindowDC (HWND hwnd)
1164{
1165 if (!hwnd) hwnd = GetDesktopWindow();
1166 return GetDCEx (hwnd, NULL, DCX_WINDOW_W | DCX_USESTYLE_W);
1167}
1168//******************************************************************************
1169//Helper for RedrawWindow (RDW_ALLCHILDREN_W)
1170//******************************************************************************
1171LRESULT WIN32API RedrawChildEnumProc(HWND hwnd, LPARAM lParam)
1172{
1173 RedrawWindow(hwnd, NULL, 0, lParam);
1174 return TRUE;
1175}
1176//******************************************************************************
1177// This implementation of RedrawWindow supports
1178// RDW_ERASE
1179// RDW_NOERASE
1180// RDW_INTERNALPAINT
1181// RDW_NOINTERNALPAINT
1182// RDW_INVALIDATE
1183// RDW_VALIDATE
1184// RDW_ERASENOW
1185// RDW_UPDATENOW
1186// RDW_FRAME
1187//
1188//TODO: Works ok for RDW_FRAME??
1189// SDK docs say RDW_FRAME is only valid for RDW_INVALIDATE...
1190//******************************************************************************
1191BOOL WIN32API RedrawWindow(HWND hwnd, const RECT* pRect, HRGN hrgn, DWORD redraw)
1192{
1193 Win32BaseWindow *wnd;
1194
1195 if(pRect) {
1196 dprintf(("RedrawWindow %x (%d,%d)(%d,%d) %x %x", hwnd, pRect->left, pRect->top, pRect->right, pRect->bottom, hrgn, redraw));
1197 }
1198 else dprintf(("RedrawWindow %x %x %x %x", hwnd, pRect, hrgn, redraw));
1199
1200 if (hwnd == NULLHANDLE)
1201 {
1202 hwnd = HWND_DESKTOP;
1203 wnd = Win32BaseWindow::GetWindowFromOS2Handle(OSLIB_HWND_DESKTOP);
1204
1205 if (!wnd)
1206 {
1207 dprintf(("USER32:dc: RedrawWindow can't find desktop window %08xh\n",
1208 hwnd));
1209 SetLastError(ERROR_INVALID_PARAMETER_W);
1210 return FALSE;
1211 }
1212 }
1213 else
1214 {
1215 wnd = Win32BaseWindow::GetWindowFromHandle (hwnd);
1216
1217 if (!wnd)
1218 {
1219 dprintf(("USER32:dc: RedrawWindow can't find window %08xh\n",
1220 hwnd));
1221 SetLastError(ERROR_INVALID_PARAMETER_W);
1222 return FALSE;
1223 }
1224 if(redraw & RDW_FRAME_W) {
1225 hwnd = wnd->getOS2FrameWindowHandle();
1226 }
1227 else hwnd = wnd->getOS2WindowHandle();
1228 }
1229
1230 //check if the application previously didn't handle a WM_PAINT msg properly
1231 wnd->checkForDirtyUpdateRegion();
1232
1233 BOOL IncludeChildren = (redraw & RDW_ALLCHILDREN_W) ? TRUE : FALSE;
1234 BOOL success = TRUE;
1235 HPS hpsTemp = NULLHANDLE;
1236 HRGN hrgnTemp = NULLHANDLE;
1237 RECTL rectl;
1238
1239 if (hrgn)
1240 {
1241 ULONG BytesNeeded;
1242 PRGNDATA RgnData;
1243 PRECTL pr;
1244 int i;
1245 LONG height;
1246
1247 if(wnd) {
1248 height = (redraw & RDW_FRAME_W) ? wnd->getWindowHeight() : wnd->getClientHeight();
1249 }
1250 else height = OSLibQueryScreenHeight();
1251
1252 if (!hrgn)
1253 goto error;
1254
1255 BytesNeeded = GetRegionData (hrgn, 0, NULL);
1256 RgnData = (PRGNDATA)_alloca (BytesNeeded);
1257 if (RgnData == NULL)
1258 goto error;
1259 GetRegionData (hrgn, BytesNeeded, RgnData);
1260
1261 pr = (PRECTL)(RgnData->Buffer);
1262 for (i = RgnData->rdh.nCount; i > 0; i--, pr++) {
1263 LONG temp = pr->yTop;
1264 pr->yTop = height - pr->yBottom;
1265 pr->yBottom = height - temp;
1266 dprintf2(("RedrawWindow: region (%d,%d) (%d,%d)", pr->xLeft, pr->yBottom, pr->xRight, pr->yTop));
1267 }
1268
1269 hpsTemp = WinGetScreenPS (HWND_DESKTOP);
1270 hrgnTemp = GpiCreateRegion (hpsTemp, RgnData->rdh.nCount, (PRECTL)(RgnData->Buffer));
1271 if (!hrgnTemp) goto error;
1272 }
1273 else if (pRect)
1274 {
1275 if(wnd) {
1276 if(redraw & RDW_FRAME_W) {
1277 mapWin32ToOS2Rect(wnd->getWindowHeight(), (PRECT)pRect, (PRECTLOS2)&rectl);
1278 }
1279 else mapWin32ToOS2Rect(wnd->getClientHeight(), (PRECT)pRect, (PRECTLOS2)&rectl);
1280 }
1281 else mapWin32ToOS2Rect(OSLibQueryScreenHeight(), (PRECT)pRect, (PRECTLOS2)&rectl);
1282 }
1283
1284 if (redraw & RDW_INVALIDATE_W)
1285 {
1286 //TODO: SvL: pingpong.exe doesn't have RDW_NOERASE, but doesn't want WM_ERASEBKGND msgs
1287 if (redraw & RDW_ERASE_W) {
1288 wnd->setEraseBkgnd(TRUE);
1289 }
1290 else
1291 //Don't clear erase background flag if the window is
1292 //already (partly) invalidated
1293 if (!WinQueryUpdateRect (hwnd, NULL)) {
1294 wnd->setEraseBkgnd(FALSE);
1295 }
1296
1297 if (!pRect && !hrgn)
1298 success = WinInvalidateRect (hwnd, NULL, IncludeChildren);
1299 else
1300 if (hrgn) {
1301 success = WinInvalidateRegion (hwnd, hrgnTemp, IncludeChildren);
1302 if(IncludeChildren && WinQueryUpdateRect(hwnd, NULL) == FALSE) {
1303 dprintf(("WARNING: WinQueryUpdateRect %x region %x returned false even though we just invalidated part of a window!!!", hwnd, hrgnTemp));
1304 success = success = WinInvalidateRegion (hwnd, hrgnTemp, FALSE);
1305 }
1306 }
1307 else {
1308 dprintf2(("WinInvalidateRect (%d,%d)(%d,%d)", rectl.xLeft, rectl.yBottom, rectl.xRight, rectl.yTop));
1309 success = WinInvalidateRect (hwnd, &rectl, IncludeChildren);
1310//SvL: If all children are included, then WinInvalidateRect is called
1311// with fIncludeChildren=1 -> rect of hwnd isn't invalidated if child(ren)
1312// overlap(s) the specified rectangle completely (EVEN if window doesn't
1313// have WS_CLIPCHILREN!)
1314// -> example: XWing vs Tie Fighter install window
1315// WinInvalidateRect with fIncludeChildren=0 invalidates both the parent
1316// and child windows
1317//SvL: Can't always set fIncludeChildren to FALSE or else some controls in
1318// the RealPlayer setup application aren't redrawn when invalidating
1319// their parent
1320// SO: Check if IncludeChildren is set and part of the window is invalid
1321// If not -> call WinInvalidateRect with IncludeChildren=0 to force
1322// the window rectangle to be invalidated
1323//
1324 if(IncludeChildren && WinQueryUpdateRect(hwnd, NULL) == FALSE) {
1325 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));
1326 success = WinInvalidateRect (hwnd, &rectl, FALSE);
1327 }
1328 }
1329
1330 if (!success) goto error;
1331 }
1332 else if (redraw & RDW_VALIDATE_W)
1333 {
1334 if (redraw & RDW_NOERASE_W)
1335 wnd->setEraseBkgnd(FALSE);
1336
1337 if (WinQueryUpdateRect (hwnd, NULL))
1338 {
1339 if(!pRect && !hrgn) {
1340 success = WinValidateRect (hwnd, NULL, IncludeChildren);
1341 }
1342 else
1343 if(hrgn) {
1344 success = WinValidateRegion (hwnd, hrgnTemp, IncludeChildren);
1345 }
1346 else {
1347 success = WinValidateRect (hwnd, &rectl, IncludeChildren);
1348 }
1349 if(!success) goto error;
1350 }
1351 }
1352
1353 if(WinQueryUpdateRect(hwnd, &rectl))
1354 {
1355 dprintf2(("Update region (%d,%d)(%d,%d)", rectl.xLeft, rectl.yBottom, rectl.xRight, rectl.yTop));
1356 //TODO: Does this work if RDW_ALLCHILDREN is set??
1357 if(redraw & RDW_UPDATENOW_W)
1358 {
1359 dprintf(("RDW_UPDATENOW -> UpdateWindow"));
1360 UpdateWindow(wnd->getWindowHandle());
1361 }
1362 else
1363// if((redraw & RDW_ERASE_W) && (redraw & RDW_ERASENOW_W))
1364 if(redraw & RDW_ERASENOW_W && wnd->needsEraseBkgnd())
1365 wnd->setEraseBkgnd(sendEraseBkgnd(wnd) == 0);
1366// if(redraw & RDW_ALLCHILDREN_W) {
1367// EnumChildWindows(wnd->getWindowHandle(), RedrawChildEnumProc, redraw);
1368// }
1369 }
1370 else if((redraw & RDW_INTERNALPAINT_W) && !(redraw & RDW_INVALIDATE_W))
1371 {
1372 dprintf(("Manual redraw"));
1373 if(redraw & RDW_UPDATENOW_W) {
1374 wnd->MsgPaint(0, FALSE);
1375 }
1376 else PostMessageA(hwnd, WINWM_PAINT, 0, 0);
1377 }
1378
1379error:
1380 /* clean up */
1381 if (hrgnTemp)
1382 GpiDestroyRegion (hpsTemp, hrgnTemp);
1383
1384 if (hpsTemp)
1385 WinReleasePS (hpsTemp);
1386
1387 if (!success) {
1388 dprintf(("RedrawWindow failure!"));
1389 SetLastError(ERROR_INVALID_PARAMETER_W);
1390 }
1391 if(wnd) RELEASE_WNDOBJ(wnd);
1392 return (success);
1393}
1394//******************************************************************************
1395//******************************************************************************
1396BOOL WIN32API UpdateWindow (HWND hwnd)
1397{
1398 Win32BaseWindow *wnd = Win32BaseWindow::GetWindowFromHandle (hwnd);
1399 RECTL rectl;
1400 BOOL rc;
1401
1402 if(!wnd) {
1403 dprintf (("ERROR: User32: UpdateWindow INVALID hwnd %x", hwnd));
1404 SetLastError(ERROR_INVALID_WINDOW_HANDLE_W);
1405 return FALSE;
1406 }
1407
1408 //check if the application previously didn't handle a WM_PAINT msg properly
1409 wnd->checkForDirtyUpdateRegion();
1410
1411#ifdef DEBUG
1412 if(WinQueryUpdateRect(wnd->getOS2WindowHandle(), &rectl))
1413 {
1414 RECT rectUpdate;
1415 mapOS2ToWin32Rect(wnd->getClientHeight(), (PRECTLOS2)&rectl, &rectUpdate);
1416
1417 dprintf (("User32: UpdateWindow hwnd %x: update rectangle (%d,%d)(%d,%d)", hwnd, rectUpdate.left, rectUpdate.top, rectUpdate.right, rectUpdate.bottom));
1418 }
1419 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))));
1420#endif
1421 //SvL: This doesn't work right (Wine uses RDW_NOCHILDREN_W -> doesn't work here)
1422 // Breaks vpbuddy
1423 //rc = RedrawWindow(hwnd, NULL, 0, RDW_UPDATENOW_W | RDW_ALLCHILDREN_W);
1424 // -> RDW_UPDATENOW causes WM_PAINT messages to be directy posted to window
1425 // handler; possibly bypassing queued WM_PAINT messages for parent window(s)
1426 // -> out of sync painting (i.e. parent paints over child)
1427
1428// if(!WinIsWindowShowing(wnd->getOS2FrameWindowHandle()) || !WinIsWindowShowing(wnd->getOS2WindowHandle())) {
1429// dprintf(("UpdateWindow: window not showing %d/%d", WinIsWindowShowing(wnd->getOS2FrameWindowHandle()), WinIsWindowShowing(wnd->getOS2WindowHandle()) ));
1430// RELEASE_WNDOBJ(wnd);
1431// return FALSE;
1432// }
1433 //Must use frame window here. If the frame window has a valid update region and we call
1434 //WinUpdateWindow for the client window, then no WM_PAINT messages will be sent.
1435 rc = WinUpdateWindow(wnd->getOS2FrameWindowHandle());
1436#ifdef DEBUG
1437 if(WinQueryUpdateRect(wnd->getOS2WindowHandle(), NULL))
1438 {
1439 //if parent has valid update region then WinUpdateWindow will still fail..
1440 //might be harmless and happen even in windows
1441 dprintf (("ERROR: User32: UpdateWindow didn't send WM_PAINT messages!!"));
1442 }
1443#endif
1444 RELEASE_WNDOBJ(wnd);
1445 return rc;
1446}
1447//******************************************************************************
1448//******************************************************************************
1449BOOL WIN32API ValidateRect( HWND hwnd, const RECT * lprc)
1450{
1451 if(lprc) {
1452 dprintf(("USER32: ValidateRect %x (%d,%d)(%d,%d)", hwnd, lprc->left, lprc->top, lprc->right, lprc->bottom));
1453 }
1454 else dprintf(("USER32: ValidateRect %x", hwnd));
1455
1456 return RedrawWindow( hwnd, lprc, 0, RDW_VALIDATE_W | RDW_NOCHILDREN_W | (hwnd==0 ? RDW_UPDATENOW_W : 0));
1457}
1458//******************************************************************************
1459//******************************************************************************
1460BOOL WIN32API ValidateRgn( HWND hwnd, HRGN hrgn)
1461{
1462 dprintf(("USER32: ValidateRgn %x %x", hwnd, hrgn));
1463 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE_W | RDW_NOCHILDREN_W | (hwnd==0 ? RDW_UPDATENOW_W : 0));
1464}
1465//******************************************************************************
1466//******************************************************************************
1467BOOL WIN32API InvalidateRect (HWND hwnd, const RECT *pRect, BOOL erase)
1468{
1469 BOOL result;
1470
1471 if(pRect) {
1472 dprintf(("InvalidateRect %x (%d,%d)(%d,%d) erase=%d", hwnd, pRect->left, pRect->top, pRect->right, pRect->bottom, erase));
1473 }
1474 else dprintf(("InvalidateRect %x NULL erase=%d", hwnd, erase));
1475#if 1
1476 result = RedrawWindow (hwnd, pRect, NULLHANDLE,
1477 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1478 (erase ? RDW_ERASE_W : 0) |
1479 (hwnd == NULLHANDLE ? RDW_UPDATENOW_W : 0));
1480#else
1481 result = RedrawWindow (hwnd, pRect, NULLHANDLE,
1482 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1483 (erase ? RDW_ERASE_W : RDW_NOERASE_W) |
1484 (hwnd == NULLHANDLE ? RDW_UPDATENOW_W : 0));
1485#endif
1486 return (result);
1487}
1488//******************************************************************************
1489//******************************************************************************
1490BOOL WIN32API InvalidateRgn (HWND hwnd, HRGN hrgn, BOOL erase)
1491{
1492 BOOL result;
1493
1494 dprintf(("InvalidateRgn %x %x erase=%d", hwnd, hrgn, erase));
1495#if 1
1496 result = RedrawWindow (hwnd, NULL, hrgn,
1497 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1498 (erase ? RDW_ERASE_W : 0) |
1499 (hwnd == NULLHANDLE ? RDW_UPDATENOW_W : 0));
1500#else
1501 result = RedrawWindow (hwnd, NULL, hrgn,
1502 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1503 (erase ? RDW_ERASE_W : RDW_NOERASE_W) |
1504 (hwnd == NULLHANDLE ? RDW_UPDATENOW_W : 0));
1505#endif
1506 return (result);
1507}
1508//******************************************************************************
1509//TODO: Change for client rectangle!!!!!
1510//******************************************************************************
1511BOOL setPMRgnIntoWinRgn (HPS hps, HRGN hrgnPM, HRGN hrgnWin, LONG height)
1512{
1513 BOOL rc;
1514 RGNRECT rgnRect;
1515 rgnRect.ircStart = 1;
1516 rgnRect.crc = 0;
1517 rgnRect.ulDirection = RECTDIR_LFRT_TOPBOT;
1518
1519 rc = GpiQueryRegionRects (hps, hrgnPM, NULL, &rgnRect, NULL);
1520
1521 if (rc && (rgnRect.crcReturned > 0))
1522 {
1523 PRECTL Rcls = new RECTL[rgnRect.crcReturned];
1524 PRECTL pRcl = Rcls;
1525
1526 if (Rcls != NULL)
1527 {
1528 rgnRect.crc = rgnRect.crcReturned;
1529 rc = GpiQueryRegionRects (hps, hrgnPM, NULL, &rgnRect, Rcls);
1530
1531 rc = SetRectRgn(hrgnWin, pRcl->xLeft, height - pRcl->yTop,
1532 pRcl->xRight, height - pRcl->yBottom);
1533
1534 if (rgnRect.crcReturned > 1)
1535 {
1536 int i;
1537 HRGN temp;
1538 temp = CreateRectRgn (0, 0, 1, 1);
1539
1540 for (i = 1, pRcl++; rc && (i < rgnRect.crcReturned); i++, pRcl++)
1541 {
1542 rc = SetRectRgn (temp, pRcl->xLeft, height - pRcl->yTop,
1543 pRcl->xRight, height - pRcl->yBottom);
1544 rc &= CombineRgn (hrgnWin, hrgnWin, temp, RGN_OR_W);
1545 }
1546 DeleteObject (temp);
1547 }
1548 delete[] Rcls;
1549 }
1550 else
1551 {
1552 rc = FALSE;
1553 }
1554 }
1555 else
1556 {
1557 rc = SetRectRgn (hrgnWin, 0, 0, 0, 0);
1558 }
1559
1560 return (rc);
1561}
1562//******************************************************************************
1563//Using WinScrollWindow to scroll child windows is better (smoother).
1564//******************************************************************************
1565INT WIN32API ScrollWindowEx(HWND hwnd, int dx, int dy, const RECT *pScroll, const RECT *pClip,
1566 HRGN hrgnUpdate, PRECT pRectUpdate, UINT scrollFlag)
1567{
1568 Win32BaseWindow *window;
1569 APIRET rc;
1570 RECTL scrollRect;
1571 RECTL clipRect;
1572 PRECTL pScrollOS2 = NULL;
1573 PRECTL pClipOS2 = NULL;
1574 ULONG scrollFlagsOS2 = 0;
1575 int orgdy = dy;
1576 int regionType = ERROR_W;
1577
1578 window = Win32BaseWindow::GetWindowFromHandle(hwnd);
1579 if(!window) {
1580 dprintf(("ScrollWindowEx, window %x not found", hwnd));
1581 return 0;
1582 }
1583
1584 dprintf(("ScrollWindowEx %x %d %d %x %x", hwnd, dx, dy, pScroll, pClip));
1585
1586 dy = revertDy (window, dy);
1587
1588 if (scrollFlag & SW_INVALIDATE_W) scrollFlagsOS2 |= SW_INVALIDATERGN;
1589 if (scrollFlag & SW_SCROLLCHILDREN_W) scrollFlagsOS2 |= SW_SCROLLCHILDREN;
1590
1591 if(pScroll) {
1592 mapWin32ToOS2Rect(window->getClientHeight(), (RECT *)pScroll, (PRECTLOS2)&scrollRect);
1593 pScrollOS2 = &scrollRect;
1594 }
1595
1596 if(pClip) {
1597 mapWin32ToOS2Rect(window->getClientHeight(), (RECT *)pClip, (PRECTLOS2)&clipRect);
1598 pClipOS2 = &clipRect;
1599 }
1600
1601 RECTL rectlUpdate;
1602 HRGN hrgn = NULLHANDLE;
1603 HPS hpsScreen = 0;
1604
1605 if(hrgnUpdate) {
1606 RECTL rectl = { 1, 1, 2, 2 };
1607
1608 hpsScreen = WinGetScreenPS (HWND_DESKTOP);
1609 hrgn = GpiCreateRegion(hpsScreen, 1, &rectl);
1610 }
1611
1612 if (scrollFlag & SW_SMOOTHSCROLL_W)
1613 {
1614 INT time = (scrollFlag >> 16) & 0xFFFF;
1615
1616 //CB: todo, scroll in several steps
1617 // is time in ms? time <-> iteration count?
1618 }
1619
1620 LONG lComplexity = WinScrollWindow(window->getOS2WindowHandle(), dx, dy,
1621 pScrollOS2, pClipOS2,
1622 hrgn, &rectlUpdate, scrollFlagsOS2);
1623 if (lComplexity == RGN_ERROR)
1624 {
1625 RELEASE_WNDOBJ(window);
1626 return ERROR_W;
1627 }
1628
1629 //Notify children that they have moved (according to the SDK docs,
1630 //they only receive a WM_MOVE message)
1631 //(PM only does this for windows with CS_MOVENOTIFY class)
1632 //TODO: Verify this (no WM_WINDOWPOSCHANGING/ED?)
1633 if (scrollFlag & SW_SCROLLCHILDREN_W)
1634 {
1635 HWND hwndChild;
1636 RECT rectChild, rc;
1637
1638 dprintf(("ScrollWindowEx: Scroll child windows"));
1639 GetClientRect(hwnd, &rc);
1640 if (pScroll) IntersectRect(&rc, &rc, pScroll);
1641
1642 hwndChild = GetWindow(hwnd, GW_CHILD_W);
1643
1644 while(hwndChild)
1645 {
1646 Win32BaseWindow *child;
1647
1648 child = Win32BaseWindow::GetWindowFromHandle(hwndChild);
1649 if(!child) {
1650 dprintf(("ERROR: ScrollWindowEx, child %x not found", hwnd));
1651 RELEASE_WNDOBJ(window);
1652 return 0;
1653 }
1654 rectChild = *child->getWindowRect();
1655 if(!pScroll || IntersectRect(&rectChild, &rectChild, &rc))
1656 {
1657 dprintf(("ScrollWindowEx: Scroll child window %x", hwndChild));
1658 child->ScrollWindow(dx, orgdy);
1659 }
1660 RELEASE_WNDOBJ(child);
1661 hwndChild = GetWindow(hwndChild, GW_HWNDNEXT_W);
1662 }
1663 dprintf(("***ScrollWindowEx: Scroll child windows DONE"));
1664 }
1665
1666 RECT winRectUpdate;
1667
1668 mapOS2ToWin32Rect(window->getClientHeight(), (PRECTLOS2)&rectlUpdate, &winRectUpdate);
1669
1670 if (pRectUpdate)
1671 *pRectUpdate = winRectUpdate;
1672
1673 if (hrgnUpdate) {
1674 rc = setPMRgnIntoWinRgn(hpsScreen, hrgn, hrgnUpdate, window->getClientHeight());
1675 GpiDestroyRegion(hpsScreen, hrgn);
1676 WinReleasePS(hpsScreen);
1677 }
1678
1679#if 0
1680 //SvL: WinScrollWindow already invalidates the area; no need to do it again
1681 //(call to invalidateRect was wrong; has to include erase flag)
1682 if ((scrollFlag & SW_INVALIDATE_W) &&
1683 ((lComplexity == RGN_RECT) || (lComplexity == RGN_COMPLEX)))
1684 {
1685 rc = RedrawWindow (hwnd, &winRectUpdate, NULLHANDLE,
1686 RDW_ALLCHILDREN_W | RDW_INVALIDATE_W |
1687 ((scrollFlag & SW_ERASE_W) ? RDW_ERASE_W : 0) |
1688 RDW_UPDATENOW_W);
1689
1690// rc = InvalidateRect (hwnd, &winRectUpdate, (scrollFlag & SW_ERASE_W) ? 1 : 0);
1691 if (rc == FALSE)
1692 {
1693 RELEASE_WNDOBJ(window);
1694 return (0);
1695 }
1696 }
1697#endif
1698
1699 switch (lComplexity)
1700 {
1701 case RGN_NULL:
1702 regionType = NULLREGION_W;
1703 break;
1704 case RGN_RECT:
1705 regionType = SIMPLEREGION_W;
1706 break;
1707 case RGN_COMPLEX:
1708 regionType = COMPLEXREGION_W;
1709 break;
1710 default:
1711 regionType = ERROR_W;
1712 break;
1713 }
1714
1715 RELEASE_WNDOBJ(window);
1716 return (regionType);
1717}
1718//******************************************************************************
1719//******************************************************************************
1720BOOL WIN32API ScrollWindow(HWND hwnd, int dx, int dy, const RECT *pScroll, const RECT *pClip)
1721{
1722 dprintf(("ScrollWindow %x %d %d %x %x", hwnd, dx, dy, pScroll, pClip));
1723 return (ERROR_W != ScrollWindowEx(hwnd, dx, dy, pScroll, pClip, 0, NULL,
1724 (pScroll ? 0 : SW_SCROLLCHILDREN_W) |
1725 SW_INVALIDATE_W ));
1726}
1727//******************************************************************************
1728//******************************************************************************
1729HWND WIN32API WindowFromDC(HDC hdc)
1730{
1731 pDCData pHps = (pDCData)GpiQueryDCData( (HPS)hdc );
1732
1733 dprintf2(("USER32: WindowFromDC %x", hdc));
1734 if ( pHps )
1735 return OS2ToWin32Handle(pHps->hwnd);
1736 else
1737 return 0;
1738}
1739//******************************************************************************
1740//******************************************************************************
1741
Note: See TracBrowser for help on using the repository browser.