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