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