LCOV - code coverage report
Current view: top level - libreoffice/sw/source/core/undo - SwUndoPageDesc.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 163 0.0 %
Date: 2012-12-17 Functions: 0 23 0.0 %
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             : 
      21             : #include <tools/resid.hxx>
      22             : #include <doc.hxx>
      23             : #include <IDocumentUndoRedo.hxx>
      24             : #include <swundo.hxx>
      25             : #include <pagedesc.hxx>
      26             : #include <SwUndoPageDesc.hxx>
      27             : #include <SwRewriter.hxx>
      28             : #include <undobj.hxx>
      29             : #include <comcore.hrc>
      30             : #include <fmtcntnt.hxx>
      31             : #include <fmthdft.hxx>
      32             : 
      33             : #if OSL_DEBUG_LEVEL > 1
      34             : #include <ndindex.hxx>
      35             : #endif
      36             : 
      37             : 
      38             : #if OSL_DEBUG_LEVEL > 1
      39             : // Pure debug help function to have a quick look at the header/footer attributes.
      40             : void DebugHeaderFooterContent( const SwPageDesc& rPageDesc )
      41             : {
      42             :     sal_uLong nHeaderMaster = ULONG_MAX;
      43             :     sal_uLong nHeaderLeft = ULONG_MAX;
      44             :     sal_uLong nFooterMaster = ULONG_MAX;
      45             :     sal_uLong nFooterLeft = ULONG_MAX;
      46             : 
      47             :     SwFmtHeader& rHead = (SwFmtHeader&)rPageDesc.GetMaster().GetHeader();
      48             :     SwFmtFooter& rFoot = (SwFmtFooter&)rPageDesc.GetMaster().GetFooter();
      49             :     SwFmtHeader& rLeftHead = (SwFmtHeader&)rPageDesc.GetLeft().GetHeader();
      50             :     SwFmtFooter& rLeftFoot = (SwFmtFooter&)rPageDesc.GetLeft().GetFooter();
      51             :     if( rHead.IsActive() )
      52             :     {
      53             :         SwFrmFmt* pHeaderFmt = rHead.GetHeaderFmt();
      54             :         if( pHeaderFmt )
      55             :         {
      56             :             const SwFmtCntnt* pCntnt = &pHeaderFmt->GetCntnt();
      57             :             if( pCntnt->GetCntntIdx() )
      58             :                 nHeaderMaster = pCntnt->GetCntntIdx()->GetIndex();
      59             :             else
      60             :                 nHeaderMaster = 0;
      61             :         }
      62             :         SwFrmFmt* pLeftHeaderFmt = rLeftHead.GetHeaderFmt();
      63             :         if( pLeftHeaderFmt )
      64             :         {
      65             :             const SwFmtCntnt* pLeftCntnt = &pLeftHeaderFmt->GetCntnt();
      66             :             if( pLeftCntnt->GetCntntIdx() )
      67             :                 nHeaderLeft = pLeftCntnt->GetCntntIdx()->GetIndex();
      68             :             else
      69             :                 nHeaderLeft = 0;
      70             :         }
      71             :     }
      72             :     if( rFoot.IsActive() )
      73             :     {
      74             :         SwFrmFmt* pFooterFmt = rFoot.GetFooterFmt();
      75             :         if( pFooterFmt )
      76             :         {
      77             :             const SwFmtCntnt* pCntnt = &pFooterFmt->GetCntnt();
      78             :             if( pCntnt->GetCntntIdx() )
      79             :                 nFooterMaster = pCntnt->GetCntntIdx()->GetIndex();
      80             :             else
      81             :                 nFooterMaster = 0;
      82             :         }
      83             :         SwFrmFmt* pLeftFooterFmt = rLeftFoot.GetFooterFmt();
      84             :         if( pLeftFooterFmt )
      85             :         {
      86             :             const SwFmtCntnt* pLeftCntnt = &pLeftFooterFmt->GetCntnt();
      87             :             if( pLeftCntnt->GetCntntIdx() )
      88             :                 nFooterLeft = pLeftCntnt->GetCntntIdx()->GetIndex();
      89             :             else
      90             :                 nFooterLeft = 0;
      91             :         }
      92             :     }
      93             : 
      94             :     (void)nHeaderMaster;
      95             :     (void)nHeaderLeft;
      96             :     (void)nFooterMaster;
      97             :     (void)nFooterLeft;
      98             : }
      99             : #endif
     100             : 
     101           0 : SwUndoPageDesc::SwUndoPageDesc(const SwPageDesc & _aOld,
     102             :                                const SwPageDesc & _aNew,
     103             :                                SwDoc * _pDoc)
     104           0 :     : SwUndo( _aOld.GetName() != _aNew.GetName() ?
     105             :               UNDO_RENAME_PAGEDESC :
     106             :               UNDO_CHANGE_PAGEDESC ),
     107           0 :       aOld(_aOld, _pDoc), aNew(_aNew, _pDoc), pDoc(_pDoc), bExchange( false )
     108             : {
     109             :     OSL_ENSURE(0 != pDoc, "no document?");
     110             : 
     111             : #if OSL_DEBUG_LEVEL > 1
     112             :     DebugHeaderFooterContent( (SwPageDesc&)aOld );
     113             :     DebugHeaderFooterContent( (SwPageDesc&)aNew );
     114             : #endif
     115             : 
     116             :     /*
     117             :     The page description changes.
     118             :     If there are no header/footer content changes like header on/off or change from shared content
     119             :     to unshared etc., there is no reason to duplicate the content nodes (Crash i55547)
     120             :     But this happens, this Undo Ctor will destroy the unnecessary duplicate and manipulate the
     121             :     content pointer of the both page descriptions.
     122             :     */
     123           0 :     SwPageDesc &rOldDesc = (SwPageDesc&)aOld;
     124           0 :     SwPageDesc &rNewDesc = (SwPageDesc&)aNew;
     125           0 :     const SwFmtHeader& rOldHead = rOldDesc.GetMaster().GetHeader();
     126           0 :     const SwFmtHeader& rNewHead = rNewDesc.GetMaster().GetHeader();
     127           0 :     const SwFmtFooter& rOldFoot = rOldDesc.GetMaster().GetFooter();
     128           0 :     const SwFmtFooter& rNewFoot = rNewDesc.GetMaster().GetFooter();
     129             :     /* bExchange must not be set, if the old page descriptor will stay active.
     130             :     Two known situations:
     131             :     #i67735#: renaming a page descriptor
     132             :     #i67334#: changing the follow style
     133             :     If header/footer will be activated or deactivated, this undo will not work.
     134             :     */
     135           0 :     bExchange = ( aOld.GetName() == aNew.GetName() ) &&
     136           0 :         ( _aOld.GetFollow() == _aNew.GetFollow() ) &&
     137           0 :         ( rOldHead.IsActive() == rNewHead.IsActive() ) &&
     138           0 :         ( rOldFoot.IsActive() == rNewFoot.IsActive() );
     139           0 :     if( rOldHead.IsActive() && ( rOldDesc.IsHeaderShared() != rNewDesc.IsHeaderShared() ) )
     140           0 :         bExchange = false;
     141           0 :     if( rOldFoot.IsActive() && ( rOldDesc.IsFooterShared() != rNewDesc.IsFooterShared() ) )
     142           0 :         bExchange = false;
     143           0 :     if( ( rOldHead.IsActive() || rOldFoot.IsActive() ) && ( rOldDesc.IsFirstShared() != rNewDesc.IsFirstShared() ) )
     144           0 :         bExchange = false;
     145           0 :     if( bExchange )
     146             :     {
     147           0 :         if( rNewHead.IsActive() )
     148             :         {
     149           0 :             SwFrmFmt* pFormat = new SwFrmFmt( *rNewHead.GetHeaderFmt() );
     150             :             // The Ctor of this object will remove the duplicate!
     151           0 :             SwFmtHeader aFmtHeader( pFormat );
     152           0 :             if( !rNewDesc.IsHeaderShared() )
     153             :             {
     154           0 :                 pFormat = new SwFrmFmt( *rNewDesc.GetLeft().GetHeader().GetHeaderFmt() );
     155             :                 // The Ctor of this object will remove the duplicate!
     156           0 :                 SwFmtHeader aFormatHeader( pFormat );
     157             :             }
     158           0 :             if( !rNewDesc.IsFirstShared() )
     159             :             {
     160           0 :                 pFormat = new SwFrmFmt( *rNewDesc.GetFirst().GetHeader().GetHeaderFmt() );
     161             :                 // The Ctor of this object will remove the duplicate!
     162           0 :                 SwFmtHeader aFormatHeader( pFormat );
     163           0 :             }
     164             :         }
     165             :         // Same procedure for footers...
     166           0 :         if( rNewFoot.IsActive() )
     167             :         {
     168           0 :             SwFrmFmt* pFormat = new SwFrmFmt( *rNewFoot.GetFooterFmt() );
     169             :             // The Ctor of this object will remove the duplicate!
     170           0 :             SwFmtFooter aFmtFooter( pFormat );
     171           0 :             if( !rNewDesc.IsFooterShared() )
     172             :             {
     173           0 :                 pFormat = new SwFrmFmt( *rNewDesc.GetLeft().GetFooter().GetFooterFmt() );
     174             :                 // The Ctor of this object will remove the duplicate!
     175           0 :                 SwFmtFooter aFormatFooter( pFormat );
     176             :             }
     177           0 :             if( !rNewDesc.IsFirstShared() )
     178             :             {
     179           0 :                 pFormat = new SwFrmFmt( *rNewDesc.GetFirst().GetFooter().GetFooterFmt() );
     180             :                 // The Ctor of this object will remove the duplicate!
     181           0 :                 SwFmtFooter aFormatFooter( pFormat );
     182           0 :             }
     183             :         }
     184             : 
     185             :         // After this exchange method the old page description will point to zero,
     186             :         // the new one will point to the node position of the original content nodes.
     187           0 :         ExchangeContentNodes( (SwPageDesc&)aOld, (SwPageDesc&)aNew );
     188             : #if OSL_DEBUG_LEVEL > 1
     189             :         DebugHeaderFooterContent( (SwPageDesc&)aOld );
     190             :         DebugHeaderFooterContent( (SwPageDesc&)aNew );
     191             : #endif
     192             :     }
     193           0 : }
     194             : 
     195           0 : SwUndoPageDesc::~SwUndoPageDesc()
     196             : {
     197           0 : }
     198             : 
     199             : 
     200           0 : void SwUndoPageDesc::ExchangeContentNodes( SwPageDesc& rSource, SwPageDesc &rDest )
     201             : {
     202             :     OSL_ENSURE( bExchange, "You shouldn't do that." );
     203           0 :     const SwFmtHeader& rDestHead = rDest.GetMaster().GetHeader();
     204           0 :     const SwFmtHeader& rSourceHead = rSource.GetMaster().GetHeader();
     205           0 :     if( rDestHead.IsActive() )
     206             :     {
     207             :         // Let the destination page descrition point to the source node position,
     208             :         // from now on this descriptor is responsible for the content nodes!
     209             :         const SfxPoolItem* pItem;
     210           0 :         rDest.GetMaster().GetAttrSet().GetItemState( RES_HEADER, sal_False, &pItem );
     211           0 :         SfxPoolItem *pNewItem = pItem->Clone();
     212           0 :         SwFrmFmt* pNewFmt = ((SwFmtHeader*)pNewItem)->GetHeaderFmt();
     213             : #if OSL_DEBUG_LEVEL > 1
     214             :         const SwFmtCntnt& rSourceCntnt = rSourceHead.GetHeaderFmt()->GetCntnt();
     215             :         (void)rSourceCntnt;
     216             :         const SwFmtCntnt& rDestCntnt = rDestHead.GetHeaderFmt()->GetCntnt();
     217             :         (void)rDestCntnt;
     218             : #endif
     219           0 :         pNewFmt->SetFmtAttr( rSourceHead.GetHeaderFmt()->GetCntnt() );
     220           0 :         delete pNewItem;
     221             : 
     222             :         // Let the source page description point to zero node position,
     223             :         // it loses the responsible and can be destroyed without removing the content nodes.
     224           0 :         rSource.GetMaster().GetAttrSet().GetItemState( RES_HEADER, sal_False, &pItem );
     225           0 :         pNewItem = pItem->Clone();
     226           0 :         pNewFmt = ((SwFmtHeader*)pNewItem)->GetHeaderFmt();
     227           0 :         pNewFmt->SetFmtAttr( SwFmtCntnt() );
     228           0 :         delete pNewItem;
     229             : 
     230           0 :         if( !rDest.IsHeaderShared() )
     231             :         {
     232             :             // Same procedure for unshared header..
     233           0 :             const SwFmtHeader& rSourceLeftHead = rSource.GetLeft().GetHeader();
     234           0 :             rDest.GetLeft().GetAttrSet().GetItemState( RES_HEADER, sal_False, &pItem );
     235           0 :             pNewItem = pItem->Clone();
     236           0 :             pNewFmt = ((SwFmtHeader*)pNewItem)->GetHeaderFmt();
     237             : #if OSL_DEBUG_LEVEL > 1
     238             :             const SwFmtCntnt& rSourceCntnt1 = rSourceLeftHead.GetHeaderFmt()->GetCntnt();
     239             :             (void)rSourceCntnt1;
     240             :             const SwFmtCntnt& rDestCntnt1 = rDest.GetLeft().GetHeader().GetHeaderFmt()->GetCntnt();
     241             :             (void)rDestCntnt1;
     242             : #endif
     243           0 :             pNewFmt->SetFmtAttr( rSourceLeftHead.GetHeaderFmt()->GetCntnt() );
     244           0 :             delete pNewItem;
     245           0 :             rSource.GetLeft().GetAttrSet().GetItemState( RES_HEADER, sal_False, &pItem );
     246           0 :             pNewItem = pItem->Clone();
     247           0 :             pNewFmt = ((SwFmtHeader*)pNewItem)->GetHeaderFmt();
     248           0 :             pNewFmt->SetFmtAttr( SwFmtCntnt() );
     249           0 :             delete pNewItem;
     250             :         }
     251             :     }
     252             :     // Same procedure for footers...
     253           0 :     const SwFmtFooter& rDestFoot = rDest.GetMaster().GetFooter();
     254           0 :     const SwFmtFooter& rSourceFoot = rSource.GetMaster().GetFooter();
     255           0 :     if( rDestFoot.IsActive() )
     256             :     {
     257             :         const SfxPoolItem* pItem;
     258           0 :         rDest.GetMaster().GetAttrSet().GetItemState( RES_FOOTER, sal_False, &pItem );
     259           0 :         SfxPoolItem *pNewItem = pItem->Clone();
     260           0 :         SwFrmFmt *pNewFmt = ((SwFmtFooter*)pNewItem)->GetFooterFmt();
     261           0 :         pNewFmt->SetFmtAttr( rSourceFoot.GetFooterFmt()->GetCntnt() );
     262           0 :         delete pNewItem;
     263             : 
     264             : #if OSL_DEBUG_LEVEL > 1
     265             :         const SwFmtCntnt& rFooterSourceCntnt = rSourceFoot.GetFooterFmt()->GetCntnt();
     266             :         (void)rFooterSourceCntnt;
     267             :         const SwFmtCntnt& rFooterDestCntnt = rDestFoot.GetFooterFmt()->GetCntnt();
     268             :         (void)rFooterDestCntnt;
     269             : #endif
     270           0 :         rSource.GetMaster().GetAttrSet().GetItemState( RES_FOOTER, sal_False, &pItem );
     271           0 :         pNewItem = pItem->Clone();
     272           0 :         pNewFmt = ((SwFmtFooter*)pNewItem)->GetFooterFmt();
     273           0 :         pNewFmt->SetFmtAttr( SwFmtCntnt() );
     274           0 :         delete pNewItem;
     275             : 
     276           0 :         if( !rDest.IsFooterShared() )
     277             :         {
     278           0 :             const SwFmtFooter& rSourceLeftFoot = rSource.GetLeft().GetFooter();
     279             : #if OSL_DEBUG_LEVEL > 1
     280             :             const SwFmtCntnt& rFooterSourceCntnt2 = rSourceLeftFoot.GetFooterFmt()->GetCntnt();
     281             :             const SwFmtCntnt& rFooterDestCntnt2 =
     282             :                 rDest.GetLeft().GetFooter().GetFooterFmt()->GetCntnt();
     283             :             (void)rFooterSourceCntnt2;
     284             :             (void)rFooterDestCntnt2;
     285             : #endif
     286           0 :             rDest.GetLeft().GetAttrSet().GetItemState( RES_FOOTER, sal_False, &pItem );
     287           0 :             pNewItem = pItem->Clone();
     288           0 :             pNewFmt = ((SwFmtFooter*)pNewItem)->GetFooterFmt();
     289           0 :             pNewFmt->SetFmtAttr( rSourceLeftFoot.GetFooterFmt()->GetCntnt() );
     290           0 :             delete pNewItem;
     291           0 :             rSource.GetLeft().GetAttrSet().GetItemState( RES_FOOTER, sal_False, &pItem );
     292           0 :             pNewItem = pItem->Clone();
     293           0 :             pNewFmt = ((SwFmtFooter*)pNewItem)->GetFooterFmt();
     294           0 :             pNewFmt->SetFmtAttr( SwFmtCntnt() );
     295           0 :             delete pNewItem;
     296             :         }
     297             :     }
     298           0 : }
     299             : 
     300           0 : void SwUndoPageDesc::UndoImpl(::sw::UndoRedoContext &)
     301             : {
     302             :     // Move (header/footer)content node responsibility from new page descriptor to old one again.
     303           0 :     if( bExchange )
     304           0 :         ExchangeContentNodes( (SwPageDesc&)aNew, (SwPageDesc&)aOld );
     305           0 :     pDoc->ChgPageDesc(aOld.GetName(), aOld);
     306           0 : }
     307             : 
     308           0 : void SwUndoPageDesc::RedoImpl(::sw::UndoRedoContext &)
     309             : {
     310             :     // Move (header/footer)content node responsibility from old page descriptor to new one again.
     311           0 :     if( bExchange )
     312           0 :         ExchangeContentNodes( (SwPageDesc&)aOld, (SwPageDesc&)aNew );
     313           0 :     pDoc->ChgPageDesc(aNew.GetName(), aNew);
     314           0 : }
     315             : 
     316           0 : SwRewriter SwUndoPageDesc::GetRewriter() const
     317             : {
     318           0 :     SwRewriter aResult;
     319             : 
     320           0 :     aResult.AddRule(UndoArg1, aOld.GetName());
     321           0 :     aResult.AddRule(UndoArg2, SW_RESSTR(STR_YIELDS));
     322           0 :     aResult.AddRule(UndoArg3, aNew.GetName());
     323             : 
     324           0 :     return aResult;
     325             : }
     326             : 
     327             : // #116530#
     328           0 : SwUndoPageDescCreate::SwUndoPageDescCreate(const SwPageDesc * pNew,
     329             :                                            SwDoc * _pDoc)
     330             :     : SwUndo(UNDO_CREATE_PAGEDESC), pDesc(pNew), aNew(*pNew, _pDoc),
     331           0 :       pDoc(_pDoc)
     332             : {
     333             :     OSL_ENSURE(0 != pDoc, "no document?");
     334           0 : }
     335             : 
     336           0 : SwUndoPageDescCreate::~SwUndoPageDescCreate()
     337             : {
     338           0 : }
     339             : 
     340           0 : void SwUndoPageDescCreate::UndoImpl(::sw::UndoRedoContext &)
     341             : {
     342             :     // -> #116530#
     343           0 :     if (pDesc)
     344             :     {
     345           0 :         aNew = *pDesc;
     346           0 :         pDesc = NULL;
     347             :     }
     348             :     // <- #116530#
     349             : 
     350           0 :     pDoc->DelPageDesc(aNew.GetName(), true);
     351           0 : }
     352             : 
     353           0 : void SwUndoPageDescCreate::DoImpl()
     354             : {
     355           0 :     SwPageDesc aPageDesc = aNew;
     356           0 :     pDoc->MakePageDesc(aNew.GetName(), &aPageDesc, false, true); // #116530#
     357           0 : }
     358             : 
     359           0 : void SwUndoPageDescCreate::RedoImpl(::sw::UndoRedoContext &)
     360             : {
     361           0 :     DoImpl();
     362           0 : }
     363             : 
     364           0 : void SwUndoPageDescCreate::RepeatImpl(::sw::RepeatContext &)
     365             : {
     366           0 :     ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
     367           0 :     DoImpl();
     368           0 : }
     369             : 
     370           0 : SwRewriter SwUndoPageDescCreate::GetRewriter() const
     371             : {
     372           0 :     SwRewriter aResult;
     373             : 
     374           0 :     if (pDesc)
     375           0 :         aResult.AddRule(UndoArg1, pDesc->GetName());
     376             :     else
     377           0 :         aResult.AddRule(UndoArg1, aNew.GetName());
     378             : 
     379             : 
     380           0 :     return aResult;
     381             : }
     382             : 
     383           0 : SwUndoPageDescDelete::SwUndoPageDescDelete(const SwPageDesc & _aOld,
     384             :                                            SwDoc * _pDoc)
     385           0 :     : SwUndo(UNDO_DELETE_PAGEDESC), aOld(_aOld, _pDoc), pDoc(_pDoc)
     386             : {
     387             :     OSL_ENSURE(0 != pDoc, "no document?");
     388           0 : }
     389             : 
     390           0 : SwUndoPageDescDelete::~SwUndoPageDescDelete()
     391             : {
     392           0 : }
     393             : 
     394           0 : void SwUndoPageDescDelete::UndoImpl(::sw::UndoRedoContext &)
     395             : {
     396           0 :     SwPageDesc aPageDesc = aOld;
     397           0 :     pDoc->MakePageDesc(aOld.GetName(), &aPageDesc, false, true); // #116530#
     398           0 : }
     399             : 
     400           0 : void SwUndoPageDescDelete::DoImpl()
     401             : {
     402           0 :     pDoc->DelPageDesc(aOld.GetName(), true); // #116530#
     403           0 : }
     404             : 
     405           0 : void SwUndoPageDescDelete::RedoImpl(::sw::UndoRedoContext &)
     406             : {
     407           0 :     DoImpl();
     408           0 : }
     409             : 
     410           0 : void SwUndoPageDescDelete::RepeatImpl(::sw::RepeatContext &)
     411             : {
     412           0 :     ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
     413           0 :     DoImpl();
     414           0 : }
     415             : 
     416           0 : SwRewriter SwUndoPageDescDelete::GetRewriter() const
     417             : {
     418           0 :     SwRewriter aResult;
     419             : 
     420           0 :     aResult.AddRule(UndoArg1, aOld.GetName());
     421             : 
     422           0 :     return aResult;
     423             : }
     424             : 
     425             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10