File: textviewer.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 (240 lines) | stat: -rw-r--r-- 6,350 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
/* 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/file.h"
#include "common/tokenizer.h"
#include "common/translation.h"

#include "graphics/font.h"
#include "graphics/fontman.h"

#include "gui/gui-manager.h"
#include "gui/ThemeEval.h"
#include "gui/textviewer.h"
#include "gui/widgets/scrollbar.h"

namespace GUI {

#define kDialogWidthPercent 0.8f
#define kDialogHeightPercent 0.8f

#define kPadX 0.05
#define kPadY 0.05

TextViewerDialog::TextViewerDialog(const Common::Path &fname)
	: Dialog(0, 0, 1, 1), _fname(fname) {
	_font = &g_gui.getFont(ThemeEngine::kFontStyleConsole);
	_charWidth = _font->getMaxCharWidth();
	_lineHeight = _font->getFontHeight() + 2;

	// Add scrollbar
	_scrollBar = new ScrollBarWidget(this, 0, 0, 1, 1);
	_scrollBar->setTarget(this);

	// I18N: Close dialog button
	_closeButton = new ButtonWidget(this, 0, 0, 1, 1, _("Close"), Common::U32String(), kCloseCmd);

	_currentPos = 0;
	_scrollLine = _linesPerPage - 1;

	reflowLayout();

	loadFile(fname);
}

TextViewerDialog::~TextViewerDialog() {
	destroy();
}

bool TextViewerDialog::loadFile(const Common::Path &fname) {
	Common::FSNode file(fname);

	if (!file.exists()) {
		warning("TextViewerDialog::loadFile(): Cannot open file %s", fname.toString(Common::Path::kNativeSeparator).c_str());

		return false;
	}

	Common::SeekableReadStream *stream = file.createReadStream();

	if (!stream) {
		warning("TextViewerDialog::loadFile(): Cannot load file %s", fname.toString(Common::Path::kNativeSeparator).c_str());

		return false;
	}

	warning("TextViewerDialog::loadFile(): File size is: %ld", long(stream->size()));

	_linesArray.clear();

	while (!stream->eos()) {
		Common::String line = stream->readString('\n');

		line.wordWrap(_lineWidth);

		Common::StringTokenizer lines(line, "\n");
		while (!lines.empty())
			_linesArray.push_back(lines.nextToken());
	}

	delete stream;

	return true;
}

void TextViewerDialog::destroy() {
	_linesArray.clear();
}

void TextViewerDialog::reflowLayout() {
	// Calculate the real width/height (rounded to char/line multiples)
	_w = (uint16)(kDialogWidthPercent * g_system->getOverlayWidth());
	_h = (uint16)((kDialogHeightPercent * g_system->getOverlayHeight() - 2) / _lineHeight);
	_h = _h * _lineHeight + 2;

	_x = (g_system->getOverlayWidth() - _w) / 2;
	_y = (g_system->getOverlayHeight() - _h) / 2;

	_padX = _w * kPadX;
	_padY = _h * kPadY;

	int16 bW = g_gui.xmlEval()->getVar("Globals.Button.Width", 0);
	int16 bH = g_gui.xmlEval()->getVar("Globals.Button.Height", 0);
	int16 padR = g_gui.xmlEval()->getVar("Globals.Padding.Right", 5);
	int16 padB = g_gui.xmlEval()->getVar("Globals.Padding.Bottom", 5);
	int16 scrollbarWidth = g_gui.xmlEval()->getVar("Globals.Scrollbar.Width", 0);

	int16 buttonOffset = bH + padB;

	_closeButton->setPos(_w - bW - padR, _h - buttonOffset);
	_closeButton->setSize(bW, bH);

	// Calculate depending values
	_lineWidth = (_w - scrollbarWidth - _padX * 2) / _charWidth;
	_linesPerPage = (_h - _padY * 2 - buttonOffset) / _lineHeight;

	_scrollBar->setPos(_w - scrollbarWidth - 1, 0);
	_scrollBar->setSize(scrollbarWidth, _h - buttonOffset);
}

void TextViewerDialog::open() {
	Dialog::open();
}

void TextViewerDialog::drawDialog(DrawLayer layerToDraw) {
	Dialog::drawDialog(layerToDraw);

	setTextDrawableArea(Common::Rect(_x, _y, _x + _w, _y + _h));

	// Draw a border
	//g_gui.hLine(_x, _y + _h - 1, _x + _w - 1, g_gui._color);

	// Draw text
	int y = _y + _padY;

	for (int line = 0; (line < _linesPerPage) && ((_currentPos + line) < (int)_linesArray.size()); line++) {
		int x = _x + _padX;
		const char *text = _linesArray[line + _currentPos].c_str();
		int w = MIN<int>(_lineWidth,  _linesArray[line + _currentPos].size());

		for (int column = 0; column < w; column++) {
			byte c = text[column];
			g_gui.theme()->drawChar(Common::Rect(x, y, x + _charWidth, y + _lineHeight), c, _font);
			x += _charWidth;
		}

		y += _lineHeight;
	}

	// Draw the scrollbar
	_scrollBar->_numEntries = _linesArray.size();
	_scrollBar->_currentPos = _currentPos;
	_scrollBar->_entriesPerPage = _linesPerPage;
	_scrollBar->recalc();
	_scrollBar->draw();
}


void TextViewerDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
	switch (cmd) {
	case kSetPositionCmd:
		_currentPos = _scrollBar->_currentPos;

		if (_currentPos < 0) {
			_currentPos = 0;
		}
		if ((_currentPos + _linesPerPage) >= (int)_linesArray.size()) {
			_currentPos = MAX<int32>(_linesArray.size() - _linesPerPage, 0);
		}

		drawDialog(kDrawLayerForeground);
		break;
	case kCloseCmd:
		close();
		break;
	default:
		return;
	}
}

void TextViewerDialog::handleMouseWheel(int x, int y, int direction) {
	_scrollBar->handleMouseWheel(x, y, direction);
}

void TextViewerDialog::handleKeyDown(Common::KeyState state) {
	switch (state.keycode) {
	case Common::KEYCODE_ESCAPE:
		close();
		break;
	case Common::KEYCODE_UP:
		_currentPos--;
		break;
	case Common::KEYCODE_DOWN:
		_currentPos++;
		break;
	case Common::KEYCODE_HOME:
		_currentPos = 0;
		break;
	case Common::KEYCODE_END:
		_currentPos = _linesArray.size() - _linesPerPage;
		break;
	case Common::KEYCODE_PAGEUP:
		_currentPos -= _linesPerPage;
		break;
	case Common::KEYCODE_PAGEDOWN:
		_currentPos += _linesPerPage;
		break;
	default:
		return;
	}

	if (_currentPos < 0) {
		_currentPos = 0;
	}
	if ((_currentPos + _linesPerPage) >= (int)_linesArray.size()) {
		_currentPos = MAX<int32>(_linesArray.size() - _linesPerPage, 0);
	}

	drawDialog(kDrawLayerForeground);
}


} // End of namespace GUI