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