ScummVM API documentation
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  void loadHistory();
123  void saveHistory();
124 
125  float _widthPercent, _heightPercent;
126 
127  int _leftPadding;
128  int _rightPadding;
129  int _topPadding;
130  int _bottomPadding;
131 
132  void slideUpAndClose();
133 
134  Common::String _prompt;
135 
136  bool _isDragging;
137 
138  int _selBegin;
139  int _selEnd;
140 
141  int _scrollDirection;
142 
143 public:
144  ConsoleDialog(float widthPercent, float heightPercent);
145  virtual ~ConsoleDialog();
146 
147  void open() override;
148  void close() override;
149  void drawDialog(DrawLayer layerToDraw) override;
150 
151  void handleTickle() override;
152  void reflowLayout() override;
153  void handleMouseWheel(int x, int y, int direction) override;
154  void handleKeyDown(Common::KeyState state) override;
155  void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
156  void handleOtherEvent(const Common::Event &evt) override;
157  void handleMouseDown(int x, int y, int button, int clickCount) override;
158  void handleMouseMoved(int x, int y, int button) override;
159  void handleMouseUp(int x, int y, int button, int clickCount) override;
160 
161  int printFormat(int dummy, MSVC_PRINTF const char *format, ...) GCC_PRINTF(3, 4);
162  int vprintFormat(int dummy, const char *format, va_list argptr);
163 
164  void printChar(int c);
165 
166  void setInputCallback(InputCallbackProc proc, void *refCon) {
167  _callbackProc = proc;
168  _callbackRefCon = refCon;
169  }
170  void setCompletionCallback(CompletionCallbackProc proc, void *refCon) {
171  _completionCallbackProc = proc;
172  _completionCallbackRefCon = refCon;
173  }
174 
175  int getCharsPerLine() {
176  return _pageWidth;
177  }
178 
179  void setPrompt(Common::String prompt);
180  void resetPrompt();
181  void clearBuffer();
182 
183 protected:
184  inline char &buffer(int idx) {
185  return _buffer[idx % kBufferSize];
186  }
187 
188  void init();
189 
190  int pos2line(int pos) { return (pos - (_scrollLine - _linesPerPage + 1) * kCharsPerLine) / kCharsPerLine; }
191 
192  void drawLine(int line);
193  void drawCaret(bool erase);
194  void printCharIntern(int c);
195  void insertIntoPrompt(const char *str);
196  void print(const char *str);
197  void updateScrollBuffer();
198  void scrollToCurrent();
199  Common::String getUserInput();
200 
201  void defaultKeyDownHandler(Common::KeyState &state);
202 
203  // Line editing
204  void specialKeys(Common::KeyCode keycode);
205  void nextLine();
206  void killChar();
207  void killLine();
208  void killLastWord();
209 
210  // History
211  void addToHistory(const Common::String &str);
212  void historyScroll(int direction);
213 };
214 
215 } // End of namespace GUI
216 
217 #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
Definition: object.h:40