1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PresenterPreviewCache.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "precompiled_sd.hxx"
36 #include "PresenterPreviewCache.hxx"
38 #include "cache/SlsCacheContext.hxx"
39 #include "tools/IdleDetection.hxx"
41 #include <cppcanvas/vclfactory.hxx>
42 #include <com/sun/star/drawing/XDrawPage.hpp>
43 #include <com/sun/star/rendering/XBitmapCanvas.hpp>
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::uno
;
47 using namespace ::sd::slidesorter::cache
;
48 using ::rtl::OUString
;
50 namespace sd
{ namespace presenter
{
53 class PresenterPreviewCache::PresenterCacheContext
: public CacheContext
56 PresenterCacheContext (void);
57 virtual ~PresenterCacheContext (void);
59 void SetDocumentSlides (
60 const Reference
<container::XIndexAccess
>& rxSlides
,
61 const Reference
<XInterface
>& rxDocument
);
62 void SetVisibleSlideRange (
63 const sal_Int32 nFirstVisibleSlideIndex
,
64 const sal_Int32 nLastVisibleSlideIndex
);
65 const SdrPage
* GetPage (const sal_Int32 nSlideIndex
) const;
66 void AddPreviewCreationNotifyListener (const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
);
67 void RemovePreviewCreationNotifyListener (const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
);
70 virtual void NotifyPreviewCreation (
71 CacheKey aKey
, const ::boost::shared_ptr
<BitmapEx
>& rPreview
);
72 virtual bool IsIdle (void);
73 virtual bool IsVisible (CacheKey aKey
);
74 virtual const SdrPage
* GetPage (CacheKey aKey
);
75 virtual ::boost::shared_ptr
<std::vector
<CacheKey
> > GetEntryList (bool bVisible
);
76 virtual sal_Int32
GetPriority (CacheKey aKey
);
77 virtual ::com::sun::star::uno::Reference
<com::sun::star::uno::XInterface
> GetModel (void);
80 Reference
<container::XIndexAccess
> mxSlides
;
81 Reference
<XInterface
> mxDocument
;
82 sal_Int32 mnFirstVisibleSlideIndex
;
83 sal_Int32 mnLastVisibleSlideIndex
;
84 typedef ::std::vector
<css::uno::Reference
<css::drawing::XSlidePreviewCacheListener
> > ListenerContainer
;
85 ListenerContainer maListeners
;
87 void CallListeners (const sal_Int32 nSlideIndex
);
93 //===== Service ===============================================================
95 Reference
<XInterface
> SAL_CALL
PresenterPreviewCache_createInstance (
96 const Reference
<XComponentContext
>& rxContext
)
98 return Reference
<XInterface
>(static_cast<XWeak
*>(new PresenterPreviewCache(rxContext
)));
104 ::rtl::OUString
PresenterPreviewCache_getImplementationName (void) throw(RuntimeException
)
106 return OUString::createFromAscii("com.sun.star.comp.Draw.PresenterPreviewCache");
112 Sequence
<rtl::OUString
> SAL_CALL
PresenterPreviewCache_getSupportedServiceNames (void)
113 throw (RuntimeException
)
115 static const ::rtl::OUString
sServiceName(
116 ::rtl::OUString::createFromAscii("com.sun.star.drawing.PresenterPreviewCache"));
117 return Sequence
<rtl::OUString
>(&sServiceName
, 1);
123 //===== PresenterPreviewCache =================================================
125 PresenterPreviewCache::PresenterPreviewCache (const Reference
<XComponentContext
>& rxContext
)
126 : PresenterPreviewCacheInterfaceBase(m_aMutex
),
127 maPreviewSize(Size(200,200)),
128 mpCacheContext(new PresenterCacheContext()),
129 mpCache(new PageCache(maPreviewSize
, mpCacheContext
))
137 PresenterPreviewCache::~PresenterPreviewCache (void)
144 //----- XInitialize -----------------------------------------------------------
146 void SAL_CALL
PresenterPreviewCache::initialize (const Sequence
<Any
>& rArguments
)
147 throw(Exception
, RuntimeException
)
149 if (rArguments
.getLength() != 0)
150 throw RuntimeException();
156 //----- XSlidePreviewCache ----------------------------------------------------
158 void SAL_CALL
PresenterPreviewCache::setDocumentSlides (
159 const Reference
<container::XIndexAccess
>& rxSlides
,
160 const Reference
<XInterface
>& rxDocument
)
161 throw (RuntimeException
)
164 OSL_ASSERT(mpCacheContext
.get()!=NULL
);
166 mpCacheContext
->SetDocumentSlides(rxSlides
, rxDocument
);
172 void SAL_CALL
PresenterPreviewCache::setVisibleRange (
173 sal_Int32 nFirstVisibleSlideIndex
,
174 sal_Int32 nLastVisibleSlideIndex
)
175 throw (css::uno::RuntimeException
)
178 OSL_ASSERT(mpCacheContext
.get()!=NULL
);
180 mpCacheContext
->SetVisibleSlideRange (nFirstVisibleSlideIndex
, nLastVisibleSlideIndex
);
186 void SAL_CALL
PresenterPreviewCache::setPreviewSize (
187 const css::geometry::IntegerSize2D
& rSize
)
188 throw (css::uno::RuntimeException
)
191 OSL_ASSERT(mpCache
.get()!=NULL
);
193 maPreviewSize
= Size(rSize
.Width
, rSize
.Height
);
194 mpCache
->ChangeSize(maPreviewSize
);
200 Reference
<rendering::XBitmap
> SAL_CALL
PresenterPreviewCache::getSlidePreview (
201 sal_Int32 nSlideIndex
,
202 const Reference
<rendering::XCanvas
>& rxCanvas
)
203 throw (css::uno::RuntimeException
)
206 OSL_ASSERT(mpCacheContext
.get()!=NULL
);
208 cppcanvas::BitmapCanvasSharedPtr
pCanvas (
209 cppcanvas::VCLFactory::getInstance().createCanvas(
210 Reference
<rendering::XBitmapCanvas
>(rxCanvas
, UNO_QUERY
)));
212 const SdrPage
* pPage
= mpCacheContext
->GetPage(nSlideIndex
);
214 throw RuntimeException();
216 const BitmapEx
aPreview (mpCache
->GetPreviewBitmap(pPage
, maPreviewSize
));
217 if (aPreview
.IsEmpty())
220 return cppcanvas::VCLFactory::getInstance().createBitmap(
222 aPreview
)->getUNOBitmap();
228 void SAL_CALL
PresenterPreviewCache::addPreviewCreationNotifyListener (
229 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
230 throw (css::uno::RuntimeException
)
232 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
235 mpCacheContext
->AddPreviewCreationNotifyListener(rxListener
);
241 void SAL_CALL
PresenterPreviewCache::removePreviewCreationNotifyListener (
242 const css::uno::Reference
<css::drawing::XSlidePreviewCacheListener
>& rxListener
)
243 throw (css::uno::RuntimeException
)
246 mpCacheContext
->RemovePreviewCreationNotifyListener(rxListener
);
252 void SAL_CALL
PresenterPreviewCache::pause (void)
253 throw (css::uno::RuntimeException
)
256 OSL_ASSERT(mpCache
.get()!=NULL
);
263 void SAL_CALL
PresenterPreviewCache::resume (void)
264 throw (css::uno::RuntimeException
)
267 OSL_ASSERT(mpCache
.get()!=NULL
);
274 //-----------------------------------------------------------------------------
276 void PresenterPreviewCache::ThrowIfDisposed (void)
277 throw (::com::sun::star::lang::DisposedException
)
279 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
281 throw lang::DisposedException (
282 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
283 "PresenterPreviewCache object has already been disposed")),
284 static_cast<uno::XWeak
*>(this));
291 //===== PresenterPreviewCache::PresenterCacheContext ==========================
294 PresenterPreviewCache::PresenterCacheContext::PresenterCacheContext (void)
297 mnFirstVisibleSlideIndex(-1),
298 mnLastVisibleSlideIndex(-1),
306 PresenterPreviewCache::PresenterCacheContext::~PresenterCacheContext (void)
313 void PresenterPreviewCache::PresenterCacheContext::SetDocumentSlides (
314 const Reference
<container::XIndexAccess
>& rxSlides
,
315 const Reference
<XInterface
>& rxDocument
)
318 mxDocument
= rxDocument
;
319 mnFirstVisibleSlideIndex
= -1;
320 mnLastVisibleSlideIndex
= -1;
326 void PresenterPreviewCache::PresenterCacheContext::SetVisibleSlideRange (
327 const sal_Int32 nFirstVisibleSlideIndex
,
328 const sal_Int32 nLastVisibleSlideIndex
)
330 if (nFirstVisibleSlideIndex
> nLastVisibleSlideIndex
|| nFirstVisibleSlideIndex
<0)
332 mnFirstVisibleSlideIndex
= -1;
333 mnLastVisibleSlideIndex
= -1;
337 mnFirstVisibleSlideIndex
= nFirstVisibleSlideIndex
;
338 mnLastVisibleSlideIndex
= nLastVisibleSlideIndex
;
340 if (mxSlides
.is() && mnLastVisibleSlideIndex
>= mxSlides
->getCount())
341 mnLastVisibleSlideIndex
= mxSlides
->getCount() - 1;
347 void PresenterPreviewCache::PresenterCacheContext::AddPreviewCreationNotifyListener (
348 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
350 maListeners
.push_back(rxListener
);
356 void PresenterPreviewCache::PresenterCacheContext::RemovePreviewCreationNotifyListener (
357 const Reference
<drawing::XSlidePreviewCacheListener
>& rxListener
)
359 ListenerContainer::iterator iListener
;
360 for (iListener
=maListeners
.begin(); iListener
!=maListeners
.end(); ++iListener
)
361 if (*iListener
== rxListener
)
363 maListeners
.erase(iListener
);
371 //----- CacheContext ----------------------------------------------------------
373 void PresenterPreviewCache::PresenterCacheContext::NotifyPreviewCreation (
375 const ::boost::shared_ptr
<BitmapEx
>& rPreview
)
379 if ( ! mxSlides
.is())
381 const sal_Int32
nCount(mxSlides
->getCount());
382 for (sal_Int32 nIndex
=0; nIndex
<nCount
; ++nIndex
)
383 if (aKey
== GetPage(nIndex
))
384 CallListeners(nIndex
);
390 bool PresenterPreviewCache::PresenterCacheContext::IsIdle (void)
394 sal_Int32 nIdleState (tools::IdleDetection::GetIdleState(NULL));
395 if (nIdleState == tools::IdleDetection::IDET_IDLE)
405 bool PresenterPreviewCache::PresenterCacheContext::IsVisible (CacheKey aKey
)
407 if (mnFirstVisibleSlideIndex
< 0)
409 for (sal_Int32 nIndex
=mnFirstVisibleSlideIndex
; nIndex
<=mnLastVisibleSlideIndex
; ++nIndex
)
411 const SdrPage
* pPage
= GetPage(nIndex
);
412 if (pPage
== static_cast<const SdrPage
*>(aKey
))
421 const SdrPage
* PresenterPreviewCache::PresenterCacheContext::GetPage (CacheKey aKey
)
423 return static_cast<const SdrPage
*>(aKey
);
429 ::boost::shared_ptr
<std::vector
<CacheKey
> >
430 PresenterPreviewCache::PresenterCacheContext::GetEntryList (bool bVisible
)
432 ::boost::shared_ptr
<std::vector
<CacheKey
> > pKeys (new std::vector
<CacheKey
>());
434 if ( ! mxSlides
.is())
437 const sal_Int32
nFirstIndex (bVisible
? mnFirstVisibleSlideIndex
: 0);
438 const sal_Int32
nLastIndex (bVisible
? mnLastVisibleSlideIndex
: mxSlides
->getCount()-1);
443 for (sal_Int32 nIndex
=nFirstIndex
; nIndex
<=nLastIndex
; ++nIndex
)
445 pKeys
->push_back(GetPage(nIndex
));
454 sal_Int32
PresenterPreviewCache::PresenterCacheContext::GetPriority (CacheKey aKey
)
456 if ( ! mxSlides
.is())
459 const sal_Int32
nCount (mxSlides
->getCount());
461 for (sal_Int32 nIndex
=mnFirstVisibleSlideIndex
; nIndex
<=mnLastVisibleSlideIndex
; ++nIndex
)
462 if (aKey
== GetPage(nIndex
))
463 return -nCount
-1+nIndex
;
465 for (sal_Int32 nIndex
=0; nIndex
<=nCount
; ++nIndex
)
466 if (aKey
== GetPage(nIndex
))
475 Reference
<XInterface
> PresenterPreviewCache::PresenterCacheContext::GetModel (void)
483 //-----------------------------------------------------------------------------
485 const SdrPage
* PresenterPreviewCache::PresenterCacheContext::GetPage (
486 const sal_Int32 nSlideIndex
) const
488 if ( ! mxSlides
.is())
490 if (nSlideIndex
< 0 || nSlideIndex
>= mxSlides
->getCount())
493 Reference
<drawing::XDrawPage
> xSlide (mxSlides
->getByIndex(nSlideIndex
), UNO_QUERY
);
494 const SdPage
* pPage
= SdPage::getImplementation(xSlide
);
495 return dynamic_cast<const SdrPage
*>(pPage
);
501 void PresenterPreviewCache::PresenterCacheContext::CallListeners (
502 const sal_Int32 nIndex
)
504 ListenerContainer
aListeners (maListeners
);
505 ListenerContainer::const_iterator iListener
;
506 for (iListener
=aListeners
.begin(); iListener
!=aListeners
.end(); ++iListener
)
510 (*iListener
)->notifyPreviewCreation(nIndex
);
512 catch (lang::DisposedException
&)
514 RemovePreviewCreationNotifyListener(*iListener
);
519 } } // end of namespace ::sd::presenter