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