Removed a number of direct accesses to the window structure.
[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 <string.h>
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "wine/winuser16.h"
12 #include "wine/unicode.h"
13 #include "wine/server.h"
14 #include "region.h"
15 #include "user.h"
16 #include "win.h"
17 #include "queue.h"
18 #include "dce.h"
19 #include "debugtools.h"
20
21 DEFAULT_DEBUG_CHANNEL(win);
22 DECLARE_DEBUG_CHANNEL(nonclient);
23
24 /* client rect in window coordinates */
25
26 #define GETCLIENTRECTW( wnd, r )        (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
27                                         (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
28                                         (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
29                                         (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
30
31   /* PAINT_RedrawWindow() control flags */
32 #define RDW_EX_DELAY_NCPAINT    0x0020
33
34   /* WIN_UpdateNCRgn() flags */
35 #define UNC_CHECK               0x0001
36 #define UNC_ENTIRE              0x0002
37 #define UNC_REGION              0x0004
38 #define UNC_UPDATE              0x0008
39 #define UNC_DELAY_NCPAINT       0x0010
40 #define UNC_IN_BEGINPAINT       0x0020
41
42   /* Last COLOR id */
43 #define COLOR_MAX   COLOR_GRADIENTINACTIVECAPTION
44
45   /* Last CTLCOLOR id */
46 #define CTLCOLOR_MAX   CTLCOLOR_STATIC
47
48
49 /***********************************************************************
50  *           add_paint_count
51  *
52  * Add an increment (normally 1 or -1) to the current paint count of a window.
53  */
54 static void add_paint_count( HWND hwnd, int incr )
55 {
56     SERVER_START_REQ( inc_queue_paint_count )
57     {
58         req->id   = (void *)GetWindowThreadProcessId( hwnd, NULL );
59         req->incr = incr;
60         SERVER_CALL();
61     }
62     SERVER_END_REQ;
63 }
64
65
66 /***********************************************************************
67  *           WIN_HaveToDelayNCPAINT
68  *
69  * Currently, in the Wine painting mechanism, the WM_NCPAINT message
70  * is generated as soon as a region intersecting the non-client area 
71  * of a window is invalidated.
72  *
73  * This technique will work fine for all windows whose parents
74  * have the WS_CLIPCHILDREN style. When the parents have that style,
75  * they are not going to override the contents of their children.
76  * However, when the parent doesn't have that style, Windows relies
77  * on a "painter's algorithm" to display the contents of the windows.
78  * That is, windows are painted from back to front. This includes the
79  * non-client area.
80  *
81  * This method looks at the current state of a window to determine
82  * if the sending of the WM_NCPAINT message should be delayed until 
83  * the BeginPaint call.
84  *
85  * PARAMS:
86  *   wndPtr   - A Locked window pointer to the window we're
87  *              examining.
88  *   uncFlags - This is the flag passed to the WIN_UpdateNCRgn
89  *              function. This is a shortcut for the cases when
90  *              we already know when to avoid scanning all the
91  *              parents of a window. If you already know that this
92  *              window's NCPAINT should be delayed, set the 
93  *              UNC_DELAY_NCPAINT flag for this parameter. 
94  *
95  *              This shortcut behavior is implemented in the
96  *              RDW_Paint() method.
97  * 
98  */
99 static BOOL WIN_HaveToDelayNCPAINT(
100   WND* wndPtr, 
101   UINT uncFlags)
102 {
103   WND* parentWnd = NULL;
104
105   /*
106    * Test the shortcut first. (duh)
107    */
108   if (uncFlags & UNC_DELAY_NCPAINT)
109     return TRUE;
110
111   /*
112    * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
113    * method only. This is another shortcut to avoid going
114    * up the parent's chain of the window to finally
115    * figure-out that none of them has an invalid region.
116    */
117   if (uncFlags & UNC_IN_BEGINPAINT)
118     return FALSE;
119
120   /*
121    * Scan all the parents of this window to find a window
122    * that doesn't have the WS_CLIPCHILDREN style and that
123    * has an invalid region. 
124    */
125   parentWnd = WIN_LockWndPtr(wndPtr->parent);
126
127   while (parentWnd!=NULL)
128   {
129     if ( ((parentWnd->dwStyle & WS_CLIPCHILDREN) == 0) &&
130          (parentWnd->hrgnUpdate != 0) )
131     {
132       WIN_ReleaseWndPtr(parentWnd);
133       return TRUE;
134     }
135
136     WIN_UpdateWndPtr(&parentWnd, parentWnd->parent);    
137   }
138
139   WIN_ReleaseWndPtr(parentWnd);
140
141   return FALSE;
142 }
143
144 /***********************************************************************
145  *           WIN_UpdateNCRgn
146  *
147  *  Things to do:
148  *      Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
149  *      Crop hrgnUpdate to a client rect, especially if it 1.
150  *      If UNC_REGION is set return update region for the client rect.
151  *
152  *  NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
153  *        The trick is that when the returned region handle may be different from hRgn.
154  *        In this case the old hRgn must be considered gone. BUT, if the returned value
155  *        is 1 then the hRgn is preserved and RDW_Paint() will have to get 
156  *        a DC without extra clipping region.
157  */
158 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
159 {
160     RECT  r;
161     HRGN  hClip = 0;
162     HRGN  hrgnRet = 0;
163
164     TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n", 
165                       wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
166
167     /* desktop window doesn't have a nonclient area */
168     if(wnd == WIN_GetDesktop()) 
169     {
170         wnd->flags &= ~WIN_NEEDS_NCPAINT;
171         if( wnd->hrgnUpdate > 1 )
172             hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
173         else 
174         {
175             hrgnRet = wnd->hrgnUpdate;
176         }
177         WIN_ReleaseDesktop();
178         return hrgnRet;
179     }
180     WIN_ReleaseDesktop();
181
182     if ((wnd->hwndSelf == GetForegroundWindow()) &&
183         !(wnd->flags & WIN_NCACTIVATED) )
184     {
185         wnd->flags |= WIN_NCACTIVATED;
186         uncFlags |= UNC_ENTIRE; 
187     }
188
189     /*
190      * If the window's non-client area needs to be painted, 
191      */
192     if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
193          !WIN_HaveToDelayNCPAINT(wnd, uncFlags) )
194     {
195             RECT r2, r3;
196
197             wnd->flags &= ~WIN_NEEDS_NCPAINT;  
198             GETCLIENTRECTW( wnd, r );
199
200             TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n", 
201                                 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
202             if( wnd->hrgnUpdate > 1 )
203             {
204                 /* Check if update rgn overlaps with nonclient area */
205
206                 GetRgnBox( wnd->hrgnUpdate, &r2 );
207                 UnionRect( &r3, &r2, &r );
208                 if( r3.left != r.left || r3.top != r.top || 
209                     r3.right != r.right || r3.bottom != r.bottom ) /* it does */
210                 {
211                     /* crop hrgnUpdate, save old one in hClip - the only
212                      * case that places a valid region handle in hClip */
213
214                     hClip = wnd->hrgnUpdate;
215                     wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
216                     if( uncFlags & UNC_REGION ) hrgnRet = hClip;
217                 }
218
219                 if( uncFlags & UNC_CHECK )
220                 {
221                     GetRgnBox( wnd->hrgnUpdate, &r3 );
222                     if( IsRectEmpty( &r3 ) )
223                     {
224                         /* delete the update region since all invalid 
225                          * parts were in the nonclient area */
226
227                         DeleteObject( wnd->hrgnUpdate );
228                         wnd->hrgnUpdate = 0;
229                         if(!(wnd->flags & WIN_INTERNAL_PAINT))
230                             add_paint_count( wnd->hwndSelf, -1 );
231
232                         wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
233                     }
234                 }
235
236                 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
237             }
238             else 
239             if( wnd->hrgnUpdate == 1 )/* entire window */
240             {
241                 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
242                 if( uncFlags & UNC_REGION ) hrgnRet = 1;
243                 uncFlags |= UNC_ENTIRE;
244             }
245     }
246     else /* no WM_NCPAINT unless forced */
247     {
248         if( wnd->hrgnUpdate >  1 )
249         {
250 copyrgn:
251             if( uncFlags & UNC_REGION )
252                 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
253         }
254         else
255         if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
256         {
257             GETCLIENTRECTW( wnd, r ); 
258             wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
259             if( uncFlags & UNC_REGION ) hrgnRet = 1;
260         }
261     }
262
263     if(!hClip && (uncFlags & UNC_ENTIRE) )
264     {
265         /* still don't do anything if there is no nonclient area */
266         hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
267     }
268
269     if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
270     {
271         if ( hClip == hrgnRet && hrgnRet > 1 ) {
272             hClip = CreateRectRgn( 0, 0, 0, 0 );
273             CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
274         }
275
276         SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
277         if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
278             DeleteObject( hClip );
279         /*
280          * Since all Window locks are suspended while processing the WM_NCPAINT
281          * we want to make sure the window still exists before continuing.
282          */
283         if (!IsWindow(wnd->hwndSelf))
284         {
285           DeleteObject(hrgnRet);
286           hrgnRet=0;
287         }
288     }
289
290     TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
291
292     return hrgnRet;
293 }
294
295
296 /***********************************************************************
297  *              BeginPaint (USER.39)
298  */
299 HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps ) 
300 {
301     PAINTSTRUCT ps;
302
303     BeginPaint( hwnd, &ps );
304     lps->hdc            = ps.hdc;
305     lps->fErase         = ps.fErase;
306     lps->rcPaint.top    = ps.rcPaint.top;
307     lps->rcPaint.left   = ps.rcPaint.left;
308     lps->rcPaint.right  = ps.rcPaint.right;
309     lps->rcPaint.bottom = ps.rcPaint.bottom;
310     lps->fRestore       = ps.fRestore;
311     lps->fIncUpdate     = ps.fIncUpdate;
312     return lps->hdc;
313 }
314
315
316 /***********************************************************************
317  *              BeginPaint (USER32.@)
318  */
319 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
320 {
321     BOOL bIcon;
322     HRGN hrgnUpdate;
323     RECT clipRect, clientRect;
324     WND *wndPtr = WIN_FindWndPtr( hwnd );
325     if (!wndPtr) return 0;
326
327     bIcon = (wndPtr->dwStyle & WS_MINIMIZE && GetClassLongA(hwnd, GCL_HICON));
328
329     wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
330
331     /* send WM_NCPAINT and make sure hrgnUpdate is a valid rgn handle */
332     WIN_UpdateNCRgn( wndPtr, 0, UNC_UPDATE | UNC_IN_BEGINPAINT);
333
334     /*
335      * Make sure the window is still a window. All window locks are suspended
336      * when the WM_NCPAINT is sent.
337      */
338     if (!IsWindow(wndPtr->hwndSelf))
339     {
340         WIN_ReleaseWndPtr(wndPtr);
341         return 0;
342     }
343
344     if( ((hrgnUpdate = wndPtr->hrgnUpdate) != 0) || (wndPtr->flags & WIN_INTERNAL_PAINT))
345         add_paint_count( hwnd, -1 );
346
347     wndPtr->hrgnUpdate = 0;
348     wndPtr->flags &= ~WIN_INTERNAL_PAINT;
349
350     HideCaret( hwnd );
351
352     TRACE("hrgnUpdate = %04x, \n", hrgnUpdate);
353
354     if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_PARENTDC)
355     {
356         /* Don't clip the output to the update region for CS_PARENTDC window */
357         if( hrgnUpdate ) 
358             DeleteObject(hrgnUpdate);
359         lps->hdc = GetDCEx( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
360                               (bIcon ? DCX_WINDOW : 0) );
361     }
362     else
363     {
364         if( hrgnUpdate ) /* convert to client coordinates */
365             OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
366                                    wndPtr->rectWindow.top - wndPtr->rectClient.top );
367         lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN | 
368                              DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
369         /* ReleaseDC() in EndPaint() will delete the region */
370     }
371
372     TRACE("hdc = %04x\n", lps->hdc);
373
374     if (!lps->hdc)
375     {
376         WARN("GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
377         WIN_ReleaseWndPtr(wndPtr);
378         return 0;
379     }
380
381     /* It is possible that the clip box is bigger than the window itself,
382        if CS_PARENTDC flag is set. Windows never return a paint rect bigger
383        than the window itself, so we need to intersect the cliprect with
384        the window  */
385     
386     GetClientRect( hwnd, &clientRect );
387
388     GetClipBox( lps->hdc, &clipRect );
389     LPtoDP(lps->hdc, (LPPOINT)&clipRect, 2);      /* GetClipBox returns LP */
390
391     IntersectRect(&lps->rcPaint, &clientRect, &clipRect);
392     DPtoLP(lps->hdc, (LPPOINT)&lps->rcPaint, 2);  /* we must return LP */
393
394     TRACE("box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
395                     lps->rcPaint.right, lps->rcPaint.bottom );
396
397     if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
398     {
399         wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
400         lps->fErase = !SendMessageA(hwnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
401                                     (WPARAM16)lps->hdc, 0 );
402     }
403     else lps->fErase = TRUE;
404
405     WIN_ReleaseWndPtr(wndPtr);
406     return lps->hdc;
407 }
408
409
410 /***********************************************************************
411  *              EndPaint (USER.40)
412  */
413 BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
414 {
415     ReleaseDC16( hwnd, lps->hdc );
416     ShowCaret( hwnd );
417     return TRUE;
418 }
419
420
421 /***********************************************************************
422  *              EndPaint (USER32.@)
423  */
424 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
425 {
426     ReleaseDC( hwnd, lps->hdc );
427     ShowCaret( hwnd );
428     return TRUE;
429 }
430
431
432 /***********************************************************************
433  *              FillWindow (USER.324)
434  */
435 void WINAPI FillWindow16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc, HBRUSH16 hbrush )
436 {
437     RECT rect;
438     RECT16 rc16;
439     GetClientRect( hwnd, &rect );
440     DPtoLP( hdc, (LPPOINT)&rect, 2 );
441     CONV_RECT32TO16( &rect, &rc16 );
442     PaintRect16( hwndParent, hwnd, hdc, hbrush, &rc16 );
443 }
444
445
446 /***********************************************************************
447  *           PAINT_GetControlBrush
448  */
449 static HBRUSH16 PAINT_GetControlBrush( HWND hParent, HWND hWnd, HDC16 hDC, UINT16 ctlType )
450 {
451     HBRUSH16 bkgBrush = (HBRUSH16)SendMessageA( hParent, WM_CTLCOLORMSGBOX + ctlType, 
452                                                              (WPARAM)hDC, (LPARAM)hWnd );
453     if( !IsGDIObject16(bkgBrush) )
454         bkgBrush = DEFWND_ControlColor( hDC, ctlType );
455     return bkgBrush;
456 }
457
458
459 /***********************************************************************
460  *              PaintRect (USER.325)
461  */
462 void WINAPI PaintRect16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc,
463                        HBRUSH16 hbrush, const RECT16 *rect)
464 {
465     if( hbrush <= CTLCOLOR_MAX ) 
466     {
467         if( hwndParent )
468             hbrush = PAINT_GetControlBrush( hwndParent, hwnd, hdc, (UINT16)hbrush );
469         else 
470             return;
471     }
472     if( hbrush ) 
473         FillRect16( hdc, rect, hbrush );
474 }
475
476
477 /***********************************************************************
478  *              GetControlBrush (USER.326)
479  */
480 HBRUSH16 WINAPI GetControlBrush16( HWND16 hwnd, HDC16 hdc, UINT16 ctlType )
481 {
482     if (ctlType <= CTLCOLOR_MAX)
483     {
484         HWND16 parent = GetParent16( hwnd );
485         if (!parent) parent = hwnd;
486         return PAINT_GetControlBrush( parent, hwnd, hdc, ctlType );
487     }
488     return 0;
489 }
490
491
492 /***********************************************************************
493  *              RDW_ValidateParent [RDW_UpdateRgns() helper] 
494  *
495  *  Validate the portions of parents that are covered by a validated child
496  *  wndPtr = child
497  */
498 static void RDW_ValidateParent(WND *wndChild)
499 {
500     WND *wndParent = WIN_LockWndPtr(wndChild->parent);
501     WND *wndDesktop = WIN_GetDesktop();
502     HRGN hrg;
503
504     if (wndChild->hrgnUpdate == 1 ) {
505         RECT r;
506         r.left = 0;
507         r.top = 0;
508         r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
509         r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
510         hrg = CreateRectRgnIndirect( &r );
511     } else
512         hrg = wndChild->hrgnUpdate;
513
514     while ((wndParent) && (wndParent != wndDesktop) ) {
515         if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
516         {
517             if (wndParent->hrgnUpdate != 0)
518             {
519                 POINT ptOffset;
520                 RECT rect, rectParent;
521                 if( wndParent->hrgnUpdate == 1 )
522                 {
523                    RECT r;
524
525                    r.left = 0;
526                    r.top = 0;
527                    r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
528                    r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
529
530                    wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
531                 }
532                 /* we must offset the child region by the offset of the child rect in the parent */
533                 GetWindowRect(wndParent->hwndSelf, &rectParent);
534                 GetWindowRect(wndChild->hwndSelf, &rect);
535                 ptOffset.x = rect.left - rectParent.left;
536                 ptOffset.y = rect.top - rectParent.top;
537                 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
538                 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
539                 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
540             }
541         }
542         WIN_UpdateWndPtr(&wndParent, wndParent->parent);    
543     }
544     if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
545     WIN_ReleaseWndPtr(wndParent);
546     WIN_ReleaseDesktop();
547 }
548
549 /***********************************************************************
550  *              RDW_UpdateRgns [RedrawWindow() helper] 
551  *
552  *  Walks the window tree and adds/removes parts of the hRgn to/from update
553  *  regions of windows that overlap it. Also, manages internal paint flags.
554  *
555  *  NOTE: Walks the window tree so the caller must lock it.
556  *        MUST preserve hRgn (can modify but then has to restore).
557  */
558 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
559 {
560     /* 
561      * Called only when one of the following is set:
562      * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
563      */
564
565     BOOL bHadOne =  wndPtr->hrgnUpdate && hRgn;
566     BOOL bChildren =  ( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) 
567                         && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
568     RECT r;
569
570     r.left = 0;
571     r.top = 0;
572     r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
573     r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
574
575     TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
576
577     if( flags & RDW_INVALIDATE )
578     {
579         if( hRgn > 1 )
580         {
581             switch( wndPtr->hrgnUpdate )
582             {
583                 default:
584                         CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
585                         /* fall through */
586                 case 0:
587                         wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate, 
588                                                              wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn, 
589                                                              &r, NULL );
590                         if( !bHadOne )
591                         {
592                             GetRgnBox( wndPtr->hrgnUpdate, &r );
593                             if( IsRectEmpty( &r ) )
594                             {
595                                 DeleteObject( wndPtr->hrgnUpdate );
596                                 wndPtr->hrgnUpdate = 0;
597                                 goto end;
598                             }
599                         }
600                         break;
601                 case 1: /* already an entire window */
602                         break;
603             }
604         }
605         else if( hRgn == 1 )
606         {
607             if( wndPtr->hrgnUpdate > 1 )
608                 DeleteObject( wndPtr->hrgnUpdate );
609             wndPtr->hrgnUpdate = 1;
610         }
611         else
612             hRgn = wndPtr->hrgnUpdate;  /* this is a trick that depends on code in PAINT_RedrawWindow() */
613
614         if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
615             add_paint_count( wndPtr->hwndSelf, 1 );
616
617         if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
618         if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
619         flags    |= RDW_FRAME;
620     }
621     else if( flags & RDW_VALIDATE )
622     {
623         if( wndPtr->hrgnUpdate )
624         {
625             if( hRgn > 1 )
626             {
627                 if( wndPtr->hrgnUpdate == 1 )
628                     wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
629
630                 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
631                     == NULLREGION )
632                 {
633                     DeleteObject( wndPtr->hrgnUpdate );
634                     wndPtr->hrgnUpdate = 0;
635                 }
636             }
637             else /* validate everything */
638             {
639                 if( wndPtr->hrgnUpdate > 1 ) DeleteObject( wndPtr->hrgnUpdate );
640                 wndPtr->hrgnUpdate = 0;
641             }
642
643             if( !wndPtr->hrgnUpdate )
644             {
645                 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
646                 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
647                     add_paint_count( wndPtr->hwndSelf, -1 );
648             }
649         }
650
651         if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
652         if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
653
654     }
655
656     if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
657         RDW_ValidateParent(wndPtr); /* validate parent covered by region */
658
659     /* in/validate child windows that intersect with the region if it
660      * is a valid handle. */
661
662     if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
663     {
664         if( hRgn > 1 && bChildren )
665         {
666             WND* wnd = wndPtr->child;
667             POINT ptTotal, prevOrigin = {0,0};
668             POINT ptClient;
669
670             ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
671             ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
672
673             for( ptTotal.x = ptTotal.y = 0; wnd; wnd = wnd->next )
674             {
675                 if( wnd->dwStyle & WS_VISIBLE )
676                 {
677                     POINT ptOffset;
678
679                     r.left = wnd->rectWindow.left + ptClient.x;
680                     r.right = wnd->rectWindow.right + ptClient.x;
681                     r.top = wnd->rectWindow.top + ptClient.y;
682                     r.bottom = wnd->rectWindow.bottom + ptClient.y;
683
684                     ptOffset.x = r.left - prevOrigin.x; 
685                     ptOffset.y = r.top - prevOrigin.y;
686                     OffsetRect( &r, -ptTotal.x, -ptTotal.y );
687
688                     if( RectInRegion( hRgn, &r ) )
689                     {
690                         OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
691                         RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
692                         prevOrigin.x = r.left + ptTotal.x;
693                         prevOrigin.y = r.top + ptTotal.y;
694                         ptTotal.x += ptOffset.x;
695                         ptTotal.y += ptOffset.y;
696                     }
697                 }
698             }
699             OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
700             bChildren = 0;
701         }
702     }
703
704     /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
705
706     if( bChildren )
707     {
708         WND* wnd;
709         for( wnd = wndPtr->child; wnd; wnd = wnd->next )
710              if( wnd->dwStyle & WS_VISIBLE )
711                  RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
712     }
713
714 end:
715
716     /* Set/clear internal paint flag */
717
718     if (flags & RDW_INTERNALPAINT)
719     {
720         if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
721             add_paint_count( wndPtr->hwndSelf, 1 );
722         wndPtr->flags |= WIN_INTERNAL_PAINT;
723     }
724     else if (flags & RDW_NOINTERNALPAINT)
725     {
726         if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
727             add_paint_count( wndPtr->hwndSelf, -1 );
728         wndPtr->flags &= ~WIN_INTERNAL_PAINT;
729     }
730 }
731
732 /***********************************************************************
733  *           RDW_Paint [RedrawWindow() helper]
734  *
735  * Walks the window tree and paints/erases windows that have
736  * nonzero update regions according to redraw flags. hrgn is a scratch
737  * region passed down during recursion. Must not be 1.
738  *
739  */
740 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
741 {
742 /* NOTE: wndPtr is locked by caller.
743  * 
744  * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
745  * SendMessage() calls. This is a comment inside DefWindowProc() source
746  * from 16-bit SDK:
747  *
748  *   This message avoids lots of inter-app message traffic
749  *   by switching to the other task and continuing the
750  *   recursion there.
751  *
752  * wParam         = flags
753  * LOWORD(lParam) = hrgnClip
754  * HIWORD(lParam) = hwndSkip  (not used; always NULL)
755  *
756  */
757     HDC  hDC;
758     HWND hWnd = wndPtr->hwndSelf;
759     BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
760
761       /* Erase/update the window itself ... */
762
763     TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
764
765     /*
766      * Check if this window should delay it's processing of WM_NCPAINT.
767      * See WIN_HaveToDelayNCPAINT for a description of the mechanism
768      */
769     if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr, 0) )
770         ex |= RDW_EX_DELAY_NCPAINT;
771
772     if (flags & RDW_UPDATENOW)
773     {
774         if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
775             SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
776     }
777     else if (flags & RDW_ERASENOW)
778     {
779         UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
780         HRGN hrgnRet;
781
782         hrgnRet = WIN_UpdateNCRgn(wndPtr, 
783                                   hrgn, 
784                                   UNC_REGION | UNC_CHECK | 
785                                   ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) ); 
786
787         if( hrgnRet )
788         {
789             if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
790             if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
791             {
792                 if( bIcon ) dcx |= DCX_WINDOW;
793                 else 
794                 if( hrgnRet )
795                     OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left, 
796                                         wndPtr->rectWindow.top  - wndPtr->rectClient.top);
797                 else
798                     dcx &= ~DCX_INTERSECTRGN;
799                 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
800                 {
801                     if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
802                                       (WPARAM)hDC, 0 ))
803                     wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
804                     ReleaseDC( hWnd, hDC );
805                 }
806             }
807         }
808     }
809
810     if( !IsWindow(hWnd) ) return hrgn;
811
812       /* ... and its child windows */
813
814     if( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) 
815         && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
816     {
817         HWND *list, *phwnd;
818
819         if( (list = WIN_BuildWinArray( wndPtr->hwndSelf )) )
820         {
821             for (phwnd = list; *phwnd; phwnd++)
822             {
823                 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
824                 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
825                      (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
826                     hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
827                 WIN_ReleaseWndPtr(wndPtr);
828             }
829             WIN_ReleaseWinArray(list);
830         }
831     }
832
833     return hrgn;
834 }
835
836
837 /***********************************************************************
838  *              RedrawWindow (USER32.@)
839  */
840 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
841                               HRGN hrgnUpdate, UINT flags )
842 {
843     HRGN hRgn = 0;
844     RECT r, r2;
845     POINT pt;
846     WND* wndPtr;
847
848     if (!hwnd) hwnd = GetDesktopWindow();
849     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
850
851     /* check if the window or its parents are visible/not minimized */
852
853     if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
854     {
855         WIN_ReleaseWndPtr(wndPtr);
856         return TRUE; 
857     }
858
859     if (TRACE_ON(win))
860     {
861         if( hrgnUpdate )
862         {
863             GetRgnBox( hrgnUpdate, &r );
864             TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
865                   hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
866         }
867         else
868         {
869             if( rectUpdate )
870                 r = *rectUpdate;
871             else
872                 SetRectEmpty( &r );
873             TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
874                         hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left, 
875                         r.top, r.right, r.bottom, hrgnUpdate, flags );
876         }
877     }
878
879     /* prepare an update region in window coordinates */
880
881     if( flags & RDW_FRAME )
882         r = wndPtr->rectWindow;
883     else
884         r = wndPtr->rectClient;
885
886     pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
887     pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
888     OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
889
890     if (flags & RDW_INVALIDATE)  /* ------------------------- Invalidate */
891     {
892         /* If the window doesn't have hrgnUpdate we leave hRgn zero
893          * and put a new region straight into wndPtr->hrgnUpdate
894          * so that RDW_UpdateRgns() won't have to do any extra work.
895          */
896
897         if( hrgnUpdate )
898         {
899             if( wndPtr->hrgnUpdate )
900                 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
901             else 
902                 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt ); 
903         }
904         else if( rectUpdate )
905         {
906             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
907             OffsetRect( &r2, pt.x, pt.y );
908
909 rect2i:
910             if( wndPtr->hrgnUpdate == 0 )
911                 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
912             else
913                 hRgn = CreateRectRgnIndirect( &r2 );
914         }
915         else /* entire window or client depending on RDW_FRAME */
916         {
917             if( flags & RDW_FRAME )
918             {
919                 if( wndPtr->hrgnUpdate )
920                     DeleteObject( wndPtr->hrgnUpdate );
921                 wndPtr->hrgnUpdate = 1;
922             }
923             else
924             {
925                 GETCLIENTRECTW( wndPtr, r2 );
926                 goto rect2i;
927             }
928         }
929     }
930     else if (flags & RDW_VALIDATE)  /* ------------------------- Validate */
931     {
932         /* In this we cannot leave with zero hRgn */
933         if( hrgnUpdate )
934         {
935             hRgn = REGION_CropRgn( hRgn, hrgnUpdate,  &r, &pt );
936             GetRgnBox( hRgn, &r2 );
937             if( IsRectEmpty( &r2 ) ) goto END;
938         }
939         else if( rectUpdate )
940         {
941             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
942                 OffsetRect( &r2, pt.x, pt.y );
943             hRgn = CreateRectRgnIndirect( &r2 );
944         }
945         else /* entire window or client depending on RDW_FRAME */
946         {
947             if( flags & RDW_FRAME ) 
948                 hRgn = 1;
949             else
950             {
951                 GETCLIENTRECTW( wndPtr, r2 );
952                 hRgn = CreateRectRgnIndirect( &r2 );
953             }
954         }
955     }
956
957     /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
958
959     RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
960
961     /* Erase/update windows, from now on hRgn is a scratch region */
962
963     hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
964
965 END:
966     if( hRgn > 1 && (hRgn != hrgnUpdate) )
967         DeleteObject(hRgn );
968     WIN_ReleaseWndPtr(wndPtr);
969     return TRUE;
970 }
971
972
973 /***********************************************************************
974  *              RedrawWindow (USER.290)
975  */
976 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
977                               HRGN16 hrgnUpdate, UINT16 flags )
978 {
979     if (rectUpdate)
980     {
981         RECT r;
982         CONV_RECT16TO32( rectUpdate, &r );
983         return (BOOL16)RedrawWindow( (HWND)hwnd, &r, hrgnUpdate, flags );
984     }
985     return RedrawWindow( hwnd, NULL, hrgnUpdate, flags );
986 }
987
988
989 /***********************************************************************
990  *              UpdateWindow (USER.124)
991  */
992 void WINAPI UpdateWindow16( HWND16 hwnd )
993 {
994     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
995 }
996
997 /***********************************************************************
998  *              UpdateWindow (USER32.@)
999  */
1000 void WINAPI UpdateWindow( HWND hwnd )
1001 {
1002     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1003 }
1004
1005 /***********************************************************************
1006  *              InvalidateRgn (USER.126)
1007  */
1008 void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1009 {
1010     RedrawWindow((HWND)hwnd, NULL, (HRGN)hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1011 }
1012
1013
1014 /***********************************************************************
1015  *              InvalidateRgn (USER32.@)
1016  */
1017 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1018 {
1019     return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1020 }
1021
1022
1023 /***********************************************************************
1024  *              InvalidateRect (USER.125)
1025  */
1026 void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
1027 {
1028     RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1029 }
1030
1031
1032 /***********************************************************************
1033  *              InvalidateRect (USER32.@)
1034  */
1035 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1036 {
1037     return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1038 }
1039
1040
1041 /***********************************************************************
1042  *              ValidateRgn (USER.128)
1043  */
1044 void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
1045 {
1046     RedrawWindow( (HWND)hwnd, NULL, (HRGN)hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
1047 }
1048
1049
1050 /***********************************************************************
1051  *              ValidateRgn (USER32.@)
1052  */
1053 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1054 {
1055     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
1056 }
1057
1058
1059 /***********************************************************************
1060  *              ValidateRect (USER.127)
1061  */
1062 void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
1063 {
1064     RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1065 }
1066
1067
1068 /***********************************************************************
1069  *              ValidateRect (USER32.@)
1070  */
1071 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1072 {
1073     RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1074 }
1075
1076
1077 /***********************************************************************
1078  *              GetUpdateRect (USER.190)
1079  */
1080 BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
1081 {
1082     RECT r;
1083     BOOL16 ret;
1084
1085     if (!rect) return GetUpdateRect( hwnd, NULL, erase );
1086     ret = GetUpdateRect( hwnd, &r, erase );
1087     CONV_RECT32TO16( &r, rect );
1088     return ret;
1089 }
1090
1091
1092 /***********************************************************************
1093  *              GetUpdateRect (USER32.@)
1094  */
1095 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1096 {
1097     BOOL retvalue;
1098     WND * wndPtr = WIN_FindWndPtr( hwnd );
1099     if (!wndPtr) return FALSE;
1100
1101     if (rect)
1102     {
1103         if (wndPtr->hrgnUpdate > 1)
1104         {
1105             HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
1106             if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
1107             {
1108                 retvalue = FALSE;
1109                 goto END;
1110             }
1111             GetRgnBox( hrgn, rect );
1112             DeleteObject( hrgn );
1113             if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
1114             {
1115                 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
1116                 {
1117                     DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect,  2);
1118                 }
1119             }
1120         }
1121         else
1122         if( wndPtr->hrgnUpdate == 1 )
1123         {
1124             GetClientRect( hwnd, rect );
1125             if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
1126         }
1127         else 
1128             SetRectEmpty( rect );
1129     }
1130     retvalue = (wndPtr->hrgnUpdate >= 1);
1131 END:
1132     WIN_ReleaseWndPtr(wndPtr);
1133     return retvalue;
1134 }
1135
1136
1137 /***********************************************************************
1138  *              GetUpdateRgn (USER.237)
1139  */
1140 INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1141 {
1142     return GetUpdateRgn( hwnd, hrgn, erase );
1143 }
1144
1145
1146 /***********************************************************************
1147  *              GetUpdateRgn (USER32.@)
1148  */
1149 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1150 {
1151     INT retval;
1152     WND * wndPtr = WIN_FindWndPtr( hwnd );
1153     if (!wndPtr) return ERROR;
1154
1155     if (wndPtr->hrgnUpdate == 0)
1156     {
1157         SetRectRgn( hrgn, 0, 0, 0, 0 );
1158         retval = NULLREGION;
1159         goto END;
1160     }
1161     else
1162     if (wndPtr->hrgnUpdate == 1)
1163     {
1164         SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1165                                 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1166         retval = SIMPLEREGION;
1167     }
1168     else
1169     {
1170         retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1171         OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1172                          wndPtr->rectWindow.top - wndPtr->rectClient.top );
1173     }
1174     if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1175 END:
1176     WIN_ReleaseWndPtr(wndPtr);
1177     return retval;
1178 }
1179
1180
1181 /***********************************************************************
1182  *              ExcludeUpdateRgn (USER.238)
1183  */
1184 INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
1185 {
1186     return ExcludeUpdateRgn( hdc, hwnd );
1187 }
1188
1189
1190 /***********************************************************************
1191  *              ExcludeUpdateRgn (USER32.@)
1192  */
1193 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1194 {
1195     RECT rect;
1196     WND * wndPtr;
1197
1198     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1199
1200     if (wndPtr->hrgnUpdate)
1201     {
1202         INT ret;
1203         HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1204                                       wndPtr->rectWindow.top - wndPtr->rectClient.top,
1205                                       wndPtr->rectWindow.right - wndPtr->rectClient.left,
1206                                       wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1207         if( wndPtr->hrgnUpdate > 1 )
1208         {
1209             CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1210             OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left, 
1211                             wndPtr->rectWindow.top - wndPtr->rectClient.top );
1212         }
1213
1214         /* do ugly coordinate translations in dce.c */
1215
1216         ret = DCE_ExcludeRgn( hdc, wndPtr, hrgn );
1217         DeleteObject( hrgn );
1218         WIN_ReleaseWndPtr(wndPtr);
1219         return ret;
1220     } 
1221     WIN_ReleaseWndPtr(wndPtr);
1222     return GetClipBox( hdc, &rect );
1223 }
1224
1225
1226
1227 /***********************************************************************
1228  *              FillRect (USER.81)
1229  * NOTE
1230  *   The Win16 variant doesn't support special color brushes like
1231  *   the Win32 one, despite the fact that Win16, as well as Win32,
1232  *   supports special background brushes for a window class.
1233  */
1234 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1235 {
1236     HBRUSH prevBrush;
1237
1238     /* coordinates are logical so we cannot fast-check 'rect',
1239      * it will be done later in the PatBlt().
1240      */
1241
1242     if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1243     PatBlt( hdc, rect->left, rect->top,
1244               rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1245     SelectObject( hdc, prevBrush );
1246     return 1;
1247 }
1248
1249
1250 /***********************************************************************
1251  *              FillRect (USER32.@)
1252  */
1253 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1254 {
1255     HBRUSH prevBrush;
1256
1257     if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1258         hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1259     }
1260
1261     if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1262     PatBlt( hdc, rect->left, rect->top,
1263               rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1264     SelectObject( hdc, prevBrush );
1265     return 1;
1266 }
1267
1268
1269 /***********************************************************************
1270  *              InvertRect (USER.82)
1271  */
1272 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1273 {
1274     PatBlt( hdc, rect->left, rect->top,
1275               rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1276 }
1277
1278
1279 /***********************************************************************
1280  *              InvertRect (USER32.@)
1281  */
1282 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1283 {
1284     return PatBlt( hdc, rect->left, rect->top,
1285                      rect->right - rect->left, rect->bottom - rect->top, 
1286                      DSTINVERT );
1287 }
1288
1289
1290 /***********************************************************************
1291  *              FrameRect (USER32.@)
1292  */
1293 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1294 {
1295     HBRUSH prevBrush;
1296     RECT r = *rect;
1297
1298     if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1299     if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1300     
1301     PatBlt( hdc, r.left, r.top, 1,
1302               r.bottom - r.top, PATCOPY );
1303     PatBlt( hdc, r.right - 1, r.top, 1,
1304               r.bottom - r.top, PATCOPY );
1305     PatBlt( hdc, r.left, r.top,
1306               r.right - r.left, 1, PATCOPY );
1307     PatBlt( hdc, r.left, r.bottom - 1,
1308               r.right - r.left, 1, PATCOPY );
1309
1310     SelectObject( hdc, prevBrush );
1311     return TRUE;
1312 }
1313
1314
1315 /***********************************************************************
1316  *              FrameRect (USER.83)
1317  */
1318 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1319 {
1320     RECT rect;
1321     CONV_RECT16TO32( rect16, &rect );
1322     return FrameRect( hdc, &rect, hbrush );
1323 }
1324
1325
1326 /***********************************************************************
1327  *              DrawFocusRect (USER.466)
1328  */
1329 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1330 {
1331     RECT rect32;
1332     CONV_RECT16TO32( rc, &rect32 );
1333     DrawFocusRect( hdc, &rect32 );
1334 }
1335
1336
1337 /***********************************************************************
1338  *              DrawFocusRect (USER32.@)
1339  *
1340  * FIXME: PatBlt(PATINVERT) with background brush.
1341  */
1342 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1343 {
1344     HBRUSH hOldBrush;
1345     HPEN hOldPen, hNewPen;
1346     INT oldDrawMode, oldBkMode;
1347
1348     hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1349     hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1350     hOldPen = SelectObject(hdc, hNewPen);
1351     oldDrawMode = SetROP2(hdc, R2_XORPEN);
1352     oldBkMode = SetBkMode(hdc, TRANSPARENT);
1353
1354     Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1355
1356     SetBkMode(hdc, oldBkMode);
1357     SetROP2(hdc, oldDrawMode);
1358     SelectObject(hdc, hOldPen);
1359     DeleteObject(hNewPen);
1360     SelectObject(hdc, hOldBrush);
1361
1362     return TRUE;
1363 }
1364
1365 /**********************************************************************
1366  *              DrawAnimatedRects (USER.448)
1367  */
1368 BOOL16 WINAPI DrawAnimatedRects16( HWND16 hwnd, INT16 idAni,
1369                                    const RECT16* lprcFrom,
1370                                    const RECT16* lprcTo )
1371 {
1372     RECT rcFrom32, rcTo32;
1373
1374     rcFrom32.left       = (INT)lprcFrom->left;
1375     rcFrom32.top        = (INT)lprcFrom->top;
1376     rcFrom32.right      = (INT)lprcFrom->right;
1377     rcFrom32.bottom     = (INT)lprcFrom->bottom;
1378
1379     rcTo32.left         = (INT)lprcTo->left;
1380     rcTo32.top          = (INT)lprcTo->top;
1381     rcTo32.right        = (INT)lprcTo->right;
1382     rcTo32.bottom       = (INT)lprcTo->bottom;
1383
1384     return DrawAnimatedRects((HWND)hwnd, (INT)idAni, &rcFrom32, &rcTo32);
1385 }
1386
1387
1388 /**********************************************************************
1389  *              DrawAnimatedRects (USER32.@)
1390  */
1391 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1392                                    const RECT* lprcFrom,
1393                                    const RECT* lprcTo )
1394 {
1395     FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1396     return TRUE;
1397 }
1398
1399
1400 /**********************************************************************
1401  *          PAINTING_DrawStateJam
1402  *
1403  * Jams in the requested type in the dc
1404  */
1405 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1406                                   DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1407                                   LPRECT rc, UINT dtflags, BOOL unicode )
1408 {
1409     HDC memdc;
1410     HBITMAP hbmsave;
1411     BOOL retval;
1412     INT cx = rc->right - rc->left;
1413     INT cy = rc->bottom - rc->top;
1414     
1415     switch(opcode)
1416     {
1417     case DST_TEXT:
1418     case DST_PREFIXTEXT:
1419         if(unicode)
1420             return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1421         else
1422             return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1423
1424     case DST_ICON:
1425         return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1426
1427     case DST_BITMAP:
1428         memdc = CreateCompatibleDC(hdc);
1429         if(!memdc) return FALSE;
1430         hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1431         if(!hbmsave) 
1432         {
1433             DeleteDC(memdc);
1434             return FALSE;
1435         }
1436         retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1437         SelectObject(memdc, hbmsave);
1438         DeleteDC(memdc);
1439         return retval;
1440             
1441     case DST_COMPLEX:
1442         if(func) {
1443             BOOL bRet;
1444             /* DRAWSTATEPROC assumes that it draws at the center of coordinates  */
1445
1446             OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1447             bRet = func(hdc, lp, wp, cx, cy);
1448             /* Restore origin */
1449             OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1450             return bRet;
1451         } else
1452             return FALSE;
1453     }
1454     return FALSE;
1455 }
1456
1457 /**********************************************************************
1458  *      PAINTING_DrawState()
1459  */
1460 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1461                                INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1462 {
1463     HBITMAP hbm, hbmsave;
1464     HFONT hfsave;
1465     HBRUSH hbsave, hbrtmp = 0;
1466     HDC memdc;
1467     RECT rc;
1468     UINT dtflags = DT_NOCLIP;
1469     COLORREF fg, bg;
1470     UINT opcode = flags & 0xf;
1471     INT len = wp;
1472     BOOL retval, tmp;
1473
1474     if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len)    /* The string is '\0' terminated */
1475     {
1476         if(unicode)
1477             len = strlenW((LPWSTR)lp);
1478         else
1479             len = strlen((LPSTR)lp);
1480     }
1481
1482     /* Find out what size the image has if not given by caller */
1483     if(!cx || !cy)
1484     {
1485         SIZE s;
1486         CURSORICONINFO *ici;
1487         BITMAP bm;
1488
1489         switch(opcode)
1490         {
1491         case DST_TEXT:
1492         case DST_PREFIXTEXT:
1493             if(unicode)
1494                 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1495             else
1496                 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1497             if(!retval) return FALSE;
1498             break;
1499             
1500         case DST_ICON:
1501             ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1502             if(!ici) return FALSE;
1503             s.cx = ici->nWidth;
1504             s.cy = ici->nHeight;
1505             GlobalUnlock16((HGLOBAL16)lp);
1506             break;            
1507
1508         case DST_BITMAP:
1509             if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1510                 return FALSE;
1511             s.cx = bm.bmWidth;
1512             s.cy = bm.bmHeight;
1513             break;
1514             
1515         case DST_COMPLEX: /* cx and cy must be set in this mode */
1516             return FALSE;
1517         }
1518                     
1519         if(!cx) cx = s.cx;
1520         if(!cy) cy = s.cy;
1521     }
1522
1523     rc.left   = x;
1524     rc.top    = y;
1525     rc.right  = x + cx;
1526     rc.bottom = y + cy;
1527
1528     if(flags & DSS_RIGHT)    /* This one is not documented in the win32.hlp file */
1529         dtflags |= DT_RIGHT;
1530     if(opcode == DST_TEXT)
1531         dtflags |= DT_NOPREFIX;
1532
1533     /* For DSS_NORMAL we just jam in the image and return */
1534     if((flags & 0x7ff0) == DSS_NORMAL)
1535     {
1536         return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1537     }
1538
1539     /* For all other states we need to convert the image to B/W in a local bitmap */
1540     /* before it is displayed */
1541     fg = SetTextColor(hdc, RGB(0, 0, 0));
1542     bg = SetBkColor(hdc, RGB(255, 255, 255));
1543     hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1544     memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1545     retval = FALSE; /* assume failure */
1546     
1547     /* From here on we must use "goto cleanup" when something goes wrong */
1548     hbm     = CreateBitmap(cx, cy, 1, 1, NULL);
1549     if(!hbm) goto cleanup;
1550     memdc   = CreateCompatibleDC(hdc);
1551     if(!memdc) goto cleanup;
1552     hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1553     if(!hbmsave) goto cleanup;
1554     rc.left = rc.top = 0;
1555     rc.right = cx;
1556     rc.bottom = cy;
1557     if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1558     SetBkColor(memdc, RGB(255, 255, 255));
1559     SetTextColor(memdc, RGB(0, 0, 0));
1560     hfsave  = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1561
1562     /* DST_COMPLEX may draw text as well,
1563      * so we must be sure that correct font is selected
1564      */
1565     if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1566     tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1567     if(hfsave) SelectObject(memdc, hfsave);
1568     if(!tmp) goto cleanup;
1569     
1570     /* This state cause the image to be dithered */
1571     if(flags & DSS_UNION)
1572     {
1573         hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1574         if(!hbsave) goto cleanup;
1575         tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1576         SelectObject(memdc, hbsave);
1577         if(!tmp) goto cleanup;
1578     }
1579
1580     if (flags & DSS_DISABLED)
1581        hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1582     else if (flags & DSS_DEFAULT)
1583        hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1584
1585     /* Draw light or dark shadow */
1586     if (flags & (DSS_DISABLED|DSS_DEFAULT))
1587     {
1588        if(!hbrtmp) goto cleanup;
1589        hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1590        if(!hbsave) goto cleanup;
1591        if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1592        SelectObject(hdc, hbsave);
1593        DeleteObject(hbrtmp);
1594        hbrtmp = 0;
1595     }
1596
1597     if (flags & DSS_DISABLED)
1598     {
1599        hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1600        if(!hbrtmp) goto cleanup;
1601     }
1602     else if (!hbr)
1603     {
1604        hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1605     }
1606
1607     hbsave = (HBRUSH)SelectObject(hdc, hbr);
1608     
1609     if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1610     
1611     retval = TRUE; /* We succeeded */
1612     
1613 cleanup:    
1614     SetTextColor(hdc, fg);
1615     SetBkColor(hdc, bg);
1616
1617     if(hbsave)  SelectObject(hdc, hbsave);
1618     if(hbmsave) SelectObject(memdc, hbmsave);
1619     if(hbrtmp)  DeleteObject(hbrtmp);
1620     if(hbm)     DeleteObject(hbm);
1621     if(memdc)   DeleteDC(memdc);
1622
1623     return retval;
1624 }
1625
1626 /**********************************************************************
1627  *              DrawStateA (USER32.@)
1628  */
1629 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1630                    DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1631                    INT x, INT y, INT cx, INT cy, UINT flags)
1632 {
1633     return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1634 }
1635
1636 /**********************************************************************
1637  *              DrawStateW (USER32.@)
1638  */
1639 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1640                    DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1641                    INT x, INT y, INT cx, INT cy, UINT flags)
1642 {
1643     return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1644 }
1645
1646 /***********************************************************************
1647  *              SelectPalette (USER.282)
1648  */
1649 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1650                                    BOOL16 bForceBackground )
1651 {
1652     WORD wBkgPalette = 1;
1653
1654     if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1655     {
1656         HWND hwnd = WindowFromDC( hDC );
1657         if (hwnd)
1658         {
1659             HWND hForeground = GetForegroundWindow();
1660             /* set primary palette if it's related to current active */
1661             if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1662         }
1663     }
1664     return GDISelectPalette16( hDC, hPal, wBkgPalette);
1665 }
1666
1667
1668 /***********************************************************************
1669  *              RealizePalette (USER.283)
1670  */
1671 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1672 {
1673     UINT16 realized = GDIRealizePalette16( hDC );
1674
1675     /* do not send anything if no colors were changed */
1676     if (realized && IsDCCurrentPalette16( hDC ))
1677     {
1678         /* send palette change notification */
1679         HWND hWnd = WindowFromDC( hDC );
1680         if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
1681     }
1682     return realized;
1683 }
1684
1685
1686 /***********************************************************************
1687  *              UserRealizePalette (USER32.@)
1688  */
1689 UINT WINAPI UserRealizePalette( HDC hDC )
1690 {
1691     return RealizePalette16( hDC );
1692 }