Validate the parent with the child window client rectangle when the
[wine] / windows / scroll.c
1 /*
2  * Scroll windows and DCs
3  *
4  * Copyright  David W. Metcalfe, 1993
5  *            Alex Korobka       1995,1996
6  *
7  *
8  */
9
10 #include <stdlib.h>
11
12 #include "wine/winuser16.h"
13 #include "winuser.h"
14 #include "dc.h"
15 #include "win.h"
16 #include "gdi.h"
17 #include "dce.h"
18 #include "region.h"
19 #include "debugtools.h"
20
21 DEFAULT_DEBUG_CHANNEL(scroll)
22
23 /*************************************************************************
24  *             ScrollWindow16   (USER.61)
25  */
26 void WINAPI ScrollWindow16(HWND16 hwnd, INT16 dx, INT16 dy, const RECT16 *rect,
27                            const RECT16 *clipRect )
28 {
29     RECT rect32, clipRect32;
30
31     if (rect) CONV_RECT16TO32( rect, &rect32 );
32     if (clipRect) CONV_RECT16TO32( clipRect, &clipRect32 );
33     ScrollWindow( hwnd, dx, dy, rect ? &rect32 : NULL,
34                     clipRect ? &clipRect32 : NULL );
35 }
36
37 /*************************************************************************
38  *             ScrollWindow32   (USER32.450)
39  *
40  */
41 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
42                               const RECT *rect, const RECT *clipRect )
43 {
44     return
45         (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
46                                     (rect ? 0 : SW_SCROLLCHILDREN) |
47                                     SW_INVALIDATE ));
48 }
49
50 /*************************************************************************
51  *             ScrollDC16   (USER.221)
52  */
53 BOOL16 WINAPI ScrollDC16( HDC16 hdc, INT16 dx, INT16 dy, const RECT16 *rect,
54                           const RECT16 *cliprc, HRGN16 hrgnUpdate,
55                           LPRECT16 rcUpdate )
56 {
57     RECT rect32, clipRect32, rcUpdate32;
58     BOOL16 ret;
59
60     if (rect) CONV_RECT16TO32( rect, &rect32 );
61     if (cliprc) CONV_RECT16TO32( cliprc, &clipRect32 );
62     ret = ScrollDC( hdc, dx, dy, rect ? &rect32 : NULL,
63                       cliprc ? &clipRect32 : NULL, hrgnUpdate, &rcUpdate32 );
64     if (rcUpdate) CONV_RECT32TO16( &rcUpdate32, rcUpdate );
65     return ret;
66 }
67
68
69 /*************************************************************************
70  *             ScrollDC32   (USER32.449)
71  * 
72  *   Only the hrgnUpdate is return in device coordinate.
73  *   rcUpdate must be returned in logical coordinate to comply with win API.
74  *
75  */
76 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *rc,
77                           const RECT *prLClip, HRGN hrgnUpdate,
78                           LPRECT rcUpdate )
79 {
80     RECT rect, rClip, rSrc;
81     POINT src, dest;
82     DC *dc = (DC *)GDI_GetObjPtr(hdc, DC_MAGIC);
83
84     TRACE("%04x %d,%d hrgnUpdate=%04x rcUpdate = %p cliprc = (%d,%d-%d,%d), rc=(%d,%d-%d,%d)\n",
85                    (HDC16)hdc, dx, dy, hrgnUpdate, rcUpdate, 
86                    prLClip ? prLClip->left : 0, prLClip ? prLClip->top : 0, prLClip ? prLClip->right : 0, prLClip ? prLClip->bottom : 0,
87                    rc ? rc->left : 0, rc ? rc->top : 0, rc ? rc->right : 0, rc ? rc->bottom : 0 );
88
89     if ( !dc || !hdc ) return FALSE;
90
91 /*
92     TRACE(scroll,"\t[wndOrgX=%i, wndExtX=%i, vportOrgX=%i, vportExtX=%i]\n",
93           dc->wndOrgX, dc->wndExtX, dc->vportOrgX, dc->vportExtX );
94     TRACE(scroll,"\t[wndOrgY=%i, wndExtY=%i, vportOrgY=%i, vportExtY=%i]\n",
95           dc->wndOrgY, dc->wndExtY, dc->vportOrgY, dc->vportExtY );
96 */
97
98     /* compute device clipping region */
99
100     if ( rc )
101         rect = *rc;
102     else /* maybe we should just return FALSE? */
103         GetClipBox( hdc, &rect );
104
105     if (prLClip)
106         IntersectRect( &rClip,&rect,prLClip );
107     else
108         rClip = rect;
109
110     rSrc = rClip;
111     OffsetRect( &rSrc, -dx, -dy );
112     IntersectRect( &rSrc, &rSrc, &rect );
113
114     if(dc->w.hVisRgn)
115     {
116         if (!IsRectEmpty(&rSrc))
117         {
118             dest.x = (src.x = rSrc.left) + dx;
119             dest.y = (src.y = rSrc.top) + dy;
120
121             /* copy bits */
122
123             if (!BitBlt( hdc, dest.x, dest.y,
124                            rSrc.right - rSrc.left, rSrc.bottom - rSrc.top,
125                            hdc, src.x, src.y, SRCCOPY))
126             {
127                 GDI_HEAP_UNLOCK( hdc );
128                 return FALSE;
129             }
130         }
131
132         /* compute update areas */
133
134         if (hrgnUpdate || rcUpdate)
135         {
136             HRGN hrgn =
137               (hrgnUpdate) ? hrgnUpdate : CreateRectRgn( 0,0,0,0 );
138             HRGN hrgn2;
139
140             dx = XLPTODP ( dc, rect.left + dx) - XLPTODP ( dc, rect.left);
141             dy = YLPTODP ( dc, rect.top + dy) - YLPTODP ( dc, rect.top);
142             LPtoDP( hdc, (LPPOINT)&rect, 2 );
143             LPtoDP( hdc, (LPPOINT)&rClip, 2 );
144             hrgn2 = CreateRectRgnIndirect( &rect );
145             OffsetRgn( hrgn2, dc->w.DCOrgX, dc->w.DCOrgY );
146             CombineRgn( hrgn2, hrgn2, dc->w.hVisRgn, RGN_AND );
147             OffsetRgn( hrgn2, -dc->w.DCOrgX, -dc->w.DCOrgY );
148             SetRectRgn( hrgn, rClip.left, rClip.top,
149                           rClip.right, rClip.bottom );
150             CombineRgn( hrgn, hrgn, hrgn2, RGN_AND );
151             OffsetRgn( hrgn2, dx, dy );
152             CombineRgn( hrgn, hrgn, hrgn2, RGN_DIFF );
153
154             if( rcUpdate )
155             {
156                 GetRgnBox( hrgn, rcUpdate );
157
158                 /* Put the rcUpdate in logical coordinate */
159                 DPtoLP( hdc, (LPPOINT)rcUpdate, 2 );
160             }
161             if (!hrgnUpdate) DeleteObject( hrgn );
162             DeleteObject( hrgn2 );
163
164         }
165     }
166     else
167     {
168        if (hrgnUpdate) SetRectRgn(hrgnUpdate, 0, 0, 0, 0);
169        if (rcUpdate) SetRectEmpty(rcUpdate);
170     }
171
172     GDI_HEAP_UNLOCK( hdc );
173     return TRUE;
174 }
175
176
177 /*************************************************************************
178  *             ScrollWindowEx16   (USER.319)
179  */
180 INT16 WINAPI ScrollWindowEx16( HWND16 hwnd, INT16 dx, INT16 dy,
181                                const RECT16 *rect, const RECT16 *clipRect,
182                                HRGN16 hrgnUpdate, LPRECT16 rcUpdate,
183                                UINT16 flags )
184 {
185     RECT rect32, clipRect32, rcUpdate32;
186     BOOL16 ret;
187
188     if (rect) CONV_RECT16TO32( rect, &rect32 );
189     if (clipRect) CONV_RECT16TO32( clipRect, &clipRect32 );
190     ret = ScrollWindowEx( hwnd, dx, dy, rect ? &rect32 : NULL,
191                             clipRect ? &clipRect32 : NULL, hrgnUpdate,
192                             (rcUpdate) ? &rcUpdate32 : NULL, flags );
193     if (rcUpdate) CONV_RECT32TO16( &rcUpdate32, rcUpdate );
194     return ret;
195 }
196
197 /*************************************************************************
198  *             SCROLL_FixCaret
199  */
200 static BOOL SCROLL_FixCaret(HWND hWnd, LPRECT lprc, UINT flags)
201 {
202    HWND hCaret = CARET_GetHwnd();
203
204    if( hCaret )
205    {
206        RECT     rc;
207        CARET_GetRect( &rc );
208        if( hCaret == hWnd ||
209           (flags & SW_SCROLLCHILDREN && IsChild(hWnd, hCaret)) )
210        {
211            POINT     pt;
212
213            pt.x = rc.left; pt.y = rc.top;
214            MapWindowPoints( hCaret, hWnd, (LPPOINT)&rc, 2 );
215            if( IntersectRect(lprc, lprc, &rc) )
216            {
217                HideCaret(0);
218                lprc->left = pt.x; lprc->top = pt.y;
219                return TRUE;
220            }
221        }
222    }
223    return FALSE;
224 }
225
226 /*************************************************************************
227  *             ScrollWindowEx32   (USER32.451)
228  *
229  * NOTE: Use this function instead of ScrollWindow32
230  */
231 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
232                                const RECT *rect, const RECT *clipRect,
233                                HRGN hrgnUpdate, LPRECT rcUpdate,
234                                UINT flags )
235 {
236     INT  retVal = NULLREGION;
237     BOOL bCaret = FALSE, bOwnRgn = TRUE;
238     RECT rc, cliprc;
239     WND*   wnd = WIN_FindWndPtr( hwnd );
240
241     if( !wnd || !WIN_IsWindowDrawable( wnd, TRUE ))
242     {
243         retVal = ERROR;
244         goto END;
245     }
246
247     GetClientRect(hwnd, &rc);
248     if (rect) IntersectRect(&rc, &rc, rect);
249
250     if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
251     else cliprc = rc;
252
253     if (!IsRectEmpty(&cliprc) && (dx || dy))
254     {
255         DC*     dc;
256         HDC     hDC;
257         BOOL  bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
258         HRGN  hrgnClip = CreateRectRgnIndirect(&cliprc);
259         HRGN  hrgnTemp = CreateRectRgnIndirect(&rc);
260         RECT  caretrc;
261
262 TRACE("%04x, %d,%d hrgnUpdate=%04x rcUpdate = %p \
263 cliprc = (%d,%d-%d,%d), rc=(%d,%d-%d,%d) %04x\n",             
264 (HWND16)hwnd, dx, dy, hrgnUpdate, rcUpdate,
265 clipRect?clipRect->left:0, clipRect?clipRect->top:0, clipRect?clipRect->right:0, clipRect?clipRect->bottom:0,
266 rc.left, rc.top, rc.right, rc.bottom, (UINT16)flags );
267
268         caretrc = rc; 
269         bCaret = SCROLL_FixCaret(hwnd, &caretrc, flags);
270
271         if( hrgnUpdate ) bOwnRgn = FALSE;
272         else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
273
274         hDC = GetDCEx( hwnd, hrgnClip, DCX_CACHE | DCX_USESTYLE |
275                          DCX_KEEPCLIPRGN | DCX_INTERSECTRGN |
276                        ((flags & SW_SCROLLCHILDREN) ? DCX_NOCLIPCHILDREN : 0) );
277         if( (dc = (DC *)GDI_GetObjPtr(hDC, DC_MAGIC)) )
278         {
279             if (dc->w.hVisRgn) {
280                 wnd->pDriver->pSurfaceCopy(wnd,dc,dx,dy,&rc,bUpdate);
281
282                 if( bUpdate )
283                     {
284                         OffsetRgn( hrgnTemp, dc->w.DCOrgX, dc->w.DCOrgY );
285                         CombineRgn( hrgnTemp, hrgnTemp, dc->w.hVisRgn,
286                                       RGN_AND );
287                         OffsetRgn( hrgnTemp, -dc->w.DCOrgX, -dc->w.DCOrgY );
288                         CombineRgn( hrgnUpdate, hrgnTemp, hrgnClip,
289                                       RGN_AND );
290                         OffsetRgn( hrgnTemp, dx, dy );
291                         retVal =
292                             CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp,
293                                           RGN_DIFF );
294
295                         if( rcUpdate ) GetRgnBox( hrgnUpdate, rcUpdate );
296                     }
297             }
298             ReleaseDC(hwnd, hDC);
299             GDI_HEAP_UNLOCK( hDC );
300         }
301
302         if( wnd->hrgnUpdate > 1 )
303         {
304             /* Takes into account the fact that some damages may have
305                occured during the scroll. */
306             CombineRgn( hrgnTemp, wnd->hrgnUpdate, 0, RGN_COPY );
307             OffsetRgn( hrgnTemp, dx, dy );
308             CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
309             CombineRgn( wnd->hrgnUpdate, wnd->hrgnUpdate, hrgnTemp, RGN_OR );
310         }
311
312         if( flags & SW_SCROLLCHILDREN )
313         {
314             RECT        r;
315             WND*        w;
316             for( w =WIN_LockWndPtr(wnd->child); w; WIN_UpdateWndPtr(&w, w->next))
317             {
318                  CONV_RECT16TO32( &w->rectWindow, &r );
319                  if( IntersectRect(&r, &r, &rc) )
320                      SetWindowPos(w->hwndSelf, 0, w->rectWindow.left + dx,
321                                     w->rectWindow.top  + dy, 0,0, SWP_NOZORDER |
322                                     SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW |
323                                     SWP_DEFERERASE );
324             }
325         }
326
327         if( flags & (SW_INVALIDATE | SW_ERASE) )
328             PAINT_RedrawWindow( hwnd, NULL, hrgnUpdate, RDW_INVALIDATE | RDW_ERASE |
329                 ((flags & SW_ERASE) ? RDW_ERASENOW : 0) | ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ), 0 );
330
331         if( bCaret )
332         {
333             SetCaretPos( caretrc.left + dx, caretrc.top + dy );
334             ShowCaret(0);
335         }
336
337         if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
338         DeleteObject( hrgnClip );
339         DeleteObject( hrgnTemp );
340     }
341 END:
342     WIN_ReleaseWndPtr(wnd);
343     return retVal;
344 }