From 6dc7d892b729b13b5af5c5edbccfaaccf461f6cf Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sat, 13 Feb 2016 20:14:34 +0100 Subject: [PATCH] Fix indentation on copying code, closes #699 --- src/ui/controls/edit.cpp | 61 ++++++++++++++++++---------------------- src/ui/controls/edit.h | 2 ++ 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/ui/controls/edit.cpp b/src/ui/controls/edit.cpp index 84570cec..1c59c6be 100644 --- a/src/ui/controls/edit.cpp +++ b/src/ui/controls/edit.cpp @@ -1858,20 +1858,28 @@ bool CEdit::ReadText(std::string filename, int addSize) bool CEdit::WriteText(std::string filename) { - char buffer[1000+20]; - int i, j, k, n; - float iDim = 0.0f; - - if ( filename[0] == 0 ) return false; + if (filename.empty()) return false; COutputStream stream; stream.open(filename); if (!stream.is_open()) { + GetLogger()->Error("Failed to open output file: '%s'", filename.c_str()); return false; } + GetIndentedText(stream, 0, m_len); + + stream.close(); + + return true; +} + +void CEdit::GetIndentedText(std::ostream& stream, unsigned int start, unsigned int end) +{ + float iDim = 0.0f; + if ( m_bAutoIndent ) { iDim = m_dim.x; @@ -1879,42 +1887,31 @@ bool CEdit::WriteText(std::string filename) Justif(); } - i = j = k = 0; - while ( m_text[i] != 0 && i < m_len ) + unsigned int i = 0, line = 0; + while ( m_text[i] != 0 && i < end && i < static_cast(m_len) ) // TODO: fix this (un)signed comparation { - if ( m_bAutoIndent && i == m_lineOffset[k] ) + if ( m_bAutoIndent && i == static_cast(m_lineOffset[line]) ) // TODO: fix this (un)signed comparation { - for ( n=0 ; n start) + { + stream << '\t'; + } } - k ++; + line++; } - buffer[j++] = m_text[i]; - - if ( j >= 1000-1 ) - { - stream.write(buffer, j); - j = 0; - } + stream << m_text[i]; i ++; } - if ( j > 0 ) - { - stream.write(buffer, j); - } - - stream.close(); if ( m_bAutoIndent ) { m_dim.x = iDim; // presents the initial width Justif(); } - - return true; } @@ -2497,7 +2494,7 @@ bool CEdit::Cut() bool CEdit::Copy(bool memorize_cursor) { - int c1, c2, start, len; + int c1, c2; c1 = m_cursor1; c2 = m_cursor2; @@ -2531,13 +2528,9 @@ bool CEdit::Copy(bool memorize_cursor) return false; } - start = c1; - len = c2 - c1; - - std::vector text(len + 1, '\0'); - strncpy(text.data(), m_text.data() + start, len); - text[len] = 0; - SDL_SetClipboardText(text.data()); //TODO: Move to CApplication + std::stringstream ss; + GetIndentedText(ss, c1, c2); + SDL_SetClipboardText(ss.str().c_str()); //TODO: Move to CApplication if (memorize_cursor) { diff --git a/src/ui/controls/edit.h b/src/ui/controls/edit.h index ba712e9a..6418a19e 100644 --- a/src/ui/controls/edit.h +++ b/src/ui/controls/edit.h @@ -230,6 +230,8 @@ protected: void SetFocus(CControl* control) override; void UpdateFocus(); // Start/stop text input mode, this toggles the on-screen keyboard + void GetIndentedText(std::ostream& stream, unsigned int start, unsigned int end); + protected: std::unique_ptr m_scroll; // vertical scrollbar on the right