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