-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomEdit.h
492 lines (484 loc) · 23.1 KB
/
CustomEdit.h
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CustomEdit.h
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright (c) 2019-2024 Adrian Maurer. All rights reserved.
// Distributed under the MIT software license (http://www.opensource.org/licenses/mit-license.php).
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#pragma once
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <optional>
#include <format>
#include "Function.h"
#include "CustomControl.h"
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CCustomEdit
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class CCustomEdit : public ILock, public CEdit
{
// ELEMENT DATA
private:
// CONSTRUCTION / DESTRUCTION / ASSIGNMENT
public:
CCustomEdit() : ILock(), CEdit() {}
// MESSAGE MAP FUNCTIONS
protected:
afx_msg void OnKeyDown(UINT, UINT, UINT);
afx_msg void OnChar(UINT, UINT, UINT);
afx_msg void OnContextMenu(CWnd*, CPoint);
DECLARE_MESSAGE_MAP()
};
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CTextEdit
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class CTextEdit : public CCustomEdit
{
// ELEMENT DATA
private:
bool changeHandlerEnabled;
// CONSTRUCTION / DESTRUCTION / ASSIGNMENT
public:
CTextEdit() : CCustomEdit(), changeHandlerEnabled(true) {}
// ELEMENT FUNCTIONS
public:
void setText(const std::wstring&, const bool = false);
std::wstring getText() const;
// MESSAGE MAP FUNCTIONS
protected:
afx_msg BOOL OnEnChange();
DECLARE_MESSAGE_MAP()
};
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr int getMaxPrecision()
requires std::integral<T>
{
return 0;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr int getMaxPrecision()
requires std::floating_point<T>
{
return std::numeric_limits<T>::digits10;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline T convertToNumber(const std::wstring& s)
requires std::signed_integral<T> && (!std::same_as<T, long long>)
{
return static_cast<T>(wcstol(s.data(), nullptr, 10));
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline T convertToNumber(const std::wstring& s)
requires std::unsigned_integral<T> && (!std::same_as<T, unsigned long long>)
{
return static_cast<T>(wcstoul(s.data(), nullptr, 10));
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline T convertToNumber(const std::wstring& s)
requires std::same_as<T, long long>
{
return wcstoll(s.data(), nullptr, 10);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline T convertToNumber(const std::wstring& s)
requires std::same_as<T, unsigned long long>
{
return wcstoull(s.data(), nullptr, 10);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline T convertToNumber(const std::wstring& s)
requires std::floating_point<T>
{
return static_cast<T>(_wtof(s.data()));
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline std::wstring convertToString(const T n, const int = 0)
requires std::integral<T>
{
return std::format(L"{}", n);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
inline std::wstring convertToString(const T n, const int p = 0)
requires std::floating_point<T>
{
size_t nss = std::numeric_limits<T>::digits10 + 10; // Number string size
std::wstring ns(nss, '\0'); // Number string
if (nss = ::swprintf_s(&ns[0], nss, !p ? L"%.0f" : std::format(L"%.{}g", p).data(), n); nss != -1)
{
ns.resize(nss);
return ns;
}
return {};
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// TNumberEdit
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <typename T>
concept number = std::integral<T> && (!std::same_as<T, bool>) && (!std::same_as<T, char>) || std::floating_point<T>;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <typename T>
requires number<T>
class TNumberEdit : public CTextEdit
{
// ELEMENT DATA
private:
bool numberValid;
T limit1;
T limit2;
T increment1;
T increment2;
int precision;
// CONSTRUCTION / DESTRUCTION / ASSIGNMENT
public:
TNumberEdit(const int = getMaxPrecision<T>());
TNumberEdit(const T, const T, const int = getMaxPrecision<T>());
TNumberEdit(const T, const T, const T, const T, const int = getMaxPrecision<T>());
// ELEMENT FUNCTIONS
private:
T calcLimit(const T n) const {return ama::limit(n, limit1, limit2);}
public:
bool isNumberValid() const {return numberValid;}
T getLimit1() const {return limit1;}
T getLimit2() const {return limit2;}
void setLimit1(const T l) {limit1 = l;}
void setLimit2(const T l) {limit2 = l;}
void removeLimit1() {limit1 = std::numeric_limits<T>::lowest();}
void removeLimit2() {limit2 = (std::numeric_limits<T>::max)();}
void setLimits(const T, const T);
void setLimits(const std::array<T, 2>&);
void removeLimits();
T getIncrement1() const {return increment1;}
T getIncrement2() const {return increment2;}
void setIncrement1(const T i) {increment1 = i;}
void setIncrement2(const T i) {increment2 = i;}
void setIncrements(const T, const T);
int getPrecision() const {return precision;}
void setPrecision(const int p) {precision = p;}
void setNumber(const T, const bool = false);
void removeNumber(const bool = false);
T getNumber() const;
void setOptionalNumber(const std::optional<T>&, const bool = false);
std::optional<T> getOptionalNumber() const;
void increment(const bool, const T) requires std::integral<T>;
void increment(const bool, const T) requires std::floating_point<T>;
void incrementLastDigit(const bool) requires std::integral<T>;
void incrementLastDigit(const bool) requires std::floating_point<T>;
// MESSAGE MAP FUNCTIONS
protected:
afx_msg void OnKillFocus(CWnd*);
afx_msg BOOL OnMouseWheel(UINT, short, CPoint);
afx_msg void OnKeyDown(UINT, UINT, UINT);
afx_msg BOOL OnEnChange();
DECLARE_MESSAGE_MAP()
};
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// TNumberEdit - CONSTRUCTION / DESTRUCTION / ASSIGNMENT
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline TNumberEdit<T>::TNumberEdit(const int p) :
CTextEdit(),
numberValid(false),
limit1(std::numeric_limits<T>::lowest()),
limit2((std::numeric_limits<T>::max)()),
increment1(T(0)),
increment2(T(0)),
precision(p)
{
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline TNumberEdit<T>::TNumberEdit(const T i1, const T i2, const int p) :
CTextEdit(),
numberValid(false),
limit1(std::numeric_limits<T>::lowest()),
limit2((std::numeric_limits<T>::max)()),
increment1(i1),
increment2(i2),
precision(p)
{
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline TNumberEdit<T>::TNumberEdit(const T l1, const T l2, const T i1, const T i2, const int p) :
CTextEdit(),
numberValid(false),
limit1(l1),
limit2(l2),
increment1(i1),
increment2(i2),
precision(p)
{
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// TNumberEdit - ELEMENT FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::setLimits(const T l1, const T l2)
{
setLimit1(l1);
setLimit2(l2);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::setLimits(const std::array<T, 2>& la)
{
setLimit1(la[0]);
setLimit2(la[1]);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::removeLimits()
{
removeLimit1();
removeLimit2();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::setIncrements(const T i1, const T i2)
{
setIncrement1(i1);
setIncrement2(i2);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::setNumber(const T n, const bool che)
{
numberValid = true;
setText(::convertToString(n, precision), che);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::removeNumber(const bool che)
{
numberValid = false;
setText(L"", che);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline T TNumberEdit<T>::getNumber() const
{
return calcLimit(::convertToNumber<T>(getText()));
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::setOptionalNumber(const std::optional<T>& o_n, const bool che)
{
o_n ? setNumber(*o_n, che) : removeNumber(che);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline std::optional<T> TNumberEdit<T>::getOptionalNumber() const
{
return numberValid ? std::optional<T>(getNumber()) : std::nullopt;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::increment(const bool dir, const T is)
requires std::integral<T>
{
if (numberValid && is > T(0))
{
using namespace std;
const wstring ns(getText()); // Number string
if (const T n = ::convertToNumber<T>(ns); n >= limit1 && n <= limit2) // Number
{
const T b = !dir ? std::numeric_limits<T>::lowest() : (std::numeric_limits<T>::max)();
const T in = !dir ? n < b + is ? b : n - is : n > b - is ? b : n + is; // Incremented number
const wstring ins = ::convertToString(calcLimit(in)); // Incremented number string
const DWORD ocp = GetSel();
SetRedraw(FALSE);
setText(ins, true);
const int ncp = LOWORD(ocp) + static_cast<int>(ins.size() - ns.size());
SetSel(ncp, ncp);
SetRedraw(TRUE);
Invalidate(FALSE);
UpdateWindow();
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::increment(const bool dir, const T is)
requires std::floating_point<T>
{
if (numberValid && is > T(0))
{
using namespace std;
const wstring ns(getText()); // Number string
const T n = ::convertToNumber<T>(ns);
if ( n >= limit1 && n <= limit2) // Number
{
const wstring ins = ::convertToString(calcLimit(!dir ? n - is : n + is), precision); // Incremented number string
const int ncp = LOWORD(GetSel()) + static_cast<int>(ins.size() - ns.size());
SetRedraw(FALSE);
setText(ins, true);
SetSel(ncp, ncp);
SetRedraw(TRUE);
Invalidate(FALSE);
UpdateWindow();
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::incrementLastDigit(const bool dir)
requires std::integral<T>
{
increment(dir, T(1));
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::incrementLastDigit(const bool dir)
requires std::floating_point<T>
{
if (numberValid)
{
using namespace std;
const wstring ns(getText()); // Number string
const size_t nss = ns.size(); // Number string size
if (const T n = ::convertToNumber<T>(ns); n >= limit1 && n <= limit2) // Number
{
const size_t ep = ::wcscspn(ns.data(), L"eE"); // Exponent position
const wstring bs(ns.substr(0, ep)); // Basis string
const wstring es(ns.substr(ep, nss - ep)); // Exponent string
const size_t pp = bs.find_first_of('.'); // Point position
const size_t p = pp != wstring::npos ? bs.size() - pp - 1 : 0; // Precision
const T bn = ::convertToNumber<T>(bs); // Basis number
const T is = T(1.0) / static_cast<T>(pow(10, p)); // Increment step
const T ibn = !dir ? bn - is : bn + is; // Incremented basis number
size_t inss = numeric_limits<T>::digits10 + 10; // Incremented number string size
wstring ins(inss, '\0'); // Incremented number string
if (inss = ::swprintf_s(&ins[0], inss, format(L"%.{}f%s", p).data(), ibn, es.data()); inss != -1)
{
ins.resize(inss);
if (const T in = ::convertToNumber<T>(ins); in >= limit1 && in <= limit2) // Incremented number
{
const int ncp = LOWORD(GetSel()) + static_cast<int>(inss - nss);
SetRedraw(FALSE);
setText(ins, true);
SetSel(ncp, ncp);
SetRedraw(TRUE);
Invalidate(FALSE);
UpdateWindow();
}
}
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// TNumberEdit - MESSAGE MAP FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PTM_WARNING_DISABLE
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <typename T>
requires number<T>
inline const AFX_MSGMAP* TNumberEdit<T>::GetMessageMap() const
{
return GetThisMessageMap();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <typename T>
requires number<T>
inline const AFX_MSGMAP* PASCAL TNumberEdit<T>::GetThisMessageMap()
{
typedef TNumberEdit<T> ThisClass;
typedef CTextEdit TheBaseClass;
__pragma(warning(push))
__pragma(warning(disable: 4640))
static const AFX_MSGMAP_ENTRY _messageEntries[] =
{
ON_WM_KILLFOCUS()
ON_WM_KEYDOWN()
ON_WM_MOUSEWHEEL()
ON_CONTROL_REFLECT_EX(EN_CHANGE, &TNumberEdit::OnEnChange)
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0}
};
__pragma(warning(pop))
static const AFX_MSGMAP messageMap = {&TheBaseClass::GetThisMessageMap, &_messageEntries[0]};
return &messageMap;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PTM_WARNING_RESTORE
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::OnKillFocus(CWnd* pNewWnd)
{
CTextEdit::OnKillFocus(pNewWnd);
if (numberValid) setNumber(getNumber());
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline void TNumberEdit<T>::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (!isLocked() && !(GetStyle() & ES_READONLY))
{
const bool shift = HIWORD(::GetKeyState(VK_SHIFT));
const bool ctrl = HIWORD(::GetKeyState(VK_CONTROL));
if (nChar == VK_DOWN)
{
if (!ctrl) increment(false, !shift ? increment1 : increment2);
else if (!shift) incrementLastDigit(false);
return;
}
else if (nChar == VK_UP)
{
if (!ctrl) increment(true, !shift ? increment1 : increment2);
else if (!shift) incrementLastDigit(true);
return;
}
}
CTextEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline BOOL TNumberEdit<T>::OnMouseWheel(UINT nFlags, short zDelta, CPoint pnt)
{
if (!isLocked() && !(GetStyle() & ES_READONLY))
{
const bool shift = nFlags & MK_SHIFT;
const bool ctrl = nFlags & MK_CONTROL;
const bool dir = zDelta > 0;
if (!ctrl) increment(dir, !shift ? increment1 : increment2);
else if (!shift) incrementLastDigit(dir);
}
return CTextEdit::OnMouseWheel(nFlags, zDelta, pnt);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
requires number<T>
inline BOOL TNumberEdit<T>::OnEnChange()
{
if (!isLocked() && !CTextEdit::OnEnChange())
{
numberValid = true;
return FALSE;
}
return TRUE;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------