/
NumericEdit.cpp
432 lines (370 loc) · 11.9 KB
/
NumericEdit.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
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
/**
* NumericEdit.cpp - CNumericEdit implementation
*
* Written By Gurmeet S. Kochar <gomzygotit@hotmail.com>
* Copyright (c) 2004. All rights reserved.
*
* This code may be used in compiled form in any way you desire
* (including commercial use). The code may be redistributed
* unmodified by any means PROVIDING it is not sold for profit
* without the authors written consent, and providing that this
* notice and the authors name and all copyright notices remains
* intact. However, this file and the accompanying source code may
* not be hosted on a website or bulletin board without the authors
* written permission.
*
* This file is provided "AS IS" with no expressed or implied warranty.
* The author accepts no liability for any damage/loss of business that
* this product may cause.
*
* HISTORY:
* Mar 03, 2004 (Gurmeet S. Kochar)
* -> First release.
* Mar 04, 2004 (Gurmeet S. Kochar)
* -> Added WM_PASTE message handling.
* Suggested by snakeware (www.codeproject.com).
* -> Minor code changes to extract decimal and
* negative symbols from default user locale.
* Suggested By BishBosh02 (www.codeproject.com).
*/
#include "stdafx.h"
#include "NumericEdit.h"
#ifndef _INC_TCHAR
# include <tchar.h>
#endif // !_INC_TCHAR
/**
* Removed By Gurmeet S. Kochar (Mar 04, 2004)
*/
//#ifndef VK_OEM_PERIOD
//# define VK_OEM_PERIOD 0xBE
//#endif // !VK_OEM_PERIOD
#ifndef VK_OEM_MINUS
# define VK_OEM_MINUS 0xBD
#endif // !VK_OEM_MINUS
// initialize static variable(s)
const CNumericEdit::_LocaleInfo CNumericEdit::DefUserLocale;
BEGIN_MESSAGE_MAP(CNumericEdit, CEdit)
ON_MESSAGE(WM_PASTE, OnPaste)
END_MESSAGE_MAP()
/**
* GetClipboardText - clipboard text extraction helper
* Checks to see if any textual data is available on the clipboard. If
* yes, extracts it, parses only numeric data out of it, and returns it.
*
* Added By Gurmeet S. Kochar (Mar 04, 2004)
*/
#pragma warning(disable: 4018) // signed/unsigned mismatch
CString CNumericEdit::GetClipboardText(void) const
{
CString strClipBrdText;
// clipboard contains textual data?
if (::IsClipboardFormatAvailable(CF_TEXT))
{
// open the clipboard to get clipboard text
if (::OpenClipboard(m_hWnd))
{
HANDLE hClipBrdData = NULL;
if ((hClipBrdData = ::GetClipboardData(CF_TEXT)) != NULL)
{
LPTSTR lpClipBrdText = (LPTSTR)::GlobalLock(hClipBrdData);
if (lpClipBrdText)
{
strClipBrdText = lpClipBrdText;
::GlobalUnlock(hClipBrdData);
}
}
VERIFY(::CloseClipboard());
/**
* parse a number out of the retrieved text
*/
bool bHasDecimal = false; // decimal symbol flag
int iStart = 0, iCount = 0;
LPTSTR lpsz = strClipBrdText.LockBuffer();
// skip leading whitespaces (including tabs)
for (; *lpsz == _T(' ') || *lpsz == _T('\t'); iStart++)
lpsz = ::_tcsinc(lpsz);
// is the first character a negative symbol?
if (*lpsz == DefUserLocale.chNegationSymbol)
{
// negative values are not allowed?
if (!m_bAllowNegativeValues)
return ((LPCTSTR)NULL);
++iCount;
lpsz = ::_tcsinc(lpsz);
}
while (*lpsz != _T('\0'))
{
if (!::_istdigit(*lpsz))
{
if ( (m_chDigitsAfterDecimal) &&
(*lpsz != DefUserLocale.chDecimalSymbol) )
break;
// a decimal symbol is already there?
if (bHasDecimal)
break;
bHasDecimal = true;
}
++iCount;
lpsz = ::_tcsinc(lpsz);
}
strClipBrdText.UnlockBuffer();
if ( (!iStart) && (!iCount) )
strClipBrdText.Empty();
else
strClipBrdText = strClipBrdText.Mid(iStart, iCount);
}
}
return (strClipBrdText);
}
/**
* OnPaste - WM_PASTE message handler
* Parses a numeric value from the clipboard text, if available, and
* displays it appropriately in the edit control
*
* Added By Gurmeet S. Kochar (Mar 04, 2004)
*/
LRESULT CNumericEdit::OnPaste(WPARAM wParam, LPARAM lParam)
{
UNUSED_ALWAYS(wParam);
UNUSED_ALWAYS(lParam);
CString strClipBrdText, // text available on clipboard
strCtrlText; // text in the edit control
DWORD dwSelStart = 0, // selection range starting position
dwSelEnd = 0; // selection range ending position
int iDecimalPos[2]; // 0 = in control, 1 = in clipboard text
strClipBrdText = GetClipboardText();
// no (valid) clipboard data available?
if (strClipBrdText.IsEmpty())
{
TRACE0("(Warning) CNumericEdit::OnPaste:"
" Unknown clipboard data format or clipboard data unavailable.\n");
::MessageBeep(MB_ICONEXCLAMATION);
return (0L);
}
// get control's current text and selection range
GetWindowText(strCtrlText);
SendMessage(EM_GETSEL, (WPARAM)&dwSelStart, (LPARAM)&dwSelEnd);
// pasting a negative value somewhere in between the current text?
if ( (strClipBrdText[0] == DefUserLocale.chNegationSymbol) && (dwSelStart) )
{
::MessageBeep(MB_ICONEXCLAMATION);
return (0L);
}
if (strCtrlText.GetLength() &&
strCtrlText[0] == DefUserLocale.chNegationSymbol)
{
if (dwSelEnd == dwSelStart)
{
::MessageBeep(MB_ICONEXCLAMATION);
return (0L);
}
}
if (m_chDigitsAfterDecimal)
{
iDecimalPos[0] = strCtrlText.Find(DefUserLocale.chDecimalSymbol);
iDecimalPos[1] = strClipBrdText.Find(DefUserLocale.chDecimalSymbol);
// both control and clipboard text contain a decimal symbol and
// decimal symbol doesn't fall within the current selection range
if ( (iDecimalPos[0] >= 0 && iDecimalPos[1] >= 0) &&
(iDecimalPos[0] < dwSelStart || iDecimalPos[0] > dwSelEnd) )
{
// extract only that much data from clipboard text
// that comes before the decimal symbol position
strClipBrdText = strClipBrdText.Left(iDecimalPos[1]);
// set iDecimalPos[1] to -1 to indicate that now
// there is no decimal symbol in the clipboard text!
iDecimalPos[1] = -1;
}
}
strCtrlText = strCtrlText.Left(dwSelStart) +
strClipBrdText +
strCtrlText.Mid(dwSelEnd);
dwSelEnd = dwSelStart + strClipBrdText.GetLength();
// if count of digits after decimal symbol exceeds the
// value specified by m_chDigitsAfterDecimal, then,
// truncate the rest of the string
iDecimalPos[0] = strCtrlText.Find(DefUserLocale.chDecimalSymbol);
if (iDecimalPos[0] >= 0)
{
if (strCtrlText.Mid(iDecimalPos[0] + 1).GetLength() > m_chDigitsAfterDecimal)
strCtrlText = strCtrlText.Left(iDecimalPos[0] + m_chDigitsAfterDecimal + 1);
}
SetWindowText(strCtrlText);
SetSel(dwSelEnd, dwSelEnd); // position caret, scroll if necessary!
// Do not call CEdit::Paste from within this function!!!
return (0L);
}
/**
* PreTranslateMessage
* Handles WM_KEYDOWN message
*
* Author: Gurmeet S. Kochar
* Date: Mar 03, 2004
*/
BOOL CNumericEdit::PreTranslateMessage(MSG *pMSG)
{
CString strBuffer; // edit control's text buffer
DWORD dwSelStart = 0, // selection starting position
dwSelEnd = 0; // selection ending position
int iPos = 0; // to hold the return value of CString::Find
UINT nKeyCode = pMSG->wParam; // virtual key code of the key pressed
// not a WM_KEYDOWN message?
if (pMSG->message != WM_KEYDOWN)
// forward for default processing!
goto LForwardMsg;
// CTRL+C, CTRL+X, or, CTRL+V?
if ( (nKeyCode == _T('C') || nKeyCode == _T('X') || nKeyCode == _T('V')) &&
(::GetKeyState(VK_CONTROL) & 0x8000) )
goto LForwardMsg;
//TRACE1("(Info) CNumericEdit::PreTranslateMessage:"
// " Virtual key code of the key pressed = 0x%04x\n", nKeyCode);
switch (nKeyCode)
{
// subtract key?
case VK_SUBTRACT:
case VK_OEM_MINUS: // Removed By Gurmeet S. Kochar (Mar 04, 2004)
{
// get into this only if negative values are allowed!
if (m_bAllowNegativeValues)
{
GetWindowText(strBuffer);
if (strBuffer.IsEmpty())
break;
// retrieve selection range
SendMessage(EM_GETSEL, (WPARAM)&dwSelStart, (LPARAM)&dwSelEnd);
// is the selection beginning from the very first character?
if (!dwSelStart)
{
// NOTE: if text buffer starts from a decimal symbol,
// then we cannot accept a negative symbol until the
// decimal points symbol falls within the current
// selection range
/**
* Removed By Gurmeet S. Kochar (Mar 04, 2004)
*/
// if (strBuffer[0] == _T('.'))
if (strBuffer[0] == DefUserLocale.chDecimalSymbol)
{
// decimal symbol falls within the current selection range?
if (dwSelEnd != dwSelStart)
// accept the negative symbol as it is going to
// replace the currently selected text
break;
::MessageBeep(MB_ICONEXCLAMATION);
return (TRUE);
}
// check to make sure the first character is not
// already a negative sign
if (strBuffer[0] != DefUserLocale.chNegationSymbol)
break;
// the first character is a negative sign but
// is it within the current selection range?
if (dwSelEnd != dwSelStart)
break;
}
}
::MessageBeep(MB_ICONEXCLAMATION);
return (TRUE);
}
// digits on the main section of keyboard?
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
// digits on the numeric keypad?
case 0x60: case 0x61: case 0x62: case 0x63: case 0x64:
case 0x65: case 0x66: case 0x67: case 0x68: case 0x69:
// decimal symbol?
//case VK_DECIMAL:
//case VK_OEM_PERIOD: // Removed By Gurmeet S. Kochar (Mar 04, 2004)
{
GetWindowText(strBuffer);
if (strBuffer.IsEmpty())
break;
/**
* Removed By Gurmeet S. Kochar (Mar 04, 2004)
*/
//iPos = strBuffer.Find(_T('.'));
iPos = strBuffer.Find(DefUserLocale.chDecimalSymbol);
// get the current selection range
SendMessage(EM_GETSEL, (WPARAM)&dwSelStart, (LPARAM)&dwSelEnd);
//TRACE2("(Info) CNumericEdit::PreTranslateMessage:"
// " SelStart = %ld, SelEnd = %ld\n", dwSelStart, dwSelEnd);
// decimal symbol?
if (nKeyCode == VK_DECIMAL /*|| nKeyCode == VK_OEM_PERIOD*/)
{
ASSERT(m_chDigitsAfterDecimal >= 0 && m_chDigitsAfterDecimal <= 9);
// accept integer values only?
if (!m_chDigitsAfterDecimal)
{
::MessageBeep(MB_ICONEXCLAMATION);
return (TRUE);
}
// a decimal symbol is already there?
if (iPos >= 0)
{
if ( (dwSelEnd == dwSelStart) &&
(iPos < dwSelStart || iPos > dwSelEnd) )
{
// there can be only one decimal symbol in a numeric value
::MessageBeep(MB_ICONEXCLAMATION);
return (TRUE);
}
}
}
// one of the digit keys has been pressed?
else
{
// iPos >= 0 : value contains a decimal symbol?
// dwSelStart > iPos : user is typing text after the decimal symbol?
// dwSelStart == dwSelEnd : the selection is empty?
// strBuffer.Mid... : number of digits after decimal symbol is
// going to exceed than the value specified
// by m_chDigitsAfterDecimal?
if ( (iPos >= 0) &&
(dwSelStart > iPos) &&
(dwSelStart == dwSelEnd) &&
(strBuffer.Mid(iPos+1).GetLength() >= m_chDigitsAfterDecimal) )
{
::MessageBeep(MB_ICONEXCLAMATION);
return (TRUE);
}
}
// neither a digit nor a decimal symbol is allowed
// at the starting of the text when the value in the
// text buffer is a negative value
/**
* Removed By Gurmeet S. Kochar (Mar 04, 2004)
*/
//if ( (!dwSelStart) && (strBuffer[0] == _T('-')) )
if ( (!dwSelStart) && (strBuffer[0] == DefUserLocale.chNegationSymbol) )
{
// negative sign is within the current selection range?
if (dwSelEnd == dwSelStart)
{
::MessageBeep(MB_ICONEXCLAMATION);
return (TRUE);
}
}
break;
}
default:
{
if ((nKeyCode >= 0x01 && nKeyCode <= 0x40) ||
(nKeyCode >= 0x5B && nKeyCode <= 0x69) ||
(nKeyCode >= 0x70 && nKeyCode <= 0xB9) ||
(nKeyCode >= 0xE5 && nKeyCode <= 0xFE))
{
// spaces are not allowed in numeric values!
if (nKeyCode == VK_SPACE)
{
::MessageBeep(MB_ICONEXCLAMATION);
return (TRUE);
}
break;
}
return (TRUE); // eat message; ignore the key!!!
}
}
LForwardMsg:
return (CEdit::PreTranslateMessage(pMSG));
}
#pragma warning(default: 4018)