1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Documentation of numeric validator classes.
4 // Author: Vadim Zeitlin based on the submission of Fulvio Senore
6 // Copyright: (c) 2010 wxWidgets team
7 // (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 Bit masks used for numeric validator styles.
14 A combination of these flags can be used when creating wxIntegerValidator
15 and wxFloatingPointValidator objects and with their SetStyle() methods.
20 enum wxNumValidatorStyle
23 Indicates absence of any other flags.
25 This value corresponds to the default behaviour.
27 wxNUM_VAL_DEFAULT
= 0,
30 Use thousands separators in the numbers.
32 When this style is used, numbers are formatted using the thousands
33 separators after validating the user entry (if the current locale uses
34 the thousands separators character).
36 wxNUM_VAL_THOUSANDS_SEPARATOR
= 1,
39 Show a value of zero as an empty string.
41 With this style a value of zero in the associated variable is
42 translated to an empty string and an empty value of the control is
43 translated to a value of zero.
45 wxNUM_VAL_ZERO_AS_BLANK
= 2,
48 Remove trailing zeroes from the fractional part of the number.
50 This style can only be used with wxFloatingPointValidator and indicates
51 that trailing zeroes should be removed from the control text when it is
52 validated. By default, as many zeroes as needed to satisfy the
53 precision used when creating the validator will be appended.
55 For example, without this style a wxFloatingPointValidator with a
56 precision 3 will show the value of 1.5 as "1.500" after validation.
57 With this style, the value will be shown as just "1.5" (while a value
58 of e.g. 1.567 will still be shown with all the three significant
61 wxNUM_VAL_NO_TRAILING_ZEROES
66 wxNumValidator is the common base class for numeric validator classes.
68 This class is never used directly, but only as a base class for
69 wxIntegerValidator and wxFloatingPointValidator.
72 Type of the values used with this validator.
79 class wxNumValidator
: public wxValidator
82 /// Type of the values this validator is used with.
86 Sets the minimal value accepted by the validator.
88 This value is inclusive, i.e. the value equal to @a min is accepted.
90 void SetMin(ValueType min
);
93 Sets the maximal value accepted by the validator.
95 This value is inclusive, i.e. the value equal to @a max is accepted.
97 void SetMax(ValueType max
);
100 Sets both minimal and maximal values accepted by the validator.
102 Calling this is equivalent to calling both SetMin() and SetMax().
104 void SetRange(ValueType min
, ValueType max
)
108 Change the validator style.
110 Can be used to change the style of the validator after its creation.
111 The @a style parameter must be a combination of the values from
112 wxNumValidatorStyle enum.
114 void SetStyle(int style
);
118 Override base class method to format the control contents.
120 This method is called when the associated window is shown and it fills
121 it with the contents of the associated variable, if any, formatted
122 according to the validator style.
124 It does nothing if there is no associated variable.
126 virtual bool TransferToWindow();
129 Override base class method to validate the control contents.
131 This method is called to check the correctness of user input and fill
132 the associated variable with the controls numeric value. It returns
133 false if it is not a number in the configured range or if the control
134 contents is empty for a validator without wxNUM_VAL_ZERO_AS_BLANK
137 It does nothing if there is no associated variable.
139 virtual bool TransferFromWindow();
143 Validator for text entries used for integer entry.
145 This validator can be used with wxTextCtrl or wxComboBox (and potentially
146 any other class implementing wxTextEntry interface) to check that only
147 valid integer values can be entered into them.
149 This is a template class which can be instantiated for all the integer types
150 (i.e. @c short, @c int, @c long and <code>long long</code> if available) as
151 well as their unsigned versions.
153 By default this validator accepts any integer values in the range
154 appropriate for its type, e.g. <code>INT_MIN..INT_MAX</code> for @c int or
155 <code>0..USHRT_MAX</code> for <code>unsigned short</code>. This range can
156 be restricted further by calling SetMin() and SetMax() or SetRange()
157 methods inherited from the base class.
159 A simple example of using this class:
161 class MyDialog : public wxDialog
167 // Allow positive integers and display them with thousands
169 wxIntegerValidator<unsigned long>
170 val(&m_value, wxNUM_VAL_THOUSANDS_SEPARATOR);
172 // If the variable were of type "long" and not "unsigned long"
173 // we would have needed to call val.SetMin(0) but as it is,
174 // this is not needed.
176 // Associate it with the text control:
177 new wxTextCtrl(this, ..., val);
181 unsigned long m_value;
184 For more information, please see @ref overview_validator.
189 @see @ref overview_validator, wxValidator, wxGenericValidator,
190 wxTextValidator, wxMakeIntegerValidator()
194 template <typename T
>
195 class wxIntegerValidator
: public wxNumValidator
<T
>
198 /// Type of the values this validator is used with.
202 Validator constructor.
205 A pointer to the variable associated with the validator. If non
206 @NULL, this variable should have a lifetime equal to or longer than
207 the validator lifetime (which is usually determined by the lifetime
210 A combination of wxNumValidatorStyle enum values with the exception
211 of wxNUM_VAL_NO_TRAILING_ZEROES which can't be used here.
213 wxIntegerValidator(ValueType
*value
= NULL
, int style
= wxNUM_VAL_DEFAULT
);
217 Creates a wxIntegerValidator object with automatic type deduction.
219 This function can be used to create wxIntegerValidator object without
220 explicitly specifying its type, e.g. write just:
222 new wxTextCtrl(..., wxMakeIntegerValidator(&m_var));
224 instead of more verbose
226 new wxTextCtrl(..., wxIntegerValidator<unsigned long>(&m_var));
231 template <typename T
>
232 wxIntegerValidator
<T
>
233 wxMakeIntegerValidator(T
*value
, int style
= wxNUM_VAL_DEFAULT
);
236 Validator for text entries used for floating point numbers entry.
238 This validator can be used with wxTextCtrl or wxComboBox (and potentially
239 any other class implementing wxTextEntry interface) to check that only
240 valid floating point values can be entered into them. Currently only fixed
241 format is supported on input, i.e. scientific format with mantissa and
242 exponent is not supported.
244 This template class can be instantiated for either @c float or @c double,
245 <code>long double</code> values are not currently supported.
247 Similarly to wxIntegerValidator<>, the range for the accepted values is by
248 default set appropriately for the type. Additionally, this validator allows
249 to specify the maximum number of digits that can be entered after the
250 decimal separator. By default this is also set appropriately for the type
251 used, e.g. 6 for @c float and 15 for @c double on a typical IEEE-754-based
252 implementation. As with the range, the precision can be restricted after
253 the validator creation if necessary.
255 A simple example of using this class:
257 class MyDialog : public wxDialog
263 // Allow floating point numbers from 0 to 100 with 2 decimal
264 // digits only and handle empty string as 0 by default.
265 wxFloatingPointValidator<float>
266 val(2, &m_value, wxNUM_VAL_ZERO_AS_BLANK);
267 val.SetRange(0, 100);
269 // Associate it with the text control:
270 new wxTextCtrl(this, ..., val);
278 For more information, please see @ref overview_validator.
283 @see @ref overview_validator, wxValidator, wxGenericValidator,
284 wxTextValidator, wxMakeIntegerValidator()
288 template <typename T
>
289 class wxFloatingPointValidator
: public wxNumValidator
<T
>
292 /// Type of the values this validator is used with.
296 Validator constructor.
299 A pointer to the variable associated with the validator. If non
300 @NULL, this variable should have a lifetime equal to or longer than
301 the validator lifetime (which is usually determined by the lifetime
304 A combination of wxNumValidatorStyle enum values.
306 The number of decimal digits after the decimal separator to show
310 wxFloatingPointValidator(ValueType
*value
= NULL
,
311 int style
= wxNUM_VAL_DEFAULT
);
312 wxFloatingPointValidator(int precision
,
313 ValueType
*value
= NULL
,
314 int style
= wxNUM_VAL_DEFAULT
);
321 Precision is the number of digits shown (and accepted on input)
322 after the decimal point. By default this is set to the maximal
323 precision supported by the type handled by the validator in its
326 void SetPrecision(unsigned precision
);
330 Creates a wxFloatingPointValidator object with automatic type deduction.
332 Similarly to wxMakeIntegerValidator(), this function allows to avoid
333 explicitly specifying the validator type.
338 template <typename T
>
339 inline wxFloatingPointValidator
<T
>
340 wxMakeFloatingPointValidator(T
*value
, int style
= wxNUM_VAL_DEFAULT
);
342 template <typename T
>
343 inline wxFloatingPointValidator
<T
>
344 wxMakeFloatingPointValidator(int precision
,
345 T
*value
, int style
= wxNUM_VAL_DEFAULT
);