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