From 322aea1d69a47e8080bcd0552473a5a5d66c5e1a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 5 Oct 2013 20:30:26 +0300 Subject: add dynamic labels string cutting. using it in char creation dialog. --- src/gui/widgets/label.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/gui/widgets/label.cpp') 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); + } +} -- cgit v1.2.3-60-g2f50