File: customdatadlg.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (312 lines) | stat: -rw-r--r-- 7,257 bytes parent folder | download
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
/*
 * Created by Mike "MjnMixael" Nelson for The FreeSpace 2 Source Code Project.
 * You may not sell or otherwise commercially exploit the source or things you
 * create based on the source.
 */

// CustomDataDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FRED.h"
#include "customdatadlg.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define NO_RESET_FOCUS 0
#define RESET_FOCUS 1

/////////////////////////////////////////////////////////////////////////////
// CustomDataDlg dialog

CustomDataDlg::CustomDataDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(CustomDataDlg::IDD, pParent)
{
}

void CustomDataDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CUSTOM_DATA_LISTER, m_data_lister);
}

// see https://stackoverflow.com/questions/17828258/how-to-prevent-mfc-dialog-closing-on-enter-and-escape-keys
// This is needed because despite the override of the normal OK and Cancel routes, Enter will still be recognized
BOOL CustomDataDlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN) {
		if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE) {
			return TRUE; // Do not process further
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

BEGIN_MESSAGE_MAP(CustomDataDlg, CDialog)
ON_BN_CLICKED(IDC_CUSTOM_ADD, OnPairAdd)
ON_BN_CLICKED(IDC_CUSTOM_REMOVE, OnPairRemove)
ON_BN_CLICKED(IDC_CUSTOM_UPDATE, OnPairUpdate)

ON_BN_CLICKED(ID_OK, OnButtonOk)
ON_BN_CLICKED(ID_CANCEL, OnButtonCancel)
ON_WM_CLOSE()

ON_LBN_SELCHANGE(IDC_CUSTOM_DATA_LISTER, OnListerSelectionChange)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CustomDataDlg message handlers

BOOL CustomDataDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// grab the existing list of custom data pairs and duplicate it. We only update it if the user clicks OK.
	m_custom_data = The_mission.custom_data;

	update_data_lister();

	// Send default name and values into dialog box
	UpdateData(FALSE);

	return TRUE; // return TRUE unless you set the focus to a control
				 // EXCEPTION: OCX Property Pages should return FALSE
}

void CustomDataDlg::OnButtonOk()
{
	// now we set the custom data to our copy
	The_mission.custom_data = m_custom_data;

	CDialog::OnOK();
}

void CustomDataDlg::OnButtonCancel()
{
	if (query_modified()) {
		int z = MessageBox("Do you want to keep your changes?", "Close", MB_ICONQUESTION | MB_YESNOCANCEL);
		if (z == IDCANCEL) {
			return;
		}

		if (z == IDYES) {
			OnButtonOk();
			return;
		}
	}

	CDialog::OnCancel();
}

// this is called when you hit the enter key
void CustomDataDlg::OnOK()
{
	// override MFC default behavior and do nothing
}

// this is called when you hit the escape key
void CustomDataDlg::OnCancel()
{
	// override MFC default behavior and do nothing
}

// this is called when you hit the Close button
void CustomDataDlg::OnClose()
{
	OnButtonCancel();
}

void CustomDataDlg::OnListerSelectionChange()
{
	const int index = m_data_lister.GetCurSel();

	if (index == LB_ERR) {
		// TODO: clear key and data text  edit boxes?
		return;
	}

	Assert(m_data_lister.GetCount() > 0);
	Assert(index >= 0 && index < static_cast<int>(m_custom_data.size()));

	const SCP_string& key = m_lister_keys[index];

	update_text_edit_boxes(key, m_custom_data[key]);
	update_help_text("No help text provided");

	for (size_t i = 0; i < Default_custom_data.size(); i++) {
		if (Default_custom_data[i].key == key) {
			update_help_text(Default_custom_data[i].description);
		}
	}

}

void CustomDataDlg::OnPairAdd()
{
	add_pair_entry(static_cast<int>(m_custom_data.size()));
}

