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 = wine_server_user_handle( 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 = wine_server_ptr_handle( 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 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect );
167 /***********************************************************************
170 static void reset_dce_attrs( struct dce *dce )
172 RestoreDC( dce->hdc, 1 ); /* initial save level is always 1 */
173 SaveDC( dce->hdc ); /* save the state again for next time */
177 /***********************************************************************
180 static void release_dce( struct dce *dce )
182 if (!dce->hwnd) return; /* already released */
184 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
186 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
189 dce->flags &= DCX_CACHE;
193 /***********************************************************************
196 static void delete_clip_rgn( struct dce *dce )
198 if (!dce->clip_rgn) return; /* nothing to do */
200 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
201 DeleteObject( dce->clip_rgn );
204 /* make it dirty so that the vis rgn gets recomputed next time */
205 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
209 /***********************************************************************
212 * Allocate a new DCE.
214 static struct dce *alloc_dce(void)
218 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
219 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
221 HeapFree( GetProcessHeap(), 0, dce );
231 /* store DCE handle in DC hook data field */
232 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
233 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
238 /***********************************************************************
241 static struct dce *get_window_dce( HWND hwnd )
244 WND *win = WIN_GetPtr( hwnd );
246 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
249 if (!dce && (dce = get_class_dce( win->class )))
254 WIN_ReleasePtr( win );
256 if (!dce) /* try to allocate one */
258 struct dce *dce_to_free = NULL;
259 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
261 if (class_style & CS_CLASSDC)
263 if (!(dce = alloc_dce())) return NULL;
265 win = WIN_GetPtr( hwnd );
266 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
268 if (win->dce) /* another thread beat us to it */
273 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
282 list_add_tail( &dce_list, &dce->entry );
284 WIN_ReleasePtr( win );
286 else dce_to_free = dce;
288 else if (class_style & CS_OWNDC)
290 if (!(dce = alloc_dce())) return NULL;
292 win = WIN_GetPtr( hwnd );
293 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
295 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
296 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
297 if (win->dce) /* another thread beat us to it */
307 list_add_tail( &dce_list, &dce->entry );
309 WIN_ReleasePtr( win );
311 else dce_to_free = dce;
316 SetDCHook( dce->hdc, NULL, 0 );
317 DeleteDC( dce->hdc );
318 HeapFree( GetProcessHeap(), 0, dce );
325 /***********************************************************************
328 * Free a class or window DCE.
330 void free_dce( struct dce *dce, HWND hwnd )
338 /* turn it into a cache entry */
339 reset_dce_attrs( dce );
341 dce->flags |= DCX_CACHE;
343 else if (dce->hwnd == hwnd)
349 /* now check for cache DCEs */
353 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
355 if (dce->hwnd != hwnd) continue;
356 if (!(dce->flags & DCX_CACHE)) continue;
358 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
368 /***********************************************************************
371 * Mark the associated DC as dirty to force a refresh of the visible region
373 static void make_dc_dirty( struct dce *dce )
377 /* Don't bother with visible regions of unused DCEs */
378 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
383 /* Set dirty bits in the hDC and DCE structs */
384 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
385 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
390 /***********************************************************************
393 * It is called from SetWindowPos() - we have to
394 * mark as dirty all busy DCEs for windows that have pWnd->parent as
395 * an ancestor and whose client rect intersects with specified update
396 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
397 * DCX_CLIPCHILDREN flag is set.
399 void invalidate_dce( HWND hwnd, const RECT *extra_rect )
403 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
405 if (!hwndScope) return;
407 GetWindowRect( hwnd, &window_rect );
409 TRACE("%p scope hwnd = %p %s (%s)\n",
410 hwnd, hwndScope, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
412 /* walk all DCEs and fixup non-empty entries */
415 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
417 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
418 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
420 if (!dce->hwnd) continue;
421 if ((dce->hwnd == hwndScope) && !(dce->flags & DCX_CLIPCHILDREN))
422 continue; /* child window positions don't bother us */
424 /* if DCE window is a child of hwnd, it has to be invalidated */
425 if (dce->hwnd == hwnd || IsChild( hwnd, dce->hwnd ))
427 make_dc_dirty( dce );
429 else /* otherwise check if the window rectangle intersects this DCE window */
431 if (hwndScope == GetDesktopWindow() ||
432 hwndScope == dce->hwnd || IsChild( hwndScope, dce->hwnd ))
435 GetWindowRect( dce->hwnd, &dce_rect );
436 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
437 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
438 make_dc_dirty( dce );
445 /***********************************************************************
448 * Implementation of ReleaseDC.
450 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
455 TRACE("%p %p\n", hwnd, hdc );
458 dce = (struct dce *)GetDCHook( hdc, NULL );
459 if (dce && dce->count)
461 if (!(dce->flags & DCX_NORESETATTRS)) reset_dce_attrs( dce );
462 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
463 if (dce->flags & DCX_CACHE) dce->count = 0;
471 /***********************************************************************
474 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
476 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
479 struct dce *dce = (struct dce *)data;
481 TRACE("hDC = %p, %u\n", hDC, code);
484 assert( dce->hdc == hDC );
488 case DCHC_INVALIDVISRGN:
489 /* GDI code calls this when it detects that the
490 * DC is dirty (usually after SetHookFlags()). This
491 * means that we have to recompute the visible region.
493 if (dce->count) update_visible_region( dce );
494 else /* non-fatal but shouldn't happen */
495 WARN("DC is not in use!\n");
499 * Windows will not let you delete a DC that is busy
500 * (between GetDC and ReleaseDC)
505 WARN("Application trying to delete a busy DC %p\n", dce->hdc);
510 list_remove( &dce->entry );
511 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
512 HeapFree( GetProcessHeap(), 0, dce );
521 /***********************************************************************
524 * Return update region (in screen coordinates) for a window.
526 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
535 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
537 SetLastError( ERROR_OUTOFMEMORY );
541 SERVER_START_REQ( get_update_region )
543 req->window = wine_server_user_handle( hwnd );
544 req->from_child = wine_server_user_handle( child ? *child : 0 );
546 wine_server_set_reply( req, data->Buffer, size );
547 if (!(status = wine_server_call( req )))
549 size_t reply_size = wine_server_reply_size( reply );
550 data->rdh.dwSize = sizeof(data->rdh);
551 data->rdh.iType = RDH_RECTANGLES;
552 data->rdh.nCount = reply_size / sizeof(RECT);
553 data->rdh.nRgnSize = reply_size;
554 hrgn = ExtCreateRegion( NULL, size, data );
555 if (child) *child = wine_server_ptr_handle( reply->child );
556 *flags = reply->flags;
558 else size = reply->total_size;
561 HeapFree( GetProcessHeap(), 0, data );
562 } while (status == STATUS_BUFFER_OVERFLOW);
564 if (status) SetLastError( RtlNtStatusToDosError(status) );
569 /***********************************************************************
572 * Get only the update flags, not the update region.
574 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
578 SERVER_START_REQ( get_update_region )
580 req->window = wine_server_user_handle( hwnd );
581 req->from_child = wine_server_user_handle( child ? *child : 0 );
582 req->flags = *flags | UPDATE_NOREGION;
583 if ((ret = !wine_server_call_err( req )))
585 if (child) *child = wine_server_ptr_handle( reply->child );
586 *flags = reply->flags;
594 /***********************************************************************
595 * redraw_window_rects
597 * Redraw part of a window.
599 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
603 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
604 return TRUE; /* nothing to do */
606 SERVER_START_REQ( redraw_window )
608 req->window = wine_server_user_handle( hwnd );
610 wine_server_add_data( req, rects, count * sizeof(RECT) );
611 ret = !wine_server_call_err( req );
618 /***********************************************************************
621 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
622 * Helper for erase_now and BeginPaint.
624 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
626 HRGN whole_rgn = get_update_region( hwnd, flags, child );
629 if (child) hwnd = *child;
631 if (hwnd == GetDesktopWindow()) return whole_rgn;
638 /* check if update rgn overlaps with nonclient area */
639 type = GetRgnBox( whole_rgn, &update );
640 WIN_GetRectangles( hwnd, COORDS_SCREEN, 0, &client );
642 if ((*flags & UPDATE_NONCLIENT) ||
643 update.left < client.left || update.top < client.top ||
644 update.right > client.right || update.bottom > client.bottom)
646 client_rgn = CreateRectRgnIndirect( &client );
647 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
649 /* check if update rgn contains complete nonclient area */
650 if (type == SIMPLEREGION)
653 GetWindowRect( hwnd, &window );
654 if (EqualRect( &window, &update ))
656 DeleteObject( whole_rgn );
663 client_rgn = whole_rgn;
667 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
669 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
670 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
677 /***********************************************************************
680 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
681 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
682 * Helper for erase_now and BeginPaint.
684 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
685 RECT *clip_rect, HDC *hdc_ret )
687 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
691 if (!clip_rect) clip_rect = &dummy;
692 if (hdc_ret || (flags & UPDATE_ERASE))
694 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
695 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
697 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
699 INT type = GetClipBox( hdc, clip_rect );
701 if (flags & UPDATE_ERASE)
703 /* don't erase if the clip box is empty */
704 if (type != NULLREGION)
705 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
707 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
710 if (hdc_ret) *hdc_ret = hdc;
712 if (!hdc) DeleteObject( client_rgn );
717 /***********************************************************************
720 * Implementation of RDW_ERASENOW behavior.
722 void erase_now( HWND hwnd, UINT rdw_flags )
726 BOOL need_erase = FALSE;
728 /* loop while we find a child to repaint */
731 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
733 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
734 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
735 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
737 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
738 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
740 if (!flags) break; /* nothing more to do */
741 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
746 /***********************************************************************
749 * Implementation of RDW_UPDATENOW behavior.
751 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
752 * SendMessage() calls. This is a comment inside DefWindowProc() source
755 * This message avoids lots of inter-app message traffic
756 * by switching to the other task and continuing the
760 * LOWORD(lParam) = hrgnClip
761 * HIWORD(lParam) = hwndSkip (not used; always NULL)
764 static void update_now( HWND hwnd, UINT rdw_flags )
768 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
769 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
771 /* loop while we find a child to repaint */
774 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
776 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
777 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
779 if (!get_update_flags( hwnd, &child, &flags )) break;
780 if (!flags) break; /* nothing more to do */
782 SendMessageW( child, WM_PAINT, 0, 0 );
783 if (rdw_flags & RDW_NOCHILDREN) break;
788 /*************************************************************************
791 * Helper for ScrollWindowEx:
792 * If the return value is 0, no special caret handling is necessary.
793 * Otherwise the return value is the handle of the window that owns the
794 * caret. Its caret needs to be hidden during the scroll operation and
795 * moved to new_caret_pos if move_caret is TRUE.
797 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
798 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
801 RECT rect, mapped_rcCaret;
802 BOOL hide_caret = FALSE;
804 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
805 if (!info.hwndCaret) return 0;
807 if (info.hwndCaret == hWnd)
809 /* Move the caret if it's (partially) in the source rectangle */
810 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
814 new_caret_pos->x = info.rcCaret.left + dx;
815 new_caret_pos->y = info.rcCaret.top + dy;
821 /* Hide the caret if it's in the destination rectangle */
823 OffsetRect(&rect, dx, dy);
824 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
829 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
833 /* Hide the caret if it's in the source or in the destination
835 mapped_rcCaret = info.rcCaret;
836 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
838 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
845 OffsetRect(&rect, dx, dy);
846 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
855 HideCaret(info.hwndCaret);
856 return info.hwndCaret;
863 /***********************************************************************
864 * BeginPaint (USER32.@)
866 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
869 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
875 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
877 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
879 TRACE("hdc = %p box = (%s), fErase = %d\n",
880 lps->hdc, wine_dbgstr_rect(&lps->rcPaint), lps->fErase);
886 /***********************************************************************
887 * EndPaint (USER32.@)
889 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
891 if (!lps) return FALSE;
892 release_dc( hwnd, lps->hdc, TRUE );
898 /***********************************************************************
901 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
903 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
904 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
906 BOOL bUpdateVisRgn = TRUE;
908 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
910 if (!hwnd) hwnd = GetDesktopWindow();
911 else hwnd = WIN_GetFullHandle( hwnd );
913 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
915 if (!IsWindow(hwnd)) return 0;
919 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
921 if (flags & DCX_USESTYLE)
923 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
925 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
927 if (!(flags & DCX_WINDOW))
929 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
931 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
932 flags |= DCX_CLIPCHILDREN;
936 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
938 parent = GetAncestor( hwnd, GA_PARENT );
939 if (!parent || (parent == GetDesktopWindow()))
940 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
942 /* it seems parent clip is ignored when clipping siblings or children */
943 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
945 if( flags & DCX_PARENTCLIP )
947 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
948 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
950 flags &= ~DCX_CLIPCHILDREN;
951 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
955 /* find a suitable DCE */
957 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
959 struct dce *dceEmpty = NULL, *dceUnused = NULL;
961 /* Strategy: First, we attempt to find a non-empty but unused DCE with
962 * compatible flags. Next, we look for an empty entry. If the cache is
963 * full we have to purge one of the unused entries.
966 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
968 if ((dce->flags & DCX_CACHE) && !dce->count)
972 if (!dce->hwnd) dceEmpty = dce;
973 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
975 TRACE("\tfound valid %p dce [%p], flags %08x\n",
976 dce, hwnd, dce->flags );
977 bUpdateVisRgn = FALSE;
983 if (&dce->entry == &dce_list) /* nothing found */
984 dce = dceEmpty ? dceEmpty : dceUnused;
986 if (dce) dce->count = 1;
990 /* if there's no dce empty or unused, allocate a new one */
993 if (!(dce = alloc_dce())) return 0;
994 dce->flags = DCX_CACHE;
996 list_add_head( &dce_list, &dce->entry );
1002 flags |= DCX_NORESETATTRS;
1003 if (dce->hwnd == hwnd)
1005 TRACE("\tskipping hVisRgn update\n");
1006 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1010 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1011 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1016 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1018 /* if the extra clip region has changed, get rid of the old one */
1019 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1020 delete_clip_rgn( dce );
1021 dce->clip_rgn = hrgnClip;
1022 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1023 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1024 bUpdateVisRgn = TRUE;
1027 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1030 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1032 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1034 if (bUpdateVisRgn) update_visible_region( dce );
1036 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1041 /***********************************************************************
1044 * Get a device context.
1047 * Success: Handle to the device context
1050 HDC WINAPI GetDC( HWND hwnd )
1052 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1053 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1057 /***********************************************************************
1058 * GetWindowDC (USER32.@)
1060 HDC WINAPI GetWindowDC( HWND hwnd )
1062 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1066 /***********************************************************************
1067 * ReleaseDC (USER32.@)
1069 * Release a device context.
1072 * Success: Non-zero. Resources used by hdc are released.
1075 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1077 return release_dc( hwnd, hdc, FALSE );
1081 /**********************************************************************
1082 * WindowFromDC (USER32.@)
1084 HWND WINAPI WindowFromDC( HDC hdc )
1090 dce = (struct dce *)GetDCHook( hdc, NULL );
1091 if (dce) hwnd = dce->hwnd;
1097 /***********************************************************************
1098 * LockWindowUpdate (USER32.@)
1100 * Enables or disables painting in the chosen window.
1103 * hwnd [I] handle to a window.
1106 * If successful, returns nonzero value. Otherwise,
1110 * You can lock only one window at a time.
1112 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1114 static HWND lockedWnd;
1116 FIXME("(%p), partial stub!\n",hwnd);
1123 /* Unlock lockedWnd */
1124 /* FIXME: Do something */
1128 /* Attempted to lock a second window */
1129 /* Return FALSE and do nothing */
1140 /***********************************************************************
1141 * RedrawWindow (USER32.@)
1143 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1145 static const RECT empty;
1148 if (!hwnd) hwnd = GetDesktopWindow();
1155 GetRgnBox( hrgn, &r );
1156 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1159 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1161 TRACE( "%p whole window ", hwnd );
1163 dump_rdw_flags(flags);
1166 /* process pending expose events before painting */
1167 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1171 if (IsRectEmpty( rect )) rect = ∅
1172 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1176 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1178 else /* need to build a list of the region rectangles */
1181 RGNDATA *data = NULL;
1183 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1184 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1185 GetRegionData( hrgn, size, data );
1186 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1187 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1189 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1190 HeapFree( GetProcessHeap(), 0, data );
1193 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1194 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1200 /***********************************************************************
1201 * UpdateWindow (USER32.@)
1203 BOOL WINAPI UpdateWindow( HWND hwnd )
1205 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1209 /***********************************************************************
1210 * InvalidateRgn (USER32.@)
1212 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1216 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1220 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1224 /***********************************************************************
1225 * InvalidateRect (USER32.@)
1227 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1228 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1230 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1232 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1236 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1240 return RedrawWindow( hwnd, rect, 0, flags );
1244 /***********************************************************************
1245 * ValidateRgn (USER32.@)
1247 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1251 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1255 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1259 /***********************************************************************
1260 * ValidateRect (USER32.@)
1262 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1263 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1265 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1267 UINT flags = RDW_VALIDATE;
1271 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1275 return RedrawWindow( hwnd, rect, 0, flags );
1279 /***********************************************************************
1280 * GetUpdateRgn (USER32.@)
1282 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1285 UINT flags = UPDATE_NOCHILDREN;
1288 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1290 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1294 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1295 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1297 flags = UPDATE_DELAYED_ERASE;
1298 get_update_flags( hwnd, NULL, &flags );
1300 /* map region to client coordinates */
1301 offset.x = offset.y = 0;
1302 ScreenToClient( hwnd, &offset );
1303 OffsetRgn( hrgn, offset.x, offset.y );
1309 /***********************************************************************
1310 * GetUpdateRect (USER32.@)
1312 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1314 UINT flags = UPDATE_NOCHILDREN;
1318 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1320 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1324 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1326 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1327 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1328 DPtoLP( hdc, (LPPOINT)rect, 2 );
1329 ReleaseDC( hwnd, hdc );
1332 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1334 /* check if we still have an update region */
1335 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1336 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1337 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1341 /***********************************************************************
1342 * ExcludeUpdateRgn (USER32.@)
1344 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1346 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1347 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1353 GetDCOrgEx( hdc, &pt );
1354 MapWindowPoints( 0, hwnd, &pt, 1 );
1355 OffsetRgn( update_rgn, -pt.x, -pt.y );
1356 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1357 DeleteObject( update_rgn );
1363 /*************************************************************************
1364 * ScrollWindowEx (USER32.@)
1366 * Note: contrary to what the doc says, pixels that are scrolled from the
1367 * outside of clipRect to the inside are NOT painted.
1370 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1371 const RECT *rect, const RECT *clipRect,
1372 HRGN hrgnUpdate, LPRECT rcUpdate,
1375 INT retVal = NULLREGION;
1376 BOOL bOwnRgn = TRUE;
1377 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1380 HRGN hrgnWinupd = 0;
1383 HWND hwndCaret = NULL;
1384 BOOL moveCaret = FALSE;
1387 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1388 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1389 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1390 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1391 FIXME("some flags (%04x) are unhandled\n", flags);
1393 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1394 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1396 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1397 hwnd = WIN_GetFullHandle( hwnd );
1399 GetClientRect(hwnd, &rc);
1401 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1404 if (rect) IntersectRect(&rc, &rc, rect);
1406 if( hrgnUpdate ) bOwnRgn = FALSE;
1407 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1409 newCaretPos.x = newCaretPos.y = 0;
1411 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1412 DWORD dcxflags = DCX_CACHE;
1413 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1415 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1417 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1418 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1419 dcxflags |= DCX_PARENTCLIP;
1420 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1421 dcxflags |= DCX_CLIPCHILDREN;
1422 hDC = GetDCEx( hwnd, 0, dcxflags);
1425 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1427 ReleaseDC( hwnd, hDC );
1430 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1433 /* If the windows has an update region, this must be
1434 * scrolled as well. Keep a copy in hrgnWinupd
1435 * to be added to hrngUpdate at the end. */
1436 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1437 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1438 if (retVal != NULLREGION)
1440 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1442 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1443 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1445 OffsetRgn( hrgnTemp, dx, dy );
1446 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1448 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1449 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1451 /* Catch the case where the scrolling amount exceeds the size of the
1452 * original window. This generated a second update area that is the
1453 * location where the original scrolled content would end up.
1454 * This second region is not returned by the ScrollDC and sets
1455 * ScrollWindowEx apart from just a ScrollDC.
1457 * This has been verified with testing on windows.
1459 if (abs(dx) > abs(rc.right - rc.left) ||
1460 abs(dy) > abs(rc.bottom - rc.top))
1462 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1463 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1464 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1467 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1469 DeleteObject( hrgnClip );
1471 DeleteObject( hrgnTemp );
1473 /* nothing was scrolled */
1475 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1476 SetRectEmpty( rcUpdate);
1479 if( flags & SW_SCROLLCHILDREN )
1481 HWND *list = WIN_ListChildren( hwnd );
1486 for (i = 0; list[i]; i++)
1488 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1489 if (!rect || IntersectRect(&dummy, &r, rect))
1490 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1491 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1492 SWP_NOREDRAW | SWP_DEFERERASE );
1494 HeapFree( GetProcessHeap(), 0, list );
1498 if( flags & (SW_INVALIDATE | SW_ERASE) )
1499 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1500 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1503 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1504 DeleteObject( hrgnWinupd);
1508 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1509 ShowCaret(hwndCaret);
1512 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1518 /*************************************************************************
1519 * ScrollWindow (USER32.@)
1522 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1523 const RECT *rect, const RECT *clipRect )
1525 return (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
1526 (rect ? 0 : SW_SCROLLCHILDREN) |
1527 SW_INVALIDATE | SW_ERASE ));
1531 /*************************************************************************
1532 * ScrollDC (USER32.@)
1534 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1535 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1536 * logical coordinates.
1538 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
1539 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
1542 return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
1545 /************************************************************************
1546 * PrintWindow (USER32.@)
1549 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1551 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1552 if(!(nFlags & PW_CLIENTONLY))
1554 flags |= PRF_NONCLIENT;
1556 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);