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

Last change on this file since 8105 was 8105, checked in by sandervl, 23 years ago

Changes & fixes for minimized windows

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