shell32/tests: Add a few more ShellExecute() file URL tests.
[wine] / dlls / user32 / painting.c
1 /*
2  * Window painting functions
3  *
4  * Copyright 1993, 1994, 1995, 2001, 2004, 2005, 2008 Alexandre Julliard
5  * Copyright 1996, 1997, 1999 Alex Korobka
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28
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"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(win);
44
45
46 struct dce
47 {
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 */
55 };
56
57 static struct list dce_list = LIST_INIT(dce_list);
58
59 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam );
60
61 static const WCHAR displayW[] = { 'D','I','S','P','L','A','Y',0 };
62
63
64 /***********************************************************************
65  *           dump_rdw_flags
66  */
67 static void dump_rdw_flags(UINT flags)
68 {
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");
82
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)
96
97     if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
98     TRACE("\n");
99 #undef RDW_FLAGS
100 }
101
102
103 /***********************************************************************
104  *              update_visible_region
105  *
106  * Set the visible region and X11 drawable for the DC associated to
107  * a given window.
108  */
109 static void update_visible_region( struct dce *dce )
110 {
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;
119
120     /* don't clip siblings if using parent clip region */
121     if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
122
123     /* fetch the visible region from the server */
124     do
125     {
126         RGNDATA *data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 );
127         if (!data) return;
128
129         SERVER_START_REQ( get_visible_region )
130         {
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 )))
135             {
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 );
142
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;
152             }
153             else size = reply->total_size;
154         }
155         SERVER_END_REQ;
156         HeapFree( GetProcessHeap(), 0, data );
157     } while (status == STATUS_BUFFER_OVERFLOW);
158
159     if (status || !vis_rgn) return;
160
161     USER_Driver->pGetDC( dce->hdc, dce->hwnd, top_win, &win_rect, &top_rect, flags );
162
163     if (dce->clip_rgn) CombineRgn( vis_rgn, vis_rgn, dce->clip_rgn,
164                                    (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
165
166     if ((win = WIN_GetPtr( top_win )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
167     {
168         surface = win->surface;
169         if (surface) window_surface_add_ref( surface );
170         WIN_ReleasePtr( win );
171     }
172
173     if (!surface) top_rect = get_virtual_screen_rect();
174     __wine_set_visible_region( dce->hdc, vis_rgn, &win_rect, &top_rect, surface );
175     if (surface) window_surface_release( surface );
176 }
177
178
179 /***********************************************************************
180  *              reset_dce_attrs
181  */
182 static void reset_dce_attrs( struct dce *dce )
183 {
184     RestoreDC( dce->hdc, 1 );  /* initial save level is always 1 */
185     SaveDC( dce->hdc );  /* save the state again for next time */
186 }
187
188
189 /***********************************************************************
190  *              release_dce
191  */
192 static void release_dce( struct dce *dce )
193 {
194     RECT vis_rect;
195
196     if (!dce->hwnd) return;  /* already released */
197
198     vis_rect = get_virtual_screen_rect();
199     __wine_set_visible_region( dce->hdc, 0, &vis_rect, &vis_rect, NULL );
200     USER_Driver->pReleaseDC( dce->hwnd, dce->hdc );
201
202     if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
203     dce->clip_rgn = 0;
204     dce->hwnd     = 0;
205     dce->flags   &= DCX_CACHE;
206 }
207
208
209 /***********************************************************************
210  *   delete_clip_rgn
211  */
212 static void delete_clip_rgn( struct dce *dce )
213 {
214     if (!dce->clip_rgn) return;  /* nothing to do */
215
216     dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
217     DeleteObject( dce->clip_rgn );
218     dce->clip_rgn = 0;
219
220     /* make it dirty so that the vis rgn gets recomputed next time */
221     SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
222 }
223
224
225 /***********************************************************************
226  *           alloc_dce
227  *
228  * Allocate a new DCE.
229  */
230 static struct dce *alloc_dce(void)
231 {
232     struct dce *dce;
233
234     if (!(dce = HeapAlloc( GetProcessHeap(), 0, sizeof(*dce) ))) return NULL;
235     if (!(dce->hdc = CreateDCW( displayW, NULL, NULL, NULL )))
236     {
237         HeapFree( GetProcessHeap(), 0, dce );
238         return 0;
239     }
240     SaveDC( dce->hdc );
241
242     dce->hwnd      = 0;
243     dce->clip_rgn  = 0;
244     dce->flags     = 0;
245     dce->count     = 1;
246
247     /* store DCE handle in DC hook data field */
248     SetDCHook( dce->hdc, dc_hook, (DWORD_PTR)dce );
249     SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
250     return dce;
251 }
252
253
254 /***********************************************************************
255  *              get_window_dce
256  */
257 static struct dce *get_window_dce( HWND hwnd )
258 {
259     struct dce *dce;
260     WND *win = WIN_GetPtr( hwnd );
261
262     if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return NULL;
263
264     dce = win->dce;
265     if (!dce && (dce = get_class_dce( win->class )))
266     {
267         win->dce = dce;
268         dce->count++;
269     }
270     WIN_ReleasePtr( win );
271
272     if (!dce)  /* try to allocate one */
273     {
274         struct dce *dce_to_free = NULL;
275         LONG class_style = GetClassLongW( hwnd, GCL_STYLE );
276
277         if (class_style & CS_CLASSDC)
278         {
279             if (!(dce = alloc_dce())) return NULL;
280
281             win = WIN_GetPtr( hwnd );
282             if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
283             {
284                 if (win->dce)  /* another thread beat us to it */
285                 {
286                     dce_to_free = dce;
287                     dce = win->dce;
288                 }
289                 else if ((win->dce = set_class_dce( win->class, dce )) != dce)
290                 {
291                     dce_to_free = dce;
292                     dce = win->dce;
293                     dce->count++;
294                 }
295                 else
296                 {
297                     dce->count++;
298                     list_add_tail( &dce_list, &dce->entry );
299                 }
300                 WIN_ReleasePtr( win );
301             }
302             else dce_to_free = dce;
303         }
304         else if (class_style & CS_OWNDC)
305         {
306             if (!(dce = alloc_dce())) return NULL;
307
308             win = WIN_GetPtr( hwnd );
309             if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
310             {
311                 if (win->dwStyle & WS_CLIPCHILDREN) dce->flags |= DCX_CLIPCHILDREN;
312                 if (win->dwStyle & WS_CLIPSIBLINGS) dce->flags |= DCX_CLIPSIBLINGS;
313                 if (win->dce)  /* another thread beat us to it */
314                 {
315                     dce_to_free = dce;
316                     dce = win->dce;
317                 }
318                 else
319                 {
320                     win->dce = dce;
321                     dce->hwnd = hwnd;
322                     dce->count++;
323                     list_add_tail( &dce_list, &dce->entry );
324                 }
325                 WIN_ReleasePtr( win );
326             }
327             else dce_to_free = dce;
328         }
329
330         if (dce_to_free)
331         {
332             SetDCHook( dce_to_free->hdc, NULL, 0 );
333             DeleteDC( dce_to_free->hdc );
334             HeapFree( GetProcessHeap(), 0, dce_to_free );
335             if (dce_to_free == dce)
336                 dce = NULL;
337         }
338     }
339     return dce;
340 }
341
342
343 /***********************************************************************
344  *           free_dce
345  *
346  * Free a class or window DCE.
347  */
348 void free_dce( struct dce *dce, HWND hwnd )
349 {
350     USER_Lock();
351
352     if (dce)
353     {
354         if (!--dce->count)
355         {
356             /* turn it into a cache entry */
357             reset_dce_attrs( dce );
358             release_dce( dce );
359             dce->flags |= DCX_CACHE;
360         }
361         else if (dce->hwnd == hwnd)
362         {
363             release_dce( dce );
364         }
365     }
366
367     /* now check for cache DCEs */
368
369     if (hwnd)
370     {
371         LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
372         {
373             if (dce->hwnd != hwnd) continue;
374             if (!(dce->flags & DCX_CACHE)) continue;
375
376             if (dce->count) WARN( "GetDC() without ReleaseDC() for window %p\n", hwnd );
377             dce->count = 0;
378             release_dce( dce );
379         }
380     }
381
382     USER_Unlock();
383 }
384
385
386 /***********************************************************************
387  *           make_dc_dirty
388  *
389  * Mark the associated DC as dirty to force a refresh of the visible region
390  */
391 static void make_dc_dirty( struct dce *dce )
392 {
393     if (!dce->count)
394     {
395         /* Don't bother with visible regions of unused DCEs */
396         TRACE("\tpurged %p dce [%p]\n", dce, dce->hwnd);
397         release_dce( dce );
398     }
399     else
400     {
401         /* Set dirty bits in the hDC and DCE structs */
402         TRACE("\tfixed up %p dce [%p]\n", dce, dce->hwnd);
403         SetHookFlags( dce->hdc, DCHF_INVALIDATEVISRGN );
404     }
405 }
406
407
408 /***********************************************************************
409  *   invalidate_dce
410  *
411  * It is called from SetWindowPos() - we have to
412  * mark as dirty all busy DCEs for windows that have pWnd->parent as
413  * an ancestor and whose client rect intersects with specified update
414  * rectangle. In addition, pWnd->parent DCEs may need to be updated if
415  * DCX_CLIPCHILDREN flag is set.
416  */
417 void invalidate_dce( WND *win, const RECT *extra_rect )
418 {
419     RECT window_rect;
420     struct dce *dce;
421
422     if (!win->parent) return;
423
424     GetWindowRect( win->obj.handle, &window_rect );
425
426     TRACE("%p parent %p %s (%s)\n",
427           win->obj.handle, win->parent, wine_dbgstr_rect(&window_rect), wine_dbgstr_rect(extra_rect) );
428
429     /* walk all DCEs and fixup non-empty entries */
430
431     LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
432     {
433         TRACE( "%p: hwnd %p dcx %08x %s %s\n", dce, dce->hwnd, dce->flags,
434                (dce->flags & DCX_CACHE) ? "Cache" : "Owned", dce->count ? "InUse" : "" );
435
436         if (!dce->hwnd) continue;
437         if ((dce->hwnd == win->parent) && !(dce->flags & DCX_CLIPCHILDREN))
438             continue;  /* child window positions don't bother us */
439
440         /* if DCE window is a child of hwnd, it has to be invalidated */
441         if (dce->hwnd == win->obj.handle || IsChild( win->obj.handle, dce->hwnd ))
442         {
443             make_dc_dirty( dce );
444             continue;
445         }
446
447         /* otherwise check if the window rectangle intersects this DCE window */
448         if (win->parent == dce->hwnd || IsChild( win->parent, dce->hwnd ))
449         {
450             RECT dce_rect, tmp;
451             GetWindowRect( dce->hwnd, &dce_rect );
452             if (IntersectRect( &tmp, &dce_rect, &window_rect ) ||
453                 (extra_rect && IntersectRect( &tmp, &dce_rect, extra_rect )))
454                 make_dc_dirty( dce );
455         }
456     }
457 }
458
459 /***********************************************************************
460  *              release_dc
461  *
462  * Implementation of ReleaseDC.
463  */
464 static INT release_dc( HWND hwnd, HDC hdc, BOOL end_paint )
465 {
466     struct dce *dce;
467     BOOL ret = FALSE;
468
469     TRACE("%p %p\n", hwnd, hdc );
470
471     USER_Lock();
472     dce = (struct dce *)GetDCHook( hdc, NULL );
473     if (dce && dce->count)
474     {
475         if (!(dce->flags & DCX_NORESETATTRS)) reset_dce_attrs( dce );
476         if (end_paint || (dce->flags & DCX_CACHE)) delete_clip_rgn( dce );
477         if (dce->flags & DCX_CACHE) dce->count = 0;
478         ret = TRUE;
479     }
480     USER_Unlock();
481     return ret;
482 }
483
484
485 /***********************************************************************
486  *              dc_hook
487  *
488  * See "Undoc. Windows" for hints (DC, SetDCHook, SetHookFlags)..
489  */
490 static BOOL CALLBACK dc_hook( HDC hDC, WORD code, DWORD_PTR data, LPARAM lParam )
491 {
492     BOOL retv = TRUE;
493     struct dce *dce = (struct dce *)data;
494
495     TRACE("hDC = %p, %u\n", hDC, code);
496
497     if (!dce) return 0;
498     assert( dce->hdc == hDC );
499
500     switch( code )
501     {
502     case DCHC_INVALIDVISRGN:
503         /* GDI code calls this when it detects that the
504          * DC is dirty (usually after SetHookFlags()). This
505          * means that we have to recompute the visible region.
506          */
507         if (dce->count) update_visible_region( dce );
508         else /* non-fatal but shouldn't happen */
509             WARN("DC is not in use!\n");
510         break;
511     case DCHC_DELETEDC:
512         /*
513          * Windows will not let you delete a DC that is busy
514          * (between GetDC and ReleaseDC)
515          */
516         USER_Lock();
517         if (dce->count > 1)
518         {
519             WARN("Application trying to delete a busy DC %p\n", dce->hdc);
520             retv = FALSE;
521         }
522         else
523         {
524             list_remove( &dce->entry );
525             if (dce->clip_rgn) DeleteObject( dce->clip_rgn );
526             HeapFree( GetProcessHeap(), 0, dce );
527         }
528         USER_Unlock();
529         break;
530     }
531     return retv;
532 }
533
534
535 /***********************************************************************
536  *           get_update_region
537  *
538  * Return update region (in screen coordinates) for a window.
539  */
540 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
541 {
542     HRGN hrgn = 0;
543     NTSTATUS status;
544     RGNDATA *data;
545     size_t size = 256;
546
547     do
548     {
549         if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
550         {
551             SetLastError( ERROR_OUTOFMEMORY );
552             return 0;
553         }
554
555         SERVER_START_REQ( get_update_region )
556         {
557             req->window     = wine_server_user_handle( hwnd );
558             req->from_child = wine_server_user_handle( child ? *child : 0 );
559             req->flags      = *flags;
560             wine_server_set_reply( req, data->Buffer, size );
561             if (!(status = wine_server_call( req )))
562             {
563                 size_t reply_size = wine_server_reply_size( reply );
564                 data->rdh.dwSize   = sizeof(data->rdh);
565                 data->rdh.iType    = RDH_RECTANGLES;
566                 data->rdh.nCount   = reply_size / sizeof(RECT);
567                 data->rdh.nRgnSize = reply_size;
568                 hrgn = ExtCreateRegion( NULL, size, data );
569                 if (child) *child = wine_server_ptr_handle( reply->child );
570                 *flags = reply->flags;
571             }
572             else size = reply->total_size;
573         }
574         SERVER_END_REQ;
575         HeapFree( GetProcessHeap(), 0, data );
576     } while (status == STATUS_BUFFER_OVERFLOW);
577
578     if (status) SetLastError( RtlNtStatusToDosError(status) );
579     return hrgn;
580 }
581
582
583 /***********************************************************************
584  *           get_update_flags
585  *
586  * Get only the update flags, not the update region.
587  */
588 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
589 {
590     BOOL ret;
591
592     SERVER_START_REQ( get_update_region )
593     {
594         req->window     = wine_server_user_handle( hwnd );
595         req->from_child = wine_server_user_handle( child ? *child : 0 );
596         req->flags      = *flags | UPDATE_NOREGION;
597         if ((ret = !wine_server_call_err( req )))
598         {
599             if (child) *child = wine_server_ptr_handle( reply->child );
600             *flags = reply->flags;
601         }
602     }
603     SERVER_END_REQ;
604     return ret;
605 }
606
607
608 /***********************************************************************
609  *           redraw_window_rects
610  *
611  * Redraw part of a window.
612  */
613 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
614 {
615     BOOL ret;
616
617     if (!(flags & (RDW_INVALIDATE|RDW_VALIDATE|RDW_INTERNALPAINT|RDW_NOINTERNALPAINT)))
618         return TRUE;  /* nothing to do */
619
620     SERVER_START_REQ( redraw_window )
621     {
622         req->window = wine_server_user_handle( hwnd );
623         req->flags  = flags;
624         wine_server_add_data( req, rects, count * sizeof(RECT) );
625         ret = !wine_server_call_err( req );
626     }
627     SERVER_END_REQ;
628     return ret;
629 }
630
631
632 /***********************************************************************
633  *           send_ncpaint
634  *
635  * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
636  * Helper for erase_now and BeginPaint.
637  */
638 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
639 {
640     HRGN whole_rgn = get_update_region( hwnd, flags, child );
641     HRGN client_rgn = 0;
642
643     if (child) hwnd = *child;
644
645     if (hwnd == GetDesktopWindow()) return whole_rgn;
646
647     if (whole_rgn)
648     {
649         RECT client, update;
650         INT type;
651
652         /* check if update rgn overlaps with nonclient area */
653         type = GetRgnBox( whole_rgn, &update );
654         WIN_GetRectangles( hwnd, COORDS_SCREEN, 0, &client );
655
656         if ((*flags & UPDATE_NONCLIENT) ||
657             update.left < client.left || update.top < client.top ||
658             update.right > client.right || update.bottom > client.bottom)
659         {
660             client_rgn = CreateRectRgnIndirect( &client );
661             CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
662
663             /* check if update rgn contains complete nonclient area */
664             if (type == SIMPLEREGION)
665             {
666                 RECT window;
667                 GetWindowRect( hwnd, &window );
668                 if (EqualRect( &window, &update ))
669                 {
670                     DeleteObject( whole_rgn );
671                     whole_rgn = (HRGN)1;
672                 }
673             }
674         }
675         else
676         {
677             client_rgn = whole_rgn;
678             whole_rgn = 0;
679         }
680
681         if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
682         {
683             if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
684             if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
685         }
686     }
687     return client_rgn;
688 }
689
690
691 /***********************************************************************
692  *           send_erase
693  *
694  * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
695  * If a DC is requested, the region is selected into it. In all cases the region is deleted.
696  * Helper for erase_now and BeginPaint.
697  */
698 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
699                         RECT *clip_rect, HDC *hdc_ret )
700 {
701     BOOL need_erase = (flags & UPDATE_DELAYED_ERASE) != 0;
702     HDC hdc = 0;
703     RECT dummy;
704
705     if (!clip_rect) clip_rect = &dummy;
706     if (hdc_ret || (flags & UPDATE_ERASE))
707     {
708         UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
709         if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
710
711         if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
712         {
713             INT type = GetClipBox( hdc, clip_rect );
714
715             if (flags & UPDATE_ERASE)
716             {
717                 /* don't erase if the clip box is empty */
718                 if (type != NULLREGION)
719                     need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
720             }
721             if (!hdc_ret) release_dc( hwnd, hdc, TRUE );
722         }
723
724         if (hdc_ret) *hdc_ret = hdc;
725     }
726     if (!hdc) DeleteObject( client_rgn );
727     return need_erase;
728 }
729
730
731 /***********************************************************************
732  *           erase_now
733  *
734  * Implementation of RDW_ERASENOW behavior.
735  */
736 void erase_now( HWND hwnd, UINT rdw_flags )
737 {
738     HWND child = 0;
739     HRGN hrgn;
740     BOOL need_erase = FALSE;
741
742     /* loop while we find a child to repaint */
743     for (;;)
744     {
745         UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
746
747         if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
748         else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
749         if (need_erase) flags |= UPDATE_DELAYED_ERASE;
750
751         if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
752         need_erase = send_erase( child, flags, hrgn, NULL, NULL );
753
754         if (!flags) break;  /* nothing more to do */
755         if ((rdw_flags & RDW_NOCHILDREN) && !need_erase) break;
756     }
757 }
758
759
760 /***********************************************************************
761  *           move_window_bits
762  *
763  * Move the window bits when a window is resized or its surface recreated.
764  */
765 void move_window_bits( HWND hwnd, struct window_surface *old_surface,
766                        struct window_surface *new_surface,
767                        const RECT *visible_rect, const RECT *old_visible_rect,
768                        const RECT *client_rect, const RECT *valid_rects )
769 {
770     RECT dst = valid_rects[0];
771     RECT src = valid_rects[1];
772
773     if (new_surface != old_surface ||
774         src.left - old_visible_rect->left != dst.left - visible_rect->left ||
775         src.top - old_visible_rect->top != dst.top - visible_rect->top)
776     {
777         char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
778         BITMAPINFO *info = (BITMAPINFO *)buffer;
779         void *bits;
780         UINT flags = UPDATE_NOCHILDREN;
781         HRGN rgn = get_update_region( hwnd, &flags, NULL );
782         HDC hdc = GetDCEx( hwnd, rgn, DCX_CACHE | DCX_EXCLUDERGN );
783
784         OffsetRect( &dst, -client_rect->left, -client_rect->top );
785         TRACE( "copying  %s -> %s\n", wine_dbgstr_rect(&src), wine_dbgstr_rect(&dst) );
786         bits = old_surface->funcs->get_info( old_surface, info );
787         old_surface->funcs->lock( old_surface );
788         SetDIBitsToDevice( hdc, dst.left, dst.top, dst.right - dst.left, dst.bottom - dst.top,
789                            src.left - old_visible_rect->left - old_surface->rect.left,
790                            old_surface->rect.bottom - (src.bottom - old_visible_rect->top),
791                            0, old_surface->rect.bottom - old_surface->rect.top,
792                            bits, info, DIB_RGB_COLORS );
793         old_surface->funcs->unlock( old_surface );
794         ReleaseDC( hwnd, hdc );
795         DeleteObject( rgn );
796     }
797 }
798
799
800 /***********************************************************************
801  *           update_now
802  *
803  * Implementation of RDW_UPDATENOW behavior.
804  */
805 static void update_now( HWND hwnd, UINT rdw_flags )
806 {
807     HWND child = 0;
808
809     /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
810     if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
811
812     /* loop while we find a child to repaint */
813     for (;;)
814     {
815         UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
816
817         if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
818         else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
819
820         if (!get_update_flags( hwnd, &child, &flags )) break;
821         if (!flags) break;  /* nothing more to do */
822
823         SendMessageW( child, WM_PAINT, 0, 0 );
824         if (rdw_flags & RDW_NOCHILDREN) break;
825     }
826 }
827
828
829 /*************************************************************************
830  *             fix_caret
831  *
832  * Helper for ScrollWindowEx:
833  * If the return value is 0, no special caret handling is necessary.
834  * Otherwise the return value is the handle of the window that owns the
835  * caret. Its caret needs to be hidden during the scroll operation and
836  * moved to new_caret_pos if move_caret is TRUE.
837  */
838 static HWND fix_caret(HWND hWnd, const RECT *scroll_rect, INT dx, INT dy,
839                      UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
840 {
841     GUITHREADINFO info;
842     RECT rect, mapped_rcCaret;
843     BOOL hide_caret = FALSE;
844
845     info.cbSize = sizeof(info);
846     if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
847     if (!info.hwndCaret) return 0;
848     
849     if (info.hwndCaret == hWnd)
850     {
851         /* Move the caret if it's (partially) in the source rectangle */
852         if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
853         {
854             *move_caret = TRUE;
855             hide_caret = TRUE;
856             new_caret_pos->x = info.rcCaret.left + dx;
857             new_caret_pos->y = info.rcCaret.top + dy;
858         }
859         else
860         {
861             *move_caret = FALSE;
862             
863             /* Hide the caret if it's in the destination rectangle */
864             rect = *scroll_rect;
865             OffsetRect(&rect, dx, dy);
866             hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
867         }
868     }
869     else
870     {
871         if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
872         {
873             *move_caret = FALSE;
874             
875             /* Hide the caret if it's in the source or in the destination
876                rectangle */
877             mapped_rcCaret = info.rcCaret;
878             MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
879             
880             if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
881             {
882                 hide_caret = TRUE;
883             }
884             else
885             {
886                 rect = *scroll_rect;
887                 OffsetRect(&rect, dx, dy);
888                 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
889             }
890         }
891         else
892             return 0;
893     }
894
895     if (hide_caret)
896     {    
897         HideCaret(info.hwndCaret);
898         return info.hwndCaret;
899     }
900     else
901         return 0;
902 }
903
904
905 /***********************************************************************
906  *              BeginPaint (USER32.@)
907  */
908 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
909 {
910     HRGN hrgn;
911     UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
912
913     if (!lps) return 0;
914
915     HideCaret( hwnd );
916
917     if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
918
919     lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
920
921     TRACE("hdc = %p box = (%s), fErase = %d\n",
922           lps->hdc, wine_dbgstr_rect(&lps->rcPaint), lps->fErase);
923
924     return lps->hdc;
925 }
926
927
928 /***********************************************************************
929  *              EndPaint (USER32.@)
930  */
931 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
932 {
933     if (!lps) return FALSE;
934     release_dc( hwnd, lps->hdc, TRUE );
935     ShowCaret( hwnd );
936     flush_window_surfaces( FALSE );
937     return TRUE;
938 }
939
940
941 /***********************************************************************
942  *              GetDCEx (USER32.@)
943  */
944 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
945 {
946     const DWORD clip_flags = DCX_PARENTCLIP | DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW;
947     const DWORD user_flags = clip_flags | DCX_NORESETATTRS; /* flags that can be set by user */
948     struct dce *dce;
949     BOOL bUpdateVisRgn = TRUE;
950     HWND parent;
951     LONG window_style = GetWindowLongW( hwnd, GWL_STYLE );
952
953     if (!hwnd) hwnd = GetDesktopWindow();
954     else hwnd = WIN_GetFullHandle( hwnd );
955
956     TRACE("hwnd %p, hrgnClip %p, flags %08x\n", hwnd, hrgnClip, flags);
957
958     if (!IsWindow(hwnd)) return 0;
959
960     /* fixup flags */
961
962     if (flags & (DCX_WINDOW | DCX_PARENTCLIP)) flags |= DCX_CACHE;
963
964     if (flags & DCX_USESTYLE)
965     {
966         flags &= ~(DCX_CLIPCHILDREN | DCX_CLIPSIBLINGS | DCX_PARENTCLIP);
967
968         if (window_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
969
970         if (!(flags & DCX_WINDOW))
971         {
972             if (GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC) flags |= DCX_PARENTCLIP;
973
974             if (window_style & WS_CLIPCHILDREN && !(window_style & WS_MINIMIZE))
975                 flags |= DCX_CLIPCHILDREN;
976         }
977     }
978
979     if (flags & DCX_WINDOW) flags &= ~DCX_CLIPCHILDREN;
980
981     parent = GetAncestor( hwnd, GA_PARENT );
982     if (!parent || (parent == GetDesktopWindow()))
983         flags = (flags & ~DCX_PARENTCLIP) | DCX_CLIPSIBLINGS;
984
985     /* it seems parent clip is ignored when clipping siblings or children */
986     if (flags & (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN)) flags &= ~DCX_PARENTCLIP;
987
988     if( flags & DCX_PARENTCLIP )
989     {
990         LONG parent_style = GetWindowLongW( parent, GWL_STYLE );
991         if( (window_style & WS_VISIBLE) && (parent_style & WS_VISIBLE) )
992         {
993             flags &= ~DCX_CLIPCHILDREN;
994             if (parent_style & WS_CLIPSIBLINGS) flags |= DCX_CLIPSIBLINGS;
995         }
996     }
997
998     /* find a suitable DCE */
999
1000     if ((flags & DCX_CACHE) || !(dce = get_window_dce( hwnd )))
1001     {
1002         struct dce *dceEmpty = NULL, *dceUnused = NULL;
1003
1004         /* Strategy: First, we attempt to find a non-empty but unused DCE with
1005          * compatible flags. Next, we look for an empty entry. If the cache is
1006          * full we have to purge one of the unused entries.
1007          */
1008         USER_Lock();
1009         LIST_FOR_EACH_ENTRY( dce, &dce_list, struct dce, entry )
1010         {
1011             if ((dce->flags & DCX_CACHE) && !dce->count)
1012             {
1013                 dceUnused = dce;
1014
1015                 if (!dce->hwnd) dceEmpty = dce;
1016                 else if ((dce->hwnd == hwnd) && !((dce->flags ^ flags) & clip_flags))
1017                 {
1018                     TRACE("\tfound valid %p dce [%p], flags %08x\n",
1019                           dce, hwnd, dce->flags );
1020                     bUpdateVisRgn = FALSE;
1021                     break;
1022                 }
1023             }
1024         }
1025
1026         if (&dce->entry == &dce_list)  /* nothing found */
1027             dce = dceEmpty ? dceEmpty : dceUnused;
1028
1029         if (dce) dce->count = 1;
1030
1031         USER_Unlock();
1032
1033         /* if there's no dce empty or unused, allocate a new one */
1034         if (!dce)
1035         {
1036             if (!(dce = alloc_dce())) return 0;
1037             dce->flags = DCX_CACHE;
1038             USER_Lock();
1039             list_add_head( &dce_list, &dce->entry );
1040             USER_Unlock();
1041         }
1042     }
1043     else
1044     {
1045         flags |= DCX_NORESETATTRS;
1046         if (dce->hwnd == hwnd)
1047         {
1048             TRACE("\tskipping hVisRgn update\n");
1049             bUpdateVisRgn = FALSE; /* updated automatically, via DCHook() */
1050         }
1051         else
1052         {
1053             /* we should free dce->clip_rgn here, but Windows apparently doesn't */
1054             dce->flags &= ~(DCX_EXCLUDERGN | DCX_INTERSECTRGN);
1055             dce->clip_rgn = 0;
1056         }
1057     }
1058
1059     if (flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN))
1060     {
1061         /* if the extra clip region has changed, get rid of the old one */
1062         if (dce->clip_rgn != hrgnClip || ((flags ^ dce->flags) & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)))
1063             delete_clip_rgn( dce );
1064         dce->clip_rgn = hrgnClip;
1065         if (!dce->clip_rgn) dce->clip_rgn = CreateRectRgn( 0, 0, 0, 0 );
1066         dce->flags |= flags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN);
1067         bUpdateVisRgn = TRUE;
1068     }
1069
1070     if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) SetLayout( dce->hdc, LAYOUT_RTL );
1071
1072     dce->hwnd = hwnd;
1073     dce->flags = (dce->flags & ~user_flags) | (flags & user_flags);
1074
1075     if (SetHookFlags( dce->hdc, DCHF_VALIDATEVISRGN )) bUpdateVisRgn = TRUE;  /* DC was dirty */
1076
1077     if (bUpdateVisRgn) update_visible_region( dce );
1078
1079     TRACE("(%p,%p,0x%x): returning %p\n", hwnd, hrgnClip, flags, dce->hdc);
1080     return dce->hdc;
1081 }
1082
1083
1084 /***********************************************************************
1085  *              GetDC (USER32.@)
1086  *
1087  * Get a device context.
1088  *
1089  * RETURNS
1090  *      Success: Handle to the device context
1091  *      Failure: NULL.
1092  */
1093 HDC WINAPI GetDC( HWND hwnd )
1094 {
1095     if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
1096     return GetDCEx( hwnd, 0, DCX_USESTYLE );
1097 }
1098
1099
1100 /***********************************************************************
1101  *              GetWindowDC (USER32.@)
1102  */
1103 HDC WINAPI GetWindowDC( HWND hwnd )
1104 {
1105     return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
1106 }
1107
1108
1109 /***********************************************************************
1110  *              ReleaseDC (USER32.@)
1111  *
1112  * Release a device context.
1113  *
1114  * RETURNS
1115  *      Success: Non-zero. Resources used by hdc are released.
1116  *      Failure: 0.
1117  */
1118 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
1119 {
1120     return release_dc( hwnd, hdc, FALSE );
1121 }
1122
1123
1124 /**********************************************************************
1125  *              WindowFromDC (USER32.@)
1126  */
1127 HWND WINAPI WindowFromDC( HDC hdc )
1128 {
1129     struct dce *dce;
1130     HWND hwnd = 0;
1131
1132     USER_Lock();
1133     dce = (struct dce *)GetDCHook( hdc, NULL );
1134     if (dce) hwnd = dce->hwnd;
1135     USER_Unlock();
1136     return hwnd;
1137 }
1138
1139
1140 /***********************************************************************
1141  *              LockWindowUpdate (USER32.@)
1142  *
1143  * Enables or disables painting in the chosen window.
1144  *
1145  * PARAMS
1146  *  hwnd [I] handle to a window.
1147  *
1148  * RETURNS
1149  *  If successful, returns nonzero value. Otherwise,
1150  *  returns 0.
1151  *
1152  * NOTES
1153  *  You can lock only one window at a time.
1154  */
1155 BOOL WINAPI LockWindowUpdate( HWND hwnd )
1156 {
1157     static HWND lockedWnd;
1158
1159     FIXME("(%p), partial stub!\n",hwnd);
1160
1161     USER_Lock();
1162     if (lockedWnd)
1163     {
1164         if (!hwnd)
1165         {
1166             /* Unlock lockedWnd */
1167             /* FIXME: Do something */
1168         }
1169         else
1170         {
1171             /* Attempted to lock a second window */
1172             /* Return FALSE and do nothing */
1173             USER_Unlock();
1174             return FALSE;
1175         }
1176     }
1177     lockedWnd = hwnd;
1178     USER_Unlock();
1179     return TRUE;
1180 }
1181
1182
1183 /***********************************************************************
1184  *              RedrawWindow (USER32.@)
1185  */
1186 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
1187 {
1188     static const RECT empty;
1189     BOOL ret;
1190
1191     if (!hwnd) hwnd = GetDesktopWindow();
1192
1193     if (TRACE_ON(win))
1194     {
1195         if (hrgn)
1196         {
1197             RECT r;
1198             GetRgnBox( hrgn, &r );
1199             TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
1200         }
1201         else if (rect)
1202             TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
1203         else
1204             TRACE( "%p whole window ", hwnd );
1205
1206         dump_rdw_flags(flags);
1207     }
1208
1209     /* process pending expose events before painting */
1210     if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
1211
1212     if (rect && !hrgn)
1213     {
1214         if (IsRectEmpty( rect )) rect = &empty;
1215         ret = redraw_window_rects( hwnd, flags, rect, 1 );
1216     }
1217     else if (!hrgn)
1218     {
1219         ret = redraw_window_rects( hwnd, flags, NULL, 0 );
1220     }
1221     else  /* need to build a list of the region rectangles */
1222     {
1223         DWORD size;
1224         RGNDATA *data = NULL;
1225
1226         if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
1227         if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
1228         GetRegionData( hrgn, size, data );
1229         if (!data->rdh.nCount)  /* empty region -> use a single all-zero rectangle */
1230             ret = redraw_window_rects( hwnd, flags, &empty, 1 );
1231         else
1232             ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
1233         HeapFree( GetProcessHeap(), 0, data );
1234     }
1235
1236     if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
1237     else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
1238
1239     return ret;
1240 }
1241
1242
1243 /***********************************************************************
1244  *              UpdateWindow (USER32.@)
1245  */
1246 BOOL WINAPI UpdateWindow( HWND hwnd )
1247 {
1248     if (!hwnd)
1249     {
1250         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1251         return FALSE;
1252     }
1253
1254     return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1255 }
1256
1257
1258 /***********************************************************************
1259  *              InvalidateRgn (USER32.@)
1260  */
1261 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1262 {
1263     if (!hwnd)
1264     {
1265         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1266         return FALSE;
1267     }
1268
1269     return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
1270 }
1271
1272
1273 /***********************************************************************
1274  *              InvalidateRect (USER32.@)
1275  *
1276  * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
1277  * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1278  */
1279 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
1280 {
1281     UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
1282
1283     if (!hwnd)
1284     {
1285         flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1286         rect = NULL;
1287     }
1288
1289     return RedrawWindow( hwnd, rect, 0, flags );
1290 }
1291
1292
1293 /***********************************************************************
1294  *              ValidateRgn (USER32.@)
1295  */
1296 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
1297 {
1298     if (!hwnd)
1299     {
1300         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1301         return FALSE;
1302     }
1303
1304     return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
1305 }
1306
1307
1308 /***********************************************************************
1309  *              ValidateRect (USER32.@)
1310  *
1311  * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
1312  * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
1313  */
1314 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
1315 {
1316     UINT flags = RDW_VALIDATE;
1317
1318     if (!hwnd)
1319     {
1320         flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
1321         rect = NULL;
1322     }
1323
1324     return RedrawWindow( hwnd, rect, 0, flags );
1325 }
1326
1327
1328 /***********************************************************************
1329  *              GetUpdateRgn (USER32.@)
1330  */
1331 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
1332 {
1333     INT retval = ERROR;
1334     UINT flags = UPDATE_NOCHILDREN;
1335     HRGN update_rgn;
1336
1337     if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1338
1339     if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
1340     {
1341         retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
1342         if (send_erase( hwnd, flags, update_rgn, NULL, NULL ))
1343         {
1344             flags = UPDATE_DELAYED_ERASE;
1345             get_update_flags( hwnd, NULL, &flags );
1346         }
1347         /* map region to client coordinates */
1348         map_window_region( 0, hwnd, hrgn );
1349     }
1350     return retval;
1351 }
1352
1353
1354 /***********************************************************************
1355  *              GetUpdateRect (USER32.@)
1356  */
1357 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
1358 {
1359     UINT flags = UPDATE_NOCHILDREN;
1360     HRGN update_rgn;
1361     BOOL need_erase;
1362
1363     if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
1364
1365     if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
1366
1367     if (rect)
1368     {
1369         if (GetRgnBox( update_rgn, rect ) != NULLREGION)
1370         {
1371             HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
1372             DWORD layout = SetLayout( hdc, 0 );  /* MapWindowPoints mirrors already */
1373             MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
1374             DPtoLP( hdc, (LPPOINT)rect, 2 );
1375             SetLayout( hdc, layout );
1376             ReleaseDC( hwnd, hdc );
1377         }
1378     }
1379     need_erase = send_erase( hwnd, flags, update_rgn, NULL, NULL );
1380
1381     /* check if we still have an update region */
1382     flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
1383     if (need_erase) flags |= UPDATE_DELAYED_ERASE;
1384     return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
1385 }
1386
1387
1388 /***********************************************************************
1389  *              ExcludeUpdateRgn (USER32.@)
1390  */
1391 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
1392 {
1393     HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
1394     INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
1395
1396     if (ret != ERROR)
1397     {
1398         POINT pt;
1399
1400         GetDCOrgEx( hdc, &pt );
1401         MapWindowPoints( 0, hwnd, &pt, 1 );
1402         OffsetRgn( update_rgn, -pt.x, -pt.y );
1403         ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
1404         DeleteObject( update_rgn );
1405     }
1406     return ret;
1407 }
1408
1409
1410 static INT scroll_window( HWND hwnd, INT dx, INT dy, const RECT *rect, const RECT *clipRect,
1411                           HRGN hrgnUpdate, LPRECT rcUpdate, UINT flags, BOOL is_ex )
1412 {
1413     INT   retVal = NULLREGION;
1414     BOOL  bOwnRgn = TRUE;
1415     BOOL  bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
1416     int rdw_flags;
1417     HRGN  hrgnTemp;
1418     HRGN  hrgnWinupd = 0;
1419     HDC   hDC;
1420     RECT  rc, cliprc;
1421     HWND hwndCaret = NULL;
1422     BOOL moveCaret = FALSE;
1423     POINT newCaretPos;
1424
1425     TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
1426            hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
1427     TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
1428     if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
1429         FIXME("some flags (%04x) are unhandled\n", flags);
1430
1431     rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
1432                                 RDW_INVALIDATE | RDW_ERASE  : RDW_INVALIDATE ;
1433
1434     if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
1435     hwnd = WIN_GetFullHandle( hwnd );
1436
1437     GetClientRect(hwnd, &rc);
1438
1439     if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
1440     else cliprc = rc;
1441
1442     if (rect) IntersectRect(&rc, &rc, rect);
1443
1444     if( hrgnUpdate ) bOwnRgn = FALSE;
1445     else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
1446
1447     newCaretPos.x = newCaretPos.y = 0;
1448
1449     if( !IsRectEmpty(&cliprc) && (dx || dy)) {
1450         DWORD dcxflags = 0;
1451         DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
1452
1453         hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
1454
1455         if (is_ex) dcxflags |= DCX_CACHE;
1456         if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
1457         if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
1458             dcxflags |= DCX_PARENTCLIP;
1459         if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
1460             dcxflags |= DCX_CLIPCHILDREN;
1461         hDC = GetDCEx( hwnd, 0, dcxflags);
1462         if (hDC)
1463         {
1464             ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
1465
1466             ReleaseDC( hwnd, hDC );
1467
1468             if (!bUpdate)
1469                 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
1470         }
1471
1472         /* If the windows has an update region, this must be
1473          * scrolled as well. Keep a copy in hrgnWinupd
1474          * to be added to hrngUpdate at the end. */
1475         hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
1476         retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
1477         if (retVal != NULLREGION)
1478         {
1479             HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
1480             if( !bOwnRgn) {
1481                 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
1482                 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
1483             }
1484             OffsetRgn( hrgnTemp, dx, dy );
1485             CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1486             if( !bOwnRgn)
1487                 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1488             RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
1489
1490            /* Catch the case where the scrolling amount exceeds the size of the
1491             * original window. This generated a second update area that is the
1492             * location where the original scrolled content would end up.
1493             * This second region is not returned by the ScrollDC and sets
1494             * ScrollWindowEx apart from just a ScrollDC.
1495             *
1496             * This has been verified with testing on windows.
1497             */
1498             if (abs(dx) > abs(rc.right - rc.left) ||
1499                 abs(dy) > abs(rc.bottom - rc.top))
1500             {
1501                 SetRectRgn( hrgnTemp, rc.left + dx, rc.top + dy, rc.right+dx, rc.bottom + dy);
1502                 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
1503                 CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp, RGN_OR );
1504
1505                 if( !bOwnRgn)
1506                     CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
1507             }
1508             DeleteObject( hrgnClip );
1509         }
1510         DeleteObject( hrgnTemp );
1511     } else {
1512         /* nothing was scrolled */
1513         if( !bOwnRgn)
1514             SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
1515         SetRectEmpty( rcUpdate);
1516     }
1517
1518     if( flags & SW_SCROLLCHILDREN )
1519     {
1520         HWND *list = WIN_ListChildren( hwnd );
1521         if (list)
1522         {
1523             int i;
1524             RECT r, dummy;
1525             for (i = 0; list[i]; i++)
1526             {
1527                 WIN_GetRectangles( list[i], COORDS_PARENT, &r, NULL );
1528                 if (!rect || IntersectRect(&dummy, &r, rect))
1529                     SetWindowPos( list[i], 0, r.left + dx, r.top  + dy, 0, 0,
1530                                   SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
1531                                   SWP_NOREDRAW | SWP_DEFERERASE );
1532             }
1533             HeapFree( GetProcessHeap(), 0, list );
1534         }
1535     }
1536
1537     if( flags & (SW_INVALIDATE | SW_ERASE) )
1538         RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
1539                       ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
1540
1541     if( hrgnWinupd) {
1542         CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
1543         DeleteObject( hrgnWinupd);
1544     }
1545
1546     if( hwndCaret ) {
1547         if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
1548         ShowCaret(hwndCaret);
1549     }
1550
1551     if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
1552
1553     return retVal;
1554 }
1555
1556
1557 /*************************************************************************
1558  *              ScrollWindowEx (USER32.@)
1559  *
1560  * Note: contrary to what the doc says, pixels that are scrolled from the
1561  *      outside of clipRect to the inside are NOT painted.
1562  *
1563  */
1564 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
1565                            const RECT *rect, const RECT *clipRect,
1566                            HRGN hrgnUpdate, LPRECT rcUpdate,
1567                            UINT flags )
1568 {
1569     return scroll_window( hwnd, dx, dy, rect, clipRect, hrgnUpdate, rcUpdate, flags, TRUE );
1570 }
1571
1572 /*************************************************************************
1573  *              ScrollWindow (USER32.@)
1574  *
1575  */
1576 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
1577                           const RECT *rect, const RECT *clipRect )
1578 {
1579     return scroll_window( hwnd, dx, dy, rect, clipRect, 0, NULL,
1580                           SW_INVALIDATE | SW_ERASE | (rect ? 0 : SW_SCROLLCHILDREN), FALSE ) != ERROR;
1581 }
1582
1583
1584 /*************************************************************************
1585  *              ScrollDC (USER32.@)
1586  *
1587  * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
1588  * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
1589  * logical coordinates.
1590  */
1591 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
1592                       const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
1593
1594 {
1595     return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
1596 }
1597
1598 /************************************************************************
1599  *              PrintWindow (USER32.@)
1600  *
1601  */
1602 BOOL WINAPI PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
1603 {
1604     UINT flags = PRF_CHILDREN | PRF_ERASEBKGND | PRF_OWNED | PRF_CLIENT;
1605     if(!(nFlags & PW_CLIENTONLY))
1606     {
1607         flags |= PRF_NONCLIENT;
1608     }
1609     SendMessageW(hwnd, WM_PRINT, (WPARAM)hdcBlt, flags);
1610     return TRUE;
1611 }