]> icculus.org git repositories - dana/openbox.git/blob - otk/focuslabel.cc
labels use half the bevel width for text
[dana/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 OtkFocusLabel::OtkFocusLabel(OtkWidget *parent)
14   : OtkFocusWidget(parent), _text("")
15 {
16   const ScreenInfo *info = OBDisplay::screenInfo(getScreen());
17   _xftdraw = XftDrawCreate(OBDisplay::display, getWindow(), info->getVisual(),
18                            info->getColormap());
19   
20   setTexture(getStyle()->getLabelFocus());
21   setUnfocusTexture(getStyle()->getLabelUnfocus());
22 }
23
24 OtkFocusLabel::~OtkFocusLabel()
25 {
26   XftDrawDestroy(_xftdraw);
27 }
28
29 void OtkFocusLabel::update(void)
30 {
31   if (_dirty) {
32     const BFont &ft = getStyle()->getFont();
33     BColor *text_color = (isFocused() ? getStyle()->getTextFocus()
34                           : getStyle()->getTextUnfocus());
35     unsigned int bevel = getStyle()->getBevelWidth() / 2;
36
37     std::string t = _text; // the actual text to draw
38     int x = bevel;         // x coord for the text
39
40     // find a string that will fit inside the area for text
41     int max_length = width() - bevel * 2;
42     if (max_length <= 0) {
43       t = ""; // can't fit anything
44     } else {
45       size_t text_len = t.size();
46       int length;
47       
48       do {
49         t.resize(text_len);
50         length = ft.measureString(t);
51       } while (length > max_length && text_len-- > 0);
52
53       // justify the text
54       switch (getStyle()->textJustify()) {
55       case Style::RightJustify:
56         x += max_length - length;
57         break;
58       case Style::CenterJustify:
59         x += (max_length - length) / 2;
60         break;
61       case Style::LeftJustify:
62         break;
63       }
64     }
65
66     OtkFocusWidget::update();
67
68     ft.drawString(_xftdraw, x, bevel, *text_color, t);
69   } else
70     OtkFocusWidget::update();
71 }
72
73 }