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