x11drv: ScrollDC fix.
[wine/wine64.git] / dlls / x11drv / scroll.c
blob78144dafe30723951bab7040a2ced2375c81c31f
1 /*
2 * Scroll windows and DCs
4 * Copyright 1993 David W. Metcalfe
5 * Copyright 1995, 1996 Alex Korobka
6 * Copyright 2001 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <X11/Xlib.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
33 #include "x11drv.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(scroll);
38 static void dump_region( const char *p, HRGN hrgn)
40 DWORD i, size;
41 RGNDATA *data = NULL;
42 RECT *rect;
44 if (!hrgn) {
45 TRACE( "%s null region\n", p );
46 return;
48 if (!(size = GetRegionData( hrgn, 0, NULL ))) {
49 return;
51 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
52 GetRegionData( hrgn, size, data );
53 TRACE("%s %ld rects:", p, data->rdh.nCount );
54 for (i = 0, rect = (RECT *)data->Buffer; i<20 && i < data->rdh.nCount; i++, rect++)
55 TRACE( " %s", wine_dbgstr_rect( rect));
56 TRACE("\n");
57 HeapFree( GetProcessHeap(), 0, data );
60 /*************************************************************************
61 * ScrollDC (X11DRV.@)
63 BOOL X11DRV_ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
64 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
66 RECT rcSrc, rcClip, offset;
67 INT dxdev, dydev, res;
68 HRGN DstRgn, clipRgn, visrgn;
69 INT code = X11DRV_START_EXPOSURES;
71 TRACE("dx,dy %d,%d rcScroll %s rcClip %s hrgnUpdate %p lprcUpdate %p\n",
72 dx, dy, wine_dbgstr_rect(lprcScroll), wine_dbgstr_rect(lprcClip),
73 hrgnUpdate, lprcUpdate);
74 /* enable X-exposure events */
75 if (hrgnUpdate || lprcUpdate)
76 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
77 /* get the visible region */
78 visrgn=CreateRectRgn( 0, 0, 0, 0);
79 GetRandomRgn( hdc, visrgn, SYSRGN);
80 if( !(GetVersion() & 0x80000000)) {
81 /* Window NT/2k/XP */
82 POINT org;
83 GetDCOrgEx(hdc, &org);
84 OffsetRgn( visrgn, -org.x, -org.y);
86 /* intersect with the clipping Region if the DC has one */
87 clipRgn = CreateRectRgn( 0, 0, 0, 0);
88 if (GetClipRgn( hdc, clipRgn) != 1) {
89 DeleteObject(clipRgn);
90 clipRgn=NULL;
91 } else
92 CombineRgn( visrgn, visrgn, clipRgn, RGN_AND);
93 /* only those pixels in the scroll rectangle that remain in the clipping
94 * rect are scrolled. So first combine Scroll and Clipping rectangles,
95 * if available */
96 if( lprcScroll)
98 if( lprcClip)
99 IntersectRect( &rcClip, lprcClip, lprcScroll);
100 else
101 rcClip = *lprcScroll;
103 else
105 if( lprcClip)
106 rcClip = *lprcClip;
107 else
108 GetClipBox( hdc, &rcClip);
110 /* Then clip again to get the source rectangle that will remain in the
111 * clipping rect */
112 rcSrc = rcClip;
113 IntersectRect( &rcSrc, &rcSrc, &rcClip);
114 /* now convert to device coordinates */
115 LPtoDP(hdc, (LPPOINT)&rcSrc, 2);
116 TRACE("source rect: %s\n", wine_dbgstr_rect(&rcSrc));
118 /* also dx and dy */
119 SetRect(&offset, 0, 0, dx, dy);
120 LPtoDP(hdc, (LPPOINT)&offset, 2);
121 dxdev = offset.right - offset.left;
122 dydev = offset.bottom - offset.top;
123 /* now intersect with the visible region to get the pixels that will
124 * actually scroll */
125 DstRgn = CreateRectRgnIndirect( &rcSrc);
126 res = CombineRgn( DstRgn, DstRgn, visrgn, RGN_AND);
127 /* and translate, giving the destination region */
128 OffsetRgn( DstRgn, dxdev, dydev);
129 if( TRACE_ON( scroll)) dump_region( "Destination scroll region: ", DstRgn);
130 /* if there are any, do it */
131 if( res > NULLREGION) {
132 RECT rect ;
133 /* clip to the destination region, so we can BitBlt with a simple
134 * bounding rectangle */
135 if( clipRgn)
136 ExtSelectClipRgn( hdc, DstRgn, RGN_AND);
137 else
138 SelectClipRgn( hdc, DstRgn);
139 GetRgnBox( DstRgn, &rect);
140 DPtoLP(hdc, (LPPOINT)&rect, 2);
141 TRACE("destination rect: %s\n", wine_dbgstr_rect(&rect));
143 BitBlt( hdc, rect.left, rect.top,
144 rect.right - rect.left, rect.bottom -rect.top,
145 hdc, rect.left - dx, rect.top - dy, SRCCOPY);
147 /* compute the update areas. This is the combined clip rectangle
148 * minus the scrolled region, and intersected with the visible
149 * region. */
150 if (hrgnUpdate || lprcUpdate)
152 HRGN hrgn = hrgnUpdate;
153 HRGN ExpRgn = 0;
155 /* collect all the exposures */
156 code = X11DRV_END_EXPOSURES;
157 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code,
158 sizeof(ExpRgn), (LPSTR)&ExpRgn );
159 /* Convert the combined clip rectangle to device coordinates */
160 LPtoDP(hdc, (LPPOINT)&rcClip, 2);
161 if( hrgn )
162 SetRectRgn( hrgn, rcClip.left, rcClip.top, rcClip.right,
163 rcClip.bottom);
164 else
165 hrgn = CreateRectRgnIndirect( &rcClip);
166 CombineRgn( hrgn, hrgn, visrgn, RGN_AND);
167 CombineRgn( hrgn, hrgn, DstRgn, RGN_DIFF);
168 /* add the exposures to this */
169 if( ExpRgn) {
170 if( TRACE_ON( scroll)) dump_region( "Expose region: ", ExpRgn);
171 CombineRgn( hrgn, hrgn, ExpRgn, RGN_OR);
172 DeleteObject( ExpRgn);
174 if( TRACE_ON( scroll)) dump_region( "Update region: ", hrgn);
175 if( lprcUpdate) {
176 GetRgnBox( hrgn, lprcUpdate );
177 /* Put the lprcUpdate in logical coordinates */
178 DPtoLP( hdc, (LPPOINT)lprcUpdate, 2 );
179 TRACE("returning lprcUpdate %s\n", wine_dbgstr_rect(lprcUpdate));
181 if( !hrgnUpdate)
182 DeleteObject( hrgn);
184 SelectClipRgn( hdc, clipRgn);
185 DeleteObject( visrgn);
186 DeleteObject( DstRgn);
187 if( clipRgn) DeleteObject( clipRgn);
188 return TRUE;