aboutsummaryrefslogtreecommitdiff
path: root/gui/ListWidget.cpp
blob: 1090ae60fd337091f109878db646b7c36df8c834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 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 "ListWidget.h"
#include "ScrollBarWidget.h"
#include "dialog.h"
#include "newgui.h"


ListWidget::ListWidget(Dialog *boss, int x, int y, int w, int h)
	: Widget(boss, x, y, w - kScrollBarWidth, h), CommandSender(boss)
{
	_flags = WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE;
	_type = kListWidget;
	_numberingMode = kListNumberingOne;
	_entriesPerPage = (_h - 2) / kLineHeight;
	_currentPos = 0;
	_selectedItem = -1;
	_scrollBar = new ScrollBarWidget(boss, _x + _w, _y, kScrollBarWidth, _h);
	_scrollBar->setTarget(this);
	_currentKeyDown = 0;
	
	// FIXME: This flag should come from widget definition
	_editable = true;

	_editMode = false;
}

ListWidget::~ListWidget()
{
}

void ListWidget::setList(const StringList& list)
{
	int size = list.size();
	_list = list;
	if (_currentPos >= size)
		_currentPos = size - 1;
	_selectedItem = -1;
	_editMode = false;
	scrollBarRecalc();
}

void ListWidget::scrollBarRecalc()
{
	_scrollBar->_numEntries = _list.size();
	_scrollBar->_entriesPerPage = _entriesPerPage;
	_scrollBar->_currentPos = _currentPos;
	_scrollBar->recalc();
}

void ListWidget::handleMouseDown(int x, int y, int button, int clickCount)
{
	if (isEnabled()) {
		int oldSelectedItem = _selectedItem;
		_selectedItem = (y - 1) / kLineHeight + _currentPos;
		if (_selectedItem > _list.size() - 1)
			_selectedItem = -1;

		if (oldSelectedItem != _selectedItem) {
			if (_editMode) {
				// undo any changes made
				_list[oldSelectedItem] = _backupString;
				_editMode = false;
			}
			sendCommand(kListSelectionChangedCmd, _selectedItem);
		}
		draw();
	}
}

void ListWidget::handleMouseUp(int x, int y, int button, int clickCount)
{
	// If this was a double click and the mouse is still over the selected item,
	// send the double click command
	if (clickCount > 1 && (_selectedItem == (y - 1) / kLineHeight + _currentPos)) {
		sendCommand(kListItemDoubleClickedCmd, _selectedItem);
	}
}

void ListWidget::handleMouseWheel(int x, int y, int direction)
{
	_scrollBar->handleMouseWheel(x, y, direction);
}

bool ListWidget::handleKeyDown(char key, int modifiers)
{
	bool handled = true;
	bool dirty = false;
	int oldSelectedItem = _selectedItem;

	if (_editMode) {

		// get rid of caret
		_list[_selectedItem].deleteLastChar();

		if (key == '\n' || key == '\r') {
			// enter, confirm edit and exit editmode
			_editMode = false;
			dirty = true;
			sendCommand(kListItemActivatedCmd, _selectedItem);
		} else if (key == 27) {
			// ESC, abort edit and exit editmode
			_editMode = false;
			dirty = true;
			_list[_selectedItem] = _backupString;
		} else if (key == 8) {	// backspace
			_list[_selectedItem].deleteLastChar();
			dirty = true;
		} else if (// filter keystrokes
					( ( key >= 'a' && key <= 'z' )
					|| ( key >= 'A' && key <= 'Z' )
					|| ( key >= '0' && key <= '9' )
					|| ( key == ' ')
					) )
			{

			_list[_selectedItem] += key;
			dirty = true;
		} else
			handled = false;

	} else {
		// not editmode

		switch (key) {
		case '\n':	// enter
		case '\r':
			if (_selectedItem >= 0) {
				// override continuous enter keydown
				if (_editable && (_currentKeyDown != '\n' && _currentKeyDown != '\r')) {
					dirty = true;
					_editMode = true;
					_backupString = _list[_selectedItem];
				}
			}
			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;
		default:
			handled = false;
		}

		scrollToCurrent();
	}

	if (_editMode)
		// re-add caret
		_list[_selectedItem] += '_';

	if (dirty || _selectedItem != oldSelectedItem)
		draw();

	if (_selectedItem != oldSelectedItem) {
		sendCommand(kListSelectionChangedCmd, _selectedItem);
		// also draw scrollbar
		_scrollBar->draw();
	}

#ifndef _WIN32_WCE

	// not done on WinCE because keyboard is emulated and
	// keyup is not generated

	_currentKeyDown = key;

#endif
	
	return handled;
}

bool ListWidget::handleKeyUp(char key, int modifiers)
{
	if (key == _currentKeyDown)
		_currentKeyDown = 0;
	return true;
}

void ListWidget::lostFocusWidget()
{
	if (_editMode) {
		// loose caret
		_list[_selectedItem].deleteLastChar();
		_editMode = false;
	}

	draw();
}

void ListWidget::handleCommand(CommandSender *sender, uint32 cmd, uint32 data)
{
	switch (cmd) {
	case kSetPositionCmd:
		if (_currentPos != (int)data) {
			_currentPos = data;
			draw();
		}
		break;
	}
}

void ListWidget::drawWidget(bool hilite)
{
	NewGui			*gui = _boss->getGui();
	int				i, pos, len = _list.size();
	ScummVM::String	buffer;

	// Draw a thin frame around the list.
	gui->hline(_x, _y, _x+_w-1, gui->_color);
	gui->hline(_x, _y+_h-1, _x+_w-1, gui->_shadowcolor);
	gui->vline(_x, _y, _y+_h-1, gui->_color);

	// Draw the list items
	for (i = 0, pos = _currentPos; i < _entriesPerPage && pos < len; i++, pos++) {
		if (_numberingMode == kListNumberingZero || _numberingMode == kListNumberingOne) {
			char temp[10];
			sprintf(temp, "%2d. ", (pos + _numberingMode));
			buffer = temp;
		} else
			buffer = "";

		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);
		}
		gui->drawString(buffer.c_str(), _x+2, _y+3 + kLineHeight * i, _w - 4,
							(_selectedItem == pos && _hasFocus) ? gui->_bgcolor : 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();
}

void ListWidget::startEditMode()
{
	if (_editable && !_editMode && _selectedItem >= 0) {
		_editMode = true;
		_backupString = _list[_selectedItem];
		_list[_selectedItem] += '_';
		draw();
	}
}

void ListWidget::abortEditMode()
{
	if (_editMode) {
		_editMode = false;
		_list[_selectedItem] = _backupString;
		draw();
	}
}