improved string wrapping (incl. '\n')

fixed potential exception in StringListWidget
This commit is contained in:
thrust26 2020-11-18 21:02:42 +01:00
parent c19cde6f11
commit 59f157187f
3 changed files with 23 additions and 11 deletions

View File

@ -307,7 +307,7 @@ void FBSurface::splitString(const GUI::Font& font, const string& s, int w,
for(pos = 0; pos < s.size(); ++pos) for(pos = 0; pos < s.size(); ++pos)
{ {
int charWidth = font.getCharWidth(s[pos]); int charWidth = font.getCharWidth(s[pos]);
if(w2 + charWidth > w) if(w2 + charWidth > w || s[pos] == '\n')
{ {
split = true; split = true;
break; break;
@ -321,7 +321,7 @@ void FBSurface::splitString(const GUI::Font& font, const string& s, int w,
if(isWhiteSpace(s[i])) if(isWhiteSpace(s[i]))
{ {
left = s.substr(0, 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++; i++;
right = s.substr(i); right = s.substr(i);
return; return;
@ -334,7 +334,7 @@ void FBSurface::splitString(const GUI::Font& font, const string& s, int w,
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FBSurface::isWhiteSpace(const char s) const bool FBSurface::isWhiteSpace(const char s) const
{ {
const string WHITESPACES = " ,.;:+-"; const string WHITESPACES = " ,.;:+-*/'([\n";
for(size_t i = 0; i < WHITESPACES.length(); ++i) for(size_t i = 0; i < WHITESPACES.length(); ++i)
if(s == WHITESPACES[i]) if(s == WHITESPACES[i])
@ -349,13 +349,14 @@ int FBSurface::drawString(const GUI::Font& font, const string& s,
ColorId color, TextAlign align, ColorId color, TextAlign align,
int deltax, bool useEllipsis, ColorId shadowColor) int deltax, bool useEllipsis, ColorId shadowColor)
{ {
int lines = 1; int lines = 0;
#ifdef GUI_SUPPORT #ifdef GUI_SUPPORT
string inStr = s; string inStr = s;
// draw multiline string // 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 is too wide.
string leftStr, rightStr; string leftStr, rightStr;
@ -367,7 +368,11 @@ int FBSurface::drawString(const GUI::Font& font, const string& s,
inStr = rightStr; inStr = rightStr;
lines++; lines++;
} }
if(inStr.length())
{
drawString(font, inStr, x, y, w, color, align, deltax, useEllipsis, shadowColor); drawString(font, inStr, x, y, w, color, align, deltax, useEllipsis, shadowColor);
lines++;
}
#endif #endif
return lines; return lines;
} }

View File

@ -53,14 +53,24 @@ void StringListWidget::setList(const StringList& list)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int StringListWidget::getToolTipIndex(Common::Point pos) const 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 string StringListWidget::getToolTip(Common::Point pos) const
{ {
Common::Rect rect = getEditRect(); 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()) if(uInt32(_font.getStringWidth(value)) > rect.w())
return _toolTipText + value; return _toolTipText + value;

View File

@ -25,9 +25,6 @@
#include "ToolTip.hxx" #include "ToolTip.hxx"
// TODOs:
// - option to disable tips
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ToolTip::ToolTip(Dialog& dialog, const GUI::Font& font) ToolTip::ToolTip(Dialog& dialog, const GUI::Font& font)
: myDialog(dialog) : myDialog(dialog)