New debug scheme with explicit debug channels declaration.
[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 "winuser.h"
13 #include "class.h"
14 #include "dc.h"
15 #include "win.h"
16 #include "gdi.h"
17 #include "dce.h"
18 #include "region.h"
19 #include "sysmetrics.h"
20 #include "debug.h"
21
22 DEFAULT_DEBUG_CHANNEL(scroll)
23
24 /*************************************************************************
25  *             ScrollWindow16   (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  *             ScrollWindow32   (USER32.450)
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  *             ScrollDC16   (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  *             ScrollDC32   (USER32.449)
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 *)GDI_GetObjPtr(hdc, DC_MAGIC);
84
85     TRACE(scroll,"%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 */
100
101     if ( rc )
102         rect = *rc;
103     else /* maybe we should just return FALSE? */
104         GetClipBox( hdc, &rect );
105
106     if (prLClip)
107         IntersectRect( &rClip,&rect,prLClip );
108     else
109         rClip = rect;
110
111     rSrc = rClip;
112     OffsetRect( &rSrc, -dx, -dy );
113     IntersectRect( &rSrc, &rSrc, &rect );
114
115     if(dc->w.hVisRgn)
116     {
117         if (!IsRectEmpty(&rSrc))
118         {
119             dest.x = (src.x = rSrc.left) + dx;
120             dest.y = (src.y = rSrc.top) + dy;
121
122             /* copy bits */
123
124             if (!BitBlt( hdc, dest.x, dest.y,
125                            rSrc.right - rSrc.left, rSrc.bottom - rSrc.top,
126                            hdc, src.x, src.y, SRCCOPY))
127             {
128                 GDI_HEAP_UNLOCK( hdc );
129                 return FALSE;
130             }
131         }
132
133         /* compute update areas */
134
135         if (hrgnUpdate || rcUpdate)
136         {
137             HRGN hrgn =
138               (hrgnUpdate) ? hrgnUpdate : CreateRectRgn( 0,0,0,0 );
139             HRGN hrgn2;
140
141             dx = XLPTODP ( dc, rect.left + dx) - XLPTODP ( dc, rect.left);
142             dy = YLPTODP ( dc, rect.top + dy) - YLPTODP ( dc, rect.top);
143             LPtoDP( hdc, (LPPOINT)&rect, 2 );
144             LPtoDP( hdc, (LPPOINT)&rClip, 2 );
145             hrgn2 = CreateRectRgnIndirect( &rect );
146             OffsetRgn( hrgn2, dc->w.DCOrgX, dc->w.DCOrgY );
147             CombineRgn( hrgn2, hrgn2, dc->w.hVisRgn, RGN_AND );
148             OffsetRgn( hrgn2, -dc->w.DCOrgX, -dc->w.DCOrgY );
149             SetRectRgn( hrgn, rClip.left, rClip.top,
150                           rClip.right, rClip.bottom );
151             CombineRgn( hrgn, hrgn, hrgn2, RGN_AND );
152             OffsetRgn( hrgn2, dx, dy );
153             CombineRgn( hrgn, hrgn, hrgn2, RGN_DIFF );
154
155             if( rcUpdate )
156             {
157                 GetRgnBox( hrgn, rcUpdate );
158
159                 //Put the rcUpdate in logical coordinate
160                 DPtoLP( hdc, (LPPOINT)rcUpdate, 2 );
161             }
162             if (!hrgnUpdate) DeleteObject( hrgn );
163             DeleteObject( hrgn2 );
164
165         }
166     }
167     else
168     {
169        if (hrgnUpdate) SetRectRgn(hrgnUpdate, 0, 0, 0, 0);
170        if (rcUpdate) SetRectEmpty(rcUpdate);
171     }
172
173     GDI_HEAP_UNLOCK( hdc );
174     return TRUE;
175 }
176
177
178 /*************************************************************************
179  *             ScrollWindowEx16   (USER.319)
180  */
181 INT16 WINAPI ScrollWindowEx16( HWND16 hwnd, INT16 dx, INT16 dy,
182                                const RECT16 *rect, const RECT16 *clipRect,
183                                HRGN16 hrgnUpdate, LPRECT16 rcUpdate,
184                                UINT16 flags )
185 {
186     RECT rect32, clipRect32, rcUpdate32;
187     BOOL16 ret;
188
189     if (rect) CONV_RECT16TO32( rect, &rect32 );
190     if (clipRect) CONV_RECT16TO32( clipRect, &clipRect32 );
191     ret = ScrollWindowEx( hwnd, dx, dy, rect ? &rect32 : NULL,
192                             clipRect ? &clipRect32 : NULL, hrgnUpdate,
193                             (rcUpdate) ? &rcUpdate32 : NULL, flags );
194     if (rcUpdate) CONV_RECT32TO16( &rcUpdate32, rcUpdate );
195     return ret;
196 }
197
198 /*************************************************************************
199  *             SCROLL_FixCaret
200  */
201 static BOOL SCROLL_FixCaret(HWND hWnd, LPRECT lprc, UINT flags)
202 {
203    HWND hCaret = CARET_GetHwnd();
204
205    if( hCaret )
206    {
207        RECT     rc;
208        CARET_GetRect( &rc );
209        if( hCaret == hWnd ||
210           (flags & SW_SCROLLCHILDREN && IsChild(hWnd, hCaret)) )
211        {
212            POINT     pt;
213
214            pt.x = rc.left; pt.y = rc.top;
215            MapWindowPoints( hCaret, hWnd, (LPPOINT)&rc, 2 );
216            if( IntersectRect(lprc, lprc, &rc) )
217            {
218                HideCaret(0);
219                lprc->left = pt.x; lprc->top = pt.y;
220                return TRUE;
221            }
222        }
223    }
224    return FALSE;
225 }
226
227 /*************************************************************************
228  *             ScrollWindowEx32   (USER32.451)
229  *
230  * NOTE: Use this function instead of ScrollWindow32
231  */
232 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
233                                const RECT *rect, const RECT *clipRect,
234                                HRGN hrgnUpdate, LPRECT rcUpdate,
235                                UINT flags )
236 {
237     INT  retVal = NULLREGION;
238     BOOL bCaret = FALSE, bOwnRgn = TRUE;
239     RECT rc, cliprc;
240     WND*   wnd = WIN_FindWndPtr( hwnd );
241
242     if( !wnd || !WIN_IsWindowDrawable( wnd, TRUE ))
243     {
244         retVal = ERROR;
245         goto END;
246     }
247
248     GetClientRect(hwnd, &rc);
249     if (rect) IntersectRect(&rc, &rc, rect);
250
251     if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
252     else cliprc = rc;
253
254     if (!IsRectEmpty(&cliprc) && (dx || dy))
255     {
256         DC*     dc;
257         HDC     hDC;
258         BOOL  bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
259         HRGN  hrgnClip = CreateRectRgnIndirect(&cliprc);
260         HRGN  hrgnTemp = CreateRectRgnIndirect(&rc);
261         RECT  caretrc;
262
263 TRACE(scroll,"%04x, %d,%d hrgnUpdate=%04x rcUpdate = %p \
264 cliprc = (%d,%d-%d,%d), rc=(%d,%d-%d,%d) %04x\n",             
265 (HWND16)hwnd, dx, dy, hrgnUpdate, rcUpdate,
266 clipRect?clipRect->left:0, clipRect?clipRect->top:0, clipRect?clipRect->right:0, clipRect?clipRect->bottom:0,
267 rc.left, rc.top, rc.right, rc.bottom, (UINT16)flags );
268
269         caretrc = rc; 
270         bCaret = SCROLL_FixCaret(hwnd, &caretrc, flags);
271
272         if( hrgnUpdate ) bOwnRgn = FALSE;
273         else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
274
275         hDC = GetDCEx( hwnd, hrgnClip, DCX_CACHE | DCX_USESTYLE |
276                          DCX_KEEPCLIPRGN | DCX_INTERSECTRGN |
277                        ((flags & SW_SCROLLCHILDREN) ? DCX_NOCLIPCHILDREN : 0) );
278         if( (dc = (DC *)GDI_GetObjPtr(hDC, DC_MAGIC)) )
279         {
280             if (dc->w.hVisRgn) {
281                 wnd->pDriver->pSurfaceCopy(wnd,dc,dx,dy,&rc,bUpdate);
282
283                 if( bUpdate )
284                     {
285                         OffsetRgn( hrgnTemp, dc->w.DCOrgX, dc->w.DCOrgY );
286                         CombineRgn( hrgnTemp, hrgnTemp, dc->w.hVisRgn,
287                                       RGN_AND );
288                         OffsetRgn( hrgnTemp, -dc->w.DCOrgX, -dc->w.DCOrgY );
289                         CombineRgn( hrgnUpdate, hrgnTemp, hrgnClip,
290                                       RGN_AND );
291                         OffsetRgn( hrgnTemp, dx, dy );
292                         retVal =
293                             CombineRgn( hrgnUpdate, hrgnUpdate, hrgnTemp,
294                                           RGN_DIFF );
295
296                         if( rcUpdate ) GetRgnBox( hrgnUpdate, rcUpdate );
297                     }
298             }
299             ReleaseDC(hwnd, hDC);
300             GDI_HEAP_UNLOCK( hDC );
301         }
302
303         if( wnd->hrgnUpdate > 1 )
304         {
305             /* Takes into account the fact that some damages may have
306                occured during the scroll. */
307             CombineRgn( hrgnTemp, wnd->hrgnUpdate, 0, RGN_COPY );
308             OffsetRgn( hrgnTemp, dx, dy );
309             CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
310             CombineRgn( wnd->hrgnUpdate, wnd->hrgnUpdate, hrgnTemp, RGN_OR );
311         }
312
313         if( flags & SW_SCROLLCHILDREN )
314         {
315             RECT        r;
316             WND*        w;
317             for( w =WIN_LockWndPtr(wnd->child); w; WIN_UpdateWndPtr(&w, w->next))
318             {
319                  CONV_RECT16TO32( &w->rectWindow, &r );
320                  if( IntersectRect(&r, &r, &rc) )
321                      SetWindowPos(w->hwndSelf, 0, w->rectWindow.left + dx,
322                                     w->rectWindow.top  + dy, 0,0, SWP_NOZORDER |
323                                     SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW |
324                                     SWP_DEFERERASE );
325             }
326         }
327
328         if( flags & (SW_INVALIDATE | SW_ERASE) )
329             PAINT_RedrawWindow( hwnd, NULL, hrgnUpdate, RDW_INVALIDATE | RDW_ERASE |
330                 ((flags & SW_ERASE) ? RDW_ERASENOW : 0) | ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ), 0 );
331
332         if( bCaret )
333         {
334             SetCaretPos( caretrc.left + dx, caretrc.top + dy );
335             ShowCaret(0);
336         }
337
338         if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
339         DeleteObject( hrgnClip );
340         DeleteObject( hrgnTemp );
341     }
342 END:
343     WIN_ReleaseWndPtr(wnd);
344     return retVal;
345 }