void CustomDataDlg::add_pair_entry(const int insert_index)
{
	if (!edit_boxes_have_valid_data(false)) {
		return;
	}

	CEdit* data_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_DATA);
	CString data_str;
	data_edit->GetWindowText(data_str);

	CEdit* key_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_KEY);
	CString key_str;
	key_edit->GetWindowText(key_str);
	m_custom_data.emplace(key_str, data_str);

	update_data_lister();
	update_text_edit_boxes("", "");
}

void CustomDataDlg::OnPairRemove()
{
	const int index = m_data_lister.GetCurSel();

	if (index == LB_ERR) {
		MessageBox("Nothing selected! You must select something to remove!");
		return;
	}

	const auto& key = m_lister_keys[index];
	m_custom_data.erase(key);

	update_data_lister();
	update_help_text("");
}

void CustomDataDlg::OnPairUpdate()
{
	if (!edit_boxes_have_valid_data(true)) {
		return;
	}

	const int index = m_data_lister.GetCurSel();

	if (index == LB_ERR) {
		MessageBox("Nothing selected! Use Add to enter new data");
		return;
	}

	const auto& key = m_lister_keys[index];
	m_custom_data.erase(key);

	CEdit* data_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_DATA);
	CString data_str;
	data_edit->GetWindowText(data_str);
	
	CEdit* key_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_KEY);
	CString key_str;
	key_edit->GetWindowText(key_str);
	m_custom_data[SCP_string(key_str)] = data_str;

	update_data_lister();
	update_text_edit_boxes("", "");
}

bool CustomDataDlg::edit_boxes_have_valid_data(bool dup_key_ok)
{
	if (!data_edit_box_has_valid_data()) {
		return false;
	}
	if (!key_edit_box_has_valid_data()) {
		return false;
	}

	return true;
}

bool CustomDataDlg::key_edit_box_has_valid_data()
{

	CEdit* key_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_KEY);
	CString key_str;
	key_edit->GetWindowText(key_str);

	if (key_str.IsEmpty()) {
		MessageBox("You have not entered a key!");
		return false;
	}

	if (key_str.Find(" ") != -1) {
		MessageBox("Keys cannot contain spaces!");
		return false;
	}

	// Only check for duplicate keys if there are other keys to check!
	if (m_lister_keys.size() > 0) {

		const int index = m_data_lister.GetCurSel();
		const auto& this_key = m_lister_keys[index];

		if (strcmp(this_key.c_str(), key_str)) {
			for (const auto& pair : m_custom_data) {
				const CString key = pair.first.c_str();
				if (key == key_str) {
					MessageBox("Keys must be unique!");
					return false;
				}
			}
		}
	}

	return true;
}

bool CustomDataDlg::data_edit_box_has_valid_data()
{
	CEdit* data_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_DATA);
	CString data_str;
	data_edit->GetWindowText(data_str);

	if (data_str.IsEmpty()) {
		MessageBox("You have not entered any data!");
		return false;
	}

	return true;
}

void CustomDataDlg::update_data_lister()
{
	m_data_lister.ResetContent();
	m_lister_keys.clear();

	for (const auto& pair : m_custom_data) {
		m_lister_keys.emplace_back(pair.first);
		const SCP_string lister_entry = pair.first + " - " + pair.second;
		m_data_lister.AddString(lister_entry.c_str());
	}

	m_data_lister.EnableWindow(m_data_lister.GetCount() > 0);
}

void CustomDataDlg::update_text_edit_boxes(const SCP_string& key, const SCP_string& data)
{
	CEdit* key_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_KEY);
	key_edit->SetWindowText(key.c_str());

	CEdit* data_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_DATA);
	data_edit->SetWindowText(data.c_str());
}

void CustomDataDlg::update_help_text(const SCP_string& description)
{
	CEdit* desc_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_DATA_DESC);
	desc_edit->SetWindowText(description.c_str());

}

bool CustomDataDlg::query_modified() const
{
	return The_mission.custom_data != m_custom_data;
}