Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / tools / logic / logic_editor_dll / NumEdit.cpp
blobc4148037468c946fda1143debe6017a5275b3caf
1 // NumEdit.cpp : implementation file
2 //
4 #include "stdafx.h"
5 #include "logic_editor.h"
6 #include "NumEdit.h"
8 #ifdef _DEBUG
9 #define new DEBUG_NEW
10 #undef THIS_FILE
11 static char THIS_FILE[] = __FILE__;
12 #endif
14 /////////////////////////////////////////////////////////////////////////////
15 // CNumEdit
17 CNumEdit::CNumEdit()
21 CNumEdit::~CNumEdit()
26 BEGIN_MESSAGE_MAP(CNumEdit, CEdit)
27 //{{AFX_MSG_MAP(CNumEdit)
28 ON_WM_CHAR()
29 //}}AFX_MSG_MAP
30 ON_MESSAGE(WM_PASTE, OnPaste)
31 END_MESSAGE_MAP()
33 /////////////////////////////////////////////////////////////////////////////
34 // CNumEdit message handlers
36 void CNumEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
38 static const CString Holder = "`~!@#$%^&*()_+|=\\qwertyuiop[]asdfghjkl;'zxcvbnm,/QWERTYUIOP{}ASDFGHJKL:/ZXCVBNM<>?/";
40 // 1st pass on unwanted characters
41 if (Holder.Find(nChar) != -1)
42 return;
45 // length, selection info
46 int num_typed = CEdit::GetWindowTextLength();
47 int start_char, end_char;
48 GetSel(start_char, end_char);
50 // current CEdit text
51 CString temp_str;
52 GetWindowText(temp_str);
54 // state variables
55 bool bSignTyped = false;
56 bool bPeriodTyped = false;
57 TCHAR temp_char;
59 // most of the work done in here..
60 for (int i = 0 ; i < temp_str.GetLength(); ++i)
62 // selected chars don't count..
63 if ((i>=start_char) && (i<end_char))
64 continue;
66 temp_char = temp_str[i];
68 if ((temp_char == '+') || (temp_char == '-')) {
69 bSignTyped = true;
70 } else if (temp_char == '.') {
71 bPeriodTyped = true;
75 // allow sign only once (first char)
76 if ((nChar == '+') || (nChar == '-')) {
77 if (bSignTyped || (start_char != 0))
78 return;
81 // allow period only once
82 if ((nChar == '.') && bPeriodTyped)
83 return;
86 CString old_str = temp_str;
87 CEdit::OnChar(nChar, nRepCnt, nFlags);
89 // check validity (if the user inserted numbers before the sign for example)
90 GetWindowText(temp_str);
91 if ( (temp_str.GetLength() > 1) && ( (temp_str[1] == '-') || (temp_str[1] == '+') ) )
93 this->SetWindowText( old_str );
99 LRESULT CNumEdit::OnPaste(WPARAM /*wParam*/, LPARAM /*lParam*/)
101 static BOOL called = FALSE;
103 CString old, temp;
104 GetWindowText( old );
106 /// \todo Malkav : handle pasting numeric data !!!
109 if (::IsClipboardFormatAvailable(CF_TEXT))
112 char * str=(char*)GetClipboardData(CF_TEXT);
113 temp = old + CString(*str);;
117 // check validity of new text in the edit window
119 if (checkValidity() == FALSE)
121 SetWindowText(old);
124 return 0;
129 BOOL CNumEdit::checkValidity()
131 CString temp;
132 GetWindowText( temp );
134 BOOL bSign = FALSE;
135 BOOL bPeriod = FALSE;
137 const int size = temp.GetLength();
138 for ( int i = 0 ; i < size ; ++i)
140 if (temp[i] <0 && temp[i]>9)
142 return FALSE;
144 else if (temp[i] == '-' || temp[i] =='+')
146 if ( (i != 0) || bSign == TRUE)
147 return FALSE;
148 else
149 bSign = TRUE;
151 else if (temp[i] == '.')
153 if (bPeriod == TRUE)
154 return FALSE;
155 else
156 bPeriod = TRUE;
160 return TRUE;