[Scummvm-cvs-logs] CVS: scummvm/gui ListWidget.cpp,1.7,1.8 ListWidget.h,1.6,1.7 dialog.cpp,1.20,1.21 util.cpp,1.4,1.5 util.h,1.3,1.4 widget.cpp,1.17,1.18 widget.h,1.15,1.16

Max Horn fingolfin at users.sourceforge.net
Tue Jul 16 03:53:04 CEST 2002


Update of /cvsroot/scummvm/scummvm/gui
In directory usw-pr-cvs1:/tmp/cvs-serv3379

Modified Files:
	ListWidget.cpp ListWidget.h dialog.cpp util.cpp util.h 
	widget.cpp widget.h 
Log Message:
patch by painelf that enables editing in the ListWidget (alas as usual with many mods to his patch by me :-)

Index: ListWidget.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/ListWidget.cpp,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- ListWidget.cpp	13 Jul 2002 22:41:29 -0000	1.7
+++ ListWidget.cpp	16 Jul 2002 10:52:47 -0000	1.8
@@ -27,8 +27,6 @@
 
 /*
  * TODO:
- * - Implement scrolling using arrow keys, pageup/pagedown, home/end keys etc.
- * - Implement editing of the selected string in a generic fashion
  */
 
 
@@ -39,7 +37,7 @@
 ListWidget::ListWidget(Dialog *boss, int x, int y, int w, int h)
 	: Widget(boss, x, y, w - kScrollBarWidth, h)
 {
-	_flags = WIDGET_ENABLED | WIDGET_CLEARBG;
+	_flags = WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE;
 	_type = kListWidget;
 	_numberingMode = kListNumberingOne;
 	_entriesPerPage = (_h - 4) / LINE_HEIGHT;
@@ -48,6 +46,11 @@
 	_scrollBar = new ScrollBarWidget(boss, _x + _w, _y, kScrollBarWidth, _h);
 	_scrollBar->setTarget(this);
 	
+	// FIXME: This flag should come from widget definition
+	_editable = true;
+
+	_editMode = false;
+
 	// FIXME - fill in dummy data for now
 	_list.push_back("A simple game?");
 	_list.push_back("This space for rent!");
@@ -81,14 +84,114 @@
 
 void ListWidget::handleMouseDown(int x, int y, int button)
 {
+	int oldSelectedItem = _selectedItem;
+
 	if (_flags & WIDGET_ENABLED) {
 		_selectedItem = (y - 2) / LINE_HEIGHT + _currentPos;
+
+		if (_editMode && oldSelectedItem != _selectedItem) {
+			// loose caret
+			_list[_selectedItem].deleteLastChar();
+			_editMode = false;
+		}
 		draw();
 	}
 }
 
 void ListWidget::handleKeyDown(char key, int modifiers)
 {
+	bool dirty = false;
+	int oldSelectedItem = _selectedItem;
+
+	if (_editMode) {
+
+		// get rid of caret
+		_list[_selectedItem].deleteLastChar();
+
+		if (key == '\n' || key == '\r') {
+			// enter, exit editmode
+			_editMode = false;
+			dirty = true;
+		}
+		else if (_editMode && key == 8) {	// backspace
+			_list[_selectedItem].deleteLastChar();
+			dirty = true;
+		} else if (_editMode &&
+					// filter keystrokes
+					( ( key >= 'a' && key <= 'z' )
+					|| ( key >= 'A' && key <= 'Z' )
+					|| ( key >= '0' && key <= '9' )
+					|| ( key == ' ')
+					) )
+			{
+
+			_list[_selectedItem] += key;
+			dirty = true;
+		}
+
+	} else {
+		// not editmode
+
+		switch (key) {
+		case '\n':	// enter
+		case '\r':
+			if (_selectedItem >= 0) {
+				_editMode = true;
+				dirty = true;
+			}
+			break;
+		case 17:	// up arrow
+			if (_selectedItem > 0)
+				_selectedItem--;
+			break;
+		case 18:	// down arrow
+			if (_selectedItem < _list.size() - 1)
+				_selectedItem++;
+			break;
+		case 24:	// pageup
+			_selectedItem -= _entriesPerPage - 1;
+			if (_selectedItem < 0)
+				_selectedItem = 0;
+			break;
+		case 25:	// pagedown
+			_selectedItem += _entriesPerPage - 1;
+			if (_selectedItem >= _list.size() )
+				_selectedItem = _list.size() - 1;
+			break;
+		case 22:	// home
+			_selectedItem = 0;
+			break;
+		case 23:	// end
+			_selectedItem = _list.size() - 1;
+			break;
+		}
+
+		scrollToCurrent();
+	}
+
+	if (_editMode)
+		// re-add caret
+		_list[_selectedItem] += '_';
+
+	if (dirty || _selectedItem != oldSelectedItem)
+		draw();
+
+	if (_selectedItem != oldSelectedItem) {
+		// also draw scrollbar
+		_scrollBar->draw();
+	}
+
+}
+
+void ListWidget::lostFocusWidget()
+{
+	if (_editMode) {
+		// loose caret
+		_list[_selectedItem].deleteLastChar();
+		_editMode = false;
+	}
+
+	draw();
 }
 
 void ListWidget::handleCommand(CommandSender *sender, uint32 cmd, uint32 data)
@@ -119,7 +222,25 @@
 		} else
 			buffer = "";
 		buffer += _list[pos];
+	
 		gui->drawString(buffer, _x+5, _y+2 + LINE_HEIGHT * i, _w - 10,
-							(_selectedItem == pos) ? gui->_textcolorhi : gui->_textcolor);
+							(_selectedItem == pos && _hasFocus) ? gui->_textcolorhi : gui->_textcolor);
+	}
+}
+
+void ListWidget::scrollToCurrent() {
+
+	// Only do something if the current item is not in our view port
+	if (_selectedItem < _currentPos) {
+		// it's above our view
+		_currentPos = _selectedItem;
+	} else if (_selectedItem >= _currentPos + _entriesPerPage ) {
+		// it's below our view
+		_currentPos = _selectedItem - _entriesPerPage + 1;
+		if (_currentPos < 0)
+			_currentPos = 0;
 	}
+
+	_scrollBar->_currentPos = _currentPos;
+	_scrollBar->recalc();
 }

Index: ListWidget.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/ListWidget.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- ListWidget.h	13 Jul 2002 18:32:08 -0000	1.6
+++ ListWidget.h	16 Jul 2002 10:52:47 -0000	1.7
@@ -37,6 +37,7 @@
 protected:
 	StringList		_list;
 	bool			_editable;
+	bool			_editMode;
 	int				_numberingMode;
 	int				_currentPos;
 	int				_entriesPerPage;
@@ -57,6 +58,8 @@
 
 protected:
 	void drawWidget(bool hilite);
+	void lostFocusWidget();
+	void scrollToCurrent();
 };
 
 #endif

Index: dialog.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/dialog.cpp,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- dialog.cpp	13 Jul 2002 22:41:29 -0000	1.20
+++ dialog.cpp	16 Jul 2002 10:52:48 -0000	1.21
@@ -73,11 +73,24 @@
 
 void Dialog::handleMouseDown(int x, int y, int button)
 {
-	_focusedWidget = findWidget(x, y);
+	Widget *w;
+	w = findWidget(x, y);
 
-	if (_focusedWidget) {
-		_focusedWidget->handleMouseDown(x - _focusedWidget->_x, y - _focusedWidget->_y, button);
+	if (w != _focusedWidget) {
+		// The focus will change. Tell the old focused widget (if any)
+		// that it lost the focus.
+		if (_focusedWidget)
+			_focusedWidget->lostFocus();
+	
+		// Tell the new focused widget (if any) that it just gained the focus.
+		if (w)
+			w->recievedFocus();
+	
+		_focusedWidget = w;
 	}
+
+	if (_focusedWidget)
+		_focusedWidget->handleMouseDown(x - _focusedWidget->_x, y - _focusedWidget->_y, button);
 }
 
 void Dialog::handleMouseUp(int x, int y, int button)
@@ -88,8 +101,10 @@
 		w = _focusedWidget;
 		
 		// Lose focus on mouseup unless the widget requested to retain the focus
-		if (! (_focusedWidget->getFlags() & WIDGET_RETAIN_FOCUS ))
+		if (! (_focusedWidget->getFlags() & WIDGET_RETAIN_FOCUS )) {
+			_focusedWidget->lostFocus();
 			_focusedWidget = 0;
+		}
 
 	} else {
 		w = findWidget(x, y);

Index: util.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/util.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- util.cpp	15 Jul 2002 20:11:39 -0000	1.4
+++ util.cpp	16 Jul 2002 10:52:48 -0000	1.5
@@ -163,6 +163,13 @@
 	return *this;
 }
 
+void String::deleteLastChar() {
+	if (_len > 0) {
+		_len--;
+		_str[_len]=0;
+	}
+}
+
 void String::clear()
 {
 	if (_str)
@@ -187,3 +194,4 @@
 		free(old_str);
 	}
 }
+

Index: util.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/util.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- util.h	15 Jul 2002 20:11:38 -0000	1.3
+++ util.h	16 Jul 2002 10:52:48 -0000	1.4
@@ -114,6 +114,7 @@
 	const char *c_str() const		{ return _str; }
 	int size() const				{ return _len; }
 	
+	void deleteLastChar();
 	void clear();
 
 protected:

Index: widget.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/widget.cpp,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- widget.cpp	15 Jul 2002 12:59:56 -0000	1.17
+++ widget.cpp	16 Jul 2002 10:52:48 -0000	1.18
@@ -25,7 +25,8 @@
 
 
 Widget::Widget (Dialog *boss, int x, int y, int w, int h)
-	: _type(0), _boss(boss), _x(x), _y(y), _w(w), _h(h), _id(0), _flags(0)
+	: _type(0), _boss(boss), _x(x), _y(y), _w(w), _h(h),
+	  _id(0), _flags(0), _hasFocus(false)
 {
 	// Insert into the widget list of the boss
 	_next = _boss->_firstWidget;

Index: widget.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/widget.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- widget.h	15 Jul 2002 12:59:56 -0000	1.15
+++ widget.h	16 Jul 2002 10:52:48 -0000	1.16
@@ -84,6 +84,7 @@
 	uint16		_w, _h;
 	uint16		_id;
 	uint16		_flags;
+	bool		_hasFocus;
 
 public:
 	Widget(Dialog *boss, int x, int y, int w, int h);
@@ -98,6 +99,8 @@
 	virtual void handleKeyUp(char key, int modifiers) {}
 	virtual void handleTickle() {}
 	void draw();
+	void recievedFocus() { _hasFocus = true; recievedFocusWidget(); }
+	void lostFocus() { _hasFocus = false; lostFocusWidget(); }
 
 	void setFlags(int flags)	{ _flags |= flags; }
 	void clearFlags(int flags)	{ _flags &= ~flags; }
@@ -105,6 +108,9 @@
 
 protected:
 	virtual void drawWidget(bool hilite) {}
+
+	virtual void recievedFocusWidget() {}
+	virtual void lostFocusWidget() {}
 };
 
 





More information about the Scummvm-git-logs mailing list