Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / unodraw / unopool.cxx
blobec293ac1bf55ea54993a5772c0478dbdd74ae782
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 .
20 #include <com/sun/star/lang/XServiceInfo.hpp>
21 #include <com/sun/star/beans/PropertyState.hpp>
23 #include <comphelper/propertysetinfo.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <vcl/svapp.hxx>
26 #include <svx/unopool.hxx>
27 #include <svx/svdmodel.hxx>
28 #include <svx/svdpool.hxx>
29 #include <svx/unoprov.hxx>
30 #include <svx/unoshprp.hxx>
31 #include <svx/xflbstit.hxx>
32 #include <svx/xflbmtit.hxx>
33 #include <svx/svdetc.hxx>
34 #include <editeng/editeng.hxx>
35 #include <tools/debug.hxx>
37 #include <memory>
39 using namespace ::com::sun::star;
40 using namespace ::cppu;
42 SvxUnoDrawPool::SvxUnoDrawPool(SdrModel* pModel, sal_Int32 nServiceId)
43 : PropertySetHelper( SvxPropertySetInfoPool::getOrCreate( nServiceId ) ), mpModel( pModel )
45 init();
48 /* deprecated */
49 SvxUnoDrawPool::SvxUnoDrawPool(SdrModel* pModel)
50 : PropertySetHelper( SvxPropertySetInfoPool::getOrCreate( SVXUNO_SERVICEID_COM_SUN_STAR_DRAWING_DEFAULTS ) ), mpModel( pModel )
52 init();
55 SvxUnoDrawPool::~SvxUnoDrawPool() throw()
57 if (mpDefaultsPool)
59 SfxItemPool* pOutlPool = mpDefaultsPool->GetSecondaryPool();
60 SfxItemPool::Free(mpDefaultsPool);
61 SfxItemPool::Free(pOutlPool);
65 void SvxUnoDrawPool::init()
67 mpDefaultsPool = new SdrItemPool();
68 SfxItemPool* pOutlPool=EditEngine::CreatePool();
69 mpDefaultsPool->SetSecondaryPool(pOutlPool);
71 SdrModel::SetTextDefaults( mpDefaultsPool, SdrEngineDefaults::GetFontHeight() );
72 mpDefaultsPool->SetDefaultMetric(SdrEngineDefaults::GetMapUnit());
73 mpDefaultsPool->FreezeIdRanges();
76 SfxItemPool* SvxUnoDrawPool::getModelPool( bool bReadOnly ) throw()
78 if( mpModel )
80 return &mpModel->GetItemPool();
82 else
84 if( bReadOnly )
85 return mpDefaultsPool;
86 else
87 return nullptr;
91 void SvxUnoDrawPool::getAny( SfxItemPool const * pPool, const comphelper::PropertyMapEntry* pEntry, uno::Any& rValue )
93 switch( pEntry->mnHandle )
95 case OWN_ATTR_FILLBMP_MODE:
97 if (pPool->GetDefaultItem(XATTR_FILLBMP_TILE).GetValue())
99 rValue <<= drawing::BitmapMode_REPEAT;
101 else if (pPool->GetDefaultItem(XATTR_FILLBMP_STRETCH).GetValue())
103 rValue <<= drawing::BitmapMode_STRETCH;
105 else
107 rValue <<= drawing::BitmapMode_NO_REPEAT;
109 break;
111 default:
113 const MapUnit eMapUnit = pPool->GetMetric(static_cast<sal_uInt16>(pEntry->mnHandle));
115 sal_uInt8 nMemberId = pEntry->mnMemberId;
116 if( eMapUnit == MapUnit::Map100thMM )
117 nMemberId &= (~CONVERT_TWIPS);
119 // Assure, that ID is a Which-ID (it could be a Slot-ID.)
120 // Thus, convert handle to Which-ID.
121 pPool->GetDefaultItem( pPool->GetWhich( static_cast<sal_uInt16>(pEntry->mnHandle) ) ).QueryValue( rValue, nMemberId );
126 // check for needed metric translation
127 const MapUnit eMapUnit = pPool->GetMetric(static_cast<sal_uInt16>(pEntry->mnHandle));
128 if(pEntry->mnMoreFlags & PropertyMoreFlags::METRIC_ITEM && eMapUnit != MapUnit::Map100thMM)
130 SvxUnoConvertToMM( eMapUnit, rValue );
132 // convert int32 to correct enum type if needed
133 else if ( pEntry->maType.getTypeClass() == uno::TypeClass_ENUM && rValue.getValueType() == ::cppu::UnoType<sal_Int32>::get() )
135 sal_Int32 nEnum;
136 rValue >>= nEnum;
138 rValue.setValue( &nEnum, pEntry->maType );
142 void SvxUnoDrawPool::putAny( SfxItemPool* pPool, const comphelper::PropertyMapEntry* pEntry, const uno::Any& rValue )
144 uno::Any aValue( rValue );
146 const MapUnit eMapUnit = pPool->GetMetric(static_cast<sal_uInt16>(pEntry->mnHandle));
147 if(pEntry->mnMoreFlags & PropertyMoreFlags::METRIC_ITEM && eMapUnit != MapUnit::Map100thMM)
149 SvxUnoConvertFromMM( eMapUnit, aValue );
152 // Assure, that ID is a Which-ID (it could be a Slot-ID.)
153 // Thus, convert handle to Which-ID.
154 const sal_uInt16 nWhich = pPool->GetWhich( static_cast<sal_uInt16>(pEntry->mnHandle) );
155 switch( nWhich )
157 case OWN_ATTR_FILLBMP_MODE:
160 drawing::BitmapMode eMode;
161 if(!(aValue >>= eMode) )
163 sal_Int32 nMode = 0;
164 if(!(aValue >>= nMode))
165 throw lang::IllegalArgumentException();
167 eMode = static_cast<drawing::BitmapMode>(nMode);
170 pPool->SetPoolDefaultItem( XFillBmpStretchItem( eMode == drawing::BitmapMode_STRETCH ) );
171 pPool->SetPoolDefaultItem( XFillBmpTileItem( eMode == drawing::BitmapMode_REPEAT ) );
172 return;
174 while(false);
176 default:
178 std::unique_ptr<SfxPoolItem> pNewItem( pPool->GetDefaultItem( nWhich ).Clone() );
179 sal_uInt8 nMemberId = pEntry->mnMemberId;
180 if( pPool->GetMetric(nWhich) == MapUnit::Map100thMM )
181 nMemberId &= (~CONVERT_TWIPS);
183 if( !pNewItem->PutValue( aValue, nMemberId ) )
184 throw lang::IllegalArgumentException();
186 pPool->SetPoolDefaultItem( *pNewItem );
191 void SvxUnoDrawPool::_setPropertyValues( const comphelper::PropertyMapEntry** ppEntries, const uno::Any* pValues )
193 SolarMutexGuard aGuard;
195 SfxItemPool* pPool = getModelPool( false );
197 DBG_ASSERT( pPool, "I need a SfxItemPool!" );
198 if( nullptr == pPool )
199 throw beans::UnknownPropertyException( "no pool, no properties..", static_cast<cppu::OWeakObject*>(this));
201 while( *ppEntries )
202 putAny( pPool, *ppEntries++, *pValues++ );
205 void SvxUnoDrawPool::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValue )
207 SolarMutexGuard aGuard;
209 SfxItemPool* pPool = getModelPool( true );
211 DBG_ASSERT( pPool, "I need a SfxItemPool!" );
212 if( nullptr == pPool )
213 throw beans::UnknownPropertyException( "no pool, no properties..", static_cast<cppu::OWeakObject*>(this));
215 while( *ppEntries )
216 getAny( pPool, *ppEntries++, *pValue++ );
219 void SvxUnoDrawPool::_getPropertyStates( const comphelper::PropertyMapEntry** ppEntries, beans::PropertyState* pStates )
221 SolarMutexGuard aGuard;
223 SfxItemPool* pPool = getModelPool( true );
225 if( pPool && pPool != mpDefaultsPool )
227 while( *ppEntries )
229 //Assure, that ID is a Which-ID (it could be a Slot-ID.)
230 // Thus, convert handle to Which-ID.
231 const sal_uInt16 nWhich = pPool->GetWhich( static_cast<sal_uInt16>((*ppEntries)->mnHandle) );
233 switch( nWhich )
235 case OWN_ATTR_FILLBMP_MODE:
237 // use method <IsStaticDefaultItem(..)> instead of using
238 // probably incompatible item pool <mpDefaultPool>.
239 if ( IsStaticDefaultItem( &(pPool->GetDefaultItem( XATTR_FILLBMP_STRETCH )) ) ||
240 IsStaticDefaultItem( &(pPool->GetDefaultItem( XATTR_FILLBMP_TILE )) ) )
242 *pStates = beans::PropertyState_DEFAULT_VALUE;
244 else
246 *pStates = beans::PropertyState_DIRECT_VALUE;
249 break;
250 default:
251 //#i18732# - correction:
252 // use method <IsStaticDefaultItem(..)> instead of using probably
253 // incompatible item pool <mpDefaultPool>.
254 const SfxPoolItem& r1 = pPool->GetDefaultItem( nWhich );
255 //const SfxPoolItem& r2 = mpDefaultPool->GetDefaultItem( nWhich );
257 if ( IsStaticDefaultItem( &r1 ) )
259 *pStates = beans::PropertyState_DEFAULT_VALUE;
261 else
263 *pStates = beans::PropertyState_DIRECT_VALUE;
267 pStates++;
268 ppEntries++;
271 else
273 // as long as we have no model, all properties are default
274 while( *ppEntries++ )
275 *pStates++ = beans::PropertyState_DEFAULT_VALUE;
276 return;
280 void SvxUnoDrawPool::_setPropertyToDefault( const comphelper::PropertyMapEntry* pEntry )
282 SolarMutexGuard aGuard;
284 SfxItemPool* pPool = getModelPool( true );
286 // Assure, that ID is a Which-ID (it could be a Slot-ID.)
287 // Thus, convert handle to Which-ID.
288 const sal_uInt16 nWhich = pPool->GetWhich( static_cast<sal_uInt16>(pEntry->mnHandle) );
289 if ( pPool && pPool != mpDefaultsPool )
291 // use method <ResetPoolDefaultItem(..)> instead of using probably incompatible item pool <mpDefaultsPool>.
292 pPool->ResetPoolDefaultItem( nWhich );
296 uno::Any SvxUnoDrawPool::_getPropertyDefault( const comphelper::PropertyMapEntry* pEntry )
298 SolarMutexGuard aGuard;
299 //#i18732# - use method <GetPoolDefaultItem(..)> instead of
300 // using probably incompatible item pool <mpDefaultsPool>
301 uno::Any aAny;
302 SfxItemPool* pPool = getModelPool( true );
303 const sal_uInt16 nWhich = pPool->GetWhich( static_cast<sal_uInt16>(pEntry->mnHandle) );
304 const SfxPoolItem *pItem = pPool->GetPoolDefaultItem ( nWhich );
305 if (pItem)
307 pItem->QueryValue( aAny, pEntry->mnMemberId );
310 return aAny;
313 // XInterface
315 uno::Any SAL_CALL SvxUnoDrawPool::queryInterface( const uno::Type & rType )
317 return OWeakAggObject::queryInterface( rType );
320 uno::Any SAL_CALL SvxUnoDrawPool::queryAggregation( const uno::Type & rType )
322 uno::Any aAny;
324 if( rType == cppu::UnoType<lang::XServiceInfo>::get())
325 aAny <<= uno::Reference< lang::XServiceInfo >(this);
326 else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
327 aAny <<= uno::Reference< lang::XTypeProvider >(this);
328 else if( rType == cppu::UnoType<beans::XPropertySet>::get())
329 aAny <<= uno::Reference< beans::XPropertySet >(this);
330 else if( rType == cppu::UnoType<beans::XPropertyState>::get())
331 aAny <<= uno::Reference< beans::XPropertyState >(this);
332 else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get())
333 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
334 else
335 aAny = OWeakAggObject::queryAggregation( rType );
337 return aAny;
340 uno::Sequence< uno::Type > SAL_CALL SvxUnoDrawPool::getTypes()
342 static const uno::Sequence aTypes {
343 cppu::UnoType<uno::XAggregation>::get(),
344 cppu::UnoType<lang::XServiceInfo>::get(),
345 cppu::UnoType<lang::XTypeProvider>::get(),
346 cppu::UnoType<beans::XPropertySet>::get(),
347 cppu::UnoType<beans::XPropertyState>::get(),
348 cppu::UnoType<beans::XMultiPropertySet>::get() };
349 return aTypes;
352 uno::Sequence< sal_Int8 > SAL_CALL SvxUnoDrawPool::getImplementationId()
354 return css::uno::Sequence<sal_Int8>();
357 // XServiceInfo
358 sal_Bool SAL_CALL SvxUnoDrawPool::supportsService( const OUString& ServiceName )
360 return cppu::supportsService(this, ServiceName);
363 OUString SAL_CALL SvxUnoDrawPool::getImplementationName()
365 return "SvxUnoDrawPool";
368 uno::Sequence< OUString > SAL_CALL SvxUnoDrawPool::getSupportedServiceNames( )
370 uno::Sequence<OUString> aSNS { "com.sun.star.drawing.Defaults" };
371 return aSNS;
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */