]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxValidator.py
Added PyPlot
[wxWidgets.git] / wxPython / demo / wxValidator.py
1 # 11/22/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import string
7 import wx
8
9
10 #----------------------------------------------------------------------
11
12 ALPHA_ONLY = 1
13 DIGIT_ONLY = 2
14
15 class MyValidator(wx.PyValidator):
16 def __init__(self, flag=None, pyVar=None):
17 wx.PyValidator.__init__(self)
18 self.flag = flag
19 self.Bind(wx.EVT_CHAR, self.OnChar)
20
21 def Clone(self):
22 return MyValidator(self.flag)
23
24 def Validate(self, win):
25 tc = self.GetWindow()
26 val = tc.GetValue()
27
28 if self.flag == ALPHA_ONLY:
29 for x in val:
30 if x not in string.letters:
31 return False
32
33 elif self.flag == DIGIT_ONLY:
34 for x in val:
35 if x not in string.digits:
36 return False
37
38 return True
39
40
41 def OnChar(self, event):
42 key = event.KeyCode()
43
44 if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
45 event.Skip()
46 return
47
48 if self.flag == ALPHA_ONLY and chr(key) in string.letters:
49 event.Skip()
50 return
51
52 if self.flag == DIGIT_ONLY and chr(key) in string.digits:
53 event.Skip()
54 return
55
56 if not wx.Validator_IsSilent():
57 wx.Bell()
58
59 # Returning without calling even.Skip eats the event before it
60 # gets to the text control
61 return
62
63 #----------------------------------------------------------------------
64
65 class TestValidatorPanel(wx.Panel):
66 def __init__(self, parent):
67 wx.Panel.__init__(self, parent, -1)
68 self.SetAutoLayout(True)
69 VSPACE = 10
70
71 fgs = wx.FlexGridSizer(0, 2)
72
73 fgs.Add((1,1))
74 fgs.Add(wx.StaticText(self, -1, "These controls have validators that limit\n"
75 "the type of characters that can be entered."))
76
77 fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
78
79 label = wx.StaticText(self, -1, "Alpha Only: ")
80 fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER)
81
82 fgs.Add(wx.TextCtrl(self, -1, "", validator = MyValidator(ALPHA_ONLY)))
83
84 fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
85
86 label = wx.StaticText(self, -1, "Digits Only: ")
87 fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER)
88 fgs.Add(wx.TextCtrl(self, -1, "", validator = MyValidator(DIGIT_ONLY)))
89
90 fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
91 fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
92 fgs.Add((0,0))
93 b = wx.Button(self, -1, "Test Dialog Validation")
94 self.Bind(wx.EVT_BUTTON, self.OnDoDialog, b)
95 fgs.Add(b)
96
97 border = wx.BoxSizer()
98 border.Add(fgs, 1, wx.GROW|wx.ALL, 25)
99 self.SetSizer(border)
100 self.Layout()
101
102 def OnDoDialog(self, evt):
103 dlg = TestValidateDialog(self)
104 dlg.ShowModal()
105 dlg.Destroy()
106
107
108 #----------------------------------------------------------------------
109
110 class TextObjectValidator(wx.PyValidator):
111 """ This validator is used to ensure that the user has entered something
112 into the text object editor dialog's text field.
113 """
114 def __init__(self):
115 """ Standard constructor.
116 """
117 wx.PyValidator.__init__(self)
118
119
120
121 def Clone(self):
122 """ Standard cloner.
123
124 Note that every validator must implement the Clone() method.
125 """
126 return TextObjectValidator()
127
128
129 def Validate(self, win):
130 """ Validate the contents of the given text control.
131 """
132 textCtrl = self.GetWindow()
133 text = textCtrl.GetValue()
134
135 if len(text) == 0:
136 wx.MessageBox("A text object must contain some text!", "Error")
137 textCtrl.SetBackgroundColour("pink")
138 textCtrl.SetFocus()
139 textCtrl.Refresh()
140 return False
141 else:
142 textCtrl.SetBackgroundColour(
143 wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
144 textCtrl.Refresh()
145 return True
146
147
148 def TransferToWindow(self):
149 """ Transfer data from validator to window.
150
151 The default implementation returns False, indicating that an error
152 occurred. We simply return True, as we don't do any data transfer.
153 """
154 return True # Prevent wxDialog from complaining.
155
156
157 def TransferFromWindow(self):
158 """ Transfer data from window to validator.
159
160 The default implementation returns False, indicating that an error
161 occurred. We simply return True, as we don't do any data transfer.
162 """
163 return True # Prevent wxDialog from complaining.
164
165 #----------------------------------------------------------------------
166
167 class TestValidateDialog(wx.Dialog):
168 def __init__(self, parent):
169 wx.Dialog.__init__(self, parent, -1, "Validated Dialog")
170
171 self.SetAutoLayout(True)
172 VSPACE = 10
173
174 fgs = wx.FlexGridSizer(0, 2)
175
176 fgs.Add((1,1));
177 fgs.Add(wx.StaticText(self, -1,
178 "These controls must have text entered into them. Each\n"
179 "one has a validator that is checked when the Okay\n"
180 "button is clicked."))
181
182 fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
183
184 label = wx.StaticText(self, -1, "First: ")
185 fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER)
186
187 fgs.Add(wx.TextCtrl(self, -1, "", validator = TextObjectValidator()))
188
189 fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
190
191 label = wx.StaticText(self, -1, "Second: ")
192 fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER)
193 fgs.Add(wx.TextCtrl(self, -1, "", validator = TextObjectValidator()))
194
195
196 buttons = wx.BoxSizer(wx.HORIZONTAL)
197 b = wx.Button(self, wx.ID_OK, "Okay")
198 b.SetDefault()
199 buttons.Add(b, 0, wx.ALL, 10)
200 buttons.Add(wx.Button(self, wx.ID_CANCEL, "Cancel"), 0, wx.ALL, 10)
201
202 border = wx.BoxSizer(wx.VERTICAL)
203 border.Add(fgs, 1, wx.GROW|wx.ALL, 25)
204 border.Add(buttons)
205 self.SetSizer(border)
206 border.Fit(self)
207 self.Layout()
208
209
210 #----------------------------------------------------------------------
211
212 def runTest(frame, nb, log):
213 win = TestValidatorPanel(nb)
214 return win
215
216 #----------------------------------------------------------------------
217
218
219
220 overview = """\
221 <html>
222 <body>
223 wxValidator is the base class for a family of validator classes that mediate
224 between a class of control, and application data.
225
226 <p>A validator has three major roles:
227
228 <p><ol>
229 <li>to transfer data from a C++ variable or own storage to and from a control;
230 <li>to validate data in a control, and show an appropriate error message;
231 <li>to filter events (such as keystrokes), thereby changing the behaviour of the associated control.
232 </ol>
233 <p>Validators can be plugged into controls dynamically.
234 </body>
235 </html>
236 """
237
238
239
240 if __name__ == '__main__':
241 import sys,os
242 import run
243 run.main(['', os.path.basename(sys.argv[0])])
244