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