diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-10-05 20:30:26 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-10-05 20:30:26 +0300 |
commit | 322aea1d69a47e8080bcd0552473a5a5d66c5e1a (patch) | |
tree | 10065fb47c0ee488e3e04da3bb86b3ddaa110e2a /src/gui/widgets/label.cpp | |
parent | 8fda16c465f8ea9ad6c748d4a5a097ff360fe960 (diff) | |
download | mv-322aea1d69a47e8080bcd0552473a5a5d66c5e1a.tar.gz mv-322aea1d69a47e8080bcd0552473a5a5d66c5e1a.tar.bz2 mv-322aea1d69a47e8080bcd0552473a5a5d66c5e1a.tar.xz mv-322aea1d69a47e8080bcd0552473a5a5d66c5e1a.zip |
add dynamic labels string cutting.
using it in char creation dialog.
Diffstat (limited to 'src/gui/widgets/label.cpp')
-rw-r--r-- | src/gui/widgets/label.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/gui/widgets/label.cpp b/src/gui/widgets/label.cpp index 29bd39af9..d57268bdd 100644 --- a/src/gui/widgets/label.cpp +++ b/src/gui/widgets/label.cpp @@ -127,3 +127,37 @@ void Label::setForegroundColorAll(const gcn::Color &color1, mForegroundColor = color1; mForegroundColor2 = color2; } + +void Label::resizeTo(const int sz) +{ + const gcn::Font *const font = getFont(); + const int pad2 = 2 * mPadding; + setHeight(font->getHeight() + pad2); + + if (font->getWidth(mCaption) + pad2 > sz) + { + const int dots = font->getWidth("..."); + if (dots > sz) + { + setWidth(sz); + return; + } + const size_t szChars = mCaption.size(); + for (size_t f = 1; f < szChars - 1; f ++) + { + const std::string text = mCaption.substr(0, szChars - f); + const int width = font->getWidth(text) + dots + pad2; + if (width <= sz) + { + setCaption(text + "..."); + setWidth(width); + return; + } + } + setWidth(sz); + } + else + { + setWidth(font->getWidth(mCaption) + pad2); + } +} |