Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sd / source / console / PresenterPaneFactory.cxx
blob822055b8ed8de2fcbfb8154418059dea0b9db756
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "PresenterPaneFactory.hxx"
21 #include "PresenterController.hxx"
22 #include "PresenterPane.hxx"
23 #include "PresenterPaneBorderPainter.hxx"
24 #include "PresenterPaneContainer.hxx"
25 #include "PresenterSpritePane.hxx"
26 #include <DrawController.hxx>
27 #include <com/sun/star/lang/XComponent.hpp>
28 #include <utility>
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::drawing::framework;
35 namespace sdext::presenter {
37 //===== PresenterPaneFactory ==================================================
39 Reference<drawing::framework::XResourceFactory> PresenterPaneFactory::Create (
40 const Reference<uno::XComponentContext>& rxContext,
41 const rtl::Reference<::sd::DrawController>& rxController,
42 const ::rtl::Reference<PresenterController>& rpPresenterController)
44 rtl::Reference<PresenterPaneFactory> pFactory (
45 new PresenterPaneFactory(rxContext,rpPresenterController));
46 pFactory->Register(rxController);
47 return Reference<drawing::framework::XResourceFactory>(pFactory);
50 PresenterPaneFactory::PresenterPaneFactory (
51 const Reference<uno::XComponentContext>& rxContext,
52 ::rtl::Reference<PresenterController> xPresenterController)
53 : PresenterPaneFactoryInterfaceBase(m_aMutex),
54 mxComponentContextWeak(rxContext),
55 mpPresenterController(std::move(xPresenterController))
59 void PresenterPaneFactory::Register (const rtl::Reference<::sd::DrawController>& rxController)
61 Reference<XConfigurationController> xCC;
62 try
64 // Get the configuration controller.
65 xCC.set(rxController->getConfigurationController());
66 mxConfigurationControllerWeak = xCC;
67 if ( ! xCC.is())
69 throw RuntimeException();
71 xCC->addResourceFactory(
72 "private:resource/pane/Presenter/*",
73 this);
75 catch (RuntimeException&)
77 OSL_ASSERT(false);
78 if (xCC.is())
79 xCC->removeResourceFactoryForReference(this);
80 mxConfigurationControllerWeak = WeakReference<XConfigurationController>();
82 throw;
86 PresenterPaneFactory::~PresenterPaneFactory()
90 void SAL_CALL PresenterPaneFactory::disposing()
92 Reference<XConfigurationController> xCC (mxConfigurationControllerWeak);
93 if (xCC.is())
94 xCC->removeResourceFactoryForReference(this);
95 mxConfigurationControllerWeak = WeakReference<XConfigurationController>();
97 // Dispose the panes in the cache.
98 if (mpResourceCache != nullptr)
100 for (const auto& rxPane : *mpResourceCache)
102 Reference<lang::XComponent> xPaneComponent (rxPane.second, UNO_QUERY);
103 if (xPaneComponent.is())
104 xPaneComponent->dispose();
106 mpResourceCache.reset();
110 //----- XPaneFactory ----------------------------------------------------------
112 Reference<XResource> SAL_CALL PresenterPaneFactory::createResource (
113 const Reference<XResourceId>& rxPaneId)
115 ThrowIfDisposed();
117 if ( ! rxPaneId.is())
118 return nullptr;
120 const OUString sPaneURL (rxPaneId->getResourceURL());
121 if (sPaneURL.isEmpty())
122 return nullptr;
124 if (mpResourceCache != nullptr)
126 // Has the requested resource already been created?
127 ResourceContainer::const_iterator iResource (mpResourceCache->find(sPaneURL));
128 if (iResource != mpResourceCache->end())
130 // Yes. Mark it as active.
131 rtl::Reference<PresenterPaneContainer> pPaneContainer(
132 mpPresenterController->GetPaneContainer());
133 PresenterPaneContainer::SharedPaneDescriptor pDescriptor (
134 pPaneContainer->FindPaneURL(sPaneURL));
135 if (pDescriptor)
137 pDescriptor->SetActivationState(true);
138 if (pDescriptor->mxBorderWindow.is())
139 pDescriptor->mxBorderWindow->setVisible(true);
140 pPaneContainer->StorePane(pDescriptor->mxPane);
143 return iResource->second;
147 // No. Create a new one.
148 Reference<XResource> xResource = CreatePane(rxPaneId);
149 return xResource;
152 void SAL_CALL PresenterPaneFactory::releaseResource (const Reference<XResource>& rxResource)
154 ThrowIfDisposed();
156 if ( ! rxResource.is())
157 throw lang::IllegalArgumentException();
159 // Mark the pane as inactive.
160 rtl::Reference<PresenterPaneContainer> pPaneContainer(
161 mpPresenterController->GetPaneContainer());
162 const OUString sPaneURL (rxResource->getResourceId()->getResourceURL());
163 PresenterPaneContainer::SharedPaneDescriptor pDescriptor (
164 pPaneContainer->FindPaneURL(sPaneURL));
165 if (!pDescriptor)
166 return;
168 pDescriptor->SetActivationState(false);
169 if (pDescriptor->mxBorderWindow.is())
170 pDescriptor->mxBorderWindow->setVisible(false);
172 if (mpResourceCache != nullptr)
174 // Store the pane in the cache.
175 (*mpResourceCache)[sPaneURL] = rxResource;
177 else
179 // Dispose the pane.
180 Reference<lang::XComponent> xPaneComponent (rxResource, UNO_QUERY);
181 if (xPaneComponent.is())
182 xPaneComponent->dispose();
187 Reference<XResource> PresenterPaneFactory::CreatePane (
188 const Reference<XResourceId>& rxPaneId)
190 if ( ! rxPaneId.is())
191 return nullptr;
193 Reference<XConfigurationController> xCC (mxConfigurationControllerWeak);
194 if ( ! xCC.is())
195 return nullptr;
197 Reference<XComponentContext> xContext (mxComponentContextWeak);
198 if ( ! xContext.is())
199 return nullptr;
201 Reference<XPane> xParentPane (xCC->getResource(rxPaneId->getAnchor()), UNO_QUERY);
202 if ( ! xParentPane.is())
203 return nullptr;
207 return CreatePane(
208 rxPaneId,
209 xParentPane,
210 rxPaneId->getFullResourceURL().Arguments == "Sprite=1");
212 catch (Exception&)
214 OSL_ASSERT(false);
217 return nullptr;
220 Reference<XResource> PresenterPaneFactory::CreatePane (
221 const Reference<XResourceId>& rxPaneId,
222 const Reference<drawing::framework::XPane>& rxParentPane,
223 const bool bIsSpritePane)
225 Reference<XComponentContext> xContext (mxComponentContextWeak);
226 Reference<lang::XMultiComponentFactory> xFactory (
227 xContext->getServiceManager(), UNO_SET_THROW);
229 // Create a border window and canvas and store it in the pane
230 // container.
232 // Create the pane.
233 ::rtl::Reference<PresenterPaneBase> xPane;
234 if (bIsSpritePane)
236 xPane.set( new PresenterSpritePane(xContext, mpPresenterController));
238 else
240 xPane.set( new PresenterPane(xContext, mpPresenterController));
243 // Supply arguments.
244 Sequence<Any> aArguments{ Any(rxPaneId),
245 Any(rxParentPane->getWindow()),
246 Any(rxParentPane->getCanvas()),
247 Any(OUString()),
248 Any(Reference<drawing::framework::XPaneBorderPainter>(
249 mpPresenterController->GetPaneBorderPainter())),
250 Any(!bIsSpritePane) };
251 xPane->initialize(aArguments);
253 // Store pane and canvases and windows in container.
254 ::rtl::Reference<PresenterPaneContainer> pContainer (
255 mpPresenterController->GetPaneContainer());
256 PresenterPaneContainer::SharedPaneDescriptor pDescriptor(
257 pContainer->StoreBorderWindow(rxPaneId, xPane->GetBorderWindow()));
258 pContainer->StorePane(xPane);
259 if (pDescriptor)
261 pDescriptor->mbIsSprite = bIsSpritePane;
263 // Get the window of the frame and make that visible.
264 Reference<awt::XWindow> xWindow (pDescriptor->mxBorderWindow, UNO_SET_THROW);
265 xWindow->setVisible(true);
268 return Reference<XResource>(static_cast<XWeak*>(xPane.get()), UNO_QUERY_THROW);
271 void PresenterPaneFactory::ThrowIfDisposed() const
273 if (rBHelper.bDisposed || rBHelper.bInDispose)
275 throw lang::DisposedException (
276 "PresenterPaneFactory object has already been disposed",
277 const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
281 } // end of namespace sdext::presenter
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */