/
RichLabel.cpp
205 lines (178 loc) · 4.43 KB
/
RichLabel.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "RichLabel.h"
#include <vector>
#include <exception>
#include "Platform.h"
#include "gui/interface/Point.h"
#include "gui/interface/Component.h"
#include "graphics/Graphics.h"
#include "graphics/FontReader.h"
#include "Colour.h"
using namespace ui;
struct RichTextParseException: public std::exception {
ByteString message;
public:
RichTextParseException(String message = String("Parse error")): message(message.ToUtf8()) {}
const char * what() const throw() override
{
return message.c_str();
}
~RichTextParseException() throw() {};
};
RichLabel::RichLabel(Point position, Point size, String labelText):
Component(position, size),
textSource(labelText),
displayText("")
{
updateRichText();
}
RichLabel::~RichLabel()
{
}
void RichLabel::updateRichText()
{
regions.clear();
displayText = "";
if(textSource.length())
{
enum State { ReadText, ReadData, ReadRegion, ReadDataStart };
State state = ReadText;
int currentDataPos = 0;
String::value_type * currentData = new String::value_type[textSource.length()+1];
std::fill(currentData, currentData+textSource.length()+1, 0);
int finalTextPos = 0;
String::value_type * finalText = new String::value_type[textSource.length()+1];
std::fill(finalText, finalText+textSource.length()+1, 0);
int originalTextPos = 0;
String::value_type * originalText = new String::value_type[textSource.length()+1];
std::copy(textSource.begin(), textSource.end(), originalText);
originalText[textSource.length()] = 0;
int stackPos = -1;
RichTextRegion * regionsStack = new RichTextRegion[256];
try
{
while(originalText[originalTextPos])
{
char current = originalText[originalTextPos];
if(state == ReadText)
{
if(current == '{')
{
if(stackPos > 255)
throw RichTextParseException("Too many nested regions");
stackPos++;
regionsStack[stackPos].start = finalTextPos;
regionsStack[stackPos].finish = finalTextPos;
state = ReadRegion;
}
else if(current == '}')
{
if(stackPos >= 0)
{
currentData[currentDataPos] = 0;
regionsStack[stackPos].actionData = String(currentData);
regions.push_back(regionsStack[stackPos]);
stackPos--;
}
else
{
throw RichTextParseException("Unexpected '}'");
}
}
else
{
finalText[finalTextPos++] = current;
finalText[finalTextPos] = 0;
if(stackPos >= 0)
{
regionsStack[stackPos].finish = finalTextPos;
}
}
}
else if(state == ReadData)
{
if(current == '|')
{
state = ReadText;
}
else
{
currentData[currentDataPos++] = current;
currentData[currentDataPos] = 0;
}
}
else if(state == ReadDataStart)
{
if(current != ':')
{
throw RichTextParseException("Expected ':'");
}
state = ReadData;
currentDataPos = 0;
}
else if(state == ReadRegion)
{
if(stackPos >= 0)
{
regionsStack[stackPos].action = current;
state = ReadDataStart;
}
else
{
throw RichTextParseException();
}
}
originalTextPos++;
}
if(stackPos != -1)
throw RichTextParseException("Unclosed region");
finalText[finalTextPos] = 0;
displayText = String(finalText);
}
catch (const RichTextParseException & e)
{
displayText = "\br[Parse exception: " + ByteString(e.what()).FromUtf8() + "]";
regions.clear();
}
delete[] currentData;
delete[] finalText;
delete[] originalText;
delete[] regionsStack;
}
TextPosition(displayText);
displayTextWrapper.Update(displayText, false, 0);
}
void RichLabel::SetText(String text)
{
textSource = text;
updateRichText();
}
String RichLabel::GetDisplayText()
{
return displayText;
}
String RichLabel::GetText()
{
return textSource;
}
void RichLabel::Draw(const Point& screenPos)
{
Graphics * g = GetGraphics();
ui::Colour textColour = Appearance.TextInactive;
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, displayText, textColour.Red, textColour.Green, textColour.Blue, 255);
}
void RichLabel::OnMouseClick(int x, int y, unsigned button)
{
int cursorPosition = displayTextWrapper.Point2Index(x - textPosition.X, y - textPosition.Y).raw_index;
for (auto const ®ion : regions)
{
if (region.start <= cursorPosition && region.finish >= cursorPosition)
{
switch (region.action)
{
case 'a':
Platform::OpenURI(region.actionData.ToUtf8());
break;
}
}
}
}