merge the formfield patch from ooo-build
[ooovba.git] / slideshow / source / engine / shapes / viewmediashape.cxx
blobcc46e5330c8326d238da36b5b7af4f10c3591451
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: viewmediashape.cxx,v $
10 * $Revision: 1.6.2.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
34 // must be first
35 #include <canvas/debug.hxx>
36 #include <tools/diagnose_ex.h>
37 #include <canvas/verbosetrace.hxx>
39 #include <math.h>
41 #include <comphelper/anytostring.hxx>
42 #include <cppuhelper/exc_hlp.hxx>
44 #include <vcl/window.hxx>
45 #include <vcl/javachild.hxx>
46 #include <vcl/salbtype.hxx>
47 #ifdef GSTREAMER
48 #include <vcl/sysdata.hxx>
49 #endif
51 #include <basegfx/tools/canvastools.hxx>
52 #include <basegfx/numeric/ftools.hxx>
53 #include <basegfx/polygon/b2dpolygon.hxx>
54 #include <basegfx/point/b2dpoint.hxx>
55 #include <basegfx/matrix/b2dhommatrix.hxx>
56 #include <basegfx/polygon/b2dpolygontools.hxx>
57 #include <basegfx/range/b2irange.hxx>
58 #include <canvas/canvastools.hxx>
59 #include <cppcanvas/vclfactory.hxx>
60 #include <cppcanvas/basegfxfactory.hxx>
61 #include <cppcanvas/basegfxfactory.hxx>
62 #include <avmedia/mediawindow.hxx>
64 #include <com/sun/star/media/XManager.hpp>
65 #include <com/sun/star/media/XPlayer.hpp>
66 #include <com/sun/star/media/XPlayerWindow.hpp>
67 #include <com/sun/star/beans/XPropertySet.hpp>
68 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
69 #include <com/sun/star/lang/NoSupportException.hpp>
70 #include <com/sun/star/awt/XWindow.hpp>
71 #include <com/sun/star/rendering/XCanvas.hpp>
72 #include <com/sun/star/lang/XComponent.hdl>
74 #include "viewmediashape.hxx"
75 #include "mediashape.hxx"
76 #include "tools.hxx"
77 #include "unoview.hxx"
79 using namespace ::com::sun::star;
81 namespace slideshow
83 namespace internal
85 ViewMediaShape::ViewMediaShape( const ViewLayerSharedPtr& rViewLayer,
86 const uno::Reference< drawing::XShape >& rxShape,
87 const uno::Reference< uno::XComponentContext >& rxContext ) :
88 mpViewLayer( rViewLayer ),
89 mpMediaWindow(),
90 maWindowOffset( 0, 0 ),
91 maBounds(),
92 mxShape( rxShape ),
93 mxPlayer(),
94 mxPlayerWindow(),
95 mxComponentContext( rxContext ),
96 mbIsSoundEnabled(true)
98 ENSURE_OR_THROW( mxShape.is(), "ViewMediaShape::ViewMediaShape(): Invalid Shape" );
99 ENSURE_OR_THROW( mpViewLayer, "ViewMediaShape::ViewMediaShape(): Invalid View" );
100 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewMediaShape::ViewMediaShape(): Invalid ViewLayer canvas" );
101 ENSURE_OR_THROW( mxComponentContext.is(), "ViewMediaShape::ViewMediaShape(): Invalid component context" );
103 UnoViewSharedPtr pUnoView (::boost::dynamic_pointer_cast<UnoView>(rViewLayer));
104 if (pUnoView)
106 mbIsSoundEnabled = pUnoView->isSoundEnabled();
110 // ---------------------------------------------------------------------
112 ViewMediaShape::~ViewMediaShape()
116 endMedia();
118 catch (uno::Exception &)
120 OSL_ENSURE( false, rtl::OUStringToOString(
121 comphelper::anyToString(
122 cppu::getCaughtException() ),
123 RTL_TEXTENCODING_UTF8 ).getStr() );
127 // ---------------------------------------------------------------------
129 ViewLayerSharedPtr ViewMediaShape::getViewLayer() const
131 return mpViewLayer;
134 // ---------------------------------------------------------------------
136 bool ViewMediaShape::startMedia()
138 if( !mxPlayer.is() )
139 implInitialize( maBounds );
141 if( mxPlayer.is() && ( mxPlayer->getDuration() > 0.0 ) )
142 mxPlayer->start();
144 return true;
147 // ---------------------------------------------------------------------
149 void ViewMediaShape::endMedia()
151 // shutdown player window
152 if( mxPlayerWindow.is() )
154 uno::Reference< lang::XComponent > xComponent( mxPlayerWindow, uno::UNO_QUERY );
156 if( xComponent.is() )
157 xComponent->dispose();
159 mxPlayerWindow.clear();
162 #ifdef GSTREAMER
163 mpMediaWindow = ::std::auto_ptr< SystemChildWindow >();
164 #else
165 mpMediaWindow = ::std::auto_ptr< JavaChildWindow >();
166 #endif
168 // shutdown player
169 if( mxPlayer.is() )
171 mxPlayer->stop();
173 uno::Reference< lang::XComponent > xComponent( mxPlayer, uno::UNO_QUERY );
175 if( xComponent.is() )
176 xComponent->dispose();
178 mxPlayer.clear();
182 // ---------------------------------------------------------------------
184 void ViewMediaShape::pauseMedia()
186 if( mxPlayer.is() && ( mxPlayer->getDuration() > 0.0 ) )
187 mxPlayer->stop();
190 // ---------------------------------------------------------------------
192 void ViewMediaShape::setMediaTime(double fTime)
194 if( mxPlayer.is() && ( mxPlayer->getDuration() > 0.0 ) )
195 mxPlayer->setMediaTime(fTime);
198 // ---------------------------------------------------------------------
200 bool ViewMediaShape::render( const ::basegfx::B2DRectangle& rBounds ) const
202 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();;
204 if( !pCanvas )
205 return false;
207 if( !mpMediaWindow.get() && !mxPlayerWindow.is() )
209 // fill the shape background with black
210 fillRect( pCanvas,
211 rBounds,
212 0x000000FFU );
215 return true;
218 bool ViewMediaShape::resize( const ::basegfx::B2DRectangle& rNewBounds ) const
220 maBounds = rNewBounds;
222 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();;
224 if( !pCanvas )
225 return false;
227 if( !mxPlayerWindow.is() )
228 return true;
230 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
231 uno::UNO_QUERY );
233 uno::Reference< awt::XWindow > xParentWindow;
234 if( xPropSet.is() &&
235 getPropertyValue( xParentWindow,
236 xPropSet,
237 ::rtl::OUString::createFromAscii( "Window" )) )
239 const awt::Rectangle aRect( xParentWindow->getPosSize() );
241 maWindowOffset.X = aRect.X;
242 maWindowOffset.Y = aRect.Y;
245 ::basegfx::B2DRange aTmpRange;
246 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
247 rNewBounds,
248 mpViewLayer->getTransformation() );
249 const ::basegfx::B2IRange& rRangePix(
250 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
252 mxPlayerWindow->setEnable( !rRangePix.isEmpty() );
254 if( rRangePix.isEmpty() )
255 return true;
257 const Point aPosPixel( rRangePix.getMinX() + maWindowOffset.X,
258 rRangePix.getMinY() + maWindowOffset.Y );
259 const Size aSizePixel( rRangePix.getMaxX() - rRangePix.getMinX(),
260 rRangePix.getMaxY() - rRangePix.getMinY() );
262 if( mpMediaWindow.get() )
264 mpMediaWindow->SetPosSizePixel( aPosPixel, aSizePixel );
265 mxPlayerWindow->setPosSize( 0, 0,
266 aSizePixel.Width(), aSizePixel.Height(),
267 0 );
269 else
271 mxPlayerWindow->setPosSize( aPosPixel.X(), aPosPixel.Y(),
272 aSizePixel.Width(), aSizePixel.Height(),
273 0 );
276 return true;
279 // ---------------------------------------------------------------------
281 bool ViewMediaShape::implInitialize( const ::basegfx::B2DRectangle& rBounds )
283 if( !mxPlayer.is() && mxShape.is() )
285 ENSURE_OR_RETURN( mpViewLayer->getCanvas(),
286 "ViewMediaShape::update(): Invalid layer canvas" );
288 uno::Reference< rendering::XCanvas > xCanvas( mpViewLayer->getCanvas()->getUNOCanvas() );
290 if( xCanvas.is() )
292 uno::Reference< beans::XPropertySet > xPropSet;
293 ::rtl::OUString aURL;
297 xPropSet.set( mxShape, uno::UNO_QUERY );
299 // create Player
300 if( xPropSet.is() &&
301 ( xPropSet->getPropertyValue(
302 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaURL" ) ) ) >>=aURL ) )
304 implInitializeMediaPlayer( aURL );
307 // create visible object
308 uno::Sequence< uno::Any > aDeviceParams;
310 if( ::canvas::tools::getDeviceInfo( xCanvas, aDeviceParams ).getLength() > 1 )
312 ::rtl::OUString aImplName;
314 aDeviceParams[ 0 ] >>= aImplName;
316 if( aImplName.endsWithIgnoreAsciiCaseAsciiL(
317 RTL_CONSTASCII_STRINGPARAM("VCL") ) || aImplName.endsWithIgnoreAsciiCaseAsciiL(
318 RTL_CONSTASCII_STRINGPARAM("Cairo") ) )
320 implInitializeVCLBasedPlayerWindow( rBounds, aDeviceParams );
322 else if( aImplName.endsWithIgnoreAsciiCaseAsciiL(
323 RTL_CONSTASCII_STRINGPARAM("DX")) ||
324 aImplName.endsWithIgnoreAsciiCaseAsciiL(
325 RTL_CONSTASCII_STRINGPARAM("DX9")))
327 implInitializeDXBasedPlayerWindow( rBounds, aDeviceParams );
331 // set player properties
332 implSetMediaProperties( xPropSet );
334 catch( uno::RuntimeException& )
336 throw;
338 catch( uno::Exception& )
340 OSL_ENSURE( false,
341 rtl::OUStringToOString(
342 comphelper::anyToString( cppu::getCaughtException() ),
343 RTL_TEXTENCODING_UTF8 ).getStr() );
348 return mxPlayer.is() || mxPlayerWindow.is();
351 // ---------------------------------------------------------------------
353 void ViewMediaShape::implSetMediaProperties( const uno::Reference< beans::XPropertySet >& rxProps )
355 if( mxPlayer.is() )
357 mxPlayer->setMediaTime( 0.0 );
359 if( rxProps.is() )
361 sal_Bool bLoop( false );
362 getPropertyValue( bLoop,
363 rxProps,
364 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Loop" )));
365 mxPlayer->setPlaybackLoop( bLoop );
367 sal_Bool bMute( false );
368 getPropertyValue( bMute,
369 rxProps,
370 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Mute" )));
371 mxPlayer->setMute( bMute || !mbIsSoundEnabled);
373 sal_Int16 nVolumeDB(0);
374 getPropertyValue( nVolumeDB,
375 rxProps,
376 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VolumeDB" )));
377 mxPlayer->setVolumeDB( nVolumeDB );
379 if( mxPlayerWindow.is() )
381 media::ZoomLevel eZoom(media::ZoomLevel_FIT_TO_WINDOW);
382 getPropertyValue( eZoom,
383 rxProps,
384 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Zoom" )));
385 mxPlayerWindow->setZoomLevel( eZoom );
391 // ---------------------------------------------------------------------
393 void ViewMediaShape::implInitializeMediaPlayer( const ::rtl::OUString& rMediaURL )
395 if( !mxPlayer.is() )
399 if( rMediaURL.getLength() )
401 mxPlayer.set( avmedia::MediaWindow::createPlayer( rMediaURL ),
402 uno::UNO_QUERY );
405 catch( uno::RuntimeException& )
407 throw;
409 catch( const uno::Exception& )
411 throw lang::NoSupportException(
412 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
413 "No video support for ") ) + rMediaURL,
414 uno::Reference<uno::XInterface>() );
419 // ---------------------------------------------------------------------
421 bool ViewMediaShape::implInitializeVCLBasedPlayerWindow( const ::basegfx::B2DRectangle& rBounds,
422 const uno::Sequence< uno::Any >& rVCLDeviceParams)
424 OSL_TRACE( "ViewMediaShape::implInitializeVCLBasedPlayerWindow" );
425 if( !mpMediaWindow.get() && !rBounds.isEmpty() )
429 sal_Int64 aVal=0;
431 rVCLDeviceParams[ 1 ] >>= aVal;
433 Window* pWindow = reinterpret_cast< Window* >( aVal );
435 if( pWindow )
437 ::basegfx::B2DRange aTmpRange;
438 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
439 rBounds,
440 mpViewLayer->getTransformation() );
441 const ::basegfx::B2IRange& rRangePix(
442 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
444 if( !rRangePix.isEmpty() )
446 uno::Sequence< uno::Any > aArgs( 3 );
447 awt::Rectangle aAWTRect( rRangePix.getMinX(),
448 rRangePix.getMinY(),
449 rRangePix.getMaxX() - rRangePix.getMinX(),
450 rRangePix.getMaxY() - rRangePix.getMinY() );
452 #ifdef GSTREAMER
453 OSL_TRACE( "created sys child window for viewmediashape" );
454 mpMediaWindow = ::std::auto_ptr< SystemChildWindow >( new SystemChildWindow( pWindow, WB_CLIPCHILDREN ) );
455 #else
456 mpMediaWindow = ::std::auto_ptr< JavaChildWindow >( new JavaChildWindow( pWindow, WB_CLIPCHILDREN ) );
457 #endif
458 mpMediaWindow->SetBackground( Color( COL_BLACK ) );
459 mpMediaWindow->SetPosSizePixel( Point( aAWTRect.X,
460 aAWTRect.Y ),
461 Size( aAWTRect.Width,
462 aAWTRect.Height ));
463 mpMediaWindow->SetParentClipMode( PARENTCLIPMODE_NOCLIP );
464 mpMediaWindow->EnableEraseBackground( FALSE );
465 mpMediaWindow->EnablePaint( FALSE );
466 mpMediaWindow->SetForwardKey( TRUE );
467 mpMediaWindow->SetMouseTransparent( TRUE );
468 mpMediaWindow->Show();
470 if( mxPlayer.is() )
472 #ifndef GSTREAMER
473 aArgs[ 0 ] = uno::makeAny(
474 sal::static_int_cast<sal_IntPtr>(
475 mpMediaWindow->getParentWindowHandleForJava()) );
476 #else
477 aArgs[ 0 ] = uno::makeAny ( (sal_Int32) 0 );
478 #endif
479 aAWTRect.X = aAWTRect.Y = 0;
480 aArgs[ 1 ] = uno::makeAny( aAWTRect );
481 #ifdef GSTREAMER
482 const SystemEnvData *pSystemData = mpMediaWindow->GetSystemData();
483 OSL_TRACE( "xwindow id: %ld", pSystemData->aWindow );
484 aArgs[ 2 ] = uno::makeAny( pSystemData->aWindow );
485 #endif
487 mxPlayerWindow.set( mxPlayer->createPlayerWindow( aArgs ) );
489 if( mxPlayerWindow.is() )
491 mxPlayerWindow->setVisible( true );
492 mxPlayerWindow->setEnable( true );
498 catch( uno::RuntimeException& )
500 throw;
502 catch( uno::Exception& )
504 OSL_ENSURE( false,
505 rtl::OUStringToOString(
506 comphelper::anyToString( cppu::getCaughtException() ),
507 RTL_TEXTENCODING_UTF8 ).getStr() );
511 return mxPlayerWindow.is();
514 // ---------------------------------------------------------------------
516 bool ViewMediaShape::implInitializeDXBasedPlayerWindow( const ::basegfx::B2DRectangle& rBounds,
517 const uno::Sequence< uno::Any >& rDXDeviceParams )
519 if( !mxPlayerWindow.is() )
523 if( rDXDeviceParams.getLength() == 2 )
525 sal_Int64 aWNDVal=0;
527 rDXDeviceParams[ 1 ] >>= aWNDVal;
529 if( aWNDVal )
531 ::basegfx::B2DRange aTmpRange;
532 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
533 rBounds,
534 mpViewLayer->getTransformation() );
535 const ::basegfx::B2IRange& rRangePix(
536 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
538 if( !rRangePix.isEmpty() )
540 uno::Sequence< uno::Any > aArgs( 2 );
541 awt::Rectangle aAWTRect( rRangePix.getMinX() + maWindowOffset.X,
542 rRangePix.getMinY() + maWindowOffset.Y,
543 rRangePix.getMaxX() - rRangePix.getMinX(),
544 rRangePix.getMaxY() - rRangePix.getMinY() );
546 if( mxPlayer.is() )
548 aArgs[ 0 ] = uno::makeAny(
549 sal::static_int_cast<sal_Int32>(
550 aWNDVal) );
551 aArgs[ 1 ] = uno::makeAny( aAWTRect );
553 mxPlayerWindow.set( mxPlayer->createPlayerWindow( aArgs ) );
559 catch( uno::RuntimeException& )
561 throw;
563 catch( uno::Exception& )
565 OSL_ENSURE( false,
566 rtl::OUStringToOString(
567 comphelper::anyToString( cppu::getCaughtException() ),
568 RTL_TEXTENCODING_UTF8 ).getStr() );
572 return mxPlayerWindow.is();