user32: DCX_NORESETATTRS should be applied at DC release time.
[wine/multimedia.git] / dlls / user32 / painting.c
blob5e57e7f8b70b04ae381ac15c29905231922b427f
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/list.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(win);
45 struct dce
47 struct list entry; /* entry in global DCE list */
48 HDC hdc;
49 HWND hwnd;
50 HRGN clip_rgn;
51 DWORD flags;
52 LONG count; /* usage count; 0 or 1 for cache DCEs, always 1 for window DCEs,
53 always >= 1 for class DCEs */
56 static struct list dce_list = LIST_INIT(dce_list);
58 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
60 static const WCHAR displayW[] = { 'D','I','S','P','L','A','Y',0 };
63 /***********************************************************************
64 * dump_rdw_flags
66 static void dump_rdw_flags(UINT flags)
68 TRACE("flags:");
69 if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
70 if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
71 if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
72 if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
73 if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
74 if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
75 if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
76 if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
77 if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
78 if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
79 if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
80 if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
82 #define RDW_FLAGS \
83 (RDW_INVALIDATE | \
84 RDW_INTERNALPAINT | \
85 RDW_ERASE | \
86 RDW_VALIDATE | \
87 RDW_NOINTERNALPAINT | \
88 RDW_NOERASE | \
89 RDW_NOCHILDREN | \
90 RDW_ALLCHILDREN | \
91 RDW_UPDATENOW | \
92 RDW_ERASENOW | \
93 RDW_FRAME | \
94 RDW_NOFRAME)
96 if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
97 TRACE("\n");
98 #undef RDW_FLAGS
102 /***********************************************************************
103 * update_visible_region
105 * Set the visible region and X11 drawable for the DC associated to
106 * a given window.
108 static void update_visible_region( struct dce *dce )
110 NTSTATUS status;
111 HRGN vis_rgn = 0;
112 HWND top_win = 0;
113 DWORD flags = dce->flags;
114 size_t size = 256;
115 RECT win_rect, top_rect;
117 /* don't clip siblings if using parent clip region */
118 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
120 /* fetch the visible region from the server */
123 RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
124 if (!data) return;
126 SERVER_START_REQ( get_visible_region )
128 req->window = wine_server_user_handle( dce->hwnd );
129 req->flags = flags;
130 wine_server_set_reply( req, data->Buffer, size );
131 if (!(status = wine_server_call( req )))
133 size_t reply_size = wine_server_reply_size( reply );
134 data->rdh.dwSize = sizeof(data->rdh);
135 data->rdh.iType = RDH_RECTANGLES;
136 data->rdh.nCount = reply_size / sizeof(RECT);
137 data->rdh.nRgnSize = reply_size;
138 vis_rgn = ExtCreateRegion( NULL, size, data );
140 top_win = wine_server_ptr_handle( reply->top_win );
141 win_rect.left = reply->win_rect.left;
142 win_rect.top = reply->win_rect.top;
143 win_rect.right = reply->win_rect.right;
144 win_rect.bottom = reply->win_rect.bottom;
145 top_rect.left = reply->top_rect.left;
146 top_rect.top = reply->top_rect.top;
147 top_rect.right = reply->top_rect.right;
148 top_rect.bottom = reply->top_rect.bottom;
150 else size = reply->total_size;
152 SERVER_END_REQ;
153 HeapFree( GetProcessHeap(), 0, data );
154 } while (status == STATUS_BUFFER_OVERFLOW);
156 if (status || !vis_rgn) return;
158 USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
160 if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
161 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
163 /* map region to DC coordinates */
164 OffsetRgn( vis_rgn, -win_rect.left, -win_rect.top );
165 SelectVisRgn( dce->hdc, vis_rgn );
166 DeleteObject( vis_rgn );
170 /***********************************************************************
171 * reset_dce_attrs
173 static void reset_dce_attrs( struct dce *dce )
175 RestoreDC( dce->hdc, 1 ); /* initial save level is always 1 */
176 SaveDC( dce->hdc ); /* save the state again for next time */
180 /***********************************************************************
181 * release_dce
183 static void release_dce( struct dce *dce )
185 if (!dce->hwnd) return; /* already released */
187 USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
189 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
190 dce->clip_rgn = 0;
191 dce->hwnd = 0;
192 dce->flags &= DCX_CACHE;
196 /***********************************************************************
197 * delete_clip_rgn
199 static void delete_clip_rgn( struct dce *dce )
201 if (!dce->clip_rgn) return; /* nothing to do */
203 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
204 DeleteObject( dce->clip_rgn );
205 dce->clip_rgn = 0;
207 /* make it dirty so that the vis rgn gets recomputed next time */
208 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
212 /***********************************************************************
213 * alloc_dce
215 * Allocate a new DCE.
217 static struct dce *alloc_dce(void)
219 struct dce *dce;
221 if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
222 if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
224 HeapFree( GetProcessHeap(), 0, dce );
225 return 0;
227 SaveDC( dce->hdc );
229 dce->hwnd = 0;
230 dce->clip_rgn = 0;
231 dce->flags = 0;
232 dce->count = 1;
234 /* store DCE handle in DC hook data field */
235 SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
236 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
237 return dce;
241 /***********************************************************************
242 * get_window_dce
244 static struct dce *get_window_dce( HWND hwnd )
246 struct dce *dce;
247 WND *win = WIN_GetPtr( hwnd );
249 if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
251 dce = win->dce;
252 if (!dce && (dce = get_class_dce( win->class )))
254 win->dce = dce;
255 dce->count++;
257 WIN_ReleasePtr( win );
259 if (!dce) /* try to allocate one */
261 struct dce *dce_to_free = NULL;
262 LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
264 if (class_style & CS_CLASSDC)
266 if (!(dce = alloc_dce())) return NULL;
268 win = WIN_GetPtr( hwnd );
269 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
271 if (win->dce) /* another thread beat us to it */
273 dce_to_free = dce;
274 dce = win->dce;
276 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
278 dce_to_free = dce;
279 dce = win->dce;
280 dce->count++;
282 else
284 dce->count++;
285 list_add_tail( &dce_list, &dce->entry );
287 WIN_ReleasePtr( win );
289 else dce_to_free = dce;
291 else if (class_style & CS_OWNDC)
293 if (!(dce = alloc_dce())) return NULL;
295 win = WIN_GetPtr( hwnd );
296 if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
298 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
299 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
300 if (win->dce) /* another thread beat us to it */
302 dce_to_free = dce;
303 dce = win->dce;
305 else
307 win->dce = dce;
308 dce->hwnd = hwnd;
309 dce->count++;
310 list_add_tail( &dce_list, &dce->entry );
312 WIN_ReleasePtr( win );
314 else dce_to_free = dce;
317 if (dce_to_free)
319 SetDCHook( dce->hdc, NULL, 0 );
320 DeleteDC( dce->hdc );
321 HeapFree( GetProcessHeap(), 0, dce );
324 return dce;
328 /***********************************************************************
329 * free_dce
331 * Free a class or window DCE.
333 void free_dce( struct dce *dce, HWND hwnd )
335 USER_Lock();
337 if (dce)
339 if (!--dce->count)
341 /* turn it into a cache entry */
342 reset_dce_attrs( dce );
343 release_dce( dce );
344 dce->flags |= DCX_CACHE;
346 else if (dce->hwnd == hwnd)
348 release_dce( dce );
352 /* now check for cache DCEs */
354 if (hwnd)
356 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
358 if (dce->hwnd != hwnd) continue;
359 if (!(dce->flags & DCX_CACHE)) continue;
361 if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
362 dce->count = 0;
363 release_dce( dce );
367 USER_Unlock();
371 /***********************************************************************
372 * make_dc_dirty
374 * Mark the associated DC as dirty to force a refresh of the visible region
376 static void make_dc_dirty( struct dce *dce )
378 if (!dce->count)
380 /* Don't bother with visible regions of unused DCEs */
381 TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
382 release_dce( dce );
384 else
386 /* Set dirty bits in the hDC and DCE structs */
387 TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
388 SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
393 /***********************************************************************
394 * invalidate_dce
396 * It is called from SetWindowPos() - we have to
397 * mark as dirty all busy DCEs for windows that have pWnd->parent as
398 * an ancestor and whose client rect intersects with specified update
399 * rectangle. In addition, pWnd->parent DCEs may need to be updated if
400 * DCX_CLIPCHILDREN flag is set.
402 void invalidate_dce( HWND hwnd, const RECT *rect )
404 RECT window_rect, extra_rect;
405 struct dce *dce;
406 HWND hwndScope = GetAncestor( hwnd, GA_PARENT );
408 if (!hwndScope) return;
410 GetWindowRect( hwnd, &window_rect );
411 if (rect)
413 extra_rect = *rect;
414 MapWindowPoints( hwndScope, 0, (POINT *)&extra_rect, 2 );
417 TRACE("%p scope hwnd = %p %s (%s)\n",
418 hwnd, hwndScope, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(rect) );
420 /* walk all DCEs and fixup non-empty entries */
422 USER_Lock();
423 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
425 TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
426 (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
428 if (!dce->hwnd) continue;
429 if ((dce->hwnd == hwndScope) && !(dce->flags & DCX_CLIPCHILDREN))
430 continue; /* child window positions don't bother us */
432 /* if DCE window is a child of hwnd, it has to be invalidated */
433 if (dce->hwnd == hwnd || IsChild( hwnd, dce->hwnd ))
435 make_dc_dirty( dce );
437 else /* otherwise check if the window rectangle intersects this DCE window */
439 if (hwndScope == GetDesktopWindow() ||
440 hwndScope == dce->hwnd || IsChild( hwndScope, dce->hwnd ))
442 RECT dce_rect, tmp;
443 GetWindowRect( dce->hwnd, &dce_rect );
444 if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
445 (rect && IntersectRect( &tmp, &dce_rect, &extra_rect )))
446 make_dc_dirty( dce );
450 USER_Unlock();
453 /***********************************************************************
454 * release_dc
456 * Implementation of ReleaseDC.
458 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
460 struct dce *dce;
461 BOOL ret = FALSE;
463 TRACE("%p %p\n", hwnd, hdc );
465 USER_Lock();
466 dce = (struct dce *)GetDCHook( hdc, NULL );
467 if (dce && dce->count)
469 if (!(dce->flags & DCX_NORESETATTRS)) reset_dce_attrs( dce );
470 if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
471 if (dce->flags & DCX_CACHE) dce->count = 0;
472 ret = TRUE;
474 USER_Unlock();
475 return ret;
479 /***********************************************************************
480 * dc_hook
482 * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
484 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
486 BOOL retv = TRUE;
487 struct dce *dce = (struct dce *)data;
489 TRACE("hDC = %p, %u\n", hDC, code);
491 if (!dce) return 0;
492 assert( dce->hdc == hDC );
494 switch( code )
496 case DCHC_INVALIDVISRGN:
497 /* GDI code calls this when it detects that the
498 * DC is dirty (usually after SetHookFlags()). This
499 * means that we have to recompute the visible region.
501 if (dce->count) update_visible_region( dce );
502 else /* non-fatal but shouldn't happen */
503 WARN("DC is not in use!\n");
504 break;
505 case DCHC_DELETEDC:
507 * Windows will not let you delete a DC that is busy
508 * (between GetDC and ReleaseDC)
510 USER_Lock();
511 if (dce->count > 1)
513 WARN("Application trying to delete a busy DC %p\n", dce->hdc);
514 retv = FALSE;
516 else
518 list_remove( &dce->entry );
519 if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
520 HeapFree( GetProcessHeap(), 0, dce );
522 USER_Unlock();
523 break;
525 return retv;
529 /***********************************************************************
530 * get_update_region
532 * Return update region (in screen coordinates) for a window.
534 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
536 HRGN hrgn = 0;
537 NTSTATUS status;
538 RGNDATA *data;
539 size_t size = 256;
543 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
545 SetLastError( ERROR_OUTOFMEMORY );
546 return 0;
549 SERVER_START_REQ( get_update_region )
551 req->window = wine_server_user_handle( hwnd );
552 req->from_child = wine_server_user_handle( child ? *child : 0 );
553 req->flags = *flags;
554 wine_server_set_reply( req, data->Buffer, size );
555 if (!(status = wine_server_call( req )))
557 size_t reply_size = wine_server_reply_size( reply );
558 data->rdh.dwSize = sizeof(data->rdh);
559 data->rdh.iType = RDH_RECTANGLES;
560 data->rdh.nCount = reply_size / sizeof(RECT);
561 data->rdh.nRgnSize = reply_size;
562 hrgn = ExtCreateRegion( NULL, size, data );
563 if (child) *child = wine_server_ptr_handle( reply->child );
564 *flags = reply->flags;
566 else size = reply->total_size;
568 SERVER_END_REQ;
569 HeapFree( GetProcessHeap(), 0, data );
570 } while (status == STATUS_BUFFER_OVERFLOW);
572 if (status) SetLastError( RtlNtStatusToDosError(status) );
573 return hrgn;
577 /***********************************************************************
578 * get_update_flags
580 * Get only the update flags, not the update region.
582 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
584 BOOL ret;
586 SERVER_START_REQ( get_update_region )
588 req->window = wine_server_user_handle( hwnd );
589 req->from_child = wine_server_user_handle( child ? *child : 0 );
590 req->flags = *flags | UPDATE_NOREGION;
591 if ((ret = !wine_server_call_err( req )))
593 if (child) *child = wine_server_ptr_handle( reply->child );
594 *flags = reply->flags;
597 SERVER_END_REQ;
598 return ret;
602 /***********************************************************************
603 * redraw_window_rects
605 * Redraw part of a window.
607 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
609 BOOL ret;
611 if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
612 return TRUE; /* nothing to do */
614 SERVER_START_REQ( redraw_window )
616 req->window = wine_server_user_handle( hwnd );
617 req->flags = flags;
618 wine_server_add_data( req, rects, count * sizeof(RECT) );
619 ret = !wine_server_call_err( req );
621 SERVER_END_REQ;
622 return ret;
626 /***********************************************************************
627 * send_ncpaint
629 * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
630 * Helper for erase_now and BeginPaint.
632 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
634 HRGN whole_rgn = get_update_region( hwnd, flags, child );
635 HRGN client_rgn = 0;
637 if (child) hwnd = *child;
639 if (hwnd == GetDesktopWindow()) return whole_rgn;
641 if (whole_rgn)
643 RECT client, update;
644 INT type;
646 /* check if update rgn overlaps with nonclient area */
647 type = GetRgnBox( whole_rgn, &update );
648 GetClientRect( hwnd, &client );
649 MapWindowPoints( hwnd, 0, (POINT *)&client, 2 );
651 if ((*flags & UPDATE_NONCLIENT) ||
652 update.left < client.left || update.top < client.top ||
653 update.right > client.right || update.bottom > client.bottom)
655 client_rgn = CreateRectRgnIndirect( &client );
656 CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
658 /* check if update rgn contains complete nonclient area */
659 if (type == SIMPLEREGION)
661 RECT window;
662 GetWindowRect( hwnd, &window );
663 if (EqualRect( &window, &update ))
665 DeleteObject( whole_rgn );
666 whole_rgn = (HRGN)1;
670 else
672 client_rgn = whole_rgn;
673 whole_rgn = 0;
676 if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
678 if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
679 if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
682 return client_rgn;
686 /***********************************************************************
687 * send_erase
689 * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
690 * If a DC is requested, the region is selected into it. In all cases the region is deleted.
691 * Helper for erase_now and BeginPaint.
693 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
694 RECT *clip_rect, HDC *hdc_ret )
696 BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
697 HDC hdc = 0;
698 RECT dummy;
700 if (!clip_rect) clip_rect = &dummy;
701 if (hdc_ret || (flags & UPDATE_ERASE))
703 UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
704 if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
706 if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
708 INT type = GetClipBox( hdc, clip_rect );
710 if (flags & UPDATE_ERASE)
712 /* don't erase if the clip box is empty */
713 if (type != NULLREGION)
714 need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
716 if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
719 if (hdc_ret) *hdc_ret = hdc;
721 if (!hdc) DeleteObject( client_rgn );
722 return need_erase;
726 /***********************************************************************
727 * erase_now
729 * Implementation of RDW_ERASENOW behavior.
731 void erase_now( HWND hwnd, UINT rdw_flags )
733 HWND child = 0;
734 HRGN hrgn;
735 BOOL need_erase = FALSE;
737 /* loop while we find a child to repaint */
738 for (;;)
740 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
742 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
743 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
744 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
746 if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
747 need_erase = send_erase( child, flags, hrgn, NULL, NULL );
749 if (!flags) break; /* nothing more to do */
750 if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
755 /***********************************************************************
756 * update_now
758 * Implementation of RDW_UPDATENOW behavior.
760 * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
761 * SendMessage() calls. This is a comment inside DefWindowProc() source
762 * from 16-bit SDK:
764 * This message avoids lots of inter-app message traffic
765 * by switching to the other task and continuing the
766 * recursion there.
768 * wParam = flags
769 * LOWORD(lParam) = hrgnClip
770 * HIWORD(lParam) = hwndSkip (not used; always NULL)
773 static void update_now( HWND hwnd, UINT rdw_flags )
775 HWND child = 0;
777 /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
778 if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
780 /* loop while we find a child to repaint */
781 for (;;)
783 UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
785 if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
786 else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
788 if (!get_update_flags( hwnd, &child, &flags )) break;
789 if (!flags) break; /* nothing more to do */
791 SendMessageW( child, WM_PAINT, 0, 0 );
792 if (rdw_flags & RDW_NOCHILDREN) break;
797 /*************************************************************************
798 * fix_caret
800 * Helper for ScrollWindowEx:
801 * If the return value is 0, no special caret handling is necessary.
802 * Otherwise the return value is the handle of the window that owns the
803 * caret. Its caret needs to be hidden during the scroll operation and
804 * moved to new_caret_pos if move_caret is TRUE.
806 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
807 UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
809 GUITHREADINFO info;
810 RECT rect, mapped_rcCaret;
811 BOOL hide_caret = FALSE;
813 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
814 if (!info.hwndCaret) return 0;
816 if (info.hwndCaret == hWnd)
818 /* Move the caret if it's (partially) in the source rectangle */
819 if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
821 *move_caret = TRUE;
822 hide_caret = TRUE;
823 new_caret_pos->x = info.rcCaret.left + dx;
824 new_caret_pos->y = info.rcCaret.top + dy;
826 else
828 *move_caret = FALSE;
830 /* Hide the caret if it's in the destination rectangle */
831 rect = *scroll_rect;
832 OffsetRect(&rect, dx, dy);
833 hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
836 else
838 if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
840 *move_caret = FALSE;
842 /* Hide the caret if it's in the source or in the destination
843 rectangle */
844 mapped_rcCaret = info.rcCaret;
845 MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
847 if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
849 hide_caret = TRUE;
851 else
853 rect = *scroll_rect;
854 OffsetRect(&rect, dx, dy);
855 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
858 else
859 return 0;
862 if (hide_caret)
864 HideCaret(info.hwndCaret);
865 return info.hwndCaret;
867 else
868 return 0;
872 /***********************************************************************
873 * BeginPaint (USER32.@)
875 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
877 HRGN hrgn;
878 UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
880 if (!lps) return 0;
882 HideCaret( hwnd );
884 if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
886 lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
888 TRACE("hdc = %p box = (%s), fErase = %d\n",
889 lps->hdc, wine_dbgstr_rect(&lps->rcPaint), lps->fErase);
891 return lps->hdc;
895 /***********************************************************************
896 * EndPaint (USER32.@)
898 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
900 if (!lps) return FALSE;
901 release_dc( hwnd, lps->hdc, TRUE );
902 ShowCaret( hwnd );
903 return TRUE;
907 /***********************************************************************
908 * GetDCEx (USER32.@)
910 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
912 static const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
913 static const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
914 struct dce *dce;
915 BOOL bUpdateVisRgn = TRUE;
916 HWND parent;
917 LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
919 if (!hwnd) hwnd = GetDesktopWindow();
920 else hwnd = WIN_GetFullHandle( hwnd );
922 TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
924 if (!IsWindow(hwnd)) return 0;
926 /* fixup flags */
928 if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
930 if (flags & DCX_USESTYLE)
932 flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
934 if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
936 if (!(flags & DCX_WINDOW))
938 if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
940 if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
941 flags |= DCX_CLIPCHILDREN;
945 if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
947 parent = GetAncestor( hwnd, GA_PARENT );
948 if (!parent || (parent == GetDesktopWindow()))
949 flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
951 /* it seems parent clip is ignored when clipping siblings or children */
952 if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
954 if( flags & DCX_PARENTCLIP )
956 LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
957 if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
959 flags &= ~DCX_CLIPCHILDREN;
960 if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
964 /* find a suitable DCE */
966 if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
968 struct dce *dceEmpty = NULL, *dceUnused = NULL;
970 /* Strategy: First, we attempt to find a non-empty but unused DCE with
971 * compatible flags. Next, we look for an empty entry. If the cache is
972 * full we have to purge one of the unused entries.
974 USER_Lock();
975 LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
977 if ((dce->flags & DCX_CACHE) && !dce->count)
979 dceUnused = dce;
981 if (!dce->hwnd) dceEmpty = dce;
982 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
984 TRACE("\tfound valid %p dce [%p], flags %08x\n",
985 dce, hwnd, dce->flags );
986 bUpdateVisRgn = FALSE;
987 break;
992 if (&dce->entry == &dce_list) /* nothing found */
993 dce = dceEmpty ? dceEmpty : dceUnused;
995 if (dce) dce->count = 1;
997 USER_Unlock();
999 /* if there's no dce empty or unused, allocate a new one */
1000 if (!dce)
1002 if (!(dce = alloc_dce())) return 0;
1003 dce->flags = DCX_CACHE;
1004 USER_Lock();
1005 list_add_head( &dce_list, &dce->entry );
1006 USER_Unlock();
1009 else
1011 flags |= DCX_NORESETATTRS;
1012 if (dce->hwnd == hwnd)
1014 TRACE("\tskipping hVisRgn update\n");
1015 bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1017 else
1019 /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1020 dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1021 dce->clip_rgn = 0;
1025 if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1027 /* if the extra clip region has changed, get rid of the old one */
1028 if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1029 delete_clip_rgn( dce );
1030 dce->clip_rgn = hrgnClip;
1031 if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1032 dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1033 bUpdateVisRgn = TRUE;
1036 dce->hwnd = hwnd;
1037 dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1039 if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE; /* DC was dirty */
1041 if (bUpdateVisRgn) update_visible_region( dce );
1043 TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1044 return dce->hdc;
1048 /***********************************************************************
1049 * GetDC (USER32.@)
1051 * Get a device context.
1053 * RETURNS
1054 * Success: Handle to the device context
1055 * Failure: NULL.
1057 HDC WINAPI GetDC( HWND hwnd )
1059 if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1060 return GetDCEx( hwnd, 0, DCX_USESTYLE );
1064 /***********************************************************************
1065 * GetWindowDC (USER32.@)
1067 HDC WINAPI GetWindowDC( HWND hwnd )
1069 return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1073 /***********************************************************************
1074 * ReleaseDC (USER32.@)
1076 * Release a device context.
1078 * RETURNS
1079 * Success: Non-zero. Resources used by hdc are released.
1080 * Failure: 0.
1082 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1084 return release_dc( hwnd, hdc, FALSE );
1088 /**********************************************************************
1089 * WindowFromDC (USER32.@)
1091 HWND WINAPI WindowFromDC( HDC hdc )
1093 struct dce *dce;
1094 HWND hwnd = 0;
1096 USER_Lock();
1097 dce = (struct dce *)GetDCHook( hdc, NULL );
1098 if (dce) hwnd = dce->hwnd;
1099 USER_Unlock();
1100 return hwnd;
1104 /***********************************************************************
1105 * LockWindowUpdate (USER32.@)
1107 * Enables or disables painting in the chosen window.
1109 * PARAMS
1110 * hwnd [I] handle to a window.
1112 * RETURNS
1113 * If successful, returns nonzero value. Otherwise,
1114 * returns 0.
1116 * NOTES
1117 * You can lock only one window at a time.
1119 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1121 static HWND lockedWnd;
1123 FIXME("(%p), partial stub!\n",hwnd);
1125 USER_Lock();
1126 if (lockedWnd)
1128 if (!hwnd)
1130 /* Unlock lockedWnd */
1131 /* FIXME: Do something */
1133 else
1135 /* Attempted to lock a second window */
1136 /* Return FALSE and do nothing */
1137 USER_Unlock();
1138 return FALSE;
1141 lockedWnd = hwnd;
1142 USER_Unlock();
1143 return TRUE;
1147 /***********************************************************************
1148 * RedrawWindow (USER32.@)
1150 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1152 static const RECT empty;
1153 BOOL ret;
1155 if (!hwnd) hwnd = GetDesktopWindow();
1157 if (TRACE_ON(win))
1159 if (hrgn)
1161 RECT r;
1162 GetRgnBox( hrgn, &r );
1163 TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1165 else if (rect)
1166 TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1167 else
1168 TRACE( "%p whole window ", hwnd );
1170 dump_rdw_flags(flags);
1173 /* process pending expose events before painting */
1174 if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1176 if (rect && !hrgn)
1178 if (IsRectEmpty( rect )) rect = &empty;
1179 ret = redraw_window_rects( hwnd, flags, rect, 1 );
1181 else if (!hrgn)
1183 ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1185 else /* need to build a list of the region rectangles */
1187 DWORD size;
1188 RGNDATA *data = NULL;
1190 if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1191 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1192 GetRegionData( hrgn, size, data );
1193 if (!data->rdh.nCount) /* empty region -> use a single all-zero rectangle */
1194 ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1195 else
1196 ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1197 HeapFree( GetProcessHeap(), 0, data );
1200 if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1201 else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1203 return ret;
1207 /***********************************************************************
1208 * UpdateWindow (USER32.@)
1210 BOOL WINAPI UpdateWindow( HWND hwnd )
1212 return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1216 /***********************************************************************
1217 * InvalidateRgn (USER32.@)
1219 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1221 if (!hwnd)
1223 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1224 return FALSE;
1227 return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1231 /***********************************************************************
1232 * InvalidateRect (USER32.@)
1234 * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1235 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1237 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1239 UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1241 if (!hwnd)
1243 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1244 rect = NULL;
1247 return RedrawWindow( hwnd, rect, 0, flags );
1251 /***********************************************************************
1252 * ValidateRgn (USER32.@)
1254 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1256 if (!hwnd)
1258 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1259 return FALSE;
1262 return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1266 /***********************************************************************
1267 * ValidateRect (USER32.@)
1269 * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1270 * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1272 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1274 UINT flags = RDW_VALIDATE;
1276 if (!hwnd)
1278 flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1279 rect = NULL;
1282 return RedrawWindow( hwnd, rect, 0, flags );
1286 /***********************************************************************
1287 * GetUpdateRgn (USER32.@)
1289 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1291 INT retval = ERROR;
1292 UINT flags = UPDATE_NOCHILDREN;
1293 HRGN update_rgn;
1295 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1297 if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1299 POINT offset;
1301 retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1302 if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1304 flags = UPDATE_DELAYED_ERASE;
1305 get_update_flags( hwnd, NULL, &flags );
1307 /* map region to client coordinates */
1308 offset.x = offset.y = 0;
1309 ScreenToClient( hwnd, &offset );
1310 OffsetRgn( hrgn, offset.x, offset.y );
1312 return retval;
1316 /***********************************************************************
1317 * GetUpdateRect (USER32.@)
1319 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1321 UINT flags = UPDATE_NOCHILDREN;
1322 HRGN update_rgn;
1323 BOOL need_erase;
1325 if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1327 if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1329 if (rect)
1331 if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1333 HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1334 MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1335 DPtoLP( hdc, (LPPOINT)rect, 2 );
1336 ReleaseDC( hwnd, hdc );
1339 need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1341 /* check if we still have an update region */
1342 flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1343 if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1344 return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1348 /***********************************************************************
1349 * ExcludeUpdateRgn (USER32.@)
1351 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1353 HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1354 INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1356 if (ret != ERROR)
1358 POINT pt;
1360 GetDCOrgEx( hdc, &pt );
1361 MapWindowPoints( 0, hwnd, &pt, 1 );
1362 OffsetRgn( update_rgn, -pt.x, -pt.y );
1363 ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1364 DeleteObject( update_rgn );
1366 return ret;
1370 /*************************************************************************
1371 * ScrollWindowEx (USER32.@)
1373 * Note: contrary to what the doc says, pixels that are scrolled from the
1374 * outside of clipRect to the inside are NOT painted.
1377 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1378 const RECT *rect, const RECT *clipRect,
1379 HRGN hrgnUpdate, LPRECT rcUpdate,
1380 UINT flags )
1382 INT retVal = NULLREGION;
1383 BOOL bOwnRgn = TRUE;
1384 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1385 int rdw_flags;
1386 HRGN hrgnTemp;
1387 HRGN hrgnWinupd = 0;
1388 HDC hDC;
1389 RECT rc, cliprc;
1390 HWND hwndCaret = NULL;
1391 BOOL moveCaret = FALSE;
1392 POINT newCaretPos;
1394 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1395 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1396 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1397 if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1398 FIXME("some flags (%04x) are unhandled\n", flags);
1400 rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1401 RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
1403 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1404 hwnd = WIN_GetFullHandle( hwnd );
1406 GetClientRect(hwnd, &rc);
1408 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1409 else cliprc = rc;
1411 if (rect) IntersectRect(&rc, &rc, rect);
1413 if( hrgnUpdate ) bOwnRgn = FALSE;
1414 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1416 newCaretPos.x = newCaretPos.y = 0;
1418 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1419 DWORD dcxflags = DCX_CACHE;
1420 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1422 hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1424 if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1425 if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1426 dcxflags |= DCX_PARENTCLIP;
1427 if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1428 dcxflags |= DCX_CLIPCHILDREN;
1429 hDC = GetDCEx( hwnd, 0, dcxflags);
1430 if (hDC)
1432 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1434 ReleaseDC( hwnd, hDC );
1436 if (!bUpdate)
1437 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1440 /* If the windows has an update region, this must be
1441 * scrolled as well. Keep a copy in hrgnWinupd
1442 * to be added to hrngUpdate at the end. */
1443 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1444 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1445 if (retVal != NULLREGION)
1447 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1448 if( !bOwnRgn) {
1449 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1450 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1452 OffsetRgn( hrgnTemp, dx, dy );
1453 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1454 if( !bOwnRgn)
1455 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1456 RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1458 /* Catch the case where the scrolling amount exceeds the size of the
1459 * original window. This generated a second update area that is the
1460 * location where the original scrolled content would end up.
1461 * This second region is not returned by the ScrollDC and sets
1462 * ScrollWindowEx apart from just a ScrollDC.
1464 * This has been verified with testing on windows.
1466 if (abs(dx) > abs(rc.right - rc.left) ||
1467 abs(dy) > abs(rc.bottom - rc.top))
1469 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1470 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1471 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1473 if( !bOwnRgn)
1474 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1476 DeleteObject( hrgnClip );
1478 DeleteObject( hrgnTemp );
1479 } else {
1480 /* nothing was scrolled */
1481 if( !bOwnRgn)
1482 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1483 SetRectEmpty( rcUpdate);
1486 if( flags & SW_SCROLLCHILDREN )
1488 HWND *list = WIN_ListChildren( hwnd );
1489 if (list)
1491 int i;
1492 RECT r, dummy;
1493 for (i = 0; list[i]; i++)
1495 GetWindowRect( list[i], &r );
1496 MapWindowPoints( 0, hwnd, (POINT *)&r, 2 );
1497 if (!rect || IntersectRect(&dummy, &r, rect))
1498 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
1499 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1500 SWP_NOREDRAW | SWP_DEFERERASE );
1502 HeapFree( GetProcessHeap(), 0, list );
1506 if( flags & (SW_INVALIDATE | SW_ERASE) )
1507 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1508 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1510 if( hrgnWinupd) {
1511 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1512 DeleteObject( hrgnWinupd);
1515 if( hwndCaret ) {
1516 if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1517 ShowCaret(hwndCaret);
1520 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1522 return retVal;
1526 /*************************************************************************
1527 * ScrollWindow (USER32.@)
1530 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1531 const RECT *rect, const RECT *clipRect )
1533 return (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
1534 (rect ? 0 : SW_SCROLLCHILDREN) |
1535 SW_INVALIDATE | SW_ERASE ));
1539 /*************************************************************************
1540 * ScrollDC (USER32.@)
1542 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1543 * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1544 * logical coordinates.
1546 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
1547 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
1550 return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
1553 /************************************************************************
1554 * PrintWindow (USER32.@)
1557 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1559 UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1560 if(!(nFlags & PW_CLIENTONLY))
1562 flags |= PRF_NONCLIENT;
1564 SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1565 return TRUE;