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 3433a6f013
commit 9eea11ef83
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)
{
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;
}

View File

@ -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;

View File

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