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 "MasterPageContainer.hxx"
22 :
23 : #include "MasterPageDescriptor.hxx"
24 : #include "MasterPageContainerFiller.hxx"
25 : #include "MasterPageContainerQueue.hxx"
26 : #include "TemplateScanner.hxx"
27 : #include "tools/AsynchronousTask.hxx"
28 : #include "strings.hrc"
29 : #include <algorithm>
30 : #include <list>
31 : #include <set>
32 :
33 : #include "unomodel.hxx"
34 : #include <com/sun/star/frame/XComponentLoader.hpp>
35 : #include <com/sun/star/io/XStream.hpp>
36 : #include <com/sun/star/io/XInputStream.hpp>
37 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
38 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 : #include <com/sun/star/uno/Reference.hxx>
40 : #include <com/sun/star/uno/Any.hxx>
41 : #include <com/sun/star/uno/Sequence.hxx>
42 : #include <com/sun/star/util/XCloseable.hpp>
43 : #include <comphelper/processfactory.hxx>
44 : #include <sfx2/app.hxx>
45 : #include <svx/svdpage.hxx>
46 : #include "DrawDocShell.hxx"
47 : #include "drawdoc.hxx"
48 : #include "sdpage.hxx"
49 : #include <svl/itemset.hxx>
50 : #include <svl/eitem.hxx>
51 : #include "sdresid.hxx"
52 : #include "tools/TimerBasedTaskExecution.hxx"
53 : #include "pres.hxx"
54 : #include <osl/mutex.hxx>
55 : #include <boost/weak_ptr.hpp>
56 :
57 : using namespace ::com::sun::star;
58 : using namespace ::com::sun::star::uno;
59 : using namespace ::sd::toolpanel::controls;
60 :
61 : namespace {
62 :
63 : typedef ::std::vector<SharedMasterPageDescriptor> MasterPageContainerType;
64 :
65 : } // end of anonymous namespace
66 :
67 :
68 : namespace sd { namespace toolpanel { namespace controls {
69 :
70 :
71 : /** Inner implementation class of the MasterPageContainer.
72 : */
73 : class MasterPageContainer::Implementation
74 : : public SdGlobalResource,
75 : public MasterPageContainerFiller::ContainerAdapter,
76 : public MasterPageContainerQueue::ContainerAdapter
77 : {
78 : public:
79 : mutable ::osl::Mutex maMutex;
80 :
81 : static ::boost::weak_ptr<Implementation> mpInstance;
82 : MasterPageContainerType maContainer;
83 :
84 : static ::boost::shared_ptr<Implementation> Instance (void);
85 :
86 : void LateInit (void);
87 : void AddChangeListener (const Link& rLink);
88 : void RemoveChangeListener (const Link& rLink);
89 : void UpdatePreviewSizePixel (void);
90 : Size GetPreviewSizePixel (PreviewSize eSize) const;
91 :
92 : bool HasToken (Token aToken) const;
93 : const SharedMasterPageDescriptor GetDescriptor (MasterPageContainer::Token aToken) const;
94 : SharedMasterPageDescriptor GetDescriptor (MasterPageContainer::Token aToken);
95 : virtual Token PutMasterPage (const SharedMasterPageDescriptor& rDescriptor);
96 : void InvalidatePreview (Token aToken);
97 : Image GetPreviewForToken (
98 : Token aToken,
99 : PreviewSize ePreviewSize);
100 : PreviewState GetPreviewState (Token aToken) const;
101 : bool RequestPreview (Token aToken);
102 :
103 : Reference<frame::XModel> GetModel (void);
104 : SdDrawDocument* GetDocument (void);
105 :
106 : void FireContainerChange (
107 : MasterPageContainerChangeEvent::EventType eType,
108 : Token aToken,
109 : bool bNotifyAsynchronously = false);
110 :
111 : virtual bool UpdateDescriptor (
112 : const SharedMasterPageDescriptor& rpDescriptor,
113 : bool bForcePageObject,
114 : bool bForcePreview,
115 : bool bSendEvents);
116 :
117 : void ReleaseDescriptor (Token aToken);
118 :
119 : /** Called by the MasterPageContainerFiller to notify that all master
120 : pages from template documents have been added.
121 : */
122 : virtual void FillingDone (void);
123 :
124 : private:
125 : Implementation (void);
126 : virtual ~Implementation (void);
127 :
128 : class Deleter { public:
129 0 : void operator() (Implementation* pObject) { delete pObject; }
130 : };
131 : friend class Deleter;
132 :
133 : enum InitializationState { NOT_INITIALIZED, INITIALIZING, INITIALIZED } meInitializationState;
134 :
135 : ::boost::scoped_ptr<MasterPageContainerQueue> mpRequestQueue;
136 : ::com::sun::star::uno::Reference<com::sun::star::frame::XModel> mxModel;
137 : SdDrawDocument* mpDocument;
138 : PreviewRenderer maPreviewRenderer;
139 : /** Remember whether the first page object has already been used to
140 : determine the correct size ratio.
141 : */
142 : bool mbFirstPageObjectSeen;
143 :
144 : // The widths for the previews contain two pixels for the border that is
145 : // painted arround the preview.
146 : static const int SMALL_PREVIEW_WIDTH = 72 + 2;
147 : static const int LARGE_PREVIEW_WIDTH = 2*72 + 2;
148 :
149 : /** This substition of page preview shows "Preparing preview" and is
150 : shown as long as the actual previews are not being present.
151 : */
152 : Image maLargePreviewBeingCreated;
153 : Image maSmallPreviewBeingCreated;
154 :
155 : /** This substition of page preview is shown when a preview can not be
156 : created and thus is not available.
157 : */
158 : Image maLargePreviewNotAvailable;
159 : Image maSmallPreviewNotAvailable;
160 :
161 : ::std::vector<Link> maChangeListeners;
162 :
163 : // We have to remember the tasks for initialization and filling in case
164 : // a MasterPageContainer object is destroyed before these tasks have
165 : // been completed.
166 : ::boost::weak_ptr<sd::tools::TimerBasedTaskExecution> mpFillerTask;
167 :
168 : Size maSmallPreviewSizePixel;
169 : Size maLargePreviewSizePixel;
170 : bool mbPageRatioKnown;
171 :
172 : bool mbContainerCleaningPending;
173 :
174 : typedef ::std::pair<MasterPageContainerChangeEvent::EventType,Token> EventData;
175 : DECL_LINK(AsynchronousNotifyCallback, EventData*);
176 : ::sd::DrawDocShell* LoadDocument (
177 : const String& sFileName,
178 : SfxObjectShellLock& rxDocumentShell);
179 :
180 : Image GetPreviewSubstitution (sal_uInt16 nId, PreviewSize ePreviewSize);
181 :
182 : void CleanContainer (void);
183 : };
184 :
185 :
186 :
187 :
188 : //===== MasterPageContainer ===================================================
189 :
190 : ::boost::weak_ptr<MasterPageContainer::Implementation>
191 6 : MasterPageContainer::Implementation::mpInstance;
192 : static const MasterPageContainer::Token NIL_TOKEN (-1);
193 :
194 :
195 :
196 :
197 : ::boost::shared_ptr<MasterPageContainer::Implementation>
198 0 : MasterPageContainer::Implementation::Instance (void)
199 : {
200 0 : ::boost::shared_ptr<MasterPageContainer::Implementation> pInstance;
201 :
202 0 : if (Implementation::mpInstance.expired())
203 : {
204 : ::osl::GetGlobalMutex aMutexFunctor;
205 0 : ::osl::MutexGuard aGuard (aMutexFunctor());
206 0 : if (Implementation::mpInstance.expired())
207 : {
208 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
209 : pInstance = ::boost::shared_ptr<MasterPageContainer::Implementation>(
210 0 : new MasterPageContainer::Implementation(),
211 0 : MasterPageContainer::Implementation::Deleter());
212 0 : SdGlobalResourceContainer::Instance().AddResource(pInstance);
213 0 : Implementation::mpInstance = pInstance;
214 : }
215 : else
216 : pInstance = ::boost::shared_ptr<MasterPageContainer::Implementation>(
217 0 : Implementation::mpInstance);
218 : }
219 : else
220 : {
221 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
222 : pInstance = ::boost::shared_ptr<MasterPageContainer::Implementation>(
223 0 : Implementation::mpInstance);
224 : }
225 :
226 : DBG_ASSERT (pInstance.get()!=NULL,
227 : "MasterPageContainer::Implementation::Instance(): instance is NULL");
228 0 : return pInstance;
229 : }
230 :
231 :
232 :
233 :
234 0 : MasterPageContainer::MasterPageContainer (void)
235 : : mpImpl(Implementation::Instance()),
236 0 : mePreviewSize(SMALL)
237 : {
238 0 : mpImpl->LateInit();
239 0 : }
240 :
241 :
242 :
243 :
244 0 : MasterPageContainer::~MasterPageContainer (void)
245 : {
246 0 : }
247 :
248 :
249 :
250 :
251 0 : void MasterPageContainer::AddChangeListener (const Link& rLink)
252 : {
253 0 : mpImpl->AddChangeListener(rLink);
254 0 : }
255 :
256 :
257 :
258 :
259 0 : void MasterPageContainer::RemoveChangeListener (const Link& rLink)
260 : {
261 0 : mpImpl->RemoveChangeListener(rLink);
262 0 : }
263 :
264 :
265 :
266 :
267 0 : void MasterPageContainer::SetPreviewSize (PreviewSize eSize)
268 : {
269 0 : mePreviewSize = eSize;
270 : mpImpl->FireContainerChange(
271 : MasterPageContainerChangeEvent::SIZE_CHANGED,
272 0 : NIL_TOKEN);
273 0 : }
274 :
275 :
276 :
277 :
278 0 : MasterPageContainer::PreviewSize MasterPageContainer::GetPreviewSize (void) const
279 : {
280 0 : return mePreviewSize;
281 : }
282 :
283 :
284 :
285 :
286 0 : Size MasterPageContainer::GetPreviewSizePixel (void) const
287 : {
288 0 : return mpImpl->GetPreviewSizePixel(mePreviewSize);
289 : }
290 :
291 :
292 :
293 :
294 0 : MasterPageContainer::Token MasterPageContainer::PutMasterPage (
295 : const SharedMasterPageDescriptor& rDescriptor)
296 : {
297 0 : return mpImpl->PutMasterPage(rDescriptor);
298 : }
299 :
300 :
301 :
302 :
303 0 : void MasterPageContainer::AcquireToken (Token aToken)
304 : {
305 0 : SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
306 0 : if (pDescriptor.get() != NULL)
307 : {
308 0 : ++pDescriptor->mnUseCount;
309 0 : }
310 0 : }
311 :
312 :
313 :
314 :
315 0 : void MasterPageContainer::ReleaseToken (Token aToken)
316 : {
317 0 : SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
318 0 : if (pDescriptor.get() != NULL)
319 : {
320 : OSL_ASSERT(pDescriptor->mnUseCount>0);
321 0 : --pDescriptor->mnUseCount;
322 0 : if (pDescriptor->mnUseCount <= 0)
323 : {
324 0 : switch (pDescriptor->meOrigin)
325 : {
326 : case DEFAULT:
327 : case TEMPLATE:
328 : default:
329 0 : break;
330 :
331 : case MASTERPAGE:
332 0 : mpImpl->ReleaseDescriptor(aToken);
333 0 : break;
334 : }
335 : }
336 0 : }
337 0 : }
338 :
339 :
340 :
341 :
342 0 : int MasterPageContainer::GetTokenCount (void) const
343 : {
344 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
345 :
346 0 : return mpImpl->maContainer.size();
347 : }
348 :
349 :
350 :
351 :
352 0 : bool MasterPageContainer::HasToken (Token aToken) const
353 : {
354 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
355 :
356 0 : return mpImpl->HasToken(aToken);
357 : }
358 :
359 :
360 :
361 :
362 0 : MasterPageContainer::Token MasterPageContainer::GetTokenForIndex (int nIndex)
363 : {
364 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
365 :
366 0 : Token aResult (NIL_TOKEN);
367 0 : if (HasToken(nIndex))
368 0 : aResult = mpImpl->maContainer[nIndex]->maToken;
369 0 : return aResult;
370 : }
371 :
372 :
373 :
374 :
375 0 : MasterPageContainer::Token MasterPageContainer::GetTokenForURL (
376 : const String& sURL)
377 : {
378 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
379 :
380 0 : Token aResult (NIL_TOKEN);
381 0 : if (sURL.Len() > 0)
382 : {
383 : MasterPageContainerType::iterator iEntry (
384 : ::std::find_if (
385 0 : mpImpl->maContainer.begin(),
386 0 : mpImpl->maContainer.end(),
387 0 : MasterPageDescriptor::URLComparator(sURL)));
388 0 : if (iEntry != mpImpl->maContainer.end())
389 0 : aResult = (*iEntry)->maToken;
390 : }
391 0 : return aResult;
392 : }
393 :
394 :
395 :
396 :
397 0 : MasterPageContainer::Token MasterPageContainer::GetTokenForStyleName (const String& sStyleName)
398 : {
399 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
400 :
401 0 : Token aResult (NIL_TOKEN);
402 0 : if (sStyleName.Len() > 0)
403 : {
404 : MasterPageContainerType::iterator iEntry (
405 : ::std::find_if (
406 0 : mpImpl->maContainer.begin(),
407 0 : mpImpl->maContainer.end(),
408 0 : MasterPageDescriptor::StyleNameComparator(sStyleName)));
409 0 : if (iEntry != mpImpl->maContainer.end())
410 0 : aResult = (*iEntry)->maToken;
411 : }
412 0 : return aResult;
413 : }
414 :
415 :
416 :
417 :
418 0 : MasterPageContainer::Token MasterPageContainer::GetTokenForPageObject (
419 : const SdPage* pPage)
420 : {
421 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
422 :
423 0 : Token aResult (NIL_TOKEN);
424 0 : if (pPage != NULL)
425 : {
426 : MasterPageContainerType::iterator iEntry (
427 : ::std::find_if (
428 0 : mpImpl->maContainer.begin(),
429 0 : mpImpl->maContainer.end(),
430 0 : MasterPageDescriptor::PageObjectComparator(pPage)));
431 0 : if (iEntry != mpImpl->maContainer.end())
432 0 : aResult = (*iEntry)->maToken;
433 : }
434 0 : return aResult;
435 : }
436 :
437 :
438 :
439 :
440 0 : String MasterPageContainer::GetURLForToken (
441 : MasterPageContainer::Token aToken)
442 : {
443 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
444 :
445 0 : SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
446 0 : if (pDescriptor.get() != NULL)
447 0 : return pDescriptor->msURL;
448 : else
449 0 : return String();
450 : }
451 :
452 :
453 :
454 :
455 0 : String MasterPageContainer::GetPageNameForToken (
456 : MasterPageContainer::Token aToken)
457 : {
458 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
459 :
460 0 : SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
461 0 : if (pDescriptor.get() != NULL)
462 0 : return pDescriptor->msPageName;
463 : else
464 0 : return String();
465 : }
466 :
467 :
468 :
469 :
470 0 : String MasterPageContainer::GetStyleNameForToken (
471 : MasterPageContainer::Token aToken)
472 : {
473 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
474 :
475 0 : SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
476 0 : if (pDescriptor.get() != NULL)
477 0 : return pDescriptor->msStyleName;
478 : else
479 0 : return String();
480 : }
481 :
482 :
483 :
484 :
485 0 : SdPage* MasterPageContainer::GetPageObjectForToken (
486 : MasterPageContainer::Token aToken,
487 : bool bLoad)
488 : {
489 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
490 :
491 0 : SdPage* pPageObject = NULL;
492 0 : SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
493 0 : if (pDescriptor.get() != NULL)
494 : {
495 0 : pPageObject = pDescriptor->mpMasterPage;
496 0 : if (pPageObject == NULL)
497 : {
498 : // The page object is not (yet) present. Call
499 : // UpdateDescriptor() to trigger the PageObjectProvider() to
500 : // provide it.
501 0 : if (bLoad)
502 0 : mpImpl->GetModel();
503 0 : if (mpImpl->UpdateDescriptor(pDescriptor,bLoad,false, true))
504 0 : pPageObject = pDescriptor->mpMasterPage;
505 : }
506 : }
507 0 : return pPageObject;
508 : }
509 :
510 :
511 :
512 :
513 0 : MasterPageContainer::Origin MasterPageContainer::GetOriginForToken (Token aToken)
514 : {
515 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
516 :
517 0 : SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
518 0 : if (pDescriptor.get() != NULL)
519 0 : return pDescriptor->meOrigin;
520 : else
521 0 : return UNKNOWN;
522 : }
523 :
524 :
525 :
526 :
527 0 : sal_Int32 MasterPageContainer::GetTemplateIndexForToken (Token aToken)
528 : {
529 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
530 :
531 0 : SharedMasterPageDescriptor pDescriptor = mpImpl->GetDescriptor(aToken);
532 0 : if (pDescriptor.get() != NULL)
533 0 : return pDescriptor->mnTemplateIndex;
534 : else
535 0 : return -1;
536 : }
537 :
538 :
539 :
540 :
541 0 : SharedMasterPageDescriptor MasterPageContainer::GetDescriptorForToken (
542 : MasterPageContainer::Token aToken)
543 : {
544 0 : const ::osl::MutexGuard aGuard (mpImpl->maMutex);
545 :
546 0 : return mpImpl->GetDescriptor(aToken);
547 : }
548 :
549 :
550 :
551 0 : void MasterPageContainer::InvalidatePreview (MasterPageContainer::Token aToken)
552 : {
553 0 : mpImpl->InvalidatePreview(aToken);
554 0 : }
555 :
556 :
557 :
558 :
559 0 : Image MasterPageContainer::GetPreviewForToken (MasterPageContainer::Token aToken)
560 : {
561 0 : return mpImpl->GetPreviewForToken(aToken,mePreviewSize);
562 : }
563 :
564 :
565 :
566 :
567 0 : MasterPageContainer::PreviewState MasterPageContainer::GetPreviewState (Token aToken)
568 : {
569 0 : return mpImpl->GetPreviewState(aToken);
570 : }
571 :
572 :
573 :
574 :
575 0 : bool MasterPageContainer::RequestPreview (Token aToken)
576 : {
577 0 : return mpImpl->RequestPreview(aToken);
578 : }
579 :
580 :
581 :
582 :
583 : //==== Implementation ================================================
584 :
585 0 : MasterPageContainer::Implementation::Implementation (void)
586 : : maMutex(),
587 : maContainer(),
588 : meInitializationState(NOT_INITIALIZED),
589 : mpRequestQueue(NULL),
590 : mxModel(NULL),
591 : mpDocument(NULL),
592 : maPreviewRenderer(),
593 : mbFirstPageObjectSeen(false),
594 : maLargePreviewBeingCreated(),
595 : maSmallPreviewBeingCreated(),
596 : maLargePreviewNotAvailable(),
597 : maSmallPreviewNotAvailable(),
598 : maChangeListeners(),
599 : maSmallPreviewSizePixel(),
600 : maLargePreviewSizePixel(),
601 : mbPageRatioKnown(false),
602 0 : mbContainerCleaningPending(true)
603 :
604 : {
605 0 : UpdatePreviewSizePixel();
606 0 : }
607 :
608 :
609 :
610 :
611 0 : MasterPageContainer::Implementation::~Implementation (void)
612 : {
613 : // When the initializer or filler tasks are still running then we have
614 : // to stop them now in order to prevent them from calling us back.
615 0 : tools::TimerBasedTaskExecution::ReleaseTask(mpFillerTask);
616 :
617 0 : mpRequestQueue.reset();
618 :
619 0 : uno::Reference<util::XCloseable> xCloseable (mxModel, uno::UNO_QUERY);
620 0 : if (xCloseable.is())
621 : {
622 : try
623 : {
624 0 : xCloseable->close(true);
625 : }
626 0 : catch (const ::com::sun::star::util::CloseVetoException&)
627 : {
628 : }
629 : }
630 0 : mxModel = NULL;
631 0 : }
632 :
633 :
634 :
635 :
636 0 : void MasterPageContainer::Implementation::LateInit (void)
637 : {
638 0 : const ::osl::MutexGuard aGuard (maMutex);
639 :
640 0 : if (meInitializationState == NOT_INITIALIZED)
641 : {
642 0 : meInitializationState = INITIALIZING;
643 :
644 : OSL_ASSERT(Instance().get()==this);
645 : mpRequestQueue.reset(MasterPageContainerQueue::Create(
646 0 : ::boost::shared_ptr<MasterPageContainerQueue::ContainerAdapter>(Instance())));
647 :
648 : mpFillerTask = ::sd::tools::TimerBasedTaskExecution::Create(
649 0 : ::boost::shared_ptr<tools::AsynchronousTask>(new MasterPageContainerFiller(*this)),
650 : 5,
651 0 : 50);
652 :
653 0 : meInitializationState = INITIALIZED;
654 0 : }
655 0 : }
656 :
657 :
658 :
659 :
660 0 : void MasterPageContainer::Implementation::AddChangeListener (const Link& rLink)
661 : {
662 0 : const ::osl::MutexGuard aGuard (maMutex);
663 :
664 : ::std::vector<Link>::iterator iListener (
665 0 : ::std::find(maChangeListeners.begin(),maChangeListeners.end(),rLink));
666 0 : if (iListener == maChangeListeners.end())
667 0 : maChangeListeners.push_back(rLink);
668 :
669 0 : }
670 :
671 :
672 :
673 :
674 0 : void MasterPageContainer::Implementation::RemoveChangeListener (const Link& rLink)
675 : {
676 0 : const ::osl::MutexGuard aGuard (maMutex);
677 :
678 : ::std::vector<Link>::iterator iListener (
679 0 : ::std::find(maChangeListeners.begin(),maChangeListeners.end(),rLink));
680 0 : if (iListener != maChangeListeners.end())
681 0 : maChangeListeners.erase(iListener);
682 0 : }
683 :
684 :
685 :
686 :
687 0 : void MasterPageContainer::Implementation::UpdatePreviewSizePixel (void)
688 : {
689 0 : const ::osl::MutexGuard aGuard (maMutex);
690 :
691 : // The default aspect ratio is 4:3
692 0 : int nWidth (4);
693 0 : int nHeight (3);
694 :
695 : // Search for the first entry with an existing master page.
696 0 : MasterPageContainerType::const_iterator iDescriptor;
697 0 : MasterPageContainerType::const_iterator iContainerEnd(maContainer.end());
698 0 : for (iDescriptor=maContainer.begin(); iDescriptor!=iContainerEnd; ++iDescriptor)
699 0 : if (*iDescriptor!=NULL && (*iDescriptor)->mpMasterPage != NULL)
700 : {
701 0 : Size aPageSize ((*iDescriptor)->mpMasterPage->GetSize());
702 : OSL_ASSERT(aPageSize.Width() > 0 && aPageSize.Height() > 0);
703 0 : if (aPageSize.Width() > 0)
704 0 : nWidth = aPageSize.Width();
705 0 : if (aPageSize.Height() > 0)
706 0 : nHeight = aPageSize.Height();
707 0 : mbFirstPageObjectSeen = true;
708 : break;
709 : }
710 :
711 0 : maSmallPreviewSizePixel.Width() = SMALL_PREVIEW_WIDTH;
712 0 : maLargePreviewSizePixel.Width() = LARGE_PREVIEW_WIDTH;
713 :
714 0 : int nNewSmallHeight ((maSmallPreviewSizePixel.Width()-2) * nHeight / nWidth + 2);
715 0 : int nNewLargeHeight ((maLargePreviewSizePixel.Width()-2) * nHeight / nWidth + 2);
716 :
717 0 : if (nNewSmallHeight!=maSmallPreviewSizePixel.Height()
718 0 : || nNewLargeHeight!=maLargePreviewSizePixel.Height())
719 : {
720 0 : maSmallPreviewSizePixel.Height() = nNewSmallHeight;
721 0 : maLargePreviewSizePixel.Height() = nNewLargeHeight;
722 : FireContainerChange(
723 : MasterPageContainerChangeEvent::SIZE_CHANGED,
724 0 : NIL_TOKEN);
725 0 : }
726 0 : }
727 :
728 :
729 :
730 :
731 0 : Size MasterPageContainer::Implementation::GetPreviewSizePixel (PreviewSize eSize) const
732 : {
733 0 : if (eSize == SMALL)
734 0 : return maSmallPreviewSizePixel;
735 : else
736 0 : return maLargePreviewSizePixel;
737 : }
738 :
739 :
740 :
741 :
742 0 : IMPL_LINK(MasterPageContainer::Implementation,AsynchronousNotifyCallback, EventData*, pData)
743 : {
744 0 : const ::osl::MutexGuard aGuard (maMutex);
745 :
746 0 : if (pData != NULL)
747 : {
748 0 : FireContainerChange(pData->first, pData->second, false);
749 0 : delete pData;
750 : }
751 :
752 0 : return 0;
753 : }
754 :
755 :
756 :
757 :
758 0 : MasterPageContainer::Token MasterPageContainer::Implementation::PutMasterPage (
759 : const SharedMasterPageDescriptor& rpDescriptor)
760 : {
761 0 : const ::osl::MutexGuard aGuard (maMutex);
762 :
763 0 : Token aResult (NIL_TOKEN);
764 :
765 : // Get page object and preview when that is inexpensive.
766 0 : UpdateDescriptor(rpDescriptor,false,false, false);
767 :
768 : // Look up the new MasterPageDescriptor and either insert it or update
769 : // an already existing one.
770 : MasterPageContainerType::iterator aEntry (
771 : ::std::find_if (
772 : maContainer.begin(),
773 : maContainer.end(),
774 0 : MasterPageDescriptor::AllComparator(rpDescriptor)));
775 0 : if (aEntry == maContainer.end())
776 : {
777 : // Insert a new MasterPageDescriptor.
778 0 : bool bIgnore (rpDescriptor->mpPageObjectProvider.get()==NULL
779 0 : && rpDescriptor->msURL.isEmpty());
780 :
781 0 : if ( ! bIgnore)
782 : {
783 0 : if (mbContainerCleaningPending)
784 0 : CleanContainer();
785 :
786 0 : aResult = maContainer.size();
787 0 : rpDescriptor->SetToken(aResult);
788 :
789 : // Templates are precious, i.e. we lock them so that they will
790 : // not be destroyed when (temporarily) no one references them.
791 : // They will only be deleted when the container is destroyed.
792 0 : switch (rpDescriptor->meOrigin)
793 : {
794 : case TEMPLATE:
795 : case DEFAULT:
796 0 : ++rpDescriptor->mnUseCount;
797 0 : break;
798 :
799 : default:
800 0 : break;
801 : }
802 :
803 0 : maContainer.push_back(rpDescriptor);
804 0 : aEntry = maContainer.end()-1;
805 :
806 0 : FireContainerChange(MasterPageContainerChangeEvent::CHILD_ADDED,aResult);
807 : }
808 : }
809 : else
810 : {
811 : // Update an existing MasterPageDescriptor.
812 0 : aResult = (*aEntry)->maToken;
813 : ::std::auto_ptr<std::vector<MasterPageContainerChangeEvent::EventType> > pEventTypes(
814 0 : (*aEntry)->Update(*rpDescriptor));
815 0 : if (pEventTypes.get()!=NULL && pEventTypes->size()>0)
816 : {
817 : // One or more aspects of the descriptor have changed. Send
818 : // appropriate events to the listeners.
819 0 : UpdateDescriptor(*aEntry,false,false, true);
820 :
821 0 : std::vector<MasterPageContainerChangeEvent::EventType>::const_iterator iEventType;
822 0 : for (iEventType=pEventTypes->begin(); iEventType!=pEventTypes->end(); ++iEventType)
823 : {
824 : FireContainerChange(
825 0 : *iEventType,
826 0 : (*aEntry)->maToken,
827 0 : false);
828 : }
829 0 : }
830 : }
831 :
832 0 : return aResult;
833 : }
834 :
835 :
836 :
837 :
838 0 : bool MasterPageContainer::Implementation::HasToken (Token aToken) const
839 : {
840 : return aToken>=0
841 0 : && (unsigned)aToken<maContainer.size()
842 0 : && maContainer[aToken].get()!=NULL;
843 : }
844 :
845 :
846 :
847 :
848 0 : const SharedMasterPageDescriptor MasterPageContainer::Implementation::GetDescriptor (
849 : Token aToken) const
850 : {
851 0 : if (aToken>=0 && (unsigned)aToken<maContainer.size())
852 0 : return maContainer[aToken];
853 : else
854 0 : return SharedMasterPageDescriptor();
855 : }
856 :
857 :
858 :
859 :
860 0 : SharedMasterPageDescriptor MasterPageContainer::Implementation::GetDescriptor (Token aToken)
861 : {
862 0 : if (aToken>=0 && (unsigned)aToken<maContainer.size())
863 0 : return maContainer[aToken];
864 : else
865 0 : return SharedMasterPageDescriptor();
866 : }
867 :
868 :
869 :
870 :
871 0 : void MasterPageContainer::Implementation::InvalidatePreview (Token aToken)
872 : {
873 0 : const ::osl::MutexGuard aGuard (maMutex);
874 :
875 0 : SharedMasterPageDescriptor pDescriptor (GetDescriptor(aToken));
876 0 : if (pDescriptor.get() != NULL)
877 : {
878 0 : pDescriptor->maSmallPreview = Image();
879 0 : pDescriptor->maLargePreview = Image();
880 0 : RequestPreview(aToken);
881 0 : }
882 0 : }
883 :
884 :
885 :
886 :
887 0 : Image MasterPageContainer::Implementation::GetPreviewForToken (
888 : MasterPageContainer::Token aToken,
889 : PreviewSize ePreviewSize)
890 : {
891 0 : const ::osl::MutexGuard aGuard (maMutex);
892 :
893 0 : Image aPreview;
894 0 : PreviewState ePreviewState (GetPreviewState(aToken));
895 :
896 0 : SharedMasterPageDescriptor pDescriptor = GetDescriptor(aToken);
897 :
898 : // When the preview is missing but inexpensively creatable then do that
899 : // now.
900 0 : if (pDescriptor.get()!=NULL)
901 : {
902 0 : if (ePreviewState == PS_CREATABLE)
903 0 : if (UpdateDescriptor(pDescriptor, false,false, true))
904 0 : if (pDescriptor->maLargePreview.GetSizePixel().Width() != 0)
905 0 : ePreviewState = PS_AVAILABLE;
906 :
907 0 : switch (ePreviewState)
908 : {
909 : case PS_AVAILABLE:
910 0 : aPreview = pDescriptor->GetPreview(ePreviewSize);
911 0 : break;
912 :
913 : case PS_PREPARING:
914 : aPreview = GetPreviewSubstitution(
915 : STR_TASKPANEL_PREPARING_PREVIEW_SUBSTITUTION,
916 0 : ePreviewSize);
917 0 : break;
918 :
919 : case PS_CREATABLE:
920 : aPreview = GetPreviewSubstitution(
921 : STR_TASKPANEL_PREPARING_PREVIEW_SUBSTITUTION,
922 0 : ePreviewSize);
923 0 : break;
924 :
925 : case PS_NOT_AVAILABLE:
926 : aPreview = GetPreviewSubstitution(
927 : STR_TASKPANEL_NOT_AVAILABLE_SUBSTITUTION,
928 0 : ePreviewSize);
929 0 : if (ePreviewSize == SMALL)
930 0 : pDescriptor->maSmallPreview = aPreview;
931 : else
932 0 : pDescriptor->maLargePreview = aPreview;
933 0 : break;
934 : }
935 : }
936 :
937 0 : return aPreview;
938 : }
939 :
940 :
941 :
942 :
943 0 : MasterPageContainer::PreviewState MasterPageContainer::Implementation::GetPreviewState (
944 : Token aToken) const
945 : {
946 0 : const ::osl::MutexGuard aGuard (maMutex);
947 :
948 0 : PreviewState eState (PS_NOT_AVAILABLE);
949 :
950 0 : SharedMasterPageDescriptor pDescriptor = GetDescriptor(aToken);
951 0 : if (pDescriptor.get() != NULL)
952 : {
953 0 : if (pDescriptor->maLargePreview.GetSizePixel().Width() != 0)
954 0 : eState = PS_AVAILABLE;
955 0 : else if (pDescriptor->mpPreviewProvider.get() != NULL)
956 : {
957 : // The preview does not exist but can be created. When that is
958 : // not expensive then do it at once.
959 0 : if (mpRequestQueue->HasRequest(aToken))
960 0 : eState = PS_PREPARING;
961 : else
962 0 : eState = PS_CREATABLE;
963 : }
964 : else
965 0 : eState = PS_NOT_AVAILABLE;
966 : }
967 :
968 0 : return eState;
969 : }
970 :
971 :
972 :
973 :
974 0 : bool MasterPageContainer::Implementation::RequestPreview (Token aToken)
975 : {
976 0 : SharedMasterPageDescriptor pDescriptor = GetDescriptor(aToken);
977 0 : if (pDescriptor.get() != NULL)
978 0 : return mpRequestQueue->RequestPreview(pDescriptor);
979 : else
980 0 : return false;
981 : }
982 :
983 :
984 :
985 :
986 0 : Reference<frame::XModel> MasterPageContainer::Implementation::GetModel (void)
987 : {
988 0 : const ::osl::MutexGuard aGuard (maMutex);
989 :
990 0 : if ( ! mxModel.is())
991 : {
992 : // Get the desktop a s service factory.
993 0 : ::rtl::OUString sDesktopServiceName ( "com.sun.star.frame.Desktop");
994 : uno::Reference<frame::XComponentLoader> xDesktop (
995 0 : ::comphelper::getProcessServiceFactory()->createInstance(
996 0 : sDesktopServiceName),
997 0 : uno::UNO_QUERY);
998 :
999 : // Create a new model.
1000 0 : ::rtl::OUString sModelServiceName ( "com.sun.star.presentation.PresentationDocument");
1001 : mxModel = uno::Reference<frame::XModel>(
1002 0 : ::comphelper::getProcessServiceFactory()->createInstance(
1003 0 : sModelServiceName),
1004 0 : uno::UNO_QUERY);
1005 :
1006 : // Initialize the model.
1007 0 : uno::Reference<frame::XLoadable> xLoadable (mxModel,uno::UNO_QUERY);
1008 0 : if (xLoadable.is())
1009 0 : xLoadable->initNew();
1010 :
1011 : // Use its tunnel to get a pointer to its core implementation.
1012 0 : uno::Reference<lang::XUnoTunnel> xUnoTunnel (mxModel, uno::UNO_QUERY);
1013 0 : if (xUnoTunnel.is())
1014 : {
1015 : mpDocument = reinterpret_cast<SdXImpressDocument*>(
1016 0 : xUnoTunnel->getSomething(
1017 0 : SdXImpressDocument::getUnoTunnelId()))->GetDoc();
1018 : }
1019 :
1020 : // Create a default page.
1021 0 : uno::Reference<drawing::XDrawPagesSupplier> xSlideSupplier (mxModel, uno::UNO_QUERY);
1022 0 : if (xSlideSupplier.is())
1023 : {
1024 : uno::Reference<drawing::XDrawPages> xSlides (
1025 0 : xSlideSupplier->getDrawPages(), uno::UNO_QUERY);
1026 0 : if (xSlides.is())
1027 : {
1028 0 : sal_Int32 nIndex (0);
1029 0 : uno::Reference<drawing::XDrawPage> xNewPage (xSlides->insertNewByIndex(nIndex));
1030 0 : uno::Reference<beans::XPropertySet> xProperties(xNewPage, uno::UNO_QUERY);
1031 0 : if (xProperties.is())
1032 0 : xProperties->setPropertyValue(
1033 : "Layout",
1034 0 : makeAny((sal_Int16)AUTOLAYOUT_TITLE));
1035 0 : }
1036 0 : }
1037 : }
1038 0 : return mxModel;
1039 : }
1040 :
1041 :
1042 :
1043 :
1044 0 : SdDrawDocument* MasterPageContainer::Implementation::GetDocument (void)
1045 : {
1046 0 : GetModel();
1047 0 : return mpDocument;
1048 : }
1049 :
1050 :
1051 :
1052 :
1053 0 : Image MasterPageContainer::Implementation::GetPreviewSubstitution (
1054 : sal_uInt16 nId,
1055 : PreviewSize ePreviewSize)
1056 : {
1057 0 : const ::osl::MutexGuard aGuard (maMutex);
1058 :
1059 0 : Image aPreview;
1060 :
1061 0 : switch (nId)
1062 : {
1063 : case STR_TASKPANEL_PREPARING_PREVIEW_SUBSTITUTION:
1064 : {
1065 : Image& rPreview (ePreviewSize==SMALL
1066 : ? maSmallPreviewBeingCreated
1067 0 : : maLargePreviewBeingCreated);
1068 0 : if (rPreview.GetSizePixel().Width() == 0)
1069 : {
1070 : rPreview = maPreviewRenderer.RenderSubstitution(
1071 : ePreviewSize==SMALL ? maSmallPreviewSizePixel : maLargePreviewSizePixel,
1072 0 : SdResId(STR_TASKPANEL_PREPARING_PREVIEW_SUBSTITUTION));
1073 : }
1074 0 : aPreview = rPreview;
1075 : }
1076 0 : break;
1077 :
1078 : case STR_TASKPANEL_NOT_AVAILABLE_SUBSTITUTION:
1079 : {
1080 : Image& rPreview (ePreviewSize==SMALL
1081 : ? maSmallPreviewNotAvailable
1082 0 : : maLargePreviewNotAvailable);
1083 0 : if (rPreview.GetSizePixel().Width() == 0)
1084 : {
1085 : rPreview = maPreviewRenderer.RenderSubstitution(
1086 : ePreviewSize==SMALL ? maSmallPreviewSizePixel : maLargePreviewSizePixel,
1087 0 : SdResId(STR_TASKPANEL_NOT_AVAILABLE_SUBSTITUTION));
1088 : }
1089 0 : aPreview = rPreview;
1090 : }
1091 0 : break;
1092 : }
1093 :
1094 0 : return aPreview;
1095 : }
1096 :
1097 :
1098 :
1099 :
1100 0 : void MasterPageContainer::Implementation::CleanContainer (void)
1101 : {
1102 : // Remove the empty elements at the end of the container. The empty
1103 : // elements in the middle can not be removed because that would
1104 : // invalidate the references still held by others.
1105 0 : int nIndex (maContainer.size()-1);
1106 0 : while (nIndex>=0 && maContainer[nIndex].get()==NULL)
1107 0 : --nIndex;
1108 0 : maContainer.resize(++nIndex);
1109 0 : }
1110 :
1111 :
1112 :
1113 :
1114 0 : void MasterPageContainer::Implementation::FireContainerChange (
1115 : MasterPageContainerChangeEvent::EventType eType,
1116 : Token aToken,
1117 : bool bNotifyAsynchronously)
1118 : {
1119 0 : if (bNotifyAsynchronously)
1120 : {
1121 : Application::PostUserEvent(
1122 : LINK(this,Implementation,AsynchronousNotifyCallback),
1123 0 : new EventData(eType,aToken));
1124 : }
1125 : else
1126 : {
1127 0 : ::std::vector<Link> aCopy(maChangeListeners.begin(),maChangeListeners.end());
1128 0 : ::std::vector<Link>::iterator iListener;
1129 : MasterPageContainerChangeEvent aEvent;
1130 0 : aEvent.meEventType = eType;
1131 0 : aEvent.maChildToken = aToken;
1132 0 : for (iListener=aCopy.begin(); iListener!=aCopy.end(); ++iListener)
1133 0 : iListener->Call(&aEvent);
1134 : }
1135 0 : }
1136 :
1137 :
1138 :
1139 :
1140 0 : bool MasterPageContainer::Implementation::UpdateDescriptor (
1141 : const SharedMasterPageDescriptor& rpDescriptor,
1142 : bool bForcePageObject,
1143 : bool bForcePreview,
1144 : bool bSendEvents)
1145 : {
1146 0 : const ::osl::MutexGuard aGuard (maMutex);
1147 :
1148 : // We have to create the page object when the preview provider needs it
1149 : // and the caller needs the preview.
1150 : bForcePageObject |= (bForcePreview
1151 0 : && rpDescriptor->mpPreviewProvider->NeedsPageObject()
1152 0 : && rpDescriptor->mpMasterPage==NULL);
1153 :
1154 : // Define a cost threshold so that an update or page object or preview
1155 : // that is at least this cost are made at once. Updates with higher cost
1156 : // are scheduled for later.
1157 0 : sal_Int32 nCostThreshold (mpRequestQueue->IsEmpty() ? 5 : 0);
1158 :
1159 : // Update the page object (which may be used for the preview update).
1160 0 : if (bForcePageObject)
1161 0 : GetDocument();
1162 : int nPageObjectModified (rpDescriptor->UpdatePageObject(
1163 : (bForcePageObject ? -1 : nCostThreshold),
1164 0 : mpDocument));
1165 0 : if (nPageObjectModified == 1 && bSendEvents)
1166 : FireContainerChange(
1167 : MasterPageContainerChangeEvent::DATA_CHANGED,
1168 0 : rpDescriptor->maToken);
1169 0 : if (nPageObjectModified == -1 && bSendEvents)
1170 : FireContainerChange(
1171 : MasterPageContainerChangeEvent::CHILD_REMOVED,
1172 0 : rpDescriptor->maToken);
1173 0 : if (nPageObjectModified && ! mbFirstPageObjectSeen)
1174 0 : UpdatePreviewSizePixel();
1175 :
1176 : // Update the preview.
1177 : bool bPreviewModified (rpDescriptor->UpdatePreview(
1178 : (bForcePreview ? -1 : nCostThreshold),
1179 : maSmallPreviewSizePixel,
1180 : maLargePreviewSizePixel,
1181 0 : maPreviewRenderer));
1182 :
1183 0 : if (bPreviewModified && bSendEvents)
1184 : FireContainerChange(
1185 : MasterPageContainerChangeEvent::PREVIEW_CHANGED,
1186 0 : rpDescriptor->maToken);
1187 :
1188 0 : return nPageObjectModified || bPreviewModified;
1189 : }
1190 :
1191 :
1192 :
1193 :
1194 0 : void MasterPageContainer::Implementation::ReleaseDescriptor (Token aToken)
1195 : {
1196 0 : if (aToken>=0 && (unsigned)aToken<maContainer.size())
1197 : {
1198 0 : maContainer[aToken].reset();
1199 0 : mbContainerCleaningPending = true;
1200 : }
1201 0 : }
1202 :
1203 :
1204 :
1205 :
1206 0 : void MasterPageContainer::Implementation::FillingDone (void)
1207 : {
1208 0 : mpRequestQueue->ProcessAllRequests();
1209 0 : }
1210 :
1211 :
1212 :
1213 18 : } } } // end of namespace ::sd::toolpanel::controls
1214 :
1215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|