From ee1797f141057c63762e69e9bd2fee26cd8022e4 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Tue, 2 Apr 2002 19:40:57 +0000 Subject: [PATCH] Fix some crashes in new wxTextCtrl. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@14915 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/x11/textctrl.cpp | 94 ++++++++++++++++++++++++++++++++------------ 1 file changed, 68 insertions(+), 26 deletions(-) diff --git a/src/x11/textctrl.cpp b/src/x11/textctrl.cpp index 8a3e806199..b7756c9518 100644 --- a/src/x11/textctrl.cpp +++ b/src/x11/textctrl.cpp @@ -269,30 +269,36 @@ void wxTextCtrl::SetValue(const wxString& value) ClearSelection(); m_lines.Clear(); m_longestLine = 0; - - int pos = 0; - for (;;) + + if (value.IsEmpty()) { - // TODO make more efficient - wxString tmp = value; - tmp.Remove( 0, pos ); - pos = tmp.Find( '\n' ); - if (pos == -1) + m_lines.Add( new wxSourceLine( wxT("") ) ); + } + else + { + int begin = 0; + int pos = 0; + for (;;) { - if (tmp.Len() > m_longestLine) - m_longestLine = tmp.Len(); - - m_lines.Add( new wxSourceLine( tmp ) ); - break; + pos = value.find( wxT('\n'), begin ); + if (pos < 0) + { + if (value.Len()-begin > m_longestLine) + m_longestLine = value.Len()-begin; + + m_lines.Add( new wxSourceLine( value.Mid( begin, value.Len()-begin ) ) ); - } - else - { - if (pos > m_longestLine) - m_longestLine = pos; + break; + } + else + { + if (pos-begin > m_longestLine) + m_longestLine = pos-begin; - tmp.Remove( pos, tmp.Len()-pos ); - m_lines.Add( new wxSourceLine( tmp ) ); + m_lines.Add( new wxSourceLine( value.Mid( begin, pos-begin ) ) ); + + begin = pos+1; + } } } @@ -342,7 +348,10 @@ void wxTextCtrl::Clear() m_cursorX = 0; m_cursorY = 0; ClearSelection(); + m_lines.Clear(); + m_lines.Add( new wxSourceLine( wxT("") ) ); + SetScrollbars( m_charWidth, m_lineHeight, 0, 0, 0, 0 ); Refresh(); m_undos.Clear(); @@ -369,6 +378,8 @@ void wxTextCtrl::SetMaxLength(unsigned long len) void wxTextCtrl::WriteText(const wxString& text2) { + if (text2.IsEmpty()) return; + m_modified = TRUE; wxString text( text2 ); @@ -423,16 +434,47 @@ void wxTextCtrl::WriteText(const wxString& text2) } } -void wxTextCtrl::AppendText(const wxString& text) +void wxTextCtrl::AppendText(const wxString& text2) { - // Leaves cursor garbage + if (text2.IsEmpty()) return; - m_cursorY = m_lines.GetCount()-1; - m_cursorX = m_lines[m_cursorY].m_text.Len(); + m_modified = TRUE; - WriteText( text ); + wxString text( text2 ); + wxArrayString lines; + int pos; + while ( (pos = text.Find('\n')) != -1 ) + { + lines.Add( text.Left( pos ) ); + text.Remove( 0, pos+1 ); + } + lines.Add( text ); + int count = (int)lines.GetCount(); - Refresh(); + size_t y = m_lines.GetCount()-1; + + wxString tmp( m_lines[y].m_text ); + tmp.Append( lines[0] ); + + if (count == 1) + { + m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, y, y, this ) ); + + m_lines[y].m_text = tmp; + RefreshLine( y ); + } + else + { + m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE, y, y+count-1, this ) ); + + m_lines[y].m_text = tmp; + int i; + for (i = 1; i < count; i++) + m_lines.Insert( new wxSourceLine( lines[i] ), y+i ); + + MyAdjustScrollbars(); + RefreshDown( y ); + } } bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)