]> icculus.org git repositories - mikachu/openbox.git/blob - otk/label.cc
no space on teh top or bottom of text..
[mikachu/openbox.git] / otk / 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 OtkLabel::OtkLabel(OtkWidget *parent)
12   : OtkWidget(parent), _text("")
13 {
14   const ScreenInfo *info = OBDisplay::screenInfo(getScreen());
15   _xftdraw = XftDrawCreate(OBDisplay::display, getWindow(), info->getVisual(),
16                            info->getColormap());
17   
18   setStyle(getStyle());
19 }
20
21 OtkLabel::~OtkLabel()
22 {
23   XftDrawDestroy(_xftdraw);
24 }
25
26 void OtkLabel::setStyle(Style *style)
27 {
28   OtkWidget::setStyle(style);
29
30   setTexture(getStyle()->getLabelUnfocus());
31 }
32
33
34 void OtkLabel::update(void)
35 {
36   if (_dirty) {
37     const BFont &ft = getStyle()->getFont();
38     unsigned int sidemargin = getStyle()->getBevelWidth() * 2;
39
40     std::string t = _text; // the actual text to draw
41     int x = sidemargin;    // x coord for the text
42
43     // find a string that will fit inside the area for text
44     int max_length = width() - sidemargin * 2;
45     if (max_length <= 0) {
46       t = ""; // can't fit anything
47     } else {
48       size_t text_len = t.size();
49       int length;
50       
51       do {
52         t.resize(text_len);
53         length = ft.measureString(t);
54       } while (length > max_length && text_len-- > 0);
55
56       // justify the text
57       switch (getStyle()->textJustify()) {
58       case Style::RightJustify:
59         x += max_length - length;
60         break;
61       case Style::CenterJustify:
62         x += (max_length - length) / 2;
63         break;
64       case Style::LeftJustify:
65         break;
66       }
67     }
68
69     OtkWidget::update();
70
71     ft.drawString(_xftdraw, x, 0, *getStyle()->getTextUnfocus(), t);
72   } else
73     OtkWidget::update();
74 }
75
76 }