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