(Merged by Marcus Meissner <marcus@jet.franken.de>)
[wine/multimedia.git] / windows / painting.c
bloba8eea979ab66324ee8740a28e3e507cca46f575a
1 /*
2 * Window painting functions
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1999 Alex Korobka
6 */
8 #include "windef.h"
9 #include "wingdi.h"
10 #include "wine/winuser16.h"
11 #include "wine/unicode.h"
12 #include "region.h"
13 #include "win.h"
14 #include "queue.h"
15 #include "dce.h"
16 #include "heap.h"
17 #include "debugtools.h"
18 #include "cache.h"
20 DEFAULT_DEBUG_CHANNEL(win);
21 DECLARE_DEBUG_CHANNEL(nonclient);
23 /* client rect in window coordinates */
25 #define GETCLIENTRECTW( wnd, r ) (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
26 (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
27 (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
28 (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
30 /* Last COLOR id */
31 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
33 /* Last CTLCOLOR id */
34 #define CTLCOLOR_MAX CTLCOLOR_STATIC
37 /***********************************************************************
38 * WIN_HaveToDelayNCPAINT
40 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
41 * is generated as soon as a region intersecting the non-client area
42 * of a window is invalidated.
44 * This technique will work fine for all windows whose parents
45 * have the WS_CLIPCHILDREN style. When the parents have that style,
46 * they are not going to override the contents of their children.
47 * However, when the parent doesn't have that style, Windows relies
48 * on a "painter's algorithm" to display the contents of the windows.
49 * That is, windows are painted from back to front. This includes the
50 * non-client area.
52 * This method looks at the current state of a window to determine
53 * if the sending of the WM_NCPAINT message should be delayed until
54 * the BeginPaint call.
56 * PARAMS:
57 * wndPtr - A Locked window pointer to the window we're
58 * examining.
59 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
60 * function. This is a shortcut for the cases when
61 * we already know when to avoid scanning all the
62 * parents of a window. If you already know that this
63 * window's NCPAINT should be delayed, set the
64 * UNC_DELAY_NCPAINT flag for this parameter.
66 * This shortcut behavior is implemented in the
67 * RDW_Paint() method.
70 static BOOL WIN_HaveToDelayNCPAINT(
71 WND* wndPtr,
72 UINT uncFlags)
74 WND* parentWnd = NULL;
77 * Test the shortcut first. (duh)
79 if (uncFlags & UNC_DELAY_NCPAINT)
80 return TRUE;
83 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
84 * method only. This is another shortcut to avoid going
85 * up the parent's chain of the window to finally
86 * figure-out that none of them has an invalid region.
88 if (uncFlags & UNC_IN_BEGINPAINT)
89 return FALSE;
92 * Scan all the parents of this window to find a window
93 * that doesn't have the WS_CLIPCHILDREN style and that
94 * has an invalid region.
96 parentWnd = WIN_LockWndPtr(wndPtr->parent);
98 while (parentWnd!=NULL)
100 if ( ((parentWnd->dwStyle & WS_CLIPCHILDREN) == 0) &&
101 (parentWnd->hrgnUpdate != 0) )
103 WIN_ReleaseWndPtr(parentWnd);
104 return TRUE;
107 WIN_UpdateWndPtr(&parentWnd, parentWnd->parent);
110 WIN_ReleaseWndPtr(parentWnd);
112 return FALSE;
115 /***********************************************************************
116 * WIN_UpdateNCRgn
118 * Things to do:
119 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
120 * Crop hrgnUpdate to a client rect, especially if it 1.
121 * If UNC_REGION is set return update region for the client rect.
123 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
124 * The trick is that when the returned region handle may be different from hRgn.
125 * In this case the old hRgn must be considered gone. BUT, if the returned value
126 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
127 * a DC without extra clipping region.
129 HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
131 RECT r;
132 HRGN hClip = 0;
133 HRGN hrgnRet = 0;
135 TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n",
136 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
138 /* desktop window doesn't have a nonclient area */
139 if(wnd == WIN_GetDesktop())
141 wnd->flags &= ~WIN_NEEDS_NCPAINT;
142 if( wnd->hrgnUpdate > 1 )
143 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
144 else
146 hrgnRet = wnd->hrgnUpdate;
148 WIN_ReleaseDesktop();
149 return hrgnRet;
151 WIN_ReleaseDesktop();
153 if ((wnd->hwndSelf == GetForegroundWindow()) &&
154 !(wnd->flags & WIN_NCACTIVATED) )
156 wnd->flags |= WIN_NCACTIVATED;
157 uncFlags |= UNC_ENTIRE;
161 * If the window's non-client area needs to be painted,
163 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
164 !WIN_HaveToDelayNCPAINT(wnd, uncFlags) )
166 RECT r2, r3;
168 wnd->flags &= ~WIN_NEEDS_NCPAINT;
169 GETCLIENTRECTW( wnd, r );
171 TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n",
172 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
173 if( wnd->hrgnUpdate > 1 )
175 /* Check if update rgn overlaps with nonclient area */
177 GetRgnBox( wnd->hrgnUpdate, &r2 );
178 UnionRect( &r3, &r2, &r );
179 if( r3.left != r.left || r3.top != r.top ||
180 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
182 /* crop hrgnUpdate, save old one in hClip - the only
183 * case that places a valid region handle in hClip */
185 hClip = wnd->hrgnUpdate;
186 wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
187 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
190 if( uncFlags & UNC_CHECK )
192 GetRgnBox( wnd->hrgnUpdate, &r3 );
193 if( IsRectEmpty( &r3 ) )
195 /* delete the update region since all invalid
196 * parts were in the nonclient area */
198 DeleteObject( wnd->hrgnUpdate );
199 wnd->hrgnUpdate = 0;
200 if(!(wnd->flags & WIN_INTERNAL_PAINT))
201 QUEUE_DecPaintCount( wnd->hmemTaskQ );
203 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
207 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
209 else
210 if( wnd->hrgnUpdate == 1 )/* entire window */
212 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
213 if( uncFlags & UNC_REGION ) hrgnRet = 1;
214 uncFlags |= UNC_ENTIRE;
217 else /* no WM_NCPAINT unless forced */
219 if( wnd->hrgnUpdate > 1 )
221 copyrgn:
222 if( uncFlags & UNC_REGION )
223 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
225 else
226 if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
228 GETCLIENTRECTW( wnd, r );
229 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
230 if( uncFlags & UNC_REGION ) hrgnRet = 1;
234 if(!hClip && (uncFlags & UNC_ENTIRE) )
236 /* still don't do anything if there is no nonclient area */
237 hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
240 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
242 if ( hClip == hrgnRet && hrgnRet > 1 ) {
243 hClip = CreateRectRgn( 0, 0, 0, 0 );
244 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
247 SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
248 if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
249 DeleteObject( hClip );
251 * Since all Window locks are suspended while processing the WM_NCPAINT
252 * we want to make sure the window still exists before continuing.
254 if (!IsWindow(wnd->hwndSelf))
256 DeleteObject(hrgnRet);
257 hrgnRet=0;
261 TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
263 return hrgnRet;
267 /***********************************************************************
268 * BeginPaint16 (USER.39)
270 HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps )
272 PAINTSTRUCT ps;
274 BeginPaint( hwnd, &ps );
275 lps->hdc = ps.hdc;
276 lps->fErase = ps.fErase;
277 lps->rcPaint.top = ps.rcPaint.top;
278 lps->rcPaint.left = ps.rcPaint.left;
279 lps->rcPaint.right = ps.rcPaint.right;
280 lps->rcPaint.bottom = ps.rcPaint.bottom;
281 lps->fRestore = ps.fRestore;
282 lps->fIncUpdate = ps.fIncUpdate;
283 return lps->hdc;
287 /***********************************************************************
288 * BeginPaint (USER32.10)
290 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
292 BOOL bIcon;
293 HRGN hrgnUpdate;
294 RECT clipRect, clientRect;
295 WND *wndPtr = WIN_FindWndPtr( hwnd );
296 if (!wndPtr) return 0;
298 bIcon = (wndPtr->dwStyle & WS_MINIMIZE && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
300 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
302 /* send WM_NCPAINT and make sure hrgnUpdate is a valid rgn handle */
303 WIN_UpdateNCRgn( wndPtr, 0, UNC_UPDATE | UNC_IN_BEGINPAINT);
306 * Make sure the window is still a window. All window locks are suspended
307 * when the WM_NCPAINT is sent.
309 if (!IsWindow(wndPtr->hwndSelf))
311 WIN_ReleaseWndPtr(wndPtr);
312 return 0;
315 if( ((hrgnUpdate = wndPtr->hrgnUpdate) != 0) || (wndPtr->flags & WIN_INTERNAL_PAINT))
316 QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
318 wndPtr->hrgnUpdate = 0;
319 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
321 HideCaret( hwnd );
323 TRACE("hrgnUpdate = %04x, \n", hrgnUpdate);
325 if (GetClassWord16(wndPtr->hwndSelf, GCW_STYLE) & CS_PARENTDC)
327 /* Don't clip the output to the update region for CS_PARENTDC window */
328 if( hrgnUpdate )
329 DeleteObject(hrgnUpdate);
330 lps->hdc = GetDCEx( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
331 (bIcon ? DCX_WINDOW : 0) );
333 else
335 if( hrgnUpdate ) /* convert to client coordinates */
336 OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
337 wndPtr->rectWindow.top - wndPtr->rectClient.top );
338 lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
339 DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
340 /* ReleaseDC() in EndPaint() will delete the region */
343 TRACE("hdc = %04x\n", lps->hdc);
345 if (!lps->hdc)
347 WARN("GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
348 WIN_ReleaseWndPtr(wndPtr);
349 return 0;
352 /* It is possible that the clip box is bigger than the window itself,
353 if CS_PARENTDC flag is set. Windows never return a paint rect bigger
354 than the window itself, so we need to intersect the cliprect with
355 the window */
357 GetClipBox( lps->hdc, &clipRect );
358 GetClientRect( hwnd, &clientRect );
360 /* The rect obtained by GetClipBox is in logical, so make the client in logical to*/
361 DPtoLP(lps->hdc, (LPPOINT)&clientRect, 2);
363 IntersectRect(&lps->rcPaint, &clientRect, &clipRect);
365 TRACE("box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
366 lps->rcPaint.right, lps->rcPaint.bottom );
368 if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
370 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
371 lps->fErase = !SendMessageA(hwnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
372 (WPARAM16)lps->hdc, 0 );
374 else lps->fErase = TRUE;
376 WIN_ReleaseWndPtr(wndPtr);
377 return lps->hdc;
381 /***********************************************************************
382 * EndPaint16 (USER.40)
384 BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
386 ReleaseDC16( hwnd, lps->hdc );
387 ShowCaret( hwnd );
388 return TRUE;
392 /***********************************************************************
393 * EndPaint (USER32.176)
395 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
397 ReleaseDC( hwnd, lps->hdc );
398 ShowCaret( hwnd );
399 return TRUE;
403 /***********************************************************************
404 * FillWindow (USER.324)
406 void WINAPI FillWindow16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc, HBRUSH16 hbrush )
408 RECT rect;
409 RECT16 rc16;
410 GetClientRect( hwnd, &rect );
411 DPtoLP( hdc, (LPPOINT)&rect, 2 );
412 CONV_RECT32TO16( &rect, &rc16 );
413 PaintRect16( hwndParent, hwnd, hdc, hbrush, &rc16 );
417 /***********************************************************************
418 * PAINT_GetControlBrush
420 static HBRUSH16 PAINT_GetControlBrush( HWND hParent, HWND hWnd, HDC16 hDC, UINT16 ctlType )
422 HBRUSH16 bkgBrush = (HBRUSH16)SendMessageA( hParent, WM_CTLCOLORMSGBOX + ctlType,
423 (WPARAM)hDC, (LPARAM)hWnd );
424 if( !IsGDIObject16(bkgBrush) )
425 bkgBrush = DEFWND_ControlColor( hDC, ctlType );
426 return bkgBrush;
430 /***********************************************************************
431 * PaintRect (USER.325)
433 void WINAPI PaintRect16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc,
434 HBRUSH16 hbrush, const RECT16 *rect)
436 if( hbrush <= CTLCOLOR_MAX )
438 if( hwndParent )
439 hbrush = PAINT_GetControlBrush( hwndParent, hwnd, hdc, (UINT16)hbrush );
440 else
441 return;
443 if( hbrush )
444 FillRect16( hdc, rect, hbrush );
448 /***********************************************************************
449 * GetControlBrush (USER.326)
451 HBRUSH16 WINAPI GetControlBrush16( HWND16 hwnd, HDC16 hdc, UINT16 ctlType )
453 WND* wndPtr = WIN_FindWndPtr( hwnd );
454 HBRUSH16 retvalue;
456 if((ctlType <= CTLCOLOR_MAX) && wndPtr )
458 WND* parent;
459 if( wndPtr->dwStyle & WS_POPUP ) parent = WIN_LockWndPtr(wndPtr->owner);
460 else parent = WIN_LockWndPtr(wndPtr->parent);
461 if( !parent ) parent = wndPtr;
462 retvalue = (HBRUSH16)PAINT_GetControlBrush( parent->hwndSelf, hwnd, hdc, ctlType );
463 WIN_ReleaseWndPtr(parent);
464 goto END;
466 retvalue = (HBRUSH16)0;
467 END:
468 WIN_ReleaseWndPtr(wndPtr);
469 return retvalue;
473 /***********************************************************************
474 * RDW_ValidateParent [RDW_UpdateRgns() helper]
476 * Validate the portions of parents that are covered by a validated child
477 * wndPtr = child
479 void RDW_ValidateParent(WND *wndChild)
481 WND *wndParent = WIN_LockWndPtr(wndChild->parent);
482 WND *wndDesktop = WIN_GetDesktop();
483 HRGN hrg;
485 if (wndChild->hrgnUpdate == 1 ) {
486 RECT r;
487 r.left = 0;
488 r.top = 0;
489 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
490 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
491 hrg = CreateRectRgnIndirect( &r );
492 } else
493 hrg = wndChild->hrgnUpdate;
495 while ((wndParent) && (wndParent != wndDesktop) ) {
496 if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
498 if (wndParent->hrgnUpdate != 0)
500 POINT ptOffset;
501 RECT rect, rectParent;
502 if( wndParent->hrgnUpdate == 1 )
504 RECT r;
506 r.left = 0;
507 r.top = 0;
508 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
509 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
511 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
513 /* we must offset the child region by the offset of the child rect in the parent */
514 GetWindowRect(wndParent->hwndSelf, &rectParent);
515 GetWindowRect(wndChild->hwndSelf, &rect);
516 ptOffset.x = rect.left - rectParent.left;
517 ptOffset.y = rect.top - rectParent.top;
518 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
519 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
520 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
523 WIN_UpdateWndPtr(&wndParent, wndParent->parent);
525 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
526 WIN_ReleaseWndPtr(wndParent);
527 WIN_ReleaseDesktop();
530 /***********************************************************************
531 * RDW_UpdateRgns [RedrawWindow() helper]
533 * Walks the window tree and adds/removes parts of the hRgn to/from update
534 * regions of windows that overlap it. Also, manages internal paint flags.
536 * NOTE: Walks the window tree so the caller must lock it.
537 * MUST preserve hRgn (can modify but then has to restore).
539 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
542 * Called only when one of the following is set:
543 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
546 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
547 BOOL bChildren = ( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE)
548 && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
549 RECT r;
551 r.left = 0;
552 r.top = 0;
553 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
554 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
556 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
558 if( flags & RDW_INVALIDATE )
560 if( hRgn > 1 )
562 switch( wndPtr->hrgnUpdate )
564 default:
565 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
566 /* fall through */
567 case 0:
568 wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate,
569 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
570 &r, NULL );
571 if( !bHadOne )
573 GetRgnBox( wndPtr->hrgnUpdate, &r );
574 if( IsRectEmpty( &r ) )
576 DeleteObject( wndPtr->hrgnUpdate );
577 wndPtr->hrgnUpdate = 0;
578 goto end;
581 break;
582 case 1: /* already an entire window */
583 break;
586 else if( hRgn == 1 )
588 if( wndPtr->hrgnUpdate > 1 )
589 DeleteObject( wndPtr->hrgnUpdate );
590 wndPtr->hrgnUpdate = 1;
592 else
593 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
595 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
596 QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
598 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
599 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
600 flags |= RDW_FRAME;
602 else if( flags & RDW_VALIDATE )
604 if( wndPtr->hrgnUpdate )
606 if( hRgn > 1 )
608 if( wndPtr->hrgnUpdate == 1 )
609 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
611 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
612 == NULLREGION )
613 goto EMPTY;
615 else /* validate everything */
617 if( wndPtr->hrgnUpdate > 1 )
619 EMPTY:
620 DeleteObject( wndPtr->hrgnUpdate );
622 wndPtr->hrgnUpdate = 0;
625 if( !wndPtr->hrgnUpdate )
627 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
628 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
629 QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
633 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
634 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
638 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
639 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
641 /* in/validate child windows that intersect with the region if it
642 * is a valid handle. */
644 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
646 if( hRgn > 1 && bChildren )
648 WND* wnd = wndPtr->child;
649 POINT ptTotal, prevOrigin = {0,0};
650 POINT ptClient;
652 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
653 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
655 for( ptTotal.x = ptTotal.y = 0; wnd; wnd = wnd->next )
657 if( wnd->dwStyle & WS_VISIBLE )
659 POINT ptOffset;
661 r.left = wnd->rectWindow.left + ptClient.x;
662 r.right = wnd->rectWindow.right + ptClient.x;
663 r.top = wnd->rectWindow.top + ptClient.y;
664 r.bottom = wnd->rectWindow.bottom + ptClient.y;
666 ptOffset.x = r.left - prevOrigin.x;
667 ptOffset.y = r.top - prevOrigin.y;
668 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
670 if( RectInRegion( hRgn, &r ) )
672 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
673 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
674 prevOrigin.x = r.left + ptTotal.x;
675 prevOrigin.y = r.top + ptTotal.y;
676 ptTotal.x += ptOffset.x;
677 ptTotal.y += ptOffset.y;
681 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
682 bChildren = 0;
686 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
688 if( bChildren )
690 WND* wnd;
691 for( wnd = wndPtr->child; wnd; wnd = wnd->next )
692 if( wnd->dwStyle & WS_VISIBLE )
693 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
696 end:
698 /* Set/clear internal paint flag */
700 if (flags & RDW_INTERNALPAINT)
702 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
703 QUEUE_IncPaintCount( wndPtr->hmemTaskQ );
704 wndPtr->flags |= WIN_INTERNAL_PAINT;
706 else if (flags & RDW_NOINTERNALPAINT)
708 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
709 QUEUE_DecPaintCount( wndPtr->hmemTaskQ );
710 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
714 /***********************************************************************
715 * RDW_Paint [RedrawWindow() helper]
717 * Walks the window tree and paints/erases windows that have
718 * nonzero update regions according to redraw flags. hrgn is a scratch
719 * region passed down during recursion. Must not be 1.
722 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
724 /* NOTE: wndPtr is locked by caller.
726 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
727 * SendMessage() calls. This is a comment inside DefWindowProc() source
728 * from 16-bit SDK:
730 * This message avoids lots of inter-app message traffic
731 * by switching to the other task and continuing the
732 * recursion there.
734 * wParam = flags
735 * LOWORD(lParam) = hrgnClip
736 * HIWORD(lParam) = hwndSkip (not used; always NULL)
739 HDC hDC;
740 HWND hWnd = wndPtr->hwndSelf;
741 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
743 /* Erase/update the window itself ... */
745 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
748 * Check if this window should delay it's processing of WM_NCPAINT.
749 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
751 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr, 0) )
752 ex |= RDW_EX_DELAY_NCPAINT;
754 if (flags & RDW_UPDATENOW)
756 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
757 SendMessage16( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
759 else if ((flags & RDW_ERASENOW) || (ex & RDW_EX_TOPFRAME))
761 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
762 HRGN hrgnRet;
764 hrgnRet = WIN_UpdateNCRgn(wndPtr,
765 hrgn,
766 UNC_REGION | UNC_CHECK |
767 ((ex & RDW_EX_TOPFRAME) ? UNC_ENTIRE : 0) |
768 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
770 if( hrgnRet )
772 if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
773 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
775 if( bIcon ) dcx |= DCX_WINDOW;
776 else
777 if( hrgnRet )
778 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
779 wndPtr->rectWindow.top - wndPtr->rectClient.top);
780 else
781 dcx &= ~DCX_INTERSECTRGN;
782 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
784 if (SendMessage16( hWnd, (bIcon) ? WM_ICONERASEBKGND
785 : WM_ERASEBKGND, (WPARAM16)hDC, 0 ))
786 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
787 ReleaseDC( hWnd, hDC );
793 if( !IsWindow(hWnd) ) return hrgn;
794 ex &= ~RDW_EX_TOPFRAME;
796 /* ... and its child windows */
798 if( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE)
799 && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
801 WND** list, **ppWnd;
803 if( (list = WIN_BuildWinArray( wndPtr, 0, NULL )) )
805 wndPtr = NULL;
806 for (ppWnd = list; *ppWnd; ppWnd++)
808 WIN_UpdateWndPtr(&wndPtr,*ppWnd);
809 if (!IsWindow(wndPtr->hwndSelf)) continue;
810 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
811 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
812 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
814 WIN_ReleaseWndPtr(wndPtr);
815 WIN_ReleaseWinArray(list);
819 return hrgn;
822 /***********************************************************************
823 * PAINT_RedrawWindow
826 BOOL PAINT_RedrawWindow( HWND hwnd, const RECT *rectUpdate,
827 HRGN hrgnUpdate, UINT flags, UINT ex )
829 HRGN hRgn = 0;
830 RECT r, r2;
831 POINT pt;
832 WND* wndPtr;
834 if (!hwnd) hwnd = GetDesktopWindow();
835 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
837 /* check if the window or its parents are visible/not minimized */
839 if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
841 WIN_ReleaseWndPtr(wndPtr);
842 return TRUE;
845 if (TRACE_ON(win))
847 if( hrgnUpdate )
849 GetRgnBox( hrgnUpdate, &r );
850 TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x, exflags=%04x\n",
851 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags, ex);
853 else
855 if( rectUpdate )
856 r = *rectUpdate;
857 else
858 SetRectEmpty( &r );
859 TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x, exflags=%04x\n",
860 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
861 r.top, r.right, r.bottom, hrgnUpdate, flags, ex );
865 /* prepare an update region in window coordinates */
867 if( flags & RDW_FRAME )
868 r = wndPtr->rectWindow;
869 else
870 r = wndPtr->rectClient;
872 if( ex & RDW_EX_XYWINDOW )
874 pt.x = pt.y = 0;
875 OffsetRect( &r, -wndPtr->rectWindow.left, -wndPtr->rectWindow.top );
877 else
879 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
880 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
881 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
884 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
886 /* If the window doesn't have hrgnUpdate we leave hRgn zero
887 * and put a new region straight into wndPtr->hrgnUpdate
888 * so that RDW_UpdateRgns() won't have to do any extra work.
891 if( hrgnUpdate )
893 if( wndPtr->hrgnUpdate )
894 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
895 else
896 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
898 else if( rectUpdate )
900 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
901 OffsetRect( &r2, pt.x, pt.y );
903 rect2i:
904 if( wndPtr->hrgnUpdate == 0 )
905 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
906 else
907 hRgn = CreateRectRgnIndirect( &r2 );
909 else /* entire window or client depending on RDW_FRAME */
911 if( flags & RDW_FRAME )
913 if( wndPtr->hrgnUpdate )
914 DeleteObject( wndPtr->hrgnUpdate );
915 wndPtr->hrgnUpdate = 1;
917 else
919 GETCLIENTRECTW( wndPtr, r2 );
920 goto rect2i;
924 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
926 /* In this we cannot leave with zero hRgn */
927 if( hrgnUpdate )
929 hRgn = REGION_CropRgn( hRgn, hrgnUpdate, &r, &pt );
930 GetRgnBox( hRgn, &r2 );
931 if( IsRectEmpty( &r2 ) ) goto END;
933 else if( rectUpdate )
935 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
936 OffsetRect( &r2, pt.x, pt.y );
937 rect2v:
938 hRgn = CreateRectRgnIndirect( &r2 );
940 else /* entire window or client depending on RDW_FRAME */
942 if( flags & RDW_FRAME )
943 hRgn = 1;
944 else
946 GETCLIENTRECTW( wndPtr, r2 );
947 goto rect2v;
952 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
954 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
956 /* Erase/update windows, from now on hRgn is a scratch region */
958 hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, ex );
960 END:
961 if( hRgn > 1 && (hRgn != hrgnUpdate) )
962 DeleteObject(hRgn );
963 WIN_ReleaseWndPtr(wndPtr);
964 return TRUE;
968 /***********************************************************************
969 * RedrawWindow (USER32.426)
971 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
972 HRGN hrgnUpdate, UINT flags )
974 return PAINT_RedrawWindow( hwnd, rectUpdate, hrgnUpdate, flags, 0 );
978 /***********************************************************************
979 * RedrawWindow16 (USER.290)
981 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
982 HRGN16 hrgnUpdate, UINT16 flags )
984 if (rectUpdate)
986 RECT r;
987 CONV_RECT16TO32( rectUpdate, &r );
988 return (BOOL16)RedrawWindow( (HWND)hwnd, &r, hrgnUpdate, flags );
990 return (BOOL16)PAINT_RedrawWindow( (HWND)hwnd, NULL,
991 (HRGN)hrgnUpdate, flags, 0 );
995 /***********************************************************************
996 * UpdateWindow16 (USER.124)
998 void WINAPI UpdateWindow16( HWND16 hwnd )
1000 PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN, 0 );
1003 /***********************************************************************
1004 * UpdateWindow (USER32.567)
1006 void WINAPI UpdateWindow( HWND hwnd )
1008 PAINT_RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN, 0 );
1011 /***********************************************************************
1012 * InvalidateRgn16 (USER.126)
1014 void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1016 PAINT_RedrawWindow((HWND)hwnd, NULL, (HRGN)hrgn,
1017 RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
1021 /***********************************************************************
1022 * InvalidateRgn (USER32.329)
1024 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1026 return PAINT_RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
1030 /***********************************************************************
1031 * InvalidateRect16 (USER.125)
1033 void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
1035 RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1039 /***********************************************************************
1040 * InvalidateRect (USER32.328)
1042 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1044 return PAINT_RedrawWindow( hwnd, rect, 0,
1045 RDW_INVALIDATE | (erase ? RDW_ERASE : 0), 0 );
1049 /***********************************************************************
1050 * ValidateRgn16 (USER.128)
1052 void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
1054 PAINT_RedrawWindow( (HWND)hwnd, NULL, (HRGN)hrgn,
1055 RDW_VALIDATE | RDW_NOCHILDREN, 0 );
1059 /***********************************************************************
1060 * ValidateRgn (USER32.572)
1062 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1064 PAINT_RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
1068 /***********************************************************************
1069 * ValidateRect16 (USER.127)
1071 void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
1073 RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1077 /***********************************************************************
1078 * ValidateRect (USER32.571)
1080 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1082 PAINT_RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN, 0 );
1086 /***********************************************************************
1087 * GetUpdateRect16 (USER.190)
1089 BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
1091 RECT r;
1092 BOOL16 ret;
1094 if (!rect) return GetUpdateRect( hwnd, NULL, erase );
1095 ret = GetUpdateRect( hwnd, &r, erase );
1096 CONV_RECT32TO16( &r, rect );
1097 return ret;
1101 /***********************************************************************
1102 * GetUpdateRect (USER32.297)
1104 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1106 BOOL retvalue;
1107 WND * wndPtr = WIN_FindWndPtr( hwnd );
1108 if (!wndPtr) return FALSE;
1110 if (rect)
1112 if (wndPtr->hrgnUpdate > 1)
1114 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
1115 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
1117 retvalue = FALSE;
1118 goto END;
1120 GetRgnBox( hrgn, rect );
1121 DeleteObject( hrgn );
1122 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
1124 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
1126 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
1130 else
1131 if( wndPtr->hrgnUpdate == 1 )
1133 GetClientRect( hwnd, rect );
1134 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
1136 else
1137 SetRectEmpty( rect );
1139 retvalue = (wndPtr->hrgnUpdate >= 1);
1140 END:
1141 WIN_ReleaseWndPtr(wndPtr);
1142 return retvalue;
1146 /***********************************************************************
1147 * GetUpdateRgn16 (USER.237)
1149 INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1151 return GetUpdateRgn( hwnd, hrgn, erase );
1155 /***********************************************************************
1156 * GetUpdateRgn (USER32.298)
1158 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1160 INT retval;
1161 WND * wndPtr = WIN_FindWndPtr( hwnd );
1162 if (!wndPtr) return ERROR;
1164 if (wndPtr->hrgnUpdate == 0)
1166 SetRectRgn( hrgn, 0, 0, 0, 0 );
1167 retval = NULLREGION;
1168 goto END;
1170 else
1171 if (wndPtr->hrgnUpdate == 1)
1173 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1174 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1175 retval = SIMPLEREGION;
1177 else
1179 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1180 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1181 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1183 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1184 END:
1185 WIN_ReleaseWndPtr(wndPtr);
1186 return retval;
1190 /***********************************************************************
1191 * ExcludeUpdateRgn16 (USER.238)
1193 INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
1195 return ExcludeUpdateRgn( hdc, hwnd );
1199 /***********************************************************************
1200 * ExcludeUpdateRgn (USER32.195)
1202 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1204 RECT rect;
1205 WND * wndPtr;
1207 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1209 if (wndPtr->hrgnUpdate)
1211 INT ret;
1212 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1213 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1214 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1215 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1216 if( wndPtr->hrgnUpdate > 1 )
1218 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1219 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1220 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1223 /* do ugly coordinate translations in dce.c */
1225 ret = DCE_ExcludeRgn( hdc, wndPtr, hrgn );
1226 DeleteObject( hrgn );
1227 WIN_ReleaseWndPtr(wndPtr);
1228 return ret;
1230 WIN_ReleaseWndPtr(wndPtr);
1231 return GetClipBox( hdc, &rect );
1236 /***********************************************************************
1237 * FillRect16 (USER.81)
1238 * NOTE
1239 * The Win16 variant doesn't support special color brushes like
1240 * the Win32 one, despite the fact that Win16, as well as Win32,
1241 * supports special background brushes for a window class.
1243 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1245 HBRUSH prevBrush;
1247 /* coordinates are logical so we cannot fast-check 'rect',
1248 * it will be done later in the PatBlt().
1251 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1252 PatBlt( hdc, rect->left, rect->top,
1253 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1254 SelectObject( hdc, prevBrush );
1255 return 1;
1259 /***********************************************************************
1260 * FillRect (USER32.197)
1262 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1264 HBRUSH prevBrush;
1266 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1267 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1270 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1271 PatBlt( hdc, rect->left, rect->top,
1272 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1273 SelectObject( hdc, prevBrush );
1274 return 1;
1278 /***********************************************************************
1279 * InvertRect16 (USER.82)
1281 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1283 PatBlt( hdc, rect->left, rect->top,
1284 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1288 /***********************************************************************
1289 * InvertRect (USER32.330)
1291 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1293 return PatBlt( hdc, rect->left, rect->top,
1294 rect->right - rect->left, rect->bottom - rect->top,
1295 DSTINVERT );
1299 /***********************************************************************
1300 * FrameRect (USER32.203)
1302 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1304 HBRUSH prevBrush;
1305 RECT r = *rect;
1307 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1308 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1310 PatBlt( hdc, r.left, r.top, 1,
1311 r.bottom - r.top, PATCOPY );
1312 PatBlt( hdc, r.right - 1, r.top, 1,
1313 r.bottom - r.top, PATCOPY );
1314 PatBlt( hdc, r.left, r.top,
1315 r.right - r.left, 1, PATCOPY );
1316 PatBlt( hdc, r.left, r.bottom - 1,
1317 r.right - r.left, 1, PATCOPY );
1319 SelectObject( hdc, prevBrush );
1320 return TRUE;
1324 /***********************************************************************
1325 * FrameRect16 (USER.83)
1327 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1329 RECT rect;
1330 CONV_RECT16TO32( rect16, &rect );
1331 return FrameRect( hdc, &rect, hbrush );
1335 /***********************************************************************
1336 * DrawFocusRect16 (USER.466)
1338 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1340 RECT rect32;
1341 CONV_RECT16TO32( rc, &rect32 );
1342 DrawFocusRect( hdc, &rect32 );
1346 /***********************************************************************
1347 * DrawFocusRect (USER32.156)
1349 * FIXME: PatBlt(PATINVERT) with background brush.
1351 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1353 HBRUSH hOldBrush;
1354 HPEN hOldPen, hNewPen;
1355 INT oldDrawMode, oldBkMode;
1357 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1358 hNewPen = CreatePen(PS_DOT, 1, GetSysColor(COLOR_WINDOWTEXT));
1359 hOldPen = SelectObject(hdc, hNewPen);
1360 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1361 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1363 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1365 SetBkMode(hdc, oldBkMode);
1366 SetROP2(hdc, oldDrawMode);
1367 SelectObject(hdc, hOldPen);
1368 DeleteObject(hNewPen);
1369 SelectObject(hdc, hOldBrush);
1371 return TRUE;
1374 /**********************************************************************
1375 * DrawAnimatedRects16 (USER.448)
1377 BOOL16 WINAPI DrawAnimatedRects16( HWND16 hwnd, INT16 idAni,
1378 const RECT16* lprcFrom,
1379 const RECT16* lprcTo )
1381 RECT rcFrom32, rcTo32;
1383 rcFrom32.left = (INT)lprcFrom->left;
1384 rcFrom32.top = (INT)lprcFrom->top;
1385 rcFrom32.right = (INT)lprcFrom->right;
1386 rcFrom32.bottom = (INT)lprcFrom->bottom;
1388 rcTo32.left = (INT)lprcTo->left;
1389 rcTo32.top = (INT)lprcTo->top;
1390 rcTo32.right = (INT)lprcTo->right;
1391 rcTo32.bottom = (INT)lprcTo->bottom;
1393 return DrawAnimatedRects((HWND)hwnd, (INT)idAni, &rcFrom32, &rcTo32);
1397 /**********************************************************************
1398 * DrawAnimatedRects (USER32.153)
1400 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1401 const RECT* lprcFrom,
1402 const RECT* lprcTo )
1404 FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1405 return TRUE;
1409 /**********************************************************************
1410 * PAINTING_DrawStateJam
1412 * Jams in the requested type in the dc
1414 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1415 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1416 LPRECT rc, UINT dtflags,
1417 BOOL unicode, BOOL _32bit)
1419 HDC memdc;
1420 HBITMAP hbmsave;
1421 BOOL retval;
1422 INT cx = rc->right - rc->left;
1423 INT cy = rc->bottom - rc->top;
1425 switch(opcode)
1427 case DST_TEXT:
1428 case DST_PREFIXTEXT:
1429 if(unicode)
1430 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1431 else if(_32bit)
1432 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1433 else
1434 return DrawTextA(hdc, (LPSTR)PTR_SEG_TO_LIN(lp), (INT)wp, rc, dtflags);
1436 case DST_ICON:
1437 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1439 case DST_BITMAP:
1440 memdc = CreateCompatibleDC(hdc);
1441 if(!memdc) return FALSE;
1442 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1443 if(!hbmsave)
1445 DeleteDC(memdc);
1446 return FALSE;
1448 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1449 SelectObject(memdc, hbmsave);
1450 DeleteDC(memdc);
1451 return retval;
1453 case DST_COMPLEX:
1454 if(func) {
1455 BOOL bRet;
1456 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1458 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1459 if(_32bit)
1460 bRet = func(hdc, lp, wp, cx, cy);
1461 else
1462 bRet = (BOOL)((DRAWSTATEPROC16)func)((HDC16)hdc, (LPARAM)lp, (WPARAM16)wp, (INT16)cx, (INT16)cy);
1463 /* Restore origin */
1464 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1465 return bRet;
1466 } else
1467 return FALSE;
1469 return FALSE;
1472 /**********************************************************************
1473 * PAINTING_DrawState()
1475 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr,
1476 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1477 INT x, INT y, INT cx, INT cy,
1478 UINT flags, BOOL unicode, BOOL _32bit)
1480 HBITMAP hbm, hbmsave;
1481 HFONT hfsave;
1482 HBRUSH hbsave, hbrtmp = 0;
1483 HDC memdc;
1484 RECT rc;
1485 UINT dtflags = DT_NOCLIP;
1486 COLORREF fg, bg;
1487 UINT opcode = flags & 0xf;
1488 INT len = wp;
1489 BOOL retval, tmp;
1491 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1493 if(unicode)
1494 len = strlenW((LPWSTR)lp);
1495 else if(_32bit)
1496 len = strlen((LPSTR)lp);
1497 else
1498 len = strlen((LPSTR)PTR_SEG_TO_LIN(lp));
1501 /* Find out what size the image has if not given by caller */
1502 if(!cx || !cy)
1504 SIZE s;
1505 CURSORICONINFO *ici;
1506 BITMAP bm;
1508 switch(opcode)
1510 case DST_TEXT:
1511 case DST_PREFIXTEXT:
1512 if(unicode)
1513 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1514 else if(_32bit)
1515 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1516 else
1517 retval = GetTextExtentPoint32A(hdc, PTR_SEG_TO_LIN(lp), len, &s);
1518 if(!retval) return FALSE;
1519 break;
1521 case DST_ICON:
1522 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1523 if(!ici) return FALSE;
1524 s.cx = ici->nWidth;
1525 s.cy = ici->nHeight;
1526 GlobalUnlock16((HGLOBAL16)lp);
1527 break;
1529 case DST_BITMAP:
1530 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1531 return FALSE;
1532 s.cx = bm.bmWidth;
1533 s.cy = bm.bmHeight;
1534 break;
1536 case DST_COMPLEX: /* cx and cy must be set in this mode */
1537 return FALSE;
1540 if(!cx) cx = s.cx;
1541 if(!cy) cy = s.cy;
1544 rc.left = x;
1545 rc.top = y;
1546 rc.right = x + cx;
1547 rc.bottom = y + cy;
1549 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1550 dtflags |= DT_RIGHT;
1551 if(opcode == DST_TEXT)
1552 dtflags |= DT_NOPREFIX;
1554 /* For DSS_NORMAL we just jam in the image and return */
1555 if((flags & 0x7ff0) == DSS_NORMAL)
1557 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode, _32bit);
1560 /* For all other states we need to convert the image to B/W in a local bitmap */
1561 /* before it is displayed */
1562 fg = SetTextColor(hdc, RGB(0, 0, 0));
1563 bg = SetBkColor(hdc, RGB(255, 255, 255));
1564 hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1565 memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1566 retval = FALSE; /* assume failure */
1568 /* From here on we must use "goto cleanup" when something goes wrong */
1569 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1570 if(!hbm) goto cleanup;
1571 memdc = CreateCompatibleDC(hdc);
1572 if(!memdc) goto cleanup;
1573 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1574 if(!hbmsave) goto cleanup;
1575 rc.left = rc.top = 0;
1576 rc.right = cx;
1577 rc.bottom = cy;
1578 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1579 SetBkColor(memdc, RGB(255, 255, 255));
1580 SetTextColor(memdc, RGB(0, 0, 0));
1581 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1583 /* DST_COMPLEX may draw text as well,
1584 * so we must be sure that correct font is selected
1586 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1587 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode, _32bit);
1588 if(hfsave) SelectObject(memdc, hfsave);
1589 if(!tmp) goto cleanup;
1591 /* This state cause the image to be dithered */
1592 if(flags & DSS_UNION)
1594 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1595 if(!hbsave) goto cleanup;
1596 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1597 SelectObject(memdc, hbsave);
1598 if(!tmp) goto cleanup;
1601 if (flags & DSS_DISABLED)
1602 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1603 else if (flags & DSS_DEFAULT)
1604 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1606 /* Draw light or dark shadow */
1607 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1609 if(!hbrtmp) goto cleanup;
1610 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1611 if(!hbsave) goto cleanup;
1612 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1613 SelectObject(hdc, hbsave);
1614 DeleteObject(hbrtmp);
1615 hbrtmp = 0;
1618 if (flags & DSS_DISABLED)
1620 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1621 if(!hbrtmp) goto cleanup;
1623 else if (!hbr)
1625 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1628 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1630 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1632 retval = TRUE; /* We succeeded */
1634 cleanup:
1635 SetTextColor(hdc, fg);
1636 SetBkColor(hdc, bg);
1638 if(hbsave) SelectObject(hdc, hbsave);
1639 if(hbmsave) SelectObject(memdc, hbmsave);
1640 if(hbrtmp) DeleteObject(hbrtmp);
1641 if(hbm) DeleteObject(hbm);
1642 if(memdc) DeleteDC(memdc);
1644 return retval;
1647 /**********************************************************************
1648 * DrawStateA() (USER32.162)
1650 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1651 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1652 INT x, INT y, INT cx, INT cy, UINT flags)
1654 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE, TRUE);
1657 /**********************************************************************
1658 * DrawStateW() (USER32.163)
1660 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1661 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1662 INT x, INT y, INT cx, INT cy, UINT flags)
1664 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE, TRUE);
1667 /**********************************************************************
1668 * DrawState16() (USER.449)
1670 BOOL16 WINAPI DrawState16(HDC16 hdc, HBRUSH16 hbr,
1671 DRAWSTATEPROC16 func, LPARAM ldata, WPARAM16 wdata,
1672 INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags)
1674 return PAINTING_DrawState(hdc, hbr, (DRAWSTATEPROC)func, ldata, wdata, x, y, cx, cy, flags, FALSE, FALSE);
1678 /***********************************************************************
1679 * SelectPalette16 (USER.282)
1681 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1682 BOOL16 bForceBackground )
1684 return SelectPalette( hDC, hPal, bForceBackground );
1688 /***********************************************************************
1689 * RealizePalette16 (USER.283)
1691 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1693 return RealizePalette( hDC );