Add wxIntegerValidator and wxFloatingPointValidator classes.

Add validators for integer and floating point numbers.

Add an example of their use to the validate sample as well as a new unit test
and documentation for them.

Use the new classes instead of wxTextValidator in wxGrid code.

Closes #12166.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66714 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2011-01-19 10:48:28 +00:00
parent 0d30c79b44
commit a54cf37118
34 changed files with 1665 additions and 16 deletions

View File

@@ -213,6 +213,7 @@ TEST_GUI_OBJECTS = \
test_gui_settings.o \
test_gui_socket.o \
test_gui_boxsizer.o \
test_gui_valnum.o \
test_gui_clientsize.o \
test_gui_setsize.o \
test_gui_xrctest.o
@@ -866,6 +867,9 @@ test_gui_socket.o: $(srcdir)/net/socket.cpp $(TEST_GUI_ODEP)
test_gui_boxsizer.o: $(srcdir)/sizers/boxsizer.cpp $(TEST_GUI_ODEP)
$(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/boxsizer.cpp
test_gui_valnum.o: $(srcdir)/validators/valnum.cpp $(TEST_GUI_ODEP)
$(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/validators/valnum.cpp
test_gui_clientsize.o: $(srcdir)/window/clientsize.cpp $(TEST_GUI_ODEP)
$(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/window/clientsize.cpp

View File

@@ -198,6 +198,7 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_settings.obj \
$(OBJS)\test_gui_socket.obj \
$(OBJS)\test_gui_boxsizer.obj \
$(OBJS)\test_gui_valnum.obj \
$(OBJS)\test_gui_clientsize.obj \
$(OBJS)\test_gui_setsize.obj \
$(OBJS)\test_gui_xrctest.obj
@@ -912,6 +913,9 @@ $(OBJS)\test_gui_socket.obj: .\net\socket.cpp
$(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp
$(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp
$(OBJS)\test_gui_valnum.obj: .\validators\valnum.cpp
$(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\validators\valnum.cpp
$(OBJS)\test_gui_clientsize.obj: .\window\clientsize.cpp
$(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\window\clientsize.cpp

View File

@@ -191,6 +191,7 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_settings.o \
$(OBJS)\test_gui_socket.o \
$(OBJS)\test_gui_boxsizer.o \
$(OBJS)\test_gui_valnum.o \
$(OBJS)\test_gui_clientsize.o \
$(OBJS)\test_gui_setsize.o \
$(OBJS)\test_gui_xrctest.o
@@ -893,6 +894,9 @@ $(OBJS)\test_gui_socket.o: ./net/socket.cpp
$(OBJS)\test_gui_boxsizer.o: ./sizers/boxsizer.cpp
$(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\test_gui_valnum.o: ./validators/valnum.cpp
$(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\test_gui_clientsize.o: ./window/clientsize.cpp
$(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<

View File

@@ -193,6 +193,7 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_settings.obj \
$(OBJS)\test_gui_socket.obj \
$(OBJS)\test_gui_boxsizer.obj \
$(OBJS)\test_gui_valnum.obj \
$(OBJS)\test_gui_clientsize.obj \
$(OBJS)\test_gui_setsize.obj \
$(OBJS)\test_gui_xrctest.obj
@@ -1038,6 +1039,9 @@ $(OBJS)\test_gui_socket.obj: .\net\socket.cpp
$(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp
$(OBJS)\test_gui_valnum.obj: .\validators\valnum.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\validators\valnum.cpp
$(OBJS)\test_gui_clientsize.obj: .\window\clientsize.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\window\clientsize.cpp

View File

@@ -433,6 +433,7 @@ TEST_GUI_OBJECTS = &
$(OBJS)\test_gui_settings.obj &
$(OBJS)\test_gui_socket.obj &
$(OBJS)\test_gui_boxsizer.obj &
$(OBJS)\test_gui_valnum.obj &
$(OBJS)\test_gui_clientsize.obj &
$(OBJS)\test_gui_setsize.obj &
$(OBJS)\test_gui_xrctest.obj
@@ -951,6 +952,9 @@ $(OBJS)\test_gui_socket.obj : .AUTODEPEND .\net\socket.cpp
$(OBJS)\test_gui_boxsizer.obj : .AUTODEPEND .\sizers\boxsizer.cpp
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $<
$(OBJS)\test_gui_valnum.obj : .AUTODEPEND .\validators\valnum.cpp
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $<
$(OBJS)\test_gui_clientsize.obj : .AUTODEPEND .\window\clientsize.cpp
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $<

View File

@@ -31,18 +31,13 @@ public:
{
// We need to use a locale with known decimal point and which uses the
// thousands separator for the tests to make sense.
wxLanguage lang;
if ( wxLocale::IsAvailable(wxLANGUAGE_ENGLISH_US) )
lang = wxLANGUAGE_ENGLISH_US;
else if ( wxLocale::IsAvailable(wxLANGUAGE_ENGLISH_UK) )
lang = wxLANGUAGE_ENGLISH_UK;
else
m_locale = new wxLocale(wxLANGUAGE_ENGLISH_UK,
wxLOCALE_DONT_LOAD_DEFAULT);
if ( !m_locale->IsOk() )
{
delete m_locale;
m_locale = NULL;
return;
}
m_locale = new wxLocale(lang, wxLOCALE_DONT_LOAD_DEFAULT);
}
virtual ~NumFormatterTestCase()

View File

@@ -199,6 +199,7 @@
-->
net/socket.cpp
sizers/boxsizer.cpp
validators/valnum.cpp
window/clientsize.cpp
window/setsize.cpp
xml/xrctest.cpp

View File

@@ -517,6 +517,10 @@ SOURCE=.\controls\treectrltest.cpp
# End Source File
# Begin Source File
SOURCE=.\validators\valnum.cpp
# End Source File
# Begin Source File
SOURCE=.\controls\virtlistctrltest.cpp
# End Source File
# Begin Source File

View File

@@ -821,6 +821,9 @@
RelativePath=".\controls\treectrltest.cpp">
</File>
<File
RelativePath=".\validators\valnum.cpp">
</File>
<File
RelativePath=".\controls\virtlistctrltest.cpp">
</File>
<File

View File

@@ -1168,6 +1168,10 @@
>
</File>
<File
RelativePath=".\validators\valnum.cpp"
>
</File>
<File
RelativePath=".\controls\virtlistctrltest.cpp"
>
</File>

View File

@@ -1140,6 +1140,10 @@
>
</File>
<File
RelativePath=".\validators\valnum.cpp"
>
</File>
<File
RelativePath=".\controls\virtlistctrltest.cpp"
>
</File>

286
tests/validators/valnum.cpp Normal file
View File

@@ -0,0 +1,286 @@
///////////////////////////////////////////////////////////////////////////////
// Name: tests/validators/valnum.cpp
// Purpose: Unit tests for numeric validators.
// Author: Vadim Zeitlin
// Created: 2011-01-18
// RCS-ID: $Id$
// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/intl.h"
#include "wx/textctrl.h"
#include "wx/validate.h"
#endif // WX_PRECOMP
#include "wx/valnum.h"
#include "asserthelper.h"
#include "testableframe.h"
#include "wx/uiaction.h"
class NumValidatorTestCase : public CppUnit::TestCase
{
public:
NumValidatorTestCase() { }
void setUp();
void tearDown();
private:
CPPUNIT_TEST_SUITE( NumValidatorTestCase );
CPPUNIT_TEST( TransferInt );
CPPUNIT_TEST( TransferUnsigned );
CPPUNIT_TEST( TransferFloat );
CPPUNIT_TEST( ZeroAsBlank );
CPPUNIT_TEST( NoTrailingZeroes );
WXUISIM_TEST( Interactive );
CPPUNIT_TEST_SUITE_END();
void TransferInt();
void TransferUnsigned();
void TransferFloat();
void ZeroAsBlank();
void NoTrailingZeroes();
#if wxUSE_UIACTIONSIMULATOR
void Interactive();
#endif // wxUSE_UIACTIONSIMULATOR
wxTextCtrl *m_text;
wxDECLARE_NO_COPY_CLASS(NumValidatorTestCase);
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( NumValidatorTestCase );
// also include in it's own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumValidatorTestCase, "NumValidatorTestCase" );
void NumValidatorTestCase::setUp()
{
m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
}
void NumValidatorTestCase::tearDown()
{
wxTheApp->GetTopWindow()->DestroyChildren();
}
void NumValidatorTestCase::TransferInt()
{
int value = 0;
wxIntegerValidator<int> valInt(&value);
valInt.SetWindow(m_text);
CPPUNIT_ASSERT( valInt.TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "0", m_text->GetValue() );
value = 17;
CPPUNIT_ASSERT( valInt.TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "17", m_text->GetValue() );
m_text->ChangeValue("foobar");
CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
m_text->ChangeValue("-234");
CPPUNIT_ASSERT( valInt.TransferFromWindow() );
CPPUNIT_ASSERT_EQUAL( -234, value );
m_text->ChangeValue("9223372036854775808"); // == LLONG_MAX + 1
CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
m_text->Clear();
CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
}
void NumValidatorTestCase::TransferUnsigned()
{
unsigned value = 0;
wxIntegerValidator<unsigned> valUnsigned(&value);
valUnsigned.SetWindow(m_text);
CPPUNIT_ASSERT( valUnsigned.TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "0", m_text->GetValue() );
value = 17;
CPPUNIT_ASSERT( valUnsigned.TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "17", m_text->GetValue() );
m_text->ChangeValue("foobar");
CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
m_text->ChangeValue("-234");
CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
m_text->ChangeValue("234");
CPPUNIT_ASSERT( valUnsigned.TransferFromWindow() );
CPPUNIT_ASSERT_EQUAL( 234, value );
m_text->ChangeValue("18446744073709551616"); // == ULLONG_MAX + 1
CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
m_text->Clear();
CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
}
void NumValidatorTestCase::TransferFloat()
{
float value = 0;
wxFloatingPointValidator<float> valFloat(3, &value);
valFloat.SetWindow(m_text);
CPPUNIT_ASSERT( valFloat.TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "0.000", m_text->GetValue() );
value = 1.234f;
CPPUNIT_ASSERT( valFloat.TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "1.234", m_text->GetValue() );
value = 1.2345678f;
CPPUNIT_ASSERT( valFloat.TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "1.235", m_text->GetValue() );
m_text->ChangeValue("foobar");
CPPUNIT_ASSERT( !valFloat.TransferFromWindow() );
m_text->ChangeValue("-234.567");
CPPUNIT_ASSERT( valFloat.TransferFromWindow() );
CPPUNIT_ASSERT_EQUAL( -234.567f, value );
m_text->Clear();
CPPUNIT_ASSERT( !valFloat.TransferFromWindow() );
}
void NumValidatorTestCase::ZeroAsBlank()
{
long value = 0;
m_text->SetValidator(
wxMakeIntegerValidator(&value, wxNUM_VAL_ZERO_AS_BLANK));
wxValidator * const val = m_text->GetValidator();
CPPUNIT_ASSERT( val->TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
value++;
CPPUNIT_ASSERT( val->TransferFromWindow() );
CPPUNIT_ASSERT_EQUAL( 0, value );
}
void NumValidatorTestCase::NoTrailingZeroes()
{
double value = 1.2;
m_text->SetValidator(
wxMakeFloatingPointValidator(3, &value, wxNUM_VAL_NO_TRAILING_ZEROES));
wxValidator * const val = m_text->GetValidator();
CPPUNIT_ASSERT( val->TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "1.2", m_text->GetValue() );
value = 1.234;
CPPUNIT_ASSERT( val->TransferToWindow() );
CPPUNIT_ASSERT_EQUAL( "1.234", m_text->GetValue() );
}
#if wxUSE_UIACTIONSIMULATOR
void NumValidatorTestCase::Interactive()
{
// Set a locale using comma as thousands separator character.
wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
m_text->SetValidator(
wxIntegerValidator<unsigned>(NULL, wxNUM_VAL_THOUSANDS_SEPARATOR));
// Create a sibling text control to be able to switch focus and thus
// trigger the control validation/normalization.
wxTextCtrl * const text2 = new wxTextCtrl(m_text->GetParent(), wxID_ANY);
text2->Move(10, 80); // Just to see it better while debugging...
wxFloatingPointValidator<float> valFloat(3);
valFloat.SetRange(-10., 10.);
text2->SetValidator(valFloat);
wxUIActionSimulator sim;
// Entering '-' in a control with positive range is not allowed.
m_text->SetFocus();
sim.Char('-');
wxYield();
CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
// Neither is entering '.' or any non-digit character.
sim.Text(".a+/");
wxYield();
CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
// Entering digits should work though and after leaving the control the
// contents should be normalized.
sim.Text("1234567");
wxYield();
text2->SetFocus();
wxYield();
if ( loc.IsOk() )
CPPUNIT_ASSERT_EQUAL( "1,234,567", m_text->GetValue() );
else
CPPUNIT_ASSERT_EQUAL( "1234567", m_text->GetValue() );
// Entering both '-' and '.' in this control should work but only in the
// correct order.
sim.Char('-');
wxYield();
CPPUNIT_ASSERT_EQUAL( "-", text2->GetValue() );
text2->SetInsertionPoint(0);
sim.Char('.');
wxYield();
CPPUNIT_ASSERT_EQUAL( "-", text2->GetValue() );
text2->SetInsertionPointEnd();
sim.Char('.');
wxYield();
CPPUNIT_ASSERT_EQUAL( "-.", text2->GetValue() );
// Adding up to three digits after the point should work.
sim.Text("987");
wxYield();
CPPUNIT_ASSERT_EQUAL( "-.987", text2->GetValue() );
// But no more.
sim.Text("654");
wxYield();
CPPUNIT_ASSERT_EQUAL( "-.987", text2->GetValue() );
// We can remove one digit and another one though.
sim.Char(WXK_BACK);
sim.Char(WXK_BACK);
sim.Char('6');
wxYield();
CPPUNIT_ASSERT_EQUAL( "-.96", text2->GetValue() );
// Also test the range constraint.
text2->Clear();
sim.Char('9');
wxYield();
CPPUNIT_ASSERT_EQUAL( "9", text2->GetValue() );
sim.Char('9');
wxYield();
CPPUNIT_ASSERT_EQUAL( "9", text2->GetValue() );
}
#endif // wxUSE_UIACTIONSIMULATOR