adjust the implementation about max length limitation according review comments

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77821 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2014-09-23 17:42:18 +00:00
parent d9b0c17412
commit fa2fa3dd9f
3 changed files with 34 additions and 37 deletions

View File

@@ -491,9 +491,7 @@ private:
wxTextPos m_posLast; wxTextPos m_posLast;
// max text line length // max text line length
long m_maxLength; unsigned long m_maxLength;
// current text line length
long m_textLength;
// selection // selection
wxTextPos m_selAnchor, wxTextPos m_selAnchor,

Binary file not shown.

View File

@@ -651,6 +651,9 @@ void wxTextCtrl::Init()
m_curCol = m_curCol =
m_curRow = 0; m_curRow = 0;
// if m_maxLength is zero means there is no restriction of max length
m_maxLength = 0;
m_heightLine = m_heightLine =
m_widthAvg = -1; m_widthAvg = -1;
@@ -747,9 +750,6 @@ bool wxTextCtrl::Create(wxWindow *parent,
SetInitialSize(size); SetInitialSize(size);
m_isEditable = !(style & wxTE_READONLY); m_isEditable = !(style & wxTE_READONLY);
m_textLength = value.Length();
// if m_maxLength is zero means there is no restriction of max length
m_maxLength = 0;
CreateCaret(); CreateCaret();
InitInsertionPoint(); InitInsertionPoint();
@@ -1280,6 +1280,10 @@ void wxTextCtrl::Replace(wxTextPos from, wxTextPos to, const wxString& text)
void wxTextCtrl::Remove(wxTextPos from, wxTextPos to) void wxTextCtrl::Remove(wxTextPos from, wxTextPos to)
{ {
// treat -1 as the last position
if ( to == -1 )
to = GetLastPosition();
// Replace() only works with correctly ordered arguments, so exchange them // Replace() only works with correctly ordered arguments, so exchange them
// if necessary // if necessary
OrderPositions(from, to); OrderPositions(from, to);
@@ -2541,6 +2545,18 @@ void wxTextCtrl::OnSize(wxSizeEvent& event)
void wxTextCtrl::SetMaxLength(unsigned long len) void wxTextCtrl::SetMaxLength(unsigned long len)
{ {
// if the existing value in the text control is too long,
// then it is clipped to the newly imposed limit.
if ( m_value.Length() > len )
{
// block the wxEVT_TEXT event temporaryly
// otherwise Remove will generate a wxEVT_TEXT event
// that not what we want
ForwardEnableTextChangedEvents(false);
Remove(len, -1);
ForwardEnableTextChangedEvents(true);
}
m_maxLength = len; m_maxLength = len;
} }
@@ -4541,31 +4557,23 @@ bool wxTextCtrl::PerformAction(const wxControlAction& actionOrig,
{ {
if ( IsEditable() && !strArg.empty() ) if ( IsEditable() && !strArg.empty() )
{ {
if ( IsSingleLine() ) wxString acceptString = strArg;
{
// it is ugly, but we don't want calculate text length every time
// in multiline text ctrl.
m_textLength += strArg.Length();
if ( m_maxLength > 0 && m_textLength > m_maxLength ) unsigned long acceptableLength = m_maxLength - m_value.Length();
if ( m_maxLength > 0 && (strArg.Length() > acceptableLength) )
{ {
wxCommandEvent event(wxEVT_TEXT_MAXLEN, m_windowId); wxCommandEvent event(wxEVT_TEXT_MAXLEN, m_windowId);
InitCommandEvent(event); InitCommandEvent(event);
GetEventHandler()->ProcessEvent(event); GetEventHandler()->ProcessEvent(event);
// if m_textLength bigger than m_maxLength. it means not allow acceptString = strArg.Left(acceptableLength);
// to insert new chars.
}
else
{
// inserting text can be undone, same as below
command = new wxTextCtrlInsertCommand(strArg);
}
}
else
{
command = new wxTextCtrlInsertCommand(strArg);
} }
if ( acceptString.Length() > 0 )
// inserting text can be undone
command = new wxTextCtrlInsertCommand(acceptString);
} }
} }
else if ( (action == wxACTION_TEXT_PAGE_UP) || else if ( (action == wxACTION_TEXT_PAGE_UP) ||
(action == wxACTION_TEXT_PAGE_DOWN) ) (action == wxACTION_TEXT_PAGE_DOWN) )
@@ -4737,20 +4745,11 @@ bool wxTextCtrl::PerformAction(const wxControlAction& actionOrig,
} }
} }
// Submit call will insert text eventually, so if has command, // Submit call will insert text and generate wxEVT_TEXT event.
// then no need to process wxEVT_TEXT event again.
if ( command ) if ( command )
{ {
// execute and remember it to be able to undo it later // execute and remember it to be able to undo it later
m_cmdProcessor->Submit(command); m_cmdProcessor->Submit(command);
}
else if ( IsEditable() && ( m_maxLength > 0 && m_textLength <= m_maxLength) )
{
wxCommandEvent event(wxEVT_TEXT, GetId());
InitCommandEvent(event);
GetEventHandler()->ProcessEvent(event);
// as the text changed...
m_isModified = true; m_isModified = true;
} }