merge the formfield patch from ooo-build
[ooovba.git] / slideshow / source / engine / shapes / viewbackgroundshape.cxx
blobfdc67c33d72cc348220249d7932dc6f032d17b37
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: viewbackgroundshape.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
34 // must be first
35 #include <canvas/debug.hxx>
36 #include <tools/diagnose_ex.h>
38 #include "viewbackgroundshape.hxx"
39 #include "tools.hxx"
41 #include <rtl/logfile.hxx>
42 #include <rtl/math.hxx>
44 #include <comphelper/anytostring.hxx>
45 #include <cppuhelper/exc_hlp.hxx>
47 #include <basegfx/polygon/b2dpolygontools.hxx>
48 #include <basegfx/polygon/b2dpolygon.hxx>
49 #include <basegfx/numeric/ftools.hxx>
50 #include <basegfx/matrix/b2dhommatrix.hxx>
52 #include <com/sun/star/rendering/XCanvas.hpp>
54 #include <canvas/verbosetrace.hxx>
55 #include <canvas/canvastools.hxx>
56 #include <cppcanvas/vclfactory.hxx>
57 #include <cppcanvas/basegfxfactory.hxx>
58 #include <cppcanvas/renderer.hxx>
59 #include <cppcanvas/bitmap.hxx>
61 using namespace ::com::sun::star;
64 namespace slideshow
66 namespace internal
69 bool ViewBackgroundShape::prefetch( const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas,
70 const GDIMetaFileSharedPtr& rMtf ) const
72 RTL_LOGFILE_CONTEXT( aLog, "::presentation::internal::ViewBackgroundShape::prefetch()" );
73 ENSURE_OR_RETURN( rMtf,
74 "ViewBackgroundShape::prefetch(): no valid metafile!" );
76 const ::basegfx::B2DHomMatrix& rCanvasTransform(
77 mpViewLayer->getTransformation() );
79 if( !mxBitmap.is() ||
80 rMtf != mpLastMtf ||
81 rCanvasTransform != maLastTransformation )
83 // buffered bitmap is invalid, re-create
85 // determine transformed page bounds
86 ::basegfx::B2DRectangle aTmpRect;
87 ::canvas::tools::calcTransformedRectBounds( aTmpRect,
88 maBounds,
89 rCanvasTransform );
91 // determine pixel size of bitmap (choose it one pixel
92 // larger, as polygon rendering takes one pixel more
93 // to the right and to the bottom)
94 const ::basegfx::B2ISize aBmpSizePixel(
95 ::basegfx::fround( aTmpRect.getRange().getX() + 1),
96 ::basegfx::fround( aTmpRect.getRange().getY() + 1) );
98 // create a bitmap of appropriate size
99 ::cppcanvas::BitmapSharedPtr pBitmap(
100 ::cppcanvas::BaseGfxFactory::getInstance().createBitmap(
101 rDestinationCanvas,
102 aBmpSizePixel ) );
104 ENSURE_OR_THROW( pBitmap,
105 "ViewBackgroundShape::prefetch(): Cannot create background bitmap" );
107 ::cppcanvas::BitmapCanvasSharedPtr pBitmapCanvas( pBitmap->getBitmapCanvas() );
109 ENSURE_OR_THROW( pBitmapCanvas,
110 "ViewBackgroundShape::prefetch(): Cannot create background bitmap canvas" );
112 // clear bitmap
113 initSlideBackground( pBitmapCanvas,
114 aBmpSizePixel );
116 // apply linear part of destination canvas transformation (linear means in this context:
117 // transformation without any translational components)
118 ::basegfx::B2DHomMatrix aLinearTransform( rCanvasTransform );
119 aLinearTransform.set( 0, 2, 0.0 );
120 aLinearTransform.set( 1, 2, 0.0 );
121 pBitmapCanvas->setTransformation( aLinearTransform );
123 ::basegfx::B2DHomMatrix aShapeTransform;
125 aShapeTransform.scale( maBounds.getWidth(), maBounds.getHeight() );
126 aShapeTransform.translate( maBounds.getMinX(), maBounds.getMinY() );
128 ::cppcanvas::RendererSharedPtr pRenderer(
129 ::cppcanvas::VCLFactory::getInstance().createRenderer(
130 pBitmapCanvas,
131 *rMtf.get(),
132 ::cppcanvas::Renderer::Parameters() ) );
134 ENSURE_OR_RETURN( pRenderer,
135 "ViewBackgroundShape::prefetch(): Could not create Renderer" );
137 pRenderer->setTransformation( aShapeTransform );
138 pRenderer->draw();
140 mxBitmap = pBitmap->getUNOBitmap();
143 mpLastMtf = rMtf;
144 maLastTransformation = rCanvasTransform;
146 return mxBitmap.is();
149 ViewBackgroundShape::ViewBackgroundShape( const ViewLayerSharedPtr& rViewLayer,
150 const ::basegfx::B2DRectangle& rShapeBounds ) :
151 mpViewLayer( rViewLayer ),
152 mxBitmap(),
153 mpLastMtf(),
154 maLastTransformation(),
155 maBounds( rShapeBounds )
157 ENSURE_OR_THROW( mpViewLayer, "ViewBackgroundShape::ViewBackgroundShape(): Invalid View" );
158 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewBackgroundShape::ViewBackgroundShape(): Invalid ViewLayer canvas" );
161 ViewLayerSharedPtr ViewBackgroundShape::getViewLayer() const
163 return mpViewLayer;
166 bool ViewBackgroundShape::render( const GDIMetaFileSharedPtr& rMtf ) const
168 RTL_LOGFILE_CONTEXT( aLog, "::presentation::internal::ViewBackgroundShape::draw()" );
170 const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas( mpViewLayer->getCanvas() );
172 if( !prefetch( rDestinationCanvas, rMtf ) )
173 return false;
175 ENSURE_OR_RETURN( mxBitmap.is(),
176 "ViewBackgroundShape::draw(): Invalid background bitmap" );
178 ::basegfx::B2DHomMatrix aTransform( mpViewLayer->getTransformation() );
180 // invert the linear part of the view transformation
181 // (i.e. the view transformation without translational
182 // components), to be able to leave the canvas
183 // transformation intact (would otherwise destroy possible
184 // clippings, as the clip polygon is relative to the view
185 // coordinate system).
186 aTransform.set(0,2, 0.0 );
187 aTransform.set(1,2, 0.0 );
188 aTransform.invert();
190 rendering::RenderState aRenderState;
191 ::canvas::tools::initRenderState( aRenderState );
193 ::canvas::tools::setRenderStateTransform( aRenderState, aTransform );
197 rDestinationCanvas->getUNOCanvas()->drawBitmap( mxBitmap,
198 rDestinationCanvas->getViewState(),
199 aRenderState );
201 catch( uno::Exception& )
203 OSL_ENSURE( false,
204 rtl::OUStringToOString(
205 comphelper::anyToString( cppu::getCaughtException() ),
206 RTL_TEXTENCODING_UTF8 ).getStr() );
208 return false;
211 return true;