ccrystaltextview.cpp

来自「用bcg库编写的java IDE 源码」· C++ 代码 · 共 2,117 行 · 第 1/5 页

CPP
2,117
字号
		  {
             VERIFY (reg1.SaveString (_T ("Extensions"), def->exts));
             VERIFY (reg1.SaveNumber (_T ("Flags"), def->flags));
             VERIFY (reg1.SaveNumber (_T ("TabSize"), def->tabsize));
             VERIFY (reg1.SaveString (_T ("OpenComment"), def->opencomment));
             VERIFY (reg1.SaveString (_T ("CloseComment"), def->closecomment));
             VERIFY (reg1.SaveString (_T ("CommentLine"), def->commentline));
             VERIFY (reg1.SaveNumber (_T ("DefaultEncoding"), def->encoding));
			 reg1.Close();
		  }
	  }
      VERIFY (reg.SaveBinary (_T ("LogFont"), (LPBYTE) &m_LogFont, sizeof (m_LogFont)));
  }
}

CCrystalTextView::CCrystalTextView ()
{
   AFX_ZERO_INIT_OBJECT(CView);
   m_rxnode = NULL;
   m_pszMatched = NULL;
   m_bSelMargin = TRUE;
   m_bWordWrap = FALSE;

   m_panSubLines = new CArray<int,int>();
   ASSERT(m_panSubLines);
   m_panSubLines->SetSize(0,4096 );

   m_pstrIncrementalSearchString = new CString;
   ASSERT(m_pstrIncrementalSearchString);
   m_pstrIncrementalSearchStringOld = new CString;
   ASSERT(m_pstrIncrementalSearchStringOld);

   ResetView();
   SetTextType(SRC_PLAIN);
   m_bSingle = false; // needed to be set in descendat classes
   m_bRememberLastPos = false;



   COLORREF nColors[24][2] =
   {
	{0x000000,0xffffff},//TEXT_COLOR
    {0xffffff,0x000000},//TEXT_SELECTION_COLOR
    {0xFFFFFF,0xffffff},//CUR_ERR_TAG_COLOR
    {0x800000,0xffffff},//BOOK_MARK_COLOR
    {0x008000,0xffffff},//BREAK_POINT_COLOR
    {0x228b22,0xffffff},//CUR_STATEMENT_COLOR
    {0x4b0082,0xffffff},//SEL_MARGIN_COLOR
    {0xff0000,0xffffff},//KEYWORD_COLOR
    {0x00ff00,0xffffff},//COMMENT_COLOR
    {0xa9a9a9,0xffffff},//NUMBER_COLOR
    {0x0000ff,0xffffff},//STRING_COLOR
    {0xff4500,0xffffff},//OPERATOR_COLOR
    {0x00ff7f,0xffffff},//HTML_ELENAME_COLOR
    {0xffff00,0xffffff},//HTML_ATTRNAME_COLOR
    {0x4169e1,0xffffff},//HTML_ATTRVAL_COLOR
    {0xee82ee,0xffffff},//HTML_COMMENT_COLOR
    {0xb0e0e6,0xffffff},//HTML_ENTITY_COLOR
    {0xc21a84,0xffffff},//HTML_TAG_DEL_COLOR
    {0xbde245,0xffffff},//HTML_STRING_COLOR
    {0x456378,0xffffff},//HTML_TAG_TEXT_COLOR
    {0x7c5e2f,0xffffff},//HTML_OPERATOR_COLOR
    {0x2ab35c,0xffffff},//HTML_SsS_COLOR
    {0x237954,0xffffff},//WIZARD_CODE_COLOR
    {0xff0000,0xffffff},//USER_DEFINED_KEYWD_COLOR
    //{0xff0000,0xffffff},//
  };

  for(int i=0; i<24; i++)
  {
    m_nColors[i][0] = nColors[i][0];
	m_nColors[i][1] = nColors[i][1];
  }
}

