basetextbuffer.cpp

来自「用bcg库编写的java IDE 源码」· C++ 代码 · 共 338 行

CPP
338
字号
// BaseTextBuffer.cpp: implementation of the CBaseTextBuffer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BaseTextBuffer.h"
#include "browser.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//IMPLEMENT_DYNCREATE (CBaseTextBuffer,CCrystalTextBuffer)
#define     CHAR_ALIGN                  16
#define     ALIGN_BUF_SIZE(size)        ((size) / CHAR_ALIGN) * CHAR_ALIGN + CHAR_ALIGN;


CBaseTextBuffer::CBaseTextBuffer()
{
}

CBaseTextBuffer::~CBaseTextBuffer()
{

}


void  CBaseTextBuffer::addNode(CBlockDecl* pBlock)
{return;

   ASSERT(pBlock->GetBound()->m_nEndLine<GetLineCount());
   SLineInfoExt* line = __line(pBlock->GetBound()->m_nEndLine);

   __DeclBound nNewBound = *(pBlock->GetBound());

   for(POSITION pos=line->m_lpNodes->GetHeadPosition(); pos!=NULL;)
   {
	  POSITION cp = pos;
      __DeclBound nDeclBound = *(line->m_lpNodes->GetNext(pos)->GetBound());
      if(nDeclBound>nNewBound)
	  {
        line->m_lpNodes->InsertBefore(cp,pBlock);
	  }
   }line->m_lpNodes->AddTail(pBlock);
}



void CBaseTextBuffer::removeNode(CBlockDecl* pBlock)
{
  POSITION pos =  __line(pBlock->GetBound()->m_nEndLine)->m_lpNodes->Find(pBlock);
  if(pos !=NULL)
    __line(pBlock->GetBound()->m_nEndLine)->m_lpNodes->RemoveAt(pos);
}



void CBaseTextBuffer::_expand(int nLine,int nKeyIndex)
{
  ASSERT(nLine<GetLineCount());
  SLineInfoExt* line = __line(nLine);
  for(POSITION pos=line->m_lpNodes->GetHeadPosition(); pos!=NULL;)
  {
    CBlockDecl* pBlock = line->m_lpNodes->GetNext(pos);
	if(pBlock->GetBound()->m_nStartIndex == nKeyIndex)
	{
      pBlock->m_bExpanded = true;
	  return;
	}
  }
}

int CBaseTextBuffer::nodeIndexAt(int nLine,int nIndex)
{
  SLineInfoExt* line = __line(nLine);
  POSITION pos=line->m_lpNodes->GetHeadPosition();
  CBlockDecl* pBlock = NULL;

  for(int i = 0; i<nIndex; i++)
    pBlock = line->m_lpNodes->GetNext(pos);

  if(nIndex>0)
  return pBlock->GetBound()->m_nStartIndex;
  else
  return line->m_lpNodes->GetHead()->GetBound()->m_nStartIndex;
}

CBlockDecl* CBaseTextBuffer::nodeDeclAt(int nLine,int nIndex)
{
  SLineInfoExt* line = __line(nLine);
  POSITION pos=line->m_lpNodes->GetHeadPosition();
  CBlockDecl* pBlock = NULL;

  for(int i = 0; i<nIndex; i++)
    pBlock = line->m_lpNodes->GetNext(pos);

  if(nIndex>0)
  return pBlock;
  else
  return line->m_lpNodes->GetHead();
}

CBlockDecl* CBaseTextBuffer::nodeAtKey(int nLine,int nKeyIndex)
{
  SLineInfoExt* line = __line(nLine);
  POSITION pos=line->m_lpNodes->GetHeadPosition();
  CBlockDecl* pBlock = NULL;

  for(pos; pos !=NULL;)
  {
    pBlock = line->m_lpNodes->GetNext(pos);
	if(pBlock->GetBound()->m_nStartIndex == nKeyIndex)return pBlock;
  }return NULL;
}

int CBaseTextBuffer::nodeCount(int nLine)
{
  return __line(nLine)->m_lpNodes->GetCount();
}


