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