vt100imp.cpp

来自「vt100终端仿真程序」· C++ 代码 · 共 1,764 行 · 第 1/3 页

CPP
1,764
字号
#include "stdafx.h"

#include "communicate.h"
#include "comport.h"
#include "telnet.h"
#include "cfgfile.h"
#include "codeconvert.h"

#include "execcomm.h"

#include "prt.h"
#include "keyborad.h"
#include "font.h"
#include "cursor.h"
#include "screen.h"
#include "status.h"
#include "auxport.h"
#include "ComPrn.h"
#include "wbx.h"

#include "Vt100Disp.h"
#include "Bh10.h"
#include "Py.h"
#include "Pr2Comm.h"


#define  ENDLINE    27

int Pr2ToBh10(char * szData);

void ShowSeleStr();
int ShowWbBuf();

//extern int (*CmdFun[])(void);
//*********************define for vt100************************
//comm 
CCommunicate *csTermCom;
CComPort csAuxCom;
extern HWND hOutWnd;
int iTransportMode; //0:direct to host 1:display on screen only 
int iDirect; //0: display 1:send to aux port 2:send to printer
int iAuxCommFlag=0;//0:|为标志;1:命令为标志
int iCommFlag=0;//0:未进入命令接收;1:进入命令接收;
BOOL m_blOnline;
int iTrace;  //0:close trace 1:open trace

//key code
char m_szKeyCode[255][5];

//串口设备类型
int iComDev; // 0:透明方式 1:密码键盘 2:磁条读写器 3:打印机
int bChinese;

//*********************external var****************************
extern char * szCommAry[COMM_NUM];
extern char * szCommAry1[COMM_NUM1];
extern int iStartPrint;

extern int g_nFuncNo;

extern pNode pRoot;		//无参数命令树根节点
extern pNode pRoot1;		//带参数命令树根节点
extern pNode pCurrNode; 
extern int fTree ;//当前搜索树标志

extern char szCurrCommand[128];//当前命令缓冲区

extern int g_iArrPara[50];	//参数数组
extern int ParaNum;		//参数数组下标

extern int fStartPara;		//参数节点标志
					//0:非参数节点
					//1:参数输入中
					//2:分号,等待参数输入

extern struct struKey struKeyMsp;
extern struct struScreen struScrMsp;//屏幕对象结构


//PR2
extern char * szHitachi[PRCOMM_NUM];
extern int g_PR2Para;
pPR2Node pPR2Head;
extern pPR2Node pPr2CurrNode;
char Pr2CurrComm[128];

//prn
extern int PrnType;

//charset
extern int G0,G1;

//window
extern HWND hOutWnd;
RECT TextRect;
TEXTMETRIC tm;

HFONT testFont4,testUlineFont4;
//HFONT AscFont4,AscUlineFont4;

char szAuxData[BUFLEN+1];

//Bh10 Flag
int fPrn;

//******************************
int PrintStr(char * Str1);
int bIM ;		//输入法标志
char GbBuf[10];	//区位码缓冲区
int QuMa,WeiMa;

char PyBuf[10];	//拼音码缓冲区
int CurrPage;//当前拼音显示页
int PageCount;//当前拼音码共有汉字的页数
int PyIndex;//当前拼音码下标

RECT WndRect;

CWbx csWbx;

extern CCodeConvert csCodeConv;


extern int iWidth;
extern int bOKI;
extern int gPr2Para;

//**********************end define*****************************

int GetKeyCode()
{
	int iRet,iNum;
	FILE *hKeyFile;

	if((hKeyFile=fopen("\\disk\\msp.kbd","rb"))==NULL){
		return 1;
	}

	memset(m_szKeyCode,0,sizeof(m_szKeyCode));
	iNum=0;
	while(1){
		iRet=fread(&m_szKeyCode[iNum][0],1,5,hKeyFile);
		if(feof(hKeyFile)){
			fclose(hKeyFile);
			break;
		}
		iNum++;
	}
	if(iNum<255)
		return 1;

	return 0;
}

