Release 971221
[wine/multimedia.git] / windows / scroll.c
blobb4daae693fbf240247728123d152a0cc85cfa850
1 /*
2 * Scroll windows and DCs
4 * Copyright David W. Metcalfe, 1993
5 * Alex Korobka 1995,1996
8 */
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"
22 extern HWND32 CARET_GetHwnd(); /* windows/caret.c */
23 extern void CARET_GetRect(LPRECT32);
24 extern void CLIPPING_UpdateGCRegion(DC* ); /* objects/clipping.c */
26 /*************************************************************************
27 * ScrollWindow16 (USER.61)
29 void WINAPI ScrollWindow16(HWND16 hwnd, INT16 dx, INT16 dy, const RECT16 *rect,
30 const RECT16 *clipRect )
32 RECT32 rect32, clipRect32;
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 );
40 /*************************************************************************
41 * ScrollWindow32 (USER32.449)
43 * FIXME: verify clipping region calculations
45 BOOL32 WINAPI ScrollWindow32( HWND32 hwnd, INT32 dx, INT32 dy,
46 const RECT32 *rect, const RECT32 *clipRect )
48 HDC32 hdc;
49 HRGN32 hrgnUpdate,hrgnClip;
50 RECT32 rc, cliprc;
51 HWND32 hCaretWnd = CARET_GetHwnd();
52 WND* wndScroll = WIN_FindWndPtr( hwnd );
54 dprintf_scroll(stddeb,"ScrollWindow: hwnd=%04x, dx=%d, dy=%d, lpRect =%p clipRect=%i,%i,%i,%i\n",
55 hwnd, dx, dy, rect,
56 clipRect ? clipRect->left : 0,
57 clipRect ? clipRect->top : 0,
58 clipRect ? clipRect->right : 0,
59 clipRect ? clipRect->bottom : 0 );
61 if ( !wndScroll || !WIN_IsWindowDrawable( wndScroll, TRUE ) ) return TRUE;
63 if ( !rect ) /* do not clip children */
65 GetClientRect32(hwnd, &rc);
66 hrgnClip = CreateRectRgnIndirect32( &rc );
68 if ((hCaretWnd == hwnd) || IsChild32(hwnd,hCaretWnd))
69 HideCaret32(hCaretWnd);
70 else hCaretWnd = 0;
72 hdc = GetDCEx32(hwnd, hrgnClip, DCX_CACHE | DCX_CLIPSIBLINGS);
73 DeleteObject32( hrgnClip );
75 else /* clip children */
77 CopyRect32(&rc, rect);
79 if (hCaretWnd == hwnd) HideCaret32(hCaretWnd);
80 else hCaretWnd = 0;
82 hdc = GetDCEx32( hwnd, 0, DCX_CACHE | DCX_USESTYLE );
85 if (clipRect == NULL)
86 GetClientRect32(hwnd, &cliprc);
87 else
88 CopyRect32(&cliprc, clipRect);
90 hrgnUpdate = CreateRectRgn32( 0, 0, 0, 0 );
91 ScrollDC32( hdc, dx, dy, &rc, &cliprc, hrgnUpdate, NULL );
92 ReleaseDC32(hwnd, hdc);
94 if( !rect ) /* move child windows and update region */
96 WND* wndPtr;
98 if( wndScroll->hrgnUpdate > 1 )
99 OffsetRgn32( wndScroll->hrgnUpdate, dx, dy );
101 for (wndPtr = wndScroll->child; wndPtr; wndPtr = wndPtr->next)
102 SetWindowPos32(wndPtr->hwndSelf, 0, wndPtr->rectWindow.left + dx,
103 wndPtr->rectWindow.top + dy, 0,0, SWP_NOZORDER |
104 SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW |
105 SWP_DEFERERASE );
108 PAINT_RedrawWindow( hwnd, NULL, hrgnUpdate, RDW_ALLCHILDREN |
109 RDW_INVALIDATE | RDW_ERASE | RDW_ERASENOW, RDW_C_USEHRGN );
111 DeleteObject32( hrgnUpdate );
112 if( hCaretWnd )
114 POINT32 pt;
115 GetCaretPos32(&pt);
116 pt.x += dx; pt.y += dy;
117 SetCaretPos32(pt.x, pt.y);
118 ShowCaret32(hCaretWnd);
120 return TRUE;
124 /*************************************************************************
125 * ScrollDC16 (USER.221)
127 BOOL16 WINAPI ScrollDC16( HDC16 hdc, INT16 dx, INT16 dy, const RECT16 *rect,
128 const RECT16 *cliprc, HRGN16 hrgnUpdate,
129 LPRECT16 rcUpdate )
131 RECT32 rect32, clipRect32, rcUpdate32;
132 BOOL16 ret;
134 if (rect) CONV_RECT16TO32( rect, &rect32 );
135 if (cliprc) CONV_RECT16TO32( cliprc, &clipRect32 );
136 ret = ScrollDC32( hdc, dx, dy, rect ? &rect32 : NULL,
137 cliprc ? &clipRect32 : NULL, hrgnUpdate, &rcUpdate32 );
138 if (rcUpdate) CONV_RECT32TO16( &rcUpdate32, rcUpdate );
139 return ret;
143 /*************************************************************************
144 * ScrollDC32 (USER32.448)
146 * Both 'rc' and 'rLClip' are in logical units but update info is
147 * returned in device coordinates.
149 BOOL32 WINAPI ScrollDC32( HDC32 hdc, INT32 dx, INT32 dy, const RECT32 *rc,
150 const RECT32 *prLClip, HRGN32 hrgnUpdate,
151 LPRECT32 rcUpdate )
153 RECT32 rDClip, rLClip;
154 HRGN32 hrgnClip = 0;
155 HRGN32 hrgnScrollClip = 0;
156 POINT32 src, dest;
157 INT32 ldx, ldy;
158 DC *dc = (DC *)GDI_GetObjPtr(hdc, DC_MAGIC);
160 dprintf_scroll(stddeb,"ScrollDC: %04x %d,%d hrgnUpdate=%04x rcUpdate = %p cliprc = (%d,%d-%d,%d), rc=(%d,%d-%d,%d)\n",
161 (HDC16)hdc, dx, dy, hrgnUpdate, rcUpdate,
162 prLClip ? prLClip->left : 0, prLClip ? prLClip->top : 0, prLClip ? prLClip->right : 0, prLClip ? prLClip->bottom : 0,
163 rc ? rc->left : 0, rc ? rc->top : 0, rc ? rc->right : 0, rc ? rc->bottom : 0 );
165 if ( !dc || !hdc ) return FALSE;
168 printf(stddeb,"\t[wndOrgX=%i, wndExtX=%i, vportOrgX=%i, vportExtX=%i]\n",
169 dc->wndOrgX, dc->wndExtX, dc->vportOrgX, dc->vportExtX );
170 printf(stddeb,"\t[wndOrgY=%i, wndExtY=%i, vportOrgY=%i, vportExtY=%i]\n",
171 dc->wndOrgY, dc->wndExtY, dc->vportOrgY, dc->vportExtY );
174 /* compute device clipping region */
176 if ( rc )
178 rLClip = *rc;
179 rDClip.left = XLPTODP(dc, rc->left); rDClip.right = XLPTODP(dc, rc->right);
180 rDClip.top = YLPTODP(dc, rc->top); rDClip.bottom = YLPTODP(dc, rc->bottom);
182 else /* maybe we should just return FALSE? */
184 GetClipBox32( hdc, &rDClip );
185 rLClip.left = XDPTOLP(dc, rDClip.left); rLClip.right = XDPTOLP(dc, rDClip.right);
186 rLClip.top = YDPTOLP(dc, rDClip.top); rLClip.bottom = YDPTOLP(dc, rDClip.bottom);
189 if (prLClip)
191 RECT32 r;
193 r.left = XLPTODP(dc, prLClip->left); r.right = XLPTODP(dc, prLClip->right);
194 r.top = YLPTODP(dc, prLClip->top); r.bottom = YLPTODP(dc, prLClip->bottom);
195 IntersectRect32(&rLClip,&rLClip,prLClip);
196 IntersectRect32(&rDClip,&rDClip,&r);
199 if( rDClip.left >= rDClip.right || rDClip.top >= rDClip.bottom )
200 return FALSE;
202 hrgnClip = GetClipRgn16(hdc);
203 hrgnScrollClip = CreateRectRgnIndirect32(&rDClip);
205 if( hrgnClip )
207 /* change device clipping region directly */
209 CombineRgn32( hrgnScrollClip, hrgnClip, 0, RGN_COPY );
210 SetRectRgn32( hrgnClip, rDClip.left, rDClip.top,
211 rDClip.right, rDClip.bottom );
213 CLIPPING_UpdateGCRegion( dc );
215 else
216 SelectClipRgn32( hdc, hrgnScrollClip );
218 /* translate coordinates */
220 ldx = dx * dc->wndExtX / dc->vportExtX;
221 ldy = dy * dc->wndExtY / dc->vportExtY;
223 if (dx > 0)
224 dest.x = (src.x = rLClip.left) + ldx;
225 else
226 src.x = (dest.x = rLClip.left) - ldx;
228 if (dy > 0)
229 dest.y = (src.y = rLClip.top) + ldy;
230 else
231 src.y = (dest.y = rLClip.top) - ldy;
233 /* copy bits */
235 if( rDClip.right - rDClip.left > dx &&
236 rDClip.bottom - rDClip.top > dy )
238 ldx = rLClip.right - rLClip.left - ldx;
239 ldy = rLClip.bottom - rLClip.top - ldy;
241 if (!BitBlt32( hdc, dest.x, dest.y, ldx, ldy,
242 hdc, src.x, src.y, SRCCOPY))
243 return FALSE;
246 /* restore clipping region */
248 if( hrgnClip )
250 CombineRgn32( hrgnClip, hrgnScrollClip, 0, RGN_COPY );
251 CLIPPING_UpdateGCRegion( dc );
252 SetRectRgn32( hrgnScrollClip, rDClip.left, rDClip.top,
253 rDClip.right, rDClip.bottom );
255 else
256 SelectClipRgn32( hdc, 0 );
258 /* compute update areas */
260 if (hrgnUpdate || rcUpdate)
262 HRGN32 hrgn = (hrgnUpdate) ? hrgnUpdate : CreateRectRgn32( 0,0,0,0 );
264 if( dc->w.hVisRgn )
266 CombineRgn32( hrgn, dc->w.hVisRgn, hrgnScrollClip, RGN_AND );
267 OffsetRgn32( hrgn, dx, dy );
268 CombineRgn32( hrgn, dc->w.hVisRgn, hrgn, RGN_DIFF );
269 CombineRgn32( hrgn, hrgn, hrgnScrollClip, RGN_AND );
271 else
273 RECT32 rect;
275 rect = rDClip; /* vertical band */
276 if (dx > 0) rect.right = rect.left + dx;
277 else if (dx < 0) rect.left = rect.right + dx;
278 else SetRectEmpty32( &rect );
279 SetRectRgn32( hrgn, rect.left, rect.top, rect.right, rect.bottom );
281 rect = rDClip; /* horizontal band */
282 if (dy > 0) rect.bottom = rect.top + dy;
283 else if (dy < 0) rect.top = rect.bottom + dy;
284 else SetRectEmpty32( &rect );
286 REGION_UnionRectWithRgn( hrgn, &rect );
289 if (rcUpdate) GetRgnBox32( hrgn, rcUpdate );
290 if (!hrgnUpdate) DeleteObject32( hrgn );
293 DeleteObject32( hrgnScrollClip );
294 return TRUE;
298 /*************************************************************************
299 * ScrollWindowEx16 (USER.319)
301 INT16 WINAPI ScrollWindowEx16( HWND16 hwnd, INT16 dx, INT16 dy,
302 const RECT16 *rect, const RECT16 *clipRect,
303 HRGN16 hrgnUpdate, LPRECT16 rcUpdate,
304 UINT16 flags )
306 RECT32 rect32, clipRect32, rcUpdate32;
307 BOOL16 ret;
309 if (rect) CONV_RECT16TO32( rect, &rect32 );
310 if (clipRect) CONV_RECT16TO32( clipRect, &clipRect32 );
311 ret = ScrollWindowEx32( hwnd, dx, dy, rect ? &rect32 : NULL,
312 clipRect ? &clipRect32 : NULL, hrgnUpdate,
313 (rcUpdate) ? &rcUpdate32 : NULL, flags );
314 if (rcUpdate) CONV_RECT32TO16( &rcUpdate32, rcUpdate );
315 return ret;
318 /*************************************************************************
319 * SCROLL_FixCaret
321 static BOOL32 SCROLL_FixCaret(HWND32 hWnd, LPRECT32 lprc, UINT32 flags)
323 HWND32 hCaret = CARET_GetHwnd();
325 if( hCaret )
327 RECT32 rc;
328 CARET_GetRect( &rc );
329 if( hCaret == hWnd ||
330 (flags & SW_SCROLLCHILDREN && IsChild32(hWnd, hCaret)) )
332 POINT32 pt;
334 pt.x = rc.left; pt.y = rc.top;
335 MapWindowPoints32( hCaret, hWnd, (LPPOINT32)&rc, 2 );
336 if( IntersectRect32(lprc, lprc, &rc) )
338 HideCaret32(0);
339 lprc->left = pt.x; lprc->top = pt.y;
340 return TRUE;
344 return FALSE;
347 /*************************************************************************
348 * ScrollWindowEx32 (USER32.450)
350 * NOTE: Use this function instead of ScrollWindow32
352 INT32 WINAPI ScrollWindowEx32( HWND32 hwnd, INT32 dx, INT32 dy,
353 const RECT32 *rect, const RECT32 *clipRect,
354 HRGN32 hrgnUpdate, LPRECT32 rcUpdate,
355 UINT32 flags )
357 INT32 retVal = NULLREGION;
358 BOOL32 bCaret = FALSE, bOwnRgn = TRUE;
359 RECT32 rc, cliprc;
360 WND* wnd = WIN_FindWndPtr( hwnd );
362 if( !wnd || !WIN_IsWindowDrawable( wnd, TRUE )) return ERROR;
364 if (rect == NULL) GetClientRect32(hwnd, &rc);
365 else rc = *rect;
367 if (clipRect) IntersectRect32(&cliprc,&rc,clipRect);
368 else cliprc = rc;
370 if (!IsRectEmpty32(&cliprc) && (dx || dy))
372 DC* dc;
373 HDC32 hDC;
374 BOOL32 bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
375 HRGN32 hrgnClip = CreateRectRgnIndirect32(&cliprc);
377 dprintf_scroll(stddeb,"ScrollWindowEx: %04x, %d,%d hrgnUpdate=%04x rcUpdate = %p \
378 cliprc = (%d,%d-%d,%d), rc=(%d,%d-%d,%d) %04x\n",
379 (HWND16)hwnd, dx, dy, hrgnUpdate, rcUpdate,
380 clipRect?clipRect->left:0, clipRect?clipRect->top:0, clipRect?clipRect->right:0, clipRect?clipRect->bottom:0,
381 rect?rect->left:0, rect?rect->top:0, rect ?rect->right:0, rect ?rect->bottom:0, (UINT16)flags );
383 rc = cliprc;
384 bCaret = SCROLL_FixCaret(hwnd, &rc, flags);
386 if( hrgnUpdate ) bOwnRgn = FALSE;
387 else if( bUpdate ) hrgnUpdate = CreateRectRgn32( 0, 0, 0, 0 );
389 hDC = GetDCEx32( hwnd, hrgnClip, DCX_CACHE | DCX_USESTYLE |
390 ((flags & SW_SCROLLCHILDREN) ? DCX_NOCLIPCHILDREN : 0) );
391 if( (dc = (DC *)GDI_GetObjPtr(hDC, DC_MAGIC)) )
393 POINT32 dst, src;
395 if( dx > 0 ) dst.x = (src.x = dc->w.DCOrgX + cliprc.left) + dx;
396 else src.x = (dst.x = dc->w.DCOrgX + cliprc.left) - dx;
398 if( dy > 0 ) dst.y = (src.y = dc->w.DCOrgY + cliprc.top) + dy;
399 else src.y = (dst.y = dc->w.DCOrgY + cliprc.top) - dy;
401 if( bUpdate ) /* handles non-Wine windows hanging over the scrolled area */
402 XSetGraphicsExposures( display, dc->u.x.gc, True );
404 XSetFunction( display, dc->u.x.gc, GXcopy );
405 XCopyArea( display, dc->u.x.drawable, dc->u.x.drawable, dc->u.x.gc,
406 src.x, src.y, cliprc.right - cliprc.left - abs(dx),
407 cliprc.bottom - cliprc.top - abs(dy), dst.x, dst.y );
409 if( bUpdate )
410 XSetGraphicsExposures( display, dc->u.x.gc, False );
412 if( dc->w.hVisRgn && bUpdate )
414 CombineRgn32( hrgnUpdate, dc->w.hVisRgn, hrgnClip, RGN_AND );
415 OffsetRgn32( hrgnUpdate, dx, dy );
416 CombineRgn32( hrgnUpdate, dc->w.hVisRgn, hrgnUpdate, RGN_DIFF );
417 CombineRgn32( hrgnUpdate, hrgnUpdate, hrgnClip, RGN_AND );
419 if( rcUpdate ) GetRgnBox32( hrgnUpdate, rcUpdate );
421 ReleaseDC32(hwnd, hDC);
424 if( wnd->hrgnUpdate > 1 )
426 if( rect || clipRect )
428 if( (CombineRgn32( hrgnClip, hrgnClip,
429 wnd->hrgnUpdate, RGN_AND ) != NULLREGION) )
431 CombineRgn32( wnd->hrgnUpdate, wnd->hrgnUpdate, hrgnClip, RGN_DIFF );
432 OffsetRgn32( hrgnClip, dx, dy );
433 CombineRgn32( wnd->hrgnUpdate, wnd->hrgnUpdate, hrgnClip, RGN_OR );
436 else
437 OffsetRgn32( wnd->hrgnUpdate, dx, dy );
440 if( flags & SW_SCROLLCHILDREN )
442 RECT32 r;
443 WND* w;
444 for( w = wnd->child; w; w = w->next )
446 CONV_RECT16TO32( &w->rectWindow, &r );
447 if( !clipRect || IntersectRect32(&r, &r, &cliprc) )
448 SetWindowPos32(w->hwndSelf, 0, w->rectWindow.left + dx,
449 w->rectWindow.top + dy, 0,0, SWP_NOZORDER |
450 SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW |
451 SWP_DEFERERASE );
455 if( flags & (SW_INVALIDATE | SW_ERASE) )
456 PAINT_RedrawWindow( hwnd, NULL, hrgnUpdate, RDW_INVALIDATE | RDW_ERASE |
457 ((flags & SW_ERASE) ? RDW_ERASENOW : 0) | ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ), 0 );
459 if( bCaret )
461 SetCaretPos32( rc.left + dx, rc.top + dy );
462 ShowCaret32(0);
465 if( bOwnRgn && hrgnUpdate ) DeleteObject32( hrgnUpdate );
466 DeleteObject32( hrgnClip );
468 return retVal;