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