Started to merge the text editing code in ListWidget and EditTextWidget

svn-id: r16692
This commit is contained in:
Max Horn 2005-01-29 16:30:51 +00:00
parent b6ea449335
commit 04fc9fe6e7
8 changed files with 319 additions and 187 deletions

View file

@ -27,58 +27,49 @@
namespace GUI {
EditTextWidget::EditTextWidget(GuiObject *boss, int x, int y, int w, int h, const String &text)
: StaticTextWidget(boss, x, y - 1, w, h + 2, text, kTextAlignLeft), _backupString(text) {
: EditableWidget(boss, x, y - 1, w, h + 2) {
_editString = text;
_backupString = text;
_flags = WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE;
_type = kEditTextWidget;
_caretVisible = false;
_caretTime = 0;
_caretPos = _editString.size();
_pos = _label.size();
_labelOffset = (g_gui.getStringWidth(_label) - (_w - 6));
if (_labelOffset < 0)
_labelOffset = 0;
_editScrollOffset = (g_gui.getStringWidth(_editString) - (getEditRect().width()));
if (_editScrollOffset < 0)
_editScrollOffset = 0;
}
void EditTextWidget::handleTickle() {
uint32 time = getMillis();
if (_caretTime < time) {
_caretTime = time + kCaretBlinkTime;
drawCaret(_caretVisible);
}
}
void EditTextWidget::handleMouseDown(int x, int y, int button, int clickCount){
void EditTextWidget::handleMouseDown(int x, int y, int button, int clickCount) {
// First remove caret
if (_caretVisible)
drawCaret(true);
NewGui *gui = &g_gui;
x += _labelOffset;
x += _editScrollOffset;
int width = 0;
uint i;
for (i = 0; i < _label.size(); ++i) {
width += gui->getCharWidth(_label[i]);
for (i = 0; i < _editString.size(); ++i) {
width += gui->getCharWidth(_editString[i]);
if (width >= x)
break;
}
_pos = i;
if (adjustOffset())
if (setCaretPos(i))
draw();
}
bool EditTextWidget::tryInsertChar(char c, int pos) {
if (isprint(c)) {
_label.insertChar(c, pos);
_editString.insertChar(c, pos);
return true;
}
return false;
}
bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
bool handled = true;
bool dirty = false;
@ -90,52 +81,44 @@ bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
switch (keycode) {
case '\n': // enter/return
case '\r':
releaseFocus();
// confirm edit and exit editmode
endEditMode();
dirty = true;
break;
case 27: // escape
_label = _backupString;
_pos = _label.size() - 1;
_labelOffset = (g_gui.getStringWidth(_label) - (_w-6));
if (_labelOffset < 0)
_labelOffset = 0;
releaseFocus();
abortEditMode();
dirty = true;
break;
case 8: // backspace
if (_pos > 0) {
_pos--;
_label.deleteChar(_pos);
if (_caretPos > 0) {
_caretPos--;
_editString.deleteChar(_caretPos);
}
dirty = true;
break;
case 127: // delete
_label.deleteChar(_pos);
_editString.deleteChar(_caretPos);
dirty = true;
break;
case 256 + 20: // left arrow
if (_pos > 0) {
_pos--;
dirty = adjustOffset();
if (_caretPos > 0) {
dirty = setCaretPos(_caretPos - 1);
}
break;
case 256 + 19: // right arrow
if (_pos < (int)_label.size()) {
_pos++;
dirty = adjustOffset();
if (_caretPos < (int)_editString.size()) {
dirty = setCaretPos(_caretPos + 1);
}
break;
case 256 + 22: // home
_pos = 0;
dirty = adjustOffset();
dirty = setCaretPos(0);
break;
case 256 + 23: // end
_pos = _label.size();
dirty = adjustOffset();
dirty = setCaretPos(_editString.size());
break;
default:
if (tryInsertChar((char)ascii, _pos)) {
_pos++;
if (tryInsertChar((char)ascii, _caretPos)) {
_caretPos++;
dirty = true;
} else {
handled = false;
@ -157,57 +140,78 @@ void EditTextWidget::drawWidget(bool hilite) {
// Draw the text
adjustOffset();
g_gui.drawString(_label, _x + 2, _y + 2, _w - 6, g_gui._textcolor, kTextAlignLeft, -_labelOffset, false);
g_gui.drawString(_editString, _x + 2, _y + 2, getEditRect().width(), g_gui._textcolor, kTextAlignLeft, -_editScrollOffset, false);
}
int EditTextWidget::getCaretPos() const {
int caretpos = 0;
for (int i = 0; i < _pos; i++)
caretpos += g_gui.getCharWidth(_label[i]);
Common::Rect EditTextWidget::getEditRect() const {
Common::Rect r(2, 1, _w - 2, _h);
return r;
}
caretpos -= _labelOffset;
int EditTextWidget::getCaretOffset() const {
int caretpos = 0;
for (int i = 0; i < _caretPos; i++)
caretpos += g_gui.getCharWidth(_editString[i]);
caretpos -= _editScrollOffset;
return caretpos;
}
void EditTextWidget::drawCaret(bool erase) {
// Only draw if item is visible
if (!isVisible() || !_boss->isVisible())
return;
void EditTextWidget::receivedFocusWidget() {
}
int16 color = erase ? g_gui._bgcolor : g_gui._textcolorhi;
int x = getAbsX() + 2;
int y = getAbsY() + 1;
void EditTextWidget::lostFocusWidget() {
// If we loose focus, 'commit' the user changes
_backupString = _editString;
drawCaret(true);
}
x += getCaretPos();
void EditTextWidget::startEditMode() {
}
g_gui.vLine(x, y, y + kLineHeight, color);
g_gui.addDirtyRect(x, y, 2, kLineHeight);
void EditTextWidget::endEditMode() {
releaseFocus();
}
_caretVisible = !erase;
void EditTextWidget::abortEditMode() {
_editString = _backupString;
_caretPos = _editString.size();
_editScrollOffset = (g_gui.getStringWidth(_editString) - (getEditRect().width()));
if (_editScrollOffset < 0)
_editScrollOffset = 0;
releaseFocus();
}
bool EditTextWidget::setCaretPos(int newPos) {
assert(newPos >= 0 && newPos <= (int)_editString.size());
_caretPos = newPos;
return adjustOffset();
}
bool EditTextWidget::adjustOffset() {
// check if the caret is still within the textbox; if it isn't,
// adjust _labelOffset
// adjust _editScrollOffset
int caretpos = getCaretPos();
int caretpos = getCaretOffset();
const int editWidth = getEditRect().width();
if (caretpos < 0) {
// scroll left
_labelOffset += caretpos;
_editScrollOffset += caretpos;
return true;
} else if (caretpos >= _w - 6) {
} else if (caretpos >= editWidth) {
// scroll right
_labelOffset -= (_w - 6 - caretpos);
_editScrollOffset -= (editWidth - caretpos);
return true;
} else if (_labelOffset > 0) {
int width = g_gui.getStringWidth(_label);
if (width - _labelOffset < (_w - 6)) {
} else if (_editScrollOffset > 0) {
const int strWidth = g_gui.getStringWidth(_editString);
if (strWidth - _editScrollOffset < editWidth) {
// scroll right
_labelOffset = (width - (_w - 6));
if (_labelOffset < 0)
_labelOffset = 0;
_editScrollOffset = (strWidth - editWidth);
if (_editScrollOffset < 0)
_editScrollOffset = 0;
}
}

View file

@ -18,29 +18,27 @@
* $Header$
*/
#ifndef EDITTEXTWIDGET_H
#define EDITTEXTWIDGET_H
#ifndef GUI_EDITTEXTWIDGET_H
#define GUI_EDITTEXTWIDGET_H
#include "gui/widget.h"
#include "gui/editable.h"
#include "common/str.h"
namespace GUI {
/* EditTextWidget */
class EditTextWidget : public StaticTextWidget {
public:
typedef Common::StringList StringList;
typedef Common::String String;
class EditTextWidget : public EditableWidget {
protected:
typedef Common::String String;
String _backupString;
bool _caretVisible;
uint32 _caretTime;
int _pos;
int _labelOffset;
public:
EditTextWidget(GuiObject *boss, int x, int y, int w, int h, const String &text);
virtual void handleTickle();
// void setString(const String &str) { _editString = str; }
const String &getString() const { return _editString; }
virtual void handleMouseDown(int x, int y, int button, int clickCount);
virtual bool handleKeyDown(uint16 ascii, int keycode, int modifiers);
@ -48,10 +46,16 @@ public:
protected:
void drawWidget(bool hilite);
void drawCaret(bool erase);
void lostFocusWidget() { _backupString = _label; drawCaret(true); }
void receivedFocusWidget();
void lostFocusWidget();
void startEditMode();
void endEditMode();
void abortEditMode();
int getCaretPos() const;
Common::Rect getEditRect() const;
int getCaretOffset() const;
bool setCaretPos(int newPos);
bool adjustOffset();
virtual bool tryInsertChar(char c, int pos);

View file

@ -28,7 +28,7 @@
namespace GUI {
ListWidget::ListWidget(GuiObject *boss, int x, int y, int w, int h)
: Widget(boss, x, y, w - kScrollBarWidth, h), CommandSender(boss) {
: EditableWidget(boss, x, y, w - kScrollBarWidth, h), CommandSender(boss) {
_flags = WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE;
_type = kListWidget;
_numberingMode = kListNumberingOne;
@ -38,16 +38,14 @@ ListWidget::ListWidget(GuiObject *boss, int x, int y, int w, int h)
_scrollBar = new ScrollBarWidget(boss, _x + _w, _y, kScrollBarWidth, _h);
_scrollBar->setTarget(this);
_currentKeyDown = 0;
_caretVisible = false;
_caretTime = 0;
_quickSelectTime = 0;
// The item is selected, thus _bgcolor is used to draw the caret and _textcolorhi to erase it
_caretInverse = true;
// FIXME: This flag should come from widget definition
_editable = true;
_editMode = false;
}
ListWidget::~ListWidget() {
@ -57,16 +55,10 @@ void ListWidget::setSelected(int item) {
assert(item >= -1 && item < (int)_list.size());
if (isEnabled() && _selectedItem != item) {
int oldSelectedItem = _selectedItem;
if (_editMode)
abortEditMode();
_selectedItem = item;
if (_editMode) {
// undo any changes made
_list[oldSelectedItem] = _backupString;
_editMode = false;
drawCaret(true);
}
sendCommand(kListSelectionChangedCmd, _selectedItem);
_currentPos = _selectedItem - _entriesPerPage / 2;
@ -110,31 +102,31 @@ void ListWidget::scrollBarRecalc() {
}
void ListWidget::handleTickle() {
uint32 time = getMillis();
if (_editMode && _caretTime < time) {
_caretTime = time + kCaretBlinkTime;
drawCaret(_caretVisible);
}
if (_editMode)
EditableWidget::handleTickle();
}
void ListWidget::handleMouseDown(int x, int y, int button, int clickCount) {
if (isEnabled()) {
int oldSelectedItem = _selectedItem;
_selectedItem = (y - 1) / kLineHeight + _currentPos;
if (_selectedItem > (int)_list.size() - 1)
_selectedItem = -1;
if (!isEnabled())
return;
if (oldSelectedItem != _selectedItem) {
if (_editMode) {
// undo any changes made
_list[oldSelectedItem] = _backupString;
_editMode = false;
drawCaret(true);
}
sendCommand(kListSelectionChangedCmd, _selectedItem);
}
draw();
// First check whether the selection changed
int newSelectedItem;
newSelectedItem = (y - 1) / kLineHeight + _currentPos;
if (newSelectedItem > (int)_list.size() - 1)
newSelectedItem = -1;
if (_selectedItem != newSelectedItem) {
if (_editMode)
abortEditMode();
_selectedItem = newSelectedItem;
sendCommand(kListSelectionChangedCmd, _selectedItem);
}
// TODO: Determine where inside the string the user clicked and place the
// caret accordingly.
draw();
}
void ListWidget::handleMouseUp(int x, int y, int button, int clickCount) {
@ -209,23 +201,21 @@ bool ListWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
case '\n': // enter/return
case '\r':
// confirm edit and exit editmode
_editMode = false;
endEditMode();
dirty = true;
sendCommand(kListItemActivatedCmd, _selectedItem);
break;
case 27: // escape
// abort edit and exit editmode
_editMode = false;
abortEditMode();
dirty = true;
_list[_selectedItem] = _backupString;
break;
case 8: // backspace
_list[_selectedItem].deleteLastChar();
_editString.deleteLastChar();
dirty = true;
break;
default:
if (isprint((char)ascii)) {
_list[_selectedItem] += (char)ascii;
_editString += (char)ascii;
dirty = true;
} else {
handled = false;
@ -242,8 +232,7 @@ bool ListWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
// override continuous enter keydown
if (_editable && (_currentKeyDown != '\n' && _currentKeyDown != '\r')) {
dirty = true;
_editMode = true;
_backupString = _list[_selectedItem];
startEditMode();
} else
sendCommand(kListItemActivatedCmd, _selectedItem);
}
@ -303,6 +292,7 @@ bool ListWidget::handleKeyUp(uint16 ascii, int keycode, int modifiers) {
}
void ListWidget::lostFocusWidget() {
// If we loose focus, we simply forget the user changes
_editMode = false;
drawCaret(true);
draw();
@ -331,13 +321,17 @@ void ListWidget::drawWidget(bool hilite) {
// Draw the list items
for (i = 0, pos = _currentPos; i < _entriesPerPage && pos < len; i++, pos++) {
if (_numberingMode == kListNumberingZero || _numberingMode == kListNumberingOne) {
if (_numberingMode != kListNumberingOff) {
char temp[10];
sprintf(temp, "%2d. ", (pos + _numberingMode));
buffer = temp;
} else {
buffer.clear();
}
if (_selectedItem == pos && _editMode)
buffer += _editString;
else
buffer += _list[pos];
} else
buffer = _list[pos];
if (_selectedItem == pos) {
if (_hasFocus)
@ -350,43 +344,27 @@ void ListWidget::drawWidget(bool hilite) {
}
}
int ListWidget::getCaretPos() const {
int caretpos = 0;
NewGui *gui = &g_gui;
Common::Rect ListWidget::getEditRect() const {
Common::Rect r(2, 1, _w - 2 , kLineHeight);
const int offset = (_selectedItem - _currentPos) * kLineHeight;
r.top += offset;
r.bottom += offset;
if (_numberingMode == kListNumberingZero || _numberingMode == kListNumberingOne) {
if (_numberingMode != kListNumberingOff) {
char temp[10];
sprintf(temp, "%2d. ", (_selectedItem + _numberingMode));
caretpos += gui->getStringWidth(temp);
r.left += g_gui.getStringWidth(temp);
}
caretpos += gui->getStringWidth(_list[_selectedItem]);
return caretpos;
return r;
}
void ListWidget::drawCaret(bool erase) {
// Only draw if item is visible
if (_selectedItem < _currentPos || _selectedItem >= _currentPos + _entriesPerPage)
return;
if (!isVisible() || !_boss->isVisible())
return;
int ListWidget::getCaretOffset() const {
int caretpos = 0;
NewGui *gui = &g_gui;
// The item is selected, thus _bgcolor is used to draw the caret and _textcolorhi to erase it
int16 color = erase ? gui->_textcolorhi : gui->_bgcolor;
int x = getAbsX() + 3;
int y = getAbsY() + 1;
y += (_selectedItem - _currentPos) * kLineHeight;
x += getCaretPos();
gui->vLine(x, y, y+kLineHeight, color);
gui->addDirtyRect(x, y, 2, kLineHeight);
caretpos += g_gui.getStringWidth(_editString);
_caretVisible = !erase;
return caretpos;
}
void ListWidget::scrollToCurrent() {
@ -411,18 +389,25 @@ void ListWidget::scrollToCurrent() {
void ListWidget::startEditMode() {
if (_editable && !_editMode && _selectedItem >= 0) {
_editMode = true;
_backupString = _list[_selectedItem];
_editString = _list[_selectedItem];
_caretPos = _editString.size();
draw();
}
}
void ListWidget::endEditMode() {
// send a message that editing finished with a return/enter key press
_editMode = false;
_list[_selectedItem] = _editString;
sendCommand(kListItemActivatedCmd, _selectedItem);
}
void ListWidget::abortEditMode() {
if (_editMode) {
_editMode = false;
_list[_selectedItem] = _backupString;
drawCaret(true);
draw();
}
// undo any changes made
assert(_selectedItem >= 0);
_editMode = false;
//drawCaret(true);
//draw();
}
} // End of namespace GUI

View file

@ -18,10 +18,10 @@
* $Header$
*/
#ifndef LISTWIDGET_H
#define LISTWIDGET_H
#ifndef GUI_LISTWIDGET_H
#define GUI_LISTWIDGET_H
#include "gui/widget.h"
#include "gui/editable.h"
#include "common/str.h"
namespace GUI {
@ -42,9 +42,10 @@ enum {
};
/* ListWidget */
class ListWidget : public Widget, public CommandSender {
typedef Common::StringList StringList;
class ListWidget : public EditableWidget, public CommandSender {
public:
typedef Common::String String;
typedef Common::StringList StringList;
protected:
StringList _list;
bool _editable;
@ -55,13 +56,10 @@ protected:
int _selectedItem;
ScrollBarWidget *_scrollBar;
int _currentKeyDown;
String _backupString;
bool _caretVisible;
uint32 _caretTime;
String _quickSelectStr;
uint32 _quickSelectTime;
public:
ListWidget(GuiObject *boss, int x, int y, int w, int h);
virtual ~ListWidget();
@ -86,16 +84,19 @@ public:
virtual bool wantsFocus() { return true; };
void scrollBarRecalc();
// Made startEditMode for SCUMM's SaveLoadChooser
void startEditMode();
void abortEditMode();
protected:
void drawWidget(bool hilite);
void scrollBarRecalc();
void endEditMode();
void abortEditMode();
int getCaretPos() const;
void drawCaret(bool erase);
Common::Rect getEditRect() const;
int getCaretOffset() const;
void lostFocusWidget();
void scrollToCurrent();

74
gui/editable.cpp Normal file
View file

@ -0,0 +1,74 @@
/* ScummVM - Scumm Interpreter
* Copyright (C) 2002-2005 The ScummVM project
*
* 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 "gui/editable.h"
#include "gui/newgui.h"
namespace GUI {
EditableWidget::EditableWidget(GuiObject *boss, int x, int y, int w, int h)
: Widget(boss, x, y, w, h) {
_caretVisible = false;
_caretTime = 0;
_caretPos = 0; // FIXME
_caretInverse = false;
_editScrollOffset = 0;
}
EditableWidget::~EditableWidget() {
}
void EditableWidget::handleTickle() {
uint32 time = getMillis();
if (_caretTime < time) {
_caretTime = time + kCaretBlinkTime;
drawCaret(_caretVisible);
}
}
void EditableWidget::drawCaret(bool erase) {
// Only draw if item is visible
if (!isVisible() || !_boss->isVisible())
return;
int16 color = (erase ^ _caretInverse) ? g_gui._bgcolor : g_gui._textcolorhi;
int x = getEditRect().left;
int y = getEditRect().top;
x += getCaretOffset();
if (y < 0 || y + kLineHeight >= _h)
return;
x += getAbsX();
y += getAbsY();
g_gui.vLine(x, y, y + kLineHeight, color);
g_gui.addDirtyRect(x, y, 2, kLineHeight);
_caretVisible = !erase;
}
} // End of namespace GUI

63
gui/editable.h Normal file
View file

@ -0,0 +1,63 @@
/* ScummVM - Scumm Interpreter
* Copyright (C) 2002-2005 The ScummVM project
*
* 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$
*/
#ifndef GUI_EDITABLE_H
#define GUI_EDITABLE_H
#include "common/str.h"
#include "common/rect.h"
#include "gui/widget.h"
namespace GUI {
class EditableWidget : public Widget {
public:
typedef Common::String String;
protected:
String _editString;
bool _caretVisible;
uint32 _caretTime;
int _caretPos;
bool _caretInverse;
int _editScrollOffset;
public:
EditableWidget(GuiObject *boss, int x, int y, int w, int h);
virtual ~EditableWidget();
virtual void handleTickle();
protected:
virtual void startEditMode() = 0;
virtual void endEditMode() = 0;
virtual void abortEditMode() = 0;
virtual Common::Rect getEditRect() const = 0;
virtual int getCaretOffset() const = 0;
void drawCaret(bool erase);
};
} // End of namespace GUI
#endif

View file

@ -82,7 +82,7 @@ public:
protected:
bool tryInsertChar(char c, int pos) {
if (isalnum(c) || c == '-' || c == '_') {
_label.insertChar(c, pos);
_editString.insertChar(c, pos);
return true;
}
return false;
@ -303,7 +303,7 @@ void EditGameDialog::open() {
void EditGameDialog::close() {
if (getResult()) {
ConfMan.set("description", _descriptionWidget->getLabel(), _domain);
ConfMan.set("description", _descriptionWidget->getString(), _domain);
Common::Language lang = (Common::Language)_langPopUp->getSelectedTag();
if (lang < 0)
@ -389,7 +389,7 @@ void EditGameDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 dat
case kOKCmd: {
// Write back changes made to config object
String newDomain(_domainWidget->getLabel());
String newDomain(_domainWidget->getString());
if (newDomain != _domain) {
if (newDomain.isEmpty() || ConfMan.hasGameDomain(newDomain)) {
MessageDialog alert("This game ID is already taken. Please choose another one.");

View file

@ -7,6 +7,7 @@ MODULE_OBJS := \
gui/console.o \
gui/consolefont.o \
gui/dialog.o \
gui/editable.o \
gui/EditTextWidget.o \
gui/launcher.o \
gui/ListWidget.o \