user32: Don't use the window surface when the pixel format was set.
[wine.git] / dlls / user32 / painting.c
blob7d106bbcffdd7acb268b5f7d9f2805f594f9fd56
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 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
61 static const WCHAR displayW[] = { 'D','I','S','P','L','A','Y',0 };
64 /***********************************************************************
65 * dump_rdw_flags
67 static void dump_rdw_flags(UINT flags)
69 TRACE("flags:");
70 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
71 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
72 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
73 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
74 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
75 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
76 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
77 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
78 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
79 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
80 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
81 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
83 #define RDW_FLAGS \
84 (RDW_INVALIDATE | \
85 RDW_INTERNALPAINT | \
86 RDW_ERASE | \
87 RDW_VALIDATE | \
88 RDW_NOINTERNALPAINT | \
89 RDW_NOERASE | \
90 RDW_NOCHILDREN | \
91 RDW_ALLCHILDREN | \
92 RDW_UPDATENOW | \
93 RDW_ERASENOW | \
94 RDW_FRAME | \
95 RDW_NOFRAME)
97 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
98 TRACE("\n");
99 #undef RDW_FLAGS
103 /***********************************************************************
104 * update_visible_region
106 * Set the visible region and X11 drawable for the DC associated to
107 * a given window.
109 static void update_visible_region( struct dce *dce )
111 struct window_surface *surface = NULL;
112 NTSTATUS status;
113 HRGN vis_rgn = 0;
114 HWND top_win = 0;
115 DWORD flags = dce->flags;
116 size_t size = 256;
117 RECT win_rect, top_rect;
118 WND *win;
120 /* don't clip siblings if using parent clip region */
121 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
123 /* fetch the visible region from the server */
126 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
127 if (!data) return;
129 SERVER_START_REQ( get_visible_region )
131 req->window = wine_server_user_handle( dce->hwnd );
132 req->flags = flags;
133 wine_server_set_reply( req, data->Buffer, size );
134 if (!(status = wine_server_call( req )))
136 size_t reply_size = wine_server_reply_size( reply );
137 data->rdh.dwSize = sizeof(data->rdh);
138 data->rdh.iType = RDH_RECTANGLES;
139 data->rdh.nCount = reply_size / sizeof(RECT);
140 data->rdh.nRgnSize = reply_size;
141 vis_rgn = ExtCreateRegion( NULL, size, data );
143 top_win = wine_server_ptr_handle( reply->top_win );
144 win_rect.left = reply->win_rect.left;
145 win_rect.top = reply->win_rect.top;
146 win_rect.right = reply->win_rect.right;
147 win_rect.bottom = reply->win_rect.bottom;
148 top_rect.left = reply->top_rect.left;
149 top_rect.top = reply->top_rect.top;
150 top_rect.right = reply->top_rect.right;
151 top_rect.bottom = reply->top_rect.bottom;
153 else size = reply->total_size;
155 SERVER_END_REQ;
156 HeapFree( GetProcessHeap(), 0, data );
157 } while (status == STATUS_BUFFER_OVERFLOW);
159 if (status || !vis_rgn) return;
161 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
163 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
164 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
166 if ((win = WIN_GetPtr( top_win )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
168 /* don't use a surface to paint the client area of OpenGL windows */
169 if (!win->pixel_format || (flags & DCX_WINDOW)) surface = win->surface;
170 if (surface) window_surface_add_ref( surface );
171 WIN_ReleasePtr( win );
174 if (!surface) top_rect = get_virtual_screen_rect();
175 __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
176 if (surface) window_surface_release( surface );
180 /***********************************************************************
181 * reset_dce_attrs
183 static void reset_dce_attrs( struct dce *dce )
185 RestoreDC( dce->hdc, 1 ); /* initial save level is always 1 */
186 SaveDC( dce->hdc ); /* save the state again for next time */
190 /***********************************************************************
191 * release_dce
193 static void release_dce( struct dce *dce )
195 if (!dce->hwnd) return; /* already released */
197 __wine_set_visible_region( dce->hdc, 0, &dummy_surface.rect, &dummy_surface.rect, &dummy_surface );
198 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
200 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
201 dce->clip_rgn = 0;
202 dce->hwnd = 0;
203 dce->flags &= DCX_CACHE;
207 /***********************************************************************
208 * delete_clip_rgn
210 static void delete_clip_rgn( struct dce *dce )
212 if (!dce->clip_rgn) return; /* nothing to do */
214 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
215 DeleteObject( dce->clip_rgn );
216 dce->clip_rgn = 0;
218 /* make it dirty so that the vis rgn gets recomputed next time */
219 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
223 /***********************************************************************
224 * alloc_dce
226 * Allocate a new DCE.
228 static struct dce *alloc_dce(void)
230 struct dce *dce;
232 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
233 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
235 HeapFree( GetProcessHeap(), 0, dce );
236 return 0;
238 SaveDC( dce->hdc );
240 dce->hwnd = 0;
241 dce->clip_rgn = 0;
242 dce->flags = 0;
243 dce->count = 1;
245 /* store DCE handle in DC hook data field */
246 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
247 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
248 return dce;
252 /***********************************************************************
253 * get_window_dce
255 static struct dce *get_window_dce( HWND hwnd )
257 struct dce *dce;
258 WND *win = WIN_GetPtr( hwnd );
260 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
262 dce = win->dce;
263 if (!dce && (dce = get_class_dce( win->class )))
265 win->dce = dce;
266 dce->count++;
268 WIN_ReleasePtr( win );
270 if (!dce) /* try to allocate one */
272 struct dce *dce_to_free = NULL;
273 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
275 if (class_style & CS_CLASSDC)
277 if (!(dce = alloc_dce())) return NULL;
279 win = WIN_GetPtr( hwnd );
280 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
282 if (win->dce) /* another thread beat us to it */
284 dce_to_free = dce;
285 dce = win->dce;
287 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
289 dce_to_free = dce;
290 dce = win->dce;
291 dce->count++;
293 else
295 dce->count++;
296 list_add_tail( &dce_list, &dce->entry );
298 WIN_ReleasePtr( win );
300 else dce_to_free = dce;
302 else if (class_style & CS_OWNDC)
304 if (!(dce = alloc_dce())) return NULL;
306 win = WIN_GetPtr( hwnd );
307 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
309 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
310 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
311 if (win->dce) /* another thread beat us to it */
313 dce_to_free = dce;
314 dce = win->dce;
316 else
318 win->dce = dce;
319 dce->hwnd = hwnd;
320 dce->count++;
321 list_add_tail( &dce_list, &dce->entry );
323 WIN_ReleasePtr( win );
325 else dce_to_free = dce;
328 if (dce_to_free)
330 SetDCHook( dce_to_free->hdc, NULL, 0 );
331 DeleteDC( dce_to_free->hdc );
332 HeapFree( GetProcessHeap(), 0, dce_to_free );
333 if (dce_to_free == dce)
334 dce = NULL;
337 return dce;
341 /***********************************************************************
342 * free_dce
344 * Free a class or window DCE.
346 void free_dce( struct dce *dce, HWND hwnd )
348 USER_Lock();
350 if (dce)
352 if (!--dce->count)
354 /* turn it into a cache entry */
355 reset_dce_attrs( dce );
356 release_dce( dce );
357 dce->flags |= DCX_CACHE;
359 else if (dce->hwnd == hwnd)
361 release_dce( dce );
365 /* now check for cache DCEs */
367 if (hwnd)
369 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
371 if (dce->hwnd != hwnd) continue;
372 if (!(dce->flags & DCX_CACHE)) continue;
374 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
375 dce->count = 0;
376 release_dce( dce );
380 USER_Unlock();
384 /***********************************************************************
385 * make_dc_dirty
387 * Mark the associated DC as dirty to force a refresh of the visible region
389 static void make_dc_dirty( struct dce *dce )
391 if (!dce->count)
393 /* Don't bother with visible regions of unused DCEs */
394 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
395 release_dce( dce );
397 else
399 /* Set dirty bits in the hDC and DCE structs */
400 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
401 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
406 /***********************************************************************
407 * invalidate_dce
409 * It is called from SetWindowPos() - we have to
410 * mark as dirty all busy DCEs for windows that have pWnd->parent as
411 * an ancestor and whose client rect intersects with specified update
412 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
413 * DCX_CLIPCHILDREN flag is set.
415 void invalidate_dce( WND *win, const RECT *extra_rect )
417 RECT window_rect;
418 struct dce *dce;
420 if (!win->parent) return;
422 GetWindowRect( win->obj.handle, &window_rect );
424 TRACE("%p parent %p %s (%s)\n",
425 win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
427 /* walk all DCEs and fixup non-empty entries */
429 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
431 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
432 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
434 if (!dce->hwnd) continue;
435 if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
436 continue; /* child window positions don't bother us */
438 /* if DCE window is a child of hwnd, it has to be invalidated */
439 if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
441 make_dc_dirty( dce );
442 continue;
445 /* otherwise check if the window rectangle intersects this DCE window */
446 if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
448 RECT dce_rect, tmp;
449 GetWindowRect( dce->hwnd, &dce_rect );
450 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
451 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
452 make_dc_dirty( dce );
457 /***********************************************************************
458 * release_dc
460 * Implementation of ReleaseDC.
462 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
464 struct dce *dce;
465 BOOL ret = FALSE;
467 TRACE("%p %p\n", hwnd, hdc );
469 USER_Lock();
470 dce = (struct dce *)GetDCHook( hdc, NULL );
471 if (dce && dce->count)
473 if (!(dce->flags & DCX_NORESETATTRS)) reset_dce_attrs( dce );
474 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
475 if (dce->flags & DCX_CACHE) dce->count = 0;
476 ret = TRUE;
478 USER_Unlock();
479 return ret;
483 /***********************************************************************
484 * dc_hook
486 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
488 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
490 BOOL retv = TRUE;
491 struct dce *dce = (struct dce *)data;
493 TRACE("hDC = %p, %u\n", hDC, code);
495 if (!dce) return 0;
496 assert( dce->hdc == hDC );
498 switch( code )
500 case DCHC_INVALIDVISRGN:
501 /* GDI code calls this when it detects that the
502 * DC is dirty (usually after SetHookFlags()). This
503 * means that we have to recompute the visible region.
505 if (dce->count) update_visible_region( dce );
506 else /* non-fatal but shouldn't happen */
507 WARN("DC is not in use!\n");
508 break;
509 case DCHC_DELETEDC:
511 * Windows will not let you delete a DC that is busy
512 * (between GetDC and ReleaseDC)
514 USER_Lock();
515 if (dce->count > 1)
517 WARN("Application trying to delete a busy 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, size, 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 );
793 DeleteObject( rgn );
798 /***********************************************************************
799 * update_now
801 * Implementation of RDW_UPDATENOW behavior.
803 static void update_now( HWND hwnd, UINT rdw_flags )
805 HWND child = 0;
807 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
808 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
810 /* loop while we find a child to repaint */
811 for (;;)
813 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
815 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
816 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
818 if (!get_update_flags( hwnd, &child, &flags )) break;
819 if (!flags) break; /* nothing more to do */
821 SendMessageW( child, WM_PAINT, 0, 0 );
822 if (rdw_flags & RDW_NOCHILDREN) break;
827 /*************************************************************************
828 * fix_caret
830 * Helper for ScrollWindowEx:
831 * If the return value is 0, no special caret handling is necessary.
832 * Otherwise the return value is the handle of the window that owns the
833 * caret. Its caret needs to be hidden during the scroll operation and
834 * moved to new_caret_pos if move_caret is TRUE.
836 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
837 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
839 GUITHREADINFO info;
840 RECT rect, mapped_rcCaret;
841 BOOL hide_caret = FALSE;
843 info.cbSize = sizeof(info);
844 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
845 if (!info.hwndCaret) return 0;
847 if (info.hwndCaret == hWnd)
849 /* Move the caret if it's (partially) in the source rectangle */
850 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
852 *move_caret = TRUE;
853 hide_caret = TRUE;
854 new_caret_pos->x = info.rcCaret.left + dx;
855 new_caret_pos->y = info.rcCaret.top + dy;
857 else
859 *move_caret = FALSE;
861 /* Hide the caret if it's in the destination rectangle */
862 rect = *scroll_rect;
863 OffsetRect(&rect, dx, dy);
864 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
867 else
869 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
871 *move_caret = FALSE;
873 /* Hide the caret if it's in the source or in the destination
874 rectangle */
875 mapped_rcCaret = info.rcCaret;
876 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
878 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
880 hide_caret = TRUE;
882 else
884 rect = *scroll_rect;
885 OffsetRect(&rect, dx, dy);
886 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
889 else
890 return 0;
893 if (hide_caret)
895 HideCaret(info.hwndCaret);
896 return info.hwndCaret;
898 else
899 return 0;
903 /***********************************************************************
904 * BeginPaint (USER32.@)
906 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
908 HRGN hrgn;
909 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
911 if (!lps) return 0;
913 HideCaret( hwnd );
915 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
917 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
919 TRACE("hdc = %p box = (%s), fErase = %d\n",
920 lps->hdc, wine_dbgstr_rect(&lps->rcPaint), lps->fErase);
922 return lps->hdc;
926 /***********************************************************************
927 * EndPaint (USER32.@)
929 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
931 if (!lps) return FALSE;
932 release_dc( hwnd, lps->hdc, TRUE );
933 ShowCaret( hwnd );
934 flush_window_surfaces( FALSE );
935 return TRUE;
939 /***********************************************************************
940 * GetDCEx (USER32.@)
942 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
944 const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
945 const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
946 struct dce *dce;
947 BOOL bUpdateVisRgn = TRUE;
948 HWND parent;
949 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
951 if (!hwnd) hwnd = GetDesktopWindow();
952 else hwnd = WIN_GetFullHandle( hwnd );
954 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
956 if (!IsWindow(hwnd)) return 0;
958 /* fixup flags */
960 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
962 if (flags & DCX_USESTYLE)
964 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
966 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
968 if (!(flags & DCX_WINDOW))
970 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
972 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
973 flags |= DCX_CLIPCHILDREN;
977 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
979 parent = GetAncestor( hwnd, GA_PARENT );
980 if (!parent || (parent == GetDesktopWindow()))
981 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
983 /* it seems parent clip is ignored when clipping siblings or children */
984 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
986 if( flags & DCX_PARENTCLIP )
988 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
989 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
991 flags &= ~DCX_CLIPCHILDREN;
992 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
996 /* find a suitable DCE */
998 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
1000 struct dce *dceEmpty = NULL, *dceUnused = NULL;
1002 /* Strategy: First, we attempt to find a non-empty but unused DCE with
1003 * compatible flags. Next, we look for an empty entry. If the cache is
1004 * full we have to purge one of the unused entries.
1006 USER_Lock();
1007 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1009 if ((dce->flags & DCX_CACHE) && !dce->count)
1011 dceUnused = dce;
1013 if (!dce->hwnd) dceEmpty = dce;
1014 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1016 TRACE("\tfound valid %p dce [%p], flags %08x\n",
1017 dce, hwnd, dce->flags );
1018 bUpdateVisRgn = FALSE;
1019 break;
1024 if (&dce->entry == &dce_list) /* nothing found */
1025 dce = dceEmpty ? dceEmpty : dceUnused;
1027 if (dce) dce->count = 1;
1029 USER_Unlock();
1031 /* if there's no dce empty or unused, allocate a new one */
1032 if (!dce)
1034 if (!(dce = alloc_dce())) return 0;
1035 dce->flags = DCX_CACHE;
1036 USER_Lock();
1037 list_add_head( &dce_list, &dce->entry );
1038 USER_Unlock();
1041 else
1043 flags |= DCX_NORESETATTRS;
1044 if (dce->hwnd == hwnd)
1046 TRACE("\tskipping hVisRgn update\n");
1047 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1049 else
1051 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1052 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1053 dce->clip_rgn = 0;
1057 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1059 /* if the extra clip region has changed, get rid of the old one */
1060 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1061 delete_clip_rgn( dce );
1062 dce->clip_rgn = hrgnClip;
1063 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1064 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1065 bUpdateVisRgn = TRUE;
1068 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1070 dce->hwnd = hwnd;
1071 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1073 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1075 if (bUpdateVisRgn) update_visible_region( dce );
1077 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1078 return dce->hdc;
1082 /***********************************************************************
1083 * GetDC (USER32.@)
1085 * Get a device context.
1087 * RETURNS
1088 * Success: Handle to the device context
1089 * Failure: NULL.
1091 HDC WINAPI GetDC( HWND hwnd )
1093 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1094 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1098 /***********************************************************************
1099 * GetWindowDC (USER32.@)
1101 HDC WINAPI GetWindowDC( HWND hwnd )
1103 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1107 /***********************************************************************
1108 * ReleaseDC (USER32.@)
1110 * Release a device context.
1112 * RETURNS
1113 * Success: Non-zero. Resources used by hdc are released.
1114 * Failure: 0.
1116 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1118 return release_dc( hwnd, hdc, FALSE );
1122 /**********************************************************************
1123 * WindowFromDC (USER32.@)
1125 HWND WINAPI WindowFromDC( HDC hdc )
1127 struct dce *dce;
1128 HWND hwnd = 0;
1130 USER_Lock();
1131 dce = (struct dce *)GetDCHook( hdc, NULL );
1132 if (dce) hwnd = dce->hwnd;
1133 USER_Unlock();
1134 return hwnd;
1138 /***********************************************************************
1139 * LockWindowUpdate (USER32.@)
1141 * Enables or disables painting in the chosen window.
1143 * PARAMS
1144 * hwnd [I] handle to a window.
1146 * RETURNS
1147 * If successful, returns nonzero value. Otherwise,
1148 * returns 0.
1150 * NOTES
1151 * You can lock only one window at a time.
1153 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1155 static HWND lockedWnd;
1157 FIXME("(%p), partial stub!\n",hwnd);
1159 USER_Lock();
1160 if (lockedWnd)
1162 if (!hwnd)
1164 /* Unlock lockedWnd */
1165 /* FIXME: Do something */
1167 else
1169 /* Attempted to lock a second window */
1170 /* Return FALSE and do nothing */
1171 USER_Unlock();
1172 return FALSE;
1175 lockedWnd = hwnd;
1176 USER_Unlock();
1177 return TRUE;
1181 /***********************************************************************
1182 * RedrawWindow (USER32.@)
1184 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1186 static const RECT empty;
1187 BOOL ret;
1189 if (!hwnd) hwnd = GetDesktopWindow();
1191 if (TRACE_ON(win))
1193 if (hrgn)
1195 RECT r;
1196 GetRgnBox( hrgn, &r );
1197 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1199 else if (rect)
1200 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1201 else
1202 TRACE( "%p whole window ", hwnd );
1204 dump_rdw_flags(flags);
1207 /* process pending expose events before painting */
1208 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1210 if (rect && !hrgn)
1212 if (IsRectEmpty( rect )) rect = &empty;
1213 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1215 else if (!hrgn)
1217 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1219 else /* need to build a list of the region rectangles */
1221 DWORD size;
1222 RGNDATA *data = NULL;
1224 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1225 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1226 GetRegionData( hrgn, size, data );
1227 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1228 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1229 else
1230 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1231 HeapFree( GetProcessHeap(), 0, data );
1234 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1235 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1237 return ret;
1241 /***********************************************************************
1242 * UpdateWindow (USER32.@)
1244 BOOL WINAPI UpdateWindow( HWND hwnd )
1246 if (!hwnd)
1248 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1249 return FALSE;
1252 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1256 /***********************************************************************
1257 * InvalidateRgn (USER32.@)
1259 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1261 if (!hwnd)
1263 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1264 return FALSE;
1267 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1271 /***********************************************************************
1272 * InvalidateRect (USER32.@)
1274 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1275 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1277 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1279 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1281 if (!hwnd)
1283 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1284 rect = NULL;
1287 return RedrawWindow( hwnd, rect, 0, flags );
1291 /***********************************************************************
1292 * ValidateRgn (USER32.@)
1294 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1296 if (!hwnd)
1298 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1299 return FALSE;
1302 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1306 /***********************************************************************
1307 * ValidateRect (USER32.@)
1309 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1310 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1312 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1314 UINT flags = RDW_VALIDATE;
1316 if (!hwnd)
1318 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1319 rect = NULL;
1322 return RedrawWindow( hwnd, rect, 0, flags );
1326 /***********************************************************************
1327 * GetUpdateRgn (USER32.@)
1329 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1331 INT retval = ERROR;
1332 UINT flags = UPDATE_NOCHILDREN;
1333 HRGN update_rgn;
1335 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1337 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1339 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1340 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1342 flags = UPDATE_DELAYED_ERASE;
1343 get_update_flags( hwnd, NULL, &flags );
1345 /* map region to client coordinates */
1346 map_window_region( 0, hwnd, hrgn );
1348 return retval;
1352 /***********************************************************************
1353 * GetUpdateRect (USER32.@)
1355 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1357 UINT flags = UPDATE_NOCHILDREN;
1358 HRGN update_rgn;
1359 BOOL need_erase;
1361 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1363 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1365 if (rect)
1367 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1369 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1370 DWORD layout = SetLayout( hdc, 0 ); /* MapWindowPoints mirrors already */
1371 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1372 DPtoLP( hdc, (LPPOINT)rect, 2 );
1373 SetLayout( hdc, layout );
1374 ReleaseDC( hwnd, hdc );
1377 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1379 /* check if we still have an update region */
1380 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1381 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1382 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1386 /***********************************************************************
1387 * ExcludeUpdateRgn (USER32.@)
1389 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1391 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1392 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1394 if (ret != ERROR)
1396 POINT pt;
1398 GetDCOrgEx( hdc, &pt );
1399 MapWindowPoints( 0, hwnd, &pt, 1 );
1400 OffsetRgn( update_rgn, -pt.x, -pt.y );
1401 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1402 DeleteObject( update_rgn );
1404 return ret;
1408 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1409 HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1411 INT retVal = NULLREGION;
1412 BOOL bOwnRgn = TRUE;
1413 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1414 int rdw_flags;
1415 HRGN hrgnTemp;
1416 HRGN hrgnWinupd = 0;
1417 HDC hDC;
1418 RECT rc, cliprc;
1419 HWND hwndCaret = NULL;
1420 BOOL moveCaret = FALSE;
1421 POINT newCaretPos;
1423 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1424 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1425 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1426 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1427 FIXME("some flags (%04x) are unhandled\n", flags);
1429 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1430 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1432 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1433 hwnd = WIN_GetFullHandle( hwnd );
1435 GetClientRect(hwnd, &rc);
1437 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1438 else cliprc = rc;
1440 if (rect) IntersectRect(&rc, &rc, rect);
1442 if( hrgnUpdate ) bOwnRgn = FALSE;
1443 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1445 newCaretPos.x = newCaretPos.y = 0;
1447 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1448 DWORD dcxflags = 0;
1449 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1451 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1453 if (is_ex) dcxflags |= DCX_CACHE;
1454 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1455 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1456 dcxflags |= DCX_PARENTCLIP;
1457 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1458 dcxflags |= DCX_CLIPCHILDREN;
1459 hDC = GetDCEx( hwnd, 0, dcxflags);
1460 if (hDC)
1462 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1464 ReleaseDC( hwnd, hDC );
1466 if (!bUpdate)
1467 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1470 /* If the windows has an update region, this must be
1471 * scrolled as well. Keep a copy in hrgnWinupd
1472 * to be added to hrngUpdate at the end. */
1473 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1474 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1475 if (retVal != NULLREGION)
1477 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1478 if( !bOwnRgn) {
1479 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1480 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1482 OffsetRgn( hrgnTemp, dx, dy );
1483 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1484 if( !bOwnRgn)
1485 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1486 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1488 /* Catch the case where the scrolling amount exceeds the size of the
1489 * original window. This generated a second update area that is the
1490 * location where the original scrolled content would end up.
1491 * This second region is not returned by the ScrollDC and sets
1492 * ScrollWindowEx apart from just a ScrollDC.
1494 * This has been verified with testing on windows.
1496 if (abs(dx) > abs(rc.right - rc.left) ||
1497 abs(dy) > abs(rc.bottom - rc.top))
1499 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1500 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1501 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1503 if( !bOwnRgn)
1504 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1506 DeleteObject( hrgnClip );
1508 DeleteObject( hrgnTemp );
1509 } else {
1510 /* nothing was scrolled */
1511 if( !bOwnRgn)
1512 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1513 SetRectEmpty( rcUpdate);
1516 if( flags & SW_SCROLLCHILDREN )
1518 HWND *list = WIN_ListChildren( hwnd );
1519 if (list)
1521 int i;
1522 RECT r, dummy;
1523 for (i = 0; list[i]; i++)
1525 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1526 if (!rect || IntersectRect(&dummy, &r, rect))
1527 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1528 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1529 SWP_NOREDRAW | SWP_DEFERERASE );
1531 HeapFree( GetProcessHeap(), 0, list );
1535 if( flags & (SW_INVALIDATE | SW_ERASE) )
1536 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1537 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1539 if( hrgnWinupd) {
1540 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1541 DeleteObject( hrgnWinupd);
1544 if( hwndCaret ) {
1545 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1546 ShowCaret(hwndCaret);
1549 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1551 return retVal;
1555 /*************************************************************************
1556 * ScrollWindowEx (USER32.@)
1558 * Note: contrary to what the doc says, pixels that are scrolled from the
1559 * outside of clipRect to the inside are NOT painted.
1562 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1563 const RECT *rect, const RECT *clipRect,
1564 HRGN hrgnUpdate, LPRECT rcUpdate,
1565 UINT flags )
1567 return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1570 /*************************************************************************
1571 * ScrollWindow (USER32.@)
1574 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1575 const RECT *rect, const RECT *clipRect )
1577 return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1578 SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1582 /*************************************************************************
1583 * ScrollDC (USER32.@)
1585 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1586 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1587 * logical coordinates.
1589 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
1590 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
1593 return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
1596 /************************************************************************
1597 * PrintWindow (USER32.@)
1600 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1602 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1603 if(!(nFlags & PW_CLIENTONLY))
1605 flags |= PRF_NONCLIENT;
1607 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1608 return TRUE;