]> icculus.org git repositories - mikachu/openbox.git/blob - otk/focuslabel.cc
allow for ignoring x errors.
[mikachu/openbox.git] / 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
33 void FocusLabel::renderForeground()
34 {
35   otk::Widget::renderForeground();
36
37   const Font *ft = style()->labelFont();
38   RenderColor *text_color = (isFocused() ? style()->textFocusColor()
39                              : style()->textUnfocusColor());
40   unsigned int sidemargin = style()->bevelWidth() * 2;
41
42   ustring t = _text; // the actual text to draw
43   int x = sidemargin;    // x coord for the text
44
45   // find a string that will fit inside the area for text
46   int max_length = width() - sidemargin * 2;
47   if (max_length <= 0) {
48     t = ""; // can't fit anything
49   } else {
50     size_t text_len = t.size();
51     int length;
52       
53     do {
54       t.resize(text_len);
55       length = ft->measureString(t);
56     } while (length > max_length && text_len-- > 0);
57
58     // justify the text
59     switch (style()->labelTextJustify()) {
60     case RenderStyle::RightJustify:
61       x += max_length - length;
62       break;
63     case RenderStyle::CenterJustify:
64       x += (max_length - length) / 2;
65       break;
66     case RenderStyle::LeftJustify:
67       break;
68     }
69   }
70
71   display->renderControl(_screen)->
72     drawString(*_surface, *ft, x, 0, *text_color, t);
73 }
74
75 }