Fix indentation on copying code, closes #699

dev-time-step
krzys-h 2016-02-13 20:14:34 +01:00
parent 463992b2c1
commit 6dc7d892b7
2 changed files with 29 additions and 34 deletions

View File

@ -1858,20 +1858,28 @@ bool CEdit::ReadText(std::string filename, int addSize)
bool CEdit::WriteText(std::string filename) bool CEdit::WriteText(std::string filename)
{ {
char buffer[1000+20]; if (filename.empty()) return false;
int i, j, k, n;
float iDim = 0.0f;
if ( filename[0] == 0 ) return false;
COutputStream stream; COutputStream stream;
stream.open(filename); stream.open(filename);
if (!stream.is_open()) if (!stream.is_open())
{ {
GetLogger()->Error("Failed to open output file: '%s'", filename.c_str());
return false; 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 ) if ( m_bAutoIndent )
{ {
iDim = m_dim.x; iDim = m_dim.x;
@ -1879,42 +1887,31 @@ bool CEdit::WriteText(std::string filename)
Justif(); Justif();
} }
i = j = k = 0; unsigned int i = 0, line = 0;
while ( m_text[i] != 0 && i < m_len ) while ( m_text[i] != 0 && i < end && i < static_cast<unsigned int>(m_len) ) // TODO: fix this (un)signed comparation
{ {
if ( m_bAutoIndent && i == m_lineOffset[k] ) if ( m_bAutoIndent && i == static_cast<unsigned int>(m_lineOffset[line]) ) // TODO: fix this (un)signed comparation
{ {
for ( n=0 ; n<m_lineIndent[k] ; n++ ) for (int n = 0; n < m_lineIndent[line]; n++)
{ {
buffer[j++] = '\t'; if (i > start)
{
stream << '\t';
} }
k ++; }
line++;
} }
buffer[j++] = m_text[i]; stream << m_text[i];
if ( j >= 1000-1 )
{
stream.write(buffer, j);
j = 0;
}
i ++; i ++;
} }
if ( j > 0 )
{
stream.write(buffer, j);
}
stream.close();
if ( m_bAutoIndent ) if ( m_bAutoIndent )
{ {
m_dim.x = iDim; // presents the initial width m_dim.x = iDim; // presents the initial width
Justif(); Justif();
} }
return true;
} }
@ -2497,7 +2494,7 @@ bool CEdit::Cut()
bool CEdit::Copy(bool memorize_cursor) bool CEdit::Copy(bool memorize_cursor)
{ {
int c1, c2, start, len; int c1, c2;
c1 = m_cursor1; c1 = m_cursor1;
c2 = m_cursor2; c2 = m_cursor2;
@ -2531,13 +2528,9 @@ bool CEdit::Copy(bool memorize_cursor)
return false; return false;
} }
start = c1; std::stringstream ss;
len = c2 - c1; GetIndentedText(ss, c1, c2);
SDL_SetClipboardText(ss.str().c_str()); //TODO: Move to CApplication
std::vector<char> text(len + 1, '\0');
strncpy(text.data(), m_text.data() + start, len);
text[len] = 0;
SDL_SetClipboardText(text.data()); //TODO: Move to CApplication
if (memorize_cursor) if (memorize_cursor)
{ {

View File

@ -230,6 +230,8 @@ protected:
void SetFocus(CControl* control) override; void SetFocus(CControl* control) override;
void UpdateFocus(); // Start/stop text input mode, this toggles the on-screen keyboard 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: protected:
std::unique_ptr<CScroll> m_scroll; // vertical scrollbar on the right std::unique_ptr<CScroll> m_scroll; // vertical scrollbar on the right