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