Update ooo320-m1
[ooovba.git] / slideshow / source / engine / shapes / viewappletshape.cxx
blob3055383bd9f0a4ab95c02b83d76c0a9106f65e2b
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: viewappletshape.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
34 // must be first
35 #include <canvas/verbosetrace.hxx>
36 #include <tools/diagnose_ex.h>
37 #include <canvas/debug.hxx>
39 #include <comphelper/anytostring.hxx>
40 #include <cppuhelper/exc_hlp.hxx>
42 #include <basegfx/matrix/b2dhommatrix.hxx>
43 #include <basegfx/range/b2irange.hxx>
44 #include <basegfx/tools/canvastools.hxx>
46 #include <cppcanvas/spritecanvas.hxx>
47 #include <canvas/canvastools.hxx>
49 #include <com/sun/star/uno/XComponentContext.hpp>
50 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
51 #include <com/sun/star/rendering/XCanvas.hpp>
52 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/beans/XPropertySet.hpp>
54 #include <com/sun/star/beans/PropertyValue.hpp>
55 #include <com/sun/star/util/XCloseable.hpp>
56 #include <com/sun/star/awt/WindowDescriptor.hpp>
57 #include <com/sun/star/awt/XToolkit.hpp>
58 #include <com/sun/star/awt/XWindow2.hpp>
59 #include <com/sun/star/awt/XWindowPeer.hpp>
60 #include <com/sun/star/awt/WindowAttribute.hpp>
61 #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
62 #include <com/sun/star/awt/PosSize.hpp>
63 #include <com/sun/star/frame/XFrame.hpp>
64 #include <com/sun/star/frame/XSynchronousFrameLoader.hpp>
66 #include "viewappletshape.hxx"
67 #include "tools.hxx"
70 using namespace ::com::sun::star;
72 namespace slideshow
74 namespace internal
76 ViewAppletShape::ViewAppletShape( const ViewLayerSharedPtr& rViewLayer,
77 const uno::Reference< drawing::XShape >& rxShape,
78 const ::rtl::OUString& rServiceName,
79 const char** pPropCopyTable,
80 sal_Size nNumPropEntries,
81 const uno::Reference< uno::XComponentContext >& rxContext ) :
82 mpViewLayer( rViewLayer ),
83 mxViewer(),
84 mxFrame(),
85 mxComponentContext( rxContext )
87 ENSURE_OR_THROW( rxShape.is(), "ViewAppletShape::ViewAppletShape(): Invalid Shape" );
88 ENSURE_OR_THROW( mpViewLayer, "ViewAppletShape::ViewAppletShape(): Invalid View" );
89 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewAppletShape::ViewAppletShape(): Invalid ViewLayer canvas" );
90 ENSURE_OR_THROW( mxComponentContext.is(), "ViewAppletShape::ViewAppletShape(): Invalid component context" );
92 uno::Reference<lang::XMultiComponentFactory> xFactory(
93 mxComponentContext->getServiceManager(),
94 uno::UNO_QUERY_THROW );
96 mxViewer.set( xFactory->createInstanceWithContext( rServiceName,
97 mxComponentContext),
98 uno::UNO_QUERY_THROW );
100 uno::Reference< beans::XPropertySet > xShapePropSet( rxShape,
101 uno::UNO_QUERY_THROW );
102 uno::Reference< beans::XPropertySet > mxViewerPropSet( mxViewer,
103 uno::UNO_QUERY_THROW );
105 // copy shape properties to applet viewer
106 ::rtl::OUString aPropName;
107 for( sal_Size i=0; i<nNumPropEntries; ++i )
109 aPropName = ::rtl::OUString::createFromAscii( pPropCopyTable[i] );
110 mxViewerPropSet->setPropertyValue( aPropName,
111 xShapePropSet->getPropertyValue(
112 aPropName ));
116 // ---------------------------------------------------------------------
118 ViewAppletShape::~ViewAppletShape()
122 endApplet();
124 catch (uno::Exception &)
126 OSL_ENSURE( false, rtl::OUStringToOString(
127 comphelper::anyToString(
128 cppu::getCaughtException() ),
129 RTL_TEXTENCODING_UTF8 ).getStr() );
133 // ---------------------------------------------------------------------
135 ViewLayerSharedPtr ViewAppletShape::getViewLayer() const
137 return mpViewLayer;
140 // ---------------------------------------------------------------------
142 bool ViewAppletShape::startApplet( const ::basegfx::B2DRectangle& rBounds )
144 ENSURE_OR_RETURN( mpViewLayer && mpViewLayer->getCanvas() && mpViewLayer->getCanvas()->getUNOCanvas().is(),
145 "ViewAppletShape::startApplet(): Invalid or disposed view" );
148 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
150 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
151 uno::UNO_QUERY_THROW );
153 uno::Reference< awt::XWindow2 > xParentWindow(
154 xPropSet->getPropertyValue(
155 ::rtl::OUString::createFromAscii( "Window" )),
156 uno::UNO_QUERY_THROW );
158 uno::Reference<lang::XMultiComponentFactory> xFactory(
159 mxComponentContext->getServiceManager() );
161 if( xFactory.is() )
163 // create an awt window to contain the applet
164 // ==========================================
166 uno::Reference< awt::XToolkit > xToolkit(
167 xFactory->createInstanceWithContext(
168 ::rtl::OUString::createFromAscii( "com.sun.star.awt.Toolkit" ),
169 mxComponentContext ),
170 uno::UNO_QUERY_THROW );
172 awt::WindowDescriptor aOwnWinDescriptor( awt::WindowClass_SIMPLE,
173 ::rtl::OUString(),
174 uno::Reference< awt::XWindowPeer >(xParentWindow,
175 uno::UNO_QUERY_THROW),
177 awt::Rectangle(),
178 awt::WindowAttribute::SHOW
179 | awt::VclWindowPeerAttribute::CLIPCHILDREN );
181 uno::Reference< awt::XWindowPeer > xNewWinPeer(
182 xToolkit->createWindow( aOwnWinDescriptor ));
183 uno::Reference< awt::XWindow > xOwnWindow( xNewWinPeer,
184 uno::UNO_QUERY_THROW );
187 // create a frame, and load the applet into it
188 // ===========================================
190 mxFrame.set(
191 xFactory->createInstanceWithContext(
192 ::rtl::OUString::createFromAscii( "com.sun.star.frame.Frame" ),
193 mxComponentContext ),
194 uno::UNO_QUERY_THROW );
196 mxFrame->initialize( xOwnWindow );
198 uno::Reference < frame::XSynchronousFrameLoader > xLoader( mxViewer,
199 uno::UNO_QUERY_THROW );
200 xLoader->load( uno::Sequence < beans::PropertyValue >(),
201 mxFrame );
204 // resize surrounding window and applet to current shape size
205 // ==========================================================
207 ::basegfx::B2DRange aTmpRange;
208 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
209 rBounds,
210 mpViewLayer->getTransformation() );
211 const ::basegfx::B2IRange& rPixelBounds(
212 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
214 uno::Reference< awt::XWindow > xSurroundingWindow( mxFrame->getContainerWindow() );
215 if( xSurroundingWindow.is() )
216 xSurroundingWindow->setPosSize( static_cast<sal_Int32>(rPixelBounds.getMinX()),
217 static_cast<sal_Int32>(rPixelBounds.getMinY()),
218 static_cast<sal_Int32>(rPixelBounds.getWidth()),
219 static_cast<sal_Int32>(rPixelBounds.getHeight()),
220 awt::PosSize::POSSIZE );
222 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
223 if( xAppletWindow.is() )
224 xAppletWindow->setPosSize( 0, 0,
225 static_cast<sal_Int32>(rPixelBounds.getWidth()),
226 static_cast<sal_Int32>(rPixelBounds.getHeight()),
227 awt::PosSize::POSSIZE );
230 catch (uno::Exception &)
232 return false;
235 return true;
238 // ---------------------------------------------------------------------
240 void ViewAppletShape::endApplet()
242 uno::Reference<util::XCloseable> xCloseable(
243 mxFrame,
244 uno::UNO_QUERY );
246 if( xCloseable.is() )
248 xCloseable->close( sal_True );
249 mxFrame.clear();
253 // ---------------------------------------------------------------------
255 bool ViewAppletShape::render( const ::basegfx::B2DRectangle& rBounds ) const
257 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
259 if( !pCanvas )
260 return false;
262 if( !mxFrame.is() )
264 // fill the shape background with black
265 fillRect( pCanvas,
266 rBounds,
267 0xFFFFFFFFU );
270 return true;
273 bool ViewAppletShape::resize( const ::basegfx::B2DRectangle& rBounds ) const
275 if( !mxFrame.is() )
276 return false;
278 ::basegfx::B2DRange aTmpRange;
279 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
280 rBounds,
281 mpViewLayer->getTransformation() );
282 const ::basegfx::B2IRange& rPixelBounds(
283 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
285 uno::Reference< awt::XWindow > xFrameWindow( mxFrame->getContainerWindow() );
286 if( xFrameWindow.is() )
287 xFrameWindow->setPosSize( static_cast<sal_Int32>(rPixelBounds.getMinX()),
288 static_cast<sal_Int32>(rPixelBounds.getMinY()),
289 static_cast<sal_Int32>(rPixelBounds.getWidth()),
290 static_cast<sal_Int32>(rPixelBounds.getHeight()),
291 awt::PosSize::POSSIZE );
293 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
294 if( xAppletWindow.is() )
295 xAppletWindow->setPosSize( 0, 0,
296 static_cast<sal_Int32>(rPixelBounds.getWidth()),
297 static_cast<sal_Int32>(rPixelBounds.getHeight()),
298 awt::PosSize::POSSIZE );
300 return true;