]> Dogcows Code - chaz/openbox/blob - otk/label.cc
add a Label class that doesnt change with focus
[chaz/openbox] / otk / label.cc
1 #include "label.hh"
2
3 namespace otk {
4
5 OtkLabel::OtkLabel(OtkWidget *parent)
6 : OtkWidget(parent), _text(""), _dirty(false)
7 {
8 setTexture(getStyle()->getLabelUnfocus());
9 }
10
11 OtkLabel::~OtkLabel()
12 {
13 }
14
15 void OtkLabel::update(void)
16 {
17 if (_dirty) {
18 const BFont &ft = getStyle()->getFont();
19 unsigned int bevel = getStyle()->getBevelWidth();
20
21 std::string t = _text; // the actual text to draw
22 int x = bevel; // x coord for the text
23
24 // find a string that will fit inside the area for text
25 int max_length = width() - getBevelWidth() * 2;
26 if (max_length <= 0) {
27 t = ""; // can't fit anything
28 } else {
29 size_t text_len = t.size();
30 int length;
31
32 do {
33 t.resize(text_len);
34 length = ft.measureString(t);
35 } while (length > max_length && text_len-- > 0);
36
37 // justify the text
38 switch (getStyle()->textJustify()) {
39 case Style::RightJustify:
40 x += max_length - length;
41 break;
42 case Style::CenterJustify:
43 x += (max_length - length) / 2;
44 break;
45 case Style::LeftJustify:
46 break;
47 }
48 }
49
50 OtkWidget::update();
51
52 ft.drawString(getWindow(), x, bevel, *getStyle()->getTextUnfocus(), t);
53 } else
54 OtkWidget::update();
55
56 _dirty = false;
57 }
58
59 int OtkLabel::exposeHandler(const XExposeEvent &e)
60 {
61 _dirty = true;
62 return OtkWidget::exposeHandler(e);
63 }
64
65 int OtkLabel::configureHandler(const XConfigureEvent &e)
66 {
67 if (!(e.width == width() && e.height == height()))
68 _dirty = true;
69 return OtkWidget::configureHandler(e);
70 }
71
72 }
This page took 0.03805 seconds and 5 git commands to generate.