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