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