1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5b190011SAndrew Rist  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19*5b190011SAndrew Rist  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sd.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "presenter/PresenterCanvasFactory.hxx"
27cdf0e10cSrcweir #include "PresenterCanvas.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/awt/Point.hpp>
30cdf0e10cSrcweir #include <com/sun/star/awt/WindowAttribute.hpp>
31cdf0e10cSrcweir #include <com/sun/star/awt/WindowClass.hpp>
32cdf0e10cSrcweir #include <com/sun/star/awt/WindowDescriptor.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
34cdf0e10cSrcweir #include <com/sun/star/rendering/CompositeOperation.hpp>
35cdf0e10cSrcweir #include <com/sun/star/drawing/CanvasFeature.hpp>
36cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
37cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
38cdf0e10cSrcweir #include <cppcanvas/vclfactory.hxx>
39cdf0e10cSrcweir #include <cppuhelper/basemutex.hxx>
40cdf0e10cSrcweir #include <cppuhelper/compbase3.hxx>
41cdf0e10cSrcweir #include <cppuhelper/compbase2.hxx>
42cdf0e10cSrcweir #include <rtl/ref.hxx>
43cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
44cdf0e10cSrcweir #include <vcl/svapp.hxx>
45cdf0e10cSrcweir #include <vcl/window.hxx>
46cdf0e10cSrcweir #include <vcl/wrkwin.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using namespace ::com::sun::star;
49cdf0e10cSrcweir using namespace ::com::sun::star::uno;
50cdf0e10cSrcweir using ::rtl::OUString;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace sd { namespace presenter {
53cdf0e10cSrcweir 
54cdf0e10cSrcweir //===== PresenterCanvasFactory::SharedWindowContainer =========================
55cdf0e10cSrcweir 
56cdf0e10cSrcweir namespace {
57cdf0e10cSrcweir     class SharedWindowDescriptor
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir     public:
60cdf0e10cSrcweir         Reference<awt::XWindow> mxSharedWindow;
61cdf0e10cSrcweir         Reference<rendering::XCanvas> mxSharedCanvas;
62cdf0e10cSrcweir     };
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir class PresenterCanvasFactory::SharedWindowContainer
66cdf0e10cSrcweir     : public ::std::vector<SharedWindowDescriptor>
67cdf0e10cSrcweir {
68cdf0e10cSrcweir public:
69cdf0e10cSrcweir     iterator FindDescriptor (const Reference<awt::XWindow>& rxWindow)
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         for (iterator iDescriptor=begin(); iDescriptor!=end(); ++iDescriptor)
72cdf0e10cSrcweir             if (iDescriptor->mxSharedWindow == rxWindow)
73cdf0e10cSrcweir                 return iDescriptor;
74cdf0e10cSrcweir         return end();
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir };
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 
81cdf0e10cSrcweir //===== PresenterCanvasFactory ================================================
82cdf0e10cSrcweir 
83cdf0e10cSrcweir class PresenterCanvasFactory::Deleter
84cdf0e10cSrcweir {
85cdf0e10cSrcweir public:
86cdf0e10cSrcweir     void operator() (const PresenterCanvasFactory* pObject) { delete pObject; }
87cdf0e10cSrcweir };
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
90cdf0e10cSrcweir ::boost::shared_ptr<PresenterCanvasFactory> PresenterCanvasFactory::mpInstance;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 
93cdf0e10cSrcweir ::boost::shared_ptr<PresenterCanvasFactory> PresenterCanvasFactory::Instance (void)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
96cdf0e10cSrcweir     if (mpInstance.get() == NULL)
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         mpInstance.reset(new PresenterCanvasFactory(), PresenterCanvasFactory::Deleter());
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     return mpInstance;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir void PresenterCanvasFactory::AddSharedWindow (
108cdf0e10cSrcweir     const Reference<awt::XWindow>& rxWindow,
109cdf0e10cSrcweir     const Reference<rendering::XCanvas>& rxCanvas)
110cdf0e10cSrcweir {
111cdf0e10cSrcweir     SharedWindowDescriptor aDescriptor;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     if (mpSharedWindows->FindDescriptor(rxWindow) != mpSharedWindows->end())
114cdf0e10cSrcweir         return;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     aDescriptor.mxSharedWindow = rxWindow;
117cdf0e10cSrcweir     aDescriptor.mxSharedCanvas = rxCanvas;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     // Store the new shared window only when both the window and the canvas
120cdf0e10cSrcweir     // are present.
121cdf0e10cSrcweir     if (aDescriptor.mxSharedCanvas.is() && aDescriptor.mxSharedCanvas.is())
122cdf0e10cSrcweir         mpSharedWindows->push_back(aDescriptor);
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
128cdf0e10cSrcweir void PresenterCanvasFactory::RemoveSharedWindow (const Reference<awt::XWindow>& rxWindow)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir     SharedWindowContainer::iterator iDescriptor = mpSharedWindows->FindDescriptor(rxWindow);
131cdf0e10cSrcweir     if (iDescriptor != mpSharedWindows->end())
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         mpSharedWindows->erase(iDescriptor);
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
140cdf0e10cSrcweir Reference<rendering::XCanvas> PresenterCanvasFactory::GetCanvas (
141cdf0e10cSrcweir     const css::uno::Reference<css::awt::XWindow>& rxSharedWindow,
142cdf0e10cSrcweir     const css::uno::Reference<css::awt::XWindow>& rxWindow,
143cdf0e10cSrcweir     const sal_Int16 nRequestedCanvasFeatures,
144cdf0e10cSrcweir     const rtl::OUString& rsCanvasServiceName)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     (void)nRequestedCanvasFeatures;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     Reference<rendering::XCanvas> xCanvas;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     if (rxSharedWindow.is() && rsCanvasServiceName.getLength()==0)
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         OSL_ASSERT(rxSharedWindow.is());
153cdf0e10cSrcweir         xCanvas = CreateSharedCanvas(rxSharedWindow, rxWindow);
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir     else
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         xCanvas = CreateCanvas(rxWindow, rsCanvasServiceName);
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     return xCanvas;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 
166cdf0e10cSrcweir Reference<rendering::XCanvas> PresenterCanvasFactory::CreateSharedCanvas (
167cdf0e10cSrcweir     const css::uno::Reference<css::awt::XWindow>& rxSharedWindow,
168cdf0e10cSrcweir     const css::uno::Reference<css::awt::XWindow>& rxWindow) const
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     // A shared window is given.  Look it up and determine which canvas
171cdf0e10cSrcweir     // to return.
172cdf0e10cSrcweir     SharedWindowContainer::iterator iDescriptor (
173cdf0e10cSrcweir         mpSharedWindows->FindDescriptor(rxSharedWindow));
174cdf0e10cSrcweir     if (iDescriptor != mpSharedWindows->end())
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         if (rxWindow == iDescriptor->mxSharedWindow || ! rxWindow.is())
177cdf0e10cSrcweir         {
178cdf0e10cSrcweir             // A shared window itself is given.  Return the previously
179cdf0e10cSrcweir             // created canvas.
180cdf0e10cSrcweir             return Reference<rendering::XCanvas>(iDescriptor->mxSharedCanvas, UNO_QUERY);
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir         else
183cdf0e10cSrcweir         {
184cdf0e10cSrcweir             // A true child window is given.  Create a canvas wrapper.
185cdf0e10cSrcweir             return new PresenterCanvas(
186cdf0e10cSrcweir                 Reference<rendering::XCanvas>(iDescriptor->mxSharedCanvas, UNO_QUERY),
187cdf0e10cSrcweir                 iDescriptor->mxSharedWindow,
188cdf0e10cSrcweir                 rxWindow);
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     return NULL;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
198cdf0e10cSrcweir Reference<rendering::XCanvas> PresenterCanvasFactory::CreateCanvasForSprite (
199cdf0e10cSrcweir     const css::uno::Reference<css::awt::XWindow>& rxSharedWindow,
200cdf0e10cSrcweir     const css::uno::Reference<css::awt::XWindow>& rxWindow) const
201cdf0e10cSrcweir {
202cdf0e10cSrcweir     OSL_ASSERT(rxSharedWindow.is());
203cdf0e10cSrcweir     (void)rxWindow.is();
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     SharedWindowContainer::iterator iDescriptor (
206cdf0e10cSrcweir         mpSharedWindows->FindDescriptor(rxSharedWindow));
207cdf0e10cSrcweir     if (iDescriptor != mpSharedWindows->end())
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         OSL_ASSERT(iDescriptor->mxSharedCanvas.is());
210cdf0e10cSrcweir         Reference<rendering::XSpriteCanvas> xSpriteCanvas(iDescriptor->mxSharedCanvas, UNO_QUERY);
211cdf0e10cSrcweir         if (xSpriteCanvas.is())
212cdf0e10cSrcweir         {
213cdf0e10cSrcweir             Reference<rendering::XCustomSprite> xSprite (
214cdf0e10cSrcweir                 xSpriteCanvas->createCustomSprite(geometry::RealSize2D(10,10)));
215cdf0e10cSrcweir             if (xSprite.is())
216cdf0e10cSrcweir             {
217cdf0e10cSrcweir                 return xSprite->getContentCanvas();
218cdf0e10cSrcweir             }
219cdf0e10cSrcweir         }
220cdf0e10cSrcweir     }
221cdf0e10cSrcweir     return NULL;
222cdf0e10cSrcweir }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 
227cdf0e10cSrcweir Reference<rendering::XCanvas> PresenterCanvasFactory::CreateCanvas (
228cdf0e10cSrcweir     const css::uno::Reference<css::awt::XWindow>& rxWindow,
229cdf0e10cSrcweir     const rtl::OUString& rsCanvasServiceName) const
230cdf0e10cSrcweir {
231cdf0e10cSrcweir     // No shared window is given or an explicit canvas service name is
232cdf0e10cSrcweir     // specified.  Create a new canvas.
233cdf0e10cSrcweir     ::Window* pWindow = VCLUnoHelper::GetWindow(rxWindow);
234cdf0e10cSrcweir     if (pWindow != NULL)
235cdf0e10cSrcweir     {
236cdf0e10cSrcweir         Sequence<Any> aArg (5);
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         // common: first any is VCL pointer to window (for VCL canvas)
239cdf0e10cSrcweir         aArg[0] = makeAny(reinterpret_cast<sal_Int64>(pWindow));
240cdf0e10cSrcweir         aArg[1] = Any();
241cdf0e10cSrcweir         aArg[2] = makeAny(::com::sun::star::awt::Rectangle());
242cdf0e10cSrcweir         aArg[3] = makeAny(sal_False);
243cdf0e10cSrcweir         aArg[4] = makeAny(rxWindow);
244cdf0e10cSrcweir 
245cdf0e10cSrcweir         Reference<lang::XMultiServiceFactory> xFactory (::comphelper::getProcessServiceFactory());
246cdf0e10cSrcweir         return Reference<rendering::XCanvas>(
247cdf0e10cSrcweir             xFactory->createInstanceWithArguments(
248cdf0e10cSrcweir                 rsCanvasServiceName.getLength()>0
249cdf0e10cSrcweir                     ? rsCanvasServiceName
250cdf0e10cSrcweir                         : OUString::createFromAscii("com.sun.star.rendering.VCLCanvas"),
251cdf0e10cSrcweir                 aArg),
252cdf0e10cSrcweir             UNO_QUERY);
253cdf0e10cSrcweir     }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     return NULL;
256cdf0e10cSrcweir }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 
261cdf0e10cSrcweir Reference<rendering::XCanvas> PresenterCanvasFactory::GetSharedCanvas (
262cdf0e10cSrcweir     const Reference<awt::XWindow>& rxSharedWindow)
263cdf0e10cSrcweir {
264cdf0e10cSrcweir     SharedWindowContainer::iterator iDescriptor = mpSharedWindows->FindDescriptor(rxSharedWindow);
265cdf0e10cSrcweir     if (iDescriptor != mpSharedWindows->end())
266cdf0e10cSrcweir         return Reference<rendering::XCanvas>(iDescriptor->mxSharedCanvas, UNO_QUERY);
267cdf0e10cSrcweir     else
268cdf0e10cSrcweir         return NULL;
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 
274cdf0e10cSrcweir Reference<rendering::XCanvas> PresenterCanvasFactory::GetSharedCanvas (
275cdf0e10cSrcweir     const Reference<rendering::XCanvas>& rxCanvas)
276cdf0e10cSrcweir {
277cdf0e10cSrcweir     PresenterCanvas* pCanvas = dynamic_cast<PresenterCanvas*>(rxCanvas.get());
278cdf0e10cSrcweir     if (pCanvas != NULL)
279cdf0e10cSrcweir         return pCanvas->GetSharedCanvas();
280cdf0e10cSrcweir     else
281cdf0e10cSrcweir         return NULL;
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 
287cdf0e10cSrcweir PresenterCanvasFactory::PresenterCanvasFactory (void)
288cdf0e10cSrcweir     : mpSharedWindows(new SharedWindowContainer())
289cdf0e10cSrcweir {
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 
295cdf0e10cSrcweir PresenterCanvasFactory::~PresenterCanvasFactory (void)
296cdf0e10cSrcweir {
297cdf0e10cSrcweir     mpSharedWindows.reset();
298cdf0e10cSrcweir }
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 
303cdf0e10cSrcweir } } // end of namespace ::sd::presenter
304