1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef CONSOLE_DIALOG_H
24 #define CONSOLE_DIALOG_H
25 
26 #include "gui/dialog.h"
27 #include "common/str.h"
28 
29 namespace GUI {
30 
31 class ScrollBarWidget;
32 
33 /*
34  FIXME #1: The console dialog code has some fundamental problems.
35  First of, note the conflict between the (constant) value kCharsPerLine, and the
36  (variable) value _pageWidth. Look a bit at the code to get familiar with them,
37  then return...
38  Now, why don't we just drop kCharsPerLine? Because of the problem of resizing!
39  When the user changes the scaler, the console will get resized. If the dialog
40  becomes smaller because of this, we may have to rewrap text. If the resolution
41  is then increased again, we'd end up with garbled content.
42 
43  One can now either ignore this problem (and modify our code accordingly to
44  implement this simple rewrapping -- we currently don't do that at all!).
45 
46  Or, one can go and implement a more complete console, by replacing the
47  _buffer by a real line buffer -- an array of char* pointers.
48  This will allow one to implement resizing perfectly, but has the drawback
49  of making things like scrolling, drawing etc. more complicated.
50 
51  Either way, the current situation is bad, and we should resolve it one way
52  or the other (and if you can think of a third, feel free to suggest it).
53 
54 
55 
56  FIXME #2: Another problem is that apparently _pageWidth isn't computed quite
57  correctly. The current line ends well before reaching the right side of the
58  console dialog. That's irritating and should be fixed.
59 
60 
61  FIXME #3: The scroll bar is not shown initially, but the area it would
62  occupy is not used for anything else. As a result, the gap described above
63  becomes even wider and thus even more irritating.
64 */
65 class ConsoleDialog : public Dialog {
66 public:
67 	typedef bool (*InputCallbackProc)(ConsoleDialog *console, const char *input, void *refCon);
68 	typedef bool (*CompletionCallbackProc)(ConsoleDialog* console, const char *input, Common::String &completion, void *refCon);
69 
70 protected:
71 	enum {
72 		kBufferSize	= 32768,
73 		kCharsPerLine = 128,
74 
75 		kHistorySize = 20
76 	};
77 
78 	const Graphics::Font *_font;
79 
80 	char	_buffer[kBufferSize];
81 	int		_linesInBuffer;
82 
83 	int		_pageWidth;
84 	int		_linesPerPage;
85 
86 	int		_currentPos;
87 	int		_scrollLine;
88 	int		_firstLineInBuffer;
89 
90 	int		_promptStartPos;
91 	int		_promptEndPos;
92 
93 	bool	_caretVisible;
94 	uint32	_caretTime;
95 
96 	enum SlideMode {
97 		kNoSlideMode,
98 		kUpSlideMode,
99 		kDownSlideMode
100 	};
101 
102 	SlideMode	_slideMode;
103 	uint32	_slideTime;
104 
105 	ScrollBarWidget *_scrollBar;
106 
107 	// The _callbackProc is called whenver a data line is entered
108 	//
109 	InputCallbackProc _callbackProc;
110 	void *_callbackRefCon;
111 
112 	// _completionCallbackProc is called when tab is pressed
113 	CompletionCallbackProc _completionCallbackProc;
114 	void *_completionCallbackRefCon;
115 
116 	Common::String _history[kHistorySize];
117 	int _historySize;
118 	int _historyIndex;
119 	int _historyLine;
120 
121 	float _widthPercent, _heightPercent;
122 
123 	int	_leftPadding;
124 	int	_rightPadding;
125 	int	_topPadding;
126 	int	_bottomPadding;
127 
128 	void slideUpAndClose();
129 
130 public:
131 	ConsoleDialog(float widthPercent, float heightPercent);
132 
133 	void open();
134 	void close();
135 	void drawDialog();
136 
137 	void handleTickle();
138 	void reflowLayout();
139 	void handleMouseWheel(int x, int y, int direction);
140 	void handleKeyDown(Common::KeyState state);
141 	void handleCommand(CommandSender *sender, uint32 cmd, uint32 data);
142 
143 	int printFormat(int dummy, const char *format, ...) GCC_PRINTF(3, 4);
144 	int vprintFormat(int dummy, const char *format, va_list argptr);
145 
146 	void printChar(int c);
147 
setInputCallback(InputCallbackProc proc,void * refCon)148 	void setInputCallback(InputCallbackProc proc, void *refCon) {
149 		_callbackProc = proc;
150 		_callbackRefCon = refCon;
151 	}
setCompletionCallback(CompletionCallbackProc proc,void * refCon)152 	void setCompletionCallback(CompletionCallbackProc proc, void *refCon) {
153 		_completionCallbackProc = proc;
154 		_completionCallbackRefCon = refCon;
155 	}
156 
getCharsPerLine()157 	int getCharsPerLine() {
158 		return _pageWidth;
159 	}
160 
161 protected:
buffer(int idx)162 	inline char &buffer(int idx) {
163 		return _buffer[idx % kBufferSize];
164 	}
165 
166 	void init();
167 
pos2line(int pos)168 	int pos2line(int pos) { return (pos - (_scrollLine - _linesPerPage + 1) * kCharsPerLine) / kCharsPerLine; }
169 
170 	void drawLine(int line, bool restoreBg = true);
171 	void drawCaret(bool erase);
172 	void printCharIntern(int c);
173 	void insertIntoPrompt(const char *str);
174 	void print(const char *str);
175 	void updateScrollBuffer();
176 	void scrollToCurrent();
177 
178 	void defaultKeyDownHandler(Common::KeyState &state);
179 
180 	// Line editing
181 	void specialKeys(int keycode);
182 	void nextLine();
183 	void killChar();
184 	void killLine();
185 	void killLastWord();
186 
187 	// History
188 	void addToHistory(const Common::String &str);
189 	void historyScroll(int direction);
190 };
191 
192 } // End of namespace GUI
193 
194 #endif
195