Added more batch functionality, including the CALL GOTO and SHIFT
[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 int i;
365
366 /***********************************************************************
367  *              RDW_UpdateRgns [RedrawWindow() helper] 
368  *
369  *  Walks the window tree and adds/removes parts of the hRgn to/from update
370  *  regions of windows that overlap it. Also, manages internal paint flags.
371  *
372  *  NOTE: Walks the window tree so the caller must lock it.
373  *        MUST preserve hRgn (can modify but then has to restore).
374  */
375 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags )
376 {
377     /* 
378      * Called only when one of the following is set:
379      * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
380      */
381
382     BOOL bHadOne =  wndPtr->hrgnUpdate && hRgn;
383     RECT r = {0, 0, wndPtr->rectWindow.right - wndPtr->rectWindow.left,
384                     wndPtr->rectWindow.bottom - wndPtr->rectWindow.top };
385     BOOL bChildren =  ( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) 
386                         && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
387
388     TRACE_(win)("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
389
390     if( flags & RDW_INVALIDATE )
391     {
392         if( hRgn > 1 )
393         {
394             switch( wndPtr->hrgnUpdate )
395             {
396                 default:
397                         CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
398                         /* fall through */
399                 case 0:
400                         wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate, 
401                                                              wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn, 
402                                                              &r, NULL );
403                         if( !bHadOne )
404                         {
405                             GetRgnBox( wndPtr->hrgnUpdate, &r );
406                             if( IsRectEmpty( &r ) )
407                             {
408                                 DeleteObject( wndPtr->hrgnUpdate );
409                                 wndPtr->hrgnUpdate = 0;
410                                 goto OUT;
411                             }
412                         }
413                         break;
414
415                 case 1: /* already an entire window */
416             }
417         }
418         else if( hRgn == 1 )
419         {
420             if( wndPtr->hrgnUpdate > 1 )
421                 DeleteObject( wndPtr->hrgnUpdate );
422             wndPtr->hrgnUpdate = 1;
423         }
424         else
425             hRgn = wndPtr->hrgnUpdate;  /* this is a trick that depends on code in PAINT_RedrawWindow() */
426
427         if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
428             QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
429
430         if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
431         if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
432         flags    |= RDW_FRAME;
433     }
434     else if( flags & RDW_VALIDATE )
435     {
436         if( wndPtr->hrgnUpdate )
437         {
438             if( hRgn > 1 )
439             {
440                 if( wndPtr->hrgnUpdate == 1 )
441                     wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
442
443                 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
444                     == NULLREGION )
445                     goto EMPTY;
446             }
447             else /* validate everything */
448             {
449                 if( wndPtr->hrgnUpdate > 1 )
450                 {
451 EMPTY:
452                     DeleteObject( wndPtr->hrgnUpdate );
453                 }
454                 wndPtr->hrgnUpdate = 0;
455             }
456
457             if( !wndPtr->hrgnUpdate )
458             {
459                 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
460                 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
461                     QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
462             }
463         }
464
465         if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
466         if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
467     }
468
469     /* in/validate child windows that intersect with the region if it
470      * is a valid handle. */
471
472     if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
473     {
474         if( hRgn > 1 && bChildren )
475         {
476             WND* wnd = wndPtr->child;
477             POINT ptClient = { wndPtr->rectClient.left - wndPtr->rectWindow.left,
478                                wndPtr->rectClient.top - wndPtr->rectWindow.top };
479             POINT ptTotal, prevOrigin = {0,0};
480
481             for( ptTotal.x = ptTotal.y = 0; wnd; wnd = wnd->next )
482             {
483                 if( wnd->dwStyle & WS_VISIBLE )
484                 {
485                     POINT ptOffset;
486
487                     r.left = wnd->rectWindow.left + ptClient.x;
488                     r.right = wnd->rectWindow.right + ptClient.x;
489                     r.top = wnd->rectWindow.top + ptClient.y;
490                     r.bottom = wnd->rectWindow.bottom + ptClient.y;
491
492                     ptOffset.x = r.left - prevOrigin.x; 
493                     ptOffset.y = r.top - prevOrigin.y;
494                     OffsetRect( &r, -ptTotal.x, -ptTotal.y );
495
496                     if( RectInRegion( hRgn, &r ) )
497                     {
498                         OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
499                         RDW_UpdateRgns( wnd, hRgn, flags );
500                         prevOrigin.x = r.left + ptTotal.x;
501                         prevOrigin.y = r.top + ptTotal.y;
502                         ptTotal.x += ptOffset.x;
503                         ptTotal.y += ptOffset.y;
504                     }
505                 }
506             }
507             OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
508             bChildren = 0;
509         }
510     }
511
512     /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
513
514     if( bChildren )
515     {
516         WND* wnd;
517         for( wnd = wndPtr->child; wnd; wnd = wnd->next )
518              if( wnd->dwStyle & WS_VISIBLE )
519                  RDW_UpdateRgns( wnd, hRgn, flags );
520     }
521
522 OUT:
523
524     /* Set/clear internal paint flag */
525
526     if (flags & RDW_INTERNALPAINT)
527     {
528         if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
529             QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
530         wndPtr->flags |= WIN_INTERNAL_PAINT;
531     }
532     else if (flags & RDW_NOINTERNALPAINT)
533     {
534         if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
535             QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
536         wndPtr->flags &= ~WIN_INTERNAL_PAINT;
537     }
538 }
539
540 /***********************************************************************
541  *           RDW_Paint [RedrawWindow() helper]
542  *
543  * Walks the window tree and paints/erases windows that have
544  * nonzero update regions according to redraw flags. hrgn is a scratch
545  * region passed down during recursion. Must not be 1.
546  *
547  */
548 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
549 {
550 /* NOTE: wndPtr is locked by caller.
551  * 
552  * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
553  * SendMessage() calls. This is a comment inside DefWindowProc() source
554  * from 16-bit SDK:
555  *
556  *   This message avoids lots of inter-app message traffic
557  *   by switching to the other task and continuing the
558  *   recursion there.
559  *
560  * wParam         = flags
561  * LOWORD(lParam) = hrgnClip
562  * HIWORD(lParam) = hwndSkip  (not used; always NULL)
563  *
564  */
565     HDC  hDC;
566     HWND hWnd = wndPtr->hwndSelf;
567     BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassWord(wndPtr->hwndSelf, GCW_HICON)); 
568
569       /* Erase/update the window itself ... */
570
571     TRACE_(win)("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
572
573     if (flags & RDW_UPDATENOW)
574     {
575         if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
576             SendMessage16( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
577     }
578     else if ((flags & RDW_ERASENOW) || (ex & RDW_EX_TOPFRAME))
579     {
580         UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
581         HRGN hrgnRet = WIN_UpdateNCRgn( wndPtr, hrgn, UNC_REGION | UNC_CHECK | 
582                             ((ex & RDW_EX_TOPFRAME) ? UNC_ENTIRE : 0) ); 
583         if( hrgnRet )
584         {
585             if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
586             if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
587             {
588                 if( bIcon ) dcx |= DCX_WINDOW;
589                 else 
590                 if( hrgnRet )
591                     OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left, 
592                                         wndPtr->rectWindow.top  - wndPtr->rectClient.top);
593                 else
594                     dcx &= ~DCX_INTERSECTRGN;
595                 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
596                 {
597                     if (SendMessage16( hWnd, (bIcon) ? WM_ICONERASEBKGND
598                                        : WM_ERASEBKGND, (WPARAM16)hDC, 0 ))
599                     wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
600                     ReleaseDC( hWnd, hDC );
601                 }
602             }
603         }
604     }
605
606     if( !IsWindow(hWnd) ) return hrgn;
607     ex &= ~RDW_EX_TOPFRAME;
608
609       /* ... and its child windows */
610
611     if( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) 
612         && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
613     {
614         WND** list, **ppWnd;
615
616         if( (list = WIN_BuildWinArray( wndPtr, 0, NULL )) )
617         {
618             wndPtr = NULL;
619             for (ppWnd = list; *ppWnd; ppWnd++)
620             {
621                 WIN_UpdateWndPtr(&wndPtr,*ppWnd);
622                 if (!IsWindow(wndPtr->hwndSelf)) continue;
623                     if ( (wndPtr->dwStyle & WS_VISIBLE) &&
624                          (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
625                         hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
626             }
627             WIN_ReleaseWndPtr(wndPtr);
628             WIN_ReleaseWinArray(list);
629         }
630     }
631
632     return hrgn;
633 }
634
635 /***********************************************************************
636  *           PAINT_RedrawWindow
637  *
638  */
639 BOOL PAINT_RedrawWindow( HWND hwnd, const RECT *rectUpdate,
640                            HRGN hrgnUpdate, UINT flags, UINT ex )
641 {
642     HRGN hRgn = 0;
643     RECT r, r2;
644     POINT pt;
645     WND* wndPtr;
646
647     if (!hwnd) hwnd = GetDesktopWindow();
648     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
649
650     /* check if the window or its parents are visible/not minimized */
651
652     if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
653     {
654         WIN_ReleaseWndPtr(wndPtr);
655         return TRUE; 
656     }
657
658     if (TRACE_ON(win))
659     {
660         if( hrgnUpdate )
661         {
662             GetRgnBox( hrgnUpdate, &r );
663             TRACE_(win)( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x, exflags=%04x\n", 
664                   hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags, ex);
665         }
666         else
667         {
668             if( rectUpdate )
669                 r = *rectUpdate;
670             else
671                 SetRectEmpty( &r );
672             TRACE_(win)( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x, exflags=%04x\n",
673                         hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left, 
674                         r.top, r.right, r.bottom, hrgnUpdate, flags, ex );
675         }
676     }
677
678     /* prepare an update region in window coordinates */
679
680     if( flags & RDW_FRAME )
681         r = wndPtr->rectWindow;
682     else
683         r = wndPtr->rectClient;
684
685     if( ex & RDW_EX_XYWINDOW )
686     {
687         pt.x = pt.y = 0;
688         OffsetRect( &r, -wndPtr->rectWindow.left, -wndPtr->rectWindow.top );
689     }
690     else
691     {
692         pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
693         pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
694         OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
695     }
696
697     if (flags & RDW_INVALIDATE)  /* ------------------------- Invalidate */
698     {
699         /* If the window doesn't have hrgnUpdate we leave hRgn zero
700          * and put a new region straight into wndPtr->hrgnUpdate
701          * so that RDW_UpdateRgns() won't have to do any extra work.
702          */
703
704         if( hrgnUpdate )
705         {
706             if( wndPtr->hrgnUpdate )
707                 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
708             else 
709                 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt ); 
710         }
711         else if( rectUpdate )
712         {
713             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
714             OffsetRect( &r2, pt.x, pt.y );
715
716 rect2i:
717             if( wndPtr->hrgnUpdate == 0 )
718                 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
719             else
720                 hRgn = CreateRectRgnIndirect( &r2 );
721         }
722         else /* entire window or client depending on RDW_FRAME */
723         {
724             if( flags & RDW_FRAME )
725             {
726                 if( wndPtr->hrgnUpdate )
727                     DeleteObject( wndPtr->hrgnUpdate );
728                 wndPtr->hrgnUpdate = 1;
729             }
730             else
731             {
732                 GETCLIENTRECTW( wndPtr, r2 );
733                 goto rect2i;
734             }
735         }
736     }
737     else if (flags & RDW_VALIDATE)  /* ------------------------- Validate */
738     {
739         /* In this we cannot leave with zero hRgn */
740         if( hrgnUpdate )
741         {
742             hRgn = REGION_CropRgn( hRgn, hrgnUpdate,  &r, &pt );
743             GetRgnBox( hRgn, &r2 );
744             if( IsRectEmpty( &r2 ) ) goto END;
745         }
746         else if( rectUpdate )
747         {
748             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
749                 OffsetRect( &r2, pt.x, pt.y );
750 rect2v:
751             hRgn = CreateRectRgnIndirect( &r2 );
752         }
753         else /* entire window or client depending on RDW_FRAME */
754         {
755             if( flags & RDW_FRAME ) 
756                 hRgn = 1;
757             else
758             {
759                 GETCLIENTRECTW( wndPtr, r2 );
760                 goto rect2v;
761             }
762         }
763     }
764
765     /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
766
767     RDW_UpdateRgns( wndPtr, hRgn, flags );
768
769     /* Erase/update windows, from now on hRgn is a scratch region */
770
771     hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, ex );
772
773 END:
774     if( hRgn > 1 && (hRgn != hrgnUpdate) )
775         DeleteObject(hRgn );
776     WIN_ReleaseWndPtr(wndPtr);
777     return TRUE;
778 }
779
780
781 /***********************************************************************
782  *           RedrawWindow32    (USER32.426)
783  */
784 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
785                               HRGN hrgnUpdate, UINT flags )
786 {
787     return PAINT_RedrawWindow( hwnd, rectUpdate, hrgnUpdate, flags, 0 );
788 }
789
790
791 /***********************************************************************
792  *           RedrawWindow16    (USER.290)
793  */
794 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
795                               HRGN16 hrgnUpdate, UINT16 flags )
796 {
797     if (rectUpdate)
798     {
799         RECT r;
800         CONV_RECT16TO32( rectUpdate, &r );
801         return (BOOL16)RedrawWindow( (HWND)hwnd, &r, hrgnUpdate, flags );
802     }
803     return (BOOL16)PAINT_RedrawWindow( (HWND)hwnd, NULL, 
804                                        (HRGN)hrgnUpdate, flags, 0 );
805 }
806
807
808 /***********************************************************************
809  *           UpdateWindow16   (USER.124)
810  */
811 void WINAPI UpdateWindow16( HWND16 hwnd )
812 {
813     PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_NOCHILDREN, 0 );
814 }
815
816 /***********************************************************************
817  *           UpdateWindow32   (USER32.567)
818  */
819 void WINAPI UpdateWindow( HWND hwnd )
820 {
821     PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_NOCHILDREN, 0 );
822 }
823
824 /***********************************************************************
825  *           InvalidateRgn16   (USER.126)
826  */
827 void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
828 {
829     PAINT_RedrawWindow((HWND)hwnd, NULL, (HRGN)hrgn, 
830                        RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
831 }
832
833
834 /***********************************************************************
835  *           InvalidateRgn32   (USER32.329)
836  */
837 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
838 {
839     return PAINT_RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
840 }
841
842
843 /***********************************************************************
844  *           InvalidateRect16   (USER.125)
845  */
846 void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
847 {
848     RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
849 }
850
851
852 /***********************************************************************
853  *           InvalidateRect32   (USER32.328)
854  */
855 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
856 {
857     return PAINT_RedrawWindow( hwnd, rect, 0, 
858                                RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
859 }
860
861
862 /***********************************************************************
863  *           ValidateRgn16   (USER.128)
864  */
865 void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
866 {
867     PAINT_RedrawWindow( (HWND)hwnd, NULL, (HRGN)hrgn, 
868                         RDW_VALIDATE | RDW_NOCHILDREN, 0 );
869 }
870
871
872 /***********************************************************************
873  *           ValidateRgn32   (USER32.572)
874  */
875 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
876 {
877     PAINT_RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
878 }
879
880
881 /***********************************************************************
882  *           ValidateRect16   (USER.127)
883  */
884 void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
885 {
886     RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
887 }
888
889
890 /***********************************************************************
891  *           ValidateRect32   (USER32.571)
892  */
893 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
894 {
895     PAINT_RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
896 }
897
898
899 /***********************************************************************
900  *           GetUpdateRect16   (USER.190)
901  */
902 BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
903 {
904     RECT r;
905     BOOL16 ret;
906
907     if (!rect) return GetUpdateRect( hwnd, NULL, erase );
908     ret = GetUpdateRect( hwnd, &r, erase );
909     CONV_RECT32TO16( &r, rect );
910     return ret;
911 }
912
913
914 /***********************************************************************
915  *           GetUpdateRect32   (USER32.297)
916  */
917 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
918 {
919     BOOL retvalue;
920     WND * wndPtr = WIN_FindWndPtr( hwnd );
921     if (!wndPtr) return FALSE;
922
923     if (rect)
924     {
925         if (wndPtr->hrgnUpdate > 1)
926         {
927             HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
928             if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
929             {
930                 retvalue = FALSE;
931                 goto END;
932             }
933             GetRgnBox( hrgn, rect );
934             DeleteObject( hrgn );
935             if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
936             {
937                 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
938                 {
939                     DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect,  2);
940                 }
941             }
942         }
943         else
944         if( wndPtr->hrgnUpdate == 1 )
945         {
946             GetClientRect( hwnd, rect );
947             if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
948         }
949         else 
950             SetRectEmpty( rect );
951     }
952     retvalue = (wndPtr->hrgnUpdate >= 1);
953 END:
954     WIN_ReleaseWndPtr(wndPtr);
955     return retvalue;
956 }
957
958
959 /***********************************************************************
960  *           GetUpdateRgn16   (USER.237)
961  */
962 INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
963 {
964     return GetUpdateRgn( hwnd, hrgn, erase );
965 }
966
967
968 /***********************************************************************
969  *           GetUpdateRgn    (USER32.298)
970  */
971 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
972 {
973     INT retval;
974     WND * wndPtr = WIN_FindWndPtr( hwnd );
975     if (!wndPtr) return ERROR;
976
977     if (wndPtr->hrgnUpdate == 0)
978     {
979         SetRectRgn( hrgn, 0, 0, 0, 0 );
980         retval = NULLREGION;
981         goto END;
982     }
983     else
984     if (wndPtr->hrgnUpdate == 1)
985     {
986         SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
987                                 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
988         retval = SIMPLEREGION;
989     }
990     else
991     {
992         retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
993         OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
994                          wndPtr->rectWindow.top - wndPtr->rectClient.top );
995     }
996     if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
997 END:
998     WIN_ReleaseWndPtr(wndPtr);
999     return retval;
1000 }
1001
1002
1003 /***********************************************************************
1004  *           ExcludeUpdateRgn16   (USER.238)
1005  */
1006 INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
1007 {
1008     return ExcludeUpdateRgn( hdc, hwnd );
1009 }
1010
1011
1012 /***********************************************************************
1013  *           ExcludeUpdateRgn32   (USER32.195)
1014  */
1015 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1016 {
1017     RECT rect;
1018     WND * wndPtr;
1019
1020     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1021
1022     if (wndPtr->hrgnUpdate)
1023     {
1024         INT ret;
1025         HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1026                                       wndPtr->rectWindow.top - wndPtr->rectClient.top,
1027                                       wndPtr->rectWindow.right - wndPtr->rectClient.left,
1028                                       wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1029         if( wndPtr->hrgnUpdate > 1 )
1030         {
1031             CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1032             OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left, 
1033                             wndPtr->rectWindow.top - wndPtr->rectClient.top );
1034         }
1035
1036         /* do ugly coordinate translations in dce.c */
1037
1038         ret = DCE_ExcludeRgn( hdc, wndPtr, hrgn );
1039         DeleteObject( hrgn );
1040         WIN_ReleaseWndPtr(wndPtr);
1041         return ret;
1042     } 
1043     WIN_ReleaseWndPtr(wndPtr);
1044     return GetClipBox( hdc, &rect );
1045 }
1046
1047