Line data Source code
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 :
20 : #include "PresenterSlidePreview.hxx"
21 : #include "PresenterCanvasHelper.hxx"
22 : #include "PresenterGeometryHelper.hxx"
23 : #include "PresenterPaintManager.hxx"
24 : #include <com/sun/star/awt/XWindow.hpp>
25 : #include <com/sun/star/awt/XWindowPeer.hpp>
26 : #include <com/sun/star/beans/XPropertySet.hpp>
27 : #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
28 : #include <com/sun/star/drawing/framework/XControllerManager.hpp>
29 : #include <com/sun/star/drawing/framework/XPane.hpp>
30 : #include <com/sun/star/rendering/CompositeOperation.hpp>
31 :
32 : using namespace ::com::sun::star;
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::drawing::framework;
35 : using ::rtl::OUString;
36 :
37 : namespace
38 : {
39 : // Use a super sample factor greater than 1 to achive a poor mans
40 : // antialiasing effect for slide previews.
41 : const sal_Int16 gnSuperSampleFactor = 2;
42 : }
43 :
44 : namespace sdext { namespace presenter {
45 :
46 : //===== PresenterSlidePreview =================================================
47 :
48 0 : PresenterSlidePreview::PresenterSlidePreview (
49 : const Reference<XComponentContext>& rxContext,
50 : const Reference<XResourceId>& rxViewId,
51 : const Reference<XPane>& rxAnchorPane,
52 : const ::rtl::Reference<PresenterController>& rpPresenterController)
53 : : PresenterSlidePreviewInterfaceBase(m_aMutex),
54 : mpPresenterController(rpPresenterController),
55 : mxPane(rxAnchorPane),
56 : mxViewId(rxViewId),
57 : mxPreviewRenderer(),
58 : mxPreview(),
59 : mxCurrentSlide(),
60 : mnSlideAspectRatio(28.0 / 21.0),
61 : mxWindow(),
62 0 : mxCanvas()
63 : {
64 0 : if ( ! rxContext.is()
65 0 : || ! rxViewId.is()
66 0 : || ! rxAnchorPane.is()
67 0 : || ! rpPresenterController.is())
68 : {
69 : throw RuntimeException(
70 : OUString(
71 : "PresenterSlidePreview can not be constructed due to empty argument"),
72 0 : static_cast<XWeak*>(this));
73 : }
74 :
75 0 : mxWindow = rxAnchorPane->getWindow();
76 0 : mxCanvas = rxAnchorPane->getCanvas();
77 :
78 0 : if (mxWindow.is())
79 : {
80 0 : mxWindow->addWindowListener(this);
81 0 : mxWindow->addPaintListener(this);
82 :
83 0 : Reference<awt::XWindowPeer> xPeer (mxWindow, UNO_QUERY);
84 0 : if (xPeer.is())
85 0 : xPeer->setBackground(util::Color(0xff000000));
86 :
87 0 : mxWindow->setVisible(sal_True);
88 : }
89 :
90 0 : if (mpPresenterController.get() != NULL)
91 0 : mnSlideAspectRatio = mpPresenterController->GetSlideAspectRatio();
92 :
93 0 : Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager(), UNO_QUERY);
94 0 : if (xFactory.is())
95 : mxPreviewRenderer = Reference<drawing::XSlideRenderer>(
96 0 : xFactory->createInstanceWithContext(
97 : OUString("com.sun.star.drawing.SlideRenderer"),
98 0 : rxContext),
99 0 : UNO_QUERY);
100 :
101 0 : Resize();
102 0 : }
103 :
104 0 : PresenterSlidePreview::~PresenterSlidePreview (void)
105 : {
106 0 : }
107 :
108 0 : void SAL_CALL PresenterSlidePreview::disposing (void)
109 : {
110 0 : if (mxWindow.is())
111 : {
112 0 : mxWindow->removeWindowListener(this);
113 0 : mxWindow->removePaintListener(this);
114 0 : mxWindow = NULL;
115 0 : mxCanvas = NULL;
116 : }
117 :
118 0 : Reference<lang::XComponent> xComponent (mxPreviewRenderer, UNO_QUERY);
119 0 : if (xComponent.is())
120 0 : xComponent->dispose();
121 0 : }
122 :
123 : //----- XResourceId -----------------------------------------------------------
124 :
125 0 : Reference<XResourceId> SAL_CALL PresenterSlidePreview::getResourceId (void)
126 : throw (RuntimeException)
127 : {
128 0 : return mxViewId;
129 : }
130 :
131 0 : sal_Bool SAL_CALL PresenterSlidePreview::isAnchorOnly (void)
132 : throw (RuntimeException)
133 : {
134 0 : return false;
135 : }
136 :
137 : //----- XWindowListener -------------------------------------------------------
138 :
139 0 : void SAL_CALL PresenterSlidePreview::windowResized (const awt::WindowEvent& rEvent)
140 : throw (RuntimeException)
141 : {
142 : (void)rEvent;
143 0 : ThrowIfDisposed();
144 0 : ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
145 0 : Resize();
146 0 : }
147 :
148 0 : void SAL_CALL PresenterSlidePreview::windowMoved (const awt::WindowEvent& rEvent)
149 : throw (RuntimeException)
150 : {
151 : (void)rEvent;
152 0 : }
153 :
154 0 : void SAL_CALL PresenterSlidePreview::windowShown (const lang::EventObject& rEvent)
155 : throw (RuntimeException)
156 : {
157 : (void)rEvent;
158 0 : ThrowIfDisposed();
159 0 : ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
160 0 : Resize();
161 0 : }
162 :
163 0 : void SAL_CALL PresenterSlidePreview::windowHidden (const lang::EventObject& rEvent)
164 : throw (RuntimeException)
165 : {
166 : (void)rEvent;
167 0 : }
168 :
169 : //----- XPaintListener --------------------------------------------------------
170 :
171 0 : void SAL_CALL PresenterSlidePreview::windowPaint (const awt::PaintEvent& rEvent)
172 : throw (RuntimeException)
173 : {
174 0 : ThrowIfDisposed();
175 :
176 0 : ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
177 0 : if (mxWindow.is())
178 : Paint(awt::Rectangle(
179 : rEvent.UpdateRect.X,
180 : rEvent.UpdateRect.Y,
181 : rEvent.UpdateRect.Width,
182 0 : rEvent.UpdateRect.Height));
183 0 : }
184 :
185 : //----- lang::XEventListener --------------------------------------------------
186 :
187 0 : void SAL_CALL PresenterSlidePreview::disposing (const lang::EventObject& rEvent)
188 : throw (RuntimeException)
189 : {
190 0 : if (rEvent.Source == mxWindow)
191 : {
192 0 : mxWindow = NULL;
193 0 : mxCanvas = NULL;
194 0 : mxPreview = NULL;
195 : }
196 0 : }
197 :
198 : //----- XDrawView -------------------------------------------------------------
199 :
200 0 : void SAL_CALL PresenterSlidePreview::setCurrentPage (const Reference<drawing::XDrawPage>& rxSlide)
201 : throw (RuntimeException)
202 : {
203 0 : ThrowIfDisposed();
204 0 : ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
205 0 : SetSlide(rxSlide);
206 0 : }
207 :
208 0 : Reference<drawing::XDrawPage> SAL_CALL PresenterSlidePreview::getCurrentPage (void)
209 : throw (RuntimeException)
210 : {
211 0 : ThrowIfDisposed();
212 0 : return NULL;
213 : }
214 :
215 : //-----------------------------------------------------------------------------
216 :
217 0 : void PresenterSlidePreview::SetSlide (const Reference<drawing::XDrawPage>& rxPage)
218 : {
219 0 : mxCurrentSlide = rxPage;
220 0 : mxPreview = NULL;
221 :
222 0 : Reference<beans::XPropertySet> xPropertySet (mxCurrentSlide, UNO_QUERY);
223 0 : if (xPropertySet.is())
224 : {
225 0 : awt::Size aSlideSize;
226 : try
227 : {
228 0 : xPropertySet->getPropertyValue(
229 0 : OUString("Width")) >>= aSlideSize.Width;
230 0 : xPropertySet->getPropertyValue(
231 0 : OUString("Height")) >>= aSlideSize.Height;
232 : }
233 0 : catch (beans::UnknownPropertyException&)
234 : {
235 : OSL_ASSERT(false);
236 : }
237 : }
238 :
239 : // The preview is not transparent, therefore only this window, not its
240 : // parent, has to be invalidated.
241 0 : mpPresenterController->GetPaintManager()->Invalidate(mxWindow);
242 0 : }
243 :
244 0 : void PresenterSlidePreview::Paint (const awt::Rectangle& rBoundingBox)
245 : {
246 : (void)rBoundingBox;
247 0 : if ( ! mxWindow.is())
248 : return;
249 0 : if ( ! mxCanvas.is())
250 : return;
251 0 : if ( ! mxPreviewRenderer.is())
252 : return;
253 :
254 : // Make sure that a preview in the correct size exists.
255 0 : awt::Rectangle aWindowBox (mxWindow->getPosSize());
256 :
257 0 : if ( ! mxPreview.is() && mxCurrentSlide.is())
258 : {
259 : // Create a new preview bitmap.
260 0 : mxPreview = mxPreviewRenderer->createPreviewForCanvas(
261 : mxCurrentSlide,
262 : awt::Size(aWindowBox.Width, aWindowBox.Height),
263 : gnSuperSampleFactor,
264 0 : mxCanvas);
265 : }
266 :
267 : // Determine the bounding box of the preview.
268 0 : awt::Rectangle aPreviewBox;
269 0 : if (mxPreview.is())
270 : {
271 0 : const geometry::IntegerSize2D aPreviewSize (mxPreview->getSize());
272 : aPreviewBox = awt::Rectangle(
273 : (aWindowBox.Width - aPreviewSize.Width)/2,
274 : (aWindowBox.Height - aPreviewSize.Height)/2,
275 : aPreviewSize.Width,
276 0 : aPreviewSize.Height);
277 : }
278 : else
279 : {
280 0 : if (mnSlideAspectRatio > 0)
281 : {
282 0 : const awt::Size aPreviewSize (mxPreviewRenderer->calculatePreviewSize(
283 0 : mnSlideAspectRatio,awt::Size(aWindowBox.Width, aWindowBox.Height)));
284 : aPreviewBox = awt::Rectangle(
285 : (aWindowBox.Width - aPreviewSize.Width)/2,
286 : (aWindowBox.Height - aPreviewSize.Height)/2,
287 : aPreviewSize.Width,
288 0 : aPreviewSize.Height);
289 : }
290 : }
291 :
292 : // Paint the background.
293 : mpPresenterController->GetCanvasHelper()->Paint(
294 0 : mpPresenterController->GetViewBackground(mxViewId->getResourceURL()),
295 : mxCanvas,
296 : rBoundingBox,
297 : awt::Rectangle(0,0,aWindowBox.Width,aWindowBox.Height),
298 0 : aPreviewBox);
299 :
300 : // Paint the preview.
301 : const rendering::ViewState aViewState(
302 : geometry::AffineMatrix2D(1,0,0, 0,1,0),
303 0 : NULL);
304 :
305 0 : Sequence<double> aBackgroundColor(4);
306 : rendering::RenderState aRenderState (
307 : geometry::AffineMatrix2D(1, 0, aPreviewBox.X, 0, 1, aPreviewBox.Y),
308 : NULL,
309 : aBackgroundColor,
310 0 : rendering::CompositeOperation::SOURCE);
311 0 : PresenterCanvasHelper::SetDeviceColor(aRenderState, 0x00000000);
312 0 : if (mxPreview.is())
313 : {
314 0 : mxCanvas->drawBitmap(mxPreview, aViewState, aRenderState);
315 : }
316 : else
317 : {
318 0 : if (mnSlideAspectRatio > 0)
319 : {
320 : Reference<rendering::XPolyPolygon2D> xPolygon (
321 0 : PresenterGeometryHelper::CreatePolygon(aPreviewBox, mxCanvas->getDevice()));
322 0 : if (xPolygon.is())
323 0 : mxCanvas->fillPolyPolygon(xPolygon, aViewState, aRenderState);
324 : }
325 : }
326 :
327 0 : Reference<rendering::XSpriteCanvas> xSpriteCanvas (mxCanvas, UNO_QUERY);
328 0 : if (xSpriteCanvas.is())
329 0 : xSpriteCanvas->updateScreen(sal_False);
330 : }
331 :
332 0 : void PresenterSlidePreview::Resize (void)
333 : {
334 0 : if (mxPreviewRenderer.is() && mxPreview.is())
335 : {
336 0 : const awt::Rectangle aWindowBox (mxWindow->getPosSize());
337 0 : const awt::Size aNewPreviewSize (mxPreviewRenderer->calculatePreviewSize(
338 : mnSlideAspectRatio,
339 0 : awt::Size(aWindowBox.Width, aWindowBox.Height)));
340 0 : const geometry::IntegerSize2D aPreviewSize (mxPreview->getSize());
341 0 : if (aNewPreviewSize.Width==aPreviewSize.Width
342 : && aNewPreviewSize.Height==aPreviewSize.Height)
343 : {
344 : // The size of the window may have changed but the preview would
345 : // be painted in the same size (but not necessarily at the same
346 : // position.)
347 0 : return;
348 : }
349 : }
350 0 : SetSlide(mxCurrentSlide);
351 : }
352 :
353 0 : void PresenterSlidePreview::ThrowIfDisposed (void)
354 : throw (::com::sun::star::lang::DisposedException)
355 : {
356 0 : if (PresenterSlidePreviewInterfaceBase::rBHelper.bDisposed || PresenterSlidePreviewInterfaceBase::rBHelper.bInDispose)
357 : {
358 : throw lang::DisposedException (
359 : ::rtl::OUString( "PresenterSlidePreview object has already been disposed"),
360 0 : static_cast<uno::XWeak*>(this));
361 : }
362 0 : }
363 :
364 : } } // end of namespace ::sd::presenter
365 :
366 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|