]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/masked/maskededit.py
src/common/wxchar.cpps/wxConvLocal/wxConvLibs/g as otherwise we may fail to convert...
[wxWidgets.git] / wxPython / wx / lib / masked / maskededit.py
1 #----------------------------------------------------------------------------
2 # Name: maskededit.py
3 # Authors: Jeff Childers, Will Sadkin
4 # Email: jchilders_98@yahoo.com, wsadkin@nameconnector.com
5 # Created: 02/11/2003
6 # Copyright: (c) 2003 by Jeff Childers, Will Sadkin, 2003
7 # Portions: (c) 2002 by Will Sadkin, 2002-2003
8 # RCS-ID: $Id$
9 # License: wxWindows license
10 #----------------------------------------------------------------------------
11 # NOTE:
12 # MaskedEdit controls are based on a suggestion made on [wxPython-Users] by
13 # Jason Hihn, and borrows liberally from Will Sadkin's original masked edit
14 # control for time entry, TimeCtrl (which is now rewritten using this
15 # control!).
16 #
17 # MaskedEdit controls do not normally use validators, because they do
18 # careful manipulation of the cursor in the text window on each keystroke,
19 # and validation is cursor-position specific, so the control intercepts the
20 # key codes before the validator would fire. However, validators can be
21 # provided to do data transfer to the controls.
22 #
23 #----------------------------------------------------------------------------
24 #
25 # This file now contains the bulk of the logic behind all masked controls,
26 # the MaskedEditMixin class, the Field class, and the autoformat codes.
27 #
28 #----------------------------------------------------------------------------
29 #
30 # 03/30/2004 - Will Sadkin (wsadkin@nameconnector.com)
31 #
32 # o Split out TextCtrl, ComboBox and IpAddrCtrl into their own files,
33 # o Reorganized code into masked package
34 #
35 # 12/09/2003 - Jeff Grimmett (grimmtooth@softhome.net)
36 #
37 # o Updated for wx namespace. No guarantees. This is one huge file.
38 #
39 # 12/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
40 #
41 # o Missed wx.DateTime stuff earlier.
42 #
43 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
44 #
45 # o MaskedEditMixin -> MaskedEditMixin
46 # o wxMaskedTextCtrl -> maskedTextCtrl
47 # o wxMaskedComboBoxSelectEvent -> MaskedComboBoxSelectEvent
48 # o wxMaskedComboBox -> MaskedComboBox
49 # o wxIpAddrCtrl -> IpAddrCtrl
50 # o wxTimeCtrl -> TimeCtrl
51 #
52
53 """\
54 ====================
55 Masked Edit Overview
56 ====================
57
58 masked.TextCtrl:
59 is a sublassed text control that can carefully control the user's input
60 based on a mask string you provide.
61
62 General usage example::
63
64 control = masked.TextCtrl( win, -1, '', mask = '(###) ###-####')
65
66 The example above will create a text control that allows only numbers to be
67 entered and then only in the positions indicated in the mask by the # sign.
68
69 masked.ComboBox:
70 is a similar subclass of wxComboBox that allows the same sort of masking,
71 but also can do auto-complete of values, and can require the value typed
72 to be in the list of choices to be colored appropriately.
73
74 masked.Ctrl:
75 is actually a factory function for several types of masked edit controls:
76
77 ================= ==================================================
78 masked.TextCtrl standard masked edit text box
79 masked.ComboBox adds combobox capabilities
80 masked.IpAddrCtrl adds special semantics for IP address entry
81 masked.TimeCtrl special subclass handling lots of types as values
82 masked.NumCtrl special subclass handling numeric values
83 ================= ==================================================
84
85 It works by looking for a *controlType* parameter in the keyword
86 arguments of the control, to determine what kind of instance to return.
87 If not specified as a keyword argument, the default control type returned
88 will be masked.TextCtrl.
89
90 Each of the above classes has its own set of arguments, but masked.Ctrl
91 provides a single "unified" interface for masked controls. Those for
92 masked.TextCtrl, masked.ComboBox and masked.IpAddrCtrl are all documented
93 below; the others have their own demo pages and interface descriptions.
94 (See end of following discussion for how to configure the wx.MaskedCtrl()
95 to select the above control types.)
96
97 =========================
98
99 Initialization Parameters
100 -------------------------
101 mask
102 Allowed mask characters and function:
103
104 ========= ==========================================================
105 Character Function
106 ========= ==========================================================
107 # Allow numeric only (0-9)
108 N Allow letters and numbers (0-9)
109 A Allow uppercase letters only
110 a Allow lowercase letters only
111 C Allow any letter, upper or lower
112 X Allow string.letters, string.punctuation, string.digits
113 & Allow string.punctuation only
114 \* Allow any ansi character
115 ========= ==========================================================
116
117
118 These controls define these sets of characters using string.letters,
119 string.uppercase, etc. These sets are affected by the system locale
120 setting, so in order to have the masked controls accept characters
121 that are specific to your users' language, your application should
122 set the locale.
123 For example, to allow international characters to be used in the
124 above masks, you can place the following in your code as part of
125 your application's initialization code::
126
127 import locale
128 locale.setlocale(locale.LC_ALL, '')
129
130 The controls now also support (by popular demand) all "ansi" chars,
131 that is, all ascii codes between 32 and 255, by use of the * mask character.
132
133 Using these mask characters, a variety of template masks can be built. See
134 the demo for some other common examples include date+time, social security
135 number, etc. If any of these characters are needed as template rather
136 than mask characters, they can be escaped with \, ie. \N means "literal N".
137 (use \\ for literal backslash, as in: r'CCC\\NNN'.)
138
139
140 *Note:*
141 Masks containing only # characters and one optional decimal point
142 character are handled specially, as "numeric" controls. Such
143 controls have special handling for typing the '-' key, handling
144 the "decimal point" character as truncating the integer portion,
145 optionally allowing grouping characters and so forth.
146 There are several parameters and format codes that only make sense
147 when combined with such masks, eg. groupChar, decimalChar, and so
148 forth (see below). These allow you to construct reasonable
149 numeric entry controls.
150
151 *Note:*
152 Changing the mask for a control deletes any previous field classes
153 (and any associated validation or formatting constraints) for them.
154
155 useFixedWidthFont
156 By default, masked edit controls use a fixed width font, so that
157 the mask characters are fixed within the control, regardless of
158 subsequent modifications to the value. Set to False if having
159 the control font be the same as other controls is required. (This is
160 a control-level parameter.)
161
162 defaultEncoding
163 (Applies to unicode systems only) By default, the default unicode encoding
164 used is latin1, or iso-8859-1. If necessary, you can set this control-level
165 parameter to govern the codec used to decode your keyboard inputs.
166 (This is a control-level parameter.)
167
168 formatcodes
169 These other properties can be passed to the class when instantiating it:
170 Formatcodes are specified as a string of single character formatting
171 codes that modify behavior of the control::
172
173 _ Allow spaces
174 ! Force upper
175 ^ Force lower
176 R Right-align field(s)
177 r Right-insert in field(s) (implies R)
178 < Stay in field until explicit navigation out of it
179
180 > Allow insert/delete within partially filled fields (as
181 opposed to the default "overwrite" mode for fixed-width
182 masked edit controls.) This allows single-field controls
183 or each field within a multi-field control to optionally
184 behave more like standard text controls.
185 (See EMAIL or phone number autoformat examples.)
186
187 *Note: This also governs whether backspace/delete operations
188 shift contents of field to right of cursor, or just blank the
189 erased section.
190
191 Also, when combined with 'r', this indicates that the field
192 or control allows right insert anywhere within the current
193 non-empty value in the field. (Otherwise right-insert behavior
194 is only performed to when the entire right-insertable field is
195 selected or the cursor is at the right edge of the field.*
196
197
198 , Allow grouping character in integer fields of numeric controls
199 and auto-group/regroup digits (if the result fits) when leaving
200 such a field. (If specified, .SetValue() will attempt to
201 auto-group as well.)
202 ',' is also the default grouping character. To change the
203 grouping character and/or decimal character, use the groupChar
204 and decimalChar parameters, respectively.
205 Note: typing the "decimal point" character in such fields will
206 clip the value to that left of the cursor for integer
207 fields of controls with "integer" or "floating point" masks.
208 If the ',' format code is specified, this will also cause the
209 resulting digits to be regrouped properly, using the current
210 grouping character.
211 - Prepend and reserve leading space for sign to mask and allow
212 signed values (negative #s shown in red by default.) Can be
213 used with argument useParensForNegatives (see below.)
214 0 integer fields get leading zeros
215 D Date[/time] field
216 T Time field
217 F Auto-Fit: the control calulates its size from
218 the length of the template mask
219 V validate entered chars against validRegex before allowing them
220 to be entered vs. being allowed by basic mask and then having
221 the resulting value just colored as invalid.
222 (See USSTATE autoformat demo for how this can be used.)
223 S select entire field when navigating to new field
224
225 fillChar
226
227 defaultValue
228 These controls have two options for the initial state of the control.
229 If a blank control with just the non-editable characters showing
230 is desired, simply leave the constructor variable fillChar as its
231 default (' '). If you want some other character there, simply
232 change the fillChar to that value. Note: changing the control's fillChar
233 will implicitly reset all of the fields' fillChars to this value.
234
235 If you need different default characters in each mask position,
236 you can specify a defaultValue parameter in the constructor, or
237 set them for each field individually.
238 This value must satisfy the non-editable characters of the mask,
239 but need not conform to the replaceable characters.
240
241 groupChar
242
243 decimalChar
244 These parameters govern what character is used to group numbers
245 and is used to indicate the decimal point for numeric format controls.
246 The default groupChar is ',', the default decimalChar is '.'
247 By changing these, you can customize the presentation of numbers
248 for your location.
249
250 Eg::
251
252 formatcodes = ',', groupChar='\'' allows 12'345.34
253 formatcodes = ',', groupChar='.', decimalChar=',' allows 12.345,34
254
255 (These are control-level parameters.)
256
257 shiftDecimalChar
258 The default "shiftDecimalChar" (used for "backwards-tabbing" until
259 shift-tab is fixed in wxPython) is '>' (for QUERTY keyboards.) for
260 other keyboards, you may want to customize this, eg '?' for shift ',' on
261 AZERTY keyboards, ':' or ';' for other European keyboards, etc.
262 (This is a control-level parameter.)
263
264 useParensForNegatives=False
265 This option can be used with signed numeric format controls to
266 indicate signs via () rather than '-'.
267 (This is a control-level parameter.)
268
269 autoSelect=False
270 This option can be used to have a field or the control try to
271 auto-complete on each keystroke if choices have been specified.
272
273 autoCompleteKeycodes=[]
274 By default, DownArrow, PageUp and PageDown will auto-complete a
275 partially entered field. Shift-DownArrow, Shift-UpArrow, PageUp
276 and PageDown will also auto-complete, but if the field already
277 contains a matched value, these keys will cycle through the list
278 of choices forward or backward as appropriate. Shift-Up and
279 Shift-Down also take you to the next/previous field after any
280 auto-complete action.
281
282 Additional auto-complete keys can be specified via this parameter.
283 Any keys so specified will act like PageDown.
284 (This is a control-level parameter.)
285
286
287
288 Validating User Input
289 =====================
290 There are a variety of initialization parameters that are used to validate
291 user input. These parameters can apply to the control as a whole, and/or
292 to individual fields:
293
294 ===================== ==================================================================
295 excludeChars A string of characters to exclude even if otherwise allowed
296 includeChars A string of characters to allow even if otherwise disallowed
297 validRegex Use a regular expression to validate the contents of the text box
298 validRange Pass a rangeas list (low,high) to limit numeric fields/values
299 choices A list of strings that are allowed choices for the control.
300 choiceRequired value must be member of choices list
301 compareNoCase Perform case-insensitive matching when validating against list
302 *Note: for masked.ComboBox, this defaults to True.*
303 emptyInvalid Boolean indicating whether an empty value should be considered
304 invalid
305
306 validFunc A function to call of the form: bool = func(candidate_value)
307 which will return True if the candidate_value satisfies some
308 external criteria for the control in addition to the the
309 other validation, or False if not. (This validation is
310 applied last in the chain of validations.)
311
312 validRequired Boolean indicating whether or not keys that are allowed by the
313 mask, but result in an invalid value are allowed to be entered
314 into the control. Setting this to True implies that a valid
315 default value is set for the control.
316
317 retainFieldValidation False by default; if True, this allows individual fields to
318 retain their own validation constraints independently of any
319 subsequent changes to the control's overall parameters.
320 (This is a control-level parameter.)
321
322 validator Validators are not normally needed for masked controls, because
323 of the nature of the validation and control of input. However,
324 you can supply one to provide data transfer routines for the
325 controls.
326 ===================== ==================================================================
327
328
329 Coloring Behavior
330 =================
331 The following parameters have been provided to allow you to change the default
332 coloring behavior of the control. These can be set at construction, or via
333 the .SetCtrlParameters() function. Pass a color as string e.g. 'Yellow':
334
335 ======================== =======================================================================
336 emptyBackgroundColour Control Background color when identified as empty. Default=White
337 invalidBackgroundColour Control Background color when identified as Not valid. Default=Yellow
338 validBackgroundColour Control Background color when identified as Valid. Default=white
339 ======================== =======================================================================
340
341
342 The following parameters control the default foreground color coloring behavior of the
343 control. Pass a color as string e.g. 'Yellow':
344
345 ======================== ======================================================================
346 foregroundColour Control foreground color when value is not negative. Default=Black
347 signedForegroundColour Control foreground color when value is negative. Default=Red
348 ======================== ======================================================================
349
350
351 Fields
352 ======
353 Each part of the mask that allows user input is considered a field. The fields
354 are represented by their own class instances. You can specify field-specific
355 constraints by constructing or accessing the field instances for the control
356 and then specifying those constraints via parameters.
357
358 fields
359 This parameter allows you to specify Field instances containing
360 constraints for the individual fields of a control, eg: local
361 choice lists, validation rules, functions, regexps, etc.
362 It can be either an ordered list or a dictionary. If a list,
363 the fields will be applied as fields 0, 1, 2, etc.
364 If a dictionary, it should be keyed by field index.
365 the values should be a instances of maskededit.Field.
366
367 Any field not represented by the list or dictionary will be
368 implicitly created by the control.
369
370 Eg::
371
372 fields = [ Field(formatcodes='_r'), Field('choices=['a', 'b', 'c']) ]
373
374 Or::
375
376 fields = {
377 1: ( Field(formatcodes='_R', choices=['a', 'b', 'c']),
378 3: ( Field(choices=['01', '02', '03'], choiceRequired=True)
379 }
380
381 The following parameters are available for individual fields, with the
382 same semantics as for the whole control but applied to the field in question:
383
384 ============== =============================================================================
385 fillChar if set for a field, it will override the control's fillChar for that field
386 groupChar if set for a field, it will override the control's default
387 defaultValue sets field-specific default value; overrides any default from control
388 compareNoCase overrides control's settings
389 emptyInvalid determines whether field is required to be filled at all times
390 validRequired if set, requires field to contain valid value
391 ============== =============================================================================
392
393 If any of the above parameters are subsequently specified for the control as a
394 whole, that new value will be propagated to each field, unless the
395 retainFieldValidation control-level parameter is set.
396
397 ============== ==============================
398 formatcodes Augments control's settings
399 excludeChars ' ' '
400 includeChars ' ' '
401 validRegex ' ' '
402 validRange ' ' '
403 choices ' ' '
404 choiceRequired ' ' '
405 validFunc ' ' '
406 ============== ==============================
407
408
409
410 Control Class Functions
411 =======================
412 .GetPlainValue(value=None)
413 Returns the value specified (or the control's text value
414 not specified) without the formatting text.
415 In the example above, might return phone no='3522640075',
416 whereas control.GetValue() would return '(352) 264-0075'
417 .ClearValue()
418 Returns the control's value to its default, and places the
419 cursor at the beginning of the control.
420 .SetValue()
421 Does "smart replacement" of passed value into the control, as does
422 the .Paste() method. As with other text entry controls, the
423 .SetValue() text replacement begins at left-edge of the control,
424 with missing mask characters inserted as appropriate.
425 .SetValue will also adjust integer, float or date mask entry values,
426 adding commas, auto-completing years, etc. as appropriate.
427 For "right-aligned" numeric controls, it will also now automatically
428 right-adjust any value whose length is less than the width of the
429 control before attempting to set the value.
430 If a value does not follow the format of the control's mask, or will
431 not fit into the control, a ValueError exception will be raised.
432
433 Eg::
434
435 mask = '(###) ###-####'
436 .SetValue('1234567890') => '(123) 456-7890'
437 .SetValue('(123)4567890') => '(123) 456-7890'
438 .SetValue('(123)456-7890') => '(123) 456-7890'
439 .SetValue('123/4567-890') => illegal paste; ValueError
440
441 mask = '#{6}.#{2}', formatcodes = '_,-',
442 .SetValue('111') => ' 111 . '
443 .SetValue(' %9.2f' % -111.12345 ) => ' -111.12'
444 .SetValue(' %9.2f' % 1234.00 ) => ' 1,234.00'
445 .SetValue(' %9.2f' % -1234567.12345 ) => insufficient room; ValueError
446
447 mask = '#{6}.#{2}', formatcodes = '_,-R' # will right-adjust value for right-aligned control
448 .SetValue('111') => padded value misalignment ValueError: " 111" will not fit
449 .SetValue('%.2f' % 111 ) => ' 111.00'
450 .SetValue('%.2f' % -111.12345 ) => ' -111.12'
451
452
453 .IsValid(value=None)
454 Returns True if the value specified (or the value of the control
455 if not specified) passes validation tests
456 .IsEmpty(value=None)
457 Returns True if the value specified (or the value of the control
458 if not specified) is equal to an "empty value," ie. all
459 editable characters == the fillChar for their respective fields.
460 .IsDefault(value=None)
461 Returns True if the value specified (or the value of the control
462 if not specified) is equal to the initial value of the control.
463
464 .Refresh()
465 Recolors the control as appropriate to its current settings.
466
467 .SetCtrlParameters(\*\*kwargs)
468 This function allows you to set up and/or change the control parameters
469 after construction; it takes a list of key/value pairs as arguments,
470 where the keys can be any of the mask-specific parameters in the constructor.
471
472 Eg::
473
474 ctl = masked.TextCtrl( self, -1 )
475 ctl.SetCtrlParameters( mask='###-####',
476 defaultValue='555-1212',
477 formatcodes='F')
478
479 .GetCtrlParameter(parametername)
480 This function allows you to retrieve the current value of a parameter
481 from the control.
482
483 *Note:* Each of the control parameters can also be set using its
484 own Set and Get function. These functions follow a regular form:
485 All of the parameter names start with lower case; for their
486 corresponding Set/Get function, the parameter name is capitalized.
487
488 Eg::
489
490 ctl.SetMask('###-####')
491 ctl.SetDefaultValue('555-1212')
492 ctl.GetChoiceRequired()
493 ctl.GetFormatcodes()
494
495 *Note:* After any change in parameters, the choices for the
496 control are reevaluated to ensure that they are still legal. If you
497 have large choice lists, it is therefore more efficient to set parameters
498 before setting the choices available.
499
500 .SetFieldParameters(field_index, \*\*kwargs)
501 This function allows you to specify change individual field
502 parameters after construction. (Indices are 0-based.)
503
504 .GetFieldParameter(field_index, parametername)
505 Allows the retrieval of field parameters after construction
506
507
508 The control detects certain common constructions. In order to use the signed feature
509 (negative numbers and coloring), the mask has to be all numbers with optionally one
510 decimal point. Without a decimal (e.g. '######', the control will treat it as an integer
511 value. With a decimal (e.g. '###.##'), the control will act as a floating point control
512 (i.e. press decimal to 'tab' to the decimal position). Pressing decimal in the
513 integer control truncates the value. However, for a true numeric control,
514 masked.NumCtrl provides all this, and true numeric input/output support as well.
515
516
517 Check your controls by calling each control's .IsValid() function and the
518 .IsEmpty() function to determine which controls have been a) filled in and
519 b) filled in properly.
520
521
522 Regular expression validations can be used flexibly and creatively.
523 Take a look at the demo; the zip-code validation succeeds as long as the
524 first five numerals are entered. the last four are optional, but if
525 any are entered, there must be 4 to be valid.
526
527 masked.Ctrl Configuration
528 =========================
529 masked.Ctrl works by looking for a special *controlType*
530 parameter in the variable arguments of the control, to determine
531 what kind of instance to return.
532 controlType can be one of::
533
534 controlTypes.TEXT
535 controlTypes.COMBO
536 controlTypes.IPADDR
537 controlTypes.TIME
538 controlTypes.NUMBER
539
540 These constants are also available individually, ie, you can
541 use either of the following::
542
543 from wxPython.wx.lib.masked import MaskedCtrl, controlTypes
544 from wxPython.wx.lib.masked import MaskedCtrl, COMBO, TEXT, NUMBER, IPADDR
545
546 If not specified as a keyword argument, the default controlType is
547 controlTypes.TEXT.
548
549 """
550
551 """
552 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
553 DEVELOPER COMMENTS:
554
555 Naming Conventions
556 ------------------
557 All methods of the Mixin that are not meant to be exposed to the external
558 interface are prefaced with '_'. Those functions that are primarily
559 intended to be internal subroutines subsequently start with a lower-case
560 letter; those that are primarily intended to be used and/or overridden
561 by derived subclasses start with a capital letter.
562
563 The following methods must be used and/or defined when deriving a control
564 from MaskedEditMixin. NOTE: if deriving from a *masked edit* control
565 (eg. class IpAddrCtrl(masked.TextCtrl) ), then this is NOT necessary,
566 as it's already been done for you in the base class.
567
568 ._SetInitialValue()
569 This function must be called after the associated base
570 control has been initialized in the subclass __init__
571 function. It sets the initial value of the control,
572 either to the value specified if non-empty, the
573 default value if specified, or the "template" for
574 the empty control as necessary. It will also set/reset
575 the font if necessary and apply formatting to the
576 control at this time.
577
578 ._GetSelection()
579 REQUIRED
580 Each class derived from MaskedEditMixin must define
581 the function for getting the start and end of the
582 current text selection. The reason for this is
583 that not all controls have the same function name for
584 doing this; eg. wx.TextCtrl uses .GetSelection(),
585 whereas we had to write a .GetMark() function for
586 wxComboBox, because .GetSelection() for the control
587 gets the currently selected list item from the combo
588 box, and the control doesn't (yet) natively provide
589 a means of determining the text selection.
590 ._SetSelection()
591 REQUIRED
592 Similarly to _GetSelection, each class derived from
593 MaskedEditMixin must define the function for setting
594 the start and end of the current text selection.
595 (eg. .SetSelection() for masked.TextCtrl, and .SetMark() for
596 masked.ComboBox.
597
598 ._GetInsertionPoint()
599 ._SetInsertionPoint()
600 REQUIRED
601 For consistency, and because the mixin shouldn't rely
602 on fixed names for any manipulations it does of any of
603 the base controls, we require each class derived from
604 MaskedEditMixin to define these functions as well.
605
606 ._GetValue()
607 ._SetValue() REQUIRED
608 Each class derived from MaskedEditMixin must define
609 the functions used to get and set the raw value of the
610 control.
611 This is necessary so that recursion doesn't take place
612 when setting the value, and so that the mixin can
613 call the appropriate function after doing all its
614 validation and manipulation without knowing what kind
615 of base control it was mixed in with. To handle undo
616 functionality, the ._SetValue() must record the current
617 selection prior to setting the value.
618
619 .Cut()
620 .Paste()
621 .Undo()
622 .SetValue() REQUIRED
623 Each class derived from MaskedEditMixin must redefine
624 these functions to call the _Cut(), _Paste(), _Undo()
625 and _SetValue() methods, respectively for the control,
626 so as to prevent programmatic corruption of the control's
627 value. This must be done in each derivation, as the
628 mixin cannot itself override a member of a sibling class.
629
630 ._Refresh() REQUIRED
631 Each class derived from MaskedEditMixin must define
632 the function used to refresh the base control.
633
634 .Refresh() REQUIRED
635 Each class derived from MaskedEditMixin must redefine
636 this function so that it checks the validity of the
637 control (via self._CheckValid) and then refreshes
638 control using the base class method.
639
640 ._IsEditable() REQUIRED
641 Each class derived from MaskedEditMixin must define
642 the function used to determine if the base control is
643 editable or not. (For masked.ComboBox, this has to
644 be done with code, rather than specifying the proper
645 function in the base control, as there isn't one...)
646 ._CalcSize() REQUIRED
647 Each class derived from MaskedEditMixin must define
648 the function used to determine how wide the control
649 should be given the mask. (The mixin function
650 ._calcSize() provides a baseline estimate.)
651
652
653 Event Handling
654 --------------
655 Event handlers are "chained", and MaskedEditMixin usually
656 swallows most of the events it sees, thereby preventing any other
657 handlers from firing in the chain. It is therefore required that
658 each class derivation using the mixin to have an option to hook up
659 the event handlers itself or forego this operation and let a
660 subclass of the masked control do so. For this reason, each
661 subclass should probably include the following code:
662
663 if setupEventHandling:
664 ## Setup event handlers
665 EVT_SET_FOCUS( self, self._OnFocus ) ## defeat automatic full selection
666 EVT_KILL_FOCUS( self, self._OnKillFocus ) ## run internal validator
667 EVT_LEFT_DCLICK(self, self._OnDoubleClick) ## select field under cursor on dclick
668 EVT_RIGHT_UP(self, self._OnContextMenu ) ## bring up an appropriate context menu
669 EVT_KEY_DOWN( self, self._OnKeyDown ) ## capture control events not normally seen, eg ctrl-tab.
670 EVT_CHAR( self, self._OnChar ) ## handle each keypress
671 EVT_TEXT( self, self.GetId(), self._OnTextChange ) ## color control appropriately & keep
672 ## track of previous value for undo
673
674 where setupEventHandling is an argument to its constructor.
675
676 These 5 handlers must be "wired up" for the masked edit
677 controls to provide default behavior. (The setupEventHandling
678 is an argument to masked.TextCtrl and masked.ComboBox, so
679 that controls derived from *them* may replace one of these
680 handlers if they so choose.)
681
682 If your derived control wants to preprocess events before
683 taking action, it should then set up the event handling itself,
684 so it can be first in the event handler chain.
685
686
687 The following routines are available to facilitate changing
688 the default behavior of masked edit controls:
689
690 ._SetKeycodeHandler(keycode, func)
691 ._SetKeyHandler(char, func)
692 Use to replace default handling for any given keycode.
693 func should take the key event as argument and return
694 False if no further action is required to handle the
695 key. Eg:
696 self._SetKeycodeHandler(WXK_UP, self.IncrementValue)
697 self._SetKeyHandler('-', self._OnChangeSign)
698
699 "Navigation" keys are assumed to change the cursor position, and
700 therefore don't cause automatic motion of the cursor as insertable
701 characters do.
702
703 ._AddNavKeycode(keycode, handler=None)
704 ._AddNavKey(char, handler=None)
705 Allows controls to specify other keys (and optional handlers)
706 to be treated as navigational characters. (eg. '.' in IpAddrCtrl)
707
708 ._GetNavKeycodes() Returns the current list of navigational keycodes.
709
710 ._SetNavKeycodes(key_func_tuples)
711 Allows replacement of the current list of keycode
712 processed as navigation keys, and bind associated
713 optional keyhandlers. argument is a list of key/handler
714 tuples. Passing a value of None for the handler in a
715 given tuple indicates that default processing for the key
716 is desired.
717
718 ._FindField(pos) Returns the Field object associated with this position
719 in the control.
720
721 ._FindFieldExtent(pos, getslice=False, value=None)
722 Returns edit_start, edit_end of the field corresponding
723 to the specified position within the control, and
724 optionally also returns the current contents of that field.
725 If value is specified, it will retrieve the slice the corresponding
726 slice from that value, rather than the current value of the
727 control.
728
729 ._AdjustField(pos)
730 This is, the function that gets called for a given position
731 whenever the cursor is adjusted to leave a given field.
732 By default, it adjusts the year in date fields if mask is a date,
733 It can be overridden by a derived class to
734 adjust the value of the control at that time.
735 (eg. IpAddrCtrl reformats the address in this way.)
736
737 ._Change() Called by internal EVT_TEXT handler. Return False to force
738 skip of the normal class change event.
739 ._Keypress(key) Called by internal EVT_CHAR handler. Return False to force
740 skip of the normal class keypress event.
741 ._LostFocus() Called by internal EVT_KILL_FOCUS handler
742
743 ._OnKeyDown(event)
744 This is the default EVT_KEY_DOWN routine; it just checks for
745 "navigation keys", and if event.ControlDown(), it fires the
746 mixin's _OnChar() routine, as such events are not always seen
747 by the "cooked" EVT_CHAR routine.
748
749 ._OnChar(event) This is the main EVT_CHAR handler for the
750 MaskedEditMixin.
751
752 The following routines are used to handle standard actions
753 for control keys:
754 _OnArrow(event) used for arrow navigation events
755 _OnCtrl_A(event) 'select all'
756 _OnCtrl_C(event) 'copy' (uses base control function, as copy is non-destructive)
757 _OnCtrl_S(event) 'save' (does nothing)
758 _OnCtrl_V(event) 'paste' - calls _Paste() method, to do smart paste
759 _OnCtrl_X(event) 'cut' - calls _Cut() method, to "erase" selection
760 _OnCtrl_Z(event) 'undo' - resets value to previous value (if any)
761
762 _OnChangeField(event) primarily used for tab events, but can be
763 used for other keys (eg. '.' in IpAddrCtrl)
764
765 _OnErase(event) used for backspace and delete
766 _OnHome(event)
767 _OnEnd(event)
768
769 The following routine provides a hook back to any class derivations, so that
770 they can react to parameter changes before any value is set/reset as a result of
771 those changes. (eg. masked.ComboBox needs to detect when the choices list is
772 modified, either implicitly or explicitly, so it can reset the base control
773 to have the appropriate choice list *before* the initial value is reset to match.)
774
775 _OnCtrlParametersChanged()
776
777 Accessor Functions
778 ------------------
779 For convenience, each class derived from MaskedEditMixin should
780 define an accessors mixin, so that it exposes only those parameters
781 that make sense for the derivation. This is done with an intermediate
782 level of inheritance, ie:
783
784 class BaseMaskedTextCtrl( TextCtrl, MaskedEditMixin ):
785
786 class TextCtrl( BaseMaskedTextCtrl, MaskedEditAccessorsMixin ):
787 class ComboBox( BaseMaskedComboBox, MaskedEditAccessorsMixin ):
788 class NumCtrl( BaseMaskedTextCtrl, MaskedNumCtrlAccessorsMixin ):
789 class IpAddrCtrl( BaseMaskedTextCtrl, IpAddrCtrlAccessorsMixin ):
790 class TimeCtrl( BaseMaskedTextCtrl, TimeCtrlAccessorsMixin ):
791
792 etc.
793
794 Each accessors mixin defines Get/Set functions for the base class parameters
795 that are appropriate for that derivation.
796 This allows the base classes to be "more generic," exposing the widest
797 set of options, while not requiring derived classes to be so general.
798 """
799
800 import copy
801 import difflib
802 import re
803 import string
804 import types
805
806 import wx
807
808 # jmg 12/9/03 - when we cut ties with Py 2.2 and earlier, this would
809 # be a good place to implement the 2.3 logger class
810 from wx.tools.dbg import Logger
811
812 ##dbg = Logger()
813 ##dbg(enable=1)
814
815 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
816
817 ## Constants for identifying control keys and classes of keys:
818
819 WXK_CTRL_A = (ord('A')+1) - ord('A') ## These keys are not already defined in wx
820 WXK_CTRL_C = (ord('C')+1) - ord('A')
821 WXK_CTRL_S = (ord('S')+1) - ord('A')
822 WXK_CTRL_V = (ord('V')+1) - ord('A')
823 WXK_CTRL_X = (ord('X')+1) - ord('A')
824 WXK_CTRL_Z = (ord('Z')+1) - ord('A')
825
826 nav = (
827 wx.WXK_BACK, wx.WXK_LEFT, wx.WXK_RIGHT, wx.WXK_UP, wx.WXK_DOWN, wx.WXK_TAB,
828 wx.WXK_HOME, wx.WXK_END, wx.WXK_RETURN, wx.WXK_PRIOR, wx.WXK_NEXT
829 )
830
831 control = (
832 wx.WXK_BACK, wx.WXK_DELETE, WXK_CTRL_A, WXK_CTRL_C, WXK_CTRL_S, WXK_CTRL_V,
833 WXK_CTRL_X, WXK_CTRL_Z
834 )
835
836
837 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
838
839 ## Constants for masking. This is where mask characters
840 ## are defined.
841 ## maskchars used to identify valid mask characters from all others
842 ## #- allow numeric 0-9 only
843 ## A- allow uppercase only. Combine with forceupper to force lowercase to upper
844 ## a- allow lowercase only. Combine with forcelower to force upper to lowercase
845 ## X- allow any character (string.letters, string.punctuation, string.digits)
846 ## Note: locale settings affect what "uppercase", lowercase, etc comprise.
847 ##
848 maskchars = ("#","A","a","X","C","N",'*','&')
849 ansichars = ""
850 for i in xrange(32, 256):
851 ansichars += chr(i)
852
853 months = '(01|02|03|04|05|06|07|08|09|10|11|12)'
854 charmonths = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)'
855 charmonths_dict = {'jan': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5, 'jun': 6,
856 'jul': 7, 'aug': 8, 'sep': 9, 'oct': 10, 'nov': 11, 'dec': 12}
857
858 days = '(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)'
859 hours = '(0\d| \d|1[012])'
860 milhours = '(00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23)'
861 minutes = """(00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|\
862 16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|\
863 36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|\
864 56|57|58|59)"""
865 seconds = minutes
866 am_pm_exclude = 'BCDEFGHIJKLMNOQRSTUVWXYZ\x8a\x8c\x8e\x9f\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde'
867
868 states = "AL,AK,AZ,AR,CA,CO,CT,DE,DC,FL,GA,GU,HI,ID,IL,IN,IA,KS,KY,LA,MA,ME,MD,MI,MN,MS,MO,MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,PA,PR,RI,SC,SD,TN,TX,UT,VA,VT,VI,WA,WV,WI,WY".split(',')
869
870 state_names = ['Alabama','Alaska','Arizona','Arkansas',
871 'California','Colorado','Connecticut',
872 'Delaware','District of Columbia',
873 'Florida','Georgia','Hawaii',
874 'Idaho','Illinois','Indiana','Iowa',
875 'Kansas','Kentucky','Louisiana',
876 'Maine','Maryland','Massachusetts','Michigan',
877 'Minnesota','Mississippi','Missouri','Montana',
878 'Nebraska','Nevada','New Hampshire','New Jersey',
879 'New Mexico','New York','North Carolina','North Dakokta',
880 'Ohio','Oklahoma','Oregon',
881 'Pennsylvania','Puerto Rico','Rhode Island',
882 'South Carolina','South Dakota',
883 'Tennessee','Texas','Utah',
884 'Vermont','Virginia',
885 'Washington','West Virginia',
886 'Wisconsin','Wyoming']
887
888 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
889
890 ## The following dictionary defines the current set of autoformats:
891
892 masktags = {
893 "USPHONEFULLEXT": {
894 'mask': "(###) ###-#### x:###",
895 'formatcodes': 'F^->',
896 'validRegex': "^\(\d{3}\) \d{3}-\d{4}",
897 'description': "Phone Number w/opt. ext"
898 },
899 "USPHONETIGHTEXT": {
900 'mask': "###-###-#### x:###",
901 'formatcodes': 'F^->',
902 'validRegex': "^\d{3}-\d{3}-\d{4}",
903 'description': "Phone Number\n (w/hyphens and opt. ext)"
904 },
905 "USPHONEFULL": {
906 'mask': "(###) ###-####",
907 'formatcodes': 'F^->',
908 'validRegex': "^\(\d{3}\) \d{3}-\d{4}",
909 'description': "Phone Number only"
910 },
911 "USPHONETIGHT": {
912 'mask': "###-###-####",
913 'formatcodes': 'F^->',
914 'validRegex': "^\d{3}-\d{3}-\d{4}",
915 'description': "Phone Number\n(w/hyphens)"
916 },
917 "USSTATE": {
918 'mask': "AA",
919 'formatcodes': 'F!V',
920 'validRegex': "([ACDFGHIKLMNOPRSTUVW] |%s)" % string.join(states,'|'),
921 'choices': states,
922 'choiceRequired': True,
923 'description': "US State Code"
924 },
925 "USSTATENAME": {
926 'mask': "ACCCCCCCCCCCCCCCCCCC",
927 'formatcodes': 'F_',
928 'validRegex': "([ACDFGHIKLMNOPRSTUVW] |%s)" % string.join(state_names,'|'),
929 'choices': state_names,
930 'choiceRequired': True,
931 'description': "US State Name"
932 },
933
934 "USDATETIMEMMDDYYYY/HHMMSS": {
935 'mask': "##/##/#### ##:##:## AM",
936 'excludeChars': am_pm_exclude,
937 'formatcodes': 'DF!',
938 'validRegex': '^' + months + '/' + days + '/' + '\d{4} ' + hours + ':' + minutes + ':' + seconds + ' (A|P)M',
939 'description': "US Date + Time"
940 },
941 "USDATETIMEMMDDYYYY-HHMMSS": {
942 'mask': "##-##-#### ##:##:## AM",
943 'excludeChars': am_pm_exclude,
944 'formatcodes': 'DF!',
945 'validRegex': '^' + months + '-' + days + '-' + '\d{4} ' + hours + ':' + minutes + ':' + seconds + ' (A|P)M',
946 'description': "US Date + Time\n(w/hypens)"
947 },
948 "USDATE24HRTIMEMMDDYYYY/HHMMSS": {
949 'mask': "##/##/#### ##:##:##",
950 'formatcodes': 'DF',
951 'validRegex': '^' + months + '/' + days + '/' + '\d{4} ' + milhours + ':' + minutes + ':' + seconds,
952 'description': "US Date + 24Hr (Military) Time"
953 },
954 "USDATE24HRTIMEMMDDYYYY-HHMMSS": {
955 'mask': "##-##-#### ##:##:##",
956 'formatcodes': 'DF',
957 'validRegex': '^' + months + '-' + days + '-' + '\d{4} ' + milhours + ':' + minutes + ':' + seconds,
958 'description': "US Date + 24Hr Time\n(w/hypens)"
959 },
960 "USDATETIMEMMDDYYYY/HHMM": {
961 'mask': "##/##/#### ##:## AM",
962 'excludeChars': am_pm_exclude,
963 'formatcodes': 'DF!',
964 'validRegex': '^' + months + '/' + days + '/' + '\d{4} ' + hours + ':' + minutes + ' (A|P)M',
965 'description': "US Date + Time\n(without seconds)"
966 },
967 "USDATE24HRTIMEMMDDYYYY/HHMM": {
968 'mask': "##/##/#### ##:##",
969 'formatcodes': 'DF',
970 'validRegex': '^' + months + '/' + days + '/' + '\d{4} ' + milhours + ':' + minutes,
971 'description': "US Date + 24Hr Time\n(without seconds)"
972 },
973 "USDATETIMEMMDDYYYY-HHMM": {
974 'mask': "##-##-#### ##:## AM",
975 'excludeChars': am_pm_exclude,
976 'formatcodes': 'DF!',
977 'validRegex': '^' + months + '-' + days + '-' + '\d{4} ' + hours + ':' + minutes + ' (A|P)M',
978 'description': "US Date + Time\n(w/hypens and w/o secs)"
979 },
980 "USDATE24HRTIMEMMDDYYYY-HHMM": {
981 'mask': "##-##-#### ##:##",
982 'formatcodes': 'DF',
983 'validRegex': '^' + months + '-' + days + '-' + '\d{4} ' + milhours + ':' + minutes,
984 'description': "US Date + 24Hr Time\n(w/hyphens and w/o seconds)"
985 },
986 "USDATEMMDDYYYY/": {
987 'mask': "##/##/####",
988 'formatcodes': 'DF',
989 'validRegex': '^' + months + '/' + days + '/' + '\d{4}',
990 'description': "US Date\n(MMDDYYYY)"
991 },
992 "USDATEMMDDYY/": {
993 'mask': "##/##/##",
994 'formatcodes': 'DF',
995 'validRegex': '^' + months + '/' + days + '/\d\d',
996 'description': "US Date\n(MMDDYY)"
997 },
998 "USDATEMMDDYYYY-": {
999 'mask': "##-##-####",
1000 'formatcodes': 'DF',
1001 'validRegex': '^' + months + '-' + days + '-' +'\d{4}',
1002 'description': "MM-DD-YYYY"
1003 },
1004
1005 "EUDATEYYYYMMDD/": {
1006 'mask': "####/##/##",
1007 'formatcodes': 'DF',
1008 'validRegex': '^' + '\d{4}'+ '/' + months + '/' + days,
1009 'description': "YYYY/MM/DD"
1010 },
1011 "EUDATEYYYYMMDD.": {
1012 'mask': "####.##.##",
1013 'formatcodes': 'DF',
1014 'validRegex': '^' + '\d{4}'+ '.' + months + '.' + days,
1015 'description': "YYYY.MM.DD"
1016 },
1017 "EUDATEDDMMYYYY/": {
1018 'mask': "##/##/####",
1019 'formatcodes': 'DF',
1020 'validRegex': '^' + days + '/' + months + '/' + '\d{4}',
1021 'description': "DD/MM/YYYY"
1022 },
1023 "EUDATEDDMMYYYY.": {
1024 'mask': "##.##.####",
1025 'formatcodes': 'DF',
1026 'validRegex': '^' + days + '.' + months + '.' + '\d{4}',
1027 'description': "DD.MM.YYYY"
1028 },
1029 "EUDATEDDMMMYYYY.": {
1030 'mask': "##.CCC.####",
1031 'formatcodes': 'DF',
1032 'validRegex': '^' + days + '.' + charmonths + '.' + '\d{4}',
1033 'description': "DD.Month.YYYY"
1034 },
1035 "EUDATEDDMMMYYYY/": {
1036 'mask': "##/CCC/####",
1037 'formatcodes': 'DF',
1038 'validRegex': '^' + days + '/' + charmonths + '/' + '\d{4}',
1039 'description': "DD/Month/YYYY"
1040 },
1041
1042 "EUDATETIMEYYYYMMDD/HHMMSS": {
1043 'mask': "####/##/## ##:##:## AM",
1044 'excludeChars': am_pm_exclude,
1045 'formatcodes': 'DF!',
1046 'validRegex': '^' + '\d{4}'+ '/' + months + '/' + days + ' ' + hours + ':' + minutes + ':' + seconds + ' (A|P)M',
1047 'description': "YYYY/MM/DD HH:MM:SS"
1048 },
1049 "EUDATETIMEYYYYMMDD.HHMMSS": {
1050 'mask': "####.##.## ##:##:## AM",
1051 'excludeChars': am_pm_exclude,
1052 'formatcodes': 'DF!',
1053 'validRegex': '^' + '\d{4}'+ '.' + months + '.' + days + ' ' + hours + ':' + minutes + ':' + seconds + ' (A|P)M',
1054 'description': "YYYY.MM.DD HH:MM:SS"
1055 },
1056 "EUDATETIMEDDMMYYYY/HHMMSS": {
1057 'mask': "##/##/#### ##:##:## AM",
1058 'excludeChars': am_pm_exclude,
1059 'formatcodes': 'DF!',
1060 'validRegex': '^' + days + '/' + months + '/' + '\d{4} ' + hours + ':' + minutes + ':' + seconds + ' (A|P)M',
1061 'description': "DD/MM/YYYY HH:MM:SS"
1062 },
1063 "EUDATETIMEDDMMYYYY.HHMMSS": {
1064 'mask': "##.##.#### ##:##:## AM",
1065 'excludeChars': am_pm_exclude,
1066 'formatcodes': 'DF!',
1067 'validRegex': '^' + days + '.' + months + '.' + '\d{4} ' + hours + ':' + minutes + ':' + seconds + ' (A|P)M',
1068 'description': "DD.MM.YYYY HH:MM:SS"
1069 },
1070
1071 "EUDATETIMEYYYYMMDD/HHMM": {
1072 'mask': "####/##/## ##:## AM",
1073 'excludeChars': am_pm_exclude,
1074 'formatcodes': 'DF!',
1075 'validRegex': '^' + '\d{4}'+ '/' + months + '/' + days + ' ' + hours + ':' + minutes + ' (A|P)M',
1076 'description': "YYYY/MM/DD HH:MM"
1077 },
1078 "EUDATETIMEYYYYMMDD.HHMM": {
1079 'mask': "####.##.## ##:## AM",
1080 'excludeChars': am_pm_exclude,
1081 'formatcodes': 'DF!',
1082 'validRegex': '^' + '\d{4}'+ '.' + months + '.' + days + ' ' + hours + ':' + minutes + ' (A|P)M',
1083 'description': "YYYY.MM.DD HH:MM"
1084 },
1085 "EUDATETIMEDDMMYYYY/HHMM": {
1086 'mask': "##/##/#### ##:## AM",
1087 'excludeChars': am_pm_exclude,
1088 'formatcodes': 'DF!',
1089 'validRegex': '^' + days + '/' + months + '/' + '\d{4} ' + hours + ':' + minutes + ' (A|P)M',
1090 'description': "DD/MM/YYYY HH:MM"
1091 },
1092 "EUDATETIMEDDMMYYYY.HHMM": {
1093 'mask': "##.##.#### ##:## AM",
1094 'excludeChars': am_pm_exclude,
1095 'formatcodes': 'DF!',
1096 'validRegex': '^' + days + '.' + months + '.' + '\d{4} ' + hours + ':' + minutes + ' (A|P)M',
1097 'description': "DD.MM.YYYY HH:MM"
1098 },
1099
1100 "EUDATE24HRTIMEYYYYMMDD/HHMMSS": {
1101 'mask': "####/##/## ##:##:##",
1102 'formatcodes': 'DF',
1103 'validRegex': '^' + '\d{4}'+ '/' + months + '/' + days + ' ' + milhours + ':' + minutes + ':' + seconds,
1104 'description': "YYYY/MM/DD 24Hr Time"
1105 },
1106 "EUDATE24HRTIMEYYYYMMDD.HHMMSS": {
1107 'mask': "####.##.## ##:##:##",
1108 'formatcodes': 'DF',
1109 'validRegex': '^' + '\d{4}'+ '.' + months + '.' + days + ' ' + milhours + ':' + minutes + ':' + seconds,
1110 'description': "YYYY.MM.DD 24Hr Time"
1111 },
1112 "EUDATE24HRTIMEDDMMYYYY/HHMMSS": {
1113 'mask': "##/##/#### ##:##:##",
1114 'formatcodes': 'DF',
1115 'validRegex': '^' + days + '/' + months + '/' + '\d{4} ' + milhours + ':' + minutes + ':' + seconds,
1116 'description': "DD/MM/YYYY 24Hr Time"
1117 },
1118 "EUDATE24HRTIMEDDMMYYYY.HHMMSS": {
1119 'mask': "##.##.#### ##:##:##",
1120 'formatcodes': 'DF',
1121 'validRegex': '^' + days + '.' + months + '.' + '\d{4} ' + milhours + ':' + minutes + ':' + seconds,
1122 'description': "DD.MM.YYYY 24Hr Time"
1123 },
1124 "EUDATE24HRTIMEYYYYMMDD/HHMM": {
1125 'mask': "####/##/## ##:##",
1126 'formatcodes': 'DF','validRegex': '^' + '\d{4}'+ '/' + months + '/' + days + ' ' + milhours + ':' + minutes,
1127 'description': "YYYY/MM/DD 24Hr Time\n(w/o seconds)"
1128 },
1129 "EUDATE24HRTIMEYYYYMMDD.HHMM": {
1130 'mask': "####.##.## ##:##",
1131 'formatcodes': 'DF',
1132 'validRegex': '^' + '\d{4}'+ '.' + months + '.' + days + ' ' + milhours + ':' + minutes,
1133 'description': "YYYY.MM.DD 24Hr Time\n(w/o seconds)"
1134 },
1135 "EUDATE24HRTIMEDDMMYYYY/HHMM": {
1136 'mask': "##/##/#### ##:##",
1137 'formatcodes': 'DF',
1138 'validRegex': '^' + days + '/' + months + '/' + '\d{4} ' + milhours + ':' + minutes,
1139 'description': "DD/MM/YYYY 24Hr Time\n(w/o seconds)"
1140 },
1141 "EUDATE24HRTIMEDDMMYYYY.HHMM": {
1142 'mask': "##.##.#### ##:##",
1143 'formatcodes': 'DF',
1144 'validRegex': '^' + days + '.' + months + '.' + '\d{4} ' + milhours + ':' + minutes,
1145 'description': "DD.MM.YYYY 24Hr Time\n(w/o seconds)"
1146 },
1147
1148 "TIMEHHMMSS": {
1149 'mask': "##:##:## AM",
1150 'excludeChars': am_pm_exclude,
1151 'formatcodes': 'TF!',
1152 'validRegex': '^' + hours + ':' + minutes + ':' + seconds + ' (A|P)M',
1153 'description': "HH:MM:SS (A|P)M\n(see TimeCtrl)"
1154 },
1155 "TIMEHHMM": {
1156 'mask': "##:## AM",
1157 'excludeChars': am_pm_exclude,
1158 'formatcodes': 'TF!',
1159 'validRegex': '^' + hours + ':' + minutes + ' (A|P)M',
1160 'description': "HH:MM (A|P)M\n(see TimeCtrl)"
1161 },
1162 "24HRTIMEHHMMSS": {
1163 'mask': "##:##:##",
1164 'formatcodes': 'TF',
1165 'validRegex': '^' + milhours + ':' + minutes + ':' + seconds,
1166 'description': "24Hr HH:MM:SS\n(see TimeCtrl)"
1167 },
1168 "24HRTIMEHHMM": {
1169 'mask': "##:##",
1170 'formatcodes': 'TF',
1171 'validRegex': '^' + milhours + ':' + minutes,
1172 'description': "24Hr HH:MM\n(see TimeCtrl)"
1173 },
1174 "USSOCIALSEC": {
1175 'mask': "###-##-####",
1176 'formatcodes': 'F',
1177 'validRegex': "\d{3}-\d{2}-\d{4}",
1178 'description': "Social Sec#"
1179 },
1180 "CREDITCARD": {
1181 'mask': "####-####-####-####",
1182 'formatcodes': 'F',
1183 'validRegex': "\d{4}-\d{4}-\d{4}-\d{4}",
1184 'description': "Credit Card"
1185 },
1186 "EXPDATEMMYY": {
1187 'mask': "##/##",
1188 'formatcodes': "F",
1189 'validRegex': "^" + months + "/\d\d",
1190 'description': "Expiration MM/YY"
1191 },
1192 "USZIP": {
1193 'mask': "#####",
1194 'formatcodes': 'F',
1195 'validRegex': "^\d{5}",
1196 'description': "US 5-digit zip code"
1197 },
1198 "USZIPPLUS4": {
1199 'mask': "#####-####",
1200 'formatcodes': 'F',
1201 'validRegex': "\d{5}-(\s{4}|\d{4})",
1202 'description': "US zip+4 code"
1203 },
1204 "PERCENT": {
1205 'mask': "0.##",
1206 'formatcodes': 'F',
1207 'validRegex': "^0.\d\d",
1208 'description': "Percentage"
1209 },
1210 "AGE": {
1211 'mask': "###",
1212 'formatcodes': "F",
1213 'validRegex': "^[1-9]{1} |[1-9][0-9] |1[0|1|2][0-9]",
1214 'description': "Age"
1215 },
1216 "EMAIL": {
1217 'mask': "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
1218 'excludeChars': " \\/*&%$#!+='\"",
1219 'formatcodes': "F>",
1220 'validRegex': "^\w+([\-\.]\w+)*@((([a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*\.)+)[a-zA-Z]{2,4}|\[(\d|\d\d|(1\d\d|2[0-4]\d|25[0-5]))(\.(\d|\d\d|(1\d\d|2[0-4]\d|25[0-5]))){3}\]) *$",
1221 'description': "Email address"
1222 },
1223 "IPADDR": {
1224 'mask': "###.###.###.###",
1225 'formatcodes': 'F_Sr',
1226 'validRegex': "( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))(\.( \d| \d\d|(1\d\d|2[0-4]\d|25[0-5]))){3}",
1227 'description': "IP Address\n(see IpAddrCtrl)"
1228 }
1229 }
1230
1231 # build demo-friendly dictionary of descriptions of autoformats
1232 autoformats = []
1233 for key, value in masktags.items():
1234 autoformats.append((key, value['description']))
1235 autoformats.sort()
1236
1237 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
1238
1239 class Field:
1240 """
1241 This class manages the individual fields in a masked edit control.
1242 Each field has a zero-based index, indicating its position in the
1243 control, an extent, an associated mask, and a plethora of optional
1244 parameters. Fields can be instantiated and then associated with
1245 parent masked controls, in order to provide field-specific configuration.
1246 Alternatively, fields will be implicitly created by the parent control
1247 if not provided at construction, at which point, the fields can then
1248 manipulated by the controls .SetFieldParameters() method.
1249 """
1250 valid_params = {
1251 'index': None, ## which field of mask; set by parent control.
1252 'mask': "", ## mask chars for this field
1253 'extent': (), ## (edit start, edit_end) of field; set by parent control.
1254 'formatcodes': "", ## codes indicating formatting options for the control
1255 'fillChar': ' ', ## used as initial value for each mask position if initial value is not given
1256 'groupChar': ',', ## used with numeric fields; indicates what char groups 3-tuple digits
1257 'decimalChar': '.', ## used with numeric fields; indicates what char separates integer from fraction
1258 'shiftDecimalChar': '>', ## used with numeric fields, indicates what is above the decimal point char on keyboard
1259 'useParensForNegatives': False, ## used with numeric fields, indicates that () should be used vs. - to show negative numbers.
1260 'defaultValue': "", ## use if you want different positional defaults vs. all the same fillChar
1261 'excludeChars': "", ## optional string of chars to exclude even if main mask type does
1262 'includeChars': "", ## optional string of chars to allow even if main mask type doesn't
1263 'validRegex': "", ## optional regular expression to use to validate the control
1264 'validRange': (), ## Optional hi-low range for numerics
1265 'choices': [], ## Optional list for character expressions
1266 'choiceRequired': False, ## If choices supplied this specifies if valid value must be in the list
1267 'compareNoCase': False, ## Optional flag to indicate whether or not to use case-insensitive list search
1268 'autoSelect': False, ## Set to True to try auto-completion on each keystroke:
1269 'validFunc': None, ## Optional function for defining additional, possibly dynamic validation constraints on contrl
1270 'validRequired': False, ## Set to True to disallow input that results in an invalid value
1271 'emptyInvalid': False, ## Set to True to make EMPTY = INVALID
1272 'description': "", ## primarily for autoformats, but could be useful elsewhere
1273 }
1274
1275 # This list contains all parameters that when set at the control level should
1276 # propagate down to each field:
1277 propagating_params = ('fillChar', 'groupChar', 'decimalChar','useParensForNegatives',
1278 'compareNoCase', 'emptyInvalid', 'validRequired')
1279
1280 def __init__(self, **kwargs):
1281 """
1282 This is the "constructor" for setting up parameters for fields.
1283 a field_index of -1 is used to indicate "the entire control."
1284 """
1285 #### dbg('Field::Field', indent=1)
1286 # Validate legitimate set of parameters:
1287 for key in kwargs.keys():
1288 if key not in Field.valid_params.keys():
1289 #### dbg(indent=0)
1290 raise TypeError('invalid parameter "%s"' % (key))
1291
1292 # Set defaults for each parameter for this instance, and fully
1293 # populate initial parameter list for configuration:
1294 for key, value in Field.valid_params.items():
1295 setattr(self, '_' + key, copy.copy(value))
1296 if not kwargs.has_key(key):
1297 kwargs[key] = copy.copy(value)
1298
1299 self._autoCompleteIndex = -1
1300 self._SetParameters(**kwargs)
1301 self._ValidateParameters(**kwargs)
1302
1303 #### dbg(indent=0)
1304
1305
1306 def _SetParameters(self, **kwargs):
1307 """
1308 This function can be used to set individual or multiple parameters for
1309 a masked edit field parameter after construction.
1310 """
1311 ## dbg(suspend=1)
1312 ## dbg('maskededit.Field::_SetParameters', indent=1)
1313 # Validate keyword arguments:
1314 for key in kwargs.keys():
1315 if key not in Field.valid_params.keys():
1316 ## dbg(indent=0, suspend=0)
1317 raise AttributeError('invalid keyword argument "%s"' % key)
1318
1319 ## if self._index is not None: dbg('field index:', self._index)
1320 ## dbg('parameters:', indent=1)
1321 for key, value in kwargs.items():
1322 ## dbg('%s:' % key, value)
1323 pass
1324 ## dbg(indent=0)
1325
1326
1327 old_fillChar = self._fillChar # store so we can change choice lists accordingly if it changes
1328
1329 # First, Assign all parameters specified:
1330 for key in Field.valid_params.keys():
1331 if kwargs.has_key(key):
1332 setattr(self, '_' + key, kwargs[key] )
1333
1334 if kwargs.has_key('formatcodes'): # (set/changed)
1335 self._forceupper = '!' in self._formatcodes
1336 self._forcelower = '^' in self._formatcodes
1337 self._groupdigits = ',' in self._formatcodes
1338 self._okSpaces = '_' in self._formatcodes
1339 self._padZero = '0' in self._formatcodes
1340 self._autofit = 'F' in self._formatcodes
1341 self._insertRight = 'r' in self._formatcodes
1342 self._allowInsert = '>' in self._formatcodes
1343 self._alignRight = 'R' in self._formatcodes or 'r' in self._formatcodes
1344 self._moveOnFieldFull = not '<' in self._formatcodes
1345 self._selectOnFieldEntry = 'S' in self._formatcodes
1346
1347 if kwargs.has_key('groupChar'):
1348 self._groupChar = kwargs['groupChar']
1349 if kwargs.has_key('decimalChar'):
1350 self._decimalChar = kwargs['decimalChar']
1351 if kwargs.has_key('shiftDecimalChar'):
1352 self._shiftDecimalChar = kwargs['shiftDecimalChar']
1353
1354 if kwargs.has_key('formatcodes') or kwargs.has_key('validRegex'):
1355 self._regexMask = 'V' in self._formatcodes and self._validRegex
1356
1357 if kwargs.has_key('fillChar'):
1358 self._old_fillChar = old_fillChar
1359 #### dbg("self._old_fillChar: '%s'" % self._old_fillChar)
1360
1361 if kwargs.has_key('mask') or kwargs.has_key('validRegex'): # (set/changed)
1362 self._isInt = _isInteger(self._mask)
1363 ## dbg('isInt?', self._isInt, 'self._mask:"%s"' % self._mask)
1364
1365 ## dbg(indent=0, suspend=0)
1366
1367
1368 def _ValidateParameters(self, **kwargs):
1369 """
1370 This function can be used to validate individual or multiple parameters for
1371 a masked edit field parameter after construction.
1372 """
1373 ## dbg(suspend=1)
1374 ## dbg('maskededit.Field::_ValidateParameters', indent=1)
1375 ## if self._index is not None: dbg('field index:', self._index)
1376 #### dbg('parameters:', indent=1)
1377 ## for key, value in kwargs.items():
1378 #### dbg('%s:' % key, value)
1379 #### dbg(indent=0)
1380 #### dbg("self._old_fillChar: '%s'" % self._old_fillChar)
1381
1382 # Verify proper numeric format params:
1383 if self._groupdigits and self._groupChar == self._decimalChar:
1384 ## dbg(indent=0, suspend=0)
1385 raise AttributeError("groupChar '%s' cannot be the same as decimalChar '%s'" % (self._groupChar, self._decimalChar))
1386
1387
1388 # Now go do validation, semantic and inter-dependency parameter processing:
1389 if kwargs.has_key('choices') or kwargs.has_key('compareNoCase') or kwargs.has_key('choiceRequired'): # (set/changed)
1390
1391 self._compareChoices = [choice.strip() for choice in self._choices]
1392
1393 if self._compareNoCase and self._choices:
1394 self._compareChoices = [item.lower() for item in self._compareChoices]
1395
1396 if kwargs.has_key('choices'):
1397 self._autoCompleteIndex = -1
1398
1399
1400 if kwargs.has_key('validRegex'): # (set/changed)
1401 if self._validRegex:
1402 try:
1403 if self._compareNoCase:
1404 self._filter = re.compile(self._validRegex, re.IGNORECASE)
1405 else:
1406 self._filter = re.compile(self._validRegex)
1407 except:
1408 ## dbg(indent=0, suspend=0)
1409 raise TypeError('%s: validRegex "%s" not a legal regular expression' % (str(self._index), self._validRegex))
1410 else:
1411 self._filter = None
1412
1413 if kwargs.has_key('validRange'): # (set/changed)
1414 self._hasRange = False
1415 self._rangeHigh = 0
1416 self._rangeLow = 0
1417 if self._validRange:
1418 if type(self._validRange) != types.TupleType or len( self._validRange )!= 2 or self._validRange[0] > self._validRange[1]:
1419 ## dbg(indent=0, suspend=0)
1420 raise TypeError('%s: validRange %s parameter must be tuple of form (a,b) where a <= b'
1421 % (str(self._index), repr(self._validRange)) )
1422
1423 self._hasRange = True
1424 self._rangeLow = self._validRange[0]
1425 self._rangeHigh = self._validRange[1]
1426
1427 if kwargs.has_key('choices') or (len(self._choices) and len(self._choices[0]) != len(self._mask)): # (set/changed)
1428 self._hasList = False
1429 if self._choices and type(self._choices) not in (types.TupleType, types.ListType):
1430 ## dbg(indent=0, suspend=0)
1431 raise TypeError('%s: choices must be a sequence of strings' % str(self._index))
1432 elif len( self._choices) > 0:
1433 for choice in self._choices:
1434 if type(choice) not in (types.StringType, types.UnicodeType):
1435 ## dbg(indent=0, suspend=0)
1436 raise TypeError('%s: choices must be a sequence of strings' % str(self._index))
1437
1438 length = len(self._mask)
1439 ## dbg('len(%s)' % self._mask, length, 'len(self._choices):', len(self._choices), 'length:', length, 'self._alignRight?', self._alignRight)
1440 if len(self._choices) and length:
1441 if len(self._choices[0]) > length:
1442 # changed mask without respecifying choices; readjust the width as appropriate:
1443 self._choices = [choice.strip() for choice in self._choices]
1444 if self._alignRight:
1445 self._choices = [choice.rjust( length ) for choice in self._choices]
1446 else:
1447 self._choices = [choice.ljust( length ) for choice in self._choices]
1448 ## dbg('aligned choices:', self._choices)
1449
1450 if hasattr(self, '_template'):
1451 # Verify each choice specified is valid:
1452 for choice in self._choices:
1453 if self.IsEmpty(choice) and not self._validRequired:
1454 # allow empty values even if invalid, (just colored differently)
1455 continue
1456 if not self.IsValid(choice):
1457 ## dbg(indent=0, suspend=0)
1458 raise ValueError('%s: "%s" is not a valid value for the control as specified.' % (str(self._index), choice))
1459 self._hasList = True
1460
1461 #### dbg("kwargs.has_key('fillChar')?", kwargs.has_key('fillChar'), "len(self._choices) > 0?", len(self._choices) > 0)
1462 #### dbg("self._old_fillChar:'%s'" % self._old_fillChar, "self._fillChar: '%s'" % self._fillChar)
1463 if kwargs.has_key('fillChar') and len(self._choices) > 0:
1464 if kwargs['fillChar'] != ' ':
1465 self._choices = [choice.replace(' ', self._fillChar) for choice in self._choices]
1466 else:
1467 self._choices = [choice.replace(self._old_fillChar, self._fillChar) for choice in self._choices]
1468 ## dbg('updated choices:', self._choices)
1469
1470
1471 if kwargs.has_key('autoSelect') and kwargs['autoSelect']:
1472 if not self._hasList:
1473 ## dbg('no list to auto complete; ignoring "autoSelect=True"')
1474 self._autoSelect = False
1475
1476 # reset field validity assumption:
1477 self._valid = True
1478 ## dbg(indent=0, suspend=0)
1479
1480
1481 def _GetParameter(self, paramname):
1482 """
1483 Routine for retrieving the value of any given parameter
1484 """
1485 if Field.valid_params.has_key(paramname):
1486 return getattr(self, '_' + paramname)
1487 else:
1488 TypeError('Field._GetParameter: invalid parameter "%s"' % key)
1489
1490
1491 def IsEmpty(self, slice):
1492 """
1493 Indicates whether the specified slice is considered empty for the
1494 field.
1495 """
1496 ## dbg('Field::IsEmpty("%s")' % slice, indent=1)
1497 if not hasattr(self, '_template'):
1498 ## dbg(indent=0)
1499 raise AttributeError('_template')
1500
1501 ## dbg('self._template: "%s"' % self._template)
1502 ## dbg('self._defaultValue: "%s"' % str(self._defaultValue))
1503 if slice == self._template and not self._defaultValue:
1504 ## dbg(indent=0)
1505 return True
1506
1507 elif slice == self._template:
1508 empty = True
1509 for pos in range(len(self._template)):
1510 #### dbg('slice[%(pos)d] != self._fillChar?' %locals(), slice[pos] != self._fillChar[pos])
1511 if slice[pos] not in (' ', self._fillChar):
1512 empty = False
1513 break
1514 ## dbg("IsEmpty? %(empty)d (do all mask chars == fillChar?)" % locals(), indent=0)
1515 return empty
1516 else:
1517 ## dbg("IsEmpty? 0 (slice doesn't match template)", indent=0)
1518 return False
1519
1520
1521 def IsValid(self, slice):
1522 """
1523 Indicates whether the specified slice is considered a valid value for the
1524 field.
1525 """
1526 ## dbg(suspend=1)
1527 ## dbg('Field[%s]::IsValid("%s")' % (str(self._index), slice), indent=1)
1528 valid = True # assume true to start
1529
1530 if self.IsEmpty(slice):
1531 ## dbg(indent=0, suspend=0)
1532 if self._emptyInvalid:
1533 return False
1534 else:
1535 return True
1536
1537 elif self._hasList and self._choiceRequired:
1538 ## dbg("(member of list required)")
1539 # do case-insensitive match on list; strip surrounding whitespace from slice (already done for choices):
1540 if self._fillChar != ' ':
1541 slice = slice.replace(self._fillChar, ' ')
1542 ## dbg('updated slice:"%s"' % slice)
1543 compareStr = slice.strip()
1544
1545 if self._compareNoCase:
1546 compareStr = compareStr.lower()
1547 valid = compareStr in self._compareChoices
1548
1549 elif self._hasRange and not self.IsEmpty(slice):
1550 ## dbg('validating against range')
1551 try:
1552 # allow float as well as int ranges (int comparisons for free.)
1553 valid = self._rangeLow <= float(slice) <= self._rangeHigh
1554 except:
1555 valid = False
1556
1557 elif self._validRegex and self._filter:
1558 ## dbg('validating against regex')
1559 valid = (re.match( self._filter, slice) is not None)
1560
1561 if valid and self._validFunc:
1562 ## dbg('validating against supplied function')
1563 valid = self._validFunc(slice)
1564 ## dbg('valid?', valid, indent=0, suspend=0)
1565 return valid
1566
1567
1568 def _AdjustField(self, slice):
1569 """ 'Fixes' an integer field. Right or left-justifies, as required."""
1570 ## dbg('Field::_AdjustField("%s")' % slice, indent=1)
1571 length = len(self._mask)
1572 #### dbg('length(self._mask):', length)
1573 #### dbg('self._useParensForNegatives?', self._useParensForNegatives)
1574 if self._isInt:
1575 if self._useParensForNegatives:
1576 signpos = slice.find('(')
1577 right_signpos = slice.find(')')
1578 intStr = slice.replace('(', '').replace(')', '') # drop sign, if any
1579 else:
1580 signpos = slice.find('-')
1581 intStr = slice.replace( '-', '' ) # drop sign, if any
1582 right_signpos = -1
1583
1584 intStr = intStr.replace(' ', '') # drop extra spaces
1585 intStr = string.replace(intStr,self._fillChar,"") # drop extra fillchars
1586 intStr = string.replace(intStr,"-","") # drop sign, if any
1587 intStr = string.replace(intStr, self._groupChar, "") # lose commas/dots
1588 #### dbg('intStr:"%s"' % intStr)
1589 start, end = self._extent
1590 field_len = end - start
1591 if not self._padZero and len(intStr) != field_len and intStr.strip():
1592 intStr = str(long(intStr))
1593 #### dbg('raw int str: "%s"' % intStr)
1594 #### dbg('self._groupdigits:', self._groupdigits, 'self._formatcodes:', self._formatcodes)
1595 if self._groupdigits:
1596 new = ''
1597 cnt = 1
1598 for i in range(len(intStr)-1, -1, -1):
1599 new = intStr[i] + new
1600 if (cnt) % 3 == 0:
1601 new = self._groupChar + new
1602 cnt += 1
1603 if new and new[0] == self._groupChar:
1604 new = new[1:]
1605 if len(new) <= length:
1606 # expanded string will still fit and leave room for sign:
1607 intStr = new
1608 # else... leave it without the commas...
1609
1610 ## dbg('padzero?', self._padZero)
1611 ## dbg('len(intStr):', len(intStr), 'field length:', length)
1612 if self._padZero and len(intStr) < length:
1613 intStr = '0' * (length - len(intStr)) + intStr
1614 if signpos != -1: # we had a sign before; restore it
1615 if self._useParensForNegatives:
1616 intStr = '(' + intStr[1:]
1617 if right_signpos != -1:
1618 intStr += ')'
1619 else:
1620 intStr = '-' + intStr[1:]
1621 elif signpos != -1 and slice[0:signpos].strip() == '': # - was before digits
1622 if self._useParensForNegatives:
1623 intStr = '(' + intStr
1624 if right_signpos != -1:
1625 intStr += ')'
1626 else:
1627 intStr = '-' + intStr
1628 elif right_signpos != -1:
1629 # must have had ')' but '(' was before field; re-add ')'
1630 intStr += ')'
1631 slice = intStr
1632
1633 slice = slice.strip() # drop extra spaces
1634
1635 if self._alignRight: ## Only if right-alignment is enabled
1636 slice = slice.rjust( length )
1637 else:
1638 slice = slice.ljust( length )
1639 if self._fillChar != ' ':
1640 slice = slice.replace(' ', self._fillChar)
1641 ## dbg('adjusted slice: "%s"' % slice, indent=0)
1642 return slice
1643
1644
1645 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
1646
1647 class MaskedEditMixin:
1648 """
1649 This class allows us to abstract the masked edit functionality that could
1650 be associated with any text entry control. (eg. wx.TextCtrl, wx.ComboBox, etc.)
1651 It forms the basis for all of the lib.masked controls.
1652 """
1653 valid_ctrl_params = {
1654 'mask': 'XXXXXXXXXXXXX', ## mask string for formatting this control
1655 'autoformat': "", ## optional auto-format code to set format from masktags dictionary
1656 'fields': {}, ## optional list/dictionary of maskededit.Field class instances, indexed by position in mask
1657 'datestyle': 'MDY', ## optional date style for date-type values. Can trigger autocomplete year
1658 'autoCompleteKeycodes': [], ## Optional list of additional keycodes which will invoke field-auto-complete
1659 'useFixedWidthFont': True, ## Use fixed-width font instead of default for base control
1660 'defaultEncoding': 'latin1', ## optional argument to indicate unicode codec to use (unicode ctrls only)
1661 'retainFieldValidation': False, ## Set this to true if setting control-level parameters independently,
1662 ## from field validation constraints
1663 'emptyBackgroundColour': "White",
1664 'validBackgroundColour': "White",
1665 'invalidBackgroundColour': "Yellow",
1666 'foregroundColour': "Black",
1667 'signedForegroundColour': "Red",
1668 'demo': False}
1669
1670
1671 def __init__(self, name = 'MaskedEdit', **kwargs):
1672 """
1673 This is the "constructor" for setting up the mixin variable parameters for the composite class.
1674 """
1675
1676 self.name = name
1677
1678 # set up flag for doing optional things to base control if possible
1679 if not hasattr(self, 'controlInitialized'):
1680 self.controlInitialized = False
1681
1682 # Set internal state var for keeping track of whether or not a character
1683 # action results in a modification of the control, since .SetValue()
1684 # doesn't modify the base control's internal state:
1685 self.modified = False
1686 self._previous_mask = None
1687
1688 # Validate legitimate set of parameters:
1689 for key in kwargs.keys():
1690 if key.replace('Color', 'Colour') not in MaskedEditMixin.valid_ctrl_params.keys() + Field.valid_params.keys():
1691 raise TypeError('%s: invalid parameter "%s"' % (name, key))
1692
1693 ## Set up dictionary that can be used by subclasses to override or add to default
1694 ## behavior for individual characters. Derived subclasses needing to change
1695 ## default behavior for keys can either redefine the default functions for the
1696 ## common keys or add functions for specific keys to this list. Each function
1697 ## added should take the key event as argument, and return False if the key
1698 ## requires no further processing.
1699 ##
1700 ## Initially populated with navigation and function control keys:
1701 self._keyhandlers = {
1702 # default navigation keys and handlers:
1703 wx.WXK_BACK: self._OnErase,
1704 wx.WXK_LEFT: self._OnArrow,
1705 wx.WXK_RIGHT: self._OnArrow,
1706 wx.WXK_UP: self._OnAutoCompleteField,
1707 wx.WXK_DOWN: self._OnAutoCompleteField,
1708 wx.WXK_TAB: self._OnChangeField,
1709 wx.WXK_HOME: self._OnHome,
1710 wx.WXK_END: self._OnEnd,
1711 wx.WXK_RETURN: self._OnReturn,
1712 wx.WXK_PRIOR: self._OnAutoCompleteField,
1713 wx.WXK_NEXT: self._OnAutoCompleteField,
1714
1715 # default function control keys and handlers:
1716 wx.WXK_DELETE: self._OnErase,
1717 WXK_CTRL_A: self._OnCtrl_A,
1718 WXK_CTRL_C: self._OnCtrl_C,
1719 WXK_CTRL_S: self._OnCtrl_S,
1720 WXK_CTRL_V: self._OnCtrl_V,
1721 WXK_CTRL_X: self._OnCtrl_X,
1722 WXK_CTRL_Z: self._OnCtrl_Z,
1723 }
1724
1725 ## bind standard navigational and control keycodes to this instance,
1726 ## so that they can be augmented and/or changed in derived classes:
1727 self._nav = list(nav)
1728 self._control = list(control)
1729
1730 ## Dynamically evaluate and store string constants for mask chars
1731 ## so that locale settings can be made after this module is imported
1732 ## and the controls created after that is done can allow the
1733 ## appropriate characters:
1734 self.maskchardict = {
1735 '#': string.digits,
1736 'A': string.uppercase,
1737 'a': string.lowercase,
1738 'X': string.letters + string.punctuation + string.digits,
1739 'C': string.letters,
1740 'N': string.letters + string.digits,
1741 '&': string.punctuation,
1742 '*': ansichars
1743 }
1744
1745 ## self._ignoreChange is used by MaskedComboBox, because
1746 ## of the hack necessary to determine the selection; it causes
1747 ## EVT_TEXT messages from the combobox to be ignored if set.
1748 self._ignoreChange = False
1749
1750 # These are used to keep track of previous value, for undo functionality:
1751 self._curValue = None
1752 self._prevValue = None
1753
1754 self._valid = True
1755
1756 # Set defaults for each parameter for this instance, and fully
1757 # populate initial parameter list for configuration:
1758 for key, value in MaskedEditMixin.valid_ctrl_params.items():
1759 setattr(self, '_' + key, copy.copy(value))
1760 if not kwargs.has_key(key):
1761 #### dbg('%s: "%s"' % (key, repr(value)))
1762 kwargs[key] = copy.copy(value)
1763
1764 # Create a "field" that holds global parameters for control constraints
1765 self._ctrl_constraints = self._fields[-1] = Field(index=-1)
1766 self.SetCtrlParameters(**kwargs)
1767
1768
1769
1770 def SetCtrlParameters(self, **kwargs):
1771 """
1772 This public function can be used to set individual or multiple masked edit
1773 parameters after construction. (See maskededit module overview for the list
1774 of valid parameters.)
1775 """
1776 ## dbg(suspend=1)
1777 ## dbg('MaskedEditMixin::SetCtrlParameters', indent=1)
1778 #### dbg('kwargs:', indent=1)
1779 ## for key, value in kwargs.items():
1780 #### dbg(key, '=', value)
1781 #### dbg(indent=0)
1782
1783 # Validate keyword arguments:
1784 constraint_kwargs = {}
1785 ctrl_kwargs = {}
1786 for key, value in kwargs.items():
1787 key = key.replace('Color', 'Colour') # for b-c, and standard wxPython spelling
1788 if key not in MaskedEditMixin.valid_ctrl_params.keys() + Field.valid_params.keys():
1789 ## dbg(indent=0, suspend=0)
1790 raise TypeError('Invalid keyword argument "%s" for control "%s"' % (key, self.name))
1791 elif key in Field.valid_params.keys():
1792 constraint_kwargs[key] = value
1793 else:
1794 ctrl_kwargs[key] = value
1795
1796 mask = None
1797 reset_args = {}
1798
1799 if ctrl_kwargs.has_key('autoformat'):
1800 autoformat = ctrl_kwargs['autoformat']
1801 else:
1802 autoformat = None
1803
1804 # handle "parochial name" backward compatibility:
1805 if autoformat and autoformat.find('MILTIME') != -1 and autoformat not in masktags.keys():
1806 autoformat = autoformat.replace('MILTIME', '24HRTIME')
1807
1808 if autoformat != self._autoformat and autoformat in masktags.keys():
1809 ## dbg('autoformat:', autoformat)
1810 self._autoformat = autoformat
1811 mask = masktags[self._autoformat]['mask']
1812 # gather rest of any autoformat parameters:
1813 for param, value in masktags[self._autoformat].items():
1814 if param == 'mask': continue # (must be present; already accounted for)
1815 constraint_kwargs[param] = value
1816
1817 elif autoformat and not autoformat in masktags.keys():
1818 raise AttributeError('invalid value for autoformat parameter: %s' % repr(autoformat))
1819 else:
1820 ## dbg('autoformat not selected')
1821 if kwargs.has_key('mask'):
1822 mask = kwargs['mask']
1823 ## dbg('mask:', mask)
1824
1825 ## Assign style flags
1826 if mask is None:
1827 ## dbg('preserving previous mask')
1828 mask = self._previous_mask # preserve previous mask
1829 else:
1830 ## dbg('mask (re)set')
1831 reset_args['reset_mask'] = mask
1832 constraint_kwargs['mask'] = mask
1833
1834 # wipe out previous fields; preserve new control-level constraints
1835 self._fields = {-1: self._ctrl_constraints}
1836
1837
1838 if ctrl_kwargs.has_key('fields'):
1839 # do field parameter type validation, and conversion to internal dictionary
1840 # as appropriate:
1841 fields = ctrl_kwargs['fields']
1842 if type(fields) in (types.ListType, types.TupleType):
1843 for i in range(len(fields)):
1844 field = fields[i]
1845 if not isinstance(field, Field):
1846 ## dbg(indent=0, suspend=0)
1847 raise AttributeError('invalid type for field parameter: %s' % repr(field))
1848 self._fields[i] = field
1849
1850 elif type(fields) == types.DictionaryType:
1851 for index, field in fields.items():
1852 if not isinstance(field, Field):
1853 ## dbg(indent=0, suspend=0)
1854 raise AttributeError('invalid type for field parameter: %s' % repr(field))
1855 self._fields[index] = field
1856 else:
1857 ## dbg(indent=0, suspend=0)
1858 raise AttributeError('fields parameter must be a list or dictionary; not %s' % repr(fields))
1859
1860 # Assign constraint parameters for entire control:
1861 #### dbg('control constraints:', indent=1)
1862 ## for key, value in constraint_kwargs.items():
1863 #### dbg('%s:' % key, value)
1864 #### dbg(indent=0)
1865
1866 # determine if changing parameters that should affect the entire control:
1867 for key in MaskedEditMixin.valid_ctrl_params.keys():
1868 if key in ( 'mask', 'fields' ): continue # (processed separately)
1869 if ctrl_kwargs.has_key(key):
1870 setattr(self, '_' + key, ctrl_kwargs[key])
1871
1872 # Validate color parameters, converting strings to named colors and validating
1873 # result if appropriate:
1874 for key in ('emptyBackgroundColour', 'invalidBackgroundColour', 'validBackgroundColour',
1875 'foregroundColour', 'signedForegroundColour'):
1876 if ctrl_kwargs.has_key(key):
1877 if type(ctrl_kwargs[key]) in (types.StringType, types.UnicodeType):
1878 c = wx.NamedColour(ctrl_kwargs[key])
1879 if c.Get() == (-1, -1, -1):
1880 raise TypeError('%s not a legal color specification for %s' % (repr(ctrl_kwargs[key]), key))
1881 else:
1882 # replace attribute with wxColour object:
1883 setattr(self, '_' + key, c)
1884 # attach a python dynamic attribute to wxColour for debug printouts
1885 c._name = ctrl_kwargs[key]
1886
1887 elif type(ctrl_kwargs[key]) != type(wx.BLACK):
1888 raise TypeError('%s not a legal color specification for %s' % (repr(ctrl_kwargs[key]), key))
1889
1890
1891 ## dbg('self._retainFieldValidation:', self._retainFieldValidation)
1892 if not self._retainFieldValidation:
1893 # Build dictionary of any changing parameters which should be propagated to the
1894 # component fields:
1895 for arg in Field.propagating_params:
1896 #### dbg('kwargs.has_key(%s)?' % arg, kwargs.has_key(arg))
1897 #### dbg('getattr(self._ctrl_constraints, _%s)?' % arg, getattr(self._ctrl_constraints, '_'+arg))
1898 reset_args[arg] = kwargs.has_key(arg) and kwargs[arg] != getattr(self._ctrl_constraints, '_'+arg)
1899 #### dbg('reset_args[%s]?' % arg, reset_args[arg])
1900
1901 # Set the control-level constraints:
1902 self._ctrl_constraints._SetParameters(**constraint_kwargs)
1903
1904 # This routine does the bulk of the interdependent parameter processing, determining
1905 # the field extents of the mask if changed, resetting parameters as appropriate,
1906 # determining the overall template value for the control, etc.
1907 self._configure(mask, **reset_args)
1908
1909 # now that we've propagated the field constraints and mask portions to the
1910 # various fields, validate the constraints
1911 self._ctrl_constraints._ValidateParameters(**constraint_kwargs)
1912
1913 # Validate that all choices for given fields are at least of the
1914 # necessary length, and that they all would be valid pastes if pasted
1915 # into their respective fields:
1916 #### dbg('validating choices')
1917 self._validateChoices()
1918
1919
1920 self._autofit = self._ctrl_constraints._autofit
1921 self._isNeg = False
1922
1923 self._isDate = 'D' in self._ctrl_constraints._formatcodes and _isDateType(mask)
1924 self._isTime = 'T' in self._ctrl_constraints._formatcodes and _isTimeType(mask)
1925 if self._isDate:
1926 # Set _dateExtent, used in date validation to locate date in string;
1927 # always set as though year will be 4 digits, even if mask only has
1928 # 2 digits, so we can always properly process the intended year for
1929 # date validation (leap years, etc.)
1930 if self._mask.find('CCC') != -1: self._dateExtent = 11
1931 else: self._dateExtent = 10
1932
1933 self._4digityear = len(self._mask) > 8 and self._mask[9] == '#'
1934
1935 if self._isDate and self._autoformat:
1936 # Auto-decide datestyle:
1937 if self._autoformat.find('MDDY') != -1: self._datestyle = 'MDY'
1938 elif self._autoformat.find('YMMD') != -1: self._datestyle = 'YMD'
1939 elif self._autoformat.find('YMMMD') != -1: self._datestyle = 'YMD'
1940 elif self._autoformat.find('DMMY') != -1: self._datestyle = 'DMY'
1941 elif self._autoformat.find('DMMMY') != -1: self._datestyle = 'DMY'
1942
1943 # Give derived controls a chance to react to parameter changes before
1944 # potentially changing current value of the control.
1945 self._OnCtrlParametersChanged()
1946
1947 if self.controlInitialized:
1948 # Then the base control is available for configuration;
1949 # take action on base control based on new settings, as appropriate.
1950 if kwargs.has_key('useFixedWidthFont'):
1951 # Set control font - fixed width by default
1952 self._setFont()
1953
1954 if reset_args.has_key('reset_mask'):
1955 ## dbg('reset mask')
1956 curvalue = self._GetValue()
1957 if curvalue.strip():
1958 try:
1959 ## dbg('attempting to _SetInitialValue(%s)' % self._GetValue())
1960 self._SetInitialValue(self._GetValue())
1961 except Exception, e:
1962 ## dbg('exception caught:', e)
1963 ## dbg("current value doesn't work; attempting to reset to template")
1964 self._SetInitialValue()
1965 else:
1966 ## dbg('attempting to _SetInitialValue() with template')
1967 self._SetInitialValue()
1968
1969 elif kwargs.has_key('useParensForNegatives'):
1970 newvalue = self._getSignedValue()[0]
1971
1972 if newvalue is not None:
1973 # Adjust for new mask:
1974 if len(newvalue) < len(self._mask):
1975 newvalue += ' '
1976 elif len(newvalue) > len(self._mask):
1977 if newvalue[-1] in (' ', ')'):
1978 newvalue = newvalue[:-1]
1979
1980 ## dbg('reconfiguring value for parens:"%s"' % newvalue)
1981 self._SetValue(newvalue)
1982
1983 if self._prevValue != newvalue:
1984 self._prevValue = newvalue # disallow undo of sign type
1985
1986 if self._autofit:
1987 ## dbg('calculated size:', self._CalcSize())
1988 self.SetClientSize(self._CalcSize())
1989 width = self.GetSize().width
1990 height = self.GetBestSize().height
1991 ## dbg('setting client size to:', (width, height))
1992 self.SetBestFittingSize((width, height))
1993
1994 # Set value/type-specific formatting
1995 self._applyFormatting()
1996 ## dbg(indent=0, suspend=0)
1997
1998 def SetMaskParameters(self, **kwargs):
1999 """ old name for the SetCtrlParameters function (DEPRECATED)"""
2000 return self.SetCtrlParameters(**kwargs)
2001
2002
2003 def GetCtrlParameter(self, paramname):
2004 """
2005 Routine for retrieving the value of any given parameter
2006 """
2007 if MaskedEditMixin.valid_ctrl_params.has_key(paramname.replace('Color','Colour')):
2008 return getattr(self, '_' + paramname.replace('Color', 'Colour'))
2009 elif Field.valid_params.has_key(paramname):
2010 return self._ctrl_constraints._GetParameter(paramname)
2011 else:
2012 TypeError('"%s".GetCtrlParameter: invalid parameter "%s"' % (self.name, paramname))
2013
2014 def GetMaskParameter(self, paramname):
2015 """ old name for the GetCtrlParameters function (DEPRECATED)"""
2016 return self.GetCtrlParameter(paramname)
2017
2018
2019 ## This idea worked, but Boa was unable to use this solution...
2020 ## def _attachMethod(self, func):
2021 ## import new
2022 ## setattr(self, func.__name__, new.instancemethod(func, self, self.__class__))
2023 ##
2024 ##
2025 ## def _DefinePropertyFunctions(exposed_params):
2026 ## for param in exposed_params:
2027 ## propname = param[0].upper() + param[1:]
2028 ##
2029 ## exec('def Set%s(self, value): self.SetCtrlParameters(%s=value)' % (propname, param))
2030 ## exec('def Get%s(self): return self.GetCtrlParameter("%s")''' % (propname, param))
2031 ## self._attachMethod(locals()['Set%s' % propname])
2032 ## self._attachMethod(locals()['Get%s' % propname])
2033 ##
2034 ## if param.find('Colour') != -1:
2035 ## # add non-british spellings, for backward-compatibility
2036 ## propname.replace('Colour', 'Color')
2037 ##
2038 ## exec('def Set%s(self, value): self.SetCtrlParameters(%s=value)' % (propname, param))
2039 ## exec('def Get%s(self): return self.GetCtrlParameter("%s")''' % (propname, param))
2040 ## self._attachMethod(locals()['Set%s' % propname])
2041 ## self._attachMethod(locals()['Get%s' % propname])
2042 ##
2043
2044
2045 def SetFieldParameters(self, field_index, **kwargs):
2046 """
2047 Routine provided to modify the parameters of a given field.
2048 Because changes to fields can affect the overall control,
2049 direct access to the fields is prevented, and the control
2050 is always "reconfigured" after setting a field parameter.
2051 (See maskededit module overview for the list of valid field-level
2052 parameters.)
2053 """
2054 if field_index not in self._field_indices:
2055 raise IndexError('%s is not a valid field for control "%s".' % (str(field_index), self.name))
2056 # set parameters as requested:
2057 self._fields[field_index]._SetParameters(**kwargs)
2058
2059 # Possibly reprogram control template due to resulting changes, and ensure
2060 # control-level params are still propagated to fields:
2061 self._configure(self._previous_mask)
2062 self._fields[field_index]._ValidateParameters(**kwargs)
2063
2064 if self.controlInitialized:
2065 if kwargs.has_key('fillChar') or kwargs.has_key('defaultValue'):
2066 self._SetInitialValue()
2067
2068 if self._autofit:
2069 # this is tricky, because, as Robin explains:
2070 # "Basically there are two sizes to deal with, that are potentially
2071 # different. The client size is the inside size and may, depending
2072 # on platform, exclude the borders and such. The normal size is
2073 # the outside size that does include the borders. What you are
2074 # calculating (in _CalcSize) is the client size, but the sizers
2075 # deal with the full size and so that is the minimum size that
2076 # we need to set with SetBestFittingSize. The root of the problem is
2077 # that in _calcSize the current client size height is returned,
2078 # instead of a height based on the current font. So I suggest using
2079 # _calcSize to just get the width, and then use GetBestSize to
2080 # get the height."
2081 self.SetClientSize(self._CalcSize())
2082 width = self.GetSize().width
2083 height = self.GetBestSize().height
2084 self.SetBestFittingSize((width, height))
2085
2086
2087 # Set value/type-specific formatting
2088 self._applyFormatting()
2089
2090
2091 def GetFieldParameter(self, field_index, paramname):
2092 """
2093 Routine provided for getting a parameter of an individual field.
2094 """
2095 if field_index not in self._field_indices:
2096 raise IndexError('%s is not a valid field for control "%s".' % (str(field_index), self.name))
2097 elif Field.valid_params.has_key(paramname):
2098 return self._fields[field_index]._GetParameter(paramname)
2099 else:
2100 TypeError('"%s".GetFieldParameter: invalid parameter "%s"' % (self.name, paramname))
2101
2102
2103 def _SetKeycodeHandler(self, keycode, func):
2104 """
2105 This function adds and/or replaces key event handling functions
2106 used by the control. <func> should take the event as argument
2107 and return False if no further action on the key is necessary.
2108 """
2109 self._keyhandlers[keycode] = func
2110
2111
2112 def _SetKeyHandler(self, char, func):
2113 """
2114 This function adds and/or replaces key event handling functions
2115 for ascii characters. <func> should take the event as argument
2116 and return False if no further action on the key is necessary.
2117 """
2118 self._SetKeycodeHandler(ord(char), func)
2119
2120
2121 def _AddNavKeycode(self, keycode, handler=None):
2122 """
2123 This function allows a derived subclass to augment the list of
2124 keycodes that are considered "navigational" keys.
2125 """
2126 self._nav.append(keycode)
2127 if handler:
2128 self._keyhandlers[keycode] = handler
2129
2130
2131 def _AddNavKey(self, char, handler=None):
2132 """
2133 This function is a convenience function so you don't have to
2134 remember to call ord() for ascii chars to be used for navigation.
2135 """
2136 self._AddNavKeycode(ord(char), handler)
2137
2138
2139 def _GetNavKeycodes(self):
2140 """
2141 This function retrieves the current list of navigational keycodes for
2142 the control.
2143 """
2144 return self._nav
2145
2146
2147 def _SetNavKeycodes(self, keycode_func_tuples):
2148 """
2149 This function allows you to replace the current list of keycode processed
2150 as navigation keys, and bind associated optional keyhandlers.
2151 """
2152 self._nav = []
2153 for keycode, func in keycode_func_tuples:
2154 self._nav.append(keycode)
2155 if func:
2156 self._keyhandlers[keycode] = func
2157
2158
2159 def _processMask(self, mask):
2160 """
2161 This subroutine expands {n} syntax in mask strings, and looks for escaped
2162 special characters and returns the expanded mask, and an dictionary
2163 of booleans indicating whether or not a given position in the mask is
2164 a mask character or not.
2165 """
2166 ## dbg('_processMask: mask', mask, indent=1)
2167 # regular expression for parsing c{n} syntax:
2168 rex = re.compile('([' +string.join(maskchars,"") + '])\{(\d+)\}')
2169 s = mask
2170 match = rex.search(s)
2171 while match: # found an(other) occurrence
2172 maskchr = s[match.start(1):match.end(1)] # char to be repeated
2173 repcount = int(s[match.start(2):match.end(2)]) # the number of times
2174 replacement = string.join( maskchr * repcount, "") # the resulting substr
2175 s = s[:match.start(1)] + replacement + s[match.end(2)+1:] #account for trailing '}'
2176 match = rex.search(s) # look for another such entry in mask
2177
2178 self._decimalChar = self._ctrl_constraints._decimalChar
2179 self._shiftDecimalChar = self._ctrl_constraints._shiftDecimalChar
2180
2181 self._isFloat = _isFloatingPoint(s) and not self._ctrl_constraints._validRegex
2182 self._isInt = _isInteger(s) and not self._ctrl_constraints._validRegex
2183 self._signOk = '-' in self._ctrl_constraints._formatcodes and (self._isFloat or self._isInt)
2184 self._useParens = self._ctrl_constraints._useParensForNegatives
2185 self._isNeg = False
2186 #### dbg('self._signOk?', self._signOk, 'self._useParens?', self._useParens)
2187 #### dbg('isFloatingPoint(%s)?' % (s), _isFloatingPoint(s),
2188 ## 'ctrl regex:', self._ctrl_constraints._validRegex)
2189
2190 if self._signOk and s[0] != ' ':
2191 s = ' ' + s
2192 if self._ctrl_constraints._defaultValue and self._ctrl_constraints._defaultValue[0] != ' ':
2193 self._ctrl_constraints._defaultValue = ' ' + self._ctrl_constraints._defaultValue
2194 self._signpos = 0
2195
2196 if self._useParens:
2197 s += ' '
2198 self._ctrl_constraints._defaultValue += ' '
2199
2200 # Now, go build up a dictionary of booleans, indexed by position,
2201 # indicating whether or not a given position is masked or not
2202 ismasked = {}
2203 i = 0
2204 while i < len(s):
2205 if s[i] == '\\': # if escaped character:
2206 ismasked[i] = False # mark position as not a mask char
2207 if i+1 < len(s): # if another char follows...
2208 s = s[:i] + s[i+1:] # elide the '\'
2209 if i+2 < len(s) and s[i+1] == '\\':
2210 # if next char also a '\', char is a literal '\'
2211 s = s[:i] + s[i+1:] # elide the 2nd '\' as well
2212 else: # else if special char, mark position accordingly
2213 ismasked[i] = s[i] in maskchars
2214 #### dbg('ismasked[%d]:' % i, ismasked[i], s)
2215 i += 1 # increment to next char
2216 #### dbg('ismasked:', ismasked)
2217 ## dbg('new mask: "%s"' % s, indent=0)
2218
2219 return s, ismasked
2220
2221
2222 def _calcFieldExtents(self):
2223 """
2224 Subroutine responsible for establishing/configuring field instances with
2225 indices and editable extents appropriate to the specified mask, and building
2226 the lookup table mapping each position to the corresponding field.
2227 """
2228 self._lookupField = {}
2229 if self._mask:
2230
2231 ## Create dictionary of positions,characters in mask
2232 self.maskdict = {}
2233 for charnum in range( len( self._mask)):
2234 self.maskdict[charnum] = self._mask[charnum:charnum+1]
2235
2236 # For the current mask, create an ordered list of field extents
2237 # and a dictionary of positions that map to field indices:
2238
2239 if self._signOk: start = 1
2240 else: start = 0
2241
2242 if self._isFloat:
2243 # Skip field "discovery", and just construct a 2-field control with appropriate
2244 # constraints for a floating-point entry.
2245
2246 # .setdefault always constructs 2nd argument even if not needed, so we do this
2247 # the old-fashioned way...
2248 if not self._fields.has_key(0):
2249 self._fields[0] = Field()
2250 if not self._fields.has_key(1):
2251 self._fields[1] = Field()
2252
2253 self._decimalpos = string.find( self._mask, '.')
2254 ## dbg('decimal pos =', self._decimalpos)
2255
2256 formatcodes = self._fields[0]._GetParameter('formatcodes')
2257 if 'R' not in formatcodes: formatcodes += 'R'
2258 self._fields[0]._SetParameters(index=0, extent=(start, self._decimalpos),
2259 mask=self._mask[start:self._decimalpos], formatcodes=formatcodes)
2260 end = len(self._mask)
2261 if self._signOk and self._useParens:
2262 end -= 1
2263 self._fields[1]._SetParameters(index=1, extent=(self._decimalpos+1, end),
2264 mask=self._mask[self._decimalpos+1:end])
2265
2266 for i in range(self._decimalpos+1):
2267 self._lookupField[i] = 0
2268
2269 for i in range(self._decimalpos+1, len(self._mask)+1):
2270 self._lookupField[i] = 1
2271
2272 elif self._isInt:
2273 # Skip field "discovery", and just construct a 1-field control with appropriate
2274 # constraints for a integer entry.
2275 if not self._fields.has_key(0):
2276 self._fields[0] = Field(index=0)
2277 end = len(self._mask)
2278 if self._signOk and self._useParens:
2279 end -= 1
2280 self._fields[0]._SetParameters(index=0, extent=(start, end),
2281 mask=self._mask[start:end])
2282 for i in range(len(self._mask)+1):
2283 self._lookupField[i] = 0
2284 else:
2285 # generic control; parse mask to figure out where the fields are:
2286 field_index = 0
2287 pos = 0
2288 i = self._findNextEntry(pos,adjustInsert=False) # go to 1st entry point:
2289 if i < len(self._mask): # no editable chars!
2290 for j in range(pos, i+1):
2291 self._lookupField[j] = field_index
2292 pos = i # figure out field for 1st editable space:
2293
2294 while i <= len(self._mask):
2295 #### dbg('searching: outer field loop: i = ', i)
2296 if self._isMaskChar(i):
2297 #### dbg('1st char is mask char; recording edit_start=', i)
2298 edit_start = i
2299 # Skip to end of editable part of current field:
2300 while i < len(self._mask) and self._isMaskChar(i):
2301 self._lookupField[i] = field_index
2302 i += 1
2303 #### dbg('edit_end =', i)
2304 edit_end = i
2305 self._lookupField[i] = field_index
2306 #### dbg('self._fields.has_key(%d)?' % field_index, self._fields.has_key(field_index))
2307 if not self._fields.has_key(field_index):
2308 kwargs = Field.valid_params.copy()
2309 kwargs['index'] = field_index
2310 kwargs['extent'] = (edit_start, edit_end)
2311 kwargs['mask'] = self._mask[edit_start:edit_end]
2312 self._fields[field_index] = Field(**kwargs)
2313 else:
2314 self._fields[field_index]._SetParameters(
2315 index=field_index,
2316 extent=(edit_start, edit_end),
2317 mask=self._mask[edit_start:edit_end])
2318 pos = i
2319 i = self._findNextEntry(pos, adjustInsert=False) # go to next field:
2320 if i > pos:
2321 for j in range(pos, i+1):
2322 self._lookupField[j] = field_index
2323 if i >= len(self._mask):
2324 break # if past end, we're done
2325 else:
2326 field_index += 1
2327 #### dbg('next field:', field_index)
2328
2329 indices = self._fields.keys()
2330 indices.sort()
2331 self._field_indices = indices[1:]
2332 #### dbg('lookupField map:', indent=1)
2333 ## for i in range(len(self._mask)):
2334 #### dbg('pos %d:' % i, self._lookupField[i])
2335 #### dbg(indent=0)
2336
2337 # Verify that all field indices specified are valid for mask:
2338 for index in self._fields.keys():
2339 if index not in [-1] + self._lookupField.values():
2340 raise IndexError('field %d is not a valid field for mask "%s"' % (index, self._mask))
2341
2342
2343 def _calcTemplate(self, reset_fillchar, reset_default):
2344 """
2345 Subroutine for processing current fillchars and default values for
2346 whole control and individual fields, constructing the resulting
2347 overall template, and adjusting the current value as necessary.
2348 """
2349 default_set = False
2350 if self._ctrl_constraints._defaultValue:
2351 default_set = True
2352 else:
2353 for field in self._fields.values():
2354 if field._defaultValue and not reset_default:
2355 default_set = True
2356 ## dbg('default set?', default_set)
2357
2358 # Determine overall new template for control, and keep track of previous
2359 # values, so that current control value can be modified as appropriate:
2360 if self.controlInitialized: curvalue = list(self._GetValue())
2361 else: curvalue = None
2362
2363 if hasattr(self, '_fillChar'): old_fillchars = self._fillChar
2364 else: old_fillchars = None
2365
2366 if hasattr(self, '_template'): old_template = self._template
2367 else: old_template = None
2368
2369 self._template = ""
2370
2371 self._fillChar = {}
2372 reset_value = False
2373
2374 for field in self._fields.values():
2375 field._template = ""
2376
2377 for pos in range(len(self._mask)):
2378 #### dbg('pos:', pos)
2379 field = self._FindField(pos)
2380 #### dbg('field:', field._index)
2381 start, end = field._extent
2382
2383 if pos == 0 and self._signOk:
2384 self._template = ' ' # always make 1st 1st position blank, regardless of fillchar
2385 elif self._isFloat and pos == self._decimalpos:
2386 self._template += self._decimalChar
2387 elif self._isMaskChar(pos):
2388 if field._fillChar != self._ctrl_constraints._fillChar and not reset_fillchar:
2389 fillChar = field._fillChar
2390 else:
2391 fillChar = self._ctrl_constraints._fillChar
2392 self._fillChar[pos] = fillChar
2393
2394 # Replace any current old fillchar with new one in current value;
2395 # if action required, set reset_value flag so we can take that action
2396 # after we're all done
2397 if self.controlInitialized and old_fillchars and old_fillchars.has_key(pos) and curvalue:
2398 if curvalue[pos] == old_fillchars[pos] and old_fillchars[pos] != fillChar:
2399 reset_value = True
2400 curvalue[pos] = fillChar
2401
2402 if not field._defaultValue and not self._ctrl_constraints._defaultValue:
2403 #### dbg('no default value')
2404 self._template += fillChar
2405 field._template += fillChar
2406
2407 elif field._defaultValue and not reset_default:
2408 #### dbg('len(field._defaultValue):', len(field._defaultValue))
2409 #### dbg('pos-start:', pos-start)
2410 if len(field._defaultValue) > pos-start:
2411 #### dbg('field._defaultValue[pos-start]: "%s"' % field._defaultValue[pos-start])
2412 self._template += field._defaultValue[pos-start]
2413 field._template += field._defaultValue[pos-start]
2414 else:
2415 #### dbg('field default not long enough; using fillChar')
2416 self._template += fillChar
2417 field._template += fillChar
2418 else:
2419 if len(self._ctrl_constraints._defaultValue) > pos:
2420 #### dbg('using control default')
2421 self._template += self._ctrl_constraints._defaultValue[pos]
2422 field._template += self._ctrl_constraints._defaultValue[pos]
2423 else:
2424 #### dbg('ctrl default not long enough; using fillChar')
2425 self._template += fillChar
2426 field._template += fillChar
2427 #### dbg('field[%d]._template now "%s"' % (field._index, field._template))
2428 #### dbg('self._template now "%s"' % self._template)
2429 else:
2430 self._template += self._mask[pos]
2431
2432 self._fields[-1]._template = self._template # (for consistency)
2433
2434 if curvalue: # had an old value, put new one back together
2435 newvalue = string.join(curvalue, "")
2436 else:
2437 newvalue = None
2438
2439 if default_set:
2440 self._defaultValue = self._template
2441 ## dbg('self._defaultValue:', self._defaultValue)
2442 if not self.IsEmpty(self._defaultValue) and not self.IsValid(self._defaultValue):
2443 #### dbg(indent=0)
2444 raise ValueError('Default value of "%s" is not a valid value for control "%s"' % (self._defaultValue, self.name))
2445
2446 # if no fillchar change, but old value == old template, replace it:
2447 if newvalue == old_template:
2448 newvalue = self._template
2449 reset_value = True
2450 else:
2451 self._defaultValue = None
2452
2453 if reset_value:
2454 ## dbg('resetting value to: "%s"' % newvalue)
2455 pos = self._GetInsertionPoint()
2456 sel_start, sel_to = self._GetSelection()
2457 self._SetValue(newvalue)
2458 self._SetInsertionPoint(pos)
2459 self._SetSelection(sel_start, sel_to)
2460
2461
2462 def _propagateConstraints(self, **reset_args):
2463 """
2464 Subroutine for propagating changes to control-level constraints and
2465 formatting to the individual fields as appropriate.
2466 """
2467 parent_codes = self._ctrl_constraints._formatcodes
2468 parent_includes = self._ctrl_constraints._includeChars
2469 parent_excludes = self._ctrl_constraints._excludeChars
2470 for i in self._field_indices:
2471 field = self._fields[i]
2472 inherit_args = {}
2473 if len(self._field_indices) == 1:
2474 inherit_args['formatcodes'] = parent_codes
2475 inherit_args['includeChars'] = parent_includes
2476 inherit_args['excludeChars'] = parent_excludes
2477 else:
2478 field_codes = current_codes = field._GetParameter('formatcodes')
2479 for c in parent_codes:
2480 if c not in field_codes: field_codes += c
2481 if field_codes != current_codes:
2482 inherit_args['formatcodes'] = field_codes
2483
2484 include_chars = current_includes = field._GetParameter('includeChars')
2485 for c in parent_includes:
2486 if not c in include_chars: include_chars += c
2487 if include_chars != current_includes:
2488 inherit_args['includeChars'] = include_chars
2489
2490 exclude_chars = current_excludes = field._GetParameter('excludeChars')
2491 for c in parent_excludes:
2492 if not c in exclude_chars: exclude_chars += c
2493 if exclude_chars != current_excludes:
2494 inherit_args['excludeChars'] = exclude_chars
2495
2496 if reset_args.has_key('defaultValue') and reset_args['defaultValue']:
2497 inherit_args['defaultValue'] = "" # (reset for field)
2498
2499 for param in Field.propagating_params:
2500 #### dbg('reset_args.has_key(%s)?' % param, reset_args.has_key(param))
2501 #### dbg('reset_args.has_key(%(param)s) and reset_args[%(param)s]?' % locals(), reset_args.has_key(param) and reset_args[param])
2502 if reset_args.has_key(param):
2503 inherit_args[param] = self.GetCtrlParameter(param)
2504 #### dbg('inherit_args[%s]' % param, inherit_args[param])
2505
2506 if inherit_args:
2507 field._SetParameters(**inherit_args)
2508 field._ValidateParameters(**inherit_args)
2509
2510
2511 def _validateChoices(self):
2512 """
2513 Subroutine that validates that all choices for given fields are at
2514 least of the necessary length, and that they all would be valid pastes
2515 if pasted into their respective fields.
2516 """
2517 for field in self._fields.values():
2518 if field._choices:
2519 index = field._index
2520 if len(self._field_indices) == 1 and index == 0 and field._choices == self._ctrl_constraints._choices:
2521 ## dbg('skipping (duplicate) choice validation of field 0')
2522 continue
2523 #### dbg('checking for choices for field', field._index)
2524 start, end = field._extent
2525 field_length = end - start
2526 #### dbg('start, end, length:', start, end, field_length)
2527 for choice in field._choices:
2528 #### dbg('testing "%s"' % choice)
2529 valid_paste, ignore, replace_to = self._validatePaste(choice, start, end)
2530 if not valid_paste:
2531 #### dbg(indent=0)
2532 raise ValueError('"%s" could not be entered into field %d of control "%s"' % (choice, index, self.name))
2533 elif replace_to > end:
2534 #### dbg(indent=0)
2535 raise ValueError('"%s" will not fit into field %d of control "%s"' (choice, index, self.name))
2536 #### dbg(choice, 'valid in field', index)
2537
2538
2539 def _configure(self, mask, **reset_args):
2540 """
2541 This function sets flags for automatic styling options. It is
2542 called whenever a control or field-level parameter is set/changed.
2543
2544 This routine does the bulk of the interdependent parameter processing, determining
2545 the field extents of the mask if changed, resetting parameters as appropriate,
2546 determining the overall template value for the control, etc.
2547
2548 reset_args is supplied if called from control's .SetCtrlParameters()
2549 routine, and indicates which if any parameters which can be
2550 overridden by individual fields have been reset by request for the
2551 whole control.
2552
2553 """
2554 ## dbg(suspend=1)
2555 ## dbg('MaskedEditMixin::_configure("%s")' % mask, indent=1)
2556
2557 # Preprocess specified mask to expand {n} syntax, handle escaped
2558 # mask characters, etc and build the resulting positionally keyed
2559 # dictionary for which positions are mask vs. template characters:
2560 self._mask, self.ismasked = self._processMask(mask)
2561 self._masklength = len(self._mask)
2562 #### dbg('processed mask:', self._mask)
2563
2564 # Preserve original mask specified, for subsequent reprocessing
2565 # if parameters change.
2566 ## dbg('mask: "%s"' % self._mask, 'previous mask: "%s"' % self._previous_mask)
2567 self._previous_mask = mask # save unexpanded mask for next time
2568 # Set expanded mask and extent of field -1 to width of entire control:
2569 self._ctrl_constraints._SetParameters(mask = self._mask, extent=(0,self._masklength))
2570
2571 # Go parse mask to determine where each field is, construct field
2572 # instances as necessary, configure them with those extents, and
2573 # build lookup table mapping each position for control to its corresponding
2574 # field.
2575 #### dbg('calculating field extents')
2576
2577 self._calcFieldExtents()
2578
2579
2580 # Go process defaultValues and fillchars to construct the overall
2581 # template, and adjust the current value as necessary:
2582 reset_fillchar = reset_args.has_key('fillChar') and reset_args['fillChar']
2583 reset_default = reset_args.has_key('defaultValue') and reset_args['defaultValue']
2584
2585 #### dbg('calculating template')
2586 self._calcTemplate(reset_fillchar, reset_default)
2587
2588 # Propagate control-level formatting and character constraints to each
2589 # field if they don't already have them; if only one field, propagate
2590 # control-level validation constraints to field as well:
2591 #### dbg('propagating constraints')
2592 self._propagateConstraints(**reset_args)
2593
2594
2595 if self._isFloat and self._fields[0]._groupChar == self._decimalChar:
2596 raise AttributeError('groupChar (%s) and decimalChar (%s) must be distinct.' %
2597 (self._fields[0]._groupChar, self._decimalChar) )
2598
2599 #### dbg('fields:', indent=1)
2600 ## for i in [-1] + self._field_indices:
2601 #### dbg('field %d:' % i, self._fields[i].__dict__)
2602 #### dbg(indent=0)
2603
2604 # Set up special parameters for numeric control, if appropriate:
2605 if self._signOk:
2606 self._signpos = 0 # assume it starts here, but it will move around on floats
2607 signkeys = ['-', '+', ' ']
2608 if self._useParens:
2609 signkeys += ['(', ')']
2610 for key in signkeys:
2611 keycode = ord(key)
2612 if not self._keyhandlers.has_key(keycode):
2613 self._SetKeyHandler(key, self._OnChangeSign)
2614
2615
2616
2617 if self._isFloat or self._isInt:
2618 if self.controlInitialized:
2619 value = self._GetValue()
2620 #### dbg('value: "%s"' % value, 'len(value):', len(value),
2621 ## 'len(self._ctrl_constraints._mask):',len(self._ctrl_constraints._mask))
2622 if len(value) < len(self._ctrl_constraints._mask):
2623 newvalue = value
2624 if self._useParens and len(newvalue) < len(self._ctrl_constraints._mask) and newvalue.find('(') == -1:
2625 newvalue += ' '
2626 if self._signOk and len(newvalue) < len(self._ctrl_constraints._mask) and newvalue.find(')') == -1:
2627 newvalue = ' ' + newvalue
2628 if len(newvalue) < len(self._ctrl_constraints._mask):
2629 if self._ctrl_constraints._alignRight:
2630 newvalue = newvalue.rjust(len(self._ctrl_constraints._mask))
2631 else:
2632 newvalue = newvalue.ljust(len(self._ctrl_constraints._mask))
2633 ## dbg('old value: "%s"' % value)
2634 ## dbg('new value: "%s"' % newvalue)
2635 try:
2636 self._SetValue(newvalue)
2637 except Exception, e:
2638 ## dbg('exception raised:', e, 'resetting to initial value')
2639 self._SetInitialValue()
2640
2641 elif len(value) > len(self._ctrl_constraints._mask):
2642 newvalue = value
2643 if not self._useParens and newvalue[-1] == ' ':
2644 newvalue = newvalue[:-1]
2645 if not self._signOk and len(newvalue) > len(self._ctrl_constraints._mask):
2646 newvalue = newvalue[1:]
2647 if not self._signOk:
2648 newvalue, signpos, right_signpos = self._getSignedValue(newvalue)
2649
2650 ## dbg('old value: "%s"' % value)
2651 ## dbg('new value: "%s"' % newvalue)
2652 try:
2653 self._SetValue(newvalue)
2654 except Exception, e:
2655 ## dbg('exception raised:', e, 'resetting to initial value')
2656 self._SetInitialValue()
2657 elif not self._signOk and ('(' in value or '-' in value):
2658 newvalue, signpos, right_signpos = self._getSignedValue(value)
2659 ## dbg('old value: "%s"' % value)
2660 ## dbg('new value: "%s"' % newvalue)
2661 try:
2662 self._SetValue(newvalue)
2663 except e:
2664 ## dbg('exception raised:', e, 'resetting to initial value')
2665 self._SetInitialValue()
2666
2667 # Replace up/down arrow default handling:
2668 # make down act like tab, up act like shift-tab:
2669
2670 #### dbg('Registering numeric navigation and control handlers (if not already set)')
2671 if not self._keyhandlers.has_key(wx.WXK_DOWN):
2672 self._SetKeycodeHandler(wx.WXK_DOWN, self._OnChangeField)
2673 if not self._keyhandlers.has_key(wx.WXK_UP):
2674 self._SetKeycodeHandler(wx.WXK_UP, self._OnUpNumeric) # (adds "shift" to up arrow, and calls _OnChangeField)
2675
2676 # On ., truncate contents right of cursor to decimal point (if any)
2677 # leaves cusor after decimal point if floating point, otherwise at 0.
2678 if not self._keyhandlers.has_key(ord(self._decimalChar)):
2679 self._SetKeyHandler(self._decimalChar, self._OnDecimalPoint)
2680 if not self._keyhandlers.has_key(ord(self._shiftDecimalChar)):
2681 self._SetKeyHandler(self._shiftDecimalChar, self._OnChangeField) # (Shift-'.' == '>' on US keyboards)
2682
2683 # Allow selective insert of groupchar in numbers:
2684 if not self._keyhandlers.has_key(ord(self._fields[0]._groupChar)):
2685 self._SetKeyHandler(self._fields[0]._groupChar, self._OnGroupChar)
2686
2687 ## dbg(indent=0, suspend=0)
2688
2689
2690 def _SetInitialValue(self, value=""):
2691 """
2692 fills the control with the generated or supplied default value.
2693 It will also set/reset the font if necessary and apply
2694 formatting to the control at this time.
2695 """
2696 ## dbg('MaskedEditMixin::_SetInitialValue("%s")' % value, indent=1)
2697 if not value:
2698 self._prevValue = self._curValue = self._template
2699 # don't apply external validation rules in this case, as template may
2700 # not coincide with "legal" value...
2701 try:
2702 self._SetValue(self._curValue) # note the use of "raw" ._SetValue()...
2703 except Exception, e:
2704 ## dbg('exception thrown:', e, indent=0)
2705 raise
2706 else:
2707 # Otherwise apply validation as appropriate to passed value:
2708 #### dbg('value = "%s", length:' % value, len(value))
2709 self._prevValue = self._curValue = value
2710 try:
2711 self.SetValue(value) # use public (validating) .SetValue()
2712 except Exception, e:
2713 ## dbg('exception thrown:', e, indent=0)
2714 raise
2715
2716
2717 # Set value/type-specific formatting
2718 self._applyFormatting()
2719 ## dbg(indent=0)
2720
2721
2722 def _calcSize(self, size=None):
2723 """ Calculate automatic size if allowed; must be called after the base control is instantiated"""
2724 #### dbg('MaskedEditMixin::_calcSize', indent=1)
2725 cont = (size is None or size == wx.DefaultSize)
2726
2727 if cont and self._autofit:
2728 sizing_text = 'M' * self._masklength
2729 if wx.Platform != "__WXMSW__": # give it a little extra space
2730 sizing_text += 'M'
2731 if wx.Platform == "__WXMAC__": # give it even a little more...
2732 sizing_text += 'M'
2733 #### dbg('len(sizing_text):', len(sizing_text), 'sizing_text: "%s"' % sizing_text)
2734 w, h = self.GetTextExtent(sizing_text)
2735 size = (w+4, self.GetSize().height)
2736 #### dbg('size:', size, indent=0)
2737 return size
2738
2739
2740 def _setFont(self):
2741 """ Set the control's font typeface -- pass the font name as str."""
2742 #### dbg('MaskedEditMixin::_setFont', indent=1)
2743 if not self._useFixedWidthFont:
2744 self._font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
2745 else:
2746 font = self.GetFont() # get size, weight, etc from current font
2747
2748 # Set to teletype font (guaranteed to be mappable to all wxWindows
2749 # platforms:
2750 self._font = wx.Font( font.GetPointSize(), wx.TELETYPE, font.GetStyle(),
2751 font.GetWeight(), font.GetUnderlined())
2752 #### dbg('font string: "%s"' % font.GetNativeFontInfo().ToString())
2753
2754 self.SetFont(self._font)
2755 #### dbg(indent=0)
2756
2757
2758 def _OnTextChange(self, event):
2759 """
2760 Handler for EVT_TEXT event.
2761 self._Change() is provided for subclasses, and may return False to
2762 skip this method logic. This function returns True if the event
2763 detected was a legitimate event, or False if it was a "bogus"
2764 EVT_TEXT event. (NOTE: There is currently an issue with calling
2765 .SetValue from within the EVT_CHAR handler that causes duplicate
2766 EVT_TEXT events for the same change.)
2767 """
2768 newvalue = self._GetValue()
2769 ## dbg('MaskedEditMixin::_OnTextChange: value: "%s"' % newvalue, indent=1)
2770 bValid = False
2771 if self._ignoreChange: # ie. if an "intermediate text change event"
2772 ## dbg(indent=0)
2773 return bValid
2774
2775 ##! WS: For some inexplicable reason, every wx.TextCtrl.SetValue
2776 ## call is generating two (2) EVT_TEXT events. On certain platforms,
2777 ## (eg. linux/GTK) the 1st is an empty string value.
2778 ## This is the only mechanism I can find to mask this problem:
2779 if newvalue == self._curValue or len(newvalue) == 0:
2780 ## dbg('ignoring bogus text change event', indent=0)
2781 pass
2782 else:
2783 ## dbg('curvalue: "%s", newvalue: "%s", len(newvalue): %d' % (self._curValue, newvalue, len(newvalue)))
2784 if self._Change():
2785 if self._signOk and self._isNeg and newvalue.find('-') == -1 and newvalue.find('(') == -1:
2786 ## dbg('clearing self._isNeg')
2787 self._isNeg = False
2788 text, self._signpos, self._right_signpos = self._getSignedValue()
2789 self._CheckValid() # Recolor control as appropriate
2790 ## dbg('calling event.Skip()')
2791 event.Skip()
2792 bValid = True
2793 self._prevValue = self._curValue # save for undo
2794 self._curValue = newvalue # Save last seen value for next iteration
2795 ## dbg(indent=0)
2796 return bValid
2797
2798
2799 def _OnKeyDown(self, event):
2800 """
2801 This function allows the control to capture Ctrl-events like Ctrl-tab,
2802 that are not normally seen by the "cooked" EVT_CHAR routine.
2803 """
2804 # Get keypress value, adjusted by control options (e.g. convert to upper etc)
2805 key = event.GetKeyCode()
2806 if key in self._nav and event.ControlDown():
2807 # then this is the only place we will likely see these events;
2808 # process them now:
2809 ## dbg('MaskedEditMixin::OnKeyDown: calling _OnChar')
2810 self._OnChar(event)
2811 return
2812 # else allow regular EVT_CHAR key processing
2813 event.Skip()
2814
2815
2816 def _OnChar(self, event):
2817 """
2818 This is the engine of MaskedEdit controls. It examines each keystroke,
2819 decides if it's allowed, where it should go or what action to take.
2820 """
2821 ## dbg('MaskedEditMixin::_OnChar', indent=1)
2822
2823 # Get keypress value, adjusted by control options (e.g. convert to upper etc)
2824 key = event.GetKeyCode()
2825 orig_pos = self._GetInsertionPoint()
2826 orig_value = self._GetValue()
2827 ## dbg('keycode = ', key)
2828 ## dbg('current pos = ', orig_pos)
2829 ## dbg('current selection = ', self._GetSelection())
2830
2831 if not self._Keypress(key):
2832 ## dbg(indent=0)
2833 return
2834
2835 # If no format string for this control, or the control is marked as "read-only",
2836 # skip the rest of the special processing, and just "do the standard thing:"
2837 if not self._mask or not self._IsEditable():
2838 event.Skip()
2839 ## dbg(indent=0)
2840 return
2841
2842 # Process navigation and control keys first, with
2843 # position/selection unadulterated:
2844 if key in self._nav + self._control:
2845 if self._keyhandlers.has_key(key):
2846 keep_processing = self._keyhandlers[key](event)
2847 if self._GetValue() != orig_value:
2848 self.modified = True
2849 if not keep_processing:
2850 ## dbg(indent=0)
2851 return
2852 self._applyFormatting()
2853 ## dbg(indent=0)
2854 return
2855
2856 # Else... adjust the position as necessary for next input key,
2857 # and determine resulting selection:
2858 pos = self._adjustPos( orig_pos, key ) ## get insertion position, adjusted as needed
2859 sel_start, sel_to = self._GetSelection() ## check for a range of selected text
2860 ## dbg("pos, sel_start, sel_to:", pos, sel_start, sel_to)
2861
2862 keep_processing = True
2863 # Capture user past end of format field
2864 if pos > len(self.maskdict):
2865 ## dbg("field length exceeded:",pos)
2866 keep_processing = False
2867
2868 if keep_processing:
2869 if self._isMaskChar(pos): ## Get string of allowed characters for validation
2870 okchars = self._getAllowedChars(pos)
2871 else:
2872 ## dbg('Not a valid position: pos = ', pos,"chars=",maskchars)
2873 okchars = ""
2874
2875 key = self._adjustKey(pos, key) # apply formatting constraints to key:
2876
2877 if self._keyhandlers.has_key(key):
2878 # there's an override for default behavior; use override function instead
2879 ## dbg('using supplied key handler:', self._keyhandlers[key])
2880 keep_processing = self._keyhandlers[key](event)
2881 if self._GetValue() != orig_value:
2882 self.modified = True
2883 if not keep_processing:
2884 ## dbg(indent=0)
2885 return
2886 # else skip default processing, but do final formatting
2887 if key < wx.WXK_SPACE or key > 255:
2888 ## dbg('key < WXK_SPACE or key > 255')
2889 event.Skip() # non alphanumeric
2890 keep_processing = False
2891 else:
2892 field = self._FindField(pos)
2893
2894 ## dbg("key ='%s'" % chr(key))
2895 if chr(key) == ' ':
2896 ## dbg('okSpaces?', field._okSpaces)
2897 pass
2898
2899 char = chr(key) # (must work if we got this far)
2900
2901 if 'unicode' in wx.PlatformInfo:
2902 char = char.decode(self._defaultEncoding)
2903 excludes = u''
2904 if type(field._excludeChars) != types.UnicodeType:
2905 excludes += field._excludeChars.decode(self._defaultEncoding)
2906 if type(self._ctrl_constraints) != types.UnicodeType:
2907 excludes += self._ctrl_constraints._excludeChars.decode(self._defaultEncoding)
2908 else:
2909 excludes = field._excludeChars + self._ctrl_constraints._excludeChars
2910
2911 if char in excludes:
2912 keep_processing = False
2913
2914 if keep_processing and self._isCharAllowed( char, pos, checkRegex = True ):
2915 ## dbg("key allowed by mask")
2916 # insert key into candidate new value, but don't change control yet:
2917 oldstr = self._GetValue()
2918 newstr, newpos, new_select_to, match_field, match_index = self._insertKey(
2919 char, pos, sel_start, sel_to, self._GetValue(), allowAutoSelect = True)
2920 ## dbg("str with '%s' inserted:" % char, '"%s"' % newstr)
2921 if self._ctrl_constraints._validRequired and not self.IsValid(newstr):
2922 ## dbg('not valid; checking to see if adjusted string is:')
2923 keep_processing = False
2924 if self._isFloat and newstr != self._template:
2925 newstr = self._adjustFloat(newstr)
2926 ## dbg('adjusted str:', newstr)
2927 if self.IsValid(newstr):
2928 ## dbg("it is!")
2929 keep_processing = True
2930 wx.CallAfter(self._SetInsertionPoint, self._decimalpos)
2931 if not keep_processing:
2932 ## dbg("key disallowed by validation")
2933 if not wx.Validator_IsSilent() and orig_pos == pos:
2934 wx.Bell()
2935
2936 if keep_processing:
2937 unadjusted = newstr
2938
2939 # special case: adjust date value as necessary:
2940 if self._isDate and newstr != self._template:
2941 newstr = self._adjustDate(newstr)
2942 ## dbg('adjusted newstr:', newstr)
2943
2944 if newstr != orig_value:
2945 self.modified = True
2946
2947 wx.CallAfter(self._SetValue, newstr)
2948
2949 # Adjust insertion point on date if just entered 2 digit year, and there are now 4 digits:
2950 if not self.IsDefault() and self._isDate and self._4digityear:
2951 year2dig = self._dateExtent - 2
2952 if pos == year2dig and unadjusted[year2dig] != newstr[year2dig]:
2953 newpos = pos+2
2954
2955 wx.CallAfter(self._SetInsertionPoint, newpos)
2956
2957 if match_field is not None:
2958 ## dbg('matched field')
2959 self._OnAutoSelect(match_field, match_index)
2960
2961 if new_select_to != newpos:
2962 ## dbg('queuing selection: (%d, %d)' % (newpos, new_select_to))
2963 wx.CallAfter(self._SetSelection, newpos, new_select_to)
2964 else:
2965 newfield = self._FindField(newpos)
2966 if newfield != field and newfield._selectOnFieldEntry:
2967 ## dbg('queuing selection: (%d, %d)' % (newfield._extent[0], newfield._extent[1]))
2968 wx.CallAfter(self._SetSelection, newfield._extent[0], newfield._extent[1])
2969 else:
2970 wx.CallAfter(self._SetSelection, newpos, new_select_to)
2971 keep_processing = False
2972
2973 elif keep_processing:
2974 ## dbg('char not allowed')
2975 keep_processing = False
2976 if (not wx.Validator_IsSilent()) and orig_pos == pos:
2977 wx.Bell()
2978
2979 self._applyFormatting()
2980
2981 # Move to next insertion point
2982 if keep_processing and key not in self._nav:
2983 pos = self._GetInsertionPoint()
2984 next_entry = self._findNextEntry( pos )
2985 if pos != next_entry:
2986 ## dbg("moving from %(pos)d to next valid entry: %(next_entry)d" % locals())
2987 wx.CallAfter(self._SetInsertionPoint, next_entry )
2988
2989 if self._isTemplateChar(pos):
2990 self._AdjustField(pos)
2991 ## dbg(indent=0)
2992
2993
2994 def _FindFieldExtent(self, pos=None, getslice=False, value=None):
2995 """ returns editable extent of field corresponding to
2996 position pos, and, optionally, the contents of that field
2997 in the control or the value specified.
2998 Template chars are bound to the preceding field.
2999 For masks beginning with template chars, these chars are ignored
3000 when calculating the current field.
3001
3002 Eg: with template (###) ###-####,
3003 >>> self._FindFieldExtent(pos=0)
3004 1, 4
3005 >>> self._FindFieldExtent(pos=1)
3006 1, 4
3007 >>> self._FindFieldExtent(pos=5)
3008 1, 4
3009 >>> self._FindFieldExtent(pos=6)
3010 6, 9
3011 >>> self._FindFieldExtent(pos=10)
3012 10, 14
3013 etc.
3014 """
3015 ## dbg('MaskedEditMixin::_FindFieldExtent(pos=%s, getslice=%s)' % (str(pos), str(getslice)) ,indent=1)
3016
3017 field = self._FindField(pos)
3018 if not field:
3019 if getslice:
3020 return None, None, ""
3021 else:
3022 return None, None
3023 edit_start, edit_end = field._extent
3024 if getslice:
3025 if value is None: value = self._GetValue()
3026 slice = value[edit_start:edit_end]
3027 ## dbg('edit_start:', edit_start, 'edit_end:', edit_end, 'slice: "%s"' % slice)
3028 ## dbg(indent=0)
3029 return edit_start, edit_end, slice
3030 else:
3031 ## dbg('edit_start:', edit_start, 'edit_end:', edit_end)
3032 ## dbg(indent=0)
3033 return edit_start, edit_end
3034
3035
3036 def _FindField(self, pos=None):
3037 """
3038 Returns the field instance in which pos resides.
3039 Template chars are bound to the preceding field.
3040 For masks beginning with template chars, these chars are ignored
3041 when calculating the current field.
3042
3043 """
3044 #### dbg('MaskedEditMixin::_FindField(pos=%s)' % str(pos) ,indent=1)
3045 if pos is None: pos = self._GetInsertionPoint()
3046 elif pos < 0 or pos > self._masklength:
3047 raise IndexError('position %s out of range of control' % str(pos))
3048
3049 if len(self._fields) == 0:
3050 ## dbg(indent=0)
3051 return None
3052
3053 # else...
3054 #### dbg(indent=0)
3055 return self._fields[self._lookupField[pos]]
3056
3057
3058 def ClearValue(self):
3059 """ Blanks the current control value by replacing it with the default value."""
3060 ## dbg("MaskedEditMixin::ClearValue - value reset to default value (template)")
3061 self._SetValue( self._template )
3062 self._SetInsertionPoint(0)
3063 self.Refresh()
3064
3065
3066 def _baseCtrlEventHandler(self, event):
3067 """
3068 This function is used whenever a key should be handled by the base control.
3069 """
3070 event.Skip()
3071 return False
3072
3073
3074 def _OnUpNumeric(self, event):
3075 """
3076 Makes up-arrow act like shift-tab should; ie. take you to start of
3077 previous field.
3078 """
3079 ## dbg('MaskedEditMixin::_OnUpNumeric', indent=1)
3080 event.m_shiftDown = 1
3081 ## dbg('event.ShiftDown()?', event.ShiftDown())
3082 self._OnChangeField(event)
3083 ## dbg(indent=0)
3084
3085
3086 def _OnArrow(self, event):
3087 """
3088 Used in response to left/right navigation keys; makes these actions skip
3089 over mask template chars.
3090 """
3091 ## dbg("MaskedEditMixin::_OnArrow", indent=1)
3092 pos = self._GetInsertionPoint()
3093 keycode = event.GetKeyCode()
3094 sel_start, sel_to = self._GetSelection()
3095 entry_end = self._goEnd(getPosOnly=True)
3096 if keycode in (wx.WXK_RIGHT, wx.WXK_DOWN):
3097 if( ( not self._isTemplateChar(pos) and pos+1 > entry_end)
3098 or ( self._isTemplateChar(pos) and pos >= entry_end) ):
3099 ## dbg("can't advance", indent=0)
3100 return False
3101 elif self._isTemplateChar(pos):
3102 self._AdjustField(pos)
3103 elif keycode in (wx.WXK_LEFT,wx.WXK_UP) and sel_start == sel_to and pos > 0 and self._isTemplateChar(pos-1):
3104 ## dbg('adjusting field')
3105 self._AdjustField(pos)
3106
3107 # treat as shifted up/down arrows as tab/reverse tab:
3108 if event.ShiftDown() and keycode in (wx.WXK_UP, wx.WXK_DOWN):
3109 # remove "shifting" and treat as (forward) tab:
3110 event.m_shiftDown = False
3111 keep_processing = self._OnChangeField(event)
3112
3113 elif self._FindField(pos)._selectOnFieldEntry:
3114 if( keycode in (wx.WXK_UP, wx.WXK_LEFT)
3115 and sel_start != 0
3116 and self._isTemplateChar(sel_start-1)
3117 and sel_start != self._masklength
3118 and not self._signOk and not self._useParens):
3119
3120 # call _OnChangeField to handle "ctrl-shifted event"
3121 # (which moves to previous field and selects it.)
3122 event.m_shiftDown = True
3123 event.m_ControlDown = True
3124 keep_processing = self._OnChangeField(event)
3125 elif( keycode in (wx.WXK_DOWN, wx.WXK_RIGHT)
3126 and sel_to != self._masklength
3127 and self._isTemplateChar(sel_to)):
3128
3129 # when changing field to the right, ensure don't accidentally go left instead
3130 event.m_shiftDown = False
3131 keep_processing = self._OnChangeField(event)
3132 else:
3133 # treat arrows as normal, allowing selection
3134 # as appropriate:
3135 ## dbg('using base ctrl event processing')
3136 event.Skip()
3137 else:
3138 if( (sel_to == self._fields[0]._extent[0] and keycode == wx.WXK_LEFT)
3139 or (sel_to == self._masklength and keycode == wx.WXK_RIGHT) ):
3140 if not wx.Validator_IsSilent():
3141 wx.Bell()
3142 else:
3143 # treat arrows as normal, allowing selection
3144 # as appropriate:
3145 ## dbg('using base event processing')
3146 event.Skip()
3147
3148 keep_processing = False
3149 ## dbg(indent=0)
3150 return keep_processing
3151
3152
3153 def _OnCtrl_S(self, event):
3154 """ Default Ctrl-S handler; prints value information if demo enabled. """
3155 ## dbg("MaskedEditMixin::_OnCtrl_S")
3156 if self._demo:
3157 print 'MaskedEditMixin.GetValue() = "%s"\nMaskedEditMixin.GetPlainValue() = "%s"' % (self.GetValue(), self.GetPlainValue())
3158 print "Valid? => " + str(self.IsValid())
3159 print "Current field, start, end, value =", str( self._FindFieldExtent(getslice=True))
3160 return False
3161
3162
3163 def _OnCtrl_X(self, event=None):
3164 """ Handles ctrl-x keypress in control and Cut operation on context menu.
3165 Should return False to skip other processing. """
3166 ## dbg("MaskedEditMixin::_OnCtrl_X", indent=1)
3167 self.Cut()
3168 ## dbg(indent=0)
3169 return False
3170
3171 def _OnCtrl_C(self, event=None):
3172 """ Handles ctrl-C keypress in control and Copy operation on context menu.
3173 Uses base control handling. Should return False to skip other processing."""
3174 self.Copy()
3175 return False
3176
3177 def _OnCtrl_V(self, event=None):
3178 """ Handles ctrl-V keypress in control and Paste operation on context menu.
3179 Should return False to skip other processing. """
3180 ## dbg("MaskedEditMixin::_OnCtrl_V", indent=1)
3181 self.Paste()
3182 ## dbg(indent=0)
3183 return False
3184
3185 def _OnCtrl_Z(self, event=None):
3186 """ Handles ctrl-Z keypress in control and Undo operation on context menu.
3187 Should return False to skip other processing. """
3188 ## dbg("MaskedEditMixin::_OnCtrl_Z", indent=1)
3189 self.Undo()
3190 ## dbg(indent=0)
3191 return False
3192
3193 def _OnCtrl_A(self,event=None):
3194 """ Handles ctrl-a keypress in control. Should return False to skip other processing. """
3195 end = self._goEnd(getPosOnly=True)
3196 if not event or (isinstance(event, wx.KeyEvent) and event.ShiftDown()):
3197 wx.CallAfter(self._SetInsertionPoint, 0)
3198 wx.CallAfter(self._SetSelection, 0, self._masklength)
3199 else:
3200 wx.CallAfter(self._SetInsertionPoint, 0)
3201 wx.CallAfter(self._SetSelection, 0, end)
3202 return False
3203
3204
3205 def _OnErase(self, event=None, just_return_value=False):
3206 """ Handles backspace and delete keypress in control. Should return False to skip other processing."""
3207 ## dbg("MaskedEditMixin::_OnErase", indent=1)
3208 sel_start, sel_to = self._GetSelection() ## check for a range of selected text
3209
3210 if event is None: # called as action routine from Cut() operation.
3211 key = wx.WXK_DELETE
3212 else:
3213 key = event.GetKeyCode()
3214
3215 field = self._FindField(sel_to)
3216 start, end = field._extent
3217 value = self._GetValue()
3218 oldstart = sel_start
3219
3220 # If trying to erase beyond "legal" bounds, disallow operation:
3221 if( (sel_to == 0 and key == wx.WXK_BACK)
3222 or (self._signOk and sel_to == 1 and value[0] == ' ' and key == wx.WXK_BACK)
3223 or (sel_to == self._masklength and sel_start == sel_to and key == wx.WXK_DELETE and not field._insertRight)
3224 or (self._signOk and self._useParens
3225 and sel_start == sel_to
3226 and sel_to == self._masklength - 1
3227 and value[sel_to] == ' ' and key == wx.WXK_DELETE and not field._insertRight) ):
3228 if not wx.Validator_IsSilent():
3229 wx.Bell()
3230 ## dbg(indent=0)
3231 return False
3232
3233
3234 if( field._insertRight # an insert-right field
3235 and value[start:end] != self._template[start:end] # and field not empty
3236 and sel_start >= start # and selection starts in field
3237 and ((sel_to == sel_start # and no selection
3238 and sel_to == end # and cursor at right edge
3239 and key in (wx.WXK_BACK, wx.WXK_DELETE)) # and either delete or backspace key
3240 or # or
3241 (key == wx.WXK_BACK # backspacing
3242 and (sel_to == end # and selection ends at right edge
3243 or sel_to < end and field._allowInsert)) ) ): # or allow right insert at any point in field
3244
3245 ## dbg('delete left')
3246 # if backspace but left of cursor is empty, adjust cursor right before deleting
3247 while( key == wx.WXK_BACK
3248 and sel_start == sel_to
3249 and sel_start < end
3250 and value[start:sel_start] == self._template[start:sel_start]):
3251 sel_start += 1
3252 sel_to = sel_start
3253
3254 ## dbg('sel_start, start:', sel_start, start)
3255
3256 if sel_start == sel_to:
3257 keep = sel_start -1
3258 else:
3259 keep = sel_start
3260 newfield = value[start:keep] + value[sel_to:end]
3261
3262 # handle sign char moving from outside field into the field:
3263 move_sign_into_field = False
3264 if not field._padZero and self._signOk and self._isNeg and value[0] in ('-', '('):
3265 signchar = value[0]
3266 newfield = signchar + newfield
3267 move_sign_into_field = True
3268 ## dbg('cut newfield: "%s"' % newfield)
3269
3270 # handle what should fill in from the left:
3271 left = ""
3272 for i in range(start, end - len(newfield)):
3273 if field._padZero:
3274 left += '0'
3275 elif( self._signOk and self._isNeg and i == 1
3276 and ((self._useParens and newfield.find('(') == -1)
3277 or (not self._useParens and newfield.find('-') == -1)) ):
3278 left += ' '
3279 else:
3280 left += self._template[i] # this can produce strange results in combination with default values...
3281 newfield = left + newfield
3282 ## dbg('filled newfield: "%s"' % newfield)
3283
3284 newstr = value[:start] + newfield + value[end:]
3285
3286 # (handle sign located in "mask position" in front of field prior to delete)
3287 if move_sign_into_field:
3288 newstr = ' ' + newstr[1:]
3289 pos = sel_to
3290 else:
3291 # handle erasure of (left) sign, moving selection accordingly...
3292 if self._signOk and sel_start == 0:
3293 newstr = value = ' ' + value[1:]
3294 sel_start += 1
3295
3296 if field._allowInsert and sel_start >= start:
3297 # selection (if any) falls within current insert-capable field:
3298 select_len = sel_to - sel_start
3299 # determine where cursor should end up:
3300 if key == wx.WXK_BACK:
3301 if select_len == 0:
3302 newpos = sel_start -1
3303 else:
3304 newpos = sel_start
3305 erase_to = sel_to
3306 else:
3307 newpos = sel_start
3308 if sel_to == sel_start:
3309 erase_to = sel_to + 1
3310 else:
3311 erase_to = sel_to
3312
3313 if self._isTemplateChar(newpos) and select_len == 0:
3314 if self._signOk:
3315 if value[newpos] in ('(', '-'):
3316 newpos += 1 # don't move cusor
3317 newstr = ' ' + value[newpos:]
3318 elif value[newpos] == ')':
3319 # erase right sign, but don't move cursor; (matching left sign handled later)
3320 newstr = value[:newpos] + ' '
3321 else:
3322 # no deletion; just move cursor
3323 newstr = value
3324 else:
3325 # no deletion; just move cursor
3326 newstr = value
3327 else:
3328 if erase_to > end: erase_to = end
3329 erase_len = erase_to - newpos
3330
3331 left = value[start:newpos]
3332 ## dbg("retained ='%s'" % value[erase_to:end], 'sel_to:', sel_to, "fill: '%s'" % self._template[end - erase_len:end])
3333 right = value[erase_to:end] + self._template[end-erase_len:end]
3334 pos_adjust = 0
3335 if field._alignRight:
3336 rstripped = right.rstrip()
3337 if rstripped != right:
3338 pos_adjust = len(right) - len(rstripped)
3339 right = rstripped
3340
3341 if not field._insertRight and value[-1] == ')' and end == self._masklength - 1:
3342 # need to shift ) into the field:
3343 right = right[:-1] + ')'
3344 value = value[:-1] + ' '
3345
3346 newfield = left+right
3347 if pos_adjust:
3348 newfield = newfield.rjust(end-start)
3349 newpos += pos_adjust
3350 ## dbg("left='%s', right ='%s', newfield='%s'" %(left, right, newfield))
3351 newstr = value[:start] + newfield + value[end:]
3352
3353 pos = newpos
3354
3355 else:
3356 if sel_start == sel_to:
3357 ## dbg("current sel_start, sel_to:", sel_start, sel_to)
3358 if key == wx.WXK_BACK:
3359 sel_start, sel_to = sel_to-1, sel_to-1
3360 ## dbg("new sel_start, sel_to:", sel_start, sel_to)
3361
3362 if field._padZero and not value[start:sel_to].replace('0', '').replace(' ','').replace(field._fillChar, ''):
3363 # preceding chars (if any) are zeros, blanks or fillchar; new char should be 0:
3364 newchar = '0'
3365 else:
3366 newchar = self._template[sel_to] ## get an original template character to "clear" the current char
3367 ## dbg('value = "%s"' % value, 'value[%d] = "%s"' %(sel_start, value[sel_start]))
3368
3369 if self._isTemplateChar(sel_to):
3370 if sel_to == 0 and self._signOk and value[sel_to] == '-': # erasing "template" sign char
3371 newstr = ' ' + value[1:]
3372 sel_to += 1
3373 elif self._signOk and self._useParens and (value[sel_to] == ')' or value[sel_to] == '('):
3374 # allow "change sign" by removing both parens:
3375 newstr = value[:self._signpos] + ' ' + value[self._signpos+1:-1] + ' '
3376 else:
3377 newstr = value
3378 newpos = sel_to
3379 else:
3380 if field._insertRight and sel_start == sel_to:
3381 # force non-insert-right behavior, by selecting char to be replaced:
3382 sel_to += 1
3383 newstr, ignore = self._insertKey(newchar, sel_start, sel_start, sel_to, value)
3384
3385 else:
3386 # selection made
3387 newstr = self._eraseSelection(value, sel_start, sel_to)
3388
3389 pos = sel_start # put cursor back at beginning of selection
3390
3391 if self._signOk and self._useParens:
3392 # account for resultant unbalanced parentheses:
3393 left_signpos = newstr.find('(')
3394 right_signpos = newstr.find(')')
3395
3396 if left_signpos == -1 and right_signpos != -1:
3397 # erased left-sign marker; get rid of right sign marker:
3398 newstr = newstr[:right_signpos] + ' ' + newstr[right_signpos+1:]
3399
3400 elif left_signpos != -1 and right_signpos == -1:
3401 # erased right-sign marker; get rid of left-sign marker:
3402 newstr = newstr[:left_signpos] + ' ' + newstr[left_signpos+1:]
3403
3404 ## dbg("oldstr:'%s'" % value, 'oldpos:', oldstart)
3405 ## dbg("newstr:'%s'" % newstr, 'pos:', pos)
3406
3407 # if erasure results in an invalid field, disallow it:
3408 ## dbg('field._validRequired?', field._validRequired)
3409 ## dbg('field.IsValid("%s")?' % newstr[start:end], field.IsValid(newstr[start:end]))
3410 if field._validRequired and not field.IsValid(newstr[start:end]):
3411 if not wx.Validator_IsSilent():
3412 wx.Bell()
3413 ## dbg(indent=0)
3414 return False
3415
3416 # if erasure results in an invalid value, disallow it:
3417 if self._ctrl_constraints._validRequired and not self.IsValid(newstr):
3418 if not wx.Validator_IsSilent():
3419 wx.Bell()
3420 ## dbg(indent=0)
3421 return False
3422
3423 if just_return_value:
3424 ## dbg(indent=0)
3425 return newstr
3426
3427 # else...
3428 ## dbg('setting value (later) to', newstr)
3429 wx.CallAfter(self._SetValue, newstr)
3430 ## dbg('setting insertion point (later) to', pos)
3431 wx.CallAfter(self._SetInsertionPoint, pos)
3432 ## dbg(indent=0)
3433 if newstr != value:
3434 self.modified = True
3435 return False
3436
3437
3438 def _OnEnd(self,event):
3439 """ Handles End keypress in control. Should return False to skip other processing. """
3440 ## dbg("MaskedEditMixin::_OnEnd", indent=1)
3441 pos = self._adjustPos(self._GetInsertionPoint(), event.GetKeyCode())
3442 if not event.ControlDown():
3443 end = self._masklength # go to end of control
3444 if self._signOk and self._useParens:
3445 end = end - 1 # account for reserved char at end
3446 else:
3447 end_of_input = self._goEnd(getPosOnly=True)
3448 sel_start, sel_to = self._GetSelection()
3449 if sel_to < pos: sel_to = pos
3450 field = self._FindField(sel_to)
3451 field_end = self._FindField(end_of_input)
3452
3453 # pick different end point if either:
3454 # - cursor not in same field
3455 # - or at or past last input already
3456 # - or current selection = end of current field:
3457 #### dbg('field != field_end?', field != field_end)
3458 #### dbg('sel_to >= end_of_input?', sel_to >= end_of_input)
3459 if field != field_end or sel_to >= end_of_input:
3460 edit_start, edit_end = field._extent
3461 #### dbg('edit_end:', edit_end)
3462 #### dbg('sel_to:', sel_to)
3463 #### dbg('sel_to == edit_end?', sel_to == edit_end)
3464 #### dbg('field._index < self._field_indices[-1]?', field._index < self._field_indices[-1])
3465
3466 if sel_to == edit_end and field._index < self._field_indices[-1]:
3467 edit_start, edit_end = self._FindFieldExtent(self._findNextEntry(edit_end)) # go to end of next field:
3468 end = edit_end
3469 ## dbg('end moved to', end)
3470
3471 elif sel_to == edit_end and field._index == self._field_indices[-1]:
3472 # already at edit end of last field; select to end of control:
3473 end = self._masklength
3474 ## dbg('end moved to', end)
3475 else:
3476 end = edit_end # select to end of current field
3477 ## dbg('end moved to ', end)
3478 else:
3479 # select to current end of input
3480 end = end_of_input
3481
3482
3483 #### dbg('pos:', pos, 'end:', end)
3484
3485 if event.ShiftDown():
3486 if not event.ControlDown():
3487 ## dbg("shift-end; select to end of control")
3488 pass
3489 else:
3490 ## dbg("shift-ctrl-end; select to end of non-whitespace")
3491 pass
3492 wx.CallAfter(self._SetInsertionPoint, pos)
3493 wx.CallAfter(self._SetSelection, pos, end)
3494 else:
3495 if not event.ControlDown():
3496 ## dbg('go to end of control:')
3497 pass
3498 wx.CallAfter(self._SetInsertionPoint, end)
3499 wx.CallAfter(self._SetSelection, end, end)
3500
3501 ## dbg(indent=0)
3502 return False
3503
3504
3505 def _OnReturn(self, event):
3506 """
3507 Swallows the return, issues a Navigate event instead, since
3508 masked controls are "single line" by defn.
3509 """
3510 ## dbg('MaskedEditMixin::OnReturn')
3511 self.Navigate(True)
3512 return False
3513
3514
3515 def _OnHome(self,event):
3516 """ Handles Home keypress in control. Should return False to skip other processing."""
3517 ## dbg("MaskedEditMixin::_OnHome", indent=1)
3518 pos = self._adjustPos(self._GetInsertionPoint(), event.GetKeyCode())
3519 sel_start, sel_to = self._GetSelection()
3520
3521 # There are 5 cases here:
3522
3523 # 1) shift: select from start of control to end of current
3524 # selection.
3525 if event.ShiftDown() and not event.ControlDown():
3526 ## dbg("shift-home; select to start of control")
3527 start = 0
3528 end = sel_start
3529
3530 # 2) no shift, no control: move cursor to beginning of control.
3531 elif not event.ControlDown():
3532 ## dbg("home; move to start of control")
3533 start = 0
3534 end = 0
3535
3536 # 3) No shift, control: move cursor back to beginning of field; if
3537 # there already, go to beginning of previous field.
3538 # 4) shift, control, start of selection not at beginning of control:
3539 # move sel_start back to start of field; if already there, go to
3540 # start of previous field.
3541 elif( event.ControlDown()
3542 and (not event.ShiftDown()
3543 or (event.ShiftDown() and sel_start > 0) ) ):
3544 if len(self._field_indices) > 1:
3545 field = self._FindField(sel_start)
3546 start, ignore = field._extent
3547 if sel_start == start and field._index != self._field_indices[0]: # go to start of previous field:
3548 start, ignore = self._FindFieldExtent(sel_start-1)
3549 elif sel_start == start:
3550 start = 0 # go to literal beginning if edit start
3551 # not at that point
3552 end_of_field = True
3553
3554 else:
3555 start = 0
3556
3557 if not event.ShiftDown():
3558 ## dbg("ctrl-home; move to beginning of field")
3559 end = start
3560 else:
3561 ## dbg("shift-ctrl-home; select to beginning of field")
3562 end = sel_to
3563
3564 else:
3565 # 5) shift, control, start of selection at beginning of control:
3566 # unselect by moving sel_to backward to beginning of current field;
3567 # if already there, move to start of previous field.
3568 start = sel_start
3569 if len(self._field_indices) > 1:
3570 # find end of previous field:
3571 field = self._FindField(sel_to)
3572 if sel_to > start and field._index != self._field_indices[0]:
3573 ignore, end = self._FindFieldExtent(field._extent[0]-1)
3574 else:
3575 end = start
3576 end_of_field = True
3577 else:
3578 end = start
3579 end_of_field = False
3580 ## dbg("shift-ctrl-home; unselect to beginning of field")
3581
3582 ## dbg('queuing new sel_start, sel_to:', (start, end))
3583 wx.CallAfter(self._SetInsertionPoint, start)
3584 wx.CallAfter(self._SetSelection, start, end)
3585 ## dbg(indent=0)
3586 return False
3587
3588
3589 def _OnChangeField(self, event):
3590 """
3591 Primarily handles TAB events, but can be used for any key that
3592 designer wants to change fields within a masked edit control.
3593 """
3594 ## dbg('MaskedEditMixin::_OnChangeField', indent = 1)
3595 # determine end of current field:
3596 pos = self._GetInsertionPoint()
3597 ## dbg('current pos:', pos)
3598 sel_start, sel_to = self._GetSelection()
3599
3600 if self._masklength < 0: # no fields; process tab normally
3601 self._AdjustField(pos)
3602 if event.GetKeyCode() == wx.WXK_TAB:
3603 ## dbg('tab to next ctrl')
3604 # As of 2.5.2, you don't call event.Skip() to do
3605 # this, but instead force explicit navigation, if
3606 # wx.TE_PROCESS_TAB is used (like in the masked edits)
3607 self.Navigate(True)
3608 #else: do nothing
3609 ## dbg(indent=0)
3610 return False
3611
3612
3613 if event.ShiftDown():
3614
3615 # "Go backward"
3616
3617 # NOTE: doesn't yet work with SHIFT-tab under wx; the control
3618 # never sees this event! (But I've coded for it should it ever work,
3619 # and it *does* work for '.' in IpAddrCtrl.)
3620 field = self._FindField(pos)
3621 index = field._index
3622 field_start = field._extent[0]
3623 if pos < field_start:
3624 ## dbg('cursor before 1st field; cannot change to a previous field')
3625 if not wx.Validator_IsSilent():
3626 wx.Bell()
3627 return False
3628
3629 if event.ControlDown():
3630 ## dbg('queuing select to beginning of field:', field_start, pos)
3631 wx.CallAfter(self._SetInsertionPoint, field_start)
3632 wx.CallAfter(self._SetSelection, field_start, pos)
3633 ## dbg(indent=0)
3634 return False
3635
3636 elif index == 0:
3637 # We're already in the 1st field; process shift-tab normally:
3638 self._AdjustField(pos)
3639 if event.GetKeyCode() == wx.WXK_TAB:
3640 ## dbg('tab to previous ctrl')
3641 # As of 2.5.2, you don't call event.Skip() to do
3642 # this, but instead force explicit navigation, if
3643 # wx.TE_PROCESS_TAB is used (like in the masked edits)
3644 self.Navigate(False)
3645 else:
3646 ## dbg('position at beginning')
3647 wx.CallAfter(self._SetInsertionPoint, field_start)
3648 ## dbg(indent=0)
3649 return False
3650 else:
3651 # find beginning of previous field:
3652 begin_prev = self._FindField(field_start-1)._extent[0]
3653 self._AdjustField(pos)
3654 ## dbg('repositioning to', begin_prev)
3655 wx.CallAfter(self._SetInsertionPoint, begin_prev)
3656 if self._FindField(begin_prev)._selectOnFieldEntry:
3657 edit_start, edit_end = self._FindFieldExtent(begin_prev)
3658 ## dbg('queuing selection to (%d, %d)' % (edit_start, edit_end))
3659 wx.CallAfter(self._SetInsertionPoint, edit_start)
3660 wx.CallAfter(self._SetSelection, edit_start, edit_end)
3661 ## dbg(indent=0)
3662 return False
3663
3664 else:
3665 # "Go forward"
3666 field = self._FindField(sel_to)
3667 field_start, field_end = field._extent
3668 if event.ControlDown():
3669 ## dbg('queuing select to end of field:', pos, field_end)
3670 wx.CallAfter(self._SetInsertionPoint, pos)
3671 wx.CallAfter(self._SetSelection, pos, field_end)
3672 ## dbg(indent=0)
3673 return False
3674 else:
3675 if pos < field_start:
3676 ## dbg('cursor before 1st field; go to start of field')
3677 wx.CallAfter(self._SetInsertionPoint, field_start)
3678 if field._selectOnFieldEntry:
3679 wx.CallAfter(self._SetSelection, field_start, field_end)
3680 else:
3681 wx.CallAfter(self._SetSelection, field_start, field_start)
3682 return False
3683 # else...
3684 ## dbg('end of current field:', field_end)
3685 ## dbg('go to next field')
3686 if field_end == self._fields[self._field_indices[-1]]._extent[1]:
3687 self._AdjustField(pos)
3688 if event.GetKeyCode() == wx.WXK_TAB:
3689 ## dbg('tab to next ctrl')
3690 # As of 2.5.2, you don't call event.Skip() to do
3691 # this, but instead force explicit navigation, if
3692 # wx.TE_PROCESS_TAB is used (like in the masked edits)
3693 self.Navigate(True)
3694 else:
3695 ## dbg('position at end')
3696 wx.CallAfter(self._SetInsertionPoint, field_end)
3697 ## dbg(indent=0)
3698 return False
3699 else:
3700 # we have to find the start of the next field
3701 next_pos = self._findNextEntry(field_end)
3702 if next_pos == field_end:
3703 ## dbg('already in last field')
3704 self._AdjustField(pos)
3705 if event.GetKeyCode() == wx.WXK_TAB:
3706 ## dbg('tab to next ctrl')
3707 # As of 2.5.2, you don't call event.Skip() to do
3708 # this, but instead force explicit navigation, if
3709 # wx.TE_PROCESS_TAB is used (like in the masked edits)
3710 self.Navigate(True)
3711 #else: do nothing
3712 ## dbg(indent=0)
3713 return False
3714 else:
3715 self._AdjustField( pos )
3716
3717 # move cursor to appropriate point in the next field and select as necessary:
3718 field = self._FindField(next_pos)
3719 edit_start, edit_end = field._extent
3720 if field._selectOnFieldEntry:
3721 ## dbg('move to ', next_pos)
3722 wx.CallAfter(self._SetInsertionPoint, next_pos)
3723 edit_start, edit_end = self._FindFieldExtent(next_pos)
3724 ## dbg('queuing select', edit_start, edit_end)
3725 wx.CallAfter(self._SetSelection, edit_start, edit_end)
3726 else:
3727 if field._insertRight:
3728 next_pos = field._extent[1]
3729 ## dbg('move to ', next_pos)
3730 wx.CallAfter(self._SetInsertionPoint, next_pos)
3731 ## dbg(indent=0)
3732 return False
3733
3734
3735 def _OnDecimalPoint(self, event):
3736 ## dbg('MaskedEditMixin::_OnDecimalPoint', indent=1)
3737
3738 pos = self._adjustPos(self._GetInsertionPoint(), event.GetKeyCode())
3739
3740 if self._isFloat: ## handle float value, move to decimal place
3741 ## dbg('key == Decimal tab; decimal pos:', self._decimalpos)
3742 value = self._GetValue()
3743 if pos < self._decimalpos:
3744 clipped_text = value[0:pos] + self._decimalChar + value[self._decimalpos+1:]
3745 ## dbg('value: "%s"' % self._GetValue(), "clipped_text:'%s'" % clipped_text)
3746 newstr = self._adjustFloat(clipped_text)
3747 else:
3748 newstr = self._adjustFloat(value)
3749 wx.CallAfter(self._SetValue, newstr)
3750 fraction = self._fields[1]
3751 start, end = fraction._extent
3752 wx.CallAfter(self._SetInsertionPoint, start)
3753 if fraction._selectOnFieldEntry:
3754 ## dbg('queuing selection after decimal point to:', (start, end))
3755 wx.CallAfter(self._SetSelection, start, end)
3756 else:
3757 wx.CallAfter(self._SetSelection, start, start)
3758 keep_processing = False
3759
3760 if self._isInt: ## handle integer value, truncate from current position
3761 ## dbg('key == Integer decimal event')
3762 value = self._GetValue()
3763 clipped_text = value[0:pos]
3764 ## dbg('value: "%s"' % self._GetValue(), "clipped_text:'%s'" % clipped_text)
3765 newstr = self._adjustInt(clipped_text)
3766 ## dbg('newstr: "%s"' % newstr)
3767 wx.CallAfter(self._SetValue, newstr)
3768 newpos = len(newstr.rstrip())
3769 if newstr.find(')') != -1:
3770 newpos -= 1 # (don't move past right paren)
3771 wx.CallAfter(self._SetInsertionPoint, newpos)
3772 wx.CallAfter(self._SetSelection, newpos, newpos)
3773 keep_processing = False
3774 ## dbg(indent=0)
3775
3776
3777 def _OnChangeSign(self, event):
3778 ## dbg('MaskedEditMixin::_OnChangeSign', indent=1)
3779 key = event.GetKeyCode()
3780 pos = self._adjustPos(self._GetInsertionPoint(), key)
3781 value = self._eraseSelection()
3782 integer = self._fields[0]
3783 start, end = integer._extent
3784
3785 #### dbg('adjusted pos:', pos)
3786 if chr(key) in ('-','+','(', ')') or (chr(key) == " " and pos == self._signpos):
3787 cursign = self._isNeg
3788 ## dbg('cursign:', cursign)
3789 if chr(key) in ('-','(', ')'):
3790 self._isNeg = (not self._isNeg) ## flip value
3791 else:
3792 self._isNeg = False
3793 ## dbg('isNeg?', self._isNeg)
3794
3795 text, self._signpos, self._right_signpos = self._getSignedValue(candidate=value)
3796 ## dbg('text:"%s"' % text, 'signpos:', self._signpos, 'right_signpos:', self._right_signpos)
3797 if text is None:
3798 text = value
3799
3800 if self._isNeg and self._signpos is not None and self._signpos != -1:
3801 if self._useParens and self._right_signpos is not None:
3802 text = text[:self._signpos] + '(' + text[self._signpos+1:self._right_signpos] + ')' + text[self._right_signpos+1:]
3803 else:
3804 text = text[:self._signpos] + '-' + text[self._signpos+1:]
3805 else:
3806 #### dbg('self._isNeg?', self._isNeg, 'self.IsValid(%s)' % text, self.IsValid(text))
3807 if self._useParens:
3808 text = text[:self._signpos] + ' ' + text[self._signpos+1:self._right_signpos] + ' ' + text[self._right_signpos+1:]
3809 else:
3810 text = text[:self._signpos] + ' ' + text[self._signpos+1:]
3811 ## dbg('clearing self._isNeg')
3812 self._isNeg = False
3813
3814 wx.CallAfter(self._SetValue, text)
3815 wx.CallAfter(self._applyFormatting)
3816 ## dbg('pos:', pos, 'signpos:', self._signpos)
3817 if pos == self._signpos or integer.IsEmpty(text[start:end]):
3818 wx.CallAfter(self._SetInsertionPoint, self._signpos+1)
3819 else:
3820 wx.CallAfter(self._SetInsertionPoint, pos)
3821
3822 keep_processing = False
3823 else:
3824 keep_processing = True
3825 ## dbg(indent=0)
3826 return keep_processing
3827
3828
3829 def _OnGroupChar(self, event):
3830 """
3831 This handler is only registered if the mask is a numeric mask.
3832 It allows the insertion of ',' or '.' if appropriate.
3833 """
3834 ## dbg('MaskedEditMixin::_OnGroupChar', indent=1)
3835 keep_processing = True
3836 pos = self._adjustPos(self._GetInsertionPoint(), event.GetKeyCode())
3837 sel_start, sel_to = self._GetSelection()
3838 groupchar = self._fields[0]._groupChar
3839 if not self._isCharAllowed(groupchar, pos, checkRegex=True):
3840 keep_processing = False
3841 if not wx.Validator_IsSilent():
3842 wx.Bell()
3843
3844 if keep_processing:
3845 newstr, newpos = self._insertKey(groupchar, pos, sel_start, sel_to, self._GetValue() )
3846 ## dbg("str with '%s' inserted:" % groupchar, '"%s"' % newstr)
3847 if self._ctrl_constraints._validRequired and not self.IsValid(newstr):
3848 keep_processing = False
3849 if not wx.Validator_IsSilent():
3850 wx.Bell()
3851
3852 if keep_processing:
3853 wx.CallAfter(self._SetValue, newstr)
3854 wx.CallAfter(self._SetInsertionPoint, newpos)
3855 keep_processing = False
3856 ## dbg(indent=0)
3857 return keep_processing
3858
3859
3860 def _findNextEntry(self,pos, adjustInsert=True):
3861 """ Find the insertion point for the next valid entry character position."""
3862 if self._isTemplateChar(pos): # if changing fields, pay attn to flag
3863 adjustInsert = adjustInsert
3864 else: # else within a field; flag not relevant
3865 adjustInsert = False
3866
3867 while self._isTemplateChar(pos) and pos < self._masklength:
3868 pos += 1
3869
3870 # if changing fields, and we've been told to adjust insert point,
3871 # look at new field; if empty and right-insert field,
3872 # adjust to right edge:
3873 if adjustInsert and pos < self._masklength:
3874 field = self._FindField(pos)
3875 start, end = field._extent
3876 slice = self._GetValue()[start:end]
3877 if field._insertRight and field.IsEmpty(slice):
3878 pos = end
3879 return pos
3880
3881
3882 def _findNextTemplateChar(self, pos):
3883 """ Find the position of the next non-editable character in the mask."""
3884 while not self._isTemplateChar(pos) and pos < self._masklength:
3885 pos += 1
3886 return pos
3887
3888
3889 def _OnAutoCompleteField(self, event):
3890 ## dbg('MaskedEditMixin::_OnAutoCompleteField', indent =1)
3891 pos = self._GetInsertionPoint()
3892 field = self._FindField(pos)
3893 edit_start, edit_end, slice = self._FindFieldExtent(pos, getslice=True)
3894
3895 match_index = None
3896 keycode = event.GetKeyCode()
3897
3898 if field._fillChar != ' ':
3899 text = slice.replace(field._fillChar, '')
3900 else:
3901 text = slice
3902 text = text.strip()
3903 keep_processing = True # (assume True to start)
3904 ## dbg('field._hasList?', field._hasList)
3905 if field._hasList:
3906 ## dbg('choices:', field._choices)
3907 ## dbg('compareChoices:', field._compareChoices)
3908 choices, choice_required = field._compareChoices, field._choiceRequired
3909 if keycode in (wx.WXK_PRIOR, wx.WXK_UP):
3910 direction = -1
3911 else:
3912 direction = 1
3913 match_index, partial_match = self._autoComplete(direction, choices, text, compareNoCase=field._compareNoCase, current_index = field._autoCompleteIndex)
3914 if( match_index is None
3915 and (keycode in self._autoCompleteKeycodes + [wx.WXK_PRIOR, wx.WXK_NEXT]
3916 or (keycode in [wx.WXK_UP, wx.WXK_DOWN] and event.ShiftDown() ) ) ):
3917 # Select the 1st thing from the list:
3918 match_index = 0
3919
3920 if( match_index is not None
3921 and ( keycode in self._autoCompleteKeycodes + [wx.WXK_PRIOR, wx.WXK_NEXT]
3922 or (keycode in [wx.WXK_UP, wx.WXK_DOWN] and event.ShiftDown())
3923 or (keycode == wx.WXK_DOWN and partial_match) ) ):
3924
3925 # We're allowed to auto-complete:
3926 ## dbg('match found')
3927 value = self._GetValue()
3928 newvalue = value[:edit_start] + field._choices[match_index] + value[edit_end:]
3929 ## dbg('setting value to "%s"' % newvalue)
3930 self._SetValue(newvalue)
3931 self._SetInsertionPoint(min(edit_end, len(newvalue.rstrip())))
3932 self._OnAutoSelect(field, match_index)
3933 self._CheckValid() # recolor as appopriate
3934
3935
3936 if keycode in (wx.WXK_UP, wx.WXK_DOWN, wx.WXK_LEFT, wx.WXK_RIGHT):
3937 # treat as left right arrow if unshifted, tab/shift tab if shifted.
3938 if event.ShiftDown():
3939 if keycode in (wx.WXK_DOWN, wx.WXK_RIGHT):
3940 # remove "shifting" and treat as (forward) tab:
3941 event.m_shiftDown = False
3942 keep_processing = self._OnChangeField(event)
3943 else:
3944 keep_processing = self._OnArrow(event)
3945 # else some other key; keep processing the key
3946
3947 ## dbg('keep processing?', keep_processing, indent=0)
3948 return keep_processing
3949
3950
3951 def _OnAutoSelect(self, field, match_index = None):
3952 """
3953 Function called if autoselect feature is enabled and entire control
3954 is selected:
3955 """
3956 ## dbg('MaskedEditMixin::OnAutoSelect', field._index)
3957 if match_index is not None:
3958 field._autoCompleteIndex = match_index
3959
3960
3961 def _autoComplete(self, direction, choices, value, compareNoCase, current_index):
3962 """
3963 This function gets called in response to Auto-complete events.
3964 It attempts to find a match to the specified value against the
3965 list of choices; if exact match, the index of then next
3966 appropriate value in the list, based on the given direction.
3967 If not an exact match, it will return the index of the 1st value from
3968 the choice list for which the partial value can be extended to match.
3969 If no match found, it will return None.
3970 The function returns a 2-tuple, with the 2nd element being a boolean
3971 that indicates if partial match was necessary.
3972 """
3973 ## dbg('autoComplete(direction=', direction, 'choices=',choices, 'value=',value,'compareNoCase?', compareNoCase, 'current_index:', current_index, indent=1)
3974 if value is None:
3975 ## dbg('nothing to match against', indent=0)
3976 return (None, False)
3977
3978 partial_match = False
3979
3980 if compareNoCase:
3981 value = value.lower()
3982
3983 last_index = len(choices) - 1
3984 if value in choices:
3985 ## dbg('"%s" in', choices)
3986 if current_index is not None and choices[current_index] == value:
3987 index = current_index
3988 else:
3989 index = choices.index(value)
3990
3991 ## dbg('matched "%s" (%d)' % (choices[index], index))
3992 if direction == -1:
3993 ## dbg('going to previous')
3994 if index == 0: index = len(choices) - 1
3995 else: index -= 1
3996 else:
3997 if index == len(choices) - 1: index = 0
3998 else: index += 1
3999 ## dbg('change value to "%s" (%d)' % (choices[index], index))
4000 match = index
4001 else:
4002 partial_match = True
4003 value = value.strip()
4004 ## dbg('no match; try to auto-complete:')
4005 match = None
4006 ## dbg('searching for "%s"' % value)
4007 if current_index is None:
4008 indices = range(len(choices))
4009 if direction == -1:
4010 indices.reverse()
4011 else:
4012 if direction == 1:
4013 indices = range(current_index +1, len(choices)) + range(current_index+1)
4014 ## dbg('range(current_index+1 (%d), len(choices) (%d)) + range(%d):' % (current_index+1, len(choices), current_index+1), indices)
4015 else:
4016 indices = range(current_index-1, -1, -1) + range(len(choices)-1, current_index-1, -1)
4017 ## dbg('range(current_index-1 (%d), -1) + range(len(choices)-1 (%d)), current_index-1 (%d):' % (current_index-1, len(choices)-1, current_index-1), indices)
4018 #### dbg('indices:', indices)
4019 for index in indices:
4020 choice = choices[index]
4021 if choice.find(value, 0) == 0:
4022 ## dbg('match found:', choice)
4023 match = index
4024 break
4025 else: dbg('choice: "%s" - no match' % choice)
4026 if match is not None:
4027 ## dbg('matched', match)
4028 pass
4029 else:
4030 ## dbg('no match found')
4031 pass
4032 ## dbg(indent=0)
4033 return (match, partial_match)
4034
4035
4036 def _AdjustField(self, pos):
4037 """
4038 This function gets called by default whenever the cursor leaves a field.
4039 The pos argument given is the char position before leaving that field.
4040 By default, floating point, integer and date values are adjusted to be
4041 legal in this function. Derived classes may override this function
4042 to modify the value of the control in a different way when changing fields.
4043
4044 NOTE: these change the value immediately, and restore the cursor to
4045 the passed location, so that any subsequent code can then move it
4046 based on the operation being performed.
4047 """
4048 newvalue = value = self._GetValue()
4049 field = self._FindField(pos)
4050 start, end, slice = self._FindFieldExtent(getslice=True)
4051 newfield = field._AdjustField(slice)
4052 newvalue = value[:start] + newfield + value[end:]
4053
4054 if self._isFloat and newvalue != self._template:
4055 newvalue = self._adjustFloat(newvalue)
4056
4057 if self._ctrl_constraints._isInt and value != self._template:
4058 newvalue = self._adjustInt(value)
4059
4060 if self._isDate and value != self._template:
4061 newvalue = self._adjustDate(value, fixcentury=True)
4062 if self._4digityear:
4063 year2dig = self._dateExtent - 2
4064 if pos == year2dig and value[year2dig] != newvalue[year2dig]:
4065 pos = pos+2
4066
4067 if newvalue != value:
4068 ## dbg('old value: "%s"\nnew value: "%s"' % (value, newvalue))
4069 self._SetValue(newvalue)
4070 self._SetInsertionPoint(pos)
4071
4072
4073 def _adjustKey(self, pos, key):
4074 """ Apply control formatting to the key (e.g. convert to upper etc). """
4075 field = self._FindField(pos)
4076 if field._forceupper and key in range(97,123):
4077 key = ord( chr(key).upper())
4078
4079 if field._forcelower and key in range(97,123):
4080 key = ord( chr(key).lower())
4081
4082 return key
4083
4084
4085 def _adjustPos(self, pos, key):
4086 """
4087 Checks the current insertion point position and adjusts it if
4088 necessary to skip over non-editable characters.
4089 """
4090 ## dbg('_adjustPos', pos, key, indent=1)
4091 sel_start, sel_to = self._GetSelection()
4092 # If a numeric or decimal mask, and negatives allowed, reserve the
4093 # first space for sign, and last one if using parens.
4094 if( self._signOk
4095 and ((pos == self._signpos and key in (ord('-'), ord('+'), ord(' ')) )
4096 or self._useParens and pos == self._masklength -1)):
4097 ## dbg('adjusted pos:', pos, indent=0)
4098 return pos
4099
4100 if key not in self._nav:
4101 field = self._FindField(pos)
4102
4103 ## dbg('field._insertRight?', field._insertRight)
4104 if field._insertRight: # if allow right-insert
4105 start, end = field._extent
4106 slice = self._GetValue()[start:end].strip()
4107 field_len = end - start
4108 if pos == end: # if cursor at right edge of field
4109 # if not filled or supposed to stay in field, keep current position
4110 #### dbg('pos==end')
4111 #### dbg('len (slice):', len(slice))
4112 #### dbg('field_len?', field_len)
4113 #### dbg('pos==end; len (slice) < field_len?', len(slice) < field_len)
4114 #### dbg('not field._moveOnFieldFull?', not field._moveOnFieldFull)
4115 if len(slice) == field_len and field._moveOnFieldFull:
4116 # move cursor to next field:
4117 pos = self._findNextEntry(pos)
4118 self._SetInsertionPoint(pos)
4119 if pos < sel_to:
4120 self._SetSelection(pos, sel_to) # restore selection
4121 else:
4122 self._SetSelection(pos, pos) # remove selection
4123 else: # leave cursor alone
4124 pass
4125 else:
4126 # if at start of control, move to right edge
4127 if (sel_to == sel_start
4128 and (self._isTemplateChar(pos) or (pos == start and len(slice)+ 1 < field_len))
4129 and pos != end):
4130 pos = end # move to right edge
4131 ## elif sel_start <= start and sel_to == end:
4132 ## # select to right edge of field - 1 (to replace char)
4133 ## pos = end - 1
4134 ## self._SetInsertionPoint(pos)
4135 ## # restore selection
4136 ## self._SetSelection(sel_start, pos)
4137
4138 elif self._signOk and sel_start == 0: # if selected to beginning and signed,
4139 # adjust to past reserved sign position:
4140 pos = self._fields[0]._extent[0]
4141 self._SetInsertionPoint(pos)
4142 # restore selection
4143 self._SetSelection(pos, sel_to)
4144 else:
4145 pass # leave position/selection alone
4146
4147 # else make sure the user is not trying to type over a template character
4148 # If they are, move them to the next valid entry position
4149 elif self._isTemplateChar(pos):
4150 if( not field._moveOnFieldFull
4151 and (not self._signOk
4152 or (self._signOk
4153 and field._index == 0
4154 and pos > 0) ) ): # don't move to next field without explicit cursor movement
4155 pass
4156 else:
4157 # find next valid position
4158 pos = self._findNextEntry(pos)
4159 self._SetInsertionPoint(pos)
4160 if pos < sel_to: # restore selection
4161 self._SetSelection(pos, sel_to)
4162 else:
4163 self._SetSelection(pos, pos)
4164 ## dbg('adjusted pos:', pos, indent=0)
4165 return pos
4166
4167
4168 def _adjustFloat(self, candidate=None):
4169 """
4170 'Fixes' an floating point control. Collapses spaces, right-justifies, etc.
4171 """
4172 ## dbg('MaskedEditMixin::_adjustFloat, candidate = "%s"' % candidate, indent=1)
4173 lenInt,lenFraction = [len(s) for s in self._mask.split('.')] ## Get integer, fraction lengths
4174
4175 if candidate is None: value = self._GetValue()
4176 else: value = candidate
4177 ## dbg('value = "%(value)s"' % locals(), 'len(value):', len(value))
4178 intStr, fracStr = value.split(self._decimalChar)
4179
4180 intStr = self._fields[0]._AdjustField(intStr)
4181 ## dbg('adjusted intStr: "%s"' % intStr)
4182 lenInt = len(intStr)
4183 fracStr = fracStr + ('0'*(lenFraction-len(fracStr))) # add trailing spaces to decimal
4184
4185 ## dbg('intStr "%(intStr)s"' % locals())
4186 ## dbg('lenInt:', lenInt)
4187
4188 intStr = string.rjust( intStr[-lenInt:], lenInt)
4189 ## dbg('right-justifed intStr = "%(intStr)s"' % locals())
4190 newvalue = intStr + self._decimalChar + fracStr
4191
4192 if self._signOk:
4193 if len(newvalue) < self._masklength:
4194 newvalue = ' ' + newvalue
4195 signedvalue = self._getSignedValue(newvalue)[0]
4196 if signedvalue is not None: newvalue = signedvalue
4197
4198 # Finally, align string with decimal position, left-padding with
4199 # fillChar:
4200 newdecpos = newvalue.find(self._decimalChar)
4201 if newdecpos < self._decimalpos:
4202 padlen = self._decimalpos - newdecpos
4203 newvalue = string.join([' ' * padlen] + [newvalue] ,'')
4204
4205 if self._signOk and self._useParens:
4206 if newvalue.find('(') != -1:
4207 newvalue = newvalue[:-1] + ')'
4208 else:
4209 newvalue = newvalue[:-1] + ' '
4210
4211 ## dbg('newvalue = "%s"' % newvalue)
4212 if candidate is None:
4213 wx.CallAfter(self._SetValue, newvalue)
4214 ## dbg(indent=0)
4215 return newvalue
4216
4217
4218 def _adjustInt(self, candidate=None):
4219 """ 'Fixes' an integer control. Collapses spaces, right or left-justifies."""
4220 ## dbg("MaskedEditMixin::_adjustInt", candidate)
4221 lenInt = self._masklength
4222 if candidate is None: value = self._GetValue()
4223 else: value = candidate
4224
4225 intStr = self._fields[0]._AdjustField(value)
4226 intStr = intStr.strip() # drop extra spaces
4227 ## dbg('adjusted field: "%s"' % intStr)
4228
4229 if self._isNeg and intStr.find('-') == -1 and intStr.find('(') == -1:
4230 if self._useParens:
4231 intStr = '(' + intStr + ')'
4232 else:
4233 intStr = '-' + intStr
4234 elif self._isNeg and intStr.find('-') != -1 and self._useParens:
4235 intStr = intStr.replace('-', '(')
4236
4237 if( self._signOk and ((self._useParens and intStr.find('(') == -1)
4238 or (not self._useParens and intStr.find('-') == -1))):
4239 intStr = ' ' + intStr
4240 if self._useParens:
4241 intStr += ' ' # space for right paren position
4242
4243 elif self._signOk and self._useParens and intStr.find('(') != -1 and intStr.find(')') == -1:
4244 # ensure closing right paren:
4245 intStr += ')'
4246
4247 if self._fields[0]._alignRight: ## Only if right-alignment is enabled
4248 intStr = intStr.rjust( lenInt )
4249 else:
4250 intStr = intStr.ljust( lenInt )
4251
4252 if candidate is None:
4253 wx.CallAfter(self._SetValue, intStr )
4254 return intStr
4255
4256
4257 def _adjustDate(self, candidate=None, fixcentury=False, force4digit_year=False):
4258 """
4259 'Fixes' a date control, expanding the year if it can.
4260 Applies various self-formatting options.
4261 """
4262 ## dbg("MaskedEditMixin::_adjustDate", indent=1)
4263 if candidate is None: text = self._GetValue()
4264 else: text = candidate
4265 ## dbg('text=', text)
4266 if self._datestyle == "YMD":
4267 year_field = 0
4268 else:
4269 year_field = 2
4270
4271 ## dbg('getYear: "%s"' % _getYear(text, self._datestyle))
4272 year = string.replace( _getYear( text, self._datestyle),self._fields[year_field]._fillChar,"") # drop extra fillChars
4273 month = _getMonth( text, self._datestyle)
4274 day = _getDay( text, self._datestyle)
4275 ## dbg('self._datestyle:', self._datestyle, 'year:', year, 'Month', month, 'day:', day)
4276
4277 yearVal = None
4278 yearstart = self._dateExtent - 4
4279 if( len(year) < 4
4280 and (fixcentury
4281 or force4digit_year
4282 or (self._GetInsertionPoint() > yearstart+1 and text[yearstart+2] == ' ')
4283 or (self._GetInsertionPoint() > yearstart+2 and text[yearstart+3] == ' ') ) ):
4284 ## user entered less than four digits and changing fields or past point where we could
4285 ## enter another digit:
4286 try:
4287 yearVal = int(year)
4288 except:
4289 ## dbg('bad year=', year)
4290 year = text[yearstart:self._dateExtent]
4291
4292 if len(year) < 4 and yearVal:
4293 if len(year) == 2:
4294 # Fix year adjustment to be less "20th century" :-) and to adjust heuristic as the
4295 # years pass...
4296 now = wx.DateTime_Now()
4297 century = (now.GetYear() /100) * 100 # "this century"
4298 twodig_year = now.GetYear() - century # "this year" (2 digits)
4299 # if separation between today's 2-digit year and typed value > 50,
4300 # assume last century,
4301 # else assume this century.
4302 #
4303 # Eg: if 2003 and yearVal == 30, => 2030
4304 # if 2055 and yearVal == 80, => 2080
4305 # if 2010 and yearVal == 96, => 1996
4306 #
4307 if abs(yearVal - twodig_year) > 50:
4308 yearVal = (century - 100) + yearVal
4309 else:
4310 yearVal = century + yearVal
4311 year = str( yearVal )
4312 else: # pad with 0's to make a 4-digit year
4313 year = "%04d" % yearVal
4314 if self._4digityear or force4digit_year:
4315 text = _makeDate(year, month, day, self._datestyle, text) + text[self._dateExtent:]
4316 ## dbg('newdate: "%s"' % text, indent=0)
4317 return text
4318
4319
4320 def _goEnd(self, getPosOnly=False):
4321 """ Moves the insertion point to the end of user-entry """
4322 ## dbg("MaskedEditMixin::_goEnd; getPosOnly:", getPosOnly, indent=1)
4323 text = self._GetValue()
4324 #### dbg('text: "%s"' % text)
4325 i = 0
4326 if len(text.rstrip()):
4327 for i in range( min( self._masklength-1, len(text.rstrip())), -1, -1):
4328 #### dbg('i:', i, 'self._isMaskChar(%d)' % i, self._isMaskChar(i))
4329 if self._isMaskChar(i):
4330 char = text[i]
4331 #### dbg("text[%d]: '%s'" % (i, char))
4332 if char != ' ':
4333 i += 1
4334 break
4335
4336 if i == 0:
4337 pos = self._goHome(getPosOnly=True)
4338 else:
4339 pos = min(i,self._masklength)
4340
4341 field = self._FindField(pos)
4342 start, end = field._extent
4343 if field._insertRight and pos < end:
4344 pos = end
4345 ## dbg('next pos:', pos)
4346 ## dbg(indent=0)
4347 if getPosOnly:
4348 return pos
4349 else:
4350 self._SetInsertionPoint(pos)
4351
4352
4353 def _goHome(self, getPosOnly=False):
4354 """ Moves the insertion point to the beginning of user-entry """
4355 ## dbg("MaskedEditMixin::_goHome; getPosOnly:", getPosOnly, indent=1)
4356 text = self._GetValue()
4357 for i in range(self._masklength):
4358 if self._isMaskChar(i):
4359 break
4360 pos = max(i, 0)
4361 ## dbg(indent=0)
4362 if getPosOnly:
4363 return pos
4364 else:
4365 self._SetInsertionPoint(max(i,0))
4366
4367
4368
4369 def _getAllowedChars(self, pos):
4370 """ Returns a string of all allowed user input characters for the provided
4371 mask character plus control options
4372 """
4373 maskChar = self.maskdict[pos]
4374 okchars = self.maskchardict[maskChar] ## entry, get mask approved characters
4375
4376 # convert okchars to unicode if required; will force subsequent appendings to
4377 # result in unicode strings
4378 if 'unicode' in wx.PlatformInfo and type(okchars) != types.UnicodeType:
4379 okchars = okchars.decode(self._defaultEncoding)
4380
4381 field = self._FindField(pos)
4382 if okchars and field._okSpaces: ## Allow spaces?
4383 okchars += " "
4384 if okchars and field._includeChars: ## any additional included characters?
4385 okchars += field._includeChars
4386 #### dbg('okchars[%d]:' % pos, okchars)
4387 return okchars
4388
4389
4390 def _isMaskChar(self, pos):
4391 """ Returns True if the char at position pos is a special mask character (e.g. NCXaA#)
4392 """
4393 if pos < self._masklength:
4394 return self.ismasked[pos]
4395 else:
4396 return False
4397
4398
4399 def _isTemplateChar(self,Pos):
4400 """ Returns True if the char at position pos is a template character (e.g. -not- NCXaA#)
4401 """
4402 if Pos < self._masklength:
4403 return not self._isMaskChar(Pos)
4404 else:
4405 return False
4406
4407
4408 def _isCharAllowed(self, char, pos, checkRegex=False, allowAutoSelect=True, ignoreInsertRight=False):
4409 """ Returns True if character is allowed at the specific position, otherwise False."""
4410 ## dbg('_isCharAllowed', char, pos, checkRegex, indent=1)
4411 field = self._FindField(pos)
4412 right_insert = False
4413
4414 if self.controlInitialized:
4415 sel_start, sel_to = self._GetSelection()
4416 else:
4417 sel_start, sel_to = pos, pos
4418
4419 if (field._insertRight or self._ctrl_constraints._insertRight) and not ignoreInsertRight:
4420 start, end = field._extent
4421 field_len = end - start
4422 if self.controlInitialized:
4423 value = self._GetValue()
4424 fstr = value[start:end].strip()
4425 if field._padZero:
4426 while fstr and fstr[0] == '0':
4427 fstr = fstr[1:]
4428 input_len = len(fstr)
4429 if self._signOk and '-' in fstr or '(' in fstr:
4430 input_len -= 1 # sign can move out of field, so don't consider it in length
4431 else:
4432 value = self._template
4433 input_len = 0 # can't get the current "value", so use 0
4434
4435
4436 # if entire field is selected or position is at end and field is not full,
4437 # or if allowed to right-insert at any point in field and field is not full and cursor is not at a fillChar
4438 # or the field is a singleton integer field and is currently 0 and we're at the end:
4439 if( (sel_start, sel_to) == field._extent
4440 or (pos == end and ((input_len < field_len)
4441 or (field_len == 1
4442 and input_len == field_len
4443 and field._isInt
4444 and value[end-1] == '0'
4445 )
4446 ) ) ):
4447 pos = end - 1
4448 ## dbg('pos = end - 1 = ', pos, 'right_insert? 1')
4449 right_insert = True
4450 elif( field._allowInsert and sel_start == sel_to
4451 and (sel_to == end or (sel_to < self._masklength and value[sel_start] != field._fillChar))
4452 and input_len < field_len ):
4453 pos = sel_to - 1 # where character will go
4454 ## dbg('pos = sel_to - 1 = ', pos, 'right_insert? 1')
4455 right_insert = True
4456 # else leave pos alone...
4457 else:
4458 ## dbg('pos stays ', pos, 'right_insert? 0')
4459 pass
4460
4461 if self._isTemplateChar( pos ): ## if a template character, return empty
4462 ## dbg('%d is a template character; returning False' % pos, indent=0)
4463 return False
4464
4465 if self._isMaskChar( pos ):
4466 okChars = self._getAllowedChars(pos)
4467
4468 if self._fields[0]._groupdigits and (self._isInt or (self._isFloat and pos < self._decimalpos)):
4469 okChars += self._fields[0]._groupChar
4470
4471 if self._signOk:
4472 if self._isInt or (self._isFloat and pos < self._decimalpos):
4473 okChars += '-'
4474 if self._useParens:
4475 okChars += '('
4476 elif self._useParens and (self._isInt or (self._isFloat and pos > self._decimalpos)):
4477 okChars += ')'
4478
4479 #### dbg('%s in %s?' % (char, okChars), char in okChars)
4480 approved = char in okChars
4481
4482 if approved and checkRegex:
4483 ## dbg("checking appropriate regex's")
4484 value = self._eraseSelection(self._GetValue())
4485 if right_insert:
4486 # move the position to the right side of the insertion:
4487 at = pos+1
4488 else:
4489 at = pos
4490 if allowAutoSelect:
4491 newvalue, ignore, ignore, ignore, ignore = self._insertKey(char, at, sel_start, sel_to, value, allowAutoSelect=True)
4492 else:
4493 newvalue, ignore = self._insertKey(char, at, sel_start, sel_to, value)
4494 ## dbg('newvalue: "%s"' % newvalue)
4495
4496 fields = [self._FindField(pos)] + [self._ctrl_constraints]
4497 for field in fields: # includes fields[-1] == "ctrl_constraints"
4498 if field._regexMask and field._filter:
4499 ## dbg('checking vs. regex')
4500 start, end = field._extent
4501 slice = newvalue[start:end]
4502 approved = (re.match( field._filter, slice) is not None)
4503 ## dbg('approved?', approved)
4504 if not approved: break
4505 ## dbg(indent=0)
4506 return approved
4507 else:
4508 ## dbg('%d is a !???! character; returning False', indent=0)
4509 return False
4510
4511
4512 def _applyFormatting(self):
4513 """ Apply formatting depending on the control's state.
4514 Need to find a way to call this whenever the value changes, in case the control's
4515 value has been changed or set programatically.
4516 """
4517 ## dbg(suspend=1)
4518 ## dbg('MaskedEditMixin::_applyFormatting', indent=1)
4519
4520 # Handle negative numbers
4521 if self._signOk:
4522 text, signpos, right_signpos = self._getSignedValue()
4523 ## dbg('text: "%s", signpos:' % text, signpos)
4524 if not text or text[signpos] not in ('-','('):
4525 self._isNeg = False
4526 ## dbg('no valid sign found; new sign:', self._isNeg)
4527 if text and signpos != self._signpos:
4528 self._signpos = signpos
4529 elif text and self._valid and not self._isNeg and text[signpos] in ('-', '('):
4530 ## dbg('setting _isNeg to True')
4531 self._isNeg = True
4532 ## dbg('self._isNeg:', self._isNeg)
4533
4534 if self._signOk and self._isNeg:
4535 fc = self._signedForegroundColour
4536 else:
4537 fc = self._foregroundColour
4538
4539 if hasattr(fc, '_name'):
4540 c =fc._name
4541 else:
4542 c = fc
4543 ## dbg('setting foreground to', c)
4544 self.SetForegroundColour(fc)
4545
4546 if self._valid:
4547 ## dbg('valid')
4548 if self.IsEmpty():
4549 bc = self._emptyBackgroundColour
4550 else:
4551 bc = self._validBackgroundColour
4552 else:
4553 ## dbg('invalid')
4554 bc = self._invalidBackgroundColour
4555 if hasattr(bc, '_name'):
4556 c =bc._name
4557 else:
4558 c = bc
4559 ## dbg('setting background to', c)
4560 self.SetBackgroundColour(bc)
4561 self._Refresh()
4562 ## dbg(indent=0, suspend=0)
4563
4564
4565 def _getAbsValue(self, candidate=None):
4566 """ Return an unsigned value (i.e. strip the '-' prefix if any), and sign position(s).
4567 """
4568 ## dbg('MaskedEditMixin::_getAbsValue; candidate="%s"' % candidate, indent=1)
4569 if candidate is None: text = self._GetValue()
4570 else: text = candidate
4571 right_signpos = text.find(')')
4572
4573 if self._isInt:
4574 if self._ctrl_constraints._alignRight and self._fields[0]._fillChar == ' ':
4575 signpos = text.find('-')
4576 if signpos == -1:
4577 ## dbg('no - found; searching for (')
4578 signpos = text.find('(')
4579 elif signpos != -1:
4580 ## dbg('- found at', signpos)
4581 pass
4582
4583 if signpos == -1:
4584 ## dbg('signpos still -1')
4585 ## dbg('len(%s) (%d) < len(%s) (%d)?' % (text, len(text), self._mask, self._masklength), len(text) < self._masklength)
4586 if len(text) < self._masklength:
4587 text = ' ' + text
4588 if len(text) < self._masklength:
4589 text += ' '
4590 if len(text) > self._masklength and text[-1] in (')', ' '):
4591 text = text[:-1]
4592 else:
4593 ## dbg('len(%s) (%d), len(%s) (%d)' % (text, len(text), self._mask, self._masklength))
4594 ## dbg('len(%s) - (len(%s) + 1):' % (text, text.lstrip()) , len(text) - (len(text.lstrip()) + 1))
4595 signpos = len(text) - (len(text.lstrip()) + 1)
4596
4597 if self._useParens and not text.strip():
4598 signpos -= 1 # empty value; use penultimate space
4599 ## dbg('signpos:', signpos)
4600 if signpos >= 0:
4601 text = text[:signpos] + ' ' + text[signpos+1:]
4602
4603 else:
4604 if self._signOk:
4605 signpos = 0
4606 text = self._template[0] + text[1:]
4607 else:
4608 signpos = -1
4609
4610 if right_signpos != -1:
4611 if self._signOk:
4612 text = text[:right_signpos] + ' ' + text[right_signpos+1:]
4613 elif len(text) > self._masklength:
4614 text = text[:right_signpos] + text[right_signpos+1:]
4615 right_signpos = -1
4616
4617
4618 elif self._useParens and self._signOk:
4619 # figure out where it ought to go:
4620 right_signpos = self._masklength - 1 # initial guess
4621 if not self._ctrl_constraints._alignRight:
4622 ## dbg('not right-aligned')
4623 if len(text.strip()) == 0:
4624 right_signpos = signpos + 1
4625 elif len(text.strip()) < self._masklength:
4626 right_signpos = len(text.rstrip())
4627 ## dbg('right_signpos:', right_signpos)
4628
4629 groupchar = self._fields[0]._groupChar
4630 try:
4631 value = long(text.replace(groupchar,'').replace('(','-').replace(')','').replace(' ', ''))
4632 except:
4633 ## dbg('invalid number', indent=0)
4634 return None, signpos, right_signpos
4635
4636 else: # float value
4637 try:
4638 groupchar = self._fields[0]._groupChar
4639 value = float(text.replace(groupchar,'').replace(self._decimalChar, '.').replace('(', '-').replace(')','').replace(' ', ''))
4640 ## dbg('value:', value)
4641 except:
4642 value = None
4643
4644 if value < 0 and value is not None:
4645 signpos = text.find('-')
4646 if signpos == -1:
4647 signpos = text.find('(')
4648
4649 text = text[:signpos] + self._template[signpos] + text[signpos+1:]
4650 else:
4651 # look forwards up to the decimal point for the 1st non-digit
4652 ## dbg('decimal pos:', self._decimalpos)
4653 ## dbg('text: "%s"' % text)
4654 if self._signOk:
4655 signpos = self._decimalpos - (len(text[:self._decimalpos].lstrip()) + 1)
4656 # prevent checking for empty string - Tomo - Wed 14 Jan 2004 03:19:09 PM CET
4657 if len(text) >= signpos+1 and text[signpos+1] in ('-','('):
4658 signpos += 1
4659 else:
4660 signpos = -1
4661 ## dbg('signpos:', signpos)
4662
4663 if self._useParens:
4664 if self._signOk:
4665 right_signpos = self._masklength - 1
4666 text = text[:right_signpos] + ' '
4667 if text[signpos] == '(':
4668 text = text[:signpos] + ' ' + text[signpos+1:]
4669 else:
4670 right_signpos = text.find(')')
4671 if right_signpos != -1:
4672 text = text[:-1]
4673 right_signpos = -1
4674
4675 if value is None:
4676 ## dbg('invalid number')
4677 text = None
4678
4679 ## dbg('abstext = "%s"' % text, 'signpos:', signpos, 'right_signpos:', right_signpos)
4680 ## dbg(indent=0)
4681 return text, signpos, right_signpos
4682
4683
4684 def _getSignedValue(self, candidate=None):
4685 """ Return a signed value by adding a "-" prefix if the value
4686 is set to negative, or a space if positive.
4687 """
4688 ## dbg('MaskedEditMixin::_getSignedValue; candidate="%s"' % candidate, indent=1)
4689 if candidate is None: text = self._GetValue()
4690 else: text = candidate
4691
4692
4693 abstext, signpos, right_signpos = self._getAbsValue(text)
4694 if self._signOk:
4695 if abstext is None:
4696 ## dbg(indent=0)
4697 return abstext, signpos, right_signpos
4698
4699 if self._isNeg or text[signpos] in ('-', '('):
4700 if self._useParens:
4701 sign = '('
4702 else:
4703 sign = '-'
4704 else:
4705 sign = ' '
4706 if abstext[signpos] not in string.digits:
4707 text = abstext[:signpos] + sign + abstext[signpos+1:]
4708 else:
4709 # this can happen if value passed is too big; sign assumed to be
4710 # in position 0, but if already filled with a digit, prepend sign...
4711 text = sign + abstext
4712 if self._useParens and text.find('(') != -1:
4713 text = text[:right_signpos] + ')' + text[right_signpos+1:]
4714 else:
4715 text = abstext
4716 ## dbg('signedtext = "%s"' % text, 'signpos:', signpos, 'right_signpos', right_signpos)
4717 ## dbg(indent=0)
4718 return text, signpos, right_signpos
4719
4720
4721 def GetPlainValue(self, candidate=None):
4722 """ Returns control's value stripped of the template text.
4723 plainvalue = MaskedEditMixin.GetPlainValue()
4724 """
4725 ## dbg('MaskedEditMixin::GetPlainValue; candidate="%s"' % candidate, indent=1)
4726
4727 if candidate is None: text = self._GetValue()
4728 else: text = candidate
4729
4730 if self.IsEmpty():
4731 ## dbg('returned ""', indent=0)
4732 return ""
4733 else:
4734 plain = ""
4735 for idx in range( min(len(self._template), len(text)) ):
4736 if self._mask[idx] in maskchars:
4737 plain += text[idx]
4738
4739 if self._isFloat or self._isInt:
4740 ## dbg('plain so far: "%s"' % plain)
4741 plain = plain.replace('(', '-').replace(')', ' ')
4742 ## dbg('plain after sign regularization: "%s"' % plain)
4743
4744 if self._signOk and self._isNeg and plain.count('-') == 0:
4745 # must be in reserved position; add to "plain value"
4746 plain = '-' + plain.strip()
4747
4748 if self._fields[0]._alignRight:
4749 lpad = plain.count(',')
4750 plain = ' ' * lpad + plain.replace(',','')
4751 else:
4752 plain = plain.replace(',','')
4753 ## dbg('plain after pad and group:"%s"' % plain)
4754
4755 ## dbg('returned "%s"' % plain.rstrip(), indent=0)
4756 return plain.rstrip()
4757
4758
4759 def IsEmpty(self, value=None):
4760 """
4761 Returns True if control is equal to an empty value.
4762 (Empty means all editable positions in the template == fillChar.)
4763 """
4764 if value is None: value = self._GetValue()
4765 if value == self._template and not self._defaultValue:
4766 #### dbg("IsEmpty? 1 (value == self._template and not self._defaultValue)")
4767 return True # (all mask chars == fillChar by defn)
4768 elif value == self._template:
4769 empty = True
4770 for pos in range(len(self._template)):
4771 #### dbg('isMaskChar(%(pos)d)?' % locals(), self._isMaskChar(pos))
4772 #### dbg('value[%(pos)d] != self._fillChar?' %locals(), value[pos] != self._fillChar[pos])
4773 if self._isMaskChar(pos) and value[pos] not in (' ', self._fillChar[pos]):
4774 empty = False
4775 #### dbg("IsEmpty? %(empty)d (do all mask chars == fillChar?)" % locals())
4776 return empty
4777 else:
4778 #### dbg("IsEmpty? 0 (value doesn't match template)")
4779 return False
4780
4781
4782 def IsDefault(self, value=None):
4783 """
4784 Returns True if the value specified (or the value of the control if not specified)
4785 is equal to the default value.
4786 """
4787 if value is None: value = self._GetValue()
4788 return value == self._template
4789
4790
4791 def IsValid(self, value=None):
4792 """ Indicates whether the value specified (or the current value of the control
4793 if not specified) is considered valid."""
4794 #### dbg('MaskedEditMixin::IsValid("%s")' % value, indent=1)
4795 if value is None: value = self._GetValue()
4796 ret = self._CheckValid(value)
4797 #### dbg(indent=0)
4798 return ret
4799
4800
4801 def _eraseSelection(self, value=None, sel_start=None, sel_to=None):
4802 """ Used to blank the selection when inserting a new character. """
4803 ## dbg("MaskedEditMixin::_eraseSelection", indent=1)
4804 if value is None: value = self._GetValue()
4805 if sel_start is None or sel_to is None:
4806 sel_start, sel_to = self._GetSelection() ## check for a range of selected text
4807 ## dbg('value: "%s"' % value)
4808 ## dbg("current sel_start, sel_to:", sel_start, sel_to)
4809
4810 newvalue = list(value)
4811 for i in range(sel_start, sel_to):
4812 if self._signOk and newvalue[i] in ('-', '(', ')'):
4813 ## dbg('found sign (%s) at' % newvalue[i], i)
4814
4815 # balance parentheses:
4816 if newvalue[i] == '(':
4817 right_signpos = value.find(')')
4818 if right_signpos != -1:
4819 newvalue[right_signpos] = ' '
4820
4821 elif newvalue[i] == ')':
4822 left_signpos = value.find('(')
4823 if left_signpos != -1:
4824 newvalue[left_signpos] = ' '
4825
4826 newvalue[i] = ' '
4827
4828 elif self._isMaskChar(i):
4829 field = self._FindField(i)
4830 if field._padZero:
4831 newvalue[i] = '0'
4832 else:
4833 newvalue[i] = self._template[i]
4834
4835 value = string.join(newvalue,"")
4836 ## dbg('new value: "%s"' % value)
4837 ## dbg(indent=0)
4838 return value
4839
4840
4841 def _insertKey(self, char, pos, sel_start, sel_to, value, allowAutoSelect=False):
4842 """ Handles replacement of the character at the current insertion point."""
4843 ## dbg('MaskedEditMixin::_insertKey', "\'" + char + "\'", pos, sel_start, sel_to, '"%s"' % value, indent=1)
4844
4845 text = self._eraseSelection(value)
4846 field = self._FindField(pos)
4847 start, end = field._extent
4848 newtext = ""
4849 newpos = pos
4850
4851 # if >= 2 chars selected in a right-insert field, do appropriate erase on field,
4852 # then set selection to end, and do usual right insert.
4853 if sel_start != sel_to and sel_to >= sel_start+2:
4854 field = self._FindField(sel_start)
4855 if( field._insertRight # if right-insert
4856 and field._allowInsert # and allow insert at any point in field
4857 and field == self._FindField(sel_to) ): # and selection all in same field
4858 text = self._OnErase(just_return_value=True) # remove selection before insert
4859 ## dbg('text after (left)erase: "%s"' % text)
4860 pos = sel_start = sel_to
4861
4862 if pos != sel_start and sel_start == sel_to:
4863 # adjustpos must have moved the position; make selection match:
4864 sel_start = sel_to = pos
4865
4866 ## dbg('field._insertRight?', field._insertRight)
4867 ## dbg('field._allowInsert?', field._allowInsert)
4868 ## dbg('sel_start, end', sel_start, end)
4869 if sel_start < end:
4870 ## dbg('text[sel_start] != field._fillChar?', text[sel_start] != field._fillChar)
4871 pass
4872
4873 if( field._insertRight # field allows right insert
4874 and ((sel_start, sel_to) == field._extent # and whole field selected
4875 or (sel_start == sel_to # or nothing selected
4876 and (sel_start == end # and cursor at right edge
4877 or (field._allowInsert # or field allows right-insert
4878 and sel_start < end # next to other char in field:
4879 and text[sel_start] != field._fillChar) ) ) ) ):
4880 ## dbg('insertRight')
4881 fstr = text[start:end]
4882 erasable_chars = [field._fillChar, ' ']
4883
4884 # if zero padding field, or a single digit, and currently a value of 0, allow erasure of 0:
4885 if field._padZero or (field._isInt and (end - start == 1) and fstr[0] == '0'):
4886 erasable_chars.append('0')
4887
4888 erased = ''
4889 #### dbg("fstr[0]:'%s'" % fstr[0])
4890 #### dbg('field_index:', field._index)
4891 #### dbg("fstr[0] in erasable_chars?", fstr[0] in erasable_chars)
4892 #### dbg("self._signOk and field._index == 0 and fstr[0] in ('-','(')?", self._signOk and field._index == 0 and fstr[0] in ('-','('))
4893 if fstr[0] in erasable_chars or (self._signOk and field._index == 0 and fstr[0] in ('-','(')):
4894 erased = fstr[0]
4895 #### dbg('value: "%s"' % text)
4896 #### dbg('fstr: "%s"' % fstr)
4897 #### dbg("erased: '%s'" % erased)
4898 field_sel_start = sel_start - start
4899 field_sel_to = sel_to - start
4900 ## dbg('left fstr: "%s"' % fstr[1:field_sel_start])
4901 ## dbg('right fstr: "%s"' % fstr[field_sel_to:end])
4902 fstr = fstr[1:field_sel_start] + char + fstr[field_sel_to:end]
4903 if field._alignRight and sel_start != sel_to:
4904 field_len = end - start
4905 ## pos += (field_len - len(fstr)) # move cursor right by deleted amount
4906 pos = sel_to
4907 ## dbg('setting pos to:', pos)
4908 if field._padZero:
4909 fstr = '0' * (field_len - len(fstr)) + fstr
4910 else:
4911 fstr = fstr.rjust(field_len) # adjust the field accordingly
4912 ## dbg('field str: "%s"' % fstr)
4913
4914 newtext = text[:start] + fstr + text[end:]
4915 if erased in ('-', '(') and self._signOk:
4916 newtext = erased + newtext[1:]
4917 ## dbg('newtext: "%s"' % newtext)
4918
4919 if self._signOk and field._index == 0:
4920 start -= 1 # account for sign position
4921
4922 #### dbg('field._moveOnFieldFull?', field._moveOnFieldFull)
4923 #### dbg('len(fstr.lstrip()) == end-start?', len(fstr.lstrip()) == end-start)
4924 if( field._moveOnFieldFull and pos == end
4925 and len(fstr.lstrip()) == end-start): # if field now full
4926 newpos = self._findNextEntry(end) # go to next field
4927 else:
4928 newpos = pos # else keep cursor at current position
4929
4930 if not newtext:
4931 ## dbg('not newtext')
4932 if newpos != pos:
4933 ## dbg('newpos:', newpos)
4934 pass
4935 if self._signOk and self._useParens:
4936 old_right_signpos = text.find(')')
4937
4938 if field._allowInsert and not field._insertRight and sel_to <= end and sel_start >= start:
4939 # inserting within a left-insert-capable field
4940 field_len = end - start
4941 before = text[start:sel_start]
4942 after = text[sel_to:end].strip()
4943 #### dbg("current field:'%s'" % text[start:end])
4944 #### dbg("before:'%s'" % before, "after:'%s'" % after)
4945 new_len = len(before) + len(after) + 1 # (for inserted char)
4946 #### dbg('new_len:', new_len)
4947
4948 if new_len < field_len:
4949 retained = after + self._template[end-(field_len-new_len):end]
4950 elif new_len > end-start:
4951 retained = after[1:]
4952 else:
4953 retained = after
4954
4955 left = text[0:start] + before
4956 #### dbg("left:'%s'" % left, "retained:'%s'" % retained)
4957 right = retained + text[end:]
4958 else:
4959 left = text[0:pos]
4960 right = text[pos+1:]
4961
4962 if 'unicode' in wx.PlatformInfo and type(char) != types.UnicodeType:
4963 # convert the keyboard constant to a unicode value, to
4964 # ensure it can be concatenated into the control value:
4965 char = char.decode(self._defaultEncoding)
4966
4967 newtext = left + char + right
4968
4969 if self._signOk and self._useParens:
4970 # Balance parentheses:
4971 left_signpos = newtext.find('(')
4972
4973 if left_signpos == -1: # erased '('; remove ')'
4974 right_signpos = newtext.find(')')
4975 if right_signpos != -1:
4976 newtext = newtext[:right_signpos] + ' ' + newtext[right_signpos+1:]
4977
4978 elif old_right_signpos != -1:
4979 right_signpos = newtext.find(')')
4980
4981 if right_signpos == -1: # just replaced right-paren
4982 if newtext[pos] == ' ': # we just erased '); erase '('
4983 newtext = newtext[:left_signpos] + ' ' + newtext[left_signpos+1:]
4984 else: # replaced with digit; move ') over
4985 if self._ctrl_constraints._alignRight or self._isFloat:
4986 newtext = newtext[:-1] + ')'
4987 else:
4988 rstripped_text = newtext.rstrip()
4989 right_signpos = len(rstripped_text)
4990 ## dbg('old_right_signpos:', old_right_signpos, 'right signpos now:', right_signpos)
4991 newtext = newtext[:right_signpos] + ')' + newtext[right_signpos+1:]
4992
4993 if( field._insertRight # if insert-right field (but we didn't start at right edge)
4994 and field._moveOnFieldFull # and should move cursor when full
4995 and len(newtext[start:end].strip()) == end-start): # and field now full
4996 newpos = self._findNextEntry(end) # go to next field
4997 ## dbg('newpos = nextentry =', newpos)
4998 else:
4999 ## dbg('pos:', pos, 'newpos:', pos+1)
5000 newpos = pos+1
5001
5002
5003 if allowAutoSelect:
5004 new_select_to = newpos # (default return values)
5005 match_field = None
5006 match_index = None
5007
5008 if field._autoSelect:
5009 match_index, partial_match = self._autoComplete(1, # (always forward)
5010 field._compareChoices,
5011 newtext[start:end],
5012 compareNoCase=field._compareNoCase,
5013 current_index = field._autoCompleteIndex-1)
5014 if match_index is not None and partial_match:
5015 matched_str = newtext[start:end]
5016 newtext = newtext[:start] + field._choices[match_index] + newtext[end:]
5017 new_select_to = end
5018 match_field = field
5019 if field._insertRight:
5020 # adjust position to just after partial match in field
5021 newpos = end - (len(field._choices[match_index].strip()) - len(matched_str.strip()))
5022
5023 elif self._ctrl_constraints._autoSelect:
5024 match_index, partial_match = self._autoComplete(
5025 1, # (always forward)
5026 self._ctrl_constraints._compareChoices,
5027 newtext,
5028 self._ctrl_constraints._compareNoCase,
5029 current_index = self._ctrl_constraints._autoCompleteIndex - 1)
5030 if match_index is not None and partial_match:
5031 matched_str = newtext
5032 newtext = self._ctrl_constraints._choices[match_index]
5033 edit_end = self._ctrl_constraints._extent[1]
5034 new_select_to = min(edit_end, len(newtext.rstrip()))
5035 match_field = self._ctrl_constraints
5036 if self._ctrl_constraints._insertRight:
5037 # adjust position to just after partial match in control:
5038 newpos = self._masklength - (len(self._ctrl_constraints._choices[match_index].strip()) - len(matched_str.strip()))
5039
5040 ## dbg('newtext: "%s"' % newtext, 'newpos:', newpos, 'new_select_to:', new_select_to)
5041 ## dbg(indent=0)
5042 return newtext, newpos, new_select_to, match_field, match_index
5043 else:
5044 ## dbg('newtext: "%s"' % newtext, 'newpos:', newpos)
5045 ## dbg(indent=0)
5046 return newtext, newpos
5047
5048
5049 def _OnFocus(self,event):
5050 """
5051 This event handler is currently necessary to work around new default
5052 behavior as of wxPython2.3.3;
5053 The TAB key auto selects the entire contents of the wx.TextCtrl *after*
5054 the EVT_SET_FOCUS event occurs; therefore we can't query/adjust the selection
5055 *here*, because it hasn't happened yet. So to prevent this behavior, and
5056 preserve the correct selection when the focus event is not due to tab,
5057 we need to pull the following trick:
5058 """
5059 ## dbg('MaskedEditMixin::_OnFocus')
5060 if self.IsBeingDeleted() or self.GetParent().IsBeingDeleted():
5061 return
5062 wx.CallAfter(self._fixSelection)
5063 event.Skip()
5064 self.Refresh()
5065
5066
5067 def _CheckValid(self, candidate=None):
5068 """
5069 This is the default validation checking routine; It verifies that the
5070 current value of the control is a "valid value," and has the side
5071 effect of coloring the control appropriately.
5072 """
5073 ## dbg(suspend=1)
5074 ## dbg('MaskedEditMixin::_CheckValid: candidate="%s"' % candidate, indent=1)
5075 oldValid = self._valid
5076 if candidate is None: value = self._GetValue()
5077 else: value = candidate
5078 ## dbg('value: "%s"' % value)
5079 oldvalue = value
5080 valid = True # assume True
5081
5082 if not self.IsDefault(value) and self._isDate: ## Date type validation
5083 valid = self._validateDate(value)
5084 ## dbg("valid date?", valid)
5085
5086 elif not self.IsDefault(value) and self._isTime:
5087 valid = self._validateTime(value)
5088 ## dbg("valid time?", valid)
5089
5090 elif not self.IsDefault(value) and (self._isInt or self._isFloat): ## Numeric type
5091 valid = self._validateNumeric(value)
5092 ## dbg("valid Number?", valid)
5093
5094 if valid: # and not self.IsDefault(value): ## generic validation accounts for IsDefault()
5095 ## valid so far; ensure also allowed by any list or regex provided:
5096 valid = self._validateGeneric(value)
5097 ## dbg("valid value?", valid)
5098
5099 ## dbg('valid?', valid)
5100
5101 if not candidate:
5102 self._valid = valid
5103 self._applyFormatting()
5104 if self._valid != oldValid:
5105 ## dbg('validity changed: oldValid =',oldValid,'newvalid =', self._valid)
5106 ## dbg('oldvalue: "%s"' % oldvalue, 'newvalue: "%s"' % self._GetValue())
5107 pass
5108 ## dbg(indent=0, suspend=0)
5109 return valid
5110
5111
5112 def _validateGeneric(self, candidate=None):
5113 """ Validate the current value using the provided list or Regex filter (if any).
5114 """
5115 if candidate is None:
5116 text = self._GetValue()
5117 else:
5118 text = candidate
5119
5120 valid = True # assume True
5121 for i in [-1] + self._field_indices: # process global constraints first:
5122 field = self._fields[i]
5123 start, end = field._extent
5124 slice = text[start:end]
5125 valid = field.IsValid(slice)
5126 if not valid:
5127 break
5128
5129 return valid
5130
5131
5132 def _validateNumeric(self, candidate=None):
5133 """ Validate that the value is within the specified range (if specified.)"""
5134 if candidate is None: value = self._GetValue()
5135 else: value = candidate
5136 try:
5137 groupchar = self._fields[0]._groupChar
5138 if self._isFloat:
5139 number = float(value.replace(groupchar, '').replace(self._decimalChar, '.').replace('(', '-').replace(')', ''))
5140 else:
5141 number = long( value.replace(groupchar, '').replace('(', '-').replace(')', ''))
5142 if value.strip():
5143 if self._fields[0]._alignRight:
5144 require_digit_at = self._fields[0]._extent[1]-1
5145 else:
5146 require_digit_at = self._fields[0]._extent[0]
5147 ## dbg('require_digit_at:', require_digit_at)
5148 ## dbg("value[rda]: '%s'" % value[require_digit_at])
5149 if value[require_digit_at] not in list(string.digits):
5150 valid = False
5151 return valid
5152 # else...
5153 ## dbg('number:', number)
5154 if self._ctrl_constraints._hasRange:
5155 valid = self._ctrl_constraints._rangeLow <= number <= self._ctrl_constraints._rangeHigh
5156 else:
5157 valid = True
5158 groupcharpos = value.rfind(groupchar)
5159 if groupcharpos != -1: # group char present
5160 ## dbg('groupchar found at', groupcharpos)
5161 if self._isFloat and groupcharpos > self._decimalpos:
5162 # 1st one found on right-hand side is past decimal point
5163 ## dbg('groupchar in fraction; illegal')
5164 return False
5165 elif self._isFloat:
5166 integer = value[:self._decimalpos].strip()
5167 else:
5168 integer = value.strip()
5169 ## dbg("integer:'%s'" % integer)
5170 if integer[0] in ('-', '('):
5171 integer = integer[1:]
5172 if integer[-1] == ')':
5173 integer = integer[:-1]
5174
5175 parts = integer.split(groupchar)
5176 ## dbg('parts:', parts)
5177 for i in range(len(parts)):
5178 if i == 0 and abs(int(parts[0])) > 999:
5179 ## dbg('group 0 too long; illegal')
5180 valid = False
5181 break
5182 elif i > 0 and (len(parts[i]) != 3 or ' ' in parts[i]):
5183 ## dbg('group %i (%s) not right size; illegal' % (i, parts[i]))
5184 valid = False
5185 break
5186 except ValueError:
5187 ## dbg('value not a valid number')
5188 valid = False
5189 return valid
5190
5191
5192 def _validateDate(self, candidate=None):
5193 """ Validate the current date value using the provided Regex filter.
5194 Generally used for character types.BufferType
5195 """
5196 ## dbg('MaskedEditMixin::_validateDate', indent=1)
5197 if candidate is None: value = self._GetValue()
5198 else: value = candidate
5199 ## dbg('value = "%s"' % value)
5200 text = self._adjustDate(value, force4digit_year=True) ## Fix the date up before validating it
5201 ## dbg('text =', text)
5202 valid = True # assume True until proven otherwise
5203
5204 try:
5205 # replace fillChar in each field with space:
5206 datestr = text[0:self._dateExtent]
5207 for i in range(3):
5208 field = self._fields[i]
5209 start, end = field._extent
5210 fstr = datestr[start:end]
5211 fstr.replace(field._fillChar, ' ')
5212 datestr = datestr[:start] + fstr + datestr[end:]
5213
5214 year, month, day = _getDateParts( datestr, self._datestyle)
5215 year = int(year)
5216 ## dbg('self._dateExtent:', self._dateExtent)
5217 if self._dateExtent == 11:
5218 month = charmonths_dict[month.lower()]
5219 else:
5220 month = int(month)
5221 day = int(day)
5222 ## dbg('year, month, day:', year, month, day)
5223
5224 except ValueError:
5225 ## dbg('cannot convert string to integer parts')
5226 valid = False
5227 except KeyError:
5228 ## dbg('cannot convert string to integer month')
5229 valid = False
5230
5231 if valid:
5232 # use wxDateTime to unambiguously try to parse the date:
5233 # ### Note: because wxDateTime is *brain-dead* and expects months 0-11,
5234 # rather than 1-12, so handle accordingly:
5235 if month > 12:
5236 valid = False
5237 else:
5238 month -= 1
5239 try:
5240 ## dbg("trying to create date from values day=%d, month=%d, year=%d" % (day,month,year))
5241 dateHandler = wx.DateTimeFromDMY(day,month,year)
5242 ## dbg("succeeded")
5243 dateOk = True
5244 except:
5245 ## dbg('cannot convert string to valid date')
5246 dateOk = False
5247 if not dateOk:
5248 valid = False
5249
5250 if valid:
5251 # wxDateTime doesn't take kindly to leading/trailing spaces when parsing,
5252 # so we eliminate them here:
5253 timeStr = text[self._dateExtent+1:].strip() ## time portion of the string
5254 if timeStr:
5255 ## dbg('timeStr: "%s"' % timeStr)
5256 try:
5257 checkTime = dateHandler.ParseTime(timeStr)
5258 valid = checkTime == len(timeStr)
5259 except:
5260 valid = False
5261 if not valid:
5262 ## dbg('cannot convert string to valid time')
5263 pass
5264 ## if valid: dbg('valid date')
5265 ## dbg(indent=0)
5266 return valid
5267
5268
5269 def _validateTime(self, candidate=None):
5270 """ Validate the current time value using the provided Regex filter.
5271 Generally used for character types.BufferType
5272 """
5273 ## dbg('MaskedEditMixin::_validateTime', indent=1)
5274 # wxDateTime doesn't take kindly to leading/trailing spaces when parsing,
5275 # so we eliminate them here:
5276 if candidate is None: value = self._GetValue().strip()
5277 else: value = candidate.strip()
5278 ## dbg('value = "%s"' % value)
5279 valid = True # assume True until proven otherwise
5280
5281 dateHandler = wx.DateTime_Today()
5282 try:
5283 checkTime = dateHandler.ParseTime(value)
5284 ## dbg('checkTime:', checkTime, 'len(value)', len(value))
5285 valid = checkTime == len(value)
5286 except:
5287 valid = False
5288
5289 if not valid:
5290 ## dbg('cannot convert string to valid time')
5291 pass
5292 ## if valid: dbg('valid time')
5293 ## dbg(indent=0)
5294 return valid
5295
5296
5297 def _OnKillFocus(self,event):
5298 """ Handler for EVT_KILL_FOCUS event.
5299 """
5300 ## dbg('MaskedEditMixin::_OnKillFocus', 'isDate=',self._isDate, indent=1)
5301 if self._mask and self._IsEditable():
5302 self._AdjustField(self._GetInsertionPoint())
5303 self._CheckValid() ## Call valid handler
5304
5305 self._LostFocus() ## Provided for subclass use
5306 event.Skip()
5307 ## dbg(indent=0)
5308
5309
5310 def _fixSelection(self):
5311 """
5312 This gets called after the TAB traversal selection is made, if the
5313 focus event was due to this, but before the EVT_LEFT_* events if
5314 the focus shift was due to a mouse event.
5315
5316 The trouble is that, a priori, there's no explicit notification of
5317 why the focus event we received. However, the whole reason we need to
5318 do this is because the default behavior on TAB traveral in a wx.TextCtrl is
5319 now to select the entire contents of the window, something we don't want.
5320 So we can *now* test the selection range, and if it's "the whole text"
5321 we can assume the cause, change the insertion point to the start of
5322 the control, and deselect.
5323 """
5324 ## dbg('MaskedEditMixin::_fixSelection', indent=1)
5325 # can get here if called with wx.CallAfter after underlying
5326 # control has been destroyed on close, but after focus
5327 # events
5328 if not self or not self._mask or not self._IsEditable():
5329 ## dbg(indent=0)
5330 return
5331
5332 sel_start, sel_to = self._GetSelection()
5333 ## dbg('sel_start, sel_to:', sel_start, sel_to, 'self.IsEmpty()?', self.IsEmpty())
5334
5335 if( sel_start == 0 and sel_to >= len( self._mask ) #(can be greater in numeric controls because of reserved space)
5336 and (not self._ctrl_constraints._autoSelect or self.IsEmpty() or self.IsDefault() ) ):
5337 # This isn't normally allowed, and so assume we got here by the new
5338 # "tab traversal" behavior, so we need to reset the selection
5339 # and insertion point:
5340 ## dbg('entire text selected; resetting selection to start of control')
5341 self._goHome()
5342 field = self._FindField(self._GetInsertionPoint())
5343 edit_start, edit_end = field._extent
5344 if field._selectOnFieldEntry:
5345 self._SetInsertionPoint(edit_start)
5346 self._SetSelection(edit_start, edit_end)
5347
5348 elif field._insertRight:
5349 self._SetInsertionPoint(edit_end)
5350 self._SetSelection(edit_end, edit_end)
5351
5352 elif (self._isFloat or self._isInt):
5353
5354 text, signpos, right_signpos = self._getAbsValue()
5355 if text is None or text == self._template:
5356 integer = self._fields[0]
5357 edit_start, edit_end = integer._extent
5358
5359 if integer._selectOnFieldEntry:
5360 ## dbg('select on field entry:')
5361 self._SetInsertionPoint(edit_start)
5362 self._SetSelection(edit_start, edit_end)
5363
5364 elif integer._insertRight:
5365 ## dbg('moving insertion point to end')
5366 self._SetInsertionPoint(edit_end)
5367 self._SetSelection(edit_end, edit_end)
5368 else:
5369 ## dbg('numeric ctrl is empty; start at beginning after sign')
5370 self._SetInsertionPoint(signpos+1) ## Move past minus sign space if signed
5371 self._SetSelection(signpos+1, signpos+1)
5372
5373 elif sel_start > self._goEnd(getPosOnly=True):
5374 ## dbg('cursor beyond the end of the user input; go to end of it')
5375 self._goEnd()
5376 else:
5377 ## dbg('sel_start, sel_to:', sel_start, sel_to, 'self._masklength:', self._masklength)
5378 pass
5379 ## dbg(indent=0)
5380
5381
5382 def _Keypress(self,key):
5383 """ Method provided to override OnChar routine. Return False to force
5384 a skip of the 'normal' OnChar process. Called before class OnChar.
5385 """
5386 return True
5387
5388
5389 def _LostFocus(self):
5390 """ Method provided for subclasses. _LostFocus() is called after
5391 the class processes its EVT_KILL_FOCUS event code.
5392 """
5393 pass
5394
5395
5396 def _OnDoubleClick(self, event):
5397 """ selects field under cursor on dclick."""
5398 pos = self._GetInsertionPoint()
5399 field = self._FindField(pos)
5400 start, end = field._extent
5401 self._SetInsertionPoint(start)
5402 self._SetSelection(start, end)
5403
5404
5405 def _Change(self):
5406 """ Method provided for subclasses. Called by internal EVT_TEXT
5407 handler. Return False to override the class handler, True otherwise.
5408 """
5409 return True
5410
5411
5412 def _Cut(self):
5413 """
5414 Used to override the default Cut() method in base controls, instead
5415 copying the selection to the clipboard and then blanking the selection,
5416 leaving only the mask in the selected area behind.
5417 Note: _Cut (read "undercut" ;-) must be called from a Cut() override in the
5418 derived control because the mixin functions can't override a method of
5419 a sibling class.
5420 """
5421 ## dbg("MaskedEditMixin::_Cut", indent=1)
5422 value = self._GetValue()
5423 ## dbg('current value: "%s"' % value)
5424 sel_start, sel_to = self._GetSelection() ## check for a range of selected text
5425 ## dbg('selected text: "%s"' % value[sel_start:sel_to].strip())
5426 do = wx.TextDataObject()
5427 do.SetText(value[sel_start:sel_to].strip())
5428 wx.TheClipboard.Open()
5429 wx.TheClipboard.SetData(do)
5430 wx.TheClipboard.Close()
5431
5432 if sel_to - sel_start != 0:
5433 self._OnErase()
5434 ## dbg(indent=0)
5435
5436
5437 # WS Note: overriding Copy is no longer necessary given that you
5438 # can no longer select beyond the last non-empty char in the control.
5439 #
5440 ## def _Copy( self ):
5441 ## """
5442 ## Override the wx.TextCtrl's .Copy function, with our own
5443 ## that does validation. Need to strip trailing spaces.
5444 ## """
5445 ## sel_start, sel_to = self._GetSelection()
5446 ## select_len = sel_to - sel_start
5447 ## textval = wx.TextCtrl._GetValue(self)
5448 ##
5449 ## do = wx.TextDataObject()
5450 ## do.SetText(textval[sel_start:sel_to].strip())
5451 ## wx.TheClipboard.Open()
5452 ## wx.TheClipboard.SetData(do)
5453 ## wx.TheClipboard.Close()
5454
5455
5456 def _getClipboardContents( self ):
5457 """ Subroutine for getting the current contents of the clipboard.
5458 """
5459 do = wx.TextDataObject()
5460 wx.TheClipboard.Open()
5461 success = wx.TheClipboard.GetData(do)
5462 wx.TheClipboard.Close()
5463
5464 if not success:
5465 return None
5466 else:
5467 # Remove leading and trailing spaces before evaluating contents
5468 return do.GetText().strip()
5469
5470
5471 def _validatePaste(self, paste_text, sel_start, sel_to, raise_on_invalid=False):
5472 """
5473 Used by paste routine and field choice validation to see
5474 if a given slice of paste text is legal for the area in question:
5475 returns validity, replacement text, and extent of paste in
5476 template.
5477 """
5478 ## dbg(suspend=1)
5479 ## dbg('MaskedEditMixin::_validatePaste("%(paste_text)s", %(sel_start)d, %(sel_to)d), raise_on_invalid? %(raise_on_invalid)d' % locals(), indent=1)
5480 select_length = sel_to - sel_start
5481 maxlength = select_length
5482 ## dbg('sel_to - sel_start:', maxlength)
5483 if maxlength == 0:
5484 maxlength = self._masklength - sel_start
5485 item = 'control'
5486 else:
5487 item = 'selection'
5488 ## dbg('maxlength:', maxlength)
5489 if 'unicode' in wx.PlatformInfo and type(paste_text) != types.UnicodeType:
5490 paste_text = paste_text.decode(self._defaultEncoding)
5491
5492 length_considered = len(paste_text)
5493 if length_considered > maxlength:
5494 ## dbg('paste text will not fit into the %s:' % item, indent=0)
5495 if raise_on_invalid:
5496 ## dbg(indent=0, suspend=0)
5497 if item == 'control':
5498 raise ValueError('"%s" will not fit into the control "%s"' % (paste_text, self.name))
5499 else:
5500 raise ValueError('"%s" will not fit into the selection' % paste_text)
5501 else:
5502 ## dbg(indent=0, suspend=0)
5503 return False, None, None
5504
5505 text = self._template
5506 ## dbg('length_considered:', length_considered)
5507
5508 valid_paste = True
5509 replacement_text = ""
5510 replace_to = sel_start
5511 i = 0
5512 while valid_paste and i < length_considered and replace_to < self._masklength:
5513 if paste_text[i:] == self._template[replace_to:length_considered]:
5514 # remainder of paste matches template; skip char-by-char analysis
5515 ## dbg('remainder paste_text[%d:] (%s) matches template[%d:%d]' % (i, paste_text[i:], replace_to, length_considered))
5516 replacement_text += paste_text[i:]
5517 replace_to = i = length_considered
5518 continue
5519 # else:
5520 char = paste_text[i]
5521 field = self._FindField(replace_to)
5522 if not field._compareNoCase:
5523 if field._forceupper: char = char.upper()
5524 elif field._forcelower: char = char.lower()
5525
5526 ## dbg('char:', "'"+char+"'", 'i =', i, 'replace_to =', replace_to)
5527 ## dbg('self._isTemplateChar(%d)?' % replace_to, self._isTemplateChar(replace_to))
5528 if not self._isTemplateChar(replace_to) and self._isCharAllowed( char, replace_to, allowAutoSelect=False, ignoreInsertRight=True):
5529 replacement_text += char
5530 ## dbg("not template(%(replace_to)d) and charAllowed('%(char)s',%(replace_to)d)" % locals())
5531 ## dbg("replacement_text:", '"'+replacement_text+'"')
5532 i += 1
5533 replace_to += 1
5534 elif( char == self._template[replace_to]
5535 or (self._signOk and
5536 ( (i == 0 and (char == '-' or (self._useParens and char == '(')))
5537 or (i == self._masklength - 1 and self._useParens and char == ')') ) ) ):
5538 replacement_text += char
5539 ## dbg("'%(char)s' == template(%(replace_to)d)" % locals())
5540 ## dbg("replacement_text:", '"'+replacement_text+'"')
5541 i += 1
5542 replace_to += 1
5543 else:
5544 next_entry = self._findNextEntry(replace_to, adjustInsert=False)
5545 if next_entry == replace_to:
5546 valid_paste = False
5547 else:
5548 replacement_text += self._template[replace_to:next_entry]
5549 ## dbg("skipping template; next_entry =", next_entry)
5550 ## dbg("replacement_text:", '"'+replacement_text+'"')
5551 replace_to = next_entry # so next_entry will be considered on next loop
5552
5553 if not valid_paste and raise_on_invalid:
5554 ## dbg('raising exception', indent=0, suspend=0)
5555 raise ValueError('"%s" cannot be inserted into the control "%s"' % (paste_text, self.name))
5556
5557 elif i < len(paste_text):
5558 valid_paste = False
5559 if raise_on_invalid:
5560 ## dbg('raising exception', indent=0, suspend=0)
5561 raise ValueError('"%s" will not fit into the control "%s"' % (paste_text, self.name))
5562
5563 ## dbg('valid_paste?', valid_paste)
5564 if valid_paste:
5565 ## dbg('replacement_text: "%s"' % replacement_text, 'replace to:', replace_to)
5566 pass
5567 ## dbg(indent=0, suspend=0)
5568 return valid_paste, replacement_text, replace_to
5569
5570
5571 def _Paste( self, value=None, raise_on_invalid=False, just_return_value=False ):
5572 """
5573 Used to override the base control's .Paste() function,
5574 with our own that does validation.
5575 Note: _Paste must be called from a Paste() override in the
5576 derived control because the mixin functions can't override a
5577 method of a sibling class.
5578 """
5579 ## dbg('MaskedEditMixin::_Paste (value = "%s")' % value, indent=1)
5580 if value is None:
5581 paste_text = self._getClipboardContents()
5582 else:
5583 paste_text = value
5584
5585 if paste_text is not None:
5586
5587 if 'unicode' in wx.PlatformInfo and type(paste_text) != types.UnicodeType:
5588 paste_text = paste_text.decode(self._defaultEncoding)
5589
5590 ## dbg('paste text: "%s"' % paste_text)
5591 # (conversion will raise ValueError if paste isn't legal)
5592 sel_start, sel_to = self._GetSelection()
5593 ## dbg('selection:', (sel_start, sel_to))
5594
5595 # special case: handle allowInsert fields properly
5596 field = self._FindField(sel_start)
5597 edit_start, edit_end = field._extent
5598 new_pos = None
5599 if field._allowInsert and sel_to <= edit_end and (sel_start + len(paste_text) < edit_end or field._insertRight):
5600 if field._insertRight:
5601 # want to paste to the left; see if it will fit:
5602 left_text = self._GetValue()[edit_start:sel_start].lstrip()
5603 ## dbg('len(left_text):', len(left_text))
5604 ## dbg('len(paste_text):', len(paste_text))
5605 ## dbg('sel_start - (len(left_text) + len(paste_text)) >= edit_start?', sel_start - (len(left_text) + len(paste_text)) >= edit_start)
5606 if sel_start - (len(left_text) - (sel_to - sel_start) + len(paste_text)) >= edit_start:
5607 # will fit! create effective paste text, and move cursor back to do so:
5608 paste_text = left_text + paste_text
5609 sel_start -= len(left_text)
5610 paste_text = paste_text.rjust(sel_to - sel_start)
5611 ## dbg('modified paste_text to be: "%s"' % paste_text)
5612 ## dbg('modified selection to:', (sel_start, sel_to))
5613 else:
5614 ## dbg("won't fit left;", 'paste text remains: "%s"' % paste_text)
5615 pass
5616 else:
5617 paste_text = paste_text + self._GetValue()[sel_to:edit_end].rstrip()
5618 ## dbg("allow insert, but not insert right;", 'paste text set to: "%s"' % paste_text)
5619
5620
5621 new_pos = sel_start + len(paste_text) # store for subsequent positioning
5622 ## dbg('paste within insertable field; adjusted paste_text: "%s"' % paste_text, 'end:', edit_end)
5623 ## dbg('expanded selection to:', (sel_start, sel_to))
5624
5625 # Another special case: paste won't fit, but it's a right-insert field where entire
5626 # non-empty value is selected, and there's room if the selection is expanded leftward:
5627 if( len(paste_text) > sel_to - sel_start
5628 and field._insertRight
5629 and sel_start > edit_start
5630 and sel_to >= edit_end
5631 and not self._GetValue()[edit_start:sel_start].strip() ):
5632 # text won't fit within selection, but left of selection is empty;
5633 # check to see if we can expand selection to accommodate the value:
5634 empty_space = sel_start - edit_start
5635 amount_needed = len(paste_text) - (sel_to - sel_start)
5636 if amount_needed <= empty_space:
5637 sel_start -= amount_needed
5638 ## dbg('expanded selection to:', (sel_start, sel_to))
5639
5640
5641 # another special case: deal with signed values properly:
5642 if self._signOk:
5643 signedvalue, signpos, right_signpos = self._getSignedValue()
5644 paste_signpos = paste_text.find('-')
5645 if paste_signpos == -1:
5646 paste_signpos = paste_text.find('(')
5647
5648 # if paste text will result in signed value:
5649 #### dbg('paste_signpos != -1?', paste_signpos != -1)
5650 #### dbg('sel_start:', sel_start, 'signpos:', signpos)
5651 #### dbg('field._insertRight?', field._insertRight)
5652 #### dbg('sel_start - len(paste_text) >= signpos?', sel_start - len(paste_text) <= signpos)
5653 if paste_signpos != -1 and (sel_start <= signpos
5654 or (field._insertRight and sel_start - len(paste_text) <= signpos)):
5655 signed = True
5656 else:
5657 signed = False
5658 # remove "sign" from paste text, so we can auto-adjust for sign type after paste:
5659 paste_text = paste_text.replace('-', ' ').replace('(',' ').replace(')','')
5660 ## dbg('unsigned paste text: "%s"' % paste_text)
5661 else:
5662 signed = False
5663
5664 # another special case: deal with insert-right fields when selection is empty and
5665 # cursor is at end of field:
5666 #### dbg('field._insertRight?', field._insertRight)
5667 #### dbg('sel_start == edit_end?', sel_start == edit_end)
5668 #### dbg('sel_start', sel_start, 'sel_to', sel_to)
5669 if field._insertRight and sel_start == edit_end and sel_start == sel_to:
5670 sel_start -= len(paste_text)
5671 if sel_start < 0:
5672 sel_start = 0
5673 ## dbg('adjusted selection:', (sel_start, sel_to))
5674
5675 try:
5676 valid_paste, replacement_text, replace_to = self._validatePaste(paste_text, sel_start, sel_to, raise_on_invalid)
5677 except:
5678 ## dbg('exception thrown', indent=0)
5679 raise
5680
5681 if not valid_paste:
5682 ## dbg('paste text not legal for the selection or portion of the control following the cursor;')
5683 if not wx.Validator_IsSilent():
5684 wx.Bell()
5685 ## dbg(indent=0)
5686 return None, -1
5687 # else...
5688 text = self._eraseSelection()
5689
5690 new_text = text[:sel_start] + replacement_text + text[replace_to:]
5691 if new_text:
5692 new_text = string.ljust(new_text,self._masklength)
5693 if signed:
5694 new_text, signpos, right_signpos = self._getSignedValue(candidate=new_text)
5695 if new_text:
5696 if self._useParens:
5697 new_text = new_text[:signpos] + '(' + new_text[signpos+1:right_signpos] + ')' + new_text[right_signpos+1:]
5698 else:
5699 new_text = new_text[:signpos] + '-' + new_text[signpos+1:]
5700 if not self._isNeg:
5701 self._isNeg = 1
5702
5703 ## dbg("new_text:", '"'+new_text+'"')
5704
5705 if not just_return_value:
5706 if new_text != self._GetValue():
5707 self.modified = True
5708 if new_text == '':
5709 self.ClearValue()
5710 else:
5711 wx.CallAfter(self._SetValue, new_text)
5712 if new_pos is None:
5713 new_pos = sel_start + len(replacement_text)
5714 wx.CallAfter(self._SetInsertionPoint, new_pos)
5715 else:
5716 ## dbg(indent=0)
5717 return new_text, replace_to
5718 elif just_return_value:
5719 ## dbg(indent=0)
5720 return self._GetValue(), sel_to
5721 ## dbg(indent=0)
5722
5723 def _Undo(self, value=None, prev=None, just_return_results=False):
5724 """ Provides an Undo() method in base controls. """
5725 ## dbg("MaskedEditMixin::_Undo", indent=1)
5726 if value is None:
5727 value = self._GetValue()
5728 if prev is None:
5729 prev = self._prevValue
5730 ## dbg('current value: "%s"' % value)
5731 ## dbg('previous value: "%s"' % prev)
5732 if prev is None:
5733 ## dbg('no previous value', indent=0)
5734 return
5735
5736 elif value != prev:
5737 # Determine what to select: (relies on fixed-length strings)
5738 # (This is a lot harder than it would first appear, because
5739 # of mask chars that stay fixed, and so break up the "diff"...)
5740
5741 # Determine where they start to differ:
5742 i = 0
5743 length = len(value) # (both are same length in masked control)
5744
5745 while( value[:i] == prev[:i] ):
5746 i += 1
5747 sel_start = i - 1
5748
5749
5750 # handle signed values carefully, so undo from signed to unsigned or vice-versa
5751 # works properly:
5752 if self._signOk:
5753 text, signpos, right_signpos = self._getSignedValue(candidate=prev)
5754 if self._useParens:
5755 if prev[signpos] == '(' and prev[right_signpos] == ')':
5756 self._isNeg = True
5757 else:
5758 self._isNeg = False
5759 # eliminate source of "far-end" undo difference if using balanced parens:
5760 value = value.replace(')', ' ')
5761 prev = prev.replace(')', ' ')
5762 elif prev[signpos] == '-':
5763 self._isNeg = True
5764 else:
5765 self._isNeg = False
5766
5767 # Determine where they stop differing in "undo" result:
5768 sm = difflib.SequenceMatcher(None, a=value, b=prev)
5769 i, j, k = sm.find_longest_match(sel_start, length, sel_start, length)
5770 ## dbg('i,j,k = ', (i,j,k), 'value[i:i+k] = "%s"' % value[i:i+k], 'prev[j:j+k] = "%s"' % prev[j:j+k] )
5771
5772 if k == 0: # no match found; select to end
5773 sel_to = length
5774 else:
5775 code_5tuples = sm.get_opcodes()
5776 for op, i1, i2, j1, j2 in code_5tuples:
5777 ## dbg("%7s value[%d:%d] (%s) prev[%d:%d] (%s)" % (op, i1, i2, value[i1:i2], j1, j2, prev[j1:j2]))
5778 pass
5779
5780 diff_found = False
5781 # look backward through operations needed to produce "previous" value;
5782 # first change wins:
5783 for next_op in range(len(code_5tuples)-1, -1, -1):
5784 op, i1, i2, j1, j2 = code_5tuples[next_op]
5785 ## dbg('value[i1:i2]: "%s"' % value[i1:i2], 'template[i1:i2] "%s"' % self._template[i1:i2])
5786 field = self._FindField(i2)
5787 if op == 'insert' and prev[j1:j2] != self._template[j1:j2]:
5788 ## dbg('insert found: selection =>', (j1, j2))
5789 sel_start = j1
5790 sel_to = j2
5791 diff_found = True
5792 break
5793 elif op == 'delete' and value[i1:i2] != self._template[i1:i2]:
5794 edit_start, edit_end = field._extent
5795 if field._insertRight and (field._allowInsert or i2 == edit_end):
5796 sel_start = i2
5797 sel_to = i2
5798 else:
5799 sel_start = i1
5800 sel_to = j1
5801 ## dbg('delete found: selection =>', (sel_start, sel_to))
5802 diff_found = True
5803 break
5804 elif op == 'replace':
5805 if not prev[i1:i2].strip() and field._insertRight:
5806 sel_start = sel_to = j2
5807 else:
5808 sel_start = j1
5809 sel_to = j2
5810 ## dbg('replace found: selection =>', (sel_start, sel_to))
5811 diff_found = True
5812 break
5813
5814
5815 if diff_found:
5816 # now go forwards, looking for earlier changes:
5817 ## dbg('searching forward...')
5818 for next_op in range(len(code_5tuples)):
5819 op, i1, i2, j1, j2 = code_5tuples[next_op]
5820 field = self._FindField(i1)
5821 if op == 'equal':
5822 continue
5823 elif op == 'replace':
5824 if field._insertRight:
5825 # if replace with spaces in an insert-right control, ignore "forward" replace
5826 if not prev[i1:i2].strip():
5827 continue
5828 elif j1 < i1:
5829 ## dbg('setting sel_start to', j1)
5830 sel_start = j1
5831 else:
5832 ## dbg('setting sel_start to', i1)
5833 sel_start = i1
5834 else:
5835 ## dbg('setting sel_start to', i1)
5836 sel_start = i1
5837 ## dbg('saw replace; breaking')
5838 break
5839 elif op == 'insert' and not value[i1:i2]:
5840 ## dbg('forward %s found' % op)
5841 if prev[j1:j2].strip():
5842 ## dbg('item to insert non-empty; setting sel_start to', j1)
5843 sel_start = j1
5844 break
5845 elif not field._insertRight:
5846 ## dbg('setting sel_start to inserted space:', j1)
5847 sel_start = j1
5848 break
5849 elif op == 'delete':
5850 ## dbg('delete; field._insertRight?', field._insertRight, 'value[%d:%d].lstrip: "%s"' % (i1,i2,value[i1:i2].lstrip()))
5851 if field._insertRight:
5852 if value[i1:i2].lstrip():
5853 ## dbg('setting sel_start to ', j1)
5854 sel_start = j1
5855 ## dbg('breaking loop')
5856 break
5857 else:
5858 continue
5859 else:
5860 ## dbg('saw delete; breaking')
5861 break
5862 else:
5863 ## dbg('unknown code!')
5864 # we've got what we need
5865 break
5866
5867
5868 if not diff_found:
5869 ## dbg('no insert,delete or replace found (!)')
5870 # do "left-insert"-centric processing of difference based on l.c.s.:
5871 if i == j and j != sel_start: # match starts after start of selection
5872 sel_to = sel_start + (j-sel_start) # select to start of match
5873 else:
5874 sel_to = j # (change ends at j)
5875
5876
5877 # There are several situations where the calculated difference is
5878 # not what we want to select. If changing sign, or just adding
5879 # group characters, we really don't want to highlight the characters
5880 # changed, but instead leave the cursor where it is.
5881 # Also, there a situations in which the difference can be ambiguous;
5882 # Consider:
5883 #
5884 # current value: 11234
5885 # previous value: 1111234
5886 #
5887 # Where did the cursor actually lie and which 1s were selected on the delete
5888 # operation?
5889 #
5890 # Also, difflib can "get it wrong;" Consider:
5891 #
5892 # current value: " 128.66"
5893 # previous value: " 121.86"
5894 #
5895 # difflib produces the following opcodes, which are sub-optimal:
5896 # equal value[0:9] ( 12) prev[0:9] ( 12)
5897 # insert value[9:9] () prev[9:11] (1.)
5898 # equal value[9:10] (8) prev[11:12] (8)
5899 # delete value[10:11] (.) prev[12:12] ()
5900 # equal value[11:12] (6) prev[12:13] (6)
5901 # delete value[12:13] (6) prev[13:13] ()
5902 #
5903 # This should have been:
5904 # equal value[0:9] ( 12) prev[0:9] ( 12)
5905 # replace value[9:11] (8.6) prev[9:11] (1.8)
5906 # equal value[12:13] (6) prev[12:13] (6)
5907 #
5908 # But it didn't figure this out!
5909 #
5910 # To get all this right, we use the previous selection recorded to help us...
5911
5912 if (sel_start, sel_to) != self._prevSelection:
5913 ## dbg('calculated selection', (sel_start, sel_to), "doesn't match previous", self._prevSelection)
5914
5915 prev_sel_start, prev_sel_to = self._prevSelection
5916 field = self._FindField(sel_start)
5917 if( self._signOk
5918 and sel_start < self._masklength
5919 and (prev[sel_start] in ('-', '(', ')')
5920 or value[sel_start] in ('-', '(', ')')) ):
5921 # change of sign; leave cursor alone...
5922 ## dbg("prev[sel_start] in ('-', '(', ')')?", prev[sel_start] in ('-', '(', ')'))
5923 ## dbg("value[sel_start] in ('-', '(', ')')?", value[sel_start] in ('-', '(', ')'))
5924 ## dbg('setting selection to previous one')
5925 sel_start, sel_to = self._prevSelection
5926
5927 elif field._groupdigits and (value[sel_start:sel_to] == field._groupChar
5928 or prev[sel_start:sel_to] == field._groupChar):
5929 # do not highlight grouping changes
5930 ## dbg('value[sel_start:sel_to] == field._groupChar?', value[sel_start:sel_to] == field._groupChar)
5931 ## dbg('prev[sel_start:sel_to] == field._groupChar?', prev[sel_start:sel_to] == field._groupChar)
5932 ## dbg('setting selection to previous one')
5933 sel_start, sel_to = self._prevSelection
5934
5935 else:
5936 calc_select_len = sel_to - sel_start
5937 prev_select_len = prev_sel_to - prev_sel_start
5938
5939 ## dbg('sel_start == prev_sel_start', sel_start == prev_sel_start)
5940 ## dbg('sel_to > prev_sel_to', sel_to > prev_sel_to)
5941
5942 if prev_select_len >= calc_select_len:
5943 # old selection was bigger; trust it:
5944 ## dbg('prev_select_len >= calc_select_len?', prev_select_len >= calc_select_len)
5945 if not field._insertRight:
5946 ## dbg('setting selection to previous one')
5947 sel_start, sel_to = self._prevSelection
5948 else:
5949 sel_to = self._prevSelection[1]
5950 ## dbg('setting selection to', (sel_start, sel_to))
5951
5952 elif( sel_to > prev_sel_to # calculated select past last selection
5953 and prev_sel_to < len(self._template) # and prev_sel_to not at end of control
5954 and sel_to == len(self._template) ): # and calculated selection goes to end of control
5955
5956 i, j, k = sm.find_longest_match(prev_sel_to, length, prev_sel_to, length)
5957 ## dbg('i,j,k = ', (i,j,k), 'value[i:i+k] = "%s"' % value[i:i+k], 'prev[j:j+k] = "%s"' % prev[j:j+k] )
5958 if k > 0:
5959 # difflib must not have optimized opcodes properly;
5960 sel_to = j
5961
5962 else:
5963 # look for possible ambiguous diff:
5964
5965 # if last change resulted in no selection, test from resulting cursor position:
5966 if prev_sel_start == prev_sel_to:
5967 calc_select_len = sel_to - sel_start
5968 field = self._FindField(prev_sel_start)
5969
5970 # determine which way to search from last cursor position for ambiguous change:
5971 if field._insertRight:
5972 test_sel_start = prev_sel_start
5973 test_sel_to = prev_sel_start + calc_select_len
5974 else:
5975 test_sel_start = prev_sel_start - calc_select_len
5976 test_sel_to = prev_sel_start
5977 else:
5978 test_sel_start, test_sel_to = prev_sel_start, prev_sel_to
5979
5980 ## dbg('test selection:', (test_sel_start, test_sel_to))
5981 ## dbg('calc change: "%s"' % prev[sel_start:sel_to])
5982 ## dbg('test change: "%s"' % prev[test_sel_start:test_sel_to])
5983
5984 # if calculated selection spans characters, and same characters
5985 # "before" the previous insertion point are present there as well,
5986 # select the ones related to the last known selection instead.
5987 if( sel_start != sel_to
5988 and test_sel_to < len(self._template)
5989 and prev[test_sel_start:test_sel_to] == prev[sel_start:sel_to] ):
5990
5991 sel_start, sel_to = test_sel_start, test_sel_to
5992
5993 # finally, make sure that the old and new values are
5994 # different where we say they're different:
5995 while( sel_to - 1 > 0
5996 and sel_to > sel_start
5997 and value[sel_to-1:] == prev[sel_to-1:]):
5998 sel_to -= 1
5999 while( sel_start + 1 < self._masklength
6000 and sel_start < sel_to
6001 and value[:sel_start+1] == prev[:sel_start+1]):
6002 sel_start += 1
6003
6004 ## dbg('sel_start, sel_to:', sel_start, sel_to)
6005 ## dbg('previous value: "%s"' % prev)
6006 ## dbg(indent=0)
6007 if just_return_results:
6008 return prev, (sel_start, sel_to)
6009 # else...
6010 self._SetValue(prev)
6011 self._SetInsertionPoint(sel_start)
6012 self._SetSelection(sel_start, sel_to)
6013
6014 else:
6015 ## dbg('no difference between previous value')
6016 ## dbg(indent=0)
6017 if just_return_results:
6018 return prev, self._GetSelection()
6019
6020
6021 def _OnClear(self, event):
6022 """ Provides an action for context menu delete operation """
6023 self.ClearValue()
6024
6025
6026 def _OnContextMenu(self, event):
6027 ## dbg('MaskedEditMixin::OnContextMenu()', indent=1)
6028 menu = wx.Menu()
6029 menu.Append(wx.ID_UNDO, "Undo", "")
6030 menu.AppendSeparator()
6031 menu.Append(wx.ID_CUT, "Cut", "")
6032 menu.Append(wx.ID_COPY, "Copy", "")
6033 menu.Append(wx.ID_PASTE, "Paste", "")
6034 menu.Append(wx.ID_CLEAR, "Delete", "")
6035 menu.AppendSeparator()
6036 menu.Append(wx.ID_SELECTALL, "Select All", "")
6037
6038 wx.EVT_MENU(menu, wx.ID_UNDO, self._OnCtrl_Z)
6039 wx.EVT_MENU(menu, wx.ID_CUT, self._OnCtrl_X)
6040 wx.EVT_MENU(menu, wx.ID_COPY, self._OnCtrl_C)
6041 wx.EVT_MENU(menu, wx.ID_PASTE, self._OnCtrl_V)
6042 wx.EVT_MENU(menu, wx.ID_CLEAR, self._OnClear)
6043 wx.EVT_MENU(menu, wx.ID_SELECTALL, self._OnCtrl_A)
6044
6045 # ## WSS: The base control apparently handles
6046 # enable/disable of wx.ID_CUT, wx.ID_COPY, wx.ID_PASTE
6047 # and wx.ID_CLEAR menu items even if the menu is one
6048 # we created. However, it doesn't do undo properly,
6049 # so we're keeping track of previous values ourselves.
6050 # Therefore, we have to override the default update for
6051 # that item on the menu:
6052 wx.EVT_UPDATE_UI(self, wx.ID_UNDO, self._UndoUpdateUI)
6053 self._contextMenu = menu
6054
6055 self.PopupMenu(menu, event.GetPosition())
6056 menu.Destroy()
6057 self._contextMenu = None
6058 ## dbg(indent=0)
6059
6060 def _UndoUpdateUI(self, event):
6061 if self._prevValue is None or self._prevValue == self._curValue:
6062 self._contextMenu.Enable(wx.ID_UNDO, False)
6063 else:
6064 self._contextMenu.Enable(wx.ID_UNDO, True)
6065
6066
6067 def _OnCtrlParametersChanged(self):
6068 """
6069 Overridable function to allow derived classes to take action as a
6070 result of parameter changes prior to possibly changing the value
6071 of the control.
6072 """
6073 pass
6074
6075 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
6076 class MaskedEditAccessorsMixin:
6077 """
6078 To avoid a ton of boiler-plate, and to automate the getter/setter generation
6079 for each valid control parameter so we never forget to add the functions when
6080 adding parameters, this class programmatically adds the masked edit mixin
6081 parameters to itself.
6082 (This makes it easier for Designers like Boa to deal with masked controls.)
6083
6084 To further complicate matters, this is done with an extra level of inheritance,
6085 so that "general" classes like masked.TextCtrl can have all possible attributes,
6086 while derived classes, like masked.TimeCtrl and masked.NumCtrl can prevent
6087 exposure of those optional attributes of their base class that do not make
6088 sense for their derivation.
6089
6090 Therefore, we define:
6091 BaseMaskedTextCtrl(TextCtrl, MaskedEditMixin)
6092 and
6093 masked.TextCtrl(BaseMaskedTextCtrl, MaskedEditAccessorsMixin).
6094
6095 This allows us to then derive:
6096 masked.NumCtrl( BaseMaskedTextCtrl )
6097
6098 and not have to expose all the same accessor functions for the
6099 derived control when they don't all make sense for it.
6100
6101 """
6102
6103 # Define the default set of attributes exposed by the most generic masked controls:
6104 exposed_basectrl_params = MaskedEditMixin.valid_ctrl_params.keys() + Field.valid_params.keys()
6105 exposed_basectrl_params.remove('index')
6106 exposed_basectrl_params.remove('extent')
6107 exposed_basectrl_params.remove('foregroundColour') # (base class already has this)
6108
6109 for param in exposed_basectrl_params:
6110 propname = param[0].upper() + param[1:]
6111 exec('def Set%s(self, value): self.SetCtrlParameters(%s=value)' % (propname, param))
6112 exec('def Get%s(self): return self.GetCtrlParameter("%s")''' % (propname, param))
6113
6114 if param.find('Colour') != -1:
6115 # add non-british spellings, for backward-compatibility
6116 propname.replace('Colour', 'Color')
6117
6118 exec('def Set%s(self, value): self.SetCtrlParameters(%s=value)' % (propname, param))
6119 exec('def Get%s(self): return self.GetCtrlParameter("%s")''' % (propname, param))
6120
6121
6122
6123
6124 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
6125 ## these are helper subroutines:
6126
6127 def _movetofloat( origvalue, fmtstring, neg, addseparators=False, sepchar = ',',fillchar=' '):
6128 """ addseparators = add separator character every three numerals if True
6129 """
6130 fmt0 = fmtstring.split('.')
6131 fmt1 = fmt0[0]
6132 fmt2 = fmt0[1]
6133 val = origvalue.split('.')[0].strip()
6134 ret = fillchar * (len(fmt1)-len(val)) + val + "." + "0" * len(fmt2)
6135 if neg:
6136 ret = '-' + ret[1:]
6137 return (ret,len(fmt1))
6138
6139
6140 def _isDateType( fmtstring ):
6141 """ Checks the mask and returns True if it fits an allowed
6142 date or datetime format.
6143 """
6144 dateMasks = ("^##/##/####",
6145 "^##-##-####",
6146 "^##.##.####",
6147 "^####/##/##",
6148 "^####-##-##",
6149 "^####.##.##",
6150 "^##/CCC/####",
6151 "^##.CCC.####",
6152 "^##/##/##$",
6153 "^##/##/## ",
6154 "^##/CCC/##$",
6155 "^##.CCC.## ",)
6156 reString = "|".join(dateMasks)
6157 filter = re.compile( reString)
6158 if re.match(filter,fmtstring): return True
6159 return False
6160
6161 def _isTimeType( fmtstring ):
6162 """ Checks the mask and returns True if it fits an allowed
6163 time format.
6164 """
6165 reTimeMask = "^##:##(:##)?( (AM|PM))?"
6166 filter = re.compile( reTimeMask )
6167 if re.match(filter,fmtstring): return True
6168 return False
6169
6170
6171 def _isFloatingPoint( fmtstring):
6172 filter = re.compile("[ ]?[#]+\.[#]+\n")
6173 if re.match(filter,fmtstring+"\n"): return True
6174 return False
6175
6176
6177 def _isInteger( fmtstring ):
6178 filter = re.compile("[#]+\n")
6179 if re.match(filter,fmtstring+"\n"): return True
6180 return False
6181
6182
6183 def _getDateParts( dateStr, dateFmt ):
6184 if len(dateStr) > 11: clip = dateStr[0:11]
6185 else: clip = dateStr
6186 if clip[-2] not in string.digits:
6187 clip = clip[:-1] # (got part of time; drop it)
6188
6189 dateSep = (('/' in clip) * '/') + (('-' in clip) * '-') + (('.' in clip) * '.')
6190 slices = clip.split(dateSep)
6191 if dateFmt == "MDY":
6192 y,m,d = (slices[2],slices[0],slices[1]) ## year, month, date parts
6193 elif dateFmt == "DMY":
6194 y,m,d = (slices[2],slices[1],slices[0]) ## year, month, date parts
6195 elif dateFmt == "YMD":
6196 y,m,d = (slices[0],slices[1],slices[2]) ## year, month, date parts
6197 else:
6198 y,m,d = None, None, None
6199 if not y:
6200 return None
6201 else:
6202 return y,m,d
6203
6204
6205 def _getDateSepChar(dateStr):
6206 clip = dateStr[0:10]
6207 dateSep = (('/' in clip) * '/') + (('-' in clip) * '-') + (('.' in clip) * '.')
6208 return dateSep
6209
6210
6211 def _makeDate( year, month, day, dateFmt, dateStr):
6212 sep = _getDateSepChar( dateStr)
6213 if dateFmt == "MDY":
6214 return "%s%s%s%s%s" % (month,sep,day,sep,year) ## year, month, date parts
6215 elif dateFmt == "DMY":
6216 return "%s%s%s%s%s" % (day,sep,month,sep,year) ## year, month, date parts
6217 elif dateFmt == "YMD":
6218 return "%s%s%s%s%s" % (year,sep,month,sep,day) ## year, month, date parts
6219 else:
6220 return none
6221
6222
6223 def _getYear(dateStr,dateFmt):
6224 parts = _getDateParts( dateStr, dateFmt)
6225 return parts[0]
6226
6227 def _getMonth(dateStr,dateFmt):
6228 parts = _getDateParts( dateStr, dateFmt)
6229 return parts[1]
6230
6231 def _getDay(dateStr,dateFmt):
6232 parts = _getDateParts( dateStr, dateFmt)
6233 return parts[2]
6234
6235 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
6236 class __test(wx.PySimpleApp):
6237 def OnInit(self):
6238 from wx.lib.rcsizer import RowColSizer
6239 self.frame = wx.Frame( None, -1, "MaskedEditMixin 0.0.7 Demo Page #1", size = (700,600))
6240 self.panel = wx.Panel( self.frame, -1)
6241 self.sizer = RowColSizer()
6242 self.labels = []
6243 self.editList = []
6244 rowcount = 4
6245
6246 id, id1 = wx.NewId(), wx.NewId()
6247 self.command1 = wx.Button( self.panel, id, "&Close" )
6248 self.command2 = wx.Button( self.panel, id1, "&AutoFormats" )
6249 self.sizer.Add(self.command1, row=0, col=0, flag=wx.ALL, border = 5)
6250 self.sizer.Add(self.command2, row=0, col=1, colspan=2, flag=wx.ALL, border = 5)
6251 self.panel.Bind(wx.EVT_BUTTON, self.onClick, self.command1 )
6252 ## self.panel.SetDefaultItem(self.command1 )
6253 self.panel.Bind(wx.EVT_BUTTON, self.onClickPage, self.command2)
6254
6255 self.check1 = wx.CheckBox( self.panel, -1, "Disallow Empty" )
6256 self.check2 = wx.CheckBox( self.panel, -1, "Highlight Empty" )
6257 self.sizer.Add( self.check1, row=0,col=3, flag=wx.ALL,border=5 )
6258 self.sizer.Add( self.check2, row=0,col=4, flag=wx.ALL,border=5 )
6259 self.panel.Bind(wx.EVT_CHECKBOX, self._onCheck1, self.check1 )
6260 self.panel.Bind(wx.EVT_CHECKBOX, self._onCheck2, self.check2 )
6261
6262
6263 label = """Press ctrl-s in any field to output the value and plain value. Press ctrl-x to clear and re-set any field.
6264 Note that all controls have been auto-sized by including F in the format code.
6265 Try entering nonsensical or partial values in validated fields to see what happens (use ctrl-s to test the valid status)."""
6266 label2 = "\nNote that the State and Last Name fields are list-limited (Name:Smith,Jones,Williams)."
6267
6268 self.label1 = wx.StaticText( self.panel, -1, label)
6269 self.label2 = wx.StaticText( self.panel, -1, "Description")
6270 self.label3 = wx.StaticText( self.panel, -1, "Mask Value")
6271 self.label4 = wx.StaticText( self.panel, -1, "Format")
6272 self.label5 = wx.StaticText( self.panel, -1, "Reg Expr Val. (opt)")
6273 self.label6 = wx.StaticText( self.panel, -1, "MaskedEdit Ctrl")
6274 self.label7 = wx.StaticText( self.panel, -1, label2)
6275 self.label7.SetForegroundColour("Blue")
6276 self.label1.SetForegroundColour("Blue")
6277 self.label2.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD))
6278 self.label3.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD))
6279 self.label4.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD))
6280 self.label5.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD))
6281 self.label6.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD))
6282
6283 self.sizer.Add( self.label1, row=1,col=0,colspan=7, flag=wx.ALL,border=5)
6284 self.sizer.Add( self.label7, row=2,col=0,colspan=7, flag=wx.ALL,border=5)
6285 self.sizer.Add( self.label2, row=3,col=0, flag=wx.ALL,border=5)
6286 self.sizer.Add( self.label3, row=3,col=1, flag=wx.ALL,border=5)
6287 self.sizer.Add( self.label4, row=3,col=2, flag=wx.ALL,border=5)
6288 self.sizer.Add( self.label5, row=3,col=3, flag=wx.ALL,border=5)
6289 self.sizer.Add( self.label6, row=3,col=4, flag=wx.ALL,border=5)
6290
6291 # The following list is of the controls for the demo. Feel free to play around with
6292 # the options!
6293 controls = [
6294 #description mask excl format regexp range,list,initial
6295 ("Phone No", "(###) ###-#### x:###", "", 'F!^-R', "^\(\d\d\d\) \d\d\d-\d\d\d\d", (),[],''),
6296 ("Last Name Only", "C{14}", "", 'F {list}', '^[A-Z][a-zA-Z]+', (),('Smith','Jones','Williams'),''),
6297 ("Full Name", "C{14}", "", 'F_', '^[A-Z][a-zA-Z]+ [A-Z][a-zA-Z]+', (),[],''),
6298 ("Social Sec#", "###-##-####", "", 'F', "\d{3}-\d{2}-\d{4}", (),[],''),
6299 ("U.S. Zip+4", "#{5}-#{4}", "", 'F', "\d{5}-(\s{4}|\d{4})",(),[],''),
6300 ("U.S. State (2 char)\n(with default)","AA", "", 'F!', "[A-Z]{2}", (),states, 'AZ'),
6301 ("Customer No", "\CAA-###", "", 'F!', "C[A-Z]{2}-\d{3}", (),[],''),
6302 ("Date (MDY) + Time\n(with default)", "##/##/#### ##:## AM", 'BCDEFGHIJKLMNOQRSTUVWXYZ','DFR!',"", (),[], r'03/05/2003 12:00 AM'),
6303 ("Invoice Total", "#{9}.##", "", 'F-R,', "", (),[], ''),
6304 ("Integer (signed)\n(with default)", "#{6}", "", 'F-R', "", (),[], '0 '),
6305 ("Integer (unsigned)\n(with default), 1-399", "######", "", 'F', "", (1,399),[], '1 '),
6306 ("Month selector", "XXX", "", 'F', "", (),
6307 ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],""),
6308 ("fraction selector","#/##", "", 'F', "^\d\/\d\d?", (),
6309 ['2/3', '3/4', '1/2', '1/4', '1/8', '1/16', '1/32', '1/64'], "")
6310 ]
6311
6312 for control in controls:
6313 self.sizer.Add( wx.StaticText( self.panel, -1, control[0]),row=rowcount, col=0,border=5,flag=wx.ALL)
6314 self.sizer.Add( wx.StaticText( self.panel, -1, control[1]),row=rowcount, col=1,border=5, flag=wx.ALL)
6315 self.sizer.Add( wx.StaticText( self.panel, -1, control[3]),row=rowcount, col=2,border=5, flag=wx.ALL)
6316 self.sizer.Add( wx.StaticText( self.panel, -1, control[4][:20]),row=rowcount, col=3,border=5, flag=wx.ALL)
6317
6318 if control in controls[:]:#-2]:
6319 newControl = MaskedTextCtrl( self.panel, -1, "",
6320 mask = control[1],
6321 excludeChars = control[2],
6322 formatcodes = control[3],
6323 includeChars = "",
6324 validRegex = control[4],
6325 validRange = control[5],
6326 choices = control[6],
6327 defaultValue = control[7],
6328 demo = True)
6329 if control[6]: newControl.SetCtrlParameters(choiceRequired = True)
6330 else:
6331 newControl = MaskedComboBox( self.panel, -1, "",
6332 choices = control[7],
6333 choiceRequired = True,
6334 mask = control[1],
6335 formatcodes = control[3],
6336 excludeChars = control[2],
6337 includeChars = "",
6338 validRegex = control[4],
6339 validRange = control[5],
6340 demo = True)
6341 self.editList.append( newControl )
6342
6343 self.sizer.Add( newControl, row=rowcount,col=4,flag=wx.ALL,border=5)
6344 rowcount += 1
6345
6346 self.sizer.AddGrowableCol(4)
6347
6348 self.panel.SetSizer(self.sizer)
6349 self.panel.SetAutoLayout(1)
6350
6351 self.frame.Show(1)
6352 self.MainLoop()
6353
6354 return True
6355
6356 def onClick(self, event):
6357 self.frame.Close()
6358
6359 def onClickPage(self, event):
6360 self.page2 = __test2(self.frame,-1,"")
6361 self.page2.Show(True)
6362
6363 def _onCheck1(self,event):
6364 """ Set required value on/off """
6365 value = event.IsChecked()
6366 if value:
6367 for control in self.editList:
6368 control.SetCtrlParameters(emptyInvalid=True)
6369 control.Refresh()
6370 else:
6371 for control in self.editList:
6372 control.SetCtrlParameters(emptyInvalid=False)
6373 control.Refresh()
6374 self.panel.Refresh()
6375
6376 def _onCheck2(self,event):
6377 """ Highlight empty values"""
6378 value = event.IsChecked()
6379 if value:
6380 for control in self.editList:
6381 control.SetCtrlParameters( emptyBackgroundColour = 'Aquamarine')
6382 control.Refresh()
6383 else:
6384 for control in self.editList:
6385 control.SetCtrlParameters( emptyBackgroundColour = 'White')
6386 control.Refresh()
6387 self.panel.Refresh()
6388
6389
6390 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
6391
6392 class __test2(wx.Frame):
6393 def __init__(self, parent, id, caption):
6394 wx.Frame.__init__( self, parent, id, "MaskedEdit control 0.0.7 Demo Page #2 -- AutoFormats", size = (550,600))
6395 from wx.lib.rcsizer import RowColSizer
6396 self.panel = wx.Panel( self, -1)
6397 self.sizer = RowColSizer()
6398 self.labels = []
6399 self.texts = []
6400 rowcount = 4
6401
6402 label = """\
6403 All these controls have been created by passing a single parameter, the AutoFormat code.
6404 The class contains an internal dictionary of types and formats (autoformats).
6405 To see a great example of validations in action, try entering a bad email address, then tab out."""
6406
6407 self.label1 = wx.StaticText( self.panel, -1, label)
6408 self.label2 = wx.StaticText( self.panel, -1, "Description")
6409 self.label3 = wx.StaticText( self.panel, -1, "AutoFormat Code")
6410 self.label4 = wx.StaticText( self.panel, -1, "MaskedEdit Control")
6411 self.label1.SetForegroundColour("Blue")
6412 self.label2.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD))
6413 self.label3.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD))
6414 self.label4.SetFont(wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD))
6415
6416 self.sizer.Add( self.label1, row=1,col=0,colspan=3, flag=wx.ALL,border=5)
6417 self.sizer.Add( self.label2, row=3,col=0, flag=wx.ALL,border=5)
6418 self.sizer.Add( self.label3, row=3,col=1, flag=wx.ALL,border=5)
6419 self.sizer.Add( self.label4, row=3,col=2, flag=wx.ALL,border=5)
6420
6421 id, id1 = wx.NewId(), wx.NewId()
6422 self.command1 = wx.Button( self.panel, id, "&Close")
6423 self.command2 = wx.Button( self.panel, id1, "&Print Formats")
6424 self.panel.Bind(wx.EVT_BUTTON, self.onClick, self.command1)
6425 self.panel.SetDefaultItem(self.command1)
6426 self.panel.Bind(wx.EVT_BUTTON, self.onClickPrint, self.command2)
6427
6428 # The following list is of the controls for the demo. Feel free to play around with
6429 # the options!
6430 controls = [
6431 ("Phone No","USPHONEFULLEXT"),
6432 ("US Date + Time","USDATETIMEMMDDYYYY/HHMM"),
6433 ("US Date MMDDYYYY","USDATEMMDDYYYY/"),
6434 ("Time (with seconds)","TIMEHHMMSS"),
6435 ("Military Time\n(without seconds)","24HRTIMEHHMM"),
6436 ("Social Sec#","USSOCIALSEC"),
6437 ("Credit Card","CREDITCARD"),
6438 ("Expiration MM/YY","EXPDATEMMYY"),
6439 ("Percentage","PERCENT"),
6440 ("Person's Age","AGE"),
6441 ("US Zip Code","USZIP"),
6442 ("US Zip+4","USZIPPLUS4"),
6443 ("Email Address","EMAIL"),
6444 ("IP Address", "(derived control IpAddrCtrl)")
6445 ]
6446
6447 for control in controls:
6448 self.sizer.Add( wx.StaticText( self.panel, -1, control[0]),row=rowcount, col=0,border=5,flag=wx.ALL)
6449 self.sizer.Add( wx.StaticText( self.panel, -1, control[1]),row=rowcount, col=1,border=5, flag=wx.ALL)
6450 if control in controls[:-1]:
6451 self.sizer.Add( MaskedTextCtrl( self.panel, -1, "",
6452 autoformat = control[1],
6453 demo = True),
6454 row=rowcount,col=2,flag=wx.ALL,border=5)
6455 else:
6456 self.sizer.Add( IpAddrCtrl( self.panel, -1, "", demo=True ),
6457 row=rowcount,col=2,flag=wx.ALL,border=5)
6458 rowcount += 1
6459
6460 self.sizer.Add(self.command1, row=0, col=0, flag=wx.ALL, border = 5)
6461 self.sizer.Add(self.command2, row=0, col=1, flag=wx.ALL, border = 5)
6462 self.sizer.AddGrowableCol(3)
6463
6464 self.panel.SetSizer(self.sizer)
6465 self.panel.SetAutoLayout(1)
6466
6467 def onClick(self, event):
6468 self.Close()
6469
6470 def onClickPrint(self, event):
6471 for format in masktags.keys():
6472 sep = "+------------------------+"
6473 print "%s\n%s \n Mask: %s \n RE Validation string: %s\n" % (sep,format, masktags[format]['mask'], masktags[format]['validRegex'])
6474
6475 ## ---------- ---------- ---------- ---------- ---------- ---------- ----------
6476
6477 if __name__ == "__main__":
6478 app = __test(False)
6479
6480 __i=0
6481 ##
6482 ## Current Issues:
6483 ## ===================================
6484 ##
6485 ## 1. WS: For some reason I don't understand, the control is generating two (2)
6486 ## EVT_TEXT events for every one (1) .SetValue() of the underlying control.
6487 ## I've been unsuccessful in determining why or in my efforts to make just one
6488 ## occur. So, I've added a hack to save the last seen value from the
6489 ## control in the EVT_TEXT handler, and if *different*, call event.Skip()
6490 ## to propagate it down the event chain, and let the application see it.
6491 ##
6492 ## 2. WS: MaskedComboBox is deficient in several areas, all having to do with the
6493 ## behavior of the underlying control that I can't fix. The problems are:
6494 ## a) The background coloring doesn't work in the text field of the control;
6495 ## instead, there's a only border around it that assumes the correct color.
6496 ## b) The control will not pass WXK_TAB to the event handler, no matter what
6497 ## I do, and there's no style wxCB_PROCESS_TAB like wxTE_PROCESS_TAB to
6498 ## indicate that we want these events. As a result, MaskedComboBox
6499 ## doesn't do the nice field-tabbing that MaskedTextCtrl does.
6500 ## c) Auto-complete had to be reimplemented for the control because programmatic
6501 ## setting of the value of the text field does not set up the auto complete
6502 ## the way that the control processing keystrokes does. (But I think I've
6503 ## implemented a fairly decent approximation.) Because of this the control
6504 ## also won't auto-complete on dropdown, and there's no event I can catch
6505 ## to work around this problem.
6506 ## d) There is no method provided for getting the selection; the hack I've
6507 ## implemented has its flaws, not the least of which is that due to the
6508 ## strategy that I'm using, the paste buffer is always replaced by the
6509 ## contents of the control's selection when in focus, on each keystroke;
6510 ## this makes it impossible to paste anything into a MaskedComboBox
6511 ## at the moment... :-(
6512 ## e) The other deficient behavior, likely induced by the workaround for (d),
6513 ## is that you can can't shift-left to select more than one character
6514 ## at a time.
6515 ##
6516 ##
6517 ## 3. WS: Controls on wxPanels don't seem to pass Shift-WXK_TAB to their
6518 ## EVT_KEY_DOWN or EVT_CHAR event handlers. Until this is fixed in
6519 ## wxWindows, shift-tab won't take you backwards through the fields of
6520 ## a MaskedTextCtrl like it should. Until then Shifted arrow keys will
6521 ## work like shift-tab and tab ought to.
6522 ##
6523
6524 ## To-Do's:
6525 ## =============================##
6526 ## 1. Add Popup list for auto-completable fields that simulates combobox on individual
6527 ## fields. Example: City validates against list of cities, or zip vs zip code list.
6528 ## 2. Allow optional monetary symbols (eg. $, pounds, etc.) at front of a "decimal"
6529 ## control.
6530 ## 3. Fix shift-left selection for MaskedComboBox.
6531 ## 5. Transform notion of "decimal control" to be less "entire control"-centric,
6532 ## so that monetary symbols can be included and still have the appropriate
6533 ## semantics. (Big job, as currently written, but would make control even
6534 ## more useful for business applications.)
6535
6536
6537 ## CHANGELOG:
6538 ## ====================
6539 ## Version 1.8
6540 ## 1. Fixed bug involving incorrect variable name, causing combobox autocomplete to fail.
6541 ## 2. Added proper support for unicode version of wxPython
6542 ## 3. Added * as mask char meaning "all ansi chars" (ordinals 32-255).
6543 ## 4. Converted doc strings to use reST format, for ePyDoc documentation.
6544 ## 5. Renamed helper functions, classes, etc. not intended to be visible in public
6545 ## interface to code.
6546 ##
6547 ## Version 1.7
6548 ## 1. Fixed intra-right-insert-field erase, such that it doesn't leave a hole, but instead
6549 ## shifts the text to the left accordingly.
6550 ## 2. Fixed _SetValue() to place cursor after last character inserted, rather than end of
6551 ## mask.
6552 ## 3. Fixed some incorrect undo behavior for right-insert fields, and allowed derived classes
6553 ## (eg. numctrl) to pass modified values for undo processing (to handle/ignore grouping
6554 ## chars properly.)
6555 ## 4. Fixed autoselect behavior to work similarly to (2) above, so that combobox
6556 ## selection will only select the non-empty text, as per request.
6557 ## 5. Fixed tabbing to work with 2.5.2 semantics.
6558 ## 6. Fixed size calculation to handle changing fonts
6559 ##
6560 ## Version 1.6
6561 ## 1. Reorganized masked controls into separate package, renamed things accordingly
6562 ## 2. Split actual controls out of this file into their own files.
6563 ## Version 1.5
6564 ## (Reported) bugs fixed:
6565 ## 1. Crash ensues if you attempt to change the mask of a read-only
6566 ## MaskedComboBox after initial construction.
6567 ## 2. Changed strategy of defining Get/Set property functions so that
6568 ## these are now generated dynamically at runtime, rather than as
6569 ## part of the class definition. (This makes it possible to have
6570 ## more general base classes that have many more options for configuration
6571 ## without requiring that derivations support the same options.)
6572 ## 3. Fixed IsModified for _Paste() and _OnErase().
6573 ##
6574 ## Enhancements:
6575 ## 1. Fixed "attribute function inheritance," since base control is more
6576 ## generic than subsequent derivations, not all property functions of a
6577 ## generic control should be exposed in those derivations. New strategy
6578 ## uses base control classes (eg. BaseMaskedTextCtrl) that should be
6579 ## used to derive new class types, and mixed with their own mixins to
6580 ## only expose those attributes from the generic masked controls that
6581 ## make sense for the derivation. (This makes Boa happier.)
6582 ## 2. Renamed (with b-c) MILTIME autoformats to 24HRTIME, so as to be less
6583 ## "parochial."
6584 ##
6585 ## Version 1.4
6586 ## (Reported) bugs fixed:
6587 ## 1. Right-click menu allowed "cut" operation that destroyed mask
6588 ## (was implemented by base control)
6589 ## 2. MaskedComboBox didn't allow .Append() of mixed-case values; all
6590 ## got converted to lower case.
6591 ## 3. MaskedComboBox selection didn't deal with spaces in values
6592 ## properly when autocompleting, and didn't have a concept of "next"
6593 ## match for handling choice list duplicates.
6594 ## 4. Size of MaskedComboBox was always default.
6595 ## 5. Email address regexp allowed some "non-standard" things, and wasn't
6596 ## general enough.
6597 ## 6. Couldn't easily reset MaskedComboBox contents programmatically.
6598 ## 7. Couldn't set emptyInvalid during construction.
6599 ## 8. Under some versions of wxPython, readonly comboboxes can apparently
6600 ## return a GetInsertionPoint() result (655535), causing masked control
6601 ## to fail.
6602 ## 9. Specifying an empty mask caused the controls to traceback.
6603 ## 10. Can't specify float ranges for validRange.
6604 ## 11. '.' from within a the static portion of a restricted IP address
6605 ## destroyed the mask from that point rightward; tab when cursor is
6606 ## before 1st field takes cursor past that field.
6607 ##
6608 ## Enhancements:
6609 ## 12. Added Ctrl-Z/Undo handling, (and implemented context-menu properly.)
6610 ## 13. Added auto-select option on char input for masked controls with
6611 ## choice lists.
6612 ## 14. Added '>' formatcode, allowing insert within a given or each field
6613 ## as appropriate, rather than requiring "overwrite". This makes single
6614 ## field controls that just have validation rules (eg. EMAIL) much more
6615 ## friendly. The same flag controls left shift when deleting vs just
6616 ## blanking the value, and for right-insert fields, allows right-insert
6617 ## at any non-blank (non-sign) position in the field.
6618 ## 15. Added option to use to indicate negative values for numeric controls.
6619 ## 16. Improved OnFocus handling of numeric controls.
6620 ## 17. Enhanced Home/End processing to allow operation on a field level,
6621 ## using ctrl key.
6622 ## 18. Added individual Get/Set functions for control parameters, for
6623 ## simplified integration with Boa Constructor.
6624 ## 19. Standardized "Colour" parameter names to match wxPython, with
6625 ## non-british spellings still supported for backward-compatibility.
6626 ## 20. Added '&' mask specification character for punctuation only (no letters
6627 ## or digits).
6628 ## 21. Added (in a separate file) wx.MaskedCtrl() factory function to provide
6629 ## unified interface to the masked edit subclasses.
6630 ##
6631 ##
6632 ## Version 1.3
6633 ## 1. Made it possible to configure grouping, decimal and shift-decimal characters,
6634 ## to make controls more usable internationally.
6635 ## 2. Added code to smart "adjust" value strings presented to .SetValue()
6636 ## for right-aligned numeric format controls if they are shorter than
6637 ## than the control width, prepending the missing portion, prepending control
6638 ## template left substring for the missing characters, so that setting
6639 ## numeric values is easier.
6640 ## 3. Renamed SetMaskParameters SetCtrlParameters() (with old name preserved
6641 ## for b-c), as this makes more sense.
6642 ##
6643 ## Version 1.2
6644 ## 1. Fixed .SetValue() to replace the current value, rather than the current
6645 ## selection. Also changed it to generate ValueError if presented with
6646 ## either a value which doesn't follow the format or won't fit. Also made
6647 ## set value adjust numeric and date controls as if user entered the value.
6648 ## Expanded doc explaining how SetValue() works.
6649 ## 2. Fixed EUDATE* autoformats, fixed IsDateType mask list, and added ability to
6650 ## use 3-char months for dates, and EUDATETIME, and EUDATEMILTIME autoformats.
6651 ## 3. Made all date autoformats automatically pick implied "datestyle".
6652 ## 4. Added IsModified override, since base wx.TextCtrl never reports modified if
6653 ## .SetValue used to change the value, which is what the masked edit controls
6654 ## use internally.
6655 ## 5. Fixed bug in date position adjustment on 2 to 4 digit date conversion when
6656 ## using tab to "leave field" and auto-adjust.
6657 ## 6. Fixed bug in _isCharAllowed() for negative number insertion on pastes,
6658 ## and bug in ._Paste() that didn't account for signs in signed masks either.
6659 ## 7. Fixed issues with _adjustPos for right-insert fields causing improper
6660 ## selection/replacement of values
6661 ## 8. Fixed _OnHome handler to properly handle extending current selection to
6662 ## beginning of control.
6663 ## 9. Exposed all (valid) autoformats to demo, binding descriptions to
6664 ## autoformats.
6665 ## 10. Fixed a couple of bugs in email regexp.
6666 ## 11. Made maskchardict an instance var, to make mask chars to be more
6667 ## amenable to international use.
6668 ## 12. Clarified meaning of '-' formatcode in doc.
6669 ## 13. Fixed a couple of coding bugs being flagged by Python2.1.
6670 ## 14. Fixed several issues with sign positioning, erasure and validity
6671 ## checking for "numeric" masked controls.
6672 ## 15. Added validation to IpAddrCtrl.SetValue().
6673 ##
6674 ## Version 1.1
6675 ## 1. Changed calling interface to use boolean "useFixedWidthFont" (True by default)
6676 ## vs. literal font facename, and use wxTELETYPE as the font family
6677 ## if so specified.
6678 ## 2. Switched to use of dbg module vs. locally defined version.
6679 ## 3. Revamped entire control structure to use Field classes to hold constraint
6680 ## and formatting data, to make code more hierarchical, allow for more
6681 ## sophisticated masked edit construction.
6682 ## 4. Better strategy for managing options, and better validation on keywords.
6683 ## 5. Added 'V' format code, which requires that in order for a character
6684 ## to be accepted, it must result in a string that passes the validRegex.
6685 ## 6. Added 'S' format code which means "select entire field when navigating
6686 ## to new field."
6687 ## 7. Added 'r' format code to allow "right-insert" fields. (implies 'R'--right-alignment)
6688 ## 8. Added '<' format code to allow fields to require explicit cursor movement
6689 ## to leave field.
6690 ## 9. Added validFunc option to other validation mechanisms, that allows derived
6691 ## classes to add dynamic validation constraints to the control.
6692 ## 10. Fixed bug in validatePaste code causing possible IndexErrors, and also
6693 ## fixed failure to obey case conversion codes when pasting.
6694 ## 11. Implemented '0' (zero-pad) formatting code, as it wasn't being done anywhere...
6695 ## 12. Removed condition from OnDecimalPoint, so that it always truncates right on '.'
6696 ## 13. Enhanced IpAddrCtrl to use right-insert fields, selection on field traversal,
6697 ## individual field validation to prevent field values > 255, and require explicit
6698 ## tab/. to change fields.
6699 ## 14. Added handler for left double-click to select field under cursor.
6700 ## 15. Fixed handling for "Read-only" styles.
6701 ## 16. Separated signedForegroundColor from 'R' style, and added foregroundColor
6702 ## attribute, for more consistent and controllable coloring.
6703 ## 17. Added retainFieldValidation parameter, allowing top-level constraints
6704 ## such as "validRequired" to be set independently of field-level equivalent.
6705 ## (needed in TimeCtrl for bounds constraints.)
6706 ## 18. Refactored code a bit, cleaned up and commented code more heavily, fixed
6707 ## some of the logic for setting/resetting parameters, eg. fillChar, defaultValue,
6708 ## etc.
6709 ## 19. Fixed maskchar setting for upper/lowercase, to work in all locales.
6710 ##
6711 ##
6712 ## Version 1.0
6713 ## 1. Decimal point behavior restored for decimal and integer type controls:
6714 ## decimal point now trucates the portion > 0.
6715 ## 2. Return key now works like the tab character and moves to the next field,
6716 ## provided no default button is set for the form panel on which the control
6717 ## resides.
6718 ## 3. Support added in _FindField() for subclasses controls (like timecontrol)
6719 ## to determine where the current insertion point is within the mask (i.e.
6720 ## which sub-'field'). See method documentation for more info and examples.
6721 ## 4. Added Field class and support for all constraints to be field-specific
6722 ## in addition to being globally settable for the control.
6723 ## Choices for each field are validated for length and pastability into
6724 ## the field in question, raising ValueError if not appropriate for the control.
6725 ## Also added selective additional validation based on individual field constraints.
6726 ## By default, SHIFT-WXK_DOWN, SHIFT-WXK_UP, WXK_PRIOR and WXK_NEXT all
6727 ## auto-complete fields with choice lists, supplying the 1st entry in
6728 ## the choice list if the field is empty, and cycling through the list in
6729 ## the appropriate direction if already a match. WXK_DOWN will also auto-
6730 ## complete if the field is partially completed and a match can be made.
6731 ## SHIFT-WXK_UP/DOWN will also take you to the next field after any
6732 ## auto-completion performed.
6733 ## 5. Added autoCompleteKeycodes=[] parameters for allowing further
6734 ## customization of the control. Any keycode supplied as a member
6735 ## of the _autoCompleteKeycodes list will be treated like WXK_NEXT. If
6736 ## requireFieldChoice is set, then a valid value from each non-empty
6737 ## choice list will be required for the value of the control to validate.
6738 ## 6. Fixed "auto-sizing" to be relative to the font actually used, rather
6739 ## than making assumptions about character width.
6740 ## 7. Fixed GetMaskParameter(), which was non-functional in previous version.
6741 ## 8. Fixed exceptions raised to provide info on which control had the error.
6742 ## 9. Fixed bug in choice management of MaskedComboBox.
6743 ## 10. Fixed bug in IpAddrCtrl causing traceback if field value was of
6744 ## the form '# #'. Modified control code for IpAddrCtrl so that '.'
6745 ## in the middle of a field clips the rest of that field, similar to
6746 ## decimal and integer controls.
6747 ##
6748 ##
6749 ## Version 0.0.7
6750 ## 1. "-" is a toggle for sign; "+" now changes - signed numerics to positive.
6751 ## 2. ',' in formatcodes now causes numeric values to be comma-delimited (e.g.333,333).
6752 ## 3. New support for selecting text within the control.(thanks Will Sadkin!)
6753 ## Shift-End and Shift-Home now select text as you would expect
6754 ## Control-Shift-End selects to the end of the mask string, even if value not entered.
6755 ## Control-A selects all *entered* text, Shift-Control-A selects everything in the control.
6756 ## 4. event.Skip() added to onKillFocus to correct remnants when running in Linux (contributed-
6757 ## for some reason I couldn't find the original email but thanks!!!)
6758 ## 5. All major key-handling code moved to their own methods for easier subclassing: OnHome,
6759 ## OnErase, OnEnd, OnCtrl_X, OnCtrl_A, etc.
6760 ## 6. Email and autoformat validations corrected using regex provided by Will Sadkin (thanks!).
6761 ## (The rest of the changes in this version were done by Will Sadkin with permission from Jeff...)
6762 ## 7. New mechanism for replacing default behavior for any given key, using
6763 ## ._SetKeycodeHandler(keycode, func) and ._SetKeyHandler(char, func) now available
6764 ## for easier subclassing of the control.
6765 ## 8. Reworked the delete logic, cut, paste and select/replace logic, as well as some bugs
6766 ## with insertion point/selection modification. Changed Ctrl-X to use standard "cut"
6767 ## semantics, erasing the selection, rather than erasing the entire control.
6768 ## 9. Added option for an "default value" (ie. the template) for use when a single fillChar
6769 ## is not desired in every position. Added IsDefault() function to mean "does the value
6770 ## equal the template?" and modified .IsEmpty() to mean "do all of the editable
6771 ## positions in the template == the fillChar?"
6772 ## 10. Extracted mask logic into mixin, so we can have both MaskedTextCtrl and MaskedComboBox,
6773 ## now included.
6774 ## 11. MaskedComboBox now adds the capability to validate from list of valid values.
6775 ## Example: City validates against list of cities, or zip vs zip code list.
6776 ## 12. Fixed oversight in EVT_TEXT handler that prevented the events from being
6777 ## passed to the next handler in the event chain, causing updates to the
6778 ## control to be invisible to the parent code.
6779 ## 13. Added IPADDR autoformat code, and subclass IpAddrCtrl for controlling tabbing within
6780 ## the control, that auto-reformats as you move between cells.
6781 ## 14. Mask characters [A,a,X,#] can now appear in the format string as literals, by using '\'.
6782 ## 15. It is now possible to specify repeating masks, e.g. #{3}-#{3}-#{14}
6783 ## 16. Fixed major bugs in date validation, due to the fact that
6784 ## wxDateTime.ParseDate is too liberal, and will accept any form that
6785 ## makes any kind of sense, regardless of the datestyle you specified
6786 ## for the control. Unfortunately, the strategy used to fix it only
6787 ## works for versions of wxPython post 2.3.3.1, as a C++ assert box
6788 ## seems to show up on an invalid date otherwise, instead of a catchable
6789 ## exception.
6790 ## 17. Enhanced date adjustment to automatically adjust heuristic based on
6791 ## current year, making last century/this century determination on
6792 ## 2-digit year based on distance between today's year and value;
6793 ## if > 50 year separation, assume last century (and don't assume last
6794 ## century is 20th.)
6795 ## 18. Added autoformats and support for including HHMMSS as well as HHMM for
6796 ## date times, and added similar time, and militaray time autoformats.
6797 ## 19. Enhanced tabbing logic so that tab takes you to the next field if the
6798 ## control is a multi-field control.
6799 ## 20. Added stub method called whenever the control "changes fields", that
6800 ## can be overridden by subclasses (eg. IpAddrCtrl.)
6801 ## 21. Changed a lot of code to be more functionally-oriented so side-effects
6802 ## aren't as problematic when maintaining code and/or adding features.
6803 ## Eg: IsValid() now does not have side-effects; it merely reflects the
6804 ## validity of the value of the control; to determine validity AND recolor
6805 ## the control, _CheckValid() should be used with a value argument of None.
6806 ## Similarly, made most reformatting function take an optional candidate value
6807 ## rather than just using the current value of the control, and only
6808 ## have them change the value of the control if a candidate is not specified.
6809 ## In this way, you can do validation *before* changing the control.
6810 ## 22. Changed validRequired to mean "disallow chars that result in invalid
6811 ## value." (Old meaning now represented by emptyInvalid.) (This was
6812 ## possible once I'd made the changes in (19) above.)
6813 ## 23. Added .SetMaskParameters and .GetMaskParameter methods, so they
6814 ## can be set/modified/retrieved after construction. Removed individual
6815 ## parameter setting functions, in favor of this mechanism, so that
6816 ## all adjustment of the control based on changing parameter values can
6817 ## be handled in one place with unified mechanism.
6818 ## 24. Did a *lot* of testing and fixing re: numeric values. Added ability
6819 ## to type "grouping char" (ie. ',') and validate as appropriate.
6820 ## 25. Fixed ZIPPLUS4 to allow either 5 or 4, but if > 5 must be 9.
6821 ## 26. Fixed assumption about "decimal or integer" masks so that they're only
6822 ## made iff there's no validRegex associated with the field. (This
6823 ## is so things like zipcodes which look like integers can have more
6824 ## restrictive validation (ie. must be 5 digits.)
6825 ## 27. Added a ton more doc strings to explain use and derivation requirements
6826 ## and did regularization of the naming conventions.
6827 ## 28. Fixed a range bug in _adjustKey preventing z from being handled properly.
6828 ## 29. Changed behavior of '.' (and shift-.) in numeric controls to move to
6829 ## reformat the value and move the next field as appropriate. (shift-'.',
6830 ## ie. '>' moves to the previous field.
6831
6832 ## Version 0.0.6
6833 ## 1. Fixed regex bug that caused autoformat AGE to invalidate any age ending
6834 ## in '0'.
6835 ## 2. New format character 'D' to trigger date type. If the user enters 2 digits in the
6836 ## year position, the control will expand the value to four digits, using numerals below
6837 ## 50 as 21st century (20+nn) and less than 50 as 20th century (19+nn).
6838 ## Also, new optional parameter datestyle = set to one of {MDY|DMY|YDM}
6839 ## 3. revalid parameter renamed validRegex to conform to standard for all validation
6840 ## parameters (see 2 new ones below).
6841 ## 4. New optional init parameter = validRange. Used only for int/dec (numeric) types.
6842 ## Allows the developer to specify a valid low/high range of values.
6843 ## 5. New optional init parameter = validList. Used for character types. Allows developer
6844 ## to send a list of values to the control to be used for specific validation.
6845 ## See the Last Name Only example - it is list restricted to Smith/Jones/Williams.
6846 ## 6. Date type fields now use wxDateTime's parser to validate the date and time.
6847 ## This works MUCH better than my kludgy regex!! Thanks to Robin Dunn for pointing
6848 ## me toward this solution!
6849 ## 7. Date fields now automatically expand 2-digit years when it can. For example,
6850 ## if the user types "03/10/67", then "67" will auto-expand to "1967". If a two-year
6851 ## date is entered it will be expanded in any case when the user tabs out of the
6852 ## field.
6853 ## 8. New class functions: SetValidBackgroundColor, SetInvalidBackgroundColor, SetEmptyBackgroundColor,
6854 ## SetSignedForeColor allow accessto override default class coloring behavior.
6855 ## 9. Documentation updated and improved.
6856 ## 10. Demo - page 2 is now a wxFrame class instead of a wxPyApp class. Works better.
6857 ## Two new options (checkboxes) - test highlight empty and disallow empty.
6858 ## 11. Home and End now work more intuitively, moving to the first and last user-entry
6859 ## value, respectively.
6860 ## 12. New class function: SetRequired(bool). Sets the control's entry required flag
6861 ## (i.e. disallow empty values if True).
6862 ##
6863 ## Version 0.0.5
6864 ## 1. get_plainValue method renamed to GetPlainValue following the wxWindows
6865 ## StudlyCaps(tm) standard (thanks Paul Moore). ;)
6866 ## 2. New format code 'F' causes the control to auto-fit (auto-size) itself
6867 ## based on the length of the mask template.
6868 ## 3. Class now supports "autoformat" codes. These can be passed to the class
6869 ## on instantiation using the parameter autoformat="code". If the code is in
6870 ## the dictionary, it will self set the mask, formatting, and validation string.
6871 ## I have included a number of samples, but I am hoping that someone out there
6872 ## can help me to define a whole bunch more.
6873 ## 4. I have added a second page to the demo (as well as a second demo class, test2)
6874 ## to showcase how autoformats work. The way they self-format and self-size is,
6875 ## I must say, pretty cool.
6876 ## 5. Comments added and some internal cosmetic revisions re: matching the code
6877 ## standards for class submission.
6878 ## 6. Regex validation is now done in real time - field turns yellow immediately
6879 ## and stays yellow until the entered value is valid
6880 ## 7. Cursor now skips over template characters in a more intuitive way (before the
6881 ## next keypress).
6882 ## 8. Change, Keypress and LostFocus methods added for convenience of subclasses.
6883 ## Developer may use these methods which will be called after EVT_TEXT, EVT_CHAR,
6884 ## and EVT_KILL_FOCUS, respectively.
6885 ## 9. Decimal and numeric handlers have been rewritten and now work more intuitively.
6886 ##
6887 ## Version 0.0.4
6888 ## 1. New .IsEmpty() method returns True if the control's value is equal to the
6889 ## blank template string
6890 ## 2. Control now supports a new init parameter: revalid. Pass a regular expression
6891 ## that the value will have to match when the control loses focus. If invalid,
6892 ## the control's BackgroundColor will turn yellow, and an internal flag is set (see next).
6893 ## 3. Demo now shows revalid functionality. Try entering a partial value, such as a
6894 ## partial social security number.
6895 ## 4. New .IsValid() value returns True if the control is empty, or if the value matches
6896 ## the revalid expression. If not, .IsValid() returns False.
6897 ## 5. Decimal values now collapse to decimal with '.00' on losefocus if the user never
6898 ## presses the decimal point.
6899 ## 6. Cursor now goes to the beginning of the field if the user clicks in an
6900 ## "empty" field intead of leaving the insertion point in the middle of the
6901 ## field.
6902 ## 7. New "N" mask type includes upper and lower chars plus digits. a-zA-Z0-9.
6903 ## 8. New formatcodes init parameter replaces other init params and adds functions.
6904 ## String passed to control on init controls:
6905 ## _ Allow spaces
6906 ## ! Force upper
6907 ## ^ Force lower
6908 ## R Show negative #s in red
6909 ## , Group digits
6910 ## - Signed numerals
6911 ## 0 Numeric fields get leading zeros
6912 ## 9. Ctrl-X in any field clears the current value.
6913 ## 10. Code refactored and made more modular (esp in OnChar method). Should be more
6914 ## easy to read and understand.
6915 ## 11. Demo enhanced.
6916 ## 12. Now has _doc_.
6917 ##
6918 ## Version 0.0.3
6919 ## 1. GetPlainValue() now returns the value without the template characters;
6920 ## so, for example, a social security number (123-33-1212) would return as
6921 ## 123331212; also removes white spaces from numeric/decimal values, so
6922 ## "- 955.32" is returned "-955.32". Press ctrl-S to see the plain value.
6923 ## 2. Press '.' in an integer style masked control and truncate any trailing digits.
6924 ## 3. Code moderately refactored. Internal names improved for clarity. Additional
6925 ## internal documentation.
6926 ## 4. Home and End keys now supported to move cursor to beginning or end of field.
6927 ## 5. Un-signed integers and decimals now supported.
6928 ## 6. Cosmetic improvements to the demo.
6929 ## 7. Class renamed to MaskedTextCtrl.
6930 ## 8. Can now specify include characters that will override the basic
6931 ## controls: for example, includeChars = "@." for email addresses
6932 ## 9. Added mask character 'C' -> allow any upper or lowercase character
6933 ## 10. .SetSignColor(str:color) sets the foreground color for negative values
6934 ## in signed controls (defaults to red)
6935 ## 11. Overview documentation written.
6936 ##
6937 ## Version 0.0.2
6938 ## 1. Tab now works properly when pressed in last position
6939 ## 2. Decimal types now work (e.g. #####.##)
6940 ## 3. Signed decimal or numeric values supported (i.e. negative numbers)
6941 ## 4. Negative decimal or numeric values now can show in red.
6942 ## 5. Can now specify an "exclude list" with the excludeChars parameter.
6943 ## See date/time formatted example - you can only enter A or P in the
6944 ## character mask space (i.e. AM/PM).
6945 ## 6. Backspace now works properly, including clearing data from a selected
6946 ## region but leaving template characters intact. Also delete key.
6947 ## 7. Left/right arrows now work properly.
6948 ## 8. Removed EventManager call from test so demo should work with wxPython 2.3.3
6949 ##