1. validator fixes: don't eat TAB. Added new SetBellOnError() function to

suppress _annoying_ bell.
2. Docs and samples updated to reflect this.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1791 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-02-25 14:47:18 +00:00
parent bb0ca8dfcc
commit a994f81b94
8 changed files with 302 additions and 271 deletions

View File

@@ -64,6 +64,13 @@ This base function returns NULL.
Returns the window associated with the validator.
\membersection{wxValidator::SetBellOnError}{wxvalidatorsetbellonerror}
\func{void}{SetBellOnError}{\param{bool}{ doIt = TRUE}}
This functions switches on or turns off the error sound produced by the
validators if an invalid key is pressed.
\membersection{wxValidator::SetWindow}\label{wxvalidatorsetwindow}
\func{void}{SetWindow}{\param{wxWindow*}{ window}}

View File

@@ -68,6 +68,8 @@ public:
void Append( const wxString &item, void* clientData );
void Append( const wxString &item, wxClientData* clientData );
void InsertItems(int nItems, const wxString items[], int pos);
void SetClientData( int n, void* clientData );
void* GetClientData( int n );
void SetClientObject( int n, wxClientData* clientData );

View File

@@ -68,6 +68,8 @@ public:
void Append( const wxString &item, void* clientData );
void Append( const wxString &item, wxClientData* clientData );
void InsertItems(int nItems, const wxString items[], int pos);
void SetClientData( int n, void* clientData );
void* GetClientData( int n );
void SetClientObject( int n, wxClientData* clientData );

View File

@@ -34,34 +34,45 @@ class WXDLLEXPORT wxWindow;
class WXDLLEXPORT wxValidator : public wxEvtHandler
{
DECLARE_DYNAMIC_CLASS(wxValidator)
public:
wxValidator(void);
wxValidator();
~wxValidator();
// Make a clone of this validator (or return NULL) - currently necessary
// if you're passing a reference to a validator.
// Another possibility is to always pass a pointer to a new validator
// (so the calling code can use a copy constructor of the relevant class).
virtual wxValidator *Clone(void) const { return (wxValidator *) NULL; }
inline bool Copy(const wxValidator& val) { m_validatorWindow = val.m_validatorWindow; return TRUE; }
virtual wxValidator *Clone() const
{ return (wxValidator *)NULL; }
bool Copy(const wxValidator& val)
{ m_validatorWindow = val.m_validatorWindow; return TRUE; }
// Called when the value in the window must be validated.
// This function can pop up an error message.
virtual bool Validate(wxWindow *WXUNUSED(parent)) { return FALSE; };
// Called to transfer data to the window
virtual bool TransferToWindow(void) { return FALSE; }
virtual bool TransferToWindow() { return FALSE; }
// Called to transfer data from the window
virtual bool TransferFromWindow(void) { return FALSE; };
virtual bool TransferFromWindow() { return FALSE; };
// ACCESSORS
inline wxWindow *GetWindow(void) const { return m_validatorWindow; }
inline void SetWindow(wxWindow *win) { m_validatorWindow = win; }
// accessors
wxWindow *GetWindow() const { return m_validatorWindow; }
void SetWindow(wxWindow *win) { m_validatorWindow = win; }
// validators beep by default if invalid key is pressed, these functions
// allow to change it
static bool IsSilent() { return ms_isSilent; }
static void SetBellOnError(bool doIt = TRUE) { ms_isSilent = doIt; }
protected:
wxWindow *m_validatorWindow;
private:
static bool ms_isSilent;
DECLARE_DYNAMIC_CLASS(wxValidator)
};
WXDLLEXPORT_DATA(extern const wxValidator) wxDefaultValidator;

View File

@@ -32,35 +32,17 @@
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
EVT_MENU(VALIDATE_TEST_DIALOG, MyFrame::OnTestDialog)
EVT_MENU(VALIDATE_SILENT, MyFrame::OnSilent)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
MyData g_data;
bool MyApp::OnInit(void)
bool MyApp::OnInit()
{
// Create the main frame window
MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "Validation Test", 50, 50, 300, 250);
// Give it an icon
#ifdef __WXMSW__
frame->SetIcon(wxIcon("mondrian"));
#endif
#ifdef __X__
frame->SetIcon(wxIcon("aiai.xbm"));
#endif
// Make a menubar
wxMenu *file_menu = new wxMenu;
file_menu->Append(VALIDATE_TEST_DIALOG, "&Test dialog");
file_menu->Append(wxID_EXIT, "E&xit");
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, "File");
frame->SetMenuBar(menu_bar);
frame->CreateStatusBar(1);
MyFrame *frame = new MyFrame((wxFrame *) NULL, "Validation Test", 50, 50, 300, 250);
// Show the frame
frame->Show(TRUE);
@@ -71,9 +53,33 @@ bool MyApp::OnInit(void)
}
// My frame constructor
MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
{}
MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h)
: wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
{
// Give it an icon
#ifdef __WXMSW__
SetIcon(wxIcon("mondrian"));
#endif
#ifdef __X__
SetIcon(wxIcon("aiai.xbm"));
#endif
// Make a menubar
wxMenu *file_menu = new wxMenu;
file_menu->Append(VALIDATE_TEST_DIALOG, "&Test dialog", "Show example dialog");
file_menu->Append(VALIDATE_SILENT, "&Bell on error", "Toggle bell on error", TRUE);
file_menu->AppendSeparator();
file_menu->Append(wxID_EXIT, "E&xit");
file_menu->Check(VALIDATE_SILENT, wxValidator::IsSilent());
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, "File");
SetMenuBar(menu_bar);
CreateStatusBar(1);
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
@@ -87,6 +93,16 @@ void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
dialog.ShowModal();
}
void MyFrame::OnSilent(wxCommandEvent& event)
{
static bool s_silent = FALSE;
s_silent = !s_silent;
wxValidator::SetBellOnError(s_silent);
event.Skip();
}
MyDialog::MyDialog( wxWindow *parent, const wxString& title,
const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)

View File

@@ -15,21 +15,22 @@
// Define a new application type
class MyApp : public wxApp
{ public:
bool OnInit(void);
{
public:
bool OnInit();
};
// Define a new frame type
class MyFrame : public wxFrame
{ public:
MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
{
public:
MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h);
void OnQuit(wxCommandEvent& event);
void OnTestDialog(wxCommandEvent& event);
void OnSilent(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
class MyDialog : public wxDialog
@@ -50,5 +51,6 @@ class MyData
#define VALIDATE_DIALOG_ID 200
#define VALIDATE_TEST_DIALOG 2
#define VALIDATE_SILENT 3
#define VALIDATE_TEXT 101

View File

@@ -32,7 +32,11 @@ const wxValidator wxDefaultValidator;
IMPLEMENT_DYNAMIC_CLASS(wxValidator, wxEvtHandler)
#endif
wxValidator::wxValidator(void)
// VZ: personally, I think TRUE would be more appropriate - these bells are
// _annoying_
bool wxValidator::ms_isSilent = FALSE;
wxValidator::wxValidator()
{
m_validatorWindow = (wxWindow *) NULL;
}

View File

@@ -134,63 +134,62 @@ bool wxTextValidator::Validate(wxWindow *parent)
wxString val(control->GetValue());
bool ok = true;
// this format string should contian exactly one '%s'
const char *errormsg = _("'%s' is invalid");
if ( m_validatorStyle & wxFILTER_INCLUDE_LIST )
{
if ( !m_includeList.Member(val) )
{
m_validatorWindow->SetFocus();
char buf[512];
sprintf(buf, _("%s is invalid."), (const char *)val);
wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
return FALSE;
ok = false;
}
}
if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
{
if ( m_excludeList.Member(val) )
{
m_validatorWindow->SetFocus();
char buf[512];
sprintf(buf, _("%s is invalid."), (const char *)val);
wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
return FALSE;
ok = false;
}
}
if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
{
m_validatorWindow->SetFocus();
char buf[512];
sprintf(buf, _("%s should only contain ASCII characters."), (const char *)val);
wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
return FALSE;
}
if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
{
m_validatorWindow->SetFocus();
char buf[512];
sprintf(buf, _("%s should only contain alphabetic characters."), (const char *)val);
wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
return FALSE;
}
if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
{
m_validatorWindow->SetFocus();
char buf[512];
sprintf(buf, _("%s should only contain alphabetic or numeric characters."), (const char *)val);
wxMessageBox(buf,_("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
return FALSE;
}
if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
ok = false;
errormsg = _("'%s' should only contain ASCII characters.");
}
else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
{
m_validatorWindow->SetFocus();
char buf[512];
sprintf(buf, _("%s should be numeric."), (const char *)val);
wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
return FALSE;
ok = false;
errormsg = _("'%s' should only contain alphabetic characters.");
}
else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
{
ok = false;
errormsg = _("'%s' should only contain alphabetic or numeric characters.");
}
else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
{
ok = false;
errormsg = _("'%s' should be numeric.");
}
return TRUE ;
if ( !ok )
{
m_validatorWindow->SetFocus();
wxString buf;
buf.Printf(errormsg, val.c_str());
wxMessageBox(buf, _("Validation conflict"),
wxOK | wxICON_EXCLAMATION, parent);
}
return ok;
}
// Called to transfer data to the window
@@ -268,43 +267,31 @@ void wxTextValidator::OnChar(wxKeyEvent& event)
return;
*/
if ( !m_validatorWindow )
return;
wxTextCtrl *textCtrl = (wxTextCtrl *)m_validatorWindow;
if ( m_validatorWindow )
{
int keyCode = event.KeyCode();
if (keyCode == WXK_DELETE || keyCode == WXK_RETURN || keyCode == WXK_BACK ||
keyCode == WXK_HOME || keyCode == WXK_LEFT || keyCode == WXK_UP ||
keyCode == WXK_RIGHT || keyCode == WXK_DOWN || keyCode == WXK_PRIOR ||
keyCode == WXK_NEXT || keyCode == WXK_END || keyCode == WXK_HOME)
// we don't filter special keys and Delete
if (
!(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
(
((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
((m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode)) ||
((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode)) ||
((m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode)
&& keyCode != '.' && keyCode != '-')
)
)
{
textCtrl->wxTextCtrl::OnChar(event);
if ( !wxValidator::IsSilent() )
wxBell();
// eat message
return;
}
}
if ( (m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode) )
{
wxBell();
return;
}
if ( (m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode) )
{
wxBell();
return;
}
if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode) )
{
wxBell();
return;
}
if ( (m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode) && keyCode != '.' && keyCode != '-')
{
wxBell();
return;
}
textCtrl->wxTextCtrl::OnChar(event);
event.Skip();
}
static bool wxIsNumeric(const wxString& val)