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