OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
validate.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5//----------------------------------------------------------------------------
6
7#if !defined(OWL_VALIDATE_H)
8#define OWL_VALIDATE_H
9
10#include <owl/private/defs.h>
11#if defined(BI_HAS_PRAGMA_ONCE)
12# pragma once
13#endif
14
15#include <owl/window.h>
16#include <owl/bitset.h>
17#include <owl/contain.h>
18#include <owl/template.h>
19
20
21namespace owl {
22
23#include <owl/preclass.h>
24
25/// \addtogroup validator
26/// @{
27
28class _OWLCLASS TXValidator;
29
30//
31/// Bit-register flags used with TValidator::SetOption and UnsetOption.
32//
34{
35 voFill = 0x0001, ///< Option to fill in chars on IsValidInput checks.
36 ///< Used by picture validators to indicate whether to fill in literal
37 ///< characters as the user types.
38
39 voTransfer = 0x0002, ///< Option to perform conversion & transfer.
40 ///< The validator handles data transfer for the input line. Currently
41 ///< only used by range validators.
42
43 voOnAppend = 0x0004, ///< Option to only validate input on appending.
44 ///< Used by picture validators to determine how to interact with edit
45 ///< controls.
46
47 voReserved = 0x00F8 ///< Reserved for future use.
48};
49
50//
51/// \class TValidator
52// ~~~~~ ~~~~~~~~~~
53/// A streamable class, TValidator defines an abstract data validation object.
54/// Although you will never actually create an instance of TValidator, it provides
55/// the abstract functions for the other data validation objects.
56///
57/// The VALIDATE.CPP sample program on BC5.0x distribution disk derives TValidateApp
58/// from TApplication in the following manner:
59/// \code
60/// class TValidateApp : public TApplication {
61/// public:
62/// TValidateApp() : TApplication("ValidateApp") { }
63/// void InitMainWindow() {
64/// MainWindow = new TTestWindow(0, "Validate Dialog Input");
65/// }
66/// \endcode
67/// The program displays the following message box if the user enters an invalid
68/// employee ID:
69/// \image html bm275.BMP
70//
72 public:
73 TValidator();
74 virtual ~TValidator();
75
76 virtual void Error(TWindow* owner); // !CQ incompatible change, needs doc!
77 virtual bool IsValidInput(LPTSTR input, bool suppressFill);
78 virtual bool IsValid(LPCTSTR input);
79 virtual uint Transfer(LPTSTR text, void* buffer, TTransferDirection direction);
80 virtual int Adjust(tstring& text, int& begPos, int& endPos, int amount); // !CQ new!
81
82 // Checks input against validator for completeness. Never modifies input.
83 // Calls error if not valid.
84 //
85 bool Valid(LPCTSTR str, TWindow* owner = 0);
86 bool Valid(const tstring& str, TWindow* owner = 0) {return Valid(str.c_str(), owner);}
87 bool HasOption(int option);
88 void SetOption(int option);
89 void UnsetOption(int option);
90
91 protected:
92/// A bitmap member used to control options for various descendants of TValidator.
93/// By default, the TValidator constructor clears all the bits in Options.
95
97};
98
100
101//
102/// \class TXValidator
103// ~~~~~ ~~~~~~~~~~~
104/// A nested class, TXValidator describes an exception that results from an invalid
105/// validator object. That is, if a validator expression is not valid, this
106/// exception is thrown.
108 public:
110
111 TXValidator* Clone();
112 void Throw();
113
114 static void Raise();
115};
116
117//
118/// \enum TPicResult
119// ~~~~ ~~~~~~~~~~~~~~~~~~~
120/// Validation result type
121//
122/// TPicResult is the result type returned by the Picture member function of
123/// TPXPictureValidator. The result type indicates whether the data entered into the
124/// edit control matches a specified format. For example, prIncomplete indicates
125/// that the data entered is missing some information that was specified in the
126/// format picture of the data.
127//
137
138//
139/// \class TPXPictureValidator
140// ~~~~~ ~~~~~~~~~~~~~~~~~~~
141/// TPXPictureValidator objects compare user input with a picture of a data format
142/// to determine the validity of entered data. The pictures are compatible with the
143/// pictures Borland's Paradox relational database uses to control data entry. For a
144/// complete description of picture specifiers, see the Picture member function.
146 public:
148 TPXPictureValidator(const tstring& pic, bool autoFill=false);
149
150 // Override TValidator's virtuals
151 //
152 void Error(TWindow* owner);
153 bool IsValidInput(LPTSTR str, bool suppressFill);
154 bool IsValid(LPCTSTR str);
155 int Adjust(tstring& text, int& begPos, int& endPos, int amount); // !CQ new!
156 virtual TPicResult Picture(LPTSTR input, bool autoFill=false);
157
158 const tstring& GetPic() const;
159 void SetPic(const tstring& pic);
160
162/// Points to a string containing the picture that specifies the format for data in
163/// the associated edit control. The constructor or SetPic sets Pic to a string that
164/// is passed.
165 tstring Pic;
166
167 private:
168 void Init(bool autoFill);
169 bool IsComplete(TPicResult rslt);
170 bool IsIncomplete(TPicResult rslt);
171 void ToGroupEnd(uint termCh, uint& i);
172 bool SkipToComma(uint termCh, uint& i);
173 uint CalcTerm(uint termCh, uint i);
174 TPicResult Iteration(LPTSTR input, uint termCh, uint& i, uint& j);
176 TPicResult CheckComplete(uint termCh, uint& i, TPicResult rslt);
177
179 TPicResult Process(LPTSTR input, uint termCh, uint& i, uint& j);
180 bool SyntaxCheck();
181
183};
184
186
187//
188/// \class TFilterValidator
189// ~~~~~ ~~~~~~~~~~~~~~~~
190/// A streamable class, TFilterValidator checks an input field as the user types
191/// into it. The validator holds a set of allowed characters. When the user enters a
192/// character, the filter validator indicates whether the character is valid or
193/// invalid. See TValidator for an example of input validation.
195 public:
197
198 // Override TValidator's virtuals
199 //
200 void Error(TWindow* owner);
201 bool IsValid(LPCTSTR str);
202 bool IsValidInput(LPTSTR str, bool suppressFill);
203
204 protected:
205 const TCharSet& GetValidChars();
206 void SetValidChars(const TCharSet& vc);
207
209/// Contains the set of all characters the user can type. For example, to allow only
210/// numeric digits, set ValidChars to "0-9". ValidChars is set by the validChars
211/// parameter passed to the constructor.
212 TCharSet ValidChars;
213
215};
216
218
219//
220/// \class TRangeValidator
221// ~~~~~ ~~~~~~~~~~~~~~~
222/// Determines whether the data typed by a user falls within a designated range of
223/// integers. TRangeValidator is a streamable class.
225{
226 public:
227 using TExtent = int;
228 struct TRange { TExtent Min, Max; };
230 TRangeValidator(const TRange& range);
231
232 // Override TValidator's virtuals.
233 //
234 void Error(TWindow* owner);
235 bool IsValid(LPCTSTR str);
236 uint Transfer(LPTSTR str, void* buffer, TTransferDirection direction);
237 int Adjust(tstring& text, int& begPos, int& endPos, int amount); // !CQ new!
238
239 // Range setting and retrieval functions.
240 //
241 auto GetMin() const -> TExtent;
242 void SetMin(TExtent minValue);
243 auto GetMax() const -> TExtent;
244 void SetMax(TExtent maxValue);
245 auto GetRange() const -> TRange;
246 void SetRange(const TRange& range);
247
249/// Range contains Min and Max.
250/// Min is the lowest valid long integer value for the edit control.
251/// Max is the highest valid long integer value for the edit control.
252 TRange Range;
253
255};
256
258
259//
260/// \class TLookupValidator
261// ~~~~~ ~~~~~~~~~~~~~~~~
262/// A streamable class, TLookupValidator compares the string typed by a user with a
263/// list of acceptable values. TLookupValidator is an abstract validator type from
264/// which you can derive useful lookup validators. You will never create an instance
265/// of TLookupValidator. When you create a lookup validator type, you need to
266/// specify a list of valid items and override the Lookup method to return true only
267/// if the user input matches an item in that list. One example of a working
268/// descendant of TLookupValidator is TStringLookupValidator.
270 public:
272
273 // Override TValidator's virtuals
274 //
275 bool IsValid(LPCTSTR str);
276
277 // Virtual lookup of a string
278 //
279 virtual bool Lookup(LPCTSTR str);
280
282};
283
285
286
287//
288/// \class TStringLookupValidator
289// ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
290/// Derived from TLookupValidator, TStringLookupValidator is a streamable class. A
291/// TStringLookupValidator object verifies the data in its associated edit control
292/// by searching through a collection of valid strings. You can use string-lookup
293/// validators when your edit control needs to accept only members of a certain set
294/// of strings.
296 public:
299
300 // Override TValidator's virtuals
301 //
302 void Error(TWindow* owner);
303 int Adjust(tstring& text, int& begPos, int& endPos, int amount); // !CQ new!
304
305 // Override TLookupValidator's virtuals
306 //
307 bool Lookup(LPCTSTR str);
308
309 void NewStringList(TSortedStringArray* strings);
310
311 protected:
312 const TSortedStringArray* GetStrings() const;
313 void SetStrings(TSortedStringArray* strings);
314
316/// Points to a string collection containing all the valid strings the user can
317/// type. If Strings is NULL, all input is validated.
318 TSortedStringArray* Strings;
319
321};
322
324
325/// @}
326
327#include <owl/posclass.h>
328
329//----------------------------------------------------------------------------
330// Inline Implementation
331//
332
333//
334/// Returns true if IsValid returns true. Otherwise, calls Error and returns false.
335/// A validator's Valid method is called by the IsValid method of its associated
336/// edit control.
337/// Edit controls with associated validator objects call the validator's Valid
338/// method under two conditions. The first condition is when the edit control's
339/// IsValid method is called with the reportErr parameter set to true. The second
340/// condition is when the dialog box that contains the edit control calls Valid for
341/// all its controls, usually because the user requested to close the dialog box or
342/// to accept an entry screen.
343//
344inline bool TValidator::Valid(LPCTSTR str, TWindow* owner)
345{
346 if (!IsValid(str)) {
347 Error(owner);
348 return false;
349 }
350 return true;
351}
352
353//
354/// Gets the Options bits. Returns true if a specified option is set.
355//
356inline bool TValidator::HasOption(int option)
357{
358 return Options & option;
359}
360
361//
362/// Sets the bits for the Options data member.
363//
364inline void TValidator::SetOption(int option)
365{
366 Options |= uint16(option);
367}
368
369//
370/// Unsets the bits specified in the Options data member.
371//
372inline void TValidator::UnsetOption(int option)
373{
374 Options &= uint16(~option);
375}
376
377//
378/// Returns the picture mask used by the validator.
379//
380inline const tstring& TPXPictureValidator::GetPic() const
381{
382 return Pic;
383}
384
385//
386/// Sets the picture mask for the validator.
387//
388inline void TPXPictureValidator::SetPic(const tstring& pic)
389{
390 Pic = pic;
391 if (!SyntaxCheck())
392 TXValidator::Raise();
393}
394
395//
396/// Returns the valid character set for the validator.
397//
398inline const TCharSet& TFilterValidator::GetValidChars()
399{
400 return ValidChars;
401}
402
403//
404/// Sets the valid character set for the validator.
405//
406inline void TFilterValidator::SetValidChars(const TCharSet& vc)
407{
408 ValidChars = vc;
409}
410
411//
412/// Constructs a range validator object using `minValue` and `maxValue` as
413/// the range of possible values.
414//
418
419//
420/// Returns the minimum number the validator can accept.
421//
422inline auto TRangeValidator::GetMin() const -> TExtent
423{
424 return Range.Min;
425}
426
427
428//
429/// Returns the maximum number the validator can accept.
430//
431inline auto TRangeValidator::GetMax() const -> TExtent
432{
433 return Range.Max;
434}
435
436//
437/// Returns the given the minimum and maximum number the validator can accept.
438//
439inline auto TRangeValidator::GetRange() const -> TRange
440{
441 return Range;
442}
443
444//
445/// Returns the set of valid strings used by the validator.
446//
448{
449 return Strings;
450}
451
452//
453/// Sets the valid strings used by the validator.
454//
456{
457 delete Strings;
458 Strings = strings;
459}
460
461} // OWL namespace
462
463
464#endif // OWL_VALIDATE_H
Definition of a bit set and a character set.
Derived from TBitSet, TCharSet sets and clears bytes for a group of characters.
Definition bitset.h:125
A streamable class, TFilterValidator checks an input field as the user types into it.
Definition validate.h:194
A streamable class, TLookupValidator compares the string typed by a user with a list of acceptable va...
Definition validate.h:269
TPXPictureValidator objects compare user input with a picture of a data format to determine the valid...
Definition validate.h:145
Determines whether the data typed by a user falls within a designated range of integers.
Definition validate.h:225
auto GetRange() const -> TRange
Returns the given the minimum and maximum number the validator can accept.
Definition validate.h:439
auto GetMax() const -> TExtent
Returns the maximum number the validator can accept.
Definition validate.h:431
auto GetMin() const -> TExtent
Returns the minimum number the validator can accept.
Definition validate.h:422
Classes that inherit from TStreamableBase are known as streamable classes (their objects can be writt...
Definition objstrm.h:108
Derived from TLookupValidator, TStringLookupValidator is a streamable class.
Definition validate.h:295
const TSortedStringArray * GetStrings() const
Returns the set of valid strings used by the validator.
Definition validate.h:447
void SetStrings(TSortedStringArray *strings)
Sets the valid strings used by the validator.
Definition validate.h:455
A streamable class, TValidator defines an abstract data validation object.
Definition validate.h:71
uint16 Options
A bitmap member used to control options for various descendants of TValidator.
Definition validate.h:94
bool Valid(const tstring &str, TWindow *owner=0)
Definition validate.h:86
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
A nested class, TXValidator describes an exception that results from an invalid validator object.
Definition validate.h:107
Definition of container classes used and made available by OWL.
#define DECLARE_STREAMABLE_OWL(cls, ver)
Definition objstrm.h:1529
#define DECLARE_STREAMABLE_INLINES(cls)
Definition objstrm.h:1538
TValidatorOptions
Bit-register flags used with TValidator::SetOption and UnsetOption.
Definition validate.h:34
TPicResult
Validation result type.
Definition validate.h:128
@ voReserved
Reserved for future use.
Definition validate.h:47
@ voTransfer
Option to perform conversion & transfer.
Definition validate.h:39
@ voOnAppend
Option to only validate input on appending.
Definition validate.h:43
@ voFill
Option to fill in chars on IsValidInput checks.
Definition validate.h:35
@ prEmpty
Definition validate.h:131
@ prAmbiguous
Definition validate.h:134
@ prError
Definition validate.h:132
@ prComplete
Definition validate.h:129
@ prIncomplete
Definition validate.h:130
@ prSyntax
Definition validate.h:133
@ prIncompNoFill
Definition validate.h:135
TTransferDirection
The TTransferDirection enum describes the constants that the transfer function uses to determine how ...
Definition window.h:92
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
#define protected_data
Definition defs.h:208
#define _OWLCLASS
Definition defs.h:338
Definition of container classes used and made available by OWL.
Base window class TWindow definition, including HWND encapsulation.