errorview.cpp

来自「词法分析程序」· C++ 代码 · 共 304 行

CPP
304
字号
// ErrorView.cpp : implementation file
//

#include "stdafx.h"
#include "SmartC.h"
#include "ErrorView.h"
#include "SmartCDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CErrorView

IMPLEMENT_DYNCREATE(CErrorView, CView)

CErrorView::CErrorView()
{
	m_nLineNum=0;
	m_nLineHeight=20;
	m_nStartLine=0;
	m_nCharNum=0;
	m_nCharWidth=8;
	m_nStartChar=0;
	m_nSelectedLine=-1;
}

CErrorView::~CErrorView()
{
}


BEGIN_MESSAGE_MAP(CErrorView, CView)
	//{{AFX_MSG_MAP(CErrorView)
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()
	ON_WM_SIZE()
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CErrorView drawing

void CErrorView::OnDraw(CDC* pDC)
{
	// TODO: add draw code here
	CRect rect;
	GetClientRect(&rect);
	CSmartCDoc* pDoc =(class CSmartCDoc*) GetDocument();
	ASSERT_VALID(pDoc);
	int y=5;
	int i;
	Node* temp;//要输出信息的链表
	char* pstr;//当前要输出的一行信息指针
	int charnum;//当前要输出的一行信息的字符数目
	temp=pDoc->errorfront.next;
	m_nLineNum=pDoc->error_linenum;
	m_nCharNum=pDoc->error_charnum;
	for(i=0;i<m_nStartLine&&temp->next;i++)
		temp=temp->next;
	i=0;
	while(temp&&y<(rect.bottom-m_nLineHeight))
	{
		//确定开始输出的字符
		pstr=temp->pchar;
		if((unsigned)m_nStartChar>=strlen(pstr))
			pstr+=strlen(pstr);
		else
			pstr+=m_nStartChar;
		/////////////////////
		charnum=(rect.right-5)/m_nCharWidth;//要输出的字符数目,初始为窗口可以显示的字符数目
		if((unsigned)charnum>strlen(pstr))
			charnum=strlen(pstr);
		if(m_nSelectedLine==m_nStartLine+i)
		{	pDC->SetBkColor(RGB(0,0,255));
			pDC->SetTextColor(RGB(255,255,255));
		}
		pDC->TextOut(5,y,pstr,charnum);
		if(m_nSelectedLine==m_nStartLine+i)
		{	pDC->SetBkColor(RGB(255,255,255));
			pDC->SetTextColor(RGB(0,0,0));
		}
		i++;
		y+=m_nLineHeight;
		temp=temp->next;
	}
	SetScrollRange(SB_VERT,0,m_nLineHeight*m_nLineNum);
	SetScrollPos(SB_VERT,m_nStartLine*m_nLineHeight);
	SetScrollRange(SB_HORZ,0,m_nCharWidth*m_nCharNum);
	SetScrollPos(SB_HORZ,m_nStartChar*m_nCharWidth);
}

/////////////////////////////////////////////////////////////////////////////
// CErrorView diagnostics

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

void CErrorView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CErrorView message handlers

