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().CompareTo(pMasterPage->GetName())==0)
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 (sal_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 : String sLayoutName (pPage->GetLayoutName());
266 0 : sLayoutName.Erase (sLayoutName.SearchAscii (SD_LT_SEPARATOR));
267 :
268 : // Copy the style sheet from source to target document.
269 : SdStyleSheetPool* pSourceStyleSheetPool =
270 0 : static_cast<SdStyleSheetPool*>(rSourceDocument.GetStyleSheetPool());
271 : SdStyleSheetPool* pTargetStyleSheetPool =
272 0 : static_cast<SdStyleSheetPool*>(rTargetDocument.GetStyleSheetPool());
273 0 : SdStyleSheetVector aCreatedStyles;
274 : pTargetStyleSheetPool->CopyLayoutSheets (
275 : sLayoutName,
276 : *pSourceStyleSheetPool,
277 0 : aCreatedStyles);
278 :
279 : // Add an undo action for the copied style sheets.
280 0 : if( !aCreatedStyles.empty() )
281 : {
282 0 : ::svl::IUndoManager* pUndoManager = rTargetDocument.GetDocSh()->GetUndoManager();
283 0 : if (pUndoManager != NULL)
284 : {
285 : SdMoveStyleSheetsUndoAction* pMovStyles =
286 : new SdMoveStyleSheetsUndoAction (
287 : &rTargetDocument,
288 : aCreatedStyles,
289 0 : sal_True);
290 0 : pUndoManager->AddUndoAction (pMovStyles);
291 : }
292 0 : }
293 0 : }
294 :
295 :
296 :
297 :
298 0 : void DocumentHelper::AssignMasterPageToPageList (
299 : SdDrawDocument& rTargetDocument,
300 : SdPage* pMasterPage,
301 : const ::boost::shared_ptr<std::vector<SdPage*> >& rpPageList)
302 : {
303 0 : if (pMasterPage == NULL || !pMasterPage->IsMasterPage())
304 0 : return;
305 :
306 : // Make the layout name by stripping ouf the layout postfix from the
307 : // layout name of the given master page.
308 0 : OUString sFullLayoutName(pMasterPage->GetLayoutName());
309 0 : String sBaseLayoutName (sFullLayoutName);
310 0 : sBaseLayoutName.Erase (sBaseLayoutName.SearchAscii (SD_LT_SEPARATOR));
311 :
312 0 : if (rpPageList->empty())
313 0 : return;
314 :
315 : // Create a second list that contains only the valid pointers to
316 : // pages for which an assignment is necessary.
317 0 : ::std::vector<SdPage*>::const_iterator iPage;
318 0 : ::std::vector<SdPage*> aCleanedList;
319 0 : for (iPage=rpPageList->begin(); iPage!=rpPageList->end(); ++iPage)
320 : {
321 : OSL_ASSERT(*iPage!=NULL && (*iPage)->GetModel() == &rTargetDocument);
322 0 : if (*iPage != NULL && (*iPage)->GetLayoutName() != sFullLayoutName)
323 : {
324 0 : aCleanedList.push_back(*iPage);
325 : }
326 : }
327 0 : if (aCleanedList.empty() )
328 0 : return;
329 :
330 0 : ::svl::IUndoManager* pUndoMgr = rTargetDocument.GetDocSh()->GetUndoManager();
331 0 : if( pUndoMgr )
332 0 : pUndoMgr->EnterListAction(String(SdResId(STR_UNDO_SET_PRESLAYOUT)), String());
333 :
334 0 : SdPage* pMasterPageInDocument = ProvideMasterPage(rTargetDocument,pMasterPage,rpPageList);
335 0 : if (pMasterPageInDocument == NULL)
336 0 : return;
337 :
338 : // Assign the master pages to the given list of pages.
339 0 : for (iPage=aCleanedList.begin();
340 0 : iPage!=aCleanedList.end();
341 : ++iPage)
342 : {
343 : AssignMasterPageToPage (
344 : pMasterPageInDocument,
345 : sBaseLayoutName,
346 0 : *iPage);
347 : }
348 :
349 0 : if( pUndoMgr )
350 0 : pUndoMgr->LeaveListAction();
351 : }
352 :
353 :
354 :
355 :
356 0 : SdPage* DocumentHelper::AddMasterPage (
357 : SdDrawDocument& rTargetDocument,
358 : SdPage* pMasterPage,
359 : sal_uInt16 nInsertionIndex)
360 : {
361 0 : SdPage* pClonedMasterPage = NULL;
362 :
363 0 : if (pMasterPage!=NULL)
364 : {
365 : // Duplicate the master page.
366 0 : pClonedMasterPage = static_cast<SdPage*>(pMasterPage->Clone());
367 :
368 : // Copy the precious flag.
369 0 : pClonedMasterPage->SetPrecious(pMasterPage->IsPrecious());
370 :
371 : // Copy the necessary styles.
372 : SdDrawDocument* pSourceDocument
373 0 : = static_cast<SdDrawDocument*>(pMasterPage->GetModel());
374 0 : if (pSourceDocument != NULL)
375 : {
376 0 : ProvideStyles (*pSourceDocument, rTargetDocument, pClonedMasterPage);
377 :
378 : // Now that the styles are available we can insert the cloned
379 : // master page.
380 0 : rTargetDocument.InsertMasterPage (pClonedMasterPage, nInsertionIndex);
381 :
382 : // Adapt the size of the new master page to that of the pages in
383 : // the document.
384 0 : Size aNewSize (rTargetDocument.GetSdPage(0, pMasterPage->GetPageKind())->GetSize());
385 : Rectangle aBorders (
386 0 : pClonedMasterPage->GetLftBorder(),
387 0 : pClonedMasterPage->GetUppBorder(),
388 0 : pClonedMasterPage->GetRgtBorder(),
389 0 : pClonedMasterPage->GetLwrBorder());
390 0 : pClonedMasterPage->ScaleObjects(aNewSize, aBorders, sal_True);
391 0 : pClonedMasterPage->SetSize(aNewSize);
392 0 : pClonedMasterPage->CreateTitleAndLayout(sal_True);
393 : }
394 : }
395 :
396 0 : return pClonedMasterPage;
397 : }
398 :
399 :
400 :
401 :
402 : /** In here we have to handle three cases:
403 : 1. pPage is a normal slide. We can use SetMasterPage to assign the
404 : master pages to it.
405 : 2. pPage is a master page that is used by at least one slide. We can
406 : assign the master page to these slides.
407 : 3. pPage is a master page that is currently not used by any slide.
408 : We can delete that page and add copies of the given master pages
409 : instead.
410 :
411 : For points 2 and 3 where one master page A is assigned to another B we have
412 : to keep in mind that the master page that page A has already been
413 : inserted into the target document.
414 : */
415 0 : void DocumentHelper::AssignMasterPageToPage (
416 : SdPage* pMasterPage,
417 : const String& rsBaseLayoutName,
418 : SdPage* pPage)
419 : {
420 : // Leave early when the parameters are invalid.
421 0 : if (pPage == NULL || pMasterPage == NULL)
422 0 : return;
423 0 : SdDrawDocument* pDocument = dynamic_cast<SdDrawDocument*>(pPage->GetModel());
424 0 : if (pDocument == NULL)
425 0 : return;
426 :
427 0 : if ( ! pPage->IsMasterPage())
428 : {
429 : // 1. Remove the background object (so that that, if it exists, does
430 : // not override the new master page) and assign the master page to
431 : // the regular slide.
432 0 : pDocument->GetDocSh()->GetUndoManager()->AddUndoAction(
433 : new SdBackgroundObjUndoAction(
434 0 : *pDocument, *pPage, pPage->getSdrPageProperties().GetItemSet()),
435 0 : sal_True);
436 0 : pPage->getSdrPageProperties().PutItem(XFillStyleItem(XFILL_NONE));
437 :
438 : pDocument->SetMasterPage (
439 0 : (pPage->GetPageNum()-1)/2,
440 : rsBaseLayoutName,
441 : pDocument,
442 : sal_False,
443 0 : sal_False);
444 : }
445 : else
446 : {
447 : // Find first slide that uses the master page.
448 0 : SdPage* pSlide = NULL;
449 0 : sal_uInt16 nPageCount = pDocument->GetSdPageCount(PK_STANDARD);
450 0 : for (sal_uInt16 nPage=0; nPage<nPageCount&&pSlide==NULL; nPage++)
451 : {
452 0 : SdrPage* pCandidate = pDocument->GetSdPage(nPage,PK_STANDARD);
453 0 : if (pCandidate != NULL
454 0 : && pCandidate->TRG_HasMasterPage()
455 0 : && &(pCandidate->TRG_GetMasterPage()) == pPage)
456 : {
457 0 : pSlide = static_cast<SdPage*>(pCandidate);
458 : }
459 : }
460 :
461 0 : if (pSlide != NULL)
462 : {
463 : // 2. Assign the given master pages to the first slide that was
464 : // found above that uses the master page.
465 : pDocument->SetMasterPage (
466 0 : (pSlide->GetPageNum()-1)/2,
467 : rsBaseLayoutName,
468 : pDocument,
469 : sal_False,
470 0 : sal_False);
471 : }
472 : else
473 : {
474 : // 3. Replace the master page A by a copy of the given master
475 : // page B.
476 : pDocument->RemoveUnnecessaryMasterPages (
477 0 : pPage, sal_False);
478 : }
479 : }
480 : }
481 :
482 :
483 :
484 :
485 0 : SdPage* DocumentHelper::ProvideMasterPage (
486 : SdDrawDocument& rTargetDocument,
487 : SdPage* pMasterPage,
488 : const ::boost::shared_ptr<std::vector<SdPage*> >& rpPageList)
489 : {
490 : // Make sure that both the master page and its notes master exist
491 : // in the source document. If one is missing then return without
492 : // making any changes.
493 0 : if (pMasterPage == NULL)
494 : {
495 : // The caller should make sure that the master page is valid.
496 : OSL_ASSERT(pMasterPage != NULL);
497 0 : return NULL;
498 : }
499 0 : SdDrawDocument* pSourceDocument = static_cast<SdDrawDocument*>(pMasterPage->GetModel());
500 0 : if (pSourceDocument == NULL)
501 0 : return NULL;
502 : SdPage* pNotesMasterPage = static_cast<SdPage*>(
503 0 : pSourceDocument->GetMasterPage(pMasterPage->GetPageNum()+1));
504 0 : if (pNotesMasterPage == NULL)
505 : {
506 : // The model is not in a valid state. Maybe a new master page
507 : // is being (not finished yet) created? Return without making
508 : // any changes.
509 0 : return NULL;
510 : }
511 :
512 0 : SdPage* pMasterPageInDocument = NULL;
513 : // Search for a master page with the same name as the given one in
514 : // the target document.
515 0 : const OUString sMasterPageLayoutName (pMasterPage->GetLayoutName());
516 0 : for (sal_uInt16 nIndex=0,nCount=rTargetDocument.GetMasterPageCount(); nIndex<nCount; ++nIndex)
517 : {
518 0 : SdPage* pCandidate = static_cast<SdPage*>(rTargetDocument.GetMasterPage(nIndex));
519 0 : if (pCandidate && sMasterPageLayoutName.equals(pCandidate->GetLayoutName()))
520 : {
521 : // The requested master page does already exist in the
522 : // target document, return it.
523 0 : return pCandidate;
524 : }
525 : }
526 :
527 : // The given master page does not already belong to the target
528 : // document so we have to create copies and insert them into the
529 : // targer document.
530 :
531 : // Determine the position where the new master pages are inserted.
532 : // By default they are inserted at the end. When we assign to a
533 : // master page then insert after the last of the (selected) pages.
534 0 : sal_uInt16 nInsertionIndex = rTargetDocument.GetMasterPageCount();
535 0 : if (rpPageList->front()->IsMasterPage())
536 : {
537 0 : nInsertionIndex = rpPageList->back()->GetPageNum();
538 : }
539 :
540 : // Clone the master page.
541 0 : if (pMasterPage->GetModel() != &rTargetDocument)
542 : {
543 0 : pMasterPageInDocument = AddMasterPage (rTargetDocument, pMasterPage, nInsertionIndex);
544 0 : if( rTargetDocument.IsUndoEnabled() )
545 : rTargetDocument.AddUndo(
546 0 : rTargetDocument.GetSdrUndoFactory().CreateUndoNewPage(*pMasterPageInDocument));
547 : }
548 : else
549 0 : pMasterPageInDocument = pMasterPage;
550 :
551 : // Clone the notes master.
552 0 : if (pNotesMasterPage->GetModel() != &rTargetDocument)
553 : {
554 : SdPage* pClonedNotesMasterPage
555 0 : = AddMasterPage (rTargetDocument, pNotesMasterPage, nInsertionIndex+1);
556 0 : if( rTargetDocument.IsUndoEnabled() )
557 : rTargetDocument.AddUndo(
558 0 : rTargetDocument.GetSdrUndoFactory().CreateUndoNewPage(*pClonedNotesMasterPage));
559 : }
560 :
561 0 : return pMasterPageInDocument;
562 : }
563 :
564 :
565 :
566 :
567 :
568 33 : } } // end of namespace sd::sidebar
569 :
570 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|