Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sd / source / core / drawdoc3.cxx
blob00006fbefd24daa9c06ec221987cf8f9ef217900
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <memory>
22 #include <string_view>
24 #include <sfx2/docfile.hxx>
25 #include <sfx2/docfilt.hxx>
26 #include <sfx2/app.hxx>
27 #include <svl/itemset.hxx>
28 #include <tools/debug.hxx>
29 #include <comphelper/diagnose_ex.hxx>
31 #include <sfx2/fcontnr.hxx>
32 #include <svl/style.hxx>
33 #include <svx/svdpagv.hxx>
34 #include <svx/svdundo.hxx>
35 #include <vcl/stdtext.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/weld.hxx>
38 #include <xmloff/autolayout.hxx>
40 #include <strings.hrc>
41 #include <drawdoc.hxx>
42 #include <sdmod.hxx>
43 #include <sdpage.hxx>
44 #include <stlpool.hxx>
45 #include <sdresid.hxx>
46 #include <customshowlist.hxx>
47 #include <sdxfer.hxx>
49 #include <unmovss.hxx>
50 #include <unchss.hxx>
51 #include <unprlout.hxx>
52 #include <DrawDocShell.hxx>
53 #include <GraphicDocShell.hxx>
54 #include <ViewShell.hxx>
55 #include <View.hxx>
56 #include <ViewShellBase.hxx>
57 #include <strings.hxx>
59 using namespace ::com::sun::star;
61 /** Concrete incarnations get called by lcl_IterateBookmarkPages, for
62 every page in the bookmark document/list
65 namespace {
67 class InsertBookmarkAsPage_FindDuplicateLayouts
69 public:
70 explicit InsertBookmarkAsPage_FindDuplicateLayouts( std::vector<OUString> &rLayoutsToTransfer )
71 : mrLayoutsToTransfer(rLayoutsToTransfer) {}
72 void operator()( SdDrawDocument&, SdPage const *, bool, SdDrawDocument* );
73 private:
74 std::vector<OUString> &mrLayoutsToTransfer;
79 void InsertBookmarkAsPage_FindDuplicateLayouts::operator()( SdDrawDocument& rDoc, SdPage const * pBMMPage, bool bRenameDuplicates, SdDrawDocument* pBookmarkDoc )
81 // now check for duplicate masterpage and layout names
83 OUString aLayout( pBMMPage->GetLayoutName() );
84 sal_Int32 nIndex = aLayout.indexOf( SD_LT_SEPARATOR );
85 if( nIndex != -1 )
86 aLayout = aLayout.copy(0, nIndex);
88 std::vector<OUString>::const_iterator pIter =
89 find(mrLayoutsToTransfer.begin(),mrLayoutsToTransfer.end(),aLayout);
91 bool bFound = pIter != mrLayoutsToTransfer.end();
93 const sal_uInt16 nMPageCount = rDoc.GetMasterPageCount();
94 for (sal_uInt16 nMPage = 0; nMPage < nMPageCount && !bFound; nMPage++)
96 // Do the layouts already exist within the document?
97 SdPage* pTestPage = static_cast<SdPage*>( rDoc.GetMasterPage(nMPage) );
98 OUString aTest(pTestPage->GetLayoutName());
99 sal_Int32 nIndex2 = aTest.indexOf( SD_LT_SEPARATOR );
100 if( nIndex2 != -1 )
101 aTest = aTest.copy(0, nIndex2);
103 if (aTest == aLayout && pBMMPage->GetPageKind() == pTestPage->GetPageKind())
105 // Ignore Layouts with "Default" these seem to be special - in the sense that there are lot of assumption all over Impress
106 // about this
107 if( bRenameDuplicates && aTest != SdResId( STR_LAYOUT_DEFAULT_NAME ) && !(pTestPage->Equals(*pBMMPage)) )
109 pBookmarkDoc->RenameLayoutTemplate(
110 pBMMPage->GetLayoutName(), pBMMPage->GetName() + "_");
111 aLayout = pBMMPage->GetName();
113 break;
115 else
116 bFound = true;
120 if (!bFound)
121 mrLayoutsToTransfer.push_back(aLayout);
124 // Inserts a bookmark as a page
125 static void lcl_IterateBookmarkPages( SdDrawDocument &rDoc, SdDrawDocument* pBookmarkDoc,
126 const std::vector<OUString> &rBookmarkList, sal_uInt16 nBMSdPageCount,
127 InsertBookmarkAsPage_FindDuplicateLayouts& rPageIterator, bool bRenameDuplicates )
130 // Refactored copy'n'pasted layout name collection from InsertBookmarkAsPage
132 int nPos, nEndPos;
134 if( rBookmarkList.empty() )
136 // no list? whole source document
137 nEndPos = nBMSdPageCount;
139 else
141 // bookmark list? number of entries
142 nEndPos = rBookmarkList.size();
145 SdPage* pBMPage;
147 // iterate over number of pages to insert
148 for (nPos = 0; nPos < nEndPos; ++nPos)
150 // the master page associated to the nPos'th page to insert
151 SdPage* pBMMPage = nullptr;
153 if( rBookmarkList.empty() )
155 // simply take master page of nPos'th page in source document
156 pBMMPage = static_cast<SdPage*>(&(pBookmarkDoc->GetSdPage(static_cast<sal_uInt16>(nPos), PageKind::Standard)->TRG_GetMasterPage()));
158 else
160 // fetch nPos'th entry from bookmark list, and determine master page
161 OUString aBMPgName(rBookmarkList[nPos]);
162 bool bIsMasterPage;
164 sal_uInt16 nBMPage = pBookmarkDoc->GetPageByName( aBMPgName, bIsMasterPage );
166 if (nBMPage != SDRPAGE_NOTFOUND)
168 pBMPage = static_cast<SdPage*>( pBookmarkDoc->GetPage(nBMPage) );
170 else
172 pBMPage = nullptr;
175 // enforce that bookmarked page is a standard page and not already a master page
176 if (pBMPage && pBMPage->GetPageKind()==PageKind::Standard && !pBMPage->IsMasterPage())
178 const sal_uInt16 nBMSdPage = (nBMPage - 1) / 2;
179 pBMMPage = static_cast<SdPage*> (&(pBookmarkDoc->GetSdPage(nBMSdPage, PageKind::Standard)->TRG_GetMasterPage()));
183 // successfully determined valid (bookmarked) page?
184 if( pBMMPage )
186 // yes, call functor
187 rPageIterator( rDoc, pBMMPage, bRenameDuplicates, pBookmarkDoc );
192 // Opens a bookmark document
193 SdDrawDocument* SdDrawDocument::OpenBookmarkDoc(SfxMedium* pMedium)
195 bool bOK = true;
196 SdDrawDocument* pBookmarkDoc = nullptr;
197 OUString aBookmarkName = pMedium->GetName();
198 std::shared_ptr<const SfxFilter> pFilter = pMedium->GetFilter();
199 if ( !pFilter )
201 pMedium->UseInteractionHandler( true );
202 SfxGetpApp()->GetFilterMatcher().GuessFilter(*pMedium, pFilter);
205 if ( !pFilter )
207 bOK = false;
209 else if ( !aBookmarkName.isEmpty() && maBookmarkFile != aBookmarkName )
211 bool bCreateGraphicShell = pFilter->GetServiceName() == "com.sun.star.drawing.DrawingDocument";
212 bool bCreateImpressShell = pFilter->GetServiceName() == "com.sun.star.presentation.PresentationDocument";
213 if ( bCreateGraphicShell || bCreateImpressShell )
215 CloseBookmarkDoc();
217 // Create a DocShell, as OLE objects might be contained in the
218 // document. (Persist)
219 // If that wasn't the case, we could load the model directly.
220 if ( bCreateGraphicShell )
221 // Draw
222 mxBookmarkDocShRef = new ::sd::GraphicDocShell(SfxObjectCreateMode::STANDARD);
223 else
224 // Impress
225 mxBookmarkDocShRef = new ::sd::DrawDocShell(SfxObjectCreateMode::STANDARD, true, DocumentType::Impress);
227 bOK = mxBookmarkDocShRef->DoLoad(pMedium);
228 if( bOK )
230 maBookmarkFile = aBookmarkName;
231 pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
236 DBG_ASSERT(!aBookmarkName.isEmpty(), "Empty document name!");
238 if (!bOK)
240 std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(nullptr,
241 VclMessageType::Warning, VclButtonsType::Ok, SdResId(STR_READ_DATA_ERROR)));
242 xErrorBox->run();
244 CloseBookmarkDoc();
245 pBookmarkDoc = nullptr;
247 else if (mxBookmarkDocShRef.is())
249 pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
252 return pBookmarkDoc;
255 // Opens a bookmark document
256 SdDrawDocument* SdDrawDocument::OpenBookmarkDoc(const OUString& rBookmarkFile)
258 SdDrawDocument* pBookmarkDoc = nullptr;
260 if (!rBookmarkFile.isEmpty() && maBookmarkFile != rBookmarkFile)
262 std::unique_ptr<SfxMedium> xMedium(new SfxMedium(rBookmarkFile, StreamMode::READ));
263 pBookmarkDoc = OpenBookmarkDoc(xMedium.release());
265 else if (mxBookmarkDocShRef.is())
267 pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
270 return pBookmarkDoc;
273 // Inserts a bookmark (page or object)
274 void SdDrawDocument::InsertBookmark(
275 const std::vector<OUString> &rBookmarkList, // List of names of the bookmarks to be inserted
276 std::vector<OUString> &rExchangeList, // List of the names to be used
277 bool bLink, // Insert bookmarks as links?
278 sal_uInt16 nInsertPos, // Insertion position of pages
279 ::sd::DrawDocShell* pBookmarkDocSh, // If set, this is the source document
280 Point const * pObjPos) // Insertion position of objects
282 bool bOK = true;
283 bool bInsertPages = false;
285 if (rBookmarkList.empty())
287 // Insert all pages
288 bInsertPages = true;
290 else
292 SdDrawDocument* pBookmarkDoc = nullptr;
294 if (pBookmarkDocSh)
296 pBookmarkDoc = pBookmarkDocSh->GetDoc();
298 else if ( mxBookmarkDocShRef.is() )
300 pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
302 else
303 bOK = false;
305 bInsertPages = bOK && std::any_of(rBookmarkList.begin(), rBookmarkList.end(),
306 [&pBookmarkDoc](const OUString& rBookmark) {
307 // Is there a page name in the bookmark list?
308 bool bIsMasterPage;
309 return pBookmarkDoc->GetPageByName(rBookmark, bIsMasterPage) != SDRPAGE_NOTFOUND;
313 bool bCalcObjCount = !rExchangeList.empty();
315 if ( bOK && bInsertPages )
317 // Insert all page bookmarks
318 bOK = InsertBookmarkAsPage(rBookmarkList, &rExchangeList, bLink, false/*bReplace*/,
319 nInsertPos, false/*bNoDialogs*/, pBookmarkDocSh, true/*bCopy*/, true, false);
322 if ( bOK && !rBookmarkList.empty() )
324 // Insert all object bookmarks
325 InsertBookmarkAsObject(rBookmarkList, rExchangeList,
326 pBookmarkDocSh, pObjPos, bCalcObjCount);
330 namespace
333 void
334 lcl_removeUnusedStyles(SfxStyleSheetBasePool* const pStyleSheetPool, StyleSheetCopyResultVector& rStyles)
336 StyleSheetCopyResultVector aUsedStyles;
337 aUsedStyles.reserve(rStyles.size());
338 for (const auto& a : rStyles)
340 if (a.m_xStyleSheet->IsUsed())
341 aUsedStyles.push_back(a);
342 else
343 pStyleSheetPool->Remove(a.m_xStyleSheet.get());
345 rStyles = aUsedStyles;
348 void
349 lcl_removeUnusedTableStyles(SdStyleSheetPool* const pStyleSheetPool, XStyleVector const & rStyles)
351 css::uno::Reference<css::container::XNameContainer> xTableFamily(
352 pStyleSheetPool->getByName("table"), css::uno::UNO_QUERY);
353 if (!xTableFamily)
354 return;
356 for (const auto& a : rStyles)
358 if (!a->isInUse())
359 xTableFamily->removeByName(a->getName());
363 SfxStyleSheet *lcl_findStyle(StyleSheetCopyResultVector& rStyles, std::u16string_view aStyleName)
365 for (const auto& a : rStyles)
367 if (a.m_xStyleSheet->GetName().startsWith(aStyleName))
368 return a.m_xStyleSheet.get();
370 return nullptr;
375 bool SdDrawDocument::InsertBookmarkAsPage(
376 const std::vector<OUString> &rBookmarkList,
377 std::vector<OUString> *pExchangeList, // List of names to be used
378 bool bLink,
379 bool bReplace,
380 sal_uInt16 nInsertPos,
381 bool bNoDialogs,
382 ::sd::DrawDocShell* pBookmarkDocSh,
383 bool bCopy,
384 bool bMergeMasterPages,
385 bool bPreservePageNames)
387 bool bContinue = true;
388 bool bScaleObjects = false;
389 sal_uInt16 nReplacedStandardPages = 0;
391 SdDrawDocument* pBookmarkDoc = nullptr;
392 OUString aBookmarkName;
394 if (pBookmarkDocSh)
396 pBookmarkDoc = pBookmarkDocSh->GetDoc();
398 if (pBookmarkDocSh->GetMedium())
400 aBookmarkName = pBookmarkDocSh->GetMedium()->GetName();
403 else if ( mxBookmarkDocShRef.is() )
405 pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
406 aBookmarkName = maBookmarkFile;
408 else
410 return false;
413 const sal_uInt16 nSdPageCount = GetSdPageCount(PageKind::Standard);
414 const sal_uInt16 nBMSdPageCount = pBookmarkDoc->GetSdPageCount(PageKind::Standard);
415 const sal_uInt16 nMPageCount = GetMasterPageCount();
417 if (nSdPageCount==0 || nBMSdPageCount==0 || nMPageCount==0)
419 return false;
422 // Store the size and some other properties of the first page and notes
423 // page so that inserted pages can be properly scaled even when inserted
424 // before the first page.
425 // Note that the pointers are used later on as general page pointers.
426 SdPage* pRefPage = GetSdPage(0, PageKind::Standard);
427 Size aSize(pRefPage->GetSize());
428 sal_Int32 nLeft = pRefPage->GetLeftBorder();
429 sal_Int32 nRight = pRefPage->GetRightBorder();
430 sal_Int32 nUpper = pRefPage->GetUpperBorder();
431 sal_Int32 nLower = pRefPage->GetLowerBorder();
432 Orientation eOrient = pRefPage->GetOrientation();
434 SdPage* pNPage = GetSdPage(0, PageKind::Notes);
435 Size aNSize(pNPage->GetSize());
436 sal_Int32 nNLeft = pNPage->GetLeftBorder();
437 sal_Int32 nNRight = pNPage->GetRightBorder();
438 sal_Int32 nNUpper = pNPage->GetUpperBorder();
439 sal_Int32 nNLower = pNPage->GetLowerBorder();
440 Orientation eNOrient = pNPage->GetOrientation();
442 // Adapt page size and margins to those of the later pages?
443 pRefPage = GetSdPage(nSdPageCount - 1, PageKind::Standard);
445 if( bNoDialogs )
447 // If this is clipboard, then no need to scale objects:
448 // this will make copied masters to differ from the originals,
449 // and thus InsertBookmarkAsPage_FindDuplicateLayouts will
450 // duplicate masters on insert to same document
451 m_bTransportContainer = (SD_MOD()->pTransferClip &&
452 SD_MOD()->pTransferClip->GetWorkDocument() == this);
453 if (!m_bTransportContainer)
455 if (rBookmarkList.empty())
456 bScaleObjects = pRefPage->IsScaleObjects();
457 else
458 bScaleObjects = true;
461 else
463 SdPage* pBMPage = pBookmarkDoc->GetSdPage(0,PageKind::Standard);
465 if (pBMPage->GetSize() != pRefPage->GetSize() ||
466 pBMPage->GetLeftBorder() != pRefPage->GetLeftBorder() ||
467 pBMPage->GetRightBorder() != pRefPage->GetRightBorder() ||
468 pBMPage->GetUpperBorder() != pRefPage->GetUpperBorder() ||
469 pBMPage->GetLowerBorder() != pRefPage->GetLowerBorder())
471 OUString aStr(SdResId(STR_SCALE_OBJECTS));
472 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(nullptr,
473 VclMessageType::Question, VclButtonsType::YesNo,
474 aStr));
475 xQueryBox->add_button(GetStandardText(StandardButtonType::Cancel), RET_CANCEL);
476 sal_uInt16 nBut = xQueryBox->run();
478 bScaleObjects = nBut == RET_YES;
479 bContinue = nBut != RET_CANCEL;
481 if (!bContinue)
483 return bContinue;
488 // Get the necessary presentation stylesheets and transfer them before
489 // the pages, else, the text objects won't reference their styles anymore.
490 SfxUndoManager* pUndoMgr = nullptr;
491 if( mpDocSh )
493 pUndoMgr = mpDocSh->GetUndoManager();
494 ViewShellId nViewShellId(-1);
495 if (sd::ViewShell* pViewShell = mpDocSh->GetViewShell())
496 nViewShellId = pViewShell->GetViewShellBase().GetViewShellId();
497 pUndoMgr->EnterListAction(SdResId(STR_UNDO_INSERTPAGES), "", 0, nViewShellId);
500 // Refactored copy'n'pasted layout name collection into IterateBookmarkPages
502 std::vector<OUString> aLayoutsToTransfer;
503 InsertBookmarkAsPage_FindDuplicateLayouts aSearchFunctor( aLayoutsToTransfer );
504 lcl_IterateBookmarkPages( *this, pBookmarkDoc, rBookmarkList, nBMSdPageCount, aSearchFunctor, ( rBookmarkList.empty() && pBookmarkDoc != this ) );
506 // Copy the style that we actually need.
507 SdStyleSheetPool& rBookmarkStyleSheetPool = dynamic_cast<SdStyleSheetPool&>(*pBookmarkDoc->GetStyleSheetPool());
508 SdStyleSheetPool& rStyleSheetPool = dynamic_cast<SdStyleSheetPool&>(*GetStyleSheetPool());
510 // When copying styles, also copy the master pages!
511 if( !aLayoutsToTransfer.empty() )
512 bMergeMasterPages = true;
514 for ( const OUString& layoutName : aLayoutsToTransfer )
516 StyleSheetCopyResultVector aCreatedStyles;
518 rStyleSheetPool.CopyLayoutSheets(layoutName, rBookmarkStyleSheetPool,aCreatedStyles);
520 if(!aCreatedStyles.empty())
522 if( pUndoMgr )
524 pUndoMgr->AddUndoAction(std::make_unique<SdMoveStyleSheetsUndoAction>(this, aCreatedStyles, true));
529 // Copy styles. This unconditionally copies all styles, even those
530 // that are not used in any of the inserted pages. The unused styles
531 // are then removed at the end of the function, where we also create
532 // undo records for the inserted styles.
533 StyleSheetCopyResultVector aNewGraphicStyles;
534 OUString aRenameStr;
535 if(!bReplace && !bNoDialogs)
536 aRenameStr = "_";
537 rStyleSheetPool.RenameAndCopyGraphicSheets(rBookmarkStyleSheetPool, aNewGraphicStyles, aRenameStr);
538 StyleSheetCopyResultVector aNewCellStyles;
539 rStyleSheetPool.CopyCellSheets(rBookmarkStyleSheetPool, aNewCellStyles);
541 // TODO handle undo of table styles too
542 XStyleVector aNewTableStyles;
543 rStyleSheetPool.CopyTableStyles(rBookmarkStyleSheetPool, aNewTableStyles);
545 // Insert document
547 const bool bUndo = IsUndoEnabled();
549 if( bUndo )
550 BegUndo(SdResId(STR_UNDO_INSERTPAGES));
552 if (rBookmarkList.empty())
554 if (nInsertPos >= GetPageCount())
556 // Add pages to the end
557 nInsertPos = GetPageCount();
560 sal_uInt16 nActualInsertPos = nInsertPos;
562 sal_uInt16 nBMSdPage;
563 std::set<sal_uInt16> aRenameSet;
564 std::map<sal_uInt16,OUString> aNameMap;
566 for (nBMSdPage=0; nBMSdPage < nBMSdPageCount; nBMSdPage++)
568 SdPage* pBMPage = pBookmarkDoc->GetSdPage(nBMSdPage, PageKind::Standard);
569 OUString sName(pBMPage->GetName());
570 bool bIsMasterPage;
572 if (bLink)
574 // Remember the names of all pages
575 aNameMap.insert(std::make_pair(nBMSdPage,sName));
578 // Have to check for duplicate names here, too
579 // don't change name if source and dest model are the same!
580 if( pBookmarkDoc != this &&
581 GetPageByName(sName, bIsMasterPage ) != SDRPAGE_NOTFOUND )
583 // delay renaming *after* pages are copied (might destroy source otherwise)
584 aRenameSet.insert(nBMSdPage);
588 Merge(*pBookmarkDoc,
589 1, // Not the handout page
590 0xFFFF, // But all others
591 nActualInsertPos, // Insert at position ...
592 bMergeMasterPages, // Move master pages?
593 false, // But only the master pages used
594 true, // Create an undo action
595 bCopy); // Copy (or merge) pages?
597 for (nBMSdPage=0; nBMSdPage < nBMSdPageCount; nBMSdPage++)
599 SdPage* pPage = static_cast<SdPage*>( GetPage(nActualInsertPos) );
600 SdPage* pNotesPage = static_cast<SdPage*>( GetPage(nActualInsertPos+1) );
602 // delay renaming *after* pages are copied (might destroy source otherwise)
603 if( aRenameSet.find(nBMSdPage) != aRenameSet.end() )
605 // Page name already in use -> Use default name for default and
606 // notes page
607 pPage->SetName(OUString());
608 pNotesPage->SetName(OUString());
611 if (bLink)
613 OUString aName(aNameMap[nBMSdPage]);
615 // Assemble all link names
616 pPage->SetFileName(aBookmarkName);
617 pPage->SetBookmarkName(aName);
620 nActualInsertPos += 2;
623 else
625 // Insert selected pages
626 SdPage* pBMPage;
628 if (nInsertPos >= GetPageCount())
630 // Add pages to the end
631 bReplace = false;
632 nInsertPos = GetPageCount();
635 sal_uInt16 nActualInsertPos = nInsertPos;
637 // Collect the bookmarked pages
638 ::std::vector<SdPage*> aBookmarkedPages (rBookmarkList.size(), nullptr);
639 for ( size_t nPos = 0, n = rBookmarkList.size(); nPos < n; ++nPos)
641 OUString aPgName(rBookmarkList[nPos]);
642 bool bIsMasterPage;
643 sal_uInt16 nBMPage = pBookmarkDoc->GetPageByName( aPgName, bIsMasterPage );
645 if (nBMPage != SDRPAGE_NOTFOUND)
647 aBookmarkedPages[nPos] = dynamic_cast<SdPage*>(pBookmarkDoc->GetPage(nBMPage));
651 for ( size_t nPos = 0, n = rBookmarkList.size(); nPos < n; ++nPos)
653 pBMPage = aBookmarkedPages[nPos];
654 sal_uInt16 nBMPage = pBMPage!=nullptr ? pBMPage->GetPageNum() : SDRPAGE_NOTFOUND;
656 if (pBMPage && pBMPage->GetPageKind()==PageKind::Standard && !pBMPage->IsMasterPage())
658 // It has to be a default page
659 bool bMustRename = false;
661 // delay renaming *after* pages are copied (might destroy source otherwise)
662 // don't change name if source and dest model are the same!
663 // avoid renaming if replacing the same page
664 OUString aPgName(rBookmarkList[nPos]);
665 bool bIsMasterPage;
666 sal_uInt16 nPageSameName = GetPageByName(aPgName, bIsMasterPage);
667 if( pBookmarkDoc != this &&
668 nPageSameName != SDRPAGE_NOTFOUND &&
669 ( !bReplace ||
670 nPageSameName != nActualInsertPos ) )
672 bMustRename = true;
675 SdPage* pBookmarkPage = pBMPage;
676 if (bReplace )
678 ReplacePageInCustomShows( dynamic_cast< SdPage* >( GetPage( nActualInsertPos ) ), pBookmarkPage );
681 Merge(*pBookmarkDoc,
682 nBMPage, // From page (default page)
683 nBMPage+1, // To page (notes page)
684 nActualInsertPos, // Insert at position
685 bMergeMasterPages, // Move master pages?
686 false, // But only the master pages used
687 true, // Create undo action
688 bCopy); // Copy (or merge) pages?
690 if( bReplace )
692 if( GetPage( nActualInsertPos ) != pBookmarkPage )
694 // bookmark page was not moved but cloned, so update custom shows again
695 ReplacePageInCustomShows( pBookmarkPage, dynamic_cast< SdPage* >( GetPage( nActualInsertPos ) ) );
699 if( bMustRename )
701 // Page name already in use -> use default name for default and
702 // notes page
703 SdPage* pPage = static_cast<SdPage*>( GetPage(nActualInsertPos) );
704 pPage->SetName(OUString());
705 SdPage* pNotesPage = static_cast<SdPage*>( GetPage(nActualInsertPos+1) );
706 pNotesPage->SetName(OUString());
709 if (bLink)
711 SdPage* pPage = static_cast<SdPage*>( GetPage(nActualInsertPos) );
712 pPage->SetFileName(aBookmarkName);
713 pPage->SetBookmarkName(aPgName);
716 if (bReplace)
718 // Remove page and notes page.
719 const sal_uInt16 nDestPageNum(nActualInsertPos + 2);
720 SdPage* pStandardPage = nullptr;
722 if(nDestPageNum < GetPageCount())
724 pStandardPage = static_cast<SdPage*>(GetPage(nDestPageNum));
727 if (pStandardPage)
729 if( bPreservePageNames )
731 // Take old slide names for inserted pages
732 SdPage* pPage = static_cast<SdPage*>( GetPage(nActualInsertPos) );
733 pPage->SetName( pStandardPage->GetRealName() );
736 if( bUndo )
737 AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pStandardPage));
739 RemovePage(nDestPageNum);
742 SdPage* pNotesPage = nullptr;
744 if(nDestPageNum < GetPageCount())
746 pNotesPage = static_cast<SdPage*>(GetPage(nDestPageNum));
749 if (pNotesPage)
751 if( bPreservePageNames )
753 // Take old slide names for inserted pages
754 SdPage* pNewNotesPage = static_cast<SdPage*>( GetPage(nActualInsertPos+1));
755 if( pNewNotesPage )
756 pNewNotesPage->SetName( pStandardPage->GetRealName() );
759 if( bUndo )
760 AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pNotesPage));
762 RemovePage(nDestPageNum);
765 nReplacedStandardPages++;
768 nActualInsertPos += 2;
773 // We might have duplicate master pages now, as the drawing engine does not
774 // recognize duplicates. Remove these now.
775 sal_uInt16 nNewMPageCount = GetMasterPageCount();
777 // Go backwards, so the numbers don't become messed up
778 for (sal_uInt16 nPage = nNewMPageCount - 1; nPage >= nMPageCount; nPage--)
780 pRefPage = static_cast<SdPage*>( GetMasterPage(nPage) );
781 OUString aMPLayout(pRefPage->GetLayoutName());
782 PageKind eKind = pRefPage->GetPageKind();
784 // Does this already exist?
785 for (sal_uInt16 nTest = 0; nTest < nMPageCount; nTest++)
787 SdPage* pTest = static_cast<SdPage*>( GetMasterPage(nTest) );
788 OUString aTest(pTest->GetLayoutName());
790 // nInsertPos > 2 is always true when inserting into non-empty models
791 if ( nInsertPos > 2 &&
792 aTest == aMPLayout &&
793 eKind == pTest->GetPageKind() )
795 if( bUndo )
796 AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pRefPage));
798 RemoveMasterPage(nPage);
800 nNewMPageCount--;
801 break;
806 // nInsertPos > 2 is always true when inserting into non-empty models
807 if (nInsertPos > 0)
809 sal_uInt16 nSdPageStart = (nInsertPos - 1) / 2;
810 sal_uInt16 nSdPageEnd = bReplace
811 ? nSdPageStart + nReplacedStandardPages - 1
812 : GetSdPageCount(PageKind::Standard) - nSdPageCount + nSdPageStart - 1;
813 const bool bRemoveEmptyPresObj =
814 (pBookmarkDoc->GetDocumentType() == DocumentType::Impress) &&
815 (GetDocumentType() == DocumentType::Draw);
817 std::vector<OUString>::iterator pExchangeIter;
819 if (pExchangeList)
820 pExchangeIter = pExchangeList->begin();
822 for (sal_uInt16 nSdPage = nSdPageStart; nSdPage <= nSdPageEnd; nSdPage++)
824 pRefPage = GetSdPage(nSdPage, PageKind::Standard);
826 if (pExchangeList && pExchangeIter != pExchangeList->end())
828 // Get the name to use from Exchange list
829 OUString aExchangeName(*pExchangeIter);
830 pRefPage->SetName(aExchangeName);
831 Broadcast(SdrHint(SdrHintKind::PageOrderChange, pRefPage));
833 SdPage* pNewNotesPage = GetSdPage(nSdPage, PageKind::Notes);
834 pNewNotesPage->SetName(aExchangeName);
835 Broadcast(SdrHint(SdrHintKind::PageOrderChange, pNewNotesPage));
837 ++pExchangeIter;
840 OUString aLayout(pRefPage->GetLayoutName());
841 sal_Int32 nIndex = aLayout.indexOf( SD_LT_SEPARATOR );
842 if( nIndex != -1 )
843 aLayout = aLayout.copy(0, nIndex);
845 // update layout and referred master page
846 pRefPage->SetPresentationLayout(aLayout);
847 if( bUndo )
848 AddUndo( GetSdrUndoFactory().CreateUndoPageChangeMasterPage( *pRefPage ) );
850 if (bScaleObjects)
852 ::tools::Rectangle aBorderRect(nLeft, nUpper, nRight, nLower);
853 pRefPage->ScaleObjects(aSize, aBorderRect, true);
855 pRefPage->SetSize(aSize);
856 pRefPage->SetBorder(nLeft, nUpper, nRight, nLower);
857 pRefPage->SetOrientation( eOrient );
859 if( bRemoveEmptyPresObj )
860 pRefPage->RemoveEmptyPresentationObjects();
862 pRefPage = GetSdPage(nSdPage, PageKind::Notes);
864 // update layout and referred master page
865 pRefPage->SetPresentationLayout(aLayout);
866 if( bUndo )
867 AddUndo( GetSdrUndoFactory().CreateUndoPageChangeMasterPage( *pRefPage ) );
869 if (bScaleObjects)
871 ::tools::Rectangle aBorderRect(nNLeft, nNUpper, nNRight, nNLower);
872 pRefPage->ScaleObjects(aNSize, aBorderRect, true);
875 pRefPage->SetSize(aNSize);
876 pRefPage->SetBorder(nNLeft, nNUpper, nNRight, nNLower);
877 pRefPage->SetOrientation( eNOrient );
879 if( bRemoveEmptyPresObj )
880 pRefPage->RemoveEmptyPresentationObjects();
883 ///Remove processed elements, to avoid doing hacks in InsertBookmarkAsObject
884 if ( pExchangeList )
885 pExchangeList->erase(pExchangeList->begin(),pExchangeIter);
887 for (sal_uInt16 nPage = nMPageCount; nPage < nNewMPageCount; nPage++)
889 pRefPage = static_cast<SdPage*>( GetMasterPage(nPage) );
890 if (pRefPage->GetPageKind() == PageKind::Standard)
892 if (bScaleObjects)
894 ::tools::Rectangle aBorderRect(nLeft, nUpper, nRight, nLower);
895 pRefPage->ScaleObjects(aSize, aBorderRect, true);
897 pRefPage->SetSize(aSize);
898 pRefPage->SetBorder(nLeft, nUpper, nRight, nLower);
899 pRefPage->SetOrientation( eOrient );
901 else // Can only be notes
903 if (bScaleObjects)
905 ::tools::Rectangle aBorderRect(nNLeft, nNUpper, nNRight, nNLower);
906 pRefPage->ScaleObjects(aNSize, aBorderRect, true);
908 pRefPage->SetSize(aNSize);
909 pRefPage->SetBorder(nNLeft, nNUpper, nNRight, nNLower);
910 pRefPage->SetOrientation( eNOrient );
913 if( bRemoveEmptyPresObj )
914 pRefPage->RemoveEmptyPresentationObjects();
918 // Make absolutely sure no double masterpages are there
919 RemoveUnnecessaryMasterPages(nullptr, true);
921 // Rename object styles if necessary
922 if(!aRenameStr.isEmpty())
926 for(sal_uInt32 p = nInsertPos; p < sal_uInt32(nInsertPos) + sal_uInt32(nBMSdPageCount); p++)
928 SdPage *pPg = static_cast<SdPage *>( GetPage(p) );
929 for(size_t i = 0; pPg && (i < pPg->GetObjCount()); ++i)
931 if(pPg->GetObj(i)->GetStyleSheet())
933 OUString aStyleName = pPg->GetObj(i)->GetStyleSheet()->GetName();
934 SfxStyleSheet *pSheet = lcl_findStyle(aNewGraphicStyles, Concat2View(aStyleName + aRenameStr));
935 if(pSheet != nullptr)
936 pPg->GetObj(i)->SetStyleSheet(pSheet, true);
941 catch(...)
943 TOOLS_WARN_EXCEPTION( "sd", "Exception while renaming styles @ SdDrawDocument::InsertBookmarkAsPage");
946 // remove copied styles not used on any inserted page and create
947 // undo records
948 // WARNING: SdMoveStyleSheetsUndoAction clears the passed list of
949 // styles, so it cannot be used after this point
950 lcl_removeUnusedStyles(GetStyleSheetPool(), aNewGraphicStyles);
951 if (!aNewGraphicStyles.empty() && pUndoMgr)
952 pUndoMgr->AddUndoAction(std::make_unique<SdMoveStyleSheetsUndoAction>(this, aNewGraphicStyles, true));
953 lcl_removeUnusedTableStyles(static_cast<SdStyleSheetPool*>(GetStyleSheetPool()), aNewTableStyles);
954 lcl_removeUnusedStyles(GetStyleSheetPool(), aNewCellStyles);
956 if( bUndo )
957 EndUndo();
959 if (pUndoMgr)
960 pUndoMgr->LeaveListAction();
962 return bContinue;
965 // Inserts a bookmark as an object
966 bool SdDrawDocument::InsertBookmarkAsObject(
967 const std::vector<OUString> &rBookmarkList,
968 const std::vector<OUString> &rExchangeList, // List of names to use
969 ::sd::DrawDocShell* pBookmarkDocSh,
970 Point const * pObjPos,
971 bool bCalcObjCount)
973 bool bOK = true;
974 bool bOLEObjFound = false;
975 std::unique_ptr<::sd::View> pBMView;
977 SdDrawDocument* pBookmarkDoc = nullptr;
979 if (pBookmarkDocSh)
981 pBookmarkDoc = pBookmarkDocSh->GetDoc();
983 else if ( mxBookmarkDocShRef.is() )
985 pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
987 else
989 return false;
992 if (rBookmarkList.empty())
994 pBMView.reset(new ::sd::View(*pBookmarkDoc, nullptr));
995 pBMView->EndListening(*pBookmarkDoc);
996 pBMView->MarkAll();
998 else
1000 SdrPage* pPage;
1001 SdrPageView* pPV;
1003 for ( const auto& rBookmark : rBookmarkList )
1005 // Get names of bookmarks from the list
1006 SdrObject* pObj = pBookmarkDoc->GetObj(rBookmark);
1008 if (pObj)
1010 // Found an object
1011 if (pObj->GetObjInventor() == SdrInventor::Default &&
1012 pObj->GetObjIdentifier() == SdrObjKind::OLE2)
1014 bOLEObjFound = true;
1017 if (!pBMView)
1019 // Create View for the first time
1020 pBMView.reset(new ::sd::View(*pBookmarkDoc, nullptr));
1021 pBMView->EndListening(*pBookmarkDoc);
1024 pPage = pObj->getSdrPageFromSdrObject();
1026 if (pPage->IsMasterPage())
1028 pPV = pBMView->ShowSdrPage(pBMView->GetModel().GetMasterPage(pPage->GetPageNum()));
1030 else
1032 pPV = pBMView->GetSdrPageView();
1033 if( !pPV || (pPV->GetPage() != pPage))
1034 pPV = pBMView->ShowSdrPage(pPage);
1037 pBMView->MarkObj(pObj, pPV);
1042 if (pBMView)
1044 // Insert selected objects
1045 std::optional<::sd::View> pView(std::in_place, *this, nullptr);
1046 pView->EndListening(*this);
1048 // Look for the page into which the objects are supposed to be inserted
1049 SdrPage* pPage = GetSdPage(0, PageKind::Standard);
1051 if (mpDocSh)
1053 ::sd::ViewShell* pViewSh = mpDocSh->GetViewShell();
1055 if (pViewSh)
1057 // Which page is currently in view?
1058 SdrPageView* pPV = pViewSh->GetView()->GetSdrPageView();
1060 if (pPV)
1062 pPage = pPV->GetPage();
1064 else if (pViewSh->GetActualPage())
1066 pPage = pViewSh->GetActualPage();
1071 Point aObjPos;
1073 if (pObjPos)
1075 aObjPos = *pObjPos;
1077 else
1079 aObjPos = ::tools::Rectangle(Point(), pPage->GetSize()).Center();
1082 size_t nCountBefore = 0;
1084 if (!rExchangeList.empty() || bCalcObjCount)
1086 // Sort OrdNums and get the number of objects before inserting
1087 pPage->RecalcObjOrdNums();
1088 nCountBefore = pPage->GetObjCount();
1091 if (bOLEObjFound)
1092 pBMView->GetDoc().SetAllocDocSh(true);
1094 SdDrawDocument* pTmpDoc = static_cast<SdDrawDocument*>( pBMView->CreateMarkedObjModel().release() );
1095 bOK = pView->Paste(*pTmpDoc, aObjPos, pPage, SdrInsertFlags::NONE);
1097 if (bOLEObjFound)
1098 pBMView->GetDoc().SetAllocDocSh(false);
1100 if (!bOLEObjFound)
1101 delete pTmpDoc; // Would otherwise be destroyed by DocShell
1103 pView.reset();
1105 // Get number of objects after inserting.
1106 const size_t nCount = pPage->GetObjCount();
1107 if (nCountBefore < nCount)
1109 size_t nObj = nCountBefore;
1110 for (const auto& rExchange : rExchangeList)
1112 // Get the name to use from the Exchange list
1113 if (pPage->GetObj(nObj))
1115 pPage->GetObj(nObj)->SetName(rExchange);
1118 ++nObj;
1119 if (nObj >= nCount)
1120 break;
1125 return bOK;
1128 // Stops the bookmark insertion
1129 void SdDrawDocument::CloseBookmarkDoc()
1131 if (mxBookmarkDocShRef.is())
1133 mxBookmarkDocShRef->DoClose();
1136 mxBookmarkDocShRef.clear();
1137 maBookmarkFile.clear();
1140 // Is this document read-only?
1141 bool SdDrawDocument::IsReadOnly() const
1143 return false;
1146 // In the subsequent AllocModel() a DocShell (xAllocedDocShRef) is created.
1147 // Any pre-existing DocShell is deleted
1148 void SdDrawDocument::SetAllocDocSh(bool bAlloc)
1150 mbAllocDocSh = bAlloc;
1152 if(mxAllocedDocShRef.is())
1154 mxAllocedDocShRef->DoClose();
1157 mxAllocedDocShRef.clear();
1160 // Return list of CustomShows (create it, too, if necessary)
1161 SdCustomShowList* SdDrawDocument::GetCustomShowList(bool bCreate)
1163 if (!mpCustomShowList && bCreate)
1165 mpCustomShowList.reset(new SdCustomShowList);
1168 return mpCustomShowList.get();
1171 // Remove unused master pages and layouts
1172 void SdDrawDocument::RemoveUnnecessaryMasterPages(SdPage* pMasterPage, bool bOnlyDuplicatePages, bool bUndo)
1174 ::sd::View* pView = nullptr;
1175 SfxUndoManager* pUndoMgr = nullptr;
1177 if( bUndo && !IsUndoEnabled() )
1178 bUndo = false;
1180 if (mpDocSh)
1182 pUndoMgr = mpDocSh->GetUndoManager();
1184 if (mpDocSh->GetViewShell())
1185 pView = mpDocSh->GetViewShell()->GetView();
1188 // Check all master pages
1189 sal_uInt16 nSdMasterPageCount = GetMasterSdPageCount( PageKind::Standard );
1190 for (sal_Int32 nMPage = nSdMasterPageCount - 1; nMPage >= 0; nMPage--)
1192 SdPage* pMaster = pMasterPage;
1193 SdPage* pNotesMaster = nullptr;
1195 if (!pMaster)
1197 pMaster = GetMasterSdPage( static_cast<sal_uInt16>(nMPage), PageKind::Standard );
1198 pNotesMaster = GetMasterSdPage( static_cast<sal_uInt16>(nMPage), PageKind::Notes );
1200 else
1202 for ( sal_uInt16 nMPg = 0; nMPg < GetMasterPageCount(); nMPg++ )
1204 if ( pMaster == GetMasterPage( nMPg ) )
1206 pNotesMaster = static_cast<SdPage*>( GetMasterPage( ++nMPg ) );
1207 break;
1212 DBG_ASSERT( pMaster->GetPageKind() == PageKind::Standard, "wrong page kind" );
1214 if ( pMaster->GetPageKind() == PageKind::Standard &&
1215 GetMasterPageUserCount( pMaster ) == 0 &&
1216 pNotesMaster )
1218 // Do not delete master pages that have their precious flag set
1219 bool bDeleteMaster = !pMaster->IsPrecious();
1220 OUString aLayoutName = pMaster->GetLayoutName();
1222 if(bOnlyDuplicatePages )
1224 // remove only duplicate pages
1225 bDeleteMaster = false;
1226 for (sal_uInt16 i = 0; i < GetMasterSdPageCount( PageKind::Standard ); i++)
1228 SdPage* pMPg = GetMasterSdPage( i, PageKind::Standard );
1229 if( pMPg != pMaster &&
1230 pMPg->GetLayoutName() == aLayoutName )
1232 // duplicate page found -> remove it
1233 bDeleteMaster = true;
1238 if( bDeleteMaster )
1240 if (pView)
1242 // if MasterPage is visible hide on pageview
1243 SdrPageView* pPgView = pView->GetSdrPageView();
1244 if (pPgView)
1246 SdrPage* pShownPage = pPgView->GetPage();
1247 if( (pShownPage == pMaster) || (pShownPage == pNotesMaster) )
1249 pView->HideSdrPage();
1250 pView->ShowSdrPage( GetSdPage( 0, PageKind::Standard ) );
1255 if( bUndo )
1257 BegUndo();
1258 AddUndo( GetSdrUndoFactory().CreateUndoDeletePage( *pNotesMaster ) );
1261 RemoveMasterPage( pNotesMaster->GetPageNum() );
1263 if( bUndo )
1264 AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pMaster));
1266 RemoveMasterPage( pMaster->GetPageNum() );
1268 if( bUndo )
1269 EndUndo(); // do this here already, so Joe's actions happen _between_ our own
1271 // Delete old, unused layout stylesheets
1272 bool bDeleteOldStyleSheets = true;
1273 for ( sal_uInt16 nMPg = 0;
1274 nMPg < GetMasterPageCount() && bDeleteOldStyleSheets;
1275 nMPg++ )
1277 SdPage* pMPg = static_cast<SdPage*>( GetMasterPage(nMPg) );
1278 if (pMPg->GetLayoutName() == aLayoutName)
1280 bDeleteOldStyleSheets = false;
1284 if (bDeleteOldStyleSheets)
1286 SdStyleSheetVector aRemove;
1287 static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->CreateLayoutSheetList( aLayoutName, aRemove );
1289 if( bUndo )
1291 StyleSheetCopyResultVector aUndoRemove;
1292 aUndoRemove.reserve(aRemove.size());
1293 for (const auto& a : aRemove)
1294 aUndoRemove.emplace_back(a.get(), true);
1296 if (pUndoMgr)
1297 pUndoMgr->AddUndoAction(std::make_unique<SdMoveStyleSheetsUndoAction>(this, aUndoRemove, false));
1300 for( const auto& a : aRemove )
1301 static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->Remove(a.get());
1306 if (pMasterPage)
1307 break; // Just this one master page!
1311 /** Exchange master page
1313 * Either the nSdPageNum gets a new, own master page or the master page is
1314 * exchanged completely (which then applies to all pages).
1316 * nSdPageNum : page number that the new master page should get.
1317 * rLayoutName : LayoutName of the new master page
1318 * pSourceDoc : document (template) to get the master page from
1319 * bMaster : exchange the master page of nSdPageNum
1320 * bCheckMasters: remove unused master pages
1322 * If pSourceDoc == NULL, an empty master page is applied.
1323 * If rLayoutName is empty, the first master page is used.
1325 // #i121863# factored out functionality
1326 static bool isMasterPageLayoutNameUnique(const SdDrawDocument& rDoc, std::u16string_view rCandidate)
1328 if (rCandidate.empty())
1330 return false;
1333 const sal_uInt16 nPageCount(rDoc.GetMasterPageCount());
1335 for(sal_uInt16 a(0); a < nPageCount; a++)
1337 const SdrPage* pCandidate = rDoc.GetMasterPage(a);
1338 OUString aPageLayoutName(pCandidate->GetLayoutName());
1339 sal_Int32 nIndex = aPageLayoutName.indexOf(SD_LT_SEPARATOR);
1340 if( nIndex != -1 )
1341 aPageLayoutName = aPageLayoutName.copy(0, nIndex);
1343 if(aPageLayoutName == rCandidate)
1345 return false;
1349 return true;
1352 // #i121863# factored out functionality
1353 static OUString createNewMasterPageLayoutName(const SdDrawDocument& rDoc)
1355 const OUString aBaseName(SdResId(STR_LAYOUT_DEFAULT_NAME));
1356 sal_uInt16 nCount(0);
1357 for (;;)
1359 OUString aRetval = aBaseName;
1360 if(nCount)
1362 aRetval += OUString::number(nCount);
1364 if (isMasterPageLayoutNameUnique(rDoc, aRetval))
1365 return aRetval;
1366 nCount++;
1370 void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
1371 std::u16string_view rLayoutName,
1372 SdDrawDocument* pSourceDoc,
1373 bool bMaster,
1374 bool bCheckMasters)
1376 SfxUndoManager* pUndoMgr = nullptr;
1378 if( mpDocSh )
1380 mpDocSh->SetWaitCursor( true );
1381 pUndoMgr = mpDocSh->GetUndoManager();
1384 const bool bUndo = pUndoMgr && IsUndoEnabled();
1386 if (bUndo)
1388 ViewShellId nViewShellId(-1);
1389 if (sd::ViewShell* pViewShell = mpDocSh->GetViewShell())
1390 nViewShellId = pViewShell->GetViewShellBase().GetViewShellId();
1391 pUndoMgr->EnterListAction(SdResId(STR_UNDO_SET_PRESLAYOUT), OUString(), 0, nViewShellId);
1394 SdPage* pSelectedPage = GetSdPage(nSdPageNum, PageKind::Standard);
1395 SdPage* pNotes = static_cast<SdPage*>( GetPage(pSelectedPage->GetPageNum()+1) );
1396 SdPage& rOldMaster = static_cast<SdPage&>(pSelectedPage->TRG_GetMasterPage());
1397 SdPage& rOldNotesMaster = static_cast<SdPage&>(pNotes->TRG_GetMasterPage());
1398 rtl::Reference<SdPage> pMaster;
1399 rtl::Reference<SdPage> pNotesMaster;
1400 OUString aOldPageLayoutName(pSelectedPage->GetLayoutName());
1401 OUString aOldLayoutName(aOldPageLayoutName);
1402 sal_Int32 nIndex = aOldLayoutName.indexOf( SD_LT_SEPARATOR );
1403 if( nIndex != -1 )
1404 aOldLayoutName = aOldLayoutName.copy(0, nIndex);
1406 if (pSourceDoc)
1408 std::vector<StyleReplaceData> aReplList; // List of replaced stylesheets
1409 bool bLayoutReloaded = false; // Was ex. layout reloaded?
1411 // LayoutName, Page and Notes page
1412 if (rLayoutName.empty())
1414 // No LayoutName: take first MasterPage
1415 pMaster = pSourceDoc->GetMasterSdPage(0, PageKind::Standard);
1416 pNotesMaster = pSourceDoc->GetMasterSdPage(0, PageKind::Notes);
1418 else
1420 OUString aSearchFor
1421 = OUString::Concat(rLayoutName) + SD_LT_SEPARATOR + STR_LAYOUT_OUTLINE;
1423 for (sal_uInt16 nMP = 0; nMP < pSourceDoc->GetMasterPageCount(); ++nMP)
1425 SdPage* pMP = static_cast<SdPage*>( pSourceDoc->GetMasterPage(nMP) );
1427 if (pMP->GetLayoutName() == aSearchFor)
1429 if (pMP->GetPageKind() == PageKind::Standard)
1430 pMaster = pMP;
1431 if (pMP->GetPageKind() == PageKind::Notes)
1432 pNotesMaster = pMP;
1434 if (pMaster && pNotesMaster)
1435 break;
1437 DBG_ASSERT(pMaster, "MasterPage (Standard page) not found");
1438 DBG_ASSERT(pNotesMaster, "MasterPage (Notes page) not found");
1440 // this should not happen, but looking at crash reports, it does
1441 if( (pMaster == nullptr) || (pNotesMaster == nullptr) )
1443 // so take the first MasterPage
1444 pMaster = pSourceDoc->GetMasterSdPage(0, PageKind::Standard);
1445 pNotesMaster = pSourceDoc->GetMasterSdPage(0, PageKind::Notes);
1449 // we should never reach this, but one never knows...
1450 if( (pMaster == nullptr) || (pNotesMaster == nullptr) )
1452 if (bUndo)
1453 pUndoMgr->LeaveListAction();
1455 if( mpDocSh )
1456 mpDocSh->SetWaitCursor( false );
1458 OSL_FAIL( "SdDrawDocument::SetMasterPage() failed!" );
1460 return;
1463 const OUString aOriginalNewLayoutName( pMaster->GetName() );
1464 OUString aTargetNewLayoutName(aOriginalNewLayoutName);
1466 if (pSourceDoc != this)
1468 // #i121863# clone masterpages, they are from another model (!)
1469 rtl::Reference<SdPage> pNewNotesMaster(dynamic_cast< SdPage* >(pNotesMaster->CloneSdrPage(*this).get()));
1470 rtl::Reference<SdPage> pNewMaster(dynamic_cast< SdPage* >(pMaster->CloneSdrPage(*this).get()));
1472 if(!pNewNotesMaster || !pNewMaster)
1474 OSL_FAIL("SdDrawDocument::SetMasterPage() cloning of MasterPage/NoteAmsterPage failed!" );
1475 return;
1478 pNotesMaster = pNewNotesMaster;
1479 pMaster = pNewMaster;
1481 // layout name needs to be unique
1482 aTargetNewLayoutName = pMaster->GetLayoutName();
1483 sal_Int32 nIndex2 = aTargetNewLayoutName.indexOf(SD_LT_SEPARATOR);
1484 if( nIndex2 != -1 )
1485 aTargetNewLayoutName = aTargetNewLayoutName.copy(0, nIndex2);
1487 if(!isMasterPageLayoutNameUnique(*this, aTargetNewLayoutName))
1489 aTargetNewLayoutName = createNewMasterPageLayoutName(*this);
1491 OUString aTemp = aTargetNewLayoutName + SD_LT_SEPARATOR + STR_LAYOUT_OUTLINE;
1493 pMaster->SetName(aTargetNewLayoutName);
1494 pMaster->SetLayoutName(aTemp);
1496 pNotesMaster->SetName(aTargetNewLayoutName);
1497 pNotesMaster->SetLayoutName(aTemp);
1501 if (pSourceDoc != this)
1503 const sal_uInt16 nMasterPageCount = GetMasterPageCount();
1504 for ( sal_uInt16 nMPage = 0; nMPage < nMasterPageCount; nMPage++ )
1506 SdPage* pCheckMaster = static_cast<SdPage*>(GetMasterPage(nMPage));
1507 if( pCheckMaster->GetName() == aTargetNewLayoutName )
1509 bLayoutReloaded = true;
1510 break;
1514 // Correct or create presentation templates --
1515 // only worry about presentation templates
1516 OUString aName;
1517 SdStyleSheetPool* pSourceStyleSheetPool = static_cast<SdStyleSheetPool*>( pSourceDoc->GetStyleSheetPool() );
1519 StyleSheetCopyResultVector aCreatedStyles; // List of created stylesheets
1520 SfxStyleSheetBase* pHisSheet = pSourceStyleSheetPool->First(SfxStyleFamily::Page);
1522 while (pHisSheet)
1524 aName = pHisSheet->GetName();
1526 // #i121863# search in source styles with original style name from source of
1527 // evtl. cloned master (not-cloned, renamed for uniqueness)
1528 if( aName.startsWith( aOriginalNewLayoutName ) )
1530 // #i121863# build name of evtl. cloned master style to search for
1531 if(aOriginalNewLayoutName != aTargetNewLayoutName)
1533 const sal_Int32 nPos(aName.indexOf(SD_LT_SEPARATOR));
1534 aName = aTargetNewLayoutName + aName.subView(nPos);
1537 SfxStyleSheet* pMySheet = static_cast<SfxStyleSheet*>( mxStyleSheetPool->Find(aName, SfxStyleFamily::Page) );
1539 if (pMySheet)
1541 // A stylesheet of the same name already exists -> overwrite contents
1542 bool bTest = pMySheet->SetName(pHisSheet->GetName());
1543 DBG_ASSERT(bTest, "Renaming StyleSheet failed.");
1544 pMySheet->GetItemSet().ClearItem(); // Delete all
1546 if (bUndo)
1548 pUndoMgr->AddUndoAction(std::make_unique<StyleSheetUndoAction>(this,
1549 pMySheet, &pHisSheet->GetItemSet()));
1551 pMySheet->GetItemSet().Put(pHisSheet->GetItemSet());
1552 pMySheet->Broadcast(SfxHint(SfxHintId::DataChanged));
1554 else
1556 // create new style
1557 OUString aHelpFile;
1558 pMySheet = static_cast<SfxStyleSheet*>( &mxStyleSheetPool->Make(aName, SfxStyleFamily::Page, pHisSheet->GetMask()) );
1559 pMySheet->SetHelpId( aHelpFile, pHisSheet->GetHelpId(aHelpFile) );
1560 pMySheet->GetItemSet().ClearItem(); // Delete all
1561 pMySheet->GetItemSet().Put(pHisSheet->GetItemSet());
1563 aCreatedStyles.emplace_back(static_cast<SdStyleSheet*>(pMySheet), true);
1566 StyleReplaceData aReplData;
1567 aReplData.nNewFamily = pMySheet->GetFamily();
1568 aReplData.nFamily = pMySheet->GetFamily();
1569 aReplData.aNewName = pMySheet->GetName();
1571 // #i121863# re-create original name of style used at page where to replace with
1572 // this new style
1573 OUString aTemp(pMySheet->GetName());
1574 const sal_Int32 nPos(aTemp.indexOf(SD_LT_SEPARATOR));
1575 aTemp = aOldLayoutName + aTemp.subView(nPos);
1576 aReplData.aName = aTemp;
1577 aReplList.push_back(aReplData);
1580 pHisSheet = pSourceStyleSheetPool->Next();
1583 // If new styles were created: re-create parent chaining of the item
1584 // sets in the styles.
1585 if(!aCreatedStyles.empty())
1587 for ( const auto& rRData : aReplList )
1589 SfxStyleSheetBase* pSOld = mxStyleSheetPool->Find(rRData.aName, SfxStyleFamily::Page);
1590 SfxStyleSheetBase* pSNew = mxStyleSheetPool->Find(rRData.aNewName, SfxStyleFamily::Page);
1592 if (pSOld && pSNew)
1594 const OUString& rParentOfOld = pSOld->GetParent();
1595 const OUString& rParentOfNew = pSNew->GetParent();
1597 if (!rParentOfOld.isEmpty() && rParentOfNew.isEmpty())
1599 std::vector<StyleReplaceData>::iterator pRDIter = std::find_if(aReplList.begin(), aReplList.end(),
1600 [&rParentOfOld](const StyleReplaceData& rRD) { return (rRD.aName == rParentOfOld) && (rRD.aName != rRD.aNewName); });
1601 if (pRDIter != aReplList.end())
1603 OUString aParentOfNew(pRDIter->aNewName);
1604 pSNew->SetParent(aParentOfNew);
1611 if (bUndo && !aCreatedStyles.empty())
1613 // Add UndoAction for creating and inserting the stylesheets to
1614 // the top of the UndoManager
1615 pUndoMgr->AddUndoAction(std::make_unique<SdMoveStyleSheetsUndoAction>( this, aCreatedStyles, true));
1619 // Create layout name based upon the name of the page layout of the
1620 // master page
1621 OUString aPageLayoutName(pMaster->GetLayoutName());
1622 OUString aLayoutName = aPageLayoutName;
1623 sal_Int32 nIndex2 = aLayoutName.indexOf( SD_LT_SEPARATOR );
1624 if( nIndex2 != -1 )
1625 aLayoutName = aLayoutName.copy( 0, nIndex2);
1627 // #i121863# Do *not* remove from original document any longer, it is potentially used there
1628 // and would lead to crashes. Rely on the automatic process of removing unused masterpages
1629 // (see RemoveUnnecessaryMasterPages)
1630 //if (pSourceDoc != this)
1632 // // Remove from the source document
1633 // pSourceDoc->RemoveMasterPage(pNotesMaster->GetPageNum());
1634 // pSourceDoc->RemoveMasterPage(pMaster->GetPageNum());
1637 // Register the new master pages with the document and then use
1638 // the new presentation layout for the default and notes pages
1639 if (pSourceDoc != this)
1641 // Insert the master pages:
1642 // Insert master pages from new layouts at the end.
1643 // If a layout is being replaced, however, insert them before the
1644 // position of the old master page, so from now on the new master
1645 // page will be found when searching (e.g.
1646 // SdPage::SetPresentationLayout).
1647 sal_uInt16 nInsertPos = rOldMaster.GetPageNum();
1648 BegUndo();
1650 if (!bLayoutReloaded)
1651 nInsertPos = 0xFFFF;
1652 InsertMasterPage(pMaster.get(), nInsertPos);
1653 if( bUndo )
1654 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pMaster));
1656 nInsertPos++;
1657 if (!bLayoutReloaded)
1658 nInsertPos = 0xFFFF;
1659 InsertMasterPage(pNotesMaster.get(), nInsertPos);
1660 if( bUndo )
1662 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pNotesMaster));
1664 EndUndo(); // do this here already, so Joe's actions happen _between_ our own.
1668 // Fill list with pages
1669 std::vector<rtl::Reference<SdPage>> aPageList;
1671 // #98456, this has to be removed according to CL (KA 07/08/2002)
1672 // #109884# but we need them again to restore the styles of the presentation objects while undo
1673 aPageList.push_back(pMaster);
1674 aPageList.push_back(pNotesMaster);
1676 if (bMaster || bLayoutReloaded)
1678 for (sal_uInt16 nPage = 1; nPage < GetPageCount(); nPage++)
1680 SdPage* pPage = static_cast<SdPage*>( GetPage(nPage) );
1681 OUString aTest = pPage->GetLayoutName();
1682 if (aTest == aOldPageLayoutName)
1684 aPageList.push_back(pPage);
1689 else
1691 aPageList.push_back(pSelectedPage);
1692 aPageList.push_back(pNotes);
1695 for (rtl::Reference<SdPage>& pPage : aPageList)
1697 AutoLayout eAutoLayout = pPage->GetAutoLayout();
1699 if( bUndo )
1701 pUndoMgr->AddUndoAction(std::make_unique<SdPresentationLayoutUndoAction>
1702 (this,
1703 pPage->IsMasterPage() ? aLayoutName : aOldLayoutName,
1704 aLayoutName,
1705 eAutoLayout, eAutoLayout, false, pPage.get()));
1707 pPage->SetPresentationLayout(aLayoutName);
1708 pPage->SetAutoLayout(eAutoLayout);
1711 // Adapt new master pages
1712 if (pSourceDoc != this)
1714 Size aSize(rOldMaster.GetSize());
1715 ::tools::Rectangle aBorderRect(rOldMaster.GetLeftBorder(),
1716 rOldMaster.GetUpperBorder(),
1717 rOldMaster.GetRightBorder(),
1718 rOldMaster.GetLowerBorder());
1719 pMaster->ScaleObjects(aSize, aBorderRect, true);
1720 pMaster->SetSize(aSize);
1721 pMaster->SetBorder(rOldMaster.GetLeftBorder(),
1722 rOldMaster.GetUpperBorder(),
1723 rOldMaster.GetRightBorder(),
1724 rOldMaster.GetLowerBorder());
1725 pMaster->SetOrientation( rOldMaster.GetOrientation() );
1726 pMaster->SetAutoLayout(pMaster->GetAutoLayout());
1728 aSize = rOldNotesMaster.GetSize();
1729 ::tools::Rectangle aNotesBorderRect(rOldNotesMaster.GetLeftBorder(),
1730 rOldNotesMaster.GetUpperBorder(),
1731 rOldNotesMaster.GetRightBorder(),
1732 rOldNotesMaster.GetLowerBorder());
1733 pNotesMaster->ScaleObjects(aSize, aNotesBorderRect, true);
1734 pNotesMaster->SetSize(aSize);
1735 pNotesMaster->SetBorder(rOldNotesMaster.GetLeftBorder(),
1736 rOldNotesMaster.GetUpperBorder(),
1737 rOldNotesMaster.GetRightBorder(),
1738 rOldNotesMaster.GetLowerBorder());
1739 pNotesMaster->SetOrientation( rOldNotesMaster.GetOrientation() );
1740 pNotesMaster->SetAutoLayout(pNotesMaster->GetAutoLayout());
1742 if( (pSourceDoc->GetDocumentType() == DocumentType::Impress) &&
1743 (GetDocumentType() == DocumentType::Draw) )
1745 pMaster->RemoveEmptyPresentationObjects();
1746 pNotesMaster->RemoveEmptyPresentationObjects();
1750 else
1752 // Find a new name for the layout
1753 OUString aName(createNewMasterPageLayoutName(*this));
1754 OUString aPageLayoutName(aName + SD_LT_SEPARATOR + STR_LAYOUT_OUTLINE);
1756 // Generate new stylesheets
1757 static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->CreateLayoutStyleSheets(aName);
1758 SdStyleSheetVector aCreatedStyles;
1759 static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->CreateLayoutSheetList(aName, aCreatedStyles);
1761 if( bUndo )
1763 StyleSheetCopyResultVector aUndoInsert;
1764 aUndoInsert.reserve(aCreatedStyles.size());
1765 for (const auto& a : aCreatedStyles)
1766 aUndoInsert.emplace_back(a.get(), true);
1767 pUndoMgr->AddUndoAction(std::make_unique<SdMoveStyleSheetsUndoAction>(this, aUndoInsert, true));
1768 // Generate new master pages and register them with the document
1769 BegUndo();
1772 pMaster = AllocSdPage(true);
1773 pMaster->SetSize(pSelectedPage->GetSize());
1774 pMaster->SetBorder(pSelectedPage->GetLeftBorder(),
1775 pSelectedPage->GetUpperBorder(),
1776 pSelectedPage->GetRightBorder(),
1777 pSelectedPage->GetLowerBorder() );
1778 pMaster->SetName(aName);
1779 pMaster->SetLayoutName(aPageLayoutName);
1780 InsertMasterPage(pMaster.get());
1782 if( bUndo )
1783 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pMaster));
1785 pMaster->SetAutoLayout(AUTOLAYOUT_NONE, true, true);
1787 pNotesMaster = AllocSdPage(true);
1788 pNotesMaster->SetPageKind(PageKind::Notes);
1789 pNotesMaster->SetSize(pNotes->GetSize());
1790 pNotesMaster->SetBorder(pNotes->GetLeftBorder(),
1791 pNotes->GetUpperBorder(),
1792 pNotes->GetRightBorder(),
1793 pNotes->GetLowerBorder() );
1794 pNotesMaster->SetName(aName);
1795 pNotesMaster->SetLayoutName(aPageLayoutName);
1796 InsertMasterPage(pNotesMaster.get());
1798 if( bUndo )
1799 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pNotesMaster));
1801 pNotesMaster->SetAutoLayout(AUTOLAYOUT_NOTES, true, true);
1803 if( bUndo )
1804 EndUndo();
1806 // Create a list of affected default and notes pages
1807 std::vector<SdPage*> aPageList;
1808 if (bMaster)
1810 for (sal_uInt16 nPage = 1; nPage < GetPageCount(); nPage++)
1812 SdPage* pPage = static_cast<SdPage*>( GetPage(nPage) );
1813 if (pPage->GetLayoutName() == aOldPageLayoutName)
1815 aPageList.push_back(pPage);
1819 else
1821 aPageList.push_back(pSelectedPage);
1822 aPageList.push_back(pNotes);
1825 // Set presentation layout and AutoLayout for the affected pages
1826 for ( auto& rpPage : aPageList )
1828 AutoLayout eOldAutoLayout = rpPage->GetAutoLayout();
1829 AutoLayout eNewAutoLayout =
1830 rpPage->GetPageKind() == PageKind::Standard ? AUTOLAYOUT_NONE : AUTOLAYOUT_NOTES;
1832 if( bUndo )
1834 pUndoMgr->AddUndoAction(std::make_unique<SdPresentationLayoutUndoAction>
1835 (this, aOldLayoutName, aName,
1836 eOldAutoLayout, eNewAutoLayout, true,
1837 rpPage));
1840 rpPage->SetPresentationLayout(aName);
1841 rpPage->SetAutoLayout(eNewAutoLayout);
1845 // If the old master pages aren't used anymore, they and their styles have
1846 // to be removed.
1847 if (bCheckMasters)
1849 // Check all
1850 RemoveUnnecessaryMasterPages();
1852 else
1854 // Check only the master page that was replaced
1855 RemoveUnnecessaryMasterPages(&rOldMaster);
1858 if( bUndo )
1859 pUndoMgr->LeaveListAction();
1861 if( mpDocSh )
1862 mpDocSh->SetWaitCursor( false );
1865 void SdDrawDocument::Merge(SdrModel& rSourceModel,
1866 sal_uInt16 nFirstPageNum, sal_uInt16 nLastPageNum,
1867 sal_uInt16 nDestPos,
1868 bool bMergeMasterPages, bool bAllMasterPages,
1869 bool bUndo, bool bTreadSourceAsConst)
1871 sal_uInt16 nMasterPageCount = GetMasterPageCount();
1872 SdrModel::Merge( rSourceModel, nFirstPageNum, nLastPageNum, nDestPos, bMergeMasterPages, bAllMasterPages, bUndo, bTreadSourceAsConst );
1874 // add style family for each new master page
1875 for( sal_uInt16 nMaster = nMasterPageCount; nMaster < GetMasterPageCount(); nMaster++ )
1877 SdPage* pPage = static_cast< SdPage* >( GetMasterPage( nMaster ) );
1878 if( pPage && pPage->IsMasterPage() && (pPage->GetPageKind() == PageKind::Standard) )
1880 // new master page created, add its style family
1881 SdStyleSheetPool* pStylePool = static_cast<SdStyleSheetPool*>( GetStyleSheetPool() );
1882 if( pStylePool )
1883 pStylePool->AddStyleFamily( pPage );
1888 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */