]> icculus.org git repositories - mikachu/openbox.git/blob - otk/label.cc
maximizing!
[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 = style()->bevelWidth() * 2;
37   resize(w + sidemargin * 2, h);
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 = style()->bevelWidth() * 2;
46     if (!_fixed_width)
47       w = ft->measureString(_text) + sidemargin * 2;
48     if (!_fixed_height)
49       h = ft->height();
50     internalResize(w, h);
51   }
52   Widget::update();
53 }
54
55
56 void Label::renderForeground(void)
57 {
58   Widget::renderForeground();
59
60   const Font *ft = style()->labelFont();
61   unsigned int sidemargin = style()->bevelWidth() * 2;
62
63   ustring t = _text; // the actual text to draw
64   int x = sidemargin;    // x coord for the text
65
66   // find a string that will fit inside the area for text
67   int max_length = width() - sidemargin * 2;
68   if (max_length <= 0) {
69     t = ""; // can't fit anything
70   } else {
71     size_t text_len = t.size();
72     int length;
73       
74     do {
75       t.resize(text_len);
76       length = ft->measureString(t);
77     } while (length > max_length && text_len-- > 0);
78
79     // justify the text
80     switch (style()->labelTextJustify()) {
81     case RenderStyle::RightJustify:
82       x += max_length - length;
83       break;
84     case RenderStyle::CenterJustify:
85       x += (max_length - length) / 2;
86       break;
87     case RenderStyle::LeftJustify:
88       break;
89     }
90   }
91
92   display->renderControl(_screen)->
93     drawString(*_surface, *ft, x, 0, *style()->textUnfocusColor(), t);
94 }
95
96 }