LCOV - code coverage report
Current view: top level - sw/source/core/undo - untblk.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 54 171 31.6 %
Date: 2014-11-03 Functions: 7 11 63.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <hintids.hxx>
      21             : #include <fmtanchr.hxx>
      22             : #include <frmfmt.hxx>
      23             : #include <doc.hxx>
      24             : #include <IDocumentUndoRedo.hxx>
      25             : #include <IDocumentRedlineAccess.hxx>
      26             : #include <IShellCursorSupplier.hxx>
      27             : #include <docary.hxx>
      28             : #include <swundo.hxx>
      29             : #include <pam.hxx>
      30             : #include <ndtxt.hxx>
      31             : #include <UndoCore.hxx>
      32             : #include <rolbck.hxx>
      33             : #include <redline.hxx>
      34             : 
      35          20 : SwUndoInserts::SwUndoInserts( SwUndoId nUndoId, const SwPaM& rPam )
      36             :     : SwUndo( nUndoId ), SwUndRng( rPam ),
      37             :     pTxtFmtColl( 0 ), pLastNdColl(0), pFrmFmts( 0 ), pRedlData( 0 ),
      38          20 :     bSttWasTxtNd( true ), nNdDiff( 0 ), nSetPos( 0 )
      39             : {
      40          20 :     pHistory = new SwHistory;
      41          20 :     SwDoc* pDoc = (SwDoc*)rPam.GetDoc();
      42             : 
      43          20 :     SwTxtNode* pTxtNd = rPam.GetPoint()->nNode.GetNode().GetTxtNode();
      44          20 :     if( pTxtNd )
      45             :     {
      46          10 :         pTxtFmtColl = pTxtNd->GetTxtColl();
      47             :         pHistory->CopyAttr( pTxtNd->GetpSwpHints(), nSttNode,
      48          10 :                             0, pTxtNd->GetTxt().getLength(), false );
      49          10 :         if( pTxtNd->HasSwAttrSet() )
      50           4 :             pHistory->CopyFmtAttr( *pTxtNd->GetpSwAttrSet(), nSttNode );
      51             : 
      52          10 :         if( !nSttCntnt )    // than take the Flys along
      53             :         {
      54           6 :             const size_t nArrLen = pDoc->GetSpzFrmFmts()->size();
      55           6 :             for( size_t n = 0; n < nArrLen; ++n )
      56             :             {
      57           0 :                 SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[n];
      58           0 :                 SwFmtAnchor const*const  pAnchor = &pFmt->GetAnchor();
      59           0 :                 const SwPosition* pAPos = pAnchor->GetCntntAnchor();
      60           0 :                 if (pAPos &&
      61           0 :                     (pAnchor->GetAnchorId() == FLY_AT_PARA) &&
      62           0 :                      nSttNode == pAPos->nNode.GetIndex() )
      63             :                 {
      64           0 :                     if( !pFrmFmts )
      65           0 :                         pFrmFmts = new std::vector<SwFrmFmt*>;
      66           0 :                     pFrmFmts->push_back( pFmt );
      67             :                 }
      68             :             }
      69             :         }
      70             :     }
      71             :     // consider Redline
      72          20 :     if( pDoc->getIDocumentRedlineAccess().IsRedlineOn() )
      73             :     {
      74           0 :         pRedlData = new SwRedlineData( nsRedlineType_t::REDLINE_INSERT, pDoc->getIDocumentRedlineAccess().GetRedlineAuthor() );
      75           0 :         SetRedlineMode( pDoc->getIDocumentRedlineAccess().GetRedlineMode() );
      76             :     }
      77          20 : }
      78             : 
      79             : // set destination after reading input
      80          20 : void SwUndoInserts::SetInsertRange( const SwPaM& rPam, bool bScanFlys,
      81             :                                     bool bSttIsTxtNd )
      82             : {
      83          20 :     const SwPosition* pTmpPos = rPam.End();
      84          20 :     nEndNode = pTmpPos->nNode.GetIndex();
      85          20 :     nEndCntnt = pTmpPos->nContent.GetIndex();
      86          20 :     if( rPam.HasMark() )
      87             :     {
      88          20 :         if( pTmpPos == rPam.GetPoint() )
      89          18 :             pTmpPos = rPam.GetMark();
      90             :         else
      91           2 :             pTmpPos = rPam.GetPoint();
      92             : 
      93          20 :         nSttNode = pTmpPos->nNode.GetIndex();
      94          20 :         nSttCntnt = pTmpPos->nContent.GetIndex();
      95             : 
      96          20 :         if( !bSttIsTxtNd )      // if a table selection is added ...
      97             :         {
      98           0 :             ++nSttNode;         // ... than the CopyPam is not fully correct
      99           0 :             bSttWasTxtNd = false;
     100             :         }
     101             :     }
     102             : 
     103          20 :     if( bScanFlys && !nSttCntnt )
     104             :     {
     105             :         // than collect all new Flys
     106          16 :         SwDoc* pDoc = (SwDoc*)rPam.GetDoc();
     107          16 :         const size_t nArrLen = pDoc->GetSpzFrmFmts()->size();
     108          68 :         for( size_t n = 0; n < nArrLen; ++n )
     109             :         {
     110          52 :             SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[n];
     111          52 :             SwFmtAnchor const*const pAnchor = &pFmt->GetAnchor();
     112          52 :             SwPosition const*const pAPos = pAnchor->GetCntntAnchor();
     113          80 :             if (pAPos &&
     114          80 :                 (pAnchor->GetAnchorId() == FLY_AT_PARA) &&
     115          28 :                 nSttNode == pAPos->nNode.GetIndex() )
     116             :             {
     117          12 :                 std::vector<SwFrmFmt*>::iterator it;
     118          36 :                 if( !pFrmFmts ||
     119          12 :                     pFrmFmts->end() == ( it = std::find( pFrmFmts->begin(), pFrmFmts->end(), pFmt ) ) )
     120             :                 {
     121             :                     ::boost::shared_ptr<SwUndoInsLayFmt> const pFlyUndo(
     122          12 :                         new SwUndoInsLayFmt(pFmt, 0, 0));
     123          12 :                     m_FlyUndos.push_back(pFlyUndo);
     124             :                 }
     125             :                 else
     126           0 :                     pFrmFmts->erase( it );
     127             :             }
     128             :         }
     129          16 :         delete pFrmFmts, pFrmFmts = 0;
     130             :     }
     131          20 : }
     132             : 
     133          40 : SwUndoInserts::~SwUndoInserts()
     134             : {
     135          20 :     if (m_pUndoNodeIndex) // delete also the section from UndoNodes array
     136             :     {
     137             :         // Insert saves content in IconSection
     138           0 :         SwNodes& rUNds = m_pUndoNodeIndex->GetNodes();
     139           0 :         rUNds.Delete(*m_pUndoNodeIndex,
     140           0 :             rUNds.GetEndOfExtras().GetIndex() - m_pUndoNodeIndex->GetIndex());
     141           0 :         m_pUndoNodeIndex.reset();
     142             :     }
     143          20 :     delete pFrmFmts;
     144          20 :     delete pRedlData;
     145          20 : }
     146             : 
     147           0 : void SwUndoInserts::UndoImpl(::sw::UndoRedoContext & rContext)
     148             : {
     149           0 :     SwDoc *const pDoc = & rContext.GetDoc();
     150           0 :     SwPaM *const pPam = & AddUndoRedoPaM(rContext);
     151             : 
     152           0 :     if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
     153           0 :         pDoc->getIDocumentRedlineAccess().DeleteRedline( *pPam, true, USHRT_MAX );
     154             : 
     155             :     // if Point and Mark are different text nodes so a JoinNext has to be done
     156           0 :     bool bJoinNext = nSttNode != nEndNode &&
     157           0 :                 pPam->GetMark()->nNode.GetNode().GetTxtNode() &&
     158           0 :                 pPam->GetPoint()->nNode.GetNode().GetTxtNode();
     159             : 
     160             :     // Is there any content? (loading from template does not have content)
     161           0 :     if( nSttNode != nEndNode || nSttCntnt != nEndCntnt )
     162             :     {
     163           0 :         if( nSttNode != nEndNode )
     164             :         {
     165           0 :             SwTxtNode* pTxtNd = pDoc->GetNodes()[ nEndNode ]->GetTxtNode();
     166           0 :             if (pTxtNd && pTxtNd->GetTxt().getLength() == nEndCntnt)
     167           0 :                 pLastNdColl = pTxtNd->GetTxtColl();
     168             :         }
     169             : 
     170           0 :         RemoveIdxFromRange( *pPam, false );
     171           0 :         SetPaM(*pPam);
     172             : 
     173             :         // are there Footnotes or CntntFlyFrames in text?
     174           0 :         nSetPos = pHistory->Count();
     175           0 :         nNdDiff = pPam->GetMark()->nNode.GetIndex();
     176           0 :         DelCntntIndex( *pPam->GetMark(), *pPam->GetPoint() );
     177           0 :         nNdDiff -= pPam->GetMark()->nNode.GetIndex();
     178             : 
     179           0 :         if( *pPam->GetPoint() != *pPam->GetMark() )
     180             :         {
     181             :             m_pUndoNodeIndex.reset(
     182           0 :                     new SwNodeIndex(pDoc->GetNodes().GetEndOfContent()));
     183           0 :             MoveToUndoNds(*pPam, m_pUndoNodeIndex.get());
     184             : 
     185           0 :             if( !bSttWasTxtNd )
     186           0 :                 pPam->Move( fnMoveBackward, fnGoCntnt );
     187             :         }
     188             :     }
     189             : 
     190           0 :     if (m_FlyUndos.size())
     191             :     {
     192           0 :         sal_uLong nTmp = pPam->GetPoint()->nNode.GetIndex();
     193           0 :         for (size_t n = m_FlyUndos.size(); 0 < n; --n)
     194             :         {
     195           0 :             m_FlyUndos[ n-1 ]->UndoImpl(rContext);
     196             :         }
     197           0 :         nNdDiff += nTmp - pPam->GetPoint()->nNode.GetIndex();
     198             :     }
     199             : 
     200           0 :     SwNodeIndex& rIdx = pPam->GetPoint()->nNode;
     201           0 :     SwTxtNode* pTxtNode = rIdx.GetNode().GetTxtNode();
     202           0 :     if( pTxtNode )
     203             :     {
     204           0 :         if( !pTxtFmtColl ) // if 0 than it's no TextNode -> delete
     205             :         {
     206           0 :             SwNodeIndex aDelIdx( rIdx );
     207           0 :             ++rIdx;
     208           0 :             SwCntntNode* pCNd = rIdx.GetNode().GetCntntNode();
     209           0 :             pPam->GetPoint()->nContent.Assign( pCNd, pCNd ? pCNd->Len() : 0 );
     210           0 :             pPam->SetMark();
     211           0 :             pPam->DeleteMark();
     212             : 
     213           0 :             RemoveIdxRel( aDelIdx.GetIndex(), *pPam->GetPoint() );
     214             : 
     215           0 :             pDoc->GetNodes().Delete( aDelIdx, 1 );
     216             :         }
     217             :         else
     218             :         {
     219           0 :             if( bJoinNext && pTxtNode->CanJoinNext())
     220             :             {
     221             :                 {
     222           0 :                     RemoveIdxRel( rIdx.GetIndex()+1, SwPosition( rIdx,
     223           0 :                         SwIndex( pTxtNode, pTxtNode->GetTxt().getLength() )));
     224             :                 }
     225           0 :                 pTxtNode->JoinNext();
     226             :             }
     227             :             // reset all text attributes in the paragraph!
     228           0 :             pTxtNode->RstTxtAttr( SwIndex(pTxtNode, 0), pTxtNode->Len(), 0, 0, true );
     229             : 
     230           0 :             pTxtNode->ResetAllAttr();
     231             : 
     232           0 :             if( USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pTxtFmtColl ))
     233           0 :                 pTxtFmtColl = (SwTxtFmtColl*)pTxtNode->ChgFmtColl( pTxtFmtColl );
     234             : 
     235           0 :             pHistory->SetTmpEnd( nSetPos );
     236           0 :             pHistory->TmpRollback( pDoc, 0, false );
     237             :         }
     238             :     }
     239           0 : }
     240             : 
     241           0 : void SwUndoInserts::RedoImpl(::sw::UndoRedoContext & rContext)
     242             : {
     243             :     // position cursor onto REDO section
     244           0 :     SwPaM *const pPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
     245           0 :     SwDoc* pDoc = pPam->GetDoc();
     246           0 :     pPam->DeleteMark();
     247           0 :     pPam->GetPoint()->nNode = nSttNode - nNdDiff;
     248           0 :     SwCntntNode* pCNd = pPam->GetCntntNode();
     249           0 :     pPam->GetPoint()->nContent.Assign( pCNd, nSttCntnt );
     250             : 
     251           0 :     SwTxtFmtColl* pSavTxtFmtColl = pTxtFmtColl;
     252           0 :     if( pTxtFmtColl && pCNd && pCNd->IsTxtNode() )
     253           0 :         pSavTxtFmtColl = ((SwTxtNode*)pCNd)->GetTxtColl();
     254             : 
     255           0 :     pHistory->SetTmpEnd( nSetPos );
     256             : 
     257             :     // retrieve start position for rollback
     258           0 :     if( ( nSttNode != nEndNode || nSttCntnt != nEndCntnt ) && m_pUndoNodeIndex)
     259             :     {
     260           0 :         const bool bMvBkwrd = MovePtBackward( *pPam );
     261             : 
     262             :         // re-insert content again (first detach m_pUndoNodeIndex!)
     263           0 :         sal_uLong const nMvNd = m_pUndoNodeIndex->GetIndex();
     264           0 :         m_pUndoNodeIndex.reset();
     265           0 :         MoveFromUndoNds(*pDoc, nMvNd, *pPam->GetMark());
     266           0 :         if( bSttWasTxtNd )
     267           0 :             MovePtForward( *pPam, bMvBkwrd );
     268           0 :         pPam->Exchange();
     269             :     }
     270             : 
     271           0 :     if( USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pTxtFmtColl ))
     272             :     {
     273           0 :         SwTxtNode* pTxtNd = pPam->GetMark()->nNode.GetNode().GetTxtNode();
     274           0 :         if( pTxtNd )
     275           0 :             pTxtNd->ChgFmtColl( pTxtFmtColl );
     276             :     }
     277           0 :     pTxtFmtColl = pSavTxtFmtColl;
     278             : 
     279           0 :     if( pLastNdColl && USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pLastNdColl ) &&
     280           0 :         pPam->GetPoint()->nNode != pPam->GetMark()->nNode )
     281             :     {
     282           0 :         SwTxtNode* pTxtNd = pPam->GetPoint()->nNode.GetNode().GetTxtNode();
     283           0 :         if( pTxtNd )
     284           0 :             pTxtNd->ChgFmtColl( pLastNdColl );
     285             :     }
     286             : 
     287           0 :     for (size_t n = m_FlyUndos.size(); 0 < n; --n)
     288             :     {
     289           0 :         m_FlyUndos[ n-1 ]->RedoImpl(rContext);
     290             :     }
     291             : 
     292           0 :     pHistory->Rollback( pDoc, nSetPos );
     293             : 
     294           0 :     if( pRedlData && IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
     295             :     {
     296           0 :         RedlineMode_t eOld = pDoc->getIDocumentRedlineAccess().GetRedlineMode();
     297           0 :         pDoc->getIDocumentRedlineAccess().SetRedlineMode_intern((RedlineMode_t)( eOld & ~nsRedlineMode_t::REDLINE_IGNORE ));
     298           0 :         pDoc->getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( *pRedlData, *pPam ), true);
     299           0 :         pDoc->getIDocumentRedlineAccess().SetRedlineMode_intern( eOld );
     300             :     }
     301           0 :     else if( !( nsRedlineMode_t::REDLINE_IGNORE & GetRedlineMode() ) &&
     302           0 :             !pDoc->getIDocumentRedlineAccess().GetRedlineTbl().empty() )
     303           0 :         pDoc->getIDocumentRedlineAccess().SplitRedline( *pPam );
     304           0 : }
     305             : 
     306           0 : void SwUndoInserts::RepeatImpl(::sw::RepeatContext & rContext)
     307             : {
     308           0 :     SwPaM aPam( rContext.GetDoc().GetNodes().GetEndOfContent() );
     309           0 :     SetPaM( aPam );
     310           0 :     SwPaM & rRepeatPaM( rContext.GetRepeatPaM() );
     311           0 :     aPam.GetDoc()->getIDocumentContentOperations().CopyRange( aPam, *rRepeatPaM.GetPoint(), false );
     312           0 : }
     313             : 
     314           2 : SwUndoInsDoc::SwUndoInsDoc( const SwPaM& rPam )
     315           2 :     : SwUndoInserts( UNDO_INSDOKUMENT, rPam )
     316             : {
     317           2 : }
     318             : 
     319          18 : SwUndoCpyDoc::SwUndoCpyDoc( const SwPaM& rPam )
     320          18 :     : SwUndoInserts( UNDO_COPY, rPam )
     321             : {
     322         288 : }
     323             : 
     324             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10