xref: /aoo41x/main/svx/source/unodraw/unopool.cxx (revision e262f9e6)
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 
54 SvxUnoDrawPool::SvxUnoDrawPool( SdrModel* pModel, sal_Int32 nServiceId ) throw()
55 : PropertySetHelper( SvxPropertySetInfoPool::getOrCreate( nServiceId ) ), mpModel( pModel )
56 {
57 	init();
58 }
59 
60 /* deprecated */
61 SvxUnoDrawPool::SvxUnoDrawPool( SdrModel* pModel ) throw()
62 : PropertySetHelper( SvxPropertySetInfoPool::getOrCreate( SVXUNO_SERVICEID_COM_SUN_STAR_DRAWING_DEFAULTS ) ), mpModel( pModel )
63 {
64 	init();
65 }
66 
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     if (pSecondaryPool != NULL)
72         SfxItemPool::Free(pSecondaryPool);
73     // memory leak #119991
74 
75     SfxItemPool::Free(mpDefaultsPool);
76 }
77 
78 void SvxUnoDrawPool::init()
79 {
80     mpDefaultsPool = new SdrItemPool();
81 	SfxItemPool* pOutlPool=EditEngine::CreatePool();
82     mpDefaultsPool->SetSecondaryPool(pOutlPool);
83 
84     SdrModel::SetTextDefaults( mpDefaultsPool, SdrEngineDefaults::GetFontHeight() );
85     mpDefaultsPool->SetDefaultMetric((SfxMapUnit)SdrEngineDefaults::GetMapUnit());
86     mpDefaultsPool->FreezeIdRanges();
87 }
88 
89 SfxItemPool* SvxUnoDrawPool::getModelPool( sal_Bool bReadOnly ) throw()
90 {
91 	if( mpModel	)
92 	{
93 		return &mpModel->GetItemPool();
94 	}
95 	else
96 	{
97 		if( bReadOnly )
98             return mpDefaultsPool;
99 		else
100 			return NULL;
101 	}
102 }
103 
104 void SvxUnoDrawPool::getAny( SfxItemPool* pPool, const comphelper::PropertyMapEntry* pEntry, uno::Any& rValue )
105 	throw(beans::UnknownPropertyException)
106 {
107 	switch( pEntry->mnHandle )
108 	{
109 	case OWN_ATTR_FILLBMP_MODE:
110 		{
111 			XFillBmpStretchItem* pStretchItem = (XFillBmpStretchItem*)&pPool->GetDefaultItem(XATTR_FILLBMP_STRETCH);
112 			XFillBmpTileItem* pTileItem = (XFillBmpTileItem*)&pPool->GetDefaultItem(XATTR_FILLBMP_TILE);
113 			if( pTileItem && pTileItem->GetValue() )
114 			{
115 				rValue <<= drawing::BitmapMode_REPEAT;
116 			}
117 			else if( pStretchItem && pStretchItem->GetValue() )
118 			{
119 				rValue <<= drawing::BitmapMode_STRETCH;
120 			}
121 			else
122 			{
123 				rValue <<= drawing::BitmapMode_NO_REPEAT;
124 			}
125 			break;
126 		}
127     default:
128 		{
129 			const SfxMapUnit eMapUnit = pPool ? pPool->GetMetric((sal_uInt16)pEntry->mnHandle) : SFX_MAPUNIT_100TH_MM;
130 
131 			sal_uInt8 nMemberId = pEntry->mnMemberId & (~SFX_METRIC_ITEM);
132 			if( eMapUnit == SFX_MAPUNIT_100TH_MM )
133 				nMemberId &= (~CONVERT_TWIPS);
134 
135             // DVO, OD 10.10.2003 #i18732#
136             // Assure, that ID is a Which-ID (it could be a Slot-ID.)
137             // Thus, convert handle to Which-ID.
138             pPool->GetDefaultItem( pPool->GetWhich( (sal_uInt16)pEntry->mnHandle ) ).QueryValue( rValue, nMemberId );
139 		}
140 	}
141 
142 
143 	// check for needed metric translation
144 	const SfxMapUnit eMapUnit = pPool->GetMetric((sal_uInt16)pEntry->mnHandle);
145 	if(pEntry->mnMemberId & SFX_METRIC_ITEM && eMapUnit != SFX_MAPUNIT_100TH_MM)
146 	{
147 		SvxUnoConvertToMM( eMapUnit, rValue );
148 	}
149 	// convert int32 to correct enum type if needed
150 	else if ( pEntry->mpType->getTypeClass() == uno::TypeClass_ENUM && rValue.getValueType() == ::getCppuType((const sal_Int32*)0) )
151 	{
152 		sal_Int32 nEnum;
153 		rValue >>= nEnum;
154 
155 		rValue.setValue( &nEnum, *pEntry->mpType );
156 	}
157 }
158 
159 void SvxUnoDrawPool::putAny( SfxItemPool* pPool, const comphelper::PropertyMapEntry* pEntry, const uno::Any& rValue )
160 	throw(beans::UnknownPropertyException, lang::IllegalArgumentException)
161 {
162 	uno::Any aValue( rValue );
163 
164 	const SfxMapUnit eMapUnit = pPool->GetMetric((sal_uInt16)pEntry->mnHandle);
165 	if(pEntry->mnMemberId & SFX_METRIC_ITEM && eMapUnit != SFX_MAPUNIT_100TH_MM)
166 	{
167 		SvxUnoConvertFromMM( eMapUnit, aValue );
168 	}
169 
170     // DVO, OD 10.10.2003 #i18732#
171     // Assure, that ID is a Which-ID (it could be a Slot-ID.)
172     // Thus, convert handle to Which-ID.
173     const sal_uInt16 nWhich = pPool->GetWhich( (sal_uInt16)pEntry->mnHandle );
174 	switch( nWhich )
175 	{
176 		case OWN_ATTR_FILLBMP_MODE:
177 			do
178 			{
179 				drawing::BitmapMode eMode;
180 				if(!(aValue >>= eMode) )
181 				{
182 					sal_Int32 nMode = 0;
183 					if(!(aValue >>= nMode))
184 						throw lang::IllegalArgumentException();
185 
186 					eMode = (drawing::BitmapMode)nMode;
187 				}
188 
189 				pPool->SetPoolDefaultItem( XFillBmpStretchItem( eMode == drawing::BitmapMode_STRETCH ) );
190 				pPool->SetPoolDefaultItem( XFillBmpTileItem( eMode == drawing::BitmapMode_REPEAT ) );
191 				return;
192 			}
193 			while(0);
194 
195     default:
196 		{
197 			::std::auto_ptr<SfxPoolItem> pNewItem( pPool->GetDefaultItem( nWhich ).Clone() );
198 			sal_uInt8 nMemberId = pEntry->mnMemberId & (~SFX_METRIC_ITEM);
199 			if( !pPool || (pPool->GetMetric(nWhich) == SFX_MAPUNIT_100TH_MM) )
200 				nMemberId &= (~CONVERT_TWIPS);
201 
202 			if( !pNewItem->PutValue( aValue, nMemberId ) )
203 				throw lang::IllegalArgumentException();
204 
205 			pPool->SetPoolDefaultItem( *pNewItem );
206 		}
207 	}
208 }
209 
210 void SvxUnoDrawPool::_setPropertyValues( const comphelper::PropertyMapEntry** ppEntries, const uno::Any* pValues )
211 	throw(beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException )
212 {
213 	vos::OGuard aGuard( Application::GetSolarMutex() );
214 
215 	SfxItemPool* pPool = getModelPool( sal_False );
216 
217 	DBG_ASSERT( pPool, "I need a SfxItemPool!" );
218 	if( NULL == pPool )
219 		throw beans::UnknownPropertyException();
220 
221 	while( *ppEntries )
222 		putAny( pPool, *ppEntries++, *pValues++ );
223 }
224 
225 void SvxUnoDrawPool::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValue )
226 	throw(beans::UnknownPropertyException, lang::WrappedTargetException )
227 {
228 	vos::OGuard aGuard( Application::GetSolarMutex() );
229 
230 	SfxItemPool* pPool = getModelPool( sal_True );
231 
232 	DBG_ASSERT( pPool, "I need a SfxItemPool!" );
233 	if( NULL == pPool )
234 		throw beans::UnknownPropertyException();
235 
236 	while( *ppEntries )
237 		getAny( pPool, *ppEntries++, *pValue++ );
238 }
239 
240 void SvxUnoDrawPool::_getPropertyStates( const comphelper::PropertyMapEntry** ppEntries, beans::PropertyState* pStates )
241 	throw(beans::UnknownPropertyException )
242 {
243 	vos::OGuard aGuard( Application::GetSolarMutex() );
244 
245 	SfxItemPool* pPool = getModelPool( sal_True );
246 
247     if( pPool && pPool != mpDefaultsPool )
248 	{
249 		while( *ppEntries )
250 		{
251             // OD 13.10.2003 #i18732#
252             // Assure, that ID is a Which-ID (it could be a Slot-ID.)
253             // Thus, convert handle to Which-ID.
254             const sal_uInt16 nWhich = pPool->GetWhich( ((sal_uInt16)(*ppEntries)->mnHandle) );
255 
256 			switch( nWhich )
257 			{
258 			case OWN_ATTR_FILLBMP_MODE:
259 				{
260                     // use method <IsStaticDefaultItem(..)> instead of using
261                     // probably incompatible item pool <mpDefaultPool>.
262                     if ( IsStaticDefaultItem( &(pPool->GetDefaultItem( XATTR_FILLBMP_STRETCH )) ) ||
263                          IsStaticDefaultItem( &(pPool->GetDefaultItem( XATTR_FILLBMP_TILE )) ) )
264 					{
265 						*pStates = beans::PropertyState_DEFAULT_VALUE;
266 					}
267 					else
268 					{
269 						*pStates = beans::PropertyState_DIRECT_VALUE;
270 					}
271 				}
272 				break;
273 			default:
274                 // OD 13.10.2003 #i18732# - correction:
275                 // use method <IsStaticDefaultItem(..)> instead of using probably
276                 // incompatible item pool <mpDefaultPool>.
277                 const SfxPoolItem& r1 = pPool->GetDefaultItem( nWhich );
278                 //const SfxPoolItem& r2 = mpDefaultPool->GetDefaultItem( nWhich );
279 
280                 if ( IsStaticDefaultItem( &r1 ) )
281 				{
282 					*pStates = beans::PropertyState_DEFAULT_VALUE;
283 				}
284 				else
285 				{
286 					*pStates = beans::PropertyState_DIRECT_VALUE;
287 				}
288 			}
289 
290 			pStates++;
291 			ppEntries++;
292 		}
293 	}
294 	else
295 	{
296 		// as long as we have no model, all properties are default
297 		while( *ppEntries++ )
298 			*pStates++ = beans::PropertyState_DEFAULT_VALUE;
299 		return;
300 	}
301 }
302 
303 void SvxUnoDrawPool::_setPropertyToDefault( const comphelper::PropertyMapEntry* pEntry )
304 	throw(beans::UnknownPropertyException )
305 {
306 	vos::OGuard aGuard( Application::GetSolarMutex() );
307 
308 	SfxItemPool* pPool = getModelPool( sal_True );
309 
310     // OD 10.10.2003 #i18732#
311     // Assure, that ID is a Which-ID (it could be a Slot-ID.)
312     // Thus, convert handle to Which-ID.
313     const sal_uInt16 nWhich = pPool->GetWhich( (sal_uInt16)pEntry->mnHandle );
314     if ( pPool && pPool != mpDefaultsPool )
315     {
316         // OD 13.10.2003 #i18732# - use method <ResetPoolDefaultItem(..)>
317         // instead of using probably incompatible item pool <mpDefaultsPool>.
318         pPool->ResetPoolDefaultItem( nWhich );
319     }
320 }
321 
322 uno::Any SvxUnoDrawPool::_getPropertyDefault( const comphelper::PropertyMapEntry* pEntry )
323 	throw(beans::UnknownPropertyException, lang::WrappedTargetException )
324 {
325 	vos::OGuard aGuard( Application::GetSolarMutex() );
326 
327     // OD 13.10.2003 #i18732# - use method <GetPoolDefaultItem(..)> instead of
328     // using probably incompatible item pool <mpDefaultsPool>
329     uno::Any aAny;
330     SfxItemPool* pPool = getModelPool( sal_True );
331     const sal_uInt16 nWhich = pPool->GetWhich( (sal_uInt16)pEntry->mnHandle );
332     const SfxPoolItem *pItem = pPool->GetPoolDefaultItem ( nWhich );
333     pItem->QueryValue( aAny, pEntry->mnMemberId );
334 
335     return aAny;
336 }
337 
338 // XInterface
339 
340 uno::Any SAL_CALL SvxUnoDrawPool::queryInterface( const uno::Type & rType )
341 	throw( uno::RuntimeException )
342 {
343 	return OWeakAggObject::queryInterface( rType );
344 }
345 
346 uno::Any SAL_CALL SvxUnoDrawPool::queryAggregation( const uno::Type & rType )
347 	throw(uno::RuntimeException)
348 {
349 	uno::Any aAny;
350 
351 	if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
352 		aAny <<= uno::Reference< lang::XServiceInfo >(this);
353 	else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
354 		aAny <<= uno::Reference< lang::XTypeProvider >(this);
355 	else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
356 		aAny <<= uno::Reference< beans::XPropertySet >(this);
357 	else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
358 		aAny <<= uno::Reference< beans::XPropertyState >(this);
359 	else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
360 		aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
361 	else
362 		aAny <<= OWeakAggObject::queryAggregation( rType );
363 
364 	return aAny;
365 }
366 
367 void SAL_CALL SvxUnoDrawPool::acquire() throw ( )
368 {
369 	OWeakAggObject::acquire();
370 }
371 
372 void SAL_CALL SvxUnoDrawPool::release() throw ( )
373 {
374 	OWeakAggObject::release();
375 }
376 
377 uno::Sequence< uno::Type > SAL_CALL SvxUnoDrawPool::getTypes()
378 	throw (uno::RuntimeException)
379 {
380 	uno::Sequence< uno::Type > aTypes( 6 );
381 	uno::Type* pTypes = aTypes.getArray();
382 
383 	*pTypes++ = ::getCppuType((const uno::Reference< uno::XAggregation>*)0);
384 	*pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
385 	*pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
386 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
387 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
388 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
389 
390 	return aTypes;
391 }
392 
393 uno::Sequence< sal_Int8 > SAL_CALL SvxUnoDrawPool::getImplementationId()
394 	throw (uno::RuntimeException)
395 {
396 	vos::OGuard aGuard( Application::GetSolarMutex() );
397 
398 	static uno::Sequence< sal_Int8 > aId;
399 	if( aId.getLength() == 0 )
400 	{
401 		aId.realloc( 16 );
402 		rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
403 	}
404 	return aId;
405 }
406 
407 // XServiceInfo
408 
409 sal_Bool SAL_CALL SvxUnoDrawPool::supportsService( const  OUString& ServiceName ) throw(uno::RuntimeException)
410 {
411     uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
412     const OUString * pArray = aSNL.getConstArray();
413 
414     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
415         if( pArray[i] == ServiceName )
416             return sal_True;
417 
418     return sal_False;
419 }
420 
421 OUString SAL_CALL SvxUnoDrawPool::getImplementationName() throw( uno::RuntimeException )
422 {
423 	return OUString( RTL_CONSTASCII_USTRINGPARAM("SvxUnoDrawPool") );
424 }
425 
426 uno::Sequence< OUString > SAL_CALL SvxUnoDrawPool::getSupportedServiceNames(  )
427 	throw( uno::RuntimeException )
428 {
429     uno::Sequence< OUString > aSNS( 1 );
430     aSNS.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.Defaults" ));
431     return aSNS;
432 }
433