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