ScrollHelper.cpp 14 KB

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