void CreateCmdTree()
{
	//////////////////////////////////////////////////////////
	//建立无参数命令树
	pRoot=(pNode)malloc(sizeof(Node));
	pRoot->pLeft =NULL;
	pRoot->pRight=NULL;
	pRoot->FuncNo =0;
	pRoot->Value =0;
	pRoot->Type=0;

	CreateTree(pRoot);

	//建立带参数命令树
	pRoot1=(pNode)malloc(sizeof(Node));
	pRoot1->pLeft =NULL;
	pRoot1->pRight=NULL;
	pRoot1->FuncNo =0;
	pRoot1->Value =0;
	pRoot1->Type=0;
	CreateTree1(pRoot1);

	pCurrNode=pRoot;

//	CmdFun[105]();
	//PR2 Tree

	pPR2Head=(pPR2Node)malloc(sizeof(PR2Node));
	pPR2Head->CommNo =0;
	pPR2Head->pLeft =NULL;
	pPR2Head->pRight =NULL;
	pPR2Head->Type =0;
	pPR2Head->Value =0;

	CreatePR2Tree(pPR2Head);
	pPr2CurrNode=pPR2Head;

}

void OnDraw(){
	HDC hDC;
	POINT arrPoint[4];



//	MessageBox(NULL,"aa","bb",MB_OK);
	if(m_blOnline){
		SetCursorHide();

		Refresh(0,0,23,iWidth-1);
		hDC=GetDC(hOutWnd);
		SetROP2(hDC,R2_WHITE);	//显示方式

		GetClientRect(hOutWnd,&WndRect);
		arrPoint[0].x=WndRect.left;
		arrPoint[0].y=struScrMsp.iChrHigh*ENDLINE-5;
		arrPoint[1].x=WndRect.right;
		arrPoint[1].y=struScrMsp.iChrHigh*ENDLINE-5;
		Polyline(hDC,arrPoint,2);
		ReleaseDC(hOutWnd,hDC);

		if (bIM==0)
		{
			hDC=GetDC(hOutWnd);

			SetROP2(hDC,R2_WHITE);	//显示方式

			GetClientRect(hOutWnd,&WndRect);

			SelectObject(hDC,testFont4);
			SetBkColor(hDC,RGB(0,0,0));
			SetTextColor(hDC,RGB(208,208,208));///设置字体颜色
			SetBkMode(hDC,OPAQUE);

			ExtTextOut(hDC,10,struScrMsp.iChrHigh*ENDLINE,ETO_CLIPPED,&TextRect,_T("                                                                                          "),80,NULL);
			ExtTextOut(hDC,10,struScrMsp.iChrHigh*ENDLINE,ETO_CLIPPED,&TextRect,_T("英文           美斯比科技有限公司 VT100 仿真系统"),33,NULL);
			ReleaseDC(hOutWnd,hDC);

		}
		else
		{
			hDC=GetDC(hOutWnd);
			SetROP2(hDC,R2_WHITE);	//显示方式

			GetClientRect(hOutWnd,&WndRect);

			arrPoint[0].x=WndRect.left;
			arrPoint[0].y=struScrMsp.iChrHigh*ENDLINE-5;
			arrPoint[1].x=WndRect.right;
			arrPoint[1].y=struScrMsp.iChrHigh*ENDLINE-5;
			Polyline(hDC,arrPoint,2);
			ReleaseDC(hOutWnd,hDC);

			switch(bIM){
			case 1:
				ShowSeleStr();
				break;

			case 2:
				ShowPyStr();
				break;

			case 3:
				ShowWbBuf();
				break;
			}
		}
		SetCursorDisplay();
	}

}

