+void Label::setText(const ustring &text)
+{
+ bool utf = text.utf8();
+ std::string s = text.c_str(); // use a normal string, for its functionality
+
+ _parsedtext.clear();
+ _text = text;
+
+ // parse it into multiple lines
+ std::string::size_type p = 0;
+ while (p != std::string::npos) {
+ std::string::size_type p2 = s.find('\n', p);
+ std::string s(s.substr(p, (p2==std::string::npos?p2:p2-p)));
+
+ // turn tabs into spaces (multiples of 8)
+ std::string::size_type t;
+ while ((t = s.find('\t')) != std::string::npos)
+ s.replace(t, 1, std::string(8 - t % 8, ' '));
+
+ _parsedtext.push_back(s);
+ _parsedtext.back().setUtf8(utf);
+ p = (p2==std::string::npos?p2:p2+1);
+ }
+ calcDefaultSizes();
+}
+
+void Label::setFont(const Font *f)
+{
+ _font = f;
+ calcDefaultSizes();
+}
+
+void Label::calcDefaultSizes()
+{
+ int longest = 0;
+ // find the longest line
+ std::vector<ustring>::iterator it, end = _parsedtext.end();
+ for (it = _parsedtext.begin(); it != end; ++it) {
+ int length = _font->measureString(*it);
+ if (length < 0) continue; // lines too long get skipped
+ if (length > longest) longest = length;
+ }
+ setMinSize(Size(longest + borderWidth() * 2 + bevel() * 4,
+ _parsedtext.size() * _font->height() + borderWidth() * 2 +
+ bevel() * 2));
+}
+
+void Label::styleChanged(const RenderStyle &style)
+{
+ if (_highlight) {
+ _texture = style.labelFocusBackground();
+ _forecolor = style.textFocusColor();
+ } else {
+ _texture = style.labelUnfocusBackground();
+ _forecolor = style.textUnfocusColor();
+ }
+ if (_font != style.labelFont()) {
+ _font = style.labelFont();
+ calcDefaultSizes();
+ }
+}
+
+void Label::renderForeground(Surface &surface)
+{
+ const RenderControl *control = display->renderControl(screen());
+ int sidemargin = bevel() * 2;
+ int y = bevel();
+ int w = area().width() - borderWidth() * 2 - sidemargin * 2;
+ int h = area().height() - borderWidth() * 2 - bevel() * 2;
+
+ switch (_justify_vert) {
+ case RenderStyle::RightBottomJustify:
+ y += h - (_parsedtext.size() * _font->height());
+ if (y < bevel()) y = bevel();
+ break;
+ case RenderStyle::CenterJustify:
+ y += (h - (_parsedtext.size() * _font->height())) / 2;
+ if (y < bevel()) y = bevel();
+ break;
+ case RenderStyle::LeftTopJustify:
+ break;
+ }
+
+ if (w <= 0) return; // can't fit anything
+
+ std::vector<ustring>::iterator it, end = _parsedtext.end();
+ for (it = _parsedtext.begin(); it != end; ++it, y += _font->height()) {
+ ustring t = *it; // the actual text to draw