Add better NULL checks to lstrcmp[i](A/W) functions.
[wine/multimedia.git] / windows / painting.c
blob8b631b6cfcffa166851b2b9bb1693611f927c7f7
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 "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "wine/winuser16.h"
31 #include "wownt32.h"
32 #include "wine/unicode.h"
33 #include "wine/server.h"
34 #include "user.h"
35 #include "win.h"
36 #include "message.h"
37 #include "dce.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
41 WINE_DECLARE_DEBUG_CHANNEL(nonclient);
43 /* client rect in window coordinates */
45 #define GETCLIENTRECTW( wnd, r ) (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
46 (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
47 (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
48 (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
50 /* PAINT_RedrawWindow() control flags */
51 #define RDW_EX_DELAY_NCPAINT 0x0020
53 /* WIN_UpdateNCRgn() flags */
54 #define UNC_CHECK 0x0001
55 #define UNC_ENTIRE 0x0002
56 #define UNC_REGION 0x0004
57 #define UNC_UPDATE 0x0008
58 #define UNC_DELAY_NCPAINT 0x0010
59 #define UNC_IN_BEGINPAINT 0x0020
61 /* Last COLOR id */
62 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
64 HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = NULL;
65 UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc) = NULL;
68 /***********************************************************************
69 * add_paint_count
71 * Add an increment (normally 1 or -1) to the current paint count of a window.
73 static void add_paint_count( HWND hwnd, int incr )
75 SERVER_START_REQ( inc_window_paint_count )
77 req->handle = hwnd;
78 req->incr = incr;
79 wine_server_call( req );
81 SERVER_END_REQ;
85 /***********************************************************************
86 * crop_rgn
88 * hSrc: Region to crop.
89 * lpRect: Clipping rectangle.
91 * hDst: Region to hold the result (a new region is created if it's 0).
92 * Allowed to be the same region as hSrc in which case everything
93 * will be done in place, with no memory reallocations.
95 * Returns: hDst if success, 0 otherwise.
97 static HRGN crop_rgn( HRGN hDst, HRGN hSrc, const RECT *rect )
99 HRGN h = CreateRectRgnIndirect( rect );
100 if (hDst == 0) hDst = h;
101 CombineRgn( hDst, hSrc, h, RGN_AND );
102 if (hDst != h) DeleteObject( h );
103 return hDst;
107 /***********************************************************************
108 * WIN_HaveToDelayNCPAINT
110 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
111 * is generated as soon as a region intersecting the non-client area
112 * of a window is invalidated.
114 * This technique will work fine for all windows whose parents
115 * have the WS_CLIPCHILDREN style. When the parents have that style,
116 * they are not going to override the contents of their children.
117 * However, when the parent doesn't have that style, Windows relies
118 * on a "painter's algorithm" to display the contents of the windows.
119 * That is, windows are painted from back to front. This includes the
120 * non-client area.
122 * This method looks at the current state of a window to determine
123 * if the sending of the WM_NCPAINT message should be delayed until
124 * the BeginPaint call.
126 * PARAMS:
127 * wndPtr - A Locked window pointer to the window we're
128 * examining.
129 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
130 * function. This is a shortcut for the cases when
131 * we already know when to avoid scanning all the
132 * parents of a window. If you already know that this
133 * window's NCPAINT should be delayed, set the
134 * UNC_DELAY_NCPAINT flag for this parameter.
136 * This shortcut behavior is implemented in the
137 * RDW_Paint() method.
140 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
143 * Test the shortcut first. (duh)
145 if (uncFlags & UNC_DELAY_NCPAINT)
146 return TRUE;
149 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
150 * method only. This is another shortcut to avoid going
151 * up the parent's chain of the window to finally
152 * figure-out that none of them has an invalid region.
154 if (uncFlags & UNC_IN_BEGINPAINT)
155 return FALSE;
158 * Scan all the parents of this window to find a window
159 * that doesn't have the WS_CLIPCHILDREN style and that
160 * has an invalid region.
162 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
164 WND* parentWnd = WIN_FindWndPtr( hwnd );
165 if (parentWnd && !(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
167 WIN_ReleaseWndPtr( parentWnd );
168 return TRUE;
170 WIN_ReleaseWndPtr( parentWnd );
172 return FALSE;
175 /***********************************************************************
176 * WIN_UpdateNCRgn
178 * Things to do:
179 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
180 * Crop hrgnUpdate to a client rect, especially if it 1.
181 * If UNC_REGION is set return update region for the client rect.
183 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
184 * The trick is that when the returned region handle may be different from hRgn.
185 * In this case the old hRgn must be considered gone. BUT, if the returned value
186 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
187 * a DC without extra clipping region.
189 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
191 RECT r;
192 HRGN hClip = 0;
193 HRGN hrgnRet = 0;
195 TRACE_(nonclient)("hwnd %p [%p] hrgn %p, unc %04x, ncf %i\n",
196 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
198 /* desktop window doesn't have a nonclient area */
199 if(wnd->hwndSelf == GetDesktopWindow())
201 wnd->flags &= ~WIN_NEEDS_NCPAINT;
202 if( wnd->hrgnUpdate > (HRGN)1 )
204 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
205 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
206 hrgnRet = hRgn;
208 else
210 hrgnRet = wnd->hrgnUpdate;
212 return hrgnRet;
215 if ((wnd->hwndSelf == GetForegroundWindow()) &&
216 !(wnd->flags & WIN_NCACTIVATED) )
218 wnd->flags |= WIN_NCACTIVATED;
219 uncFlags |= UNC_ENTIRE;
223 * If the window's non-client area needs to be painted,
225 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
226 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
228 RECT r2, r3;
230 wnd->flags &= ~WIN_NEEDS_NCPAINT;
231 GETCLIENTRECTW( wnd, r );
233 TRACE_(nonclient)( "\tclient box (%ld,%ld-%ld,%ld), hrgnUpdate %p\n",
234 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
235 if( wnd->hrgnUpdate > (HRGN)1 )
237 /* Check if update rgn overlaps with nonclient area */
239 GetRgnBox( wnd->hrgnUpdate, &r2 );
240 UnionRect( &r3, &r2, &r );
241 if( r3.left != r.left || r3.top != r.top ||
242 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
244 /* crop hrgnUpdate, save old one in hClip - the only
245 * case that places a valid region handle in hClip */
247 hClip = wnd->hrgnUpdate;
248 wnd->hrgnUpdate = crop_rgn( hRgn, hClip, &r );
249 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
252 if( uncFlags & UNC_CHECK )
254 GetRgnBox( wnd->hrgnUpdate, &r3 );
255 if( IsRectEmpty( &r3 ) )
257 /* delete the update region since all invalid
258 * parts were in the nonclient area */
260 DeleteObject( wnd->hrgnUpdate );
261 wnd->hrgnUpdate = 0;
262 if(!(wnd->flags & WIN_INTERNAL_PAINT))
263 add_paint_count( wnd->hwndSelf, -1 );
265 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
269 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
271 else
272 if( wnd->hrgnUpdate == (HRGN)1 )/* entire window */
274 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
275 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
276 uncFlags |= UNC_ENTIRE;
279 else /* no WM_NCPAINT unless forced */
281 if( wnd->hrgnUpdate > (HRGN)1 )
283 copyrgn:
284 if( uncFlags & UNC_REGION )
286 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
287 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
288 hrgnRet = hRgn;
291 else
292 if( wnd->hrgnUpdate == (HRGN)1 && (uncFlags & UNC_UPDATE) )
294 GETCLIENTRECTW( wnd, r );
295 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
296 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
300 if(!hClip && (uncFlags & UNC_ENTIRE) )
302 /* still don't do anything if there is no nonclient area */
303 hClip = (HRGN)(memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
306 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
308 if ( hClip == hrgnRet && hrgnRet > (HRGN)1 ) {
309 hClip = CreateRectRgn( 0, 0, 0, 0 );
310 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
313 SendMessageA( wnd->hwndSelf, WM_NCPAINT, (WPARAM)hClip, 0L );
314 if( (hClip > (HRGN)1) && (hClip != hRgn) && (hClip != hrgnRet) )
315 DeleteObject( hClip );
317 * Since all Window locks are suspended while processing the WM_NCPAINT
318 * we want to make sure the window still exists before continuing.
320 if (!IsWindow(wnd->hwndSelf))
322 DeleteObject(hrgnRet);
323 hrgnRet=0;
327 TRACE_(nonclient)("returning %p (hClip = %p, hrgnUpdate = %p)\n", hrgnRet, hClip, wnd->hrgnUpdate );
329 return hrgnRet;
333 /***********************************************************************
334 * RDW_ValidateParent [RDW_UpdateRgns() helper]
336 * Validate the portions of parents that are covered by a validated child
337 * wndPtr = child
339 static void RDW_ValidateParent(WND *wndChild)
341 HWND parent;
342 HRGN hrg;
344 if (wndChild->hrgnUpdate == (HRGN)1 ) {
345 RECT r;
346 r.left = 0;
347 r.top = 0;
348 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
349 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
350 hrg = CreateRectRgnIndirect( &r );
351 } else
352 hrg = wndChild->hrgnUpdate;
354 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
355 while (parent && parent != GetDesktopWindow())
357 WND *wndParent = WIN_FindWndPtr( parent );
358 if (wndParent && !(wndParent->dwStyle & WS_CLIPCHILDREN))
360 if (wndParent->hrgnUpdate != 0)
362 POINT ptOffset;
363 RECT rect, rectParent;
364 if( wndParent->hrgnUpdate == (HRGN)1 )
366 RECT r;
368 r.left = 0;
369 r.top = 0;
370 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
371 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
373 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
375 /* we must offset the child region by the offset of the child rect in the parent */
376 GetWindowRect(wndParent->hwndSelf, &rectParent);
377 GetWindowRect(wndChild->hwndSelf, &rect);
378 ptOffset.x = rect.left - rectParent.left;
379 ptOffset.y = rect.top - rectParent.top;
380 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
381 if (CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF ) == NULLREGION)
383 /* the update region has become empty */
384 DeleteObject( wndParent->hrgnUpdate );
385 wndParent->hrgnUpdate = 0;
386 wndParent->flags &= ~WIN_NEEDS_ERASEBKGND;
387 if( !(wndParent->flags & WIN_INTERNAL_PAINT) )
388 add_paint_count( wndParent->hwndSelf, -1 );
390 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
393 WIN_ReleaseWndPtr( wndParent );
394 parent = GetAncestor( parent, GA_PARENT );
396 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
399 /***********************************************************************
400 * RDW_UpdateRgns [RedrawWindow() helper]
402 * Walks the window tree and adds/removes parts of the hRgn to/from update
403 * regions of windows that overlap it. Also, manages internal paint flags.
405 * NOTE: Walks the window tree so the caller must lock it.
406 * MUST preserve hRgn (can modify but then has to restore).
408 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
411 * Called only when one of the following is set:
412 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
415 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
416 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
417 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
418 RECT r;
420 r.left = 0;
421 r.top = 0;
422 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
423 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
425 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
427 if( flags & RDW_INVALIDATE )
429 if( hRgn > (HRGN)1 )
431 switch ((UINT)wndPtr->hrgnUpdate)
433 default:
434 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
435 /* fall through */
436 case 0:
437 wndPtr->hrgnUpdate = crop_rgn( wndPtr->hrgnUpdate,
438 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
439 &r );
440 if( !bHadOne )
442 GetRgnBox( wndPtr->hrgnUpdate, &r );
443 if( IsRectEmpty( &r ) )
445 DeleteObject( wndPtr->hrgnUpdate );
446 wndPtr->hrgnUpdate = 0;
447 goto end;
450 break;
451 case 1: /* already an entire window */
452 break;
455 else if( hRgn == (HRGN)1 )
457 if( wndPtr->hrgnUpdate > (HRGN)1 )
458 DeleteObject( wndPtr->hrgnUpdate );
459 wndPtr->hrgnUpdate = (HRGN)1;
461 else
463 /* hRgn is zero */
464 if( wndPtr->hrgnUpdate > (HRGN)1)
466 GetRgnBox( wndPtr->hrgnUpdate, &r );
467 if( IsRectEmpty( &r ) )
469 DeleteObject( wndPtr->hrgnUpdate );
470 wndPtr->hrgnUpdate = 0;
471 goto end;
474 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends
475 * on code in RDW_Paint() */
478 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
479 add_paint_count( wndPtr->hwndSelf, 1 );
481 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
482 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
483 flags |= RDW_FRAME;
485 else if( flags & RDW_VALIDATE )
487 if( wndPtr->hrgnUpdate )
489 if( hRgn > (HRGN)1 )
491 if( wndPtr->hrgnUpdate == (HRGN)1 )
492 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
494 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
495 == NULLREGION )
497 DeleteObject( wndPtr->hrgnUpdate );
498 wndPtr->hrgnUpdate = 0;
501 else /* validate everything */
503 if( wndPtr->hrgnUpdate > (HRGN)1 ) DeleteObject( wndPtr->hrgnUpdate );
504 wndPtr->hrgnUpdate = 0;
507 if( !wndPtr->hrgnUpdate )
509 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
510 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
511 add_paint_count( wndPtr->hwndSelf, -1 );
515 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
516 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
520 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
521 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
523 /* in/validate child windows that intersect with the region if it
524 * is a valid handle. */
526 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
528 HWND *list;
529 if( hRgn > (HRGN)1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
531 POINT ptTotal, prevOrigin = {0,0};
532 POINT ptClient;
533 INT i;
535 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
536 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
538 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
540 WND *wnd = WIN_FindWndPtr( list[i] );
541 if (!wnd) continue;
542 if( wnd->dwStyle & WS_VISIBLE )
544 POINT ptOffset;
546 r.left = wnd->rectWindow.left + ptClient.x;
547 r.right = wnd->rectWindow.right + ptClient.x;
548 r.top = wnd->rectWindow.top + ptClient.y;
549 r.bottom = wnd->rectWindow.bottom + ptClient.y;
551 ptOffset.x = r.left - prevOrigin.x;
552 ptOffset.y = r.top - prevOrigin.y;
553 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
555 if( RectInRegion( hRgn, &r ) )
557 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
558 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
559 prevOrigin.x = r.left + ptTotal.x;
560 prevOrigin.y = r.top + ptTotal.y;
561 ptTotal.x += ptOffset.x;
562 ptTotal.y += ptOffset.y;
565 WIN_ReleaseWndPtr( wnd );
567 HeapFree( GetProcessHeap(), 0, list );
568 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
569 bChildren = 0;
573 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
575 if( bChildren )
577 HWND *list;
578 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
580 INT i;
581 for (i = 0; list[i]; i++)
583 WND *wnd = WIN_FindWndPtr( list[i] );
584 if (!wnd) continue;
585 if( wnd->dwStyle & WS_VISIBLE )
586 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
587 WIN_ReleaseWndPtr( wnd );
589 HeapFree( GetProcessHeap(), 0, list );
593 end:
595 /* Set/clear internal paint flag */
597 if (flags & RDW_INTERNALPAINT)
599 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
600 add_paint_count( wndPtr->hwndSelf, 1 );
601 wndPtr->flags |= WIN_INTERNAL_PAINT;
603 else if (flags & RDW_NOINTERNALPAINT)
605 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
606 add_paint_count( wndPtr->hwndSelf, -1 );
607 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
611 /***********************************************************************
612 * RDW_Paint [RedrawWindow() helper]
614 * Walks the window tree and paints/erases windows that have
615 * nonzero update regions according to redraw flags. hrgn is a scratch
616 * region passed down during recursion. Must not be 1.
619 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
621 /* NOTE: wndPtr is locked by caller.
623 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
624 * SendMessage() calls. This is a comment inside DefWindowProc() source
625 * from 16-bit SDK:
627 * This message avoids lots of inter-app message traffic
628 * by switching to the other task and continuing the
629 * recursion there.
631 * wParam = flags
632 * LOWORD(lParam) = hrgnClip
633 * HIWORD(lParam) = hwndSkip (not used; always NULL)
636 HDC hDC;
637 HWND hWnd = wndPtr->hwndSelf;
638 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
640 /* Erase/update the window itself ... */
642 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
645 * Check if this window should delay it's processing of WM_NCPAINT.
646 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
648 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
649 ex |= RDW_EX_DELAY_NCPAINT;
651 if (flags & RDW_UPDATENOW)
653 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
654 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
656 else if (flags & RDW_ERASENOW)
658 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
659 HRGN hrgnRet;
661 hrgnRet = WIN_UpdateNCRgn(wndPtr,
662 hrgn,
663 UNC_REGION | UNC_CHECK |
664 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
666 if( hrgnRet )
668 if( hrgnRet > (HRGN)1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
669 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
671 if( bIcon ) dcx |= DCX_WINDOW;
672 else
673 if( hrgnRet )
674 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
675 wndPtr->rectWindow.top - wndPtr->rectClient.top);
676 else
677 dcx &= ~DCX_INTERSECTRGN;
678 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
680 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
681 (WPARAM)hDC, 0 ))
682 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
683 ReleaseDC( hWnd, hDC );
689 if( !IsWindow(hWnd) ) return hrgn;
691 /* ... and its child windows */
693 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
694 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
696 HWND *list, *phwnd;
698 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
700 for (phwnd = list; *phwnd; phwnd++)
702 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
703 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
704 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
705 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
706 WIN_ReleaseWndPtr(wndPtr);
708 HeapFree( GetProcessHeap(), 0, list );
712 return hrgn;
716 /***********************************************************************
717 * dump_rdw_flags
719 static void dump_rdw_flags(UINT flags)
721 TRACE("flags:");
722 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
723 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
724 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
725 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
726 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
727 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
728 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
729 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
730 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
731 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
732 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
733 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
735 #define RDW_FLAGS \
736 (RDW_INVALIDATE | \
737 RDW_INTERNALPAINT | \
738 RDW_ERASE | \
739 RDW_VALIDATE | \
740 RDW_NOINTERNALPAINT | \
741 RDW_NOERASE | \
742 RDW_NOCHILDREN | \
743 RDW_ALLCHILDREN | \
744 RDW_UPDATENOW | \
745 RDW_ERASENOW | \
746 RDW_FRAME | \
747 RDW_NOFRAME)
749 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
750 TRACE("\n");
751 #undef RDW_FLAGS
755 /***********************************************************************
756 * RedrawWindow (USER32.@)
758 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
759 HRGN hrgnUpdate, UINT flags )
761 HRGN hRgn = 0;
762 RECT r, r2;
763 POINT pt;
764 WND* wndPtr;
766 if (!hwnd) hwnd = GetDesktopWindow();
768 /* check if the window or its parents are visible/not minimized */
770 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
772 /* process pending events and messages before painting */
773 if (flags & RDW_UPDATENOW)
774 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
776 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
777 if (TRACE_ON(win))
779 if( hrgnUpdate )
781 GetRgnBox( hrgnUpdate, &r );
782 TRACE( "%p (%p) NULL %p box (%ld,%ld-%ld,%ld) flags=%04x\n",
783 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
785 else
787 if( rectUpdate )
788 r = *rectUpdate;
789 else
790 SetRectEmpty( &r );
791 TRACE( "%p (%p) %s %ld,%ld-%ld,%ld %p flags=%04x\n",
792 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
793 r.top, r.right, r.bottom, hrgnUpdate, flags );
795 dump_rdw_flags(flags);
798 /* prepare an update region in window coordinates */
800 if (((flags & (RDW_INVALIDATE|RDW_FRAME)) == (RDW_INVALIDATE|RDW_FRAME)) ||
801 ((flags & (RDW_VALIDATE|RDW_NOFRAME)) == (RDW_VALIDATE|RDW_NOFRAME)))
802 r = wndPtr->rectWindow;
803 else
804 r = wndPtr->rectClient;
806 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
807 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
808 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
810 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
812 /* If the window doesn't have hrgnUpdate we leave hRgn zero
813 * and put a new region straight into wndPtr->hrgnUpdate
814 * so that RDW_UpdateRgns() won't have to do any extra work.
817 if( hrgnUpdate )
819 if( wndPtr->hrgnUpdate )
821 hRgn = CreateRectRgn( 0, 0, 0, 0 );
822 CombineRgn( hRgn, hrgnUpdate, 0, RGN_COPY );
823 OffsetRgn( hRgn, pt.x, pt.y );
825 else
827 wndPtr->hrgnUpdate = crop_rgn( 0, hrgnUpdate, &r );
828 OffsetRgn( wndPtr->hrgnUpdate, pt.x, pt.y );
831 else if( rectUpdate )
833 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
834 OffsetRect( &r2, pt.x, pt.y );
835 if( wndPtr->hrgnUpdate == 0 )
836 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
837 else
838 hRgn = CreateRectRgnIndirect( &r2 );
840 else /* entire window or client depending on RDW_FRAME */
842 if( flags & RDW_FRAME )
844 if (wndPtr->hrgnUpdate) hRgn = (HRGN)1;
845 else wndPtr->hrgnUpdate = (HRGN)1;
847 else
849 GETCLIENTRECTW( wndPtr, r2 );
850 if( wndPtr->hrgnUpdate == 0 )
851 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
852 else
853 hRgn = CreateRectRgnIndirect( &r2 );
857 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
859 /* In this we cannot leave with zero hRgn */
860 if( hrgnUpdate )
862 hRgn = crop_rgn( hRgn, hrgnUpdate, &r );
863 OffsetRgn( hRgn, pt.x, pt.y );
864 GetRgnBox( hRgn, &r2 );
865 if( IsRectEmpty( &r2 ) ) goto END;
867 else if( rectUpdate )
869 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
870 OffsetRect( &r2, pt.x, pt.y );
871 hRgn = CreateRectRgnIndirect( &r2 );
873 else /* entire window or client depending on RDW_NOFRAME */
875 if( flags & RDW_NOFRAME )
876 hRgn = (HRGN)1;
877 else
879 GETCLIENTRECTW( wndPtr, r2 );
880 hRgn = CreateRectRgnIndirect( &r2 );
885 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
887 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
889 /* Erase/update windows, from now on hRgn is a scratch region */
891 hRgn = RDW_Paint( wndPtr, (hRgn == (HRGN)1) ? 0 : hRgn, flags, 0 );
893 END:
894 if( hRgn > (HRGN)1 && (hRgn != hrgnUpdate) )
895 DeleteObject(hRgn );
896 WIN_ReleaseWndPtr(wndPtr);
897 return TRUE;
901 /***********************************************************************
902 * UpdateWindow (USER32.@)
904 BOOL WINAPI UpdateWindow( HWND hwnd )
906 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
910 /***********************************************************************
911 * InvalidateRgn (USER32.@)
913 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
915 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
919 /***********************************************************************
920 * InvalidateRect (USER32.@)
922 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
924 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
928 /***********************************************************************
929 * ValidateRgn (USER32.@)
931 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
933 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
937 /***********************************************************************
938 * ValidateRect (USER32.@)
940 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
942 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
946 /***********************************************************************
947 * GetUpdateRect (USER32.@)
949 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
951 BOOL retvalue;
952 WND * wndPtr = WIN_FindWndPtr( hwnd );
953 if (!wndPtr) return FALSE;
955 if (rect)
957 if (wndPtr->hrgnUpdate > (HRGN)1)
959 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
960 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
962 retvalue = FALSE;
963 goto END;
965 GetRgnBox( hrgn, rect );
966 DeleteObject( hrgn );
967 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
969 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
971 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
975 else
976 if( wndPtr->hrgnUpdate == (HRGN)1 )
978 GetClientRect( hwnd, rect );
979 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
981 else
982 SetRectEmpty( rect );
984 retvalue = (wndPtr->hrgnUpdate >= (HRGN)1);
985 END:
986 WIN_ReleaseWndPtr(wndPtr);
987 return retvalue;
991 /***********************************************************************
992 * GetUpdateRgn (USER32.@)
994 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
996 INT retval;
997 WND * wndPtr = WIN_FindWndPtr( hwnd );
998 if (!wndPtr) return ERROR;
1000 if (wndPtr->hrgnUpdate == 0)
1002 SetRectRgn( hrgn, 0, 0, 0, 0 );
1003 retval = NULLREGION;
1004 goto END;
1006 else
1007 if (wndPtr->hrgnUpdate == (HRGN)1)
1009 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
1010 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
1011 retval = SIMPLEREGION;
1013 else
1015 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1016 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1017 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1019 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1020 END:
1021 WIN_ReleaseWndPtr(wndPtr);
1022 return retval;
1026 /***********************************************************************
1027 * ExcludeUpdateRgn (USER32.@)
1029 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1031 RECT rect;
1032 WND * wndPtr;
1034 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1036 if (wndPtr->hrgnUpdate)
1038 INT ret;
1039 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1040 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1041 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1042 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1043 if( wndPtr->hrgnUpdate > (HRGN)1 )
1045 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1046 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1047 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1050 /* do ugly coordinate translations in dce.c */
1052 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
1053 DeleteObject( hrgn );
1054 WIN_ReleaseWndPtr(wndPtr);
1055 return ret;
1057 WIN_ReleaseWndPtr(wndPtr);
1058 return GetClipBox( hdc, &rect );
1063 /***********************************************************************
1064 * FillRect (USER.81)
1065 * NOTE
1066 * The Win16 variant doesn't support special color brushes like
1067 * the Win32 one, despite the fact that Win16, as well as Win32,
1068 * supports special background brushes for a window class.
1070 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1072 HBRUSH prevBrush;
1074 /* coordinates are logical so we cannot fast-check 'rect',
1075 * it will be done later in the PatBlt().
1078 if (!(prevBrush = SelectObject( HDC_32(hdc), HBRUSH_32(hbrush) ))) return 0;
1079 PatBlt( HDC_32(hdc), rect->left, rect->top,
1080 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1081 SelectObject( HDC_32(hdc), prevBrush );
1082 return 1;
1086 /***********************************************************************
1087 * FillRect (USER32.@)
1089 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1091 HBRUSH prevBrush;
1093 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1094 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1097 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1098 PatBlt( hdc, rect->left, rect->top,
1099 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1100 SelectObject( hdc, prevBrush );
1101 return 1;
1105 /***********************************************************************
1106 * InvertRect (USER.82)
1108 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1110 PatBlt( HDC_32(hdc), rect->left, rect->top,
1111 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1115 /***********************************************************************
1116 * InvertRect (USER32.@)
1118 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1120 return PatBlt( hdc, rect->left, rect->top,
1121 rect->right - rect->left, rect->bottom - rect->top,
1122 DSTINVERT );
1126 /***********************************************************************
1127 * FrameRect (USER32.@)
1129 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1131 HBRUSH prevBrush;
1132 RECT r = *rect;
1134 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1135 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1137 PatBlt( hdc, r.left, r.top, 1,
1138 r.bottom - r.top, PATCOPY );
1139 PatBlt( hdc, r.right - 1, r.top, 1,
1140 r.bottom - r.top, PATCOPY );
1141 PatBlt( hdc, r.left, r.top,
1142 r.right - r.left, 1, PATCOPY );
1143 PatBlt( hdc, r.left, r.bottom - 1,
1144 r.right - r.left, 1, PATCOPY );
1146 SelectObject( hdc, prevBrush );
1147 return TRUE;
1151 /***********************************************************************
1152 * FrameRect (USER.83)
1154 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1156 RECT rect;
1157 CONV_RECT16TO32( rect16, &rect );
1158 return FrameRect( HDC_32(hdc), &rect, HBRUSH_32(hbrush) );
1162 /***********************************************************************
1163 * DrawFocusRect (USER.466)
1165 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1167 RECT rect32;
1168 CONV_RECT16TO32( rc, &rect32 );
1169 DrawFocusRect( HDC_32(hdc), &rect32 );
1173 /***********************************************************************
1174 * DrawFocusRect (USER32.@)
1176 * FIXME: PatBlt(PATINVERT) with background brush.
1178 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1180 HBRUSH hOldBrush;
1181 HPEN hOldPen, hNewPen;
1182 INT oldDrawMode, oldBkMode;
1184 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1185 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1186 hOldPen = SelectObject(hdc, hNewPen);
1187 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1188 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1190 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1192 SetBkMode(hdc, oldBkMode);
1193 SetROP2(hdc, oldDrawMode);
1194 SelectObject(hdc, hOldPen);
1195 DeleteObject(hNewPen);
1196 SelectObject(hdc, hOldBrush);
1198 return TRUE;
1202 /**********************************************************************
1203 * DrawAnimatedRects (USER32.@)
1205 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1206 const RECT* lprcFrom,
1207 const RECT* lprcTo )
1209 FIXME("(%p,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1210 return TRUE;
1214 /**********************************************************************
1215 * PAINTING_DrawStateJam
1217 * Jams in the requested type in the dc
1219 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1220 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1221 LPRECT rc, UINT dtflags, BOOL unicode )
1223 HDC memdc;
1224 HBITMAP hbmsave;
1225 BOOL retval;
1226 INT cx = rc->right - rc->left;
1227 INT cy = rc->bottom - rc->top;
1229 switch(opcode)
1231 case DST_TEXT:
1232 case DST_PREFIXTEXT:
1233 if(unicode)
1234 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1235 else
1236 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1238 case DST_ICON:
1239 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1241 case DST_BITMAP:
1242 memdc = CreateCompatibleDC(hdc);
1243 if(!memdc) return FALSE;
1244 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1245 if(!hbmsave)
1247 DeleteDC(memdc);
1248 return FALSE;
1250 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1251 SelectObject(memdc, hbmsave);
1252 DeleteDC(memdc);
1253 return retval;
1255 case DST_COMPLEX:
1256 if(func) {
1257 BOOL bRet;
1258 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1260 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1261 bRet = func(hdc, lp, wp, cx, cy);
1262 /* Restore origin */
1263 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1264 return bRet;
1265 } else
1266 return FALSE;
1268 return FALSE;
1271 /**********************************************************************
1272 * PAINTING_DrawState()
1274 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1275 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1277 HBITMAP hbm, hbmsave;
1278 HFONT hfsave;
1279 HBRUSH hbsave, hbrtmp = 0;
1280 HDC memdc;
1281 RECT rc;
1282 UINT dtflags = DT_NOCLIP;
1283 COLORREF fg, bg;
1284 UINT opcode = flags & 0xf;
1285 INT len = wp;
1286 BOOL retval, tmp;
1288 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1290 if(unicode)
1291 len = strlenW((LPWSTR)lp);
1292 else
1293 len = strlen((LPSTR)lp);
1296 /* Find out what size the image has if not given by caller */
1297 if(!cx || !cy)
1299 SIZE s;
1300 CURSORICONINFO *ici;
1301 BITMAP bm;
1303 switch(opcode)
1305 case DST_TEXT:
1306 case DST_PREFIXTEXT:
1307 if(unicode)
1308 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1309 else
1310 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1311 if(!retval) return FALSE;
1312 break;
1314 case DST_ICON:
1315 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1316 if(!ici) return FALSE;
1317 s.cx = ici->nWidth;
1318 s.cy = ici->nHeight;
1319 GlobalUnlock16((HGLOBAL16)lp);
1320 break;
1322 case DST_BITMAP:
1323 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1324 return FALSE;
1325 s.cx = bm.bmWidth;
1326 s.cy = bm.bmHeight;
1327 break;
1329 case DST_COMPLEX: /* cx and cy must be set in this mode */
1330 return FALSE;
1333 if(!cx) cx = s.cx;
1334 if(!cy) cy = s.cy;
1337 rc.left = x;
1338 rc.top = y;
1339 rc.right = x + cx;
1340 rc.bottom = y + cy;
1342 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1343 dtflags |= DT_RIGHT;
1344 if(opcode == DST_TEXT)
1345 dtflags |= DT_NOPREFIX;
1347 /* For DSS_NORMAL we just jam in the image and return */
1348 if((flags & 0x7ff0) == DSS_NORMAL)
1350 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1353 /* For all other states we need to convert the image to B/W in a local bitmap */
1354 /* before it is displayed */
1355 fg = SetTextColor(hdc, RGB(0, 0, 0));
1356 bg = SetBkColor(hdc, RGB(255, 255, 255));
1357 hbm = NULL; hbmsave = NULL;
1358 memdc = NULL; hbsave = NULL;
1359 retval = FALSE; /* assume failure */
1361 /* From here on we must use "goto cleanup" when something goes wrong */
1362 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1363 if(!hbm) goto cleanup;
1364 memdc = CreateCompatibleDC(hdc);
1365 if(!memdc) goto cleanup;
1366 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1367 if(!hbmsave) goto cleanup;
1368 rc.left = rc.top = 0;
1369 rc.right = cx;
1370 rc.bottom = cy;
1371 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1372 SetBkColor(memdc, RGB(255, 255, 255));
1373 SetTextColor(memdc, RGB(0, 0, 0));
1374 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1376 /* DST_COMPLEX may draw text as well,
1377 * so we must be sure that correct font is selected
1379 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1380 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1381 if(hfsave) SelectObject(memdc, hfsave);
1382 if(!tmp) goto cleanup;
1384 /* This state cause the image to be dithered */
1385 if(flags & DSS_UNION)
1387 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1388 if(!hbsave) goto cleanup;
1389 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1390 SelectObject(memdc, hbsave);
1391 if(!tmp) goto cleanup;
1394 if (flags & DSS_DISABLED)
1395 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1396 else if (flags & DSS_DEFAULT)
1397 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1399 /* Draw light or dark shadow */
1400 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1402 if(!hbrtmp) goto cleanup;
1403 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1404 if(!hbsave) goto cleanup;
1405 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1406 SelectObject(hdc, hbsave);
1407 DeleteObject(hbrtmp);
1408 hbrtmp = 0;
1411 if (flags & DSS_DISABLED)
1413 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1414 if(!hbrtmp) goto cleanup;
1416 else if (!hbr)
1418 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1421 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1423 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1425 retval = TRUE; /* We succeeded */
1427 cleanup:
1428 SetTextColor(hdc, fg);
1429 SetBkColor(hdc, bg);
1431 if(hbsave) SelectObject(hdc, hbsave);
1432 if(hbmsave) SelectObject(memdc, hbmsave);
1433 if(hbrtmp) DeleteObject(hbrtmp);
1434 if(hbm) DeleteObject(hbm);
1435 if(memdc) DeleteDC(memdc);
1437 return retval;
1440 /**********************************************************************
1441 * DrawStateA (USER32.@)
1443 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1444 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1445 INT x, INT y, INT cx, INT cy, UINT flags)
1447 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1450 /**********************************************************************
1451 * DrawStateW (USER32.@)
1453 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1454 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1455 INT x, INT y, INT cx, INT cy, UINT flags)
1457 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1461 /***********************************************************************
1462 * SelectPalette (Not a Windows API)
1464 HPALETTE WINAPI SelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
1466 WORD wBkgPalette = 1;
1468 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1470 HWND hwnd = WindowFromDC( hDC );
1471 if (hwnd)
1473 HWND hForeground = GetForegroundWindow();
1474 /* set primary palette if it's related to current active */
1475 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1478 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
1482 /***********************************************************************
1483 * UserRealizePalette (USER32.@)
1485 UINT WINAPI UserRealizePalette( HDC hDC )
1487 UINT realized = pfnGDIRealizePalette( hDC );
1489 /* do not send anything if no colors were changed */
1490 if (realized && IsDCCurrentPalette16( HDC_16(hDC) ))
1492 /* send palette change notification */
1493 HWND hWnd = WindowFromDC( hDC );
1494 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
1495 SMTO_ABORTIFHUNG, 2000, NULL );
1497 return realized;