1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_slideshow.hxx"
26 
27 // must be first
28 #include <canvas/verbosetrace.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <canvas/debug.hxx>
31 
32 #include <comphelper/anytostring.hxx>
33 #include <cppuhelper/exc_hlp.hxx>
34 
35 #include <basegfx/matrix/b2dhommatrix.hxx>
36 #include <basegfx/range/b2irange.hxx>
37 #include <basegfx/tools/canvastools.hxx>
38 
39 #include <cppcanvas/spritecanvas.hxx>
40 #include <canvas/canvastools.hxx>
41 
42 #include <com/sun/star/uno/XComponentContext.hpp>
43 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
44 #include <com/sun/star/rendering/XCanvas.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <com/sun/star/beans/PropertyValue.hpp>
48 #include <com/sun/star/util/XCloseable.hpp>
49 #include <com/sun/star/awt/WindowDescriptor.hpp>
50 #include <com/sun/star/awt/XToolkit.hpp>
51 #include <com/sun/star/awt/XWindow2.hpp>
52 #include <com/sun/star/awt/XWindowPeer.hpp>
53 #include <com/sun/star/awt/WindowAttribute.hpp>
54 #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
55 #include <com/sun/star/awt/PosSize.hpp>
56 #include <com/sun/star/frame/XFrame.hpp>
57 #include <com/sun/star/frame/XSynchronousFrameLoader.hpp>
58 
59 #include "viewappletshape.hxx"
60 #include "tools.hxx"
61 
62 
63 using namespace ::com::sun::star;
64 
65 namespace slideshow
66 {
67     namespace internal
68     {
ViewAppletShape(const ViewLayerSharedPtr & rViewLayer,const uno::Reference<drawing::XShape> & rxShape,const::rtl::OUString & rServiceName,const char ** pPropCopyTable,sal_Size nNumPropEntries,const uno::Reference<uno::XComponentContext> & rxContext)69         ViewAppletShape::ViewAppletShape( const ViewLayerSharedPtr&                       rViewLayer,
70                                           const uno::Reference< drawing::XShape >&        rxShape,
71                                           const ::rtl::OUString&                          rServiceName,
72                                           const char**                                    pPropCopyTable,
73                                           sal_Size                                        nNumPropEntries,
74                                           const uno::Reference< uno::XComponentContext >& rxContext ) :
75 			mpViewLayer( rViewLayer ),
76             mxViewer(),
77             mxFrame(),
78             mxComponentContext( rxContext )
79         {
80             ENSURE_OR_THROW( rxShape.is(), "ViewAppletShape::ViewAppletShape(): Invalid Shape" );
81             ENSURE_OR_THROW( mpViewLayer, "ViewAppletShape::ViewAppletShape(): Invalid View" );
82             ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewAppletShape::ViewAppletShape(): Invalid ViewLayer canvas" );
83             ENSURE_OR_THROW( mxComponentContext.is(), "ViewAppletShape::ViewAppletShape(): Invalid component context" );
84 
85             uno::Reference<lang::XMultiComponentFactory> xFactory(
86                 mxComponentContext->getServiceManager(),
87                 uno::UNO_QUERY_THROW );
88 
89             mxViewer.set( xFactory->createInstanceWithContext( rServiceName,
90                                                                mxComponentContext),
91                           uno::UNO_QUERY_THROW );
92 
93             uno::Reference< beans::XPropertySet > xShapePropSet( rxShape,
94                                                                  uno::UNO_QUERY_THROW );
95             uno::Reference< beans::XPropertySet > mxViewerPropSet( mxViewer,
96                                                                    uno::UNO_QUERY_THROW );
97 
98             // copy shape properties to applet viewer
99             ::rtl::OUString aPropName;
100             for( sal_Size i=0; i<nNumPropEntries; ++i )
101             {
102                 aPropName = ::rtl::OUString::createFromAscii( pPropCopyTable[i] );
103                 mxViewerPropSet->setPropertyValue( aPropName,
104                                                    xShapePropSet->getPropertyValue(
105                                                        aPropName ));
106             }
107         }
108 
109 		// ---------------------------------------------------------------------
110 
~ViewAppletShape()111 		ViewAppletShape::~ViewAppletShape()
112 		{
113             try
114             {
115                 endApplet();
116             }
117             catch (uno::Exception &)
118             {
119                 OSL_ENSURE( false, rtl::OUStringToOString(
120                                 comphelper::anyToString(
121                                     cppu::getCaughtException() ),
122                                 RTL_TEXTENCODING_UTF8 ).getStr() );
123             }
124 		}
125 
126 		// ---------------------------------------------------------------------
127 
getViewLayer() const128         ViewLayerSharedPtr ViewAppletShape::getViewLayer() const
129         {
130             return mpViewLayer;
131         }
132 
133 		// ---------------------------------------------------------------------
134 
startApplet(const::basegfx::B2DRectangle & rBounds)135         bool ViewAppletShape::startApplet( const ::basegfx::B2DRectangle& rBounds )
136         {
137             ENSURE_OR_RETURN_FALSE( mpViewLayer && mpViewLayer->getCanvas() && mpViewLayer->getCanvas()->getUNOCanvas().is(),
138                                "ViewAppletShape::startApplet(): Invalid or disposed view" );
139             try
140             {
141                 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
142 
143                 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
144                                                                 uno::UNO_QUERY_THROW );
145 
146                 uno::Reference< awt::XWindow2 > xParentWindow(
147                     xPropSet->getPropertyValue(
148                         ::rtl::OUString::createFromAscii( "Window" )),
149                     uno::UNO_QUERY_THROW );
150 
151                 uno::Reference<lang::XMultiComponentFactory> xFactory(
152                     mxComponentContext->getServiceManager() );
153 
154                 if( xFactory.is() )
155                 {
156                     // create an awt window to contain the applet
157                     // ==========================================
158 
159                     uno::Reference< awt::XToolkit > xToolkit(
160                         xFactory->createInstanceWithContext(
161                             ::rtl::OUString::createFromAscii( "com.sun.star.awt.Toolkit" ),
162                             mxComponentContext ),
163                         uno::UNO_QUERY_THROW );
164 
165                     awt::WindowDescriptor aOwnWinDescriptor( awt::WindowClass_SIMPLE,
166                                                              ::rtl::OUString(),
167                                                              uno::Reference< awt::XWindowPeer >(xParentWindow,
168                                                                                                 uno::UNO_QUERY_THROW),
169                                                              0,
170                                                              awt::Rectangle(),
171                                                              awt::WindowAttribute::SHOW
172                                                              | awt::VclWindowPeerAttribute::CLIPCHILDREN );
173 
174                     uno::Reference< awt::XWindowPeer > xNewWinPeer(
175                         xToolkit->createWindow( aOwnWinDescriptor ));
176                     uno::Reference< awt::XWindow > xOwnWindow( xNewWinPeer,
177                                                                uno::UNO_QUERY_THROW );
178 
179 
180                     // create a frame, and load the applet into it
181                     // ===========================================
182 
183                     mxFrame.set(
184                         xFactory->createInstanceWithContext(
185                             ::rtl::OUString::createFromAscii( "com.sun.star.frame.Frame" ),
186                             mxComponentContext ),
187                         uno::UNO_QUERY_THROW );
188 
189                     mxFrame->initialize( xOwnWindow );
190 
191                     uno::Reference < frame::XSynchronousFrameLoader > xLoader( mxViewer,
192                                                                                uno::UNO_QUERY_THROW );
193                     xLoader->load( uno::Sequence < beans::PropertyValue >(),
194                                    mxFrame );
195 
196 
197                     // resize surrounding window and applet to current shape size
198                     // ==========================================================
199 
200                     ::basegfx::B2DRange aTmpRange;
201                     ::canvas::tools::calcTransformedRectBounds( aTmpRange,
202                                                                 rBounds,
203                                                                 mpViewLayer->getTransformation() );
204                     const ::basegfx::B2IRange& rPixelBounds(
205                         ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
206 
207                     uno::Reference< awt::XWindow > xSurroundingWindow( mxFrame->getContainerWindow() );
208                     if( xSurroundingWindow.is() )
209                         xSurroundingWindow->setPosSize( static_cast<sal_Int32>(rPixelBounds.getMinX()),
210                                                         static_cast<sal_Int32>(rPixelBounds.getMinY()),
211                                                         static_cast<sal_Int32>(rPixelBounds.getWidth()),
212                                                         static_cast<sal_Int32>(rPixelBounds.getHeight()),
213                                                         awt::PosSize::POSSIZE );
214 
215                     uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
216                     if( xAppletWindow.is() )
217                         xAppletWindow->setPosSize( 0, 0,
218                                                    static_cast<sal_Int32>(rPixelBounds.getWidth()),
219                                                    static_cast<sal_Int32>(rPixelBounds.getHeight()),
220                                                    awt::PosSize::POSSIZE );
221                 }
222             }
223             catch (uno::Exception &)
224             {
225                 return false;
226             }
227 
228             return true;
229         }
230 
231 		// ---------------------------------------------------------------------
232 
endApplet()233         void ViewAppletShape::endApplet()
234         {
235             uno::Reference<util::XCloseable> xCloseable(
236                 mxFrame,
237                 uno::UNO_QUERY );
238 
239             if( xCloseable.is() )
240             {
241                 xCloseable->close( sal_True );
242                 mxFrame.clear();
243             }
244         }
245 
246 		// ---------------------------------------------------------------------
247 
render(const::basegfx::B2DRectangle & rBounds) const248         bool ViewAppletShape::render( const ::basegfx::B2DRectangle& rBounds ) const
249         {
250             ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
251 
252             if( !pCanvas )
253                 return false;
254 
255             if( !mxFrame.is() )
256             {
257                 // fill the shape background with black
258                 fillRect( pCanvas,
259                           rBounds,
260                           0xFFFFFFFFU );
261             }
262 
263             return true;
264         }
265 
resize(const::basegfx::B2DRectangle & rBounds) const266         bool ViewAppletShape::resize( const ::basegfx::B2DRectangle& rBounds ) const
267         {
268             if( !mxFrame.is() )
269                 return false;
270 
271             ::basegfx::B2DRange aTmpRange;
272             ::canvas::tools::calcTransformedRectBounds( aTmpRange,
273                                                         rBounds,
274                                                         mpViewLayer->getTransformation() );
275             const ::basegfx::B2IRange& rPixelBounds(
276                 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
277 
278             uno::Reference< awt::XWindow > xFrameWindow( mxFrame->getContainerWindow() );
279             if( xFrameWindow.is() )
280                 xFrameWindow->setPosSize( static_cast<sal_Int32>(rPixelBounds.getMinX()),
281                                           static_cast<sal_Int32>(rPixelBounds.getMinY()),
282                                           static_cast<sal_Int32>(rPixelBounds.getWidth()),
283                                           static_cast<sal_Int32>(rPixelBounds.getHeight()),
284                                           awt::PosSize::POSSIZE );
285 
286             uno::Reference< awt::XWindow > xAppletWindow( mxFrame->getComponentWindow() );
287             if( xAppletWindow.is() )
288                 xAppletWindow->setPosSize( 0, 0,
289                                            static_cast<sal_Int32>(rPixelBounds.getWidth()),
290                                            static_cast<sal_Int32>(rPixelBounds.getHeight()),
291                                            awt::PosSize::POSSIZE );
292 
293             return true;
294         }
295 	}
296 }
297