bump product version to 5.0.4.1
[LibreOffice.git] / sdext / source / presenter / PresenterSlidePreview.cxx
blob2610348377bd9d8ba7f841c8599356d858b68d7f
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 "PresenterScrollBar.hxx"
25 #include "PresenterBitmapContainer.hxx"
26 #include <com/sun/star/awt/XWindow.hpp>
27 #include <com/sun/star/awt/XWindowPeer.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
30 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
31 #include <com/sun/star/drawing/framework/XPane.hpp>
32 #include <com/sun/star/rendering/CompositeOperation.hpp>
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::drawing::framework;
38 namespace
40 // Use a super sample factor greater than 1 to achieve a poor mans
41 // antialiasing effect for slide previews.
42 const sal_Int16 gnSuperSampleFactor = 2;
45 namespace sdext { namespace presenter {
47 //===== PresenterSlidePreview =================================================
49 PresenterSlidePreview::PresenterSlidePreview (
50 const Reference<XComponentContext>& rxContext,
51 const Reference<XResourceId>& rxViewId,
52 const Reference<XPane>& rxAnchorPane,
53 const ::rtl::Reference<PresenterController>& rpPresenterController)
54 : PresenterSlidePreviewInterfaceBase(m_aMutex),
55 mpPresenterController(rpPresenterController),
56 mxPane(rxAnchorPane),
57 mxViewId(rxViewId),
58 mxPreviewRenderer(),
59 mxPreview(),
60 mxCurrentSlide(),
61 mnSlideAspectRatio(28.0 / 21.0),
62 mxWindow(),
63 mxCanvas()
65 if ( ! rxContext.is()
66 || ! rxViewId.is()
67 || ! rxAnchorPane.is()
68 || ! rpPresenterController.is())
70 throw RuntimeException(
71 "PresenterSlidePreview can not be constructed due to empty argument",
72 static_cast<XWeak*>(this));
75 mxWindow = rxAnchorPane->getWindow();
76 mxCanvas = rxAnchorPane->getCanvas();
78 if (mxWindow.is())
80 mxWindow->addWindowListener(this);
81 mxWindow->addPaintListener(this);
83 Reference<awt::XWindowPeer> xPeer (mxWindow, UNO_QUERY);
84 if (xPeer.is())
85 xPeer->setBackground(util::Color(0xff000000));
87 mxWindow->setVisible(sal_True);
90 if (mpPresenterController.get() != NULL)
91 mnSlideAspectRatio = mpPresenterController->GetSlideAspectRatio();
93 Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager(), UNO_QUERY);
94 if (xFactory.is())
95 mxPreviewRenderer = Reference<drawing::XSlideRenderer>(
96 xFactory->createInstanceWithContext(
97 OUString("com.sun.star.drawing.SlideRenderer"),
98 rxContext),
99 UNO_QUERY);
100 mpBitmaps.reset(new PresenterBitmapContainer(
101 OUString("PresenterScreenSettings/ScrollBar/Bitmaps"),
102 ::boost::shared_ptr<PresenterBitmapContainer>(),
103 rxContext,
104 mxCanvas));
105 Resize();
108 PresenterSlidePreview::~PresenterSlidePreview()
112 void SAL_CALL PresenterSlidePreview::disposing()
114 if (mxWindow.is())
116 mxWindow->removeWindowListener(this);
117 mxWindow->removePaintListener(this);
118 mxWindow = NULL;
119 mxCanvas = NULL;
122 Reference<lang::XComponent> xComponent (mxPreviewRenderer, UNO_QUERY);
123 if (xComponent.is())
124 xComponent->dispose();
127 //----- XResourceId -----------------------------------------------------------
129 Reference<XResourceId> SAL_CALL PresenterSlidePreview::getResourceId()
130 throw (RuntimeException, std::exception)
132 return mxViewId;
135 sal_Bool SAL_CALL PresenterSlidePreview::isAnchorOnly()
136 throw (RuntimeException, std::exception)
138 return false;
141 //----- XWindowListener -------------------------------------------------------
143 void SAL_CALL PresenterSlidePreview::windowResized (const awt::WindowEvent& rEvent)
144 throw (RuntimeException, std::exception)
146 (void)rEvent;
147 ThrowIfDisposed();
148 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
149 Resize();
152 void SAL_CALL PresenterSlidePreview::windowMoved (const awt::WindowEvent& rEvent)
153 throw (RuntimeException, std::exception)
155 (void)rEvent;
158 void SAL_CALL PresenterSlidePreview::windowShown (const lang::EventObject& rEvent)
159 throw (RuntimeException, std::exception)
161 (void)rEvent;
162 ThrowIfDisposed();
163 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
164 Resize();
167 void SAL_CALL PresenterSlidePreview::windowHidden (const lang::EventObject& rEvent)
168 throw (RuntimeException, std::exception)
170 (void)rEvent;
173 //----- XPaintListener --------------------------------------------------------
175 void SAL_CALL PresenterSlidePreview::windowPaint (const awt::PaintEvent& rEvent)
176 throw (RuntimeException, std::exception)
178 ThrowIfDisposed();
180 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
181 if (mxWindow.is())
182 Paint(awt::Rectangle(
183 rEvent.UpdateRect.X,
184 rEvent.UpdateRect.Y,
185 rEvent.UpdateRect.Width,
186 rEvent.UpdateRect.Height));
189 //----- lang::XEventListener --------------------------------------------------
191 void SAL_CALL PresenterSlidePreview::disposing (const lang::EventObject& rEvent)
192 throw (RuntimeException, std::exception)
194 if (rEvent.Source == mxWindow)
196 mxWindow = NULL;
197 mxCanvas = NULL;
198 mxPreview = NULL;
202 //----- XDrawView -------------------------------------------------------------
204 void SAL_CALL PresenterSlidePreview::setCurrentPage (const Reference<drawing::XDrawPage>& rxSlide)
205 throw (RuntimeException, std::exception)
207 ThrowIfDisposed();
208 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
209 SetSlide(rxSlide);
212 Reference<drawing::XDrawPage> SAL_CALL PresenterSlidePreview::getCurrentPage()
213 throw (RuntimeException, std::exception)
215 ThrowIfDisposed();
216 return NULL;
221 void PresenterSlidePreview::SetSlide (const Reference<drawing::XDrawPage>& rxPage)
223 mxCurrentSlide = rxPage;
224 mxPreview = NULL;
226 Reference<beans::XPropertySet> xPropertySet (mxCurrentSlide, UNO_QUERY);
227 if (xPropertySet.is())
229 awt::Size aSlideSize;
232 xPropertySet->getPropertyValue(
233 OUString("Width")) >>= aSlideSize.Width;
234 xPropertySet->getPropertyValue(
235 OUString("Height")) >>= aSlideSize.Height;
237 catch (beans::UnknownPropertyException&)
239 OSL_ASSERT(false);
243 // The preview is not transparent, therefore only this window, not its
244 // parent, has to be invalidated.
245 mpPresenterController->GetPaintManager()->Invalidate(mxWindow);
248 void PresenterSlidePreview::Paint (const awt::Rectangle& rBoundingBox)
250 (void)rBoundingBox;
251 if ( ! mxWindow.is())
252 return;
253 if ( ! mxCanvas.is())
254 return;
255 if ( ! mxPreviewRenderer.is())
256 return;
258 // Make sure that a preview in the correct size exists.
259 awt::Rectangle aWindowBox (mxWindow->getPosSize());
261 bool bCustomAnimation = false;
262 bool bTransition = false;
263 if( mxCurrentSlide.is() )
265 bCustomAnimation = PresenterController::HasCustomAnimation(mxCurrentSlide);
266 bTransition = PresenterController::HasTransition(mxCurrentSlide);
269 if ( ! mxPreview.is() && mxCurrentSlide.is())
271 // Create a new preview bitmap.
272 mxPreview = mxPreviewRenderer->createPreviewForCanvas(
273 mxCurrentSlide,
274 awt::Size(aWindowBox.Width, aWindowBox.Height),
275 gnSuperSampleFactor,
276 mxCanvas);
279 // Determine the bounding box of the preview.
280 awt::Rectangle aPreviewBox;
281 if (mxPreview.is())
283 const geometry::IntegerSize2D aPreviewSize (mxPreview->getSize());
284 aPreviewBox = awt::Rectangle(
285 (aWindowBox.Width - aPreviewSize.Width)/2,
286 (aWindowBox.Height - aPreviewSize.Height)/2,
287 aPreviewSize.Width,
288 aPreviewSize.Height);
290 else
292 if (mnSlideAspectRatio > 0)
294 const awt::Size aPreviewSize (mxPreviewRenderer->calculatePreviewSize(
295 mnSlideAspectRatio,awt::Size(aWindowBox.Width, aWindowBox.Height)));
296 aPreviewBox = awt::Rectangle(
297 (aWindowBox.Width - aPreviewSize.Width)/2,
298 (aWindowBox.Height - aPreviewSize.Height)/2,
299 aPreviewSize.Width,
300 aPreviewSize.Height);
304 // Paint the background.
305 mpPresenterController->GetCanvasHelper()->Paint(
306 mpPresenterController->GetViewBackground(mxViewId->getResourceURL()),
307 mxCanvas,
308 rBoundingBox,
309 awt::Rectangle(0,0,aWindowBox.Width,aWindowBox.Height),
310 aPreviewBox);
312 // Paint the preview.
313 const rendering::ViewState aViewState(
314 geometry::AffineMatrix2D(1,0,0, 0,1,0),
315 NULL);
317 Sequence<double> aBackgroundColor(4);
318 rendering::RenderState aRenderState (
319 geometry::AffineMatrix2D(1, 0, aPreviewBox.X, 0, 1, aPreviewBox.Y),
320 NULL,
321 aBackgroundColor,
322 rendering::CompositeOperation::SOURCE);
323 PresenterCanvasHelper::SetDeviceColor(aRenderState, 0x00000000);
324 if (mxPreview.is())
326 mxCanvas->drawBitmap(mxPreview, aViewState, aRenderState);
327 if( bTransition )
329 const awt::Rectangle aTransitionPreviewBox(5, aWindowBox.Height-20, 0, 0);
330 SharedBitmapDescriptor aTransitionDescriptor = mpBitmaps->GetBitmap("Transition");
331 Reference<rendering::XBitmap> xTransitionIcon (aTransitionDescriptor->GetNormalBitmap());
332 rendering::RenderState aTransitionRenderState (
333 geometry::AffineMatrix2D(1, 0, aTransitionPreviewBox.X, 0, 1, aTransitionPreviewBox.Y),
334 NULL,
335 aBackgroundColor,
336 rendering::CompositeOperation::SOURCE);
337 mxCanvas->drawBitmap(xTransitionIcon, aViewState, aTransitionRenderState);
339 if( bCustomAnimation )
341 const awt::Rectangle aAnimationPreviewBox(5, aWindowBox.Height-40, 0, 0);
342 SharedBitmapDescriptor aAnimationDescriptor = mpBitmaps->GetBitmap("Animation");
343 Reference<rendering::XBitmap> xAnimationIcon (aAnimationDescriptor->GetNormalBitmap());
344 rendering::RenderState aAnimationRenderState (
345 geometry::AffineMatrix2D(1, 0, aAnimationPreviewBox.X, 0, 1, aAnimationPreviewBox.Y),
346 NULL,
347 aBackgroundColor,
348 rendering::CompositeOperation::SOURCE);
349 mxCanvas->drawBitmap(xAnimationIcon, aViewState, aAnimationRenderState);
352 else
354 if (mnSlideAspectRatio > 0)
356 Reference<rendering::XPolyPolygon2D> xPolygon (
357 PresenterGeometryHelper::CreatePolygon(aPreviewBox, mxCanvas->getDevice()));
358 if (xPolygon.is())
359 mxCanvas->fillPolyPolygon(xPolygon, aViewState, aRenderState);
363 Reference<rendering::XSpriteCanvas> xSpriteCanvas (mxCanvas, UNO_QUERY);
364 if (xSpriteCanvas.is())
365 xSpriteCanvas->updateScreen(sal_False);
368 void PresenterSlidePreview::Resize()
370 if (mxPreviewRenderer.is() && mxPreview.is())
372 const awt::Rectangle aWindowBox (mxWindow->getPosSize());
373 const awt::Size aNewPreviewSize (mxPreviewRenderer->calculatePreviewSize(
374 mnSlideAspectRatio,
375 awt::Size(aWindowBox.Width, aWindowBox.Height)));
376 const geometry::IntegerSize2D aPreviewSize (mxPreview->getSize());
377 if (aNewPreviewSize.Width==aPreviewSize.Width
378 && aNewPreviewSize.Height==aPreviewSize.Height)
380 // The size of the window may have changed but the preview would
381 // be painted in the same size (but not necessarily at the same
382 // position.)
383 return;
386 SetSlide(mxCurrentSlide);
389 void PresenterSlidePreview::ThrowIfDisposed()
390 throw (::com::sun::star::lang::DisposedException)
392 if (PresenterSlidePreviewInterfaceBase::rBHelper.bDisposed || PresenterSlidePreviewInterfaceBase::rBHelper.bInDispose)
394 throw lang::DisposedException (
395 "PresenterSlidePreview object has already been disposed",
396 static_cast<uno::XWeak*>(this));
400 } } // end of namespace ::sd::presenter
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */