File: customstringsdlg.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 (385 lines) | stat: -rw-r--r-- 8,838 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * 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.
 */

// CustomStringsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FRED.h"
#include "customstringsdlg.h"


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

#define NO_RESET_FOCUS 0
#define RESET_FOCUS 1

/////////////////////////////////////////////////////////////////////////////
// CustomStringsDlg dialog

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

void CustomStringsDlg::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 CustomStringsDlg::PreTranslateMessage(MSG* pMsg)
{
	if (!m_text_edit_focus && 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(CustomStringsDlg, CDialog)
ON_BN_CLICKED(IDC_CUSTOM_ADD, OnStringAdd)
ON_BN_CLICKED(IDC_CUSTOM_REMOVE, OnStringRemove)
ON_BN_CLICKED(IDC_CUSTOM_UPDATE, OnStringUpdate)

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

ON_LBN_SELCHANGE(IDC_CUSTOM_DATA_LISTER, OnListerSelectionChange)

ON_EN_SETFOCUS(IDC_CUSTOM_STRING, OnEnSetFocusEditString)
ON_EN_KILLFOCUS(IDC_CUSTOM_STRING, OnEnKillFocusEditString)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CustomStringsDlg message handlers

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

	m_text_edit_focus = false;
	m_modified = false;

	// grab the existing list of custom strings and duplicate it. We only update it if the user clicks OK.
	m_custom_strings = The_mission.custom_strings;

	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 CustomStringsDlg::OnButtonOk()
{
	// now we set the custom data to our copy
	The_mission.custom_strings = m_custom_strings;

	CDialog::OnOK();
}

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

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

	CDialog::OnCancel();
}

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

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

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

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

	if (index == LB_ERR) {
		update_text_edit_boxes("", "", "");
		return;
	}

	Assertion(m_data_lister.GetCount() > 0, "Attempt to change custom string selection when there are no custom strings! Please report.");
	Assertion(index >= 0 && index < static_cast<int>(m_custom_strings.size()), "Selected an invalid custom string! Please report.");

	const SCP_string& key = m_lister_keys[index];
	const mission_custom_string *cs = nullptr;

	for (size_t i = 0; i <= m_custom_strings.size(); i++) {
		if (m_custom_strings[i].name == key) {
			cs = &m_custom_strings[i];
			break;
		}
	}

	if (cs != nullptr) {
		update_text_edit_boxes(cs->name, cs->value, cs->text);
	}

}

void CustomStringsDlg::OnStringAdd()
{
	add_pair_entry();
}

void CustomStringsDlg::add_pair_entry()
{
	if (!edit_boxes_have_valid_data()) {
		return;
	}

	mission_custom_string cs;

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

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

	CEdit* text_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_STRING);
	CString text_str;
	text_edit->GetWindowText(text_str);
	cs.text = text_str;

	m_custom_strings.push_back(cs);
	m_modified = true;

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

void CustomStringsDlg::OnStringRemove()
{
	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];

	for (size_t i = 0; i <= m_custom_strings.size(); i++) {
		if (m_custom_strings[i].name == key) {
			m_custom_strings.erase(m_custom_strings.begin() + i);
			break;
		}
	}
	m_modified = true;

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

void CustomStringsDlg::OnStringUpdate()
{
	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];

	size_t i;

	for (i = 0; i <= m_custom_strings.size(); i++) {
		if (m_custom_strings[i].name == key) {
			break;
		}
	}
	
	CEdit* key_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_KEY);
	CString key_str;
	key_edit->GetWindowText(key_str);
	m_custom_strings[i].name = key_str;

	CEdit* data_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_DATA);
	CString data_str;
	data_edit->GetWindowText(data_str);
	m_custom_strings[i].value = data_str;

	CEdit* text_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_STRING);
	CString text_str;
	text_edit->GetWindowText(text_str);
	m_custom_strings[i].text = text_str;

	m_modified = true;

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

bool CustomStringsDlg::edit_boxes_have_valid_data(bool update)
{
	if (!data_edit_box_has_valid_data()) {
		return false;
	}

	if (!text_edit_box_has_valid_data()) {
		return false;
	}

	if (!key_edit_box_has_valid_data(update)) {
		return false;
	}

	return true;
}

bool CustomStringsDlg::key_edit_box_has_valid_data(bool update)
{

	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("Names cannot contain spaces!");
		return false;
	}

	for (const auto& cs : m_custom_strings) {
		const CString key = cs.name.c_str();
		if (update) {
			const int index = m_data_lister.GetCurSel();

			if (!SCP_vector_inbounds(m_lister_keys, index)) {
				MessageBox("Must select an item to update!");
				return false;
			}

			const auto& this_key = m_lister_keys[index];
			if (!strcmp(this_key.c_str(), key)) {
				continue;
			}
		}
		if (key == key_str) {
			MessageBox("Names must be unique!");
			return false;
		}
	}

	return true;
}

bool CustomStringsDlg::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;
}

bool CustomStringsDlg::text_edit_box_has_valid_data()
{
	CEdit* text_edit = (CEdit*)GetDlgItem(IDC_CUSTOM_STRING);
	CString text_str;
	text_edit->GetWindowText(text_str);

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

	return true;
}

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

	for (const auto& cs : m_custom_strings) {
		m_lister_keys.emplace_back(cs.name);
		const SCP_string lister_entry = cs.name + " - " + cs.value;
		m_data_lister.AddString(lister_entry.c_str());
	}

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

void CustomStringsDlg::update_text_edit_boxes(const SCP_string& key, const SCP_string& value, const SCP_string& text)
{
	
	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(value.c_str());

	CEdit* data_text = (CEdit*)GetDlgItem(IDC_CUSTOM_STRING);
	data_text->SetWindowText(text.c_str());
}

void CustomStringsDlg::OnEnSetFocusEditString()
{
	m_text_edit_focus = true;
}

void CustomStringsDlg::OnEnKillFocusEditString()
{
	m_text_edit_focus = false;
}

bool CustomStringsDlg::query_modified() const
{
	return m_modified;
}