]>
Dogcows Code - chaz/openbox/blob - otk/label.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
7 #include "rendercontrol.hh"
13 Label::Label(Widget
*parent
)
17 _justify_horz(RenderStyle::LeftTopJustify
),
18 _justify_vert(RenderStyle::LeftTopJustify
),
21 styleChanged(*RenderStyle::style(screen()));
28 void Label::setHorizontalJustify(RenderStyle::Justify j
)
34 void Label::setVerticalJustify(RenderStyle::Justify j
)
40 void Label::setHighlighted(bool h
)
43 styleChanged(*RenderStyle::style(screen()));
47 void Label::setText(const ustring
&text
)
49 bool utf
= text
.utf8();
50 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 std::string
s(s
.substr(p
, (p2
==std::string::npos
?p2
:p2
-p
)));
61 // turn tabs into spaces (multiples of 8)
62 std::string::size_type t
;
63 while ((t
= s
.find('\t')) != std::string::npos
)
64 s
.replace(t
, 1, std::string(8 - t
% 8, ' '));
66 _parsedtext
.push_back(s
);
67 _parsedtext
.back().setUtf8(utf
);
68 p
= (p2
==std::string::npos
?p2
:p2
+1);
73 void Label::setFont(const Font
*f
)
79 void Label::calcDefaultSizes()
82 // find the longest line
83 std::vector
<ustring
>::iterator it
, end
= _parsedtext
.end();
84 for (it
= _parsedtext
.begin(); it
!= end
; ++it
) {
85 int length
= _font
->measureString(*it
);
86 if (length
< 0) continue; // lines too long get skipped
87 if (length
> longest
) longest
= length
;
89 setMinSize(Size(longest
+ borderWidth() * 2 + bevel() * 4,
90 _parsedtext
.size() * _font
->height() + borderWidth() * 2 +
94 void Label::styleChanged(const RenderStyle
&style
)
97 _texture
= style
.labelFocusBackground();
98 _forecolor
= style
.textFocusColor();
100 _texture
= style
.labelUnfocusBackground();
101 _forecolor
= style
.textUnfocusColor();
103 if (_font
!= style
.labelFont()) {
104 _font
= style
.labelFont();
109 void Label::renderForeground(Surface
&surface
)
111 const RenderControl
*control
= display
->renderControl(screen());
112 int sidemargin
= bevel() * 2;
114 int w
= area().width() - borderWidth() * 2 - sidemargin
* 2;
115 int h
= area().height() - borderWidth() * 2 - bevel() * 2;
117 switch (_justify_vert
) {
118 case RenderStyle::RightBottomJustify
:
119 y
+= h
- (_parsedtext
.size() * _font
->height());
120 if (y
< bevel()) y
= bevel();
122 case RenderStyle::CenterJustify
:
123 y
+= (h
- (_parsedtext
.size() * _font
->height())) / 2;
124 if (y
< bevel()) y
= bevel();
126 case RenderStyle::LeftTopJustify
:
130 if (w
<= 0) return; // can't fit anything
132 std::vector
<ustring
>::iterator it
, end
= _parsedtext
.end();
133 for (it
= _parsedtext
.begin(); it
!= end
; ++it
, y
+= _font
->height()) {
134 ustring t
= *it
; // the actual text to draw
135 int x
= sidemargin
; // x coord for the text
137 // find a string that will fit inside the area for text
138 ustring::size_type text_len
= t
.size();
143 length
= _font
->measureString(t
);
144 } while (length
> w
&& text_len
-- > 0);
145 if (length
< 0) continue; // lines too long get skipped
147 if (text_len
<= 0) continue; // won't fit anything
150 switch (_justify_horz
) {
151 case RenderStyle::RightBottomJustify
:
154 case RenderStyle::CenterJustify
:
155 x
+= (w
- length
) / 2;
157 case RenderStyle::LeftTopJustify
:
161 control
->drawString(surface
, *_font
, x
, y
, *_forecolor
, t
);
This page took 0.04327 seconds and 4 git commands to generate.