openbox/src/labelwidget.cc

118 lines
2.1 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 {
2003-01-11 19:42:43 +00:00
LabelWidget::LabelWidget(otk::Widget *parent, WidgetBase::WidgetType type)
: otk::Widget(parent),
WidgetBase(type)
2002-12-18 11:35:26 +00:00
{
}
2003-01-11 19:42:43 +00:00
LabelWidget::~LabelWidget()
2002-12-18 11:35:26 +00:00
{
}
2003-01-13 05:54:40 +00:00
void LabelWidget::setText(const otk::ustring &text)
{
_text = text;
_dirty = true;
}
2003-01-11 19:42:43 +00:00
void LabelWidget::setTextures()
{
if (_focused) {
setTexture(_style->labelFocusBackground());
_text_color = _style->textFocusColor();
} else {
setTexture(_style->labelUnfocusBackground());
_text_color = _style->textUnfocusColor();
}
2002-12-18 11:35:26 +00:00
}
void LabelWidget::setStyle(otk::RenderStyle *style)
2002-12-18 11:35:26 +00:00
{
2003-01-11 19:42:43 +00:00
otk::Widget::setStyle(style);
setTextures();
_font = style->labelFont();
_sidemargin = style->bevelWidth() * 2;
_justify = style->labelTextJustify();
assert(_font);
}
2002-12-18 11:35:26 +00:00
2003-01-11 19:42:43 +00:00
void LabelWidget::focus()
{
2003-01-11 19:42:43 +00:00
otk::Widget::focus();
setTextures();
2002-12-18 11:35:26 +00:00
}
2003-01-11 19:42:43 +00:00
void LabelWidget::unfocus()
2002-12-18 11:35:26 +00:00
{
2003-01-11 19:42:43 +00:00
otk::Widget::unfocus();
setTextures();
}
2002-12-18 11:35:26 +00:00
void LabelWidget::renderForeground()
{
2002-12-27 16:29:32 +00:00
bool draw = _dirty;
2002-12-27 10:07:57 +00:00
otk::Widget::renderForeground();
2002-12-27 10:07:57 +00:00
2002-12-27 16:29:32 +00:00
if (draw) {
2003-01-13 05:54:40 +00:00
otk::ustring t = _text;
2002-12-27 16:29:32 +00:00
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::RenderStyle::RightJustify:
2002-12-27 16:29:32 +00:00
x += max_length - length;
break;
case otk::RenderStyle::CenterJustify:
2002-12-27 16:29:32 +00:00
x += (max_length - length) / 2;
break;
case otk::RenderStyle::LeftJustify:
2002-12-27 16:29:32 +00:00
break;
}
}
otk::display->renderControl(_screen)->drawString
(*_surface, *_font, x, 0, *_text_color, t);
2002-12-27 16:29:32 +00:00
}
2002-12-18 11:35:26 +00:00
}
2003-01-11 19:42:43 +00:00
void LabelWidget::adjust()
{
// nothing to adjust. no children.
}
2002-12-18 11:35:26 +00:00
}