📄 coloreditview.cpp
字号:
// ColorEditView.cpp : implementation of the CColorEditView class
//
// Copyright (c) 1996-1997, Keith Rule
// You may freely use or modify this code provided this
// copyright is included in all derived versions.
#include "stdafx.h"
#include "ColorEdit.h"
#include "ColorEditDoc.h"
#include "ColorEditView.h"
#include "Search.h"
#include <afxadv.h>
const int ScrollTimer = 2;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColorEditView
IMPLEMENT_DYNCREATE(CColorEditView, CScrollView)
BEGIN_MESSAGE_MAP(CColorEditView, CScrollView)
//{{AFX_MSG_MAP(CColorEditView)
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
ON_WM_CHAR()
ON_COMMAND(ID_EDIT_FIND, OnEditFind)
ON_UPDATE_COMMAND_UI(ID_EDIT_FIND, OnUpdateEditFind)
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
ON_WM_KEYDOWN()
ON_WM_TIMER()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorEditView construction/destruction
CColorEditView::CColorEditView()
{
m_tabSize = 4;
m_indent = 10;
m_selectStart = m_selectEnd = -1;
m_caret = 0;
m_mouseMove = m_mouseDown = FALSE;
m_caretPos = CPoint(0, 0);
m_caretVisible = FALSE;
m_scrollTimerEnabled = FALSE;
}
CColorEditView::~CColorEditView()
{
}
BOOL CColorEditView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CColorEditView drawing
void CColorEditView::OnDraw(CDC* dc)
{
CColorEditDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CalcScrollSize(dc);
CMemDC pDC(dc);
long lineStart, lineStop;
LOGFONT lf;
CPoint scrollPos = GetScrollPosition();
CRect rcBounds;
if (pDC->IsPrinting()) {
m_font.GetLogFont(&lf);
CFont* oldFont = pDC->SelectObject(&m_font);
CPoint caret = GetCaretPos(pDC, m_indent);
if (CPoint(caret-scrollPos) != m_caretPos) {
m_caretPos = CPoint(caret-scrollPos);
SetCaretPos(m_caretPos);
}
TRACE(_T("m_nPageStartLine = %d, m_nPageEndLine = %d, lineCount = %d\r\n"), m_nPageStartLine, m_nPageEndLine, pDoc->GetLineCount());
for (int i = m_nPageStartLine; i < m_nPageEndLine && i < pDoc->GetLineCount(); i++) {
DrawText(pDC, CPoint(m_indent, ((i-m_nPageStartLine)*lf.lfHeight)), i, FALSE);
}
pDC->SelectObject(oldFont);
} else {
GetClientRect(&rcBounds);
rcBounds += scrollPos;
pDC->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
m_font.GetLogFont(&lf);
CFont* oldFont = pDC->SelectObject(&m_font);
CPoint caret = GetCaretPos(pDC, m_indent);
if (CPoint(caret-scrollPos) != m_caretPos) {
m_caretPos = CPoint(caret-scrollPos);
SetCaretPos(m_caretPos);
}
lineStart = rcBounds.top/lf.lfHeight;
lineStop = rcBounds.bottom/lf.lfHeight+1;
for (int i = lineStart; i < lineStop && i < pDoc->GetLineCount(); i++) {
DrawText(pDC, CPoint(m_indent, (i*lf.lfHeight)), i);
}
pDC->SelectObject(oldFont);
}
}
void CColorEditView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CWnd* desktop = (CWnd*) GetDesktopWindow();
CDC* pDC = desktop->GetDC();
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
m_cHeight = lf.lfHeight = tm.tmHeight;
strcpy(lf.lfFaceName, _T("Courier New"));
m_font.CreateFontIndirect(&lf);
CalcScrollSize(pDC);
desktop->ReleaseDC(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CColorEditView printing
BOOL CColorEditView::OnPreparePrinting(CPrintInfo* pInfo)
{
return DoPreparePrinting(pInfo);
}
void CColorEditView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
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;
int nPages = (int)(pDoc->GetLineCount() / m_nLinesPerPage) + 1;
m_nPageStartLine = (m_nCurPage-1)*m_nLinesPerPage;
m_nPageEndLine = m_nPageStartLine+m_nLinesPerPage-1;
pInfo->m_nNumPreviewPages = nPages;
pInfo->SetMaxPage(nPages);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(nLogPixPerInchX, nLogPixPerInchY);
pDC->SetViewportExt(nPrinterPixPerInchX, nPrinterPixPerInchY);
}
void CColorEditView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CColorEditView diagnostics
#ifdef _DEBUG
void CColorEditView::AssertValid() const
{
CScrollView::AssertValid();
}
void CColorEditView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CColorEditDoc* CColorEditView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CColorEditDoc)));
return (CColorEditDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CColorEditView message handlers
CPoint CColorEditView::GetCaretPos(CDC * pDC, long xoffset)
{
CColorEditDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
long line = pDoc->Offset2Line(m_caret);
long col = m_caret - pDoc->GetLineOffset(line);
CString& string = pDoc->GetLine(line).Left(col);
return GetExtent(pDC, string, xoffset, line);
}
CPoint CColorEditView::GetExtent(CDC * pDC, const CString & string, long xoffset, long line)
{
CSize size = pDC->GetTextExtent(_T(" "));
int x = 0, y = size.cy;
int tabWidth = size.cx*m_tabSize;
for (int i = 0; i < string.GetLength(); i++) {
TCHAR c = string[i];
if (iswprint(c) || iswspace(c)) {
if (c == '\t') {
x = ((x/tabWidth)+1)*tabWidth;
} else {
x += pDC->GetTextExtent(c).cx;
}
}
}
return CPoint(x+xoffset, y*line);
}
CSize CColorEditView::GetScrollSize(CDC * pDC)
{
CColorEditDoc* pDoc = GetDocument();
long lineCount = pDoc->GetLineCount();
long width = 0;
CString line;
long x = 0;
CSize size = pDC->GetTextExtent(_T(" "));
CFont* oldFont = pDC->SelectObject(&m_font);
for (long i = 0; i < lineCount; i++) {
CString& line = pDoc->GetLine(i);
x = GetExtent(pDC, line, m_indent*2, i).x;
if (x > width) {
width = x;
}
}
pDC->SelectObject(oldFont);
return CSize(width, size.cy*(lineCount+1));
}
CPoint CColorEditView::DrawText(CDC * pDC, CPoint pos, int line, BOOL bDrawSelection)
{
CColorEditDoc* pDoc = GetDocument();
long x = pos.x, y = pos.y;
long offset = pDoc->GetLineOffset(line);
int spaceWidth = GetExtent(pDC, _T(" ")).x;
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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -