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