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
|
#include "stdafx.h"
CMLuaOptionsMain::CMLuaOptionsMain() :
CDlgBase(g_plugin, IDD_OPTIONSMAIN),
m_popupOnError(this, IDC_POPUPONERROR),
m_popupOnObsolete(this, IDC_POPUPONOBSOLETE),
m_scriptsList(this, IDC_SCRIPTS),
m_reload(this, IDC_RELOAD)
{
CreateLink(m_popupOnError, "PopupOnError", DBVT_BYTE, 1);
CreateLink(m_popupOnObsolete, "PopupOnObsolete", DBVT_BYTE, 1);
m_scriptsList.OnClick = Callback(this, &CMLuaOptionsMain::OnScriptListClick);
m_reload.OnClick = Callback(this, &CMLuaOptionsMain::OnReload);
}
static int ScriptStatusToIcon(ScriptStatus status)
{
switch (status) {
case ScriptStatus::None:
return -1;
case ScriptStatus::Loaded:
return 0;
case ScriptStatus::Failed:
return 1;
}
return 0;
}
void CMLuaOptionsMain::LoadScripts()
{
for (auto &script : g_plugin.m_scripts.rev_iter()) {
int iIcon = ScriptStatusToIcon(script->GetStatus());
int iItem = m_scriptsList.AddItem(script->GetName(), iIcon, (LPARAM)script);
m_scriptsList.SetCheckState(iItem, script->IsEnabled());
m_scriptsList.SetItem(iItem, 1, TranslateT("Open"), 2);
m_scriptsList.SetItem(iItem, 2, TranslateT("Reload"), 3);
if (!script->IsBinary())
m_scriptsList.SetItem(iItem, 3, TranslateT("Compile"), 4);
}
}
bool CMLuaOptionsMain::OnInitDialog()
{
CDlgBase::OnInitDialog();
m_scriptsList.SetExtendedListViewStyle(LVS_EX_SUBITEMIMAGES | LVS_EX_FULLROWSELECT | LVS_EX_CHECKBOXES | LVS_EX_INFOTIP);
HIMAGELIST hImageList = m_scriptsList.CreateImageList(LVSIL_SMALL);
ImageList_AddIcon(hImageList, g_plugin.getIcon(IDI_LOADED));
ImageList_AddIcon(hImageList, g_plugin.getIcon(IDI_FAILED));
ImageList_AddIcon(hImageList, g_plugin.getIcon(IDI_OPEN));
ImageList_AddIcon(hImageList, g_plugin.getIcon(IDI_RELOAD));
ImageList_AddIcon(hImageList, g_plugin.getIcon(IDI_COMPILE));
wchar_t scriptDir[MAX_PATH];
FoldersGetCustomPathW(g_hScriptsFolder, scriptDir, _countof(scriptDir), VARSW(MIRLUA_PATHT));
wchar_t relativeScriptDir[MAX_PATH];
PathToRelativeW(scriptDir, relativeScriptDir, nullptr);
wchar_t header[MAX_PATH + 100];
mir_snwprintf(header, L"%s (%s)", TranslateT("Common scripts"), relativeScriptDir);
m_scriptsList.AddColumn(0, TranslateT("Script"), 346);
m_scriptsList.AddColumn(1, nullptr, 34 - GetSystemMetrics(SM_CXVSCROLL));
m_scriptsList.AddColumn(2, nullptr, 36 - GetSystemMetrics(SM_CXVSCROLL));
m_scriptsList.AddColumn(3, nullptr, 36 - GetSystemMetrics(SM_CXVSCROLL));
LoadScripts();
return true;
}
bool CMLuaOptionsMain::OnApply()
{
int count = m_scriptsList.GetItemCount();
for (int iItem = 0; iItem < count; iItem++) {
CMLuaScript *script = (CMLuaScript*)m_scriptsList.GetItemData(iItem);
if (!m_scriptsList.GetCheckState(iItem))
script->Disable();
else
script->Enable();
}
return true;
}
void CMLuaOptionsMain::OnScriptListClick(CCtrlListView::TEventInfo *evt)
{
LVITEM lvi = {};
lvi.iItem = evt->nmlvia->iItem;
if (lvi.iItem == -1)
return;
lvi.pszText = (LPTSTR)mir_calloc(MAX_PATH * sizeof(wchar_t));
lvi.cchTextMax = MAX_PATH;
lvi.mask = LVIF_GROUPID | LVIF_TEXT | LVIF_PARAM;
m_scriptsList.GetItem(&lvi);
lvi.iSubItem = evt->nmlvia->iSubItem;
CMLuaScript *script = (CMLuaScript*)lvi.lParam;
switch (lvi.iSubItem) {
case 1:
ShellExecute(m_hwnd, L"open", script->GetFilePath(), nullptr, nullptr, SW_SHOWNORMAL);
break;
case 2:
script->Reload();
lvi.mask = LVIF_IMAGE;
lvi.iSubItem = 0;
lvi.iImage = ScriptStatusToIcon(script->GetStatus());
m_scriptsList.SetItem(&lvi);
m_scriptsList.Update(lvi.iItem);
break;
case 3:
if (script->IsBinary())
break;
script->Compile();
lvi.mask = LVIF_IMAGE;
lvi.iSubItem = 0;
lvi.iImage = ScriptStatusToIcon(script->GetStatus());
m_scriptsList.SetItem(&lvi);
m_scriptsList.Update(lvi.iItem);
break;
}
mir_free(lvi.pszText);
}
void CMLuaOptionsMain::OnReload(CCtrlBase*)
{
m_scriptsList.SetSilent(true);
m_scriptsList.DeleteAllItems();
g_plugin.ReloadLuaScripts();
LoadScripts();
m_scriptsList.SetSilent(false);
}
/***********************************************/
CMLuaEvaluateOptions::CMLuaEvaluateOptions() :
CDlgBase(g_plugin, IDD_OPTIONSEVALUATE),
m_script(this, IDC_SCRIPTTEXT),
m_result(this, IDC_SCRIPTRESULT),
m_autoEval(this, IDC_AUTOEVAL),
m_evaluate(this, IDC_EVALUATE)
{
this->L = lua_newthread(g_plugin.L);
threadRef = luaL_ref(L, LUA_REGISTRYINDEX);
CreateLink(m_autoEval, "AutoEval", DBVT_BYTE, 0);
m_autoEval.OnChange = Callback(this, &CMLuaEvaluateOptions::OnAutoEvalChange);
m_evaluate.OnClick = Callback(this, &CMLuaEvaluateOptions::OnEvaluate);
}
CMLuaEvaluateOptions::~CMLuaEvaluateOptions()
{
luaL_unref(L, LUA_REGISTRYINDEX, threadRef);
threadRef = 0;
}
bool CMLuaEvaluateOptions::OnInitDialog()
{
m_script.SetText(m_pPlugin.getMStringW("Script"));
if (m_autoEval.IsChecked()) {
m_script.OnChange = Callback(this, &CMLuaEvaluateOptions::OnEvaluate);
m_evaluate.Click();
}
return true;
}
bool CMLuaEvaluateOptions::OnApply()
{
m_pPlugin.setWString("Script", m_script.GetText());
return true;
}
void CMLuaEvaluateOptions::OnAutoEvalChange(CCtrlBase*)
{
if (m_autoEval.IsChecked()) {
m_script.OnChange = Callback(this, &CMLuaEvaluateOptions::OnEvaluate);
m_evaluate.Click();
}
else m_script.OnChange = CCallback<CCtrlBase>();
}
void CMLuaEvaluateOptions::OnEvaluate(CCtrlBase*)
{
ptrW script(m_script.GetText());
CMLuaEnvironment env(L);
env.Eval(script);
m_result.SetText(ptrW(mir_utf8decodeW(lua_tostring(L, -1))));
lua_pop(L, 1);
env.Unload();
}
/***********************************************/
|