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