user32: Activate an MDI child on WM_SETFOCUS as well as on WM_CHILDACTIVATE.
[wine] / dlls / user32 / painting.c
1 /*
2  * Window painting functions
3  *
4  * Copyright 1993, 1994, 1995, 2001, 2004 Alexandre Julliard
5  * Copyright 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 <stdarg.h>
26 #include <string.h>
27
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "wine/server.h"
35 #include "win.h"
36 #include "user_private.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(win);
40
41
42 /***********************************************************************
43  *           dump_rdw_flags
44  */
45 static void dump_rdw_flags(UINT flags)
46 {
47     TRACE("flags:");
48     if (flags & RDW_INVALIDATE) TRACE(" RDW_INVALIDATE");
49     if (flags & RDW_INTERNALPAINT) TRACE(" RDW_INTERNALPAINT");
50     if (flags & RDW_ERASE) TRACE(" RDW_ERASE");
51     if (flags & RDW_VALIDATE) TRACE(" RDW_VALIDATE");
52     if (flags & RDW_NOINTERNALPAINT) TRACE(" RDW_NOINTERNALPAINT");
53     if (flags & RDW_NOERASE) TRACE(" RDW_NOERASE");
54     if (flags & RDW_NOCHILDREN) TRACE(" RDW_NOCHILDREN");
55     if (flags & RDW_ALLCHILDREN) TRACE(" RDW_ALLCHILDREN");
56     if (flags & RDW_UPDATENOW) TRACE(" RDW_UPDATENOW");
57     if (flags & RDW_ERASENOW) TRACE(" RDW_ERASENOW");
58     if (flags & RDW_FRAME) TRACE(" RDW_FRAME");
59     if (flags & RDW_NOFRAME) TRACE(" RDW_NOFRAME");
60
61 #define RDW_FLAGS \
62     (RDW_INVALIDATE | \
63     RDW_INTERNALPAINT | \
64     RDW_ERASE | \
65     RDW_VALIDATE | \
66     RDW_NOINTERNALPAINT | \
67     RDW_NOERASE | \
68     RDW_NOCHILDREN | \
69     RDW_ALLCHILDREN | \
70     RDW_UPDATENOW | \
71     RDW_ERASENOW | \
72     RDW_FRAME | \
73     RDW_NOFRAME)
74
75     if (flags & ~RDW_FLAGS) TRACE(" %04x", flags & ~RDW_FLAGS);
76     TRACE("\n");
77 #undef RDW_FLAGS
78 }
79
80
81 /***********************************************************************
82  *           get_update_region
83  *
84  * Return update region (in screen coordinates) for a window.
85  */
86 static HRGN get_update_region( HWND hwnd, UINT *flags, HWND *child )
87 {
88     HRGN hrgn = 0;
89     NTSTATUS status;
90     RGNDATA *data;
91     size_t size = 256;
92
93     do
94     {
95         if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
96         {
97             SetLastError( ERROR_OUTOFMEMORY );
98             return 0;
99         }
100
101         SERVER_START_REQ( get_update_region )
102         {
103             req->window     = hwnd;
104             req->from_child = child ? *child : 0;
105             req->flags      = *flags;
106             wine_server_set_reply( req, data->Buffer, size );
107             if (!(status = wine_server_call( req )))
108             {
109                 size_t reply_size = wine_server_reply_size( reply );
110                 data->rdh.dwSize   = sizeof(data->rdh);
111                 data->rdh.iType    = RDH_RECTANGLES;
112                 data->rdh.nCount   = reply_size / sizeof(RECT);
113                 data->rdh.nRgnSize = reply_size;
114                 hrgn = ExtCreateRegion( NULL, size, data );
115                 if (child) *child = reply->child;
116                 *flags = reply->flags;
117             }
118             else size = reply->total_size;
119         }
120         SERVER_END_REQ;
121         HeapFree( GetProcessHeap(), 0, data );
122     } while (status == STATUS_BUFFER_OVERFLOW);
123
124     if (status) SetLastError( RtlNtStatusToDosError(status) );
125     return hrgn;
126 }
127
128
129 /***********************************************************************
130  *           get_update_flags
131  *
132  * Get only the update flags, not the update region.
133  */
134 static BOOL get_update_flags( HWND hwnd, HWND *child, UINT *flags )
135 {
136     BOOL ret;
137
138     SERVER_START_REQ( get_update_region )
139     {
140         req->window     = hwnd;
141         req->from_child = child ? *child : 0;
142         req->flags      = *flags | UPDATE_NOREGION;
143         if ((ret = !wine_server_call_err( req )))
144         {
145             if (child) *child = reply->child;
146             *flags = reply->flags;
147         }
148     }
149     SERVER_END_REQ;
150     return ret;
151 }
152
153
154 /***********************************************************************
155  *           redraw_window_rects
156  *
157  * Redraw part of a window.
158  */
159 static BOOL redraw_window_rects( HWND hwnd, UINT flags, const RECT *rects, UINT count )
160 {
161     BOOL ret;
162
163     SERVER_START_REQ( redraw_window )
164     {
165         req->window = hwnd;
166         req->flags  = flags;
167         wine_server_add_data( req, rects, count * sizeof(RECT) );
168         ret = !wine_server_call_err( req );
169     }
170     SERVER_END_REQ;
171     return ret;
172 }
173
174
175 /***********************************************************************
176  *           send_ncpaint
177  *
178  * Send a WM_NCPAINT message if needed, and return the resulting update region (in screen coords).
179  * Helper for erase_now and BeginPaint.
180  */
181 static HRGN send_ncpaint( HWND hwnd, HWND *child, UINT *flags )
182 {
183     HRGN whole_rgn = get_update_region( hwnd, flags, child );
184     HRGN client_rgn = 0;
185
186     if (child) hwnd = *child;
187
188     if (whole_rgn)
189     {
190         RECT client, update;
191         INT type;
192
193         /* check if update rgn overlaps with nonclient area */
194         type = GetRgnBox( whole_rgn, &update );
195         GetClientRect( hwnd, &client );
196         MapWindowPoints( hwnd, 0, (POINT *)&client, 2 );
197
198         if ((*flags & UPDATE_NONCLIENT) ||
199             update.left < client.left || update.top < client.top ||
200             update.right > client.right || update.bottom > client.bottom)
201         {
202             client_rgn = CreateRectRgnIndirect( &client );
203             CombineRgn( client_rgn, client_rgn, whole_rgn, RGN_AND );
204
205             /* check if update rgn contains complete nonclient area */
206             if (type == SIMPLEREGION)
207             {
208                 RECT window;
209                 GetWindowRect( hwnd, &window );
210                 if (EqualRect( &window, &update ))
211                 {
212                     DeleteObject( whole_rgn );
213                     whole_rgn = (HRGN)1;
214                 }
215             }
216         }
217         else
218         {
219             client_rgn = whole_rgn;
220             whole_rgn = 0;
221         }
222
223         if (whole_rgn) /* NOTE: WM_NCPAINT allows wParam to be 1 */
224         {
225             if (*flags & UPDATE_NONCLIENT) SendMessageW( hwnd, WM_NCPAINT, (WPARAM)whole_rgn, 0 );
226             if (whole_rgn > (HRGN)1) DeleteObject( whole_rgn );
227         }
228     }
229     return client_rgn;
230 }
231
232
233 /***********************************************************************
234  *           send_erase
235  *
236  * Send a WM_ERASEBKGND message if needed, and optionally return the DC for painting.
237  * If a DC is requested, the region is selected into it. In all cases the region is deleted.
238  * Helper for erase_now and BeginPaint.
239  */
240 static BOOL send_erase( HWND hwnd, UINT flags, HRGN client_rgn,
241                         RECT *clip_rect, HDC *hdc_ret )
242 {
243     BOOL need_erase = FALSE;
244     HDC hdc = 0;
245     RECT dummy;
246
247     if (!clip_rect) clip_rect = &dummy;
248     if (hdc_ret || (flags & UPDATE_ERASE))
249     {
250         UINT dcx_flags = DCX_INTERSECTRGN | DCX_USESTYLE;
251         if (IsIconic(hwnd)) dcx_flags |= DCX_WINDOW;
252
253         if ((hdc = GetDCEx( hwnd, client_rgn, dcx_flags )))
254         {
255             INT type = GetClipBox( hdc, clip_rect );
256
257             if (flags & UPDATE_ERASE)
258             {
259                 /* don't erase if the clip box is empty */
260                 if (type != NULLREGION)
261                     need_erase = !SendMessageW( hwnd, WM_ERASEBKGND, (WPARAM)hdc, 0 );
262             }
263             if (!hdc_ret)
264             {
265                 if (need_erase && hwnd != GetDesktopWindow())  /* FIXME: mark it as needing erase again */
266                     RedrawWindow( hwnd, clip_rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_NOCHILDREN );
267                 USER_Driver->pReleaseDC( hwnd, hdc, TRUE );
268             }
269         }
270
271         if (hdc_ret) *hdc_ret = hdc;
272     }
273     if (!hdc) DeleteObject( client_rgn );
274     return need_erase;
275 }
276
277
278 /***********************************************************************
279  *           erase_now
280  *
281  * Implementation of RDW_ERASENOW behavior.
282  */
283 static void erase_now( HWND hwnd, UINT rdw_flags )
284 {
285     HWND child = 0;
286     HRGN hrgn;
287
288     /* loop while we find a child to repaint */
289     for (;;)
290     {
291         UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE;
292
293         if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
294         else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
295
296         if (!(hrgn = send_ncpaint( hwnd, &child, &flags ))) break;
297         send_erase( child, flags, hrgn, NULL, NULL );
298
299         if (!flags) break;  /* nothing more to do */
300         if (rdw_flags & RDW_NOCHILDREN) break;
301     }
302 }
303
304
305 /***********************************************************************
306  *           update_now
307  *
308  * Implementation of RDW_UPDATENOW behavior.
309  *
310  * FIXME: Windows uses WM_SYNCPAINT to cut down the number of intertask
311  * SendMessage() calls. This is a comment inside DefWindowProc() source
312  * from 16-bit SDK:
313  *
314  *   This message avoids lots of inter-app message traffic
315  *   by switching to the other task and continuing the
316  *   recursion there.
317  *
318  * wParam         = flags
319  * LOWORD(lParam) = hrgnClip
320  * HIWORD(lParam) = hwndSkip  (not used; always NULL)
321  *
322  */
323 static void update_now( HWND hwnd, UINT rdw_flags )
324 {
325     HWND child = 0;
326
327     /* desktop window never gets WM_PAINT, only WM_ERASEBKGND */
328     if (hwnd == GetDesktopWindow()) erase_now( hwnd, rdw_flags | RDW_NOCHILDREN );
329
330     /* loop while we find a child to repaint */
331     for (;;)
332     {
333         UINT flags = UPDATE_PAINT | UPDATE_INTERNALPAINT;
334
335         if (rdw_flags & RDW_NOCHILDREN) flags |= UPDATE_NOCHILDREN;
336         else if (rdw_flags & RDW_ALLCHILDREN) flags |= UPDATE_ALLCHILDREN;
337
338         if (!get_update_flags( hwnd, &child, &flags )) break;
339         if (!flags) break;  /* nothing more to do */
340
341         SendMessageW( child, WM_PAINT, 0, 0 );
342         if (rdw_flags & RDW_NOCHILDREN) break;
343     }
344 }
345
346
347 /*************************************************************************
348  *             fix_caret
349  *
350  * Helper for ScrollWindowEx:
351  * If the return value is 0, no special caret handling is necessary.
352  * Otherwise the return value is the handle of the window that owns the
353  * caret. Its caret needs to be hidden during the scroll operation and
354  * moved to new_caret_pos if move_caret is TRUE.
355  */
356 static HWND fix_caret(HWND hWnd, const LPRECT scroll_rect, INT dx, INT dy,
357                      UINT flags, LPBOOL move_caret, LPPOINT new_caret_pos)
358 {
359     GUITHREADINFO info;
360     RECT rect, mapped_rcCaret;
361     BOOL hide_caret = FALSE;
362
363     if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
364     if (!info.hwndCaret) return 0;
365     
366     if (info.hwndCaret == hWnd)
367     {
368         /* Move the caret if it's (partially) in the source rectangle */
369         if (IntersectRect(&rect, scroll_rect, &info.rcCaret))
370         {
371             *move_caret = TRUE;
372             hide_caret = TRUE;
373             new_caret_pos->x = info.rcCaret.left + dx;
374             new_caret_pos->y = info.rcCaret.top + dy;
375         }
376         else
377         {
378             *move_caret = FALSE;
379             
380             /* Hide the caret if it's in the destination rectangle */
381             rect = *scroll_rect;
382             OffsetRect(&rect, dx, dy);
383             hide_caret = IntersectRect(&rect, &rect, &info.rcCaret);
384         }
385     }
386     else
387     {
388         if ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret))
389         {
390             *move_caret = FALSE;
391             
392             /* Hide the caret if it's in the source or in the destination
393                rectangle */
394             mapped_rcCaret = info.rcCaret;
395             MapWindowPoints(info.hwndCaret, hWnd, (LPPOINT)&mapped_rcCaret, 2);
396             
397             if (IntersectRect(&rect, scroll_rect, &mapped_rcCaret))
398             {
399                 hide_caret = TRUE;
400             }
401             else
402             {
403                 rect = *scroll_rect;
404                 OffsetRect(&rect, dx, dy);
405                 hide_caret = IntersectRect(&rect, &rect, &mapped_rcCaret);
406             }
407         }
408         else
409             return 0;
410     }
411
412     if (hide_caret)
413     {    
414         HideCaret(info.hwndCaret);
415         return info.hwndCaret;
416     }
417     else
418         return 0;
419 }
420
421
422 /***********************************************************************
423  *              BeginPaint (USER32.@)
424  */
425 HDC WINAPI BeginPaint( HWND hwnd, PAINTSTRUCT *lps )
426 {
427     HWND full_handle;
428     HRGN hrgn;
429     UINT flags = UPDATE_NONCLIENT | UPDATE_ERASE | UPDATE_PAINT | UPDATE_INTERNALPAINT | UPDATE_NOCHILDREN;
430
431     if (!lps) return 0;
432
433     if (!(full_handle = WIN_IsCurrentThread( hwnd )))
434     {
435         if (IsWindow(hwnd))
436             FIXME( "window %p belongs to other thread\n", hwnd );
437         return 0;
438     }
439     hwnd = full_handle;
440
441     HideCaret( hwnd );
442
443     if (!(hrgn = send_ncpaint( hwnd, NULL, &flags ))) return 0;
444
445     lps->fErase = send_erase( hwnd, flags, hrgn, &lps->rcPaint, &lps->hdc );
446
447     TRACE("hdc = %p box = (%d,%d - %d,%d), fErase = %d\n",
448           lps->hdc, lps->rcPaint.left, lps->rcPaint.top, lps->rcPaint.right, lps->rcPaint.bottom,
449           lps->fErase);
450
451     return lps->hdc;
452 }
453
454
455 /***********************************************************************
456  *              EndPaint (USER32.@)
457  */
458 BOOL WINAPI EndPaint( HWND hwnd, const PAINTSTRUCT *lps )
459 {
460     if (!lps) return FALSE;
461     USER_Driver->pReleaseDC( hwnd, lps->hdc, TRUE );
462     ShowCaret( hwnd );
463     return TRUE;
464 }
465
466
467 /***********************************************************************
468  *              GetDCEx (USER32.@)
469  */
470 HDC WINAPI GetDCEx( HWND hwnd, HRGN hrgnClip, DWORD flags )
471 {
472     if (!hwnd) hwnd = GetDesktopWindow();
473     else hwnd = WIN_GetFullHandle( hwnd );
474
475     return USER_Driver->pGetDCEx( hwnd, hrgnClip, flags );
476 }
477
478
479 /***********************************************************************
480  *              GetDC (USER32.@)
481  *
482  * Get a device context.
483  *
484  * RETURNS
485  *      Success: Handle to the device context
486  *      Failure: NULL.
487  */
488 HDC WINAPI GetDC( HWND hwnd )
489 {
490     if (!hwnd) return GetDCEx( 0, 0, DCX_CACHE | DCX_WINDOW );
491     return GetDCEx( hwnd, 0, DCX_USESTYLE );
492 }
493
494
495 /***********************************************************************
496  *              GetWindowDC (USER32.@)
497  */
498 HDC WINAPI GetWindowDC( HWND hwnd )
499 {
500     return GetDCEx( hwnd, 0, DCX_USESTYLE | DCX_WINDOW );
501 }
502
503
504 /***********************************************************************
505  *              ReleaseDC (USER32.@)
506  *
507  * Release a device context.
508  *
509  * RETURNS
510  *      Success: Non-zero. Resources used by hdc are released.
511  *      Failure: 0.
512  */
513 INT WINAPI ReleaseDC( HWND hwnd, HDC hdc )
514 {
515     return USER_Driver->pReleaseDC( hwnd, hdc, FALSE );
516 }
517
518
519 /**********************************************************************
520  *              WindowFromDC (USER32.@)
521  */
522 HWND WINAPI WindowFromDC( HDC hDC )
523 {
524     return USER_Driver->pWindowFromDC( hDC );
525 }
526
527
528 /***********************************************************************
529  *              LockWindowUpdate (USER32.@)
530  */
531 BOOL WINAPI LockWindowUpdate( HWND hwnd )
532 {
533     static HWND lockedWnd;
534
535     /* This function is fully implemented by the following patch:
536      *
537      * http://www.winehq.org/hypermail/wine-patches/2004/01/0142.html
538      *
539      * but in order to work properly, it needs the ability to invalidate
540      * DCEs in other processes when the lock window is changed, which
541      * isn't possible yet.
542      * -mike
543      */
544
545     FIXME("(%p), partial stub!\n",hwnd);
546
547     USER_Lock();
548     if (lockedWnd)
549     {
550         if (!hwnd)
551         {
552             /* Unlock lockedWnd */
553             /* FIXME: Do something */
554         }
555         else
556         {
557             /* Attempted to lock a second window */
558             /* Return FALSE and do nothing */
559             USER_Unlock();
560             return FALSE;
561         }
562     }
563     lockedWnd = hwnd;
564     USER_Unlock();
565     return TRUE;
566 }
567
568
569 /***********************************************************************
570  *              RedrawWindow (USER32.@)
571  */
572 BOOL WINAPI RedrawWindow( HWND hwnd, const RECT *rect, HRGN hrgn, UINT flags )
573 {
574     static const RECT empty;
575     BOOL ret;
576
577     if (!hwnd) hwnd = GetDesktopWindow();
578
579     if (TRACE_ON(win))
580     {
581         if (hrgn)
582         {
583             RECT r;
584             GetRgnBox( hrgn, &r );
585             TRACE( "%p region %p box %s ", hwnd, hrgn, wine_dbgstr_rect(&r) );
586         }
587         else if (rect)
588             TRACE( "%p rect %s ", hwnd, wine_dbgstr_rect(rect) );
589         else
590             TRACE( "%p whole window ", hwnd );
591
592         dump_rdw_flags(flags);
593     }
594
595     /* process pending expose events before painting */
596     if (flags & RDW_UPDATENOW) USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_PAINT, 0 );
597
598     if (rect && !hrgn)
599     {
600         if (IsRectEmpty( rect )) rect = &empty;
601         ret = redraw_window_rects( hwnd, flags, rect, 1 );
602     }
603     else if (!hrgn)
604     {
605         ret = redraw_window_rects( hwnd, flags, NULL, 0 );
606     }
607     else  /* need to build a list of the region rectangles */
608     {
609         DWORD size;
610         RGNDATA *data = NULL;
611
612         if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
613         if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
614         GetRegionData( hrgn, size, data );
615         if (!data->rdh.nCount)  /* empty region -> use a single all-zero rectangle */
616             ret = redraw_window_rects( hwnd, flags, &empty, 1 );
617         else
618             ret = redraw_window_rects( hwnd, flags, (const RECT *)data->Buffer, data->rdh.nCount );
619         HeapFree( GetProcessHeap(), 0, data );
620     }
621
622     if (flags & RDW_UPDATENOW) update_now( hwnd, flags );
623     else if (flags & RDW_ERASENOW) erase_now( hwnd, flags );
624
625     return ret;
626 }
627
628
629 /***********************************************************************
630  *              UpdateWindow (USER32.@)
631  */
632 BOOL WINAPI UpdateWindow( HWND hwnd )
633 {
634     return RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
635 }
636
637
638 /***********************************************************************
639  *              InvalidateRgn (USER32.@)
640  */
641 BOOL WINAPI InvalidateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
642 {
643     if (!hwnd)
644     {
645         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
646         return FALSE;
647     }
648
649     return RedrawWindow(hwnd, NULL, hrgn, RDW_INVALIDATE | (erase ? RDW_ERASE : 0) );
650 }
651
652
653 /***********************************************************************
654  *              InvalidateRect (USER32.@)
655  *
656  * MSDN: if hwnd parameter is NULL, InvalidateRect invalidates and redraws
657  * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
658  */
659 BOOL WINAPI InvalidateRect( HWND hwnd, const RECT *rect, BOOL erase )
660 {
661     UINT flags = RDW_INVALIDATE | (erase ? RDW_ERASE : 0);
662
663     if (!hwnd)
664     {
665         flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
666         rect = NULL;
667     }
668
669     return RedrawWindow( hwnd, rect, 0, flags );
670 }
671
672
673 /***********************************************************************
674  *              ValidateRgn (USER32.@)
675  */
676 BOOL WINAPI ValidateRgn( HWND hwnd, HRGN hrgn )
677 {
678     if (!hwnd)
679     {
680         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
681         return FALSE;
682     }
683
684     return RedrawWindow( hwnd, NULL, hrgn, RDW_VALIDATE );
685 }
686
687
688 /***********************************************************************
689  *              ValidateRect (USER32.@)
690  *
691  * MSDN: if hwnd parameter is NULL, ValidateRect invalidates and redraws
692  * all windows and sends WM_ERASEBKGND and WM_NCPAINT.
693  */
694 BOOL WINAPI ValidateRect( HWND hwnd, const RECT *rect )
695 {
696     UINT flags = RDW_VALIDATE;
697
698     if (!hwnd)
699     {
700         flags = RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ERASENOW;
701         rect = NULL;
702     }
703
704     return RedrawWindow( hwnd, rect, 0, flags );
705 }
706
707
708 /***********************************************************************
709  *              GetUpdateRgn (USER32.@)
710  */
711 INT WINAPI GetUpdateRgn( HWND hwnd, HRGN hrgn, BOOL erase )
712 {
713     INT retval = ERROR;
714     UINT flags = UPDATE_NOCHILDREN;
715     HRGN update_rgn;
716
717     if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
718
719     if ((update_rgn = send_ncpaint( hwnd, NULL, &flags )))
720     {
721         POINT offset;
722
723         retval = CombineRgn( hrgn, update_rgn, 0, RGN_COPY );
724         send_erase( hwnd, flags, update_rgn, NULL, NULL );
725         /* map region to client coordinates */
726         offset.x = offset.y = 0;
727         ScreenToClient( hwnd, &offset );
728         OffsetRgn( hrgn, offset.x, offset.y );
729     }
730     return retval;
731 }
732
733
734 /***********************************************************************
735  *              GetUpdateRect (USER32.@)
736  */
737 BOOL WINAPI GetUpdateRect( HWND hwnd, LPRECT rect, BOOL erase )
738 {
739     UINT flags = UPDATE_NOCHILDREN;
740     HRGN update_rgn;
741
742     if (erase) flags |= UPDATE_NONCLIENT | UPDATE_ERASE;
743
744     if (!(update_rgn = send_ncpaint( hwnd, NULL, &flags ))) return FALSE;
745
746     if (rect)
747     {
748         if (GetRgnBox( update_rgn, rect ) != NULLREGION)
749         {
750             HDC hdc = GetDCEx( hwnd, 0, DCX_USESTYLE );
751             MapWindowPoints( 0, hwnd, (LPPOINT)rect, 2 );
752             DPtoLP( hdc, (LPPOINT)rect, 2 );
753             ReleaseDC( hwnd, hdc );
754         }
755     }
756     send_erase( hwnd, flags, update_rgn, NULL, NULL );
757
758     /* check if we still have an update region */
759     flags = UPDATE_PAINT | UPDATE_NOCHILDREN;
760     return (get_update_flags( hwnd, NULL, &flags ) && (flags & UPDATE_PAINT));
761 }
762
763
764 /***********************************************************************
765  *              ExcludeUpdateRgn (USER32.@)
766  */
767 INT WINAPI ExcludeUpdateRgn( HDC hdc, HWND hwnd )
768 {
769     HRGN update_rgn = CreateRectRgn( 0, 0, 0, 0 );
770     INT ret = GetUpdateRgn( hwnd, update_rgn, FALSE );
771
772     if (ret != ERROR)
773     {
774         POINT pt;
775
776         GetDCOrgEx( hdc, &pt );
777         MapWindowPoints( 0, hwnd, &pt, 1 );
778         OffsetRgn( update_rgn, -pt.x, -pt.y );
779         ret = ExtSelectClipRgn( hdc, update_rgn, RGN_DIFF );
780         DeleteObject( update_rgn );
781     }
782     return ret;
783 }
784
785
786 /*************************************************************************
787  *              ScrollWindowEx (USER32.@)
788  *
789  * Note: contrary to what the doc says, pixels that are scrolled from the
790  *      outside of clipRect to the inside are NOT painted.
791  *
792  */
793 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
794                            const RECT *rect, const RECT *clipRect,
795                            HRGN hrgnUpdate, LPRECT rcUpdate,
796                            UINT flags )
797 {
798     INT   retVal = NULLREGION;
799     BOOL  bOwnRgn = TRUE;
800     BOOL  bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
801     int rdw_flags;
802     HRGN  hrgnTemp;
803     HRGN  hrgnWinupd = 0;
804     HDC   hDC;
805     RECT  rc, cliprc;
806     HWND hwndCaret = NULL;
807     BOOL moveCaret = FALSE;
808     POINT newCaretPos;
809
810     TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
811            hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
812     TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
813     if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
814         FIXME("some flags (%04x) are unhandled\n", flags);
815
816     rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
817                                 RDW_INVALIDATE | RDW_ERASE  : RDW_INVALIDATE ;
818
819     if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
820     hwnd = WIN_GetFullHandle( hwnd );
821
822     GetClientRect(hwnd, &rc);
823     if (rect) IntersectRect(&rc, &rc, rect);
824
825     if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
826     else cliprc = rc;
827
828     if( hrgnUpdate ) bOwnRgn = FALSE;
829     else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
830
831     newCaretPos.x = newCaretPos.y = 0;
832
833     if( !IsRectEmpty(&cliprc) && (dx || dy)) {
834         DWORD dcxflags = DCX_CACHE;
835         DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
836
837         hwndCaret = fix_caret(hwnd, &rc, dx, dy, flags, &moveCaret, &newCaretPos);
838
839         if( style & WS_CLIPSIBLINGS) dcxflags |= DCX_CLIPSIBLINGS;
840         if( GetClassLongW( hwnd, GCL_STYLE ) & CS_PARENTDC)
841             dcxflags |= DCX_PARENTCLIP;
842         if( !(flags & SW_SCROLLCHILDREN) && (style & WS_CLIPCHILDREN))
843             dcxflags |= DCX_CLIPCHILDREN;
844         hDC = GetDCEx( hwnd, 0, dcxflags);
845         if (hDC)
846         {
847             ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
848
849             ReleaseDC( hwnd, hDC );
850
851             if (!bUpdate)
852                 RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
853         }
854
855         /* If the windows has an update region, this must be
856          * scrolled as well. Keep a copy in hrgnWinupd
857          * to be added to hrngUpdate at the end. */
858         hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
859         retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
860         if (retVal != NULLREGION)
861         {
862             HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
863             if( !bOwnRgn) {
864                 hrgnWinupd = CreateRectRgn( 0, 0, 0, 0);
865                 CombineRgn( hrgnWinupd, hrgnTemp, 0, RGN_COPY);
866             }
867             OffsetRgn( hrgnTemp, dx, dy );
868             CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
869             if( !bOwnRgn)
870                 CombineRgn( hrgnWinupd, hrgnWinupd, hrgnTemp, RGN_OR );
871             RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
872             DeleteObject( hrgnClip );
873         }
874         DeleteObject( hrgnTemp );
875     } else {
876         /* nothing was scrolled */
877         if( !bOwnRgn)
878             SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
879         SetRectEmpty( rcUpdate);
880     }
881
882     if( flags & SW_SCROLLCHILDREN )
883     {
884         HWND *list = WIN_ListChildren( hwnd );
885         if (list)
886         {
887             int i;
888             RECT r, dummy;
889             for (i = 0; list[i]; i++)
890             {
891                 GetWindowRect( list[i], &r );
892                 MapWindowPoints( 0, hwnd, (POINT *)&r, 2 );
893                 if (!rect || IntersectRect(&dummy, &r, rect))
894                     SetWindowPos( list[i], 0, r.left + dx, r.top  + dy, 0, 0,
895                                   SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
896                                   SWP_NOREDRAW | SWP_DEFERERASE );
897             }
898             HeapFree( GetProcessHeap(), 0, list );
899         }
900     }
901
902     if( flags & (SW_INVALIDATE | SW_ERASE) )
903         RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
904                       ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
905
906     if( hrgnWinupd) {
907         CombineRgn( hrgnUpdate, hrgnUpdate, hrgnWinupd, RGN_OR);
908         DeleteObject( hrgnWinupd);
909     }
910
911     if( hwndCaret ) {
912         if ( moveCaret ) SetCaretPos( newCaretPos.x, newCaretPos.y );
913         ShowCaret(hwndCaret);
914     }
915
916     if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
917
918     return retVal;
919 }
920
921
922 /*************************************************************************
923  *              ScrollWindow (USER32.@)
924  *
925  */
926 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
927                           const RECT *rect, const RECT *clipRect )
928 {
929     return (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
930                                      (rect ? 0 : SW_SCROLLCHILDREN) |
931                                      SW_INVALIDATE | SW_ERASE ));
932 }
933
934
935 /*************************************************************************
936  *              ScrollDC (USER32.@)
937  *
938  * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is
939  * wrong) hrgnUpdate is returned in device coordinates with rcUpdate in
940  * logical coordinates.
941  */
942 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
943                       const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
944
945 {
946     return USER_Driver->pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
947 }