File: richtext.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (272 lines) | stat: -rw-r--r-- 8,089 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 of the License, or
 * (at your option) 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 "common/system.h"
#include "common/unicode-bidi.h"

#include "graphics/macgui/mactext.h"

#include "gui/gui-manager.h"

#include "gui/ThemeEngine.h"
#include "gui/ThemeEval.h"

#include "gui/widgets/richtext.h"
#include "gui/widgets/scrollbar.h"

namespace GUI {

const Graphics::TTFMap ttfFamily[] = {
	{"NotoSans-Regular.ttf", Graphics::kMacFontRegular},
	{"NotoSans-Bold.ttf", Graphics::kMacFontBold},
	{"NotoSerif-Italic.ttf", Graphics::kMacFontItalic},
	{"NotoSerif-Bold-Italic.ttf", Graphics::kMacFontBold | Graphics::kMacFontItalic},
	{nullptr, 0}
};

RichTextWidget::RichTextWidget(GuiObject *boss, int x, int y, int w, int h, bool scale, const Common::U32String &text, const Common::U32String &tooltip)
	: Widget(boss, x, y, w, h, scale, tooltip), CommandSender(nullptr)  {

	_text = text;

	init();
}

RichTextWidget::RichTextWidget(GuiObject *boss, int x, int y, int w, int h, const Common::U32String &text, const Common::U32String &tooltip)
	: RichTextWidget(boss, x, y, w, h, false, text, tooltip) {
}

RichTextWidget::RichTextWidget(GuiObject *boss, const Common::String &name, const Common::U32String &text, const Common::U32String &tooltip)
	: Widget(boss, name, tooltip), CommandSender(nullptr)  {

	_text = text;

	init();
}

void RichTextWidget::init() {
	setFlags(WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_TRACK_MOUSE | WIDGET_DYN_TOOLTIP);

	_type = kRichTextWidget;

	_verticalScroll = new ScrollBarWidget(this, _w - 16, 0, 16, _h);
	_verticalScroll->setTarget(this);
	_scrolledX = 0;
	_scrolledY = 0;

	_innerMargin = g_gui.xmlEval()->getVar("Globals.RichTextWidget.InnerMargin", 0);
	_scrollbarWidth = g_gui.xmlEval()->getVar("Globals.Scrollbar.Width", 0);

	_textWidth = MAX(1, _w - _scrollbarWidth - 2 * _innerMargin);
	_textHeight = MAX(1, _h - 2 * _innerMargin);

	_limitH = 140;
}


RichTextWidget::~RichTextWidget() {
	delete _txtWnd;

	if (_surface)
		_surface->free();

	delete _surface;
}

void RichTextWidget::handleMouseWheel(int x, int y, int direction) {
	_verticalScroll->handleMouseWheel(x, y, direction);
}

void RichTextWidget::handleMouseDown(int x, int y, int button, int clickCount) {
	_mouseDownY = _mouseDownStartY = y;
}

void RichTextWidget::handleMouseUp(int x, int y, int button, int clickCount) {
	// Allow some tiny finger slipping
	if (ABS(_mouseDownY - _mouseDownStartY) > 5) {
		_mouseDownY = _mouseDownStartY = 0;

		return;
	}

	_mouseDownY = _mouseDownStartY = 0;

	Common::String link = _txtWnd->getMouseLink(x - _innerMargin + _scrolledX, y - _innerMargin + _scrolledY).encode();

	if (link.hasPrefixIgnoreCase("http"))
		g_system->openUrl(link);
}

void RichTextWidget::handleMouseMoved(int x, int y, int button) {
	if (_mouseDownStartY == 0 || _mouseDownY == y)
		return;

	int h = _txtWnd->getTextHeight();
	int prevScrolledY = _scrolledY;

	_scrolledY = CLIP(_scrolledY - (y - _mouseDownY), 0, h);

	_mouseDownY = y;

	if (_scrolledY == prevScrolledY)
		return;

	recalc();
	_verticalScroll->recalc();
	markAsDirty();
}

void RichTextWidget::handleTooltipUpdate(int x, int y) {
	_tooltip = _txtWnd->getMouseLink(x - _innerMargin + _scrolledX, y - _innerMargin + _scrolledY);
}

void RichTextWidget::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
	Widget::handleCommand(sender, cmd, data);
	switch (cmd) {
	case kSetPositionCmd:
		_scrolledY = _verticalScroll->_currentPos;
		reflowLayout();
		g_gui.scheduleTopDialogRedraw();
		break;
	default:
		break;
	}
}

