Fixed DrawState16 callback support.
[wine/wine-kai.git] / windows / painting.c
blob6ee8b9cc721a7534bf62c95abd16f6f6f1a3dc12
1 /*
2 * Window painting functions
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1999 Alex Korobka
6 */
8 #include <string.h>
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "wine/winuser16.h"
12 #include "wine/unicode.h"
13 #include "wine/server.h"
14 #include "region.h"
15 #include "user.h"
16 #include "win.h"
17 #include "queue.h"
18 #include "dce.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
42 /* Last COLOR id */
43 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
45 /* Last CTLCOLOR id */
46 #define CTLCOLOR_MAX CTLCOLOR_STATIC
49 /***********************************************************************
50 * add_paint_count
52 * Add an increment (normally 1 or -1) to the current paint count of a window.
54 static void add_paint_count( HWND hwnd, int incr )
56 SERVER_START_REQ( inc_queue_paint_count )
58 req->id = (void *)GetWindowThreadProcessId( hwnd, NULL );
59 req->incr = incr;
60 SERVER_CALL();
62 SERVER_END_REQ;
66 /***********************************************************************
67 * WIN_HaveToDelayNCPAINT
69 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
70 * is generated as soon as a region intersecting the non-client area
71 * of a window is invalidated.
73 * This technique will work fine for all windows whose parents
74 * have the WS_CLIPCHILDREN style. When the parents have that style,
75 * they are not going to override the contents of their children.
76 * However, when the parent doesn't have that style, Windows relies
77 * on a "painter's algorithm" to display the contents of the windows.
78 * That is, windows are painted from back to front. This includes the
79 * non-client area.
81 * This method looks at the current state of a window to determine
82 * if the sending of the WM_NCPAINT message should be delayed until
83 * the BeginPaint call.
85 * PARAMS:
86 * wndPtr - A Locked window pointer to the window we're
87 * examining.
88 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
89 * function. This is a shortcut for the cases when
90 * we already know when to avoid scanning all the
91 * parents of a window. If you already know that this
92 * window's NCPAINT should be delayed, set the
93 * UNC_DELAY_NCPAINT flag for this parameter.
95 * This shortcut behavior is implemented in the
96 * RDW_Paint() method.
99 static BOOL WIN_HaveToDelayNCPAINT(
100 WND* wndPtr,
101 UINT uncFlags)
103 WND* parentWnd = NULL;
106 * Test the shortcut first. (duh)
108 if (uncFlags & UNC_DELAY_NCPAINT)
109 return TRUE;
112 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
113 * method only. This is another shortcut to avoid going
114 * up the parent's chain of the window to finally
115 * figure-out that none of them has an invalid region.
117 if (uncFlags & UNC_IN_BEGINPAINT)
118 return FALSE;
121 * Scan all the parents of this window to find a window
122 * that doesn't have the WS_CLIPCHILDREN style and that
123 * has an invalid region.
125 parentWnd = WIN_LockWndPtr(wndPtr->parent);
127 while (parentWnd!=NULL)
129 if ( ((parentWnd->dwStyle & WS_CLIPCHILDREN) == 0) &&
130 (parentWnd->hrgnUpdate != 0) )
132 WIN_ReleaseWndPtr(parentWnd);
133 return TRUE;
136 WIN_UpdateWndPtr(&parentWnd, parentWnd->parent);
139 WIN_ReleaseWndPtr(parentWnd);
141 return FALSE;
144 /***********************************************************************
145 * WIN_UpdateNCRgn
147 * Things to do:
148 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
149 * Crop hrgnUpdate to a client rect, especially if it 1.
150 * If UNC_REGION is set return update region for the client rect.
152 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
153 * The trick is that when the returned region handle may be different from hRgn.
154 * In this case the old hRgn must be considered gone. BUT, if the returned value
155 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
156 * a DC without extra clipping region.
158 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
160 RECT r;
161 HRGN hClip = 0;
162 HRGN hrgnRet = 0;
164 TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n",
165 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
167 /* desktop window doesn't have a nonclient area */
168 if(wnd == WIN_GetDesktop())
170 wnd->flags &= ~WIN_NEEDS_NCPAINT;
171 if( wnd->hrgnUpdate > 1 )
172 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
173 else
175 hrgnRet = wnd->hrgnUpdate;
177 WIN_ReleaseDesktop();
178 return hrgnRet;
180 WIN_ReleaseDesktop();
182 if ((wnd->hwndSelf == GetForegroundWindow()) &&
183 !(wnd->flags & WIN_NCACTIVATED) )
185 wnd->flags |= WIN_NCACTIVATED;
186 uncFlags |= UNC_ENTIRE;
190 * If the window's non-client area needs to be painted,
192 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
193 !WIN_HaveToDelayNCPAINT(wnd, uncFlags) )
195 RECT r2, r3;
197 wnd->flags &= ~WIN_NEEDS_NCPAINT;
198 GETCLIENTRECTW( wnd, r );
200 TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n",
201 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
202 if( wnd->hrgnUpdate > 1 )
204 /* Check if update rgn overlaps with nonclient area */
206 GetRgnBox( wnd->hrgnUpdate, &r2 );
207 UnionRect( &r3, &r2, &r );
208 if( r3.left != r.left || r3.top != r.top ||
209 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
211 /* crop hrgnUpdate, save old one in hClip - the only
212 * case that places a valid region handle in hClip */
214 hClip = wnd->hrgnUpdate;
215 wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
216 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
219 if( uncFlags & UNC_CHECK )
221 GetRgnBox( wnd->hrgnUpdate, &r3 );
222 if( IsRectEmpty( &r3 ) )
224 /* delete the update region since all invalid
225 * parts were in the nonclient area */
227 DeleteObject( wnd->hrgnUpdate );
228 wnd->hrgnUpdate = 0;
229 if(!(wnd->flags & WIN_INTERNAL_PAINT))
230 add_paint_count( wnd->hwndSelf, -1 );
232 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
236 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
238 else
239 if( wnd->hrgnUpdate == 1 )/* entire window */
241 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
242 if( uncFlags & UNC_REGION ) hrgnRet = 1;
243 uncFlags |= UNC_ENTIRE;
246 else /* no WM_NCPAINT unless forced */
248 if( wnd->hrgnUpdate > 1 )
250 copyrgn:
251 if( uncFlags & UNC_REGION )
252 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
254 else
255 if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
257 GETCLIENTRECTW( wnd, r );
258 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
259 if( uncFlags & UNC_REGION ) hrgnRet = 1;
263 if(!hClip && (uncFlags & UNC_ENTIRE) )
265 /* still don't do anything if there is no nonclient area */
266 hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
269 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
271 if ( hClip == hrgnRet && hrgnRet > 1 ) {
272 hClip = CreateRectRgn( 0, 0, 0, 0 );
273 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
276 SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
277 if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
278 DeleteObject( hClip );
280 * Since all Window locks are suspended while processing the WM_NCPAINT
281 * we want to make sure the window still exists before continuing.
283 if (!IsWindow(wnd->hwndSelf))
285 DeleteObject(hrgnRet);
286 hrgnRet=0;
290 TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
292 return hrgnRet;
296 /***********************************************************************
297 * BeginPaint (USER.39)
299 HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps )
301 PAINTSTRUCT ps;
303 BeginPaint( hwnd, &ps );
304 lps->hdc = ps.hdc;
305 lps->fErase = ps.fErase;
306 lps->rcPaint.top = ps.rcPaint.top;
307 lps->rcPaint.left = ps.rcPaint.left;
308 lps->rcPaint.right = ps.rcPaint.right;
309 lps->rcPaint.bottom = ps.rcPaint.bottom;
310 lps->fRestore = ps.fRestore;
311 lps->fIncUpdate = ps.fIncUpdate;
312 return lps->hdc;
316 /***********************************************************************
317 * BeginPaint (USER32.@)
319 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
321 BOOL bIcon;
322 HRGN hrgnUpdate;
323 RECT clipRect, clientRect;
324 WND *wndPtr = WIN_FindWndPtr( hwnd );
325 if (!wndPtr) return 0;
327 bIcon = (wndPtr->dwStyle & WS_MINIMIZE && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
329 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
331 /* send WM_NCPAINT and make sure hrgnUpdate is a valid rgn handle */
332 WIN_UpdateNCRgn( wndPtr, 0, UNC_UPDATE | UNC_IN_BEGINPAINT);
335 * Make sure the window is still a window. All window locks are suspended
336 * when the WM_NCPAINT is sent.
338 if (!IsWindow(wndPtr->hwndSelf))
340 WIN_ReleaseWndPtr(wndPtr);
341 return 0;
344 if( ((hrgnUpdate = wndPtr->hrgnUpdate) != 0) || (wndPtr->flags & WIN_INTERNAL_PAINT))
345 add_paint_count( hwnd, -1 );
347 wndPtr->hrgnUpdate = 0;
348 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
350 HideCaret( hwnd );
352 TRACE("hrgnUpdate = %04x, \n", hrgnUpdate);
354 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_PARENTDC)
356 /* Don't clip the output to the update region for CS_PARENTDC window */
357 if( hrgnUpdate )
358 DeleteObject(hrgnUpdate);
359 lps->hdc = GetDCEx( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
360 (bIcon ? DCX_WINDOW : 0) );
362 else
364 if( hrgnUpdate ) /* convert to client coordinates */
365 OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
366 wndPtr->rectWindow.top - wndPtr->rectClient.top );
367 lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
368 DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
369 /* ReleaseDC() in EndPaint() will delete the region */
372 TRACE("hdc = %04x\n", lps->hdc);
374 if (!lps->hdc)
376 WARN("GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
377 WIN_ReleaseWndPtr(wndPtr);
378 return 0;
381 /* It is possible that the clip box is bigger than the window itself,
382 if CS_PARENTDC flag is set. Windows never return a paint rect bigger
383 than the window itself, so we need to intersect the cliprect with
384 the window */
386 GetClientRect( hwnd, &clientRect );
388 GetClipBox( lps->hdc, &clipRect );
389 LPtoDP(lps->hdc, (LPPOINT)&clipRect, 2); /* GetClipBox returns LP */
391 IntersectRect(&lps->rcPaint, &clientRect, &clipRect);
392 DPtoLP(lps->hdc, (LPPOINT)&lps->rcPaint, 2); /* we must return LP */
394 TRACE("box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
395 lps->rcPaint.right, lps->rcPaint.bottom );
397 if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
399 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
400 lps->fErase = !SendMessageA(hwnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
401 (WPARAM16)lps->hdc, 0 );
403 else lps->fErase = TRUE;
405 WIN_ReleaseWndPtr(wndPtr);
406 return lps->hdc;
410 /***********************************************************************
411 * EndPaint (USER.40)
413 BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
415 ReleaseDC16( hwnd, lps->hdc );
416 ShowCaret( hwnd );
417 return TRUE;
421 /***********************************************************************
422 * EndPaint (USER32.@)
424 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
426 ReleaseDC( hwnd, lps->hdc );
427 ShowCaret( hwnd );
428 return TRUE;
432 /***********************************************************************
433 * FillWindow (USER.324)
435 void WINAPI FillWindow16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc, HBRUSH16 hbrush )
437 RECT rect;
438 RECT16 rc16;
439 GetClientRect( hwnd, &rect );
440 DPtoLP( hdc, (LPPOINT)&rect, 2 );
441 CONV_RECT32TO16( &rect, &rc16 );
442 PaintRect16( hwndParent, hwnd, hdc, hbrush, &rc16 );
446 /***********************************************************************
447 * PAINT_GetControlBrush
449 static HBRUSH16 PAINT_GetControlBrush( HWND hParent, HWND hWnd, HDC16 hDC, UINT16 ctlType )
451 HBRUSH16 bkgBrush = (HBRUSH16)SendMessageA( hParent, WM_CTLCOLORMSGBOX + ctlType,
452 (WPARAM)hDC, (LPARAM)hWnd );
453 if( !IsGDIObject16(bkgBrush) )
454 bkgBrush = DEFWND_ControlColor( hDC, ctlType );
455 return bkgBrush;
459 /***********************************************************************
460 * PaintRect (USER.325)
462 void WINAPI PaintRect16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc,
463 HBRUSH16 hbrush, const RECT16 *rect)
465 if( hbrush <= CTLCOLOR_MAX )
467 if( hwndParent )
468 hbrush = PAINT_GetControlBrush( hwndParent, hwnd, hdc, (UINT16)hbrush );
469 else
470 return;
472 if( hbrush )
473 FillRect16( hdc, rect, hbrush );
477 /***********************************************************************
478 * GetControlBrush (USER.326)
480 HBRUSH16 WINAPI GetControlBrush16( HWND16 hwnd, HDC16 hdc, UINT16 ctlType )
482 if (ctlType <= CTLCOLOR_MAX)
484 HWND16 parent = GetParent16( hwnd );
485 if (!parent) parent = hwnd;
486 return PAINT_GetControlBrush( parent, hwnd, hdc, ctlType );
488 return 0;
492 /***********************************************************************
493 * RDW_ValidateParent [RDW_UpdateRgns() helper]
495 * Validate the portions of parents that are covered by a validated child
496 * wndPtr = child
498 static void RDW_ValidateParent(WND *wndChild)
500 WND *wndParent = WIN_LockWndPtr(wndChild->parent);
501 WND *wndDesktop = WIN_GetDesktop();
502 HRGN hrg;
504 if (wndChild->hrgnUpdate == 1 ) {
505 RECT r;
506 r.left = 0;
507 r.top = 0;
508 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
509 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
510 hrg = CreateRectRgnIndirect( &r );
511 } else
512 hrg = wndChild->hrgnUpdate;
514 while ((wndParent) && (wndParent != wndDesktop) ) {
515 if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
517 if (wndParent->hrgnUpdate != 0)
519 POINT ptOffset;
520 RECT rect, rectParent;
521 if( wndParent->hrgnUpdate == 1 )
523 RECT r;
525 r.left = 0;
526 r.top = 0;
527 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
528 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
530 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
532 /* we must offset the child region by the offset of the child rect in the parent */
533 GetWindowRect(wndParent->hwndSelf, &rectParent);
534 GetWindowRect(wndChild->hwndSelf, &rect);
535 ptOffset.x = rect.left - rectParent.left;
536 ptOffset.y = rect.top - rectParent.top;
537 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
538 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
539 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
542 WIN_UpdateWndPtr(&wndParent, wndParent->parent);
544 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
545 WIN_ReleaseWndPtr(wndParent);
546 WIN_ReleaseDesktop();
549 /***********************************************************************
550 * RDW_UpdateRgns [RedrawWindow() helper]
552 * Walks the window tree and adds/removes parts of the hRgn to/from update
553 * regions of windows that overlap it. Also, manages internal paint flags.
555 * NOTE: Walks the window tree so the caller must lock it.
556 * MUST preserve hRgn (can modify but then has to restore).
558 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
561 * Called only when one of the following is set:
562 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
565 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
566 BOOL bChildren = ( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE)
567 && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
568 RECT r;
570 r.left = 0;
571 r.top = 0;
572 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
573 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
575 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
577 if( flags & RDW_INVALIDATE )
579 if( hRgn > 1 )
581 switch( wndPtr->hrgnUpdate )
583 default:
584 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
585 /* fall through */
586 case 0:
587 wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate,
588 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
589 &r, NULL );
590 if( !bHadOne )
592 GetRgnBox( wndPtr->hrgnUpdate, &r );
593 if( IsRectEmpty( &r ) )
595 DeleteObject( wndPtr->hrgnUpdate );
596 wndPtr->hrgnUpdate = 0;
597 goto end;
600 break;
601 case 1: /* already an entire window */
602 break;
605 else if( hRgn == 1 )
607 if( wndPtr->hrgnUpdate > 1 )
608 DeleteObject( wndPtr->hrgnUpdate );
609 wndPtr->hrgnUpdate = 1;
611 else
612 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
614 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
615 add_paint_count( wndPtr->hwndSelf, 1 );
617 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
618 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
619 flags |= RDW_FRAME;
621 else if( flags & RDW_VALIDATE )
623 if( wndPtr->hrgnUpdate )
625 if( hRgn > 1 )
627 if( wndPtr->hrgnUpdate == 1 )
628 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
630 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
631 == NULLREGION )
633 DeleteObject( wndPtr->hrgnUpdate );
634 wndPtr->hrgnUpdate = 0;
637 else /* validate everything */
639 if( wndPtr->hrgnUpdate > 1 ) DeleteObject( wndPtr->hrgnUpdate );
640 wndPtr->hrgnUpdate = 0;
643 if( !wndPtr->hrgnUpdate )
645 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
646 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
647 add_paint_count( wndPtr->hwndSelf, -1 );
651 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
652 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
656 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
657 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
659 /* in/validate child windows that intersect with the region if it
660 * is a valid handle. */
662 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
664 if( hRgn > 1 && bChildren )
666 WND* wnd = wndPtr->child;
667 POINT ptTotal, prevOrigin = {0,0};
668 POINT ptClient;
670 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
671 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
673 for( ptTotal.x = ptTotal.y = 0; wnd; wnd = wnd->next )
675 if( wnd->dwStyle & WS_VISIBLE )
677 POINT ptOffset;
679 r.left = wnd->rectWindow.left + ptClient.x;
680 r.right = wnd->rectWindow.right + ptClient.x;
681 r.top = wnd->rectWindow.top + ptClient.y;
682 r.bottom = wnd->rectWindow.bottom + ptClient.y;
684 ptOffset.x = r.left - prevOrigin.x;
685 ptOffset.y = r.top - prevOrigin.y;
686 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
688 if( RectInRegion( hRgn, &r ) )
690 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
691 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
692 prevOrigin.x = r.left + ptTotal.x;
693 prevOrigin.y = r.top + ptTotal.y;
694 ptTotal.x += ptOffset.x;
695 ptTotal.y += ptOffset.y;
699 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
700 bChildren = 0;
704 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
706 if( bChildren )
708 WND* wnd;
709 for( wnd = wndPtr->child; wnd; wnd = wnd->next )
710 if( wnd->dwStyle & WS_VISIBLE )
711 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
714 end:
716 /* Set/clear internal paint flag */
718 if (flags & RDW_INTERNALPAINT)
720 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
721 add_paint_count( wndPtr->hwndSelf, 1 );
722 wndPtr->flags |= WIN_INTERNAL_PAINT;
724 else if (flags & RDW_NOINTERNALPAINT)
726 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
727 add_paint_count( wndPtr->hwndSelf, -1 );
728 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
732 /***********************************************************************
733 * RDW_Paint [RedrawWindow() helper]
735 * Walks the window tree and paints/erases windows that have
736 * nonzero update regions according to redraw flags. hrgn is a scratch
737 * region passed down during recursion. Must not be 1.
740 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
742 /* NOTE: wndPtr is locked by caller.
744 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
745 * SendMessage() calls. This is a comment inside DefWindowProc() source
746 * from 16-bit SDK:
748 * This message avoids lots of inter-app message traffic
749 * by switching to the other task and continuing the
750 * recursion there.
752 * wParam = flags
753 * LOWORD(lParam) = hrgnClip
754 * HIWORD(lParam) = hwndSkip (not used; always NULL)
757 HDC hDC;
758 HWND hWnd = wndPtr->hwndSelf;
759 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
761 /* Erase/update the window itself ... */
763 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
766 * Check if this window should delay it's processing of WM_NCPAINT.
767 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
769 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr, 0) )
770 ex |= RDW_EX_DELAY_NCPAINT;
772 if (flags & RDW_UPDATENOW)
774 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
775 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
777 else if (flags & RDW_ERASENOW)
779 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
780 HRGN hrgnRet;
782 hrgnRet = WIN_UpdateNCRgn(wndPtr,
783 hrgn,
784 UNC_REGION | UNC_CHECK |
785 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
787 if( hrgnRet )
789 if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
790 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
792 if( bIcon ) dcx |= DCX_WINDOW;
793 else
794 if( hrgnRet )
795 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
796 wndPtr->rectWindow.top - wndPtr->rectClient.top);
797 else
798 dcx &= ~DCX_INTERSECTRGN;
799 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
801 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
802 (WPARAM)hDC, 0 ))
803 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
804 ReleaseDC( hWnd, hDC );
810 if( !IsWindow(hWnd) ) return hrgn;
812 /* ... and its child windows */
814 if( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE)
815 && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
817 WND** list, **ppWnd;
819 if( (list = WIN_BuildWinArray( wndPtr, 0, NULL )) )
821 wndPtr = NULL;
822 for (ppWnd = list; *ppWnd; ppWnd++)
824 WIN_UpdateWndPtr(&wndPtr,*ppWnd);
825 if (!IsWindow(wndPtr->hwndSelf)) continue;
826 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
827 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
828 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
830 WIN_ReleaseWndPtr(wndPtr);
831 WIN_ReleaseWinArray(list);
835 return hrgn;
839 /***********************************************************************
840 * RedrawWindow (USER32.@)
842 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
843 HRGN hrgnUpdate, UINT flags )
845 HRGN hRgn = 0;
846 RECT r, r2;
847 POINT pt;
848 WND* wndPtr;
850 if (!hwnd) hwnd = GetDesktopWindow();
851 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
853 /* check if the window or its parents are visible/not minimized */
855 if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
857 WIN_ReleaseWndPtr(wndPtr);
858 return TRUE;
861 if (TRACE_ON(win))
863 if( hrgnUpdate )
865 GetRgnBox( hrgnUpdate, &r );
866 TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
867 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
869 else
871 if( rectUpdate )
872 r = *rectUpdate;
873 else
874 SetRectEmpty( &r );
875 TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
876 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
877 r.top, r.right, r.bottom, hrgnUpdate, flags );
881 /* prepare an update region in window coordinates */
883 if( flags & RDW_FRAME )
884 r = wndPtr->rectWindow;
885 else
886 r = wndPtr->rectClient;
888 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
889 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
890 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
892 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
894 /* If the window doesn't have hrgnUpdate we leave hRgn zero
895 * and put a new region straight into wndPtr->hrgnUpdate
896 * so that RDW_UpdateRgns() won't have to do any extra work.
899 if( hrgnUpdate )
901 if( wndPtr->hrgnUpdate )
902 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
903 else
904 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
906 else if( rectUpdate )
908 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
909 OffsetRect( &r2, pt.x, pt.y );
911 rect2i:
912 if( wndPtr->hrgnUpdate == 0 )
913 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
914 else
915 hRgn = CreateRectRgnIndirect( &r2 );
917 else /* entire window or client depending on RDW_FRAME */
919 if( flags & RDW_FRAME )
921 if( wndPtr->hrgnUpdate )
922 DeleteObject( wndPtr->hrgnUpdate );
923 wndPtr->hrgnUpdate = 1;
925 else
927 GETCLIENTRECTW( wndPtr, r2 );
928 goto rect2i;
932 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
934 /* In this we cannot leave with zero hRgn */
935 if( hrgnUpdate )
937 hRgn = REGION_CropRgn( hRgn, hrgnUpdate, &r, &pt );
938 GetRgnBox( hRgn, &r2 );
939 if( IsRectEmpty( &r2 ) ) goto END;
941 else if( rectUpdate )
943 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
944 OffsetRect( &r2, pt.x, pt.y );
945 hRgn = CreateRectRgnIndirect( &r2 );
947 else /* entire window or client depending on RDW_FRAME */
949 if( flags & RDW_FRAME )
950 hRgn = 1;
951 else
953 GETCLIENTRECTW( wndPtr, r2 );
954 hRgn = CreateRectRgnIndirect( &r2 );
959 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
961 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
963 /* Erase/update windows, from now on hRgn is a scratch region */
965 hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
967 END:
968 if( hRgn > 1 && (hRgn != hrgnUpdate) )
969 DeleteObject(hRgn );
970 WIN_ReleaseWndPtr(wndPtr);
971 return TRUE;
975 /***********************************************************************
976 * RedrawWindow (USER.290)
978 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
979 HRGN16 hrgnUpdate, UINT16 flags )
981 if (rectUpdate)
983 RECT r;
984 CONV_RECT16TO32( rectUpdate, &r );
985 return (BOOL16)RedrawWindow( (HWND)hwnd, &r, hrgnUpdate, flags );
987 return RedrawWindow( hwnd, NULL, hrgnUpdate, flags );
991 /***********************************************************************
992 * UpdateWindow (USER.124)
994 void WINAPI UpdateWindow16( HWND16 hwnd )
996 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
999 /***********************************************************************
1000 * UpdateWindow (USER32.@)
1002 void WINAPI UpdateWindow( HWND hwnd )
1004 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1007 /***********************************************************************
1008 * InvalidateRgn (USER.126)
1010 void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1012 RedrawWindow((HWND)hwnd, NULL, (HRGN)hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1016 /***********************************************************************
1017 * InvalidateRgn (USER32.@)
1019 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1021 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1025 /***********************************************************************
1026 * InvalidateRect (USER.125)
1028 void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
1030 RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1034 /***********************************************************************
1035 * InvalidateRect (USER32.@)
1037 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1039 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1043 /***********************************************************************
1044 * ValidateRgn (USER.128)
1046 void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
1048 RedrawWindow( (HWND)hwnd, NULL, (HRGN)hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
1052 /***********************************************************************
1053 * ValidateRgn (USER32.@)
1055 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1057 RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
1061 /***********************************************************************
1062 * ValidateRect (USER.127)
1064 void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
1066 RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1070 /***********************************************************************
1071 * ValidateRect (USER32.@)
1073 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1075 RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1079 /***********************************************************************
1080 * GetUpdateRect (USER.190)
1082 BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
1084 RECT r;
1085 BOOL16 ret;
1087 if (!rect) return GetUpdateRect( hwnd, NULL, erase );
1088 ret = GetUpdateRect( hwnd, &r, erase );
1089 CONV_RECT32TO16( &r, rect );
1090 return ret;
1094 /***********************************************************************
1095 * GetUpdateRect (USER32.@)
1097 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1099 BOOL retvalue;
1100 WND * wndPtr = WIN_FindWndPtr( hwnd );
1101 if (!wndPtr) return FALSE;
1103 if (rect)
1105 if (wndPtr->hrgnUpdate > 1)
1107 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
1108 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
1110 retvalue = FALSE;
1111 goto END;
1113 GetRgnBox( hrgn, rect );
1114 DeleteObject( hrgn );
1115 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
1117 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
1119 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
1123 else
1124 if( wndPtr->hrgnUpdate == 1 )
1126 GetClientRect( hwnd, rect );
1127 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
1129 else
1130 SetRectEmpty( rect );
1132 retvalue = (wndPtr->hrgnUpdate >= 1);
1133 END:
1134 WIN_ReleaseWndPtr(wndPtr);
1135 return retvalue;
1139 /***********************************************************************
1140 * GetUpdateRgn (USER.237)
1142 INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1144 return GetUpdateRgn( hwnd, hrgn, erase );
1148 /***********************************************************************
1149 * GetUpdateRgn (USER32.@)
1151 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1153 INT retval;
1154 WND * wndPtr = WIN_FindWndPtr( hwnd );
1155 if (!wndPtr) return ERROR;
1157 if (wndPtr->hrgnUpdate == 0)
1159 SetRectRgn( hrgn, 0, 0, 0, 0 );
1160 retval = NULLREGION;
1161 goto END;
1163 else
1164 if (wndPtr->hrgnUpdate == 1)
1166 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1167 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1168 retval = SIMPLEREGION;
1170 else
1172 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1173 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1174 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1176 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1177 END:
1178 WIN_ReleaseWndPtr(wndPtr);
1179 return retval;
1183 /***********************************************************************
1184 * ExcludeUpdateRgn (USER.238)
1186 INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
1188 return ExcludeUpdateRgn( hdc, hwnd );
1192 /***********************************************************************
1193 * ExcludeUpdateRgn (USER32.@)
1195 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1197 RECT rect;
1198 WND * wndPtr;
1200 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1202 if (wndPtr->hrgnUpdate)
1204 INT ret;
1205 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1206 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1207 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1208 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1209 if( wndPtr->hrgnUpdate > 1 )
1211 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1212 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1213 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1216 /* do ugly coordinate translations in dce.c */
1218 ret = DCE_ExcludeRgn( hdc, wndPtr, hrgn );
1219 DeleteObject( hrgn );
1220 WIN_ReleaseWndPtr(wndPtr);
1221 return ret;
1223 WIN_ReleaseWndPtr(wndPtr);
1224 return GetClipBox( hdc, &rect );
1229 /***********************************************************************
1230 * FillRect (USER.81)
1231 * NOTE
1232 * The Win16 variant doesn't support special color brushes like
1233 * the Win32 one, despite the fact that Win16, as well as Win32,
1234 * supports special background brushes for a window class.
1236 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1238 HBRUSH prevBrush;
1240 /* coordinates are logical so we cannot fast-check 'rect',
1241 * it will be done later in the PatBlt().
1244 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1245 PatBlt( hdc, rect->left, rect->top,
1246 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1247 SelectObject( hdc, prevBrush );
1248 return 1;
1252 /***********************************************************************
1253 * FillRect (USER32.@)
1255 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1257 HBRUSH prevBrush;
1259 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1260 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1263 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1264 PatBlt( hdc, rect->left, rect->top,
1265 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1266 SelectObject( hdc, prevBrush );
1267 return 1;
1271 /***********************************************************************
1272 * InvertRect (USER.82)
1274 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1276 PatBlt( hdc, rect->left, rect->top,
1277 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1281 /***********************************************************************
1282 * InvertRect (USER32.@)
1284 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1286 return PatBlt( hdc, rect->left, rect->top,
1287 rect->right - rect->left, rect->bottom - rect->top,
1288 DSTINVERT );
1292 /***********************************************************************
1293 * FrameRect (USER32.@)
1295 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1297 HBRUSH prevBrush;
1298 RECT r = *rect;
1300 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1301 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1303 PatBlt( hdc, r.left, r.top, 1,
1304 r.bottom - r.top, PATCOPY );
1305 PatBlt( hdc, r.right - 1, r.top, 1,
1306 r.bottom - r.top, PATCOPY );
1307 PatBlt( hdc, r.left, r.top,
1308 r.right - r.left, 1, PATCOPY );
1309 PatBlt( hdc, r.left, r.bottom - 1,
1310 r.right - r.left, 1, PATCOPY );
1312 SelectObject( hdc, prevBrush );
1313 return TRUE;
1317 /***********************************************************************
1318 * FrameRect (USER.83)
1320 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1322 RECT rect;
1323 CONV_RECT16TO32( rect16, &rect );
1324 return FrameRect( hdc, &rect, hbrush );
1328 /***********************************************************************
1329 * DrawFocusRect (USER.466)
1331 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1333 RECT rect32;
1334 CONV_RECT16TO32( rc, &rect32 );
1335 DrawFocusRect( hdc, &rect32 );
1339 /***********************************************************************
1340 * DrawFocusRect (USER32.@)
1342 * FIXME: PatBlt(PATINVERT) with background brush.
1344 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1346 HBRUSH hOldBrush;
1347 HPEN hOldPen, hNewPen;
1348 INT oldDrawMode, oldBkMode;
1350 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1351 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1352 hOldPen = SelectObject(hdc, hNewPen);
1353 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1354 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1356 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1358 SetBkMode(hdc, oldBkMode);
1359 SetROP2(hdc, oldDrawMode);
1360 SelectObject(hdc, hOldPen);
1361 DeleteObject(hNewPen);
1362 SelectObject(hdc, hOldBrush);
1364 return TRUE;
1367 /**********************************************************************
1368 * DrawAnimatedRects (USER.448)
1370 BOOL16 WINAPI DrawAnimatedRects16( HWND16 hwnd, INT16 idAni,
1371 const RECT16* lprcFrom,
1372 const RECT16* lprcTo )
1374 RECT rcFrom32, rcTo32;
1376 rcFrom32.left = (INT)lprcFrom->left;
1377 rcFrom32.top = (INT)lprcFrom->top;
1378 rcFrom32.right = (INT)lprcFrom->right;
1379 rcFrom32.bottom = (INT)lprcFrom->bottom;
1381 rcTo32.left = (INT)lprcTo->left;
1382 rcTo32.top = (INT)lprcTo->top;
1383 rcTo32.right = (INT)lprcTo->right;
1384 rcTo32.bottom = (INT)lprcTo->bottom;
1386 return DrawAnimatedRects((HWND)hwnd, (INT)idAni, &rcFrom32, &rcTo32);
1390 /**********************************************************************
1391 * DrawAnimatedRects (USER32.@)
1393 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1394 const RECT* lprcFrom,
1395 const RECT* lprcTo )
1397 FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1398 return TRUE;
1402 /**********************************************************************
1403 * PAINTING_DrawStateJam
1405 * Jams in the requested type in the dc
1407 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1408 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1409 LPRECT rc, UINT dtflags, BOOL unicode )
1411 HDC memdc;
1412 HBITMAP hbmsave;
1413 BOOL retval;
1414 INT cx = rc->right - rc->left;
1415 INT cy = rc->bottom - rc->top;
1417 switch(opcode)
1419 case DST_TEXT:
1420 case DST_PREFIXTEXT:
1421 if(unicode)
1422 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1423 else
1424 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1426 case DST_ICON:
1427 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1429 case DST_BITMAP:
1430 memdc = CreateCompatibleDC(hdc);
1431 if(!memdc) return FALSE;
1432 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1433 if(!hbmsave)
1435 DeleteDC(memdc);
1436 return FALSE;
1438 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1439 SelectObject(memdc, hbmsave);
1440 DeleteDC(memdc);
1441 return retval;
1443 case DST_COMPLEX:
1444 if(func) {
1445 BOOL bRet;
1446 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1448 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1449 bRet = func(hdc, lp, wp, cx, cy);
1450 /* Restore origin */
1451 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1452 return bRet;
1453 } else
1454 return FALSE;
1456 return FALSE;
1459 /**********************************************************************
1460 * PAINTING_DrawState()
1462 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1463 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1465 HBITMAP hbm, hbmsave;
1466 HFONT hfsave;
1467 HBRUSH hbsave, hbrtmp = 0;
1468 HDC memdc;
1469 RECT rc;
1470 UINT dtflags = DT_NOCLIP;
1471 COLORREF fg, bg;
1472 UINT opcode = flags & 0xf;
1473 INT len = wp;
1474 BOOL retval, tmp;
1476 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1478 if(unicode)
1479 len = strlenW((LPWSTR)lp);
1480 else
1481 len = strlen((LPSTR)lp);
1484 /* Find out what size the image has if not given by caller */
1485 if(!cx || !cy)
1487 SIZE s;
1488 CURSORICONINFO *ici;
1489 BITMAP bm;
1491 switch(opcode)
1493 case DST_TEXT:
1494 case DST_PREFIXTEXT:
1495 if(unicode)
1496 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1497 else
1498 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1499 if(!retval) return FALSE;
1500 break;
1502 case DST_ICON:
1503 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1504 if(!ici) return FALSE;
1505 s.cx = ici->nWidth;
1506 s.cy = ici->nHeight;
1507 GlobalUnlock16((HGLOBAL16)lp);
1508 break;
1510 case DST_BITMAP:
1511 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1512 return FALSE;
1513 s.cx = bm.bmWidth;
1514 s.cy = bm.bmHeight;
1515 break;
1517 case DST_COMPLEX: /* cx and cy must be set in this mode */
1518 return FALSE;
1521 if(!cx) cx = s.cx;
1522 if(!cy) cy = s.cy;
1525 rc.left = x;
1526 rc.top = y;
1527 rc.right = x + cx;
1528 rc.bottom = y + cy;
1530 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1531 dtflags |= DT_RIGHT;
1532 if(opcode == DST_TEXT)
1533 dtflags |= DT_NOPREFIX;
1535 /* For DSS_NORMAL we just jam in the image and return */
1536 if((flags & 0x7ff0) == DSS_NORMAL)
1538 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1541 /* For all other states we need to convert the image to B/W in a local bitmap */
1542 /* before it is displayed */
1543 fg = SetTextColor(hdc, RGB(0, 0, 0));
1544 bg = SetBkColor(hdc, RGB(255, 255, 255));
1545 hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1546 memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1547 retval = FALSE; /* assume failure */
1549 /* From here on we must use "goto cleanup" when something goes wrong */
1550 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1551 if(!hbm) goto cleanup;
1552 memdc = CreateCompatibleDC(hdc);
1553 if(!memdc) goto cleanup;
1554 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1555 if(!hbmsave) goto cleanup;
1556 rc.left = rc.top = 0;
1557 rc.right = cx;
1558 rc.bottom = cy;
1559 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1560 SetBkColor(memdc, RGB(255, 255, 255));
1561 SetTextColor(memdc, RGB(0, 0, 0));
1562 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1564 /* DST_COMPLEX may draw text as well,
1565 * so we must be sure that correct font is selected
1567 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1568 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1569 if(hfsave) SelectObject(memdc, hfsave);
1570 if(!tmp) goto cleanup;
1572 /* This state cause the image to be dithered */
1573 if(flags & DSS_UNION)
1575 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1576 if(!hbsave) goto cleanup;
1577 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1578 SelectObject(memdc, hbsave);
1579 if(!tmp) goto cleanup;
1582 if (flags & DSS_DISABLED)
1583 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1584 else if (flags & DSS_DEFAULT)
1585 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1587 /* Draw light or dark shadow */
1588 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1590 if(!hbrtmp) goto cleanup;
1591 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1592 if(!hbsave) goto cleanup;
1593 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1594 SelectObject(hdc, hbsave);
1595 DeleteObject(hbrtmp);
1596 hbrtmp = 0;
1599 if (flags & DSS_DISABLED)
1601 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1602 if(!hbrtmp) goto cleanup;
1604 else if (!hbr)
1606 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1609 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1611 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1613 retval = TRUE; /* We succeeded */
1615 cleanup:
1616 SetTextColor(hdc, fg);
1617 SetBkColor(hdc, bg);
1619 if(hbsave) SelectObject(hdc, hbsave);
1620 if(hbmsave) SelectObject(memdc, hbmsave);
1621 if(hbrtmp) DeleteObject(hbrtmp);
1622 if(hbm) DeleteObject(hbm);
1623 if(memdc) DeleteDC(memdc);
1625 return retval;
1628 /**********************************************************************
1629 * DrawStateA (USER32.@)
1631 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1632 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1633 INT x, INT y, INT cx, INT cy, UINT flags)
1635 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1638 /**********************************************************************
1639 * DrawStateW (USER32.@)
1641 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1642 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1643 INT x, INT y, INT cx, INT cy, UINT flags)
1645 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1648 /***********************************************************************
1649 * SelectPalette (USER.282)
1651 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1652 BOOL16 bForceBackground )
1654 WORD wBkgPalette = 1;
1656 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1658 HWND hwnd = WindowFromDC( hDC );
1659 if (hwnd)
1661 HWND hForeground = GetForegroundWindow();
1662 /* set primary palette if it's related to current active */
1663 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1666 return GDISelectPalette16( hDC, hPal, wBkgPalette);
1670 /***********************************************************************
1671 * RealizePalette (USER.283)
1673 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1675 UINT16 realized = GDIRealizePalette16( hDC );
1677 /* do not send anything if no colors were changed */
1678 if (realized && IsDCCurrentPalette16( hDC ))
1680 /* send palette change notification */
1681 HWND hWnd = WindowFromDC( hDC );
1682 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
1684 return realized;
1688 /***********************************************************************
1689 * UserRealizePalette (USER32.@)
1691 UINT WINAPI UserRealizePalette( HDC hDC )
1693 return RealizePalette16( hDC );