Moved MsgWaitForMultipleObjects call out of the window lock.
[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 && !(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 && !(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     /* process pending events and messages before painting */
692     if (flags & RDW_UPDATENOW)
693         MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
694
695     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
696     if (TRACE_ON(win))
697     {
698         if( hrgnUpdate )
699         {
700             GetRgnBox( hrgnUpdate, &r );
701             TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
702                   hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
703         }
704         else
705         {
706             if( rectUpdate )
707                 r = *rectUpdate;
708             else
709                 SetRectEmpty( &r );
710             TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
711                         hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
712                         r.top, r.right, r.bottom, hrgnUpdate, flags );
713         }
714     }
715
716     /* prepare an update region in window coordinates */
717
718     if( flags & RDW_FRAME )
719         r = wndPtr->rectWindow;
720     else
721         r = wndPtr->rectClient;
722
723     pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
724     pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
725     OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
726
727     if (flags & RDW_INVALIDATE)  /* ------------------------- Invalidate */
728     {
729         /* If the window doesn't have hrgnUpdate we leave hRgn zero
730          * and put a new region straight into wndPtr->hrgnUpdate
731          * so that RDW_UpdateRgns() won't have to do any extra work.
732          */
733
734         if( hrgnUpdate )
735         {
736             if( wndPtr->hrgnUpdate )
737                 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
738             else
739                 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
740         }
741         else if( rectUpdate )
742         {
743             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
744             OffsetRect( &r2, pt.x, pt.y );
745             if( wndPtr->hrgnUpdate == 0 )
746                 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
747             else
748                 hRgn = CreateRectRgnIndirect( &r2 );
749         }
750         else /* entire window or client depending on RDW_FRAME */
751         {
752             if( flags & RDW_FRAME )
753             {
754                 if (wndPtr->hrgnUpdate) hRgn = 1;
755                 else wndPtr->hrgnUpdate = 1;
756             }
757             else
758             {
759                 GETCLIENTRECTW( wndPtr, r2 );
760                 if( wndPtr->hrgnUpdate == 0 )
761                     wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
762                 else
763                     hRgn = CreateRectRgnIndirect( &r2 );
764             }
765         }
766     }
767     else if (flags & RDW_VALIDATE)  /* ------------------------- Validate */
768     {
769         /* In this we cannot leave with zero hRgn */
770         if( hrgnUpdate )
771         {
772             hRgn = REGION_CropRgn( hRgn, hrgnUpdate,  &r, &pt );
773             GetRgnBox( hRgn, &r2 );
774             if( IsRectEmpty( &r2 ) ) goto END;
775         }
776         else if( rectUpdate )
777         {
778             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
779                 OffsetRect( &r2, pt.x, pt.y );
780             hRgn = CreateRectRgnIndirect( &r2 );
781         }
782         else /* entire window or client depending on RDW_FRAME */
783         {
784             if( flags & RDW_FRAME )
785                 hRgn = 1;
786             else
787             {
788                 GETCLIENTRECTW( wndPtr, r2 );
789                 hRgn = CreateRectRgnIndirect( &r2 );
790             }
791         }
792     }
793
794     /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
795
796     RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
797
798     /* Erase/update windows, from now on hRgn is a scratch region */
799
800     hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
801
802 END:
803     if( hRgn > 1 && (hRgn != hrgnUpdate) )
804         DeleteObject(hRgn );
805     WIN_ReleaseWndPtr(wndPtr);
806     return TRUE;
807 }
808
809
810 /***********************************************************************
811  *              UpdateWindow (USER32.@)
812  */
813 void WINAPI UpdateWindow( HWND hwnd )
814 {
815     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
816 }
817
818
819 /***********************************************************************
820  *              InvalidateRgn (USER32.@)
821  */
822 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
823 {
824     return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
825 }
826
827
828 /***********************************************************************
829  *              InvalidateRect (USER32.@)
830  */
831 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
832 {
833     return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
834 }
835
836
837 /***********************************************************************
838  *              ValidateRgn (USER32.@)
839  */
840 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
841 {
842     RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
843 }
844
845
846 /***********************************************************************
847  *              ValidateRect (USER32.@)
848  */
849 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
850 {
851     RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
852 }
853
854
855 /***********************************************************************
856  *              GetUpdateRect (USER32.@)
857  */
858 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
859 {
860     BOOL retvalue;
861     WND * wndPtr = WIN_FindWndPtr( hwnd );
862     if (!wndPtr) return FALSE;
863
864     if (rect)
865     {
866         if (wndPtr->hrgnUpdate > 1)
867         {
868             HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
869             if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
870             {
871                 retvalue = FALSE;
872                 goto END;
873             }
874             GetRgnBox( hrgn, rect );
875             DeleteObject( hrgn );
876             if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
877             {
878                 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
879                 {
880                     DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect,  2);
881                 }
882             }
883         }
884         else
885         if( wndPtr->hrgnUpdate == 1 )
886         {
887             GetClientRect( hwnd, rect );
888             if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
889         }
890         else
891             SetRectEmpty( rect );
892     }
893     retvalue = (wndPtr->hrgnUpdate >= 1);
894 END:
895     WIN_ReleaseWndPtr(wndPtr);
896     return retvalue;
897 }
898
899
900 /***********************************************************************
901  *              GetUpdateRgn (USER32.@)
902  */
903 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
904 {
905     INT retval;
906     WND * wndPtr = WIN_FindWndPtr( hwnd );
907     if (!wndPtr) return ERROR;
908
909     if (wndPtr->hrgnUpdate == 0)
910     {
911         SetRectRgn( hrgn, 0, 0, 0, 0 );
912         retval = NULLREGION;
913         goto END;
914     }
915     else
916     if (wndPtr->hrgnUpdate == 1)
917     {
918         SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
919                                 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
920         retval = SIMPLEREGION;
921     }
922     else
923     {
924         retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
925         OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
926                          wndPtr->rectWindow.top - wndPtr->rectClient.top );
927     }
928     if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
929 END:
930     WIN_ReleaseWndPtr(wndPtr);
931     return retval;
932 }
933
934
935 /***********************************************************************
936  *              ExcludeUpdateRgn (USER32.@)
937  */
938 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
939 {
940     RECT rect;
941     WND * wndPtr;
942
943     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
944
945     if (wndPtr->hrgnUpdate)
946     {
947         INT ret;
948         HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
949                                       wndPtr->rectWindow.top - wndPtr->rectClient.top,
950                                       wndPtr->rectWindow.right - wndPtr->rectClient.left,
951                                       wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
952         if( wndPtr->hrgnUpdate > 1 )
953         {
954             CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
955             OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
956                             wndPtr->rectWindow.top - wndPtr->rectClient.top );
957         }
958
959         /* do ugly coordinate translations in dce.c */
960
961         ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
962         DeleteObject( hrgn );
963         WIN_ReleaseWndPtr(wndPtr);
964         return ret;
965     }
966     WIN_ReleaseWndPtr(wndPtr);
967     return GetClipBox( hdc, &rect );
968 }
969
970
971
972 /***********************************************************************
973  *              FillRect (USER.81)
974  * NOTE
975  *   The Win16 variant doesn't support special color brushes like
976  *   the Win32 one, despite the fact that Win16, as well as Win32,
977  *   supports special background brushes for a window class.
978  */
979 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
980 {
981     HBRUSH prevBrush;
982
983     /* coordinates are logical so we cannot fast-check 'rect',
984      * it will be done later in the PatBlt().
985      */
986
987     if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
988     PatBlt( hdc, rect->left, rect->top,
989               rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
990     SelectObject( hdc, prevBrush );
991     return 1;
992 }
993
994
995 /***********************************************************************
996  *              FillRect (USER32.@)
997  */
998 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
999 {
1000     HBRUSH prevBrush;
1001
1002     if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1003         hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1004     }
1005
1006     if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1007     PatBlt( hdc, rect->left, rect->top,
1008               rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1009     SelectObject( hdc, prevBrush );
1010     return 1;
1011 }
1012
1013
1014 /***********************************************************************
1015  *              InvertRect (USER.82)
1016  */
1017 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1018 {
1019     PatBlt( hdc, rect->left, rect->top,
1020               rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1021 }
1022
1023
1024 /***********************************************************************
1025  *              InvertRect (USER32.@)
1026  */
1027 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1028 {
1029     return PatBlt( hdc, rect->left, rect->top,
1030                      rect->right - rect->left, rect->bottom - rect->top,
1031                      DSTINVERT );
1032 }
1033
1034
1035 /***********************************************************************
1036  *              FrameRect (USER32.@)
1037  */
1038 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1039 {
1040     HBRUSH prevBrush;
1041     RECT r = *rect;
1042
1043     if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1044     if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1045
1046     PatBlt( hdc, r.left, r.top, 1,
1047               r.bottom - r.top, PATCOPY );
1048     PatBlt( hdc, r.right - 1, r.top, 1,
1049               r.bottom - r.top, PATCOPY );
1050     PatBlt( hdc, r.left, r.top,
1051               r.right - r.left, 1, PATCOPY );
1052     PatBlt( hdc, r.left, r.bottom - 1,
1053               r.right - r.left, 1, PATCOPY );
1054
1055     SelectObject( hdc, prevBrush );
1056     return TRUE;
1057 }
1058
1059
1060 /***********************************************************************
1061  *              FrameRect (USER.83)
1062  */
1063 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1064 {
1065     RECT rect;
1066     CONV_RECT16TO32( rect16, &rect );
1067     return FrameRect( hdc, &rect, hbrush );
1068 }
1069
1070
1071 /***********************************************************************
1072  *              DrawFocusRect (USER.466)
1073  */
1074 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1075 {
1076     RECT rect32;
1077     CONV_RECT16TO32( rc, &rect32 );
1078     DrawFocusRect( hdc, &rect32 );
1079 }
1080
1081
1082 /***********************************************************************
1083  *              DrawFocusRect (USER32.@)
1084  *
1085  * FIXME: PatBlt(PATINVERT) with background brush.
1086  */
1087 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1088 {
1089     HBRUSH hOldBrush;
1090     HPEN hOldPen, hNewPen;
1091     INT oldDrawMode, oldBkMode;
1092
1093     hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1094     hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1095     hOldPen = SelectObject(hdc, hNewPen);
1096     oldDrawMode = SetROP2(hdc, R2_XORPEN);
1097     oldBkMode = SetBkMode(hdc, TRANSPARENT);
1098
1099     Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1100
1101     SetBkMode(hdc, oldBkMode);
1102     SetROP2(hdc, oldDrawMode);
1103     SelectObject(hdc, hOldPen);
1104     DeleteObject(hNewPen);
1105     SelectObject(hdc, hOldBrush);
1106
1107     return TRUE;
1108 }
1109
1110
1111 /**********************************************************************
1112  *              DrawAnimatedRects (USER32.@)
1113  */
1114 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1115                                    const RECT* lprcFrom,
1116                                    const RECT* lprcTo )
1117 {
1118     FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1119     return TRUE;
1120 }
1121
1122
1123 /**********************************************************************
1124  *          PAINTING_DrawStateJam
1125  *
1126  * Jams in the requested type in the dc
1127  */
1128 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1129                                   DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1130                                   LPRECT rc, UINT dtflags, BOOL unicode )
1131 {
1132     HDC memdc;
1133     HBITMAP hbmsave;
1134     BOOL retval;
1135     INT cx = rc->right - rc->left;
1136     INT cy = rc->bottom - rc->top;
1137
1138     switch(opcode)
1139     {
1140     case DST_TEXT:
1141     case DST_PREFIXTEXT:
1142         if(unicode)
1143             return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1144         else
1145             return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1146
1147     case DST_ICON:
1148         return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1149
1150     case DST_BITMAP:
1151         memdc = CreateCompatibleDC(hdc);
1152         if(!memdc) return FALSE;
1153         hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1154         if(!hbmsave)
1155         {
1156             DeleteDC(memdc);
1157             return FALSE;
1158         }
1159         retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1160         SelectObject(memdc, hbmsave);
1161         DeleteDC(memdc);
1162         return retval;
1163
1164     case DST_COMPLEX:
1165         if(func) {
1166             BOOL bRet;
1167             /* DRAWSTATEPROC assumes that it draws at the center of coordinates  */
1168
1169             OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1170             bRet = func(hdc, lp, wp, cx, cy);
1171             /* Restore origin */
1172             OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1173             return bRet;
1174         } else
1175             return FALSE;
1176     }
1177     return FALSE;
1178 }
1179
1180 /**********************************************************************
1181  *      PAINTING_DrawState()
1182  */
1183 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1184                                INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1185 {
1186     HBITMAP hbm, hbmsave;
1187     HFONT hfsave;
1188     HBRUSH hbsave, hbrtmp = 0;
1189     HDC memdc;
1190     RECT rc;
1191     UINT dtflags = DT_NOCLIP;
1192     COLORREF fg, bg;
1193     UINT opcode = flags & 0xf;
1194     INT len = wp;
1195     BOOL retval, tmp;
1196
1197     if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len)    /* The string is '\0' terminated */
1198     {
1199         if(unicode)
1200             len = strlenW((LPWSTR)lp);
1201         else
1202             len = strlen((LPSTR)lp);
1203     }
1204
1205     /* Find out what size the image has if not given by caller */
1206     if(!cx || !cy)
1207     {
1208         SIZE s;
1209         CURSORICONINFO *ici;
1210         BITMAP bm;
1211
1212         switch(opcode)
1213         {
1214         case DST_TEXT:
1215         case DST_PREFIXTEXT:
1216             if(unicode)
1217                 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1218             else
1219                 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1220             if(!retval) return FALSE;
1221             break;
1222
1223         case DST_ICON:
1224             ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1225             if(!ici) return FALSE;
1226             s.cx = ici->nWidth;
1227             s.cy = ici->nHeight;
1228             GlobalUnlock16((HGLOBAL16)lp);
1229             break;
1230
1231         case DST_BITMAP:
1232             if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1233                 return FALSE;
1234             s.cx = bm.bmWidth;
1235             s.cy = bm.bmHeight;
1236             break;
1237
1238         case DST_COMPLEX: /* cx and cy must be set in this mode */
1239             return FALSE;
1240         }
1241
1242         if(!cx) cx = s.cx;
1243         if(!cy) cy = s.cy;
1244     }
1245
1246     rc.left   = x;
1247     rc.top    = y;
1248     rc.right  = x + cx;
1249     rc.bottom = y + cy;
1250
1251     if(flags & DSS_RIGHT)    /* This one is not documented in the win32.hlp file */
1252         dtflags |= DT_RIGHT;
1253     if(opcode == DST_TEXT)
1254         dtflags |= DT_NOPREFIX;
1255
1256     /* For DSS_NORMAL we just jam in the image and return */
1257     if((flags & 0x7ff0) == DSS_NORMAL)
1258     {
1259         return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1260     }
1261
1262     /* For all other states we need to convert the image to B/W in a local bitmap */
1263     /* before it is displayed */
1264     fg = SetTextColor(hdc, RGB(0, 0, 0));
1265     bg = SetBkColor(hdc, RGB(255, 255, 255));
1266     hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1267     memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1268     retval = FALSE; /* assume failure */
1269
1270     /* From here on we must use "goto cleanup" when something goes wrong */
1271     hbm     = CreateBitmap(cx, cy, 1, 1, NULL);
1272     if(!hbm) goto cleanup;
1273     memdc   = CreateCompatibleDC(hdc);
1274     if(!memdc) goto cleanup;
1275     hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1276     if(!hbmsave) goto cleanup;
1277     rc.left = rc.top = 0;
1278     rc.right = cx;
1279     rc.bottom = cy;
1280     if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1281     SetBkColor(memdc, RGB(255, 255, 255));
1282     SetTextColor(memdc, RGB(0, 0, 0));
1283     hfsave  = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1284
1285     /* DST_COMPLEX may draw text as well,
1286      * so we must be sure that correct font is selected
1287      */
1288     if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1289     tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1290     if(hfsave) SelectObject(memdc, hfsave);
1291     if(!tmp) goto cleanup;
1292
1293     /* This state cause the image to be dithered */
1294     if(flags & DSS_UNION)
1295     {
1296         hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1297         if(!hbsave) goto cleanup;
1298         tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1299         SelectObject(memdc, hbsave);
1300         if(!tmp) goto cleanup;
1301     }
1302
1303     if (flags & DSS_DISABLED)
1304        hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1305     else if (flags & DSS_DEFAULT)
1306        hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1307
1308     /* Draw light or dark shadow */
1309     if (flags & (DSS_DISABLED|DSS_DEFAULT))
1310     {
1311        if(!hbrtmp) goto cleanup;
1312        hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1313        if(!hbsave) goto cleanup;
1314        if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1315        SelectObject(hdc, hbsave);
1316        DeleteObject(hbrtmp);
1317        hbrtmp = 0;
1318     }
1319
1320     if (flags & DSS_DISABLED)
1321     {
1322        hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1323        if(!hbrtmp) goto cleanup;
1324     }
1325     else if (!hbr)
1326     {
1327        hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1328     }
1329
1330     hbsave = (HBRUSH)SelectObject(hdc, hbr);
1331
1332     if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1333
1334     retval = TRUE; /* We succeeded */
1335
1336 cleanup:
1337     SetTextColor(hdc, fg);
1338     SetBkColor(hdc, bg);
1339
1340     if(hbsave)  SelectObject(hdc, hbsave);
1341     if(hbmsave) SelectObject(memdc, hbmsave);
1342     if(hbrtmp)  DeleteObject(hbrtmp);
1343     if(hbm)     DeleteObject(hbm);
1344     if(memdc)   DeleteDC(memdc);
1345
1346     return retval;
1347 }
1348
1349 /**********************************************************************
1350  *              DrawStateA (USER32.@)
1351  */
1352 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1353                    DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1354                    INT x, INT y, INT cx, INT cy, UINT flags)
1355 {
1356     return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1357 }
1358
1359 /**********************************************************************
1360  *              DrawStateW (USER32.@)
1361  */
1362 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1363                    DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1364                    INT x, INT y, INT cx, INT cy, UINT flags)
1365 {
1366     return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1367 }
1368
1369
1370 /**********************************************************************
1371  *           DrawState    (USER.449)
1372  */
1373 BOOL16 WINAPI DrawState16( HDC16 hdc, HBRUSH16 hbr, DRAWSTATEPROC16 func, LPARAM ldata,
1374                            WPARAM16 wdata, INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags )
1375 {
1376     struct draw_state_info info;
1377     UINT opcode = flags & 0xf;
1378
1379     if (opcode == DST_TEXT || opcode == DST_PREFIXTEXT)
1380     {
1381         /* make sure DrawStateA doesn't try to use ldata as a pointer */
1382         if (!wdata) wdata = strlen( MapSL(ldata) );
1383         if (!cx || !cy)
1384         {
1385             SIZE s;
1386             if (!GetTextExtentPoint32A( hdc, MapSL(ldata), wdata, &s )) return FALSE;
1387             if (!cx) cx = s.cx;
1388             if (!cy) cy = s.cy;
1389         }
1390     }
1391     info.proc  = func;
1392     info.param = ldata;
1393     return DrawStateA( hdc, hbr, draw_state_callback, (LPARAM)&info, wdata, x, y, cx, cy, flags );
1394 }
1395
1396
1397 /***********************************************************************
1398  *              SelectPalette (USER.282)
1399  */
1400 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1401                                    BOOL16 bForceBackground )
1402 {
1403     WORD wBkgPalette = 1;
1404
1405     if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1406     {
1407         HWND hwnd = WindowFromDC( hDC );
1408         if (hwnd)
1409         {
1410             HWND hForeground = GetForegroundWindow();
1411             /* set primary palette if it's related to current active */
1412             if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1413         }
1414     }
1415     return GDISelectPalette16( hDC, hPal, wBkgPalette);
1416 }
1417
1418
1419 /***********************************************************************
1420  *              RealizePalette (USER.283)
1421  */
1422 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1423 {
1424     UINT16 realized = GDIRealizePalette16( hDC );
1425
1426     /* do not send anything if no colors were changed */
1427     if (realized && IsDCCurrentPalette16( hDC ))
1428     {
1429         /* send palette change notification */
1430         HWND hWnd = WindowFromDC( hDC );
1431         if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0L);
1432     }
1433     return realized;
1434 }
1435
1436
1437 /***********************************************************************
1438  *              UserRealizePalette (USER32.@)
1439  */
1440 UINT WINAPI UserRealizePalette( HDC hDC )
1441 {
1442     return RealizePalette16( hDC );
1443 }