1999-09-30 03:11:20 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import string
|
|
|
|
import wx
|
1999-09-30 03:11:20 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
ALPHA_ONLY = 1
|
|
|
|
DIGIT_ONLY = 2
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class MyValidator(wx.PyValidator):
|
1999-09-30 03:11:20 -04:00
|
|
|
def __init__(self, flag=None, pyVar=None):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.PyValidator.__init__(self)
|
1999-09-30 03:11:20 -04:00
|
|
|
self.flag = flag
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_CHAR, self.OnChar)
|
1999-09-30 03:11:20 -04:00
|
|
|
|
|
|
|
def Clone(self):
|
|
|
|
return MyValidator(self.flag)
|
|
|
|
|
|
|
|
def Validate(self, win):
|
2001-05-17 18:47:09 -04:00
|
|
|
tc = self.GetWindow()
|
1999-09-30 03:11:20 -04:00
|
|
|
val = tc.GetValue()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-30 03:11:20 -04:00
|
|
|
if self.flag == ALPHA_ONLY:
|
|
|
|
for x in val:
|
|
|
|
if x not in string.letters:
|
2003-03-25 01:35:27 -05:00
|
|
|
return False
|
1999-09-30 03:11:20 -04:00
|
|
|
|
|
|
|
elif self.flag == DIGIT_ONLY:
|
|
|
|
for x in val:
|
|
|
|
if x not in string.digits:
|
2003-03-25 01:35:27 -05:00
|
|
|
return False
|
1999-09-30 03:11:20 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
return True
|
1999-09-30 03:11:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
def OnChar(self, event):
|
|
|
|
key = event.KeyCode()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
|
1999-09-30 03:11:20 -04:00
|
|
|
event.Skip()
|
|
|
|
return
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-30 03:11:20 -04:00
|
|
|
if self.flag == ALPHA_ONLY and chr(key) in string.letters:
|
|
|
|
event.Skip()
|
|
|
|
return
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-30 03:11:20 -04:00
|
|
|
if self.flag == DIGIT_ONLY and chr(key) in string.digits:
|
|
|
|
event.Skip()
|
|
|
|
return
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
if not wx.Validator_IsSilent():
|
|
|
|
wx.Bell()
|
1999-09-30 03:11:20 -04:00
|
|
|
|
|
|
|
# Returning without calling even.Skip eats the event before it
|
|
|
|
# gets to the text control
|
|
|
|
return
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestValidatorPanel(wx.Panel):
|
1999-09-30 03:11:20 -04:00
|
|
|
def __init__(self, parent):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
1999-09-30 03:11:20 -04:00
|
|
|
VSPACE = 10
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
fgs = wx.FlexGridSizer(0, 2)
|
1999-09-30 03:11:20 -04:00
|
|
|
|
2003-11-21 02:36:53 -05:00
|
|
|
fgs.Add((1,1))
|
2003-12-11 14:55:48 -05:00
|
|
|
fgs.Add(wx.StaticText(self, -1, "These controls have validators that limit\n"
|
1999-09-30 03:11:20 -04:00
|
|
|
"the type of characters that can be entered."))
|
|
|
|
|
2003-11-21 02:36:53 -05:00
|
|
|
fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
|
1999-09-30 03:11:20 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
label = wx.StaticText(self, -1, "Alpha Only: ")
|
|
|
|
fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER)
|
1999-09-30 03:11:20 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
fgs.Add(wx.TextCtrl(self, -1, "", validator = MyValidator(ALPHA_ONLY)))
|
1999-09-30 03:11:20 -04:00
|
|
|
|
2003-11-21 02:36:53 -05:00
|
|
|
fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
|
1999-09-30 03:11:20 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
label = wx.StaticText(self, -1, "Digits Only: ")
|
2003-12-11 14:55:48 -05:00
|
|
|
fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER)
|
2003-12-08 20:23:28 -05:00
|
|
|
fgs.Add(wx.TextCtrl(self, -1, "", validator = MyValidator(DIGIT_ONLY)))
|
1999-09-30 03:11:20 -04:00
|
|
|
|
2003-11-21 02:36:53 -05:00
|
|
|
fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
|
|
|
|
fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
|
|
|
|
fgs.Add((0,0))
|
2003-12-08 20:23:28 -05:00
|
|
|
b = wx.Button(self, -1, "Test Dialog Validation")
|
2003-12-11 14:55:48 -05:00
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnDoDialog, b)
|
2001-05-17 18:47:09 -04:00
|
|
|
fgs.Add(b)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
border = wx.BoxSizer()
|
|
|
|
border.Add(fgs, 1, wx.GROW|wx.ALL, 25)
|
1999-09-30 03:11:20 -04:00
|
|
|
self.SetSizer(border)
|
|
|
|
self.Layout()
|
|
|
|
|
2001-05-17 18:47:09 -04:00
|
|
|
def OnDoDialog(self, evt):
|
|
|
|
dlg = TestValidateDialog(self)
|
|
|
|
dlg.ShowModal()
|
|
|
|
dlg.Destroy()
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TextObjectValidator(wx.PyValidator):
|
2001-05-17 18:47:09 -04:00
|
|
|
""" This validator is used to ensure that the user has entered something
|
|
|
|
into the text object editor dialog's text field.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
""" Standard constructor.
|
|
|
|
"""
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.PyValidator.__init__(self)
|
2001-05-17 18:47:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def Clone(self):
|
|
|
|
""" Standard cloner.
|
|
|
|
|
|
|
|
Note that every validator must implement the Clone() method.
|
|
|
|
"""
|
|
|
|
return TextObjectValidator()
|
|
|
|
|
|
|
|
|
|
|
|
def Validate(self, win):
|
|
|
|
""" Validate the contents of the given text control.
|
|
|
|
"""
|
|
|
|
textCtrl = self.GetWindow()
|
|
|
|
text = textCtrl.GetValue()
|
|
|
|
|
|
|
|
if len(text) == 0:
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.MessageBox("A text object must contain some text!", "Error")
|
2002-06-20 17:42:30 -04:00
|
|
|
textCtrl.SetBackgroundColour("pink")
|
2001-05-17 18:47:09 -04:00
|
|
|
textCtrl.SetFocus()
|
2002-06-20 17:42:30 -04:00
|
|
|
textCtrl.Refresh()
|
2003-03-25 01:35:27 -05:00
|
|
|
return False
|
2001-05-17 18:47:09 -04:00
|
|
|
else:
|
2002-06-20 17:42:30 -04:00
|
|
|
textCtrl.SetBackgroundColour(
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
|
2002-06-20 17:42:30 -04:00
|
|
|
textCtrl.Refresh()
|
2003-03-25 01:35:27 -05:00
|
|
|
return True
|
2001-05-17 18:47:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
def TransferToWindow(self):
|
|
|
|
""" Transfer data from validator to window.
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The default implementation returns False, indicating that an error
|
|
|
|
occurred. We simply return True, as we don't do any data transfer.
|
2001-05-17 18:47:09 -04:00
|
|
|
"""
|
2003-03-25 01:35:27 -05:00
|
|
|
return True # Prevent wxDialog from complaining.
|
2001-05-17 18:47:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
def TransferFromWindow(self):
|
|
|
|
""" Transfer data from window to validator.
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
The default implementation returns False, indicating that an error
|
|
|
|
occurred. We simply return True, as we don't do any data transfer.
|
2001-05-17 18:47:09 -04:00
|
|
|
"""
|
2003-03-25 01:35:27 -05:00
|
|
|
return True # Prevent wxDialog from complaining.
|
2001-05-17 18:47:09 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestValidateDialog(wx.Dialog):
|
2001-05-17 18:47:09 -04:00
|
|
|
def __init__(self, parent):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Dialog.__init__(self, parent, -1, "Validated Dialog")
|
2001-05-17 18:47:09 -04:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
self.SetAutoLayout(True)
|
2001-05-17 18:47:09 -04:00
|
|
|
VSPACE = 10
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
fgs = wx.FlexGridSizer(0, 2)
|
2001-05-17 18:47:09 -04:00
|
|
|
|
2003-11-21 02:36:53 -05:00
|
|
|
fgs.Add((1,1));
|
2003-12-11 14:55:48 -05:00
|
|
|
fgs.Add(wx.StaticText(self, -1,
|
2001-05-17 18:47:09 -04:00
|
|
|
"These controls must have text entered into them. Each\n"
|
|
|
|
"one has a validator that is checked when the Okay\n"
|
|
|
|
"button is clicked."))
|
|
|
|
|
2003-11-21 02:36:53 -05:00
|
|
|
fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
|
2001-05-17 18:47:09 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
label = wx.StaticText(self, -1, "First: ")
|
|
|
|
fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER)
|
2001-05-17 18:47:09 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
fgs.Add(wx.TextCtrl(self, -1, "", validator = TextObjectValidator()))
|
2001-05-17 18:47:09 -04:00
|
|
|
|
2003-11-21 02:36:53 -05:00
|
|
|
fgs.Add((1,VSPACE)); fgs.Add((1,VSPACE))
|
2001-05-17 18:47:09 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
label = wx.StaticText(self, -1, "Second: ")
|
|
|
|
fgs.Add(label, 0, wx.ALIGN_RIGHT|wx.CENTER)
|
|
|
|
fgs.Add(wx.TextCtrl(self, -1, "", validator = TextObjectValidator()))
|
2001-05-17 18:47:09 -04:00
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
buttons = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
b = wx.Button(self, wx.ID_OK, "Okay")
|
2002-06-20 17:42:30 -04:00
|
|
|
b.SetDefault()
|
2003-12-08 20:23:28 -05:00
|
|
|
buttons.Add(b, 0, wx.ALL, 10)
|
|
|
|
buttons.Add(wx.Button(self, wx.ID_CANCEL, "Cancel"), 0, wx.ALL, 10)
|
2001-05-17 18:47:09 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
border = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
border.Add(fgs, 1, wx.GROW|wx.ALL, 25)
|
2001-05-17 18:47:09 -04:00
|
|
|
border.Add(buttons)
|
|
|
|
self.SetSizer(border)
|
|
|
|
border.Fit(self)
|
|
|
|
self.Layout()
|
|
|
|
|
1999-09-30 03:11:20 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = TestValidatorPanel(nb)
|
|
|
|
return win
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
2003-12-08 20:23:28 -05:00
|
|
|
<html>
|
|
|
|
<body>
|
2004-01-12 22:17:17 -05:00
|
|
|
wx.Validator is the base class for a family of validator classes that mediate
|
2003-12-08 20:23:28 -05:00
|
|
|
between a class of control, and application data.
|
|
|
|
|
|
|
|
<p>A validator has three major roles:
|
|
|
|
|
|
|
|
<p><ol>
|
|
|
|
<li>to transfer data from a C++ variable or own storage to and from a control;
|
|
|
|
<li>to validate data in a control, and show an appropriate error message;
|
|
|
|
<li>to filter events (such as keystrokes), thereby changing the behaviour of the associated control.
|
|
|
|
</ol>
|
|
|
|
<p>Validators can be plugged into controls dynamically.
|
|
|
|
</body>
|
|
|
|
</html>
|
1999-09-30 03:11:20 -04:00
|
|
|
"""
|
2003-03-25 01:35:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
2003-03-25 01:35:27 -05:00
|
|
|
|