PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ExtLibs/wxWidgets/interface/wx/valnum.h

https://bitbucket.org/cafu/cafu
C Header | 368 lines | 48 code | 24 blank | 296 comment | 0 complexity | 54716ebcf3a9c2b0bd1011ecab48e305 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, BSD-3-Clause, LGPL-3.0, LGPL-2.1, AGPL-3.0
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/valnum.h
  3. // Purpose: Documentation of numeric validator classes.
  4. // Author: Vadim Zeitlin based on the submission of Fulvio Senore
  5. // Created: 2010-11-06
  6. // Copyright: (c) 2010 wxWidgets team
  7. // (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. /**
  11. Bit masks used for numeric validator styles.
  12. A combination of these flags can be used when creating wxIntegerValidator
  13. and wxFloatingPointValidator objects and with their SetStyle() methods.
  14. @since 2.9.2
  15. @category{validator}
  16. */
  17. enum wxNumValidatorStyle
  18. {
  19. /**
  20. Indicates absence of any other flags.
  21. This value corresponds to the default behaviour.
  22. */
  23. wxNUM_VAL_DEFAULT = 0,
  24. /**
  25. Use thousands separators in the numbers.
  26. When this style is used, numbers are formatted using the thousands
  27. separators after validating the user entry (if the current locale uses
  28. the thousands separators character).
  29. */
  30. wxNUM_VAL_THOUSANDS_SEPARATOR = 1,
  31. /**
  32. Show a value of zero as an empty string.
  33. With this style a value of zero in the associated variable is
  34. translated to an empty string and an empty value of the control is
  35. translated to a value of zero.
  36. */
  37. wxNUM_VAL_ZERO_AS_BLANK = 2,
  38. /**
  39. Remove trailing zeroes from the fractional part of the number.
  40. This style can only be used with wxFloatingPointValidator and indicates
  41. that trailing zeroes should be removed from the control text when it is
  42. validated. By default, as many zeroes as needed to satisfy the
  43. precision used when creating the validator will be appended.
  44. For example, without this style a wxFloatingPointValidator with a
  45. precision 3 will show the value of 1.5 as "1.500" after validation.
  46. With this style, the value will be shown as just "1.5" (while a value
  47. of e.g. 1.567 will still be shown with all the three significant
  48. digits, of course).
  49. */
  50. wxNUM_VAL_NO_TRAILING_ZEROES
  51. };
  52. /**
  53. wxNumValidator is the common base class for numeric validator classes.
  54. This class is never used directly, but only as a base class for
  55. wxIntegerValidator and wxFloatingPointValidator.
  56. @tparam T
  57. Type of the values used with this validator.
  58. @category{validator}
  59. @since 2.9.2
  60. */
  61. template <typename T>
  62. class wxNumValidator : public wxValidator
  63. {
  64. public:
  65. /// Type of the values this validator is used with.
  66. typedef T ValueType;
  67. /**
  68. Sets the minimal value accepted by the validator.
  69. This value is inclusive, i.e. the value equal to @a min is accepted.
  70. */
  71. void SetMin(ValueType min);
  72. /**
  73. Sets the maximal value accepted by the validator.
  74. This value is inclusive, i.e. the value equal to @a max is accepted.
  75. */
  76. void SetMax(ValueType max);
  77. /**
  78. Sets both minimal and maximal values accepted by the validator.
  79. Calling this is equivalent to calling both SetMin() and SetMax().
  80. */
  81. void SetRange(ValueType min, ValueType max);
  82. /**
  83. Change the validator style.
  84. Can be used to change the style of the validator after its creation.
  85. The @a style parameter must be a combination of the values from
  86. wxNumValidatorStyle enum.
  87. */
  88. void SetStyle(int style);
  89. /**
  90. Override base class method to format the control contents.
  91. This method is called when the associated window is shown and it fills
  92. it with the contents of the associated variable, if any, formatted
  93. according to the validator style.
  94. It does nothing if there is no associated variable.
  95. */
  96. virtual bool TransferToWindow();
  97. /**
  98. Override base class method to validate the control contents.
  99. This method is called to check the correctness of user input and fill
  100. the associated variable with the controls numeric value. It returns
  101. false if it is not a number in the configured range or if the control
  102. contents is empty for a validator without wxNUM_VAL_ZERO_AS_BLANK
  103. style.
  104. It does nothing if there is no associated variable.
  105. */
  106. virtual bool TransferFromWindow();
  107. };
  108. /**
  109. Validator for text entries used for integer entry.
  110. This validator can be used with wxTextCtrl or wxComboBox (and potentially
  111. any other class implementing wxTextEntry interface) to check that only
  112. valid integer values can be entered into them.
  113. This is a template class which can be instantiated for all the integer types
  114. (i.e. @c short, @c int, @c long and <code>long long</code> if available) as
  115. well as their unsigned versions.
  116. By default this validator accepts any integer values in the range
  117. appropriate for its type, e.g. <code>INT_MIN..INT_MAX</code> for @c int or
  118. <code>0..USHRT_MAX</code> for <code>unsigned short</code>. This range can
  119. be restricted further by calling SetMin() and SetMax() or SetRange()
  120. methods inherited from the base class.
  121. When the validator displays integers with thousands separators, the
  122. character used for the separators (usually "." or ",") depends on the locale
  123. set with wxLocale (note that you shouldn't change locale with setlocale()
  124. as this can result in a mismatch between the thousands separator used by
  125. wxLocale and the one used by the run-time library).
  126. A simple example of using this class:
  127. @code
  128. class MyDialog : public wxDialog
  129. {
  130. public:
  131. MyDialog()
  132. {
  133. ...
  134. // Allow positive integers and display them with thousands
  135. // separators.
  136. wxIntegerValidator<unsigned long>
  137. val(&m_value, wxNUM_VAL_THOUSANDS_SEPARATOR);
  138. // If the variable were of type "long" and not "unsigned long"
  139. // we would have needed to call val.SetMin(0) but as it is,
  140. // this is not needed.
  141. // Associate it with the text control:
  142. new wxTextCtrl(this, ..., val);
  143. }
  144. private:
  145. unsigned long m_value;
  146. };
  147. @endcode
  148. For more information, please see @ref overview_validator.
  149. @library{wxcore}
  150. @category{validator}
  151. @see @ref overview_validator, wxValidator, wxGenericValidator,
  152. wxTextValidator, wxMakeIntegerValidator()
  153. @since 2.9.2
  154. */
  155. template <typename T>
  156. class wxIntegerValidator : public wxNumValidator<T>
  157. {
  158. public:
  159. /// Type of the values this validator is used with.
  160. typedef T ValueType;
  161. /**
  162. Validator constructor.
  163. @param value
  164. A pointer to the variable associated with the validator. If non
  165. @NULL, this variable should have a lifetime equal to or longer than
  166. the validator lifetime (which is usually determined by the lifetime
  167. of the window).
  168. @param style
  169. A combination of wxNumValidatorStyle enum values with the exception
  170. of wxNUM_VAL_NO_TRAILING_ZEROES which can't be used here.
  171. */
  172. wxIntegerValidator(ValueType *value = NULL, int style = wxNUM_VAL_DEFAULT);
  173. };
  174. /**
  175. Creates a wxIntegerValidator object with automatic type deduction.
  176. This function can be used to create wxIntegerValidator object without
  177. explicitly specifying its type, e.g. write just:
  178. @code
  179. new wxTextCtrl(..., wxMakeIntegerValidator(&m_var));
  180. @endcode
  181. instead of more verbose
  182. @code
  183. new wxTextCtrl(..., wxIntegerValidator<unsigned long>(&m_var));
  184. @endcode
  185. @since 2.9.2
  186. */
  187. template <typename T>
  188. wxIntegerValidator<T>
  189. wxMakeIntegerValidator(T *value, int style = wxNUM_VAL_DEFAULT);
  190. /**
  191. Validator for text entries used for floating point numbers entry.
  192. This validator can be used with wxTextCtrl or wxComboBox (and potentially
  193. any other class implementing wxTextEntry interface) to check that only
  194. valid floating point values can be entered into them. Currently only fixed
  195. format is supported on input, i.e. scientific format with mantissa and
  196. exponent is not supported.
  197. This template class can be instantiated for either @c float or @c double,
  198. <code>long double</code> values are not currently supported.
  199. Similarly to wxIntegerValidator<>, the range for the accepted values is by
  200. default set appropriately for the type. Additionally, this validator allows
  201. to specify the maximum number of digits that can be entered after the
  202. decimal separator. By default this is also set appropriately for the type
  203. used, e.g. 6 for @c float and 15 for @c double on a typical IEEE-754-based
  204. implementation. As with the range, the precision can be restricted after
  205. the validator creation if necessary.
  206. When the validator displays numbers with decimal or thousands separators,
  207. the characters used for the separators (usually "." or ",") depend on the
  208. locale set with wxLocale (note that you shouldn't change locale with
  209. setlocale() as this can result in a mismatch between the separators used by
  210. wxLocale and the one used by the run-time library).
  211. A simple example of using this class:
  212. @code
  213. class MyDialog : public wxDialog
  214. {
  215. public:
  216. MyDialog()
  217. {
  218. ...
  219. // Allow floating point numbers from 0 to 100 with 2 decimal
  220. // digits only and handle empty string as 0 by default.
  221. wxFloatingPointValidator<float>
  222. val(2, &m_value, wxNUM_VAL_ZERO_AS_BLANK);
  223. val.SetRange(0, 100);
  224. // Associate it with the text control:
  225. new wxTextCtrl(this, ..., val);
  226. }
  227. private:
  228. float m_value;
  229. };
  230. @endcode
  231. For more information, please see @ref overview_validator.
  232. @library{wxcore}
  233. @category{validator}
  234. @see @ref overview_validator, wxValidator, wxGenericValidator,
  235. wxTextValidator, wxMakeIntegerValidator()
  236. @since 2.9.2
  237. */
  238. template <typename T>
  239. class wxFloatingPointValidator : public wxNumValidator<T>
  240. {
  241. public:
  242. /// Type of the values this validator is used with.
  243. typedef T ValueType;
  244. /**
  245. Constructor for validator using the default precision.
  246. @param value
  247. A pointer to the variable associated with the validator. If non
  248. @NULL, this variable should have a lifetime equal to or longer than
  249. the validator lifetime (which is usually determined by the lifetime
  250. of the window).
  251. @param style
  252. A combination of wxNumValidatorStyle enum values.
  253. */
  254. wxFloatingPointValidator(ValueType *value = NULL,
  255. int style = wxNUM_VAL_DEFAULT);
  256. /**
  257. Constructor for validator specifying the precision.
  258. @param value
  259. A pointer to the variable associated with the validator. If non
  260. @NULL, this variable should have a lifetime equal to or longer than
  261. the validator lifetime (which is usually determined by the lifetime
  262. of the window).
  263. @param style
  264. A combination of wxNumValidatorStyle enum values.
  265. @param precision
  266. The number of decimal digits after the decimal separator to show
  267. and accept.
  268. */
  269. wxFloatingPointValidator(int precision,
  270. ValueType *value = NULL,
  271. int style = wxNUM_VAL_DEFAULT);
  272. /**
  273. Set precision.
  274. Precision is the number of digits shown (and accepted on input)
  275. after the decimal point. By default this is set to the maximal
  276. precision supported by the type handled by the validator in its
  277. constructor.
  278. */
  279. void SetPrecision(unsigned precision);
  280. };
  281. /**
  282. Creates a wxFloatingPointValidator object with automatic type deduction.
  283. Similarly to wxMakeIntegerValidator(), this function allows to avoid
  284. explicitly specifying the validator type.
  285. @since 2.9.2
  286. */
  287. //@{
  288. template <typename T>
  289. inline wxFloatingPointValidator<T>
  290. wxMakeFloatingPointValidator(T *value, int style = wxNUM_VAL_DEFAULT);
  291. template <typename T>
  292. inline wxFloatingPointValidator<T>
  293. wxMakeFloatingPointValidator(int precision,
  294. T *value, int style = wxNUM_VAL_DEFAULT);
  295. //@}