diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-11-29 21:30:22 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-11-29 21:30:22 +0100 |
commit | 25adc2eeda98d56c641ea6c33c019740fd332f98 (patch) | |
tree | e77f020515e413ecfc3e2bb18970bb2d6d3826e7 /plugingui/label.cc | |
parent | 2aeb2c9cf4da7ded3f68c046f6fa2773dded44a8 (diff) |
Add text alignment and one-time autosize to Label.
Diffstat (limited to 'plugingui/label.cc')
-rw-r--r-- | plugingui/label.cc | 32 |
1 files changed, 27 insertions, 5 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:: |