⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmmgview.cpp

📁 一个类c语言的解释器
💻 CPP
字号:
// cmmgView.cpp : CcmmgView 类的实现
//

#include "stdafx.h"
#include "cmmg.h"

#include "cmmgDoc.h"
#include "cmmgView.h"
#include "util.h"
#include "MyRichCtrl.h"
#include <vector>
using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CcmmgView

IMPLEMENT_DYNCREATE(CcmmgView, CView)

BEGIN_MESSAGE_MAP(CcmmgView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_COMMAND(ID_NEW,NewFile)
	ON_COMMAND(ID_EDIT_COPY,copy)
	ON_COMMAND(ID_OPEN,OpenFile)
	ON_COMMAND(ID_FILE_SAVE,SaveFile)
	ON_COMMAND(ID_FILE_SAVE_AS,SaveFileAs)
	ON_COMMAND(ID_EDIT_PASTE,paste)
	ON_COMMAND(ID_EDIT_CUT,cut)
	ON_COMMAND(ID_EDIT_UNDO,undo)
	ON_COMMAND(4103,closeTab)

	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY,OnUpDateEditCopyUI)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE,OnUpDateEditPasteUI)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CUT,OnUpDateEditCutUI)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO,OnUpDateEditUndoUI)

	ON_UPDATE_COMMAND_UI(ID_PRO_SCAN,OnUpDateProScanUI)
	ON_UPDATE_COMMAND_UI(ID_PRO_PARSE,OnUpDateProParseUI)
	ON_UPDATE_COMMAND_UI(ID_PRO_EXEC,OnUpDateProExecUI)

	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE,OnUpDateFileSaveUI)
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS,OnUpDateFileSaveAsUI)

	ON_NOTIFY(TCN_SELCHANGING,4002,OnSelChanging)
	ON_NOTIFY(TCN_SELCHANGE,4002,OnSelChange)

	ON_NOTIFY_RANGE(NM_RCLICK,5000,5100,EditRClick)

	ON_WM_SIZE()
	ON_WM_SIZING()

	ON_MESSAGE(10000,closeTab)
END_MESSAGE_MAP()

// CcmmgView 构造/析构

CcmmgView::CcmmgView()
{
	// TODO: 在此处添加构造代码

}

CcmmgView::~CcmmgView()
{
}

BOOL CcmmgView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CcmmgView 绘制

void CcmmgView::OnDraw(CDC* /*pDC*/)
{
	CcmmgDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
}


// CcmmgView 打印

BOOL CcmmgView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CcmmgView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CcmmgView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清除过程
}


// CcmmgView 诊断

#ifdef _DEBUG
void CcmmgView::AssertValid() const
{
	CView::AssertValid();
}

void CcmmgView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CcmmgDoc* CcmmgView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CcmmgDoc)));
	return (CcmmgDoc*)m_pDocument;
}
#endif //_DEBUG

void CcmmgView::OnInitialUpdate(){
	GetDocument()->main = ((CMainFrame*)AfxGetMainWnd())->m_wndSplitter2.GetPane(0,1);
}
// CcmmgView 消息处理程序

