openbox/otk/focuslabel.cc

111 lines
2.5 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
{
2003-01-28 00:30:57 +00:00
setStyle(_style);
2002-11-16 13:02:29 +00:00
}
FocusLabel::~FocusLabel()
2002-11-16 13:02:29 +00:00
{
}
2003-01-22 22:46:16 +00:00
void FocusLabel::setStyle(RenderStyle *style)
{
FocusWidget::setStyle(style);
2003-01-20 06:11:12 +00:00
2003-01-22 22:46:16 +00:00
setTexture(style->labelFocusBackground());
setUnfocusTexture(style->labelUnfocusBackground());
}
2003-01-30 21:11:04 +00:00
void FocusLabel::fitString(const std::string &str)
{
const Font *ft = style()->labelFont();
fitSize(ft->measureString(str), ft->height());
}
void FocusLabel::fitSize(int w, int h)
{
2003-02-05 07:37:48 +00:00
unsigned int sidemargin = _bevel_width * 2;
2003-02-05 07:40:56 +00:00
resize(w + sidemargin * 2, h + _bevel_width * 2);
2003-01-30 21:11:04 +00:00
}
void FocusLabel::update()
{
if (_dirty) {
int w = _rect.width(), h = _rect.height();
const Font *ft = style()->labelFont();
2003-02-05 07:37:48 +00:00
unsigned int sidemargin = _bevel_width * 2;
2003-01-30 21:11:04 +00:00
if (!_fixed_width)
w = ft->measureString(_text) + sidemargin * 2;
if (!_fixed_height)
h = ft->height();
2003-02-04 15:07:39 +00:00
// enforce a minimum size
if (w > _rect.width()) {
if (h > _rect.height())
internalResize(w, h);
else
internalResize(w, _rect.height());
2003-02-05 05:48:01 +00:00
} else if (h > _rect.height())
2003-02-04 15:07:39 +00:00
internalResize(_rect.width(), h);
2003-01-30 21:11:04 +00:00
}
FocusWidget::update();
}
2003-01-22 23:17:28 +00:00
void FocusLabel::renderForeground()
2002-11-16 13:02:29 +00:00
{
2003-01-30 21:11:04 +00:00
FocusWidget::renderForeground();
2003-01-23 00:08:50 +00:00
2003-01-22 22:46:16 +00:00
const Font *ft = style()->labelFont();
RenderColor *text_color = (isFocused() ? style()->textFocusColor()
: style()->textUnfocusColor());
2003-02-05 07:37:48 +00:00
unsigned int sidemargin = _bevel_width * 2;
2002-11-16 13:02:29 +00:00
2003-01-20 06:11:12 +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
2003-01-20 06:11:12 +00:00
// 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-11-16 13:02:29 +00:00
2003-01-20 06:11:12 +00:00
do {
t.resize(text_len);
length = ft->measureString(t);
} while (length > max_length && text_len-- > 0);
2002-11-16 13:02:29 +00:00
2003-01-20 06:11:12 +00:00
// justify the text
2003-01-22 22:46:16 +00:00
switch (style()->labelTextJustify()) {
case RenderStyle::RightJustify:
2003-01-20 06:11:12 +00:00
x += max_length - length;
break;
2003-01-22 22:46:16 +00:00
case RenderStyle::CenterJustify:
2003-01-20 06:11:12 +00:00
x += (max_length - length) / 2;
break;
2003-01-22 22:46:16 +00:00
case RenderStyle::LeftJustify:
2003-01-20 06:11:12 +00:00
break;
2002-11-16 13:02:29 +00:00
}
2003-01-20 06:11:12 +00:00
}
2002-11-16 13:02:29 +00:00
2003-01-20 06:11:12 +00:00
display->renderControl(_screen)->
2003-02-05 07:40:56 +00:00
drawString(*_surface, *ft, x, _bevel_width, *text_color, t);
2002-11-16 13:02:29 +00:00
}
}