Reject pointer messages in SendNotifyMessage[AW] and
[wine/testsucceed.git] / windows / painting.c
blob795fd1faa9afa1ba7b04d08a39ae977f89493b44
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 "server.h"
14 #include "region.h"
15 #include "user.h"
16 #include "win.h"
17 #include "queue.h"
18 #include "dce.h"
19 #include "heap.h"
20 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(win);
23 DECLARE_DEBUG_CHANNEL(nonclient);
25 /* client rect in window coordinates */
27 #define GETCLIENTRECTW( wnd, r ) (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
28 (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
29 (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
30 (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
32 /* PAINT_RedrawWindow() control flags */
33 #define RDW_EX_DELAY_NCPAINT 0x0020
35 /* WIN_UpdateNCRgn() flags */
36 #define UNC_CHECK 0x0001
37 #define UNC_ENTIRE 0x0002
38 #define UNC_REGION 0x0004
39 #define UNC_UPDATE 0x0008
40 #define UNC_DELAY_NCPAINT 0x0010
41 #define UNC_IN_BEGINPAINT 0x0020
43 /* Last COLOR id */
44 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
46 /* Last CTLCOLOR id */
47 #define CTLCOLOR_MAX CTLCOLOR_STATIC
50 /***********************************************************************
51 * add_paint_count
53 * Add an increment (normally 1 or -1) to the current paint count of a window.
55 static void add_paint_count( HWND hwnd, int incr )
57 SERVER_START_REQ( inc_queue_paint_count )
59 req->id = (void *)GetWindowThreadProcessId( hwnd, NULL );
60 req->incr = incr;
61 SERVER_CALL();
63 SERVER_END_REQ;
67 /***********************************************************************
68 * WIN_HaveToDelayNCPAINT
70 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
71 * is generated as soon as a region intersecting the non-client area
72 * of a window is invalidated.
74 * This technique will work fine for all windows whose parents
75 * have the WS_CLIPCHILDREN style. When the parents have that style,
76 * they are not going to override the contents of their children.
77 * However, when the parent doesn't have that style, Windows relies
78 * on a "painter's algorithm" to display the contents of the windows.
79 * That is, windows are painted from back to front. This includes the
80 * non-client area.
82 * This method looks at the current state of a window to determine
83 * if the sending of the WM_NCPAINT message should be delayed until
84 * the BeginPaint call.
86 * PARAMS:
87 * wndPtr - A Locked window pointer to the window we're
88 * examining.
89 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
90 * function. This is a shortcut for the cases when
91 * we already know when to avoid scanning all the
92 * parents of a window. If you already know that this
93 * window's NCPAINT should be delayed, set the
94 * UNC_DELAY_NCPAINT flag for this parameter.
96 * This shortcut behavior is implemented in the
97 * RDW_Paint() method.
100 static BOOL WIN_HaveToDelayNCPAINT(
101 WND* wndPtr,
102 UINT uncFlags)
104 WND* parentWnd = NULL;
107 * Test the shortcut first. (duh)
109 if (uncFlags & UNC_DELAY_NCPAINT)
110 return TRUE;
113 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
114 * method only. This is another shortcut to avoid going
115 * up the parent's chain of the window to finally
116 * figure-out that none of them has an invalid region.
118 if (uncFlags & UNC_IN_BEGINPAINT)
119 return FALSE;
122 * Scan all the parents of this window to find a window
123 * that doesn't have the WS_CLIPCHILDREN style and that
124 * has an invalid region.
126 parentWnd = WIN_LockWndPtr(wndPtr->parent);
128 while (parentWnd!=NULL)
130 if ( ((parentWnd->dwStyle & WS_CLIPCHILDREN) == 0) &&
131 (parentWnd->hrgnUpdate != 0) )
133 WIN_ReleaseWndPtr(parentWnd);
134 return TRUE;
137 WIN_UpdateWndPtr(&parentWnd, parentWnd->parent);
140 WIN_ReleaseWndPtr(parentWnd);
142 return FALSE;
145 /***********************************************************************
146 * WIN_UpdateNCRgn
148 * Things to do:
149 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
150 * Crop hrgnUpdate to a client rect, especially if it 1.
151 * If UNC_REGION is set return update region for the client rect.
153 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
154 * The trick is that when the returned region handle may be different from hRgn.
155 * In this case the old hRgn must be considered gone. BUT, if the returned value
156 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
157 * a DC without extra clipping region.
159 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
161 RECT r;
162 HRGN hClip = 0;
163 HRGN hrgnRet = 0;
165 TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n",
166 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
168 /* desktop window doesn't have a nonclient area */
169 if(wnd == WIN_GetDesktop())
171 wnd->flags &= ~WIN_NEEDS_NCPAINT;
172 if( wnd->hrgnUpdate > 1 )
173 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
174 else
176 hrgnRet = wnd->hrgnUpdate;
178 WIN_ReleaseDesktop();
179 return hrgnRet;
181 WIN_ReleaseDesktop();
183 if ((wnd->hwndSelf == GetForegroundWindow()) &&
184 !(wnd->flags & WIN_NCACTIVATED) )
186 wnd->flags |= WIN_NCACTIVATED;
187 uncFlags |= UNC_ENTIRE;
191 * If the window's non-client area needs to be painted,
193 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
194 !WIN_HaveToDelayNCPAINT(wnd, uncFlags) )
196 RECT r2, r3;
198 wnd->flags &= ~WIN_NEEDS_NCPAINT;
199 GETCLIENTRECTW( wnd, r );
201 TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n",
202 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
203 if( wnd->hrgnUpdate > 1 )
205 /* Check if update rgn overlaps with nonclient area */
207 GetRgnBox( wnd->hrgnUpdate, &r2 );
208 UnionRect( &r3, &r2, &r );
209 if( r3.left != r.left || r3.top != r.top ||
210 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
212 /* crop hrgnUpdate, save old one in hClip - the only
213 * case that places a valid region handle in hClip */
215 hClip = wnd->hrgnUpdate;
216 wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
217 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
220 if( uncFlags & UNC_CHECK )
222 GetRgnBox( wnd->hrgnUpdate, &r3 );
223 if( IsRectEmpty( &r3 ) )
225 /* delete the update region since all invalid
226 * parts were in the nonclient area */
228 DeleteObject( wnd->hrgnUpdate );
229 wnd->hrgnUpdate = 0;
230 if(!(wnd->flags & WIN_INTERNAL_PAINT))
231 add_paint_count( wnd->hwndSelf, -1 );
233 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
237 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
239 else
240 if( wnd->hrgnUpdate == 1 )/* entire window */
242 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
243 if( uncFlags & UNC_REGION ) hrgnRet = 1;
244 uncFlags |= UNC_ENTIRE;
247 else /* no WM_NCPAINT unless forced */
249 if( wnd->hrgnUpdate > 1 )
251 copyrgn:
252 if( uncFlags & UNC_REGION )
253 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
255 else
256 if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
258 GETCLIENTRECTW( wnd, r );
259 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
260 if( uncFlags & UNC_REGION ) hrgnRet = 1;
264 if(!hClip && (uncFlags & UNC_ENTIRE) )
266 /* still don't do anything if there is no nonclient area */
267 hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
270 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
272 if ( hClip == hrgnRet && hrgnRet > 1 ) {
273 hClip = CreateRectRgn( 0, 0, 0, 0 );
274 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
277 SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
278 if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
279 DeleteObject( hClip );
281 * Since all Window locks are suspended while processing the WM_NCPAINT
282 * we want to make sure the window still exists before continuing.
284 if (!IsWindow(wnd->hwndSelf))
286 DeleteObject(hrgnRet);
287 hrgnRet=0;
291 TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
293 return hrgnRet;
297 /***********************************************************************
298 * BeginPaint (USER.39)
300 HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps )
302 PAINTSTRUCT ps;
304 BeginPaint( hwnd, &ps );
305 lps->hdc = ps.hdc;
306 lps->fErase = ps.fErase;
307 lps->rcPaint.top = ps.rcPaint.top;
308 lps->rcPaint.left = ps.rcPaint.left;
309 lps->rcPaint.right = ps.rcPaint.right;
310 lps->rcPaint.bottom = ps.rcPaint.bottom;
311 lps->fRestore = ps.fRestore;
312 lps->fIncUpdate = ps.fIncUpdate;
313 return lps->hdc;
317 /***********************************************************************
318 * BeginPaint (USER32.@)
320 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
322 BOOL bIcon;
323 HRGN hrgnUpdate;
324 RECT clipRect, clientRect;
325 WND *wndPtr = WIN_FindWndPtr( hwnd );
326 if (!wndPtr) return 0;
328 bIcon = (wndPtr->dwStyle & WS_MINIMIZE && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
330 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
332 /* send WM_NCPAINT and make sure hrgnUpdate is a valid rgn handle */
333 WIN_UpdateNCRgn( wndPtr, 0, UNC_UPDATE | UNC_IN_BEGINPAINT);
336 * Make sure the window is still a window. All window locks are suspended
337 * when the WM_NCPAINT is sent.
339 if (!IsWindow(wndPtr->hwndSelf))
341 WIN_ReleaseWndPtr(wndPtr);
342 return 0;
345 if( ((hrgnUpdate = wndPtr->hrgnUpdate) != 0) || (wndPtr->flags & WIN_INTERNAL_PAINT))
346 add_paint_count( hwnd, -1 );
348 wndPtr->hrgnUpdate = 0;
349 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
351 HideCaret( hwnd );
353 TRACE("hrgnUpdate = %04x, \n", hrgnUpdate);
355 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_PARENTDC)
357 /* Don't clip the output to the update region for CS_PARENTDC window */
358 if( hrgnUpdate )
359 DeleteObject(hrgnUpdate);
360 lps->hdc = GetDCEx( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
361 (bIcon ? DCX_WINDOW : 0) );
363 else
365 if( hrgnUpdate ) /* convert to client coordinates */
366 OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
367 wndPtr->rectWindow.top - wndPtr->rectClient.top );
368 lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
369 DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
370 /* ReleaseDC() in EndPaint() will delete the region */
373 TRACE("hdc = %04x\n", lps->hdc);
375 if (!lps->hdc)
377 WARN("GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
378 WIN_ReleaseWndPtr(wndPtr);
379 return 0;
382 /* It is possible that the clip box is bigger than the window itself,
383 if CS_PARENTDC flag is set. Windows never return a paint rect bigger
384 than the window itself, so we need to intersect the cliprect with
385 the window */
387 GetClientRect( hwnd, &clientRect );
389 GetClipBox( lps->hdc, &clipRect );
390 LPtoDP(lps->hdc, (LPPOINT)&clipRect, 2); /* GetClipBox returns LP */
392 IntersectRect(&lps->rcPaint, &clientRect, &clipRect);
393 DPtoLP(lps->hdc, (LPPOINT)&lps->rcPaint, 2); /* we must return LP */
395 TRACE("box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
396 lps->rcPaint.right, lps->rcPaint.bottom );
398 if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
400 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
401 lps->fErase = !SendMessageA(hwnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
402 (WPARAM16)lps->hdc, 0 );
404 else lps->fErase = TRUE;
406 WIN_ReleaseWndPtr(wndPtr);
407 return lps->hdc;
411 /***********************************************************************
412 * EndPaint (USER.40)
414 BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
416 ReleaseDC16( hwnd, lps->hdc );
417 ShowCaret( hwnd );
418 return TRUE;
422 /***********************************************************************
423 * EndPaint (USER32.@)
425 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
427 ReleaseDC( hwnd, lps->hdc );
428 ShowCaret( hwnd );
429 return TRUE;
433 /***********************************************************************
434 * FillWindow (USER.324)
436 void WINAPI FillWindow16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc, HBRUSH16 hbrush )
438 RECT rect;
439 RECT16 rc16;
440 GetClientRect( hwnd, &rect );
441 DPtoLP( hdc, (LPPOINT)&rect, 2 );
442 CONV_RECT32TO16( &rect, &rc16 );
443 PaintRect16( hwndParent, hwnd, hdc, hbrush, &rc16 );
447 /***********************************************************************
448 * PAINT_GetControlBrush
450 static HBRUSH16 PAINT_GetControlBrush( HWND hParent, HWND hWnd, HDC16 hDC, UINT16 ctlType )
452 HBRUSH16 bkgBrush = (HBRUSH16)SendMessageA( hParent, WM_CTLCOLORMSGBOX + ctlType,
453 (WPARAM)hDC, (LPARAM)hWnd );
454 if( !IsGDIObject16(bkgBrush) )
455 bkgBrush = DEFWND_ControlColor( hDC, ctlType );
456 return bkgBrush;
460 /***********************************************************************
461 * PaintRect (USER.325)
463 void WINAPI PaintRect16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc,
464 HBRUSH16 hbrush, const RECT16 *rect)
466 if( hbrush <= CTLCOLOR_MAX )
468 if( hwndParent )
469 hbrush = PAINT_GetControlBrush( hwndParent, hwnd, hdc, (UINT16)hbrush );
470 else
471 return;
473 if( hbrush )
474 FillRect16( hdc, rect, hbrush );
478 /***********************************************************************
479 * GetControlBrush (USER.326)
481 HBRUSH16 WINAPI GetControlBrush16( HWND16 hwnd, HDC16 hdc, UINT16 ctlType )
483 WND* wndPtr = WIN_FindWndPtr( hwnd );
484 HBRUSH16 retvalue;
486 if((ctlType <= CTLCOLOR_MAX) && wndPtr )
488 WND* parent;
489 if( wndPtr->dwStyle & WS_POPUP ) parent = WIN_LockWndPtr(wndPtr->owner);
490 else parent = WIN_LockWndPtr(wndPtr->parent);
491 if( !parent ) parent = wndPtr;
492 retvalue = (HBRUSH16)PAINT_GetControlBrush( parent->hwndSelf, hwnd, hdc, ctlType );
493 WIN_ReleaseWndPtr(parent);
494 goto END;
496 retvalue = (HBRUSH16)0;
497 END:
498 WIN_ReleaseWndPtr(wndPtr);
499 return retvalue;
503 /***********************************************************************
504 * RDW_ValidateParent [RDW_UpdateRgns() helper]
506 * Validate the portions of parents that are covered by a validated child
507 * wndPtr = child
509 static void RDW_ValidateParent(WND *wndChild)
511 WND *wndParent = WIN_LockWndPtr(wndChild->parent);
512 WND *wndDesktop = WIN_GetDesktop();
513 HRGN hrg;
515 if (wndChild->hrgnUpdate == 1 ) {
516 RECT r;
517 r.left = 0;
518 r.top = 0;
519 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
520 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
521 hrg = CreateRectRgnIndirect( &r );
522 } else
523 hrg = wndChild->hrgnUpdate;
525 while ((wndParent) && (wndParent != wndDesktop) ) {
526 if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
528 if (wndParent->hrgnUpdate != 0)
530 POINT ptOffset;
531 RECT rect, rectParent;
532 if( wndParent->hrgnUpdate == 1 )
534 RECT r;
536 r.left = 0;
537 r.top = 0;
538 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
539 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
541 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
543 /* we must offset the child region by the offset of the child rect in the parent */
544 GetWindowRect(wndParent->hwndSelf, &rectParent);
545 GetWindowRect(wndChild->hwndSelf, &rect);
546 ptOffset.x = rect.left - rectParent.left;
547 ptOffset.y = rect.top - rectParent.top;
548 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
549 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
550 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
553 WIN_UpdateWndPtr(&wndParent, wndParent->parent);
555 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
556 WIN_ReleaseWndPtr(wndParent);
557 WIN_ReleaseDesktop();
560 /***********************************************************************
561 * RDW_UpdateRgns [RedrawWindow() helper]
563 * Walks the window tree and adds/removes parts of the hRgn to/from update
564 * regions of windows that overlap it. Also, manages internal paint flags.
566 * NOTE: Walks the window tree so the caller must lock it.
567 * MUST preserve hRgn (can modify but then has to restore).
569 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
572 * Called only when one of the following is set:
573 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
576 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
577 BOOL bChildren = ( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE)
578 && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
579 RECT r;
581 r.left = 0;
582 r.top = 0;
583 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
584 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
586 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
588 if( flags & RDW_INVALIDATE )
590 if( hRgn > 1 )
592 switch( wndPtr->hrgnUpdate )
594 default:
595 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
596 /* fall through */
597 case 0:
598 wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate,
599 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
600 &r, NULL );
601 if( !bHadOne )
603 GetRgnBox( wndPtr->hrgnUpdate, &r );
604 if( IsRectEmpty( &r ) )
606 DeleteObject( wndPtr->hrgnUpdate );
607 wndPtr->hrgnUpdate = 0;
608 goto end;
611 break;
612 case 1: /* already an entire window */
613 break;
616 else if( hRgn == 1 )
618 if( wndPtr->hrgnUpdate > 1 )
619 DeleteObject( wndPtr->hrgnUpdate );
620 wndPtr->hrgnUpdate = 1;
622 else
623 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
625 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
626 add_paint_count( wndPtr->hwndSelf, 1 );
628 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
629 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
630 flags |= RDW_FRAME;
632 else if( flags & RDW_VALIDATE )
634 if( wndPtr->hrgnUpdate )
636 if( hRgn > 1 )
638 if( wndPtr->hrgnUpdate == 1 )
639 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
641 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
642 == NULLREGION )
644 DeleteObject( wndPtr->hrgnUpdate );
645 wndPtr->hrgnUpdate = 0;
648 else /* validate everything */
650 if( wndPtr->hrgnUpdate > 1 ) DeleteObject( wndPtr->hrgnUpdate );
651 wndPtr->hrgnUpdate = 0;
654 if( !wndPtr->hrgnUpdate )
656 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
657 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
658 add_paint_count( wndPtr->hwndSelf, -1 );
662 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
663 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
667 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
668 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
670 /* in/validate child windows that intersect with the region if it
671 * is a valid handle. */
673 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
675 if( hRgn > 1 && bChildren )
677 WND* wnd = wndPtr->child;
678 POINT ptTotal, prevOrigin = {0,0};
679 POINT ptClient;
681 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
682 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
684 for( ptTotal.x = ptTotal.y = 0; wnd; wnd = wnd->next )
686 if( wnd->dwStyle & WS_VISIBLE )
688 POINT ptOffset;
690 r.left = wnd->rectWindow.left + ptClient.x;
691 r.right = wnd->rectWindow.right + ptClient.x;
692 r.top = wnd->rectWindow.top + ptClient.y;
693 r.bottom = wnd->rectWindow.bottom + ptClient.y;
695 ptOffset.x = r.left - prevOrigin.x;
696 ptOffset.y = r.top - prevOrigin.y;
697 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
699 if( RectInRegion( hRgn, &r ) )
701 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
702 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
703 prevOrigin.x = r.left + ptTotal.x;
704 prevOrigin.y = r.top + ptTotal.y;
705 ptTotal.x += ptOffset.x;
706 ptTotal.y += ptOffset.y;
710 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
711 bChildren = 0;
715 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
717 if( bChildren )
719 WND* wnd;
720 for( wnd = wndPtr->child; wnd; wnd = wnd->next )
721 if( wnd->dwStyle & WS_VISIBLE )
722 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
725 end:
727 /* Set/clear internal paint flag */
729 if (flags & RDW_INTERNALPAINT)
731 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
732 add_paint_count( wndPtr->hwndSelf, 1 );
733 wndPtr->flags |= WIN_INTERNAL_PAINT;
735 else if (flags & RDW_NOINTERNALPAINT)
737 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
738 add_paint_count( wndPtr->hwndSelf, -1 );
739 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
743 /***********************************************************************
744 * RDW_Paint [RedrawWindow() helper]
746 * Walks the window tree and paints/erases windows that have
747 * nonzero update regions according to redraw flags. hrgn is a scratch
748 * region passed down during recursion. Must not be 1.
751 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
753 /* NOTE: wndPtr is locked by caller.
755 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
756 * SendMessage() calls. This is a comment inside DefWindowProc() source
757 * from 16-bit SDK:
759 * This message avoids lots of inter-app message traffic
760 * by switching to the other task and continuing the
761 * recursion there.
763 * wParam = flags
764 * LOWORD(lParam) = hrgnClip
765 * HIWORD(lParam) = hwndSkip (not used; always NULL)
768 HDC hDC;
769 HWND hWnd = wndPtr->hwndSelf;
770 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassWord(wndPtr->hwndSelf, GCW_HICON));
772 /* Erase/update the window itself ... */
774 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
777 * Check if this window should delay it's processing of WM_NCPAINT.
778 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
780 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr, 0) )
781 ex |= RDW_EX_DELAY_NCPAINT;
783 if (flags & RDW_UPDATENOW)
785 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
786 SendMessage16( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
788 else if (flags & RDW_ERASENOW)
790 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
791 HRGN hrgnRet;
793 hrgnRet = WIN_UpdateNCRgn(wndPtr,
794 hrgn,
795 UNC_REGION | UNC_CHECK |
796 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
798 if( hrgnRet )
800 if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
801 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
803 if( bIcon ) dcx |= DCX_WINDOW;
804 else
805 if( hrgnRet )
806 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
807 wndPtr->rectWindow.top - wndPtr->rectClient.top);
808 else
809 dcx &= ~DCX_INTERSECTRGN;
810 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
812 if (SendMessage16( hWnd, (bIcon) ? WM_ICONERASEBKGND
813 : WM_ERASEBKGND, (WPARAM16)hDC, 0 ))
814 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
815 ReleaseDC( hWnd, hDC );
821 if( !IsWindow(hWnd) ) return hrgn;
823 /* ... and its child windows */
825 if( wndPtr->child && !(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE)
826 && ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
828 WND** list, **ppWnd;
830 if( (list = WIN_BuildWinArray( wndPtr, 0, NULL )) )
832 wndPtr = NULL;
833 for (ppWnd = list; *ppWnd; ppWnd++)
835 WIN_UpdateWndPtr(&wndPtr,*ppWnd);
836 if (!IsWindow(wndPtr->hwndSelf)) continue;
837 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
838 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
839 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
841 WIN_ReleaseWndPtr(wndPtr);
842 WIN_ReleaseWinArray(list);
846 return hrgn;
850 /***********************************************************************
851 * RedrawWindow (USER32.@)
853 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
854 HRGN hrgnUpdate, UINT flags )
856 HRGN hRgn = 0;
857 RECT r, r2;
858 POINT pt;
859 WND* wndPtr;
861 if (!hwnd) hwnd = GetDesktopWindow();
862 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
864 /* check if the window or its parents are visible/not minimized */
866 if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
868 WIN_ReleaseWndPtr(wndPtr);
869 return TRUE;
872 if (TRACE_ON(win))
874 if( hrgnUpdate )
876 GetRgnBox( hrgnUpdate, &r );
877 TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
878 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
880 else
882 if( rectUpdate )
883 r = *rectUpdate;
884 else
885 SetRectEmpty( &r );
886 TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
887 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
888 r.top, r.right, r.bottom, hrgnUpdate, flags );
892 /* prepare an update region in window coordinates */
894 if( flags & RDW_FRAME )
895 r = wndPtr->rectWindow;
896 else
897 r = wndPtr->rectClient;
899 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
900 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
901 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
903 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
905 /* If the window doesn't have hrgnUpdate we leave hRgn zero
906 * and put a new region straight into wndPtr->hrgnUpdate
907 * so that RDW_UpdateRgns() won't have to do any extra work.
910 if( hrgnUpdate )
912 if( wndPtr->hrgnUpdate )
913 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
914 else
915 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
917 else if( rectUpdate )
919 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
920 OffsetRect( &r2, pt.x, pt.y );
922 rect2i:
923 if( wndPtr->hrgnUpdate == 0 )
924 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
925 else
926 hRgn = CreateRectRgnIndirect( &r2 );
928 else /* entire window or client depending on RDW_FRAME */
930 if( flags & RDW_FRAME )
932 if( wndPtr->hrgnUpdate )
933 DeleteObject( wndPtr->hrgnUpdate );
934 wndPtr->hrgnUpdate = 1;
936 else
938 GETCLIENTRECTW( wndPtr, r2 );
939 goto rect2i;
943 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
945 /* In this we cannot leave with zero hRgn */
946 if( hrgnUpdate )
948 hRgn = REGION_CropRgn( hRgn, hrgnUpdate, &r, &pt );
949 GetRgnBox( hRgn, &r2 );
950 if( IsRectEmpty( &r2 ) ) goto END;
952 else if( rectUpdate )
954 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
955 OffsetRect( &r2, pt.x, pt.y );
956 hRgn = CreateRectRgnIndirect( &r2 );
958 else /* entire window or client depending on RDW_FRAME */
960 if( flags & RDW_FRAME )
961 hRgn = 1;
962 else
964 GETCLIENTRECTW( wndPtr, r2 );
965 hRgn = CreateRectRgnIndirect( &r2 );
970 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
972 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
974 /* Erase/update windows, from now on hRgn is a scratch region */
976 hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
978 END:
979 if( hRgn > 1 && (hRgn != hrgnUpdate) )
980 DeleteObject(hRgn );
981 WIN_ReleaseWndPtr(wndPtr);
982 return TRUE;
986 /***********************************************************************
987 * RedrawWindow (USER.290)
989 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
990 HRGN16 hrgnUpdate, UINT16 flags )
992 if (rectUpdate)
994 RECT r;
995 CONV_RECT16TO32( rectUpdate, &r );
996 return (BOOL16)RedrawWindow( (HWND)hwnd, &r, hrgnUpdate, flags );
998 return RedrawWindow( hwnd, NULL, hrgnUpdate, flags );
1002 /***********************************************************************
1003 * UpdateWindow (USER.124)
1005 void WINAPI UpdateWindow16( HWND16 hwnd )
1007 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1010 /***********************************************************************
1011 * UpdateWindow (USER32.@)
1013 void WINAPI UpdateWindow( HWND hwnd )
1015 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1018 /***********************************************************************
1019 * InvalidateRgn (USER.126)
1021 void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1023 RedrawWindow((HWND)hwnd, NULL, (HRGN)hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1027 /***********************************************************************
1028 * InvalidateRgn (USER32.@)
1030 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1032 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1036 /***********************************************************************
1037 * InvalidateRect (USER.125)
1039 void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
1041 RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1045 /***********************************************************************
1046 * InvalidateRect (USER32.@)
1048 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1050 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1054 /***********************************************************************
1055 * ValidateRgn (USER.128)
1057 void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
1059 RedrawWindow( (HWND)hwnd, NULL, (HRGN)hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
1063 /***********************************************************************
1064 * ValidateRgn (USER32.@)
1066 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1068 RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
1072 /***********************************************************************
1073 * ValidateRect (USER.127)
1075 void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
1077 RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1081 /***********************************************************************
1082 * ValidateRect (USER32.@)
1084 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1086 RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1090 /***********************************************************************
1091 * GetUpdateRect (USER.190)
1093 BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
1095 RECT r;
1096 BOOL16 ret;
1098 if (!rect) return GetUpdateRect( hwnd, NULL, erase );
1099 ret = GetUpdateRect( hwnd, &r, erase );
1100 CONV_RECT32TO16( &r, rect );
1101 return ret;
1105 /***********************************************************************
1106 * GetUpdateRect (USER32.@)
1108 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1110 BOOL retvalue;
1111 WND * wndPtr = WIN_FindWndPtr( hwnd );
1112 if (!wndPtr) return FALSE;
1114 if (rect)
1116 if (wndPtr->hrgnUpdate > 1)
1118 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
1119 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
1121 retvalue = FALSE;
1122 goto END;
1124 GetRgnBox( hrgn, rect );
1125 DeleteObject( hrgn );
1126 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
1128 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
1130 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
1134 else
1135 if( wndPtr->hrgnUpdate == 1 )
1137 GetClientRect( hwnd, rect );
1138 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
1140 else
1141 SetRectEmpty( rect );
1143 retvalue = (wndPtr->hrgnUpdate >= 1);
1144 END:
1145 WIN_ReleaseWndPtr(wndPtr);
1146 return retvalue;
1150 /***********************************************************************
1151 * GetUpdateRgn (USER.237)
1153 INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1155 return GetUpdateRgn( hwnd, hrgn, erase );
1159 /***********************************************************************
1160 * GetUpdateRgn (USER32.@)
1162 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1164 INT retval;
1165 WND * wndPtr = WIN_FindWndPtr( hwnd );
1166 if (!wndPtr) return ERROR;
1168 if (wndPtr->hrgnUpdate == 0)
1170 SetRectRgn( hrgn, 0, 0, 0, 0 );
1171 retval = NULLREGION;
1172 goto END;
1174 else
1175 if (wndPtr->hrgnUpdate == 1)
1177 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1178 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1179 retval = SIMPLEREGION;
1181 else
1183 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1184 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1185 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1187 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1188 END:
1189 WIN_ReleaseWndPtr(wndPtr);
1190 return retval;
1194 /***********************************************************************
1195 * ExcludeUpdateRgn (USER.238)
1197 INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
1199 return ExcludeUpdateRgn( hdc, hwnd );
1203 /***********************************************************************
1204 * ExcludeUpdateRgn (USER32.@)
1206 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1208 RECT rect;
1209 WND * wndPtr;
1211 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1213 if (wndPtr->hrgnUpdate)
1215 INT ret;
1216 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1217 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1218 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1219 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1220 if( wndPtr->hrgnUpdate > 1 )
1222 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1223 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1224 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1227 /* do ugly coordinate translations in dce.c */
1229 ret = DCE_ExcludeRgn( hdc, wndPtr, hrgn );
1230 DeleteObject( hrgn );
1231 WIN_ReleaseWndPtr(wndPtr);
1232 return ret;
1234 WIN_ReleaseWndPtr(wndPtr);
1235 return GetClipBox( hdc, &rect );
1240 /***********************************************************************
1241 * FillRect (USER.81)
1242 * NOTE
1243 * The Win16 variant doesn't support special color brushes like
1244 * the Win32 one, despite the fact that Win16, as well as Win32,
1245 * supports special background brushes for a window class.
1247 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1249 HBRUSH prevBrush;
1251 /* coordinates are logical so we cannot fast-check 'rect',
1252 * it will be done later in the PatBlt().
1255 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1256 PatBlt( hdc, rect->left, rect->top,
1257 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1258 SelectObject( hdc, prevBrush );
1259 return 1;
1263 /***********************************************************************
1264 * FillRect (USER32.@)
1266 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1268 HBRUSH prevBrush;
1270 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1271 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1274 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1275 PatBlt( hdc, rect->left, rect->top,
1276 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1277 SelectObject( hdc, prevBrush );
1278 return 1;
1282 /***********************************************************************
1283 * InvertRect (USER.82)
1285 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1287 PatBlt( hdc, rect->left, rect->top,
1288 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1292 /***********************************************************************
1293 * InvertRect (USER32.@)
1295 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1297 return PatBlt( hdc, rect->left, rect->top,
1298 rect->right - rect->left, rect->bottom - rect->top,
1299 DSTINVERT );
1303 /***********************************************************************
1304 * FrameRect (USER32.@)
1306 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1308 HBRUSH prevBrush;
1309 RECT r = *rect;
1311 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1312 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1314 PatBlt( hdc, r.left, r.top, 1,
1315 r.bottom - r.top, PATCOPY );
1316 PatBlt( hdc, r.right - 1, r.top, 1,
1317 r.bottom - r.top, PATCOPY );
1318 PatBlt( hdc, r.left, r.top,
1319 r.right - r.left, 1, PATCOPY );
1320 PatBlt( hdc, r.left, r.bottom - 1,
1321 r.right - r.left, 1, PATCOPY );
1323 SelectObject( hdc, prevBrush );
1324 return TRUE;
1328 /***********************************************************************
1329 * FrameRect (USER.83)
1331 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1333 RECT rect;
1334 CONV_RECT16TO32( rect16, &rect );
1335 return FrameRect( hdc, &rect, hbrush );
1339 /***********************************************************************
1340 * DrawFocusRect (USER.466)
1342 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1344 RECT rect32;
1345 CONV_RECT16TO32( rc, &rect32 );
1346 DrawFocusRect( hdc, &rect32 );
1350 /***********************************************************************
1351 * DrawFocusRect (USER32.@)
1353 * FIXME: PatBlt(PATINVERT) with background brush.
1355 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1357 HBRUSH hOldBrush;
1358 HPEN hOldPen, hNewPen;
1359 INT oldDrawMode, oldBkMode;
1361 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1362 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1363 hOldPen = SelectObject(hdc, hNewPen);
1364 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1365 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1367 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1369 SetBkMode(hdc, oldBkMode);
1370 SetROP2(hdc, oldDrawMode);
1371 SelectObject(hdc, hOldPen);
1372 DeleteObject(hNewPen);
1373 SelectObject(hdc, hOldBrush);
1375 return TRUE;
1378 /**********************************************************************
1379 * DrawAnimatedRects (USER.448)
1381 BOOL16 WINAPI DrawAnimatedRects16( HWND16 hwnd, INT16 idAni,
1382 const RECT16* lprcFrom,
1383 const RECT16* lprcTo )
1385 RECT rcFrom32, rcTo32;
1387 rcFrom32.left = (INT)lprcFrom->left;
1388 rcFrom32.top = (INT)lprcFrom->top;
1389 rcFrom32.right = (INT)lprcFrom->right;
1390 rcFrom32.bottom = (INT)lprcFrom->bottom;
1392 rcTo32.left = (INT)lprcTo->left;
1393 rcTo32.top = (INT)lprcTo->top;
1394 rcTo32.right = (INT)lprcTo->right;
1395 rcTo32.bottom = (INT)lprcTo->bottom;
1397 return DrawAnimatedRects((HWND)hwnd, (INT)idAni, &rcFrom32, &rcTo32);
1401 /**********************************************************************
1402 * DrawAnimatedRects (USER32.@)
1404 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1405 const RECT* lprcFrom,
1406 const RECT* lprcTo )
1408 FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1409 return TRUE;
1413 /**********************************************************************
1414 * PAINTING_DrawStateJam
1416 * Jams in the requested type in the dc
1418 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1419 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1420 LPRECT rc, UINT dtflags,
1421 BOOL unicode, BOOL _32bit)
1423 HDC memdc;
1424 HBITMAP hbmsave;
1425 BOOL retval;
1426 INT cx = rc->right - rc->left;
1427 INT cy = rc->bottom - rc->top;
1429 switch(opcode)
1431 case DST_TEXT:
1432 case DST_PREFIXTEXT:
1433 if(unicode)
1434 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1435 else if(_32bit)
1436 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1437 else
1438 return DrawTextA(hdc, MapSL(lp), (INT)wp, rc, dtflags);
1440 case DST_ICON:
1441 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1443 case DST_BITMAP:
1444 memdc = CreateCompatibleDC(hdc);
1445 if(!memdc) return FALSE;
1446 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1447 if(!hbmsave)
1449 DeleteDC(memdc);
1450 return FALSE;
1452 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1453 SelectObject(memdc, hbmsave);
1454 DeleteDC(memdc);
1455 return retval;
1457 case DST_COMPLEX:
1458 if(func) {
1459 BOOL bRet;
1460 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1462 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1463 if(_32bit)
1464 bRet = func(hdc, lp, wp, cx, cy);
1465 else
1466 bRet = (BOOL)((DRAWSTATEPROC16)func)((HDC16)hdc, (LPARAM)lp, (WPARAM16)wp, (INT16)cx, (INT16)cy);
1467 /* Restore origin */
1468 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1469 return bRet;
1470 } else
1471 return FALSE;
1473 return FALSE;
1476 /**********************************************************************
1477 * PAINTING_DrawState()
1479 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr,
1480 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1481 INT x, INT y, INT cx, INT cy,
1482 UINT flags, BOOL unicode, BOOL _32bit)
1484 HBITMAP hbm, hbmsave;
1485 HFONT hfsave;
1486 HBRUSH hbsave, hbrtmp = 0;
1487 HDC memdc;
1488 RECT rc;
1489 UINT dtflags = DT_NOCLIP;
1490 COLORREF fg, bg;
1491 UINT opcode = flags & 0xf;
1492 INT len = wp;
1493 BOOL retval, tmp;
1495 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1497 if(unicode)
1498 len = strlenW((LPWSTR)lp);
1499 else if(_32bit)
1500 len = strlen((LPSTR)lp);
1501 else
1502 len = strlen(MapSL(lp));
1505 /* Find out what size the image has if not given by caller */
1506 if(!cx || !cy)
1508 SIZE s;
1509 CURSORICONINFO *ici;
1510 BITMAP bm;
1512 switch(opcode)
1514 case DST_TEXT:
1515 case DST_PREFIXTEXT:
1516 if(unicode)
1517 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1518 else if(_32bit)
1519 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1520 else
1521 retval = GetTextExtentPoint32A(hdc, MapSL(lp), len, &s);
1522 if(!retval) return FALSE;
1523 break;
1525 case DST_ICON:
1526 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1527 if(!ici) return FALSE;
1528 s.cx = ici->nWidth;
1529 s.cy = ici->nHeight;
1530 GlobalUnlock16((HGLOBAL16)lp);
1531 break;
1533 case DST_BITMAP:
1534 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1535 return FALSE;
1536 s.cx = bm.bmWidth;
1537 s.cy = bm.bmHeight;
1538 break;
1540 case DST_COMPLEX: /* cx and cy must be set in this mode */
1541 return FALSE;
1544 if(!cx) cx = s.cx;
1545 if(!cy) cy = s.cy;
1548 rc.left = x;
1549 rc.top = y;
1550 rc.right = x + cx;
1551 rc.bottom = y + cy;
1553 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1554 dtflags |= DT_RIGHT;
1555 if(opcode == DST_TEXT)
1556 dtflags |= DT_NOPREFIX;
1558 /* For DSS_NORMAL we just jam in the image and return */
1559 if((flags & 0x7ff0) == DSS_NORMAL)
1561 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode, _32bit);
1564 /* For all other states we need to convert the image to B/W in a local bitmap */
1565 /* before it is displayed */
1566 fg = SetTextColor(hdc, RGB(0, 0, 0));
1567 bg = SetBkColor(hdc, RGB(255, 255, 255));
1568 hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1569 memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1570 retval = FALSE; /* assume failure */
1572 /* From here on we must use "goto cleanup" when something goes wrong */
1573 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1574 if(!hbm) goto cleanup;
1575 memdc = CreateCompatibleDC(hdc);
1576 if(!memdc) goto cleanup;
1577 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1578 if(!hbmsave) goto cleanup;
1579 rc.left = rc.top = 0;
1580 rc.right = cx;
1581 rc.bottom = cy;
1582 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1583 SetBkColor(memdc, RGB(255, 255, 255));
1584 SetTextColor(memdc, RGB(0, 0, 0));
1585 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1587 /* DST_COMPLEX may draw text as well,
1588 * so we must be sure that correct font is selected
1590 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1591 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode, _32bit);
1592 if(hfsave) SelectObject(memdc, hfsave);
1593 if(!tmp) goto cleanup;
1595 /* This state cause the image to be dithered */
1596 if(flags & DSS_UNION)
1598 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1599 if(!hbsave) goto cleanup;
1600 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1601 SelectObject(memdc, hbsave);
1602 if(!tmp) goto cleanup;
1605 if (flags & DSS_DISABLED)
1606 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1607 else if (flags & DSS_DEFAULT)
1608 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1610 /* Draw light or dark shadow */
1611 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1613 if(!hbrtmp) goto cleanup;
1614 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1615 if(!hbsave) goto cleanup;
1616 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1617 SelectObject(hdc, hbsave);
1618 DeleteObject(hbrtmp);
1619 hbrtmp = 0;
1622 if (flags & DSS_DISABLED)
1624 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1625 if(!hbrtmp) goto cleanup;
1627 else if (!hbr)
1629 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1632 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1634 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1636 retval = TRUE; /* We succeeded */
1638 cleanup:
1639 SetTextColor(hdc, fg);
1640 SetBkColor(hdc, bg);
1642 if(hbsave) SelectObject(hdc, hbsave);
1643 if(hbmsave) SelectObject(memdc, hbmsave);
1644 if(hbrtmp) DeleteObject(hbrtmp);
1645 if(hbm) DeleteObject(hbm);
1646 if(memdc) DeleteDC(memdc);
1648 return retval;
1651 /**********************************************************************
1652 * DrawStateA (USER32.@)
1654 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1655 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1656 INT x, INT y, INT cx, INT cy, UINT flags)
1658 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE, TRUE);
1661 /**********************************************************************
1662 * DrawStateW (USER32.@)
1664 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1665 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1666 INT x, INT y, INT cx, INT cy, UINT flags)
1668 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE, TRUE);
1671 /**********************************************************************
1672 * DrawState (USER.449)
1674 BOOL16 WINAPI DrawState16(HDC16 hdc, HBRUSH16 hbr,
1675 DRAWSTATEPROC16 func, LPARAM ldata, WPARAM16 wdata,
1676 INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags)
1678 return PAINTING_DrawState(hdc, hbr, (DRAWSTATEPROC)func, ldata, wdata, x, y, cx, cy, flags, FALSE, FALSE);
1682 /***********************************************************************
1683 * SelectPalette (USER.282)
1685 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1686 BOOL16 bForceBackground )
1688 WORD wBkgPalette = 1;
1690 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1692 HWND hwnd = WindowFromDC( hDC );
1693 if (hwnd)
1695 HWND hForeground = GetForegroundWindow();
1696 /* set primary palette if it's related to current active */
1697 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1700 return GDISelectPalette16( hDC, hPal, wBkgPalette);
1704 /***********************************************************************
1705 * RealizePalette (USER.283)
1707 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1709 UINT16 realized = GDIRealizePalette16( hDC );
1711 /* do not send anything if no colors were changed */
1712 if (realized && IsDCCurrentPalette16( hDC ))
1714 /* send palette change notification */
1715 HWND hWnd = WindowFromDC( hDC );
1716 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
1718 return realized;
1722 /***********************************************************************
1723 * UserRealizePalette (USER32.@)
1725 UINT WINAPI UserRealizePalette( HDC hDC )
1727 return RealizePalette16( hDC );