]> icculus.org git repositories - mikachu/openbox.git/blob - otk/label.cc
provide pkg-config info for libotk
[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   setStyle(_style);
15 }
16
17 Label::~Label()
18 {
19 }
20
21 void Label::setStyle(RenderStyle *style)
22 {
23   Widget::setStyle(style);
24
25   setTexture(style->labelUnfocusBackground());
26 }
27
28 void Label::fitString(const std::string &str)
29 {
30   const Font *ft = style()->labelFont();
31   fitSize(ft->measureString(str), ft->height());
32 }
33
34 void Label::fitSize(int w, int h)
35 {
36   unsigned int sidemargin = _bevel_width * 2;
37   resize(w + sidemargin * 2, h + _bevel_width * 2);
38 }
39
40 void Label::update()
41 {
42   if (_dirty) {
43     int w = _rect.width(), h = _rect.height();
44     const Font *ft = style()->labelFont();
45     unsigned int sidemargin = _bevel_width * 2;
46     if (!_fixed_width)
47       w = ft->measureString(_text) + sidemargin * 2;
48     if (!_fixed_height)
49       h = ft->height();
50
51     // enforce a minimum size
52     if (w > _rect.width()) {
53       if (h > _rect.height())
54         internalResize(w, h);
55       else
56         internalResize(w, _rect.height());
57     } else if (h > _rect.height())
58       internalResize(_rect.width(), h);
59   }
60   Widget::update();
61 }
62
63
64 void Label::renderForeground(void)
65 {
66   Widget::renderForeground();
67
68   const Font *ft = style()->labelFont();
69   unsigned int sidemargin = _bevel_width * 2;
70
71   ustring t = _text; // the actual text to draw
72   int x = sidemargin;    // x coord for the text
73
74   // find a string that will fit inside the area for text
75   int max_length = width() - sidemargin * 2;
76   if (max_length <= 0) {
77     t = ""; // can't fit anything
78   } else {
79     size_t text_len = t.size();
80     int length;
81       
82     do {
83       t.resize(text_len);
84       length = ft->measureString(t);
85     } while (length > max_length && text_len-- > 0);
86
87     // justify the text
88     switch (style()->labelTextJustify()) {
89     case RenderStyle::RightJustify:
90       x += max_length - length;
91       break;
92     case RenderStyle::CenterJustify:
93       x += (max_length - length) / 2;
94       break;
95     case RenderStyle::LeftJustify:
96       break;
97     }
98   }
99
100   display->renderControl(_screen)->
101     drawString(*_surface, *ft, x, _bevel_width, *style()->textUnfocusColor(), t);
102 }
103
104 }