ScrollHelper.cpp 15 KB

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