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