void CBaseTextBuffer::_colapse(int nLine,int nKeyIndex)
{
  ASSERT(nLine<GetLineCount());
  SLineInfoExt* line = __line(nLine);
  for(POSITION pos=line->m_lpNodes->GetHeadPosition(); pos!=NULL;)
  {
    CBlockDecl* pBlock = line->m_lpNodes->GetNext(pos);
	if(pBlock->GetBound()->m_nStartIndex == nKeyIndex)
	{
      pBlock->m_bExpanded = false;
	  return;
	}
  }
}

BOOL CBaseTextBuffer::isExpanded(int nLine,int nKeyIndex)
{
  ASSERT(nLine<GetLineCount());
  SLineInfoExt* line = __line(nLine);
  for(POSITION pos=line->m_lpNodes->GetHeadPosition(); pos!=NULL;)
  {
    CBlockDecl* pBlock = line->m_lpNodes->GetNext(pos);
	if(pBlock->GetBound()->m_nStartIndex == nKeyIndex)
      return pBlock->m_bExpanded;
  }return false;
}

BOOL CBaseTextBuffer::isColapsed(int nLine,int nKeyIndex)
{
  ASSERT(nLine<GetLineCount());
  SLineInfoExt* line = __line(nLine);
  for(POSITION pos=line->m_lpNodes->GetHeadPosition(); pos!=NULL;)
  {
    CBlockDecl* pBlock = line->m_lpNodes->GetNext(pos);
	if(pBlock->GetBound()->m_nStartIndex == nKeyIndex)
      return (pBlock->m_bExpanded==false);
  }return false;
}

void CBaseTextBuffer::_toggle(int nLine,int nKeyIndex,
							         CCrystalTextView* pView /*=NULL*/)
{
  ASSERT(nLine<GetLineCount());

  CBlockDecl* pNode = nodeAtKey(nLine,nKeyIndex);

  if(pNode->m_bExpanded)
  {
	HideBlockText(pView,pNode);
    HideBlockDecls(pNode);
    pNode->m_bExpanded = FALSE;	
  }
  else
  {
	ShowBlockText(pView,pNode);
	ShowBlockDecls(pNode);
    pNode->m_bExpanded = TRUE;
  }
}

void CBaseTextBuffer::UpdateParents(CBlockDecl* pDecl,int offset)
{
  __CBaseDecl* pParent = pDecl->getParent();
  while(pParent)
  {
    m_pDeclManager->updateBlock(pParent);
	pParent = pParent->getParent();
  }
}

void CBaseTextBuffer::HideBlockDecls(CBlockDecl* pBlock)
{   

	/**
   POSITION pos = pBlock->skipBlock();
   if(pos !=NULL)
   {
     //update coords
     __DeclBound nBound(pBlock->GetBound()->m_nEndIndex+1,
	                    pBlock->GetBound()->m_nEndLine,
	                    pBlock->GetBound()->m_nBlockEndIndex-1,
					    pBlock->GetBound()->m_nBlockEndLine);


     m_pDeclManager->UpdateCoordsOnDelete(pos,nBound);
   }
   //update the block bound of parent
   UpdateParents(pBlock,0);   
   **/
}

void CBaseTextBuffer::ShowBlockDecls(CBlockDecl* pBlock)
{   
	/**
   POSITION pos = pBlock->skipBlock();
   if(pos !=NULL)
   {
     //update coords
     __DeclBound nBound(pBlock->GetBound()->m_nEndIndex+1,
	                    pBlock->GetBound()->m_nEndLine,
	                    pBlock->GetBound()->m_nBlockEndIndex-1,
					    pBlock->GetBound()->m_nBlockEndLine);


     m_pDeclManager->UpdateCoordsOnInsert(pos,nBound);   
   }
   //update the block bound of parent
   UpdateParents(pBlock,0);   
   **/
}



