openbox/otk/focuslabel.cc

81 lines
1.8 KiB
C++
Raw Normal View History

// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
#ifdef HAVE_CONFIG_H
# include "../config.h"
#endif
2002-11-16 13:02:29 +00:00
#include "focuslabel.hh"
2002-12-03 08:54:51 +00:00
#include "display.hh"
#include "screeninfo.hh"
2002-11-16 13:02:29 +00:00
namespace otk {
FocusLabel::FocusLabel(Widget *parent)
: FocusWidget(parent), _text("")
2002-11-16 13:02:29 +00:00
{
const ScreenInfo *info = display->screenInfo(screen());
_xftdraw = XftDrawCreate(**display, window(), info->visual(),
2002-12-20 15:33:58 +00:00
info->colormap());
2002-11-16 13:02:29 +00:00
}
FocusLabel::~FocusLabel()
2002-11-16 13:02:29 +00:00
{
2002-12-03 08:54:51 +00:00
XftDrawDestroy(_xftdraw);
2002-11-16 13:02:29 +00:00
}
void FocusLabel::setStyle(Style *style)
{
FocusWidget::setStyle(style);
2002-12-20 15:26:34 +00:00
setTexture(style->getLabelFocus());
setUnfocusTexture(style->getLabelUnfocus());
}
void FocusLabel::update(void)
2002-11-16 13:02:29 +00:00
{
if (_dirty) {
const Font *ft = style()->getFont();
Color *text_color = (isFocused() ? style()->getTextFocus()
2002-12-20 15:26:34 +00:00
: style()->getTextUnfocus());
unsigned int sidemargin = style()->getBevelWidth() * 2;
2002-11-16 13:02:29 +00:00
ustring t = _text; // the actual text to draw
int x = sidemargin; // x coord for the text
2002-11-16 13:02:29 +00:00
// find a string that will fit inside the area for text
int max_length = width() - sidemargin * 2;
2002-11-16 13:02:29 +00:00
if (max_length <= 0) {
t = ""; // can't fit anything
} else {
size_t text_len = t.size();
int length;
do {
t.resize(text_len);
length = ft->measureString(t);
2002-11-16 13:02:29 +00:00
} while (length > max_length && text_len-- > 0);
// justify the text
2002-12-20 15:26:34 +00:00
switch (style()->textJustify()) {
2002-11-16 13:02:29 +00:00
case Style::RightJustify:
x += max_length - length;
break;
case Style::CenterJustify:
x += (max_length - length) / 2;
break;
case Style::LeftJustify:
break;
}
}
FocusWidget::update();
2002-11-16 13:02:29 +00:00
ft->drawString(_xftdraw, x, 0, *text_color, t);
2002-11-16 13:02:29 +00:00
} else
FocusWidget::update();
2002-11-16 13:02:29 +00:00
}
}