📄 ccrystaleditview.cpp
字号:
if (point.x < rcClientRect.left + GetMarginWidth() + DRAG_BORDER_X)
{
HideDropIndicator();
ScrollLeft();
UpdateWindow();
ShowDropIndicator(point);
return;
}
if (point.x >= rcClientRect.right - DRAG_BORDER_X)
{
HideDropIndicator();
ScrollRight();
UpdateWindow();
ShowDropIndicator(point);
return;
}
}
BOOL CCrystalEditView::DoDropText(COleDataObject *pDataObject, const CPoint &ptClient)
{
HGLOBAL hData = pDataObject->GetGlobalData(CF_TEXT);
if (hData == NULL)
return FALSE;
CPoint ptDropPos = ClientToText(ptClient);
if (IsDraggingText() && IsInsideSelection(ptDropPos))
{
SetAnchor(ptDropPos);
SetSelection(ptDropPos, ptDropPos);
SetCursorPos(ptDropPos);
EnsureVisible(ptDropPos);
return FALSE;
}
LPSTR pszText = (LPSTR) ::GlobalLock(hData);
if (pszText == NULL)
return FALSE;
int x, y;
USES_CONVERSION;
m_pTextBuffer->InsertText(this, ptDropPos.y, ptDropPos.x, A2T(pszText), y, x, CE_ACTION_DRAGDROP); // [JRT]
CPoint ptCurPos(x, y);
ASSERT_VALIDTEXTPOS(ptCurPos);
SetAnchor(ptDropPos);
SetSelection(ptDropPos, ptCurPos);
SetCursorPos(ptCurPos);
EnsureVisible(ptCurPos);
::GlobalUnlock(hData);
return TRUE;
}
int CCrystalEditView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CCrystalTextView::OnCreate(lpCreateStruct) == -1)
return -1;
ASSERT(m_pDropTarget == NULL);
m_pDropTarget = new CEditDropTargetImpl(this);
if (! m_pDropTarget->Register(this))
{
TRACE0("Warning: Unable to register drop target for CCrystalEditView.\n");
delete m_pDropTarget;
m_pDropTarget = NULL;
}
return 0;
}
void CCrystalEditView::OnDestroy()
{
if (m_pDropTarget != NULL)
{
m_pDropTarget->Revoke();
delete m_pDropTarget;
m_pDropTarget = NULL;
}
CCrystalTextView::OnDestroy();
}
void CCrystalEditView::ShowDropIndicator(const CPoint &point)
{
if (! m_bDropPosVisible)
{
HideCursor();
m_ptSavedCaretPos = GetCursorPos();
m_bDropPosVisible = TRUE;
::CreateCaret(m_hWnd, (HBITMAP) 1, 2, GetLineHeight());
}
m_ptDropPos = ClientToText(point);
if (m_ptDropPos.x >= m_nOffsetChar)
{
SetCaretPos(TextToClient(m_ptDropPos));
ShowCaret();
}
else
{
HideCaret();
}
}
void CCrystalEditView::HideDropIndicator()
{
if (m_bDropPosVisible)
{
SetCursorPos(m_ptSavedCaretPos);
ShowCursor();
m_bDropPosVisible = FALSE;
}
}
DROPEFFECT CCrystalEditView::GetDropEffect()
{
return DROPEFFECT_COPY | DROPEFFECT_MOVE;
}
void CCrystalEditView::OnDropSource(DROPEFFECT de)
{
if (! IsDraggingText())
return;
ASSERT_VALIDTEXTPOS(m_ptDraggedTextBegin);
ASSERT_VALIDTEXTPOS(m_ptDraggedTextEnd);
if (de == DROPEFFECT_MOVE)
{
m_pTextBuffer->DeleteText(this, m_ptDraggedTextBegin.y, m_ptDraggedTextBegin.x, m_ptDraggedTextEnd.y,
m_ptDraggedTextEnd.x, CE_ACTION_DRAGDROP); // [JRT]
}
}
void CCrystalEditView::UpdateView(CCrystalTextView *pSource, CUpdateContext *pContext, DWORD dwFlags, int nLineIndex /*= -1*/)
{
CCrystalTextView::UpdateView(pSource, pContext, dwFlags, nLineIndex);
if (m_bSelectionPushed && pContext != NULL)
{
pContext->RecalcPoint(m_ptSavedSelStart);
pContext->RecalcPoint(m_ptSavedSelEnd);
ASSERT_VALIDTEXTPOS(m_ptSavedSelStart);
ASSERT_VALIDTEXTPOS(m_ptSavedSelEnd);
}
}
void CCrystalEditView::OnEditReplace()
{
if (! QueryEditable())
return;
CWinApp *pApp = AfxGetApp();
ASSERT(pApp != NULL);
CEditReplaceDlg dlg(this);
// Take search parameters from registry
dlg.m_bMatchCase = pApp->GetProfileInt(REG_REPLACE_SUBKEY, REG_MATCH_CASE, FALSE);
dlg.m_bWholeWord = pApp->GetProfileInt(REG_REPLACE_SUBKEY, REG_WHOLE_WORD, FALSE);
dlg.m_sText = pApp->GetProfileString(REG_REPLACE_SUBKEY, REG_FIND_WHAT, _T(""));
dlg.m_sNewText = pApp->GetProfileString(REG_REPLACE_SUBKEY, REG_REPLACE_WITH, _T(""));
if (IsSelection())
{
GetSelection(m_ptSavedSelStart, m_ptSavedSelEnd);
m_bSelectionPushed = TRUE;
dlg.m_nScope = 0; // Replace in current selection
dlg.m_ptCurrentPos = m_ptSavedSelStart;
dlg.m_bEnableScopeSelection = TRUE;
dlg.m_ptBlockBegin = m_ptSavedSelStart;
dlg.m_ptBlockEnd = m_ptSavedSelEnd;
}
else
{
dlg.m_nScope = 1; // Replace in whole text
dlg.m_ptCurrentPos = GetCursorPos();
dlg.m_bEnableScopeSelection = FALSE;
}
// Execute Replace dialog
m_bShowInactiveSelection = TRUE;
dlg.DoModal();
m_bShowInactiveSelection = FALSE;
// Restore selection
if (m_bSelectionPushed)
{
SetSelection(m_ptSavedSelStart, m_ptSavedSelEnd);
m_bSelectionPushed = FALSE;
}
// Save search parameters to registry
pApp->WriteProfileInt(REG_REPLACE_SUBKEY, REG_MATCH_CASE, dlg.m_bMatchCase);
pApp->WriteProfileInt(REG_REPLACE_SUBKEY, REG_WHOLE_WORD, dlg.m_bWholeWord);
pApp->WriteProfileString(REG_REPLACE_SUBKEY, REG_FIND_WHAT, dlg.m_sText);
pApp->WriteProfileString(REG_REPLACE_SUBKEY, REG_REPLACE_WITH, dlg.m_sNewText);
}
BOOL CCrystalEditView::ReplaceSelection(LPCTSTR pszNewText)
{
ASSERT(pszNewText != NULL);
if (! IsSelection())
return FALSE;
DeleteCurrentSelection();
CPoint ptCursorPos = GetCursorPos();
ASSERT_VALIDTEXTPOS(ptCursorPos);
int x, y;
m_pTextBuffer->InsertText(this, ptCursorPos.y, ptCursorPos.x, pszNewText, y, x, CE_ACTION_REPLACE); // [JRT]
CPoint ptEndOfBlock = CPoint(x, y);
ASSERT_VALIDTEXTPOS(ptCursorPos);
ASSERT_VALIDTEXTPOS(ptEndOfBlock);
SetAnchor(ptEndOfBlock);
SetSelection(ptCursorPos, ptEndOfBlock);
SetCursorPos(ptEndOfBlock);
EnsureVisible(ptEndOfBlock);
return TRUE;
}
void CCrystalEditView::OnUpdateEditUndo(CCmdUI* pCmdUI)
{
BOOL bCanUndo = m_pTextBuffer != NULL && m_pTextBuffer->CanUndo();
pCmdUI->Enable(bCanUndo);
// Since we need text only for menus...
if (pCmdUI->m_pMenu != NULL)
{
// Tune up 'resource handle'
HINSTANCE hOldResHandle = AfxGetResourceHandle();
AfxSetResourceHandle(GetResourceHandle());
CString menu;
if (bCanUndo)
{
// Format menu item text using the provided item description
CString desc;
m_pTextBuffer->GetUndoDescription(desc);
menu.Format(IDS_MENU_UNDO_FORMAT, desc);
}
else
{
// Just load default menu item text
menu.LoadString(IDS_MENU_UNDO_DEFAULT);
}
// Restore original handle
AfxSetResourceHandle(hOldResHandle);
// Set menu item text
pCmdUI->SetText(menu);
}
}
void CCrystalEditView::OnEditUndo()
{
if (m_pTextBuffer != NULL && m_pTextBuffer->CanUndo())
{
CPoint ptCursorPos;
if (m_pTextBuffer->Undo(ptCursorPos))
{
GoToLine(ptCursorPos.y, ptCursorPos.x);
}
}
}
// [JRT]
void CCrystalEditView::SetDisableBSAtSOL(BOOL bDisableBSAtSOL)
{
m_bDisableBSAtSOL = bDisableBSAtSOL;
}
void CCrystalEditView::OnEditRedo()
{
if (m_pTextBuffer != NULL && m_pTextBuffer->CanRedo())
{
CPoint ptCursorPos;
if (m_pTextBuffer->Redo(ptCursorPos))
{
GoToLine(ptCursorPos.y, ptCursorPos.x);
}
}
}
void CCrystalEditView::OnUpdateEditRedo(CCmdUI* pCmdUI)
{
BOOL bCanRedo = m_pTextBuffer != NULL && m_pTextBuffer->CanRedo();
pCmdUI->Enable(bCanRedo);
// Since we need text only for menus...
if (pCmdUI->m_pMenu != NULL)
{
// Tune up 'resource handle'
HINSTANCE hOldResHandle = AfxGetResourceHandle();
AfxSetResourceHandle(GetResourceHandle());
CString menu;
if (bCanRedo)
{
// Format menu item text using the provided item description
CString desc;
m_pTextBuffer->GetRedoDescription(desc);
menu.Format(IDS_MENU_REDO_FORMAT, desc);
}
else
{
// Just load default menu item text
menu.LoadString(IDS_MENU_REDO_DEFAULT);
}
// Restore original handle
AfxSetResourceHandle(hOldResHandle);
// Set menu item text
pCmdUI->SetText(menu);
}
}
void CCrystalEditView::OnEditOperation(int nAction, LPCTSTR pszText)
{
if (m_bAutoIndent)
{
// Analyse last action...
if (nAction == CE_ACTION_TYPING && _tcscmp(pszText, _T("\r\n")) == 0 && ! m_bOvrMode)
{
// Enter stroke!
CPoint ptCursorPos = GetCursorPos();
ASSERT(ptCursorPos.y > 0);
// Take indentation from the previos line
int nLength = m_pTextBuffer->GetLineLength(ptCursorPos.y - 1);
LPCTSTR pszLineChars = m_pTextBuffer->GetLineChars(ptCursorPos.y - 1);
int nPos = 0;
while (nPos < nLength && isspace(pszLineChars[nPos]))
nPos ++;
if (nPos > 0)
{
// Insert part of the previos line
TCHAR *pszInsertStr = (TCHAR *) _alloca(sizeof(TCHAR) * (nLength + 1));
_tcsncpy(pszInsertStr, pszLineChars, nPos);
pszInsertStr[nPos] = 0;
int x, y;
m_pTextBuffer->InsertText(NULL, ptCursorPos.y, ptCursorPos.x,
pszInsertStr, y, x, CE_ACTION_AUTOINDENT);
CPoint pt(x, y);
SetCursorPos(pt);
SetSelection(pt, pt);
SetAnchor(pt);
EnsureVisible(pt);
}
}
}
}
CCrystalEditView::LineChange CCrystalEditView::NotifyEnterPressed(CPoint ptCursor, CString& strLine)
{
return NOTIF_NO_CHANGES;
}
bool CCrystalEditView::NotifyEnterPressed()
{
CPoint ptCursorPos= GetCursorPos();
CString strLine= GetCurLine();
LineChange eChange= NotifyEnterPressed(ptCursorPos, strLine);
if (eChange == NOTIF_LINE_MODIFIED)
{
int nLine= ptCursorPos.y;
// modify current line
SetSelection(CPoint(0, nLine), CPoint(GetLineLength(nLine), nLine));
ReplaceSelection(strLine);
}
return eChange != NOTIF_LINE_ERROR;
}
void CCrystalEditView::OnEditDeleteToEOL()
{
if (!QueryEditable() || m_pTextBuffer == NULL)
return;
CPoint ptCursorPos = GetCursorPos();
ASSERT_VALIDTEXTPOS(ptCursorPos);
CPoint ptEnd= CPoint(GetLineLength(ptCursorPos.y), ptCursorPos.y);
ASSERT_VALIDTEXTPOS(ptEnd);
// if no text in the buffer, just leave
if (ptEnd == ptCursorPos)
return;
// delete it
m_pTextBuffer->DeleteText(this, ptCursorPos.y, ptCursorPos.x, ptEnd.y, ptEnd.x, CE_ACTION_DELETE);
}
void CCrystalEditView::OnUpdateEditDeleteToEol(CCmdUI* pCmdUI)
{
pCmdUI->Enable(QueryEditable());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -