]> icculus.org git repositories - mikachu/openbox.git/blob - src/labelwidget.cc
new render system in effect. now ot make it look right
[mikachu/openbox.git] / src / labelwidget.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 "otk/screeninfo.hh"
8 #include "otk/display.hh"
9 #include "labelwidget.hh"
10
11 namespace ob {
12
13 LabelWidget::LabelWidget(otk::Widget *parent, WidgetBase::WidgetType type)
14   : otk::Widget(parent),
15     WidgetBase(type)
16 {
17 }
18
19
20 LabelWidget::~LabelWidget()
21 {
22 }
23
24
25 void LabelWidget::setText(const otk::ustring &text)
26 {
27   _text = text;
28   _dirty = true;
29 }
30
31
32 void LabelWidget::setTextures()
33 {
34   if (_focused) {
35     setTexture(_style->labelFocusBackground());
36     _text_color = _style->textFocusColor();
37   } else {
38     setTexture(_style->labelUnfocusBackground());
39     _text_color = _style->textUnfocusColor();
40   }
41 }
42
43
44 void LabelWidget::setStyle(otk::RenderStyle *style)
45 {
46   otk::Widget::setStyle(style);
47   setTextures();
48   _font = style->labelFont();
49   _sidemargin = style->bevelWidth() * 2;
50   _justify = style->labelTextJustify();
51
52   assert(_font);
53 }
54
55
56 void LabelWidget::focus()
57 {
58   otk::Widget::focus();
59   setTextures();
60 }
61
62
63 void LabelWidget::unfocus()
64 {
65   otk::Widget::unfocus();
66   setTextures();
67 }
68
69
70 void LabelWidget::update()
71 {
72   printf("LabelWidget::update()\n");
73   otk::Widget::update();
74 }
75
76
77 void LabelWidget::renderForeground()
78 {
79   bool draw = _dirty;
80
81   printf("LabelWidget::renderForeground()\n");
82   otk::Widget::renderForeground();
83
84   if (draw) {
85     otk::ustring t = _text;
86     int x = _sidemargin;    // x coord for the text
87
88     // find a string that will fit inside the area for text
89     int max_length = width() - _sidemargin * 2;
90     if (max_length <= 0) {
91       t = ""; // can't fit anything
92     } else {
93       size_t text_len = t.size();
94       int length;
95       
96       do {
97         t.resize(text_len);
98         length = _font->measureString(t);
99       } while (length > max_length && text_len-- > 0);
100
101       // justify the text
102       switch (_justify) {
103       case otk::RenderStyle::RightJustify:
104         x += max_length - length;
105         break;
106       case otk::RenderStyle::CenterJustify:
107         x += (max_length - length) / 2;
108         break;
109       case otk::RenderStyle::LeftJustify:
110         break;
111       }
112     }
113
114     otk::display->renderControl(_screen)->drawString
115       (*_surface, *_font, x, 0, *_text_color, t);
116   }
117 }
118
119
120 void LabelWidget::adjust()
121 {
122   // nothing to adjust. no children.
123 }
124
125 }