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