merge the formfield patch from ooo-build
[ooovba.git] / sdext / source / presenter / PresenterViewFactory.cxx
blobb257f4203dd1284ad3183ed8bcab72aa5c2bf6d5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PresenterViewFactory.cxx,v $
11 * $Revision: 1.6 $
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 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "PresenterViewFactory.hxx"
36 #include "PresenterPaneContainer.hxx"
37 #include "PresenterHelper.hxx"
38 #include "PresenterHelpView.hxx"
39 #include "PresenterNotesView.hxx"
40 #include "PresenterSlideShowView.hxx"
41 #include "PresenterSlidePreview.hxx"
42 #include "PresenterSlideSorter.hxx"
43 #include "PresenterToolBar.hxx"
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <com/sun/star/drawing/framework/ResourceId.hpp>
46 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
47 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
48 #include <com/sun/star/drawing/XDrawPages.hpp>
49 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
50 #include <com/sun/star/drawing/XSlideSorterBase.hpp>
51 #include <com/sun/star/presentation/XSlideShow.hpp>
52 #include <com/sun/star/presentation/XSlideShowView.hpp>
53 #include <cppuhelper/compbase1.hxx>
54 #include <boost/bind.hpp>
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::drawing::framework;
59 using ::rtl::OUString;
61 #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString)))
63 namespace sdext { namespace presenter {
65 const ::rtl::OUString PresenterViewFactory::msCurrentSlidePreviewViewURL(
66 A2S("private:resource/view/Presenter/CurrentSlidePreview"));
67 const ::rtl::OUString PresenterViewFactory::msNextSlidePreviewViewURL(
68 A2S("private:resource/view/Presenter/NextSlidePreview"));
69 const ::rtl::OUString PresenterViewFactory::msNotesViewURL(
70 A2S("private:resource/view/Presenter/Notes"));
71 const ::rtl::OUString PresenterViewFactory::msToolBarViewURL(
72 A2S("private:resource/view/Presenter/ToolBar"));
73 const ::rtl::OUString PresenterViewFactory::msSlideSorterURL(
74 A2S("private:resource/view/Presenter/SlideSorter"));
75 const ::rtl::OUString PresenterViewFactory::msHelpViewURL(
76 A2S("private:resource/view/Presenter/Help"));
80 //===== SimpleView ============================================================
82 namespace {
84 typedef ::cppu::WeakComponentImplHelper1 <XView> SimpleViewInterfaceBase;
86 class SimpleView
87 : private ::cppu::BaseMutex,
88 public SimpleViewInterfaceBase
90 public:
91 SimpleView (const Reference<XResourceId>& rxViewId)
92 : SimpleViewInterfaceBase(m_aMutex),mxResourceId(rxViewId) {};
93 virtual ~SimpleView (void) {};
95 // XView
97 virtual Reference<XResourceId> SAL_CALL getResourceId (void) throw (RuntimeException)
98 { return mxResourceId; };
100 private:
101 Reference<XResourceId> mxResourceId;
106 /** By default the PresenterSlidePreview shows the preview of the current
107 slide. This adapter class makes it display the preview of the next
108 slide.
110 class NextSlidePreview : public PresenterSlidePreview
112 public:
113 NextSlidePreview (
114 const css::uno::Reference<css::uno::XComponentContext>& rxContext,
115 const css::uno::Reference<css::drawing::framework::XResourceId>& rxViewId,
116 const css::uno::Reference<css::drawing::framework::XPane>& rxAnchorPane,
117 const ::rtl::Reference<PresenterController>& rpPresenterController)
118 : PresenterSlidePreview(rxContext, rxViewId, rxAnchorPane, rpPresenterController)
121 virtual ~NextSlidePreview (void) {}
122 virtual void SAL_CALL setCurrentPage (
123 const css::uno::Reference<css::drawing::XDrawPage>& rxSlide)
124 throw (css::uno::RuntimeException)
126 Reference<presentation::XSlideShowController> xSlideShowController (
127 mpPresenterController->GetSlideShowController());
128 Reference<drawing::XDrawPage> xSlide;
129 if (xSlideShowController.is())
131 const sal_Int32 nCount (xSlideShowController->getSlideCount());
132 sal_Int32 nNextSlideIndex (-1);
133 if (xSlideShowController->getCurrentSlide() == rxSlide)
135 nNextSlideIndex = xSlideShowController->getNextSlideIndex();
137 else
139 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
141 if (rxSlide == Reference<drawing::XDrawPage>(
142 xSlideShowController->getSlideByIndex(nIndex), UNO_QUERY))
144 nNextSlideIndex = nIndex + 1;
148 if (nNextSlideIndex >= 0)
150 if (nNextSlideIndex < nCount)
152 xSlide = Reference<drawing::XDrawPage>(
153 xSlideShowController->getSlideByIndex(nNextSlideIndex),
154 UNO_QUERY);
158 PresenterSlidePreview::setCurrentPage(xSlide);
162 } // end of anonymous namespace
167 //===== PresenterViewFactory ==============================================
169 PresenterViewFactory::PresenterViewFactory (
170 const Reference<uno::XComponentContext>& rxContext,
171 const Reference<frame::XController>& rxController,
172 const ::rtl::Reference<PresenterController>& rpPresenterController)
173 : PresenterViewFactoryInterfaceBase(m_aMutex),
174 mxComponentContext(rxContext),
175 mxConfigurationController(),
176 mxControllerWeak(rxController),
177 mpPresenterController(rpPresenterController),
178 mpResourceCache()
185 Reference<drawing::framework::XResourceFactory> PresenterViewFactory::Create (
186 const Reference<uno::XComponentContext>& rxContext,
187 const Reference<frame::XController>& rxController,
188 const ::rtl::Reference<PresenterController>& rpPresenterController)
190 rtl::Reference<PresenterViewFactory> pFactory (
191 new PresenterViewFactory(rxContext,rxController,rpPresenterController));
192 pFactory->Register(rxController);
193 return Reference<drawing::framework::XResourceFactory>(
194 static_cast<XWeak*>(pFactory.get()), UNO_QUERY);
200 void PresenterViewFactory::Register (const Reference<frame::XController>& rxController)
204 // Get the configuration controller.
205 Reference<XControllerManager> xCM (rxController, UNO_QUERY_THROW);
206 mxConfigurationController = xCM->getConfigurationController();
207 if ( ! mxConfigurationController.is())
209 throw RuntimeException();
211 else
213 mxConfigurationController->addResourceFactory(msCurrentSlidePreviewViewURL, this);
214 mxConfigurationController->addResourceFactory(msNextSlidePreviewViewURL, this);
215 mxConfigurationController->addResourceFactory(msNotesViewURL, this);
216 mxConfigurationController->addResourceFactory(msToolBarViewURL, this);
217 mxConfigurationController->addResourceFactory(msSlideSorterURL, this);
218 mxConfigurationController->addResourceFactory(msHelpViewURL, this);
221 catch (RuntimeException&)
223 OSL_ASSERT(false);
224 if (mxConfigurationController.is())
225 mxConfigurationController->removeResourceFactoryForReference(this);
226 mxConfigurationController = NULL;
228 throw;
235 PresenterViewFactory::~PresenterViewFactory (void)
242 void SAL_CALL PresenterViewFactory::disposing (void)
243 throw (RuntimeException)
245 if (mxConfigurationController.is())
246 mxConfigurationController->removeResourceFactoryForReference(this);
247 mxConfigurationController = NULL;
249 if (mpResourceCache.get() != NULL)
251 // Dispose all views in the cache.
252 ResourceContainer::const_iterator iView (mpResourceCache->begin());
253 ResourceContainer::const_iterator iEnd (mpResourceCache->end());
254 for ( ; iView!=iEnd; ++iView)
258 Reference<lang::XComponent> xComponent (iView->second.first, UNO_QUERY);
259 if (xComponent.is())
260 xComponent->dispose();
262 catch (lang::DisposedException&)
266 mpResourceCache.reset();
273 //----- XViewFactory ----------------------------------------------------------
275 Reference<XResource> SAL_CALL PresenterViewFactory::createResource (
276 const Reference<XResourceId>& rxViewId)
277 throw (RuntimeException)
279 ThrowIfDisposed();
281 Reference<XResource> xView;
283 if (rxViewId.is())
285 Reference<XPane> xAnchorPane (
286 mxConfigurationController->getResource(rxViewId->getAnchor()),
287 UNO_QUERY_THROW);
288 xView = GetViewFromCache(rxViewId, xAnchorPane);
289 if (xView == NULL)
290 xView = CreateView(rxViewId, xAnchorPane);
292 // Activate the view.
293 PresenterPaneContainer::SharedPaneDescriptor pDescriptor (
294 mpPresenterController->GetPaneContainer()->FindPaneId(rxViewId->getAnchor()));
295 if (pDescriptor.get() != NULL)
296 pDescriptor->SetActivationState(true);
299 return xView;
305 void SAL_CALL PresenterViewFactory::releaseResource (const Reference<XResource>& rxView)
306 throw (RuntimeException)
308 ThrowIfDisposed();
310 if ( ! rxView.is())
311 return;
313 // Deactivate the view.
314 PresenterPaneContainer::SharedPaneDescriptor pDescriptor (
315 mpPresenterController->GetPaneContainer()->FindPaneId(
316 rxView->getResourceId()->getAnchor()));
317 if (pDescriptor.get() != NULL)
318 pDescriptor->SetActivationState(false);
320 // Dispose only views that we can not put into the cache.
321 CachablePresenterView* pView = dynamic_cast<CachablePresenterView*>(rxView.get());
322 if (pView == NULL || mpResourceCache.get()==NULL)
326 if (pView != NULL)
327 pView->ReleaseView();
328 Reference<lang::XComponent> xComponent (rxView, UNO_QUERY);
329 if (xComponent.is())
330 xComponent->dispose();
332 catch (lang::DisposedException&)
334 // Do not let disposed exceptions get out. It might be interpreted
335 // as coming from the factory, which would then be removed from the
336 // drawing framework.
339 else
341 // Put cachable views in the cache.
342 Reference<XResourceId> xViewId (rxView->getResourceId());
343 if (xViewId.is())
345 Reference<XPane> xAnchorPane (
346 mxConfigurationController->getResource(xViewId->getAnchor()),
347 UNO_QUERY_THROW);
348 (*mpResourceCache)[xViewId->getResourceURL()]
349 = ViewResourceDescriptor(Reference<XView>(rxView, UNO_QUERY), xAnchorPane);
350 pView->DeactivatePresenterView();
358 //-----------------------------------------------------------------------------
360 Reference<XResource> PresenterViewFactory::GetViewFromCache(
361 const Reference<XResourceId>& rxViewId,
362 const Reference<XPane>& rxAnchorPane) const
364 if (mpResourceCache.get() == NULL)
365 return NULL;
369 const OUString sResourceURL (rxViewId->getResourceURL());
371 // Can we use a view from the cache?
372 ResourceContainer::const_iterator iView (mpResourceCache->find(sResourceURL));
373 if (iView != mpResourceCache->end())
375 // The view is in the container but it can only be used if
376 // the anchor pane is the same now as it was at creation of
377 // the view.
378 if (iView->second.second == rxAnchorPane)
380 CachablePresenterView* pView
381 = dynamic_cast<CachablePresenterView*>(iView->second.first.get());
382 if (pView != NULL)
383 pView->ActivatePresenterView();
384 return Reference<XResource>(iView->second.first, UNO_QUERY);
387 // Right view, wrong pane. Create a new view.
390 catch (RuntimeException&)
393 return NULL;
399 Reference<XResource> PresenterViewFactory::CreateView(
400 const Reference<XResourceId>& rxViewId,
401 const Reference<XPane>& rxAnchorPane)
403 Reference<XView> xView;
407 const OUString sResourceURL (rxViewId->getResourceURL());
409 if (sResourceURL.equals(msCurrentSlidePreviewViewURL))
411 xView = CreateSlideShowView(rxViewId);
413 else if (sResourceURL.equals(msNotesViewURL))
415 xView = CreateNotesView(rxViewId, rxAnchorPane);
417 else if (sResourceURL.equals(msNextSlidePreviewViewURL))
419 xView = CreateSlidePreviewView(rxViewId, rxAnchorPane);
421 else if (sResourceURL.equals(msToolBarViewURL))
423 xView = CreateToolBarView(rxViewId);
425 else if (sResourceURL.equals(msSlideSorterURL))
427 xView = CreateSlideSorterView(rxViewId);
429 else if (sResourceURL.equals(msHelpViewURL))
431 xView = CreateHelpView(rxViewId);
434 // Activate it.
435 CachablePresenterView* pView = dynamic_cast<CachablePresenterView*>(xView.get());
436 if (pView != NULL)
437 pView->ActivatePresenterView();
439 catch (RuntimeException&)
441 xView = NULL;
444 return Reference<XResource>(xView, UNO_QUERY);
450 Reference<XView> PresenterViewFactory::CreateSlideShowView(
451 const Reference<XResourceId>& rxViewId) const
453 Reference<XView> xView;
455 if ( ! mxConfigurationController.is())
456 return xView;
457 if ( ! mxComponentContext.is())
458 return xView;
462 rtl::Reference<PresenterSlideShowView> pShowView (
463 new PresenterSlideShowView(
464 mxComponentContext,
465 rxViewId,
466 Reference<frame::XController>(mxControllerWeak),
467 mpPresenterController));
468 pShowView->LateInit();
469 xView = Reference<XView>(pShowView.get());
471 catch (RuntimeException&)
473 xView = NULL;
476 return xView;
482 Reference<XView> PresenterViewFactory::CreateSlidePreviewView(
483 const Reference<XResourceId>& rxViewId,
484 const Reference<XPane>& rxAnchorPane) const
486 Reference<XView> xView;
488 if ( ! mxConfigurationController.is())
489 return xView;
490 if ( ! mxComponentContext.is())
491 return xView;
495 xView = Reference<XView>(
496 static_cast<XWeak*>(new NextSlidePreview(
497 mxComponentContext,
498 rxViewId,
499 rxAnchorPane,
500 mpPresenterController)),
501 UNO_QUERY_THROW);
503 catch (RuntimeException&)
505 xView = NULL;
508 return xView;
514 Reference<XView> PresenterViewFactory::CreateToolBarView(
515 const Reference<XResourceId>& rxViewId) const
517 return new PresenterToolBarView(
518 mxComponentContext,
519 rxViewId,
520 Reference<frame::XController>(mxControllerWeak),
521 mpPresenterController);
527 Reference<XView> PresenterViewFactory::CreateNotesView(
528 const Reference<XResourceId>& rxViewId,
529 const Reference<XPane>& rxAnchorPane) const
531 (void)rxAnchorPane;
532 Reference<XView> xView;
534 if ( ! mxConfigurationController.is())
535 return xView;
536 if ( ! mxComponentContext.is())
537 return xView;
541 xView = Reference<XView>(static_cast<XWeak*>(
542 new PresenterNotesView(
543 mxComponentContext,
544 rxViewId,
545 Reference<frame::XController>(mxControllerWeak),
546 mpPresenterController)),
547 UNO_QUERY_THROW);
549 catch (RuntimeException&)
551 xView = NULL;
554 return xView;
560 Reference<XView> PresenterViewFactory::CreateSlideSorterView(
561 const Reference<XResourceId>& rxViewId) const
563 Reference<XView> xView;
565 if ( ! mxConfigurationController.is())
566 return xView;
567 if ( ! mxComponentContext.is())
568 return xView;
572 rtl::Reference<PresenterSlideSorter> pView (
573 new PresenterSlideSorter(
574 mxComponentContext,
575 rxViewId,
576 Reference<frame::XController>(mxControllerWeak),
577 mpPresenterController));
578 PresenterPaneContainer::SharedPaneDescriptor pDescriptor (
579 mpPresenterController->GetPaneContainer()->FindPaneId(rxViewId->getAnchor()));
580 if (pDescriptor.get() != NULL)
581 pDescriptor->maActivator = ::boost::bind(
582 &PresenterSlideSorter::SetActiveState, pView.get(), _1);
583 xView = pView.get();
585 catch (RuntimeException&)
587 xView = NULL;
590 return xView;
596 Reference<XView> PresenterViewFactory::CreateHelpView(
597 const Reference<XResourceId>& rxViewId) const
599 return Reference<XView>(new PresenterHelpView(
600 mxComponentContext,
601 rxViewId,
602 Reference<frame::XController>(mxControllerWeak),
603 mpPresenterController));
610 void PresenterViewFactory::ThrowIfDisposed (void) const
611 throw (::com::sun::star::lang::DisposedException)
613 if (rBHelper.bDisposed || rBHelper.bInDispose)
615 throw lang::DisposedException (
616 OUString(RTL_CONSTASCII_USTRINGPARAM(
617 "PresenterViewFactory object has already been disposed")),
618 const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
625 //===== CachablePresenterView =================================================
627 CachablePresenterView::CachablePresenterView (void)
628 : mbIsPresenterViewActive(true)
635 void CachablePresenterView::ActivatePresenterView (void)
637 mbIsPresenterViewActive = true;
643 void CachablePresenterView::DeactivatePresenterView (void)
645 mbIsPresenterViewActive = false;
651 void CachablePresenterView::ReleaseView (void)