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