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

📄 coloreditview.cpp

📁 ColorEdit源程序代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	long		length = pDoc->GetLength();
	TCHAR		c;
	COLORREF	color;
	CString		string;
	long		i;


	if (offset < length) {
		for (i = offset, c = pDoc->GetAt(i), color = pDoc->GetColor(i); i < length && c != '\n'; i++, 
					c = (i < length ? pDoc->GetAt(i) : c), color = (i < length ? pDoc->GetColor(i) : color)) {
			if (bDrawSelection && i >= m_selectStart && i < m_selectEnd) {
#ifndef BLACK_BACKGROUND
				// Hilight text on selection
				pDC->SetBkColor(RGB(230, 230, 230));
				pDC->SetTextColor(color);
#else
				// Invert color on selection
				pDC->SetBkColor(RGB(0, 0, 0));
				pDC->SetTextColor((~color) & 0xffffff);
#endif
			} else {
				pDC->SetBkColor(RGB(255, 255, 255));
				pDC->SetTextColor(color);
			}
			
			string = c;
			if (c != '\t') pDC->TextOut(x, y, c);
			x += GetExtent(pDC, string).x;
		}
	}

	return CPoint(x, y);
}

BOOL CColorEditView::OnEraseBkgnd(CDC* pDC) 
{
	return FALSE;
}

void CColorEditView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	if (bActivate) {
		if (!m_caretVisible) {
			CreateSolidCaret(2, m_cHeight);
			ShowCaret();
			SetCaretPos(m_caretPos);
			m_caretVisible = TRUE;
		}
	} else {
		if (m_caretVisible) {
			HideCaret();
			m_caretVisible = FALSE;
		}
	}

	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void CColorEditView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	long			p;
	CColorEditDoc*	pDoc = GetDocument();

	if ((p = SelectionPoint(point))  >= 0 && p < pDoc->GetLength()) {
		if (p >= 0 && p < pDoc->GetLength()) {
			if (!IsWhite(pDoc->GetAt(p))) {
				m_selectStart = m_selectEnd = p;

				while (m_selectStart > 1 && !IsWhite(pDoc->GetAt(m_selectStart-1)))
					m_selectStart--;

				while (m_selectEnd > 0 && !IsWhite(pDoc->GetAt(m_selectEnd)))
					m_selectEnd++;

				m_caret = m_selectEnd;
			}
		}

		Invalidate();
	}
}

void CColorEditView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	long	p;

	SetCapture();
	m_mouseDown = TRUE;
	m_mouseMove = FALSE;

	if ((p = SelectionPoint(point)) != m_caret) {
		m_caret = m_selectStart = m_selectEnd = p;
		Invalidate();
	}
}

void CColorEditView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	m_mouseDown = FALSE;
	m_mouseMove = FALSE;
	ReleaseCapture();
	if (m_scrollTimerEnabled) {
		m_scrollTimerEnabled = FALSE;
		KillTimer(ScrollTimer);
	}
	CScrollView::OnLButtonUp(nFlags, point);
}

void CColorEditView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CRect	client;

	GetClientRect(&client);

	m_mouseMove = TRUE;
	m_mousePosition = point;

	if (m_mouseDown) {
		if (client.PtInRect(point)) {
			if (m_scrollTimerEnabled) {
				m_scrollTimerEnabled = FALSE;
				KillTimer(ScrollTimer);
			}
			long p = SelectionPoint(point);
			m_caret = p;
			if (p <= m_selectStart) {
				m_selectStart = p;
			} else {
				m_selectEnd = p;
			}
			Invalidate();
		} else {
			if (!m_scrollTimerEnabled) {
				m_autoScrollCount = 0;
				m_scrollTimerEnabled = TRUE;
				SetTimer(ScrollTimer, 175, NULL);
			}
		}
	} 
	
	CScrollView::OnMouseMove(nFlags, point);
}

long CColorEditView::SelectionPoint(CPoint point)
{
	LOGFONT			lf;
	CPaintDC		dc(this);

	CColorEditDoc* pDoc = GetDocument();

	OnPrepareDC(&dc);
	dc.DPtoLP(&point);
	CPoint	pos = point;

	m_font.GetLogFont(&lf);
	CFont* oldFont = dc.SelectObject(&m_font);
	CSize		size = GetExtent(&dc, _T(" "));

	long line = pos.y/size.cy;

	CString string = pDoc->GetLine(line);
	long offset = pDoc->GetLineOffset(line);

	for (int i = 0; i < string.GetLength()-1; i++) {
		CSize size = GetExtent(&dc, string.Left(i+1));
		if (pos.x-m_indent < size.cx) {
			return offset + i;
		}
	}

	dc.SelectObject(oldFont);
	return offset + string.GetLength();
}

BOOL CColorEditView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	if (nHitTest == HTCLIENT && pWnd == this) {
		::SetCursor(::LoadCursor(NULL, IDC_IBEAM));
		return TRUE;   
	}
	
	return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}

void CColorEditView::OnEditUndo() 
{
	CColorEditDoc* pDoc = GetDocument();
	pDoc->Undo();
	pDoc->m_text.Reset();
	Invalidate();
}

void CColorEditView::OnUpdateEditUndo(CCmdUI* pCmdUI) 
{
	CColorEditDoc* pDoc = GetDocument();
	pCmdUI->Enable(pDoc->CanUndo());
}

void CColorEditView::OnEditRedo() 
{
	CColorEditDoc* pDoc = GetDocument();
	pDoc->Redo();
	pDoc->m_text.Reset();
	Invalidate();
}

void CColorEditView::OnUpdateEditRedo(CCmdUI* pCmdUI) 
{
	CColorEditDoc* pDoc = GetDocument();
	pCmdUI->Enable(pDoc->CanRedo());
}

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

	switch (nChar) {
	case VK_DELETE:
		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 && 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:
		{
			long lineoffset = pDoc->GetLineOffset(pDoc->Offset2Line(m_caret));
		
			m_caret = pDoc->m_text.GetLength()+lineoffset;//-1;
		
			ScrollToCaret();
		
			break;
		}
	case VK_HOME:			
		{
		long lineoffset = pDoc->GetLineOffset(pDoc->Offset2Line(m_caret));
		//m_caret = 0;
		m_caret=lineoffset;
		ScrollToCaret();
		break;
		}
	case VK_LEFT:
		if (m_caret > 0) {
			m_caret--;
			ScrollToCaret();
		}
		break;
	case VK_RIGHT:
		if (m_caret < pDoc->m_text.GetLength()) {
			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);
	}
}

int m_bracket=0;
/////////////

///////////////   Global variables for exchange information among classes
    COLORREF KeywordColor;
	COLORREF PmacKeywordColor;
	COLORREF PVariableColor;
	COLORREF QVariableColor;
	COLORREF IVariableColor;
	COLORREF MVariableColor;

	//////////////////
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();
	} 


	if('('==nChar)
		m_bracket++;
	else if(')'==nChar)
		m_bracket--;

}

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();

⌨️ 快捷键说明

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