tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / slideshow / source / engine / shapes / viewappletshape.cxx
blob460d0170c633284413c75f728186cb2848137321
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 <comphelper/diagnose_ex.hxx>
23 #include <basegfx/matrix/b2dhommatrix.hxx>
24 #include <basegfx/range/b2irange.hxx>
25 #include <basegfx/utils/canvastools.hxx>
27 #include <cppcanvas/canvas.hxx>
28 #include <canvas/canvastools.hxx>
30 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
32 #include <com/sun/star/rendering/XCanvas.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/beans/PropertyValue.hpp>
35 #include <com/sun/star/util/XCloseable.hpp>
36 #include <com/sun/star/awt/WindowDescriptor.hpp>
37 #include <com/sun/star/awt/Toolkit.hpp>
38 #include <com/sun/star/awt/XWindow2.hpp>
39 #include <com/sun/star/awt/XWindowPeer.hpp>
40 #include <com/sun/star/awt/WindowAttribute.hpp>
41 #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
42 #include <com/sun/star/awt/PosSize.hpp>
43 #include <com/sun/star/frame/Frame.hpp>
44 #include <com/sun/star/frame/XSynchronousFrameLoader.hpp>
46 #include "viewappletshape.hxx"
47 #include <tools.hxx>
48 #include <utility>
51 using namespace ::com::sun::star;
53 namespace slideshow::internal
55 ViewAppletShape::ViewAppletShape( ViewLayerSharedPtr xViewLayer,
56 const uno::Reference< drawing::XShape >& rxShape,
57 const OUString& rServiceName,
58 const char** pPropCopyTable,
59 std::size_t nNumPropEntries,
60 uno::Reference< uno::XComponentContext > xContext ) :
61 mpViewLayer(std::move( xViewLayer )),
62 mxViewer(),
63 mxFrame(),
64 mxComponentContext(std::move( xContext ))
66 ENSURE_OR_THROW( rxShape.is(), "ViewAppletShape::ViewAppletShape(): Invalid Shape" );
67 ENSURE_OR_THROW( mpViewLayer, "ViewAppletShape::ViewAppletShape(): Invalid View" );
68 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewAppletShape::ViewAppletShape(): Invalid ViewLayer canvas" );
69 ENSURE_OR_THROW( mxComponentContext.is(), "ViewAppletShape::ViewAppletShape(): Invalid component context" );
71 uno::Reference<lang::XMultiComponentFactory> xFactory(
72 mxComponentContext->getServiceManager(),
73 uno::UNO_SET_THROW );
75 mxViewer.set( xFactory->createInstanceWithContext( rServiceName,
76 mxComponentContext),
77 uno::UNO_QUERY_THROW );
79 uno::Reference< beans::XPropertySet > xShapePropSet( rxShape,
80 uno::UNO_QUERY_THROW );
81 uno::Reference< beans::XPropertySet > xViewerPropSet( mxViewer,
82 uno::UNO_QUERY_THROW );
84 // copy shape properties to applet viewer
85 OUString aPropName;
86 for( std::size_t i=0; i<nNumPropEntries; ++i )
88 aPropName = OUString::createFromAscii( pPropCopyTable[i] );
89 xViewerPropSet->setPropertyValue( aPropName,
90 xShapePropSet->getPropertyValue(
91 aPropName ));
95 ViewAppletShape::~ViewAppletShape()
97 try
99 endApplet();
101 catch (const uno::Exception &)
103 TOOLS_WARN_EXCEPTION("slideshow", "");
107 const ViewLayerSharedPtr& ViewAppletShape::getViewLayer() const
109 return mpViewLayer;
112 void ViewAppletShape::startApplet( const ::basegfx::B2DRectangle& rBounds )
114 ENSURE_OR_RETURN_VOID( mpViewLayer && mpViewLayer->getCanvas() && mpViewLayer->getCanvas()->getUNOCanvas().is(),
115 "ViewAppletShape::startApplet(): Invalid or disposed view" );
118 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
120 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
121 uno::UNO_QUERY_THROW );
123 uno::Reference< awt::XWindow2 > xParentWindow(
124 xPropSet->getPropertyValue(u"Window"_ustr),
125 uno::UNO_QUERY_THROW );
127 uno::Reference<lang::XMultiComponentFactory> xFactory(
128 mxComponentContext->getServiceManager() );
130 if( xFactory.is() )
132 // create an awt window to contain the applet
133 // ==========================================
135 uno::Reference< awt::XToolkit2 > xToolkit = awt::Toolkit::create(mxComponentContext);
137 awt::WindowDescriptor aOwnWinDescriptor( awt::WindowClass_SIMPLE,
138 OUString(),
139 uno::Reference< awt::XWindowPeer >(xParentWindow,
140 uno::UNO_QUERY_THROW),
142 awt::Rectangle(),
143 awt::WindowAttribute::SHOW
144 | awt::VclWindowPeerAttribute::CLIPCHILDREN );
146 uno::Reference< awt::XWindowPeer > xNewWinPeer(
147 xToolkit->createWindow( aOwnWinDescriptor ));
148 uno::Reference< awt::XWindow > xOwnWindow( xNewWinPeer,
149 uno::UNO_QUERY_THROW );
152 // create a frame, and load the applet into it
153 // ===========================================
155 mxFrame = frame::Frame::create( mxComponentContext );
156 mxFrame->initialize( xOwnWindow );
158 uno::Reference < frame::XSynchronousFrameLoader > xLoader( mxViewer,
159 uno::UNO_SET_THROW );
160 xLoader->load( uno::Sequence < beans::PropertyValue >(),
161 uno::Reference<frame::XFrame>(mxFrame, uno::UNO_QUERY_THROW) );
164 // resize surrounding window and applet to current shape size
165 // ==========================================================
167 ::basegfx::B2DRange aTmpRange = ::canvas::tools::calcTransformedRectBounds(
168 rBounds,
169 mpViewLayer->getTransformation() );
170 const ::basegfx::B2IRange aPixelBounds(
171 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
173 uno::Reference< awt::XWindow > xSurroundingWindow( mxFrame->getContainerWindow() );
174 if( xSurroundingWindow.is() )
175 xSurroundingWindow->setPosSize( aPixelBounds.getMinX(),
176 aPixelBounds.getMinY(),
177 static_cast<sal_Int32>(aPixelBounds.getWidth()),
178 static_cast<sal_Int32>(aPixelBounds.getHeight()),
179 awt::PosSize::POSSIZE );
181 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
182 if( xAppletWindow.is() )
183 xAppletWindow->setPosSize( 0, 0,
184 static_cast<sal_Int32>(aPixelBounds.getWidth()),
185 static_cast<sal_Int32>(aPixelBounds.getHeight()),
186 awt::PosSize::POSSIZE );
189 catch (uno::Exception &)
195 void ViewAppletShape::endApplet()
197 uno::Reference<util::XCloseable> xCloseable(
198 mxFrame,
199 uno::UNO_QUERY );
201 if( xCloseable.is() )
203 xCloseable->close( true );
204 mxFrame.clear();
209 bool ViewAppletShape::render( const ::basegfx::B2DRectangle& rBounds ) const
211 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
213 if( !pCanvas )
214 return false;
216 if( !mxFrame.is() )
218 // fill the shape background with black
219 fillRect( pCanvas,
220 rBounds,
221 0xFFFFFFFFU );
224 return true;
227 bool ViewAppletShape::resize( const ::basegfx::B2DRectangle& rBounds ) const
229 if( !mxFrame.is() )
230 return false;
232 ::basegfx::B2DRange aTmpRange = ::canvas::tools::calcTransformedRectBounds(
233 rBounds,
234 mpViewLayer->getTransformation() );
235 const ::basegfx::B2IRange aPixelBounds(
236 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
238 uno::Reference< awt::XWindow > xFrameWindow( mxFrame->getContainerWindow() );
239 if( xFrameWindow.is() )
240 xFrameWindow->setPosSize( aPixelBounds.getMinX(),
241 aPixelBounds.getMinY(),
242 static_cast<sal_Int32>(aPixelBounds.getWidth()),
243 static_cast<sal_Int32>(aPixelBounds.getHeight()),
244 awt::PosSize::POSSIZE );
246 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
247 if( xAppletWindow.is() )
248 xAppletWindow->setPosSize( 0, 0,
249 static_cast<sal_Int32>(aPixelBounds.getWidth()),
250 static_cast<sal_Int32>(aPixelBounds.getHeight()),
251 awt::PosSize::POSSIZE );
253 return true;
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */