Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / ui / slideshow / slideshowviewimpl.cxx
blobbc1828521f427a034119b1251d49d0ee4f79f933
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 "slideshowviewimpl.hxx"
21 #include "slideshowimpl.hxx"
22 #include <osl/mutex.hxx>
24 #include <com/sun/star/awt/Pointer.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <basegfx/polygon/b2dpolygon.hxx>
28 #include <basegfx/polygon/b2dpolygontools.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
31 #include <cppcanvas/vclfactory.hxx>
32 #include <cppcanvas/basegfxfactory.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::WeakReference;
38 using ::com::sun::star::uno::RuntimeException;
39 using ::com::sun::star::uno::Exception;
40 using ::com::sun::star::presentation::XSlideShowView;
42 using namespace ::com::sun::star;
44 namespace sd
47 // SlideShowViewListeners
48 SlideShowViewListeners::SlideShowViewListeners( ::osl::Mutex& rMutex )
49 : mrMutex( rMutex )
53 void SlideShowViewListeners::addListener( const Reference< util::XModifyListener >& _rxListener )
55 ::osl::MutexGuard aGuard( mrMutex );
57 WeakReference< util::XModifyListener > xWeak( _rxListener );
58 if( std::find( maListeners.begin(), maListeners.end(), xWeak ) == maListeners.end() )
59 maListeners.push_back( xWeak );
62 void SlideShowViewListeners::removeListener( const Reference< util::XModifyListener >& _rxListener )
64 ::osl::MutexGuard aGuard( mrMutex );
66 WeakReference< util::XModifyListener > xWeak( _rxListener );
67 ViewListenerVector::iterator aIter( std::find( maListeners.begin(), maListeners.end(), xWeak ) );
68 if( aIter != maListeners.end() )
69 maListeners.erase( aIter );
72 void SlideShowViewListeners::notify( const lang::EventObject& _rEvent )
74 ::osl::MutexGuard aGuard( mrMutex );
76 ViewListenerVector::iterator aIter( maListeners.begin() );
77 while( aIter != maListeners.end() )
79 Reference< util::XModifyListener > xListener( (*aIter) );
80 if( xListener.is() )
82 xListener->modified( _rEvent );
83 ++aIter;
85 else
87 aIter = maListeners.erase( aIter );
92 void SlideShowViewListeners::disposing( const lang::EventObject& _rEventSource )
94 ::osl::MutexGuard aGuard( mrMutex );
96 ViewListenerVector::iterator aIter( maListeners.begin() );
97 while( aIter != maListeners.end() )
99 Reference< util::XModifyListener > xListener( (*aIter++) );
100 if( xListener.is() )
101 xListener->disposing( _rEventSource );
104 maListeners.clear();
107 // SlideShowViewPaintListeners
108 SlideShowViewPaintListeners::SlideShowViewPaintListeners( ::osl::Mutex& rMutex )
109 : SlideShowViewPaintListeners_Base( rMutex )
113 bool SlideShowViewPaintListeners::implTypedNotify( const Reference< awt::XPaintListener >& rListener,
114 const awt::PaintEvent& rEvent )
116 rListener->windowPaint( rEvent );
117 return true; // continue calling listeners
120 // SlideShowViewMouseListeners
121 SlideShowViewMouseListeners::SlideShowViewMouseListeners( ::osl::Mutex& rMutex ) :
122 SlideShowViewMouseListeners_Base( rMutex )
126 bool SlideShowViewMouseListeners::implTypedNotify( const Reference< awt::XMouseListener >& rListener,
127 const WrappedMouseEvent& rEvent )
129 switch( rEvent.meType )
131 case WrappedMouseEvent::PRESSED:
132 rListener->mousePressed( rEvent.maEvent );
133 break;
135 case WrappedMouseEvent::RELEASED:
136 rListener->mouseReleased( rEvent.maEvent );
137 break;
139 case WrappedMouseEvent::ENTERED:
140 rListener->mouseEntered( rEvent.maEvent );
141 break;
143 case WrappedMouseEvent::EXITED:
144 rListener->mouseExited( rEvent.maEvent );
145 break;
148 return true; // continue calling listeners
151 // SlideShowViewMouseMotionListeners
152 SlideShowViewMouseMotionListeners::SlideShowViewMouseMotionListeners( ::osl::Mutex& rMutex ) :
153 SlideShowViewMouseMotionListeners_Base( rMutex )
157 bool SlideShowViewMouseMotionListeners::implTypedNotify( const Reference< awt::XMouseMotionListener >& rListener,
158 const WrappedMouseMotionEvent& rEvent )
160 switch( rEvent.meType )
162 case WrappedMouseMotionEvent::DRAGGED:
163 rListener->mouseDragged( rEvent.maEvent );
164 break;
166 case WrappedMouseMotionEvent::MOVED:
167 rListener->mouseMoved( rEvent.maEvent );
168 break;
171 return true; // continue calling listeners
174 // SlideShowView
175 SlideShowView::SlideShowView( ShowWindow& rOutputWindow,
176 SdDrawDocument* pDoc,
177 AnimationMode eAnimationMode,
178 SlideshowImpl* pSlideShow,
179 bool bFullScreen )
180 : SlideShowView_Base( m_aMutex ),
181 mpCanvas( ::cppcanvas::VCLFactory::createSpriteCanvas( rOutputWindow ) ),
182 mxWindow( VCLUnoHelper::GetInterface( &rOutputWindow ), uno::UNO_QUERY_THROW ),
183 mxWindowPeer( mxWindow, uno::UNO_QUERY_THROW ),
184 mxPointer(),
185 mpSlideShow( pSlideShow ),
186 mrOutputWindow( rOutputWindow ),
187 mpViewListeners( new SlideShowViewListeners( m_aMutex ) ),
188 mpPaintListeners( new SlideShowViewPaintListeners( m_aMutex ) ),
189 mpMouseListeners( new SlideShowViewMouseListeners( m_aMutex ) ),
190 mpMouseMotionListeners( new SlideShowViewMouseMotionListeners( m_aMutex ) ),
191 mpDoc( pDoc ),
192 mbIsMouseMotionListener( false ),
193 meAnimationMode( eAnimationMode ),
194 mbFirstPaint( true ),
195 mbFullScreen( bFullScreen ),
196 mbMousePressedEaten( false )
198 init();
200 mTranslationOffset.Width = 0;
201 mTranslationOffset.Height = 0;
204 // Dispose all internal references
205 void SAL_CALL SlideShowView::dispose()
207 ::osl::MutexGuard aGuard( m_aMutex );
209 mpSlideShow = nullptr;
211 // deregister listeners
212 if( mxWindow.is() )
214 mxWindow->removeWindowListener( this );
215 mxWindow->removeMouseListener( this );
217 if( mbIsMouseMotionListener )
218 mxWindow->removeMouseMotionListener( this );
221 mpCanvas.reset();
222 mxWindow.clear();
224 // clear all listener containers
225 disposing( lang::EventObject() );
227 // call base
228 WeakComponentImplHelperBase::dispose();
231 // Disposing our broadcaster
232 void SAL_CALL SlideShowView::disposing( const lang::EventObject& )
234 ::osl::MutexGuard aGuard( m_aMutex );
236 // notify all listeners that _we_ are going down (send a disposing()),
237 // then delete listener containers:
238 lang::EventObject const evt( static_cast<OWeakObject *>(this) );
239 if (mpViewListeners.get() != nullptr) {
240 mpViewListeners->disposing( evt );
241 mpViewListeners.reset();
243 if (mpPaintListeners.get() != nullptr) {
244 mpPaintListeners->disposing( evt );
245 mpPaintListeners.reset();
247 if (mpMouseListeners.get() != nullptr) {
248 mpMouseListeners->disposing( evt );
249 mpMouseListeners.reset();
251 if (mpMouseMotionListeners.get() != nullptr) {
252 mpMouseMotionListeners->disposing( evt );
253 mpMouseMotionListeners.reset();
257 void SlideShowView::paint( const awt::PaintEvent& e )
259 ::osl::ClearableMutexGuard aGuard( m_aMutex );
261 if( mbFirstPaint )
263 mbFirstPaint = false;
264 SlideshowImpl* pSlideShow = mpSlideShow;
265 aGuard.clear();
266 if( pSlideShow )
267 pSlideShow->onFirstPaint();
269 else
271 // Change event source, to enable listeners to match event
272 // with view
273 awt::PaintEvent aEvent( e );
274 aEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
275 mpPaintListeners->notify( aEvent );
276 updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
280 // XSlideShowView methods
281 Reference< rendering::XSpriteCanvas > SAL_CALL SlideShowView::getCanvas( )
283 ::osl::MutexGuard aGuard( m_aMutex );
285 return mpCanvas.get() ? mpCanvas->getUNOSpriteCanvas() : Reference< rendering::XSpriteCanvas >();
288 void SAL_CALL SlideShowView::clear()
290 // paint background in black
291 ::osl::MutexGuard aGuard( m_aMutex );
292 SolarMutexGuard aSolarGuard;
294 // fill the bounds rectangle in black
296 const Size aWindowSize( mrOutputWindow.GetSizePixel() );
298 ::basegfx::B2DPolygon aPoly( ::basegfx::utils::createPolygonFromRect(
299 ::basegfx::B2DRectangle(0.0,0.0,
300 aWindowSize.Width(),
301 aWindowSize.Height() ) ) );
302 ::cppcanvas::PolyPolygonSharedPtr pPolyPoly(
303 ::cppcanvas::BaseGfxFactory::createPolyPolygon( mpCanvas, aPoly ) );
305 if( pPolyPoly.get() )
307 pPolyPoly->setRGBAFillColor( 0x000000FFU );
308 pPolyPoly->draw();
312 geometry::IntegerSize2D SAL_CALL SlideShowView::getTranslationOffset( )
314 return mTranslationOffset;
317 geometry::AffineMatrix2D SAL_CALL SlideShowView::getTransformation( )
319 ::osl::MutexGuard aGuard( m_aMutex );
320 SolarMutexGuard aSolarGuard;
322 const Size& rTmpSize( mrOutputWindow.GetSizePixel() );
324 if (rTmpSize.Width()<=0 || rTmpSize.Height()<=0)
326 return geometry::AffineMatrix2D (1,0,0,0,1,0);
329 const Size aWindowSize( mrOutputWindow.GetSizePixel() );
330 Size aOutputSize( aWindowSize );
332 if( meAnimationMode != ANIMATIONMODE_SHOW )
334 aOutputSize.setWidth( static_cast<long>( aOutputSize.Width() / 1.03 ) );
335 aOutputSize.setHeight( static_cast<long>( aOutputSize.Height() / 1.03 ) );
338 SdPage* pP = mpDoc->GetSdPage( 0, PageKind::Standard );
339 Size aPageSize( pP->GetSize() );
341 const double page_ratio = static_cast<double>(aPageSize.Width()) / static_cast<double>(aPageSize.Height());
342 const double output_ratio = static_cast<double>(aOutputSize.Width()) / static_cast<double>(aOutputSize.Height());
344 if( page_ratio > output_ratio )
346 aOutputSize.setHeight( ( aOutputSize.Width() * aPageSize.Height() ) / aPageSize.Width() );
348 else if( page_ratio < output_ratio )
350 aOutputSize.setWidth( ( aOutputSize.Height() * aPageSize.Width() ) / aPageSize.Height() );
353 Point aOutputOffset( ( aWindowSize.Width() - aOutputSize.Width() ) >> 1,
354 ( aWindowSize.Height() - aOutputSize.Height() ) >> 1 );
356 // Reduce available width by one, as the slides might actually
357 // render one pixel wider and higher as aPageSize below specifies
358 // (when shapes of page size have visible border lines)
359 aOutputSize.AdjustWidth( -1 );
360 aOutputSize.AdjustHeight( -1 );
362 // Record mTranslationOffset
363 mTranslationOffset.Height = aOutputOffset.Y();
364 mTranslationOffset.Width = aOutputOffset.X();
366 maPresentationArea = ::tools::Rectangle( aOutputOffset, aOutputSize );
367 mrOutputWindow.SetPresentationArea( maPresentationArea );
369 // scale presentation into available window rect (minus 10%); center in the window
370 const basegfx::B2DHomMatrix aMatrix(basegfx::utils::createScaleTranslateB2DHomMatrix(
371 aOutputSize.Width(), aOutputSize.Height(), aOutputOffset.X(), aOutputOffset.Y()));
373 geometry::AffineMatrix2D aRes;
375 return ::basegfx::unotools::affineMatrixFromHomMatrix( aRes, aMatrix );
378 void SAL_CALL SlideShowView::addTransformationChangedListener( const Reference< util::XModifyListener >& xListener )
380 ::osl::MutexGuard aGuard( m_aMutex );
382 if( mpViewListeners.get() )
383 mpViewListeners->addListener( xListener );
386 void SAL_CALL SlideShowView::removeTransformationChangedListener( const Reference< util::XModifyListener >& xListener )
388 ::osl::MutexGuard aGuard( m_aMutex );
390 if( mpViewListeners.get() )
391 mpViewListeners->removeListener( xListener );
394 void SAL_CALL SlideShowView::addPaintListener( const Reference< awt::XPaintListener >& xListener )
396 ::osl::MutexGuard aGuard( m_aMutex );
398 if( mpPaintListeners.get() )
399 mpPaintListeners->addTypedListener( xListener );
402 void SAL_CALL SlideShowView::removePaintListener( const Reference< awt::XPaintListener >& xListener )
404 ::osl::MutexGuard aGuard( m_aMutex );
406 if( mpPaintListeners.get() )
407 mpPaintListeners->removeTypedListener( xListener );
410 void SAL_CALL SlideShowView::addMouseListener( const Reference< awt::XMouseListener >& xListener )
412 ::osl::MutexGuard aGuard( m_aMutex );
414 if( mpMouseListeners.get() )
415 mpMouseListeners->addTypedListener( xListener );
418 void SAL_CALL SlideShowView::removeMouseListener( const Reference< awt::XMouseListener >& xListener )
420 ::osl::MutexGuard aGuard( m_aMutex );
422 if( mpMouseListeners.get() )
423 mpMouseListeners->removeTypedListener( xListener );
426 void SAL_CALL SlideShowView::addMouseMotionListener( const Reference< awt::XMouseMotionListener >& xListener )
428 ::osl::MutexGuard aGuard( m_aMutex );
430 if( !mbIsMouseMotionListener && mxWindow.is() )
432 // delay motion event registration, until we really
433 // need it
434 mbIsMouseMotionListener = true;
435 mxWindow->addMouseMotionListener( this );
438 if( mpMouseMotionListeners.get() )
439 mpMouseMotionListeners->addTypedListener( xListener );
442 void SAL_CALL SlideShowView::removeMouseMotionListener( const Reference< awt::XMouseMotionListener >& xListener )
444 ::osl::MutexGuard aGuard( m_aMutex );
446 if( mpMouseMotionListeners.get() )
447 mpMouseMotionListeners->removeTypedListener( xListener );
449 // TODO(P1): Might be nice to deregister for mouse motion
450 // events, when the last listener is gone.
453 void SAL_CALL SlideShowView::setMouseCursor( sal_Int16 nPointerShape )
455 ::osl::MutexGuard aGuard( m_aMutex );
457 // forward to window
458 if( mxPointer.is() )
459 mxPointer->setType( nPointerShape );
461 if( mxWindowPeer.is() )
462 mxWindowPeer->setPointer( mxPointer );
465 awt::Rectangle SAL_CALL SlideShowView::getCanvasArea( )
467 awt::Rectangle aRectangle;
469 if( mxWindow.is() )
470 return mxWindow->getPosSize();
472 aRectangle.X = aRectangle.Y = aRectangle.Width = aRectangle.Height = 0;
474 return aRectangle;
477 void SlideShowView::updateimpl( ::osl::ClearableMutexGuard& rGuard, SlideshowImpl* pSlideShow )
479 if( pSlideShow )
481 ::rtl::Reference< SlideshowImpl > aSLGuard( pSlideShow );
483 if( mbFirstPaint )
485 mbFirstPaint = false;
486 SlideshowImpl* pTmpSlideShow = mpSlideShow;
487 rGuard.clear();
488 if( pTmpSlideShow )
489 pTmpSlideShow->onFirstPaint();
490 } else
491 rGuard.clear();
493 pSlideShow->startUpdateTimer();
497 // XWindowListener methods
498 void SAL_CALL SlideShowView::windowResized( const awt::WindowEvent& e )
500 ::osl::ClearableMutexGuard aGuard( m_aMutex );
502 if( mpViewListeners.get() )
504 // Change event source, to enable listeners to match event
505 // with view
506 awt::WindowEvent aEvent( e );
507 aEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
509 mpViewListeners->notify( aEvent );
510 updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
514 void SAL_CALL SlideShowView::windowMoved( const awt::WindowEvent& )
516 // ignored
519 void SAL_CALL SlideShowView::windowShown( const lang::EventObject& )
521 // ignored
524 void SAL_CALL SlideShowView::windowHidden( const lang::EventObject& )
526 // ignored
529 // XMouseListener implementation
530 void SAL_CALL SlideShowView::mousePressed( const awt::MouseEvent& e )
532 ::osl::ClearableMutexGuard aGuard( m_aMutex );
533 if( mpSlideShow && mpSlideShow->isInputFreezed() )
535 mbMousePressedEaten = true;
537 else
539 mbMousePressedEaten = false;
541 // Change event source, to enable listeners to match event
542 // with view
543 WrappedMouseEvent aEvent;
544 aEvent.meType = WrappedMouseEvent::PRESSED;
545 aEvent.maEvent = e;
546 aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
548 if( mpMouseListeners.get() )
549 mpMouseListeners->notify( aEvent );
550 updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
554 void SAL_CALL SlideShowView::mouseReleased( const awt::MouseEvent& e )
556 ::osl::ClearableMutexGuard aGuard( m_aMutex );
557 if( mbMousePressedEaten )
559 // if mouse button down was ignored, also ignore mouse button up
560 mbMousePressedEaten = false;
562 else if( mpSlideShow && !mpSlideShow->isInputFreezed() )
564 // Change event source, to enable listeners to match event
565 // with view
566 WrappedMouseEvent aEvent;
567 aEvent.meType = WrappedMouseEvent::RELEASED;
568 aEvent.maEvent = e;
569 aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
571 if( mpMouseListeners.get() )
572 mpMouseListeners->notify( aEvent );
573 updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
577 void SAL_CALL SlideShowView::mouseEntered( const awt::MouseEvent& e )
579 ::osl::ClearableMutexGuard aGuard( m_aMutex );
581 // Change event source, to enable listeners to match event
582 // with view
583 WrappedMouseEvent aEvent;
584 aEvent.meType = WrappedMouseEvent::ENTERED;
585 aEvent.maEvent = e;
586 aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
588 if( mpMouseListeners.get() )
589 mpMouseListeners->notify( aEvent );
590 updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
593 void SAL_CALL SlideShowView::mouseExited( const awt::MouseEvent& e )
595 ::osl::ClearableMutexGuard aGuard( m_aMutex );
597 // Change event source, to enable listeners to match event
598 // with view
599 WrappedMouseEvent aEvent;
600 aEvent.meType = WrappedMouseEvent::EXITED;
601 aEvent.maEvent = e;
602 aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
604 if( mpMouseListeners.get() )
605 mpMouseListeners->notify( aEvent );
606 updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
609 // XMouseMotionListener implementation
610 void SAL_CALL SlideShowView::mouseDragged( const awt::MouseEvent& e )
612 ::osl::ClearableMutexGuard aGuard( m_aMutex );
614 // Change event source, to enable listeners to match event
615 // with view
616 WrappedMouseMotionEvent aEvent;
617 aEvent.meType = WrappedMouseMotionEvent::DRAGGED;
618 aEvent.maEvent = e;
619 aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
621 if( mpMouseMotionListeners.get() )
622 mpMouseMotionListeners->notify( aEvent );
623 updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
626 void SAL_CALL SlideShowView::mouseMoved( const awt::MouseEvent& e )
628 ::osl::ClearableMutexGuard aGuard( m_aMutex );
630 // Change event source, to enable listeners to match event
631 // with view
632 WrappedMouseMotionEvent aEvent;
633 aEvent.meType = WrappedMouseMotionEvent::MOVED;
634 aEvent.maEvent = e;
635 aEvent.maEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
637 if( mpMouseMotionListeners.get() )
638 mpMouseMotionListeners->notify( aEvent );
639 updateimpl( aGuard, mpSlideShow ); // warning: clears guard!
642 void SlideShowView::init()
644 mxWindow->addWindowListener( this );
645 mxWindow->addMouseListener( this );
647 mxPointer = awt::Pointer::create( ::comphelper::getProcessComponentContext() );
649 getTransformation();
651 // #i48939# only switch on kind of hacky scroll optimization, when
652 // running fullscreen. this minimizes the probability that other
653 // windows partially cover the show.
654 if( mbFullScreen )
658 Reference< beans::XPropertySet > xCanvasProps( getCanvas(),
659 uno::UNO_QUERY_THROW );
660 xCanvasProps->setPropertyValue("UnsafeScrolling",
661 uno::makeAny( true ) );
663 catch( uno::Exception& )
669 } // namespace ::sd
671 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */