]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | # 11/23/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # | |
5 | # 11/26/2003 - Jeff Grimmett (grimmtooth@softhome.net) | |
6 | # | |
7 | # o the three libraries below all have not been hit by the | |
8 | # wx renamer. | |
9 | # | |
10 | ||
11 | import string | |
12 | import sys | |
13 | import traceback | |
14 | ||
15 | import wx | |
16 | import wx.lib.maskededit as med | |
17 | import wx.lib.maskedctrl as mctl | |
18 | import wx.lib.scrolledpanel as scroll | |
1fded56b | 19 | |
8b9a4190 | 20 | |
1fded56b RD |
21 | class demoMixin: |
22 | """ | |
23 | Centralized routines common to demo pages, to remove repetition. | |
24 | """ | |
25 | def labelGeneralTable(self, sizer): | |
8fa876ca RD |
26 | description = wx.StaticText( self, -1, "Description", ) |
27 | mask = wx.StaticText( self, -1, "Mask Value" ) | |
28 | formatcode = wx.StaticText( self, -1, "Format" ) | |
29 | regex = wx.StaticText( self, -1, "Regexp Validator(opt.)" ) | |
30 | ctrl = wx.StaticText( self, -1, "wxMaskedTextCtrl" ) | |
31 | ||
32 | description.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD)) | |
33 | mask.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD)) | |
34 | formatcode.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD) ) | |
35 | regex.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD)) | |
36 | ctrl.SetFont( wx.Font(9, wx.SWISS, wx.NORMAL, wx.BOLD)) | |
1fded56b RD |
37 | |
38 | sizer.Add(description) | |
39 | sizer.Add(mask) | |
40 | sizer.Add(formatcode) | |
41 | sizer.Add(regex) | |
42 | sizer.Add(ctrl) | |
43 | ||
44 | ||
45 | def layoutGeneralTable(self, controls, sizer): | |
46 | for control in controls: | |
8fa876ca RD |
47 | sizer.Add( wx.StaticText( self, -1, control[0]) ) |
48 | sizer.Add( wx.StaticText( self, -1, control[1]) ) | |
49 | sizer.Add( wx.StaticText( self, -1, control[3]) ) | |
50 | sizer.Add( wx.StaticText( self, -1, control[4]) ) | |
1fded56b RD |
51 | |
52 | if control in controls: | |
8fa876ca | 53 | newControl = med.wxMaskedTextCtrl( self, -1, "", |
1fded56b RD |
54 | mask = control[1], |
55 | excludeChars = control[2], | |
56 | formatcodes = control[3], | |
57 | includeChars = "", | |
58 | validRegex = control[4], | |
59 | validRange = control[5], | |
60 | choices = control[6], | |
61 | choiceRequired = True, | |
62 | defaultValue = control[7], | |
63 | demo = True, | |
64 | name = control[0]) | |
65 | self.editList.append(newControl) | |
66 | sizer.Add(newControl) | |
67 | ||
68 | ||
69 | def changeControlParams(self, event, parameter, checked_value, notchecked_value): | |
8fa876ca | 70 | if event.IsChecked(): value = checked_value |
1fded56b | 71 | else: value = notchecked_value |
8fa876ca | 72 | |
1fded56b | 73 | kwargs = {parameter: value} |
8fa876ca | 74 | |
1fded56b RD |
75 | for control in self.editList: |
76 | control.SetCtrlParameters(**kwargs) | |
77 | control.Refresh() | |
8fa876ca | 78 | |
1fded56b RD |
79 | self.Refresh() |
80 | ||
81 | ||
82 | ||
83 | #---------------------------------------------------------------------------- | |
8fa876ca | 84 | class demoPage1(scroll.wxScrolledPanel, demoMixin): |
1fded56b | 85 | def __init__(self, parent, log): |
8fa876ca RD |
86 | scroll.wxScrolledPanel.__init__(self, parent, -1) |
87 | self.sizer = wx.BoxSizer( wx.VERTICAL ) | |
1fded56b RD |
88 | self.editList = [] |
89 | ||
8fa876ca RD |
90 | label = wx.StaticText( self, -1, """\ |
91 | Here are some basic MaskedTextCtrls to give you an idea of what you can do | |
1fded56b RD |
92 | with this control. Note that all controls have been auto-sized by including 'F' in |
93 | the format codes. | |
94 | ||
95 | Try entering nonsensical or partial values in validated fields to see what happens. | |
96 | Note that the State and Last Name fields are list-limited (valid last names are: | |
97 | Smith, Jones, Williams). Signs on numbers can be toggled with the minus key. | |
98 | """) | |
99 | label.SetForegroundColour( "Blue" ) | |
8fa876ca RD |
100 | header = wx.BoxSizer( wx.HORIZONTAL ) |
101 | header.Add( label, 0, flag=wx.ALIGN_LEFT|wx.ALL, border = 5 ) | |
1fded56b | 102 | |
8fa876ca RD |
103 | highlight = wx.CheckBox( self, -1, "Highlight Empty" ) |
104 | disallow = wx.CheckBox( self, -1, "Disallow Empty" ) | |
105 | showFill = wx.CheckBox( self, -1, "change fillChar" ) | |
1fded56b | 106 | |
8fa876ca RD |
107 | vbox = wx.BoxSizer( wx.VERTICAL ) |
108 | vbox.Add( highlight, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) | |
109 | vbox.Add( disallow, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) | |
110 | vbox.Add( showFill, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) | |
111 | header.Add((15, 0)) | |
112 | header.Add(vbox, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 ) | |
1fded56b | 113 | |
8fa876ca RD |
114 | self.Bind(wx.EVT_CHECKBOX, self.onHighlightEmpty, id=highlight.GetId()) |
115 | self.Bind(wx.EVT_CHECKBOX, self.onDisallowEmpty, id=disallow.GetId()) | |
116 | self.Bind(wx.EVT_CHECKBOX, self.onShowFill, id=showFill.GetId()) | |
1fded56b | 117 | |
8fa876ca | 118 | grid = wx.FlexGridSizer( 0, 5, vgap=10, hgap=10 ) |
1fded56b RD |
119 | self.labelGeneralTable(grid) |
120 | ||
121 | # The following list is of the controls for the demo. Feel free to play around with | |
122 | # the options! | |
123 | controls = [ | |
124 | #description mask excl format regexp range,list,initial | |
125 | ("Phone No", "(###) ###-#### x:###", "", 'F^-', "^\(\d{3}\) \d{3}-\d{4}", '','',''), | |
126 | ("Social Sec#", "###-##-####", "", 'F', "\d{3}-\d{2}-\d{4}", '','',''), | |
127 | ("Full Name", "C{14}", "", 'F_', '^[A-Z][a-zA-Z]+ [A-Z][a-zA-Z]+', '','',''), | |
128 | ("Last Name Only", "C{14}", "", 'F {list}', '^[A-Z][a-zA-Z]+', '',('Smith','Jones','Williams'),''), | |
129 | ("Zip plus 4", "#{5}-#{4}", "", 'F', "\d{5}-(\s{4}|\d{4})", '','',''), | |
130 | ("Customer No", "\CAA-###", "", 'F!', "C[A-Z]{2}-\d{3}", '','',''), | |
131 | ("Invoice Total", "#{9}.##", "", 'F-_,', "", '','',''), | |
132 | ("Integer", "#{9}", "", 'F-_', "", '','',''), | |
133 | ] | |
134 | ||
135 | self.layoutGeneralTable(controls, grid) | |
8fa876ca RD |
136 | self.sizer.Add( header, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 ) |
137 | self.sizer.Add( grid, 0, flag= wx.ALIGN_LEFT|wx.LEFT, border=5 ) | |
1fded56b RD |
138 | self.SetSizer(self.sizer) |
139 | self.SetupScrolling() | |
140 | self.SetAutoLayout(1) | |
141 | ||
142 | ||
143 | def onDisallowEmpty( self, event ): | |
144 | """ Set emptyInvalid parameter on/off """ | |
145 | self.changeControlParams( event, "emptyInvalid", True, False ) | |
146 | ||
147 | def onHighlightEmpty( self, event ): | |
148 | """ Highlight empty values""" | |
8b9a4190 | 149 | self.changeControlParams( event, "emptyBackgroundColour", "Blue", "White" ) |
1fded56b RD |
150 | |
151 | def onShowFill( self, event ): | |
152 | """ Set fillChar parameter to '?' or ' ' """ | |
153 | self.changeControlParams( event, "fillChar", '?', ' ' ) | |
154 | ||
155 | ||
8fa876ca | 156 | class demoPage2(scroll.wxScrolledPanel, demoMixin): |
1fded56b RD |
157 | def __init__( self, parent, log ): |
158 | self.log = log | |
8fa876ca RD |
159 | scroll.wxScrolledPanel.__init__( self, parent, -1 ) |
160 | self.sizer = wx.BoxSizer( wx.VERTICAL ) | |
1fded56b | 161 | |
8fa876ca | 162 | label = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
163 | All these controls have been created by passing a single parameter, the autoformat code, |
164 | and use the factory class wxMaskedCtrl with its default controlType. | |
165 | The maskededit module contains an internal dictionary of types and formats (autoformats). | |
1fded56b RD |
166 | Many of these already do complicated validation; To see some examples, try |
167 | 29 Feb 2002 vs. 2004 for the date formats, or email address validation. | |
168 | """) | |
169 | ||
170 | label.SetForegroundColour( "Blue" ) | |
8fa876ca | 171 | self.sizer.Add( label, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) |
1fded56b | 172 | |
8fa876ca RD |
173 | description = wx.StaticText( self, -1, "Description") |
174 | autofmt = wx.StaticText( self, -1, "AutoFormat Code") | |
175 | ctrl = wx.StaticText( self, -1, "MaskedCtrl") | |
1fded56b | 176 | |
8fa876ca RD |
177 | description.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) ) |
178 | autofmt.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) ) | |
179 | ctrl.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) ) | |
1fded56b | 180 | |
8fa876ca RD |
181 | grid = wx.FlexGridSizer( 0, 3, vgap=10, hgap=5 ) |
182 | grid.Add( description, 0, wx.ALIGN_LEFT ) | |
183 | grid.Add( autofmt, 0, wx.ALIGN_LEFT ) | |
184 | grid.Add( ctrl, 0, wx.ALIGN_LEFT ) | |
1fded56b | 185 | |
8fa876ca RD |
186 | for autoformat, desc in med.autoformats: |
187 | grid.Add( wx.StaticText( self, -1, desc), 0, wx.ALIGN_LEFT ) | |
188 | grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT ) | |
189 | grid.Add( mctl.wxMaskedCtrl( self, -1, "", | |
8b9a4190 RD |
190 | autoformat = autoformat, |
191 | demo = True, | |
192 | name = autoformat), | |
8fa876ca | 193 | 0, wx.ALIGN_LEFT ) |
1fded56b | 194 | |
8fa876ca | 195 | self.sizer.Add( grid, 0, wx.ALIGN_LEFT|wx.ALL, border=5 ) |
1fded56b RD |
196 | self.SetSizer( self.sizer ) |
197 | self.SetAutoLayout( 1 ) | |
198 | self.SetupScrolling() | |
199 | ||
200 | ||
8fa876ca | 201 | class demoPage3(scroll.wxScrolledPanel, demoMixin): |
1fded56b RD |
202 | def __init__(self, parent, log): |
203 | self.log = log | |
8fa876ca RD |
204 | scroll.wxScrolledPanel.__init__(self, parent, -1) |
205 | self.sizer = wx.BoxSizer( wx.VERTICAL ) | |
1fded56b RD |
206 | self.editList = [] |
207 | ||
8fa876ca | 208 | label = wx.StaticText( self, -1, """\ |
1fded56b RD |
209 | Here wxMaskedTextCtrls that have default values. The states |
210 | control has a list of valid values, and the unsigned integer | |
211 | has a legal range specified. | |
212 | """) | |
213 | label.SetForegroundColour( "Blue" ) | |
8fa876ca RD |
214 | requireValid = wx.CheckBox( self, -1, "Require Valid Value" ) |
215 | self.Bind(wx.EVT_CHECKBOX, self.onRequireValid, id=requireValid.GetId()) | |
1fded56b | 216 | |
8fa876ca RD |
217 | header = wx.BoxSizer( wx.HORIZONTAL ) |
218 | header.Add( label, 0, flag=wx.ALIGN_LEFT|wx.ALL, border = 5) | |
219 | header.Add((75, 0)) | |
220 | header.Add( requireValid, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=10 ) | |
1fded56b | 221 | |
8fa876ca | 222 | grid = wx.FlexGridSizer( 0, 5, vgap=10, hgap=10 ) |
1fded56b RD |
223 | self.labelGeneralTable( grid ) |
224 | ||
225 | controls = [ | |
226 | #description mask excl format regexp range,list,initial | |
8fa876ca | 227 | ("U.S. State (2 char)", "AA", "", 'F!_', "[A-Z]{2}", '',med.states, med.states[0]), |
8b9a4190 | 228 | ("Integer (signed)", "#{6}", "", 'F-_', "", '','', ' 0 '), |
1fded56b RD |
229 | ("Integer (unsigned)\n(1-399)","######", "", 'F_', "", (1,399),'', '1 '), |
230 | ("Float (signed)", "#{6}.#{9}", "", 'F-_R', "", '','', '000000.000000000'), | |
8fa876ca | 231 | ("Date (MDY) + Time", "##/##/#### ##:##:## AM", 'BCDEFGHIJKLMNOQRSTUVWXYZ','DF!',"", '','', wx.DateTime_Now().Format("%m/%d/%Y %I:%M:%S %p")), |
1fded56b RD |
232 | ] |
233 | self.layoutGeneralTable( controls, grid ) | |
234 | ||
8fa876ca RD |
235 | self.sizer.Add( header, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 ) |
236 | self.sizer.Add( grid, 0, flag=wx.ALIGN_LEFT|wx.ALL, border=5 ) | |
1fded56b RD |
237 | |
238 | self.SetSizer( self.sizer ) | |
239 | self.SetAutoLayout( 1 ) | |
240 | self.SetupScrolling() | |
241 | ||
242 | def onRequireValid( self, event ): | |
243 | """ Set validRequired parameter on/off """ | |
244 | self.changeControlParams( event, "validRequired", True, False ) | |
245 | ||
246 | ||
8fa876ca | 247 | class demoPage4(scroll.wxScrolledPanel, demoMixin): |
1fded56b RD |
248 | def __init__( self, parent, log ): |
249 | self.log = log | |
8fa876ca RD |
250 | scroll.wxScrolledPanel.__init__( self, parent, -1 ) |
251 | self.sizer = wx.BoxSizer( wx.VERTICAL ) | |
1fded56b | 252 | |
8fa876ca | 253 | label = wx.StaticText( self, -1, """\ |
1fded56b RD |
254 | These controls have field-specific choice lists and allow autocompletion. |
255 | ||
256 | Down arrow or Page Down in an uncompleted field with an auto-completable field will attempt | |
257 | to auto-complete a field if it has a choice list. | |
258 | Page Down and Shift-Down arrow will also auto-complete, or cycle through the complete list. | |
259 | Page Up and Shift-Up arrow will similarly cycle backwards through the list. | |
260 | """) | |
261 | ||
262 | label.SetForegroundColour( "Blue" ) | |
8fa876ca | 263 | self.sizer.Add( label, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) |
1fded56b | 264 | |
8fa876ca RD |
265 | description = wx.StaticText( self, -1, "Description" ) |
266 | autofmt = wx.StaticText( self, -1, "AutoFormat Code" ) | |
267 | fields = wx.StaticText( self, -1, "Field Objects" ) | |
268 | ctrl = wx.StaticText( self, -1, "wxMaskedTextCtrl" ) | |
1fded56b | 269 | |
8fa876ca RD |
270 | description.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) ) |
271 | autofmt.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) ) | |
272 | fields.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) ) | |
273 | ctrl.SetFont( wx.Font( 9, wx.SWISS, wx.NORMAL, wx.BOLD ) ) | |
1fded56b | 274 | |
8fa876ca RD |
275 | grid = wx.FlexGridSizer( 0, 4, vgap=10, hgap=10 ) |
276 | grid.Add( description, 0, wx.ALIGN_LEFT ) | |
277 | grid.Add( autofmt, 0, wx.ALIGN_LEFT ) | |
278 | grid.Add( fields, 0, wx.ALIGN_LEFT ) | |
279 | grid.Add( ctrl, 0, wx.ALIGN_LEFT ) | |
1fded56b RD |
280 | |
281 | autoformat = "USPHONEFULLEXT" | |
8fa876ca | 282 | fieldsDict = {0: med.Field(choices=["617","781","508","978","413"], choiceRequired=True)} |
1fded56b RD |
283 | fieldsLabel = """\ |
284 | {0: Field(choices=[ | |
285 | "617","781", | |
286 | "508","978","413"], | |
287 | choiceRequired=True)}""" | |
8fa876ca RD |
288 | grid.Add( wx.StaticText( self, -1, "Restricted Area Code"), 0, wx.ALIGN_LEFT ) |
289 | grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT ) | |
290 | grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT ) | |
291 | grid.Add( med.wxMaskedTextCtrl( self, -1, "", | |
1fded56b RD |
292 | autoformat = autoformat, |
293 | fields = fieldsDict, | |
294 | demo = True, | |
295 | name = autoformat), | |
8fa876ca | 296 | 0, wx.ALIGN_LEFT ) |
1fded56b RD |
297 | |
298 | autoformat = "EXPDATEMMYY" | |
8fa876ca | 299 | fieldsDict = {1: med.Field(choices=["03", "04", "05"], choiceRequired=True)} |
1fded56b RD |
300 | fieldsLabel = """\ |
301 | {1: Field(choices=[ | |
302 | "03", "04", "05"], | |
303 | choiceRequired=True)}""" | |
8fa876ca | 304 | exp = med.wxMaskedTextCtrl( self, -1, "", |
1fded56b RD |
305 | autoformat = autoformat, |
306 | fields = fieldsDict, | |
307 | demo = True, | |
308 | name = autoformat) | |
309 | ||
8fa876ca RD |
310 | grid.Add( wx.StaticText( self, -1, "Restricted Expiration"), 0, wx.ALIGN_LEFT ) |
311 | grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT ) | |
312 | grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT ) | |
313 | grid.Add( exp, 0, wx.ALIGN_LEFT ) | |
1fded56b | 314 | |
8fa876ca RD |
315 | fieldsDict = {0: med.Field(choices=["02134","02155"], choiceRequired=True), |
316 | 1: med.Field(choices=["1234", "5678"], choiceRequired=False)} | |
1fded56b RD |
317 | fieldsLabel = """\ |
318 | {0: Field(choices=["02134","02155"], | |
319 | choiceRequired=True), | |
320 | 1: Field(choices=["1234", "5678"], | |
321 | choiceRequired=False)}""" | |
322 | autoformat = "USZIPPLUS4" | |
8fa876ca | 323 | zip = med.wxMaskedTextCtrl( self, -1, "", |
1fded56b RD |
324 | autoformat = autoformat, |
325 | fields = fieldsDict, | |
326 | demo = True, | |
327 | name = autoformat) | |
328 | ||
8fa876ca RD |
329 | grid.Add( wx.StaticText( self, -1, "Restricted Zip + 4"), 0, wx.ALIGN_LEFT ) |
330 | grid.Add( wx.StaticText( self, -1, autoformat), 0, wx.ALIGN_LEFT ) | |
331 | grid.Add( wx.StaticText( self, -1, fieldsLabel), 0, wx.ALIGN_LEFT ) | |
332 | grid.Add( zip, 0, wx.ALIGN_LEFT ) | |
1fded56b | 333 | |
8fa876ca | 334 | self.sizer.Add( grid, 0, wx.ALIGN_LEFT|wx.ALL, border=5 ) |
1fded56b RD |
335 | self.SetSizer( self.sizer ) |
336 | self.SetAutoLayout(1) | |
337 | self.SetupScrolling() | |
338 | ||
339 | ||
8fa876ca | 340 | class demoPage5(scroll.wxScrolledPanel, demoMixin): |
1fded56b RD |
341 | def __init__( self, parent, log ): |
342 | self.log = log | |
8fa876ca RD |
343 | scroll.wxScrolledPanel.__init__( self, parent, -1 ) |
344 | self.sizer = wx.BoxSizer( wx.VERTICAL ) | |
8b9a4190 RD |
345 | |
346 | ||
8fa876ca | 347 | labelMaskedCombos = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
348 | These are some examples of wxMaskedComboBox:""") |
349 | labelMaskedCombos.SetForegroundColour( "Blue" ) | |
350 | ||
351 | ||
8fa876ca | 352 | label_statecode = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
353 | A state selector; only |
354 | "legal" values can be | |
355 | entered:""") | |
8fa876ca RD |
356 | statecode = med.wxMaskedComboBox( self, -1, med.states[0], |
357 | choices = med.states, | |
8b9a4190 RD |
358 | autoformat="USSTATE") |
359 | ||
8fa876ca | 360 | label_statename = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
361 | A state name selector, |
362 | with auto-select:""") | |
363 | ||
364 | # Create this one using factory function: | |
8fa876ca RD |
365 | statename = mctl.wxMaskedCtrl( self, -1, med.state_names[0], |
366 | controlType = mctl.controlTypes.MASKEDCOMBO, | |
367 | choices = med.state_names, | |
8b9a4190 RD |
368 | autoformat="USSTATENAME", |
369 | autoSelect=True) | |
370 | statename.SetCtrlParameters(formatcodes = 'F!V_') | |
371 | ||
1fded56b RD |
372 | |
373 | numerators = [ str(i) for i in range(1, 4) ] | |
374 | denominators = [ string.ljust(str(i), 2) for i in [2,3,4,5,8,16,32,64] ] | |
8fa876ca RD |
375 | fieldsDict = {0: med.Field(choices=numerators, choiceRequired=False), |
376 | 1: med.Field(choices=denominators, choiceRequired=True)} | |
1fded56b RD |
377 | choices = [] |
378 | for n in numerators: | |
379 | for d in denominators: | |
380 | if n != d: | |
381 | choices.append( '%s/%s' % (n,d) ) | |
382 | ||
383 | ||
8fa876ca | 384 | label_fraction = wx.StaticText( self, -1, """\ |
1fded56b | 385 | A masked ComboBox for fraction selection. |
8b9a4190 RD |
386 | Choices for each side of the fraction can |
387 | be selected with PageUp/Down:""") | |
1fded56b | 388 | |
8fa876ca RD |
389 | fraction = mctl.wxMaskedCtrl( self, -1, "", |
390 | controlType = mctl.MASKEDCOMBO, | |
8b9a4190 RD |
391 | choices = choices, |
392 | choiceRequired = True, | |
393 | mask = "#/##", | |
394 | formatcodes = "F_", | |
395 | validRegex = "^\d\/\d\d?", | |
396 | fields = fieldsDict ) | |
1fded56b RD |
397 | |
398 | ||
8fa876ca | 399 | label_code = wx.StaticText( self, -1, """\ |
1fded56b RD |
400 | A masked ComboBox to validate |
401 | text from a list of numeric codes:""") | |
402 | ||
403 | choices = ["91", "136", "305", "4579"] | |
8fa876ca | 404 | code = med.wxMaskedComboBox( self, -1, choices[0], |
1fded56b RD |
405 | choices = choices, |
406 | choiceRequired = True, | |
407 | formatcodes = "F_r", | |
408 | mask = "####") | |
409 | ||
8fa876ca | 410 | label_selector = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
411 | Programmatically set |
412 | choice sets:""") | |
8fa876ca RD |
413 | self.list_selector = wx.ComboBox(self, -1, '', choices = ['list1', 'list2', 'list3']) |
414 | self.dynamicbox = mctl.wxMaskedCtrl( self, -1, ' ', | |
415 | controlType = mctl.controlTypes.MASKEDCOMBO, | |
8b9a4190 RD |
416 | mask = 'XXXX', |
417 | formatcodes = 'F_', | |
418 | # these are to give dropdown some initial height, | |
419 | # as base control apparently only sets that size | |
420 | # during initial construction <sigh>: | |
421 | choices = ['', '1', '2', '3', '4', '5'] ) | |
1fded56b | 422 | |
8b9a4190 | 423 | self.dynamicbox.Clear() # get rid of initial choices used to size the dropdown |
1fded56b | 424 | |
1fded56b | 425 | |
8fa876ca | 426 | labelIpAddrs = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
427 | Here are some examples of wxIpAddrCtrl, a control derived from wxMaskedTextCtrl:""") |
428 | labelIpAddrs.SetForegroundColour( "Blue" ) | |
1fded56b RD |
429 | |
430 | ||
8fa876ca RD |
431 | label_ipaddr1 = wx.StaticText( self, -1, "An empty control:") |
432 | ipaddr1 = med.wxIpAddrCtrl( self, -1, style = wx.TE_PROCESS_TAB ) | |
1fded56b RD |
433 | |
434 | ||
8fa876ca RD |
435 | label_ipaddr2 = wx.StaticText( self, -1, "A restricted mask:") |
436 | ipaddr2 = med.wxIpAddrCtrl( self, -1, mask=" 10. 1.109.###" ) | |
1fded56b RD |
437 | |
438 | ||
8fa876ca | 439 | label_ipaddr3 = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
440 | A control with restricted legal values: |
441 | 10. (1|2) . (129..255) . (0..255)""") | |
8fa876ca RD |
442 | ipaddr3 = mctl.wxMaskedCtrl( self, -1, |
443 | controlType = mctl.controlTypes.IPADDR, | |
8b9a4190 RD |
444 | mask=" 10. #.###.###") |
445 | ipaddr3.SetFieldParameters(0, validRegex="1|2",validRequired=False ) # requires entry to match or not allowed | |
446 | ||
1fded56b | 447 | # This allows any value in penultimate field, but colors anything outside of the range invalid: |
8b9a4190 RD |
448 | ipaddr3.SetFieldParameters(1, validRange=(129,255), validRequired=False ) |
449 | ||
1fded56b | 450 | |
1fded56b | 451 | |
8fa876ca | 452 | labelNumerics = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
453 | Here are some useful configurations of a wxMaskedTextCtrl for integer and floating point input that still treat |
454 | the control as a text control. (For a true numeric control, check out the wxMaskedNumCtrl class!)""") | |
455 | labelNumerics.SetForegroundColour( "Blue" ) | |
456 | ||
8fa876ca | 457 | label_intctrl1 = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
458 | An integer entry control with |
459 | shifting insert enabled:""") | |
8fa876ca RD |
460 | self.intctrl1 = med.wxMaskedTextCtrl(self, -1, name='intctrl', mask="#{9}", formatcodes = '_-,F>') |
461 | label_intctrl2 = wx.StaticText( self, -1, """\ | |
8b9a4190 | 462 | Right-insert integer entry:""") |
8fa876ca | 463 | self.intctrl2 = med.wxMaskedTextCtrl(self, -1, name='intctrl', mask="#{9}", formatcodes = '_-,Fr') |
8b9a4190 | 464 | |
8fa876ca | 465 | label_floatctrl = wx.StaticText( self, -1, """\ |
1fded56b RD |
466 | A floating point entry control |
467 | with right-insert for ordinal:""") | |
8fa876ca | 468 | self.floatctrl = med.wxMaskedTextCtrl(self, -1, name='floatctrl', mask="#{9}.#{2}", formatcodes="F,_-R", useParensForNegatives=False) |
1fded56b RD |
469 | self.floatctrl.SetFieldParameters(0, formatcodes='r<', validRequired=True) # right-insert, require explicit cursor movement to change fields |
470 | self.floatctrl.SetFieldParameters(1, defaultValue='00') # don't allow blank fraction | |
471 | ||
8fa876ca | 472 | label_numselect = wx.StaticText( self, -1, """\ |
8b9a4190 RD |
473 | <= Programmatically set the value |
474 | of the float entry ctrl:""") | |
8fa876ca | 475 | numselect = wx.ComboBox(self, -1, choices = [ '', '111', '222.22', '-3', '54321.666666666', '-1353.978', |
8b9a4190 RD |
476 | '1234567', '-1234567', '123456789', '-123456789.1', |
477 | '1234567890.', '-1234567890.1' ]) | |
478 | ||
8fa876ca RD |
479 | parens_check = wx.CheckBox(self, -1, "Use () to indicate negatives in above controls") |
480 | ||
481 | ||
482 | ||
483 | gridCombos = wx.FlexGridSizer( 0, 4, vgap=10, hgap = 10 ) | |
484 | gridCombos.Add( label_statecode, 0, wx.ALIGN_LEFT ) | |
485 | gridCombos.Add( statecode, 0, wx.ALIGN_LEFT ) | |
486 | gridCombos.Add( label_fraction, 0, wx.ALIGN_LEFT ) | |
487 | gridCombos.Add( fraction, 0, wx.ALIGN_LEFT ) | |
488 | gridCombos.Add( label_statename, 0, wx.ALIGN_LEFT ) | |
489 | gridCombos.Add( statename, 0, wx.ALIGN_LEFT ) | |
490 | gridCombos.Add( label_code, 0, wx.ALIGN_LEFT ) | |
491 | gridCombos.Add( code, 0, wx.ALIGN_LEFT ) | |
492 | gridCombos.Add( label_selector, 0, wx.ALIGN_LEFT) | |
493 | hbox = wx.BoxSizer( wx.HORIZONTAL ) | |
494 | hbox.Add( self.list_selector, 0, wx.ALIGN_LEFT ) | |
495 | hbox.Add(wx.StaticText(self, -1, ' => '), 0, wx.ALIGN_LEFT) | |
496 | hbox.Add( self.dynamicbox, 0, wx.ALIGN_LEFT ) | |
497 | gridCombos.Add( hbox, 0, wx.ALIGN_LEFT ) | |
498 | ||
499 | gridIpAddrs = wx.FlexGridSizer( 0, 4, vgap=10, hgap = 15 ) | |
500 | gridIpAddrs.Add( label_ipaddr1, 0, wx.ALIGN_LEFT ) | |
501 | gridIpAddrs.Add( ipaddr1, 0, wx.ALIGN_LEFT ) | |
502 | gridIpAddrs.Add( label_ipaddr2, 0, wx.ALIGN_LEFT ) | |
503 | gridIpAddrs.Add( ipaddr2, 0, wx.ALIGN_LEFT ) | |
504 | gridIpAddrs.Add( label_ipaddr3, 0, wx.ALIGN_LEFT ) | |
505 | gridIpAddrs.Add( ipaddr3, 0, wx.ALIGN_LEFT ) | |
506 | ||
507 | gridNumerics = wx.FlexGridSizer( 0, 4, vgap=10, hgap = 10 ) | |
508 | gridNumerics.Add( label_intctrl1, 0, wx.ALIGN_LEFT ) | |
509 | gridNumerics.Add( self.intctrl1, 0, wx.ALIGN_LEFT ) | |
510 | gridNumerics.Add( label_intctrl2, 0, wx.ALIGN_RIGHT ) | |
511 | gridNumerics.Add( self.intctrl2, 0, wx.ALIGN_LEFT ) | |
512 | gridNumerics.Add( label_floatctrl, 0, wx.ALIGN_LEFT ) | |
513 | gridNumerics.Add( self.floatctrl, 0, wx.ALIGN_LEFT ) | |
514 | gridNumerics.Add( label_numselect, 0, wx.ALIGN_RIGHT ) | |
515 | gridNumerics.Add( numselect, 0, wx.ALIGN_LEFT ) | |
516 | ||
517 | self.sizer.Add( labelMaskedCombos, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) | |
518 | self.sizer.Add( gridCombos, 0, wx.ALIGN_LEFT|wx.ALL, border=5 ) | |
519 | self.sizer.Add( wx.StaticLine(self, -1), 0, wx.EXPAND|wx.TOP|wx.BOTTOM, border=8 ) | |
520 | self.sizer.Add( labelIpAddrs, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) | |
521 | self.sizer.Add( gridIpAddrs, 0, wx.ALIGN_LEFT|wx.ALL, border=5 ) | |
522 | self.sizer.Add( wx.StaticLine(self, -1), 0, wx.EXPAND|wx.TOP|wx.BOTTOM, border=8 ) | |
523 | self.sizer.Add( labelNumerics, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) | |
524 | self.sizer.Add( gridNumerics, 0, wx.ALIGN_LEFT|wx.ALL, border=5 ) | |
525 | self.sizer.Add( parens_check, 0, wx.ALIGN_LEFT|wx.ALL, 5 ) | |
1fded56b | 526 | |
1fded56b RD |
527 | self.SetSizer( self.sizer ) |
528 | self.SetAutoLayout(1) | |
529 | self.SetupScrolling() | |
530 | ||
8fa876ca RD |
531 | self.Bind(wx.EVT_COMBOBOX, self.OnComboSelection, id=fraction.GetId()) |
532 | self.Bind(wx.EVT_COMBOBOX, self.OnComboSelection, id=code.GetId()) | |
533 | self.Bind(wx.EVT_COMBOBOX, self.OnComboSelection, id=statecode.GetId()) | |
534 | self.Bind(wx.EVT_COMBOBOX, self.OnComboSelection, id=statename.GetId()) | |
535 | self.Bind(wx.EVT_TEXT, self.OnTextChange, id=code.GetId()) | |
536 | self.Bind(wx.EVT_TEXT, self.OnTextChange, id=statecode.GetId()) | |
537 | self.Bind(wx.EVT_TEXT, self.OnTextChange, id=statename.GetId()) | |
538 | self.Bind(wx.EVT_COMBOBOX, self.OnListSelection, id=self.list_selector.GetId()) | |
8b9a4190 | 539 | |
8fa876ca RD |
540 | self.Bind(wx.EVT_TEXT, self.OnTextChange, id=self.intctrl1.GetId()) |
541 | self.Bind(wx.EVT_TEXT, self.OnTextChange, id=self.intctrl2.GetId()) | |
542 | self.Bind(wx.EVT_TEXT, self.OnTextChange, id=self.floatctrl.GetId()) | |
543 | self.Bind(wx.EVT_COMBOBOX, self.OnNumberSelect, id=numselect.GetId()) | |
544 | self.Bind(wx.EVT_CHECKBOX, self.OnParensCheck, id=parens_check.GetId()) | |
8b9a4190 | 545 | |
8fa876ca RD |
546 | self.Bind(wx.EVT_TEXT, self.OnIpAddrChange, id=ipaddr1.GetId()) |
547 | self.Bind(wx.EVT_TEXT, self.OnIpAddrChange, id=ipaddr2.GetId()) | |
548 | self.Bind(wx.EVT_TEXT, self.OnIpAddrChange, id=ipaddr3.GetId()) | |
1fded56b RD |
549 | |
550 | ||
8b9a4190 RD |
551 | |
552 | ||
553 | def OnComboSelection( self, event ): | |
1fded56b RD |
554 | ctl = self.FindWindowById( event.GetId() ) |
555 | if not ctl.IsValid(): | |
556 | self.log.write('current value not a valid choice') | |
8b9a4190 | 557 | self.log.write('new value = %s' % ctl.GetValue()) |
1fded56b RD |
558 | |
559 | def OnTextChange( self, event ): | |
560 | ctl = self.FindWindowById( event.GetId() ) | |
561 | if ctl.IsValid(): | |
562 | self.log.write('new value = %s\n' % ctl.GetValue() ) | |
563 | ||
564 | def OnNumberSelect( self, event ): | |
565 | value = event.GetString() | |
1fded56b RD |
566 | # Format choice to fit into format for #{9}.#{2}, with sign position reserved: |
567 | # (ordinal + fraction == 11 + decimal point + sign == 13) | |
1fded56b RD |
568 | if value: |
569 | floattext = "%13.2f" % float(value) | |
570 | else: | |
571 | floattext = value # clear the value again | |
572 | try: | |
573 | self.floatctrl.SetValue(floattext) | |
574 | except: | |
575 | type, value, tb = sys.exc_info() | |
576 | for line in traceback.format_exception_only(type, value): | |
577 | self.log.write(line) | |
578 | ||
8b9a4190 RD |
579 | def OnParensCheck( self, event ): |
580 | self.intctrl1.SetCtrlParameters(useParensForNegatives=event.Checked()) | |
581 | self.intctrl2.SetCtrlParameters(useParensForNegatives=event.Checked()) | |
582 | self.floatctrl.SetCtrlParameters(useParensForNegatives=event.Checked()) | |
583 | ||
584 | def OnIpAddrChange( self, event ): | |
585 | ipaddr = self.FindWindowById( event.GetId() ) | |
586 | if ipaddr.IsValid(): | |
587 | self.log.write('new addr = %s\n' % ipaddr.GetAddress() ) | |
588 | ||
589 | def OnListSelection( self, event ): | |
590 | list = self.list_selector.GetStringSelection() | |
591 | formatcodes = 'F_' | |
592 | if list == 'list1': | |
593 | choices = ['abc', 'defg', 'hi'] | |
594 | mask = 'aaaa' | |
595 | elif list == 'list2': | |
596 | choices = ['1', '2', '34', '567'] | |
597 | formatcodes += 'r' | |
598 | mask = '###' | |
599 | else: | |
600 | choices = states | |
601 | mask = 'AA' | |
602 | formatcodes += '!' | |
603 | self.dynamicbox.SetCtrlParameters( mask = mask, | |
604 | choices = choices, | |
605 | choiceRequired=True, | |
606 | autoSelect=True, | |
607 | formatcodes=formatcodes) | |
608 | self.dynamicbox.SetValue(choices[0]) | |
609 | ||
1fded56b | 610 | # --------------------------------------------------------------------- |
8fa876ca | 611 | class TestMaskedTextCtrls(wx.Notebook): |
1fded56b | 612 | def __init__(self, parent, id, log): |
8fa876ca | 613 | wx.Notebook.__init__(self, parent, id) |
1fded56b RD |
614 | self.log = log |
615 | ||
616 | win = demoPage1(self, log) | |
617 | self.AddPage(win, "General examples") | |
618 | ||
619 | win = demoPage2(self, log) | |
620 | self.AddPage(win, 'Auto-formatted controls') | |
621 | ||
622 | win = demoPage3(self, log) | |
623 | self.AddPage(win, "Using default values") | |
624 | ||
625 | win = demoPage4(self, log) | |
626 | self.AddPage(win, 'Using auto-complete fields') | |
627 | ||
628 | win = demoPage5(self, log) | |
629 | self.AddPage(win, 'Other masked controls') | |
630 | ||
631 | ||
632 | #---------------------------------------------------------------------------- | |
633 | ||
634 | def runTest(frame, nb, log): | |
635 | testWin = TestMaskedTextCtrls(nb, -1, log) | |
636 | return testWin | |
637 | ||
638 | def RunStandalone(): | |
8fa876ca RD |
639 | app = wx.PySimpleApp() |
640 | frame = wx.Frame(None, -1, "Test MaskedTextCtrl", size=(640, 480)) | |
1fded56b RD |
641 | win = TestMaskedTextCtrls(frame, -1, sys.stdout) |
642 | frame.Show(True) | |
643 | app.MainLoop() | |
644 | #---------------------------------------------------------------------------- | |
1fded56b RD |
645 | |
646 | overview = """<html> | |
647 | <PRE><FONT SIZE=-1> | |
8fa876ca | 648 | """ + med.__doc__ + """ |
1fded56b RD |
649 | </FONT></PRE> |
650 | """ | |
651 | ||
652 | if __name__ == "__main__": | |
653 | import sys,os | |
654 | import run | |
655 | run.main(['', os.path.basename(sys.argv[0])]) |