Bump version to 6.0-36
[LibreOffice.git] / slideshow / source / engine / shapes / viewappletshape.cxx
blob66bd38b9e66545a3c31221feee0375c7415d8385
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <tools/diagnose_ex.h>
23 #include <comphelper/anytostring.hxx>
24 #include <cppuhelper/exc_hlp.hxx>
26 #include <basegfx/matrix/b2dhommatrix.hxx>
27 #include <basegfx/range/b2irange.hxx>
28 #include <basegfx/utils/canvastools.hxx>
30 #include <cppcanvas/spritecanvas.hxx>
31 #include <canvas/canvastools.hxx>
33 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
35 #include <com/sun/star/rendering/XCanvas.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <com/sun/star/util/XCloseable.hpp>
40 #include <com/sun/star/awt/WindowDescriptor.hpp>
41 #include <com/sun/star/awt/Toolkit.hpp>
42 #include <com/sun/star/awt/XWindow2.hpp>
43 #include <com/sun/star/awt/XWindowPeer.hpp>
44 #include <com/sun/star/awt/WindowAttribute.hpp>
45 #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
46 #include <com/sun/star/awt/PosSize.hpp>
47 #include <com/sun/star/frame/Frame.hpp>
48 #include <com/sun/star/frame/XSynchronousFrameLoader.hpp>
50 #include "viewappletshape.hxx"
51 #include <tools.hxx>
54 using namespace ::com::sun::star;
56 namespace slideshow
58 namespace internal
60 ViewAppletShape::ViewAppletShape( const ViewLayerSharedPtr& rViewLayer,
61 const uno::Reference< drawing::XShape >& rxShape,
62 const OUString& rServiceName,
63 const char** pPropCopyTable,
64 std::size_t nNumPropEntries,
65 const uno::Reference< uno::XComponentContext >& rxContext ) :
66 mpViewLayer( rViewLayer ),
67 mxViewer(),
68 mxFrame(),
69 mxComponentContext( rxContext )
71 ENSURE_OR_THROW( rxShape.is(), "ViewAppletShape::ViewAppletShape(): Invalid Shape" );
72 ENSURE_OR_THROW( mpViewLayer, "ViewAppletShape::ViewAppletShape(): Invalid View" );
73 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewAppletShape::ViewAppletShape(): Invalid ViewLayer canvas" );
74 ENSURE_OR_THROW( mxComponentContext.is(), "ViewAppletShape::ViewAppletShape(): Invalid component context" );
76 uno::Reference<lang::XMultiComponentFactory> xFactory(
77 mxComponentContext->getServiceManager(),
78 uno::UNO_QUERY_THROW );
80 mxViewer.set( xFactory->createInstanceWithContext( rServiceName,
81 mxComponentContext),
82 uno::UNO_QUERY_THROW );
84 uno::Reference< beans::XPropertySet > xShapePropSet( rxShape,
85 uno::UNO_QUERY_THROW );
86 uno::Reference< beans::XPropertySet > xViewerPropSet( mxViewer,
87 uno::UNO_QUERY_THROW );
89 // copy shape properties to applet viewer
90 OUString aPropName;
91 for( std::size_t i=0; i<nNumPropEntries; ++i )
93 aPropName = OUString::createFromAscii( pPropCopyTable[i] );
94 xViewerPropSet->setPropertyValue( aPropName,
95 xShapePropSet->getPropertyValue(
96 aPropName ));
100 ViewAppletShape::~ViewAppletShape()
104 endApplet();
106 catch (const uno::Exception &e)
108 SAL_WARN("slideshow", e);
112 const ViewLayerSharedPtr& ViewAppletShape::getViewLayer() const
114 return mpViewLayer;
117 void ViewAppletShape::startApplet( const ::basegfx::B2DRectangle& rBounds )
119 ENSURE_OR_RETURN_VOID( mpViewLayer && mpViewLayer->getCanvas() && mpViewLayer->getCanvas()->getUNOCanvas().is(),
120 "ViewAppletShape::startApplet(): Invalid or disposed view" );
123 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
125 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
126 uno::UNO_QUERY_THROW );
128 uno::Reference< awt::XWindow2 > xParentWindow(
129 xPropSet->getPropertyValue("Window"),
130 uno::UNO_QUERY_THROW );
132 uno::Reference<lang::XMultiComponentFactory> xFactory(
133 mxComponentContext->getServiceManager() );
135 if( xFactory.is() )
137 // create an awt window to contain the applet
138 // ==========================================
140 uno::Reference< awt::XToolkit2 > xToolkit = awt::Toolkit::create(mxComponentContext);
142 awt::WindowDescriptor aOwnWinDescriptor( awt::WindowClass_SIMPLE,
143 OUString(),
144 uno::Reference< awt::XWindowPeer >(xParentWindow,
145 uno::UNO_QUERY_THROW),
147 awt::Rectangle(),
148 awt::WindowAttribute::SHOW
149 | awt::VclWindowPeerAttribute::CLIPCHILDREN );
151 uno::Reference< awt::XWindowPeer > xNewWinPeer(
152 xToolkit->createWindow( aOwnWinDescriptor ));
153 uno::Reference< awt::XWindow > xOwnWindow( xNewWinPeer,
154 uno::UNO_QUERY_THROW );
157 // create a frame, and load the applet into it
158 // ===========================================
160 mxFrame = frame::Frame::create( mxComponentContext );
161 mxFrame->initialize( xOwnWindow );
163 uno::Reference < frame::XSynchronousFrameLoader > xLoader( mxViewer,
164 uno::UNO_QUERY_THROW );
165 xLoader->load( uno::Sequence < beans::PropertyValue >(),
166 uno::Reference<frame::XFrame>(mxFrame, uno::UNO_QUERY_THROW) );
169 // resize surrounding window and applet to current shape size
170 // ==========================================================
172 ::basegfx::B2DRange aTmpRange;
173 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
174 rBounds,
175 mpViewLayer->getTransformation() );
176 const ::basegfx::B2IRange& rPixelBounds(
177 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
179 uno::Reference< awt::XWindow > xSurroundingWindow( mxFrame->getContainerWindow() );
180 if( xSurroundingWindow.is() )
181 xSurroundingWindow->setPosSize( rPixelBounds.getMinX(),
182 rPixelBounds.getMinY(),
183 static_cast<sal_Int32>(rPixelBounds.getWidth()),
184 static_cast<sal_Int32>(rPixelBounds.getHeight()),
185 awt::PosSize::POSSIZE );
187 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
188 if( xAppletWindow.is() )
189 xAppletWindow->setPosSize( 0, 0,
190 static_cast<sal_Int32>(rPixelBounds.getWidth()),
191 static_cast<sal_Int32>(rPixelBounds.getHeight()),
192 awt::PosSize::POSSIZE );
195 catch (uno::Exception &)
201 void ViewAppletShape::endApplet()
203 uno::Reference<util::XCloseable> xCloseable(
204 mxFrame,
205 uno::UNO_QUERY );
207 if( xCloseable.is() )
209 xCloseable->close( true );
210 mxFrame.clear();
215 bool ViewAppletShape::render( const ::basegfx::B2DRectangle& rBounds ) const
217 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
219 if( !pCanvas )
220 return false;
222 if( !mxFrame.is() )
224 // fill the shape background with black
225 fillRect( pCanvas,
226 rBounds,
227 0xFFFFFFFFU );
230 return true;
233 bool ViewAppletShape::resize( const ::basegfx::B2DRectangle& rBounds ) const
235 if( !mxFrame.is() )
236 return false;
238 ::basegfx::B2DRange aTmpRange;
239 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
240 rBounds,
241 mpViewLayer->getTransformation() );
242 const ::basegfx::B2IRange& rPixelBounds(
243 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
245 uno::Reference< awt::XWindow > xFrameWindow( mxFrame->getContainerWindow() );
246 if( xFrameWindow.is() )
247 xFrameWindow->setPosSize( rPixelBounds.getMinX(),
248 rPixelBounds.getMinY(),
249 static_cast<sal_Int32>(rPixelBounds.getWidth()),
250 static_cast<sal_Int32>(rPixelBounds.getHeight()),
251 awt::PosSize::POSSIZE );
253 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
254 if( xAppletWindow.is() )
255 xAppletWindow->setPosSize( 0, 0,
256 static_cast<sal_Int32>(rPixelBounds.getWidth()),
257 static_cast<sal_Int32>(rPixelBounds.getHeight()),
258 awt::PosSize::POSSIZE );
260 return true;
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */