aboutsummaryrefslogtreecommitdiff
path: root/gui/newgui.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gui/newgui.cpp')
-rw-r--r--gui/newgui.cpp58
1 files changed, 55 insertions, 3 deletions
diff --git a/gui/newgui.cpp b/gui/newgui.cpp
index d7326fee01..bf79c81a34 100644
--- a/gui/newgui.cpp
+++ b/gui/newgui.cpp
@@ -435,16 +435,68 @@ int NewGui::getCharWidth(byte c) {
return guifont[c+6];
}
-void NewGui::drawString(const String &str, int x, int y, int w, NewGuiColor color, int align, int deltax) {
+void NewGui::drawString(const String &s, int x, int y, int w, NewGuiColor color, int align, int deltax, bool useEllipsis) {
const int leftX = x, rightX = x + w;
- int width = getStringWidth(str);
+ int i;
+ int width = getStringWidth(s);
+ String str;
+
+ if (useEllipsis && width > w) {
+ // String is too wide. So we shorten it "intellegently", by replacing
+ // parts of it by an ellipsis ("..."). There are three possibilities
+ // for this: replace the start, the end, or the middle of the string.
+ // What is best really depends on the context; but unless we want to
+ // make this configurable, replacing the middle probably is a good
+ // compromise.
+ const int ellipsisWidth = getStringWidth("...");
+
+ // SLOW algorithm to remove enough of the middle. But it is good enough
+ // for now.
+ const int halfWidth = (w - ellipsisWidth) / 2;
+ int w2 = 0;
+
+ for (i = 0; i < s.size(); ++i) {
+ int charWidth = getCharWidth(s[i]);
+ if (w2 + charWidth > halfWidth)
+ break;
+ w2 += charWidth;
+ str += s[i];
+ }
+ // At this point we know that the first 'i' chars are together 'w2'
+ // pixels wide. We took the first i-1, and add "..." to them.
+ str += "...";
+
+ // The original string is width wide. Of those we already skipped past
+ // w2 pixels, which means (width - w2) remain.
+ // The new str is (w2+ellipsisWidth) wide, so we can accomodate about
+ // (w - (w2+ellipsisWidth)) more pixels.
+ // Thus we skip ((width - w2) - (w - (w2+ellipsisWidth))) =
+ // (width + ellipsisWidth - w)
+ int skip = width + ellipsisWidth - w;
+ for (; i < s.size() && skip > 0; ++i) {
+ skip -= getCharWidth(s[i]);
+ }
+
+ // Append the remaining chars, if any
+ for (; i < s.size(); ++i) {
+ str += s[i];
+ }
+
+ width = getStringWidth(str);
+
+ } else {
+ str = s;
+ }
+
+
+
if (align == kTextAlignCenter)
x = x + (w - width - 1)/2;
else if (align == kTextAlignRight)
x = x + w - width;
x += deltax;
- for (int i = 0; i < str.size(); ++i) {
+ for (i = 0; i < str.size(); ++i) {
w = getCharWidth(str[i]);
if (x+w > rightX)
break;