xref: /aoo41x/main/sd/source/ui/view/ViewClipboard.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 
31 #include "ViewClipboard.hxx"
32 
33 #include "DrawDocShell.hxx"
34 #include "View.hxx"
35 #include "ViewShell.hxx"
36 #include "Window.hxx"
37 
38 #include "drawdoc.hxx"
39 #include "sdpage.hxx"
40 #include "sdxfer.hxx"
41 #include "sdresid.hxx"
42 #include "glob.hrc"
43 
44 #include <svx/svdpagv.hxx>
45 #include <vos/mutex.hxx>
46 #include <vcl/svapp.hxx>
47 
48 namespace sd {
49 
50 ViewClipboard::ViewClipboard (::sd::View& rView)
51     : mrView(rView)
52 {
53 }
54 
55 
56 
57 
58 ViewClipboard::~ViewClipboard (void)
59 {
60 }
61 
62 
63 
64 
65 void ViewClipboard::HandlePageDrop (const SdTransferable& rTransferable)
66 {
67     // Determine whether to insert the given set of slides or to assign a
68     // given master page.
69     SdPage* pMasterPage = GetFirstMasterPage (rTransferable);
70     if (pMasterPage != NULL)
71         AssignMasterPage (rTransferable, pMasterPage);
72     else
73         InsertSlides (rTransferable, DetermineInsertPosition (rTransferable));
74 }
75 
76 
77 
78 
79 SdPage* ViewClipboard::GetFirstMasterPage (const SdTransferable& rTransferable)
80 {
81     SdPage* pFirstMasterPage = NULL;
82 
83     if (rTransferable.HasPageBookmarks())
84     {
85         do
86         {
87             const List* pBookmarks = &rTransferable.GetPageBookmarks();
88             if (pBookmarks == NULL)
89                 break;
90 
91             DrawDocShell* pDocShell = rTransferable.GetPageDocShell();
92             if (pDocShell == NULL)
93                 break;
94 
95             SdDrawDocument* pDocument = pDocShell->GetDoc();
96             if (pDocument == NULL)
97                 break;
98 
99             if (pBookmarks->Count() <= 0)
100                 break;
101 
102             int nBookmarkCount = pBookmarks->Count();
103             for (int nIndex=0; nIndex<nBookmarkCount; nIndex++)
104             {
105                 String sName (*(String*) pBookmarks->GetObject(nIndex));
106                 sal_Bool bIsMasterPage;
107 
108                 // SdPage* GetMasterSdPage(sal_uInt16 nPgNum, PageKind ePgKind);
109                 // sal_uInt16 GetMasterSdPageCount(PageKind ePgKind) const;
110 
111                 sal_uInt16 nBMPage = pDocument->GetPageByName (
112                     sName, bIsMasterPage);
113                 if ( ! bIsMasterPage)
114                 {
115                     // At least one regular slide: return NULL to indicate
116                     // that not all bookmarks point to master pages.
117                     pFirstMasterPage = NULL;
118                     break;
119                 }
120                 else if (pFirstMasterPage == NULL)
121                 {
122                     // Remember the first master page for later.
123                     if (nBMPage != SDRPAGE_NOTFOUND)
124                         pFirstMasterPage = static_cast<SdPage*>(
125                             pDocument->GetMasterPage(nBMPage));
126                 }
127             }
128         }
129         while (false);
130     }
131 
132     return pFirstMasterPage;
133 }
134 
135 
136 
137 
138 void ViewClipboard::AssignMasterPage (
139     const SdTransferable& rTransferable,
140     SdPage* pMasterPage)
141 {
142     do
143     {
144         if (pMasterPage == NULL)
145             return;
146 
147         // Get the target page to which the master page is assigned.
148         SdrPageView* pPageView = mrView.GetSdrPageView();
149         if (pPageView == NULL)
150             break;
151 
152         SdPage* pPage = static_cast<SdPage*>(pPageView->GetPage());
153         if (pPage == NULL)
154             break;
155 
156         SdDrawDocument* pDocument = mrView.GetDoc();
157         if (pDocument == NULL)
158             break;
159 
160         if ( ! rTransferable.HasPageBookmarks())
161             break;
162 
163         DrawDocShell* pDataDocShell = rTransferable.GetPageDocShell();
164         if (pDataDocShell == NULL)
165             break;
166 
167         SdDrawDocument* pSourceDocument = pDataDocShell->GetDoc();
168         if (pSourceDocument == NULL)
169             break;
170 
171         // We have to remove the layout suffix from the layout name which is
172         // appended again by SetMasterPage() to the given name.  Don't ask.
173         String sLayoutSuffix (RTL_CONSTASCII_STRINGPARAM(SD_LT_SEPARATOR));
174         sLayoutSuffix.Append (SdResId(STR_LAYOUT_OUTLINE));
175         sal_uInt16 nLength = sLayoutSuffix.Len();
176         String sLayoutName (pMasterPage->GetLayoutName());
177         if (String(sLayoutName, sLayoutName.Len()-nLength, nLength).Equals (
178             sLayoutSuffix))
179             sLayoutName = String(sLayoutName, 0, sLayoutName.Len()-nLength);
180 
181         pDocument->SetMasterPage (
182             pPage->GetPageNum() / 2,
183             sLayoutName,
184             pSourceDocument,
185             sal_False, // Exchange the master page of only the target page.
186             sal_False // Keep unused master pages.
187             );
188     }
189     while (false);
190 }
191 
192 
193 
194 
195 sal_uInt16 ViewClipboard::DetermineInsertPosition  (
196     const SdTransferable& )
197 {
198 	SdDrawDocument* pDoc = mrView.GetDoc();
199     sal_uInt16 nPgCnt = pDoc->GetSdPageCount( PK_STANDARD );
200 
201     // Insert position is the behind the last selected page or behind the
202     // last page when the selection is empty.
203     sal_uInt16 nInsertPos = pDoc->GetSdPageCount( PK_STANDARD ) * 2 + 1;
204     for( sal_uInt16 nPage = 0; nPage < nPgCnt; nPage++ )
205     {
206         SdPage* pPage = pDoc->GetSdPage( nPage, PK_STANDARD );
207 
208         if( pPage->IsSelected() )
209             nInsertPos = nPage * 2 + 3;
210     }
211 
212     return nInsertPos;
213 }
214 
215 
216 
217 
218 sal_uInt16 ViewClipboard::InsertSlides (
219     const SdTransferable& rTransferable,
220     sal_uInt16 nInsertPosition)
221 {
222 	SdDrawDocument* pDoc = mrView.GetDoc();
223 
224     sal_uInt16 nInsertPgCnt = 0;
225     sal_Bool bMergeMasterPages = !rTransferable.HasSourceDoc( pDoc );
226 
227     // Prepare the insertion.
228     const List* pBookmarkList;
229     DrawDocShell* pDataDocSh;
230     if (rTransferable.HasPageBookmarks())
231     {
232         // When the transferable contains page bookmarks then the referenced
233         // pages are inserted.
234         pBookmarkList = &rTransferable.GetPageBookmarks();
235         pDataDocSh = rTransferable.GetPageDocShell();
236         nInsertPgCnt = (sal_uInt16)pBookmarkList->Count();
237     }
238     else
239     {
240         // Otherwise all pages of the document of the transferable are
241         // inserted.
242         SfxObjectShell* pShell = rTransferable.GetDocShell();
243         pDataDocSh = (DrawDocShell*) pShell;
244         SdDrawDocument* pDataDoc = pDataDocSh->GetDoc();
245         pBookmarkList = NULL;
246         if (pDataDoc!=NULL && pDataDoc->GetSdPageCount(PK_STANDARD))
247             nInsertPgCnt = pDataDoc->GetSdPageCount(PK_STANDARD);
248     }
249     if (nInsertPgCnt > 0)
250     {
251         const ::vos::OGuard aGuard( Application::GetSolarMutex() );
252         ::sd::Window* pWin = mrView.GetViewShell()->GetActiveWindow();
253         const sal_Bool bWait = pWin && pWin->IsWait();
254 
255         if( bWait )
256             pWin->LeaveWait();
257 
258         pDoc->InsertBookmarkAsPage(
259             const_cast<List*>(pBookmarkList),
260             NULL,
261             sal_False,
262             sal_False,
263             nInsertPosition,
264             (&rTransferable == SD_MOD()->pTransferDrag),
265             pDataDocSh,
266             sal_True,
267             bMergeMasterPages,
268             sal_False);
269 
270         if( bWait )
271             pWin->EnterWait();
272     }
273 
274     return nInsertPgCnt;
275 }
276 
277 
278 } // end of namespace ::sd
279