Fix for mingw32.
[wine/wine64.git] / windows / painting.c
blob9dc25b5004c598906600a0f9c2db0bbaa93f7be9
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 "wine/unicode.h"
27 #include "wine/server.h"
28 #include "region.h"
29 #include "user.h"
30 #include "win.h"
31 #include "queue.h"
32 #include "dce.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(win);
36 WINE_DECLARE_DEBUG_CHANNEL(nonclient);
38 /* client rect in window coordinates */
40 #define GETCLIENTRECTW( wnd, r ) (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
41 (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
42 (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
43 (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
45 /* PAINT_RedrawWindow() control flags */
46 #define RDW_EX_DELAY_NCPAINT 0x0020
48 /* WIN_UpdateNCRgn() flags */
49 #define UNC_CHECK 0x0001
50 #define UNC_ENTIRE 0x0002
51 #define UNC_REGION 0x0004
52 #define UNC_UPDATE 0x0008
53 #define UNC_DELAY_NCPAINT 0x0010
54 #define UNC_IN_BEGINPAINT 0x0020
56 /* Last COLOR id */
57 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
60 /* ### start build ### */
61 extern WORD CALLBACK PAINTING_CallTo16_word_wlwww(DRAWSTATEPROC16,WORD,LONG,WORD,WORD,WORD);
62 /* ### stop build ### */
64 struct draw_state_info
66 DRAWSTATEPROC16 proc;
67 LPARAM param;
70 /* callback for 16-bit DrawState functions */
71 static BOOL CALLBACK draw_state_callback( HDC hdc, LPARAM lparam, WPARAM wparam, int cx, int cy )
73 const struct draw_state_info *info = (struct draw_state_info *)lparam;
74 return PAINTING_CallTo16_word_wlwww( info->proc, hdc, info->param, wparam, cx, cy );
78 /***********************************************************************
79 * add_paint_count
81 * Add an increment (normally 1 or -1) to the current paint count of a window.
83 static void add_paint_count( HWND hwnd, int incr )
85 SERVER_START_REQ( inc_window_paint_count )
87 req->handle = hwnd;
88 req->incr = incr;
89 wine_server_call( req );
91 SERVER_END_REQ;
95 /***********************************************************************
96 * WIN_HaveToDelayNCPAINT
98 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
99 * is generated as soon as a region intersecting the non-client area
100 * of a window is invalidated.
102 * This technique will work fine for all windows whose parents
103 * have the WS_CLIPCHILDREN style. When the parents have that style,
104 * they are not going to override the contents of their children.
105 * However, when the parent doesn't have that style, Windows relies
106 * on a "painter's algorithm" to display the contents of the windows.
107 * That is, windows are painted from back to front. This includes the
108 * non-client area.
110 * This method looks at the current state of a window to determine
111 * if the sending of the WM_NCPAINT message should be delayed until
112 * the BeginPaint call.
114 * PARAMS:
115 * wndPtr - A Locked window pointer to the window we're
116 * examining.
117 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
118 * function. This is a shortcut for the cases when
119 * we already know when to avoid scanning all the
120 * parents of a window. If you already know that this
121 * window's NCPAINT should be delayed, set the
122 * UNC_DELAY_NCPAINT flag for this parameter.
124 * This shortcut behavior is implemented in the
125 * RDW_Paint() method.
128 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
131 * Test the shortcut first. (duh)
133 if (uncFlags & UNC_DELAY_NCPAINT)
134 return TRUE;
137 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
138 * method only. This is another shortcut to avoid going
139 * up the parent's chain of the window to finally
140 * figure-out that none of them has an invalid region.
142 if (uncFlags & UNC_IN_BEGINPAINT)
143 return FALSE;
146 * Scan all the parents of this window to find a window
147 * that doesn't have the WS_CLIPCHILDREN style and that
148 * has an invalid region.
150 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
152 WND* parentWnd = WIN_FindWndPtr( hwnd );
153 if (!(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
155 WIN_ReleaseWndPtr( parentWnd );
156 return TRUE;
158 WIN_ReleaseWndPtr( parentWnd );
160 return FALSE;
163 /***********************************************************************
164 * WIN_UpdateNCRgn
166 * Things to do:
167 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
168 * Crop hrgnUpdate to a client rect, especially if it 1.
169 * If UNC_REGION is set return update region for the client rect.
171 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
172 * The trick is that when the returned region handle may be different from hRgn.
173 * In this case the old hRgn must be considered gone. BUT, if the returned value
174 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
175 * a DC without extra clipping region.
177 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
179 RECT r;
180 HRGN hClip = 0;
181 HRGN hrgnRet = 0;
183 TRACE_(nonclient)("hwnd %04x [%04x] hrgn %04x, unc %04x, ncf %i\n",
184 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
186 /* desktop window doesn't have a nonclient area */
187 if(wnd->hwndSelf == GetDesktopWindow())
189 wnd->flags &= ~WIN_NEEDS_NCPAINT;
190 if( wnd->hrgnUpdate > 1 )
191 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
192 else
194 hrgnRet = wnd->hrgnUpdate;
196 return hrgnRet;
199 if ((wnd->hwndSelf == GetForegroundWindow()) &&
200 !(wnd->flags & WIN_NCACTIVATED) )
202 wnd->flags |= WIN_NCACTIVATED;
203 uncFlags |= UNC_ENTIRE;
207 * If the window's non-client area needs to be painted,
209 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
210 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
212 RECT r2, r3;
214 wnd->flags &= ~WIN_NEEDS_NCPAINT;
215 GETCLIENTRECTW( wnd, r );
217 TRACE_(nonclient)( "\tclient box (%i,%i-%i,%i), hrgnUpdate %04x\n",
218 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
219 if( wnd->hrgnUpdate > 1 )
221 /* Check if update rgn overlaps with nonclient area */
223 GetRgnBox( wnd->hrgnUpdate, &r2 );
224 UnionRect( &r3, &r2, &r );
225 if( r3.left != r.left || r3.top != r.top ||
226 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
228 /* crop hrgnUpdate, save old one in hClip - the only
229 * case that places a valid region handle in hClip */
231 hClip = wnd->hrgnUpdate;
232 wnd->hrgnUpdate = REGION_CropRgn( hRgn, hClip, &r, NULL );
233 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
236 if( uncFlags & UNC_CHECK )
238 GetRgnBox( wnd->hrgnUpdate, &r3 );
239 if( IsRectEmpty( &r3 ) )
241 /* delete the update region since all invalid
242 * parts were in the nonclient area */
244 DeleteObject( wnd->hrgnUpdate );
245 wnd->hrgnUpdate = 0;
246 if(!(wnd->flags & WIN_INTERNAL_PAINT))
247 add_paint_count( wnd->hwndSelf, -1 );
249 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
253 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
255 else
256 if( wnd->hrgnUpdate == 1 )/* entire window */
258 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
259 if( uncFlags & UNC_REGION ) hrgnRet = 1;
260 uncFlags |= UNC_ENTIRE;
263 else /* no WM_NCPAINT unless forced */
265 if( wnd->hrgnUpdate > 1 )
267 copyrgn:
268 if( uncFlags & UNC_REGION )
269 hrgnRet = REGION_CropRgn( hRgn, wnd->hrgnUpdate, NULL, NULL );
271 else
272 if( wnd->hrgnUpdate == 1 && (uncFlags & UNC_UPDATE) )
274 GETCLIENTRECTW( wnd, r );
275 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
276 if( uncFlags & UNC_REGION ) hrgnRet = 1;
280 if(!hClip && (uncFlags & UNC_ENTIRE) )
282 /* still don't do anything if there is no nonclient area */
283 hClip = (memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
286 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
288 if ( hClip == hrgnRet && hrgnRet > 1 ) {
289 hClip = CreateRectRgn( 0, 0, 0, 0 );
290 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
293 SendMessageA( wnd->hwndSelf, WM_NCPAINT, hClip, 0L );
294 if( (hClip > 1) && (hClip != hRgn) && (hClip != hrgnRet) )
295 DeleteObject( hClip );
297 * Since all Window locks are suspended while processing the WM_NCPAINT
298 * we want to make sure the window still exists before continuing.
300 if (!IsWindow(wnd->hwndSelf))
302 DeleteObject(hrgnRet);
303 hrgnRet=0;
307 TRACE_(nonclient)("returning %04x (hClip = %04x, hrgnUpdate = %04x)\n", hrgnRet, hClip, wnd->hrgnUpdate );
309 return hrgnRet;
313 /***********************************************************************
314 * RDW_ValidateParent [RDW_UpdateRgns() helper]
316 * Validate the portions of parents that are covered by a validated child
317 * wndPtr = child
319 static void RDW_ValidateParent(WND *wndChild)
321 HWND parent;
322 HRGN hrg;
324 if (wndChild->hrgnUpdate == 1 ) {
325 RECT r;
326 r.left = 0;
327 r.top = 0;
328 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
329 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
330 hrg = CreateRectRgnIndirect( &r );
331 } else
332 hrg = wndChild->hrgnUpdate;
334 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
335 while (parent && parent != GetDesktopWindow())
337 WND *wndParent = WIN_FindWndPtr( parent );
338 if (!(wndParent->dwStyle & WS_CLIPCHILDREN))
340 if (wndParent->hrgnUpdate != 0)
342 POINT ptOffset;
343 RECT rect, rectParent;
344 if( wndParent->hrgnUpdate == 1 )
346 RECT r;
348 r.left = 0;
349 r.top = 0;
350 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
351 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
353 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
355 /* we must offset the child region by the offset of the child rect in the parent */
356 GetWindowRect(wndParent->hwndSelf, &rectParent);
357 GetWindowRect(wndChild->hwndSelf, &rect);
358 ptOffset.x = rect.left - rectParent.left;
359 ptOffset.y = rect.top - rectParent.top;
360 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
361 CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF );
362 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
365 WIN_ReleaseWndPtr( wndParent );
366 parent = GetAncestor( parent, GA_PARENT );
368 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
371 /***********************************************************************
372 * RDW_UpdateRgns [RedrawWindow() helper]
374 * Walks the window tree and adds/removes parts of the hRgn to/from update
375 * regions of windows that overlap it. Also, manages internal paint flags.
377 * NOTE: Walks the window tree so the caller must lock it.
378 * MUST preserve hRgn (can modify but then has to restore).
380 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
383 * Called only when one of the following is set:
384 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
387 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
388 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
389 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
390 RECT r;
392 r.left = 0;
393 r.top = 0;
394 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
395 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
397 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
399 if( flags & RDW_INVALIDATE )
401 if( hRgn > 1 )
403 switch( wndPtr->hrgnUpdate )
405 default:
406 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
407 /* fall through */
408 case 0:
409 wndPtr->hrgnUpdate = REGION_CropRgn( wndPtr->hrgnUpdate,
410 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
411 &r, NULL );
412 if( !bHadOne )
414 GetRgnBox( wndPtr->hrgnUpdate, &r );
415 if( IsRectEmpty( &r ) )
417 DeleteObject( wndPtr->hrgnUpdate );
418 wndPtr->hrgnUpdate = 0;
419 goto end;
422 break;
423 case 1: /* already an entire window */
424 break;
427 else if( hRgn == 1 )
429 if( wndPtr->hrgnUpdate > 1 )
430 DeleteObject( wndPtr->hrgnUpdate );
431 wndPtr->hrgnUpdate = 1;
433 else
434 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
436 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
437 add_paint_count( wndPtr->hwndSelf, 1 );
439 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
440 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
441 flags |= RDW_FRAME;
443 else if( flags & RDW_VALIDATE )
445 if( wndPtr->hrgnUpdate )
447 if( hRgn > 1 )
449 if( wndPtr->hrgnUpdate == 1 )
450 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
452 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
453 == NULLREGION )
455 DeleteObject( wndPtr->hrgnUpdate );
456 wndPtr->hrgnUpdate = 0;
459 else /* validate everything */
461 if( wndPtr->hrgnUpdate > 1 ) DeleteObject( wndPtr->hrgnUpdate );
462 wndPtr->hrgnUpdate = 0;
465 if( !wndPtr->hrgnUpdate )
467 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
468 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
469 add_paint_count( wndPtr->hwndSelf, -1 );
473 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
474 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
478 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
479 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
481 /* in/validate child windows that intersect with the region if it
482 * is a valid handle. */
484 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
486 HWND *list;
487 if( hRgn > 1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
489 POINT ptTotal, prevOrigin = {0,0};
490 POINT ptClient;
491 INT i;
493 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
494 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
496 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
498 WND *wnd = WIN_FindWndPtr( list[i] );
499 if (!wnd) continue;
500 if( wnd->dwStyle & WS_VISIBLE )
502 POINT ptOffset;
504 r.left = wnd->rectWindow.left + ptClient.x;
505 r.right = wnd->rectWindow.right + ptClient.x;
506 r.top = wnd->rectWindow.top + ptClient.y;
507 r.bottom = wnd->rectWindow.bottom + ptClient.y;
509 ptOffset.x = r.left - prevOrigin.x;
510 ptOffset.y = r.top - prevOrigin.y;
511 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
513 if( RectInRegion( hRgn, &r ) )
515 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
516 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
517 prevOrigin.x = r.left + ptTotal.x;
518 prevOrigin.y = r.top + ptTotal.y;
519 ptTotal.x += ptOffset.x;
520 ptTotal.y += ptOffset.y;
523 WIN_ReleaseWndPtr( wnd );
525 HeapFree( GetProcessHeap(), 0, list );
526 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
527 bChildren = 0;
531 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
533 if( bChildren )
535 HWND *list;
536 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
538 INT i;
539 for (i = 0; list[i]; i++)
541 WND *wnd = WIN_FindWndPtr( list[i] );
542 if (!wnd) continue;
543 if( wnd->dwStyle & WS_VISIBLE )
544 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
545 WIN_ReleaseWndPtr( wnd );
547 HeapFree( GetProcessHeap(), 0, list );
551 end:
553 /* Set/clear internal paint flag */
555 if (flags & RDW_INTERNALPAINT)
557 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
558 add_paint_count( wndPtr->hwndSelf, 1 );
559 wndPtr->flags |= WIN_INTERNAL_PAINT;
561 else if (flags & RDW_NOINTERNALPAINT)
563 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
564 add_paint_count( wndPtr->hwndSelf, -1 );
565 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
569 /***********************************************************************
570 * RDW_Paint [RedrawWindow() helper]
572 * Walks the window tree and paints/erases windows that have
573 * nonzero update regions according to redraw flags. hrgn is a scratch
574 * region passed down during recursion. Must not be 1.
577 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
579 /* NOTE: wndPtr is locked by caller.
581 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
582 * SendMessage() calls. This is a comment inside DefWindowProc() source
583 * from 16-bit SDK:
585 * This message avoids lots of inter-app message traffic
586 * by switching to the other task and continuing the
587 * recursion there.
589 * wParam = flags
590 * LOWORD(lParam) = hrgnClip
591 * HIWORD(lParam) = hwndSkip (not used; always NULL)
594 HDC hDC;
595 HWND hWnd = wndPtr->hwndSelf;
596 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
598 /* Erase/update the window itself ... */
600 TRACE("\thwnd %04x [%04x] -> hrgn [%04x], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
603 * Check if this window should delay it's processing of WM_NCPAINT.
604 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
606 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
607 ex |= RDW_EX_DELAY_NCPAINT;
609 if (flags & RDW_UPDATENOW)
611 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
612 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
614 else if (flags & RDW_ERASENOW)
616 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
617 HRGN hrgnRet;
619 hrgnRet = WIN_UpdateNCRgn(wndPtr,
620 hrgn,
621 UNC_REGION | UNC_CHECK |
622 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
624 if( hrgnRet )
626 if( hrgnRet > 1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
627 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
629 if( bIcon ) dcx |= DCX_WINDOW;
630 else
631 if( hrgnRet )
632 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
633 wndPtr->rectWindow.top - wndPtr->rectClient.top);
634 else
635 dcx &= ~DCX_INTERSECTRGN;
636 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
638 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
639 (WPARAM)hDC, 0 ))
640 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
641 ReleaseDC( hWnd, hDC );
647 if( !IsWindow(hWnd) ) return hrgn;
649 /* ... and its child windows */
651 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
652 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
654 HWND *list, *phwnd;
656 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
658 for (phwnd = list; *phwnd; phwnd++)
660 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
661 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
662 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
663 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
664 WIN_ReleaseWndPtr(wndPtr);
666 HeapFree( GetProcessHeap(), 0, list );
670 return hrgn;
674 /***********************************************************************
675 * RedrawWindow (USER32.@)
677 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
678 HRGN hrgnUpdate, UINT flags )
680 HRGN hRgn = 0;
681 RECT r, r2;
682 POINT pt;
683 WND* wndPtr;
685 if (!hwnd) hwnd = GetDesktopWindow();
687 /* check if the window or its parents are visible/not minimized */
689 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
691 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
692 if (TRACE_ON(win))
694 if( hrgnUpdate )
696 GetRgnBox( hrgnUpdate, &r );
697 TRACE( "%04x (%04x) NULL %04x box (%i,%i-%i,%i) flags=%04x\n",
698 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
700 else
702 if( rectUpdate )
703 r = *rectUpdate;
704 else
705 SetRectEmpty( &r );
706 TRACE( "%04x (%04x) %s %d,%d-%d,%d %04x flags=%04x\n",
707 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
708 r.top, r.right, r.bottom, hrgnUpdate, flags );
713 /* process pending events and messages before painting */
714 if (flags & RDW_UPDATENOW)
715 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
717 /* prepare an update region in window coordinates */
719 if( flags & RDW_FRAME )
720 r = wndPtr->rectWindow;
721 else
722 r = wndPtr->rectClient;
724 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
725 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
726 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
728 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
730 /* If the window doesn't have hrgnUpdate we leave hRgn zero
731 * and put a new region straight into wndPtr->hrgnUpdate
732 * so that RDW_UpdateRgns() won't have to do any extra work.
735 if( hrgnUpdate )
737 if( wndPtr->hrgnUpdate )
738 hRgn = REGION_CropRgn( 0, hrgnUpdate, NULL, &pt );
739 else
740 wndPtr->hrgnUpdate = REGION_CropRgn( 0, hrgnUpdate, &r, &pt );
742 else if( rectUpdate )
744 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
745 OffsetRect( &r2, pt.x, pt.y );
746 if( wndPtr->hrgnUpdate == 0 )
747 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
748 else
749 hRgn = CreateRectRgnIndirect( &r2 );
751 else /* entire window or client depending on RDW_FRAME */
753 if( flags & RDW_FRAME )
755 if (wndPtr->hrgnUpdate) hRgn = 1;
756 else wndPtr->hrgnUpdate = 1;
758 else
760 GETCLIENTRECTW( wndPtr, r2 );
761 if( wndPtr->hrgnUpdate == 0 )
762 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
763 else
764 hRgn = CreateRectRgnIndirect( &r2 );
768 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
770 /* In this we cannot leave with zero hRgn */
771 if( hrgnUpdate )
773 hRgn = REGION_CropRgn( hRgn, hrgnUpdate, &r, &pt );
774 GetRgnBox( hRgn, &r2 );
775 if( IsRectEmpty( &r2 ) ) goto END;
777 else if( rectUpdate )
779 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
780 OffsetRect( &r2, pt.x, pt.y );
781 hRgn = CreateRectRgnIndirect( &r2 );
783 else /* entire window or client depending on RDW_FRAME */
785 if( flags & RDW_FRAME )
786 hRgn = 1;
787 else
789 GETCLIENTRECTW( wndPtr, r2 );
790 hRgn = CreateRectRgnIndirect( &r2 );
795 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
797 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
799 /* Erase/update windows, from now on hRgn is a scratch region */
801 hRgn = RDW_Paint( wndPtr, (hRgn == 1) ? 0 : hRgn, flags, 0 );
803 END:
804 if( hRgn > 1 && (hRgn != hrgnUpdate) )
805 DeleteObject(hRgn );
806 WIN_ReleaseWndPtr(wndPtr);
807 return TRUE;
811 /***********************************************************************
812 * UpdateWindow (USER32.@)
814 void WINAPI UpdateWindow( HWND hwnd )
816 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
820 /***********************************************************************
821 * InvalidateRgn (USER32.@)
823 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
825 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
829 /***********************************************************************
830 * InvalidateRect (USER32.@)
832 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
834 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
838 /***********************************************************************
839 * ValidateRgn (USER32.@)
841 void WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
843 RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
847 /***********************************************************************
848 * ValidateRect (USER32.@)
850 void WINAPI ValidateRect( HWND hwnd, const RECT *rect )
852 RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
856 /***********************************************************************
857 * GetUpdateRect (USER32.@)
859 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
861 BOOL retvalue;
862 WND * wndPtr = WIN_FindWndPtr( hwnd );
863 if (!wndPtr) return FALSE;
865 if (rect)
867 if (wndPtr->hrgnUpdate > 1)
869 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
870 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
872 retvalue = FALSE;
873 goto END;
875 GetRgnBox( hrgn, rect );
876 DeleteObject( hrgn );
877 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
879 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
881 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
885 else
886 if( wndPtr->hrgnUpdate == 1 )
888 GetClientRect( hwnd, rect );
889 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
891 else
892 SetRectEmpty( rect );
894 retvalue = (wndPtr->hrgnUpdate >= 1);
895 END:
896 WIN_ReleaseWndPtr(wndPtr);
897 return retvalue;
901 /***********************************************************************
902 * GetUpdateRgn (USER32.@)
904 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
906 INT retval;
907 WND * wndPtr = WIN_FindWndPtr( hwnd );
908 if (!wndPtr) return ERROR;
910 if (wndPtr->hrgnUpdate == 0)
912 SetRectRgn( hrgn, 0, 0, 0, 0 );
913 retval = NULLREGION;
914 goto END;
916 else
917 if (wndPtr->hrgnUpdate == 1)
919 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
920 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
921 retval = SIMPLEREGION;
923 else
925 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
926 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
927 wndPtr->rectWindow.top - wndPtr->rectClient.top );
929 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
930 END:
931 WIN_ReleaseWndPtr(wndPtr);
932 return retval;
936 /***********************************************************************
937 * ExcludeUpdateRgn (USER32.@)
939 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
941 RECT rect;
942 WND * wndPtr;
944 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
946 if (wndPtr->hrgnUpdate)
948 INT ret;
949 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
950 wndPtr->rectWindow.top - wndPtr->rectClient.top,
951 wndPtr->rectWindow.right - wndPtr->rectClient.left,
952 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
953 if( wndPtr->hrgnUpdate > 1 )
955 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
956 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
957 wndPtr->rectWindow.top - wndPtr->rectClient.top );
960 /* do ugly coordinate translations in dce.c */
962 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
963 DeleteObject( hrgn );
964 WIN_ReleaseWndPtr(wndPtr);
965 return ret;
967 WIN_ReleaseWndPtr(wndPtr);
968 return GetClipBox( hdc, &rect );
973 /***********************************************************************
974 * FillRect (USER.81)
975 * NOTE
976 * The Win16 variant doesn't support special color brushes like
977 * the Win32 one, despite the fact that Win16, as well as Win32,
978 * supports special background brushes for a window class.
980 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
982 HBRUSH prevBrush;
984 /* coordinates are logical so we cannot fast-check 'rect',
985 * it will be done later in the PatBlt().
988 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
989 PatBlt( hdc, rect->left, rect->top,
990 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
991 SelectObject( hdc, prevBrush );
992 return 1;
996 /***********************************************************************
997 * FillRect (USER32.@)
999 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1001 HBRUSH prevBrush;
1003 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1004 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1007 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1008 PatBlt( hdc, rect->left, rect->top,
1009 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1010 SelectObject( hdc, prevBrush );
1011 return 1;
1015 /***********************************************************************
1016 * InvertRect (USER.82)
1018 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1020 PatBlt( hdc, rect->left, rect->top,
1021 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1025 /***********************************************************************
1026 * InvertRect (USER32.@)
1028 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1030 return PatBlt( hdc, rect->left, rect->top,
1031 rect->right - rect->left, rect->bottom - rect->top,
1032 DSTINVERT );
1036 /***********************************************************************
1037 * FrameRect (USER32.@)
1039 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1041 HBRUSH prevBrush;
1042 RECT r = *rect;
1044 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1045 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1047 PatBlt( hdc, r.left, r.top, 1,
1048 r.bottom - r.top, PATCOPY );
1049 PatBlt( hdc, r.right - 1, r.top, 1,
1050 r.bottom - r.top, PATCOPY );
1051 PatBlt( hdc, r.left, r.top,
1052 r.right - r.left, 1, PATCOPY );
1053 PatBlt( hdc, r.left, r.bottom - 1,
1054 r.right - r.left, 1, PATCOPY );
1056 SelectObject( hdc, prevBrush );
1057 return TRUE;
1061 /***********************************************************************
1062 * FrameRect (USER.83)
1064 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1066 RECT rect;
1067 CONV_RECT16TO32( rect16, &rect );
1068 return FrameRect( hdc, &rect, hbrush );
1072 /***********************************************************************
1073 * DrawFocusRect (USER.466)
1075 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1077 RECT rect32;
1078 CONV_RECT16TO32( rc, &rect32 );
1079 DrawFocusRect( hdc, &rect32 );
1083 /***********************************************************************
1084 * DrawFocusRect (USER32.@)
1086 * FIXME: PatBlt(PATINVERT) with background brush.
1088 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1090 HBRUSH hOldBrush;
1091 HPEN hOldPen, hNewPen;
1092 INT oldDrawMode, oldBkMode;
1094 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1095 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1096 hOldPen = SelectObject(hdc, hNewPen);
1097 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1098 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1100 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1102 SetBkMode(hdc, oldBkMode);
1103 SetROP2(hdc, oldDrawMode);
1104 SelectObject(hdc, hOldPen);
1105 DeleteObject(hNewPen);
1106 SelectObject(hdc, hOldBrush);
1108 return TRUE;
1112 /**********************************************************************
1113 * DrawAnimatedRects (USER32.@)
1115 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1116 const RECT* lprcFrom,
1117 const RECT* lprcTo )
1119 FIXME_(win)("(0x%x,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1120 return TRUE;
1124 /**********************************************************************
1125 * PAINTING_DrawStateJam
1127 * Jams in the requested type in the dc
1129 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1130 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1131 LPRECT rc, UINT dtflags, BOOL unicode )
1133 HDC memdc;
1134 HBITMAP hbmsave;
1135 BOOL retval;
1136 INT cx = rc->right - rc->left;
1137 INT cy = rc->bottom - rc->top;
1139 switch(opcode)
1141 case DST_TEXT:
1142 case DST_PREFIXTEXT:
1143 if(unicode)
1144 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1145 else
1146 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1148 case DST_ICON:
1149 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1151 case DST_BITMAP:
1152 memdc = CreateCompatibleDC(hdc);
1153 if(!memdc) return FALSE;
1154 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1155 if(!hbmsave)
1157 DeleteDC(memdc);
1158 return FALSE;
1160 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1161 SelectObject(memdc, hbmsave);
1162 DeleteDC(memdc);
1163 return retval;
1165 case DST_COMPLEX:
1166 if(func) {
1167 BOOL bRet;
1168 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1170 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1171 bRet = func(hdc, lp, wp, cx, cy);
1172 /* Restore origin */
1173 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1174 return bRet;
1175 } else
1176 return FALSE;
1178 return FALSE;
1181 /**********************************************************************
1182 * PAINTING_DrawState()
1184 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1185 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1187 HBITMAP hbm, hbmsave;
1188 HFONT hfsave;
1189 HBRUSH hbsave, hbrtmp = 0;
1190 HDC memdc;
1191 RECT rc;
1192 UINT dtflags = DT_NOCLIP;
1193 COLORREF fg, bg;
1194 UINT opcode = flags & 0xf;
1195 INT len = wp;
1196 BOOL retval, tmp;
1198 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1200 if(unicode)
1201 len = strlenW((LPWSTR)lp);
1202 else
1203 len = strlen((LPSTR)lp);
1206 /* Find out what size the image has if not given by caller */
1207 if(!cx || !cy)
1209 SIZE s;
1210 CURSORICONINFO *ici;
1211 BITMAP bm;
1213 switch(opcode)
1215 case DST_TEXT:
1216 case DST_PREFIXTEXT:
1217 if(unicode)
1218 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1219 else
1220 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1221 if(!retval) return FALSE;
1222 break;
1224 case DST_ICON:
1225 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1226 if(!ici) return FALSE;
1227 s.cx = ici->nWidth;
1228 s.cy = ici->nHeight;
1229 GlobalUnlock16((HGLOBAL16)lp);
1230 break;
1232 case DST_BITMAP:
1233 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1234 return FALSE;
1235 s.cx = bm.bmWidth;
1236 s.cy = bm.bmHeight;
1237 break;
1239 case DST_COMPLEX: /* cx and cy must be set in this mode */
1240 return FALSE;
1243 if(!cx) cx = s.cx;
1244 if(!cy) cy = s.cy;
1247 rc.left = x;
1248 rc.top = y;
1249 rc.right = x + cx;
1250 rc.bottom = y + cy;
1252 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1253 dtflags |= DT_RIGHT;
1254 if(opcode == DST_TEXT)
1255 dtflags |= DT_NOPREFIX;
1257 /* For DSS_NORMAL we just jam in the image and return */
1258 if((flags & 0x7ff0) == DSS_NORMAL)
1260 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1263 /* For all other states we need to convert the image to B/W in a local bitmap */
1264 /* before it is displayed */
1265 fg = SetTextColor(hdc, RGB(0, 0, 0));
1266 bg = SetBkColor(hdc, RGB(255, 255, 255));
1267 hbm = (HBITMAP)NULL; hbmsave = (HBITMAP)NULL;
1268 memdc = (HDC)NULL; hbsave = (HBRUSH)NULL;
1269 retval = FALSE; /* assume failure */
1271 /* From here on we must use "goto cleanup" when something goes wrong */
1272 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1273 if(!hbm) goto cleanup;
1274 memdc = CreateCompatibleDC(hdc);
1275 if(!memdc) goto cleanup;
1276 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1277 if(!hbmsave) goto cleanup;
1278 rc.left = rc.top = 0;
1279 rc.right = cx;
1280 rc.bottom = cy;
1281 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1282 SetBkColor(memdc, RGB(255, 255, 255));
1283 SetTextColor(memdc, RGB(0, 0, 0));
1284 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1286 /* DST_COMPLEX may draw text as well,
1287 * so we must be sure that correct font is selected
1289 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1290 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1291 if(hfsave) SelectObject(memdc, hfsave);
1292 if(!tmp) goto cleanup;
1294 /* This state cause the image to be dithered */
1295 if(flags & DSS_UNION)
1297 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1298 if(!hbsave) goto cleanup;
1299 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1300 SelectObject(memdc, hbsave);
1301 if(!tmp) goto cleanup;
1304 if (flags & DSS_DISABLED)
1305 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1306 else if (flags & DSS_DEFAULT)
1307 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1309 /* Draw light or dark shadow */
1310 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1312 if(!hbrtmp) goto cleanup;
1313 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1314 if(!hbsave) goto cleanup;
1315 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1316 SelectObject(hdc, hbsave);
1317 DeleteObject(hbrtmp);
1318 hbrtmp = 0;
1321 if (flags & DSS_DISABLED)
1323 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1324 if(!hbrtmp) goto cleanup;
1326 else if (!hbr)
1328 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1331 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1333 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1335 retval = TRUE; /* We succeeded */
1337 cleanup:
1338 SetTextColor(hdc, fg);
1339 SetBkColor(hdc, bg);
1341 if(hbsave) SelectObject(hdc, hbsave);
1342 if(hbmsave) SelectObject(memdc, hbmsave);
1343 if(hbrtmp) DeleteObject(hbrtmp);
1344 if(hbm) DeleteObject(hbm);
1345 if(memdc) DeleteDC(memdc);
1347 return retval;
1350 /**********************************************************************
1351 * DrawStateA (USER32.@)
1353 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1354 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1355 INT x, INT y, INT cx, INT cy, UINT flags)
1357 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1360 /**********************************************************************
1361 * DrawStateW (USER32.@)
1363 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1364 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1365 INT x, INT y, INT cx, INT cy, UINT flags)
1367 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1371 /**********************************************************************
1372 * DrawState (USER.449)
1374 BOOL16 WINAPI DrawState16( HDC16 hdc, HBRUSH16 hbr, DRAWSTATEPROC16 func, LPARAM ldata,
1375 WPARAM16 wdata, INT16 x, INT16 y, INT16 cx, INT16 cy, UINT16 flags )
1377 struct draw_state_info info;
1378 UINT opcode = flags & 0xf;
1380 if (opcode == DST_TEXT || opcode == DST_PREFIXTEXT)
1382 /* make sure DrawStateA doesn't try to use ldata as a pointer */
1383 if (!wdata) wdata = strlen( MapSL(ldata) );
1384 if (!cx || !cy)
1386 SIZE s;
1387 if (!GetTextExtentPoint32A( hdc, MapSL(ldata), wdata, &s )) return FALSE;
1388 if (!cx) cx = s.cx;
1389 if (!cy) cy = s.cy;
1392 info.proc = func;
1393 info.param = ldata;
1394 return DrawStateA( hdc, hbr, draw_state_callback, (LPARAM)&info, wdata, x, y, cx, cy, flags );
1398 /***********************************************************************
1399 * SelectPalette (USER.282)
1401 HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
1402 BOOL16 bForceBackground )
1404 WORD wBkgPalette = 1;
1406 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1408 HWND hwnd = WindowFromDC( hDC );
1409 if (hwnd)
1411 HWND hForeground = GetForegroundWindow();
1412 /* set primary palette if it's related to current active */
1413 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1416 return GDISelectPalette16( hDC, hPal, wBkgPalette);
1420 /***********************************************************************
1421 * RealizePalette (USER.283)
1423 UINT16 WINAPI RealizePalette16( HDC16 hDC )
1425 UINT16 realized = GDIRealizePalette16( hDC );
1427 /* do not send anything if no colors were changed */
1428 if (realized && IsDCCurrentPalette16( hDC ))
1430 /* send palette change notification */
1431 HWND hWnd = WindowFromDC( hDC );
1432 if (hWnd) SendMessageA( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0L);
1434 return realized;
1438 /***********************************************************************
1439 * UserRealizePalette (USER32.@)
1441 UINT WINAPI UserRealizePalette( HDC hDC )
1443 return RealizePalette16( hDC );