void CErrorView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	CRect rect;
	GetClientRect(&rect);

	switch(nSBCode)
	{
	case SB_BOTTOM:
		m_nStartLine=m_nLineNum-(rect.bottom)/m_nLineHeight/2;
		break;
	case SB_TOP:
		m_nStartLine=0;
		break;
	case SB_LINEDOWN:
		m_nStartLine++;
		if(m_nStartLine>m_nLineNum-(rect.bottom)/m_nLineHeight/2)
			m_nStartLine=m_nLineNum-(rect.bottom)/m_nLineHeight/2;
		break;
	case SB_LINEUP:
		m_nStartLine--;
		if(m_nStartLine<0)
			m_nStartLine=0;
		break;
	case SB_PAGEDOWN:
		m_nStartLine+=(rect.bottom)/m_nLineHeight;
		if(m_nStartLine>m_nLineNum-(rect.bottom)/m_nLineHeight/2)
			m_nStartLine=m_nLineNum-(rect.bottom)/m_nLineHeight/2;
		break;
	case SB_PAGEUP:
		m_nStartLine-=(rect.bottom)/m_nLineHeight;
		if(m_nStartLine<0)
			m_nStartLine=0;
		break;
	case SB_THUMBPOSITION:
		m_nStartLine=nPos/m_nLineHeight;
		if(m_nStartLine>m_nLineNum-(rect.bottom)/m_nLineHeight/2)
			m_nStartLine=m_nLineNum-(rect.bottom)/m_nLineHeight/2;
		break;
	case SB_THUMBTRACK:
		m_nStartLine=nPos/m_nLineHeight;
		if(m_nStartLine>m_nLineNum-(rect.bottom)/m_nLineHeight/2)
			m_nStartLine=m_nLineNum-(rect.bottom)/m_nLineHeight/2;
		break;
	default:
		break;
	}
	Invalidate();
	CView::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CErrorView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	CRect rect;
	GetClientRect(&rect);

	switch(nSBCode)
	{
	case SB_RIGHT:
		m_nStartChar=m_nCharNum;
		if(m_nStartChar%2)//输出中文必需保证2
			m_nStartChar--;
		break;
	case SB_LEFT:
		m_nStartChar=0;
		break;
	case SB_LINERIGHT:
		m_nStartChar+=2;
		if(m_nStartChar>m_nCharNum)
			m_nStartChar=m_nCharNum;
		if(m_nStartChar%2)//输出中文必需保证2
			m_nStartChar--;
		break;
	case SB_LINELEFT:
		m_nStartChar-=2;
		if(m_nStartChar<0)
			m_nStartChar=0;
		break;
	case SB_PAGERIGHT:
		m_nStartChar+=(rect.right-5)/m_nCharWidth;
		if(m_nStartChar>m_nCharNum)
			m_nStartChar=m_nCharNum;
		if(m_nStartChar%2)//输出中文必需保证2
			m_nStartChar--;
		break;
	case SB_PAGELEFT:
		m_nStartChar-=(rect.right-5)/m_nCharWidth;
		if(m_nStartChar%2)//输出中文必需保证2
			m_nStartChar++;
		if(m_nStartChar<0)
			m_nStartChar=0;
		break;
	case SB_THUMBPOSITION:
		m_nStartChar=nPos/m_nCharWidth;
		if(m_nStartChar>m_nCharNum)
			m_nStartChar=m_nCharNum;
		if(m_nStartChar%2)//输出中文必需保证2
			m_nStartChar--;
		break;
	case SB_THUMBTRACK:
		m_nStartChar=nPos/m_nCharWidth;
		if(m_nStartChar>m_nCharNum)
			m_nStartChar=m_nCharNum;
		if(m_nStartChar%2)//输出中文必需保证2
			m_nStartChar++;
		break;
	default:
		break;
	}
	Invalidate();
	CView::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CErrorView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	
}

void CErrorView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	
}

void CErrorView::OnFileSave() 
{
	// TODO: Add your command handler code here
	
}

void CErrorView::OnFileNew() 
{
	// TODO: Add your command handler code here
	
}

void CErrorView::OnFileSaveAs() 
{
	// TODO: Add your command handler code here
	
}

void CErrorView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
/*	CEdit& theEdit = GetEditCtrl();
	DWORD dwSel = theEdit.GetSel();
	theEdit.SetSel(HIWORD(dwSel), -1);
*/
	int y=point.y/m_nLineHeight;
	if(y<=m_nLineNum-m_nStartLine)
	{
		CSmartCDoc* pDoc =(class CSmartCDoc*) GetDocument();
		ASSERT_VALID(pDoc);
		Node* temp;//要输出信息的链表
		char* pstr;//当前要输出的一行信息指针
		int charnum;//当前要输出的一行信息的字符数目
		temp=pDoc->errorfront.next;
		for(int i=0;i<m_nStartLine+y&&temp->next;i++)
			temp=temp->next;
		pstr=temp->pchar;
		m_nSelectedLine=m_nStartLine+y;
		Invalidate();
		///18---21 is line no.
		pstr=temp->pchar+18;
		int line_no=0;
		for(i=0;i<4;i++)
		{
			line_no*=10;
			line_no+=*pstr-'0';
			pstr++;
		}
		POSITION pos =pDoc->GetFirstViewPosition();
		CEditView* pView =(CEditView*)pDoc->GetNextView(pos);
		CEdit& theEdit =pView->GetEditCtrl();
		int start=theEdit.LineIndex(line_no-1);
		int end=theEdit.LineIndex(line_no);
		theEdit.SetSel(start,end);
	}
	CView::OnLButtonDblClk(nFlags, point);
}

⌨️ 快捷键说明

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