From 9eea11ef832daf2af0654198b0cddb5847673378 Mon Sep 17 00:00:00 2001 From: thrust26 Date: Wed, 18 Nov 2020 21:02:42 +0100 Subject: [PATCH] improved string wrapping (incl. '\n') fixed potential exception in StringListWidget --- src/emucore/FBSurface.cxx | 17 +++++++++++------ src/gui/StringListWidget.cxx | 14 ++++++++++++-- src/gui/ToolTip.cxx | 3 --- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/emucore/FBSurface.cxx b/src/emucore/FBSurface.cxx index cfcf58ff3..6421dd171 100644 --- a/src/emucore/FBSurface.cxx +++ b/src/emucore/FBSurface.cxx @@ -307,7 +307,7 @@ void FBSurface::splitString(const GUI::Font& font, const string& s, int w, for(pos = 0; pos < s.size(); ++pos) { int charWidth = font.getCharWidth(s[pos]); - if(w2 + charWidth > w) + if(w2 + charWidth > w || s[pos] == '\n') { split = true; break; @@ -321,7 +321,7 @@ void FBSurface::splitString(const GUI::Font& font, const string& s, int w, if(isWhiteSpace(s[i])) { left = s.substr(0, i); - if(s[i] == ' ') // skip leading space after line break + if(s[i] == ' ' || s[pos] == '\n') // skip leading space after line break i++; right = s.substr(i); return; @@ -334,7 +334,7 @@ void FBSurface::splitString(const GUI::Font& font, const string& s, int w, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool FBSurface::isWhiteSpace(const char s) const { - const string WHITESPACES = " ,.;:+-"; + const string WHITESPACES = " ,.;:+-*/'([\n"; for(size_t i = 0; i < WHITESPACES.length(); ++i) if(s == WHITESPACES[i]) @@ -349,13 +349,14 @@ int FBSurface::drawString(const GUI::Font& font, const string& s, ColorId color, TextAlign align, int deltax, bool useEllipsis, ColorId shadowColor) { - int lines = 1; + int lines = 0; #ifdef GUI_SUPPORT string inStr = s; // draw multiline string - while (font.getStringWidth(inStr) > w && h >= font.getFontHeight() * 2) + //while (font.getStringWidth(inStr) > w && h >= font.getFontHeight() * 2) + while(inStr.length() && h >= font.getFontHeight() * 2) { // String is too wide. string leftStr, rightStr; @@ -367,7 +368,11 @@ int FBSurface::drawString(const GUI::Font& font, const string& s, inStr = rightStr; lines++; } - drawString(font, inStr, x, y, w, color, align, deltax, useEllipsis, shadowColor); + if(inStr.length()) + { + drawString(font, inStr, x, y, w, color, align, deltax, useEllipsis, shadowColor); + lines++; + } #endif return lines; } diff --git a/src/gui/StringListWidget.cxx b/src/gui/StringListWidget.cxx index 74cf23e40..04820090c 100644 --- a/src/gui/StringListWidget.cxx +++ b/src/gui/StringListWidget.cxx @@ -53,14 +53,24 @@ void StringListWidget::setList(const StringList& list) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int StringListWidget::getToolTipIndex(Common::Point pos) const { - return (pos.y - getAbsY()) / _lineHeight + _currentPos; + int idx = (pos.y - getAbsY()) / _lineHeight + _currentPos; + + if(idx >= int(_list.size())) + return -1; + else + return idx; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - string StringListWidget::getToolTip(Common::Point pos) const { Common::Rect rect = getEditRect(); - const string value = _list[getToolTipIndex(pos)]; + int idx = getToolTipIndex(pos); + + if(idx < 0) + return EmptyString; + + const string value = _list[idx]; if(uInt32(_font.getStringWidth(value)) > rect.w()) return _toolTipText + value; diff --git a/src/gui/ToolTip.cxx b/src/gui/ToolTip.cxx index 5cfed635c..6e6ef9fc9 100644 --- a/src/gui/ToolTip.cxx +++ b/src/gui/ToolTip.cxx @@ -25,9 +25,6 @@ #include "ToolTip.hxx" -// TODOs: -// - option to disable tips - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ToolTip::ToolTip(Dialog& dialog, const GUI::Font& font) : myDialog(dialog)