2 * Window painting functions
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
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.
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.
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
25 #include "wine/winuser16.h"
27 #include "wine/unicode.h"
28 #include "wine/server.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(win);
37 WINE_DECLARE_DEBUG_CHANNEL(nonclient);
39 /* client rect in window coordinates */
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
46 /* PAINT_RedrawWindow() control flags */
47 #define RDW_EX_DELAY_NCPAINT 0x0020
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
58 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
60 HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = NULL;
61 UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc) = NULL;
64 /***********************************************************************
67 * Add an increment (normally 1 or -1) to the current paint count of a window.
69 static void add_paint_count( HWND hwnd, int incr )
71 SERVER_START_REQ( inc_window_paint_count )
75 wine_server_call( req );
81 /***********************************************************************
84 * hSrc: Region to crop.
85 * lpRect: Clipping rectangle.
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.
91 * Returns: hDst if success, 0 otherwise.
93 static HRGN crop_rgn( HRGN hDst, HRGN hSrc, const RECT *rect )
95 HRGN h = CreateRectRgnIndirect( rect );
96 if (hDst == 0) hDst = h;
97 CombineRgn( hDst, hSrc, h, RGN_AND );
98 if (hDst != h) DeleteObject( h );
103 /***********************************************************************
104 * WIN_HaveToDelayNCPAINT
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.
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
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.
123 * wndPtr - A Locked window pointer to the window we're
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.
132 * This shortcut behavior is implemented in the
133 * RDW_Paint() method.
136 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
139 * Test the shortcut first. (duh)
141 if (uncFlags & UNC_DELAY_NCPAINT)
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.
150 if (uncFlags & UNC_IN_BEGINPAINT)
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.
158 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
160 WND* parentWnd = WIN_FindWndPtr( hwnd );
161 if (parentWnd && !(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
163 WIN_ReleaseWndPtr( parentWnd );
166 WIN_ReleaseWndPtr( parentWnd );
171 /***********************************************************************
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.
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.
185 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
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);
194 /* desktop window doesn't have a nonclient area */
195 if(wnd->hwndSelf == GetDesktopWindow())
197 wnd->flags &= ~WIN_NEEDS_NCPAINT;
198 if( wnd->hrgnUpdate > (HRGN)1 )
200 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
201 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
206 hrgnRet = wnd->hrgnUpdate;
211 if ((wnd->hwndSelf == GetForegroundWindow()) &&
212 !(wnd->flags & WIN_NCACTIVATED) )
214 wnd->flags |= WIN_NCACTIVATED;
215 uncFlags |= UNC_ENTIRE;
219 * If the window's non-client area needs to be painted,
221 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
222 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
226 wnd->flags &= ~WIN_NEEDS_NCPAINT;
227 GETCLIENTRECTW( wnd, r );
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 )
233 /* Check if update rgn overlaps with nonclient area */
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 */
240 /* crop hrgnUpdate, save old one in hClip - the only
241 * case that places a valid region handle in hClip */
243 hClip = wnd->hrgnUpdate;
244 wnd->hrgnUpdate = crop_rgn( hRgn, hClip, &r );
245 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
248 if( uncFlags & UNC_CHECK )
250 GetRgnBox( wnd->hrgnUpdate, &r3 );
251 if( IsRectEmpty( &r3 ) )
253 /* delete the update region since all invalid
254 * parts were in the nonclient area */
256 DeleteObject( wnd->hrgnUpdate );
258 if(!(wnd->flags & WIN_INTERNAL_PAINT))
259 add_paint_count( wnd->hwndSelf, -1 );
261 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
265 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
268 if( wnd->hrgnUpdate == (HRGN)1 )/* entire window */
270 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
271 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
272 uncFlags |= UNC_ENTIRE;
275 else /* no WM_NCPAINT unless forced */
277 if( wnd->hrgnUpdate > (HRGN)1 )
280 if( uncFlags & UNC_REGION )
282 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
283 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
288 if( wnd->hrgnUpdate == (HRGN)1 && (uncFlags & UNC_UPDATE) )
290 GETCLIENTRECTW( wnd, r );
291 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
292 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
296 if(!hClip && (uncFlags & UNC_ENTIRE) )
298 /* still don't do anything if there is no nonclient area */
299 hClip = (HRGN)(memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
302 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
304 if ( hClip == hrgnRet && hrgnRet > (HRGN)1 ) {
305 hClip = CreateRectRgn( 0, 0, 0, 0 );
306 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
309 SendMessageA( wnd->hwndSelf, WM_NCPAINT, (WPARAM)hClip, 0L );
310 if( (hClip > (HRGN)1) && (hClip != hRgn) && (hClip != hrgnRet) )
311 DeleteObject( hClip );
313 * Since all Window locks are suspended while processing the WM_NCPAINT
314 * we want to make sure the window still exists before continuing.
316 if (!IsWindow(wnd->hwndSelf))
318 DeleteObject(hrgnRet);
323 TRACE_(nonclient)("returning %p (hClip = %p, hrgnUpdate = %p)\n", hrgnRet, hClip, wnd->hrgnUpdate );
329 /***********************************************************************
330 * RDW_ValidateParent [RDW_UpdateRgns() helper]
332 * Validate the portions of parents that are covered by a validated child
335 static void RDW_ValidateParent(WND *wndChild)
340 if (wndChild->hrgnUpdate == (HRGN)1 ) {
344 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
345 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
346 hrg = CreateRectRgnIndirect( &r );
348 hrg = wndChild->hrgnUpdate;
350 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
351 while (parent && parent != GetDesktopWindow())
353 WND *wndParent = WIN_FindWndPtr( parent );
354 if (wndParent && !(wndParent->dwStyle & WS_CLIPCHILDREN))
356 if (wndParent->hrgnUpdate != 0)
359 RECT rect, rectParent;
360 if( wndParent->hrgnUpdate == (HRGN)1 )
366 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
367 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
369 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
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 );
381 WIN_ReleaseWndPtr( wndParent );
382 parent = GetAncestor( parent, GA_PARENT );
384 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
387 /***********************************************************************
388 * RDW_UpdateRgns [RedrawWindow() helper]
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.
393 * NOTE: Walks the window tree so the caller must lock it.
394 * MUST preserve hRgn (can modify but then has to restore).
396 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
399 * Called only when one of the following is set:
400 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
403 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
404 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
405 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
410 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
411 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
413 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
415 if( flags & RDW_INVALIDATE )
419 switch ((UINT)wndPtr->hrgnUpdate)
422 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
425 wndPtr->hrgnUpdate = crop_rgn( wndPtr->hrgnUpdate,
426 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
430 GetRgnBox( wndPtr->hrgnUpdate, &r );
431 if( IsRectEmpty( &r ) )
433 DeleteObject( wndPtr->hrgnUpdate );
434 wndPtr->hrgnUpdate = 0;
439 case 1: /* already an entire window */
443 else if( hRgn == (HRGN)1 )
445 if( wndPtr->hrgnUpdate > (HRGN)1 )
446 DeleteObject( wndPtr->hrgnUpdate );
447 wndPtr->hrgnUpdate = (HRGN)1;
450 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
452 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
453 add_paint_count( wndPtr->hwndSelf, 1 );
455 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
456 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
459 else if( flags & RDW_VALIDATE )
461 if( wndPtr->hrgnUpdate )
465 if( wndPtr->hrgnUpdate == (HRGN)1 )
466 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
468 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
471 DeleteObject( wndPtr->hrgnUpdate );
472 wndPtr->hrgnUpdate = 0;
475 else /* validate everything */
477 if( wndPtr->hrgnUpdate > (HRGN)1 ) DeleteObject( wndPtr->hrgnUpdate );
478 wndPtr->hrgnUpdate = 0;
481 if( !wndPtr->hrgnUpdate )
483 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
484 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
485 add_paint_count( wndPtr->hwndSelf, -1 );
489 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
490 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
494 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
495 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
497 /* in/validate child windows that intersect with the region if it
498 * is a valid handle. */
500 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
503 if( hRgn > (HRGN)1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
505 POINT ptTotal, prevOrigin = {0,0};
509 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
510 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
512 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
514 WND *wnd = WIN_FindWndPtr( list[i] );
516 if( wnd->dwStyle & WS_VISIBLE )
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;
525 ptOffset.x = r.left - prevOrigin.x;
526 ptOffset.y = r.top - prevOrigin.y;
527 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
529 if( RectInRegion( hRgn, &r ) )
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;
539 WIN_ReleaseWndPtr( wnd );
541 HeapFree( GetProcessHeap(), 0, list );
542 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
547 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
552 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
555 for (i = 0; list[i]; i++)
557 WND *wnd = WIN_FindWndPtr( list[i] );
559 if( wnd->dwStyle & WS_VISIBLE )
560 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
561 WIN_ReleaseWndPtr( wnd );
563 HeapFree( GetProcessHeap(), 0, list );
569 /* Set/clear internal paint flag */
571 if (flags & RDW_INTERNALPAINT)
573 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
574 add_paint_count( wndPtr->hwndSelf, 1 );
575 wndPtr->flags |= WIN_INTERNAL_PAINT;
577 else if (flags & RDW_NOINTERNALPAINT)
579 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
580 add_paint_count( wndPtr->hwndSelf, -1 );
581 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
585 /***********************************************************************
586 * RDW_Paint [RedrawWindow() helper]
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.
593 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
595 /* NOTE: wndPtr is locked by caller.
597 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
598 * SendMessage() calls. This is a comment inside DefWindowProc() source
601 * This message avoids lots of inter-app message traffic
602 * by switching to the other task and continuing the
606 * LOWORD(lParam) = hrgnClip
607 * HIWORD(lParam) = hwndSkip (not used; always NULL)
611 HWND hWnd = wndPtr->hwndSelf;
612 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
614 /* Erase/update the window itself ... */
616 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
619 * Check if this window should delay it's processing of WM_NCPAINT.
620 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
622 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
623 ex |= RDW_EX_DELAY_NCPAINT;
625 if (flags & RDW_UPDATENOW)
627 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
628 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
630 else if (flags & RDW_ERASENOW)
632 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
635 hrgnRet = WIN_UpdateNCRgn(wndPtr,
637 UNC_REGION | UNC_CHECK |
638 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
642 if( hrgnRet > (HRGN)1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
643 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
645 if( bIcon ) dcx |= DCX_WINDOW;
648 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
649 wndPtr->rectWindow.top - wndPtr->rectClient.top);
651 dcx &= ~DCX_INTERSECTRGN;
652 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
654 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
656 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
657 ReleaseDC( hWnd, hDC );
663 if( !IsWindow(hWnd) ) return hrgn;
665 /* ... and its child windows */
667 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
668 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
672 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
674 for (phwnd = list; *phwnd; phwnd++)
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);
682 HeapFree( GetProcessHeap(), 0, list );
690 /***********************************************************************
691 * RedrawWindow (USER32.@)
693 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
694 HRGN hrgnUpdate, UINT flags )
701 if (!hwnd) hwnd = GetDesktopWindow();
703 /* check if the window or its parents are visible/not minimized */
705 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
707 /* process pending events and messages before painting */
708 if (flags & RDW_UPDATENOW)
709 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
711 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
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 );
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 );
732 /* prepare an update region in window coordinates */
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;
738 r = wndPtr->rectClient;
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 );
744 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
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.
753 if( wndPtr->hrgnUpdate )
755 hRgn = CreateRectRgn( 0, 0, 0, 0 );
756 CombineRgn( hRgn, hrgnUpdate, 0, RGN_COPY );
757 OffsetRgn( hRgn, pt.x, pt.y );
761 wndPtr->hrgnUpdate = crop_rgn( 0, hrgnUpdate, &r );
762 OffsetRgn( wndPtr->hrgnUpdate, pt.x, pt.y );
765 else if( rectUpdate )
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 );
772 hRgn = CreateRectRgnIndirect( &r2 );
774 else /* entire window or client depending on RDW_FRAME */
776 if( flags & RDW_FRAME )
778 if (wndPtr->hrgnUpdate) hRgn = (HRGN)1;
779 else wndPtr->hrgnUpdate = (HRGN)1;
783 GETCLIENTRECTW( wndPtr, r2 );
784 if( wndPtr->hrgnUpdate == 0 )
785 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
787 hRgn = CreateRectRgnIndirect( &r2 );
791 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
793 /* In this we cannot leave with zero hRgn */
796 hRgn = crop_rgn( hRgn, hrgnUpdate, &r );
797 OffsetRgn( hRgn, pt.x, pt.y );
798 GetRgnBox( hRgn, &r2 );
799 if( IsRectEmpty( &r2 ) ) goto END;
801 else if( rectUpdate )
803 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
804 OffsetRect( &r2, pt.x, pt.y );
805 hRgn = CreateRectRgnIndirect( &r2 );
807 else /* entire window or client depending on RDW_NOFRAME */
809 if( flags & RDW_NOFRAME )
813 GETCLIENTRECTW( wndPtr, r2 );
814 hRgn = CreateRectRgnIndirect( &r2 );
819 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
821 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
823 /* Erase/update windows, from now on hRgn is a scratch region */
825 hRgn = RDW_Paint( wndPtr, (hRgn == (HRGN)1) ? 0 : hRgn, flags, 0 );
828 if( hRgn > (HRGN)1 && (hRgn != hrgnUpdate) )
830 WIN_ReleaseWndPtr(wndPtr);
835 /***********************************************************************
836 * UpdateWindow (USER32.@)
838 BOOL WINAPI UpdateWindow( HWND hwnd )
840 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
844 /***********************************************************************
845 * InvalidateRgn (USER32.@)
847 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
849 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
853 /***********************************************************************
854 * InvalidateRect (USER32.@)
856 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
858 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
862 /***********************************************************************
863 * ValidateRgn (USER32.@)
865 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
867 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
871 /***********************************************************************
872 * ValidateRect (USER32.@)
874 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
876 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
880 /***********************************************************************
881 * GetUpdateRect (USER32.@)
883 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
886 WND * wndPtr = WIN_FindWndPtr( hwnd );
887 if (!wndPtr) return FALSE;
891 if (wndPtr->hrgnUpdate > (HRGN)1)
893 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
894 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
899 GetRgnBox( hrgn, rect );
900 DeleteObject( hrgn );
901 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
903 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
905 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
910 if( wndPtr->hrgnUpdate == (HRGN)1 )
912 GetClientRect( hwnd, rect );
913 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
916 SetRectEmpty( rect );
918 retvalue = (wndPtr->hrgnUpdate >= (HRGN)1);
920 WIN_ReleaseWndPtr(wndPtr);
925 /***********************************************************************
926 * GetUpdateRgn (USER32.@)
928 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
931 WND * wndPtr = WIN_FindWndPtr( hwnd );
932 if (!wndPtr) return ERROR;
934 if (wndPtr->hrgnUpdate == 0)
936 SetRectRgn( hrgn, 0, 0, 0, 0 );
941 if (wndPtr->hrgnUpdate == (HRGN)1)
943 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
944 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
945 retval = SIMPLEREGION;
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 );
953 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
955 WIN_ReleaseWndPtr(wndPtr);
960 /***********************************************************************
961 * ExcludeUpdateRgn (USER32.@)
963 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
968 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
970 if (wndPtr->hrgnUpdate)
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 )
979 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
980 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
981 wndPtr->rectWindow.top - wndPtr->rectClient.top );
984 /* do ugly coordinate translations in dce.c */
986 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
987 DeleteObject( hrgn );
988 WIN_ReleaseWndPtr(wndPtr);
991 WIN_ReleaseWndPtr(wndPtr);
992 return GetClipBox( hdc, &rect );
997 /***********************************************************************
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.
1004 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1008 /* coordinates are logical so we cannot fast-check 'rect',
1009 * it will be done later in the PatBlt().
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 );
1020 /***********************************************************************
1021 * FillRect (USER32.@)
1023 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1027 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1028 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
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 );
1039 /***********************************************************************
1040 * InvertRect (USER.82)
1042 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1044 PatBlt( HDC_32(hdc), rect->left, rect->top,
1045 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1049 /***********************************************************************
1050 * InvertRect (USER32.@)
1052 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1054 return PatBlt( hdc, rect->left, rect->top,
1055 rect->right - rect->left, rect->bottom - rect->top,
1060 /***********************************************************************
1061 * FrameRect (USER32.@)
1063 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1068 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1069 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
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 );
1080 SelectObject( hdc, prevBrush );
1085 /***********************************************************************
1086 * FrameRect (USER.83)
1088 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1091 CONV_RECT16TO32( rect16, &rect );
1092 return FrameRect( HDC_32(hdc), &rect, HBRUSH_32(hbrush) );
1096 /***********************************************************************
1097 * DrawFocusRect (USER.466)
1099 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1102 CONV_RECT16TO32( rc, &rect32 );
1103 DrawFocusRect( HDC_32(hdc), &rect32 );
1107 /***********************************************************************
1108 * DrawFocusRect (USER32.@)
1110 * FIXME: PatBlt(PATINVERT) with background brush.
1112 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1115 HPEN hOldPen, hNewPen;
1116 INT oldDrawMode, oldBkMode;
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);
1124 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1126 SetBkMode(hdc, oldBkMode);
1127 SetROP2(hdc, oldDrawMode);
1128 SelectObject(hdc, hOldPen);
1129 DeleteObject(hNewPen);
1130 SelectObject(hdc, hOldBrush);
1136 /**********************************************************************
1137 * DrawAnimatedRects (USER32.@)
1139 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1140 const RECT* lprcFrom,
1141 const RECT* lprcTo )
1143 FIXME_(win)("(%p,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1148 /**********************************************************************
1149 * PAINTING_DrawStateJam
1151 * Jams in the requested type in the dc
1153 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1154 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1155 LPRECT rc, UINT dtflags, BOOL unicode )
1160 INT cx = rc->right - rc->left;
1161 INT cy = rc->bottom - rc->top;
1166 case DST_PREFIXTEXT:
1168 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1170 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1173 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1176 memdc = CreateCompatibleDC(hdc);
1177 if(!memdc) return FALSE;
1178 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1184 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1185 SelectObject(memdc, hbmsave);
1192 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
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);
1205 /**********************************************************************
1206 * PAINTING_DrawState()
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 )
1211 HBITMAP hbm, hbmsave;
1213 HBRUSH hbsave, hbrtmp = 0;
1216 UINT dtflags = DT_NOCLIP;
1218 UINT opcode = flags & 0xf;
1222 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1225 len = strlenW((LPWSTR)lp);
1227 len = strlen((LPSTR)lp);
1230 /* Find out what size the image has if not given by caller */
1234 CURSORICONINFO *ici;
1240 case DST_PREFIXTEXT:
1242 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1244 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1245 if(!retval) return FALSE;
1249 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1250 if(!ici) return FALSE;
1252 s.cy = ici->nHeight;
1253 GlobalUnlock16((HGLOBAL16)lp);
1257 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1263 case DST_COMPLEX: /* cx and cy must be set in this mode */
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;
1281 /* For DSS_NORMAL we just jam in the image and return */
1282 if((flags & 0x7ff0) == DSS_NORMAL)
1284 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
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 */
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;
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));
1310 /* DST_COMPLEX may draw text as well,
1311 * so we must be sure that correct font is selected
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;
1318 /* This state cause the image to be dithered */
1319 if(flags & DSS_UNION)
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;
1328 if (flags & DSS_DISABLED)
1329 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1330 else if (flags & DSS_DEFAULT)
1331 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1333 /* Draw light or dark shadow */
1334 if (flags & (DSS_DISABLED|DSS_DEFAULT))
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);
1345 if (flags & DSS_DISABLED)
1347 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1348 if(!hbrtmp) goto cleanup;
1352 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1355 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1357 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1359 retval = TRUE; /* We succeeded */
1362 SetTextColor(hdc, fg);
1363 SetBkColor(hdc, bg);
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);
1374 /**********************************************************************
1375 * DrawStateA (USER32.@)
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)
1381 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1384 /**********************************************************************
1385 * DrawStateW (USER32.@)
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)
1391 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1395 /***********************************************************************
1396 * SelectPalette (Not a Windows API)
1398 HPALETTE WINAPI SelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
1400 WORD wBkgPalette = 1;
1402 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1404 HWND hwnd = WindowFromDC( hDC );
1407 HWND hForeground = GetForegroundWindow();
1408 /* set primary palette if it's related to current active */
1409 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1412 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
1416 /***********************************************************************
1417 * UserRealizePalette (USER32.@)
1419 UINT WINAPI UserRealizePalette( HDC hDC )
1421 UINT realized = pfnGDIRealizePalette( hDC );
1423 /* do not send anything if no colors were changed */
1424 if (realized && IsDCCurrentPalette16( HDC_16(hDC) ))
1426 /* send palette change notification */
1427 HWND hWnd = WindowFromDC( hDC );
1428 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0L);