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