bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / view / ViewClipboard.cxx
blob063dfaf69ff07e28802861e4ba76657f367a2285
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "ViewClipboard.hxx"
22 #include "DrawDocShell.hxx"
23 #include "View.hxx"
24 #include "ViewShell.hxx"
25 #include "Window.hxx"
27 #include "drawdoc.hxx"
28 #include "sdpage.hxx"
29 #include "sdxfer.hxx"
30 #include "sdresid.hxx"
31 #include "glob.hrc"
33 #include <svx/svdpagv.hxx>
34 #include <osl/mutex.hxx>
35 #include <vcl/svapp.hxx>
37 namespace sd {
39 ViewClipboard::ViewClipboard (::sd::View& rView)
40 : mrView(rView)
44 ViewClipboard::~ViewClipboard()
48 void ViewClipboard::HandlePageDrop (const SdTransferable& rTransferable)
50 // Determine whether to insert the given set of slides or to assign a
51 // given master page.
52 SdPage* pMasterPage = GetFirstMasterPage (rTransferable);
53 if (pMasterPage != NULL)
54 AssignMasterPage (rTransferable, pMasterPage);
55 else
56 InsertSlides (rTransferable, DetermineInsertPosition (rTransferable));
59 SdPage* ViewClipboard::GetFirstMasterPage (const SdTransferable& rTransferable)
61 SdPage* pFirstMasterPage = NULL;
63 if (rTransferable.HasPageBookmarks())
67 const std::vector<OUString> &rBookmarks = rTransferable.GetPageBookmarks();
69 if (rBookmarks.empty())
70 break;
72 DrawDocShell* pDocShell = rTransferable.GetPageDocShell();
73 if (pDocShell == NULL)
74 break;
76 SdDrawDocument* pDocument = pDocShell->GetDoc();
77 if (pDocument == NULL)
78 break;
80 std::vector<OUString>::const_iterator pIter;
81 for ( pIter = rBookmarks.begin(); pIter != rBookmarks.end(); ++pIter )
83 OUString sName (*pIter);
84 bool bIsMasterPage;
86 // SdPage* GetMasterSdPage(sal_uInt16 nPgNum, PageKind ePgKind);
87 // sal_uInt16 GetMasterSdPageCount(PageKind ePgKind) const;
89 sal_uInt16 nBMPage = pDocument->GetPageByName (
90 sName, bIsMasterPage);
91 if ( ! bIsMasterPage)
93 // At least one regular slide: return NULL to indicate
94 // that not all bookmarks point to master pages.
95 pFirstMasterPage = NULL;
96 break;
98 else if (pFirstMasterPage == NULL)
100 // Remember the first master page for later.
101 if (nBMPage != SDRPAGE_NOTFOUND)
102 pFirstMasterPage = static_cast<SdPage*>(
103 pDocument->GetMasterPage(nBMPage));
107 while (false);
110 return pFirstMasterPage;
113 void ViewClipboard::AssignMasterPage (
114 const SdTransferable& rTransferable,
115 SdPage* pMasterPage)
117 if (pMasterPage == NULL)
118 return;
120 // Get the target page to which the master page is assigned.
121 SdrPageView* pPageView = mrView.GetSdrPageView();
122 if (pPageView == NULL)
123 return;
125 SdPage* pPage = static_cast<SdPage*>(pPageView->GetPage());
126 if (pPage == NULL)
127 return;
129 SdDrawDocument& rDocument = mrView.GetDoc();
131 if ( ! rTransferable.HasPageBookmarks())
132 return;
134 DrawDocShell* pDataDocShell = rTransferable.GetPageDocShell();
135 if (pDataDocShell == NULL)
136 return;
138 SdDrawDocument* pSourceDocument = pDataDocShell->GetDoc();
139 if (pSourceDocument == NULL)
140 return;
142 // We have to remove the layout suffix from the layout name which is
143 // appended again by SetMasterPage() to the given name. Don't ask.
144 OUString sLayoutSuffix = SD_LT_SEPARATOR + SD_RESSTR(STR_LAYOUT_OUTLINE);
145 sal_Int32 nLength = sLayoutSuffix.getLength();
146 OUString sLayoutName = pMasterPage->GetLayoutName();
147 if (sLayoutName.endsWith(sLayoutSuffix))
148 sLayoutName = sLayoutName.copy(0, sLayoutName.getLength() - nLength);
150 rDocument.SetMasterPage (
151 pPage->GetPageNum() / 2,
152 sLayoutName,
153 pSourceDocument,
154 false, // Exchange the master page of only the target page.
155 false // Keep unused master pages.
159 sal_uInt16 ViewClipboard::DetermineInsertPosition (
160 const SdTransferable& )
162 SdDrawDocument& rDoc = mrView.GetDoc();
163 sal_uInt16 nPgCnt = rDoc.GetSdPageCount( PK_STANDARD );
165 // Insert position is the behind the last selected page or behind the
166 // last page when the selection is empty.
167 sal_uInt16 nInsertPos = rDoc.GetSdPageCount( PK_STANDARD ) * 2 + 1;
168 for( sal_uInt16 nPage = 0; nPage < nPgCnt; nPage++ )
170 SdPage* pPage = rDoc.GetSdPage( nPage, PK_STANDARD );
172 if( pPage->IsSelected() )
173 nInsertPos = nPage * 2 + 3;
176 return nInsertPos;
179 sal_uInt16 ViewClipboard::InsertSlides (
180 const SdTransferable& rTransferable,
181 sal_uInt16 nInsertPosition)
183 SdDrawDocument& rDoc = mrView.GetDoc();
185 sal_uInt16 nInsertPgCnt = 0;
186 bool bMergeMasterPages = !rTransferable.HasSourceDoc( &rDoc );
188 // Prepare the insertion.
189 const std::vector<OUString> *pBookmarkList = NULL;
190 DrawDocShell* pDataDocSh;
191 if (rTransferable.HasPageBookmarks())
193 // When the transferable contains page bookmarks then the referenced
194 // pages are inserted.
195 pBookmarkList = &rTransferable.GetPageBookmarks();
196 pDataDocSh = rTransferable.GetPageDocShell();
197 nInsertPgCnt = (sal_uInt16)pBookmarkList->size();
199 else
201 // Otherwise all pages of the document of the transferable are
202 // inserted.
203 SfxObjectShell* pShell = rTransferable.GetDocShell();
204 pDataDocSh = static_cast<DrawDocShell*>(pShell);
205 SdDrawDocument* pDataDoc = pDataDocSh->GetDoc();
207 if (pDataDoc!=NULL && pDataDoc->GetSdPageCount(PK_STANDARD))
208 nInsertPgCnt = pDataDoc->GetSdPageCount(PK_STANDARD);
210 if (nInsertPgCnt > 0)
212 const SolarMutexGuard aGuard;
213 ::sd::Window* pWin = mrView.GetViewShell()->GetActiveWindow();
214 const bool bWait = pWin && pWin->IsWait();
216 if( bWait )
217 pWin->LeaveWait();
219 rDoc.InsertBookmarkAsPage(
220 pBookmarkList ? *pBookmarkList : std::vector<OUString>(),
221 NULL,
222 false,
223 false,
224 nInsertPosition,
225 (&rTransferable == SD_MOD()->pTransferDrag),
226 pDataDocSh,
227 true,
228 bMergeMasterPages,
229 false);
231 if( bWait )
232 pWin->EnterWait();
235 return nInsertPgCnt;
238 } // end of namespace ::sd
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */