Remove the validated child area from the update region of parent for
[wine] / windows / painting.c
1 /*
2  * Window painting functions
3  *
4  * Copyright 1993, 1994, 1995 Alexandre Julliard
5  *                       1999 Alex Korobka
6  */
7
8 #include "region.h"
9 #include "win.h"
10 #include "queue.h"
11 #include "dce.h"
12 #include "heap.h"
13 #include "debugtools.h"
14 #include "wine/winuser16.h"
15
16 DECLARE_DEBUG_CHANNEL(nonclient)
17 DECLARE_DEBUG_CHANNEL(win)
18
19 /* client rect in window coordinates */
20
21 #define GETCLIENTRECTW( wnd, r )        (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
22                                         (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
23                                         (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
24                                         (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
25
26   /* Last CTLCOLOR id */
27 #define CTLCOLOR_MAX   CTLCOLOR_STATIC
28
29
30 /***********************************************************************
31  *           WIN_UpdateNCRgn
32  *
33  *  Things to do:
34  *      Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
35  *      Crop hrgnUpdate to a client rect, especially if it 1.
36  *      If UNC_REGION is set return update region for the client rect.
37  *
38  *  NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
39  *        The trick is that when the returned region handle may be different from hRgn.
40  *        In this case the old hRgn must be considered gone. BUT, if the returned value
41  *        is 1 then the hRgn is preserved and RDW_Paint() will have to get 
42  *        a DC without extra clipping region.
43  */
44 HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
45 {
46     RECT  r;
47     HRGN  hClip = 0;
48     HRGN  hrgnRet = 0;
49
50     TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n", 
51                       wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
52
53     /* desktop window doesn't have a nonclient area */
54     if(wnd == WIN_GetDesktop()) 
55     {
56         wnd->flags &= ~WIN_NEEDS_NCPAINT;
57         if( wnd->hrgnUpdate > 1 )
58             hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
59         else 
60         {
61             hrgnRet = wnd->hrgnUpdate;
62         }
63         WIN_ReleaseDesktop();
64         return hrgnRet;
65     }
66     WIN_ReleaseDesktop();
67
68     if ((wnd->hwndSelf == GetForegroundWindow()) &&
69         !(wnd->flags & WIN_NCACTIVATED) )
70     {
71         wnd->flags |= WIN_NCACTIVATED;
72         uncFlags |= UNC_ENTIRE; 
73     }
74
75     if( wnd->flags & WIN_NEEDS_NCPAINT )
76     {
77             RECT r2, r3;
78
79             wnd->flags &= ~WIN_NEEDS_NCPAINT;  
80             GETCLIENTRECTW( wnd, r );
81
82             TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n", 
83                                 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
84             if( wnd->hrgnUpdate > 1 )
85             {
86                 /* Check if update rgn overlaps with nonclient area */
87
88                 GetRgnBox( wnd->hrgnUpdate, &r2 );
89                 UnionRect( &r3, &r2, &r );
90                 if( r3.left != r.left || r3.top != r.top || 
91                     r3.right != r.right || r3.bottom != r.bottom ) /* it does */
92                 {
93                     /* crop hrgnUpdate, save old one in hClip - the only
94                      * case that places a valid region handle in hClip */
95
96                     hClip = wnd->hrgnUpdate;
97                     wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
98                     if( uncFlags & UNC_REGION ) hrgnRet = hClip;
99                 }
100
101                 if( uncFlags & UNC_CHECK )
102                 {
103                     GetRgnBox( wnd->hrgnUpdate, &r3 );
104                     if( IsRectEmpty( &r3 ) )
105                     {
106                         /* delete the update region since all invalid 
107                          * parts were in the nonclient area */
108
109                         DeleteObject( wnd->hrgnUpdate );
110                         wnd->hrgnUpdate = 0;
111                         if(!(wnd->flags & WIN_INTERNAL_PAINT))
112                             QUEUE_DecPaintCount( wnd->hmemTaskQ );
113
114                         wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
115                     }
116                 }
117
118                 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
119             }
120             else 
121             if( wnd->hrgnUpdate == 1 )/* entire window */
122             {
123                 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
124                 if( uncFlags & UNC_REGION ) hrgnRet = 1;
125                 uncFlags |= UNC_ENTIRE;
126             }
127     }
128     else /* no WM_NCPAINT unless forced */
129     {
130         if( wnd->hrgnUpdate >  1 )
131         {
132 copyrgn:
133             if( uncFlags & UNC_REGION )
134                 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
135         }
136         else
137         if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
138         {
139             GETCLIENTRECTW( wnd, r ); 
140             wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
141             if( uncFlags & UNC_REGION ) hrgnRet = 1;
142         }
143     }
144
145     if(!hClip && (uncFlags & UNC_ENTIRE) )
146     {
147         /* still don't do anything if there is no nonclient area */
148         hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
149     }
150
151     if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
152     {
153         SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
154         if( (hClip > 1)&& (hClip != hRgn) && (hClip != hrgnRet) ) DeleteObject( hClip );
155         /*
156          * Since all Window locks are suspended while processing the WM_NCPAINT
157          * we want to make sure the window still exists before continuing.
158          */
159         if (!IsWindow(wnd->hwndSelf))
160         {
161           DeleteObject(hrgnRet);
162           hrgnRet=0;
163         }
164     }
165
166     TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
167
168     return hrgnRet;
169 }
170
171
172 /***********************************************************************
173  *           BeginPaint16    (USER.39)
174  */
175 HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps ) 
176 {
177     BOOL bIcon;
178     HRGN hrgnUpdate;
179     WND *wndPtr = WIN_FindWndPtr( hwnd );
180     if (!wndPtr) return 0;
181
182     bIcon = (wndPtr->dwStyle & WS_MINIMIZE && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
183
184     wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
185
186     /* send WM_NCPAINT and make sure hrgnUpdate is a valid rgn handle */
187     WIN_UpdateNCRgn( wndPtr, 0, UNC_UPDATE );
188
189     /*
190      * Make sure the window is still a window. All window locks are suspended
191      * when the WM_NCPAINT is sent.
192      */
193     if (!IsWindow(wndPtr->hwndSelf))
194     {
195         WIN_ReleaseWndPtr(wndPtr);
196         return 0;
197     }
198
199     if( ((hrgnUpdate = wndPtr->hrgnUpdate) != 0) || (wndPtr->flags & WIN_INTERNAL_PAINT))
200         QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
201
202     wndPtr->hrgnUpdate = 0;
203     wndPtr->flags &= ~WIN_INTERNAL_PAINT;
204
205     HideCaret( hwnd );
206
207     TRACE_(win)("hrgnUpdate = %04x, \n", hrgnUpdate);
208
209     if (GetClassWord16(wndPtr->hwndSelf, GCW_STYLE) & CS_PARENTDC)
210     {
211         /* Don't clip the output to the update region for CS_PARENTDC window */
212         if( hrgnUpdate ) 
213             DeleteObject(hrgnUpdate);
214         lps->hdc = GetDCEx16( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
215                               (bIcon ? DCX_WINDOW : 0) );
216     }
217     else
218     {
219         if( hrgnUpdate ) /* convert to client coordinates */
220             OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
221                                    wndPtr->rectWindow.top - wndPtr->rectClient.top );
222         lps->hdc = GetDCEx16(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
223                              DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
224         /* ReleaseDC() in EndPaint() will delete the region */
225     }
226
227     TRACE_(win)("hdc = %04x\n", lps->hdc);
228
229     if (!lps->hdc)
230     {
231         WARN_(win)("GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
232         WIN_ReleaseWndPtr(wndPtr);
233         return 0;
234     }
235
236     GetClipBox16( lps->hdc, &lps->rcPaint );
237
238     TRACE_(win)("box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
239                     lps->rcPaint.right, lps->rcPaint.bottom );
240
241     if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
242     {
243         wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
244         lps->fErase = !SendMessage16(hwnd, (bIcon) ? WM_ICONERASEBKGND
245                                                    : WM_ERASEBKGND,
246                                      (WPARAM16)lps->hdc, 0 );
247     }
248     else lps->fErase = TRUE;
249
250     WIN_ReleaseWndPtr(wndPtr);
251     return lps->hdc;
252 }
253
254
255 /***********************************************************************
256  *           BeginPaint32    (USER32.10)
257  */
258 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
259 {
260     PAINTSTRUCT16 ps;
261
262     BeginPaint16( hwnd, &ps );
263     lps->hdc            = (HDC)ps.hdc;
264     lps->fErase         = ps.fErase;
265     lps->rcPaint.top    = ps.rcPaint.top;
266     lps->rcPaint.left   = ps.rcPaint.left;
267     lps->rcPaint.right  = ps.rcPaint.right;
268     lps->rcPaint.bottom = ps.rcPaint.bottom;
269     lps->fRestore       = ps.fRestore;
270     lps->fIncUpdate     = ps.fIncUpdate;
271     return lps->hdc;
272 }
273
274
275 /***********************************************************************
276  *           EndPaint16    (USER.40)
277  */
278 BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
279 {
280     ReleaseDC16( hwnd, lps->hdc );
281     ShowCaret( hwnd );
282     return TRUE;
283 }
284
285
286 /***********************************************************************
287  *           EndPaint32    (USER32.176)
288  */
289 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
290 {
291     ReleaseDC( hwnd, lps->hdc );
292     ShowCaret( hwnd );
293     return TRUE;
294 }
295
296
297 /***********************************************************************
298  *           FillWindow    (USER.324)
299  */
300 void WINAPI FillWindow16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc, HBRUSH16 hbrush )
301 {
302     RECT16 rect;
303     GetClientRect16( hwnd, &rect );
304     DPtoLP16( hdc, (LPPOINT16)&rect, 2 );
305     PaintRect16( hwndParent, hwnd, hdc, hbrush, &rect );
306 }
307
308
309 /***********************************************************************
310  *           PAINT_GetControlBrush
311  */
312 static HBRUSH16 PAINT_GetControlBrush( HWND hParent, HWND hWnd, HDC16 hDC, UINT16 ctlType )
313 {
314     HBRUSH16 bkgBrush = (HBRUSH16)SendMessageA( hParent, WM_CTLCOLORMSGBOX + ctlType, 
315                                                              (WPARAM)hDC, (LPARAM)hWnd );
316     if( !IsGDIObject16(bkgBrush) )
317         bkgBrush = DEFWND_ControlColor( hDC, ctlType );
318     return bkgBrush;
319 }
320
321
322 /***********************************************************************
323  *           PaintRect    (USER.325)
324  */
325 void WINAPI PaintRect16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc,
326                        HBRUSH16 hbrush, const RECT16 *rect)
327 {
328     if( hbrush <= CTLCOLOR_MAX ) 
329     {
330         if( hwndParent )
331             hbrush = PAINT_GetControlBrush( hwndParent, hwnd, hdc, (UINT16)hbrush );
332         else 
333             return;
334     }
335     if( hbrush ) 
336         FillRect16( hdc, rect, hbrush );
337 }
338
339
340 /***********************************************************************
341  *           GetControlBrush    (USER.326)
342  */
343 HBRUSH16 WINAPI GetControlBrush16( HWND16 hwnd, HDC16 hdc, UINT16 ctlType )
344 {
345     WND* wndPtr = WIN_FindWndPtr( hwnd );
346     HBRUSH16 retvalue;
347
348     if((ctlType <= CTLCOLOR_MAX) && wndPtr )
349     {
350         WND* parent;
351         if( wndPtr->dwStyle & WS_POPUP ) parent = WIN_LockWndPtr(wndPtr->owner);
352         else parent = WIN_LockWndPtr(wndPtr->parent);
353         if( !parent ) parent = wndPtr;
354         retvalue = (HBRUSH16)PAINT_GetControlBrush( parent->hwndSelf, hwnd, hdc, ctlType );
355         WIN_ReleaseWndPtr(parent);
356         goto END;
357     }
358     retvalue = (HBRUSH16)0;
359 END:
360     WIN_ReleaseWndPtr(wndPtr);
361     return retvalue;
362 }
363
364
365 /***********************************************************************
366  *              RDW_ValidateParent [RDW_UpdateRgns() helper] 
367  *
368  *  Validate the portions of parent that are covered by a validated child
369  *  wndPtr = child
370  */
371 void  RDW_ValidateParent(WND *wndChild)  
372 {
373     WND *wndParent = WIN_LockWndPtr(wndChild->parent);
374     WND *wndDesktop = WIN_GetDesktop();
375
376     if ((wndParent) && (wndParent != wndDesktop) && !(wndParent->dwStyle & WS_CLIPCHILDREN))
377     {
378         HRGN hrg;
379         if (wndChild->hrgnUpdate == 1 )
380         {
381             RECT r = {0, 0, wndChild->rectWindow.right - wndChild->rectWindow.left,
382                             wndChild->rectWindow.bottom - wndChild->rectWindow.top };
383             hrg = CreateRectRgnIndirect( &r );
384         }
385         else
386            hrg = wndChild->hrgnUpdate;
387         if (wndParent->hrgnUpdate != 0)
388         {
389             POINT ptOffset;
390             RECT rect, rectParent;
391             if( wndParent->hrgnUpdate == 1 )
392             {
393                RECT r = {0, 0, wndParent->rectWindow.right - wndParent->rectWindow.left,
394                             wndParent->rectWindow.bottom - wndParent->rectWindow.top };
395                wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
396             }
397             /* we must offset the child region by the offset of the child rect in the parent */
398             GetWindowRect(wndParent->hwndSelf, &rectParent);
399             GetWindowRect(wndChild->hwndSelf, &rect);
400             ptOffset.x = rect.left - rectParent.left;
401             ptOffset.y = rect.top - rectParent.top;
402             OffsetRgn( hrg, ptOffset.x, ptOffset.y );
403             CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
404             OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
405         }
406         if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
407     }
408     WIN_ReleaseWndPtr(wndParent);
409     WIN_ReleaseDesktop();
410 }
411
412 /***********************************************************************
413  *              RDW_UpdateRgns [RedrawWindow() helper] 
414  *
415  *  Walks the window tree and adds/removes parts of the hRgn to/from update
416  *  regions of windows that overlap it. Also, manages internal paint flags.
417  *
418  *  NOTE: Walks the window tree so the caller must lock it.
419  *        MUST preserve hRgn (can modify but then has to restore).
420  */
421 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
422 {
423     /* 
424      * Called only when one of the following is set:
425      * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
426      */
427
428     BOOL bHadOne =  wndPtr->hrgnUpdate && hRgn;
429     BOOL bChildren =  ( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) 
430                         && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
431     RECT r;
432
433     r.left = 0;
434     r.top = 0;
435     r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
436     r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
437
438     TRACE_(win)("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
439
440     if( flags & RDW_INVALIDATE )
441     {
442         if( hRgn > 1 )
443         {
444             switch( wndPtr->hrgnUpdate )
445             {
446                 default:
447                         CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
448                         /* fall through */
449                 case 0:
450                         wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate, 
451                                                              wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn, 
452                                                              &r, NULL );
453                         if( !bHadOne )
454                         {
455                             GetRgnBox( wndPtr->hrgnUpdate, &r );
456                             if( IsRectEmpty( &r ) )
457                             {
458                                 DeleteObject( wndPtr->hrgnUpdate );
459                                 wndPtr->hrgnUpdate = 0;
460                                 goto OUT;
461                             }
462                         }
463                         break;
464                 case 1: /* already an entire window */
465                         break;
466             }
467         }
468         else if( hRgn == 1 )
469         {
470             if( wndPtr->hrgnUpdate > 1 )
471                 DeleteObject( wndPtr->hrgnUpdate );
472             wndPtr->hrgnUpdate = 1;
473         }
474         else
475             hRgn = wndPtr->hrgnUpdate;  /* this is a trick that depends on code in PAINT_RedrawWindow() */
476
477         if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
478             QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
479
480         if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
481         if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
482         flags    |= RDW_FRAME;
483     }
484     else if( flags & RDW_VALIDATE )
485     {
486         if( wndPtr->hrgnUpdate )
487         {
488             if( hRgn > 1 )
489             {
490                 if( wndPtr->hrgnUpdate == 1 )
491                     wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
492
493                 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
494                     == NULLREGION )
495                     goto EMPTY;
496             }
497             else /* validate everything */
498             {
499                 if( wndPtr->hrgnUpdate > 1 )
500                 {
501 EMPTY:
502                     DeleteObject( wndPtr->hrgnUpdate );
503                 }
504                 wndPtr->hrgnUpdate = 0;
505             }
506
507             if( !wndPtr->hrgnUpdate )
508             {
509                 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
510                 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
511                     QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
512             }
513         }
514
515         if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
516         if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
517
518     }
519
520     if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
521         RDW_ValidateParent(wndPtr); /* validate parent covered by region */
522
523     /* in/validate child windows that intersect with the region if it
524      * is a valid handle. */
525
526     if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
527     {
528         if( hRgn > 1 && bChildren )
529         {
530             WND* wnd = wndPtr->child;
531             POINT ptTotal, prevOrigin = {0,0};
532             POINT ptClient;
533
534             ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
535             ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
536
537             for( ptTotal.x = ptTotal.y = 0; wnd; wnd = wnd->next )
538             {
539                 if( wnd->dwStyle & WS_VISIBLE )
540                 {
541                     POINT ptOffset;
542
543                     r.left = wnd->rectWindow.left + ptClient.x;
544                     r.right = wnd->rectWindow.right + ptClient.x;
545                     r.top = wnd->rectWindow.top + ptClient.y;
546                     r.bottom = wnd->rectWindow.bottom + ptClient.y;
547
548                     ptOffset.x = r.left - prevOrigin.x; 
549                     ptOffset.y = r.top - prevOrigin.y;
550                     OffsetRect( &r, -ptTotal.x, -ptTotal.y );
551
552                     if( RectInRegion( hRgn, &r ) )
553                     {
554                         OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
555                         RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
556                         prevOrigin.x = r.left + ptTotal.x;
557                         prevOrigin.y = r.top + ptTotal.y;
558                         ptTotal.x += ptOffset.x;
559                         ptTotal.y += ptOffset.y;
560                     }
561                 }
562             }
563             OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
564             bChildren = 0;
565         }
566     }
567
568     /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
569
570     if( bChildren )
571     {
572         WND* wnd;
573         for( wnd = wndPtr->child; wnd; wnd = wnd->next )
574              if( wnd->dwStyle & WS_VISIBLE )
575                  RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
576     }
577
578 OUT:
579
580     /* Set/clear internal paint flag */
581
582     if (flags & RDW_INTERNALPAINT)
583     {
584         if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
585             QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
586         wndPtr->flags |= WIN_INTERNAL_PAINT;
587     }
588     else if (flags & RDW_NOINTERNALPAINT)
589     {
590         if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
591             QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
592         wndPtr->flags &= ~WIN_INTERNAL_PAINT;
593     }
594 }
595
596 /***********************************************************************
597  *           RDW_Paint [RedrawWindow() helper]
598  *
599  * Walks the window tree and paints/erases windows that have
600  * nonzero update regions according to redraw flags. hrgn is a scratch
601  * region passed down during recursion. Must not be 1.
602  *
603  */
604 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
605 {
606 /* NOTE: wndPtr is locked by caller.
607  * 
608  * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
609  * SendMessage() calls. This is a comment inside DefWindowProc() source
610  * from 16-bit SDK:
611  *
612  *   This message avoids lots of inter-app message traffic
613  *   by switching to the other task and continuing the
614  *   recursion there.
615  *
616  * wParam         = flags
617  * LOWORD(lParam) = hrgnClip
618  * HIWORD(lParam) = hwndSkip  (not used; always NULL)
619  *
620  */
621     HDC  hDC;
622     HWND hWnd = wndPtr->hwndSelf;
623     BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassWord(wndPtr->hwndSelf, GCW_HICON)); 
624
625       /* Erase/update the window itself ... */
626
627     TRACE_(win)("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
628
629     if (flags & RDW_UPDATENOW)
630     {
631         if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
632             SendMessage16( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
633     }
634     else if ((flags & RDW_ERASENOW) || (ex & RDW_EX_TOPFRAME))
635     {
636         UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
637         HRGN hrgnRet = WIN_UpdateNCRgn( wndPtr, hrgn, UNC_REGION | UNC_CHECK | 
638                             ((ex & RDW_EX_TOPFRAME) ? UNC_ENTIRE : 0) ); 
639         if( hrgnRet )
640         {
641             if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
642             if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
643             {
644                 if( bIcon ) dcx |= DCX_WINDOW;
645                 else 
646                 if( hrgnRet )
647                     OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left, 
648                                         wndPtr->rectWindow.top  - wndPtr->rectClient.top);
649                 else
650                     dcx &= ~DCX_INTERSECTRGN;
651                 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
652                 {
653                     if (SendMessage16( hWnd, (bIcon) ? WM_ICONERASEBKGND
654                                        : WM_ERASEBKGND, (WPARAM16)hDC, 0 ))
655                     wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
656                     ReleaseDC( hWnd, hDC );
657                 }
658             }
659         }
660     }
661
662     if( !IsWindow(hWnd) ) return hrgn;
663     ex &= ~RDW_EX_TOPFRAME;
664
665       /* ... and its child windows */
666
667     if( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) 
668         && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
669     {
670         WND** list, **ppWnd;
671
672         if( (list = WIN_BuildWinArray( wndPtr, 0, NULL )) )
673         {
674             wndPtr = NULL;
675             for (ppWnd = list; *ppWnd; ppWnd++)
676             {
677                 WIN_UpdateWndPtr(&wndPtr,*ppWnd);
678                 if (!IsWindow(wndPtr->hwndSelf)) continue;
679                     if ( (wndPtr->dwStyle & WS_VISIBLE) &&
680                          (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
681                         hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
682             }
683             WIN_ReleaseWndPtr(wndPtr);
684             WIN_ReleaseWinArray(list);
685         }
686     }
687
688     return hrgn;
689 }
690
691 /***********************************************************************
692  *           PAINT_RedrawWindow
693  *
694  */
695 BOOL PAINT_RedrawWindow( HWND hwnd, const RECT *rectUpdate,
696                            HRGN hrgnUpdate, UINT flags, UINT ex )
697 {
698     HRGN hRgn = 0;
699     RECT r, r2;
700     POINT pt;
701     WND* wndPtr;
702
703     if (!hwnd) hwnd = GetDesktopWindow();
704     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
705
706     /* check if the window or its parents are visible/not minimized */
707
708     if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
709     {
710         WIN_ReleaseWndPtr(wndPtr);
711         return TRUE; 
712     }
713
714     if (TRACE_ON(win))
715     {
716         if( hrgnUpdate )
717         {
718             GetRgnBox( hrgnUpdate, &r );
719             TRACE_(win)( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x, exflags=%04x\n", 
720                   hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags, ex);
721         }
722         else
723         {
724             if( rectUpdate )
725                 r = *rectUpdate;
726             else
727                 SetRectEmpty( &r );
728             TRACE_(win)( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x, exflags=%04x\n",
729                         hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left, 
730                         r.top, r.right, r.bottom, hrgnUpdate, flags, ex );
731         }
732     }
733
734     /* prepare an update region in window coordinates */
735
736     if( flags & RDW_FRAME )
737         r = wndPtr->rectWindow;
738     else
739         r = wndPtr->rectClient;
740
741     if( ex & RDW_EX_XYWINDOW )
742     {
743         pt.x = pt.y = 0;
744         OffsetRect( &r, -wndPtr->rectWindow.left, -wndPtr->rectWindow.top );
745     }
746     else
747     {
748         pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
749         pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
750         OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
751     }
752
753     if (flags & RDW_INVALIDATE)  /* ------------------------- Invalidate */
754     {
755         /* If the window doesn't have hrgnUpdate we leave hRgn zero
756          * and put a new region straight into wndPtr->hrgnUpdate
757          * so that RDW_UpdateRgns() won't have to do any extra work.
758          */
759
760         if( hrgnUpdate )
761         {
762             if( wndPtr->hrgnUpdate )
763                 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
764             else 
765                 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt ); 
766         }
767         else if( rectUpdate )
768         {
769             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
770             OffsetRect( &r2, pt.x, pt.y );
771
772 rect2i:
773             if( wndPtr->hrgnUpdate == 0 )
774                 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
775             else
776                 hRgn = CreateRectRgnIndirect( &r2 );
777         }
778         else /* entire window or client depending on RDW_FRAME */
779         {
780             if( flags & RDW_FRAME )
781             {
782                 if( wndPtr->hrgnUpdate )
783                     DeleteObject( wndPtr->hrgnUpdate );
784                 wndPtr->hrgnUpdate = 1;
785             }
786             else
787             {
788                 GETCLIENTRECTW( wndPtr, r2 );
789                 goto rect2i;
790             }
791         }
792     }
793     else if (flags & RDW_VALIDATE)  /* ------------------------- Validate */
794     {
795         /* In this we cannot leave with zero hRgn */
796         if( hrgnUpdate )
797         {
798             hRgn = REGION_CropRgn( hRgn, hrgnUpdate,  &r, &pt );
799             GetRgnBox( hRgn, &r2 );
800             if( IsRectEmpty( &r2 ) ) goto END;
801         }
802         else if( rectUpdate )
803         {
804             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
805                 OffsetRect( &r2, pt.x, pt.y );
806 rect2v:
807             hRgn = CreateRectRgnIndirect( &r2 );
808         }
809         else /* entire window or client depending on RDW_FRAME */
810         {
811             if( flags & RDW_FRAME ) 
812                 hRgn = 1;
813             else
814             {
815                 GETCLIENTRECTW( wndPtr, r2 );
816                 goto rect2v;
817             }
818         }
819     }
820
821     /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
822
823     RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
824
825     /* Erase/update windows, from now on hRgn is a scratch region */
826
827     hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, ex );
828
829 END:
830     if( hRgn > 1 && (hRgn != hrgnUpdate) )
831         DeleteObject(hRgn );
832     WIN_ReleaseWndPtr(wndPtr);
833     return TRUE;
834 }
835
836
837 /***********************************************************************
838  *           RedrawWindow32    (USER32.426)
839  */
840 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
841                               HRGN hrgnUpdate, UINT flags )
842 {
843     return PAINT_RedrawWindow( hwnd, rectUpdate, hrgnUpdate, flags, 0 );
844 }
845
846
847 /***********************************************************************
848  *           RedrawWindow16    (USER.290)
849  */
850 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
851                               HRGN16 hrgnUpdate, UINT16 flags )
852 {
853     if (rectUpdate)
854     {
855         RECT r;
856         CONV_RECT16TO32( rectUpdate, &r );
857         return (BOOL16)RedrawWindow( (HWND)hwnd, &r, hrgnUpdate, flags );
858     }
859     return (BOOL16)PAINT_RedrawWindow( (HWND)hwnd, NULL, 
860                                        (HRGN)hrgnUpdate, flags, 0 );
861 }
862
863
864 /***********************************************************************
865  *           UpdateWindow16   (USER.124)
866  */
867 void WINAPI UpdateWindow16( HWND16 hwnd )
868 {
869     PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_NOCHILDREN, 0 );
870 }
871
872 /***********************************************************************
873  *           UpdateWindow32   (USER32.567)
874  */
875 void WINAPI UpdateWindow( HWND hwnd )
876 {
877     PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_NOCHILDREN, 0 );
878 }
879
880 /***********************************************************************
881  *           InvalidateRgn16   (USER.126)
882  */
883 void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
884 {
885     PAINT_RedrawWindow((HWND)hwnd, NULL, (HRGN)hrgn, 
886                        RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
887 }
888
889
890 /***********************************************************************
891  *           InvalidateRgn32   (USER32.329)
892  */
893 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
894 {
895     return PAINT_RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
896 }
897
898
899 /***********************************************************************
900  *           InvalidateRect16   (USER.125)
901  */
902 void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
903 {
904     RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
905 }
906
907
908 /***********************************************************************
909  *           InvalidateRect32   (USER32.328)
910  */
911 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
912 {
913     return PAINT_RedrawWindow( hwnd, rect, 0, 
914                                RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
915 }
916
917
918 /***********************************************************************
919  *           ValidateRgn16   (USER.128)
920  */
921 void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
922 {
923     PAINT_RedrawWindow( (HWND)hwnd, NULL, (HRGN)hrgn, 
924                         RDW_VALIDATE | RDW_NOCHILDREN, 0 );
925 }
926
927
928 /***********************************************************************
929  *           ValidateRgn32   (USER32.572)
930  */
931 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
932 {
933     PAINT_RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
934 }
935
936
937 /***********************************************************************
938  *           ValidateRect16   (USER.127)
939  */
940 void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
941 {
942     RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
943 }
944
945
946 /***********************************************************************
947  *           ValidateRect32   (USER32.571)
948  */
949 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
950 {
951     PAINT_RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
952 }
953
954
955 /***********************************************************************
956  *           GetUpdateRect16   (USER.190)
957  */
958 BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
959 {
960     RECT r;
961     BOOL16 ret;
962
963     if (!rect) return GetUpdateRect( hwnd, NULL, erase );
964     ret = GetUpdateRect( hwnd, &r, erase );
965     CONV_RECT32TO16( &r, rect );
966     return ret;
967 }
968
969
970 /***********************************************************************
971  *           GetUpdateRect32   (USER32.297)
972  */
973 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
974 {
975     BOOL retvalue;
976     WND * wndPtr = WIN_FindWndPtr( hwnd );
977     if (!wndPtr) return FALSE;
978
979     if (rect)
980     {
981         if (wndPtr->hrgnUpdate > 1)
982         {
983             HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
984             if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
985             {
986                 retvalue = FALSE;
987                 goto END;
988             }
989             GetRgnBox( hrgn, rect );
990             DeleteObject( hrgn );
991             if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
992             {
993                 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
994                 {
995                     DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect,  2);
996                 }
997             }
998         }
999         else
1000         if( wndPtr->hrgnUpdate == 1 )
1001         {
1002             GetClientRect( hwnd, rect );
1003             if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
1004         }
1005         else 
1006             SetRectEmpty( rect );
1007     }
1008     retvalue = (wndPtr->hrgnUpdate >= 1);
1009 END:
1010     WIN_ReleaseWndPtr(wndPtr);
1011     return retvalue;
1012 }
1013
1014
1015 /***********************************************************************
1016  *           GetUpdateRgn16   (USER.237)
1017  */
1018 INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1019 {
1020     return GetUpdateRgn( hwnd, hrgn, erase );
1021 }
1022
1023
1024 /***********************************************************************
1025  *           GetUpdateRgn    (USER32.298)
1026  */
1027 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1028 {
1029     INT retval;
1030     WND * wndPtr = WIN_FindWndPtr( hwnd );
1031     if (!wndPtr) return ERROR;
1032
1033     if (wndPtr->hrgnUpdate == 0)
1034     {
1035         SetRectRgn( hrgn, 0, 0, 0, 0 );
1036         retval = NULLREGION;
1037         goto END;
1038     }
1039     else
1040     if (wndPtr->hrgnUpdate == 1)
1041     {
1042         SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1043                                 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1044         retval = SIMPLEREGION;
1045     }
1046     else
1047     {
1048         retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1049         OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1050                          wndPtr->rectWindow.top - wndPtr->rectClient.top );
1051     }
1052     if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1053 END:
1054     WIN_ReleaseWndPtr(wndPtr);
1055     return retval;
1056 }
1057
1058
1059 /***********************************************************************
1060  *           ExcludeUpdateRgn16   (USER.238)
1061  */
1062 INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
1063 {
1064     return ExcludeUpdateRgn( hdc, hwnd );
1065 }
1066
1067
1068 /***********************************************************************
1069  *           ExcludeUpdateRgn32   (USER32.195)
1070  */
1071 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1072 {
1073     RECT rect;
1074     WND * wndPtr;
1075
1076     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1077
1078     if (wndPtr->hrgnUpdate)
1079     {
1080         INT ret;
1081         HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1082                                       wndPtr->rectWindow.top - wndPtr->rectClient.top,
1083                                       wndPtr->rectWindow.right - wndPtr->rectClient.left,
1084                                       wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1085         if( wndPtr->hrgnUpdate > 1 )
1086         {
1087             CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1088             OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left, 
1089                             wndPtr->rectWindow.top - wndPtr->rectClient.top );
1090         }
1091
1092         /* do ugly coordinate translations in dce.c */
1093
1094         ret = DCE_ExcludeRgn( hdc, wndPtr, hrgn );
1095         DeleteObject( hrgn );
1096         WIN_ReleaseWndPtr(wndPtr);
1097         return ret;
1098     } 
1099     WIN_ReleaseWndPtr(wndPtr);
1100     return GetClipBox( hdc, &rect );
1101 }
1102
1103