diff options
author | Jan-Marek Glogowski <glogow@fbihome.de> | 2016-08-11 18:39:20 +0200 |
---|---|---|
committer | Jan-Marek Glogowski <glogow@fbihome.de> | 2016-08-29 13:13:17 +0200 |
commit | d943d4faf92f0b86c25f3cd6fe77ed8a7d4905d1 (patch) | |
tree | 620b5623841fe6480e6e4f9e278a59f8d57dbc1d | |
parent | 345b7023b80587e1a022ff7d2ce16e8afb23d9c5 (diff) |
Fix SwDoc::AppendDoc for trailing sections
We already treat the StartOfContent node special in the CopyRange
function to prevent merging of SwTextNodes.
For trailing sections, we have to expand the code to treat
EndOfContent special too, because the supplied SwPaM range is
handled as [mark, point[, so it previously missed the section end
node, which resulted in "unhiding" the last section, if it was the
last node in the document.
Change-Id: Ie094e2a0182647a49c9ba45d08a7dd2cabe667c6
-rw-r--r-- | sw/inc/IDocumentContentOperations.hxx | 2 | ||||
-rw-r--r-- | sw/qa/extras/mailmerge/data/sections_first_last.odt | bin | 0 -> 9010 bytes | |||
-rw-r--r-- | sw/qa/extras/mailmerge/mailmerge.cxx | 38 | ||||
-rw-r--r-- | sw/source/core/doc/docnew.cxx | 40 | ||||
-rw-r--r-- | sw/source/core/docnode/nodes.cxx | 39 |
5 files changed, 86 insertions, 33 deletions
diff --git a/sw/inc/IDocumentContentOperations.hxx b/sw/inc/IDocumentContentOperations.hxx index 75b4f841c28d..1572e218288e 100644 --- a/sw/inc/IDocumentContentOperations.hxx +++ b/sw/inc/IDocumentContentOperations.hxx @@ -80,7 +80,7 @@ public: be within the range! \warning The range has to include at least two nodes or has to be a - SwDoc::IsColumnSelection! + SwDoc::IsColumnSelection, because the rPam is treated [mark, point[. Normally this function should work only with content nodes. But there is a special case used by SwDoc::Paste, which starts the SwPaM at the diff --git a/sw/qa/extras/mailmerge/data/sections_first_last.odt b/sw/qa/extras/mailmerge/data/sections_first_last.odt Binary files differnew file mode 100644 index 000000000000..5a92adb0838d --- /dev/null +++ b/sw/qa/extras/mailmerge/data/sections_first_last.odt diff --git a/sw/qa/extras/mailmerge/mailmerge.cxx b/sw/qa/extras/mailmerge/mailmerge.cxx index 4b2e8ef05794..84331cc8b4cf 100644 --- a/sw/qa/extras/mailmerge/mailmerge.cxx +++ b/sw/qa/extras/mailmerge/mailmerge.cxx @@ -32,6 +32,7 @@ #include <edtwin.hxx> #include <olmenu.hxx> #include <cmdid.h> +#include <pagefrm.hxx> /** * Maps database URIs to the registered database names for quick lookups @@ -531,5 +532,42 @@ DECLARE_SHELL_MAILMERGE_TEST_SELECTION(testTdf95292, "linked-labels.odt", "10-te CPPUNIT_ASSERT_EQUAL( sal_uInt16( 5 ), pWrtShell->GetPhyPageNum() ); } +DECLARE_SHELL_MAILMERGE_TEST(test_sections_first_last, "sections_first_last.odt", "10-testing-addresses.ods", "testing-addresses") +{ + // A document with a leading, middle and trailing section + // Originally we were losing the trailing section during merge + executeMailMerge(); + + SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get()); + CPPUNIT_ASSERT(pTextDoc); + + // Get the size of the document in nodes + SwDoc *pDoc = pTextDoc->GetDocShell()->GetDoc(); + sal_uLong nSize = pDoc->GetNodes().GetEndOfContent().GetIndex() - pDoc->GetNodes().GetEndOfExtras().GetIndex(); + nSize -= 2; // The common start and end node + CPPUNIT_ASSERT_EQUAL( sal_uLong(13), nSize ); + + SwXTextDocument* pTextDocMM = dynamic_cast<SwXTextDocument *>(mxMMComponent.get()); + CPPUNIT_ASSERT(pTextDocMM); + + SwDoc *pDocMM = pTextDocMM->GetDocShell()->GetDoc(); + sal_uLong nSizeMM = pDocMM->GetNodes().GetEndOfContent().GetIndex() - pDocMM->GetNodes().GetEndOfExtras().GetIndex(); + nSizeMM -= 2; + CPPUNIT_ASSERT_EQUAL( sal_uLong(10 * nSize), nSizeMM ); + + CPPUNIT_ASSERT_EQUAL( sal_uInt16(19), pDocMM->GetDocShell()->GetWrtShell()->GetPhyPageNum() ); + + // All even pages should be empty, all sub-documents have two pages + const SwRootFrame* pLayout = pDocMM->getIDocumentLayoutAccess().GetCurrentLayout(); + const SwPageFrame* pPageFrm = static_cast<const SwPageFrame*>( pLayout->Lower() ); + while ( pPageFrm ) + { + bool bOdd = (1 == (pPageFrm->GetPhyPageNum() % 2)); + CPPUNIT_ASSERT_EQUAL( !bOdd, pPageFrm->IsEmptyPage() ); + CPPUNIT_ASSERT_EQUAL( sal_uInt16( bOdd ? 1 : 2 ), pPageFrm->GetVirtPageNum() ); + pPageFrm = static_cast<const SwPageFrame*>( pPageFrm->GetNext() ); + } +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/doc/docnew.cxx b/sw/source/core/doc/docnew.cxx index 8079e8fb37d2..aeaf70245d41 100644 --- a/sw/source/core/doc/docnew.cxx +++ b/sw/source/core/doc/docnew.cxx @@ -964,20 +964,13 @@ SwNodeIndex SwDoc::AppendDoc(const SwDoc& rSource, sal_uInt16 const nStartPageNu SAL_INFO( "sw.pagefrm", "(SwDoc::AppendDoc in " << bDeletePrevious ); // GetEndOfExtras + 1 = StartOfContent == no content node! - // this ensures, that we have at least two nodes in the SwPaM. - // @see IDocumentContentOperations::CopyRange + // This ensures it won't be merged in the SwTextNode at the position. SwNodeIndex aSourceIdx( rSource.GetNodes().GetEndOfExtras(), 1 ); - SwNodeIndex aSourceEndIdx( rSource.GetNodes().GetEndOfContent(), -1 ); - SwPaM aCpyPam( aSourceIdx ); - - if ( aSourceEndIdx.GetNode().IsTextNode() ) { - aCpyPam.SetMark(); - // moves to the last content node before EOC; for single paragraph - // documents this would result in [n, n], which is considered empty - aCpyPam.Move( fnMoveForward, fnGoDoc ); - } - else - aCpyPam = SwPaM( aSourceIdx, aSourceEndIdx ); + // CopyRange works on the range a [mark, point[ and considers an + // index < point outside the selection. + // @see IDocumentContentOperations::CopyRange + SwNodeIndex aSourceEndIdx( rSource.GetNodes().GetEndOfContent(), 0 ); + SwPaM aCpyPam( aSourceIdx, aSourceEndIdx ); #ifdef DBG_UTIL SAL_INFO( "sw.docappend", "NodeType 0x" << std::hex << (int) aSourceIdx.GetNode().GetNodeType() @@ -994,10 +987,8 @@ SwNodeIndex SwDoc::AppendDoc(const SwDoc& rSource, sal_uInt16 const nStartPageNu SAL_INFO( "sw.docappend", ".." ); SAL_INFO( "sw.docappend", "NodeType 0x" << std::hex << (int) aSourceEndIdx.GetNode().GetNodeType() << std::dec << " " << aSourceEndIdx.GetNode().GetIndex() ); - aSourceEndIdx++; SAL_INFO( "sw.docappend", "NodeType 0x" << std::hex << (int) aSourceEndIdx.GetNode().GetNodeType() << std::dec << " " << aSourceEndIdx.GetNode().GetIndex() ); - aSourceEndIdx--; SAL_INFO( "sw.docappend", "Src-Nd: " << CNTNT_DOC( &rSource ) ); SAL_INFO( "sw.docappend", "Nd: " << CNTNT_DOC( this ) ); #endif @@ -1041,9 +1032,23 @@ SwNodeIndex SwDoc::AppendDoc(const SwDoc& rSource, sal_uInt16 const nStartPageNu } // Otherwise we have to handle SwPlaceholderNodes as first node - if ( pTargetPageDesc ) { + if ( pTargetPageDesc ) + { + SwNodeIndex aBreakIdx( GetNodes().GetEndOfContent(), -1 ); + SwPosition aBreakPos( aBreakIdx ); + // InsertPageBreak just works on SwTextNode nodes, so make + // sure the last node is one! + bool bIsTextNode = aBreakIdx.GetNode().IsTextNode(); + if ( !bIsTextNode ) + getIDocumentContentOperations().AppendTextNode( aBreakPos ); OUString name = pTargetPageDesc->GetName(); pTargetShell->InsertPageBreak( &name, nStartPageNumber ); + if ( !bIsTextNode ) + { + pTargetShell->SttEndDoc( false ); + --aBreakIdx; + GetNodes().Delete( aBreakIdx, 1 ); + } // There is now a new empty text node on the new page. If it has // any marks, those are from the previous page: move them back @@ -1103,7 +1108,8 @@ SwNodeIndex SwDoc::AppendDoc(const SwDoc& rSource, sal_uInt16 const nStartPageNu this->GetIDocumentUndoRedo().StartUndo( UNDO_INSGLOSSARY, nullptr ); this->getIDocumentFieldsAccess().LockExpFields(); - // Position where the appended doc starts. Will be filled in later (uses GetEndOfContent() because SwNodeIndex has no default ctor). + // Position where the appended doc starts. Will be filled in later. + // Initially uses GetEndOfContent() because SwNodeIndex has no default ctor. SwNodeIndex aStartAppendIndex( GetNodes().GetEndOfContent() ); { diff --git a/sw/source/core/docnode/nodes.cxx b/sw/source/core/docnode/nodes.cxx index 7927af3502a8..4b04d07f5469 100644 --- a/sw/source/core/docnode/nodes.cxx +++ b/sw/source/core/docnode/nodes.cxx @@ -1695,24 +1695,30 @@ void SwNodes::CopyNodes( const SwNodeRange& rRange, !pAktNode->m_pStartOfSection->IsSectionNode() ) ) ++aRg.aStart; - // if aEnd-1 points to no ContentNode, search previous one - --aRg.aEnd; - // #i107142#: if aEnd is start node of a special section, do nothing. - // Otherwise this could lead to crash: going through all previous - // special section nodes and then one before the first. - if (aRg.aEnd.GetNode().StartOfSectionIndex() != 0) + const SwNode *aEndNode = &aRg.aEnd.GetNode(); + int nIsEndOfContent = (aEndNode == &aEndNode->GetNodes().GetEndOfContent()) ? 1 : 0; + + if (0 == nIsEndOfContent) { - while( ((pAktNode = & aRg.aEnd.GetNode())->GetStartNode() && - !pAktNode->IsSectionNode() ) || - ( pAktNode->IsEndNode() && - ND_STARTNODE == pAktNode->m_pStartOfSection->GetNodeType()) ) + // if aEnd-1 points to no ContentNode, search previous one + --aRg.aEnd; + // #i107142#: if aEnd is start node of a special section, do nothing. + // Otherwise this could lead to crash: going through all previous + // special section nodes and then one before the first. + if (aRg.aEnd.GetNode().StartOfSectionIndex() != 0) { - --aRg.aEnd; + while( ((pAktNode = & aRg.aEnd.GetNode())->GetStartNode() && + !pAktNode->IsSectionNode() ) || + ( pAktNode->IsEndNode() && + ND_STARTNODE == pAktNode->m_pStartOfSection->GetNodeType()) ) + { + --aRg.aEnd; + } } + ++aRg.aEnd; } - ++aRg.aEnd; - // if in same array, check insertion position + // is there anything left to copy? if( aRg.aStart >= aRg.aEnd ) return; @@ -1787,7 +1793,7 @@ void SwNodes::CopyNodes( const SwNodeRange& rRange, if (nDistance < nNodeCnt) nNodeCnt -= nDistance; else - nNodeCnt = 1; + nNodeCnt = 1 - nIsEndOfContent; aRg.aStart = pAktNode->EndOfSectionIndex(); @@ -1815,7 +1821,7 @@ void SwNodes::CopyNodes( const SwNodeRange& rRange, if (nDistance < nNodeCnt) nNodeCnt -= nDistance; else - nNodeCnt = 1; + nNodeCnt = 1 - nIsEndOfContent; aRg.aStart = pAktNode->EndOfSectionIndex(); if( bNewFrames && pSectNd && @@ -1840,6 +1846,9 @@ void SwNodes::CopyNodes( const SwNodeRange& rRange, --nLevel; ++aInsPos; // EndNode already exists } + else if( 1 == nNodeCnt && 1 == nIsEndOfContent ) + // we have reached the EndOfContent node - nothing to do! + continue; else if( !pAktNode->m_pStartOfSection->IsSectionNode() ) { // create a section at the original InsertPosition |