2 * Window painting functions
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
11 #include "wine/winuser16.h"
12 #include "wine/unicode.h"
13 #include "wine/server.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(win);
22 DECLARE_DEBUG_CHANNEL(nonclient);
24 /* client rect in window coordinates */
26 #define GETCLIENTRECTW( wnd, r ) (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
27 (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
28 (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
29 (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
31 /* PAINT_RedrawWindow() control flags */
32 #define RDW_EX_DELAY_NCPAINT 0x0020
34 /* WIN_UpdateNCRgn() flags */
35 #define UNC_CHECK 0x0001
36 #define UNC_ENTIRE 0x0002
37 #define UNC_REGION 0x0004
38 #define UNC_UPDATE 0x0008
39 #define UNC_DELAY_NCPAINT 0x0010
40 #define UNC_IN_BEGINPAINT 0x0020
43 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
46 /* ### start build ### */
47 extern WORD CALLBACK PAINTING_CallTo16_word_wlwww(DRAWSTATEPROC16,WORD,LONG,WORD,WORD,WORD);
48 /* ### stop build ### */
50 struct draw_state_info
56 /* callback for 16-bit DrawState functions */
57 static BOOL CALLBACK draw_state_callback( HDC hdc, LPARAM lparam, WPARAM wparam, int cx, int cy )
59 const struct draw_state_info *info = (struct draw_state_info *)lparam;
60 return PAINTING_CallTo16_word_wlwww( info->proc, hdc, info->param, wparam, cx, cy );
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 /***********************************************************************
82 * WIN_HaveToDelayNCPAINT
84 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
85 * is generated as soon as a region intersecting the non-client area
86 * of a window is invalidated.
88 * This technique will work fine for all windows whose parents
89 * have the WS_CLIPCHILDREN style. When the parents have that style,
90 * they are not going to override the contents of their children.
91 * However, when the parent doesn't have that style, Windows relies
92 * on a "painter's algorithm" to display the contents of the windows.
93 * That is, windows are painted from back to front. This includes the
96 * This method looks at the current state of a window to determine
97 * if the sending of the WM_NCPAINT message should be delayed until
98 * the BeginPaint call.
101 * wndPtr - A Locked window pointer to the window we're
103 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
104 * function. This is a shortcut for the cases when
105 * we already know when to avoid scanning all the
106 * parents of a window. If you already know that this
107 * window's NCPAINT should be delayed, set the
108 * UNC_DELAY_NCPAINT flag for this parameter.
110 * This shortcut behavior is implemented in the
111 * RDW_Paint() method.
114 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
117 * Test the shortcut first. (duh)
119 if (uncFlags & UNC_DELAY_NCPAINT)
123 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
124 * method only. This is another shortcut to avoid going
125 * up the parent's chain of the window to finally
126 * figure-out that none of them has an invalid region.
128 if (uncFlags & UNC_IN_BEGINPAINT)
132 * Scan all the parents of this window to find a window
133 * that doesn't have the WS_CLIPCHILDREN style and that
134 * has an invalid region.
136 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
138 WND* parentWnd = WIN_FindWndPtr( hwnd );
139 if (!(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
141 WIN_ReleaseWndPtr( parentWnd );
144 WIN_ReleaseWndPtr( parentWnd );
149 /***********************************************************************
153 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
154 * Crop hrgnUpdate to a client rect, especially if it 1.
155 * If UNC_REGION is set return update region for the client rect.
157 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
158 * The trick is that when the returned region handle may be different from hRgn.
159 * In this case the old hRgn must be considered gone. BUT, if the returned value
160 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
161 * a DC without extra clipping region.
163 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
169 TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n",
170 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
172 /* desktop window doesn't have a nonclient area */
173 if(wnd->hwndSelf == GetDesktopWindow())
175 wnd->flags &= ~WIN_NEEDS_NCPAINT;
176 if( wnd->hrgnUpdate > 1 )
177 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
180 hrgnRet = wnd->hrgnUpdate;
185 if ((wnd->hwndSelf == GetForegroundWindow()) &&
186 !(wnd->flags & WIN_NCACTIVATED) )
188 wnd->flags |= WIN_NCACTIVATED;
189 uncFlags |= UNC_ENTIRE;
193 * If the window's non-client area needs to be painted,
195 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
196 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
200 wnd->flags &= ~WIN_NEEDS_NCPAINT;
201 GETCLIENTRECTW( wnd, r );
203 TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n",
204 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
205 if( wnd->hrgnUpdate > 1 )
207 /* Check if update rgn overlaps with nonclient area */
209 GetRgnBox( wnd->hrgnUpdate, &r2 );
210 UnionRect( &r3, &r2, &r );
211 if( r3.left != r.left || r3.top != r.top ||
212 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
214 /* crop hrgnUpdate, save old one in hClip - the only
215 * case that places a valid region handle in hClip */
217 hClip = wnd->hrgnUpdate;
218 wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
219 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
222 if( uncFlags & UNC_CHECK )
224 GetRgnBox( wnd->hrgnUpdate, &r3 );
225 if( IsRectEmpty( &r3 ) )
227 /* delete the update region since all invalid
228 * parts were in the nonclient area */
230 DeleteObject( wnd->hrgnUpdate );
232 if(!(wnd->flags & WIN_INTERNAL_PAINT))
233 add_paint_count( wnd->hwndSelf, -1 );
235 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
239 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
242 if( wnd->hrgnUpdate == 1 )/* entire window */
244 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
245 if( uncFlags & UNC_REGION ) hrgnRet = 1;
246 uncFlags |= UNC_ENTIRE;
249 else /* no WM_NCPAINT unless forced */
251 if( wnd->hrgnUpdate > 1 )
254 if( uncFlags & UNC_REGION )
255 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
258 if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
260 GETCLIENTRECTW( wnd, r );
261 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
262 if( uncFlags & UNC_REGION ) hrgnRet = 1;
266 if(!hClip && (uncFlags & UNC_ENTIRE) )
268 /* still don't do anything if there is no nonclient area */
269 hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
272 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
274 if ( hClip == hrgnRet && hrgnRet > 1 ) {
275 hClip = CreateRectRgn( 0, 0, 0, 0 );
276 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
279 SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
280 if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
281 DeleteObject( hClip );
283 * Since all Window locks are suspended while processing the WM_NCPAINT
284 * we want to make sure the window still exists before continuing.
286 if (!IsWindow(wnd->hwndSelf))
288 DeleteObject(hrgnRet);
293 TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
299 /***********************************************************************
300 * RDW_ValidateParent [RDW_UpdateRgns() helper]
302 * Validate the portions of parents that are covered by a validated child
305 static void RDW_ValidateParent(WND *wndChild)
310 if (wndChild->hrgnUpdate == 1 ) {
314 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
315 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
316 hrg = CreateRectRgnIndirect( &r );
318 hrg = wndChild->hrgnUpdate;
320 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
321 while (parent && parent != GetDesktopWindow())
323 WND *wndParent = WIN_FindWndPtr( parent );
324 if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
326 if (wndParent->hrgnUpdate != 0)
329 RECT rect, rectParent;
330 if( wndParent->hrgnUpdate == 1 )
336 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
337 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
339 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
341 /* we must offset the child region by the offset of the child rect in the parent */
342 GetWindowRect(wndParent->hwndSelf, &rectParent);
343 GetWindowRect(wndChild->hwndSelf, &rect);
344 ptOffset.x = rect.left - rectParent.left;
345 ptOffset.y = rect.top - rectParent.top;
346 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
347 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
348 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
351 WIN_ReleaseWndPtr( wndParent );
352 parent = GetAncestor( parent, GA_PARENT );
354 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
357 /***********************************************************************
358 * RDW_UpdateRgns [RedrawWindow() helper]
360 * Walks the window tree and adds/removes parts of the hRgn to/from update
361 * regions of windows that overlap it. Also, manages internal paint flags.
363 * NOTE: Walks the window tree so the caller must lock it.
364 * MUST preserve hRgn (can modify but then has to restore).
366 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
369 * Called only when one of the following is set:
370 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
373 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
374 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
375 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
380 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
381 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
383 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
385 if( flags & RDW_INVALIDATE )
389 switch( wndPtr->hrgnUpdate )
392 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
395 wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate,
396 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
400 GetRgnBox( wndPtr->hrgnUpdate, &r );
401 if( IsRectEmpty( &r ) )
403 DeleteObject( wndPtr->hrgnUpdate );
404 wndPtr->hrgnUpdate = 0;
409 case 1: /* already an entire window */
415 if( wndPtr->hrgnUpdate > 1 )
416 DeleteObject( wndPtr->hrgnUpdate );
417 wndPtr->hrgnUpdate = 1;
420 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
422 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
423 add_paint_count( wndPtr->hwndSelf, 1 );
425 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
426 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
429 else if( flags & RDW_VALIDATE )
431 if( wndPtr->hrgnUpdate )
435 if( wndPtr->hrgnUpdate == 1 )
436 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
438 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
441 DeleteObject( wndPtr->hrgnUpdate );
442 wndPtr->hrgnUpdate = 0;
445 else /* validate everything */
447 if( wndPtr->hrgnUpdate > 1 ) DeleteObject( wndPtr->hrgnUpdate );
448 wndPtr->hrgnUpdate = 0;
451 if( !wndPtr->hrgnUpdate )
453 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
454 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
455 add_paint_count( wndPtr->hwndSelf, -1 );
459 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
460 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
464 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
465 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
467 /* in/validate child windows that intersect with the region if it
468 * is a valid handle. */
470 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
473 if( hRgn > 1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
475 POINT ptTotal, prevOrigin = {0,0};
479 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
480 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
482 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
484 WND *wnd = WIN_FindWndPtr( list[i] );
486 if( wnd->dwStyle & WS_VISIBLE )
490 r.left = wnd->rectWindow.left + ptClient.x;
491 r.right = wnd->rectWindow.right + ptClient.x;
492 r.top = wnd->rectWindow.top + ptClient.y;
493 r.bottom = wnd->rectWindow.bottom + ptClient.y;
495 ptOffset.x = r.left - prevOrigin.x;
496 ptOffset.y = r.top - prevOrigin.y;
497 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
499 if( RectInRegion( hRgn, &r ) )
501 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
502 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
503 prevOrigin.x = r.left + ptTotal.x;
504 prevOrigin.y = r.top + ptTotal.y;
505 ptTotal.x += ptOffset.x;
506 ptTotal.y += ptOffset.y;
509 WIN_ReleaseWndPtr( wnd );
511 HeapFree( GetProcessHeap(), 0, list );
512 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
517 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
522 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
525 for (i = 0; list[i]; i++)
527 WND *wnd = WIN_FindWndPtr( list[i] );
529 if( wnd->dwStyle & WS_VISIBLE )
530 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
531 WIN_ReleaseWndPtr( wnd );
533 HeapFree( GetProcessHeap(), 0, list );
539 /* Set/clear internal paint flag */
541 if (flags & RDW_INTERNALPAINT)
543 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
544 add_paint_count( wndPtr->hwndSelf, 1 );
545 wndPtr->flags |= WIN_INTERNAL_PAINT;
547 else if (flags & RDW_NOINTERNALPAINT)
549 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
550 add_paint_count( wndPtr->hwndSelf, -1 );
551 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
555 /***********************************************************************
556 * RDW_Paint [RedrawWindow() helper]
558 * Walks the window tree and paints/erases windows that have
559 * nonzero update regions according to redraw flags. hrgn is a scratch
560 * region passed down during recursion. Must not be 1.
563 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
565 /* NOTE: wndPtr is locked by caller.
567 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
568 * SendMessage() calls. This is a comment inside DefWindowProc() source
571 * This message avoids lots of inter-app message traffic
572 * by switching to the other task and continuing the
576 * LOWORD(lParam) = hrgnClip
577 * HIWORD(lParam) = hwndSkip (not used; always NULL)
581 HWND hWnd = wndPtr->hwndSelf;
582 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
584 /* Erase/update the window itself ... */
586 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
589 * Check if this window should delay it's processing of WM_NCPAINT.
590 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
592 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
593 ex |= RDW_EX_DELAY_NCPAINT;
595 if (flags & RDW_UPDATENOW)
597 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
598 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
600 else if (flags & RDW_ERASENOW)
602 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
605 hrgnRet = WIN_UpdateNCRgn(wndPtr,
607 UNC_REGION | UNC_CHECK |
608 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
612 if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
613 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
615 if( bIcon ) dcx |= DCX_WINDOW;
618 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
619 wndPtr->rectWindow.top - wndPtr->rectClient.top);
621 dcx &= ~DCX_INTERSECTRGN;
622 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
624 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
626 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
627 ReleaseDC( hWnd, hDC );
633 if( !IsWindow(hWnd) ) return hrgn;
635 /* ... and its child windows */
637 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
638 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
642 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
644 for (phwnd = list; *phwnd; phwnd++)
646 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
647 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
648 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
649 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
650 WIN_ReleaseWndPtr(wndPtr);
652 HeapFree( GetProcessHeap(), 0, list );
660 /***********************************************************************
661 * RedrawWindow (USER32.@)
663 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
664 HRGN hrgnUpdate, UINT flags )
671 if (!hwnd) hwnd = GetDesktopWindow();
673 /* check if the window or its parents are visible/not minimized */
675 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
677 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
682 GetRgnBox( hrgnUpdate, &r );
683 TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
684 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
692 TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
693 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
694 r.top, r.right, r.bottom, hrgnUpdate, flags );
699 /* process pending events and messages before painting */
700 if (flags & RDW_UPDATENOW)
701 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
703 /* prepare an update region in window coordinates */
705 if( flags & RDW_FRAME )
706 r = wndPtr->rectWindow;
708 r = wndPtr->rectClient;
710 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
711 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
712 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
714 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
716 /* If the window doesn't have hrgnUpdate we leave hRgn zero
717 * and put a new region straight into wndPtr->hrgnUpdate
718 * so that RDW_UpdateRgns() won't have to do any extra work.
723 if( wndPtr->hrgnUpdate )
724 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
726 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
728 else if( rectUpdate )
730 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
731 OffsetRect( &r2, pt.x, pt.y );
732 if( wndPtr->hrgnUpdate == 0 )
733 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
735 hRgn = CreateRectRgnIndirect( &r2 );
737 else /* entire window or client depending on RDW_FRAME */
739 if( flags & RDW_FRAME )
741 if (wndPtr->hrgnUpdate) hRgn = 1;
742 else wndPtr->hrgnUpdate = 1;
746 GETCLIENTRECTW( wndPtr, r2 );
747 if( wndPtr->hrgnUpdate == 0 )
748 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
750 hRgn = CreateRectRgnIndirect( &r2 );
754 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
756 /* In this we cannot leave with zero hRgn */
759 hRgn = REGION_CropRgn( hRgn, hrgnUpdate, &r, &pt );
760 GetRgnBox( hRgn, &r2 );
761 if( IsRectEmpty( &r2 ) ) goto END;
763 else if( rectUpdate )
765 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
766 OffsetRect( &r2, pt.x, pt.y );
767 hRgn = CreateRectRgnIndirect( &r2 );
769 else /* entire window or client depending on RDW_FRAME */
771 if( flags & RDW_FRAME )
775 GETCLIENTRECTW( wndPtr, r2 );
776 hRgn = CreateRectRgnIndirect( &r2 );
781 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
783 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
785 /* Erase/update windows, from now on hRgn is a scratch region */
787 hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
790 if( hRgn > 1 && (hRgn != hrgnUpdate) )
792 WIN_ReleaseWndPtr(wndPtr);
797 /***********************************************************************
798 * UpdateWindow (USER32.@)
800 void WINAPI UpdateWindow( HWND hwnd )
802 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
806 /***********************************************************************
807 * InvalidateRgn (USER32.@)
809 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
811 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
815 /***********************************************************************
816 * InvalidateRect (USER32.@)
818 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
820 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
824 /***********************************************************************
825 * ValidateRgn (USER32.@)
827 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
829 RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
833 /***********************************************************************
834 * ValidateRect (USER32.@)
836 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
838 RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
842 /***********************************************************************
843 * GetUpdateRect (USER32.@)
845 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
848 WND * wndPtr = WIN_FindWndPtr( hwnd );
849 if (!wndPtr) return FALSE;
853 if (wndPtr->hrgnUpdate > 1)
855 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
856 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
861 GetRgnBox( hrgn, rect );
862 DeleteObject( hrgn );
863 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
865 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
867 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
872 if( wndPtr->hrgnUpdate == 1 )
874 GetClientRect( hwnd, rect );
875 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
878 SetRectEmpty( rect );
880 retvalue = (wndPtr->hrgnUpdate >= 1);
882 WIN_ReleaseWndPtr(wndPtr);
887 /***********************************************************************
888 * GetUpdateRgn (USER32.@)
890 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
893 WND * wndPtr = WIN_FindWndPtr( hwnd );
894 if (!wndPtr) return ERROR;
896 if (wndPtr->hrgnUpdate == 0)
898 SetRectRgn( hrgn, 0, 0, 0, 0 );
903 if (wndPtr->hrgnUpdate == 1)
905 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
906 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
907 retval = SIMPLEREGION;
911 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
912 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
913 wndPtr->rectWindow.top - wndPtr->rectClient.top );
915 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
917 WIN_ReleaseWndPtr(wndPtr);
922 /***********************************************************************
923 * ExcludeUpdateRgn (USER32.@)
925 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
930 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
932 if (wndPtr->hrgnUpdate)
935 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
936 wndPtr->rectWindow.top - wndPtr->rectClient.top,
937 wndPtr->rectWindow.right - wndPtr->rectClient.left,
938 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
939 if( wndPtr->hrgnUpdate > 1 )
941 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
942 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
943 wndPtr->rectWindow.top - wndPtr->rectClient.top );
946 /* do ugly coordinate translations in dce.c */
948 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
949 DeleteObject( hrgn );
950 WIN_ReleaseWndPtr(wndPtr);
953 WIN_ReleaseWndPtr(wndPtr);
954 return GetClipBox( hdc, &rect );
959 /***********************************************************************
962 * The Win16 variant doesn't support special color brushes like
963 * the Win32 one, despite the fact that Win16, as well as Win32,
964 * supports special background brushes for a window class.
966 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
970 /* coordinates are logical so we cannot fast-check 'rect',
971 * it will be done later in the PatBlt().
974 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
975 PatBlt( hdc, rect->left, rect->top,
976 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
977 SelectObject( hdc, prevBrush );
982 /***********************************************************************
983 * FillRect (USER32.@)
985 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
989 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
990 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
993 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
994 PatBlt( hdc, rect->left, rect->top,
995 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
996 SelectObject( hdc, prevBrush );
1001 /***********************************************************************
1002 * InvertRect (USER.82)
1004 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1006 PatBlt( hdc, rect->left, rect->top,
1007 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1011 /***********************************************************************
1012 * InvertRect (USER32.@)
1014 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1016 return PatBlt( hdc, rect->left, rect->top,
1017 rect->right - rect->left, rect->bottom - rect->top,
1022 /***********************************************************************
1023 * FrameRect (USER32.@)
1025 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1030 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1031 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1033 PatBlt( hdc, r.left, r.top, 1,
1034 r.bottom - r.top, PATCOPY );
1035 PatBlt( hdc, r.right - 1, r.top, 1,
1036 r.bottom - r.top, PATCOPY );
1037 PatBlt( hdc, r.left, r.top,
1038 r.right - r.left, 1, PATCOPY );
1039 PatBlt( hdc, r.left, r.bottom - 1,
1040 r.right - r.left, 1, PATCOPY );
1042 SelectObject( hdc, prevBrush );
1047 /***********************************************************************
1048 * FrameRect (USER.83)
1050 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1053 CONV_RECT16TO32( rect16, &rect );
1054 return FrameRect( hdc, &rect, hbrush );
1058 /***********************************************************************
1059 * DrawFocusRect (USER.466)
1061 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1064 CONV_RECT16TO32( rc, &rect32 );
1065 DrawFocusRect( hdc, &rect32 );
1069 /***********************************************************************
1070 * DrawFocusRect (USER32.@)
1072 * FIXME: PatBlt(PATINVERT) with background brush.
1074 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1077 HPEN hOldPen, hNewPen;
1078 INT oldDrawMode, oldBkMode;
1080 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1081 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1082 hOldPen = SelectObject(hdc, hNewPen);
1083 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1084 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1086 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1088 SetBkMode(hdc, oldBkMode);
1089 SetROP2(hdc, oldDrawMode);
1090 SelectObject(hdc, hOldPen);
1091 DeleteObject(hNewPen);
1092 SelectObject(hdc, hOldBrush);
1098 /**********************************************************************
1099 * DrawAnimatedRects (USER32.@)
1101 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1102 const RECT* lprcFrom,
1103 const RECT* lprcTo )
1105 FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1110 /**********************************************************************
1111 * PAINTING_DrawStateJam
1113 * Jams in the requested type in the dc
1115 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1116 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1117 LPRECT rc, UINT dtflags, BOOL unicode )
1122 INT cx = rc->right - rc->left;
1123 INT cy = rc->bottom - rc->top;
1128 case DST_PREFIXTEXT:
1130 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1132 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1135 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1138 memdc = CreateCompatibleDC(hdc);
1139 if(!memdc) return FALSE;
1140 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1146 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1147 SelectObject(memdc, hbmsave);
1154 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1156 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1157 bRet = func(hdc, lp, wp, cx, cy);
1158 /* Restore origin */
1159 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1167 /**********************************************************************
1168 * PAINTING_DrawState()
1170 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1171 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1173 HBITMAP hbm, hbmsave;
1175 HBRUSH hbsave, hbrtmp = 0;
1178 UINT dtflags = DT_NOCLIP;
1180 UINT opcode = flags & 0xf;
1184 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1187 len = strlenW((LPWSTR)lp);
1189 len = strlen((LPSTR)lp);
1192 /* Find out what size the image has if not given by caller */
1196 CURSORICONINFO *ici;
1202 case DST_PREFIXTEXT:
1204 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1206 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1207 if(!retval) return FALSE;
1211 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1212 if(!ici) return FALSE;
1214 s.cy = ici->nHeight;
1215 GlobalUnlock16((HGLOBAL16)lp);
1219 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1225 case DST_COMPLEX: /* cx and cy must be set in this mode */
1238 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1239 dtflags |= DT_RIGHT;
1240 if(opcode == DST_TEXT)
1241 dtflags |= DT_NOPREFIX;
1243 /* For DSS_NORMAL we just jam in the image and return */
1244 if((flags & 0x7ff0) == DSS_NORMAL)
1246 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1249 /* For all other states we need to convert the image to B/W in a local bitmap */
1250 /* before it is displayed */
1251 fg = SetTextColor(hdc, RGB(0, 0, 0));
1252 bg = SetBkColor(hdc, RGB(255, 255, 255));
1253 hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1254 memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1255 retval = FALSE; /* assume failure */
1257 /* From here on we must use "goto cleanup" when something goes wrong */
1258 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1259 if(!hbm) goto cleanup;
1260 memdc = CreateCompatibleDC(hdc);
1261 if(!memdc) goto cleanup;
1262 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1263 if(!hbmsave) goto cleanup;
1264 rc.left = rc.top = 0;
1267 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1268 SetBkColor(memdc, RGB(255, 255, 255));
1269 SetTextColor(memdc, RGB(0, 0, 0));
1270 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1272 /* DST_COMPLEX may draw text as well,
1273 * so we must be sure that correct font is selected
1275 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1276 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1277 if(hfsave) SelectObject(memdc, hfsave);
1278 if(!tmp) goto cleanup;
1280 /* This state cause the image to be dithered */
1281 if(flags & DSS_UNION)
1283 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1284 if(!hbsave) goto cleanup;
1285 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1286 SelectObject(memdc, hbsave);
1287 if(!tmp) goto cleanup;
1290 if (flags & DSS_DISABLED)
1291 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1292 else if (flags & DSS_DEFAULT)
1293 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1295 /* Draw light or dark shadow */
1296 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1298 if(!hbrtmp) goto cleanup;
1299 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1300 if(!hbsave) goto cleanup;
1301 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1302 SelectObject(hdc, hbsave);
1303 DeleteObject(hbrtmp);
1307 if (flags & DSS_DISABLED)
1309 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1310 if(!hbrtmp) goto cleanup;
1314 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1317 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1319 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1321 retval = TRUE; /* We succeeded */
1324 SetTextColor(hdc, fg);
1325 SetBkColor(hdc, bg);
1327 if(hbsave) SelectObject(hdc, hbsave);
1328 if(hbmsave) SelectObject(memdc, hbmsave);
1329 if(hbrtmp) DeleteObject(hbrtmp);
1330 if(hbm) DeleteObject(hbm);
1331 if(memdc) DeleteDC(memdc);
1336 /**********************************************************************
1337 * DrawStateA (USER32.@)
1339 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1340 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1341 INT x, INT y, INT cx, INT cy, UINT flags)
1343 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1346 /**********************************************************************
1347 * DrawStateW (USER32.@)
1349 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1350 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1351 INT x, INT y, INT cx, INT cy, UINT flags)
1353 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1357 /**********************************************************************
1358 * DrawState (USER.449)
1360 BOOL16 WINAPI DrawState16( HDC16 hdc, HBRUSH16 hbr, DRAWSTATEPROC16 func, LPARAM ldata,
1361 WPARAM16 wdata, INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags )
1363 struct draw_state_info info;
1364 UINT opcode = flags & 0xf;
1366 if (opcode == DST_TEXT || opcode == DST_PREFIXTEXT)
1368 /* make sure DrawStateA doesn't try to use ldata as a pointer */
1369 if (!wdata) wdata = strlen( MapSL(ldata) );
1373 if (!GetTextExtentPoint32A( hdc, MapSL(ldata), wdata, &s )) return FALSE;
1380 return DrawStateA( hdc, hbr, draw_state_callback, (LPARAM)&info, wdata, x, y, cx, cy, flags );
1384 /***********************************************************************
1385 * SelectPalette (USER.282)
1387 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1388 BOOL16 bForceBackground )
1390 WORD wBkgPalette = 1;
1392 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1394 HWND hwnd = WindowFromDC( hDC );
1397 HWND hForeground = GetForegroundWindow();
1398 /* set primary palette if it's related to current active */
1399 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1402 return GDISelectPalette16( hDC, hPal, wBkgPalette);
1406 /***********************************************************************
1407 * RealizePalette (USER.283)
1409 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1411 UINT16 realized = GDIRealizePalette16( hDC );
1413 /* do not send anything if no colors were changed */
1414 if (realized && IsDCCurrentPalette16( hDC ))
1416 /* send palette change notification */
1417 HWND hWnd = WindowFromDC( hDC );
1418 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0L);
1424 /***********************************************************************
1425 * UserRealizePalette (USER32.@)
1427 UINT WINAPI UserRealizePalette( HDC hDC )
1429 return RealizePalette16( hDC );