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 )
111 struct window_surface *surface = NULL;
115 DWORD flags = dce->flags;
117 RECT win_rect, top_rect;
120 /* don't clip siblings if using parent clip region */
121 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
123 /* fetch the visible region from the server */
126 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
129 SERVER_START_REQ( get_visible_region )
131 req->window = wine_server_user_handle( dce->hwnd );
133 wine_server_set_reply( req, data->Buffer, size );
134 if (!(status = wine_server_call( req )))
136 size_t reply_size = wine_server_reply_size( reply );
137 data->rdh.dwSize = sizeof(data->rdh);
138 data->rdh.iType = RDH_RECTANGLES;
139 data->rdh.nCount = reply_size / sizeof(RECT);
140 data->rdh.nRgnSize = reply_size;
141 vis_rgn = ExtCreateRegion( NULL, size, data );
143 top_win = wine_server_ptr_handle( reply->top_win );
144 win_rect.left = reply->win_rect.left;
145 win_rect.top = reply->win_rect.top;
146 win_rect.right = reply->win_rect.right;
147 win_rect.bottom = reply->win_rect.bottom;
148 top_rect.left = reply->top_rect.left;
149 top_rect.top = reply->top_rect.top;
150 top_rect.right = reply->top_rect.right;
151 top_rect.bottom = reply->top_rect.bottom;
153 else size = reply->total_size;
156 HeapFree( GetProcessHeap(), 0, data );
157 } while (status == STATUS_BUFFER_OVERFLOW);
159 if (status || !vis_rgn) return;
161 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
163 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
164 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
166 if ((win = WIN_GetPtr( top_win )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
168 surface = win->surface;
169 if (surface) window_surface_add_ref( surface );
170 WIN_ReleasePtr( win );
173 if (!surface) top_rect = get_virtual_screen_rect();
174 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
175 if (surface) window_surface_release( surface );
179 /***********************************************************************
182 static void reset_dce_attrs( struct dce *dce )
184 RestoreDC( dce->hdc, 1 ); /* initial save level is always 1 */
185 SaveDC( dce->hdc ); /* save the state again for next time */
189 /***********************************************************************
192 static void release_dce( struct dce *dce )
194 if (!dce->hwnd) return; /* already released */
196 __wine_set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
197 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
199 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
202 dce->flags &= DCX_CACHE;
206 /***********************************************************************
209 static void delete_clip_rgn( struct dce *dce )
211 if (!dce->clip_rgn) return; /* nothing to do */
213 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
214 DeleteObject( dce->clip_rgn );
217 /* make it dirty so that the vis rgn gets recomputed next time */
218 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
222 /***********************************************************************
225 * Allocate a new DCE.
227 static struct dce *alloc_dce(void)
231 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
232 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
234 HeapFree( GetProcessHeap(), 0, dce );
244 /* store DCE handle in DC hook data field */
245 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
246 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
251 /***********************************************************************
254 static struct dce *get_window_dce( HWND hwnd )
257 WND *win = WIN_GetPtr( hwnd );
259 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
262 if (!dce && (dce = get_class_dce( win->class )))
267 WIN_ReleasePtr( win );
269 if (!dce) /* try to allocate one */
271 struct dce *dce_to_free = NULL;
272 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
274 if (class_style & CS_CLASSDC)
276 if (!(dce = alloc_dce())) return NULL;
278 win = WIN_GetPtr( hwnd );
279 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
281 if (win->dce) /* another thread beat us to it */
286 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
295 list_add_tail( &dce_list, &dce->entry );
297 WIN_ReleasePtr( win );
299 else dce_to_free = dce;
301 else if (class_style & CS_OWNDC)
303 if (!(dce = alloc_dce())) return NULL;
305 win = WIN_GetPtr( hwnd );
306 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
308 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
309 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
310 if (win->dce) /* another thread beat us to it */
320 list_add_tail( &dce_list, &dce->entry );
322 WIN_ReleasePtr( win );
324 else dce_to_free = dce;
329 SetDCHook( dce_to_free->hdc, NULL, 0 );
330 DeleteDC( dce_to_free->hdc );
331 HeapFree( GetProcessHeap(), 0, dce_to_free );
332 if (dce_to_free == dce)
340 /***********************************************************************
343 * Free a class or window DCE.
345 void free_dce( struct dce *dce, HWND hwnd )
353 /* turn it into a cache entry */
354 reset_dce_attrs( dce );
356 dce->flags |= DCX_CACHE;
358 else if (dce->hwnd == hwnd)
364 /* now check for cache DCEs */
368 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
370 if (dce->hwnd != hwnd) continue;
371 if (!(dce->flags & DCX_CACHE)) continue;
373 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
383 /***********************************************************************
386 * Mark the associated DC as dirty to force a refresh of the visible region
388 static void make_dc_dirty( struct dce *dce )
392 /* Don't bother with visible regions of unused DCEs */
393 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
398 /* Set dirty bits in the hDC and DCE structs */
399 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
400 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
405 /***********************************************************************
408 * It is called from SetWindowPos() - we have to
409 * mark as dirty all busy DCEs for windows that have pWnd->parent as
410 * an ancestor and whose client rect intersects with specified update
411 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
412 * DCX_CLIPCHILDREN flag is set.
414 void invalidate_dce( WND *win, const RECT *extra_rect )
419 if (!win->parent) return;
421 GetWindowRect( win->obj.handle, &window_rect );
423 TRACE("%p parent %p %s (%s)\n",
424 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
426 /* walk all DCEs and fixup non-empty entries */
428 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
430 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
431 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
433 if (!dce->hwnd) continue;
434 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
435 continue; /* child window positions don't bother us */
437 /* if DCE window is a child of hwnd, it has to be invalidated */
438 if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
440 make_dc_dirty( dce );
444 /* otherwise check if the window rectangle intersects this DCE window */
445 if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
448 GetWindowRect( dce->hwnd, &dce_rect );
449 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
450 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
451 make_dc_dirty( dce );
456 /***********************************************************************
459 * Implementation of ReleaseDC.
461 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
466 TRACE("%p %p\n", hwnd, hdc );
469 dce = (struct dce *)GetDCHook( hdc, NULL );
470 if (dce && dce->count)
472 if (!(dce->flags & DCX_NORESETATTRS)) reset_dce_attrs( dce );
473 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
474 if (dce->flags & DCX_CACHE) dce->count = 0;
482 /***********************************************************************
485 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
487 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
490 struct dce *dce = (struct dce *)data;
492 TRACE("hDC = %p, %u\n", hDC, code);
495 assert( dce->hdc == hDC );
499 case DCHC_INVALIDVISRGN:
500 /* GDI code calls this when it detects that the
501 * DC is dirty (usually after SetHookFlags()). This
502 * means that we have to recompute the visible region.
504 if (dce->count) update_visible_region( dce );
505 else /* non-fatal but shouldn't happen */
506 WARN("DC is not in use!\n");
510 * Windows will not let you delete a DC that is busy
511 * (between GetDC and ReleaseDC)
516 WARN("Application trying to delete a busy DC %p\n", dce->hdc);
521 list_remove( &dce->entry );
522 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
523 HeapFree( GetProcessHeap(), 0, dce );
532 /***********************************************************************
535 * Return update region (in screen coordinates) for a window.
537 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
546 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
548 SetLastError( ERROR_OUTOFMEMORY );
552 SERVER_START_REQ( get_update_region )
554 req->window = wine_server_user_handle( hwnd );
555 req->from_child = wine_server_user_handle( child ? *child : 0 );
557 wine_server_set_reply( req, data->Buffer, size );
558 if (!(status = wine_server_call( req )))
560 size_t reply_size = wine_server_reply_size( reply );
561 data->rdh.dwSize = sizeof(data->rdh);
562 data->rdh.iType = RDH_RECTANGLES;
563 data->rdh.nCount = reply_size / sizeof(RECT);
564 data->rdh.nRgnSize = reply_size;
565 hrgn = ExtCreateRegion( NULL, size, data );
566 if (child) *child = wine_server_ptr_handle( reply->child );
567 *flags = reply->flags;
569 else size = reply->total_size;
572 HeapFree( GetProcessHeap(), 0, data );
573 } while (status == STATUS_BUFFER_OVERFLOW);
575 if (status) SetLastError( RtlNtStatusToDosError(status) );
580 /***********************************************************************
583 * Get only the update flags, not the update region.
585 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
589 SERVER_START_REQ( get_update_region )
591 req->window = wine_server_user_handle( hwnd );
592 req->from_child = wine_server_user_handle( child ? *child : 0 );
593 req->flags = *flags | UPDATE_NOREGION;
594 if ((ret = !wine_server_call_err( req )))
596 if (child) *child = wine_server_ptr_handle( reply->child );
597 *flags = reply->flags;
605 /***********************************************************************
606 * redraw_window_rects
608 * Redraw part of a window.
610 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
614 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
615 return TRUE; /* nothing to do */
617 SERVER_START_REQ( redraw_window )
619 req->window = wine_server_user_handle( hwnd );
621 wine_server_add_data( req, rects, count * sizeof(RECT) );
622 ret = !wine_server_call_err( req );
629 /***********************************************************************
632 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
633 * Helper for erase_now and BeginPaint.
635 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
637 HRGN whole_rgn = get_update_region( hwnd, flags, child );
640 if (child) hwnd = *child;
642 if (hwnd == GetDesktopWindow()) return whole_rgn;
649 /* check if update rgn overlaps with nonclient area */
650 type = GetRgnBox( whole_rgn, &update );
651 WIN_GetRectangles( hwnd, COORDS_SCREEN, 0, &client );
653 if ((*flags & UPDATE_NONCLIENT) ||
654 update.left < client.left || update.top < client.top ||
655 update.right > client.right || update.bottom > client.bottom)
657 client_rgn = CreateRectRgnIndirect( &client );
658 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
660 /* check if update rgn contains complete nonclient area */
661 if (type == SIMPLEREGION)
664 GetWindowRect( hwnd, &window );
665 if (EqualRect( &window, &update ))
667 DeleteObject( whole_rgn );
674 client_rgn = whole_rgn;
678 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
680 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
681 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
688 /***********************************************************************
691 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
692 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
693 * Helper for erase_now and BeginPaint.
695 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
696 RECT *clip_rect, HDC *hdc_ret )
698 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
702 if (!clip_rect) clip_rect = &dummy;
703 if (hdc_ret || (flags & UPDATE_ERASE))
705 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
706 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
708 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
710 INT type = GetClipBox( hdc, clip_rect );
712 if (flags & UPDATE_ERASE)
714 /* don't erase if the clip box is empty */
715 if (type != NULLREGION)
716 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
718 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
721 if (hdc_ret) *hdc_ret = hdc;
723 if (!hdc) DeleteObject( client_rgn );
728 /***********************************************************************
731 * Implementation of RDW_ERASENOW behavior.
733 void erase_now( HWND hwnd, UINT rdw_flags )
737 BOOL need_erase = FALSE;
739 /* loop while we find a child to repaint */
742 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
744 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
745 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
746 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
748 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
749 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
751 if (!flags) break; /* nothing more to do */
752 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
757 /***********************************************************************
760 * Move the window bits when a window is resized or its surface recreated.
762 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
763 struct window_surface *new_surface,
764 const RECT *visible_rect, const RECT *old_visible_rect,
765 const RECT *client_rect, const RECT *valid_rects )
767 RECT dst = valid_rects[0];
768 RECT src = valid_rects[1];
770 if (new_surface != old_surface ||
771 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
772 src.top - old_visible_rect->top != dst.top - visible_rect->top)
774 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
775 BITMAPINFO *info = (BITMAPINFO *)buffer;
777 UINT flags = UPDATE_NOCHILDREN;
778 HRGN rgn = get_update_region( hwnd, &flags, NULL );
779 HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_EXCLUDERGN );
781 OffsetRect( &dst, -client_rect->left, -client_rect->top );
782 TRACE( "copying %s -> %s\n", wine_dbgstr_rect(&src), wine_dbgstr_rect(&dst) );
783 bits = old_surface->funcs->get_info( old_surface, info );
784 old_surface->funcs->lock( old_surface );
785 SetDIBitsToDevice( hdc, dst.left, dst.top, dst.right - dst.left, dst.bottom - dst.top,
786 src.left - old_visible_rect->left - old_surface->rect.left,
787 old_surface->rect.bottom - (src.bottom - old_visible_rect->top),
788 0, old_surface->rect.bottom - old_surface->rect.top,
789 bits, info, DIB_RGB_COLORS );
790 old_surface->funcs->unlock( old_surface );
791 ReleaseDC( hwnd, hdc );
797 /***********************************************************************
800 * Implementation of RDW_UPDATENOW behavior.
802 static void update_now( HWND hwnd, UINT rdw_flags )
806 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
807 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
809 /* loop while we find a child to repaint */
812 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
814 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
815 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
817 if (!get_update_flags( hwnd, &child, &flags )) break;
818 if (!flags) break; /* nothing more to do */
820 SendMessageW( child, WM_PAINT, 0, 0 );
821 if (rdw_flags & RDW_NOCHILDREN) break;
826 /*************************************************************************
829 * Helper for ScrollWindowEx:
830 * If the return value is 0, no special caret handling is necessary.
831 * Otherwise the return value is the handle of the window that owns the
832 * caret. Its caret needs to be hidden during the scroll operation and
833 * moved to new_caret_pos if move_caret is TRUE.
835 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
836 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
839 RECT rect, mapped_rcCaret;
840 BOOL hide_caret = FALSE;
842 info.cbSize = sizeof(info);
843 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
844 if (!info.hwndCaret) return 0;
846 if (info.hwndCaret == hWnd)
848 /* Move the caret if it's (partially) in the source rectangle */
849 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
853 new_caret_pos->x = info.rcCaret.left + dx;
854 new_caret_pos->y = info.rcCaret.top + dy;
860 /* Hide the caret if it's in the destination rectangle */
862 OffsetRect(&rect, dx, dy);
863 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
868 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
872 /* Hide the caret if it's in the source or in the destination
874 mapped_rcCaret = info.rcCaret;
875 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
877 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
884 OffsetRect(&rect, dx, dy);
885 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
894 HideCaret(info.hwndCaret);
895 return info.hwndCaret;
902 /***********************************************************************
903 * BeginPaint (USER32.@)
905 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
908 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
914 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
916 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
918 TRACE("hdc = %p box = (%s), fErase = %d\n",
919 lps->hdc, wine_dbgstr_rect(&lps->rcPaint), lps->fErase);
925 /***********************************************************************
926 * EndPaint (USER32.@)
928 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
930 if (!lps) return FALSE;
931 release_dc( hwnd, lps->hdc, TRUE );
933 flush_window_surfaces( FALSE );
938 /***********************************************************************
941 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
943 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
944 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
946 BOOL bUpdateVisRgn = TRUE;
948 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
950 if (!hwnd) hwnd = GetDesktopWindow();
951 else hwnd = WIN_GetFullHandle( hwnd );
953 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
955 if (!IsWindow(hwnd)) return 0;
959 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
961 if (flags & DCX_USESTYLE)
963 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
965 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
967 if (!(flags & DCX_WINDOW))
969 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
971 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
972 flags |= DCX_CLIPCHILDREN;
976 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
978 parent = GetAncestor( hwnd, GA_PARENT );
979 if (!parent || (parent == GetDesktopWindow()))
980 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
982 /* it seems parent clip is ignored when clipping siblings or children */
983 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
985 if( flags & DCX_PARENTCLIP )
987 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
988 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
990 flags &= ~DCX_CLIPCHILDREN;
991 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
995 /* find a suitable DCE */
997 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
999 struct dce *dceEmpty = NULL, *dceUnused = NULL;
1001 /* Strategy: First, we attempt to find a non-empty but unused DCE with
1002 * compatible flags. Next, we look for an empty entry. If the cache is
1003 * full we have to purge one of the unused entries.
1006 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1008 if ((dce->flags & DCX_CACHE) && !dce->count)
1012 if (!dce->hwnd) dceEmpty = dce;
1013 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1015 TRACE("\tfound valid %p dce [%p], flags %08x\n",
1016 dce, hwnd, dce->flags );
1017 bUpdateVisRgn = FALSE;
1023 if (&dce->entry == &dce_list) /* nothing found */
1024 dce = dceEmpty ? dceEmpty : dceUnused;
1026 if (dce) dce->count = 1;
1030 /* if there's no dce empty or unused, allocate a new one */
1033 if (!(dce = alloc_dce())) return 0;
1034 dce->flags = DCX_CACHE;
1036 list_add_head( &dce_list, &dce->entry );
1042 flags |= DCX_NORESETATTRS;
1043 if (dce->hwnd == hwnd)
1045 TRACE("\tskipping hVisRgn update\n");
1046 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1050 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1051 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1056 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1058 /* if the extra clip region has changed, get rid of the old one */
1059 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1060 delete_clip_rgn( dce );
1061 dce->clip_rgn = hrgnClip;
1062 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1063 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1064 bUpdateVisRgn = TRUE;
1067 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1070 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1072 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1074 if (bUpdateVisRgn) update_visible_region( dce );
1076 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1081 /***********************************************************************
1084 * Get a device context.
1087 * Success: Handle to the device context
1090 HDC WINAPI GetDC( HWND hwnd )
1092 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1093 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1097 /***********************************************************************
1098 * GetWindowDC (USER32.@)
1100 HDC WINAPI GetWindowDC( HWND hwnd )
1102 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1106 /***********************************************************************
1107 * ReleaseDC (USER32.@)
1109 * Release a device context.
1112 * Success: Non-zero. Resources used by hdc are released.
1115 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1117 return release_dc( hwnd, hdc, FALSE );
1121 /**********************************************************************
1122 * WindowFromDC (USER32.@)
1124 HWND WINAPI WindowFromDC( HDC hdc )
1130 dce = (struct dce *)GetDCHook( hdc, NULL );
1131 if (dce) hwnd = dce->hwnd;
1137 /***********************************************************************
1138 * LockWindowUpdate (USER32.@)
1140 * Enables or disables painting in the chosen window.
1143 * hwnd [I] handle to a window.
1146 * If successful, returns nonzero value. Otherwise,
1150 * You can lock only one window at a time.
1152 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1154 static HWND lockedWnd;
1156 FIXME("(%p), partial stub!\n",hwnd);
1163 /* Unlock lockedWnd */
1164 /* FIXME: Do something */
1168 /* Attempted to lock a second window */
1169 /* Return FALSE and do nothing */
1180 /***********************************************************************
1181 * RedrawWindow (USER32.@)
1183 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1185 static const RECT empty;
1188 if (!hwnd) hwnd = GetDesktopWindow();
1195 GetRgnBox( hrgn, &r );
1196 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1199 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1201 TRACE( "%p whole window ", hwnd );
1203 dump_rdw_flags(flags);
1206 /* process pending expose events before painting */
1207 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1211 if (IsRectEmpty( rect )) rect = ∅
1212 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1216 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1218 else /* need to build a list of the region rectangles */
1221 RGNDATA *data = NULL;
1223 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1224 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1225 GetRegionData( hrgn, size, data );
1226 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1227 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1229 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1230 HeapFree( GetProcessHeap(), 0, data );
1233 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1234 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1240 /***********************************************************************
1241 * UpdateWindow (USER32.@)
1243 BOOL WINAPI UpdateWindow( HWND hwnd )
1247 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1251 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1255 /***********************************************************************
1256 * InvalidateRgn (USER32.@)
1258 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1262 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1266 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1270 /***********************************************************************
1271 * InvalidateRect (USER32.@)
1273 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1274 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1276 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1278 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1282 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1286 return RedrawWindow( hwnd, rect, 0, flags );
1290 /***********************************************************************
1291 * ValidateRgn (USER32.@)
1293 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1297 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1301 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1305 /***********************************************************************
1306 * ValidateRect (USER32.@)
1308 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1309 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1311 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1313 UINT flags = RDW_VALIDATE;
1317 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1321 return RedrawWindow( hwnd, rect, 0, flags );
1325 /***********************************************************************
1326 * GetUpdateRgn (USER32.@)
1328 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1331 UINT flags = UPDATE_NOCHILDREN;
1334 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1336 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1338 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1339 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1341 flags = UPDATE_DELAYED_ERASE;
1342 get_update_flags( hwnd, NULL, &flags );
1344 /* map region to client coordinates */
1345 map_window_region( 0, hwnd, hrgn );
1351 /***********************************************************************
1352 * GetUpdateRect (USER32.@)
1354 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1356 UINT flags = UPDATE_NOCHILDREN;
1360 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1362 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1366 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1368 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1369 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1370 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1371 DPtoLP( hdc, (LPPOINT)rect, 2 );
1372 SetLayout( hdc, layout );
1373 ReleaseDC( hwnd, hdc );
1376 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1378 /* check if we still have an update region */
1379 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1380 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1381 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1385 /***********************************************************************
1386 * ExcludeUpdateRgn (USER32.@)
1388 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1390 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1391 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1397 GetDCOrgEx( hdc, &pt );
1398 MapWindowPoints( 0, hwnd, &pt, 1 );
1399 OffsetRgn( update_rgn, -pt.x, -pt.y );
1400 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1401 DeleteObject( update_rgn );
1407 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1408 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1410 INT retVal = NULLREGION;
1411 BOOL bOwnRgn = TRUE;
1412 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1415 HRGN hrgnWinupd = 0;
1418 HWND hwndCaret = NULL;
1419 BOOL moveCaret = FALSE;
1422 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1423 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1424 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1425 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1426 FIXME("some flags (%04x) are unhandled\n", flags);
1428 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1429 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1431 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1432 hwnd = WIN_GetFullHandle( hwnd );
1434 GetClientRect(hwnd, &rc);
1436 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1439 if (rect) IntersectRect(&rc, &rc, rect);
1441 if( hrgnUpdate ) bOwnRgn = FALSE;
1442 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1444 newCaretPos.x = newCaretPos.y = 0;
1446 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1448 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1450 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1452 if (is_ex) dcxflags |= DCX_CACHE;
1453 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1454 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1455 dcxflags |= DCX_PARENTCLIP;
1456 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1457 dcxflags |= DCX_CLIPCHILDREN;
1458 hDC = GetDCEx( hwnd, 0, dcxflags);
1461 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1463 ReleaseDC( hwnd, hDC );
1466 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1469 /* If the windows has an update region, this must be
1470 * scrolled as well. Keep a copy in hrgnWinupd
1471 * to be added to hrngUpdate at the end. */
1472 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1473 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1474 if (retVal != NULLREGION)
1476 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1478 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1479 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1481 OffsetRgn( hrgnTemp, dx, dy );
1482 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1484 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1485 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1487 /* Catch the case where the scrolling amount exceeds the size of the
1488 * original window. This generated a second update area that is the
1489 * location where the original scrolled content would end up.
1490 * This second region is not returned by the ScrollDC and sets
1491 * ScrollWindowEx apart from just a ScrollDC.
1493 * This has been verified with testing on windows.
1495 if (abs(dx) > abs(rc.right - rc.left) ||
1496 abs(dy) > abs(rc.bottom - rc.top))
1498 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1499 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1500 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1503 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1505 DeleteObject( hrgnClip );
1507 DeleteObject( hrgnTemp );
1509 /* nothing was scrolled */
1511 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1512 SetRectEmpty( rcUpdate);
1515 if( flags & SW_SCROLLCHILDREN )
1517 HWND *list = WIN_ListChildren( hwnd );
1522 for (i = 0; list[i]; i++)
1524 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1525 if (!rect || IntersectRect(&dummy, &r, rect))
1526 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1527 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1528 SWP_NOREDRAW | SWP_DEFERERASE );
1530 HeapFree( GetProcessHeap(), 0, list );
1534 if( flags & (SW_INVALIDATE | SW_ERASE) )
1535 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1536 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1539 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1540 DeleteObject( hrgnWinupd);
1544 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1545 ShowCaret(hwndCaret);
1548 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1554 /*************************************************************************
1555 * ScrollWindowEx (USER32.@)
1557 * Note: contrary to what the doc says, pixels that are scrolled from the
1558 * outside of clipRect to the inside are NOT painted.
1561 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1562 const RECT *rect, const RECT *clipRect,
1563 HRGN hrgnUpdate, LPRECT rcUpdate,
1566 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1569 /*************************************************************************
1570 * ScrollWindow (USER32.@)
1573 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1574 const RECT *rect, const RECT *clipRect )
1576 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1577 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1581 /*************************************************************************
1582 * ScrollDC (USER32.@)
1584 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1585 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1586 * logical coordinates.
1588 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
1589 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
1592 return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
1595 /************************************************************************
1596 * PrintWindow (USER32.@)
1599 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1601 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1602 if(!(nFlags & PW_CLIENTONLY))
1604 flags |= PRF_NONCLIENT;
1606 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);