-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MScrollView.hpp
275 lines (224 loc) · 7.29 KB
/
MScrollView.hpp
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
////////////////////////////////////////////////////////////////////////////
// MScrollView.hpp -- MZC3 scroll view
// This file is part of MZC3. See file "ReadMe.txt" and "License.txt".
////////////////////////////////////////////////////////////////////////////
#ifndef __MZC3_SCROLLVIEW__
#define __MZC3_SCROLLVIEW__
#ifndef _INC_WINDOWS
#include <windows.h>
#endif
#include <windowsx.h>
#include <vector>
#include "MPointSizeRect.hpp"
////////////////////////////////////////////////////////////////////////////
// MScrollCtrlInfo
struct MScrollCtrlInfo
{
HWND m_hwndCtrl;
MRect m_rcCtrl;
MScrollCtrlInfo() noexcept;
MScrollCtrlInfo(HWND hwndCtrl) noexcept;
MScrollCtrlInfo(HWND hwndCtrl, const MRect& rcCtrl) noexcept;
MScrollCtrlInfo(HWND hwndCtrl, const MPoint& ptCtrl, const MSize& sizCtrl) noexcept;
};
////////////////////////////////////////////////////////////////////////////
// MScrollView
class MScrollView
{
public:
MScrollView() noexcept;
MScrollView(HWND hwndParent) noexcept;
MScrollView(HWND hwndParent, HWND hHScrollBar, HWND hVScrollBar) noexcept;
virtual ~MScrollView() noexcept;
// parent
HWND GetParent() const noexcept;
void SetParent(HWND hwndParent) noexcept;
// parent scroll bars
void ShowScrollBars(BOOL fHScroll, BOOL fVScroll) noexcept;
// add/set control info
void AddCtrlInfo(HWND hwndCtrl);
void AddCtrlInfo(HWND hwndCtrl, const MRect& rcCtrl);
void AddCtrlInfo(HWND hwndCtrl, const MPoint& ptCtrl, const MSize& sizCtrl);
void SetCtrlInfo(HWND hwndCtrl, const MRect& rcCtrl);
void SetCtrlInfo(HWND hwndCtrl, const MPoint& ptCtrl, const MSize& sizCtrl);
void RemoveCtrlInfo(HWND hwndCtrl) noexcept;
void AddCtrlInfo(UINT idCtrl);
void AddCtrlInfo(UINT idCtrl, const MRect& rcCtrl);
void AddCtrlInfo(UINT idCtrl, const MPoint& ptCtrl, const MSize& sizCtrl);
void SetCtrlInfo(UINT idCtrl, const MRect& rcCtrl);
void SetCtrlInfo(UINT idCtrl, const MPoint& ptCtrl, const MSize& sizCtrl);
void RemoveCtrlInfo(UINT idCtrl) noexcept;
bool empty() const noexcept;
void clear() noexcept;
size_t size() const noexcept;
// find control info
MScrollCtrlInfo* FindCtrlInfo(HWND hwndCtrl) noexcept;
const MScrollCtrlInfo* FindCtrlInfo(HWND hwndCtrl) const noexcept;
// extent
MSize& Extent() noexcept;
const MSize& Extent() const noexcept;
void SetExtentForAllCtrls(INT cxExtra = 0, INT cyExtra = 0) noexcept;
// ensure visible
void EnsureCtrlVisible(HWND hwndCtrl, bool update_all = true) noexcept;
// update
void UpdateCtrlsPos() noexcept;
void UpdateAll() noexcept;
// NOTE: Call MScrollView::Scroll on parent's WM_HSCROLL/WM_VSCROLL.
void Scroll(int bar, int nSB_, int pos) noexcept;
int GetNextPos(int bar, int nSB_, int pos) const noexcept;
MScrollCtrlInfo& operator[](size_t index) noexcept;
const MScrollCtrlInfo& operator[](size_t index) const noexcept;
protected:
HWND m_hwndParent;
MSize m_sizExtent;
std::vector<MScrollCtrlInfo> m_vecInfo;
BOOL HasChildStyle(HWND hwnd) const noexcept;
private:
// NOTE: MScrollView is not copyable.
MScrollView(const MScrollView&) = delete;
MScrollView& operator=(const MScrollView&) = delete;
};
////////////////////////////////////////////////////////////////////////////
inline MScrollCtrlInfo::MScrollCtrlInfo() noexcept : m_hwndCtrl(nullptr)
{
}
inline MScrollCtrlInfo::MScrollCtrlInfo(HWND hwndCtrl) noexcept :
m_hwndCtrl(hwndCtrl)
{
}
inline
MScrollCtrlInfo::MScrollCtrlInfo(HWND hwndCtrl, const MRect& rcCtrl) noexcept :
m_hwndCtrl(hwndCtrl), m_rcCtrl(rcCtrl)
{
}
inline MScrollCtrlInfo::MScrollCtrlInfo(
HWND hwndCtrl, const MPoint& ptCtrl, const MSize& sizCtrl) noexcept :
m_hwndCtrl(hwndCtrl), m_rcCtrl(ptCtrl, sizCtrl)
{
}
inline MScrollView::MScrollView() noexcept :
m_hwndParent(nullptr)
{
}
inline MScrollView::MScrollView(HWND hwndParent) noexcept :
m_hwndParent(nullptr)
{
MScrollView::SetParent(hwndParent);
}
inline MScrollView::MScrollView(
HWND hwndParent, HWND hHScrollBar, HWND hVScrollBar) noexcept :
m_hwndParent(nullptr)
{
UNREFERENCED_PARAMETER(hHScrollBar);
UNREFERENCED_PARAMETER(hVScrollBar);
MScrollView::SetParent(hwndParent);
}
inline /*virtual*/ MScrollView::~MScrollView() noexcept
{
}
inline HWND MScrollView::GetParent() const noexcept
{
return m_hwndParent;
}
inline void MScrollView::SetParent(HWND hwndParent) noexcept
{
assert(::IsWindow(hwndParent));
m_hwndParent = hwndParent;
DWORD_PTR style = ::GetWindowLongPtrW(m_hwndParent, GWL_STYLE);
style |= WS_CLIPCHILDREN;
::SetWindowLongPtrW(m_hwndParent, GWL_STYLE, style);
}
inline
void MScrollView::ShowScrollBars(BOOL fHScroll, BOOL fVScroll) noexcept
{
::ShowScrollBar(m_hwndParent, SB_HORZ, fHScroll);
::ShowScrollBar(m_hwndParent, SB_VERT, fVScroll);
}
inline void MScrollView::AddCtrlInfo(HWND hwndCtrl)
{
assert(::IsWindow(hwndCtrl));
assert(HasChildStyle(hwndCtrl));
m_vecInfo.emplace_back(hwndCtrl);
}
inline void MScrollView::AddCtrlInfo(HWND hwndCtrl, const MRect& rcCtrl)
{
assert(::IsWindow(hwndCtrl));
assert(HasChildStyle(hwndCtrl));
assert(rcCtrl.left >= 0);
assert(rcCtrl.top >= 0);
m_vecInfo.emplace_back(hwndCtrl, rcCtrl);
}
inline void MScrollView::AddCtrlInfo(
HWND hwndCtrl, const MPoint& ptCtrl, const MSize& sizCtrl)
{
assert(::IsWindow(hwndCtrl));
assert(HasChildStyle(hwndCtrl));
assert(ptCtrl.x >= 0);
assert(ptCtrl.y >= 0);
m_vecInfo.emplace_back(hwndCtrl, ptCtrl, sizCtrl);
}
inline MSize& MScrollView::Extent() noexcept
{
return m_sizExtent;
}
inline const MSize& MScrollView::Extent() const noexcept
{
return m_sizExtent;
}
inline BOOL MScrollView::HasChildStyle(HWND hwnd) const noexcept
{
return (::GetWindowLongPtrW(hwnd, GWL_STYLE) & WS_CHILD);
}
inline bool MScrollView::empty() const noexcept
{
return m_vecInfo.empty();
}
inline void MScrollView::clear() noexcept
{
m_vecInfo.clear();
}
inline size_t MScrollView::size() const noexcept
{
return m_vecInfo.size();
}
inline void MScrollView::UpdateAll() noexcept
{
UpdateCtrlsPos();
}
inline void MScrollView::AddCtrlInfo(UINT idCtrl)
{
AddCtrlInfo(::GetDlgItem(m_hwndParent, idCtrl));
}
inline void MScrollView::AddCtrlInfo(UINT idCtrl, const MRect& rcCtrl)
{
AddCtrlInfo(::GetDlgItem(m_hwndParent, idCtrl), rcCtrl);
}
inline void MScrollView::AddCtrlInfo(
UINT idCtrl, const MPoint& ptCtrl, const MSize& sizCtrl)
{
AddCtrlInfo(::GetDlgItem(m_hwndParent, idCtrl), ptCtrl, sizCtrl);
}
inline void MScrollView::SetCtrlInfo(UINT idCtrl, const MRect& rcCtrl)
{
SetCtrlInfo(::GetDlgItem(m_hwndParent, idCtrl), rcCtrl);
}
inline void MScrollView::SetCtrlInfo(
UINT idCtrl, const MPoint& ptCtrl, const MSize& sizCtrl)
{
SetCtrlInfo(::GetDlgItem(m_hwndParent, idCtrl), ptCtrl, sizCtrl);
}
inline void MScrollView::RemoveCtrlInfo(UINT idCtrl) noexcept
{
RemoveCtrlInfo(::GetDlgItem(m_hwndParent, idCtrl));
}
inline MScrollCtrlInfo& MScrollView::operator[](size_t index) noexcept
{
assert(index < size());
return m_vecInfo[index];
}
inline const MScrollCtrlInfo& MScrollView::operator[](size_t index) const noexcept
{
assert(index < size());
return m_vecInfo[index];
}
#endif // ndef __MZC3_SCROLLVIEW__