XTPSyntaxEditCtrl.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:195k
源码类别:
对话框与窗口
开发平台:
Visual C++
- // XTPSyntaxEditCtrl.cpp
- //
- // This file is a part of the XTREME TOOLKIT PRO MFC class library.
- // (c)1998-2008 Codejock Software, All Rights Reserved.
- //
- // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
- // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
- // CONSENT OF CODEJOCK SOFTWARE.
- //
- // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
- // IN THE XTREME SYNTAX EDIT LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
- // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
- // SINGLE COMPUTER.
- //
- // CONTACT INFORMATION:
- // support@codejock.com
- // http://www.codejock.com
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Resource.h"
- // common includes
- #include "Common/XTPDrawHelpers.h"
- #include "Common/XTPImageManager.h"
- #include "Common/XTPNotifyConnection.h"
- #include "Common/XTPSmartPtrInternalT.h"
- #include "Common/XTPVC80Helpers.h"
- #include "Common/XTPVC50Helpers.h"
- #include "Common/XTPResourceManager.h"
- // syntax editor includes
- #include "XTPSyntaxEditDefines.h"
- #include "XTPSyntaxEditStruct.h"
- #include "XTPSyntaxEditUndoManager.h"
- #include "XTPSyntaxEditLineMarksManager.h"
- #include "XTPSyntaxEditLexPtrs.h"
- #include "XTPSyntaxEditLexClassSubObjT.h"
- #include "XTPSyntaxEditTextIterator.h"
- #include "XTPSyntaxEditSectionManager.h"
- #include "XTPSyntaxEditLexCfgFileReader.h"
- #include "XTPSyntaxEditLexClassSubObjDef.h"
- #include "XTPSyntaxEditLexClass.h"
- #include "XTPSyntaxEditLexParser.h"
- #include "XTPSyntaxEditLexColorFileReader.h"
- #include "XTPSyntaxEditBufferManager.h"
- #include "XTPSyntaxEditToolTipCtrl.h"
- #include "XTPSyntaxEditAutoCompleteWnd.h"
- #include "XTPSyntaxEditFindReplaceDlg.h"
- #include "XTPSyntaxEditCtrl.h"
- #include "XTPSyntaxEditDoc.h"
- #include "XTPSyntaxEditView.h"
- #include "XTPSyntaxEditPaintManager.h"
- #include <math.h>
- #define XTP_IDC_EDIT_DRAG_MOVE 0xE12D
- #define XTP_IDC_EDIT_DRAG_COPY 0xE12E
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- namespace XTPSyntaxEditLexAnalyser
- {
- CString DBG_TraceTB_StartEndCls(CXTPSyntaxEditLexTextBlock* pTB);
- }
- const int MARGIN_LENGTH = 20;
- const int NODES_WIDTH = 10;
- const int TEXT_LEFT_OFFSET = 4;
- const UINT TIMER_SELECTION_ID = 100;
- const UINT TIMER_SELECTION_TIME = 50;
- const UINT TIMER_REDRAW_WHEN_PARSE = 200;
- const UINT TIMER_AUTOSCROLL_ID = 110;
- const UINT TIMER_AUTOSCROLL_TIME = 120;
- enum XTPSyntaxEditFlags
- {
- xtpEditRedraw = 0x0001,
- xtpEditForceRedraw = 0x0002,
- xtpEditTextAsBlock = 0x0004,
- xtpEditDispCol = 0x0008,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CXTPSyntaxEditCtrl
- CXTPSyntaxEditCtrl::CXTPSyntaxEditCtrl()
- : m_bVertScrollBar(TRUE)
- , m_bHorzScrollBar(TRUE)
- , m_bSyntaxColor(TRUE)
- , m_bAutoIndent(TRUE)
- , m_bSelMargin(TRUE)
- , m_bLineNumbers(TRUE)
- , m_bWideCaret(TRUE)
- , m_bTabWithSpace(FALSE)
- , m_bDragging(FALSE)
- , m_bDroppable(FALSE)
- , m_bTokensLoaded(FALSE)
- , m_bIsScrollingEndRow(FALSE)
- , m_bRightButtonDrag(FALSE)
- , m_bIsSmartIndent(TRUE)
- , m_bEnableOleDrag(FALSE)
- , m_bEnableWhiteSpace(FALSE)
- , m_bCaseSensitive(TRUE)
- , m_bScrolling(FALSE)
- , m_bDrawNodes(TRUE)
- , m_bFocused(FALSE)
- , m_bIsActive(FALSE)
- , m_nTopCalculatedRow(-1)
- , m_nBottomCalculatedRow(-1)
- , m_nTopRow(1)
- , m_nCurrentCol(1)
- , m_nCurrentDocumentRow(1)
- , m_nDispCol(1)
- , m_nAutoIndentCol(0)
- , m_nMarginLength(MARGIN_LENGTH)
- , m_nLineNumLength(0)
- , m_nNodesWidth(NODES_WIDTH)
- , m_nEditbarLength(MARGIN_LENGTH + NODES_WIDTH)
- , m_nCollapsedTextRowsCount(0)
- , m_nWheelScroll(4)
- , m_nAverageLineLen(XTP_EDIT_AVELINELEN)
- , m_dwInsertPos(0)
- , m_dwLastRedrawTime(0)
- , m_ptPrevMouse(CPoint(0,0))
- , m_pBuffer(NULL)
- , m_pParentWnd(NULL)
- {
- m_Selection.m_pOwnerCtrl = this;
- m_bCreateScrollbarOnParent = TRUE;
- m_bVirtualSpace = FALSE;
- m_bReadOnly = FALSE;
- m_bHideCaret = FALSE;
- m_bEnableEditAccelerators = FALSE;
- #ifndef _UNICODE
- m_chPrevLeadByte = 0;
- #endif
- m_bIMEsupported = FALSE;
- m_pPaintManeger = new CXTPSyntaxEditPaintManager();
- GetRegValues();
- RegisterWindowClass();
- m_strDefaultCfgFilePath = GetModulePath() + XTP_EDIT_LEXPARSER_CFG_FILENAME;
- m_nHScrollMaxWidth = 0;
- m_bWndCreateInProgress = FALSE;
- m_dwAutoScrollDirection = 0;
- m_bDisableRedraw = FALSE;
- m_bDeleteOnFinalRelease = FALSE;
- // Set editor fonts.
- SetFontIndirect(NULL);
- m_pToolTip = new CXTPSyntaxEditToolTipCtrl();
- m_pAutoComplete = new CXTPSyntaxEditAutoCompleteWnd();
- }
- CXTPSyntaxEditCtrl::~CXTPSyntaxEditCtrl()
- {
- if (m_pBuffer && m_pBuffer->GetLexParser())
- m_pBuffer->GetLexParser()->CloseParseThread();
- m_Sink.UnadviseAll();
- m_arOnScreenSchCache.RemoveAll();
- CMDTARGET_RELEASE(m_pBuffer);
- DestroyWindow();
- CMDTARGET_RELEASE(m_pPaintManeger);
- SAFE_DELETE(m_pToolTip);
- SAFE_DELETE(m_pAutoComplete);
- }
- void CXTPSyntaxEditCtrl::OnFinalRelease()
- {
- CWnd::OnFinalRelease();
- if (m_bDeleteOnFinalRelease)
- {
- CCmdTarget::OnFinalRelease();
- }
- }
- IMPLEMENT_DYNAMIC(CXTPSyntaxEditCtrl, CWnd)
- BEGIN_MESSAGE_MAP(CXTPSyntaxEditCtrl, CWnd)
- //{{AFX_MSG_MAP(CXTPSyntaxEditCtrl)
- ON_WM_PAINT()
- ON_WM_SETCURSOR()
- ON_WM_HSCROLL()
- ON_WM_VSCROLL()
- ON_WM_KEYDOWN()
- ON_WM_SYSKEYDOWN()
- ON_WM_CHAR()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_WM_SETFOCUS()
- ON_WM_NCACTIVATE()
- ON_WM_MOUSEACTIVATE()
- ON_WM_KEYUP()
- ON_WM_LBUTTONDBLCLK()
- ON_WM_RBUTTONDOWN()
- ON_WM_RBUTTONUP()
- ON_WM_ERASEBKGND()
- ON_WM_CONTEXTMENU()
- ON_WM_TIMER()
- ON_WM_KILLFOCUS()
- ON_WM_SHOWWINDOW()
- ON_WM_ACTIVATE()
- ON_WM_MOUSEWHEEL()
- ON_WM_GETDLGCODE()
- ON_WM_DESTROY()
- ON_COMMAND(XTP_IDC_EDIT_DRAG_COPY, OnDragCopy)
- ON_COMMAND(XTP_IDC_EDIT_DRAG_MOVE, OnDragMove)
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_SETTEXT, OnSetText)
- ON_MESSAGE(WM_GETTEXT, OnGetText)
- ON_MESSAGE(WM_GETTEXTLENGTH, OnGetTextLen)
- ON_MESSAGE(WM_GETFONT, OnGetFont)
- ON_MESSAGE(WM_SETFONT, OnSetFont)
- ON_MESSAGE(WM_INPUTLANGCHANGE, OnInputLanguage)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CXTPSyntaxEditCtrl message handlers
- BOOL CXTPSyntaxEditCtrl::GetRegValues()
- {
- CWinApp* pWinApp = AfxGetApp();
- if (pWinApp != NULL)
- {
- m_bVertScrollBar = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_VSCROLLBAR, m_bVertScrollBar);
- m_bHorzScrollBar = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_HSCROLLBAR, m_bHorzScrollBar);
- m_bSyntaxColor = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_SYNTAXCOLOR, m_bSyntaxColor);
- m_bAutoIndent = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_AUTOINDENT, m_bAutoIndent);
- m_bSelMargin = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_SELMARGIN, m_bSelMargin);
- m_bLineNumbers = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_LINENUMBERS, m_bLineNumbers);
- m_bWideCaret = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_WIDECARET, m_bWideCaret);
- m_bTabWithSpace = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_TABWITHSPACE, m_bTabWithSpace);
- m_bVirtualSpace = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_VIRTUALSPACE, m_bVirtualSpace);
- m_bDrawNodes = pWinApp->GetProfileInt(XTP_EDIT_REG_SETTINGS, XTP_EDIT_REG_COLLAPSIBLENODES, m_bDrawNodes);
- return TRUE;
- }
- return FALSE;
- }
- BOOL CXTPSyntaxEditCtrl::SetValueInt(LPCTSTR lpszValue, int nNewValue, int& nRefValue, BOOL bUpdateReg)
- {
- nRefValue = nNewValue;
- if (bUpdateReg)
- {
- CWinApp* pWinApp = AfxGetApp();
- if (pWinApp != NULL)
- {
- if (pWinApp->WriteProfileInt(XTP_EDIT_REG_SETTINGS, lpszValue, nNewValue))
- return TRUE;
- }
- return FALSE;
- }
- return TRUE;
- }
- BOOL CXTPSyntaxEditCtrl::SetValueBool(LPCTSTR lpszValue, BOOL bNewValue, BOOL& bRefValue, BOOL bUpdateReg)
- {
- bRefValue = bNewValue;
- if (bUpdateReg)
- {
- CWinApp* pWinApp = AfxGetApp();
- if (pWinApp != NULL)
- {
- if (pWinApp->WriteProfileInt(XTP_EDIT_REG_SETTINGS, lpszValue, (int)bNewValue))
- return TRUE;
- }
- return FALSE;
- }
- return TRUE;
- }
- BOOL CXTPSyntaxEditCtrl::SetAutoIndent(BOOL bAutoIndent, BOOL bUpdateReg/*=FALSE*/)
- {
- if (!SetValueBool(XTP_EDIT_REG_AUTOINDENT, bAutoIndent, m_bAutoIndent, bUpdateReg))
- return FALSE;
- return TRUE;
- }
- void CXTPSyntaxEditCtrl::OnPaint()
- {
- DWORD dwStartTime = ::GetTickCount();
- CPaintDC dc(this); // device context for painting
- CXTPClientRect rcClient(this);
- //if ((!m_bChanged || m_bDisableRedraw) && m_bmpCache.GetSafeHandle() != 0)
- if (m_bDisableRedraw && m_bmpCache.GetSafeHandle() != 0)
- {
- CXTPCompatibleDC memDC(&dc, &m_bmpCache);
- dc.BitBlt(0, 0, rcClient.right, rcClient.bottom, &memDC, 0, 0, SRCCOPY);
- }
- else
- {
- CDC memDC;
- memDC.CreateCompatibleDC(&dc);
- if (!m_bmpCache.m_hObject)
- m_bmpCache.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
- CXTPBitmapDC autoBitmap(&memDC, &m_bmpCache);
- #ifdef _DEBUG
- memDC.FillSolidRect(rcClient, 0xFF);
- #endif
- Draw(&memDC, rcClient);
- if (!IsWindowEnabled())
- {
- XTPImageManager()->DisableBitmap(memDC, rcClient, XTP_EDIT_DISABLED_COLOR_LIGHT, XTP_EDIT_DISABLED_COLOR_DARK);
- }
- dc.BitBlt(0, 0, rcClient.right, rcClient.bottom, &memDC, 0, 0, SRCCOPY);
- //m_bChanged = FALSE;
- // Draw caret
- CSize szCaret;
- szCaret.cx = GetWideCaret() ? m_DrawTextProcessor.GetTextMetrics().tmAveCharWidth : 2;
- szCaret.cy = m_DrawTextProcessor.GetRowHeight();
- BOOL bVirtSpace = _IsVirtualSpaceActive() || m_nAutoIndentCol;
- int nCol = m_nDispCol - 1;
- BOOL bHideCaret = m_bHideCaret || !m_bFocused;
- CPoint ptCaret = m_DrawTextProcessor.SetCaretPos(this, szCaret, max(0, GetCurrentVisibleRow() - 1), nCol,
- bHideCaret, bVirtSpace);
- // IME Support
- if (m_bIMEsupported)
- {
- XTP_HIMC hIMC = m_ImmWrapper.ImmGetContext(m_hWnd);
- if (hIMC)
- {
- COMPOSITIONFORM compForm;
- ::ZeroMemory(&compForm, sizeof(compForm));
- compForm.dwStyle = CFS_POINT; //CFS_FORCE_POSITION
- compForm.ptCurrentPos = ptCaret;
- VERIFY(m_ImmWrapper.ImmSetCompositionWindow(hIMC, &compForm));
- VERIFY(m_ImmWrapper.ImmReleaseContext(m_hWnd, hIMC));
- }
- }
- }
- DWORD dwEndTime = ::GetTickCount();
- m_aveRedrawScreenTime.AddValue(::labs(dwEndTime - dwStartTime));
- //TRACE(_T("aveRedrawScreenTime = %d ms n"), (int)m_aveRedrawScreenTime.GetAverageValue(0));
- }
- AFX_STATIC void AFX_CDECL XTPSECollapsedBlockDeleteFn(void* pPtr)
- {
- XTP_EDIT_COLLAPSEDBLOCK* pBlock = (XTP_EDIT_COLLAPSEDBLOCK*)pPtr;
- SAFE_DELETE(pBlock);
- }
- void CXTPSyntaxEditCtrl::Draw(CDC *pDC, const CRect& rcRect)
- {
- ASSERT(pDC);
- if (!pDC)
- return;
- pDC->SetBkMode(TRANSPARENT);
- // calculate rects
- CRect rcBookMarks, rcLineNum, rcNodes, rcText;
- CalcEditRects(&rcBookMarks, &rcLineNum, &rcNodes, &rcText, &rcRect);
- // set text rect
- m_DrawTextProcessor.SetTextRect(rcText);
- m_DrawTextProcessor.SetTabSize(GetTabSize());
- // Set Row Height
- m_DrawTextProcessor.RecalcRowHeight(pDC, GetPaintManager()->GetFont());
- //--------------------------------------------------
- m_fcCollapsable.Update(m_nTopRow);
- m_fcRowColors.Update(m_nTopRow);
- //-------------------
- int nRowHeight = max(1, m_DrawTextProcessor.GetRowHeight());
- int nLinesCount = rcRect.Height() / nRowHeight + ((rcRect.Height() % nRowHeight) ? 1 : 0);
- int nSkipLines = 0;
- m_nCollapsedTextRowsCount = 0;
- for (int nLine = 0; nLine < nLinesCount;)
- {
- int nTextRow = m_nTopRow + nLine + nSkipLines;
- int nCollapsedRowsCount = 0;
- DWORD dwCollapcedType = ProcessCollapsedRowsBeroreDraw(nTextRow, nCollapsedRowsCount);
- nSkipLines += nCollapsedRowsCount;
- //if (nCollapsedRowsCount)
- // continue;
- // save current row colors to restore after drawing (because DrawLineMarks may change it).
- COLORREF clrFont = 0, crBack = 0;
- clrFont = GetRowColor(nTextRow);
- crBack = GetRowBkColor(nTextRow);
- // Draw Line attributes (bookmark, number, node)
- int nLineY0 = rcRect.top + m_DrawTextProcessor.GetRowHeight() * nLine;
- int nLineY1 = nLineY0 + m_DrawTextProcessor.GetRowHeight();
- if (GetSelMargin())
- {
- CRect rcLMark(rcBookMarks);
- rcLMark.top = nLineY0;
- rcLMark.bottom = nLineY1;
- GetPaintManager()->DrawLineMarks(pDC, rcLMark, nTextRow, this);
- }
- if (GetLineNumbers())
- {
- CRect rcLNum(rcLineNum);
- rcLNum.top = nLineY0;
- rcLNum.bottom = nLineY1;
- GetPaintManager()->DrawLineNumber(pDC, rcLNum, nTextRow, this);
- }
- if (m_bDrawNodes)
- {
- CRect rcNode, rcNodeFull;
- GetLineNodeRect(nLine, rcNode, &rcNodeFull);
- GetPaintManager()->DrawLineNode(pDC, rcNode, rcNodeFull, dwCollapcedType, nTextRow, this);
- }
- else
- {
- CRect rcGap(rcLineNum);
- rcGap.top = nLineY0;
- rcGap.bottom = nLineY1;
- rcGap.left = rcLineNum.right;
- rcGap.right = rcText.left;
- pDC->FillSolidRect(&rcGap, GetPaintManager()->GetBackColorEx(this));
- }
- // D R A W T E X T /////////////////////////////////////////////
- CRect rcTextLine(rcText);
- rcTextLine.top = nLineY0;
- rcTextLine.bottom = nLineY1;
- GetPaintManager()->DrawLineTextEx(pDC, rcTextLine, nTextRow, nLine, this);
- SetRowColor(nTextRow, clrFont);
- SetRowBkColor(nTextRow, crBack);
- nLine++;
- }
- //---------------------------------------------------------------------------
- GetPaintManager()->DrawCollapsedTextMarks(this, pDC);
- //---------------------------------------------------------------------------
- }
- int CXTPSyntaxEditCtrl::PrintPage(CDC *pDC, const CRect& rcRect, int nFlags) // returns printed rows count
- {
- int nPrintedRowsCount = 0;
- ASSERT(pDC);
- if (!pDC)
- return 0;
- BOOL bSelMargin_orig = m_bSelMargin;
- m_bSelMargin = FALSE;
- CalculateEditbarLength(pDC);
- pDC->SetBkMode(TRANSPARENT);
- // calculate rects
- CRect rcBookMarks, rcLineNum, rcNodes, rcText;
- CalcEditRects(&rcBookMarks, &rcLineNum, &rcNodes, &rcText, &rcRect);
- // set text rect
- m_DrawTextProcessor.SetTextRect(rcText);
- m_DrawTextProcessor.SetTabSize(GetTabSize());
- // Set Row Height
- m_DrawTextProcessor.RecalcRowHeight(pDC, GetPaintManager()->GetFont());
- //--------------------------------------------------
- m_fcCollapsable.Update(m_nTopRow);
- m_fcRowColors.Update(m_nTopRow);
- //-------------------
- int nRowHeight = max(1, m_DrawTextProcessor.GetRowHeight());
- int nLinesCount = rcRect.Height() / nRowHeight + ((rcRect.Height() % nRowHeight) ? 1 : 0);
- int nSkipLines = 0;
- m_nCollapsedTextRowsCount = 0;
- int nNextLineY = 0;
- for (int nLine = 0;
- nLine < nLinesCount && nNextLineY + nRowHeight < rcText.Height();
- nPrintedRowsCount++)
- {
- int nTextRow = m_nTopRow + nLine + nSkipLines;
- int nCollapsedRowsCount = 0;
- ProcessCollapsedRowsBeroreDraw(nTextRow, nCollapsedRowsCount);
- nSkipLines += nCollapsedRowsCount;
- // if (nCollapsedRowsCount)
- // continue;
- // save current row colors to restore after drawing (because DrawLineMarks may change it).
- COLORREF clrFont = 0, crBack = 0;
- //BOOL bSwitchColor = !pDC->IsPrinting();// && GetSelMargin(); to show breakpoints even if selection margin unchecked
- clrFont = GetRowColor(nTextRow);
- crBack = GetRowBkColor(nTextRow);
- // Draw Line attributes (bookmark, number, node)
- int nLineY0 = rcRect.top + nNextLineY; //m_DrawTextProcessor.GetRowHeight() * nLine;
- int nLineY1 = nLineY0 + m_DrawTextProcessor.GetRowHeight();
- CRect rcTextLine(rcText);
- rcTextLine.top = nLineY0;
- rcTextLine.bottom = nLineY1;
- // calculate is enough vertical space to print all line text
- int nLineHeight = GetPaintManager()->PrintLineTextEx(pDC, rcTextLine, nTextRow, nLine,
- this, nFlags);
- if (nLineHeight < 0)
- break;
- // if (GetSelMargin())
- // {
- // CRect rcLMark(rcBookMarks);
- // rcLMark.top = nLineY0;
- // rcLMark.bottom = nLineY1;
- // GetPaintManager()->DrawLineMarks(pDC, rcLMark, nTextRow, this);
- // }
- if (GetLineNumbers() && (nFlags & DT_CALCRECT) == 0)
- {
- CRect rcLNum(rcLineNum);
- rcLNum.top = nLineY0;
- rcLNum.bottom = nLineY1;
- GetPaintManager()->DrawLineNumber(pDC, rcLNum, nTextRow, this);
- }
- // if (m_bDrawNodes)
- // {
- // CRect rcNode, rcNodeFull;
- // GetLineNodeRect(nLine, rcNode, &rcNodeFull);
- // GetPaintManager()->DrawLineNode(pDC, rcNode, rcNodeFull, dwCollapcedType, this);
- // }
- // D R A W T E X T /////////////////////////////////////////////
- // if ((nFlags & DT_CALCRECT) == 0)
- // nLineHeight = GetPaintManager()->PrintLineTextEx(pDC, rcTextLine, nTextRow,
- // nLine, this, nFlags);
- nNextLineY += nLineHeight;
- SetRowColor(nTextRow, clrFont);
- SetRowBkColor(nTextRow, crBack);
- nLine++;
- }
- //---------------------------------------------------------------------------
- GetPaintManager()->DrawCollapsedTextMarks(this, pDC);
- //---------------------------------------------------------------------------
- m_bSelMargin = bSelMargin_orig;
- return nPrintedRowsCount;
- }
- void CXTPSyntaxEditCtrl::UpdateRowInfoInternally(int nTextRow)
- {
- CClientDC wndDC(this);
- CDC memDC;
- memDC.CreateCompatibleDC(&wndDC);
- int nDispRow = GetVisibleRow(nTextRow) - 1;
- if (nDispRow > m_DrawTextProcessor.GetRowsCount(TRUE) + 10)
- nDispRow = m_DrawTextProcessor.GetRowsCount(TRUE) + 10;
- CRect rcTextLine;
- CalcEditRects(NULL, NULL, NULL, &rcTextLine);
- int nCollapsedRowsCount = 0;
- ProcessCollapsedRowsBeroreDraw(nTextRow, nCollapsedRowsCount); // to update collapsed block info.
- GetPaintManager()->DrawLineTextEx(&memDC, rcTextLine, nTextRow, nDispRow, this);
- }
- DWORD CXTPSyntaxEditCtrl::ProcessCollapsedRowsBeroreDraw(int nTextRow, int& rnSkipRowsCount)
- {
- rnSkipRowsCount = 0;
- if (nTextRow > GetRowCount())
- return XTP_EDIT_ROWNODE_NOTHING;
- XTP_EDIT_LMPARAM LMCoParam;
- BOOL bIsRowCollapsed = HasRowMark(nTextRow, xtpEditLMT_Collapsed, &LMCoParam);
- // retrieve row nodes
- CXTPSyntaxEditRowsBlockArray arCoBlocks;
- DWORD dwType = XTP_EDIT_ROWNODE_NOTHING;
- GetCollapsableBlocksInfo(nTextRow, arCoBlocks);
- BOOL bCollapsedProcessed = FALSE;
- int nCount = (int)arCoBlocks.GetSize();
- //-----------------------------------------
- if (bIsRowCollapsed && nCount == 0)
- {
- GetLineMarksManager()->DeleteLineMark(nTextRow, xtpEditLMT_Collapsed);
- }
- //-----------------------------------------
- for (int i = 0; i < nCount; i++)
- {
- XTP_EDIT_ROWSBLOCK coBlk = arCoBlocks[i];
- if (coBlk.lcStart.nLine == nTextRow)
- {
- if (bIsRowCollapsed && !bCollapsedProcessed)
- {
- XTP_EDIT_COLLAPSEDBLOCK* pCoDrawBlk;
- pCoDrawBlk = (XTP_EDIT_COLLAPSEDBLOCK*)LMCoParam.GetPtr();
- if (!pCoDrawBlk)
- {
- pCoDrawBlk = new XTP_EDIT_COLLAPSEDBLOCK;
- LMCoParam.SetPtr(pCoDrawBlk, XTPSECollapsedBlockDeleteFn);
- }
- pCoDrawBlk->collBlock = coBlk;
- GetLineMarksManager()->SetLineMark(nTextRow,
- xtpEditLMT_Collapsed, &LMCoParam);
- m_arCollapsedTextRows.SetAtGrow(m_nCollapsedTextRowsCount, nTextRow);
- m_nCollapsedTextRowsCount++;
- bCollapsedProcessed = TRUE;
- rnSkipRowsCount = max(1, coBlk.lcEnd.nLine - coBlk.lcStart.nLine);
- dwType |= XTP_EDIT_ROWNODE_COLLAPSED;
- }
- else
- {
- dwType |= XTP_EDIT_ROWNODE_EXPANDED;
- }
- }
- BOOL bLastLineEnd = coBlk.lcEnd.nLine > nTextRow && nTextRow == GetRowCount();
- if (coBlk.lcEnd.nLine == nTextRow || bLastLineEnd)
- {
- dwType |= XTP_EDIT_ROWNODE_ENDMARK;
- }
- if (coBlk.lcStart.nLine < nTextRow)
- {
- dwType |= XTP_EDIT_ROWNODE_NODEUP;
- }
- if (coBlk.lcEnd.nLine > nTextRow && !bCollapsedProcessed && !bLastLineEnd)
- {
- dwType |= XTP_EDIT_ROWNODE_NODEDOWN;
- }
- // check whether to skip the row
- if ((coBlk.lcStart.nLine < nTextRow) &&
- (coBlk.lcEnd.nLine >= nTextRow))
- {
- if (HasRowMark(coBlk.lcStart.nLine, xtpEditLMT_Collapsed))
- {
- rnSkipRowsCount = max(1, coBlk.lcEnd.nLine - nTextRow);
- }
- }
- }
- return dwType;
- }
- CRect CXTPSyntaxEditCtrl::CalcEditRects(CRect* prcBookMarks, CRect* prcLineNum, CRect* prcNodes, CRect* prcText,
- const CRect* prcClient)
- {
- CRect rcClient(0, 0, 100, 100);
- if (prcClient)
- rcClient = *prcClient;
- else if (m_hWnd)
- GetClientRect(&rcClient);
- // calculate rects
- CRect rcBookMarks = rcClient;
- rcBookMarks.right = rcBookMarks.left + (GetSelMargin() ? m_nMarginLength : 0);
- CRect rcLineNum = rcClient;
- rcLineNum.left = rcBookMarks.right;
- rcLineNum.right = rcLineNum.left + (GetLineNumbers() ? m_nLineNumLength : 0);
- CRect rcNodes = rcClient;
- rcNodes.left = rcLineNum.right;
- rcNodes.right = rcNodes.left + (m_bDrawNodes ? m_nNodesWidth : 0);
- CRect rcText = rcClient;
- rcText.left += m_nEditbarLength;
- //--------------------------------------------
- if (prcBookMarks)
- *prcBookMarks = rcBookMarks;
- if (prcLineNum)
- *prcLineNum = rcLineNum;
- if (prcNodes)
- *prcNodes = rcNodes;
- if (prcText)
- *prcText = rcText;
- return rcClient;
- }
- BOOL CXTPSyntaxEditCtrl::RegisterWindowClass(HINSTANCE hInstance /*= NULL*/)
- {
- return XTPDrawHelpers()->RegisterWndClass(hInstance,
- XTP_EDIT_CLASSNAME_EDITCTRL, CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS);
- }
- BOOL CXTPSyntaxEditCtrl::PreCreateWindow(CREATESTRUCT& )
- {
- m_bWndCreateInProgress = TRUE;
- return TRUE;
- }
- void CXTPSyntaxEditCtrl::PreSubclassWindow()
- {
- if (!m_bWndCreateInProgress)
- {
- // process only for SubclassWindow call
- DWORD dwStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);
- m_bVertScrollBar = 0 != (dwStyle & WS_VSCROLL);
- m_bHorzScrollBar = 0 != (dwStyle & WS_HSCROLL);
- m_pParentWnd = GetParent();
- _InitEditControl();
- }
- }
- int CXTPSyntaxEditCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- TRACE(_T("CXTPSyntaxEditCtrl::OnCreate() - %d n"), ::GetTickCount());
- m_bWndCreateInProgress = FALSE;
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- if (!_InitEditControl())
- return -1;
- return 0;
- }
- void CXTPSyntaxEditCtrl::OnDestroy()
- {
- //TRACE(_T("CXTPSyntaxEditCtrl::OnDestroy() - %d nn"), ::GetTickCount());
- if (m_pToolTip->GetSafeHwnd())
- m_pToolTip->DestroyWindow();
- if (m_pAutoComplete->GetSafeHwnd())
- m_pAutoComplete->DestroyWindow();
- m_Sink.UnadviseAll();
- CWnd::OnDestroy();
- }
- BOOL CXTPSyntaxEditCtrl::Create(CWnd* pParentWnd, BOOL bHorzScroll, BOOL bVertScroll,
- CXTPSyntaxEditBufferManager *pBuffer,
- CCreateContext *lpCS, UINT nID)
- {
- if (pBuffer)
- {
- CMDTARGET_ADDREF(pBuffer);
- CMDTARGET_RELEASE(m_pBuffer);
- m_pBuffer = pBuffer;
- }
- //-------------------------------------------
- m_bVertScrollBar = bVertScroll;
- m_bHorzScrollBar = bHorzScroll;
- DWORD dwStyle = (WS_CHILD | WS_VISIBLE);
- if (m_bVertScrollBar && !IsCreateScrollbarOnParent())
- dwStyle |= WS_VSCROLL;
- if (m_bHorzScrollBar && !IsCreateScrollbarOnParent())
- dwStyle |= WS_HSCROLL;
- //-------------------------------------------
- ASSERT_VALID(pParentWnd); // must be valid.
- m_pParentWnd = pParentWnd;
- BOOL bCreate = CWnd::CreateEx(WS_EX_ACCEPTFILES, XTP_EDIT_CLASSNAME_EDITCTRL, NULL,
- dwStyle, CRect(0, 0, 100, 100), m_pParentWnd, nID, (LPVOID)lpCS);
- if (!bCreate)
- {
- TRACE0("Failed to create edit window.n");
- }
- return bCreate;
- }
- BOOL CXTPSyntaxEditCtrl::_InitEditControl()
- {
- // create tip window
- VERIFY(m_pToolTip->Create(this));
- // Create AutoComplete window.
- VERIFY(m_pAutoComplete->Create(this));
- CRect rcText(0, 0, 3000, 2000);
- m_DrawTextProcessor.SetTextRect(rcText);
- m_DrawTextProcessor.SetTabSize(GetTabSize());
- // create data manager if need
- if (!m_pBuffer)
- m_pBuffer = new CXTPSyntaxEditBufferManager;
- if (!m_pBuffer)
- return FALSE;
- if (GetConfigFile().IsEmpty())
- {
- CString csCfgFilePath = GetDefaultCfgFilePath();
- if (!SetConfigFile(csCfgFilePath))
- {
- TRACE1("n*** Could not locate config file '%s'.nn", (LPCTSTR)csCfgFilePath);
- }
- }
- _UpdateIMEStatus();
- SetCurCaretPos(1, 1, FALSE, FALSE);
- // Advise to events
- m_Sink.UnadviseAll();
- CXTPNotifyConnection* ptrConnParser = m_pBuffer->GetLexParser()->GetConnection();
- ASSERT(ptrConnParser);
- if (ptrConnParser)
- {
- m_Sink.Advise(ptrConnParser, xtpEditOnParserStarted, &CXTPSyntaxEditCtrl::OnParseEvent);
- m_Sink.Advise(ptrConnParser, xtpEditOnTextBlockParsed, &CXTPSyntaxEditCtrl::OnParseEvent);
- m_Sink.Advise(ptrConnParser, xtpEditOnParserEnded, &CXTPSyntaxEditCtrl::OnParseEvent);
- }
- CXTPNotifyConnection* ptrConnBufMan = m_pBuffer->GetConnection();
- ASSERT(ptrConnBufMan);
- if (ptrConnBufMan)
- {
- m_Sink.Advise(ptrConnBufMan, xtpEditClassSchWasChanged, &CXTPSyntaxEditCtrl::OnLexCfgWasChanged);
- m_Sink.Advise(ptrConnBufMan, xtpEditThemeWasChanged, &CXTPSyntaxEditCtrl::OnLexCfgWasChanged);
- m_Sink.Advise(ptrConnBufMan, xtpEditAllConfigWasChanged, &CXTPSyntaxEditCtrl::OnLexCfgWasChanged);
- }
- return TRUE;
- }
- UINT CXTPSyntaxEditCtrl::OnGetDlgCode()
- {
- return DLGC_WANTTAB | DLGC_WANTARROWS | DLGC_WANTCHARS | DLGC_WANTALLKEYS;
- }
- BOOL CXTPSyntaxEditCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- UNREFERENCED_PARAMETER(pWnd);
- UNREFERENCED_PARAMETER(nHitTest);
- UNREFERENCED_PARAMETER(message);
- CPoint pt;
- ::GetCursorPos(&pt);
- ScreenToClient(&pt);
- CXTPClientRect rcClient(this);
- CRect rcText(rcClient);
- rcText.left += m_nEditbarLength;
- CRect rcBookmark(0, 0, GetSelMargin() ? m_nMarginLength : 0, rcClient.Height());
- CRect rcLineNumAndNodes(rcBookmark.right, 0, m_nEditbarLength, rcClient.Height());
- CXTPEmptyRect rcNode;
- int nRow = 0, nCol = 0, nDispRow = 0;
- RowColFromPoint(pt, &nRow, &nCol, &nDispRow);
- if (m_bDrawNodes)
- {
- // calculate node icon rect
- DWORD dwType = XTP_EDIT_ROWNODE_NOTHING;
- if (GetRowNodes(nRow, dwType) && (dwType & (XTP_EDIT_ROWNODE_COLLAPSED | XTP_EDIT_ROWNODE_EXPANDED)) )
- {
- GetLineNodeRect(nDispRow-1, rcNode);
- }
- }
- if (m_bDragging)
- {
- if (rcClient.PtInRect(pt))
- {
- if ((::GetKeyState(VK_CONTROL) & KF_UP) == 0)
- {
- SetCursor(GetPaintManager()->GetCurMove());
- return TRUE;
- }
- else
- {
- SetCursor(GetPaintManager()->GetCurCopy());
- return TRUE;
- }
- }
- else
- {
- SetCursor(GetPaintManager()->GetCurNO());
- return TRUE;
- }
- }
- else if (rcText.PtInRect(pt))
- {
- if (m_Selection.IsInSel_str(nRow, nCol-1) && !m_Selection.bSelectingRunning
- || m_bRightButtonDrag)
- {
- SetCursor(GetPaintManager()->GetCurArrow());
- return TRUE;
- }
- else
- {
- if (!GetBlockFromPt(pt))
- {
- SetCursor(GetPaintManager()->GetCurIBeam());
- return TRUE;
- }
- }
- }
- else if (rcBookmark.PtInRect(pt))
- {
- SetCursor(GetPaintManager()->GetCurArrow());
- return TRUE;
- }
- else if (rcNode.PtInRect(pt))
- {
- SetCursor(GetPaintManager()->GetCurArrow());
- return TRUE;
- }
- else if (rcLineNumAndNodes.PtInRect(pt))
- {
- SetCursor(GetPaintManager()->GetCurLine());
- return TRUE;
- }
- SetCursor(GetPaintManager()->GetCurArrow());
- return TRUE;
- }
- LRESULT CXTPSyntaxEditCtrl::OnSetText(WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(wParam);
- LPCTSTR szText = (LPCTSTR)lParam;
- SetText(szText);
- return (LRESULT)TRUE;
- }
- LRESULT CXTPSyntaxEditCtrl::OnGetTextLen(WPARAM, LPARAM)
- {
- if (!GetEditBuffer())
- return 0;
- int nTextSize = 0;
- int nRowsCount = GetRowCount();
- for (int i = 1; i <= nRowsCount; i++)
- {
- nTextSize += GetEditBuffer()->GetLineTextLength(i, i < nRowsCount);
- }
- return (LRESULT)nTextSize;
- }
- LRESULT CXTPSyntaxEditCtrl::OnGetText(WPARAM wBufferSize, LPARAM lpBuffer)
- {
- if (wBufferSize == 0)
- return OnGetTextLen(0, 0);
- CMemFile memFile;
- if (!GetText(memFile, (int)wBufferSize))
- return 0;
- void* pTextData = NULL;
- void* pTextEnd = NULL;
- UINT uBufferB = memFile.GetBufferPtr(CFile::bufferRead, (UINT)wBufferSize, &pTextData, &pTextEnd);
- LPTSTR pDest = (LPTSTR)lpBuffer;
- STRNCPY_S(pDest, wBufferSize, (LPCTSTR)pTextData, wBufferSize);
- pDest[wBufferSize-1] = _T(' ');
- int nTextSize = (int)uBufferB/sizeof(TCHAR) - sizeof(TCHAR);
- nTextSize = max(0, nTextSize);
- nTextSize = min(nTextSize, (int)wBufferSize);
- //ASSERT(nTextSize == (int)_tcslen(pDest));
- return (LRESULT)nTextSize;
- }
- CString CXTPSyntaxEditCtrl::GetText(int nMaxLen)
- {
- CMemFile memFile;
- if (!GetText(memFile, nMaxLen))
- return _T("");
- void* pTextData = NULL;
- void* pTextEnd = NULL;
- memFile.GetBufferPtr(CFile::bufferRead, (UINT)nMaxLen, &pTextData, &pTextEnd);
- if (pTextEnd)
- {
- TCHAR* pEnd = (TCHAR*)pTextEnd;
- *(pEnd - 1) = _T(' ');
- }
- return CString((LPCTSTR)pTextData);
- }
- BOOL CXTPSyntaxEditCtrl::GetText(CMemFile& memFile, int nMaxLen)
- {
- if (!GetEditBuffer())
- return FALSE;
- CArchive ar(&memFile, CArchive::store);
- BOOL bUnicode = (sizeof(TCHAR) == 2);
- if (nMaxLen > 0)
- nMaxLen = nMaxLen * sizeof(TCHAR);
- GetEditBuffer()->SerializeEx(ar, bUnicode, FALSE, (UINT)-1, NULL, nMaxLen);
- ar << (TCHAR)0;
- ar.Close();
- memFile.SeekToBegin();
- return TRUE;
- }
- void CXTPSyntaxEditCtrl::SetText(LPCTSTR pcszText)
- {
- if (!GetEditBuffer() || !pcszText)
- {
- ASSERT(FALSE);
- return;
- }
- int nStrLenB = (int)_tcslen(pcszText) * sizeof(TCHAR);
- CMemFile memFile((BYTE*)pcszText, nStrLenB);
- memFile.SeekToBegin();
- CArchive ar(&memFile, CArchive::load);
- BOOL bUnicode = (sizeof(TCHAR) == 2);
- GetEditBuffer()->SerializeEx(ar, bUnicode);
- //---------------------------------------
- RefreshColors();
- RecalcScrollBars();
- Invalidate(FALSE);
- }
- void CXTPSyntaxEditCtrl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- UNREFERENCED_PARAMETER(pScrollBar); UNREFERENCED_PARAMETER(nPos);
- SCROLLINFO info;
- ZeroMemory(&info, sizeof(SCROLLINFO));
- info.cbSize = sizeof(SCROLLINFO);
- info.fMask = SIF_ALL;
- GetScrollInfo(SB_HORZ, &info);
- int nStep = m_DrawTextProcessor.GetTextMetrics().tmAveCharWidth;
- int nCurrPos = m_DrawTextProcessor.GetScrollXOffset(); //GetScrollPos(SB_HORZ);
- switch(nSBCode)
- {
- case SB_LINELEFT:
- nCurrPos -= nStep;
- break;
- case SB_LINERIGHT:
- nCurrPos += nStep;
- break;
- case SB_PAGELEFT:
- nCurrPos -= info.nPage;
- break;
- case SB_PAGERIGHT:
- nCurrPos += info.nPage;
- break;
- case SB_LEFT:
- nCurrPos = info.nMin;
- break;
- case SB_RIGHT:
- nCurrPos = info.nMax;
- case SB_ENDSCROLL:
- return;
- case SB_THUMBPOSITION:
- case SB_THUMBTRACK:
- {
- nCurrPos = nPos;
- int nDelta = ((nCurrPos % nStep) >= nStep/2 ? 1 : 0);
- nCurrPos = (nCurrPos / nStep + nDelta) * nStep;
- ASSERT(nCurrPos <= info.nMax);
- }
- break;
- default:
- break;
- }
- nCurrPos = max(0, nCurrPos);
- SetScrollPos(SB_HORZ, nCurrPos);
- m_DrawTextProcessor.SetScrollXOffset(nCurrPos);
- if (m_bFocused)
- {
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE, FALSE);
- }
- Invalidate(FALSE);
- UpdateWindow();
- }
- void CXTPSyntaxEditCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- UNREFERENCED_PARAMETER(nPos);
- UNREFERENCED_PARAMETER(pScrollBar);
- SCROLLINFO info;
- ZeroMemory(&info, sizeof(SCROLLINFO));
- info.cbSize = sizeof(SCROLLINFO);
- info.fMask = SIF_ALL;
- GetScrollInfo(SB_VERT, &info);
- int iMin = 0, iMax = 0;
- GetScrollRange(SB_VERT, &iMin, &iMax);
- int iPos = GetScrollPos(SB_VERT);
- BOOL bChanged = FALSE;
- switch(nSBCode) {
- case SB_LINEDOWN:
- bChanged = ShiftCurrentVisibleRowDown(1);
- if (bChanged)
- SetScrollPos(SB_VERT, iPos + 1);
- break;
- case SB_LINEUP:
- bChanged = ShiftCurrentVisibleRowUp(1);
- if (bChanged)
- SetScrollPos(SB_VERT, iPos - 1);
- break;
- case SB_PAGEUP:
- bChanged = ShiftCurrentVisibleRowUp(info.nPage);
- if (bChanged)
- SetScrollPos(SB_VERT, max(1, iPos - info.nPage));
- break;
- case SB_PAGEDOWN:
- bChanged = ShiftCurrentVisibleRowDown(info.nPage);
- if (bChanged)
- SetScrollPos(SB_VERT, (iPos + info.nPage));
- break;
- case SB_TOP:
- SetScrollPos(SB_VERT, iMin);
- SetCurrentDocumentRow(iMin);
- m_nCurrentCol = m_nDispCol = 1;
- bChanged = TRUE;
- break;
- case SB_BOTTOM:
- SetScrollPos(SB_VERT, iMax);
- SetCurrentDocumentRow(iMax);
- m_nCurrentCol = m_nDispCol = 1;
- bChanged = TRUE;
- break;
- case SB_THUMBTRACK:
- {
- SCROLLINFO si;
- ZeroMemory(&si, sizeof(SCROLLINFO));
- si.cbSize = sizeof(SCROLLINFO);
- si.fMask = SIF_TRACKPOS;
- if (GetScrollInfo(SB_VERT, &si))
- {
- int nShift = si.nTrackPos - GetVisibleRowsCount(m_nTopRow);
- if (nShift > 0)
- {
- bChanged = ShiftCurrentVisibleRowDown(nShift);
- }
- else if (nShift < 0)
- {
- bChanged = ShiftCurrentVisibleRowUp(-nShift);
- }
- SetScrollPos(SB_VERT, si.nTrackPos);
- }
- }
- break;
- case SB_ENDSCROLL:
- return;
- default:
- break;
- }
- int nTopDocRow = GetDocumentRow(1);
- int nBottomDocRow = GetDocumentRow(GetRowPerPage());
- int nCurDocRow = GetCurrentDocumentRow();
- if (nCurDocRow >= nTopDocRow && nCurDocRow <= nBottomDocRow)
- {
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE, FALSE);
- }
- if (bChanged)
- {
- Invalidate(FALSE);
- UpdateWindow();
- }
- RecalcScrollBars();
- }
- void CXTPSyntaxEditCtrl::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CXTPSmartPtrInternalT<CCmdTarget> ptrThisLock(this, TRUE);
- #ifdef XTP_SYNTAXEDIT_SITENOTIFY_KEY
- if (!XTP_SYNTAXEDIT_SITENOTIFY_KEY(this, TRUE, nChar))
- return;
- #endif
- if (nChar == 0)
- {
- return;
- }
- CWnd::OnSysKeyDown(nChar, nRepCnt, nFlags);
- }
- void CXTPSyntaxEditCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CXTPSmartPtrInternalT<CCmdTarget> ptrThisLock(this, TRUE);
- #ifdef XTP_SYNTAXEDIT_SITENOTIFY_KEY
- if (!XTP_SYNTAXEDIT_SITENOTIFY_KEY(this, TRUE, nChar))
- return;
- #endif
- if (nChar == 0)
- {
- return;
- }
- m_pToolTip->Hide();
- if (m_bDragging || m_bRightButtonDrag || m_Selection.bSelectingRunning /* || m_bSelectionStarted*/)
- {
- OnSetCursor(0,0,0);
- return;
- }
- BOOL bCtrlKey = (::GetKeyState(VK_CONTROL) & KF_UP) != 0;
- BOOL bShiftKey = (::GetKeyState(VK_SHIFT) & KF_UP) != 0;
- BOOL bUpdateAll = FALSE;
- int nTopRow_prev = m_nTopRow;
- int nDispCol_prev = m_nDispCol;
- int nTextRow_prev = GetCurrentDocumentRow();
- int nRowCount_prev = GetRowCount();
- CXTPSyntaxEditSelection selData_prev = m_Selection;
- switch(nChar)
- {
- case VK_UP:
- m_nAutoIndentCol = 0;
- if (GetCurrentDocumentRow() < 1)
- {
- // Do nothing
- }
- else if (bCtrlKey)
- {
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol) ;
- ShiftCurrentVisibleRowUp(1, TRUE);
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE);
- }
- else if (GetCurrentDocumentRow() >= 1)
- {
- if (!m_Selection.IsSelExist())
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (!bShiftKey)
- {
- m_Selection.Reset_disp(m_Selection.GetNormalStart_disp().nLine,
- m_nDispCol); //m_Selection.GetNormalEnd_disp().nCol);
- SetCurCaretPos(m_Selection.GetNormalStart_disp().nLine,
- m_Selection.GetNormalEnd_disp().nCol, FALSE);
- }
- MoveCurrentVisibleRowUp(1);
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE);
- if (bShiftKey)
- m_Selection.SetEnd_disp(GetCurrentDocumentRow(), m_nDispCol);
- else
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- }
- break;
- case VK_DOWN:
- m_nAutoIndentCol = 0;
- if (bCtrlKey)
- {
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol) ;
- ShiftCurrentVisibleRowDown(1, TRUE);
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE);
- }
- else if (GetCurrentDocumentRow() <= GetRowCount())
- {
- if (!m_Selection.IsSelExist())
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (!bShiftKey)
- {
- m_Selection.Reset_disp(m_Selection.GetNormalEnd_disp().nLine,
- m_nDispCol); //m_Selection.GetNormalEnd_disp().nCol);
- SetCurCaretPos(m_Selection.GetNormalStart_str().nLine,
- m_Selection.GetNormalEnd_disp().nCol, FALSE, TRUE);
- }
- MoveCurrentVisibleRowDown(1);
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE/*, FALSE*/);
- if (bShiftKey)
- m_Selection.SetEnd_disp(GetCurrentDocumentRow(), m_nDispCol);
- else
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- }
- break;
- case VK_LEFT:
- // if (m_nAutoIndentCol > 0)
- // {
- m_nAutoIndentCol = 0;
- // m_nCurrentCol = m_nDispCol = 1;
- // break;
- // }
- if (!m_Selection.IsSelExist())
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (!bShiftKey && m_Selection.IsSelExist())
- {
- // move cursor to the selection begin
- m_Selection.Reset_disp(m_Selection.GetNormalStart_disp().nLine,
- m_Selection.GetNormalStart_disp().nCol);
- SetCurCaretPos(m_Selection.GetNormalStart_disp().nLine,
- m_Selection.GetNormalStart_disp().nCol, FALSE/*, FALSE*/);
- break;
- }
- if (!bCtrlKey)
- {
- int nCurDocRow = GetCurrentDocumentRow();
- if ((nCurDocRow > 1 && m_nCurrentCol >= 1) ||
- (nCurDocRow == 1 && m_nCurrentCol > 1))
- {
- m_nCurrentCol--;
- }
- if (m_nCurrentCol < 1 && nCurDocRow > 1)
- {
- if (_IsVirtualSpaceActive())
- m_nCurrentCol = 1;
- else
- MoveCurrentVisibleRowUp(1);
- }
- LPCTSTR szText = GetLineText(GetCurrentDocumentRow());
- if (m_nCurrentCol < 1)
- m_nCurrentCol = (int)_tcsclen(szText) + 1;
- m_nDispCol = CalcDispCol(szText, m_nCurrentCol);
- }
- else
- {
- // This will modify Current Row, m_nCurrentCol, m_nDispCol
- FindWord(XTP_EDIT_FINDWORD_PREV);
- }
- if (bShiftKey)
- m_Selection.SetEnd_disp(GetCurrentDocumentRow(), m_nDispCol);
- else
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- break;
- case VK_RIGHT:
- m_nAutoIndentCol = 0;
- if (!m_Selection.IsSelExist())
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (!bShiftKey && m_Selection.IsSelExist())
- {
- // move cursor to the end of the selection
- m_Selection.Reset_disp(m_Selection.GetNormalEnd_disp().nLine,
- m_Selection.GetNormalEnd_disp().nCol);
- SetCurCaretPos(m_Selection.GetNormalStart_str().nLine,
- m_Selection.GetNormalEnd_disp().nCol, FALSE/*, FALSE*/);
- break;
- }
- if (!bCtrlKey)
- {
- int nCurDocRow = GetCurrentDocumentRow();
- LPCTSTR szText = GetLineText(nCurDocRow);
- const int nTextLen = (int)_tcsclen(szText);
- if (m_nCurrentCol > nTextLen && (nCurDocRow < GetRowCount()) && !_IsVirtualSpaceActive())
- {
- MoveCurrentVisibleRowDown(1);
- m_nCurrentCol = 1;
- }
- else if (m_nCurrentCol <= nTextLen || _IsVirtualSpaceActive())
- {
- m_nCurrentCol++;
- }
- m_nDispCol = CalcDispCol(szText, m_nCurrentCol);
- }
- else
- {
- FindWord(XTP_EDIT_FINDWORD_NEXT);
- }
- if (bShiftKey)
- m_Selection.SetEnd_disp(GetCurrentDocumentRow(), m_nDispCol);
- else
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- break;
- case VK_HOME:
- m_nAutoIndentCol = 0;
- if (!m_Selection.IsSelExist())
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (!bShiftKey && m_Selection.IsSelExist())
- {
- // move cursor to the selection begin
- m_Selection.Reset_disp(m_Selection.GetNormalStart_disp().nLine,
- m_Selection.GetNormalEnd_disp().nCol);
- SetCurCaretPos(m_Selection.GetNormalStart_disp().nLine,
- m_Selection.GetNormalEnd_disp().nCol, FALSE/*, FALSE*/);
- }
- if (bCtrlKey)
- {
- SetCurrentDocumentRow(1);
- EnsureVisibleRow(1);
- SetCurCaretPos(1, 1, FALSE/*, FALSE*/);
- }
- else
- {
- LPCTSTR szText = GetLineText(GetCurrentDocumentRow());
- // find number of tabs and spaces from the left
- int iCol = 1;
- LPCTSTR szPtr = szText;
- while (szPtr)
- {
- if (*szPtr == _T('t') || *szPtr == _T(' '))
- iCol++;
- else
- break;
- szPtr = _tcsinc(szPtr);
- }
- if (m_nCurrentCol == iCol)
- {
- //m_nCurrentCol = 1;
- //m_nDispCol = 1;
- SetCurCaretPos(GetCurrentDocumentRow(), 1, FALSE/*, FALSE*/);
- }
- else
- {
- m_nDispCol = CalcDispCol(szText, iCol);
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE/*, FALSE*/);
- }
- }
- if (bShiftKey)
- m_Selection.SetEnd_disp(GetCurrentDocumentRow(), m_nDispCol);
- else
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- break;
- case VK_END:
- {
- m_nAutoIndentCol = 0;
- if (!m_Selection.IsSelExist())
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (!bShiftKey && m_Selection.IsSelExist())
- {
- // move cursor to the end of the selection
- m_Selection.Reset_disp(m_Selection.GetNormalEnd_disp().nLine,
- m_Selection.GetNormalEnd_disp().nCol);
- SetCurCaretPos(m_Selection.GetNormalStart_str().nLine,
- m_Selection.GetNormalEnd_disp().nCol, FALSE/*, FALSE*/);
- }
- if (bCtrlKey)
- {
- SetCurrentDocumentRow(GetRowCount());
- EnsureVisibleRow(GetRowCount());
- }
- LPCTSTR szText = GetLineText(GetCurrentDocumentRow());
- int nLenC = (int)_tcsclen(szText);
- SetCurCaretPos(GetCurrentDocumentRow(), CalcDispCol(szText, nLenC + 1),
- FALSE/*, FALSE*/);
- }
- if (bShiftKey)
- m_Selection.SetEnd_disp(GetCurrentDocumentRow(), m_nDispCol);
- else
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- break;
- case VK_PRIOR:
- m_nAutoIndentCol = 0;
- {
- if (!m_Selection.IsSelExist())
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (!bShiftKey && m_Selection.IsSelExist())
- {
- // move cursor to the selection begin
- m_Selection.Reset_disp(m_Selection.GetNormalStart_disp().nLine,
- m_Selection.GetNormalEnd_disp().nCol);
- SetCurCaretPos(m_Selection.GetNormalStart_disp().nLine,
- m_Selection.GetNormalEnd_disp().nCol, FALSE/*, FALSE*/);
- }
- int iRowPerPage = GetRowPerPage();
- if ((m_nTopRow - iRowPerPage) < 1)
- {
- SetCurrentDocumentRow(1);
- EnsureVisibleRow(1);
- }
- else
- {
- MoveCurrentVisibleRowUp(iRowPerPage);
- }
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE/*, FALSE*/);
- if (bShiftKey)
- m_Selection.SetEnd_disp(GetCurrentDocumentRow(), m_nDispCol);
- else
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- }
- break;
- case VK_NEXT:
- m_nAutoIndentCol = 0;
- {
- if (!m_Selection.IsSelExist())
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (!bShiftKey && m_Selection.IsSelExist())
- {
- // move cursor to the end of the selection
- m_Selection.Reset_disp(m_Selection.GetNormalEnd_disp().nLine,
- m_Selection.GetNormalEnd_disp().nCol);
- SetCurCaretPos(m_Selection.GetNormalStart_str().nLine,
- m_Selection.GetNormalEnd_disp().nCol, FALSE/*, FALSE*/);
- }
- int iRowPerPage = GetRowPerPage();
- if ((m_nTopRow + iRowPerPage) > GetRowCount())
- {
- SetCurrentDocumentRow(GetRowCount());
- EnsureVisibleRow(GetRowCount());
- }
- else
- {
- MoveCurrentVisibleRowDown(iRowPerPage);
- }
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE/*, FALSE*/);
- if (bShiftKey)
- m_Selection.SetEnd_disp(GetCurrentDocumentRow(), m_nDispCol);
- else
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- }
- break;
- case VK_DELETE:
- {
- BOOL bDeleted = TRUE;
- if (m_Selection.IsSelExist() && !bCtrlKey)
- {
- bDeleted = DeleteSelection();
- }
- else if (!bCtrlKey)
- {
- if (GetAutoIndent() && m_nAutoIndentCol > 0)
- {
- CString strInsertText(
- CString(_T('t'), m_nInsertTabCount) +
- CString(_T(' '), m_nInsertSpaceCount));
- int iNewDispCol = (m_nInsertTabCount * GetTabSize()) + m_nInsertSpaceCount + 1;
- int iNewCol = m_nInsertTabCount + m_nInsertSpaceCount + 1;
- //**----------------------
- OnBeforeEditChanged(GetCurrentDocumentRow(), 1);
- m_pBuffer->InsertText(strInsertText, GetCurrentDocumentRow(), 1);
- OnEditChanged(GetCurrentDocumentRow(), 1, GetCurrentDocumentRow(), iNewCol, xtpEditActInsert);
- //**----------------------
- m_nCurrentCol = iNewCol;
- m_nDispCol = iNewDispCol;
- m_nAutoIndentCol = 0;
- }
- bDeleted = DeleteChar(GetCurrentDocumentRow(), m_nCurrentCol, xtpEditDelPosAfter);
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- }
- else
- {
- BOOL bSelectionExist = m_Selection.IsSelExist();
- int nDelFlags = 0;
- if (bSelectionExist)
- {
- nDelFlags |= xtpEditForceRedraw;
- DeleteSelection();
- }
- int iStartRow = GetCurrentDocumentRow();
- int iStartCol = m_nCurrentCol;
- FindWord(XTP_EDIT_FINDWORD_NEXT);
- int iEndRow = GetCurrentDocumentRow();
- int iEndCol = m_nCurrentCol;
- DeleteBuffer(iStartRow, iStartCol, iEndRow, iEndCol, nDelFlags);
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (bSelectionExist)
- m_pBuffer->GetUndoRedoManager()->ChainLastCommand();
- }
- bUpdateAll = TRUE;
- m_nAutoIndentCol = 0;
- }
- break;
- case VK_BACK:
- if (m_nAutoIndentCol > 0)
- {
- if (m_nInsertSpaceCount == 0)
- m_nInsertTabCount--;
- else
- m_nInsertSpaceCount--;
- if (m_nInsertTabCount || m_nInsertSpaceCount)
- {
- m_nAutoIndentCol = (m_nInsertTabCount * GetTabSize()) + m_nInsertSpaceCount + 1;
- m_nDispCol = m_nAutoIndentCol;
- }
- else
- {
- m_nAutoIndentCol = 0;
- m_nDispCol = 1;
- }
- }
- else if (!bCtrlKey)
- {
- if (m_Selection.IsSelExist())
- {
- DeleteSelection();
- }
- else
- {
- DeleteChar(GetCurrentDocumentRow(), m_nCurrentCol, xtpEditDelPosBefore);
- }
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- }
- else
- {
- BOOL bSelectionExist = m_Selection.IsSelExist();
- if (bSelectionExist)
- {
- DeleteSelection();
- }
- int iStartRow = GetCurrentDocumentRow();
- int iStartCol = m_nCurrentCol;
- FindWord(XTP_EDIT_FINDWORD_PREV);
- int iEndRow = GetCurrentDocumentRow();
- int iEndCol = m_nCurrentCol;
- DeleteBuffer(iStartRow, iStartCol, iEndRow, iEndCol);
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- if (bSelectionExist)
- m_pBuffer->GetUndoRedoManager()->ChainLastCommand();
- }
- bUpdateAll = TRUE;
- break;
- case VK_INSERT:
- {
- NotifyParent(XTP_EDIT_NM_INSERTKEY);
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE/*, FALSE*/);
- SetOverwriteMode(!m_pBuffer->GetOverwriteFlag());
- }
- break;
- case VK_SPACE:
- if (bCtrlKey)
- {
- CPoint pt(CWnd::GetCaretPos());
- pt.y += m_DrawTextProcessor.GetRowHeight();
- ClientToScreen(&pt);
- CString strText(GetLineText(GetCurrentDocumentRow()));
- int nTextPos = m_nCurrentCol - 2;
- CString strSearch;
- while (strText.GetLength() > 0 && nTextPos >= 0 && m_pAutoComplete->m_strDelims.Find(strText.GetAt(nTextPos)) < 0)
- {
- strSearch = strText.GetAt(nTextPos--) + strSearch;
- }
- SetAutoCompleteList();
- m_pAutoComplete->Show(pt, strSearch);
- }
- break;
- }
- BOOL bChanged = nTopRow_prev != m_nTopRow || nDispCol_prev != m_nDispCol ||
- nTextRow_prev != GetCurrentDocumentRow() || nRowCount_prev != GetRowCount() ||
- selData_prev != m_Selection;
- if (bChanged)
- {
- int nCurDocRow = GetCurrentDocumentRow();
- SetCurCaretPos(nCurDocRow, m_nDispCol);
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- Invalidate(FALSE);
- UpdateWindow();
- if (nTopRow_prev != m_nTopRow)
- RecalcScrollBars();
- DWORD dwUpdate = (XTP_EDIT_UPDATE_HORZ|XTP_EDIT_UPDATE_VERT);
- if (bUpdateAll)
- dwUpdate |= XTP_EDIT_UPDATE_DIAG;
- UpdateScrollPos(dwUpdate);
- }
- CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CXTPSyntaxEditCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if (m_bDragging || m_bRightButtonDrag)
- {
- OnSetCursor(0,0,0);
- return;
- }
- NotifySelInit();
- BOOL bAltKey = (::GetKeyState(VK_MENU) & KF_UP) != 0;
- BOOL bCtrlKey = (::GetKeyState(VK_CONTROL) & KF_UP) != 0;
- BOOL bShiftKey = (::GetKeyState(VK_SHIFT) & KF_UP) != 0;
- CString strCurCRLF = m_pBuffer->GetCurCRLF();
- LPCTSTR szCurCRLF = strCurCRLF;
- BOOL bProcessed = FALSE;
- if (nChar == VK_TAB)
- {
- if (bShiftKey)
- bProcessed = DecreaseIndent();
- else
- bProcessed = IncreaseIndent();
- }
- if (!bProcessed && (bCtrlKey && !bAltKey) && nChar == 0x0C) // 0x0C is 'l' or 'L'
- {
- // Delete the entire line or the selection
- DeleteSelectedLines(GetCurrentDocumentRow());
- }
- else if (nChar != VK_BACK && nChar != VK_ESCAPE && !(bCtrlKey && !bAltKey) && !bProcessed)
- {
- if (!CanEditDoc())
- {
- return;
- }
- BOOL bModified = FALSE;
- //**----------------------
- OnBeforeEditChanged(GetCurrentDocumentRow(), m_nCurrentCol);
- //**----------------------
- int nChainActionCount = 1;
- BOOL bRedraw = FALSE;
- BOOL bPrevOverwriteMode = m_pBuffer->GetOverwriteFlag();
- if (m_Selection.IsSelExist())
- {
- if (!m_pBuffer->GetOverwriteFlag() || (m_pBuffer->GetOverwriteFlag() && nChar != VK_RETURN))
- {
- BOOL bRes = DeleteSelection();
- bModified |= bRes;
- nChainActionCount = 2;
- bRedraw = TRUE;
- m_pBuffer->SetOverwriteFlag(FALSE);
- }
- }
- TCHAR szText[3];
- szText[0] = (TCHAR)nChar;
- szText[1] = NULL;
- // DBCS Support (specially for IME)
- #ifndef _UNICODE
- //if (m_bIsImeEnabled)
- if (isleadbyte((int)nChar) && m_chPrevLeadByte == 0)
- {
- // do not process lead byte. just remember it and exit.
- m_chPrevLeadByte = (BYTE)nChar;
- return;
- }
- if (m_chPrevLeadByte)
- {
- szText[0] = (TCHAR)m_chPrevLeadByte;
- szText[1] = (TCHAR)nChar;
- szText[2] = NULL;
- }
- m_chPrevLeadByte = 0;
- #endif
- if (nChar == _T('r'))
- {
- lstrcpy(szText, szCurCRLF);
- szText[2] = NULL;
- // here we should expand collapsed block if we are on it
- if (GetLineMarksManager()->HasRowMark(GetCurrentDocumentRow(), xtpEditLMT_Collapsed))
- {
- GetLineMarksManager()->DeleteLineMark(GetCurrentDocumentRow(), xtpEditLMT_Collapsed);
- }
- }
- int iNewDispCol = m_nDispCol;
- int iNewCol = m_nCurrentCol;
- int iNewRow = GetCurrentDocumentRow();
- int iEditRowFrom = iNewRow;
- int iEditRowTo = iNewRow;
- CString strTextToIns;
- // Create text to insert
- BOOL bCanProcess =
- CreateInsertText(szText,
- strTextToIns,
- iNewRow,
- iNewCol,
- iNewDispCol,
- iEditRowFrom,
- iEditRowTo,
- nChainActionCount);
- BOOL bGroupInsMode = FALSE;
- if (!bPrevOverwriteMode)
- {
- static LPCTSTR szSeps = _T(" [{()}];.,trn"");
- if (_tcschr(szSeps, (TCHAR)nChar))
- bGroupInsMode = FALSE;
- else
- bGroupInsMode = TRUE;
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(bGroupInsMode);
- }
- else
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- if (bCanProcess)
- {
- int nInsCol = m_nCurrentCol;
- if (nChar == VK_RETURN && m_nAutoIndentCol)
- nInsCol = min(m_nCurrentCol, m_pBuffer->GetLineTextLengthC(GetCurrentDocumentRow()) + 1);
- BOOL bInsRes = m_pBuffer->InsertText(strTextToIns, GetCurrentDocumentRow(), nInsCol, TRUE);
- bModified |= bInsRes;
- if (nChainActionCount > 1)
- {
- m_pBuffer->GetUndoRedoManager()->ChainLastCommand();
- m_pBuffer->GetUndoRedoManager()->SetLastCommandText(XTP_IDS_EDIT_TYPING);
- }
- }
- //m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- m_pBuffer->SetOverwriteFlag(bPrevOverwriteMode);
- //**----------------------
- OnEditChanged(GetCurrentDocumentRow(), m_nCurrentCol, iNewRow, iNewCol, xtpEditActInsert);
- //**----------------------
- BOOL bInsAt0 = (m_nCurrentCol == 1) || GetLineText(GetCurrentDocumentRow()).GetLength() == 0;
- m_nCurrentCol = iNewCol;
- m_nDispCol = iNewDispCol;
- SetCurrentDocumentRow(iNewRow);
- BOOL bNewRow = FALSE;
- if (nChar == VK_RETURN)
- bNewRow = TRUE;
- UINT nAction = XTP_EDIT_EDITACTION_MODIFYROW;
- if (bNewRow)
- nAction |= XTP_EDIT_EDITACTION_INSERTROW;
- if (bNewRow && bInsAt0)
- nAction |= XTP_EDIT_EDITACTION_INSERTROW_NEW;
- NotifyEditChanged(iEditRowFrom, iEditRowTo, nAction);
- // if (nChar == VK_RETURN)
- // {
- // DoAutoIndentIfNeed(nDispCol_prev);
- // }
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol);
- RecalcScrollBars();
- Invalidate(FALSE);
- UpdateWindow();
- UpdateScrollPos();
- if (bModified)
- SetDocModified();
- }
- else if (!bProcessed && nChar == VK_ESCAPE)
- {
- Unselect();
- UpdateScrollPos();
- }
- // AutoComplete Processing
- if (m_pAutoComplete->IsOpenTag((TCHAR)nChar))
- {
- CPoint pt(CWnd::GetCaretPos());
- pt.y += m_DrawTextProcessor.GetRowHeight();
- ClientToScreen(&pt);
- SetAutoCompleteList();
- m_pAutoComplete->Show(pt);
- }
- CWnd::OnChar(nChar, nRepCnt, nFlags);
- }
- void CXTPSyntaxEditCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CXTPSmartPtrInternalT<CCmdTarget> ptrThisLock(this, TRUE);
- #ifdef XTP_SYNTAXEDIT_SITENOTIFY_KEY
- if (!XTP_SYNTAXEDIT_SITENOTIFY_KEY(this, FALSE, nChar))
- return;
- #endif
- if (nChar == 0)
- {
- return;
- }
- if (m_bDragging)
- {
- OnSetCursor(0,0,0);
- return;
- }
- CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
- }
- void CXTPSyntaxEditCtrl::SetCurPos(int nTextRow, int nDispCol, BOOL bRemainSelected, BOOL bForceVisible)
- {
- if (nTextRow > GetRowCount())
- nTextRow = GetRowCount();
- //reset autoindent
- m_nAutoIndentCol = 0;
- CString strText = GetLineText(nTextRow);
- // validate col
- m_nCurrentCol = CalcAbsCol(strText, nDispCol);
- m_nDispCol = CalcDispCol(strText, m_nCurrentCol);
- if (!bRemainSelected && m_Selection.IsSelExist())
- {
- m_Selection.Reset_disp(nTextRow, m_nDispCol);
- }
- SetCurCaretPos(nTextRow, m_nDispCol, TRUE, bForceVisible);
- Invalidate(FALSE);
- UpdateWindow();
- UpdateScrollPos(XTP_EDIT_UPDATE_HORZ|XTP_EDIT_UPDATE_VERT);
- }
- void CXTPSyntaxEditCtrl::SetCurCaretPos(int nTextRow, int nDispCol, BOOL bRowColNotify, BOOL bEnsureVisible)
- {
- if (bEnsureVisible)
- _EnsureVisible(nTextRow, nDispCol);
- int nDispRow = GetVisibleRow(nTextRow);
- if (nDispRow > m_DrawTextProcessor.GetRowsCount(TRUE) + 10)
- nDispRow = m_DrawTextProcessor.GetRowsCount(TRUE) + 10;
- if (m_nAutoIndentCol > 0)
- nDispCol = m_nAutoIndentCol;
- int nCol = max(0, nDispCol - 1);
- m_nDispCol = nCol + 1;
- m_nCurrentCol = CalcAbsCol(nTextRow, m_nDispCol);
- SetCurrentDocumentRow(nTextRow);
- if (bRowColNotify)
- NotifyCurRowCol(GetCurrentDocumentRow(), m_nDispCol);
- Invalidate(FALSE);
- //TRACE(_T("CXTPSyntaxEditCtrl::SetCurCaretPos(%d, %d) n"), nDispRow, nDispCol);
- }
- void CXTPSyntaxEditCtrl::_EnsureVisible(int nTextRow, int nDispCol/*, BOOL bRedraw*/)
- {
- BOOL bVScrolled = EnsureVisibleRow(nTextRow);
- nDispCol = nDispCol;
- if (m_nAutoIndentCol > 0)
- nDispCol = m_nAutoIndentCol;
- int nDispRow = GetVisibleRow(nTextRow);
- BOOL bHScrolled = EnsureVisibleCol(nDispRow, nDispCol);
- // BOOL bVirtSpace = _IsVirtualSpaceActive() || m_nAutoIndentCol;
- // m_nCurrentCol = m_DrawTextProcessor.DispPosToStrPos(nDispRow - 1, m_nDispCol-1, bVirtSpace) + 1;
- if (bVScrolled || bHScrolled /*|| bRedraw*/)
- {
- Invalidate(FALSE);
- }
- }
- BOOL CXTPSyntaxEditCtrl::EnsureVisibleRow(int nTextRow)
- {
- int nPrevTopRow = m_nTopRow;
- int nDispRowsCount = m_DrawTextProcessor.GetRowsCount(FALSE);
- int nCurPageMaxTextRow = GetDocumentRow(nDispRowsCount);
- if (nDispRowsCount == 0)
- {
- return FALSE;
- }
- if (nTextRow > nCurPageMaxTextRow)
- {
- m_nTopRow += nTextRow - nCurPageMaxTextRow;
- }
- else if (nTextRow < m_nTopRow)
- {
- m_nTopRow = max (1, nTextRow);
- }
- if (GetVertScrollBar() /*&& nPrevTopRow != m_nTopRow*/)
- {
- BOOL bEnableVertScrl = (CalculateVisibleRow(1, GetRowCount()) > nDispRowsCount);
- EnableScrollBarCtrl(SB_VERT, bEnableVertScrl);
- if (bEnableVertScrl)
- SetScrollPos(SB_VERT, CalculateVisibleRow(1, m_nTopRow));
- }
- return nPrevTopRow != m_nTopRow;
- }
- BOOL CXTPSyntaxEditCtrl::EnsureVisibleCol(int nDispRow, int nDispCol)
- {
- int nTextRow = GetDocumentRow(nDispRow);
- UpdateRowInfoInternally(nTextRow);
- int nColWidth = 0;
- int nColPos = m_DrawTextProcessor.GetColPosX(nDispRow - 1, nDispCol, &nColWidth, m_bVirtualSpace);
- int nOffsetX = m_DrawTextProcessor.GetScrollXOffset();
- CRect rcText = m_DrawTextProcessor.GetTextRect();
- int nNewScrollOffset = -1;
- if (nColPos - max(14, nColWidth * 2) < rcText.left)
- {
- nNewScrollOffset = max(0, nOffsetX - (rcText.left - nColPos )- nColWidth * 15);
- }
- else if (nColPos + nColWidth >= rcText.right)
- {
- nNewScrollOffset = nOffsetX + (nColPos + nColWidth) - rcText.right + min(rcText.Width()/3, nColWidth * 15);
- }
- if (nNewScrollOffset >= 0)
- {
- SetScrollPos(SB_HORZ, nNewScrollOffset);
- m_DrawTextProcessor.SetScrollXOffset(nNewScrollOffset);
- }
- return nNewScrollOffset >= 0;
- }
- int CXTPSyntaxEditCtrl::CalcDispCol(int nTextRow, int nActualCol)
- {
- CString strLine = GetLineText(nTextRow);
- return CalcDispCol(strLine, nActualCol);
- }
- int CXTPSyntaxEditCtrl::CalcAbsCol(int nTextRow, int nDispCol)
- {
- CString strLine = GetLineText(nTextRow);
- return CalcAbsCol(strLine, nDispCol);
- }
- int CXTPSyntaxEditCtrl::CalcDispCol(LPCTSTR szText, int nActualCol)
- {
- int iDispCol = 0;
- int nAbsCol = 1;
- for (LPCTSTR pcszChar = szText; pcszChar && *pcszChar; pcszChar = _tcsinc(pcszChar))
- {
- if (nAbsCol >= nActualCol)
- break;
- if (*pcszChar == _T('t'))
- {
- // Now calculate tab size
- iDispCol += (GetTabSize() - (iDispCol % GetTabSize()));
- }
- else
- iDispCol++;
- nAbsCol++;
- }
- iDispCol++;
- if (nAbsCol < nActualCol)
- {
- if (_IsVirtualSpaceActive())
- iDispCol += nActualCol - nAbsCol;
- else
- iDispCol++;
- }
- return iDispCol;
- }
- int CXTPSyntaxEditCtrl::CalcAbsCol(LPCTSTR szText, int iDispCol)
- {
- int nAbsCol = 0;
- int nDispColCalc = 0;
- for (LPCTSTR pcszChar = szText; pcszChar && *pcszChar; pcszChar = _tcsinc(pcszChar))
- {
- if (nDispColCalc >= iDispCol)
- break;
- //if (szText[i] == _T('t'))
- if (*pcszChar == _T('t'))
- nDispColCalc += (GetTabSize() - (nDispColCalc % GetTabSize()));
- else
- nDispColCalc++;
- nAbsCol++;
- }
- if (nDispColCalc < iDispCol)
- {
- if (_IsVirtualSpaceActive())
- nAbsCol += iDispCol - nDispColCalc;
- else
- nAbsCol++;
- }
- if (nAbsCol == 0)
- nAbsCol = 1;
- return nAbsCol;
- }
- int CXTPSyntaxEditCtrl::CalcValidDispCol(LPCTSTR szText, int iCol)
- {
- int iDispCol = 0;
- for (LPCTSTR pcszChar = szText; pcszChar && *pcszChar; pcszChar = _tcsinc(pcszChar))
- {
- if (iDispCol >= iCol-1)
- break;
- if (*pcszChar == _T('t'))
- {
- // Now calculate tab size
- iDispCol += (GetTabSize() - (iDispCol % GetTabSize()));
- }
- else
- iDispCol++;
- }
- return ++iDispCol;
- }
- int CXTPSyntaxEditCtrl::CalcMaximumWidth(LPCTSTR szText)
- {
- int iMaxWidth = 1;
- for (LPCTSTR pcszChar = szText; pcszChar && *pcszChar; pcszChar = _tcsinc(pcszChar))
- {
- if (*pcszChar == _T('t'))
- {
- // Now calculate tab size
- iMaxWidth += ((GetTabSize() + 1) - (iMaxWidth - ((iMaxWidth / GetTabSize()) * GetTabSize())));
- }
- else
- iMaxWidth++;
- }
- return iMaxWidth;
- }
- void CXTPSyntaxEditCtrl::GetLineNodeRect(int nRow, CRect& rcNode, CRect* prcNodeFull)
- {
- // calculate node rect
- CRect rcNodes, rcText;
- CalcEditRects(NULL, NULL, &rcNodes, &rcText);
- int nRowHeight = m_DrawTextProcessor.GetRowHeight();
- int nYPos = rcNodes.top + nRow * nRowHeight;
- rcNode = rcNodes;
- rcNode.top = nYPos + (nRowHeight - m_nNodesWidth) / 2;
- rcNode.bottom = rcNode.top + m_nNodesWidth - 1;
- if (prcNodeFull)
- {
- prcNodeFull->left = rcNodes.left;
- prcNodeFull->right = rcText.left;
- prcNodeFull->top = nYPos;
- prcNodeFull->bottom = nYPos + nRowHeight;
- }
- }
- BOOL CXTPSyntaxEditCtrl::ProcessCollapsedTextEx(CDC* pDC, XTP_EDIT_COLLAPSEDBLOCK* pCoDrawBlk,
- const XTP_EDIT_TEXTBLOCK& txtBlk,
- CRect& rcCoBlk)
- {
- if (!pCoDrawBlk)
- {
- ASSERT(FALSE);
- return FALSE;
- }
- if (txtBlk.nPos < pCoDrawBlk->collBlock.lcStart.nCol)
- {
- return FALSE;
- }
- ProcessCollapsedText(pDC, pCoDrawBlk, rcCoBlk);
- return TRUE;
- }
- void CXTPSyntaxEditCtrl::ProcessCollapsedText(CDC* pDC, XTP_EDIT_COLLAPSEDBLOCK* pCoDrawBlk,
- CRect& rcCoBlk)
- {
- CString strText = pCoDrawBlk->collBlock.strCollapsedText; // "[..]"
- CXTPFontDC fontDC(pDC, GetPaintManager()->GetFont());
- rcCoBlk.right = rcCoBlk.left + pDC->GetTextExtent(strText).cx + 3;
- pCoDrawBlk->rcCollMark = rcCoBlk;
- }
- CString CXTPSyntaxEditCtrl::GetCollapsedText(XTP_EDIT_COLLAPSEDBLOCK* pCoDrawBlk, int nMaxLinesCount)
- {
- if (!pCoDrawBlk || !m_pBuffer)
- {
- ASSERT(FALSE);
- return _T("");
- }
- CString strCoText;
- int nLine1 = pCoDrawBlk->collBlock.lcStart.nLine;
- int nLine2 = pCoDrawBlk->collBlock.lcEnd.nLine;
- int nLine2max = min(nLine2, nLine1 + nMaxLinesCount-1);
- int nCol1 = pCoDrawBlk->collBlock.lcStart.nCol;
- int nCol2 = pCoDrawBlk->collBlock.lcEnd.nCol;
- for (int nLine = nLine1; nLine <= nLine2max; nLine++)
- {
- CString strTmp = m_pBuffer->GetLineText(nLine);
- int nTextLen = strTmp.GetLength();
- if (nTextLen)
- {
- if (nLine == nLine2)
- {
- ASSERT(nCol2 < nTextLen+2);
- nCol2 = max(0, min(nCol2, nTextLen-1));
- strTmp = strTmp.Left(nCol2+1);
- }
- if (nLine == nLine1)
- {
- nTextLen = strTmp.GetLength();
- ASSERT(nCol1 < nTextLen+2);
- if (nCol1 < nTextLen)
- {
- strTmp = strTmp.Mid(nCol1);
- }
- else
- {
- strTmp.Empty();
- }
- }
- }
- if (!strCoText.IsEmpty())
- {
- strCoText += _T("rn");
- }
- strCoText += strTmp;
- }
- return strCoText;
- }
- int CXTPSyntaxEditCtrl::ExpandChars(CDC* pDC, LPCTSTR pszChars, int nCurPos, CString& strBuffer)
- {
- int nTabSize = GetTabSize();
- const int nLength = (int)_tcslen(pszChars);
- if (nLength == 0)
- return 0;
- int nActualOffset = nCurPos;
- int I;
- for (I = 0; I < nLength; I++)
- {
- if (pszChars[I] == _T('t'))
- nActualOffset += (nTabSize - nActualOffset % nTabSize);
- else
- nActualOffset ++;
- }
- int nActualLength = nActualOffset - nCurPos;
- for (I = 0; I < nLength; I++)
- {
- if (pszChars[I] == _T('t'))
- {
- int nSpaces = nTabSize - (nCurPos % nTabSize);
- BOOL bFirstChar = TRUE;
- while (nSpaces > 0)
- {
- if (bFirstChar && m_bEnableWhiteSpace && !pDC->IsPrinting())
- {
- strBuffer += (TCHAR)(unsigned char)0xBB;
- bFirstChar = FALSE;
- }
- else
- strBuffer += _T(' ');
- nSpaces--;
- nCurPos++;
- }
- }
- else
- {
- if (m_bEnableWhiteSpace && pszChars[I] == _T(' ') && !pDC->IsPrinting())
- strBuffer += (TCHAR)(unsigned char)0xB7;
- else
- strBuffer += pszChars[I];
- nCurPos++;
- }
- }
- return nActualLength;
- }
- void CXTPSyntaxEditCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- SetFocus();
- // Manage AutoCompleteView
- if (m_pAutoComplete->IsActive())
- {
- CRect rcACcomplete;
- m_pAutoComplete->GetWindowRect(&rcACcomplete);
- ScreenToClient(&rcACcomplete);
- if (rcACcomplete.PtInRect(point))
- return;
- else
- m_pAutoComplete->Hide();
- }
- m_bDragging = FALSE;
- m_bRightButtonDrag = FALSE;
- m_nAutoIndentCol = 0;
- m_Selection.bSelectingRunning = FALSE;
- if (m_Selection.IsSelExist())
- Invalidate(FALSE);
- NotifySelInit();
- SetCapture();
- BOOL bCtrlKey = (::GetKeyState(VK_CONTROL) & KF_UP) != 0;
- BOOL bShiftKey = (::GetKeyState(VK_SHIFT) & KF_UP) != 0;
- BOOL bAltKey = (::GetKeyState(VK_MENU) & KF_UP) != 0;
- CXTPClientRect rcClient(this);
- CRect rcBookmark, rcLineNum, rcNodes, rcText;
- rcClient.bottom = rcClient.top + m_DrawTextProcessor.GetRowHeight() * m_DrawTextProcessor.GetRowsCount(FALSE);
- CalcEditRects(&rcBookmark, &rcLineNum, &rcNodes, &rcText, &rcClient);
- CRect rcLineNumAndNodes(rcLineNum);
- rcLineNumAndNodes.right = rcNodes.right;
- int nNewRow = 0, nNewCol = 0, nNewDispRow = 0, nNewDispCol = 0;
- RowColFromPoint(point, &nNewRow, &nNewCol, &nNewDispRow, &nNewDispCol);
- BOOL bSelFromLeftBar = rcLineNumAndNodes.PtInRect(point);
- // TRACE(_T("*** OnLButtonDown. RowColFromPoint: nNewRow=%d, nNewCol=%d, nNewDispRow=%d, nNewDispCol=%d n"),
- // nNewRow, nNewCol, nNewDispRow, nNewDispCol);
- //--------------------------------------------------------------------
- if (GetCurrentDocumentRow() != nNewRow || m_nDispCol != nNewDispCol)
- {
- // reset undo buffer group mode processing
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- }
- //--------------------------------------------------------------------
- if (rcBookmark.PtInRect(point))
- {
- m_Selection.Reset_disp(nNewRow, nNewDispCol);
- if (!NotifyMarginLBtnClick(nNewRow, nNewDispRow))
- {
- AddRemoveBreakPoint(nNewRow);
- SetCurPos(nNewRow, 1);
- }
- CWnd::OnLButtonDown(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_LBUTTONDOWN, nFlags, point);
- return;
- }
- //--------------------------------------------------------------------
- if (m_bDrawNodes && rcNodes.PtInRect(point))
- {
- CXTPEmptyRect rcNode;
- DWORD dwType = XTP_EDIT_ROWNODE_NOTHING;
- if (GetRowNodes(nNewRow, dwType) && (dwType & (XTP_EDIT_ROWNODE_COLLAPSED | XTP_EDIT_ROWNODE_EXPANDED)))
- {
- GetLineNodeRect(nNewDispRow-1, rcNode);
- }
- if (rcNode.PtInRect(point) && m_pBuffer)
- {
- m_Selection.Reset_disp(m_Selection.GetEnd_disp().nLine, m_Selection.GetEnd_disp().nCol);
- // expand/collapse a node
- CollapseExpandBlock(nNewRow);
- SetCurPos(nNewRow, 1);
- CWnd::OnLButtonDown(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_LBUTTONDOWN, nFlags, point);
- return;
- }
- }
- //--------------------------------------------------------------------
- if (rcText.PtInRect(point) && m_Selection.IsInSel_disp(nNewRow, nNewDispCol))
- {
- m_Selection.bSelectingRunning = FALSE;
- if (!m_bEnableOleDrag)
- m_bDragging = TRUE;
- else
- {
- NotifyParent(XTP_EDIT_NM_STARTOLEDRAG);
- }
- CWnd::OnLButtonDown(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_LBUTTONDOWN, nFlags, point);
- return;
- }
- //--------------------------------------------------------------------
- if (bAltKey)
- {
- m_Selection.Reset_disp(nNewRow, nNewDispCol);
- m_Selection.bSelectingRunning = TRUE;
- m_Selection.bBlockSelectionMode = TRUE;
- }
- else if (bShiftKey)
- {
- m_Selection.SetEnd_disp(nNewRow, nNewDispCol);
- m_Selection.bSelectingRunning = TRUE;
- m_Selection.bBlockSelectionMode = FALSE;
- m_Selection.bWordSelectionMode = bCtrlKey;
- }
- else
- {
- m_Selection.Reset_disp(nNewRow, nNewDispCol);
- m_Selection.bSelectingRunning = TRUE;
- m_Selection.bWordSelectionMode = bCtrlKey;
- if (bSelFromLeftBar)
- m_Selection.nSelStartTextRowFromLeftBar = m_Selection.GetStart_str().nLine;
- }
- // TRACE(_T("Selection: (row=%d, strPos=%d, DispCol=%d)-(row=%d, strPos=%d, DispCol=%d) n"),
- // m_Selection.GetStart_str().nLine, m_Selection.GetStart_str().nCol, m_Selection.GetStart_disp().nCol,
- // m_Selection.GetEnd_str().nLine, m_Selection.GetEnd_str().nCol, m_Selection.GetEnd_disp().nCol);
- if (m_Selection.bWordSelectionMode)
- {
- XTP_EDIT_LINECOL lcWord1, lcWord2;
- BOOL bOverSpace = FALSE;
- UINT nFindDir = m_Selection.IsSelNormal() ? XTP_EDIT_FINDWORD_NEXT : XTP_EDIT_FINDWORD_PREV;
- BOOL bFind = FindWordEx_str(nFindDir, m_Selection.GetEnd_str(), lcWord1, lcWord2, bOverSpace);
- // TRACE(_T("1-FindWordEx_str = %d (%d,%d - %d,%d)"), bFind,
- // lcWord1.nLine, lcWord1.nCol,lcWord2.nLine, lcWord2.nCol);
- if (bFind && lcWord1.IsValidData() && lcWord1 != m_Selection.GetNormalStart_str())
- {
- if (m_Selection.IsSelNormal())
- m_Selection.SetStart_str(lcWord1.nLine, lcWord1.nCol-1);
- else
- m_Selection.SetEnd_str(lcWord1.nLine, lcWord1.nCol-1);
- }
- if (lcWord2.IsValidData() && lcWord2 != m_Selection.GetNormalEnd_str())
- {
- if (m_Selection.IsSelNormal())
- m_Selection.SetEnd_str(lcWord2.nLine, lcWord2.nCol-1);
- else
- m_Selection.SetStart_str(lcWord2.nLine, lcWord2.nCol-1);
- }
- }
- SetCurrentDocumentRow(nNewRow);
- m_nDispCol = m_Selection.GetEnd_disp().nCol;
- m_nCurrentCol = m_Selection.GetEnd_str().nCol + 1;
- SetCurCaretPos(m_Selection.GetEnd_disp().nLine, m_nDispCol, TRUE, FALSE);
- if (m_Selection.bSelectingRunning)
- SetTimer(TIMER_SELECTION_ID, TIMER_SELECTION_TIME, NULL);
- Invalidate(FALSE);
- CWnd::OnLButtonDown(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_LBUTTONDOWN, nFlags, point);
- }
- void CXTPSyntaxEditCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- KillTimer(TIMER_SELECTION_ID);
- int nRow = ProcessCollapsedBlockDblClick(point);
- if (nRow > 0)
- {
- // close tooltip.
- ShowCollapsedToolTip(CPoint(0,0));
- }
- else
- {
- // Select the word
- SelectWord(point);
- }
- CWnd::OnLButtonDblClk(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_LBUTTONDBLCLICK, nFlags, point);
- }
- void CXTPSyntaxEditCtrl::OnLButtonUp(UINT nFlags, CPoint point)
- {
- if (!GetCapture() || GetCapture() != this || GetFocus() != this)
- {
- return;
- }
- if (GetCapture() == this)
- {
- ReleaseCapture();
- }
- int nNewRow = 0, nNewCol = 0, nNewDispRow = 0, nNewDispCol = 0;
- BOOL bRowCol = RowColFromPoint(point, &nNewRow, &nNewCol, &nNewDispRow, &nNewDispCol);
- if (bRowCol && m_Selection.IsInSel_disp(nNewRow, nNewDispCol) &&
- !m_Selection.bSelectingRunning && !m_bDragging) // dragging
- {
- m_Selection.Reset_disp(m_Selection.GetStart_disp().nLine, m_Selection.GetStart_disp().nCol);
- }
- else
- {
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol, FALSE, FALSE);
- }
- m_Selection.bSelectingRunning = FALSE;
- KillTimer(TIMER_SELECTION_ID);
- m_dwAutoScrollDirection = 0;
- KillTimer(TIMER_AUTOSCROLL_ID);
- //-----------------------------------------------------------------------
- if (m_bDragging)
- {
- if (m_bDroppable)
- {
- BOOL bCtrlKey = (::GetKeyState(VK_CONTROL) & KF_UP) != 0;
- CopyOrMoveText(bCtrlKey);
- }
- else
- {
- m_Selection.Reset_disp(GetCurrentDocumentRow(), m_nDispCol);
- }
- }
- m_bDragging = FALSE;
- m_bDroppable = FALSE;
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol);
- Invalidate(FALSE);
- UpdateWindow();
- CWnd::OnLButtonUp(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_LBUTTONUP, nFlags, point);
- }
- void CXTPSyntaxEditCtrl::OnMouseMove(UINT nFlags, CPoint point)
- {
- if (m_bDragging || m_bRightButtonDrag)
- {
- m_bDragging = TRUE;
- CXTPClientRect rcWnd(this);
- if (rcWnd.PtInRect(point))
- {
- int nNewRow = 0, nNewCol = 0, nNewDispCol = 0;
- RowColFromPoint(point, &nNewRow, &nNewCol, NULL, &nNewDispCol);
- //TRACE(_T("DropPOS: nNewRow=%d, nNewCol=%d, nNewDispCol=%d n"), nNewRow, nNewCol, nNewDispCol);
- if (nNewRow != m_ptDropPos.y || nNewDispCol != m_ptDropPos.x)
- {
- m_nDispCol = nNewDispCol;
- m_ptDropPos.x = m_nDispCol;
- m_ptDropPos.y = nNewRow;
- //SetCurrentDocumentRow(nNewRow);
- SetCurCaretPos(m_ptDropPos.y, m_ptDropPos.x, FALSE, FALSE);
- }
- m_bDroppable = !m_Selection.IsInSel_disp(m_ptDropPos.y, m_ptDropPos.x);
- OnSetCursor(0,0,0);
- }
- else
- {
- m_bDroppable = FALSE;
- SetCursor(AfxGetApp()->LoadStandardCursor(IDC_NO));
- }
- }
- // else if (m_Selection.bSelectingRunning)
- // {
- // OnTimer(TIMER_SELECTION_ID);
- // }
- ShowCollapsedToolTip(point);
- CWnd::OnMouseMove(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_MOUSEMOVE, nFlags, point);
- }
- XTP_EDIT_COLLAPSEDBLOCK* CXTPSyntaxEditCtrl::GetBlockFromPt(const CPoint& ptMouse)
- {
- // enumerate all collapsed blocks coordinates
- // to find the one where mouse into
- XTP_EDIT_COLLAPSEDBLOCK* pCoDrawBlk = NULL;
- XTP_EDIT_LMPARAM LMCoParam;
- int nActualRow = 0;
- for (int i = 0; i < m_nCollapsedTextRowsCount; i++)
- {
- int nRow = m_arCollapsedTextRows[i];
- if (nRow <= nActualRow)
- {
- continue;
- }
- if (!HasRowMark(nRow, xtpEditLMT_Collapsed, &LMCoParam))
- {
- continue;
- }
- // get count of collapsed rows under this row
- int nHiddenRows = 0;
- if (!GetCollapsedBlockLen(nRow, nHiddenRows))
- {
- continue;
- }
- nActualRow = nRow + nHiddenRows;
- // get collapsed block pointer
- pCoDrawBlk = (XTP_EDIT_COLLAPSEDBLOCK*)LMCoParam.GetPtr();
- if (!pCoDrawBlk)
- {
- ASSERT(FALSE);
- continue;
- }
- CRect crCollapsed(pCoDrawBlk->rcCollMark);
- if (crCollapsed.PtInRect(ptMouse))
- {
- return pCoDrawBlk;
- }
- }
- return NULL;
- }
- int CXTPSyntaxEditCtrl::ProcessCollapsedBlockDblClick(const CPoint& ptMouse)
- {
- // Find required collapsed block by coordinates
- XTP_EDIT_COLLAPSEDBLOCK* pCoDrawBlk = GetBlockFromPt(ptMouse);
- if (pCoDrawBlk)
- {
- int nRow = pCoDrawBlk->collBlock.lcStart.nLine;
- CollapseExpandBlock(nRow);
- return nRow;
- }
- return 0;
- }
- AFX_STATIC CString AFX_CDECL TrimLeftIndent(const CString& csText)
- {
- CString csTrimmedText(csText);
- int nLastLine = csTrimmedText.ReverseFind('n');
- CString csTrimText(csTrimmedText.Right(csTrimmedText.GetLength() - nLastLine).SpanIncluding(_T("n t")));
- REPLACE_S(csTrimmedText, csTrimText, _T("n"));
- return csTrimmedText;
- }
- void CXTPSyntaxEditCtrl::ShowCollapsedToolTip(const CPoint& ptMouse)
- {
- if (ptMouse == m_ptPrevMouse)
- {
- return;
- }
- // Find required collapsed block by coordinates
- XTP_EDIT_COLLAPSEDBLOCK* pCoDrawBlk = GetBlockFromPt(ptMouse);
- if (pCoDrawBlk)
- {
- m_ptPrevMouse = ptMouse;
- // set tooltip rect
- CRect rcTip(ptMouse, ptMouse);
- rcTip.right += 200;
- rcTip.bottom += 200;
- rcTip += CSize(18, 18);
- ClientToScreen(&rcTip);
- m_pToolTip->SetHoverRect(rcTip);
- // set tooltip text
- CString strToolText(GetCollapsedText(pCoDrawBlk));
- m_pToolTip->Activate(TrimLeftIndent(strToolText));
- }
- // Then Hide Tip if it is displayed for some other block
- if (ptMouse != m_ptPrevMouse)
- {
- m_pToolTip->Hide();
- }
- }
- void CXTPSyntaxEditCtrl::OnRButtonDown(UINT nFlags, CPoint point)
- {
- m_bDragging = FALSE;
- m_nAutoIndentCol = 0;
- KillTimer(TIMER_SELECTION_ID);
- int iRow, iCol;
- RowColFromPoint(point, &iRow, &iCol);
- if (!m_Selection.IsSelExist() || !m_Selection.IsInSel_str(iRow, iCol))
- {
- LPCTSTR szLineText = GetLineText(iRow);
- //SetCurrentDocumentRow(iRow);
- m_nDispCol = CalcValidDispCol(szLineText, iCol);
- m_nCurrentCol = CalcAbsCol(szLineText, iCol);
- SetCurCaretPos(iRow, m_nDispCol);
- //Unselect();
- m_Selection.Reset_disp(iRow, m_nDispCol);
- NotifySelInit();
- }
- SetCapture();
- if (m_Selection.IsSelExist() && m_Selection.IsInSel_str(iRow, iCol))
- {
- m_bRightButtonDrag = TRUE;
- if (m_bEnableOleDrag)
- {
- NotifyParent(XTP_EDIT_NM_STARTOLEDRAG);
- }
- }
- // if (!m_Selection.IsSelExist() && !m_bEnableOleDrag)
- // {
- // RowColFromPoint(point, &iRow, &iCol);
- // // SetCurPos will determine valid column
- // SetCurPos(iRow, iCol);
- // }
- CWnd::OnRButtonDown(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_RBUTTONDOWN, nFlags, point);
- }
- void CXTPSyntaxEditCtrl::OnRButtonUp(UINT nFlags, CPoint point)
- {
- CWnd::OnRButtonUp(nFlags, point);
- NotifyMouseEvent(XTP_EDIT_NM_RBUTTONUP, nFlags, point);
- }
- void CXTPSyntaxEditCtrl::Scroll(int x, int y)
- {
- m_bScrolling = TRUE;
- if (y > 0)
- ShiftCurrentVisibleRowDown(y);
- else if (y < 0)
- ShiftCurrentVisibleRowUp(-y);
- if (x)
- {
- SCROLLINFO info;
- ZeroMemory(&info, sizeof(SCROLLINFO));
- info.cbSize = sizeof(SCROLLINFO);
- info.fMask = SIF_ALL;
- GetScrollInfo(SB_HORZ, &info);
- int nStep = m_DrawTextProcessor.GetTextMetrics().tmAveCharWidth;
- int nCurrPos = m_DrawTextProcessor.GetScrollXOffset();
- nCurrPos += x * nStep;
- nCurrPos = max(0, nCurrPos);
- // TRACE(_T("SCROLL.X. from %d -> %d n"), (int)m_DrawTextProcessor.GetScrollXOffset(), nCurrPos);
- SetScrollPos(SB_HORZ, nCurrPos);
- m_DrawTextProcessor.SetScrollXOffset(nCurrPos);
- }
- //RecalcVertScrollPos();
- //RecalcHorzScrollPos();
- Invalidate(FALSE);
- UpdateWindow();
- RecalcScrollBars();
- int nTopDocRow = GetDocumentRow(1);
- int nBottomDocRow = GetDocumentRow(GetRowPerPage());
- int nCurDocRow = GetCurrentDocumentRow();
- if (nCurDocRow >= nTopDocRow && nCurDocRow <= nBottomDocRow)
- {
- SetCurCaretPos(nCurDocRow, m_nDispCol, FALSE, FALSE);
- }
- DWORD dwUpdate = 0;
- if (y != 0)
- dwUpdate |= XTP_EDIT_UPDATE_HORZ;
- if (x != 0)
- dwUpdate |= XTP_EDIT_UPDATE_VERT;
- if (dwUpdate != 0)
- UpdateScrollPos(dwUpdate);
- m_bScrolling = FALSE;
- }
- BOOL CXTPSyntaxEditCtrl::RowColFromPoint(CPoint pt, int *pRow, int *pCol,
- int *pDispRow, int *pDispCol, BOOL bVirtualSpace)
- {
- if (bVirtualSpace < 0)
- bVirtualSpace = _IsVirtualSpaceActive();
- int nDispRow0 = 0, nDispCol0 = 0;
- if (!m_DrawTextProcessor.HitTestRow(pt.y, nDispRow0))
- {
- nDispRow0 = pt.y < m_DrawTextProcessor.GetTextRect().top ? 0 : m_DrawTextProcessor.GetRowsCount(FALSE) - 1;
- }
- int nRow = GetDocumentRow(nDispRow0 + 1);
- if (nRow < 1 || nRow > GetRowCount())
- nRow = nRow < 1 ? 1 : GetRowCount();
- nDispRow0 = GetVisibleRow(nRow) - 1;
- UpdateRowInfoInternally(nRow); // to ensure than x positions are valid
- BOOL bRet = m_DrawTextProcessor.ColFromXPos(nDispRow0, pt.x, nDispCol0, bVirtualSpace);
- if (!bRet)
- {
- if (pt.x < m_DrawTextProcessor.GetTextRect().left)
- {
- nDispCol0 = 0;
- }
- else if (pt.x > m_DrawTextProcessor.GetTextRect().right)
- {
- int nX1 = max(0, m_DrawTextProcessor.GetTextRect().right - 1);
- m_DrawTextProcessor.ColFromXPos(nDispRow0, nX1, nDispCol0, bVirtualSpace);
- }
- }
- int nCol0 = m_DrawTextProcessor.DispPosToStrPos(nDispRow0, nDispCol0, bVirtualSpace);
- // to align to TABs positions
- nDispCol0 = m_DrawTextProcessor.StrPosToDispPos(nDispRow0, nCol0, bVirtualSpace);
- if (pRow)
- *pRow = nRow;
- if (pCol)
- *pCol = nCol0 + 1;
- if (pDispRow)
- *pDispRow = nDispRow0 + 1;
- if (pDispCol)
- *pDispCol = nDispCol0 + 1;
- return bRet;
- }
- void CXTPSyntaxEditCtrl::OnSize(UINT nType, int cx, int cy)
- {
- CWnd::OnSize(nType, cx, cy);
- if (m_bmpCache.m_hObject)
- m_bmpCache.DeleteObject();
- if (!GetPaintManager()->GetFont()->GetSafeHandle())
- return;
- CRect rcText;
- CalcEditRects(NULL, NULL, NULL, &rcText);
- m_DrawTextProcessor.SetTextRect(rcText);
- RecalcVertScrollPos();
- Invalidate(FALSE);
- UpdateWindow();
- RecalcHorzScrollPos();
- }
- int CXTPSyntaxEditCtrl::GetScrollPos(int nBar)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- return pParentWnd->GetScrollPos(nBar);
- }
- return CWnd::GetScrollPos(nBar);
- }
- void CXTPSyntaxEditCtrl::GetScrollRange(int nBar, LPINT lpMinPos, LPINT lpMaxPos) const
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- pParentWnd->GetScrollRange(nBar, lpMinPos, lpMaxPos);
- }
- else
- {
- CWnd::GetScrollRange(nBar, lpMinPos, lpMaxPos);
- }
- }
- void CXTPSyntaxEditCtrl::ScrollWindow(int xAmount, int yAmount, LPCRECT lpRect, LPCRECT lpClipRect)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- pParentWnd->ScrollWindow(xAmount, yAmount, lpRect, lpClipRect);
- }
- else
- {
- CWnd::ScrollWindow(xAmount, yAmount, lpRect, lpClipRect);
- }
- }
- int CXTPSyntaxEditCtrl::ScrollWindowEx(int dx, int dy, LPCRECT lpRectScroll, LPCRECT lpRectClip, CRgn *prgnUpdate, LPRECT lpRectUpdate, UINT flags)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- return pParentWnd->ScrollWindowEx(dx, dy, lpRectScroll, lpRectClip, prgnUpdate, lpRectUpdate, flags);
- }
- return CWnd::ScrollWindowEx(dx, dy, lpRectScroll, lpRectClip, prgnUpdate, lpRectUpdate, flags);
- }
- BOOL CXTPSyntaxEditCtrl::GetScrollInfo(int nBar, LPSCROLLINFO lpScrollInfo, UINT nMask)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- return pParentWnd->GetScrollInfo(nBar, lpScrollInfo, nMask);
- }
- return CWnd::GetScrollInfo(nBar, lpScrollInfo, nMask);
- }
- int CXTPSyntaxEditCtrl::GetScrollLimit(int nBar)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- return pParentWnd->GetScrollLimit(nBar);
- }
- return CWnd::GetScrollLimit(nBar);
- }
- BOOL CXTPSyntaxEditCtrl::SetScrollInfo(int nBar, LPSCROLLINFO lpScrollInfo, BOOL bRedraw)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- return pParentWnd->SetScrollInfo(nBar, lpScrollInfo, bRedraw);
- }
- return CWnd::SetScrollInfo(nBar, lpScrollInfo, bRedraw);
- }
- int CXTPSyntaxEditCtrl::SetScrollPos(int nBar, int nPos, BOOL bRedraw)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- return pParentWnd->SetScrollPos(nBar, nPos, bRedraw);
- }
- return CWnd::SetScrollPos(nBar, nPos, bRedraw);
- }
- void CXTPSyntaxEditCtrl::SetScrollRange(int nBar, int nMinPos, int nMaxPos, BOOL bRedraw)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- pParentWnd->SetScrollRange(nBar, nMinPos, nMaxPos, bRedraw);
- }
- else
- {
- CWnd::SetScrollRange(nBar, nMinPos, nMaxPos, bRedraw);
- }
- }
- void CXTPSyntaxEditCtrl::ShowScrollBar(UINT nBar, BOOL bShow)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- pParentWnd->ShowScrollBar(nBar, bShow);
- }
- else
- {
- CWnd::ShowScrollBar(nBar, bShow);
- }
- }
- void CXTPSyntaxEditCtrl::EnableScrollBarCtrl(int nBar, BOOL bEnable)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- pParentWnd->EnableScrollBarCtrl(nBar, bEnable);
- DWORD dwScrollBar = (nBar == SB_HORZ) ? WS_HSCROLL : 0;
- dwScrollBar |= (nBar == SB_VERT) ? WS_VSCROLL : 0;
- dwScrollBar |= (nBar == SB_BOTH) ? (WS_HSCROLL | WS_VSCROLL) : 0;
- _EnableScrollBarNotify((DWORD)dwScrollBar, bEnable ? (DWORD)dwScrollBar : 0);
- }
- else
- {
- CWnd::EnableScrollBarCtrl(nBar, bEnable);
- }
- }
- CScrollBar* CXTPSyntaxEditCtrl::GetScrollBarCtrl(int nBar) const
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- return pParentWnd->GetScrollBarCtrl(nBar);
- }
- return CWnd::GetScrollBarCtrl(nBar);
- }
- void CXTPSyntaxEditCtrl::RepositionBars(UINT nIDFirst, UINT nIDLast, UINT nIDLeftOver, UINT nFlag, LPRECT lpRectParam, LPCRECT lpRectClient, BOOL bStretch)
- {
- CWnd* pParentWnd = DYNAMIC_DOWNCAST(CView, m_pParentWnd);
- if (pParentWnd != NULL && IsCreateScrollbarOnParent())
- {
- pParentWnd->RepositionBars(nIDFirst, nIDLast, nIDLeftOver, nFlag, lpRectParam, lpRectClient, bStretch);
- }
- else
- {
- CWnd::RepositionBars(nIDFirst, nIDLast, nIDLeftOver, nFlag, lpRectParam, lpRectClient, bStretch);
- }
- }
- BOOL CXTPSyntaxEditCtrl::DeleteSelection()
- {
- if (!m_Selection.IsSelExist())
- return TRUE;
- int nFlags = (m_Selection.bBlockSelectionMode ? xtpEditTextAsBlock : 0) | xtpEditDispCol;
- if (!DeleteBuffer(m_Selection.GetNormalStart_disp().nLine, m_Selection.GetNormalStart_disp().nCol,
- m_Selection.GetNormalEnd_disp().nLine, m_Selection.GetNormalEnd_disp().nCol,
- nFlags))
- {
- return FALSE;
- }
- m_Selection.Reset_disp(m_Selection.GetNormalStart_disp().nLine, m_Selection.GetNormalStart_disp().nCol);
- SetCurrentDocumentRow(m_Selection.GetNormalStart_disp().nLine);
- EnsureVisibleRow(GetCurrentDocumentRow());
- SetCurCaretPos(GetCurrentDocumentRow(), m_Selection.GetStart_disp().nCol);
- Invalidate(FALSE);
- return TRUE;
- }
- BOOL CXTPSyntaxEditCtrl::DeleteBuffer(int iRowFrom, int iColFrom, int iRowTo, int iColTo,
- int nFlags )
- {
- int iTempRow1 = iRowFrom, iTempRow2 = iRowTo;
- int iTempCol1 = iColFrom, iTempCol2 = iColTo;
- if (iTempRow2 < iTempRow1)
- {
- iRowFrom = iTempRow2;
- iColFrom = iTempCol2;
- iRowTo = iTempRow1;
- iColTo = iTempCol1;
- }
- else if (iTempRow1 == iTempRow2)
- {
- iColFrom = min(iTempCol1, iTempCol2);
- iColTo = max(iTempCol1, iTempCol2);
- }
- if ((nFlags & xtpEditTextAsBlock) &&
- (iColTo < iColFrom))
- {
- // swap them
- int nTemp = iColTo;
- iColTo = iColFrom;
- iColFrom = nTemp;
- }
- if (!CanEditDoc())
- {
- return FALSE;
- }
- BOOL bDispCol = (nFlags & xtpEditDispCol) != 0;
- int nColFrom_str = bDispCol ? CalcAbsCol(iRowFrom, iColFrom) : iColFrom;
- int nColTo_str = bDispCol ? CalcAbsCol(iRowTo, iColTo) : iColTo;
- //**----------------------
- OnBeforeEditChanged(iRowFrom, nColFrom_str);
- //**----------------------
- ASSERT((nFlags & xtpEditTextAsBlock) && bDispCol || !(nFlags & xtpEditTextAsBlock));
- if ((nFlags & xtpEditTextAsBlock) && iRowFrom != iRowTo)
- {
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(TRUE);
- for (int i = iRowFrom; i <= iRowTo; i++)
- {
- m_pBuffer->DeleteText(i, iColFrom, i, iColTo, TRUE, bDispCol);
- }
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- m_pBuffer->GetUndoRedoManager()->SetLastCommandText(XTP_IDS_EDIT_DELETE_TEXT_BLOCK);
- }
- else
- {
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- m_pBuffer->DeleteText(iRowFrom, iColFrom, iRowTo, iColTo, TRUE, bDispCol);
- }
- LPCTSTR szLineText = GetLineText(iRowFrom);
- SetCurrentDocumentRow(iRowFrom);
- m_nCurrentCol = nColFrom_str;
- m_nDispCol = CalcDispCol(szLineText, m_nCurrentCol);
- //**----------------------
- OnEditChanged(iRowFrom, nColFrom_str, iRowTo, nColTo_str, xtpEditActDelete);
- //**----------------------
- UINT uFlags = XTP_EDIT_EDITACTION_DELETEROW | XTP_EDIT_EDITACTION_MODIFYROW;
- NotifyEditChanged(iRowFrom, iRowTo, uFlags);
- SetDocModified();
- RecalcScrollBars();
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol);
- if (nFlags & xtpEditRedraw)
- {
- Invalidate(FALSE);
- UpdateWindow();
- }
- return TRUE;
- }
- BOOL CXTPSyntaxEditCtrl::DeleteChar(int iRow, int iCol, XTPSyntaxEditDeletePos pos)
- {
- CString strText;
- int iRowTo = iRow, iColTo = iCol;
- if (pos == xtpEditDelPosAfter) // If deleting using DEL key
- {
- GetLineText(iRow, strText);
- int nLineLen = (int)_tcsclen(strText);
- if (iCol > nLineLen)
- {
- iRowTo = iRow + 1;
- iColTo = 1;
- if (iRowTo > GetRowCount())
- return FALSE;
- int nColFrom = nLineLen + 1;
- int nAdditionalTabs = 0;
- int nDispCol2 = CalcDispCol(strText, iCol);
- int nDispCol0 = CalcDispCol(strText, nColFrom);
- int nDC = nDispCol0 + GetTabSize() - (nDispCol0 -1) % GetTabSize();
- if (nDC <= nDispCol2)
- nAdditionalTabs++;
- else
- nDC = nDispCol0;
- for(; nDC + GetTabSize() <= nDispCol2; nDC += GetTabSize())
- nAdditionalTabs++;
- int nAdditionalSpaces = max(0, nDispCol2 - nDC);
- CString strTabs = CString(_T('t'), nAdditionalTabs) + CString(_T(' '), nAdditionalSpaces);
- //**----------------------
- OnBeforeEditChanged(iRow, nColFrom);
- XTP_EDIT_LINECOL lcFinal;
- m_pBuffer->InsertText(strTabs, iRow, nColFrom, TRUE, &lcFinal);
- m_pBuffer->GetUndoRedoManager()->SetLastCommandText(XTP_IDS_EDIT_FORMAT);
- OnEditChanged(iRow, nColFrom, iRow, lcFinal.nCol, xtpEditActInsert);
- //**----------------------
- iCol = lcFinal.nCol;
- }
- else
- iColTo++;
- }
- else // If deleting using back space
- {
- if (iCol == 1)
- {
- if (iRow > 1)
- {
- iRow--;
- // if we are in the end of collapsed block, expand it...
- CXTPSyntaxEditRowsBlockArray arCoBlocks;
- GetCollapsableBlocksInfo(iRow, arCoBlocks);
- int nCount = (int)arCoBlocks.GetSize();
- for(int i = 0; i < nCount; i++)
- {
- XTP_EDIT_ROWSBLOCK coBlk = arCoBlocks[i];
- if (coBlk.lcEnd.nLine == iRow) // Expand!
- {
- if (HasRowMark(coBlk.lcStart.nLine, xtpEditLMT_Collapsed))
- GetLineMarksManager()->DeleteLineMark(coBlk.lcStart.nLine, xtpEditLMT_Collapsed);
- }
- }
- //GetLineText(iRow, strText);
- //iCol = (int)_tcsclen(strText) + 1;
- iCol = m_pBuffer->GetLineTextLengthC(iRow) + 1;
- }
- else
- return FALSE;
- }
- else
- iCol--;
- }
- if (!DeleteBuffer(iRow, iCol, iRowTo, iColTo))
- return FALSE;
- return TRUE;
- }
- BOOL CXTPSyntaxEditCtrl::DoUndoRedo(int nActionsCount, BOOL bUndoRedo)
- {
- CWaitCursor wait;
- if (!CanEditDoc())
- return FALSE;
- CXTPSyntaxEditUndoRedoManager* pUndoMgr = m_pBuffer->GetUndoRedoManager();
- if (!pUndoMgr)
- return FALSE;
- m_Selection.Reset_disp(m_Selection.GetEnd_disp().nLine, m_Selection.GetEnd_disp().nCol);
- m_nAutoIndentCol = 0;
- int nCurDocRow = GetCurrentDocumentRow();
- XTP_EDIT_LINECOL lcTotalFrom = XTP_EDIT_LINECOL::MAXPOS;
- XTP_EDIT_LINECOL lcTotalTo = XTP_EDIT_LINECOL::MINPOS;
- for (int nAction = 0; nAction < nActionsCount; nAction++)
- {
- XTP_EDIT_LINECOL lcFrom, lcTo;
- // perform undo
- int nEditAction = bUndoRedo ?
- pUndoMgr->DoUndo(lcFrom, lcTo, this) :
- pUndoMgr->DoRedo(lcFrom, lcTo, this);
- // perform some total calculations
- lcTotalFrom = min(lcTotalFrom, lcFrom);
- lcTotalTo = max(lcTotalTo, lcTo);
- // TODO: ???????????????????????????????????
- // send update notifications
- // Commented out to eliminate double call of OnEditChanged that
- // leads to improper calculation of collapsible blocks borders
- // if (nEditAction & XTP_EDIT_EDITACTION_INSERTTEXT)
- // {
- // OnEditChanged(lcFrom, lcTo, xtpEditActInsert);
- // }
- // else if (nEditAction & XTP_EDIT_EDITACTION_DELETETEXT)
- // {
- // OnEditChanged(lcFrom, lcTo, xtpEditActDelete);
- // }
- NotifyEditChanged(lcFrom.nLine, lcTo.nLine, nEditAction);
- }
- int nRowsAffected = lcTotalTo.nLine - lcTotalFrom.nLine;
- m_nDispCol = CalcDispCol(GetCurrentDocumentRow(), m_nCurrentCol);
- RecalcScrollBars();
- if (GetCurrentDocumentRow() > GetRowCount())
- {
- SetCurrentDocumentRow(GetRowCount());
- m_nCurrentCol = m_nDispCol = 1;
- }
- nCurDocRow = GetCurrentDocumentRow();
- if (nActionsCount > 1)
- {
- ValidateCol(nCurDocRow, m_nDispCol, m_nCurrentCol);
- SetCurCaretPos(nCurDocRow, m_nDispCol, FALSE/*, FALSE*/);
- }
- nCurDocRow = GetCurrentDocumentRow();
- if ((nCurDocRow > 0 && m_nCurrentCol > 0) ||
- !(nRowsAffected == 0 && nActionsCount == 1))
- {
- SetCurCaretPos(nCurDocRow, m_nDispCol);
- }
- Invalidate(FALSE);
- UpdateWindow();
- UpdateScrollPos();
- SetDocModified(m_pBuffer->IsModified());
- return TRUE;
- }
- BOOL CXTPSyntaxEditCtrl::Undo(int nActionsCount)
- {
- if (CanUndo())
- {
- return DoUndoRedo(nActionsCount, TRUE);
- }
- return FALSE;
- }
- BOOL CXTPSyntaxEditCtrl::Redo(int nActionsCount)
- {
- if (CanRedo())
- {
- return DoUndoRedo(nActionsCount, FALSE);
- }
- return FALSE;
- }
- BOOL CXTPSyntaxEditCtrl::CanUndo()
- {
- if (m_pBuffer && m_pBuffer->GetUndoRedoManager())
- return CanEditDoc() && m_pBuffer->GetUndoRedoManager()->CanUndo();
- return FALSE;
- }
- BOOL CXTPSyntaxEditCtrl::CanRedo()
- {
- if (m_pBuffer && m_pBuffer->GetUndoRedoManager())
- return CanEditDoc() && m_pBuffer->GetUndoRedoManager()->CanRedo();
- return FALSE;
- }
- void CXTPSyntaxEditCtrl::Cut()
- {
- if (m_Selection.IsSelExist())
- {
- Copy();
- DeleteSelection();
- UpdateWindow();
- }
- }
- void CXTPSyntaxEditCtrl::Copy()
- {
- if (!m_Selection.IsSelExist())
- return;
- CWaitCursor wait;
- if (!OpenClipboard())
- {
- #ifdef _DEBUG
- LPVOID lpMsgBuf;
- ::FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- ::GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL
- );
- // Process any inserts in lpMsgBuf.
- // ...
- // Display the string.
- AfxMessageBox((LPCTSTR)lpMsgBuf, MB_OK | MB_ICONINFORMATION );
- // Free the buffer.
- ::LocalFree( lpMsgBuf );
- #endif
- return;
- }
- // prepare clipboard
- ::EmptyClipboard();
- // retrieve the text string from buffer
- CMemFile file(CalcAveDataSize(m_Selection.GetNormalStart_str().nLine,
- m_Selection.GetNormalEnd_str().nLine));
- BOOL bRes = m_pBuffer->GetBuffer(m_Selection.GetNormalStart_disp(),
- m_Selection.GetNormalEnd_disp(), file,
- m_Selection.bBlockSelectionMode, TRUE);
- if (!bRes)
- return;
- // Copy the buffer to clipboard
- DWORD dwBytes = (DWORD)file.GetLength();
- BYTE *pBytes = file.Detach();
- HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, dwBytes+sizeof(TCHAR));
- if (!hMem)
- return;
- LPVOID lpSource = (LPVOID)::GlobalLock(hMem);
- if (!lpSource)
- return;
- MEMCPY_S(lpSource, pBytes, dwBytes);
- ::ZeroMemory(((BYTE *)lpSource+dwBytes), sizeof(TCHAR));
- free(pBytes);
- UINT uCodePage = m_pBuffer->GetCodePage();
- #ifdef _UNICODE
- // Determine the byte requirement
- int nLen = ::WideCharToMultiByte(uCodePage, 0, (LPWSTR)lpSource, -1, NULL, 0, NULL, NULL);
- HGLOBAL hMBCSMem = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (nLen + 2)); // Maximum number of bytes is dwBytes
- if (!hMBCSMem)
- {
- CloseClipboard();
- return;
- }
- LPSTR lpMBCSSource = (LPSTR)::GlobalLock(hMBCSMem);
- if (!lpMBCSSource)
- return;
- int nBytes = ::WideCharToMultiByte(uCodePage, 0, (LPWSTR)lpSource, -1, lpMBCSSource, nLen, NULL, NULL);
- ASSERT(nBytes <= (int)dwBytes);
- lpMBCSSource[nBytes] = _T(' ');
- ::GlobalUnlock(hMem);
- ::GlobalUnlock(hMBCSMem);
- // lpSource is Unicode text
- ::SetClipboardData(CF_UNICODETEXT, hMem);
- ::SetClipboardData(CF_TEXT, hMBCSMem);
- ::SetClipboardData(CF_OEMTEXT, hMBCSMem);
- #else
- int nUBytes = (dwBytes+2) * sizeof(WCHAR);
- HGLOBAL hUnicodeMem = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, nUBytes);
- if (!hUnicodeMem)
- {
- CloseClipboard();
- return;
- }
- LPWSTR lpWSource = (LPWSTR)::GlobalLock(hUnicodeMem);
- if (!lpWSource)
- {
- CloseClipboard();
- return;
- }
- int nLen = ::MultiByteToWideChar(uCodePage, 0, (LPCSTR)lpSource, -1, lpWSource, nUBytes);
- ASSERT(nLen <= (int)(nUBytes/sizeof(WCHAR)));
- lpWSource[nLen] = _T(' ');
- ::GlobalUnlock(hMem);
- ::GlobalUnlock(hUnicodeMem);
- // lpSource is MBCS text
- ::SetClipboardData(CF_TEXT, hMem);
- ::SetClipboardData(CF_OEMTEXT, hMem);
- ::SetClipboardData(CF_UNICODETEXT, hUnicodeMem);
- #endif
- if (m_Selection.bBlockSelectionMode)
- {
- HGLOBAL hMem_BLK = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, 1);
- if (!hMem_BLK)
- {
- CloseClipboard();
- return;
- }
- BYTE* pMem_BLK = (BYTE*)::GlobalLock(hMem_BLK);
- if (pMem_BLK)
- {
- *pMem_BLK = 1;
- }
- ::GlobalUnlock(hMem_BLK);
- CLIPFORMAT uCF_MSDEVColumnSelect = (CLIPFORMAT)::RegisterClipboardFormat(XTP_EDIT_CFMSDEVCOLSEL);
- ::SetClipboardData(uCF_MSDEVColumnSelect, hMem_BLK);
- }
- CloseClipboard();
- }
- void CXTPSyntaxEditCtrl::Paste()
- {
- CWaitCursor wait;
- if (!OpenClipboard())
- return;
- BOOL bCBTextIsBlock = FALSE;
- CLIPFORMAT uCF_MSDEVColumnSelect = (CLIPFORMAT)::RegisterClipboardFormat(XTP_EDIT_CFMSDEVCOLSEL);
- HANDLE hMem_TextBlok = ::GetClipboardData(uCF_MSDEVColumnSelect);
- if (hMem_TextBlok)
- {
- int nSize = (int)::GlobalSize(hMem_TextBlok);
- BYTE* arColBlk = (BYTE*)::GlobalLock(hMem_TextBlok);
- if (arColBlk && nSize)
- {
- bCBTextIsBlock = TRUE;
- //TRACE(_T("MSDEVColumnSelect data [size=%d] = %d n"), nSize, (int)arColBlk[0]);
- }
- }
- #ifdef _UNICODE
- HANDLE hMem = ::GetClipboardData(CF_UNICODETEXT);
- #else
- HANDLE hMem = ::GetClipboardData(CF_TEXT);
- #endif
- if (!hMem)
- {
- return;
- }
- LPTSTR szData = (LPTSTR)::GlobalLock(hMem);
- BOOL bReplace = GetTabWithSpace();
- BOOL bOverwrite = m_pBuffer->GetOverwriteFlag();
- SetTabWithSpace(FALSE);
- m_pBuffer->SetOverwriteFlag(FALSE);
- if (bCBTextIsBlock)
- {
- InsertTextBlock(szData, GetCurrentDocumentRow(), m_nCurrentCol, m_Selection.IsSelExist());
- }
- else
- {
- InsertString(szData, GetCurrentDocumentRow(), m_nCurrentCol, m_Selection.IsSelExist());
- }
- SetTabWithSpace(bReplace);
- m_pBuffer->SetOverwriteFlag(bOverwrite);
- m_pBuffer->GetUndoRedoManager()->SetLastCommandText(XTP_IDS_EDIT_PASTE);
- ::GlobalUnlock(hMem);
- CloseClipboard();
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol);
- SetDocModified();
- UpdateScrollPos(XTP_EDIT_UPDATE_HORZ|XTP_EDIT_UPDATE_VERT);
- UpdateWindow();
- }
- void CXTPSyntaxEditCtrl::SetRowText(int nRow, LPCTSTR pcszText, BOOL bCanUndo)
- {
- if (!m_pBuffer)
- return;
- int nRowLen = m_pBuffer->GetLineTextLengthC(nRow, FALSE);
- if (nRowLen)
- {
- //**----------------------
- OnBeforeEditChanged(nRow, 1);
- //**----------------------
- if (bCanUndo)
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- m_pBuffer->DeleteText(nRow, 1, nRow, nRowLen + 1, bCanUndo, FALSE);
- //**----------------------
- OnEditChanged(nRow, 1, nRow + 1, 1, xtpEditActDelete);
- //**----------------------
- }
- //**----------------------
- OnBeforeEditChanged(nRow, 1);
- //**----------------------
- if (bCanUndo)
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- //XTP_EDIT_LINECOL finalLC;
- m_pBuffer->InsertText(pcszText, nRow, 1, bCanUndo);//finalLC);
- //**----------------------
- OnEditChanged(nRow, 1, nRow + 1, 1, xtpEditActInsert);
- //**----------------------
- NotifyEditChanged(nRow, nRow, XTP_EDIT_EDITACTION_MODIFYROW);
- }
- void CXTPSyntaxEditCtrl::InsertRow(int nRow, LPCTSTR pcszText, BOOL bCanUndo)
- {
- if (!m_pBuffer)
- return;
- CString strLine = pcszText;
- strLine += m_pBuffer->GetCurCRLF();
- //**----------------------
- OnBeforeEditChanged(nRow, 1);
- //**----------------------
- if (bCanUndo)
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- //XTP_EDIT_LINECOL finalLC;
- m_pBuffer->InsertText(pcszText, nRow, 1, bCanUndo);//finalLC);
- //**----------------------
- OnEditChanged(nRow, 1, nRow + 1, 1, xtpEditActInsert);
- //**----------------------
- NotifyEditChanged(nRow, nRow, XTP_EDIT_EDITACTION_INSERTROW);
- }
- void CXTPSyntaxEditCtrl::RemoveRow(int nRow, BOOL bCanUndo)
- {
- if (!m_pBuffer)
- return;
- //**----------------------
- OnBeforeEditChanged(nRow, 1);
- //**----------------------
- if (bCanUndo)
- m_pBuffer->GetUndoRedoManager()->SetGroupInsertMode(FALSE);
- m_pBuffer->RemoveLine(nRow, bCanUndo);
- //**----------------------
- OnEditChanged(nRow, 1, nRow + 1, 1, xtpEditActDelete);
- //**----------------------
- NotifyEditChanged(nRow, nRow, XTP_EDIT_EDITACTION_DELETEROW);
- }
- void CXTPSyntaxEditCtrl::OnSetFocus(CWnd* pOldWnd)
- {
- CXTPSmartPtrInternalT<CCmdTarget> ptrThisLock(this, TRUE);
- // TRACE (_T("CXTPSyntaxEditCtrl::OnSetFocus. this=%x, pOldWnd=%x n"), this, pOldWnd);
- ::DestroyCaret();
- m_bFocused = TRUE;
- Invalidate(FALSE);
- RestoreCursor();
- NotifyCurRowCol(GetCurrentDocumentRow(), m_nDispCol);
- UpdateWindow();
- CWnd::OnSetFocus(pOldWnd);
- #ifdef XTP_SYNTAXEDIT_SITENOTIFY_ONFOCUS
- XTP_SYNTAXEDIT_SITENOTIFY_ONFOCUS(this, this, TRUE)
- #endif
- }
- void CXTPSyntaxEditCtrl::OnKillFocus(CWnd* pNewWnd)
- {
- CXTPSmartPtrInternalT<CCmdTarget> ptrThisLock(this, TRUE);
- CWnd::OnKillFocus(pNewWnd);
- ::DestroyCaret();
- m_pAutoComplete->Hide();
- m_bFocused = FALSE;
- #ifdef XTP_SYNTAXEDIT_SITENOTIFY_ONFOCUS
- XTP_SYNTAXEDIT_SITENOTIFY_ONFOCUS(this, this, FALSE)
- #endif
- }
- BOOL CXTPSyntaxEditCtrl::OnNcActivate(BOOL bActive)
- {
- return CWnd::OnNcActivate(bActive);
- }
- int CXTPSyntaxEditCtrl::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
- {
- // SetFocus();
- return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
- }
- BOOL CXTPSyntaxEditCtrl::IsSelectionExist()
- {
- return m_Selection.IsSelExist();
- }
- void CXTPSyntaxEditCtrl::SelectAll()
- {
- int nLastRow = GetRowCount();
- LPCTSTR szLineText = GetLineText(nLastRow);
- const int nEndCol = (int)_tcsclen(szLineText) + 1;
- SetCurrentDocumentRow(nLastRow);
- m_nCurrentCol = nEndCol;
- m_nDispCol = CalcDispCol(szLineText, m_nCurrentCol);
- m_Selection.Reset_disp(1, 1);
- m_Selection.SetEnd_disp(nLastRow, m_nDispCol);
- m_nAutoIndentCol = 0;
- SetCurCaretPos(GetCurrentDocumentRow(), m_nDispCol);
- }
- void CXTPSyntaxEditCtrl::SetFontIndirect(LPLOGFONT pLogfont, BOOL bUpdateReg/*=FALSE*/)
- {
- GetPaintManager()->CreateFontIndirect(pLogfont, bUpdateReg);
- CWindowDC dc(NULL);