]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | # 11/22/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # | |
2f90df85 | 5 | |
8fa876ca RD |
6 | import string |
7 | import wx | |
2f90df85 | 8 | |
2f90df85 RD |
9 | |
10 | #---------------------------------------------------------------------- | |
11 | ||
12 | ALPHA_ONLY = 1 | |
13 | DIGIT_ONLY = 2 | |
14 | ||
8fa876ca | 15 | class MyValidator(wx.PyValidator): |
2f90df85 | 16 | def __init__(self, flag=None, pyVar=None): |
8fa876ca | 17 | wx.PyValidator.__init__(self) |
2f90df85 | 18 | self.flag = flag |
8fa876ca | 19 | self.Bind(wx.EVT_CHAR, self.OnChar) |
2f90df85 RD |
20 | |
21 | def Clone(self): | |
22 | return MyValidator(self.flag) | |
23 | ||
24 | def Validate(self, win): | |
9416aa89 | 25 | tc = self.GetWindow() |
2f90df85 | 26 | val = tc.GetValue() |
8fa876ca | 27 | |
2f90df85 RD |
28 | if self.flag == ALPHA_ONLY: |
29 | for x in val: | |
30 | if x not in string.letters: | |
1e4a197e | 31 | return False |
2f90df85 RD |
32 | |
33 | elif self.flag == DIGIT_ONLY: | |
34 | for x in val: | |
35 | if x not in string.digits: | |
1e4a197e | 36 | return False |
2f90df85 | 37 | |
1e4a197e | 38 | return True |
2f90df85 RD |
39 | |
40 | ||
41 | def OnChar(self, event): | |
42 | key = event.KeyCode() | |
8fa876ca RD |
43 | |
44 | if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255: | |
2f90df85 RD |
45 | event.Skip() |
46 | return | |
8fa876ca | 47 | |
2f90df85 RD |
48 | if self.flag == ALPHA_ONLY and chr(key) in string.letters: |
49 | event.Skip() | |
50 | return | |
8fa876ca | 51 | |
2f90df85 RD |
52 | if self.flag == DIGIT_ONLY and chr(key) in string.digits: |
53 | event.Skip() | |
54 | return | |
55 | ||
8fa876ca RD |
56 | if not wx.Validator_IsSilent(): |
57 | wx.Bell() | |
2f90df85 RD |
58 | |
59 | # Returning without calling even.Skip eats the event before it | |
60 | # gets to the text control | |
61 | return | |
62 | ||
63 | #---------------------------------------------------------------------- | |
64 | ||
8fa876ca | 65 | class TestValidatorPanel(wx.Panel): |
2f90df85 | 66 | def __init__(self, parent): |
8fa876ca | 67 | wx.Panel.__init__(self, parent, -1) |
1e4a197e | 68 | self.SetAutoLayout(True) |
2f90df85 RD |
69 | VSPACE = 10 |
70 | ||
8fa876ca | 71 | fgs = wx.FlexGridSizer(0, 2) |
2f90df85 | 72 | |
fbd5dd1d | 73 | fgs.Add((1,1)) |
372bde9b | 74 | fgs.Add(wx.StaticText(self, -1, "These controls have validators that limit\n" |
2f90df85 RD |
75 | "the type of characters that can be entered.")) |
76 | ||
fbd5dd1d | 77 | fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE)) |
2f90df85 | 78 | |
8fa876ca RD |
79 | label = wx.StaticText(self, -1, "Alpha Only: ") |
80 | fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER) | |
2f90df85 | 81 | |
8fa876ca | 82 | fgs.Add(wx.TextCtrl(self, -1, "", validator = MyValidator(ALPHA_ONLY))) |
2f90df85 | 83 | |
fbd5dd1d | 84 | fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE)) |
2f90df85 | 85 | |
8fa876ca | 86 | label = wx.StaticText(self, -1, "Digits Only: ") |
372bde9b | 87 | fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER) |
8fa876ca | 88 | fgs.Add(wx.TextCtrl(self, -1, "", validator = MyValidator(DIGIT_ONLY))) |
2f90df85 | 89 | |
fbd5dd1d RD |
90 | fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE)) |
91 | fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE)) | |
92 | fgs.Add((0,0)) | |
8fa876ca | 93 | b = wx.Button(self, -1, "Test Dialog Validation") |
372bde9b | 94 | self.Bind(wx.EVT_BUTTON, self.OnDoDialog, b) |
9416aa89 RD |
95 | fgs.Add(b) |
96 | ||
8fa876ca RD |
97 | border = wx.BoxSizer() |
98 | border.Add(fgs, 1, wx.GROW|wx.ALL, 25) | |
2f90df85 RD |
99 | self.SetSizer(border) |
100 | self.Layout() | |
101 | ||
9416aa89 RD |
102 | def OnDoDialog(self, evt): |
103 | dlg = TestValidateDialog(self) | |
104 | dlg.ShowModal() | |
105 | dlg.Destroy() | |
106 | ||
107 | ||
108 | #---------------------------------------------------------------------- | |
109 | ||
8fa876ca | 110 | class TextObjectValidator(wx.PyValidator): |
9416aa89 RD |
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 | """ | |
8fa876ca | 117 | wx.PyValidator.__init__(self) |
9416aa89 RD |
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: | |
8fa876ca | 136 | wx.MessageBox("A text object must contain some text!", "Error") |
9de6ef00 | 137 | textCtrl.SetBackgroundColour("pink") |
9416aa89 | 138 | textCtrl.SetFocus() |
9de6ef00 | 139 | textCtrl.Refresh() |
1e4a197e | 140 | return False |
9416aa89 | 141 | else: |
9de6ef00 | 142 | textCtrl.SetBackgroundColour( |
8fa876ca | 143 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
9de6ef00 | 144 | textCtrl.Refresh() |
1e4a197e | 145 | return True |
9416aa89 RD |
146 | |
147 | ||
148 | def TransferToWindow(self): | |
149 | """ Transfer data from validator to window. | |
150 | ||
1e4a197e RD |
151 | The default implementation returns False, indicating that an error |
152 | occurred. We simply return True, as we don't do any data transfer. | |
9416aa89 | 153 | """ |
1e4a197e | 154 | return True # Prevent wxDialog from complaining. |
9416aa89 RD |
155 | |
156 | ||
157 | def TransferFromWindow(self): | |
158 | """ Transfer data from window to validator. | |
159 | ||
1e4a197e RD |
160 | The default implementation returns False, indicating that an error |
161 | occurred. We simply return True, as we don't do any data transfer. | |
9416aa89 | 162 | """ |
1e4a197e | 163 | return True # Prevent wxDialog from complaining. |
9416aa89 RD |
164 | |
165 | #---------------------------------------------------------------------- | |
166 | ||
8fa876ca | 167 | class TestValidateDialog(wx.Dialog): |
9416aa89 | 168 | def __init__(self, parent): |
8fa876ca | 169 | wx.Dialog.__init__(self, parent, -1, "Validated Dialog") |
9416aa89 | 170 | |
1e4a197e | 171 | self.SetAutoLayout(True) |
9416aa89 RD |
172 | VSPACE = 10 |
173 | ||
8fa876ca | 174 | fgs = wx.FlexGridSizer(0, 2) |
9416aa89 | 175 | |
fbd5dd1d | 176 | fgs.Add((1,1)); |
372bde9b | 177 | fgs.Add(wx.StaticText(self, -1, |
9416aa89 RD |
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 | ||
fbd5dd1d | 182 | fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE)) |
9416aa89 | 183 | |
8fa876ca RD |
184 | label = wx.StaticText(self, -1, "First: ") |
185 | fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER) | |
9416aa89 | 186 | |
8fa876ca | 187 | fgs.Add(wx.TextCtrl(self, -1, "", validator = TextObjectValidator())) |
9416aa89 | 188 | |
fbd5dd1d | 189 | fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE)) |
9416aa89 | 190 | |
8fa876ca RD |
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())) | |
9416aa89 RD |
194 | |
195 | ||
8fa876ca RD |
196 | buttons = wx.BoxSizer(wx.HORIZONTAL) |
197 | b = wx.Button(self, wx.ID_OK, "Okay") | |
9de6ef00 | 198 | b.SetDefault() |
8fa876ca RD |
199 | buttons.Add(b, 0, wx.ALL, 10) |
200 | buttons.Add(wx.Button(self, wx.ID_CANCEL, "Cancel"), 0, wx.ALL, 10) | |
9416aa89 | 201 | |
8fa876ca RD |
202 | border = wx.BoxSizer(wx.VERTICAL) |
203 | border.Add(fgs, 1, wx.GROW|wx.ALL, 25) | |
9416aa89 RD |
204 | border.Add(buttons) |
205 | self.SetSizer(border) | |
206 | border.Fit(self) | |
207 | self.Layout() | |
208 | ||
2f90df85 RD |
209 | |
210 | #---------------------------------------------------------------------- | |
211 | ||
212 | def runTest(frame, nb, log): | |
213 | win = TestValidatorPanel(nb) | |
214 | return win | |
215 | ||
216 | #---------------------------------------------------------------------- | |
217 | ||
218 | ||
219 | ||
2f90df85 | 220 | overview = """\ |
8fa876ca RD |
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> | |
2f90df85 | 236 | """ |
1e4a197e RD |
237 | |
238 | ||
239 | ||
240 | if __name__ == '__main__': | |
241 | import sys,os | |
242 | import run | |
243 | run.main(['', os.path.basename(sys.argv[0])]) | |
244 |