aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/archetype/wrap.cpp
blob: ddc38500437a4e29d89cf6ae351e23baa7f0bb96 (plain)
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
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/archetype/wrap.h"
#include "glk/archetype/archetype.h"

namespace Glk {
namespace Archetype {

const int
	MAXCOLS = 75,			// leave room for punctuation
	SAFETY_MARGIN = 3,
	MAXROWS = 24,

	REVERSE_VID = 3,
	BOLDFACE = 8;

int Rows;
int cursor;

void wrap_init() {
	cursor_reset();
	Rows = 0;
}

static void wrap_wait() {
#ifdef UNUSED
	char ch;

	TextColor(BOLDFACE); TextBackground(REVERSE_VID);
	write('Hit any key to continue...');
	ch := ReadKey;
	write(chr(13));
	NormVideo;
	ClrScr;				// or ClrEol if you don't want the whole screen
	Rows : = 0
#endif
}

void wrapint(int i, bool terminate) {
	String s = String::format("%d", i);
	wrapout(s, terminate);
}

void wrapout(const String &str, bool terminate) {
	int thisline, maxchars, startnext;
	String s = str;

	// 'thisline' starts out as the maximum number of characters that can be
	// written before a newline; it gets trimmed back to being the number of
	// characters from the string that are actually written on this line
	maxchars = MAXCOLS - cursor;

	const char CHARS[7] = { '.', ',', ':', ';', ')', '-', '"' };
	for (int i = 0; i < 7; ++i) {
		if (!s.empty() && s[0] == CHARS[i]) {
			maxchars += SAFETY_MARGIN;
			break;
		}
	}

	thisline = maxchars;
	while (thisline < (int)s.size()) {
		while (thisline >= 0 && s[thisline] != ' ')
			--thisline;
	}

	// If we were unable to find a wrapping point then it means one of two
	// things : a) the string is too long to fit on one line, andmust be
	// split unnaturally; or b) we are near the end of a line andmust wrap
	// the entire string; i.e.print nothing, finish the line andgo on
	if (thisline == 0 && s.size() > MAXCOLS)
	thisline = maxchars + 1;

	g_vm->writeln(s.substring(0, thisline - 1));
	++Rows;
	if (Rows >= MAXROWS)
		wrap_wait();

	startnext = thisline;
	while (startnext < (int)s.size() && s[startnext] == ' ') {
		++startnext;

		s = s.substring(startnext, s.size());
		cursor = 1;
		thisline = MAXCOLS - cursor;
	}

	g_vm->write(s);
	cursor += s.size();

	if (terminate) {
		g_vm->writeln();
		++Rows;
		if (Rows >= MAXROWS)
			wrap_wait();
		cursor = 1;
	}
}

void wraperr(const String &s) {
	if (cursor > 1)
		g_vm->writeln();
	g_vm->writeln(s);

	String tmp;
	for (int i = 1; i < cursor; ++i)
		tmp += ' ';
	g_vm->write(tmp);
}

StringPtr ReadLine(bool full_line) {
	String s;

	if (full_line)
		g_vm->readln(s);
	else
		s = g_vm->ReadKey();

	return NewDynStr(s);
}

void cursor_reset() {
	cursor = 1;
}

} // End of namespace Archetype
} // End of namespace Glk