aboutsummaryrefslogtreecommitdiff
path: root/gui/console.h
blob: 8a9659c1e9405c32808cb20b7bd7f1434edae858 (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
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
/* 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.
 *
 */

#ifndef CONSOLE_DIALOG_H
#define CONSOLE_DIALOG_H

#include "gui/dialog.h"
#include "common/str.h"

namespace GUI {

class ScrollBarWidget;

/*
 FIXME #1: The console dialog code has some fundamental problems.
 First of, note the conflict between the (constant) value kCharsPerLine, and the
 (variable) value _pageWidth. Look a bit at the code to get familiar with them,
 then return...
 Now, why don't we just drop kCharsPerLine? Because of the problem of resizing!
 When the user changes the scaler, the console will get resized. If the dialog
 becomes smaller because of this, we may have to rewrap text. If the resolution
 is then increased again, we'd end up with garbled content.

 One can now either ignore this problem (and modify our code accordingly to
 implement this simple rewrapping -- we currently don't do that at all!).

 Or, one can go and implement a more complete console, by replacing the
 _buffer by a real line buffer -- an array of char* pointers.
 This will allow one to implement resizing perfectly, but has the drawback
 of making things like scrolling, drawing etc. more complicated.

 Either way, the current situation is bad, and we should resolve it one way
 or the other (and if you can think of a third, feel free to suggest it).



 FIXME #2: Another problem is that apparently _pageWidth isn't computed quite
 correctly. The current line ends well before reaching the right side of the
 console dialog. That's irritating and should be fixed.


 FIXME #3: The scroll bar is not shown initially, but the area it would
 occupy is not used for anything else. As a result, the gap described above
 becomes even wider and thus even more irritating.
*/
class ConsoleDialog : public Dialog {
public:
	typedef bool (*InputCallbackProc)(ConsoleDialog *console, const char *input, void *refCon);
	typedef bool (*CompletionCallbackProc)(ConsoleDialog* console, const char *input, Common::String &completion, void *refCon);

protected:
	enum {
		kBufferSize   = 32768,
		kCharsPerLine = 128,

		kHistorySize  = 20
	};

	const Graphics::Font *_font;

	char _buffer[kBufferSize];
	int  _linesInBuffer;

	int _pageWidth;
	int _linesPerPage;

	int _currentPos;
	int _scrollLine;
	int _firstLineInBuffer;

	int _promptStartPos;
	int _promptEndPos;

	bool   _caretVisible;
	uint32 _caretTime;

	enum SlideMode {
		kNoSlideMode,
		kUpSlideMode,
		kDownSlideMode
	};

	SlideMode _slideMode;
	uint32    _slideTime;

	ScrollBarWidget *_scrollBar;

	// The _callbackProc is called whenver a data line is entered
	//
	InputCallbackProc _callbackProc;
	void *_callbackRefCon;

	// _completionCallbackProc is called when tab is pressed
	CompletionCallbackProc _completionCallbackProc;
	void *_completionCallbackRefCon;

	Common::String _history[kHistorySize];
	int _historySize;
	int _historyIndex;
	int _historyLine;

	float _widthPercent, _heightPercent;

	int _leftPadding;
	int _rightPadding;
	int _topPadding;
	int _bottomPadding;

	void slideUpAndClose();

public:
	ConsoleDialog(float widthPercent, float heightPercent);

	void open() override;
	void close() override;
	void drawDialog(DrawLayer layerToDraw) override;

	void handleTickle() override;
	void reflowLayout() override;
	void handleMouseWheel(int x, int y, int direction) override;
	void handleKeyDown(Common::KeyState state) override;
	void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;

	int printFormat(int dummy, const char *format, ...) GCC_PRINTF(3, 4);
	int vprintFormat(int dummy, const char *format, va_list argptr);

	void printChar(int c);

	void setInputCallback(InputCallbackProc proc, void *refCon) {
		_callbackProc = proc;
		_callbackRefCon = refCon;
	}
	void setCompletionCallback(CompletionCallbackProc proc, void *refCon) {
		_completionCallbackProc = proc;
		_completionCallbackRefCon = refCon;
	}

	int getCharsPerLine() {
		return _pageWidth;
	}

protected:
	inline char &buffer(int idx) {
		return _buffer[idx % kBufferSize];
	}

	void init();

	int pos2line(int pos) { return (pos - (_scrollLine - _linesPerPage + 1) * kCharsPerLine) / kCharsPerLine; }

	void drawLine(int line);
	void drawCaret(bool erase);
	void printCharIntern(int c);
	void insertIntoPrompt(const char *str);
	void print(const char *str);
	void updateScrollBuffer();
	void scrollToCurrent();
	Common::String getUserInput();

	void defaultKeyDownHandler(Common::KeyState &state);

	// Line editing
	void specialKeys(Common::KeyCode keycode);
	void nextLine();
	void killChar();
	void killLine();
	void killLastWord();

	// History
	void addToHistory(const Common::String &str);
	void historyScroll(int direction);
};

} // End of namespace GUI

#endif