d2d1: Create feature level 10.0 device context state objects.
[wine.git] / dlls / user32 / painting.c
blob41c0bb3c6e1ea9daa8b94e69324a9e522a1a183e
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;
643 if (child) hwnd = *child;
645 if (hwnd == GetDesktopWindow()) return whole_rgn;
647 if (whole_rgn)
649 DPI_AWARENESS_CONTEXT context;
650 RECT client, window, update;
651 INT type;
653 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
655 /* check if update rgn overlaps with nonclient area */
656 type = GetRgnBox( whole_rgn, &update );
657 WIN_GetRectangles( hwnd, COORDS_SCREEN, &window, &client );
659 if ((*flags & UPDATE_NONCLIENT) ||
660 update.left < client.left || update.top < client.top ||
661 update.right > client.right || update.bottom > client.bottom)
663 client_rgn = CreateRectRgnIndirect( &client );
664 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
666 /* check if update rgn contains complete nonclient area */
667 if (type == SIMPLEREGION && EqualRect( &window, &update ))
669 DeleteObject( whole_rgn );
670 whole_rgn = (HRGN)1;
673 else
675 client_rgn = whole_rgn;
676 whole_rgn = 0;
679 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
681 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
682 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
684 SetThreadDpiAwarenessContext( context );
686 return client_rgn;
690 /***********************************************************************
691 * send_erase
693 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
694 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
695 * Helper for erase_now and BeginPaint.
697 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
698 RECT *clip_rect, HDC *hdc_ret )
700 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
701 HDC hdc = 0;
702 RECT dummy;
704 if (!clip_rect) clip_rect = &dummy;
705 if (hdc_ret || (flags & UPDATE_ERASE))
707 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
708 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
710 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
712 INT type = GetClipBox( hdc, clip_rect );
714 if (flags & UPDATE_ERASE)
716 /* don't erase if the clip box is empty */
717 if (type != NULLREGION)
718 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
720 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
723 if (hdc_ret) *hdc_ret = hdc;
725 if (!hdc) DeleteObject( client_rgn );
726 return need_erase;
730 /***********************************************************************
731 * erase_now
733 * Implementation of RDW_ERASENOW behavior.
735 void erase_now( HWND hwnd, UINT rdw_flags )
737 HWND child = 0;
738 HRGN hrgn;
739 BOOL need_erase = FALSE;
741 /* loop while we find a child to repaint */
742 for (;;)
744 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
746 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
747 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
748 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
750 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
751 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
753 if (!flags) break; /* nothing more to do */
754 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
759 /***********************************************************************
760 * copy_bits_from_surface
762 * Copy bits from a window surface; helper for move_window_bits and move_window_bits_parent.
764 static void copy_bits_from_surface( HWND hwnd, struct window_surface *surface,
765 const RECT *dst, const RECT *src )
767 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
768 BITMAPINFO *info = (BITMAPINFO *)buffer;
769 void *bits;
770 UINT flags = UPDATE_NOCHILDREN | UPDATE_CLIPCHILDREN;
771 HRGN rgn = get_update_region( hwnd, &flags, NULL );
772 HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_WINDOW | DCX_EXCLUDERGN );
774 bits = surface->funcs->get_info( surface, info );
775 surface->funcs->lock( surface );
776 SetDIBitsToDevice( hdc, dst->left, dst->top, dst->right - dst->left, dst->bottom - dst->top,
777 src->left - surface->rect.left, surface->rect.bottom - src->bottom,
778 0, surface->rect.bottom - surface->rect.top,
779 bits, info, DIB_RGB_COLORS );
780 surface->funcs->unlock( surface );
781 ReleaseDC( hwnd, hdc );
785 /***********************************************************************
786 * move_window_bits
788 * Move the window bits when a window is resized or its surface recreated.
790 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
791 struct window_surface *new_surface,
792 const RECT *visible_rect, const RECT *old_visible_rect,
793 const RECT *window_rect, const RECT *valid_rects )
795 RECT dst = valid_rects[0];
796 RECT src = valid_rects[1];
798 if (new_surface != old_surface ||
799 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
800 src.top - old_visible_rect->top != dst.top - visible_rect->top)
802 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
803 OffsetRect( &src, -old_visible_rect->left, -old_visible_rect->top );
804 OffsetRect( &dst, -window_rect->left, -window_rect->top );
805 copy_bits_from_surface( hwnd, old_surface, &dst, &src );
810 /***********************************************************************
811 * move_window_bits_parent
813 * Move the window bits in the parent surface when a child is moved.
815 void move_window_bits_parent( HWND hwnd, HWND parent, const RECT *window_rect, const RECT *valid_rects )
817 struct window_surface *surface;
818 RECT dst = valid_rects[0];
819 RECT src = valid_rects[1];
820 WND *win;
822 if (src.left == dst.left && src.top == dst.top) return;
824 if (!(win = WIN_GetPtr( parent ))) return;
825 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
826 if (!(surface = win->surface))
828 WIN_ReleasePtr( win );
829 return;
832 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
833 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), parent, (POINT *)&src, 2 );
834 OffsetRect( &src, win->client_rect.left - win->visible_rect.left,
835 win->client_rect.top - win->visible_rect.top );
836 OffsetRect( &dst, -window_rect->left, -window_rect->top );
837 window_surface_add_ref( surface );
838 WIN_ReleasePtr( win );
840 copy_bits_from_surface( hwnd, surface, &dst, &src );
841 window_surface_release( surface );
845 /***********************************************************************
846 * update_now
848 * Implementation of RDW_UPDATENOW behavior.
850 static void update_now( HWND hwnd, UINT rdw_flags )
852 HWND child = 0;
854 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
855 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
857 /* loop while we find a child to repaint */
858 for (;;)
860 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
862 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
863 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
865 if (!get_update_flags( hwnd, &child, &flags )) break;
866 if (!flags) break; /* nothing more to do */
868 SendMessageW( child, WM_PAINT, 0, 0 );
869 if (rdw_flags & RDW_NOCHILDREN) break;
874 /*************************************************************************
875 * fix_caret
877 * Helper for ScrollWindowEx:
878 * If the return value is 0, no special caret handling is necessary.
879 * Otherwise the return value is the handle of the window that owns the
880 * caret. Its caret needs to be hidden during the scroll operation and
881 * moved to new_caret_pos if move_caret is TRUE.
883 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
884 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
886 GUITHREADINFO info;
887 RECT rect, mapped_rcCaret;
889 info.cbSize = sizeof(info);
890 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
891 if (!info.hwndCaret) return 0;
893 mapped_rcCaret = info.rcCaret;
894 if (info.hwndCaret == hWnd)
896 /* The caret needs to be moved along with scrolling even if it's
897 * outside the visible area. Otherwise, when the caret is scrolled
898 * out from the view, the position won't get updated anymore and
899 * the caret will never scroll back again. */
900 *move_caret = TRUE;
901 new_caret_pos->x = info.rcCaret.left + dx;
902 new_caret_pos->y = info.rcCaret.top + dy;
904 else
906 *move_caret = FALSE;
907 if (!(flags & SW_SCROLLCHILDREN) || !IsChild(hWnd, info.hwndCaret))
908 return 0;
909 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
912 /* If the caret is not in the src/dest rects, all is fine done. */
913 if (!IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
915 rect = *scroll_rect;
916 OffsetRect(&rect, dx, dy);
917 if (!IntersectRect(&rect, &rect, &mapped_rcCaret))
918 return 0;
921 /* Indicate that the caret needs to be updated during the scrolling. */
922 return info.hwndCaret;
926 /***********************************************************************
927 * BeginPaint (USER32.@)
929 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
931 HRGN hrgn;
932 HDC hdc;
933 BOOL erase;
934 RECT rect;
935 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
937 HideCaret( hwnd );
939 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
941 erase = send_erase( hwnd, flags, hrgn, &rect, &hdc );
943 TRACE("hdc = %p box = (%s), fErase = %d\n", hdc, wine_dbgstr_rect(&rect), erase);
945 if (!lps)
947 release_dc( hwnd, hdc, TRUE );
948 return 0;
950 lps->fErase = erase;
951 lps->rcPaint = rect;
952 lps->hdc = hdc;
953 return hdc;
957 /***********************************************************************
958 * EndPaint (USER32.@)
960 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
962 ShowCaret( hwnd );
963 flush_window_surfaces( FALSE );
964 if (!lps) return FALSE;
965 release_dc( hwnd, lps->hdc, TRUE );
966 return TRUE;
970 /***********************************************************************
971 * GetDCEx (USER32.@)
973 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
975 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
976 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
977 struct dce *dce;
978 BOOL bUpdateVisRgn = TRUE;
979 HWND parent;
980 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
982 if (!hwnd) hwnd = GetDesktopWindow();
983 else hwnd = WIN_GetFullHandle( hwnd );
985 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
987 if (!IsWindow(hwnd)) return 0;
989 /* fixup flags */
991 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
993 if (flags & DCX_USESTYLE)
995 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
997 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
999 if (!(flags & DCX_WINDOW))
1001 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
1003 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
1004 flags |= DCX_CLIPCHILDREN;
1008 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
1010 parent = GetAncestor( hwnd, GA_PARENT );
1011 if (!parent || (parent == GetDesktopWindow()))
1012 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
1014 /* it seems parent clip is ignored when clipping siblings or children */
1015 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
1017 if( flags & DCX_PARENTCLIP )
1019 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
1020 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
1022 flags &= ~DCX_CLIPCHILDREN;
1023 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
1027 /* find a suitable DCE */
1029 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
1031 struct dce *dceEmpty = NULL, *dceUnused = NULL, *found = NULL;
1032 unsigned int count = 0;
1034 /* Strategy: First, we attempt to find a non-empty but unused DCE with
1035 * compatible flags. Next, we look for an empty entry. If the cache is
1036 * full we have to purge one of the unused entries.
1038 USER_Lock();
1039 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1041 if (!(dce->flags & DCX_CACHE)) break;
1042 count++;
1043 if (dce->count) continue;
1044 dceUnused = dce;
1045 if (!dce->hwnd) dceEmpty = dce;
1046 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1048 TRACE( "found valid %p hwnd %p, flags %08x\n", dce->hdc, hwnd, dce->flags );
1049 found = dce;
1050 bUpdateVisRgn = FALSE;
1051 break;
1054 if (!found) found = dceEmpty;
1055 if (!found && count >= DCE_CACHE_SIZE) found = dceUnused;
1057 dce = found;
1058 if (dce)
1060 dce->count = 1;
1061 SetHookFlags( dce->hdc, DCHF_ENABLEDC );
1063 USER_Unlock();
1065 /* if there's no dce empty or unused, allocate a new one */
1066 if (!dce)
1068 if (!(dce = alloc_dce())) return 0;
1069 dce->flags = DCX_CACHE;
1070 USER_Lock();
1071 list_add_head( &dce_list, &dce->entry );
1072 USER_Unlock();
1075 else
1077 flags |= DCX_NORESETATTRS;
1078 if (dce->hwnd != hwnd)
1080 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1081 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1082 dce->clip_rgn = 0;
1084 else bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1087 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1089 /* if the extra clip region has changed, get rid of the old one */
1090 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1091 delete_clip_rgn( dce );
1092 dce->clip_rgn = hrgnClip;
1093 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1094 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1095 bUpdateVisRgn = TRUE;
1098 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1100 dce->hwnd = hwnd;
1101 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1103 /* cross-process invalidation is not supported yet, so always update the vis rgn */
1104 if (!WIN_IsCurrentProcess( hwnd )) bUpdateVisRgn = TRUE;
1106 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1108 if (bUpdateVisRgn) update_visible_region( dce );
1110 TRACE("(%p,%p,0x%x): returning %p%s\n", hwnd, hrgnClip, flags, dce->hdc,
1111 bUpdateVisRgn ? " (updated)" : "");
1112 return dce->hdc;
1116 /***********************************************************************
1117 * GetDC (USER32.@)
1119 * Get a device context.
1121 * RETURNS
1122 * Success: Handle to the device context
1123 * Failure: NULL.
1125 HDC WINAPI GetDC( HWND hwnd )
1127 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1128 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1132 /***********************************************************************
1133 * GetWindowDC (USER32.@)
1135 HDC WINAPI GetWindowDC( HWND hwnd )
1137 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1141 /***********************************************************************
1142 * ReleaseDC (USER32.@)
1144 * Release a device context.
1146 * RETURNS
1147 * Success: Non-zero. Resources used by hdc are released.
1148 * Failure: 0.
1150 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1152 return release_dc( hwnd, hdc, FALSE );
1156 /**********************************************************************
1157 * WindowFromDC (USER32.@)
1159 HWND WINAPI WindowFromDC( HDC hdc )
1161 struct dce *dce;
1162 HWND hwnd = 0;
1164 USER_Lock();
1165 dce = (struct dce *)GetDCHook( hdc, NULL );
1166 if (dce) hwnd = dce->hwnd;
1167 USER_Unlock();
1168 return hwnd;
1172 /***********************************************************************
1173 * LockWindowUpdate (USER32.@)
1175 * Enables or disables painting in the chosen window.
1177 * PARAMS
1178 * hwnd [I] handle to a window.
1180 * RETURNS
1181 * If successful, returns nonzero value. Otherwise,
1182 * returns 0.
1184 * NOTES
1185 * You can lock only one window at a time.
1187 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1189 static HWND lockedWnd;
1191 FIXME("(%p), partial stub!\n",hwnd);
1193 USER_Lock();
1194 if (lockedWnd)
1196 if (!hwnd)
1198 /* Unlock lockedWnd */
1199 /* FIXME: Do something */
1201 else
1203 /* Attempted to lock a second window */
1204 /* Return FALSE and do nothing */
1205 USER_Unlock();
1206 return FALSE;
1209 lockedWnd = hwnd;
1210 USER_Unlock();
1211 return TRUE;
1215 /***********************************************************************
1216 * RedrawWindow (USER32.@)
1218 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1220 static const RECT empty;
1221 BOOL ret;
1223 if (TRACE_ON(win))
1225 if (hrgn)
1227 RECT r;
1228 GetRgnBox( hrgn, &r );
1229 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1231 else if (rect)
1232 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1233 else
1234 TRACE( "%p whole window ", hwnd );
1236 dump_rdw_flags(flags);
1239 /* process pending expose events before painting */
1240 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1242 if (rect && !hrgn)
1244 if (IsRectEmpty( rect )) rect = &empty;
1245 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1247 else if (!hrgn)
1249 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1251 else /* need to build a list of the region rectangles */
1253 DWORD size;
1254 RGNDATA *data;
1256 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1257 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1258 GetRegionData( hrgn, size, data );
1259 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1260 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1261 else
1262 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1263 HeapFree( GetProcessHeap(), 0, data );
1266 if (!hwnd) hwnd = GetDesktopWindow();
1268 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1269 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1271 return ret;
1275 /***********************************************************************
1276 * UpdateWindow (USER32.@)
1278 BOOL WINAPI UpdateWindow( HWND hwnd )
1280 if (!hwnd)
1282 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1283 return FALSE;
1286 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1290 /***********************************************************************
1291 * InvalidateRgn (USER32.@)
1293 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1295 if (!hwnd)
1297 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1298 return FALSE;
1301 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1305 /***********************************************************************
1306 * InvalidateRect (USER32.@)
1308 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1309 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1311 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1313 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1315 if (!hwnd)
1317 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1318 rect = NULL;
1321 return RedrawWindow( hwnd, rect, 0, flags );
1325 /***********************************************************************
1326 * ValidateRgn (USER32.@)
1328 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1330 if (!hwnd)
1332 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1333 return FALSE;
1336 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1340 /***********************************************************************
1341 * ValidateRect (USER32.@)
1343 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1344 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1346 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1348 UINT flags = RDW_VALIDATE;
1350 if (!hwnd)
1352 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1353 rect = NULL;
1356 return RedrawWindow( hwnd, rect, 0, flags );
1360 /***********************************************************************
1361 * GetUpdateRgn (USER32.@)
1363 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1365 DPI_AWARENESS_CONTEXT context;
1366 INT retval = ERROR;
1367 UINT flags = UPDATE_NOCHILDREN;
1368 HRGN update_rgn;
1370 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1372 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1374 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1376 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1377 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1379 flags = UPDATE_DELAYED_ERASE;
1380 get_update_flags( hwnd, NULL, &flags );
1382 /* map region to client coordinates */
1383 map_window_region( 0, hwnd, hrgn );
1385 SetThreadDpiAwarenessContext( context );
1386 return retval;
1390 /***********************************************************************
1391 * GetUpdateRect (USER32.@)
1393 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1395 DPI_AWARENESS_CONTEXT context;
1396 UINT flags = UPDATE_NOCHILDREN;
1397 HRGN update_rgn;
1398 BOOL need_erase;
1400 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1402 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1404 if (rect)
1406 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1408 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1409 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1410 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1411 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1412 SetThreadDpiAwarenessContext( context );
1413 *rect = rect_win_to_thread_dpi( hwnd, *rect );
1414 DPtoLP( hdc, (LPPOINT)rect, 2 );
1415 SetLayout( hdc, layout );
1416 ReleaseDC( hwnd, hdc );
1419 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1421 /* check if we still have an update region */
1422 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1423 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1424 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1428 /***********************************************************************
1429 * ExcludeUpdateRgn (USER32.@)
1431 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1433 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1434 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1436 if (ret != ERROR)
1438 DPI_AWARENESS_CONTEXT context;
1439 POINT pt;
1441 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1442 GetDCOrgEx( hdc, &pt );
1443 MapWindowPoints( 0, hwnd, &pt, 1 );
1444 OffsetRgn( update_rgn, -pt.x, -pt.y );
1445 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1446 SetThreadDpiAwarenessContext( context );
1448 DeleteObject( update_rgn );
1449 return ret;
1453 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1454 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1456 INT retVal = NULLREGION;
1457 BOOL bOwnRgn = TRUE;
1458 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1459 int rdw_flags;
1460 HRGN hrgnTemp;
1461 HRGN hrgnWinupd = 0;
1462 HDC hDC;
1463 RECT rc, cliprc;
1464 HWND hwndCaret = NULL;
1465 BOOL moveCaret = FALSE;
1466 POINT newCaretPos;
1468 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1469 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1470 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1471 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1472 FIXME("some flags (%04x) are unhandled\n", flags);
1474 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1475 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1477 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1478 hwnd = WIN_GetFullHandle( hwnd );
1480 GetClientRect(hwnd, &rc);
1482 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1483 else cliprc = rc;
1485 if (rect) IntersectRect(&rc, &rc, rect);
1487 if( hrgnUpdate ) bOwnRgn = FALSE;
1488 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1490 newCaretPos.x = newCaretPos.y = 0;
1492 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1493 DWORD dcxflags = 0;
1494 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1496 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1497 if (hwndCaret)
1498 HideCaret(hwndCaret);
1500 if (is_ex) dcxflags |= DCX_CACHE;
1501 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1502 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1503 dcxflags |= DCX_PARENTCLIP;
1504 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1505 dcxflags |= DCX_CLIPCHILDREN;
1506 hDC = GetDCEx( hwnd, 0, dcxflags);
1507 if (hDC)
1509 NtUserScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1511 ReleaseDC( hwnd, hDC );
1513 if (!bUpdate)
1514 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1517 /* If the windows has an update region, this must be
1518 * scrolled as well. Keep a copy in hrgnWinupd
1519 * to be added to hrngUpdate at the end. */
1520 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1521 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1522 if (retVal != NULLREGION)
1524 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1525 if( !bOwnRgn) {
1526 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1527 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1529 OffsetRgn( hrgnTemp, dx, dy );
1530 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1531 if( !bOwnRgn)
1532 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1533 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1535 /* Catch the case where the scrolling amount exceeds the size of the
1536 * original window. This generated a second update area that is the
1537 * location where the original scrolled content would end up.
1538 * This second region is not returned by the ScrollDC and sets
1539 * ScrollWindowEx apart from just a ScrollDC.
1541 * This has been verified with testing on windows.
1543 if (abs(dx) > abs(rc.right - rc.left) ||
1544 abs(dy) > abs(rc.bottom - rc.top))
1546 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1547 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1548 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1550 if (rcUpdate)
1552 RECT rcTemp;
1553 GetRgnBox( hrgnTemp, &rcTemp );
1554 UnionRect( rcUpdate, rcUpdate, &rcTemp );
1557 if( !bOwnRgn)
1558 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1560 DeleteObject( hrgnClip );
1562 DeleteObject( hrgnTemp );
1563 } else {
1564 /* nothing was scrolled */
1565 if( !bOwnRgn)
1566 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1567 SetRectEmpty( rcUpdate);
1570 if( flags & SW_SCROLLCHILDREN )
1572 HWND *list = WIN_ListChildren( hwnd );
1573 if (list)
1575 int i;
1576 RECT r, dummy;
1577 for (i = 0; list[i]; i++)
1579 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1580 if (!rect || IntersectRect(&dummy, &r, rect))
1581 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1582 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1583 SWP_NOREDRAW | SWP_DEFERERASE );
1585 HeapFree( GetProcessHeap(), 0, list );
1589 if( flags & (SW_INVALIDATE | SW_ERASE) )
1590 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1591 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1593 if( hrgnWinupd) {
1594 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1595 DeleteObject( hrgnWinupd);
1598 if( moveCaret )
1599 SetCaretPos( newCaretPos.x, newCaretPos.y );
1600 if( hwndCaret )
1601 ShowCaret( hwndCaret );
1603 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1605 return retVal;
1609 /*************************************************************************
1610 * ScrollWindowEx (USER32.@)
1612 * Note: contrary to what the doc says, pixels that are scrolled from the
1613 * outside of clipRect to the inside are NOT painted.
1616 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1617 const RECT *rect, const RECT *clipRect,
1618 HRGN hrgnUpdate, LPRECT rcUpdate,
1619 UINT flags )
1621 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1624 /*************************************************************************
1625 * ScrollWindow (USER32.@)
1628 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1629 const RECT *rect, const RECT *clipRect )
1631 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1632 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1635 /************************************************************************
1636 * PrintWindow (USER32.@)
1639 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1641 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1642 if(!(nFlags & PW_CLIENTONLY))
1644 flags |= PRF_NONCLIENT;
1646 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1647 return TRUE;