1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
54 using namespace ::com::sun::star
;
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
),
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
,
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
91 for( std::size_t i
=0; i
<nNumPropEntries
; ++i
)
93 aPropName
= OUString::createFromAscii( pPropCopyTable
[i
] );
94 xViewerPropSet
->setPropertyValue( aPropName
,
95 xShapePropSet
->getPropertyValue(
100 ViewAppletShape::~ViewAppletShape()
106 catch (const uno::Exception
&e
)
108 SAL_WARN("slideshow", e
);
112 const ViewLayerSharedPtr
& ViewAppletShape::getViewLayer() const
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() );
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
,
144 uno::Reference
< awt::XWindowPeer
>(xParentWindow
,
145 uno::UNO_QUERY_THROW
),
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
,
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(
207 if( xCloseable
.is() )
209 xCloseable
->close( true );
215 bool ViewAppletShape::render( const ::basegfx::B2DRectangle
& rBounds
) const
217 ::cppcanvas::CanvasSharedPtr pCanvas
= mpViewLayer
->getCanvas();
224 // fill the shape background with black
233 bool ViewAppletShape::resize( const ::basegfx::B2DRectangle
& rBounds
) const
238 ::basegfx::B2DRange aTmpRange
;
239 ::canvas::tools::calcTransformedRectBounds( aTmpRange
,
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
);
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */