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

📄 coloreditview.cpp

📁 检测语法的Edit类
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			pDoc->m_text.Delete(m_selectStart, m_selectEnd-m_selectStart);
			m_caret = m_selectEnd = m_selectStart;
		} else if (m_caret >= 0 && pDoc->m_text.GetLength() > m_caret+1) {
			pDoc->m_text.Delete(m_caret, 1);
		}
		pDoc->SetModifiedFlag();
		pDoc->CheckPoint();
		CalcScrollSize();
		Invalidate();
		break;
	case VK_BACK:
		if (m_selectStart != m_selectEnd && m_selectStart >= 0) {
			pDoc->m_text.Delete(m_selectStart, m_selectEnd-m_selectStart);
			m_caret = m_selectEnd = m_selectStart;
		} else if (m_caret > 0) {
			m_caret -= 1;
			pDoc->m_text.Delete(m_caret, 1);
		}
		pDoc->SetModifiedFlag();
		pDoc->CheckPoint();
		CalcScrollSize();
		Invalidate();
		break;
	case VK_RETURN:
		pDoc->m_text.Insert(m_caret, _T('\n'));
		m_caret += 1;
		pDoc->SetModifiedFlag();
		pDoc->CheckPoint();
		CalcScrollSize();
		Invalidate();
		break;
	case VK_END:
		m_caret = pDoc->m_text.GetLength()-1;
		ScrollToCaret();
		break;
	case VK_HOME:
		m_caret = 0;
		ScrollToCaret();
		break;
	case VK_LEFT:
		if (m_caret > 0) {
			m_caret--;
			ScrollToCaret();
		}
		break;
	case VK_RIGHT:
		if (m_caret < pDoc->m_text.GetLength()-1) {
			m_caret++;
			ScrollToCaret();
		}
		break;
	case VK_UP:
		{
			long line = pDoc->Offset2Line(m_caret);
			long lineoffset = pDoc->GetLineOffset(line);
			long pos = m_caret - lineoffset;

			if (line > 0) {
				long prevLine = pDoc->GetLineOffset(line-1);
				long prevLineLen = pDoc->GetLine(line-1).GetLength();

				if (pos < prevLineLen) {
					m_caret = prevLine + pos;
				} else {
					m_caret = prevLine + prevLineLen;
				}

				ScrollToCaret();
			}
		}
		break;
	case VK_DOWN:
		{
			long line = pDoc->Offset2Line(m_caret);
			long lineoffset = pDoc->GetLineOffset(line);
			long pos = m_caret - lineoffset;
			long lineCount = pDoc->GetLineCount();

			if (line < lineCount-1) {
				long nextLine = pDoc->GetLineOffset(line+1);
				long nextLineLen = pDoc->GetLine(line+1).GetLength();

				if (pos < nextLineLen) {
					m_caret = nextLine + pos;
				} else {
					m_caret = nextLine + nextLineLen;
				}

				ScrollToCaret();
			}
		}
		break;
		break;
	default:
		CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
	}
}

void CColorEditView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	CColorEditDoc*	pDoc = GetDocument();

	if (iswprint(nChar)) {
		pDoc->m_text.Insert(m_caret, nChar);
		m_caret += 1;
		pDoc->SetModifiedFlag();
		pDoc->CheckPoint();
		CalcScrollSize();
		Invalidate();
	} 
}

void CColorEditView::CalcScrollSize(CDC * pDC)
{
	if (pDC == NULL) {
		LOGFONT			lf;
		CPaintDC		dc(this);
		OnPrepareDC(&dc);
		m_font.GetLogFont(&lf);
		CFont* oldFont = dc.SelectObject(&m_font);
		m_scrollSize = GetScrollSize(&dc);
		CSize status = dc.SetWindowExt(m_scrollSize);
		ASSERT(status != CSize(0, 0));

		SetScrollSizes(MM_TEXT, m_scrollSize);
		dc.SelectObject(oldFont);

		ASSERT(m_scrollSize == GetTotalSize());
	} else {
		m_scrollSize = GetScrollSize(pDC);
		CSize status = pDC->SetWindowExt(m_scrollSize);
		ASSERT(status != CSize(0, 0));
		SetScrollSizes(MM_TEXT, m_scrollSize);

		ASSERT(m_scrollSize == GetTotalSize());
	}
}

long CColorEditView::CurrentPosition()
{
	return m_caret;
}


void CColorEditView::SetCurrentPosition(long position)
{
	CColorEditDoc* pDoc = GetDocument();
	if (position > 0 && position < pDoc->GetLength()) {
		m_caret = position;
		ScrollToCurrent();
	}
}

void CColorEditView::ScrollToCurrent()
{
	LOGFONT			lf;
	CPaintDC		dc(this);
	CRect			client;
	CPoint			scrollPos = GetScrollPosition();

	CColorEditDoc* pDoc = GetDocument();

	OnPrepareDC(&dc);

	m_font.GetLogFont(&lf);
	CFont* oldFont = dc.SelectObject(&m_font);

	GetClientRect(&client);

	client += scrollPos;
	CPoint caret = TextPosition2ScreenCoord(&dc, m_caret);
	long x = scrollPos.x;
	long y = scrollPos.y;

	if (caret.y > client.bottom) {
		y = caret.y-client.Height()/2;
	} else if (caret.y < client.top) {
		y = caret.y-client.Height()/2;
	}

	if (caret.x < client.left) {
		if (caret.x < client.Width()) {
			x = 0;
		} else {
			x = caret.x-client.Width()/2;
		}
	} else if (caret.x > client.right) {
		if (caret.x < client.Width()) {
			x = 0;
		} else {
			x = caret.x-client.Width()/2;
		}
	}

	dc.SelectObject(oldFont);

	ScrollToPosition(CPoint(x, y));

	Invalidate();
}

void CColorEditView::SetSelection(long start, long end)
{
	if (start < end) {
		m_selectStart = start;
		m_caret = m_selectEnd = end;
	} else {
		m_caret = m_selectStart = end;
		m_selectEnd = start;
	}
}

void CColorEditView::ScrollToCaret()
{
	LOGFONT			lf;
	CPaintDC		dc(this);
	CRect			client;
	CPoint			scrollPos = GetScrollPosition();

	CColorEditDoc* pDoc = GetDocument();

	OnPrepareDC(&dc);

	m_font.GetLogFont(&lf);
	CFont* oldFont = dc.SelectObject(&m_font);

	GetClientRect(&client);

	client += scrollPos;
	CPoint caret = TextPosition2ScreenCoord(&dc, m_caret);
	long x = scrollPos.x;
	long y = scrollPos.y;

	if (caret.y > client.bottom) {
		y = caret.y-client.Height()/2;
	} else if (caret.y < client.top) {
		y = caret.y-client.Height()/2;
	}

	if (caret.x < client.left) {
		if (caret.x < client.Width()) {
			x = 0;
		} else {
			x = caret.x-client.Width()/2;
		}
	} else if (caret.x > client.right) {
		if (caret.x < client.Width()) {
			x = 0;
		} else {
			x = caret.x-client.Width()/2;
		}
	}

	dc.SelectObject(oldFont);

	ScrollToPosition(CPoint(x, y));

	Invalidate();
}

void CColorEditView::ScrollToSelection()
{
	LOGFONT			lf;
	CPaintDC		dc(this);
	CRect			client;
	CPoint			scrollPos = GetScrollPosition();

	CColorEditDoc* pDoc = GetDocument();

	OnPrepareDC(&dc);

	m_font.GetLogFont(&lf);
	CFont* oldFont = dc.SelectObject(&m_font);

	GetClientRect(&client);

	client += scrollPos;
	CPoint selectEnd = TextPosition2ScreenCoord(&dc, m_selectEnd);
	long x = scrollPos.x;
	long y = scrollPos.y;

	if (selectEnd.y > client.bottom) {
		y = selectEnd.y-client.Height()/2;
	} else if (selectEnd.y < client.top) {
		y = selectEnd.y-client.Height()/2;
	}

	if (selectEnd.x < client.left) {
		if (selectEnd.x < client.Width()) {
			x = 0;
		} else {
			x = selectEnd.x-client.Width()/2;
		}
	} else if (selectEnd.x > client.right) {
		if (selectEnd.x < client.Width()) {
			x = 0;
		} else {
			x = selectEnd.x-client.Width()/2;
		}
	}

	dc.SelectObject(oldFont);

	ScrollToPosition(CPoint(x, y));

	Invalidate();
}

long CColorEditView::GetSearchPosition()
{
	if (m_selectEnd >= 0) {
		return m_selectEnd;
	}
	return CurrentPosition();
}

CPoint CColorEditView::TextPosition2ScreenCoord(CDC* pDC, long pos)
{
	CColorEditDoc*	pDoc = GetDocument();
	long		line = pDoc->m_text.Offset2Line(pos);
	long		offset = pDoc->GetLineOffset(line);
	CString		string = pDoc->m_text.Mid(offset, pos-offset);
	return CPoint(GetExtent(pDC, string).x+m_indent, GetExtent(pDC, _T(" ")).y*line);
}

BOOL CColorEditView::ReplaceSelection(CString string)
{
	CColorEditDoc*	pDoc = GetDocument();
	if (m_selectStart != m_selectEnd) {
		pDoc->m_text.Replace(m_selectStart, m_selectEnd - m_selectStart, string);
		m_caret = m_selectEnd = m_selectStart;
		pDoc->CheckPoint();
		CalcScrollSize();
		Invalidate();
		return TRUE;
	}
	return FALSE;
}

void CColorEditView::OnEditFind() 
{
	CColorEditDoc* pDoc = GetDocument();
	CSearch	dlg;
	dlg.m_pString = &pDoc->m_text;
	dlg.m_view = this;
	dlg.m_search_string = pDoc->Range(m_selectStart, m_selectEnd);
	dlg.DoModal();
	CreateSolidCaret(2, m_cHeight);
	ShowCaret();
	ScrollToCurrent();
	pDoc->UpdateAllViews(NULL);	
}

void CColorEditView::OnUpdateEditFind(CCmdUI* pCmdUI) 
{
	CColorEditDoc* pDoc = GetDocument();
	pCmdUI->Enable(pDoc->GetLength() > 0);	
}

void CColorEditView::DisableCheckPoint()
{
	CColorEditDoc* pDoc = GetDocument();
	pDoc->DisableCheckPoint();
}

void CColorEditView::EnableCheckPoint()
{
	CColorEditDoc* pDoc = GetDocument();
	pDoc->EnableCheckPoint();
}

void CColorEditView::CheckPoint()
{
	CColorEditDoc* pDoc = GetDocument();
	pDoc->CheckPoint();
}

void CColorEditView::OnEditCopy() 
{
	CColorEditDoc* pDoc = GetDocument();
	CString text = pDoc->Range(m_selectStart, m_selectEnd);	

	COleDataSource*	pSource = new COleDataSource();
	CSharedFile		sf(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT);
	sf.Write(text, text.GetLength());
	HGLOBAL hMem = sf.Detach();
	if (!hMem) return;
	pSource->CacheGlobalData(CF_TEXT, hMem);
	pSource->SetClipboard();
}

void CColorEditView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_selectStart < m_selectEnd);
}

void CColorEditView::OnEditCut() 
{
	CColorEditDoc* pDoc = GetDocument();
	CString text = pDoc->Range(m_selectStart, m_selectEnd);	

	COleDataSource*	pSource = new COleDataSource();
	CSharedFile		sf(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT);
	sf.Write(text, text.GetLength());
	HGLOBAL hMem = sf.Detach();
	if (!hMem) return;
	pSource->CacheGlobalData(CF_TEXT, hMem);
	pSource->SetClipboard();
	ReplaceSelection(_T(""));
}

