📄 ccrystaltextview2.cpp
字号:
(!m_bDisableDragAndDrop)) // And D&D Not Disabled
{
m_bPreparingToDrag = TRUE;
}
else
{
AdjustTextPoint(point);
m_ptCursorPos = ClientToText(point);
if (! bShift)
m_ptAnchor = m_ptCursorPos;
CPoint ptStart, ptEnd;
if (bControl)
{
if (m_ptCursorPos.y < m_ptAnchor.y ||
m_ptCursorPos.y == m_ptAnchor.y && m_ptCursorPos.x < m_ptAnchor.x)
{
ptStart = WordToLeft(m_ptCursorPos);
ptEnd = WordToRight(m_ptAnchor);
}
else
{
ptStart = WordToLeft(m_ptAnchor);
ptEnd = WordToRight(m_ptCursorPos);
}
}
else
{
ptStart = m_ptAnchor;
ptEnd = m_ptCursorPos;
}
m_ptCursorPos = ptEnd;
UpdateCaret();
EnsureVisible(m_ptCursorPos);
SetSelection(ptStart, ptEnd);
SetCapture();
m_nDragSelTimer = SetTimer(CRYSTAL_TIMER_DRAGSEL, 100, NULL);
ASSERT(m_nDragSelTimer != 0);
m_bWordSelection = bControl;
m_bLineSelection = FALSE;
m_bDragSelection = TRUE;
}
}
ASSERT_VALIDTEXTPOS(m_ptCursorPos);
}
void CCrystalTextView::OnMouseMove(UINT nFlags, CPoint point)
{
CView::OnMouseMove(nFlags, point);
if (m_bDragSelection)
{
BOOL bOnMargin = point.x < GetMarginWidth();
AdjustTextPoint(point);
CPoint ptNewCursorPos = ClientToText(point);
CPoint ptStart, ptEnd;
if (m_bLineSelection)
{
if (bOnMargin)
{
if (ptNewCursorPos.y < m_ptAnchor.y ||
ptNewCursorPos.y == m_ptAnchor.y && ptNewCursorPos.x < m_ptAnchor.x)
{
ptEnd = m_ptAnchor;
if (ptEnd.y == GetLineCount() - 1)
{
ptEnd.x = GetLineLength(ptEnd.y);
}
else
{
ptEnd.y ++;
ptEnd.x = 0;
}
ptNewCursorPos.x = 0;
m_ptCursorPos = ptNewCursorPos;
}
else
{
ptEnd = m_ptAnchor;
ptEnd.x = 0;
m_ptCursorPos = ptNewCursorPos;
if (ptNewCursorPos.y == GetLineCount() - 1)
{
ptNewCursorPos.x = GetLineLength(ptNewCursorPos.y);
}
else
{
ptNewCursorPos.y ++;
ptNewCursorPos.x = 0;
}
m_ptCursorPos.x = 0;
}
UpdateCaret();
SetSelection(ptNewCursorPos, ptEnd);
return;
}
// Moving to normal selection mode
::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_IBEAM)));
m_bLineSelection = m_bWordSelection = FALSE;
}
if (m_bWordSelection)
{
if (ptNewCursorPos.y < m_ptAnchor.y ||
ptNewCursorPos.y == m_ptAnchor.y && ptNewCursorPos.x < m_ptAnchor.x)
{
ptStart = WordToLeft(ptNewCursorPos);
ptEnd = WordToRight(m_ptAnchor);
}
else
{
ptStart = WordToLeft(m_ptAnchor);
ptEnd = WordToRight(ptNewCursorPos);
}
}
else
{
ptStart = m_ptAnchor;
ptEnd = ptNewCursorPos;
}
m_ptCursorPos = ptEnd;
UpdateCaret();
SetSelection(ptStart, ptEnd);
}
if (m_bPreparingToDrag)
{
m_bPreparingToDrag = FALSE;
HGLOBAL hData = PrepareDragData();
if (hData != NULL)
{
if (m_pTextBuffer != NULL)
m_pTextBuffer->BeginUndoGroup();
COleDataSource ds;
ds.CacheGlobalData(CF_TEXT, hData);
m_bDraggingText = TRUE;
DROPEFFECT de = ds.DoDragDrop(GetDropEffect());
if (de != DROPEFFECT_NONE)
OnDropSource(de);
m_bDraggingText = FALSE;
if (m_pTextBuffer != NULL)
m_pTextBuffer->FlushUndoGroup(this);
}
}
ASSERT_VALIDTEXTPOS(m_ptCursorPos);
}
void CCrystalTextView::OnLButtonUp(UINT nFlags, CPoint point)
{
CView::OnLButtonUp(nFlags, point);
if (m_bDragSelection)
{
AdjustTextPoint(point);
CPoint ptNewCursorPos = ClientToText(point);
CPoint ptStart, ptEnd;
if (m_bLineSelection)
{
CPoint ptEnd;
if (ptNewCursorPos.y < m_ptAnchor.y ||
ptNewCursorPos.y == m_ptAnchor.y && ptNewCursorPos.x < m_ptAnchor.x)
{
ptEnd = m_ptAnchor;
if (ptEnd.y == GetLineCount() - 1)
{
ptEnd.x = GetLineLength(ptEnd.y);
}
else
{
ptEnd.y ++;
ptEnd.x = 0;
}
ptNewCursorPos.x = 0;
m_ptCursorPos = ptNewCursorPos;
}
else
{
ptEnd = m_ptAnchor;
ptEnd.x = 0;
if (ptNewCursorPos.y == GetLineCount() - 1)
{
ptNewCursorPos.x = GetLineLength(ptNewCursorPos.y);
}
else
{
ptNewCursorPos.y ++;
ptNewCursorPos.x = 0;
}
m_ptCursorPos = ptNewCursorPos;
}
EnsureVisible(m_ptCursorPos);
UpdateCaret();
SetSelection(ptNewCursorPos, ptEnd);
}
else
{
if (m_bWordSelection)
{
if (ptNewCursorPos.y < m_ptAnchor.y ||
ptNewCursorPos.y == m_ptAnchor.y && ptNewCursorPos.x < m_ptAnchor.x)
{
ptStart = WordToLeft(ptNewCursorPos);
ptEnd = WordToRight(m_ptAnchor);
}
else
{
ptStart = WordToLeft(m_ptAnchor);
ptEnd = WordToRight(ptNewCursorPos);
}
}
else
{
ptStart = m_ptAnchor;
ptEnd = m_ptCursorPos;
}
m_ptCursorPos = ptEnd;
EnsureVisible(m_ptCursorPos);
UpdateCaret();
SetSelection(ptStart, ptEnd);
}
ReleaseCapture();
KillTimer(m_nDragSelTimer);
m_bDragSelection = FALSE;
}
if (m_bPreparingToDrag)
{
m_bPreparingToDrag = FALSE;
AdjustTextPoint(point);
m_ptCursorPos = ClientToText(point);
EnsureVisible(m_ptCursorPos);
UpdateCaret();
SetSelection(m_ptCursorPos, m_ptCursorPos);
}
ASSERT_VALIDTEXTPOS(m_ptCursorPos);
}
void CCrystalTextView::OnTimer(UINT nIDEvent)
{
CView::OnTimer(nIDEvent);
if (nIDEvent == CRYSTAL_TIMER_DRAGSEL)
{
ASSERT(m_bDragSelection);
CPoint pt;
::GetCursorPos(&pt);
ScreenToClient(&pt);
CRect rcClient;
GetClientRect(&rcClient);
BOOL bChanged = FALSE;
// Scroll vertically, if necessary
int nNewTopLine = m_nTopLine;
int nLineCount = GetLineCount();
if (pt.y < rcClient.top)
{
nNewTopLine --;
if (pt.y < rcClient.top - GetLineHeight())
nNewTopLine -= 2;
}
else
if (pt.y >= rcClient.bottom)
{
nNewTopLine ++;
if (pt.y >= rcClient.bottom + GetLineHeight())
nNewTopLine += 2;
}
if (nNewTopLine < 0)
nNewTopLine = 0;
if (nNewTopLine >= nLineCount)
nNewTopLine = nLineCount - 1;
if (m_nTopLine != nNewTopLine)
{
ScrollToLine(nNewTopLine);
UpdateSiblingScrollPos(TRUE);
bChanged = TRUE;
}
// Scroll horizontally, if necessary
int nNewOffsetChar = m_nOffsetChar;
int nMaxLineLength = GetMaxLineLength();
if (pt.x < rcClient.left)
nNewOffsetChar --;
else
if (pt.x >= rcClient.right)
nNewOffsetChar ++;
if (nNewOffsetChar >= nMaxLineLength)
nNewOffsetChar = nMaxLineLength - 1;
if (nNewOffsetChar < 0)
nNewOffsetChar = 0;
if (m_nOffsetChar != nNewOffsetChar)
{
ScrollToChar(nNewOffsetChar);
UpdateCaret();
UpdateSiblingScrollPos(FALSE);
bChanged = TRUE;
}
// Fix changes
if (bChanged)
{
AdjustTextPoint(pt);
CPoint ptNewCursorPos = ClientToText(pt);
if (ptNewCursorPos != m_ptCursorPos)
{
m_ptCursorPos = ptNewCursorPos;
UpdateCaret();
}
SetSelection(m_ptAnchor, m_ptCursorPos);
}
}
}
void CCrystalTextView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CView::OnLButtonDblClk(nFlags, point);
if (! m_bDragSelection)
{
AdjustTextPoint(point);
m_ptCursorPos = ClientToText(point);
m_ptAnchor = m_ptCursorPos;
CPoint ptStart, ptEnd;
if (m_ptCursorPos.y < m_ptAnchor.y ||
m_ptCursorPos.y == m_ptAnchor.y && m_ptCursorPos.x < m_ptAnchor.x)
{
ptStart = WordToLeft(m_ptCursorPos);
ptEnd = WordToRight(m_ptAnchor);
}
else
{
ptStart = WordToLeft(m_ptAnchor);
ptEnd = WordToRight(m_ptCursorPos);
}
m_ptCursorPos = ptEnd;
UpdateCaret();
EnsureVisible(m_ptCursorPos);
SetSelection(ptStart, ptEnd);
SetCapture();
m_nDragSelTimer = SetTimer(CRYSTAL_TIMER_DRAGSEL, 100, NULL);
ASSERT(m_nDragSelTimer != 0);
m_bWordSelection = TRUE;
m_bLineSelection = FALSE;
m_bDragSelection = TRUE;
}
}
void CCrystalTextView::OnEditCopy()
{
Copy();
}
void CCrystalTextView::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_ptSelStart != m_ptSelEnd);
}
void CCrystalTextView::OnEditSelectAll()
{
SelectAll();
}
void CCrystalTextView::OnUpdateEditSelectAll(CCmdUI* pCmdUI)
{
pCmdUI->Enable(TRUE);
}
void CCrystalTextView::OnRButtonDown(UINT nFlags, CPoint point)
{
CPoint pt = point;
AdjustTextPoint(pt);
pt = ClientToText(pt);
if (! IsInsideSelBlock(pt))
{
m_ptAnchor = m_ptCursorPos = pt;
SetSelection(m_ptCursorPos, m_ptCursorPos);
EnsureVisible(m_ptCursorPos);
UpdateCaret();
}
CView::OnRButtonDown(nFlags, point);
}
BOOL CCrystalTextView::IsSelection()
{
return m_ptSelStart != m_ptSelEnd;
}
void CCrystalTextView::Copy()
{
if (m_ptSelStart == m_ptSelEnd)
return;
PrepareSelBounds();
CString text;
GetText(m_ptDrawSelStart, m_ptDrawSelEnd, text);
PutToClipboard(text);
}
BOOL CCrystalTextView::TextInClipboard()
{
return IsClipboardFormatAvailable(CF_TEXT);
}
BOOL CCrystalTextView::PutToClipboard(LPCTSTR pszText)
{
if (pszText == NULL || lstrlen(pszText) == 0)
return FALSE;
CWaitCursor wc;
BOOL bOK = FALSE;
if (OpenClipboard())
{
EmptyClipboard();
HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, lstrlen(pszText) + 1);
if (hData != NULL)
{
LPSTR pszData = (LPSTR) ::GlobalLock(hData);
USES_CONVERSION;
strcpy(pszData, T2A((LPTSTR) pszText));
GlobalUnlock(hData);
bOK = SetClipboardData(CF_TEXT, hData) != NULL;
}
CloseClipboard();
}
return bOK;
}
BOOL CCrystalTextView::GetFromClipboard(CString &text)
{
BOOL bSuccess = FALSE;
if (OpenClipboard())
{
HGLOBAL hData = GetClipboardData(CF_TEXT);
if (hData != NULL)
{
LPSTR pszData = (LPSTR) GlobalLock(hData);
if (pszData != NULL)
{
text = pszData;
GlobalUnlock(hData);
bSuccess = TRUE;
}
}
CloseClipboard();
}
return bSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -