Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sd / source / console / PresenterSlidePreview.cxx
blob9de4c8820b8c823e23a60d2e84f37bf07f2022f8
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 "PresenterSlidePreview.hxx"
21 #include "PresenterCanvasHelper.hxx"
22 #include "PresenterGeometryHelper.hxx"
23 #include "PresenterPaintManager.hxx"
24 #include "PresenterBitmapContainer.hxx"
25 #include <com/sun/star/awt/XWindowPeer.hpp>
26 #include <com/sun/star/rendering/CompositeOperation.hpp>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::drawing::framework;
32 namespace
34 // Use a super sample factor greater than 1 to achieve a poor mans
35 // antialiasing effect for slide previews.
36 const sal_Int16 gnSuperSampleFactor = 2;
39 namespace sdext::presenter {
41 //===== PresenterSlidePreview =================================================
43 PresenterSlidePreview::PresenterSlidePreview (
44 const Reference<XComponentContext>& rxContext,
45 const Reference<XResourceId>& rxViewId,
46 const Reference<XPane>& rxAnchorPane,
47 const ::rtl::Reference<PresenterController>& rpPresenterController)
48 : PresenterSlidePreviewInterfaceBase(m_aMutex),
49 mpPresenterController(rpPresenterController),
50 mxViewId(rxViewId),
51 mnSlideAspectRatio(28.0 / 21.0)
53 if ( ! rxContext.is()
54 || ! rxViewId.is()
55 || ! rxAnchorPane.is()
56 || ! rpPresenterController.is())
58 throw RuntimeException(
59 "PresenterSlidePreview can not be constructed due to empty argument",
60 static_cast<XWeak*>(this));
63 mxWindow = rxAnchorPane->getWindow();
64 mxCanvas = rxAnchorPane->getCanvas();
66 if (mxWindow.is())
68 mxWindow->addWindowListener(this);
69 mxWindow->addPaintListener(this);
71 Reference<awt::XWindowPeer> xPeer (mxWindow, UNO_QUERY);
72 if (xPeer.is())
73 xPeer->setBackground(util::Color(0xff000000));
75 mxWindow->setVisible(true);
78 if (mpPresenterController)
79 mnSlideAspectRatio = mpPresenterController->GetSlideAspectRatio();
81 Reference<lang::XMultiComponentFactory> xFactory = rxContext->getServiceManager();
82 if (xFactory.is())
83 mxPreviewRenderer.set(
84 xFactory->createInstanceWithContext(
85 "com.sun.star.drawing.SlideRenderer",
86 rxContext),
87 UNO_QUERY);
88 mpBitmaps = std::make_shared<PresenterBitmapContainer>(
89 "PresenterScreenSettings/ScrollBar/Bitmaps",
90 std::shared_ptr<PresenterBitmapContainer>(),
91 rxContext,
92 mxCanvas);
93 Resize();
96 PresenterSlidePreview::~PresenterSlidePreview()
100 void SAL_CALL PresenterSlidePreview::disposing()
102 if (mxWindow.is())
104 mxWindow->removeWindowListener(this);
105 mxWindow->removePaintListener(this);
106 mxWindow = nullptr;
107 mxCanvas = nullptr;
110 Reference<lang::XComponent> xComponent (mxPreviewRenderer, UNO_QUERY);
111 if (xComponent.is())
112 xComponent->dispose();
115 //----- XResourceId -----------------------------------------------------------
117 Reference<XResourceId> SAL_CALL PresenterSlidePreview::getResourceId()
119 return mxViewId;
122 sal_Bool SAL_CALL PresenterSlidePreview::isAnchorOnly()
124 return false;
127 //----- XWindowListener -------------------------------------------------------
129 void SAL_CALL PresenterSlidePreview::windowResized (const awt::WindowEvent&)
131 ThrowIfDisposed();
132 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
133 Resize();
136 void SAL_CALL PresenterSlidePreview::windowMoved (const awt::WindowEvent&) {}
138 void SAL_CALL PresenterSlidePreview::windowShown (const lang::EventObject&)
140 ThrowIfDisposed();
141 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
142 Resize();
145 void SAL_CALL PresenterSlidePreview::windowHidden (const lang::EventObject&) {}
147 //----- XPaintListener --------------------------------------------------------
149 void SAL_CALL PresenterSlidePreview::windowPaint (const awt::PaintEvent& rEvent)
151 ThrowIfDisposed();
153 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
154 if (mxWindow.is())
155 Paint(awt::Rectangle(
156 rEvent.UpdateRect.X,
157 rEvent.UpdateRect.Y,
158 rEvent.UpdateRect.Width,
159 rEvent.UpdateRect.Height));
162 //----- lang::XEventListener --------------------------------------------------
164 void SAL_CALL PresenterSlidePreview::disposing (const lang::EventObject& rEvent)
166 if (rEvent.Source == mxWindow)
168 mxWindow = nullptr;
169 mxCanvas = nullptr;
170 mxPreview = nullptr;
174 //----- XDrawView -------------------------------------------------------------
176 void SAL_CALL PresenterSlidePreview::setCurrentPage (const Reference<drawing::XDrawPage>& rxSlide)
178 ThrowIfDisposed();
179 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
180 SetSlide(rxSlide);
183 Reference<drawing::XDrawPage> SAL_CALL PresenterSlidePreview::getCurrentPage()
185 ThrowIfDisposed();
186 return nullptr;
190 void PresenterSlidePreview::SetSlide (const Reference<drawing::XDrawPage>& rxPage)
192 mxCurrentSlide = rxPage;
193 mxPreview = nullptr;
195 // The preview is not transparent, therefore only this window, not its
196 // parent, has to be invalidated.
197 mpPresenterController->GetPaintManager()->Invalidate(mxWindow);
200 void PresenterSlidePreview::Paint (const awt::Rectangle& rBoundingBox)
202 if ( ! mxWindow.is())
203 return;
204 if ( ! mxCanvas.is())
205 return;
206 if ( ! mxPreviewRenderer.is())
207 return;
209 // Make sure that a preview in the correct size exists.
210 awt::Rectangle aWindowBox (mxWindow->getPosSize());
212 bool bCustomAnimation = false;
213 bool bTransition = false;
214 if( mxCurrentSlide.is() )
216 bCustomAnimation = PresenterController::HasCustomAnimation(mxCurrentSlide);
217 bTransition = PresenterController::HasTransition(mxCurrentSlide);
220 if ( ! mxPreview.is() && mxCurrentSlide.is())
222 // Create a new preview bitmap.
223 mxPreview = mxPreviewRenderer->createPreviewForCanvas(
224 mxCurrentSlide,
225 awt::Size(aWindowBox.Width, aWindowBox.Height),
226 gnSuperSampleFactor,
227 mxCanvas);
230 // Determine the bounding box of the preview.
231 awt::Rectangle aPreviewBox;
232 if (mxPreview.is())
234 const geometry::IntegerSize2D aPreviewSize (mxPreview->getSize());
235 aPreviewBox = awt::Rectangle(
236 (aWindowBox.Width - aPreviewSize.Width)/2,
237 (aWindowBox.Height - aPreviewSize.Height)/2,
238 aPreviewSize.Width,
239 aPreviewSize.Height);
241 else
243 if (mnSlideAspectRatio > 0)
245 const awt::Size aPreviewSize (mxPreviewRenderer->calculatePreviewSize(
246 mnSlideAspectRatio,awt::Size(aWindowBox.Width, aWindowBox.Height)));
247 aPreviewBox = awt::Rectangle(
248 (aWindowBox.Width - aPreviewSize.Width)/2,
249 (aWindowBox.Height - aPreviewSize.Height)/2,
250 aPreviewSize.Width,
251 aPreviewSize.Height);
255 // Paint the background.
256 mpPresenterController->GetCanvasHelper()->Paint(
257 mpPresenterController->GetViewBackground(mxViewId->getResourceURL()),
258 mxCanvas,
259 rBoundingBox,
260 awt::Rectangle(0,0,aWindowBox.Width,aWindowBox.Height),
261 aPreviewBox);
263 // Paint the preview.
264 const rendering::ViewState aViewState(
265 geometry::AffineMatrix2D(1,0,0, 0,1,0),
266 nullptr);
268 Sequence<double> aBackgroundColor(4);
269 rendering::RenderState aRenderState (
270 geometry::AffineMatrix2D(1, 0, aPreviewBox.X, 0, 1, aPreviewBox.Y),
271 nullptr,
272 aBackgroundColor,
273 rendering::CompositeOperation::SOURCE);
274 PresenterCanvasHelper::SetDeviceColor(aRenderState, 0x00000000);
275 if (mxPreview.is())
277 mxCanvas->drawBitmap(mxPreview, aViewState, aRenderState);
278 if( bTransition )
280 const awt::Rectangle aTransitionPreviewBox(5, aWindowBox.Height-20, 0, 0);
281 SharedBitmapDescriptor aTransitionDescriptor = mpBitmaps->GetBitmap("Transition");
282 Reference<rendering::XBitmap> xTransitionIcon (aTransitionDescriptor->GetNormalBitmap());
283 rendering::RenderState aTransitionRenderState (
284 geometry::AffineMatrix2D(1, 0, aTransitionPreviewBox.X, 0, 1, aTransitionPreviewBox.Y),
285 nullptr,
286 aBackgroundColor,
287 rendering::CompositeOperation::SOURCE);
288 mxCanvas->drawBitmap(xTransitionIcon, aViewState, aTransitionRenderState);
290 if( bCustomAnimation )
292 const awt::Rectangle aAnimationPreviewBox(5, aWindowBox.Height-40, 0, 0);
293 SharedBitmapDescriptor aAnimationDescriptor = mpBitmaps->GetBitmap("Animation");
294 Reference<rendering::XBitmap> xAnimationIcon (aAnimationDescriptor->GetNormalBitmap());
295 rendering::RenderState aAnimationRenderState (
296 geometry::AffineMatrix2D(1, 0, aAnimationPreviewBox.X, 0, 1, aAnimationPreviewBox.Y),
297 nullptr,
298 aBackgroundColor,
299 rendering::CompositeOperation::SOURCE);
300 mxCanvas->drawBitmap(xAnimationIcon, aViewState, aAnimationRenderState);
303 else
305 if (mnSlideAspectRatio > 0)
307 Reference<rendering::XPolyPolygon2D> xPolygon (
308 PresenterGeometryHelper::CreatePolygon(aPreviewBox, mxCanvas->getDevice()));
309 if (xPolygon.is())
310 mxCanvas->fillPolyPolygon(xPolygon, aViewState, aRenderState);
314 Reference<rendering::XSpriteCanvas> xSpriteCanvas (mxCanvas, UNO_QUERY);
315 if (xSpriteCanvas.is())
316 xSpriteCanvas->updateScreen(false);
319 void PresenterSlidePreview::Resize()
321 if (mxPreviewRenderer.is() && mxPreview.is())
323 const awt::Rectangle aWindowBox (mxWindow->getPosSize());
324 const awt::Size aNewPreviewSize (mxPreviewRenderer->calculatePreviewSize(
325 mnSlideAspectRatio,
326 awt::Size(aWindowBox.Width, aWindowBox.Height)));
327 const geometry::IntegerSize2D aPreviewSize (mxPreview->getSize());
328 if (aNewPreviewSize.Width==aPreviewSize.Width
329 && aNewPreviewSize.Height==aPreviewSize.Height)
331 // The size of the window may have changed but the preview would
332 // be painted in the same size (but not necessarily at the same
333 // position.)
334 return;
337 SetSlide(mxCurrentSlide);
340 void PresenterSlidePreview::ThrowIfDisposed()
342 if (PresenterSlidePreviewInterfaceBase::rBHelper.bDisposed || PresenterSlidePreviewInterfaceBase::rBHelper.bInDispose)
344 throw lang::DisposedException (
345 "PresenterSlidePreview object has already been disposed",
346 static_cast<uno::XWeak*>(this));
350 } // end of namespace ::sdext::presenter
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */