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 /* don't use a surface to paint the client area of OpenGL windows */
169 if (!win->pixel_format || (flags & DCX_WINDOW)) surface = win->surface;
170 if (surface) window_surface_add_ref( surface );
171 WIN_ReleasePtr( win );
174 if (!surface) top_rect = get_virtual_screen_rect();
175 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
176 if (surface) window_surface_release( surface );
180 /***********************************************************************
183 static void release_dce( struct dce *dce )
185 if (!dce->hwnd) return; /* already released */
187 __wine_set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
188 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
190 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
193 dce->flags &= DCX_CACHE;
197 /***********************************************************************
200 static void delete_clip_rgn( struct dce *dce )
202 if (!dce->clip_rgn) return; /* nothing to do */
204 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
205 DeleteObject( dce->clip_rgn );
208 /* make it dirty so that the vis rgn gets recomputed next time */
209 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
213 /***********************************************************************
216 * Allocate a new DCE.
218 static struct dce *alloc_dce(void)
222 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
223 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
225 HeapFree( GetProcessHeap(), 0, dce );
233 /* store DCE handle in DC hook data field */
234 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
235 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
240 /***********************************************************************
243 static struct dce *get_window_dce( HWND hwnd )
246 WND *win = WIN_GetPtr( hwnd );
248 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
251 if (!dce && (dce = get_class_dce( win->class )))
256 WIN_ReleasePtr( win );
258 if (!dce) /* try to allocate one */
260 struct dce *dce_to_free = NULL;
261 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
263 if (class_style & CS_CLASSDC)
265 if (!(dce = alloc_dce())) return NULL;
267 win = WIN_GetPtr( hwnd );
268 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
270 if (win->dce) /* another thread beat us to it */
275 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
284 list_add_tail( &dce_list, &dce->entry );
286 WIN_ReleasePtr( win );
288 else dce_to_free = dce;
290 else if (class_style & CS_OWNDC)
292 if (!(dce = alloc_dce())) return NULL;
294 win = WIN_GetPtr( hwnd );
295 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
297 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
298 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
299 if (win->dce) /* another thread beat us to it */
309 list_add_tail( &dce_list, &dce->entry );
311 WIN_ReleasePtr( win );
313 else dce_to_free = dce;
318 SetDCHook( dce_to_free->hdc, NULL, 0 );
319 DeleteDC( dce_to_free->hdc );
320 HeapFree( GetProcessHeap(), 0, dce_to_free );
321 if (dce_to_free == dce)
329 /***********************************************************************
332 * Free a class or window DCE.
334 void free_dce( struct dce *dce, HWND hwnd )
342 /* turn it into a cache entry */
343 SetHookFlags( dce->hdc, DCHF_RESETDC );
345 dce->flags |= DCX_CACHE;
347 else if (dce->hwnd == hwnd)
353 /* now check for cache DCEs */
357 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
359 if (dce->hwnd != hwnd) continue;
360 if (!(dce->flags & DCX_CACHE)) continue;
362 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
372 /***********************************************************************
375 * Mark the associated DC as dirty to force a refresh of the visible region
377 static void make_dc_dirty( struct dce *dce )
381 /* Don't bother with visible regions of unused DCEs */
382 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
387 /* Set dirty bits in the hDC and DCE structs */
388 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
389 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
394 /***********************************************************************
397 * It is called from SetWindowPos() - we have to
398 * mark as dirty all busy DCEs for windows that have pWnd->parent as
399 * an ancestor and whose client rect intersects with specified update
400 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
401 * DCX_CLIPCHILDREN flag is set.
403 void invalidate_dce( WND *win, const RECT *extra_rect )
408 if (!win->parent) return;
410 GetWindowRect( win->obj.handle, &window_rect );
412 TRACE("%p parent %p %s (%s)\n",
413 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
415 /* walk all DCEs and fixup non-empty entries */
417 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
419 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
420 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
422 if (!dce->hwnd) continue;
423 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
424 continue; /* child window positions don't bother us */
426 /* if DCE window is a child of hwnd, it has to be invalidated */
427 if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
429 make_dc_dirty( dce );
433 /* otherwise check if the window rectangle intersects this DCE window */
434 if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
437 GetWindowRect( dce->hwnd, &dce_rect );
438 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
439 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
440 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)) SetHookFlags( dce->hdc, DCHF_RESETDC );
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 * Move the window bits when a window is resized or its surface recreated.
751 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
752 struct window_surface *new_surface,
753 const RECT *visible_rect, const RECT *old_visible_rect,
754 const RECT *client_rect, const RECT *valid_rects )
756 RECT dst = valid_rects[0];
757 RECT src = valid_rects[1];
759 if (new_surface != old_surface ||
760 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
761 src.top - old_visible_rect->top != dst.top - visible_rect->top)
763 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
764 BITMAPINFO *info = (BITMAPINFO *)buffer;
766 UINT flags = UPDATE_NOCHILDREN;
767 HRGN rgn = get_update_region( hwnd, &flags, NULL );
768 HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_EXCLUDERGN );
770 OffsetRect( &dst, -client_rect->left, -client_rect->top );
771 TRACE( "copying %s -> %s\n", wine_dbgstr_rect(&src), wine_dbgstr_rect(&dst) );
772 bits = old_surface->funcs->get_info( old_surface, info );
773 old_surface->funcs->lock( old_surface );
774 SetDIBitsToDevice( hdc, dst.left, dst.top, dst.right - dst.left, dst.bottom - dst.top,
775 src.left - old_visible_rect->left - old_surface->rect.left,
776 old_surface->rect.bottom - (src.bottom - old_visible_rect->top),
777 0, old_surface->rect.bottom - old_surface->rect.top,
778 bits, info, DIB_RGB_COLORS );
779 old_surface->funcs->unlock( old_surface );
780 ReleaseDC( hwnd, hdc );
785 /***********************************************************************
788 * Implementation of RDW_UPDATENOW behavior.
790 static void update_now( HWND hwnd, UINT rdw_flags )
794 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
795 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
797 /* loop while we find a child to repaint */
800 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
802 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
803 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
805 if (!get_update_flags( hwnd, &child, &flags )) break;
806 if (!flags) break; /* nothing more to do */
808 SendMessageW( child, WM_PAINT, 0, 0 );
809 if (rdw_flags & RDW_NOCHILDREN) break;
814 /*************************************************************************
817 * Helper for ScrollWindowEx:
818 * If the return value is 0, no special caret handling is necessary.
819 * Otherwise the return value is the handle of the window that owns the
820 * caret. Its caret needs to be hidden during the scroll operation and
821 * moved to new_caret_pos if move_caret is TRUE.
823 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
824 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
827 RECT rect, mapped_rcCaret;
828 BOOL hide_caret = FALSE;
830 info.cbSize = sizeof(info);
831 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
832 if (!info.hwndCaret) return 0;
834 if (info.hwndCaret == hWnd)
836 /* Move the caret if it's (partially) in the source rectangle */
837 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
841 new_caret_pos->x = info.rcCaret.left + dx;
842 new_caret_pos->y = info.rcCaret.top + dy;
848 /* Hide the caret if it's in the destination rectangle */
850 OffsetRect(&rect, dx, dy);
851 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
856 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
860 /* Hide the caret if it's in the source or in the destination
862 mapped_rcCaret = info.rcCaret;
863 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
865 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
872 OffsetRect(&rect, dx, dy);
873 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
882 HideCaret(info.hwndCaret);
883 return info.hwndCaret;
890 /***********************************************************************
891 * BeginPaint (USER32.@)
893 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
899 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
903 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
905 erase = send_erase( hwnd, flags, hrgn, &rect, &hdc );
907 TRACE("hdc = %p box = (%s), fErase = %d\n", hdc, wine_dbgstr_rect(&rect), erase);
911 release_dc( hwnd, hdc, TRUE );
921 /***********************************************************************
922 * EndPaint (USER32.@)
924 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
927 flush_window_surfaces( FALSE );
928 if (!lps) return FALSE;
929 release_dc( hwnd, lps->hdc, TRUE );
934 /***********************************************************************
937 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
939 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
940 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
942 BOOL bUpdateVisRgn = TRUE;
944 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
946 if (!hwnd) hwnd = GetDesktopWindow();
947 else hwnd = WIN_GetFullHandle( hwnd );
949 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
951 if (!IsWindow(hwnd)) return 0;
955 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
957 if (flags & DCX_USESTYLE)
959 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
961 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
963 if (!(flags & DCX_WINDOW))
965 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
967 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
968 flags |= DCX_CLIPCHILDREN;
972 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
974 parent = GetAncestor( hwnd, GA_PARENT );
975 if (!parent || (parent == GetDesktopWindow()))
976 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
978 /* it seems parent clip is ignored when clipping siblings or children */
979 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
981 if( flags & DCX_PARENTCLIP )
983 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
984 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
986 flags &= ~DCX_CLIPCHILDREN;
987 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
991 /* find a suitable DCE */
993 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
995 struct dce *dceEmpty = NULL, *dceUnused = NULL;
997 /* Strategy: First, we attempt to find a non-empty but unused DCE with
998 * compatible flags. Next, we look for an empty entry. If the cache is
999 * full we have to purge one of the unused entries.
1002 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1004 if ((dce->flags & DCX_CACHE) && !dce->count)
1008 if (!dce->hwnd) dceEmpty = dce;
1009 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1011 TRACE("\tfound valid %p dce [%p], flags %08x\n",
1012 dce, hwnd, dce->flags );
1013 bUpdateVisRgn = FALSE;
1019 if (&dce->entry == &dce_list) /* nothing found */
1020 dce = dceEmpty ? dceEmpty : dceUnused;
1022 if (dce) dce->count = 1;
1026 /* if there's no dce empty or unused, allocate a new one */
1029 if (!(dce = alloc_dce())) return 0;
1030 dce->flags = DCX_CACHE;
1032 list_add_head( &dce_list, &dce->entry );
1038 flags |= DCX_NORESETATTRS;
1039 if (dce->hwnd == hwnd)
1041 TRACE("\tskipping hVisRgn update\n");
1042 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1046 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1047 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1052 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1054 /* if the extra clip region has changed, get rid of the old one */
1055 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1056 delete_clip_rgn( dce );
1057 dce->clip_rgn = hrgnClip;
1058 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1059 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1060 bUpdateVisRgn = TRUE;
1063 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1066 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1068 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1070 if (bUpdateVisRgn) update_visible_region( dce );
1072 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1077 /***********************************************************************
1080 * Get a device context.
1083 * Success: Handle to the device context
1086 HDC WINAPI GetDC( HWND hwnd )
1088 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1089 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1093 /***********************************************************************
1094 * GetWindowDC (USER32.@)
1096 HDC WINAPI GetWindowDC( HWND hwnd )
1098 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1102 /***********************************************************************
1103 * ReleaseDC (USER32.@)
1105 * Release a device context.
1108 * Success: Non-zero. Resources used by hdc are released.
1111 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1113 return release_dc( hwnd, hdc, FALSE );
1117 /**********************************************************************
1118 * WindowFromDC (USER32.@)
1120 HWND WINAPI WindowFromDC( HDC hdc )
1126 dce = (struct dce *)GetDCHook( hdc, NULL );
1127 if (dce) hwnd = dce->hwnd;
1133 /***********************************************************************
1134 * LockWindowUpdate (USER32.@)
1136 * Enables or disables painting in the chosen window.
1139 * hwnd [I] handle to a window.
1142 * If successful, returns nonzero value. Otherwise,
1146 * You can lock only one window at a time.
1148 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1150 static HWND lockedWnd;
1152 FIXME("(%p), partial stub!\n",hwnd);
1159 /* Unlock lockedWnd */
1160 /* FIXME: Do something */
1164 /* Attempted to lock a second window */
1165 /* Return FALSE and do nothing */
1176 /***********************************************************************
1177 * RedrawWindow (USER32.@)
1179 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1181 static const RECT empty;
1184 if (!hwnd) hwnd = GetDesktopWindow();
1191 GetRgnBox( hrgn, &r );
1192 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1195 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1197 TRACE( "%p whole window ", hwnd );
1199 dump_rdw_flags(flags);
1202 /* process pending expose events before painting */
1203 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1207 if (IsRectEmpty( rect )) rect = ∅
1208 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1212 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1214 else /* need to build a list of the region rectangles */
1217 RGNDATA *data = NULL;
1219 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1220 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1221 GetRegionData( hrgn, size, data );
1222 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1223 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1225 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1226 HeapFree( GetProcessHeap(), 0, data );
1229 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1230 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1236 /***********************************************************************
1237 * UpdateWindow (USER32.@)
1239 BOOL WINAPI UpdateWindow( HWND hwnd )
1243 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1247 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1251 /***********************************************************************
1252 * InvalidateRgn (USER32.@)
1254 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1258 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1262 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1266 /***********************************************************************
1267 * InvalidateRect (USER32.@)
1269 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1270 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1272 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1274 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1278 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1282 return RedrawWindow( hwnd, rect, 0, flags );
1286 /***********************************************************************
1287 * ValidateRgn (USER32.@)
1289 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1293 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1297 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1301 /***********************************************************************
1302 * ValidateRect (USER32.@)
1304 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1305 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1307 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1309 UINT flags = RDW_VALIDATE;
1313 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1317 return RedrawWindow( hwnd, rect, 0, flags );
1321 /***********************************************************************
1322 * GetUpdateRgn (USER32.@)
1324 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1327 UINT flags = UPDATE_NOCHILDREN;
1330 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1332 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1334 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1335 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1337 flags = UPDATE_DELAYED_ERASE;
1338 get_update_flags( hwnd, NULL, &flags );
1340 /* map region to client coordinates */
1341 map_window_region( 0, hwnd, hrgn );
1347 /***********************************************************************
1348 * GetUpdateRect (USER32.@)
1350 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1352 UINT flags = UPDATE_NOCHILDREN;
1356 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1358 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1362 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1364 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1365 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1366 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1367 DPtoLP( hdc, (LPPOINT)rect, 2 );
1368 SetLayout( hdc, layout );
1369 ReleaseDC( hwnd, hdc );
1372 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1374 /* check if we still have an update region */
1375 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1376 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1377 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1381 /***********************************************************************
1382 * ExcludeUpdateRgn (USER32.@)
1384 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1386 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1387 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1393 GetDCOrgEx( hdc, &pt );
1394 MapWindowPoints( 0, hwnd, &pt, 1 );
1395 OffsetRgn( update_rgn, -pt.x, -pt.y );
1396 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1397 DeleteObject( update_rgn );
1403 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1404 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1406 INT retVal = NULLREGION;
1407 BOOL bOwnRgn = TRUE;
1408 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1411 HRGN hrgnWinupd = 0;
1414 HWND hwndCaret = NULL;
1415 BOOL moveCaret = FALSE;
1418 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1419 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1420 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1421 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1422 FIXME("some flags (%04x) are unhandled\n", flags);
1424 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1425 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1427 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1428 hwnd = WIN_GetFullHandle( hwnd );
1430 GetClientRect(hwnd, &rc);
1432 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1435 if (rect) IntersectRect(&rc, &rc, rect);
1437 if( hrgnUpdate ) bOwnRgn = FALSE;
1438 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1440 newCaretPos.x = newCaretPos.y = 0;
1442 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1444 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1446 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1448 if (is_ex) dcxflags |= DCX_CACHE;
1449 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1450 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1451 dcxflags |= DCX_PARENTCLIP;
1452 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1453 dcxflags |= DCX_CLIPCHILDREN;
1454 hDC = GetDCEx( hwnd, 0, dcxflags);
1457 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1459 ReleaseDC( hwnd, hDC );
1462 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1465 /* If the windows has an update region, this must be
1466 * scrolled as well. Keep a copy in hrgnWinupd
1467 * to be added to hrngUpdate at the end. */
1468 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1469 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1470 if (retVal != NULLREGION)
1472 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1474 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1475 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1477 OffsetRgn( hrgnTemp, dx, dy );
1478 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1480 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1481 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1483 /* Catch the case where the scrolling amount exceeds the size of the
1484 * original window. This generated a second update area that is the
1485 * location where the original scrolled content would end up.
1486 * This second region is not returned by the ScrollDC and sets
1487 * ScrollWindowEx apart from just a ScrollDC.
1489 * This has been verified with testing on windows.
1491 if (abs(dx) > abs(rc.right - rc.left) ||
1492 abs(dy) > abs(rc.bottom - rc.top))
1494 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1495 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1496 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1499 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1501 DeleteObject( hrgnClip );
1503 DeleteObject( hrgnTemp );
1505 /* nothing was scrolled */
1507 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1508 SetRectEmpty( rcUpdate);
1511 if( flags & SW_SCROLLCHILDREN )
1513 HWND *list = WIN_ListChildren( hwnd );
1518 for (i = 0; list[i]; i++)
1520 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1521 if (!rect || IntersectRect(&dummy, &r, rect))
1522 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1523 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1524 SWP_NOREDRAW | SWP_DEFERERASE );
1526 HeapFree( GetProcessHeap(), 0, list );
1530 if( flags & (SW_INVALIDATE | SW_ERASE) )
1531 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1532 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1535 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1536 DeleteObject( hrgnWinupd);
1540 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1541 ShowCaret(hwndCaret);
1544 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1550 /*************************************************************************
1551 * ScrollWindowEx (USER32.@)
1553 * Note: contrary to what the doc says, pixels that are scrolled from the
1554 * outside of clipRect to the inside are NOT painted.
1557 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1558 const RECT *rect, const RECT *clipRect,
1559 HRGN hrgnUpdate, LPRECT rcUpdate,
1562 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1565 /*************************************************************************
1566 * ScrollWindow (USER32.@)
1569 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1570 const RECT *rect, const RECT *clipRect )
1572 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1573 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1577 /*************************************************************************
1578 * ScrollDC (USER32.@)
1580 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1581 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1582 * logical coordinates.
1584 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
1585 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
1588 return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
1591 /************************************************************************
1592 * PrintWindow (USER32.@)
1595 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1597 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1598 if(!(nFlags & PW_CLIENTONLY))
1600 flags |= PRF_NONCLIENT;
1602 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);