Release 970629
[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 "windows.h"
12 #include "class.h"
13 #include "win.h"
14 #include "gdi.h"
15 #include "dce.h"
16 #include "region.h"
17 #include "graphics.h"
18 #include "sysmetrics.h"
19 #include "stddebug.h"
20 #include "debug.h"
21
22 extern HWND32 CARET_GetHwnd();                  /* windows/caret.c */
23 extern void   CARET_GetRect(LPRECT32);
24 extern void CLIPPING_UpdateGCRegion(DC* );      /* objects/clipping.c */
25
26 /*************************************************************************
27  *             ScrollWindow16   (USER.61)
28  */
29 void ScrollWindow16( HWND16 hwnd, INT16 dx, INT16 dy, const RECT16 *rect,
30                      const RECT16 *clipRect )
31 {
32     RECT32 rect32, clipRect32;
33
34     if (rect) CONV_RECT16TO32( rect, &rect32 );
35     if (clipRect) CONV_RECT16TO32( clipRect, &clipRect32 );
36     ScrollWindow32( hwnd, dx, dy, rect ? &rect32 : NULL,
37                     clipRect ? &clipRect32 : NULL );
38 }
39
40 /*************************************************************************
41  *             ScrollWindow32   (USER32.449)
42  */
43 BOOL32 ScrollWindow32( HWND32 hwnd, INT32 dx, INT32 dy, const RECT32 *rect,
44                        const RECT32 *clipRect )
45 {
46     HDC32       hdc;
47     HRGN32      hrgnUpdate,hrgnClip;
48     RECT32      rc, cliprc;
49     HWND32      hCaretWnd = CARET_GetHwnd();
50     WND*        wndScroll = WIN_FindWndPtr( hwnd );
51
52     dprintf_scroll(stddeb,"ScrollWindow: hwnd=%04x, dx=%d, dy=%d, lpRect =%p clipRect=%i,%i,%i,%i\n", 
53                    hwnd, dx, dy, rect,
54                    clipRect ? clipRect->left : 0,
55                    clipRect ? clipRect->top : 0,
56                    clipRect ? clipRect->right : 0, 
57                    clipRect ? clipRect->bottom : 0 );
58
59     if ( !wndScroll || !WIN_IsWindowDrawable( wndScroll, TRUE ) ) return TRUE;
60
61     if ( !rect ) /* do not clip children */
62        {
63           GetClientRect32(hwnd, &rc);
64           hrgnClip = CreateRectRgnIndirect32( &rc );
65
66           if ((hCaretWnd == hwnd) || IsChild32(hwnd,hCaretWnd))
67               HideCaret32(hCaretWnd);
68           else hCaretWnd = 0;
69  
70           hdc = GetDCEx32(hwnd, hrgnClip, DCX_CACHE | DCX_CLIPSIBLINGS);
71           DeleteObject32( hrgnClip );
72        }
73     else        /* clip children */
74        {
75           CopyRect32(&rc, rect);
76
77           if (hCaretWnd == hwnd) HideCaret32(hCaretWnd);
78           else hCaretWnd = 0;
79
80           hdc = GetDCEx32( hwnd, 0, DCX_CACHE | DCX_USESTYLE );
81        }
82
83     if (clipRect == NULL)
84         GetClientRect32(hwnd, &cliprc);
85     else
86         CopyRect32(&cliprc, clipRect);
87
88     hrgnUpdate = CreateRectRgn32( 0, 0, 0, 0 );
89     ScrollDC32( hdc, dx, dy, &rc, &cliprc, hrgnUpdate, NULL );
90     ReleaseDC32(hwnd, hdc);
91
92     if( !rect )         /* move child windows and update region */
93     { 
94       WND*      wndPtr;
95
96       if( wndScroll->hrgnUpdate > 1 )
97         OffsetRgn32( wndScroll->hrgnUpdate, dx, dy );
98
99       for (wndPtr = wndScroll->child; wndPtr; wndPtr = wndPtr->next)
100         SetWindowPos32(wndPtr->hwndSelf, 0, wndPtr->rectWindow.left + dx,
101                        wndPtr->rectWindow.top  + dy, 0,0, SWP_NOZORDER |
102                        SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW |
103                        SWP_DEFERERASE );
104     }
105
106     PAINT_RedrawWindow( hwnd, NULL, hrgnUpdate, RDW_ALLCHILDREN |
107                             RDW_INVALIDATE | RDW_ERASE | RDW_ERASENOW, RDW_C_USEHRGN );
108
109     DeleteObject32( hrgnUpdate );
110     if( hCaretWnd ) 
111     {
112         POINT32 pt;
113         GetCaretPos32(&pt);
114         pt.x += dx; pt.y += dy;
115         SetCaretPos32(pt.x, pt.y);
116         ShowCaret32(hCaretWnd);
117     }
118     return TRUE;
119 }
120
121
122 /*************************************************************************
123  *             ScrollDC16   (USER.221)
124  */
125 BOOL16 ScrollDC16( HDC16 hdc, INT16 dx, INT16 dy, const RECT16 *rect,
126                    const RECT16 *cliprc, HRGN16 hrgnUpdate, LPRECT16 rcUpdate )
127 {
128     RECT32 rect32, clipRect32, rcUpdate32;
129     BOOL16 ret;
130
131     if (rect) CONV_RECT16TO32( rect, &rect32 );
132     if (cliprc) CONV_RECT16TO32( cliprc, &clipRect32 );
133     ret = ScrollDC32( hdc, dx, dy, rect ? &rect32 : NULL,
134                       cliprc ? &clipRect32 : NULL, hrgnUpdate, &rcUpdate32 );
135     if (rcUpdate) CONV_RECT32TO16( &rcUpdate32, rcUpdate );
136     return ret;
137 }
138
139
140 /*************************************************************************
141  *             ScrollDC32   (USER32.448)
142  * 
143  * Both 'rc' and 'rLClip' are in logical units but update info is 
144  * returned in device coordinates.
145  */
146 BOOL32 ScrollDC32( HDC32 hdc, INT32 dx, INT32 dy, const RECT32 *rc,
147                    const RECT32 *prLClip, HRGN32 hrgnUpdate, LPRECT32 rcUpdate )
148 {
149     RECT32 rDClip, rLClip;
150     HRGN32 hrgnClip = 0;
151     HRGN32 hrgnScrollClip = 0;
152     POINT32 src, dest;
153     INT32  ldx, ldy;
154     DC *dc = (DC *)GDI_GetObjPtr(hdc, DC_MAGIC);
155
156     dprintf_scroll(stddeb,"ScrollDC: %04x %d,%d hrgnUpdate=%04x rcUpdate = %p cliprc = (%d,%d-%d,%d), rc=(%d,%d-%d,%d)\n",
157                    (HDC16)hdc, dx, dy, hrgnUpdate, rcUpdate, 
158                    prLClip ? prLClip->left : 0, prLClip ? prLClip->top : 0, prLClip ? prLClip->right : 0, prLClip ? prLClip->bottom : 0,
159                    rc ? rc->left : 0, rc ? rc->top : 0, rc ? rc->right : 0, rc ? rc->bottom : 0 );
160
161     if ( !dc || !hdc ) return FALSE;
162
163 /*
164     printf(stddeb,"\t[wndOrgX=%i, wndExtX=%i, vportOrgX=%i, vportExtX=%i]\n",
165                   dc->wndOrgX, dc->wndExtX, dc->vportOrgX, dc->vportExtX );
166     printf(stddeb,"\t[wndOrgY=%i, wndExtY=%i, vportOrgY=%i, vportExtY=%i]\n",
167                   dc->wndOrgY, dc->wndExtY, dc->vportOrgY, dc->vportExtY );
168 */
169
170     /* compute device clipping region */
171
172     if ( rc )
173     {
174         rLClip = *rc;
175         rDClip.left = XLPTODP(dc, rc->left); rDClip.right = XLPTODP(dc, rc->right);
176         rDClip.top = YLPTODP(dc, rc->top); rDClip.bottom = YLPTODP(dc, rc->bottom);
177     }
178     else /* maybe we should just return FALSE? */
179     {
180         GetClipBox32( hdc, &rDClip );
181         rLClip.left = XDPTOLP(dc, rDClip.left); rLClip.right = XDPTOLP(dc, rDClip.right);
182         rLClip.top = YDPTOLP(dc, rDClip.top); rLClip.bottom = YDPTOLP(dc, rDClip.bottom);
183     }
184
185     if (prLClip)
186     {
187         RECT32 r;
188
189         r.left = XLPTODP(dc, prLClip->left); r.right = XLPTODP(dc, prLClip->right);
190         r.top = YLPTODP(dc, prLClip->top); r.bottom = YLPTODP(dc, prLClip->bottom);
191         IntersectRect32(&rLClip,&rLClip,prLClip);
192         IntersectRect32(&rDClip,&rDClip,&r);
193     }
194
195     if( rDClip.left >= rDClip.right || rDClip.top >= rDClip.bottom )
196         return FALSE;
197     
198     hrgnClip = GetClipRgn16(hdc);
199     hrgnScrollClip = CreateRectRgnIndirect32(&rDClip);
200
201     if( hrgnClip )
202       {
203         /* change device clipping region directly */
204
205         CombineRgn32( hrgnScrollClip, hrgnClip, 0, RGN_COPY );
206         SetRectRgn32( hrgnClip, rDClip.left, rDClip.top,
207                       rDClip.right, rDClip.bottom );
208
209         CLIPPING_UpdateGCRegion( dc );
210       }
211     else
212         SelectClipRgn32( hdc, hrgnScrollClip );
213
214     /* translate coordinates */
215
216     ldx = dx * dc->wndExtX / dc->vportExtX;
217     ldy = dy * dc->wndExtY / dc->vportExtY;
218
219     if (dx > 0)
220         dest.x = (src.x = rLClip.left) + ldx;
221     else
222         src.x = (dest.x = rLClip.left) - ldx;
223
224     if (dy > 0)
225         dest.y = (src.y = rLClip.top) + ldy;
226     else
227         src.y = (dest.y = rLClip.top) - ldy;
228
229     /* copy bits */
230
231     if( rDClip.right - rDClip.left > dx &&
232         rDClip.bottom - rDClip.top > dy )
233     {
234         ldx = rLClip.right - rLClip.left - ldx;
235         ldy = rLClip.bottom - rLClip.top - ldy;
236
237         if (!BitBlt32( hdc, dest.x, dest.y, ldx, ldy,
238                        hdc, src.x, src.y, SRCCOPY))
239             return FALSE;
240     }
241
242     /* restore clipping region */
243
244     if( hrgnClip )
245     {
246         CombineRgn32( hrgnClip, hrgnScrollClip, 0, RGN_COPY );
247         CLIPPING_UpdateGCRegion( dc );
248         SetRectRgn32( hrgnScrollClip, rDClip.left, rDClip.top, 
249                       rDClip.right, rDClip.bottom );
250     }
251     else
252         SelectClipRgn32( hdc, 0 );
253
254     /* compute update areas */
255
256     if (hrgnUpdate || rcUpdate)
257     {
258         HRGN32 hrgn = (hrgnUpdate) ? hrgnUpdate : CreateRectRgn32( 0,0,0,0 );
259
260         if( dc->w.hVisRgn )
261         {
262           CombineRgn32( hrgn, dc->w.hVisRgn, hrgnScrollClip, RGN_AND );
263           OffsetRgn32( hrgn, dx, dy );
264           CombineRgn32( hrgn, dc->w.hVisRgn, hrgn, RGN_DIFF );
265           CombineRgn32( hrgn, hrgn, hrgnScrollClip, RGN_AND );
266         }
267         else
268         {
269           RECT32 rect;
270
271           rect = rDClip;                                /* vertical band */
272           if (dx > 0) rect.right = rect.left + dx;
273           else if (dx < 0) rect.left = rect.right + dx;
274           else SetRectEmpty32( &rect );
275           SetRectRgn32( hrgn, rect.left, rect.top, rect.right, rect.bottom );
276
277           rect = rDClip;                                /* horizontal band */
278           if (dy > 0) rect.bottom = rect.top + dy;
279           else if (dy < 0) rect.top = rect.bottom + dy;
280           else SetRectEmpty32( &rect );
281
282           REGION_UnionRectWithRgn( hrgn, &rect );
283         }
284
285         if (rcUpdate) GetRgnBox32( hrgn, rcUpdate );
286         if (!hrgnUpdate) DeleteObject32( hrgn );
287     }
288
289     DeleteObject32( hrgnScrollClip );     
290     return TRUE;
291 }
292
293
294 /*************************************************************************
295  *             ScrollWindowEx16   (USER.319)
296  */
297 INT16 ScrollWindowEx16( HWND16 hwnd, INT16 dx, INT16 dy, const RECT16 *rect,
298                         const RECT16 *clipRect, HRGN16 hrgnUpdate,
299                         LPRECT16 rcUpdate, UINT16 flags )
300 {
301     RECT32 rect32, clipRect32, rcUpdate32;
302     BOOL16 ret;
303
304     if (rect) CONV_RECT16TO32( rect, &rect32 );
305     if (clipRect) CONV_RECT16TO32( clipRect, &clipRect32 );
306     ret = ScrollWindowEx32( hwnd, dx, dy, rect ? &rect32 : NULL,
307                             clipRect ? &clipRect32 : NULL, hrgnUpdate,
308                             (rcUpdate) ? &rcUpdate32 : NULL, flags );
309     if (rcUpdate) CONV_RECT32TO16( &rcUpdate32, rcUpdate );
310     return ret;
311 }
312
313 /*************************************************************************
314  *             SCROLL_FixCaret
315  */
316 BOOL32 SCROLL_FixCaret(HWND32 hWnd, LPRECT32 lprc, UINT32 flags)
317 {
318    HWND32 hCaret = CARET_GetHwnd();
319
320    if( hCaret )
321    {
322        RECT32   rc;
323        CARET_GetRect( &rc );
324        if( hCaret == hWnd ||
325           (flags & SW_SCROLLCHILDREN && IsChild32(hWnd, hCaret)) )
326        {
327            POINT32     pt;
328
329            pt.x = rc.left; pt.y = rc.top;
330            MapWindowPoints32( hCaret, hWnd, (LPPOINT32)&rc, 2 );
331            if( IntersectRect32(lprc, lprc, &rc) )
332            {
333                HideCaret32(0);
334                lprc->left = pt.x; lprc->top = pt.y;
335                return TRUE;
336            }
337        }
338    }
339    return FALSE;
340 }
341
342 /*************************************************************************
343  *             ScrollWindowEx32   (USER32.450)
344  */
345 INT32 ScrollWindowEx32( HWND32 hwnd, INT32 dx, INT32 dy, const RECT32 *rect,
346                         const RECT32 *clipRect, HRGN32 hrgnUpdate,
347                         LPRECT32 rcUpdate, UINT32 flags )
348 {
349     INT32  retVal = NULLREGION;
350     BOOL32 bCaret = FALSE, bOwnRgn = TRUE;
351     RECT32 rc, cliprc;
352     WND*   wnd = WIN_FindWndPtr( hwnd );
353
354     if( !wnd || !WIN_IsWindowDrawable( wnd, TRUE )) return ERROR;
355
356     if (rect == NULL) GetClientRect32(hwnd, &rc);
357     else rc = *rect;
358
359     if (clipRect) IntersectRect32(&cliprc,&rc,clipRect);
360     else cliprc = rc;
361
362     if (!IsRectEmpty32(&cliprc) && (dx || dy))
363     {
364         DC*     dc;
365         HDC32   hDC;
366         BOOL32  bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
367         HRGN32  hrgnClip = CreateRectRgnIndirect32(&cliprc);
368
369 dprintf_scroll(stddeb,"ScrollWindowEx: %04x, %d,%d hrgnUpdate=%04x rcUpdate = %p \
370 cliprc = (%d,%d-%d,%d), rc=(%d,%d-%d,%d) %04x\n",             
371 (HWND16)hwnd, dx, dy, hrgnUpdate, rcUpdate,
372 clipRect?clipRect->left:0, clipRect?clipRect->top:0, clipRect?clipRect->right:0, clipRect?clipRect->bottom:0,
373 rect?rect->left:0, rect?rect->top:0, rect ?rect->right:0, rect ?rect->bottom:0, (UINT16)flags );
374
375         rc = cliprc;
376         bCaret = SCROLL_FixCaret(hwnd, &rc, flags);
377
378         if( hrgnUpdate ) bOwnRgn = FALSE;
379         else if( bUpdate ) hrgnUpdate = CreateRectRgn32( 0, 0, 0, 0 );
380
381         hDC = GetDCEx32( hwnd, hrgnClip, DCX_CACHE | DCX_USESTYLE | 
382                         (flags & SW_SCROLLCHILDREN) ? DCX_NOCLIPCHILDREN : 0 );
383         if( (dc = (DC *)GDI_GetObjPtr(hDC, DC_MAGIC)) )
384         {
385             POINT32 dst, src;
386
387             if( dx > 0 ) dst.x = (src.x = dc->w.DCOrgX + cliprc.left) + dx;
388             else src.x = (dst.x = dc->w.DCOrgX + cliprc.left) - dx;
389
390             if( dy > 0 ) dst.y = (src.y = dc->w.DCOrgY + cliprc.top) + dy;
391             else src.y = (dst.y = dc->w.DCOrgY + cliprc.top) - dy;
392
393             if( bUpdate )
394                 XSetGraphicsExposures( display, dc->u.x.gc, True );
395             XSetFunction( display, dc->u.x.gc, GXcopy );
396             XCopyArea( display, dc->u.x.drawable, dc->u.x.drawable, dc->u.x.gc, 
397                        src.x, src.y, cliprc.right - cliprc.left - abs(dx),
398                        cliprc.bottom - cliprc.top - abs(dy), dst.x, dst.y );
399             if( bUpdate )
400                 XSetGraphicsExposures( display, dc->u.x.gc, False );
401
402             if( dc->w.hVisRgn && bUpdate )
403             {
404                 CombineRgn32( hrgnUpdate, dc->w.hVisRgn, hrgnClip, RGN_AND );
405                 OffsetRgn32( hrgnUpdate, dx, dy );
406                 CombineRgn32( hrgnUpdate, dc->w.hVisRgn, hrgnUpdate, RGN_DIFF );
407                 CombineRgn32( hrgnUpdate, hrgnUpdate, hrgnClip, RGN_AND );
408
409                 if( rcUpdate ) GetRgnBox32( hrgnUpdate, rcUpdate );
410             }
411             ReleaseDC32(hwnd, hDC);
412         }
413
414         if( wnd->hrgnUpdate > 1 )
415         {
416             if( rect || clipRect )
417             {
418                 if( (CombineRgn32( hrgnClip, hrgnClip, 
419                                    wnd->hrgnUpdate, RGN_AND ) != NULLREGION) )
420                 {
421                     CombineRgn32( wnd->hrgnUpdate, wnd->hrgnUpdate, hrgnClip, RGN_DIFF );
422                     OffsetRgn32( hrgnClip, dx, dy );
423                     CombineRgn32( wnd->hrgnUpdate, wnd->hrgnUpdate, hrgnClip, RGN_OR );
424                 }
425             }
426             else  
427                 OffsetRgn32( wnd->hrgnUpdate, dx, dy );
428         }
429
430         if( flags & SW_SCROLLCHILDREN )
431         {
432             RECT32      r;
433             WND*        w;
434             for( w = wnd->child; w; w = w->next )
435             {
436                  CONV_RECT16TO32( &w->rectWindow, &r );
437                  if( IntersectRect32(&r, &r, &cliprc) )
438                      SetWindowPos32(w->hwndSelf, 0, w->rectWindow.left + dx,
439                                     w->rectWindow.top  + dy, 0,0, SWP_NOZORDER |
440                                     SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW |
441                                     SWP_DEFERERASE );
442             }
443         }
444
445         if( flags & (SW_INVALIDATE | SW_ERASE) )
446             PAINT_RedrawWindow( hwnd, NULL, hrgnUpdate, RDW_INVALIDATE | RDW_ERASE |
447                                         ((flags & SW_ERASE) ? RDW_ERASENOW : 0), 0 );
448
449         if( bCaret )
450         {
451             SetCaretPos32( rc.left + dx, rc.top + dy );
452             ShowCaret32(0);
453         }
454
455         if( bOwnRgn && hrgnUpdate ) DeleteObject32( hrgnUpdate );
456         DeleteObject32( hrgnClip );
457     }
458     return retVal;
459 }
460