summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/mlua_options.cpp
blob: caca892e487fccc31233ed9cc66315878e38522b (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
#include "stdafx.h"

CCtrlScriptList::CCtrlScriptList(CDlgBase* dlg, int ctrlId)
	: CCtrlListView(dlg, ctrlId)
{
}

BOOL CCtrlScriptList::OnNotify(int idCtrl, NMHDR *pnmh)
{
	if (pnmh->code == NM_CLICK)
	{
		TEventInfo evt = { this, pnmh };
		OnClick(&evt);
		return TRUE;
	}
	return CCtrlListView::OnNotify(idCtrl, pnmh);
}

/****************************************/

CLuaOptions::CLuaOptions(int idDialog) : CDlgBase(g_hInstance, idDialog),
	m_scripts(this, IDC_SCRIPTS), isScriptListInit(false),
	m_reload(this, IDC_RELOAD)
{
	m_scripts.OnClick = Callback(this, &CLuaOptions::OnScriptListClick);
	m_reload.OnClick = Callback(this, &CLuaOptions::OnReload);
}

void CLuaOptions::CreateLink(CCtrlData& ctrl, const char *szSetting, BYTE type, DWORD iValue)
{
	ctrl.CreateDbLink(MODULE, szSetting, type, iValue);
}

void CLuaOptions::CreateLink(CCtrlData& ctrl, const char *szSetting, TCHAR *szValue)
{
	ctrl.CreateDbLink(MODULE, szSetting, szValue);
}

int CLuaOptions::OnOptionsInit(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.flags = ODPF_BOLDGROUPS | ODPF_TCHAR | ODPF_DONTTRANSLATE;
	odp.ptszGroup = LPGENT("Scripts");
	odp.ptszTitle = _T("Lua");
	odp.pDialog = CLuaOptions::CreateOptionsPage();
	Options_AddPage(wParam, &odp);

	return 0;
}

void CLuaOptions::LoadScripts(const TCHAR *scriptDir, int iGroup)
{
	TCHAR searchMask[MAX_PATH];
	mir_sntprintf(searchMask, _T("%s\\%s"), scriptDir, _T("*.lua"));

	WIN32_FIND_DATA fd;
	HANDLE hFind = FindFirstFile(searchMask, &fd);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			{
				int iItem = m_scripts.AddItem(fd.cFileName, -1, NULL, iGroup);
				if (db_get_b(NULL, MODULE, _T2A(fd.cFileName), 1))
					m_scripts.SetCheckState(iItem, TRUE);
				m_scripts.SetItem(iItem, 1, _T(""), 0);
				m_scripts.SetItem(iItem, 2, _T(""), 1);
			}
		} while (FindNextFile(hFind, &fd));
		FindClose(hFind);
	}
}

void CLuaOptions::LoadScripts()
{
	TCHAR scriptDir[MAX_PATH], relativeScriptDir[MAX_PATH], header[MAX_PATH + 100];
	FoldersGetCustomPathT(g_hCommonFolderPath, scriptDir, _countof(scriptDir), VARST(COMMON_SCRIPTS_PATHT));
	PathToRelativeT(scriptDir, relativeScriptDir, NULL);
	mir_sntprintf(header, _T("%s (%s)"), TranslateT("Common scripts"), relativeScriptDir);
	m_scripts.AddGroup(0, header);
	LoadScripts(scriptDir, 0);

	FoldersGetCustomPathT(g_hCustomFolderPath, scriptDir, _countof(scriptDir), VARST(CUSTOM_SCRIPTS_PATHT));
	PathToRelativeT(scriptDir, relativeScriptDir, NULL);
	mir_sntprintf(header, _T("%s (%s)"), TranslateT("Custom scripts"), relativeScriptDir);
	m_scripts.AddGroup(1, header);
	LoadScripts(scriptDir, 1);
}

void CLuaOptions::OnInitDialog()
{
	CDlgBase::OnInitDialog();

	m_scripts.SetExtendedListViewStyle(LVS_EX_SUBITEMIMAGES | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES | LVS_EX_INFOTIP);
	m_scripts.EnableGroupView(TRUE);
	m_scripts.AddColumn(0, _T("Script"), 420);
	m_scripts.AddColumn(1, NULL, 32 - GetSystemMetrics(SM_CXVSCROLL));
	m_scripts.AddColumn(2, NULL, 32 - GetSystemMetrics(SM_CXVSCROLL));

	HIMAGELIST hImageList = m_scripts.CreateImageList(LVSIL_SMALL);
	ImageList_AddIcon(hImageList, LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_OPEN)));
	ImageList_AddIcon(hImageList, LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_RELOAD)));

	LoadScripts();

	isScriptListInit = true;
}

void CLuaOptions::OnApply()
{
	int count = m_scripts.GetItemCount();
	for (int iItem = 0; iItem < count; iItem++)
	{
		TCHAR fileName[MAX_PATH];
		m_scripts.GetItemText(iItem, 0, fileName, _countof(fileName));
		if (!m_scripts.GetCheckState(iItem))
			db_set_b(NULL, MODULE, _T2A(fileName), 0);
		else
			db_unset(NULL, MODULE, _T2A(fileName));
	}
}

INT_PTR CLuaOptions::DlgProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_NOTIFY:
	{
		LPNMHDR lpnmHdr = (LPNMHDR)lParam;
		if (lpnmHdr->idFrom == m_scripts.GetCtrlId() && lpnmHdr->code == LVN_ITEMCHANGED)
		{
			LPNMLISTVIEW pnmv = (LPNMLISTVIEW)lParam;
			if (pnmv->uChanged & LVIF_STATE && pnmv->uNewState & LVIS_STATEIMAGEMASK)
			{
				if (isScriptListInit)
					NotifyChange();
			}
		}
	}
	break;
	}

	return CDlgBase::DlgProc(msg, wParam, lParam);
}

void CLuaOptions::OnScriptListClick(CCtrlListView::TEventInfo *evt)
{
	LVITEM lvi = { 0 };
	lvi.iItem = evt->nmlvia->iItem;
	if (lvi.iItem == -1) return;
	lvi.pszText = (LPTSTR)mir_calloc(MAX_PATH * sizeof(TCHAR));
	lvi.cchTextMax = MAX_PATH;
	lvi.mask = LVIF_GROUPID | LVIF_TEXT;
	evt->treeviewctrl->GetItem(&lvi);
	lvi.iSubItem = evt->nmlvia->iSubItem;
	if (lvi.iSubItem == 1)
	{
		TCHAR path[MAX_PATH];
		if (lvi.iGroupId == 0)
			FoldersGetCustomPathT(g_hCommonFolderPath, path, _countof(path), VARST(COMMON_SCRIPTS_PATHT));
		else
			FoldersGetCustomPathT(g_hCustomFolderPath, path, _countof(path), VARST(CUSTOM_SCRIPTS_PATHT));
		ShellExecute(m_hwnd, NULL, lvi.pszText, NULL, path, SW_SHOWNORMAL);
	}
	else if (lvi.iSubItem == 2)
	{
		TCHAR path[MAX_PATH];
		if (lvi.iGroupId == 0)
			FoldersGetCustomPathT(g_hCommonFolderPath, path, _countof(path), VARST(COMMON_SCRIPTS_PATHT));
		else
			FoldersGetCustomPathT(g_hCustomFolderPath, path, _countof(path), VARST(CUSTOM_SCRIPTS_PATHT));
		mir_sntprintf(path, _T("%s\\%s"), path, lvi.pszText);
		g_mLua->Reload(path);
	}
	mir_free(lvi.pszText);
}

void CLuaOptions::OnReload(CCtrlBase*)
{
	isScriptListInit = false;
	m_scripts.DeleteAllItems();
	LoadScripts();
	isScriptListInit = true;
	g_mLua->Reload();
}