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