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
54 // parse it into multiple lines
55 std::string::size_type p = 0;
56 while (p != std::string::npos) {
57 std::string::size_type p2 = s.find('\n', p);
58 std::string s(s.substr(p, (p2==std::string::npos?p2:p2-p)));
60 // turn tabs into spaces (multiples of 8)
61 std::string::size_type t;
62 while ((t = s.find('\t')) != std::string::npos)
63 s.replace(t, 1, std::string(8 - t % 8, ' '));
65 _parsedtext.push_back(s);
66 _parsedtext.back().setUtf8(utf);
67 p = (p2==std::string::npos?p2:p2+1);
72 void Label::setFont(const Font *f)
78 void Label::calcDefaultSizes()
81 // find the longest line
82 std::vector<ustring>::iterator it, end = _parsedtext.end();
83 for (it = _parsedtext.begin(); it != end; ++it) {
84 int length = _font->measureString(*it);
85 if (length < 0) continue; // lines too long get skipped
86 if (length > longest) longest = length;
88 setMinSize(Size(longest + borderWidth() * 2 + bevel() * 4,
89 _parsedtext.size() * _font->height() + borderWidth() * 2 +
93 void Label::styleChanged(const RenderStyle &style)
96 _texture = style.labelFocusBackground();
97 _forecolor = style.textFocusColor();
99 _texture = style.labelUnfocusBackground();
100 _forecolor = style.textUnfocusColor();
102 if (_font != style.labelFont()) {
103 _font = style.labelFont();
108 void Label::renderForeground(Surface &surface)
110 const RenderControl *control = display->renderControl(screen());
111 int sidemargin = bevel() * 2;
113 int w = area().width() - borderWidth() * 2 - sidemargin * 2;
114 int h = area().height() - borderWidth() * 2 - bevel() * 2;
116 switch (_justify_vert) {
117 case RenderStyle::RightBottomJustify:
118 y += h - (_parsedtext.size() * _font->height());
119 if (y < bevel()) y = bevel();
121 case RenderStyle::CenterJustify:
122 y += (h - (_parsedtext.size() * _font->height())) / 2;
123 if (y < bevel()) y = bevel();
125 case RenderStyle::LeftTopJustify:
129 if (w <= 0) return; // can't fit anything
131 std::vector<ustring>::iterator it, end = _parsedtext.end();
132 for (it = _parsedtext.begin(); it != end; ++it, y += _font->height()) {
133 ustring t = *it; // the actual text to draw
134 int x = sidemargin; // x coord for the text
136 // find a string that will fit inside the area for text
137 ustring::size_type text_len = t.size();
142 length = _font->measureString(t);
143 } while (length > w && text_len-- > 0);
144 if (length < 0) continue; // lines too long get skipped
146 if (text_len <= 0) continue; // won't fit anything
149 switch (_justify_horz) {
150 case RenderStyle::RightBottomJustify:
153 case RenderStyle::CenterJustify:
154 x += (w - length) / 2;
156 case RenderStyle::LeftTopJustify:
160 control->drawString(surface, *_font, x, y, *_forecolor, t);