Updated core
[LibreOffice.git] / avmedia / source / viewer / mediawindow_impl.cxx
blob274f58a2729a58b8db6981357a23d9cb88970e83
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 "mediawindow_impl.hxx"
21 #include "mediaevent_impl.hxx"
22 #include "mediamisc.hxx"
23 #include "mediawindow.hrc"
24 #include "helpids.hrc"
26 #include <algorithm>
27 #include <cmath>
28 #include <osl/mutex.hxx>
29 #include <vcl/svapp.hxx>
30 #ifdef UNX
31 #include <vcl/sysdata.hxx>
32 #endif
34 #include <com/sun/star/awt/SystemPointer.hpp>
35 #include <com/sun/star/lang/XComponent.hpp>
37 using namespace ::com::sun::star;
39 namespace avmedia { namespace priv {
41 // ----------------------
42 // - MediaWindowControl -
43 // ----------------------
45 MediaWindowControl::MediaWindowControl( Window* pParent ) :
46 MediaControl( pParent, MEDIACONTROLSTYLE_MULTILINE )
50 // ---------------------------------------------------------------------
52 MediaWindowControl::~MediaWindowControl()
56 // ---------------------------------------------------------------------
58 void MediaWindowControl::update()
60 MediaItem aItem;
62 static_cast< MediaWindowImpl* >( GetParent() )->updateMediaItem( aItem );
63 setState( aItem );
66 // ---------------------------------------------------------------------
68 void MediaWindowControl::execute( const MediaItem& rItem )
70 static_cast< MediaWindowImpl* >( GetParent() )->executeMediaItem( rItem );
73 // --------------------
74 // - MediaChildWindow -
75 // --------------------
77 MediaChildWindow::MediaChildWindow( Window* pParent ) :
78 SystemChildWindow( pParent, WB_CLIPCHILDREN )
82 // ---------------------------------------------------------------------
84 MediaChildWindow::~MediaChildWindow()
88 // ---------------------------------------------------------------------
90 void MediaChildWindow::MouseMove( const MouseEvent& rMEvt )
92 const MouseEvent aTransformedEvent( GetParent()->ScreenToOutputPixel( OutputToScreenPixel( rMEvt.GetPosPixel() ) ),
93 rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(), rMEvt.GetModifier() );
95 SystemChildWindow::MouseMove( rMEvt );
96 GetParent()->MouseMove( aTransformedEvent );
99 // ---------------------------------------------------------------------
101 void MediaChildWindow::MouseButtonDown( const MouseEvent& rMEvt )
103 const MouseEvent aTransformedEvent( GetParent()->ScreenToOutputPixel( OutputToScreenPixel( rMEvt.GetPosPixel() ) ),
104 rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(), rMEvt.GetModifier() );
106 SystemChildWindow::MouseButtonDown( rMEvt );
107 GetParent()->MouseButtonDown( aTransformedEvent );
110 // ---------------------------------------------------------------------
112 void MediaChildWindow::MouseButtonUp( const MouseEvent& rMEvt )
114 const MouseEvent aTransformedEvent( GetParent()->ScreenToOutputPixel( OutputToScreenPixel( rMEvt.GetPosPixel() ) ),
115 rMEvt.GetClicks(), rMEvt.GetMode(), rMEvt.GetButtons(), rMEvt.GetModifier() );
117 SystemChildWindow::MouseButtonUp( rMEvt );
118 GetParent()->MouseButtonUp( aTransformedEvent );
121 // ---------------------------------------------------------------------
123 void MediaChildWindow::KeyInput( const KeyEvent& rKEvt )
125 SystemChildWindow::KeyInput( rKEvt );
126 GetParent()->KeyInput( rKEvt );
129 // ---------------------------------------------------------------------
131 void MediaChildWindow::KeyUp( const KeyEvent& rKEvt )
133 SystemChildWindow::KeyUp( rKEvt );
134 GetParent()->KeyUp( rKEvt );
137 // ---------------------------------------------------------------------
139 void MediaChildWindow::Command( const CommandEvent& rCEvt )
141 const CommandEvent aTransformedEvent( GetParent()->ScreenToOutputPixel( OutputToScreenPixel( rCEvt.GetMousePosPixel() ) ),
142 rCEvt.GetCommand(), rCEvt.IsMouseEvent(), rCEvt.GetData() );
144 SystemChildWindow::Command( rCEvt );
145 GetParent()->Command( aTransformedEvent );
148 // ----------------------
149 // - MediaWindowImpl -
150 // ----------------------
152 MediaWindowImpl::MediaWindowImpl( Window* pParent, MediaWindow* pMediaWindow, bool bInternalMediaControl ) :
153 Control( pParent ),
154 MediaWindowBaseImpl( pMediaWindow ),
155 DropTargetHelper( this ),
156 DragSourceHelper( this ),
157 mxEventsIf( static_cast< ::cppu::OWeakObject* >( mpEvents = new MediaEventListenersImpl( maChildWindow ) ) ),
158 maChildWindow( this ),
159 mpMediaWindowControl( bInternalMediaControl ? new MediaWindowControl( this ) : NULL ),
160 mpEmptyBmpEx( NULL ),
161 mpAudioBmpEx( NULL )
163 maChildWindow.SetBackground( Color( COL_BLACK ) );
164 maChildWindow.SetHelpId( HID_AVMEDIA_PLAYERWINDOW );
165 maChildWindow.Hide();
167 if( mpMediaWindowControl )
169 mpMediaWindowControl->SetSizePixel( mpMediaWindowControl->getMinSizePixel() );
170 mpMediaWindowControl->Show();
174 // ---------------------------------------------------------------------
176 MediaWindowImpl::~MediaWindowImpl()
178 delete mpEmptyBmpEx;
179 delete mpAudioBmpEx;
180 delete mpMediaWindowControl;
183 // ---------------------------------------------------------------------
185 void MediaWindowImpl::cleanUp()
187 uno::Reference< media::XPlayerWindow > xPlayerWindow( getPlayerWindow() );
189 mpEvents->cleanUp();
191 if( xPlayerWindow.is() )
193 xPlayerWindow->removeKeyListener( uno::Reference< awt::XKeyListener >( mxEventsIf, uno::UNO_QUERY ) );
194 xPlayerWindow->removeMouseListener( uno::Reference< awt::XMouseListener >( mxEventsIf, uno::UNO_QUERY ) );
195 xPlayerWindow->removeMouseMotionListener( uno::Reference< awt::XMouseMotionListener >( mxEventsIf, uno::UNO_QUERY ) );
197 uno::Reference< lang::XComponent > xComponent( xPlayerWindow, uno::UNO_QUERY );
198 if( xComponent.is() )
199 xComponent->dispose();
201 setPlayerWindow( NULL );
204 MediaWindowBaseImpl::cleanUp();
207 // ---------------------------------------------------------------------
209 void MediaWindowImpl::onURLChanged()
211 if( getPlayer().is() )
213 uno::Sequence< uno::Any > aArgs( 3 );
214 uno::Reference< media::XPlayerWindow > xPlayerWindow;
215 const Point aPoint;
216 const Size aSize( maChildWindow.GetSizePixel() );
217 const sal_Int32 nWndHandle = 0;
219 aArgs[ 0 ] = uno::makeAny( nWndHandle );
220 aArgs[ 1 ] = uno::makeAny( awt::Rectangle( aPoint.X(), aPoint.Y(), aSize.Width(), aSize.Height() ) );
221 aArgs[ 2 ] = uno::makeAny( reinterpret_cast< sal_IntPtr >( &maChildWindow ) );
225 xPlayerWindow = getPlayer()->createPlayerWindow( aArgs );
227 catch( uno::RuntimeException )
229 // happens eg, on MacOSX where Java frames cannot be created from X11 window handles
232 setPlayerWindow( xPlayerWindow );
234 if( xPlayerWindow.is() )
236 xPlayerWindow->addKeyListener( uno::Reference< awt::XKeyListener >( mxEventsIf, uno::UNO_QUERY ) );
237 xPlayerWindow->addMouseListener( uno::Reference< awt::XMouseListener >( mxEventsIf, uno::UNO_QUERY ) );
238 xPlayerWindow->addMouseMotionListener( uno::Reference< awt::XMouseMotionListener >( mxEventsIf, uno::UNO_QUERY ) );
239 xPlayerWindow->addFocusListener( uno::Reference< awt::XFocusListener >( mxEventsIf, uno::UNO_QUERY ) );
242 else
243 setPlayerWindow( NULL );
245 if( getPlayerWindow().is() )
246 maChildWindow.Show();
247 else
248 maChildWindow.Hide();
250 if( mpMediaWindowControl )
252 MediaItem aItem;
254 updateMediaItem( aItem );
255 mpMediaWindowControl->setState( aItem );
259 // ---------------------------------------------------------------------
261 void MediaWindowImpl::setPosSize( const Rectangle& rRect )
263 SetPosSizePixel( rRect.TopLeft(), rRect.GetSize() );
266 // ---------------------------------------------------------------------
268 void MediaWindowImpl::setPointer( const Pointer& rPointer )
270 uno::Reference< media::XPlayerWindow > xPlayerWindow( getPlayerWindow() );
272 SetPointer( rPointer );
273 maChildWindow.SetPointer( rPointer );
275 if( xPlayerWindow.is() )
277 long nPointer;
279 switch( rPointer.GetStyle() )
281 case( POINTER_CROSS ): nPointer = awt::SystemPointer::CROSS; break;
282 case( POINTER_HAND ): nPointer = awt::SystemPointer::HAND; break;
283 case( POINTER_MOVE ): nPointer = awt::SystemPointer::MOVE; break;
284 case( POINTER_WAIT ): nPointer = awt::SystemPointer::WAIT; break;
286 default: nPointer = awt::SystemPointer::ARROW; break;
289 xPlayerWindow->setPointerType( nPointer );
293 // ---------------------------------------------------------------------
295 void MediaWindowImpl::Resize()
297 uno::Reference< media::XPlayerWindow > xPlayerWindow( getPlayerWindow() );
298 const Size aCurSize( GetOutputSizePixel() );
299 const sal_Int32 nOffset( mpMediaWindowControl ? AVMEDIA_CONTROLOFFSET : 0 );
300 Size aPlayerWindowSize( aCurSize.Width() - ( nOffset << 1 ),
301 aCurSize.Height() - ( nOffset << 1 ) );
303 if( mpMediaWindowControl )
305 const sal_Int32 nControlHeight = mpMediaWindowControl->GetSizePixel().Height();
306 const sal_Int32 nControlY = ::std::max( aCurSize.Height() - nControlHeight - nOffset, 0L );
308 aPlayerWindowSize.Height() = ( nControlY - ( nOffset << 1 ) );
309 mpMediaWindowControl->SetPosSizePixel( Point( nOffset, nControlY ), Size( aCurSize.Width() - ( nOffset << 1 ), nControlHeight ) );
312 if( xPlayerWindow.is() )
313 xPlayerWindow->setPosSize( 0, 0, aPlayerWindowSize.Width(), aPlayerWindowSize.Height(), 0 );
315 maChildWindow.SetPosSizePixel( Point( nOffset, nOffset ), aPlayerWindowSize );
318 // ---------------------------------------------------------------------
320 void MediaWindowImpl::StateChanged( StateChangedType eType )
322 uno::Reference< media::XPlayerWindow > xPlayerWindow( getPlayerWindow() );
324 if( xPlayerWindow.is() )
326 // stop playing when going disabled or hidden
327 switch( eType )
329 case STATE_CHANGE_VISIBLE:
331 stopPlayingInternal( !IsVisible() );
332 xPlayerWindow->setVisible( IsVisible() );
334 break;
336 case STATE_CHANGE_ENABLE:
338 stopPlayingInternal( !IsEnabled() );
339 xPlayerWindow->setEnable( IsEnabled() );
341 break;
343 default:
344 break;
349 // ---------------------------------------------------------------------
351 void MediaWindowImpl::Paint( const Rectangle& )
353 BitmapEx* pLogo = NULL;
355 if( !getPlayer().is() )
357 if( !mpEmptyBmpEx )
358 mpEmptyBmpEx = new BitmapEx( AVMEDIA_RESID( AVMEDIA_BMP_EMPTYLOGO ) );
360 pLogo = mpEmptyBmpEx;
362 else if( !getPlayerWindow().is() )
364 if( !mpAudioBmpEx )
365 mpAudioBmpEx = new BitmapEx( AVMEDIA_RESID( AVMEDIA_BMP_AUDIOLOGO ) );
367 pLogo = mpAudioBmpEx;
370 const Point aBasePos( maChildWindow.GetPosPixel() );
371 const Rectangle aVideoRect( aBasePos, maChildWindow.GetSizePixel() );
373 if( pLogo && !pLogo->IsEmpty() && ( aVideoRect.GetWidth() > 0 ) && ( aVideoRect.GetHeight() > 0 ) )
375 Size aLogoSize( pLogo->GetSizePixel() );
376 const Color aBackgroundColor( 67, 67, 67 );
378 SetLineColor( aBackgroundColor );
379 SetFillColor( aBackgroundColor );
380 DrawRect( aVideoRect );
382 if( ( aLogoSize.Width() > aVideoRect.GetWidth() || aLogoSize.Height() > aVideoRect.GetHeight() ) &&
383 ( aLogoSize.Height() > 0 ) )
385 const double fLogoWH = (double) aLogoSize.Width() / aLogoSize.Height();
387 if( fLogoWH < ( (double) aVideoRect.GetWidth() / aVideoRect.GetHeight() ) )
389 aLogoSize.Width() = (long) ( aVideoRect.GetHeight() * fLogoWH );
390 aLogoSize.Height()= aVideoRect.GetHeight();
392 else
394 aLogoSize.Width() = aVideoRect.GetWidth();
395 aLogoSize.Height()= (long) ( aVideoRect.GetWidth() / fLogoWH );
399 DrawBitmapEx( Point( aBasePos.X() + ( ( aVideoRect.GetWidth() - aLogoSize.Width() ) >> 1 ),
400 aBasePos.Y() + ( ( aVideoRect.GetHeight() - aLogoSize.Height() ) >> 1 ) ),
401 aLogoSize, *pLogo );
405 // ---------------------------------------------------------------------
407 void MediaWindowImpl::GetFocus()
411 // ---------------------------------------------------------------------
413 void MediaWindowImpl::MouseMove( const MouseEvent& rMEvt )
415 MediaWindow* pMediaWindow = getMediaWindow();
417 if( pMediaWindow )
418 pMediaWindow->MouseMove( rMEvt );
421 // ---------------------------------------------------------------------
423 void MediaWindowImpl::MouseButtonDown( const MouseEvent& rMEvt )
425 MediaWindow* pMediaWindow = getMediaWindow();
427 if( pMediaWindow )
428 pMediaWindow->MouseButtonDown( rMEvt );
431 // ---------------------------------------------------------------------
433 void MediaWindowImpl::MouseButtonUp( const MouseEvent& rMEvt )
435 MediaWindow* pMediaWindow = getMediaWindow();
437 if( pMediaWindow )
438 pMediaWindow->MouseButtonUp( rMEvt );
441 // ---------------------------------------------------------------------
443 void MediaWindowImpl::KeyInput( const KeyEvent& rKEvt )
445 MediaWindow* pMediaWindow = getMediaWindow();
447 if( pMediaWindow )
448 pMediaWindow->KeyInput( rKEvt );
451 // ---------------------------------------------------------------------
453 void MediaWindowImpl::KeyUp( const KeyEvent& rKEvt )
455 MediaWindow* pMediaWindow = getMediaWindow();
457 if( pMediaWindow )
458 pMediaWindow->KeyUp( rKEvt );
461 // ---------------------------------------------------------------------
463 void MediaWindowImpl::Command( const CommandEvent& rCEvt )
465 MediaWindow* pMediaWindow = getMediaWindow();
467 if( pMediaWindow )
468 pMediaWindow->Command( rCEvt );
471 // ---------------------------------------------------------------------
473 sal_Int8 MediaWindowImpl::AcceptDrop( const AcceptDropEvent& rEvt )
475 MediaWindow* pMediaWindow = getMediaWindow();
476 return( pMediaWindow ? pMediaWindow->AcceptDrop( rEvt ) : 0 );
479 // ---------------------------------------------------------------------
481 sal_Int8 MediaWindowImpl::ExecuteDrop( const ExecuteDropEvent& rEvt )
483 MediaWindow* pMediaWindow = getMediaWindow();
484 return( pMediaWindow ? pMediaWindow->ExecuteDrop( rEvt ) : 0 );
487 // ---------------------------------------------------------------------
489 void MediaWindowImpl::StartDrag( sal_Int8 nAction, const Point& rPosPixel )
491 MediaWindow* pMediaWindow = getMediaWindow();
493 if( pMediaWindow )
494 pMediaWindow->StartDrag( nAction, rPosPixel );
497 } // namespace priv
498 } // namespace avmedia
500 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */