1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 #include "backgroundshape.hxx"
30 #include <slideshowexceptions.hxx>
31 #include <slideshowcontext.hxx>
32 #include "gdimtftools.hxx"
34 #include "viewbackgroundshape.hxx"
37 using namespace ::com::sun::star
;
40 namespace slideshow::internal
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
52 @attention this class is to be treated 'final', i.e. one
53 should not derive from it.
55 class BackgroundShape
: public Shape
58 /** Create the background shape.
60 This method creates a shape that handles the
61 peculiarities of the draw API regarding background
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
;
74 virtual void addViewLayer( const ViewLayerSharedPtr
& rNewLayer
,
75 bool bRedrawLayer
) override
;
76 virtual bool removeViewLayer( const ViewLayerSharedPtr
& rNewLayer
) override
;
77 virtual void clearAllViewLayers() override
;
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
;
95 virtual bool update() const override
;
96 virtual bool render() const override
;
97 virtual bool isContentChanged() const override
;
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
) :
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
);
130 xMtf
= getMetaFile( uno::Reference
<lang::XComponent
>(xMasterPage
, uno::UNO_QUERY
),
131 xDrawPage
, MTF_LOAD_BACKGROUND_ONLY
,
132 rContext
.mxComponentContext
);
137 throw ShapeLoadFailedException();
140 // there is a special background shape, add it
143 sal_Int32 nDocWidth
=0;
144 sal_Int32 nDocHeight
=0;
145 xPropSet
->getPropertyValue(u
"Width"_ustr
) >>= nDocWidth
;
146 xPropSet
->getPropertyValue(u
"Height"_ustr
) >>= nDocHeight
;
148 mpMtf
= std::move(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
,
162 if( ::std::any_of( maViewShapes
.begin(),
164 [&rNewLayer
]( const ViewBackgroundShapeSharedPtr
& pBgShape
)
165 { return pBgShape
->getViewLayer() == rNewLayer
; } ) )
167 // yes, nothing to do
171 maViewShapes
.push_back(
172 std::make_shared
<ViewBackgroundShape
>(
173 rNewLayer
, maBounds
) );
175 // render the Shape on the newly added ViewLayer
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(),
186 [&rLayer
]( const ViewBackgroundShapeSharedPtr
& pBgShape
)
187 { return pBgShape
->getViewLayer() == rLayer
; } ) < 2,
188 "BackgroundShape::removeViewLayer(): Duplicate ViewLayer entries!" );
190 // TODO : needed for the moment since ANDROID doesn't know size_t return from std::erase_if
192 ViewBackgroundShapeVector::iterator aIter
;
194 if( (aIter
=::std::remove_if( maViewShapes
.begin(),
196 [&rLayer
]( const ViewBackgroundShapeSharedPtr
& pBgShape
)
197 { return pBgShape
->getViewLayer() == rLayer
; } )) == aEnd
)
199 // view layer seemingly was not added, failed
203 // actually erase from container
204 maViewShapes
.erase( aIter
, aEnd
);
209 size_t nb
= std::erase_if(maViewShapes
,
210 [&rLayer
]( const ViewBackgroundShapeSharedPtr
& pBgShape
)
211 { return pBgShape
->getViewLayer() == rLayer
; } );
212 // if nb == 0, it means view shape seemingly was not added, failed
217 void BackgroundShape::clearAllViewLayers()
219 maViewShapes
.clear();
222 ::basegfx::B2DRectangle
BackgroundShape::getBounds() const
227 ::basegfx::B2DRectangle
BackgroundShape::getDomBounds() const
232 ::basegfx::B2DRectangle
BackgroundShape::getUpdateArea() const
234 // TODO(F1): Need to expand background, too, when
237 // no transformation etc. possible for background shape
241 bool BackgroundShape::isVisible() const
246 double BackgroundShape::getPriority() const
248 return 0.0; // lowest prio, we're the background
251 bool BackgroundShape::update() const
256 bool BackgroundShape::render() const
258 SAL_INFO( "slideshow", "::presentation::internal::BackgroundShape::render()" );
259 SAL_INFO( "slideshow", "::presentation::internal::BackgroundShape: 0x" << std::hex
<< this );
262 const ::basegfx::B2DRectangle
aCurrBounds( BackgroundShape::getBounds() );
264 if( aCurrBounds
.getRange().equalZero() )
266 // zero-sized shapes are effectively invisible,
267 // thus, we save us the rendering...
271 // redraw all view shapes, by calling their render() method
272 if( o3tl::make_unsigned(::std::count_if( maViewShapes
.begin(),
274 [this]( const ViewBackgroundShapeSharedPtr
& pBgShape
)
275 { return pBgShape
->render( this->mpMtf
); } ))
276 != maViewShapes
.size() )
278 // at least one of the ViewBackgroundShape::render() calls did return
279 // false - update failed on at least one ViewLayer
286 bool BackgroundShape::isContentChanged() const
291 bool BackgroundShape::isBackgroundDetached() const
293 return false; // we're not animatable
297 ShapeSharedPtr
createBackgroundShape(
298 const uno::Reference
< drawing::XDrawPage
>& xDrawPage
,
299 const uno::Reference
< drawing::XDrawPage
>& xMasterPage
,
300 const SlideShowContext
& rContext
)
302 return std::make_shared
<BackgroundShape
>(
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */