diff options
author | Max Horn | 2002-12-15 02:24:32 +0000 |
---|---|---|
committer | Max Horn | 2002-12-15 02:24:32 +0000 |
commit | 1d40232efbba9ec61c4e3c023e679d594380b062 (patch) | |
tree | b207a0fb3bc16794f7b47f0206aba072ecf05b89 /gui | |
parent | 83df0608121f8168cefcfa33c85aa3ceb2b9ac8e (diff) | |
download | scummvm-rg350-1d40232efbba9ec61c4e3c023e679d594380b062.tar.gz scummvm-rg350-1d40232efbba9ec61c4e3c023e679d594380b062.tar.bz2 scummvm-rg350-1d40232efbba9ec61c4e3c023e679d594380b062.zip |
added a history (based on code by olki)
svn-id: r5976
Diffstat (limited to 'gui')
-rw-r--r-- | gui/console.cpp | 66 | ||||
-rw-r--r-- | gui/console.h | 13 |
2 files changed, 77 insertions, 2 deletions
diff --git a/gui/console.cpp b/gui/console.cpp index 9318e73d9a..e97fecef9f 100644 --- a/gui/console.cpp +++ b/gui/console.cpp @@ -72,8 +72,16 @@ ConsoleDialog::ConsoleDialog(NewGui *gui) _promptStartPos = _promptEndPos = -1; + // Init callback _callbackProc = 0; _callbackRefCon = 0; + + // Init History + _historyIndex = 0; + _historyLine = 0; + _historySize = 0; + for (int i = 0; i < kHistorySize; i++) + _history[i][0] = '\0'; } void ConsoleDialog::open() @@ -148,8 +156,10 @@ void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) char str[len + 1]; for (i = 0; i < len; i++) str[i] = _buffer[(_promptStartPos + i) % kBufferSize]; - str[len] = 0; + str[len] = '\0'; + addToHistory(str); + bool keepRunning = true; if (_callbackProc) keepRunning = (*_callbackProc)(this, str, _callbackRefCon); @@ -203,6 +213,12 @@ void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) updateScrollBar(); draw(); break; + case 273: // cursor up + historyScroll(+1); + break; + case 274: // cursor down + historyScroll(-1); + break; case 275: // cursor right if (_currentPos < _promptEndPos) _currentPos++; @@ -302,6 +318,54 @@ void ConsoleDialog::killLastWord() _promptEndPos -= cnt + 1; } +void ConsoleDialog::addToHistory(const char *str) +{ + strcpy(_history[_historyIndex], str); + _historyIndex = (_historyIndex + 1) % kHistorySize; + _historyLine = 0; + if (_historySize < kHistorySize) + _historySize++; +} + +void ConsoleDialog::historyScroll(int direction) +{ + if (_historySize == 0) + return; + + // Advance to the next line in the history + int line = _historyLine + direction; + if ((direction < 0 && line < 0) || (direction > 0 && line > _historySize)) + return; + _historyLine = line; + + // Hide caret if visible + if (_caretVisible) + drawCaret(true); + + // Remove the current user text + _currentPos = _promptStartPos; + killLine(); + + // ... and ensure the prompt is visible + scrollToCurrent(); + + // Print the text from the history + if (_historyLine > 0) { + int idx = (_historyIndex - _historyLine + _historySize) % _historySize; + for (int i = 0; i < kLineBufferSize && _history[idx][i] != '\0'; i++) + putcharIntern(_history[idx][i]); + _promptEndPos = _currentPos; + + // Ensure once more the caret is visible (in case of very long history entries) + scrollToCurrent(); + } else { + // TODO print the text which the user had typed before using the history + } + + draw(); +} + + void ConsoleDialog::nextLine() { int line = _currentPos / _lineWidth; diff --git a/gui/console.h b/gui/console.h index a3b91223a6..149a2248a5 100644 --- a/gui/console.h +++ b/gui/console.h @@ -29,7 +29,9 @@ enum { kBufferSize = 32768, kLineBufferSize = 256, - kCharWidth = 8 + kCharWidth = 8, + + kHistorySize = 20, }; class ScrollBarWidget; @@ -61,6 +63,11 @@ protected: InputCallbackProc _callbackProc; void *_callbackRefCon; + char _history[kHistorySize][kLineBufferSize]; + int _historySize; + int _historyIndex; + int _historyLine; + public: ConsoleDialog(NewGui *gui); @@ -97,6 +104,10 @@ protected: void killChar(); void killLine(); void killLastWord(); + + // History + void addToHistory(const char *str); + void historyScroll(int direction); }; #endif |