]> Dogcows Code - chaz/openbox/blob - src/labelwidget.cc
add an exit callback
[chaz/openbox] / 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::renderForeground()
71 {
72 bool draw = _dirty;
73
74 otk::Widget::renderForeground();
75
76 if (draw) {
77 otk::ustring t = _text;
78 int x = _sidemargin; // x coord for the text
79
80 // find a string that will fit inside the area for text
81 int max_length = width() - _sidemargin * 2;
82 if (max_length <= 0) {
83 t = ""; // can't fit anything
84 } else {
85 size_t text_len = t.size();
86 int length;
87
88 do {
89 t.resize(text_len);
90 length = _font->measureString(t);
91 } while (length > max_length && text_len-- > 0);
92
93 // justify the text
94 switch (_justify) {
95 case otk::RenderStyle::RightJustify:
96 x += max_length - length;
97 break;
98 case otk::RenderStyle::CenterJustify:
99 x += (max_length - length) / 2;
100 break;
101 case otk::RenderStyle::LeftJustify:
102 break;
103 }
104 }
105
106 otk::display->renderControl(_screen)->drawString
107 (*_surface, *_font, x, 0, *_text_color, t);
108 }
109 }
110
111
112 void LabelWidget::adjust()
113 {
114 // nothing to adjust. no children.
115 }
116
117 }
This page took 0.037478 seconds and 4 git commands to generate.