LibreOffice Module slideshow (master) 1
viewappletshape.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
22
26
27#include <cppcanvas/canvas.hxx>
29
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>
45
46#include "viewappletshape.hxx"
47#include <tools.hxx>
48#include <utility>
49
50
51using namespace ::com::sun::star;
52
53namespace slideshow::internal
54{
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 ))
65 {
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" );
70
71 uno::Reference<lang::XMultiComponentFactory> xFactory(
72 mxComponentContext->getServiceManager(),
73 uno::UNO_SET_THROW );
74
75 mxViewer.set( xFactory->createInstanceWithContext( rServiceName,
76 mxComponentContext),
77 uno::UNO_QUERY_THROW );
78
79 uno::Reference< beans::XPropertySet > xShapePropSet( rxShape,
80 uno::UNO_QUERY_THROW );
81 uno::Reference< beans::XPropertySet > xViewerPropSet( mxViewer,
82 uno::UNO_QUERY_THROW );
83
84 // copy shape properties to applet viewer
85 OUString aPropName;
86 for( std::size_t i=0; i<nNumPropEntries; ++i )
87 {
88 aPropName = OUString::createFromAscii( pPropCopyTable[i] );
89 xViewerPropSet->setPropertyValue( aPropName,
90 xShapePropSet->getPropertyValue(
91 aPropName ));
92 }
93 }
94
95 ViewAppletShape::~ViewAppletShape()
96 {
97 try
98 {
99 endApplet();
100 }
101 catch (const uno::Exception &)
102 {
103 TOOLS_WARN_EXCEPTION("slideshow", "");
104 }
105 }
106
107 const ViewLayerSharedPtr& ViewAppletShape::getViewLayer() const
108 {
109 return mpViewLayer;
110 }
111
112 void ViewAppletShape::startApplet( const ::basegfx::B2DRectangle& rBounds )
113 {
114 ENSURE_OR_RETURN_VOID( mpViewLayer && mpViewLayer->getCanvas() && mpViewLayer->getCanvas()->getUNOCanvas().is(),
115 "ViewAppletShape::startApplet(): Invalid or disposed view" );
116 try
117 {
118 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
119
120 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
121 uno::UNO_QUERY_THROW );
122
124 xPropSet->getPropertyValue("Window"),
125 uno::UNO_QUERY_THROW );
126
128 mxComponentContext->getServiceManager() );
129
130 if( xFactory.is() )
131 {
132 // create an awt window to contain the applet
133 // ==========================================
134
135 uno::Reference< awt::XToolkit2 > xToolkit = awt::Toolkit::create(mxComponentContext);
136
137 awt::WindowDescriptor aOwnWinDescriptor( awt::WindowClass_SIMPLE,
138 OUString(),
140 uno::UNO_QUERY_THROW),
141 0,
142 awt::Rectangle(),
143 awt::WindowAttribute::SHOW
144 | awt::VclWindowPeerAttribute::CLIPCHILDREN );
145
147 xToolkit->createWindow( aOwnWinDescriptor ));
148 uno::Reference< awt::XWindow > xOwnWindow( xNewWinPeer,
149 uno::UNO_QUERY_THROW );
150
151
152 // create a frame, and load the applet into it
153 // ===========================================
154
155 mxFrame = frame::Frame::create( mxComponentContext );
156 mxFrame->initialize( xOwnWindow );
157
159 uno::UNO_SET_THROW );
161 uno::Reference<frame::XFrame>(mxFrame, uno::UNO_QUERY_THROW) );
162
163
164 // resize surrounding window and applet to current shape size
165 // ==========================================================
166
167 ::basegfx::B2DRange aTmpRange;
168 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
169 rBounds,
170 mpViewLayer->getTransformation() );
171 const ::basegfx::B2IRange& rPixelBounds(
172 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
173
174 uno::Reference< awt::XWindow > xSurroundingWindow( mxFrame->getContainerWindow() );
175 if( xSurroundingWindow.is() )
176 xSurroundingWindow->setPosSize( rPixelBounds.getMinX(),
177 rPixelBounds.getMinY(),
178 static_cast<sal_Int32>(rPixelBounds.getWidth()),
179 static_cast<sal_Int32>(rPixelBounds.getHeight()),
180 awt::PosSize::POSSIZE );
181
182 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
183 if( xAppletWindow.is() )
184 xAppletWindow->setPosSize( 0, 0,
185 static_cast<sal_Int32>(rPixelBounds.getWidth()),
186 static_cast<sal_Int32>(rPixelBounds.getHeight()),
187 awt::PosSize::POSSIZE );
188 }
189 }
190 catch (uno::Exception &)
191 {
192 }
193 }
194
195
196 void ViewAppletShape::endApplet()
197 {
199 mxFrame,
200 uno::UNO_QUERY );
201
202 if( xCloseable.is() )
203 {
204 xCloseable->close( true );
205 mxFrame.clear();
206 }
207 }
208
209
210 bool ViewAppletShape::render( const ::basegfx::B2DRectangle& rBounds ) const
211 {
212 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
213
214 if( !pCanvas )
215 return false;
216
217 if( !mxFrame.is() )
218 {
219 // fill the shape background with black
220 fillRect( pCanvas,
221 rBounds,
222 0xFFFFFFFFU );
223 }
224
225 return true;
226 }
227
228 bool ViewAppletShape::resize( const ::basegfx::B2DRectangle& rBounds ) const
229 {
230 if( !mxFrame.is() )
231 return false;
232
233 ::basegfx::B2DRange aTmpRange;
234 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
235 rBounds,
236 mpViewLayer->getTransformation() );
237 const ::basegfx::B2IRange& rPixelBounds(
238 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
239
240 uno::Reference< awt::XWindow > xFrameWindow( mxFrame->getContainerWindow() );
241 if( xFrameWindow.is() )
242 xFrameWindow->setPosSize( rPixelBounds.getMinX(),
243 rPixelBounds.getMinY(),
244 static_cast<sal_Int32>(rPixelBounds.getWidth()),
245 static_cast<sal_Int32>(rPixelBounds.getHeight()),
246 awt::PosSize::POSSIZE );
247
248 uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
249 if( xAppletWindow.is() )
250 xAppletWindow->setPosSize( 0, 0,
251 static_cast<sal_Int32>(rPixelBounds.getWidth()),
252 static_cast<sal_Int32>(rPixelBounds.getHeight()),
253 awt::PosSize::POSSIZE );
254
255 return true;
256 }
257}
258
259/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > mxComponentContext
ViewAppletShape(ViewLayerSharedPtr xViewLayer, const css::uno::Reference< css::drawing::XShape > &rxShape, const OUString &rServiceName, const char **pPropCopyTable, std::size_t nNumPropEntries, css::uno::Reference< css::uno::XComponentContext > xContext)
Create a ViewAppletShape for the given View.
#define TOOLS_WARN_EXCEPTION(area, stream)
#define ENSURE_OR_THROW(c, m)
#define ENSURE_OR_RETURN_VOID(c, m)
Reference< XSingleServiceFactory > xFactory
std::shared_ptr< Canvas > CanvasSharedPtr
int i
OUString aPropName
std::shared_ptr< ViewLayer > ViewLayerSharedPtr
Definition: viewlayer.hxx:168
void fillRect(const ::cppcanvas::CanvasSharedPtr &rCanvas, const ::basegfx::B2DRectangle &rRect, ::cppcanvas::IntSRGBA aFillColor)
Definition: tools.cxx:657