1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
24 #include <osl/mutex.hxx>
25 #include <vcl/svapp.hxx>
27 #include <com/sun/star/awt/Pointer.hpp>
28 #include <com/sun/star/awt/XWindow.hpp>
29 #include <com/sun/star/awt/XWindowPeer.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <basegfx/polygon/b2dpolygon.hxx>
33 #include <basegfx/polygon/b2dpolygontools.hxx>
34 #include <basegfx/matrix/b2dhommatrixtools.hxx>
35 #include <basegfx/matrix/b2dhommatrix.hxx>
37 #include <cppcanvas/vclfactory.hxx>
38 #include <cppcanvas/basegfxfactory.hxx>
39 #include <basegfx/utils/canvastools.hxx>
41 #include <toolkit/helper/vclunohelper.hxx>
42 #include <comphelper/processfactory.hxx>
44 using ::com::sun::star::uno::Reference
;
45 using ::com::sun::star::uno::WeakReference
;
46 using ::com::sun::star::uno::RuntimeException
;
47 using ::com::sun::star::uno::Exception
;
48 using ::com::sun::star::presentation::XSlideShowView
;
50 using namespace ::com::sun::star
;
55 // SlideShowViewListeners
56 SlideShowViewListeners::SlideShowViewListeners( ::osl::Mutex
& rMutex
)
61 void SlideShowViewListeners::addListener( const Reference
< util::XModifyListener
>& _rxListener
)
63 ::osl::MutexGuard
aGuard( mrMutex
);
65 WeakReference
< util::XModifyListener
> xWeak( _rxListener
);
66 if( std::find( maListeners
.begin(), maListeners
.end(), xWeak
) == maListeners
.end() )
67 maListeners
.push_back( xWeak
);
70 void SlideShowViewListeners::removeListener( const Reference
< util::XModifyListener
>& _rxListener
)
72 ::osl::MutexGuard
aGuard( mrMutex
);
74 WeakReference
< util::XModifyListener
> xWeak( _rxListener
);
75 ViewListenerVector::iterator
aIter( std::find( maListeners
.begin(), maListeners
.end(), xWeak
) );
76 if( aIter
!= maListeners
.end() )
77 maListeners
.erase( aIter
);
80 void SlideShowViewListeners::notify( const lang::EventObject
& _rEvent
)
82 ::osl::MutexGuard
aGuard( mrMutex
);
84 ViewListenerVector::iterator
aIter( maListeners
.begin() );
85 while( aIter
!= maListeners
.end() )
87 Reference
< util::XModifyListener
> xListener( *aIter
);
90 xListener
->modified( _rEvent
);
95 aIter
= maListeners
.erase( aIter
);
100 void SlideShowViewListeners::disposing( const lang::EventObject
& _rEventSource
)
102 ::osl::MutexGuard
aGuard( mrMutex
);
104 for( const auto& rxListener
: maListeners
)
106 Reference
< util::XModifyListener
> xListener( rxListener
);
108 xListener
->disposing( _rEventSource
);
114 // SlideShowViewPaintListeners
115 SlideShowViewPaintListeners::SlideShowViewPaintListeners( ::osl::Mutex
& rMutex
)
116 : SlideShowViewPaintListeners_Base( rMutex
)
120 bool SlideShowViewPaintListeners::implTypedNotify( const Reference
< awt::XPaintListener
>& rListener
,
121 const awt::PaintEvent
& rEvent
)
123 rListener
->windowPaint( rEvent
);
124 return true; // continue calling listeners
127 // SlideShowViewMouseListeners
128 SlideShowViewMouseListeners::SlideShowViewMouseListeners( ::osl::Mutex
& rMutex
) :
129 SlideShowViewMouseListeners_Base( rMutex
)
133 bool SlideShowViewMouseListeners::implTypedNotify( const Reference
< awt::XMouseListener
>& rListener
,
134 const WrappedMouseEvent
& rEvent
)
136 switch( rEvent
.meType
)
138 case WrappedMouseEvent::PRESSED
:
139 rListener
->mousePressed( rEvent
.maEvent
);
142 case WrappedMouseEvent::RELEASED
:
143 rListener
->mouseReleased( rEvent
.maEvent
);
146 case WrappedMouseEvent::ENTERED
:
147 rListener
->mouseEntered( rEvent
.maEvent
);
150 case WrappedMouseEvent::EXITED
:
151 rListener
->mouseExited( rEvent
.maEvent
);
155 return true; // continue calling listeners
158 // SlideShowViewMouseMotionListeners
159 SlideShowViewMouseMotionListeners::SlideShowViewMouseMotionListeners( ::osl::Mutex
& rMutex
) :
160 SlideShowViewMouseMotionListeners_Base( rMutex
)
164 bool SlideShowViewMouseMotionListeners::implTypedNotify( const Reference
< awt::XMouseMotionListener
>& rListener
,
165 const WrappedMouseMotionEvent
& rEvent
)
167 switch( rEvent
.meType
)
169 case WrappedMouseMotionEvent::DRAGGED
:
170 rListener
->mouseDragged( rEvent
.maEvent
);
173 case WrappedMouseMotionEvent::MOVED
:
174 rListener
->mouseMoved( rEvent
.maEvent
);
178 return true; // continue calling listeners
182 SlideShowView::SlideShowView( ShowWindow
& rOutputWindow
,
183 SdDrawDocument
* pDoc
,
184 AnimationMode eAnimationMode
,
185 SlideshowImpl
* pSlideShow
,
187 : SlideShowView_Base( m_aMutex
),
188 mpCanvas( ::cppcanvas::VCLFactory::createSpriteCanvas( rOutputWindow
) ),
189 mxWindow( VCLUnoHelper::GetInterface( &rOutputWindow
), uno::UNO_SET_THROW
),
190 mxWindowPeer( mxWindow
, uno::UNO_QUERY_THROW
),
192 mpSlideShow( pSlideShow
),
193 mrOutputWindow( rOutputWindow
),
194 mpViewListeners( new SlideShowViewListeners( m_aMutex
) ),
195 mpPaintListeners( new SlideShowViewPaintListeners( m_aMutex
) ),
196 mpMouseListeners( new SlideShowViewMouseListeners( m_aMutex
) ),
197 mpMouseMotionListeners( new SlideShowViewMouseMotionListeners( m_aMutex
) ),
199 mbIsMouseMotionListener( false ),
200 meAnimationMode( eAnimationMode
),
201 mbFirstPaint( true ),
202 mbFullScreen( bFullScreen
),
203 mbMousePressedEaten( false )
207 mTranslationOffset
.Width
= 0;
208 mTranslationOffset
.Height
= 0;
211 // Dispose all internal references
212 void SAL_CALL
SlideShowView::dispose()
214 ::osl::MutexGuard
aGuard( m_aMutex
);
216 mpSlideShow
= nullptr;
218 // deregister listeners
221 mxWindow
->removeWindowListener( this );
222 mxWindow
->removeMouseListener( this );
224 if( mbIsMouseMotionListener
)
225 mxWindow
->removeMouseMotionListener( this );
231 // clear all listener containers
232 disposing( lang::EventObject() );
235 WeakComponentImplHelperBase::dispose();
238 // Disposing our broadcaster
239 void SAL_CALL
SlideShowView::disposing( const lang::EventObject
& )
241 ::osl::MutexGuard
aGuard( m_aMutex
);
243 // notify all listeners that _we_ are going down (send a disposing()),
244 // then delete listener containers:
245 lang::EventObject
const evt( static_cast<OWeakObject
*>(this) );
246 if (mpViewListeners
!= nullptr)
248 mpViewListeners
->disposing( evt
);
249 mpViewListeners
.reset();
251 if (mpPaintListeners
!= nullptr)
253 mpPaintListeners
->disposing( evt
);
254 mpPaintListeners
.reset();
256 if (mpMouseListeners
!= nullptr)
258 mpMouseListeners
->disposing( evt
);
259 mpMouseListeners
.reset();
261 if (mpMouseMotionListeners
!= nullptr)
263 mpMouseMotionListeners
->disposing( evt
);
264 mpMouseMotionListeners
.reset();
268 void SlideShowView::paint( const awt::PaintEvent
& e
)
270 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
274 mbFirstPaint
= false;
275 SlideshowImpl
* pSlideShow
= mpSlideShow
;
278 pSlideShow
->onFirstPaint();
282 // Change event source, to enable listeners to match event
284 awt::PaintEvent
aEvent( e
);
285 aEvent
.Source
= static_cast< ::cppu::OWeakObject
* >( this );
286 mpPaintListeners
->notify( aEvent
);
287 updateimpl( aGuard
, mpSlideShow
); // warning: clears guard!
291 // XSlideShowView methods
292 Reference
< rendering::XSpriteCanvas
> SAL_CALL
SlideShowView::getCanvas( )
294 ::osl::MutexGuard
aGuard( m_aMutex
);
296 return mpCanvas
.get() ? mpCanvas
->getUNOSpriteCanvas() : Reference
< rendering::XSpriteCanvas
>();
299 void SAL_CALL
SlideShowView::clear()
301 // paint background in black
302 ::osl::MutexGuard
aGuard( m_aMutex
);
303 SolarMutexGuard aSolarGuard
;
305 // fill the bounds rectangle in black
307 const Size
aWindowSize( mrOutputWindow
.GetSizePixel() );
309 ::basegfx::B2DPolygon
aPoly( ::basegfx::utils::createPolygonFromRect(
310 ::basegfx::B2DRectangle(0.0,0.0,
312 aWindowSize
.Height() ) ) );
313 ::cppcanvas::PolyPolygonSharedPtr
pPolyPoly(
314 ::cppcanvas::BaseGfxFactory::createPolyPolygon( mpCanvas
, aPoly
) );
316 if( pPolyPoly
.get() )
318 pPolyPoly
->setRGBAFillColor( 0x000000FFU
);
323 geometry::IntegerSize2D SAL_CALL
SlideShowView::getTranslationOffset( )
325 return mTranslationOffset
;
328 geometry::AffineMatrix2D SAL_CALL
SlideShowView::getTransformation( )
330 ::osl::MutexGuard
aGuard( m_aMutex
);
331 SolarMutexGuard aSolarGuard
;
333 const Size
& rTmpSize( mrOutputWindow
.GetSizePixel() );
335 if (rTmpSize
.Width()<=0 || rTmpSize
.Height()<=0)
337 return geometry::AffineMatrix2D (1,0,0,0,1,0);
340 const Size
aWindowSize( mrOutputWindow
.GetSizePixel() );
341 Size
aOutputSize( aWindowSize
);
343 if( meAnimationMode
!= ANIMATIONMODE_SHOW
)
345 aOutputSize
.setWidth( static_cast<long>( aOutputSize
.Width() / 1.03 ) );
346 aOutputSize
.setHeight( static_cast<long>( aOutputSize
.Height() / 1.03 ) );
349 SdPage
* pP
= mpDoc
->GetSdPage( 0, PageKind::Standard
);
350 Size
aPageSize( pP
->GetSize() );
352 const double page_ratio
= static_cast<double>(aPageSize
.Width()) / static_cast<double>(aPageSize
.Height());
353 const double output_ratio
= static_cast<double>(aOutputSize
.Width()) / static_cast<double>(aOutputSize
.Height());
355 if( page_ratio
> output_ratio
)
357 aOutputSize
.setHeight( ( aOutputSize
.Width() * aPageSize
.Height() ) / aPageSize
.Width() );
359 else if( page_ratio
< output_ratio
)
361 aOutputSize
.setWidth( ( aOutputSize
.Height() * aPageSize
.Width() ) / aPageSize
.Height() );
364 Point
aOutputOffset( ( aWindowSize
.Width() - aOutputSize
.Width() ) >> 1,
365 ( aWindowSize
.Height() - aOutputSize
.Height() ) >> 1 );
367 // Reduce available width by one, as the slides might actually
368 // render one pixel wider and higher as aPageSize below specifies
369 // (when shapes of page size have visible border lines)
370 aOutputSize
.AdjustWidth( -1 );
371 aOutputSize
.AdjustHeight( -1 );
373 // Record mTranslationOffset
374 mTranslationOffset
.Height
= aOutputOffset
.Y();
375 mTranslationOffset
.Width
= aOutputOffset
.X();
377 // scale presentation into available window rect (minus 10%); center in the window
378 const basegfx::B2DHomMatrix
aMatrix(basegfx::utils::createScaleTranslateB2DHomMatrix(
379 aOutputSize
.Width(), aOutputSize
.Height(), aOutputOffset
.X(), aOutputOffset
.Y()));
381 geometry::AffineMatrix2D aRes
;
383 return ::basegfx::unotools::affineMatrixFromHomMatrix( aRes
, aMatrix
);
386 void SAL_CALL
SlideShowView::addTransformationChangedListener( const Reference
< util::XModifyListener
>& xListener
)
388 ::osl::MutexGuard
aGuard( m_aMutex
);
391 mpViewListeners
->addListener( xListener
);
394 void SAL_CALL
SlideShowView::removeTransformationChangedListener( const Reference
< util::XModifyListener
>& xListener
)
396 ::osl::MutexGuard
aGuard( m_aMutex
);
399 mpViewListeners
->removeListener( xListener
);
402 void SAL_CALL
SlideShowView::addPaintListener( const Reference
< awt::XPaintListener
>& xListener
)
404 ::osl::MutexGuard
aGuard( m_aMutex
);
406 if (mpPaintListeners
)
407 mpPaintListeners
->addTypedListener( xListener
);
410 void SAL_CALL
SlideShowView::removePaintListener( const Reference
< awt::XPaintListener
>& xListener
)
412 ::osl::MutexGuard
aGuard( m_aMutex
);
414 if (mpPaintListeners
)
415 mpPaintListeners
->removeTypedListener( xListener
);
418 void SAL_CALL
SlideShowView::addMouseListener( const Reference
< awt::XMouseListener
>& xListener
)
420 ::osl::MutexGuard
aGuard( m_aMutex
);
422 if (mpMouseListeners
)
423 mpMouseListeners
->addTypedListener( xListener
);
426 void SAL_CALL
SlideShowView::removeMouseListener( const Reference
< awt::XMouseListener
>& xListener
)
428 ::osl::MutexGuard
aGuard( m_aMutex
);
430 if (mpMouseListeners
)
431 mpMouseListeners
->removeTypedListener( xListener
);
434 void SAL_CALL
SlideShowView::addMouseMotionListener( const Reference
< awt::XMouseMotionListener
>& xListener
)
436 ::osl::MutexGuard
aGuard( m_aMutex
);
438 if( !mbIsMouseMotionListener
&& mxWindow
.is() )
440 // delay motion event registration, until we really
442 mbIsMouseMotionListener
= true;
443 mxWindow
->addMouseMotionListener( this );
446 if (mpMouseMotionListeners
)
447 mpMouseMotionListeners
->addTypedListener( xListener
);
450 void SAL_CALL
SlideShowView::removeMouseMotionListener( const Reference
< awt::XMouseMotionListener
>& xListener
)
452 ::osl::MutexGuard
aGuard( m_aMutex
);
454 if (mpMouseMotionListeners
)
455 mpMouseMotionListeners
->removeTypedListener( xListener
);
457 // TODO(P1): Might be nice to deregister for mouse motion
458 // events, when the last listener is gone.
461 void SAL_CALL
SlideShowView::setMouseCursor( sal_Int16 nPointerShape
)
463 ::osl::MutexGuard
aGuard( m_aMutex
);
467 mxPointer
->setType( nPointerShape
);
469 if( mxWindowPeer
.is() )
470 mxWindowPeer
->setPointer( mxPointer
);
473 awt::Rectangle SAL_CALL
SlideShowView::getCanvasArea( )
475 awt::Rectangle aRectangle
;
478 return mxWindow
->getPosSize();
480 aRectangle
.X
= aRectangle
.Y
= aRectangle
.Width
= aRectangle
.Height
= 0;
485 void SlideShowView::updateimpl( ::osl::ClearableMutexGuard
& rGuard
, SlideshowImpl
* pSlideShow
)
490 ::rtl::Reference
< SlideshowImpl
> aSLGuard( pSlideShow
);
494 mbFirstPaint
= false;
495 SlideshowImpl
* pTmpSlideShow
= mpSlideShow
;
498 pTmpSlideShow
->onFirstPaint();
502 pSlideShow
->startUpdateTimer();
505 // XWindowListener methods
506 void SAL_CALL
SlideShowView::windowResized( const awt::WindowEvent
& e
)
508 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
512 // Change event source, to enable listeners to match event
514 awt::WindowEvent
aEvent( e
);
515 aEvent
.Source
= static_cast< ::cppu::OWeakObject
* >( this );
517 mpViewListeners
->notify( aEvent
);
518 updateimpl( aGuard
, mpSlideShow
); // warning: clears guard!
522 void SAL_CALL
SlideShowView::windowMoved( const awt::WindowEvent
& )
527 void SAL_CALL
SlideShowView::windowShown( const lang::EventObject
& )
532 void SAL_CALL
SlideShowView::windowHidden( const lang::EventObject
& )
537 // XMouseListener implementation
538 void SAL_CALL
SlideShowView::mousePressed( const awt::MouseEvent
& e
)
540 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
541 if( mpSlideShow
&& mpSlideShow
->isInputFreezed() )
543 mbMousePressedEaten
= true;
547 mbMousePressedEaten
= false;
549 // Change event source, to enable listeners to match event
551 WrappedMouseEvent aEvent
;
552 aEvent
.meType
= WrappedMouseEvent::PRESSED
;
554 aEvent
.maEvent
.Source
= static_cast< ::cppu::OWeakObject
* >( this );
556 if (mpMouseListeners
)
557 mpMouseListeners
->notify( aEvent
);
558 updateimpl( aGuard
, mpSlideShow
); // warning: clears guard!
562 void SAL_CALL
SlideShowView::mouseReleased( const awt::MouseEvent
& e
)
564 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
565 if( mbMousePressedEaten
)
567 // if mouse button down was ignored, also ignore mouse button up
568 mbMousePressedEaten
= false;
570 else if( mpSlideShow
&& !mpSlideShow
->isInputFreezed() )
572 // Change event source, to enable listeners to match event
574 WrappedMouseEvent aEvent
;
575 aEvent
.meType
= WrappedMouseEvent::RELEASED
;
577 aEvent
.maEvent
.Source
= static_cast< ::cppu::OWeakObject
* >( this );
579 if (mpMouseListeners
)
580 mpMouseListeners
->notify( aEvent
);
581 updateimpl( aGuard
, mpSlideShow
); // warning: clears guard!
585 void SAL_CALL
SlideShowView::mouseEntered( const awt::MouseEvent
& e
)
587 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
589 // Change event source, to enable listeners to match event
591 WrappedMouseEvent aEvent
;
592 aEvent
.meType
= WrappedMouseEvent::ENTERED
;
594 aEvent
.maEvent
.Source
= static_cast< ::cppu::OWeakObject
* >( this );
596 if (mpMouseListeners
)
597 mpMouseListeners
->notify( aEvent
);
598 updateimpl( aGuard
, mpSlideShow
); // warning: clears guard!
601 void SAL_CALL
SlideShowView::mouseExited( const awt::MouseEvent
& e
)
603 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
605 // Change event source, to enable listeners to match event
607 WrappedMouseEvent aEvent
;
608 aEvent
.meType
= WrappedMouseEvent::EXITED
;
610 aEvent
.maEvent
.Source
= static_cast< ::cppu::OWeakObject
* >( this );
612 if (mpMouseListeners
)
613 mpMouseListeners
->notify( aEvent
);
614 updateimpl( aGuard
, mpSlideShow
); // warning: clears guard!
617 // XMouseMotionListener implementation
618 void SAL_CALL
SlideShowView::mouseDragged( const awt::MouseEvent
& e
)
620 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
622 // Change event source, to enable listeners to match event
624 WrappedMouseMotionEvent aEvent
;
625 aEvent
.meType
= WrappedMouseMotionEvent::DRAGGED
;
627 aEvent
.maEvent
.Source
= static_cast< ::cppu::OWeakObject
* >( this );
629 if (mpMouseMotionListeners
)
630 mpMouseMotionListeners
->notify( aEvent
);
631 updateimpl( aGuard
, mpSlideShow
); // warning: clears guard!
634 void SAL_CALL
SlideShowView::mouseMoved( const awt::MouseEvent
& e
)
636 ::osl::ClearableMutexGuard
aGuard( m_aMutex
);
638 // Change event source, to enable listeners to match event
640 WrappedMouseMotionEvent aEvent
;
641 aEvent
.meType
= WrappedMouseMotionEvent::MOVED
;
643 aEvent
.maEvent
.Source
= static_cast< ::cppu::OWeakObject
* >( this );
645 if (mpMouseMotionListeners
)
646 mpMouseMotionListeners
->notify( aEvent
);
647 updateimpl( aGuard
, mpSlideShow
); // warning: clears guard!
650 void SlideShowView::init()
652 mxWindow
->addWindowListener( this );
653 mxWindow
->addMouseListener( this );
655 mxPointer
= awt::Pointer::create( ::comphelper::getProcessComponentContext() );
659 // #i48939# only switch on kind of hacky scroll optimization, when
660 // running fullscreen. this minimizes the probability that other
661 // windows partially cover the show.
667 Reference
< beans::XPropertySet
> xCanvasProps( getCanvas(),
668 uno::UNO_QUERY_THROW
);
669 xCanvasProps
->setPropertyValue("UnsafeScrolling",
670 uno::makeAny( true ) );
672 catch( uno::Exception
& )
679 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */