1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
9 #include "rendercontrol.hh"
15 Label::Label(Widget *parent)
18 _justify_horz(RenderStyle::LeftTopJustify),
19 _justify_vert(RenderStyle::LeftTopJustify)
21 styleChanged(*RenderStyle::style(screen()));
28 void Label::setHorizontalJustify(RenderStyle::Justify j)
34 void Label::setVerticalJustify(RenderStyle::Justify j)
40 void Label::setText(const ustring &text)
42 bool utf = text.utf8();
43 std::string s = text.c_str(); // use a normal string, for its functionality
47 // parse it into multiple lines
48 std::string::size_type p = 0;
49 while (p != std::string::npos) {
50 std::string::size_type p2 = s.find('\n', p);
51 _parsedtext.push_back(s.substr(p, (p2==std::string::npos?p2:p2-p)));
52 _parsedtext.back().setUtf8(utf);
53 p = (p2==std::string::npos?p2:p2+1);
58 void Label::setFont(const Font *f)
64 void Label::calcDefaultSizes()
66 unsigned int longest = 0;
67 // find the longest line
68 std::vector<ustring>::iterator it, end = _parsedtext.end();
69 for (it = _parsedtext.begin(); it != end; ++it) {
70 unsigned int length = _font->measureString(*it);
71 if (length > longest) longest = length;
73 setMinSize(Size(longest + borderWidth() * 2 + bevel() * 4,
74 _parsedtext.size() * _font->height() + borderWidth() * 2 +
78 void Label::styleChanged(const RenderStyle &style)
80 _texture = style.labelFocusBackground();
81 _forecolor = style.textFocusColor();
82 _font = style.labelFont();
83 Widget::styleChanged(style);
87 void Label::renderForeground(Surface &surface)
89 const RenderControl *control = display->renderControl(screen());
90 unsigned int sidemargin = bevel() * 2;
92 unsigned int w = area().width() - borderWidth() * 2 - sidemargin * 2;
93 unsigned int h = area().height() - borderWidth() * 2 - bevel() * 2;
95 switch (_justify_vert) {
96 case RenderStyle::RightBottomJustify:
97 y += h - (_parsedtext.size() * _font->height());
98 if (y < bevel()) y = bevel();
100 case RenderStyle::CenterJustify:
101 y += (h - (_parsedtext.size() * _font->height())) / 2;
102 if (y < bevel()) y = bevel();
104 case RenderStyle::LeftTopJustify:
108 if (w <= 0) return; // can't fit anything
110 std::vector<ustring>::iterator it, end = _parsedtext.end();
111 for (it = _parsedtext.begin(); it != end; ++it, y += _font->height()) {
112 ustring t = *it; // the actual text to draw
113 int x = sidemargin; // x coord for the text
115 // find a string that will fit inside the area for text
116 ustring::size_type text_len = t.size();
121 length = _font->measureString(t);
122 } while (length > w && text_len-- > 0);
124 if (text_len <= 0) continue; // won't fit anything
127 switch (_justify_horz) {
128 case RenderStyle::RightBottomJustify:
131 case RenderStyle::CenterJustify:
132 x += (w - length) / 2;
134 case RenderStyle::LeftTopJustify:
138 control->drawString(surface, *_font, x, y, *_forecolor, t);