void RichTextWidget::recalc() {
	_innerMargin = g_gui.xmlEval()->getVar("Globals.RichTextWidget.InnerMargin", 0);
	_scrollbarWidth = g_gui.xmlEval()->getVar("Globals.Scrollbar.Width", 0);
	_textWidth = MAX(1, _w - _scrollbarWidth - 2 * _innerMargin);
	_textHeight = MAX(1, _h - 2 * _innerMargin);
	_limitH = _textHeight;

	// Workaround: Currently Graphics::MacText::setMaxWidth does not work well.
	// There is a known limitation that the size is skipped when the text contains table,
	// and there is also an issue with the font.
	// So for now we recreate the widget.
//	if (_txtWnd) {
//		_txtWnd->setMaxWidth(_textWidth);
//		if (_surface->w != _textWidth || _surface->h != _textHeight)
//			_surface->create(_textWidth, _textHeight, g_gui.getWM()->_pixelformat);
//	} else {
//		createWidget();
//	}
	if (!_surface || _surface->w != _textWidth) {
		delete _txtWnd;
		createWidget();
	} else if (_surface->h != _textHeight)
		_surface->create(_textWidth, _textHeight, g_gui.getWM()->_pixelformat);

	int h = _txtWnd->getTextHeight();

	if (h <= _limitH) _scrolledY = 0;
	if (_scrolledY > h - _limitH) _scrolledY = MAX(0, h - _limitH);

	_verticalScroll->_numEntries = h;
	_verticalScroll->_currentPos = _scrolledY;
	_verticalScroll->_entriesPerPage = _limitH;
	_verticalScroll->_singleStep = _h / 4;
	_verticalScroll->setPos(_w - _scrollbarWidth, 0);
	_verticalScroll->setSize(_scrollbarWidth, _h - 1);
}

void RichTextWidget::createWidget() {
	Graphics::MacWindowManager *wm = g_gui.getWM();

	uint32 bg = wm->_pixelformat.ARGBToColor(0, 0xff, 0xff, 0xff); // transparent
	TextColorData *normal = g_gui.theme()->getTextColorData(kTextColorNormal);
	uint32 fg = wm->_pixelformat.RGBToColor(normal->r, normal->g, normal->b);

	const int fontHeight = g_gui.xmlEval()->getVar("Globals.Font.Height", 25);

#if 1
	Graphics::MacFont macFont(Graphics::kMacFontNewYork, fontHeight, Graphics::kMacFontRegular);
	(void)ttfFamily;
#else
	int newId = wm->_fontMan->registerTTFFont(ttfFamily);
	Graphics::MacFont macFont(newId, fontHeight, Graphics::kMacFontRegular);
#endif

	_txtWnd = new Graphics::MacText(Common::U32String(), wm, &macFont, fg, bg, _textWidth, Graphics::kTextAlignLeft);

	if (!_imageArchive.empty())
		_txtWnd->setImageArchive(_imageArchive);

	_txtWnd->setMarkdownText(_text);

	if (_surface)
		_surface->create(_textWidth, _textHeight, g_gui.getWM()->_pixelformat);
	else
		_surface = new Graphics::ManagedSurface(_textWidth, _textHeight, wm->_pixelformat);
}

void RichTextWidget::reflowLayout() {
	Widget::reflowLayout();

	recalc();

	_verticalScroll->setVisible(_verticalScroll->_numEntries > _limitH); //show when there is something to scroll
	_verticalScroll->recalc();
}

void RichTextWidget::drawWidget() {
	if (!_txtWnd)
		recalc();

	g_gui.theme()->drawWidgetBackground(Common::Rect(_x, _y, _x + _w, _y + _h), ThemeEngine::kWidgetBackgroundPlain);

	_surface->clear(g_gui.getWM()->_pixelformat.ARGBToColor(0, 0xff, 0xff, 0xff)); // transparent

	_txtWnd->draw(_surface, 0, _scrolledY, _textWidth, _textHeight, 0, 0);

	g_gui.theme()->drawManagedSurface(Common::Point(_x + _innerMargin, _y + _innerMargin), *_surface, Graphics::ALPHA_FULL);
}

void RichTextWidget::draw() {
	Widget::draw();

	if (_verticalScroll->isVisible()) {
		_verticalScroll->draw();
	}
}

void RichTextWidget::markAsDirty() {
	Widget::markAsDirty();

	if (_verticalScroll->isVisible()) {
		_verticalScroll->markAsDirty();
	}
}

bool RichTextWidget::containsWidget(Widget *w) const {
	if (w == _verticalScroll || _verticalScroll->containsWidget(w))
		return true;
	return false;
}

Widget *RichTextWidget::findWidget(int x, int y) {
	if (_verticalScroll->isVisible() && x >= _w - _scrollbarWidth)
		return _verticalScroll;

	return this;
}

} // End of namespace GUI