1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
23 #include <sfx2/docfile.hxx>
24 #include <sfx2/docfilt.hxx>
25 #include <sfx2/app.hxx>
26 #include <svl/itemset.hxx>
27 #include <tools/debug.hxx>
29 #include <sfx2/fcontnr.hxx>
30 #include <svl/style.hxx>
31 #include <svx/svdpagv.hxx>
32 #include <svx/svdundo.hxx>
33 #include <vcl/stdtext.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/weld.hxx>
36 #include <xmloff/autolayout.hxx>
38 #include <strings.hrc>
39 #include <drawdoc.hxx>
42 #include <stlpool.hxx>
43 #include <sdresid.hxx>
44 #include <customshowlist.hxx>
47 #include <unmovss.hxx>
49 #include <unprlout.hxx>
50 #include <DrawDocShell.hxx>
51 #include <GraphicDocShell.hxx>
52 #include <ViewShell.hxx>
54 #include <ViewShellBase.hxx>
55 #include <strings.hxx>
57 using namespace ::com::sun::star
;
59 /** Concrete incarnations get called by lcl_IterateBookmarkPages, for
60 every page in the bookmark document/list
63 class InsertBookmarkAsPage_FindDuplicateLayouts
66 explicit InsertBookmarkAsPage_FindDuplicateLayouts( std::vector
<OUString
> &rLayoutsToTransfer
)
67 : mrLayoutsToTransfer(rLayoutsToTransfer
) {}
68 void operator()( SdDrawDocument
&, SdPage
const *, bool, SdDrawDocument
* );
70 std::vector
<OUString
> &mrLayoutsToTransfer
;
73 void InsertBookmarkAsPage_FindDuplicateLayouts::operator()( SdDrawDocument
& rDoc
, SdPage
const * pBMMPage
, bool bRenameDuplicates
, SdDrawDocument
* pBookmarkDoc
)
75 // now check for duplicate masterpage and layout names
77 OUString
aLayout( pBMMPage
->GetLayoutName() );
78 sal_Int32 nIndex
= aLayout
.indexOf( SD_LT_SEPARATOR
);
80 aLayout
= aLayout
.copy(0, nIndex
);
82 std::vector
<OUString
>::const_iterator pIter
=
83 find(mrLayoutsToTransfer
.begin(),mrLayoutsToTransfer
.end(),aLayout
);
85 bool bFound
= pIter
!= mrLayoutsToTransfer
.end();
87 const sal_uInt16 nMPageCount
= rDoc
.GetMasterPageCount();
88 for (sal_uInt16 nMPage
= 0; nMPage
< nMPageCount
&& !bFound
; nMPage
++)
90 // Do the layouts already exist within the document?
91 SdPage
* pTestPage
= static_cast<SdPage
*>( rDoc
.GetMasterPage(nMPage
) );
92 OUString
aTest(pTestPage
->GetLayoutName());
93 sal_Int32 nIndex2
= aTest
.indexOf( SD_LT_SEPARATOR
);
95 aTest
= aTest
.copy(0, nIndex2
);
97 if (aTest
== aLayout
&& pBMMPage
->GetPageKind() == pTestPage
->GetPageKind())
99 // Ignore Layouts with "Default" these seem to be special - in the sense that there are lot of assumption all over Impress
101 if( bRenameDuplicates
&& aTest
!= SdResId( STR_LAYOUT_DEFAULT_NAME
) && !(pTestPage
->Equals(*pBMMPage
)) )
103 pBookmarkDoc
->RenameLayoutTemplate(
104 pBMMPage
->GetLayoutName(), pBMMPage
->GetName() + "_");
105 aLayout
= pBMMPage
->GetName();
115 mrLayoutsToTransfer
.push_back(aLayout
);
118 // Inserts a bookmark as a page
119 static void lcl_IterateBookmarkPages( SdDrawDocument
&rDoc
, SdDrawDocument
* pBookmarkDoc
,
120 const std::vector
<OUString
> &rBookmarkList
, sal_uInt16 nBMSdPageCount
,
121 InsertBookmarkAsPage_FindDuplicateLayouts
& rPageIterator
, bool bRenameDuplicates
)
124 // Refactored copy'n'pasted layout name collection from InsertBookmarkAsPage
128 if( rBookmarkList
.empty() )
130 // no list? whole source document
131 nEndPos
= nBMSdPageCount
;
135 // bookmark list? number of entries
136 nEndPos
= rBookmarkList
.size();
141 // iterate over number of pages to insert
142 for (nPos
= 0; nPos
< nEndPos
; ++nPos
)
144 // the master page associated to the nPos'th page to insert
145 SdPage
* pBMMPage
= nullptr;
147 if( rBookmarkList
.empty() )
149 // simply take master page of nPos'th page in source document
150 pBMMPage
= static_cast<SdPage
*>(&(pBookmarkDoc
->GetSdPage(static_cast<sal_uInt16
>(nPos
), PageKind::Standard
)->TRG_GetMasterPage()));
154 // fetch nPos'th entry from bookmark list, and determine master page
155 OUString
aBMPgName(rBookmarkList
[nPos
]);
158 sal_uInt16 nBMPage
= pBookmarkDoc
->GetPageByName( aBMPgName
, bIsMasterPage
);
160 if (nBMPage
!= SDRPAGE_NOTFOUND
)
162 pBMPage
= static_cast<SdPage
*>( pBookmarkDoc
->GetPage(nBMPage
) );
169 // enforce that bookmarked page is a standard page and not already a master page
170 if (pBMPage
&& pBMPage
->GetPageKind()==PageKind::Standard
&& !pBMPage
->IsMasterPage())
172 const sal_uInt16 nBMSdPage
= (nBMPage
- 1) / 2;
173 pBMMPage
= static_cast<SdPage
*> (&(pBookmarkDoc
->GetSdPage(nBMSdPage
, PageKind::Standard
)->TRG_GetMasterPage()));
177 // successfully determined valid (bookmarked) page?
181 rPageIterator( rDoc
, pBMMPage
, bRenameDuplicates
, pBookmarkDoc
);
186 // Opens a bookmark document
187 SdDrawDocument
* SdDrawDocument::OpenBookmarkDoc(SfxMedium
* pMedium
)
190 SdDrawDocument
* pBookmarkDoc
= nullptr;
191 OUString aBookmarkName
= pMedium
->GetName();
192 std::shared_ptr
<const SfxFilter
> pFilter
= pMedium
->GetFilter();
195 pMedium
->UseInteractionHandler( true );
196 SfxGetpApp()->GetFilterMatcher().GuessFilter(*pMedium
, pFilter
);
203 else if ( !aBookmarkName
.isEmpty() && maBookmarkFile
!= aBookmarkName
)
205 bool bCreateGraphicShell
= pFilter
->GetServiceName() == "com.sun.star.drawing.DrawingDocument";
206 bool bCreateImpressShell
= pFilter
->GetServiceName() == "com.sun.star.presentation.PresentationDocument";
207 if ( bCreateGraphicShell
|| bCreateImpressShell
)
211 // Create a DocShell, as OLE objects might be contained in the
212 // document. (Persist)
213 // If that wasn't the case, we could load the model directly.
214 if ( bCreateGraphicShell
)
216 mxBookmarkDocShRef
= new ::sd::GraphicDocShell(SfxObjectCreateMode::STANDARD
);
219 mxBookmarkDocShRef
= new ::sd::DrawDocShell(SfxObjectCreateMode::STANDARD
, true, DocumentType::Impress
);
221 bOK
= mxBookmarkDocShRef
->DoLoad(pMedium
);
224 maBookmarkFile
= aBookmarkName
;
225 pBookmarkDoc
= mxBookmarkDocShRef
->GetDoc();
230 DBG_ASSERT(!aBookmarkName
.isEmpty(), "Empty document name!");
234 std::unique_ptr
<weld::MessageDialog
> xErrorBox(Application::CreateMessageDialog(nullptr,
235 VclMessageType::Warning
, VclButtonsType::Ok
, SdResId(STR_READ_DATA_ERROR
)));
239 pBookmarkDoc
= nullptr;
241 else if (mxBookmarkDocShRef
.is())
243 pBookmarkDoc
= mxBookmarkDocShRef
->GetDoc();
249 // Opens a bookmark document
250 SdDrawDocument
* SdDrawDocument::OpenBookmarkDoc(const OUString
& rBookmarkFile
)
252 SdDrawDocument
* pBookmarkDoc
= nullptr;
254 if (!rBookmarkFile
.isEmpty() && maBookmarkFile
!= rBookmarkFile
)
256 std::unique_ptr
<SfxMedium
> xMedium(new SfxMedium(rBookmarkFile
, StreamMode::READ
));
257 pBookmarkDoc
= OpenBookmarkDoc(xMedium
.release());
259 else if (mxBookmarkDocShRef
.is())
261 pBookmarkDoc
= mxBookmarkDocShRef
->GetDoc();
267 // Inserts a bookmark (page or object)
268 void SdDrawDocument::InsertBookmark(
269 const std::vector
<OUString
> &rBookmarkList
, // List of names of the bookmarks to be inserted
270 std::vector
<OUString
> &rExchangeList
, // List of the names to be used
271 bool bLink
, // Insert bookmarks as links?
272 sal_uInt16 nInsertPos
, // Insertion position of pages
273 ::sd::DrawDocShell
* pBookmarkDocSh
, // If set, this is the source document
274 Point
const * pObjPos
) // Insertion position of objects
277 bool bInsertPages
= false;
279 if (rBookmarkList
.empty())
286 SdDrawDocument
* pBookmarkDoc
= nullptr;
290 pBookmarkDoc
= pBookmarkDocSh
->GetDoc();
292 else if ( mxBookmarkDocShRef
.is() )
294 pBookmarkDoc
= mxBookmarkDocShRef
->GetDoc();
299 bInsertPages
= bOK
&& std::any_of(rBookmarkList
.begin(), rBookmarkList
.end(),
300 [&pBookmarkDoc
](const OUString
& rBookmark
) {
301 // Is there a page name in the bookmark list?
303 return pBookmarkDoc
->GetPageByName(rBookmark
, bIsMasterPage
) != SDRPAGE_NOTFOUND
;
307 bool bCalcObjCount
= !rExchangeList
.empty();
309 if ( bOK
&& bInsertPages
)
311 // Insert all page bookmarks
312 bOK
= InsertBookmarkAsPage(rBookmarkList
, &rExchangeList
, bLink
, false/*bReplace*/,
313 nInsertPos
, false/*bNoDialogs*/, pBookmarkDocSh
, true/*bCopy*/, true, false);
316 if ( bOK
&& !rBookmarkList
.empty() )
318 // Insert all object bookmarks
319 InsertBookmarkAsObject(rBookmarkList
, rExchangeList
,
320 pBookmarkDocSh
, pObjPos
, bCalcObjCount
);
328 lcl_removeUnusedStyles(SfxStyleSheetBasePool
* const pStyleSheetPool
, StyleSheetCopyResultVector
& rStyles
)
330 StyleSheetCopyResultVector aUsedStyles
;
331 aUsedStyles
.reserve(rStyles
.size());
332 for (const auto& a
: rStyles
)
334 if (a
.m_xStyleSheet
->IsUsed())
335 aUsedStyles
.push_back(a
);
337 pStyleSheetPool
->Remove(a
.m_xStyleSheet
.get());
339 rStyles
= aUsedStyles
;
342 SfxStyleSheet
*lcl_findStyle(StyleSheetCopyResultVector
& rStyles
, const OUString
& aStyleName
)
344 for (const auto& a
: rStyles
)
346 if (a
.m_xStyleSheet
->GetName().startsWith(aStyleName
))
347 return a
.m_xStyleSheet
.get();
354 bool SdDrawDocument::InsertBookmarkAsPage(
355 const std::vector
<OUString
> &rBookmarkList
,
356 std::vector
<OUString
> *pExchangeList
, // List of names to be used
359 sal_uInt16 nInsertPos
,
361 ::sd::DrawDocShell
* pBookmarkDocSh
,
363 bool bMergeMasterPages
,
364 bool bPreservePageNames
)
366 bool bContinue
= true;
367 bool bScaleObjects
= false;
368 sal_uInt16 nReplacedStandardPages
= 0;
370 SdDrawDocument
* pBookmarkDoc
= nullptr;
371 OUString aBookmarkName
;
375 pBookmarkDoc
= pBookmarkDocSh
->GetDoc();
377 if (pBookmarkDocSh
->GetMedium())
379 aBookmarkName
= pBookmarkDocSh
->GetMedium()->GetName();
382 else if ( mxBookmarkDocShRef
.is() )
384 pBookmarkDoc
= mxBookmarkDocShRef
->GetDoc();
385 aBookmarkName
= maBookmarkFile
;
392 const sal_uInt16 nSdPageCount
= GetSdPageCount(PageKind::Standard
);
393 const sal_uInt16 nBMSdPageCount
= pBookmarkDoc
->GetSdPageCount(PageKind::Standard
);
394 const sal_uInt16 nMPageCount
= GetMasterPageCount();
396 if (nSdPageCount
==0 || nBMSdPageCount
==0 || nMPageCount
==0)
401 // Store the size and some other properties of the first page and notes
402 // page so that inserted pages can be properly scaled even when inserted
403 // before the first page.
404 // Note that the pointers are used later on as general page pointers.
405 SdPage
* pRefPage
= GetSdPage(0, PageKind::Standard
);
406 Size
aSize(pRefPage
->GetSize());
407 sal_Int32 nLeft
= pRefPage
->GetLeftBorder();
408 sal_Int32 nRight
= pRefPage
->GetRightBorder();
409 sal_Int32 nUpper
= pRefPage
->GetUpperBorder();
410 sal_Int32 nLower
= pRefPage
->GetLowerBorder();
411 Orientation eOrient
= pRefPage
->GetOrientation();
413 SdPage
* pNPage
= GetSdPage(0, PageKind::Notes
);
414 Size
aNSize(pNPage
->GetSize());
415 sal_Int32 nNLeft
= pNPage
->GetLeftBorder();
416 sal_Int32 nNRight
= pNPage
->GetRightBorder();
417 sal_Int32 nNUpper
= pNPage
->GetUpperBorder();
418 sal_Int32 nNLower
= pNPage
->GetLowerBorder();
419 Orientation eNOrient
= pNPage
->GetOrientation();
421 // Adapt page size and margins to those of the later pages?
422 pRefPage
= GetSdPage(nSdPageCount
- 1, PageKind::Standard
);
426 // If this is clipboard, then no need to scale objects:
427 // this will make copied masters to differ from the originals,
428 // and thus InsertBookmarkAsPage_FindDuplicateLayouts will
429 // duplicate masters on insert to same document
430 m_bTransportContainer
= (SD_MOD()->pTransferClip
&&
431 SD_MOD()->pTransferClip
->GetWorkDocument() == this);
432 if (!m_bTransportContainer
)
434 if (rBookmarkList
.empty())
435 bScaleObjects
= pRefPage
->IsScaleObjects();
437 bScaleObjects
= true;
442 SdPage
* pBMPage
= pBookmarkDoc
->GetSdPage(0,PageKind::Standard
);
444 if (pBMPage
->GetSize() != pRefPage
->GetSize() ||
445 pBMPage
->GetLeftBorder() != pRefPage
->GetLeftBorder() ||
446 pBMPage
->GetRightBorder() != pRefPage
->GetRightBorder() ||
447 pBMPage
->GetUpperBorder() != pRefPage
->GetUpperBorder() ||
448 pBMPage
->GetLowerBorder() != pRefPage
->GetLowerBorder())
450 OUString
aStr(SdResId(STR_SCALE_OBJECTS
));
451 std::unique_ptr
<weld::MessageDialog
> xQueryBox(Application::CreateMessageDialog(nullptr,
452 VclMessageType::Question
, VclButtonsType::YesNo
,
454 xQueryBox
->add_button(GetStandardText(StandardButtonType::Cancel
), RET_CANCEL
);
455 sal_uInt16 nBut
= xQueryBox
->run();
457 bScaleObjects
= nBut
== RET_YES
;
458 bContinue
= nBut
!= RET_CANCEL
;
467 // Get the necessary presentation stylesheets and transfer them before
468 // the pages, else, the text objects won't reference their styles anymore.
469 SfxUndoManager
* pUndoMgr
= nullptr;
472 pUndoMgr
= mpDocSh
->GetUndoManager();
473 ViewShellId
nViewShellId(-1);
474 if (sd::ViewShell
* pViewShell
= mpDocSh
->GetViewShell())
475 nViewShellId
= pViewShell
->GetViewShellBase().GetViewShellId();
476 pUndoMgr
->EnterListAction(SdResId(STR_UNDO_INSERTPAGES
), "", 0, nViewShellId
);
479 // Refactored copy'n'pasted layout name collection into IterateBookmarkPages
481 std::vector
<OUString
> aLayoutsToTransfer
;
482 InsertBookmarkAsPage_FindDuplicateLayouts
aSearchFunctor( aLayoutsToTransfer
);
483 lcl_IterateBookmarkPages( *this, pBookmarkDoc
, rBookmarkList
, nBMSdPageCount
, aSearchFunctor
, ( rBookmarkList
.empty() && pBookmarkDoc
!= this ) );
485 // Copy the style that we actually need.
486 SdStyleSheetPool
& rBookmarkStyleSheetPool
= dynamic_cast<SdStyleSheetPool
&>(*pBookmarkDoc
->GetStyleSheetPool());
487 SdStyleSheetPool
& rStyleSheetPool
= dynamic_cast<SdStyleSheetPool
&>(*GetStyleSheetPool());
489 // When copying styles, also copy the master pages!
490 if( !aLayoutsToTransfer
.empty() )
491 bMergeMasterPages
= true;
493 for ( const OUString
& layoutName
: aLayoutsToTransfer
)
495 StyleSheetCopyResultVector aCreatedStyles
;
497 rStyleSheetPool
.CopyLayoutSheets(layoutName
, rBookmarkStyleSheetPool
,aCreatedStyles
);
499 if(!aCreatedStyles
.empty())
503 pUndoMgr
->AddUndoAction(std::make_unique
<SdMoveStyleSheetsUndoAction
>(this, aCreatedStyles
, true));
508 // Copy styles. This unconditionally copies all styles, even those
509 // that are not used in any of the inserted pages. The unused styles
510 // are then removed at the end of the function, where we also create
511 // undo records for the inserted styles.
512 StyleSheetCopyResultVector aNewGraphicStyles
;
514 if(!bReplace
&& !bNoDialogs
)
516 rStyleSheetPool
.RenameAndCopyGraphicSheets(rBookmarkStyleSheetPool
, aNewGraphicStyles
, aRenameStr
);
517 StyleSheetCopyResultVector aNewCellStyles
;
518 rStyleSheetPool
.CopyCellSheets(rBookmarkStyleSheetPool
, aNewCellStyles
);
520 // TODO handle undo of table styles too
521 rStyleSheetPool
.CopyTableStyles(rBookmarkStyleSheetPool
);
525 const bool bUndo
= IsUndoEnabled();
528 BegUndo(SdResId(STR_UNDO_INSERTPAGES
));
530 if (rBookmarkList
.empty())
532 if (nInsertPos
>= GetPageCount())
534 // Add pages to the end
535 nInsertPos
= GetPageCount();
538 sal_uInt16 nActualInsertPos
= nInsertPos
;
540 sal_uInt16 nBMSdPage
;
541 std::set
<sal_uInt16
> aRenameSet
;
542 std::map
<sal_uInt16
,OUString
> aNameMap
;
544 for (nBMSdPage
=0; nBMSdPage
< nBMSdPageCount
; nBMSdPage
++)
546 SdPage
* pBMPage
= pBookmarkDoc
->GetSdPage(nBMSdPage
, PageKind::Standard
);
547 OUString
sName(pBMPage
->GetName());
552 // Remember the names of all pages
553 aNameMap
.insert(std::make_pair(nBMSdPage
,sName
));
556 // Have to check for duplicate names here, too
557 // don't change name if source and dest model are the same!
558 if( pBookmarkDoc
!= this &&
559 GetPageByName(sName
, bIsMasterPage
) != SDRPAGE_NOTFOUND
)
561 // delay renaming *after* pages are copied (might destroy source otherwise)
562 aRenameSet
.insert(nBMSdPage
);
567 1, // Not the handout page
568 0xFFFF, // But all others
569 nActualInsertPos
, // Insert at position ...
570 bMergeMasterPages
, // Move master pages?
571 false, // But only the master pages used
572 true, // Create an undo action
573 bCopy
); // Copy (or merge) pages?
575 for (nBMSdPage
=0; nBMSdPage
< nBMSdPageCount
; nBMSdPage
++)
577 SdPage
* pPage
= static_cast<SdPage
*>( GetPage(nActualInsertPos
) );
578 SdPage
* pNotesPage
= static_cast<SdPage
*>( GetPage(nActualInsertPos
+1) );
580 // delay renaming *after* pages are copied (might destroy source otherwise)
581 if( aRenameSet
.find(nBMSdPage
) != aRenameSet
.end() )
583 // Page name already in use -> Use default name for default and
585 pPage
->SetName(OUString());
586 pNotesPage
->SetName(OUString());
591 OUString
aName(aNameMap
[nBMSdPage
]);
593 // Assemble all link names
594 pPage
->SetFileName(aBookmarkName
);
595 pPage
->SetBookmarkName(aName
);
598 nActualInsertPos
+= 2;
603 // Insert selected pages
606 if (nInsertPos
>= GetPageCount())
608 // Add pages to the end
610 nInsertPos
= GetPageCount();
613 sal_uInt16 nActualInsertPos
= nInsertPos
;
615 // Collect the bookmarked pages
616 ::std::vector
<SdPage
*> aBookmarkedPages (rBookmarkList
.size(), nullptr);
617 for ( size_t nPos
= 0, n
= rBookmarkList
.size(); nPos
< n
; ++nPos
)
619 OUString
aPgName(rBookmarkList
[nPos
]);
621 sal_uInt16 nBMPage
= pBookmarkDoc
->GetPageByName( aPgName
, bIsMasterPage
);
623 if (nBMPage
!= SDRPAGE_NOTFOUND
)
625 aBookmarkedPages
[nPos
] = dynamic_cast<SdPage
*>(pBookmarkDoc
->GetPage(nBMPage
));
629 for ( size_t nPos
= 0, n
= rBookmarkList
.size(); nPos
< n
; ++nPos
)
631 pBMPage
= aBookmarkedPages
[nPos
];
632 sal_uInt16 nBMPage
= pBMPage
!=nullptr ? pBMPage
->GetPageNum() : SDRPAGE_NOTFOUND
;
634 if (pBMPage
&& pBMPage
->GetPageKind()==PageKind::Standard
&& !pBMPage
->IsMasterPage())
636 // It has to be a default page
637 bool bMustRename
= false;
639 // delay renaming *after* pages are copied (might destroy source otherwise)
640 // don't change name if source and dest model are the same!
641 // avoid renaming if replacing the same page
642 OUString
aPgName(rBookmarkList
[nPos
]);
644 sal_uInt16 nPageSameName
= GetPageByName(aPgName
, bIsMasterPage
);
645 if( pBookmarkDoc
!= this &&
646 nPageSameName
!= SDRPAGE_NOTFOUND
&&
648 nPageSameName
!= nActualInsertPos
) )
653 SdPage
* pBookmarkPage
= pBMPage
;
656 ReplacePageInCustomShows( dynamic_cast< SdPage
* >( GetPage( nActualInsertPos
) ), pBookmarkPage
);
660 nBMPage
, // From page (default page)
661 nBMPage
+1, // To page (notes page)
662 nActualInsertPos
, // Insert at position
663 bMergeMasterPages
, // Move master pages?
664 false, // But only the master pages used
665 true, // Create undo action
666 bCopy
); // Copy (or merge) pages?
670 if( GetPage( nActualInsertPos
) != pBookmarkPage
)
672 // bookmark page was not moved but cloned, so update custom shows again
673 ReplacePageInCustomShows( pBookmarkPage
, dynamic_cast< SdPage
* >( GetPage( nActualInsertPos
) ) );
679 // Page name already in use -> use default name for default and
681 SdPage
* pPage
= static_cast<SdPage
*>( GetPage(nActualInsertPos
) );
682 pPage
->SetName(OUString());
683 SdPage
* pNotesPage
= static_cast<SdPage
*>( GetPage(nActualInsertPos
+1) );
684 pNotesPage
->SetName(OUString());
689 SdPage
* pPage
= static_cast<SdPage
*>( GetPage(nActualInsertPos
) );
690 pPage
->SetFileName(aBookmarkName
);
691 pPage
->SetBookmarkName(aPgName
);
696 // Remove page and notes page.
697 const sal_uInt16
nDestPageNum(nActualInsertPos
+ 2);
698 SdPage
* pStandardPage
= nullptr;
700 if(nDestPageNum
< GetPageCount())
702 pStandardPage
= static_cast<SdPage
*>(GetPage(nDestPageNum
));
707 if( bPreservePageNames
)
709 // Take old slide names for inserted pages
710 SdPage
* pPage
= static_cast<SdPage
*>( GetPage(nActualInsertPos
) );
711 pPage
->SetName( pStandardPage
->GetRealName() );
715 AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pStandardPage
));
717 RemovePage(nDestPageNum
);
720 delete pStandardPage
;
723 SdPage
* pNotesPage
= nullptr;
725 if(nDestPageNum
< GetPageCount())
727 pNotesPage
= static_cast<SdPage
*>(GetPage(nDestPageNum
));
732 if( bPreservePageNames
)
734 // Take old slide names for inserted pages
735 SdPage
* pNewNotesPage
= static_cast<SdPage
*>( GetPage(nActualInsertPos
+1));
737 pNewNotesPage
->SetName( pStandardPage
->GetRealName() );
741 AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pNotesPage
));
743 RemovePage(nDestPageNum
);
749 nReplacedStandardPages
++;
752 nActualInsertPos
+= 2;
757 // We might have duplicate master pages now, as the drawing engine does not
758 // recognize duplicates. Remove these now.
759 sal_uInt16 nNewMPageCount
= GetMasterPageCount();
761 // Go backwards, so the numbers don't become messed up
762 for (sal_uInt16 nPage
= nNewMPageCount
- 1; nPage
>= nMPageCount
; nPage
--)
764 pRefPage
= static_cast<SdPage
*>( GetMasterPage(nPage
) );
765 OUString
aMPLayout(pRefPage
->GetLayoutName());
766 PageKind eKind
= pRefPage
->GetPageKind();
768 // Does this already exist?
769 for (sal_uInt16 nTest
= 0; nTest
< nMPageCount
; nTest
++)
771 SdPage
* pTest
= static_cast<SdPage
*>( GetMasterPage(nTest
) );
772 OUString
aTest(pTest
->GetLayoutName());
774 // nInsertPos > 2 is always true when inserting into non-empty models
775 if ( nInsertPos
> 2 &&
776 aTest
== aMPLayout
&&
777 eKind
== pTest
->GetPageKind() )
780 AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pRefPage
));
782 RemoveMasterPage(nPage
);
792 // nInsertPos > 2 is always true when inserting into non-empty models
795 sal_uInt16 nSdPageStart
= (nInsertPos
- 1) / 2;
796 sal_uInt16 nSdPageEnd
= bReplace
797 ? nSdPageStart
+ nReplacedStandardPages
- 1
798 : GetSdPageCount(PageKind::Standard
) - nSdPageCount
+ nSdPageStart
- 1;
799 const bool bRemoveEmptyPresObj
=
800 (pBookmarkDoc
->GetDocumentType() == DocumentType::Impress
) &&
801 (GetDocumentType() == DocumentType::Draw
);
803 std::vector
<OUString
>::iterator pExchangeIter
;
806 pExchangeIter
= pExchangeList
->begin();
808 for (sal_uInt16 nSdPage
= nSdPageStart
; nSdPage
<= nSdPageEnd
; nSdPage
++)
810 pRefPage
= GetSdPage(nSdPage
, PageKind::Standard
);
812 if (pExchangeList
&& pExchangeIter
!= pExchangeList
->end())
814 // Get the name to use from Exchange list
815 OUString
aExchangeName(*pExchangeIter
);
816 pRefPage
->SetName(aExchangeName
);
817 Broadcast(SdrHint(SdrHintKind::PageOrderChange
, pRefPage
));
819 SdPage
* pNewNotesPage
= GetSdPage(nSdPage
, PageKind::Notes
);
820 pNewNotesPage
->SetName(aExchangeName
);
821 Broadcast(SdrHint(SdrHintKind::PageOrderChange
, pNewNotesPage
));
826 OUString
aLayout(pRefPage
->GetLayoutName());
827 sal_Int32 nIndex
= aLayout
.indexOf( SD_LT_SEPARATOR
);
829 aLayout
= aLayout
.copy(0, nIndex
);
831 // update layout and referred master page
832 pRefPage
->SetPresentationLayout(aLayout
);
834 AddUndo( GetSdrUndoFactory().CreateUndoPageChangeMasterPage( *pRefPage
) );
838 ::tools::Rectangle
aBorderRect(nLeft
, nUpper
, nRight
, nLower
);
839 pRefPage
->ScaleObjects(aSize
, aBorderRect
, true);
841 pRefPage
->SetSize(aSize
);
842 pRefPage
->SetBorder(nLeft
, nUpper
, nRight
, nLower
);
843 pRefPage
->SetOrientation( eOrient
);
845 if( bRemoveEmptyPresObj
)
846 pRefPage
->RemoveEmptyPresentationObjects();
848 pRefPage
= GetSdPage(nSdPage
, PageKind::Notes
);
850 // update layout and referred master page
851 pRefPage
->SetPresentationLayout(aLayout
);
853 AddUndo( GetSdrUndoFactory().CreateUndoPageChangeMasterPage( *pRefPage
) );
857 ::tools::Rectangle
aBorderRect(nNLeft
, nNUpper
, nNRight
, nNLower
);
858 pRefPage
->ScaleObjects(aNSize
, aBorderRect
, true);
861 pRefPage
->SetSize(aNSize
);
862 pRefPage
->SetBorder(nNLeft
, nNUpper
, nNRight
, nNLower
);
863 pRefPage
->SetOrientation( eNOrient
);
865 if( bRemoveEmptyPresObj
)
866 pRefPage
->RemoveEmptyPresentationObjects();
869 ///Remove processed elements, to avoid doing hacks in InsertBookmarkAsObject
871 pExchangeList
->erase(pExchangeList
->begin(),pExchangeIter
);
873 for (sal_uInt16 nPage
= nMPageCount
; nPage
< nNewMPageCount
; nPage
++)
875 pRefPage
= static_cast<SdPage
*>( GetMasterPage(nPage
) );
876 if (pRefPage
->GetPageKind() == PageKind::Standard
)
880 ::tools::Rectangle
aBorderRect(nLeft
, nUpper
, nRight
, nLower
);
881 pRefPage
->ScaleObjects(aSize
, aBorderRect
, true);
883 pRefPage
->SetSize(aSize
);
884 pRefPage
->SetBorder(nLeft
, nUpper
, nRight
, nLower
);
885 pRefPage
->SetOrientation( eOrient
);
887 else // Can only be notes
891 ::tools::Rectangle
aBorderRect(nNLeft
, nNUpper
, nNRight
, nNLower
);
892 pRefPage
->ScaleObjects(aNSize
, aBorderRect
, true);
894 pRefPage
->SetSize(aNSize
);
895 pRefPage
->SetBorder(nNLeft
, nNUpper
, nNRight
, nNLower
);
896 pRefPage
->SetOrientation( eNOrient
);
899 if( bRemoveEmptyPresObj
)
900 pRefPage
->RemoveEmptyPresentationObjects();
904 // Make absolutely sure no double masterpages are there
905 RemoveUnnecessaryMasterPages(nullptr, true);
907 // Rename object styles if necessary
908 if(!aRenameStr
.isEmpty())
912 for(sal_uInt32 p
= nInsertPos
; p
< sal_uInt32(nInsertPos
) + sal_uInt32(nBMSdPageCount
); p
++)
914 SdPage
*pPg
= static_cast<SdPage
*>( GetPage(p
) );
915 for(size_t i
= 0; pPg
&& (i
< pPg
->GetObjCount()); ++i
)
917 if(pPg
->GetObj(i
)->GetStyleSheet())
919 OUString aStyleName
= pPg
->GetObj(i
)->GetStyleSheet()->GetName();
920 SfxStyleSheet
*pSheet
= lcl_findStyle(aNewGraphicStyles
, aStyleName
+ aRenameStr
);
921 if(pSheet
!= nullptr)
922 pPg
->GetObj(i
)->SetStyleSheet(pSheet
, true);
929 OSL_FAIL("Exception while renaming styles @ SdDrawDocument::InsertBookmarkAsPage");
932 // remove copied styles not used on any inserted page and create
934 // WARNING: SdMoveStyleSheetsUndoAction clears the passed list of
935 // styles, so it cannot be used after this point
936 lcl_removeUnusedStyles(GetStyleSheetPool(), aNewGraphicStyles
);
937 if (!aNewGraphicStyles
.empty() && pUndoMgr
)
938 pUndoMgr
->AddUndoAction(std::make_unique
<SdMoveStyleSheetsUndoAction
>(this, aNewGraphicStyles
, true));
939 lcl_removeUnusedStyles(GetStyleSheetPool(), aNewCellStyles
);
940 if (!aNewCellStyles
.empty() && pUndoMgr
)
941 pUndoMgr
->AddUndoAction(std::make_unique
<SdMoveStyleSheetsUndoAction
>(this, aNewCellStyles
, true));
947 pUndoMgr
->LeaveListAction();
952 // Inserts a bookmark as an object
953 bool SdDrawDocument::InsertBookmarkAsObject(
954 const std::vector
<OUString
> &rBookmarkList
,
955 const std::vector
<OUString
> &rExchangeList
, // List of names to use
956 ::sd::DrawDocShell
* pBookmarkDocSh
,
957 Point
const * pObjPos
,
961 bool bOLEObjFound
= false;
962 std::unique_ptr
<::sd::View
> pBMView
;
964 SdDrawDocument
* pBookmarkDoc
= nullptr;
968 pBookmarkDoc
= pBookmarkDocSh
->GetDoc();
970 else if ( mxBookmarkDocShRef
.is() )
972 pBookmarkDoc
= mxBookmarkDocShRef
->GetDoc();
979 if (rBookmarkList
.empty())
981 pBMView
.reset(new ::sd::View(*pBookmarkDoc
, nullptr));
982 pBMView
->EndListening(*pBookmarkDoc
);
990 for ( const auto& rBookmark
: rBookmarkList
)
992 // Get names of bookmarks from the list
993 SdrObject
* pObj
= pBookmarkDoc
->GetObj(rBookmark
);
998 if (pObj
->GetObjInventor() == SdrInventor::Default
&&
999 pObj
->GetObjIdentifier() == OBJ_OLE2
)
1001 bOLEObjFound
= true;
1006 // Create View for the first time
1007 pBMView
.reset(new ::sd::View(*pBookmarkDoc
, nullptr));
1008 pBMView
->EndListening(*pBookmarkDoc
);
1011 pPage
= pObj
->getSdrPageFromSdrObject();
1013 if (pPage
->IsMasterPage())
1015 pPV
= pBMView
->ShowSdrPage(pBMView
->GetModel()->GetMasterPage(pPage
->GetPageNum()));
1019 pPV
= pBMView
->GetSdrPageView();
1020 if( !pPV
|| (pPV
->GetPage() != pPage
))
1021 pPV
= pBMView
->ShowSdrPage(pPage
);
1024 pBMView
->MarkObj(pObj
, pPV
);
1031 // Insert selected objects
1032 std::unique_ptr
<::sd::View
> pView(new ::sd::View(*this, nullptr));
1033 pView
->EndListening(*this);
1035 // Look for the page into which the objects are supposed to be inserted
1036 SdrPage
* pPage
= GetSdPage(0, PageKind::Standard
);
1040 ::sd::ViewShell
* pViewSh
= mpDocSh
->GetViewShell();
1044 // Which page is currently in view?
1045 SdrPageView
* pPV
= pViewSh
->GetView()->GetSdrPageView();
1049 pPage
= pPV
->GetPage();
1051 else if (pViewSh
->GetActualPage())
1053 pPage
= pViewSh
->GetActualPage();
1066 aObjPos
= ::tools::Rectangle(Point(), pPage
->GetSize()).Center();
1069 size_t nCountBefore
= 0;
1071 if (!rExchangeList
.empty() || bCalcObjCount
)
1073 // Sort OrdNums and get the number of objects before inserting
1074 pPage
->RecalcObjOrdNums();
1075 nCountBefore
= pPage
->GetObjCount();
1079 pBMView
->GetDoc().SetAllocDocSh(true);
1081 SdDrawDocument
* pTmpDoc
= static_cast<SdDrawDocument
*>( pBMView
->CreateMarkedObjModel().release() );
1082 bOK
= pView
->Paste(*pTmpDoc
, aObjPos
, pPage
, SdrInsertFlags::NONE
);
1085 pBMView
->GetDoc().SetAllocDocSh(false);
1088 delete pTmpDoc
; // Would otherwise be destroyed by DocShell
1092 // Get number of objects after inserting.
1093 const size_t nCount
= pPage
->GetObjCount();
1094 if (nCountBefore
< nCount
)
1096 size_t nObj
= nCountBefore
;
1097 for (const auto& rExchange
: rExchangeList
)
1099 // Get the name to use from the Exchange list
1100 if (pPage
->GetObj(nObj
))
1102 pPage
->GetObj(nObj
)->SetName(rExchange
);
1115 // Stops the bookmark insertion
1116 void SdDrawDocument::CloseBookmarkDoc()
1118 if (mxBookmarkDocShRef
.is())
1120 mxBookmarkDocShRef
->DoClose();
1123 mxBookmarkDocShRef
.clear();
1124 maBookmarkFile
.clear();
1127 // Is this document read-only?
1128 bool SdDrawDocument::IsReadOnly() const
1133 // In the subsequent AllocModel() a DocShell (xAllocedDocShRef) is created.
1134 // Any pre-existing DocShell is deleted
1135 void SdDrawDocument::SetAllocDocSh(bool bAlloc
)
1137 mbAllocDocSh
= bAlloc
;
1139 if(mxAllocedDocShRef
.is())
1141 mxAllocedDocShRef
->DoClose();
1144 mxAllocedDocShRef
.clear();
1147 // Return list of CustomShows (create it, too, if necessary)
1148 SdCustomShowList
* SdDrawDocument::GetCustomShowList(bool bCreate
)
1150 if (!mpCustomShowList
&& bCreate
)
1152 mpCustomShowList
.reset(new SdCustomShowList
);
1155 return mpCustomShowList
.get();
1158 // Remove unused master pages and layouts
1159 void SdDrawDocument::RemoveUnnecessaryMasterPages(SdPage
* pMasterPage
, bool bOnlyDuplicatePages
, bool bUndo
)
1161 ::sd::View
* pView
= nullptr;
1162 SfxUndoManager
* pUndoMgr
= nullptr;
1164 if( bUndo
&& !IsUndoEnabled() )
1169 pUndoMgr
= mpDocSh
->GetUndoManager();
1171 if (mpDocSh
->GetViewShell())
1172 pView
= mpDocSh
->GetViewShell()->GetView();
1175 // Check all master pages
1176 sal_uInt16 nSdMasterPageCount
= GetMasterSdPageCount( PageKind::Standard
);
1177 for (sal_Int32 nMPage
= nSdMasterPageCount
- 1; nMPage
>= 0; nMPage
--)
1179 SdPage
* pMaster
= pMasterPage
;
1180 SdPage
* pNotesMaster
= nullptr;
1184 pMaster
= GetMasterSdPage( static_cast<sal_uInt16
>(nMPage
), PageKind::Standard
);
1185 pNotesMaster
= GetMasterSdPage( static_cast<sal_uInt16
>(nMPage
), PageKind::Notes
);
1189 for ( sal_uInt16 nMPg
= 0; nMPg
< GetMasterPageCount(); nMPg
++ )
1191 if ( pMaster
== GetMasterPage( nMPg
) )
1193 pNotesMaster
= static_cast<SdPage
*>( GetMasterPage( ++nMPg
) );
1199 DBG_ASSERT( pMaster
->GetPageKind() == PageKind::Standard
, "wrong page kind" );
1201 if ( pMaster
->GetPageKind() == PageKind::Standard
&&
1202 GetMasterPageUserCount( pMaster
) == 0 &&
1205 // Do not delete master pages that have their precious flag set
1206 bool bDeleteMaster
= !pMaster
->IsPrecious();
1207 OUString aLayoutName
= pMaster
->GetLayoutName();
1209 if(bOnlyDuplicatePages
)
1211 // remove only duplicate pages
1212 bDeleteMaster
= false;
1213 for (sal_uInt16 i
= 0; i
< GetMasterSdPageCount( PageKind::Standard
); i
++)
1215 SdPage
* pMPg
= GetMasterSdPage( i
, PageKind::Standard
);
1216 if( pMPg
!= pMaster
&&
1217 pMPg
->GetLayoutName() == aLayoutName
)
1219 // duplicate page found -> remove it
1220 bDeleteMaster
= true;
1229 // if MasterPage is visible hide on pageview
1230 SdrPageView
* pPgView
= pView
->GetSdrPageView();
1233 SdrPage
* pShownPage
= pPgView
->GetPage();
1234 if( (pShownPage
== pMaster
) || (pShownPage
== pNotesMaster
) )
1236 pView
->HideSdrPage();
1237 pView
->ShowSdrPage( GetSdPage( 0, PageKind::Standard
) );
1245 AddUndo( GetSdrUndoFactory().CreateUndoDeletePage( *pNotesMaster
) );
1248 RemoveMasterPage( pNotesMaster
->GetPageNum() );
1251 delete pNotesMaster
;
1254 AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pMaster
));
1256 RemoveMasterPage( pMaster
->GetPageNum() );
1262 EndUndo(); // do this here already, so Joe's actions happen _between_ our own
1264 // Delete old, unused layout stylesheets
1265 bool bDeleteOldStyleSheets
= true;
1266 for ( sal_uInt16 nMPg
= 0;
1267 nMPg
< GetMasterPageCount() && bDeleteOldStyleSheets
;
1270 SdPage
* pMPg
= static_cast<SdPage
*>( GetMasterPage(nMPg
) );
1271 if (pMPg
->GetLayoutName() == aLayoutName
)
1273 bDeleteOldStyleSheets
= false;
1277 if (bDeleteOldStyleSheets
)
1279 SdStyleSheetVector aRemove
;
1280 static_cast<SdStyleSheetPool
*>( mxStyleSheetPool
.get())->CreateLayoutSheetList( aLayoutName
, aRemove
);
1284 StyleSheetCopyResultVector aUndoRemove
;
1285 aUndoRemove
.reserve(aRemove
.size());
1286 for (const auto& a
: aRemove
)
1287 aUndoRemove
.emplace_back(a
.get(), true);
1290 pUndoMgr
->AddUndoAction(std::make_unique
<SdMoveStyleSheetsUndoAction
>(this, aUndoRemove
, false));
1293 for( const auto& a
: aRemove
)
1294 static_cast<SdStyleSheetPool
*>( mxStyleSheetPool
.get())->Remove(a
.get());
1300 break; // Just this one master page!
1304 /** Exchange master page
1306 * Either the nSdPageNum gets a new, own master page or the master page is
1307 * exchanged completely (which then applies to all pages).
1309 * nSdPageNum : page number that the new master page should get.
1310 * rLayoutName : LayoutName of the new master page
1311 * pSourceDoc : document (template) to get the master page from
1312 * bMaster : exchange the master page of nSdPageNum
1313 * bCheckMasters: remove unused master pages
1315 * If pSourceDoc == NULL, an empty master page is applied.
1316 * If rLayoutName is empty, the first master page is used.
1318 // #i121863# factored out functionality
1319 static bool isMasterPageLayoutNameUnique(const SdDrawDocument
& rDoc
, const OUString
& rCandidate
)
1321 if (rCandidate
.isEmpty())
1326 const sal_uInt16
nPageCount(rDoc
.GetMasterPageCount());
1328 for(sal_uInt16
a(0); a
< nPageCount
; a
++)
1330 const SdrPage
* pCandidate
= rDoc
.GetMasterPage(a
);
1331 OUString
aPageLayoutName(pCandidate
->GetLayoutName());
1332 sal_Int32 nIndex
= aPageLayoutName
.indexOf(SD_LT_SEPARATOR
);
1334 aPageLayoutName
= aPageLayoutName
.copy(0, nIndex
);
1336 if(aPageLayoutName
== rCandidate
)
1345 // #i121863# factored out functinality
1346 static OUString
createNewMasterPageLayoutName(const SdDrawDocument
& rDoc
)
1348 const OUString
aBaseName(SdResId(STR_LAYOUT_DEFAULT_NAME
));
1349 sal_uInt16
nCount(0);
1352 OUString aRetval
= aBaseName
;
1355 aRetval
+= OUString::number(nCount
);
1357 if (isMasterPageLayoutNameUnique(rDoc
, aRetval
))
1363 void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum
,
1364 const OUString
& rLayoutName
,
1365 SdDrawDocument
* pSourceDoc
,
1369 SfxUndoManager
* pUndoMgr
= nullptr;
1373 mpDocSh
->SetWaitCursor( true );
1374 pUndoMgr
= mpDocSh
->GetUndoManager();
1377 const bool bUndo
= pUndoMgr
&& IsUndoEnabled();
1381 ViewShellId
nViewShellId(-1);
1382 if (sd::ViewShell
* pViewShell
= mpDocSh
->GetViewShell())
1383 nViewShellId
= pViewShell
->GetViewShellBase().GetViewShellId();
1384 pUndoMgr
->EnterListAction(SdResId(STR_UNDO_SET_PRESLAYOUT
), OUString(), 0, nViewShellId
);
1387 SdPage
* pSelectedPage
= GetSdPage(nSdPageNum
, PageKind::Standard
);
1388 SdPage
* pNotes
= static_cast<SdPage
*>( GetPage(pSelectedPage
->GetPageNum()+1) );
1389 SdPage
& rOldMaster
= static_cast<SdPage
&>(pSelectedPage
->TRG_GetMasterPage());
1390 SdPage
& rOldNotesMaster
= static_cast<SdPage
&>(pNotes
->TRG_GetMasterPage());
1391 SdPage
* pMaster
= nullptr;
1392 SdPage
* pNotesMaster
= nullptr;
1393 OUString
aOldPageLayoutName(pSelectedPage
->GetLayoutName());
1394 OUString
aOldLayoutName(aOldPageLayoutName
);
1395 sal_Int32 nIndex
= aOldLayoutName
.indexOf( SD_LT_SEPARATOR
);
1397 aOldLayoutName
= aOldLayoutName
.copy(0, nIndex
);
1401 std::vector
<StyleReplaceData
> aReplList
; // List of replaced stylesheets
1402 bool bLayoutReloaded
= false; // Was ex. layout reloaded?
1404 // LayoutName, Page and Notes page
1405 if (rLayoutName
.isEmpty())
1407 // No LayoutName: take first MasterPage
1408 pMaster
= pSourceDoc
->GetMasterSdPage(0, PageKind::Standard
);
1409 pNotesMaster
= pSourceDoc
->GetMasterSdPage(0, PageKind::Notes
);
1413 OUString aSearchFor
= rLayoutName
+ SD_LT_SEPARATOR STR_LAYOUT_OUTLINE
;
1415 for (sal_uInt16 nMP
= 0; nMP
< pSourceDoc
->GetMasterPageCount(); ++nMP
)
1417 SdPage
* pMP
= static_cast<SdPage
*>( pSourceDoc
->GetMasterPage(nMP
) );
1419 if (pMP
->GetLayoutName() == aSearchFor
)
1421 if (pMP
->GetPageKind() == PageKind::Standard
)
1423 if (pMP
->GetPageKind() == PageKind::Notes
)
1426 if (pMaster
&& pNotesMaster
)
1429 DBG_ASSERT(pMaster
, "MasterPage (Standard page) not found");
1430 DBG_ASSERT(pNotesMaster
, "MasterPage (Notes page) not found");
1432 // this should not happen, but looking at crash reports, it does
1433 if( (pMaster
== nullptr) || (pNotesMaster
== nullptr) )
1435 // so take the first MasterPage
1436 pMaster
= pSourceDoc
->GetMasterSdPage(0, PageKind::Standard
);
1437 pNotesMaster
= pSourceDoc
->GetMasterSdPage(0, PageKind::Notes
);
1441 // we should never reach this, but one never knows...
1442 if( (pMaster
== nullptr) || (pNotesMaster
== nullptr) )
1445 pUndoMgr
->LeaveListAction();
1448 mpDocSh
->SetWaitCursor( false );
1450 OSL_FAIL( "SdDrawDocument::SetMasterPage() failed!" );
1455 const OUString
aOriginalNewLayoutName( pMaster
->GetName() );
1456 OUString
aTargetNewLayoutName(aOriginalNewLayoutName
);
1458 if (pSourceDoc
!= this)
1460 // #i121863# clone masterpages, they are from another model (!)
1461 std::unique_ptr
<SdPage
> pNewNotesMaster(dynamic_cast< SdPage
* >(pNotesMaster
->CloneSdrPage(*this)));
1462 std::unique_ptr
<SdPage
> pNewMaster(dynamic_cast< SdPage
* >(pMaster
->CloneSdrPage(*this)));
1464 if(!pNewNotesMaster
|| !pNewMaster
)
1466 OSL_FAIL("SdDrawDocument::SetMasterPage() cloning of MasterPage/NoteAmsterPage failed!" );
1470 pNotesMaster
= pNewNotesMaster
.release();
1471 pMaster
= pNewMaster
.release();
1473 // layout name needs to be unique
1474 aTargetNewLayoutName
= pMaster
->GetLayoutName();
1475 sal_Int32 nIndex2
= aTargetNewLayoutName
.indexOf(SD_LT_SEPARATOR
);
1477 aTargetNewLayoutName
= aTargetNewLayoutName
.copy(0, nIndex2
);
1479 if(!isMasterPageLayoutNameUnique(*this, aTargetNewLayoutName
))
1481 aTargetNewLayoutName
= createNewMasterPageLayoutName(*this);
1483 OUString aTemp
= aTargetNewLayoutName
+ SD_LT_SEPARATOR STR_LAYOUT_OUTLINE
;
1485 pMaster
->SetName(aTargetNewLayoutName
);
1486 pMaster
->SetLayoutName(aTemp
);
1488 pNotesMaster
->SetName(aTargetNewLayoutName
);
1489 pNotesMaster
->SetLayoutName(aTemp
);
1493 if (pSourceDoc
!= this)
1495 const sal_uInt16 nMasterPageCount
= GetMasterPageCount();
1496 for ( sal_uInt16 nMPage
= 0; nMPage
< nMasterPageCount
; nMPage
++ )
1498 SdPage
* pCheckMaster
= static_cast<SdPage
*>(GetMasterPage(nMPage
));
1499 if( pCheckMaster
->GetName() == aTargetNewLayoutName
)
1501 bLayoutReloaded
= true;
1506 // Correct or create presentation templates --
1507 // only worry about presentation templates
1509 SdStyleSheetPool
* pSourceStyleSheetPool
= static_cast<SdStyleSheetPool
*>( pSourceDoc
->GetStyleSheetPool() );
1510 pSourceStyleSheetPool
->SetSearchMask(SfxStyleFamily::Page
);
1511 static_cast<SdStyleSheetPool
*>( mxStyleSheetPool
.get())->SetSearchMask(SfxStyleFamily::Page
);
1513 StyleSheetCopyResultVector aCreatedStyles
; // List of created stylesheets
1514 SfxStyleSheetBase
* pHisSheet
= pSourceStyleSheetPool
->First();
1518 aName
= pHisSheet
->GetName();
1520 // #i121863# search in source styles with original style name from source of
1521 // evtl. cloned master (not-cloned, renamed for uniqueness)
1522 if( aName
.startsWith( aOriginalNewLayoutName
) )
1524 // #i121863# build name of evtl. cloned master style to search for
1525 if(aOriginalNewLayoutName
!= aTargetNewLayoutName
)
1527 const sal_Int32
nPos(aName
.indexOf(SD_LT_SEPARATOR
));
1528 aName
= aTargetNewLayoutName
+ aName
.copy(nPos
);
1531 SfxStyleSheet
* pMySheet
= static_cast<SfxStyleSheet
*>( mxStyleSheetPool
->Find(aName
, SfxStyleFamily::Page
) );
1535 // A stylesheet of the same name already exists -> overwrite contents
1536 bool bTest
= pMySheet
->SetName(pHisSheet
->GetName());
1537 DBG_ASSERT(bTest
, "Renaming StyleSheet failed.");
1538 pMySheet
->GetItemSet().ClearItem(); // Delete all
1542 pUndoMgr
->AddUndoAction(std::make_unique
<StyleSheetUndoAction
>(this,
1543 pMySheet
, &pHisSheet
->GetItemSet()));
1545 pMySheet
->GetItemSet().Put(pHisSheet
->GetItemSet());
1546 pMySheet
->Broadcast(SfxHint(SfxHintId::DataChanged
));
1552 pMySheet
= static_cast<SfxStyleSheet
*>( &mxStyleSheetPool
->Make(aName
, SfxStyleFamily::Page
, pHisSheet
->GetMask()) );
1553 pMySheet
->SetHelpId( aHelpFile
, pHisSheet
->GetHelpId(aHelpFile
) );
1554 pMySheet
->GetItemSet().ClearItem(); // Delete all
1555 pMySheet
->GetItemSet().Put(pHisSheet
->GetItemSet());
1557 aCreatedStyles
.emplace_back(static_cast<SdStyleSheet
*>(pMySheet
), true);
1560 StyleReplaceData aReplData
;
1561 aReplData
.nNewFamily
= pMySheet
->GetFamily();
1562 aReplData
.nFamily
= pMySheet
->GetFamily();
1563 aReplData
.aNewName
= pMySheet
->GetName();
1565 // #i121863# re-create original name of style used at page where to replace with
1567 OUString
aTemp(pMySheet
->GetName());
1568 const sal_Int32
nPos(aTemp
.indexOf(SD_LT_SEPARATOR
));
1569 aTemp
= aOldLayoutName
+ aTemp
.copy(nPos
);
1570 aReplData
.aName
= aTemp
;
1571 aReplList
.push_back(aReplData
);
1574 pHisSheet
= pSourceStyleSheetPool
->Next();
1577 // If new styles were created: re-create parent chaining of the item
1578 // sets in the styles.
1579 if(!aCreatedStyles
.empty())
1581 for ( const auto& rRData
: aReplList
)
1583 SfxStyleSheetBase
* pSOld
= mxStyleSheetPool
->Find(rRData
.aName
);
1584 SfxStyleSheetBase
* pSNew
= mxStyleSheetPool
->Find(rRData
.aNewName
);
1588 const OUString
& rParentOfOld
= pSOld
->GetParent();
1589 const OUString
& rParentOfNew
= pSNew
->GetParent();
1591 if (!rParentOfOld
.isEmpty() && rParentOfNew
.isEmpty())
1593 std::vector
<StyleReplaceData
>::iterator pRDIter
= std::find_if(aReplList
.begin(), aReplList
.end(),
1594 [&rParentOfOld
](const StyleReplaceData
& rRD
) { return (rRD
.aName
== rParentOfOld
) && (rRD
.aName
!= rRD
.aNewName
); });
1595 if (pRDIter
!= aReplList
.end())
1597 OUString
aParentOfNew(pRDIter
->aNewName
);
1598 pSNew
->SetParent(aParentOfNew
);
1604 // Now look for all of them when searching
1605 pSourceStyleSheetPool
->SetSearchMask(SfxStyleFamily::All
);
1606 mxStyleSheetPool
->SetSearchMask(SfxStyleFamily::All
);
1609 if (bUndo
&& !aCreatedStyles
.empty())
1611 // Add UndoAction for creating and inserting the stylesheets to
1612 // the top of the UndoManager
1613 pUndoMgr
->AddUndoAction(std::make_unique
<SdMoveStyleSheetsUndoAction
>( this, aCreatedStyles
, true));
1617 // Create layout name based upon the name of the page layout of the
1619 OUString
aPageLayoutName(pMaster
->GetLayoutName());
1620 OUString aLayoutName
= aPageLayoutName
;
1621 sal_Int32 nIndex2
= aLayoutName
.indexOf( SD_LT_SEPARATOR
);
1623 aLayoutName
= aLayoutName
.copy( 0, nIndex2
);
1625 // #i121863# Do *not* remove from original document any longer, it is potentially used there
1626 // and would lead to crashes. Rely on the automatic process of removing unused masterpages
1627 // (see RemoveUnnecessaryMasterPages)
1628 //if (pSourceDoc != this)
1630 // // Remove from the source document
1631 // pSourceDoc->RemoveMasterPage(pNotesMaster->GetPageNum());
1632 // pSourceDoc->RemoveMasterPage(pMaster->GetPageNum());
1635 // Register the new master pages with the document and then use
1636 // the new presentation layout for the default and notes pages
1637 if (pSourceDoc
!= this)
1639 // Insert the master pages:
1640 // Insert master pages from new layouts at the end.
1641 // If a layout is being replaced, however, insert them before the
1642 // position of the old master page, so from now on the new master
1643 // page will be found when searching (e.g.
1644 // SdPage::SetPresentationLayout).
1645 sal_uInt16 nInsertPos
= rOldMaster
.GetPageNum();
1648 if (!bLayoutReloaded
)
1649 nInsertPos
= 0xFFFF;
1650 InsertMasterPage(pMaster
, nInsertPos
);
1652 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pMaster
));
1655 if (!bLayoutReloaded
)
1656 nInsertPos
= 0xFFFF;
1657 InsertMasterPage(pNotesMaster
, nInsertPos
);
1660 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pNotesMaster
));
1662 EndUndo(); // do this here already, so Joe's actions happen _between_ our own.
1666 // Fill list with pages
1667 std::vector
<SdPage
*> aPageList
;
1669 // #98456, this has to be removed according to CL (KA 07/08/2002)
1670 // #109884# but we need them again to restore the styles of the presentation objects while undo
1671 aPageList
.push_back(pMaster
);
1672 aPageList
.push_back(pNotesMaster
);
1674 if (bMaster
|| bLayoutReloaded
)
1676 for (sal_uInt16 nPage
= 1; nPage
< GetPageCount(); nPage
++)
1678 SdPage
* pPage
= static_cast<SdPage
*>( GetPage(nPage
) );
1679 OUString aTest
= pPage
->GetLayoutName();
1680 if (aTest
== aOldPageLayoutName
)
1682 aPageList
.push_back(pPage
);
1689 aPageList
.push_back(pSelectedPage
);
1690 aPageList
.push_back(pNotes
);
1693 for (SdPage
* pPage
: aPageList
)
1695 AutoLayout eAutoLayout
= pPage
->GetAutoLayout();
1699 pUndoMgr
->AddUndoAction(std::make_unique
<SdPresentationLayoutUndoAction
>
1701 pPage
->IsMasterPage() ? aLayoutName
: aOldLayoutName
,
1703 eAutoLayout
, eAutoLayout
, false, pPage
));
1705 pPage
->SetPresentationLayout(aLayoutName
);
1706 pPage
->SetAutoLayout(eAutoLayout
);
1709 // Adapt new master pages
1710 if (pSourceDoc
!= this)
1712 Size
aSize(rOldMaster
.GetSize());
1713 ::tools::Rectangle
aBorderRect(rOldMaster
.GetLeftBorder(),
1714 rOldMaster
.GetUpperBorder(),
1715 rOldMaster
.GetRightBorder(),
1716 rOldMaster
.GetLowerBorder());
1717 pMaster
->ScaleObjects(aSize
, aBorderRect
, true);
1718 pMaster
->SetSize(aSize
);
1719 pMaster
->SetBorder(rOldMaster
.GetLeftBorder(),
1720 rOldMaster
.GetUpperBorder(),
1721 rOldMaster
.GetRightBorder(),
1722 rOldMaster
.GetLowerBorder());
1723 pMaster
->SetOrientation( rOldMaster
.GetOrientation() );
1724 pMaster
->SetAutoLayout(pMaster
->GetAutoLayout());
1726 aSize
= rOldNotesMaster
.GetSize();
1727 ::tools::Rectangle
aNotesBorderRect(rOldNotesMaster
.GetLeftBorder(),
1728 rOldNotesMaster
.GetUpperBorder(),
1729 rOldNotesMaster
.GetRightBorder(),
1730 rOldNotesMaster
.GetLowerBorder());
1731 pNotesMaster
->ScaleObjects(aSize
, aNotesBorderRect
, true);
1732 pNotesMaster
->SetSize(aSize
);
1733 pNotesMaster
->SetBorder(rOldNotesMaster
.GetLeftBorder(),
1734 rOldNotesMaster
.GetUpperBorder(),
1735 rOldNotesMaster
.GetRightBorder(),
1736 rOldNotesMaster
.GetLowerBorder());
1737 pNotesMaster
->SetOrientation( rOldNotesMaster
.GetOrientation() );
1738 pNotesMaster
->SetAutoLayout(pNotesMaster
->GetAutoLayout());
1740 if( (pSourceDoc
->GetDocumentType() == DocumentType::Impress
) &&
1741 (GetDocumentType() == DocumentType::Draw
) )
1743 pMaster
->RemoveEmptyPresentationObjects();
1744 pNotesMaster
->RemoveEmptyPresentationObjects();
1750 // Find a new name for the layout
1751 OUString
aName(createNewMasterPageLayoutName(*this));
1752 OUString
aPageLayoutName(aName
+ SD_LT_SEPARATOR STR_LAYOUT_OUTLINE
);
1754 // Generate new stylesheets
1755 static_cast<SdStyleSheetPool
*>( mxStyleSheetPool
.get())->CreateLayoutStyleSheets(aName
);
1756 SdStyleSheetVector aCreatedStyles
;
1757 static_cast<SdStyleSheetPool
*>( mxStyleSheetPool
.get())->CreateLayoutSheetList(aName
, aCreatedStyles
);
1761 StyleSheetCopyResultVector aUndoInsert
;
1762 aUndoInsert
.reserve(aCreatedStyles
.size());
1763 for (const auto& a
: aCreatedStyles
)
1764 aUndoInsert
.emplace_back(a
.get(), true);
1765 pUndoMgr
->AddUndoAction(std::make_unique
<SdMoveStyleSheetsUndoAction
>(this, aUndoInsert
, true));
1766 // Generate new master pages and register them with the document
1770 pMaster
= AllocSdPage(true);
1771 pMaster
->SetSize(pSelectedPage
->GetSize());
1772 pMaster
->SetBorder(pSelectedPage
->GetLeftBorder(),
1773 pSelectedPage
->GetUpperBorder(),
1774 pSelectedPage
->GetRightBorder(),
1775 pSelectedPage
->GetLowerBorder() );
1776 pMaster
->SetName(aName
);
1777 pMaster
->SetLayoutName(aPageLayoutName
);
1778 InsertMasterPage(pMaster
);
1781 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pMaster
));
1783 pMaster
->SetAutoLayout(AUTOLAYOUT_NONE
, true, true);
1785 pNotesMaster
= AllocSdPage(true);
1786 pNotesMaster
->SetPageKind(PageKind::Notes
);
1787 pNotesMaster
->SetSize(pNotes
->GetSize());
1788 pNotesMaster
->SetBorder(pNotes
->GetLeftBorder(),
1789 pNotes
->GetUpperBorder(),
1790 pNotes
->GetRightBorder(),
1791 pNotes
->GetLowerBorder() );
1792 pNotesMaster
->SetName(aName
);
1793 pNotesMaster
->SetLayoutName(aPageLayoutName
);
1794 InsertMasterPage(pNotesMaster
);
1797 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pNotesMaster
));
1799 pNotesMaster
->SetAutoLayout(AUTOLAYOUT_NOTES
, true, true);
1804 // Create a list of affected default and notes pages
1805 std::vector
<SdPage
*> aPageList
;
1808 for (sal_uInt16 nPage
= 1; nPage
< GetPageCount(); nPage
++)
1810 SdPage
* pPage
= static_cast<SdPage
*>( GetPage(nPage
) );
1811 if (pPage
->GetLayoutName() == aOldPageLayoutName
)
1813 aPageList
.push_back(pPage
);
1819 aPageList
.push_back(pSelectedPage
);
1820 aPageList
.push_back(pNotes
);
1823 // Set presentation layout and AutoLayout for the affected pages
1824 for ( auto& rpPage
: aPageList
)
1826 AutoLayout eOldAutoLayout
= rpPage
->GetAutoLayout();
1827 AutoLayout eNewAutoLayout
=
1828 rpPage
->GetPageKind() == PageKind::Standard
? AUTOLAYOUT_NONE
: AUTOLAYOUT_NOTES
;
1832 pUndoMgr
->AddUndoAction(std::make_unique
<SdPresentationLayoutUndoAction
>
1833 (this, aOldLayoutName
, aName
,
1834 eOldAutoLayout
, eNewAutoLayout
, true,
1838 rpPage
->SetPresentationLayout(aName
);
1839 rpPage
->SetAutoLayout(eNewAutoLayout
);
1843 // If the old master pages aren't used anymore, they and their styles have
1848 RemoveUnnecessaryMasterPages();
1852 // Check only the master page that was replaced
1853 RemoveUnnecessaryMasterPages(&rOldMaster
);
1857 pUndoMgr
->LeaveListAction();
1860 mpDocSh
->SetWaitCursor( false );
1863 void SdDrawDocument::Merge(SdrModel
& rSourceModel
,
1864 sal_uInt16 nFirstPageNum
, sal_uInt16 nLastPageNum
,
1865 sal_uInt16 nDestPos
,
1866 bool bMergeMasterPages
, bool bAllMasterPages
,
1867 bool bUndo
, bool bTreadSourceAsConst
)
1869 sal_uInt16 nMasterPageCount
= GetMasterPageCount();
1870 SdrModel::Merge( rSourceModel
, nFirstPageNum
, nLastPageNum
, nDestPos
, bMergeMasterPages
, bAllMasterPages
, bUndo
, bTreadSourceAsConst
);
1872 // add style family for each new master page
1873 for( sal_uInt16 nMaster
= nMasterPageCount
; nMaster
< GetMasterPageCount(); nMaster
++ )
1875 SdPage
* pPage
= static_cast< SdPage
* >( GetMasterPage( nMaster
) );
1876 if( pPage
&& pPage
->IsMasterPage() && (pPage
->GetPageKind() == PageKind::Standard
) )
1878 // new master page created, add its style family
1879 SdStyleSheetPool
* pStylePool
= static_cast<SdStyleSheetPool
*>( GetStyleSheetPool() );
1881 pStylePool
->AddStyleFamily( pPage
);
1886 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */