Bump version to 24.04.3.4
[LibreOffice.git] / svx / source / unodraw / unopool.cxx
blob5dc577a3e87b0bf79d7227cdf4bbf7439dd1629d
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, rtl::Reference<comphelper::PropertySetInfo> const & xDefaults)
43 : SvxUnoDrawPool_Base(), PropertySetHelper( xDefaults ), mpModel( pModel )
45 init();
48 SvxUnoDrawPool::~SvxUnoDrawPool() noexcept
52 void SvxUnoDrawPool::init()
54 mpDefaultsPool = new SdrItemPool();
55 rtl::Reference<SfxItemPool> pOutlPool = EditEngine::CreatePool();
56 mpDefaultsPool->SetSecondaryPool(pOutlPool.get());
58 SdrModel::SetTextDefaults( mpDefaultsPool.get(), SdrEngineDefaults::GetFontHeight() );
59 mpDefaultsPool->SetDefaultMetric(SdrEngineDefaults::GetMapUnit());
60 mpDefaultsPool->FreezeIdRanges();
63 SfxItemPool* SvxUnoDrawPool::getModelPool( bool bReadOnly ) noexcept
65 if( mpModel )
67 return &mpModel->GetItemPool();
69 else
71 if( bReadOnly )
72 return mpDefaultsPool.get();
73 else
74 return nullptr;
78 void SvxUnoDrawPool::getAny( SfxItemPool const * pPool, const comphelper::PropertyMapEntry* pEntry, uno::Any& rValue )
80 switch( pEntry->mnHandle )
82 case OWN_ATTR_FILLBMP_MODE:
84 if (pPool->GetDefaultItem(XATTR_FILLBMP_TILE).GetValue())
86 rValue <<= drawing::BitmapMode_REPEAT;
88 else if (pPool->GetDefaultItem(XATTR_FILLBMP_STRETCH).GetValue())
90 rValue <<= drawing::BitmapMode_STRETCH;
92 else
94 rValue <<= drawing::BitmapMode_NO_REPEAT;
96 break;
98 default:
100 const MapUnit eMapUnit = pPool->GetMetric(static_cast<sal_uInt16>(pEntry->mnHandle));
102 sal_uInt8 nMemberId = pEntry->mnMemberId;
103 if( eMapUnit == MapUnit::Map100thMM )
104 nMemberId &= (~CONVERT_TWIPS);
106 // Assure, that ID is a Which-ID (it could be a Slot-ID.)
107 // Thus, convert handle to Which-ID.
108 pPool->GetDefaultItem( pPool->GetWhich( static_cast<sal_uInt16>(pEntry->mnHandle) ) ).QueryValue( rValue, nMemberId );
113 // check for needed metric translation
114 const MapUnit eMapUnit = pPool->GetMetric(static_cast<sal_uInt16>(pEntry->mnHandle));
115 if(pEntry->mnMoreFlags & PropertyMoreFlags::METRIC_ITEM && eMapUnit != MapUnit::Map100thMM)
117 SvxUnoConvertToMM( eMapUnit, rValue );
119 // convert int32 to correct enum type if needed
120 else if ( pEntry->maType.getTypeClass() == uno::TypeClass_ENUM && rValue.getValueType() == ::cppu::UnoType<sal_Int32>::get() )
122 sal_Int32 nEnum;
123 rValue >>= nEnum;
125 rValue.setValue( &nEnum, pEntry->maType );
129 void SvxUnoDrawPool::putAny( SfxItemPool* pPool, const comphelper::PropertyMapEntry* pEntry, const uno::Any& rValue )
131 uno::Any aValue( rValue );
133 const MapUnit eMapUnit = pPool->GetMetric(static_cast<sal_uInt16>(pEntry->mnHandle));
134 if(pEntry->mnMoreFlags & PropertyMoreFlags::METRIC_ITEM && eMapUnit != MapUnit::Map100thMM)
136 SvxUnoConvertFromMM( eMapUnit, aValue );
139 // Assure, that ID is a Which-ID (it could be a Slot-ID.)
140 // Thus, convert handle to Which-ID.
141 const sal_uInt16 nWhich = pPool->GetWhich( static_cast<sal_uInt16>(pEntry->mnHandle) );
142 switch( nWhich )
144 case OWN_ATTR_FILLBMP_MODE:
147 drawing::BitmapMode eMode;
148 if(!(aValue >>= eMode) )
150 sal_Int32 nMode = 0;
151 if(!(aValue >>= nMode))
152 throw lang::IllegalArgumentException();
154 eMode = static_cast<drawing::BitmapMode>(nMode);
157 pPool->SetPoolDefaultItem( XFillBmpStretchItem( eMode == drawing::BitmapMode_STRETCH ) );
158 pPool->SetPoolDefaultItem( XFillBmpTileItem( eMode == drawing::BitmapMode_REPEAT ) );
159 return;
161 while(false);
163 default:
165 std::unique_ptr<SfxPoolItem> pNewItem( pPool->GetDefaultItem( nWhich ).Clone() );
166 sal_uInt8 nMemberId = pEntry->mnMemberId;
167 if( pPool->GetMetric(nWhich) == MapUnit::Map100thMM )
168 nMemberId &= (~CONVERT_TWIPS);
170 if( !pNewItem->PutValue( aValue, nMemberId ) )
171 throw lang::IllegalArgumentException();
173 pPool->SetPoolDefaultItem( *pNewItem );
178 void SvxUnoDrawPool::_setPropertyValues( const comphelper::PropertyMapEntry** ppEntries, const uno::Any* pValues )
180 SolarMutexGuard aGuard;
182 SfxItemPool* pPool = getModelPool( false );
184 DBG_ASSERT( pPool, "I need a SfxItemPool!" );
185 if( nullptr == pPool )
186 throw beans::UnknownPropertyException( "no pool, no properties..", getXWeak());
188 while( *ppEntries )
189 putAny( pPool, *ppEntries++, *pValues++ );
192 void SvxUnoDrawPool::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValue )
194 SolarMutexGuard aGuard;
196 SfxItemPool* pPool = getModelPool( true );
198 DBG_ASSERT( pPool, "I need a SfxItemPool!" );
199 if( nullptr == pPool )
200 throw beans::UnknownPropertyException( "no pool, no properties..", getXWeak());
202 while( *ppEntries )
203 getAny( pPool, *ppEntries++, *pValue++ );
206 void SvxUnoDrawPool::_getPropertyStates( const comphelper::PropertyMapEntry** ppEntries, beans::PropertyState* pStates )
208 SolarMutexGuard aGuard;
210 SfxItemPool* pPool = getModelPool( true );
212 if( pPool && pPool != mpDefaultsPool.get() )
214 while( *ppEntries )
216 //Assure, that ID is a Which-ID (it could be a Slot-ID.)
217 // Thus, convert handle to Which-ID.
218 const sal_uInt16 nWhich = pPool->GetWhich( static_cast<sal_uInt16>((*ppEntries)->mnHandle) );
220 switch( nWhich )
222 case OWN_ATTR_FILLBMP_MODE:
224 // use method <IsStaticDefaultItem(..)> instead of using
225 // probably incompatible item pool <mpDefaultPool>.
226 if ( IsStaticDefaultItem( &(pPool->GetDefaultItem( XATTR_FILLBMP_STRETCH )) ) ||
227 IsStaticDefaultItem( &(pPool->GetDefaultItem( XATTR_FILLBMP_TILE )) ) )
229 *pStates = beans::PropertyState_DEFAULT_VALUE;
231 else
233 *pStates = beans::PropertyState_DIRECT_VALUE;
236 break;
237 case OWN_ATTR_TEXTCOLUMNS:
238 if (IsStaticDefaultItem(&pPool->GetDefaultItem(sal_uInt16(SDRATTR_TEXTCOLUMNS_NUMBER)))
239 && IsStaticDefaultItem(&pPool->GetDefaultItem(sal_uInt16(SDRATTR_TEXTCOLUMNS_SPACING))))
240 *pStates = beans::PropertyState_DEFAULT_VALUE;
241 else
242 *pStates = beans::PropertyState_DIRECT_VALUE;
243 break;
244 default:
245 //#i18732# - correction:
246 // use method <IsStaticDefaultItem(..)> instead of using probably
247 // incompatible item pool <mpDefaultPool>.
248 const SfxPoolItem& r1 = pPool->GetDefaultItem( nWhich );
249 //const SfxPoolItem& r2 = mpDefaultPool->GetDefaultItem( nWhich );
251 if ( IsStaticDefaultItem( &r1 ) )
253 *pStates = beans::PropertyState_DEFAULT_VALUE;
255 else
257 *pStates = beans::PropertyState_DIRECT_VALUE;
261 pStates++;
262 ppEntries++;
265 else
267 // as long as we have no model, all properties are default
268 while( *ppEntries++ )
269 *pStates++ = beans::PropertyState_DEFAULT_VALUE;
270 return;
274 void SvxUnoDrawPool::_setPropertyToDefault( const comphelper::PropertyMapEntry* pEntry )
276 SolarMutexGuard aGuard;
278 SfxItemPool* pPool = getModelPool( true );
280 // Assure, that ID is a Which-ID (it could be a Slot-ID.)
281 // Thus, convert handle to Which-ID.
282 const sal_uInt16 nWhich = pPool->GetWhich( static_cast<sal_uInt16>(pEntry->mnHandle) );
283 if ( pPool && pPool != mpDefaultsPool.get() )
285 // use method <ResetPoolDefaultItem(..)> instead of using probably incompatible item pool <mpDefaultsPool>.
286 pPool->ResetPoolDefaultItem( nWhich );
290 uno::Any SvxUnoDrawPool::_getPropertyDefault( const comphelper::PropertyMapEntry* pEntry )
292 SolarMutexGuard aGuard;
293 //#i18732# - use method <GetPoolDefaultItem(..)> instead of
294 // using probably incompatible item pool <mpDefaultsPool>
295 uno::Any aAny;
296 SfxItemPool* pPool = getModelPool( true );
297 const sal_uInt16 nWhich = pPool->GetWhich( static_cast<sal_uInt16>(pEntry->mnHandle) );
298 const SfxPoolItem *pItem = pPool->GetPoolDefaultItem ( nWhich );
299 if (pItem)
301 pItem->QueryValue( aAny, pEntry->mnMemberId );
304 return aAny;
307 // XInterface
309 uno::Any SAL_CALL SvxUnoDrawPool::queryInterface( const uno::Type & rType )
311 uno::Any aAny;
313 if( rType == cppu::UnoType<lang::XServiceInfo>::get())
314 aAny <<= uno::Reference< lang::XServiceInfo >(this);
315 else if( rType == cppu::UnoType<lang::XTypeProvider>::get())
316 aAny <<= uno::Reference< lang::XTypeProvider >(this);
317 else if( rType == cppu::UnoType<beans::XPropertySet>::get())
318 aAny <<= uno::Reference< beans::XPropertySet >(this);
319 else if( rType == cppu::UnoType<beans::XPropertyState>::get())
320 aAny <<= uno::Reference< beans::XPropertyState >(this);
321 else if( rType == cppu::UnoType<beans::XMultiPropertySet>::get())
322 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
323 else
324 aAny = OWeakObject::queryInterface( rType );
326 return aAny;
329 uno::Sequence< uno::Type > SAL_CALL SvxUnoDrawPool::getTypes()
331 static const uno::Sequence aTypes {
332 cppu::UnoType<lang::XServiceInfo>::get(),
333 cppu::UnoType<lang::XTypeProvider>::get(),
334 cppu::UnoType<beans::XPropertySet>::get(),
335 cppu::UnoType<beans::XPropertyState>::get(),
336 cppu::UnoType<beans::XMultiPropertySet>::get() };
337 return aTypes;
340 uno::Sequence< sal_Int8 > SAL_CALL SvxUnoDrawPool::getImplementationId()
342 return css::uno::Sequence<sal_Int8>();
345 // XServiceInfo
346 sal_Bool SAL_CALL SvxUnoDrawPool::supportsService( const OUString& ServiceName )
348 return cppu::supportsService(this, ServiceName);
351 OUString SAL_CALL SvxUnoDrawPool::getImplementationName()
353 return "SvxUnoDrawPool";
356 uno::Sequence< OUString > SAL_CALL SvxUnoDrawPool::getSupportedServiceNames( )
358 uno::Sequence<OUString> aSNS { "com.sun.star.drawing.Defaults" };
359 return aSNS;
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */