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