xref: /aoo4110/main/svx/source/unodraw/unopool.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #ifndef _COM_SUN_STAR_BEANS_PROPERTYSTATE_HDL_
28 #include <com/sun/star/beans/PropertyState.hpp>
29 #endif
30 
31 #include <comphelper/propertysetinfo.hxx>
32 #include <rtl/uuid.h>
33 #include <vos/mutex.hxx>
34 #include <vcl/svapp.hxx>
35 #include "svx/unopool.hxx"
36 #include <svx/svdmodel.hxx>
37 #include <svx/svdpool.hxx>
38 #include <svx/unoprov.hxx>
39 #include <svx/svdobj.hxx>
40 #include <svx/unoshprp.hxx>
41 #include <svx/xflbstit.hxx>
42 #include <svx/xflbmtit.hxx>
43 #include <svx/unopage.hxx>
44 #include <svx/svdetc.hxx>
45 #include <editeng/editeng.hxx>
46 
47 #include "svx/unoapi.hxx"
48 #include <memory>
49 
50 using namespace ::com::sun::star;
51 using namespace ::rtl;
52 using namespace ::cppu;
53 
SvxUnoDrawPool(SdrModel * pModel,sal_Int32 nServiceId)54 SvxUnoDrawPool::SvxUnoDrawPool( SdrModel* pModel, sal_Int32 nServiceId ) throw()
55 : PropertySetHelper( SvxPropertySetInfoPool::getOrCreate( nServiceId ) ), mpModel( pModel )
56 {
57 	init();
58 }
59 
60 /* deprecated */
SvxUnoDrawPool(SdrModel * pModel)61 SvxUnoDrawPool::SvxUnoDrawPool( SdrModel* pModel ) throw()
62 : PropertySetHelper( SvxPropertySetInfoPool::getOrCreate( SVXUNO_SERVICEID_COM_SUN_STAR_DRAWING_DEFAULTS ) ), mpModel( pModel )
63 {
64 	init();
65 }
66 
~SvxUnoDrawPool()67 SvxUnoDrawPool::~SvxUnoDrawPool() throw()
68 {
69     // memory leak #119991#: to release the secondary pool created in function SvxUnoDrawPool::init()
70     SfxItemPool* pSecondaryPool = mpDefaultsPool->GetSecondaryPool();
71 
72     // #119991# delete master pool first, this will reset the pMaster entry in pSecondaryPool as needed.
73     // This is the needed order (see SdrModel::~SdrModel for example)
74     SfxItemPool::Free(mpDefaultsPool);
75 
76     // delete pSecondaryPool if exists
77     if(pSecondaryPool)
78     {
79         SfxItemPool::Free(pSecondaryPool);
80     }
81 }
82 
init()83 void SvxUnoDrawPool::init()
84 {
85     mpDefaultsPool = new SdrItemPool();
86 	SfxItemPool* pOutlPool=EditEngine::CreatePool();
87     mpDefaultsPool->SetSecondaryPool(pOutlPool);
88 
89     SdrModel::SetTextDefaults( mpDefaultsPool, SdrEngineDefaults::GetFontHeight() );
90     mpDefaultsPool->SetDefaultMetric((SfxMapUnit)SdrEngineDefaults::GetMapUnit());
91     mpDefaultsPool->FreezeIdRanges();
92 }
93 
getModelPool(sal_Bool bReadOnly)94 SfxItemPool* SvxUnoDrawPool::getModelPool( sal_Bool bReadOnly ) throw()
95 {
96 	if( mpModel	)
97 	{
98 		return &mpModel->GetItemPool();
99 	}
100 	else
101 	{
102 		if( bReadOnly )
103             return mpDefaultsPool;
104 		else
105 			return NULL;
106 	}
107 }
108 
getAny(SfxItemPool * pPool,const comphelper::PropertyMapEntry * pEntry,uno::Any & rValue)109 void SvxUnoDrawPool::getAny( SfxItemPool* pPool, const comphelper::PropertyMapEntry* pEntry, uno::Any& rValue )
110 	throw(beans::UnknownPropertyException)
111 {
112 	switch( pEntry->mnHandle )
113 	{
114 	case OWN_ATTR_FILLBMP_MODE:
115 		{
116 			XFillBmpStretchItem* pStretchItem = (XFillBmpStretchItem*)&pPool->GetDefaultItem(XATTR_FILLBMP_STRETCH);
117 			XFillBmpTileItem* pTileItem = (XFillBmpTileItem*)&pPool->GetDefaultItem(XATTR_FILLBMP_TILE);
118 			if( pTileItem && pTileItem->GetValue() )
119 			{
120 				rValue <<= drawing::BitmapMode_REPEAT;
121 			}
122 			else if( pStretchItem && pStretchItem->GetValue() )
123 			{
124 				rValue <<= drawing::BitmapMode_STRETCH;
125 			}
126 			else
127 			{
128 				rValue <<= drawing::BitmapMode_NO_REPEAT;
129 			}
130 			break;
131 		}
132     default:
133 		{
134 			const SfxMapUnit eMapUnit = pPool ? pPool->GetMetric((sal_uInt16)pEntry->mnHandle) : SFX_MAPUNIT_100TH_MM;
135 
136 			sal_uInt8 nMemberId = pEntry->mnMemberId & (~SFX_METRIC_ITEM);
137 			if( eMapUnit == SFX_MAPUNIT_100TH_MM )
138 				nMemberId &= (~CONVERT_TWIPS);
139 
140             // DVO, OD 10.10.2003 #i18732#
141             // Assure, that ID is a Which-ID (it could be a Slot-ID.)
142             // Thus, convert handle to Which-ID.
143             pPool->GetDefaultItem( pPool->GetWhich( (sal_uInt16)pEntry->mnHandle ) ).QueryValue( rValue, nMemberId );
144 		}
145 	}
146 
147 
148 	// check for needed metric translation
149 	const SfxMapUnit eMapUnit = pPool->GetMetric((sal_uInt16)pEntry->mnHandle);
150 	if(pEntry->mnMemberId & SFX_METRIC_ITEM && eMapUnit != SFX_MAPUNIT_100TH_MM)
151 	{
152 		SvxUnoConvertToMM( eMapUnit, rValue );
153 	}
154 	// convert int32 to correct enum type if needed
155 	else if ( pEntry->mpType->getTypeClass() == uno::TypeClass_ENUM && rValue.getValueType() == ::getCppuType((const sal_Int32*)0) )
156 	{
157 		sal_Int32 nEnum;
158 		rValue >>= nEnum;
159 
160 		rValue.setValue( &nEnum, *pEntry->mpType );
161 	}
162 }
163 
putAny(SfxItemPool * pPool,const comphelper::PropertyMapEntry * pEntry,const uno::Any & rValue)164 void SvxUnoDrawPool::putAny( SfxItemPool* pPool, const comphelper::PropertyMapEntry* pEntry, const uno::Any& rValue )
165 	throw(beans::UnknownPropertyException, lang::IllegalArgumentException)
166 {
167 	uno::Any aValue( rValue );
168 
169 	const SfxMapUnit eMapUnit = pPool->GetMetric((sal_uInt16)pEntry->mnHandle);
170 	if(pEntry->mnMemberId & SFX_METRIC_ITEM && eMapUnit != SFX_MAPUNIT_100TH_MM)
171 	{
172 		SvxUnoConvertFromMM( eMapUnit, aValue );
173 	}
174 
175     // DVO, OD 10.10.2003 #i18732#
176     // Assure, that ID is a Which-ID (it could be a Slot-ID.)
177     // Thus, convert handle to Which-ID.
178     const sal_uInt16 nWhich = pPool->GetWhich( (sal_uInt16)pEntry->mnHandle );
179 	switch( nWhich )
180 	{
181 		case OWN_ATTR_FILLBMP_MODE:
182 			do
183 			{
184 				drawing::BitmapMode eMode;
185 				if(!(aValue >>= eMode) )
186 				{
187 					sal_Int32 nMode = 0;
188 					if(!(aValue >>= nMode))
189 						throw lang::IllegalArgumentException();
190 
191 					eMode = (drawing::BitmapMode)nMode;
192 				}
193 
194 				pPool->SetPoolDefaultItem( XFillBmpStretchItem( eMode == drawing::BitmapMode_STRETCH ) );
195 				pPool->SetPoolDefaultItem( XFillBmpTileItem( eMode == drawing::BitmapMode_REPEAT ) );
196 				return;
197 			}
198 			while(0);
199 
200     default:
201 		{
202 			::std::auto_ptr<SfxPoolItem> pNewItem( pPool->GetDefaultItem( nWhich ).Clone() );
203 			sal_uInt8 nMemberId = pEntry->mnMemberId & (~SFX_METRIC_ITEM);
204 			if( !pPool || (pPool->GetMetric(nWhich) == SFX_MAPUNIT_100TH_MM) )
205 				nMemberId &= (~CONVERT_TWIPS);
206 
207 			if( !pNewItem->PutValue( aValue, nMemberId ) )
208 				throw lang::IllegalArgumentException();
209 
210 			pPool->SetPoolDefaultItem( *pNewItem );
211 		}
212 	}
213 }
214 
_setPropertyValues(const comphelper::PropertyMapEntry ** ppEntries,const uno::Any * pValues)215 void SvxUnoDrawPool::_setPropertyValues( const comphelper::PropertyMapEntry** ppEntries, const uno::Any* pValues )
216 	throw(beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException )
217 {
218 	vos::OGuard aGuard( Application::GetSolarMutex() );
219 
220 	SfxItemPool* pPool = getModelPool( sal_False );
221 
222 	DBG_ASSERT( pPool, "I need a SfxItemPool!" );
223 	if( NULL == pPool )
224 		throw beans::UnknownPropertyException();
225 
226 	while( *ppEntries )
227 		putAny( pPool, *ppEntries++, *pValues++ );
228 }
229 
_getPropertyValues(const comphelper::PropertyMapEntry ** ppEntries,uno::Any * pValue)230 void SvxUnoDrawPool::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValue )
231 	throw(beans::UnknownPropertyException, lang::WrappedTargetException )
232 {
233 	vos::OGuard aGuard( Application::GetSolarMutex() );
234 
235 	SfxItemPool* pPool = getModelPool( sal_True );
236 
237 	DBG_ASSERT( pPool, "I need a SfxItemPool!" );
238 	if( NULL == pPool )
239 		throw beans::UnknownPropertyException();
240 
241 	while( *ppEntries )
242 		getAny( pPool, *ppEntries++, *pValue++ );
243 }
244 
_getPropertyStates(const comphelper::PropertyMapEntry ** ppEntries,beans::PropertyState * pStates)245 void SvxUnoDrawPool::_getPropertyStates( const comphelper::PropertyMapEntry** ppEntries, beans::PropertyState* pStates )
246 	throw(beans::UnknownPropertyException )
247 {
248 	vos::OGuard aGuard( Application::GetSolarMutex() );
249 
250 	SfxItemPool* pPool = getModelPool( sal_True );
251 
252     if( pPool && pPool != mpDefaultsPool )
253 	{
254 		while( *ppEntries )
255 		{
256             // OD 13.10.2003 #i18732#
257             // Assure, that ID is a Which-ID (it could be a Slot-ID.)
258             // Thus, convert handle to Which-ID.
259             const sal_uInt16 nWhich = pPool->GetWhich( ((sal_uInt16)(*ppEntries)->mnHandle) );
260 
261 			switch( nWhich )
262 			{
263 			case OWN_ATTR_FILLBMP_MODE:
264 				{
265                     // use method <IsStaticDefaultItem(..)> instead of using
266                     // probably incompatible item pool <mpDefaultPool>.
267                     if ( IsStaticDefaultItem( &(pPool->GetDefaultItem( XATTR_FILLBMP_STRETCH )) ) ||
268                          IsStaticDefaultItem( &(pPool->GetDefaultItem( XATTR_FILLBMP_TILE )) ) )
269 					{
270 						*pStates = beans::PropertyState_DEFAULT_VALUE;
271 					}
272 					else
273 					{
274 						*pStates = beans::PropertyState_DIRECT_VALUE;
275 					}
276 				}
277 				break;
278 			default:
279                 // OD 13.10.2003 #i18732# - correction:
280                 // use method <IsStaticDefaultItem(..)> instead of using probably
281                 // incompatible item pool <mpDefaultPool>.
282                 const SfxPoolItem& r1 = pPool->GetDefaultItem( nWhich );
283                 //const SfxPoolItem& r2 = mpDefaultPool->GetDefaultItem( nWhich );
284 
285                 if ( IsStaticDefaultItem( &r1 ) )
286 				{
287 					*pStates = beans::PropertyState_DEFAULT_VALUE;
288 				}
289 				else
290 				{
291 					*pStates = beans::PropertyState_DIRECT_VALUE;
292 				}
293 			}
294 
295 			pStates++;
296 			ppEntries++;
297 		}
298 	}
299 	else
300 	{
301 		// as long as we have no model, all properties are default
302 		while( *ppEntries++ )
303 			*pStates++ = beans::PropertyState_DEFAULT_VALUE;
304 		return;
305 	}
306 }
307 
_setPropertyToDefault(const comphelper::PropertyMapEntry * pEntry)308 void SvxUnoDrawPool::_setPropertyToDefault( const comphelper::PropertyMapEntry* pEntry )
309 	throw(beans::UnknownPropertyException )
310 {
311 	vos::OGuard aGuard( Application::GetSolarMutex() );
312 
313 	SfxItemPool* pPool = getModelPool( sal_True );
314 
315     // OD 10.10.2003 #i18732#
316     // Assure, that ID is a Which-ID (it could be a Slot-ID.)
317     // Thus, convert handle to Which-ID.
318     const sal_uInt16 nWhich = pPool->GetWhich( (sal_uInt16)pEntry->mnHandle );
319     if ( pPool && pPool != mpDefaultsPool )
320     {
321         // OD 13.10.2003 #i18732# - use method <ResetPoolDefaultItem(..)>
322         // instead of using probably incompatible item pool <mpDefaultsPool>.
323         pPool->ResetPoolDefaultItem( nWhich );
324     }
325 }
326 
_getPropertyDefault(const comphelper::PropertyMapEntry * pEntry)327 uno::Any SvxUnoDrawPool::_getPropertyDefault( const comphelper::PropertyMapEntry* pEntry )
328 	throw(beans::UnknownPropertyException, lang::WrappedTargetException )
329 {
330 	vos::OGuard aGuard( Application::GetSolarMutex() );
331 
332     // OD 13.10.2003 #i18732# - use method <GetPoolDefaultItem(..)> instead of
333     // using probably incompatible item pool <mpDefaultsPool>
334     uno::Any aAny;
335     SfxItemPool* pPool = getModelPool( sal_True );
336     const sal_uInt16 nWhich = pPool->GetWhich( (sal_uInt16)pEntry->mnHandle );
337     const SfxPoolItem *pItem = pPool->GetPoolDefaultItem ( nWhich );
338     pItem->QueryValue( aAny, pEntry->mnMemberId );
339 
340     return aAny;
341 }
342 
343 // XInterface
344 
queryInterface(const uno::Type & rType)345 uno::Any SAL_CALL SvxUnoDrawPool::queryInterface( const uno::Type & rType )
346 	throw( uno::RuntimeException )
347 {
348 	return OWeakAggObject::queryInterface( rType );
349 }
350 
queryAggregation(const uno::Type & rType)351 uno::Any SAL_CALL SvxUnoDrawPool::queryAggregation( const uno::Type & rType )
352 	throw(uno::RuntimeException)
353 {
354 	uno::Any aAny;
355 
356 	if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
357 		aAny <<= uno::Reference< lang::XServiceInfo >(this);
358 	else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
359 		aAny <<= uno::Reference< lang::XTypeProvider >(this);
360 	else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
361 		aAny <<= uno::Reference< beans::XPropertySet >(this);
362 	else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
363 		aAny <<= uno::Reference< beans::XPropertyState >(this);
364 	else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
365 		aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
366 	else
367 		aAny <<= OWeakAggObject::queryAggregation( rType );
368 
369 	return aAny;
370 }
371 
acquire()372 void SAL_CALL SvxUnoDrawPool::acquire() throw ( )
373 {
374 	OWeakAggObject::acquire();
375 }
376 
release()377 void SAL_CALL SvxUnoDrawPool::release() throw ( )
378 {
379 	OWeakAggObject::release();
380 }
381 
getTypes()382 uno::Sequence< uno::Type > SAL_CALL SvxUnoDrawPool::getTypes()
383 	throw (uno::RuntimeException)
384 {
385 	uno::Sequence< uno::Type > aTypes( 6 );
386 	uno::Type* pTypes = aTypes.getArray();
387 
388 	*pTypes++ = ::getCppuType((const uno::Reference< uno::XAggregation>*)0);
389 	*pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
390 	*pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
391 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
392 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
393 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
394 
395 	return aTypes;
396 }
397 
getImplementationId()398 uno::Sequence< sal_Int8 > SAL_CALL SvxUnoDrawPool::getImplementationId()
399 	throw (uno::RuntimeException)
400 {
401 	vos::OGuard aGuard( Application::GetSolarMutex() );
402 
403 	static uno::Sequence< sal_Int8 > aId;
404 	if( aId.getLength() == 0 )
405 	{
406 		aId.realloc( 16 );
407 		rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
408 	}
409 	return aId;
410 }
411 
412 // XServiceInfo
413 
supportsService(const OUString & ServiceName)414 sal_Bool SAL_CALL SvxUnoDrawPool::supportsService( const  OUString& ServiceName ) throw(uno::RuntimeException)
415 {
416     uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
417     const OUString * pArray = aSNL.getConstArray();
418 
419     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
420         if( pArray[i] == ServiceName )
421             return sal_True;
422 
423     return sal_False;
424 }
425 
getImplementationName()426 OUString SAL_CALL SvxUnoDrawPool::getImplementationName() throw( uno::RuntimeException )
427 {
428 	return OUString( RTL_CONSTASCII_USTRINGPARAM("SvxUnoDrawPool") );
429 }
430 
getSupportedServiceNames()431 uno::Sequence< OUString > SAL_CALL SvxUnoDrawPool::getSupportedServiceNames(  )
432 	throw( uno::RuntimeException )
433 {
434     uno::Sequence< OUString > aSNS( 1 );
435     aSNS.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.Defaults" ));
436     return aSNS;
437 }
438