void CColorEditView::OnUpdateEditCut(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_selectStart < m_selectEnd);
}

void CColorEditView::OnEditPaste() 
{
	COleDataObject	obj;

	if (obj.AttachClipboard()) {
		if (obj.IsDataAvailable(CF_TEXT)) {
			HGLOBAL hmem = obj.GetGlobalData(CF_TEXT);
			CMemFile sf((BYTE*) ::GlobalLock(hmem), ::GlobalSize(hmem));
			CString buffer;

			LPSTR str = buffer.GetBufferSetLength(::GlobalSize(hmem));
			sf.Read(str, ::GlobalSize(hmem));
			::GlobalUnlock(hmem);

			if (m_selectStart < m_selectEnd) {
				ReplaceSelection(buffer);
			} else {
				CColorEditDoc*	pDoc = GetDocument();
				pDoc->m_text.Insert(m_caret, buffer);
				pDoc->CheckPoint();
				CalcScrollSize();
				Invalidate();
			}
		}
	}
	
}

void CColorEditView::OnUpdateEditPaste(CCmdUI* pCmdUI) 
{
	COleDataObject	obj;
	obj.AttachClipboard();
	pCmdUI->Enable(obj.IsDataAvailable(CF_TEXT));
}

void CColorEditView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	if (pDC->IsPrinting()) {
		CColorEditDoc*	pDoc = GetDocument();
		ASSERT_VALID(pDoc);

		int nPrinterPixCX = pDC->GetDeviceCaps(HORZRES);  
		int nPrinterPixCY = pDC->GetDeviceCaps(VERTRES);  

		int nPrinterPixPerInchX = pDC->GetDeviceCaps(LOGPIXELSX);
		int nPrinterPixPerInchY = pDC->GetDeviceCaps(LOGPIXELSY);

		CClientDC dcDisplay(this);
		int nLogPixPerInchX = dcDisplay.GetDeviceCaps(LOGPIXELSX);
		int nLogPixPerInchY = dcDisplay.GetDeviceCaps(LOGPIXELSY);

		int nPagePixWidth = (int)((DWORD)nPrinterPixCX * nLogPixPerInchX / nPrinterPixPerInchX);
		int nPagePixHeight = (int)((DWORD)nPrinterPixCY * nLogPixPerInchY / nPrinterPixPerInchY);
		
		m_nCurPage = pInfo->m_nCurPage;

		LOGFONT lf;
		m_font.GetLogFont(&lf);
		int nTextHeight = lf.lfHeight;
 		
 		m_nLinesPerPage = nPagePixHeight / nTextHeight - 1;

		TRACE("m_nLinesPerPage = %d\r\n", m_nLinesPerPage);
		
		m_nPageStartLine = (m_nCurPage-1)*m_nLinesPerPage;
		m_nPageEndLine = m_nPageStartLine+m_nLinesPerPage-1;

		pDC->SetMapMode(MM_ANISOTROPIC);
		pDC->SetWindowExt(nLogPixPerInchX, nLogPixPerInchY);
		pDC->SetViewportExt(nPrinterPixPerInchX, nPrinterPixPerInchY);
	} else {
		CScrollView::OnPrepareDC(pDC, pInfo);
	}
}


void CColorEditView::OnTimer(UINT nIDEvent) 
{
	if (nIDEvent == ScrollTimer) {
		AutoScroll();
		return;
	} 
	CScrollView::OnTimer(nIDEvent);
}

void CColorEditView::AutoScroll()
{
	CRect	client;
	GetClientRect(&client);

	if (client.PtInRect(m_mousePosition)) return;
	long p = SelectionPoint(m_mousePosition);
	m_caret = p;
	if (p <= m_selectStart) {
		m_selectStart = p;
	} else {
		m_selectEnd = p;
	}
	SetCurrentPosition(p);
	TRACE(_T("AutoScroll(%d)\r\n"), m_autoScrollCount++);
}

⌨️ 快捷键说明

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