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
|
void HandlePaste(HWND hWnd);
LRESULT CALLBACK
MyEditProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg == WM_CHAR)
{
if(0x8000 & GetKeyState(VK_CONTROL))
{
switch(wParam)
{
case 0x16: // ctrl-V
{
HandlePaste(hWnd);
return 0;
}
break;
case 0x03: // ctrl-C
case 0x18: // ctrl-X
{
// allow through
}
break;
default:
{
return 0;
}
}
}
else
{
// Only check non-digits
if(!isdigit(wParam))
{
switch(wParam)
{
case _T('-'):
{
DWORD from = 0;
DWORD to = 0;
SendMessage(hWnd, EM_GETSEL, (WPARAM)&from, (WPARAM)&to);
if(0 == from)
{
// if to is greater than zero, we're replacing the 0th
// char so we don't need to worry. If we're inserting
// have to check the first char isn't already a -
if(0 == to)
{
TCHAR buffer[2] = _T(""); // big enough for one char plus term null
SendMessage(hWnd, WM_GETTEXT, (WPARAM)2, (WPARAM)buffer);
if(buffer[0] == _T('-'))
return 0;
}
// allow through
}
else
{
return 0;
}
}
break;
case VK_BACK:
{
// allow backspace through
}
break;
default:
{
// block everything else
return 0;
}
}
}
}
}
// call through to original WndProc for Edit control
return CallWindowProc(g_pfnHexEditProc_Old, hWnd, uMsg, wParam, lParam);
}
bool IsValidNumber(const char* pszText, bool allowMinus)
{
if(allowMinus && ('-' == *pszText))
++pszText;
while(*pszText != '\0')
{
if(!isdigit(*pszText))
return false;
++pszText;
}
return true;
}
void HandlePaste(HWND hWnd)
{
bool isValidNumber = false;
if(OpenClipboard(hWnd))
{
HANDLE hData = GetClipboardData(CF_TEXT);
if(NULL != hData)
{
LPSTR pszText = (LPSTR)GlobalLock(hData);
if(NULL != pszText)
{
bool doPaste = true;
bool allowMinus = true;
DWORD from = 0;
DWORD to = 0;
SendMessageA(hWnd, EM_GETSEL, (WPARAM)&from, (WPARAM)&to);
if(0 == from)
{
if(0 == to)
{
char buffer[2] = ""; // big enough for one char plus term null
SendMessageA(hWnd, WM_GETTEXT, (WPARAM)2, (WPARAM)buffer);
if(buffer[0] == '-')
doPaste = false;
}
}
else
{
allowMinus = false;
}
if(doPaste)
{
doPaste = IsValidNumber(pszText, allowMinus);
}
if(doPaste)
{
SendMessageA(hWnd, EM_REPLACESEL, (WPARAM)TRUE, (WPARAM)pszText);
}
}
GlobalUnlock(hData);
}
CloseClipboard();
}
}
|