1 #include "studiox_includes.h"
2 #include "custom_edit.h"
3
4 ///////////////////////////////////////////////////////////////////////////////
custom_edit()5 custom_edit::custom_edit()
6 {
7 m_bNegativeValueAllowed = FALSE;
8 m_bNumberCheck = FALSE;
9 }
10
11 ///////////////////////////////////////////////////////////////////////////////
ErrorMessage()12 void custom_edit::ErrorMessage()
13 {
14 ErrorMsg("Unacceptable Character. You can only type a number here.", this);
15 }
16
17 ///////////////////////////////////////////////////////////////////////////////
OnPaste()18 BOOL custom_edit::OnPaste()
19 {
20 if (!m_bNumberCheck)
21 {
22 return FALSE;
23 }
24
25 if (OpenClipboard())
26 {
27 BOOL invalid_input = FALSE;
28 HANDLE hMem = GetClipboardData(CF_UNICODETEXT);
29
30 if (!hMem)
31 {
32 CloseClipboard();
33 ErrorMsg("Unknown clipboard data format.", this);
34 return NULL;
35 }
36
37 LPTSTR lptstr = (LPTSTR)GlobalLock(hMem);
38
39 if (lptstr)
40 {
41 CString string;
42 INT start, end;
43 GetWindowText(string);
44
45 // Pickup selection range.
46 SendMessage(EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
47
48 // Prapare string for checking.
49 if (start != end)
50 {
51 string.Delete(start, end - start);
52 }
53 string.Insert(start, lptstr);
54
55 // Check invalid string.
56 wchar_t character;
57 for (int index = 0; index < string.GetLength(); index++)
58 {
59 character = string.GetAt(index);
60
61 if (!(m_bNegativeValueAllowed) || (index != 0) || (character != '-'))
62 {
63 if (character < '0' || character > '9')
64 {
65 // Only numeric value is allowed.
66 invalid_input = TRUE;
67 break;
68 }
69 }
70 }
71 }
72
73 GlobalUnlock(hMem);
74
75 CloseClipboard();
76
77 if (invalid_input)
78 {
79 ErrorMessage();
80 return TRUE;
81 }
82 }
83 return FALSE;
84 }
85
86 ///////////////////////////////////////////////////////////////////////////////
OnChar(UINT key)87 BOOL custom_edit::OnChar(UINT key)
88 {
89 if (!m_bNumberCheck)
90 {
91 return FALSE;
92 }
93
94 if (!isprint(key))
95 {
96 return FALSE;
97 }
98
99 BOOL input_invalid = FALSE;
100
101 if (m_bNegativeValueAllowed && (key == '-'))
102 {
103 CString string;
104 INT start, end;
105 GetWindowTextW(string);
106 if (!string.IsEmpty())
107 {
108 // Pickup selection range.
109 SendMessage(EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
110 if (start == 0)
111 {
112 if ((string.GetAt(0) == '-') && (start == end))
113 {
114 // Insert negative sign is not allowed,
115 // as the first character is already a negative sign.
116 input_invalid = TRUE;
117 }
118 }
119 else
120 {
121 // Insert negative sign in the middle of a numeric string is not allowed.
122 input_invalid = TRUE;
123 }
124 }
125 }
126 else
127 {
128 if (key < '0' || key > '9')
129 {
130 // Only numeric value is allowed.
131 input_invalid = TRUE;
132 }
133 }
134
135 if (input_invalid)
136 {
137 // Prompt error message.
138 ErrorMessage();
139 return TRUE;
140 }
141
142 return FALSE;
143 }
144
145 ///////////////////////////////////////////////////////////////////////////////
WindowProc(UINT message,WPARAM wParam,LPARAM lParam)146 LRESULT custom_edit::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
147 {
148 switch (message)
149 {
150 case WM_PASTE:
151 if (OnPaste())
152 {
153 return TRUE;
154 }
155 break;
156
157 case WM_CHAR:
158 if (OnChar(wParam))
159 {
160 return TRUE;
161 }
162 break;
163 }
164 return CEdit::WindowProc(message, wParam, lParam);
165 }
166