1 #---------------------------------------------------------------------------- 
   2 # Name:         wxPython.lib.masked.numctrl.py 
   5 # Copyright:   (c) 2003 by Will Sadkin 
   7 # License:     wxWidgets license 
   8 #---------------------------------------------------------------------------- 
  10 #   This was written to provide a numeric edit control for wxPython that 
  11 #   does things like right-insert (like a calculator), and does grouping, etc. 
  12 #   (ie. the features of masked.TextCtrl), but allows Get/Set of numeric 
  13 #   values, rather than text. 
  15 #   Masked.NumCtrl permits integer, and floating point values to be set 
  16 #   retrieved or set via .GetValue() and .SetValue() (type chosen based on 
  17 #   fraction width, and provides an masked.EVT_NUM() event function for trapping 
  18 #   changes to the control. 
  20 #   It supports negative numbers as well as the naturals, and has the option 
  21 #   of not permitting leading zeros or an empty control; if an empty value is 
  22 #   not allowed, attempting to delete the contents of the control will result 
  23 #   in a (selected) value of zero, thus preserving a legitimate numeric value. 
  24 #   Similarly, replacing the contents of the control with '-' will result in 
  25 #   a selected (absolute) value of -1. 
  27 #   masked.NumCtrl also supports range limits, with the option of either 
  28 #   enforcing them or simply coloring the text of the control if the limits 
  31 #   masked.NumCtrl is intended to support fixed-point numeric entry, and 
  32 #   is derived from BaseMaskedTextCtrl.  As such, it supports a limited range 
  33 #   of values to comply with a fixed-width entry mask. 
  34 #---------------------------------------------------------------------------- 
  35 # 12/09/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
  37 # o Updated for wx namespace 
  39 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
  41 # o wxMaskedEditMixin -> MaskedEditMixin 
  42 # o wxMaskedTextCtrl -> masked.TextCtrl 
  43 # o wxMaskedNumNumberUpdatedEvent -> masked.NumberUpdatedEvent 
  44 # o wxMaskedNumCtrl -> masked.NumCtrl 
  49 <B>masked.NumCtrl:</B> 
  51 <LI>allows you to get and set integer or floating point numbers as value,</LI> 
  52 <LI>provides bounds support and optional value limiting,</LI> 
  53 <LI>has the right-insert input style that MaskedTextCtrl supports,</LI> 
  54 <LI>provides optional automatic grouping, sign control and format, grouping and decimal 
  55 character selection, etc. etc.</LI> 
  58 Being derived from MaskedTextCtrl, the control only allows 
  59 fixed-point  notation.  That is, it has a fixed (though reconfigurable) 
  60 maximum width for the integer portion and optional fixed width 
  65     <B>masked.NumCtrl</B>( 
  68          pos = wx.DefaultPosition, 
  69          size = wx.DefaultSize, 
  71          validator = wx.DefaultValidator, 
  72          name = "masked.number", 
  73          <B>integerWidth</B> = 10, 
  74          <B>fractionWidth</B> = 0, 
  75          <B>allowNone</B> = False, 
  76          <B>allowNegative</B> = True, 
  77          <B>useParensForNegatives</B> = False, 
  78          <B>groupDigits</B> = False, 
  79          <B>groupChar</B> = ',', 
  80          <B>decimalChar</B> = '.', 
  83          <B>limited</B> = False, 
  84          <B>selectOnEntry</b> = True, 
  85          <B>foregroundColour</B> = "Black", 
  86          <B>signedForegroundColour</B> = "Red", 
  87          <B>emptyBackgroundColour</B> = "White", 
  88          <B>validBackgroundColour</B> = "White", 
  89          <B>invalidBackgroundColour</B> = "Yellow", 
  90          <B>autoSize</B> = True 
  95     <DD>If no initial value is set, the default will be zero, or 
  96         the minimum value, if specified.  If an illegal string is specified, 
  97         a ValueError will result. (You can always later set the initial 
  98         value with SetValue() after instantiation of the control.) 
 100     <DL><B>integerWidth</B> 
 101     <DD>Indicates how many places to the right of any decimal point 
 102         should be allowed in the control.  This will, perforce, limit 
 103         the size of the values that can be entered. This number need 
 104         not include space for grouping characters or the sign, if either 
 105         of these options are enabled, as the resulting underlying 
 106         mask is automatically by the control.  The default of 10 
 107         will allow any 32 bit integer value.  The minimum value 
 108         for integerWidth is 1. 
 110     <DL><B>fractionWidth</B> 
 111     <DD>Indicates how many decimal places to show for numeric value. 
 112         If default (0), then the control will display and return only 
 113         integer or long values. 
 116     <DD>Boolean indicating whether or not the control is allowed to be 
 117         empty, representing a value of None for the control. 
 119     <DT><B>allowNegative</B> 
 120     <DD>Boolean indicating whether or not control is allowed to hold 
 123     <DT><B>useParensForNegatives</B> 
 124     <DD>If true, this will cause negative numbers to be displayed with ()s 
 125         rather than -, (although '-' will still trigger a negative number.) 
 127     <DT><B>groupDigits</B> 
 128     <DD>Indicates whether or not grouping characters should be allowed and/or 
 129         inserted when leaving the control or the decimal character is entered. 
 132     <DD>What grouping character will be used if allowed. (By default ',') 
 134     <DT><B>decimalChar</B> 
 135     <DD>If fractionWidth is > 0, what character will be used to represent 
 136         the decimal point.  (By default '.') 
 139     <DD>The minimum value that the control should allow.  This can be also be 
 140         adjusted with SetMin().  If the control is not limited, any value 
 141         below this bound will result in a background colored with the current 
 142         invalidBackgroundColour.  If the min specified will not fit into the 
 143         control, the min setting will be ignored. 
 146     <DD>The maximum value that the control should allow.  This can be 
 147         adjusted with SetMax().  If the control is not limited, any value 
 148         above this bound will result in a background colored with the current 
 149         invalidBackgroundColour.  If the max specified will not fit into the 
 150         control, the max setting will be ignored. 
 153     <DD>Boolean indicating whether the control prevents values from 
 154         exceeding the currently set minimum and maximum values (bounds). 
 155         If False and bounds are set, out-of-bounds values will 
 156         result in a background colored with the current invalidBackgroundColour. 
 158     <DT><B>selectOnEntry</B> 
 159     <DD>Boolean indicating whether or not the value in each field of the 
 160         control should be automatically selected (for replacement) when 
 161         that field is entered, either by cursor movement or tabbing. 
 162         This can be desirable when using these controls for rapid data entry. 
 164     <DT><B>foregroundColour</B> 
 165     <DD>Color value used for positive values of the control. 
 167     <DT><B>signedForegroundColour</B> 
 168     <DD>Color value used for negative values of the control. 
 170     <DT><B>emptyBackgroundColour</B> 
 171     <DD>What background color to use when the control is considered 
 172         "empty." (allow_none must be set to trigger this behavior.) 
 174     <DT><B>validBackgroundColour</B> 
 175     <DD>What background color to use when the control value is 
 178     <DT><B>invalidBackgroundColour</B> 
 179     <DD>Color value used for illegal values or values out-of-bounds of the 
 180         control when the bounds are set but the control is not limited. 
 183     <DD>Boolean indicating whether or not the control should set its own 
 184         width based on the integer and fraction widths.  True by default. 
 185         <B><I>Note:</I></B> Setting this to False will produce seemingly odd 
 186         behavior unless the control is large enough to hold the maximum 
 187         specified value given the widths and the sign positions; if not, 
 188         the control will appear to "jump around" as the contents scroll. 
 189         (ie. autoSize is highly recommended.) 
 193 <DT><B>masked.EVT_NUM(win, id, func)</B> 
 194 <DD>Respond to a EVT_COMMAND_MASKED_NUMBER_UPDATED event, generated when 
 195 the value changes. Notice that this event will always be sent when the 
 196 control's contents changes - whether this is due to user input or 
 197 comes from the program itself (for example, if SetValue() is called.) 
 200 <DT><B>SetValue(int|long|float|string)</B> 
 201 <DD>Sets the value of the control to the value specified, if 
 202 possible.  The resulting actual value of the control may be 
 203 altered to conform to the format of the control, changed 
 204 to conform with the bounds set on the control if limited, 
 205 or colored if not limited but the value is out-of-bounds. 
 206 A ValueError exception will be raised if an invalid value 
 209 <DT><B>GetValue()</B> 
 210 <DD>Retrieves the numeric value from the control.  The value 
 211 retrieved will be either be returned as a long if the 
 212 fractionWidth is 0, or a float otherwise. 
 215 <DT><B>SetParameters(**kwargs)</B> 
 216 <DD>Allows simultaneous setting of various attributes 
 217 of the control after construction.  Keyword arguments 
 218 allowed are the same parameters as supported in the constructor. 
 221 <DT><B>SetIntegerWidth(value)</B> 
 222 <DD>Resets the width of the integer portion of the control.  The 
 223 value must be >= 1, or an AttributeError exception will result. 
 224 This value should account for any grouping characters that might 
 225 be inserted (if grouping is enabled), but does not need to account 
 226 for the sign, as that is handled separately by the control. 
 227 <DT><B>GetIntegerWidth()</B> 
 228 <DD>Returns the current width of the integer portion of the control, 
 229 not including any reserved sign position. 
 232 <DT><B>SetFractionWidth(value)</B> 
 233 <DD>Resets the width of the fractional portion of the control.  The 
 234 value must be >= 0, or an AttributeError exception will result.  If 
 235 0, the current value of the control will be truncated to an integer 
 237 <DT><B>GetFractionWidth()</B> 
 238 <DD>Returns the current width of the fractional portion of the control. 
 241 <DT><B>SetMin(min=None)</B> 
 242 <DD>Resets the minimum value of the control.  If a value of <I>None</I> 
 243 is provided, then the control will have no explicit minimum value. 
 244 If the value specified is greater than the current maximum value, 
 245 then the function returns False and the minimum will not change from 
 246 its current setting.  On success, the function returns True. 
 248 If successful and the current value is lower than the new lower 
 249 bound, if the control is limited, the value will be automatically 
 250 adjusted to the new minimum value; if not limited, the value in the 
 251 control will be colored as invalid. 
 253 If min > the max value allowed by the width of the control, 
 254 the function will return False, and the min will not be set. 
 257 <DD>Gets the current lower bound value for the control. 
 258 It will return None if no lower bound is currently specified. 
 261 <DT><B>SetMax(max=None)</B> 
 262 <DD>Resets the maximum value of the control. If a value of <I>None</I> 
 263 is provided, then the control will have no explicit maximum value. 
 264 If the value specified is less than the current minimum value, then 
 265 the function returns False and the maximum will not change from its 
 266 current setting. On success, the function returns True. 
 268 If successful and the current value is greater than the new upper 
 269 bound, if the control is limited the value will be automatically 
 270 adjusted to this maximum value; if not limited, the value in the 
 271 control will be colored as invalid. 
 273 If max > the max value allowed by the width of the control, 
 274 the function will return False, and the max will not be set. 
 277 <DD>Gets the current upper bound value for the control. 
 278 It will return None if no upper bound is currently specified. 
 281 <DT><B>SetBounds(min=None,max=None)</B> 
 282 <DD>This function is a convenience function for setting the min and max 
 283 values at the same time.  The function only applies the maximum bound 
 284 if setting the minimum bound is successful, and returns True 
 285 only if both operations succeed.  <B><I>Note:</I></B> leaving out an argument 
 286 will remove the corresponding bound. 
 287 <DT><B>GetBounds()</B> 
 288 <DD>This function returns a two-tuple (min,max), indicating the 
 289 current bounds of the control.  Each value can be None if 
 290 that bound is not set. 
 293 <DT><B>IsInBounds(value=None)</B> 
 294 <DD>Returns <I>True</I> if no value is specified and the current value 
 295 of the control falls within the current bounds.  This function can also 
 296 be called with a value to see if that value would fall within the current 
 297 bounds of the given control. 
 300 <DT><B>SetLimited(bool)</B> 
 301 <DD>If called with a value of True, this function will cause the control 
 302 to limit the value to fall within the bounds currently specified. 
 303 If the control's value currently exceeds the bounds, it will then 
 304 be limited accordingly. 
 305 If called with a value of False, this function will disable value 
 306 limiting, but coloring of out-of-bounds values will still take 
 307 place if bounds have been set for the control. 
 308 <DT><B>GetLimited()</B> 
 309 <DT><B>IsLimited()</B> 
 310 <DD>Returns <I>True</I> if the control is currently limiting the 
 311 value to fall within the current bounds. 
 314 <DT><B>SetAllowNone(bool)</B> 
 315 <DD>If called with a value of True, this function will cause the control 
 316 to allow the value to be empty, representing a value of None. 
 317 If called with a value of False, this function will prevent the value 
 318 from being None.  If the value of the control is currently None, 
 319 ie. the control is empty, then the value will be changed to that 
 320 of the lower bound of the control, or 0 if no lower bound is set. 
 321 <DT><B>GetAllowNone()</B> 
 322 <DT><B>IsNoneAllowed()</B> 
 323 <DD>Returns <I>True</I> if the control currently allows its 
 327 <DT><B>SetAllowNegative(bool)</B> 
 328 <DD>If called with a value of True, this function will cause the 
 329 control to allow the value to be negative (and reserve space for 
 330 displaying the sign. If called with a value of False, and the 
 331 value of the control is currently negative, the value of the 
 332 control will be converted to the absolute value, and then 
 333 limited appropriately based on the existing bounds of the control 
 335 <DT><B>GetAllowNegative()</B> 
 336 <DT><B>IsNegativeAllowed()</B> 
 337 <DD>Returns <I>True</I> if the control currently permits values 
 341 <DT><B>SetGroupDigits(bool)</B> 
 342 <DD>If called with a value of True, this will make the control 
 343 automatically add and manage grouping characters to the presented 
 344 value in integer portion of the control. 
 345 <DT><B>GetGroupDigits()</B> 
 346 <DT><B>IsGroupingAllowed()</B> 
 347 <DD>Returns <I>True</I> if the control is currently set to group digits. 
 350 <DT><B>SetGroupChar()</B> 
 351 <DD>Sets the grouping character for the integer portion of the 
 352 control.  (The default grouping character this is ','. 
 353 <DT><B>GetGroupChar()</B> 
 354 <DD>Returns the current grouping character for the control. 
 357 <DT><B>SetSelectOnEntry()</B> 
 358 <DD>If called with a value of <I>True</I>, this will make the control 
 359 automatically select the contents of each field as it is entered 
 360 within the control.  (The default is True.) 
 361 <DT><B>GetSelectOnEntry()</B> 
 362 <DD>Returns <I>True</I> if the control currently auto selects 
 363 the field values on entry. 
 366 <DT><B>SetAutoSize(bool)</B> 
 367 <DD>Resets the autoSize attribute of the control. 
 368 <DT><B>GetAutoSize()</B> 
 369 <DD>Returns the current state of the autoSize attribute for the control. 
 382 from sys 
import maxint
 
 383 MAXINT 
= maxint     
# (constants should be in upper case) 
 386 from wx
.tools
.dbg 
import Logger
 
 387 from wx
.lib
.masked 
import MaskedEditMixin
, Field
, BaseMaskedTextCtrl
 
 391 #---------------------------------------------------------------------------- 
 393 wxEVT_COMMAND_MASKED_NUMBER_UPDATED 
= wx
.NewEventType() 
 394 EVT_NUM 
= wx
.PyEventBinder(wxEVT_COMMAND_MASKED_NUMBER_UPDATED
, 1) 
 396 #---------------------------------------------------------------------------- 
 398 class NumberUpdatedEvent(wx
.PyCommandEvent
): 
 399     def __init__(self
, id, value 
= 0, object=None): 
 400         wx
.PyCommandEvent
.__init
__(self
, wxEVT_COMMAND_MASKED_NUMBER_UPDATED
, id) 
 403         self
.SetEventObject(object) 
 406         """Retrieve the value of the control at the time 
 407         this event was generated.""" 
 411 #---------------------------------------------------------------------------- 
 412 class NumCtrlAccessorsMixin
: 
 413     # Define masked.NumCtrl's list of attributes having their own 
 414     # Get/Set functions, ignoring those that make no sense for 
 415     # an numeric control. 
 416     exposed_basectrl_params 
= ( 
 420          'useParensForNegatives', 
 426          'signedForegroundColour', 
 427          'emptyBackgroundColour', 
 428          'validBackgroundColour', 
 429          'invalidBackgroundColour', 
 435     for param 
in exposed_basectrl_params
: 
 436         propname 
= param
[0].upper() + param
[1:] 
 437         exec('def Set%s(self, value): self.SetCtrlParameters(%s=value)' % (propname
, param
)) 
 438         exec('def Get%s(self): return self.GetCtrlParameter("%s")''' % (propname, param)) 
 440         if param.find('Colour
') != -1: 
 441             # add non-british spellings, for backward-compatibility 
 442             propname.replace('Colour
', 'Color
') 
 444             exec('def Set
%s(self
, value
): self
.SetCtrlParameters(%s=value
)' % (propname, param)) 
 445             exec('def Get
%s(self
): return self
.GetCtrlParameter("%s")''' % (propname, param)) 
 449 #---------------------------------------------------------------------------- 
 451 class NumCtrl(BaseMaskedTextCtrl, NumCtrlAccessorsMixin): 
 454     valid_ctrl_params = { 
 455         'integerWidth': 10,                 # by default allow all 32-bit integers 
 456         'fractionWidth': 0,                 # by default, use integers 
 457         'decimalChar': '.',                 # by default, use '.' for decimal point 
 458         'allowNegative': True,              # by default, allow negative numbers 
 459         'useParensForNegatives': False,     # by default, use '-' to indicate negatives 
 460         'groupDigits': True,                # by default, don't insert grouping 
 461         'groupChar': ',',                   # by default, use ',' for grouping 
 462         'min': None,                        # by default, no bounds set 
 464         'limited': False,                   # by default, no limiting even if bounds set 
 465         'allowNone': False,                 # by default, don't allow empty value 
 466         'selectOnEntry': True,              # by default, select the value of each field on entry 
 467         'foregroundColour': "Black", 
 468         'signedForegroundColour': "Red", 
 469         'emptyBackgroundColour': "White", 
 470         'validBackgroundColour': "White", 
 471         'invalidBackgroundColour': "Yellow", 
 472         'useFixedWidthFont': True,          # by default, use a fixed-width font 
 473         'autoSize': True,                   # by default, set the width of the control based on the mask 
 478                 self, parent, id=-1, value = 0, 
 479                 pos = wx.DefaultPosition, size = wx.DefaultSize, 
 480                 style = wx.TE_PROCESS_TAB, validator = wx.DefaultValidator, 
 484 ##        dbg('masked.NumCtrl::__init__', indent=1) 
 486         # Set defaults for control: 
 487 ##        dbg('setting defaults:') 
 488         for key, param_value in NumCtrl.valid_ctrl_params.items(): 
 489             # This is done this way to make setattr behave consistently with 
 490             # "private attribute" name mangling 
 491             setattr(self, '_' + key, copy.copy(param_value)) 
 493         # Assign defaults for all attributes: 
 494         init_args = copy.deepcopy(NumCtrl.valid_ctrl_params) 
 495 ##        dbg('kwargs:', kwargs) 
 496         for key, param_value in kwargs.items(): 
 497             key = key.replace('Color', 'Colour') 
 498             if key not in NumCtrl.valid_ctrl_params.keys(): 
 499                 raise AttributeError('invalid keyword argument "%s"' % key) 
 501                 init_args[key] = param_value 
 502 ##        dbg('init_args:', indent=1) 
 503         for key, param_value in init_args.items(): 
 504 ##            dbg('%s:' % key, param_value) 
 508         # Process initial fields for the control, as part of construction: 
 509         if type(init_args['integerWidth']) != types.IntType: 
 510             raise AttributeError('invalid integerWidth (%s) specified; expected integer' % repr(init_args['integerWidth'])) 
 511         elif init_args['integerWidth'] < 1: 
 512             raise AttributeError('invalid integerWidth (%s) specified; must be > 0' % repr(init_args['integerWidth'])) 
 516         if init_args.has_key('fractionWidth'): 
 517             if type(init_args['fractionWidth']) != types.IntType: 
 518                 raise AttributeError('invalid fractionWidth (%s) specified; expected integer' % repr(self._fractionWidth)) 
 519             elif init_args['fractionWidth'] < 0: 
 520                 raise AttributeError('invalid fractionWidth (%s) specified; must be >= 0' % repr(init_args['fractionWidth'])) 
 521             self._fractionWidth = init_args['fractionWidth'] 
 523         if self._fractionWidth: 
 524             fracmask = '.' + '#{%d}' % self._fractionWidth 
 525 ##            dbg('fracmask:', fracmask) 
 526             fields[1] = Field(defaultValue='0'*self._fractionWidth) 
 530         self._integerWidth = init_args['integerWidth'] 
 531         if init_args['groupDigits']: 
 532             self._groupSpace = (self._integerWidth - 1) / 3 
 535         intmask = '#{%d}' % (self._integerWidth + self._groupSpace) 
 536         if self._fractionWidth: 
 540         fields[0] = Field(formatcodes='r<>', emptyInvalid=emptyInvalid) 
 541 ##        dbg('intmask:', intmask) 
 543         # don't bother to reprocess these arguments: 
 544         del init_args['integerWidth'] 
 545         del init_args['fractionWidth'] 
 547         self._autoSize = init_args['autoSize'] 
 554         mask = intmask+fracmask 
 556         # initial value of state vars 
 559         self._typedSign = False 
 561         # Construct the base control: 
 562         BaseMaskedTextCtrl.__init__( 
 563                 self, parent, id, '', 
 564                 pos, size, style, validator, name, 
 566                 formatcodes = formatcodes, 
 568                 validFunc=self.IsInBounds, 
 569                 setupEventHandling = False) 
 571         self.Bind(wx.EVT_SET_FOCUS, self._OnFocus )        ## defeat automatic full selection 
 572         self.Bind(wx.EVT_KILL_FOCUS, self._OnKillFocus )   ## run internal validator 
 573         self.Bind(wx.EVT_LEFT_DCLICK, self._OnDoubleClick)  ## select field under cursor on dclick 
 574         self.Bind(wx.EVT_RIGHT_UP, self._OnContextMenu )    ## bring up an appropriate context menu 
 575         self.Bind(wx.EVT_KEY_DOWN, self._OnKeyDown )       ## capture control events not normally seen, eg ctrl-tab. 
 576         self.Bind(wx.EVT_CHAR, self._OnChar )              ## handle each keypress 
 577         self.Bind(wx.EVT_TEXT, self.OnTextChange )      ## color control appropriately & keep 
 578                                                            ## track of previous value for undo 
 580         # Establish any additional parameters, with appropriate error checking 
 581         self.SetParameters(**init_args) 
 583         # Set the value requested (if possible) 
 584 ##        wxCallAfter(self.SetValue, value) 
 587         # Ensure proper coloring: 
 589 ##        dbg('finished NumCtrl::__init__', indent=0) 
 592     def SetParameters(self, **kwargs): 
 594         This routine is used to initialize and reconfigure the control: 
 596 ##        dbg('NumCtrl::SetParameters', indent=1) 
 597         maskededit_kwargs = {} 
 598         reset_fraction_width = False 
 601         if( (kwargs.has_key('integerWidth') and kwargs['integerWidth'] != self._integerWidth) 
 602             or (kwargs.has_key('fractionWidth') and kwargs['fractionWidth'] != self._fractionWidth) 
 603             or (kwargs.has_key('groupDigits') and kwargs['groupDigits'] != self._groupDigits) 
 604             or (kwargs.has_key('autoSize') and kwargs['autoSize'] != self._autoSize) ): 
 608             if kwargs.has_key('fractionWidth'): 
 609                 if type(kwargs['fractionWidth']) != types.IntType: 
 610                     raise AttributeError('invalid fractionWidth (%s) specified; expected integer' % repr(kwargs['fractionWidth'])) 
 611                 elif kwargs['fractionWidth'] < 0: 
 612                     raise AttributeError('invalid fractionWidth (%s) specified; must be >= 0' % repr(kwargs['fractionWidth'])) 
 614                     if self._fractionWidth != kwargs['fractionWidth']: 
 615                         self._fractionWidth = kwargs['fractionWidth'] 
 617             if self._fractionWidth: 
 618                 fracmask = '.' + '#{%d}' % self._fractionWidth 
 619                 fields[1] = Field(defaultValue='0'*self._fractionWidth) 
 624 ##            dbg('fracmask:', fracmask) 
 626             if kwargs.has_key('integerWidth'): 
 627                 if type(kwargs['integerWidth']) != types.IntType: 
 629                     raise AttributeError('invalid integerWidth (%s) specified; expected integer' % repr(kwargs['integerWidth'])) 
 630                 elif kwargs['integerWidth'] < 0: 
 632                     raise AttributeError('invalid integerWidth (%s) specified; must be > 0' % repr(kwargs['integerWidth'])) 
 634                     self._integerWidth = kwargs['integerWidth'] 
 636             if kwargs.has_key('groupDigits'): 
 637                 self._groupDigits = kwargs['groupDigits'] 
 639             if self._groupDigits: 
 640                 self._groupSpace = (self._integerWidth - 1) / 3 
 644             intmask = '#{%d}' % (self._integerWidth + self._groupSpace) 
 645 ##            dbg('intmask:', intmask) 
 646             fields[0] = Field(formatcodes='r<>', emptyInvalid=emptyInvalid) 
 647             maskededit_kwargs['fields'] = fields 
 649             # don't bother to reprocess these arguments: 
 650             if kwargs.has_key('integerWidth'): 
 651                 del kwargs['integerWidth'] 
 652             if kwargs.has_key('fractionWidth'): 
 653                 del kwargs['fractionWidth'] 
 655             maskededit_kwargs['mask'] = intmask+fracmask 
 657         if kwargs.has_key('groupChar'): 
 658             old_groupchar = self._groupChar     # save so we can reformat properly 
 659 ##            dbg("old_groupchar: '%s'" % old_groupchar) 
 660             maskededit_kwargs['groupChar'] = kwargs['groupChar'] 
 661         if kwargs.has_key('decimalChar'): 
 662             old_decimalchar = self._decimalChar 
 663 ##            dbg("old_decimalchar: '%s'" % old_decimalchar) 
 664             maskededit_kwargs['decimalChar'] = kwargs['decimalChar'] 
 666         # for all other parameters, assign keyword args as appropriate: 
 667         for key, param_value in kwargs.items(): 
 668             key = key.replace('Color', 'Colour') 
 669             if key not in NumCtrl.valid_ctrl_params.keys(): 
 670                 raise AttributeError('invalid keyword argument "%s"' % key) 
 671             elif key not in MaskedEditMixin.valid_ctrl_params.keys(): 
 672                 setattr(self, '_' + key, param_value) 
 673             elif key in ('mask', 'autoformat'): # disallow explicit setting of mask 
 674                 raise AttributeError('invalid keyword argument "%s"' % key) 
 676                 maskededit_kwargs[key] = param_value 
 677 ##        dbg('kwargs:', kwargs) 
 679         # reprocess existing format codes to ensure proper resulting format: 
 680         formatcodes = self.GetCtrlParameter('formatcodes') 
 681         if kwargs.has_key('allowNegative'): 
 682             if kwargs['allowNegative'] and '-' not in formatcodes: 
 684                 maskededit_kwargs['formatcodes'] = formatcodes 
 685             elif not kwargs['allowNegative'] and '-' in formatcodes: 
 686                 formatcodes = formatcodes.replace('-','') 
 687                 maskededit_kwargs['formatcodes'] = formatcodes 
 689         if kwargs.has_key('groupDigits'): 
 690             if kwargs['groupDigits'] and ',' not in formatcodes: 
 692                 maskededit_kwargs['formatcodes'] = formatcodes 
 693             elif not kwargs['groupDigits'] and ',' in formatcodes: 
 694                 formatcodes = formatcodes.replace(',','') 
 695                 maskededit_kwargs['formatcodes'] = formatcodes 
 697         if kwargs.has_key('selectOnEntry'): 
 698             self._selectOnEntry = kwargs['selectOnEntry'] 
 699 ##            dbg("kwargs['selectOnEntry']?", kwargs['selectOnEntry'], "'S' in formatcodes?", 'S' in formatcodes) 
 700             if kwargs['selectOnEntry'] and 'S' not in formatcodes: 
 702                 maskededit_kwargs['formatcodes'] = formatcodes 
 703             elif not kwargs['selectOnEntry'] and 'S' in formatcodes: 
 704                 formatcodes = formatcodes.replace('S','') 
 705                 maskededit_kwargs['formatcodes'] = formatcodes 
 707         if kwargs.has_key('autoSize'): 
 708             self._autoSize = kwargs['autoSize'] 
 709             if kwargs['autoSize'] and 'F' not in formatcodes: 
 711                 maskededit_kwargs['formatcodes'] = formatcodes 
 712             elif not kwargs['autoSize'] and 'F' in formatcodes: 
 713                 formatcodes = formatcodes.replace('F', '') 
 714                 maskededit_kwargs['formatcodes'] = formatcodes 
 717         if 'r' in formatcodes and self._fractionWidth: 
 718             # top-level mask should only be right insert if no fractional 
 719             # part will be shown; ie. if reconfiguring control, remove 
 720             # previous "global" setting. 
 721             formatcodes = formatcodes.replace('r', '') 
 722             maskededit_kwargs['formatcodes'] = formatcodes 
 725         if kwargs.has_key('limited'): 
 726             if kwargs['limited'] and not self._limited: 
 727                 maskededit_kwargs['validRequired'] = True 
 728             elif not kwargs['limited'] and self._limited: 
 729                 maskededit_kwargs['validRequired'] = False 
 730             self._limited = kwargs['limited'] 
 732 ##        dbg('maskededit_kwargs:', maskededit_kwargs) 
 733         if maskededit_kwargs.keys(): 
 734             self.SetCtrlParameters(**maskededit_kwargs) 
 736         # Record end of integer and place cursor there: 
 737         integerEnd = self._fields[0]._extent[1] 
 738         self.SetInsertionPoint(0) 
 739         self.SetInsertionPoint(integerEnd) 
 740         self.SetSelection(integerEnd, integerEnd) 
 742         # Go ensure all the format codes necessary are present: 
 743         orig_intformat = intformat = self.GetFieldParameter(0, 'formatcodes') 
 744         if 'r' not in intformat: 
 746         if '>' not in intformat: 
 748         if intformat != orig_intformat: 
 749             if self._fractionWidth: 
 750                 self.SetFieldParameters(0, formatcodes=intformat) 
 752                 self.SetCtrlParameters(formatcodes=intformat) 
 754         # Set min and max as appropriate: 
 755         if kwargs.has_key('min'): 
 757             if( self._max is None 
 759                 or (self._max is not None and self._max >= min) ): 
 760 ##                dbg('examining min') 
 763                         textmin = self._toGUI(min, apply_limits = False) 
 765 ##                        dbg('min will not fit into control; ignoring', indent=0) 
 767 ##                dbg('accepted min') 
 770 ##                dbg('ignoring min') 
 774         if kwargs.has_key('max'): 
 776             if( self._min is None 
 778                 or (self._min is not None and self._min <= max) ): 
 779 ##                dbg('examining max') 
 782                         textmax = self._toGUI(max, apply_limits = False) 
 784 ##                        dbg('max will not fit into control; ignoring', indent=0) 
 786 ##                dbg('accepted max') 
 789 ##                dbg('ignoring max') 
 792         if kwargs.has_key('allowNegative'): 
 793             self._allowNegative = kwargs['allowNegative'] 
 795         # Ensure current value of control obeys any new restrictions imposed: 
 796         text = self._GetValue() 
 797 ##        dbg('text value: "%s"' % text) 
 798         if kwargs.has_key('groupChar') and text.find(old_groupchar) != -1: 
 799             text = text.replace(old_groupchar, self._groupChar) 
 800         if kwargs.has_key('decimalChar') and text.find(old_decimalchar) != -1: 
 801             text = text.replace(old_decimalchar, self._decimalChar) 
 802         if text != self._GetValue(): 
 803             wx.TextCtrl.SetValue(self, text) 
 805         value = self.GetValue() 
 807 ##        dbg('self._allowNegative?', self._allowNegative) 
 808         if not self._allowNegative and self._isNeg: 
 810 ##            dbg('abs(value):', value) 
 813         elif not self._allowNone and BaseMaskedTextCtrl.GetValue(self) == '': 
 819         sel_start, sel_to = self.GetSelection() 
 820         if self.IsLimited() and self._min is not None and value < self._min: 
 821 ##            dbg('Set to min value:', self._min) 
 822             self._SetValue(self._toGUI(self._min)) 
 824         elif self.IsLimited() and self._max is not None and value > self._max: 
 825 ##            dbg('Setting to max value:', self._max) 
 826             self._SetValue(self._toGUI(self._max)) 
 828             # reformat current value as appropriate to possibly new conditions 
 829 ##            dbg('Reformatting value:', value) 
 830             sel_start, sel_to = self.GetSelection() 
 831             self._SetValue(self._toGUI(value)) 
 832         self.Refresh() # recolor as appropriate 
 833 ##        dbg('finished NumCtrl::SetParameters', indent=0) 
 837     def _GetNumValue(self, value): 
 839         This function attempts to "clean up" a text value, providing a regularized 
 840         convertable string, via atol() or atof(), for any well-formed numeric text value. 
 842         return value.replace(self._groupChar, '').replace(self._decimalChar, '.').replace('(', '-').replace(')','').strip() 
 845     def GetFraction(self, candidate=None): 
 847         Returns the fractional portion of the value as a float.  If there is no 
 848         fractional portion, the value returned will be 0.0. 
 850         if not self._fractionWidth: 
 853             fracstart, fracend = self._fields[1]._extent 
 854             if candidate is None: 
 855                 value = self._toGUI(BaseMaskedTextCtrl.GetValue(self)) 
 857                 value = self._toGUI(candidate) 
 858             fracstring = value[fracstart:fracend].strip() 
 862                 return string.atof(fracstring) 
 864     def _OnChangeSign(self, event): 
 865 ##        dbg('NumCtrl::_OnChangeSign', indent=1) 
 866         self._typedSign = True 
 867         MaskedEditMixin._OnChangeSign(self, event) 
 871     def _disallowValue(self): 
 872 ##        dbg('NumCtrl::_disallowValue') 
 873         # limited and -1 is out of bounds 
 876         if not wx.Validator_IsSilent(): 
 878         sel_start, sel_to = self._GetSelection() 
 879 ##        dbg('queuing reselection of (%d, %d)' % (sel_start, sel_to)) 
 880         wx.CallAfter(self.SetInsertionPoint, sel_start)      # preserve current selection/position 
 881         wx.CallAfter(self.SetSelection, sel_start, sel_to) 
 883     def _SetValue(self, value): 
 885         This routine supersedes the base masked control _SetValue().  It is 
 886         needed to ensure that the value of the control is always representable/convertable 
 887         to a numeric return value (via GetValue().)  This routine also handles 
 888         automatic adjustment and grouping of the value without explicit intervention 
 892 ##        dbg('NumCtrl::_SetValue("%s")' % value, indent=1) 
 894         if( (self._fractionWidth and value.find(self._decimalChar) == -1) or 
 895             (self._fractionWidth == 0 and value.find(self._decimalChar) != -1) ) : 
 896             value = self._toGUI(value) 
 898         numvalue = self._GetNumValue(value) 
 899 ##        dbg('cleansed value: "%s"' % numvalue) 
 904 ##                dbg('calling base BaseMaskedTextCtrl._SetValue(self, "%s")' % value) 
 905                 BaseMaskedTextCtrl._SetValue(self, value) 
 908             elif self._min > 0 and self.IsLimited(): 
 909                 replacement = self._min 
 912 ##            dbg('empty value; setting replacement:', replacement) 
 914         if replacement is None: 
 915             # Go get the integer portion about to be set and verify its validity 
 916             intstart, intend = self._fields[0]._extent 
 917 ##            dbg('intstart, intend:', intstart, intend) 
 918 ##            dbg('raw integer:"%s"' % value[intstart:intend]) 
 919             int = self._GetNumValue(value[intstart:intend]) 
 920             numval = self._fromGUI(value) 
 922 ##            dbg('integer: "%s"' % int) 
 924                 fracval = self.GetFraction(value) 
 925             except ValueError, e: 
 926 ##                dbg('Exception:', e, 'must be out of bounds; disallow value') 
 927                 self._disallowValue() 
 932 ##                dbg('self._isNeg?', self._isNeg) 
 933                 if int == '-' and self._oldvalue < 0 and not self._typedSign: 
 934 ##                    dbg('just a negative sign; old value < 0; setting replacement of 0') 
 937                 elif int[:2] == '-0' and self._fractionWidth == 0: 
 938                     if self._oldvalue < 0: 
 939 ##                        dbg('-0; setting replacement of 0') 
 942                     elif not self._limited or (self._min < -1 and self._max >= -1): 
 943 ##                        dbg('-0; setting replacement of -1') 
 947                         # limited and -1 is out of bounds 
 948                         self._disallowValue() 
 952                 elif int == '-' and (self._oldvalue >= 0 or self._typedSign) and self._fractionWidth == 0: 
 953                     if not self._limited or (self._min < -1 and self._max >= -1): 
 954 ##                        dbg('just a negative sign; setting replacement of -1') 
 957                         # limited and -1 is out of bounds 
 958                         self._disallowValue() 
 962                 elif( self._typedSign 
 963                       and int.find('-') != -1 
 965                       and not self._min <= numval <= self._max): 
 966                     # changed sign resulting in value that's now out-of-bounds; 
 968                     self._disallowValue() 
 972             if replacement is None: 
 973                 if int and int != '-': 
 977                         # integer requested is not legal.  This can happen if the user 
 978                         # is attempting to insert a digit in the middle of the control 
 979                         # resulting in something like "   3   45". Disallow such actions: 
 980 ##                        dbg('>>>>>>>>>>>>>>>> "%s" does not convert to a long!' % int) 
 981                         if not wx.Validator_IsSilent(): 
 983                         sel_start, sel_to = self._GetSelection() 
 984 ##                        dbg('queuing reselection of (%d, %d)' % (sel_start, sel_to)) 
 985                         wx.CallAfter(self.SetInsertionPoint, sel_start)      # preserve current selection/position 
 986                         wx.CallAfter(self.SetSelection, sel_start, sel_to) 
 990                     if int[0] == '0' and len(int) > 1: 
 991 ##                        dbg('numvalue: "%s"' % numvalue.replace(' ', '')) 
 992                         if self._fractionWidth: 
 993                             value = self._toGUI(string.atof(numvalue)) 
 995                             value = self._toGUI(string.atol(numvalue)) 
 996 ##                        dbg('modified value: "%s"' % value) 
 998         self._typedSign = False     # reset state var 
1000         if replacement is not None: 
1001             # Value presented wasn't a legal number, but control should do something 
1002             # reasonable instead: 
1003 ##            dbg('setting replacement value:', replacement) 
1004             self._SetValue(self._toGUI(replacement)) 
1005             sel_start = BaseMaskedTextCtrl.GetValue(self).find(str(abs(replacement)))   # find where it put the 1, so we can select it 
1006             sel_to = sel_start + len(str(abs(replacement))) 
1007 ##            dbg('queuing selection of (%d, %d)' %(sel_start, sel_to)) 
1008             wx.CallAfter(self.SetInsertionPoint, sel_start) 
1009             wx.CallAfter(self.SetSelection, sel_start, sel_to) 
1013         # Otherwise, apply appropriate formatting to value: 
1015         # Because we're intercepting the value and adjusting it 
1016         # before a sign change is detected, we need to do this here: 
1017         if '-' in value or '(' in value: 
1022 ##        dbg('value:"%s"' % value, 'self._useParens:', self._useParens) 
1023         if self._fractionWidth: 
1024             adjvalue = self._adjustFloat(self._GetNumValue(value).replace('.',self._decimalChar)) 
1026             adjvalue = self._adjustInt(self._GetNumValue(value)) 
1027 ##        dbg('adjusted value: "%s"' % adjvalue) 
1030         sel_start, sel_to = self._GetSelection()     # record current insertion point 
1031 ##        dbg('calling BaseMaskedTextCtrl._SetValue(self, "%s")' % adjvalue) 
1032         BaseMaskedTextCtrl._SetValue(self, adjvalue) 
1033         # After all actions so far scheduled, check that resulting cursor 
1034         # position is appropriate, and move if not: 
1035         wx.CallAfter(self._CheckInsertionPoint) 
1037 ##        dbg('finished NumCtrl::_SetValue', indent=0) 
1039     def _CheckInsertionPoint(self): 
1040         # If current insertion point is before the end of the integer and 
1041         # its before the 1st digit, place it just after the sign position: 
1042 ##        dbg('NumCtrl::CheckInsertionPoint', indent=1) 
1043         sel_start, sel_to = self._GetSelection() 
1044         text = self._GetValue() 
1045         if sel_to < self._fields[0]._extent[1] and text[sel_to] in (' ', '-', '('): 
1046             text, signpos, right_signpos = self._getSignedValue() 
1047 ##            dbg('setting selection(%d, %d)' % (signpos+1, signpos+1)) 
1048             self.SetInsertionPoint(signpos+1) 
1049             self.SetSelection(signpos+1, signpos+1) 
1053     def _OnErase( self, event ): 
1055         This overrides the base control _OnErase, so that erasing around 
1056         grouping characters auto selects the digit before or after the 
1057         grouping character, so that the erasure does the right thing. 
1059 ##        dbg('NumCtrl::_OnErase', indent=1) 
1061         #if grouping digits, make sure deletes next to group char always 
1062         # delete next digit to appropriate side: 
1063         if self._groupDigits: 
1064             key = event.GetKeyCode() 
1065             value = BaseMaskedTextCtrl.GetValue(self) 
1066             sel_start, sel_to = self._GetSelection() 
1068             if key == wx.WXK_BACK: 
1069                 # if 1st selected char is group char, select to previous digit 
1070                 if sel_start > 0 and sel_start < len(self._mask) and value[sel_start:sel_to] == self._groupChar: 
1071                     self.SetInsertionPoint(sel_start-1) 
1072                     self.SetSelection(sel_start-1, sel_to) 
1074                 # elif previous char is group char, select to previous digit 
1075                 elif sel_start > 1 and sel_start == sel_to and value[sel_start-1:sel_start] == self._groupChar: 
1076                     self.SetInsertionPoint(sel_start-2) 
1077                     self.SetSelection(sel_start-2, sel_to) 
1079             elif key == wx.WXK_DELETE: 
1080                 if( sel_to < len(self._mask) - 2 + (1 *self._useParens) 
1081                     and sel_start == sel_to 
1082                     and value[sel_to] == self._groupChar ): 
1083                     self.SetInsertionPoint(sel_start) 
1084                     self.SetSelection(sel_start, sel_to+2) 
1086                 elif( sel_to < len(self._mask) - 2 + (1 *self._useParens) 
1087                            and value[sel_start:sel_to] == self._groupChar ): 
1088                     self.SetInsertionPoint(sel_start) 
1089                     self.SetSelection(sel_start, sel_to+1) 
1091         BaseMaskedTextCtrl._OnErase(self, event) 
1095     def OnTextChange( self, event ): 
1097         Handles an event indicating that the text control's value 
1098         has changed, and issue EVT_NUM event. 
1099         NOTE: using wxTextCtrl.SetValue() to change the control's 
1100         contents from within a EVT_CHAR handler can cause double 
1101         text events.  So we check for actual changes to the text 
1102         before passing the events on. 
1104 ##        dbg('NumCtrl::OnTextChange', indent=1) 
1105         if not BaseMaskedTextCtrl._OnTextChange(self, event): 
1109         # else... legal value 
1111         value = self.GetValue() 
1112         if value != self._oldvalue: 
1114                 self.GetEventHandler().ProcessEvent( 
1115                     NumberUpdatedEvent( self.GetId(), self.GetValue(), self ) ) 
1119             # let normal processing of the text continue 
1121         self._oldvalue = value # record for next event 
1124     def _GetValue(self): 
1126         Override of BaseMaskedTextCtrl to allow mixin to get the raw text value of the 
1127         control with this function. 
1129         return wx.TextCtrl.GetValue(self) 
1134         Returns the current numeric value of the control. 
1136         return self._fromGUI( BaseMaskedTextCtrl.GetValue(self) ) 
1138     def SetValue(self, value): 
1140         Sets the value of the control to the value specified. 
1141         The resulting actual value of the control may be altered to 
1142         conform with the bounds set on the control if limited, 
1143         or colored if not limited but the value is out-of-bounds. 
1144         A ValueError exception will be raised if an invalid value 
1147         BaseMaskedTextCtrl.SetValue( self, self._toGUI(value) ) 
1150     def SetIntegerWidth(self, value): 
1151         self.SetParameters(integerWidth=value) 
1152     def GetIntegerWidth(self): 
1153         return self._integerWidth 
1155     def SetFractionWidth(self, value): 
1156         self.SetParameters(fractionWidth=value) 
1157     def GetFractionWidth(self): 
1158         return self._fractionWidth 
1162     def SetMin(self, min=None): 
1164         Sets the minimum value of the control.  If a value of None 
1165         is provided, then the control will have no explicit minimum value. 
1166         If the value specified is greater than the current maximum value, 
1167         then the function returns False and the minimum will not change from 
1168         its current setting.  On success, the function returns True. 
1170         If successful and the current value is lower than the new lower 
1171         bound, if the control is limited, the value will be automatically 
1172         adjusted to the new minimum value; if not limited, the value in the 
1173         control will be colored as invalid. 
1175         If min > the max value allowed by the width of the control, 
1176         the function will return False, and the min will not be set. 
1178 ##        dbg('NumCtrl::SetMin(%s)' % repr(min), indent=1) 
1179         if( self._max is None 
1181             or (self._max is not None and self._max >= min) ): 
1183                 self.SetParameters(min=min) 
1194         Gets the lower bound value of the control.  It will return 
1195         None if not specified. 
1200     def SetMax(self, max=None): 
1202         Sets the maximum value of the control. If a value of None 
1203         is provided, then the control will have no explicit maximum value. 
1204         If the value specified is less than the current minimum value, then 
1205         the function returns False and the maximum will not change from its 
1206         current setting. On success, the function returns True. 
1208         If successful and the current value is greater than the new upper 
1209         bound, if the control is limited the value will be automatically 
1210         adjusted to this maximum value; if not limited, the value in the 
1211         control will be colored as invalid. 
1213         If max > the max value allowed by the width of the control, 
1214         the function will return False, and the max will not be set. 
1216         if( self._min is None 
1218             or (self._min is not None and self._min <= max) ): 
1220                 self.SetParameters(max=max) 
1232         Gets the maximum value of the control.  It will return the current 
1233         maximum integer, or None if not specified. 
1238     def SetBounds(self, min=None, max=None): 
1240         This function is a convenience function for setting the min and max 
1241         values at the same time.  The function only applies the maximum bound 
1242         if setting the minimum bound is successful, and returns True 
1243         only if both operations succeed. 
1244         NOTE: leaving out an argument will remove the corresponding bound. 
1246         ret = self.SetMin(min) 
1247         return ret and self.SetMax(max) 
1250     def GetBounds(self): 
1252         This function returns a two-tuple (min,max), indicating the 
1253         current bounds of the control.  Each value can be None if 
1254         that bound is not set. 
1256         return (self._min, self._max) 
1259     def SetLimited(self, limited): 
1261         If called with a value of True, this function will cause the control 
1262         to limit the value to fall within the bounds currently specified. 
1263         If the control's value currently exceeds the bounds, it will then 
1264         be limited accordingly. 
1266         If called with a value of False, this function will disable value 
1267         limiting, but coloring of out-of-bounds values will still take 
1268         place if bounds have been set for the control. 
1270         self.SetParameters(limited = limited) 
1273     def IsLimited(self): 
1275         Returns True if the control is currently limiting the 
1276         value to fall within the current bounds. 
1278         return self._limited 
1280     def GetLimited(self): 
1281         """ (For regularization of property accessors) """ 
1282         return self.IsLimited 
1285     def IsInBounds(self, value=None): 
1287         Returns True if no value is specified and the current value 
1288         of the control falls within the current bounds.  This function can 
1289         also be called with a value to see if that value would fall within 
1290         the current bounds of the given control. 
1292 ##        dbg('IsInBounds(%s)' % repr(value), indent=1) 
1294             value = self.GetValue() 
1297                 value = self._GetNumValue(self._toGUI(value)) 
1298             except ValueError, e: 
1299 ##                dbg('error getting NumValue(self._toGUI(value)):', e, indent=0) 
1301             if value.strip() == '': 
1303             elif self._fractionWidth: 
1304                 value = float(value) 
1310         if min is None: min = value 
1311         if max is None: max = value 
1313         # if bounds set, and value is None, return False 
1314         if value == None and (min is not None or max is not None): 
1315 ##            dbg('finished IsInBounds', indent=0) 
1318 ##            dbg('finished IsInBounds', indent=0) 
1319             return min <= value <= max 
1322     def SetAllowNone(self, allow_none): 
1324         Change the behavior of the validation code, allowing control 
1325         to have a value of None or not, as appropriate.  If the value 
1326         of the control is currently None, and allow_none is False, the 
1327         value of the control will be set to the minimum value of the 
1328         control, or 0 if no lower bound is set. 
1330         self._allowNone = allow_none 
1331         if not allow_none and self.GetValue() is None: 
1333             if min is not None: self.SetValue(min) 
1334             else:               self.SetValue(0) 
1337     def IsNoneAllowed(self): 
1338         return self._allowNone 
1339     def GetAllowNone(self): 
1340         """ (For regularization of property accessors) """ 
1341         return self.IsNoneAllowed() 
1343     def SetAllowNegative(self, value): 
1344         self.SetParameters(allowNegative=value) 
1345     def IsNegativeAllowed(self): 
1346         return self._allowNegative 
1347     def GetAllowNegative(self): 
1348         """ (For regularization of property accessors) """ 
1349         return self.IsNegativeAllowed() 
1351     def SetGroupDigits(self, value): 
1352         self.SetParameters(groupDigits=value) 
1353     def IsGroupingAllowed(self): 
1354         return self._groupDigits 
1355     def GetGroupDigits(self): 
1356         """ (For regularization of property accessors) """ 
1357         return self.IsGroupingAllowed() 
1359     def SetGroupChar(self, value): 
1360         self.SetParameters(groupChar=value) 
1361     def GetGroupChar(self): 
1362         return self._groupChar 
1364     def SetDecimalChar(self, value): 
1365         self.SetParameters(decimalChar=value) 
1366     def GetDecimalChar(self): 
1367         return self._decimalChar 
1369     def SetSelectOnEntry(self, value): 
1370         self.SetParameters(selectOnEntry=value) 
1371     def GetSelectOnEntry(self): 
1372         return self._selectOnEntry 
1374     def SetAutoSize(self, value): 
1375         self.SetParameters(autoSize=value) 
1376     def GetAutoSize(self): 
1377         return self._autoSize 
1380     # (Other parameter accessors are inherited from base class) 
1383     def _toGUI( self, value, apply_limits = True ): 
1385         Conversion function used to set the value of the control; does 
1386         type and bounds checking and raises ValueError if argument is 
1389 ##        dbg('NumCtrl::_toGUI(%s)' % repr(value), indent=1) 
1390         if value is None and self.IsNoneAllowed(): 
1392             return self._template 
1394         elif type(value) in (types.StringType, types.UnicodeType): 
1395             value = self._GetNumValue(value) 
1396 ##            dbg('cleansed num value: "%s"' % value) 
1398                 if self.IsNoneAllowed(): 
1400                     return self._template 
1402 ##                    dbg('exception raised:', e, indent=0) 
1403                     raise ValueError ('NumCtrl requires numeric value, passed %s'% repr(value) ) 
1406                 if self._fractionWidth or value.find('.') != -1: 
1407                     value = float(value) 
1410             except Exception, e: 
1411 ##                dbg('exception raised:', e, indent=0) 
1412                 raise ValueError ('NumCtrl requires numeric value, passed %s'% repr(value) ) 
1414         elif type(value) not in (types.IntType, types.LongType, types.FloatType): 
1417                 'NumCtrl requires numeric value, passed %s'% repr(value) ) 
1419         if not self._allowNegative and value < 0: 
1421                 'control configured to disallow negative values, passed %s'% repr(value) ) 
1423         if self.IsLimited() and apply_limits: 
1426             if not min is None and value < min: 
1429                     'value %d is below minimum value of control'% value ) 
1430             if not max is None and value > max: 
1433                     'value %d exceeds value of control'% value ) 
1435         adjustwidth = len(self._mask) - (1 * self._useParens * self._signOk) 
1436 ##        dbg('len(%s):' % self._mask, len(self._mask)) 
1437 ##        dbg('adjustwidth - groupSpace:', adjustwidth - self._groupSpace) 
1438 ##        dbg('adjustwidth:', adjustwidth) 
1439         if self._fractionWidth == 0: 
1440             s = str(long(value)).rjust(self._integerWidth) 
1442             format = '%' + '%d.%df' % (self._integerWidth+self._fractionWidth+1, self._fractionWidth) 
1443             s = format % float(value) 
1444 ##        dbg('s:"%s"' % s, 'len(s):', len(s)) 
1445         if len(s) > (adjustwidth - self._groupSpace): 
1447             raise ValueError ('value %s exceeds the integer width of the control (%d)' % (s, self._integerWidth)) 
1448         elif s[0] not in ('-', ' ') and self._allowNegative and len(s) == (adjustwidth - self._groupSpace): 
1450             raise ValueError ('value %s exceeds the integer width of the control (%d)' % (s, self._integerWidth)) 
1452         s = s.rjust(adjustwidth).replace('.', self._decimalChar) 
1453         if self._signOk and self._useParens: 
1454             if s.find('-') != -1: 
1455                 s = s.replace('-', '(') + ')' 
1458 ##        dbg('returned: "%s"' % s, indent=0) 
1462     def _fromGUI( self, value ): 
1464         Conversion function used in getting the value of the control. 
1467 ##        dbg('NumCtrl::_fromGUI(%s)' % value, indent=1) 
1468         # One or more of the underlying text control implementations 
1469         # issue an intermediate EVT_TEXT when replacing the control's 
1470         # value, where the intermediate value is an empty string. 
1471         # So, to ensure consistency and to prevent spurious ValueErrors, 
1472         # we make the following test, and react accordingly: 
1474         if value.strip() == '': 
1475             if not self.IsNoneAllowed(): 
1476 ##                dbg('empty value; not allowed,returning 0', indent = 0) 
1477                 if self._fractionWidth: 
1482 ##                dbg('empty value; returning None', indent = 0) 
1485             value = self._GetNumValue(value) 
1486 ##            dbg('Num value: "%s"' % value) 
1487             if self._fractionWidth: 
1490                     return float( value ) 
1492 ##                    dbg("couldn't convert to float; returning None") 
1503                        return long( value ) 
1505 ##                       dbg("couldn't convert to long; returning None") 
1511 ##                    dbg('exception occurred; returning None') 
1515     def _Paste( self, value=None, raise_on_invalid=False, just_return_value=False ): 
1517         Preprocessor for base control paste; if value needs to be right-justified 
1518         to fit in control, do so prior to paste: 
1520 ##        dbg('NumCtrl::_Paste (value = "%s")' % value) 
1522             paste_text = self._getClipboardContents() 
1526         # treat paste as "replace number", if appropriate: 
1527         sel_start, sel_to = self._GetSelection() 
1528         if sel_start == sel_to or self._selectOnEntry and (sel_start, sel_to) == self._fields[0]._extent: 
1529             paste_text = self._toGUI(paste_text) 
1530             self._SetSelection(0, len(self._mask)) 
1532         return MaskedEditMixin._Paste(self, 
1534                                         raise_on_invalid=raise_on_invalid, 
1535                                         just_return_value=just_return_value) 
1539 #=========================================================================== 
1541 if __name__ == '__main__': 
1545     class myDialog(wx.Dialog): 
1546         def __init__(self, parent, id, title, 
1547             pos = wx.DefaultPosition, size = wx.DefaultSize, 
1548             style = wx.DEFAULT_DIALOG_STYLE ): 
1549             wx.Dialog.__init__(self, parent, id, title, pos, size, style) 
1551             self.int_ctrl = NumCtrl(self, wx.NewId(), size=(55,20)) 
1552             self.OK = wx.Button( self, wx.ID_OK, "OK") 
1553             self.Cancel = wx.Button( self, wx.ID_CANCEL, "Cancel") 
1555             vs = wx.BoxSizer( wx.VERTICAL ) 
1556             vs.Add( self.int_ctrl, 0, wx.ALIGN_CENTRE|wx.ALL, 5 ) 
1557             hs = wx.BoxSizer( wx.HORIZONTAL ) 
1558             hs.Add( self.OK, 0, wx.ALIGN_CENTRE|wx.ALL, 5 ) 
1559             hs.Add( self.Cancel, 0, wx.ALIGN_CENTRE|wx.ALL, 5 ) 
1560             vs.Add(hs, 0, wx.ALIGN_CENTRE|wx.ALL, 5 ) 
1562             self.SetAutoLayout( True ) 
1565             vs.SetSizeHints( self ) 
1566             self.Bind(EVT_NUM, self.OnChange, self.int_ctrl) 
1568         def OnChange(self, event): 
1569             print 'value now', event.GetValue() 
1571     class TestApp(wx.App): 
1574                 self.frame = wx.Frame(None, -1, "Test", (20,20), (120,100)  ) 
1575                 self.panel = wx.Panel(self.frame, -1) 
1576                 button = wx.Button(self.panel, -1, "Push Me", (20, 20)) 
1577                 self.Bind(wx.EVT_BUTTON, self.OnClick, button) 
1579                 traceback.print_exc() 
1583         def OnClick(self, event): 
1584             dlg = myDialog(self.panel, -1, "test NumCtrl") 
1585             dlg.int_ctrl.SetValue(501) 
1586             dlg.int_ctrl.SetInsertionPoint(1) 
1587             dlg.int_ctrl.SetSelection(1,2) 
1588             rc = dlg.ShowModal() 
1589             print 'final value', dlg.int_ctrl.GetValue() 
1591             self.frame.Destroy() 
1594             self.frame.Show(True) 
1601         traceback.print_exc() 
1605 ## =============================## 
1606 ##   1. Add support for printf-style format specification. 
1607 ##   2. Add option for repositioning on 'illegal' insertion point. 
1610 ##   1. Fixed .SetIntegerWidth() and .SetFractionWidth() functions. 
1611 ##   2. Added autoSize parameter, to allow manual sizing of the control. 
1612 ##   3. Changed inheritance to use wxBaseMaskedTextCtrl, to remove exposure of 
1613 ##      nonsensical parameter methods from the control, so it will work 
1614 ##      properly with Boa. 
1615 ##   4. Fixed allowNone bug found by user sameerc1@grandecom.net