VOID CcmmgView::NewFile(){
	CFileDialog file(TRUE,0,0,4|2,TEXT("所有文件(*.*)|*.*|源文件(*.cmm)|*.cmm||"));
	if (file.DoModal()==IDOK)
	{
		GetDocument()->main = ((CMainFrame*)AfxGetMainWnd())->m_wndSplitter2.GetPane(0,1);
		if (GetDocument()->paths.size()==0)
		{
			RECT rect;
			::GetClientRect(GetDocument()->main->m_hWnd,&rect);
			GetDocument()->editTab.Create(WS_CHILD|WS_VISIBLE|TCS_FIXEDWIDTH|TCS_TABS ,rect,GetDocument()->main,4002);
			GetDocument()->editTab.AdjustRect(FALSE,&rect);
			GetDocument()->editTab.MoveWindow(rect.left-5,rect.top-5,rect.right+10,rect.bottom+10);
			GetDocument()->activeIndex=-1;
			btnClose.Create(TEXT("X"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect,GetDocument()->main,4103);
			btnClose.SetWindowPos(NULL,rect.right-25,0,23,23,SWP_SHOWWINDOW);
		}
		GetDocument()->activeIndex++;
		CString path(file.GetPathName()) ;
		CFile *pfile = new CFile(path,CFile::modeCreate);
		pfile->Close();
		CMyRichCtrl* cre = new CMyRichCtrl;
		RECT rect;
		GetDocument()->editTab.GetClientRect(&rect);
		cre->Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|ES_MULTILINE,rect,&GetDocument()->editTab,5000+GetDocument()->inc++);
		cre->SetWindowPos(NULL,rect.left+5,rect.top+30,rect.right-rect.left-10,rect.bottom-rect.top-35,SWP_SHOWWINDOW);
		cre->ShowWindow(SW_SHOW);
		long event = cre->GetEventMask();
		event |=ENM_MOUSEEVENTS;
		cre->SetEventMask(event);
		TCITEM tc;
		tc.mask=TCIF_TEXT;
		CString cstr = file.GetFileName();
		tc.pszText=cstr.GetBuffer();
		cstr.ReleaseBuffer();
		GetDocument()->editTab.InsertItem(0,&tc);
		//GetDocument()->pedits.push_back(cre);
		GetDocument()->paths.push_back(path);
		GetDocument()->hedits.push_back(cre->m_hWnd);
		open(path,GetDocument()->hedits.back());
		GetDocument()->tree->InsertItem(file.GetFileName(),GetDocument()->source,TVI_LAST);
		OnSelChange(NULL,NULL);
		OnSelChanging(NULL,NULL);
	}
}



VOID CcmmgView::OpenFile(){
	CFileDialog file(TRUE,0,0,4|2,TEXT("所有文件(*.*)|*.*|源文件(*.cmm)|*.cmm||"));
	if (file.DoModal()==IDOK)
	{
		GetDocument()->main = ((CMainFrame*)AfxGetMainWnd())->m_wndSplitter2.GetPane(0,1);
		RECT rect;
		if (GetDocument()->hedits.size()<1)
		{			
			::GetClientRect(GetDocument()->main->m_hWnd,&rect);
			GetDocument()->editTab.Create(WS_CHILD|WS_VISIBLE|TCS_FIXEDWIDTH|TCS_TABS ,rect,GetDocument()->main,4002);
			GetDocument()->editTab.AdjustRect(FALSE,&rect);
			GetDocument()->editTab.MoveWindow(rect.left-5,rect.top-5,rect.right+10,rect.bottom+10);
			btnClose.Create(TEXT("X"),WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect,GetDocument()->main,4103);
			btnClose.SetWindowPos(NULL,rect.right-25,0,23,23,SWP_SHOWWINDOW);
		}
		GetDocument()->activeIndex++;
		CString path(file.GetPathName()) ;
		CMyRichCtrl* cre = new CMyRichCtrl;
		cre->Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|ES_MULTILINE,rect,GetDocument()->main,5000+GetDocument()->inc++);
		TCITEM tc;
		tc.mask=TCIF_TEXT;
		CString cstr = file.GetFileName();
		tc.pszText=cstr.GetBuffer();
		cstr.ReleaseBuffer();
		GetDocument()->editTab.InsertItem(0,&tc);
		//GetDocument()->pedits.push_back(cre);
		GetDocument()->paths.push_back(path);
		GetDocument()->hedits.push_back(cre->m_hWnd);
		open(path,GetDocument()->hedits.back());
		GetDocument()->editTab.SetCurSel(0);
		GetDocument()->editTab.GetClientRect(&rect);
		HWND h = GetDocument()->hedits.back();
		::MoveWindow(GetDocument()->hedits.back(),rect.left+5,rect.top+30,rect.right-rect.left-10,rect.bottom-rect.top-35,TRUE);
		::ShowWindow(GetDocument()->hedits.back(),SW_SHOW);
		GetDocument()->tree->InsertItem(file.GetFileName(),GetDocument()->source,TVI_LAST);
		OnSelChange(NULL,NULL);
		OnSelChanging(NULL,NULL);
	}
}

VOID CcmmgView::SaveFile(){
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	save(GetDocument()->paths.at(k),GetDocument()->hedits.at(k));
}

