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