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
23 #include "wine/port.h"
30 #include "wine/winuser16.h"
32 #include "wine/unicode.h"
33 #include "wine/server.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
41 WINE_DECLARE_DEBUG_CHANNEL(nonclient);
43 /* client rect in window coordinates */
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
50 /* PAINT_RedrawWindow() control flags */
51 #define RDW_EX_DELAY_NCPAINT 0x0020
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
62 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
64 HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = NULL;
65 UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc) = NULL;
68 /***********************************************************************
71 * Add an increment (normally 1 or -1) to the current paint count of a window.
73 static void add_paint_count( HWND hwnd, int incr )
75 SERVER_START_REQ( inc_window_paint_count )
79 wine_server_call( req );
85 /***********************************************************************
88 * hSrc: Region to crop.
89 * lpRect: Clipping rectangle.
91 * hDst: Region to hold the result (a new region is created if it's 0).
92 * Allowed to be the same region as hSrc in which case everything
93 * will be done in place, with no memory reallocations.
95 * Returns: hDst if success, 0 otherwise.
97 static HRGN crop_rgn( HRGN hDst, HRGN hSrc, const RECT *rect )
99 HRGN h = CreateRectRgnIndirect( rect );
100 if (hDst == 0) hDst = h;
101 CombineRgn( hDst, hSrc, h, RGN_AND );
102 if (hDst != h) DeleteObject( h );
107 /***********************************************************************
108 * WIN_HaveToDelayNCPAINT
110 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
111 * is generated as soon as a region intersecting the non-client area
112 * of a window is invalidated.
114 * This technique will work fine for all windows whose parents
115 * have the WS_CLIPCHILDREN style. When the parents have that style,
116 * they are not going to override the contents of their children.
117 * However, when the parent doesn't have that style, Windows relies
118 * on a "painter's algorithm" to display the contents of the windows.
119 * That is, windows are painted from back to front. This includes the
122 * This method looks at the current state of a window to determine
123 * if the sending of the WM_NCPAINT message should be delayed until
124 * the BeginPaint call.
127 * wndPtr - A Locked window pointer to the window we're
129 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
130 * function. This is a shortcut for the cases when
131 * we already know when to avoid scanning all the
132 * parents of a window. If you already know that this
133 * window's NCPAINT should be delayed, set the
134 * UNC_DELAY_NCPAINT flag for this parameter.
136 * This shortcut behavior is implemented in the
137 * RDW_Paint() method.
140 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
143 * Test the shortcut first. (duh)
145 if (uncFlags & UNC_DELAY_NCPAINT)
149 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
150 * method only. This is another shortcut to avoid going
151 * up the parent's chain of the window to finally
152 * figure-out that none of them has an invalid region.
154 if (uncFlags & UNC_IN_BEGINPAINT)
158 * Scan all the parents of this window to find a window
159 * that doesn't have the WS_CLIPCHILDREN style and that
160 * has an invalid region.
162 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
164 WND* parentWnd = WIN_FindWndPtr( hwnd );
165 if (parentWnd && !(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
167 WIN_ReleaseWndPtr( parentWnd );
170 WIN_ReleaseWndPtr( parentWnd );
175 /***********************************************************************
179 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
180 * Crop hrgnUpdate to a client rect, especially if it 1.
181 * If UNC_REGION is set return update region for the client rect.
183 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
184 * The trick is that when the returned region handle may be different from hRgn.
185 * In this case the old hRgn must be considered gone. BUT, if the returned value
186 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
187 * a DC without extra clipping region.
189 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
195 TRACE_(nonclient)("hwnd %p [%p] hrgn %p, unc %04x, ncf %i\n",
196 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
198 /* desktop window doesn't have a nonclient area */
199 if(wnd->hwndSelf == GetDesktopWindow())
201 wnd->flags &= ~WIN_NEEDS_NCPAINT;
202 if( wnd->hrgnUpdate > (HRGN)1 )
204 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
205 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
210 hrgnRet = wnd->hrgnUpdate;
215 if ((wnd->hwndSelf == GetForegroundWindow()) &&
216 !(wnd->flags & WIN_NCACTIVATED) )
218 wnd->flags |= WIN_NCACTIVATED;
219 uncFlags |= UNC_ENTIRE;
223 * If the window's non-client area needs to be painted,
225 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
226 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
230 wnd->flags &= ~WIN_NEEDS_NCPAINT;
231 GETCLIENTRECTW( wnd, r );
233 TRACE_(nonclient)( "\tclient box (%ld,%ld-%ld,%ld), hrgnUpdate %p\n",
234 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
235 if( wnd->hrgnUpdate > (HRGN)1 )
237 /* Check if update rgn overlaps with nonclient area */
239 GetRgnBox( wnd->hrgnUpdate, &r2 );
240 UnionRect( &r3, &r2, &r );
241 if( r3.left != r.left || r3.top != r.top ||
242 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
244 /* crop hrgnUpdate, save old one in hClip - the only
245 * case that places a valid region handle in hClip */
247 hClip = wnd->hrgnUpdate;
248 wnd->hrgnUpdate = crop_rgn( hRgn, hClip, &r );
249 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
252 if( uncFlags & UNC_CHECK )
254 GetRgnBox( wnd->hrgnUpdate, &r3 );
255 if( IsRectEmpty( &r3 ) )
257 /* delete the update region since all invalid
258 * parts were in the nonclient area */
260 DeleteObject( wnd->hrgnUpdate );
262 if(!(wnd->flags & WIN_INTERNAL_PAINT))
263 add_paint_count( wnd->hwndSelf, -1 );
265 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
269 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
272 if( wnd->hrgnUpdate == (HRGN)1 )/* entire window */
274 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
275 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
276 uncFlags |= UNC_ENTIRE;
279 else /* no WM_NCPAINT unless forced */
281 if( wnd->hrgnUpdate > (HRGN)1 )
284 if( uncFlags & UNC_REGION )
286 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
287 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
292 if( wnd->hrgnUpdate == (HRGN)1 && (uncFlags & UNC_UPDATE) )
294 GETCLIENTRECTW( wnd, r );
295 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
296 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
300 if(!hClip && (uncFlags & UNC_ENTIRE) )
302 /* still don't do anything if there is no nonclient area */
303 hClip = (HRGN)(memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
306 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
308 if ( hClip == hrgnRet && hrgnRet > (HRGN)1 ) {
309 hClip = CreateRectRgn( 0, 0, 0, 0 );
310 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
313 SendMessageA( wnd->hwndSelf, WM_NCPAINT, (WPARAM)hClip, 0L );
314 if( (hClip > (HRGN)1) && (hClip != hRgn) && (hClip != hrgnRet) )
315 DeleteObject( hClip );
317 * Since all Window locks are suspended while processing the WM_NCPAINT
318 * we want to make sure the window still exists before continuing.
320 if (!IsWindow(wnd->hwndSelf))
322 DeleteObject(hrgnRet);
327 TRACE_(nonclient)("returning %p (hClip = %p, hrgnUpdate = %p)\n", hrgnRet, hClip, wnd->hrgnUpdate );
333 /***********************************************************************
334 * RDW_ValidateParent [RDW_UpdateRgns() helper]
336 * Validate the portions of parents that are covered by a validated child
339 static void RDW_ValidateParent(WND *wndChild)
344 if (wndChild->hrgnUpdate == (HRGN)1 ) {
348 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
349 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
350 hrg = CreateRectRgnIndirect( &r );
352 hrg = wndChild->hrgnUpdate;
354 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
355 while (parent && parent != GetDesktopWindow())
357 WND *wndParent = WIN_FindWndPtr( parent );
358 if (wndParent && !(wndParent->dwStyle & WS_CLIPCHILDREN))
360 if (wndParent->hrgnUpdate != 0)
363 RECT rect, rectParent;
364 if( wndParent->hrgnUpdate == (HRGN)1 )
370 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
371 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
373 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
375 /* we must offset the child region by the offset of the child rect in the parent */
376 GetWindowRect(wndParent->hwndSelf, &rectParent);
377 GetWindowRect(wndChild->hwndSelf, &rect);
378 ptOffset.x = rect.left - rectParent.left;
379 ptOffset.y = rect.top - rectParent.top;
380 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
381 if (CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF ) == NULLREGION)
383 /* the update region has become empty */
384 DeleteObject( wndParent->hrgnUpdate );
385 wndParent->hrgnUpdate = 0;
386 wndParent->flags &= ~WIN_NEEDS_ERASEBKGND;
387 if( !(wndParent->flags & WIN_INTERNAL_PAINT) )
388 add_paint_count( wndParent->hwndSelf, -1 );
390 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
393 WIN_ReleaseWndPtr( wndParent );
394 parent = GetAncestor( parent, GA_PARENT );
396 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
399 /***********************************************************************
400 * RDW_UpdateRgns [RedrawWindow() helper]
402 * Walks the window tree and adds/removes parts of the hRgn to/from update
403 * regions of windows that overlap it. Also, manages internal paint flags.
405 * NOTE: Walks the window tree so the caller must lock it.
406 * MUST preserve hRgn (can modify but then has to restore).
408 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
411 * Called only when one of the following is set:
412 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
415 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
416 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
417 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
422 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
423 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
425 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
427 if( flags & RDW_INVALIDATE )
431 switch ((UINT)wndPtr->hrgnUpdate)
434 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
437 wndPtr->hrgnUpdate = crop_rgn( wndPtr->hrgnUpdate,
438 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
442 GetRgnBox( wndPtr->hrgnUpdate, &r );
443 if( IsRectEmpty( &r ) )
445 DeleteObject( wndPtr->hrgnUpdate );
446 wndPtr->hrgnUpdate = 0;
451 case 1: /* already an entire window */
455 else if( hRgn == (HRGN)1 )
457 if( wndPtr->hrgnUpdate > (HRGN)1 )
458 DeleteObject( wndPtr->hrgnUpdate );
459 wndPtr->hrgnUpdate = (HRGN)1;
464 if( wndPtr->hrgnUpdate > (HRGN)1)
466 GetRgnBox( wndPtr->hrgnUpdate, &r );
467 if( IsRectEmpty( &r ) )
469 DeleteObject( wndPtr->hrgnUpdate );
470 wndPtr->hrgnUpdate = 0;
474 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends
475 * on code in RDW_Paint() */
478 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
479 add_paint_count( wndPtr->hwndSelf, 1 );
481 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
482 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
485 else if( flags & RDW_VALIDATE )
487 if( wndPtr->hrgnUpdate )
491 if( wndPtr->hrgnUpdate == (HRGN)1 )
492 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
494 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
497 DeleteObject( wndPtr->hrgnUpdate );
498 wndPtr->hrgnUpdate = 0;
501 else /* validate everything */
503 if( wndPtr->hrgnUpdate > (HRGN)1 ) DeleteObject( wndPtr->hrgnUpdate );
504 wndPtr->hrgnUpdate = 0;
507 if( !wndPtr->hrgnUpdate )
509 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
510 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
511 add_paint_count( wndPtr->hwndSelf, -1 );
515 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
516 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
520 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
521 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
523 /* in/validate child windows that intersect with the region if it
524 * is a valid handle. */
526 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
529 if( hRgn > (HRGN)1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
531 POINT ptTotal, prevOrigin = {0,0};
535 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
536 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
538 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
540 WND *wnd = WIN_FindWndPtr( list[i] );
542 if( wnd->dwStyle & WS_VISIBLE )
546 r.left = wnd->rectWindow.left + ptClient.x;
547 r.right = wnd->rectWindow.right + ptClient.x;
548 r.top = wnd->rectWindow.top + ptClient.y;
549 r.bottom = wnd->rectWindow.bottom + ptClient.y;
551 ptOffset.x = r.left - prevOrigin.x;
552 ptOffset.y = r.top - prevOrigin.y;
553 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
555 if( RectInRegion( hRgn, &r ) )
557 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
558 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
559 prevOrigin.x = r.left + ptTotal.x;
560 prevOrigin.y = r.top + ptTotal.y;
561 ptTotal.x += ptOffset.x;
562 ptTotal.y += ptOffset.y;
565 WIN_ReleaseWndPtr( wnd );
567 HeapFree( GetProcessHeap(), 0, list );
568 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
573 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
578 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
581 for (i = 0; list[i]; i++)
583 WND *wnd = WIN_FindWndPtr( list[i] );
585 if( wnd->dwStyle & WS_VISIBLE )
586 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
587 WIN_ReleaseWndPtr( wnd );
589 HeapFree( GetProcessHeap(), 0, list );
595 /* Set/clear internal paint flag */
597 if (flags & RDW_INTERNALPAINT)
599 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
600 add_paint_count( wndPtr->hwndSelf, 1 );
601 wndPtr->flags |= WIN_INTERNAL_PAINT;
603 else if (flags & RDW_NOINTERNALPAINT)
605 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
606 add_paint_count( wndPtr->hwndSelf, -1 );
607 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
611 /***********************************************************************
612 * RDW_Paint [RedrawWindow() helper]
614 * Walks the window tree and paints/erases windows that have
615 * nonzero update regions according to redraw flags. hrgn is a scratch
616 * region passed down during recursion. Must not be 1.
619 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
621 /* NOTE: wndPtr is locked by caller.
623 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
624 * SendMessage() calls. This is a comment inside DefWindowProc() source
627 * This message avoids lots of inter-app message traffic
628 * by switching to the other task and continuing the
632 * LOWORD(lParam) = hrgnClip
633 * HIWORD(lParam) = hwndSkip (not used; always NULL)
637 HWND hWnd = wndPtr->hwndSelf;
638 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
640 /* Erase/update the window itself ... */
642 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
645 * Check if this window should delay it's processing of WM_NCPAINT.
646 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
648 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
649 ex |= RDW_EX_DELAY_NCPAINT;
651 if (flags & RDW_UPDATENOW)
653 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
654 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
656 else if (flags & RDW_ERASENOW)
658 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
661 hrgnRet = WIN_UpdateNCRgn(wndPtr,
663 UNC_REGION | UNC_CHECK |
664 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
668 if( hrgnRet > (HRGN)1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
669 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
671 if( bIcon ) dcx |= DCX_WINDOW;
674 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
675 wndPtr->rectWindow.top - wndPtr->rectClient.top);
677 dcx &= ~DCX_INTERSECTRGN;
678 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
680 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
682 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
683 ReleaseDC( hWnd, hDC );
689 if( !IsWindow(hWnd) ) return hrgn;
691 /* ... and its child windows */
693 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
694 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
698 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
700 for (phwnd = list; *phwnd; phwnd++)
702 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
703 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
704 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
705 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
706 WIN_ReleaseWndPtr(wndPtr);
708 HeapFree( GetProcessHeap(), 0, list );
716 /***********************************************************************
719 static void dump_rdw_flags(UINT flags)
722 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
723 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
724 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
725 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
726 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
727 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
728 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
729 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
730 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
731 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
732 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
733 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
737 RDW_INTERNALPAINT | \
740 RDW_NOINTERNALPAINT | \
749 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
755 /***********************************************************************
756 * RedrawWindow (USER32.@)
758 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
759 HRGN hrgnUpdate, UINT flags )
766 if (!hwnd) hwnd = GetDesktopWindow();
768 /* check if the window or its parents are visible/not minimized */
770 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
772 /* process pending events and messages before painting */
773 if (flags & RDW_UPDATENOW)
774 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
776 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
781 GetRgnBox( hrgnUpdate, &r );
782 TRACE( "%p (%p) NULL %p box (%ld,%ld-%ld,%ld) flags=%04x\n",
783 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
791 TRACE( "%p (%p) %s %ld,%ld-%ld,%ld %p flags=%04x\n",
792 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
793 r.top, r.right, r.bottom, hrgnUpdate, flags );
795 dump_rdw_flags(flags);
798 /* prepare an update region in window coordinates */
800 if (((flags & (RDW_INVALIDATE|RDW_FRAME)) == (RDW_INVALIDATE|RDW_FRAME)) ||
801 ((flags & (RDW_VALIDATE|RDW_NOFRAME)) == (RDW_VALIDATE|RDW_NOFRAME)))
802 r = wndPtr->rectWindow;
804 r = wndPtr->rectClient;
806 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
807 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
808 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
810 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
812 /* If the window doesn't have hrgnUpdate we leave hRgn zero
813 * and put a new region straight into wndPtr->hrgnUpdate
814 * so that RDW_UpdateRgns() won't have to do any extra work.
819 if( wndPtr->hrgnUpdate )
821 hRgn = CreateRectRgn( 0, 0, 0, 0 );
822 CombineRgn( hRgn, hrgnUpdate, 0, RGN_COPY );
823 OffsetRgn( hRgn, pt.x, pt.y );
827 wndPtr->hrgnUpdate = crop_rgn( 0, hrgnUpdate, &r );
828 OffsetRgn( wndPtr->hrgnUpdate, pt.x, pt.y );
831 else if( rectUpdate )
833 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
834 OffsetRect( &r2, pt.x, pt.y );
835 if( wndPtr->hrgnUpdate == 0 )
836 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
838 hRgn = CreateRectRgnIndirect( &r2 );
840 else /* entire window or client depending on RDW_FRAME */
842 if( flags & RDW_FRAME )
844 if (wndPtr->hrgnUpdate) hRgn = (HRGN)1;
845 else wndPtr->hrgnUpdate = (HRGN)1;
849 GETCLIENTRECTW( wndPtr, r2 );
850 if( wndPtr->hrgnUpdate == 0 )
851 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
853 hRgn = CreateRectRgnIndirect( &r2 );
857 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
859 /* In this we cannot leave with zero hRgn */
862 hRgn = crop_rgn( hRgn, hrgnUpdate, &r );
863 OffsetRgn( hRgn, pt.x, pt.y );
864 GetRgnBox( hRgn, &r2 );
865 if( IsRectEmpty( &r2 ) ) goto END;
867 else if( rectUpdate )
869 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
870 OffsetRect( &r2, pt.x, pt.y );
871 hRgn = CreateRectRgnIndirect( &r2 );
873 else /* entire window or client depending on RDW_NOFRAME */
875 if( flags & RDW_NOFRAME )
879 GETCLIENTRECTW( wndPtr, r2 );
880 hRgn = CreateRectRgnIndirect( &r2 );
885 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
887 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
889 /* Erase/update windows, from now on hRgn is a scratch region */
891 hRgn = RDW_Paint( wndPtr, (hRgn == (HRGN)1) ? 0 : hRgn, flags, 0 );
894 if( hRgn > (HRGN)1 && (hRgn != hrgnUpdate) )
896 WIN_ReleaseWndPtr(wndPtr);
901 /***********************************************************************
902 * UpdateWindow (USER32.@)
904 BOOL WINAPI UpdateWindow( HWND hwnd )
906 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
910 /***********************************************************************
911 * InvalidateRgn (USER32.@)
913 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
915 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
919 /***********************************************************************
920 * InvalidateRect (USER32.@)
922 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
924 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
928 /***********************************************************************
929 * ValidateRgn (USER32.@)
931 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
933 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
937 /***********************************************************************
938 * ValidateRect (USER32.@)
940 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
942 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
946 /***********************************************************************
947 * FillRect (USER32.@)
949 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
953 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
954 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
957 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
958 PatBlt( hdc, rect->left, rect->top,
959 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
960 SelectObject( hdc, prevBrush );
965 /***********************************************************************
966 * InvertRect (USER32.@)
968 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
970 return PatBlt( hdc, rect->left, rect->top,
971 rect->right - rect->left, rect->bottom - rect->top,
976 /***********************************************************************
977 * FrameRect (USER32.@)
979 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
984 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
985 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
987 PatBlt( hdc, r.left, r.top, 1,
988 r.bottom - r.top, PATCOPY );
989 PatBlt( hdc, r.right - 1, r.top, 1,
990 r.bottom - r.top, PATCOPY );
991 PatBlt( hdc, r.left, r.top,
992 r.right - r.left, 1, PATCOPY );
993 PatBlt( hdc, r.left, r.bottom - 1,
994 r.right - r.left, 1, PATCOPY );
996 SelectObject( hdc, prevBrush );
1001 /***********************************************************************
1002 * DrawFocusRect (USER32.@)
1004 * FIXME: PatBlt(PATINVERT) with background brush.
1006 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1009 HPEN hOldPen, hNewPen;
1010 INT oldDrawMode, oldBkMode;
1012 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1013 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1014 hOldPen = SelectObject(hdc, hNewPen);
1015 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1016 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1018 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1020 SetBkMode(hdc, oldBkMode);
1021 SetROP2(hdc, oldDrawMode);
1022 SelectObject(hdc, hOldPen);
1023 DeleteObject(hNewPen);
1024 SelectObject(hdc, hOldBrush);
1030 /**********************************************************************
1031 * DrawAnimatedRects (USER32.@)
1033 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1034 const RECT* lprcFrom,
1035 const RECT* lprcTo )
1037 FIXME("(%p,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1042 /**********************************************************************
1043 * PAINTING_DrawStateJam
1045 * Jams in the requested type in the dc
1047 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1048 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1049 LPRECT rc, UINT dtflags, BOOL unicode )
1054 INT cx = rc->right - rc->left;
1055 INT cy = rc->bottom - rc->top;
1060 case DST_PREFIXTEXT:
1062 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1064 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1067 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1070 memdc = CreateCompatibleDC(hdc);
1071 if(!memdc) return FALSE;
1072 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1078 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1079 SelectObject(memdc, hbmsave);
1086 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1088 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1089 bRet = func(hdc, lp, wp, cx, cy);
1090 /* Restore origin */
1091 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1099 /**********************************************************************
1100 * PAINTING_DrawState()
1102 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1103 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1105 HBITMAP hbm, hbmsave;
1107 HBRUSH hbsave, hbrtmp = 0;
1110 UINT dtflags = DT_NOCLIP;
1112 UINT opcode = flags & 0xf;
1116 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1119 len = strlenW((LPWSTR)lp);
1121 len = strlen((LPSTR)lp);
1124 /* Find out what size the image has if not given by caller */
1128 CURSORICONINFO *ici;
1134 case DST_PREFIXTEXT:
1136 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1138 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1139 if(!retval) return FALSE;
1143 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1144 if(!ici) return FALSE;
1146 s.cy = ici->nHeight;
1147 GlobalUnlock16((HGLOBAL16)lp);
1151 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1157 case DST_COMPLEX: /* cx and cy must be set in this mode */
1170 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1171 dtflags |= DT_RIGHT;
1172 if(opcode == DST_TEXT)
1173 dtflags |= DT_NOPREFIX;
1175 /* For DSS_NORMAL we just jam in the image and return */
1176 if((flags & 0x7ff0) == DSS_NORMAL)
1178 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1181 /* For all other states we need to convert the image to B/W in a local bitmap */
1182 /* before it is displayed */
1183 fg = SetTextColor(hdc, RGB(0, 0, 0));
1184 bg = SetBkColor(hdc, RGB(255, 255, 255));
1185 hbm = NULL; hbmsave = NULL;
1186 memdc = NULL; hbsave = NULL;
1187 retval = FALSE; /* assume failure */
1189 /* From here on we must use "goto cleanup" when something goes wrong */
1190 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1191 if(!hbm) goto cleanup;
1192 memdc = CreateCompatibleDC(hdc);
1193 if(!memdc) goto cleanup;
1194 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1195 if(!hbmsave) goto cleanup;
1196 rc.left = rc.top = 0;
1199 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1200 SetBkColor(memdc, RGB(255, 255, 255));
1201 SetTextColor(memdc, RGB(0, 0, 0));
1202 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1204 /* DST_COMPLEX may draw text as well,
1205 * so we must be sure that correct font is selected
1207 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1208 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1209 if(hfsave) SelectObject(memdc, hfsave);
1210 if(!tmp) goto cleanup;
1212 /* This state cause the image to be dithered */
1213 if(flags & DSS_UNION)
1215 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1216 if(!hbsave) goto cleanup;
1217 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1218 SelectObject(memdc, hbsave);
1219 if(!tmp) goto cleanup;
1222 if (flags & DSS_DISABLED)
1223 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1224 else if (flags & DSS_DEFAULT)
1225 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1227 /* Draw light or dark shadow */
1228 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1230 if(!hbrtmp) goto cleanup;
1231 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1232 if(!hbsave) goto cleanup;
1233 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1234 SelectObject(hdc, hbsave);
1235 DeleteObject(hbrtmp);
1239 if (flags & DSS_DISABLED)
1241 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1242 if(!hbrtmp) goto cleanup;
1246 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1249 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1251 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1253 retval = TRUE; /* We succeeded */
1256 SetTextColor(hdc, fg);
1257 SetBkColor(hdc, bg);
1259 if(hbsave) SelectObject(hdc, hbsave);
1260 if(hbmsave) SelectObject(memdc, hbmsave);
1261 if(hbrtmp) DeleteObject(hbrtmp);
1262 if(hbm) DeleteObject(hbm);
1263 if(memdc) DeleteDC(memdc);
1268 /**********************************************************************
1269 * DrawStateA (USER32.@)
1271 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1272 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1273 INT x, INT y, INT cx, INT cy, UINT flags)
1275 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1278 /**********************************************************************
1279 * DrawStateW (USER32.@)
1281 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1282 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1283 INT x, INT y, INT cx, INT cy, UINT flags)
1285 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1289 /***********************************************************************
1290 * SelectPalette (Not a Windows API)
1292 HPALETTE WINAPI SelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
1294 WORD wBkgPalette = 1;
1296 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1298 HWND hwnd = WindowFromDC( hDC );
1301 HWND hForeground = GetForegroundWindow();
1302 /* set primary palette if it's related to current active */
1303 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1306 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
1310 /***********************************************************************
1311 * UserRealizePalette (USER32.@)
1313 UINT WINAPI UserRealizePalette( HDC hDC )
1315 UINT realized = pfnGDIRealizePalette( hDC );
1317 /* do not send anything if no colors were changed */
1318 if (realized && IsDCCurrentPalette16( HDC_16(hDC) ))
1320 /* send palette change notification */
1321 HWND hWnd = WindowFromDC( hDC );
1322 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
1323 SMTO_ABORTIFHUNG, 2000, NULL );