/
hexedit.h
235 lines (192 loc) · 4.41 KB
/
hexedit.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
#pragma once
#ifndef __cplusplus
#error WTL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLMISC_H__
#error hexedit.h requires atlmisc.h to be included first
#endif
#ifndef __ATLCTRLS_H__
#error hexedit.h requires atlctrls.h to be included first
#endif
/*
#ifdef _WIN64
address = _wcstoui64(hexString, NULL, 16);
#else
address = wcstoul(hexString, NULL, 16);
#endif
*/
template< class T, typename NUM_T, class TBase = CEdit, class TWinTraits = CControlWinTraits >
class ATL_NO_VTABLE CHexEditImpl : public CWindowImpl< T, TBase, TWinTraits >
{
public:
static const short int BASE = 16;
static const size_t DIGITS = sizeof(NUM_T) * 2; // 2 digits / byte
static const size_t STRSIZE = DIGITS + 1;
static const TCHAR Digits[];
static const TCHAR OldProcProp[];
// Operations
BOOL SubclassWindow(HWND hWnd)
{
ATLASSERT(m_hWnd == NULL);
ATLASSERT(::IsWindow(hWnd));
BOOL bRet = CWindowImpl< T, TBase, TWinTraits >::SubclassWindow(hWnd);
if( bRet ) _Init();
return bRet;
}
NUM_T GetValue() const
{
ATLASSERT(::IsWindow(m_hWnd));
TCHAR String[STRSIZE] = { 0 };
GetWindowText(String, _countof(String));
return _StringToNum(String);
}
void SetValue(NUM_T Num, bool Fill = true)
{
ATLASSERT(::IsWindow(m_hWnd));
TCHAR String[STRSIZE] = { 0 };
_NumToString(Num, String, Fill);
SetWindowText(String);
}
// Implementation
void _Init()
{
ATLASSERT(::IsWindow(m_hWnd));
LimitText(DIGITS);
}
bool _IsValidChar(TCHAR Char) const
{
return ((NUM_T)-1 != _CharToNum(Char));
}
bool _IsValidString(const TCHAR String[STRSIZE]) const
{
for(int i = 0; String[i]; i++)
{
if(!_IsValidChar(String[i]))
return false;
}
return true;
}
NUM_T _CharToNum(TCHAR Char) const
{
Char = _totupper(Char);
for(int i = 0; Digits[i]; i++)
{
if(Char == Digits[i])
return i;
}
return -1;
}
NUM_T _StringToNum(const TCHAR String[STRSIZE]) const
{
NUM_T CharNum;
NUM_T Num = 0;
for(int i = 0; String[i]; i++)
{
CharNum = _CharToNum(String[i]);
if(CharNum == (NUM_T)-1)
break;
Num *= BASE;
Num += CharNum;
}
return Num;
}
TCHAR _NumToChar(NUM_T Num) const
{
return Digits[Num % BASE];
}
void _NumToString(NUM_T Num, TCHAR String[STRSIZE], bool Fill) const
{
NUM_T Nums[DIGITS];
int i, j;
for(i = DIGITS-1; i >= 0; i--)
{
Nums[i] = Num % BASE;
Num /= BASE;
}
for(i = j = 0; i < DIGITS; i++)
{
// Only copy num if : non-null OR Fill OR non-null encountered before OR last num
if(Nums[i] || Fill || j || i == DIGITS-1)
{
String[j++] = _NumToChar(Nums[i]);
}
}
String[j] = '\0';
}
bool _GetClipboardText(TCHAR String[STRSIZE])
{
#ifdef UNICODE
const UINT Format = CF_UNICODETEXT;
#else
const UINT Format = CF_TEXT;
#endif
bool RetVal = false;
if(IsClipboardFormatAvailable(Format) && OpenClipboard())
{
HANDLE HMem = GetClipboardData(Format);
if(HMem)
{
_tcsncpy_s(String, STRSIZE, (TCHAR *)GlobalLock(HMem), STRSIZE-1);
String[STRSIZE-1] = '\0';
GlobalUnlock(HMem);
RetVal = true;
}
CloseClipboard();
}
return RetVal;
}
// Message map and handlers
BEGIN_MSG_MAP_EX(CHexEditImpl)
MESSAGE_HANDLER_EX(WM_CREATE, OnCreate)
MSG_WM_SETTEXT(OnSetText)
MSG_WM_PASTE(OnPaste)
MSG_WM_CHAR(OnChar)
END_MSG_MAP()
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lRes = DefWindowProc();
_Init();
return lRes;
}
int OnSetText(LPCTSTR lpstrText)
{
bool PassThrough = (_tcslen(lpstrText) <= DIGITS) && _IsValidString(lpstrText);
if(!PassThrough)
{
MessageBeep(-1);
return FALSE;
}
SetMsgHandled(FALSE);
return TRUE;
}
void OnPaste()
{
TCHAR String[STRSIZE];
bool PassThrough = !_GetClipboardText(String) || _IsValidString(String);
if(!PassThrough)
{
MessageBeep(-1);
return;
}
SetMsgHandled(FALSE);
return;
}
void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// ignore all printable chars (incl. space) which are not valid digits
bool PassThrough = !_istprint((TCHAR)nChar) || _IsValidChar((TCHAR)nChar);
if(!PassThrough)
{
MessageBeep(-1);
return;
}
SetMsgHandled(FALSE);
return;
}
};
template< class T, typename NUM_T, class TBase, class TWinTraits > const TCHAR CHexEditImpl< T, NUM_T, TBase, TWinTraits >::Digits[] = _T("0123456789ABCDEF");
template<typename NUM_T> class CHexEdit : public CHexEditImpl<CHexEdit<NUM_T>, NUM_T>
{
public:
DECLARE_WND_CLASS(_T("WTL_HexEdit"))
};