File: EditableListCtrl.cpp

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (318 lines) | stat: -rw-r--r-- 7,975 bytes parent folder | download | duplicates (4)
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
311
312
313
314
315
316
317
318
/* Copyright (C) 2014 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "EditableListCtrl.h"

#include "EditableListCtrlCommands.h"
#include "FieldEditCtrl.h"
#include "General/AtlasWindowCommandProc.h"
#include "AtlasObject/AtlasObject.h"
#include "AtlasObject/AtlasObjectText.h"
#include "General/AtlasClipboard.h"

const int BlanksAtEnd = 2;

EditableListCtrl::EditableListCtrl(wxWindow *parent,
								   wxWindowID id,
								   const wxPoint& pos,
								   const wxSize& size,
								   long style,
								   const wxValidator& validator,
								   const wxString& name)
	: wxListCtrl(parent, id, pos, size, style | wxLC_VIRTUAL, validator, name)
{
	m_ListItemAttr[0].SetBackgroundColour(wxColor(0xff, 0xff, 0xff));
	m_ListItemAttr[1].SetBackgroundColour(wxColor(0xee, 0xee, 0xee));

	wxASSERT_MSG(style & wxLC_REPORT, _T("EditableListCtrl must be LC_REPORT"));
	UpdateDisplay();
}

EditableListCtrl::~EditableListCtrl()
{
	size_t count = m_ColumnTypes.size();
	for (size_t n = 0; n < count; ++n)
		delete (FieldEditCtrl*)m_ColumnTypes[n].ctrl;

	m_ColumnTypes.clear();
}

void EditableListCtrl::AddColumnType(const wxString& title, int width, const char* objectkey, FieldEditCtrl* ctrl)
{
	int n = GetColumnCount();
	wxASSERT(m_ColumnTypes.size() == (size_t) n); // check internal consistency

	InsertColumn(n, title, wxLIST_FORMAT_LEFT, width);

	m_ColumnTypes.push_back(ColumnData(objectkey, ctrl));
}


void EditableListCtrl::OnMouseEvent(wxMouseEvent& event)
{
	// Double-clicking/right-clicking on a cell lets the user edit it.
	// The editing method depends on what column the cell is in.

	if (event.LeftDClick() || event.RightDown())
	{
		// Work out what cell was clicked on:

		wxPoint pt = event.GetPosition();
		int col = GetColumnAtPosition(pt);

		wxCHECK2(col >= 0 && col < (int)m_ColumnTypes.size(), return);

		int flags;
		long row = HitTest(pt, flags);

		if (row != wxNOT_FOUND && (flags & wxLIST_HITTEST_ONITEM))
		{
			// Calculate the exact positioning of the clicked cell
			wxRect rect;
			GetCellRect(row, col, rect);

			// Execute the appropriate FieldEditCtrl
			FieldEditCtrl* editor = (FieldEditCtrl*)m_ColumnTypes[col].ctrl;
			editor->StartEdit(this, rect, row, col);
		}
	}
}

void EditableListCtrl::OnKeyDown(wxKeyEvent& event)
{
	// TODO: Don't use magic key-code numbers

	// Check for Copy
	if ((event.GetKeyCode() == 3) || // ctrl+c
		(event.GetKeyCode() == WXK_INSERT && event.ControlDown())) // ctrl+insert
	{
		AtObj row;
		long selection = GetSelection();
		if (selection >= 0 && selection < (long)m_ListData.size())
			row = m_ListData[selection];
		AtlasClipboard::SetClipboard(row);
	}

	// Check for Paste
	else
	if ((event.GetKeyCode() == 22) || // ctrl+v
		(event.GetKeyCode() == WXK_INSERT && event.ShiftDown())) // shift+insert
	{
		AtObj row;
		if (AtlasClipboard::GetClipboard(row))
		{
			long selection = GetSelection();
			AtlasWindowCommandProc* commandProc = AtlasWindowCommandProc::GetFromParentFrame(this);
			commandProc->Submit(new PasteCommand(this, selection, row));
		}
	}
	else
		event.Skip();
}

int EditableListCtrl::GetColumnAtPosition(wxPoint& pos)
{
	// Find the column which pos is in.

	// Get the origin of the table, in case it's scrolled horizontally
	wxRect rect;
	GetItemRect(0, rect);
	int x = rect.GetX();

	// Loop through each column
	int numCols = GetColumnCount();
	for (int i = 0; i < numCols; ++i)
	{
		// Calculate the position of this column's right-hand edge
		x += GetColumnWidth(i);

		// Test if pos was within this column (and assume it wasn't in an earlier one)
		if (pos.x <= x)
			return i;
	}

	// Point is outside the table's right edge
	return -1;
}

void EditableListCtrl::GetCellRect(long row, int col, wxRect& rect)
{
	wxASSERT(col >= 0 && col < GetColumnCount());
	wxASSERT(row >= 0 && row < GetItemCount());

	GetItemRect(row, rect);

	for (int i = 0; i < col; ++i)
		rect.x += GetColumnWidth(i);

	rect.width = GetColumnWidth(col);
}


bool EditableListCtrl::IsRowBlank(int n)
{
	return ! m_ListData[n].hasContent();
}

void EditableListCtrl::TrimBlankEnds()
{
	while (m_ListData.size() && !m_ListData.back().defined())
		m_ListData.pop_back();
}


void EditableListCtrl::SetSelection(long item)
{
	SetItemState(item, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
}

long EditableListCtrl::GetSelection()
{
	for (long item = 0; item < GetItemCount(); ++item)
		if (GetItemState(item, wxLIST_STATE_SELECTED))
			return item;
	return 0;
}


void EditableListCtrl::MakeSizeAtLeast(int n)
{
	if ((int)m_ListData.size() < n)
		m_ListData.resize(n);
}

void EditableListCtrl::AddRow(AtObj& obj)
{
	m_ListData.push_back(obj);
}

void EditableListCtrl::AddRow(AtIter& iter)
{
	AtObj obj = *iter;
	AddRow(obj);
}

void EditableListCtrl::UpdateDisplay()
{
	TrimBlankEnds();
	SetItemCount((int)m_ListData.size() + BlanksAtEnd);
	Refresh();
}


void EditableListCtrl::CloneListData(std::vector<AtObj>& out)
{
	out = m_ListData;
}

void EditableListCtrl::SetListData(std::vector<AtObj>& in)
{
	m_ListData = in;
}

void EditableListCtrl::DeleteData()
{
	m_ListData.clear();
}

wxString EditableListCtrl::GetCellString(long item, long column) const
{
	wxCHECK(item >= 0 && column >= 0 && column < (int)m_ColumnTypes.size(), _T(""));

	if (item >= (int)m_ListData.size())
		return _T("");

	AtObj cell = *m_ListData[item][m_ColumnTypes[column].key];
	return AtlasObject::ConvertToString(cell).c_str();
}

AtObj EditableListCtrl::GetCellObject(long item, long column) const
{
	wxCHECK(item >= 0 && column >= 0 && column < (int)m_ColumnTypes.size(), AtObj());

	if (item >= (int)m_ListData.size())
		return AtObj();

	return *m_ListData[item][m_ColumnTypes[column].key];
}

void EditableListCtrl::SetCellString(long item, long column, wxString& str)
{
	wxCHECK(item >= 0 && column >= 0 && column < (int)m_ColumnTypes.size(), );
	MakeSizeAtLeast(item+1);
	m_ListData[item].set(m_ColumnTypes[column].key, str);
}

void EditableListCtrl::SetCellObject(long item, long column, AtObj& obj)
{
	wxCHECK(item >= 0 && column >= 0 && column < (int)m_ColumnTypes.size(), );
	MakeSizeAtLeast(item+1);
	m_ListData[item].set(m_ColumnTypes[column].key, obj);
}

wxString EditableListCtrl::OnGetItemText(long item, long column) const
{
	return GetCellString(item, column);
}

wxListItemAttr* EditableListCtrl::OnGetItemAttr(long item) const
{
	// Make the last two rows white
	if (item >= (long)m_ListData.size())
		return const_cast<wxListItemAttr*>(&m_ListItemAttr[0]);

	// Make the background colors of rows alternate
	else
		return const_cast<wxListItemAttr*>(&m_ListItemAttr[item%2]);
}

void EditableListCtrl::ImportData(AtObj& in)
{
	return DoImport(in);
}

AtObj EditableListCtrl::ExportData()
{
	return DoExport();
}


void EditableListCtrl::ThawData(AtObj& in)
{
	m_ListData.clear();
	for (AtIter it = in["item"]; it.defined(); ++it)
		m_ListData.push_back(*it);
	UpdateDisplay();
}

AtObj EditableListCtrl::FreezeData()
{
	AtObj out;
	for (std::vector<AtObj>::iterator it = m_ListData.begin(); it != m_ListData.end(); ++it)
		out.add("item", *it);
	return out;
}



BEGIN_EVENT_TABLE(EditableListCtrl, wxListCtrl)
	EVT_LEFT_DCLICK(EditableListCtrl::OnMouseEvent)
	EVT_RIGHT_DOWN(EditableListCtrl::OnMouseEvent)
	EVT_CHAR(EditableListCtrl::OnKeyDown)
END_EVENT_TABLE()