Update ooo320-m1
[ooovba.git] / sd / source / ui / presenter / PresenterCanvasFactory.cxx
blob799ca905d9c9b17afe86ffd3e109b33793579a99
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: PresenterCanvasFactory.cxx,v $
11 * $Revision: 1.3 $
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 #include "precompiled_sd.hxx"
34 #include "presenter/PresenterCanvasFactory.hxx"
35 #include "PresenterCanvas.hxx"
37 #include <com/sun/star/awt/Point.hpp>
38 #include <com/sun/star/awt/WindowAttribute.hpp>
39 #include <com/sun/star/awt/WindowClass.hpp>
40 #include <com/sun/star/awt/WindowDescriptor.hpp>
41 #include <com/sun/star/lang/XInitialization.hpp>
42 #include <com/sun/star/rendering/CompositeOperation.hpp>
43 #include <com/sun/star/drawing/CanvasFeature.hpp>
44 #include <com/sun/star/uno/XComponentContext.hpp>
45 #include <comphelper/processfactory.hxx>
46 #include <cppcanvas/vclfactory.hxx>
47 #include <cppuhelper/basemutex.hxx>
48 #include <cppuhelper/compbase3.hxx>
49 #include <cppuhelper/compbase2.hxx>
50 #include <rtl/ref.hxx>
51 #include <toolkit/helper/vclunohelper.hxx>
52 #include <vcl/svapp.hxx>
53 #include <vcl/window.hxx>
54 #include <vcl/wrkwin.hxx>
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::uno;
58 using ::rtl::OUString;
60 namespace sd { namespace presenter {
62 //===== PresenterCanvasFactory::SharedWindowContainer =========================
64 namespace {
65 class SharedWindowDescriptor
67 public:
68 Reference<awt::XWindow> mxSharedWindow;
69 Reference<rendering::XCanvas> mxSharedCanvas;
73 class PresenterCanvasFactory::SharedWindowContainer
74 : public ::std::vector<SharedWindowDescriptor>
76 public:
77 iterator FindDescriptor (const Reference<awt::XWindow>& rxWindow)
79 for (iterator iDescriptor=begin(); iDescriptor!=end(); ++iDescriptor)
80 if (iDescriptor->mxSharedWindow == rxWindow)
81 return iDescriptor;
82 return end();
89 //===== PresenterCanvasFactory ================================================
91 class PresenterCanvasFactory::Deleter
93 public:
94 void operator() (const PresenterCanvasFactory* pObject) { delete pObject; }
98 ::boost::shared_ptr<PresenterCanvasFactory> PresenterCanvasFactory::mpInstance;
101 ::boost::shared_ptr<PresenterCanvasFactory> PresenterCanvasFactory::Instance (void)
103 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
104 if (mpInstance.get() == NULL)
106 mpInstance.reset(new PresenterCanvasFactory(), PresenterCanvasFactory::Deleter());
109 return mpInstance;
115 void PresenterCanvasFactory::AddSharedWindow (
116 const Reference<awt::XWindow>& rxWindow,
117 const Reference<rendering::XCanvas>& rxCanvas)
119 SharedWindowDescriptor aDescriptor;
121 if (mpSharedWindows->FindDescriptor(rxWindow) != mpSharedWindows->end())
122 return;
124 aDescriptor.mxSharedWindow = rxWindow;
125 aDescriptor.mxSharedCanvas = rxCanvas;
127 // Store the new shared window only when both the window and the canvas
128 // are present.
129 if (aDescriptor.mxSharedCanvas.is() && aDescriptor.mxSharedCanvas.is())
130 mpSharedWindows->push_back(aDescriptor);
136 void PresenterCanvasFactory::RemoveSharedWindow (const Reference<awt::XWindow>& rxWindow)
138 SharedWindowContainer::iterator iDescriptor = mpSharedWindows->FindDescriptor(rxWindow);
139 if (iDescriptor != mpSharedWindows->end())
141 mpSharedWindows->erase(iDescriptor);
148 Reference<rendering::XCanvas> PresenterCanvasFactory::GetCanvas (
149 const css::uno::Reference<css::awt::XWindow>& rxSharedWindow,
150 const css::uno::Reference<css::awt::XWindow>& rxWindow,
151 const sal_Int16 nRequestedCanvasFeatures,
152 const rtl::OUString& rsCanvasServiceName)
154 (void)nRequestedCanvasFeatures;
156 Reference<rendering::XCanvas> xCanvas;
158 if (rxSharedWindow.is() && rsCanvasServiceName.getLength()==0)
160 OSL_ASSERT(rxSharedWindow.is());
161 xCanvas = CreateSharedCanvas(rxSharedWindow, rxWindow);
163 else
165 xCanvas = CreateCanvas(rxWindow, rsCanvasServiceName);
168 return xCanvas;
174 Reference<rendering::XCanvas> PresenterCanvasFactory::CreateSharedCanvas (
175 const css::uno::Reference<css::awt::XWindow>& rxSharedWindow,
176 const css::uno::Reference<css::awt::XWindow>& rxWindow) const
178 // A shared window is given. Look it up and determine which canvas
179 // to return.
180 SharedWindowContainer::iterator iDescriptor (
181 mpSharedWindows->FindDescriptor(rxSharedWindow));
182 if (iDescriptor != mpSharedWindows->end())
184 if (rxWindow == iDescriptor->mxSharedWindow || ! rxWindow.is())
186 // A shared window itself is given. Return the previously
187 // created canvas.
188 return Reference<rendering::XCanvas>(iDescriptor->mxSharedCanvas, UNO_QUERY);
190 else
192 // A true child window is given. Create a canvas wrapper.
193 return new PresenterCanvas(
194 Reference<rendering::XCanvas>(iDescriptor->mxSharedCanvas, UNO_QUERY),
195 iDescriptor->mxSharedWindow,
196 rxWindow);
200 return NULL;
206 Reference<rendering::XCanvas> PresenterCanvasFactory::CreateCanvasForSprite (
207 const css::uno::Reference<css::awt::XWindow>& rxSharedWindow,
208 const css::uno::Reference<css::awt::XWindow>& rxWindow) const
210 OSL_ASSERT(rxSharedWindow.is());
211 (void)rxWindow.is();
213 SharedWindowContainer::iterator iDescriptor (
214 mpSharedWindows->FindDescriptor(rxSharedWindow));
215 if (iDescriptor != mpSharedWindows->end())
217 OSL_ASSERT(iDescriptor->mxSharedCanvas.is());
218 Reference<rendering::XSpriteCanvas> xSpriteCanvas(iDescriptor->mxSharedCanvas, UNO_QUERY);
219 if (xSpriteCanvas.is())
221 Reference<rendering::XCustomSprite> xSprite (
222 xSpriteCanvas->createCustomSprite(geometry::RealSize2D(10,10)));
223 if (xSprite.is())
225 return xSprite->getContentCanvas();
229 return NULL;
235 Reference<rendering::XCanvas> PresenterCanvasFactory::CreateCanvas (
236 const css::uno::Reference<css::awt::XWindow>& rxWindow,
237 const rtl::OUString& rsCanvasServiceName) const
239 // No shared window is given or an explicit canvas service name is
240 // specified. Create a new canvas.
241 ::Window* pWindow = VCLUnoHelper::GetWindow(rxWindow);
242 if (pWindow != NULL)
244 Sequence<Any> aArg (5);
246 // common: first any is VCL pointer to window (for VCL canvas)
247 aArg[0] = makeAny(reinterpret_cast<sal_Int64>(pWindow));
248 aArg[1] = Any();
249 aArg[2] = makeAny(::com::sun::star::awt::Rectangle());
250 aArg[3] = makeAny(sal_False);
251 aArg[4] = makeAny(rxWindow);
253 Reference<lang::XMultiServiceFactory> xFactory (::comphelper::getProcessServiceFactory());
254 return Reference<rendering::XCanvas>(
255 xFactory->createInstanceWithArguments(
256 rsCanvasServiceName.getLength()>0
257 ? rsCanvasServiceName
258 : OUString::createFromAscii("com.sun.star.rendering.VCLCanvas"),
259 aArg),
260 UNO_QUERY);
263 return NULL;
269 Reference<rendering::XCanvas> PresenterCanvasFactory::GetSharedCanvas (
270 const Reference<awt::XWindow>& rxSharedWindow)
272 SharedWindowContainer::iterator iDescriptor = mpSharedWindows->FindDescriptor(rxSharedWindow);
273 if (iDescriptor != mpSharedWindows->end())
274 return Reference<rendering::XCanvas>(iDescriptor->mxSharedCanvas, UNO_QUERY);
275 else
276 return NULL;
282 Reference<rendering::XCanvas> PresenterCanvasFactory::GetSharedCanvas (
283 const Reference<rendering::XCanvas>& rxCanvas)
285 PresenterCanvas* pCanvas = dynamic_cast<PresenterCanvas*>(rxCanvas.get());
286 if (pCanvas != NULL)
287 return pCanvas->GetSharedCanvas();
288 else
289 return NULL;
295 PresenterCanvasFactory::PresenterCanvasFactory (void)
296 : mpSharedWindows(new SharedWindowContainer())
303 PresenterCanvasFactory::~PresenterCanvasFactory (void)
305 mpSharedWindows.reset();
311 } } // end of namespace ::sd::presenter