Filter Library | Camera | Interface Physics |
#include <ControlFilterEdit.h>
Inheritance diagram for CFilterEdit:
Base class CFilterEdit lets you specify several ascpects of its behaviour:
To implement the checking behaviour, do the following.
Note: CFilterEdit can also be used by itself solely with the purpose to create a colored edit field.
Make sure that the control is included in method DoDataExchange() of the dialog classes where you use the edit control.
Class CFilterEdit is a modification of the class CFilterEdit by Ben Hanson [FILTEREDIT] in that the regular expression pattern matching [REGEXP] is replaced by a simple lexical scanner [Aho et al.].
Another interesting article is: A Validating Edit Control by Joseph M. Newcomer [VALEDTCTL]. This article describes the use of a tooltip to indicate the reason the contents of an edit control is invalid.
The following example shows an edit field that limits its input to signed integer numbers in octal, decimal or hexadecimal notation.
The interface.
// ControlOctDecHexEdit.h - class declaration. #ifndef CFL_CONTROLOCTDECHEXEDIT_H #define CFL_CONTROLOCTDECHEXEDIT_H #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <cfl/ControlFilterEdit.h> // for base class CFilterEdit // // COctDecHexEdit // class COctDecHexEdit : public CFilterEdit { public: ~COctDecHexEdit(); COctDecHexEdit( bool bSigned = true ); protected: bool Check( const CString &csText ); bool Match( const CString &csText ); private: bool m_bSigned; }; // // destructor. // inline COctDecHexEdit::~COctDecHexEdit() { ; // do nothing } // // destructor. // inline COctDecHexEdit::COctDecHexEdit( bool bSigned ) : CFilterEdit(), m_bSigned( bSigned ) { ; // do nothing } #endif // CFL_CONTROLOCTDECHEXEDIT_H
The implementation.
// ControlOctDecHexEdit.cpp - class implementation. #include "stdafx.h" // for common (pre-compiled) headers #include <cfl/ControlOctDecHexEdit.h> // header // // true if input is complete and valid. // bool COctDecHexEdit::Check( const CString &csText ) { TCHAR* ePtr; // let strtol() determine the base: (void) _tcstol( csText, &ePtr, 0 ); // ok if all characters are used for conversion return _TCHAR('\0') == *ePtr; } // // true if (partial) input valid. // // number : sign? ( octal | decimal | hexadecimal ) // sign : [+-] // octal : "0" [0-7]+ // decimal : [1-9][0-9]* // hexadecimal : "0" [xX] [0-9a-fA-F]+ // bool COctDecHexEdit::Match( const CString &csText ) { int base = 10; const TCHAR* cPtr = csText; // optional sign? if ( _TCHAR('+') == *cPtr || ( m_bSigned && _TCHAR('-') == *cPtr ) ) ++cPtr; // leading octal/hexadecimal 0? if ( _TCHAR('0') == *cPtr ) { ++cPtr; base = 8; } // leading hexadecimal 0x, 0X? if ( 8 == base && ( _TCHAR('x') == *cPtr || _TCHAR('X') == *cPtr ) ) { ++cPtr; base = 16; } // scan remaining digits: if ( 8 == base ) { while ( *cPtr && _TCHAR('0') <= *cPtr && *cPtr <= _TCHAR('7') ) ++cPtr; } else if ( 10 == base ) { while ( *cPtr && _istdigit( *cPtr ) ) ++cPtr; } else if ( 16 == base ) { while ( *cPtr && _istxdigit( *cPtr ) ) ++cPtr; } // ok if all characters matched: return cPtr == (const TCHAR*) csText + csText.GetLength(); }
Definition at line 335 of file ControlFilterEdit.h.
Public Member Functions | |
Predicates | |
bool | IsValidChar () |
true if last entered character is valid. | |
bool | IsValidInput () |
true if the entered text is complete and valid. | |
Accessors | |
const CString & | GetProposedText () const |
the entered text, so far. | |
Mutators | |
void | SetProcessChars (const bool bProcessChars) |
validate as the user types (also settable in the constructor). | |
void | SetAutoValidate (const bool bAutoValidate, const bool bModal) |
check that the entire input is valid when the control loses focus (bAutoValidate), set focus back to the control if the input is invalid when the control loses focus (bModal). | |
void | SetShowInvalid (const bool bShowInvalid, const bool bSetBkOnError, const bool bWavyLineOnError) |
show if the input is invalid (bShowInvalid), via the background color (bSetBkOnError) and with a wavy line under the text (bWavyLineOnError). | |
void | SetBeepOnInvalid (const bool bBeepOnInvalid) |
beep when the final validation fails (bBeepOnInvalid). | |
void | SetBackgroundColourOK (const COLORREF crBkOK) |
set the normal ('OK') background color. | |
void | SetForegroundColourOK (const COLORREF crFgOK) |
set the normal ('OK') foreground color. | |
void | SetBackgroundColourError (const COLORREF crBkError) |
set the error background color. | |
void | SetForegroundColourError (const COLORREF crFgError) |
set the error foreground color. | |
void | Select () |
focus the control and select it content. | |
Protected Member Functions | |
bool | ValidateChar (UINT nChar) |
true if this character is acceptable at this point. | |
void | ResetValid () |
restore valid state. | |
virtual bool | Check (const CString &csText) |
true if input is complete and valid; override in derived classes. | |
virtual bool | Match (const CString &csText) |
true if partial input is acceptable; override in derived classes. | |
virtual LRESULT | WindowProc (UINT message, WPARAM wParam, LPARAM lParam) |
handle window message, like cut paste etc. | |
Mutators | |
afx_msg void | OnChar (UINT nChar, UINT nRepCnt, UINT nFlags) |
validate new character (if SetProcessChars(true)) and let it be further processed, unless its invalid. | |
afx_msg void | OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags) |
handle control-key press (Del, Ctrl+X, Ctrl+V). | |
afx_msg void | OnKeyUp (UINT nChar, UINT nRepCnt, UINT nFlags) |
handle control-key release (Ctrl). | |
afx_msg HBRUSH | CtlColor (CDC *pDC, UINT) |
handle the ON_WM_CTLCOLOR_REFLECT message. | |
afx_msg void | OnSetFocus (CWnd *pOldWnd) |
handle set focus message. | |
afx_msg void | OnKillFocus (CWnd *pNewWnd) |
validate on kill focus message. | |
afx_msg void | OnPaint () |
handle paint message. | |
Private Types | |
typedef CFilterEdit | self |
this type | |
Private Member Functions | |
bool | CanDelete () |
handle cut, Ctrl+X | |
bool | CanPaste () |
handle paste, Ctrl+V | |
CFilterEdit (CFilterEdit const &) | |
prevent copy-assignment construction | |
self & | operator= (CFilterEdit const &) |
prevent assignment | |
Private Attributes | |
bool | m_bProcessChars |
validate characters while typing (flag) | |
bool | m_bAutoValidate |
(flag) | |
bool | m_bModal |
(flag) | |
bool | m_bShowInvalid |
indicate invalid input (flag) | |
bool | m_bSetBkOnError |
change background color for invalid input (flag) | |
bool | m_bWavyLineOnError |
draw wavy line if input is invalid (flag) | |
bool | m_bBeepOnInvalid |
beep if input is invalid (flag) | |
bool | m_bValid |
input is valid (flag) | |
bool | m_bCharValid |
current character is valid (flag) | |
CString | m_strProposedText |
current valid text | |
bool | m_bControlDown |
control key is down (flag) | |
COLORREF | m_crFgOK |
ok foreground color | |
COLORREF | m_crBkOK |
ok background color | |
COLORREF | m_crFgError |
error foreground color | |
COLORREF | m_crBkError |
error background color | |
CBrush | m_brOK |
ok brush | |
CBrush | m_brError |
error brush |