]> Dogcows Code - chaz/openbox/blob - otk/focuslabel.cc
better historyplacement
[chaz/openbox] / otk / focuslabel.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 "focuslabel.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10
11 namespace otk {
12
13 FocusLabel::FocusLabel(Widget *parent)
14 : FocusWidget(parent), _text("")
15 {
16 setStyle(_style);
17 }
18
19 FocusLabel::~FocusLabel()
20 {
21 }
22
23
24 void FocusLabel::setStyle(RenderStyle *style)
25 {
26 FocusWidget::setStyle(style);
27
28 setTexture(style->labelFocusBackground());
29 setUnfocusTexture(style->labelUnfocusBackground());
30 }
31
32 void FocusLabel::fitString(const std::string &str)
33 {
34 const Font *ft = style()->labelFont();
35 fitSize(ft->measureString(str), ft->height());
36 }
37
38 void FocusLabel::fitSize(int w, int h)
39 {
40 unsigned int sidemargin = style()->bevelWidth() * 2;
41 resize(w + sidemargin * 2, h);
42 }
43
44 void FocusLabel::update()
45 {
46 if (_dirty) {
47 int w = _rect.width(), h = _rect.height();
48 const Font *ft = style()->labelFont();
49 unsigned int sidemargin = style()->bevelWidth() * 2;
50 if (!_fixed_width)
51 w = ft->measureString(_text) + sidemargin * 2;
52 if (!_fixed_height)
53 h = ft->height();
54 internalResize(w, h);
55 }
56 FocusWidget::update();
57 }
58
59
60 void FocusLabel::renderForeground()
61 {
62 FocusWidget::renderForeground();
63
64 const Font *ft = style()->labelFont();
65 RenderColor *text_color = (isFocused() ? style()->textFocusColor()
66 : style()->textUnfocusColor());
67 unsigned int sidemargin = style()->bevelWidth() * 2;
68
69 ustring t = _text; // the actual text to draw
70 int x = sidemargin; // x coord for the text
71
72 // find a string that will fit inside the area for text
73 int max_length = width() - sidemargin * 2;
74 if (max_length <= 0) {
75 t = ""; // can't fit anything
76 } else {
77 size_t text_len = t.size();
78 int length;
79
80 do {
81 t.resize(text_len);
82 length = ft->measureString(t);
83 } while (length > max_length && text_len-- > 0);
84
85 // justify the text
86 switch (style()->labelTextJustify()) {
87 case RenderStyle::RightJustify:
88 x += max_length - length;
89 break;
90 case RenderStyle::CenterJustify:
91 x += (max_length - length) / 2;
92 break;
93 case RenderStyle::LeftJustify:
94 break;
95 }
96 }
97
98 display->renderControl(_screen)->
99 drawString(*_surface, *ft, x, 0, *text_color, t);
100 }
101
102 }
This page took 0.039294 seconds and 4 git commands to generate.