/*!
  Create reverse change record of this one
  \return Reverse change record
*/
PX_ChangeRecord * PX_ChangeRecord::reverse(void) const
{
	PX_ChangeRecord * pcr = new PX_ChangeRecord(getRevType(),
												m_position,
												m_indexAP,
												m_iXID);
	UT_ASSERT_HARMLESS(pcr);
	pcr->setAdjustment( m_iAdjust);
	return pcr;
}
Beispiel #2
0
bool pt_PieceTable::undoCmd(void)
{
	// do a user-atomic undo.
	// return false if we can't.
	
	PX_ChangeRecord * pcr;
	if (!m_history.getUndo(&pcr))
		return false;
	UT_return_val_if_fail (pcr,false);

	// the first undo record tells us whether it is
	// a simple change or a glob.  there are two kinds
	// of globs: a multi-step change (display atomic)
	// like deleting a selection that spans a paragraph
	// break; and a user-atomic glob like doing a search
	// and replace over the whole document.
	//
	// for a simple change, we just do it and return.
	// for a glob, we loop until we do the
	// corresponding other end.
	m_history.setScanningUndoGLOB(false);
	UT_Byte flagsFirst = GETGLOBFLAGS(pcr);
	if(m_fragments.areFragsDirty())
	{
		m_fragments.cleanFrags();
	}
	do
	{
		PX_ChangeRecord * pcrRev = pcr->reverse(); // we must delete this.
		pcrRev->setAdjustment(pcr->getAdjustment());
		pcrRev->setDocument(getDocument());
		pcrRev->setCRNumber();
		UT_return_val_if_fail (pcrRev,false);
		UT_Byte flagsRev = GETGLOBFLAGS(pcrRev);
		bool bResult = _doTheDo(pcrRev, true);
		delete pcrRev;
		
		if (!bResult)
			return false;
		if (flagsRev == flagsFirst)		// stop when we have a matching end
			break;

	} while (m_history.getUndo(&pcr));
	m_history.setScanningUndoGLOB(false);
	m_pDocument->updateFields();
	return true;
}
bool px_ChangeHistory::getRedo(PX_ChangeRecord ** ppcr) const
{
	if ((m_iAdjustOffset == 0) && (m_undoPosition >= m_vecChangeRecords.getItemCount()))
		return false;
	
	if (m_bOverlap)
		return false;
	
	UT_sint32 iRedoPos = m_undoPosition-m_iAdjustOffset;
	if(iRedoPos <0)
		return false;
	PX_ChangeRecord * pcr = m_vecChangeRecords.getNthItem(iRedoPos);
	UT_return_val_if_fail(pcr, false);

	// leave records from external documents in place so we can correct
	bool bIncrementAdjust = false;

	if (pcr->isFromThisDoc())
	{
		*ppcr = pcr;
		if (m_iAdjustOffset == 0)
		{
		     return true;
		}
		else
		{
		     bIncrementAdjust = true;
		     m_iAdjustOffset--;
		}
	}
	
	while (pcr && !pcr->isFromThisDoc() && (m_iAdjustOffset > 0))
	{
	    pcr = m_vecChangeRecords.getNthItem(iRedoPos);
	    m_iAdjustOffset--;
		iRedoPos++;
	    bIncrementAdjust = true;
	    xxx_UT_DEBUGMSG(("AdjustOffset decremented -1 %d ", m_iAdjustOffset));
	}
	
	if (pcr && bIncrementAdjust)
	{
	    PX_ChangeRecord * pcrOrig = pcr;
	    pcr->setAdjustment(0);
	    PT_DocPosition low,high;
	    getCRRange(pcr,low,high);
	    PT_DocPosition pos = pcr->getPosition();
	    UT_sint32 iAdj = 0;
	    for (UT_sint32 i = m_iAdjustOffset; i >= 1;i--)
	    {
			pcr = m_vecChangeRecords.getNthItem(m_undoPosition-i);
			if (!pcr->isFromThisDoc())
			{
				UT_sint32 iCur = getDoc()->getAdjustmentForCR(pcr);
			    if (pcr->getPosition() <= static_cast<PT_DocPosition>(static_cast<UT_sint32>(pos) + iAdj + iCur))
			    {
					iAdj += iCur; 
					low += iCur;
					high += iCur;
			    }
				PT_DocPosition p1,p2;
				getCRRange(pcr,p1,p2);
				bool bZero = (p1 == p2);
				if(bZero)
					m_bOverlap = doesOverlap(pcr,low+1,high);
				else
					m_bOverlap = doesOverlap(pcr,low,high);
			    if (m_bOverlap)
			    {
					*ppcr = NULL;
					return false;
			    }
			}
	    }
	    pcr = pcrOrig;
	    pcr->setAdjustment(iAdj);
	    xxx_UT_DEBUGMSG(("Redo Adjustment set to %d \n",iAdj));
	}
	
	if (pcr && pcr->isFromThisDoc())
	{  
	    *ppcr = pcr;
	    if(bIncrementAdjust)
	    {
	        m_iAdjustOffset += 1; // for didRedo
	        xxx_UT_DEBUGMSG(("AdjustOffset incremented -2 %d \n", m_iAdjustOffset));
	    }
	    return true;
	}

	*ppcr = NULL;
	return false;
}
bool px_ChangeHistory::getUndo(PX_ChangeRecord ** ppcr, bool bStatic) const
{
	if (m_bOverlap)
	{
		*ppcr = NULL;
		return false;
	}
	UT_sint32 iGLOB = 0;
	bool bGotOne = false;
	PX_ChangeRecord * pcr = NULL;
	PX_ChangeRecord * pcrFirst = NULL;
	bool bCorrect = false;
	UT_sint32 iAdjust = m_iAdjustOffset;
	UT_sint32 iLoop = 0;
	//	_printHistory(50);
	while (!bGotOne)
	{
		if ((m_undoPosition - m_iAdjustOffset -iLoop) <= m_iMinUndo)
		{
			if (bStatic)
				m_iAdjustOffset = iAdjust;
			return false;
		}
		
		pcr = m_vecChangeRecords.getNthItem(m_undoPosition-m_iAdjustOffset-1-iLoop);
		UT_return_val_if_fail(pcr, false); // just bail out, everything seems wrong

		//
		// Do Adjustments for blocks of remote CR's. Scan through local globs
		// to check for remote CR's which overlap it.
		//
		if((iGLOB== 0) && !pcr->isFromThisDoc())
		{
			bCorrect = true;
			m_iAdjustOffset++;
			UT_DEBUGMSG(("Doing undo iAdjust incremented to %d \n",m_iAdjustOffset));
		}
		else if ((iGLOB==0) && (pcr->getType() == PX_ChangeRecord::PXT_GlobMarker) && pcr->isFromThisDoc() && !isScanningUndoGLOB() && (m_iAdjustOffset > 0))
		{
			iGLOB++;
			pcrFirst = pcr;
			iLoop++;
			setScanningUndoGLOB(true);
		}
		else if((iGLOB>0) && (pcr->getType() == PX_ChangeRecord::PXT_GlobMarker) &&  pcr->isFromThisDoc())
		{
			if(isScanningUndoGLOB())
				pcr = pcrFirst;
			bGotOne = true;
		}
		else if(iGLOB == 0)
		{
			bGotOne = true;
			if(m_iAdjustOffset > 0)
				bCorrect = true;
		}
		//
		// we're here if we've started scanning through a glob in the local
		// document to see if it overlaps a later remote change.
		//
		else
		{
			PT_DocPosition low, high;
			PT_DocPosition lowWork = 0;
            PT_DocPosition highWork;
			UT_sint32 iAccumOffset = 0;
			getCRRange(pcr, low, high);
			for (UT_sint32 i = 0; i<m_iAdjustOffset;i++)
			{
				PX_ChangeRecord *pcrTmp = m_vecChangeRecords.getNthItem(m_undoPosition-i-1);
				if (!pcrTmp->isFromThisDoc())
				{
					UT_sint32 iCur = getDoc()->getAdjustmentForCR(pcrTmp);
					if(pcrTmp->getPosition() <= lowWork+iCur)
					{
						iAccumOffset += iCur;
					}
					lowWork = low + iAccumOffset;
					highWork = high + iAccumOffset;
					PT_DocPosition p1,p2;
					getCRRange(pcrTmp,p1,p2);
					bool bZero = (p1 == p2);
					if(bZero)
						lowWork++;
					if (doesOverlap(pcrTmp,lowWork,highWork))
					{
						*ppcr = NULL;
						//
						// OK now we have to invalidate the undo stack
						// to just before the first pcr we pulled off.
						//
						if(m_undoPosition-iAdjust > 0)
						{
							m_iMinUndo = m_undoPosition-iAdjust-1;
						}
						else
						{
							m_iMinUndo = 0;
						}
						m_iAdjustOffset = iAdjust;
						m_iAdjustOffset++;
						return false;
					}
				}
			}
			
			iLoop++;
		}
	}

	PX_ChangeRecord * pcrOrig = pcr;
	if (bCorrect)
	{
	    pcr->setAdjustment(0);
	    PT_DocPosition pos = pcr->getPosition();
	    UT_sint32 iAdj = 0;
		UT_sint32 iCurrAdj  = 0;
	    PT_DocPosition low, high;
	    getCRRange(pcr, low, high);
	    for (UT_sint32 i = m_iAdjustOffset-1; i>=0;i--)
	    {
			pcr = m_vecChangeRecords.getNthItem(m_undoPosition-i-1);
			if (!pcr->isFromThisDoc())
			{
				iCurrAdj = getDoc()->getAdjustmentForCR(pcr);
			    if(pcr->getPosition() <= static_cast<PT_DocPosition>(static_cast<UT_sint32>(pos) + iAdj + iCurrAdj))
			    {
					iAdj += iCurrAdj;
					low += iCurrAdj;
					high += iCurrAdj;
			    }
				PT_DocPosition p1,p2;
				getCRRange(pcr,p1,p2);
				bool bZero = (p1 == p2);
				PT_DocPosition low1 = low;
				if(bZero)
					low1++;
			    if (doesOverlap(pcr,low1,high))
			    {
					UT_DEBUGMSG(("CR Type %d adj pos %d Overlaps found with CR pos %d \n",pcrOrig->getType(),pcrOrig->getPosition()+iAdj,pcr->getPosition()));
					UT_DEBUGMSG((" Orig Adj low %d high %d \n",low,high));

					*ppcr = NULL;
					m_iMinUndo = m_undoPosition-m_iAdjustOffset-1;
					return false;
			    }
			}
	    }
	    pcrOrig->setAdjustment(iAdj);
	    m_iAdjustOffset++;
	}

	UT_ASSERT(pcrOrig->isFromThisDoc());
	*ppcr = pcrOrig;
	if(bStatic)
	    m_iAdjustOffset = iAdjust;
	return true;
}