]> icculus.org git repositories - mikachu/openbox.git/blob - otk/label.cc
save/load the pixel in the cache
[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 Label::Label(Widget *parent)
12   : Widget(parent), _text("")
13 {
14 }
15
16 Label::~Label()
17 {
18 }
19
20 void Label::setStyle(RenderStyle *style)
21 {
22   Widget::setStyle(style);
23
24   setTexture(style->labelUnfocusBackground());
25 }
26
27
28 void Label::renderForeground(void)
29 {
30   const Font *ft = style()->labelFont();
31   unsigned int sidemargin = style()->bevelWidth() * 2;
32
33   ustring t = _text; // the actual text to draw
34   int x = sidemargin;    // x coord for the text
35
36   // find a string that will fit inside the area for text
37   int max_length = width() - sidemargin * 2;
38   if (max_length <= 0) {
39     t = ""; // can't fit anything
40   } else {
41     size_t text_len = t.size();
42     int length;
43       
44     do {
45       t.resize(text_len);
46       length = ft->measureString(t);
47     } while (length > max_length && text_len-- > 0);
48
49     // justify the text
50     switch (style()->labelTextJustify()) {
51     case RenderStyle::RightJustify:
52       x += max_length - length;
53       break;
54     case RenderStyle::CenterJustify:
55       x += (max_length - length) / 2;
56       break;
57     case RenderStyle::LeftJustify:
58       break;
59     }
60   }
61
62   display->renderControl(_screen)->
63     drawString(*_surface, *ft, x, 0, *style()->textUnfocusColor(), t);
64 }
65
66 }