[Scummvm-cvs-logs] CVS: scummvm/gui EditTextWidget.cpp,1.30,1.31 EditTextWidget.h,1.17,1.18 ListWidget.cpp,1.42,1.43 ListWidget.h,1.25,1.26 editable.cpp,1.1,1.2 editable.h,1.1,1.2 launcher.cpp,1.110,1.111

Max Horn fingolfin at users.sourceforge.net
Sat Jan 29 10:04:56 CET 2005


Update of /cvsroot/scummvm/scummvm/gui
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1820

Modified Files:
	EditTextWidget.cpp EditTextWidget.h ListWidget.cpp 
	ListWidget.h editable.cpp editable.h launcher.cpp 
Log Message:
Move more text editing code into class EditableWidget; ListWidget now has all the editing capabilities of EditTextWidget

Index: EditTextWidget.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/EditTextWidget.cpp,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- EditTextWidget.cpp	29 Jan 2005 16:30:50 -0000	1.30
+++ EditTextWidget.cpp	29 Jan 2005 18:04:34 -0000	1.31
@@ -28,16 +28,15 @@
 
 EditTextWidget::EditTextWidget(GuiObject *boss, int x, int y, int w, int h, const String &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;
 
-	_caretPos = _editString.size();
+	setEditString(text);
+}
 
-	_editScrollOffset = (g_gui.getStringWidth(_editString) - (getEditRect().width()));
-	if (_editScrollOffset < 0)
-		_editScrollOffset = 0;
+void EditTextWidget::setEditString(const String &str) {
+	EditableWidget::setEditString(str);
+	_backupString = str;
 }
 
 void EditTextWidget::handleMouseDown(int x, int y, int button, int clickCount) {
@@ -61,75 +60,6 @@
 		draw();
 }
 
-bool EditTextWidget::tryInsertChar(char c, int pos) {
-	if (isprint(c)) {
-		_editString.insertChar(c, pos);
-		return true;
-	}
-	return false;
-}
-
-
-bool EditTextWidget::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);
-		}
-		break;
-	case 256 + 19:	// right arrow
-		if (_caretPos < (int)_editString.size()) {
-			dirty = setCaretPos(_caretPos + 1);
-		}
-		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;
-}
 
 void EditTextWidget::drawWidget(bool hilite) {
 	// Draw a thin frame around us.
@@ -149,16 +79,6 @@
 	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;
-}
-
 void EditTextWidget::receivedFocusWidget() {
 }
 
@@ -176,46 +96,8 @@
 }
 
 void EditTextWidget::abortEditMode() {
-	_editString = _backupString;
-	_caretPos = _editString.size();
-	_editScrollOffset = (g_gui.getStringWidth(_editString) - (getEditRect().width()));
-	if (_editScrollOffset < 0)
-		_editScrollOffset = 0;
+	setEditString(_backupString);
 	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 _editScrollOffset 
-
-	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) {
-		const int strWidth = g_gui.getStringWidth(_editString);
-		if (strWidth - _editScrollOffset < editWidth) {
-			// scroll right
-			_editScrollOffset = (strWidth - editWidth);
-			if (_editScrollOffset < 0)
-				_editScrollOffset = 0;
-		}
-	}
-
-	return false;
-}
-
 } // End of namespace GUI

Index: EditTextWidget.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/EditTextWidget.h,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- EditTextWidget.h	29 Jan 2005 16:30:50 -0000	1.17
+++ EditTextWidget.h	29 Jan 2005 18:04:34 -0000	1.18
@@ -36,11 +36,9 @@
 public:
 	EditTextWidget(GuiObject *boss, int x, int y, int w, int h, const String &text);
 
-//	void setString(const String &str)	{ _editString = str; }
-	const String &getString() const		{ return _editString; }
+	void setEditString(const String &str);
 
 	virtual void handleMouseDown(int x, int y, int button, int clickCount);
-	virtual bool handleKeyDown(uint16 ascii, int keycode, int modifiers);
 
 	virtual bool wantsFocus() { return true; };
 
@@ -54,11 +52,6 @@
 	void abortEditMode();
 
 	Common::Rect getEditRect() const;
-	int getCaretOffset() const;
-	bool setCaretPos(int newPos);
-	bool adjustOffset();
-	
-	virtual bool tryInsertChar(char c, int pos);
 };
 
 } // End of namespace GUI

Index: ListWidget.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/ListWidget.cpp,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -d -r1.42 -r1.43
--- ListWidget.cpp	29 Jan 2005 16:30:51 -0000	1.42
+++ ListWidget.cpp	29 Jan 2005 18:04:34 -0000	1.43
@@ -124,7 +124,7 @@
 	}
 	
 	// TODO: Determine where inside the string the user clicked and place the
-	// caret accordingly.
+	// caret accordingly. See _editScrollOffset and EditTextWidget::handleMouseDown.
 	draw();
 
 }
