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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
/*
* The Mana Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2012 The Mana Developers
* Copyright (C) 2009 Aethyra Development Team
*
* This file is part of The Mana Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gui/widgets/browserbox.h"
#include "client.h"
#include "gui/gui.h"
#include "gui/truetypefont.h"
#include "gui/widgets/linkhandler.h"
#include "resources/itemdb.h"
#include "resources/iteminfo.h"
#include "resources/theme.h"
#include <guichan/graphics.hpp>
#include <guichan/font.hpp>
#include <guichan/cliprectangle.hpp>
#include <algorithm>
struct LayoutContext
{
LayoutContext(gcn::Font *font);
int y = 0;
gcn::Font *font;
const int fontHeight;
const int minusWidth;
const int tildeWidth;
int lineHeight;
gcn::Color selColor;
const gcn::Color textColor;
};
LayoutContext::LayoutContext(gcn::Font *font)
: font(font)
, fontHeight(font->getHeight())
, minusWidth(font->getWidth("-"))
, tildeWidth(font->getWidth("~"))
, lineHeight(fontHeight)
, selColor(Theme::getThemeColor(Theme::TEXT))
, textColor(Theme::getThemeColor(Theme::TEXT))
{
if (auto *trueTypeFont = dynamic_cast<const TrueTypeFont*>(font))
lineHeight = trueTypeFont->getLineHeight();
}
BrowserBox::BrowserBox(unsigned int mode):
mMode(mode)
{
setFocusable(true);
addMouseListener(this);
}
BrowserBox::~BrowserBox() = default;
void BrowserBox::addRow(const std::string &row)
{
TextRow &newRow = mTextRows.emplace_back();
// Use links and user defined colors
if (mUseLinksAndUserColors)
{
std::string tmp = row;
// Check for links in format "@@link|Caption@@"
auto idx1 = tmp.find("@@");
while (idx1 != std::string::npos)
{
const auto idx2 = tmp.find("|", idx1);
const auto idx3 = tmp.find("@@", idx2);
if (idx2 == std::string::npos || idx3 == std::string::npos)
break;
BrowserLink &link = newRow.links.emplace_back();
link.link = tmp.substr(idx1 + 2, idx2 - (idx1 + 2));
link.caption = tmp.substr(idx2 + 1, idx3 - (idx2 + 1));
if (link.caption.empty())
{
const int id = atoi(link.link.c_str());
if (id)
link.caption = itemDb->get(id).getName();
else
link.caption = link.link;
}
newRow.text += tmp.substr(0, idx1);
newRow.text += "##<" + link.caption;
tmp.erase(0, idx3 + 2);
if (!tmp.empty())
{
newRow.text += "##>";
}
idx1 = tmp.find("@@");
}
newRow.text += tmp;
}
// Don't use links and user defined colors
else
{
newRow.text = row;
}
// Layout the newly added row
LayoutContext context(getFont());
context.y = getHeight();
layoutTextRow(newRow, context);
// Auto size mode
if (mMode == AUTO_SIZE && newRow.width > getWidth())
setWidth(newRow.width);
// Discard older rows when a row limit has been set
// (this might invalidate the newRow reference)
int removedHeight = 0;
while (mMaxRows > 0 && mTextRows.size() > mMaxRows)
{
removedHeight += mTextRows.front().height;
mTextRows.pop_front();
}
if (removedHeight > 0)
{
for (auto &row : mTextRows)
{
for (auto &part : row.parts)
{
part.y -= removedHeight;
}
for (auto &link : row.links)
{
link.y1 -= removedHeight;
link.y2 -= removedHeight;
}
}
}
setHeight(context.y - removedHeight);
}
void BrowserBox::clearRows()
{
mTextRows.clear();
setSize(0, 0);
mHoveredLink.reset();
maybeRelayoutText();
}
void BrowserBox::mousePressed(gcn::MouseEvent &event)
{
if (!mLinkHandler)
return;
updateHoveredLink(event.getX(), event.getY());
if (mHoveredLink)
mLinkHandler->handleLink(mHoveredLink->link);
}
void BrowserBox::mouseMoved(gcn::MouseEvent &event)
{
updateHoveredLink(event.getX(), event.getY());
}
void BrowserBox::mouseExited(gcn::MouseEvent &event)
{
mHoveredLink.reset();
}
void BrowserBox::draw(gcn::Graphics *graphics)
{
const gcn::ClipRectangle &cr = graphics->getCurrentClipArea();
int yStart = cr.y - cr.yOffset;
int yEnd = yStart + cr.height;
if (yStart < 0)
yStart = 0;
if (getWidth() != mLastLayoutWidth)
maybeRelayoutText();
if (mHoveredLink)
{
auto &link = *mHoveredLink;
if (mHighlightMode & BACKGROUND)
{
graphics->setColor(Theme::getThemeColor(Theme::HIGHLIGHT));
graphics->fillRectangle(gcn::Rectangle(
link.x1,
link.y1,
link.x2 - link.x1,
link.y2 - link.y1
));
}
if (mHighlightMode & UNDERLINE)
{
graphics->setColor(Theme::getThemeColor(Theme::HYPERLINK));
graphics->drawLine(
link.x1,
link.y2,
link.x2,
link.y2);
}
}
for (const auto &row : mTextRows)
{
for (const auto &part : row.parts)
{
if (part.y + 50 < yStart)
continue;
if (part.y > yEnd)
return;
auto font = part.font;
// Handle text shadows
if (mShadows)
{
graphics->setColor(Theme::getThemeColor(Theme::SHADOW,
part.color.a / 2));
if (mOutline)
font->drawString(graphics, part.text, part.x + 2, part.y + 2);
else
font->drawString(graphics, part.text, part.x + 1, part.y + 1);
}
if (mOutline)
{
// Text outline
graphics->setColor(Theme::getThemeColor(Theme::OUTLINE,
part.color.a / 4));
font->drawString(graphics, part.text, part.x + 1, part.y);
font->drawString(graphics, part.text, part.x - 1, part.y);
font->drawString(graphics, part.text, part.x, part.y + 1);
font->drawString(graphics, part.text, part.x, part.y - 1);
}
// the main text
graphics->setColor(part.color);
font->drawString(graphics, part.text, part.x, part.y);
}
}
}
/**
* Relayouts all text rows and returns the new height of the BrowserBox.
*/
void BrowserBox::relayoutText()
{
LayoutContext context(getFont());
for (auto &row : mTextRows)
layoutTextRow(row, context);
mLastLayoutWidth = getWidth();
mLastLayoutTime = tick_time;
setHeight(context.y);
}
/**
* Layers out the given \a row of text starting at the given \a context position.
* @return the context position for the next row.
*/
void BrowserBox::layoutTextRow(TextRow &row, LayoutContext &context)
{
context.font = getFont(); // each line starts with normal font
const int startY = context.y;
row.parts.clear();
unsigned linkIndex = 0;
bool wrapped = false;
int x = 0;
// Check for separator lines
if (startsWith(row.text, "---"))
{
for (x = 0; x < getWidth(); x += context.minusWidth - 1)
{
row.parts.push_back(LinePart {
x,
context.y,
context.selColor,
"-",
context.font
});
}
context.y += row.height;
row.width = getWidth();
row.height = context.y - startY;
return;
}
gcn::Color prevColor = context.selColor;
// TODO: Check if we must take texture size limits into account here
// TODO: Check if some of the O(n) calls can be removed
for (std::string::size_type start = 0, end = std::string::npos;
start != std::string::npos;
start = end, end = std::string::npos)
{
// Wrapped line continuation shall be indented
if (wrapped)
{
context.y += context.lineHeight;
x = 15;
wrapped = false;
}
// "Tokenize" the string at control sequences
if (mUseLinksAndUserColors)
end = row.text.find("##", start + 1);
if (mUseLinksAndUserColors ||
(!mUseLinksAndUserColors && (start == 0)))
{
// Check for color or font change in format "##x", x = [<,>,B,p,0..9]
if (row.text.find("##", start) == start && row.text.size() > start + 2)
{
const char c = row.text.at(start + 2);
bool valid;
const gcn::Color &col = Theme::getThemeColor(c, valid);
if (c == '>')
{
context.selColor = prevColor;
}
else if (c == '<')
{
prevColor = context.selColor;
context.selColor = col;
}
else if (c == 'B')
{
context.font = boldFont;
}
else if (c == 'b')
{
context.font = getFont();
}
else if (valid)
{
context.selColor = col;
}
else switch (c)
{
case '1': context.selColor = RED; break;
case '2': context.selColor = GREEN; break;
case '3': context.selColor = BLUE; break;
case '4': context.selColor = ORANGE; break;
case '5': context.selColor = YELLOW; break;
case '6': context.selColor = PINK; break;
case '7': context.selColor = PURPLE; break;
case '8': context.selColor = GRAY; break;
case '9': context.selColor = BROWN; break;
case '0':
default:
context.selColor = context.textColor;
}
// Update the position of the links
if (c == '<' && linkIndex < row.links.size())
{
auto &link = row.links[linkIndex];
const int size = context.font->getWidth(link.caption) + 1;
link.x1 = x;
link.y1 = context.y;
link.x2 = link.x1 + size;
link.y2 = context.y + context.fontHeight - 1;
linkIndex++;
}
start += 3;
if (start == row.text.size())
break;
}
}
if (start >= row.text.length())
break;
std::string::size_type len =
end == std::string::npos ? end : end - start;
std::string part = row.text.substr(start, len);
int partWidth = context.font->getWidth(part);
// Auto wrap mode
if (mMode == AUTO_WRAP && getWidth() > 0
&& partWidth > 0
&& (x + partWidth + 10) > getWidth())
{
bool forced = false;
/* FIXME: This code layout makes it easy to crash remote
clients by talking garbage. Forged long utf-8 characters
will cause either a buffer underflow in substr or an
infinite loop in the main loop. */
do
{
if (!forced)
end = row.text.rfind(' ', end);
// Check if we have to (stupidly) force-wrap
if (end == std::string::npos || end <= start)
{
forced = true;
end = row.text.size();
x += context.tildeWidth; // Account for the wrap-notifier
continue;
}
// Skip to the start of the current character
while ((row.text[end] & 192) == 128)
end--;
end--; // And then to the last byte of the previous one
part = row.text.substr(start, end - start + 1);
partWidth = context.font->getWidth(part);
}
while (end > start && partWidth > 0
&& (x + partWidth + 10) > getWidth());
if (forced)
{
x -= context.tildeWidth; // Remove the wrap-notifier accounting
row.parts.push_back(LinePart {
getWidth() - context.tildeWidth,
context.y,
context.selColor,
"~",
getFont()
});
end++; // Skip to the next character
}
else
{
end += 2; // Skip to after the space
}
wrapped = true;
}
row.parts.push_back(LinePart {
x,
context.y,
context.selColor,
std::move(part),
context.font
});
row.width = std::max(row.width, x + partWidth);
if (mMode == AUTO_WRAP && partWidth == 0)
break;
x += partWidth;
}
context.y += context.lineHeight;
row.height = context.y - startY;
}
void BrowserBox::updateHoveredLink(int x, int y)
{
mHoveredLink.reset();
for (const auto &row : mTextRows)
{
for (const auto &link : row.links)
{
if (link.contains(x, y))
{
mHoveredLink = link;
return;
}
}
}
}
void BrowserBox::maybeRelayoutText()
{
// Reduce relayouting frequency when there is a lot of text
if (mTextRows.size() > 100)
if (mLastLayoutTime && std::abs(mLastLayoutTime - tick_time) < 10)
return;
relayoutText();
}
|