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