VOID CcmmgView::SaveFileAs(){
	CFileDialog file(TRUE,0,0,4|2,TEXT("所有文件(*.*)|*.*|源文件(*.cmm)|*.cmm||"));
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	if (file.DoModal()==IDOK)
	{
		save(file.GetPathName(),GetDocument()->hedits.at(k));
	}
}

void CcmmgView::copy(){
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	if (k<0)
	{
		return;
	}
	::SendMessage(GetDocument()->hedits[k],WM_COPY,(LPARAM)0,(WPARAM)0);
}

void CcmmgView::paste(){
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	if (k<0)
	{
		return;
	}
	::SendMessage(GetDocument()->hedits[k],WM_PASTE,(LPARAM)0,(WPARAM)0);
}

void CcmmgView::cut(){
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	if (k<0)
	{
		return;
	}
	::SendMessage(GetDocument()->hedits[k],WM_CUT,(LPARAM)0,(WPARAM)0);
}

void CcmmgView::undo(){
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	if (k<0)
	{
		return;
	}
	::SendMessage(GetDocument()->hedits[k],WM_UNDO,(LPARAM)0,(WPARAM)0);
}

VOID CcmmgView::show(){
	if (GetDocument()->hedits.size()<1)
	{
		return;
	}
	
	RECT rect;
	GetDocument()->editTab.GetClientRect(&rect);
	HWND h = GetDocument()->hedits.back();
	::MoveWindow(GetDocument()->hedits.back(),rect.left+5,rect.top+30,rect.right-rect.left-10,rect.bottom-rect.top-35,TRUE);
	::ShowWindow(GetDocument()->hedits.back(),SW_SHOW);
}

void CcmmgView::OnUpDateEditCopyUI(CCmdUI *pCmdUI){
	pCmdUI->Enable(GetDocument()->hedits.size()>0);
}
void CcmmgView::OnUpDateEditCutUI(CCmdUI *pCmdUI){
	pCmdUI->Enable(GetDocument()->hedits.size()>0);
}
void CcmmgView::OnUpDateEditPasteUI(CCmdUI *pCmdUI){
	pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT)&&GetDocument()->hedits.size()>0);
}
void CcmmgView::OnUpDateEditUndoUI(CCmdUI *pCmdUI){
	pCmdUI->Enable(GetDocument()->hedits.size()>0);
}
void CcmmgView::OnUpDateProScanUI(CCmdUI* pCmdUI){
	pCmdUI->Enable(GetDocument()->hedits.size()>0);
}
void CcmmgView::OnUpDateProParseUI(CCmdUI* pCmdUI){
	pCmdUI->Enable(GetDocument()->hedits.size()>0);
}
void CcmmgView::OnUpDateProExecUI(CCmdUI* pCmdUI){
	pCmdUI->Enable(GetDocument()->hedits.size()>0);
}

void CcmmgView::OnUpDateFileSaveUI(CCmdUI *pCmdUI){
	pCmdUI->Enable(GetDocument()->hedits.size()>0);
}

void CcmmgView::OnUpDateFileSaveAsUI(CCmdUI *pCmdUI){
	pCmdUI->Enable(GetDocument()->hedits.size()>0);
}

VOID CcmmgView::OnSelChanging(NMHDR * pNotifyStruct, LRESULT * result){
	
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	::ShowWindow(GetDocument()->hedits.at(k),SW_HIDE);
}

VOID CcmmgView::OnSelChange(NMHDR * pNotifyStruct, LRESULT * result){
	vector<HWND>::iterator it;
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	HWND h = GetDocument()->hedits.at(k);
	RECT rect;
	GetDocument()->editTab.GetClientRect(&rect);
	::MoveWindow(GetDocument()->hedits.at(k),rect.left+5,rect.top+30,rect.right-rect.left-10,rect.bottom-rect.top-35,TRUE);
	::ShowWindow(GetDocument()->hedits.at(k),SW_SHOW);
}

