The IliInputMask Class
An input mask is similar to a format except that it allows you to control how values are entered by the end user in addition to the formatting.
The following is an example of an input mask:
00"-"00"-"00
This input mask lets the end user type six digits and nothing else. It also separates each pair of digits from the next by displaying a '-' character between them. This character is only used for display and is not part of the value.
See Mask Syntax for more information on how masks can be specified.
There can be cases where the mask specification language described in Appendix C is not sufficient for a given task. Fortunately, it is possible to define input masks in C++ by subclassing class IliInputMaskIpl.
Data Access provides the IliDateType (manages date and time) and the IliTimeType (manages time only) data types. In some circumstances, it may be necessary to use the IliDateType data type when only the time part needs to be managed. This happens, for instance, when using a database system that exclusively supports a date-time type. Since such a database system does not support a time-only type, it is necessary to use IliDateType instead of IliTimeType, even thought it is expected that the date part of values will be constrained to be some constant date.
The following code samples create an input mask that allows editing of the time part of a date-time value. The date part is constrained to be some constant date.
First, a subclass of IliInputMaskIpl must be defined:
#include <wctype.h>
#include <ilviews/dataccess/inpmask.h>
const char* MyMaskName = "MyMask";
// Use "30 Dec 1899" with an Access database.
const char* DatePart = "1-1-1901 ";
class MyMask : public IliInputMaskIpl
{
public:
MyMask()
: IliInputMaskIpl(MyMaskName),
_format("Time")
{
setMaxCharMask(8);
}
virtual IlvBoolean unFormat(IliString& dest,
const char* src) const {
if (src && *src)
dest << DatePart << src;
return IlvTrue;
}
IlvBoolean isValidChar(IlInt pos,
wchar_t c,
IlvBoolean editMode = IlTrue) const {
if (isFixChar(pos))
return (c == L':');
return iswdigit(c);
}
virtual IlBoolean isFixChar(IlInt pos) const {
return (pos == 2 || pos == 5);
}
virtual wchar_t filterChar(IlInt pos, wchar_t c) {
return (isFixChar(pos) ? L':' : c);
}
virtual const IliFormat& getValueFormat() const {
return _format;
}
IliFormat _format;
};
Then, at initialization time, the following code should be executed:
// Register the mask.
IliInputMaskIpl::AddCustomMask(new MyMask);
IliInputMask::AddAlias("MyMask", "MyMask");
Here is how the mask could be used:
//Use the mask.
IliDbField* fld;
...
IliInputMask m("MyMask");
fld->f_setMask(m);