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