Declare DeviceIoControl.
[wine.git] / windows / painting.c
blobaf15f71cdab680eb2d14cc446a5c61e091e7a9ff
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 "wine/server.h"
14 #include "region.h"
15 #include "user.h"
16 #include "win.h"
17 #include "queue.h"
18 #include "dce.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
42 /* Last COLOR id */
43 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
46 /***********************************************************************
47 * add_paint_count
49 * Add an increment (normally 1 or -1) to the current paint count of a window.
51 static void add_paint_count( HWND hwnd, int incr )
53 SERVER_START_REQ( inc_queue_paint_count )
55 req->id = (void *)GetWindowThreadProcessId( hwnd, NULL );
56 req->incr = incr;
57 SERVER_CALL();
59 SERVER_END_REQ;
63 /***********************************************************************
64 * WIN_HaveToDelayNCPAINT
66 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
67 * is generated as soon as a region intersecting the non-client area
68 * of a window is invalidated.
70 * This technique will work fine for all windows whose parents
71 * have the WS_CLIPCHILDREN style. When the parents have that style,
72 * they are not going to override the contents of their children.
73 * However, when the parent doesn't have that style, Windows relies
74 * on a "painter's algorithm" to display the contents of the windows.
75 * That is, windows are painted from back to front. This includes the
76 * non-client area.
78 * This method looks at the current state of a window to determine
79 * if the sending of the WM_NCPAINT message should be delayed until
80 * the BeginPaint call.
82 * PARAMS:
83 * wndPtr - A Locked window pointer to the window we're
84 * examining.
85 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
86 * function. This is a shortcut for the cases when
87 * we already know when to avoid scanning all the
88 * parents of a window. If you already know that this
89 * window's NCPAINT should be delayed, set the
90 * UNC_DELAY_NCPAINT flag for this parameter.
92 * This shortcut behavior is implemented in the
93 * RDW_Paint() method.
96 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
99 * Test the shortcut first. (duh)
101 if (uncFlags & UNC_DELAY_NCPAINT)
102 return TRUE;
105 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
106 * method only. This is another shortcut to avoid going
107 * up the parent's chain of the window to finally
108 * figure-out that none of them has an invalid region.
110 if (uncFlags & UNC_IN_BEGINPAINT)
111 return FALSE;
114 * Scan all the parents of this window to find a window
115 * that doesn't have the WS_CLIPCHILDREN style and that
116 * has an invalid region.
118 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
120 WND* parentWnd = WIN_FindWndPtr( hwnd );
121 if (!(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
123 WIN_ReleaseWndPtr( parentWnd );
124 return TRUE;
126 WIN_ReleaseWndPtr( parentWnd );
128 return FALSE;
131 /***********************************************************************
132 * WIN_UpdateNCRgn
134 * Things to do:
135 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
136 * Crop hrgnUpdate to a client rect, especially if it 1.
137 * If UNC_REGION is set return update region for the client rect.
139 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
140 * The trick is that when the returned region handle may be different from hRgn.
141 * In this case the old hRgn must be considered gone. BUT, if the returned value
142 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
143 * a DC without extra clipping region.
145 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
147 RECT r;
148 HRGN hClip = 0;
149 HRGN hrgnRet = 0;
151 TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n",
152 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
154 /* desktop window doesn't have a nonclient area */
155 if(wnd->hwndSelf == GetDesktopWindow())
157 wnd->flags &= ~WIN_NEEDS_NCPAINT;
158 if( wnd->hrgnUpdate > 1 )
159 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
160 else
162 hrgnRet = wnd->hrgnUpdate;
164 return hrgnRet;
167 if ((wnd->hwndSelf == GetForegroundWindow()) &&
168 !(wnd->flags & WIN_NCACTIVATED) )
170 wnd->flags |= WIN_NCACTIVATED;
171 uncFlags |= UNC_ENTIRE;
175 * If the window's non-client area needs to be painted,
177 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
178 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
180 RECT r2, r3;
182 wnd->flags &= ~WIN_NEEDS_NCPAINT;
183 GETCLIENTRECTW( wnd, r );
185 TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n",
186 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
187 if( wnd->hrgnUpdate > 1 )
189 /* Check if update rgn overlaps with nonclient area */
191 GetRgnBox( wnd->hrgnUpdate, &r2 );
192 UnionRect( &r3, &r2, &r );
193 if( r3.left != r.left || r3.top != r.top ||
194 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
196 /* crop hrgnUpdate, save old one in hClip - the only
197 * case that places a valid region handle in hClip */
199 hClip = wnd->hrgnUpdate;
200 wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
201 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
204 if( uncFlags & UNC_CHECK )
206 GetRgnBox( wnd->hrgnUpdate, &r3 );
207 if( IsRectEmpty( &r3 ) )
209 /* delete the update region since all invalid
210 * parts were in the nonclient area */
212 DeleteObject( wnd->hrgnUpdate );
213 wnd->hrgnUpdate = 0;
214 if(!(wnd->flags & WIN_INTERNAL_PAINT))
215 add_paint_count( wnd->hwndSelf, -1 );
217 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
221 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
223 else
224 if( wnd->hrgnUpdate == 1 )/* entire window */
226 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
227 if( uncFlags & UNC_REGION ) hrgnRet = 1;
228 uncFlags |= UNC_ENTIRE;
231 else /* no WM_NCPAINT unless forced */
233 if( wnd->hrgnUpdate > 1 )
235 copyrgn:
236 if( uncFlags & UNC_REGION )
237 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
239 else
240 if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
242 GETCLIENTRECTW( wnd, r );
243 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
244 if( uncFlags & UNC_REGION ) hrgnRet = 1;
248 if(!hClip && (uncFlags & UNC_ENTIRE) )
250 /* still don't do anything if there is no nonclient area */
251 hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
254 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
256 if ( hClip == hrgnRet && hrgnRet > 1 ) {
257 hClip = CreateRectRgn( 0, 0, 0, 0 );
258 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
261 SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
262 if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
263 DeleteObject( hClip );
265 * Since all Window locks are suspended while processing the WM_NCPAINT
266 * we want to make sure the window still exists before continuing.
268 if (!IsWindow(wnd->hwndSelf))
270 DeleteObject(hrgnRet);
271 hrgnRet=0;
275 TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
277 return hrgnRet;
281 /***********************************************************************
282 * BeginPaint (USER.39)
284 HDC16 WINAPI BeginPaint16( HWND16 hwnd, LPPAINTSTRUCT16 lps )
286 PAINTSTRUCT ps;
288 BeginPaint( WIN_Handle32(hwnd), &ps );
289 lps->hdc = ps.hdc;
290 lps->fErase = ps.fErase;
291 lps->rcPaint.top = ps.rcPaint.top;
292 lps->rcPaint.left = ps.rcPaint.left;
293 lps->rcPaint.right = ps.rcPaint.right;
294 lps->rcPaint.bottom = ps.rcPaint.bottom;
295 lps->fRestore = ps.fRestore;
296 lps->fIncUpdate = ps.fIncUpdate;
297 return lps->hdc;
301 /***********************************************************************
302 * BeginPaint (USER32.@)
304 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
306 BOOL bIcon;
307 HRGN hrgnUpdate;
308 RECT clipRect, clientRect;
309 WND *wndPtr = WIN_FindWndPtr( hwnd );
310 if (!wndPtr) return 0;
312 bIcon = (wndPtr->dwStyle & WS_MINIMIZE && GetClassLongA(hwnd, GCL_HICON));
314 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
316 /* send WM_NCPAINT and make sure hrgnUpdate is a valid rgn handle */
317 WIN_UpdateNCRgn( wndPtr, 0, UNC_UPDATE | UNC_IN_BEGINPAINT);
320 * Make sure the window is still a window. All window locks are suspended
321 * when the WM_NCPAINT is sent.
323 if (!IsWindow(wndPtr->hwndSelf))
325 WIN_ReleaseWndPtr(wndPtr);
326 return 0;
329 if( ((hrgnUpdate = wndPtr->hrgnUpdate) != 0) || (wndPtr->flags & WIN_INTERNAL_PAINT))
330 add_paint_count( hwnd, -1 );
332 wndPtr->hrgnUpdate = 0;
333 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
335 HideCaret( hwnd );
337 TRACE("hrgnUpdate = %04x, \n", hrgnUpdate);
339 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_PARENTDC)
341 /* Don't clip the output to the update region for CS_PARENTDC window */
342 if( hrgnUpdate )
343 DeleteObject(hrgnUpdate);
344 lps->hdc = GetDCEx( hwnd, 0, DCX_WINDOWPAINT | DCX_USESTYLE |
345 (bIcon ? DCX_WINDOW : 0) );
347 else
349 if( hrgnUpdate ) /* convert to client coordinates */
350 OffsetRgn( hrgnUpdate, wndPtr->rectWindow.left - wndPtr->rectClient.left,
351 wndPtr->rectWindow.top - wndPtr->rectClient.top );
352 lps->hdc = GetDCEx(hwnd, hrgnUpdate, DCX_INTERSECTRGN |
353 DCX_WINDOWPAINT | DCX_USESTYLE | (bIcon ? DCX_WINDOW : 0) );
354 /* ReleaseDC() in EndPaint() will delete the region */
357 TRACE("hdc = %04x\n", lps->hdc);
359 if (!lps->hdc)
361 WARN("GetDCEx() failed in BeginPaint(), hwnd=%04x\n", hwnd);
362 WIN_ReleaseWndPtr(wndPtr);
363 return 0;
366 /* It is possible that the clip box is bigger than the window itself,
367 if CS_PARENTDC flag is set. Windows never return a paint rect bigger
368 than the window itself, so we need to intersect the cliprect with
369 the window */
371 GetClientRect( hwnd, &clientRect );
373 GetClipBox( lps->hdc, &clipRect );
374 LPtoDP(lps->hdc, (LPPOINT)&clipRect, 2); /* GetClipBox returns LP */
376 IntersectRect(&lps->rcPaint, &clientRect, &clipRect);
377 DPtoLP(lps->hdc, (LPPOINT)&lps->rcPaint, 2); /* we must return LP */
379 TRACE("box = (%i,%i - %i,%i)\n", lps->rcPaint.left, lps->rcPaint.top,
380 lps->rcPaint.right, lps->rcPaint.bottom );
382 if (wndPtr->flags & WIN_NEEDS_ERASEBKGND)
384 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
385 lps->fErase = !SendMessageA(hwnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
386 (WPARAM16)lps->hdc, 0 );
388 else lps->fErase = TRUE;
390 WIN_ReleaseWndPtr(wndPtr);
391 return lps->hdc;
395 /***********************************************************************
396 * EndPaint (USER.40)
398 BOOL16 WINAPI EndPaint16( HWND16 hwnd, const PAINTSTRUCT16* lps )
400 ReleaseDC16( hwnd, lps->hdc );
401 ShowCaret16( hwnd );
402 return TRUE;
406 /***********************************************************************
407 * EndPaint (USER32.@)
409 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
411 ReleaseDC( hwnd, lps->hdc );
412 ShowCaret( hwnd );
413 return TRUE;
417 /***********************************************************************
418 * RDW_ValidateParent [RDW_UpdateRgns() helper]
420 * Validate the portions of parents that are covered by a validated child
421 * wndPtr = child
423 static void RDW_ValidateParent(WND *wndChild)
425 HWND parent;
426 HRGN hrg;
428 if (wndChild->hrgnUpdate == 1 ) {
429 RECT r;
430 r.left = 0;
431 r.top = 0;
432 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
433 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
434 hrg = CreateRectRgnIndirect( &r );
435 } else
436 hrg = wndChild->hrgnUpdate;
438 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
439 while (parent && parent != GetDesktopWindow())
441 WND *wndParent = WIN_FindWndPtr( parent );
442 if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
444 if (wndParent->hrgnUpdate != 0)
446 POINT ptOffset;
447 RECT rect, rectParent;
448 if( wndParent->hrgnUpdate == 1 )
450 RECT r;
452 r.left = 0;
453 r.top = 0;
454 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
455 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
457 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
459 /* we must offset the child region by the offset of the child rect in the parent */
460 GetWindowRect(wndParent->hwndSelf, &rectParent);
461 GetWindowRect(wndChild->hwndSelf, &rect);
462 ptOffset.x = rect.left - rectParent.left;
463 ptOffset.y = rect.top - rectParent.top;
464 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
465 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
466 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
469 WIN_ReleaseWndPtr( wndParent );
470 parent = GetAncestor( parent, GA_PARENT );
472 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
475 /***********************************************************************
476 * RDW_UpdateRgns [RedrawWindow() helper]
478 * Walks the window tree and adds/removes parts of the hRgn to/from update
479 * regions of windows that overlap it. Also, manages internal paint flags.
481 * NOTE: Walks the window tree so the caller must lock it.
482 * MUST preserve hRgn (can modify but then has to restore).
484 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
487 * Called only when one of the following is set:
488 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
491 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
492 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
493 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
494 RECT r;
496 r.left = 0;
497 r.top = 0;
498 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
499 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
501 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
503 if( flags & RDW_INVALIDATE )
505 if( hRgn > 1 )
507 switch( wndPtr->hrgnUpdate )
509 default:
510 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
511 /* fall through */
512 case 0:
513 wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate,
514 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
515 &r, NULL );
516 if( !bHadOne )
518 GetRgnBox( wndPtr->hrgnUpdate, &r );
519 if( IsRectEmpty( &r ) )
521 DeleteObject( wndPtr->hrgnUpdate );
522 wndPtr->hrgnUpdate = 0;
523 goto end;
526 break;
527 case 1: /* already an entire window */
528 break;
531 else if( hRgn == 1 )
533 if( wndPtr->hrgnUpdate > 1 )
534 DeleteObject( wndPtr->hrgnUpdate );
535 wndPtr->hrgnUpdate = 1;
537 else
538 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
540 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
541 add_paint_count( wndPtr->hwndSelf, 1 );
543 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
544 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
545 flags |= RDW_FRAME;
547 else if( flags & RDW_VALIDATE )
549 if( wndPtr->hrgnUpdate )
551 if( hRgn > 1 )
553 if( wndPtr->hrgnUpdate == 1 )
554 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
556 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
557 == NULLREGION )
559 DeleteObject( wndPtr->hrgnUpdate );
560 wndPtr->hrgnUpdate = 0;
563 else /* validate everything */
565 if( wndPtr->hrgnUpdate > 1 ) DeleteObject( wndPtr->hrgnUpdate );
566 wndPtr->hrgnUpdate = 0;
569 if( !wndPtr->hrgnUpdate )
571 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
572 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
573 add_paint_count( wndPtr->hwndSelf, -1 );
577 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
578 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
582 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
583 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
585 /* in/validate child windows that intersect with the region if it
586 * is a valid handle. */
588 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
590 HWND *list;
591 if( hRgn > 1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
593 POINT ptTotal, prevOrigin = {0,0};
594 POINT ptClient;
595 INT i;
597 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
598 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
600 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
602 WND *wnd = WIN_FindWndPtr( list[i] );
603 if (!wnd) continue;
604 if( wnd->dwStyle & WS_VISIBLE )
606 POINT ptOffset;
608 r.left = wnd->rectWindow.left + ptClient.x;
609 r.right = wnd->rectWindow.right + ptClient.x;
610 r.top = wnd->rectWindow.top + ptClient.y;
611 r.bottom = wnd->rectWindow.bottom + ptClient.y;
613 ptOffset.x = r.left - prevOrigin.x;
614 ptOffset.y = r.top - prevOrigin.y;
615 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
617 if( RectInRegion( hRgn, &r ) )
619 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
620 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
621 prevOrigin.x = r.left + ptTotal.x;
622 prevOrigin.y = r.top + ptTotal.y;
623 ptTotal.x += ptOffset.x;
624 ptTotal.y += ptOffset.y;
627 WIN_ReleaseWndPtr( wnd );
629 HeapFree( GetProcessHeap(), 0, list );
630 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
631 bChildren = 0;
635 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
637 if( bChildren )
639 HWND *list;
640 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
642 INT i;
643 for (i = 0; list[i]; i++)
645 WND *wnd = WIN_FindWndPtr( list[i] );
646 if (!wnd) continue;
647 if( wnd->dwStyle & WS_VISIBLE )
648 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
649 WIN_ReleaseWndPtr( wnd );
651 HeapFree( GetProcessHeap(), 0, list );
655 end:
657 /* Set/clear internal paint flag */
659 if (flags & RDW_INTERNALPAINT)
661 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
662 add_paint_count( wndPtr->hwndSelf, 1 );
663 wndPtr->flags |= WIN_INTERNAL_PAINT;
665 else if (flags & RDW_NOINTERNALPAINT)
667 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
668 add_paint_count( wndPtr->hwndSelf, -1 );
669 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
673 /***********************************************************************
674 * RDW_Paint [RedrawWindow() helper]
676 * Walks the window tree and paints/erases windows that have
677 * nonzero update regions according to redraw flags. hrgn is a scratch
678 * region passed down during recursion. Must not be 1.
681 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
683 /* NOTE: wndPtr is locked by caller.
685 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
686 * SendMessage() calls. This is a comment inside DefWindowProc() source
687 * from 16-bit SDK:
689 * This message avoids lots of inter-app message traffic
690 * by switching to the other task and continuing the
691 * recursion there.
693 * wParam = flags
694 * LOWORD(lParam) = hrgnClip
695 * HIWORD(lParam) = hwndSkip (not used; always NULL)
698 HDC hDC;
699 HWND hWnd = wndPtr->hwndSelf;
700 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
702 /* Erase/update the window itself ... */
704 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
707 * Check if this window should delay it's processing of WM_NCPAINT.
708 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
710 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
711 ex |= RDW_EX_DELAY_NCPAINT;
713 if (flags & RDW_UPDATENOW)
715 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
716 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
718 else if (flags & RDW_ERASENOW)
720 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
721 HRGN hrgnRet;
723 hrgnRet = WIN_UpdateNCRgn(wndPtr,
724 hrgn,
725 UNC_REGION | UNC_CHECK |
726 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
728 if( hrgnRet )
730 if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
731 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
733 if( bIcon ) dcx |= DCX_WINDOW;
734 else
735 if( hrgnRet )
736 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
737 wndPtr->rectWindow.top - wndPtr->rectClient.top);
738 else
739 dcx &= ~DCX_INTERSECTRGN;
740 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
742 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
743 (WPARAM)hDC, 0 ))
744 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
745 ReleaseDC( hWnd, hDC );
751 if( !IsWindow(hWnd) ) return hrgn;
753 /* ... and its child windows */
755 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
756 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
758 HWND *list, *phwnd;
760 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
762 for (phwnd = list; *phwnd; phwnd++)
764 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
765 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
766 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
767 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
768 WIN_ReleaseWndPtr(wndPtr);
770 HeapFree( GetProcessHeap(), 0, list );
774 return hrgn;
778 /***********************************************************************
779 * RedrawWindow (USER32.@)
781 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
782 HRGN hrgnUpdate, UINT flags )
784 HRGN hRgn = 0;
785 RECT r, r2;
786 POINT pt;
787 WND* wndPtr;
789 if (!hwnd) hwnd = GetDesktopWindow();
790 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
792 /* check if the window or its parents are visible/not minimized */
794 if (!WIN_IsWindowDrawable( wndPtr, !(flags & RDW_FRAME) ) )
796 WIN_ReleaseWndPtr(wndPtr);
797 return TRUE;
800 if (TRACE_ON(win))
802 if( hrgnUpdate )
804 GetRgnBox( hrgnUpdate, &r );
805 TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
806 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
808 else
810 if( rectUpdate )
811 r = *rectUpdate;
812 else
813 SetRectEmpty( &r );
814 TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
815 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
816 r.top, r.right, r.bottom, hrgnUpdate, flags );
820 /* prepare an update region in window coordinates */
822 if( flags & RDW_FRAME )
823 r = wndPtr->rectWindow;
824 else
825 r = wndPtr->rectClient;
827 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
828 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
829 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
831 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
833 /* If the window doesn't have hrgnUpdate we leave hRgn zero
834 * and put a new region straight into wndPtr->hrgnUpdate
835 * so that RDW_UpdateRgns() won't have to do any extra work.
838 if( hrgnUpdate )
840 if( wndPtr->hrgnUpdate )
841 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
842 else
843 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
845 else if( rectUpdate )
847 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
848 OffsetRect( &r2, pt.x, pt.y );
850 rect2i:
851 if( wndPtr->hrgnUpdate == 0 )
852 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
853 else
854 hRgn = CreateRectRgnIndirect( &r2 );
856 else /* entire window or client depending on RDW_FRAME */
858 if( flags & RDW_FRAME )
860 if( wndPtr->hrgnUpdate )
861 DeleteObject( wndPtr->hrgnUpdate );
862 wndPtr->hrgnUpdate = 1;
864 else
866 GETCLIENTRECTW( wndPtr, r2 );
867 goto rect2i;
871 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
873 /* In this we cannot leave with zero hRgn */
874 if( hrgnUpdate )
876 hRgn = REGION_CropRgn( hRgn, hrgnUpdate, &r, &pt );
877 GetRgnBox( hRgn, &r2 );
878 if( IsRectEmpty( &r2 ) ) goto END;
880 else if( rectUpdate )
882 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
883 OffsetRect( &r2, pt.x, pt.y );
884 hRgn = CreateRectRgnIndirect( &r2 );
886 else /* entire window or client depending on RDW_FRAME */
888 if( flags & RDW_FRAME )
889 hRgn = 1;
890 else
892 GETCLIENTRECTW( wndPtr, r2 );
893 hRgn = CreateRectRgnIndirect( &r2 );
898 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
900 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
902 /* Erase/update windows, from now on hRgn is a scratch region */
904 hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
906 END:
907 if( hRgn > 1 && (hRgn != hrgnUpdate) )
908 DeleteObject(hRgn );
909 WIN_ReleaseWndPtr(wndPtr);
910 return TRUE;
914 /***********************************************************************
915 * UpdateWindow (USER32.@)
917 void WINAPI UpdateWindow( HWND hwnd )
919 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
923 /***********************************************************************
924 * InvalidateRgn (USER32.@)
926 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
928 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
932 /***********************************************************************
933 * InvalidateRect (USER32.@)
935 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
937 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
941 /***********************************************************************
942 * ValidateRgn (USER32.@)
944 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
946 RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
950 /***********************************************************************
951 * ValidateRect (USER32.@)
953 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
955 RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
959 /***********************************************************************
960 * GetUpdateRect (USER32.@)
962 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
964 BOOL retvalue;
965 WND * wndPtr = WIN_FindWndPtr( hwnd );
966 if (!wndPtr) return FALSE;
968 if (rect)
970 if (wndPtr->hrgnUpdate > 1)
972 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
973 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
975 retvalue = FALSE;
976 goto END;
978 GetRgnBox( hrgn, rect );
979 DeleteObject( hrgn );
980 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
982 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
984 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
988 else
989 if( wndPtr->hrgnUpdate == 1 )
991 GetClientRect( hwnd, rect );
992 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
994 else
995 SetRectEmpty( rect );
997 retvalue = (wndPtr->hrgnUpdate >= 1);
998 END:
999 WIN_ReleaseWndPtr(wndPtr);
1000 return retvalue;
1004 /***********************************************************************
1005 * GetUpdateRgn (USER32.@)
1007 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1009 INT retval;
1010 WND * wndPtr = WIN_FindWndPtr( hwnd );
1011 if (!wndPtr) return ERROR;
1013 if (wndPtr->hrgnUpdate == 0)
1015 SetRectRgn( hrgn, 0, 0, 0, 0 );
1016 retval = NULLREGION;
1017 goto END;
1019 else
1020 if (wndPtr->hrgnUpdate == 1)
1022 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1023 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1024 retval = SIMPLEREGION;
1026 else
1028 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1029 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1030 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1032 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1033 END:
1034 WIN_ReleaseWndPtr(wndPtr);
1035 return retval;
1039 /***********************************************************************
1040 * ExcludeUpdateRgn (USER32.@)
1042 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1044 RECT rect;
1045 WND * wndPtr;
1047 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1049 if (wndPtr->hrgnUpdate)
1051 INT ret;
1052 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1053 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1054 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1055 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1056 if( wndPtr->hrgnUpdate > 1 )
1058 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1059 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1060 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1063 /* do ugly coordinate translations in dce.c */
1065 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
1066 DeleteObject( hrgn );
1067 WIN_ReleaseWndPtr(wndPtr);
1068 return ret;
1070 WIN_ReleaseWndPtr(wndPtr);
1071 return GetClipBox( hdc, &rect );
1076 /***********************************************************************
1077 * FillRect (USER.81)
1078 * NOTE
1079 * The Win16 variant doesn't support special color brushes like
1080 * the Win32 one, despite the fact that Win16, as well as Win32,
1081 * supports special background brushes for a window class.
1083 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1085 HBRUSH prevBrush;
1087 /* coordinates are logical so we cannot fast-check 'rect',
1088 * it will be done later in the PatBlt().
1091 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1092 PatBlt( hdc, rect->left, rect->top,
1093 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1094 SelectObject( hdc, prevBrush );
1095 return 1;
1099 /***********************************************************************
1100 * FillRect (USER32.@)
1102 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1104 HBRUSH prevBrush;
1106 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1107 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1110 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1111 PatBlt( hdc, rect->left, rect->top,
1112 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1113 SelectObject( hdc, prevBrush );
1114 return 1;
1118 /***********************************************************************
1119 * InvertRect (USER.82)
1121 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1123 PatBlt( hdc, rect->left, rect->top,
1124 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1128 /***********************************************************************
1129 * InvertRect (USER32.@)
1131 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1133 return PatBlt( hdc, rect->left, rect->top,
1134 rect->right - rect->left, rect->bottom - rect->top,
1135 DSTINVERT );
1139 /***********************************************************************
1140 * FrameRect (USER32.@)
1142 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1144 HBRUSH prevBrush;
1145 RECT r = *rect;
1147 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1148 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1150 PatBlt( hdc, r.left, r.top, 1,
1151 r.bottom - r.top, PATCOPY );
1152 PatBlt( hdc, r.right - 1, r.top, 1,
1153 r.bottom - r.top, PATCOPY );
1154 PatBlt( hdc, r.left, r.top,
1155 r.right - r.left, 1, PATCOPY );
1156 PatBlt( hdc, r.left, r.bottom - 1,
1157 r.right - r.left, 1, PATCOPY );
1159 SelectObject( hdc, prevBrush );
1160 return TRUE;
1164 /***********************************************************************
1165 * FrameRect (USER.83)
1167 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1169 RECT rect;
1170 CONV_RECT16TO32( rect16, &rect );
1171 return FrameRect( hdc, &rect, hbrush );
1175 /***********************************************************************
1176 * DrawFocusRect (USER.466)
1178 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1180 RECT rect32;
1181 CONV_RECT16TO32( rc, &rect32 );
1182 DrawFocusRect( hdc, &rect32 );
1186 /***********************************************************************
1187 * DrawFocusRect (USER32.@)
1189 * FIXME: PatBlt(PATINVERT) with background brush.
1191 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1193 HBRUSH hOldBrush;
1194 HPEN hOldPen, hNewPen;
1195 INT oldDrawMode, oldBkMode;
1197 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1198 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1199 hOldPen = SelectObject(hdc, hNewPen);
1200 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1201 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1203 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1205 SetBkMode(hdc, oldBkMode);
1206 SetROP2(hdc, oldDrawMode);
1207 SelectObject(hdc, hOldPen);
1208 DeleteObject(hNewPen);
1209 SelectObject(hdc, hOldBrush);
1211 return TRUE;
1215 /**********************************************************************
1216 * DrawAnimatedRects (USER32.@)
1218 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1219 const RECT* lprcFrom,
1220 const RECT* lprcTo )
1222 FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1223 return TRUE;
1227 /**********************************************************************
1228 * PAINTING_DrawStateJam
1230 * Jams in the requested type in the dc
1232 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1233 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1234 LPRECT rc, UINT dtflags, BOOL unicode )
1236 HDC memdc;
1237 HBITMAP hbmsave;
1238 BOOL retval;
1239 INT cx = rc->right - rc->left;
1240 INT cy = rc->bottom - rc->top;
1242 switch(opcode)
1244 case DST_TEXT:
1245 case DST_PREFIXTEXT:
1246 if(unicode)
1247 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1248 else
1249 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1251 case DST_ICON:
1252 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1254 case DST_BITMAP:
1255 memdc = CreateCompatibleDC(hdc);
1256 if(!memdc) return FALSE;
1257 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1258 if(!hbmsave)
1260 DeleteDC(memdc);
1261 return FALSE;
1263 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1264 SelectObject(memdc, hbmsave);
1265 DeleteDC(memdc);
1266 return retval;
1268 case DST_COMPLEX:
1269 if(func) {
1270 BOOL bRet;
1271 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1273 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1274 bRet = func(hdc, lp, wp, cx, cy);
1275 /* Restore origin */
1276 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1277 return bRet;
1278 } else
1279 return FALSE;
1281 return FALSE;
1284 /**********************************************************************
1285 * PAINTING_DrawState()
1287 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1288 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1290 HBITMAP hbm, hbmsave;
1291 HFONT hfsave;
1292 HBRUSH hbsave, hbrtmp = 0;
1293 HDC memdc;
1294 RECT rc;
1295 UINT dtflags = DT_NOCLIP;
1296 COLORREF fg, bg;
1297 UINT opcode = flags & 0xf;
1298 INT len = wp;
1299 BOOL retval, tmp;
1301 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1303 if(unicode)
1304 len = strlenW((LPWSTR)lp);
1305 else
1306 len = strlen((LPSTR)lp);
1309 /* Find out what size the image has if not given by caller */
1310 if(!cx || !cy)
1312 SIZE s;
1313 CURSORICONINFO *ici;
1314 BITMAP bm;
1316 switch(opcode)
1318 case DST_TEXT:
1319 case DST_PREFIXTEXT:
1320 if(unicode)
1321 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1322 else
1323 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1324 if(!retval) return FALSE;
1325 break;
1327 case DST_ICON:
1328 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1329 if(!ici) return FALSE;
1330 s.cx = ici->nWidth;
1331 s.cy = ici->nHeight;
1332 GlobalUnlock16((HGLOBAL16)lp);
1333 break;
1335 case DST_BITMAP:
1336 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1337 return FALSE;
1338 s.cx = bm.bmWidth;
1339 s.cy = bm.bmHeight;
1340 break;
1342 case DST_COMPLEX: /* cx and cy must be set in this mode */
1343 return FALSE;
1346 if(!cx) cx = s.cx;
1347 if(!cy) cy = s.cy;
1350 rc.left = x;
1351 rc.top = y;
1352 rc.right = x + cx;
1353 rc.bottom = y + cy;
1355 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1356 dtflags |= DT_RIGHT;
1357 if(opcode == DST_TEXT)
1358 dtflags |= DT_NOPREFIX;
1360 /* For DSS_NORMAL we just jam in the image and return */
1361 if((flags & 0x7ff0) == DSS_NORMAL)
1363 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1366 /* For all other states we need to convert the image to B/W in a local bitmap */
1367 /* before it is displayed */
1368 fg = SetTextColor(hdc, RGB(0, 0, 0));
1369 bg = SetBkColor(hdc, RGB(255, 255, 255));
1370 hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1371 memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1372 retval = FALSE; /* assume failure */
1374 /* From here on we must use "goto cleanup" when something goes wrong */
1375 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1376 if(!hbm) goto cleanup;
1377 memdc = CreateCompatibleDC(hdc);
1378 if(!memdc) goto cleanup;
1379 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1380 if(!hbmsave) goto cleanup;
1381 rc.left = rc.top = 0;
1382 rc.right = cx;
1383 rc.bottom = cy;
1384 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1385 SetBkColor(memdc, RGB(255, 255, 255));
1386 SetTextColor(memdc, RGB(0, 0, 0));
1387 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1389 /* DST_COMPLEX may draw text as well,
1390 * so we must be sure that correct font is selected
1392 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1393 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1394 if(hfsave) SelectObject(memdc, hfsave);
1395 if(!tmp) goto cleanup;
1397 /* This state cause the image to be dithered */
1398 if(flags & DSS_UNION)
1400 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1401 if(!hbsave) goto cleanup;
1402 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1403 SelectObject(memdc, hbsave);
1404 if(!tmp) goto cleanup;
1407 if (flags & DSS_DISABLED)
1408 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1409 else if (flags & DSS_DEFAULT)
1410 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1412 /* Draw light or dark shadow */
1413 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1415 if(!hbrtmp) goto cleanup;
1416 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1417 if(!hbsave) goto cleanup;
1418 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1419 SelectObject(hdc, hbsave);
1420 DeleteObject(hbrtmp);
1421 hbrtmp = 0;
1424 if (flags & DSS_DISABLED)
1426 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1427 if(!hbrtmp) goto cleanup;
1429 else if (!hbr)
1431 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1434 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1436 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1438 retval = TRUE; /* We succeeded */
1440 cleanup:
1441 SetTextColor(hdc, fg);
1442 SetBkColor(hdc, bg);
1444 if(hbsave) SelectObject(hdc, hbsave);
1445 if(hbmsave) SelectObject(memdc, hbmsave);
1446 if(hbrtmp) DeleteObject(hbrtmp);
1447 if(hbm) DeleteObject(hbm);
1448 if(memdc) DeleteDC(memdc);
1450 return retval;
1453 /**********************************************************************
1454 * DrawStateA (USER32.@)
1456 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1457 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1458 INT x, INT y, INT cx, INT cy, UINT flags)
1460 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1463 /**********************************************************************
1464 * DrawStateW (USER32.@)
1466 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1467 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1468 INT x, INT y, INT cx, INT cy, UINT flags)
1470 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1473 /***********************************************************************
1474 * SelectPalette (USER.282)
1476 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1477 BOOL16 bForceBackground )
1479 WORD wBkgPalette = 1;
1481 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1483 HWND hwnd = WindowFromDC( hDC );
1484 if (hwnd)
1486 HWND hForeground = GetForegroundWindow();
1487 /* set primary palette if it's related to current active */
1488 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1491 return GDISelectPalette16( hDC, hPal, wBkgPalette);
1495 /***********************************************************************
1496 * RealizePalette (USER.283)
1498 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1500 UINT16 realized = GDIRealizePalette16( hDC );
1502 /* do not send anything if no colors were changed */
1503 if (realized && IsDCCurrentPalette16( hDC ))
1505 /* send palette change notification */
1506 HWND hWnd = WindowFromDC( hDC );
1507 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0L);
1509 return realized;
1513 /***********************************************************************
1514 * UserRealizePalette (USER32.@)
1516 UINT WINAPI UserRealizePalette( HDC hDC )
1518 return RealizePalette16( hDC );