aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/mptrack/AdvancedConfigDlg.cpp
blob: 101a148d9bd823b59022d2ea54f721a6a5bd209c (plain) (blame)
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
/*
 * AdvancedConfigDlg.cpp
 * ---------------------
 * Purpose: Implementation of the advanced settings dialog.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#include "stdafx.h"
#include "Mainfrm.h"
#include "AdvancedConfigDlg.h"
#include "Settings.h"
#include "dlg_misc.h"

OPENMPT_NAMESPACE_BEGIN

BEGIN_MESSAGE_MAP(COptionsAdvanced, CPropertyPage)
	ON_NOTIFY(NM_DBLCLK,	IDC_LIST1,	&COptionsAdvanced::OnOptionDblClick)
#ifndef MPT_MFC_FULL
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &COptionsAdvanced::OnCustomDrawList)
#endif
	ON_EN_CHANGE(IDC_EDIT1,				&COptionsAdvanced::OnFindStringChanged)
	ON_COMMAND(IDC_BUTTON1, &COptionsAdvanced::OnSaveNow)
END_MESSAGE_MAP()

void COptionsAdvanced::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModTypeDlg)
	DDX_Control(pDX, IDC_LIST1,			m_List);
	//}}AFX_DATA_MAP
}


BOOL COptionsAdvanced::PreTranslateMessage(MSG *msg)
{
	if(msg->message == WM_KEYDOWN && msg->wParam == VK_RETURN)
	{
		OnOptionDblClick(nullptr, nullptr);
		return TRUE;
	}
	return FALSE;
}


BOOL COptionsAdvanced::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	m_List.SetExtendedStyle(m_List.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);

	ListView_EnableGroupView(m_List.m_hWnd, FALSE); // try to set known state
	int enableGroupsResult1 = static_cast<int>(ListView_EnableGroupView(m_List.m_hWnd, TRUE));
	int enableGroupsResult2 = static_cast<int>(ListView_EnableGroupView(m_List.m_hWnd, TRUE));
	// Looks like we have to check enabling and check that a second enabling does
	// not change anything.
	// Just checking if enabling fails with -1 does not work for older control
	// versions because they just do not know the window message at all and return
	// 0, always. At least Wine does behave this way.
	if(enableGroupsResult1 == 1 && enableGroupsResult2 == 0)
	{
		m_listGrouped = true;
	} else
	{
		// Did not behave as documented or expected, the actual state of the
		// control is unknown by now.
		// Play safe and set and assume the traditional ungrouped mode again.
		ListView_EnableGroupView(m_List.m_hWnd, FALSE);
		m_listGrouped = false;
	}

	if(m_listGrouped)
	{
		static constexpr ListCtrl::Header headers[] =
		{
			{ _T("Setting"), 150, LVCFMT_LEFT },
			{ _T("Type"),    40,  LVCFMT_LEFT },
			{ _T("Value"),   140, LVCFMT_LEFT },
			{ _T("Default"), 62,  LVCFMT_LEFT },
		};
		m_List.SetHeaders(headers);
	} else
	{
		static constexpr ListCtrl::Header headers[] =
		{
			{ _T("Setting"), 200, LVCFMT_LEFT },
			{ _T("Type"),    40,  LVCFMT_LEFT },
			{ _T("Value"),   100, LVCFMT_LEFT },
			{ _T("Default"), 52,  LVCFMT_LEFT },
		};
		m_List.SetHeaders(headers);
	}

	ReInit();
	return TRUE;
}


void COptionsAdvanced::ReInit()
{
	m_List.SetRedraw(FALSE);
	m_List.DeleteAllItems();
	if(m_listGrouped)
	{
		ListView_RemoveAllGroups(m_List.m_hWnd);
	}
	m_List.SetItemCount(static_cast<int>(theApp.GetSettings().size()));

	m_indexToPath.clear();
	m_indexToPath.reserve(theApp.GetSettings().size());
	m_groups.clear();
	int numGroups = 0;

	mpt::ustring findStr = mpt::ToLowerCase(GetWindowTextUnicode(*GetDlgItem(IDC_EDIT1)));

	LVITEMW lvi;
	lvi.mask = LVIF_TEXT | LVIF_PARAM;
	lvi.mask |= (m_listGrouped ? LVIF_GROUPID : 0);
	lvi.iSubItem = 0;
	lvi.state = 0;
	lvi.stateMask = 0;
	lvi.cchTextMax = 0;
	lvi.iImage = 0;
	lvi.iIndent = 0;
	lvi.iGroupId = 0;

	int i = 0;
	for(const auto &[path, state] : theApp.GetSettings())
	{
		// In MPT_USTRING_MODE_WIDE mode,
		// this loop is heavily optimized to avoid as much string copies as possible
		// in order to perform ok-ish in debug builds.
		// MPT_USTRING_MODE_UTF8 is not optimized as we (currently) do not build in
		// this mode by default.
		const mpt::ustring &section = path.GetRefSection();
		const mpt::ustring &key = path.GetRefKey();
		const SettingValue &value = state.GetRefValue();
		const SettingValue &defaultValue = state.GetRefDefault();

		if(!findStr.empty())
		{
			mpt::ustring str = path.FormatAsString() + U_("=") + value.FormatValueAsString();
			str = mpt::ToLowerCase(str);
			if(str.find(findStr) == mpt::ustring::npos)
			{
				continue;
			}
		}

		int index;
		lvi.iItem = i++;
		lvi.lParam = m_indexToPath.size();

		if(m_listGrouped)
		{

			auto gi = m_groups.find(section);
			if(gi == m_groups.end())
			{
				LVGROUP group;
	#if _WIN32_WINNT >= 0x0600
				group.cbSize = LVGROUP_V5_SIZE;
	#else
				group.cbSize = sizeof(group);
	#endif
				group.mask = LVGF_HEADER | LVGF_GROUPID;
#if MPT_USTRING_MODE_WIDE
				group.pszHeader = const_cast<wchar_t *>(section.c_str());
#else
				const std::wstring wsection = mpt::ToWide(section);
				group.pszHeader = const_cast<wchar_t *>(wsection.c_str());
#endif
				group.cchHeader = 0;
				group.pszFooter = nullptr;
				group.cchFooter = 0;
				group.iGroupId = lvi.iGroupId = numGroups++;
				group.stateMask = LVGS_COLLAPSIBLE;
				group.state = LVGS_COLLAPSIBLE;
				group.uAlign = LVGA_HEADER_LEFT;
				ListView_InsertGroup(m_List.m_hWnd, -1, &group);
				m_groups.insert(std::make_pair(section, lvi.iGroupId));
			} else
			{
				lvi.iGroupId = gi->second;
			}

#if MPT_USTRING_MODE_WIDE
			lvi.pszText = const_cast<wchar_t *>(key.c_str());
#else
			const std::wstring wkey = mpt::ToWide(key);
			lvi.pszText = const_cast<wchar_t *>(wkey.c_str());
#endif
			index = static_cast<int>(m_List.SendMessage(LVM_INSERTITEMW, 0, (LPARAM)(&lvi)));

		} else
		{

			const mpt::ustring sectionAndKey = path.FormatAsString();
#if MPT_USTRING_MODE_WIDE
			lvi.pszText = const_cast<wchar_t *>(sectionAndKey.c_str());
#else
			const std::wstring wsectionAndKey = mpt::ToWide(sectionAndKey);
			lvi.pszText = const_cast<wchar_t *>(wsectionAndKey.c_str());
#endif
			index = static_cast<int>(m_List.SendMessage(LVM_INSERTITEMW, 0, (LPARAM)(&lvi)));

		}

#if MPT_USTRING_MODE_WIDE
		m_List.SetItemText(index, 1, value.FormatTypeAsString().c_str());
		m_List.SetItemText(index, 2, value.FormatValueAsString().c_str());
		m_List.SetItemText(index, 3, defaultValue.FormatValueAsString().c_str());
#else
		m_List.SetItemText(index, 1, mpt::ToCString(value.FormatTypeAsString()));
		m_List.SetItemText(index, 2, mpt::ToCString(value.FormatValueAsString()));
		m_List.SetItemText(index, 3, mpt::ToCString(defaultValue.FormatValueAsString()));
#endif
		m_indexToPath.push_back(path);
	}

	m_List.SetItemCount(i);
	m_List.SetRedraw(TRUE);
	m_List.Invalidate(FALSE);
}


void COptionsAdvanced::OnOK()
{
	CSoundFile::SetDefaultNoteNames();
	CMainFrame *pMainFrm = CMainFrame::GetMainFrame();
	if (pMainFrm) pMainFrm->PostMessage(WM_MOD_INVALIDATEPATTERNS, HINT_MPTOPTIONS);
	CPropertyPage::OnOK();
}


BOOL COptionsAdvanced::OnSetActive()
{
	ReInit();
	CMainFrame::m_nLastOptionsPage = OPTIONS_PAGE_ADVANCED;
	return CPropertyPage::OnSetActive();
}


#ifdef MPT_MFC_FULL


COLORREF CAdvancedSettingsList::OnGetCellBkColor(int nRow, int /* nColumn */ )
{
	const bool isDefault = theApp.GetSettings().GetMap().find(m_indexToPath[GetItemData(nRow)])->second.IsDefault();
	COLORREF defColor = GetBkColor();
	COLORREF txtColor = GetTextColor();
	COLORREF modColor = RGB(GetRValue(defColor) * 0.9 + GetRValue(txtColor) * 0.1, GetGValue(defColor) * 0.9 + GetGValue(txtColor) * 0.1, GetBValue(defColor) * 0.9 + GetBValue(txtColor) * 0.1);
	return isDefault ? defColor : modColor;
}


