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