openbox/src/labelwidget.cc

120 lines
2.3 KiB
C++
Raw Normal View History

2002-12-18 11:35:26 +00:00
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
#ifdef HAVE_CONFIG_H
# include "../config.h"
#endif
#include "otk/screeninfo.hh"
#include "otk/display.hh"
2002-12-18 11:35:26 +00:00
#include "labelwidget.hh"
namespace ob {
OBLabelWidget::OBLabelWidget(otk::OtkWidget *parent, OBWidget::WidgetType type)
: otk::OtkWidget(parent),
2002-12-18 11:35:26 +00:00
OBWidget(type)
{
const otk::ScreenInfo *info = otk::OBDisplay::screenInfo(_screen);
_xftdraw = XftDrawCreate(otk::OBDisplay::display, _window, info->visual(),
info->colormap());
2002-12-18 11:35:26 +00:00
}
OBLabelWidget::~OBLabelWidget()
{
XftDrawDestroy(_xftdraw);
}
void OBLabelWidget::setText(const std::string &text)
{
_text = text;
_dirty = true;
}
void OBLabelWidget::setTextures()
{
if (_focused) {
setTexture(_style->getLabelFocus());
_text_color = _style->getTextFocus();
} else {
setTexture(_style->getLabelUnfocus());
_text_color = _style->getTextUnfocus();
}
2002-12-18 11:35:26 +00:00
}
void OBLabelWidget::setStyle(otk::Style *style)
{
OtkWidget::setStyle(style);
setTextures();
_font = style->getFont();
assert(_font);
_sidemargin = style->getBevelWidth() * 2;
_justify = style->textJustify();
}
2002-12-18 11:35:26 +00:00
void OBLabelWidget::focus()
{
otk::OtkWidget::focus();
setTextures();
2002-12-18 11:35:26 +00:00
}
void OBLabelWidget::unfocus()
2002-12-18 11:35:26 +00:00
{
otk::OtkWidget::unfocus();
setTextures();
}
2002-12-18 11:35:26 +00:00
void OBLabelWidget::update()
{
2002-12-27 16:29:32 +00:00
bool draw = _dirty;
2002-12-27 10:07:57 +00:00
2002-12-27 16:29:32 +00:00
OtkWidget::update();
2002-12-27 10:07:57 +00:00
2002-12-27 16:29:32 +00:00
if (draw) {
std::string t = _text;
int x = _sidemargin; // x coord for the text
// find a string that will fit inside the area for text
int max_length = width() - _sidemargin * 2;
if (max_length <= 0) {
t = ""; // can't fit anything
} else {
size_t text_len = t.size();
int length;
2002-12-27 16:29:32 +00:00
do {
t.resize(text_len);
length = _font->measureString(t);
} while (length > max_length && text_len-- > 0);
// justify the text
switch (_justify) {
case otk::Style::RightJustify:
x += max_length - length;
break;
case otk::Style::CenterJustify:
x += (max_length - length) / 2;
break;
case otk::Style::LeftJustify:
break;
}
}
2002-12-27 16:29:32 +00:00
_font->drawString(_xftdraw, x, 0, *_text_color, t);
}
2002-12-18 11:35:26 +00:00
}
void OBLabelWidget::adjust()
{
// nothing to adjust. no children.
}
2002-12-18 11:35:26 +00:00
}