]> Dogcows Code - chaz/openbox/blob - src/labelwidget.cc
correct a comment
[chaz/openbox] / 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 LabelWidget::LabelWidget(otk::Widget *parent, WidgetBase::WidgetType type)
14 : otk::Widget(parent),
15 WidgetBase(type)
16 {
17 const otk::ScreenInfo *info = otk::display->screenInfo(_screen);
18 _xftdraw = XftDrawCreate(**otk::display, _window, info->visual(),
19 info->colormap());
20 }
21
22
23 LabelWidget::~LabelWidget()
24 {
25 XftDrawDestroy(_xftdraw);
26 }
27
28
29 void LabelWidget::setText(const otk::ustring &text)
30 {
31 _text = text;
32 _dirty = true;
33 }
34
35
36 void LabelWidget::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 LabelWidget::setStyle(otk::Style *style)
49 {
50 otk::Widget::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 LabelWidget::focus()
60 {
61 otk::Widget::focus();
62 setTextures();
63 }
64
65
66 void LabelWidget::unfocus()
67 {
68 otk::Widget::unfocus();
69 setTextures();
70 }
71
72
73 void LabelWidget::update()
74 {
75 bool draw = _dirty;
76
77 otk::Widget::update();
78
79 if (draw) {
80 otk::ustring t = _text;
81 int x = _sidemargin; // x coord for the text
82
83 // find a string that will fit inside the area for text
84 int max_length = width() - _sidemargin * 2;
85 if (max_length <= 0) {
86 t = ""; // can't fit anything
87 } else {
88 size_t text_len = t.size();
89 int length;
90
91 do {
92 t.resize(text_len);
93 length = _font->measureString(t);
94 } while (length > max_length && text_len-- > 0);
95
96 // justify the text
97 switch (_justify) {
98 case otk::Style::RightJustify:
99 x += max_length - length;
100 break;
101 case otk::Style::CenterJustify:
102 x += (max_length - length) / 2;
103 break;
104 case otk::Style::LeftJustify:
105 break;
106 }
107 }
108
109 _font->drawString(_xftdraw, x, 0, *_text_color, t);
110 }
111 }
112
113
114 void LabelWidget::adjust()
115 {
116 // nothing to adjust. no children.
117 }
118
119 }
This page took 0.041944 seconds and 4 git commands to generate.