diff options
-rw-r--r-- | plugingui/label.cc | 32 | ||||
-rw-r--r-- | plugingui/label.h | 15 |
2 files changed, 40 insertions, 7 deletions
diff --git a/plugingui/label.cc b/plugingui/label.cc index 18ba1e8..f4a2cc1 100644 --- a/plugingui/label.cc +++ b/plugingui/label.cc @@ -31,19 +31,28 @@ namespace GUI { -Label::Label(GUI::Widget *parent) +Label::Label(Widget *parent) : Widget(parent) - , font(":fontemboss.png") { } -void Label::setText(std::string text) +void Label::setText(const std::string& text) { _text = text; repaintEvent(nullptr); } -void Label::repaintEvent(GUI::RepaintEvent* repaintEvent) +void Label::setAlignment(TextAlignment alignment) +{ + this->alignment = alignment; +} + +void Label::resizeToText() +{ + resize(font.textWidth(_text) + border, font.textHeight()); +} + +void Label::repaintEvent(RepaintEvent* repaintEvent) { Painter p(*this); @@ -51,7 +60,20 @@ void Label::repaintEvent(GUI::RepaintEvent* repaintEvent) p.setColour(Colour(1)); - p.drawText(10, (height() + font.textHeight()) / 2, font, _text, true); + int offset = 0; + switch(alignment) { + case TextAlignment::left: + offset = border; + break; + case TextAlignment::center: + offset = (width() - font.textWidth(_text)) / 2; + break; + case TextAlignment::right: + offset = width() - font.textWidth(_text) - border; + break; + } + + p.drawText(offset, (height() + font.textHeight()) / 2, font, _text, true); } } // GUI:: diff --git a/plugingui/label.h b/plugingui/label.h index 3f0b93f..794778d 100644 --- a/plugingui/label.h +++ b/plugingui/label.h @@ -34,11 +34,21 @@ namespace GUI { +enum class TextAlignment { + left, + center, + right, +}; + class Label : public Widget { public: Label(Widget *parent); - void setText(std::string text); + void setText(const std::string& text); + + void setAlignment(TextAlignment alignment); + + void resizeToText(); protected: // From Widget: @@ -47,7 +57,8 @@ protected: private: std::string _text; Font font{":fontemboss.png"}; - + TextAlignment alignment{TextAlignment::left}; + int border{0}; }; } // GUI:: |