sw/source/core/doc/docnew.cxx |   33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

New commits:
commit 9271152c5fb64a698de167861e39f6b073a60ad6
Author:     Michael Weghorn <m.wegh...@posteo.de>
AuthorDate: Tue Oct 15 09:03:51 2019 +0200
Commit:     Michael Weghorn <m.wegh...@posteo.de>
CommitDate: Tue Oct 15 22:34:24 2019 +0200

    tdf#128148 SwDoc::Append: Copy page descs if any has header/footer
    
    Don't only check the first SwPageDesc, but also its
    follows whether they have a header or footer and copy
    the page styles in this case, since otherwise the
    header/footer content can be wrong if the first page's
    page style doesn't have header/footer but a following
    one does.
    
    Change-Id: If5ef76a7faf93ad533976b501f35f171336615c8
    Reviewed-on: https://gerrit.libreoffice.org/80814
    Tested-by: Jenkins
    Reviewed-by: Michael Stahl <michael.st...@cib.de>

diff --git a/sw/source/core/doc/docnew.cxx b/sw/source/core/doc/docnew.cxx
index eb3032565cb0..01ae7cc6ed45 100644
--- a/sw/source/core/doc/docnew.cxx
+++ b/sw/source/core/doc/docnew.cxx
@@ -19,6 +19,8 @@
 
 #include <config_features.h>
 
+#include <unordered_set>
+
 #include <doc.hxx>
 #include <dcontact.hxx>
 #include <proofreadingiterator.hxx>
@@ -932,6 +934,25 @@ static OUString lcl_FindUniqueName(SwWrtShell* 
pTargetShell, const OUString& rSt
     while( true );
 }
 
+/** Returns whether the passed SwPageDesc& or any of its (transitive) follows
+   contains a header or footer. */
+static bool lcl_PageDescOrFollowContainsHeaderFooter(const SwPageDesc& 
rPageDesc)
+{
+    // remember already checked page descs to avoid cycle
+    std::unordered_set<const SwPageDesc*> aCheckedPageDescs;
+    const SwPageDesc* pCurPageDesc = &rPageDesc;
+    while (aCheckedPageDescs.count(pCurPageDesc) == 0)
+    {
+        const SwFrameFormat& rMaster = pCurPageDesc->GetMaster();
+        if (rMaster.GetHeader().IsActive() || rMaster.GetFooter().IsActive())
+            return true;
+
+        aCheckedPageDescs.insert(pCurPageDesc);
+        pCurPageDesc = pCurPageDesc->GetFollow();
+    }
+    return false;
+}
+
 static void lcl_CopyFollowPageDesc(
                             SwWrtShell& rTargetShell,
                             const SwPageDesc& rSourcePageDesc,
@@ -1031,12 +1052,10 @@ SwNodeIndex SwDoc::AppendDoc(const SwDoc& rSource, 
sal_uInt16 const nStartPageNu
             // if the source uses headers or footers the target document
             // needs inidividual page styles
             const SwWrtShell *pSourceShell = 
rSource.GetDocShell()->GetWrtShell();
-            const SwPageDesc *pSourcePageDesc = &pSourceShell->GetPageDesc(
+            const SwPageDesc rSourcePageDesc = pSourceShell->GetPageDesc(
                                                     
pSourceShell->GetCurPageDesc());
-            const OUString sStartingPageDesc = pSourcePageDesc->GetName();
-            const SwFrameFormat& rMaster = pSourcePageDesc->GetMaster();
-            const bool bPageStylesWithHeaderFooter = 
rMaster.GetHeader().IsActive() ||
-                                                     
rMaster.GetFooter().IsActive();
+            const OUString sStartingPageDesc = rSourcePageDesc.GetName();
+            const bool bPageStylesWithHeaderFooter = 
lcl_PageDescOrFollowContainsHeaderFooter(rSourcePageDesc);
             if( bPageStylesWithHeaderFooter )
             {
                 // create a new pagestyle
@@ -1046,8 +1065,8 @@ SwNodeIndex SwDoc::AppendDoc(const SwDoc& rSource, 
sal_uInt16 const nStartPageNu
                 pTargetPageDesc = MakePageDesc( sNewPageDescName );
                 if( pTargetPageDesc )
                 {
-                    CopyPageDesc( *pSourcePageDesc, *pTargetPageDesc, false );
-                    lcl_CopyFollowPageDesc( *pTargetShell, *pSourcePageDesc, 
*pTargetPageDesc, nDocNo );
+                    CopyPageDesc( rSourcePageDesc, *pTargetPageDesc, false );
+                    lcl_CopyFollowPageDesc( *pTargetShell, rSourcePageDesc, 
*pTargetPageDesc, nDocNo );
                 }
             }
             else
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to