1*70f497fbSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*70f497fbSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*70f497fbSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*70f497fbSAndrew Rist  * distributed with this work for additional information
6*70f497fbSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*70f497fbSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*70f497fbSAndrew Rist  * "License"); you may not use this file except in compliance
9*70f497fbSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*70f497fbSAndrew Rist  *
11*70f497fbSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*70f497fbSAndrew Rist  *
13*70f497fbSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*70f497fbSAndrew Rist  * software distributed under the License is distributed on an
15*70f497fbSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*70f497fbSAndrew Rist  * KIND, either express or implied.  See the License for the
17*70f497fbSAndrew Rist  * specific language governing permissions and limitations
18*70f497fbSAndrew Rist  * under the License.
19*70f497fbSAndrew Rist  *
20*70f497fbSAndrew Rist  *************************************************************/
21*70f497fbSAndrew Rist 
22*70f497fbSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // must be first
28cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
29cdf0e10cSrcweir #include <tools/diagnose_ex.h>
30cdf0e10cSrcweir #include <canvas/debug.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <comphelper/anytostring.hxx>
33cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
36cdf0e10cSrcweir #include <basegfx/range/b2irange.hxx>
37cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <cppcanvas/spritecanvas.hxx>
40cdf0e10cSrcweir #include <canvas/canvastools.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
43cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
44cdf0e10cSrcweir #include <com/sun/star/rendering/XCanvas.hpp>
45cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
47cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
48cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
49cdf0e10cSrcweir #include <com/sun/star/awt/WindowDescriptor.hpp>
50cdf0e10cSrcweir #include <com/sun/star/awt/XToolkit.hpp>
51cdf0e10cSrcweir #include <com/sun/star/awt/XWindow2.hpp>
52cdf0e10cSrcweir #include <com/sun/star/awt/XWindowPeer.hpp>
53cdf0e10cSrcweir #include <com/sun/star/awt/WindowAttribute.hpp>
54cdf0e10cSrcweir #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
55cdf0e10cSrcweir #include <com/sun/star/awt/PosSize.hpp>
56cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
57cdf0e10cSrcweir #include <com/sun/star/frame/XSynchronousFrameLoader.hpp>
58cdf0e10cSrcweir 
59cdf0e10cSrcweir #include "viewappletshape.hxx"
60cdf0e10cSrcweir #include "tools.hxx"
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 
63cdf0e10cSrcweir using namespace ::com::sun::star;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir namespace slideshow
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     namespace internal
68cdf0e10cSrcweir     {
ViewAppletShape(const ViewLayerSharedPtr & rViewLayer,const uno::Reference<drawing::XShape> & rxShape,const::rtl::OUString & rServiceName,const char ** pPropCopyTable,sal_Size nNumPropEntries,const uno::Reference<uno::XComponentContext> & rxContext)69cdf0e10cSrcweir         ViewAppletShape::ViewAppletShape( const ViewLayerSharedPtr&                       rViewLayer,
70cdf0e10cSrcweir                                           const uno::Reference< drawing::XShape >&        rxShape,
71cdf0e10cSrcweir                                           const ::rtl::OUString&                          rServiceName,
72cdf0e10cSrcweir                                           const char**                                    pPropCopyTable,
73cdf0e10cSrcweir                                           sal_Size                                        nNumPropEntries,
74cdf0e10cSrcweir                                           const uno::Reference< uno::XComponentContext >& rxContext ) :
75cdf0e10cSrcweir 			mpViewLayer( rViewLayer ),
76cdf0e10cSrcweir             mxViewer(),
77cdf0e10cSrcweir             mxFrame(),
78cdf0e10cSrcweir             mxComponentContext( rxContext )
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             ENSURE_OR_THROW( rxShape.is(), "ViewAppletShape::ViewAppletShape(): Invalid Shape" );
81cdf0e10cSrcweir             ENSURE_OR_THROW( mpViewLayer, "ViewAppletShape::ViewAppletShape(): Invalid View" );
82cdf0e10cSrcweir             ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewAppletShape::ViewAppletShape(): Invalid ViewLayer canvas" );
83cdf0e10cSrcweir             ENSURE_OR_THROW( mxComponentContext.is(), "ViewAppletShape::ViewAppletShape(): Invalid component context" );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir             uno::Reference<lang::XMultiComponentFactory> xFactory(
86cdf0e10cSrcweir                 mxComponentContext->getServiceManager(),
87cdf0e10cSrcweir                 uno::UNO_QUERY_THROW );
88cdf0e10cSrcweir 
89cdf0e10cSrcweir             mxViewer.set( xFactory->createInstanceWithContext( rServiceName,
90cdf0e10cSrcweir                                                                mxComponentContext),
91cdf0e10cSrcweir                           uno::UNO_QUERY_THROW );
92cdf0e10cSrcweir 
93cdf0e10cSrcweir             uno::Reference< beans::XPropertySet > xShapePropSet( rxShape,
94cdf0e10cSrcweir                                                                  uno::UNO_QUERY_THROW );
95cdf0e10cSrcweir             uno::Reference< beans::XPropertySet > mxViewerPropSet( mxViewer,
96cdf0e10cSrcweir                                                                    uno::UNO_QUERY_THROW );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             // copy shape properties to applet viewer
99cdf0e10cSrcweir             ::rtl::OUString aPropName;
100cdf0e10cSrcweir             for( sal_Size i=0; i<nNumPropEntries; ++i )
101cdf0e10cSrcweir             {
102cdf0e10cSrcweir                 aPropName = ::rtl::OUString::createFromAscii( pPropCopyTable[i] );
103cdf0e10cSrcweir                 mxViewerPropSet->setPropertyValue( aPropName,
104cdf0e10cSrcweir                                                    xShapePropSet->getPropertyValue(
105cdf0e10cSrcweir                                                        aPropName ));
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		// ---------------------------------------------------------------------
110cdf0e10cSrcweir 
~ViewAppletShape()111cdf0e10cSrcweir 		ViewAppletShape::~ViewAppletShape()
112cdf0e10cSrcweir 		{
113cdf0e10cSrcweir             try
114cdf0e10cSrcweir             {
115cdf0e10cSrcweir                 endApplet();
116cdf0e10cSrcweir             }
117cdf0e10cSrcweir             catch (uno::Exception &)
118cdf0e10cSrcweir             {
119cdf0e10cSrcweir                 OSL_ENSURE( false, rtl::OUStringToOString(
120cdf0e10cSrcweir                                 comphelper::anyToString(
121cdf0e10cSrcweir                                     cppu::getCaughtException() ),
122cdf0e10cSrcweir                                 RTL_TEXTENCODING_UTF8 ).getStr() );
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir 		}
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 		// ---------------------------------------------------------------------
127cdf0e10cSrcweir 
getViewLayer() const128cdf0e10cSrcweir         ViewLayerSharedPtr ViewAppletShape::getViewLayer() const
129cdf0e10cSrcweir         {
130cdf0e10cSrcweir             return mpViewLayer;
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		// ---------------------------------------------------------------------
134cdf0e10cSrcweir 
startApplet(const::basegfx::B2DRectangle & rBounds)135cdf0e10cSrcweir         bool ViewAppletShape::startApplet( const ::basegfx::B2DRectangle& rBounds )
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             ENSURE_OR_RETURN_FALSE( mpViewLayer && mpViewLayer->getCanvas() && mpViewLayer->getCanvas()->getUNOCanvas().is(),
138cdf0e10cSrcweir                                "ViewAppletShape::startApplet(): Invalid or disposed view" );
139cdf0e10cSrcweir             try
140cdf0e10cSrcweir             {
141cdf0e10cSrcweir                 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
144cdf0e10cSrcweir                                                                 uno::UNO_QUERY_THROW );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir                 uno::Reference< awt::XWindow2 > xParentWindow(
147cdf0e10cSrcweir                     xPropSet->getPropertyValue(
148cdf0e10cSrcweir                         ::rtl::OUString::createFromAscii( "Window" )),
149cdf0e10cSrcweir                     uno::UNO_QUERY_THROW );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir                 uno::Reference<lang::XMultiComponentFactory> xFactory(
152cdf0e10cSrcweir                     mxComponentContext->getServiceManager() );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir                 if( xFactory.is() )
155cdf0e10cSrcweir                 {
156cdf0e10cSrcweir                     // create an awt window to contain the applet
157cdf0e10cSrcweir                     // ==========================================
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                     uno::Reference< awt::XToolkit > xToolkit(
160cdf0e10cSrcweir                         xFactory->createInstanceWithContext(
161cdf0e10cSrcweir                             ::rtl::OUString::createFromAscii( "com.sun.star.awt.Toolkit" ),
162cdf0e10cSrcweir                             mxComponentContext ),
163cdf0e10cSrcweir                         uno::UNO_QUERY_THROW );
164cdf0e10cSrcweir 
165cdf0e10cSrcweir                     awt::WindowDescriptor aOwnWinDescriptor( awt::WindowClass_SIMPLE,
166cdf0e10cSrcweir                                                              ::rtl::OUString(),
167cdf0e10cSrcweir                                                              uno::Reference< awt::XWindowPeer >(xParentWindow,
168cdf0e10cSrcweir                                                                                                 uno::UNO_QUERY_THROW),
169cdf0e10cSrcweir                                                              0,
170cdf0e10cSrcweir                                                              awt::Rectangle(),
171cdf0e10cSrcweir                                                              awt::WindowAttribute::SHOW
172cdf0e10cSrcweir                                                              | awt::VclWindowPeerAttribute::CLIPCHILDREN );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir                     uno::Reference< awt::XWindowPeer > xNewWinPeer(
175cdf0e10cSrcweir                         xToolkit->createWindow( aOwnWinDescriptor ));
176cdf0e10cSrcweir                     uno::Reference< awt::XWindow > xOwnWindow( xNewWinPeer,
177cdf0e10cSrcweir                                                                uno::UNO_QUERY_THROW );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 
180cdf0e10cSrcweir                     // create a frame, and load the applet into it
181cdf0e10cSrcweir                     // ===========================================
182cdf0e10cSrcweir 
183cdf0e10cSrcweir                     mxFrame.set(
184cdf0e10cSrcweir                         xFactory->createInstanceWithContext(
185cdf0e10cSrcweir                             ::rtl::OUString::createFromAscii( "com.sun.star.frame.Frame" ),
186cdf0e10cSrcweir                             mxComponentContext ),
187cdf0e10cSrcweir                         uno::UNO_QUERY_THROW );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir                     mxFrame->initialize( xOwnWindow );
190cdf0e10cSrcweir 
191cdf0e10cSrcweir                     uno::Reference < frame::XSynchronousFrameLoader > xLoader( mxViewer,
192cdf0e10cSrcweir                                                                                uno::UNO_QUERY_THROW );
193cdf0e10cSrcweir                     xLoader->load( uno::Sequence < beans::PropertyValue >(),
194cdf0e10cSrcweir                                    mxFrame );
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
197cdf0e10cSrcweir                     // resize surrounding window and applet to current shape size
198cdf0e10cSrcweir                     // ==========================================================
199cdf0e10cSrcweir 
200cdf0e10cSrcweir                     ::basegfx::B2DRange aTmpRange;
201cdf0e10cSrcweir                     ::canvas::tools::calcTransformedRectBounds( aTmpRange,
202cdf0e10cSrcweir                                                                 rBounds,
203cdf0e10cSrcweir                                                                 mpViewLayer->getTransformation() );
204cdf0e10cSrcweir                     const ::basegfx::B2IRange& rPixelBounds(
205cdf0e10cSrcweir                         ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
206cdf0e10cSrcweir 
207cdf0e10cSrcweir                     uno::Reference< awt::XWindow > xSurroundingWindow( mxFrame->getContainerWindow() );
208cdf0e10cSrcweir                     if( xSurroundingWindow.is() )
209cdf0e10cSrcweir                         xSurroundingWindow->setPosSize( static_cast<sal_Int32>(rPixelBounds.getMinX()),
210cdf0e10cSrcweir                                                         static_cast<sal_Int32>(rPixelBounds.getMinY()),
211cdf0e10cSrcweir                                                         static_cast<sal_Int32>(rPixelBounds.getWidth()),
212cdf0e10cSrcweir                                                         static_cast<sal_Int32>(rPixelBounds.getHeight()),
213cdf0e10cSrcweir                                                         awt::PosSize::POSSIZE );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir                     uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
216cdf0e10cSrcweir                     if( xAppletWindow.is() )
217cdf0e10cSrcweir                         xAppletWindow->setPosSize( 0, 0,
218cdf0e10cSrcweir                                                    static_cast<sal_Int32>(rPixelBounds.getWidth()),
219cdf0e10cSrcweir                                                    static_cast<sal_Int32>(rPixelBounds.getHeight()),
220cdf0e10cSrcweir                                                    awt::PosSize::POSSIZE );
221cdf0e10cSrcweir                 }
222cdf0e10cSrcweir             }
223cdf0e10cSrcweir             catch (uno::Exception &)
224cdf0e10cSrcweir             {
225cdf0e10cSrcweir                 return false;
226cdf0e10cSrcweir             }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir             return true;
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 		// ---------------------------------------------------------------------
232cdf0e10cSrcweir 
endApplet()233cdf0e10cSrcweir         void ViewAppletShape::endApplet()
234cdf0e10cSrcweir         {
235cdf0e10cSrcweir             uno::Reference<util::XCloseable> xCloseable(
236cdf0e10cSrcweir                 mxFrame,
237cdf0e10cSrcweir                 uno::UNO_QUERY );
238cdf0e10cSrcweir 
239cdf0e10cSrcweir             if( xCloseable.is() )
240cdf0e10cSrcweir             {
241cdf0e10cSrcweir                 xCloseable->close( sal_True );
242cdf0e10cSrcweir                 mxFrame.clear();
243cdf0e10cSrcweir             }
244cdf0e10cSrcweir         }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 		// ---------------------------------------------------------------------
247cdf0e10cSrcweir 
render(const::basegfx::B2DRectangle & rBounds) const248cdf0e10cSrcweir         bool ViewAppletShape::render( const ::basegfx::B2DRectangle& rBounds ) const
249cdf0e10cSrcweir         {
250cdf0e10cSrcweir             ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
251cdf0e10cSrcweir 
252cdf0e10cSrcweir             if( !pCanvas )
253cdf0e10cSrcweir                 return false;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir             if( !mxFrame.is() )
256cdf0e10cSrcweir             {
257cdf0e10cSrcweir                 // fill the shape background with black
258cdf0e10cSrcweir                 fillRect( pCanvas,
259cdf0e10cSrcweir                           rBounds,
260cdf0e10cSrcweir                           0xFFFFFFFFU );
261cdf0e10cSrcweir             }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir             return true;
264cdf0e10cSrcweir         }
265cdf0e10cSrcweir 
resize(const::basegfx::B2DRectangle & rBounds) const266cdf0e10cSrcweir         bool ViewAppletShape::resize( const ::basegfx::B2DRectangle& rBounds ) const
267cdf0e10cSrcweir         {
268cdf0e10cSrcweir             if( !mxFrame.is() )
269cdf0e10cSrcweir                 return false;
270cdf0e10cSrcweir 
271cdf0e10cSrcweir             ::basegfx::B2DRange aTmpRange;
272cdf0e10cSrcweir             ::canvas::tools::calcTransformedRectBounds( aTmpRange,
273cdf0e10cSrcweir                                                         rBounds,
274cdf0e10cSrcweir                                                         mpViewLayer->getTransformation() );
275cdf0e10cSrcweir             const ::basegfx::B2IRange& rPixelBounds(
276cdf0e10cSrcweir                 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
277cdf0e10cSrcweir 
278cdf0e10cSrcweir             uno::Reference< awt::XWindow > xFrameWindow( mxFrame->getContainerWindow() );
279cdf0e10cSrcweir             if( xFrameWindow.is() )
280cdf0e10cSrcweir                 xFrameWindow->setPosSize( static_cast<sal_Int32>(rPixelBounds.getMinX()),
281cdf0e10cSrcweir                                           static_cast<sal_Int32>(rPixelBounds.getMinY()),
282cdf0e10cSrcweir                                           static_cast<sal_Int32>(rPixelBounds.getWidth()),
283cdf0e10cSrcweir                                           static_cast<sal_Int32>(rPixelBounds.getHeight()),
284cdf0e10cSrcweir                                           awt::PosSize::POSSIZE );
285cdf0e10cSrcweir 
286cdf0e10cSrcweir             uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
287cdf0e10cSrcweir             if( xAppletWindow.is() )
288cdf0e10cSrcweir                 xAppletWindow->setPosSize( 0, 0,
289cdf0e10cSrcweir                                            static_cast<sal_Int32>(rPixelBounds.getWidth()),
290cdf0e10cSrcweir                                            static_cast<sal_Int32>(rPixelBounds.getHeight()),
291cdf0e10cSrcweir                                            awt::PosSize::POSSIZE );
292cdf0e10cSrcweir 
293cdf0e10cSrcweir             return true;
294cdf0e10cSrcweir         }
295cdf0e10cSrcweir 	}
296cdf0e10cSrcweir }
297