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