]> icculus.org git repositories - mikachu/openbox.git/blob - otk/focuslabel.cc
provide RenderControls to all otk from the display class. initialize them all there...
[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 }
17
18 FocusLabel::~FocusLabel()
19 {
20 }
21
22
23 void FocusLabel::setStyle(Style *style)
24 {
25   FocusWidget::setStyle(style);
26   
27   setTexture(style->getLabelFocus());
28   setUnfocusTexture(style->getLabelUnfocus());
29 }
30
31
32 void FocusLabel::update(void)
33 {
34   if (_dirty) {
35     const Font *ft = style()->getFont();
36     Color *text_color = (isFocused() ? style()->getTextFocus()
37                           : style()->getTextUnfocus());
38     unsigned int sidemargin = style()->getBevelWidth() * 2;
39
40     ustring t = _text; // the actual text to draw
41     int x = sidemargin;    // x coord for the text
42
43     // find a string that will fit inside the area for text
44     int max_length = width() - sidemargin * 2;
45     if (max_length <= 0) {
46       t = ""; // can't fit anything
47     } else {
48       size_t text_len = t.size();
49       int length;
50       
51       do {
52         t.resize(text_len);
53         length = ft->measureString(t);
54       } while (length > max_length && text_len-- > 0);
55
56       // justify the text
57       switch (style()->textJustify()) {
58       case Style::RightJustify:
59         x += max_length - length;
60         break;
61       case Style::CenterJustify:
62         x += (max_length - length) / 2;
63         break;
64       case Style::LeftJustify:
65         break;
66       }
67     }
68
69     FocusWidget::update();
70
71     display->renderControl(_screen)->
72       drawString(this, *ft, x, 0, *text_color, t);
73   } else
74     FocusWidget::update();
75 }
76
77 }