X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;f=src%2Flabelwidget.cc;h=445ae260c0efc2c3816c2f47a7ab1dce013ce4f0;hb=ecfac5f20c72647b4865a14ccffc307c2b116319;hp=9b18c0a97ec1c55499f39f4ef2cc4eb943a8a99f;hpb=83727b8e6404f7e00e7369c39da8779dfb85ae09;p=chaz%2Fopenbox diff --git a/src/labelwidget.cc b/src/labelwidget.cc index 9b18c0a9..445ae260 100644 --- a/src/labelwidget.cc +++ b/src/labelwidget.cc @@ -4,37 +4,116 @@ # include "../config.h" #endif +#include "otk/screeninfo.hh" +#include "otk/display.hh" #include "labelwidget.hh" namespace ob { -OBLabelWidget::OBLabelWidget(otk::OtkWidget *parent, OBWidget::WidgetType type) - : otk::OtkFocusLabel(parent), - OBWidget(type) +LabelWidget::LabelWidget(otk::Widget *parent, WidgetBase::WidgetType type) + : otk::Widget(parent), + WidgetBase(type) { + const otk::ScreenInfo *info = otk::Display::screenInfo(_screen); + _xftdraw = XftDrawCreate(otk::Display::display, _window, info->visual(), + info->colormap()); } -OBLabelWidget::~OBLabelWidget() +LabelWidget::~LabelWidget() { + XftDrawDestroy(_xftdraw); } -void OBLabelWidget::setStyle(otk::Style *style) +void LabelWidget::setText(const std::string &text) { - setTexture(style->getLabelFocus()); - setUnfocusTexture(style->getLabelUnfocus()); + _text = text; + _dirty = true; +} + + +void LabelWidget::setTextures() +{ + if (_focused) { + setTexture(_style->getLabelFocus()); + _text_color = _style->getTextFocus(); + } else { + setTexture(_style->getLabelUnfocus()); + _text_color = _style->getTextUnfocus(); + } +} + + +void LabelWidget::setStyle(otk::Style *style) +{ + otk::Widget::setStyle(style); + setTextures(); + _font = style->getFont(); + assert(_font); + _sidemargin = style->getBevelWidth() * 2; + _justify = style->textJustify(); +} + + +void LabelWidget::focus() +{ + otk::Widget::focus(); + setTextures(); +} + - otk::OtkFocusLabel::setStyle(style); +void LabelWidget::unfocus() +{ + otk::Widget::unfocus(); + setTextures(); } -void OBLabelWidget::adjust() +void LabelWidget::update() { - otk::OtkFocusLabel::adjust(); + bool draw = _dirty; + + otk::Widget::update(); + + if (draw) { + std::string t = _text; + int x = _sidemargin; // x coord for the text - // XXX: adjust shit + // 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; + + 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; + } + } + + _font->drawString(_xftdraw, x, 0, *_text_color, t); + } } +void LabelWidget::adjust() +{ + // nothing to adjust. no children. +} + }