Move the word-wrapping code to support, so that it can be re-used.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@29559 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2009-05-07 12:23:12 +00:00
parent 128c2b31d8
commit cca51a04c2
3 changed files with 54 additions and 23 deletions

View File

@ -385,7 +385,6 @@ bool InsetCitation::isCompatibleCommand(string const & cmd)
docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
{
static unsigned int maxwdth = 80;
Buffer const & buf = bv.buffer();
// Only after the buffer is loaded from file...
if (!buf.isFullyLoaded())
@ -404,32 +403,12 @@ docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
vector<docstring>::const_iterator en = keys.end();
docstring tip;
for (; it != en; ++it) {
docstring key_info = bi.getInfo(*it);
docstring const key_info = bi.getInfo(*it);
if (key_info.empty())
continue;
if (!tip.empty())
tip += "\n";
docstring newkey;
while (key_info.size() > maxwdth) {
int i = maxwdth - 1;
// find the last space
for (; i >= 0; --i)
if (key_info[i] == ' ')
break;
if (i < 0) {
// no space found
key_info = key_info.substr(0, maxwdth - 3) + "...";
break;
}
if (!newkey.empty())
newkey += "\n";
newkey += key_info.substr(0, i);
key_info = " " + key_info.substr(i);
}
if (!newkey.empty())
newkey += "\n";
newkey += key_info;
tip += newkey;
tip += wrap(key_info, -4);
}
return tip;
}

View File

@ -928,6 +928,47 @@ docstring const escape(docstring const & lab)
}
docstring wrap(docstring const & str, int const ind, size_t const width)
{
docstring s = trim(str);
if (s.empty())
return docstring();
docstring indent;
if (ind < 0)
for (int j = 0; j > ind; --j)
indent += " ";
else if (ind > 0)
for (int j = 0; j < ind; ++j)
s = " " + s;
docstring retval;
while (s.size() > width) {
size_t i = width - 1;
// find the last space
for (; i >= 0; --i)
if (s[i] == ' ')
break;
if (i < 0) {
// no space found
s = s.substr(0, width - 3) + "...";
break;
}
if (!retval.empty())
retval += "\n";
retval += s.substr(0, i);
s = indent + s.substr(i);
}
if (!s.empty()) {
if (!retval.empty())
retval += "\n";
retval += s;
}
return retval;
}
namespace {
template<typename String> vector<String> const

View File

@ -226,6 +226,17 @@ std::string const rsplit(std::string const & a, std::string & piece, char delim)
/// problems in latex labels.
docstring const escape(docstring const & lab);
/// Word-wraps the provided docstring, returning a line-broken string
/// of width no wider than width, with the string broken at spaces.
/// If the string cannot be broken appropriately, it returns something
/// with "..." at the end, again no wider than width.
/// We assume here that str does not contain newlines.
/// If indent is positive, then the first line is indented that many
/// spaces. If it is negative, then successive lines are indented, as
/// if the first line were "outdented".
docstring wrap(docstring const & str, int const indent = 0,
size_t const width = 80);
/// gives a vector of stringparts which have the delimiter delim
/// If \p keepempty is true, empty strings will be pushed to the vector as well
std::vector<std::string> const getVectorFromString(std::string const & str,