summaryrefslogtreecommitdiff
path: root/src/gui/widgets/label.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/widgets/label.cpp')
-rw-r--r--src/gui/widgets/label.cpp34
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);
+ }
+}