user32: Fix caret disappearing after scrolling.
[wine.git] / dlls / user32 / painting.c
blob622dcad845f32be7cd8751325eb761aafd129ad0
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 "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "wine/server.h"
36 #include "win.h"
37 #include "user_private.h"
38 #include "controls.h"
39 #include "wine/gdi_driver.h"
40 #include "wine/list.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(win);
46 struct dce
48 struct list entry; /* entry in global DCE list */
49 HDC hdc;
50 HWND hwnd;
51 HRGN clip_rgn;
52 DWORD flags;
53 LONG count; /* usage count; 0 or 1 for cache DCEs, always 1 for window DCEs,
54 always >= 1 for class DCEs */
57 static struct list dce_list = LIST_INIT(dce_list);
59 #define DCE_CACHE_SIZE 64
61 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
63 static const WCHAR displayW[] = { 'D','I','S','P','L','A','Y',0 };
66 /***********************************************************************
67 * dump_rdw_flags
69 static void dump_rdw_flags(UINT flags)
71 TRACE("flags:");
72 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
73 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
74 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
75 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
76 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
77 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
78 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
79 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
80 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
81 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
82 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
83 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
85 #define RDW_FLAGS \
86 (RDW_INVALIDATE | \
87 RDW_INTERNALPAINT | \
88 RDW_ERASE | \
89 RDW_VALIDATE | \
90 RDW_NOINTERNALPAINT | \
91 RDW_NOERASE | \
92 RDW_NOCHILDREN | \
93 RDW_ALLCHILDREN | \
94 RDW_UPDATENOW | \
95 RDW_ERASENOW | \
96 RDW_FRAME | \
97 RDW_NOFRAME)
99 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
100 TRACE("\n");
101 #undef RDW_FLAGS
105 /***********************************************************************
106 * update_visible_region
108 * Set the visible region and X11 drawable for the DC associated to
109 * a given window.
111 static void update_visible_region( struct dce *dce )
113 struct window_surface *surface = NULL;
114 NTSTATUS status;
115 HRGN vis_rgn = 0;
116 HWND top_win = 0;
117 DWORD flags = dce->flags;
118 DWORD paint_flags = 0;
119 size_t size = 256;
120 RECT win_rect, top_rect;
121 WND *win;
123 /* don't clip siblings if using parent clip region */
124 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
126 /* fetch the visible region from the server */
129 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
130 if (!data) return;
132 SERVER_START_REQ( get_visible_region )
134 req->window = wine_server_user_handle( dce->hwnd );
135 req->flags = flags;
136 wine_server_set_reply( req, data->Buffer, size );
137 if (!(status = wine_server_call( req )))
139 size_t reply_size = wine_server_reply_size( reply );
140 data->rdh.dwSize = sizeof(data->rdh);
141 data->rdh.iType = RDH_RECTANGLES;
142 data->rdh.nCount = reply_size / sizeof(RECT);
143 data->rdh.nRgnSize = reply_size;
144 vis_rgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
146 top_win = wine_server_ptr_handle( reply->top_win );
147 win_rect.left = reply->win_rect.left;
148 win_rect.top = reply->win_rect.top;
149 win_rect.right = reply->win_rect.right;
150 win_rect.bottom = reply->win_rect.bottom;
151 top_rect.left = reply->top_rect.left;
152 top_rect.top = reply->top_rect.top;
153 top_rect.right = reply->top_rect.right;
154 top_rect.bottom = reply->top_rect.bottom;
155 paint_flags = reply->paint_flags;
157 else size = reply->total_size;
159 SERVER_END_REQ;
160 HeapFree( GetProcessHeap(), 0, data );
161 } while (status == STATUS_BUFFER_OVERFLOW);
163 if (status || !vis_rgn) return;
165 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
167 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
168 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
170 /* don't use a surface to paint the client area of OpenGL windows */
171 if (!(paint_flags & SET_WINPOS_PIXEL_FORMAT) || (flags & DCX_WINDOW))
173 win = WIN_GetPtr( top_win );
174 if (win && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
176 surface = win->surface;
177 if (surface) window_surface_add_ref( surface );
178 WIN_ReleasePtr( win );
182 if (!surface) top_rect = get_virtual_screen_rect();
183 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
184 if (surface) window_surface_release( surface );
188 /***********************************************************************
189 * release_dce
191 static void release_dce( struct dce *dce )
193 if (!dce->hwnd) return; /* already released */
195 __wine_set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
196 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
198 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
199 dce->clip_rgn = 0;
200 dce->hwnd = 0;
201 dce->flags &= DCX_CACHE;
205 /***********************************************************************
206 * delete_clip_rgn
208 static void delete_clip_rgn( struct dce *dce )
210 if (!dce->clip_rgn) return; /* nothing to do */
212 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
213 DeleteObject( dce->clip_rgn );
214 dce->clip_rgn = 0;
216 /* make it dirty so that the vis rgn gets recomputed next time */
217 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
221 /***********************************************************************
222 * alloc_dce
224 * Allocate a new DCE.
226 static struct dce *alloc_dce(void)
228 struct dce *dce;
230 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
231 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
233 HeapFree( GetProcessHeap(), 0, dce );
234 return 0;
236 dce->hwnd = 0;
237 dce->clip_rgn = 0;
238 dce->flags = 0;
239 dce->count = 1;
241 /* store DCE handle in DC hook data field */
242 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
243 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
244 return dce;
248 /***********************************************************************
249 * get_window_dce
251 static struct dce *get_window_dce( HWND hwnd )
253 struct dce *dce;
254 WND *win = WIN_GetPtr( hwnd );
256 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
258 dce = win->dce;
259 if (!dce && (dce = get_class_dce( win->class )))
261 win->dce = dce;
262 dce->count++;
264 WIN_ReleasePtr( win );
266 if (!dce) /* try to allocate one */
268 struct dce *dce_to_free = NULL;
269 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
271 if (class_style & CS_CLASSDC)
273 if (!(dce = alloc_dce())) return NULL;
275 win = WIN_GetPtr( hwnd );
276 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
278 if (win->dce) /* another thread beat us to it */
280 dce_to_free = dce;
281 dce = win->dce;
283 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
285 dce_to_free = dce;
286 dce = win->dce;
287 dce->count++;
289 else
291 dce->count++;
292 list_add_tail( &dce_list, &dce->entry );
294 WIN_ReleasePtr( win );
296 else dce_to_free = dce;
298 else if (class_style & CS_OWNDC)
300 if (!(dce = alloc_dce())) return NULL;
302 win = WIN_GetPtr( hwnd );
303 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
305 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
306 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
307 if (win->dce) /* another thread beat us to it */
309 dce_to_free = dce;
310 dce = win->dce;
312 else
314 win->dce = dce;
315 dce->hwnd = hwnd;
316 list_add_tail( &dce_list, &dce->entry );
318 WIN_ReleasePtr( win );
320 else dce_to_free = dce;
323 if (dce_to_free)
325 SetDCHook( dce_to_free->hdc, NULL, 0 );
326 DeleteDC( dce_to_free->hdc );
327 HeapFree( GetProcessHeap(), 0, dce_to_free );
328 if (dce_to_free == dce)
329 dce = NULL;
332 return dce;
336 /***********************************************************************
337 * free_dce
339 * Free a class or window DCE.
341 void free_dce( struct dce *dce, HWND hwnd )
343 struct dce *dce_to_free = NULL;
345 USER_Lock();
347 if (dce)
349 if (!--dce->count)
351 release_dce( dce );
352 list_remove( &dce->entry );
353 dce_to_free = dce;
355 else if (dce->hwnd == hwnd)
357 release_dce( dce );
361 /* now check for cache DCEs */
363 if (hwnd)
365 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
367 if (dce->hwnd != hwnd) continue;
368 if (!(dce->flags & DCX_CACHE)) break;
370 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
371 dce->count = 0;
372 release_dce( dce );
376 USER_Unlock();
378 if (dce_to_free)
380 SetDCHook( dce_to_free->hdc, NULL, 0 );
381 DeleteDC( dce_to_free->hdc );
382 HeapFree( GetProcessHeap(), 0, dce_to_free );
387 /***********************************************************************
388 * make_dc_dirty
390 * Mark the associated DC as dirty to force a refresh of the visible region
392 static void make_dc_dirty( struct dce *dce )
394 if (!dce->count)
396 /* Don't bother with visible regions of unused DCEs */
397 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
398 release_dce( dce );
400 else
402 /* Set dirty bits in the hDC and DCE structs */
403 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
404 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
409 /***********************************************************************
410 * invalidate_dce
412 * It is called from SetWindowPos() - we have to
413 * mark as dirty all busy DCEs for windows that have pWnd->parent as
414 * an ancestor and whose client rect intersects with specified update
415 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
416 * DCX_CLIPCHILDREN flag is set.
418 void invalidate_dce( WND *win, const RECT *extra_rect )
420 RECT window_rect;
421 struct dce *dce;
423 if (!win->parent) return;
425 GetWindowRect( win->obj.handle, &window_rect );
427 TRACE("%p parent %p %s (%s)\n",
428 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
430 /* walk all DCEs and fixup non-empty entries */
432 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
434 if (!dce->hwnd) continue;
436 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
437 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
439 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
440 continue; /* child window positions don't bother us */
442 /* if DCE window is a child of hwnd, it has to be invalidated */
443 if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
445 make_dc_dirty( dce );
446 continue;
449 /* otherwise check if the window rectangle intersects this DCE window */
450 if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
452 RECT dce_rect, tmp;
453 GetWindowRect( dce->hwnd, &dce_rect );
454 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
455 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
456 make_dc_dirty( dce );
461 /***********************************************************************
462 * release_dc
464 * Implementation of ReleaseDC.
466 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
468 struct dce *dce;
469 BOOL ret = FALSE;
471 TRACE("%p %p\n", hwnd, hdc );
473 USER_Lock();
474 dce = (struct dce *)GetDCHook( hdc, NULL );
475 if (dce && dce->count)
477 if (!(dce->flags & DCX_NORESETATTRS)) SetHookFlags( dce->hdc, DCHF_RESETDC );
478 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
479 if (dce->flags & DCX_CACHE) dce->count = 0;
480 ret = TRUE;
482 USER_Unlock();
483 return ret;
487 /***********************************************************************
488 * dc_hook
490 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
492 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
494 BOOL retv = TRUE;
495 struct dce *dce = (struct dce *)data;
497 TRACE("hDC = %p, %u\n", hDC, code);
499 if (!dce) return FALSE;
500 assert( dce->hdc == hDC );
502 switch( code )
504 case DCHC_INVALIDVISRGN:
505 /* GDI code calls this when it detects that the
506 * DC is dirty (usually after SetHookFlags()). This
507 * means that we have to recompute the visible region.
509 if (dce->count) update_visible_region( dce );
510 else /* non-fatal but shouldn't happen */
511 WARN("DC is not in use!\n");
512 break;
513 case DCHC_DELETEDC:
514 USER_Lock();
515 if (!(dce->flags & DCX_CACHE))
517 WARN("Application trying to delete an owned DC %p\n", dce->hdc);
518 retv = FALSE;
520 else
522 list_remove( &dce->entry );
523 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
524 HeapFree( GetProcessHeap(), 0, dce );
526 USER_Unlock();
527 break;
529 return retv;
533 /***********************************************************************
534 * get_update_region
536 * Return update region (in screen coordinates) for a window.
538 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
540 HRGN hrgn = 0;
541 NTSTATUS status;
542 RGNDATA *data;
543 size_t size = 256;
547 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
549 SetLastError( ERROR_OUTOFMEMORY );
550 return 0;
553 SERVER_START_REQ( get_update_region )
555 req->window = wine_server_user_handle( hwnd );
556 req->from_child = wine_server_user_handle( child ? *child : 0 );
557 req->flags = *flags;
558 wine_server_set_reply( req, data->Buffer, size );
559 if (!(status = wine_server_call( req )))
561 size_t reply_size = wine_server_reply_size( reply );
562 data->rdh.dwSize = sizeof(data->rdh);
563 data->rdh.iType = RDH_RECTANGLES;
564 data->rdh.nCount = reply_size / sizeof(RECT);
565 data->rdh.nRgnSize = reply_size;
566 hrgn = ExtCreateRegion( NULL, data->rdh.dwSize + data->rdh.nRgnSize, data );
567 if (child) *child = wine_server_ptr_handle( reply->child );
568 *flags = reply->flags;
570 else size = reply->total_size;
572 SERVER_END_REQ;
573 HeapFree( GetProcessHeap(), 0, data );
574 } while (status == STATUS_BUFFER_OVERFLOW);
576 if (status) SetLastError( RtlNtStatusToDosError(status) );
577 return hrgn;
581 /***********************************************************************
582 * get_update_flags
584 * Get only the update flags, not the update region.
586 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
588 BOOL ret;
590 SERVER_START_REQ( get_update_region )
592 req->window = wine_server_user_handle( hwnd );
593 req->from_child = wine_server_user_handle( child ? *child : 0 );
594 req->flags = *flags | UPDATE_NOREGION;
595 if ((ret = !wine_server_call_err( req )))
597 if (child) *child = wine_server_ptr_handle( reply->child );
598 *flags = reply->flags;
601 SERVER_END_REQ;
602 return ret;
606 /***********************************************************************
607 * redraw_window_rects
609 * Redraw part of a window.
611 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
613 BOOL ret;
615 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
616 return TRUE; /* nothing to do */
618 SERVER_START_REQ( redraw_window )
620 req->window = wine_server_user_handle( hwnd );
621 req->flags = flags;
622 wine_server_add_data( req, rects, count * sizeof(RECT) );
623 ret = !wine_server_call_err( req );
625 SERVER_END_REQ;
626 return ret;
630 /***********************************************************************
631 * send_ncpaint
633 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
634 * Helper for erase_now and BeginPaint.
636 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
638 HRGN whole_rgn = get_update_region( hwnd, flags, child );
639 HRGN client_rgn = 0;
641 if (child) hwnd = *child;
643 if (hwnd == GetDesktopWindow()) return whole_rgn;
645 if (whole_rgn)
647 RECT client, update;
648 INT type;
650 /* check if update rgn overlaps with nonclient area */
651 type = GetRgnBox( whole_rgn, &update );
652 WIN_GetRectangles( hwnd, COORDS_SCREEN, 0, &client );
654 if ((*flags & UPDATE_NONCLIENT) ||
655 update.left < client.left || update.top < client.top ||
656 update.right > client.right || update.bottom > client.bottom)
658 client_rgn = CreateRectRgnIndirect( &client );
659 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
661 /* check if update rgn contains complete nonclient area */
662 if (type == SIMPLEREGION)
664 RECT window;
665 GetWindowRect( hwnd, &window );
666 if (EqualRect( &window, &update ))
668 DeleteObject( whole_rgn );
669 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 );
685 return client_rgn;
689 /***********************************************************************
690 * send_erase
692 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
693 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
694 * Helper for erase_now and BeginPaint.
696 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
697 RECT *clip_rect, HDC *hdc_ret )
699 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
700 HDC hdc = 0;
701 RECT dummy;
703 if (!clip_rect) clip_rect = &dummy;
704 if (hdc_ret || (flags & UPDATE_ERASE))
706 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
707 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
709 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
711 INT type = GetClipBox( hdc, clip_rect );
713 if (flags & UPDATE_ERASE)
715 /* don't erase if the clip box is empty */
716 if (type != NULLREGION)
717 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
719 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
722 if (hdc_ret) *hdc_ret = hdc;
724 if (!hdc) DeleteObject( client_rgn );
725 return need_erase;
729 /***********************************************************************
730 * erase_now
732 * Implementation of RDW_ERASENOW behavior.
734 void erase_now( HWND hwnd, UINT rdw_flags )
736 HWND child = 0;
737 HRGN hrgn;
738 BOOL need_erase = FALSE;
740 /* loop while we find a child to repaint */
741 for (;;)
743 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
745 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
746 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
747 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
749 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
750 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
752 if (!flags) break; /* nothing more to do */
753 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
758 /***********************************************************************
759 * move_window_bits
761 * Move the window bits when a window is resized or its surface recreated.
763 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
764 struct window_surface *new_surface,
765 const RECT *visible_rect, const RECT *old_visible_rect,
766 const RECT *client_rect, const RECT *valid_rects )
768 RECT dst = valid_rects[0];
769 RECT src = valid_rects[1];
771 if (new_surface != old_surface ||
772 src.left - old_visible_rect->left != dst.left - visible_rect->left ||
773 src.top - old_visible_rect->top != dst.top - visible_rect->top)
775 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
776 BITMAPINFO *info = (BITMAPINFO *)buffer;
777 void *bits;
778 UINT flags = UPDATE_NOCHILDREN;
779 HRGN rgn = get_update_region( hwnd, &flags, NULL );
780 HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_EXCLUDERGN );
782 OffsetRect( &dst, -client_rect->left, -client_rect->top );
783 TRACE( "copying %s -> %s\n", wine_dbgstr_rect(&src), wine_dbgstr_rect(&dst) );
784 bits = old_surface->funcs->get_info( old_surface, info );
785 old_surface->funcs->lock( old_surface );
786 SetDIBitsToDevice( hdc, dst.left, dst.top, dst.right - dst.left, dst.bottom - dst.top,
787 src.left - old_visible_rect->left - old_surface->rect.left,
788 old_surface->rect.bottom - (src.bottom - old_visible_rect->top),
789 0, old_surface->rect.bottom - old_surface->rect.top,
790 bits, info, DIB_RGB_COLORS );
791 old_surface->funcs->unlock( old_surface );
792 ReleaseDC( hwnd, hdc );
797 /***********************************************************************
798 * update_now
800 * Implementation of RDW_UPDATENOW behavior.
802 static void update_now( HWND hwnd, UINT rdw_flags )
804 HWND child = 0;
806 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
807 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
809 /* loop while we find a child to repaint */
810 for (;;)
812 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
814 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
815 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
817 if (!get_update_flags( hwnd, &child, &flags )) break;
818 if (!flags) break; /* nothing more to do */
820 SendMessageW( child, WM_PAINT, 0, 0 );
821 if (rdw_flags & RDW_NOCHILDREN) break;
826 /*************************************************************************
827 * fix_caret
829 * Helper for ScrollWindowEx:
830 * If the return value is 0, no special caret handling is necessary.
831 * Otherwise the return value is the handle of the window that owns the
832 * caret. Its caret needs to be hidden during the scroll operation and
833 * moved to new_caret_pos if move_caret is TRUE.
835 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
836 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
838 GUITHREADINFO info;
839 RECT rect, mapped_rcCaret;
841 info.cbSize = sizeof(info);
842 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
843 if (!info.hwndCaret) return 0;
845 mapped_rcCaret = info.rcCaret;
846 if (info.hwndCaret == hWnd)
848 /* The caret needs to be moved along with scrolling even if it's
849 * outside the visible area. Otherwise, when the caret is scrolled
850 * out from the view, the position won't get updated anymore and
851 * the caret will never scroll back again. */
852 *move_caret = TRUE;
853 new_caret_pos->x = info.rcCaret.left + dx;
854 new_caret_pos->y = info.rcCaret.top + dy;
856 else
858 *move_caret = FALSE;
859 if (!(flags & SW_SCROLLCHILDREN) || !IsChild(hWnd, info.hwndCaret))
860 return 0;
861 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
864 /* If the caret is not in the src/dest rects, all is fine done. */
865 if (!IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
867 rect = *scroll_rect;
868 OffsetRect(&rect, dx, dy);
869 if (!IntersectRect(&rect, &rect, &mapped_rcCaret))
870 return 0;
873 /* Indicate that the caret needs to be updated during the scrolling. */
874 return info.hwndCaret;
878 /***********************************************************************
879 * BeginPaint (USER32.@)
881 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
883 HRGN hrgn;
884 HDC hdc;
885 BOOL erase;
886 RECT rect;
887 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
889 HideCaret( hwnd );
891 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
893 erase = send_erase( hwnd, flags, hrgn, &rect, &hdc );
895 TRACE("hdc = %p box = (%s), fErase = %d\n", hdc, wine_dbgstr_rect(&rect), erase);
897 if (!lps)
899 release_dc( hwnd, hdc, TRUE );
900 return 0;
902 lps->fErase = erase;
903 lps->rcPaint = rect;
904 lps->hdc = hdc;
905 return hdc;
909 /***********************************************************************
910 * EndPaint (USER32.@)
912 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
914 ShowCaret( hwnd );
915 flush_window_surfaces( FALSE );
916 if (!lps) return FALSE;
917 release_dc( hwnd, lps->hdc, TRUE );
918 return TRUE;
922 /***********************************************************************
923 * GetDCEx (USER32.@)
925 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
927 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
928 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
929 struct dce *dce;
930 BOOL bUpdateVisRgn = TRUE;
931 HWND parent;
932 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
934 if (!hwnd) hwnd = GetDesktopWindow();
935 else hwnd = WIN_GetFullHandle( hwnd );
937 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
939 if (!IsWindow(hwnd)) return 0;
941 /* fixup flags */
943 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
945 if (flags & DCX_USESTYLE)
947 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
949 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
951 if (!(flags & DCX_WINDOW))
953 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
955 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
956 flags |= DCX_CLIPCHILDREN;
960 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
962 parent = GetAncestor( hwnd, GA_PARENT );
963 if (!parent || (parent == GetDesktopWindow()))
964 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
966 /* it seems parent clip is ignored when clipping siblings or children */
967 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
969 if( flags & DCX_PARENTCLIP )
971 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
972 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
974 flags &= ~DCX_CLIPCHILDREN;
975 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
979 /* find a suitable DCE */
981 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
983 struct dce *dceEmpty = NULL, *dceUnused = NULL, *found = NULL;
984 unsigned int count = 0;
986 /* Strategy: First, we attempt to find a non-empty but unused DCE with
987 * compatible flags. Next, we look for an empty entry. If the cache is
988 * full we have to purge one of the unused entries.
990 USER_Lock();
991 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
993 if (!(dce->flags & DCX_CACHE)) break;
994 count++;
995 if (dce->count) continue;
996 dceUnused = dce;
997 if (!dce->hwnd) dceEmpty = dce;
998 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1000 TRACE( "found valid %p dce [%p], flags %08x\n", dce, hwnd, dce->flags );
1001 found = dce;
1002 bUpdateVisRgn = FALSE;
1003 break;
1006 if (!found) found = dceEmpty;
1007 if (!found && count >= DCE_CACHE_SIZE) found = dceUnused;
1009 dce = found;
1010 if (dce) dce->count = 1;
1012 USER_Unlock();
1014 /* if there's no dce empty or unused, allocate a new one */
1015 if (!dce)
1017 if (!(dce = alloc_dce())) return 0;
1018 dce->flags = DCX_CACHE;
1019 USER_Lock();
1020 list_add_head( &dce_list, &dce->entry );
1021 USER_Unlock();
1024 else
1026 flags |= DCX_NORESETATTRS;
1027 if (dce->hwnd == hwnd)
1029 TRACE("\tskipping hVisRgn update\n");
1030 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1032 else
1034 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1035 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1036 dce->clip_rgn = 0;
1040 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1042 /* if the extra clip region has changed, get rid of the old one */
1043 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1044 delete_clip_rgn( dce );
1045 dce->clip_rgn = hrgnClip;
1046 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1047 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1048 bUpdateVisRgn = TRUE;
1051 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1053 dce->hwnd = hwnd;
1054 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1056 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1058 if (bUpdateVisRgn) update_visible_region( dce );
1060 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1061 return dce->hdc;
1065 /***********************************************************************
1066 * GetDC (USER32.@)
1068 * Get a device context.
1070 * RETURNS
1071 * Success: Handle to the device context
1072 * Failure: NULL.
1074 HDC WINAPI GetDC( HWND hwnd )
1076 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1077 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1081 /***********************************************************************
1082 * GetWindowDC (USER32.@)
1084 HDC WINAPI GetWindowDC( HWND hwnd )
1086 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1090 /***********************************************************************
1091 * ReleaseDC (USER32.@)
1093 * Release a device context.
1095 * RETURNS
1096 * Success: Non-zero. Resources used by hdc are released.
1097 * Failure: 0.
1099 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1101 return release_dc( hwnd, hdc, FALSE );
1105 /**********************************************************************
1106 * WindowFromDC (USER32.@)
1108 HWND WINAPI WindowFromDC( HDC hdc )
1110 struct dce *dce;
1111 HWND hwnd = 0;
1113 USER_Lock();
1114 dce = (struct dce *)GetDCHook( hdc, NULL );
1115 if (dce) hwnd = dce->hwnd;
1116 USER_Unlock();
1117 return hwnd;
1121 /***********************************************************************
1122 * LockWindowUpdate (USER32.@)
1124 * Enables or disables painting in the chosen window.
1126 * PARAMS
1127 * hwnd [I] handle to a window.
1129 * RETURNS
1130 * If successful, returns nonzero value. Otherwise,
1131 * returns 0.
1133 * NOTES
1134 * You can lock only one window at a time.
1136 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1138 static HWND lockedWnd;
1140 FIXME("(%p), partial stub!\n",hwnd);
1142 USER_Lock();
1143 if (lockedWnd)
1145 if (!hwnd)
1147 /* Unlock lockedWnd */
1148 /* FIXME: Do something */
1150 else
1152 /* Attempted to lock a second window */
1153 /* Return FALSE and do nothing */
1154 USER_Unlock();
1155 return FALSE;
1158 lockedWnd = hwnd;
1159 USER_Unlock();
1160 return TRUE;
1164 /***********************************************************************
1165 * RedrawWindow (USER32.@)
1167 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1169 static const RECT empty;
1170 BOOL ret;
1172 if (!hwnd) hwnd = GetDesktopWindow();
1174 if (TRACE_ON(win))
1176 if (hrgn)
1178 RECT r;
1179 GetRgnBox( hrgn, &r );
1180 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1182 else if (rect)
1183 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1184 else
1185 TRACE( "%p whole window ", hwnd );
1187 dump_rdw_flags(flags);
1190 /* process pending expose events before painting */
1191 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1193 if (rect && !hrgn)
1195 if (IsRectEmpty( rect )) rect = &empty;
1196 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1198 else if (!hrgn)
1200 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1202 else /* need to build a list of the region rectangles */
1204 DWORD size;
1205 RGNDATA *data;
1207 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1208 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1209 GetRegionData( hrgn, size, data );
1210 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1211 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1212 else
1213 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1214 HeapFree( GetProcessHeap(), 0, data );
1217 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1218 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1220 return ret;
1224 /***********************************************************************
1225 * UpdateWindow (USER32.@)
1227 BOOL WINAPI UpdateWindow( HWND hwnd )
1229 if (!hwnd)
1231 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1232 return FALSE;
1235 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1239 /***********************************************************************
1240 * InvalidateRgn (USER32.@)
1242 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1244 if (!hwnd)
1246 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1247 return FALSE;
1250 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1254 /***********************************************************************
1255 * InvalidateRect (USER32.@)
1257 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1258 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1260 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1262 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1264 if (!hwnd)
1266 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1267 rect = NULL;
1270 return RedrawWindow( hwnd, rect, 0, flags );
1274 /***********************************************************************
1275 * ValidateRgn (USER32.@)
1277 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1279 if (!hwnd)
1281 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1282 return FALSE;
1285 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1289 /***********************************************************************
1290 * ValidateRect (USER32.@)
1292 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1293 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1295 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1297 UINT flags = RDW_VALIDATE;
1299 if (!hwnd)
1301 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1302 rect = NULL;
1305 return RedrawWindow( hwnd, rect, 0, flags );
1309 /***********************************************************************
1310 * GetUpdateRgn (USER32.@)
1312 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1314 INT retval = ERROR;
1315 UINT flags = UPDATE_NOCHILDREN;
1316 HRGN update_rgn;
1318 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1320 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1322 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1323 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1325 flags = UPDATE_DELAYED_ERASE;
1326 get_update_flags( hwnd, NULL, &flags );
1328 /* map region to client coordinates */
1329 map_window_region( 0, hwnd, hrgn );
1331 return retval;
1335 /***********************************************************************
1336 * GetUpdateRect (USER32.@)
1338 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1340 UINT flags = UPDATE_NOCHILDREN;
1341 HRGN update_rgn;
1342 BOOL need_erase;
1344 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1346 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1348 if (rect)
1350 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1352 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1353 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1354 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1355 DPtoLP( hdc, (LPPOINT)rect, 2 );
1356 SetLayout( hdc, layout );
1357 ReleaseDC( hwnd, hdc );
1360 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1362 /* check if we still have an update region */
1363 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1364 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1365 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1369 /***********************************************************************
1370 * ExcludeUpdateRgn (USER32.@)
1372 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1374 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1375 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1377 if (ret != ERROR)
1379 POINT pt;
1381 GetDCOrgEx( hdc, &pt );
1382 MapWindowPoints( 0, hwnd, &pt, 1 );
1383 OffsetRgn( update_rgn, -pt.x, -pt.y );
1384 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1386 DeleteObject( update_rgn );
1387 return ret;
1391 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1392 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1394 INT retVal = NULLREGION;
1395 BOOL bOwnRgn = TRUE;
1396 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1397 int rdw_flags;
1398 HRGN hrgnTemp;
1399 HRGN hrgnWinupd = 0;
1400 HDC hDC;
1401 RECT rc, cliprc;
1402 HWND hwndCaret = NULL;
1403 BOOL moveCaret = FALSE;
1404 POINT newCaretPos;
1406 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1407 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1408 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1409 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1410 FIXME("some flags (%04x) are unhandled\n", flags);
1412 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1413 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1415 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1416 hwnd = WIN_GetFullHandle( hwnd );
1418 GetClientRect(hwnd, &rc);
1420 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1421 else cliprc = rc;
1423 if (rect) IntersectRect(&rc, &rc, rect);
1425 if( hrgnUpdate ) bOwnRgn = FALSE;
1426 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1428 newCaretPos.x = newCaretPos.y = 0;
1430 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1431 DWORD dcxflags = 0;
1432 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1434 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1435 if (hwndCaret)
1436 HideCaret(hwndCaret);
1438 if (is_ex) dcxflags |= DCX_CACHE;
1439 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1440 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1441 dcxflags |= DCX_PARENTCLIP;
1442 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1443 dcxflags |= DCX_CLIPCHILDREN;
1444 hDC = GetDCEx( hwnd, 0, dcxflags);
1445 if (hDC)
1447 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1449 ReleaseDC( hwnd, hDC );
1451 if (!bUpdate)
1452 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1455 /* If the windows has an update region, this must be
1456 * scrolled as well. Keep a copy in hrgnWinupd
1457 * to be added to hrngUpdate at the end. */
1458 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1459 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1460 if (retVal != NULLREGION)
1462 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1463 if( !bOwnRgn) {
1464 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1465 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1467 OffsetRgn( hrgnTemp, dx, dy );
1468 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1469 if( !bOwnRgn)
1470 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1471 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1473 /* Catch the case where the scrolling amount exceeds the size of the
1474 * original window. This generated a second update area that is the
1475 * location where the original scrolled content would end up.
1476 * This second region is not returned by the ScrollDC and sets
1477 * ScrollWindowEx apart from just a ScrollDC.
1479 * This has been verified with testing on windows.
1481 if (abs(dx) > abs(rc.right - rc.left) ||
1482 abs(dy) > abs(rc.bottom - rc.top))
1484 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1485 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1486 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1488 if (rcUpdate)
1490 RECT rcTemp;
1491 GetRgnBox( hrgnTemp, &rcTemp );
1492 UnionRect( rcUpdate, rcUpdate, &rcTemp );
1495 if( !bOwnRgn)
1496 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1498 DeleteObject( hrgnClip );
1500 DeleteObject( hrgnTemp );
1501 } else {
1502 /* nothing was scrolled */
1503 if( !bOwnRgn)
1504 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1505 SetRectEmpty( rcUpdate);
1508 if( flags & SW_SCROLLCHILDREN )
1510 HWND *list = WIN_ListChildren( hwnd );
1511 if (list)
1513 int i;
1514 RECT r, dummy;
1515 for (i = 0; list[i]; i++)
1517 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1518 if (!rect || IntersectRect(&dummy, &r, rect))
1519 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1520 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1521 SWP_NOREDRAW | SWP_DEFERERASE );
1523 HeapFree( GetProcessHeap(), 0, list );
1527 if( flags & (SW_INVALIDATE | SW_ERASE) )
1528 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1529 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1531 if( hrgnWinupd) {
1532 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1533 DeleteObject( hrgnWinupd);
1536 if( moveCaret )
1537 SetCaretPos( newCaretPos.x, newCaretPos.y );
1538 if( hwndCaret )
1539 ShowCaret( hwndCaret );
1541 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1543 return retVal;
1547 /*************************************************************************
1548 * ScrollWindowEx (USER32.@)
1550 * Note: contrary to what the doc says, pixels that are scrolled from the
1551 * outside of clipRect to the inside are NOT painted.
1554 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1555 const RECT *rect, const RECT *clipRect,
1556 HRGN hrgnUpdate, LPRECT rcUpdate,
1557 UINT flags )
1559 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1562 /*************************************************************************
1563 * ScrollWindow (USER32.@)
1566 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1567 const RECT *rect, const RECT *clipRect )
1569 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1570 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1574 /*************************************************************************
1575 * ScrollDC (USER32.@)
1577 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1578 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1579 * logical coordinates.
1581 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
1582 HRGN ret_update_rgn, LPRECT update_rect )
1585 HRGN update_rgn = ret_update_rgn;
1586 RECT src_rect, clip_rect, offset;
1587 INT dxdev, dydev;
1588 HRGN dstrgn, cliprgn, visrgn;
1589 BOOL ret;
1591 TRACE( "dx,dy %d,%d scroll %s clip %s update %p rect %p\n",
1592 dx, dy, wine_dbgstr_rect(scroll), wine_dbgstr_rect(clip), ret_update_rgn, update_rect );
1594 /* get the visible region */
1595 visrgn = CreateRectRgn( 0, 0, 0, 0 );
1596 GetRandomRgn( hdc, visrgn, SYSRGN );
1597 if (!(GetVersion() & 0x80000000))
1599 POINT org;
1600 GetDCOrgEx( hdc, &org );
1601 OffsetRgn( visrgn, -org.x, -org.y );
1604 /* intersect with the clipping region if the DC has one */
1605 cliprgn = CreateRectRgn( 0, 0, 0, 0);
1606 if (GetClipRgn( hdc, cliprgn ) != 1)
1608 DeleteObject( cliprgn );
1609 cliprgn = 0;
1611 else CombineRgn( visrgn, visrgn, cliprgn, RGN_AND );
1613 /* only those pixels in the scroll rectangle that remain in the clipping
1614 * rect are scrolled. */
1615 if (clip)
1616 clip_rect = *clip;
1617 else
1618 GetClipBox( hdc, &clip_rect );
1619 src_rect = clip_rect;
1620 OffsetRect( &clip_rect, -dx, -dy );
1621 IntersectRect( &src_rect, &src_rect, &clip_rect );
1623 /* if an scroll rectangle is specified, only the pixels within that
1624 * rectangle are scrolled */
1625 if (scroll) IntersectRect( &src_rect, &src_rect, scroll );
1627 /* now convert to device coordinates */
1628 LPtoDP( hdc, (LPPOINT)&src_rect, 2 );
1629 TRACE( "source rect: %s\n", wine_dbgstr_rect(&src_rect) );
1630 /* also dx and dy */
1631 SetRect( &offset, 0, 0, dx, dy );
1632 LPtoDP( hdc, (LPPOINT)&offset, 2 );
1633 dxdev = offset.right - offset.left;
1634 dydev = offset.bottom - offset.top;
1636 /* now intersect with the visible region to get the pixels that will actually scroll */
1637 dstrgn = CreateRectRgnIndirect( &src_rect );
1638 CombineRgn( dstrgn, dstrgn, visrgn, RGN_AND );
1639 OffsetRgn( dstrgn, dxdev, dydev );
1640 ExtSelectClipRgn( hdc, dstrgn, RGN_AND );
1642 /* compute the update areas. This is the combined clip rectangle
1643 * minus the scrolled region, and intersected with the visible region. */
1644 if (ret_update_rgn || update_rect)
1646 /* intersect clip and scroll rectangles, allowing NULL values */
1647 if (scroll)
1649 if (clip)
1650 IntersectRect( &clip_rect, clip, scroll );
1651 else
1652 clip_rect = *scroll;
1654 else if (clip)
1655 clip_rect = *clip;
1656 else
1657 GetClipBox( hdc, &clip_rect );
1659 /* Convert the combined clip rectangle to device coordinates */
1660 LPtoDP( hdc, (LPPOINT)&clip_rect, 2 );
1661 if (update_rgn)
1662 SetRectRgn( update_rgn, clip_rect.left, clip_rect.top, clip_rect.right, clip_rect.bottom );
1663 else
1664 update_rgn = CreateRectRgnIndirect( &clip_rect );
1666 CombineRgn( update_rgn, update_rgn, visrgn, RGN_AND );
1667 CombineRgn( update_rgn, update_rgn, dstrgn, RGN_DIFF );
1670 ret = USER_Driver->pScrollDC( hdc, dx, dy, update_rgn );
1672 if (ret && update_rect)
1674 GetRgnBox( update_rgn, update_rect );
1675 DPtoLP( hdc, (LPPOINT)update_rect, 2 );
1676 TRACE( "returning update_rect %s\n", wine_dbgstr_rect(update_rect) );
1678 if (!ret_update_rgn) DeleteObject( update_rgn );
1679 SelectClipRgn( hdc, cliprgn );
1680 if (cliprgn) DeleteObject( cliprgn );
1681 DeleteObject( visrgn );
1682 DeleteObject( dstrgn );
1683 return ret;
1686 /************************************************************************
1687 * PrintWindow (USER32.@)
1690 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1692 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1693 if(!(nFlags & PW_CLIENTONLY))
1695 flags |= PRF_NONCLIENT;
1697 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1698 return TRUE;