123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- #include "stdafx.h"
- #include "icrEdit.h"
- #include "SystemFunctionList.h"
- #include "public.h"
- #include "icrEditDoc.h"
- #include "icrEditView.h"
- #include "Mainfrm.h"
- #include "SelectVariantDlg.h"
- #include "scriptproc.h"
- #include "scriptobject.h"
- #define unsigned int UWORD
- char * g_CurrentDir;
- extern BOOL ProcessScript(CString& strScript);
- DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4, 0xb0, 0x0,
- 0xaa, 0x0, 0x4a, 0x55, 0xe8);
- // 错误输出
- extern void HRVERIFY(HRESULT hr, char * msg);
- /////////////////////////////////////////////////////////////////////////////
- // CIcrEditView
- IMPLEMENT_DYNCREATE(CIcrEditView, CSynEditView)
- BEGIN_MESSAGE_MAP(CIcrEditView, CSynEditView)
- //{{AFX_MSG_MAP(CIcrEditView)
- ON_WM_KEYDOWN()
- ON_WM_KEYUP()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_RBUTTONDOWN()
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
- ON_COMMAND(ID_CHECK, OnCheck)
- ON_COMMAND(ID_SELANALOG, OnSelAnalog)
- ON_COMMAND(ID_FUNCTION, OnFunction)
- ON_COMMAND(ID_SELDIGITAL, OnSelDigital)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CSynEditView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CSynEditView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CSynEditView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CIcrEditView construction/destruction
- CIcrEditView::CIcrEditView()
- {
- // TODO: add construction code here
- }
- CIcrEditView::~CIcrEditView()
- {
- }
- BOOL CIcrEditView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CSynEditView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CIcrEditView drawing
- void CIcrEditView::OnDraw(CDC* pDC)
- {
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CIcrEditView printing
- BOOL CIcrEditView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CIcrEditView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CIcrEditView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CIcrEditView diagnostics
- #ifdef _DEBUG
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CIcrEditView message handlers
- LRESULT CIcrEditView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: Add your specialized code here and/or call the base class
-
- return CSynEditView::WindowProc(message, wParam, lParam);
- }
- void CIcrEditView::ShowCursorPosition()
- {
- long start, end;
- CRichEditCtrl& richctrl = GetRichEditCtrl();
- richctrl.GetSel(start, end);
- long row = richctrl.LineFromChar(start);
- long column = start - richctrl.LineIndex(row);
- CString strShow;
- strShow.Format(_T("Ln:%d, Col:%d"), row+1, column+1);
- CMainFrame *mainfrm = (CMainFrame *)AfxGetMainWnd();
- CStatusBarCtrl &statusCtrl = mainfrm->m_wndStatusBar.GetStatusBarCtrl();
- statusCtrl.SetText(strShow, 1, 0);
- }
- CString CIcrEditView::GetCursorString()
- {
- long start, end;
- CRichEditCtrl& richctrl = GetRichEditCtrl();
- richctrl.GetSel(start, end);
- long startline = richctrl.LineFromChar(start);
- long endlie = richctrl.LineFromChar(end);
- if(startline == endlie && start != end) //用户选定了文本
- {
- return richctrl.GetSelText();
- }
- else
- {
- int nlnstart, nlnend;
- long line = richctrl.LineFromChar(end);
- int ntmp = end-richctrl.LineIndex(line); //计算行内偏移
-
- CString strline;
- GetLineString(line, strline);
- int i=0, noffset=0;
- while(noffset!=ntmp) //得到光标所在行的字符串中的位置(字符串中中文为两个字符)
- {
- if(IsDBCSLeadByte(strline[i])) ++i;
- ++i; ++noffset;
- }
-
- ntmp = i;
-
- while(ntmp>0)
- {
- --ntmp;
- if(!isalnum(strline[ntmp]) && strline[ntmp]!='_')
- {
- if(ntmp>0)
- {
- if(IsDBCSLeadByte(strline[ntmp-1])) //检查是不是中文字符
- {
- --ntmp;
- }
- else
- break;
- }
- }
- }
- if(!(isalnum(strline[ntmp]) || IsDBCSLeadByte(strline[ntmp])) || strline[ntmp]=='_')
- nlnstart = ntmp+1;
- else
- nlnstart = ntmp;
-
- ntmp = i; //计算行内偏移
-
- int nstrlen = strline.GetLength();
- while(ntmp<nstrlen-1)
- {
- if(IsDBCSLeadByte(strline[ntmp])) //检查是不是中文字符
- ++ntmp;
- else
- {
- if(!isalnum(strline[ntmp]) && strline[ntmp]!='_')
- break;
- }
- ++ntmp;
- }
- nlnend = ntmp;
-
- CString str = strline.Mid(nlnstart, nlnend-nlnstart);
- return str;
- }
- }
- void CIcrEditView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CSynEditView::OnKeyDown(nChar, nRepCnt, nFlags);
- ShowCursorPosition();
- }
- void CIcrEditView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CSynEditView::OnKeyUp(nChar, nRepCnt, nFlags);
- ShowCursorPosition();
- }
- void CIcrEditView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CSynEditView::OnLButtonDown(nFlags, point);
- ShowCursorPosition();
- }
- void CIcrEditView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CSynEditView::OnLButtonUp(nFlags, point);
- ShowCursorPosition();
- }
- void CIcrEditView::OnRButtonDown(UINT nFlags, CPoint point)
- {
- POINT ptCursor;
- ::GetCursorPos(&ptCursor);
-
- CMenu menu;
- VERIFY(menu.LoadMenu(IDR_MAINFRAME));
-
- CMenu* pPopup = menu.GetSubMenu(1);
- ASSERT(pPopup != NULL);
- CWnd* pWndPopupOwner = this;
-
- while (pWndPopupOwner->GetStyle() & WS_CHILD)
- pWndPopupOwner = pWndPopupOwner->GetParent();
-
- pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, ptCursor.x, ptCursor.y,
- pWndPopupOwner);
-
- CSynEditView::OnRButtonDown(nFlags, point);
- }
- void CIcrEditView::OnFilePrintPreview()
- {
- // TODO: Add your command handler code here
- CSynEditView::OnFilePrintPreview();
- }
- /* 语法检查 */
- void CIcrEditView::OnCheck()
- {
- // 预处理脚本
- CString scrpt;
- GetRichEditCtrl().GetWindowText(scrpt);
- scrpt = scrpt + "\nShowResult(\"a\")";
- ProcessScript(scrpt);
- MyActiveScriptSite g_iActiveScriptSite;
- IActiveScript *m_iActiveScript;
- IActiveScriptParse *m_iActiveScriptParse;
- CScriptObject m_ScriptObject;
- // 用引擎进行检查
- g_iActiveScriptSite.m_pUnkScriptObject =
- m_ScriptObject.GetInterface(&IID_IUnknown);
-
- // Start inproc script engine, VBSCRIPT.DLL
- HRVERIFY(CoCreateInstance(CLSID_VBScript, NULL, CLSCTX_INPROC_SERVER,
- IID_IActiveScript, (void **)&m_iActiveScript),
- "CoCreateInstance() for CLSID_VBScript");
-
- // Get engine's IActiveScriptParse interface.
- HRVERIFY(m_iActiveScript->QueryInterface(IID_IActiveScriptParse,
- (void **)&m_iActiveScriptParse),
- "QueryInterface() for IID_IActiveScriptParse");
-
- // Give engine our IActiveScriptSite interface...
- HRVERIFY(m_iActiveScript->SetScriptSite(&g_iActiveScriptSite),
- "IActiveScript::SetScriptSite()");
-
- // Give the engine a chance to initialize itself...
- HRVERIFY(m_iActiveScriptParse->InitNew(),
- "IActiveScriptParse::InitNew()");
-
- // Add a root-level item to the engine's name space...
- HRVERIFY(m_iActiveScript->AddNamedItem(L"MyObject",
- SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE),
- "IActiveScript::AddNamedItem()");
-
- // Parse the code scriptlet...
- EXCEPINFO ei;
- BSTR pParseText = scrpt.AllocSysString();
- m_iActiveScriptParse->ParseScriptText(pParseText, L"MyObject", NULL,
- NULL, 0, 0, 0L, NULL, &ei);
- /* 启动脚本执行 */
- try
- {
- m_iActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
- }
- catch(CException* e)
- {
- delete e;
- }
- }
- /* 选择系统变量 */
- void CIcrEditView::OnSelAnalog()
- {
- CSelectVariantDlg dlg;
- dlg.m_nType = 0;
- if(dlg.DoModal()==IDOK)
- {
- CString var = dlg.m_strVar;
- HANDLE hGlobalMemory = GlobalAlloc(GHND, var.GetLength()+1); // 分配内存
- LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存
- memcpy(lpGlobalMemory,var,var.GetLength()+1);
- GlobalUnlock(hGlobalMemory); // 锁定内存块解锁
- HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄
- ::OpenClipboard(hWnd); // 打开剪贴板
- ::EmptyClipboard(); // 清空剪贴板
- ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板
- ::CloseClipboard(); // 关闭剪贴板
- GetRichEditCtrl().Paste();
- }
- }
- /* 选择系统函数 */
- void CIcrEditView::OnFunction()
- {
- // TODO: Add your command handler code here
- CSystemFunctionList dlg;
- if(dlg.DoModal()==IDOK)
- {
- CString fun = dlg.m_strFunction;
- HANDLE hGlobalMemory = GlobalAlloc(GHND, fun.GetLength()+1); // 分配内存
- LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存
- memcpy(lpGlobalMemory,fun,fun.GetLength()+1);
- GlobalUnlock(hGlobalMemory); // 锁定内存块解锁
- HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄
- ::OpenClipboard(hWnd); // 打开剪贴板
- ::EmptyClipboard(); // 清空剪贴板
- ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板
- ::CloseClipboard(); // 关闭剪贴板
- GetRichEditCtrl().Paste();
- }
- }
- void CIcrEditView::OnSelDigital()
- {
- CSelectVariantDlg dlg;
- dlg.m_nType = 1;
- if(dlg.DoModal()==IDOK)
- {
- CString var = dlg.m_strVar;
- HANDLE hGlobalMemory = GlobalAlloc(GHND, var.GetLength()+1); // 分配内存
- LPBYTE lpGlobalMemory = (LPBYTE)GlobalLock(hGlobalMemory); // 锁定内存
- memcpy(lpGlobalMemory,var,var.GetLength()+1);
- GlobalUnlock(hGlobalMemory); // 锁定内存块解锁
- HWND hWnd = GetSafeHwnd(); // 获取安全窗口句柄
- ::OpenClipboard(hWnd); // 打开剪贴板
- ::EmptyClipboard(); // 清空剪贴板
- ::SetClipboardData(CF_TEXT, hGlobalMemory); // 将内存中的数据放置到剪贴板
- ::CloseClipboard(); // 关闭剪贴板
- GetRichEditCtrl().Paste();
- }
- }
|