build fix
[LibreOffice.git] / slideshow / source / engine / shapes / viewappletshape.cxx
blobc9de11fb34fd0c38f6722643ec3dc40a6b6530d7
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/tools/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 ));
101 ViewAppletShape::~ViewAppletShape()
105 endApplet();
107 catch (uno::Exception &)
109 OSL_FAIL( OUStringToOString(
110 comphelper::anyToString(
111 cppu::getCaughtException() ),
112 RTL_TEXTENCODING_UTF8 ).getStr() );
117 const ViewLayerSharedPtr& ViewAppletShape::getViewLayer() const
119 return mpViewLayer;
123 void ViewAppletShape::startApplet( const ::basegfx::B2DRectangle& rBounds )
125 ENSURE_OR_RETURN_VOID( mpViewLayer && mpViewLayer->getCanvas() && mpViewLayer->getCanvas()->getUNOCanvas().is(),
126 "ViewAppletShape::startApplet(): Invalid or disposed view" );
129 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
131 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
132 uno::UNO_QUERY_THROW );
134 uno::Reference< awt::XWindow2 > xParentWindow(
135 xPropSet->getPropertyValue("Window"),
136 uno::UNO_QUERY_THROW );
138 uno::Reference<lang::XMultiComponentFactory> xFactory(
139 mxComponentContext->getServiceManager() );
141 if( xFactory.is() )
143 // create an awt window to contain the applet
144 // ==========================================
146 uno::Reference< awt::XToolkit2 > xToolkit = awt::Toolkit::create(mxComponentContext);
148 awt::WindowDescriptor aOwnWinDescriptor( awt::WindowClass_SIMPLE,
149 OUString(),
150 uno::Reference< awt::XWindowPeer >(xParentWindow,
151 uno::UNO_QUERY_THROW),
153 awt::Rectangle(),
154 awt::WindowAttribute::SHOW
155 | awt::VclWindowPeerAttribute::CLIPCHILDREN );
157 uno::Reference< awt::XWindowPeer > xNewWinPeer(
158 xToolkit->createWindow( aOwnWinDescriptor ));
159 uno::Reference< awt::XWindow > xOwnWindow( xNewWinPeer,
160 uno::UNO_QUERY_THROW );
163 // create a frame, and load the applet into it
164 // ===========================================
166 mxFrame = frame::Frame::create( mxComponentContext );
167 mxFrame->initialize( xOwnWindow );
169 uno::Reference < frame::XSynchronousFrameLoader > xLoader( mxViewer,
170 uno::UNO_QUERY_THROW );
171 xLoader->load( uno::Sequence < beans::PropertyValue >(),
172 uno::Reference<frame::XFrame>(mxFrame, uno::UNO_QUERY_THROW) );
175 // resize surrounding window and applet to current shape size
176 // ==========================================================
178 ::basegfx::B2DRange aTmpRange;
179 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
180 rBounds,
181 mpViewLayer->getTransformation() );
182 const ::basegfx::B2IRange& rPixelBounds(
183 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
185 uno::Reference< awt::XWindow > xSurroundingWindow( mxFrame->getContainerWindow() );
186 if( xSurroundingWindow.is() )
187 xSurroundingWindow->setPosSize( static_cast<sal_Int32>(rPixelBounds.getMinX()),
188 static_cast<sal_Int32>(rPixelBounds.getMinY()),
189 static_cast<sal_Int32>(rPixelBounds.getWidth()),
190 static_cast<sal_Int32>(rPixelBounds.getHeight()),
191 awt::PosSize::POSSIZE );
193 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
194 if( xAppletWindow.is() )
195 xAppletWindow->setPosSize( 0, 0,
196 static_cast<sal_Int32>(rPixelBounds.getWidth()),
197 static_cast<sal_Int32>(rPixelBounds.getHeight()),
198 awt::PosSize::POSSIZE );
201 catch (uno::Exception &)
207 void ViewAppletShape::endApplet()
209 uno::Reference<util::XCloseable> xCloseable(
210 mxFrame,
211 uno::UNO_QUERY );
213 if( xCloseable.is() )
215 xCloseable->close( true );
216 mxFrame.clear();
221 bool ViewAppletShape::render( const ::basegfx::B2DRectangle& rBounds ) const
223 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
225 if( !pCanvas )
226 return false;
228 if( !mxFrame.is() )
230 // fill the shape background with black
231 fillRect( pCanvas,
232 rBounds,
233 0xFFFFFFFFU );
236 return true;
239 bool ViewAppletShape::resize( const ::basegfx::B2DRectangle& rBounds ) const
241 if( !mxFrame.is() )
242 return false;
244 ::basegfx::B2DRange aTmpRange;
245 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
246 rBounds,
247 mpViewLayer->getTransformation() );
248 const ::basegfx::B2IRange& rPixelBounds(
249 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
251 uno::Reference< awt::XWindow > xFrameWindow( mxFrame->getContainerWindow() );
252 if( xFrameWindow.is() )
253 xFrameWindow->setPosSize( static_cast<sal_Int32>(rPixelBounds.getMinX()),
254 static_cast<sal_Int32>(rPixelBounds.getMinY()),
255 static_cast<sal_Int32>(rPixelBounds.getWidth()),
256 static_cast<sal_Int32>(rPixelBounds.getHeight()),
257 awt::PosSize::POSSIZE );
259 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
260 if( xAppletWindow.is() )
261 xAppletWindow->setPosSize( 0, 0,
262 static_cast<sal_Int32>(rPixelBounds.getWidth()),
263 static_cast<sal_Int32>(rPixelBounds.getHeight()),
264 awt::PosSize::POSSIZE );
266 return true;
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */