update dev300-m58
[ooovba.git] / slideshow / source / engine / shapes / backgroundshape.cxx
blobebd29248e03c488e8d3ff93790c7cb3684af6794
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: backgroundshape.cxx,v $
10 * $Revision: 1.3 $
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>
37 #include <com/sun/star/awt/Rectangle.hpp>
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/awt/FontWeight.hpp>
40 #include <rtl/logfile.hxx>
42 #include <vcl/metaact.hxx>
43 #include <vcl/gdimtf.hxx>
45 #include <basegfx/numeric/ftools.hxx>
47 #include <boost/bind.hpp>
49 #include <cmath> // for trigonometry and fabs
50 #include <algorithm>
51 #include <functional>
52 #include <limits>
54 #include "backgroundshape.hxx"
55 #include "slideshowexceptions.hxx"
56 #include "slideshowcontext.hxx"
57 #include "gdimtftools.hxx"
58 #include "shape.hxx"
59 #include "viewbackgroundshape.hxx"
62 using namespace ::com::sun::star;
65 namespace slideshow
67 namespace internal
69 /** Representation of a draw document's background shape.
71 This class implements the Shape interface for the
72 background shape. Since the background shape is neither
73 animatable nor attributable, those more specialized
74 derivations of the Shape interface are not implemented
75 here.
77 @attention this class is to be treated 'final', i.e. one
78 should not derive from it.
80 class BackgroundShape : public Shape
82 public:
83 /** Create the background shape.
85 This method creates a shape that handles the
86 peculiarities of the draw API regarding background
87 content.
89 BackgroundShape( const ::com::sun::star::uno::Reference<
90 ::com::sun::star::drawing::XDrawPage >& xDrawPage,
91 const ::com::sun::star::uno::Reference<
92 ::com::sun::star::drawing::XDrawPage >& xMasterPage,
93 const SlideShowContext& rContext ); // throw ShapeLoadFailedException;
95 virtual ::com::sun::star::uno::Reference<
96 ::com::sun::star::drawing::XShape > getXShape() const;
98 // View layer methods
99 //------------------------------------------------------------------
101 virtual void addViewLayer( const ViewLayerSharedPtr& rNewLayer,
102 bool bRedrawLayer );
103 virtual bool removeViewLayer( const ViewLayerSharedPtr& rNewLayer );
104 virtual bool clearAllViewLayers();
107 // attribute methods
108 //------------------------------------------------------------------
110 virtual ::basegfx::B2DRectangle getBounds() const;
111 virtual ::basegfx::B2DRectangle getDomBounds() const;
112 virtual ::basegfx::B2DRectangle getUpdateArea() const;
113 virtual bool isVisible() const;
114 virtual double getPriority() const;
115 virtual bool isBackgroundDetached() const;
118 // render methods
119 //------------------------------------------------------------------
121 virtual bool update() const;
122 virtual bool render() const;
123 virtual bool isContentChanged() const;
125 private:
126 /// The metafile actually representing the Shape
127 GDIMetaFileSharedPtr mpMtf;
129 // The attributes of this Shape
130 ::basegfx::B2DRectangle maBounds; // always needed for rendering
132 /// the list of active view shapes (one for each registered view layer)
133 typedef ::std::vector< ViewBackgroundShapeSharedPtr > ViewBackgroundShapeVector;
134 ViewBackgroundShapeVector maViewShapes;
138 ////////////////////////////////////////////////////////////////////////////////
141 BackgroundShape::BackgroundShape( const uno::Reference< drawing::XDrawPage >& xDrawPage,
142 const uno::Reference< drawing::XDrawPage >& xMasterPage,
143 const SlideShowContext& rContext ) :
144 mpMtf(),
145 maBounds(),
146 maViewShapes()
148 uno::Reference< beans::XPropertySet > xPropSet( xDrawPage,
149 uno::UNO_QUERY_THROW );
150 GDIMetaFileSharedPtr pMtf( new GDIMetaFile() );
152 // first try the page background (overrides
153 // masterpage background), then try masterpage
154 if( !getMetaFile( uno::Reference<lang::XComponent>(xDrawPage, uno::UNO_QUERY),
155 xDrawPage, *pMtf, MTF_LOAD_BACKGROUND_ONLY,
156 rContext.mxComponentContext ) &&
157 !getMetaFile( uno::Reference<lang::XComponent>(xMasterPage, uno::UNO_QUERY),
158 xDrawPage, *pMtf, MTF_LOAD_BACKGROUND_ONLY,
159 rContext.mxComponentContext ))
161 throw ShapeLoadFailedException();
164 // there is a special background shape, add it
165 // as the first one
166 // ---------------------------------------------------
168 sal_Int32 nDocWidth=0;
169 sal_Int32 nDocHeight=0;
170 xPropSet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Width") ) ) >>= nDocWidth;
171 xPropSet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Height") ) ) >>= nDocHeight;
173 mpMtf = pMtf;
174 maBounds = ::basegfx::B2DRectangle( 0,0,nDocWidth, nDocHeight );
177 uno::Reference< drawing::XShape > BackgroundShape::getXShape() const
179 // no real XShape representative
180 return uno::Reference< drawing::XShape >();
183 void BackgroundShape::addViewLayer( const ViewLayerSharedPtr& rNewLayer,
184 bool bRedrawLayer )
186 ViewBackgroundShapeVector::iterator aEnd( maViewShapes.end() );
188 // already added?
189 if( ::std::find_if( maViewShapes.begin(),
190 aEnd,
191 ::boost::bind<bool>(
192 ::std::equal_to< ViewLayerSharedPtr >(),
193 ::boost::bind( &ViewBackgroundShape::getViewLayer,
194 _1 ),
195 ::boost::cref( rNewLayer ) ) ) != aEnd )
197 // yes, nothing to do
198 return;
201 maViewShapes.push_back(
202 ViewBackgroundShapeSharedPtr(
203 new ViewBackgroundShape( rNewLayer,
204 maBounds ) ) );
206 // render the Shape on the newly added ViewLayer
207 if( bRedrawLayer )
208 maViewShapes.back()->render( mpMtf );
211 bool BackgroundShape::removeViewLayer( const ViewLayerSharedPtr& rLayer )
213 const ViewBackgroundShapeVector::iterator aEnd( maViewShapes.end() );
215 OSL_ENSURE( ::std::count_if(maViewShapes.begin(),
216 aEnd,
217 ::boost::bind<bool>(
218 ::std::equal_to< ViewLayerSharedPtr >(),
219 ::boost::bind( &ViewBackgroundShape::getViewLayer,
220 _1 ),
221 ::boost::cref( rLayer ) ) ) < 2,
222 "BackgroundShape::removeViewLayer(): Duplicate ViewLayer entries!" );
224 ViewBackgroundShapeVector::iterator aIter;
226 if( (aIter=::std::remove_if( maViewShapes.begin(),
227 aEnd,
228 ::boost::bind<bool>(
229 ::std::equal_to< ViewLayerSharedPtr >(),
230 ::boost::bind( &ViewBackgroundShape::getViewLayer,
231 _1 ),
232 ::boost::cref( rLayer ) ) )) == aEnd )
234 // view layer seemingly was not added, failed
235 return false;
238 // actually erase from container
239 maViewShapes.erase( aIter, aEnd );
241 return true;
244 bool BackgroundShape::clearAllViewLayers()
246 maViewShapes.clear();
247 return true;
250 ::basegfx::B2DRectangle BackgroundShape::getBounds() const
252 return maBounds;
255 ::basegfx::B2DRectangle BackgroundShape::getDomBounds() const
257 return maBounds;
260 ::basegfx::B2DRectangle BackgroundShape::getUpdateArea() const
262 // TODO(F1): Need to expand background, too, when
263 // antialiasing?
265 // no transformation etc. possible for background shape
266 return maBounds;
269 bool BackgroundShape::isVisible() const
271 return true;
274 double BackgroundShape::getPriority() const
276 return 0.0; // lowest prio, we're the background
279 bool BackgroundShape::update() const
281 return render();
284 bool BackgroundShape::render() const
286 RTL_LOGFILE_CONTEXT( aLog, "::presentation::internal::BackgroundShape::render()" );
287 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "::presentation::internal::BackgroundShape: 0x%X", this );
289 // gcc again...
290 const ::basegfx::B2DRectangle& rCurrBounds( BackgroundShape::getBounds() );
292 if( rCurrBounds.getRange().equalZero() )
294 // zero-sized shapes are effectively invisible,
295 // thus, we save us the rendering...
296 return true;
299 // redraw all view shapes, by calling their render() method
300 if( ::std::count_if( maViewShapes.begin(),
301 maViewShapes.end(),
302 ::boost::bind( &ViewBackgroundShape::render,
304 ::boost::cref( mpMtf ) ) )
305 != static_cast<ViewBackgroundShapeVector::difference_type>(maViewShapes.size()) )
307 // at least one of the ViewBackgroundShape::render() calls did return
308 // false - update failed on at least one ViewLayer
309 return false;
312 return true;
315 bool BackgroundShape::isContentChanged() const
317 return false;
320 bool BackgroundShape::isBackgroundDetached() const
322 return false; // we're not animatable
325 //////////////////////////////////////////////////////////
327 ShapeSharedPtr createBackgroundShape(
328 const uno::Reference< drawing::XDrawPage >& xDrawPage,
329 const uno::Reference< drawing::XDrawPage >& xMasterPage,
330 const SlideShowContext& rContext )
332 return ShapeSharedPtr(
333 new BackgroundShape(
334 xDrawPage,
335 xMasterPage,
336 rContext ));