Started to merge the text editing code in ListWidget and EditTextWidget
svn-id: r16692
This commit is contained in:
parent
b6ea449335
commit
04fc9fe6e7
8 changed files with 319 additions and 187 deletions
|
@ -27,58 +27,49 @@
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
EditTextWidget::EditTextWidget(GuiObject *boss, int x, int y, int w, int h, const String &text)
|
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;
|
_flags = WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE;
|
||||||
_type = kEditTextWidget;
|
_type = kEditTextWidget;
|
||||||
|
|
||||||
_caretVisible = false;
|
_caretPos = _editString.size();
|
||||||
_caretTime = 0;
|
|
||||||
|
|
||||||
_pos = _label.size();
|
_editScrollOffset = (g_gui.getStringWidth(_editString) - (getEditRect().width()));
|
||||||
|
if (_editScrollOffset < 0)
|
||||||
_labelOffset = (g_gui.getStringWidth(_label) - (_w - 6));
|
_editScrollOffset = 0;
|
||||||
if (_labelOffset < 0)
|
|
||||||
_labelOffset = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditTextWidget::handleTickle() {
|
void EditTextWidget::handleMouseDown(int x, int y, int button, int clickCount) {
|
||||||
uint32 time = getMillis();
|
|
||||||
if (_caretTime < time) {
|
|
||||||
_caretTime = time + kCaretBlinkTime;
|
|
||||||
drawCaret(_caretVisible);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void EditTextWidget::handleMouseDown(int x, int y, int button, int clickCount){
|
|
||||||
// First remove caret
|
// First remove caret
|
||||||
if (_caretVisible)
|
if (_caretVisible)
|
||||||
drawCaret(true);
|
drawCaret(true);
|
||||||
|
|
||||||
NewGui *gui = &g_gui;
|
NewGui *gui = &g_gui;
|
||||||
|
|
||||||
x += _labelOffset;
|
x += _editScrollOffset;
|
||||||
|
|
||||||
int width = 0;
|
int width = 0;
|
||||||
uint i;
|
uint i;
|
||||||
|
|
||||||
for (i = 0; i < _label.size(); ++i) {
|
for (i = 0; i < _editString.size(); ++i) {
|
||||||
width += gui->getCharWidth(_label[i]);
|
width += gui->getCharWidth(_editString[i]);
|
||||||
if (width >= x)
|
if (width >= x)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_pos = i;
|
if (setCaretPos(i))
|
||||||
if (adjustOffset())
|
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EditTextWidget::tryInsertChar(char c, int pos) {
|
bool EditTextWidget::tryInsertChar(char c, int pos) {
|
||||||
if (isprint(c)) {
|
if (isprint(c)) {
|
||||||
_label.insertChar(c, pos);
|
_editString.insertChar(c, pos);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
|
bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
|
||||||
bool handled = true;
|
bool handled = true;
|
||||||
bool dirty = false;
|
bool dirty = false;
|
||||||
|
@ -90,52 +81,44 @@ bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
|
||||||
switch (keycode) {
|
switch (keycode) {
|
||||||
case '\n': // enter/return
|
case '\n': // enter/return
|
||||||
case '\r':
|
case '\r':
|
||||||
releaseFocus();
|
// confirm edit and exit editmode
|
||||||
|
endEditMode();
|
||||||
dirty = true;
|
dirty = true;
|
||||||
break;
|
break;
|
||||||
case 27: // escape
|
case 27: // escape
|
||||||
_label = _backupString;
|
abortEditMode();
|
||||||
_pos = _label.size() - 1;
|
|
||||||
_labelOffset = (g_gui.getStringWidth(_label) - (_w-6));
|
|
||||||
if (_labelOffset < 0)
|
|
||||||
_labelOffset = 0;
|
|
||||||
releaseFocus();
|
|
||||||
dirty = true;
|
dirty = true;
|
||||||
break;
|
break;
|
||||||
case 8: // backspace
|
case 8: // backspace
|
||||||
if (_pos > 0) {
|
if (_caretPos > 0) {
|
||||||
_pos--;
|
_caretPos--;
|
||||||
_label.deleteChar(_pos);
|
_editString.deleteChar(_caretPos);
|
||||||
}
|
}
|
||||||
dirty = true;
|
dirty = true;
|
||||||
break;
|
break;
|
||||||
case 127: // delete
|
case 127: // delete
|
||||||
_label.deleteChar(_pos);
|
_editString.deleteChar(_caretPos);
|
||||||
dirty = true;
|
dirty = true;
|
||||||
break;
|
break;
|
||||||
case 256 + 20: // left arrow
|
case 256 + 20: // left arrow
|
||||||
if (_pos > 0) {
|
if (_caretPos > 0) {
|
||||||
_pos--;
|
dirty = setCaretPos(_caretPos - 1);
|
||||||
dirty = adjustOffset();
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 256 + 19: // right arrow
|
case 256 + 19: // right arrow
|
||||||
if (_pos < (int)_label.size()) {
|
if (_caretPos < (int)_editString.size()) {
|
||||||
_pos++;
|
dirty = setCaretPos(_caretPos + 1);
|
||||||
dirty = adjustOffset();
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 256 + 22: // home
|
case 256 + 22: // home
|
||||||
_pos = 0;
|
dirty = setCaretPos(0);
|
||||||
dirty = adjustOffset();
|
|
||||||
break;
|
break;
|
||||||
case 256 + 23: // end
|
case 256 + 23: // end
|
||||||
_pos = _label.size();
|
dirty = setCaretPos(_editString.size());
|
||||||
dirty = adjustOffset();
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (tryInsertChar((char)ascii, _pos)) {
|
if (tryInsertChar((char)ascii, _caretPos)) {
|
||||||
_pos++;
|
_caretPos++;
|
||||||
dirty = true;
|
dirty = true;
|
||||||
} else {
|
} else {
|
||||||
handled = false;
|
handled = false;
|
||||||
|
@ -157,57 +140,78 @@ void EditTextWidget::drawWidget(bool hilite) {
|
||||||
|
|
||||||
// Draw the text
|
// Draw the text
|
||||||
adjustOffset();
|
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 {
|
Common::Rect EditTextWidget::getEditRect() const {
|
||||||
int caretpos = 0;
|
Common::Rect r(2, 1, _w - 2, _h);
|
||||||
for (int i = 0; i < _pos; i++)
|
|
||||||
caretpos += g_gui.getCharWidth(_label[i]);
|
|
||||||
|
|
||||||
caretpos -= _labelOffset;
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int EditTextWidget::getCaretOffset() const {
|
||||||
|
int caretpos = 0;
|
||||||
|
for (int i = 0; i < _caretPos; i++)
|
||||||
|
caretpos += g_gui.getCharWidth(_editString[i]);
|
||||||
|
|
||||||
|
caretpos -= _editScrollOffset;
|
||||||
|
|
||||||
return caretpos;
|
return caretpos;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditTextWidget::drawCaret(bool erase) {
|
void EditTextWidget::receivedFocusWidget() {
|
||||||
// Only draw if item is visible
|
}
|
||||||
if (!isVisible() || !_boss->isVisible())
|
|
||||||
return;
|
|
||||||
|
|
||||||
int16 color = erase ? g_gui._bgcolor : g_gui._textcolorhi;
|
void EditTextWidget::lostFocusWidget() {
|
||||||
int x = getAbsX() + 2;
|
// If we loose focus, 'commit' the user changes
|
||||||
int y = getAbsY() + 1;
|
_backupString = _editString;
|
||||||
|
drawCaret(true);
|
||||||
|
}
|
||||||
|
|
||||||
x += getCaretPos();
|
void EditTextWidget::startEditMode() {
|
||||||
|
}
|
||||||
|
|
||||||
g_gui.vLine(x, y, y + kLineHeight, color);
|
void EditTextWidget::endEditMode() {
|
||||||
g_gui.addDirtyRect(x, y, 2, kLineHeight);
|
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() {
|
bool EditTextWidget::adjustOffset() {
|
||||||
// check if the caret is still within the textbox; if it isn't,
|
// 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) {
|
if (caretpos < 0) {
|
||||||
// scroll left
|
// scroll left
|
||||||
_labelOffset += caretpos;
|
_editScrollOffset += caretpos;
|
||||||
return true;
|
return true;
|
||||||
} else if (caretpos >= _w - 6) {
|
} else if (caretpos >= editWidth) {
|
||||||
// scroll right
|
// scroll right
|
||||||
_labelOffset -= (_w - 6 - caretpos);
|
_editScrollOffset -= (editWidth - caretpos);
|
||||||
return true;
|
return true;
|
||||||
} else if (_labelOffset > 0) {
|
} else if (_editScrollOffset > 0) {
|
||||||
int width = g_gui.getStringWidth(_label);
|
const int strWidth = g_gui.getStringWidth(_editString);
|
||||||
if (width - _labelOffset < (_w - 6)) {
|
if (strWidth - _editScrollOffset < editWidth) {
|
||||||
// scroll right
|
// scroll right
|
||||||
_labelOffset = (width - (_w - 6));
|
_editScrollOffset = (strWidth - editWidth);
|
||||||
if (_labelOffset < 0)
|
if (_editScrollOffset < 0)
|
||||||
_labelOffset = 0;
|
_editScrollOffset = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,29 +18,27 @@
|
||||||
* $Header$
|
* $Header$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef EDITTEXTWIDGET_H
|
#ifndef GUI_EDITTEXTWIDGET_H
|
||||||
#define EDITTEXTWIDGET_H
|
#define GUI_EDITTEXTWIDGET_H
|
||||||
|
|
||||||
#include "gui/widget.h"
|
#include "gui/editable.h"
|
||||||
#include "common/str.h"
|
#include "common/str.h"
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
/* EditTextWidget */
|
/* EditTextWidget */
|
||||||
class EditTextWidget : public StaticTextWidget {
|
class EditTextWidget : public EditableWidget {
|
||||||
public:
|
|
||||||
typedef Common::StringList StringList;
|
|
||||||
typedef Common::String String;
|
|
||||||
protected:
|
protected:
|
||||||
|
typedef Common::String String;
|
||||||
|
|
||||||
String _backupString;
|
String _backupString;
|
||||||
bool _caretVisible;
|
|
||||||
uint32 _caretTime;
|
|
||||||
int _pos;
|
|
||||||
int _labelOffset;
|
|
||||||
public:
|
public:
|
||||||
EditTextWidget(GuiObject *boss, int x, int y, int w, int h, const String &text);
|
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 void handleMouseDown(int x, int y, int button, int clickCount);
|
||||||
virtual bool handleKeyDown(uint16 ascii, int keycode, int modifiers);
|
virtual bool handleKeyDown(uint16 ascii, int keycode, int modifiers);
|
||||||
|
|
||||||
|
@ -48,10 +46,16 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void drawWidget(bool hilite);
|
void drawWidget(bool hilite);
|
||||||
void drawCaret(bool erase);
|
void receivedFocusWidget();
|
||||||
void lostFocusWidget() { _backupString = _label; drawCaret(true); }
|
void lostFocusWidget();
|
||||||
|
|
||||||
int getCaretPos() const;
|
void startEditMode();
|
||||||
|
void endEditMode();
|
||||||
|
void abortEditMode();
|
||||||
|
|
||||||
|
Common::Rect getEditRect() const;
|
||||||
|
int getCaretOffset() const;
|
||||||
|
bool setCaretPos(int newPos);
|
||||||
bool adjustOffset();
|
bool adjustOffset();
|
||||||
|
|
||||||
virtual bool tryInsertChar(char c, int pos);
|
virtual bool tryInsertChar(char c, int pos);
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
ListWidget::ListWidget(GuiObject *boss, int x, int y, int w, int h)
|
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;
|
_flags = WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE;
|
||||||
_type = kListWidget;
|
_type = kListWidget;
|
||||||
_numberingMode = kListNumberingOne;
|
_numberingMode = kListNumberingOne;
|
||||||
|
@ -39,15 +39,13 @@ ListWidget::ListWidget(GuiObject *boss, int x, int y, int w, int h)
|
||||||
_scrollBar->setTarget(this);
|
_scrollBar->setTarget(this);
|
||||||
_currentKeyDown = 0;
|
_currentKeyDown = 0;
|
||||||
|
|
||||||
_caretVisible = false;
|
|
||||||
_caretTime = 0;
|
|
||||||
|
|
||||||
_quickSelectTime = 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
|
// FIXME: This flag should come from widget definition
|
||||||
_editable = true;
|
_editable = true;
|
||||||
|
|
||||||
_editMode = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ListWidget::~ListWidget() {
|
ListWidget::~ListWidget() {
|
||||||
|
@ -57,16 +55,10 @@ void ListWidget::setSelected(int item) {
|
||||||
assert(item >= -1 && item < (int)_list.size());
|
assert(item >= -1 && item < (int)_list.size());
|
||||||
|
|
||||||
if (isEnabled() && _selectedItem != item) {
|
if (isEnabled() && _selectedItem != item) {
|
||||||
int oldSelectedItem = _selectedItem;
|
if (_editMode)
|
||||||
|
abortEditMode();
|
||||||
|
|
||||||
_selectedItem = item;
|
_selectedItem = item;
|
||||||
|
|
||||||
if (_editMode) {
|
|
||||||
// undo any changes made
|
|
||||||
_list[oldSelectedItem] = _backupString;
|
|
||||||
_editMode = false;
|
|
||||||
drawCaret(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
sendCommand(kListSelectionChangedCmd, _selectedItem);
|
sendCommand(kListSelectionChangedCmd, _selectedItem);
|
||||||
|
|
||||||
_currentPos = _selectedItem - _entriesPerPage / 2;
|
_currentPos = _selectedItem - _entriesPerPage / 2;
|
||||||
|
@ -110,31 +102,31 @@ void ListWidget::scrollBarRecalc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListWidget::handleTickle() {
|
void ListWidget::handleTickle() {
|
||||||
uint32 time = getMillis();
|
if (_editMode)
|
||||||
if (_editMode && _caretTime < time) {
|
EditableWidget::handleTickle();
|
||||||
_caretTime = time + kCaretBlinkTime;
|
|
||||||
drawCaret(_caretVisible);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListWidget::handleMouseDown(int x, int y, int button, int clickCount) {
|
void ListWidget::handleMouseDown(int x, int y, int button, int clickCount) {
|
||||||
if (isEnabled()) {
|
if (!isEnabled())
|
||||||
int oldSelectedItem = _selectedItem;
|
return;
|
||||||
_selectedItem = (y - 1) / kLineHeight + _currentPos;
|
|
||||||
if (_selectedItem > (int)_list.size() - 1)
|
|
||||||
_selectedItem = -1;
|
|
||||||
|
|
||||||
if (oldSelectedItem != _selectedItem) {
|
// First check whether the selection changed
|
||||||
if (_editMode) {
|
int newSelectedItem;
|
||||||
// undo any changes made
|
newSelectedItem = (y - 1) / kLineHeight + _currentPos;
|
||||||
_list[oldSelectedItem] = _backupString;
|
if (newSelectedItem > (int)_list.size() - 1)
|
||||||
_editMode = false;
|
newSelectedItem = -1;
|
||||||
drawCaret(true);
|
|
||||||
}
|
if (_selectedItem != newSelectedItem) {
|
||||||
sendCommand(kListSelectionChangedCmd, _selectedItem);
|
if (_editMode)
|
||||||
}
|
abortEditMode();
|
||||||
draw();
|
_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) {
|
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 '\n': // enter/return
|
||||||
case '\r':
|
case '\r':
|
||||||
// confirm edit and exit editmode
|
// confirm edit and exit editmode
|
||||||
_editMode = false;
|
endEditMode();
|
||||||
dirty = true;
|
dirty = true;
|
||||||
sendCommand(kListItemActivatedCmd, _selectedItem);
|
|
||||||
break;
|
break;
|
||||||
case 27: // escape
|
case 27: // escape
|
||||||
// abort edit and exit editmode
|
// abort edit and exit editmode
|
||||||
_editMode = false;
|
abortEditMode();
|
||||||
dirty = true;
|
dirty = true;
|
||||||
_list[_selectedItem] = _backupString;
|
|
||||||
break;
|
break;
|
||||||
case 8: // backspace
|
case 8: // backspace
|
||||||
_list[_selectedItem].deleteLastChar();
|
_editString.deleteLastChar();
|
||||||
dirty = true;
|
dirty = true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (isprint((char)ascii)) {
|
if (isprint((char)ascii)) {
|
||||||
_list[_selectedItem] += (char)ascii;
|
_editString += (char)ascii;
|
||||||
dirty = true;
|
dirty = true;
|
||||||
} else {
|
} else {
|
||||||
handled = false;
|
handled = false;
|
||||||
|
@ -242,8 +232,7 @@ bool ListWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
|
||||||
// override continuous enter keydown
|
// override continuous enter keydown
|
||||||
if (_editable && (_currentKeyDown != '\n' && _currentKeyDown != '\r')) {
|
if (_editable && (_currentKeyDown != '\n' && _currentKeyDown != '\r')) {
|
||||||
dirty = true;
|
dirty = true;
|
||||||
_editMode = true;
|
startEditMode();
|
||||||
_backupString = _list[_selectedItem];
|
|
||||||
} else
|
} else
|
||||||
sendCommand(kListItemActivatedCmd, _selectedItem);
|
sendCommand(kListItemActivatedCmd, _selectedItem);
|
||||||
}
|
}
|
||||||
|
@ -303,6 +292,7 @@ bool ListWidget::handleKeyUp(uint16 ascii, int keycode, int modifiers) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListWidget::lostFocusWidget() {
|
void ListWidget::lostFocusWidget() {
|
||||||
|
// If we loose focus, we simply forget the user changes
|
||||||
_editMode = false;
|
_editMode = false;
|
||||||
drawCaret(true);
|
drawCaret(true);
|
||||||
draw();
|
draw();
|
||||||
|
@ -331,13 +321,17 @@ void ListWidget::drawWidget(bool hilite) {
|
||||||
|
|
||||||
// Draw the list items
|
// Draw the list items
|
||||||
for (i = 0, pos = _currentPos; i < _entriesPerPage && pos < len; i++, pos++) {
|
for (i = 0, pos = _currentPos; i < _entriesPerPage && pos < len; i++, pos++) {
|
||||||
if (_numberingMode == kListNumberingZero || _numberingMode == kListNumberingOne) {
|
if (_numberingMode != kListNumberingOff) {
|
||||||
char temp[10];
|
char temp[10];
|
||||||
sprintf(temp, "%2d. ", (pos + _numberingMode));
|
sprintf(temp, "%2d. ", (pos + _numberingMode));
|
||||||
buffer = temp;
|
buffer = temp;
|
||||||
|
} else {
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
|
if (_selectedItem == pos && _editMode)
|
||||||
|
buffer += _editString;
|
||||||
|
else
|
||||||
buffer += _list[pos];
|
buffer += _list[pos];
|
||||||
} else
|
|
||||||
buffer = _list[pos];
|
|
||||||
|
|
||||||
if (_selectedItem == pos) {
|
if (_selectedItem == pos) {
|
||||||
if (_hasFocus)
|
if (_hasFocus)
|
||||||
|
@ -350,43 +344,27 @@ void ListWidget::drawWidget(bool hilite) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ListWidget::getCaretPos() const {
|
Common::Rect ListWidget::getEditRect() const {
|
||||||
int caretpos = 0;
|
Common::Rect r(2, 1, _w - 2 , kLineHeight);
|
||||||
NewGui *gui = &g_gui;
|
const int offset = (_selectedItem - _currentPos) * kLineHeight;
|
||||||
|
r.top += offset;
|
||||||
|
r.bottom += offset;
|
||||||
|
|
||||||
if (_numberingMode == kListNumberingZero || _numberingMode == kListNumberingOne) {
|
if (_numberingMode != kListNumberingOff) {
|
||||||
char temp[10];
|
char temp[10];
|
||||||
sprintf(temp, "%2d. ", (_selectedItem + _numberingMode));
|
sprintf(temp, "%2d. ", (_selectedItem + _numberingMode));
|
||||||
caretpos += gui->getStringWidth(temp);
|
r.left += g_gui.getStringWidth(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
caretpos += gui->getStringWidth(_list[_selectedItem]);
|
return r;
|
||||||
|
|
||||||
return caretpos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListWidget::drawCaret(bool erase) {
|
int ListWidget::getCaretOffset() const {
|
||||||
// Only draw if item is visible
|
int caretpos = 0;
|
||||||
if (_selectedItem < _currentPos || _selectedItem >= _currentPos + _entriesPerPage)
|
|
||||||
return;
|
|
||||||
if (!isVisible() || !_boss->isVisible())
|
|
||||||
return;
|
|
||||||
|
|
||||||
NewGui *gui = &g_gui;
|
caretpos += g_gui.getStringWidth(_editString);
|
||||||
|
|
||||||
// The item is selected, thus _bgcolor is used to draw the caret and _textcolorhi to erase it
|
return caretpos;
|
||||||
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);
|
|
||||||
|
|
||||||
_caretVisible = !erase;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListWidget::scrollToCurrent() {
|
void ListWidget::scrollToCurrent() {
|
||||||
|
@ -411,18 +389,25 @@ void ListWidget::scrollToCurrent() {
|
||||||
void ListWidget::startEditMode() {
|
void ListWidget::startEditMode() {
|
||||||
if (_editable && !_editMode && _selectedItem >= 0) {
|
if (_editable && !_editMode && _selectedItem >= 0) {
|
||||||
_editMode = true;
|
_editMode = true;
|
||||||
_backupString = _list[_selectedItem];
|
_editString = _list[_selectedItem];
|
||||||
|
_caretPos = _editString.size();
|
||||||
draw();
|
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() {
|
void ListWidget::abortEditMode() {
|
||||||
if (_editMode) {
|
// undo any changes made
|
||||||
_editMode = false;
|
assert(_selectedItem >= 0);
|
||||||
_list[_selectedItem] = _backupString;
|
_editMode = false;
|
||||||
drawCaret(true);
|
//drawCaret(true);
|
||||||
draw();
|
//draw();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // End of namespace GUI
|
} // End of namespace GUI
|
||||||
|
|
|
@ -18,10 +18,10 @@
|
||||||
* $Header$
|
* $Header$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LISTWIDGET_H
|
#ifndef GUI_LISTWIDGET_H
|
||||||
#define LISTWIDGET_H
|
#define GUI_LISTWIDGET_H
|
||||||
|
|
||||||
#include "gui/widget.h"
|
#include "gui/editable.h"
|
||||||
#include "common/str.h"
|
#include "common/str.h"
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
@ -42,9 +42,10 @@ enum {
|
||||||
};
|
};
|
||||||
|
|
||||||
/* ListWidget */
|
/* ListWidget */
|
||||||
class ListWidget : public Widget, public CommandSender {
|
class ListWidget : public EditableWidget, public CommandSender {
|
||||||
typedef Common::StringList StringList;
|
public:
|
||||||
typedef Common::String String;
|
typedef Common::String String;
|
||||||
|
typedef Common::StringList StringList;
|
||||||
protected:
|
protected:
|
||||||
StringList _list;
|
StringList _list;
|
||||||
bool _editable;
|
bool _editable;
|
||||||
|
@ -55,13 +56,10 @@ protected:
|
||||||
int _selectedItem;
|
int _selectedItem;
|
||||||
ScrollBarWidget *_scrollBar;
|
ScrollBarWidget *_scrollBar;
|
||||||
int _currentKeyDown;
|
int _currentKeyDown;
|
||||||
String _backupString;
|
|
||||||
|
|
||||||
bool _caretVisible;
|
|
||||||
uint32 _caretTime;
|
|
||||||
|
|
||||||
String _quickSelectStr;
|
String _quickSelectStr;
|
||||||
uint32 _quickSelectTime;
|
uint32 _quickSelectTime;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ListWidget(GuiObject *boss, int x, int y, int w, int h);
|
ListWidget(GuiObject *boss, int x, int y, int w, int h);
|
||||||
virtual ~ListWidget();
|
virtual ~ListWidget();
|
||||||
|
@ -86,16 +84,19 @@ public:
|
||||||
|
|
||||||
virtual bool wantsFocus() { return true; };
|
virtual bool wantsFocus() { return true; };
|
||||||
|
|
||||||
void scrollBarRecalc();
|
// Made startEditMode for SCUMM's SaveLoadChooser
|
||||||
|
|
||||||
void startEditMode();
|
void startEditMode();
|
||||||
void abortEditMode();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void drawWidget(bool hilite);
|
void drawWidget(bool hilite);
|
||||||
|
|
||||||
int getCaretPos() const;
|
void scrollBarRecalc();
|
||||||
void drawCaret(bool erase);
|
|
||||||
|
void endEditMode();
|
||||||
|
void abortEditMode();
|
||||||
|
|
||||||
|
Common::Rect getEditRect() const;
|
||||||
|
int getCaretOffset() const;
|
||||||
|
|
||||||
void lostFocusWidget();
|
void lostFocusWidget();
|
||||||
void scrollToCurrent();
|
void scrollToCurrent();
|
||||||
|
|
74
gui/editable.cpp
Normal file
74
gui/editable.cpp
Normal 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
63
gui/editable.h
Normal 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
|
|
@ -82,7 +82,7 @@ public:
|
||||||
protected:
|
protected:
|
||||||
bool tryInsertChar(char c, int pos) {
|
bool tryInsertChar(char c, int pos) {
|
||||||
if (isalnum(c) || c == '-' || c == '_') {
|
if (isalnum(c) || c == '-' || c == '_') {
|
||||||
_label.insertChar(c, pos);
|
_editString.insertChar(c, pos);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -303,7 +303,7 @@ void EditGameDialog::open() {
|
||||||
|
|
||||||
void EditGameDialog::close() {
|
void EditGameDialog::close() {
|
||||||
if (getResult()) {
|
if (getResult()) {
|
||||||
ConfMan.set("description", _descriptionWidget->getLabel(), _domain);
|
ConfMan.set("description", _descriptionWidget->getString(), _domain);
|
||||||
|
|
||||||
Common::Language lang = (Common::Language)_langPopUp->getSelectedTag();
|
Common::Language lang = (Common::Language)_langPopUp->getSelectedTag();
|
||||||
if (lang < 0)
|
if (lang < 0)
|
||||||
|
@ -389,7 +389,7 @@ void EditGameDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 dat
|
||||||
|
|
||||||
case kOKCmd: {
|
case kOKCmd: {
|
||||||
// Write back changes made to config object
|
// Write back changes made to config object
|
||||||
String newDomain(_domainWidget->getLabel());
|
String newDomain(_domainWidget->getString());
|
||||||
if (newDomain != _domain) {
|
if (newDomain != _domain) {
|
||||||
if (newDomain.isEmpty() || ConfMan.hasGameDomain(newDomain)) {
|
if (newDomain.isEmpty() || ConfMan.hasGameDomain(newDomain)) {
|
||||||
MessageDialog alert("This game ID is already taken. Please choose another one.");
|
MessageDialog alert("This game ID is already taken. Please choose another one.");
|
||||||
|
|
|
@ -7,6 +7,7 @@ MODULE_OBJS := \
|
||||||
gui/console.o \
|
gui/console.o \
|
||||||
gui/consolefont.o \
|
gui/consolefont.o \
|
||||||
gui/dialog.o \
|
gui/dialog.o \
|
||||||
|
gui/editable.o \
|
||||||
gui/EditTextWidget.o \
|
gui/EditTextWidget.o \
|
||||||
gui/launcher.o \
|
gui/launcher.o \
|
||||||
gui/ListWidget.o \
|
gui/ListWidget.o \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue