ScrollHelper.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  207. {
  208. if ( m_attachWnd == NULL )
  209. return FALSE;
  210. // Don't do anything if the vertical scrollbar is not enabled.
  211. int scrollMin = 0, scrollMax = 0;
  212. m_attachWnd->GetScrollRange(SB_VERT, &scrollMin, &scrollMax);
  213. if ( scrollMin == scrollMax )
  214. return FALSE;
  215. // Compute the number of scrolling increments requested.
  216. int numScrollIncrements = abs(zDelta) / WHEEL_DELTA;
  217. // Each scrolling increment corresponds to a certain number of
  218. // scroll lines (one scroll line is like a SB_LINEUP or SB_LINEDOWN).
  219. // We need to query the system parameters for this value.
  220. int numScrollLinesPerIncrement = 0;
  221. ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &numScrollLinesPerIncrement, 0);
  222. // Check if a page scroll was requested.
  223. if ( numScrollLinesPerIncrement == WHEEL_PAGESCROLL )
  224. {
  225. // Call the vscroll message handler to do the work.
  226. OnVScroll(zDelta > 0 ? SB_PAGEUP : SB_PAGEDOWN, 0, NULL);
  227. return TRUE;
  228. }
  229. // Compute total number of lines to scroll.
  230. int numScrollLines = numScrollIncrements * numScrollLinesPerIncrement;
  231. // Adjust numScrollLines to slow down the scrolling a bit more.
  232. numScrollLines = max(numScrollLines/3, 1);
  233. // Do the scrolling.
  234. for(int i = 0; i < numScrollLines; ++i)
  235. {
  236. // Call the vscroll message handler to do the work.
  237. OnVScroll(zDelta > 0 ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
  238. }
  239. return TRUE;
  240. }
  241. BOOL CScrollHelper::OnMouseHWheel(UINT nFlags, short zDelta, CPoint pt)
  242. {
  243. if (m_attachWnd == NULL)
  244. return FALSE;
  245. // Don't do anything if the vertical scrollbar is not enabled.
  246. int scrollMin = 0, scrollMax = 0;
  247. m_attachWnd->GetScrollRange(SB_VERT, &scrollMin, &scrollMax);
  248. if (scrollMin == scrollMax)
  249. return FALSE;
  250. // Compute the number of scrolling increments requested.
  251. int numScrollIncrements = abs(zDelta) / WHEEL_DELTA;
  252. // Each scrolling increment corresponds to a certain number of
  253. // scroll lines (one scroll line is like a SB_LINEUP or SB_LINEDOWN).
  254. // We need to query the system parameters for this value.
  255. int numScrollLinesPerIncrement = 0;
  256. ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &numScrollLinesPerIncrement, 0);
  257. // Check if a page scroll was requested.
  258. if (numScrollLinesPerIncrement == WHEEL_PAGESCROLL)
  259. {
  260. // Call the vscroll message handler to do the work.
  261. OnHScroll(zDelta > 0 ? SB_PAGEUP : SB_PAGEDOWN, 0, NULL);
  262. return TRUE;
  263. }
  264. // Compute total number of lines to scroll.
  265. int numScrollLines = numScrollIncrements * numScrollLinesPerIncrement;
  266. // Adjust numScrollLines to slow down the scrolling a bit more.
  267. numScrollLines = max(numScrollLines / 3, 1);
  268. // Do the scrolling.
  269. for (int i = 0; i < numScrollLines; ++i)
  270. {
  271. // Call the vscroll message handler to do the work.
  272. OnHScroll(zDelta > 0 ? SB_LINEUP : SB_LINEDOWN, 0, NULL);
  273. }
  274. return TRUE;
  275. }
  276. void CScrollHelper::OnSize(UINT nType, int cx, int cy)
  277. {
  278. UpdateScrollInfo();
  279. }
  280. int CScrollHelper::Get32BitScrollPos(int bar, CScrollBar* pScrollBar)
  281. {
  282. // Code below is from MSDN Article ID 152252, "How To Get
  283. // 32-bit Scroll Position During Scroll Messages".
  284. // First determine if the user scrolled a scroll bar control
  285. // on the window or scrolled the window itself.
  286. ASSERT( m_attachWnd != NULL );
  287. HWND hWndScroll;
  288. if ( pScrollBar == NULL )
  289. hWndScroll = m_attachWnd->m_hWnd;
  290. else
  291. hWndScroll = pScrollBar->m_hWnd;
  292. SCROLLINFO si;
  293. si.cbSize = sizeof(SCROLLINFO);
  294. si.fMask = SIF_TRACKPOS;
  295. ::GetScrollInfo(hWndScroll, bar, &si);
  296. int scrollPos = si.nTrackPos;
  297. return scrollPos;
  298. }
  299. void CScrollHelper::UpdateScrollInfo()
  300. {
  301. if ( m_attachWnd == NULL )
  302. return;
  303. // Get the width/height of the attached wnd that includes the area
  304. // covered by the scrollbars (if any). The reason we need this is
  305. // because when scrollbars are present, both cx/cy and GetClientRect()
  306. // when accessed from OnSize() do not include the scrollbar covered
  307. // areas. In other words, their values are smaller than what you would
  308. // expect.
  309. CRect rect;
  310. GetClientRectSB(m_attachWnd, rect);
  311. CSize windowSize(rect.Width(), rect.Height());
  312. // Update horizontal scrollbar.
  313. CSize deltaPos(0,0);
  314. UpdateScrollBar(SB_HORZ, windowSize.cx, m_displaySize.cx,
  315. m_pageSize.cx, m_scrollPos.cx, deltaPos.cx);
  316. // Update vertical scrollbar.
  317. UpdateScrollBar(SB_VERT, windowSize.cy, m_displaySize.cy,
  318. m_pageSize.cy, m_scrollPos.cy, deltaPos.cy);
  319. // See if we need to scroll the window back in place.
  320. // This is needed to handle the case where the scrollbar is
  321. // moved all the way to the right for example, and controls
  322. // at the left side disappear from the view. Then the user
  323. // resizes the window wider until scrollbars disappear. Without
  324. // this code below, the controls off the page will be gone forever.
  325. if ( deltaPos.cx != 0 || deltaPos.cy != 0 )
  326. {
  327. m_attachWnd->ScrollWindow(deltaPos.cx, deltaPos.cy);
  328. }
  329. }
  330. void CScrollHelper::UpdateScrollBar(int bar, int windowSize, int displaySize,
  331. LONG& pageSize, LONG& scrollPos, LONG& deltaPos)
  332. {
  333. int scrollMax = 0;
  334. deltaPos = 0;
  335. if ( windowSize < displaySize )
  336. {
  337. scrollMax = displaySize - 1;
  338. if ( pageSize > 0 && scrollPos > 0 )
  339. {
  340. // Adjust the scroll position when the window size is changed.
  341. scrollPos = (LONG)(1.0 * scrollPos * windowSize / pageSize);
  342. }
  343. pageSize = windowSize;
  344. scrollPos = min(scrollPos, displaySize - pageSize - 1);
  345. deltaPos = m_attachWnd->GetScrollPos(bar) - scrollPos;
  346. }
  347. else
  348. {
  349. // Force the scrollbar to go away.
  350. pageSize = 0;
  351. scrollPos = 0;
  352. deltaPos = m_attachWnd->GetScrollPos(bar);
  353. }
  354. SCROLLINFO si;
  355. memset(&si, 0, sizeof(SCROLLINFO));
  356. si.cbSize = sizeof(SCROLLINFO);
  357. si.fMask = SIF_ALL; // SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS;
  358. si.nMin = 0;
  359. si.nMax = scrollMax;
  360. si.nPage = pageSize;
  361. si.nPos = scrollPos;
  362. m_attachWnd->SetScrollInfo(bar, &si, TRUE);
  363. }
  364. // END