Reinitialize thumb when trackbar size is changed.
[wine.git] / windows / painting.c
blobbb2df1aea83c16d30a97893d56a6d9fb2f4b0e35
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 "gdi.h"
35 #include "user.h"
36 #include "win.h"
37 #include "message.h"
38 #include "dce.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(win);
42 WINE_DECLARE_DEBUG_CHANNEL(nonclient);
44 /* client rect in window coordinates */
46 #define GETCLIENTRECTW( wnd, r ) (r).left = (wnd)->rectClient.left - (wnd)->rectWindow.left; \
47 (r).top = (wnd)->rectClient.top - (wnd)->rectWindow.top; \
48 (r).right = (wnd)->rectClient.right - (wnd)->rectWindow.left; \
49 (r).bottom = (wnd)->rectClient.bottom - (wnd)->rectWindow.top
51 /* PAINT_RedrawWindow() control flags */
52 #define RDW_EX_DELAY_NCPAINT 0x0020
54 /* WIN_UpdateNCRgn() flags */
55 #define UNC_CHECK 0x0001
56 #define UNC_ENTIRE 0x0002
57 #define UNC_REGION 0x0004
58 #define UNC_UPDATE 0x0008
59 #define UNC_DELAY_NCPAINT 0x0010
60 #define UNC_IN_BEGINPAINT 0x0020
62 /* Last COLOR id */
63 #define COLOR_MAX COLOR_GRADIENTINACTIVECAPTION
65 HPALETTE (WINAPI *pfnGDISelectPalette)(HDC hdc, HPALETTE hpal, WORD bkgnd ) = NULL;
66 UINT (WINAPI *pfnGDIRealizePalette)(HDC hdc) = NULL;
69 /***********************************************************************
70 * add_paint_count
72 * Add an increment (normally 1 or -1) to the current paint count of a window.
74 static void add_paint_count( HWND hwnd, int incr )
76 SERVER_START_REQ( inc_window_paint_count )
78 req->handle = hwnd;
79 req->incr = incr;
80 wine_server_call( req );
82 SERVER_END_REQ;
86 /***********************************************************************
87 * crop_rgn
89 * hSrc: Region to crop.
90 * lpRect: Clipping rectangle.
92 * hDst: Region to hold the result (a new region is created if it's 0).
93 * Allowed to be the same region as hSrc in which case everything
94 * will be done in place, with no memory reallocations.
96 * Returns: hDst if success, 0 otherwise.
98 static HRGN crop_rgn( HRGN hDst, HRGN hSrc, const RECT *rect )
100 HRGN h = CreateRectRgnIndirect( rect );
101 if (hDst == 0) hDst = h;
102 CombineRgn( hDst, hSrc, h, RGN_AND );
103 if (hDst != h) DeleteObject( h );
104 return hDst;
108 /***********************************************************************
109 * WIN_HaveToDelayNCPAINT
111 * Currently, in the Wine painting mechanism, the WM_NCPAINT message
112 * is generated as soon as a region intersecting the non-client area
113 * of a window is invalidated.
115 * This technique will work fine for all windows whose parents
116 * have the WS_CLIPCHILDREN style. When the parents have that style,
117 * they are not going to override the contents of their children.
118 * However, when the parent doesn't have that style, Windows relies
119 * on a "painter's algorithm" to display the contents of the windows.
120 * That is, windows are painted from back to front. This includes the
121 * non-client area.
123 * This method looks at the current state of a window to determine
124 * if the sending of the WM_NCPAINT message should be delayed until
125 * the BeginPaint call.
127 * PARAMS:
128 * wndPtr - A Locked window pointer to the window we're
129 * examining.
130 * uncFlags - This is the flag passed to the WIN_UpdateNCRgn
131 * function. This is a shortcut for the cases when
132 * we already know when to avoid scanning all the
133 * parents of a window. If you already know that this
134 * window's NCPAINT should be delayed, set the
135 * UNC_DELAY_NCPAINT flag for this parameter.
137 * This shortcut behavior is implemented in the
138 * RDW_Paint() method.
141 static BOOL WIN_HaveToDelayNCPAINT( HWND hwnd, UINT uncFlags)
144 * Test the shortcut first. (duh)
146 if (uncFlags & UNC_DELAY_NCPAINT)
147 return TRUE;
150 * The UNC_IN_BEGINPAINT flag is set in the BeginPaint
151 * method only. This is another shortcut to avoid going
152 * up the parent's chain of the window to finally
153 * figure-out that none of them has an invalid region.
155 if (uncFlags & UNC_IN_BEGINPAINT)
156 return FALSE;
159 * Scan all the parents of this window to find a window
160 * that doesn't have the WS_CLIPCHILDREN style and that
161 * has an invalid region.
163 while ((hwnd = GetAncestor( hwnd, GA_PARENT )))
165 WND* parentWnd = WIN_FindWndPtr( hwnd );
166 if (parentWnd && !(parentWnd->dwStyle & WS_CLIPCHILDREN) && parentWnd->hrgnUpdate)
168 WIN_ReleaseWndPtr( parentWnd );
169 return TRUE;
171 WIN_ReleaseWndPtr( parentWnd );
173 return FALSE;
176 /***********************************************************************
177 * WIN_UpdateNCRgn
179 * Things to do:
180 * Send WM_NCPAINT if required (when nonclient is invalid or UNC_ENTIRE flag is set)
181 * Crop hrgnUpdate to a client rect, especially if it 1.
182 * If UNC_REGION is set return update region for the client rect.
184 * NOTE: UNC_REGION is mainly for the RDW_Paint() chunk that sends WM_ERASEBKGND message.
185 * The trick is that when the returned region handle may be different from hRgn.
186 * In this case the old hRgn must be considered gone. BUT, if the returned value
187 * is 1 then the hRgn is preserved and RDW_Paint() will have to get
188 * a DC without extra clipping region.
190 static HRGN WIN_UpdateNCRgn(WND* wnd, HRGN hRgn, UINT uncFlags )
192 RECT r;
193 HRGN hClip = 0;
194 HRGN hrgnRet = 0;
196 TRACE_(nonclient)("hwnd %p [%p] hrgn %p, unc %04x, ncf %i\n",
197 wnd->hwndSelf, wnd->hrgnUpdate, hRgn, uncFlags, wnd->flags & WIN_NEEDS_NCPAINT);
199 /* desktop window doesn't have a nonclient area */
200 if(wnd->hwndSelf == GetDesktopWindow())
202 wnd->flags &= ~WIN_NEEDS_NCPAINT;
203 if( wnd->hrgnUpdate > (HRGN)1 )
205 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
206 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
207 hrgnRet = hRgn;
209 else
211 hrgnRet = wnd->hrgnUpdate;
213 return hrgnRet;
216 if ((wnd->hwndSelf == GetForegroundWindow()) &&
217 !(wnd->flags & WIN_NCACTIVATED) )
219 wnd->flags |= WIN_NCACTIVATED;
220 uncFlags |= UNC_ENTIRE;
224 * If the window's non-client area needs to be painted,
226 if ( ( wnd->flags & WIN_NEEDS_NCPAINT ) &&
227 !WIN_HaveToDelayNCPAINT(wnd->hwndSelf, uncFlags) )
229 RECT r2, r3;
231 wnd->flags &= ~WIN_NEEDS_NCPAINT;
232 GETCLIENTRECTW( wnd, r );
234 TRACE_(nonclient)( "\tclient box (%ld,%ld-%ld,%ld), hrgnUpdate %p\n",
235 r.left, r.top, r.right, r.bottom, wnd->hrgnUpdate );
236 if( wnd->hrgnUpdate > (HRGN)1 )
238 /* Check if update rgn overlaps with nonclient area */
240 GetRgnBox( wnd->hrgnUpdate, &r2 );
241 UnionRect( &r3, &r2, &r );
242 if( r3.left != r.left || r3.top != r.top ||
243 r3.right != r.right || r3.bottom != r.bottom ) /* it does */
245 /* crop hrgnUpdate, save old one in hClip - the only
246 * case that places a valid region handle in hClip */
248 hClip = wnd->hrgnUpdate;
249 wnd->hrgnUpdate = crop_rgn( hRgn, hClip, &r );
250 if( uncFlags & UNC_REGION ) hrgnRet = hClip;
253 if( uncFlags & UNC_CHECK )
255 GetRgnBox( wnd->hrgnUpdate, &r3 );
256 if( IsRectEmpty( &r3 ) )
258 /* delete the update region since all invalid
259 * parts were in the nonclient area */
261 DeleteObject( wnd->hrgnUpdate );
262 wnd->hrgnUpdate = 0;
263 if(!(wnd->flags & WIN_INTERNAL_PAINT))
264 add_paint_count( wnd->hwndSelf, -1 );
266 wnd->flags &= ~WIN_NEEDS_ERASEBKGND;
270 if(!hClip && wnd->hrgnUpdate ) goto copyrgn;
272 else
273 if( wnd->hrgnUpdate == (HRGN)1 )/* entire window */
275 if( uncFlags & UNC_UPDATE ) wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
276 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
277 uncFlags |= UNC_ENTIRE;
280 else /* no WM_NCPAINT unless forced */
282 if( wnd->hrgnUpdate > (HRGN)1 )
284 copyrgn:
285 if( uncFlags & UNC_REGION )
287 if (!hRgn) hRgn = CreateRectRgn( 0, 0, 0, 0 );
288 CombineRgn( hRgn, wnd->hrgnUpdate, 0, RGN_COPY );
289 hrgnRet = hRgn;
292 else
293 if( wnd->hrgnUpdate == (HRGN)1 && (uncFlags & UNC_UPDATE) )
295 GETCLIENTRECTW( wnd, r );
296 wnd->hrgnUpdate = CreateRectRgnIndirect( &r );
297 if( uncFlags & UNC_REGION ) hrgnRet = (HRGN)1;
301 if(!hClip && (uncFlags & UNC_ENTIRE) )
303 /* still don't do anything if there is no nonclient area */
304 hClip = (HRGN)(memcmp( &wnd->rectWindow, &wnd->rectClient, sizeof(RECT) ) != 0);
307 if( hClip ) /* NOTE: WM_NCPAINT allows wParam to be 1 */
309 if ( hClip == hrgnRet && hrgnRet > (HRGN)1 ) {
310 hClip = CreateRectRgn( 0, 0, 0, 0 );
311 CombineRgn( hClip, hrgnRet, 0, RGN_COPY );
314 SendMessageA( wnd->hwndSelf, WM_NCPAINT, (WPARAM)hClip, 0L );
315 if( (hClip > (HRGN)1) && (hClip != hRgn) && (hClip != hrgnRet) )
316 DeleteObject( hClip );
318 * Since all Window locks are suspended while processing the WM_NCPAINT
319 * we want to make sure the window still exists before continuing.
321 if (!IsWindow(wnd->hwndSelf))
323 DeleteObject(hrgnRet);
324 hrgnRet=0;
328 TRACE_(nonclient)("returning %p (hClip = %p, hrgnUpdate = %p)\n", hrgnRet, hClip, wnd->hrgnUpdate );
330 return hrgnRet;
334 /***********************************************************************
335 * RDW_ValidateParent [RDW_UpdateRgns() helper]
337 * Validate the portions of parents that are covered by a validated child
338 * wndPtr = child
340 static void RDW_ValidateParent(WND *wndChild)
342 HWND parent;
343 HRGN hrg;
345 if (wndChild->hrgnUpdate == (HRGN)1 ) {
346 RECT r;
347 r.left = 0;
348 r.top = 0;
349 r.right = wndChild->rectWindow.right - wndChild->rectWindow.left;
350 r.bottom = wndChild->rectWindow.bottom - wndChild->rectWindow.top;
351 hrg = CreateRectRgnIndirect( &r );
352 } else
353 hrg = wndChild->hrgnUpdate;
355 parent = GetAncestor( wndChild->hwndSelf, GA_PARENT );
356 while (parent && parent != GetDesktopWindow())
358 WND *wndParent = WIN_FindWndPtr( parent );
359 if (wndParent && !(wndParent->dwStyle & WS_CLIPCHILDREN))
361 if (wndParent->hrgnUpdate != 0)
363 POINT ptOffset;
364 RECT rect, rectParent;
365 if( wndParent->hrgnUpdate == (HRGN)1 )
367 RECT r;
369 r.left = 0;
370 r.top = 0;
371 r.right = wndParent->rectWindow.right - wndParent->rectWindow.left;
372 r.bottom = wndParent->rectWindow.bottom - wndParent->rectWindow.top;
374 wndParent->hrgnUpdate = CreateRectRgnIndirect( &r );
376 /* we must offset the child region by the offset of the child rect in the parent */
377 GetWindowRect(wndParent->hwndSelf, &rectParent);
378 GetWindowRect(wndChild->hwndSelf, &rect);
379 ptOffset.x = rect.left - rectParent.left;
380 ptOffset.y = rect.top - rectParent.top;
381 OffsetRgn( hrg, ptOffset.x, ptOffset.y );
382 if (CombineRgn( wndParent->hrgnUpdate, wndParent->hrgnUpdate, hrg, RGN_DIFF ) == NULLREGION)
384 /* the update region has become empty */
385 DeleteObject( wndParent->hrgnUpdate );
386 wndParent->hrgnUpdate = 0;
387 wndParent->flags &= ~WIN_NEEDS_ERASEBKGND;
388 if( !(wndParent->flags & WIN_INTERNAL_PAINT) )
389 add_paint_count( wndParent->hwndSelf, -1 );
391 OffsetRgn( hrg, -ptOffset.x, -ptOffset.y );
394 WIN_ReleaseWndPtr( wndParent );
395 parent = GetAncestor( parent, GA_PARENT );
397 if (hrg != wndChild->hrgnUpdate) DeleteObject( hrg );
400 /***********************************************************************
401 * RDW_UpdateRgns [RedrawWindow() helper]
403 * Walks the window tree and adds/removes parts of the hRgn to/from update
404 * regions of windows that overlap it. Also, manages internal paint flags.
406 * NOTE: Walks the window tree so the caller must lock it.
407 * MUST preserve hRgn (can modify but then has to restore).
409 static void RDW_UpdateRgns( WND* wndPtr, HRGN hRgn, UINT flags, BOOL firstRecursLevel )
412 * Called only when one of the following is set:
413 * (RDW_INVALIDATE | RDW_VALIDATE | RDW_INTERNALPAINT | RDW_NOINTERNALPAINT)
416 BOOL bHadOne = wndPtr->hrgnUpdate && hRgn;
417 BOOL bChildren = (!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
418 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) );
419 RECT r;
421 r.left = 0;
422 r.top = 0;
423 r.right = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
424 r.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
426 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", wndPtr->hwndSelf, wndPtr->hrgnUpdate, hRgn, flags );
428 if( flags & RDW_INVALIDATE )
430 if( hRgn > (HRGN)1 )
432 switch ((UINT)wndPtr->hrgnUpdate)
434 default:
435 CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_OR );
436 /* fall through */
437 case 0:
438 wndPtr->hrgnUpdate = crop_rgn( wndPtr->hrgnUpdate,
439 wndPtr->hrgnUpdate ? wndPtr->hrgnUpdate : hRgn,
440 &r );
441 if( !bHadOne )
443 GetRgnBox( wndPtr->hrgnUpdate, &r );
444 if( IsRectEmpty( &r ) )
446 DeleteObject( wndPtr->hrgnUpdate );
447 wndPtr->hrgnUpdate = 0;
448 goto end;
451 break;
452 case 1: /* already an entire window */
453 break;
456 else if( hRgn == (HRGN)1 )
458 if( wndPtr->hrgnUpdate > (HRGN)1 )
459 DeleteObject( wndPtr->hrgnUpdate );
460 wndPtr->hrgnUpdate = (HRGN)1;
462 else
463 hRgn = wndPtr->hrgnUpdate; /* this is a trick that depends on code in PAINT_RedrawWindow() */
465 if( !bHadOne && !(wndPtr->flags & WIN_INTERNAL_PAINT) )
466 add_paint_count( wndPtr->hwndSelf, 1 );
468 if (flags & RDW_FRAME) wndPtr->flags |= WIN_NEEDS_NCPAINT;
469 if (flags & RDW_ERASE) wndPtr->flags |= WIN_NEEDS_ERASEBKGND;
470 flags |= RDW_FRAME;
472 else if( flags & RDW_VALIDATE )
474 if( wndPtr->hrgnUpdate )
476 if( hRgn > (HRGN)1 )
478 if( wndPtr->hrgnUpdate == (HRGN)1 )
479 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r );
481 if( CombineRgn( wndPtr->hrgnUpdate, wndPtr->hrgnUpdate, hRgn, RGN_DIFF )
482 == NULLREGION )
484 DeleteObject( wndPtr->hrgnUpdate );
485 wndPtr->hrgnUpdate = 0;
488 else /* validate everything */
490 if( wndPtr->hrgnUpdate > (HRGN)1 ) DeleteObject( wndPtr->hrgnUpdate );
491 wndPtr->hrgnUpdate = 0;
494 if( !wndPtr->hrgnUpdate )
496 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
497 if( !(wndPtr->flags & WIN_INTERNAL_PAINT) )
498 add_paint_count( wndPtr->hwndSelf, -1 );
502 if (flags & RDW_NOFRAME) wndPtr->flags &= ~WIN_NEEDS_NCPAINT;
503 if (flags & RDW_NOERASE) wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
507 if ((firstRecursLevel) && (wndPtr->hrgnUpdate != 0) && (flags & RDW_UPDATENOW))
508 RDW_ValidateParent(wndPtr); /* validate parent covered by region */
510 /* in/validate child windows that intersect with the region if it
511 * is a valid handle. */
513 if( flags & (RDW_INVALIDATE | RDW_VALIDATE) )
515 HWND *list;
516 if( hRgn > (HRGN)1 && bChildren && (list = WIN_ListChildren( wndPtr->hwndSelf )))
518 POINT ptTotal, prevOrigin = {0,0};
519 POINT ptClient;
520 INT i;
522 ptClient.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
523 ptClient.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
525 for(i = ptTotal.x = ptTotal.y = 0; list[i]; i++)
527 WND *wnd = WIN_FindWndPtr( list[i] );
528 if (!wnd) continue;
529 if( wnd->dwStyle & WS_VISIBLE )
531 POINT ptOffset;
533 r.left = wnd->rectWindow.left + ptClient.x;
534 r.right = wnd->rectWindow.right + ptClient.x;
535 r.top = wnd->rectWindow.top + ptClient.y;
536 r.bottom = wnd->rectWindow.bottom + ptClient.y;
538 ptOffset.x = r.left - prevOrigin.x;
539 ptOffset.y = r.top - prevOrigin.y;
540 OffsetRect( &r, -ptTotal.x, -ptTotal.y );
542 if( RectInRegion( hRgn, &r ) )
544 OffsetRgn( hRgn, -ptOffset.x, -ptOffset.y );
545 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
546 prevOrigin.x = r.left + ptTotal.x;
547 prevOrigin.y = r.top + ptTotal.y;
548 ptTotal.x += ptOffset.x;
549 ptTotal.y += ptOffset.y;
552 WIN_ReleaseWndPtr( wnd );
554 HeapFree( GetProcessHeap(), 0, list );
555 OffsetRgn( hRgn, ptTotal.x, ptTotal.y );
556 bChildren = 0;
560 /* handle hRgn == 1 (alias for entire window) and/or internal paint recursion */
562 if( bChildren )
564 HWND *list;
565 if ((list = WIN_ListChildren( wndPtr->hwndSelf )))
567 INT i;
568 for (i = 0; list[i]; i++)
570 WND *wnd = WIN_FindWndPtr( list[i] );
571 if (!wnd) continue;
572 if( wnd->dwStyle & WS_VISIBLE )
573 RDW_UpdateRgns( wnd, hRgn, flags, FALSE );
574 WIN_ReleaseWndPtr( wnd );
576 HeapFree( GetProcessHeap(), 0, list );
580 end:
582 /* Set/clear internal paint flag */
584 if (flags & RDW_INTERNALPAINT)
586 if ( !wndPtr->hrgnUpdate && !(wndPtr->flags & WIN_INTERNAL_PAINT))
587 add_paint_count( wndPtr->hwndSelf, 1 );
588 wndPtr->flags |= WIN_INTERNAL_PAINT;
590 else if (flags & RDW_NOINTERNALPAINT)
592 if ( !wndPtr->hrgnUpdate && (wndPtr->flags & WIN_INTERNAL_PAINT))
593 add_paint_count( wndPtr->hwndSelf, -1 );
594 wndPtr->flags &= ~WIN_INTERNAL_PAINT;
598 /***********************************************************************
599 * RDW_Paint [RedrawWindow() helper]
601 * Walks the window tree and paints/erases windows that have
602 * nonzero update regions according to redraw flags. hrgn is a scratch
603 * region passed down during recursion. Must not be 1.
606 static HRGN RDW_Paint( WND* wndPtr, HRGN hrgn, UINT flags, UINT ex )
608 /* NOTE: wndPtr is locked by caller.
610 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
611 * SendMessage() calls. This is a comment inside DefWindowProc() source
612 * from 16-bit SDK:
614 * This message avoids lots of inter-app message traffic
615 * by switching to the other task and continuing the
616 * recursion there.
618 * wParam = flags
619 * LOWORD(lParam) = hrgnClip
620 * HIWORD(lParam) = hwndSkip (not used; always NULL)
623 HDC hDC;
624 HWND hWnd = wndPtr->hwndSelf;
625 BOOL bIcon = ((wndPtr->dwStyle & WS_MINIMIZE) && GetClassLongA(hWnd, GCL_HICON));
627 /* Erase/update the window itself ... */
629 TRACE("\thwnd %p [%p] -> hrgn [%p], flags [%04x]\n", hWnd, wndPtr->hrgnUpdate, hrgn, flags );
632 * Check if this window should delay it's processing of WM_NCPAINT.
633 * See WIN_HaveToDelayNCPAINT for a description of the mechanism
635 if ((ex & RDW_EX_DELAY_NCPAINT) || WIN_HaveToDelayNCPAINT(wndPtr->hwndSelf, 0) )
636 ex |= RDW_EX_DELAY_NCPAINT;
638 if (flags & RDW_UPDATENOW)
640 if (wndPtr->hrgnUpdate) /* wm_painticon wparam is 1 */
641 SendMessageW( hWnd, (bIcon) ? WM_PAINTICON : WM_PAINT, bIcon, 0 );
643 else if (flags & RDW_ERASENOW)
645 UINT dcx = DCX_INTERSECTRGN | DCX_USESTYLE | DCX_KEEPCLIPRGN | DCX_WINDOWPAINT | DCX_CACHE;
646 HRGN hrgnRet;
648 hrgnRet = WIN_UpdateNCRgn(wndPtr,
649 hrgn,
650 UNC_REGION | UNC_CHECK |
651 ((ex & RDW_EX_DELAY_NCPAINT) ? UNC_DELAY_NCPAINT : 0) );
653 if( hrgnRet )
655 if( hrgnRet > (HRGN)1 ) hrgn = hrgnRet; else hrgnRet = 0; /* entire client */
656 if( wndPtr->flags & WIN_NEEDS_ERASEBKGND )
658 if( bIcon ) dcx |= DCX_WINDOW;
659 else
660 if( hrgnRet )
661 OffsetRgn( hrgnRet, wndPtr->rectWindow.left - wndPtr->rectClient.left,
662 wndPtr->rectWindow.top - wndPtr->rectClient.top);
663 else
664 dcx &= ~DCX_INTERSECTRGN;
665 if (( hDC = GetDCEx( hWnd, hrgnRet, dcx )) )
667 if (SendMessageW( hWnd, (bIcon) ? WM_ICONERASEBKGND : WM_ERASEBKGND,
668 (WPARAM)hDC, 0 ))
669 wndPtr->flags &= ~WIN_NEEDS_ERASEBKGND;
670 ReleaseDC( hWnd, hDC );
676 if( !IsWindow(hWnd) ) return hrgn;
678 /* ... and its child windows */
680 if(!(flags & RDW_NOCHILDREN) && !(wndPtr->dwStyle & WS_MINIMIZE) &&
681 ((flags & RDW_ALLCHILDREN) || !(wndPtr->dwStyle & WS_CLIPCHILDREN)) )
683 HWND *list, *phwnd;
685 if( (list = WIN_ListChildren( wndPtr->hwndSelf )) )
687 for (phwnd = list; *phwnd; phwnd++)
689 if (!(wndPtr = WIN_FindWndPtr( *phwnd ))) continue;
690 if ( (wndPtr->dwStyle & WS_VISIBLE) &&
691 (wndPtr->hrgnUpdate || (wndPtr->flags & WIN_INTERNAL_PAINT)) )
692 hrgn = RDW_Paint( wndPtr, hrgn, flags, ex );
693 WIN_ReleaseWndPtr(wndPtr);
695 HeapFree( GetProcessHeap(), 0, list );
699 return hrgn;
703 /***********************************************************************
704 * dump_rdw_flags
706 static void dump_rdw_flags(UINT flags)
708 TRACE("flags:");
709 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
710 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
711 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
712 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
713 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
714 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
715 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
716 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
717 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
718 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
719 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
720 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
722 #define RDW_FLAGS \
723 (RDW_INVALIDATE | \
724 RDW_INTERNALPAINT | \
725 RDW_ERASE | \
726 RDW_VALIDATE | \
727 RDW_NOINTERNALPAINT | \
728 RDW_NOERASE | \
729 RDW_NOCHILDREN | \
730 RDW_ALLCHILDREN | \
731 RDW_UPDATENOW | \
732 RDW_ERASENOW | \
733 RDW_FRAME | \
734 RDW_NOFRAME)
736 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
737 TRACE("\n");
738 #undef RDW_FLAGS
742 /***********************************************************************
743 * RedrawWindow (USER32.@)
745 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rectUpdate,
746 HRGN hrgnUpdate, UINT flags )
748 HRGN hRgn = 0;
749 RECT r, r2;
750 POINT pt;
751 WND* wndPtr;
753 if (!hwnd) hwnd = GetDesktopWindow();
755 /* check if the window or its parents are visible/not minimized */
757 if (!WIN_IsWindowDrawable( hwnd, !(flags & RDW_FRAME) )) return TRUE;
759 /* process pending events and messages before painting */
760 if (flags & RDW_UPDATENOW)
761 MsgWaitForMultipleObjects( 0, NULL, FALSE, 0, QS_ALLINPUT );
763 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return FALSE;
764 if (TRACE_ON(win))
766 if( hrgnUpdate )
768 GetRgnBox( hrgnUpdate, &r );
769 TRACE( "%p (%p) NULL %p box (%ld,%ld-%ld,%ld) flags=%04x\n",
770 hwnd, wndPtr->hrgnUpdate, hrgnUpdate, r.left, r.top, r.right, r.bottom, flags );
772 else
774 if( rectUpdate )
775 r = *rectUpdate;
776 else
777 SetRectEmpty( &r );
778 TRACE( "%p (%p) %s %ld,%ld-%ld,%ld %p flags=%04x\n",
779 hwnd, wndPtr->hrgnUpdate, rectUpdate ? "rect" : "NULL", r.left,
780 r.top, r.right, r.bottom, hrgnUpdate, flags );
782 dump_rdw_flags(flags);
785 /* prepare an update region in window coordinates */
787 if (((flags & (RDW_INVALIDATE|RDW_FRAME)) == (RDW_INVALIDATE|RDW_FRAME)) ||
788 ((flags & (RDW_VALIDATE|RDW_NOFRAME)) == (RDW_VALIDATE|RDW_NOFRAME)))
789 r = wndPtr->rectWindow;
790 else
791 r = wndPtr->rectClient;
793 pt.x = wndPtr->rectClient.left - wndPtr->rectWindow.left;
794 pt.y = wndPtr->rectClient.top - wndPtr->rectWindow.top;
795 OffsetRect( &r, -wndPtr->rectClient.left, -wndPtr->rectClient.top );
797 if (flags & RDW_INVALIDATE) /* ------------------------- Invalidate */
799 /* If the window doesn't have hrgnUpdate we leave hRgn zero
800 * and put a new region straight into wndPtr->hrgnUpdate
801 * so that RDW_UpdateRgns() won't have to do any extra work.
804 if( hrgnUpdate )
806 if( wndPtr->hrgnUpdate )
808 hRgn = CreateRectRgn( 0, 0, 0, 0 );
809 CombineRgn( hRgn, hrgnUpdate, 0, RGN_COPY );
810 OffsetRgn( hRgn, pt.x, pt.y );
812 else
814 wndPtr->hrgnUpdate = crop_rgn( 0, hrgnUpdate, &r );
815 OffsetRgn( wndPtr->hrgnUpdate, pt.x, pt.y );
818 else if( rectUpdate )
820 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
821 OffsetRect( &r2, pt.x, pt.y );
822 if( wndPtr->hrgnUpdate == 0 )
823 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
824 else
825 hRgn = CreateRectRgnIndirect( &r2 );
827 else /* entire window or client depending on RDW_FRAME */
829 if( flags & RDW_FRAME )
831 if (wndPtr->hrgnUpdate) hRgn = (HRGN)1;
832 else wndPtr->hrgnUpdate = (HRGN)1;
834 else
836 GETCLIENTRECTW( wndPtr, r2 );
837 if( wndPtr->hrgnUpdate == 0 )
838 wndPtr->hrgnUpdate = CreateRectRgnIndirect( &r2 );
839 else
840 hRgn = CreateRectRgnIndirect( &r2 );
844 else if (flags & RDW_VALIDATE) /* ------------------------- Validate */
846 /* In this we cannot leave with zero hRgn */
847 if( hrgnUpdate )
849 hRgn = crop_rgn( hRgn, hrgnUpdate, &r );
850 OffsetRgn( hRgn, pt.x, pt.y );
851 GetRgnBox( hRgn, &r2 );
852 if( IsRectEmpty( &r2 ) ) goto END;
854 else if( rectUpdate )
856 if( !IntersectRect( &r2, &r, rectUpdate ) ) goto END;
857 OffsetRect( &r2, pt.x, pt.y );
858 hRgn = CreateRectRgnIndirect( &r2 );
860 else /* entire window or client depending on RDW_NOFRAME */
862 if( flags & RDW_NOFRAME )
863 hRgn = (HRGN)1;
864 else
866 GETCLIENTRECTW( wndPtr, r2 );
867 hRgn = CreateRectRgnIndirect( &r2 );
872 /* At this point hRgn is either an update region in window coordinates or 1 or 0 */
874 RDW_UpdateRgns( wndPtr, hRgn, flags, TRUE );
876 /* Erase/update windows, from now on hRgn is a scratch region */
878 hRgn = RDW_Paint( wndPtr, (hRgn == (HRGN)1) ? 0 : hRgn, flags, 0 );
880 END:
881 if( hRgn > (HRGN)1 && (hRgn != hrgnUpdate) )
882 DeleteObject(hRgn );
883 WIN_ReleaseWndPtr(wndPtr);
884 return TRUE;
888 /***********************************************************************
889 * UpdateWindow (USER32.@)
891 BOOL WINAPI UpdateWindow( HWND hwnd )
893 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
897 /***********************************************************************
898 * InvalidateRgn (USER32.@)
900 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
902 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
906 /***********************************************************************
907 * InvalidateRect (USER32.@)
909 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
911 return RedrawWindow( hwnd, rect, 0, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
915 /***********************************************************************
916 * ValidateRgn (USER32.@)
918 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
920 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE | RDW_NOCHILDREN );
924 /***********************************************************************
925 * ValidateRect (USER32.@)
927 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
929 return RedrawWindow( hwnd, rect, 0, RDW_VALIDATE | RDW_NOCHILDREN );
933 /***********************************************************************
934 * GetUpdateRect (USER32.@)
936 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
938 BOOL retvalue;
939 WND * wndPtr = WIN_FindWndPtr( hwnd );
940 if (!wndPtr) return FALSE;
942 if (rect)
944 if (wndPtr->hrgnUpdate > (HRGN)1)
946 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
947 if (GetUpdateRgn( hwnd, hrgn, erase ) == ERROR)
949 retvalue = FALSE;
950 goto END;
952 GetRgnBox( hrgn, rect );
953 DeleteObject( hrgn );
954 if (GetClassLongA(wndPtr->hwndSelf, GCL_STYLE) & CS_OWNDC)
956 if (GetMapMode(wndPtr->dce->hDC) != MM_TEXT)
958 DPtoLP (wndPtr->dce->hDC, (LPPOINT)rect, 2);
962 else
963 if( wndPtr->hrgnUpdate == (HRGN)1 )
965 GetClientRect( hwnd, rect );
966 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_ERASENOW | RDW_NOCHILDREN );
968 else
969 SetRectEmpty( rect );
971 retvalue = (wndPtr->hrgnUpdate >= (HRGN)1);
972 END:
973 WIN_ReleaseWndPtr(wndPtr);
974 return retvalue;
978 /***********************************************************************
979 * GetUpdateRgn (USER32.@)
981 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
983 INT retval;
984 WND * wndPtr = WIN_FindWndPtr( hwnd );
985 if (!wndPtr) return ERROR;
987 if (wndPtr->hrgnUpdate == 0)
989 SetRectRgn( hrgn, 0, 0, 0, 0 );
990 retval = NULLREGION;
991 goto END;
993 else
994 if (wndPtr->hrgnUpdate == (HRGN)1)
996 SetRectRgn( hrgn, 0, 0, wndPtr->rectClient.right - wndPtr->rectClient.left,
997 wndPtr->rectClient.bottom - wndPtr->rectClient.top );
998 retval = SIMPLEREGION;
1000 else
1002 retval = CombineRgn( hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY );
1003 OffsetRgn( hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1004 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1006 if (erase) RedrawWindow( hwnd, NULL, 0, RDW_ERASENOW | RDW_NOCHILDREN );
1007 END:
1008 WIN_ReleaseWndPtr(wndPtr);
1009 return retval;
1013 /***********************************************************************
1014 * ExcludeUpdateRgn (USER32.@)
1016 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1018 RECT rect;
1019 WND * wndPtr;
1021 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return ERROR;
1023 if (wndPtr->hrgnUpdate)
1025 INT ret;
1026 HRGN hrgn = CreateRectRgn(wndPtr->rectWindow.left - wndPtr->rectClient.left,
1027 wndPtr->rectWindow.top - wndPtr->rectClient.top,
1028 wndPtr->rectWindow.right - wndPtr->rectClient.left,
1029 wndPtr->rectWindow.bottom - wndPtr->rectClient.top);
1030 if( wndPtr->hrgnUpdate > (HRGN)1 )
1032 CombineRgn(hrgn, wndPtr->hrgnUpdate, 0, RGN_COPY);
1033 OffsetRgn(hrgn, wndPtr->rectWindow.left - wndPtr->rectClient.left,
1034 wndPtr->rectWindow.top - wndPtr->rectClient.top );
1037 /* do ugly coordinate translations in dce.c */
1039 ret = DCE_ExcludeRgn( hdc, hwnd, hrgn );
1040 DeleteObject( hrgn );
1041 WIN_ReleaseWndPtr(wndPtr);
1042 return ret;
1044 WIN_ReleaseWndPtr(wndPtr);
1045 return GetClipBox( hdc, &rect );
1050 /***********************************************************************
1051 * FillRect (USER.81)
1052 * NOTE
1053 * The Win16 variant doesn't support special color brushes like
1054 * the Win32 one, despite the fact that Win16, as well as Win32,
1055 * supports special background brushes for a window class.
1057 INT16 WINAPI FillRect16( HDC16 hdc, const RECT16 *rect, HBRUSH16 hbrush )
1059 HBRUSH prevBrush;
1061 /* coordinates are logical so we cannot fast-check 'rect',
1062 * it will be done later in the PatBlt().
1065 if (!(prevBrush = SelectObject( HDC_32(hdc), HBRUSH_32(hbrush) ))) return 0;
1066 PatBlt( HDC_32(hdc), rect->left, rect->top,
1067 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1068 SelectObject( HDC_32(hdc), prevBrush );
1069 return 1;
1073 /***********************************************************************
1074 * FillRect (USER32.@)
1076 INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1078 HBRUSH prevBrush;
1080 if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) {
1081 hbrush = GetSysColorBrush( (INT) hbrush - 1 );
1084 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1085 PatBlt( hdc, rect->left, rect->top,
1086 rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1087 SelectObject( hdc, prevBrush );
1088 return 1;
1092 /***********************************************************************
1093 * InvertRect (USER.82)
1095 void WINAPI InvertRect16( HDC16 hdc, const RECT16 *rect )
1097 PatBlt( HDC_32(hdc), rect->left, rect->top,
1098 rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
1102 /***********************************************************************
1103 * InvertRect (USER32.@)
1105 BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
1107 return PatBlt( hdc, rect->left, rect->top,
1108 rect->right - rect->left, rect->bottom - rect->top,
1109 DSTINVERT );
1113 /***********************************************************************
1114 * FrameRect (USER32.@)
1116 INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
1118 HBRUSH prevBrush;
1119 RECT r = *rect;
1121 if ( (r.right <= r.left) || (r.bottom <= r.top) ) return 0;
1122 if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;
1124 PatBlt( hdc, r.left, r.top, 1,
1125 r.bottom - r.top, PATCOPY );
1126 PatBlt( hdc, r.right - 1, r.top, 1,
1127 r.bottom - r.top, PATCOPY );
1128 PatBlt( hdc, r.left, r.top,
1129 r.right - r.left, 1, PATCOPY );
1130 PatBlt( hdc, r.left, r.bottom - 1,
1131 r.right - r.left, 1, PATCOPY );
1133 SelectObject( hdc, prevBrush );
1134 return TRUE;
1138 /***********************************************************************
1139 * FrameRect (USER.83)
1141 INT16 WINAPI FrameRect16( HDC16 hdc, const RECT16 *rect16, HBRUSH16 hbrush )
1143 RECT rect;
1144 CONV_RECT16TO32( rect16, &rect );
1145 return FrameRect( HDC_32(hdc), &rect, HBRUSH_32(hbrush) );
1149 /***********************************************************************
1150 * DrawFocusRect (USER.466)
1152 void WINAPI DrawFocusRect16( HDC16 hdc, const RECT16* rc )
1154 RECT rect32;
1155 CONV_RECT16TO32( rc, &rect32 );
1156 DrawFocusRect( HDC_32(hdc), &rect32 );
1160 /***********************************************************************
1161 * DrawFocusRect (USER32.@)
1163 * FIXME: PatBlt(PATINVERT) with background brush.
1165 BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
1167 HBRUSH hOldBrush;
1168 HPEN hOldPen, hNewPen;
1169 INT oldDrawMode, oldBkMode;
1171 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1172 hNewPen = CreatePen(PS_ALTERNATE, 1, GetSysColor(COLOR_WINDOWTEXT));
1173 hOldPen = SelectObject(hdc, hNewPen);
1174 oldDrawMode = SetROP2(hdc, R2_XORPEN);
1175 oldBkMode = SetBkMode(hdc, TRANSPARENT);
1177 Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);
1179 SetBkMode(hdc, oldBkMode);
1180 SetROP2(hdc, oldDrawMode);
1181 SelectObject(hdc, hOldPen);
1182 DeleteObject(hNewPen);
1183 SelectObject(hdc, hOldBrush);
1185 return TRUE;
1189 /**********************************************************************
1190 * DrawAnimatedRects (USER32.@)
1192 BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni,
1193 const RECT* lprcFrom,
1194 const RECT* lprcTo )
1196 FIXME_(win)("(%p,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
1197 return TRUE;
1201 /**********************************************************************
1202 * PAINTING_DrawStateJam
1204 * Jams in the requested type in the dc
1206 static BOOL PAINTING_DrawStateJam(HDC hdc, UINT opcode,
1207 DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1208 LPRECT rc, UINT dtflags, BOOL unicode )
1210 HDC memdc;
1211 HBITMAP hbmsave;
1212 BOOL retval;
1213 INT cx = rc->right - rc->left;
1214 INT cy = rc->bottom - rc->top;
1216 switch(opcode)
1218 case DST_TEXT:
1219 case DST_PREFIXTEXT:
1220 if(unicode)
1221 return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
1222 else
1223 return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);
1225 case DST_ICON:
1226 return DrawIcon(hdc, rc->left, rc->top, (HICON)lp);
1228 case DST_BITMAP:
1229 memdc = CreateCompatibleDC(hdc);
1230 if(!memdc) return FALSE;
1231 hbmsave = (HBITMAP)SelectObject(memdc, (HBITMAP)lp);
1232 if(!hbmsave)
1234 DeleteDC(memdc);
1235 return FALSE;
1237 retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
1238 SelectObject(memdc, hbmsave);
1239 DeleteDC(memdc);
1240 return retval;
1242 case DST_COMPLEX:
1243 if(func) {
1244 BOOL bRet;
1245 /* DRAWSTATEPROC assumes that it draws at the center of coordinates */
1247 OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
1248 bRet = func(hdc, lp, wp, cx, cy);
1249 /* Restore origin */
1250 OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
1251 return bRet;
1252 } else
1253 return FALSE;
1255 return FALSE;
1258 /**********************************************************************
1259 * PAINTING_DrawState()
1261 static BOOL PAINTING_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
1262 INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
1264 HBITMAP hbm, hbmsave;
1265 HFONT hfsave;
1266 HBRUSH hbsave, hbrtmp = 0;
1267 HDC memdc;
1268 RECT rc;
1269 UINT dtflags = DT_NOCLIP;
1270 COLORREF fg, bg;
1271 UINT opcode = flags & 0xf;
1272 INT len = wp;
1273 BOOL retval, tmp;
1275 if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len) /* The string is '\0' terminated */
1277 if(unicode)
1278 len = strlenW((LPWSTR)lp);
1279 else
1280 len = strlen((LPSTR)lp);
1283 /* Find out what size the image has if not given by caller */
1284 if(!cx || !cy)
1286 SIZE s;
1287 CURSORICONINFO *ici;
1288 BITMAP bm;
1290 switch(opcode)
1292 case DST_TEXT:
1293 case DST_PREFIXTEXT:
1294 if(unicode)
1295 retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
1296 else
1297 retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
1298 if(!retval) return FALSE;
1299 break;
1301 case DST_ICON:
1302 ici = (CURSORICONINFO *)GlobalLock16((HGLOBAL16)lp);
1303 if(!ici) return FALSE;
1304 s.cx = ici->nWidth;
1305 s.cy = ici->nHeight;
1306 GlobalUnlock16((HGLOBAL16)lp);
1307 break;
1309 case DST_BITMAP:
1310 if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm))
1311 return FALSE;
1312 s.cx = bm.bmWidth;
1313 s.cy = bm.bmHeight;
1314 break;
1316 case DST_COMPLEX: /* cx and cy must be set in this mode */
1317 return FALSE;
1320 if(!cx) cx = s.cx;
1321 if(!cy) cy = s.cy;
1324 rc.left = x;
1325 rc.top = y;
1326 rc.right = x + cx;
1327 rc.bottom = y + cy;
1329 if(flags & DSS_RIGHT) /* This one is not documented in the win32.hlp file */
1330 dtflags |= DT_RIGHT;
1331 if(opcode == DST_TEXT)
1332 dtflags |= DT_NOPREFIX;
1334 /* For DSS_NORMAL we just jam in the image and return */
1335 if((flags & 0x7ff0) == DSS_NORMAL)
1337 return PAINTING_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
1340 /* For all other states we need to convert the image to B/W in a local bitmap */
1341 /* before it is displayed */
1342 fg = SetTextColor(hdc, RGB(0, 0, 0));
1343 bg = SetBkColor(hdc, RGB(255, 255, 255));
1344 hbm = NULL; hbmsave = NULL;
1345 memdc = NULL; hbsave = NULL;
1346 retval = FALSE; /* assume failure */
1348 /* From here on we must use "goto cleanup" when something goes wrong */
1349 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1350 if(!hbm) goto cleanup;
1351 memdc = CreateCompatibleDC(hdc);
1352 if(!memdc) goto cleanup;
1353 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1354 if(!hbmsave) goto cleanup;
1355 rc.left = rc.top = 0;
1356 rc.right = cx;
1357 rc.bottom = cy;
1358 if(!FillRect(memdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH))) goto cleanup;
1359 SetBkColor(memdc, RGB(255, 255, 255));
1360 SetTextColor(memdc, RGB(0, 0, 0));
1361 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1363 /* DST_COMPLEX may draw text as well,
1364 * so we must be sure that correct font is selected
1366 if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
1367 tmp = PAINTING_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
1368 if(hfsave) SelectObject(memdc, hfsave);
1369 if(!tmp) goto cleanup;
1371 /* This state cause the image to be dithered */
1372 if(flags & DSS_UNION)
1374 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1375 if(!hbsave) goto cleanup;
1376 tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
1377 SelectObject(memdc, hbsave);
1378 if(!tmp) goto cleanup;
1381 if (flags & DSS_DISABLED)
1382 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
1383 else if (flags & DSS_DEFAULT)
1384 hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1386 /* Draw light or dark shadow */
1387 if (flags & (DSS_DISABLED|DSS_DEFAULT))
1389 if(!hbrtmp) goto cleanup;
1390 hbsave = (HBRUSH)SelectObject(hdc, hbrtmp);
1391 if(!hbsave) goto cleanup;
1392 if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1393 SelectObject(hdc, hbsave);
1394 DeleteObject(hbrtmp);
1395 hbrtmp = 0;
1398 if (flags & DSS_DISABLED)
1400 hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
1401 if(!hbrtmp) goto cleanup;
1403 else if (!hbr)
1405 hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);
1408 hbsave = (HBRUSH)SelectObject(hdc, hbr);
1410 if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
1412 retval = TRUE; /* We succeeded */
1414 cleanup:
1415 SetTextColor(hdc, fg);
1416 SetBkColor(hdc, bg);
1418 if(hbsave) SelectObject(hdc, hbsave);
1419 if(hbmsave) SelectObject(memdc, hbmsave);
1420 if(hbrtmp) DeleteObject(hbrtmp);
1421 if(hbm) DeleteObject(hbm);
1422 if(memdc) DeleteDC(memdc);
1424 return retval;
1427 /**********************************************************************
1428 * DrawStateA (USER32.@)
1430 BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
1431 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1432 INT x, INT y, INT cx, INT cy, UINT flags)
1434 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
1437 /**********************************************************************
1438 * DrawStateW (USER32.@)
1440 BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
1441 DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
1442 INT x, INT y, INT cx, INT cy, UINT flags)
1444 return PAINTING_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
1448 /***********************************************************************
1449 * SelectPalette (Not a Windows API)
1451 HPALETTE WINAPI SelectPalette( HDC hDC, HPALETTE hPal, BOOL bForceBackground )
1453 WORD wBkgPalette = 1;
1455 if (!bForceBackground && (hPal != GetStockObject(DEFAULT_PALETTE)))
1457 HWND hwnd = WindowFromDC( hDC );
1458 if (hwnd)
1460 HWND hForeground = GetForegroundWindow();
1461 /* set primary palette if it's related to current active */
1462 if (hForeground == hwnd || IsChild(hForeground,hwnd)) wBkgPalette = 0;
1465 return pfnGDISelectPalette( hDC, hPal, wBkgPalette);
1469 /***********************************************************************
1470 * UserRealizePalette (USER32.@)
1472 UINT WINAPI UserRealizePalette( HDC hDC )
1474 UINT realized = pfnGDIRealizePalette( hDC );
1476 /* do not send anything if no colors were changed */
1477 if (realized && IsDCCurrentPalette16( HDC_16(hDC) ))
1479 /* send palette change notification */
1480 HWND hWnd = WindowFromDC( hDC );
1481 if (hWnd) SendMessageTimeoutW( HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)hWnd, 0,
1482 SMTO_ABORTIFHUNG, 2000, NULL );
1484 return realized;