all repos — openbox @ dd6f90684899e8e26e653af4b2e7e7eab1abf798

openbox fork - make it a bit more like ryudo

src/labelwidget.cc (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// -*- 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"
#include "labelwidget.hh"

namespace ob {

LabelWidget::LabelWidget(otk::Widget *parent, WidgetBase::WidgetType type)
  : otk::Widget(parent),
    WidgetBase(type)
{
}


LabelWidget::~LabelWidget()
{
}


void LabelWidget::setText(const otk::ustring &text)
{
  _text = text;
  _dirty = true;
}


void LabelWidget::setTextures()
{
  if (_focused) {
    setTexture(_style->labelFocusBackground());
    _text_color = _style->textFocusColor();
  } else {
    setTexture(_style->labelUnfocusBackground());
    _text_color = _style->textUnfocusColor();
  }
}


void LabelWidget::setStyle(otk::RenderStyle *style)
{
  otk::Widget::setStyle(style);
  setTextures();
  _font = style->labelFont();
  _sidemargin = style->bevelWidth() * 2;
  _justify = style->labelTextJustify();

  assert(_font);
}


void LabelWidget::focus()
{
  otk::Widget::focus();
  setTextures();
}


void LabelWidget::unfocus()
{
  otk::Widget::unfocus();
  setTextures();
}


void LabelWidget::update()
{
  printf("LabelWidget::update()\n");
}


void LabelWidget::renderForeground()
{
  bool draw = _dirty;

  otk::Widget::renderForeground();

  if (draw) {
    otk::ustring 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;
      
      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:
        x += max_length - length;
        break;
      case otk::RenderStyle::CenterJustify:
        x += (max_length - length) / 2;
        break;
      case otk::RenderStyle::LeftJustify:
        break;
      }
    }

    otk::display->renderControl(_screen)->drawString
      (*_surface, *_font, x, 0, *_text_color, t);
  }
}


void LabelWidget::adjust()
{
  // nothing to adjust. no children.
}

}