LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sw/source/core/undo - untblk.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 38 175 21.7 %
Date: 2013-07-09 Functions: 6 11 54.5 %
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 <IShellCursorSupplier.hxx>
      26             : #include <docary.hxx>
      27             : #include <swundo.hxx>
      28             : #include <pam.hxx>
      29             : #include <ndtxt.hxx>
      30             : #include <UndoCore.hxx>
      31             : #include <rolbck.hxx>
      32             : #include <redline.hxx>
      33             : 
      34           3 : SwUndoInserts::SwUndoInserts( SwUndoId nUndoId, const SwPaM& rPam )
      35             :     : SwUndo( nUndoId ), SwUndRng( rPam ),
      36             :     pTxtFmtColl( 0 ), pLastNdColl(0), pFrmFmts( 0 ), pRedlData( 0 ),
      37           3 :     bSttWasTxtNd( sal_True ), nNdDiff( 0 ), nSetPos( 0 )
      38             : {
      39           3 :     pHistory = new SwHistory;
      40           3 :     SwDoc* pDoc = (SwDoc*)rPam.GetDoc();
      41             : 
      42           3 :     SwTxtNode* pTxtNd = rPam.GetPoint()->nNode.GetNode().GetTxtNode();
      43           3 :     if( pTxtNd )
      44             :     {
      45           3 :         pTxtFmtColl = pTxtNd->GetTxtColl();
      46             :         pHistory->CopyAttr( pTxtNd->GetpSwpHints(), nSttNode,
      47           3 :                             0, pTxtNd->GetTxt().getLength(), false );
      48           3 :         if( pTxtNd->HasSwAttrSet() )
      49           0 :             pHistory->CopyFmtAttr( *pTxtNd->GetpSwAttrSet(), nSttNode );
      50             : 
      51           3 :         if( !nSttCntnt )    // than take the Flys along
      52             :         {
      53           3 :             sal_uInt16 nArrLen = pDoc->GetSpzFrmFmts()->size();
      54           3 :             for( sal_uInt16 n = 0; n < nArrLen; ++n )
      55             :             {
      56           0 :                 SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[n];
      57           0 :                 SwFmtAnchor const*const  pAnchor = &pFmt->GetAnchor();
      58           0 :                 const SwPosition* pAPos = pAnchor->GetCntntAnchor();
      59           0 :                 if (pAPos &&
      60           0 :                     (pAnchor->GetAnchorId() == FLY_AT_PARA) &&
      61           0 :                      nSttNode == pAPos->nNode.GetIndex() )
      62             :                 {
      63           0 :                     if( !pFrmFmts )
      64           0 :                         pFrmFmts = new std::vector<SwFrmFmt*>;
      65           0 :                     pFrmFmts->push_back( pFmt );
      66             :                 }
      67             :             }
      68             :         }
      69             :     }
      70             :     // consider Redline
      71           3 :     if( pDoc->IsRedlineOn() )
      72             :     {
      73           0 :         pRedlData = new SwRedlineData( nsRedlineType_t::REDLINE_INSERT, pDoc->GetRedlineAuthor() );
      74           0 :         SetRedlineMode( pDoc->GetRedlineMode() );
      75             :     }
      76           3 : }
      77             : 
      78             : // set destination after reading input
      79           3 : void SwUndoInserts::SetInsertRange( const SwPaM& rPam, sal_Bool bScanFlys,
      80             :                                     sal_Bool bSttIsTxtNd )
      81             : {
      82           3 :     const SwPosition* pTmpPos = rPam.End();
      83           3 :     nEndNode = pTmpPos->nNode.GetIndex();
      84           3 :     nEndCntnt = pTmpPos->nContent.GetIndex();
      85           3 :     if( rPam.HasMark() )
      86             :     {
      87           3 :         if( pTmpPos == rPam.GetPoint() )
      88           3 :             pTmpPos = rPam.GetMark();
      89             :         else
      90           0 :             pTmpPos = rPam.GetPoint();
      91             : 
      92           3 :         nSttNode = pTmpPos->nNode.GetIndex();
      93           3 :         nSttCntnt = pTmpPos->nContent.GetIndex();
      94             : 
      95           3 :         if( !bSttIsTxtNd )      // if a table selection is added ...
      96             :         {
      97           0 :             ++nSttNode;         // ... than the CopyPam is not fully correct
      98           0 :             bSttWasTxtNd = sal_False;
      99             :         }
     100             :     }
     101             : 
     102           3 :     if( bScanFlys && !nSttCntnt )
     103             :     {
     104             :         // than collect all new Flys
     105           3 :         SwDoc* pDoc = (SwDoc*)rPam.GetDoc();
     106           3 :         sal_uInt16 nArrLen = pDoc->GetSpzFrmFmts()->size();
     107           3 :         for( sal_uInt16 n = 0; n < nArrLen; ++n )
     108             :         {
     109           0 :             SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[n];
     110           0 :             SwFmtAnchor const*const pAnchor = &pFmt->GetAnchor();
     111           0 :             SwPosition const*const pAPos = pAnchor->GetCntntAnchor();
     112           0 :             if (pAPos &&
     113           0 :                 (pAnchor->GetAnchorId() == FLY_AT_PARA) &&
     114           0 :                 nSttNode == pAPos->nNode.GetIndex() )
     115             :             {
     116           0 :                 std::vector<SwFrmFmt*>::iterator it;
     117           0 :                 if( !pFrmFmts ||
     118           0 :                     pFrmFmts->end() == ( it = std::find( pFrmFmts->begin(), pFrmFmts->end(), pFmt ) ) )
     119             :                 {
     120             :                     ::boost::shared_ptr<SwUndoInsLayFmt> const pFlyUndo(
     121           0 :                         new SwUndoInsLayFmt(pFmt, 0, 0));
     122           0 :                     m_FlyUndos.push_back(pFlyUndo);
     123             :                 }
     124             :                 else
     125           0 :                     pFrmFmts->erase( it );
     126             :             }
     127             :         }
     128           3 :         delete pFrmFmts, pFrmFmts = 0;
     129             :     }
     130           3 : }
     131             : 
     132           6 : SwUndoInserts::~SwUndoInserts()
     133             : {
     134           3 :     if (m_pUndoNodeIndex) // delete also the section from UndoNodes array
     135             :     {
     136             :         // Insert saves content in IconSection
     137           0 :         SwNodes& rUNds = m_pUndoNodeIndex->GetNodes();
     138           0 :         rUNds.Delete(*m_pUndoNodeIndex,
     139           0 :             rUNds.GetEndOfExtras().GetIndex() - m_pUndoNodeIndex->GetIndex());
     140           0 :         m_pUndoNodeIndex.reset();
     141             :     }
     142           3 :     delete pFrmFmts;
     143           3 :     delete pRedlData;
     144           3 : }
     145             : 
     146           0 : void SwUndoInserts::UndoImpl(::sw::UndoRedoContext & rContext)
     147             : {
     148           0 :     SwDoc *const pDoc = & rContext.GetDoc();
     149           0 :     SwPaM *const pPam = & AddUndoRedoPaM(rContext);
     150             : 
     151           0 :     if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
     152           0 :         pDoc->DeleteRedline( *pPam, true, USHRT_MAX );
     153             : 
     154             :     // if Point and Mark are different text nodes so a JoinNext has to be done
     155           0 :     bool bJoinNext = nSttNode != nEndNode &&
     156           0 :                 pPam->GetMark()->nNode.GetNode().GetTxtNode() &&
     157           0 :                 pPam->GetPoint()->nNode.GetNode().GetTxtNode();
     158             : 
     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, sal_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 :             xub_StrLen nCnt = 0;
     210           0 :             if( pCNd )
     211           0 :                 nCnt = pCNd->Len();
     212           0 :             pPam->GetPoint()->nContent.Assign( pCNd, nCnt );
     213           0 :             pPam->SetMark();
     214           0 :             pPam->DeleteMark();
     215             : 
     216           0 :             RemoveIdxRel( aDelIdx.GetIndex(), *pPam->GetPoint() );
     217             : 
     218           0 :             pDoc->GetNodes().Delete( aDelIdx, 1 );
     219             :         }
     220             :         else
     221             :         {
     222           0 :             if( bJoinNext && pTxtNode->CanJoinNext())
     223             :             {
     224             :                 {
     225           0 :                     RemoveIdxRel( rIdx.GetIndex()+1, SwPosition( rIdx,
     226           0 :                             SwIndex(pTxtNode, pTxtNode->GetTxt().getLength())));
     227             :                 }
     228           0 :                 pTxtNode->JoinNext();
     229             :             }
     230             :             // reset all text attributes in the paragraph!
     231             :     //i121897, change the hints clearing method from 'RstAttr' to 'ClarSwpHints' as the certain tox mark index hint reason
     232           0 :             if( pTxtNode && pTxtNode->GetpSwpHints() )
     233           0 :                 pTxtNode->ClearSwpHintsArr( true );
     234             : 
     235           0 :             pTxtNode->ResetAllAttr();
     236             : 
     237           0 :             if( USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pTxtFmtColl ))
     238           0 :                 pTxtFmtColl = (SwTxtFmtColl*)pTxtNode->ChgFmtColl( pTxtFmtColl );
     239             : 
     240           0 :             pHistory->SetTmpEnd( nSetPos );
     241           0 :             pHistory->TmpRollback( pDoc, 0, false );
     242             :         }
     243             :     }
     244           0 : }
     245             : 
     246           0 : void SwUndoInserts::RedoImpl(::sw::UndoRedoContext & rContext)
     247             : {
     248             :     // position cursor onto REDO section
     249           0 :     SwPaM *const pPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
     250           0 :     SwDoc* pDoc = pPam->GetDoc();
     251           0 :     pPam->DeleteMark();
     252           0 :     pPam->GetPoint()->nNode = nSttNode - nNdDiff;
     253           0 :     SwCntntNode* pCNd = pPam->GetCntntNode();
     254           0 :     pPam->GetPoint()->nContent.Assign( pCNd, nSttCntnt );
     255             : 
     256           0 :     SwTxtFmtColl* pSavTxtFmtColl = pTxtFmtColl;
     257           0 :     if( pTxtFmtColl && pCNd && pCNd->IsTxtNode() )
     258           0 :         pSavTxtFmtColl = ((SwTxtNode*)pCNd)->GetTxtColl();
     259             : 
     260           0 :     pHistory->SetTmpEnd( nSetPos );
     261             : 
     262             :     // retrieve start position for rollback
     263           0 :     if( ( nSttNode != nEndNode || nSttCntnt != nEndCntnt ) && m_pUndoNodeIndex)
     264             :     {
     265           0 :         sal_Bool bMvBkwrd = MovePtBackward( *pPam );
     266             : 
     267             :         // re-insert content again (first detach m_pUndoNodeIndex!)
     268           0 :         sal_uLong const nMvNd = m_pUndoNodeIndex->GetIndex();
     269           0 :         m_pUndoNodeIndex.reset();
     270           0 :         MoveFromUndoNds(*pDoc, nMvNd, *pPam->GetMark());
     271           0 :         if( bSttWasTxtNd )
     272           0 :             MovePtForward( *pPam, bMvBkwrd );
     273           0 :         pPam->Exchange();
     274             :     }
     275             : 
     276           0 :     if( USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pTxtFmtColl ))
     277             :     {
     278           0 :         SwTxtNode* pTxtNd = pPam->GetMark()->nNode.GetNode().GetTxtNode();
     279           0 :         if( pTxtNd )
     280           0 :             pTxtNd->ChgFmtColl( pTxtFmtColl );
     281             :     }
     282           0 :     pTxtFmtColl = pSavTxtFmtColl;
     283             : 
     284           0 :     if( pLastNdColl && USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pLastNdColl ) &&
     285           0 :         pPam->GetPoint()->nNode != pPam->GetMark()->nNode )
     286             :     {
     287           0 :         SwTxtNode* pTxtNd = pPam->GetPoint()->nNode.GetNode().GetTxtNode();
     288           0 :         if( pTxtNd )
     289           0 :             pTxtNd->ChgFmtColl( pLastNdColl );
     290             :     }
     291             : 
     292           0 :     for (size_t n = m_FlyUndos.size(); 0 < n; --n)
     293             :     {
     294           0 :         m_FlyUndos[ n-1 ]->RedoImpl(rContext);
     295             :     }
     296             : 
     297           0 :     pHistory->Rollback( pDoc, nSetPos );
     298             : 
     299           0 :     if( pRedlData && IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
     300             :     {
     301           0 :         RedlineMode_t eOld = pDoc->GetRedlineMode();
     302           0 :         pDoc->SetRedlineMode_intern((RedlineMode_t)( eOld & ~nsRedlineMode_t::REDLINE_IGNORE ));
     303           0 :         pDoc->AppendRedline( new SwRedline( *pRedlData, *pPam ), true);
     304           0 :         pDoc->SetRedlineMode_intern( eOld );
     305             :     }
     306           0 :     else if( !( nsRedlineMode_t::REDLINE_IGNORE & GetRedlineMode() ) &&
     307           0 :             !pDoc->GetRedlineTbl().empty() )
     308           0 :         pDoc->SplitRedline( *pPam );
     309           0 : }
     310             : 
     311           0 : void SwUndoInserts::RepeatImpl(::sw::RepeatContext & rContext)
     312             : {
     313           0 :     SwPaM aPam( rContext.GetDoc().GetNodes().GetEndOfContent() );
     314           0 :     SetPaM( aPam );
     315           0 :     SwPaM & rRepeatPaM( rContext.GetRepeatPaM() );
     316           0 :     aPam.GetDoc()->CopyRange( aPam, *rRepeatPaM.GetPoint(), false );
     317           0 : }
     318             : 
     319           0 : SwUndoInsDoc::SwUndoInsDoc( const SwPaM& rPam )
     320           0 :     : SwUndoInserts( UNDO_INSDOKUMENT, rPam )
     321             : {
     322           0 : }
     323             : 
     324           3 : SwUndoCpyDoc::SwUndoCpyDoc( const SwPaM& rPam )
     325           3 :     : SwUndoInserts( UNDO_COPY, rPam )
     326             : {
     327         102 : }
     328             : 
     329             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10