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