1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
7 #include "rendercontrol.hh"
13 Label::Label(Widget *parent)
16 _justify_horz(RenderStyle::LeftTopJustify),
17 _justify_vert(RenderStyle::LeftTopJustify),
20 styleChanged(*RenderStyle::style(screen()));
27 void Label::setHorizontalJustify(RenderStyle::Justify j)
33 void Label::setVerticalJustify(RenderStyle::Justify j)
39 void Label::setHighlighted(bool h)
42 styleChanged(*RenderStyle::style(screen()));
46 void Label::setText(const ustring &text)
48 bool utf = text.utf8();
49 std::string s = text.c_str(); // use a normal string, for its functionality
53 // parse it into multiple lines
54 std::string::size_type p = 0;
55 while (p != std::string::npos) {
56 std::string::size_type p2 = s.find('\n', p);
57 _parsedtext.push_back(s.substr(p, (p2==std::string::npos?p2:p2-p)));
58 _parsedtext.back().setUtf8(utf);
59 p = (p2==std::string::npos?p2:p2+1);
64 void Label::setFont(const Font *f)
70 void Label::calcDefaultSizes()
73 // find the longest line
74 std::vector<ustring>::iterator it, end = _parsedtext.end();
75 for (it = _parsedtext.begin(); it != end; ++it) {
76 int length = _font->measureString(*it);
77 if (length < 0) continue; // lines too long get skipped
78 if (length > longest) longest = length;
80 setMinSize(Size(longest + borderWidth() * 2 + bevel() * 4,
81 _parsedtext.size() * _font->height() + borderWidth() * 2 +
85 void Label::styleChanged(const RenderStyle &style)
88 _texture = style.labelFocusBackground();
89 _forecolor = style.textFocusColor();
91 _texture = style.labelUnfocusBackground();
92 _forecolor = style.textUnfocusColor();
94 if (_font != style.labelFont()) {
95 _font = style.labelFont();
100 void Label::renderForeground(Surface &surface)
102 const RenderControl *control = display->renderControl(screen());
103 int sidemargin = bevel() * 2;
105 int w = area().width() - borderWidth() * 2 - sidemargin * 2;
106 int h = area().height() - borderWidth() * 2 - bevel() * 2;
108 switch (_justify_vert) {
109 case RenderStyle::RightBottomJustify:
110 y += h - (_parsedtext.size() * _font->height());
111 if (y < bevel()) y = bevel();
113 case RenderStyle::CenterJustify:
114 y += (h - (_parsedtext.size() * _font->height())) / 2;
115 if (y < bevel()) y = bevel();
117 case RenderStyle::LeftTopJustify:
121 if (w <= 0) return; // can't fit anything
123 std::vector<ustring>::iterator it, end = _parsedtext.end();
124 for (it = _parsedtext.begin(); it != end; ++it, y += _font->height()) {
125 ustring t = *it; // the actual text to draw
126 int x = sidemargin; // x coord for the text
128 // find a string that will fit inside the area for text
129 ustring::size_type text_len = t.size();
134 length = _font->measureString(t);
135 } while (length > w && text_len-- > 0);
136 if (length < 0) continue; // lines too long get skipped
138 if (text_len <= 0) continue; // won't fit anything
141 switch (_justify_horz) {
142 case RenderStyle::RightBottomJustify:
145 case RenderStyle::CenterJustify:
146 x += (w - length) / 2;
148 case RenderStyle::LeftTopJustify:
152 control->drawString(surface, *_font, x, y, *_forecolor, t);