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