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