1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <ViewClipboard.hxx>
22 #include <DrawDocShell.hxx>
23 #include <DrawViewShell.hxx>
25 #include <ViewShell.hxx>
28 #include <drawdoc.hxx>
32 #include <strings.hxx>
34 #include <svx/svdpagv.hxx>
35 #include <vcl/svapp.hxx>
39 ViewClipboard::ViewClipboard (::sd::View
& 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
52 // tdf#113405 only assign master pages to normal pages, don't attempt to assign a master
53 // page to a master page
54 sd::DrawViewShell
* pDrawViewShell
= dynamic_cast<::sd::DrawViewShell
*>(mrView
.GetViewShell());
55 SdPage
* pMasterPage
= (pDrawViewShell
&& pDrawViewShell
->GetEditMode() == EditMode::Page
) ? GetFirstMasterPage(rTransferable
) : nullptr;
57 AssignMasterPage (rTransferable
, pMasterPage
);
59 InsertSlides (rTransferable
, DetermineInsertPosition ());
62 SdPage
* ViewClipboard::GetFirstMasterPage (const SdTransferable
& rTransferable
)
64 SdPage
* pFirstMasterPage
= nullptr;
66 if (rTransferable
.HasPageBookmarks())
70 const std::vector
<OUString
> &rBookmarks
= rTransferable
.GetPageBookmarks();
72 if (rBookmarks
.empty())
75 DrawDocShell
* pDocShell
= rTransferable
.GetPageDocShell();
76 if (pDocShell
== nullptr)
79 SdDrawDocument
* pDocument
= pDocShell
->GetDoc();
80 if (pDocument
== nullptr)
83 for (const OUString
& sName
: rBookmarks
)
87 // SdPage* GetMasterSdPage(sal_uInt16 nPgNum, PageKind ePgKind);
88 // sal_uInt16 GetMasterSdPageCount(PageKind ePgKind) const;
90 sal_uInt16 nBMPage
= pDocument
->GetPageByName (
91 sName
, bIsMasterPage
);
94 // At least one regular slide: return NULL to indicate
95 // that not all bookmarks point to master pages.
96 pFirstMasterPage
= nullptr;
99 else if (pFirstMasterPage
== nullptr)
101 // Remember the first master page for later.
102 if (nBMPage
!= SDRPAGE_NOTFOUND
)
103 pFirstMasterPage
= static_cast<SdPage
*>(
104 pDocument
->GetMasterPage(nBMPage
));
111 return pFirstMasterPage
;
114 void ViewClipboard::AssignMasterPage (
115 const SdTransferable
& rTransferable
,
116 SdPage
const * pMasterPage
)
118 if (pMasterPage
== nullptr)
121 // Get the target page to which the master page is assigned.
122 SdrPageView
* pPageView
= mrView
.GetSdrPageView();
123 if (pPageView
== nullptr)
126 SdPage
* pPage
= static_cast<SdPage
*>(pPageView
->GetPage());
127 if (pPage
== nullptr)
130 SdDrawDocument
& rDocument
= mrView
.GetDoc();
132 if ( ! rTransferable
.HasPageBookmarks())
135 DrawDocShell
* pDataDocShell
= rTransferable
.GetPageDocShell();
136 if (pDataDocShell
== nullptr)
139 SdDrawDocument
* pSourceDocument
= pDataDocShell
->GetDoc();
140 if (pSourceDocument
== nullptr)
143 // We have to remove the layout suffix from the layout name which is
144 // appended again by SetMasterPage() to the given name. Don't ask.
145 OUString sLayoutSuffix
= SD_LT_SEPARATOR
+ STR_LAYOUT_OUTLINE
;
146 sal_Int32 nLength
= sLayoutSuffix
.getLength();
147 OUString sLayoutName
= pMasterPage
->GetLayoutName();
148 if (sLayoutName
.endsWith(sLayoutSuffix
))
149 sLayoutName
= sLayoutName
.copy(0, sLayoutName
.getLength() - nLength
);
151 rDocument
.SetMasterPage (
152 pPage
->GetPageNum() / 2,
155 false, // Exchange the master page of only the target page.
156 false // Keep unused master pages.
160 sal_uInt16
ViewClipboard::DetermineInsertPosition ()
162 SdDrawDocument
& rDoc
= mrView
.GetDoc();
163 sal_uInt16 nPgCnt
= rDoc
.GetSdPageCount( PageKind::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( PageKind::Standard
) * 2 + 1;
168 for( sal_uInt16 nPage
= 0; nPage
< nPgCnt
; nPage
++ )
170 SdPage
* pPage
= rDoc
.GetSdPage( nPage
, PageKind::Standard
);
172 if( pPage
->IsSelected() )
173 nInsertPos
= nPage
* 2 + 3;
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
= nullptr;
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
= static_cast<sal_uInt16
>(pBookmarkList
->size());
201 // Otherwise all pages of the document of the transferable are
203 SfxObjectShell
* pShell
= rTransferable
.GetDocShell().get();
204 pDataDocSh
= static_cast<DrawDocShell
*>(pShell
);
205 SdDrawDocument
* pDataDoc
= pDataDocSh
->GetDoc();
207 if (pDataDoc
!=nullptr && pDataDoc
->GetSdPageCount(PageKind::Standard
))
208 nInsertPgCnt
= pDataDoc
->GetSdPageCount(PageKind::Standard
);
210 if (nInsertPgCnt
> 0)
212 const SolarMutexGuard aGuard
;
213 ::sd::Window
* pWin
= mrView
.GetViewShell()->GetActiveWindow();
214 const bool bWait
= pWin
&& pWin
->IsWait();
219 rDoc
.InsertBookmarkAsPage(
220 pBookmarkList
? *pBookmarkList
: std::vector
<OUString
>(),
225 (&rTransferable
== SD_MOD()->pTransferDrag
),
238 } // end of namespace ::sd
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */