Mercurial > traipse_dev
comparison orpg/tools/inputValidator.py @ 0:4385a7d0efd1 grumpy-goblin
Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author | sirebral |
---|---|
date | Tue, 14 Jul 2009 16:41:58 -0500 |
parents | |
children | dcae32e219f1 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4385a7d0efd1 |
---|---|
1 # Copyright (C) 2000-2001 The OpenRPG Project | |
2 # | |
3 # openrpg-dev@lists.sourceforge.net | |
4 # | |
5 # This program is free software; you can redistribute it and/or modify | |
6 # it under the terms of the GNU General Public License as published by | |
7 # the Free Software Foundation; either version 2 of the License, or | |
8 # (at your option) any later version. | |
9 # | |
10 # This program is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with this program; if not, write to the Free Software | |
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 # -- | |
19 # | |
20 # File: inputValidator.py | |
21 # Author: Greg Copeland | |
22 # Maintainer: | |
23 # | |
24 # Description: Contains simple input validators to help reduce the amount of | |
25 # user input generated text. | |
26 # | |
27 | |
28 __version__ = "$Id: inputValidator.py,v 1.11 2006/11/04 21:24:22 digitalxero Exp $" | |
29 | |
30 | |
31 ## | |
32 ## Module Loading | |
33 ## | |
34 from orpg.orpg_wx import * | |
35 import string | |
36 | |
37 | |
38 ## | |
39 ## Text Only input (no numbers allowed) | |
40 ## | |
41 class TextOnlyValidator(wx.PyValidator): | |
42 def __init__( self ): | |
43 wx.PyValidator.__init__( self ) | |
44 self.Bind(wx.EVT_CHAR, self.onChar) | |
45 | |
46 | |
47 | |
48 def Clone( self ): | |
49 return TextOnlyValidator() | |
50 | |
51 | |
52 | |
53 def Validate( self, win ): | |
54 tc = self.GetWindow() | |
55 val = tc.GetValue() | |
56 | |
57 retVal = True | |
58 for x in val: | |
59 if x not in string.letters: | |
60 retVal = False | |
61 break | |
62 | |
63 return retVal | |
64 | |
65 | |
66 | |
67 def onChar( self, event ): | |
68 key = event.GetKeyCode() | |
69 if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: | |
70 event.Skip() | |
71 | |
72 elif chr(key) in string.letters: | |
73 event.Skip() | |
74 | |
75 else: | |
76 if not wxValidator_IsSilent(): | |
77 wxBell() | |
78 | |
79 # Returning without calling even. Skip eats the event before it | |
80 # gets to the text control | |
81 return | |
82 | |
83 | |
84 | |
85 ## | |
86 ## Number Only input (no text allowed) | |
87 ## | |
88 class NumberOnlyValidator(wx.PyValidator): | |
89 def __init__( self ): | |
90 wx.PyValidator.__init__( self ) | |
91 self.Bind(wx.EVT_CHAR, self.onChar) | |
92 | |
93 | |
94 | |
95 def Clone( self ): | |
96 return NumberOnlyValidator() | |
97 | |
98 | |
99 | |
100 def Validate( self, win ): | |
101 tc = self.GetWindow() | |
102 val = tc.GetValue() | |
103 | |
104 retVal = True | |
105 for x in val: | |
106 if x not in string.digits: | |
107 retVal = False | |
108 break | |
109 | |
110 return retVal | |
111 | |
112 | |
113 | |
114 def onChar( self, event ): | |
115 key = event.GetKeyCode() | |
116 if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: | |
117 event.Skip() | |
118 | |
119 elif chr(key) in string.digits: | |
120 event.Skip() | |
121 | |
122 else: | |
123 if not wxValidator_IsSilent(): | |
124 wxBell() | |
125 | |
126 # Returning without calling even. Skip eats the event before it | |
127 # gets to the text control | |
128 return | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | |
135 ## | |
136 ## Math Only input (no text allowed, only numbers of math symbols) | |
137 ## | |
138 class MathOnlyValidator(wx.PyValidator): | |
139 def __init__( self ): | |
140 wx.PyValidator.__init__( self ) | |
141 | |
142 # Build it as part of the class and not per Validate() call | |
143 self.allowedInput = "0123456789()*/+-<>" | |
144 self.Bind(wx.EVT_CHAR, self.onChar) | |
145 | |
146 | |
147 | |
148 def Clone( self ): | |
149 return MathOnlyValidator() | |
150 | |
151 | |
152 | |
153 def Validate( self, win ): | |
154 tc = self.GetWindow() | |
155 val = tc.GetValue() | |
156 | |
157 retVal = True | |
158 for x in val: | |
159 if x not in self.allowedInput: | |
160 retVal = False | |
161 break | |
162 | |
163 return retVal | |
164 | |
165 | |
166 | |
167 def onChar( self, event ): | |
168 key = event.GetKeyCode() | |
169 if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: | |
170 event.Skip() | |
171 | |
172 elif chr(key) in self.allowedInput: | |
173 event.Skip() | |
174 | |
175 else: | |
176 if not wxValidator_IsSilent(): | |
177 wxBell() | |
178 | |
179 # Returning without calling even. Skip eats the event before it | |
180 # gets to the text control | |
181 return | |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | |
188 ## | |
189 ## Text and number input but DO NOT allow ANY HTML type input (no numbers allowed) | |
190 ## | |
191 class NoHTMLValidator(wx.PyValidator): | |
192 def __init__( self ): | |
193 wx.PyValidator.__init__( self ) | |
194 | |
195 # Build it as part of the class and not per Validate() call | |
196 self.allowedInput = " 1234567890!@#$%^&*()_-+=`~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,[]{}|;:'\",./?\\" | |
197 self.Bind(wx.EVT_CHAR, self.onChar) | |
198 | |
199 | |
200 | |
201 def Clone( self ): | |
202 return NoHTMLValidator() | |
203 | |
204 | |
205 | |
206 def Validate( self, win ): | |
207 tc = self.GetWindow() | |
208 val = tc.GetValue() | |
209 | |
210 retVal = True | |
211 for x in val: | |
212 if x not in self.allowedInput: | |
213 retVal = False | |
214 break | |
215 | |
216 return retVal | |
217 | |
218 | |
219 | |
220 def onChar( self, event ): | |
221 key = event.GetKeyCode() | |
222 if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: | |
223 event.Skip() | |
224 | |
225 elif chr(key) in self.allowedInput: | |
226 event.Skip() | |
227 | |
228 else: | |
229 if not wxValidator_IsSilent(): | |
230 wxBell() | |
231 | |
232 # Returning without calling even. Skip eats the event before it | |
233 # gets to the text control | |
234 return |