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