android: Update app-specific/MIME type icons
[LibreOffice.git] / slideshow / source / engine / shapes / backgroundshape.cxx
blobd304b9f901e6258892c374276c39eae925cf156d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <sal/log.hxx>
24 #include <o3tl/safeint.hxx>
25 #include <osl/diagnose.h>
27 #include <algorithm>
29 #include "backgroundshape.hxx"
30 #include <slideshowexceptions.hxx>
31 #include <slideshowcontext.hxx>
32 #include "gdimtftools.hxx"
33 #include <shape.hxx>
34 #include "viewbackgroundshape.hxx"
37 using namespace ::com::sun::star;
40 namespace slideshow::internal
42 namespace {
44 /** Representation of a draw document's background shape.
46 This class implements the Shape interface for the
47 background shape. Since the background shape is neither
48 animatable nor attributable, those more specialized
49 derivations of the Shape interface are not implemented
50 here.
52 @attention this class is to be treated 'final', i.e. one
53 should not derive from it.
55 class BackgroundShape : public Shape
57 public:
58 /** Create the background shape.
60 This method creates a shape that handles the
61 peculiarities of the draw API regarding background
62 content.
64 BackgroundShape( const css::uno::Reference< css::drawing::XDrawPage >& xDrawPage,
65 const css::uno::Reference< css::drawing::XDrawPage >& xMasterPage,
66 const SlideShowContext& rContext ); // throw ShapeLoadFailedException;
68 virtual css::uno::Reference<
69 css::drawing::XShape > getXShape() const override;
71 // View layer methods
74 virtual void addViewLayer( const ViewLayerSharedPtr& rNewLayer,
75 bool bRedrawLayer ) override;
76 virtual bool removeViewLayer( const ViewLayerSharedPtr& rNewLayer ) override;
77 virtual void clearAllViewLayers() override;
80 // attribute methods
83 virtual ::basegfx::B2DRectangle getBounds() const override;
84 virtual ::basegfx::B2DRectangle getDomBounds() const override;
85 virtual ::basegfx::B2DRectangle getUpdateArea() const override;
86 virtual bool isVisible() const override;
87 virtual double getPriority() const override;
88 virtual bool isForeground() const override { return false; }
89 virtual bool isBackgroundDetached() const override;
92 // render methods
95 virtual bool update() const override;
96 virtual bool render() const override;
97 virtual bool isContentChanged() const override;
99 private:
100 /// The metafile actually representing the Shape
101 GDIMetaFileSharedPtr mpMtf;
103 // The attributes of this Shape
104 ::basegfx::B2DRectangle maBounds; // always needed for rendering
106 /// the list of active view shapes (one for each registered view layer)
107 typedef ::std::vector< ViewBackgroundShapeSharedPtr > ViewBackgroundShapeVector;
108 ViewBackgroundShapeVector maViewShapes;
113 BackgroundShape::BackgroundShape( const uno::Reference< drawing::XDrawPage >& xDrawPage,
114 const uno::Reference< drawing::XDrawPage >& xMasterPage,
115 const SlideShowContext& rContext ) :
116 mpMtf(),
117 maBounds(),
118 maViewShapes()
120 uno::Reference< beans::XPropertySet > xPropSet( xDrawPage,
121 uno::UNO_QUERY_THROW );
122 // first try the page background (overrides
123 // masterpage background), then try masterpage
124 GDIMetaFileSharedPtr xMtf = getMetaFile(uno::Reference<lang::XComponent>(xDrawPage, uno::UNO_QUERY),
125 xDrawPage, MTF_LOAD_BACKGROUND_ONLY,
126 rContext.mxComponentContext);
128 if (!xMtf)
130 xMtf = getMetaFile( uno::Reference<lang::XComponent>(xMasterPage, uno::UNO_QUERY),
131 xDrawPage, MTF_LOAD_BACKGROUND_ONLY,
132 rContext.mxComponentContext );
135 if (!xMtf)
137 throw ShapeLoadFailedException();
140 // there is a special background shape, add it
141 // as the first one
143 sal_Int32 nDocWidth=0;
144 sal_Int32 nDocHeight=0;
145 xPropSet->getPropertyValue("Width") >>= nDocWidth;
146 xPropSet->getPropertyValue("Height") >>= nDocHeight;
148 mpMtf = xMtf;
149 maBounds = ::basegfx::B2DRectangle( 0,0,nDocWidth, nDocHeight );
152 uno::Reference< drawing::XShape > BackgroundShape::getXShape() const
154 // no real XShape representative
155 return uno::Reference< drawing::XShape >();
158 void BackgroundShape::addViewLayer( const ViewLayerSharedPtr& rNewLayer,
159 bool bRedrawLayer )
161 // already added?
162 if( ::std::any_of( maViewShapes.begin(),
163 maViewShapes.end(),
164 [&rNewLayer]( const ViewBackgroundShapeSharedPtr& pBgShape )
165 { return pBgShape->getViewLayer() == rNewLayer; } ) )
167 // yes, nothing to do
168 return;
171 maViewShapes.push_back(
172 std::make_shared<ViewBackgroundShape>(
173 rNewLayer, maBounds ) );
175 // render the Shape on the newly added ViewLayer
176 if( bRedrawLayer )
177 maViewShapes.back()->render( mpMtf );
180 bool BackgroundShape::removeViewLayer( const ViewLayerSharedPtr& rLayer )
182 const ViewBackgroundShapeVector::iterator aEnd( maViewShapes.end() );
184 OSL_ENSURE( ::std::count_if(maViewShapes.begin(),
185 aEnd,
186 [&rLayer]( const ViewBackgroundShapeSharedPtr& pBgShape )
187 { return pBgShape->getViewLayer() == rLayer; } ) < 2,
188 "BackgroundShape::removeViewLayer(): Duplicate ViewLayer entries!" );
190 ViewBackgroundShapeVector::iterator aIter;
192 if( (aIter=::std::remove_if( maViewShapes.begin(),
193 aEnd,
194 [&rLayer]( const ViewBackgroundShapeSharedPtr& pBgShape )
195 { return pBgShape->getViewLayer() == rLayer; } )) == aEnd )
197 // view layer seemingly was not added, failed
198 return false;
201 // actually erase from container
202 maViewShapes.erase( aIter, aEnd );
204 return true;
207 void BackgroundShape::clearAllViewLayers()
209 maViewShapes.clear();
212 ::basegfx::B2DRectangle BackgroundShape::getBounds() const
214 return maBounds;
217 ::basegfx::B2DRectangle BackgroundShape::getDomBounds() const
219 return maBounds;
222 ::basegfx::B2DRectangle BackgroundShape::getUpdateArea() const
224 // TODO(F1): Need to expand background, too, when
225 // antialiasing?
227 // no transformation etc. possible for background shape
228 return maBounds;
231 bool BackgroundShape::isVisible() const
233 return true;
236 double BackgroundShape::getPriority() const
238 return 0.0; // lowest prio, we're the background
241 bool BackgroundShape::update() const
243 return render();
246 bool BackgroundShape::render() const
248 SAL_INFO( "slideshow", "::presentation::internal::BackgroundShape::render()" );
249 SAL_INFO( "slideshow", "::presentation::internal::BackgroundShape: 0x" << std::hex << this );
251 // gcc again...
252 const ::basegfx::B2DRectangle& rCurrBounds( BackgroundShape::getBounds() );
254 if( rCurrBounds.getRange().equalZero() )
256 // zero-sized shapes are effectively invisible,
257 // thus, we save us the rendering...
258 return true;
261 // redraw all view shapes, by calling their render() method
262 if( o3tl::make_unsigned(::std::count_if( maViewShapes.begin(),
263 maViewShapes.end(),
264 [this]( const ViewBackgroundShapeSharedPtr& pBgShape )
265 { return pBgShape->render( this->mpMtf ); } ))
266 != maViewShapes.size() )
268 // at least one of the ViewBackgroundShape::render() calls did return
269 // false - update failed on at least one ViewLayer
270 return false;
273 return true;
276 bool BackgroundShape::isContentChanged() const
278 return false;
281 bool BackgroundShape::isBackgroundDetached() const
283 return false; // we're not animatable
287 ShapeSharedPtr createBackgroundShape(
288 const uno::Reference< drawing::XDrawPage >& xDrawPage,
289 const uno::Reference< drawing::XDrawPage >& xMasterPage,
290 const SlideShowContext& rContext )
292 return std::make_shared<BackgroundShape>(
293 xDrawPage,
294 xMasterPage,
295 rContext );
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */