xref: /trunk/main/svx/source/unodraw/unopool.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
111     switch( pEntry->mnHandle )
112     {
113     case OWN_ATTR_FILLBMP_MODE:
114         {
115             XFillBmpStretchItem* pStretchItem = (XFillBmpStretchItem*)&pPool->GetDefaultItem(XATTR_FILLBMP_STRETCH);
116             XFillBmpTileItem* pTileItem = (XFillBmpTileItem*)&pPool->GetDefaultItem(XATTR_FILLBMP_TILE);
117             if( pTileItem && pTileItem->GetValue() )
118             {
119                 rValue <<= drawing::BitmapMode_REPEAT;
120             }
121             else if( pStretchItem && pStretchItem->GetValue() )
122             {
123                 rValue <<= drawing::BitmapMode_STRETCH;
124             }
125             else
126             {
127                 rValue <<= drawing::BitmapMode_NO_REPEAT;
128             }
129             break;
130         }
131     default:
132         {
133             const SfxMapUnit eMapUnit = pPool ? pPool->GetMetric((sal_uInt16)pEntry->mnHandle) : SFX_MAPUNIT_100TH_MM;
134 
135             sal_uInt8 nMemberId = pEntry->mnMemberId & (~SFX_METRIC_ITEM);
136             if( eMapUnit == SFX_MAPUNIT_100TH_MM )
137                 nMemberId &= (~CONVERT_TWIPS);
138 
139             // DVO, OD 10.10.2003 #i18732#
140             // Assure, that ID is a Which-ID (it could be a Slot-ID.)
141             // Thus, convert handle to Which-ID.
142             pPool->GetDefaultItem( pPool->GetWhich( (sal_uInt16)pEntry->mnHandle ) ).QueryValue( rValue, nMemberId );
143         }
144     }
145 
146 
147     // check for needed metric translation
148     const SfxMapUnit eMapUnit = pPool->GetMetric((sal_uInt16)pEntry->mnHandle);
149     if(pEntry->mnMemberId & SFX_METRIC_ITEM && eMapUnit != SFX_MAPUNIT_100TH_MM)
150     {
151         SvxUnoConvertToMM( eMapUnit, rValue );
152     }
153     // convert int32 to correct enum type if needed
154     else if ( pEntry->mpType->getTypeClass() == uno::TypeClass_ENUM && rValue.getValueType() == ::getCppuType((const sal_Int32*)0) )
155     {
156         sal_Int32 nEnum;
157         rValue >>= nEnum;
158 
159         rValue.setValue( &nEnum, *pEntry->mpType );
160     }
161 }
162 
putAny(SfxItemPool * pPool,const comphelper::PropertyMapEntry * pEntry,const uno::Any & rValue)163 void SvxUnoDrawPool::putAny( SfxItemPool* pPool, const comphelper::PropertyMapEntry* pEntry, const uno::Any& rValue )
164 {
165     uno::Any aValue( rValue );
166 
167     const SfxMapUnit eMapUnit = pPool->GetMetric((sal_uInt16)pEntry->mnHandle);
168     if(pEntry->mnMemberId & SFX_METRIC_ITEM && eMapUnit != SFX_MAPUNIT_100TH_MM)
169     {
170         SvxUnoConvertFromMM( eMapUnit, aValue );
171     }
172 
173     // DVO, OD 10.10.2003 #i18732#
174     // Assure, that ID is a Which-ID (it could be a Slot-ID.)
175     // Thus, convert handle to Which-ID.
176     const sal_uInt16 nWhich = pPool->GetWhich( (sal_uInt16)pEntry->mnHandle );
177     switch( nWhich )
178     {
179         case OWN_ATTR_FILLBMP_MODE:
180             do
181             {
182                 drawing::BitmapMode eMode;
183                 if(!(aValue >>= eMode) )
184                 {
185                     sal_Int32 nMode = 0;
186                     if(!(aValue >>= nMode))
187                         throw lang::IllegalArgumentException();
188 
189                     eMode = (drawing::BitmapMode)nMode;
190                 }
191 
192                 pPool->SetPoolDefaultItem( XFillBmpStretchItem( eMode == drawing::BitmapMode_STRETCH ) );
193                 pPool->SetPoolDefaultItem( XFillBmpTileItem( eMode == drawing::BitmapMode_REPEAT ) );
194                 return;
195             }
196             while(0);
197 
198     default:
199         {
200             ::std::auto_ptr<SfxPoolItem> pNewItem( pPool->GetDefaultItem( nWhich ).Clone() );
201             sal_uInt8 nMemberId = pEntry->mnMemberId & (~SFX_METRIC_ITEM);
202             if( !pPool || (pPool->GetMetric(nWhich) == SFX_MAPUNIT_100TH_MM) )
203                 nMemberId &= (~CONVERT_TWIPS);
204 
205             if( !pNewItem->PutValue( aValue, nMemberId ) )
206                 throw lang::IllegalArgumentException();
207 
208             pPool->SetPoolDefaultItem( *pNewItem );
209         }
210     }
211 }
212 
_setPropertyValues(const comphelper::PropertyMapEntry ** ppEntries,const uno::Any * pValues)213 void SvxUnoDrawPool::_setPropertyValues( const comphelper::PropertyMapEntry** ppEntries, const uno::Any* pValues )
214 {
215     vos::OGuard aGuard( Application::GetSolarMutex() );
216 
217     SfxItemPool* pPool = getModelPool( sal_False );
218 
219     DBG_ASSERT( pPool, "I need a SfxItemPool!" );
220     if( NULL == pPool )
221         throw beans::UnknownPropertyException();
222 
223     while( *ppEntries )
224         putAny( pPool, *ppEntries++, *pValues++ );
225 }
226 
_getPropertyValues(const comphelper::PropertyMapEntry ** ppEntries,uno::Any * pValue)227 void SvxUnoDrawPool::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValue )
228 {
229     vos::OGuard aGuard( Application::GetSolarMutex() );
230 
231     SfxItemPool* pPool = getModelPool( sal_True );
232 
233     DBG_ASSERT( pPool, "I need a SfxItemPool!" );
234     if( NULL == pPool )
235         throw beans::UnknownPropertyException();
236 
237     while( *ppEntries )
238         getAny( pPool, *ppEntries++, *pValue++ );
239 }
240 
_getPropertyStates(const comphelper::PropertyMapEntry ** ppEntries,beans::PropertyState * pStates)241 void SvxUnoDrawPool::_getPropertyStates( const comphelper::PropertyMapEntry** ppEntries, beans::PropertyState* pStates )
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 
_setPropertyToDefault(const comphelper::PropertyMapEntry * pEntry)303 void SvxUnoDrawPool::_setPropertyToDefault( const comphelper::PropertyMapEntry* pEntry )
304 {
305     vos::OGuard aGuard( Application::GetSolarMutex() );
306 
307     SfxItemPool* pPool = getModelPool( sal_True );
308 
309     // OD 10.10.2003 #i18732#
310     // Assure, that ID is a Which-ID (it could be a Slot-ID.)
311     // Thus, convert handle to Which-ID.
312     const sal_uInt16 nWhich = pPool->GetWhich( (sal_uInt16)pEntry->mnHandle );
313     if ( pPool && pPool != mpDefaultsPool )
314     {
315         // OD 13.10.2003 #i18732# - use method <ResetPoolDefaultItem(..)>
316         // instead of using probably incompatible item pool <mpDefaultsPool>.
317         pPool->ResetPoolDefaultItem( nWhich );
318     }
319 }
320 
_getPropertyDefault(const comphelper::PropertyMapEntry * pEntry)321 uno::Any SvxUnoDrawPool::_getPropertyDefault( const comphelper::PropertyMapEntry* pEntry )
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 
queryInterface(const uno::Type & rType)338 uno::Any SAL_CALL SvxUnoDrawPool::queryInterface( const uno::Type & rType )
339 {
340     return OWeakAggObject::queryInterface( rType );
341 }
342 
queryAggregation(const uno::Type & rType)343 uno::Any SAL_CALL SvxUnoDrawPool::queryAggregation( const uno::Type & rType )
344 {
345     uno::Any aAny;
346 
347     if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
348         aAny <<= uno::Reference< lang::XServiceInfo >(this);
349     else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
350         aAny <<= uno::Reference< lang::XTypeProvider >(this);
351     else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
352         aAny <<= uno::Reference< beans::XPropertySet >(this);
353     else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
354         aAny <<= uno::Reference< beans::XPropertyState >(this);
355     else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
356         aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
357     else
358         aAny <<= OWeakAggObject::queryAggregation( rType );
359 
360     return aAny;
361 }
362 
acquire()363 void SAL_CALL SvxUnoDrawPool::acquire() throw ( )
364 {
365     OWeakAggObject::acquire();
366 }
367 
release()368 void SAL_CALL SvxUnoDrawPool::release() throw ( )
369 {
370     OWeakAggObject::release();
371 }
372 
getTypes()373 uno::Sequence< uno::Type > SAL_CALL SvxUnoDrawPool::getTypes()
374 {
375     uno::Sequence< uno::Type > aTypes( 6 );
376     uno::Type* pTypes = aTypes.getArray();
377 
378     *pTypes++ = ::getCppuType((const uno::Reference< uno::XAggregation>*)0);
379     *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
380     *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
381     *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
382     *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
383     *pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
384 
385     return aTypes;
386 }
387 
getImplementationId()388 uno::Sequence< sal_Int8 > SAL_CALL SvxUnoDrawPool::getImplementationId()
389 {
390     vos::OGuard aGuard( Application::GetSolarMutex() );
391 
392     static uno::Sequence< sal_Int8 > aId;
393     if( aId.getLength() == 0 )
394     {
395         aId.realloc( 16 );
396         rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
397     }
398     return aId;
399 }
400 
401 // XServiceInfo
402 
supportsService(const OUString & ServiceName)403 sal_Bool SAL_CALL SvxUnoDrawPool::supportsService( const  OUString& ServiceName )
404 {
405     uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
406     const OUString * pArray = aSNL.getConstArray();
407 
408     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
409         if( pArray[i] == ServiceName )
410             return sal_True;
411 
412     return sal_False;
413 }
414 
getImplementationName()415 OUString SAL_CALL SvxUnoDrawPool::getImplementationName()
416 {
417     return OUString( RTL_CONSTASCII_USTRINGPARAM("SvxUnoDrawPool") );
418 }
419 
getSupportedServiceNames()420 uno::Sequence< OUString > SAL_CALL SvxUnoDrawPool::getSupportedServiceNames(  )
421 {
422     uno::Sequence< OUString > aSNS( 1 );
423     aSNS.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.Defaults" ));
424     return aSNS;
425 }
426