]> Dogcows Code - chaz/openbox/blob - otk/label.cc
use the c++ std cheaders
[chaz/openbox] / otk / label.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "label.hh"
6 #include "display.hh"
7 #include "rendercontrol.hh"
8
9 #include <string>
10
11 namespace otk {
12
13 Label::Label(Widget *parent)
14 : Widget(parent),
15 _text(""),
16 _justify_horz(RenderStyle::LeftTopJustify),
17 _justify_vert(RenderStyle::LeftTopJustify),
18 _highlight(false)
19 {
20 styleChanged(*RenderStyle::style(screen()));
21 }
22
23 Label::~Label()
24 {
25 }
26
27 void Label::setHorizontalJustify(RenderStyle::Justify j)
28 {
29 _justify_horz = j;
30 refresh();
31 }
32
33 void Label::setVerticalJustify(RenderStyle::Justify j)
34 {
35 _justify_vert = j;
36 refresh();
37 }
38
39 void Label::setHighlighted(bool h)
40 {
41 _highlight = h;
42 styleChanged(*RenderStyle::style(screen()));
43 refresh();
44 }
45
46 void Label::setText(const ustring &text)
47 {
48 bool utf = text.utf8();
49 std::string s = text.c_str(); // use a normal string, for its functionality
50
51 _parsedtext.clear();
52
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);
60 }
61 calcDefaultSizes();
62 }
63
64 void Label::setFont(const Font *f)
65 {
66 _font = f;
67 calcDefaultSizes();
68 }
69
70 void Label::calcDefaultSizes()
71 {
72 int longest = 0;
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;
79 }
80 setMinSize(Size(longest + borderWidth() * 2 + bevel() * 4,
81 _parsedtext.size() * _font->height() + borderWidth() * 2 +
82 bevel() * 2));
83 }
84
85 void Label::styleChanged(const RenderStyle &style)
86 {
87 if (_highlight) {
88 _texture = style.labelFocusBackground();
89 _forecolor = style.textFocusColor();
90 } else {
91 _texture = style.labelUnfocusBackground();
92 _forecolor = style.textUnfocusColor();
93 }
94 if (_font != style.labelFont()) {
95 _font = style.labelFont();
96 calcDefaultSizes();
97 }
98 }
99
100 void Label::renderForeground(Surface &surface)
101 {
102 const RenderControl *control = display->renderControl(screen());
103 int sidemargin = bevel() * 2;
104 int y = bevel();
105 int w = area().width() - borderWidth() * 2 - sidemargin * 2;
106 int h = area().height() - borderWidth() * 2 - bevel() * 2;
107
108 switch (_justify_vert) {
109 case RenderStyle::RightBottomJustify:
110 y += h - (_parsedtext.size() * _font->height());
111 if (y < bevel()) y = bevel();
112 break;
113 case RenderStyle::CenterJustify:
114 y += (h - (_parsedtext.size() * _font->height())) / 2;
115 if (y < bevel()) y = bevel();
116 break;
117 case RenderStyle::LeftTopJustify:
118 break;
119 }
120
121 if (w <= 0) return; // can't fit anything
122
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
127
128 // find a string that will fit inside the area for text
129 ustring::size_type text_len = t.size();
130 int length;
131
132 do {
133 t.resize(text_len);
134 length = _font->measureString(t);
135 } while (length > w && text_len-- > 0);
136 if (length < 0) continue; // lines too long get skipped
137
138 if (text_len <= 0) continue; // won't fit anything
139
140 // justify the text
141 switch (_justify_horz) {
142 case RenderStyle::RightBottomJustify:
143 x += w - length;
144 break;
145 case RenderStyle::CenterJustify:
146 x += (w - length) / 2;
147 break;
148 case RenderStyle::LeftTopJustify:
149 break;
150 }
151
152 control->drawString(surface, *_font, x, y, *_forecolor, t);
153 }
154 }
155
156 }
This page took 0.040769 seconds and 5 git commands to generate.