Add a missing </sect3>.
[wine.git] / windows / painting.c
blob072c52a290933bba61fcd32c7dc92804406dddae
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 "region.h"
14 #include "user.h"
15 #include "win.h"
16 #include "queue.h"
17 #include "dce.h"
18 #include "heap.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 /* Last COLOR id */
32 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
34 /* Last CTLCOLOR id */
35 #define CTLCOLOR_MAX CTLCOLOR_STATIC
38 /***********************************************************************
39 * WIN_HaveToDelayNCPAINT
41 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
42 * is generated as soon as a region intersecting the non-client area
43 * of a window is invalidated.
45 * This technique will work fine for all windows whose parents
46 * have the WS_CLIPCHILDREN style. When the parents have that style,
47 * they are not going to override the contents of their children.
48 * However, when the parent doesn't have that style, Windows relies
49 * on a "painter's algorithm" to display the contents of the windows.
50 * That is, windows are painted from back to front. This includes the
51 * non-client area.
53 * This method looks at the current state of a window to determine
54 * if the sending of the WM_NCPAINT message should be delayed until
55 * the BeginPaint call.
57 * PARAMS:
58 * wndPtr - A Locked window pointer to the window we're
59 * examining.
60 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
61 * function. This is a shortcut for the cases when
62 * we already know when to avoid scanning all the
63 * parents of a window. If you already know that this
64 * window's NCPAINT should be delayed, set the
65 * UNC_DELAY_NCPAINT flag for this parameter.
67 * This shortcut behavior is implemented in the
68 * RDW_Paint() method.
71 static BOOL WIN_HaveToDelayNCPAINT(
72 WND* wndPtr,
73 UINT uncFlags)
75 WND* parentWnd = NULL;
78 * Test the shortcut first. (duh)
80 if (uncFlags & UNC_DELAY_NCPAINT)
81 return TRUE;
84 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
85 * method only. This is another shortcut to avoid going
86 * up the parent's chain of the window to finally
87 * figure-out that none of them has an invalid region.
89 if (uncFlags & UNC_IN_BEGINPAINT)
90 return FALSE;
93 * Scan all the parents of this window to find a window
94 * that doesn't have the WS_CLIPCHILDREN style and that
95 * has an invalid region.
97 parentWnd = WIN_LockWndPtr(wndPtr->parent);
99 while (parentWnd!=NULL)
101 if ( ((parentWnd->dwStyle & WS_CLIPCHILDREN) == 0) &&
102 (parentWnd->hrgnUpdate != 0) )
104 WIN_ReleaseWndPtr(parentWnd);
105 return TRUE;
108 WIN_UpdateWndPtr(&parentWnd, parentWnd->parent);
111 WIN_ReleaseWndPtr(parentWnd);
113 return FALSE;
116 /***********************************************************************
117 * WIN_UpdateNCRgn
119 * Things to do:
120 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
121 * Crop hrgnUpdate to a client rect, especially if it 1.
122 * If UNC_REGION is set return update region for the client rect.
124 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
125 * The trick is that when the returned region handle may be different from hRgn.
126 * In this case the old hRgn must be considered gone. BUT, if the returned value
127 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
128 * a DC without extra clipping region.
130 HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
132 RECT r;
133 HRGN hClip = 0;
134 HRGN hrgnRet = 0;
136 TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n",
137 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
139 /* desktop window doesn't have a nonclient area */
140 if(wnd == WIN_GetDesktop())
142 wnd->flags &= ~WIN_NEEDS_NCPAINT;
143 if( wnd->hrgnUpdate > 1 )
144 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
145 else
147 hrgnRet = wnd->hrgnUpdate;
149 WIN_ReleaseDesktop();
150 return hrgnRet;
152 WIN_ReleaseDesktop();
154 if ((wnd->hwndSelf == GetForegroundWindow()) &&
155 !(wnd->flags & WIN_NCACTIVATED) )
157 wnd->flags |= WIN_NCACTIVATED;
158 uncFlags |= UNC_ENTIRE;
162 * If the window's non-client area needs to be painted,
164 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
165 !WIN_HaveToDelayNCPAINT(wnd, uncFlags) )
167 RECT r2, r3;
169 wnd->flags &= ~WIN_NEEDS_NCPAINT;
170 GETCLIENTRECTW( wnd, r );
172 TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n",
173 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
174 if( wnd->hrgnUpdate > 1 )
176 /* Check if update rgn overlaps with nonclient area */
178 GetRgnBox( wnd->hrgnUpdate, &r2 );
179 UnionRect( &r3, &r2, &r );
180 if( r3.left != r.left || r3.top != r.top ||
181 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
183 /* crop hrgnUpdate, save old one in hClip - the only
184 * case that places a valid region handle in hClip */
186 hClip = wnd->hrgnUpdate;
187 wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
188 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
191 if( uncFlags & UNC_CHECK )
193 GetRgnBox( wnd->hrgnUpdate, &r3 );
194 if( IsRectEmpty( &r3 ) )
196 /* delete the update region since all invalid
197 * parts were in the nonclient area */
199 DeleteObject( wnd->hrgnUpdate );
200 wnd->hrgnUpdate = 0;
201 if(!(wnd->flags & WIN_INTERNAL_PAINT))
202 QUEUE_DecPaintCount( wnd->hmemTaskQ );
204 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
208 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
210 else
211 if( wnd->hrgnUpdate == 1 )/* entire window */
213 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
214 if( uncFlags & UNC_REGION ) hrgnRet = 1;
215 uncFlags |= UNC_ENTIRE;
218 else /* no WM_NCPAINT unless forced */
220 if( wnd->hrgnUpdate > 1 )
222 copyrgn:
223 if( uncFlags & UNC_REGION )
224 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
226 else
227 if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
229 GETCLIENTRECTW( wnd, r );
230 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
231 if( uncFlags & UNC_REGION ) hrgnRet = 1;
235 if(!hClip && (uncFlags & UNC_ENTIRE) )
237 /* still don't do anything if there is no nonclient area */
238 hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
241 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
243 if ( hClip == hrgnRet && hrgnRet > 1 ) {
244 hClip = CreateRectRgn( 0, 0, 0, 0 );
245 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
248 SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
249 if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
250 DeleteObject( hClip );
252 * Since all Window locks are suspended while processing the WM_NCPAINT
253 * we want to make sure the window still exists before continuing.
255 if (!IsWindow(wnd->hwndSelf))
257 DeleteObject(hrgnRet);
258 hrgnRet=0;
262 TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
264 return hrgnRet;
268 /***********************************************************************
269 * BeginPaint (USER.39)
271 HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps )
273 PAINTSTRUCT ps;
275 BeginPaint( hwnd, &ps );
276 lps->hdc = ps.hdc;
277 lps->fErase = ps.fErase;
278 lps->rcPaint.top = ps.rcPaint.top;
279 lps->rcPaint.left = ps.rcPaint.left;
280 lps->rcPaint.right = ps.rcPaint.right;
281 lps->rcPaint.bottom = ps.rcPaint.bottom;
282 lps->fRestore = ps.fRestore;
283 lps->fIncUpdate = ps.fIncUpdate;
284 return lps->hdc;
288 /***********************************************************************
289 * BeginPaint (USER32.@)
291 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
293 BOOL bIcon;
294 HRGN hrgnUpdate;
295 RECT clipRect, clientRect;
296 WND *wndPtr = WIN_FindWndPtr( hwnd );
297 if (!wndPtr) return 0;
299 bIcon = (wndPtr->dwStyle & WS_MINIMIZE && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
301 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
303 /* send WM_NCPAINT and make sure hrgnUpdate is a valid rgn handle */
304 WIN_UpdateNCRgn( wndPtr, 0, UNC_UPDATE | UNC_IN_BEGINPAINT);
307 * Make sure the window is still a window. All window locks are suspended
308 * when the WM_NCPAINT is sent.
310 if (!IsWindow(wndPtr->hwndSelf))
312 WIN_ReleaseWndPtr(wndPtr);
313 return 0;
316 if( ((hrgnUpdate = wndPtr->hrgnUpdate) != 0) || (wndPtr->flags & WIN_INTERNAL_PAINT))
317 QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
319 wndPtr->hrgnUpdate = 0;
320 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
322 HideCaret( hwnd );
324 TRACE("hrgnUpdate = %04x, \n", hrgnUpdate);
326 if (GetClassWord16(wndPtr->hwndSelf, GCW_STYLE) & CS_PARENTDC)
328 /* Don't clip the output to the update region for CS_PARENTDC window */
329 if( hrgnUpdate )
330 DeleteObject(hrgnUpdate);
331 lps->hdc = GetDCEx( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
332 (bIcon ? DCX_WINDOW : 0) );
334 else
336 if( hrgnUpdate ) /* convert to client coordinates */
337 OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
338 wndPtr->rectWindow.top - wndPtr->rectClient.top );
339 lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
340 DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
341 /* ReleaseDC() in EndPaint() will delete the region */
344 TRACE("hdc = %04x\n", lps->hdc);
346 if (!lps->hdc)
348 WARN("GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
349 WIN_ReleaseWndPtr(wndPtr);
350 return 0;
353 /* It is possible that the clip box is bigger than the window itself,
354 if CS_PARENTDC flag is set. Windows never return a paint rect bigger
355 than the window itself, so we need to intersect the cliprect with
356 the window */
358 GetClientRect( hwnd, &clientRect );
360 GetClipBox( lps->hdc, &clipRect );
361 LPtoDP(lps->hdc, (LPPOINT)&clipRect, 2); /* GetClipBox returns LP */
363 IntersectRect(&lps->rcPaint, &clientRect, &clipRect);
364 DPtoLP(lps->hdc, (LPPOINT)&lps->rcPaint, 2); /* we must return LP */
366 TRACE("box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
367 lps->rcPaint.right, lps->rcPaint.bottom );
369 if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
371 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
372 lps->fErase = !SendMessageA(hwnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
373 (WPARAM16)lps->hdc, 0 );
375 else lps->fErase = TRUE;
377 WIN_ReleaseWndPtr(wndPtr);
378 return lps->hdc;
382 /***********************************************************************
383 * EndPaint (USER.40)
385 BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
387 ReleaseDC16( hwnd, lps->hdc );
388 ShowCaret( hwnd );
389 return TRUE;
393 /***********************************************************************
394 * EndPaint (USER32.@)
396 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
398 ReleaseDC( hwnd, lps->hdc );
399 ShowCaret( hwnd );
400 return TRUE;
404 /***********************************************************************
405 * FillWindow (USER.324)
407 void WINAPI FillWindow16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc, HBRUSH16 hbrush )
409 RECT rect;
410 RECT16 rc16;
411 GetClientRect( hwnd, &rect );
412 DPtoLP( hdc, (LPPOINT)&rect, 2 );
413 CONV_RECT32TO16( &rect, &rc16 );
414 PaintRect16( hwndParent, hwnd, hdc, hbrush, &rc16 );
418 /***********************************************************************
419 * PAINT_GetControlBrush
421 static HBRUSH16 PAINT_GetControlBrush( HWND hParent, HWND hWnd, HDC16 hDC, UINT16 ctlType )
423 HBRUSH16 bkgBrush = (HBRUSH16)SendMessageA( hParent, WM_CTLCOLORMSGBOX + ctlType,
424 (WPARAM)hDC, (LPARAM)hWnd );
425 if( !IsGDIObject16(bkgBrush) )
426 bkgBrush = DEFWND_ControlColor( hDC, ctlType );
427 return bkgBrush;
431 /***********************************************************************
432 * PaintRect (USER.325)
434 void WINAPI PaintRect16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc,
435 HBRUSH16 hbrush, const RECT16 *rect)
437 if( hbrush <= CTLCOLOR_MAX )
439 if( hwndParent )
440 hbrush = PAINT_GetControlBrush( hwndParent, hwnd, hdc, (UINT16)hbrush );
441 else
442 return;
444 if( hbrush )
445 FillRect16( hdc, rect, hbrush );
449 /***********************************************************************
450 * GetControlBrush (USER.326)
452 HBRUSH16 WINAPI GetControlBrush16( HWND16 hwnd, HDC16 hdc, UINT16 ctlType )
454 WND* wndPtr = WIN_FindWndPtr( hwnd );
455 HBRUSH16 retvalue;
457 if((ctlType <= CTLCOLOR_MAX) && wndPtr )
459 WND* parent;
460 if( wndPtr->dwStyle & WS_POPUP ) parent = WIN_LockWndPtr(wndPtr->owner);
461 else parent = WIN_LockWndPtr(wndPtr->parent);
462 if( !parent ) parent = wndPtr;
463 retvalue = (HBRUSH16)PAINT_GetControlBrush( parent->hwndSelf, hwnd, hdc, ctlType );
464 WIN_ReleaseWndPtr(parent);
465 goto END;
467 retvalue = (HBRUSH16)0;
468 END:
469 WIN_ReleaseWndPtr(wndPtr);
470 return retvalue;
474 /***********************************************************************
475 * RDW_ValidateParent [RDW_UpdateRgns() helper]
477 * Validate the portions of parents that are covered by a validated child
478 * wndPtr = child
480 void RDW_ValidateParent(WND *wndChild)
482 WND *wndParent = WIN_LockWndPtr(wndChild->parent);
483 WND *wndDesktop = WIN_GetDesktop();
484 HRGN hrg;
486 if (wndChild->hrgnUpdate == 1 ) {
487 RECT r;
488 r.left = 0;
489 r.top = 0;
490 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
491 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
492 hrg = CreateRectRgnIndirect( &r );
493 } else
494 hrg = wndChild->hrgnUpdate;
496 while ((wndParent) && (wndParent != wndDesktop) ) {
497 if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
499 if (wndParent->hrgnUpdate != 0)
501 POINT ptOffset;
502 RECT rect, rectParent;
503 if( wndParent->hrgnUpdate == 1 )
505 RECT r;
507 r.left = 0;
508 r.top = 0;
509 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
510 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
512 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
514 /* we must offset the child region by the offset of the child rect in the parent */
515 GetWindowRect(wndParent->hwndSelf, &rectParent);
516 GetWindowRect(wndChild->hwndSelf, &rect);
517 ptOffset.x = rect.left - rectParent.left;
518 ptOffset.y = rect.top - rectParent.top;
519 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
520 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
521 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
524 WIN_UpdateWndPtr(&wndParent, wndParent->parent);
526 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
527 WIN_ReleaseWndPtr(wndParent);
528 WIN_ReleaseDesktop();
531 /***********************************************************************
532 * RDW_UpdateRgns [RedrawWindow() helper]
534 * Walks the window tree and adds/removes parts of the hRgn to/from update
535 * regions of windows that overlap it. Also, manages internal paint flags.
537 * NOTE: Walks the window tree so the caller must lock it.
538 * MUST preserve hRgn (can modify but then has to restore).
540 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
543 * Called only when one of the following is set:
544 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
547 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
548 BOOL bChildren = ( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE)
549 && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
550 RECT r;
552 r.left = 0;
553 r.top = 0;
554 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
555 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
557 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
559 if( flags & RDW_INVALIDATE )
561 if( hRgn > 1 )
563 switch( wndPtr->hrgnUpdate )
565 default:
566 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
567 /* fall through */
568 case 0:
569 wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate,
570 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
571 &r, NULL );
572 if( !bHadOne )
574 GetRgnBox( wndPtr->hrgnUpdate, &r );
575 if( IsRectEmpty( &r ) )
577 DeleteObject( wndPtr->hrgnUpdate );
578 wndPtr->hrgnUpdate = 0;
579 goto end;
582 break;
583 case 1: /* already an entire window */
584 break;
587 else if( hRgn == 1 )
589 if( wndPtr->hrgnUpdate > 1 )
590 DeleteObject( wndPtr->hrgnUpdate );
591 wndPtr->hrgnUpdate = 1;
593 else
594 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
596 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
597 QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
599 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
600 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
601 flags |= RDW_FRAME;
603 else if( flags & RDW_VALIDATE )
605 if( wndPtr->hrgnUpdate )
607 if( hRgn > 1 )
609 if( wndPtr->hrgnUpdate == 1 )
610 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
612 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
613 == NULLREGION )
614 goto EMPTY;
616 else /* validate everything */
618 if( wndPtr->hrgnUpdate > 1 )
620 EMPTY:
621 DeleteObject( wndPtr->hrgnUpdate );
623 wndPtr->hrgnUpdate = 0;
626 if( !wndPtr->hrgnUpdate )
628 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
629 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
630 QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
634 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
635 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
639 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
640 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
642 /* in/validate child windows that intersect with the region if it
643 * is a valid handle. */
645 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
647 if( hRgn > 1 && bChildren )
649 WND* wnd = wndPtr->child;
650 POINT ptTotal, prevOrigin = {0,0};
651 POINT ptClient;
653 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
654 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
656 for( ptTotal.x = ptTotal.y = 0; wnd; wnd = wnd->next )
658 if( wnd->dwStyle & WS_VISIBLE )
660 POINT ptOffset;
662 r.left = wnd->rectWindow.left + ptClient.x;
663 r.right = wnd->rectWindow.right + ptClient.x;
664 r.top = wnd->rectWindow.top + ptClient.y;
665 r.bottom = wnd->rectWindow.bottom + ptClient.y;
667 ptOffset.x = r.left - prevOrigin.x;
668 ptOffset.y = r.top - prevOrigin.y;
669 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
671 if( RectInRegion( hRgn, &r ) )
673 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
674 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
675 prevOrigin.x = r.left + ptTotal.x;
676 prevOrigin.y = r.top + ptTotal.y;
677 ptTotal.x += ptOffset.x;
678 ptTotal.y += ptOffset.y;
682 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
683 bChildren = 0;
687 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
689 if( bChildren )
691 WND* wnd;
692 for( wnd = wndPtr->child; wnd; wnd = wnd->next )
693 if( wnd->dwStyle & WS_VISIBLE )
694 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
697 end:
699 /* Set/clear internal paint flag */
701 if (flags & RDW_INTERNALPAINT)
703 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
704 QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
705 wndPtr->flags |= WIN_INTERNAL_PAINT;
707 else if (flags & RDW_NOINTERNALPAINT)
709 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
710 QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
711 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
715 /***********************************************************************
716 * RDW_Paint [RedrawWindow() helper]
718 * Walks the window tree and paints/erases windows that have
719 * nonzero update regions according to redraw flags. hrgn is a scratch
720 * region passed down during recursion. Must not be 1.
723 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
725 /* NOTE: wndPtr is locked by caller.
727 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
728 * SendMessage() calls. This is a comment inside DefWindowProc() source
729 * from 16-bit SDK:
731 * This message avoids lots of inter-app message traffic
732 * by switching to the other task and continuing the
733 * recursion there.
735 * wParam = flags
736 * LOWORD(lParam) = hrgnClip
737 * HIWORD(lParam) = hwndSkip (not used; always NULL)
740 HDC hDC;
741 HWND hWnd = wndPtr->hwndSelf;
742 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
744 /* Erase/update the window itself ... */
746 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
749 * Check if this window should delay it's processing of WM_NCPAINT.
750 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
752 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr, 0) )
753 ex |= RDW_EX_DELAY_NCPAINT;
755 if (flags & RDW_UPDATENOW)
757 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
758 SendMessage16( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
760 else if ((flags & RDW_ERASENOW) || (ex & RDW_EX_TOPFRAME))
762 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
763 HRGN hrgnRet;
765 hrgnRet = WIN_UpdateNCRgn(wndPtr,
766 hrgn,
767 UNC_REGION | UNC_CHECK |
768 ((ex & RDW_EX_TOPFRAME) ? UNC_ENTIRE : 0) |
769 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
771 if( hrgnRet )
773 if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
774 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
776 if( bIcon ) dcx |= DCX_WINDOW;
777 else
778 if( hrgnRet )
779 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
780 wndPtr->rectWindow.top - wndPtr->rectClient.top);
781 else
782 dcx &= ~DCX_INTERSECTRGN;
783 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
785 if (SendMessage16( hWnd, (bIcon) ? WM_ICONERASEBKGND
786 : WM_ERASEBKGND, (WPARAM16)hDC, 0 ))
787 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
788 ReleaseDC( hWnd, hDC );
794 if( !IsWindow(hWnd) ) return hrgn;
795 ex &= ~RDW_EX_TOPFRAME;
797 /* ... and its child windows */
799 if( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE)
800 && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
802 WND** list, **ppWnd;
804 if( (list = WIN_BuildWinArray( wndPtr, 0, NULL )) )
806 wndPtr = NULL;
807 for (ppWnd = list; *ppWnd; ppWnd++)
809 WIN_UpdateWndPtr(&wndPtr,*ppWnd);
810 if (!IsWindow(wndPtr->hwndSelf)) continue;
811 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
812 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
813 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
815 WIN_ReleaseWndPtr(wndPtr);
816 WIN_ReleaseWinArray(list);
820 return hrgn;
823 /***********************************************************************
824 * PAINT_RedrawWindow
827 BOOL PAINT_RedrawWindow( HWND hwnd, const RECT *rectUpdate,
828 HRGN hrgnUpdate, UINT flags, UINT ex )
830 HRGN hRgn = 0;
831 RECT r, r2;
832 POINT pt;
833 WND* wndPtr;
835 if (!hwnd) hwnd = GetDesktopWindow();
836 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
838 /* check if the window or its parents are visible/not minimized */
840 if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
842 WIN_ReleaseWndPtr(wndPtr);
843 return TRUE;
846 if (TRACE_ON(win))
848 if( hrgnUpdate )
850 GetRgnBox( hrgnUpdate, &r );
851 TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x, exflags=%04x\n",
852 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags, ex);
854 else
856 if( rectUpdate )
857 r = *rectUpdate;
858 else
859 SetRectEmpty( &r );
860 TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x, exflags=%04x\n",
861 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
862 r.top, r.right, r.bottom, hrgnUpdate, flags, ex );
866 /* prepare an update region in window coordinates */
868 if( flags & RDW_FRAME )
869 r = wndPtr->rectWindow;
870 else
871 r = wndPtr->rectClient;
873 if( ex & RDW_EX_XYWINDOW )
875 pt.x = pt.y = 0;
876 OffsetRect( &r, -wndPtr->rectWindow.left, -wndPtr->rectWindow.top );
878 else
880 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
881 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
882 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
885 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
887 /* If the window doesn't have hrgnUpdate we leave hRgn zero
888 * and put a new region straight into wndPtr->hrgnUpdate
889 * so that RDW_UpdateRgns() won't have to do any extra work.
892 if( hrgnUpdate )
894 if( wndPtr->hrgnUpdate )
895 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
896 else
897 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
899 else if( rectUpdate )
901 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
902 OffsetRect( &r2, pt.x, pt.y );
904 rect2i:
905 if( wndPtr->hrgnUpdate == 0 )
906 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
907 else
908 hRgn = CreateRectRgnIndirect( &r2 );
910 else /* entire window or client depending on RDW_FRAME */
912 if( flags & RDW_FRAME )
914 if( wndPtr->hrgnUpdate )
915 DeleteObject( wndPtr->hrgnUpdate );
916 wndPtr->hrgnUpdate = 1;
918 else
920 GETCLIENTRECTW( wndPtr, r2 );
921 goto rect2i;
925 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
927 /* In this we cannot leave with zero hRgn */
928 if( hrgnUpdate )
930 hRgn = REGION_CropRgn( hRgn, hrgnUpdate, &r, &pt );
931 GetRgnBox( hRgn, &r2 );
932 if( IsRectEmpty( &r2 ) ) goto END;
934 else if( rectUpdate )
936 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
937 OffsetRect( &r2, pt.x, pt.y );
938 rect2v:
939 hRgn = CreateRectRgnIndirect( &r2 );
941 else /* entire window or client depending on RDW_FRAME */
943 if( flags & RDW_FRAME )
944 hRgn = 1;
945 else
947 GETCLIENTRECTW( wndPtr, r2 );
948 goto rect2v;
953 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
955 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
957 /* Erase/update windows, from now on hRgn is a scratch region */
959 hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, ex );
961 END:
962 if( hRgn > 1 && (hRgn != hrgnUpdate) )
963 DeleteObject(hRgn );
964 WIN_ReleaseWndPtr(wndPtr);
965 return TRUE;
969 /***********************************************************************
970 * RedrawWindow (USER32.@)
972 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
973 HRGN hrgnUpdate, UINT flags )
975 return PAINT_RedrawWindow( hwnd, rectUpdate, hrgnUpdate, flags, 0 );
979 /***********************************************************************
980 * RedrawWindow (USER.290)
982 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
983 HRGN16 hrgnUpdate, UINT16 flags )
985 if (rectUpdate)
987 RECT r;
988 CONV_RECT16TO32( rectUpdate, &r );
989 return (BOOL16)RedrawWindow( (HWND)hwnd, &r, hrgnUpdate, flags );
991 return (BOOL16)PAINT_RedrawWindow( (HWND)hwnd, NULL,
992 (HRGN)hrgnUpdate, flags, 0 );
996 /***********************************************************************
997 * UpdateWindow (USER.124)
999 void WINAPI UpdateWindow16( HWND16 hwnd )
1001 PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN, 0 );
1004 /***********************************************************************
1005 * UpdateWindow (USER32.@)
1007 void WINAPI UpdateWindow( HWND hwnd )
1009 PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN, 0 );
1012 /***********************************************************************
1013 * InvalidateRgn (USER.126)
1015 void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1017 PAINT_RedrawWindow((HWND)hwnd, NULL, (HRGN)hrgn,
1018 RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
1022 /***********************************************************************
1023 * InvalidateRgn (USER32.@)
1025 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1027 return PAINT_RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
1031 /***********************************************************************
1032 * InvalidateRect (USER.125)
1034 void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
1036 RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1040 /***********************************************************************
1041 * InvalidateRect (USER32.@)
1043 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1045 return PAINT_RedrawWindow( hwnd, rect, 0,
1046 RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
1050 /***********************************************************************
1051 * ValidateRgn (USER.128)
1053 void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
1055 PAINT_RedrawWindow( (HWND)hwnd, NULL, (HRGN)hrgn,
1056 RDW_VALIDATE | RDW_NOCHILDREN, 0 );
1060 /***********************************************************************
1061 * ValidateRgn (USER32.@)
1063 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1065 PAINT_RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
1069 /***********************************************************************
1070 * ValidateRect (USER.127)
1072 void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
1074 RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1078 /***********************************************************************
1079 * ValidateRect (USER32.@)
1081 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1083 PAINT_RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
1087 /***********************************************************************
1088 * GetUpdateRect (USER.190)
1090 BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
1092 RECT r;
1093 BOOL16 ret;
1095 if (!rect) return GetUpdateRect( hwnd, NULL, erase );
1096 ret = GetUpdateRect( hwnd, &r, erase );
1097 CONV_RECT32TO16( &r, rect );
1098 return ret;
1102 /***********************************************************************
1103 * GetUpdateRect (USER32.@)
1105 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1107 BOOL retvalue;
1108 WND * wndPtr = WIN_FindWndPtr( hwnd );
1109 if (!wndPtr) return FALSE;
1111 if (rect)
1113 if (wndPtr->hrgnUpdate > 1)
1115 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
1116 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
1118 retvalue = FALSE;
1119 goto END;
1121 GetRgnBox( hrgn, rect );
1122 DeleteObject( hrgn );
1123 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
1125 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
1127 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
1131 else
1132 if( wndPtr->hrgnUpdate == 1 )
1134 GetClientRect( hwnd, rect );
1135 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
1137 else
1138 SetRectEmpty( rect );
1140 retvalue = (wndPtr->hrgnUpdate >= 1);
1141 END:
1142 WIN_ReleaseWndPtr(wndPtr);
1143 return retvalue;
1147 /***********************************************************************
1148 * GetUpdateRgn (USER.237)
1150 INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1152 return GetUpdateRgn( hwnd, hrgn, erase );
1156 /***********************************************************************
1157 * GetUpdateRgn (USER32.@)
1159 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1161 INT retval;
1162 WND * wndPtr = WIN_FindWndPtr( hwnd );
1163 if (!wndPtr) return ERROR;
1165 if (wndPtr->hrgnUpdate == 0)
1167 SetRectRgn( hrgn, 0, 0, 0, 0 );
1168 retval = NULLREGION;
1169 goto END;
1171 else
1172 if (wndPtr->hrgnUpdate == 1)
1174 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1175 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1176 retval = SIMPLEREGION;
1178 else
1180 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1181 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1182 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1184 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1185 END:
1186 WIN_ReleaseWndPtr(wndPtr);
1187 return retval;
1191 /***********************************************************************
1192 * ExcludeUpdateRgn (USER.238)
1194 INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
1196 return ExcludeUpdateRgn( hdc, hwnd );
1200 /***********************************************************************
1201 * ExcludeUpdateRgn (USER32.@)
1203 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1205 RECT rect;
1206 WND * wndPtr;
1208 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1210 if (wndPtr->hrgnUpdate)
1212 INT ret;
1213 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1214 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1215 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1216 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1217 if( wndPtr->hrgnUpdate > 1 )
1219 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1220 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1221 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1224 /* do ugly coordinate translations in dce.c */
1226 ret = DCE_ExcludeRgn( hdc, wndPtr, hrgn );
1227 DeleteObject( hrgn );
1228 WIN_ReleaseWndPtr(wndPtr);
1229 return ret;
1231 WIN_ReleaseWndPtr(wndPtr);
1232 return GetClipBox( hdc, &rect );
1237 /***********************************************************************
1238 * FillRect (USER.81)
1239 * NOTE
1240 * The Win16 variant doesn't support special color brushes like
1241 * the Win32 one, despite the fact that Win16, as well as Win32,
1242 * supports special background brushes for a window class.
1244 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1246 HBRUSH prevBrush;
1248 /* coordinates are logical so we cannot fast-check 'rect',
1249 * it will be done later in the PatBlt().
1252 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1253 PatBlt( hdc, rect->left, rect->top,
1254 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1255 SelectObject( hdc, prevBrush );
1256 return 1;
1260 /***********************************************************************
1261 * FillRect (USER32.@)
1263 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1265 HBRUSH prevBrush;
1267 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1268 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1271 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1272 PatBlt( hdc, rect->left, rect->top,
1273 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1274 SelectObject( hdc, prevBrush );
1275 return 1;
1279 /***********************************************************************
1280 * InvertRect (USER.82)
1282 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1284 PatBlt( hdc, rect->left, rect->top,
1285 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1289 /***********************************************************************
1290 * InvertRect (USER32.@)
1292 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1294 return PatBlt( hdc, rect->left, rect->top,
1295 rect->right - rect->left, rect->bottom - rect->top,
1296 DSTINVERT );
1300 /***********************************************************************
1301 * FrameRect (USER32.@)
1303 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1305 HBRUSH prevBrush;
1306 RECT r = *rect;
1308 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1309 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1311 PatBlt( hdc, r.left, r.top, 1,
1312 r.bottom - r.top, PATCOPY );
1313 PatBlt( hdc, r.right - 1, r.top, 1,
1314 r.bottom - r.top, PATCOPY );
1315 PatBlt( hdc, r.left, r.top,
1316 r.right - r.left, 1, PATCOPY );
1317 PatBlt( hdc, r.left, r.bottom - 1,
1318 r.right - r.left, 1, PATCOPY );
1320 SelectObject( hdc, prevBrush );
1321 return TRUE;
1325 /***********************************************************************
1326 * FrameRect (USER.83)
1328 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1330 RECT rect;
1331 CONV_RECT16TO32( rect16, &rect );
1332 return FrameRect( hdc, &rect, hbrush );
1336 /***********************************************************************
1337 * DrawFocusRect (USER.466)
1339 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1341 RECT rect32;
1342 CONV_RECT16TO32( rc, &rect32 );
1343 DrawFocusRect( hdc, &rect32 );
1347 /***********************************************************************
1348 * DrawFocusRect (USER32.@)
1350 * FIXME: PatBlt(PATINVERT) with background brush.
1352 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1354 HBRUSH hOldBrush;
1355 HPEN hOldPen, hNewPen;
1356 INT oldDrawMode, oldBkMode;
1358 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1359 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1360 hOldPen = SelectObject(hdc, hNewPen);
1361 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1362 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1364 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1366 SetBkMode(hdc, oldBkMode);
1367 SetROP2(hdc, oldDrawMode);
1368 SelectObject(hdc, hOldPen);
1369 DeleteObject(hNewPen);
1370 SelectObject(hdc, hOldBrush);
1372 return TRUE;
1375 /**********************************************************************
1376 * DrawAnimatedRects (USER.448)
1378 BOOL16 WINAPI DrawAnimatedRects16( HWND16 hwnd, INT16 idAni,
1379 const RECT16* lprcFrom,
1380 const RECT16* lprcTo )
1382 RECT rcFrom32, rcTo32;
1384 rcFrom32.left = (INT)lprcFrom->left;
1385 rcFrom32.top = (INT)lprcFrom->top;
1386 rcFrom32.right = (INT)lprcFrom->right;
1387 rcFrom32.bottom = (INT)lprcFrom->bottom;
1389 rcTo32.left = (INT)lprcTo->left;
1390 rcTo32.top = (INT)lprcTo->top;
1391 rcTo32.right = (INT)lprcTo->right;
1392 rcTo32.bottom = (INT)lprcTo->bottom;
1394 return DrawAnimatedRects((HWND)hwnd, (INT)idAni, &rcFrom32, &rcTo32);
1398 /**********************************************************************
1399 * DrawAnimatedRects (USER32.@)
1401 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1402 const RECT* lprcFrom,
1403 const RECT* lprcTo )
1405 FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1406 return TRUE;
1410 /**********************************************************************
1411 * PAINTING_DrawStateJam
1413 * Jams in the requested type in the dc
1415 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1416 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1417 LPRECT rc, UINT dtflags,
1418 BOOL unicode, BOOL _32bit)
1420 HDC memdc;
1421 HBITMAP hbmsave;
1422 BOOL retval;
1423 INT cx = rc->right - rc->left;
1424 INT cy = rc->bottom - rc->top;
1426 switch(opcode)
1428 case DST_TEXT:
1429 case DST_PREFIXTEXT:
1430 if(unicode)
1431 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1432 else if(_32bit)
1433 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1434 else
1435 return DrawTextA(hdc, MapSL(lp), (INT)wp, rc, dtflags);
1437 case DST_ICON:
1438 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1440 case DST_BITMAP:
1441 memdc = CreateCompatibleDC(hdc);
1442 if(!memdc) return FALSE;
1443 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1444 if(!hbmsave)
1446 DeleteDC(memdc);
1447 return FALSE;
1449 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1450 SelectObject(memdc, hbmsave);
1451 DeleteDC(memdc);
1452 return retval;
1454 case DST_COMPLEX:
1455 if(func) {
1456 BOOL bRet;
1457 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1459 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1460 if(_32bit)
1461 bRet = func(hdc, lp, wp, cx, cy);
1462 else
1463 bRet = (BOOL)((DRAWSTATEPROC16)func)((HDC16)hdc, (LPARAM)lp, (WPARAM16)wp, (INT16)cx, (INT16)cy);
1464 /* Restore origin */
1465 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1466 return bRet;
1467 } else
1468 return FALSE;
1470 return FALSE;
1473 /**********************************************************************
1474 * PAINTING_DrawState()
1476 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr,
1477 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1478 INT x, INT y, INT cx, INT cy,
1479 UINT flags, BOOL unicode, BOOL _32bit)
1481 HBITMAP hbm, hbmsave;
1482 HFONT hfsave;
1483 HBRUSH hbsave, hbrtmp = 0;
1484 HDC memdc;
1485 RECT rc;
1486 UINT dtflags = DT_NOCLIP;
1487 COLORREF fg, bg;
1488 UINT opcode = flags & 0xf;
1489 INT len = wp;
1490 BOOL retval, tmp;
1492 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1494 if(unicode)
1495 len = strlenW((LPWSTR)lp);
1496 else if(_32bit)
1497 len = strlen((LPSTR)lp);
1498 else
1499 len = strlen(MapSL(lp));
1502 /* Find out what size the image has if not given by caller */
1503 if(!cx || !cy)
1505 SIZE s;
1506 CURSORICONINFO *ici;
1507 BITMAP bm;
1509 switch(opcode)
1511 case DST_TEXT:
1512 case DST_PREFIXTEXT:
1513 if(unicode)
1514 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1515 else if(_32bit)
1516 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1517 else
1518 retval = GetTextExtentPoint32A(hdc, MapSL(lp), len, &s);
1519 if(!retval) return FALSE;
1520 break;
1522 case DST_ICON:
1523 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1524 if(!ici) return FALSE;
1525 s.cx = ici->nWidth;
1526 s.cy = ici->nHeight;
1527 GlobalUnlock16((HGLOBAL16)lp);
1528 break;
1530 case DST_BITMAP:
1531 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1532 return FALSE;
1533 s.cx = bm.bmWidth;
1534 s.cy = bm.bmHeight;
1535 break;
1537 case DST_COMPLEX: /* cx and cy must be set in this mode */
1538 return FALSE;
1541 if(!cx) cx = s.cx;
1542 if(!cy) cy = s.cy;
1545 rc.left = x;
1546 rc.top = y;
1547 rc.right = x + cx;
1548 rc.bottom = y + cy;
1550 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1551 dtflags |= DT_RIGHT;
1552 if(opcode == DST_TEXT)
1553 dtflags |= DT_NOPREFIX;
1555 /* For DSS_NORMAL we just jam in the image and return */
1556 if((flags & 0x7ff0) == DSS_NORMAL)
1558 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode, _32bit);
1561 /* For all other states we need to convert the image to B/W in a local bitmap */
1562 /* before it is displayed */
1563 fg = SetTextColor(hdc, RGB(0, 0, 0));
1564 bg = SetBkColor(hdc, RGB(255, 255, 255));
1565 hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1566 memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1567 retval = FALSE; /* assume failure */
1569 /* From here on we must use "goto cleanup" when something goes wrong */
1570 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1571 if(!hbm) goto cleanup;
1572 memdc = CreateCompatibleDC(hdc);
1573 if(!memdc) goto cleanup;
1574 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1575 if(!hbmsave) goto cleanup;
1576 rc.left = rc.top = 0;
1577 rc.right = cx;
1578 rc.bottom = cy;
1579 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1580 SetBkColor(memdc, RGB(255, 255, 255));
1581 SetTextColor(memdc, RGB(0, 0, 0));
1582 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1584 /* DST_COMPLEX may draw text as well,
1585 * so we must be sure that correct font is selected
1587 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1588 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode, _32bit);
1589 if(hfsave) SelectObject(memdc, hfsave);
1590 if(!tmp) goto cleanup;
1592 /* This state cause the image to be dithered */
1593 if(flags & DSS_UNION)
1595 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1596 if(!hbsave) goto cleanup;
1597 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1598 SelectObject(memdc, hbsave);
1599 if(!tmp) goto cleanup;
1602 if (flags & DSS_DISABLED)
1603 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1604 else if (flags & DSS_DEFAULT)
1605 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1607 /* Draw light or dark shadow */
1608 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1610 if(!hbrtmp) goto cleanup;
1611 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1612 if(!hbsave) goto cleanup;
1613 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1614 SelectObject(hdc, hbsave);
1615 DeleteObject(hbrtmp);
1616 hbrtmp = 0;
1619 if (flags & DSS_DISABLED)
1621 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1622 if(!hbrtmp) goto cleanup;
1624 else if (!hbr)
1626 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1629 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1631 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1633 retval = TRUE; /* We succeeded */
1635 cleanup:
1636 SetTextColor(hdc, fg);
1637 SetBkColor(hdc, bg);
1639 if(hbsave) SelectObject(hdc, hbsave);
1640 if(hbmsave) SelectObject(memdc, hbmsave);
1641 if(hbrtmp) DeleteObject(hbrtmp);
1642 if(hbm) DeleteObject(hbm);
1643 if(memdc) DeleteDC(memdc);
1645 return retval;
1648 /**********************************************************************
1649 * DrawStateA (USER32.@)
1651 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1652 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1653 INT x, INT y, INT cx, INT cy, UINT flags)
1655 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE, TRUE);
1658 /**********************************************************************
1659 * DrawStateW (USER32.@)
1661 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1662 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1663 INT x, INT y, INT cx, INT cy, UINT flags)
1665 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE, TRUE);
1668 /**********************************************************************
1669 * DrawState (USER.449)
1671 BOOL16 WINAPI DrawState16(HDC16 hdc, HBRUSH16 hbr,
1672 DRAWSTATEPROC16 func, LPARAM ldata, WPARAM16 wdata,
1673 INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags)
1675 return PAINTING_DrawState(hdc, hbr, (DRAWSTATEPROC)func, ldata, wdata, x, y, cx, cy, flags, FALSE, FALSE);
1679 /***********************************************************************
1680 * SelectPalette (USER.282)
1682 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1683 BOOL16 bForceBackground )
1685 WORD wBkgPalette = 1;
1687 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1689 HWND hwnd = WindowFromDC( hDC );
1690 if (hwnd)
1692 HWND hForeground = GetForegroundWindow();
1693 /* set primary palette if it's related to current active */
1694 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1697 return GDISelectPalette16( hDC, hPal, wBkgPalette);
1701 /***********************************************************************
1702 * RealizePalette (USER.283)
1704 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1706 UINT16 realized = GDIRealizePalette16( hDC );
1708 /* do not send anything if no colors were changed */
1709 if (realized && IsDCCurrentPalette16( hDC ))
1711 /* send palette change notification */
1712 HWND hWnd = WindowFromDC( hDC );
1713 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
1715 return realized;
1719 /***********************************************************************
1720 * UserRealizePalette (USER32.@)
1722 UINT WINAPI UserRealizePalette( HDC hDC )
1724 return RealizePalette16( hDC );