2002-12-14 14:31:44 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2004-01-06 12:45:34 +00:00
|
|
|
* Copyright (C) 2002-2004 The ScummVM project
|
2002-12-14 14:31:44 +00:00
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2003-09-23 18:07:50 +00:00
|
|
|
#include "gui/console.h"
|
2003-11-10 23:40:48 +00:00
|
|
|
#include "gui/ScrollBarWidget.h"
|
2002-12-14 14:31:44 +00:00
|
|
|
|
2003-09-18 02:07:18 +00:00
|
|
|
#include "base/engine.h"
|
2004-02-09 01:27:27 +00:00
|
|
|
#include "base/version.h"
|
2002-12-14 14:31:44 +00:00
|
|
|
|
2004-03-21 21:20:25 +00:00
|
|
|
#include "graphics/font.h"
|
2004-08-15 13:15:55 +00:00
|
|
|
|
2004-08-15 14:39:35 +00:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
extern const Graphics::NewFont g_consolefont;
|
|
|
|
|
|
|
|
#define kConsoleCharWidth (g_consolefont.getMaxCharWidth())
|
|
|
|
#define kConsoleLineHeight (g_consolefont.getFontHeight() + 2)
|
2003-11-19 23:46:39 +00:00
|
|
|
|
2004-11-27 01:25:25 +00:00
|
|
|
enum {
|
|
|
|
kConsoleSlideDownDuration = 300 // Time in milliseconds
|
|
|
|
};
|
2002-12-14 18:57:15 +00:00
|
|
|
|
2003-11-10 23:40:48 +00:00
|
|
|
|
2002-12-16 06:21:08 +00:00
|
|
|
#define PROMPT ") "
|
2002-12-14 18:57:15 +00:00
|
|
|
|
2002-12-14 14:31:44 +00:00
|
|
|
/* TODO:
|
|
|
|
* - it is very inefficient to redraw the full thingy when just one char is added/removed.
|
|
|
|
* Instead, we could just copy the GFX of the blank console (i.e. after the transparent
|
|
|
|
* background is drawn, before any text is drawn). Then using that, it becomes trivial
|
|
|
|
* to erase a single character, do scrolling etc.
|
|
|
|
* - a *lot* of others things, this code is in no way complete and heavily under progress
|
|
|
|
*/
|
2003-11-02 02:18:16 +00:00
|
|
|
ConsoleDialog::ConsoleDialog(float widthPercent, float heightPercent)
|
|
|
|
: Dialog(0, 0, 1, 1),
|
2003-09-20 01:08:48 +00:00
|
|
|
_widthPercent(widthPercent), _heightPercent(heightPercent) {
|
|
|
|
|
|
|
|
// Setup basic layout/dialog size
|
|
|
|
reflowLayout();
|
2002-12-14 14:31:44 +00:00
|
|
|
|
|
|
|
memset(_buffer, ' ', kBufferSize);
|
|
|
|
_linesInBuffer = kBufferSize / _lineWidth;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-12-14 21:37:40 +00:00
|
|
|
_currentPos = 0;
|
2002-12-14 18:57:15 +00:00
|
|
|
_scrollLine = _linesPerPage - 1;
|
2004-05-23 14:06:52 +00:00
|
|
|
_firstLineInBuffer = 0;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-12-14 20:04:46 +00:00
|
|
|
_caretVisible = false;
|
|
|
|
_caretTime = 0;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2004-11-27 01:25:25 +00:00
|
|
|
_slideUpAndClose = false;
|
|
|
|
_slideTime = 0;
|
|
|
|
|
2002-12-14 20:04:46 +00:00
|
|
|
// Add scrollbar
|
|
|
|
_scrollBar = new ScrollBarWidget(this, _w - kScrollBarWidth - 1, 0, kScrollBarWidth, _h);
|
|
|
|
_scrollBar->setTarget(this);
|
|
|
|
|
2002-12-15 02:24:32 +00:00
|
|
|
// Init callback
|
2002-12-15 00:36:34 +00:00
|
|
|
_callbackProc = 0;
|
|
|
|
_callbackRefCon = 0;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
|
|
|
// Init History
|
|
|
|
_historyIndex = 0;
|
|
|
|
_historyLine = 0;
|
|
|
|
_historySize = 0;
|
|
|
|
for (int i = 0; i < kHistorySize; i++)
|
|
|
|
_history[i][0] = '\0';
|
2003-09-20 01:08:48 +00:00
|
|
|
|
2004-05-23 14:06:52 +00:00
|
|
|
_promptStartPos = _promptEndPos = -1;
|
|
|
|
|
2003-09-20 01:08:48 +00:00
|
|
|
// Display greetings & prompt
|
|
|
|
print(gScummVMFullVersion);
|
|
|
|
print("\nConsole is ready\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDialog::reflowLayout() {
|
2004-03-13 13:03:25 +00:00
|
|
|
// Calculate the real width/height (rounded to char/line multiples)
|
2004-03-28 16:30:50 +00:00
|
|
|
_w = (uint16)(_widthPercent * g_system->getOverlayWidth());
|
2004-08-15 14:39:35 +00:00
|
|
|
// _w = (_widthPercent * g_system->getOverlayWidth() - kScrollBarWidth - 2) / kConsoleCharWidth;
|
|
|
|
// _w = _w * kConsoleCharWidth + kScrollBarWidth + 2;
|
|
|
|
_h = (uint16)((_heightPercent * g_system->getOverlayHeight() - 2) / kConsoleLineHeight);
|
|
|
|
_h = _h * kConsoleLineHeight + 2;
|
2003-11-08 23:22:16 +00:00
|
|
|
|
2003-09-20 01:08:48 +00:00
|
|
|
// Calculate depending values
|
2004-08-15 14:39:35 +00:00
|
|
|
_lineWidth = (_w - kScrollBarWidth - 2) / kConsoleCharWidth;
|
|
|
|
_linesPerPage = (_h - 2) / kConsoleLineHeight;
|
2002-12-15 00:36:34 +00:00
|
|
|
}
|
|
|
|
|
2004-11-27 01:25:25 +00:00
|
|
|
void ConsoleDialog::slideUpAndClose() {
|
|
|
|
if (!_slideUpAndClose) {
|
|
|
|
_slideTime = g_system->getMillis();
|
|
|
|
_slideUpAndClose = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::open() {
|
2004-11-27 01:25:25 +00:00
|
|
|
// Initiate sliding the console down. We do a very simple trick to achieve
|
|
|
|
// this effect: we simply move the console dialog just above (outside) the
|
|
|
|
// visible screen area, then shift it down in handleTickle() over a
|
|
|
|
// certain period of time.
|
|
|
|
_y = -_h;
|
|
|
|
_slideTime = g_system->getMillis();
|
|
|
|
|
2002-12-15 00:36:34 +00:00
|
|
|
Dialog::open();
|
|
|
|
if (_promptStartPos == -1) {
|
|
|
|
print(PROMPT);
|
|
|
|
_promptStartPos = _promptEndPos = _currentPos;
|
|
|
|
}
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::drawDialog() {
|
2002-12-14 16:08:37 +00:00
|
|
|
// Blend over the background
|
2003-11-02 02:18:16 +00:00
|
|
|
g_gui.blendRect(_x, _y, _w, _h, g_gui._bgcolor, 2);
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-12-14 16:08:37 +00:00
|
|
|
// Draw a border
|
2003-11-02 02:18:16 +00:00
|
|
|
g_gui.hLine(_x, _y + _h - 1, _x + _w - 1, g_gui._color);
|
2002-12-14 14:31:44 +00:00
|
|
|
|
|
|
|
// Draw text
|
|
|
|
int start = _scrollLine - _linesPerPage + 1;
|
2002-12-14 18:57:15 +00:00
|
|
|
int y = _y + 2;
|
2004-05-23 14:06:52 +00:00
|
|
|
|
2002-12-14 14:31:44 +00:00
|
|
|
for (int line = 0; line < _linesPerPage; line++) {
|
|
|
|
int x = _x + 1;
|
|
|
|
for (int column = 0; column < _lineWidth; column++) {
|
2004-05-05 23:08:30 +00:00
|
|
|
#if 0
|
2003-03-06 19:52:54 +00:00
|
|
|
int l = (start + line) % _linesInBuffer;
|
2004-05-05 23:06:44 +00:00
|
|
|
byte c = buffer(l * _lineWidth + column);
|
2004-05-05 23:08:30 +00:00
|
|
|
#else
|
|
|
|
byte c = buffer((start + line) * _lineWidth + column);
|
|
|
|
#endif
|
2004-08-15 14:39:35 +00:00
|
|
|
g_gui.drawChar(c, x, y, g_gui._textcolor, &g_consolefont);
|
|
|
|
x += kConsoleCharWidth;
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
2004-08-15 14:39:35 +00:00
|
|
|
y += kConsoleLineHeight;
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
|
|
|
|
2002-12-14 20:04:46 +00:00
|
|
|
// Draw the scrollbar
|
|
|
|
_scrollBar->draw();
|
|
|
|
|
2002-12-14 16:08:37 +00:00
|
|
|
// Finally blit it all to the screen
|
2003-11-02 02:18:16 +00:00
|
|
|
g_gui.addDirtyRect(_x, _y, _w, _h);
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::handleTickle() {
|
2004-09-28 20:19:37 +00:00
|
|
|
uint32 time = g_system->getMillis();
|
2002-12-14 18:57:15 +00:00
|
|
|
if (_caretTime < time) {
|
|
|
|
_caretTime = time + kCaretBlinkTime;
|
2003-11-28 22:08:52 +00:00
|
|
|
drawCaret(_caretVisible);
|
2002-12-14 18:57:15 +00:00
|
|
|
}
|
2004-11-27 01:25:25 +00:00
|
|
|
|
|
|
|
// Perform the "slide animation".
|
|
|
|
if (_slideUpAndClose) {
|
|
|
|
_y = -(int)(_h * (float)(g_system->getMillis() - _slideTime) / kConsoleSlideDownDuration);
|
|
|
|
if (_y <= -_h) {
|
|
|
|
_slideUpAndClose = false;
|
|
|
|
close();
|
|
|
|
} else
|
|
|
|
draw();
|
|
|
|
} else
|
|
|
|
if (_y < 0) {
|
|
|
|
_y = -(int)(_h * (1.0 - (float)(g_system->getMillis() - _slideTime) / kConsoleSlideDownDuration));
|
|
|
|
if (_y > 0)
|
|
|
|
_y = 0;
|
|
|
|
|
|
|
|
draw();
|
|
|
|
}
|
2002-12-14 18:57:15 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::handleMouseWheel(int x, int y, int direction) {
|
2002-12-14 20:04:46 +00:00
|
|
|
_scrollBar->handleMouseWheel(x, y, direction);
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
|
2002-12-14 22:18:00 +00:00
|
|
|
int i;
|
2004-11-27 01:25:25 +00:00
|
|
|
|
|
|
|
if (_slideUpAndClose)
|
|
|
|
return;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
switch (keycode) {
|
2003-09-24 06:33:59 +00:00
|
|
|
case '\n': // enter/return
|
|
|
|
case '\r': {
|
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
2002-12-14 22:18:00 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
nextLine();
|
2002-12-14 22:18:00 +00:00
|
|
|
|
2003-11-28 22:08:52 +00:00
|
|
|
assert(_promptEndPos >= _promptStartPos);
|
2003-09-24 06:33:59 +00:00
|
|
|
int len = _promptEndPos - _promptStartPos;
|
|
|
|
bool keepRunning = true;
|
2002-12-16 03:21:38 +00:00
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
if (len > 0) {
|
2002-12-14 22:18:00 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
// We have to allocate the string buffer with new, since VC++ sadly does not
|
|
|
|
// comply to the C++ standard, so we can't use a dynamic sized stack array.
|
|
|
|
char *str = new char[len + 1];
|
2002-12-16 22:25:54 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
// Copy the user input to str
|
|
|
|
for (i = 0; i < len; i++)
|
2004-05-05 23:06:44 +00:00
|
|
|
str[i] = buffer(_promptStartPos + i);
|
2003-09-24 06:33:59 +00:00
|
|
|
str[len] = '\0';
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
// Add the input to the history
|
|
|
|
addToHistory(str);
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
// Pass it to the input callback, if any
|
|
|
|
if (_callbackProc)
|
|
|
|
keepRunning = (*_callbackProc)(this, str, _callbackRefCon);
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
// Get rid of the string buffer
|
|
|
|
delete [] str;
|
|
|
|
}
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
print(PROMPT);
|
|
|
|
_promptStartPos = _promptEndPos = _currentPos;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
draw();
|
|
|
|
if (!keepRunning)
|
2004-11-27 01:25:25 +00:00
|
|
|
slideUpAndClose();
|
2003-09-24 06:33:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 27: // escape
|
2004-11-27 01:25:25 +00:00
|
|
|
slideUpAndClose();
|
2003-09-24 06:33:59 +00:00
|
|
|
break;
|
|
|
|
case 8: // backspace
|
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
|
|
|
if (_currentPos > _promptStartPos) {
|
|
|
|
_currentPos--;
|
|
|
|
killChar();
|
|
|
|
}
|
|
|
|
scrollToCurrent();
|
|
|
|
draw(); // FIXME - not nice to redraw the full console just for one char!
|
|
|
|
break;
|
|
|
|
case 9: // tab
|
|
|
|
{
|
|
|
|
if (_completionCallbackProc) {
|
|
|
|
int len = _currentPos - _promptStartPos;
|
|
|
|
assert(len >= 0);
|
|
|
|
char *str = new char[len + 1];
|
2003-11-08 23:22:16 +00:00
|
|
|
|
2003-09-24 06:33:59 +00:00
|
|
|
// Copy the user input to str
|
|
|
|
for (i = 0; i < len; i++)
|
2004-05-05 23:06:44 +00:00
|
|
|
str[i] = buffer(_promptStartPos + i);
|
2003-09-24 06:33:59 +00:00
|
|
|
str[len] = '\0';
|
|
|
|
|
|
|
|
char *completion = 0;
|
|
|
|
if ((*_completionCallbackProc)(this, str, completion, _callbackRefCon)) {
|
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
insertIntoPrompt(completion);
|
|
|
|
scrollToCurrent();
|
|
|
|
draw();
|
|
|
|
delete[] completion;
|
2003-05-03 21:49:19 +00:00
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
delete[] str;
|
2003-05-03 21:49:19 +00:00
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 127:
|
|
|
|
killChar();
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 256 + 24: // pageup
|
2004-05-23 14:06:52 +00:00
|
|
|
if (modifiers == OSystem::KBD_SHIFT) {
|
|
|
|
_scrollLine -= _linesPerPage - 1;
|
|
|
|
if (_scrollLine < _firstLineInBuffer + _linesPerPage - 1)
|
|
|
|
_scrollLine = _firstLineInBuffer + _linesPerPage - 1;
|
|
|
|
updateScrollBuffer();
|
|
|
|
draw();
|
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
break;
|
|
|
|
case 256 + 25: // pagedown
|
2004-05-23 14:06:52 +00:00
|
|
|
if (modifiers == OSystem::KBD_SHIFT) {
|
|
|
|
_scrollLine += _linesPerPage - 1;
|
|
|
|
if (_scrollLine > _promptEndPos / _lineWidth)
|
|
|
|
_scrollLine = _promptEndPos / _lineWidth;
|
|
|
|
updateScrollBuffer();
|
|
|
|
draw();
|
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
break;
|
|
|
|
case 256 + 22: // home
|
2004-05-23 14:06:52 +00:00
|
|
|
if (modifiers == OSystem::KBD_SHIFT) {
|
|
|
|
_scrollLine = _firstLineInBuffer + _linesPerPage - 1;
|
|
|
|
updateScrollBuffer();
|
|
|
|
} else {
|
|
|
|
_currentPos = _promptStartPos;
|
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 256 + 23: // end
|
2004-05-23 14:06:52 +00:00
|
|
|
if (modifiers == OSystem::KBD_SHIFT) {
|
|
|
|
_scrollLine = _promptEndPos / _lineWidth;
|
|
|
|
if (_scrollLine < _linesPerPage - 1)
|
|
|
|
_scrollLine = _linesPerPage - 1;
|
|
|
|
updateScrollBuffer();
|
|
|
|
} else {
|
|
|
|
_currentPos = _promptEndPos;
|
|
|
|
}
|
2003-09-24 06:33:59 +00:00
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 273: // cursor up
|
|
|
|
historyScroll(+1);
|
|
|
|
break;
|
|
|
|
case 274: // cursor down
|
|
|
|
historyScroll(-1);
|
|
|
|
break;
|
|
|
|
case 275: // cursor right
|
|
|
|
if (_currentPos < _promptEndPos)
|
|
|
|
_currentPos++;
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 276: // cursor left
|
|
|
|
if (_currentPos > _promptStartPos)
|
|
|
|
_currentPos--;
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (ascii == '~' || ascii == '#') {
|
2004-11-27 01:25:25 +00:00
|
|
|
slideUpAndClose();
|
2003-09-24 06:33:59 +00:00
|
|
|
} else if (modifiers == OSystem::KBD_CTRL) {
|
|
|
|
specialKeys(keycode);
|
|
|
|
} else if (isprint((char)ascii)) {
|
2003-11-08 23:22:16 +00:00
|
|
|
for (i = _promptEndPos - 1; i >= _currentPos; i--)
|
2004-05-05 23:06:44 +00:00
|
|
|
buffer(i + 1) = buffer(i);
|
2003-09-24 06:33:59 +00:00
|
|
|
_promptEndPos++;
|
|
|
|
putchar((char)ascii);
|
|
|
|
scrollToCurrent();
|
|
|
|
}
|
2002-12-14 18:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-03 21:49:19 +00:00
|
|
|
void ConsoleDialog::insertIntoPrompt(const char* str)
|
|
|
|
{
|
|
|
|
unsigned int l = strlen(str);
|
2003-11-08 23:22:16 +00:00
|
|
|
for (int i = _promptEndPos - 1; i >= _currentPos; i--)
|
2004-05-05 23:06:44 +00:00
|
|
|
buffer(i + l) = buffer(i);
|
2003-05-03 21:49:19 +00:00
|
|
|
for (unsigned int j = 0; j < l; ++j) {
|
|
|
|
_promptEndPos++;
|
|
|
|
putcharIntern(str[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
|
2002-12-14 20:04:46 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case kSetPositionCmd:
|
2004-05-23 14:06:52 +00:00
|
|
|
int newPos = (int)data + _linesPerPage - 1 + _firstLineInBuffer;
|
2002-12-14 20:04:46 +00:00
|
|
|
if (newPos != _scrollLine) {
|
|
|
|
_scrollLine = newPos;
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::specialKeys(int keycode) {
|
2002-12-14 21:37:40 +00:00
|
|
|
switch (keycode) {
|
2003-09-24 06:33:59 +00:00
|
|
|
case 'a':
|
|
|
|
_currentPos = _promptStartPos;
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (_currentPos < _promptEndPos) {
|
|
|
|
killChar();
|
2002-12-14 21:37:40 +00:00
|
|
|
draw();
|
2003-09-24 06:33:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
_currentPos = _promptEndPos;
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
killLine();
|
|
|
|
draw();
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
killLastWord();
|
|
|
|
draw();
|
|
|
|
break;
|
2002-12-14 21:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::killChar() {
|
2002-12-14 22:25:09 +00:00
|
|
|
for (int i = _currentPos; i < _promptEndPos; i++)
|
2004-05-05 23:06:44 +00:00
|
|
|
buffer(i) = buffer(i + 1);
|
|
|
|
buffer(_promptEndPos) = ' ';
|
2002-12-14 22:25:09 +00:00
|
|
|
_promptEndPos--;
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::killLine() {
|
2002-12-14 21:37:40 +00:00
|
|
|
for (int i = _currentPos; i < _promptEndPos; i++)
|
2004-05-05 23:06:44 +00:00
|
|
|
buffer(i) = ' ';
|
2002-12-14 21:37:40 +00:00
|
|
|
_promptEndPos = _currentPos;
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::killLastWord() {
|
2002-12-14 22:25:09 +00:00
|
|
|
int cnt = 0;
|
2003-08-28 19:46:52 +00:00
|
|
|
bool space = true;
|
2002-12-14 21:37:40 +00:00
|
|
|
while (_currentPos > _promptStartPos) {
|
2004-05-05 23:06:44 +00:00
|
|
|
if (buffer(_currentPos - 1) == ' ') {
|
2003-08-28 19:46:52 +00:00
|
|
|
if (!space)
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
space = false;
|
|
|
|
_currentPos--;
|
|
|
|
cnt++;
|
2002-12-14 21:37:40 +00:00
|
|
|
}
|
2002-12-14 22:25:09 +00:00
|
|
|
|
|
|
|
for (int i = _currentPos; i < _promptEndPos; i++)
|
2004-05-05 23:06:44 +00:00
|
|
|
buffer(i) = buffer(i + cnt);
|
|
|
|
buffer(_promptEndPos) = ' ';
|
2003-05-03 00:08:38 +00:00
|
|
|
_promptEndPos -= cnt;
|
2002-12-14 21:37:40 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::addToHistory(const char *str) {
|
2002-12-15 02:24:32 +00:00
|
|
|
strcpy(_history[_historyIndex], str);
|
|
|
|
_historyIndex = (_historyIndex + 1) % kHistorySize;
|
|
|
|
_historyLine = 0;
|
|
|
|
if (_historySize < kHistorySize)
|
|
|
|
_historySize++;
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::historyScroll(int direction) {
|
2002-12-15 02:24:32 +00:00
|
|
|
if (_historySize == 0)
|
|
|
|
return;
|
2002-12-15 12:56:13 +00:00
|
|
|
|
|
|
|
if (_historyLine == 0 && direction > 0) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < _promptEndPos - _promptStartPos; i++)
|
2004-05-05 23:06:44 +00:00
|
|
|
_history[_historyIndex][i] = buffer(_promptStartPos + i);
|
2002-12-15 12:56:13 +00:00
|
|
|
_history[_historyIndex][i] = '\0';
|
|
|
|
}
|
2003-11-08 23:22:16 +00:00
|
|
|
|
2002-12-15 02:24:32 +00:00
|
|
|
// 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
|
2002-12-15 12:56:13 +00:00
|
|
|
int idx;
|
|
|
|
if (_historyLine > 0)
|
|
|
|
idx = (_historyIndex - _historyLine + _historySize) % _historySize;
|
|
|
|
else
|
|
|
|
idx = _historyIndex;
|
|
|
|
for (int i = 0; i < kLineBufferSize && _history[idx][i] != '\0'; i++)
|
|
|
|
putcharIntern(_history[idx][i]);
|
|
|
|
_promptEndPos = _currentPos;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-12-15 12:56:13 +00:00
|
|
|
// Ensure once more the caret is visible (in case of very long history entries)
|
|
|
|
scrollToCurrent();
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-12-15 02:24:32 +00:00
|
|
|
draw();
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::nextLine() {
|
2002-12-14 21:37:40 +00:00
|
|
|
int line = _currentPos / _lineWidth;
|
|
|
|
if (line == _scrollLine)
|
2002-12-14 15:45:45 +00:00
|
|
|
_scrollLine++;
|
2002-12-14 21:37:40 +00:00
|
|
|
_currentPos = (line + 1) * _lineWidth;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2004-05-23 14:06:52 +00:00
|
|
|
updateScrollBuffer();
|
2002-12-14 20:04:46 +00:00
|
|
|
}
|
|
|
|
|
2004-05-23 14:06:52 +00:00
|
|
|
|
|
|
|
// Call this (at least) when the current line changes or when
|
|
|
|
// a new line is added
|
|
|
|
void ConsoleDialog::updateScrollBuffer() {
|
|
|
|
int lastchar = MAX(_promptEndPos, _currentPos);
|
|
|
|
int line = lastchar / _lineWidth;
|
|
|
|
int numlines = (line < _linesInBuffer) ? line + 1 : _linesInBuffer;
|
|
|
|
int firstline = line - numlines + 1;
|
|
|
|
if (firstline > _firstLineInBuffer) {
|
|
|
|
// clear old line from buffer
|
|
|
|
for (int i = lastchar; i < (line+1) * _lineWidth; ++i)
|
|
|
|
buffer(i) = ' ';
|
|
|
|
_firstLineInBuffer = firstline;
|
|
|
|
}
|
|
|
|
|
|
|
|
_scrollBar->_numEntries = numlines;
|
2002-12-14 21:37:40 +00:00
|
|
|
_scrollBar->_currentPos = _scrollBar->_numEntries - (line - _scrollLine + _linesPerPage);
|
2002-12-14 20:04:46 +00:00
|
|
|
_scrollBar->_entriesPerPage = _linesPerPage;
|
|
|
|
_scrollBar->recalc();
|
2002-12-14 15:45:45 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
int ConsoleDialog::printf(const char *format, ...) {
|
2002-12-14 15:45:45 +00:00
|
|
|
va_list argptr;
|
|
|
|
|
|
|
|
va_start(argptr, format);
|
|
|
|
int count = this->vprintf(format, argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
int ConsoleDialog::vprintf(const char *format, va_list argptr) {
|
2003-04-30 13:57:57 +00:00
|
|
|
#ifdef __PALM_OS__
|
|
|
|
char buf[256];
|
|
|
|
#else
|
2002-12-14 15:45:45 +00:00
|
|
|
char buf[2048];
|
2003-04-30 13:57:57 +00:00
|
|
|
#endif
|
2002-12-14 15:45:45 +00:00
|
|
|
|
2002-12-16 19:53:41 +00:00
|
|
|
#if defined(WIN32)
|
|
|
|
int count = _vsnprintf(buf, sizeof(buf), format, argptr);
|
|
|
|
#else
|
2002-12-14 15:45:45 +00:00
|
|
|
int count = vsnprintf(buf, sizeof(buf), format, argptr);
|
2002-12-16 19:53:41 +00:00
|
|
|
#endif
|
2002-12-14 15:45:45 +00:00
|
|
|
print(buf);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::putchar(int c) {
|
2002-12-14 21:57:30 +00:00
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
|
|
|
putcharIntern(c);
|
|
|
|
draw(); // FIXME - not nice to redraw the full console just for one char!
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::putcharIntern(int c) {
|
2002-12-14 15:45:45 +00:00
|
|
|
if (c == '\n')
|
|
|
|
nextLine();
|
|
|
|
else {
|
2004-05-05 23:06:44 +00:00
|
|
|
buffer(_currentPos) = (char)c;
|
2002-12-14 21:37:40 +00:00
|
|
|
_currentPos++;
|
|
|
|
if ((_scrollLine + 1) * _lineWidth == _currentPos) {
|
|
|
|
_scrollLine++;
|
2004-05-23 14:06:52 +00:00
|
|
|
updateScrollBuffer();
|
2002-12-14 21:37:40 +00:00
|
|
|
}
|
2002-12-14 15:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::print(const char *str) {
|
2002-12-14 21:57:30 +00:00
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
2002-12-14 21:37:40 +00:00
|
|
|
while (*str)
|
2002-12-14 21:57:30 +00:00
|
|
|
putcharIntern(*str++);
|
|
|
|
|
|
|
|
draw();
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::drawCaret(bool erase) {
|
2002-12-14 21:37:40 +00:00
|
|
|
int line = _currentPos / _lineWidth;
|
|
|
|
int displayLine = line - _scrollLine + _linesPerPage - 1;
|
2002-12-14 18:57:15 +00:00
|
|
|
|
2002-12-14 21:57:30 +00:00
|
|
|
// Only draw caret if visible
|
|
|
|
if (!isVisible() || displayLine < 0 || displayLine >= _linesPerPage) {
|
|
|
|
_caretVisible = false;
|
2002-12-14 18:57:15 +00:00
|
|
|
return;
|
2002-12-14 21:57:30 +00:00
|
|
|
}
|
2002-12-14 18:57:15 +00:00
|
|
|
|
2004-08-15 14:39:35 +00:00
|
|
|
int x = _x + 1 + (_currentPos % _lineWidth) * kConsoleCharWidth;
|
|
|
|
int y = _y + displayLine * kConsoleLineHeight;
|
2002-12-14 18:57:15 +00:00
|
|
|
|
2004-05-05 23:06:44 +00:00
|
|
|
char c = buffer(_currentPos);
|
2002-12-14 21:57:30 +00:00
|
|
|
if (erase) {
|
2004-08-15 14:39:35 +00:00
|
|
|
g_gui.fillRect(x, y, kConsoleCharWidth, kConsoleLineHeight, g_gui._bgcolor);
|
|
|
|
g_gui.drawChar(c, x, y + 2, g_gui._textcolor, &g_consolefont);
|
2002-12-14 21:57:30 +00:00
|
|
|
} else {
|
2004-08-15 14:39:35 +00:00
|
|
|
g_gui.fillRect(x, y, kConsoleCharWidth, kConsoleLineHeight, g_gui._textcolor);
|
|
|
|
g_gui.drawChar(c, x, y + 2, g_gui._bgcolor, &g_consolefont);
|
2002-12-14 21:57:30 +00:00
|
|
|
}
|
2004-08-15 14:39:35 +00:00
|
|
|
g_gui.addDirtyRect(x, y, kConsoleCharWidth, kConsoleLineHeight);
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-12-14 18:57:15 +00:00
|
|
|
_caretVisible = !erase;
|
2002-12-14 14:31:44 +00:00
|
|
|
}
|
2002-12-14 22:10:37 +00:00
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void ConsoleDialog::scrollToCurrent() {
|
2004-05-23 14:06:52 +00:00
|
|
|
int line = _promptEndPos / _lineWidth;
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2003-11-28 22:08:52 +00:00
|
|
|
if (line + _linesPerPage <= _scrollLine) {
|
2002-12-14 22:10:37 +00:00
|
|
|
// TODO - this should only occur for loong edit lines, though
|
2003-11-28 22:08:52 +00:00
|
|
|
} else if (line > _scrollLine) {
|
|
|
|
_scrollLine = line;
|
2004-05-23 14:06:52 +00:00
|
|
|
updateScrollBuffer();
|
2002-12-14 22:10:37 +00:00
|
|
|
}
|
|
|
|
}
|
2003-11-10 23:40:48 +00:00
|
|
|
|
|
|
|
} // End of namespace GUI
|