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