@@ -193,35 +193,8 @@
 
 		scrollToCurrent();
 	} else if (_editMode) {
-
-		if (_caretVisible)
-			drawCaret(true);
-
-		switch (keycode) {
-		case '\n':	// enter/return
-		case '\r':
-			// confirm edit and exit editmode
-			endEditMode();
-			dirty = true;
-			break;
-		case 27:	// escape
-			// abort edit and exit editmode
-			abortEditMode();
-			dirty = true;
-			break;
-		case 8:		// backspace
-			_editString.deleteLastChar();
-			dirty = true;
-			break;
-		default:
-			if (isprint((char)ascii)) {
-				_editString += (char)ascii;
-				dirty = true;
-			} else {
-				handled = false;
-			}
-		}
-
+		// Class EditableWidget handles all text editing related key presses for us
+		handled = EditableWidget::handleKeyDown(ascii, keycode, modifiers);
 	} else {
 		// not editmode
 
@@ -313,6 +286,7 @@
 	NewGui *gui = &g_gui;
 	int i, pos, len = _list.size();
 	Common::String buffer;
+	int offset, deltax;
 
 	// Draw a thin frame around the list.
 	gui->hLine(_x, _y, _x + _w - 1, gui->_color);
@@ -321,26 +295,41 @@
 
 	// Draw the list items
 	for (i = 0, pos = _currentPos; i < _entriesPerPage && pos < len; i++, pos++) {
+		const OverlayColor textColor = (_selectedItem == pos && _hasFocus) ? gui->_bgcolor : gui->_textcolor;
+		const int y = _y + 2 + kLineHeight * i;
+
+		// Draw the selected item inverted, on a highlighted background.
+		if (_selectedItem == pos) {
+			if (_hasFocus)
+				gui->fillRect(_x + 1, _y + 1 + kLineHeight * i, _w - 1, kLineHeight, gui->_textcolorhi);
+			else
+				gui->frameRect(_x + 1, _y + 1 + kLineHeight * i, _w - 1, kLineHeight, gui->_textcolorhi);
+		}
+
+		// If in numbering mode, we first print a number prefix
 		if (_numberingMode != kListNumberingOff) {
 			char temp[10];
 			sprintf(temp, "%2d. ", (pos + _numberingMode));
 			buffer = temp;
+			gui->drawString(buffer, _x + 2, y, _w - 4, textColor);
+			offset = gui->getStringWidth(buffer);
 		} else {
-			buffer.clear();
+			offset = 0;
 		}
-		if (_selectedItem == pos && _editMode)
-			buffer += _editString;
-		else
-			buffer += _list[pos];
 
-		if (_selectedItem == pos) {
-			if (_hasFocus)
-				gui->fillRect(_x + 1, _y + 1 + kLineHeight * i, _w - 1, kLineHeight, gui->_textcolorhi);
-			else
-				gui->frameRect(_x + 1, _y + 1 + kLineHeight * i, _w - 1, kLineHeight, gui->_textcolorhi);
+		Common::Rect r(getEditRect());
+		if (_selectedItem == pos && _editMode) {
+
+			buffer = _editString;
+			adjustOffset();
+			deltax = -_editScrollOffset;
+	
+			gui->drawString(buffer, _x + r.left, y, r.width(), textColor, kTextAlignLeft, deltax, false);
+		} else {
+			buffer = _list[pos];
+			deltax = 0;
+			gui->drawString(buffer, _x + r.left, y, r.width(), textColor);
 		}
-		gui->drawString(buffer, _x + 2, _y + 2 + kLineHeight * i, _w - 4,
-							(_selectedItem == pos && _hasFocus) ? gui->_bgcolor : gui->_textcolor);
 	}
 }
 
@@ -359,14 +348,6 @@
 	return r;
 }
 
