From 24d6018cd908b95825e861a5c9df134060ff9286 Mon Sep 17 00:00:00 2001 From: José Ávila Date: Sun, 3 Jul 2005 17:26:58 +0000 Subject: Implemented wrap text in chat box correctly ;) --- src/gui/browserbox.cpp | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'src/gui/browserbox.cpp') diff --git a/src/gui/browserbox.cpp b/src/gui/browserbox.cpp index 168708ad..a2b93c9d 100644 --- a/src/gui/browserbox.cpp +++ b/src/gui/browserbox.cpp @@ -328,14 +328,36 @@ void BrowserBox::draw(gcn::Graphics* graphics) // Auto wrap mode if (mMode == AUTO_WRAP) { - /** NOTE (by Javila): this is just a simple example and - * will force text wrap at widget width!!! - * Maybe it can need improvements. - */ - if ((x + 2 * browserFont->getWidth('~')) > getWidth()) + unsigned int nextChar = j + 1; + char hyphen = '~'; + int hyphenWidth = browserFont->getWidth(hyphen); + + // Wraping between words (at blank spaces) + if ((nextChar < row.size()) && (row.at(nextChar) == ' ')) + { + int nextSpacePos = row.find(" ", (nextChar + 1)); + if (nextSpacePos <= 0) + { + nextSpacePos = row.size() - 1; + } + int nextWordWidth = browserFont->getWidth( + row.substr(nextChar, + (nextSpacePos - nextChar))); + + if ((x + nextWordWidth + 10) > getWidth()) + { + x = 15; // Ident in new line + y += browserFont->getHeight(); + wrappedLines++; + j++; + } + } + + // Wrapping looong lines (brutal force) + else if ((x + 2 * hyphenWidth) > getWidth()) { - browserFont->drawGlyph(graphics, '~', - getWidth() - browserFont->getWidth('~'), y); + browserFont->drawGlyph(graphics, hyphen, + getWidth() - hyphenWidth, y); x = 15; // Ident in new line y += browserFont->getHeight(); wrappedLines++; -- cgit v1.2.3-70-g09d2