2 * Window painting functions
4 * Copyright 1993, 1994, 1995, 2001, 2004, 2005, 2008 Alexandre Julliard
5 * Copyright 1996, 1997, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
30 #define WIN32_NO_STATUS
35 #include "wine/server.h"
37 #include "user_private.h"
39 #include "wine/list.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(win);
47 struct list entry; /* entry in global DCE list */
52 LONG count; /* usage count; 0 or 1 for cache DCEs, always 1 for window DCEs,
53 always >= 1 for class DCEs */
56 static struct list dce_list = LIST_INIT(dce_list);
58 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
60 static const WCHAR displayW[] = { 'D','I','S','P','L','A','Y',0 };
63 /***********************************************************************
66 static void dump_rdw_flags(UINT flags)
69 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
70 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
71 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
72 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
73 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
74 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
75 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
76 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
77 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
78 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
79 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
80 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
87 RDW_NOINTERNALPAINT | \
96 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
102 /***********************************************************************
103 * update_visible_region
105 * Set the visible region and X11 drawable for the DC associated to
108 static void update_visible_region( struct dce *dce )
113 DWORD flags = dce->flags;
115 RECT win_rect, top_rect;
117 /* don't clip siblings if using parent clip region */
118 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
120 /* fetch the visible region from the server */
123 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
126 SERVER_START_REQ( get_visible_region )
128 req->window = dce->hwnd;
130 wine_server_set_reply( req, data->Buffer, size );
131 if (!(status = wine_server_call( req )))
133 size_t reply_size = wine_server_reply_size( reply );
134 data->rdh.dwSize = sizeof(data->rdh);
135 data->rdh.iType = RDH_RECTANGLES;
136 data->rdh.nCount = reply_size / sizeof(RECT);
137 data->rdh.nRgnSize = reply_size;
138 vis_rgn = ExtCreateRegion( NULL, size, data );
140 top_win = reply->top_win;
141 win_rect.left = reply->win_rect.left;
142 win_rect.top = reply->win_rect.top;
143 win_rect.right = reply->win_rect.right;
144 win_rect.bottom = reply->win_rect.bottom;
145 top_rect.left = reply->top_rect.left;
146 top_rect.top = reply->top_rect.top;
147 top_rect.right = reply->top_rect.right;
148 top_rect.bottom = reply->top_rect.bottom;
150 else size = reply->total_size;
153 HeapFree( GetProcessHeap(), 0, data );
154 } while (status == STATUS_BUFFER_OVERFLOW);
156 if (status || !vis_rgn) return;
158 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
160 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
161 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
163 /* map region to DC coordinates */
164 OffsetRgn( vis_rgn, -win_rect.left, -win_rect.top );
165 SelectVisRgn( dce->hdc, vis_rgn );
166 DeleteObject( vis_rgn );
170 /***********************************************************************
173 static void release_dce( struct dce *dce )
175 if (!dce->hwnd) return; /* already released */
177 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
179 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
182 dce->flags &= DCX_CACHE;
186 /***********************************************************************
189 static void delete_clip_rgn( struct dce *dce )
191 if (!dce->clip_rgn) return; /* nothing to do */
193 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
194 DeleteObject( dce->clip_rgn );
197 /* make it dirty so that the vis rgn gets recomputed next time */
198 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
202 /***********************************************************************
205 * Allocate a new DCE.
207 static struct dce *alloc_dce(void)
211 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
212 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
214 HeapFree( GetProcessHeap(), 0, dce );
224 /* store DCE handle in DC hook data field */
225 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
226 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
231 /***********************************************************************
234 static struct dce *get_window_dce( HWND hwnd )
237 WND *win = WIN_GetPtr( hwnd );
239 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
242 if (!dce && (dce = get_class_dce( win->class )))
247 WIN_ReleasePtr( win );
249 if (!dce) /* try to allocate one */
251 struct dce *dce_to_free = NULL;
252 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
254 if (class_style & CS_CLASSDC)
256 if (!(dce = alloc_dce())) return NULL;
258 win = WIN_GetPtr( hwnd );
259 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
261 if (win->dce) /* another thread beat us to it */
266 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
275 list_add_tail( &dce_list, &dce->entry );
277 WIN_ReleasePtr( win );
279 else dce_to_free = dce;
281 else if (class_style & CS_OWNDC)
283 if (!(dce = alloc_dce())) return NULL;
285 win = WIN_GetPtr( hwnd );
286 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
288 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
289 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
290 if (win->dce) /* another thread beat us to it */
299 list_add_tail( &dce_list, &dce->entry );
301 WIN_ReleasePtr( win );
303 else dce_to_free = dce;
308 SetDCHook( dce->hdc, NULL, 0 );
309 DeleteDC( dce->hdc );
310 HeapFree( GetProcessHeap(), 0, dce );
317 /***********************************************************************
320 * Free a class or window DCE.
322 void free_dce( struct dce *dce, HWND hwnd )
330 /* turn it into a cache entry */
332 dce->flags |= DCX_CACHE;
334 else if (dce->hwnd == hwnd)
340 /* now check for cache DCEs */
344 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
346 if (dce->hwnd != hwnd) continue;
347 if (!(dce->flags & DCX_CACHE)) continue;
349 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
359 /***********************************************************************
362 * Mark the associated DC as dirty to force a refresh of the visible region
364 static void make_dc_dirty( struct dce *dce )
368 /* Don't bother with visible regions of unused DCEs */
369 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
374 /* Set dirty bits in the hDC and DCE structs */
375 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
376 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
381 /***********************************************************************
384 * It is called from SetWindowPos() - we have to
385 * mark as dirty all busy DCEs for windows that have pWnd->parent as
386 * an ancestor and whose client rect intersects with specified update
387 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
388 * DCX_CLIPCHILDREN flag is set.
390 void invalidate_dce( HWND hwnd, const RECT *rect )
392 RECT window_rect, extra_rect;
394 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
396 if (!hwndScope) return;
398 GetWindowRect( hwnd, &window_rect );
402 MapWindowPoints( hwndScope, 0, (POINT *)&extra_rect, 2 );
405 TRACE("%p scope hwnd = %p %s (%s)\n",
406 hwnd, hwndScope, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(rect) );
408 /* walk all DCEs and fixup non-empty entries */
411 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
413 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
414 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
416 if (!dce->hwnd) continue;
417 if ((dce->hwnd == hwndScope) && !(dce->flags & DCX_CLIPCHILDREN))
418 continue; /* child window positions don't bother us */
420 /* if DCE window is a child of hwnd, it has to be invalidated */
421 if (dce->hwnd == hwnd || IsChild( hwnd, dce->hwnd ))
423 make_dc_dirty( dce );
425 else /* otherwise check if the window rectangle intersects this DCE window */
427 if (hwndScope == GetDesktopWindow() ||
428 hwndScope == dce->hwnd || IsChild( hwndScope, dce->hwnd ))
431 GetWindowRect( dce->hwnd, &dce_rect );
432 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
433 (rect && IntersectRect( &tmp, &dce_rect, &extra_rect )))
434 make_dc_dirty( dce );
441 /***********************************************************************
444 * Implementation of ReleaseDC.
446 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
451 TRACE("%p %p\n", hwnd, hdc );
454 dce = (struct dce *)GetDCHook( hdc, NULL );
455 if (dce && dce->count)
457 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
458 if (dce->flags & DCX_CACHE) dce->count = 0;
466 /***********************************************************************
469 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
471 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
474 struct dce *dce = (struct dce *)data;
476 TRACE("hDC = %p, %u\n", hDC, code);
479 assert( dce->hdc == hDC );
483 case DCHC_INVALIDVISRGN:
484 /* GDI code calls this when it detects that the
485 * DC is dirty (usually after SetHookFlags()). This
486 * means that we have to recompute the visible region.
488 if (dce->count) update_visible_region( dce );
489 else /* non-fatal but shouldn't happen */
490 WARN("DC is not in use!\n");
494 * Windows will not let you delete a DC that is busy
495 * (between GetDC and ReleaseDC)
500 WARN("Application trying to delete a busy DC %p\n", dce->hdc);
505 list_remove( &dce->entry );
506 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
507 HeapFree( GetProcessHeap(), 0, dce );
516 /***********************************************************************
519 * Return update region (in screen coordinates) for a window.
521 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
530 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
532 SetLastError( ERROR_OUTOFMEMORY );
536 SERVER_START_REQ( get_update_region )
539 req->from_child = child ? *child : 0;
541 wine_server_set_reply( req, data->Buffer, size );
542 if (!(status = wine_server_call( req )))
544 size_t reply_size = wine_server_reply_size( reply );
545 data->rdh.dwSize = sizeof(data->rdh);
546 data->rdh.iType = RDH_RECTANGLES;
547 data->rdh.nCount = reply_size / sizeof(RECT);
548 data->rdh.nRgnSize = reply_size;
549 hrgn = ExtCreateRegion( NULL, size, data );
550 if (child) *child = reply->child;
551 *flags = reply->flags;
553 else size = reply->total_size;
556 HeapFree( GetProcessHeap(), 0, data );
557 } while (status == STATUS_BUFFER_OVERFLOW);
559 if (status) SetLastError( RtlNtStatusToDosError(status) );
564 /***********************************************************************
567 * Get only the update flags, not the update region.
569 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
573 SERVER_START_REQ( get_update_region )
576 req->from_child = child ? *child : 0;
577 req->flags = *flags | UPDATE_NOREGION;
578 if ((ret = !wine_server_call_err( req )))
580 if (child) *child = reply->child;
581 *flags = reply->flags;
589 /***********************************************************************
590 * redraw_window_rects
592 * Redraw part of a window.
594 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
598 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
599 return TRUE; /* nothing to do */
601 SERVER_START_REQ( redraw_window )
605 wine_server_add_data( req, rects, count * sizeof(RECT) );
606 ret = !wine_server_call_err( req );
613 /***********************************************************************
616 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
617 * Helper for erase_now and BeginPaint.
619 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
621 HRGN whole_rgn = get_update_region( hwnd, flags, child );
624 if (child) hwnd = *child;
631 /* check if update rgn overlaps with nonclient area */
632 type = GetRgnBox( whole_rgn, &update );
633 GetClientRect( hwnd, &client );
634 MapWindowPoints( hwnd, 0, (POINT *)&client, 2 );
636 if ((*flags & UPDATE_NONCLIENT) ||
637 update.left < client.left || update.top < client.top ||
638 update.right > client.right || update.bottom > client.bottom)
640 client_rgn = CreateRectRgnIndirect( &client );
641 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
643 /* check if update rgn contains complete nonclient area */
644 if (type == SIMPLEREGION)
647 GetWindowRect( hwnd, &window );
648 if (EqualRect( &window, &update ))
650 DeleteObject( whole_rgn );
657 client_rgn = whole_rgn;
661 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
663 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
664 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
671 /***********************************************************************
674 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
675 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
676 * Helper for erase_now and BeginPaint.
678 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
679 RECT *clip_rect, HDC *hdc_ret )
681 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
685 if (!clip_rect) clip_rect = &dummy;
686 if (hdc_ret || (flags & UPDATE_ERASE))
688 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
689 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
691 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
693 INT type = GetClipBox( hdc, clip_rect );
695 if (flags & UPDATE_ERASE)
697 /* don't erase if the clip box is empty */
698 if (type != NULLREGION)
699 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
701 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
704 if (hdc_ret) *hdc_ret = hdc;
706 if (!hdc) DeleteObject( client_rgn );
711 /***********************************************************************
714 * Implementation of RDW_ERASENOW behavior.
716 void erase_now( HWND hwnd, UINT rdw_flags )
720 BOOL need_erase = FALSE;
722 /* loop while we find a child to repaint */
725 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
727 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
728 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
729 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
731 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
732 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
734 if (!flags) break; /* nothing more to do */
735 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
740 /***********************************************************************
743 * Implementation of RDW_UPDATENOW behavior.
745 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
746 * SendMessage() calls. This is a comment inside DefWindowProc() source
749 * This message avoids lots of inter-app message traffic
750 * by switching to the other task and continuing the
754 * LOWORD(lParam) = hrgnClip
755 * HIWORD(lParam) = hwndSkip (not used; always NULL)
758 static void update_now( HWND hwnd, UINT rdw_flags )
762 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
763 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
765 /* loop while we find a child to repaint */
768 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
770 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
771 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
773 if (!get_update_flags( hwnd, &child, &flags )) break;
774 if (!flags) break; /* nothing more to do */
776 SendMessageW( child, WM_PAINT, 0, 0 );
777 if (rdw_flags & RDW_NOCHILDREN) break;
782 /*************************************************************************
785 * Helper for ScrollWindowEx:
786 * If the return value is 0, no special caret handling is necessary.
787 * Otherwise the return value is the handle of the window that owns the
788 * caret. Its caret needs to be hidden during the scroll operation and
789 * moved to new_caret_pos if move_caret is TRUE.
791 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
792 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
795 RECT rect, mapped_rcCaret;
796 BOOL hide_caret = FALSE;
798 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
799 if (!info.hwndCaret) return 0;
801 if (info.hwndCaret == hWnd)
803 /* Move the caret if it's (partially) in the source rectangle */
804 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
808 new_caret_pos->x = info.rcCaret.left + dx;
809 new_caret_pos->y = info.rcCaret.top + dy;
815 /* Hide the caret if it's in the destination rectangle */
817 OffsetRect(&rect, dx, dy);
818 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
823 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
827 /* Hide the caret if it's in the source or in the destination
829 mapped_rcCaret = info.rcCaret;
830 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
832 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
839 OffsetRect(&rect, dx, dy);
840 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
849 HideCaret(info.hwndCaret);
850 return info.hwndCaret;
857 /***********************************************************************
858 * BeginPaint (USER32.@)
860 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
863 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
869 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
871 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
873 TRACE("hdc = %p box = (%s), fErase = %d\n",
874 lps->hdc, wine_dbgstr_rect(&lps->rcPaint), lps->fErase);
880 /***********************************************************************
881 * EndPaint (USER32.@)
883 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
885 if (!lps) return FALSE;
886 release_dc( hwnd, lps->hdc, TRUE );
892 /***********************************************************************
895 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
897 static const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
899 BOOL bUpdateVisRgn = TRUE;
901 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
903 if (!hwnd) hwnd = GetDesktopWindow();
904 else hwnd = WIN_GetFullHandle( hwnd );
906 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
910 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
912 if (flags & DCX_USESTYLE)
914 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
916 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
918 if (!(flags & DCX_WINDOW))
920 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
922 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
923 flags |= DCX_CLIPCHILDREN;
927 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
929 parent = GetAncestor( hwnd, GA_PARENT );
930 if (!parent || (parent == GetDesktopWindow()))
931 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
933 /* it seems parent clip is ignored when clipping siblings or children */
934 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
936 if( flags & DCX_PARENTCLIP )
938 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
939 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
941 flags &= ~DCX_CLIPCHILDREN;
942 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
946 /* find a suitable DCE */
948 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
950 struct dce *dceEmpty = NULL, *dceUnused = NULL;
952 /* Strategy: First, we attempt to find a non-empty but unused DCE with
953 * compatible flags. Next, we look for an empty entry. If the cache is
954 * full we have to purge one of the unused entries.
957 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
959 if ((dce->flags & DCX_CACHE) && !dce->count)
963 if (!dce->hwnd) dceEmpty = dce;
964 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
966 TRACE("\tfound valid %p dce [%p], flags %08x\n",
967 dce, hwnd, dce->flags );
968 bUpdateVisRgn = FALSE;
974 if (&dce->entry == &dce_list) /* nothing found */
975 dce = dceEmpty ? dceEmpty : dceUnused;
977 if (dce) dce->count = 1;
981 /* if there's no dce empty or unused, allocate a new one */
984 if (!(dce = alloc_dce())) return 0;
985 dce->flags = DCX_CACHE;
987 list_add_head( &dce_list, &dce->entry );
993 flags |= DCX_NORESETATTRS;
994 if (dce->hwnd == hwnd)
996 TRACE("\tskipping hVisRgn update\n");
997 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1001 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1002 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1007 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1009 /* if the extra clip region has changed, get rid of the old one */
1010 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1011 delete_clip_rgn( dce );
1012 dce->clip_rgn = hrgnClip;
1013 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1014 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1015 bUpdateVisRgn = TRUE;
1019 dce->flags = (dce->flags & ~clip_flags) | (flags & clip_flags);
1021 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1023 if (bUpdateVisRgn) update_visible_region( dce );
1025 if (!(flags & DCX_NORESETATTRS))
1027 RestoreDC( dce->hdc, 1 ); /* initial save level is always 1 */
1028 SaveDC( dce->hdc ); /* save the state again for next time */
1031 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1036 /***********************************************************************
1039 * Get a device context.
1042 * Success: Handle to the device context
1045 HDC WINAPI GetDC( HWND hwnd )
1047 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1048 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1052 /***********************************************************************
1053 * GetWindowDC (USER32.@)
1055 HDC WINAPI GetWindowDC( HWND hwnd )
1057 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1061 /***********************************************************************
1062 * ReleaseDC (USER32.@)
1064 * Release a device context.
1067 * Success: Non-zero. Resources used by hdc are released.
1070 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1072 return release_dc( hwnd, hdc, FALSE );
1076 /**********************************************************************
1077 * WindowFromDC (USER32.@)
1079 HWND WINAPI WindowFromDC( HDC hdc )
1085 dce = (struct dce *)GetDCHook( hdc, NULL );
1086 if (dce) hwnd = dce->hwnd;
1092 /***********************************************************************
1093 * LockWindowUpdate (USER32.@)
1095 * Enables or disables painting in the chosen window.
1098 * hwnd [I] handle to a window.
1101 * If successful, returns nonzero value. Otherwise,
1105 * You can lock only one window at a time.
1107 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1109 static HWND lockedWnd;
1111 /* This function is fully implemented by the following patch:
1113 * http://www.winehq.org/hypermail/wine-patches/2004/01/0142.html
1115 * but in order to work properly, it needs the ability to invalidate
1116 * DCEs in other processes when the lock window is changed, which
1117 * isn't possible yet.
1121 FIXME("(%p), partial stub!\n",hwnd);
1128 /* Unlock lockedWnd */
1129 /* FIXME: Do something */
1133 /* Attempted to lock a second window */
1134 /* Return FALSE and do nothing */
1145 /***********************************************************************
1146 * RedrawWindow (USER32.@)
1148 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1150 static const RECT empty;
1153 if (!hwnd) hwnd = GetDesktopWindow();
1160 GetRgnBox( hrgn, &r );
1161 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1164 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1166 TRACE( "%p whole window ", hwnd );
1168 dump_rdw_flags(flags);
1171 /* process pending expose events before painting */
1172 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1176 if (IsRectEmpty( rect )) rect = ∅
1177 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1181 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1183 else /* need to build a list of the region rectangles */
1186 RGNDATA *data = NULL;
1188 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1189 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1190 GetRegionData( hrgn, size, data );
1191 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1192 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1194 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1195 HeapFree( GetProcessHeap(), 0, data );
1198 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1199 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1205 /***********************************************************************
1206 * UpdateWindow (USER32.@)
1208 BOOL WINAPI UpdateWindow( HWND hwnd )
1210 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1214 /***********************************************************************
1215 * InvalidateRgn (USER32.@)
1217 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1221 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1225 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1229 /***********************************************************************
1230 * InvalidateRect (USER32.@)
1232 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1233 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1235 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1237 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1241 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1245 return RedrawWindow( hwnd, rect, 0, flags );
1249 /***********************************************************************
1250 * ValidateRgn (USER32.@)
1252 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1256 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1260 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1264 /***********************************************************************
1265 * ValidateRect (USER32.@)
1267 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1268 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1270 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1272 UINT flags = RDW_VALIDATE;
1276 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1280 return RedrawWindow( hwnd, rect, 0, flags );
1284 /***********************************************************************
1285 * GetUpdateRgn (USER32.@)
1287 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1290 UINT flags = UPDATE_NOCHILDREN;
1293 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1295 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1299 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1300 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1302 flags = UPDATE_DELAYED_ERASE;
1303 get_update_flags( hwnd, NULL, &flags );
1305 /* map region to client coordinates */
1306 offset.x = offset.y = 0;
1307 ScreenToClient( hwnd, &offset );
1308 OffsetRgn( hrgn, offset.x, offset.y );
1314 /***********************************************************************
1315 * GetUpdateRect (USER32.@)
1317 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1319 UINT flags = UPDATE_NOCHILDREN;
1323 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1325 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1329 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1331 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1332 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1333 DPtoLP( hdc, (LPPOINT)rect, 2 );
1334 ReleaseDC( hwnd, hdc );
1337 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1339 /* check if we still have an update region */
1340 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1341 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1342 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1346 /***********************************************************************
1347 * ExcludeUpdateRgn (USER32.@)
1349 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1351 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1352 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1358 GetDCOrgEx( hdc, &pt );
1359 MapWindowPoints( 0, hwnd, &pt, 1 );
1360 OffsetRgn( update_rgn, -pt.x, -pt.y );
1361 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1362 DeleteObject( update_rgn );
1368 /*************************************************************************
1369 * ScrollWindowEx (USER32.@)
1371 * Note: contrary to what the doc says, pixels that are scrolled from the
1372 * outside of clipRect to the inside are NOT painted.
1375 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1376 const RECT *rect, const RECT *clipRect,
1377 HRGN hrgnUpdate, LPRECT rcUpdate,
1380 INT retVal = NULLREGION;
1381 BOOL bOwnRgn = TRUE;
1382 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1385 HRGN hrgnWinupd = 0;
1388 HWND hwndCaret = NULL;
1389 BOOL moveCaret = FALSE;
1392 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1393 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1394 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1395 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1396 FIXME("some flags (%04x) are unhandled\n", flags);
1398 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1399 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1401 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1402 hwnd = WIN_GetFullHandle( hwnd );
1404 GetClientRect(hwnd, &rc);
1405 if (rect) IntersectRect(&rc, &rc, rect);
1407 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1410 if( hrgnUpdate ) bOwnRgn = FALSE;
1411 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1413 newCaretPos.x = newCaretPos.y = 0;
1415 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1416 DWORD dcxflags = DCX_CACHE;
1417 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1419 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1421 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1422 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1423 dcxflags |= DCX_PARENTCLIP;
1424 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1425 dcxflags |= DCX_CLIPCHILDREN;
1426 hDC = GetDCEx( hwnd, 0, dcxflags);
1429 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1431 ReleaseDC( hwnd, hDC );
1434 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1437 /* If the windows has an update region, this must be
1438 * scrolled as well. Keep a copy in hrgnWinupd
1439 * to be added to hrngUpdate at the end. */
1440 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1441 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1442 if (retVal != NULLREGION)
1444 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1446 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1447 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1449 OffsetRgn( hrgnTemp, dx, dy );
1450 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1452 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1453 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1455 /* Catch the case where the scrolling amount exceeds the size of the
1456 * original window. This generated a second update area that is the
1457 * location where the original scrolled content would end up.
1458 * This second region is not returned by the ScrollDC and sets
1459 * ScrollWindowEx apart from just a ScrollDC.
1461 * This has been verified with testing on windows.
1463 if (abs(dx) > abs(rc.right - rc.left) ||
1464 abs(dy) > abs(rc.bottom - rc.top))
1466 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1467 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1468 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1471 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1473 DeleteObject( hrgnClip );
1475 DeleteObject( hrgnTemp );
1477 /* nothing was scrolled */
1479 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1480 SetRectEmpty( rcUpdate);
1483 if( flags & SW_SCROLLCHILDREN )
1485 HWND *list = WIN_ListChildren( hwnd );
1490 for (i = 0; list[i]; i++)
1492 GetWindowRect( list[i], &r );
1493 MapWindowPoints( 0, hwnd, (POINT *)&r, 2 );
1494 if (!rect || IntersectRect(&dummy, &r, rect))
1495 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1496 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1497 SWP_NOREDRAW | SWP_DEFERERASE );
1499 HeapFree( GetProcessHeap(), 0, list );
1503 if( flags & (SW_INVALIDATE | SW_ERASE) )
1504 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1505 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1508 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1509 DeleteObject( hrgnWinupd);
1513 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1514 ShowCaret(hwndCaret);
1517 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1523 /*************************************************************************
1524 * ScrollWindow (USER32.@)
1527 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1528 const RECT *rect, const RECT *clipRect )
1530 return (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
1531 (rect ? 0 : SW_SCROLLCHILDREN) |
1532 SW_INVALIDATE | SW_ERASE ));
1536 /*************************************************************************
1537 * ScrollDC (USER32.@)
1539 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1540 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1541 * logical coordinates.
1543 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
1544 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
1547 return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );