2005-01-29 16:30:51 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2005-2006 The ScummVM project
|
2005-01-29 16:30:51 +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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-01-29 16:30:51 +00:00
|
|
|
*
|
2006-02-11 10:08:56 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2005-01-29 16:30:51 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2005-01-29 16:30:51 +00:00
|
|
|
#include "gui/editable.h"
|
|
|
|
#include "gui/newgui.h"
|
|
|
|
|
|
|
|
namespace GUI {
|
|
|
|
|
2005-05-20 15:03:26 +00:00
|
|
|
EditableWidget::EditableWidget(GuiObject *boss, int x, int y, int w, int h, WidgetSize ws)
|
2005-01-29 16:30:51 +00:00
|
|
|
: Widget(boss, x, y, w, h) {
|
2006-03-07 05:39:52 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2006-03-08 01:42:02 +00:00
|
|
|
EditableWidget::EditableWidget(GuiObject *boss, String name)
|
2006-03-07 05:39:52 +00:00
|
|
|
: Widget(boss, name) {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditableWidget::init() {
|
2005-01-29 16:30:51 +00:00
|
|
|
_caretVisible = false;
|
|
|
|
_caretTime = 0;
|
|
|
|
_caretPos = 0; // FIXME
|
|
|
|
|
|
|
|
_caretInverse = false;
|
|
|
|
|
|
|
|
_editScrollOffset = 0;
|
2006-05-27 05:46:04 +00:00
|
|
|
|
|
|
|
_font = Theme::kFontStyleBold;
|
2005-01-29 16:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EditableWidget::~EditableWidget() {
|
|
|
|
}
|
|
|
|
|
2005-01-29 18:04:34 +00:00
|
|
|
void EditableWidget::setEditString(const String &str) {
|
|
|
|
// TODO: We probably should filter the input string here,
|
|
|
|
// e.g. using tryInsertChar.
|
|
|
|
_editString = str;
|
|
|
|
_caretPos = _editString.size();
|
|
|
|
|
2006-05-27 05:46:04 +00:00
|
|
|
_editScrollOffset = (g_gui.getStringWidth(_editString) - (getEditRect().width()), _font);
|
2005-01-29 18:04:34 +00:00
|
|
|
if (_editScrollOffset < 0)
|
|
|
|
_editScrollOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditableWidget::tryInsertChar(char c, int pos) {
|
|
|
|
if (isprint(c)) {
|
|
|
|
_editString.insertChar(c, pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
void EditableWidget::handleTickle() {
|
|
|
|
uint32 time = getMillis();
|
|
|
|
if (_caretTime < time) {
|
|
|
|
_caretTime = time + kCaretBlinkTime;
|
|
|
|
drawCaret(_caretVisible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-29 18:04:34 +00:00
|
|
|
bool EditableWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
|
|
|
|
bool handled = true;
|
|
|
|
bool dirty = false;
|
|
|
|
|
|
|
|
// First remove caret
|
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
|
|
|
switch (keycode) {
|
|
|
|
case '\n': // enter/return
|
|
|
|
case '\r':
|
|
|
|
// confirm edit and exit editmode
|
|
|
|
endEditMode();
|
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
case 27: // escape
|
|
|
|
abortEditMode();
|
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
case 8: // backspace
|
|
|
|
if (_caretPos > 0) {
|
|
|
|
_caretPos--;
|
|
|
|
_editString.deleteChar(_caretPos);
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 127: // delete
|
|
|
|
_editString.deleteChar(_caretPos);
|
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
case 256 + 20: // left arrow
|
|
|
|
if (_caretPos > 0) {
|
|
|
|
dirty = setCaretPos(_caretPos - 1);
|
|
|
|
}
|
2006-05-27 05:46:04 +00:00
|
|
|
dirty = true;
|
2005-01-29 18:04:34 +00:00
|
|
|
break;
|
|
|
|
case 256 + 19: // right arrow
|
|
|
|
if (_caretPos < (int)_editString.size()) {
|
|
|
|
dirty = setCaretPos(_caretPos + 1);
|
|
|
|
}
|
2006-05-27 05:46:04 +00:00
|
|
|
dirty = true;
|
2005-01-29 18:04:34 +00:00
|
|
|
break;
|
|
|
|
case 256 + 22: // home
|
|
|
|
dirty = setCaretPos(0);
|
|
|
|
break;
|
|
|
|
case 256 + 23: // end
|
|
|
|
dirty = setCaretPos(_editString.size());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (tryInsertChar((char)ascii, _caretPos)) {
|
|
|
|
_caretPos++;
|
|
|
|
dirty = true;
|
|
|
|
} else {
|
|
|
|
handled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirty)
|
|
|
|
draw();
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EditableWidget::getCaretOffset() const {
|
|
|
|
int caretpos = 0;
|
|
|
|
for (int i = 0; i < _caretPos; i++)
|
2006-05-27 05:46:04 +00:00
|
|
|
caretpos += g_gui.getCharWidth(_editString[i], _font);
|
2005-01-29 18:04:34 +00:00
|
|
|
|
|
|
|
caretpos -= _editScrollOffset;
|
|
|
|
|
|
|
|
return caretpos;
|
|
|
|
}
|
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
void EditableWidget::drawCaret(bool erase) {
|
|
|
|
// Only draw if item is visible
|
|
|
|
if (!isVisible() || !_boss->isVisible())
|
|
|
|
return;
|
|
|
|
|
2005-05-16 13:43:31 +00:00
|
|
|
Common::Rect editRect = getEditRect();
|
|
|
|
|
|
|
|
int x = editRect.left;
|
2005-05-17 06:19:11 +00:00
|
|
|
int y = editRect.top + 1;
|
2005-01-29 16:30:51 +00:00
|
|
|
|
|
|
|
x += getCaretOffset();
|
|
|
|
|
2005-05-17 06:19:11 +00:00
|
|
|
if (y < 0 || y + editRect.height() - 2 >= _h)
|
2005-01-29 16:30:51 +00:00
|
|
|
return;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
x += getAbsX();
|
|
|
|
y += getAbsY();
|
|
|
|
|
2006-02-24 20:25:34 +00:00
|
|
|
g_gui.theme()->drawCaret(Common::Rect(x, y, x + 1, y + editRect.height() - 2), erase);
|
2006-01-27 15:43:23 +00:00
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
_caretVisible = !erase;
|
|
|
|
}
|
|
|
|
|
2005-01-29 18:04:34 +00:00
|
|
|
bool EditableWidget::setCaretPos(int newPos) {
|
|
|
|
assert(newPos >= 0 && newPos <= (int)_editString.size());
|
|
|
|
_caretPos = newPos;
|
|
|
|
return adjustOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditableWidget::adjustOffset() {
|
|
|
|
// check if the caret is still within the textbox; if it isn't,
|
2005-07-30 21:11:48 +00:00
|
|
|
// adjust _editScrollOffset
|
2005-01-29 18:04:34 +00:00
|
|
|
|
|
|
|
int caretpos = getCaretOffset();
|
|
|
|
const int editWidth = getEditRect().width();
|
|
|
|
|
|
|
|
if (caretpos < 0) {
|
|
|
|
// scroll left
|
|
|
|
_editScrollOffset += caretpos;
|
|
|
|
return true;
|
|
|
|
} else if (caretpos >= editWidth) {
|
|
|
|
// scroll right
|
|
|
|
_editScrollOffset -= (editWidth - caretpos);
|
|
|
|
return true;
|
|
|
|
} else if (_editScrollOffset > 0) {
|
2006-05-27 05:46:04 +00:00
|
|
|
const int strWidth = g_gui.getStringWidth(_editString, _font);
|
2005-01-29 18:04:34 +00:00
|
|
|
if (strWidth - _editScrollOffset < editWidth) {
|
|
|
|
// scroll right
|
|
|
|
_editScrollOffset = (strWidth - editWidth);
|
|
|
|
if (_editScrollOffset < 0)
|
|
|
|
_editScrollOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-29 16:30:51 +00:00
|
|
|
|
|
|
|
} // End of namespace GUI
|