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( HWND hwnd, UINT uncFlags)
102 * Test the shortcut first. (duh)
104 if (uncFlags & UNC_DELAY_NCPAINT)
108 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
109 * method only. This is another shortcut to avoid going
110 * up the parent's chain of the window to finally
111 * figure-out that none of them has an invalid region.
113 if (uncFlags & UNC_IN_BEGINPAINT)
117 * Scan all the parents of this window to find a window
118 * that doesn't have the WS_CLIPCHILDREN style and that
119 * has an invalid region.
121 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
123 WND* parentWnd = WIN_FindWndPtr( hwnd );
124 if (!(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
126 WIN_ReleaseWndPtr( parentWnd );
129 WIN_ReleaseWndPtr( parentWnd );
134 /***********************************************************************
138 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
139 * Crop hrgnUpdate to a client rect, especially if it 1.
140 * If UNC_REGION is set return update region for the client rect.
142 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
143 * The trick is that when the returned region handle may be different from hRgn.
144 * In this case the old hRgn must be considered gone. BUT, if the returned value
145 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
146 * a DC without extra clipping region.
148 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
154 TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n",
155 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
157 /* desktop window doesn't have a nonclient area */
158 if(wnd->hwndSelf == GetDesktopWindow())
160 wnd->flags &= ~WIN_NEEDS_NCPAINT;
161 if( wnd->hrgnUpdate > 1 )
162 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
165 hrgnRet = wnd->hrgnUpdate;
170 if ((wnd->hwndSelf == GetForegroundWindow()) &&
171 !(wnd->flags & WIN_NCACTIVATED) )
173 wnd->flags |= WIN_NCACTIVATED;
174 uncFlags |= UNC_ENTIRE;
178 * If the window's non-client area needs to be painted,
180 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
181 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
185 wnd->flags &= ~WIN_NEEDS_NCPAINT;
186 GETCLIENTRECTW( wnd, r );
188 TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n",
189 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
190 if( wnd->hrgnUpdate > 1 )
192 /* Check if update rgn overlaps with nonclient area */
194 GetRgnBox( wnd->hrgnUpdate, &r2 );
195 UnionRect( &r3, &r2, &r );
196 if( r3.left != r.left || r3.top != r.top ||
197 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
199 /* crop hrgnUpdate, save old one in hClip - the only
200 * case that places a valid region handle in hClip */
202 hClip = wnd->hrgnUpdate;
203 wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
204 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
207 if( uncFlags & UNC_CHECK )
209 GetRgnBox( wnd->hrgnUpdate, &r3 );
210 if( IsRectEmpty( &r3 ) )
212 /* delete the update region since all invalid
213 * parts were in the nonclient area */
215 DeleteObject( wnd->hrgnUpdate );
217 if(!(wnd->flags & WIN_INTERNAL_PAINT))
218 add_paint_count( wnd->hwndSelf, -1 );
220 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
224 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
227 if( wnd->hrgnUpdate == 1 )/* entire window */
229 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
230 if( uncFlags & UNC_REGION ) hrgnRet = 1;
231 uncFlags |= UNC_ENTIRE;
234 else /* no WM_NCPAINT unless forced */
236 if( wnd->hrgnUpdate > 1 )
239 if( uncFlags & UNC_REGION )
240 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
243 if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
245 GETCLIENTRECTW( wnd, r );
246 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
247 if( uncFlags & UNC_REGION ) hrgnRet = 1;
251 if(!hClip && (uncFlags & UNC_ENTIRE) )
253 /* still don't do anything if there is no nonclient area */
254 hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
257 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
259 if ( hClip == hrgnRet && hrgnRet > 1 ) {
260 hClip = CreateRectRgn( 0, 0, 0, 0 );
261 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
264 SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
265 if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
266 DeleteObject( hClip );
268 * Since all Window locks are suspended while processing the WM_NCPAINT
269 * we want to make sure the window still exists before continuing.
271 if (!IsWindow(wnd->hwndSelf))
273 DeleteObject(hrgnRet);
278 TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
284 /***********************************************************************
285 * BeginPaint (USER.39)
287 HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps )
291 BeginPaint( hwnd, &ps );
293 lps->fErase = ps.fErase;
294 lps->rcPaint.top = ps.rcPaint.top;
295 lps->rcPaint.left = ps.rcPaint.left;
296 lps->rcPaint.right = ps.rcPaint.right;
297 lps->rcPaint.bottom = ps.rcPaint.bottom;
298 lps->fRestore = ps.fRestore;
299 lps->fIncUpdate = ps.fIncUpdate;
304 /***********************************************************************
305 * BeginPaint (USER32.@)
307 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
311 RECT clipRect, clientRect;
312 WND *wndPtr = WIN_FindWndPtr( hwnd );
313 if (!wndPtr) return 0;
315 bIcon = (wndPtr->dwStyle & WS_MINIMIZE && GetClassLongA(hwnd, GCL_HICON));
317 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
319 /* send WM_NCPAINT and make sure hrgnUpdate is a valid rgn handle */
320 WIN_UpdateNCRgn( wndPtr, 0, UNC_UPDATE | UNC_IN_BEGINPAINT);
323 * Make sure the window is still a window. All window locks are suspended
324 * when the WM_NCPAINT is sent.
326 if (!IsWindow(wndPtr->hwndSelf))
328 WIN_ReleaseWndPtr(wndPtr);
332 if( ((hrgnUpdate = wndPtr->hrgnUpdate) != 0) || (wndPtr->flags & WIN_INTERNAL_PAINT))
333 add_paint_count( hwnd, -1 );
335 wndPtr->hrgnUpdate = 0;
336 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
340 TRACE("hrgnUpdate = %04x, \n", hrgnUpdate);
342 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_PARENTDC)
344 /* Don't clip the output to the update region for CS_PARENTDC window */
346 DeleteObject(hrgnUpdate);
347 lps->hdc = GetDCEx( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
348 (bIcon ? DCX_WINDOW : 0) );
352 if( hrgnUpdate ) /* convert to client coordinates */
353 OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
354 wndPtr->rectWindow.top - wndPtr->rectClient.top );
355 lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
356 DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
357 /* ReleaseDC() in EndPaint() will delete the region */
360 TRACE("hdc = %04x\n", lps->hdc);
364 WARN("GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
365 WIN_ReleaseWndPtr(wndPtr);
369 /* It is possible that the clip box is bigger than the window itself,
370 if CS_PARENTDC flag is set. Windows never return a paint rect bigger
371 than the window itself, so we need to intersect the cliprect with
374 GetClientRect( hwnd, &clientRect );
376 GetClipBox( lps->hdc, &clipRect );
377 LPtoDP(lps->hdc, (LPPOINT)&clipRect, 2); /* GetClipBox returns LP */
379 IntersectRect(&lps->rcPaint, &clientRect, &clipRect);
380 DPtoLP(lps->hdc, (LPPOINT)&lps->rcPaint, 2); /* we must return LP */
382 TRACE("box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
383 lps->rcPaint.right, lps->rcPaint.bottom );
385 if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
387 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
388 lps->fErase = !SendMessageA(hwnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
389 (WPARAM16)lps->hdc, 0 );
391 else lps->fErase = TRUE;
393 WIN_ReleaseWndPtr(wndPtr);
398 /***********************************************************************
401 BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
403 ReleaseDC16( hwnd, lps->hdc );
409 /***********************************************************************
410 * EndPaint (USER32.@)
412 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
414 ReleaseDC( hwnd, lps->hdc );
420 /***********************************************************************
421 * FillWindow (USER.324)
423 void WINAPI FillWindow16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc, HBRUSH16 hbrush )
427 GetClientRect( hwnd, &rect );
428 DPtoLP( hdc, (LPPOINT)&rect, 2 );
429 CONV_RECT32TO16( &rect, &rc16 );
430 PaintRect16( hwndParent, hwnd, hdc, hbrush, &rc16 );
434 /***********************************************************************
435 * PAINT_GetControlBrush
437 static HBRUSH16 PAINT_GetControlBrush( HWND hParent, HWND hWnd, HDC16 hDC, UINT16 ctlType )
439 HBRUSH16 bkgBrush = (HBRUSH16)SendMessageA( hParent, WM_CTLCOLORMSGBOX + ctlType,
440 (WPARAM)hDC, (LPARAM)hWnd );
441 if( !IsGDIObject16(bkgBrush) )
442 bkgBrush = DEFWND_ControlColor( hDC, ctlType );
447 /***********************************************************************
448 * PaintRect (USER.325)
450 void WINAPI PaintRect16( HWND16 hwndParent, HWND16 hwnd, HDC16 hdc,
451 HBRUSH16 hbrush, const RECT16 *rect)
453 if( hbrush <= CTLCOLOR_MAX )
456 hbrush = PAINT_GetControlBrush( hwndParent, hwnd, hdc, (UINT16)hbrush );
461 FillRect16( hdc, rect, hbrush );
465 /***********************************************************************
466 * GetControlBrush (USER.326)
468 HBRUSH16 WINAPI GetControlBrush16( HWND16 hwnd, HDC16 hdc, UINT16 ctlType )
470 if (ctlType <= CTLCOLOR_MAX)
472 HWND16 parent = GetParent16( hwnd );
473 if (!parent) parent = hwnd;
474 return PAINT_GetControlBrush( parent, hwnd, hdc, ctlType );
480 /***********************************************************************
481 * RDW_ValidateParent [RDW_UpdateRgns() helper]
483 * Validate the portions of parents that are covered by a validated child
486 static void RDW_ValidateParent(WND *wndChild)
491 if (wndChild->hrgnUpdate == 1 ) {
495 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
496 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
497 hrg = CreateRectRgnIndirect( &r );
499 hrg = wndChild->hrgnUpdate;
501 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
502 while (parent && parent != GetDesktopWindow())
504 WND *wndParent = WIN_FindWndPtr( parent );
505 if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
507 if (wndParent->hrgnUpdate != 0)
510 RECT rect, rectParent;
511 if( wndParent->hrgnUpdate == 1 )
517 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
518 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
520 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
522 /* we must offset the child region by the offset of the child rect in the parent */
523 GetWindowRect(wndParent->hwndSelf, &rectParent);
524 GetWindowRect(wndChild->hwndSelf, &rect);
525 ptOffset.x = rect.left - rectParent.left;
526 ptOffset.y = rect.top - rectParent.top;
527 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
528 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
529 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
532 WIN_ReleaseWndPtr( wndParent );
533 parent = GetAncestor( parent, GA_PARENT );
535 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
538 /***********************************************************************
539 * RDW_UpdateRgns [RedrawWindow() helper]
541 * Walks the window tree and adds/removes parts of the hRgn to/from update
542 * regions of windows that overlap it. Also, manages internal paint flags.
544 * NOTE: Walks the window tree so the caller must lock it.
545 * MUST preserve hRgn (can modify but then has to restore).
547 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
550 * Called only when one of the following is set:
551 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
554 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
555 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
556 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
561 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
562 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
564 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
566 if( flags & RDW_INVALIDATE )
570 switch( wndPtr->hrgnUpdate )
573 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
576 wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate,
577 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
581 GetRgnBox( wndPtr->hrgnUpdate, &r );
582 if( IsRectEmpty( &r ) )
584 DeleteObject( wndPtr->hrgnUpdate );
585 wndPtr->hrgnUpdate = 0;
590 case 1: /* already an entire window */
596 if( wndPtr->hrgnUpdate > 1 )
597 DeleteObject( wndPtr->hrgnUpdate );
598 wndPtr->hrgnUpdate = 1;
601 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
603 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
604 add_paint_count( wndPtr->hwndSelf, 1 );
606 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
607 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
610 else if( flags & RDW_VALIDATE )
612 if( wndPtr->hrgnUpdate )
616 if( wndPtr->hrgnUpdate == 1 )
617 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
619 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
622 DeleteObject( wndPtr->hrgnUpdate );
623 wndPtr->hrgnUpdate = 0;
626 else /* validate everything */
628 if( wndPtr->hrgnUpdate > 1 ) DeleteObject( wndPtr->hrgnUpdate );
629 wndPtr->hrgnUpdate = 0;
632 if( !wndPtr->hrgnUpdate )
634 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
635 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
636 add_paint_count( wndPtr->hwndSelf, -1 );
640 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
641 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
645 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
646 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
648 /* in/validate child windows that intersect with the region if it
649 * is a valid handle. */
651 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
654 if( hRgn > 1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
656 POINT ptTotal, prevOrigin = {0,0};
660 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
661 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
663 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
665 WND *wnd = WIN_FindWndPtr( list[i] );
667 if( wnd->dwStyle & WS_VISIBLE )
671 r.left = wnd->rectWindow.left + ptClient.x;
672 r.right = wnd->rectWindow.right + ptClient.x;
673 r.top = wnd->rectWindow.top + ptClient.y;
674 r.bottom = wnd->rectWindow.bottom + ptClient.y;
676 ptOffset.x = r.left - prevOrigin.x;
677 ptOffset.y = r.top - prevOrigin.y;
678 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
680 if( RectInRegion( hRgn, &r ) )
682 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
683 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
684 prevOrigin.x = r.left + ptTotal.x;
685 prevOrigin.y = r.top + ptTotal.y;
686 ptTotal.x += ptOffset.x;
687 ptTotal.y += ptOffset.y;
690 WIN_ReleaseWndPtr( wnd );
692 HeapFree( GetProcessHeap(), 0, list );
693 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
698 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
703 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
706 for (i = 0; list[i]; i++)
708 WND *wnd = WIN_FindWndPtr( list[i] );
710 if( wnd->dwStyle & WS_VISIBLE )
711 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
712 WIN_ReleaseWndPtr( wnd );
714 HeapFree( GetProcessHeap(), 0, list );
720 /* Set/clear internal paint flag */
722 if (flags & RDW_INTERNALPAINT)
724 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
725 add_paint_count( wndPtr->hwndSelf, 1 );
726 wndPtr->flags |= WIN_INTERNAL_PAINT;
728 else if (flags & RDW_NOINTERNALPAINT)
730 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
731 add_paint_count( wndPtr->hwndSelf, -1 );
732 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
736 /***********************************************************************
737 * RDW_Paint [RedrawWindow() helper]
739 * Walks the window tree and paints/erases windows that have
740 * nonzero update regions according to redraw flags. hrgn is a scratch
741 * region passed down during recursion. Must not be 1.
744 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
746 /* NOTE: wndPtr is locked by caller.
748 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
749 * SendMessage() calls. This is a comment inside DefWindowProc() source
752 * This message avoids lots of inter-app message traffic
753 * by switching to the other task and continuing the
757 * LOWORD(lParam) = hrgnClip
758 * HIWORD(lParam) = hwndSkip (not used; always NULL)
762 HWND hWnd = wndPtr->hwndSelf;
763 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
765 /* Erase/update the window itself ... */
767 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
770 * Check if this window should delay it's processing of WM_NCPAINT.
771 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
773 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
774 ex |= RDW_EX_DELAY_NCPAINT;
776 if (flags & RDW_UPDATENOW)
778 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
779 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
781 else if (flags & RDW_ERASENOW)
783 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
786 hrgnRet = WIN_UpdateNCRgn(wndPtr,
788 UNC_REGION | UNC_CHECK |
789 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
793 if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
794 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
796 if( bIcon ) dcx |= DCX_WINDOW;
799 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
800 wndPtr->rectWindow.top - wndPtr->rectClient.top);
802 dcx &= ~DCX_INTERSECTRGN;
803 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
805 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
807 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
808 ReleaseDC( hWnd, hDC );
814 if( !IsWindow(hWnd) ) return hrgn;
816 /* ... and its child windows */
818 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
819 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
823 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
825 for (phwnd = list; *phwnd; phwnd++)
827 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
828 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
829 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
830 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
831 WIN_ReleaseWndPtr(wndPtr);
833 HeapFree( GetProcessHeap(), 0, list );
841 /***********************************************************************
842 * RedrawWindow (USER32.@)
844 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
845 HRGN hrgnUpdate, UINT flags )
852 if (!hwnd) hwnd = GetDesktopWindow();
853 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
855 /* check if the window or its parents are visible/not minimized */
857 if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
859 WIN_ReleaseWndPtr(wndPtr);
867 GetRgnBox( hrgnUpdate, &r );
868 TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
869 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
877 TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
878 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
879 r.top, r.right, r.bottom, hrgnUpdate, flags );
883 /* prepare an update region in window coordinates */
885 if( flags & RDW_FRAME )
886 r = wndPtr->rectWindow;
888 r = wndPtr->rectClient;
890 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
891 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
892 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
894 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
896 /* If the window doesn't have hrgnUpdate we leave hRgn zero
897 * and put a new region straight into wndPtr->hrgnUpdate
898 * so that RDW_UpdateRgns() won't have to do any extra work.
903 if( wndPtr->hrgnUpdate )
904 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
906 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
908 else if( rectUpdate )
910 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
911 OffsetRect( &r2, pt.x, pt.y );
914 if( wndPtr->hrgnUpdate == 0 )
915 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
917 hRgn = CreateRectRgnIndirect( &r2 );
919 else /* entire window or client depending on RDW_FRAME */
921 if( flags & RDW_FRAME )
923 if( wndPtr->hrgnUpdate )
924 DeleteObject( wndPtr->hrgnUpdate );
925 wndPtr->hrgnUpdate = 1;
929 GETCLIENTRECTW( wndPtr, r2 );
934 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
936 /* In this we cannot leave with zero hRgn */
939 hRgn = REGION_CropRgn( hRgn, hrgnUpdate, &r, &pt );
940 GetRgnBox( hRgn, &r2 );
941 if( IsRectEmpty( &r2 ) ) goto END;
943 else if( rectUpdate )
945 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
946 OffsetRect( &r2, pt.x, pt.y );
947 hRgn = CreateRectRgnIndirect( &r2 );
949 else /* entire window or client depending on RDW_FRAME */
951 if( flags & RDW_FRAME )
955 GETCLIENTRECTW( wndPtr, r2 );
956 hRgn = CreateRectRgnIndirect( &r2 );
961 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
963 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
965 /* Erase/update windows, from now on hRgn is a scratch region */
967 hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
970 if( hRgn > 1 && (hRgn != hrgnUpdate) )
972 WIN_ReleaseWndPtr(wndPtr);
977 /***********************************************************************
978 * RedrawWindow (USER.290)
980 BOOL16 WINAPI RedrawWindow16( HWND16 hwnd, const RECT16 *rectUpdate,
981 HRGN16 hrgnUpdate, UINT16 flags )
986 CONV_RECT16TO32( rectUpdate, &r );
987 return (BOOL16)RedrawWindow( (HWND)hwnd, &r, hrgnUpdate, flags );
989 return RedrawWindow( hwnd, NULL, hrgnUpdate, flags );
993 /***********************************************************************
994 * UpdateWindow (USER.124)
996 void WINAPI UpdateWindow16( HWND16 hwnd )
998 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1001 /***********************************************************************
1002 * UpdateWindow (USER32.@)
1004 void WINAPI UpdateWindow( HWND hwnd )
1006 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1009 /***********************************************************************
1010 * InvalidateRgn (USER.126)
1012 void WINAPI InvalidateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1014 RedrawWindow((HWND)hwnd, NULL, (HRGN)hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1018 /***********************************************************************
1019 * InvalidateRgn (USER32.@)
1021 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1023 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1027 /***********************************************************************
1028 * InvalidateRect (USER.125)
1030 void WINAPI InvalidateRect16( HWND16 hwnd, const RECT16 *rect, BOOL16 erase )
1032 RedrawWindow16( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1036 /***********************************************************************
1037 * InvalidateRect (USER32.@)
1039 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1041 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1045 /***********************************************************************
1046 * ValidateRgn (USER.128)
1048 void WINAPI ValidateRgn16( HWND16 hwnd, HRGN16 hrgn )
1050 RedrawWindow( (HWND)hwnd, NULL, (HRGN)hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
1054 /***********************************************************************
1055 * ValidateRgn (USER32.@)
1057 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1059 RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
1063 /***********************************************************************
1064 * ValidateRect (USER.127)
1066 void WINAPI ValidateRect16( HWND16 hwnd, const RECT16 *rect )
1068 RedrawWindow16( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1072 /***********************************************************************
1073 * ValidateRect (USER32.@)
1075 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1077 RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
1081 /***********************************************************************
1082 * GetUpdateRect (USER.190)
1084 BOOL16 WINAPI GetUpdateRect16( HWND16 hwnd, LPRECT16 rect, BOOL16 erase )
1089 if (!rect) return GetUpdateRect( hwnd, NULL, erase );
1090 ret = GetUpdateRect( hwnd, &r, erase );
1091 CONV_RECT32TO16( &r, rect );
1096 /***********************************************************************
1097 * GetUpdateRect (USER32.@)
1099 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1102 WND * wndPtr = WIN_FindWndPtr( hwnd );
1103 if (!wndPtr) return FALSE;
1107 if (wndPtr->hrgnUpdate > 1)
1109 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
1110 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
1115 GetRgnBox( hrgn, rect );
1116 DeleteObject( hrgn );
1117 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
1119 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
1121 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
1126 if( wndPtr->hrgnUpdate == 1 )
1128 GetClientRect( hwnd, rect );
1129 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
1132 SetRectEmpty( rect );
1134 retvalue = (wndPtr->hrgnUpdate >= 1);
1136 WIN_ReleaseWndPtr(wndPtr);
1141 /***********************************************************************
1142 * GetUpdateRgn (USER.237)
1144 INT16 WINAPI GetUpdateRgn16( HWND16 hwnd, HRGN16 hrgn, BOOL16 erase )
1146 return GetUpdateRgn( hwnd, hrgn, erase );
1150 /***********************************************************************
1151 * GetUpdateRgn (USER32.@)
1153 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1156 WND * wndPtr = WIN_FindWndPtr( hwnd );
1157 if (!wndPtr) return ERROR;
1159 if (wndPtr->hrgnUpdate == 0)
1161 SetRectRgn( hrgn, 0, 0, 0, 0 );
1162 retval = NULLREGION;
1166 if (wndPtr->hrgnUpdate == 1)
1168 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1169 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1170 retval = SIMPLEREGION;
1174 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1175 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1176 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1178 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1180 WIN_ReleaseWndPtr(wndPtr);
1185 /***********************************************************************
1186 * ExcludeUpdateRgn (USER.238)
1188 INT16 WINAPI ExcludeUpdateRgn16( HDC16 hdc, HWND16 hwnd )
1190 return ExcludeUpdateRgn( hdc, hwnd );
1194 /***********************************************************************
1195 * ExcludeUpdateRgn (USER32.@)
1197 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1202 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1204 if (wndPtr->hrgnUpdate)
1207 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1208 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1209 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1210 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1211 if( wndPtr->hrgnUpdate > 1 )
1213 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1214 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1215 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1218 /* do ugly coordinate translations in dce.c */
1220 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
1221 DeleteObject( hrgn );
1222 WIN_ReleaseWndPtr(wndPtr);
1225 WIN_ReleaseWndPtr(wndPtr);
1226 return GetClipBox( hdc, &rect );
1231 /***********************************************************************
1232 * FillRect (USER.81)
1234 * The Win16 variant doesn't support special color brushes like
1235 * the Win32 one, despite the fact that Win16, as well as Win32,
1236 * supports special background brushes for a window class.
1238 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1242 /* coordinates are logical so we cannot fast-check 'rect',
1243 * it will be done later in the PatBlt().
1246 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1247 PatBlt( hdc, rect->left, rect->top,
1248 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1249 SelectObject( hdc, prevBrush );
1254 /***********************************************************************
1255 * FillRect (USER32.@)
1257 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1261 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1262 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1265 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1266 PatBlt( hdc, rect->left, rect->top,
1267 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1268 SelectObject( hdc, prevBrush );
1273 /***********************************************************************
1274 * InvertRect (USER.82)
1276 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1278 PatBlt( hdc, rect->left, rect->top,
1279 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1283 /***********************************************************************
1284 * InvertRect (USER32.@)
1286 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1288 return PatBlt( hdc, rect->left, rect->top,
1289 rect->right - rect->left, rect->bottom - rect->top,
1294 /***********************************************************************
1295 * FrameRect (USER32.@)
1297 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1302 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1303 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1305 PatBlt( hdc, r.left, r.top, 1,
1306 r.bottom - r.top, PATCOPY );
1307 PatBlt( hdc, r.right - 1, r.top, 1,
1308 r.bottom - r.top, PATCOPY );
1309 PatBlt( hdc, r.left, r.top,
1310 r.right - r.left, 1, PATCOPY );
1311 PatBlt( hdc, r.left, r.bottom - 1,
1312 r.right - r.left, 1, PATCOPY );
1314 SelectObject( hdc, prevBrush );
1319 /***********************************************************************
1320 * FrameRect (USER.83)
1322 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1325 CONV_RECT16TO32( rect16, &rect );
1326 return FrameRect( hdc, &rect, hbrush );
1330 /***********************************************************************
1331 * DrawFocusRect (USER.466)
1333 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1336 CONV_RECT16TO32( rc, &rect32 );
1337 DrawFocusRect( hdc, &rect32 );
1341 /***********************************************************************
1342 * DrawFocusRect (USER32.@)
1344 * FIXME: PatBlt(PATINVERT) with background brush.
1346 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1349 HPEN hOldPen, hNewPen;
1350 INT oldDrawMode, oldBkMode;
1352 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1353 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1354 hOldPen = SelectObject(hdc, hNewPen);
1355 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1356 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1358 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1360 SetBkMode(hdc, oldBkMode);
1361 SetROP2(hdc, oldDrawMode);
1362 SelectObject(hdc, hOldPen);
1363 DeleteObject(hNewPen);
1364 SelectObject(hdc, hOldBrush);
1369 /**********************************************************************
1370 * DrawAnimatedRects (USER.448)
1372 BOOL16 WINAPI DrawAnimatedRects16( HWND16 hwnd, INT16 idAni,
1373 const RECT16* lprcFrom,
1374 const RECT16* lprcTo )
1376 RECT rcFrom32, rcTo32;
1378 rcFrom32.left = (INT)lprcFrom->left;
1379 rcFrom32.top = (INT)lprcFrom->top;
1380 rcFrom32.right = (INT)lprcFrom->right;
1381 rcFrom32.bottom = (INT)lprcFrom->bottom;
1383 rcTo32.left = (INT)lprcTo->left;
1384 rcTo32.top = (INT)lprcTo->top;
1385 rcTo32.right = (INT)lprcTo->right;
1386 rcTo32.bottom = (INT)lprcTo->bottom;
1388 return DrawAnimatedRects((HWND)hwnd, (INT)idAni, &rcFrom32, &rcTo32);
1392 /**********************************************************************
1393 * DrawAnimatedRects (USER32.@)
1395 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1396 const RECT* lprcFrom,
1397 const RECT* lprcTo )
1399 FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1404 /**********************************************************************
1405 * PAINTING_DrawStateJam
1407 * Jams in the requested type in the dc
1409 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1410 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1411 LPRECT rc, UINT dtflags, BOOL unicode )
1416 INT cx = rc->right - rc->left;
1417 INT cy = rc->bottom - rc->top;
1422 case DST_PREFIXTEXT:
1424 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1426 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1429 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1432 memdc = CreateCompatibleDC(hdc);
1433 if(!memdc) return FALSE;
1434 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1440 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1441 SelectObject(memdc, hbmsave);
1448 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1450 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1451 bRet = func(hdc, lp, wp, cx, cy);
1452 /* Restore origin */
1453 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1461 /**********************************************************************
1462 * PAINTING_DrawState()
1464 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1465 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1467 HBITMAP hbm, hbmsave;
1469 HBRUSH hbsave, hbrtmp = 0;
1472 UINT dtflags = DT_NOCLIP;
1474 UINT opcode = flags & 0xf;
1478 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1481 len = strlenW((LPWSTR)lp);
1483 len = strlen((LPSTR)lp);
1486 /* Find out what size the image has if not given by caller */
1490 CURSORICONINFO *ici;
1496 case DST_PREFIXTEXT:
1498 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1500 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1501 if(!retval) return FALSE;
1505 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1506 if(!ici) return FALSE;
1508 s.cy = ici->nHeight;
1509 GlobalUnlock16((HGLOBAL16)lp);
1513 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1519 case DST_COMPLEX: /* cx and cy must be set in this mode */
1532 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1533 dtflags |= DT_RIGHT;
1534 if(opcode == DST_TEXT)
1535 dtflags |= DT_NOPREFIX;
1537 /* For DSS_NORMAL we just jam in the image and return */
1538 if((flags & 0x7ff0) == DSS_NORMAL)
1540 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1543 /* For all other states we need to convert the image to B/W in a local bitmap */
1544 /* before it is displayed */
1545 fg = SetTextColor(hdc, RGB(0, 0, 0));
1546 bg = SetBkColor(hdc, RGB(255, 255, 255));
1547 hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1548 memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1549 retval = FALSE; /* assume failure */
1551 /* From here on we must use "goto cleanup" when something goes wrong */
1552 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1553 if(!hbm) goto cleanup;
1554 memdc = CreateCompatibleDC(hdc);
1555 if(!memdc) goto cleanup;
1556 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1557 if(!hbmsave) goto cleanup;
1558 rc.left = rc.top = 0;
1561 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1562 SetBkColor(memdc, RGB(255, 255, 255));
1563 SetTextColor(memdc, RGB(0, 0, 0));
1564 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1566 /* DST_COMPLEX may draw text as well,
1567 * so we must be sure that correct font is selected
1569 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1570 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1571 if(hfsave) SelectObject(memdc, hfsave);
1572 if(!tmp) goto cleanup;
1574 /* This state cause the image to be dithered */
1575 if(flags & DSS_UNION)
1577 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1578 if(!hbsave) goto cleanup;
1579 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1580 SelectObject(memdc, hbsave);
1581 if(!tmp) goto cleanup;
1584 if (flags & DSS_DISABLED)
1585 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1586 else if (flags & DSS_DEFAULT)
1587 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1589 /* Draw light or dark shadow */
1590 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1592 if(!hbrtmp) goto cleanup;
1593 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1594 if(!hbsave) goto cleanup;
1595 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1596 SelectObject(hdc, hbsave);
1597 DeleteObject(hbrtmp);
1601 if (flags & DSS_DISABLED)
1603 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1604 if(!hbrtmp) goto cleanup;
1608 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1611 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1613 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1615 retval = TRUE; /* We succeeded */
1618 SetTextColor(hdc, fg);
1619 SetBkColor(hdc, bg);
1621 if(hbsave) SelectObject(hdc, hbsave);
1622 if(hbmsave) SelectObject(memdc, hbmsave);
1623 if(hbrtmp) DeleteObject(hbrtmp);
1624 if(hbm) DeleteObject(hbm);
1625 if(memdc) DeleteDC(memdc);
1630 /**********************************************************************
1631 * DrawStateA (USER32.@)
1633 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1634 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1635 INT x, INT y, INT cx, INT cy, UINT flags)
1637 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1640 /**********************************************************************
1641 * DrawStateW (USER32.@)
1643 BOOL WINAPI DrawStateW(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, TRUE);
1650 /***********************************************************************
1651 * SelectPalette (USER.282)
1653 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1654 BOOL16 bForceBackground )
1656 WORD wBkgPalette = 1;
1658 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1660 HWND hwnd = WindowFromDC( hDC );
1663 HWND hForeground = GetForegroundWindow();
1664 /* set primary palette if it's related to current active */
1665 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1668 return GDISelectPalette16( hDC, hPal, wBkgPalette);
1672 /***********************************************************************
1673 * RealizePalette (USER.283)
1675 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1677 UINT16 realized = GDIRealizePalette16( hDC );
1679 /* do not send anything if no colors were changed */
1680 if (realized && IsDCCurrentPalette16( hDC ))
1682 /* send palette change notification */
1683 HWND hWnd = WindowFromDC( hDC );
1684 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, hWnd, 0L);
1690 /***********************************************************************
1691 * UserRealizePalette (USER32.@)
1693 UINT WINAPI UserRealizePalette( HDC hDC )
1695 return RealizePalette16( hDC );