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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include "textedit.h"
#include "painter.h"
namespace GUI
{
TextEdit::TextEdit(Widget* parent) : Widget(parent), scroll(this)
{
setReadOnly(true);
scroll.move(width() - 2*x_border - 3, y_border - 1);
scroll.resize(16, 100);
CONNECT(&scroll, valueChangeNotifier, this, &TextEdit::scrolled);
}
TextEdit::~TextEdit()
{
}
void TextEdit::resize(std::size_t width, std::size_t height)
{
Widget::resize(width, height);
needs_preprocessing = true;
scroll.move(width - 2*x_border - 3, y_border - 1);
scroll.resize(scroll.width(), std::max((int)height - 2*(y_border - 1), 0));
}
void TextEdit::setReadOnly(bool readonly)
{
this->readonly = readonly;
}
bool TextEdit::readOnly()
{
return readonly;
}
void TextEdit::setText(const std::string& text)
{
this->text = text;
needs_preprocessing = true;
redraw();
textChangedNotifier();
}
std::string TextEdit::getText()
{
return text;
}
void TextEdit::preprocessText()
{
std::vector<std::string> lines;
preprocessed_text.clear();
std::string text = this->text;
for(std::size_t i = 0; i < text.length(); ++i)
{
char ch = text.at(i);
if(ch == '\t')
{
text.erase(i, 1);
text.insert(i, 4, ' ');
}
}
for(std::size_t i = 0; i < text.length(); ++i)
{
char ch = text.at(i);
if(ch == '\r')
{
text.erase(i, 1);
}
}
std::size_t pos = 0;
do
{
pos = text.find("\n");
lines.push_back(text.substr(0, pos));
text = text.substr(pos + 1);
} while(pos != std::string::npos);
for(auto it = lines.begin(); it != lines.end(); ++it)
{
auto line = *it;
for(std::size_t i = 0; i < line.length(); ++i)
{
auto linewidth = font.textWidth(line.substr(0, i));
if(linewidth >= width() - 2*x_border - 10 - scroll.width())
{
preprocessed_text.push_back(line.substr(0, i));
line = line.substr(i);
i = 0;
}
}
preprocessed_text.push_back(line);
}
}
void TextEdit::repaintEvent(RepaintEvent* repaintEvent)
{
if(needs_preprocessing)
{
preprocessText();
}
Painter p(*this);
scroll.setRange(height() / font.textHeight());
scroll.setMaximum(preprocessed_text.size());
if((width() == 0) || (height() == 0))
{
return;
}
box.setSize(width(), height());
p.drawImage(0, 0, box);
p.setColour(Colour(183.0f/255.0f, 219.0f/255.0f, 255.0f/255.0f, 1.0f));
int ypos = font.textHeight() + y_border;
auto scroll_value = scroll.value();
for(std::size_t i = 0; i < preprocessed_text.size() - scroll_value; ++i)
{
if(i * font.textHeight() >= (height() - y_border - font.textHeight()))
{
break;
}
auto const& line = preprocessed_text[scroll_value + i];
p.drawText(x_border, ypos, font, line);
ypos += font.textHeight();
}
}
void TextEdit::scrollEvent(ScrollEvent* scrollEvent)
{
scroll.setValue(scroll.value() + scrollEvent->delta);
}
void TextEdit::scrolled(int value)
{
(void)value;
redraw();
}
}
|