1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
7 #include "otk/screeninfo.hh"
8 #include "otk/display.hh"
9 #include "labelwidget.hh"
13 LabelWidget::LabelWidget(otk::Widget *parent, WidgetBase::WidgetType type)
14 : otk::Widget(parent),
17 const otk::ScreenInfo *info = otk::display->screenInfo(_screen);
18 _xftdraw = XftDrawCreate(**otk::display, _window, info->visual(),
23 LabelWidget::~LabelWidget()
25 XftDrawDestroy(_xftdraw);
29 void LabelWidget::setText(const otk::ustring &text)
36 void LabelWidget::setTextures()
39 setTexture(_style->getLabelFocus());
40 _text_color = _style->getTextFocus();
42 setTexture(_style->getLabelUnfocus());
43 _text_color = _style->getTextUnfocus();
48 void LabelWidget::setStyle(otk::Style *style)
50 otk::Widget::setStyle(style);
52 _font = style->getFont();
54 _sidemargin = style->getBevelWidth() * 2;
55 _justify = style->textJustify();
59 void LabelWidget::focus()
66 void LabelWidget::unfocus()
68 otk::Widget::unfocus();
73 void LabelWidget::update()
77 otk::Widget::update();
80 otk::ustring t = _text;
81 int x = _sidemargin; // x coord for the text
83 // find a string that will fit inside the area for text
84 int max_length = width() - _sidemargin * 2;
85 if (max_length <= 0) {
86 t = ""; // can't fit anything
88 size_t text_len = t.size();
93 length = _font->measureString(t);
94 } while (length > max_length && text_len-- > 0);
98 case otk::Style::RightJustify:
99 x += max_length - length;
101 case otk::Style::CenterJustify:
102 x += (max_length - length) / 2;
104 case otk::Style::LeftJustify:
109 _font->drawString(_xftdraw, x, 0, *_text_color, t);
114 void LabelWidget::adjust()
116 // nothing to adjust. no children.