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

📄 twscriptedit.cpp

📁 模拟了tec2000的所有功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998 by Juraj Rojko jrojko@twist.cz
// All rights reserved
//
// TWScriptEdit.cpp : implementation file
//

#include "stdafx.h"
#include "TWScriptEdit.h"
#include "TEC2000.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTWScriptEdit

CTWScriptEdit::CTWScriptEdit()
{

	runflag=true;
	end=0;
//	asmtemp=new Casm;
	inter1_0=false;
	inter2_0=false;
	inter3_0=false;
	inter1_1=false;
	inter2_1=false;
	inter3_1=false;
	inter=false;

	m_chComment = 1;
	m_bCaseSensitive = FALSE; //大小写敏感
	m_bChangeCase = TRUE; //改变大小写

	SetStringQuotes(_T("\""));

//	SetKeywordColor(RGB(0,0,0), TRUE); 
	SetKeywordColor(RGB(0,0,255), FALSE); //设置关键字的颜色值,蓝色
	SetConstantColor(RGB(0,0,0), TRUE); //设置常量的颜色值,黑色
	SetCommentColor(RGB(0,0,0), TRUE);//设置注释的颜色值,绿色
	SetNumberColor(RGB(0,0,0), TRUE);//设置数字的颜色值,黑色
	SetStringColor(RGB(0,0,0), TRUE);//设置字符串的颜色值
	SetOtherColor(RGB(0,0,0), TRUE);
//0x23,0x8e,0x23
	m_bInForcedChange = FALSE; 
	m_changeType = ctUndo; //改变类型初始值设为撤销
	m_crOldSel.cpMin = m_crOldSel.cpMax = 0; //字符的长度范围,最大值和最小值都为0

	interBase=0x2400;
	asmtemp.pEdit=this;
	this->inslength=0;
	strins[0]='\0';

	instemp[0]='\0';
	instemplen=0;


	this->m_hEvent=CreateEvent(NULL,FALSE,TRUE,NULL);

	hTh=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Run,this,0,NULL);
//	hOut=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)OutConsole,this,0,NULL);
//	hTh=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Run,,0,NULL);


//	m_pThread=AfxBeginThread(Run,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
}
void CTWScriptEdit::Run(LPVOID param){
    CTWScriptEdit *pthis=(CTWScriptEdit *)param;
	ResetEvent(pthis->m_hEvent);
	WaitForSingleObject(pthis->m_hEvent,INFINITE);
	while(pthis->runflag){
	//	AfxMessageBox(pthis->strins);
		ResetEvent(pthis->m_hEvent);
	//	MakeLow(pthis->strins,pthis->inslength);
    	pthis->asmtemp.traControl(pthis->strins);
	//	pthis->strins[0]='\0';
	 //  	pthis->inslength=0;
		WaitForSingleObject(pthis->m_hEvent,INFINITE);
	}
//	::AfxMessageBox("hello");
//	::SuspendThread(pthis->hTh);
//	::ResumeThread(pthis->hTh);

}

CTWScriptEdit::~CTWScriptEdit()
{
}

void CTWScriptEdit::Initialize() 
{
	PARAFORMAT pf;  //RichEdit中的段落格式的变量
	pf.cbSize = sizeof(PARAFORMAT);
	pf.dwMask = PFM_TABSTOPS ;  //按TAB键可以停止输入,调到其他控件上去
	pf.cTabCount = MAX_TAB_STOPS;
	for( int itab = 0 ; itab < pf.cTabCount  ; itab++ )
		pf.rgxTabs[itab] = (itab + 1) * 1440/5 ;

	SetParaFormat( pf ); //设置段落的格式

	CHARFORMAT cfDefault; //字体格式的变量
	cfDefault.cbSize = sizeof(cfDefault); //默认格式
	cfDefault.dwEffects = CFE_PROTECTED;  
	cfDefault.dwMask = CFM_BOLD | CFM_FACE | CFM_SIZE | CFM_CHARSET | CFM_PROTECTED;
	cfDefault.yHeight = 200;
	cfDefault.bCharSet = 0xEE; 
	strcpy(cfDefault.szFaceName, _T("Courier New")); 

	SetDefaultCharFormat(cfDefault); //设置字体格式
	
	SetEventMask(ENM_CHANGE | ENM_SELCHANGE | ENM_PROTECTED);

}

void CTWScriptEdit::SetSLComment(TCHAR chComment, TCHAR chComment2)
{
	m_chComment = chComment;
	m_chComment2 = chComment2;
}

void CTWScriptEdit::SetSLComment(LPCTSTR lpszComment)
{
	m_strComment = lpszComment;
}

//增加关键字
void CTWScriptEdit::AddKeywords(LPCTSTR lpszKwd)
{
	m_strKeywords = m_strKeywords + lpszKwd;
	m_strKeywordsLower = m_strKeywords;
	if (!m_bCaseSensitive)
		m_strKeywordsLower.MakeLower();
}

//减少关键字
void CTWScriptEdit::ClearKeywords()
{
	m_strKeywords.Empty();
	m_strKeywordsLower.Empty();
}							  

//增加常量
void CTWScriptEdit::AddConstants(LPCTSTR lpszConst)
{
	m_strConstants = m_strConstants + lpszConst;
	m_strConstantsLower = m_strConstants;
	if (!m_bCaseSensitive)
		m_strConstantsLower.MakeLower();
}
//减少常量
void CTWScriptEdit::ClearConstants()
{
	m_strConstants.Empty();
	m_strConstantsLower.Empty();
}							  

//设置大小写敏感的变量
void CTWScriptEdit::SetCaseSensitive(BOOL bSensitive)
{
	m_bCaseSensitive = bSensitive;
}
//改变大小写
void CTWScriptEdit::SetChangeCase(BOOL bChange)
{
	m_bChangeCase = bChange;
}

void CTWScriptEdit::SetStringQuotes(LPCTSTR lpszStrQ)
{
	m_strStringQuotes = lpszStrQ;
}
//设置关键字的颜色值
void CTWScriptEdit::SetKeywordColor(COLORREF clr, BOOL bBold)
{
	m_icKeyword.clrColor = clr;
	m_icKeyword.bBold = bBold;
}
//设置常量的颜色值
void CTWScriptEdit::SetConstantColor(COLORREF clr, BOOL bBold)
{
	m_icConstant.clrColor = clr;
	m_icConstant.bBold = bBold;
}
void CTWScriptEdit::SetOtherColor(COLORREF clr, BOOL bBold)
{
	m_icOther.clrColor = clr;
	m_icOther.bBold = bBold;  
}
//设置注释的颜色值
void CTWScriptEdit::SetCommentColor(COLORREF clr, BOOL bBold)
{
	m_icComment.clrColor = clr;
	m_icComment.bBold = bBold;
}
//设置数字的颜色值
void CTWScriptEdit::SetNumberColor(COLORREF clr, BOOL bBold)
{
	m_icNumber.clrColor = clr;
	m_icNumber.bBold = bBold;
}
//设置字符串的颜色值
void CTWScriptEdit::SetStringColor(COLORREF clr, BOOL bBold)
{
	m_icString.clrColor = clr;
	m_icString.bBold = bBold;
}

//消息映射
BEGIN_MESSAGE_MAP(CTWScriptEdit, CRichEditCtrl)
	//{{AFX_MSG_MAP(CTWScriptEdit)
	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	ON_WM_GETDLGCODE()
	ON_WM_CHAR()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	//}}AFX_MSG_MAP
	ON_NOTIFY_REFLECT(EN_PROTECTED, OnProtected)
	ON_NOTIFY_REFLECT(EN_SELCHANGE, OnSelChange)
	ON_MESSAGE(WM_SETTEXT, OnSetText)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTWScriptEdit message handlers

UINT CTWScriptEdit::OnGetDlgCode() 
{
	UINT uCode = CRichEditCtrl::OnGetDlgCode();
	
	uCode = DLGC_WANTALLKEYS | DLGC_WANTARROWS | DLGC_WANTCHARS | DLGC_WANTMESSAGE | DLGC_WANTTAB;

	return uCode;
}
//WM_CHAR 的相应函数,限制Backspace的删除作用
void CTWScriptEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{

	
//	::ResumeThread(hTh);
	if(end==1)
		end=0;
	if(hTh==NULL){
	//	AfxMessageBox("dsf");
		runflag=true;
		this->m_hEvent=CreateEvent(NULL,FALSE,TRUE,NULL);
		hTh=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Run,this,0,NULL);
	}


    asmtemp.Ioport80=nChar;
	asmtemp.IoportFlag81|=0x0002;

	curchar=nChar;
