]> icculus.org git repositories - mikachu/openbox.git/blob - src/labelwidget.cc
might not compile... ob uses its own widgets now, which subclass only the base otk...
[mikachu/openbox.git] / src / labelwidget.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "otk/screeninfo.hh"
8 #include "otk/display.hh"
9 #include "labelwidget.hh"
10
11 namespace ob {
12
13 OBLabelWidget::OBLabelWidget(otk::OtkWidget *parent, OBWidget::WidgetType type)
14   : otk::OtkWidget(parent),
15     OBWidget(type)
16 {
17   const otk::ScreenInfo *info = otk::OBDisplay::screenInfo(_screen);
18   _xftdraw = XftDrawCreate(otk::OBDisplay::display, _window, info->visual(),
19                            info->colormap());
20 }
21
22
23 OBLabelWidget::~OBLabelWidget()
24 {
25   XftDrawDestroy(_xftdraw);
26 }
27
28
29 void OBLabelWidget::setText(const std::string &text)
30 {
31   _text = text;
32   _dirty = true;
33 }
34
35
36 void OBLabelWidget::setTextures()
37 {
38   if (_focused) {
39     setTexture(_style->getLabelFocus());
40     _text_color = _style->getTextFocus();
41   } else {
42     setTexture(_style->getLabelUnfocus());
43     _text_color = _style->getTextUnfocus();
44   }
45 }
46
47
48 void OBLabelWidget::setStyle(otk::Style *style)
49 {
50   OtkWidget::setStyle(style);
51   setTextures();
52   _font = style->getFont();
53   assert(_font);
54   _sidemargin = style->getBevelWidth() * 2;
55   _justify = style->textJustify();
56 }
57
58
59 void OBLabelWidget::focus()
60 {
61   otk::OtkWidget::focus();
62   setTextures();
63 }
64
65
66 void OBLabelWidget::unfocus()
67 {
68   otk::OtkWidget::unfocus();
69   setTextures();
70 }
71
72
73 void OBLabelWidget::update()
74 {
75   if (_dirty) {
76     std::string t = _text;
77     int x = _sidemargin;    // x coord for the text
78
79     // find a string that will fit inside the area for text
80     int max_length = width() - _sidemargin * 2;
81     if (max_length <= 0) {
82       t = ""; // can't fit anything
83     } else {
84       size_t text_len = t.size();
85       int length;
86       
87       do {
88         t.resize(text_len);
89         length = _font->measureString(t);
90       } while (length > max_length && text_len-- > 0);
91
92       // justify the text
93       switch (_justify) {
94       case otk::Style::RightJustify:
95         x += max_length - length;
96         break;
97       case otk::Style::CenterJustify:
98         x += (max_length - length) / 2;
99         break;
100       case otk::Style::LeftJustify:
101         break;
102       }
103     }
104
105     OtkWidget::update();
106
107     _font->drawString(_xftdraw, x, 0, *_text_color, t);
108   } else
109     OtkWidget::update();
110 }
111
112
113 void OBLabelWidget::adjust()
114 {
115   // XXX: adjust shit
116 }
117
118 }