ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
console.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef CONSOLE_DIALOG_H
23 #define CONSOLE_DIALOG_H
24 
25 #include "gui/dialog.h"
26 #include "common/str.h"
27 
28 namespace GUI {
29 
30 class ScrollBarWidget;
31 
32 /*
33  FIXME #1: The console dialog code has some fundamental problems.
34  First of, note the conflict between the (constant) value kCharsPerLine, and the
35  (variable) value _pageWidth. Look a bit at the code to get familiar with them,
36  then return...
37  Now, why don't we just drop kCharsPerLine? Because of the problem of resizing!
38  When the user changes the scaler, the console will get resized. If the dialog
39  becomes smaller because of this, we may have to rewrap text. If the resolution
40  is then increased again, we'd end up with garbled content.
41 
42  One can now either ignore this problem (and modify our code accordingly to
43  implement this simple rewrapping -- we currently don't do that at all!).
44 
45  Or, one can go and implement a more complete console, by replacing the
46  _buffer by a real line buffer -- an array of char* pointers.
47  This will allow one to implement resizing perfectly, but has the drawback
48  of making things like scrolling, drawing etc. more complicated.
49 
50  Either way, the current situation is bad, and we should resolve it one way
51  or the other (and if you can think of a third, feel free to suggest it).
52 
53 
54 
55  FIXME #2: Another problem is that apparently _pageWidth isn't computed quite
56  correctly. The current line ends well before reaching the right side of the
57  console dialog. That's irritating and should be fixed.
58 
59 
60  FIXME #3: The scroll bar is not shown initially, but the area it would
61  occupy is not used for anything else. As a result, the gap described above
62  becomes even wider and thus even more irritating.
63 */
64 class ConsoleDialog : public Dialog {
65 public:
66  typedef bool (*InputCallbackProc)(ConsoleDialog *console, const char *input, void *refCon);
67  typedef bool (*CompletionCallbackProc)(ConsoleDialog* console, const char *input, Common::String &completion, void *refCon);
68 
69 protected:
70  enum {
71  kCharsPerLine = 128,
72  kBufferSize = kCharsPerLine * 1024,
73 
74  kHistorySize = 20,
75  kDraggingTime = 10
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  uint32 _selectionTime;
96 
97  enum SlideMode {
98  kNoSlideMode,
99  kUpSlideMode,
100  kDownSlideMode
101  };
102 
103  SlideMode _slideMode;
104  uint32 _slideTime;
105 
106  ScrollBarWidget *_scrollBar;
107 
108  // The _callbackProc is called whenver a data line is entered
109  //
110  InputCallbackProc _callbackProc;
111  void *_callbackRefCon;
112 
113  // _completionCallbackProc is called when tab is pressed
114  CompletionCallbackProc _completionCallbackProc;
115  void *_completionCallbackRefCon;
116 
117  Common::String _history[kHistorySize];
118  int _historySize;
119  int _historyIndex;
120  int _historyLine;
121 
122  float _widthPercent, _heightPercent;
123 
124  int _leftPadding;
125  int _rightPadding;
126  int _topPadding;
127  int _bottomPadding;
128 
129  void slideUpAndClose();
130 
131  Common::String _prompt;
132 
133  bool _isDragging;
134 
135  int _selBegin;
136  int _selEnd;
137 
138  int _scrollDirection;
139 
140 public:
141  ConsoleDialog(float widthPercent, float heightPercent);
142  virtual ~ConsoleDialog();
143 
144  void open() override;
145  void close() override;
146  void drawDialog(DrawLayer layerToDraw) override;
147 
148  void handleTickle() override;
149  void reflowLayout() override;
150  void handleMouseWheel(int x, int y, int direction) override;
151  void handleKeyDown(Common::KeyState state) override;
152  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
153  void handleOtherEvent(const Common::Event &evt) override;
154  void handleMouseDown(int x, int y, int button, int clickCount) override;
155  void handleMouseMoved(int x, int y, int button) override;
156  void handleMouseUp(int x, int y, int button, int clickCount) override;
157 
158  int printFormat(int dummy, MSVC_PRINTF const char *format, ...) GCC_PRINTF(3, 4);
159  int vprintFormat(int dummy, const char *format, va_list argptr);
160 
161  void printChar(int c);
162 
163  void setInputCallback(InputCallbackProc proc, void *refCon) {
164  _callbackProc = proc;
165  _callbackRefCon = refCon;
166  }
167  void setCompletionCallback(CompletionCallbackProc proc, void *refCon) {
168  _completionCallbackProc = proc;
169  _completionCallbackRefCon = refCon;
170  }
171 
172  int getCharsPerLine() {
173  return _pageWidth;
174  }
175 
176  void setPrompt(Common::String prompt);
177  void resetPrompt();
178  void clearBuffer();
179 
180 protected:
181  inline char &buffer(int idx) {
182  return _buffer[idx % kBufferSize];
183  }
184 
185  void init();
186 
187  int pos2line(int pos) { return (pos - (_scrollLine - _linesPerPage + 1) * kCharsPerLine) / kCharsPerLine; }
188 
189  void drawLine(int line);
190  void drawCaret(bool erase);
191  void printCharIntern(int c);
192  void insertIntoPrompt(const char *str);
193  void print(const char *str);
194  void updateScrollBuffer();
195  void scrollToCurrent();
196  Common::String getUserInput();
197 
198  void defaultKeyDownHandler(Common::KeyState &state);
199 
200  // Line editing
201  void specialKeys(Common::KeyCode keycode);
202  void nextLine();
203  void killChar();
204  void killLine();
205  void killLastWord();
206 
207  // History
208  void loadHistory();
209  void saveHistory();
210  void addToHistory(const Common::String &str);
211  void historyScroll(int direction);
212 
216  bool clampSelection(int &sel);
217 };
218 
219 } // End of namespace GUI
220 
221 #endif
Definition: console.h:64
Definition: str.h:59
Definition: font.h:83
Definition: scrollbar.h:34
Definition: system.h:46
int FORCEINLINE GCC_PRINTF(2, 0) int vsprintf_s(T(&dst)[N]
Definition: events.h:199
Definition: dialog.h:49
Definition: keyboard.h:294
void drawDialog(DrawLayer layerToDraw) override
bool clampSelection(int &sel)
Definition: object.h:40