add maxline restriction in single line mode textCtrl. let textctrl will fire wxEVT_TEXT_MAXLEN and not recive the chars when the text length beyond the maxlength

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@77819 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2014-09-23 17:42:12 +00:00
parent db0d7e67a9
commit fe733e34c2
4 changed files with 40 additions and 3 deletions

View File

@@ -110,6 +110,7 @@ public:
virtual bool IsModified() const wxOVERRIDE; virtual bool IsModified() const wxOVERRIDE;
virtual bool IsEditable() const wxOVERRIDE; virtual bool IsEditable() const wxOVERRIDE;
virtual void SetMaxLength(unsigned long len) wxOVERRIDE;
// If the return values from and to are the same, there is no selection. // If the return values from and to are the same, there is no selection.
virtual void GetSelection(wxTextPos* from, wxTextPos* to) const wxOVERRIDE; virtual void GetSelection(wxTextPos* from, wxTextPos* to) const wxOVERRIDE;
@@ -489,6 +490,11 @@ private:
// last position (only used by GetLastPosition()) // last position (only used by GetLastPosition())
wxTextPos m_posLast; wxTextPos m_posLast;
// max text line length
long m_maxLength;
// current text line length
long m_textLength;
// selection // selection
wxTextPos m_selAnchor, wxTextPos m_selAnchor,
m_selStart, m_selStart,

BIN
src/univ/.textctrl.cpp.swp Normal file

Binary file not shown.

View File

@@ -747,6 +747,9 @@ 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();
@@ -2536,6 +2539,11 @@ void wxTextCtrl::OnSize(wxSizeEvent& event)
event.Skip(); event.Skip();
} }
void wxTextCtrl::SetMaxLength(unsigned long len)
{
m_maxLength = len;
}
wxCoord wxTextCtrl::GetTotalWidth() const wxCoord wxTextCtrl::GetTotalWidth() const
{ {
wxCoord w; wxCoord w;
@@ -4533,8 +4541,30 @@ bool wxTextCtrl::PerformAction(const wxControlAction& actionOrig,
{ {
if ( IsEditable() && !strArg.empty() ) if ( IsEditable() && !strArg.empty() )
{ {
// inserting text can be undone if ( IsSingleLine() )
command = new wxTextCtrlInsertCommand(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 )
{
wxCommandEvent event(wxEVT_TEXT_MAXLEN, m_windowId);
InitCommandEvent(event);
GetEventHandler()->ProcessEvent(event);
// if m_textLength bigger than m_maxLength. it means not allow
// to insert new chars.
}
else
{
// inserting text can be undone, same as below
command = new wxTextCtrlInsertCommand(strArg);
}
}
else
{
command = new wxTextCtrlInsertCommand(strArg);
}
} }
} }
else if ( (action == wxACTION_TEXT_PAGE_UP) || else if ( (action == wxACTION_TEXT_PAGE_UP) ||
@@ -4714,7 +4744,7 @@ bool wxTextCtrl::PerformAction(const wxControlAction& actionOrig,
// 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() ) else if ( IsEditable() && ( m_maxLength > 0 && m_textLength <= m_maxLength) )
{ {
wxCommandEvent event(wxEVT_TEXT, GetId()); wxCommandEvent event(wxEVT_TEXT, GetId());
InitCommandEvent(event); InitCommandEvent(event);

View File

@@ -223,6 +223,7 @@ void TextCtrlTestCase::MaxLength()
EventCounter maxlen(m_text, wxEVT_TEXT_MAXLEN); EventCounter maxlen(m_text, wxEVT_TEXT_MAXLEN);
m_text->SetFocus(); m_text->SetFocus();
wxYield();
m_text->SetMaxLength(10); m_text->SetMaxLength(10);
wxUIActionSimulator sim; wxUIActionSimulator sim;