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