summaryrefslogtreecommitdiff
path: root/src/gui/widgets
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/widgets')
-rw-r--r--src/gui/widgets/label.cpp34
-rw-r--r--src/gui/widgets/label.h2
2 files changed, 36 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);
+ }
+}
diff --git a/src/gui/widgets/label.h b/src/gui/widgets/label.h
index 31d75b6ff..42f07d1c2 100644
--- a/src/gui/widgets/label.h
+++ b/src/gui/widgets/label.h
@@ -68,6 +68,8 @@ class Label final : public gcn::Label, public Widget2
void setForegroundColorAll(const gcn::Color &color1,
const gcn::Color &color2);
+ void resizeTo(const int sz);
+
static Skin *mSkin;
static int mInstances;