]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/demo/wxValidator.py
Fixes to avoid an endless event looping for wxGTK
[wxWidgets.git] / utils / 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):
22 print 'validate'
23 tc = wxPyTypeCast(win, "wxTextCtrl")
24 val = tc.GetValue()
25 if self.flag == ALPHA_ONLY:
26 for x in val:
27 if x not in string.letters:
28 return false
29
30 elif self.flag == DIGIT_ONLY:
31 for x in val:
32 if x not in string.digits:
33 return false
34
35 return true
36
37
38 def OnChar(self, event):
39 key = event.KeyCode()
40 if key < WXK_SPACE or key == WXK_DELETE or key > 255:
41 event.Skip()
42 return
43 if self.flag == ALPHA_ONLY and chr(key) in string.letters:
44 event.Skip()
45 return
46 if self.flag == DIGIT_ONLY and chr(key) in string.digits:
47 event.Skip()
48 return
49
50 if not wxValidator_IsSilent():
51 wxBell()
52
53 # Returning without calling even.Skip eats the event before it
54 # gets to the text control
55 return
56
57#----------------------------------------------------------------------
58
59class TestValidatorPanel(wxPanel):
60 def __init__(self, parent):
61 wxPanel.__init__(self, parent, -1)
62 self.SetAutoLayout(true)
63 VSPACE = 10
64
65 fgs = wxFlexGridSizer(0, 2)
66
67 fgs.Add(1,1);
68 fgs.Add(wxStaticText(self, -1, "These controls have validators that limit\n"
69 "the type of characters that can be entered."))
70
71 fgs.Add(1,VSPACE); fgs.Add(1,VSPACE)
72
73 label = wxStaticText(self, -1, "Alpha Only: ")
74 fgs.Add(label, 0, wxALIGN_RIGHT|wxCENTER)
75
e9183621 76 fgs.Add(wxTextCtrl(self, -1, "", validator = MyValidator(ALPHA_ONLY)))
2f90df85
RD
77
78 fgs.Add(1,VSPACE); fgs.Add(1,VSPACE)
79
80 label = wxStaticText(self, -1, "Digits Only: ")
81 fgs.Add(label, 0, wxALIGN_RIGHT|wxCENTER)
e9183621 82 fgs.Add(wxTextCtrl(self, -1, "", validator = MyValidator(DIGIT_ONLY)))
2f90df85
RD
83
84 border = wxBoxSizer()
85 border.Add(fgs, 1, wxGROW|wxALL, 25)
86 self.SetSizer(border)
87 self.Layout()
88
89
90#----------------------------------------------------------------------
91
92def runTest(frame, nb, log):
93 win = TestValidatorPanel(nb)
94 return win
95
96#----------------------------------------------------------------------
97
98
99
100
101
102
103
104
105
106overview = """\
107wxValidator is the base class for a family of validator classes that mediate between a class of control, and application data.
108
109A validator has three major roles:
110
1111. to transfer data from a C++ variable or own storage to and from a control;
112
1132. to validate data in a control, and show an appropriate error message;
114
1153. to filter events (such as keystrokes), thereby changing the behaviour of the associated control.
116
117Validators can be plugged into controls dynamically.
118
119"""