Test some more ntdll types.
[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 "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "wine/winuser16.h"
31 #include "wownt32.h"
32 #include "wine/unicode.h"
33 #include "wine/server.h"
34 #include "user.h"
35 #include "win.h"
36 #include "message.h"
37 #include "dce.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
41 WINE_DECLARE_DEBUG_CHANNEL(nonclient);
42
43 /* client rect in window coordinates */
44
45 #define GETCLIENTRECTW( wnd, r )        (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
46                                         (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
47                                         (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
48                                         (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
49
50   /* PAINT_RedrawWindow() control flags */
51 #define RDW_EX_DELAY_NCPAINT    0x0020
52
53   /* WIN_UpdateNCRgn() flags */
54 #define UNC_CHECK               0x0001
55 #define UNC_ENTIRE              0x0002
56 #define UNC_REGION              0x0004
57 #define UNC_UPDATE              0x0008
58 #define UNC_DELAY_NCPAINT       0x0010
59 #define UNC_IN_BEGINPAINT       0x0020
60
61 HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = NULL;
62 UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc) = NULL;
63
64
65 /***********************************************************************
66  *           add_paint_count
67  *
68  * Add an increment (normally 1 or -1) to the current paint count of a window.
69  */
70 static void add_paint_count( HWND hwnd, int incr )
71 {
72     SERVER_START_REQ( inc_window_paint_count )
73     {
74         req->handle = hwnd;
75         req->incr   = incr;
76         wine_server_call( req );
77     }
78     SERVER_END_REQ;
79 }
80
81
82 /***********************************************************************
83  *           crop_rgn
84  *
85  * hSrc:        Region to crop.
86  * lpRect:      Clipping rectangle.
87  *
88  * hDst: Region to hold the result (a new region is created if it's 0).
89  *       Allowed to be the same region as hSrc in which case everything
90  *       will be done in place, with no memory reallocations.
91  *
92  * Returns: hDst if success, 0 otherwise.
93  */
94 static HRGN crop_rgn( HRGN hDst, HRGN hSrc, const RECT *rect )
95 {
96     HRGN h = CreateRectRgnIndirect( rect );
97     if (hDst == 0) hDst = h;
98     CombineRgn( hDst, hSrc, h, RGN_AND );
99     if (hDst != h) DeleteObject( h );
100     return hDst;
101 }
102
103
104 /***********************************************************************
105  *           WIN_HaveToDelayNCPAINT
106  *
107  * Currently, in the Wine painting mechanism, the WM_NCPAINT message
108  * is generated as soon as a region intersecting the non-client area
109  * of a window is invalidated.
110  *
111  * This technique will work fine for all windows whose parents
112  * have the WS_CLIPCHILDREN style. When the parents have that style,
113  * they are not going to override the contents of their children.
114  * However, when the parent doesn't have that style, Windows relies
115  * on a "painter's algorithm" to display the contents of the windows.
116  * That is, windows are painted from back to front. This includes the
117  * non-client area.
118  *
119  * This method looks at the current state of a window to determine
120  * if the sending of the WM_NCPAINT message should be delayed until
121  * the BeginPaint call.
122  *
123  * PARAMS:
124  *   wndPtr   - A Locked window pointer to the window we're
125  *              examining.
126  *   uncFlags - This is the flag passed to the WIN_UpdateNCRgn
127  *              function. This is a shortcut for the cases when
128  *              we already know when to avoid scanning all the
129  *              parents of a window. If you already know that this
130  *              window's NCPAINT should be delayed, set the
131  *              UNC_DELAY_NCPAINT flag for this parameter.
132  *
133  *              This shortcut behavior is implemented in the
134  *              RDW_Paint() method.
135  *
136  */
137 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
138 {
139   /*
140    * Test the shortcut first. (duh)
141    */
142   if (uncFlags & UNC_DELAY_NCPAINT)
143     return TRUE;
144
145   /*
146    * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
147    * method only. This is another shortcut to avoid going
148    * up the parent's chain of the window to finally
149    * figure-out that none of them has an invalid region.
150    */
151   if (uncFlags & UNC_IN_BEGINPAINT)
152     return FALSE;
153
154   /*
155    * Scan all the parents of this window to find a window
156    * that doesn't have the WS_CLIPCHILDREN style and that
157    * has an invalid region.
158    */
159   while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
160   {
161       WND* parentWnd = WIN_FindWndPtr( hwnd );
162       if (parentWnd && !(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
163       {
164           WIN_ReleaseWndPtr( parentWnd );
165           return TRUE;
166       }
167       WIN_ReleaseWndPtr( parentWnd );
168   }
169   return FALSE;
170 }
171
172 /***********************************************************************
173  *           WIN_UpdateNCRgn
174  *
175  *  Things to do:
176  *      Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
177  *      Crop hrgnUpdate to a client rect, especially if it 1.
178  *      If UNC_REGION is set return update region for the client rect.
179  *
180  *  NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
181  *        The trick is that when the returned region handle may be different from hRgn.
182  *        In this case the old hRgn must be considered gone. BUT, if the returned value
183  *        is 1 then the hRgn is preserved and RDW_Paint() will have to get
184  *        a DC without extra clipping region.
185  */
186 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
187 {
188     RECT  r;
189     HRGN  hClip = 0;
190     HRGN  hrgnRet = 0;
191
192     TRACE_(nonclient)("hwnd %p [%p] hrgn %p, unc %04x, ncf %i\n",
193                       wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
194
195     /* desktop window doesn't have a nonclient area */
196     if(wnd->hwndSelf == GetDesktopWindow())
197     {
198         wnd->flags &= ~WIN_NEEDS_NCPAINT;
199         if( wnd->hrgnUpdate > (HRGN)1 )
200         {
201             if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
202             CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
203             hrgnRet = hRgn;
204         }
205         else
206         {
207             hrgnRet = wnd->hrgnUpdate;
208         }
209         return hrgnRet;
210     }
211
212     if ((wnd->hwndSelf == GetForegroundWindow()) &&
213         !(wnd->flags & WIN_NCACTIVATED) )
214     {
215         wnd->flags |= WIN_NCACTIVATED;
216         uncFlags |= UNC_ENTIRE;
217     }
218
219     /*
220      * If the window's non-client area needs to be painted,
221      */
222     if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
223          !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
224     {
225             RECT r2, r3;
226
227             wnd->flags &= ~WIN_NEEDS_NCPAINT;
228             GETCLIENTRECTW( wnd, r );
229
230             TRACE_(nonclient)( "\tclient box (%ld,%ld-%ld,%ld), hrgnUpdate %p\n",
231                                 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
232             if( wnd->hrgnUpdate > (HRGN)1 )
233             {
234                 /* Check if update rgn overlaps with nonclient area */
235
236                 GetRgnBox( wnd->hrgnUpdate, &r2 );
237                 UnionRect( &r3, &r2, &r );
238                 if( r3.left != r.left || r3.top != r.top ||
239                     r3.right != r.right || r3.bottom != r.bottom ) /* it does */
240                 {
241                     /* crop hrgnUpdate, save old one in hClip - the only
242                      * case that places a valid region handle in hClip */
243
244                     hClip = wnd->hrgnUpdate;
245                     wnd->hrgnUpdate = crop_rgn( hRgn, hClip, &r );
246                     if( uncFlags & UNC_REGION ) hrgnRet = hClip;
247                 }
248
249                 if( uncFlags & UNC_CHECK )
250                 {
251                     GetRgnBox( wnd->hrgnUpdate, &r3 );
252                     if( IsRectEmpty( &r3 ) )
253                     {
254                         /* delete the update region since all invalid
255                          * parts were in the nonclient area */
256
257                         DeleteObject( wnd->hrgnUpdate );
258                         wnd->hrgnUpdate = 0;
259                         if(!(wnd->flags & WIN_INTERNAL_PAINT))
260                             add_paint_count( wnd->hwndSelf, -1 );
261
262                         wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
263                     }
264                 }
265
266                 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
267             }
268             else
269             if( wnd->hrgnUpdate == (HRGN)1 )/* entire window */
270             {
271                 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
272                 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
273                 uncFlags |= UNC_ENTIRE;
274             }
275     }
276     else /* no WM_NCPAINT unless forced */
277     {
278         if( wnd->hrgnUpdate >  (HRGN)1 )
279         {
280 copyrgn:
281             if( uncFlags & UNC_REGION )
282             {
283                 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
284                 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
285                 hrgnRet = hRgn;
286             }
287         }
288         else
289         if( wnd->hrgnUpdate == (HRGN)1 && (uncFlags & UNC_UPDATE) )
290         {
291             GETCLIENTRECTW( wnd, r );
292             wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
293             if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
294         }
295     }
296
297     if(!hClip && (uncFlags & UNC_ENTIRE) )
298     {
299         /* still don't do anything if there is no nonclient area */
300         hClip = (HRGN)(memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
301     }
302
303     if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
304     {
305         if ( hClip == hrgnRet && hrgnRet > (HRGN)1 ) {
306             hClip = CreateRectRgn( 0, 0, 0, 0 );
307             CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
308         }
309
310         SendMessageA( wnd->hwndSelf, WM_NCPAINT, (WPARAM)hClip, 0L );
311         if( (hClip > (HRGN)1) && (hClip != hRgn) && (hClip != hrgnRet) )
312             DeleteObject( hClip );
313         /*
314          * Since all Window locks are suspended while processing the WM_NCPAINT
315          * we want to make sure the window still exists before continuing.
316          */
317         if (!IsWindow(wnd->hwndSelf))
318         {
319           DeleteObject(hrgnRet);
320           hrgnRet=0;
321         }
322     }
323
324     TRACE_(nonclient)("returning %p (hClip = %p, hrgnUpdate = %p)\n", hrgnRet, hClip, wnd->hrgnUpdate );
325
326     return hrgnRet;
327 }
328
329
330 /***********************************************************************
331  *              RDW_ValidateParent [RDW_UpdateRgns() helper]
332  *
333  *  Validate the portions of parents that are covered by a validated child
334  *  wndPtr = child
335  */
336 static void RDW_ValidateParent(WND *wndChild)
337 {
338     HWND parent;
339     HRGN hrg;
340
341     if (wndChild->hrgnUpdate == (HRGN)1 ) {
342         RECT r;
343         r.left = 0;
344         r.top = 0;
345         r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
346         r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
347         hrg = CreateRectRgnIndirect( &r );
348     } else
349         hrg = wndChild->hrgnUpdate;
350
351     parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
352     while (parent && parent != GetDesktopWindow())
353     {
354         WND *wndParent = WIN_FindWndPtr( parent );
355         if (wndParent && !(wndParent->dwStyle & WS_CLIPCHILDREN))
356         {
357             if (wndParent->hrgnUpdate != 0)
358             {
359                 POINT ptOffset;
360                 RECT rect, rectParent;
361                 if( wndParent->hrgnUpdate == (HRGN)1 )
362                 {
363                    RECT r;
364
365                    r.left = 0;
366                    r.top = 0;
367                    r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
368                    r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
369
370                    wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
371                 }
372                 /* we must offset the child region by the offset of the child rect in the parent */
373                 GetWindowRect(wndParent->hwndSelf, &rectParent);
374                 GetWindowRect(wndChild->hwndSelf, &rect);
375                 ptOffset.x = rect.left - rectParent.left;
376                 ptOffset.y = rect.top - rectParent.top;
377                 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
378                 if (CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF ) == NULLREGION)
379                 {
380                     /* the update region has become empty */
381                     DeleteObject( wndParent->hrgnUpdate );
382                     wndParent->hrgnUpdate = 0;
383                     wndParent->flags &= ~WIN_NEEDS_ERASEBKGND;
384                     if( !(wndParent->flags & WIN_INTERNAL_PAINT) )
385                         add_paint_count( wndParent->hwndSelf, -1 );
386                 }
387                 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
388             }
389         }
390         WIN_ReleaseWndPtr( wndParent );
391         parent = GetAncestor( parent, GA_PARENT );
392     }
393     if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
394 }
395
396 /***********************************************************************
397  *              RDW_UpdateRgns [RedrawWindow() helper]
398  *
399  *  Walks the window tree and adds/removes parts of the hRgn to/from update
400  *  regions of windows that overlap it. Also, manages internal paint flags.
401  *
402  *  NOTE: Walks the window tree so the caller must lock it.
403  *        MUST preserve hRgn (can modify but then has to restore).
404  */
405 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
406 {
407     /*
408      * Called only when one of the following is set:
409      * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
410      */
411
412     BOOL bHadOne =  wndPtr->hrgnUpdate && hRgn;
413     BOOL bChildren =  (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
414                        ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
415     RECT r;
416
417     r.left = 0;
418     r.top = 0;
419     r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
420     r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
421
422     TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
423
424     if( flags & RDW_INVALIDATE )
425     {
426         if( hRgn > (HRGN)1 )
427         {
428             switch ((UINT)wndPtr->hrgnUpdate)
429             {
430                 default:
431                         CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
432                         /* fall through */
433                 case 0:
434                         wndPtr->hrgnUpdate = crop_rgn( wndPtr->hrgnUpdate,
435                                                        wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
436                                                        &r );
437                         if( !bHadOne )
438                         {
439                             GetRgnBox( wndPtr->hrgnUpdate, &r );
440                             if( IsRectEmpty( &r ) )
441                             {
442                                 DeleteObject( wndPtr->hrgnUpdate );
443                                 wndPtr->hrgnUpdate = 0;
444                                 goto end;
445                             }
446                         }
447                         break;
448                 case 1: /* already an entire window */
449                         break;
450             }
451         }
452         else if( hRgn == (HRGN)1 )
453         {
454             if( wndPtr->hrgnUpdate > (HRGN)1 )
455                 DeleteObject( wndPtr->hrgnUpdate );
456             wndPtr->hrgnUpdate = (HRGN)1;
457         }
458         else
459         {
460             /* hRgn is zero */
461             if( wndPtr->hrgnUpdate > (HRGN)1)
462             {
463                 GetRgnBox( wndPtr->hrgnUpdate, &r );
464                 if( IsRectEmpty( &r ) )
465                 {
466                     DeleteObject( wndPtr->hrgnUpdate );
467                     wndPtr->hrgnUpdate = 0;
468                     goto end;
469                 }
470             }
471             hRgn = wndPtr->hrgnUpdate;  /* this is a trick that depends
472                                          * on code in RDW_Paint() */
473         }
474
475         if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
476             add_paint_count( wndPtr->hwndSelf, 1 );
477
478         if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
479         if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
480         flags    |= RDW_FRAME;
481     }
482     else if( flags & RDW_VALIDATE )
483     {
484         if( wndPtr->hrgnUpdate )
485         {
486             if( hRgn > (HRGN)1 )
487             {
488                 if( wndPtr->hrgnUpdate == (HRGN)1 )
489                     wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
490
491                 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
492                     == NULLREGION )
493                 {
494                     DeleteObject( wndPtr->hrgnUpdate );
495                     wndPtr->hrgnUpdate = 0;
496                 }
497             }
498             else /* validate everything */
499             {
500                 if( wndPtr->hrgnUpdate > (HRGN)1 ) DeleteObject( wndPtr->hrgnUpdate );
501                 wndPtr->hrgnUpdate = 0;
502             }
503
504             if( !wndPtr->hrgnUpdate )
505             {
506                 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
507                 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
508                     add_paint_count( wndPtr->hwndSelf, -1 );
509             }
510         }
511
512         if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
513         if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
514
515     }
516
517     if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
518         RDW_ValidateParent(wndPtr); /* validate parent covered by region */
519
520     /* in/validate child windows that intersect with the region if it
521      * is a valid handle. */
522
523     if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
524     {
525         HWND *list;
526         if( hRgn > (HRGN)1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
527         {
528             POINT ptTotal, prevOrigin = {0,0};
529             POINT ptClient;
530             INT i;
531
532             ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
533             ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
534
535             for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
536             {
537                 WND *wnd = WIN_FindWndPtr( list[i] );
538                 if (!wnd) continue;
539                 if( wnd->dwStyle & WS_VISIBLE )
540                 {
541                     POINT ptOffset;
542
543                     r.left = wnd->rectWindow.left + ptClient.x;
544                     r.right = wnd->rectWindow.right + ptClient.x;
545                     r.top = wnd->rectWindow.top + ptClient.y;
546                     r.bottom = wnd->rectWindow.bottom + ptClient.y;
547
548                     ptOffset.x = r.left - prevOrigin.x;
549                     ptOffset.y = r.top - prevOrigin.y;
550                     OffsetRect( &r, -ptTotal.x, -ptTotal.y );
551
552                     if( RectInRegion( hRgn, &r ) )
553                     {
554                         OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
555                         RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
556                         prevOrigin.x = r.left + ptTotal.x;
557                         prevOrigin.y = r.top + ptTotal.y;
558                         ptTotal.x += ptOffset.x;
559                         ptTotal.y += ptOffset.y;
560                     }
561                 }
562                 WIN_ReleaseWndPtr( wnd );
563             }
564             HeapFree( GetProcessHeap(), 0, list );
565             OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
566             bChildren = 0;
567         }
568     }
569
570     /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
571
572     if( bChildren )
573     {
574         HWND *list;
575         if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
576         {
577             INT i;
578             for (i = 0; list[i]; i++)
579             {
580                 WND *wnd = WIN_FindWndPtr( list[i] );
581                 if (!wnd) continue;
582                 if( wnd->dwStyle & WS_VISIBLE )
583                     RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
584                 WIN_ReleaseWndPtr( wnd );
585             }
586             HeapFree( GetProcessHeap(), 0, list );
587         }
588     }
589
590 end:
591
592     /* Set/clear internal paint flag */
593
594     if (flags & RDW_INTERNALPAINT)
595     {
596         if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
597             add_paint_count( wndPtr->hwndSelf, 1 );
598         wndPtr->flags |= WIN_INTERNAL_PAINT;
599     }
600     else if (flags & RDW_NOINTERNALPAINT)
601     {
602         if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
603             add_paint_count( wndPtr->hwndSelf, -1 );
604         wndPtr->flags &= ~WIN_INTERNAL_PAINT;
605     }
606 }
607
608 /***********************************************************************
609  *           RDW_Paint [RedrawWindow() helper]
610  *
611  * Walks the window tree and paints/erases windows that have
612  * nonzero update regions according to redraw flags. hrgn is a scratch
613  * region passed down during recursion. Must not be 1.
614  *
615  */
616 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
617 {
618 /* NOTE: wndPtr is locked by caller.
619  *
620  * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
621  * SendMessage() calls. This is a comment inside DefWindowProc() source
622  * from 16-bit SDK:
623  *
624  *   This message avoids lots of inter-app message traffic
625  *   by switching to the other task and continuing the
626  *   recursion there.
627  *
628  * wParam         = flags
629  * LOWORD(lParam) = hrgnClip
630  * HIWORD(lParam) = hwndSkip  (not used; always NULL)
631  *
632  */
633     HDC  hDC;
634     HWND hWnd = wndPtr->hwndSelf;
635     BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
636
637       /* Erase/update the window itself ... */
638
639     TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
640
641     /*
642      * Check if this window should delay it's processing of WM_NCPAINT.
643      * See WIN_HaveToDelayNCPAINT for a description of the mechanism
644      */
645     if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
646         ex |= RDW_EX_DELAY_NCPAINT;
647
648     if (flags & RDW_UPDATENOW)
649     {
650         if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
651             SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
652     }
653     else if (flags & RDW_ERASENOW)
654     {
655         UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
656         HRGN hrgnRet;
657
658         hrgnRet = WIN_UpdateNCRgn(wndPtr,
659                                   hrgn,
660                                   UNC_REGION | UNC_CHECK |
661                                   ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
662
663         if( hrgnRet )
664         {
665             if( hrgnRet > (HRGN)1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
666             if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
667             {
668                 if( bIcon ) dcx |= DCX_WINDOW;
669                 else
670                 if( hrgnRet )
671                     OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
672                                         wndPtr->rectWindow.top  - wndPtr->rectClient.top);
673                 else
674                     dcx &= ~DCX_INTERSECTRGN;
675                 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
676                 {
677                     if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
678                                       (WPARAM)hDC, 0 ))
679                         wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
680                     ReleaseDC( hWnd, hDC );
681                 }
682             }
683         }
684     }
685
686     if( !IsWindow(hWnd) ) return hrgn;
687
688       /* ... and its child windows */
689
690     if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
691        ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
692     {
693         HWND *list, *phwnd;
694
695         if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
696         {
697             for (phwnd = list; *phwnd; phwnd++)
698             {
699                 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
700                 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
701                      (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
702                     hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
703                 WIN_ReleaseWndPtr(wndPtr);
704             }
705             HeapFree( GetProcessHeap(), 0, list );
706         }
707     }
708
709     return hrgn;
710 }
711
712
713 /***********************************************************************
714  *           dump_rdw_flags
715  */
716 static void dump_rdw_flags(UINT flags)
717 {
718     TRACE("flags:");
719     if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
720     if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
721     if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
722     if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
723     if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
724     if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
725     if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
726     if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
727     if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
728     if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
729     if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
730     if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
731
732 #define RDW_FLAGS \
733     (RDW_INVALIDATE | \
734     RDW_INTERNALPAINT | \
735     RDW_ERASE | \
736     RDW_VALIDATE | \
737     RDW_NOINTERNALPAINT | \
738     RDW_NOERASE | \
739     RDW_NOCHILDREN | \
740     RDW_ALLCHILDREN | \
741     RDW_UPDATENOW | \
742     RDW_ERASENOW | \
743     RDW_FRAME | \
744     RDW_NOFRAME)
745
746     if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
747     TRACE("\n");
748 #undef RDW_FLAGS
749 }
750
751
752 /***********************************************************************
753  *              RedrawWindow (USER32.@)
754  */
755 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
756                               HRGN hrgnUpdate, UINT flags )
757 {
758     HRGN hRgn = 0;
759     RECT r, r2;
760     POINT pt;
761     WND* wndPtr;
762
763     if (!hwnd) hwnd = GetDesktopWindow();
764
765     /* check if the window or its parents are visible/not minimized */
766
767     if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
768
769     /* process pending events and messages before painting */
770     if (flags & RDW_UPDATENOW)
771         MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
772
773     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
774     if (TRACE_ON(win))
775     {
776         if( hrgnUpdate )
777         {
778             GetRgnBox( hrgnUpdate, &r );
779             TRACE( "%p (%p) NULL %p box (%ld,%ld-%ld,%ld) flags=%04x\n",
780                   hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
781         }
782         else
783         {
784             if( rectUpdate )
785                 r = *rectUpdate;
786             else
787                 SetRectEmpty( &r );
788             TRACE( "%p (%p) %s %ld,%ld-%ld,%ld %p flags=%04x\n",
789                    hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
790                    r.top, r.right, r.bottom, hrgnUpdate, flags );
791         }
792         dump_rdw_flags(flags);
793     }
794
795     /* prepare an update region in window coordinates */
796
797     if (((flags & (RDW_INVALIDATE|RDW_FRAME)) == (RDW_INVALIDATE|RDW_FRAME)) ||
798         ((flags & (RDW_VALIDATE|RDW_NOFRAME)) == (RDW_VALIDATE|RDW_NOFRAME)))
799         r = wndPtr->rectWindow;
800     else
801         r = wndPtr->rectClient;
802
803     pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
804     pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
805     OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
806
807     if (flags & RDW_INVALIDATE)  /* ------------------------- Invalidate */
808     {
809         /* If the window doesn't have hrgnUpdate we leave hRgn zero
810          * and put a new region straight into wndPtr->hrgnUpdate
811          * so that RDW_UpdateRgns() won't have to do any extra work.
812          */
813
814         if( hrgnUpdate )
815         {
816             if( wndPtr->hrgnUpdate )
817             {
818                 hRgn = CreateRectRgn( 0, 0, 0, 0 );
819                 CombineRgn( hRgn, hrgnUpdate, 0, RGN_COPY );
820                 OffsetRgn( hRgn, pt.x, pt.y );
821             }
822             else
823             {
824                 wndPtr->hrgnUpdate = crop_rgn( 0, hrgnUpdate, &r );
825                 OffsetRgn( wndPtr->hrgnUpdate, pt.x, pt.y );
826             }
827         }
828         else if( rectUpdate )
829         {
830             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
831             OffsetRect( &r2, pt.x, pt.y );
832             if( wndPtr->hrgnUpdate == 0 )
833                 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
834             else
835                 hRgn = CreateRectRgnIndirect( &r2 );
836         }
837         else /* entire window or client depending on RDW_FRAME */
838         {
839             if( flags & RDW_FRAME )
840             {
841                 if (wndPtr->hrgnUpdate) hRgn = (HRGN)1;
842                 else wndPtr->hrgnUpdate = (HRGN)1;
843             }
844             else
845             {
846                 GETCLIENTRECTW( wndPtr, r2 );
847                 if( wndPtr->hrgnUpdate == 0 )
848                     wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
849                 else
850                     hRgn = CreateRectRgnIndirect( &r2 );
851             }
852         }
853     }
854     else if (flags & RDW_VALIDATE)  /* ------------------------- Validate */
855     {
856         /* In this we cannot leave with zero hRgn */
857         if( hrgnUpdate )
858         {
859             hRgn = crop_rgn( hRgn, hrgnUpdate,  &r );
860             OffsetRgn( hRgn, pt.x, pt.y );
861             GetRgnBox( hRgn, &r2 );
862             if( IsRectEmpty( &r2 ) ) goto END;
863         }
864         else if( rectUpdate )
865         {
866             if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
867                 OffsetRect( &r2, pt.x, pt.y );
868             hRgn = CreateRectRgnIndirect( &r2 );
869         }
870         else /* entire window or client depending on RDW_NOFRAME */
871         {
872             if( flags & RDW_NOFRAME )
873                 hRgn = (HRGN)1;
874             else
875             {
876                 GETCLIENTRECTW( wndPtr, r2 );
877                 hRgn = CreateRectRgnIndirect( &r2 );
878             }
879         }
880     }
881
882     /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
883
884     RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
885
886     /* Erase/update windows, from now on hRgn is a scratch region */
887
888     hRgn = RDW_Paint( wndPtr, (hRgn == (HRGN)1) ? 0 : hRgn, flags, 0 );
889
890 END:
891     if( hRgn > (HRGN)1 && (hRgn != hrgnUpdate) )
892         DeleteObject(hRgn );
893     WIN_ReleaseWndPtr(wndPtr);
894     return TRUE;
895 }
896
897
898 /***********************************************************************
899  *              UpdateWindow (USER32.@)
900  */
901 BOOL WINAPI UpdateWindow( HWND hwnd )
902 {
903     return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
904 }
905
906
907 /***********************************************************************
908  *              InvalidateRgn (USER32.@)
909  */
910 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
911 {
912     return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
913 }
914
915
916 /***********************************************************************
917  *              InvalidateRect (USER32.@)
918  */
919 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
920 {
921     return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
922 }
923
924
925 /***********************************************************************
926  *              ValidateRgn (USER32.@)
927  */
928 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
929 {
930     return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
931 }
932
933
934 /***********************************************************************
935  *              ValidateRect (USER32.@)
936  */
937 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
938 {
939     return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
940 }
941
942
943 /***********************************************************************
944  *              SelectPalette (Not a Windows API)
945  */
946 HPALETTE WINAPI SelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
947 {
948     WORD wBkgPalette = 1;
949
950     if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
951     {
952         HWND hwnd = WindowFromDC( hDC );
953         if (hwnd)
954         {
955             HWND hForeground = GetForegroundWindow();
956             /* set primary palette if it's related to current active */
957             if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
958         }
959     }
960     return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
961 }
962
963
964 /***********************************************************************
965  *              UserRealizePalette (USER32.@)
966  */
967 UINT WINAPI UserRealizePalette( HDC hDC )
968 {
969     UINT realized = pfnGDIRealizePalette( hDC );
970
971     /* do not send anything if no colors were changed */
972     if (realized && IsDCCurrentPalette16( HDC_16(hDC) ))
973     {
974         /* send palette change notification */
975         HWND hWnd = WindowFromDC( hDC );
976         if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
977                                        SMTO_ABORTIFHUNG, 2000, NULL );
978     }
979     return realized;
980 }