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/gdi_driver.h"
40 #include "wine/list.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(win);
48 struct list entry; /* entry in global DCE list */
53 LONG count; /* usage count; 0 or 1 for cache DCEs, always 1 for window DCEs,
54 always >= 1 for class DCEs */
57 static struct list dce_list = LIST_INIT(dce_list);
59 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
61 static const WCHAR displayW[] = { 'D','I','S','P','L','A','Y',0 };
64 /***********************************************************************
67 static void dump_rdw_flags(UINT flags)
70 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
71 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
72 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
73 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
74 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
75 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
76 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
77 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
78 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
79 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
80 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
81 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
88 RDW_NOINTERNALPAINT | \
97 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
103 /***********************************************************************
104 * update_visible_region
106 * Set the visible region and X11 drawable for the DC associated to
109 static void update_visible_region( struct dce *dce )
114 DWORD flags = dce->flags;
116 RECT win_rect, top_rect;
118 /* don't clip siblings if using parent clip region */
119 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
121 /* fetch the visible region from the server */
124 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
127 SERVER_START_REQ( get_visible_region )
129 req->window = wine_server_user_handle( dce->hwnd );
131 wine_server_set_reply( req, data->Buffer, size );
132 if (!(status = wine_server_call( req )))
134 size_t reply_size = wine_server_reply_size( reply );
135 data->rdh.dwSize = sizeof(data->rdh);
136 data->rdh.iType = RDH_RECTANGLES;
137 data->rdh.nCount = reply_size / sizeof(RECT);
138 data->rdh.nRgnSize = reply_size;
139 vis_rgn = ExtCreateRegion( NULL, size, data );
141 top_win = wine_server_ptr_handle( reply->top_win );
142 win_rect.left = reply->win_rect.left;
143 win_rect.top = reply->win_rect.top;
144 win_rect.right = reply->win_rect.right;
145 win_rect.bottom = reply->win_rect.bottom;
146 top_rect.left = reply->top_rect.left;
147 top_rect.top = reply->top_rect.top;
148 top_rect.right = reply->top_rect.right;
149 top_rect.bottom = reply->top_rect.bottom;
151 else size = reply->total_size;
154 HeapFree( GetProcessHeap(), 0, data );
155 } while (status == STATUS_BUFFER_OVERFLOW);
157 if (status || !vis_rgn) return;
159 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
161 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
162 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
164 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect );
168 /***********************************************************************
171 static void reset_dce_attrs( struct dce *dce )
173 RestoreDC( dce->hdc, 1 ); /* initial save level is always 1 */
174 SaveDC( dce->hdc ); /* save the state again for next time */
178 /***********************************************************************
181 static void release_dce( struct dce *dce )
183 if (!dce->hwnd) return; /* already released */
185 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
187 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
190 dce->flags &= DCX_CACHE;
194 /***********************************************************************
197 static void delete_clip_rgn( struct dce *dce )
199 if (!dce->clip_rgn) return; /* nothing to do */
201 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
202 DeleteObject( dce->clip_rgn );
205 /* make it dirty so that the vis rgn gets recomputed next time */
206 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
210 /***********************************************************************
213 * Allocate a new DCE.
215 static struct dce *alloc_dce(void)
219 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
220 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
222 HeapFree( GetProcessHeap(), 0, dce );
232 /* store DCE handle in DC hook data field */
233 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
234 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
239 /***********************************************************************
242 static struct dce *get_window_dce( HWND hwnd )
245 WND *win = WIN_GetPtr( hwnd );
247 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
250 if (!dce && (dce = get_class_dce( win->class )))
255 WIN_ReleasePtr( win );
257 if (!dce) /* try to allocate one */
259 struct dce *dce_to_free = NULL;
260 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
262 if (class_style & CS_CLASSDC)
264 if (!(dce = alloc_dce())) return NULL;
266 win = WIN_GetPtr( hwnd );
267 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
269 if (win->dce) /* another thread beat us to it */
274 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
283 list_add_tail( &dce_list, &dce->entry );
285 WIN_ReleasePtr( win );
287 else dce_to_free = dce;
289 else if (class_style & CS_OWNDC)
291 if (!(dce = alloc_dce())) return NULL;
293 win = WIN_GetPtr( hwnd );
294 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
296 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
297 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
298 if (win->dce) /* another thread beat us to it */
308 list_add_tail( &dce_list, &dce->entry );
310 WIN_ReleasePtr( win );
312 else dce_to_free = dce;
317 SetDCHook( dce_to_free->hdc, NULL, 0 );
318 DeleteDC( dce_to_free->hdc );
319 HeapFree( GetProcessHeap(), 0, dce_to_free );
320 if (dce_to_free == dce)
328 /***********************************************************************
331 * Free a class or window DCE.
333 void free_dce( struct dce *dce, HWND hwnd )
341 /* turn it into a cache entry */
342 reset_dce_attrs( dce );
344 dce->flags |= DCX_CACHE;
346 else if (dce->hwnd == hwnd)
352 /* now check for cache DCEs */
356 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
358 if (dce->hwnd != hwnd) continue;
359 if (!(dce->flags & DCX_CACHE)) continue;
361 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
371 /***********************************************************************
374 * Mark the associated DC as dirty to force a refresh of the visible region
376 static void make_dc_dirty( struct dce *dce )
380 /* Don't bother with visible regions of unused DCEs */
381 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
386 /* Set dirty bits in the hDC and DCE structs */
387 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
388 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
393 /***********************************************************************
396 * It is called from SetWindowPos() - we have to
397 * mark as dirty all busy DCEs for windows that have pWnd->parent as
398 * an ancestor and whose client rect intersects with specified update
399 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
400 * DCX_CLIPCHILDREN flag is set.
402 void invalidate_dce( HWND hwnd, const RECT *extra_rect )
406 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
408 if (!hwndScope) return;
410 GetWindowRect( hwnd, &window_rect );
412 TRACE("%p scope hwnd = %p %s (%s)\n",
413 hwnd, hwndScope, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
415 /* walk all DCEs and fixup non-empty entries */
418 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
420 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
421 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
423 if (!dce->hwnd) continue;
424 if ((dce->hwnd == hwndScope) && !(dce->flags & DCX_CLIPCHILDREN))
425 continue; /* child window positions don't bother us */
427 /* if DCE window is a child of hwnd, it has to be invalidated */
428 if (dce->hwnd == hwnd || IsChild( hwnd, dce->hwnd ))
430 make_dc_dirty( dce );
434 /* otherwise check if the window rectangle intersects this DCE window */
435 if (hwndScope == dce->hwnd || IsChild( hwndScope, dce->hwnd ))
438 GetWindowRect( dce->hwnd, &dce_rect );
439 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
440 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
441 make_dc_dirty( dce );
447 /***********************************************************************
450 * Implementation of ReleaseDC.
452 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
457 TRACE("%p %p\n", hwnd, hdc );
460 dce = (struct dce *)GetDCHook( hdc, NULL );
461 if (dce && dce->count)
463 if (!(dce->flags & DCX_NORESETATTRS)) reset_dce_attrs( dce );
464 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
465 if (dce->flags & DCX_CACHE) dce->count = 0;
473 /***********************************************************************
476 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
478 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
481 struct dce *dce = (struct dce *)data;
483 TRACE("hDC = %p, %u\n", hDC, code);
486 assert( dce->hdc == hDC );
490 case DCHC_INVALIDVISRGN:
491 /* GDI code calls this when it detects that the
492 * DC is dirty (usually after SetHookFlags()). This
493 * means that we have to recompute the visible region.
495 if (dce->count) update_visible_region( dce );
496 else /* non-fatal but shouldn't happen */
497 WARN("DC is not in use!\n");
501 * Windows will not let you delete a DC that is busy
502 * (between GetDC and ReleaseDC)
507 WARN("Application trying to delete a busy DC %p\n", dce->hdc);
512 list_remove( &dce->entry );
513 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
514 HeapFree( GetProcessHeap(), 0, dce );
523 /***********************************************************************
526 * Return update region (in screen coordinates) for a window.
528 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
537 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
539 SetLastError( ERROR_OUTOFMEMORY );
543 SERVER_START_REQ( get_update_region )
545 req->window = wine_server_user_handle( hwnd );
546 req->from_child = wine_server_user_handle( child ? *child : 0 );
548 wine_server_set_reply( req, data->Buffer, size );
549 if (!(status = wine_server_call( req )))
551 size_t reply_size = wine_server_reply_size( reply );
552 data->rdh.dwSize = sizeof(data->rdh);
553 data->rdh.iType = RDH_RECTANGLES;
554 data->rdh.nCount = reply_size / sizeof(RECT);
555 data->rdh.nRgnSize = reply_size;
556 hrgn = ExtCreateRegion( NULL, size, data );
557 if (child) *child = wine_server_ptr_handle( reply->child );
558 *flags = reply->flags;
560 else size = reply->total_size;
563 HeapFree( GetProcessHeap(), 0, data );
564 } while (status == STATUS_BUFFER_OVERFLOW);
566 if (status) SetLastError( RtlNtStatusToDosError(status) );
571 /***********************************************************************
574 * Get only the update flags, not the update region.
576 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
580 SERVER_START_REQ( get_update_region )
582 req->window = wine_server_user_handle( hwnd );
583 req->from_child = wine_server_user_handle( child ? *child : 0 );
584 req->flags = *flags | UPDATE_NOREGION;
585 if ((ret = !wine_server_call_err( req )))
587 if (child) *child = wine_server_ptr_handle( reply->child );
588 *flags = reply->flags;
596 /***********************************************************************
597 * redraw_window_rects
599 * Redraw part of a window.
601 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
605 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
606 return TRUE; /* nothing to do */
608 SERVER_START_REQ( redraw_window )
610 req->window = wine_server_user_handle( hwnd );
612 wine_server_add_data( req, rects, count * sizeof(RECT) );
613 ret = !wine_server_call_err( req );
620 /***********************************************************************
623 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
624 * Helper for erase_now and BeginPaint.
626 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
628 HRGN whole_rgn = get_update_region( hwnd, flags, child );
631 if (child) hwnd = *child;
633 if (hwnd == GetDesktopWindow()) return whole_rgn;
640 /* check if update rgn overlaps with nonclient area */
641 type = GetRgnBox( whole_rgn, &update );
642 WIN_GetRectangles( hwnd, COORDS_SCREEN, 0, &client );
644 if ((*flags & UPDATE_NONCLIENT) ||
645 update.left < client.left || update.top < client.top ||
646 update.right > client.right || update.bottom > client.bottom)
648 client_rgn = CreateRectRgnIndirect( &client );
649 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
651 /* check if update rgn contains complete nonclient area */
652 if (type == SIMPLEREGION)
655 GetWindowRect( hwnd, &window );
656 if (EqualRect( &window, &update ))
658 DeleteObject( whole_rgn );
665 client_rgn = whole_rgn;
669 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
671 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
672 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
679 /***********************************************************************
682 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
683 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
684 * Helper for erase_now and BeginPaint.
686 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
687 RECT *clip_rect, HDC *hdc_ret )
689 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
693 if (!clip_rect) clip_rect = &dummy;
694 if (hdc_ret || (flags & UPDATE_ERASE))
696 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
697 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
699 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
701 INT type = GetClipBox( hdc, clip_rect );
703 if (flags & UPDATE_ERASE)
705 /* don't erase if the clip box is empty */
706 if (type != NULLREGION)
707 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
709 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
712 if (hdc_ret) *hdc_ret = hdc;
714 if (!hdc) DeleteObject( client_rgn );
719 /***********************************************************************
722 * Implementation of RDW_ERASENOW behavior.
724 void erase_now( HWND hwnd, UINT rdw_flags )
728 BOOL need_erase = FALSE;
730 /* loop while we find a child to repaint */
733 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
735 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
736 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
737 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
739 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
740 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
742 if (!flags) break; /* nothing more to do */
743 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
748 /***********************************************************************
751 * Implementation of RDW_UPDATENOW behavior.
753 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
754 * SendMessage() calls. This is a comment inside DefWindowProc() source
757 * This message avoids lots of inter-app message traffic
758 * by switching to the other task and continuing the
762 * LOWORD(lParam) = hrgnClip
763 * HIWORD(lParam) = hwndSkip (not used; always NULL)
766 static void update_now( HWND hwnd, UINT rdw_flags )
770 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
771 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
773 /* loop while we find a child to repaint */
776 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
778 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
779 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
781 if (!get_update_flags( hwnd, &child, &flags )) break;
782 if (!flags) break; /* nothing more to do */
784 SendMessageW( child, WM_PAINT, 0, 0 );
785 if (rdw_flags & RDW_NOCHILDREN) break;
790 /*************************************************************************
793 * Helper for ScrollWindowEx:
794 * If the return value is 0, no special caret handling is necessary.
795 * Otherwise the return value is the handle of the window that owns the
796 * caret. Its caret needs to be hidden during the scroll operation and
797 * moved to new_caret_pos if move_caret is TRUE.
799 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
800 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
803 RECT rect, mapped_rcCaret;
804 BOOL hide_caret = FALSE;
806 info.cbSize = sizeof(info);
807 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
808 if (!info.hwndCaret) return 0;
810 if (info.hwndCaret == hWnd)
812 /* Move the caret if it's (partially) in the source rectangle */
813 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
817 new_caret_pos->x = info.rcCaret.left + dx;
818 new_caret_pos->y = info.rcCaret.top + dy;
824 /* Hide the caret if it's in the destination rectangle */
826 OffsetRect(&rect, dx, dy);
827 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
832 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
836 /* Hide the caret if it's in the source or in the destination
838 mapped_rcCaret = info.rcCaret;
839 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
841 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
848 OffsetRect(&rect, dx, dy);
849 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
858 HideCaret(info.hwndCaret);
859 return info.hwndCaret;
866 /***********************************************************************
867 * BeginPaint (USER32.@)
869 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
872 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
878 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
880 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
882 TRACE("hdc = %p box = (%s), fErase = %d\n",
883 lps->hdc, wine_dbgstr_rect(&lps->rcPaint), lps->fErase);
889 /***********************************************************************
890 * EndPaint (USER32.@)
892 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
894 if (!lps) return FALSE;
895 release_dc( hwnd, lps->hdc, TRUE );
901 /***********************************************************************
904 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
906 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
907 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
909 BOOL bUpdateVisRgn = TRUE;
911 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
913 if (!hwnd) hwnd = GetDesktopWindow();
914 else hwnd = WIN_GetFullHandle( hwnd );
916 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
918 if (!IsWindow(hwnd)) return 0;
922 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
924 if (flags & DCX_USESTYLE)
926 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
928 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
930 if (!(flags & DCX_WINDOW))
932 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
934 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
935 flags |= DCX_CLIPCHILDREN;
939 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
941 parent = GetAncestor( hwnd, GA_PARENT );
942 if (!parent || (parent == GetDesktopWindow()))
943 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
945 /* it seems parent clip is ignored when clipping siblings or children */
946 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
948 if( flags & DCX_PARENTCLIP )
950 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
951 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
953 flags &= ~DCX_CLIPCHILDREN;
954 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
958 /* find a suitable DCE */
960 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
962 struct dce *dceEmpty = NULL, *dceUnused = NULL;
964 /* Strategy: First, we attempt to find a non-empty but unused DCE with
965 * compatible flags. Next, we look for an empty entry. If the cache is
966 * full we have to purge one of the unused entries.
969 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
971 if ((dce->flags & DCX_CACHE) && !dce->count)
975 if (!dce->hwnd) dceEmpty = dce;
976 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
978 TRACE("\tfound valid %p dce [%p], flags %08x\n",
979 dce, hwnd, dce->flags );
980 bUpdateVisRgn = FALSE;
986 if (&dce->entry == &dce_list) /* nothing found */
987 dce = dceEmpty ? dceEmpty : dceUnused;
989 if (dce) dce->count = 1;
993 /* if there's no dce empty or unused, allocate a new one */
996 if (!(dce = alloc_dce())) return 0;
997 dce->flags = DCX_CACHE;
999 list_add_head( &dce_list, &dce->entry );
1005 flags |= DCX_NORESETATTRS;
1006 if (dce->hwnd == hwnd)
1008 TRACE("\tskipping hVisRgn update\n");
1009 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1013 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1014 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1019 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1021 /* if the extra clip region has changed, get rid of the old one */
1022 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1023 delete_clip_rgn( dce );
1024 dce->clip_rgn = hrgnClip;
1025 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1026 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1027 bUpdateVisRgn = TRUE;
1030 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1033 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1035 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1037 if (bUpdateVisRgn) update_visible_region( dce );
1039 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1044 /***********************************************************************
1047 * Get a device context.
1050 * Success: Handle to the device context
1053 HDC WINAPI GetDC( HWND hwnd )
1055 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1056 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1060 /***********************************************************************
1061 * GetWindowDC (USER32.@)
1063 HDC WINAPI GetWindowDC( HWND hwnd )
1065 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1069 /***********************************************************************
1070 * ReleaseDC (USER32.@)
1072 * Release a device context.
1075 * Success: Non-zero. Resources used by hdc are released.
1078 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1080 return release_dc( hwnd, hdc, FALSE );
1084 /**********************************************************************
1085 * WindowFromDC (USER32.@)
1087 HWND WINAPI WindowFromDC( HDC hdc )
1093 dce = (struct dce *)GetDCHook( hdc, NULL );
1094 if (dce) hwnd = dce->hwnd;
1100 /***********************************************************************
1101 * LockWindowUpdate (USER32.@)
1103 * Enables or disables painting in the chosen window.
1106 * hwnd [I] handle to a window.
1109 * If successful, returns nonzero value. Otherwise,
1113 * You can lock only one window at a time.
1115 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1117 static HWND lockedWnd;
1119 FIXME("(%p), partial stub!\n",hwnd);
1126 /* Unlock lockedWnd */
1127 /* FIXME: Do something */
1131 /* Attempted to lock a second window */
1132 /* Return FALSE and do nothing */
1143 /***********************************************************************
1144 * RedrawWindow (USER32.@)
1146 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1148 static const RECT empty;
1151 if (!hwnd) hwnd = GetDesktopWindow();
1158 GetRgnBox( hrgn, &r );
1159 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1162 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1164 TRACE( "%p whole window ", hwnd );
1166 dump_rdw_flags(flags);
1169 /* process pending expose events before painting */
1170 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1174 if (IsRectEmpty( rect )) rect = ∅
1175 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1179 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1181 else /* need to build a list of the region rectangles */
1184 RGNDATA *data = NULL;
1186 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1187 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1188 GetRegionData( hrgn, size, data );
1189 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1190 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1192 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1193 HeapFree( GetProcessHeap(), 0, data );
1196 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1197 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1203 /***********************************************************************
1204 * UpdateWindow (USER32.@)
1206 BOOL WINAPI UpdateWindow( HWND hwnd )
1210 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1214 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1218 /***********************************************************************
1219 * InvalidateRgn (USER32.@)
1221 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1225 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1229 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1233 /***********************************************************************
1234 * InvalidateRect (USER32.@)
1236 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1237 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1239 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1241 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1245 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1249 return RedrawWindow( hwnd, rect, 0, flags );
1253 /***********************************************************************
1254 * ValidateRgn (USER32.@)
1256 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1260 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1264 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1268 /***********************************************************************
1269 * ValidateRect (USER32.@)
1271 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1272 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1274 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1276 UINT flags = RDW_VALIDATE;
1280 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1284 return RedrawWindow( hwnd, rect, 0, flags );
1288 /***********************************************************************
1289 * GetUpdateRgn (USER32.@)
1291 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1294 UINT flags = UPDATE_NOCHILDREN;
1297 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1299 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1301 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1302 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1304 flags = UPDATE_DELAYED_ERASE;
1305 get_update_flags( hwnd, NULL, &flags );
1307 /* map region to client coordinates */
1308 map_window_region( 0, hwnd, hrgn );
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 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1333 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1334 DPtoLP( hdc, (LPPOINT)rect, 2 );
1335 SetLayout( hdc, layout );
1336 ReleaseDC( hwnd, hdc );
1339 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1341 /* check if we still have an update region */
1342 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1343 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1344 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1348 /***********************************************************************
1349 * ExcludeUpdateRgn (USER32.@)
1351 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1353 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1354 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1360 GetDCOrgEx( hdc, &pt );
1361 MapWindowPoints( 0, hwnd, &pt, 1 );
1362 OffsetRgn( update_rgn, -pt.x, -pt.y );
1363 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1364 DeleteObject( update_rgn );
1370 /*************************************************************************
1371 * ScrollWindowEx (USER32.@)
1373 * Note: contrary to what the doc says, pixels that are scrolled from the
1374 * outside of clipRect to the inside are NOT painted.
1377 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1378 const RECT *rect, const RECT *clipRect,
1379 HRGN hrgnUpdate, LPRECT rcUpdate,
1382 INT retVal = NULLREGION;
1383 BOOL bOwnRgn = TRUE;
1384 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1387 HRGN hrgnWinupd = 0;
1390 HWND hwndCaret = NULL;
1391 BOOL moveCaret = FALSE;
1394 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1395 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1396 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1397 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1398 FIXME("some flags (%04x) are unhandled\n", flags);
1400 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1401 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1403 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1404 hwnd = WIN_GetFullHandle( hwnd );
1406 GetClientRect(hwnd, &rc);
1408 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1411 if (rect) IntersectRect(&rc, &rc, rect);
1413 if( hrgnUpdate ) bOwnRgn = FALSE;
1414 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1416 newCaretPos.x = newCaretPos.y = 0;
1418 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1419 DWORD dcxflags = DCX_CACHE;
1420 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1422 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1424 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1425 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1426 dcxflags |= DCX_PARENTCLIP;
1427 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1428 dcxflags |= DCX_CLIPCHILDREN;
1429 hDC = GetDCEx( hwnd, 0, dcxflags);
1432 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1434 ReleaseDC( hwnd, hDC );
1437 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1440 /* If the windows has an update region, this must be
1441 * scrolled as well. Keep a copy in hrgnWinupd
1442 * to be added to hrngUpdate at the end. */
1443 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1444 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1445 if (retVal != NULLREGION)
1447 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1449 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1450 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1452 OffsetRgn( hrgnTemp, dx, dy );
1453 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1455 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1456 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1458 /* Catch the case where the scrolling amount exceeds the size of the
1459 * original window. This generated a second update area that is the
1460 * location where the original scrolled content would end up.
1461 * This second region is not returned by the ScrollDC and sets
1462 * ScrollWindowEx apart from just a ScrollDC.
1464 * This has been verified with testing on windows.
1466 if (abs(dx) > abs(rc.right - rc.left) ||
1467 abs(dy) > abs(rc.bottom - rc.top))
1469 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1470 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1471 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1474 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1476 DeleteObject( hrgnClip );
1478 DeleteObject( hrgnTemp );
1480 /* nothing was scrolled */
1482 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1483 SetRectEmpty( rcUpdate);
1486 if( flags & SW_SCROLLCHILDREN )
1488 HWND *list = WIN_ListChildren( hwnd );
1493 for (i = 0; list[i]; i++)
1495 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1496 if (!rect || IntersectRect(&dummy, &r, rect))
1497 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1498 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1499 SWP_NOREDRAW | SWP_DEFERERASE );
1501 HeapFree( GetProcessHeap(), 0, list );
1505 if( flags & (SW_INVALIDATE | SW_ERASE) )
1506 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1507 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1510 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1511 DeleteObject( hrgnWinupd);
1515 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1516 ShowCaret(hwndCaret);
1519 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1525 /*************************************************************************
1526 * ScrollWindow (USER32.@)
1529 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1530 const RECT *rect, const RECT *clipRect )
1532 return (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
1533 (rect ? 0 : SW_SCROLLCHILDREN) |
1534 SW_INVALIDATE | SW_ERASE ));
1538 /*************************************************************************
1539 * ScrollDC (USER32.@)
1541 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1542 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1543 * logical coordinates.
1545 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
1546 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
1549 return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
1552 /************************************************************************
1553 * PrintWindow (USER32.@)
1556 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1558 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1559 if(!(nFlags & PW_CLIENTONLY))
1561 flags |= PRF_NONCLIENT;
1563 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);