-int ListWidget::getCaretOffset() const {
-	int caretpos = 0;
-
-	caretpos += g_gui.getStringWidth(_editString);
-	
-	return caretpos;
-}
-
 void ListWidget::scrollToCurrent() {
 	// Only do something if the current item is not in our view port
 	if (_selectedItem < _currentPos) {
@@ -389,8 +370,7 @@
 void ListWidget::startEditMode() {
 	if (_editable && !_editMode && _selectedItem >= 0) {
 		_editMode = true;
-		_editString = _list[_selectedItem];
-		_caretPos = _editString.size();
+		setEditString(_list[_selectedItem]);
 		draw();
 	}
 }

Index: ListWidget.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/ListWidget.h,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- ListWidget.h	29 Jan 2005 16:30:51 -0000	1.25
+++ ListWidget.h	29 Jan 2005 18:04:34 -0000	1.26
@@ -96,7 +96,6 @@
 	void abortEditMode();
 	
 	Common::Rect getEditRect() const;
-	int getCaretOffset() const;
 
 	void lostFocusWidget();
 	void scrollToCurrent();

Index: editable.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/editable.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- editable.cpp	29 Jan 2005 16:30:51 -0000	1.1
+++ editable.cpp	29 Jan 2005 18:04:34 -0000	1.2
@@ -1,5 +1,5 @@
 /* ScummVM - Scumm Interpreter
- * Copyright (C) 2002-2005 The ScummVM project
+ * Copyright (C) 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
@@ -39,6 +39,25 @@
 EditableWidget::~EditableWidget() {
 }
 
+void EditableWidget::setEditString(const String &str) {
+	// TODO: We probably should filter the input string here,
+	// e.g. using tryInsertChar.
+	_editString = str;
+	_caretPos = _editString.size();
+
+	_editScrollOffset = (g_gui.getStringWidth(_editString) - (getEditRect().width()));
+	if (_editScrollOffset < 0)
+		_editScrollOffset = 0;
+}
+
+bool EditableWidget::tryInsertChar(char c, int pos) {
+	if (isprint(c)) {
+		_editString.insertChar(c, pos);
+		return true;
+	}
+	return false;
+}
+
 void EditableWidget::handleTickle() {
 	uint32 time = getMillis();
 	if (_caretTime < time) {
@@ -47,6 +66,77 @@
 	}
 }
 
+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);
+		}
+		break;
+	case 256 + 19:	// right arrow
+		if (_caretPos < (int)_editString.size()) {
+			dirty = setCaretPos(_caretPos + 1);
+		}
+		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++)
+		caretpos += g_gui.getCharWidth(_editString[i]);
+
+	caretpos -= _editScrollOffset;
+
+	return caretpos;
+}
+
 void EditableWidget::drawCaret(bool erase) {
 	// Only draw if item is visible
 	if (!isVisible() || !_boss->isVisible())
@@ -70,5 +160,39 @@
 	_caretVisible = !erase;
 }
 
+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,
+	// adjust _editScrollOffset 
+
+	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) {
+		const int strWidth = g_gui.getStringWidth(_editString);
+		if (strWidth - _editScrollOffset < editWidth) {
+			// scroll right
+			_editScrollOffset = (strWidth - editWidth);
+			if (_editScrollOffset < 0)
+				_editScrollOffset = 0;
+		}
+	}
+
+	return false;
+}
+
 
 } // End of namespace GUI

Index: editable.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/editable.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- editable.h	29 Jan 2005 16:30:51 -0000	1.1
+++ editable.h	29 Jan 2005 18:04:34 -0000	1.2
@@ -1,5 +1,5 @@
 /* ScummVM - Scumm Interpreter
- * Copyright (C) 2002-2005 The ScummVM project
+ * Copyright (C) 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
@@ -27,7 +27,10 @@
 
 namespace GUI {
 
-
+/**
+ * Base class for widgets which need to edit text, like ListWidget and
+ * EditTextWidget.
+ */
 class EditableWidget : public Widget {
 public:
 	typedef Common::String String;
@@ -46,7 +49,11 @@
 	EditableWidget(GuiObject *boss, int x, int y, int w, int h);
 	virtual ~EditableWidget();
 
+	virtual void setEditString(const String &str);
+	virtual const String &getEditString() const		{ return _editString; }
+
 	virtual void handleTickle();
+	virtual bool handleKeyDown(uint16 ascii, int keycode, int modifiers);
 
 protected:
 	virtual void startEditMode() = 0;
@@ -54,8 +61,12 @@
 	virtual void abortEditMode() = 0;
 
 	virtual Common::Rect getEditRect() const = 0;
-	virtual int getCaretOffset() const = 0;
+	virtual int getCaretOffset() const;
 	void drawCaret(bool erase);
+	bool setCaretPos(int newPos);
+	bool adjustOffset();
+	
+	virtual bool tryInsertChar(char c, int pos);
 };
 
 } // End of namespace GUI

Index: launcher.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/launcher.cpp,v
retrieving revision 1.110
retrieving revision 1.111
diff -u -d -r1.110 -r1.111
--- launcher.cpp	29 Jan 2005 16:30:51 -0000	1.110
+++ launcher.cpp	29 Jan 2005 18:04:34 -0000	1.111
@@ -303,7 +303,7 @@
 
 void EditGameDialog::close() {
 	if (getResult()) {
-		ConfMan.set("description", _descriptionWidget->getString(), _domain);
+		ConfMan.set("description", _descriptionWidget->getEditString(), _domain);
 
 		Common::Language lang = (Common::Language)_langPopUp->getSelectedTag();
 		if (lang < 0)
@@ -389,7 +389,7 @@
 
 	case kOKCmd: {
 		// Write back changes made to config object
-		String newDomain(_domainWidget->getString());
+		String newDomain(_domainWidget->getEditString());
 		if (newDomain != _domain) {
 			if (newDomain.isEmpty() || ConfMan.hasGameDomain(newDomain)) {
 				MessageDialog alert("This game ID is already taken. Please choose another one.");





More information about the Scummvm-git-logs mailing list