2002-11-21 12:48:50 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2003-03-06 21:46:56 +00:00
|
|
|
* Copyright (C) 2002-2003 The ScummVM project
|
2002-11-21 12:48:50 +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"
|
|
|
|
#include "EditTextWidget.h"
|
|
|
|
#include "dialog.h"
|
|
|
|
#include "newgui.h"
|
2002-11-21 15:20:52 +00:00
|
|
|
|
2003-11-02 14:50:53 +00:00
|
|
|
EditTextWidget::EditTextWidget(GuiObject *boss, int x, int y, int w, int h, const String &text)
|
2003-03-06 19:52:54 +00:00
|
|
|
: StaticTextWidget(boss, x, y-1, w, h+2, text, kTextAlignLeft), _backupString(text) {
|
2002-11-21 15:20:52 +00:00
|
|
|
_flags = WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE;
|
|
|
|
_type = kEditTextWidget;
|
|
|
|
|
|
|
|
_caretVisible = false;
|
|
|
|
_caretTime = 0;
|
2003-01-10 21:33:42 +00:00
|
|
|
|
|
|
|
_pos = _label.size();
|
2003-05-05 16:10:19 +00:00
|
|
|
|
2003-11-02 02:18:16 +00:00
|
|
|
_labelOffset = (g_gui.getStringWidth(_label) - (_w - 6));
|
2003-05-05 16:10:19 +00:00
|
|
|
if (_labelOffset < 0)
|
|
|
|
_labelOffset = 0;
|
2002-11-21 15:20:52 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void EditTextWidget::handleTickle() {
|
2003-11-02 02:18:16 +00:00
|
|
|
uint32 time = g_system->get_msecs();
|
2002-11-21 15:20:52 +00:00
|
|
|
if (_caretTime < time) {
|
|
|
|
_caretTime = time + kCaretBlinkTime;
|
|
|
|
if (_caretVisible) {
|
|
|
|
drawCaret(true);
|
|
|
|
} else {
|
|
|
|
drawCaret(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-05 16:12:45 +00:00
|
|
|
void EditTextWidget::handleMouseDown(int x, int y, int button, int clickCount){
|
|
|
|
// First remove caret
|
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
2003-11-02 02:18:16 +00:00
|
|
|
NewGui *gui = &g_gui;
|
2003-05-05 16:12:45 +00:00
|
|
|
|
|
|
|
x += _labelOffset;
|
|
|
|
|
|
|
|
int width = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < _label.size(); ++i) {
|
|
|
|
width += gui->getCharWidth(_label[i]);
|
|
|
|
if (width >= x)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_pos = i;
|
|
|
|
if (adjustOffset())
|
|
|
|
draw();
|
2002-11-21 15:20:52 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
|
2002-11-21 15:20:52 +00:00
|
|
|
bool handled = true;
|
|
|
|
bool dirty = false;
|
|
|
|
|
|
|
|
// First remove caret
|
|
|
|
if (_caretVisible)
|
|
|
|
drawCaret(true);
|
|
|
|
|
2002-11-22 14:02:54 +00:00
|
|
|
switch (keycode) {
|
2003-09-24 06:33:59 +00:00
|
|
|
case '\n': // enter/return
|
|
|
|
case '\r':
|
2003-11-02 14:50:53 +00:00
|
|
|
releaseFocus();
|
2003-09-24 06:33:59 +00:00
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
case 27: // escape
|
|
|
|
_label = _backupString;
|
|
|
|
_pos = _label.size() - 1;
|
2003-11-02 02:18:16 +00:00
|
|
|
_labelOffset = (g_gui.getStringWidth(_label) - (_w-6));
|
2003-09-24 06:33:59 +00:00
|
|
|
if (_labelOffset < 0)
|
|
|
|
_labelOffset = 0;
|
2003-11-02 14:50:53 +00:00
|
|
|
releaseFocus();
|
2003-09-24 06:33:59 +00:00
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
case 8: // backspace
|
|
|
|
if (_pos > 0) {
|
|
|
|
_pos--;
|
2003-01-10 21:33:42 +00:00
|
|
|
_label.deleteChar(_pos);
|
2003-09-24 06:33:59 +00:00
|
|
|
}
|
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
case 127: // delete
|
|
|
|
_label.deleteChar(_pos);
|
|
|
|
dirty = true;
|
|
|
|
break;
|
|
|
|
case 256 + 20: // left arrow
|
|
|
|
if (_pos > 0) {
|
|
|
|
_pos--;
|
2003-05-05 16:10:19 +00:00
|
|
|
dirty = adjustOffset();
|
2003-09-24 06:33:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 256 + 19: // right arrow
|
|
|
|
if (_pos < _label.size()) {
|
|
|
|
_pos++;
|
2003-05-05 16:10:19 +00:00
|
|
|
dirty = adjustOffset();
|
2003-09-24 06:33:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 256 + 22: // home
|
|
|
|
_pos = 0;
|
|
|
|
dirty = adjustOffset();
|
|
|
|
break;
|
|
|
|
case 256 + 23: // end
|
|
|
|
_pos = _label.size();
|
|
|
|
dirty = adjustOffset();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isprint((char)ascii)) {
|
|
|
|
_label.insertChar((char)ascii, _pos++);
|
|
|
|
//_label += (char)ascii;
|
|
|
|
dirty = true;
|
|
|
|
} else {
|
|
|
|
handled = false;
|
|
|
|
}
|
2002-11-21 15:20:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dirty)
|
|
|
|
draw();
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-11-21 15:20:52 +00:00
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void EditTextWidget::drawWidget(bool hilite) {
|
2002-11-21 15:20:52 +00:00
|
|
|
// Draw a thin frame around us.
|
2003-11-02 02:18:16 +00:00
|
|
|
g_gui.hLine(_x, _y, _x + _w - 1, g_gui._color);
|
|
|
|
g_gui.hLine(_x, _y + _h - 1, _x +_w - 1, g_gui._shadowcolor);
|
|
|
|
g_gui.vLine(_x, _y, _y + _h - 1, g_gui._color);
|
|
|
|
g_gui.vLine(_x + _w - 1, _y, _y + _h - 1, g_gui._shadowcolor);
|
2002-11-21 15:20:52 +00:00
|
|
|
|
|
|
|
// Draw the text
|
2003-05-05 16:10:19 +00:00
|
|
|
adjustOffset();
|
2003-11-04 22:00:36 +00:00
|
|
|
g_gui.drawString(_label, _x + 2, _y + 3, _w - 6, g_gui._textcolor, kTextAlignLeft, -_labelOffset, false);
|
2003-05-05 16:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int EditTextWidget::getCaretPos() {
|
|
|
|
int caretpos = 0;
|
|
|
|
for (int i = 0; i < _pos; i++)
|
2003-11-02 02:18:16 +00:00
|
|
|
caretpos += g_gui.getCharWidth(_label[i]);
|
2003-05-05 16:10:19 +00:00
|
|
|
|
|
|
|
caretpos -= _labelOffset;
|
|
|
|
|
|
|
|
return caretpos;
|
2002-11-21 15:20:52 +00:00
|
|
|
}
|
|
|
|
|
2003-03-06 19:52:54 +00:00
|
|
|
void EditTextWidget::drawCaret(bool erase) {
|
2002-11-21 15:20:52 +00:00
|
|
|
// Only draw if item is visible
|
|
|
|
if (!isVisible() || !_boss->isVisible())
|
|
|
|
return;
|
|
|
|
|
2003-11-02 02:18:16 +00:00
|
|
|
int16 color = erase ? g_gui._bgcolor : g_gui._textcolorhi;
|
2003-11-02 17:41:01 +00:00
|
|
|
int x = getAbsX() + 2;
|
|
|
|
int y = getAbsY() + 1;
|
2002-11-21 15:20:52 +00:00
|
|
|
|
2003-05-05 16:10:19 +00:00
|
|
|
int width = getCaretPos();
|
2002-11-21 15:20:52 +00:00
|
|
|
x += width;
|
|
|
|
|
2003-11-02 02:18:16 +00:00
|
|
|
g_gui.vLine(x, y, y + kLineHeight, color);
|
|
|
|
g_gui.addDirtyRect(x, y, 2, kLineHeight);
|
2003-03-06 19:52:54 +00:00
|
|
|
|
2002-11-21 15:20:52 +00:00
|
|
|
_caretVisible = !erase;
|
|
|
|
}
|
2003-05-05 16:10:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool EditTextWidget::adjustOffset() {
|
|
|
|
// check if the caret is still within the textbox; if it isn't,
|
|
|
|
// adjust _labelOffset
|
|
|
|
|
|
|
|
int caretpos = getCaretPos();
|
|
|
|
|
|
|
|
if (caretpos < 0) {
|
|
|
|
// scroll left
|
|
|
|
_labelOffset += caretpos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (caretpos >= _w - 6)
|
|
|
|
{
|
|
|
|
// scroll right
|
|
|
|
_labelOffset -= (_w - 6 - caretpos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (_labelOffset > 0)
|
|
|
|
{
|
2003-11-02 02:18:16 +00:00
|
|
|
int width = g_gui.getStringWidth(_label);
|
2003-05-05 16:10:19 +00:00
|
|
|
if (width - _labelOffset < (_w - 6)) {
|
|
|
|
// scroll right
|
|
|
|
_labelOffset = (width - (_w - 6));
|
|
|
|
if (_labelOffset < 0)
|
|
|
|
_labelOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|