COLORREF CAdvancedSettingsList::OnGetCellTextColor(int nRow, int nColumn)
{
	return CMFCListCtrlEx::OnGetCellTextColor(nRow, nColumn);
}


#else // !MPT_MFC_FULL


void COptionsAdvanced::OnCustomDrawList(NMHDR* pNMHDR, LRESULT* pResult)
{
	NMLVCUSTOMDRAW *pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
	*pResult = CDRF_DODEFAULT;
	switch(pLVCD->nmcd.dwDrawStage)
	{
	case CDDS_PREPAINT:
		*pResult = CDRF_NOTIFYITEMDRAW;
		break;
	case CDDS_ITEMPREPAINT:
		{
			const bool isDefault = theApp.GetSettings().GetMap().find(m_indexToPath[pLVCD->nmcd.lItemlParam])->second.IsDefault();
			COLORREF defColor = m_List.GetBkColor();
			COLORREF txtColor = m_List.GetTextColor();
			COLORREF modColor = RGB(GetRValue(defColor) * 0.9 + GetRValue(txtColor) * 0.1, GetGValue(defColor) * 0.9 + GetGValue(txtColor) * 0.1, GetBValue(defColor) * 0.9 + GetBValue(txtColor) * 0.1);
			pLVCD->clrTextBk = isDefault ? defColor : modColor;
		}
		break;
	}	
}


