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 "DocumentHelper.hxx"
21 :
22 : #include "drawdoc.hxx"
23 : #include "DrawDocShell.hxx"
24 : #include "sdpage.hxx"
25 : #include "glob.hxx"
26 : #include "unmovss.hxx"
27 : #include "strings.hrc"
28 : #include "sdresid.hxx"
29 : #include "undoback.hxx"
30 : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
31 : #include <com/sun/star/drawing/XDrawPages.hpp>
32 : #include <com/sun/star/frame/XComponentLoader.hpp>
33 : #include <com/sun/star/container/XIndexAccess.hpp>
34 : #include "stlpool.hxx"
35 : #include <svx/xfillit0.hxx>
36 : #include <tools/diagnose_ex.h>
37 :
38 : using namespace ::com::sun::star;
39 :
40 : namespace sd { namespace sidebar {
41 :
42 0 : SdPage* DocumentHelper::CopyMasterPageToLocalDocument (
43 : SdDrawDocument& rTargetDocument,
44 : SdPage* pMasterPage)
45 : {
46 0 : SdPage* pNewMasterPage = NULL;
47 :
48 : do
49 : {
50 0 : if (pMasterPage == NULL)
51 0 : break;
52 :
53 : // Check the presence of the source document.
54 : SdDrawDocument* pSourceDocument = static_cast<SdDrawDocument*>(
55 0 : pMasterPage->GetModel());
56 0 : if (pSourceDocument == NULL)
57 0 : break;
58 :
59 : // When the given master page already belongs to the target document
60 : // then there is nothing more to do.
61 0 : if (pSourceDocument == &rTargetDocument)
62 : {
63 0 : pNewMasterPage = pMasterPage;
64 0 : break;
65 : }
66 :
67 : // Test if the master pages of both the slide and its notes page are
68 : // present. This is not the case when we are called during the
69 : // creation of the slide master page because then the notes master
70 : // page is not there.
71 0 : sal_uInt16 nSourceMasterPageCount = pSourceDocument->GetMasterPageCount();
72 0 : if (nSourceMasterPageCount%2 == 0)
73 : // There should be 1 handout page + n slide masters + n notes
74 : // masters = 2*n+1. An even value indicates that a new slide
75 : // master but not yet the notes master has been inserted.
76 0 : break;
77 0 : sal_uInt16 nIndex = pMasterPage->GetPageNum();
78 0 : if (nSourceMasterPageCount <= nIndex+1)
79 0 : break;
80 : // Get the slide master page.
81 0 : if (pMasterPage != static_cast<SdPage*>(
82 0 : pSourceDocument->GetMasterPage(nIndex)))
83 0 : break;
84 : // Get the notes master page.
85 : SdPage* pNotesMasterPage = static_cast<SdPage*>(
86 0 : pSourceDocument->GetMasterPage(nIndex+1));
87 0 : if (pNotesMasterPage == NULL)
88 0 : break;
89 :
90 : // Check if a master page with the same name as that of the given
91 : // master page already exists.
92 0 : bool bPageExists (false);
93 0 : sal_uInt16 nMasterPageCount(rTargetDocument.GetMasterSdPageCount(PK_STANDARD));
94 0 : for (sal_uInt16 nMaster=0; nMaster<nMasterPageCount; nMaster++)
95 : {
96 : SdPage* pCandidate = static_cast<SdPage*>(
97 0 : rTargetDocument.GetMasterSdPage (nMaster, PK_STANDARD));
98 0 : if (pMasterPage!=NULL
99 0 : && pCandidate->GetName() == pMasterPage->GetName())
100 : {
101 0 : bPageExists = true;
102 0 : pNewMasterPage = pCandidate;
103 0 : break;
104 : }
105 : }
106 0 : if (bPageExists)
107 0 : break;
108 :
109 : // Create a new slide (and its notes page.)
110 : uno::Reference<drawing::XDrawPagesSupplier> xSlideSupplier (
111 0 : rTargetDocument.getUnoModel(), uno::UNO_QUERY);
112 0 : if ( ! xSlideSupplier.is())
113 0 : break;
114 : uno::Reference<drawing::XDrawPages> xSlides (
115 0 : xSlideSupplier->getDrawPages(), uno::UNO_QUERY);
116 0 : if ( ! xSlides.is())
117 0 : break;
118 0 : xSlides->insertNewByIndex (xSlides->getCount());
119 :
120 : // Set a layout.
121 : SdPage* pSlide = rTargetDocument.GetSdPage(
122 0 : rTargetDocument.GetSdPageCount(PK_STANDARD)-1,
123 0 : PK_STANDARD);
124 0 : if (pSlide == NULL)
125 0 : break;
126 0 : pSlide->SetAutoLayout(AUTOLAYOUT_TITLE, true);
127 :
128 : // Create a copy of the master page and the associated notes
129 : // master page and insert them into our document.
130 0 : pNewMasterPage = AddMasterPage(rTargetDocument, pMasterPage);
131 0 : if (pNewMasterPage==NULL)
132 0 : break;
133 : SdPage* pNewNotesMasterPage
134 0 : = AddMasterPage(rTargetDocument, pNotesMasterPage);
135 0 : if (pNewNotesMasterPage==NULL)
136 0 : break;
137 :
138 : // Make the connection from the new slide to the master page
139 : // (and do the same for the notes page.)
140 : rTargetDocument.SetMasterPage (
141 0 : rTargetDocument.GetSdPageCount(PK_STANDARD)-1,
142 0 : pNewMasterPage->GetName(),
143 : &rTargetDocument,
144 : false, // Connect the new master page with the new slide but
145 : // do not modify other (master) pages.
146 0 : true);
147 : }
148 : while (false);
149 :
150 : // We are not interested in any automatisms for our modified internal
151 : // document.
152 0 : rTargetDocument.SetChanged(false);
153 :
154 0 : return pNewMasterPage;
155 : }
156 :
157 0 : SdPage* DocumentHelper::GetSlideForMasterPage (SdPage* pMasterPage)
158 : {
159 0 : SdPage* pCandidate = NULL;
160 :
161 0 : SdDrawDocument* pDocument = NULL;
162 0 : if (pMasterPage != NULL)
163 0 : pDocument = dynamic_cast<SdDrawDocument*>(pMasterPage->GetModel());
164 :
165 : // Iterate over all pages and check if it references the given master
166 : // page.
167 0 : if (pDocument!=NULL && pDocument->GetSdPageCount(PK_STANDARD) > 0)
168 : {
169 : // In most cases a new slide has just been inserted so start with
170 : // the last page.
171 0 : sal_uInt16 nPageIndex (pDocument->GetSdPageCount(PK_STANDARD)-1);
172 0 : bool bFound (false);
173 0 : while ( ! bFound)
174 : {
175 : pCandidate = pDocument->GetSdPage(
176 : nPageIndex,
177 0 : PK_STANDARD);
178 0 : if (pCandidate != NULL)
179 : {
180 0 : if (static_cast<SdPage*>(&pCandidate->TRG_GetMasterPage())
181 : == pMasterPage)
182 : {
183 0 : bFound = true;
184 0 : break;
185 : }
186 : }
187 :
188 0 : if (nPageIndex == 0)
189 0 : break;
190 : else
191 0 : nPageIndex --;
192 : }
193 :
194 : // If no page was found that refernced the given master page reset
195 : // the pointer that is returned.
196 0 : if ( ! bFound)
197 0 : pCandidate = NULL;
198 : }
199 :
200 0 : return pCandidate;
201 : }
202 :
203 0 : SdPage* DocumentHelper::AddMasterPage (
204 : SdDrawDocument& rTargetDocument,
205 : SdPage* pMasterPage)
206 : {
207 0 : SdPage* pClonedMasterPage = NULL;
208 :
209 0 : if (pMasterPage!=NULL)
210 : {
211 : try
212 : {
213 : // Duplicate the master page.
214 0 : pClonedMasterPage = static_cast<SdPage*>(pMasterPage->Clone());
215 :
216 : // Copy the necessary styles.
217 : SdDrawDocument* pSourceDocument
218 0 : = static_cast<SdDrawDocument*>(pMasterPage->GetModel());
219 0 : if (pSourceDocument != NULL)
220 0 : ProvideStyles (*pSourceDocument, rTargetDocument, pClonedMasterPage);
221 :
222 : // Copy the precious flag.
223 0 : pClonedMasterPage->SetPrecious(pMasterPage->IsPrecious());
224 :
225 : // Now that the styles are available we can insert the cloned
226 : // master page.
227 0 : rTargetDocument.InsertMasterPage (pClonedMasterPage);
228 : }
229 0 : catch(const uno::Exception&)
230 : {
231 0 : pClonedMasterPage = NULL;
232 : DBG_UNHANDLED_EXCEPTION();
233 : }
234 0 : catch(const ::std::exception&)
235 : {
236 0 : pClonedMasterPage = NULL;
237 : OSL_TRACE ("caught general exception");
238 : }
239 0 : catch(...)
240 : {
241 0 : pClonedMasterPage = NULL;
242 : OSL_TRACE ("caught general exception");
243 : }
244 : }
245 :
246 0 : return pClonedMasterPage;
247 : }
248 :
249 0 : void DocumentHelper::ProvideStyles (
250 : SdDrawDocument& rSourceDocument,
251 : SdDrawDocument& rTargetDocument,
252 : SdPage* pPage)
253 : {
254 : // Get the layout name of the given page.
255 0 : OUString sLayoutName (pPage->GetLayoutName());
256 0 : sal_Int32 nIndex = sLayoutName.indexOf(SD_LT_SEPARATOR);
257 0 : if( nIndex != -1 )
258 0 : sLayoutName = sLayoutName.copy(0, nIndex);
259 :
260 : // Copy the style sheet from source to target document.
261 : SdStyleSheetPool* pSourceStyleSheetPool =
262 0 : static_cast<SdStyleSheetPool*>(rSourceDocument.GetStyleSheetPool());
263 : SdStyleSheetPool* pTargetStyleSheetPool =
264 0 : static_cast<SdStyleSheetPool*>(rTargetDocument.GetStyleSheetPool());
265 0 : SdStyleSheetVector aCreatedStyles;
266 : pTargetStyleSheetPool->CopyLayoutSheets (
267 : sLayoutName,
268 : *pSourceStyleSheetPool,
269 0 : aCreatedStyles);
270 :
271 : // Add an undo action for the copied style sheets.
272 0 : if( !aCreatedStyles.empty() )
273 : {
274 0 : ::svl::IUndoManager* pUndoManager = rTargetDocument.GetDocSh()->GetUndoManager();
275 0 : if (pUndoManager != NULL)
276 : {
277 : SdMoveStyleSheetsUndoAction* pMovStyles =
278 : new SdMoveStyleSheetsUndoAction (
279 : &rTargetDocument,
280 : aCreatedStyles,
281 0 : true);
282 0 : pUndoManager->AddUndoAction (pMovStyles);
283 : }
284 0 : }
285 0 : }
286 :
287 0 : void DocumentHelper::AssignMasterPageToPageList (
288 : SdDrawDocument& rTargetDocument,
289 : SdPage* pMasterPage,
290 : const ::boost::shared_ptr<std::vector<SdPage*> >& rpPageList)
291 : {
292 0 : if (pMasterPage == NULL || !pMasterPage->IsMasterPage())
293 0 : return;
294 :
295 : // Make the layout name by stripping ouf the layout postfix from the
296 : // layout name of the given master page.
297 0 : OUString sFullLayoutName(pMasterPage->GetLayoutName());
298 0 : OUString sBaseLayoutName (sFullLayoutName);
299 0 : sal_Int32 nIndex = sBaseLayoutName.indexOf(SD_LT_SEPARATOR);
300 0 : if( nIndex != -1 )
301 0 : sBaseLayoutName = sBaseLayoutName.copy(0, nIndex);
302 :
303 0 : if (rpPageList->empty())
304 0 : return;
305 :
306 : // Create a second list that contains only the valid pointers to
307 : // pages for which an assignment is necessary.
308 0 : ::std::vector<SdPage*>::const_iterator iPage;
309 0 : ::std::vector<SdPage*> aCleanedList;
310 0 : for (iPage=rpPageList->begin(); iPage!=rpPageList->end(); ++iPage)
311 : {
312 : OSL_ASSERT(*iPage!=NULL && (*iPage)->GetModel() == &rTargetDocument);
313 0 : if (*iPage != NULL && (*iPage)->GetLayoutName() != sFullLayoutName)
314 : {
315 0 : aCleanedList.push_back(*iPage);
316 : }
317 : }
318 0 : if (aCleanedList.empty() )
319 0 : return;
320 :
321 0 : ::svl::IUndoManager* pUndoMgr = rTargetDocument.GetDocSh()->GetUndoManager();
322 0 : if( pUndoMgr )
323 0 : pUndoMgr->EnterListAction(SD_RESSTR(STR_UNDO_SET_PRESLAYOUT), OUString());
324 :
325 0 : SdPage* pMasterPageInDocument = ProvideMasterPage(rTargetDocument,pMasterPage,rpPageList);
326 0 : if (pMasterPageInDocument == NULL)
327 0 : return;
328 :
329 : // Assign the master pages to the given list of pages.
330 0 : for (iPage=aCleanedList.begin();
331 0 : iPage!=aCleanedList.end();
332 : ++iPage)
333 : {
334 : AssignMasterPageToPage (
335 : pMasterPageInDocument,
336 : sBaseLayoutName,
337 0 : *iPage);
338 : }
339 :
340 0 : if( pUndoMgr )
341 0 : pUndoMgr->LeaveListAction();
342 : }
343 :
344 0 : SdPage* DocumentHelper::AddMasterPage (
345 : SdDrawDocument& rTargetDocument,
346 : SdPage* pMasterPage,
347 : sal_uInt16 nInsertionIndex)
348 : {
349 0 : SdPage* pClonedMasterPage = NULL;
350 :
351 0 : if (pMasterPage!=NULL)
352 : {
353 : // Duplicate the master page.
354 0 : pClonedMasterPage = static_cast<SdPage*>(pMasterPage->Clone());
355 :
356 : // Copy the precious flag.
357 0 : pClonedMasterPage->SetPrecious(pMasterPage->IsPrecious());
358 :
359 : // Copy the necessary styles.
360 : SdDrawDocument* pSourceDocument
361 0 : = static_cast<SdDrawDocument*>(pMasterPage->GetModel());
362 0 : if (pSourceDocument != NULL)
363 : {
364 0 : ProvideStyles (*pSourceDocument, rTargetDocument, pClonedMasterPage);
365 :
366 : // Now that the styles are available we can insert the cloned
367 : // master page.
368 0 : rTargetDocument.InsertMasterPage (pClonedMasterPage, nInsertionIndex);
369 :
370 : // Adapt the size of the new master page to that of the pages in
371 : // the document.
372 0 : Size aNewSize (rTargetDocument.GetSdPage(0, pMasterPage->GetPageKind())->GetSize());
373 : Rectangle aBorders (
374 0 : pClonedMasterPage->GetLftBorder(),
375 0 : pClonedMasterPage->GetUppBorder(),
376 0 : pClonedMasterPage->GetRgtBorder(),
377 0 : pClonedMasterPage->GetLwrBorder());
378 0 : pClonedMasterPage->ScaleObjects(aNewSize, aBorders, true);
379 0 : pClonedMasterPage->SetSize(aNewSize);
380 0 : pClonedMasterPage->CreateTitleAndLayout(true);
381 : }
382 : }
383 :
384 0 : return pClonedMasterPage;
385 : }
386 :
387 : /** In here we have to handle three cases:
388 : 1. pPage is a normal slide. We can use SetMasterPage to assign the
389 : master pages to it.
390 : 2. pPage is a master page that is used by at least one slide. We can
391 : assign the master page to these slides.
392 : 3. pPage is a master page that is currently not used by any slide.
393 : We can delete that page and add copies of the given master pages
394 : instead.
395 :
396 : For points 2 and 3 where one master page A is assigned to another B we have
397 : to keep in mind that the master page that page A has already been
398 : inserted into the target document.
399 : */
400 0 : void DocumentHelper::AssignMasterPageToPage (
401 : SdPage* pMasterPage,
402 : const OUString& rsBaseLayoutName,
403 : SdPage* pPage)
404 : {
405 : // Leave early when the parameters are invalid.
406 0 : if (pPage == NULL || pMasterPage == NULL)
407 0 : return;
408 0 : SdDrawDocument* pDocument = dynamic_cast<SdDrawDocument*>(pPage->GetModel());
409 0 : if (pDocument == NULL)
410 0 : return;
411 :
412 0 : if ( ! pPage->IsMasterPage())
413 : {
414 : // 1. Remove the background object (so that that, if it exists, does
415 : // not override the new master page) and assign the master page to
416 : // the regular slide.
417 0 : pDocument->GetDocSh()->GetUndoManager()->AddUndoAction(
418 : new SdBackgroundObjUndoAction(
419 0 : *pDocument, *pPage, pPage->getSdrPageProperties().GetItemSet()),
420 0 : true);
421 0 : pPage->getSdrPageProperties().PutItem(XFillStyleItem(drawing::FillStyle_NONE));
422 :
423 : pDocument->SetMasterPage (
424 0 : (pPage->GetPageNum()-1)/2,
425 : rsBaseLayoutName,
426 : pDocument,
427 : false,
428 0 : false);
429 : }
430 : else
431 : {
432 : // Find first slide that uses the master page.
433 0 : SdPage* pSlide = NULL;
434 0 : sal_uInt16 nPageCount = pDocument->GetSdPageCount(PK_STANDARD);
435 0 : for (sal_uInt16 nPage=0; nPage<nPageCount&&pSlide==NULL; nPage++)
436 : {
437 0 : SdrPage* pCandidate = pDocument->GetSdPage(nPage,PK_STANDARD);
438 0 : if (pCandidate != NULL
439 0 : && pCandidate->TRG_HasMasterPage()
440 0 : && &(pCandidate->TRG_GetMasterPage()) == pPage)
441 : {
442 0 : pSlide = static_cast<SdPage*>(pCandidate);
443 : }
444 : }
445 :
446 0 : if (pSlide != NULL)
447 : {
448 : // 2. Assign the given master pages to the first slide that was
449 : // found above that uses the master page.
450 : pDocument->SetMasterPage (
451 0 : (pSlide->GetPageNum()-1)/2,
452 : rsBaseLayoutName,
453 : pDocument,
454 : false,
455 0 : false);
456 : }
457 : else
458 : {
459 : // 3. Replace the master page A by a copy of the given master
460 : // page B.
461 : pDocument->RemoveUnnecessaryMasterPages (
462 0 : pPage, false);
463 : }
464 : }
465 : }
466 :
467 0 : SdPage* DocumentHelper::ProvideMasterPage (
468 : SdDrawDocument& rTargetDocument,
469 : SdPage* pMasterPage,
470 : const ::boost::shared_ptr<std::vector<SdPage*> >& rpPageList)
471 : {
472 : // Make sure that both the master page and its notes master exist
473 : // in the source document. If one is missing then return without
474 : // making any changes.
475 0 : if (pMasterPage == NULL)
476 : {
477 : // The caller should make sure that the master page is valid.
478 : OSL_ASSERT(pMasterPage != NULL);
479 0 : return NULL;
480 : }
481 0 : SdDrawDocument* pSourceDocument = static_cast<SdDrawDocument*>(pMasterPage->GetModel());
482 0 : if (pSourceDocument == NULL)
483 0 : return NULL;
484 : SdPage* pNotesMasterPage = static_cast<SdPage*>(
485 0 : pSourceDocument->GetMasterPage(pMasterPage->GetPageNum()+1));
486 0 : if (pNotesMasterPage == NULL)
487 : {
488 : // The model is not in a valid state. Maybe a new master page
489 : // is being (not finished yet) created? Return without making
490 : // any changes.
491 0 : return NULL;
492 : }
493 :
494 0 : SdPage* pMasterPageInDocument = NULL;
495 : // Search for a master page with the same name as the given one in
496 : // the target document.
497 0 : const OUString sMasterPageLayoutName (pMasterPage->GetLayoutName());
498 0 : for (sal_uInt16 nIndex=0,nCount=rTargetDocument.GetMasterPageCount(); nIndex<nCount; ++nIndex)
499 : {
500 0 : SdPage* pCandidate = static_cast<SdPage*>(rTargetDocument.GetMasterPage(nIndex));
501 0 : if (pCandidate && sMasterPageLayoutName.equals(pCandidate->GetLayoutName()))
502 : {
503 : // The requested master page does already exist in the
504 : // target document, return it.
505 0 : return pCandidate;
506 : }
507 : }
508 :
509 : // The given master page does not already belong to the target
510 : // document so we have to create copies and insert them into the
511 : // targer document.
512 :
513 : // Determine the position where the new master pages are inserted.
514 : // By default they are inserted at the end. When we assign to a
515 : // master page then insert after the last of the (selected) pages.
516 0 : sal_uInt16 nInsertionIndex = rTargetDocument.GetMasterPageCount();
517 0 : if (rpPageList->front()->IsMasterPage())
518 : {
519 0 : nInsertionIndex = rpPageList->back()->GetPageNum();
520 : }
521 :
522 : // Clone the master page.
523 0 : if (pMasterPage->GetModel() != &rTargetDocument)
524 : {
525 0 : pMasterPageInDocument = AddMasterPage (rTargetDocument, pMasterPage, nInsertionIndex);
526 0 : if( rTargetDocument.IsUndoEnabled() )
527 : rTargetDocument.AddUndo(
528 0 : rTargetDocument.GetSdrUndoFactory().CreateUndoNewPage(*pMasterPageInDocument));
529 : }
530 : else
531 0 : pMasterPageInDocument = pMasterPage;
532 :
533 : // Clone the notes master.
534 0 : if (pNotesMasterPage->GetModel() != &rTargetDocument)
535 : {
536 : SdPage* pClonedNotesMasterPage
537 0 : = AddMasterPage (rTargetDocument, pNotesMasterPage, nInsertionIndex+1);
538 0 : if( rTargetDocument.IsUndoEnabled() )
539 : rTargetDocument.AddUndo(
540 0 : rTargetDocument.GetSdrUndoFactory().CreateUndoNewPage(*pClonedNotesMasterPage));
541 : }
542 :
543 0 : return pMasterPageInDocument;
544 : }
545 :
546 114 : } } // end of namespace sd::sidebar
547 :
548 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|