CCrystalTextView::~CCrystalTextView ()
{
  ASSERT (m_hAccel == NULL);
  ASSERT (m_pCacheBitmap == NULL);
  ASSERT (m_pTextBuffer == NULL);   //  Must be correctly detached

  if (m_pszLastFindWhat != NULL)
    free (m_pszLastFindWhat);
  if (m_pdwParseCookies != NULL)
    delete m_pdwParseCookies;
  if (m_pnActualLineLength != NULL)
    delete m_pnActualLineLength;
  if (m_rxnode)
    RxFree (m_rxnode);
  if (m_pszMatched)
    delete m_pszMatched;
	//BEGIN SW
	if( m_panSubLines )
		delete m_panSubLines;
	if( m_pstrIncrementalSearchString )
		delete m_pstrIncrementalSearchString;
	if( m_pstrIncrementalSearchStringOld )
		delete m_pstrIncrementalSearchStringOld;
	//END SW
}

BOOL CCrystalTextView::PreCreateWindow (CREATESTRUCT & cs)
{
  CWnd *pParentWnd = CWnd::FromHandlePermanent(cs.hwndParent);
  if(pParentWnd == NULL || !pParentWnd->IsKindOf(RUNTIME_CLASS(CSplitterWnd)))
  {
     //View must always create its own scrollbars,
     //if only it's not used within splitter
	 if(m_bWordWrap)// we do not need a horizontal scroll bar, if we wrap the lines
		cs.style|= WS_VSCROLL;
	 else
		cs.style |= (WS_HSCROLL | WS_VSCROLL);
		/*ORIGINAL
		cs.style |= (WS_HSCROLL | WS_VSCROLL);
		*/
  }
  cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS);
  return CView::PreCreateWindow (cs);
}


/////////////////////////////////////////////////////////////////////////////
// CCrystalTextView drawing

void CCrystalTextView::GetSelection (CPoint & ptStart, CPoint & ptEnd)
{
  PrepareSelBounds();
  ptStart = m_ptDrawSelStart;
  ptEnd   = m_ptDrawSelEnd;
}

CCrystalTextBuffer *CCrystalTextView::LocateTextBuffer ()
{
  return NULL;
}

int CCrystalTextView::GetLineActualLength (int nLineIndex)
{
  int nLineCount = GetLineCount ();
  ASSERT (nLineCount > 0);
  ASSERT (nLineIndex >= 0 && nLineIndex < nLineCount);

  if(m_pnActualLineLength == NULL)
  {
     m_pnActualLineLength = new int[nLineCount];
     memset (m_pnActualLineLength, 0xff, sizeof (int) * nLineCount);
     m_nActualLengthArraySize = nLineCount;
  }

  if(m_pnActualLineLength[nLineIndex] >= 0)
    return m_pnActualLineLength[nLineIndex];

  //  Actual line length is not determined yet, let's calculate a little
  int nActualLength = 0;
  int nLength = GetLineLength(nLineIndex);

  if(nLength > 0)
  {
      LPCTSTR pszLine = GetLineChars (nLineIndex);
      LPTSTR pszChars = (LPTSTR) _alloca (sizeof (TCHAR) * (nLength + 1));
      memcpy (pszChars, pszLine, sizeof (TCHAR) * nLength);
      pszChars[nLength] = 0;
      LPTSTR pszCurrent = pszChars;

      int nTabSize = GetTabSize ();
      for(;;)
	  {
          LPTSTR psz = _tcschr (pszCurrent, _T('\t'));
          if(psz == NULL)
		  {
            nActualLength += (pszChars + nLength - pszCurrent);
            break;
          }

          nActualLength += (psz - pszCurrent);
          nActualLength += (nTabSize - nActualLength % nTabSize);
          pszCurrent = psz + 1;
	  }
    }

  m_pnActualLineLength[nLineIndex] = nActualLength;
  return nActualLength;
}

void CCrystalTextView::ScrollToChar (int  nNewOffsetChar, 
									 BOOL bNoSmoothScroll /*= FALSE*/ , 
									 BOOL bTrackScrollBar /*= TRUE*/ )
{
	// no horizontal scrolling, when word wrapping is enabled
  if(m_bWordWrap)
	  return;
	
  //  For now, ignoring bNoSmoothScroll and m_bSmoothScroll
  if(m_nOffsetChar != nNewOffsetChar)
  {
      int nScrollChars = m_nOffsetChar - nNewOffsetChar;
      m_nOffsetChar = nNewOffsetChar;
      
	  CRect rcScroll;
      GetClientRect (&rcScroll);
      rcScroll.left += GetMarginWidth();

      ScrollWindow(nScrollChars * GetCharWidth(), 0, &rcScroll, &rcScroll);
      UpdateWindow ();
      
	  if(bTrackScrollBar)
        RecalcHorzScrollBar(TRUE);
  }
}

//BEGIN SW
void CCrystalTextView::ScrollToSubLine( int nNewTopSubLine, 
									BOOL bNoSmoothScroll /*= FALSE*/, BOOL bTrackScrollBar /*= TRUE*/ )
{
	if(m_nTopSubLine != nNewTopSubLine)
	{
		if(bNoSmoothScroll || ! m_bSmoothScroll)
		{
			int nScrollLines = m_nTopSubLine - nNewTopSubLine;
			m_nTopSubLine = nNewTopSubLine;
			ScrollWindow(0, nScrollLines * GetLineHeight());
			
			UpdateWindow();
			
			if(bTrackScrollBar)RecalcVertScrollBar(TRUE);
		}
		else
		{
			//	Do smooth scrolling
			int nLineHeight = GetLineHeight();
			if(m_nTopSubLine > nNewTopSubLine)
			{
				int nIncrement = (m_nTopSubLine - nNewTopSubLine) / SMOOTH_SCROLL_FACTOR + 1;
				while(m_nTopSubLine != nNewTopSubLine)
				{
					int nTopSubLine = m_nTopSubLine - nIncrement;
					if (nTopSubLine < nNewTopSubLine)
						nTopSubLine = nNewTopSubLine;
					int nScrollLines = nTopSubLine - m_nTopSubLine;
					m_nTopSubLine = nTopSubLine;
					ScrollWindow(0, - nLineHeight * nScrollLines);
					UpdateWindow();
					if (bTrackScrollBar)
						RecalcVertScrollBar(TRUE);
				}
			}
			else
			{
				int nIncrement = (nNewTopSubLine - m_nTopSubLine) / SMOOTH_SCROLL_FACTOR + 1;
				while (m_nTopSubLine != nNewTopSubLine)
				{
					int nTopSubLine = m_nTopSubLine + nIncrement;
					if (nTopSubLine > nNewTopSubLine)
						nTopSubLine = nNewTopSubLine;
					int nScrollLines = nTopSubLine - m_nTopSubLine;
					m_nTopSubLine = nTopSubLine;
					ScrollWindow(0, - nLineHeight * nScrollLines);
					UpdateWindow();
					if (bTrackScrollBar)
						RecalcVertScrollBar(TRUE);
				}
			}
		}
		int nDummy;
		GetLineBySubLine( m_nTopSubLine, m_nTopLine, nDummy );
		InvalidateRect( NULL );	// repaint whole window
	}
}


void CCrystalTextView::ScrollToLine (int nNewTopLine, BOOL bNoSmoothScroll 
									 /*= FALSE*/ , BOOL bTrackScrollBar /*= TRUE*/ )
{
	//BEGIN SW
	if( m_nTopLine != nNewTopLine )
		ScrollToSubLine( GetSubLineIndex( nNewTopLine ), bNoSmoothScroll, bTrackScrollBar );

	/*ORIGINAL
	if (m_nTopLine != nNewTopLine)
	{
		if (bNoSmoothScroll || ! m_bSmoothScroll)
		{
			int nScrollLines = m_nTopLine - nNewTopLine;
			m_nTopLine = nNewTopLine;
			ScrollWindow(0, nScrollLines * GetLineHeight());
			UpdateWindow();
			if (bTrackScrollBar)
				RecalcVertScrollBar(TRUE);
		}
		else
		{
			//	Do smooth scrolling
			int nLineHeight = GetLineHeight();
			if (m_nTopLine > nNewTopLine)
			{
				int nIncrement = (m_nTopLine - nNewTopLine) / SMOOTH_SCROLL_FACTOR + 1;
				while (m_nTopLine != nNewTopLine)
				{
					int nTopLine = m_nTopLine - nIncrement;
					if (nTopLine < nNewTopLine)
						nTopLine = nNewTopLine;
					int nScrollLines = nTopLine - m_nTopLine;
					m_nTopLine = nTopLine;
					ScrollWindow(0, - nLineHeight * nScrollLines);
					UpdateWindow();
					if (bTrackScrollBar)
						RecalcVertScrollBar(TRUE);
				}
			}
			else
			{
				int nIncrement = (nNewTopLine - m_nTopLine) / SMOOTH_SCROLL_FACTOR + 1;
				while (m_nTopLine != nNewTopLine)
				{
					int nTopLine = m_nTopLine + nIncrement;
					if (nTopLine > nNewTopLine)
						nTopLine = nNewTopLine;
					int nScrollLines = nTopLine - m_nTopLine;
					m_nTopLine = nTopLine;
					ScrollWindow(0, - nLineHeight * nScrollLines);
					UpdateWindow();
					if (bTrackScrollBar)
						RecalcVertScrollBar(TRUE);
				}
			}
		}
	}
	*///END SW
}

void CCrystalTextView::ExpandChars (LPCTSTR pszChars, int nOffset, int nCount,
									                               CString & line)
{
  if(nCount <= 0)
  {
    line = _T ("");
    return;
  }

  int nTabSize = GetTabSize();

  int nActualOffset = 0;
  for(int I = 0; I < nOffset; I++)
  {
     if(pszChars[I] == _T ('\t'))
        nActualOffset += (nTabSize - nActualOffset % nTabSize);
     else
        nActualOffset++;
  }

  pszChars += nOffset;
  int nLength = nCount;

  int nTabCount = 0;
  for(I = 0; I < nLength; I++)
  {
     if(pszChars[I] == _T ('\t'))
        nTabCount++;
  }

  LPTSTR pszBuf = line.GetBuffer (nLength + nTabCount * (nTabSize - 1) + 1);
  int nCurPos = 0;

  if(nTabCount > 0 || m_bViewTabs)
  {
      for(I = 0; I < nLength; I++)
	  {
          if(pszChars[I] == _T ('\t'))
		  {
              int nSpaces = nTabSize - (nActualOffset + nCurPos) % nTabSize;
              if(m_bViewTabs)
			  {
                  pszBuf[nCurPos++] = TAB_CHARACTER;
                  nSpaces--;
              }
              
			  while(nSpaces > 0)
              {
                 pszBuf[nCurPos++] = _T (' ');
                 nSpaces--;
              }
		  }
          else
          {
             if(pszChars[I] == _T (' ') && m_bViewTabs)
                pszBuf[nCurPos] = SPACE_CHARACTER;
              else
                pszBuf[nCurPos] = pszChars[I];
              nCurPos++;
          }
	  }
  }
  else
  {
     memcpy (pszBuf, pszChars, sizeof (TCHAR) * nLength);
     nCurPos = nLength;
  }
  pszBuf[nCurPos] = 0;
  line.ReleaseBuffer ();
}

void CCrystalTextView::DrawLineHelperImpl(CDC * pdc,CPoint & ptOrigin,
										  const CRect & rcClip,LPCTSTR pszChars,
										  int nOffset, int nCount)
{
  ASSERT (nCount >= 0);
  if(nCount > 0)
  {
     CString line;
     ExpandChars(pszChars,nOffset,nCount,line);
     int nWidth = rcClip.right - ptOrigin.x;

     if(nWidth > 0)
	 {
        int nCharWidth = GetCharWidth ();
        int nCount = line.GetLength ();
        int nCountFit = nWidth / nCharWidth + 1;
        if (nCount > nCountFit)
            nCount = nCountFit;

        VERIFY(pdc->ExtTextOut(ptOrigin.x,ptOrigin.y,ETO_CLIPPED,&rcClip,line,nCount,NULL));
	 }
     ptOrigin.x += GetCharWidth () * line.GetLength ();
  }

⌨️ 快捷键说明

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