scroll.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Scroll windows and DCs
  3. *
  4. * Copyright David W. Metcalfe, 1993
  5. * Alex Korobka 1995,1996
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #include <stdarg.h>
  22. #include <stdlib.h>
  23. #include "windef.h"
  24. #include "winbase.h"
  25. #include "wingdi.h"
  26. #include "wine/winuser16.h"
  27. #include "winuser.h"
  28. #include "user_private.h"
  29. #include "win.h"
  30. #include "wine/debug.h"
  31. WINE_DEFAULT_DEBUG_CHANNEL(scroll);
  32. /*************************************************************************
  33. * fix_caret
  34. */
  35. static HWND fix_caret(HWND hWnd, LPRECT lprc, UINT flags)
  36. {
  37. GUITHREADINFO info;
  38. if (!GetGUIThreadInfo( GetCurrentThreadId(), &info )) return 0;
  39. if (!info.hwndCaret) return 0;
  40. if (info.hwndCaret == hWnd ||
  41. ((flags & SW_SCROLLCHILDREN) && IsChild(hWnd, info.hwndCaret)))
  42. {
  43. POINT pt;
  44. pt.x = info.rcCaret.left;
  45. pt.y = info.rcCaret.top;
  46. MapWindowPoints( info.hwndCaret, hWnd, (LPPOINT)&info.rcCaret, 2 );
  47. if( IntersectRect(lprc, lprc, &info.rcCaret) )
  48. {
  49. HideCaret(0);
  50. lprc->left = pt.x;
  51. lprc->top = pt.y;
  52. return info.hwndCaret;
  53. }
  54. }
  55. return 0;
  56. }
  57. /*************************************************************************
  58. * ScrollWindowEx (USER32.@)
  59. *
  60. * Note: contrary to what the doc says, pixels that are scrolled from the
  61. * outside of clipRect to the inside are NOT painted.
  62. *
  63. */
  64. INT WINAPI ScrollWindowEx( HWND hwnd, INT dx, INT dy,
  65. const RECT *rect, const RECT *clipRect,
  66. HRGN hrgnUpdate, LPRECT rcUpdate,
  67. UINT flags )
  68. {
  69. INT retVal = NULLREGION;
  70. BOOL bOwnRgn = TRUE;
  71. BOOL bUpdate = (rcUpdate || hrgnUpdate || flags & (SW_INVALIDATE | SW_ERASE));
  72. int rdw_flags;
  73. HRGN hrgnTemp;
  74. HDC hDC;
  75. RECT rc, cliprc;
  76. RECT caretrc;
  77. HWND hwndCaret = NULL;
  78. TRACE( "%p, %d,%d hrgnUpdate=%p rcUpdate = %p %s %04x\n",
  79. hwnd, dx, dy, hrgnUpdate, rcUpdate, wine_dbgstr_rect(rect), flags );
  80. TRACE( "clipRect = %s\n", wine_dbgstr_rect(clipRect));
  81. if( flags & ~( SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE))
  82. FIXME("some flags (%04x) are unhandled\n", flags);
  83. rdw_flags = (flags & SW_ERASE) && (flags & SW_INVALIDATE) ?
  84. RDW_INVALIDATE | RDW_ERASE : RDW_INVALIDATE ;
  85. if (!WIN_IsWindowDrawable( hwnd, TRUE )) return ERROR;
  86. hwnd = WIN_GetFullHandle( hwnd );
  87. GetClientRect(hwnd, &rc);
  88. if (rect) IntersectRect(&rc, &rc, rect);
  89. if (clipRect) IntersectRect(&cliprc,&rc,clipRect);
  90. else cliprc = rc;
  91. if( hrgnUpdate ) bOwnRgn = FALSE;
  92. else if( bUpdate ) hrgnUpdate = CreateRectRgn( 0, 0, 0, 0 );
  93. if( !IsRectEmpty(&cliprc) && (dx || dy)) {
  94. caretrc = rc;
  95. hwndCaret = fix_caret(hwnd, &caretrc, flags);
  96. hDC = GetDCEx( hwnd, 0, DCX_CACHE | DCX_USESTYLE );
  97. if (hDC)
  98. {
  99. ScrollDC( hDC, dx, dy, &rc, &cliprc, hrgnUpdate, rcUpdate );
  100. ReleaseDC( hwnd, hDC );
  101. if (!bUpdate)
  102. RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags);
  103. }
  104. /* Take into account the fact that some damage may have occurred during
  105. * the scroll */
  106. hrgnTemp = CreateRectRgn( 0, 0, 0, 0 );
  107. retVal = GetUpdateRgn( hwnd, hrgnTemp, FALSE );
  108. if (retVal != NULLREGION)
  109. {
  110. HRGN hrgnClip = CreateRectRgnIndirect(&cliprc);
  111. OffsetRgn( hrgnTemp, dx, dy );
  112. CombineRgn( hrgnTemp, hrgnTemp, hrgnClip, RGN_AND );
  113. RedrawWindow( hwnd, NULL, hrgnTemp, rdw_flags);
  114. DeleteObject( hrgnClip );
  115. }
  116. DeleteObject( hrgnTemp );
  117. } else {
  118. /* nothing was scrolled */
  119. if( !bOwnRgn)
  120. SetRectRgn( hrgnUpdate, 0, 0, 0, 0 );
  121. SetRectEmpty( rcUpdate);
  122. }
  123. if( flags & SW_SCROLLCHILDREN )
  124. {
  125. HWND *list = WIN_ListChildren( hwnd );
  126. if (list)
  127. {
  128. int i;
  129. RECT r, dummy;
  130. for (i = 0; list[i]; i++)
  131. {
  132. GetWindowRect( list[i], &r );
  133. MapWindowPoints( 0, hwnd, (POINT *)&r, 2 );
  134. if (!rect || IntersectRect(&dummy, &r, rect))
  135. SetWindowPos( list[i], 0, r.left + dx, r.top + dy, 0, 0,
  136. SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE |
  137. SWP_NOREDRAW | SWP_DEFERERASE );
  138. }
  139. HeapFree( GetProcessHeap(), 0, list );
  140. }
  141. }
  142. if( flags & (SW_INVALIDATE | SW_ERASE) )
  143. RedrawWindow( hwnd, NULL, hrgnUpdate, rdw_flags |
  144. ((flags & SW_SCROLLCHILDREN) ? RDW_ALLCHILDREN : 0 ) );
  145. if( hwndCaret ) {
  146. SetCaretPos( caretrc.left + dx, caretrc.top + dy );
  147. ShowCaret(hwndCaret);
  148. }
  149. if( bOwnRgn && hrgnUpdate ) DeleteObject( hrgnUpdate );
  150. return retVal;
  151. }
  152. /*************************************************************************
  153. * ScrollWindow (USER32.@)
  154. *
  155. */
  156. BOOL WINAPI ScrollWindow( HWND hwnd, INT dx, INT dy,
  157. const RECT *rect, const RECT *clipRect )
  158. {
  159. return
  160. (ERROR != ScrollWindowEx( hwnd, dx, dy, rect, clipRect, 0, NULL,
  161. (rect ? 0 : SW_SCROLLCHILDREN) |
  162. SW_INVALIDATE ));
  163. }
  164. /*************************************************************************
  165. * ScrollDC (USER32.@)
  166. *
  167. * dx, dy, lprcScroll and lprcClip are all in logical coordinates (msdn is wrong)
  168. * hrgnUpdate is returned in device coordinates with rcUpdate in logical coordinates.
  169. */
  170. BOOL WINAPI ScrollDC( HDC hdc, INT dx, INT dy, const RECT *lprcScroll,
  171. const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate )
  172. {
  173. RECT rSrc, rClipped_src, rClip, rDst, offset;
  174. TRACE( "%p %d,%d hrgnUpdate=%p lprcUpdate = %p\n", hdc, dx, dy, hrgnUpdate, lprcUpdate );
  175. if (lprcClip) TRACE( "lprcClip = %s\n", wine_dbgstr_rect(lprcClip));
  176. if (lprcScroll) TRACE( "lprcScroll = %s\n", wine_dbgstr_rect(lprcScroll));
  177. if (USER_Driver.pScrollDC)
  178. return USER_Driver.pScrollDC( hdc, dx, dy, lprcScroll, lprcClip, hrgnUpdate, lprcUpdate );
  179. /* compute device clipping region (in device coordinates) */
  180. if (lprcScroll) rSrc = *lprcScroll;
  181. else GetClipBox( hdc, &rSrc );
  182. LPtoDP(hdc, (LPPOINT)&rSrc, 2);
  183. if (lprcClip) rClip = *lprcClip;
  184. else GetClipBox( hdc, &rClip );
  185. LPtoDP(hdc, (LPPOINT)&rClip, 2);
  186. IntersectRect( &rClipped_src, &rSrc, &rClip );
  187. TRACE("rSrc %s rClip %s clipped rSrc %s\n", wine_dbgstr_rect(&rSrc),
  188. wine_dbgstr_rect(&rClip), wine_dbgstr_rect(&rClipped_src));
  189. rDst = rClipped_src;
  190. SetRect(&offset, 0, 0, dx, dy);
  191. LPtoDP(hdc, (LPPOINT)&offset, 2);
  192. OffsetRect( &rDst, offset.right - offset.left, offset.bottom - offset.top );
  193. TRACE("rDst before clipping %s\n", wine_dbgstr_rect(&rDst));
  194. IntersectRect( &rDst, &rDst, &rClip );
  195. TRACE("rDst after clipping %s\n", wine_dbgstr_rect(&rDst));
  196. if (!IsRectEmpty(&rDst))
  197. {
  198. /* copy bits */
  199. RECT rDst_lp = rDst, rSrc_lp = rDst;
  200. OffsetRect( &rSrc_lp, offset.left - offset.right, offset.top - offset.bottom );
  201. DPtoLP(hdc, (LPPOINT)&rDst_lp, 2);
  202. DPtoLP(hdc, (LPPOINT)&rSrc_lp, 2);
  203. if (!BitBlt( hdc, rDst_lp.left, rDst_lp.top,
  204. rDst_lp.right - rDst_lp.left, rDst_lp.bottom - rDst_lp.top,
  205. hdc, rSrc_lp.left, rSrc_lp.top, SRCCOPY))
  206. return FALSE;
  207. }
  208. /* compute update areas. This is the clipped source or'ed with the unclipped source translated minus the
  209. clipped src translated (rDst) all clipped to rClip */
  210. if (hrgnUpdate || lprcUpdate)
  211. {
  212. HRGN hrgn = hrgnUpdate, hrgn2;
  213. if (hrgn) SetRectRgn( hrgn, rClipped_src.left, rClipped_src.top, rClipped_src.right, rClipped_src.bottom );
  214. else hrgn = CreateRectRgn( rClipped_src.left, rClipped_src.top, rClipped_src.right, rClipped_src.bottom );
  215. hrgn2 = CreateRectRgnIndirect( &rSrc );
  216. OffsetRgn(hrgn2, offset.right - offset.left, offset.bottom - offset.top );
  217. CombineRgn(hrgn, hrgn, hrgn2, RGN_OR);
  218. SetRectRgn( hrgn2, rDst.left, rDst.top, rDst.right, rDst.bottom );
  219. CombineRgn( hrgn, hrgn, hrgn2, RGN_DIFF );
  220. SetRectRgn( hrgn2, rClip.left, rClip.top, rClip.right, rClip.bottom );
  221. CombineRgn( hrgn, hrgn, hrgn2, RGN_AND );
  222. if( lprcUpdate )
  223. {
  224. GetRgnBox( hrgn, lprcUpdate );
  225. /* Put the lprcUpdate in logical coordinate */
  226. DPtoLP( hdc, (LPPOINT)lprcUpdate, 2 );
  227. TRACE("returning lprcUpdate %s\n", wine_dbgstr_rect(lprcUpdate));
  228. }
  229. if (!hrgnUpdate) DeleteObject( hrgn );
  230. DeleteObject( hrgn2 );
  231. }
  232. return TRUE;
  233. }