void Online()
{
	// TODO: Add your command handler code here
	int iRetCode;
	char szTemp[50];
	int i;
	int nWidth;
	RECT WndRect;
	CCfgFile csCfgFile;

	POINT arrPoint[4];

	iDirect=0;
	iTransportMode=0;

	GetClientRect(hOutWnd,&TextRect);

	//get keycode
	if(GetKeyCode()>0){
		MessageBox(hOutWnd,_T("Can't open file msp.kbd"),_T("Error"),MB_OK);
		return ;
	}
	
	//double click online button
	if(m_blOnline==TRUE){
		csTermCom->ResetCom();
		delete csTermCom;
		csTermCom=NULL;

		CloseAuxPort();
	}
	
	if(csTermCom==NULL){
		//get connect type
		iRetCode=csCfgFile.GetCfgItemInt(CONNECTTYPE);
		if(iRetCode==0)
			csTermCom=new CComPort;
		else
			csTermCom=new CTelnet;
	}

	csTermCom->SetMsgWnd(hOutWnd); 
	iRetCode=csTermCom->InitCom();
	if(iRetCode!=0){
		sprintf(szTemp,"Initialise failed! Error Code= %d",iRetCode);
#ifdef UNICODE
		TCHAR wcTemp[50];
	
		mbstowcs(wcTemp,szTemp,sizeof(wcTemp));
		MessageBox(hOutWnd,wcTemp,_T("Error"),MB_OK);
#else
		MessageBox(hOutWnd,szTemp,_T("Error"),MB_OK);
#endif

		return;
	}
	InitKeyborad();
	InitScreen();
	m_blOnline=TRUE;
	
	//取列宽
	iWidth=csCfgFile.GetCfgItemInt(WIDTH);
	
	iTrace=csCfgFile.GetCfgItemInt(TRACE); 

	HDC hDC;
//	HFONT testFont4;
	
	hDC=GetDC(hOutWnd);
//	SetROP2(hDC,R2_NOT);	//显示方式
	SetROP2(hDC,R2_WHITE);	//显示方式
 
//	testFont4=CreateFont(22, 11, 0,0, 400, FALSE, FALSE, 0, 
//	ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 
//	DEFAULT_QUALITY, DEFAULT_PITCH|FF_MODERN, _T("line printer"));//创建等宽字体

//	nWidth=GetDeviceCaps(hDC,HORZRES);
	GetClientRect(hOutWnd,&WndRect);
	nWidth=WndRect.right-WndRect.left;


			if (nWidth<700)
			{
				if (iWidth==80)
				{
					struScrMsp.iChrWidth=9;//nWidth/80;	
				}
				else
				{
					struScrMsp.iChrWidth=4;//nWidth/132;	
				}
				struScrMsp.iChrHigh=18;//struScrMsp.iChrWidth*2;
			}
			else
			{
				if (nWidth < 1000)
				{
					if (iWidth==80)
					{
						struScrMsp.iChrWidth=10;//nWidth/80;	
					}
					else
					{
						struScrMsp.iChrWidth=6;//nWidth/132;	
					}
					struScrMsp.iChrHigh=20;//struScrMsp.iChrWidth*2;
				}
				else
				{
					if (iWidth==80)
					{
						struScrMsp.iChrWidth=11;//nWidth/80;	
					}
					else
					{
						struScrMsp.iChrWidth=7;//nWidth/132;	
					}
					struScrMsp.iChrHigh=22;//struScrMsp.iChrWidth*2;
				}
			}

	LOGFONT hNormal,hUnderLine;

	hNormal.lfHeight=struScrMsp.iChrHigh;
	hNormal.lfWidth=struScrMsp.iChrWidth;
	hNormal.lfEscapement=0;
	hNormal.lfOrientation=0;
	hNormal.lfWeight=400;
	hNormal.lfItalic=FALSE;
	hNormal.lfUnderline=FALSE;
	hNormal.lfStrikeOut=0;
	hNormal.lfCharSet=DEFAULT_CHARSET ;//GB2312_CHARSET;
	hNormal.lfOutPrecision= OUT_DEFAULT_PRECIS;
	hNormal.lfClipPrecision= CLIP_DEFAULT_PRECIS;
	hNormal.lfQuality= DEFAULT_QUALITY;
	hNormal.lfPitchAndFamily =DEFAULT_PITCH|FF_MODERN;
#ifdef UNICODE	
	wcscpy(hNormal.lfFaceName,_T("COUR"));
#else
	strcpy(hNormal.lfFaceName,"COUR");
#endif
	testFont4=CreateFontIndirect(&hNormal);

//	hNormal.lfCharSet=ANSI_CHARSET;
//	AscFont4=CreateFontIndirect(&hNormal);


	hUnderLine.lfHeight=struScrMsp.iChrHigh;
	hUnderLine.lfWidth=struScrMsp.iChrWidth;
	hUnderLine.lfEscapement=0;
	hUnderLine.lfOrientation=0;
	hUnderLine.lfWeight=400;
	hUnderLine.lfItalic=FALSE;
	hUnderLine.lfUnderline=TRUE;
	hUnderLine.lfStrikeOut=0;
	hUnderLine.lfCharSet=DEFAULT_CHARSET ;//GB2312_CHARSET;
	hUnderLine.lfOutPrecision= OUT_DEFAULT_PRECIS;
	hUnderLine.lfClipPrecision= CLIP_DEFAULT_PRECIS;
	hUnderLine.lfQuality= DEFAULT_QUALITY;
	hUnderLine.lfPitchAndFamily =DEFAULT_PITCH|FF_MODERN;
#ifdef UNICODE
	wcscpy(hUnderLine.lfFaceName, _T("COUR"));
#else
	strcpy(hUnderLine.lfFaceName, "COUR");
#endif
	
	testUlineFont4=CreateFontIndirect(&hUnderLine);

//	hUnderLine.lfCharSet=ANSI_CHARSET;
//	AscUlineFont4=CreateFontIndirect(&hUnderLine);

	/*	testFont4=CreateFont(struScrMsp.iChrHigh,struScrMsp.iChrWidth, 0,0, 400, FALSE, FALSE, 0, 
	ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 
	DEFAULT_QUALITY, DEFAULT_PITCH|FF_MODERN, _T("line printer"));//创建等宽字体

	testUlineFont4=CreateFont(struScrMsp.iChrHigh,struScrMsp.iChrWidth, 0,0, 400, TRUE, TRUE, 0, 
	ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 
	DEFAULT_QUALITY, DEFAULT_PITCH|FF_MODERN, _T("line printer"));//创建等宽下划线字体
*/
	
/*
	if (nWidth<1000)
	{
		testFont4=CreateFont(16,8, 0,0, 400, FALSE, FALSE, 0, 
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 
		DEFAULT_QUALITY, DEFAULT_PITCH|FF_MODERN, _T("line printer"));//创建等宽字体

		testUlineFont4=CreateFont(16,8, 0,0, 400, TRUE, TRUE, 0, 
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 
		DEFAULT_QUALITY, DEFAULT_PITCH|FF_MODERN, _T("line printer"));//创建等宽下划线字体
		struScrMsp.iChrWidth=8;//tm.tmMaxCharWidth-1;///初始的字体宽度定为标准字宽的最大宽度
		struScrMsp.iChrHigh=16;//tm.tmHeight;//取字高
	}
	else
	{
		testFont4=CreateFont(20, 10, 0,0, 400, FALSE, FALSE, 0, 
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 
		DEFAULT_QUALITY, DEFAULT_PITCH|FF_MODERN, _T("line printer"));//创建等宽字体

		testUlineFont4=CreateFont(20, 10, 0,0, 400, FALSE, TRUE, 0, 
		ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 
		DEFAULT_QUALITY, DEFAULT_PITCH|FF_MODERN, _T("line printer"));//创建等宽下划线字体
		struScrMsp.iChrWidth=10;//tm.tmMaxCharWidth-1;///初始的字体宽度定为标准字宽的最大宽度
		struScrMsp.iChrHigh=20;//tm.tmHeight;//取字高
	}
*/
    SelectObject(hDC,testFont4);
    GetTextMetrics(hDC,&tm);	//取字体属性
//	m_iChrWidth=tm.tmMaxCharWidth;///初始的字体宽度定为标准字宽的最大宽度
//	m_iChrHigh=tm.tmHeight;//取字高

	SetBkColor(hDC,RGB(0,0,0));
//	pDC->UpdateColors();
    SetTextColor(hDC,RGB(208,208,208));///设置字体颜色
	SetBkMode(hDC,OPAQUE);

	struScrMsp.iXPos =0;
	struScrMsp.iYPos =0;

    ///初始时显示线状光标
	CreateCaret(hOutWnd,NULL,struScrMsp.iChrWidth,1);//创建光标
	SetCaretPos(struScrMsp.iXPos,struScrMsp.iYPos +struScrMsp.iChrHigh);//设置光标位置
	ShowCaret(hOutWnd);//显示光标

	//下添清显示缓冲区、设滚动区域起始指针、滚动区域开始结束行、当前显示行
	ClearBuf(0,0,23,131);
	Refresh(0,0,23,131);
	struScrMsp.iXPos =0;
	struScrMsp.iYPos =0;
	struScrMsp.iBaseRow=0;
	struScrMsp.iTopRow =0;
	struScrMsp.iButtomRow =23;	

	for (i=0; i<iWidth; i+=8)
		struScrMsp.iArrTab [i]=1;
	struScrMsp.iArrTab [iWidth-1]=1;

	bIM=0;
	arrPoint[0].x=WndRect.left;
	arrPoint[0].y=struScrMsp.iChrHigh*ENDLINE-5;
	arrPoint[1].x=WndRect.right;
	arrPoint[1].y=struScrMsp.iChrHigh*ENDLINE-5;
	Polyline(hDC,arrPoint,2);

	ExtTextOut(hDC,10,struScrMsp.iChrHigh*ENDLINE,ETO_CLIPPED,&TextRect,_T("英文           美斯比科技有限公司 VT100 仿真系统"),33,NULL);

	G0=0;
	G1=2;

	ReleaseDC(hOutWnd,hDC);
}

void Offline()
{
	// TODO: Add your command handler code here
	int iRetCode;
	char szTemp[50];

	if(m_blOnline==FALSE)
		return ;

	m_blOnline=FALSE;
	iRetCode=csTermCom->ResetCom();
	if(iRetCode!=0){
		sprintf(szTemp,"Reset failed! Error Code= %d",iRetCode);
#ifdef UNICODE
		TCHAR wcTemp[50];
	
		mbstowcs(wcTemp,szTemp,sizeof(wcTemp));
		MessageBox(hOutWnd,wcTemp,_T("Error"),MB_OK);
#else
		MessageBox(hOutWnd,szTemp,_T("Error"),MB_OK);
#endif
		return;
	}

	if(csTermCom!=NULL){
		delete (CTelnet *)csTermCom;
		csTermCom=NULL;
	}

	CloseAuxPort();

	//for test
	//restore font
	//??
	HideCaret(hOutWnd);
	struScrMsp.iXPos= 0;
	struScrMsp.iYPos =0;

}

void OnReleaseWnd()
{
	if(m_blOnline){
		csTermCom->ResetCom();
		csAuxCom.ResetCom(); 
	}
	
	if(csTermCom!=NULL){
		delete csTermCom;
		csTermCom=NULL;
	}

	PostQuitMessage(0);
}

void OnCreateWnd()
{
	CreateCmdTree();
			
	//init parameter
	m_blOnline=FALSE; //initialise Comm parameter 
	csTermCom=NULL;
}

void OnKeyDown(WORD wParam,DWORD lParam)
{
	/*
	char szKeyBuf[6];
	int iOffset[]={237,238,239,240};

⌨️ 快捷键说明

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