]> Dogcows Code - chaz/openbox/blob - label.cc
cacbb08e18f8df6c2ae1293566c502796c6a94e4
[chaz/openbox] / label.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 "label.hh"
8
9 namespace otk {
10
11 Label::Label(Widget *parent)
12 : Widget(parent), _text("")
13 {
14 const ScreenInfo *info = Display::screenInfo(screen());
15 _xftdraw = XftDrawCreate(Display::display, window(), info->visual(),
16 info->colormap());
17 }
18
19 Label::~Label()
20 {
21 XftDrawDestroy(_xftdraw);
22 }
23
24 void Label::setStyle(Style *style)
25 {
26 Widget::setStyle(style);
27
28 setTexture(style->getLabelUnfocus());
29 }
30
31
32 void Label::update(void)
33 {
34 if (_dirty) {
35 const Font *ft = style()->getFont();
36 unsigned int sidemargin = style()->getBevelWidth() * 2;
37
38 userstring t = _text; // the actual text to draw
39 int x = sidemargin; // x coord for the text
40
41 // find a string that will fit inside the area for text
42 int max_length = width() - sidemargin * 2;
43 if (max_length <= 0) {
44 t = ""; // can't fit anything
45 } else {
46 size_t text_len = t.size();
47 int length;
48
49 do {
50 t.resize(text_len);
51 length = ft->measureString(t);
52 } while (length > max_length && text_len-- > 0);
53
54 // justify the text
55 switch (style()->textJustify()) {
56 case Style::RightJustify:
57 x += max_length - length;
58 break;
59 case Style::CenterJustify:
60 x += (max_length - length) / 2;
61 break;
62 case Style::LeftJustify:
63 break;
64 }
65 }
66
67 Widget::update();
68
69 ft->drawString(_xftdraw, x, 0, *style()->getTextUnfocus(), t);
70 } else
71 Widget::update();
72 }
73
74 }
This page took 0.039836 seconds and 3 git commands to generate.