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