Stop testing a sequence upon the first expected error, to avoid
[wine.git] / windows / scroll.c
blob266f87d51ed3a6667cd4e0d3e45b193cfc12b60b
1 /*
2 * Scroll windows and DCs
4 * Copyright David W. Metcalfe, 1993
5 * Alex Korobka 1995,1996
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdarg.h>
23 #include <stdlib.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "wine/winuser16.h"
29 #include "winuser.h"
30 #include "user.h"
31 #include "win.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(scroll);
36 /*************************************************************************
37 * fix_caret
39 static HWND fix_caret(HWND hWnd, LPRECT lprc, UINT flags)
41 GUITHREADINFO info;
43 if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
44 if (!info.hwndCaret) return 0;
45 if (info.hwndCaret == hWnd ||
46 ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret)))
48 POINT pt;
49 pt.x = info.rcCaret.left;
50 pt.y = info.rcCaret.top;
51 MapWindowPoints( info.hwndCaret, hWnd, (LPPOINT)&info.rcCaret, 2 );
52 if( IntersectRect(lprc, lprc, &info.rcCaret) )
54 HideCaret(0);
55 lprc->left = pt.x;
56 lprc->top = pt.y;
57 return info.hwndCaret;
60 return 0;
63 /*************************************************************************
64 * ScrollWindowEx (USER32.@)
66 * Note: contrary to what the doc says, pixels that are scrolled from the
67 * outside of clipRect to the inside are NOT painted.
70 INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
71 const RECT *rect, const RECT *clipRect,
72 HRGN hrgnUpdate, LPRECT rcUpdate,
73 UINT flags )
75 INT retVal = NULLREGION;
76 BOOL bOwnRgn = TRUE;
77 BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
78 HRGN hrgnTemp;
79 HDC hDC;
80 RECT rc, cliprc;
81 RECT caretrc;
82 HWND hwndCaret = NULL;
84 TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
85 hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
86 TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
88 if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
89 hwnd = WIN_GetFullHandle( hwnd );
91 GetClientRect(hwnd, &rc);
92 if (rect) IntersectRect(&rc, &rc, rect);
94 if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
95 else cliprc = rc;
97 if( hrgnUpdate ) bOwnRgn = FALSE;
98 else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
100 if( !IsRectEmpty(&cliprc) && (dx || dy)) {
101 caretrc = rc;
102 hwndCaret = fix_caret(hwnd, &caretrc, flags);
104 hDC = GetDCEx( hwnd, 0, DCX_CACHE | DCX_USESTYLE );
105 if (hDC)
107 ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
109 ReleaseDC( hwnd, hDC );
111 if (!bUpdate)
112 RedrawWindow( hwnd, NULL, hrgnUpdate, RDW_INVALIDATE | RDW_ERASE );
115 /* Take into account the fact that some damage may have occurred during
116 * the scroll */
117 hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
118 retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
119 if (retVal != NULLREGION)
121 HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
122 OffsetRgn( hrgnTemp, dx, dy );
123 CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
124 RedrawWindow( hwnd, NULL, hrgnTemp, RDW_INVALIDATE | RDW_ERASE );
125 DeleteObject( hrgnClip );
127 DeleteObject( hrgnTemp );
128 } else {
129 /* nothing was scrolled */
130 if( !bOwnRgn)
131 SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
132 SetRectEmpty( rcUpdate);
135 if( flags & SW_SCROLLCHILDREN )
137 HWND *list = WIN_ListChildren( hwnd );
138 if (list)
140 int i;
141 RECT r, dummy;
142 for (i = 0; list[i]; i++)
144 GetWindowRect( list[i], &r );
145 MapWindowPoints( 0, hwnd, (POINT *)&r, 2 );
146 if (!rect || IntersectRect(&dummy, &r, rect))
147 SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
148 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
149 SWP_NOREDRAW | SWP_DEFERERASE );
151 HeapFree( GetProcessHeap(), 0, list );
155 if( flags & (SW_INVALIDATE | SW_ERASE) )
156 RedrawWindow( hwnd, NULL, hrgnUpdate, RDW_INVALIDATE | RDW_ERASE |
157 ((flags & SW_ERASE) ? RDW_ERASENOW : 0) |
158 ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
160 if( hwndCaret ) {
161 SetCaretPos( caretrc.left + dx, caretrc.top + dy );
162 ShowCaret(hwndCaret);
165 if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
167 return retVal;
170 /*************************************************************************
171 * ScrollWindow (USER32.@)
174 BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
175 const RECT *rect, const RECT *clipRect )
177 return
178 (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
179 (rect ? 0 : SW_SCROLLCHILDREN) |
180 SW_INVALIDATE ));
183 /*************************************************************************
184 * ScrollDC (USER32.@)
186 * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is wrong)
187 * hrgnUpdate is returned in device coordinates with rcUpdate in logical coordinates.
189 BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
190 const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
193 RECT rSrc, rClipped_src, rClip, rDst, offset;
195 TRACE( "%p %d,%d hrgnUpdate=%p lprcUpdate = %p\n", hdc, dx, dy, hrgnUpdate, lprcUpdate );
196 if (lprcClip) TRACE( "lprcClip = %s\n", wine_dbgstr_rect(lprcClip));
197 if (lprcScroll) TRACE( "lprcScroll = %s\n", wine_dbgstr_rect(lprcScroll));
199 if (USER_Driver.pScrollDC)
200 return USER_Driver.pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
202 /* compute device clipping region (in device coordinates) */
203 if (lprcScroll) rSrc = *lprcScroll;
204 else GetClipBox( hdc, &rSrc );
205 LPtoDP(hdc, (LPPOINT)&rSrc, 2);
207 if (lprcClip) rClip = *lprcClip;
208 else GetClipBox( hdc, &rClip );
209 LPtoDP(hdc, (LPPOINT)&rClip, 2);
211 IntersectRect( &rClipped_src, &rSrc, &rClip );
212 TRACE("rSrc %s rClip %s clipped rSrc %s\n", wine_dbgstr_rect(&rSrc),
213 wine_dbgstr_rect(&rClip), wine_dbgstr_rect(&rClipped_src));
215 rDst = rClipped_src;
216 SetRect(&offset, 0, 0, dx, dy);
217 LPtoDP(hdc, (LPPOINT)&offset, 2);
218 OffsetRect( &rDst, offset.right - offset.left, offset.bottom - offset.top );
219 TRACE("rDst before clipping %s\n", wine_dbgstr_rect(&rDst));
220 IntersectRect( &rDst, &rDst, &rClip );
221 TRACE("rDst after clipping %s\n", wine_dbgstr_rect(&rDst));
223 if (!IsRectEmpty(&rDst))
225 /* copy bits */
226 RECT rDst_lp = rDst, rSrc_lp = rDst;
228 OffsetRect( &rSrc_lp, offset.left - offset.right, offset.top - offset.bottom );
229 DPtoLP(hdc, (LPPOINT)&rDst_lp, 2);
230 DPtoLP(hdc, (LPPOINT)&rSrc_lp, 2);
232 if (!BitBlt( hdc, rDst_lp.left, rDst_lp.top,
233 rDst_lp.right - rDst_lp.left, rDst_lp.bottom - rDst_lp.top,
234 hdc, rSrc_lp.left, rSrc_lp.top, SRCCOPY))
235 return FALSE;
238 /* compute update areas. This is the clipped source or'ed with the unclipped source translated minus the
239 clipped src translated (rDst) all clipped to rClip */
241 if (hrgnUpdate || lprcUpdate)
243 HRGN hrgn = hrgnUpdate, hrgn2;
245 if (hrgn) SetRectRgn( hrgn, rClipped_src.left, rClipped_src.top, rClipped_src.right, rClipped_src.bottom );
246 else hrgn = CreateRectRgn( rClipped_src.left, rClipped_src.top, rClipped_src.right, rClipped_src.bottom );
248 hrgn2 = CreateRectRgnIndirect( &rSrc );
249 OffsetRgn(hrgn2, offset.right - offset.left, offset.bottom - offset.top );
250 CombineRgn(hrgn, hrgn, hrgn2, RGN_OR);
252 SetRectRgn( hrgn2, rDst.left, rDst.top, rDst.right, rDst.bottom );
253 CombineRgn( hrgn, hrgn, hrgn2, RGN_DIFF );
255 SetRectRgn( hrgn2, rClip.left, rClip.top, rClip.right, rClip.bottom );
256 CombineRgn( hrgn, hrgn, hrgn2, RGN_AND );
258 if( lprcUpdate )
260 GetRgnBox( hrgn, lprcUpdate );
262 /* Put the lprcUpdate in logical coordinate */
263 DPtoLP( hdc, (LPPOINT)lprcUpdate, 2 );
264 TRACE("returning lprcUpdate %s\n", wine_dbgstr_rect(lprcUpdate));
266 if (!hrgnUpdate) DeleteObject( hrgn );
267 DeleteObject( hrgn2 );
269 return TRUE;