include: Make sure __int64 is correctly defined on PPC64.
[wine.git] / dlls / user32 / painting.c
blobdb244ff04a5e19367c9c8ef9ff34b78aac720264
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 "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "wine/server.h"
33 #include "win.h"
34 #include "user_private.h"
35 #include "controls.h"
36 #include "wine/gdi_driver.h"
37 #include "wine/list.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win);
43 struct dce
45 struct list entry; /* entry in global DCE list */
46 HDC hdc;
47 HWND hwnd;
48 HRGN clip_rgn;
49 DWORD flags;
50 LONG count; /* usage count; 0 or 1 for cache DCEs, always 1 for window DCEs,
51 always >= 1 for class DCEs */
54 static struct list dce_list = LIST_INIT(dce_list);
56 #define DCE_CACHE_SIZE 64
58 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
61 /***********************************************************************
62 * dump_rdw_flags
64 static void dump_rdw_flags(UINT flags)
66 TRACE("flags:");
67 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
68 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
69 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
70 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
71 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
72 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
73 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
74 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
75 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
76 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
77 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
78 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
80 #define RDW_FLAGS \
81 (RDW_INVALIDATE | \
82 RDW_INTERNALPAINT | \
83 RDW_ERASE | \
84 RDW_VALIDATE | \
85 RDW_NOINTERNALPAINT | \
86 RDW_NOERASE | \
87 RDW_NOCHILDREN | \
88 RDW_ALLCHILDREN | \
89 RDW_UPDATENOW | \
90 RDW_ERASENOW | \
91 RDW_FRAME | \
92 RDW_NOFRAME)
94 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
95 TRACE("\n");
96 #undef RDW_FLAGS
100 /***********************************************************************
101 * update_visible_region
103 * Set the visible region and X11 drawable for the DC associated to
104 * a given window.
106 static void update_visible_region( struct dce *dce )
108 struct window_surface *surface = NULL;
109 NTSTATUS status;
110 HRGN vis_rgn = 0;
111 HWND top_win = 0;
112 DWORD flags = dce->flags;
113 DWORD paint_flags = 0;
114 size_t size = 256;
115 RECT win_rect, top_rect;
116 WND *win;
118 /* don't clip siblings if using parent clip region */
119 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
121 /* fetch the visible region from the server */
124 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
125 if (!data) return;
127 SERVER_START_REQ( get_visible_region )
129 req->window = wine_server_user_handle( dce->hwnd );
130 req->flags = flags;
131 wine_server_set_reply( req, data->Buffer, size );
132 if (!(status = wine_server_call( req )))
134 size_t reply_size = wine_server_reply_size( reply );
135 data->rdh.dwSize = sizeof(data->rdh);
136 data->rdh.iType = RDH_RECTANGLES;
137 data->rdh.nCount = reply_size / sizeof(RECT);
138 data->rdh.nRgnSize = reply_size;
139 vis_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
141 top_win = wine_server_ptr_handle( reply->top_win );
142 win_rect.left = reply->win_rect.left;
143 win_rect.top = reply->win_rect.top;
144 win_rect.right = reply->win_rect.right;
145 win_rect.bottom = reply->win_rect.bottom;
146 top_rect.left = reply->top_rect.left;
147 top_rect.top = reply->top_rect.top;
148 top_rect.right = reply->top_rect.right;
149 top_rect.bottom = reply->top_rect.bottom;
150 paint_flags = reply->paint_flags;
152 else size = reply->total_size;
154 SERVER_END_REQ;
155 HeapFree( GetProcessHeap(), 0, data );
156 } while (status == STATUS_BUFFER_OVERFLOW);
158 if (status || !vis_rgn) return;
160 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
162 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
163 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
165 /* don't use a surface to paint the client area of OpenGL windows */
166 if (!(paint_flags & SET_WINPOS_PIXEL_FORMAT) || (flags & DCX_WINDOW))
168 win = WIN_GetPtr( top_win );
169 if (win && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
171 surface = win->surface;
172 if (surface) window_surface_add_ref( surface );
173 WIN_ReleasePtr( win );
177 if (!surface) SetRectEmpty( &top_rect );
178 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
179 if (surface) window_surface_release( surface );
183 /***********************************************************************
184 * release_dce
186 static void release_dce( struct dce *dce )
188 if (!dce->hwnd) return; /* already released */
190 __wine_set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
191 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
193 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
194 dce->clip_rgn = 0;
195 dce->hwnd = 0;
196 dce->flags &= DCX_CACHE;
200 /***********************************************************************
201 * delete_clip_rgn
203 static void delete_clip_rgn( struct dce *dce )
205 if (!dce->clip_rgn) return; /* nothing to do */
207 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
208 DeleteObject( dce->clip_rgn );
209 dce->clip_rgn = 0;
211 /* make it dirty so that the vis rgn gets recomputed next time */
212 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
216 /***********************************************************************
217 * alloc_dce
219 * Allocate a new DCE.
221 static struct dce *alloc_dce(void)
223 struct dce *dce;
225 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
226 if (!(dce->hdc = CreateDCW( L"DISPLAY", NULL, NULL, NULL )))
228 HeapFree( GetProcessHeap(), 0, dce );
229 return 0;
231 dce->hwnd = 0;
232 dce->clip_rgn = 0;
233 dce->flags = 0;
234 dce->count = 1;
236 /* store DCE handle in DC hook data field */
237 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
238 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
239 return dce;
243 /***********************************************************************
244 * get_window_dce
246 static struct dce *get_window_dce( HWND hwnd )
248 struct dce *dce;
249 WND *win = WIN_GetPtr( hwnd );
251 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
253 dce = win->dce;
254 if (!dce && (dce = get_class_dce( win->class )))
256 win->dce = dce;
257 dce->count++;
259 WIN_ReleasePtr( win );
261 if (!dce) /* try to allocate one */
263 struct dce *dce_to_free = NULL;
264 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
266 if (class_style & CS_CLASSDC)
268 if (!(dce = alloc_dce())) return NULL;
270 win = WIN_GetPtr( hwnd );
271 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
273 if (win->dce) /* another thread beat us to it */
275 dce_to_free = dce;
276 dce = win->dce;
278 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
280 dce_to_free = dce;
281 dce = win->dce;
282 dce->count++;
284 else
286 dce->count++;
287 list_add_tail( &dce_list, &dce->entry );
289 WIN_ReleasePtr( win );
291 else dce_to_free = dce;
293 else if (class_style & CS_OWNDC)
295 if (!(dce = alloc_dce())) return NULL;
297 win = WIN_GetPtr( hwnd );
298 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
300 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
301 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
302 if (win->dce) /* another thread beat us to it */
304 dce_to_free = dce;
305 dce = win->dce;
307 else
309 win->dce = dce;
310 dce->hwnd = hwnd;
311 list_add_tail( &dce_list, &dce->entry );
313 WIN_ReleasePtr( win );
315 else dce_to_free = dce;
318 if (dce_to_free)
320 SetDCHook( dce_to_free->hdc, NULL, 0 );
321 DeleteDC( dce_to_free->hdc );
322 HeapFree( GetProcessHeap(), 0, dce_to_free );
323 if (dce_to_free == dce)
324 dce = NULL;
327 return dce;
331 /***********************************************************************
332 * free_dce
334 * Free a class or window DCE.
336 void free_dce( struct dce *dce, HWND hwnd )
338 struct dce *dce_to_free = NULL;
340 USER_Lock();
342 if (dce)
344 if (!--dce->count)
346 release_dce( dce );
347 list_remove( &dce->entry );
348 dce_to_free = dce;
350 else if (dce->hwnd == hwnd)
352 release_dce( dce );
356 /* now check for cache DCEs */
358 if (hwnd)
360 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
362 if (dce->hwnd != hwnd) continue;
363 if (!(dce->flags & DCX_CACHE)) break;
365 release_dce( dce );
366 if (dce->count)
368 WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
369 dce->count = 0;
370 SetHookFlags( dce->hdc, DCHF_DISABLEDC );
375 USER_Unlock();
377 if (dce_to_free)
379 SetDCHook( dce_to_free->hdc, NULL, 0 );
380 DeleteDC( dce_to_free->hdc );
381 HeapFree( GetProcessHeap(), 0, dce_to_free );
386 /***********************************************************************
387 * make_dc_dirty
389 * Mark the associated DC as dirty to force a refresh of the visible region
391 static void make_dc_dirty( struct dce *dce )
393 if (!dce->count)
395 /* Don't bother with visible regions of unused DCEs */
396 TRACE("purged %p hwnd %p\n", dce->hdc, dce->hwnd);
397 release_dce( dce );
399 else
401 /* Set dirty bits in the hDC and DCE structs */
402 TRACE("fixed up %p hwnd %p\n", dce->hdc, dce->hwnd);
403 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
408 /***********************************************************************
409 * invalidate_dce
411 * It is called from SetWindowPos() - we have to
412 * mark as dirty all busy DCEs for windows that have pWnd->parent as
413 * an ancestor and whose client rect intersects with specified update
414 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
415 * DCX_CLIPCHILDREN flag is set.
417 void invalidate_dce( WND *win, const RECT *extra_rect )
419 DPI_AWARENESS_CONTEXT context;
420 RECT window_rect;
421 struct dce *dce;
423 if (!win->parent) return;
425 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( win->obj.handle ));
427 GetWindowRect( win->obj.handle, &window_rect );
429 TRACE("%p parent %p %s (%s)\n",
430 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
432 /* walk all DCEs and fixup non-empty entries */
434 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
436 if (!dce->hwnd) continue;
438 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce->hdc, dce->hwnd, dce->flags,
439 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
441 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
442 continue; /* child window positions don't bother us */
444 /* if DCE window is a child of hwnd, it has to be invalidated */
445 if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
447 make_dc_dirty( dce );
448 continue;
451 /* otherwise check if the window rectangle intersects this DCE window */
452 if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
454 RECT dce_rect, tmp;
455 GetWindowRect( dce->hwnd, &dce_rect );
456 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
457 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
458 make_dc_dirty( dce );
461 SetThreadDpiAwarenessContext( context );
464 /***********************************************************************
465 * release_dc
467 * Implementation of ReleaseDC.
469 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
471 struct dce *dce;
472 BOOL ret = FALSE;
474 TRACE("%p %p\n", hwnd, hdc );
476 USER_Lock();
477 dce = (struct dce *)GetDCHook( hdc, NULL );
478 if (dce && dce->count && dce->hwnd)
480 if (!(dce->flags & DCX_NORESETATTRS)) SetHookFlags( dce->hdc, DCHF_RESETDC );
481 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
482 if (dce->flags & DCX_CACHE)
484 dce->count = 0;
485 SetHookFlags( dce->hdc, DCHF_DISABLEDC );
487 ret = TRUE;
489 USER_Unlock();
490 return ret;
494 /***********************************************************************
495 * dc_hook
497 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
499 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
501 BOOL retv = TRUE;
502 struct dce *dce = (struct dce *)data;
504 TRACE("hDC = %p, %u\n", hDC, code);
506 if (!dce) return FALSE;
507 assert( dce->hdc == hDC );
509 switch( code )
511 case DCHC_INVALIDVISRGN:
512 /* GDI code calls this when it detects that the
513 * DC is dirty (usually after SetHookFlags()). This
514 * means that we have to recompute the visible region.
516 if (dce->count) update_visible_region( dce );
517 else /* non-fatal but shouldn't happen */
518 WARN("DC is not in use!\n");
519 break;
520 case DCHC_DELETEDC:
521 USER_Lock();
522 if (!(dce->flags & DCX_CACHE))
524 WARN("Application trying to delete an owned DC %p\n", dce->hdc);
525 retv = FALSE;
527 else
529 list_remove( &dce->entry );
530 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
531 HeapFree( GetProcessHeap(), 0, dce );
533 USER_Unlock();
534 break;
536 return retv;
540 /***********************************************************************
541 * get_update_region
543 * Return update region (in screen coordinates) for a window.
545 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
547 HRGN hrgn = 0;
548 NTSTATUS status;
549 RGNDATA *data;
550 size_t size = 256;
554 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
556 SetLastError( ERROR_OUTOFMEMORY );
557 return 0;
560 SERVER_START_REQ( get_update_region )
562 req->window = wine_server_user_handle( hwnd );
563 req->from_child = wine_server_user_handle( child ? *child : 0 );
564 req->flags = *flags;
565 wine_server_set_reply( req, data->Buffer, size );
566 if (!(status = wine_server_call( req )))
568 size_t reply_size = wine_server_reply_size( reply );
569 data->rdh.dwSize = sizeof(data->rdh);
570 data->rdh.iType = RDH_RECTANGLES;
571 data->rdh.nCount = reply_size / sizeof(RECT);
572 data->rdh.nRgnSize = reply_size;
573 hrgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
574 if (child) *child = wine_server_ptr_handle( reply->child );
575 *flags = reply->flags;
577 else size = reply->total_size;
579 SERVER_END_REQ;
580 HeapFree( GetProcessHeap(), 0, data );
581 } while (status == STATUS_BUFFER_OVERFLOW);
583 if (status) SetLastError( RtlNtStatusToDosError(status) );
584 return hrgn;
588 /***********************************************************************
589 * get_update_flags
591 * Get only the update flags, not the update region.
593 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
595 BOOL ret;
597 SERVER_START_REQ( get_update_region )
599 req->window = wine_server_user_handle( hwnd );
600 req->from_child = wine_server_user_handle( child ? *child : 0 );
601 req->flags = *flags | UPDATE_NOREGION;
602 if ((ret = !wine_server_call_err( req )))
604 if (child) *child = wine_server_ptr_handle( reply->child );
605 *flags = reply->flags;
608 SERVER_END_REQ;
609 return ret;
613 /***********************************************************************
614 * redraw_window_rects
616 * Redraw part of a window.
618 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
620 BOOL ret;
622 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
623 return TRUE; /* nothing to do */
625 SERVER_START_REQ( redraw_window )
627 req->window = wine_server_user_handle( hwnd );
628 req->flags = flags;
629 wine_server_add_data( req, rects, count * sizeof(RECT) );
630 ret = !wine_server_call_err( req );
632 SERVER_END_REQ;
633 return ret;
637 /***********************************************************************
638 * send_ncpaint
640 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
641 * Helper for erase_now and BeginPaint.
643 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
645 HRGN whole_rgn = get_update_region( hwnd, flags, child );
646 HRGN client_rgn = 0;
648 if (child) hwnd = *child;
650 if (hwnd == GetDesktopWindow()) return whole_rgn;
652 if (whole_rgn)
654 DPI_AWARENESS_CONTEXT context;
655 RECT client, window, update;
656 INT type;
658 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
660 /* check if update rgn overlaps with nonclient area */
661 type = GetRgnBox( whole_rgn, &update );
662 WIN_GetRectangles( hwnd, COORDS_SCREEN, &window, &client );
664 if ((*flags & UPDATE_NONCLIENT) ||
665 update.left < client.left || update.top < client.top ||
666 update.right > client.right || update.bottom > client.bottom)
668 client_rgn = CreateRectRgnIndirect( &client );
669 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
671 /* check if update rgn contains complete nonclient area */
672 if (type == SIMPLEREGION && EqualRect( &window, &update ))
674 DeleteObject( whole_rgn );
675 whole_rgn = (HRGN)1;
678 else
680 client_rgn = whole_rgn;
681 whole_rgn = 0;
684 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
686 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
687 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
689 SetThreadDpiAwarenessContext( context );
691 return client_rgn;
695 /***********************************************************************
696 * send_erase
698 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
699 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
700 * Helper for erase_now and BeginPaint.
702 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
703 RECT *clip_rect, HDC *hdc_ret )
705 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
706 HDC hdc = 0;
707 RECT dummy;
709 if (!clip_rect) clip_rect = &dummy;
710 if (hdc_ret || (flags & UPDATE_ERASE))
712 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
713 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
715 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
717 INT type = GetClipBox( hdc, clip_rect );
719 if (flags & UPDATE_ERASE)
721 /* don't erase if the clip box is empty */
722 if (type != NULLREGION)
723 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
725 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
728 if (hdc_ret) *hdc_ret = hdc;
730 if (!hdc) DeleteObject( client_rgn );
731 return need_erase;
735 /***********************************************************************
736 * erase_now
738 * Implementation of RDW_ERASENOW behavior.
740 void erase_now( HWND hwnd, UINT rdw_flags )
742 HWND child = 0;
743 HRGN hrgn;
744 BOOL need_erase = FALSE;
746 /* loop while we find a child to repaint */
747 for (;;)
749 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
751 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
752 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
753 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
755 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
756 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
758 if (!flags) break; /* nothing more to do */
759 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
764 /***********************************************************************
765 * copy_bits_from_surface
767 * Copy bits from a window surface; helper for move_window_bits and move_window_bits_parent.
769 static void copy_bits_from_surface( HWND hwnd, struct window_surface *surface,
770 const RECT *dst, const RECT *src )
772 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
773 BITMAPINFO *info = (BITMAPINFO *)buffer;
774 void *bits;
775 UINT flags = UPDATE_NOCHILDREN | UPDATE_CLIPCHILDREN;
776 HRGN rgn = get_update_region( hwnd, &flags, NULL );
777 HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_WINDOW | DCX_EXCLUDERGN );
779 bits = surface->funcs->get_info( surface, info );
780 surface->funcs->lock( surface );
781 SetDIBitsToDevice( hdc, dst->left, dst->top, dst->right - dst->left, dst->bottom - dst->top,
782 src->left - surface->rect.left, surface->rect.bottom - src->bottom,
783 0, surface->rect.bottom - surface->rect.top,
784 bits, info, DIB_RGB_COLORS );
785 surface->funcs->unlock( surface );
786 ReleaseDC( hwnd, hdc );
790 /***********************************************************************
791 * move_window_bits
793 * Move the window bits when a window is resized or its surface recreated.
795 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
796 struct window_surface *new_surface,
797 const RECT *visible_rect, const RECT *old_visible_rect,
798 const RECT *window_rect, const RECT *valid_rects )
800 RECT dst = valid_rects[0];
801 RECT src = valid_rects[1];
803 if (new_surface != old_surface ||
804 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
805 src.top - old_visible_rect->top != dst.top - visible_rect->top)
807 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
808 OffsetRect( &src, -old_visible_rect->left, -old_visible_rect->top );
809 OffsetRect( &dst, -window_rect->left, -window_rect->top );
810 copy_bits_from_surface( hwnd, old_surface, &dst, &src );
815 /***********************************************************************
816 * move_window_bits_parent
818 * Move the window bits in the parent surface when a child is moved.
820 void move_window_bits_parent( HWND hwnd, HWND parent, const RECT *window_rect, const RECT *valid_rects )
822 struct window_surface *surface;
823 RECT dst = valid_rects[0];
824 RECT src = valid_rects[1];
825 WND *win;
827 if (src.left == dst.left && src.top == dst.top) return;
829 if (!(win = WIN_GetPtr( parent ))) return;
830 if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return;
831 if (!(surface = win->surface))
833 WIN_ReleasePtr( win );
834 return;
837 TRACE( "copying %s -> %s\n", wine_dbgstr_rect( &src ), wine_dbgstr_rect( &dst ));
838 MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), parent, (POINT *)&src, 2 );
839 OffsetRect( &src, win->client_rect.left - win->visible_rect.left,
840 win->client_rect.top - win->visible_rect.top );
841 OffsetRect( &dst, -window_rect->left, -window_rect->top );
842 window_surface_add_ref( surface );
843 WIN_ReleasePtr( win );
845 copy_bits_from_surface( hwnd, surface, &dst, &src );
846 window_surface_release( surface );
850 /***********************************************************************
851 * update_now
853 * Implementation of RDW_UPDATENOW behavior.
855 static void update_now( HWND hwnd, UINT rdw_flags )
857 HWND child = 0;
859 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
860 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
862 /* loop while we find a child to repaint */
863 for (;;)
865 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
867 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
868 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
870 if (!get_update_flags( hwnd, &child, &flags )) break;
871 if (!flags) break; /* nothing more to do */
873 SendMessageW( child, WM_PAINT, 0, 0 );
874 if (rdw_flags & RDW_NOCHILDREN) break;
879 /*************************************************************************
880 * fix_caret
882 * Helper for ScrollWindowEx:
883 * If the return value is 0, no special caret handling is necessary.
884 * Otherwise the return value is the handle of the window that owns the
885 * caret. Its caret needs to be hidden during the scroll operation and
886 * moved to new_caret_pos if move_caret is TRUE.
888 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
889 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
891 GUITHREADINFO info;
892 RECT rect, mapped_rcCaret;
894 info.cbSize = sizeof(info);
895 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
896 if (!info.hwndCaret) return 0;
898 mapped_rcCaret = info.rcCaret;
899 if (info.hwndCaret == hWnd)
901 /* The caret needs to be moved along with scrolling even if it's
902 * outside the visible area. Otherwise, when the caret is scrolled
903 * out from the view, the position won't get updated anymore and
904 * the caret will never scroll back again. */
905 *move_caret = TRUE;
906 new_caret_pos->x = info.rcCaret.left + dx;
907 new_caret_pos->y = info.rcCaret.top + dy;
909 else
911 *move_caret = FALSE;
912 if (!(flags & SW_SCROLLCHILDREN) || !IsChild(hWnd, info.hwndCaret))
913 return 0;
914 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
917 /* If the caret is not in the src/dest rects, all is fine done. */
918 if (!IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
920 rect = *scroll_rect;
921 OffsetRect(&rect, dx, dy);
922 if (!IntersectRect(&rect, &rect, &mapped_rcCaret))
923 return 0;
926 /* Indicate that the caret needs to be updated during the scrolling. */
927 return info.hwndCaret;
931 /***********************************************************************
932 * BeginPaint (USER32.@)
934 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
936 HRGN hrgn;
937 HDC hdc;
938 BOOL erase;
939 RECT rect;
940 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
942 HideCaret( hwnd );
944 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
946 erase = send_erase( hwnd, flags, hrgn, &rect, &hdc );
948 TRACE("hdc = %p box = (%s), fErase = %d\n", hdc, wine_dbgstr_rect(&rect), erase);
950 if (!lps)
952 release_dc( hwnd, hdc, TRUE );
953 return 0;
955 lps->fErase = erase;
956 lps->rcPaint = rect;
957 lps->hdc = hdc;
958 return hdc;
962 /***********************************************************************
963 * EndPaint (USER32.@)
965 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
967 ShowCaret( hwnd );
968 flush_window_surfaces( FALSE );
969 if (!lps) return FALSE;
970 release_dc( hwnd, lps->hdc, TRUE );
971 return TRUE;
975 /***********************************************************************
976 * GetDCEx (USER32.@)
978 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
980 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
981 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
982 struct dce *dce;
983 BOOL bUpdateVisRgn = TRUE;
984 HWND parent;
985 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
987 if (!hwnd) hwnd = GetDesktopWindow();
988 else hwnd = WIN_GetFullHandle( hwnd );
990 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
992 if (!IsWindow(hwnd)) return 0;
994 /* fixup flags */
996 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
998 if (flags & DCX_USESTYLE)
1000 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
1002 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
1004 if (!(flags & DCX_WINDOW))
1006 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
1008 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
1009 flags |= DCX_CLIPCHILDREN;
1013 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
1015 parent = GetAncestor( hwnd, GA_PARENT );
1016 if (!parent || (parent == GetDesktopWindow()))
1017 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
1019 /* it seems parent clip is ignored when clipping siblings or children */
1020 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
1022 if( flags & DCX_PARENTCLIP )
1024 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
1025 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
1027 flags &= ~DCX_CLIPCHILDREN;
1028 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
1032 /* find a suitable DCE */
1034 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
1036 struct dce *dceEmpty = NULL, *dceUnused = NULL, *found = NULL;
1037 unsigned int count = 0;
1039 /* Strategy: First, we attempt to find a non-empty but unused DCE with
1040 * compatible flags. Next, we look for an empty entry. If the cache is
1041 * full we have to purge one of the unused entries.
1043 USER_Lock();
1044 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1046 if (!(dce->flags & DCX_CACHE)) break;
1047 count++;
1048 if (dce->count) continue;
1049 dceUnused = dce;
1050 if (!dce->hwnd) dceEmpty = dce;
1051 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1053 TRACE( "found valid %p hwnd %p, flags %08x\n", dce->hdc, hwnd, dce->flags );
1054 found = dce;
1055 bUpdateVisRgn = FALSE;
1056 break;
1059 if (!found) found = dceEmpty;
1060 if (!found && count >= DCE_CACHE_SIZE) found = dceUnused;
1062 dce = found;
1063 if (dce)
1065 dce->count = 1;
1066 SetHookFlags( dce->hdc, DCHF_ENABLEDC );
1068 USER_Unlock();
1070 /* if there's no dce empty or unused, allocate a new one */
1071 if (!dce)
1073 if (!(dce = alloc_dce())) return 0;
1074 dce->flags = DCX_CACHE;
1075 USER_Lock();
1076 list_add_head( &dce_list, &dce->entry );
1077 USER_Unlock();
1080 else
1082 flags |= DCX_NORESETATTRS;
1083 if (dce->hwnd != hwnd)
1085 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1086 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1087 dce->clip_rgn = 0;
1089 else bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1092 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1094 /* if the extra clip region has changed, get rid of the old one */
1095 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1096 delete_clip_rgn( dce );
1097 dce->clip_rgn = hrgnClip;
1098 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1099 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1100 bUpdateVisRgn = TRUE;
1103 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1105 dce->hwnd = hwnd;
1106 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1108 /* cross-process invalidation is not supported yet, so always update the vis rgn */
1109 if (!WIN_IsCurrentProcess( hwnd )) bUpdateVisRgn = TRUE;
1111 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1113 if (bUpdateVisRgn) update_visible_region( dce );
1115 TRACE("(%p,%p,0x%x): returning %p%s\n", hwnd, hrgnClip, flags, dce->hdc,
1116 bUpdateVisRgn ? " (updated)" : "");
1117 return dce->hdc;
1121 /***********************************************************************
1122 * GetDC (USER32.@)
1124 * Get a device context.
1126 * RETURNS
1127 * Success: Handle to the device context
1128 * Failure: NULL.
1130 HDC WINAPI GetDC( HWND hwnd )
1132 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1133 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1137 /***********************************************************************
1138 * GetWindowDC (USER32.@)
1140 HDC WINAPI GetWindowDC( HWND hwnd )
1142 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1146 /***********************************************************************
1147 * ReleaseDC (USER32.@)
1149 * Release a device context.
1151 * RETURNS
1152 * Success: Non-zero. Resources used by hdc are released.
1153 * Failure: 0.
1155 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1157 return release_dc( hwnd, hdc, FALSE );
1161 /**********************************************************************
1162 * WindowFromDC (USER32.@)
1164 HWND WINAPI WindowFromDC( HDC hdc )
1166 struct dce *dce;
1167 HWND hwnd = 0;
1169 USER_Lock();
1170 dce = (struct dce *)GetDCHook( hdc, NULL );
1171 if (dce) hwnd = dce->hwnd;
1172 USER_Unlock();
1173 return hwnd;
1177 /***********************************************************************
1178 * LockWindowUpdate (USER32.@)
1180 * Enables or disables painting in the chosen window.
1182 * PARAMS
1183 * hwnd [I] handle to a window.
1185 * RETURNS
1186 * If successful, returns nonzero value. Otherwise,
1187 * returns 0.
1189 * NOTES
1190 * You can lock only one window at a time.
1192 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1194 static HWND lockedWnd;
1196 FIXME("(%p), partial stub!\n",hwnd);
1198 USER_Lock();
1199 if (lockedWnd)
1201 if (!hwnd)
1203 /* Unlock lockedWnd */
1204 /* FIXME: Do something */
1206 else
1208 /* Attempted to lock a second window */
1209 /* Return FALSE and do nothing */
1210 USER_Unlock();
1211 return FALSE;
1214 lockedWnd = hwnd;
1215 USER_Unlock();
1216 return TRUE;
1220 /***********************************************************************
1221 * RedrawWindow (USER32.@)
1223 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1225 static const RECT empty;
1226 BOOL ret;
1228 if (TRACE_ON(win))
1230 if (hrgn)
1232 RECT r;
1233 GetRgnBox( hrgn, &r );
1234 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1236 else if (rect)
1237 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1238 else
1239 TRACE( "%p whole window ", hwnd );
1241 dump_rdw_flags(flags);
1244 /* process pending expose events before painting */
1245 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1247 if (rect && !hrgn)
1249 if (IsRectEmpty( rect )) rect = &empty;
1250 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1252 else if (!hrgn)
1254 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1256 else /* need to build a list of the region rectangles */
1258 DWORD size;
1259 RGNDATA *data;
1261 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1262 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1263 GetRegionData( hrgn, size, data );
1264 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1265 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1266 else
1267 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1268 HeapFree( GetProcessHeap(), 0, data );
1271 if (!hwnd) hwnd = GetDesktopWindow();
1273 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1274 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1276 return ret;
1280 /***********************************************************************
1281 * UpdateWindow (USER32.@)
1283 BOOL WINAPI UpdateWindow( HWND hwnd )
1285 if (!hwnd)
1287 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1288 return FALSE;
1291 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1295 /***********************************************************************
1296 * InvalidateRgn (USER32.@)
1298 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1300 if (!hwnd)
1302 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1303 return FALSE;
1306 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1310 /***********************************************************************
1311 * InvalidateRect (USER32.@)
1313 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1314 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1316 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1318 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1320 if (!hwnd)
1322 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1323 rect = NULL;
1326 return RedrawWindow( hwnd, rect, 0, flags );
1330 /***********************************************************************
1331 * ValidateRgn (USER32.@)
1333 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1335 if (!hwnd)
1337 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1338 return FALSE;
1341 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1345 /***********************************************************************
1346 * ValidateRect (USER32.@)
1348 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1349 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1351 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1353 UINT flags = RDW_VALIDATE;
1355 if (!hwnd)
1357 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1358 rect = NULL;
1361 return RedrawWindow( hwnd, rect, 0, flags );
1365 /***********************************************************************
1366 * GetUpdateRgn (USER32.@)
1368 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1370 DPI_AWARENESS_CONTEXT context;
1371 INT retval = ERROR;
1372 UINT flags = UPDATE_NOCHILDREN;
1373 HRGN update_rgn;
1375 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1377 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1379 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1381 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1382 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1384 flags = UPDATE_DELAYED_ERASE;
1385 get_update_flags( hwnd, NULL, &flags );
1387 /* map region to client coordinates */
1388 map_window_region( 0, hwnd, hrgn );
1390 SetThreadDpiAwarenessContext( context );
1391 return retval;
1395 /***********************************************************************
1396 * GetUpdateRect (USER32.@)
1398 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1400 DPI_AWARENESS_CONTEXT context;
1401 UINT flags = UPDATE_NOCHILDREN;
1402 HRGN update_rgn;
1403 BOOL need_erase;
1405 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1407 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1409 if (rect)
1411 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1413 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1414 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1415 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1416 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1417 SetThreadDpiAwarenessContext( context );
1418 *rect = rect_win_to_thread_dpi( hwnd, *rect );
1419 DPtoLP( hdc, (LPPOINT)rect, 2 );
1420 SetLayout( hdc, layout );
1421 ReleaseDC( hwnd, hdc );
1424 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1426 /* check if we still have an update region */
1427 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1428 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1429 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1433 /***********************************************************************
1434 * ExcludeUpdateRgn (USER32.@)
1436 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1438 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1439 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1441 if (ret != ERROR)
1443 DPI_AWARENESS_CONTEXT context;
1444 POINT pt;
1446 context = SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( hwnd ));
1447 GetDCOrgEx( hdc, &pt );
1448 MapWindowPoints( 0, hwnd, &pt, 1 );
1449 OffsetRgn( update_rgn, -pt.x, -pt.y );
1450 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1451 SetThreadDpiAwarenessContext( context );
1453 DeleteObject( update_rgn );
1454 return ret;
1458 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1459 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1461 INT retVal = NULLREGION;
1462 BOOL bOwnRgn = TRUE;
1463 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1464 int rdw_flags;
1465 HRGN hrgnTemp;
1466 HRGN hrgnWinupd = 0;
1467 HDC hDC;
1468 RECT rc, cliprc;
1469 HWND hwndCaret = NULL;
1470 BOOL moveCaret = FALSE;
1471 POINT newCaretPos;
1473 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1474 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1475 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1476 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1477 FIXME("some flags (%04x) are unhandled\n", flags);
1479 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1480 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1482 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1483 hwnd = WIN_GetFullHandle( hwnd );
1485 GetClientRect(hwnd, &rc);
1487 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1488 else cliprc = rc;
1490 if (rect) IntersectRect(&rc, &rc, rect);
1492 if( hrgnUpdate ) bOwnRgn = FALSE;
1493 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1495 newCaretPos.x = newCaretPos.y = 0;
1497 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1498 DWORD dcxflags = 0;
1499 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1501 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1502 if (hwndCaret)
1503 HideCaret(hwndCaret);
1505 if (is_ex) dcxflags |= DCX_CACHE;
1506 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1507 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1508 dcxflags |= DCX_PARENTCLIP;
1509 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1510 dcxflags |= DCX_CLIPCHILDREN;
1511 hDC = GetDCEx( hwnd, 0, dcxflags);
1512 if (hDC)
1514 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1516 ReleaseDC( hwnd, hDC );
1518 if (!bUpdate)
1519 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1522 /* If the windows has an update region, this must be
1523 * scrolled as well. Keep a copy in hrgnWinupd
1524 * to be added to hrngUpdate at the end. */
1525 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1526 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1527 if (retVal != NULLREGION)
1529 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1530 if( !bOwnRgn) {
1531 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1532 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1534 OffsetRgn( hrgnTemp, dx, dy );
1535 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1536 if( !bOwnRgn)
1537 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1538 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1540 /* Catch the case where the scrolling amount exceeds the size of the
1541 * original window. This generated a second update area that is the
1542 * location where the original scrolled content would end up.
1543 * This second region is not returned by the ScrollDC and sets
1544 * ScrollWindowEx apart from just a ScrollDC.
1546 * This has been verified with testing on windows.
1548 if (abs(dx) > abs(rc.right - rc.left) ||
1549 abs(dy) > abs(rc.bottom - rc.top))
1551 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1552 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1553 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1555 if (rcUpdate)
1557 RECT rcTemp;
1558 GetRgnBox( hrgnTemp, &rcTemp );
1559 UnionRect( rcUpdate, rcUpdate, &rcTemp );
1562 if( !bOwnRgn)
1563 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1565 DeleteObject( hrgnClip );
1567 DeleteObject( hrgnTemp );
1568 } else {
1569 /* nothing was scrolled */
1570 if( !bOwnRgn)
1571 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1572 SetRectEmpty( rcUpdate);
1575 if( flags & SW_SCROLLCHILDREN )
1577 HWND *list = WIN_ListChildren( hwnd );
1578 if (list)
1580 int i;
1581 RECT r, dummy;
1582 for (i = 0; list[i]; i++)
1584 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1585 if (!rect || IntersectRect(&dummy, &r, rect))
1586 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1587 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1588 SWP_NOREDRAW | SWP_DEFERERASE );
1590 HeapFree( GetProcessHeap(), 0, list );
1594 if( flags & (SW_INVALIDATE | SW_ERASE) )
1595 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1596 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1598 if( hrgnWinupd) {
1599 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1600 DeleteObject( hrgnWinupd);
1603 if( moveCaret )
1604 SetCaretPos( newCaretPos.x, newCaretPos.y );
1605 if( hwndCaret )
1606 ShowCaret( hwndCaret );
1608 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1610 return retVal;
1614 /*************************************************************************
1615 * ScrollWindowEx (USER32.@)
1617 * Note: contrary to what the doc says, pixels that are scrolled from the
1618 * outside of clipRect to the inside are NOT painted.
1621 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1622 const RECT *rect, const RECT *clipRect,
1623 HRGN hrgnUpdate, LPRECT rcUpdate,
1624 UINT flags )
1626 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1629 /*************************************************************************
1630 * ScrollWindow (USER32.@)
1633 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1634 const RECT *rect, const RECT *clipRect )
1636 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1637 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1641 /*************************************************************************
1642 * ScrollDC (USER32.@)
1644 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1645 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1646 * logical coordinates.
1648 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
1649 HRGN ret_update_rgn, LPRECT update_rect )
1652 HRGN update_rgn = ret_update_rgn;
1653 RECT src_rect, clip_rect, offset;
1654 INT dxdev, dydev;
1655 HRGN dstrgn, cliprgn, visrgn;
1656 BOOL ret;
1658 TRACE( "dx,dy %d,%d scroll %s clip %s update %p rect %p\n",
1659 dx, dy, wine_dbgstr_rect(scroll), wine_dbgstr_rect(clip), ret_update_rgn, update_rect );
1661 /* get the visible region */
1662 visrgn = CreateRectRgn( 0, 0, 0, 0 );
1663 GetRandomRgn( hdc, visrgn, SYSRGN );
1664 if (!(GetVersion() & 0x80000000))
1666 POINT org;
1667 GetDCOrgEx( hdc, &org );
1668 OffsetRgn( visrgn, -org.x, -org.y );
1671 /* intersect with the clipping region if the DC has one */
1672 cliprgn = CreateRectRgn( 0, 0, 0, 0);
1673 if (GetClipRgn( hdc, cliprgn ) != 1)
1675 DeleteObject( cliprgn );
1676 cliprgn = 0;
1678 else CombineRgn( visrgn, visrgn, cliprgn, RGN_AND );
1680 /* only those pixels in the scroll rectangle that remain in the clipping
1681 * rect are scrolled. */
1682 if (clip)
1683 clip_rect = *clip;
1684 else
1685 GetClipBox( hdc, &clip_rect );
1686 src_rect = clip_rect;
1687 OffsetRect( &clip_rect, -dx, -dy );
1688 IntersectRect( &src_rect, &src_rect, &clip_rect );
1690 /* if an scroll rectangle is specified, only the pixels within that
1691 * rectangle are scrolled */
1692 if (scroll) IntersectRect( &src_rect, &src_rect, scroll );
1694 /* now convert to device coordinates */
1695 LPtoDP( hdc, (LPPOINT)&src_rect, 2 );
1696 TRACE( "source rect: %s\n", wine_dbgstr_rect(&src_rect) );
1697 /* also dx and dy */
1698 SetRect( &offset, 0, 0, dx, dy );
1699 LPtoDP( hdc, (LPPOINT)&offset, 2 );
1700 dxdev = offset.right - offset.left;
1701 dydev = offset.bottom - offset.top;
1703 /* now intersect with the visible region to get the pixels that will actually scroll */
1704 dstrgn = CreateRectRgnIndirect( &src_rect );
1705 CombineRgn( dstrgn, dstrgn, visrgn, RGN_AND );
1706 OffsetRgn( dstrgn, dxdev, dydev );
1707 ExtSelectClipRgn( hdc, dstrgn, RGN_AND );
1709 /* compute the update areas. This is the combined clip rectangle
1710 * minus the scrolled region, and intersected with the visible region. */
1711 if (ret_update_rgn || update_rect)
1713 /* intersect clip and scroll rectangles, allowing NULL values */
1714 if (scroll)
1716 if (clip)
1717 IntersectRect( &clip_rect, clip, scroll );
1718 else
1719 clip_rect = *scroll;
1721 else if (clip)
1722 clip_rect = *clip;
1723 else
1724 GetClipBox( hdc, &clip_rect );
1726 /* Convert the combined clip rectangle to device coordinates */
1727 LPtoDP( hdc, (LPPOINT)&clip_rect, 2 );
1728 if (update_rgn)
1729 SetRectRgn( update_rgn, clip_rect.left, clip_rect.top, clip_rect.right, clip_rect.bottom );
1730 else
1731 update_rgn = CreateRectRgnIndirect( &clip_rect );
1733 CombineRgn( update_rgn, update_rgn, visrgn, RGN_AND );
1734 CombineRgn( update_rgn, update_rgn, dstrgn, RGN_DIFF );
1737 ret = USER_Driver->pScrollDC( hdc, dx, dy, update_rgn );
1739 if (ret && update_rect)
1741 GetRgnBox( update_rgn, update_rect );
1742 DPtoLP( hdc, (LPPOINT)update_rect, 2 );
1743 TRACE( "returning update_rect %s\n", wine_dbgstr_rect(update_rect) );
1745 if (!ret_update_rgn) DeleteObject( update_rgn );
1746 SelectClipRgn( hdc, cliprgn );
1747 if (cliprgn) DeleteObject( cliprgn );
1748 DeleteObject( visrgn );
1749 DeleteObject( dstrgn );
1750 return ret;
1753 /************************************************************************
1754 * PrintWindow (USER32.@)
1757 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1759 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1760 if(!(nFlags & PW_CLIENTONLY))
1762 flags |= PRF_NONCLIENT;
1764 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1765 return TRUE;