6#include "fifechan/widgets/label.hpp"
16#include "fifechan/exception.hpp"
17#include "fifechan/font.hpp"
18#include "fifechan/graphics.hpp"
27 Label::Label(std::string caption) :
mCaption(std::move(caption))
78 std::vector<std::string> lines;
80 std::string::size_type start = 0;
82 auto pos =
mCaption.find(
'\n', start);
83 if (pos == std::string::npos) {
84 lines.push_back(
mCaption.substr(start));
87 lines.push_back(
mCaption.substr(start, pos - start));
92 int const maxWidth = std::accumulate(lines.begin(), lines.end(), 0, [&](
int acc, std::string
const & line) {
93 return std::max(acc, getFont()->getWidth(line));
96 int const lineCount =
static_cast<int>(lines.size());
122 int const contentWidth = contentRight - contentLeft;
123 int const contentHeight = contentBottom - contentTop;
128 int const lineCount =
static_cast<int>(std::count(
getCaption().begin(),
getCaption().end(),
'\n')) + 1;
129 int const totalTextHeight = lineCount * fontHeight;
136 case Graphics::VerticalAlignment::Top:
139 case Graphics::VerticalAlignment::Center:
140 textY = contentTop + ((contentHeight - totalTextHeight) / 2);
142 case Graphics::VerticalAlignment::Bottom:
143 textY = contentBottom - totalTextHeight;
151 case Graphics::Alignment::Left:
154 case Graphics::Alignment::Center:
155 textX = contentLeft + (contentWidth / 2);
157 case Graphics::Alignment::Right:
158 textX = contentRight;
168 std::string::size_type start = 0;
174 if (pos == std::string::npos) {
178 line =
getCaption().substr(start, pos - start);
182 int const y = textY + (lineIndex * fontHeightLocal);
virtual int getHeight() const =0
Gets the height of the glyphs in the font.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
VerticalAlignment
Vertical alignments for text drawing.
Alignment
Horizontal alignments for text drawing.
void drawText(std::string const &text, int x, int y)
Draws text with a default left alignment.
virtual void setFont(Font *font)
Sets the font to use when drawing text.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
void draw(Graphics *graphics) override
Draws the widget.
void setVerticalAlignment(Graphics::VerticalAlignment alignment)
Sets the vertical alignment of the caption.
void setAlignment(Graphics::Alignment alignment)
Sets the alignment of the caption.
void adjustSize() override
Resizes the widget's size to fit the content exactly.
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
std::string const & getCaption() const
Gets the caption of the label.
Graphics::Alignment getAlignment() const
Gets the alignment of the caption.
void setCaption(std::string const &caption)
Sets the caption of the label.
Graphics::Alignment mAlignment
Holds the horizontal alignment of the caption.
std::string mCaption
Holds the caption of the label.
Graphics::VerticalAlignment mVerticalAlignment
Holds the vertical alignment of the caption.
Graphics::VerticalAlignment getVerticalAlignment() const
Gets the vertical alignment of the caption.
void adjustSizeImpl()
Adjusts the size of the label to fit the caption.
Represents a rectangular area (X, Y, Width, Height).
int y
Holds the x coordinate of the rectangle.
int x
Holds the x coordinate of the rectangle.
Used replacement tokens by configure_file():
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.