Ownerdraw buttons should erase themselves in WM_ERASEBKGND.
[wine.git] / windows / painting.c
blobf0cf411d7b30905cebe7e6d92a9a757998b6e899
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 <stdarg.h>
23 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "wine/winuser16.h"
28 #include "wownt32.h"
29 #include "wine/unicode.h"
30 #include "wine/server.h"
31 #include "gdi.h"
32 #include "user.h"
33 #include "win.h"
34 #include "message.h"
35 #include "dce.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(win);
39 WINE_DECLARE_DEBUG_CHANNEL(nonclient);
41 /* client rect in window coordinates */
43 #define GETCLIENTRECTW( wnd, r ) (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
44 (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
45 (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
46 (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
48 /* PAINT_RedrawWindow() control flags */
49 #define RDW_EX_DELAY_NCPAINT 0x0020
51 /* WIN_UpdateNCRgn() flags */
52 #define UNC_CHECK 0x0001
53 #define UNC_ENTIRE 0x0002
54 #define UNC_REGION 0x0004
55 #define UNC_UPDATE 0x0008
56 #define UNC_DELAY_NCPAINT 0x0010
57 #define UNC_IN_BEGINPAINT 0x0020
59 /* Last COLOR id */
60 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
62 HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = NULL;
63 UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc) = NULL;
66 /***********************************************************************
67 * add_paint_count
69 * Add an increment (normally 1 or -1) to the current paint count of a window.
71 static void add_paint_count( HWND hwnd, int incr )
73 SERVER_START_REQ( inc_window_paint_count )
75 req->handle = hwnd;
76 req->incr = incr;
77 wine_server_call( req );
79 SERVER_END_REQ;
83 /***********************************************************************
84 * crop_rgn
86 * hSrc: Region to crop.
87 * lpRect: Clipping rectangle.
89 * hDst: Region to hold the result (a new region is created if it's 0).
90 * Allowed to be the same region as hSrc in which case everything
91 * will be done in place, with no memory reallocations.
93 * Returns: hDst if success, 0 otherwise.
95 static HRGN crop_rgn( HRGN hDst, HRGN hSrc, const RECT *rect )
97 HRGN h = CreateRectRgnIndirect( rect );
98 if (hDst == 0) hDst = h;
99 CombineRgn( hDst, hSrc, h, RGN_AND );
100 if (hDst != h) DeleteObject( h );
101 return hDst;
105 /***********************************************************************
106 * WIN_HaveToDelayNCPAINT
108 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
109 * is generated as soon as a region intersecting the non-client area
110 * of a window is invalidated.
112 * This technique will work fine for all windows whose parents
113 * have the WS_CLIPCHILDREN style. When the parents have that style,
114 * they are not going to override the contents of their children.
115 * However, when the parent doesn't have that style, Windows relies
116 * on a "painter's algorithm" to display the contents of the windows.
117 * That is, windows are painted from back to front. This includes the
118 * non-client area.
120 * This method looks at the current state of a window to determine
121 * if the sending of the WM_NCPAINT message should be delayed until
122 * the BeginPaint call.
124 * PARAMS:
125 * wndPtr - A Locked window pointer to the window we're
126 * examining.
127 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
128 * function. This is a shortcut for the cases when
129 * we already know when to avoid scanning all the
130 * parents of a window. If you already know that this
131 * window's NCPAINT should be delayed, set the
132 * UNC_DELAY_NCPAINT flag for this parameter.
134 * This shortcut behavior is implemented in the
135 * RDW_Paint() method.
138 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
141 * Test the shortcut first. (duh)
143 if (uncFlags & UNC_DELAY_NCPAINT)
144 return TRUE;
147 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
148 * method only. This is another shortcut to avoid going
149 * up the parent's chain of the window to finally
150 * figure-out that none of them has an invalid region.
152 if (uncFlags & UNC_IN_BEGINPAINT)
153 return FALSE;
156 * Scan all the parents of this window to find a window
157 * that doesn't have the WS_CLIPCHILDREN style and that
158 * has an invalid region.
160 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
162 WND* parentWnd = WIN_FindWndPtr( hwnd );
163 if (parentWnd && !(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
165 WIN_ReleaseWndPtr( parentWnd );
166 return TRUE;
168 WIN_ReleaseWndPtr( parentWnd );
170 return FALSE;
173 /***********************************************************************
174 * WIN_UpdateNCRgn
176 * Things to do:
177 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
178 * Crop hrgnUpdate to a client rect, especially if it 1.
179 * If UNC_REGION is set return update region for the client rect.
181 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
182 * The trick is that when the returned region handle may be different from hRgn.
183 * In this case the old hRgn must be considered gone. BUT, if the returned value
184 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
185 * a DC without extra clipping region.
187 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
189 RECT r;
190 HRGN hClip = 0;
191 HRGN hrgnRet = 0;
193 TRACE_(nonclient)("hwnd %p [%p] hrgn %p, unc %04x, ncf %i\n",
194 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
196 /* desktop window doesn't have a nonclient area */
197 if(wnd->hwndSelf == GetDesktopWindow())
199 wnd->flags &= ~WIN_NEEDS_NCPAINT;
200 if( wnd->hrgnUpdate > (HRGN)1 )
202 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
203 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
204 hrgnRet = hRgn;
206 else
208 hrgnRet = wnd->hrgnUpdate;
210 return hrgnRet;
213 if ((wnd->hwndSelf == GetForegroundWindow()) &&
214 !(wnd->flags & WIN_NCACTIVATED) )
216 wnd->flags |= WIN_NCACTIVATED;
217 uncFlags |= UNC_ENTIRE;
221 * If the window's non-client area needs to be painted,
223 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
224 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
226 RECT r2, r3;
228 wnd->flags &= ~WIN_NEEDS_NCPAINT;
229 GETCLIENTRECTW( wnd, r );
231 TRACE_(nonclient)( "\tclient box (%ld,%ld-%ld,%ld), hrgnUpdate %p\n",
232 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
233 if( wnd->hrgnUpdate > (HRGN)1 )
235 /* Check if update rgn overlaps with nonclient area */
237 GetRgnBox( wnd->hrgnUpdate, &r2 );
238 UnionRect( &r3, &r2, &r );
239 if( r3.left != r.left || r3.top != r.top ||
240 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
242 /* crop hrgnUpdate, save old one in hClip - the only
243 * case that places a valid region handle in hClip */
245 hClip = wnd->hrgnUpdate;
246 wnd->hrgnUpdate = crop_rgn( hRgn, hClip, &r );
247 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
250 if( uncFlags & UNC_CHECK )
252 GetRgnBox( wnd->hrgnUpdate, &r3 );
253 if( IsRectEmpty( &r3 ) )
255 /* delete the update region since all invalid
256 * parts were in the nonclient area */
258 DeleteObject( wnd->hrgnUpdate );
259 wnd->hrgnUpdate = 0;
260 if(!(wnd->flags & WIN_INTERNAL_PAINT))
261 add_paint_count( wnd->hwndSelf, -1 );
263 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
267 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
269 else
270 if( wnd->hrgnUpdate == (HRGN)1 )/* entire window */
272 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
273 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
274 uncFlags |= UNC_ENTIRE;
277 else /* no WM_NCPAINT unless forced */
279 if( wnd->hrgnUpdate > (HRGN)1 )
281 copyrgn:
282 if( uncFlags & UNC_REGION )
284 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
285 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
286 hrgnRet = hRgn;
289 else
290 if( wnd->hrgnUpdate == (HRGN)1 && (uncFlags & UNC_UPDATE) )
292 GETCLIENTRECTW( wnd, r );
293 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
294 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
298 if(!hClip && (uncFlags & UNC_ENTIRE) )
300 /* still don't do anything if there is no nonclient area */
301 hClip = (HRGN)(memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
304 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
306 if ( hClip == hrgnRet && hrgnRet > (HRGN)1 ) {
307 hClip = CreateRectRgn( 0, 0, 0, 0 );
308 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
311 SendMessageA( wnd->hwndSelf, WM_NCPAINT, (WPARAM)hClip, 0L );
312 if( (hClip > (HRGN)1) && (hClip != hRgn) && (hClip != hrgnRet) )
313 DeleteObject( hClip );
315 * Since all Window locks are suspended while processing the WM_NCPAINT
316 * we want to make sure the window still exists before continuing.
318 if (!IsWindow(wnd->hwndSelf))
320 DeleteObject(hrgnRet);
321 hrgnRet=0;
325 TRACE_(nonclient)("returning %p (hClip = %p, hrgnUpdate = %p)\n", hrgnRet, hClip, wnd->hrgnUpdate );
327 return hrgnRet;
331 /***********************************************************************
332 * RDW_ValidateParent [RDW_UpdateRgns() helper]
334 * Validate the portions of parents that are covered by a validated child
335 * wndPtr = child
337 static void RDW_ValidateParent(WND *wndChild)
339 HWND parent;
340 HRGN hrg;
342 if (wndChild->hrgnUpdate == (HRGN)1 ) {
343 RECT r;
344 r.left = 0;
345 r.top = 0;
346 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
347 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
348 hrg = CreateRectRgnIndirect( &r );
349 } else
350 hrg = wndChild->hrgnUpdate;
352 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
353 while (parent && parent != GetDesktopWindow())
355 WND *wndParent = WIN_FindWndPtr( parent );
356 if (wndParent && !(wndParent->dwStyle & WS_CLIPCHILDREN))
358 if (wndParent->hrgnUpdate != 0)
360 POINT ptOffset;
361 RECT rect, rectParent;
362 if( wndParent->hrgnUpdate == (HRGN)1 )
364 RECT r;
366 r.left = 0;
367 r.top = 0;
368 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
369 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
371 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
373 /* we must offset the child region by the offset of the child rect in the parent */
374 GetWindowRect(wndParent->hwndSelf, &rectParent);
375 GetWindowRect(wndChild->hwndSelf, &rect);
376 ptOffset.x = rect.left - rectParent.left;
377 ptOffset.y = rect.top - rectParent.top;
378 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
379 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
380 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
383 WIN_ReleaseWndPtr( wndParent );
384 parent = GetAncestor( parent, GA_PARENT );
386 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
389 /***********************************************************************
390 * RDW_UpdateRgns [RedrawWindow() helper]
392 * Walks the window tree and adds/removes parts of the hRgn to/from update
393 * regions of windows that overlap it. Also, manages internal paint flags.
395 * NOTE: Walks the window tree so the caller must lock it.
396 * MUST preserve hRgn (can modify but then has to restore).
398 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
401 * Called only when one of the following is set:
402 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
405 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
406 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
407 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
408 RECT r;
410 r.left = 0;
411 r.top = 0;
412 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
413 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
415 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
417 if( flags & RDW_INVALIDATE )
419 if( hRgn > (HRGN)1 )
421 switch ((UINT)wndPtr->hrgnUpdate)
423 default:
424 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
425 /* fall through */
426 case 0:
427 wndPtr->hrgnUpdate = crop_rgn( wndPtr->hrgnUpdate,
428 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
429 &r );
430 if( !bHadOne )
432 GetRgnBox( wndPtr->hrgnUpdate, &r );
433 if( IsRectEmpty( &r ) )
435 DeleteObject( wndPtr->hrgnUpdate );
436 wndPtr->hrgnUpdate = 0;
437 goto end;
440 break;
441 case 1: /* already an entire window */
442 break;
445 else if( hRgn == (HRGN)1 )
447 if( wndPtr->hrgnUpdate > (HRGN)1 )
448 DeleteObject( wndPtr->hrgnUpdate );
449 wndPtr->hrgnUpdate = (HRGN)1;
451 else
452 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
454 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
455 add_paint_count( wndPtr->hwndSelf, 1 );
457 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
458 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
459 flags |= RDW_FRAME;
461 else if( flags & RDW_VALIDATE )
463 if( wndPtr->hrgnUpdate )
465 if( hRgn > (HRGN)1 )
467 if( wndPtr->hrgnUpdate == (HRGN)1 )
468 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
470 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
471 == NULLREGION )
473 DeleteObject( wndPtr->hrgnUpdate );
474 wndPtr->hrgnUpdate = 0;
477 else /* validate everything */
479 if( wndPtr->hrgnUpdate > (HRGN)1 ) DeleteObject( wndPtr->hrgnUpdate );
480 wndPtr->hrgnUpdate = 0;
483 if( !wndPtr->hrgnUpdate )
485 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
486 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
487 add_paint_count( wndPtr->hwndSelf, -1 );
491 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
492 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
496 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
497 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
499 /* in/validate child windows that intersect with the region if it
500 * is a valid handle. */
502 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
504 HWND *list;
505 if( hRgn > (HRGN)1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
507 POINT ptTotal, prevOrigin = {0,0};
508 POINT ptClient;
509 INT i;
511 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
512 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
514 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
516 WND *wnd = WIN_FindWndPtr( list[i] );
517 if (!wnd) continue;
518 if( wnd->dwStyle & WS_VISIBLE )
520 POINT ptOffset;
522 r.left = wnd->rectWindow.left + ptClient.x;
523 r.right = wnd->rectWindow.right + ptClient.x;
524 r.top = wnd->rectWindow.top + ptClient.y;
525 r.bottom = wnd->rectWindow.bottom + ptClient.y;
527 ptOffset.x = r.left - prevOrigin.x;
528 ptOffset.y = r.top - prevOrigin.y;
529 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
531 if( RectInRegion( hRgn, &r ) )
533 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
534 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
535 prevOrigin.x = r.left + ptTotal.x;
536 prevOrigin.y = r.top + ptTotal.y;
537 ptTotal.x += ptOffset.x;
538 ptTotal.y += ptOffset.y;
541 WIN_ReleaseWndPtr( wnd );
543 HeapFree( GetProcessHeap(), 0, list );
544 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
545 bChildren = 0;
549 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
551 if( bChildren )
553 HWND *list;
554 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
556 INT i;
557 for (i = 0; list[i]; i++)
559 WND *wnd = WIN_FindWndPtr( list[i] );
560 if (!wnd) continue;
561 if( wnd->dwStyle & WS_VISIBLE )
562 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
563 WIN_ReleaseWndPtr( wnd );
565 HeapFree( GetProcessHeap(), 0, list );
569 end:
571 /* Set/clear internal paint flag */
573 if (flags & RDW_INTERNALPAINT)
575 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
576 add_paint_count( wndPtr->hwndSelf, 1 );
577 wndPtr->flags |= WIN_INTERNAL_PAINT;
579 else if (flags & RDW_NOINTERNALPAINT)
581 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
582 add_paint_count( wndPtr->hwndSelf, -1 );
583 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
587 /***********************************************************************
588 * RDW_Paint [RedrawWindow() helper]
590 * Walks the window tree and paints/erases windows that have
591 * nonzero update regions according to redraw flags. hrgn is a scratch
592 * region passed down during recursion. Must not be 1.
595 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
597 /* NOTE: wndPtr is locked by caller.
599 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
600 * SendMessage() calls. This is a comment inside DefWindowProc() source
601 * from 16-bit SDK:
603 * This message avoids lots of inter-app message traffic
604 * by switching to the other task and continuing the
605 * recursion there.
607 * wParam = flags
608 * LOWORD(lParam) = hrgnClip
609 * HIWORD(lParam) = hwndSkip (not used; always NULL)
612 HDC hDC;
613 HWND hWnd = wndPtr->hwndSelf;
614 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
616 /* Erase/update the window itself ... */
618 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
621 * Check if this window should delay it's processing of WM_NCPAINT.
622 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
624 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
625 ex |= RDW_EX_DELAY_NCPAINT;
627 if (flags & RDW_UPDATENOW)
629 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
630 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
632 else if (flags & RDW_ERASENOW)
634 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
635 HRGN hrgnRet;
637 hrgnRet = WIN_UpdateNCRgn(wndPtr,
638 hrgn,
639 UNC_REGION | UNC_CHECK |
640 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
642 if( hrgnRet )
644 if( hrgnRet > (HRGN)1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
645 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
647 if( bIcon ) dcx |= DCX_WINDOW;
648 else
649 if( hrgnRet )
650 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
651 wndPtr->rectWindow.top - wndPtr->rectClient.top);
652 else
653 dcx &= ~DCX_INTERSECTRGN;
654 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
656 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
657 (WPARAM)hDC, 0 ))
658 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
659 ReleaseDC( hWnd, hDC );
665 if( !IsWindow(hWnd) ) return hrgn;
667 /* ... and its child windows */
669 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
670 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
672 HWND *list, *phwnd;
674 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
676 for (phwnd = list; *phwnd; phwnd++)
678 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
679 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
680 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
681 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
682 WIN_ReleaseWndPtr(wndPtr);
684 HeapFree( GetProcessHeap(), 0, list );
688 return hrgn;
692 /***********************************************************************
693 * dump_rdw_flags
695 static void dump_rdw_flags(UINT flags)
697 TRACE("flags:");
698 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
699 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
700 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
701 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
702 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
703 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
704 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
705 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
706 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
707 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
708 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
709 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
711 #define RDW_FLAGS \
712 (RDW_INVALIDATE | \
713 RDW_INTERNALPAINT | \
714 RDW_ERASE | \
715 RDW_VALIDATE | \
716 RDW_NOINTERNALPAINT | \
717 RDW_NOERASE | \
718 RDW_NOCHILDREN | \
719 RDW_ALLCHILDREN | \
720 RDW_UPDATENOW | \
721 RDW_ERASENOW | \
722 RDW_FRAME | \
723 RDW_NOFRAME)
725 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
726 TRACE("\n");
727 #undef RDW_FLAGS
731 /***********************************************************************
732 * RedrawWindow (USER32.@)
734 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
735 HRGN hrgnUpdate, UINT flags )
737 HRGN hRgn = 0;
738 RECT r, r2;
739 POINT pt;
740 WND* wndPtr;
742 if (!hwnd) hwnd = GetDesktopWindow();
744 /* check if the window or its parents are visible/not minimized */
746 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
748 /* process pending events and messages before painting */
749 if (flags & RDW_UPDATENOW)
750 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
752 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
753 if (TRACE_ON(win))
755 if( hrgnUpdate )
757 GetRgnBox( hrgnUpdate, &r );
758 TRACE( "%p (%p) NULL %p box (%ld,%ld-%ld,%ld) flags=%04x\n",
759 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
761 else
763 if( rectUpdate )
764 r = *rectUpdate;
765 else
766 SetRectEmpty( &r );
767 TRACE( "%p (%p) %s %ld,%ld-%ld,%ld %p flags=%04x\n",
768 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
769 r.top, r.right, r.bottom, hrgnUpdate, flags );
771 dump_rdw_flags(flags);
774 /* prepare an update region in window coordinates */
776 if (((flags & (RDW_INVALIDATE|RDW_FRAME)) == (RDW_INVALIDATE|RDW_FRAME)) ||
777 ((flags & (RDW_VALIDATE|RDW_NOFRAME)) == (RDW_VALIDATE|RDW_NOFRAME)))
778 r = wndPtr->rectWindow;
779 else
780 r = wndPtr->rectClient;
782 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
783 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
784 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
786 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
788 /* If the window doesn't have hrgnUpdate we leave hRgn zero
789 * and put a new region straight into wndPtr->hrgnUpdate
790 * so that RDW_UpdateRgns() won't have to do any extra work.
793 if( hrgnUpdate )
795 if( wndPtr->hrgnUpdate )
797 hRgn = CreateRectRgn( 0, 0, 0, 0 );
798 CombineRgn( hRgn, hrgnUpdate, 0, RGN_COPY );
799 OffsetRgn( hRgn, pt.x, pt.y );
801 else
803 wndPtr->hrgnUpdate = crop_rgn( 0, hrgnUpdate, &r );
804 OffsetRgn( wndPtr->hrgnUpdate, pt.x, pt.y );
807 else if( rectUpdate )
809 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
810 OffsetRect( &r2, pt.x, pt.y );
811 if( wndPtr->hrgnUpdate == 0 )
812 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
813 else
814 hRgn = CreateRectRgnIndirect( &r2 );
816 else /* entire window or client depending on RDW_FRAME */
818 if( flags & RDW_FRAME )
820 if (wndPtr->hrgnUpdate) hRgn = (HRGN)1;
821 else wndPtr->hrgnUpdate = (HRGN)1;
823 else
825 GETCLIENTRECTW( wndPtr, r2 );
826 if( wndPtr->hrgnUpdate == 0 )
827 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
828 else
829 hRgn = CreateRectRgnIndirect( &r2 );
833 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
835 /* In this we cannot leave with zero hRgn */
836 if( hrgnUpdate )
838 hRgn = crop_rgn( hRgn, hrgnUpdate, &r );
839 OffsetRgn( hRgn, pt.x, pt.y );
840 GetRgnBox( hRgn, &r2 );
841 if( IsRectEmpty( &r2 ) ) goto END;
843 else if( rectUpdate )
845 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
846 OffsetRect( &r2, pt.x, pt.y );
847 hRgn = CreateRectRgnIndirect( &r2 );
849 else /* entire window or client depending on RDW_NOFRAME */
851 if( flags & RDW_NOFRAME )
852 hRgn = (HRGN)1;
853 else
855 GETCLIENTRECTW( wndPtr, r2 );
856 hRgn = CreateRectRgnIndirect( &r2 );
861 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
863 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
865 /* Erase/update windows, from now on hRgn is a scratch region */
867 hRgn = RDW_Paint( wndPtr, (hRgn == (HRGN)1) ? 0 : hRgn, flags, 0 );
869 END:
870 if( hRgn > (HRGN)1 && (hRgn != hrgnUpdate) )
871 DeleteObject(hRgn );
872 WIN_ReleaseWndPtr(wndPtr);
873 return TRUE;
877 /***********************************************************************
878 * UpdateWindow (USER32.@)
880 BOOL WINAPI UpdateWindow( HWND hwnd )
882 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
886 /***********************************************************************
887 * InvalidateRgn (USER32.@)
889 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
891 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
895 /***********************************************************************
896 * InvalidateRect (USER32.@)
898 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
900 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
904 /***********************************************************************
905 * ValidateRgn (USER32.@)
907 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
909 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
913 /***********************************************************************
914 * ValidateRect (USER32.@)
916 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
918 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
922 /***********************************************************************
923 * GetUpdateRect (USER32.@)
925 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
927 BOOL retvalue;
928 WND * wndPtr = WIN_FindWndPtr( hwnd );
929 if (!wndPtr) return FALSE;
931 if (rect)
933 if (wndPtr->hrgnUpdate > (HRGN)1)
935 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
936 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
938 retvalue = FALSE;
939 goto END;
941 GetRgnBox( hrgn, rect );
942 DeleteObject( hrgn );
943 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
945 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
947 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
951 else
952 if( wndPtr->hrgnUpdate == (HRGN)1 )
954 GetClientRect( hwnd, rect );
955 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
957 else
958 SetRectEmpty( rect );
960 retvalue = (wndPtr->hrgnUpdate >= (HRGN)1);
961 END:
962 WIN_ReleaseWndPtr(wndPtr);
963 return retvalue;
967 /***********************************************************************
968 * GetUpdateRgn (USER32.@)
970 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
972 INT retval;
973 WND * wndPtr = WIN_FindWndPtr( hwnd );
974 if (!wndPtr) return ERROR;
976 if (wndPtr->hrgnUpdate == 0)
978 SetRectRgn( hrgn, 0, 0, 0, 0 );
979 retval = NULLREGION;
980 goto END;
982 else
983 if (wndPtr->hrgnUpdate == (HRGN)1)
985 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
986 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
987 retval = SIMPLEREGION;
989 else
991 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
992 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
993 wndPtr->rectWindow.top - wndPtr->rectClient.top );
995 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
996 END:
997 WIN_ReleaseWndPtr(wndPtr);
998 return retval;
1002 /***********************************************************************
1003 * ExcludeUpdateRgn (USER32.@)
1005 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1007 RECT rect;
1008 WND * wndPtr;
1010 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1012 if (wndPtr->hrgnUpdate)
1014 INT ret;
1015 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1016 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1017 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1018 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1019 if( wndPtr->hrgnUpdate > (HRGN)1 )
1021 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1022 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1023 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1026 /* do ugly coordinate translations in dce.c */
1028 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
1029 DeleteObject( hrgn );
1030 WIN_ReleaseWndPtr(wndPtr);
1031 return ret;
1033 WIN_ReleaseWndPtr(wndPtr);
1034 return GetClipBox( hdc, &rect );
1039 /***********************************************************************
1040 * FillRect (USER.81)
1041 * NOTE
1042 * The Win16 variant doesn't support special color brushes like
1043 * the Win32 one, despite the fact that Win16, as well as Win32,
1044 * supports special background brushes for a window class.
1046 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1048 HBRUSH prevBrush;
1050 /* coordinates are logical so we cannot fast-check 'rect',
1051 * it will be done later in the PatBlt().
1054 if (!(prevBrush = SelectObject( HDC_32(hdc), HBRUSH_32(hbrush) ))) return 0;
1055 PatBlt( HDC_32(hdc), rect->left, rect->top,
1056 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1057 SelectObject( HDC_32(hdc), prevBrush );
1058 return 1;
1062 /***********************************************************************
1063 * FillRect (USER32.@)
1065 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1067 HBRUSH prevBrush;
1069 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1070 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1073 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1074 PatBlt( hdc, rect->left, rect->top,
1075 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1076 SelectObject( hdc, prevBrush );
1077 return 1;
1081 /***********************************************************************
1082 * InvertRect (USER.82)
1084 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1086 PatBlt( HDC_32(hdc), rect->left, rect->top,
1087 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1091 /***********************************************************************
1092 * InvertRect (USER32.@)
1094 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1096 return PatBlt( hdc, rect->left, rect->top,
1097 rect->right - rect->left, rect->bottom - rect->top,
1098 DSTINVERT );
1102 /***********************************************************************
1103 * FrameRect (USER32.@)
1105 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1107 HBRUSH prevBrush;
1108 RECT r = *rect;
1110 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1111 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1113 PatBlt( hdc, r.left, r.top, 1,
1114 r.bottom - r.top, PATCOPY );
1115 PatBlt( hdc, r.right - 1, r.top, 1,
1116 r.bottom - r.top, PATCOPY );
1117 PatBlt( hdc, r.left, r.top,
1118 r.right - r.left, 1, PATCOPY );
1119 PatBlt( hdc, r.left, r.bottom - 1,
1120 r.right - r.left, 1, PATCOPY );
1122 SelectObject( hdc, prevBrush );
1123 return TRUE;
1127 /***********************************************************************
1128 * FrameRect (USER.83)
1130 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1132 RECT rect;
1133 CONV_RECT16TO32( rect16, &rect );
1134 return FrameRect( HDC_32(hdc), &rect, HBRUSH_32(hbrush) );
1138 /***********************************************************************
1139 * DrawFocusRect (USER.466)
1141 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1143 RECT rect32;
1144 CONV_RECT16TO32( rc, &rect32 );
1145 DrawFocusRect( HDC_32(hdc), &rect32 );
1149 /***********************************************************************
1150 * DrawFocusRect (USER32.@)
1152 * FIXME: PatBlt(PATINVERT) with background brush.
1154 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1156 HBRUSH hOldBrush;
1157 HPEN hOldPen, hNewPen;
1158 INT oldDrawMode, oldBkMode;
1160 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1161 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1162 hOldPen = SelectObject(hdc, hNewPen);
1163 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1164 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1166 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1168 SetBkMode(hdc, oldBkMode);
1169 SetROP2(hdc, oldDrawMode);
1170 SelectObject(hdc, hOldPen);
1171 DeleteObject(hNewPen);
1172 SelectObject(hdc, hOldBrush);
1174 return TRUE;
1178 /**********************************************************************
1179 * DrawAnimatedRects (USER32.@)
1181 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1182 const RECT* lprcFrom,
1183 const RECT* lprcTo )
1185 FIXME_(win)("(%p,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1186 return TRUE;
1190 /**********************************************************************
1191 * PAINTING_DrawStateJam
1193 * Jams in the requested type in the dc
1195 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1196 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1197 LPRECT rc, UINT dtflags, BOOL unicode )
1199 HDC memdc;
1200 HBITMAP hbmsave;
1201 BOOL retval;
1202 INT cx = rc->right - rc->left;
1203 INT cy = rc->bottom - rc->top;
1205 switch(opcode)
1207 case DST_TEXT:
1208 case DST_PREFIXTEXT:
1209 if(unicode)
1210 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1211 else
1212 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1214 case DST_ICON:
1215 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1217 case DST_BITMAP:
1218 memdc = CreateCompatibleDC(hdc);
1219 if(!memdc) return FALSE;
1220 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1221 if(!hbmsave)
1223 DeleteDC(memdc);
1224 return FALSE;
1226 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1227 SelectObject(memdc, hbmsave);
1228 DeleteDC(memdc);
1229 return retval;
1231 case DST_COMPLEX:
1232 if(func) {
1233 BOOL bRet;
1234 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1236 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1237 bRet = func(hdc, lp, wp, cx, cy);
1238 /* Restore origin */
1239 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1240 return bRet;
1241 } else
1242 return FALSE;
1244 return FALSE;
1247 /**********************************************************************
1248 * PAINTING_DrawState()
1250 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1251 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1253 HBITMAP hbm, hbmsave;
1254 HFONT hfsave;
1255 HBRUSH hbsave, hbrtmp = 0;
1256 HDC memdc;
1257 RECT rc;
1258 UINT dtflags = DT_NOCLIP;
1259 COLORREF fg, bg;
1260 UINT opcode = flags & 0xf;
1261 INT len = wp;
1262 BOOL retval, tmp;
1264 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1266 if(unicode)
1267 len = strlenW((LPWSTR)lp);
1268 else
1269 len = strlen((LPSTR)lp);
1272 /* Find out what size the image has if not given by caller */
1273 if(!cx || !cy)
1275 SIZE s;
1276 CURSORICONINFO *ici;
1277 BITMAP bm;
1279 switch(opcode)
1281 case DST_TEXT:
1282 case DST_PREFIXTEXT:
1283 if(unicode)
1284 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1285 else
1286 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1287 if(!retval) return FALSE;
1288 break;
1290 case DST_ICON:
1291 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1292 if(!ici) return FALSE;
1293 s.cx = ici->nWidth;
1294 s.cy = ici->nHeight;
1295 GlobalUnlock16((HGLOBAL16)lp);
1296 break;
1298 case DST_BITMAP:
1299 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1300 return FALSE;
1301 s.cx = bm.bmWidth;
1302 s.cy = bm.bmHeight;
1303 break;
1305 case DST_COMPLEX: /* cx and cy must be set in this mode */
1306 return FALSE;
1309 if(!cx) cx = s.cx;
1310 if(!cy) cy = s.cy;
1313 rc.left = x;
1314 rc.top = y;
1315 rc.right = x + cx;
1316 rc.bottom = y + cy;
1318 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1319 dtflags |= DT_RIGHT;
1320 if(opcode == DST_TEXT)
1321 dtflags |= DT_NOPREFIX;
1323 /* For DSS_NORMAL we just jam in the image and return */
1324 if((flags & 0x7ff0) == DSS_NORMAL)
1326 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1329 /* For all other states we need to convert the image to B/W in a local bitmap */
1330 /* before it is displayed */
1331 fg = SetTextColor(hdc, RGB(0, 0, 0));
1332 bg = SetBkColor(hdc, RGB(255, 255, 255));
1333 hbm = NULL; hbmsave = NULL;
1334 memdc = NULL; hbsave = NULL;
1335 retval = FALSE; /* assume failure */
1337 /* From here on we must use "goto cleanup" when something goes wrong */
1338 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1339 if(!hbm) goto cleanup;
1340 memdc = CreateCompatibleDC(hdc);
1341 if(!memdc) goto cleanup;
1342 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1343 if(!hbmsave) goto cleanup;
1344 rc.left = rc.top = 0;
1345 rc.right = cx;
1346 rc.bottom = cy;
1347 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1348 SetBkColor(memdc, RGB(255, 255, 255));
1349 SetTextColor(memdc, RGB(0, 0, 0));
1350 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1352 /* DST_COMPLEX may draw text as well,
1353 * so we must be sure that correct font is selected
1355 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1356 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1357 if(hfsave) SelectObject(memdc, hfsave);
1358 if(!tmp) goto cleanup;
1360 /* This state cause the image to be dithered */
1361 if(flags & DSS_UNION)
1363 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1364 if(!hbsave) goto cleanup;
1365 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1366 SelectObject(memdc, hbsave);
1367 if(!tmp) goto cleanup;
1370 if (flags & DSS_DISABLED)
1371 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1372 else if (flags & DSS_DEFAULT)
1373 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1375 /* Draw light or dark shadow */
1376 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1378 if(!hbrtmp) goto cleanup;
1379 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1380 if(!hbsave) goto cleanup;
1381 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1382 SelectObject(hdc, hbsave);
1383 DeleteObject(hbrtmp);
1384 hbrtmp = 0;
1387 if (flags & DSS_DISABLED)
1389 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1390 if(!hbrtmp) goto cleanup;
1392 else if (!hbr)
1394 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1397 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1399 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1401 retval = TRUE; /* We succeeded */
1403 cleanup:
1404 SetTextColor(hdc, fg);
1405 SetBkColor(hdc, bg);
1407 if(hbsave) SelectObject(hdc, hbsave);
1408 if(hbmsave) SelectObject(memdc, hbmsave);
1409 if(hbrtmp) DeleteObject(hbrtmp);
1410 if(hbm) DeleteObject(hbm);
1411 if(memdc) DeleteDC(memdc);
1413 return retval;
1416 /**********************************************************************
1417 * DrawStateA (USER32.@)
1419 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1420 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1421 INT x, INT y, INT cx, INT cy, UINT flags)
1423 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1426 /**********************************************************************
1427 * DrawStateW (USER32.@)
1429 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1430 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1431 INT x, INT y, INT cx, INT cy, UINT flags)
1433 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1437 /***********************************************************************
1438 * SelectPalette (Not a Windows API)
1440 HPALETTE WINAPI SelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
1442 WORD wBkgPalette = 1;
1444 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1446 HWND hwnd = WindowFromDC( hDC );
1447 if (hwnd)
1449 HWND hForeground = GetForegroundWindow();
1450 /* set primary palette if it's related to current active */
1451 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1454 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
1458 /***********************************************************************
1459 * UserRealizePalette (USER32.@)
1461 UINT WINAPI UserRealizePalette( HDC hDC )
1463 UINT realized = pfnGDIRealizePalette( hDC );
1465 /* do not send anything if no colors were changed */
1466 if (realized && IsDCCurrentPalette16( HDC_16(hDC) ))
1468 /* send palette change notification */
1469 HWND hWnd = WindowFromDC( hDC );
1470 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
1471 SMTO_ABORTIFHUNG, 2000, NULL );
1473 return realized;