merge the formfield patch from ooo-build
[ooovba.git] / avmedia / source / viewer / mediawindowbase_impl.cxx
blobd7b42104eda72a49c27dd440c5ca4b9321f8dd0a
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: mediawindowbase_impl.cxx,v $
10 * $Revision: 1.7 $
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 #include "mediawindowbase_impl.hxx"
32 #include <avmedia/mediaitem.hxx>
33 #include "mediamisc.hxx"
34 #include "mediawindow.hrc"
35 #include <tools/urlobj.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/media/XManager.hpp>
39 #ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HDL_
40 #include <com/sun/star/lang/XComponent.hdl>
41 #endif
43 #define MEDIA_TIMER_TIMEOUT 100
45 using namespace ::com::sun::star;
47 namespace avmedia { namespace priv {
49 // -----------------------
50 // - MediaWindowBaseImpl -
51 // -----------------------
53 MediaWindowBaseImpl::MediaWindowBaseImpl( MediaWindow* pMediaWindow ) :
54 mpMediaWindow( pMediaWindow )
58 // ---------------------------------------------------------------------
60 MediaWindowBaseImpl::~MediaWindowBaseImpl()
62 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
65 // -------------------------------------------------------------------------
67 uno::Reference< media::XPlayer > MediaWindowBaseImpl::createPlayer( const ::rtl::OUString& rURL )
69 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
70 uno::Reference< media::XPlayer > xPlayer;
72 if( xFactory.is() )
74 try
77 uno::Reference< ::com::sun::star::media::XManager > xManager(
78 xFactory->createInstance( ::rtl::OUString::createFromAscii( AVMEDIA_MANAGER_SERVICE_NAME ) ),
79 uno::UNO_QUERY );
81 if( xManager.is() )
83 xPlayer = uno::Reference< ::com::sun::star::media::XPlayer >(
84 xManager->createPlayer( rURL ), uno::UNO_QUERY );
87 catch( ... )
92 return xPlayer;
96 // ---------------------------------------------------------------------
98 void MediaWindowBaseImpl::setURL( const ::rtl::OUString& rURL )
100 if( rURL != getURL() )
102 INetURLObject aURL( maFileURL = rURL );
104 if( mxPlayer.is() )
105 mxPlayer->stop();
107 if( mxPlayerWindow.is() )
109 mxPlayerWindow->setVisible( false );
110 mxPlayerWindow.clear();
113 mxPlayer.clear();
115 if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
116 maFileURL = aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
118 mxPlayer = createPlayer( maFileURL );
119 onURLChanged();
123 // ---------------------------------------------------------------------
125 void MediaWindowBaseImpl::onURLChanged()
129 // ---------------------------------------------------------------------
131 const ::rtl::OUString& MediaWindowBaseImpl::getURL() const
133 return maFileURL;
136 // ---------------------------------------------------------------------
138 bool MediaWindowBaseImpl::isValid() const
140 return( getPlayer().is() );
143 // ---------------------------------------------------------------------
145 void MediaWindowBaseImpl::stopPlayingInternal( bool bStop )
147 if( isPlaying() )
149 if( bStop )
150 mxPlayer->start();
151 else
152 mxPlayer->stop();
156 // ---------------------------------------------------------------------
158 MediaWindow* MediaWindowBaseImpl::getMediaWindow() const
160 return mpMediaWindow;
163 // ---------------------------------------------------------------------
165 uno::Reference< media::XPlayer > MediaWindowBaseImpl::getPlayer() const
167 return mxPlayer;
170 // ---------------------------------------------------------------------
172 uno::Reference< media::XPlayerWindow > MediaWindowBaseImpl::getPlayerWindow() const
174 return mxPlayerWindow;
177 // ---------------------------------------------------------------------
179 void MediaWindowBaseImpl::setPlayerWindow( const uno::Reference< media::XPlayerWindow >& rxPlayerWindow )
181 mxPlayerWindow = rxPlayerWindow;
184 // ---------------------------------------------------------------------
186 void MediaWindowBaseImpl::cleanUp()
188 if( mxPlayer.is() )
190 mxPlayer->stop();
192 uno::Reference< lang::XComponent > xComponent( mxPlayer, uno::UNO_QUERY );
194 if( xComponent.is() )
195 xComponent->dispose();
197 mxPlayer.clear();
200 mpMediaWindow = NULL;
203 // ---------------------------------------------------------------------
205 bool MediaWindowBaseImpl::hasPreferredSize() const
207 return( mxPlayerWindow.is() );
210 // ---------------------------------------------------------------------
212 Size MediaWindowBaseImpl::getPreferredSize() const
214 Size aRet;
216 if( mxPlayer.is() )
218 awt::Size aPrefSize( mxPlayer->getPreferredPlayerWindowSize() );
220 aRet.Width() = aPrefSize.Width;
221 aRet.Height() = aPrefSize.Height;
224 return aRet;
227 // ---------------------------------------------------------------------
229 bool MediaWindowBaseImpl::setZoom( ::com::sun::star::media::ZoomLevel eLevel )
231 return( mxPlayerWindow.is() ? mxPlayerWindow->setZoomLevel( eLevel ) : false );
234 // -------------------------------------------------------------------------
236 ::com::sun::star::media::ZoomLevel MediaWindowBaseImpl::getZoom() const
238 return( mxPlayerWindow.is() ? mxPlayerWindow->getZoomLevel() : ::com::sun::star::media::ZoomLevel_NOT_AVAILABLE );
241 // ---------------------------------------------------------------------
243 bool MediaWindowBaseImpl::start()
245 return( mxPlayer.is() ? ( mxPlayer->start(), true ) : false );
248 // ---------------------------------------------------------------------
250 void MediaWindowBaseImpl::stop()
252 if( mxPlayer.is() )
253 mxPlayer->stop();
256 // ---------------------------------------------------------------------
258 bool MediaWindowBaseImpl::isPlaying() const
260 return( mxPlayer.is() && mxPlayer->isPlaying() );
263 // ---------------------------------------------------------------------
265 double MediaWindowBaseImpl::getDuration() const
267 return( mxPlayer.is() ? mxPlayer->getDuration() : 0.0 );
270 // ---------------------------------------------------------------------
272 void MediaWindowBaseImpl::setMediaTime( double fTime )
274 if( mxPlayer.is() )
275 mxPlayer->setMediaTime( fTime );
278 // ---------------------------------------------------------------------
280 double MediaWindowBaseImpl::getMediaTime() const
282 return( mxPlayer.is() ? mxPlayer->getMediaTime() : 0.0 );
285 // ---------------------------------------------------------------------
287 void MediaWindowBaseImpl::setStopTime( double fTime )
289 if( mxPlayer.is() )
290 mxPlayer->setStopTime( fTime );
293 // ---------------------------------------------------------------------
295 double MediaWindowBaseImpl::getStopTime() const
297 return( mxPlayer.is() ? mxPlayer->getStopTime() : 0.0 );
300 // ---------------------------------------------------------------------
302 void MediaWindowBaseImpl::setRate( double fRate )
304 if( mxPlayer.is() )
305 mxPlayer->setRate( fRate );
308 // ---------------------------------------------------------------------
310 double MediaWindowBaseImpl::getRate() const
312 return( mxPlayer.is() ? mxPlayer->getRate() : 0.0 );
315 // ---------------------------------------------------------------------
317 void MediaWindowBaseImpl::setPlaybackLoop( bool bSet )
319 if( mxPlayer.is() )
320 mxPlayer->setPlaybackLoop( bSet );
323 // ---------------------------------------------------------------------
325 bool MediaWindowBaseImpl::isPlaybackLoop() const
327 return( mxPlayer.is() ? mxPlayer->isPlaybackLoop() : false );
330 // ---------------------------------------------------------------------
332 void MediaWindowBaseImpl::setMute( bool bSet )
334 if( mxPlayer.is() )
335 mxPlayer->setMute( bSet );
338 // ---------------------------------------------------------------------
340 bool MediaWindowBaseImpl::isMute() const
342 return( mxPlayer.is() ? mxPlayer->isMute() : false );
345 // ---------------------------------------------------------------------
347 void MediaWindowBaseImpl::setVolumeDB( sal_Int16 nVolumeDB )
349 if( mxPlayer.is() )
350 mxPlayer->setVolumeDB( nVolumeDB );
353 // ---------------------------------------------------------------------
355 sal_Int16 MediaWindowBaseImpl::getVolumeDB() const
357 return( mxPlayer.is() ? mxPlayer->getVolumeDB() : 0 );
360 // -------------------------------------------------------------------------
362 void MediaWindowBaseImpl::updateMediaItem( MediaItem& rItem ) const
364 if( isPlaying() )
365 rItem.setState( ( getRate() > 1.0 ) ? MEDIASTATE_PLAYFFW : MEDIASTATE_PLAY );
366 else
367 rItem.setState( ( 0.0 == getMediaTime() ) ? MEDIASTATE_STOP : MEDIASTATE_PAUSE );
369 rItem.setDuration( getDuration() );
370 rItem.setTime( getMediaTime() );
371 rItem.setLoop( isPlaybackLoop() );
372 rItem.setMute( isMute() );
373 rItem.setVolumeDB( getVolumeDB() );
374 rItem.setZoom( getZoom() );
375 rItem.setURL( getURL() );
378 // -------------------------------------------------------------------------
380 void MediaWindowBaseImpl::executeMediaItem( const MediaItem& rItem )
382 const sal_uInt32 nMaskSet = rItem.getMaskSet();
384 // set URL first
385 if( nMaskSet & AVMEDIA_SETMASK_URL )
386 setURL( rItem.getURL() );
388 // set different states next
389 if( nMaskSet & AVMEDIA_SETMASK_TIME )
390 setMediaTime( ::std::min( rItem.getTime(), getDuration() ) );
392 if( nMaskSet & AVMEDIA_SETMASK_LOOP )
393 setPlaybackLoop( rItem.isLoop() );
395 if( nMaskSet & AVMEDIA_SETMASK_MUTE )
396 setMute( rItem.isMute() );
398 if( nMaskSet & AVMEDIA_SETMASK_VOLUMEDB )
399 setVolumeDB( rItem.getVolumeDB() );
401 if( nMaskSet & AVMEDIA_SETMASK_ZOOM )
402 setZoom( rItem.getZoom() );
404 // set play state at last
405 if( nMaskSet & AVMEDIA_SETMASK_STATE )
407 switch( rItem.getState() )
409 case( MEDIASTATE_PLAY ):
410 case( MEDIASTATE_PLAYFFW ):
413 const double fNewRate = ( ( MEDIASTATE_PLAYFFW == rItem.getState() ) ? AVMEDIA_FFW_PLAYRATE : 1.0 );
415 if( getRate() != fNewRate )
416 setRate( fNewRate );
418 if( !isPlaying() )
419 start();
421 break;
423 case( MEDIASTATE_PAUSE ):
425 if( isPlaying() )
426 stop();
428 break;
430 case( MEDIASTATE_STOP ):
432 if( isPlaying() )
434 setMediaTime( 0.0 );
435 stop();
436 setMediaTime( 0.0 );
439 break;
444 } // namespace priv
445 } // namespace avemdia