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 When the validator displays integers with thousands separators, the
160 character used for the separators (usually "." or ",") depends on the locale
161 set with wxLocale (note that you shouldn't change locale with setlocale()
162 as this can result in a mismatch between the thousands separator used by
163 wxLocale and the one used by the run-time library).
165 A simple example of using this class:
167 class MyDialog : public wxDialog
173 // Allow positive integers and display them with thousands
175 wxIntegerValidator<unsigned long>
176 val(&m_value, wxNUM_VAL_THOUSANDS_SEPARATOR);
178 // If the variable were of type "long" and not "unsigned long"
179 // we would have needed to call val.SetMin(0) but as it is,
180 // this is not needed.
182 // Associate it with the text control:
183 new wxTextCtrl(this, ..., val);
187 unsigned long m_value;
190 For more information, please see @ref overview_validator.
195 @see @ref overview_validator, wxValidator, wxGenericValidator,
196 wxTextValidator, wxMakeIntegerValidator()
200 template <typename T
>
201 class wxIntegerValidator
: public wxNumValidator
<T
>
204 /// Type of the values this validator is used with.
208 Validator constructor.
211 A pointer to the variable associated with the validator. If non
212 @NULL, this variable should have a lifetime equal to or longer than
213 the validator lifetime (which is usually determined by the lifetime
216 A combination of wxNumValidatorStyle enum values with the exception
217 of wxNUM_VAL_NO_TRAILING_ZEROES which can't be used here.
219 wxIntegerValidator(ValueType
*value
= NULL
, int style
= wxNUM_VAL_DEFAULT
);
223 Creates a wxIntegerValidator object with automatic type deduction.
225 This function can be used to create wxIntegerValidator object without
226 explicitly specifying its type, e.g. write just:
228 new wxTextCtrl(..., wxMakeIntegerValidator(&m_var));
230 instead of more verbose
232 new wxTextCtrl(..., wxIntegerValidator<unsigned long>(&m_var));
237 template <typename T
>
238 wxIntegerValidator
<T
>
239 wxMakeIntegerValidator(T
*value
, int style
= wxNUM_VAL_DEFAULT
);
242 Validator for text entries used for floating point numbers entry.
244 This validator can be used with wxTextCtrl or wxComboBox (and potentially
245 any other class implementing wxTextEntry interface) to check that only
246 valid floating point values can be entered into them. Currently only fixed
247 format is supported on input, i.e. scientific format with mantissa and
248 exponent is not supported.
250 This template class can be instantiated for either @c float or @c double,
251 <code>long double</code> values are not currently supported.
253 Similarly to wxIntegerValidator<>, the range for the accepted values is by
254 default set appropriately for the type. Additionally, this validator allows
255 to specify the maximum number of digits that can be entered after the
256 decimal separator. By default this is also set appropriately for the type
257 used, e.g. 6 for @c float and 15 for @c double on a typical IEEE-754-based
258 implementation. As with the range, the precision can be restricted after
259 the validator creation if necessary.
261 When the validator displays numbers with decimal or thousands separators,
262 the characters used for the separators (usually "." or ",") depend on the
263 locale set with wxLocale (note that you shouldn't change locale with
264 setlocale() as this can result in a mismatch between the separators used by
265 wxLocale and the one used by the run-time library).
267 A simple example of using this class:
269 class MyDialog : public wxDialog
275 // Allow floating point numbers from 0 to 100 with 2 decimal
276 // digits only and handle empty string as 0 by default.
277 wxFloatingPointValidator<float>
278 val(2, &m_value, wxNUM_VAL_ZERO_AS_BLANK);
279 val.SetRange(0, 100);
281 // Associate it with the text control:
282 new wxTextCtrl(this, ..., val);
290 For more information, please see @ref overview_validator.
295 @see @ref overview_validator, wxValidator, wxGenericValidator,
296 wxTextValidator, wxMakeIntegerValidator()
300 template <typename T
>
301 class wxFloatingPointValidator
: public wxNumValidator
<T
>
304 /// Type of the values this validator is used with.
308 Validator constructor.
311 A pointer to the variable associated with the validator. If non
312 @NULL, this variable should have a lifetime equal to or longer than
313 the validator lifetime (which is usually determined by the lifetime
316 A combination of wxNumValidatorStyle enum values.
318 The number of decimal digits after the decimal separator to show
322 wxFloatingPointValidator(ValueType
*value
= NULL
,
323 int style
= wxNUM_VAL_DEFAULT
);
324 wxFloatingPointValidator(int precision
,
325 ValueType
*value
= NULL
,
326 int style
= wxNUM_VAL_DEFAULT
);
333 Precision is the number of digits shown (and accepted on input)
334 after the decimal point. By default this is set to the maximal
335 precision supported by the type handled by the validator in its
338 void SetPrecision(unsigned precision
);
342 Creates a wxFloatingPointValidator object with automatic type deduction.
344 Similarly to wxMakeIntegerValidator(), this function allows to avoid
345 explicitly specifying the validator type.
350 template <typename T
>
351 inline wxFloatingPointValidator
<T
>
352 wxMakeFloatingPointValidator(T
*value
, int style
= wxNUM_VAL_DEFAULT
);
354 template <typename T
>
355 inline wxFloatingPointValidator
<T
>
356 wxMakeFloatingPointValidator(int precision
,
357 T
*value
, int style
= wxNUM_VAL_DEFAULT
);