summaryrefslogtreecommitdiff
path: root/plugingui/textedit.cc
diff options
context:
space:
mode:
authorAndré Nusser <andre.nusser@googlemail.com>2019-03-04 00:51:03 +0100
committerAndré Nusser <andre.nusser@googlemail.com>2019-03-04 18:42:23 +0100
commitf536d8479e37b1515ffb5737a9f276976bef3cf0 (patch)
tree082b19fcb566c60d98e6a206881bf224d3ab35a0 /plugingui/textedit.cc
parent96c059c3354d26a1bbebd389fb827322aaf1d0ec (diff)
Add word-break instead of naive character-break in TextEdit.
Diffstat (limited to 'plugingui/textedit.cc')
-rw-r--r--plugingui/textedit.cc36
1 files changed, 26 insertions, 10 deletions
diff --git a/plugingui/textedit.cc b/plugingui/textedit.cc
index b6945de..9688b82 100644
--- a/plugingui/textedit.cc
+++ b/plugingui/textedit.cc
@@ -115,21 +115,37 @@ void TextEdit::preprocessText()
} while(pos != std::string::npos);
// Wrap long lines
- for(auto it = lines.begin(); it != lines.end(); ++it)
+ auto const max_width = width() - 2*x_border - 10 - scroll.width();
+ for(auto const& line: lines)
{
- auto line = *it;
-
- for(std::size_t i = 0; i < line.length(); ++i)
+ std::string valid;
+ std::string current;
+ for(auto c: line)
{
- auto linewidth = font.textWidth(line.substr(0, i));
- if(linewidth >= width() - 2*x_border - 10 - scroll.width())
+ current += c;
+ if(c == ' ')
+ {
+ valid.append(current.substr(valid.size()));
+ }
+
+ if(font.textWidth(current) >= max_width)
{
- preprocessed_text.push_back(line.substr(0, i));
- line = line.substr(i);
- i = 0;
+ if(valid.empty())
+ {
+ current.pop_back();
+ preprocessed_text.push_back(current);
+ current = c;
+ }
+ else
+ {
+ current = current.substr(valid.size());
+ valid.pop_back();
+ preprocessed_text.push_back(valid);
+ valid.clear();
+ }
}
}
- preprocessed_text.push_back(line);
+ preprocessed_text.push_back(current);
}
}