LibreOffice Module sdext (master)  1
PresenterSlidePreview.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
25 #include <com/sun/star/awt/XWindowPeer.hpp>
26 #include <com/sun/star/rendering/CompositeOperation.hpp>
27 
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::drawing::framework;
31 
32 namespace
33 {
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;
37 }
38 
39 namespace sdext::presenter {
40 
41 //===== PresenterSlidePreview =================================================
42 
44  const Reference<XComponentContext>& rxContext,
45  const Reference<XResourceId>& rxViewId,
46  const Reference<XPane>& rxAnchorPane,
47  const ::rtl::Reference<PresenterController>& rpPresenterController)
49  mpPresenterController(rpPresenterController),
50  mxViewId(rxViewId),
51  mxPreviewRenderer(),
52  mxPreview(),
53  mxCurrentSlide(),
54  mnSlideAspectRatio(28.0 / 21.0),
55  mxWindow(),
56  mxCanvas()
57 {
58  if ( ! rxContext.is()
59  || ! rxViewId.is()
60  || ! rxAnchorPane.is()
61  || ! rpPresenterController.is())
62  {
63  throw RuntimeException(
64  "PresenterSlidePreview can not be constructed due to empty argument",
65  static_cast<XWeak*>(this));
66  }
67 
68  mxWindow = rxAnchorPane->getWindow();
69  mxCanvas = rxAnchorPane->getCanvas();
70 
71  if (mxWindow.is())
72  {
73  mxWindow->addWindowListener(this);
74  mxWindow->addPaintListener(this);
75 
76  Reference<awt::XWindowPeer> xPeer (mxWindow, UNO_QUERY);
77  if (xPeer.is())
78  xPeer->setBackground(util::Color(0xff000000));
79 
80  mxWindow->setVisible(true);
81  }
82 
84  mnSlideAspectRatio = mpPresenterController->GetSlideAspectRatio();
85 
86  Reference<lang::XMultiComponentFactory> xFactory = rxContext->getServiceManager();
87  if (xFactory.is())
88  mxPreviewRenderer.set(
89  xFactory->createInstanceWithContext(
90  "com.sun.star.drawing.SlideRenderer",
91  rxContext),
92  UNO_QUERY);
93  mpBitmaps = std::make_shared<PresenterBitmapContainer>(
94  "PresenterScreenSettings/ScrollBar/Bitmaps",
95  std::shared_ptr<PresenterBitmapContainer>(),
96  rxContext,
97  mxCanvas);
98  Resize();
99 }
100 
102 {
103 }
104 
106 {
107  if (mxWindow.is())
108  {
109  mxWindow->removeWindowListener(this);
110  mxWindow->removePaintListener(this);
111  mxWindow = nullptr;
112  mxCanvas = nullptr;
113  }
114 
115  Reference<lang::XComponent> xComponent (mxPreviewRenderer, UNO_QUERY);
116  if (xComponent.is())
117  xComponent->dispose();
118 }
119 
120 //----- XResourceId -----------------------------------------------------------
121 
122 Reference<XResourceId> SAL_CALL PresenterSlidePreview::getResourceId()
123 {
124  return mxViewId;
125 }
126 
128 {
129  return false;
130 }
131 
132 //----- XWindowListener -------------------------------------------------------
133 
134 void SAL_CALL PresenterSlidePreview::windowResized (const awt::WindowEvent&)
135 {
136  ThrowIfDisposed();
137  ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
138  Resize();
139 }
140 
141 void SAL_CALL PresenterSlidePreview::windowMoved (const awt::WindowEvent&) {}
142 
143 void SAL_CALL PresenterSlidePreview::windowShown (const lang::EventObject&)
144 {
145  ThrowIfDisposed();
146  ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
147  Resize();
148 }
149 
150 void SAL_CALL PresenterSlidePreview::windowHidden (const lang::EventObject&) {}
151 
152 //----- XPaintListener --------------------------------------------------------
153 
154 void SAL_CALL PresenterSlidePreview::windowPaint (const awt::PaintEvent& rEvent)
155 {
156  ThrowIfDisposed();
157 
158  ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
159  if (mxWindow.is())
160  Paint(awt::Rectangle(
161  rEvent.UpdateRect.X,
162  rEvent.UpdateRect.Y,
163  rEvent.UpdateRect.Width,
164  rEvent.UpdateRect.Height));
165 }
166 
167 //----- lang::XEventListener --------------------------------------------------
168 
169 void SAL_CALL PresenterSlidePreview::disposing (const lang::EventObject& rEvent)
170 {
171  if (rEvent.Source == mxWindow)
172  {
173  mxWindow = nullptr;
174  mxCanvas = nullptr;
175  mxPreview = nullptr;
176  }
177 }
178 
179 //----- XDrawView -------------------------------------------------------------
180 
182 {
183  ThrowIfDisposed();
184  ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
185  SetSlide(rxSlide);
186 }
187 
189 {
190  ThrowIfDisposed();
191  return nullptr;
192 }
193 
194 
196 {
197  mxCurrentSlide = rxPage;
198  mxPreview = nullptr;
199 
200  // The preview is not transparent, therefore only this window, not its
201  // parent, has to be invalidated.
202  mpPresenterController->GetPaintManager()->Invalidate(mxWindow);
203 }
204 
205 void PresenterSlidePreview::Paint (const awt::Rectangle& rBoundingBox)
206 {
207  if ( ! mxWindow.is())
208  return;
209  if ( ! mxCanvas.is())
210  return;
211  if ( ! mxPreviewRenderer.is())
212  return;
213 
214  // Make sure that a preview in the correct size exists.
215  awt::Rectangle aWindowBox (mxWindow->getPosSize());
216 
217  bool bCustomAnimation = false;
218  bool bTransition = false;
219  if( mxCurrentSlide.is() )
220  {
223  }
224 
225  if ( ! mxPreview.is() && mxCurrentSlide.is())
226  {
227  // Create a new preview bitmap.
228  mxPreview = mxPreviewRenderer->createPreviewForCanvas(
230  awt::Size(aWindowBox.Width, aWindowBox.Height),
231  gnSuperSampleFactor,
232  mxCanvas);
233  }
234 
235  // Determine the bounding box of the preview.
236  awt::Rectangle aPreviewBox;
237  if (mxPreview.is())
238  {
239  const geometry::IntegerSize2D aPreviewSize (mxPreview->getSize());
240  aPreviewBox = awt::Rectangle(
241  (aWindowBox.Width - aPreviewSize.Width)/2,
242  (aWindowBox.Height - aPreviewSize.Height)/2,
243  aPreviewSize.Width,
244  aPreviewSize.Height);
245  }
246  else
247  {
248  if (mnSlideAspectRatio > 0)
249  {
250  const awt::Size aPreviewSize (mxPreviewRenderer->calculatePreviewSize(
251  mnSlideAspectRatio,awt::Size(aWindowBox.Width, aWindowBox.Height)));
252  aPreviewBox = awt::Rectangle(
253  (aWindowBox.Width - aPreviewSize.Width)/2,
254  (aWindowBox.Height - aPreviewSize.Height)/2,
255  aPreviewSize.Width,
256  aPreviewSize.Height);
257  }
258  }
259 
260  // Paint the background.
261  mpPresenterController->GetCanvasHelper()->Paint(
262  mpPresenterController->GetViewBackground(mxViewId->getResourceURL()),
263  mxCanvas,
264  rBoundingBox,
265  awt::Rectangle(0,0,aWindowBox.Width,aWindowBox.Height),
266  aPreviewBox);
267 
268  // Paint the preview.
269  const rendering::ViewState aViewState(
270  geometry::AffineMatrix2D(1,0,0, 0,1,0),
271  nullptr);
272 
273  Sequence<double> aBackgroundColor(4);
274  rendering::RenderState aRenderState (
275  geometry::AffineMatrix2D(1, 0, aPreviewBox.X, 0, 1, aPreviewBox.Y),
276  nullptr,
277  aBackgroundColor,
278  rendering::CompositeOperation::SOURCE);
279  PresenterCanvasHelper::SetDeviceColor(aRenderState, 0x00000000);
280  if (mxPreview.is())
281  {
282  mxCanvas->drawBitmap(mxPreview, aViewState, aRenderState);
283  if( bTransition )
284  {
285  const awt::Rectangle aTransitionPreviewBox(5, aWindowBox.Height-20, 0, 0);
286  SharedBitmapDescriptor aTransitionDescriptor = mpBitmaps->GetBitmap("Transition");
287  Reference<rendering::XBitmap> xTransitionIcon (aTransitionDescriptor->GetNormalBitmap());
288  rendering::RenderState aTransitionRenderState (
289  geometry::AffineMatrix2D(1, 0, aTransitionPreviewBox.X, 0, 1, aTransitionPreviewBox.Y),
290  nullptr,
291  aBackgroundColor,
292  rendering::CompositeOperation::SOURCE);
293  mxCanvas->drawBitmap(xTransitionIcon, aViewState, aTransitionRenderState);
294  }
295  if( bCustomAnimation )
296  {
297  const awt::Rectangle aAnimationPreviewBox(5, aWindowBox.Height-40, 0, 0);
298  SharedBitmapDescriptor aAnimationDescriptor = mpBitmaps->GetBitmap("Animation");
299  Reference<rendering::XBitmap> xAnimationIcon (aAnimationDescriptor->GetNormalBitmap());
300  rendering::RenderState aAnimationRenderState (
301  geometry::AffineMatrix2D(1, 0, aAnimationPreviewBox.X, 0, 1, aAnimationPreviewBox.Y),
302  nullptr,
303  aBackgroundColor,
304  rendering::CompositeOperation::SOURCE);
305  mxCanvas->drawBitmap(xAnimationIcon, aViewState, aAnimationRenderState);
306  }
307  }
308  else
309  {
310  if (mnSlideAspectRatio > 0)
311  {
312  Reference<rendering::XPolyPolygon2D> xPolygon (
313  PresenterGeometryHelper::CreatePolygon(aPreviewBox, mxCanvas->getDevice()));
314  if (xPolygon.is())
315  mxCanvas->fillPolyPolygon(xPolygon, aViewState, aRenderState);
316  }
317  }
318 
319  Reference<rendering::XSpriteCanvas> xSpriteCanvas (mxCanvas, UNO_QUERY);
320  if (xSpriteCanvas.is())
321  xSpriteCanvas->updateScreen(false);
322 }
323 
325 {
326  if (mxPreviewRenderer.is() && mxPreview.is())
327  {
328  const awt::Rectangle aWindowBox (mxWindow->getPosSize());
329  const awt::Size aNewPreviewSize (mxPreviewRenderer->calculatePreviewSize(
331  awt::Size(aWindowBox.Width, aWindowBox.Height)));
332  const geometry::IntegerSize2D aPreviewSize (mxPreview->getSize());
333  if (aNewPreviewSize.Width==aPreviewSize.Width
334  && aNewPreviewSize.Height==aPreviewSize.Height)
335  {
336  // The size of the window may have changed but the preview would
337  // be painted in the same size (but not necessarily at the same
338  // position.)
339  return;
340  }
341  }
343 }
344 
346 {
347  if (PresenterSlidePreviewInterfaceBase::rBHelper.bDisposed || PresenterSlidePreviewInterfaceBase::rBHelper.bInDispose)
348  {
349  throw lang::DisposedException (
350  "PresenterSlidePreview object has already been disposed",
351  static_cast<uno::XWeak*>(this));
352  }
353 }
354 
355 } // end of namespace ::sdext::presenter
356 
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< PresenterBitmapContainer > mpBitmaps
std::shared_ptr< PresenterBitmapContainer > mpBitmaps
Reference< rendering::XCanvas > mxCanvas
css::uno::Reference< css::drawing::framework::XResourceId > mxViewId
css::uno::Reference< css::awt::XWindow > mxWindow
osl::Mutex m_aMutex
css::uno::Reference< css::rendering::XCanvas > mxCanvas
virtual void SAL_CALL disposing() override
static bool HasCustomAnimation(css::uno::Reference< css::drawing::XDrawPage > const &rxPage)
css::uno::Reference< css::rendering::XCanvas > mxCanvas
virtual sal_Bool SAL_CALL isAnchorOnly() override
std::shared_ptr< PresenterBitmapContainer::BitmapDescriptor > SharedBitmapDescriptor
css::uno::Reference< css::drawing::XDrawPage > mxCurrentSlide
The current slide for which a preview is displayed.
virtual void SAL_CALL windowMoved(const css::awt::WindowEvent &rEvent) override
void Resize()
React to a resize of the anchor pane.
virtual css::uno::Reference< css::drawing::XDrawPage > SAL_CALL getCurrentPage() override
virtual void SAL_CALL windowHidden(const css::lang::EventObject &rEvent) override
css::uno::Reference< css::drawing::XSlideRenderer > mxPreviewRenderer
css::uno::Reference< css::rendering::XBitmap > mxPreview
This Image holds the preview of the current slide.
unsigned char sal_Bool
virtual void SAL_CALL setCurrentPage(const css::uno::Reference< css::drawing::XDrawPage > &rxSlide) override
void SetSlide(const css::uno::Reference< css::drawing::XDrawPage > &rxPage)
Set the given slide as the current slide of the called PresenterSlidePreview object.
static css::uno::Reference< css::rendering::XPolyPolygon2D > CreatePolygon(const css::awt::Rectangle &rBox, const css::uno::Reference< css::rendering::XGraphicDevice > &rxDevice)
::rtl::Reference< PresenterController > mpPresenterController
::cppu::WeakComponentImplHelper< css::drawing::framework::XView, css::drawing::XDrawView, css::awt::XPaintListener, css::awt::XWindowListener > PresenterSlidePreviewInterfaceBase
virtual void SAL_CALL windowResized(const css::awt::WindowEvent &rEvent) override
rtl::Reference< PresenterController > mpPresenterController
virtual void SAL_CALL windowPaint(const css::awt::PaintEvent &rEvent) override
virtual void SAL_CALL windowShown(const css::lang::EventObject &rEvent) override
static void SetDeviceColor(css::rendering::RenderState &rRenderState, const css::util::Color aColor)
virtual css::uno::Reference< css::drawing::framework::XResourceId > SAL_CALL getResourceId() override
PresenterSlidePreview(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::drawing::framework::XResourceId > &rxViewId, const css::uno::Reference< css::drawing::framework::XPane > &rxAnchorPane, const ::rtl::Reference< PresenterController > &rpPresenterController)
Reference< XSingleServiceFactory > xFactory
css::uno::Reference< css::awt::XWindow > mxWindow
void Paint(const css::awt::Rectangle &rBoundingBox)
Paint the preview of the current slide centered in the window of the anchor pane. ...
static bool HasTransition(css::uno::Reference< css::drawing::XDrawPage > const &rxPage)