diff --git a/include/wx/univ/textctrl.h b/include/wx/univ/textctrl.h index 87aafa2b5e..e7132f9a8b 100644 --- a/include/wx/univ/textctrl.h +++ b/include/wx/univ/textctrl.h @@ -110,6 +110,7 @@ public: virtual bool IsModified() 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. virtual void GetSelection(wxTextPos* from, wxTextPos* to) const wxOVERRIDE; @@ -489,6 +490,11 @@ private: // last position (only used by GetLastPosition()) wxTextPos m_posLast; + // max text line length + long m_maxLength; + // current text line length + long m_textLength; + // selection wxTextPos m_selAnchor, m_selStart, diff --git a/src/univ/.textctrl.cpp.swp b/src/univ/.textctrl.cpp.swp new file mode 100644 index 0000000000..9a70922bb3 Binary files /dev/null and b/src/univ/.textctrl.cpp.swp differ diff --git a/src/univ/textctrl.cpp b/src/univ/textctrl.cpp index f6184a0e03..473efcdcd3 100644 --- a/src/univ/textctrl.cpp +++ b/src/univ/textctrl.cpp @@ -747,6 +747,9 @@ bool wxTextCtrl::Create(wxWindow *parent, SetInitialSize(size); 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(); InitInsertionPoint(); @@ -2536,6 +2539,11 @@ void wxTextCtrl::OnSize(wxSizeEvent& event) event.Skip(); } +void wxTextCtrl::SetMaxLength(unsigned long len) +{ + m_maxLength = len; +} + wxCoord wxTextCtrl::GetTotalWidth() const { wxCoord w; @@ -4533,8 +4541,30 @@ bool wxTextCtrl::PerformAction(const wxControlAction& actionOrig, { if ( IsEditable() && !strArg.empty() ) { - // inserting text can be undone - command = new wxTextCtrlInsertCommand(strArg); + if ( IsSingleLine() ) + { + // 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) || @@ -4714,7 +4744,7 @@ bool wxTextCtrl::PerformAction(const wxControlAction& actionOrig, // execute and remember it to be able to undo it later m_cmdProcessor->Submit(command); } - else if ( IsEditable() ) + else if ( IsEditable() && ( m_maxLength > 0 && m_textLength <= m_maxLength) ) { wxCommandEvent event(wxEVT_TEXT, GetId()); InitCommandEvent(event); diff --git a/tests/controls/textctrltest.cpp b/tests/controls/textctrltest.cpp index 57421088c4..0e503915e2 100644 --- a/tests/controls/textctrltest.cpp +++ b/tests/controls/textctrltest.cpp @@ -223,6 +223,7 @@ void TextCtrlTestCase::MaxLength() EventCounter maxlen(m_text, wxEVT_TEXT_MAXLEN); m_text->SetFocus(); + wxYield(); m_text->SetMaxLength(10); wxUIActionSimulator sim;