From ee253d4c9075667de1c4977894d6dfef9c2d9a13 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 4 Nov 2003 22:00:36 +0000 Subject: modified NewGui::drawString to shorten strings which don't fit using ellipsis (disabled for the EditTextWidget for now) svn-id: r11136 --- gui/EditTextWidget.cpp | 2 +- gui/launcher.cpp | 1 - gui/newgui.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++--- gui/newgui.h | 2 +- gui/options.cpp | 1 - 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/gui/EditTextWidget.cpp b/gui/EditTextWidget.cpp index 0e1781393f..28b2709954 100644 --- a/gui/EditTextWidget.cpp +++ b/gui/EditTextWidget.cpp @@ -151,7 +151,7 @@ void EditTextWidget::drawWidget(bool hilite) { // Draw the text adjustOffset(); - g_gui.drawString(_label, _x + 2, _y + 3, _w - 6, g_gui._textcolor, kTextAlignLeft, -_labelOffset); + g_gui.drawString(_label, _x + 2, _y + 3, _w - 6, g_gui._textcolor, kTextAlignLeft, -_labelOffset, false); } int EditTextWidget::getCaretPos() { diff --git a/gui/launcher.cpp b/gui/launcher.cpp index e53c550ac3..00ca321c4e 100644 --- a/gui/launcher.cpp +++ b/gui/launcher.cpp @@ -127,7 +127,6 @@ EditGameDialog::EditGameDialog(const String &domain, GameSettings target) yoffset += 16; // GUI: Label for the game path - // TODO: Allow editing, and clip to the RIGHT on long paths (to keep meaningful portions) new StaticTextWidget(tab, 10, yoffset, 40, kLineHeight, "Path: ", kTextAlignRight); new StaticTextWidget(tab, 50, yoffset, _w - 50 - 10, kLineHeight, path, kTextAlignLeft); 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; diff --git a/gui/newgui.h b/gui/newgui.h index ac0c454df0..09ccd612b2 100644 --- a/gui/newgui.h +++ b/gui/newgui.h @@ -137,7 +137,7 @@ public: void drawChar(byte c, int x, int y, NewGuiColor color); int getStringWidth(const String &str); int getCharWidth(byte c); - void drawString(const String &str, int x, int y, int w, NewGuiColor color, int align = kTextAlignLeft, int deltax = 0); + void drawString(const String &str, int x, int y, int w, NewGuiColor color, int align = kTextAlignLeft, int deltax = 0, bool useEllipsis = true); void blitFromBuffer(int x, int y, int w, int h, const byte *buf, int pitch); void blitToBuffer(int x, int y, int w, int h, byte *buf, int pitch); diff --git a/gui/options.cpp b/gui/options.cpp index f5de911609..3247e6911f 100644 --- a/gui/options.cpp +++ b/gui/options.cpp @@ -125,7 +125,6 @@ GlobalOptionsDialog::GlobalOptionsDialog(GameDetector &detector) yoffset = vBorder; // The MIDI mode popup & a label - //new StaticTextWidget(tab, 5, vBorder+2, 100, kLineHeight, "Music driver: ", kTextAlignRight); _midiPopUp = new PopUpWidget(tab, 5, yoffset, 280, kLineHeight, "Music driver: ", 100); yoffset += 16; -- cgit v1.2.3