-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MScrollView.cpp
394 lines (335 loc) · 10.5 KB
/
MScrollView.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
////////////////////////////////////////////////////////////////////////////
// MScrollView.cpp -- MZC3 scroll view
// This file is part of MZC3. See file "ReadMe.txt" and "License.txt".
////////////////////////////////////////////////////////////////////////////
#define NOMINMAX
#include "MScrollView.hpp"
////////////////////////////////////////////////////////////////////////////
void MScrollView::SetCtrlInfo(HWND hwndCtrl, const MRect& rcCtrl)
{
assert(::IsWindow(hwndCtrl));
assert(HasChildStyle(hwndCtrl));
assert(rcCtrl.left >= 0);
assert(rcCtrl.top >= 0);
MScrollCtrlInfo* info = FindCtrlInfo(hwndCtrl);
if (info)
info->m_rcCtrl = rcCtrl;
else
AddCtrlInfo(hwndCtrl, rcCtrl);
}
void MScrollView::SetCtrlInfo(
HWND hwndCtrl, const MPoint& ptCtrl, const MSize& sizCtrl)
{
assert(::IsWindow(hwndCtrl));
assert(HasChildStyle(hwndCtrl));
assert(ptCtrl.x >= 0);
assert(ptCtrl.y >= 0);
MScrollCtrlInfo* info = FindCtrlInfo(hwndCtrl);
if (info)
info->m_rcCtrl = MRect(ptCtrl, sizCtrl);
else
AddCtrlInfo(hwndCtrl, ptCtrl, sizCtrl);
}
MScrollCtrlInfo* MScrollView::FindCtrlInfo(HWND hwndCtrl) noexcept
{
assert(::IsWindow(hwndCtrl));
assert(HasChildStyle(hwndCtrl));
const size_t siz = size();
for (size_t i = 0; i < siz; ++i)
{
if (m_vecInfo[i].m_hwndCtrl == hwndCtrl)
return &m_vecInfo[i];
}
return nullptr;
}
const MScrollCtrlInfo* MScrollView::FindCtrlInfo(HWND hwndCtrl) const noexcept
{
assert(::IsWindow(hwndCtrl));
assert(HasChildStyle(hwndCtrl));
const size_t siz = size();
for (size_t i = 0; i < siz; ++i)
{
if (m_vecInfo[i].m_hwndCtrl == hwndCtrl)
return &m_vecInfo[i];
}
return nullptr;
}
void MScrollView::RemoveCtrlInfo(HWND hwndCtrl) noexcept
{
assert(::IsWindow(hwndCtrl));
assert(HasChildStyle(hwndCtrl));
std::vector<MScrollCtrlInfo>::iterator it, end = m_vecInfo.end();
for (it = m_vecInfo.begin(); it != end; ++it)
{
if (it->m_hwndCtrl == hwndCtrl)
{
m_vecInfo.erase(it);
break;
}
}
}
// ensure visible
void MScrollView::EnsureCtrlVisible(HWND hwndCtrl, bool update_all/* = true*/) noexcept
{
MRect rcClient;
GetClientRect(m_hwndParent, &rcClient);
const int yScroll = ::GetScrollPos(m_hwndParent, SB_VERT);
const int siz = static_cast<int>(size());
for (int i = 0; i < siz; ++i)
{
if (m_vecInfo[i].m_hwndCtrl != hwndCtrl)
continue;
if (!::IsWindow(hwndCtrl))
break;
const MRect& rcCtrl = m_vecInfo[i].m_rcCtrl;
if (rcCtrl.bottom > yScroll + rcClient.Height())
{
Scroll(SB_VERT, SB_THUMBPOSITION, rcCtrl.bottom - rcClient.Height());
}
if (rcCtrl.top < yScroll)
{
Scroll(SB_VERT, SB_THUMBPOSITION, rcCtrl.top);
}
break;
}
if (update_all)
UpdateAll();
}
void MScrollView::SetExtentForAllCtrls(INT cxExtra, INT cyExtra) noexcept
{
Extent().cx = Extent().cy = 0;
const int siz = static_cast<int>(size());
for (int i = 0; i < siz; ++i)
{
if (!::IsWindowVisible(m_vecInfo[i].m_hwndCtrl))
continue;
const MRect& rcCtrl = m_vecInfo[i].m_rcCtrl;
if (Extent().cx < rcCtrl.right - 1)
Extent().cx = rcCtrl.right - 1;
if (Extent().cy < rcCtrl.bottom - 1)
Extent().cy = rcCtrl.bottom - 1;
}
Extent().cx += cxExtra;
Extent().cy += cyExtra;
MRect rc;
::GetClientRect(m_hwndParent, &rc);
SCROLLINFO si = { sizeof(si) };
si.fMask = SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = Extent().cx;
si.nPage = rc.Width();
::SetScrollInfo(m_hwndParent, SB_HORZ, &si, TRUE);
si.fMask = SIF_PAGE | SIF_RANGE | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = Extent().cy;
si.nPage = rc.Height();
::SetScrollInfo(m_hwndParent, SB_VERT, &si, TRUE);
::InvalidateRect(m_hwndParent, nullptr, TRUE);
}
void MScrollView::UpdateCtrlsPos() noexcept
{
const int xScroll = ::GetScrollPos(m_hwndParent, SB_HORZ);
const int yScroll = ::GetScrollPos(m_hwndParent, SB_VERT);
const int siz = static_cast<int>(size());
HDWP hDWP = ::BeginDeferWindowPos(siz);
if (hDWP)
{
for (int i = 0; i < siz; ++i)
{
const MRect& rcCtrl = m_vecInfo[i].m_rcCtrl;
hDWP = ::DeferWindowPos(hDWP, m_vecInfo[i].m_hwndCtrl,
nullptr,
rcCtrl.left - xScroll, rcCtrl.top - yScroll,
rcCtrl.Width(), rcCtrl.Height(),
SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOCOPYBITS);
}
::EndDeferWindowPos(hDWP);
}
}
int MScrollView::GetNextPos(int bar, int nSB_, int pos) const noexcept
{
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
::GetScrollInfo(m_hwndParent, bar, &si);
switch (nSB_)
{
case SB_TOP:
return 0;
case SB_BOTTOM:
return si.nMax - si.nPage;
case SB_LINELEFT:
return std::max(0, int(si.nPos - 40));
case SB_LINERIGHT:
return std::min(int(si.nMax - si.nPage), int(si.nPos + 40));
case SB_PAGELEFT:
return std::max(0, int(si.nPos - si.nPage));
case SB_PAGERIGHT:
return std::min(int(si.nMax - si.nPage), int(si.nPos + si.nPage));
case SB_THUMBPOSITION:
pos = std::max(0, int(pos));
pos = std::min(int(si.nMax - si.nPage), int(pos));
return pos;
case SB_THUMBTRACK:
pos = std::max(0, int(si.nTrackPos));
pos = std::min(int(si.nMax - si.nPage), int(pos));
return pos;
default:
break;
}
return -1;
}
void MScrollView::Scroll(int bar, int nSB_, int pos) noexcept
{
const int nNextPos = GetNextPos(bar, nSB_, pos);
if (nNextPos == -1)
return;
const int nOldPos = ::GetScrollPos(m_hwndParent, bar);
if (bar == SB_HORZ)
{
const int dx = nNextPos - nOldPos;
::SetScrollPos(m_hwndParent, bar, nNextPos, TRUE);
::ScrollWindow(m_hwndParent, -dx, 0, nullptr, nullptr);
}
else if (bar == SB_VERT)
{
const int dy = nNextPos - nOldPos;
::SetScrollPos(m_hwndParent, bar, nNextPos, TRUE);
::ScrollWindow(m_hwndParent, 0, -dy, nullptr, nullptr);
}
}
////////////////////////////////////////////////////////////////////////////
#ifdef SCROLLVIEW_UNITTEST
HINSTANCE g_hInstance = nullptr;
HWND g_hMainWnd = nullptr;
MScrollView g_sv;
std::vector<HWND> g_ahwndCtrls;
const int c_nCtrls = 100;
const int c_height = 25;
bool g_bInited = false;
static const LPCTSTR s_pszName = TEXT("MZC3 ScrollView Test");
void OnSize(HWND hWnd)
{
if (!g_bInited)
return;
g_sv.clear();
MRect rcClient;
::GetClientRect(hWnd, &rcClient);
for (int i = 0; i < c_nCtrls; ++i)
{
MRect rcCtrl(MPoint(0, c_height * i),
MSize(rcClient.Width(), c_height));
g_sv.AddCtrlInfo(g_ahwndCtrls[i], rcCtrl);
}
g_sv.SetExtentForAllCtrls();
g_sv.UpdateAll();
}
BOOL OnCreate(HWND hWnd)
{
g_sv.SetParent(hWnd);
g_sv.ShowScrollBars(FALSE, TRUE);
HWND hwndCtrl;
TCHAR sz[32];
for (int i = 0; i < c_nCtrls; ++i)
{
::wsprintf(sz, TEXT("%d"), i);
#if 1
// text box
hwndCtrl = ::CreateWindowEx(
WS_EX_CLIENTEDGE,
TEXT("EDIT"), sz,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
0, 0, 0, 0, hWnd, nullptr, g_hInstance, nullptr);
#else
// button
hwndCtrl = ::CreateWindowEx(
0,
TEXT("BUTTON"), sz,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT | BS_CENTER | BS_VCENTER,
0, 0, 0, 0, hWnd, nullptr, g_hInstance, nullptr);
#endif
if (hwndCtrl == nullptr)
return FALSE;
::SendMessage(hwndCtrl, WM_SETFONT,
reinterpret_cast<WPARAM>(::GetStockObject(DEFAULT_GUI_FONT)),
FALSE);
g_ahwndCtrls.emplace_back(hwndCtrl);
}
g_bInited = true;
OnSize(hWnd);
return TRUE;
}
void OnDestroy(HWND hWnd)
{
for (int i = 0; i < c_nCtrls; ++i)
{
::DestroyWindow(g_ahwndCtrls[i]);
}
::PostQuitMessage(0);
}
LRESULT CALLBACK
WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
return OnCreate(hWnd) ? 0 : -1;
case WM_DESTROY:
OnDestroy(hWnd);
break;
case WM_SIZE:
OnSize(hWnd);
break;
case WM_HSCROLL:
g_sv.HScroll(LOWORD(wParam), HIWORD(wParam));
break;
case WM_VSCROLL:
g_sv.VScroll(LOWORD(wParam), HIWORD(wParam));
break;
default:
return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
extern "C"
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInstance = hInstance;
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.style = 0;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hIcon = ::LoadIcon(nullptr, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = reinterpret_cast<HBRUSH>(
static_cast<LONG_PTR>(COLOR_3DFACE + 1));
wc.lpszClassName = s_pszName;
if (!::RegisterClass(&wc))
{
return 1;
}
g_hMainWnd = ::CreateWindow(s_pszName, s_pszName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
nullptr, nullptr, hInstance, nullptr);
if (g_hMainWnd == nullptr)
{
return 2;
}
::ShowWindow(g_hMainWnd, nCmdShow);
::UpdateWindow(g_hMainWnd);
MSG msg;
while (::GetMessage(&msg, nullptr, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
#endif // def SCROLLVIEW_UNITTEST
////////////////////////////////////////////////////////////////////////////