ScrollHelper.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // Filename: ScrollHelper.cpp
  2. // 2005-07-01 nschan Initial revision.
  3. // 2005-09-08 nschan Added GetClientRectSB() function.
  4. #include "stdafx.h"
  5. #include "ScrollHelper.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. // Helper function to get client rect with possible
  12. // modification by adding scrollbar width/height.
  13. static void GetClientRectSB(CWnd* pWnd, CRect& rect)
  14. {
  15. ASSERT( pWnd != NULL );
  16. CRect winRect;
  17. pWnd->GetWindowRect(&winRect);
  18. pWnd->ScreenToClient(&winRect);
  19. pWnd->GetClientRect(&rect);
  20. int cxSB = ::GetSystemMetrics(SM_CXVSCROLL);
  21. int cySB = ::GetSystemMetrics(SM_CYHSCROLL);
  22. if ( winRect.right >= (rect.right + cxSB) )
  23. rect.right += cxSB;
  24. if ( winRect.bottom >= (rect.bottom + cySB) )
  25. rect.bottom += cySB;
  26. }
  27. // CScrollHelper /////////////////////////////////////////////////////////////////////
  28. CScrollHelper::CScrollHelper()
  29. {
  30. m_attachWnd = NULL;
  31. m_pageSize = CSize(0,0);
  32. m_displaySize = CSize(0,0);
  33. m_scrollPos = CSize(0,0);
  34. }
  35. CScrollHelper::~CScrollHelper()
  36. {
  37. DetachWnd();
  38. }
  39. void CScrollHelper::AttachWnd(CWnd* pWnd)
  40. {
  41. m_attachWnd = pWnd;
  42. }
  43. void CScrollHelper::DetachWnd()
  44. {
  45. m_attachWnd = NULL;
  46. }
  47. void CScrollHelper::SetDisplaySize(int displayWidth, int displayHeight)
  48. {
  49. int cxSB = ::GetSystemMetrics(SM_CXVSCROLL);
  50. int cySB = ::GetSystemMetrics(SM_CYHSCROLL);
  51. m_displaySize = CSize(displayWidth + cxSB, displayHeight + cySB);
  52. if ( m_attachWnd != NULL && ::IsWindow(m_attachWnd->m_hWnd) )
  53. UpdateScrollInfo();
  54. }
  55. const CSize& CScrollHelper::GetDisplaySize() const
  56. {
  57. return m_displaySize;
  58. }
  59. const CSize& CScrollHelper::GetScrollPos() const
  60. {
  61. return m_scrollPos;
  62. }
  63. const CSize& CScrollHelper::GetPageSize() const
  64. {
  65. return m_pageSize;
  66. }
  67. void CScrollHelper::ScrollToOrigin(bool scrollLeft, bool scrollTop)
  68. {
  69. if ( m_attachWnd == NULL )
  70. return;
  71. if ( scrollLeft )
  72. {
  73. if ( m_displaySize.cx > 0 && m_pageSize.cx > 0 && m_scrollPos.cx > 0 )
  74. {
  75. int deltaPos = -m_scrollPos.cx;
  76. m_scrollPos.cx += deltaPos;
  77. m_attachWnd->SetScrollPos(SB_HORZ, m_scrollPos.cx, TRUE);
  78. m_attachWnd->ScrollWindow(-deltaPos, 0);
  79. }
  80. }
  81. if ( scrollTop )
  82. {
  83. if ( m_displaySize.cy > 0 && m_pageSize.cy > 0 && m_scrollPos.cy > 0 )
  84. {
  85. int deltaPos = -m_scrollPos.cy;
  86. m_scrollPos.cy += deltaPos;
  87. m_attachWnd->SetScrollPos(SB_VERT, m_scrollPos.cy, TRUE);
  88. m_attachWnd->ScrollWindow(0, -deltaPos);
  89. }
  90. }
  91. }
  92. void CScrollHelper::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  93. {
  94. if ( m_attachWnd == NULL )
  95. return;
  96. const int lineOffset = 60;
  97. // Compute the desired change or delta in scroll position.
  98. int deltaPos = 0;
  99. switch( nSBCode )
  100. {
  101. case SB_LINELEFT:
  102. // Left scroll arrow was pressed.
  103. deltaPos = -lineOffset;
  104. break;
  105. case SB_LINERIGHT:
  106. // Right scroll arrow was pressed.
  107. deltaPos = lineOffset;
  108. break;
  109. case SB_PAGELEFT:
  110. // User clicked inbetween left arrow and thumb.
  111. deltaPos = -m_pageSize.cx;
  112. break;
  113. case SB_PAGERIGHT:
  114. // User clicked inbetween thumb and right arrow.
  115. deltaPos = m_pageSize.cx;
  116. break;
  117. case SB_THUMBTRACK:
  118. // Scrollbar thumb is being dragged.
  119. deltaPos = Get32BitScrollPos(SB_HORZ, pScrollBar) - m_scrollPos.cx;
  120. break;
  121. case SB_THUMBPOSITION:
  122. // Scrollbar thumb was released.
  123. deltaPos = Get32BitScrollPos(SB_HORZ, pScrollBar) - m_scrollPos.cx;
  124. break;
  125. default:
  126. // We don't process other scrollbar messages.
  127. return;
  128. }
  129. // Compute the new scroll position.
  130. int newScrollPos = m_scrollPos.cx + deltaPos;
  131. // If the new scroll position is negative, we adjust
  132. // deltaPos in order to scroll the window back to origin.
  133. if ( newScrollPos < 0 )
  134. deltaPos = -m_scrollPos.cx;
  135. // If the new scroll position is greater than the max scroll position,
  136. // we adjust deltaPos in order to scroll the window precisely to the
  137. // maximum position.
  138. int maxScrollPos = m_displaySize.cx - m_pageSize.cx;
  139. if ( newScrollPos > maxScrollPos )
  140. deltaPos = maxScrollPos - m_scrollPos.cx;
  141. // Scroll the window if needed.
  142. if ( deltaPos != 0 )
  143. {
  144. m_scrollPos.cx += deltaPos;
  145. m_attachWnd->SetScrollPos(SB_HORZ, m_scrollPos.cx, TRUE);
  146. m_attachWnd->ScrollWindow(-deltaPos, 0);
  147. }
  148. }
  149. void CScrollHelper::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  150. {
  151. if ( m_attachWnd == NULL )
  152. return;
  153. const int lineOffset = 60;
  154. // Compute the desired change or delta in scroll position.
  155. int deltaPos = 0;
  156. switch( nSBCode )
  157. {
  158. case SB_LINEUP:
  159. // Up arrow button on scrollbar was pressed.
  160. deltaPos = -lineOffset;
  161. break;
  162. case SB_LINEDOWN:
  163. // Down arrow button on scrollbar was pressed.
  164. deltaPos = lineOffset;
  165. break;
  166. case SB_PAGEUP:
  167. // User clicked inbetween up arrow and thumb.
  168. deltaPos = -m_pageSize.cy;
  169. break;
  170. case SB_PAGEDOWN:
  171. // User clicked inbetween thumb and down arrow.
  172. deltaPos = m_pageSize.cy;
  173. break;
  174. case SB_THUMBTRACK:
  175. // Scrollbar thumb is being dragged.
  176. deltaPos = Get32BitScrollPos(SB_VERT, pScrollBar) - m_scrollPos.cy;
  177. break;
  178. case SB_THUMBPOSITION:
  179. // Scrollbar thumb was released.
  180. deltaPos = Get32BitScrollPos(SB_VERT, pScrollBar) - m_scrollPos.cy;
  181. break;
  182. default:
  183. // We don't process other scrollbar messages.
  184. return;
  185. }
  186. // Compute the new scroll position.
  187. int newScrollPos = m_scrollPos.cy + deltaPos;
  188. // If the new scroll position is negative, we adjust
  189. // deltaPos in order to scroll the window back to origin.
  190. if ( newScrollPos < 0 )
  191. deltaPos = -m_scrollPos.cy;
  192. // If the new scroll position is greater than the max scroll position,
  193. // we adjust deltaPos in order to scroll the window precisely to the
  194. // maximum position.
  195. int maxScrollPos = m_displaySize.cy - m_pageSize.cy;
  196. if ( newScrollPos > maxScrollPos )
  197. deltaPos = maxScrollPos - m_scrollPos.cy;
  198. // Scroll the window if needed.
  199. if ( deltaPos != 0 )
  200. {
  201. m_scrollPos.cy += deltaPos;
  202. m_attachWnd->SetScrollPos(SB_VERT, m_scrollPos.cy, TRUE);
  203. m_attachWnd->ScrollWindow(0, -deltaPos);
  204. }
  205. }
  206. BOOL CScrollHelper::Update(CPoint changes)
  207. {
  208. int deltaYPos = changes.y;
  209. int newYScrollPos = m_scrollPos.cy + deltaYPos;
  210. // If the new scroll position is negative, we adjust
  211. // deltaPos in order to scroll the window back to origin.
  212. if (newYScrollPos < 0)
  213. deltaYPos = -m_scrollPos.cy;
  214. // If the new scroll position is greater than the max scroll position,
  215. // we adjust deltaPos in order to scroll the window precisely to the
  216. // maximum position.
  217. int maxYScrollPos = m_displaySize.cy - m_pageSize.cy;
  218. if (newYScrollPos > maxYScrollPos)
  219. deltaYPos = maxYScrollPos - m_scrollPos.cy;
  220. if (changes.y != 0)
  221. {
  222. m_scrollPos.cy += deltaYPos;
  223. m_attachWnd->SetScrollPos(SB_VERT, m_scrollPos.cy, TRUE);
  224. }
  225. int deltaXPos = changes.x;
  226. int newXScrollPos = m_scrollPos.cx + deltaXPos;
  227. // If the new scroll position is negative, we adjust
  228. // deltaPos in order to scroll the window back to origin.
  229. if (newXScrollPos < 0)
  230. deltaXPos = -m_scrollPos.cx;
  231. // If the new scroll position is greater than the max scroll position,
  232. // we adjust deltaPos in order to scroll the window precisely to the
  233. // maximum position.
  234. int maxXScrollPos = m_displaySize.cx - m_pageSize.cx;
  235. if (newXScrollPos > maxXScrollPos)
  236. deltaXPos = maxXScrollPos - m_scrollPos.cx;
  237. if (changes.x != 0)
  238. {
  239. m_scrollPos.cx += deltaXPos;
  240. m_attachWnd->SetScrollPos(SB_HORZ, m_scrollPos.cx, TRUE);
  241. }
  242. m_attachWnd->ScrollWindow(-deltaXPos, -deltaYPos);
  243. return 1;
  244. }
  245. BOOL CScrollHelper::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  246. {
  247. if ( m_attachWnd == NULL )
  248. return FALSE;
  249. // Don't do anything if the vertical scrollbar is not enabled.
  250. int scrollMin = 0, scrollMax = 0;
  251. m_attachWnd->GetScrollRange(SB_VERT, &scrollMin, &scrollMax);
  252. if ( scrollMin == scrollMax )
  253. return FALSE;
  254. // Compute the number of scrolling increments requested.
  255. int numScrollIncrements = abs(zDelta) / WHEEL_DELTA;
  256. // Each scrolling increment corresponds to a certain number of
  257. // scroll lines (one scroll line is like a SB_LINEUP or SB_LINEDOWN).
  258. // We need to query the system parameters for this value.
  259. int numScrollLinesPerIncrement = 0;
  260. ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &numScrollLinesPerIncrement, 0);
  261. // Check if a page scroll was requested.
  262. if ( numScrollLinesPerIncrement == WHEEL_PAGESCROLL )
  263. {
  264. // Call the vscroll message handler to do the work.
  265. OnVScroll(zDelta > 0 ? SB_PAGEUP : SB_PAGEDOWN, 0, NULL);
  266. return TRUE;
  267. }
  268. // Compute total number of lines to scroll.
  269. int numScrollLines = numScrollIncrements * numScrollLinesPerIncrement;
  270. // Adjust numScrollLines to slow down the scrolling a bit more.
  271. numScrollLines = max(numScrollLines/3, 1);
  272. // Do the scrolling.
  273. for(int i = 0; i < numScrollLines; ++i)
  274. {
  275. // Call the vscroll message handler to do the work.
  276. OnVScroll(zDelta > 0 ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
  277. }
  278. return TRUE;
  279. }
  280. BOOL CScrollHelper::OnMouseHWheel(UINT nFlags, short zDelta, CPoint pt)
  281. {
  282. if (m_attachWnd == NULL)
  283. return FALSE;
  284. // Don't do anything if the vertical scrollbar is not enabled.
  285. int scrollMin = 0, scrollMax = 0;
  286. m_attachWnd->GetScrollRange(SB_VERT, &scrollMin, &scrollMax);
  287. if (scrollMin == scrollMax)
  288. return FALSE;
  289. // Compute the number of scrolling increments requested.
  290. int numScrollIncrements = abs(zDelta) / WHEEL_DELTA;
  291. // Each scrolling increment corresponds to a certain number of
  292. // scroll lines (one scroll line is like a SB_LINEUP or SB_LINEDOWN).
  293. // We need to query the system parameters for this value.
  294. int numScrollLinesPerIncrement = 0;
  295. ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &numScrollLinesPerIncrement, 0);
  296. // Check if a page scroll was requested.
  297. if (numScrollLinesPerIncrement == WHEEL_PAGESCROLL)
  298. {
  299. // Call the vscroll message handler to do the work.
  300. OnHScroll(zDelta > 0 ? SB_PAGEUP : SB_PAGEDOWN, 0, NULL);
  301. return TRUE;
  302. }
  303. // Compute total number of lines to scroll.
  304. int numScrollLines = numScrollIncrements * numScrollLinesPerIncrement;
  305. // Adjust numScrollLines to slow down the scrolling a bit more.
  306. numScrollLines = max(numScrollLines / 3, 1);
  307. // Do the scrolling.
  308. for (int i = 0; i < numScrollLines; ++i)
  309. {
  310. // Call the vscroll message handler to do the work.
  311. OnHScroll(zDelta > 0 ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
  312. }
  313. return TRUE;
  314. }
  315. void CScrollHelper::OnSize(UINT nType, int cx, int cy)
  316. {
  317. UpdateScrollInfo();
  318. }
  319. int CScrollHelper::Get32BitScrollPos(int bar, CScrollBar* pScrollBar)
  320. {
  321. // Code below is from MSDN Article ID 152252, "How To Get
  322. // 32-bit Scroll Position During Scroll Messages".
  323. // First determine if the user scrolled a scroll bar control
  324. // on the window or scrolled the window itself.
  325. ASSERT( m_attachWnd != NULL );
  326. HWND hWndScroll;
  327. if ( pScrollBar == NULL )
  328. hWndScroll = m_attachWnd->m_hWnd;
  329. else
  330. hWndScroll = pScrollBar->m_hWnd;
  331. SCROLLINFO si;
  332. si.cbSize = sizeof(SCROLLINFO);
  333. si.fMask = SIF_TRACKPOS;
  334. ::GetScrollInfo(hWndScroll, bar, &si);
  335. int scrollPos = si.nTrackPos;
  336. return scrollPos;
  337. }
  338. void CScrollHelper::UpdateScrollInfo()
  339. {
  340. if ( m_attachWnd == NULL )
  341. return;
  342. // Get the width/height of the attached wnd that includes the area
  343. // covered by the scrollbars (if any). The reason we need this is
  344. // because when scrollbars are present, both cx/cy and GetClientRect()
  345. // when accessed from OnSize() do not include the scrollbar covered
  346. // areas. In other words, their values are smaller than what you would
  347. // expect.
  348. CRect rect;
  349. GetClientRectSB(m_attachWnd, rect);
  350. CSize windowSize(rect.Width(), rect.Height());
  351. // Update horizontal scrollbar.
  352. CSize deltaPos(0,0);
  353. UpdateScrollBar(SB_HORZ, windowSize.cx, m_displaySize.cx,
  354. m_pageSize.cx, m_scrollPos.cx, deltaPos.cx);
  355. // Update vertical scrollbar.
  356. UpdateScrollBar(SB_VERT, windowSize.cy, m_displaySize.cy,
  357. m_pageSize.cy, m_scrollPos.cy, deltaPos.cy);
  358. // See if we need to scroll the window back in place.
  359. // This is needed to handle the case where the scrollbar is
  360. // moved all the way to the right for example, and controls
  361. // at the left side disappear from the view. Then the user
  362. // resizes the window wider until scrollbars disappear. Without
  363. // this code below, the controls off the page will be gone forever.
  364. if ( deltaPos.cx != 0 || deltaPos.cy != 0 )
  365. {
  366. m_attachWnd->ScrollWindow(deltaPos.cx, deltaPos.cy);
  367. }
  368. }
  369. void CScrollHelper::UpdateScrollBar(int bar, int windowSize, int displaySize,
  370. LONG& pageSize, LONG& scrollPos, LONG& deltaPos)
  371. {
  372. int scrollMax = 0;
  373. deltaPos = 0;
  374. if ( windowSize < displaySize )
  375. {
  376. scrollMax = displaySize - 1;
  377. if ( pageSize > 0 && scrollPos > 0 )
  378. {
  379. // Adjust the scroll position when the window size is changed.
  380. scrollPos = (LONG)(1.0 * scrollPos * windowSize / pageSize);
  381. }
  382. pageSize = windowSize;
  383. scrollPos = min(scrollPos, displaySize - pageSize - 1);
  384. deltaPos = m_attachWnd->GetScrollPos(bar) - scrollPos;
  385. }
  386. else
  387. {
  388. // Force the scrollbar to go away.
  389. pageSize = 0;
  390. scrollPos = 0;
  391. deltaPos = m_attachWnd->GetScrollPos(bar);
  392. }
  393. SCROLLINFO si;
  394. memset(&si, 0, sizeof(SCROLLINFO));
  395. si.cbSize = sizeof(SCROLLINFO);
  396. si.fMask = SIF_ALL; // SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS;
  397. si.nMin = 0;
  398. si.nMax = scrollMax;
  399. si.nPage = pageSize;
  400. si.nPos = scrollPos;
  401. m_attachWnd->SetScrollInfo(bar, &si, TRUE);
  402. }
  403. // END