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. 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} \membersection{wxValidator::SetWindow}\label{wxvalidatorsetwindow}
\func{void}{SetWindow}{\param{wxWindow*}{ window}} \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, void* clientData );
void Append( const wxString &item, wxClientData* clientData ); void Append( const wxString &item, wxClientData* clientData );
void InsertItems(int nItems, const wxString items[], int pos);
void SetClientData( int n, void* clientData ); void SetClientData( int n, void* clientData );
void* GetClientData( int n ); void* GetClientData( int n );
void SetClientObject( int n, wxClientData* clientData ); 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, void* clientData );
void Append( const wxString &item, wxClientData* clientData ); void Append( const wxString &item, wxClientData* clientData );
void InsertItems(int nItems, const wxString items[], int pos);
void SetClientData( int n, void* clientData ); void SetClientData( int n, void* clientData );
void* GetClientData( int n ); void* GetClientData( int n );
void SetClientObject( int n, wxClientData* clientData ); void SetClientObject( int n, wxClientData* clientData );

View File

@@ -13,7 +13,7 @@
#define _WX_VALIDATEH__ #define _WX_VALIDATEH__
#ifdef __GNUG__ #ifdef __GNUG__
#pragma interface "validate.h" #pragma interface "validate.h"
#endif #endif
#include "wx/event.h" #include "wx/event.h"
@@ -30,38 +30,49 @@ class WXDLLEXPORT wxWindow;
to intercept e.g. OnChar. to intercept e.g. OnChar.
Note that wxValidator and derived classes use reference counting. Note that wxValidator and derived classes use reference counting.
*/ */
class WXDLLEXPORT wxValidator: public wxEvtHandler class WXDLLEXPORT wxValidator : public wxEvtHandler
{ {
DECLARE_DYNAMIC_CLASS(wxValidator)
public: public:
wxValidator(void); wxValidator();
~wxValidator(); ~wxValidator();
// Make a clone of this validator (or return NULL) - currently necessary // Make a clone of this validator (or return NULL) - currently necessary
// if you're passing a reference to a validator. // if you're passing a reference to a validator.
// Another possibility is to always pass a pointer to a new 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). // (so the calling code can use a copy constructor of the relevant class).
virtual wxValidator *Clone(void) const { return (wxValidator *) NULL; } virtual wxValidator *Clone() const
inline bool Copy(const wxValidator& val) { m_validatorWindow = val.m_validatorWindow; return TRUE; } { 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. // Called when the value in the window must be validated.
// This function can pop up an error message. // This function can pop up an error message.
virtual bool Validate(wxWindow *WXUNUSED(parent)) { return FALSE; }; virtual bool Validate(wxWindow *WXUNUSED(parent)) { return FALSE; };
// Called to transfer data to the window // 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 // Called to transfer data from the window
virtual bool TransferFromWindow(void) { return FALSE; }; virtual bool TransferFromWindow() { return FALSE; };
// ACCESSORS // accessors
inline wxWindow *GetWindow(void) const { return m_validatorWindow; } wxWindow *GetWindow() const { return m_validatorWindow; }
inline void SetWindow(wxWindow *win) { m_validatorWindow = win; } 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: protected:
wxWindow *m_validatorWindow; wxWindow *m_validatorWindow;
private:
static bool ms_isSilent;
DECLARE_DYNAMIC_CLASS(wxValidator)
}; };
WXDLLEXPORT_DATA(extern const wxValidator) wxDefaultValidator; WXDLLEXPORT_DATA(extern const wxValidator) wxDefaultValidator;

View File

@@ -32,35 +32,17 @@
BEGIN_EVENT_TABLE(MyFrame, wxFrame) BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit) EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
EVT_MENU(VALIDATE_TEST_DIALOG, MyFrame::OnTestDialog) EVT_MENU(VALIDATE_TEST_DIALOG, MyFrame::OnTestDialog)
EVT_MENU(VALIDATE_SILENT, MyFrame::OnSilent)
END_EVENT_TABLE() END_EVENT_TABLE()
IMPLEMENT_APP(MyApp) IMPLEMENT_APP(MyApp)
MyData g_data; MyData g_data;
bool MyApp::OnInit(void) bool MyApp::OnInit()
{ {
// Create the main frame window // Create the main frame window
MyFrame *frame = new MyFrame((wxFrame *) NULL, (char *) "Validation Test", 50, 50, 300, 250); MyFrame *frame = new MyFrame((wxFrame *) NULL, "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);
// Show the frame // Show the frame
frame->Show(TRUE); frame->Show(TRUE);
@@ -71,9 +53,33 @@ bool MyApp::OnInit(void)
} }
// My frame constructor // My frame constructor
MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int 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)) : 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)) void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{ {
@@ -87,6 +93,16 @@ void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
dialog.ShowModal(); 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, MyDialog::MyDialog( wxWindow *parent, const wxString& title,
const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) : const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL) wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)

View File

@@ -14,25 +14,26 @@
#endif #endif
// Define a new application type // Define a new application type
class MyApp: public wxApp class MyApp : public wxApp
{ public: {
bool OnInit(void); public:
bool OnInit();
}; };
// Define a new frame type // Define a new frame type
class MyFrame: public wxFrame 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);
public:
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnTestDialog(wxCommandEvent& event); void OnTestDialog(wxCommandEvent& event);
void OnSilent(wxCommandEvent& event);
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
class MyDialog: public wxDialog class MyDialog : public wxDialog
{ {
public: public:
MyDialog(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size, MyDialog(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
@@ -41,7 +42,7 @@ public:
class MyData class MyData
{ {
public: public:
wxString m_string; wxString m_string;
MyData() { m_string = "My string"; } MyData() { m_string = "My string"; }
@@ -50,5 +51,6 @@ class MyData
#define VALIDATE_DIALOG_ID 200 #define VALIDATE_DIALOG_ID 200
#define VALIDATE_TEST_DIALOG 2 #define VALIDATE_TEST_DIALOG 2
#define VALIDATE_SILENT 3
#define VALIDATE_TEXT 101 #define VALIDATE_TEXT 101

View File

@@ -10,18 +10,18 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__ #ifdef __GNUG__
#pragma implementation "validate.h" #pragma implementation "validate.h"
#endif #endif
// For compilers that support precompilation, includes "wx.h". // For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h" #include "wx/wxprec.h"
#ifdef __BORLANDC__ #ifdef __BORLANDC__
#pragma hdrstop #pragma hdrstop
#endif #endif
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/wx.h" #include "wx/wx.h"
#endif #endif
#include "wx/validate.h" #include "wx/validate.h"
@@ -29,10 +29,14 @@
const wxValidator wxDefaultValidator; const wxValidator wxDefaultValidator;
#if !USE_SHARED_LIBRARY #if !USE_SHARED_LIBRARY
IMPLEMENT_DYNAMIC_CLASS(wxValidator, wxEvtHandler) IMPLEMENT_DYNAMIC_CLASS(wxValidator, wxEvtHandler)
#endif #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; m_validatorWindow = (wxWindow *) NULL;
} }

View File

@@ -10,22 +10,22 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__ #ifdef __GNUG__
#pragma implementation "valtext.h" #pragma implementation "valtext.h"
#endif #endif
// For compilers that support precompilation, includes "wx.h". // For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h" #include "wx/wxprec.h"
#ifdef __BORLANDC__ #ifdef __BORLANDC__
#pragma hdrstop #pragma hdrstop
#endif #endif
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include <stdio.h> #include <stdio.h>
#include "wx/textctrl.h" #include "wx/textctrl.h"
#include "wx/utils.h" #include "wx/utils.h"
#include "wx/msgdlg.h" #include "wx/msgdlg.h"
#include "wx/intl.h" #include "wx/intl.h"
#endif #endif
#include "wx/valtext.h" #include "wx/valtext.h"
@@ -35,7 +35,7 @@
#include <stdlib.h> #include <stdlib.h>
#ifdef __SALFORDC__ #ifdef __SALFORDC__
#include <clib.h> #include <clib.h>
#endif #endif
#if !USE_SHARED_LIBRARY #if !USE_SHARED_LIBRARY
@@ -134,63 +134,62 @@ bool wxTextValidator::Validate(wxWindow *parent)
wxString val(control->GetValue()); 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_validatorStyle & wxFILTER_INCLUDE_LIST )
{ {
if ( !m_includeList.Member(val) ) if ( !m_includeList.Member(val) )
{ {
m_validatorWindow->SetFocus(); ok = false;
char buf[512];
sprintf(buf, _("%s is invalid."), (const char *)val);
wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
return FALSE;
} }
} }
if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST ) else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
{ {
if ( m_excludeList.Member(val) ) if ( m_excludeList.Member(val) )
{ {
m_validatorWindow->SetFocus(); ok = false;
char buf[512];
sprintf(buf, _("%s is invalid."), (const char *)val);
wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
return FALSE;
} }
} }
if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
{ {
m_validatorWindow->SetFocus(); ok = false;
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))
errormsg = _("'%s' should only contain ASCII characters.");
}
else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
{ {
m_validatorWindow->SetFocus(); ok = false;
char buf[512];
sprintf(buf, _("%s should be numeric."), (const char *)val); errormsg = _("'%s' should only contain alphabetic characters.");
wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); }
return FALSE; 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 // Called to transfer data to the window
@@ -268,43 +267,31 @@ void wxTextValidator::OnChar(wxKeyEvent& event)
return; return;
*/ */
if ( !m_validatorWindow ) if ( m_validatorWindow )
return; {
wxTextCtrl *textCtrl = (wxTextCtrl *)m_validatorWindow;
int keyCode = event.KeyCode(); int keyCode = event.KeyCode();
if (keyCode == WXK_DELETE || keyCode == WXK_RETURN || keyCode == WXK_BACK ||
keyCode == WXK_HOME || keyCode == WXK_LEFT || keyCode == WXK_UP || // we don't filter special keys and Delete
keyCode == WXK_RIGHT || keyCode == WXK_DOWN || keyCode == WXK_PRIOR || if (
keyCode == WXK_NEXT || keyCode == WXK_END || keyCode == WXK_HOME) !(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() )
return ; wxBell();
// eat message
return;
}
} }
if ( (m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode) ) event.Skip();
{
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);
} }
static bool wxIsNumeric(const wxString& val) static bool wxIsNumeric(const wxString& val)