android: Update app-specific/MIME type icons
[LibreOffice.git] / slideshow / source / engine / shapes / viewmediashape.cxx
blobf91972f8fe40676e5b4167f6433ef84385d84018
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 .
20 #include <config_features.h>
22 #include <comphelper/diagnose_ex.hxx>
24 #include <sal/log.hxx>
25 #include <utility>
26 #include <vcl/canvastools.hxx>
27 #include <vcl/syschild.hxx>
28 #include <vcl/sysdata.hxx>
29 #include <vcl/window.hxx>
30 #include <vcl/graph.hxx>
32 #include <basegfx/utils/canvastools.hxx>
33 #include <basegfx/matrix/b2dhommatrixtools.hxx>
34 #include <basegfx/point/b2dpoint.hxx>
35 #include <basegfx/matrix/b2dhommatrix.hxx>
36 #include <basegfx/range/b2irange.hxx>
37 #include <canvas/canvastools.hxx>
38 #include <cppcanvas/canvas.hxx>
39 #include <avmedia/mediawindow.hxx>
40 #include <svx/svdobj.hxx>
41 #include <svx/svdomedia.hxx>
43 #include <com/sun/star/awt/XWindow.hpp>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/lang/NoSupportException.hpp>
47 #include <com/sun/star/media/XPlayer.hpp>
48 #include <com/sun/star/media/XPlayerWindow.hpp>
49 #include <com/sun/star/presentation/XSlideShowView.hpp>
50 #include <com/sun/star/rendering/XCanvas.hpp>
52 #include "viewmediashape.hxx"
53 #include <tools.hxx>
54 #include <unoview.hxx>
56 using namespace ::com::sun::star;
58 namespace slideshow::internal
60 ViewMediaShape::ViewMediaShape( const ViewLayerSharedPtr& rViewLayer,
61 uno::Reference< drawing::XShape > xShape,
62 uno::Reference< uno::XComponentContext > xContext ) :
63 mpViewLayer( rViewLayer ),
64 maWindowOffset( 0, 0 ),
65 maBounds(),
66 mxShape(std::move( xShape )),
67 mxPlayer(),
68 mxPlayerWindow(),
69 mxComponentContext(std::move( xContext )),
70 mbIsSoundEnabled(true)
72 ENSURE_OR_THROW( mxShape.is(), "ViewMediaShape::ViewMediaShape(): Invalid Shape" );
73 ENSURE_OR_THROW( mpViewLayer, "ViewMediaShape::ViewMediaShape(): Invalid View" );
74 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewMediaShape::ViewMediaShape(): Invalid ViewLayer canvas" );
75 ENSURE_OR_THROW( mxComponentContext.is(), "ViewMediaShape::ViewMediaShape(): Invalid component context" );
77 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(rViewLayer));
78 if (xUnoView)
80 mbIsSoundEnabled = xUnoView->isSoundEnabled();
84 ViewMediaShape::~ViewMediaShape()
86 try
88 endMedia();
90 catch (const uno::Exception &)
92 TOOLS_WARN_EXCEPTION("slideshow", "");
96 const ViewLayerSharedPtr& ViewMediaShape::getViewLayer() const
98 return mpViewLayer;
101 void ViewMediaShape::startMedia()
103 if( !mxPlayer.is() )
104 implInitialize( maBounds );
106 if (mxPlayer.is())
107 mxPlayer->start();
110 void ViewMediaShape::endMedia()
112 // shutdown player window
113 if( mxPlayerWindow.is() )
115 mxPlayerWindow->dispose();
116 mxPlayerWindow.clear();
119 mpMediaWindow.disposeAndClear();
121 // shutdown player
122 if( mxPlayer.is() )
124 mxPlayer->stop();
126 uno::Reference< lang::XComponent > xComponent( mxPlayer, uno::UNO_QUERY );
128 if( xComponent.is() )
129 xComponent->dispose();
131 mxPlayer.clear();
135 void ViewMediaShape::pauseMedia()
137 if (mxPlayer.is())
138 mxPlayer->stop();
141 void ViewMediaShape::setMediaTime(double fTime)
143 if (mxPlayer.is())
144 mxPlayer->setMediaTime(fTime);
147 void ViewMediaShape::setLooping(bool bLooping)
149 if (mxPlayer.is())
151 mxPlayer->setPlaybackLoop(bLooping);
155 bool ViewMediaShape::render( const ::basegfx::B2DRectangle& rBounds ) const
157 #if !HAVE_FEATURE_AVMEDIA
158 (void) rBounds;
159 #else
160 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
162 if( !pCanvas )
163 return false;
165 if( !mpMediaWindow && !mxPlayerWindow.is() )
167 uno::Reference< graphic::XGraphic > xGraphic;
168 uno::Reference< beans::XPropertySet > xPropSet( mxShape, uno::UNO_QUERY );
169 if (xPropSet.is())
171 xPropSet->getPropertyValue("FallbackGraphic") >>= xGraphic;
174 Graphic aGraphic(xGraphic);
175 const BitmapEx aBmp = aGraphic.GetBitmapEx();
177 uno::Reference< rendering::XBitmap > xBitmap(vcl::unotools::xBitmapFromBitmapEx(aBmp));
179 rendering::ViewState aViewState;
180 aViewState.AffineTransform = pCanvas->getViewState().AffineTransform;
182 rendering::RenderState aRenderState;
183 ::canvas::tools::initRenderState( aRenderState );
185 const ::Size aBmpSize( aBmp.GetSizePixel() );
187 const ::basegfx::B2DVector aScale( rBounds.getWidth() / aBmpSize.Width(),
188 rBounds.getHeight() / aBmpSize.Height() );
189 const basegfx::B2DHomMatrix aTranslation(basegfx::utils::createScaleTranslateB2DHomMatrix(
190 aScale, rBounds.getMinimum()));
191 ::canvas::tools::setRenderStateTransform( aRenderState, aTranslation );
193 pCanvas->getUNOCanvas()->drawBitmap( xBitmap,
194 aViewState,
195 aRenderState );
197 #endif
198 return true;
201 bool ViewMediaShape::resize( const ::basegfx::B2DRectangle& rNewBounds ) const
203 maBounds = rNewBounds;
205 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
207 if( !pCanvas )
208 return false;
210 if( !mxPlayerWindow.is() )
211 return true;
213 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
214 uno::UNO_QUERY );
216 uno::Reference< awt::XWindow > xParentWindow;
217 if( xPropSet.is() &&
218 getPropertyValue( xParentWindow,
219 xPropSet,
220 "Window") )
222 const awt::Rectangle aRect( xParentWindow->getPosSize() );
224 maWindowOffset.X = aRect.X;
225 maWindowOffset.Y = aRect.Y;
228 ::basegfx::B2DRange aTmpRange;
229 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
230 rNewBounds,
231 mpViewLayer->getTransformation() );
232 const ::basegfx::B2IRange& rRangePix(
233 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
235 mxPlayerWindow->setEnable( !rRangePix.isEmpty() );
237 if( rRangePix.isEmpty() )
238 return true;
240 awt::Rectangle aCanvasArea;
241 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(mpViewLayer));
242 if (xUnoView)
243 aCanvasArea = xUnoView->getUnoView()->getCanvasArea();
245 const Point aPosPixel( rRangePix.getMinX() + maWindowOffset.X + aCanvasArea.X,
246 rRangePix.getMinY() + maWindowOffset.Y + aCanvasArea.Y );
247 const Size aSizePixel( rRangePix.getMaxX() - rRangePix.getMinX(),
248 rRangePix.getMaxY() - rRangePix.getMinY() );
250 if( mpMediaWindow )
252 mpMediaWindow->SetPosSizePixel( aPosPixel, aSizePixel );
253 mxPlayerWindow->setPosSize( 0, 0,
254 aSizePixel.Width(), aSizePixel.Height(),
255 0 );
257 else
259 mxPlayerWindow->setPosSize( aPosPixel.X(), aPosPixel.Y(),
260 aSizePixel.Width(), aSizePixel.Height(),
261 0 );
264 return true;
268 bool ViewMediaShape::implInitialize( const ::basegfx::B2DRectangle& rBounds )
270 if( !mxPlayer.is() && mxShape.is() )
272 ENSURE_OR_RETURN_FALSE( mpViewLayer->getCanvas(),
273 "ViewMediaShape::implInitialize(): Invalid layer canvas" );
275 uno::Reference< rendering::XCanvas > xCanvas( mpViewLayer->getCanvas()->getUNOCanvas() );
277 if( xCanvas.is() )
279 uno::Reference< beans::XPropertySet > xPropSet;
282 xPropSet.set( mxShape, uno::UNO_QUERY );
283 OUString sMimeType;
285 // create Player
286 if (xPropSet.is())
288 OUString aURL;
289 xPropSet->getPropertyValue("MediaMimeType") >>= sMimeType;
290 if ((xPropSet->getPropertyValue("PrivateTempFileURL") >>= aURL)
291 && !aURL.isEmpty())
293 implInitializeMediaPlayer( aURL, sMimeType );
295 else if (xPropSet->getPropertyValue("MediaURL") >>= aURL)
297 implInitializeMediaPlayer( aURL, sMimeType );
301 // create visible object
302 uno::Sequence< uno::Any > aDeviceParams;
304 if( ::canvas::tools::getDeviceInfo( xCanvas, aDeviceParams ).getLength() > 1 )
306 implInitializePlayerWindow( rBounds, aDeviceParams );
309 // set player properties
310 implSetMediaProperties( xPropSet );
312 catch( uno::RuntimeException& )
314 throw;
316 catch( uno::Exception& )
318 TOOLS_WARN_EXCEPTION( "slideshow", "" );
323 return mxPlayer.is() || mxPlayerWindow.is();
327 void ViewMediaShape::implSetMediaProperties( const uno::Reference< beans::XPropertySet >& rxProps )
329 if( !mxPlayer.is() )
330 return;
332 mxPlayer->setMediaTime( 0.0 );
334 if( !rxProps.is() )
335 return;
337 bool bLoop( false );
338 getPropertyValue( bLoop,
339 rxProps,
340 "Loop");
341 mxPlayer->setPlaybackLoop( bLoop );
343 bool bMute( false );
344 getPropertyValue( bMute,
345 rxProps,
346 "Mute");
347 mxPlayer->setMute( bMute || !mbIsSoundEnabled);
349 sal_Int16 nVolumeDB(0);
350 getPropertyValue( nVolumeDB,
351 rxProps,
352 "VolumeDB");
353 mxPlayer->setVolumeDB( nVolumeDB );
355 if( mxPlayerWindow.is() )
357 media::ZoomLevel eZoom(media::ZoomLevel_FIT_TO_WINDOW);
358 getPropertyValue( eZoom,
359 rxProps,
360 "Zoom");
361 mxPlayerWindow->setZoomLevel( eZoom );
366 void ViewMediaShape::implInitializeMediaPlayer( const OUString& rMediaURL, const OUString& rMimeType )
368 #if !HAVE_FEATURE_AVMEDIA
369 (void) rMediaURL;
370 (void) rMimeType;
371 #else
372 if( mxPlayer.is() )
373 return;
377 if( !rMediaURL.isEmpty() )
379 mxPlayer = avmedia::MediaWindow::createPlayer( rMediaURL, ""/*TODO!*/, &rMimeType );
382 catch( uno::RuntimeException& )
384 throw;
386 catch( const uno::Exception& )
388 throw lang::NoSupportException( "No video support for " + rMediaURL );
390 #endif
394 void ViewMediaShape::implInitializePlayerWindow( const ::basegfx::B2DRectangle& rBounds,
395 const uno::Sequence< uno::Any >& rVCLDeviceParams )
397 SAL_INFO("slideshow", "ViewMediaShape::implInitializePlayerWindow" );
398 if( mpMediaWindow || rBounds.isEmpty() )
399 return;
403 sal_Int64 aVal=0;
405 rVCLDeviceParams[ 1 ] >>= aVal;
407 OutputDevice* pDevice = reinterpret_cast<OutputDevice*>(aVal);
408 vcl::Window* pWindow = pDevice ? pDevice->GetOwnerWindow() : nullptr;
410 if( pWindow )
412 ::basegfx::B2DRange aTmpRange;
413 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
414 rBounds,
415 mpViewLayer->getTransformation() );
416 const ::basegfx::B2IRange& rRangePix(
417 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
419 if( !rRangePix.isEmpty() )
421 awt::Rectangle aAWTRect( rRangePix.getMinX(),
422 rRangePix.getMinY(),
423 rRangePix.getMaxX() - rRangePix.getMinX(),
424 rRangePix.getMaxY() - rRangePix.getMinY() );
426 mpMediaWindow.disposeAndClear();
427 mpMediaWindow = VclPtr<SystemChildWindow>::Create( pWindow, WB_CLIPCHILDREN );
428 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(mpViewLayer));
429 if (xUnoView)
431 awt::Rectangle aCanvasArea = xUnoView->getUnoView()->getCanvasArea();
432 aAWTRect.X += aCanvasArea.X;
433 aAWTRect.Y += aCanvasArea.Y;
435 mpMediaWindow->SetPosSizePixel( Point( aAWTRect.X, aAWTRect.Y ),
436 Size( aAWTRect.Width, aAWTRect.Height ) );
438 mpMediaWindow->SetBackground( COL_BLACK );
439 mpMediaWindow->SetParentClipMode( ParentClipMode::NoClip );
440 mpMediaWindow->EnableEraseBackground( false );
441 mpMediaWindow->SetForwardKey( true );
442 mpMediaWindow->SetMouseTransparent( true );
443 mpMediaWindow->Show();
445 if( mxPlayer.is() )
447 sal_IntPtr nParentWindowHandle(0);
448 const SystemEnvData* pEnvData = mpMediaWindow->GetSystemData();
449 // tdf#139609 gtk doesn't need the handle, and fetching it is undesirable
450 if (!pEnvData || pEnvData->toolkit != SystemEnvData::Toolkit::Gtk)
451 nParentWindowHandle = mpMediaWindow->GetParentWindowHandle();
453 aAWTRect.X = aAWTRect.Y = 0;
455 SdrObject* pObj = SdrObject::getSdrObjectFromXShape(mxShape);
456 auto pMediaObj = dynamic_cast<SdrMediaObj*>(pObj);
457 const avmedia::MediaItem* pMediaItem = nullptr;
458 if (pMediaObj)
460 pMediaItem = &pMediaObj->getMediaProperties();
463 uno::Sequence< uno::Any > aArgs{
464 uno::Any(nParentWindowHandle),
465 uno::Any(aAWTRect),
466 uno::Any(reinterpret_cast< sal_IntPtr >( mpMediaWindow.get() )),
467 // Media item contains media properties, e.g. cropping.
468 uno::Any(reinterpret_cast< sal_IntPtr >( pMediaItem ))
471 mxPlayerWindow.set( mxPlayer->createPlayerWindow( aArgs ) );
473 if( mxPlayerWindow.is() )
475 mxPlayerWindow->setVisible( true );
476 mxPlayerWindow->setEnable( true );
480 if( !mxPlayerWindow.is() )
482 //if there was no playerwindow, then clear the mpMediaWindow too
483 //so that we can draw a placeholder instead in that space
484 mpMediaWindow.disposeAndClear();
489 catch( uno::RuntimeException& )
491 throw;
493 catch( uno::Exception& )
495 TOOLS_WARN_EXCEPTION( "slideshow", "" );
500 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */