]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/lib/masked/numctrl.py
reSWIGged
[wxWidgets.git] / wxPython / wx / lib / masked / numctrl.py
CommitLineData
d14a1e28 1#----------------------------------------------------------------------------
c878ceea 2# Name: wxPython.lib.masked.numctrl.py
d14a1e28
RD
3# Author: Will Sadkin
4# Created: 09/06/2003
5# Copyright: (c) 2003 by Will Sadkin
6# RCS-ID: $Id$
fffd96b7 7# License: wxWidgets license
d14a1e28
RD
8#----------------------------------------------------------------------------
9# NOTE:
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.
c878ceea 12# (ie. the features of masked.TextCtrl), but allows Get/Set of numeric
d14a1e28
RD
13# values, rather than text.
14#
c878ceea 15# Masked.NumCtrl permits integer, and floating point values to be set
d14a1e28 16# retrieved or set via .GetValue() and .SetValue() (type chosen based on
c878ceea 17# fraction width, and provides an masked.EVT_NUM() event function for trapping
d14a1e28
RD
18# changes to the control.
19#
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.
26#
c878ceea 27# masked.NumCtrl also supports range limits, with the option of either
d14a1e28
RD
28# enforcing them or simply coloring the text of the control if the limits
29# are exceeded.
30#
c878ceea 31# masked.NumCtrl is intended to support fixed-point numeric entry, and
fffd96b7 32# is derived from BaseMaskedTextCtrl. As such, it supports a limited range
d14a1e28 33# of values to comply with a fixed-width entry mask.
b881fc78
RD
34#----------------------------------------------------------------------------
35# 12/09/2003 - Jeff Grimmett (grimmtooth@softhome.net)
36#
37# o Updated for wx namespace
c878ceea 38#
d4b73b1b
RD
39# 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
40#
41# o wxMaskedEditMixin -> MaskedEditMixin
c878ceea
RD
42# o wxMaskedTextCtrl -> masked.TextCtrl
43# o wxMaskedNumNumberUpdatedEvent -> masked.NumberUpdatedEvent
44# o wxMaskedNumCtrl -> masked.NumCtrl
d4b73b1b 45#
b881fc78 46
d14a1e28
RD
47"""<html><body>
48<P>
c878ceea 49<B>masked.NumCtrl:</B>
d14a1e28
RD
50<UL>
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>
d4b73b1b 53<LI>has the right-insert input style that MaskedTextCtrl supports,</LI>
d14a1e28
RD
54<LI>provides optional automatic grouping, sign control and format, grouping and decimal
55character selection, etc. etc.</LI>
56</UL>
57<P>
d4b73b1b 58Being derived from MaskedTextCtrl, the control only allows
d14a1e28
RD
59fixed-point notation. That is, it has a fixed (though reconfigurable)
60maximum width for the integer portion and optional fixed width
61fractional portion.
62<P>
63Here's the API:
64<DL><PRE>
c878ceea 65 <B>masked.NumCtrl</B>(
d14a1e28
RD
66 parent, id = -1,
67 <B>value</B> = 0,
fffd96b7
RD
68 pos = wx.DefaultPosition,
69 size = wx.DefaultSize,
d14a1e28 70 style = 0,
fffd96b7 71 validator = wx.DefaultValidator,
c878ceea 72 name = "masked.number",
d14a1e28
RD
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> = '.',
81 <B>min</B> = None,
82 <B>max</B> = None,
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",
c878ceea 90 <B>autoSize</B> = True
d14a1e28
RD
91 )
92</PRE>
93<UL>
94 <DT><B>value</B>
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.)
99 <BR>
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.
109 <BR>
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.
114 <BR>
115 <DT><B>allowNone</B>
116 <DD>Boolean indicating whether or not the control is allowed to be
117 empty, representing a value of None for the control.
118 <BR>
119 <DT><B>allowNegative</B>
120 <DD>Boolean indicating whether or not control is allowed to hold
121 negative numbers.
122 <BR>
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.)
126 <BR>
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.
130 <BR>
131 <DT><B>groupChar</B>
132 <DD>What grouping character will be used if allowed. (By default ',')
133 <BR>
134 <DT><B>decimalChar</B>
135 <DD>If fractionWidth is > 0, what character will be used to represent
136 the decimal point. (By default '.')
137 <BR>
138 <DL><B>min</B>
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.
144 <BR>
145 <DT><B>max</B>
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.
151 <BR>
152 <DT><B>limited</B>
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.
157 <BR>
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.
163 <BR>
164 <DT><B>foregroundColour</B>
165 <DD>Color value used for positive values of the control.
166 <BR>
167 <DT><B>signedForegroundColour</B>
168 <DD>Color value used for negative values of the control.
169 <BR>
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.)
173 <BR>
174 <DT><B>validBackgroundColour</B>
175 <DD>What background color to use when the control value is
176 considered valid.
177 <BR>
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.
fffd96b7
RD
181 <BR>
182 <DT><B>autoSize</B>
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.)
d14a1e28
RD
190</UL>
191<BR>
192<BR>
c878ceea 193<DT><B>masked.EVT_NUM(win, id, func)</B>
fffd96b7 194<DD>Respond to a EVT_COMMAND_MASKED_NUMBER_UPDATED event, generated when
d14a1e28
RD
195the value changes. Notice that this event will always be sent when the
196control's contents changes - whether this is due to user input or
197comes from the program itself (for example, if SetValue() is called.)
198<BR>
199<BR>
200<DT><B>SetValue(int|long|float|string)</B>
201<DD>Sets the value of the control to the value specified, if
202possible. The resulting actual value of the control may be
203altered to conform to the format of the control, changed
204to conform with the bounds set on the control if limited,
205or colored if not limited but the value is out-of-bounds.
206A ValueError exception will be raised if an invalid value
207is specified.
208<BR>
209<DT><B>GetValue()</B>
210<DD>Retrieves the numeric value from the control. The value
211retrieved will be either be returned as a long if the
212fractionWidth is 0, or a float otherwise.
213<BR>
214<BR>
215<DT><B>SetParameters(**kwargs)</B>
216<DD>Allows simultaneous setting of various attributes
217of the control after construction. Keyword arguments
218allowed are the same parameters as supported in the constructor.
219<BR>
220<BR>
221<DT><B>SetIntegerWidth(value)</B>
222<DD>Resets the width of the integer portion of the control. The
223value must be >= 1, or an AttributeError exception will result.
224This value should account for any grouping characters that might
225be inserted (if grouping is enabled), but does not need to account
226for 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,
229not including any reserved sign position.
230<BR>
231<BR>
232<DT><B>SetFractionWidth(value)</B>
233<DD>Resets the width of the fractional portion of the control. The
234value must be >= 0, or an AttributeError exception will result. If
2350, the current value of the control will be truncated to an integer
236value.
237<DT><B>GetFractionWidth()</B>
238<DD>Returns the current width of the fractional portion of the control.
239<BR>
240<BR>
241<DT><B>SetMin(min=None)</B>
242<DD>Resets the minimum value of the control. If a value of <I>None</I>
243is provided, then the control will have no explicit minimum value.
244If the value specified is greater than the current maximum value,
245then the function returns False and the minimum will not change from
246its current setting. On success, the function returns True.
247<DT><DD>
248If successful and the current value is lower than the new lower
249bound, if the control is limited, the value will be automatically
250adjusted to the new minimum value; if not limited, the value in the
251control will be colored as invalid.
252<DT><DD>
253If min > the max value allowed by the width of the control,
254the function will return False, and the min will not be set.
255<BR>
256<DT><B>GetMin()</B>
257<DD>Gets the current lower bound value for the control.
258It will return None if no lower bound is currently specified.
259<BR>
260<BR>
261<DT><B>SetMax(max=None)</B>
262<DD>Resets the maximum value of the control. If a value of <I>None</I>
263is provided, then the control will have no explicit maximum value.
264If the value specified is less than the current minimum value, then
265the function returns False and the maximum will not change from its
266current setting. On success, the function returns True.
267<DT><DD>
268If successful and the current value is greater than the new upper
269bound, if the control is limited the value will be automatically
270adjusted to this maximum value; if not limited, the value in the
271control will be colored as invalid.
272<DT><DD>
273If max > the max value allowed by the width of the control,
274the function will return False, and the max will not be set.
275<BR>
276<DT><B>GetMax()</B>
277<DD>Gets the current upper bound value for the control.
278It will return None if no upper bound is currently specified.
279<BR>
280<BR>
281<DT><B>SetBounds(min=None,max=None)</B>
282<DD>This function is a convenience function for setting the min and max
283values at the same time. The function only applies the maximum bound
284if setting the minimum bound is successful, and returns True
285only if both operations succeed. <B><I>Note:</I></B> leaving out an argument
286will remove the corresponding bound.
287<DT><B>GetBounds()</B>
288<DD>This function returns a two-tuple (min,max), indicating the
289current bounds of the control. Each value can be None if
290that bound is not set.
291<BR>
292<BR>
293<DT><B>IsInBounds(value=None)</B>
294<DD>Returns <I>True</I> if no value is specified and the current value
295of the control falls within the current bounds. This function can also
296be called with a value to see if that value would fall within the current
297bounds of the given control.
298<BR>
299<BR>
300<DT><B>SetLimited(bool)</B>
301<DD>If called with a value of True, this function will cause the control
302to limit the value to fall within the bounds currently specified.
303If the control's value currently exceeds the bounds, it will then
304be limited accordingly.
305If called with a value of False, this function will disable value
306limiting, but coloring of out-of-bounds values will still take
307place 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
311value to fall within the current bounds.
312<BR>
313<BR>
314<DT><B>SetAllowNone(bool)</B>
315<DD>If called with a value of True, this function will cause the control
316to allow the value to be empty, representing a value of None.
317If called with a value of False, this function will prevent the value
318from being None. If the value of the control is currently None,
319ie. the control is empty, then the value will be changed to that
320of 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
324value to be None.
325<BR>
326<BR>
327<DT><B>SetAllowNegative(bool)</B>
328<DD>If called with a value of True, this function will cause the
329control to allow the value to be negative (and reserve space for
330displaying the sign. If called with a value of False, and the
331value of the control is currently negative, the value of the
332control will be converted to the absolute value, and then
333limited appropriately based on the existing bounds of the control
334(if any).
335<DT><B>GetAllowNegative()</B>
336<DT><B>IsNegativeAllowed()</B>
337<DD>Returns <I>True</I> if the control currently permits values
338to be negative.
339<BR>
340<BR>
341<DT><B>SetGroupDigits(bool)</B>
342<DD>If called with a value of True, this will make the control
343automatically add and manage grouping characters to the presented
344value 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.
348<BR>
349<BR>
350<DT><B>SetGroupChar()</B>
351<DD>Sets the grouping character for the integer portion of the
352control. (The default grouping character this is ','.
353<DT><B>GetGroupChar()</B>
354<DD>Returns the current grouping character for the control.
355<BR>
356<BR>
357<DT><B>SetSelectOnEntry()</B>
358<DD>If called with a value of <I>True</I>, this will make the control
359automatically select the contents of each field as it is entered
360within the control. (The default is True.)
361<DT><B>GetSelectOnEntry()</B>
362<DD>Returns <I>True</I> if the control currently auto selects
363the field values on entry.
364<BR>
365<BR>
fffd96b7
RD
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.
370<BR>
371<BR>
d14a1e28
RD
372</DL>
373</body></html>
374"""
8b9a4190 375
b881fc78
RD
376import copy
377import string
378import types
379
380import wx
381
d14a1e28
RD
382from sys import maxint
383MAXINT = maxint # (constants should be in upper case)
384MININT = -maxint-1
8b9a4190 385
b881fc78 386from wx.tools.dbg import Logger
c878ceea 387from wx.lib.masked import MaskedEditMixin, Field, BaseMaskedTextCtrl
d14a1e28 388dbg = Logger()
339983ff 389##dbg(enable=1)
d14a1e28
RD
390
391#----------------------------------------------------------------------------
392
b881fc78 393wxEVT_COMMAND_MASKED_NUMBER_UPDATED = wx.NewEventType()
c878ceea 394EVT_NUM = wx.PyEventBinder(wxEVT_COMMAND_MASKED_NUMBER_UPDATED, 1)
d14a1e28 395
b881fc78 396#----------------------------------------------------------------------------
d14a1e28 397
c878ceea 398class NumberUpdatedEvent(wx.PyCommandEvent):
d14a1e28 399 def __init__(self, id, value = 0, object=None):
b881fc78 400 wx.PyCommandEvent.__init__(self, wxEVT_COMMAND_MASKED_NUMBER_UPDATED, id)
d14a1e28
RD
401
402 self.__value = value
403 self.SetEventObject(object)
404
405 def GetValue(self):
406 """Retrieve the value of the control at the time
407 this event was generated."""
408 return self.__value
409
410
411#----------------------------------------------------------------------------
c878ceea
RD
412class NumCtrlAccessorsMixin:
413 # Define masked.NumCtrl's list of attributes having their own
fffd96b7
RD
414 # Get/Set functions, ignoring those that make no sense for
415 # an numeric control.
416 exposed_basectrl_params = (
417 'decimalChar',
418 'shiftDecimalChar',
419 'groupChar',
420 'useParensForNegatives',
421 'defaultValue',
422 'description',
423
424 'useFixedWidthFont',
425 'autoSize',
426 'signedForegroundColour',
427 'emptyBackgroundColour',
428 'validBackgroundColour',
429 'invalidBackgroundColour',
430
431 'emptyInvalid',
432 'validFunc',
433 'validRequired',
434 )
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))
439
440 if param.find('Colour') != -1:
441 # add non-british spellings, for backward-compatibility
442 propname.replace('Colour', 'Color')
443
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))
446
447
448
449#----------------------------------------------------------------------------
c878ceea
RD
450
451class NumCtrl(BaseMaskedTextCtrl, NumCtrlAccessorsMixin):
d14a1e28 452
d14a1e28
RD
453
454 valid_ctrl_params = {
455 'integerWidth': 10, # by default allow all 32-bit integers
fffd96b7 456 'fractionWidth': 0, # by default, use integers
d14a1e28
RD
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
fffd96b7 460 'groupDigits': True, # by default, don't insert grouping
d14a1e28
RD
461 'groupChar': ',', # by default, use ',' for grouping
462 'min': None, # by default, no bounds set
463 'max': None,
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",
fffd96b7 472 'useFixedWidthFont': True, # by default, use a fixed-width font
c878ceea 473 'autoSize': True, # by default, set the width of the control based on the mask
d14a1e28
RD
474 }
475
476
477 def __init__ (
478 self, parent, id=-1, value = 0,
b881fc78
RD
479 pos = wx.DefaultPosition, size = wx.DefaultSize,
480 style = wx.TE_PROCESS_TAB, validator = wx.DefaultValidator,
c878ceea 481 name = "masked.num",
d14a1e28
RD
482 **kwargs ):
483
c878ceea 484## dbg('masked.NumCtrl::__init__', indent=1)
d14a1e28
RD
485
486 # Set defaults for control:
c878ceea
RD
487## dbg('setting defaults:')
488 for key, param_value in NumCtrl.valid_ctrl_params.items():
d14a1e28
RD
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))
492
493 # Assign defaults for all attributes:
c878ceea
RD
494 init_args = copy.deepcopy(NumCtrl.valid_ctrl_params)
495## dbg('kwargs:', kwargs)
d14a1e28
RD
496 for key, param_value in kwargs.items():
497 key = key.replace('Color', 'Colour')
c878ceea 498 if key not in NumCtrl.valid_ctrl_params.keys():
d14a1e28
RD
499 raise AttributeError('invalid keyword argument "%s"' % key)
500 else:
501 init_args[key] = param_value
c878ceea 502## dbg('init_args:', indent=1)
d14a1e28 503 for key, param_value in init_args.items():
c878ceea
RD
504## dbg('%s:' % key, param_value)
505 pass
506## dbg(indent=0)
d14a1e28
RD
507
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']))
513
514 fields = {}
515
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']
522
523 if self._fractionWidth:
524 fracmask = '.' + '#{%d}' % self._fractionWidth
c878ceea 525## dbg('fracmask:', fracmask)
d14a1e28
RD
526 fields[1] = Field(defaultValue='0'*self._fractionWidth)
527 else:
528 fracmask = ''
529
530 self._integerWidth = init_args['integerWidth']
531 if init_args['groupDigits']:
532 self._groupSpace = (self._integerWidth - 1) / 3
533 else:
534 self._groupSpace = 0
535 intmask = '#{%d}' % (self._integerWidth + self._groupSpace)
536 if self._fractionWidth:
537 emptyInvalid = False
538 else:
539 emptyInvalid = True
540 fields[0] = Field(formatcodes='r<>', emptyInvalid=emptyInvalid)
c878ceea 541## dbg('intmask:', intmask)
d14a1e28
RD
542
543 # don't bother to reprocess these arguments:
544 del init_args['integerWidth']
545 del init_args['fractionWidth']
546
fffd96b7
RD
547 self._autoSize = init_args['autoSize']
548 if self._autoSize:
549 formatcodes = 'FR<'
550 else:
551 formatcodes = 'R<'
552
d14a1e28
RD
553
554 mask = intmask+fracmask
555
556 # initial value of state vars
557 self._oldvalue = 0
558 self._integerEnd = 0
559 self._typedSign = False
560
561 # Construct the base control:
fffd96b7 562 BaseMaskedTextCtrl.__init__(
d14a1e28
RD
563 self, parent, id, '',
564 pos, size, style, validator, name,
565 mask = mask,
fffd96b7 566 formatcodes = formatcodes,
d14a1e28
RD
567 fields = fields,
568 validFunc=self.IsInBounds,
569 setupEventHandling = False)
570
b881fc78
RD
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
d14a1e28
RD
578 ## track of previous value for undo
579
580 # Establish any additional parameters, with appropriate error checking
581 self.SetParameters(**init_args)
582
583 # Set the value requested (if possible)
584## wxCallAfter(self.SetValue, value)
585 self.SetValue(value)
586
587 # Ensure proper coloring:
588 self.Refresh()
c878ceea 589## dbg('finished NumCtrl::__init__', indent=0)
d14a1e28
RD
590
591
592 def SetParameters(self, **kwargs):
593 """
594 This routine is used to initialize and reconfigure the control:
595 """
c878ceea 596## dbg('NumCtrl::SetParameters', indent=1)
d14a1e28
RD
597 maskededit_kwargs = {}
598 reset_fraction_width = False
599
600
601 if( (kwargs.has_key('integerWidth') and kwargs['integerWidth'] != self._integerWidth)
602 or (kwargs.has_key('fractionWidth') and kwargs['fractionWidth'] != self._fractionWidth)
fffd96b7
RD
603 or (kwargs.has_key('groupDigits') and kwargs['groupDigits'] != self._groupDigits)
604 or (kwargs.has_key('autoSize') and kwargs['autoSize'] != self._autoSize) ):
d14a1e28
RD
605
606 fields = {}
607
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']))
613 else:
614 if self._fractionWidth != kwargs['fractionWidth']:
615 self._fractionWidth = kwargs['fractionWidth']
616
617 if self._fractionWidth:
618 fracmask = '.' + '#{%d}' % self._fractionWidth
619 fields[1] = Field(defaultValue='0'*self._fractionWidth)
620 emptyInvalid = False
621 else:
622 emptyInvalid = True
623 fracmask = ''
c878ceea 624## dbg('fracmask:', fracmask)
d14a1e28
RD
625
626 if kwargs.has_key('integerWidth'):
627 if type(kwargs['integerWidth']) != types.IntType:
c878ceea 628## dbg(indent=0)
d14a1e28
RD
629 raise AttributeError('invalid integerWidth (%s) specified; expected integer' % repr(kwargs['integerWidth']))
630 elif kwargs['integerWidth'] < 0:
c878ceea 631## dbg(indent=0)
d14a1e28
RD
632 raise AttributeError('invalid integerWidth (%s) specified; must be > 0' % repr(kwargs['integerWidth']))
633 else:
634 self._integerWidth = kwargs['integerWidth']
635
636 if kwargs.has_key('groupDigits'):
637 self._groupDigits = kwargs['groupDigits']
638
639 if self._groupDigits:
640 self._groupSpace = (self._integerWidth - 1) / 3
641 else:
642 self._groupSpace = 0
643
644 intmask = '#{%d}' % (self._integerWidth + self._groupSpace)
c878ceea 645## dbg('intmask:', intmask)
d14a1e28
RD
646 fields[0] = Field(formatcodes='r<>', emptyInvalid=emptyInvalid)
647 maskededit_kwargs['fields'] = fields
648
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']
654
655 maskededit_kwargs['mask'] = intmask+fracmask
656
339983ff 657 if kwargs.has_key('groupChar') or kwargs.has_key('decimalChar'):
d14a1e28 658 old_groupchar = self._groupChar # save so we can reformat properly
d14a1e28 659 old_decimalchar = self._decimalChar
339983ff 660## dbg("old_groupchar: '%s'" % old_groupchar)
c878ceea 661## dbg("old_decimalchar: '%s'" % old_decimalchar)
339983ff
RD
662 groupchar = old_groupchar
663 decimalchar = old_decimalchar
664
665 if kwargs.has_key('groupChar'):
666 maskededit_kwargs['groupChar'] = kwargs['groupChar']
667 groupchar = kwargs['groupChar']
668 if kwargs.has_key('decimalChar'):
669 maskededit_kwargs['decimalChar'] = kwargs['decimalChar']
670 decimalchar = kwargs['decimalChar']
671
672 # Add sanity check to make sure these are distinct, and if not,
673 # raise attribute error
674 if groupchar == decimalchar:
675 raise AttributeError('groupChar and decimalChar must be distinct')
676
d14a1e28
RD
677
678 # for all other parameters, assign keyword args as appropriate:
679 for key, param_value in kwargs.items():
680 key = key.replace('Color', 'Colour')
c878ceea 681 if key not in NumCtrl.valid_ctrl_params.keys():
d14a1e28 682 raise AttributeError('invalid keyword argument "%s"' % key)
d4b73b1b 683 elif key not in MaskedEditMixin.valid_ctrl_params.keys():
d14a1e28
RD
684 setattr(self, '_' + key, param_value)
685 elif key in ('mask', 'autoformat'): # disallow explicit setting of mask
686 raise AttributeError('invalid keyword argument "%s"' % key)
687 else:
688 maskededit_kwargs[key] = param_value
c878ceea 689## dbg('kwargs:', kwargs)
d14a1e28
RD
690
691 # reprocess existing format codes to ensure proper resulting format:
c878ceea 692 formatcodes = self.GetCtrlParameter('formatcodes')
d14a1e28
RD
693 if kwargs.has_key('allowNegative'):
694 if kwargs['allowNegative'] and '-' not in formatcodes:
695 formatcodes += '-'
696 maskededit_kwargs['formatcodes'] = formatcodes
697 elif not kwargs['allowNegative'] and '-' in formatcodes:
698 formatcodes = formatcodes.replace('-','')
699 maskededit_kwargs['formatcodes'] = formatcodes
700
701 if kwargs.has_key('groupDigits'):
702 if kwargs['groupDigits'] and ',' not in formatcodes:
703 formatcodes += ','
704 maskededit_kwargs['formatcodes'] = formatcodes
705 elif not kwargs['groupDigits'] and ',' in formatcodes:
706 formatcodes = formatcodes.replace(',','')
707 maskededit_kwargs['formatcodes'] = formatcodes
708
709 if kwargs.has_key('selectOnEntry'):
710 self._selectOnEntry = kwargs['selectOnEntry']
c878ceea 711## dbg("kwargs['selectOnEntry']?", kwargs['selectOnEntry'], "'S' in formatcodes?", 'S' in formatcodes)
d14a1e28
RD
712 if kwargs['selectOnEntry'] and 'S' not in formatcodes:
713 formatcodes += 'S'
714 maskededit_kwargs['formatcodes'] = formatcodes
715 elif not kwargs['selectOnEntry'] and 'S' in formatcodes:
716 formatcodes = formatcodes.replace('S','')
717 maskededit_kwargs['formatcodes'] = formatcodes
718
fffd96b7
RD
719 if kwargs.has_key('autoSize'):
720 self._autoSize = kwargs['autoSize']
721 if kwargs['autoSize'] and 'F' not in formatcodes:
722 formatcodes += 'F'
723 maskededit_kwargs['formatcodes'] = formatcodes
724 elif not kwargs['autoSize'] and 'F' in formatcodes:
725 formatcodes = formatcodes.replace('F', '')
726 maskededit_kwargs['formatcodes'] = formatcodes
727
728
d14a1e28
RD
729 if 'r' in formatcodes and self._fractionWidth:
730 # top-level mask should only be right insert if no fractional
731 # part will be shown; ie. if reconfiguring control, remove
732 # previous "global" setting.
733 formatcodes = formatcodes.replace('r', '')
734 maskededit_kwargs['formatcodes'] = formatcodes
735
fffd96b7 736
d14a1e28
RD
737 if kwargs.has_key('limited'):
738 if kwargs['limited'] and not self._limited:
739 maskededit_kwargs['validRequired'] = True
740 elif not kwargs['limited'] and self._limited:
741 maskededit_kwargs['validRequired'] = False
742 self._limited = kwargs['limited']
743
c878ceea 744## dbg('maskededit_kwargs:', maskededit_kwargs)
d14a1e28
RD
745 if maskededit_kwargs.keys():
746 self.SetCtrlParameters(**maskededit_kwargs)
747
748 # Record end of integer and place cursor there:
749 integerEnd = self._fields[0]._extent[1]
fffd96b7 750 self.SetInsertionPoint(0)
d14a1e28
RD
751 self.SetInsertionPoint(integerEnd)
752 self.SetSelection(integerEnd, integerEnd)
753
754 # Go ensure all the format codes necessary are present:
755 orig_intformat = intformat = self.GetFieldParameter(0, 'formatcodes')
756 if 'r' not in intformat:
757 intformat += 'r'
758 if '>' not in intformat:
759 intformat += '>'
760 if intformat != orig_intformat:
761 if self._fractionWidth:
762 self.SetFieldParameters(0, formatcodes=intformat)
763 else:
764 self.SetCtrlParameters(formatcodes=intformat)
765
766 # Set min and max as appropriate:
767 if kwargs.has_key('min'):
768 min = kwargs['min']
769 if( self._max is None
770 or min is None
771 or (self._max is not None and self._max >= min) ):
c878ceea 772## dbg('examining min')
d14a1e28
RD
773 if min is not None:
774 try:
775 textmin = self._toGUI(min, apply_limits = False)
776 except ValueError:
c878ceea 777## dbg('min will not fit into control; ignoring', indent=0)
d14a1e28 778 raise
c878ceea 779## dbg('accepted min')
d14a1e28
RD
780 self._min = min
781 else:
c878ceea
RD
782## dbg('ignoring min')
783 pass
d14a1e28
RD
784
785
786 if kwargs.has_key('max'):
787 max = kwargs['max']
788 if( self._min is None
789 or max is None
790 or (self._min is not None and self._min <= max) ):
c878ceea 791## dbg('examining max')
d14a1e28
RD
792 if max is not None:
793 try:
794 textmax = self._toGUI(max, apply_limits = False)
795 except ValueError:
c878ceea 796## dbg('max will not fit into control; ignoring', indent=0)
d14a1e28 797 raise
c878ceea 798## dbg('accepted max')
d14a1e28
RD
799 self._max = max
800 else:
c878ceea
RD
801## dbg('ignoring max')
802 pass
d14a1e28
RD
803
804 if kwargs.has_key('allowNegative'):
805 self._allowNegative = kwargs['allowNegative']
806
807 # Ensure current value of control obeys any new restrictions imposed:
808 text = self._GetValue()
c878ceea 809## dbg('text value: "%s"' % text)
d14a1e28
RD
810 if kwargs.has_key('groupChar') and text.find(old_groupchar) != -1:
811 text = text.replace(old_groupchar, self._groupChar)
812 if kwargs.has_key('decimalChar') and text.find(old_decimalchar) != -1:
813 text = text.replace(old_decimalchar, self._decimalChar)
814 if text != self._GetValue():
b881fc78 815 wx.TextCtrl.SetValue(self, text)
d14a1e28
RD
816
817 value = self.GetValue()
818
c878ceea 819## dbg('self._allowNegative?', self._allowNegative)
d14a1e28
RD
820 if not self._allowNegative and self._isNeg:
821 value = abs(value)
c878ceea 822## dbg('abs(value):', value)
d14a1e28
RD
823 self._isNeg = False
824
fffd96b7 825 elif not self._allowNone and BaseMaskedTextCtrl.GetValue(self) == '':
d14a1e28
RD
826 if self._min > 0:
827 value = self._min
828 else:
829 value = 0
830
831 sel_start, sel_to = self.GetSelection()
832 if self.IsLimited() and self._min is not None and value < self._min:
c878ceea 833## dbg('Set to min value:', self._min)
d14a1e28
RD
834 self._SetValue(self._toGUI(self._min))
835
836 elif self.IsLimited() and self._max is not None and value > self._max:
c878ceea 837## dbg('Setting to max value:', self._max)
d14a1e28
RD
838 self._SetValue(self._toGUI(self._max))
839 else:
840 # reformat current value as appropriate to possibly new conditions
c878ceea 841## dbg('Reformatting value:', value)
d14a1e28
RD
842 sel_start, sel_to = self.GetSelection()
843 self._SetValue(self._toGUI(value))
844 self.Refresh() # recolor as appropriate
c878ceea 845## dbg('finished NumCtrl::SetParameters', indent=0)
d14a1e28
RD
846
847
848
849 def _GetNumValue(self, value):
850 """
851 This function attempts to "clean up" a text value, providing a regularized
852 convertable string, via atol() or atof(), for any well-formed numeric text value.
853 """
854 return value.replace(self._groupChar, '').replace(self._decimalChar, '.').replace('(', '-').replace(')','').strip()
855
856
857 def GetFraction(self, candidate=None):
858 """
859 Returns the fractional portion of the value as a float. If there is no
860 fractional portion, the value returned will be 0.0.
861 """
862 if not self._fractionWidth:
863 return 0.0
864 else:
865 fracstart, fracend = self._fields[1]._extent
866 if candidate is None:
fffd96b7 867 value = self._toGUI(BaseMaskedTextCtrl.GetValue(self))
d14a1e28
RD
868 else:
869 value = self._toGUI(candidate)
870 fracstring = value[fracstart:fracend].strip()
871 if not value:
872 return 0.0
873 else:
874 return string.atof(fracstring)
875
876 def _OnChangeSign(self, event):
c878ceea 877## dbg('NumCtrl::_OnChangeSign', indent=1)
d14a1e28 878 self._typedSign = True
d4b73b1b 879 MaskedEditMixin._OnChangeSign(self, event)
c878ceea 880## dbg(indent=0)
d14a1e28
RD
881
882
883 def _disallowValue(self):
c878ceea 884## dbg('NumCtrl::_disallowValue')
d14a1e28
RD
885 # limited and -1 is out of bounds
886 if self._typedSign:
887 self._isNeg = False
b881fc78
RD
888 if not wx.Validator_IsSilent():
889 wx.Bell()
d14a1e28 890 sel_start, sel_to = self._GetSelection()
c878ceea 891## dbg('queuing reselection of (%d, %d)' % (sel_start, sel_to))
b881fc78
RD
892 wx.CallAfter(self.SetInsertionPoint, sel_start) # preserve current selection/position
893 wx.CallAfter(self.SetSelection, sel_start, sel_to)
d14a1e28
RD
894
895 def _SetValue(self, value):
896 """
897 This routine supersedes the base masked control _SetValue(). It is
898 needed to ensure that the value of the control is always representable/convertable
899 to a numeric return value (via GetValue().) This routine also handles
900 automatic adjustment and grouping of the value without explicit intervention
901 by the user.
902 """
903
c878ceea 904## dbg('NumCtrl::_SetValue("%s")' % value, indent=1)
d14a1e28
RD
905
906 if( (self._fractionWidth and value.find(self._decimalChar) == -1) or
907 (self._fractionWidth == 0 and value.find(self._decimalChar) != -1) ) :
908 value = self._toGUI(value)
909
910 numvalue = self._GetNumValue(value)
c878ceea 911## dbg('cleansed value: "%s"' % numvalue)
d14a1e28
RD
912 replacement = None
913
914 if numvalue == "":
915 if self._allowNone:
c878ceea 916## dbg('calling base BaseMaskedTextCtrl._SetValue(self, "%s")' % value)
fffd96b7 917 BaseMaskedTextCtrl._SetValue(self, value)
d14a1e28
RD
918 self.Refresh()
919 return
920 elif self._min > 0 and self.IsLimited():
921 replacement = self._min
922 else:
923 replacement = 0
c878ceea 924## dbg('empty value; setting replacement:', replacement)
d14a1e28
RD
925
926 if replacement is None:
927 # Go get the integer portion about to be set and verify its validity
928 intstart, intend = self._fields[0]._extent
c878ceea
RD
929## dbg('intstart, intend:', intstart, intend)
930## dbg('raw integer:"%s"' % value[intstart:intend])
d14a1e28
RD
931 int = self._GetNumValue(value[intstart:intend])
932 numval = self._fromGUI(value)
933
c878ceea 934## dbg('integer: "%s"' % int)
d14a1e28
RD
935 try:
936 fracval = self.GetFraction(value)
937 except ValueError, e:
c878ceea 938## dbg('Exception:', e, 'must be out of bounds; disallow value')
d14a1e28 939 self._disallowValue()
c878ceea 940## dbg(indent=0)
d14a1e28
RD
941 return
942
943 if fracval == 0.0:
c878ceea 944## dbg('self._isNeg?', self._isNeg)
d14a1e28 945 if int == '-' and self._oldvalue < 0 and not self._typedSign:
c878ceea 946## dbg('just a negative sign; old value < 0; setting replacement of 0')
d14a1e28
RD
947 replacement = 0
948 self._isNeg = False
949 elif int[:2] == '-0' and self._fractionWidth == 0:
950 if self._oldvalue < 0:
c878ceea 951## dbg('-0; setting replacement of 0')
d14a1e28
RD
952 replacement = 0
953 self._isNeg = False
954 elif not self._limited or (self._min < -1 and self._max >= -1):
c878ceea 955## dbg('-0; setting replacement of -1')
d14a1e28
RD
956 replacement = -1
957 self._isNeg = True
958 else:
959 # limited and -1 is out of bounds
960 self._disallowValue()
c878ceea 961## dbg(indent=0)
d14a1e28
RD
962 return
963
964 elif int == '-' and (self._oldvalue >= 0 or self._typedSign) and self._fractionWidth == 0:
965 if not self._limited or (self._min < -1 and self._max >= -1):
c878ceea 966## dbg('just a negative sign; setting replacement of -1')
d14a1e28
RD
967 replacement = -1
968 else:
969 # limited and -1 is out of bounds
970 self._disallowValue()
c878ceea 971## dbg(indent=0)
d14a1e28
RD
972 return
973
974 elif( self._typedSign
975 and int.find('-') != -1
976 and self._limited
977 and not self._min <= numval <= self._max):
978 # changed sign resulting in value that's now out-of-bounds;
979 # disallow
980 self._disallowValue()
c878ceea 981## dbg(indent=0)
d14a1e28
RD
982 return
983
984 if replacement is None:
985 if int and int != '-':
986 try:
987 string.atol(int)
988 except ValueError:
989 # integer requested is not legal. This can happen if the user
990 # is attempting to insert a digit in the middle of the control
991 # resulting in something like " 3 45". Disallow such actions:
c878ceea 992## dbg('>>>>>>>>>>>>>>>> "%s" does not convert to a long!' % int)
b881fc78
RD
993 if not wx.Validator_IsSilent():
994 wx.Bell()
d14a1e28 995 sel_start, sel_to = self._GetSelection()
c878ceea 996## dbg('queuing reselection of (%d, %d)' % (sel_start, sel_to))
b881fc78
RD
997 wx.CallAfter(self.SetInsertionPoint, sel_start) # preserve current selection/position
998 wx.CallAfter(self.SetSelection, sel_start, sel_to)
c878ceea 999## dbg(indent=0)
d14a1e28
RD
1000 return
1001
1002 if int[0] == '0' and len(int) > 1:
c878ceea 1003## dbg('numvalue: "%s"' % numvalue.replace(' ', ''))
d14a1e28
RD
1004 if self._fractionWidth:
1005 value = self._toGUI(string.atof(numvalue))
1006 else:
1007 value = self._toGUI(string.atol(numvalue))
c878ceea 1008## dbg('modified value: "%s"' % value)
d14a1e28
RD
1009
1010 self._typedSign = False # reset state var
1011
1012 if replacement is not None:
1013 # Value presented wasn't a legal number, but control should do something
1014 # reasonable instead:
c878ceea 1015## dbg('setting replacement value:', replacement)
d14a1e28 1016 self._SetValue(self._toGUI(replacement))
c878ceea 1017 sel_start = BaseMaskedTextCtrl.GetValue(self).find(str(abs(replacement))) # find where it put the 1, so we can select it
d14a1e28 1018 sel_to = sel_start + len(str(abs(replacement)))
c878ceea 1019## dbg('queuing selection of (%d, %d)' %(sel_start, sel_to))
b881fc78
RD
1020 wx.CallAfter(self.SetInsertionPoint, sel_start)
1021 wx.CallAfter(self.SetSelection, sel_start, sel_to)
c878ceea 1022## dbg(indent=0)
d14a1e28
RD
1023 return
1024
1025 # Otherwise, apply appropriate formatting to value:
1026
1027 # Because we're intercepting the value and adjusting it
1028 # before a sign change is detected, we need to do this here:
1029 if '-' in value or '(' in value:
1030 self._isNeg = True
1031 else:
1032 self._isNeg = False
1033
c878ceea 1034## dbg('value:"%s"' % value, 'self._useParens:', self._useParens)
d14a1e28
RD
1035 if self._fractionWidth:
1036 adjvalue = self._adjustFloat(self._GetNumValue(value).replace('.',self._decimalChar))
1037 else:
1038 adjvalue = self._adjustInt(self._GetNumValue(value))
c878ceea 1039## dbg('adjusted value: "%s"' % adjvalue)
d14a1e28
RD
1040
1041
1042 sel_start, sel_to = self._GetSelection() # record current insertion point
c878ceea 1043## dbg('calling BaseMaskedTextCtrl._SetValue(self, "%s")' % adjvalue)
fffd96b7 1044 BaseMaskedTextCtrl._SetValue(self, adjvalue)
d14a1e28
RD
1045 # After all actions so far scheduled, check that resulting cursor
1046 # position is appropriate, and move if not:
b881fc78 1047 wx.CallAfter(self._CheckInsertionPoint)
d14a1e28 1048
c878ceea 1049## dbg('finished NumCtrl::_SetValue', indent=0)
d14a1e28
RD
1050
1051 def _CheckInsertionPoint(self):
1052 # If current insertion point is before the end of the integer and
1053 # its before the 1st digit, place it just after the sign position:
c878ceea 1054## dbg('NumCtrl::CheckInsertionPoint', indent=1)
d14a1e28
RD
1055 sel_start, sel_to = self._GetSelection()
1056 text = self._GetValue()
1057 if sel_to < self._fields[0]._extent[1] and text[sel_to] in (' ', '-', '('):
1058 text, signpos, right_signpos = self._getSignedValue()
c878ceea 1059## dbg('setting selection(%d, %d)' % (signpos+1, signpos+1))
d14a1e28
RD
1060 self.SetInsertionPoint(signpos+1)
1061 self.SetSelection(signpos+1, signpos+1)
c878ceea 1062## dbg(indent=0)
d14a1e28
RD
1063
1064
5f280eaa 1065 def _OnErase( self, event=None, just_return_value=False ):
d14a1e28
RD
1066 """
1067 This overrides the base control _OnErase, so that erasing around
1068 grouping characters auto selects the digit before or after the
1069 grouping character, so that the erasure does the right thing.
1070 """
c878ceea 1071## dbg('NumCtrl::_OnErase', indent=1)
5f280eaa
RD
1072 if event is None: # called as action routine from Cut() operation.
1073 key = wx.WXK_DELETE
1074 else:
1075 key = event.GetKeyCode()
d14a1e28
RD
1076 #if grouping digits, make sure deletes next to group char always
1077 # delete next digit to appropriate side:
1078 if self._groupDigits:
fffd96b7 1079 value = BaseMaskedTextCtrl.GetValue(self)
d14a1e28
RD
1080 sel_start, sel_to = self._GetSelection()
1081
b881fc78 1082 if key == wx.WXK_BACK:
d14a1e28
RD
1083 # if 1st selected char is group char, select to previous digit
1084 if sel_start > 0 and sel_start < len(self._mask) and value[sel_start:sel_to] == self._groupChar:
1085 self.SetInsertionPoint(sel_start-1)
1086 self.SetSelection(sel_start-1, sel_to)
1087
1088 # elif previous char is group char, select to previous digit
1089 elif sel_start > 1 and sel_start == sel_to and value[sel_start-1:sel_start] == self._groupChar:
1090 self.SetInsertionPoint(sel_start-2)
1091 self.SetSelection(sel_start-2, sel_to)
1092
b881fc78 1093 elif key == wx.WXK_DELETE:
d14a1e28
RD
1094 if( sel_to < len(self._mask) - 2 + (1 *self._useParens)
1095 and sel_start == sel_to
1096 and value[sel_to] == self._groupChar ):
1097 self.SetInsertionPoint(sel_start)
1098 self.SetSelection(sel_start, sel_to+2)
1099
1100 elif( sel_to < len(self._mask) - 2 + (1 *self._useParens)
1101 and value[sel_start:sel_to] == self._groupChar ):
1102 self.SetInsertionPoint(sel_start)
1103 self.SetSelection(sel_start, sel_to+1)
c878ceea 1104## dbg(indent=0)
339983ff 1105 return BaseMaskedTextCtrl._OnErase(self, event, just_return_value)
d14a1e28
RD
1106
1107
1108 def OnTextChange( self, event ):
1109 """
1110 Handles an event indicating that the text control's value
c878ceea 1111 has changed, and issue EVT_NUM event.
d14a1e28
RD
1112 NOTE: using wxTextCtrl.SetValue() to change the control's
1113 contents from within a EVT_CHAR handler can cause double
1114 text events. So we check for actual changes to the text
1115 before passing the events on.
1116 """
c878ceea 1117## dbg('NumCtrl::OnTextChange', indent=1)
fffd96b7 1118 if not BaseMaskedTextCtrl._OnTextChange(self, event):
c878ceea 1119## dbg(indent=0)
d14a1e28
RD
1120 return
1121
1122 # else... legal value
1123
1124 value = self.GetValue()
1125 if value != self._oldvalue:
1126 try:
1127 self.GetEventHandler().ProcessEvent(
c878ceea 1128 NumberUpdatedEvent( self.GetId(), self.GetValue(), self ) )
d14a1e28 1129 except ValueError:
c878ceea 1130## dbg(indent=0)
d14a1e28
RD
1131 return
1132 # let normal processing of the text continue
1133 event.Skip()
1134 self._oldvalue = value # record for next event
c878ceea 1135## dbg(indent=0)
d14a1e28
RD
1136
1137 def _GetValue(self):
1138 """
fffd96b7 1139 Override of BaseMaskedTextCtrl to allow mixin to get the raw text value of the
d14a1e28
RD
1140 control with this function.
1141 """
b881fc78 1142 return wx.TextCtrl.GetValue(self)
d14a1e28
RD
1143
1144
1145 def GetValue(self):
1146 """
1147 Returns the current numeric value of the control.
1148 """
fffd96b7 1149 return self._fromGUI( BaseMaskedTextCtrl.GetValue(self) )
d14a1e28
RD
1150
1151 def SetValue(self, value):
1152 """
1153 Sets the value of the control to the value specified.
1154 The resulting actual value of the control may be altered to
1155 conform with the bounds set on the control if limited,
1156 or colored if not limited but the value is out-of-bounds.
1157 A ValueError exception will be raised if an invalid value
1158 is specified.
1159 """
339983ff 1160## dbg('NumCtrl::SetValue(%s)' % value, indent=1)
fffd96b7 1161 BaseMaskedTextCtrl.SetValue( self, self._toGUI(value) )
339983ff 1162## dbg(indent=0)
d14a1e28
RD
1163
1164
1165 def SetIntegerWidth(self, value):
fffd96b7 1166 self.SetParameters(integerWidth=value)
d14a1e28
RD
1167 def GetIntegerWidth(self):
1168 return self._integerWidth
1169
1170 def SetFractionWidth(self, value):
fffd96b7 1171 self.SetParameters(fractionWidth=value)
d14a1e28
RD
1172 def GetFractionWidth(self):
1173 return self._fractionWidth
1174
1175
1176
1177 def SetMin(self, min=None):
1178 """
1179 Sets the minimum value of the control. If a value of None
1180 is provided, then the control will have no explicit minimum value.
1181 If the value specified is greater than the current maximum value,
1182 then the function returns False and the minimum will not change from
1183 its current setting. On success, the function returns True.
1184
1185 If successful and the current value is lower than the new lower
1186 bound, if the control is limited, the value will be automatically
1187 adjusted to the new minimum value; if not limited, the value in the
1188 control will be colored as invalid.
1189
1190 If min > the max value allowed by the width of the control,
1191 the function will return False, and the min will not be set.
1192 """
c878ceea 1193## dbg('NumCtrl::SetMin(%s)' % repr(min), indent=1)
d14a1e28
RD
1194 if( self._max is None
1195 or min is None
1196 or (self._max is not None and self._max >= min) ):
1197 try:
1198 self.SetParameters(min=min)
1199 bRet = True
1200 except ValueError:
1201 bRet = False
1202 else:
1203 bRet = False
c878ceea 1204## dbg(indent=0)
d14a1e28
RD
1205 return bRet
1206
1207 def GetMin(self):
1208 """
1209 Gets the lower bound value of the control. It will return
1210 None if not specified.
1211 """
1212 return self._min
1213
1214
1215 def SetMax(self, max=None):
1216 """
1217 Sets the maximum value of the control. If a value of None
1218 is provided, then the control will have no explicit maximum value.
1219 If the value specified is less than the current minimum value, then
1220 the function returns False and the maximum will not change from its
1221 current setting. On success, the function returns True.
1222
1223 If successful and the current value is greater than the new upper
1224 bound, if the control is limited the value will be automatically
1225 adjusted to this maximum value; if not limited, the value in the
1226 control will be colored as invalid.
1227
1228 If max > the max value allowed by the width of the control,
1229 the function will return False, and the max will not be set.
1230 """
1231 if( self._min is None
1232 or max is None
1233 or (self._min is not None and self._min <= max) ):
1234 try:
1235 self.SetParameters(max=max)
1236 bRet = True
1237 except ValueError:
1238 bRet = False
1239 else:
1240 bRet = False
1241
1242 return bRet
1243
1244
1245 def GetMax(self):
1246 """
1247 Gets the maximum value of the control. It will return the current
1248 maximum integer, or None if not specified.
1249 """
1250 return self._max
1251
1252
1253 def SetBounds(self, min=None, max=None):
1254 """
1255 This function is a convenience function for setting the min and max
1256 values at the same time. The function only applies the maximum bound
1257 if setting the minimum bound is successful, and returns True
1258 only if both operations succeed.
1259 NOTE: leaving out an argument will remove the corresponding bound.
1260 """
1261 ret = self.SetMin(min)
1262 return ret and self.SetMax(max)
1263
1264
1265 def GetBounds(self):
1266 """
1267 This function returns a two-tuple (min,max), indicating the
1268 current bounds of the control. Each value can be None if
1269 that bound is not set.
1270 """
1271 return (self._min, self._max)
1272
1273
1274 def SetLimited(self, limited):
1275 """
1276 If called with a value of True, this function will cause the control
1277 to limit the value to fall within the bounds currently specified.
1278 If the control's value currently exceeds the bounds, it will then
1279 be limited accordingly.
1280
1281 If called with a value of False, this function will disable value
1282 limiting, but coloring of out-of-bounds values will still take
1283 place if bounds have been set for the control.
1284 """
1285 self.SetParameters(limited = limited)
1286
1287
1288 def IsLimited(self):
1289 """
1290 Returns True if the control is currently limiting the
1291 value to fall within the current bounds.
1292 """
1293 return self._limited
1294
1295 def GetLimited(self):
1296 """ (For regularization of property accessors) """
1297 return self.IsLimited
1298
1299
1300 def IsInBounds(self, value=None):
1301 """
1302 Returns True if no value is specified and the current value
1303 of the control falls within the current bounds. This function can
1304 also be called with a value to see if that value would fall within
1305 the current bounds of the given control.
1306 """
c878ceea 1307## dbg('IsInBounds(%s)' % repr(value), indent=1)
d14a1e28
RD
1308 if value is None:
1309 value = self.GetValue()
1310 else:
1311 try:
1312 value = self._GetNumValue(self._toGUI(value))
1313 except ValueError, e:
c878ceea 1314## dbg('error getting NumValue(self._toGUI(value)):', e, indent=0)
d14a1e28 1315 return False
fffd96b7 1316 if value.strip() == '':
d14a1e28
RD
1317 value = None
1318 elif self._fractionWidth:
1319 value = float(value)
1320 else:
1321 value = long(value)
1322
1323 min = self.GetMin()
1324 max = self.GetMax()
1325 if min is None: min = value
1326 if max is None: max = value
1327
1328 # if bounds set, and value is None, return False
1329 if value == None and (min is not None or max is not None):
c878ceea 1330## dbg('finished IsInBounds', indent=0)
d14a1e28
RD
1331 return 0
1332 else:
c878ceea 1333## dbg('finished IsInBounds', indent=0)
d14a1e28
RD
1334 return min <= value <= max
1335
1336
1337 def SetAllowNone(self, allow_none):
1338 """
1339 Change the behavior of the validation code, allowing control
1340 to have a value of None or not, as appropriate. If the value
1341 of the control is currently None, and allow_none is False, the
1342 value of the control will be set to the minimum value of the
1343 control, or 0 if no lower bound is set.
1344 """
1345 self._allowNone = allow_none
1346 if not allow_none and self.GetValue() is None:
1347 min = self.GetMin()
1348 if min is not None: self.SetValue(min)
1349 else: self.SetValue(0)
1350
1351
1352 def IsNoneAllowed(self):
1353 return self._allowNone
1354 def GetAllowNone(self):
1355 """ (For regularization of property accessors) """
1356 return self.IsNoneAllowed()
1357
1358 def SetAllowNegative(self, value):
1359 self.SetParameters(allowNegative=value)
1360 def IsNegativeAllowed(self):
1361 return self._allowNegative
1362 def GetAllowNegative(self):
1363 """ (For regularization of property accessors) """
1364 return self.IsNegativeAllowed()
1365
1366 def SetGroupDigits(self, value):
1367 self.SetParameters(groupDigits=value)
1368 def IsGroupingAllowed(self):
1369 return self._groupDigits
1370 def GetGroupDigits(self):
1371 """ (For regularization of property accessors) """
1372 return self.IsGroupingAllowed()
1373
1374 def SetGroupChar(self, value):
1375 self.SetParameters(groupChar=value)
1376 def GetGroupChar(self):
1377 return self._groupChar
1378
1379 def SetDecimalChar(self, value):
1380 self.SetParameters(decimalChar=value)
1381 def GetDecimalChar(self):
1382 return self._decimalChar
1383
1384 def SetSelectOnEntry(self, value):
1385 self.SetParameters(selectOnEntry=value)
1386 def GetSelectOnEntry(self):
1387 return self._selectOnEntry
1388
fffd96b7
RD
1389 def SetAutoSize(self, value):
1390 self.SetParameters(autoSize=value)
1391 def GetAutoSize(self):
1392 return self._autoSize
1393
1394
d14a1e28
RD
1395 # (Other parameter accessors are inherited from base class)
1396
1397
1398 def _toGUI( self, value, apply_limits = True ):
1399 """
1400 Conversion function used to set the value of the control; does
1401 type and bounds checking and raises ValueError if argument is
1402 not a valid value.
1403 """
c878ceea 1404## dbg('NumCtrl::_toGUI(%s)' % repr(value), indent=1)
d14a1e28 1405 if value is None and self.IsNoneAllowed():
c878ceea 1406## dbg(indent=0)
d14a1e28
RD
1407 return self._template
1408
1409 elif type(value) in (types.StringType, types.UnicodeType):
1410 value = self._GetNumValue(value)
c878ceea 1411## dbg('cleansed num value: "%s"' % value)
fffd96b7
RD
1412 if value == "":
1413 if self.IsNoneAllowed():
c878ceea 1414## dbg(indent=0)
fffd96b7
RD
1415 return self._template
1416 else:
c878ceea
RD
1417## dbg('exception raised:', e, indent=0)
1418 raise ValueError ('NumCtrl requires numeric value, passed %s'% repr(value) )
fffd96b7 1419 # else...
d14a1e28
RD
1420 try:
1421 if self._fractionWidth or value.find('.') != -1:
1422 value = float(value)
1423 else:
1424 value = long(value)
1425 except Exception, e:
c878ceea
RD
1426## dbg('exception raised:', e, indent=0)
1427 raise ValueError ('NumCtrl requires numeric value, passed %s'% repr(value) )
d14a1e28
RD
1428
1429 elif type(value) not in (types.IntType, types.LongType, types.FloatType):
c878ceea 1430## dbg(indent=0)
d14a1e28 1431 raise ValueError (
c878ceea 1432 'NumCtrl requires numeric value, passed %s'% repr(value) )
d14a1e28
RD
1433
1434 if not self._allowNegative and value < 0:
1435 raise ValueError (
1436 'control configured to disallow negative values, passed %s'% repr(value) )
1437
1438 if self.IsLimited() and apply_limits:
1439 min = self.GetMin()
1440 max = self.GetMax()
1441 if not min is None and value < min:
c878ceea 1442## dbg(indent=0)
d14a1e28
RD
1443 raise ValueError (
1444 'value %d is below minimum value of control'% value )
1445 if not max is None and value > max:
c878ceea 1446## dbg(indent=0)
d14a1e28
RD
1447 raise ValueError (
1448 'value %d exceeds value of control'% value )
1449
1450 adjustwidth = len(self._mask) - (1 * self._useParens * self._signOk)
c878ceea
RD
1451## dbg('len(%s):' % self._mask, len(self._mask))
1452## dbg('adjustwidth - groupSpace:', adjustwidth - self._groupSpace)
1453## dbg('adjustwidth:', adjustwidth)
d14a1e28
RD
1454 if self._fractionWidth == 0:
1455 s = str(long(value)).rjust(self._integerWidth)
1456 else:
1457 format = '%' + '%d.%df' % (self._integerWidth+self._fractionWidth+1, self._fractionWidth)
1458 s = format % float(value)
c878ceea 1459## dbg('s:"%s"' % s, 'len(s):', len(s))
d14a1e28 1460 if len(s) > (adjustwidth - self._groupSpace):
c878ceea 1461## dbg(indent=0)
d14a1e28
RD
1462 raise ValueError ('value %s exceeds the integer width of the control (%d)' % (s, self._integerWidth))
1463 elif s[0] not in ('-', ' ') and self._allowNegative and len(s) == (adjustwidth - self._groupSpace):
c878ceea 1464## dbg(indent=0)
d14a1e28
RD
1465 raise ValueError ('value %s exceeds the integer width of the control (%d)' % (s, self._integerWidth))
1466
1467 s = s.rjust(adjustwidth).replace('.', self._decimalChar)
1468 if self._signOk and self._useParens:
1469 if s.find('-') != -1:
1470 s = s.replace('-', '(') + ')'
1471 else:
1472 s += ' '
c878ceea 1473## dbg('returned: "%s"' % s, indent=0)
d14a1e28
RD
1474 return s
1475
1476
1477 def _fromGUI( self, value ):
1478 """
1479 Conversion function used in getting the value of the control.
1480 """
c878ceea
RD
1481## dbg(suspend=0)
1482## dbg('NumCtrl::_fromGUI(%s)' % value, indent=1)
d14a1e28
RD
1483 # One or more of the underlying text control implementations
1484 # issue an intermediate EVT_TEXT when replacing the control's
1485 # value, where the intermediate value is an empty string.
1486 # So, to ensure consistency and to prevent spurious ValueErrors,
1487 # we make the following test, and react accordingly:
1488 #
fffd96b7 1489 if value.strip() == '':
d14a1e28 1490 if not self.IsNoneAllowed():
c878ceea 1491## dbg('empty value; not allowed,returning 0', indent = 0)
d14a1e28
RD
1492 if self._fractionWidth:
1493 return 0.0
1494 else:
1495 return 0
1496 else:
c878ceea 1497## dbg('empty value; returning None', indent = 0)
d14a1e28
RD
1498 return None
1499 else:
1500 value = self._GetNumValue(value)
c878ceea 1501## dbg('Num value: "%s"' % value)
d14a1e28
RD
1502 if self._fractionWidth:
1503 try:
c878ceea 1504## dbg(indent=0)
d14a1e28
RD
1505 return float( value )
1506 except ValueError:
c878ceea 1507## dbg("couldn't convert to float; returning None")
d14a1e28
RD
1508 return None
1509 else:
1510 raise
1511 else:
1512 try:
c878ceea 1513## dbg(indent=0)
d14a1e28
RD
1514 return int( value )
1515 except ValueError:
1516 try:
c878ceea 1517## dbg(indent=0)
d14a1e28
RD
1518 return long( value )
1519 except ValueError:
c878ceea 1520## dbg("couldn't convert to long; returning None")
d14a1e28
RD
1521 return None
1522
1523 else:
1524 raise
1525 else:
c878ceea 1526## dbg('exception occurred; returning None')
d14a1e28
RD
1527 return None
1528
1529
1530 def _Paste( self, value=None, raise_on_invalid=False, just_return_value=False ):
1531 """
1532 Preprocessor for base control paste; if value needs to be right-justified
1533 to fit in control, do so prior to paste:
1534 """
339983ff 1535## dbg('NumCtrl::_Paste (value = "%s")' % value, indent=1)
d14a1e28
RD
1536 if value is None:
1537 paste_text = self._getClipboardContents()
1538 else:
1539 paste_text = value
d14a1e28 1540 sel_start, sel_to = self._GetSelection()
5f280eaa
RD
1541 orig_sel_start = sel_start
1542 orig_sel_to = sel_to
1543## dbg('selection:', (sel_start, sel_to))
1544 old_value = self._GetValue()
d14a1e28 1545
5f280eaa
RD
1546 #
1547 field = self._FindField(sel_start)
1548 edit_start, edit_end = field._extent
339983ff 1549 paste_text = paste_text.replace(self._groupChar, '').replace('(', '-').replace(')','')
5f280eaa
RD
1550 if field._insertRight and self._groupDigits:
1551 # want to paste to the left; see if it will fit:
1552 left_text = old_value[edit_start:sel_start].lstrip()
1553## dbg('len(left_text):', len(left_text))
1554## dbg('len(paste_text):', len(paste_text))
1555## dbg('sel_start - (len(left_text) + len(paste_text)) >= edit_start?', sel_start - (len(left_text) + len(paste_text)) >= edit_start)
1556 if sel_start - (len(left_text) + len(paste_text)) >= edit_start:
1557 # will fit! create effective paste text, and move cursor back to do so:
1558 paste_text = left_text + paste_text
1559 sel_start -= len(paste_text)
1560 sel_start += sel_to - orig_sel_start # decrease by amount selected
1561 else:
1562## dbg("won't fit left;", 'paste text remains: "%s"' % paste_text)
339983ff
RD
1563## dbg('adjusted start before accounting for grouping:', sel_start)
1564## dbg('adjusted paste_text before accounting for grouping: "%s"' % paste_text)
5f280eaa
RD
1565 pass
1566 if self._groupDigits and sel_start != orig_sel_start:
1567 left_len = len(old_value[:sel_to].lstrip())
1568 # remove group chars from adjusted paste string, and left pad to wipe out
1569 # old characters, so that selection will remove the right chars, and
1570 # readjust will do the right thing:
1571 paste_text = paste_text.replace(self._groupChar,'')
1572 adjcount = left_len - len(paste_text)
1573 paste_text = ' ' * adjcount + paste_text
1574 sel_start = sel_to - len(paste_text)
1575## dbg('adjusted start after accounting for grouping:', sel_start)
1576## dbg('adjusted paste_text after accounting for grouping: "%s"' % paste_text)
1577 self.SetInsertionPoint(sel_to)
1578 self.SetSelection(sel_start, sel_to)
1579
5f280eaa 1580 new_text, replace_to = MaskedEditMixin._Paste(self,
d14a1e28
RD
1581 paste_text,
1582 raise_on_invalid=raise_on_invalid,
5f280eaa
RD
1583 just_return_value=True)
1584 self._SetInsertionPoint(orig_sel_to)
1585 self._SetSelection(orig_sel_start, orig_sel_to)
1586 if not just_return_value and new_text is not None:
1587 if new_text != self._GetValue():
1588 self.modified = True
1589 if new_text == '':
1590 self.ClearValue()
1591 else:
1592 wx.CallAfter(self._SetValue, new_text)
1593 wx.CallAfter(self._SetInsertionPoint, replace_to)
1594## dbg(indent=0)
1595 else:
1596## dbg(indent=0)
1597 return new_text, replace_to
1598
1599 def _Undo(self, value=None, prev=None):
1600 '''numctrl's undo is more complicated than the base control's, due to
1601 grouping characters; we don't want to consider them when calculating
1602 the undone portion.'''
1603## dbg('NumCtrl::_Undo', indent=1)
1604 if value is None: value = self._GetValue()
1605 if prev is None: prev = self._prevValue
1606 if not self._groupDigits:
1607 ignore, (new_sel_start, new_sel_to) = BaseMaskedTextCtrl._Undo(self, value, prev, just_return_results = True)
1608 self._SetValue(prev)
1609 self._SetInsertionPoint(new_sel_start)
1610 self._SetSelection(new_sel_start, new_sel_to)
1611 self._prevSelection = (new_sel_start, new_sel_to)
1612## dbg('resetting "prev selection" to', self._prevSelection)
1613## dbg(indent=0)
1614 return
1615 # else...
1616 sel_start, sel_to = self._prevSelection
1617 edit_start, edit_end = self._FindFieldExtent(0)
1618
1619 adjvalue = self._GetNumValue(value).rjust(self._masklength)
1620 adjprev = self._GetNumValue(prev ).rjust(self._masklength)
1621
1622 # move selection to account for "ungrouped" value:
1623 left_text = value[sel_start:].lstrip()
1624 numleftgroups = len(left_text) - len(left_text.replace(self._groupChar, ''))
1625 adjsel_start = sel_start + numleftgroups
1626 right_text = value[sel_to:].lstrip()
1627 numrightgroups = len(right_text) - len(right_text.replace(self._groupChar, ''))
1628 adjsel_to = sel_to + numrightgroups
1629## dbg('adjusting "previous" selection from', (sel_start, sel_to), 'to:', (adjsel_start, adjsel_to))
1630 self._prevSelection = (adjsel_start, adjsel_to)
1631
1632 # determine appropriate selection for ungrouped undo
1633 ignore, (new_sel_start, new_sel_to) = BaseMaskedTextCtrl._Undo(self, adjvalue, adjprev, just_return_results = True)
1634
1635 # adjust new selection based on grouping:
1636 left_len = edit_end - new_sel_start
1637 numleftgroups = left_len / 3
1638 new_sel_start -= numleftgroups
1639 if numleftgroups and left_len % 3 == 0:
1640 new_sel_start += 1
1641
1642 if new_sel_start < self._masklength and prev[new_sel_start] == self._groupChar:
1643 new_sel_start += 1
1644
1645 right_len = edit_end - new_sel_to
1646 numrightgroups = right_len / 3
1647 new_sel_to -= numrightgroups
1648
1649 if new_sel_to and prev[new_sel_to-1] == self._groupChar:
1650 new_sel_to -= 1
1651
1652 if new_sel_start > new_sel_to:
1653 new_sel_to = new_sel_start
1654
1655 # for numbers, we don't care about leading whitespace; adjust selection if
1656 # it includes leading space.
1657 prev_stripped = prev.lstrip()
1658 prev_start = self._masklength - len(prev_stripped)
1659 if new_sel_start < prev_start:
1660 new_sel_start = prev_start
1661
1662## dbg('adjusted selection accounting for grouping:', (new_sel_start, new_sel_to))
1663 self._SetValue(prev)
1664 self._SetInsertionPoint(new_sel_start)
1665 self._SetSelection(new_sel_start, new_sel_to)
1666 self._prevSelection = (new_sel_start, new_sel_to)
1667## dbg('resetting "prev selection" to', self._prevSelection)
1668## dbg(indent=0)
d14a1e28
RD
1669
1670#===========================================================================
1671
1672if __name__ == '__main__':
1673
1674 import traceback
1675
b881fc78 1676 class myDialog(wx.Dialog):
d14a1e28 1677 def __init__(self, parent, id, title,
b881fc78
RD
1678 pos = wx.DefaultPosition, size = wx.DefaultSize,
1679 style = wx.DEFAULT_DIALOG_STYLE ):
1680 wx.Dialog.__init__(self, parent, id, title, pos, size, style)
d14a1e28 1681
c878ceea 1682 self.int_ctrl = NumCtrl(self, wx.NewId(), size=(55,20))
b881fc78
RD
1683 self.OK = wx.Button( self, wx.ID_OK, "OK")
1684 self.Cancel = wx.Button( self, wx.ID_CANCEL, "Cancel")
d14a1e28 1685
b881fc78
RD
1686 vs = wx.BoxSizer( wx.VERTICAL )
1687 vs.Add( self.int_ctrl, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
1688 hs = wx.BoxSizer( wx.HORIZONTAL )
1689 hs.Add( self.OK, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
1690 hs.Add( self.Cancel, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
1691 vs.Add(hs, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
d14a1e28
RD
1692
1693 self.SetAutoLayout( True )
1694 self.SetSizer( vs )
1695 vs.Fit( self )
1696 vs.SetSizeHints( self )
c878ceea 1697 self.Bind(EVT_NUM, self.OnChange, self.int_ctrl)
d14a1e28
RD
1698
1699 def OnChange(self, event):
1700 print 'value now', event.GetValue()
1701
b881fc78 1702 class TestApp(wx.App):
d14a1e28
RD
1703 def OnInit(self):
1704 try:
b881fc78
RD
1705 self.frame = wx.Frame(None, -1, "Test", (20,20), (120,100) )
1706 self.panel = wx.Panel(self.frame, -1)
1707 button = wx.Button(self.panel, -1, "Push Me", (20, 20))
1708 self.Bind(wx.EVT_BUTTON, self.OnClick, button)
d14a1e28
RD
1709 except:
1710 traceback.print_exc()
1711 return False
1712 return True
1713
1714 def OnClick(self, event):
c878ceea 1715 dlg = myDialog(self.panel, -1, "test NumCtrl")
d14a1e28
RD
1716 dlg.int_ctrl.SetValue(501)
1717 dlg.int_ctrl.SetInsertionPoint(1)
1718 dlg.int_ctrl.SetSelection(1,2)
1719 rc = dlg.ShowModal()
1720 print 'final value', dlg.int_ctrl.GetValue()
1721 del dlg
1722 self.frame.Destroy()
1723
1724 def Show(self):
1725 self.frame.Show(True)
1726
1727 try:
1728 app = TestApp(0)
1729 app.Show()
1730 app.MainLoop()
1731 except:
1732 traceback.print_exc()
1733
1734i=0
1735## To-Do's:
1736## =============================##
1737## 1. Add support for printf-style format specification.
1738## 2. Add option for repositioning on 'illegal' insertion point.
fffd96b7 1739##
5f280eaa
RD
1740## Version 1.2
1741## 1. Allowed select/replace digits.
1742## 2. Fixed undo to ignore grouping chars.
1743##
fffd96b7
RD
1744## Version 1.1
1745## 1. Fixed .SetIntegerWidth() and .SetFractionWidth() functions.
1746## 2. Added autoSize parameter, to allow manual sizing of the control.
1747## 3. Changed inheritance to use wxBaseMaskedTextCtrl, to remove exposure of
1748## nonsensical parameter methods from the control, so it will work
1749## properly with Boa.
1750## 4. Fixed allowNone bug found by user sameerc1@grandecom.net
5f280eaa 1751##