]> icculus.org git repositories - mikachu/openbox.git/blob - otk/label.cc
use otk objects in the ob scripts by importing otk
[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   otk::Widget::renderForeground();
31
32   const Font *ft = style()->labelFont();
33   unsigned int sidemargin = style()->bevelWidth() * 2;
34
35   ustring t = _text; // the actual text to draw
36   int x = sidemargin;    // x coord for the text
37
38   // find a string that will fit inside the area for text
39   int max_length = width() - sidemargin * 2;
40   if (max_length <= 0) {
41     t = ""; // can't fit anything
42   } else {
43     size_t text_len = t.size();
44     int length;
45       
46     do {
47       t.resize(text_len);
48       length = ft->measureString(t);
49     } while (length > max_length && text_len-- > 0);
50
51     // justify the text
52     switch (style()->labelTextJustify()) {
53     case RenderStyle::RightJustify:
54       x += max_length - length;
55       break;
56     case RenderStyle::CenterJustify:
57       x += (max_length - length) / 2;
58       break;
59     case RenderStyle::LeftJustify:
60       break;
61     }
62   }
63
64   display->renderControl(_screen)->
65     drawString(*_surface, *ft, x, 0, *style()->textUnfocusColor(), t);
66 }
67
68 }