-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
476 lines (399 loc) · 16.1 KB
/
main.cpp
File metadata and controls
476 lines (399 loc) · 16.1 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include <windows.h>
#include <ole2.h>
#include <string>
#include <vector>
#include "listerplugin.h"
#include "browserhost.h"
#include "bslhighlight.h"
#include "webview2host.h"
#include "monaco_template.h"
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
// --- Globals ---
static HINSTANCE g_hInst = NULL;
static const char* WNDCLASS_NAME = "BSLViewMainWnd";
static const char* PROP_BROWSER = "BSLView_Browser"; // IE browser host
static const char* PROP_WEBVIEW = "BSLView_WebView2"; // WebView2 host
static const char* PROP_TEMPFILE = "BSLView_TempFile"; // temp HTML file path
static char g_iniPath[MAX_PATH] = {0};
// --- INI helpers ---
static int GetIniInt(const char* section, const char* key, int def)
{
return GetPrivateProfileIntA(section, key, def, g_iniPath);
}
static std::string GetIniStr(const char* section, const char* key, const char* def)
{
char buf[512];
GetPrivateProfileStringA(section, key, def, buf, sizeof(buf), g_iniPath);
return buf;
}
// --- File reading ---
static std::wstring ReadFileToWString(const char* path)
{
HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) return L"";
DWORD fileSize = GetFileSize(hFile, NULL);
if (fileSize == 0 || fileSize == INVALID_FILE_SIZE) {
CloseHandle(hFile);
return L"";
}
std::vector<BYTE> data(fileSize);
DWORD bytesRead = 0;
ReadFile(hFile, data.data(), fileSize, &bytesRead, NULL);
CloseHandle(hFile);
if (bytesRead < fileSize) data.resize(bytesRead);
const BYTE* p = data.data();
size_t sz = data.size();
// UTF-8 BOM
if (sz >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF) {
int wlen = MultiByteToWideChar(CP_UTF8, 0, (const char*)p + 3, (int)(sz - 3), NULL, 0);
std::wstring result(wlen, 0);
MultiByteToWideChar(CP_UTF8, 0, (const char*)p + 3, (int)(sz - 3), &result[0], wlen);
return result;
}
// UTF-16 LE BOM
if (sz >= 2 && p[0] == 0xFF && p[1] == 0xFE) {
return std::wstring((const wchar_t*)(p + 2), (sz - 2) / sizeof(wchar_t));
}
// UTF-16 BE BOM
if (sz >= 2 && p[0] == 0xFE && p[1] == 0xFF) {
std::wstring result((sz - 2) / sizeof(wchar_t), 0);
for (size_t i = 0; i < result.size(); i++)
result[i] = (wchar_t)((p[2 + i * 2] << 8) | p[2 + i * 2 + 1]);
return result;
}
// Try UTF-8
int wlen = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (const char*)p, (int)sz, NULL, 0);
if (wlen > 0) {
std::wstring result(wlen, 0);
MultiByteToWideChar(CP_UTF8, 0, (const char*)p, (int)sz, &result[0], wlen);
return result;
}
// Fallback: Windows-1251
wlen = MultiByteToWideChar(1251, 0, (const char*)p, (int)sz, NULL, 0);
std::wstring result(wlen, 0);
MultiByteToWideChar(1251, 0, (const char*)p, (int)sz, &result[0], wlen);
return result;
}
// --- Check file extension ---
static bool HasExtension(const char* filePath, const char* extensions)
{
const char* dot = strrchr(filePath, '.');
if (!dot) return false;
dot++;
std::string ext(dot);
for (auto& c : ext) c = (char)tolower((unsigned char)c);
std::string exts(extensions);
for (auto& c : exts) c = (char)tolower((unsigned char)c);
size_t pos = 0;
while (pos < exts.size()) {
size_t sep = exts.find(';', pos);
if (sep == std::string::npos) sep = exts.size();
if (exts.substr(pos, sep - pos) == ext) return true;
pos = sep + 1;
}
return false;
}
// --- JavaScript string escaping for Monaco content ---
static std::string JsEscapeWide(const std::wstring& src)
{
std::string out;
out.reserve(src.size() * 2);
for (wchar_t ch : src) {
if (ch == L'\\') out += "\\\\";
else if (ch == L'\'') out += "\\'";
else if (ch == L'\n') out += "\\n";
else if (ch == L'\r') out += "\\r";
else if (ch == L'\t') out += "\\t";
else if (ch < 0x20) { /* skip control chars */ }
else if (ch < 0x80) out += (char)ch;
else {
// Output as UTF-8
if (ch < 0x800) {
out += (char)(0xC0 | (ch >> 6));
out += (char)(0x80 | (ch & 0x3F));
} else {
out += (char)(0xE0 | (ch >> 12));
out += (char)(0x80 | ((ch >> 6) & 0x3F));
out += (char)(0x80 | (ch & 0x3F));
}
}
}
return out;
}
// --- Generate Monaco HTML ---
static std::string GenerateMonacoHTML(const std::wstring& content, bool darkMode, int fontSize)
{
std::string html(MONACO_HTML_PART1);
html += MONACO_HTML_PART2;
// Replace all occurrences of %THEME%
std::string theme = darkMode ? "bsl-dark" : "bsl-light";
for (size_t pos = html.find("%THEME%"); pos != std::string::npos; pos = html.find("%THEME%", pos))
html.replace(pos, 7, theme);
// Replace all occurrences of %FONTSIZE%
std::string fs = std::to_string(fontSize > 0 ? fontSize : 14);
for (size_t pos = html.find("%FONTSIZE%"); pos != std::string::npos; pos = html.find("%FONTSIZE%", pos))
html.replace(pos, 10, fs);
// Replace BSL_CONTENT with the escaped file content
std::string escaped = "'" + JsEscapeWide(content) + "'";
size_t pos = html.find("BSL_CONTENT");
if (pos != std::string::npos) html.replace(pos, 11, escaped);
return html;
}
// --- Write temp HTML file ---
static std::wstring WriteTempHTML(const std::string& html)
{
wchar_t tempDir[MAX_PATH];
GetTempPathW(MAX_PATH, tempDir);
std::wstring tempPath = std::wstring(tempDir) + L"BSLView_monaco.html";
HANDLE hFile = CreateFileW(tempPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) return L"";
// Write UTF-8 BOM + content
BYTE bom[] = {0xEF, 0xBB, 0xBF};
DWORD written;
WriteFile(hFile, bom, 3, &written, NULL);
WriteFile(hFile, html.c_str(), (DWORD)html.size(), &written, NULL);
CloseHandle(hFile);
return tempPath;
}
// --- Window procedure ---
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_SIZE: {
CWebView2Host* wv = (CWebView2Host*)GetPropA(hwnd, PROP_WEBVIEW);
if (wv) { wv->Resize(); return 0; }
CBrowserHost* browser = (CBrowserHost*)GetPropA(hwnd, PROP_BROWSER);
if (browser) browser->Resize();
return 0;
}
case WM_DESTROY: {
CWebView2Host* wv = (CWebView2Host*)GetPropA(hwnd, PROP_WEBVIEW);
if (wv) {
wv->Close();
delete wv;
RemovePropA(hwnd, PROP_WEBVIEW);
}
CBrowserHost* browser = (CBrowserHost*)GetPropA(hwnd, PROP_BROWSER);
if (browser) {
browser->Quit();
browser->Release();
RemovePropA(hwnd, PROP_BROWSER);
}
// Delete temp file
wchar_t* tempFile = (wchar_t*)GetPropW(hwnd, L"BSLView_TempFile");
if (tempFile) {
DeleteFileW(tempFile);
free(tempFile);
RemovePropW(hwnd, L"BSLView_TempFile");
}
return 0;
}
}
return DefWindowProcA(hwnd, msg, wParam, lParam);
}
// --- Load BSL with IE fallback ---
static bool LoadBSLFileIE(CBrowserHost* browser, const char* filePath, int showFlags)
{
std::string bslExts = GetIniStr("Extensions", "BSLExtensions", "bsl;os");
std::string queryExts = GetIniStr("Extensions", "QueryExtensions", "sdbl;query");
if (!HasExtension(filePath, bslExts.c_str()) &&
!HasExtension(filePath, queryExts.c_str()))
return false;
std::wstring content = ReadFileToWString(filePath);
if (content.empty()) return false;
BSLHighlightOptions opts;
opts.darkMode = (showFlags & lcp_darkmode) != 0;
opts.lineNumbers = GetIniInt("Options", "LineNumbers", 1) != 0;
std::string fontFamily = GetIniStr("Options", "FontFamily", "Consolas, Courier New, monospace");
opts.fontFamily = fontFamily.c_str();
opts.fontSize = GetIniInt("Options", "FontSize", 14);
opts.tabSize = GetIniInt("Options", "TabSize", 4);
std::string html = HasExtension(filePath, queryExts.c_str())
? HighlightSDBL(content.c_str(), content.size(), opts)
: HighlightBSL(content.c_str(), content.size(), opts);
browser->LoadHTML(html);
return true;
}
// --- DLL entry ---
BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID)
{
if (reason == DLL_PROCESS_ATTACH) {
g_hInst = (HINSTANCE)hModule;
DisableThreadLibraryCalls(hModule);
GetModuleFileNameA(g_hInst, g_iniPath, MAX_PATH);
char* lastSlash = strrchr(g_iniPath, '\\');
if (!lastSlash) lastSlash = strrchr(g_iniPath, '/');
if (lastSlash) strcpy(lastSlash + 1, "BSLView.ini");
WNDCLASSA wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = g_hInst;
wc.lpszClassName = WNDCLASS_NAME;
RegisterClassA(&wc);
}
return TRUE;
}
// --- WLX Exports ---
extern "C" {
__declspec(dllexport)
void __stdcall ListSetDefaultParams(ListDefaultParamStruct* dps)
{
GetModuleFileNameA(g_hInst, g_iniPath, MAX_PATH);
char* lastSlash = strrchr(g_iniPath, '\\');
if (!lastSlash) lastSlash = strrchr(g_iniPath, '/');
if (lastSlash) strcpy(lastSlash + 1, "BSLView.ini");
}
__declspec(dllexport)
void __stdcall ListGetDetectString(char* DetectString, int maxlen)
{
strncpy(DetectString, "EXT=\"BSL\" | EXT=\"OS\" | EXT=\"SDBL\" | EXT=\"QUERY\"", maxlen - 1);
DetectString[maxlen - 1] = 0;
}
__declspec(dllexport)
HWND __stdcall ListLoad(HWND ParentWin, char* FileToLoad, int ShowFlags)
{
std::string bslExts = GetIniStr("Extensions", "BSLExtensions", "bsl;os");
std::string queryExts = GetIniStr("Extensions", "QueryExtensions", "sdbl;query");
if (!HasExtension(FileToLoad, bslExts.c_str()) &&
!HasExtension(FileToLoad, queryExts.c_str()))
return NULL;
RECT rcParent;
GetClientRect(ParentWin, &rcParent);
HWND hwnd = CreateWindowExA(0, WNDCLASS_NAME, "",
WS_CHILD | WS_VISIBLE,
0, 0, rcParent.right, rcParent.bottom,
ParentWin, NULL, g_hInst, NULL);
if (!hwnd) return NULL;
// Read file content
std::wstring content = ReadFileToWString(FileToLoad);
if (content.empty()) { DestroyWindow(hwnd); return NULL; }
int fontSize = GetIniInt("Options", "FontSize", 14);
// Theme: auto (follow TC), light, dark
std::string themeOpt = GetIniStr("Options", "Theme", "auto");
bool darkMode;
if (themeOpt == "light") darkMode = false;
else if (themeOpt == "dark") darkMode = true;
else darkMode = (ShowFlags & lcp_darkmode) != 0;
// Try WebView2 first (Monaco Editor)
bool useMonaco = GetIniInt("Options", "UseMonaco", 1) != 0;
if (useMonaco) {
CWebView2Host* wv = new CWebView2Host();
if (wv->Create(hwnd)) {
SetPropA(hwnd, PROP_WEBVIEW, (HANDLE)wv);
// Store the source file path for save functionality
int wlen = MultiByteToWideChar(CP_ACP, 0, FileToLoad, -1, NULL, 0);
std::wstring wFilePath(wlen - 1, 0);
MultiByteToWideChar(CP_ACP, 0, FileToLoad, -1, &wFilePath[0], wlen);
wv->mFilePath = wFilePath;
wv->SetupMessageHandler();
std::string html = GenerateMonacoHTML(content, darkMode, fontSize);
std::wstring tempPath = WriteTempHTML(html);
if (!tempPath.empty()) {
// Store temp file path for cleanup
wchar_t* stored = _wcsdup(tempPath.c_str());
SetPropW(hwnd, L"BSLView_TempFile", (HANDLE)stored);
wv->NavigateToFile(tempPath);
return hwnd;
}
}
delete wv;
RemovePropA(hwnd, PROP_WEBVIEW);
}
// Fallback to IE with C++ highlighter
OleInitialize(NULL);
CBrowserHost* browser = new CBrowserHost();
browser->mAllowScripts = true;
if (browser->CreateBrowser(hwnd)) {
SetPropA(hwnd, PROP_BROWSER, (HANDLE)browser);
if (LoadBSLFileIE(browser, FileToLoad, ShowFlags))
return hwnd;
browser->Quit();
browser->Release();
RemovePropA(hwnd, PROP_BROWSER);
}
DestroyWindow(hwnd);
return NULL;
}
__declspec(dllexport)
int __stdcall ListLoadNext(HWND ParentWin, HWND PluginWin, char* FileToLoad, int ShowFlags)
{
std::wstring content = ReadFileToWString(FileToLoad);
if (content.empty()) return LISTPLUGIN_ERROR;
int fontSize = GetIniInt("Options", "FontSize", 14);
std::string themeOpt = GetIniStr("Options", "Theme", "auto");
bool darkMode;
if (themeOpt == "light") darkMode = false;
else if (themeOpt == "dark") darkMode = true;
else darkMode = (ShowFlags & lcp_darkmode) != 0;
// Try WebView2
CWebView2Host* wv = (CWebView2Host*)GetPropA(PluginWin, PROP_WEBVIEW);
if (wv && wv->mReady) {
// Delete old temp file
wchar_t* oldTemp = (wchar_t*)GetPropW(PluginWin, L"BSLView_TempFile");
if (oldTemp) { DeleteFileW(oldTemp); free(oldTemp); }
std::string html = GenerateMonacoHTML(content, darkMode, fontSize);
std::wstring tempPath = WriteTempHTML(html);
if (!tempPath.empty()) {
wchar_t* stored = _wcsdup(tempPath.c_str());
SetPropW(PluginWin, L"BSLView_TempFile", (HANDLE)stored);
wv->NavigateToFile(tempPath);
return LISTPLUGIN_OK;
}
}
// Fallback IE
CBrowserHost* browser = (CBrowserHost*)GetPropA(PluginWin, PROP_BROWSER);
if (browser) {
if (LoadBSLFileIE(browser, FileToLoad, ShowFlags))
return LISTPLUGIN_OK;
}
return LISTPLUGIN_ERROR;
}
__declspec(dllexport)
void __stdcall ListCloseWindow(HWND ListWin)
{
DestroyWindow(ListWin);
}
__declspec(dllexport)
int __stdcall ListSearchText(HWND ListWin, char* SearchString, int SearchParameter)
{
// WebView2: Monaco has built-in Ctrl+F search, so this is just for IE fallback
CBrowserHost* browser = (CBrowserHost*)GetPropA(ListWin, PROP_BROWSER);
if (!browser || !SearchString) return LISTPLUGIN_ERROR;
int wlen = MultiByteToWideChar(CP_ACP, 0, SearchString, -1, NULL, 0);
std::wstring ws(wlen, 0);
MultiByteToWideChar(CP_ACP, 0, SearchString, -1, &ws[0], wlen);
return browser->FindText(ws.c_str(), SearchParameter) ? LISTPLUGIN_OK : LISTPLUGIN_ERROR;
}
__declspec(dllexport)
int __stdcall ListSearchTextW(HWND ListWin, WCHAR* SearchString, int SearchParameter)
{
CBrowserHost* browser = (CBrowserHost*)GetPropA(ListWin, PROP_BROWSER);
if (!browser || !SearchString) return LISTPLUGIN_ERROR;
return browser->FindText(SearchString, SearchParameter) ? LISTPLUGIN_OK : LISTPLUGIN_ERROR;
}
__declspec(dllexport)
int __stdcall ListSendCommand(HWND ListWin, int Command, int Parameter)
{
// WebView2: Monaco handles copy/selectall internally
CWebView2Host* wv = (CWebView2Host*)GetPropA(ListWin, PROP_WEBVIEW);
if (wv) return LISTPLUGIN_OK;
CBrowserHost* browser = (CBrowserHost*)GetPropA(ListWin, PROP_BROWSER);
if (!browser || !browser->mWebBrowser) return LISTPLUGIN_ERROR;
IDispatch* pDisp = NULL;
browser->mWebBrowser->get_Document(&pDisp);
if (!pDisp) return LISTPLUGIN_ERROR;
IHTMLDocument2* pDoc = NULL;
pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
pDisp->Release();
if (!pDoc) return LISTPLUGIN_ERROR;
VARIANT_BOOL success;
VARIANT vIn;
VariantInit(&vIn);
if (Command == lc_copy)
pDoc->execCommand(L"Copy", VARIANT_FALSE, vIn, &success);
else if (Command == lc_selectall)
pDoc->execCommand(L"SelectAll", VARIANT_FALSE, vIn, &success);
pDoc->Release();
return LISTPLUGIN_OK;
}
} // extern "C"