VOID CcmmgView::closeTab(){
	vector<HWND>::iterator hit;
	vector<CString>::iterator cit;
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	int k1 = GetDocument()->editTab.GetCurSel();
	int i=0;
	for(hit=GetDocument()->hedits.begin(),cit=GetDocument()->paths.begin();i<k;i++,hit++,cit++);
	::ShowWindow(GetDocument()->hedits.at(k),SW_HIDE);
	::DestroyWindow(GetDocument()->hedits.at(k));
	GetDocument()->hedits.erase(hit);
	GetDocument()->paths.erase(cit);
	GetDocument()->editTab.DeleteItem(GetDocument()->editTab.GetCurSel());
	if (GetDocument()->hedits.size()==0)
	{
		::DestroyWindow(GetDocument()->editTab.m_hWnd);
		::DestroyWindow(btnClose.m_hWnd);
	}
	else{
		
		if (k1==0)
		{
			RECT rect;
			GetDocument()->main->GetClientRect(&rect);
			GetDocument()->editTab.SetCurSel(0);
			::MoveWindow(GetDocument()->hedits.at(k-1),rect.left+5,rect.top+30,rect.right-rect.left-10,rect.bottom-rect.top-35,TRUE);
			::ShowWindow(GetDocument()->hedits.at(k-1),SW_SHOW);
		}
		else{
			RECT rect;
			GetDocument()->main->GetClientRect(&rect);
			GetDocument()->editTab.SetCurSel(k1-1);
			::MoveWindow(GetDocument()->hedits.at(k),rect.left+5,rect.top+30,rect.right-rect.left-10,rect.bottom-rect.top-35,TRUE);
			::ShowWindow(GetDocument()->hedits.at(k),SW_SHOW);
		}
	}
	HTREEITEM hChildItem = GetDocument()->tree->GetChildItem(GetDocument()->source);
	i=0;
	while (hChildItem != NULL)
	{
		if(i==k){
			GetDocument()->tree->DeleteItem(hChildItem);
			break;
		}
		hChildItem = GetDocument()->tree->GetNextItem(hChildItem, TVGN_NEXT);
		i++;
	}
}

void CcmmgView::EditRClick(UINT id, NMHDR *pNotifyStruct, LRESULT *result){
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	POINT p;
	::GetCursorPos(&p);
	::ScreenToClient(GetDocument()->hedits.at(k),&p);
	CMenu hMenu;
	hMenu.CreatePopupMenu();
	hMenu.AppendMenu(MF_STRING,ID_EDIT_UNDO,TEXT("撤销"));
	hMenu.AppendMenu(MF_SEPARATOR);
	hMenu.AppendMenu(MF_STRING,ID_EDIT_COPY,TEXT("复制"));
	hMenu.AppendMenu(MF_STRING,ID_EDIT_CUT,TEXT("剪切"));
	hMenu.AppendMenu(MF_STRING,ID_EDIT_PASTE,TEXT("粘贴"));
	hMenu.TrackPopupMenu(TPM_LEFTALIGN,p.x,p.y,AfxGetMainWnd());
}

LRESULT CcmmgView::closeTab(WPARAM wParam, LPARAM lParam){
	int k = GetDocument()->hedits.size() - GetDocument()->editTab.GetCurSel()-1;
	GetDocument()->editTab.SetCurSel((int)wParam);
	closeTab();
	return NULL;
}

void CcmmgView::OnSize(UINT nType, int cx, int cy){
	CView::OnSize(nType,cx,cy);
	if (GetDocument()->editTab.m_hWnd)
	{
		GetDocument()->editTab.SetWindowPos(NULL,0,0,cx,cy,SWP_SHOWWINDOW);
		int k = GetDocument()->editTab.GetCurSel();
		RECT rect;
		GetDocument()->editTab.GetClientRect(&rect);
		::MoveWindow(GetDocument()->hedits.at(k),rect.left+5,rect.top+30,rect.right-rect.left-10,rect.bottom-rect.top-35,TRUE);
		::ShowWindow(GetDocument()->hedits.at(k),SW_SHOW);
		btnClose.SetWindowPos(NULL,rect.right-25,0,23,23,SWP_SHOWWINDOW);
	}
}

void CcmmgView::OnSizing(UINT nSide, LPRECT lpRect){
	if (lpRect->right-lpRect->left<200||lpRect->bottom-lpRect->top<200)
	{
		return;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -