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),
22 styleChanged(*RenderStyle::style(screen()));
29 void Label::setHorizontalJustify(RenderStyle::Justify j)
35 void Label::setVerticalJustify(RenderStyle::Justify j)
41 void Label::setHighlighted(bool h)
44 styleChanged(*RenderStyle::style(screen()));
48 void Label::setText(const ustring &text)
50 bool utf = text.utf8();
51 std::string s = text.c_str(); // use a normal string, for its functionality
55 // parse it into multiple lines
56 std::string::size_type p = 0;
57 while (p != std::string::npos) {
58 std::string::size_type p2 = s.find('\n', p);
59 _parsedtext.push_back(s.substr(p, (p2==std::string::npos?p2:p2-p)));
60 _parsedtext.back().setUtf8(utf);
61 p = (p2==std::string::npos?p2:p2+1);
66 void Label::setFont(const Font *f)
72 void Label::calcDefaultSizes()
74 unsigned int longest = 0;
75 // find the longest line
76 std::vector<ustring>::iterator it, end = _parsedtext.end();
77 for (it = _parsedtext.begin(); it != end; ++it) {
78 unsigned int length = _font->measureString(*it);
79 if (length > longest) longest = length;
81 setMinSize(Size(longest + borderWidth() * 2 + bevel() * 4,
82 _parsedtext.size() * _font->height() + borderWidth() * 2 +
86 void Label::styleChanged(const RenderStyle &style)
89 _texture = style.labelFocusBackground();
90 _forecolor = style.textFocusColor();
92 _texture = style.labelUnfocusBackground();
93 _forecolor = style.textUnfocusColor();
95 _font = style.labelFont();
96 Widget::styleChanged(style);
100 void Label::renderForeground(Surface &surface)
102 const RenderControl *control = display->renderControl(screen());
103 unsigned int sidemargin = bevel() * 2;
105 unsigned int w = area().width() - borderWidth() * 2 - sidemargin * 2;
106 unsigned 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);
137 if (text_len <= 0) continue; // won't fit anything
140 switch (_justify_horz) {
141 case RenderStyle::RightBottomJustify:
144 case RenderStyle::CenterJustify:
145 x += (w - length) / 2;
147 case RenderStyle::LeftTopJustify:
151 control->drawString(surface, *_font, x, y, *_forecolor, t);