merge the formfield patch from ooo-build
[ooovba.git] / avmedia / source / viewer / mediawindow.cxx
blobd345bb8056eefaa085195c9bd75966e3c32514c2
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: mediawindow.cxx,v $
10 * $Revision: 1.12 $
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 <stdio.h>
33 #include <avmedia/mediawindow.hxx>
34 #include "mediawindow_impl.hxx"
35 #include "mediamisc.hxx"
36 #include "mediawindow.hrc"
37 #include <tools/urlobj.hxx>
38 #include <vcl/msgbox.hxx>
39 #include <svtools/pathoptions.hxx>
40 #include <sfx2/filedlghelper.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/media/XManager.hpp>
44 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
46 #define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
48 using namespace ::com::sun::star;
50 namespace avmedia {
52 // ---------------
53 // - MediaWindow -
54 // ---------------
56 MediaWindow::MediaWindow( Window* parent, bool bInternalMediaControl ) :
57 mpImpl( new priv::MediaWindowImpl( parent, this, bInternalMediaControl ) )
59 mpImpl->Show();
62 // -------------------------------------------------------------------------
64 MediaWindow::~MediaWindow()
66 mpImpl->cleanUp();
67 delete mpImpl;
68 mpImpl = NULL;
71 // -------------------------------------------------------------------------
73 void MediaWindow::setURL( const ::rtl::OUString& rURL )
75 if( mpImpl )
76 mpImpl->setURL( rURL );
79 // -------------------------------------------------------------------------
81 const ::rtl::OUString& MediaWindow::getURL() const
83 return mpImpl->getURL();
86 // -------------------------------------------------------------------------
88 bool MediaWindow::isValid() const
90 return( mpImpl != NULL && mpImpl->isValid() );
93 // -------------------------------------------------------------------------
95 void MediaWindow::MouseMove( const MouseEvent& /* rMEvt */ )
99 // ---------------------------------------------------------------------
101 void MediaWindow::MouseButtonDown( const MouseEvent& /* rMEvt */ )
105 // ---------------------------------------------------------------------
107 void MediaWindow::MouseButtonUp( const MouseEvent& /* rMEvt */ )
111 // -------------------------------------------------------------------------
113 void MediaWindow::KeyInput( const KeyEvent& /* rKEvt */ )
117 // -------------------------------------------------------------------------
119 void MediaWindow::KeyUp( const KeyEvent& /* rKEvt */ )
123 // -------------------------------------------------------------------------
125 void MediaWindow::Command( const CommandEvent& /* rCEvt */ )
129 // -------------------------------------------------------------------------
131 sal_Int8 MediaWindow::AcceptDrop( const AcceptDropEvent& /* rEvt */ )
133 return 0;
136 // -------------------------------------------------------------------------
138 sal_Int8 MediaWindow::ExecuteDrop( const ExecuteDropEvent& /* rEvt */ )
140 return 0;
143 // -------------------------------------------------------------------------
145 void MediaWindow::StartDrag( sal_Int8 /* nAction */, const Point& /* rPosPixel */ )
149 // -------------------------------------------------------------------------
151 bool MediaWindow::hasPreferredSize() const
153 return( mpImpl != NULL && mpImpl->hasPreferredSize() );
156 // -------------------------------------------------------------------------
158 Size MediaWindow::getPreferredSize() const
160 return mpImpl->getPreferredSize();
163 // -------------------------------------------------------------------------
165 void MediaWindow::setPosSize( const Rectangle& rNewRect )
167 if( mpImpl )
168 mpImpl->setPosSize( rNewRect );
171 // -------------------------------------------------------------------------
173 Rectangle MediaWindow::getPosSize() const
175 return Rectangle( mpImpl->GetPosPixel(), mpImpl->GetSizePixel() );
178 // -------------------------------------------------------------------------
180 void MediaWindow::setPointer( const Pointer& rPointer )
182 if( mpImpl )
183 mpImpl->setPointer( rPointer );
186 // -------------------------------------------------------------------------
188 const Pointer& MediaWindow::getPointer() const
190 return mpImpl->getPointer();
193 // -------------------------------------------------------------------------
195 bool MediaWindow::setZoom( ::com::sun::star::media::ZoomLevel eLevel )
197 return( mpImpl != NULL && mpImpl->setZoom( eLevel ) );
200 // -------------------------------------------------------------------------
202 ::com::sun::star::media::ZoomLevel MediaWindow::getZoom() const
204 return mpImpl->getZoom();
207 // -------------------------------------------------------------------------
209 bool MediaWindow::start()
211 return( mpImpl != NULL && mpImpl->start() );
214 // -------------------------------------------------------------------------
216 void MediaWindow::stop()
218 if( mpImpl )
219 mpImpl->stop();
222 // -------------------------------------------------------------------------
224 bool MediaWindow::isPlaying() const
226 return( mpImpl != NULL && mpImpl->isPlaying() );
229 // -------------------------------------------------------------------------
231 double MediaWindow::getDuration() const
233 return mpImpl->getDuration();
236 // -------------------------------------------------------------------------
238 void MediaWindow::setMediaTime( double fTime )
240 if( mpImpl )
241 mpImpl->setMediaTime( fTime );
244 // -------------------------------------------------------------------------
246 double MediaWindow::getMediaTime() const
248 return mpImpl->getMediaTime();
251 // -------------------------------------------------------------------------
253 void MediaWindow::setStopTime( double fTime )
255 if( mpImpl )
256 mpImpl->setStopTime( fTime );
259 // -------------------------------------------------------------------------
261 double MediaWindow::getStopTime() const
263 return mpImpl->getStopTime();
266 // -------------------------------------------------------------------------
268 void MediaWindow::setRate( double fRate )
270 if( mpImpl )
271 mpImpl->setRate( fRate );
274 // -------------------------------------------------------------------------
276 double MediaWindow::getRate() const
278 return mpImpl->getRate();
281 // -------------------------------------------------------------------------
283 void MediaWindow::setPlaybackLoop( bool bSet )
285 if( mpImpl )
286 mpImpl->setPlaybackLoop( bSet );
289 // -------------------------------------------------------------------------
291 bool MediaWindow::isPlaybackLoop() const
293 return mpImpl->isPlaybackLoop();
296 // -------------------------------------------------------------------------
298 void MediaWindow::setMute( bool bSet )
300 if( mpImpl )
301 mpImpl->setMute( bSet );
304 // -------------------------------------------------------------------------
306 bool MediaWindow::isMute() const
308 return mpImpl->isMute();
311 // -------------------------------------------------------------------------
313 void MediaWindow::updateMediaItem( MediaItem& rItem ) const
315 if( mpImpl )
316 mpImpl->updateMediaItem( rItem );
319 // -------------------------------------------------------------------------
321 void MediaWindow::executeMediaItem( const MediaItem& rItem )
323 if( mpImpl )
324 mpImpl->executeMediaItem( rItem );
327 // -------------------------------------------------------------------------
329 void MediaWindow::show()
331 if( mpImpl )
332 mpImpl->Show();
335 // -------------------------------------------------------------------------
337 void MediaWindow::hide()
339 if( mpImpl )
340 mpImpl->Hide();
343 // -------------------------------------------------------------------------
345 void MediaWindow::enable()
347 if( mpImpl )
348 mpImpl->Enable();
351 // -------------------------------------------------------------------------
353 void MediaWindow::disable()
355 if( mpImpl )
356 mpImpl->Disable();
359 // -------------------------------------------------------------------------
361 Window* MediaWindow::getWindow() const
363 return mpImpl;
366 // -------------------------------------------------------------------------
368 void MediaWindow::getMediaFilters( FilterNameVector& rFilterNameVector )
370 static const char* pFilters[] = { "AIF Audio", "aif;aiff",
371 "AU Audio", "au",
372 "AVI", "avi",
373 "CD Audio", "cda",
374 "FLAC Audio", "flac",
375 "MIDI Audio", "mid;midi",
376 "MPEG Audio", "mp2;mp3;mpa",
377 "MPEG Video", "mpg;mpeg;mpv;mp4",
378 "Ogg bitstream", "ogg",
379 "Quicktime Video", "mov",
380 "Vivo Video", "viv",
381 "WAVE Audio", "wav" };
383 unsigned int i;
384 for( i = 0; i < ( sizeof( pFilters ) / sizeof( char* ) ); i += 2 )
386 rFilterNameVector.push_back( ::std::make_pair< ::rtl::OUString, ::rtl::OUString >(
387 ::rtl::OUString::createFromAscii( pFilters[ i ] ),
388 ::rtl::OUString::createFromAscii( pFilters[ i + 1 ] ) ) );
392 // -------------------------------------------------------------------------
394 bool MediaWindow::executeMediaURLDialog( Window* /* pParent */, ::rtl::OUString& rURL, bool bInsertDialog )
396 ::sfx2::FileDialogHelper aDlg( com::sun::star::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0 );
397 static const ::rtl::OUString aWildcard( RTL_CONSTASCII_USTRINGPARAM( "*." ) );
398 FilterNameVector aFilters;
399 const ::rtl::OUString aSeparator( RTL_CONSTASCII_USTRINGPARAM( ";" ) );
400 ::rtl::OUString aAllTypes;
402 aDlg.SetTitle( AVMEDIA_RESID( bInsertDialog ? AVMEDIA_STR_INSERTMEDIA_DLG : AVMEDIA_STR_OPENMEDIA_DLG ) );
404 getMediaFilters( aFilters );
406 unsigned int i;
407 for( i = 0; i < aFilters.size(); ++i )
409 for( sal_Int32 nIndex = 0; nIndex >= 0; )
411 if( aAllTypes.getLength() )
412 aAllTypes += aSeparator;
414 ( aAllTypes += aWildcard ) += aFilters[ i ].second.getToken( 0, ';', nIndex );
418 // add filter for all media types
419 aDlg.AddFilter( AVMEDIA_RESID( AVMEDIA_STR_ALL_MEDIAFILES ), aAllTypes );
421 for( i = 0; i < aFilters.size(); ++i )
423 ::rtl::OUString aTypes;
425 for( sal_Int32 nIndex = 0; nIndex >= 0; )
427 if( aTypes.getLength() )
428 aTypes += aSeparator;
430 ( aTypes += aWildcard ) += aFilters[ i ].second.getToken( 0, ';', nIndex );
433 // add single filters
434 aDlg.AddFilter( aFilters[ i ].first, aTypes );
437 // add filter for all types
438 aDlg.AddFilter( AVMEDIA_RESID( AVMEDIA_STR_ALL_FILES ), String( RTL_CONSTASCII_USTRINGPARAM( "*.*" ) ) );
440 if( aDlg.Execute() == ERRCODE_NONE )
442 const INetURLObject aURL( aDlg.GetPath() );
443 rURL = aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
445 else if( rURL.getLength() )
446 rURL = ::rtl::OUString();
448 return( rURL.getLength() > 0 );
451 // -------------------------------------------------------------------------
453 void MediaWindow::executeFormatErrorBox( Window* pParent )
455 ErrorBox aErrBox( pParent, AVMEDIA_RESID( AVMEDIA_ERR_URL ) );
457 aErrBox.Execute();
460 // -------------------------------------------------------------------------
462 bool MediaWindow::isMediaURL( const ::rtl::OUString& rURL, bool bDeep, Size* pPreferredSizePixel )
464 const INetURLObject aURL( rURL );
465 bool bRet = false;
467 if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
469 if( bDeep || pPreferredSizePixel )
471 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
473 if( xFactory.is() )
477 fprintf(stderr, "-->%s uno reference \n\n",AVMEDIA_MANAGER_SERVICE_NAME);
479 uno::Reference< ::com::sun::star::media::XManager > xManager(
480 xFactory->createInstance( ::rtl::OUString::createFromAscii( AVMEDIA_MANAGER_SERVICE_NAME ) ),
481 uno::UNO_QUERY );
483 if( xManager.is() )
485 uno::Reference< media::XPlayer > xPlayer( xManager->createPlayer( aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) ) );
487 if( xPlayer.is() )
489 bRet = true;
491 if( pPreferredSizePixel )
493 const awt::Size aAwtSize( xPlayer->getPreferredPlayerWindowSize() );
495 pPreferredSizePixel->Width() = aAwtSize.Width;
496 pPreferredSizePixel->Height() = aAwtSize.Height;
501 catch( ... )
506 else
508 FilterNameVector aFilters;
509 const ::rtl::OUString aExt( aURL.getExtension() );
511 getMediaFilters( aFilters );
513 unsigned int i;
514 for( i = 0; ( i < aFilters.size() ) && !bRet; ++i )
516 for( sal_Int32 nIndex = 0; nIndex >= 0 && !bRet; )
518 if( aExt.equalsIgnoreAsciiCase( aFilters[ i ].second.getToken( 0, ';', nIndex ) ) )
519 bRet = true;
525 return bRet;
528 // -------------------------------------------------------------------------
530 uno::Reference< media::XPlayer > MediaWindow::createPlayer( const ::rtl::OUString& rURL )
532 return priv::MediaWindowImpl::createPlayer( rURL );
535 // -------------------------------------------------------------------------
537 uno::Reference< graphic::XGraphic > MediaWindow::grabFrame( const ::rtl::OUString& rURL,
538 bool bAllowToCreateReplacementGraphic,
539 double fMediaTime )
541 uno::Reference< media::XPlayer > xPlayer( createPlayer( rURL ) );
542 uno::Reference< graphic::XGraphic > xRet;
543 ::std::auto_ptr< Graphic > apGraphic;
545 if( xPlayer.is() )
547 uno::Reference< media::XFrameGrabber > xGrabber( xPlayer->createFrameGrabber() );
549 if( xGrabber.is() )
551 if( AVMEDIA_FRAMEGRABBER_DEFAULTFRAME == fMediaTime )
552 fMediaTime = AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME;
554 if( fMediaTime >= xPlayer->getDuration() )
555 fMediaTime = ( xPlayer->getDuration() * 0.5 );
557 xRet = xGrabber->grabFrame( fMediaTime );
560 if( !xRet.is() && bAllowToCreateReplacementGraphic )
562 awt::Size aPrefSize( xPlayer->getPreferredPlayerWindowSize() );
564 if( !aPrefSize.Width && !aPrefSize.Height )
566 const BitmapEx aBmpEx( AVMEDIA_RESID( AVMEDIA_BMP_AUDIOLOGO ) );
567 apGraphic.reset( new Graphic( aBmpEx ) );
572 if( !xRet.is() && !apGraphic.get() && bAllowToCreateReplacementGraphic )
574 const BitmapEx aBmpEx( AVMEDIA_RESID( AVMEDIA_BMP_EMPTYLOGO ) );
575 apGraphic.reset( new Graphic( aBmpEx ) );
578 if( apGraphic.get() )
579 xRet = apGraphic->GetXGraphic();
581 return xRet;
584 } // namespace avemdia