-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCViewScrollView.cpp
375 lines (305 loc) · 7.2 KB
/
CViewScrollView.cpp
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
// CViewScrollView.cpp : implementation of the CCViewScrollView class
//
#include "stdafx.h"
#include "CViewScrollView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCViewScrollView
IMPLEMENT_DYNCREATE(CCViewScrollView, CView)
BEGIN_MESSAGE_MAP(CCViewScrollView, CView)
//{{AFX_MSG_MAP(CCViewScrollView)
ON_WM_VSCROLL()
ON_WM_HSCROLL()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCViewScrollView construction/destruction
CCViewScrollView::CCViewScrollView()
{
m_amt_page = 0;
m_amt_total = 0;
m_font_height = 0;
m_greatest_width = 0;
m_client_screen_width = 0;
}
CCViewScrollView::~CCViewScrollView()
{
}
BOOL CCViewScrollView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCViewScrollView drawing
void CCViewScrollView::OnDraw(CDC* pDC)
{
CFont thin_font;
thin_font.CreatePointFont(100, "Courier", pDC);
pDC->SelectObject(&thin_font);
//Get the text ready.
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
int charheight = tm.tmHeight + tm.tmExternalLeading;
int line_number = 0;
CString to_view;
to_view.Format("The position of the vertical scroll bar: %d", GetScrollPos(SB_VERT));
int number_of_lines = 20;
m_greatest_width = 0;
for (int loop = 0; loop < number_of_lines; loop++)
{
CString number;
number.Format("%d ", loop);
CSize text_dimensions = pDC->GetTextExtent(number + to_view);
m_greatest_width = max(m_greatest_width, 0/*x offset*/ + text_dimensions.cx);
TextOut(pDC, 0, line_number++, charheight, number + to_view);
}
//If area size doesn't change then this doesn't need to be called.
m_amt_total = number_of_lines;
m_font_height = charheight;
fix_scrollbars();
}
/////////////////////////////////////////////////////////////////////////////
// CCViewScrollView printing
BOOL CCViewScrollView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCViewScrollView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCViewScrollView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCViewScrollView diagnostics
#ifdef _DEBUG
void CCViewScrollView::AssertValid() const
{
CView::AssertValid();
}
void CCViewScrollView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCViewScrollView message handlers
BOOL CCViewScrollView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
{
int old_y, y;
old_y = GetScrollPos(SB_VERT);
y = old_y;
switch(LOBYTE(nScrollCode))
{
case SB_TOP: //Scroll to top.
{
y = 0;
break;
}
case SB_BOTTOM:
{
y = INT_MAX;
break;
}
case SB_LINEDOWN: //Scroll one line down.
{
y++;
break;
}
case SB_LINEUP: //Scroll one line up.
{
y--;
break;
}
case SB_PAGEDOWN: //Scroll one page down.
{
y += m_amt_page;
break;
}
case SB_PAGEUP: //Scroll one page up.
{
y -= m_amt_page;
break;
}
case SB_THUMBTRACK: //Drag scroll box to specified position. The current position is provided in nPos.
{
y = nPos;
break;
}
}
int old_x, x;
old_x = GetScrollPos(SB_HORZ);
x = old_x;
switch(HIBYTE(nScrollCode))
{
case SB_TOP: //Scroll to top.
{
x = 0;
break;
}
case SB_BOTTOM:
{
x = INT_MAX;
break;
}
case SB_LINEDOWN: //Scroll one line down.
{
x++;
break;
}
case SB_LINEUP: //Scroll one line up.
{
x--;
break;
}
case SB_PAGEDOWN: //Scroll one page down.
{
x += m_client_screen_width;
break;
}
case SB_PAGEUP: //Scroll one page up.
{
x -= m_client_screen_width;
break;
}
case SB_THUMBTRACK: //Drag scroll box to specified position. The current position is provided in nPos.
{
x = nPos;
break;
}
}
bool bresult;
//Take care of the necessary scrolling.
if (OnScrollBy(CSize(x - old_x, y - old_y), bDoScroll))
{
bresult = true;
}
else
{
bresult = false;
}
if (bresult && bDoScroll)
{
Invalidate(true);
}
return(false);
}
void CCViewScrollView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if (pScrollBar != NULL && pScrollBar->SendChildNotifyLastMsg())
{
//Unknown effect. Research this later.
return;
}
if (pScrollBar != GetScrollBarCtrl(SB_VERT))
{
//This isn't the correct scroll bar..
return;
}
OnScroll(MAKEWORD(nSBCode, -1), nPos);
}
void CCViewScrollView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if (pScrollBar != NULL && pScrollBar->SendChildNotifyLastMsg())
{
//Unknown effect. Research this later.
return;
}
if (pScrollBar != GetScrollBarCtrl(SB_HORZ))
{
//This isn't the correct scroll bar..
return;
}
OnScroll(MAKEWORD(-1, nSBCode), nPos);
}
BOOL CCViewScrollView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)
{
int y2, y = GetScrollPos(SB_VERT);
y2 = y;
if (sizeScroll.cy > 0)
{
y2 = min(GetScrollLimit(SB_VERT), y2 + sizeScroll.cy);
}
else
{
y2 = max(0, y2 + sizeScroll.cy);
}
int x2, x = GetScrollPos(SB_HORZ);
x2 = x;
if (sizeScroll.cx > 0)
{
x2 = min(GetScrollLimit(SB_HORZ), x2 + sizeScroll.cx);
}
else
{
x2 = max(0, x2 + sizeScroll.cx);
}
if (y == y2 && x == x2)
{
//There were no changes.
return(false);
}
SetScrollPos(SB_VERT, y2);
SetScrollPos(SB_HORZ, x2);
return(true);
}
void CCViewScrollView::TextOut(CDC* pDC, int x, int line_number, int charheight, CString to_view)
{
if (GetScrollPos(SB_VERT) <= line_number && GetScrollPos(SB_VERT) + m_amt_page + 1 > line_number)
{
pDC->TextOut(x - GetScrollPos(SB_HORZ), (line_number - GetScrollPos(SB_VERT)) * charheight, to_view);
}
}
void CCViewScrollView::OnInitialUpdate()
{
CView::OnInitialUpdate();
//EnableScrollBar(SB_BOTH, ESB_ENABLE_BOTH);
SetScrollPos(SB_HORZ, 0);
SetScrollPos(SB_VERT, 0);
fix_scrollbars();
}
void CCViewScrollView::fix_scrollbars()
{
//EnableScrollBarCtrl(SB_BOTH, true);
//EnableScrollBarCtrl(SB_VERT, true);
RECT screen_rect;
GetClientRect(&screen_rect);//Should use one that counts scroll bars.
//How many lines can fit in one screen?
if (m_font_height == 0 || screen_rect.bottom == 0)
{
m_amt_page = 0;
}
else
{
m_amt_page = screen_rect.bottom / m_font_height;
}
SCROLLINFO info;
info.fMask = SIF_PAGE | SIF_RANGE;
info.nMin = 0;
info.nMax = m_amt_total;
info.nPage = m_amt_page;
if (!SetScrollInfo(SB_VERT, &info, true))
{
SetScrollRange(SB_VERT, info.nMin, info.nMax, true);
}
//This m_client_screen_width, is only used for horz scrolling..
m_client_screen_width = screen_rect.right;
info.nMin = 0;
info.nMax = m_greatest_width;
info.nPage = screen_rect.right;
if (!SetScrollInfo(SB_HORZ, &info, true))
{
SetScrollRange(SB_HORZ, info.nMin, info.nMax, true);
}
}