icrEditView.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include "stdafx.h"
  2. #include "icrEdit.h"
  3. #include "SystemFunctionList.h"
  4. #include "public.h"
  5. #include "icrEditDoc.h"
  6. #include "icrEditView.h"
  7. #include "Mainfrm.h"
  8. #include "SelectVariantDlg.h"
  9. #include "scriptproc.h"
  10. #include "scriptobject.h"
  11. #define unsigned int UWORD
  12. char * g_CurrentDir;
  13. extern BOOL ProcessScript(CString& strScript);
  14. DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4, 0xb0, 0x0,
  15. 0xaa, 0x0, 0x4a, 0x55, 0xe8);
  16. // 错误输出
  17. extern void HRVERIFY(HRESULT hr, char * msg);
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CIcrEditView
  20. IMPLEMENT_DYNCREATE(CIcrEditView, CSynEditView)
  21. BEGIN_MESSAGE_MAP(CIcrEditView, CSynEditView)
  22. //{{AFX_MSG_MAP(CIcrEditView)
  23. ON_WM_KEYDOWN()
  24. ON_WM_KEYUP()
  25. ON_WM_LBUTTONDOWN()
  26. ON_WM_LBUTTONUP()
  27. ON_WM_RBUTTONDOWN()
  28. ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
  29. ON_COMMAND(ID_CHECK, OnCheck)
  30. ON_COMMAND(ID_SELANALOG, OnSelAnalog)
  31. ON_COMMAND(ID_FUNCTION, OnFunction)
  32. ON_COMMAND(ID_SELDIGITAL, OnSelDigital)
  33. //}}AFX_MSG_MAP
  34. // Standard printing commands
  35. ON_COMMAND(ID_FILE_PRINT, CSynEditView::OnFilePrint)
  36. ON_COMMAND(ID_FILE_PRINT_DIRECT, CSynEditView::OnFilePrint)
  37. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CSynEditView::OnFilePrintPreview)
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CIcrEditView construction/destruction
  41. CIcrEditView::CIcrEditView()
  42. {
  43. // TODO: add construction code here
  44. }
  45. CIcrEditView::~CIcrEditView()
  46. {
  47. }
  48. BOOL CIcrEditView::PreCreateWindow(CREATESTRUCT& cs)
  49. {
  50. // TODO: Modify the Window class or styles here by modifying
  51. // the CREATESTRUCT cs
  52. return CSynEditView::PreCreateWindow(cs);
  53. }
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CIcrEditView drawing
  56. void CIcrEditView::OnDraw(CDC* pDC)
  57. {
  58. // TODO: add draw code for native data here
  59. }
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CIcrEditView printing
  62. BOOL CIcrEditView::OnPreparePrinting(CPrintInfo* pInfo)
  63. {
  64. // default preparation
  65. return DoPreparePrinting(pInfo);
  66. }
  67. void CIcrEditView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  68. {
  69. // TODO: add extra initialization before printing
  70. }
  71. void CIcrEditView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  72. {
  73. // TODO: add cleanup after printing
  74. }
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CIcrEditView diagnostics
  77. #ifdef _DEBUG
  78. #endif //_DEBUG
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CIcrEditView message handlers
  81. LRESULT CIcrEditView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  82. {
  83. // TODO: Add your specialized code here and/or call the base class
  84. return CSynEditView::WindowProc(message, wParam, lParam);
  85. }
  86. void CIcrEditView::ShowCursorPosition()
  87. {
  88. long start, end;
  89. CRichEditCtrl& richctrl = GetRichEditCtrl();
  90. richctrl.GetSel(start, end);
  91. long row = richctrl.LineFromChar(start);
  92. long column = start - richctrl.LineIndex(row);
  93. CString strShow;
  94. strShow.Format(_T("Ln:%d, Col:%d"), row+1, column+1);
  95. CMainFrame *mainfrm = (CMainFrame *)AfxGetMainWnd();
  96. CStatusBarCtrl &statusCtrl = mainfrm->m_wndStatusBar.GetStatusBarCtrl();
  97. statusCtrl.SetText(strShow, 1, 0);
  98. }
  99. CString CIcrEditView::GetCursorString()
  100. {
  101. long start, end;
  102. CRichEditCtrl& richctrl = GetRichEditCtrl();
  103. richctrl.GetSel(start, end);
  104. long startline = richctrl.LineFromChar(start);
  105. long endlie = richctrl.LineFromChar(end);
  106. if(startline == endlie && start != end) //用户选定了文本
  107. {
  108. return richctrl.GetSelText();
  109. }
  110. else
  111. {
  112. int nlnstart, nlnend;
  113. long line = richctrl.LineFromChar(end);
  114. int ntmp = end-richctrl.LineIndex(line); //计算行内偏移
  115. CString strline;
  116. GetLineString(line, strline);
  117. int i=0, noffset=0;
  118. while(noffset!=ntmp) //得到光标所在行的字符串中的位置(字符串中中文为两个字符)
  119. {
  120. if(IsDBCSLeadByte(strline[i])) ++i;
  121. ++i; ++noffset;
  122. }
  123. ntmp = i;
  124. while(ntmp>0)
  125. {
  126. --ntmp;
  127. if(!isalnum(strline[ntmp]) && strline[ntmp]!='_')
  128. {
  129. if(ntmp>0)
  130. {
  131. if(IsDBCSLeadByte(strline[ntmp-1])) //检查是不是中文字符
  132. {
  133. --ntmp;
  134. }
  135. else
  136. break;
  137. }
  138. }
  139. }
  140. if(!(isalnum(strline[ntmp]) || IsDBCSLeadByte(strline[ntmp])) || strline[ntmp]=='_')
  141. nlnstart = ntmp+1;
  142. else
  143. nlnstart = ntmp;
  144. ntmp = i; //计算行内偏移
  145. int nstrlen = strline.GetLength();
  146. while(ntmp<nstrlen-1)
  147. {
  148. if(IsDBCSLeadByte(strline[ntmp])) //检查是不是中文字符
  149. ++ntmp;
  150. else
  151. {
  152. if(!isalnum(strline[ntmp]) && strline[ntmp]!='_')
  153. break;
  154. }
  155. ++ntmp;
  156. }
  157. nlnend = ntmp;
  158. CString str = strline.Mid(nlnstart, nlnend-nlnstart);
  159. return str;
  160. }
  161. }
  162. void CIcrEditView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  163. {
  164. CSynEditView::OnKeyDown(nChar, nRepCnt, nFlags);
  165. ShowCursorPosition();
  166. }
  167. void CIcrEditView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
  168. {
  169. CSynEditView::OnKeyUp(nChar, nRepCnt, nFlags);
  170. ShowCursorPosition();
  171. }
  172. void CIcrEditView::OnLButtonDown(UINT nFlags, CPoint point)
  173. {
  174. CSynEditView::OnLButtonDown(nFlags, point);
  175. ShowCursorPosition();
  176. }
  177. void CIcrEditView::OnLButtonUp(UINT nFlags, CPoint point)
  178. {
  179. CSynEditView::OnLButtonUp(nFlags, point);
  180. ShowCursorPosition();
  181. }
  182. void CIcrEditView::OnRButtonDown(UINT nFlags, CPoint point)
  183. {
  184. POINT ptCursor;
  185. ::GetCursorPos(&ptCursor);
  186. CMenu menu;
  187. VERIFY(menu.LoadMenu(IDR_MAINFRAME));
  188. CMenu* pPopup = menu.GetSubMenu(1);
  189. ASSERT(pPopup != NULL);
  190. CWnd* pWndPopupOwner = this;
  191. while (pWndPopupOwner->GetStyle() & WS_CHILD)
  192. pWndPopupOwner = pWndPopupOwner->GetParent();
  193. pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, ptCursor.x, ptCursor.y,
  194. pWndPopupOwner);
  195. CSynEditView::OnRButtonDown(nFlags, point);
  196. }
  197. void CIcrEditView::OnFilePrintPreview()
  198. {
  199. // TODO: Add your command handler code here
  200. CSynEditView::OnFilePrintPreview();
  201. }
  202. /* 语法检查 */
  203. void CIcrEditView::OnCheck()
  204. {
  205. // 预处理脚本
  206. CString scrpt;
  207. GetRichEditCtrl().GetWindowText(scrpt);
  208. scrpt = scrpt + "\nShowResult(\"a\")";
  209. ProcessScript(scrpt);
  210. MyActiveScriptSite g_iActiveScriptSite;
  211. IActiveScript *m_iActiveScript;
  212. IActiveScriptParse *m_iActiveScriptParse;
  213. CScriptObject m_ScriptObject;
  214. // 用引擎进行检查
  215. g_iActiveScriptSite.m_pUnkScriptObject =
  216. m_ScriptObject.GetInterface(&IID_IUnknown);
  217. // Start inproc script engine, VBSCRIPT.DLL
  218. HRVERIFY(CoCreateInstance(CLSID_VBScript, NULL, CLSCTX_INPROC_SERVER,
  219. IID_IActiveScript, (void **)&m_iActiveScript),
  220. "CoCreateInstance() for CLSID_VBScript");
  221. // Get engine's IActiveScriptParse interface.
  222. HRVERIFY(m_iActiveScript->QueryInterface(IID_IActiveScriptParse,
  223. (void **)&m_iActiveScriptParse),
  224. "QueryInterface() for IID_IActiveScriptParse");
  225. // Give engine our IActiveScriptSite interface...
  226. HRVERIFY(m_iActiveScript->SetScriptSite(&g_iActiveScriptSite),
  227. "IActiveScript::SetScriptSite()");
  228. // Give the engine a chance to initialize itself...
  229. HRVERIFY(m_iActiveScriptParse->InitNew(),
  230. "IActiveScriptParse::InitNew()");
  231. // Add a root-level item to the engine's name space...
  232. HRVERIFY(m_iActiveScript->AddNamedItem(L"MyObject",
  233. SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE),
  234. "IActiveScript::AddNamedItem()");
  235. // Parse the code scriptlet...
  236. EXCEPINFO ei;
  237. BSTR pParseText = scrpt.AllocSysString();
  238. m_iActiveScriptParse->ParseScriptText(pParseText, L"MyObject", NULL,
  239. NULL, 0, 0, 0L, NULL, &ei);
  240. /* 启动脚本执行 */
  241. try
  242. {
  243. m_iActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
  244. }
  245. catch(CException* e)
  246. {
  247. delete e;
  248. }
  249. }
  250. /* 选择系统变量 */
  251. void CIcrEditView::OnSelAnalog()
  252. {
  253. CSelectVariantDlg dlg;
  254. dlg.m_nType = 0;
  255. if(dlg.DoModal()==IDOK)
  256. {
  257. CString var = dlg.m_strVar;
  258. HANDLE hGlobalMemory = GlobalAlloc(GHND, var.GetLength()+1); // 分配内存
  259. LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存
  260. memcpy(lpGlobalMemory,var,var.GetLength()+1);
  261. GlobalUnlock(hGlobalMemory); // 锁定内存块解锁
  262. HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄
  263. ::OpenClipboard(hWnd); // 打开剪贴板
  264. ::EmptyClipboard(); // 清空剪贴板
  265. ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板
  266. ::CloseClipboard(); // 关闭剪贴板
  267. GetRichEditCtrl().Paste();
  268. }
  269. }
  270. /* 选择系统函数 */
  271. void CIcrEditView::OnFunction()
  272. {
  273. // TODO: Add your command handler code here
  274. CSystemFunctionList dlg;
  275. if(dlg.DoModal()==IDOK)
  276. {
  277. CString fun = dlg.m_strFunction;
  278. HANDLE hGlobalMemory = GlobalAlloc(GHND, fun.GetLength()+1); // 分配内存
  279. LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存
  280. memcpy(lpGlobalMemory,fun,fun.GetLength()+1);
  281. GlobalUnlock(hGlobalMemory); // 锁定内存块解锁
  282. HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄
  283. ::OpenClipboard(hWnd); // 打开剪贴板
  284. ::EmptyClipboard(); // 清空剪贴板
  285. ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板
  286. ::CloseClipboard(); // 关闭剪贴板
  287. GetRichEditCtrl().Paste();
  288. }
  289. }
  290. void CIcrEditView::OnSelDigital()
  291. {
  292. CSelectVariantDlg dlg;
  293. dlg.m_nType = 1;
  294. if(dlg.DoModal()==IDOK)
  295. {
  296. CString var = dlg.m_strVar;
  297. HANDLE hGlobalMemory = GlobalAlloc(GHND, var.GetLength()+1); // 分配内存
  298. LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存
  299. memcpy(lpGlobalMemory,var,var.GetLength()+1);
  300. GlobalUnlock(hGlobalMemory); // 锁定内存块解锁
  301. HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄
  302. ::OpenClipboard(hWnd); // 打开剪贴板
  303. ::EmptyClipboard(); // 清空剪贴板
  304. ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板
  305. ::CloseClipboard(); // 关闭剪贴板
  306. GetRichEditCtrl().Paste();
  307. }
  308. }