#endif // MPT_MFC_FULL


void COptionsAdvanced::OnOptionDblClick(NMHDR *, LRESULT *)
{
	const int index = m_List.GetSelectionMark();
	if(index < 0)
		return;
	const SettingPath path = m_indexToPath[m_List.GetItemData(index)];
	SettingValue val = theApp.GetSettings().GetMap().find(path)->second;
	if(val.GetType() == SettingTypeBool)
	{
		val = !val.as<bool>();
	} else
	{
		CInputDlg inputDlg(this, _T("Enter new value for ") + mpt::ToCString(path.FormatAsString()), mpt::ToCString(val.FormatValueAsString()));
		if(inputDlg.DoModal() != IDOK)
		{
			return;
		}
		val.SetFromString(inputDlg.resultAsString);
	}
	theApp.GetSettings().Write(path, val);
#if MPT_USTRING_MODE_WIDE
	m_List.SetItemText(index, 2, val.FormatValueAsString().c_str());
#else
	m_List.SetItemText(index, 2, mpt::ToCString(val.FormatValueAsString()));
#endif
	m_List.SetSelectionMark(index);
	OnSettingsChanged();
}


void COptionsAdvanced::OnSaveNow()
{
	TrackerSettings::Instance().SaveSettings();
	theApp.GetSettings().WriteSettings();
}


OPENMPT_NAMESPACE_END