LCOV - code coverage report
Current view: top level - sd/source/core - drawdoc3.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 883 0.0 %
Date: 2014-04-14 Functions: 0 21 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <com/sun/star/embed/ElementModes.hpp>
      21             : #include <com/sun/star/beans/XPropertySet.hpp>
      22             : 
      23             : #include "comphelper/anytostring.hxx"
      24             : #include "cppuhelper/exc_hlp.hxx"
      25             : 
      26             : #include <vcl/wrkwin.hxx>
      27             : #include <sfx2/docfile.hxx>
      28             : #include <sot/storage.hxx>
      29             : #include <sfx2/app.hxx>
      30             : #include <svl/itemset.hxx>
      31             : 
      32             : #include <unotools/ucbstreamhelper.hxx>
      33             : #include <sfx2/fcontnr.hxx>
      34             : #include <svx/svdopath.hxx>
      35             : #include <svx/svditer.hxx>
      36             : #include <svl/style.hxx>
      37             : #include <sfx2/linkmgr.hxx>
      38             : #include <svx/svdpagv.hxx>
      39             : #include <svx/svdogrp.hxx>
      40             : #include <svx/svdundo.hxx>
      41             : #include <vcl/msgbox.hxx>
      42             : #include <sot/formats.hxx>
      43             : 
      44             : #include "glob.hrc"
      45             : #include "drawdoc.hxx"
      46             : #include "sdpage.hxx"
      47             : #include "stlpool.hxx"
      48             : #include "sdresid.hxx"
      49             : #include "sdiocmpt.hxx"
      50             : #include "strmname.h"
      51             : #include "anminfo.hxx"
      52             : #include "customshowlist.hxx"
      53             : 
      54             : #include "../ui/inc/unmovss.hxx"
      55             : #include "../ui/inc/unchss.hxx"
      56             : #include "../ui/inc/unprlout.hxx"
      57             : #include "../ui/inc/DrawDocShell.hxx"
      58             : #include "../ui/inc/GraphicDocShell.hxx"
      59             : #include "../ui/inc/ViewShell.hxx"
      60             : #include "../ui/inc/View.hxx"
      61             : #include "../ui/inc/cfgids.hxx"
      62             : #include "../ui/inc/strings.hrc"
      63             : 
      64             : using namespace ::com::sun::star;
      65             : 
      66             : /** Concrete incarnations get called by lcl_IterateBookmarkPages, for
      67             :     every page in the bookmark document/list
      68             :  */
      69             : 
      70             : class InsertBookmarkAsPage_FindDuplicateLayouts
      71             : {
      72             : public:
      73           0 :     InsertBookmarkAsPage_FindDuplicateLayouts( std::vector<OUString> &rLayoutsToTransfer )
      74           0 :         : mrLayoutsToTransfer(rLayoutsToTransfer) {}
      75             :     void operator()( SdDrawDocument&, SdPage*, bool, SdDrawDocument* );
      76             : private:
      77             :     std::vector<OUString> &mrLayoutsToTransfer;
      78             : };
      79             : 
      80           0 : void InsertBookmarkAsPage_FindDuplicateLayouts::operator()( SdDrawDocument& rDoc, SdPage* pBMMPage, bool bRenameDuplicates, SdDrawDocument* pBookmarkDoc )
      81             : {
      82             :     // now check for duplicate masterpage and layout names
      83             : 
      84           0 :     OUString aFullNameLayout( pBMMPage->GetLayoutName() );
      85           0 :     sal_Int32 nIndex = aFullNameLayout.indexOf( SD_LT_SEPARATOR );
      86           0 :     if( nIndex != -1 )
      87           0 :         aFullNameLayout = aFullNameLayout.copy(0, nIndex);
      88             : 
      89           0 :     OUString aLayout(aFullNameLayout);
      90             : 
      91             :     std::vector<OUString>::const_iterator pIter =
      92           0 :             find(mrLayoutsToTransfer.begin(),mrLayoutsToTransfer.end(),aLayout);
      93             : 
      94           0 :     bool bFound = pIter != mrLayoutsToTransfer.end();
      95             : 
      96           0 :     const sal_uInt16 nMPageCount = rDoc.GetMasterPageCount();
      97           0 :     for (sal_uInt16 nMPage = 0; nMPage < nMPageCount && !bFound; nMPage++)
      98             :     {
      99             :         // Do the layouts already exist within the document?
     100           0 :         SdPage* pTestPage = (SdPage*) rDoc.GetMasterPage(nMPage);
     101           0 :         OUString aFullTest(pTestPage->GetLayoutName());
     102           0 :         sal_Int32 nIndex2 = aFullTest.indexOf( SD_LT_SEPARATOR );
     103           0 :         if( nIndex2 != -1 )
     104           0 :             aFullTest = aFullTest.copy(0, nIndex2);
     105             : 
     106           0 :         OUString aTest(aFullTest);
     107             : 
     108           0 :         if (aTest == aLayout)
     109             :         {
     110           0 :             if( bRenameDuplicates && pTestPage->getHash() != pBMMPage->getHash() )
     111             :             {
     112           0 :                 pBookmarkDoc->RenameLayoutTemplate( pBMMPage->GetLayoutName(), OUString(pBMMPage->GetName())+=OUString("_") );
     113           0 :                 aLayout = pBMMPage->GetName();
     114             : 
     115           0 :                 break;
     116             :             }
     117             :             else
     118           0 :                 bFound = true;
     119             :         }
     120           0 :     }
     121             : 
     122           0 :     if (!bFound)
     123           0 :         mrLayoutsToTransfer.push_back(aLayout);
     124           0 : }
     125             : 
     126             : // Inserts a bookmark as a page
     127           0 : static void lcl_IterateBookmarkPages( SdDrawDocument &rDoc, SdDrawDocument* pBookmarkDoc,
     128             :                                const std::vector<OUString> &rBookmarkList, sal_uInt16 nBMSdPageCount,
     129             :                                InsertBookmarkAsPage_FindDuplicateLayouts& rPageIterator, bool bRenameDuplicates = false )
     130             : {
     131             : 
     132             :     // Refactored copy'n'pasted layout name collection from InsertBookmarkAsPage
     133             : 
     134             :     int nPos, nEndPos;
     135             : 
     136           0 :     if( rBookmarkList.empty() )
     137             :     {
     138             :         // no list? whole source document
     139           0 :         nEndPos = nBMSdPageCount;
     140             :     }
     141             :     else
     142             :     {
     143             :         // bookmark list? number of entries
     144           0 :         nEndPos = rBookmarkList.size();
     145             :     }
     146             : 
     147             :     SdPage* pBMPage;
     148             : 
     149             :     // iterate over number of pages to insert
     150           0 :     for (nPos = 0; nPos < nEndPos; ++nPos)
     151             :     {
     152             :         // the master page associated to the nPos'th page to insert
     153           0 :         SdPage* pBMMPage = NULL;
     154             : 
     155           0 :         if( rBookmarkList.empty() )
     156             :         {
     157             :             // simply take master page of nPos'th page in source document
     158           0 :             pBMMPage = (SdPage*)(&(pBookmarkDoc->GetSdPage((sal_uInt16)nPos, PK_STANDARD)->TRG_GetMasterPage()));
     159             :         }
     160             :         else
     161             :         {
     162             :             // fetch nPos'th entry from bookmark list, and determine master page
     163           0 :             OUString aBMPgName(rBookmarkList[nPos]);
     164             :             sal_Bool  bIsMasterPage;
     165             : 
     166           0 :             sal_uInt16 nBMPage = pBookmarkDoc->GetPageByName( aBMPgName, bIsMasterPage );
     167             : 
     168           0 :             if (nBMPage != SDRPAGE_NOTFOUND)
     169             :             {
     170           0 :                 pBMPage = (SdPage*) pBookmarkDoc->GetPage(nBMPage);
     171             :             }
     172             :             else
     173             :             {
     174           0 :                 pBMPage = NULL;
     175             :             }
     176             : 
     177             :             // enforce that bookmarked page is a standard page and not already a master page
     178           0 :             if (pBMPage && pBMPage->GetPageKind()==PK_STANDARD && !pBMPage->IsMasterPage())
     179             :             {
     180           0 :                 const sal_uInt16 nBMSdPage = (nBMPage - 1) / 2;
     181           0 :                 pBMMPage = (SdPage*) (&(pBookmarkDoc->GetSdPage(nBMSdPage, PK_STANDARD)->TRG_GetMasterPage()));
     182           0 :             }
     183             :         }
     184             : 
     185             :         // successfully determined valid (bookmarked) page?
     186           0 :         if( pBMMPage )
     187             :         {
     188             :             // yes, call functor
     189           0 :             rPageIterator( rDoc, pBMMPage, bRenameDuplicates, pBookmarkDoc );
     190             :         }
     191             :     }
     192           0 : }
     193             : 
     194             : // Opens a bookmark document
     195           0 : SdDrawDocument* SdDrawDocument::OpenBookmarkDoc(SfxMedium& rMedium)
     196             : {
     197           0 :     sal_Bool bOK = sal_True;
     198           0 :     SdDrawDocument* pBookmarkDoc = NULL;
     199           0 :     OUString aBookmarkName = rMedium.GetName();
     200           0 :     const SfxFilter* pFilter = rMedium.GetFilter();
     201           0 :     if ( !pFilter )
     202             :     {
     203           0 :         rMedium.UseInteractionHandler( true );
     204           0 :         SFX_APP()->GetFilterMatcher().GuessFilter( rMedium, &pFilter );
     205             :     }
     206             : 
     207           0 :     if ( !pFilter )
     208             :     {
     209           0 :         bOK = sal_False;
     210             :     }
     211           0 :     else if ( !aBookmarkName.isEmpty() && maBookmarkFile != aBookmarkName )
     212             :     {
     213           0 :         sal_Bool bCreateGraphicShell = pFilter->GetServiceName() == "com.sun.star.drawing.DrawingDocument";
     214           0 :         sal_Bool bCreateImpressShell = pFilter->GetServiceName() == "com.sun.star.presentation.PresentationDocument";
     215           0 :         if ( bCreateGraphicShell || bCreateImpressShell )
     216             :         {
     217           0 :             CloseBookmarkDoc();
     218             : 
     219             :             // Create a DocShell, as OLE objects might be contained in the
     220             :             // document. (Persist)
     221             :             // If that wasn't the case, we could load the model directly.
     222           0 :             if ( bCreateGraphicShell )
     223             :                 // Draw
     224           0 :                 mxBookmarkDocShRef = new ::sd::GraphicDocShell(SFX_CREATE_MODE_STANDARD, sal_True);
     225             :             else
     226             :                 // Impress
     227           0 :                 mxBookmarkDocShRef = new ::sd::DrawDocShell(SFX_CREATE_MODE_STANDARD, sal_True);
     228             : 
     229           0 :             bOK = mxBookmarkDocShRef->DoLoad(&rMedium);
     230           0 :             if( bOK )
     231             :             {
     232           0 :                 maBookmarkFile = aBookmarkName;
     233           0 :                 pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
     234             :             }
     235             :         }
     236             :     }
     237             : 
     238             :     DBG_ASSERT(!aBookmarkName.isEmpty(), "Empty document name!");
     239             : 
     240           0 :     if (!bOK)
     241             :     {
     242           0 :         ErrorBox aErrorBox( NULL, (WinBits)WB_OK, SD_RESSTR(STR_READ_DATA_ERROR));
     243           0 :         aErrorBox.Execute();
     244             : 
     245           0 :         CloseBookmarkDoc();
     246           0 :         pBookmarkDoc = NULL;
     247             :     }
     248           0 :     else if (mxBookmarkDocShRef.Is())
     249             :     {
     250           0 :         pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
     251             :     }
     252             : 
     253           0 :     return(pBookmarkDoc);
     254             : }
     255             : 
     256             : // Opens a bookmark document
     257           0 : SdDrawDocument* SdDrawDocument::OpenBookmarkDoc(const OUString& rBookmarkFile)
     258             : {
     259           0 :     SdDrawDocument* pBookmarkDoc = NULL;
     260             : 
     261           0 :     if (!rBookmarkFile.isEmpty() && maBookmarkFile != rBookmarkFile)
     262             :     {
     263           0 :         SfxMedium* pMedium = new SfxMedium( rBookmarkFile, STREAM_READ );
     264           0 :         pBookmarkDoc = OpenBookmarkDoc(*pMedium);
     265             :     }
     266           0 :     else if (mxBookmarkDocShRef.Is())
     267             :     {
     268           0 :         pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
     269             :     }
     270             : 
     271           0 :     return(pBookmarkDoc);
     272             : }
     273             : 
     274             : // Inserts a bookmark (page or object)
     275           0 : sal_Bool SdDrawDocument::InsertBookmark(
     276             :     const std::vector<OUString> &rBookmarkList,    // List of names of the bookmarks to be inserted
     277             :     std::vector<OUString> &rExchangeList,          // List of the names to be used
     278             :     sal_Bool bLink,                                     // Insert bookmarks as links?
     279             :     sal_Bool bReplace,                                  // Replace current default and notes pages?
     280             :     sal_uInt16 nInsertPos,                              // Insertion position of pages
     281             :     sal_Bool bNoDialogs,                                // Don't show dialogs
     282             :     ::sd::DrawDocShell* pBookmarkDocSh,                 // If set, this is the source document
     283             :     sal_Bool bCopy,                                     // Copy the pages?
     284             :     Point* pObjPos)                                     // Insertion position of objects
     285             : {
     286           0 :     sal_Bool bOK = sal_True;
     287           0 :     sal_Bool bInsertPages = sal_False;
     288             : 
     289           0 :     if (rBookmarkList.empty())
     290             :     {
     291             :         // Insert all pages
     292           0 :         bInsertPages = sal_True;
     293             :     }
     294             :     else
     295             :     {
     296           0 :         SdDrawDocument* pBookmarkDoc = NULL;
     297             : 
     298           0 :         if (pBookmarkDocSh)
     299             :         {
     300           0 :             pBookmarkDoc = pBookmarkDocSh->GetDoc();
     301             :         }
     302           0 :         else if ( mxBookmarkDocShRef.Is() )
     303             :         {
     304           0 :             pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
     305             :         }
     306             :         else
     307           0 :             bOK = sal_False;
     308             : 
     309           0 :         std::vector<OUString>::const_iterator pIter;
     310           0 :         for ( pIter = rBookmarkList.begin(); bOK && pIter != rBookmarkList.end() && !bInsertPages; ++pIter )
     311             :         {
     312             :             // Is there a page name in the bookmark list?
     313             :             sal_Bool    bIsMasterPage;
     314           0 :             if( pBookmarkDoc->GetPageByName( *pIter, bIsMasterPage ) != SDRPAGE_NOTFOUND )
     315             :             {
     316             :                 // Found the page
     317           0 :                 bInsertPages = sal_True;
     318             :             }
     319             :         }
     320             :     }
     321             : 
     322           0 :     sal_Bool bCalcObjCount = !rExchangeList.empty();
     323             : 
     324           0 :     if ( bOK && bInsertPages )
     325             :     {
     326             :         // Insert all page bookmarks
     327             :         bOK = InsertBookmarkAsPage(rBookmarkList, &rExchangeList, bLink, bReplace,
     328           0 :                                    nInsertPos, bNoDialogs, pBookmarkDocSh, bCopy, sal_True, sal_False);
     329             :     }
     330             : 
     331           0 :     if ( bOK && !rBookmarkList.empty() )
     332             :     {
     333             :         // Insert all object bookmarks
     334             :         bOK = InsertBookmarkAsObject(rBookmarkList, rExchangeList, bLink,
     335           0 :                                      pBookmarkDocSh, pObjPos, bCalcObjCount);
     336             :     }
     337             : 
     338           0 :     return bOK;
     339             : }
     340             : 
     341             : namespace
     342             : {
     343             : 
     344             : void
     345           0 : lcl_removeUnusedStyles(SfxStyleSheetBasePool* const pStyleSheetPool, SdStyleSheetVector& rStyles)
     346             : {
     347           0 :     SdStyleSheetVector aUsedStyles;
     348           0 :     aUsedStyles.reserve(rStyles.size());
     349           0 :     for (SdStyleSheetVector::const_iterator aIt(rStyles.begin()), aLast(rStyles.end()); aIt != aLast; ++aIt)
     350             :     {
     351           0 :         if ((*aIt)->IsUsed())
     352           0 :             aUsedStyles.push_back(*aIt);
     353             :         else
     354           0 :             pStyleSheetPool->Remove((*aIt).get());
     355             :     }
     356           0 :     rStyles = aUsedStyles;
     357           0 : }
     358             : 
     359           0 : SfxStyleSheet *lcl_findStyle(SdStyleSheetVector& rStyles, const OUString& aStyleName)
     360             : {
     361           0 :     for(SdStyleSheetVector::const_iterator aIt(rStyles.begin()), aLast(rStyles.end()); aIt != aLast; ++aIt)
     362             :     {
     363           0 :         if((*aIt)->GetName().startsWith(aStyleName))
     364           0 :             return (*aIt).get();
     365             :     }
     366           0 :     return NULL;
     367             : }
     368             : 
     369             : }
     370             : 
     371           0 : sal_Bool SdDrawDocument::InsertBookmarkAsPage(
     372             :     const std::vector<OUString> &rBookmarkList,
     373             :     std::vector<OUString> *pExchangeList,            // List of names to be used
     374             :     sal_Bool bLink,
     375             :     sal_Bool bReplace,
     376             :     sal_uInt16 nInsertPos,
     377             :     sal_Bool bNoDialogs,
     378             :     ::sd::DrawDocShell* pBookmarkDocSh,
     379             :     sal_Bool bCopy,
     380             :     sal_Bool bMergeMasterPages,
     381             :     sal_Bool bPreservePageNames)
     382             : {
     383           0 :     sal_Bool bOK = sal_True;
     384           0 :     sal_Bool bContinue = sal_True;
     385           0 :     sal_Bool bScaleObjects = sal_False;
     386           0 :     sal_uInt16 nReplacedStandardPages = 0;
     387             : 
     388           0 :     SdDrawDocument* pBookmarkDoc = NULL;
     389           0 :     OUString aBookmarkName;
     390             : 
     391           0 :     if (pBookmarkDocSh)
     392             :     {
     393           0 :         pBookmarkDoc = pBookmarkDocSh->GetDoc();
     394             : 
     395           0 :         if (pBookmarkDocSh->GetMedium())
     396             :         {
     397           0 :             aBookmarkName = pBookmarkDocSh->GetMedium()->GetName();
     398             :         }
     399             :     }
     400           0 :     else if ( mxBookmarkDocShRef.Is() )
     401             :     {
     402           0 :         pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
     403           0 :         aBookmarkName = maBookmarkFile;
     404             :     }
     405             :     else
     406             :     {
     407           0 :         return sal_False;
     408             :     }
     409             : 
     410           0 :     const sal_uInt16 nSdPageCount = GetSdPageCount(PK_STANDARD);
     411           0 :     const sal_uInt32 nBMSdPageCount = pBookmarkDoc->GetSdPageCount(PK_STANDARD);
     412           0 :     const sal_uInt16 nMPageCount = GetMasterPageCount();
     413             : 
     414           0 :     if (nSdPageCount==0 || nBMSdPageCount==0 || nMPageCount==0)
     415             :     {
     416           0 :         bContinue = bOK = sal_False;
     417           0 :         return(bContinue);
     418             :     }
     419             : 
     420             :     // Store the size and some other properties of the first page and notes
     421             :     // page so that inserted pages can be properly scaled even when inserted
     422             :     // before the first page.
     423             :     // Note that the pointers are used later on as general page pointers.
     424           0 :     SdPage* pRefPage = GetSdPage(0, PK_STANDARD);
     425           0 :     Size  aSize(pRefPage->GetSize());
     426           0 :     sal_Int32 nLeft  = pRefPage->GetLftBorder();
     427           0 :     sal_Int32 nRight = pRefPage->GetRgtBorder();
     428           0 :     sal_Int32 nUpper = pRefPage->GetUppBorder();
     429           0 :     sal_Int32 nLower = pRefPage->GetLwrBorder();
     430           0 :     Orientation eOrient = pRefPage->GetOrientation();
     431             : 
     432           0 :     SdPage* pNPage = GetSdPage(0, PK_NOTES);
     433           0 :     Size aNSize(pNPage->GetSize());
     434           0 :     sal_Int32 nNLeft  = pNPage->GetLftBorder();
     435           0 :     sal_Int32 nNRight = pNPage->GetRgtBorder();
     436           0 :     sal_Int32 nNUpper = pNPage->GetUppBorder();
     437           0 :     sal_Int32 nNLower = pNPage->GetLwrBorder();
     438           0 :     Orientation eNOrient = pRefPage->GetOrientation();
     439             : 
     440             :     // Adapt page size and margins to those of the later pages?
     441           0 :     pRefPage = GetSdPage(nSdPageCount - 1, PK_STANDARD);
     442             : 
     443           0 :     if( bNoDialogs )
     444             :     {
     445           0 :         if( rBookmarkList.empty() )
     446           0 :             bScaleObjects = pRefPage->IsScaleObjects();
     447             :         else
     448           0 :             bScaleObjects = sal_True;
     449             :     }
     450             :     else
     451             :     {
     452           0 :         SdPage* pBMPage = pBookmarkDoc->GetSdPage(0,PK_STANDARD);
     453             : 
     454           0 :         if (pBMPage->GetSize()        != pRefPage->GetSize()         ||
     455           0 :             pBMPage->GetLftBorder()   != pRefPage->GetLftBorder()    ||
     456           0 :             pBMPage->GetRgtBorder()   != pRefPage->GetRgtBorder()    ||
     457           0 :             pBMPage->GetUppBorder()   != pRefPage->GetUppBorder()    ||
     458           0 :             pBMPage->GetLwrBorder()   != pRefPage->GetLwrBorder())
     459             :         {
     460           0 :             OUString aStr(SD_RESSTR(STR_SCALE_OBJECTS));
     461           0 :             sal_uInt16 nBut = QueryBox( NULL, WB_YES_NO_CANCEL, aStr).Execute();
     462             : 
     463           0 :             bScaleObjects = nBut == RET_YES;
     464           0 :             bContinue     = nBut != RET_CANCEL;
     465             : 
     466           0 :             if (!bContinue)
     467             :             {
     468           0 :                 return(bContinue);
     469           0 :             }
     470             :         }
     471             :     }
     472             : 
     473             :     // Get the necessary presentation stylesheets and transfer them before
     474             :     // the pages, else, the text objects won't reference their styles anymore.
     475           0 :     ::svl::IUndoManager* pUndoMgr = NULL;
     476           0 :     if( mpDocSh )
     477             :     {
     478           0 :         pUndoMgr = mpDocSh->GetUndoManager();
     479           0 :         pUndoMgr->EnterListAction(SD_RESSTR(STR_UNDO_INSERTPAGES), "");
     480             :     }
     481             : 
     482             :     // Refactored copy'n'pasted layout name collection into IterateBookmarkPages
     483             : 
     484           0 :     std::vector<OUString> aLayoutsToTransfer;
     485           0 :     InsertBookmarkAsPage_FindDuplicateLayouts aSearchFunctor( aLayoutsToTransfer );
     486           0 :     lcl_IterateBookmarkPages( *this, pBookmarkDoc, rBookmarkList, nBMSdPageCount, aSearchFunctor, ( rBookmarkList.empty() && pBookmarkDoc != this ) );
     487             : 
     488             :     // Copy the style that we actually need.
     489           0 :     SdStyleSheetPool& rBookmarkStyleSheetPool = dynamic_cast<SdStyleSheetPool&>(*pBookmarkDoc->GetStyleSheetPool());
     490           0 :     SdStyleSheetPool& rStyleSheetPool = dynamic_cast<SdStyleSheetPool&>(*GetStyleSheetPool());
     491             : 
     492             :     // When copying styles, also copy the master pages!
     493           0 :     if( !aLayoutsToTransfer.empty() )
     494           0 :         bMergeMasterPages = sal_True;
     495             : 
     496           0 :     std::vector<OUString>::const_iterator pIter;
     497           0 :     for ( pIter = aLayoutsToTransfer.begin(); pIter != aLayoutsToTransfer.end(); ++pIter )
     498             :     {
     499           0 :         SdStyleSheetVector aCreatedStyles;
     500           0 :         OUString layoutName = *pIter;
     501             : 
     502           0 :         rStyleSheetPool.CopyLayoutSheets(layoutName, rBookmarkStyleSheetPool,aCreatedStyles);
     503             : 
     504           0 :         if(!aCreatedStyles.empty())
     505             :         {
     506           0 :             if( pUndoMgr )
     507             :             {
     508           0 :                 SdMoveStyleSheetsUndoAction* pMovStyles = new SdMoveStyleSheetsUndoAction(this, aCreatedStyles, true);
     509           0 :                 pUndoMgr->AddUndoAction(pMovStyles);
     510             :             }
     511             :         }
     512           0 :     }
     513             : 
     514             :     // Copy styles. This unconditionally copies all styles, even those
     515             :     // that are not used in any of the inserted pages. The unused styles
     516             :     // are then removed at the end of the function, where we also create
     517             :     // undo records for the inserted styles.
     518           0 :     SdStyleSheetVector aNewGraphicStyles;
     519           0 :     OUString aRenameStr;
     520           0 :     if(!bReplace && !bNoDialogs)
     521           0 :         aRenameStr = OUString("_");
     522           0 :     rStyleSheetPool.RenameAndCopyGraphicSheets(rBookmarkStyleSheetPool, aNewGraphicStyles, aRenameStr);
     523           0 :     SdStyleSheetVector aNewCellStyles;
     524           0 :     rStyleSheetPool.CopyCellSheets(rBookmarkStyleSheetPool, aNewCellStyles);
     525             : 
     526             :     // TODO handle undo of table styles too
     527           0 :     rStyleSheetPool.CopyTableStyles(rBookmarkStyleSheetPool);
     528             : 
     529             :     // Insert document
     530             : 
     531           0 :     const bool bUndo = IsUndoEnabled();
     532             : 
     533           0 :     if( bUndo )
     534           0 :         BegUndo(SD_RESSTR(STR_UNDO_INSERTPAGES));
     535             : 
     536           0 :     if (rBookmarkList.empty())
     537             :     {
     538           0 :         if (nInsertPos >= GetPageCount())
     539             :         {
     540             :             // Add pages to the end
     541           0 :             nInsertPos = GetPageCount();
     542             :         }
     543             : 
     544           0 :         sal_uInt16 nActualInsertPos = nInsertPos;
     545             : 
     546             :         sal_uInt16 nBMSdPage;
     547           0 :         std::set<sal_uInt16> aRenameSet;
     548           0 :         std::map<sal_uInt16,OUString> aNameMap;
     549             : 
     550           0 :         for (nBMSdPage=0; nBMSdPage < nBMSdPageCount; nBMSdPage++)
     551             :         {
     552           0 :             SdPage* pBMPage = pBookmarkDoc->GetSdPage(nBMSdPage, PK_STANDARD);
     553           0 :             OUString sName(pBMPage->GetName());
     554             :             sal_Bool    bIsMasterPage;
     555             : 
     556           0 :             if (bLink)
     557             :             {
     558             :                 // Remember the names of all pages
     559           0 :                 aNameMap.insert(std::make_pair(nBMSdPage,sName));
     560             :             }
     561             : 
     562             :             // Have to check for duplicate names here, too
     563             :             // don't change name if source and dest model are the same!
     564           0 :             if( pBookmarkDoc != this &&
     565           0 :                 GetPageByName(sName, bIsMasterPage ) != SDRPAGE_NOTFOUND )
     566             :             {
     567             :                 // delay renaming *after* pages are copied (might destroy source otherwise)
     568           0 :                 aRenameSet.insert(nBMSdPage);
     569             :             }
     570           0 :         }
     571             : 
     572             :         Merge(*pBookmarkDoc,
     573             :               1,                 // Not the handout page
     574             :               0xFFFF,            // But all others
     575             :               nActualInsertPos,  // Insert at position ...
     576             :               bMergeMasterPages, // Move master pages?
     577             :               false,             // But only the master pages used
     578             :               true,              // Create an undo action
     579           0 :               bCopy);            // Copy (or merge) pages?
     580             : 
     581           0 :         for (nBMSdPage=0; nBMSdPage < nBMSdPageCount; nBMSdPage++)
     582             :         {
     583           0 :             SdPage* pPage       = (SdPage*) GetPage(nActualInsertPos);
     584           0 :             SdPage* pNotesPage  = (SdPage*) GetPage(nActualInsertPos+1);
     585             : 
     586             :             // delay renaming *after* pages are copied (might destroy source otherwise)
     587           0 :             if( aRenameSet.find(nBMSdPage) != aRenameSet.end() )
     588             :             {
     589             :                 // Page name already in use -> Use default name for default and
     590             :                 // notes page
     591           0 :                 pPage->SetName(OUString());
     592           0 :                 pNotesPage->SetName(OUString());
     593             :             }
     594             : 
     595           0 :             if (bLink)
     596             :             {
     597           0 :                 OUString aName(aNameMap[nBMSdPage]);
     598             : 
     599             :                 // Assemble all link names
     600           0 :                 pPage->SetFileName(aBookmarkName);
     601           0 :                 pPage->SetBookmarkName(aName);
     602           0 :                 pPage->SetModel(this);
     603             :             }
     604             : 
     605           0 :             nActualInsertPos += 2;
     606           0 :         }
     607             :     }
     608             :     else
     609             :     {
     610             :         // Insert selected pages
     611             :         SdPage* pBMPage;
     612             : 
     613           0 :         if (nInsertPos >= GetPageCount())
     614             :         {
     615             :             // Add pages to the end
     616           0 :             bReplace = sal_False;
     617           0 :             nInsertPos = GetPageCount();
     618             :         }
     619             : 
     620           0 :         sal_uInt16 nActualInsertPos = nInsertPos;
     621             : 
     622             :         // Collect the bookmarked pages
     623           0 :         ::std::vector<SdPage*> aBookmarkedPages (rBookmarkList.size(), NULL);
     624           0 :         for ( size_t nPos = 0, n = rBookmarkList.size(); nPos < n; ++nPos)
     625             :         {
     626           0 :             OUString aPgName(rBookmarkList[nPos]);
     627             :             sal_Bool    bIsMasterPage;
     628           0 :             sal_uInt16  nBMPage = pBookmarkDoc->GetPageByName( aPgName, bIsMasterPage );
     629             : 
     630           0 :             if (nBMPage != SDRPAGE_NOTFOUND)
     631             :             {
     632           0 :                 aBookmarkedPages[nPos] =  dynamic_cast<SdPage*>(pBookmarkDoc->GetPage(nBMPage));
     633             :             }
     634           0 :         }
     635             : 
     636           0 :         for ( size_t nPos = 0, n = rBookmarkList.size(); nPos < n; ++nPos)
     637             :         {
     638           0 :             pBMPage = aBookmarkedPages[nPos];
     639           0 :             sal_uInt16 nBMPage = pBMPage!=NULL ? pBMPage->GetPageNum() : SDRPAGE_NOTFOUND;
     640             : 
     641           0 :             if (pBMPage && pBMPage->GetPageKind()==PK_STANDARD && !pBMPage->IsMasterPage())
     642             :             {
     643             :                 // It has to be a default page
     644           0 :                 sal_Bool bMustRename = sal_False;
     645             : 
     646             :                 // delay renaming *after* pages are copied (might destroy source otherwise)
     647             :                 // don't change name if source and dest model are the same!
     648             :                 // avoid renaming if replacing the same page
     649           0 :                 OUString aPgName(rBookmarkList[nPos]);
     650             :                 sal_Bool    bIsMasterPage;
     651           0 :                 sal_uInt16 nPageSameName = GetPageByName(aPgName, bIsMasterPage);
     652           0 :                 if( pBookmarkDoc != this &&
     653           0 :                     nPageSameName != SDRPAGE_NOTFOUND &&
     654           0 :                     ( !bReplace ||
     655             :                       nPageSameName != nActualInsertPos ) )
     656             :                 {
     657           0 :                     bMustRename = sal_True;
     658             :                 }
     659             : 
     660           0 :                 SdPage* pBookmarkPage = pBMPage;
     661           0 :                 if (bReplace )
     662             :                 {
     663           0 :                     ReplacePageInCustomShows( dynamic_cast< SdPage* >( GetPage( nActualInsertPos ) ), pBookmarkPage );
     664             :                 }
     665             : 
     666             :                 Merge(*pBookmarkDoc,
     667             :                       nBMPage,           // From page (default page)
     668             :                       nBMPage+1,         // To page (notes page)
     669             :                       nActualInsertPos,  // Insert at position
     670             :                       bMergeMasterPages, // Move master pages?
     671             :                       false,             // But only the master pages used
     672             :                       true,              // Create undo action
     673           0 :                       bCopy);            // Copy (or merge) pages?
     674             : 
     675           0 :                 if( bReplace )
     676             :                 {
     677           0 :                     if( GetPage( nActualInsertPos ) != pBookmarkPage )
     678             :                     {
     679             :                         // bookmark page was not moved but cloned, so update custom shows again
     680           0 :                         ReplacePageInCustomShows( pBookmarkPage, dynamic_cast< SdPage* >( GetPage( nActualInsertPos ) ) );
     681             :                     }
     682             :                 }
     683             : 
     684           0 :                 if( bMustRename )
     685             :                 {
     686             :                     // Page name already in use -> use default name for default and
     687             :                     // notes page
     688           0 :                     SdPage* pPage = (SdPage*) GetPage(nActualInsertPos);
     689           0 :                     pPage->SetName(OUString());
     690           0 :                     SdPage* pNotesPage = (SdPage*) GetPage(nActualInsertPos+1);
     691           0 :                     pNotesPage->SetName(OUString());
     692             :                 }
     693             : 
     694           0 :                 if (bLink)
     695             :                 {
     696           0 :                     SdPage* pPage = (SdPage*) GetPage(nActualInsertPos);
     697           0 :                     pPage->SetFileName(aBookmarkName);
     698           0 :                     pPage->SetBookmarkName(aPgName);
     699           0 :                     pPage->SetModel(this);
     700             :                 }
     701             : 
     702           0 :                 if (bReplace)
     703             :                 {
     704             :                     // Remove page and notes page.
     705           0 :                     const sal_uInt16 nDestPageNum(nActualInsertPos + 2);
     706           0 :                     SdPage* pStandardPage = 0L;
     707             : 
     708           0 :                     if(nDestPageNum < GetPageCount())
     709             :                     {
     710           0 :                         pStandardPage = (SdPage*)GetPage(nDestPageNum);
     711             :                     }
     712             : 
     713           0 :                     if (pStandardPage)
     714             :                     {
     715           0 :                         if( bPreservePageNames )
     716             :                         {
     717             :                             // Take old slide names for inserted pages
     718           0 :                             SdPage* pPage = (SdPage*) GetPage(nActualInsertPos);
     719           0 :                             pPage->SetName( pStandardPage->GetRealName() );
     720             :                         }
     721             : 
     722           0 :                         if( bUndo )
     723           0 :                             AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pStandardPage));
     724             : 
     725           0 :                         RemovePage(nDestPageNum);
     726             : 
     727           0 :                         if( !bUndo )
     728           0 :                             delete pStandardPage;
     729             :                     }
     730             : 
     731           0 :                     SdPage* pNotesPage = 0L;
     732             : 
     733           0 :                     if(nDestPageNum < GetPageCount())
     734             :                     {
     735           0 :                         pNotesPage = (SdPage*)GetPage(nDestPageNum);
     736             :                     }
     737             : 
     738           0 :                     if (pNotesPage)
     739             :                     {
     740           0 :                         if( bPreservePageNames )
     741             :                         {
     742             :                             // Take old slide names for inserted pages
     743           0 :                             SdPage* pNewNotesPage = (SdPage*) GetPage(nActualInsertPos+1);
     744           0 :                             if( pNewNotesPage )
     745           0 :                                 pNewNotesPage->SetName( pStandardPage->GetRealName() );
     746             :                         }
     747             : 
     748           0 :                         if( bUndo )
     749           0 :                             AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pNotesPage));
     750             : 
     751           0 :                         RemovePage(nDestPageNum);
     752             : 
     753           0 :                         if( !bUndo )
     754           0 :                             delete pNotesPage;
     755             :                     }
     756             : 
     757           0 :                     nReplacedStandardPages++;
     758             :                 }
     759             : 
     760           0 :                 nActualInsertPos += 2;
     761             :             }
     762           0 :         }
     763             :     }
     764             : 
     765             :     // We might have duplicate master pages now, as the drawing engine does not
     766             :     // recognize duplicates. Remove these now.
     767           0 :     sal_uInt16 nNewMPageCount = GetMasterPageCount();
     768             : 
     769             :     // Go backwards, so the numbers don't become messed up
     770           0 :     for (sal_uInt16 nPage = nNewMPageCount - 1; nPage >= nMPageCount; nPage--)
     771             :     {
     772           0 :         pRefPage = (SdPage*) GetMasterPage(nPage);
     773           0 :         OUString aMPLayout(pRefPage->GetLayoutName());
     774           0 :         PageKind eKind = pRefPage->GetPageKind();
     775             : 
     776             :         // Does this already exist?
     777           0 :         for (sal_uInt16 nTest = 0; nTest < nMPageCount; nTest++)
     778             :         {
     779           0 :             SdPage* pTest = (SdPage*) GetMasterPage(nTest);
     780           0 :             OUString aTest(pTest->GetLayoutName());
     781             : 
     782             :             // nInsertPos > 2 is always true when inserting into non-empty models
     783           0 :             if ( nInsertPos > 2 &&
     784           0 :                  aTest == aMPLayout &&
     785           0 :                  eKind == pTest->GetPageKind() )
     786             :             {
     787           0 :                 if( bUndo )
     788           0 :                     AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pRefPage));
     789             : 
     790           0 :                 RemoveMasterPage(nPage);
     791             : 
     792           0 :                 if( !bUndo )
     793           0 :                     delete pRefPage;
     794           0 :                 nNewMPageCount--;
     795           0 :                 break;
     796             :             }
     797           0 :         }
     798           0 :     }
     799             : 
     800             :     // nInsertPos > 2 is always true when inserting into non-empty models
     801           0 :     if (nInsertPos > 0)
     802             :     {
     803           0 :         sal_uInt16 nSdPageStart = (nInsertPos - 1) / 2;
     804           0 :         sal_uInt16 nSdPageEnd = GetSdPageCount(PK_STANDARD) - nSdPageCount +
     805           0 :                             nSdPageStart - 1;
     806             :         const bool bRemoveEmptyPresObj =
     807           0 :                 (pBookmarkDoc->GetDocumentType() == DOCUMENT_TYPE_IMPRESS) &&
     808           0 :                 (GetDocumentType() == DOCUMENT_TYPE_DRAW);
     809             : 
     810           0 :         if( bReplace )
     811             :         {
     812           0 :             nSdPageEnd = nSdPageStart + nReplacedStandardPages - 1;
     813             :         }
     814             : 
     815           0 :         std::vector<OUString>::iterator pExchangeIter;
     816             : 
     817           0 :         if (pExchangeList)
     818           0 :             pExchangeIter = pExchangeList->begin();
     819             : 
     820           0 :         for (sal_uInt16 nSdPage = nSdPageStart; nSdPage <= nSdPageEnd; nSdPage++)
     821             :         {
     822           0 :             pRefPage = GetSdPage(nSdPage, PK_STANDARD);
     823             : 
     824           0 :             if (pExchangeList && pExchangeIter != pExchangeList->end())
     825             :             {
     826             :                 // Get the name to use from Exchange list
     827           0 :                 OUString aExchangeName(*pExchangeIter);
     828           0 :                 pRefPage->SetName(aExchangeName);
     829           0 :                 SdrHint aHint(HINT_PAGEORDERCHG);
     830           0 :                 aHint.SetPage(pRefPage);
     831           0 :                 Broadcast(aHint);
     832           0 :                 SdPage* pNewNotesPage = GetSdPage(nSdPage, PK_NOTES);
     833           0 :                 pNewNotesPage->SetName(aExchangeName);
     834           0 :                 aHint.SetPage(pNewNotesPage);
     835           0 :                 Broadcast(aHint);
     836             : 
     837           0 :                 ++pExchangeIter;
     838             :             }
     839             : 
     840           0 :             OUString aLayout(pRefPage->GetLayoutName());
     841           0 :             sal_Int32 nIndex = aLayout.indexOf( SD_LT_SEPARATOR );
     842           0 :             if( nIndex != -1 )
     843           0 :                 aLayout = aLayout.copy(0, nIndex);
     844             : 
     845             :             // update layout and referred master page
     846           0 :             pRefPage->SetPresentationLayout(aLayout);
     847           0 :             if( bUndo )
     848           0 :                 AddUndo( GetSdrUndoFactory().CreateUndoPageChangeMasterPage( *pRefPage ) );
     849             : 
     850           0 :             if (bScaleObjects)
     851             :             {
     852           0 :                 Rectangle aBorderRect(nLeft, nUpper, nRight, nLower);
     853           0 :                 pRefPage->ScaleObjects(aSize, aBorderRect, sal_True);
     854             :             }
     855           0 :             pRefPage->SetSize(aSize);
     856           0 :             pRefPage->SetBorder(nLeft, nUpper, nRight, nLower);
     857           0 :             pRefPage->SetOrientation( eOrient );
     858             : 
     859           0 :             if( bRemoveEmptyPresObj )
     860           0 :                 pRefPage->RemoveEmptyPresentationObjects();
     861             : 
     862           0 :             pRefPage = GetSdPage(nSdPage, PK_NOTES);
     863             : 
     864             :             // update layout and referred master page
     865           0 :             pRefPage->SetPresentationLayout(aLayout);
     866           0 :             if( bUndo )
     867           0 :                 AddUndo( GetSdrUndoFactory().CreateUndoPageChangeMasterPage( *pRefPage ) );
     868             : 
     869           0 :             if (bScaleObjects)
     870             :             {
     871           0 :                 Rectangle aBorderRect(nNLeft, nNUpper, nNRight, nNLower);
     872           0 :                 pRefPage->ScaleObjects(aNSize, aBorderRect, sal_True);
     873             :             }
     874             : 
     875           0 :             pRefPage->SetSize(aNSize);
     876           0 :             pRefPage->SetBorder(nNLeft, nNUpper, nNRight, nNLower);
     877           0 :             pRefPage->SetOrientation( eNOrient );
     878             : 
     879           0 :             if( bRemoveEmptyPresObj )
     880           0 :                 pRefPage->RemoveEmptyPresentationObjects();
     881           0 :         }
     882             : 
     883             :         ///Remove processed elements, to avoid doings hacks in InsertBookmarkAsObject
     884           0 :         if ( pExchangeList )
     885           0 :             pExchangeList->erase(pExchangeList->begin(),pExchangeIter);
     886             : 
     887           0 :         for (sal_uInt16 nPage = nMPageCount; nPage < nNewMPageCount; nPage++)
     888             :         {
     889           0 :             pRefPage = (SdPage*) GetMasterPage(nPage);
     890           0 :             if (pRefPage->GetPageKind() == PK_STANDARD)
     891             :             {
     892           0 :                 if (bScaleObjects)
     893             :                 {
     894           0 :                     Rectangle aBorderRect(nLeft, nUpper, nRight, nLower);
     895           0 :                     pRefPage->ScaleObjects(aSize, aBorderRect, sal_True);
     896             :                 }
     897           0 :                 pRefPage->SetSize(aSize);
     898           0 :                 pRefPage->SetBorder(nLeft, nUpper, nRight, nLower);
     899           0 :                 pRefPage->SetOrientation( eOrient );
     900             :             }
     901             :             else        // Can only be notes
     902             :             {
     903           0 :                 if (bScaleObjects)
     904             :                 {
     905           0 :                     Rectangle aBorderRect(nNLeft, nNUpper, nNRight, nNLower);
     906           0 :                     pRefPage->ScaleObjects(aNSize, aBorderRect, sal_True);
     907             :                 }
     908           0 :                 pRefPage->SetSize(aNSize);
     909           0 :                 pRefPage->SetBorder(nNLeft, nNUpper, nNRight, nNLower);
     910           0 :                 pRefPage->SetOrientation( eNOrient );
     911             :             }
     912             : 
     913           0 :             if( bRemoveEmptyPresObj )
     914           0 :                 pRefPage->RemoveEmptyPresentationObjects();
     915             :         }
     916             :     }
     917             : 
     918             :     // Make absolutely sure no double masterpages are there
     919           0 :     RemoveUnnecessaryMasterPages(NULL, sal_True, sal_True);
     920             : 
     921             :     // Rename object styles if necessary
     922           0 :     if(!aRenameStr.isEmpty())
     923             :     {
     924             :         try
     925             :         {
     926           0 :             for(sal_uInt32 p = nInsertPos; p < (nInsertPos + nBMSdPageCount); p++)
     927             :             {
     928           0 :                 SdPage *pPg = (SdPage *) GetPage(p);
     929           0 :                 for(sal_uIntPtr i = 0; i < pPg->GetObjCount(); i++)
     930             :                 {
     931           0 :                     if(pPg->GetObj(i)->GetStyleSheet())
     932             :                     {
     933           0 :                         OUString aStyleName = pPg->GetObj(i)->GetStyleSheet()->GetName();
     934           0 :                         SfxStyleSheet *pSheet = lcl_findStyle(aNewGraphicStyles, aStyleName + aRenameStr);
     935           0 :                         if(pSheet != NULL)
     936           0 :                             pPg->GetObj(i)->SetStyleSheet(pSheet, true);
     937             :                     }
     938             :                 }
     939             :             }
     940             :         }
     941           0 :         catch(...)
     942             :         {
     943             :             OSL_FAIL("Exception while renaming styles @ SdDrawDocument::InsertBookmarkAsPage");
     944             :         }
     945             :     }
     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           0 :     lcl_removeUnusedStyles(GetStyleSheetPool(), aNewGraphicStyles);
     951           0 :     if (!aNewGraphicStyles.empty() && pUndoMgr)
     952           0 :         pUndoMgr->AddUndoAction(new SdMoveStyleSheetsUndoAction(this, aNewGraphicStyles, true));
     953           0 :     lcl_removeUnusedStyles(GetStyleSheetPool(), aNewCellStyles);
     954           0 :     if (!aNewCellStyles.empty() && pUndoMgr)
     955           0 :         pUndoMgr->AddUndoAction(new SdMoveStyleSheetsUndoAction(this, aNewCellStyles, true));
     956             : 
     957           0 :     if( bUndo )
     958           0 :         EndUndo();
     959             : 
     960           0 :     if (pUndoMgr)
     961           0 :         pUndoMgr->LeaveListAction();
     962             : 
     963           0 :     return bContinue;
     964             : }
     965             : 
     966             : // Inserts a bookmark as an object
     967           0 : sal_Bool SdDrawDocument::InsertBookmarkAsObject(
     968             :     const std::vector<OUString> &rBookmarkList,
     969             :     const std::vector<OUString> &rExchangeList,            // List of names to use
     970             :     sal_Bool /* bLink */,
     971             :     ::sd::DrawDocShell* pBookmarkDocSh,
     972             :     Point* pObjPos, bool bCalcObjCount)
     973             : {
     974           0 :     sal_Bool bOK = sal_True;
     975           0 :     sal_Bool bOLEObjFound = sal_False;
     976           0 :     ::sd::View* pBMView = NULL;
     977             : 
     978           0 :     SdDrawDocument* pBookmarkDoc = NULL;
     979             : 
     980           0 :     if (pBookmarkDocSh)
     981             :     {
     982           0 :         pBookmarkDoc = pBookmarkDocSh->GetDoc();
     983             :     }
     984           0 :     else if ( mxBookmarkDocShRef.Is() )
     985             :     {
     986           0 :         pBookmarkDoc = mxBookmarkDocShRef->GetDoc();
     987             :     }
     988             :     else
     989             :     {
     990           0 :         return sal_False;
     991             :     }
     992             : 
     993           0 :     if (rBookmarkList.empty())
     994             :     {
     995           0 :         pBMView = new ::sd::View(*pBookmarkDoc, (OutputDevice*) NULL);
     996           0 :         pBMView->EndListening(*pBookmarkDoc);
     997           0 :         pBMView->MarkAll();
     998             :     }
     999             :     else
    1000             :     {
    1001             :         SdrPage* pPage;
    1002             :         SdrPageView* pPV;
    1003             : 
    1004           0 :         std::vector<OUString>::const_iterator pIter;
    1005           0 :         for ( pIter = rBookmarkList.begin(); pIter != rBookmarkList.end(); ++pIter )
    1006             :         {
    1007             :             // Get names of bookmarks from the list
    1008           0 :             SdrObject* pObj = pBookmarkDoc->GetObj(*pIter);
    1009             : 
    1010           0 :             if (pObj)
    1011             :             {
    1012             :                 // Found an object
    1013           0 :                 if (pObj->GetObjInventor() == SdrInventor &&
    1014           0 :                     pObj->GetObjIdentifier() == OBJ_OLE2)
    1015             :                 {
    1016           0 :                     bOLEObjFound = sal_True;
    1017             :                 }
    1018             : 
    1019           0 :                 if (!pBMView)
    1020             :                 {
    1021             :                     // Create View for the first time
    1022           0 :                     pBMView = new ::sd::View(*pBookmarkDoc, (OutputDevice*) NULL);
    1023           0 :                     pBMView->EndListening(*pBookmarkDoc);
    1024             :                 }
    1025             : 
    1026           0 :                 pPage = pObj->GetPage();
    1027             : 
    1028           0 :                 if (pPage->IsMasterPage())
    1029             :                 {
    1030           0 :                     pPV = pBMView->ShowSdrPage(pBMView->GetModel()->GetMasterPage(pPage->GetPageNum()));
    1031             :                 }
    1032             :                 else
    1033             :                 {
    1034           0 :                     pPV = pBMView->GetSdrPageView();
    1035           0 :                     if( !pPV || (pPV->GetPage() != pPage))
    1036           0 :                         pPV = pBMView->ShowSdrPage(pPage);
    1037             :                 }
    1038             : 
    1039           0 :                 pBMView->MarkObj(pObj, pPV, false);
    1040             :             }
    1041             :         }
    1042             :     }
    1043             : 
    1044           0 :     if (pBMView)
    1045             :     {
    1046             :         // Insert selected objects
    1047           0 :         ::sd::View* pView = new ::sd::View(*this, (OutputDevice*) NULL);
    1048           0 :         pView->EndListening(*this);
    1049             : 
    1050             :         // Look for the page into which the objects are supposed to be inserted
    1051           0 :         SdrPage* pPage = GetSdPage(0, PK_STANDARD);
    1052             : 
    1053           0 :         if (mpDocSh)
    1054             :         {
    1055           0 :             ::sd::ViewShell* pViewSh = mpDocSh->GetViewShell();
    1056             : 
    1057           0 :             if (pViewSh)
    1058             :             {
    1059             :                 // Which page is currently in view?
    1060           0 :                 SdrPageView* pPV = pViewSh->GetView()->GetSdrPageView();
    1061             : 
    1062           0 :                 if (pPV)
    1063             :                 {
    1064           0 :                     pPage = pPV->GetPage();
    1065             :                 }
    1066           0 :                 else if (pViewSh->GetActualPage())
    1067             :                 {
    1068           0 :                     pPage = pViewSh->GetActualPage();
    1069             :                 }
    1070             :             }
    1071             :         }
    1072             : 
    1073           0 :         Point aObjPos;
    1074             : 
    1075           0 :         if (pObjPos)
    1076             :         {
    1077           0 :             aObjPos = *pObjPos;
    1078             :         }
    1079             :         else
    1080             :         {
    1081           0 :             aObjPos = Rectangle(Point(), pPage->GetSize()).Center();
    1082             :         }
    1083             : 
    1084           0 :         sal_uLong nCountBefore = 0;
    1085             : 
    1086           0 :         if (!rExchangeList.empty() || bCalcObjCount)
    1087             :         {
    1088             :             // Sort OrdNums and get the number of objects before inserting
    1089           0 :             pPage->RecalcObjOrdNums();
    1090           0 :             nCountBefore = pPage->GetObjCount();
    1091             :         }
    1092             : 
    1093           0 :         if (bOLEObjFound)
    1094           0 :             pBMView->GetDoc().SetAllocDocSh(true);
    1095             : 
    1096           0 :         SdDrawDocument* pTmpDoc = (SdDrawDocument*) pBMView->GetMarkedObjModel();
    1097           0 :         bOK = pView->Paste(*pTmpDoc, aObjPos, pPage);
    1098             : 
    1099           0 :         if (bOLEObjFound)
    1100           0 :             pBMView->GetDoc().SetAllocDocSh(false);
    1101             : 
    1102           0 :         if (!bOLEObjFound)
    1103           0 :             delete pTmpDoc;         // Would otherwise be destroyed by DocShell
    1104             : 
    1105           0 :         delete pView;
    1106             : 
    1107           0 :         if (!rExchangeList.empty())
    1108             :         {
    1109             :             // Get number of objects after inserting.
    1110           0 :             sal_uLong nCount = pPage->GetObjCount();
    1111             : 
    1112           0 :             std::vector<OUString>::const_iterator pIter = rExchangeList.begin();
    1113           0 :             for (sal_uLong nObj = nCountBefore; nObj < nCount; nObj++)
    1114             :             {
    1115             :                 // Get the name to use from the Exchange list
    1116           0 :                 if (pIter != rExchangeList.end())
    1117             :                 {
    1118           0 :                     if (pPage->GetObj(nObj))
    1119             :                     {
    1120           0 :                         pPage->GetObj(nObj)->SetName(*pIter);
    1121             :                     }
    1122             : 
    1123           0 :                     ++pIter;
    1124             :                 }
    1125             :             }
    1126             :         }
    1127             :     }
    1128             : 
    1129           0 :     delete pBMView;
    1130             : 
    1131           0 :     return bOK;
    1132             : }
    1133             : 
    1134             : // Stops the bookmark insertion
    1135           0 : void SdDrawDocument::CloseBookmarkDoc()
    1136             : {
    1137           0 :     if (mxBookmarkDocShRef.Is())
    1138             :     {
    1139           0 :         mxBookmarkDocShRef->DoClose();
    1140             :     }
    1141             : 
    1142           0 :     mxBookmarkDocShRef.Clear();
    1143           0 :     maBookmarkFile = "";
    1144           0 : }
    1145             : 
    1146             : // Is this document read-only?
    1147           0 : bool SdDrawDocument::IsReadOnly() const
    1148             : {
    1149           0 :     return false;
    1150             : }
    1151             : 
    1152             : // In the subsequent AllocModel() a DocShell (xAllocedDocShRef) is created.
    1153             : // Any pre-existing DocShell is deleted
    1154           0 : void SdDrawDocument::SetAllocDocSh(bool bAlloc)
    1155             : {
    1156           0 :     mbAllocDocSh = bAlloc;
    1157             : 
    1158           0 :     if(mxAllocedDocShRef.Is())
    1159             :     {
    1160           0 :         mxAllocedDocShRef->DoClose();
    1161             :     }
    1162             : 
    1163           0 :     mxAllocedDocShRef.Clear();
    1164           0 : }
    1165             : 
    1166             : // Return list of CustomShows (create it, too, if necessary)
    1167           0 : SdCustomShowList* SdDrawDocument::GetCustomShowList(sal_Bool bCreate)
    1168             : {
    1169           0 :     if (!mpCustomShowList && bCreate)
    1170             :     {
    1171           0 :         mpCustomShowList = new SdCustomShowList;
    1172             :     }
    1173             : 
    1174           0 :     return mpCustomShowList;
    1175             : }
    1176             : 
    1177             : // Remove unused master pages and layouts
    1178           0 : void SdDrawDocument::RemoveUnnecessaryMasterPages(SdPage* pMasterPage, sal_Bool bOnlyDuplicatePages, sal_Bool bUndo)
    1179             : {
    1180           0 :     ::sd::View* pView = NULL;
    1181           0 :     ::svl::IUndoManager* pUndoMgr = NULL;
    1182             : 
    1183           0 :     if( bUndo && !IsUndoEnabled() )
    1184           0 :         bUndo = sal_False;
    1185             : 
    1186           0 :     if (mpDocSh)
    1187             :     {
    1188           0 :         pUndoMgr = mpDocSh->GetUndoManager();
    1189             : 
    1190           0 :         if (mpDocSh->GetViewShell())
    1191           0 :             pView = mpDocSh->GetViewShell()->GetView();
    1192             :     }
    1193             : 
    1194             :     // Check all master pages
    1195           0 :     sal_uInt16 nSdMasterPageCount = GetMasterSdPageCount( PK_STANDARD );
    1196           0 :     for (sal_Int32 nMPage = nSdMasterPageCount - 1; nMPage >= 0; nMPage--)
    1197             :     {
    1198           0 :         SdPage* pMaster = pMasterPage;
    1199           0 :         SdPage* pNotesMaster = NULL;
    1200             : 
    1201           0 :         if (!pMaster)
    1202             :         {
    1203           0 :             pMaster = (SdPage*) GetMasterSdPage( (sal_uInt16) nMPage, PK_STANDARD );
    1204           0 :             pNotesMaster = (SdPage*) GetMasterSdPage( (sal_uInt16) nMPage, PK_NOTES );
    1205             :         }
    1206             :         else
    1207             :         {
    1208           0 :             for ( sal_uInt16 nMPg = 0; nMPg < GetMasterPageCount(); nMPg++ )
    1209             :             {
    1210           0 :                 if ( pMaster == GetMasterPage( nMPg ) )
    1211             :                 {
    1212           0 :                     pNotesMaster = (SdPage*) GetMasterPage( ++nMPg );
    1213           0 :                     break;
    1214             :                 }
    1215             :             }
    1216             :         }
    1217             : 
    1218             :         DBG_ASSERT( pMaster->GetPageKind() == PK_STANDARD, "wrong page kind" );
    1219             : 
    1220           0 :         if ( pMaster->GetPageKind() == PK_STANDARD &&
    1221           0 :              GetMasterPageUserCount( pMaster ) == 0 &&
    1222             :              pNotesMaster )
    1223             :         {
    1224             :             // Do not delete master pages that have their precious flag set
    1225           0 :             sal_Bool bDeleteMaster = !pMaster->IsPrecious();
    1226           0 :             OUString aLayoutName = pMaster->GetLayoutName();
    1227             : 
    1228           0 :             if(bOnlyDuplicatePages )
    1229             :             {
    1230             :                 // remove only duplicate pages
    1231           0 :                 bDeleteMaster = sal_False;
    1232           0 :                 for (sal_uInt16 i = 0; i < GetMasterSdPageCount( PK_STANDARD ); i++)
    1233             :                 {
    1234           0 :                     SdPage* pMPg = (SdPage*) GetMasterSdPage( i, PK_STANDARD );
    1235           0 :                     if( pMPg != pMaster &&
    1236           0 :                         pMPg->GetLayoutName() == aLayoutName )
    1237             :                     {
    1238             :                         // duplicate page found -> remove it
    1239           0 :                         bDeleteMaster = sal_True;
    1240             :                     }
    1241             :                 }
    1242             :             }
    1243             : 
    1244           0 :             if( bDeleteMaster )
    1245             :             {
    1246           0 :                 if (pView)
    1247             :                 {
    1248             :                     // if MasterPage is visible hide on pageview
    1249           0 :                     SdrPageView* pPgView = pView->GetSdrPageView();
    1250           0 :                     if (pPgView)
    1251             :                     {
    1252           0 :                         SdrPage* pShownPage = pPgView->GetPage();
    1253           0 :                         if( (pShownPage == pMaster) || (pShownPage == pNotesMaster) )
    1254             :                         {
    1255           0 :                             pView->HideSdrPage();
    1256           0 :                             pView->ShowSdrPage( GetSdPage( 0, PK_STANDARD ) );
    1257             :                         }
    1258             :                     }
    1259             :                 }
    1260             : 
    1261           0 :                 if( bUndo )
    1262             :                 {
    1263           0 :                     BegUndo();
    1264           0 :                     AddUndo( GetSdrUndoFactory().CreateUndoDeletePage( *pNotesMaster ) );
    1265             :                 }
    1266             : 
    1267           0 :                 RemoveMasterPage( pNotesMaster->GetPageNum() );
    1268             : 
    1269           0 :                 if( !bUndo )
    1270           0 :                     delete pNotesMaster;
    1271             : 
    1272           0 :                 if( bUndo )
    1273           0 :                     AddUndo(GetSdrUndoFactory().CreateUndoDeletePage(*pMaster));
    1274             : 
    1275           0 :                 RemoveMasterPage( pMaster->GetPageNum() );
    1276             : 
    1277           0 :                 if( !bUndo )
    1278           0 :                     delete pMaster;
    1279             : 
    1280           0 :                 if( bUndo )
    1281           0 :                     EndUndo();  // do this here already, so Joe's actions happen _between_ our own
    1282             : 
    1283             :                 // Delete old, unused layout stylesheets
    1284           0 :                 sal_Bool bDeleteOldStyleSheets = sal_True;
    1285           0 :                 for ( sal_uInt16 nMPg = 0;
    1286           0 :                           nMPg < GetMasterPageCount() && bDeleteOldStyleSheets;
    1287             :                        nMPg++ )
    1288             :                 {
    1289           0 :                     SdPage* pMPg = (SdPage*) GetMasterPage(nMPg);
    1290           0 :                     if (pMPg->GetLayoutName() == aLayoutName)
    1291             :                     {
    1292           0 :                         bDeleteOldStyleSheets = sal_False;
    1293             :                     }
    1294             :                 }
    1295             : 
    1296           0 :                 if (bDeleteOldStyleSheets)
    1297             :                 {
    1298           0 :                     SdStyleSheetVector aRemove;
    1299           0 :                     static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->CreateLayoutSheetList( aLayoutName, aRemove );
    1300             : 
    1301           0 :                     if( bUndo )
    1302             :                     {
    1303             :                         // This list belongs to UndoAction
    1304           0 :                         SdMoveStyleSheetsUndoAction* pMovStyles = new SdMoveStyleSheetsUndoAction( this, aRemove, false );
    1305             : 
    1306           0 :                         if (pUndoMgr)
    1307           0 :                             pUndoMgr->AddUndoAction(pMovStyles);
    1308             :                     }
    1309             : 
    1310           0 :                     for( SdStyleSheetVector::iterator iter = aRemove.begin(); iter != aRemove.end(); ++iter )
    1311           0 :                         static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->Remove((*iter).get());
    1312             :                 }
    1313           0 :             }
    1314             :         }
    1315             : 
    1316           0 :         if (pMasterPage)
    1317           0 :             break;                      // Just this one master page!
    1318             :     }
    1319           0 : }
    1320             : 
    1321             : /** Exchange master page
    1322             :   *
    1323             :   * Either the nSdPageNum gets a new, own master page or the master page is
    1324             :   * exchanged completely (which then applies to all pages).
    1325             :   *
    1326             :   * nSdPageNum   : page number that the new master page should get.
    1327             :   * rLayoutName  : LayoutName of the new master page
    1328             :   * pSourceDoc   : document (template) to get the master page from
    1329             :   * bMaster      : exchange the master page of nSdPageNum
    1330             :   * bCheckMasters: remove unused master pages
    1331             :   *
    1332             :   * If pSourceDoc == NULL, an empty master page is applied.
    1333             :   * If rLayoutName is empty, the first master page is used.
    1334             :   */
    1335             : // #i121863# factored out functionality
    1336           0 : bool isMasterPageLayoutNameUnique(const SdDrawDocument& rDoc, const OUString& rCandidate)
    1337             : {
    1338           0 :     if (rCandidate.isEmpty())
    1339             :     {
    1340           0 :         return false;
    1341             :     }
    1342             : 
    1343           0 :     const sal_uInt16 nPageCount(rDoc.GetMasterPageCount());
    1344             : 
    1345           0 :     for(sal_uInt16 a(0); a < nPageCount; a++)
    1346             :     {
    1347           0 :         const SdrPage* pCandidate = rDoc.GetMasterPage(a);
    1348           0 :         OUString aPageLayoutName(pCandidate->GetLayoutName());
    1349           0 :         sal_Int32 nIndex = aPageLayoutName.indexOf(SD_LT_SEPARATOR);
    1350           0 :         if( nIndex != -1 )
    1351           0 :             aPageLayoutName = aPageLayoutName.copy(0, nIndex);
    1352             : 
    1353           0 :         if(aPageLayoutName == rCandidate)
    1354             :         {
    1355           0 :             return false;
    1356             :         }
    1357           0 :     }
    1358             : 
    1359           0 :     return true;
    1360             : }
    1361             : 
    1362             : // #i121863# factored out functinality
    1363           0 : OUString createNewMasterPageLayoutName(const SdDrawDocument& rDoc)
    1364             : {
    1365           0 :     const OUString aBaseName(SdResId(STR_LAYOUT_DEFAULT_NAME));
    1366           0 :     OUString aRetval;
    1367           0 :     sal_uInt16 nCount(0);
    1368             : 
    1369           0 :     while (aRetval.isEmpty())
    1370             :     {
    1371           0 :         aRetval = aBaseName;
    1372             : 
    1373           0 :         if(nCount)
    1374             :         {
    1375           0 :             aRetval += OUString::number(nCount);
    1376             :         }
    1377             : 
    1378           0 :         nCount++;
    1379             : 
    1380           0 :         if(!isMasterPageLayoutNameUnique(rDoc, aRetval))
    1381             :         {
    1382           0 :             aRetval = "";
    1383             :         }
    1384             :     }
    1385             : 
    1386           0 :     return aRetval;
    1387             : }
    1388             : 
    1389           0 : void SdDrawDocument::SetMasterPage(sal_uInt16 nSdPageNum,
    1390             :                                    const OUString& rLayoutName,
    1391             :                                    SdDrawDocument* pSourceDoc,
    1392             :                                    sal_Bool bMaster,
    1393             :                                    sal_Bool bCheckMasters)
    1394             : {
    1395           0 :     ::svl::IUndoManager* pUndoMgr = NULL;
    1396             : 
    1397           0 :     if( mpDocSh )
    1398             :     {
    1399           0 :         mpDocSh->SetWaitCursor( true );
    1400           0 :         pUndoMgr = mpDocSh->GetUndoManager();
    1401             :     }
    1402             : 
    1403           0 :     const bool bUndo = pUndoMgr && IsUndoEnabled();
    1404             : 
    1405           0 :     if (bUndo)
    1406             :     {
    1407           0 :         pUndoMgr->EnterListAction(SD_RESSTR(STR_UNDO_SET_PRESLAYOUT), OUString());
    1408             :     }
    1409             : 
    1410           0 :     SdPage* pSelectedPage   = GetSdPage(nSdPageNum, PK_STANDARD);
    1411           0 :     SdPage* pNotes          = (SdPage*) GetPage(pSelectedPage->GetPageNum()+1);
    1412           0 :     SdPage& rOldMaster      = (SdPage&)pSelectedPage->TRG_GetMasterPage();
    1413           0 :     SdPage& rOldNotesMaster = (SdPage&)pNotes->TRG_GetMasterPage();
    1414           0 :     SdPage* pMaster         = NULL;
    1415           0 :     SdPage* pNotesMaster    = NULL;
    1416           0 :     SdPage* pPage           = NULL;
    1417           0 :     OUString aOldPageLayoutName(pSelectedPage->GetLayoutName());
    1418           0 :     OUString aOldLayoutName(aOldPageLayoutName);
    1419           0 :     sal_Int32 nIndex = aOldLayoutName.indexOf( SD_LT_SEPARATOR );
    1420           0 :     if( nIndex != -1 )
    1421           0 :         aOldLayoutName = aOldLayoutName.copy(0, nIndex);
    1422             : 
    1423           0 :     if (pSourceDoc)
    1424             :     {
    1425           0 :         std::vector<StyleReplaceData> aReplList; // List of replaced stylesheets
    1426           0 :         sal_Bool bLayoutReloaded = sal_False;   // Wurde ex. Layout wieder geladen?
    1427             : 
    1428             :         // LayoutName, Page and Notes page
    1429           0 :         if (rLayoutName.isEmpty())
    1430             :         {
    1431             :             // No LayoutName: take first MasterPage
    1432           0 :             pMaster = (SdPage*) pSourceDoc->GetMasterSdPage(0, PK_STANDARD);
    1433           0 :             pNotesMaster = (SdPage*) pSourceDoc->GetMasterSdPage(0, PK_NOTES);
    1434             :         }
    1435             :         else
    1436             :         {
    1437           0 :             OUStringBuffer aBuf(rLayoutName);
    1438           0 :             aBuf.append(SD_LT_SEPARATOR).append(SdResId(STR_LAYOUT_OUTLINE).toString());
    1439           0 :             OUString aSearchFor(aBuf.makeStringAndClear());
    1440             : 
    1441           0 :             for (sal_uInt16 nMP = 0; nMP < pSourceDoc->GetMasterPageCount(); ++nMP)
    1442             :             {
    1443           0 :                 SdPage* pMP = (SdPage*) pSourceDoc->GetMasterPage(nMP);
    1444             : 
    1445           0 :                 if (pMP->GetLayoutName() == aSearchFor)
    1446             :                 {
    1447           0 :                     if (pMP->GetPageKind() == PK_STANDARD)
    1448           0 :                         pMaster = pMP;
    1449           0 :                     if (pMP->GetPageKind() == PK_NOTES)
    1450           0 :                         pNotesMaster = pMP;
    1451             :                 }
    1452           0 :                 if (pMaster && pNotesMaster)
    1453           0 :                     break;
    1454             :             }
    1455             :             DBG_ASSERT(pMaster, "MasterPage (Standard page) not found");
    1456             :             DBG_ASSERT(pNotesMaster, "MasterPage (Notes page) not found");
    1457             : 
    1458             :             // this should not happen, but looking at crashreports, it does
    1459           0 :             if( (pMaster == NULL) || (pNotesMaster == NULL) )
    1460             :             {
    1461             :                 // so take the first MasterPage
    1462           0 :                 pMaster = (SdPage*) pSourceDoc->GetMasterSdPage(0, PK_STANDARD);
    1463           0 :                 pNotesMaster = (SdPage*) pSourceDoc->GetMasterSdPage(0, PK_NOTES);
    1464           0 :             }
    1465             :         }
    1466             : 
    1467             :         // we should never reach this, but one never knows...
    1468           0 :         if( (pMaster == NULL) || (pNotesMaster == NULL) )
    1469             :         {
    1470           0 :             if (bUndo)
    1471           0 :                 pUndoMgr->LeaveListAction();
    1472             : 
    1473           0 :             if( mpDocSh )
    1474           0 :                 mpDocSh->SetWaitCursor( false );
    1475             : 
    1476             :             OSL_FAIL( "SdDrawDocument::SetMasterPage() failed!" );
    1477             : 
    1478           0 :             return;
    1479             :         }
    1480             : 
    1481           0 :         const OUString aOriginalNewLayoutName( pMaster->GetName() );
    1482           0 :         OUString aTargetNewLayoutName(aOriginalNewLayoutName);
    1483             : 
    1484           0 :         if (pSourceDoc != this)
    1485             :         {
    1486             :             // #i121863# clone masterpages, they are from another model (!)
    1487           0 :             SdPage* pNewNotesMaster = dynamic_cast< SdPage* >(pNotesMaster->Clone(this));
    1488           0 :             SdPage* pNewMaster = dynamic_cast< SdPage* >(pMaster->Clone(this));
    1489             : 
    1490           0 :             if(!pNewNotesMaster || !pNewMaster)
    1491             :             {
    1492           0 :                 delete pNewNotesMaster;
    1493           0 :                 delete pNewMaster;
    1494             :                 OSL_FAIL("SdDrawDocument::SetMasterPage() cloning of MasterPage/NoteAmsterPage failed!" );
    1495           0 :                 return;
    1496             :             }
    1497             : 
    1498           0 :             pNotesMaster = pNewNotesMaster;
    1499           0 :             pMaster = pNewMaster;
    1500             : 
    1501             :             // layout name needs to be unique
    1502           0 :             aTargetNewLayoutName = pMaster->GetLayoutName();
    1503           0 :             sal_Int32 nIndex2 = aTargetNewLayoutName.indexOf(SD_LT_SEPARATOR);
    1504           0 :             if( nIndex2 != -1 )
    1505           0 :                 aTargetNewLayoutName = aTargetNewLayoutName.copy(0, nIndex2);
    1506             : 
    1507           0 :             if(!isMasterPageLayoutNameUnique(*this, aTargetNewLayoutName))
    1508             :             {
    1509           0 :                 aTargetNewLayoutName = createNewMasterPageLayoutName(*this);
    1510             : 
    1511           0 :                 OUString aTemp(aTargetNewLayoutName);
    1512           0 :                 aTemp += SD_LT_SEPARATOR;
    1513           0 :                 aTemp += SD_RESSTR(STR_LAYOUT_OUTLINE);
    1514             : 
    1515           0 :                 pMaster->SetName(aTargetNewLayoutName);
    1516           0 :                 pMaster->SetLayoutName(aTemp);
    1517             : 
    1518           0 :                 pNotesMaster->SetName(aTargetNewLayoutName);
    1519           0 :                 pNotesMaster->SetLayoutName(aTemp);
    1520             :             }
    1521             :         }
    1522             : 
    1523           0 :         if (pSourceDoc != this)
    1524             :         {
    1525           0 :             const sal_uInt16 nMasterPageCount = GetMasterPageCount();
    1526           0 :             for ( sal_uInt16 nMPage = 0; nMPage < nMasterPageCount; nMPage++ )
    1527             :             {
    1528           0 :                 SdPage* pCheckMaster = (SdPage*)GetMasterPage(nMPage);
    1529           0 :                 if( pCheckMaster->GetName() == aTargetNewLayoutName )
    1530             :                 {
    1531           0 :                     bLayoutReloaded = sal_True;
    1532           0 :                     break;
    1533             :                 }
    1534             :             }
    1535             : 
    1536             :             // Correct or create presentation templates --
    1537             :             // only worry about presentation templates
    1538           0 :             OUString aName;
    1539           0 :             SdStyleSheetPool* pSourceStyleSheetPool = (SdStyleSheetPool*) pSourceDoc->GetStyleSheetPool();
    1540           0 :             pSourceStyleSheetPool->SetSearchMask(SD_STYLE_FAMILY_MASTERPAGE);
    1541           0 :             static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->SetSearchMask(SD_STYLE_FAMILY_MASTERPAGE);
    1542             : 
    1543           0 :             SdStyleSheetVector aCreatedStyles;          // List of created stylesheets
    1544           0 :             SfxStyleSheetBase* pHisSheet = pSourceStyleSheetPool->First();
    1545             : 
    1546           0 :             while (pHisSheet)
    1547             :             {
    1548           0 :                 aName = pHisSheet->GetName();
    1549             : 
    1550             :                 // #i121863# search in source styles with original style name from source of
    1551             :                 // evtl. cloned master (not-cloned, renamed for uniqueness)
    1552           0 :                 if( aName.startsWith( aOriginalNewLayoutName ) )
    1553             :                 {
    1554             :                     // #i121863# build name of evtl. cloned master style to search for
    1555           0 :                     if(aOriginalNewLayoutName != aTargetNewLayoutName)
    1556             :                     {
    1557           0 :                         const sal_Int32 nPos(aName.indexOf(SD_LT_SEPARATOR));
    1558           0 :                         aName = aTargetNewLayoutName + aName.copy(nPos);
    1559             :                     }
    1560             : 
    1561           0 :                     SfxStyleSheet* pMySheet = static_cast<SfxStyleSheet*>( mxStyleSheetPool->Find(aName, SD_STYLE_FAMILY_MASTERPAGE) );
    1562             : 
    1563           0 :                     if (pMySheet)
    1564             :                     {
    1565             :                         // A stylesheet of the same name already exists -> overwrite contents
    1566             : #ifdef DBG_UTIL
    1567             :                         sal_Bool bTest =
    1568             : #endif
    1569           0 :                             pMySheet->SetName(pHisSheet->GetName());
    1570             :                         DBG_ASSERT(bTest, "Renaming StyleSheet failed.");
    1571           0 :                         pMySheet->GetItemSet().ClearItem(0);  // Delete all
    1572             : 
    1573             :                         StyleSheetUndoAction* pUndoChStyle = new StyleSheetUndoAction(this,
    1574           0 :                                                                  pMySheet, &pHisSheet->GetItemSet());
    1575           0 :                         if (bUndo)
    1576           0 :                             pUndoMgr->AddUndoAction(pUndoChStyle);
    1577           0 :                         pMySheet->GetItemSet().Put(pHisSheet->GetItemSet());
    1578           0 :                         pMySheet->Broadcast(SfxSimpleHint(SFX_HINT_DATACHANGED));
    1579             :                     }
    1580             :                     else
    1581             :                     {
    1582             :                         // create new style
    1583           0 :                         OUString aHelpFile;
    1584           0 :                         pMySheet = static_cast<SfxStyleSheet*>( &mxStyleSheetPool->Make(aName, SD_STYLE_FAMILY_MASTERPAGE, pHisSheet->GetMask()) );
    1585           0 :                         pMySheet->SetHelpId( aHelpFile, pHisSheet->GetHelpId(aHelpFile) );
    1586           0 :                         pMySheet->GetItemSet().ClearItem(0);  // Delete all
    1587           0 :                         pMySheet->GetItemSet().Put(pHisSheet->GetItemSet());
    1588             : 
    1589           0 :                         aCreatedStyles.push_back( SdStyleSheetRef( static_cast< SdStyleSheet* >( pMySheet ) ) );
    1590             :                     }
    1591             : 
    1592           0 :                     StyleReplaceData aReplData;
    1593           0 :                     aReplData.nNewFamily = pMySheet->GetFamily();
    1594           0 :                     aReplData.nFamily    = pMySheet->GetFamily();
    1595           0 :                     aReplData.aNewName   = pMySheet->GetName();
    1596             : 
    1597             :                     // #i121863# re-create original name of styte used at page where to replace with
    1598             :                     // this new style
    1599           0 :                     OUString aTemp(pMySheet->GetName());
    1600           0 :                     const sal_Int32 nPos(aTemp.indexOf(SD_LT_SEPARATOR));
    1601           0 :                     aTemp = aOldLayoutName + aTemp.copy(nPos);
    1602           0 :                     aReplData.aName = aTemp;
    1603           0 :                     aReplList.push_back(aReplData);
    1604             :                 }
    1605             : 
    1606           0 :                 pHisSheet = (SfxStyleSheet*) pSourceStyleSheetPool->Next();
    1607             :             }
    1608             : 
    1609             :             // If new styles were created: re-create parent chaining of the item
    1610             :             // sets in the styles.
    1611           0 :             if(!aCreatedStyles.empty())
    1612             :             {
    1613           0 :                 std::vector<StyleReplaceData>::iterator pRDataIter;
    1614           0 :                 for ( pRDataIter = aReplList.begin(); pRDataIter != aReplList.end(); ++pRDataIter )
    1615             :                 {
    1616           0 :                     SfxStyleSheetBase* pSOld = mxStyleSheetPool->Find(pRDataIter->aName);
    1617           0 :                     SfxStyleSheetBase* pSNew = mxStyleSheetPool->Find(pRDataIter->aNewName);
    1618             : 
    1619           0 :                     if (pSOld && pSNew)
    1620             :                     {
    1621           0 :                         const OUString& rParentOfOld = pSOld->GetParent();
    1622           0 :                         const OUString& rParentOfNew = pSNew->GetParent();
    1623             : 
    1624           0 :                         if (!rParentOfOld.isEmpty() && rParentOfNew.isEmpty())
    1625             :                         {
    1626           0 :                             std::vector<StyleReplaceData>::iterator pRDIter;
    1627           0 :                             for ( pRDIter = aReplList.begin(); pRDIter != aReplList.end(); ++pRDIter )
    1628             :                             {
    1629           0 :                                 if ((pRDIter->aName == rParentOfOld) && (pRDIter->aName != pRDIter->aNewName))
    1630             :                                 {
    1631           0 :                                     OUString aParentOfNew(pRDIter->aNewName);
    1632           0 :                                     pSNew->SetParent(aParentOfNew);
    1633           0 :                                     break;
    1634             :                                 }
    1635             :                             }
    1636             :                         }
    1637             :                     }
    1638             :                 }
    1639             : 
    1640             :                 // Now look for all of them when searching
    1641           0 :                 pSourceStyleSheetPool->SetSearchMask(SFX_STYLE_FAMILY_ALL);
    1642           0 :                 mxStyleSheetPool->SetSearchMask(SFX_STYLE_FAMILY_ALL);
    1643             :             }
    1644             : 
    1645           0 :             if( !aCreatedStyles.empty() )
    1646             :             {
    1647             :                 // Add UndoAction for creating and inserting the stylesheets to
    1648             :                 // the top of the UndoManager
    1649           0 :                 SdMoveStyleSheetsUndoAction* pMovStyles = new SdMoveStyleSheetsUndoAction( this, aCreatedStyles, true);
    1650           0 :                 if (bUndo)
    1651           0 :                     pUndoMgr->AddUndoAction(pMovStyles);
    1652           0 :             }
    1653             :         }
    1654             : 
    1655             :         // Create layout name based upon the name of the page layout of the
    1656             :         // master page
    1657           0 :         OUString aPageLayoutName(pMaster->GetLayoutName());
    1658           0 :         OUString aLayoutName = aPageLayoutName;
    1659           0 :         sal_Int32 nIndex2 = aLayoutName.indexOf( SD_LT_SEPARATOR );
    1660           0 :         if( nIndex2 != -1 )
    1661           0 :             aLayoutName = aLayoutName.copy( 0, nIndex2);
    1662             : 
    1663             :         // #i121863# Do *not* remove from original document any longer, it is potentially used there
    1664             :         // and would lead to crashes. Rely on the automatic process of removing unused masterpages
    1665             :         // (see RemoveUnnecessaryMasterPages)
    1666             :         //if (pSourceDoc != this)
    1667             :         //{
    1668             :         //    // Remove from the source document
    1669             :         //    pSourceDoc->RemoveMasterPage(pNotesMaster->GetPageNum());
    1670             :         //    pSourceDoc->RemoveMasterPage(pMaster->GetPageNum());
    1671             :         //}
    1672             : 
    1673             :         // Register the new master pages with the document and then use the
    1674             :         // the new presentation layout for the default and notes pages
    1675           0 :         if (pSourceDoc != this)
    1676             :         {
    1677             :             // Insert the master pages:
    1678             :             // Insert master pages from new layouts at the end.
    1679             :             // If a layout is being replaced, however, insert them before the
    1680             :             // position of the old master page, so from now on the new master
    1681             :             // page will be found when searching (e.g.
    1682             :             // SdPage::SetPresentationLayout).
    1683           0 :             sal_uInt16 nInsertPos = rOldMaster.GetPageNum();
    1684           0 :             BegUndo();
    1685             : 
    1686           0 :             if (!bLayoutReloaded)
    1687           0 :                 nInsertPos = 0xFFFF;
    1688           0 :             InsertMasterPage(pMaster, nInsertPos);
    1689           0 :             if( bUndo )
    1690           0 :                 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pMaster));
    1691             : 
    1692           0 :             nInsertPos++;
    1693           0 :             if (!bLayoutReloaded)
    1694           0 :                 nInsertPos = 0xFFFF;
    1695           0 :             InsertMasterPage(pNotesMaster, nInsertPos);
    1696           0 :             if( bUndo )
    1697             :             {
    1698           0 :                 AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pNotesMaster));
    1699             : 
    1700           0 :                 EndUndo(); // do this here already, so Joe's actions happen _between_ our own.
    1701             :             }
    1702             :         }
    1703             : 
    1704             :         // Fill list with pages
    1705           0 :         std::vector<SdPage*> aPageList;
    1706             : 
    1707             : //      #98456, this has to be removed according to CL (KA 07/08/2002)
    1708             : //      #109884# but we need them again to restore the styles of the presentation objects while undo
    1709           0 :         aPageList.push_back(pMaster);
    1710           0 :         aPageList.push_back(pNotesMaster);
    1711             : 
    1712           0 :         if (bMaster || bLayoutReloaded)
    1713             :         {
    1714           0 :             for (sal_uInt16 nPage = 1; nPage < GetPageCount(); nPage++)
    1715             :             {
    1716           0 :                 pPage = (SdPage*) GetPage(nPage);
    1717           0 :                 OUString aTest = pPage->GetLayoutName();
    1718           0 :                 if (aTest == aOldPageLayoutName)
    1719             :                 {
    1720           0 :                     aPageList.push_back(pPage);
    1721             :                 }
    1722           0 :             }
    1723             : 
    1724             :         }
    1725             :         else
    1726             :         {
    1727           0 :             aPageList.push_back(pSelectedPage);
    1728           0 :             aPageList.push_back(pNotes);
    1729             :         }
    1730             : 
    1731           0 :         for (std::vector<SdPage*>::iterator pIter = aPageList.begin(); pIter != aPageList.end(); ++pIter)
    1732             :         {
    1733           0 :             AutoLayout eAutoLayout = (*pIter)->GetAutoLayout();
    1734             : 
    1735           0 :             if( bUndo )
    1736             :             {
    1737             :                 SdPresentationLayoutUndoAction * pPLUndoAction =
    1738             :                     new SdPresentationLayoutUndoAction
    1739             :                         (this,
    1740           0 :                         ( pPage && pPage->IsMasterPage() ) ? aLayoutName : aOldLayoutName,
    1741             :                         aLayoutName,
    1742           0 :                          eAutoLayout, eAutoLayout, sal_False, *pIter);
    1743           0 :                 pUndoMgr->AddUndoAction(pPLUndoAction);
    1744             :             }
    1745           0 :             (*pIter)->SetPresentationLayout(aLayoutName);
    1746           0 :             (*pIter)->SetAutoLayout(eAutoLayout);
    1747             :         }
    1748             : 
    1749             :         // Adapt new master pages
    1750           0 :         if (pSourceDoc != this)
    1751             :         {
    1752           0 :             Size aSize(rOldMaster.GetSize());
    1753           0 :             Rectangle aBorderRect(rOldMaster.GetLftBorder(),
    1754           0 :                                   rOldMaster.GetUppBorder(),
    1755           0 :                                   rOldMaster.GetRgtBorder(),
    1756           0 :                                   rOldMaster.GetLwrBorder());
    1757           0 :             pMaster->ScaleObjects(aSize, aBorderRect, sal_True);
    1758           0 :             pMaster->SetSize(aSize);
    1759           0 :             pMaster->SetBorder(rOldMaster.GetLftBorder(),
    1760           0 :                                rOldMaster.GetUppBorder(),
    1761           0 :                                rOldMaster.GetRgtBorder(),
    1762           0 :                                rOldMaster.GetLwrBorder());
    1763           0 :             pMaster->SetOrientation( rOldMaster.GetOrientation() );
    1764           0 :             pMaster->SetAutoLayout(pMaster->GetAutoLayout());
    1765             : 
    1766           0 :             aSize = rOldNotesMaster.GetSize();
    1767           0 :             Rectangle aNotesBorderRect(rOldNotesMaster.GetLftBorder(),
    1768           0 :                                        rOldNotesMaster.GetUppBorder(),
    1769           0 :                                        rOldNotesMaster.GetRgtBorder(),
    1770           0 :                                        rOldNotesMaster.GetLwrBorder());
    1771           0 :             pNotesMaster->ScaleObjects(aSize, aNotesBorderRect, sal_True);
    1772           0 :             pNotesMaster->SetSize(aSize);
    1773           0 :             pNotesMaster->SetBorder(rOldNotesMaster.GetLftBorder(),
    1774           0 :                                     rOldNotesMaster.GetUppBorder(),
    1775           0 :                                     rOldNotesMaster.GetRgtBorder(),
    1776           0 :                                     rOldNotesMaster.GetLwrBorder());
    1777           0 :             pNotesMaster->SetOrientation( rOldNotesMaster.GetOrientation() );
    1778           0 :             pNotesMaster->SetAutoLayout(pNotesMaster->GetAutoLayout());
    1779             : 
    1780           0 :             if( (pSourceDoc->GetDocumentType() == DOCUMENT_TYPE_IMPRESS) &&
    1781           0 :                 (GetDocumentType() == DOCUMENT_TYPE_DRAW) )
    1782             :             {
    1783           0 :                 pMaster->RemoveEmptyPresentationObjects();
    1784           0 :                 pNotesMaster->RemoveEmptyPresentationObjects();
    1785             :             }
    1786           0 :         }
    1787             :     }
    1788             :     else
    1789             :     {
    1790             :         // Find a new name for the layout
    1791           0 :         OUString aName(createNewMasterPageLayoutName(*this));
    1792           0 :         OUString aPageLayoutName(aName + SD_LT_SEPARATOR + SD_RESSTR(STR_LAYOUT_OUTLINE));
    1793             : 
    1794             :         // Generate new stylesheets
    1795           0 :         static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->CreateLayoutStyleSheets(aName);
    1796           0 :         SdStyleSheetVector aCreatedStyles;
    1797           0 :         static_cast<SdStyleSheetPool*>( mxStyleSheetPool.get())->CreateLayoutSheetList(aName, aCreatedStyles);
    1798             : 
    1799           0 :         if( bUndo )
    1800             :         {
    1801           0 :             SdMoveStyleSheetsUndoAction* pMovStyles = new SdMoveStyleSheetsUndoAction(this, aCreatedStyles, true);
    1802           0 :             pUndoMgr->AddUndoAction(pMovStyles);
    1803             :         }
    1804             : 
    1805             :         // Generate new master pages and register them with the document
    1806           0 :         if( bUndo )
    1807           0 :             BegUndo();
    1808             : 
    1809           0 :         pMaster = (SdPage*) AllocPage(true);
    1810           0 :         pMaster->SetSize(pSelectedPage->GetSize());
    1811           0 :         pMaster->SetBorder(pSelectedPage->GetLftBorder(),
    1812           0 :                            pSelectedPage->GetUppBorder(),
    1813           0 :                            pSelectedPage->GetRgtBorder(),
    1814           0 :                            pSelectedPage->GetLwrBorder() );
    1815           0 :         pMaster->SetName(aName);
    1816           0 :         pMaster->SetLayoutName(aPageLayoutName);
    1817           0 :         InsertMasterPage(pMaster);
    1818             : 
    1819           0 :         if( bUndo )
    1820           0 :             AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pMaster));
    1821             : 
    1822           0 :         pMaster->SetAutoLayout(AUTOLAYOUT_NONE, true, true);
    1823             : 
    1824           0 :         pNotesMaster = (SdPage*) AllocPage(true);
    1825           0 :         pNotesMaster->SetPageKind(PK_NOTES);
    1826           0 :         pNotesMaster->SetSize(pNotes->GetSize());
    1827           0 :         pNotesMaster->SetBorder(pNotes->GetLftBorder(),
    1828           0 :                                 pNotes->GetUppBorder(),
    1829           0 :                                 pNotes->GetRgtBorder(),
    1830           0 :                                 pNotes->GetLwrBorder() );
    1831           0 :         pNotesMaster->SetName(aName);
    1832           0 :         pNotesMaster->SetLayoutName(aPageLayoutName);
    1833           0 :         InsertMasterPage(pNotesMaster);
    1834             : 
    1835           0 :         if( bUndo )
    1836           0 :             AddUndo(GetSdrUndoFactory().CreateUndoNewPage(*pNotesMaster));
    1837             : 
    1838           0 :         pNotesMaster->SetAutoLayout(AUTOLAYOUT_NOTES, true, true);
    1839             : 
    1840           0 :         if( bUndo )
    1841           0 :             EndUndo();
    1842             : 
    1843             :         // Create a list of affected default and notes pages
    1844           0 :         std::vector<SdPage*> aPageList;
    1845           0 :         if (bMaster)
    1846             :         {
    1847           0 :             for (sal_uInt16 nPage = 1; nPage < GetPageCount(); nPage++)
    1848             :             {
    1849           0 :                 pPage = (SdPage*) GetPage(nPage);
    1850           0 :                 if (pPage->GetLayoutName() == aOldPageLayoutName)
    1851             :                 {
    1852           0 :                     aPageList.push_back(pPage);
    1853             :                 }
    1854             :             }
    1855             :         }
    1856             :         else
    1857             :         {
    1858           0 :             aPageList.push_back(pSelectedPage);
    1859           0 :             aPageList.push_back(pNotes);
    1860             :         }
    1861             : 
    1862             :         // Set presentation layout and AutoLayout for the affected pages
    1863           0 :         for ( std::vector<SdPage*>::iterator pIter = aPageList.begin(); pIter != aPageList.end(); ++pIter )
    1864             :         {
    1865           0 :             AutoLayout eOldAutoLayout = (*pIter)->GetAutoLayout();
    1866             :             AutoLayout eNewAutoLayout =
    1867           0 :                 (*pIter)->GetPageKind() == PK_STANDARD ? AUTOLAYOUT_NONE : AUTOLAYOUT_NOTES;
    1868             : 
    1869           0 :             if( bUndo )
    1870             :             {
    1871             :                 SdPresentationLayoutUndoAction * pPLUndoAction =
    1872             :                     new SdPresentationLayoutUndoAction
    1873             :                             (this, aOldLayoutName, aName,
    1874             :                              eOldAutoLayout, eNewAutoLayout, sal_True,
    1875           0 :                              *pIter);
    1876           0 :                 pUndoMgr->AddUndoAction(pPLUndoAction);
    1877             :             }
    1878             : 
    1879           0 :             (*pIter)->SetPresentationLayout(aName);
    1880           0 :             (*pIter)->SetAutoLayout(eNewAutoLayout);
    1881           0 :         }
    1882             :     }
    1883             : 
    1884             :     // If the old master pages aren't used anymore, they and their styles have
    1885             :     // to be removed.
    1886           0 :     if (bCheckMasters)
    1887             :     {
    1888             :         // Check all
    1889           0 :         RemoveUnnecessaryMasterPages();
    1890             :     }
    1891             :     else
    1892             :     {
    1893             :         // Check only the master page that was replaced
    1894           0 :         RemoveUnnecessaryMasterPages(&rOldMaster);
    1895             :     }
    1896             : 
    1897           0 :     if( bUndo )
    1898           0 :         pUndoMgr->LeaveListAction();
    1899             : 
    1900           0 :     if( mpDocSh )
    1901           0 :         mpDocSh->SetWaitCursor( false );
    1902             : }
    1903             : 
    1904           0 : void SdDrawDocument::Merge(SdrModel& rSourceModel,
    1905             :                sal_uInt16 nFirstPageNum, sal_uInt16 nLastPageNum,
    1906             :                sal_uInt16 nDestPos,
    1907             :                bool bMergeMasterPages, bool bAllMasterPages,
    1908             :                bool bUndo, bool bTreadSourceAsConst)
    1909             : {
    1910           0 :     sal_uInt16 nMasterPageCount = GetMasterPageCount();
    1911           0 :     SdrModel::Merge( rSourceModel, nFirstPageNum, nLastPageNum, nDestPos, bMergeMasterPages, bAllMasterPages, bUndo, bTreadSourceAsConst );
    1912             : 
    1913             :     // add style family for each new master page
    1914           0 :     for( sal_uInt16 nMaster = nMasterPageCount; nMaster < GetMasterPageCount(); nMaster++ )
    1915             :     {
    1916           0 :         SdPage* pPage = static_cast< SdPage* >( GetMasterPage( nMaster ) );
    1917           0 :         if( pPage && pPage->IsMasterPage() && (pPage->GetPageKind() == PK_STANDARD) )
    1918             :         {
    1919             :             // new master page created, add its style family
    1920           0 :             SdStyleSheetPool* pStylePool = (SdStyleSheetPool*) GetStyleSheetPool();
    1921           0 :             if( pStylePool )
    1922           0 :                 pStylePool->AddStyleFamily( pPage );
    1923             :         }
    1924             :     }
    1925           0 : }
    1926             : 
    1927             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10