-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorWnd.h
More file actions
373 lines (332 loc) · 8.86 KB
/
Copy pathEditorWnd.h
File metadata and controls
373 lines (332 loc) · 8.86 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
#pragma once
#include "stdafx.h"
#include "resource.h"
#include <atlbase.h>
#include <atlwin.h>
#include <atlstr.h>
#include <string>
#include <WinFile.h>
#include <DataBuffer.h>
#include <atlpath.h>
#include "Scintilla.h"
#include "SciLexer.h"
#include "utf8.h"
extern CAppModule _Module;
class CMainFrame;
template<class TEdit>
class CEditFileCommands
{
CMainFrame* m_pFrame;
public:
BEGIN_MSG_MAP(CEditFileCommands<TEdit>)
ALT_MSG_MAP(2)
COMMAND_ID_HANDLER(ID_FILE_SAVE, OnFileSave)
COMMAND_ID_HANDLER(ID_FILE_SAVE_AS, OnFileSaveAs)
COMMAND_ID_HANDLER(ID_FILE_OPEN, OnFileOpen)
//COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
COMMAND_CODE_HANDLER(EN_CHANGE, OnEditChange)
END_MSG_MAP()
public:
CEditFileCommands(CMainFrame* pFrame) :m_pFrame(pFrame)
{
m_bDirty = false;
}
private:
bool m_bDirty;
CPath m_path;
BOOL ShowFileOpen()
{
COMDLG_FILTERSPEC filter[] = { \
_T("vbscript files"),_T("*.vbs"),
_T("jscript files"), _T("*.js"),
_T("python script files"), _T("*.pys"),
_T("python files"), _T("*.py"),
_T("all files"), _T("*.*")\
};
CShellFileOpenDialog dlg((CString&)m_path,6208UL,(LPCWSTR)0,
filter,5);
BOOL res = FALSE;
if (IDOK == dlg.DoModal()) {
LPTSTR buf = m_path.m_strPath.GetBufferSetLength(256);
HRESULT hr = dlg.GetFilePath(buf,256);
if (SUCCEEDED(hr)) {
TEdit* pEdit = (TEdit*)this;
#ifdef _DEBUG
pEdit->MessageBox((LPCTSTR)m_path);
pEdit->MessageBox(m_path.GetExtension());
#endif
res = TRUE;
}
m_path.m_strPath.ReleaseBuffer();
}
return res;
}
BOOL ShowFileSave() {
BOOL res = FALSE;
COMDLG_FILTERSPEC filter[] = { \
_T("vbscript files"),_T("*.vbs"),
_T("jscript files"), _T("*.js"),
_T("python script files"), _T("*.ps"),
_T("python files"), _T("*.py"),
_T("all files"), _T("*.*")\
};
CShellFileSaveDialog dlg;
if (IDOK == dlg.DoModal()) {
LPTSTR buf = m_path.m_strPath.GetBufferSetLength(256);
HRESULT hr = dlg.GetFilePath(buf,256);
if (SUCCEEDED(hr)) {
res = TRUE;
}
}
return res;
}
BOOL SaveToPath()
{
NO5TL::CWinFile wf;
TEdit* pEdit = (TEdit*)this;
CString s;
NO5TL::CDataBuffer<char> db;
s = pEdit->GetWindowText();
CStringA ss = CT2A(s);
db.Add((LPBYTE)(LPCSTR)ss, ss.GetLength());
db.AddNull();
BOOL res = FALSE;
if (wf.Create(m_path, CREATE_ALWAYS)) {
if (res = wf.Write(db)) {
m_bDirty = false;
res = TRUE;
}
}
return res;
}
public:
LRESULT OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
TEdit* pEdit = (TEdit*)this;
BOOL res = TRUE;
int choice = IDNO;
if (m_bDirty && pEdit->GetWindowTextLength()) {
CString question;
question.LoadStringW(IDS_SAVE);
if (IDYES == (choice = pEdit->MessageBox(question, NULL, MB_YESNOCANCEL))) {
CString path = m_path;
if (path.IsEmpty()) {
OnFileSaveAs(0, 0, 0, res);
}
else {
OnFileSave(0, 0, 0, res);
}
}
}
if (choice != IDCANCEL && ShowFileOpen()) {
NO5TL::CWinFile wf;
BOOL res = wf.Create(m_path, OPEN_EXISTING, GENERIC_READ);
if (res) {
NO5TL::CDataBuffer<char> db;
res = wf.ReadAll<char>(db);
if (res) {
db.AddNull();
TEdit* pEdit = (TEdit*)this;
pEdit->SetWindowText(CA2T(db.GetData()));
m_pFrame->SetPath(m_path);
m_bDirty = false;
}
}
}
return 0;
}
LRESULT OnFileSaveAs(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
if (ShowFileSave()) {
if (SaveToPath()) {
m_bDirty = false;
m_pFrame->SetPath(m_path);
}
}
return 0;
}
LRESULT OnFileSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
if (m_path.GetExtension().IsEmpty()) {
BOOL b;
return OnFileSaveAs(0, 0, 0, b);
}
if (SaveToPath())
m_bDirty = false;
return 0;
}
LRESULT OnEditChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
m_bDirty = true;
return 0;
}
};
class CMainFrame;
class EditorWindow : public CWindowImpl<EditorWindow>,
public CEditFileCommands<EditorWindow>,
public CEditCommands<EditorWindow>,
public CMessageFilter
{
public:
DECLARE_WND_SUPERCLASS(NULL,_T("Scintilla"))
CMainFrame* m_pFrame;
EditorWindow(CMainFrame* pFrame) :CEditFileCommands<EditorWindow>(pFrame),
m_pFrame(pFrame)
{
//
}
BEGIN_MSG_MAP(EditorWindow)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_KEYDOWN, OnKeyDown)
MESSAGE_HANDLER(WM_DESTROY,OnDestroy)
MESSAGE_HANDLER(WM_NOTIFY,OnSciNotify)
CHAIN_COMMANDS_ALT(CEditCommands<EditorWindow>, 1)
CHAIN_COMMANDS_ALT(CEditFileCommands<EditorWindow>,2)
END_MSG_MAP()
LRESULT OnCreate(UINT, WPARAM, LPARAM, BOOL& bHandled) {
//SendMessage(hSci, SCI_SETLEXER, SCLEX_VBSCRIPT, 0);
LRESULT res = DefWindowProc();
SendMessage(SCI_STYLESETFORE, SCE_B_KEYWORD, RGB(0, 0, 255));
SendMessage(SCI_STYLESETFORE, SCE_B_STRING, RGB(163, 21, 21));
SendMessage(SCI_STYLESETFORE, SCE_B_COMMENT, RGB(0, 128, 0));
CMessageLoop *loop = _Module.GetMessageLoop();
loop -> AddMessageFilter(this);
return res;
}
BOOL PreTranslateMessage(MSG* pMsg)
{
return FALSE;
}
std::string BuildAutoList(const char* prefix) {
const char* words =
"and byref byval call case class const dim do each else elseif end "
"erase error event exit explicit false for function get goto if in let loop "
"me new next not nothing on option or property private public redim rem "
"select set sub then true until wend while with";
std::string list;
std::istringstream iss(words);
std::string w;
while (iss >> w) {
if (w.rfind(prefix, 0) == 0) {
if (!list.empty()) list += " ";
list += w;
}
}
return list;
}
std::string BuildAutoListJScript(const char* prefix, const char* words) {
std::string list;
std::istringstream iss(words);
std::string w;
while (iss >> w) {
if (prefix[0] == '\0' || w.rfind(prefix, 0) == 0) {
if (!list.empty()) list += " ";
list += w;
}
}
return list;
}
std::string BuildAutoListPython(const char* prefix) {
const char* words =
"False None True and as assert async await break class continue def del "
"elif else except finally for from global if import in is lambda nonlocal "
"not or pass raise return try while with yield";
std::string list;
std::istringstream iss(words);
std::string w;
while (iss >> w) {
if (w.rfind(prefix, 0) == 0) {
if (!list.empty()) list += " ";
list += w;
}
}
return list;
}
void ShowAutoComplete();
LRESULT OnDestroy(UINT, WPARAM wParam, LPARAM, BOOL& bHandled)
{
CMessageLoop* pLoop = _Module.GetMessageLoop();
pLoop->RemoveMessageFilter(this);
bHandled = FALSE;
return 0;
}
LRESULT OnSciNotify(UINT,WPARAM wParam,LPARAM lParam, BOOL& bHandled)
{
SCNotification* scn = (SCNotification*)lParam;
if (scn->nmhdr.hwndFrom == m_hWnd && scn->nmhdr.code == SCN_CHARADDED) {
char ch = (char)scn->ch;
if (isalnum((unsigned char)ch))
ShowAutoComplete();
}
return 0;
}
LRESULT OnKeyDown(UINT, WPARAM wParam, LPARAM, BOOL& bHandled) {
if ((GetKeyState(VK_CONTROL) & 0x8000) && wParam == 'R') {
int len = SendMessage( SCI_GETTEXTLENGTH, 0, 0);
std::string text(len + 1, '\0');
SendMessage(SCI_GETTEXT, len + 1, (LPARAM)text.data());
//RunVbScript(text.c_str(),m_hWnd);
}
else
bHandled = FALSE;
return 0;
}
BOOL SetWindowText(LPCTSTR psz)
{
CStringA utf8 = wide_to_utf8_char(psz);
SendMessage(SCI_SETTEXT, (WPARAM)0, (LPARAM)(LPCSTR)utf8);
return TRUE;
}
int GetWindowTextLength()
{
return SendMessage(SCI_GETTEXTLENGTH);
}
CStringA GetWindowText(void)
{
int len = GetWindowTextLength();
CStringA s;
LPCSTR p = s.GetBufferSetLength(len + 1);
SendMessage(SCI_GETTEXT, (WPARAM)(len + 1),(LPARAM)p);
return s;
}
void Copy()
{
SendMessage(SCI_COPY);
}
void Cut()
{
SendMessage(SCI_CUT);
}
void Paste()
{
SendMessage(SCI_PASTE);
}
void Undo()
{
SendMessage(SCI_UNDO);
}
void SetSel(int start, int end)
{
SendMessage(SCI_SETSEL, (WPARAM)start, (LPARAM)end);
}
void Clear()
{
SendMessage(SCI_CLEAR);
}
bool HasSelection()
{
return SendMessage(SCI_GETSELECTIONEND) > SendMessage(SCI_GETSELECTIONSTART);
}
bool CanCut()
{
return HasSelection();
}
bool CanCopy()
{
return CanCut();
}
bool CanPaste()
{
return SendMessage(SCI_CANPASTE) ? true : false;
}
};