//	LockWindowUpdate();
 //HideSelection(TRUE, FALSE);

	if(asmtemp.signG==0)
		OutConsole(this);
	else{
		if(nChar=='\b'){
			int OldlengthAll;
        	OldlengthAll=GetWindowTextLength();
			int templen=instemplen;

			if(instemplen>0)
		    	inslength--;
	    	instemp[instemplen]='\0';
	    	if(templen>0){
		    	SetSel(OldlengthAll-1,OldlengthAll);
	        	ReplaceSel("");
		    	//AfxMessageBox(instemp);
			}

		}
		else{
	    	instemp[instemplen++]=nChar;
		}
	}

//	if(asmtemp.signG==1){

	DWORD nEnd=GetWindowTextLength();
	DWORD nStart=nEnd-inslength;
	FormatTextRange(nStart, nEnd);

	int lines=GetLineCount();
	FormatTextLines(lines, lines);
//	ChangeColor();
//	int OldlengthAll=this->GetWindowTextLength();//没加新字符的总文件长度
//	SetSel(OldlengthAll,OldlengthAll);

//	HideSelection(FALSE, FALSE);
//  UnlockWindowUpdate();
	CRichEditCtrl::OnChar(nChar, nRepCnt, nFlags);
}


void CTWScriptEdit::GetLastLineFromText(char *line){
	int lines=GetLineCount();
	this->GetLine(lines-1,line,500);
}

void CTWScriptEdit::GetFirstCharFromLastLine(char * ch){
	int lines=GetLineCount();
	this->GetLine(lines-1,ch,2);
}


void CTWScriptEdit::GetLastCmdFromText(char * cmd){
	char buffer[500];
	int lines=GetLineCount();
	this->GetLine(lines-1,buffer,200);
	char buffer1[200];
	int len=strlen(buffer);
	int i,j;
	for(i=0,j=1;j<len-2;i++,j++)
		buffer1[i]=buffer[j];
	buffer1[i]='\0'; //buffer1里保存当前行出>外的所有字符
	for(i=0;i<len;i++,j++){
		if(buffer1[i]==';')
			break;
		else
			cmd[i]=buffer1[i];		
	}
	cmd[i]='\0';
}
void CTWScriptEdit::GetLastInsFromText(char * ins){
	char cmd[500];
	GetLastCmdFromText(cmd);
	GetInsFromCmd(ins,cmd);

	int len=strlen(cmd);
	for(int i=0;i<len;i++){
		if(ins[i]>='a'&&ins[i]<='z')
			ins[i]-=32;
	}		
/*	CString temp1;
    temp1.Format("%d",len);
	AfxMessageBox(temp1);
   	AfxMessageBox(cmd);	
	len=strlen(ins);
	temp1.Format("%d",len);
	AfxMessageBox(temp1);
   	AfxMessageBox(ins);	*/
}
void CTWScriptEdit::ShowHelp(){
	CTEC2000App *pApp;
   	pApp=(CTEC2000App *)AfxGetApp();
    CMainFrame *p;
   	p=(CMainFrame *)pApp->GetMainWnd();
  	CWnd *pHelpDlg=p->m_wndViewHelpDlg.GetDlgItem(IDC_DHELP);
   	CEdit * ph=(CEdit *)pHelpDlg;
   	ph->SetWindowText(help.HelpString);
}

int CTWScriptEdit::GetLengthOfLastLine(){
	char buffer[500];
	int lines=GetLineCount();
	this->GetLine(lines-1,buffer,500);
	int len=strlen(buffer);
	return len;
}

void CTWScriptEdit::MakeUp(char *ch,int n){
	for(int i=0;i<n;i++){
		if(ch[i]>='a'&&ch[i]<='z')
			ch[i]=ch[i]-32;
	}

}

void CTWScriptEdit::MakeLow(char *ch,int n){
	for(int i=0;i<n;i++){
		if(ch[i]>='A'&&ch[i]<='Z')
			ch[i]=ch[i]+32;
	}
}


void CTWScriptEdit::ShowRegisterContext(){
	
	CTEC2000App *pApp;
    pApp=(CTEC2000App *)AfxGetApp();
	for(int i=0;i<16;i++){
		pApp->R[i]=asmtemp.R[i];
	}
	for(i=0;i<8;i++){
		pApp->F[i]=asmtemp.F[i];
	}
	CMainFrame *p;
	p=(CMainFrame *)pApp->GetMainWnd();
	CWnd *pRegDlg=p->m_wndViewRegisterDlg.GetDlgItem(IDC_REGISTEREDIT);
	CString temp,temp1;
	CString r;
	r="\r\n";
	for(i=0;i<16;i++){
		temp.Format("%04x",pApp->R[i]);
		temp1.Format("%02d",i);
		r+="R";r+=temp1;r+="=  ";r+=temp;r+="\r\n";
	}
	temp.Format("%d",pApp->F[0]);
	r+="\r\n C =  ";r+=temp;
	temp.Format("%d",pApp->F[1]);
	r+="\r\n Z =  ";r+=temp;

	temp.Format("%d",pApp->F[2]);
	r+="\r\n V =  ";r+=temp;
    temp.Format("%d",pApp->F[3]);
	r+="\r\n S =  ";r+=temp;
	((CEdit*)pRegDlg)->SetWindowText(r);

}
void CTWScriptEdit::OutConsole(LPVOID param)
{
	CTWScriptEdit *pthis=(CTWScriptEdit *)param;
	char ch=pthis->curchar;
	char ins[200];  //用于存放操作码
	int OldlengthAll;
	OldlengthAll=pthis->GetWindowTextLength();
	if(pthis->asmtemp.signE){
		if(ch=='\r'){
			pthis->strins[pthis->inslength++]=ch;
	    	pthis->strins[pthis->inslength]='\0';

		    
		//	if(pthis->asmtemp.signG==1)


			SetEvent(pthis->m_hEvent);
	
			return ;
		}
		if(ch==' '){
			pthis->strins[pthis->inslength++]=ch;
	    	pthis->strins[pthis->inslength]='\0';

		//	if(pthis->asmtemp.signG==1)
            SetEvent(pthis->m_hEvent);
	
			return ;
		}
	}

	if(ch=='\r'){
		if(pthis->inslength>0){

    		pthis->GetInsFromCmd(ins,pthis->strins);

		//	if(pthis->asmtemp.signG==1)
		    SetEvent(pthis->m_hEvent);

		}
		else if(pthis->inslength==0){
		
		//	if(pthis->asmtemp.signG==1)
		    SetEvent(pthis->m_hEvent);
         
		}

		
		pthis->GetInsFromCmd(ins,pthis->strins);
        int lenins=strlen(ins);
		char chtemp[10];
		pthis->GetFirstCharFromLastLine(chtemp);
		//显示动态帮助
		pthis->MakeUp(ins,lenins);
		if(chtemp[0]=='>'){
			pthis->help.MakeCHelp(ins,lenins);
			pthis->ShowHelp();
		}	
		else{
			pthis->help.MakeIHelp(ins,lenins);
	        pthis->ShowHelp();
		}

	}
	else if(ch=='\b'){
		int templen=pthis->inslength;
		if(pthis->inslength>0)
			pthis->inslength--;
		pthis->strins[pthis->inslength]='\0';
		if(templen>0){
			pthis->SetSel(OldlengthAll-1,OldlengthAll);
	    	pthis->ReplaceSel("");
		}
		if(pthis->inslength<=4){
			pthis->GetInsFromCmd(ins,pthis->strins);
            int lenins=strlen(ins);
	    	char chtemp[10];
	    	pthis->GetFirstCharFromLastLine(chtemp);
			//显示动态帮助
			pthis->MakeUp(ins,lenins);
	    	if(chtemp[0]=='>'){
		    	pthis->help.MakeCHelp(ins,lenins);
		    	pthis->ShowHelp();
			}	
	    	else{
		    	pthis->help.MakeIHelp(ins,lenins);
	            pthis->ShowHelp();
			}
		}

	
	}
	else{
		pthis->strins[pthis->inslength++]=ch;

⌨️ 快捷键说明

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