void CBaseTextBuffer::HideBlockText(CCrystalTextView * pSource,CBlockDecl* pBlock)
{
	/**
  int nBlockStartLine   = pBlock->GetBound()->m_nEndLine;
  int nBlockStartIndex  = pBlock->GetBound()->m_nEndIndex;
  int nBlockEndIndex    = pBlock->GetBound()->m_nBlockEndIndex;
  int nBlockEndLine     = pBlock->GetBound()->m_nBlockEndLine;


  CDeleteContext context;
  context.m_ptStart.y = nBlockStartLine;
  context.m_ptStart.x = nBlockStartIndex;
  context.m_ptEnd.y   = nBlockEndLine;
  context.m_ptEnd.x   = nBlockEndIndex;
  
  SLineInfoExt* li = __line(nBlockStartLine);

  //remove all nodes from hidden block
  DECL_LIST* pList = pBlock->getMembers();
  for(POSITION pos = pList->GetHeadPosition(); pos !=NULL;)
  {
     __CBaseDecl* pDecl = pList->GetNext(pos);
	 if(ISBLOCK(pDecl->getKind()))
       removeNode((CBlockDecl*)pDecl);
  }

  //get and save text then delete
  CString strText;
  GetText(nBlockStartLine,nBlockStartIndex+1,nBlockEndLine,nBlockEndIndex,strText);
  li->SetText(strText);

  //do special delete
  InternalDeleteText(pSource,nBlockStartLine,nBlockStartIndex+1,
	                 nBlockEndLine,nBlockEndIndex);
  UpdateViews(pSource,&context,UPDATE_HORZRANGE|UPDATE_VERTRANGE,nBlockStartLine);
  **/

}

void CBaseTextBuffer::ShowBlockText(CCrystalTextView * pSource,CBlockDecl* pBlock)
{
	/**
   int nBlockStartLine   = pBlock->GetBound()->m_nEndLine;
   int nBlockStartIndex  = pBlock->GetBound()->m_nEndIndex;
   int nBlockEndIndex    = pBlock->GetBound()->m_nBlockEndIndex;
   int nBlockEndLine     = pBlock->GetBound()->m_nBlockEndLine;

   CInsertContext context;
   context.m_ptStart.x = nBlockStartIndex;
   context.m_ptStart.y = nBlockStartLine;

   SLineInfoExt* li = __line(nBlockStartLine);
   //do special insert
   if(li->m_hiddenInfo.length>0)
   {
     InternalInsertText(pSource,nBlockStartLine,nBlockStartIndex+1,
		               li->m_hiddenInfo.m_lszpHiddenText,
	                 nBlockEndLine,nBlockEndIndex);   
   }
   li->FreeText();

   //restore hidden nodes
   DECL_LIST* pList = pBlock->getMembers();
   for(POSITION pos = pList->GetHeadPosition(); pos !=NULL;)
   {
     __CBaseDecl* pDecl = pList->GetNext(pos);
	 if(ISBLOCK(pDecl->getKind()))
       addNode((CBlockDecl*)pDecl);
   }
  context.m_ptEnd.x = nBlockEndIndex;
  context.m_ptEnd.y = nBlockEndLine;


  if(nBlockEndLine>nBlockStartLine)
    UpdateViews(pSource,&context,UPDATE_HORZRANGE|UPDATE_VERTRANGE,nBlockStartLine);
  else
    UpdateViews(pSource,&context,UPDATE_SINGLELINE|UPDATE_HORZRANGE,nBlockStartLine);
	**/
}

int CBaseTextBuffer::AlignBuf(int nLength)
{
   int nMax = ALIGN_BUF_SIZE(nLength);
   ASSERT(nMax >= nLength);
   return nMax;
}


void CBaseTextBuffer::AddDeclToLine(int nLine,__CBaseDecl* pDecl)
{
  SLineInfoExt* pLi = (SLineInfoExt*) &m_aLines[nLine];
  pLi->m_lDecls.AddTail(pDecl);
}

void CBaseTextBuffer::RemoveDeclFromLine(int nLine,__CBaseDecl* pDecl)
{
  SLineInfoExt* pLi = (SLineInfoExt*) &m_aLines[nLine];
  pLi->m_lDecls.RemoveAt(pLi->m_lDecls.Find(pDecl));
}

⌨️ 快捷键说明

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