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 .
20 #include <com/sun/star/drawing/BitmapMode.hpp>
21 #include <vcl/svapp.hxx>
22 #include <svl/itemset.hxx>
23 #include <svx/svdpool.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <svx/xflbstit.hxx>
26 #include <svx/xflbmtit.hxx>
27 #include <svx/svdobj.hxx>
28 #include <svx/unoshape.hxx>
29 #include <svx/unoshprp.hxx>
31 #include "unopback.hxx"
32 #include <drawdoc.hxx>
33 #include <unokywds.hxx>
35 using namespace ::com::sun::star
;
37 const SvxItemPropertySet
* ImplGetPageBackgroundPropertySet()
39 static const SfxItemPropertyMapEntry aPageBackgroundPropertyMap_Impl
[] =
44 static SvxItemPropertySet
aPageBackgroundPropertySet_Impl( aPageBackgroundPropertyMap_Impl
, SdrObject::GetGlobalDrawObjectItemPool() );
45 return &aPageBackgroundPropertySet_Impl
;
48 SdUnoPageBackground::SdUnoPageBackground(
49 SdDrawDocument
* pDoc
/* = NULL */,
50 const SfxItemSet
* pSet
/* = NULL */)
51 : mpPropSet(ImplGetPageBackgroundPropertySet()),
56 StartListening( *pDoc
);
57 mpSet
= std::make_unique
<SfxItemSetFixed
<XATTR_FILL_FIRST
, XATTR_FILL_LAST
>>( pDoc
->GetPool() );
64 SdUnoPageBackground::~SdUnoPageBackground() noexcept
69 EndListening( *mpDoc
);
72 void SdUnoPageBackground::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
74 if (rHint
.GetId() != SfxHintId::ThisIsAnSdrHint
)
76 const SdrHint
* pSdrHint
= static_cast<const SdrHint
*>( &rHint
);
78 // delete item set if document is dying because then the pool
80 if( pSdrHint
->GetKind() == SdrHintKind::ModelCleared
)
87 void SdUnoPageBackground::fillItemSet( SdDrawDocument
* pDoc
, SfxItemSet
& rSet
)
91 if( mpSet
== nullptr )
93 StartListening( *pDoc
);
96 mpSet
= std::make_unique
<SfxItemSetFixed
<XATTR_FILL_FIRST
, XATTR_FILL_LAST
>>( *rSet
.GetPool() );
98 if( maUsrAnys
.AreThereOwnUsrAnys() )
100 for( const auto & rPair
: mpPropSet
->getPropertyMap().getPropertyEntries() )
102 const SfxItemPropertyMapEntry
* pProp
= rPair
.second
;
103 uno::Any
* pAny
= maUsrAnys
.GetUsrAnyForID( *pProp
);
106 const OUString
& aPropertyName
= pProp
->aName
;
107 switch( pProp
->nWID
)
109 case XATTR_FILLFLOATTRANSPARENCE
:
110 case XATTR_FILLGRADIENT
:
112 if ( ( pAny
->getValueType() == ::cppu::UnoType
< css::awt::Gradient
>::get() )
113 && ( pProp
->nMemberId
== MID_FILLGRADIENT
) )
115 setPropertyValue( aPropertyName
, *pAny
);
117 else if ( ( pAny
->getValueType() == ::cppu::UnoType
<OUString
>::get() ) &&
118 ( pProp
->nMemberId
== MID_NAME
) )
120 setPropertyValue( aPropertyName
, *pAny
);
124 case XATTR_FILLHATCH
:
126 if ( ( pAny
->getValueType() == ::cppu::UnoType
< css::drawing::Hatch
>::get() )
127 && ( pProp
->nMemberId
== MID_FILLHATCH
) )
129 setPropertyValue( aPropertyName
, *pAny
);
131 else if ( ( pAny
->getValueType() == ::cppu::UnoType
<OUString
>::get() ) &&
132 ( pProp
->nMemberId
== MID_NAME
) )
134 setPropertyValue( aPropertyName
, *pAny
);
138 case XATTR_FILLBITMAP
:
140 if (pProp
->nMemberId
== MID_BITMAP
&&
141 (pAny
->getValueTypeClass() == css::uno::TypeClass_INTERFACE
))
143 setPropertyValue( aPropertyName
, *pAny
);
145 else if (pAny
->getValueType() == ::cppu::UnoType
<OUString
>::get() && pProp
->nMemberId
== MID_NAME
)
147 setPropertyValue( aPropertyName
, *pAny
);
153 setPropertyValue( aPropertyName
, *pAny
);
164 OUString SAL_CALL
SdUnoPageBackground::getImplementationName()
166 return u
"SdUnoPageBackground"_ustr
;
169 sal_Bool SAL_CALL
SdUnoPageBackground::supportsService( const OUString
& ServiceName
)
171 return cppu::supportsService( this, ServiceName
);
174 uno::Sequence
< OUString
> SAL_CALL
SdUnoPageBackground::getSupportedServiceNames()
176 return { sUNO_Service_PageBackground
, sUNO_Service_FillProperties
};
180 uno::Reference
< beans::XPropertySetInfo
> SAL_CALL
SdUnoPageBackground::getPropertySetInfo()
182 return mpPropSet
->getPropertySetInfo();
185 void SAL_CALL
SdUnoPageBackground::setPropertyValue( const OUString
& aPropertyName
, const uno::Any
& aValue
)
187 SolarMutexGuard aGuard
;
189 const SfxItemPropertyMapEntry
* pEntry
= getPropertyMapEntry( aPropertyName
);
191 if( pEntry
== nullptr )
193 throw beans::UnknownPropertyException( aPropertyName
, static_cast<cppu::OWeakObject
*>(this));
198 if( pEntry
->nWID
== OWN_ATTR_FILLBMP_MODE
)
200 drawing::BitmapMode eMode
;
201 if( aValue
>>= eMode
)
203 mpSet
->Put( XFillBmpStretchItem( eMode
== drawing::BitmapMode_STRETCH
) );
204 mpSet
->Put( XFillBmpTileItem( eMode
== drawing::BitmapMode_REPEAT
) );
207 throw lang::IllegalArgumentException();
210 SfxItemPool
& rPool
= *mpSet
->GetPool();
211 SfxItemSet
aSet( rPool
, pEntry
->nWID
, pEntry
->nWID
);
215 aSet
.Put( rPool
.GetUserOrPoolDefaultItem( pEntry
->nWID
) );
217 if( pEntry
->nMemberId
== MID_NAME
&& ( pEntry
->nWID
== XATTR_FILLBITMAP
|| pEntry
->nWID
== XATTR_FILLGRADIENT
|| pEntry
->nWID
== XATTR_FILLHATCH
|| pEntry
->nWID
== XATTR_FILLFLOATTRANSPARENCE
) )
220 if(!(aValue
>>= aName
))
221 throw lang::IllegalArgumentException();
223 SvxShape::SetFillAttribute( pEntry
->nWID
, aName
, aSet
);
227 SvxItemPropertySet_setPropertyValue( pEntry
, aValue
, aSet
);
235 SvxItemPropertySet::setPropertyValue( pEntry
, aValue
, maUsrAnys
);
239 uno::Any SAL_CALL
SdUnoPageBackground::getPropertyValue( const OUString
& PropertyName
)
241 SolarMutexGuard aGuard
;
244 const SfxItemPropertyMapEntry
* pEntry
= getPropertyMapEntry(PropertyName
);
246 if( pEntry
== nullptr )
248 throw beans::UnknownPropertyException( PropertyName
, static_cast<cppu::OWeakObject
*>(this));
253 if( pEntry
->nWID
== OWN_ATTR_FILLBMP_MODE
)
255 const XFillBmpStretchItem
* pStretchItem
= mpSet
->GetItem
<XFillBmpStretchItem
>(XATTR_FILLBMP_STRETCH
);
256 const XFillBmpTileItem
* pTileItem
= mpSet
->GetItem
<XFillBmpTileItem
>(XATTR_FILLBMP_TILE
);
258 if( pStretchItem
&& pTileItem
)
260 if( pTileItem
->GetValue() )
261 aAny
<<= drawing::BitmapMode_REPEAT
;
262 else if( pStretchItem
->GetValue() )
263 aAny
<<= drawing::BitmapMode_STRETCH
;
265 aAny
<<= drawing::BitmapMode_NO_REPEAT
;
270 SfxItemPool
& rPool
= *mpSet
->GetPool();
271 SfxItemSet
aSet( rPool
, pEntry
->nWID
, pEntry
->nWID
);
275 aSet
.Put( rPool
.GetUserOrPoolDefaultItem( pEntry
->nWID
) );
277 // get value from ItemSet
278 aAny
= SvxItemPropertySet_getPropertyValue( pEntry
, aSet
);
284 aAny
= mpPropSet
->getPropertyValue( pEntry
, maUsrAnys
);
289 void SAL_CALL
SdUnoPageBackground::addPropertyChangeListener( const OUString
& , const uno::Reference
< beans::XPropertyChangeListener
>& ) {}
290 void SAL_CALL
SdUnoPageBackground::removePropertyChangeListener( const OUString
& , const uno::Reference
< beans::XPropertyChangeListener
>& ) {}
291 void SAL_CALL
SdUnoPageBackground::addVetoableChangeListener( const OUString
& , const uno::Reference
< beans::XVetoableChangeListener
>& ) {}
292 void SAL_CALL
SdUnoPageBackground::removeVetoableChangeListener( const OUString
& , const uno::Reference
< beans::XVetoableChangeListener
>& ) {}
295 beans::PropertyState SAL_CALL
SdUnoPageBackground::getPropertyState( const OUString
& PropertyName
)
297 SolarMutexGuard aGuard
;
299 const SfxItemPropertyMapEntry
* pEntry
= getPropertyMapEntry(PropertyName
);
301 if( pEntry
== nullptr )
302 throw beans::UnknownPropertyException( PropertyName
, static_cast<cppu::OWeakObject
*>(this));
306 if( pEntry
->nWID
== OWN_ATTR_FILLBMP_MODE
)
308 if( mpSet
->GetItemState( XATTR_FILLBMP_STRETCH
, false ) == SfxItemState::SET
||
309 mpSet
->GetItemState( XATTR_FILLBMP_TILE
, false ) == SfxItemState::SET
)
311 return beans::PropertyState_DIRECT_VALUE
;
315 return beans::PropertyState_AMBIGUOUS_VALUE
;
319 switch( mpSet
->GetItemState( pEntry
->nWID
, false ) )
321 case SfxItemState::SET
:
322 return beans::PropertyState_DIRECT_VALUE
;
323 case SfxItemState::DEFAULT
:
324 return beans::PropertyState_DEFAULT_VALUE
;
326 // case SfxItemState::INVALID:
327 // case SfxItemState::DISABLED:
328 return beans::PropertyState_AMBIGUOUS_VALUE
;
333 if( nullptr == maUsrAnys
.GetUsrAnyForID(*pEntry
) )
334 return beans::PropertyState_DEFAULT_VALUE
;
336 return beans::PropertyState_DIRECT_VALUE
;
340 uno::Sequence
< beans::PropertyState
> SAL_CALL
SdUnoPageBackground::getPropertyStates( const uno::Sequence
< OUString
>& aPropertyName
)
342 SolarMutexGuard aGuard
;
344 sal_Int32 nCount
= aPropertyName
.getLength();
346 uno::Sequence
< beans::PropertyState
> aPropertyStateSequence( nCount
);
348 std::transform(aPropertyName
.begin(), aPropertyName
.end(), aPropertyStateSequence
.getArray(),
349 [this](const OUString
& rName
) -> beans::PropertyState
{ return getPropertyState(rName
); });
351 return aPropertyStateSequence
;
354 void SAL_CALL
SdUnoPageBackground::setPropertyToDefault( const OUString
& PropertyName
)
356 SolarMutexGuard aGuard
;
358 const SfxItemPropertyMapEntry
* pEntry
= getPropertyMapEntry(PropertyName
);
360 if( pEntry
== nullptr )
361 throw beans::UnknownPropertyException( PropertyName
, static_cast<cppu::OWeakObject
*>(this));
365 if( pEntry
->nWID
== OWN_ATTR_FILLBMP_MODE
)
367 mpSet
->ClearItem( XATTR_FILLBMP_STRETCH
);
368 mpSet
->ClearItem( XATTR_FILLBMP_TILE
);
372 mpSet
->ClearItem( pEntry
->nWID
);
377 uno::Any SAL_CALL
SdUnoPageBackground::getPropertyDefault( const OUString
& aPropertyName
)
379 SolarMutexGuard aGuard
;
381 const SfxItemPropertyMapEntry
* pEntry
= getPropertyMapEntry(aPropertyName
);
382 if( pEntry
== nullptr || mpSet
== nullptr )
383 throw beans::UnknownPropertyException( aPropertyName
, static_cast<cppu::OWeakObject
*>(this));
386 if (pEntry
->nWID
== OWN_ATTR_FILLBMP_MODE
)
388 aAny
<<= drawing::BitmapMode_REPEAT
;
392 SfxItemPool
& rPool
= *mpSet
->GetPool();
393 SfxItemSet
aSet(rPool
, pEntry
->nWID
, pEntry
->nWID
);
394 aSet
.Put(rPool
.GetUserOrPoolDefaultItem(pEntry
->nWID
));
396 aAny
= SvxItemPropertySet_getPropertyValue(pEntry
, aSet
);
401 /** this is used because our property map is not sorted yet */
402 const SfxItemPropertyMapEntry
* SdUnoPageBackground::getPropertyMapEntry( const OUString
& rPropertyName
) const noexcept
404 return mpPropSet
->getPropertyMap().getByName(rPropertyName
);
407 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */