Removed a few remaining WINVER checks.
[wine/multimedia.git] / windows / painting.c
blobc95abddda1e307e529abc5330677d80f3110bd60
1 /*
2 * Window painting functions
4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <string.h>
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "wine/winuser16.h"
26 #include "wownt32.h"
27 #include "wine/unicode.h"
28 #include "wine/server.h"
29 #include "gdi.h"
30 #include "user.h"
31 #include "win.h"
32 #include "message.h"
33 #include "dce.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(win);
37 WINE_DECLARE_DEBUG_CHANNEL(nonclient);
39 /* client rect in window coordinates */
41 #define GETCLIENTRECTW( wnd, r ) (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
42 (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
43 (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
44 (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
46 /* PAINT_RedrawWindow() control flags */
47 #define RDW_EX_DELAY_NCPAINT 0x0020
49 /* WIN_UpdateNCRgn() flags */
50 #define UNC_CHECK 0x0001
51 #define UNC_ENTIRE 0x0002
52 #define UNC_REGION 0x0004
53 #define UNC_UPDATE 0x0008
54 #define UNC_DELAY_NCPAINT 0x0010
55 #define UNC_IN_BEGINPAINT 0x0020
57 /* Last COLOR id */
58 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
60 HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = NULL;
61 UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc) = NULL;
64 /***********************************************************************
65 * add_paint_count
67 * Add an increment (normally 1 or -1) to the current paint count of a window.
69 static void add_paint_count( HWND hwnd, int incr )
71 SERVER_START_REQ( inc_window_paint_count )
73 req->handle = hwnd;
74 req->incr = incr;
75 wine_server_call( req );
77 SERVER_END_REQ;
81 /***********************************************************************
82 * crop_rgn
84 * hSrc: Region to crop.
85 * lpRect: Clipping rectangle.
87 * hDst: Region to hold the result (a new region is created if it's 0).
88 * Allowed to be the same region as hSrc in which case everything
89 * will be done in place, with no memory reallocations.
91 * Returns: hDst if success, 0 otherwise.
93 static HRGN crop_rgn( HRGN hDst, HRGN hSrc, const RECT *rect )
95 HRGN h = CreateRectRgnIndirect( rect );
96 if (hDst == 0) hDst = h;
97 CombineRgn( hDst, hSrc, h, RGN_AND );
98 if (hDst != h) DeleteObject( h );
99 return hDst;
103 /***********************************************************************
104 * WIN_HaveToDelayNCPAINT
106 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
107 * is generated as soon as a region intersecting the non-client area
108 * of a window is invalidated.
110 * This technique will work fine for all windows whose parents
111 * have the WS_CLIPCHILDREN style. When the parents have that style,
112 * they are not going to override the contents of their children.
113 * However, when the parent doesn't have that style, Windows relies
114 * on a "painter's algorithm" to display the contents of the windows.
115 * That is, windows are painted from back to front. This includes the
116 * non-client area.
118 * This method looks at the current state of a window to determine
119 * if the sending of the WM_NCPAINT message should be delayed until
120 * the BeginPaint call.
122 * PARAMS:
123 * wndPtr - A Locked window pointer to the window we're
124 * examining.
125 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
126 * function. This is a shortcut for the cases when
127 * we already know when to avoid scanning all the
128 * parents of a window. If you already know that this
129 * window's NCPAINT should be delayed, set the
130 * UNC_DELAY_NCPAINT flag for this parameter.
132 * This shortcut behavior is implemented in the
133 * RDW_Paint() method.
136 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
139 * Test the shortcut first. (duh)
141 if (uncFlags & UNC_DELAY_NCPAINT)
142 return TRUE;
145 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
146 * method only. This is another shortcut to avoid going
147 * up the parent's chain of the window to finally
148 * figure-out that none of them has an invalid region.
150 if (uncFlags & UNC_IN_BEGINPAINT)
151 return FALSE;
154 * Scan all the parents of this window to find a window
155 * that doesn't have the WS_CLIPCHILDREN style and that
156 * has an invalid region.
158 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
160 WND* parentWnd = WIN_FindWndPtr( hwnd );
161 if (parentWnd && !(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
163 WIN_ReleaseWndPtr( parentWnd );
164 return TRUE;
166 WIN_ReleaseWndPtr( parentWnd );
168 return FALSE;
171 /***********************************************************************
172 * WIN_UpdateNCRgn
174 * Things to do:
175 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
176 * Crop hrgnUpdate to a client rect, especially if it 1.
177 * If UNC_REGION is set return update region for the client rect.
179 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
180 * The trick is that when the returned region handle may be different from hRgn.
181 * In this case the old hRgn must be considered gone. BUT, if the returned value
182 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
183 * a DC without extra clipping region.
185 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
187 RECT r;
188 HRGN hClip = 0;
189 HRGN hrgnRet = 0;
191 TRACE_(nonclient)("hwnd %p [%p] hrgn %p, unc %04x, ncf %i\n",
192 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
194 /* desktop window doesn't have a nonclient area */
195 if(wnd->hwndSelf == GetDesktopWindow())
197 wnd->flags &= ~WIN_NEEDS_NCPAINT;
198 if( wnd->hrgnUpdate > (HRGN)1 )
200 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
201 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
202 hrgnRet = hRgn;
204 else
206 hrgnRet = wnd->hrgnUpdate;
208 return hrgnRet;
211 if ((wnd->hwndSelf == GetForegroundWindow()) &&
212 !(wnd->flags & WIN_NCACTIVATED) )
214 wnd->flags |= WIN_NCACTIVATED;
215 uncFlags |= UNC_ENTIRE;
219 * If the window's non-client area needs to be painted,
221 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
222 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
224 RECT r2, r3;
226 wnd->flags &= ~WIN_NEEDS_NCPAINT;
227 GETCLIENTRECTW( wnd, r );
229 TRACE_(nonclient)( "\tclient box (%ld,%ld-%ld,%ld), hrgnUpdate %p\n",
230 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
231 if( wnd->hrgnUpdate > (HRGN)1 )
233 /* Check if update rgn overlaps with nonclient area */
235 GetRgnBox( wnd->hrgnUpdate, &r2 );
236 UnionRect( &r3, &r2, &r );
237 if( r3.left != r.left || r3.top != r.top ||
238 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
240 /* crop hrgnUpdate, save old one in hClip - the only
241 * case that places a valid region handle in hClip */
243 hClip = wnd->hrgnUpdate;
244 wnd->hrgnUpdate = crop_rgn( hRgn, hClip, &r );
245 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
248 if( uncFlags & UNC_CHECK )
250 GetRgnBox( wnd->hrgnUpdate, &r3 );
251 if( IsRectEmpty( &r3 ) )
253 /* delete the update region since all invalid
254 * parts were in the nonclient area */
256 DeleteObject( wnd->hrgnUpdate );
257 wnd->hrgnUpdate = 0;
258 if(!(wnd->flags & WIN_INTERNAL_PAINT))
259 add_paint_count( wnd->hwndSelf, -1 );
261 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
265 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
267 else
268 if( wnd->hrgnUpdate == (HRGN)1 )/* entire window */
270 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
271 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
272 uncFlags |= UNC_ENTIRE;
275 else /* no WM_NCPAINT unless forced */
277 if( wnd->hrgnUpdate > (HRGN)1 )
279 copyrgn:
280 if( uncFlags & UNC_REGION )
282 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
283 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
284 hrgnRet = hRgn;
287 else
288 if( wnd->hrgnUpdate == (HRGN)1 && (uncFlags & UNC_UPDATE) )
290 GETCLIENTRECTW( wnd, r );
291 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
292 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
296 if(!hClip && (uncFlags & UNC_ENTIRE) )
298 /* still don't do anything if there is no nonclient area */
299 hClip = (HRGN)(memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
302 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
304 if ( hClip == hrgnRet && hrgnRet > (HRGN)1 ) {
305 hClip = CreateRectRgn( 0, 0, 0, 0 );
306 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
309 SendMessageA( wnd->hwndSelf, WM_NCPAINT, (WPARAM)hClip, 0L );
310 if( (hClip > (HRGN)1) && (hClip != hRgn) && (hClip != hrgnRet) )
311 DeleteObject( hClip );
313 * Since all Window locks are suspended while processing the WM_NCPAINT
314 * we want to make sure the window still exists before continuing.
316 if (!IsWindow(wnd->hwndSelf))
318 DeleteObject(hrgnRet);
319 hrgnRet=0;
323 TRACE_(nonclient)("returning %p (hClip = %p, hrgnUpdate = %p)\n", hrgnRet, hClip, wnd->hrgnUpdate );
325 return hrgnRet;
329 /***********************************************************************
330 * RDW_ValidateParent [RDW_UpdateRgns() helper]
332 * Validate the portions of parents that are covered by a validated child
333 * wndPtr = child
335 static void RDW_ValidateParent(WND *wndChild)
337 HWND parent;
338 HRGN hrg;
340 if (wndChild->hrgnUpdate == (HRGN)1 ) {
341 RECT r;
342 r.left = 0;
343 r.top = 0;
344 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
345 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
346 hrg = CreateRectRgnIndirect( &r );
347 } else
348 hrg = wndChild->hrgnUpdate;
350 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
351 while (parent && parent != GetDesktopWindow())
353 WND *wndParent = WIN_FindWndPtr( parent );
354 if (wndParent && !(wndParent->dwStyle & WS_CLIPCHILDREN))
356 if (wndParent->hrgnUpdate != 0)
358 POINT ptOffset;
359 RECT rect, rectParent;
360 if( wndParent->hrgnUpdate == (HRGN)1 )
362 RECT r;
364 r.left = 0;
365 r.top = 0;
366 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
367 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
369 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
371 /* we must offset the child region by the offset of the child rect in the parent */
372 GetWindowRect(wndParent->hwndSelf, &rectParent);
373 GetWindowRect(wndChild->hwndSelf, &rect);
374 ptOffset.x = rect.left - rectParent.left;
375 ptOffset.y = rect.top - rectParent.top;
376 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
377 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
378 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
381 WIN_ReleaseWndPtr( wndParent );
382 parent = GetAncestor( parent, GA_PARENT );
384 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
387 /***********************************************************************
388 * RDW_UpdateRgns [RedrawWindow() helper]
390 * Walks the window tree and adds/removes parts of the hRgn to/from update
391 * regions of windows that overlap it. Also, manages internal paint flags.
393 * NOTE: Walks the window tree so the caller must lock it.
394 * MUST preserve hRgn (can modify but then has to restore).
396 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
399 * Called only when one of the following is set:
400 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
403 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
404 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
405 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
406 RECT r;
408 r.left = 0;
409 r.top = 0;
410 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
411 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
413 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
415 if( flags & RDW_INVALIDATE )
417 if( hRgn > (HRGN)1 )
419 switch ((UINT)wndPtr->hrgnUpdate)
421 default:
422 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
423 /* fall through */
424 case 0:
425 wndPtr->hrgnUpdate = crop_rgn( wndPtr->hrgnUpdate,
426 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
427 &r );
428 if( !bHadOne )
430 GetRgnBox( wndPtr->hrgnUpdate, &r );
431 if( IsRectEmpty( &r ) )
433 DeleteObject( wndPtr->hrgnUpdate );
434 wndPtr->hrgnUpdate = 0;
435 goto end;
438 break;
439 case 1: /* already an entire window */
440 break;
443 else if( hRgn == (HRGN)1 )
445 if( wndPtr->hrgnUpdate > (HRGN)1 )
446 DeleteObject( wndPtr->hrgnUpdate );
447 wndPtr->hrgnUpdate = (HRGN)1;
449 else
450 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
452 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
453 add_paint_count( wndPtr->hwndSelf, 1 );
455 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
456 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
457 flags |= RDW_FRAME;
459 else if( flags & RDW_VALIDATE )
461 if( wndPtr->hrgnUpdate )
463 if( hRgn > (HRGN)1 )
465 if( wndPtr->hrgnUpdate == (HRGN)1 )
466 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
468 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
469 == NULLREGION )
471 DeleteObject( wndPtr->hrgnUpdate );
472 wndPtr->hrgnUpdate = 0;
475 else /* validate everything */
477 if( wndPtr->hrgnUpdate > (HRGN)1 ) DeleteObject( wndPtr->hrgnUpdate );
478 wndPtr->hrgnUpdate = 0;
481 if( !wndPtr->hrgnUpdate )
483 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
484 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
485 add_paint_count( wndPtr->hwndSelf, -1 );
489 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
490 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
494 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
495 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
497 /* in/validate child windows that intersect with the region if it
498 * is a valid handle. */
500 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
502 HWND *list;
503 if( hRgn > (HRGN)1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
505 POINT ptTotal, prevOrigin = {0,0};
506 POINT ptClient;
507 INT i;
509 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
510 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
512 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
514 WND *wnd = WIN_FindWndPtr( list[i] );
515 if (!wnd) continue;
516 if( wnd->dwStyle & WS_VISIBLE )
518 POINT ptOffset;
520 r.left = wnd->rectWindow.left + ptClient.x;
521 r.right = wnd->rectWindow.right + ptClient.x;
522 r.top = wnd->rectWindow.top + ptClient.y;
523 r.bottom = wnd->rectWindow.bottom + ptClient.y;
525 ptOffset.x = r.left - prevOrigin.x;
526 ptOffset.y = r.top - prevOrigin.y;
527 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
529 if( RectInRegion( hRgn, &r ) )
531 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
532 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
533 prevOrigin.x = r.left + ptTotal.x;
534 prevOrigin.y = r.top + ptTotal.y;
535 ptTotal.x += ptOffset.x;
536 ptTotal.y += ptOffset.y;
539 WIN_ReleaseWndPtr( wnd );
541 HeapFree( GetProcessHeap(), 0, list );
542 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
543 bChildren = 0;
547 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
549 if( bChildren )
551 HWND *list;
552 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
554 INT i;
555 for (i = 0; list[i]; i++)
557 WND *wnd = WIN_FindWndPtr( list[i] );
558 if (!wnd) continue;
559 if( wnd->dwStyle & WS_VISIBLE )
560 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
561 WIN_ReleaseWndPtr( wnd );
563 HeapFree( GetProcessHeap(), 0, list );
567 end:
569 /* Set/clear internal paint flag */
571 if (flags & RDW_INTERNALPAINT)
573 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
574 add_paint_count( wndPtr->hwndSelf, 1 );
575 wndPtr->flags |= WIN_INTERNAL_PAINT;
577 else if (flags & RDW_NOINTERNALPAINT)
579 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
580 add_paint_count( wndPtr->hwndSelf, -1 );
581 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
585 /***********************************************************************
586 * RDW_Paint [RedrawWindow() helper]
588 * Walks the window tree and paints/erases windows that have
589 * nonzero update regions according to redraw flags. hrgn is a scratch
590 * region passed down during recursion. Must not be 1.
593 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
595 /* NOTE: wndPtr is locked by caller.
597 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
598 * SendMessage() calls. This is a comment inside DefWindowProc() source
599 * from 16-bit SDK:
601 * This message avoids lots of inter-app message traffic
602 * by switching to the other task and continuing the
603 * recursion there.
605 * wParam = flags
606 * LOWORD(lParam) = hrgnClip
607 * HIWORD(lParam) = hwndSkip (not used; always NULL)
610 HDC hDC;
611 HWND hWnd = wndPtr->hwndSelf;
612 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
614 /* Erase/update the window itself ... */
616 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
619 * Check if this window should delay it's processing of WM_NCPAINT.
620 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
622 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
623 ex |= RDW_EX_DELAY_NCPAINT;
625 if (flags & RDW_UPDATENOW)
627 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
628 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
630 else if (flags & RDW_ERASENOW)
632 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
633 HRGN hrgnRet;
635 hrgnRet = WIN_UpdateNCRgn(wndPtr,
636 hrgn,
637 UNC_REGION | UNC_CHECK |
638 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
640 if( hrgnRet )
642 if( hrgnRet > (HRGN)1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
643 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
645 if( bIcon ) dcx |= DCX_WINDOW;
646 else
647 if( hrgnRet )
648 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
649 wndPtr->rectWindow.top - wndPtr->rectClient.top);
650 else
651 dcx &= ~DCX_INTERSECTRGN;
652 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
654 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
655 (WPARAM)hDC, 0 ))
656 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
657 ReleaseDC( hWnd, hDC );
663 if( !IsWindow(hWnd) ) return hrgn;
665 /* ... and its child windows */
667 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
668 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
670 HWND *list, *phwnd;
672 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
674 for (phwnd = list; *phwnd; phwnd++)
676 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
677 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
678 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
679 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
680 WIN_ReleaseWndPtr(wndPtr);
682 HeapFree( GetProcessHeap(), 0, list );
686 return hrgn;
690 /***********************************************************************
691 * dump_rdw_flags
693 static void dump_rdw_flags(UINT flags)
695 TRACE("flags:");
696 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
697 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
698 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
699 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
700 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
701 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
702 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
703 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
704 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
705 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
706 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
707 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
709 #define RDW_FLAGS \
710 (RDW_INVALIDATE | \
711 RDW_INTERNALPAINT | \
712 RDW_ERASE | \
713 RDW_VALIDATE | \
714 RDW_NOINTERNALPAINT | \
715 RDW_NOERASE | \
716 RDW_NOCHILDREN | \
717 RDW_ALLCHILDREN | \
718 RDW_UPDATENOW | \
719 RDW_ERASENOW | \
720 RDW_FRAME | \
721 RDW_NOFRAME)
723 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
724 TRACE("\n");
725 #undef RDW_FLAGS
729 /***********************************************************************
730 * RedrawWindow (USER32.@)
732 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
733 HRGN hrgnUpdate, UINT flags )
735 HRGN hRgn = 0;
736 RECT r, r2;
737 POINT pt;
738 WND* wndPtr;
740 if (!hwnd) hwnd = GetDesktopWindow();
742 /* check if the window or its parents are visible/not minimized */
744 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
746 /* process pending events and messages before painting */
747 if (flags & RDW_UPDATENOW)
748 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
750 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
751 if (TRACE_ON(win))
753 if( hrgnUpdate )
755 GetRgnBox( hrgnUpdate, &r );
756 TRACE( "%p (%p) NULL %p box (%ld,%ld-%ld,%ld) flags=%04x\n",
757 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
759 else
761 if( rectUpdate )
762 r = *rectUpdate;
763 else
764 SetRectEmpty( &r );
765 TRACE( "%p (%p) %s %ld,%ld-%ld,%ld %p flags=%04x\n",
766 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
767 r.top, r.right, r.bottom, hrgnUpdate, flags );
769 dump_rdw_flags(flags);
772 /* prepare an update region in window coordinates */
774 if (((flags & (RDW_INVALIDATE|RDW_FRAME)) == (RDW_INVALIDATE|RDW_FRAME)) ||
775 ((flags & (RDW_VALIDATE|RDW_NOFRAME)) == (RDW_VALIDATE|RDW_NOFRAME)))
776 r = wndPtr->rectWindow;
777 else
778 r = wndPtr->rectClient;
780 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
781 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
782 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
784 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
786 /* If the window doesn't have hrgnUpdate we leave hRgn zero
787 * and put a new region straight into wndPtr->hrgnUpdate
788 * so that RDW_UpdateRgns() won't have to do any extra work.
791 if( hrgnUpdate )
793 if( wndPtr->hrgnUpdate )
795 hRgn = CreateRectRgn( 0, 0, 0, 0 );
796 CombineRgn( hRgn, hrgnUpdate, 0, RGN_COPY );
797 OffsetRgn( hRgn, pt.x, pt.y );
799 else
801 wndPtr->hrgnUpdate = crop_rgn( 0, hrgnUpdate, &r );
802 OffsetRgn( wndPtr->hrgnUpdate, pt.x, pt.y );
805 else if( rectUpdate )
807 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
808 OffsetRect( &r2, pt.x, pt.y );
809 if( wndPtr->hrgnUpdate == 0 )
810 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
811 else
812 hRgn = CreateRectRgnIndirect( &r2 );
814 else /* entire window or client depending on RDW_FRAME */
816 if( flags & RDW_FRAME )
818 if (wndPtr->hrgnUpdate) hRgn = (HRGN)1;
819 else wndPtr->hrgnUpdate = (HRGN)1;
821 else
823 GETCLIENTRECTW( wndPtr, r2 );
824 if( wndPtr->hrgnUpdate == 0 )
825 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
826 else
827 hRgn = CreateRectRgnIndirect( &r2 );
831 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
833 /* In this we cannot leave with zero hRgn */
834 if( hrgnUpdate )
836 hRgn = crop_rgn( hRgn, hrgnUpdate, &r );
837 OffsetRgn( hRgn, pt.x, pt.y );
838 GetRgnBox( hRgn, &r2 );
839 if( IsRectEmpty( &r2 ) ) goto END;
841 else if( rectUpdate )
843 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
844 OffsetRect( &r2, pt.x, pt.y );
845 hRgn = CreateRectRgnIndirect( &r2 );
847 else /* entire window or client depending on RDW_NOFRAME */
849 if( flags & RDW_NOFRAME )
850 hRgn = (HRGN)1;
851 else
853 GETCLIENTRECTW( wndPtr, r2 );
854 hRgn = CreateRectRgnIndirect( &r2 );
859 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
861 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
863 /* Erase/update windows, from now on hRgn is a scratch region */
865 hRgn = RDW_Paint( wndPtr, (hRgn == (HRGN)1) ? 0 : hRgn, flags, 0 );
867 END:
868 if( hRgn > (HRGN)1 && (hRgn != hrgnUpdate) )
869 DeleteObject(hRgn );
870 WIN_ReleaseWndPtr(wndPtr);
871 return TRUE;
875 /***********************************************************************
876 * UpdateWindow (USER32.@)
878 BOOL WINAPI UpdateWindow( HWND hwnd )
880 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
884 /***********************************************************************
885 * InvalidateRgn (USER32.@)
887 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
889 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
893 /***********************************************************************
894 * InvalidateRect (USER32.@)
896 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
898 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
902 /***********************************************************************
903 * ValidateRgn (USER32.@)
905 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
907 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
911 /***********************************************************************
912 * ValidateRect (USER32.@)
914 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
916 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
920 /***********************************************************************
921 * GetUpdateRect (USER32.@)
923 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
925 BOOL retvalue;
926 WND * wndPtr = WIN_FindWndPtr( hwnd );
927 if (!wndPtr) return FALSE;
929 if (rect)
931 if (wndPtr->hrgnUpdate > (HRGN)1)
933 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
934 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
936 retvalue = FALSE;
937 goto END;
939 GetRgnBox( hrgn, rect );
940 DeleteObject( hrgn );
941 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
943 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
945 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
949 else
950 if( wndPtr->hrgnUpdate == (HRGN)1 )
952 GetClientRect( hwnd, rect );
953 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
955 else
956 SetRectEmpty( rect );
958 retvalue = (wndPtr->hrgnUpdate >= (HRGN)1);
959 END:
960 WIN_ReleaseWndPtr(wndPtr);
961 return retvalue;
965 /***********************************************************************
966 * GetUpdateRgn (USER32.@)
968 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
970 INT retval;
971 WND * wndPtr = WIN_FindWndPtr( hwnd );
972 if (!wndPtr) return ERROR;
974 if (wndPtr->hrgnUpdate == 0)
976 SetRectRgn( hrgn, 0, 0, 0, 0 );
977 retval = NULLREGION;
978 goto END;
980 else
981 if (wndPtr->hrgnUpdate == (HRGN)1)
983 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
984 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
985 retval = SIMPLEREGION;
987 else
989 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
990 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
991 wndPtr->rectWindow.top - wndPtr->rectClient.top );
993 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
994 END:
995 WIN_ReleaseWndPtr(wndPtr);
996 return retval;
1000 /***********************************************************************
1001 * ExcludeUpdateRgn (USER32.@)
1003 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1005 RECT rect;
1006 WND * wndPtr;
1008 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1010 if (wndPtr->hrgnUpdate)
1012 INT ret;
1013 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1014 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1015 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1016 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1017 if( wndPtr->hrgnUpdate > (HRGN)1 )
1019 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1020 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1021 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1024 /* do ugly coordinate translations in dce.c */
1026 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
1027 DeleteObject( hrgn );
1028 WIN_ReleaseWndPtr(wndPtr);
1029 return ret;
1031 WIN_ReleaseWndPtr(wndPtr);
1032 return GetClipBox( hdc, &rect );
1037 /***********************************************************************
1038 * FillRect (USER.81)
1039 * NOTE
1040 * The Win16 variant doesn't support special color brushes like
1041 * the Win32 one, despite the fact that Win16, as well as Win32,
1042 * supports special background brushes for a window class.
1044 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1046 HBRUSH prevBrush;
1048 /* coordinates are logical so we cannot fast-check 'rect',
1049 * it will be done later in the PatBlt().
1052 if (!(prevBrush = SelectObject( HDC_32(hdc), HBRUSH_32(hbrush) ))) return 0;
1053 PatBlt( HDC_32(hdc), rect->left, rect->top,
1054 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1055 SelectObject( HDC_32(hdc), prevBrush );
1056 return 1;
1060 /***********************************************************************
1061 * FillRect (USER32.@)
1063 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1065 HBRUSH prevBrush;
1067 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1068 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1071 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1072 PatBlt( hdc, rect->left, rect->top,
1073 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1074 SelectObject( hdc, prevBrush );
1075 return 1;
1079 /***********************************************************************
1080 * InvertRect (USER.82)
1082 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1084 PatBlt( HDC_32(hdc), rect->left, rect->top,
1085 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1089 /***********************************************************************
1090 * InvertRect (USER32.@)
1092 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1094 return PatBlt( hdc, rect->left, rect->top,
1095 rect->right - rect->left, rect->bottom - rect->top,
1096 DSTINVERT );
1100 /***********************************************************************
1101 * FrameRect (USER32.@)
1103 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1105 HBRUSH prevBrush;
1106 RECT r = *rect;
1108 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1109 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1111 PatBlt( hdc, r.left, r.top, 1,
1112 r.bottom - r.top, PATCOPY );
1113 PatBlt( hdc, r.right - 1, r.top, 1,
1114 r.bottom - r.top, PATCOPY );
1115 PatBlt( hdc, r.left, r.top,
1116 r.right - r.left, 1, PATCOPY );
1117 PatBlt( hdc, r.left, r.bottom - 1,
1118 r.right - r.left, 1, PATCOPY );
1120 SelectObject( hdc, prevBrush );
1121 return TRUE;
1125 /***********************************************************************
1126 * FrameRect (USER.83)
1128 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1130 RECT rect;
1131 CONV_RECT16TO32( rect16, &rect );
1132 return FrameRect( HDC_32(hdc), &rect, HBRUSH_32(hbrush) );
1136 /***********************************************************************
1137 * DrawFocusRect (USER.466)
1139 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1141 RECT rect32;
1142 CONV_RECT16TO32( rc, &rect32 );
1143 DrawFocusRect( HDC_32(hdc), &rect32 );
1147 /***********************************************************************
1148 * DrawFocusRect (USER32.@)
1150 * FIXME: PatBlt(PATINVERT) with background brush.
1152 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1154 HBRUSH hOldBrush;
1155 HPEN hOldPen, hNewPen;
1156 INT oldDrawMode, oldBkMode;
1158 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1159 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1160 hOldPen = SelectObject(hdc, hNewPen);
1161 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1162 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1164 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1166 SetBkMode(hdc, oldBkMode);
1167 SetROP2(hdc, oldDrawMode);
1168 SelectObject(hdc, hOldPen);
1169 DeleteObject(hNewPen);
1170 SelectObject(hdc, hOldBrush);
1172 return TRUE;
1176 /**********************************************************************
1177 * DrawAnimatedRects (USER32.@)
1179 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1180 const RECT* lprcFrom,
1181 const RECT* lprcTo )
1183 FIXME_(win)("(%p,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1184 return TRUE;
1188 /**********************************************************************
1189 * PAINTING_DrawStateJam
1191 * Jams in the requested type in the dc
1193 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1194 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1195 LPRECT rc, UINT dtflags, BOOL unicode )
1197 HDC memdc;
1198 HBITMAP hbmsave;
1199 BOOL retval;
1200 INT cx = rc->right - rc->left;
1201 INT cy = rc->bottom - rc->top;
1203 switch(opcode)
1205 case DST_TEXT:
1206 case DST_PREFIXTEXT:
1207 if(unicode)
1208 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1209 else
1210 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1212 case DST_ICON:
1213 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1215 case DST_BITMAP:
1216 memdc = CreateCompatibleDC(hdc);
1217 if(!memdc) return FALSE;
1218 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1219 if(!hbmsave)
1221 DeleteDC(memdc);
1222 return FALSE;
1224 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1225 SelectObject(memdc, hbmsave);
1226 DeleteDC(memdc);
1227 return retval;
1229 case DST_COMPLEX:
1230 if(func) {
1231 BOOL bRet;
1232 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1234 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1235 bRet = func(hdc, lp, wp, cx, cy);
1236 /* Restore origin */
1237 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1238 return bRet;
1239 } else
1240 return FALSE;
1242 return FALSE;
1245 /**********************************************************************
1246 * PAINTING_DrawState()
1248 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1249 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1251 HBITMAP hbm, hbmsave;
1252 HFONT hfsave;
1253 HBRUSH hbsave, hbrtmp = 0;
1254 HDC memdc;
1255 RECT rc;
1256 UINT dtflags = DT_NOCLIP;
1257 COLORREF fg, bg;
1258 UINT opcode = flags & 0xf;
1259 INT len = wp;
1260 BOOL retval, tmp;
1262 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1264 if(unicode)
1265 len = strlenW((LPWSTR)lp);
1266 else
1267 len = strlen((LPSTR)lp);
1270 /* Find out what size the image has if not given by caller */
1271 if(!cx || !cy)
1273 SIZE s;
1274 CURSORICONINFO *ici;
1275 BITMAP bm;
1277 switch(opcode)
1279 case DST_TEXT:
1280 case DST_PREFIXTEXT:
1281 if(unicode)
1282 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1283 else
1284 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1285 if(!retval) return FALSE;
1286 break;
1288 case DST_ICON:
1289 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1290 if(!ici) return FALSE;
1291 s.cx = ici->nWidth;
1292 s.cy = ici->nHeight;
1293 GlobalUnlock16((HGLOBAL16)lp);
1294 break;
1296 case DST_BITMAP:
1297 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1298 return FALSE;
1299 s.cx = bm.bmWidth;
1300 s.cy = bm.bmHeight;
1301 break;
1303 case DST_COMPLEX: /* cx and cy must be set in this mode */
1304 return FALSE;
1307 if(!cx) cx = s.cx;
1308 if(!cy) cy = s.cy;
1311 rc.left = x;
1312 rc.top = y;
1313 rc.right = x + cx;
1314 rc.bottom = y + cy;
1316 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1317 dtflags |= DT_RIGHT;
1318 if(opcode == DST_TEXT)
1319 dtflags |= DT_NOPREFIX;
1321 /* For DSS_NORMAL we just jam in the image and return */
1322 if((flags & 0x7ff0) == DSS_NORMAL)
1324 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1327 /* For all other states we need to convert the image to B/W in a local bitmap */
1328 /* before it is displayed */
1329 fg = SetTextColor(hdc, RGB(0, 0, 0));
1330 bg = SetBkColor(hdc, RGB(255, 255, 255));
1331 hbm = NULL; hbmsave = NULL;
1332 memdc = NULL; hbsave = NULL;
1333 retval = FALSE; /* assume failure */
1335 /* From here on we must use "goto cleanup" when something goes wrong */
1336 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1337 if(!hbm) goto cleanup;
1338 memdc = CreateCompatibleDC(hdc);
1339 if(!memdc) goto cleanup;
1340 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1341 if(!hbmsave) goto cleanup;
1342 rc.left = rc.top = 0;
1343 rc.right = cx;
1344 rc.bottom = cy;
1345 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1346 SetBkColor(memdc, RGB(255, 255, 255));
1347 SetTextColor(memdc, RGB(0, 0, 0));
1348 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1350 /* DST_COMPLEX may draw text as well,
1351 * so we must be sure that correct font is selected
1353 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1354 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1355 if(hfsave) SelectObject(memdc, hfsave);
1356 if(!tmp) goto cleanup;
1358 /* This state cause the image to be dithered */
1359 if(flags & DSS_UNION)
1361 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1362 if(!hbsave) goto cleanup;
1363 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1364 SelectObject(memdc, hbsave);
1365 if(!tmp) goto cleanup;
1368 if (flags & DSS_DISABLED)
1369 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1370 else if (flags & DSS_DEFAULT)
1371 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1373 /* Draw light or dark shadow */
1374 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1376 if(!hbrtmp) goto cleanup;
1377 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1378 if(!hbsave) goto cleanup;
1379 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1380 SelectObject(hdc, hbsave);
1381 DeleteObject(hbrtmp);
1382 hbrtmp = 0;
1385 if (flags & DSS_DISABLED)
1387 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1388 if(!hbrtmp) goto cleanup;
1390 else if (!hbr)
1392 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1395 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1397 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1399 retval = TRUE; /* We succeeded */
1401 cleanup:
1402 SetTextColor(hdc, fg);
1403 SetBkColor(hdc, bg);
1405 if(hbsave) SelectObject(hdc, hbsave);
1406 if(hbmsave) SelectObject(memdc, hbmsave);
1407 if(hbrtmp) DeleteObject(hbrtmp);
1408 if(hbm) DeleteObject(hbm);
1409 if(memdc) DeleteDC(memdc);
1411 return retval;
1414 /**********************************************************************
1415 * DrawStateA (USER32.@)
1417 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1418 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1419 INT x, INT y, INT cx, INT cy, UINT flags)
1421 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1424 /**********************************************************************
1425 * DrawStateW (USER32.@)
1427 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1428 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1429 INT x, INT y, INT cx, INT cy, UINT flags)
1431 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1435 /***********************************************************************
1436 * SelectPalette (Not a Windows API)
1438 HPALETTE WINAPI SelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
1440 WORD wBkgPalette = 1;
1442 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1444 HWND hwnd = WindowFromDC( hDC );
1445 if (hwnd)
1447 HWND hForeground = GetForegroundWindow();
1448 /* set primary palette if it's related to current active */
1449 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1452 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
1456 /***********************************************************************
1457 * UserRealizePalette (USER32.@)
1459 UINT WINAPI UserRealizePalette( HDC hDC )
1461 UINT realized = pfnGDIRealizePalette( hDC );
1463 /* do not send anything if no colors were changed */
1464 if (realized && IsDCCurrentPalette16( HDC_16(hDC) ))
1466 /* send palette change notification */
1467 HWND hWnd = WindowFromDC( hDC );
1468 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0L);
1470 return realized;