d3d11/tests: Add some tests for ID3DUserDefinedAnnotation.
[wine.git] / dlls / user32 / painting.c
blob63a8e8409989b0ee37fa2aa6cdd75aff954bdf00
1 /*
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
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <string.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "user_private.h"
29 #include "win.h"
30 #include "controls.h"
31 #include "wine/server.h"
32 #include "wine/list.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(win);
38 struct dce
40 struct list entry; /* entry in global DCE list */
41 HDC hdc;
42 HWND hwnd;
43 HRGN clip_rgn;
44 DWORD flags;
45 LONG count; /* usage count; 0 or 1 for cache DCEs, always 1 for window DCEs,
46 always >= 1 for class DCEs */
49 static struct list dce_list = LIST_INIT(dce_list);
51 #define DCE_CACHE_SIZE 64
53 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
56 /***********************************************************************
57 * dump_rdw_flags
59 static void dump_rdw_flags(UINT flags)
61 TRACE("flags:");
62 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
63 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
64 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
65 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
66 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
67 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
68 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
69 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
70 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
71 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
72 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
73 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
75 #define RDW_FLAGS \
76 (RDW_INVALIDATE | \
77 RDW_INTERNALPAINT | \
78 RDW_ERASE | \
79 RDW_VALIDATE | \
80 RDW_NOINTERNALPAINT | \
81 RDW_NOERASE | \
82 RDW_NOCHILDREN | \
83 RDW_ALLCHILDREN | \
84 RDW_UPDATENOW | \
85 RDW_ERASENOW | \
86 RDW_FRAME | \
87 RDW_NOFRAME)
89 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
90 TRACE("\n");
91 #undef RDW_FLAGS
95 /***********************************************************************
96 * update_visible_region
98 * Set the visible region and X11 drawable for the DC associated to
99 * a given window.
101 static void update_visible_region( struct dce *dce )
103 struct window_surface *surface = NULL;
104 NTSTATUS status;
105 HRGN vis_rgn = 0;
106 HWND top_win = 0;
107 DWORD flags = dce->flags;
108 DWORD paint_flags = 0;
109 size_t size = 256;
110 RECT win_rect, top_rect;
111 WND *win;
113 /* don't clip siblings if using parent clip region */
114 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
116 /* fetch the visible region from the server */
119 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
120 if (!data) return;
122 SERVER_START_REQ( get_visible_region )
124 req->window = wine_server_user_handle( dce->hwnd );
125 req->flags = flags;
126 wine_server_set_reply( req, data->Buffer, size );
127 if (!(status = wine_server_call( req )))
129 size_t reply_size = wine_server_reply_size( reply );
130 data->rdh.dwSize = sizeof(data->rdh);
131 data->rdh.iType = RDH_RECTANGLES;
132 data->rdh.nCount = reply_size / sizeof(RECT);
133 data->rdh.nRgnSize = reply_size;
134 vis_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
136 top_win = wine_server_ptr_handle( reply->top_win );
137 win_rect.left = reply->win_rect.left;
138 win_rect.top = reply->win_rect.top;
139 win_rect.right = reply->win_rect.right;
140 win_rect.bottom = reply->win_rect.bottom;
141 top_rect.left = reply->top_rect.left;
142 top_rect.top = reply->top_rect.top;
143 top_rect.right = reply->top_rect.right;
144 top_rect.bottom = reply->top_rect.bottom;
145 paint_flags = reply->paint_flags;
147 else size = reply->total_size;
149 SERVER_END_REQ;
150 HeapFree( GetProcessHeap(), 0, data );
151 } while (status == STATUS_BUFFER_OVERFLOW);
153 if (status || !vis_rgn) return;
155 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
157 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
158 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
160 /* don't use a surface to paint the client area of OpenGL windows */
161 if (!(paint_flags & SET_WINPOS_PIXEL_FORMAT) || (flags & DCX_WINDOW))
163 win = WIN_GetPtr( top_win );
164 if (win && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
166 surface = win->surface;
167 if (surface) window_surface_add_ref( surface );
168 WIN_ReleasePtr( win );
172 if (!surface) SetRectEmpty( &top_rect );
173 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
174 if (surface) window_surface_release( surface );
178 /***********************************************************************
179 * release_dce
181 static void release_dce( struct dce *dce )
183 if (!dce->hwnd) return; /* already released */
185 __wine_set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
186 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
188 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
189 dce->clip_rgn = 0;
190 dce->hwnd = 0;
191 dce->flags &= DCX_CACHE;
195 /***********************************************************************
196 * delete_clip_rgn
198 static void delete_clip_rgn( struct dce *dce )
200 if (!dce->clip_rgn) return; /* nothing to do */
202 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
203 DeleteObject( dce->clip_rgn );
204 dce->clip_rgn = 0;
206 /* make it dirty so that the vis rgn gets recomputed next time */
207 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
211 /***********************************************************************
212 * alloc_dce
214 * Allocate a new DCE.
216 static struct dce *alloc_dce(void)
218 struct dce *dce;
220 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
221 if (!(dce->hdc = CreateDCW( L"DISPLAY", NULL, NULL, NULL )))
223 HeapFree( GetProcessHeap(), 0, dce );
224 return 0;
226 dce->hwnd = 0;
227 dce->clip_rgn = 0;
228 dce->flags = 0;
229 dce->count = 1;
231 /* store DCE handle in DC hook data field */
232 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
233 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
234 return dce;
238 /***********************************************************************
239 * get_window_dce
241 static struct dce *get_window_dce( HWND hwnd )
243 struct dce *dce;
244 WND *win = WIN_GetPtr( hwnd );
246 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
248 dce = win->dce;
249 if (!dce && (dce = get_class_dce( win->class )))
251 win->dce = dce;
252 dce->count++;
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 */
270 dce_to_free = dce;
271 dce = win->dce;
273 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
275 dce_to_free = dce;
276 dce = win->dce;
277 dce->count++;
279 else
281 dce->count++;
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 */
299 dce_to_free = dce;
300 dce = win->dce;
302 else
304 win->dce = dce;
305 dce->hwnd = hwnd;
306 list_add_tail( &dce_list, &dce->entry );
308 WIN_ReleasePtr( win );
310 else dce_to_free = dce;
313 if (dce_to_free)
315 SetDCHook( dce_to_free->hdc, NULL, 0 );
316 DeleteDC( dce_to_free->hdc );
317 HeapFree( GetProcessHeap(), 0, dce_to_free );
318 if (dce_to_free == dce)
319 dce = NULL;
322 return dce;
326 /***********************************************************************
327 * free_dce
329 * Free a class or window DCE.
331 void free_dce( struct dce *dce, HWND hwnd )
333 struct dce *dce_to_free = NULL;
335 USER_Lock();
337 if (dce)
339 if (!--dce->count)
341 release_dce( dce );
342 list_remove( &dce->entry );
343 dce_to_free = dce;
345 else if (dce->hwnd == hwnd)
347 release_dce( dce );
351 /* now check for cache DCEs */
353 if (hwnd)
355 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
357 if (dce->hwnd != hwnd) continue;
358 if (!(dce->flags & DCX_CACHE)) break;
360 release_dce( dce );
361 if (dce->count)
363 WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
364 dce->count = 0;
365 SetHookFlags( dce->hdc, DCHF_DISABLEDC );
370 USER_Unlock();
372 if (dce_to_free)
374 SetDCHook( dce_to_free->hdc, NULL, 0 );
375 DeleteDC( dce_to_free->hdc );
376 HeapFree( GetProcessHeap(), 0, dce_to_free );
381 /***********************************************************************
382 * make_dc_dirty
384 * Mark the associated DC as dirty to force a refresh of the visible region
386 static void make_dc_dirty( struct dce *dce )
388 if (!dce->count)
390 /* Don't bother with visible regions of unused DCEs */
391 TRACE("purged %p hwnd %p\n", dce->hdc, dce->hwnd);
392 release_dce( dce );
394 else
396 /* Set dirty bits in the hDC and DCE structs */
397 TRACE("fixed up %p hwnd %p\n", dce->hdc, dce->hwnd);
398 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
403 /***********************************************************************
404 * invalidate_dce
406 * It is called from SetWindowPos() - we have to
407 * mark as dirty all busy DCEs for windows that have pWnd->parent as
408 * an ancestor and whose client rect intersects with specified update
409 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
410 * DCX_CLIPCHILDREN flag is set.
412 void invalidate_dce( WND *win, const RECT *extra_rect )
414 DPI_AWARENESS_CONTEXT context;
415 RECT window_rect;
416 struct dce *dce;
418 if (!win->parent) return;
420 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( win->obj.handle ));
422 GetWindowRect( win->obj.handle, &window_rect );
424 TRACE("%p parent %p %s (%s)\n",
425 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
427 /* walk all DCEs and fixup non-empty entries */
429 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
431 if (!dce->hwnd) continue;
433 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce->hdc, dce->hwnd, dce->flags,
434 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
436 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
437 continue; /* child window positions don't bother us */
439 /* if DCE window is a child of hwnd, it has to be invalidated */
440 if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
442 make_dc_dirty( dce );
443 continue;
446 /* otherwise check if the window rectangle intersects this DCE window */
447 if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
449 RECT dce_rect, tmp;
450 GetWindowRect( dce->hwnd, &dce_rect );
451 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
452 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
453 make_dc_dirty( dce );
456 SetThreadDpiAwarenessContext( context );
459 /***********************************************************************
460 * release_dc
462 * Implementation of ReleaseDC.
464 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
466 struct dce *dce;
467 BOOL ret = FALSE;
469 TRACE("%p %p\n", hwnd, hdc );
471 USER_Lock();
472 dce = (struct dce *)GetDCHook( hdc, NULL );
473 if (dce && dce->count && dce->hwnd)
475 if (!(dce->flags & DCX_NORESETATTRS)) SetHookFlags( dce->hdc, DCHF_RESETDC );
476 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
477 if (dce->flags & DCX_CACHE)
479 dce->count = 0;
480 SetHookFlags( dce->hdc, DCHF_DISABLEDC );
482 ret = TRUE;
484 USER_Unlock();
485 return ret;
489 /***********************************************************************
490 * dc_hook
492 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
494 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
496 BOOL retv = TRUE;
497 struct dce *dce = (struct dce *)data;
499 TRACE("hDC = %p, %u\n", hDC, code);
501 if (!dce) return FALSE;
502 assert( dce->hdc == hDC );
504 switch( code )
506 case DCHC_INVALIDVISRGN:
507 /* GDI code calls this when it detects that the
508 * DC is dirty (usually after SetHookFlags()). This
509 * means that we have to recompute the visible region.
511 if (dce->count) update_visible_region( dce );
512 else /* non-fatal but shouldn't happen */
513 WARN("DC is not in use!\n");
514 break;
515 case DCHC_DELETEDC:
516 USER_Lock();
517 if (!(dce->flags & DCX_CACHE))
519 WARN("Application trying to delete an owned DC %p\n", dce->hdc);
520 retv = FALSE;
522 else
524 list_remove( &dce->entry );
525 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
526 HeapFree( GetProcessHeap(), 0, dce );
528 USER_Unlock();
529 break;
531 return retv;
535 /***********************************************************************
536 * get_update_region
538 * Return update region (in screen coordinates) for a window.
540 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
542 HRGN hrgn = 0;
543 NTSTATUS status;
544 RGNDATA *data;
545 size_t size = 256;
549 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
551 SetLastError( ERROR_OUTOFMEMORY );
552 return 0;
555 SERVER_START_REQ( get_update_region )
557 req->window = wine_server_user_handle( hwnd );
558 req->from_child = wine_server_user_handle( child ? *child : 0 );
559 req->flags = *flags;
560 wine_server_set_reply( req, data->Buffer, size );
561 if (!(status = wine_server_call( req )))
563 size_t reply_size = wine_server_reply_size( reply );
564 data->rdh.dwSize = sizeof(data->rdh);
565 data->rdh.iType = RDH_RECTANGLES;
566 data->rdh.nCount = reply_size / sizeof(RECT);
567 data->rdh.nRgnSize = reply_size;
568 hrgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
569 if (child) *child = wine_server_ptr_handle( reply->child );
570 *flags = reply->flags;
572 else size = reply->total_size;
574 SERVER_END_REQ;
575 HeapFree( GetProcessHeap(), 0, data );
576 } while (status == STATUS_BUFFER_OVERFLOW);
578 if (status) SetLastError( RtlNtStatusToDosError(status) );
579 return hrgn;
583 /***********************************************************************
584 * get_update_flags
586 * Get only the update flags, not the update region.
588 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
590 BOOL ret;
592 SERVER_START_REQ( get_update_region )
594 req->window = wine_server_user_handle( hwnd );
595 req->from_child = wine_server_user_handle( child ? *child : 0 );
596 req->flags = *flags | UPDATE_NOREGION;
597 if ((ret = !wine_server_call_err( req )))
599 if (child) *child = wine_server_ptr_handle( reply->child );
600 *flags = reply->flags;
603 SERVER_END_REQ;
604 return ret;
608 /***********************************************************************
609 * redraw_window_rects
611 * Redraw part of a window.
613 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
615 BOOL ret;
617 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
618 return TRUE; /* nothing to do */
620 SERVER_START_REQ( redraw_window )
622 req->window = wine_server_user_handle( hwnd );
623 req->flags = flags;
624 wine_server_add_data( req, rects, count * sizeof(RECT) );
625 ret = !wine_server_call_err( req );
627 SERVER_END_REQ;
628 return ret;
632 /***********************************************************************
633 * send_ncpaint
635 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
636 * Helper for erase_now and BeginPaint.
638 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
640 HRGN whole_rgn = get_update_region( hwnd, flags, child );
641 HRGN client_rgn = 0;
642 DWORD style;
644 if (child) hwnd = *child;
646 if (hwnd == GetDesktopWindow()) return whole_rgn;
648 if (whole_rgn)
650 DPI_AWARENESS_CONTEXT context;
651 RECT client, window, update;
652 INT type;
654 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
656 /* check if update rgn overlaps with nonclient area */
657 type = GetRgnBox( whole_rgn, &update );
658 WIN_GetRectangles( hwnd, COORDS_SCREEN, &window, &client );
660 if ((*flags & UPDATE_NONCLIENT) ||
661 update.left < client.left || update.top < client.top ||
662 update.right > client.right || update.bottom > client.bottom)
664 client_rgn = CreateRectRgnIndirect( &client );
665 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
667 /* check if update rgn contains complete nonclient area */
668 if (type == SIMPLEREGION && EqualRect( &window, &update ))
670 DeleteObject( whole_rgn );
671 whole_rgn = (HRGN)1;
674 else
676 client_rgn = whole_rgn;
677 whole_rgn = 0;
680 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
682 if (*flags & UPDATE_NONCLIENT)
684 /* Mark standard scroll bars as not painted before sending WM_NCPAINT */
685 style = GetWindowLongW( hwnd, GWL_STYLE );
686 if (style & WS_HSCROLL)
687 SCROLL_SetStandardScrollPainted( hwnd, SB_HORZ, FALSE );
688 if (style & WS_VSCROLL)
689 SCROLL_SetStandardScrollPainted( hwnd, SB_VERT, FALSE );
691 SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
693 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
695 SetThreadDpiAwarenessContext( context );
697 return client_rgn;
701 /***********************************************************************
702 * send_erase
704 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
705 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
706 * Helper for erase_now and BeginPaint.
708 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
709 RECT *clip_rect, HDC *hdc_ret )
711 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
712 HDC hdc = 0;
713 RECT dummy;
715 if (!clip_rect) clip_rect = &dummy;
716 if (hdc_ret || (flags & UPDATE_ERASE))
718 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
719 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
721 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
723 INT type = GetClipBox( hdc, clip_rect );
725 if (flags & UPDATE_ERASE)
727 /* don't erase if the clip box is empty */
728 if (type != NULLREGION)
729 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
731 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
734 if (hdc_ret) *hdc_ret = hdc;
736 if (!hdc) DeleteObject( client_rgn );
737 return need_erase;
741 /***********************************************************************
742 * erase_now
744 * Implementation of RDW_ERASENOW behavior.
746 void erase_now( HWND hwnd, UINT rdw_flags )
748 HWND child = 0;
749 HRGN hrgn;
750 BOOL need_erase = FALSE;
752 /* loop while we find a child to repaint */
753 for (;;)
755 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
757 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
758 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
759 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
761 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
762 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
764 if (!flags) break; /* nothing more to do */
765 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
770 /***********************************************************************
771 * copy_bits_from_surface
773 * Copy bits from a window surface; helper for move_window_bits and move_window_bits_parent.
775 static void copy_bits_from_surface( HWND hwnd, struct window_surface *surface,
776 const RECT *dst, const RECT *src )
778 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
779 BITMAPINFO *info = (BITMAPINFO *)buffer;
780 void *bits;
781 UINT flags = UPDATE_NOCHILDREN | UPDATE_CLIPCHILDREN;
782 HRGN rgn = get_update_region( hwnd, &flags, NULL );
783 HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_WINDOW | DCX_EXCLUDERGN );
785 bits = surface->funcs->get_info( surface, info );
786 surface->funcs->lock( surface );
787 SetDIBitsToDevice( hdc, dst->left, dst->top, dst->right - dst->left, dst->bottom - dst->top,
788 src->left - surface->rect.left, surface->rect.bottom - src->bottom,
789 0, surface->rect.bottom - surface->rect.top,
790 bits, info, DIB_RGB_COLORS );
791 surface->funcs->unlock( surface );
792 ReleaseDC( hwnd, hdc );
796 /***********************************************************************
797 * move_window_bits
799 * Move the window bits when a window is resized or its surface recreated.
801 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
802 struct window_surface *new_surface,
803 const RECT *visible_rect, const RECT *old_visible_rect,
804 const RECT *window_rect, const RECT *valid_rects )
806 RECT dst = valid_rects[0];
807 RECT src = valid_rects[1];
809 if (new_surface != old_surface ||
810 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
811 src.top - old_visible_rect->top != dst.top - visible_rect->top)
813 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
814 OffsetRect( &src, -old_visible_rect->left, -old_visible_rect->top );
815 OffsetRect( &dst, -window_rect->left, -window_rect->top );
816 copy_bits_from_surface( hwnd, old_surface, &dst, &src );
821 /***********************************************************************
822 * move_window_bits_parent
824 * Move the window bits in the parent surface when a child is moved.
826 void move_window_bits_parent( HWND hwnd, HWND parent, const RECT *window_rect, const RECT *valid_rects )
828 struct window_surface *surface;
829 RECT dst = valid_rects[0];
830 RECT src = valid_rects[1];
831 WND *win;
833 if (src.left == dst.left && src.top == dst.top) return;
835 if (!(win = WIN_GetPtr( parent ))) return;
836 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
837 if (!(surface = win->surface))
839 WIN_ReleasePtr( win );
840 return;
843 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
844 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), parent, (POINT *)&src, 2 );
845 OffsetRect( &src, win->client_rect.left - win->visible_rect.left,
846 win->client_rect.top - win->visible_rect.top );
847 OffsetRect( &dst, -window_rect->left, -window_rect->top );
848 window_surface_add_ref( surface );
849 WIN_ReleasePtr( win );
851 copy_bits_from_surface( hwnd, surface, &dst, &src );
852 window_surface_release( surface );
856 /***********************************************************************
857 * update_now
859 * Implementation of RDW_UPDATENOW behavior.
861 static void update_now( HWND hwnd, UINT rdw_flags )
863 HWND child = 0;
865 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
866 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
868 /* loop while we find a child to repaint */
869 for (;;)
871 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
873 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
874 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
876 if (!get_update_flags( hwnd, &child, &flags )) break;
877 if (!flags) break; /* nothing more to do */
879 SendMessageW( child, WM_PAINT, 0, 0 );
880 if (rdw_flags & RDW_NOCHILDREN) break;
885 /*************************************************************************
886 * fix_caret
888 * Helper for ScrollWindowEx:
889 * If the return value is 0, no special caret handling is necessary.
890 * Otherwise the return value is the handle of the window that owns the
891 * caret. Its caret needs to be hidden during the scroll operation and
892 * moved to new_caret_pos if move_caret is TRUE.
894 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
895 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
897 GUITHREADINFO info;
898 RECT rect, mapped_rcCaret;
900 info.cbSize = sizeof(info);
901 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
902 if (!info.hwndCaret) return 0;
904 mapped_rcCaret = info.rcCaret;
905 if (info.hwndCaret == hWnd)
907 /* The caret needs to be moved along with scrolling even if it's
908 * outside the visible area. Otherwise, when the caret is scrolled
909 * out from the view, the position won't get updated anymore and
910 * the caret will never scroll back again. */
911 *move_caret = TRUE;
912 new_caret_pos->x = info.rcCaret.left + dx;
913 new_caret_pos->y = info.rcCaret.top + dy;
915 else
917 *move_caret = FALSE;
918 if (!(flags & SW_SCROLLCHILDREN) || !IsChild(hWnd, info.hwndCaret))
919 return 0;
920 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
923 /* If the caret is not in the src/dest rects, all is fine done. */
924 if (!IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
926 rect = *scroll_rect;
927 OffsetRect(&rect, dx, dy);
928 if (!IntersectRect(&rect, &rect, &mapped_rcCaret))
929 return 0;
932 /* Indicate that the caret needs to be updated during the scrolling. */
933 return info.hwndCaret;
937 /***********************************************************************
938 * BeginPaint (USER32.@)
940 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
942 HRGN hrgn;
943 HDC hdc;
944 BOOL erase;
945 RECT rect;
946 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
948 HideCaret( hwnd );
950 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
952 erase = send_erase( hwnd, flags, hrgn, &rect, &hdc );
954 TRACE("hdc = %p box = (%s), fErase = %d\n", hdc, wine_dbgstr_rect(&rect), erase);
956 if (!lps)
958 release_dc( hwnd, hdc, TRUE );
959 return 0;
961 lps->fErase = erase;
962 lps->rcPaint = rect;
963 lps->hdc = hdc;
964 return hdc;
968 /***********************************************************************
969 * EndPaint (USER32.@)
971 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
973 ShowCaret( hwnd );
974 flush_window_surfaces( FALSE );
975 if (!lps) return FALSE;
976 release_dc( hwnd, lps->hdc, TRUE );
977 return TRUE;
981 /***********************************************************************
982 * GetDCEx (USER32.@)
984 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
986 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
987 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
988 struct dce *dce;
989 BOOL bUpdateVisRgn = TRUE;
990 HWND parent;
991 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
993 if (!hwnd) hwnd = GetDesktopWindow();
994 else hwnd = WIN_GetFullHandle( hwnd );
996 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
998 if (!IsWindow(hwnd)) return 0;
1000 /* fixup flags */
1002 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
1004 if (flags & DCX_USESTYLE)
1006 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
1008 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
1010 if (!(flags & DCX_WINDOW))
1012 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
1014 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
1015 flags |= DCX_CLIPCHILDREN;
1019 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
1021 parent = GetAncestor( hwnd, GA_PARENT );
1022 if (!parent || (parent == GetDesktopWindow()))
1023 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
1025 /* it seems parent clip is ignored when clipping siblings or children */
1026 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
1028 if( flags & DCX_PARENTCLIP )
1030 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
1031 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
1033 flags &= ~DCX_CLIPCHILDREN;
1034 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
1038 /* find a suitable DCE */
1040 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
1042 struct dce *dceEmpty = NULL, *dceUnused = NULL, *found = NULL;
1043 unsigned int count = 0;
1045 /* Strategy: First, we attempt to find a non-empty but unused DCE with
1046 * compatible flags. Next, we look for an empty entry. If the cache is
1047 * full we have to purge one of the unused entries.
1049 USER_Lock();
1050 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1052 if (!(dce->flags & DCX_CACHE)) break;
1053 count++;
1054 if (dce->count) continue;
1055 dceUnused = dce;
1056 if (!dce->hwnd) dceEmpty = dce;
1057 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1059 TRACE( "found valid %p hwnd %p, flags %08x\n", dce->hdc, hwnd, dce->flags );
1060 found = dce;
1061 bUpdateVisRgn = FALSE;
1062 break;
1065 if (!found) found = dceEmpty;
1066 if (!found && count >= DCE_CACHE_SIZE) found = dceUnused;
1068 dce = found;
1069 if (dce)
1071 dce->count = 1;
1072 SetHookFlags( dce->hdc, DCHF_ENABLEDC );
1074 USER_Unlock();
1076 /* if there's no dce empty or unused, allocate a new one */
1077 if (!dce)
1079 if (!(dce = alloc_dce())) return 0;
1080 dce->flags = DCX_CACHE;
1081 USER_Lock();
1082 list_add_head( &dce_list, &dce->entry );
1083 USER_Unlock();
1086 else
1088 flags |= DCX_NORESETATTRS;
1089 if (dce->hwnd != hwnd)
1091 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1092 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1093 dce->clip_rgn = 0;
1095 else bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1098 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1100 /* if the extra clip region has changed, get rid of the old one */
1101 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1102 delete_clip_rgn( dce );
1103 dce->clip_rgn = hrgnClip;
1104 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1105 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1106 bUpdateVisRgn = TRUE;
1109 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1111 dce->hwnd = hwnd;
1112 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1114 /* cross-process invalidation is not supported yet, so always update the vis rgn */
1115 if (!WIN_IsCurrentProcess( hwnd )) bUpdateVisRgn = TRUE;
1117 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1119 if (bUpdateVisRgn) update_visible_region( dce );
1121 TRACE("(%p,%p,0x%x): returning %p%s\n", hwnd, hrgnClip, flags, dce->hdc,
1122 bUpdateVisRgn ? " (updated)" : "");
1123 return dce->hdc;
1127 /***********************************************************************
1128 * GetDC (USER32.@)
1130 * Get a device context.
1132 * RETURNS
1133 * Success: Handle to the device context
1134 * Failure: NULL.
1136 HDC WINAPI GetDC( HWND hwnd )
1138 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1139 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1143 /***********************************************************************
1144 * GetWindowDC (USER32.@)
1146 HDC WINAPI GetWindowDC( HWND hwnd )
1148 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1152 /***********************************************************************
1153 * ReleaseDC (USER32.@)
1155 * Release a device context.
1157 * RETURNS
1158 * Success: Non-zero. Resources used by hdc are released.
1159 * Failure: 0.
1161 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1163 return release_dc( hwnd, hdc, FALSE );
1167 /**********************************************************************
1168 * WindowFromDC (USER32.@)
1170 HWND WINAPI WindowFromDC( HDC hdc )
1172 struct dce *dce;
1173 HWND hwnd = 0;
1175 USER_Lock();
1176 dce = (struct dce *)GetDCHook( hdc, NULL );
1177 if (dce) hwnd = dce->hwnd;
1178 USER_Unlock();
1179 return hwnd;
1183 /***********************************************************************
1184 * LockWindowUpdate (USER32.@)
1186 * Enables or disables painting in the chosen window.
1188 * PARAMS
1189 * hwnd [I] handle to a window.
1191 * RETURNS
1192 * If successful, returns nonzero value. Otherwise,
1193 * returns 0.
1195 * NOTES
1196 * You can lock only one window at a time.
1198 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1200 static HWND lockedWnd;
1202 FIXME("(%p), partial stub!\n",hwnd);
1204 USER_Lock();
1205 if (lockedWnd)
1207 if (!hwnd)
1209 /* Unlock lockedWnd */
1210 /* FIXME: Do something */
1212 else
1214 /* Attempted to lock a second window */
1215 /* Return FALSE and do nothing */
1216 USER_Unlock();
1217 return FALSE;
1220 lockedWnd = hwnd;
1221 USER_Unlock();
1222 return TRUE;
1226 /***********************************************************************
1227 * RedrawWindow (USER32.@)
1229 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1231 static const RECT empty;
1232 BOOL ret;
1234 if (TRACE_ON(win))
1236 if (hrgn)
1238 RECT r;
1239 GetRgnBox( hrgn, &r );
1240 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1242 else if (rect)
1243 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1244 else
1245 TRACE( "%p whole window ", hwnd );
1247 dump_rdw_flags(flags);
1250 /* process pending expose events before painting */
1251 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1253 if (rect && !hrgn)
1255 if (IsRectEmpty( rect )) rect = &empty;
1256 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1258 else if (!hrgn)
1260 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1262 else /* need to build a list of the region rectangles */
1264 DWORD size;
1265 RGNDATA *data;
1267 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1268 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1269 GetRegionData( hrgn, size, data );
1270 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1271 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1272 else
1273 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1274 HeapFree( GetProcessHeap(), 0, data );
1277 if (!hwnd) hwnd = GetDesktopWindow();
1279 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1280 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1282 return ret;
1286 /***********************************************************************
1287 * UpdateWindow (USER32.@)
1289 BOOL WINAPI UpdateWindow( HWND hwnd )
1291 if (!hwnd)
1293 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1294 return FALSE;
1297 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1301 /***********************************************************************
1302 * InvalidateRgn (USER32.@)
1304 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1306 if (!hwnd)
1308 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1309 return FALSE;
1312 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1316 /***********************************************************************
1317 * InvalidateRect (USER32.@)
1319 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1320 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1322 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1324 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1326 if (!hwnd)
1328 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1329 rect = NULL;
1332 return RedrawWindow( hwnd, rect, 0, flags );
1336 /***********************************************************************
1337 * ValidateRgn (USER32.@)
1339 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1341 if (!hwnd)
1343 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1344 return FALSE;
1347 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1351 /***********************************************************************
1352 * ValidateRect (USER32.@)
1354 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1355 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1357 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1359 UINT flags = RDW_VALIDATE;
1361 if (!hwnd)
1363 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1364 rect = NULL;
1367 return RedrawWindow( hwnd, rect, 0, flags );
1371 /***********************************************************************
1372 * GetUpdateRgn (USER32.@)
1374 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1376 DPI_AWARENESS_CONTEXT context;
1377 INT retval = ERROR;
1378 UINT flags = UPDATE_NOCHILDREN;
1379 HRGN update_rgn;
1381 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1383 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1385 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1387 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1388 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1390 flags = UPDATE_DELAYED_ERASE;
1391 get_update_flags( hwnd, NULL, &flags );
1393 /* map region to client coordinates */
1394 map_window_region( 0, hwnd, hrgn );
1396 SetThreadDpiAwarenessContext( context );
1397 return retval;
1401 /***********************************************************************
1402 * GetUpdateRect (USER32.@)
1404 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1406 DPI_AWARENESS_CONTEXT context;
1407 UINT flags = UPDATE_NOCHILDREN;
1408 HRGN update_rgn;
1409 BOOL need_erase;
1411 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1413 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1415 if (rect)
1417 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1419 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1420 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1421 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1422 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1423 SetThreadDpiAwarenessContext( context );
1424 *rect = rect_win_to_thread_dpi( hwnd, *rect );
1425 DPtoLP( hdc, (LPPOINT)rect, 2 );
1426 SetLayout( hdc, layout );
1427 ReleaseDC( hwnd, hdc );
1430 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1432 /* check if we still have an update region */
1433 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1434 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1435 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1439 /***********************************************************************
1440 * ExcludeUpdateRgn (USER32.@)
1442 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1444 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1445 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1447 if (ret != ERROR)
1449 DPI_AWARENESS_CONTEXT context;
1450 POINT pt;
1452 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1453 GetDCOrgEx( hdc, &pt );
1454 MapWindowPoints( 0, hwnd, &pt, 1 );
1455 OffsetRgn( update_rgn, -pt.x, -pt.y );
1456 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1457 SetThreadDpiAwarenessContext( context );
1459 DeleteObject( update_rgn );
1460 return ret;
1464 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1465 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1467 INT retVal = NULLREGION;
1468 BOOL bOwnRgn = TRUE;
1469 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1470 int rdw_flags;
1471 HRGN hrgnTemp;
1472 HRGN hrgnWinupd = 0;
1473 HDC hDC;
1474 RECT rc, cliprc;
1475 HWND hwndCaret = NULL;
1476 BOOL moveCaret = FALSE;
1477 POINT newCaretPos;
1479 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1480 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1481 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1482 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1483 FIXME("some flags (%04x) are unhandled\n", flags);
1485 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1486 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1488 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1489 hwnd = WIN_GetFullHandle( hwnd );
1491 GetClientRect(hwnd, &rc);
1493 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1494 else cliprc = rc;
1496 if (rect) IntersectRect(&rc, &rc, rect);
1498 if( hrgnUpdate ) bOwnRgn = FALSE;
1499 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1501 newCaretPos.x = newCaretPos.y = 0;
1503 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1504 DWORD dcxflags = 0;
1505 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1507 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1508 if (hwndCaret)
1509 HideCaret(hwndCaret);
1511 if (is_ex) dcxflags |= DCX_CACHE;
1512 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1513 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1514 dcxflags |= DCX_PARENTCLIP;
1515 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1516 dcxflags |= DCX_CLIPCHILDREN;
1517 hDC = GetDCEx( hwnd, 0, dcxflags);
1518 if (hDC)
1520 NtUserScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1522 ReleaseDC( hwnd, hDC );
1524 if (!bUpdate)
1525 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1528 /* If the windows has an update region, this must be
1529 * scrolled as well. Keep a copy in hrgnWinupd
1530 * to be added to hrngUpdate at the end. */
1531 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1532 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1533 if (retVal != NULLREGION)
1535 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1536 if( !bOwnRgn) {
1537 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1538 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1540 OffsetRgn( hrgnTemp, dx, dy );
1541 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1542 if( !bOwnRgn)
1543 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1544 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1546 /* Catch the case where the scrolling amount exceeds the size of the
1547 * original window. This generated a second update area that is the
1548 * location where the original scrolled content would end up.
1549 * This second region is not returned by the ScrollDC and sets
1550 * ScrollWindowEx apart from just a ScrollDC.
1552 * This has been verified with testing on windows.
1554 if (abs(dx) > abs(rc.right - rc.left) ||
1555 abs(dy) > abs(rc.bottom - rc.top))
1557 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1558 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1559 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1561 if (rcUpdate)
1563 RECT rcTemp;
1564 GetRgnBox( hrgnTemp, &rcTemp );
1565 UnionRect( rcUpdate, rcUpdate, &rcTemp );
1568 if( !bOwnRgn)
1569 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1571 DeleteObject( hrgnClip );
1573 DeleteObject( hrgnTemp );
1574 } else {
1575 /* nothing was scrolled */
1576 if( !bOwnRgn)
1577 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1578 SetRectEmpty( rcUpdate);
1581 if( flags & SW_SCROLLCHILDREN )
1583 HWND *list = WIN_ListChildren( hwnd );
1584 if (list)
1586 int i;
1587 RECT r, dummy;
1588 for (i = 0; list[i]; i++)
1590 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1591 if (!rect || IntersectRect(&dummy, &r, rect))
1592 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1593 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1594 SWP_NOREDRAW | SWP_DEFERERASE );
1596 HeapFree( GetProcessHeap(), 0, list );
1600 if( flags & (SW_INVALIDATE | SW_ERASE) )
1601 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1602 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1604 if( hrgnWinupd) {
1605 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1606 DeleteObject( hrgnWinupd);
1609 if( moveCaret )
1610 SetCaretPos( newCaretPos.x, newCaretPos.y );
1611 if( hwndCaret )
1612 ShowCaret( hwndCaret );
1614 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1616 return retVal;
1620 /*************************************************************************
1621 * ScrollWindowEx (USER32.@)
1623 * Note: contrary to what the doc says, pixels that are scrolled from the
1624 * outside of clipRect to the inside are NOT painted.
1627 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1628 const RECT *rect, const RECT *clipRect,
1629 HRGN hrgnUpdate, LPRECT rcUpdate,
1630 UINT flags )
1632 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1635 /*************************************************************************
1636 * ScrollWindow (USER32.@)
1639 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1640 const RECT *rect, const RECT *clipRect )
1642 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1643 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1646 /************************************************************************
1647 * PrintWindow (USER32.@)
1650 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1652 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1653 if(!(nFlags & PW_CLIENTONLY))
1655 flags |= PRF_NONCLIENT;
1657 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1658 return TRUE;