Update ooo320-m1
[ooovba.git] / sd / source / ui / presenter / SlideRenderer.cxx
blob6bcabf5a736f670e919aec774cecbf24785ec130
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlideRenderer.cxx,v $
11 * $Revision: 1.3 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "precompiled_sd.hxx"
34 #include "SlideRenderer.hxx"
35 #include "sdpage.hxx"
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include <vos/mutex.hxx>
38 #include <vcl/svapp.hxx>
39 #include <cppcanvas/vclfactory.hxx>
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::uno;
43 using ::rtl::OUString;
45 namespace sd { namespace presenter {
47 //===== Service ===============================================================
49 Reference<XInterface> SAL_CALL SlideRenderer_createInstance (
50 const Reference<XComponentContext>& rxContext)
52 return Reference<XInterface>(static_cast<XWeak*>(new SlideRenderer(rxContext)));
58 ::rtl::OUString SlideRenderer_getImplementationName (void) throw(RuntimeException)
60 return OUString::createFromAscii("com.sun.star.comp.Draw.SlideRenderer");
66 Sequence<rtl::OUString> SAL_CALL SlideRenderer_getSupportedServiceNames (void)
67 throw (RuntimeException)
69 static const ::rtl::OUString sServiceName(
70 ::rtl::OUString::createFromAscii("com.sun.star.drawing.SlideRenderer"));
71 return Sequence<rtl::OUString>(&sServiceName, 1);
77 //===== SlideRenderer ==========================================================
79 SlideRenderer::SlideRenderer (const Reference<XComponentContext>& rxContext)
80 : SlideRendererInterfaceBase(m_aMutex),
81 maPreviewRenderer()
83 (void)rxContext;
89 SlideRenderer::~SlideRenderer (void)
96 void SAL_CALL SlideRenderer::disposing (void)
103 //----- XInitialization -------------------------------------------------------
105 void SAL_CALL SlideRenderer::initialize (const Sequence<Any>& rArguments)
106 throw (Exception, RuntimeException)
108 ThrowIfDisposed();
110 if (rArguments.getLength() == 0)
115 catch (RuntimeException&)
117 throw;
120 else
122 throw RuntimeException(
123 OUString::createFromAscii("SlideRenderer: invalid number of arguments"),
124 static_cast<XWeak*>(this));
131 //----- XSlideRenderer --------------------------------------------------------
133 Reference<awt::XBitmap> SlideRenderer::createPreview (
134 const Reference<drawing::XDrawPage>& rxSlide,
135 const awt::Size& rMaximalSize,
136 sal_Int16 nSuperSampleFactor)
137 throw (css::uno::RuntimeException)
139 ThrowIfDisposed();
140 ::vos::OGuard aGuard (Application::GetSolarMutex());
142 return VCLUnoHelper::CreateBitmap(
143 CreatePreview(rxSlide, rMaximalSize, nSuperSampleFactor));
149 Reference<rendering::XBitmap> SlideRenderer::createPreviewForCanvas (
150 const Reference<drawing::XDrawPage>& rxSlide,
151 const awt::Size& rMaximalSize,
152 sal_Int16 nSuperSampleFactor,
153 const Reference<rendering::XCanvas>& rxCanvas)
154 throw (css::uno::RuntimeException)
156 ThrowIfDisposed();
157 ::vos::OGuard aGuard (Application::GetSolarMutex());
159 cppcanvas::BitmapCanvasSharedPtr pCanvas (cppcanvas::VCLFactory::getInstance().createCanvas(
160 Reference<rendering::XBitmapCanvas>(rxCanvas, UNO_QUERY)));
161 if (pCanvas.get() != NULL)
162 return cppcanvas::VCLFactory::getInstance().createBitmap(
163 pCanvas,
164 CreatePreview(rxSlide, rMaximalSize, nSuperSampleFactor))->getUNOBitmap();
165 else
166 return NULL;
172 awt::Size SAL_CALL SlideRenderer::calculatePreviewSize (
173 double nSlideAspectRatio,
174 const awt::Size& rMaximalSize)
175 throw (css::uno::RuntimeException)
177 if (rMaximalSize.Width <= 0
178 || rMaximalSize.Height <= 0
179 || nSlideAspectRatio <= 0)
181 return awt::Size(0,0);
184 const double nWindowAspectRatio (double(rMaximalSize.Width) / double(rMaximalSize.Height));
185 if (nSlideAspectRatio < nWindowAspectRatio)
186 return awt::Size(
187 sal::static_int_cast<sal_Int32>(rMaximalSize.Height * nSlideAspectRatio),
188 rMaximalSize.Height);
189 else
190 return awt::Size(
191 rMaximalSize.Width,
192 sal::static_int_cast<sal_Int32>(rMaximalSize.Width / nSlideAspectRatio));
198 //-----------------------------------------------------------------------------
200 BitmapEx SlideRenderer::CreatePreview (
201 const Reference<drawing::XDrawPage>& rxSlide,
202 const awt::Size& rMaximalSize,
203 sal_Int16 nSuperSampleFactor)
204 throw (css::uno::RuntimeException)
206 const SdPage* pPage = SdPage::getImplementation(rxSlide);
207 if (pPage == NULL)
208 throw lang::IllegalArgumentException(
209 OUString::createFromAscii("SlideRenderer::createPreview() called with invalid slide"),
210 static_cast<XWeak*>(this),
213 // Determine the size of the current slide and its aspect ratio.
214 Size aPageSize = pPage->GetSize();
215 if (aPageSize.Height() <= 0)
216 throw lang::IllegalArgumentException(
217 OUString::createFromAscii("SlideRenderer::createPreview() called with invalid size"),
218 static_cast<XWeak*>(this),
221 // Compare with the aspect ratio of the window (which rMaximalSize
222 // assumed to be) and calculate the size of the preview so that it
223 // a) will have the aspect ratio of the page and
224 // b) will be as large as possible.
225 awt::Size aPreviewSize (calculatePreviewSize(
226 double(aPageSize.Width()) / double(aPageSize.Height()),
227 rMaximalSize));
228 if (aPreviewSize.Width <= 0 || aPreviewSize.Height <= 0)
229 return BitmapEx();
231 // Make sure that the super sample factor has a sane value.
232 sal_Int16 nFactor (nSuperSampleFactor);
233 if (nFactor < 1)
234 nFactor = 1;
235 else if (nFactor > 10)
236 nFactor = 10;
238 // Create the preview. When the super sample factor n is greater than 1
239 // then a preview is created in size (n*width, n*height) and then scaled
240 // down to (width, height). This is a poor mans antialiasing for the
241 // time being. When we have true antialiasing support this workaround
242 // can be removed.
243 const Image aPreview = maPreviewRenderer.RenderPage (
244 pPage,
245 Size(aPreviewSize.Width*nFactor, aPreviewSize.Height*nFactor),
246 ::rtl::OUString());
247 if (nFactor == 1)
248 return aPreview.GetBitmapEx();
249 else
251 BitmapEx aScaledPreview = aPreview.GetBitmapEx();
252 aScaledPreview.Scale(
253 Size(aPreviewSize.Width,aPreviewSize.Height),
254 BMP_SCALE_INTERPOLATE);
255 return aScaledPreview;
262 void SlideRenderer::ThrowIfDisposed (void)
263 throw (::com::sun::star::lang::DisposedException)
265 if (SlideRendererInterfaceBase::rBHelper.bDisposed || SlideRendererInterfaceBase::rBHelper.bInDispose)
267 throw lang::DisposedException (
268 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
269 "SlideRenderer object has already been disposed")),
270 static_cast<uno::XWeak*>(this));
275 } } // end of namespace ::sd::presenter