Update ooo320-m1
[ooovba.git] / svtools / source / items1 / itemprop.cxx
blobb9d0e82ceb9c7064eed31ecdd16c9d680fa55489
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: itemprop.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include <svtools/itemprop.hxx>
35 #include <svtools/itempool.hxx>
36 #include <svtools/itemset.hxx>
37 #include <com/sun/star/beans/PropertyAttribute.hpp>
38 #include <hash_map>
39 /*************************************************************************
40 UNO III Implementation
41 *************************************************************************/
42 using namespace com::sun::star;
43 using namespace com::sun::star::beans;
44 using namespace com::sun::star::lang;
45 using namespace com::sun::star::uno;
46 using namespace ::rtl;
47 /*-- 16.02.2009 10:03:55---------------------------------------------------
49 -----------------------------------------------------------------------*/
51 struct equalOUString
53 bool operator()(const ::rtl::OUString& r1, const ::rtl::OUString& r2) const
55 return r1.equals( r2 );
59 typedef ::std::hash_map< ::rtl::OUString,
60 SfxItemPropertySimpleEntry,
61 ::rtl::OUStringHash,
62 equalOUString > SfxItemPropertyHashMap_t;
64 class SfxItemPropertyMap_Impl : public SfxItemPropertyHashMap_t
66 public:
67 mutable uno::Sequence< beans::Property > m_aPropSeq;
69 SfxItemPropertyMap_Impl(){}
70 SfxItemPropertyMap_Impl( const SfxItemPropertyMap_Impl* pSource );
72 SfxItemPropertyMap_Impl::SfxItemPropertyMap_Impl( const SfxItemPropertyMap_Impl* pSource )
74 this->SfxItemPropertyHashMap_t::operator=( *pSource );
75 m_aPropSeq = pSource->m_aPropSeq;
78 /*-- 16.02.2009 10:03:51---------------------------------------------------
80 -----------------------------------------------------------------------*/
81 SfxItemPropertyMap::SfxItemPropertyMap( const SfxItemPropertyMapEntry* pEntries ) :
82 m_pImpl( new SfxItemPropertyMap_Impl )
84 while( pEntries->pName )
86 ::rtl::OUString sEntry(pEntries->pName, pEntries->nNameLen, RTL_TEXTENCODING_ASCII_US );
87 (*m_pImpl) [ sEntry ] = pEntries;
88 ++pEntries;
91 /*-- 16.02.2009 12:46:41---------------------------------------------------
93 -----------------------------------------------------------------------*/
94 SfxItemPropertyMap::SfxItemPropertyMap( const SfxItemPropertyMap* pSource ) :
95 m_pImpl( new SfxItemPropertyMap_Impl( pSource->m_pImpl ) )
98 /*-- 16.02.2009 10:03:51---------------------------------------------------
100 -----------------------------------------------------------------------*/
101 SfxItemPropertyMap::~SfxItemPropertyMap()
103 delete m_pImpl;
105 /*-- 16.02.2009 10:03:51---------------------------------------------------
107 -----------------------------------------------------------------------*/
108 const SfxItemPropertySimpleEntry* SfxItemPropertyMap::getByName( const ::rtl::OUString &rName ) const
110 SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
111 if( aIter == m_pImpl->end() )
112 return 0;
113 return &aIter->second;
116 /*-- 16.02.2009 10:44:24---------------------------------------------------
118 -----------------------------------------------------------------------*/
119 uno::Sequence<beans::Property> SfxItemPropertyMap::getProperties() const
121 if( !m_pImpl->m_aPropSeq.getLength() )
123 m_pImpl->m_aPropSeq.realloc( m_pImpl->size() );
124 beans::Property* pPropArray = m_pImpl->m_aPropSeq.getArray();
125 sal_uInt32 n = 0;
126 SfxItemPropertyHashMap_t::const_iterator aIt = m_pImpl->begin();
127 while( aIt != m_pImpl->end() )
128 //for ( const SfxItemPropertyMap *pMap = _pMap; pMap->pName; ++pMap )
130 const SfxItemPropertySimpleEntry* pEntry = &(*aIt).second;
131 pPropArray[n].Name = (*aIt).first;
132 pPropArray[n].Handle = pEntry->nWID;
133 if(pEntry->pType)
134 pPropArray[n].Type = *pEntry->pType;
135 pPropArray[n].Attributes =
136 sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
137 n++;
138 ++aIt;
142 return m_pImpl->m_aPropSeq;
144 /*-- 16.02.2009 11:04:31---------------------------------------------------
146 -----------------------------------------------------------------------*/
147 beans::Property SfxItemPropertyMap::getPropertyByName( const ::rtl::OUString rName ) const
148 throw( beans::UnknownPropertyException )
150 SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
151 if( aIter == m_pImpl->end() )
152 throw UnknownPropertyException();
153 const SfxItemPropertySimpleEntry* pEntry = &aIter->second;
154 beans::Property aProp;
155 aProp.Name = rName;
156 aProp.Handle = pEntry->nWID;
157 if(pEntry->pType)
158 aProp.Type = *pEntry->pType;
159 aProp.Attributes = sal::static_int_cast< sal_Int16 >(pEntry->nFlags);
160 return aProp;
162 /*-- 16.02.2009 11:09:16---------------------------------------------------
164 -----------------------------------------------------------------------*/
165 sal_Bool SfxItemPropertyMap::hasPropertyByName( const ::rtl::OUString& rName ) const
167 SfxItemPropertyHashMap_t::const_iterator aIter = m_pImpl->find(rName);
168 return aIter != m_pImpl->end();
170 /*-- 16.02.2009 11:25:14---------------------------------------------------
172 -----------------------------------------------------------------------*/
173 void SfxItemPropertyMap::mergeProperties( const uno::Sequence< beans::Property >& rPropSeq )
175 const beans::Property* pPropArray = rPropSeq.getConstArray();
176 sal_uInt32 nElements = rPropSeq.getLength();
177 for( sal_uInt32 nElement = 0; nElement < nElements; ++nElement )
179 SfxItemPropertySimpleEntry aTemp(
180 sal::static_int_cast< sal_Int16 >( pPropArray[nElement].Handle ), //nWID
181 &pPropArray[nElement].Type, //pType
182 pPropArray[nElement].Attributes, //nFlags
183 0 ); //nMemberId
184 (*m_pImpl)[pPropArray[nElement].Name] = aTemp;
187 /*-- 18.02.2009 12:04:42---------------------------------------------------
189 -----------------------------------------------------------------------*/
190 PropertyEntryVector_t SfxItemPropertyMap::getPropertyEntries() const
192 PropertyEntryVector_t aRet;
193 aRet.reserve(m_pImpl->size());
195 SfxItemPropertyHashMap_t::const_iterator aIt = m_pImpl->begin();
196 while( aIt != m_pImpl->end() )
198 const SfxItemPropertySimpleEntry* pEntry = &(*aIt).second;
199 aRet.push_back( SfxItemPropertyNamedEntry( (*aIt).first, * pEntry ) );
200 ++aIt;
202 return aRet;
204 /*-- 18.02.2009 15:11:06---------------------------------------------------
206 -----------------------------------------------------------------------*/
207 sal_uInt32 SfxItemPropertyMap::getSize() const
209 return m_pImpl->size();
211 /*-- 16.02.2009 13:44:54---------------------------------------------------
213 -----------------------------------------------------------------------*/
214 SfxItemPropertySet::~SfxItemPropertySet()
217 /* -----------------------------21.02.00 11:26--------------------------------
219 ---------------------------------------------------------------------------*/
220 BOOL SfxItemPropertySet::FillItem(SfxItemSet&, USHORT, BOOL) const
222 return FALSE;
224 /* -----------------------------06.06.01 12:32--------------------------------
226 ---------------------------------------------------------------------------*/
227 void SfxItemPropertySet::getPropertyValue( const SfxItemPropertySimpleEntry& rEntry,
228 const SfxItemSet& rSet, Any& rAny ) const
229 throw(RuntimeException)
231 // get the SfxPoolItem
232 const SfxPoolItem* pItem = 0;
233 SfxItemState eState = rSet.GetItemState( rEntry.nWID, TRUE, &pItem );
234 if(SFX_ITEM_SET != eState && SFX_WHICH_MAX > rEntry.nWID )
235 pItem = &rSet.GetPool()->GetDefaultItem(rEntry.nWID);
236 // return item values as uno::Any
237 if(eState >= SFX_ITEM_DEFAULT && pItem)
239 pItem->QueryValue( rAny, rEntry.nMemberId );
241 else
243 SfxItemSet aSet(*rSet.GetPool(), rEntry.nWID, rEntry.nWID);
244 if(FillItem(aSet, rEntry.nWID, TRUE))
246 const SfxPoolItem& rItem = aSet.Get(rEntry.nWID);
247 rItem.QueryValue( rAny, rEntry.nMemberId );
249 else if(0 == (rEntry.nFlags & PropertyAttribute::MAYBEVOID))
250 throw RuntimeException();
254 // convert general SfxEnumItem values to specific values
255 if( rEntry.pType && TypeClass_ENUM == rEntry.pType->getTypeClass() &&
256 rAny.getValueTypeClass() == TypeClass_LONG )
258 INT32 nTmp = *(INT32*)rAny.getValue();
259 rAny.setValue( &nTmp, *rEntry.pType );
262 /* -----------------------------06.06.01 12:32--------------------------------
264 ---------------------------------------------------------------------------*/
265 void SfxItemPropertySet::getPropertyValue( const OUString &rName,
266 const SfxItemSet& rSet, Any& rAny ) const
267 throw(RuntimeException, UnknownPropertyException)
269 // detect which-id
270 const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
271 if ( !pEntry )
272 throw UnknownPropertyException();
273 getPropertyValue( *pEntry,rSet, rAny );
275 /* -----------------------------21.02.00 11:26--------------------------------
277 ---------------------------------------------------------------------------*/
278 Any SfxItemPropertySet::getPropertyValue( const OUString &rName,
279 const SfxItemSet& rSet ) const
280 throw(RuntimeException, UnknownPropertyException)
282 Any aVal;
283 getPropertyValue( rName,rSet, aVal );
284 return aVal;
286 /* -----------------------------15.11.00 14:46--------------------------------
288 ---------------------------------------------------------------------------*/
289 void SfxItemPropertySet::setPropertyValue( const SfxItemPropertySimpleEntry& rEntry,
290 const Any& aVal,
291 SfxItemSet& rSet ) const
292 throw(RuntimeException,
293 IllegalArgumentException)
295 // get the SfxPoolItem
296 const SfxPoolItem* pItem = 0;
297 SfxPoolItem *pNewItem = 0;
298 SfxItemState eState = rSet.GetItemState( rEntry.nWID, TRUE, &pItem );
299 if(SFX_ITEM_SET != eState && SFX_WHICH_MAX > rEntry.nWID )
300 pItem = &rSet.GetPool()->GetDefaultItem(rEntry.nWID);
301 //maybe there's another way to find an Item
302 if(eState < SFX_ITEM_DEFAULT)
304 SfxItemSet aSet(*rSet.GetPool(), rEntry.nWID, rEntry.nWID);
305 if(FillItem(aSet, rEntry.nWID, FALSE))
307 const SfxPoolItem &rItem = aSet.Get(rEntry.nWID);
308 pNewItem = rItem.Clone();
311 if(!pNewItem && pItem)
313 pNewItem = pItem->Clone();
315 if(pNewItem)
317 if( !pNewItem->PutValue( aVal, rEntry.nMemberId ) )
319 DELETEZ(pNewItem);
320 throw IllegalArgumentException();
322 // apply new item
323 rSet.Put( *pNewItem, rEntry.nWID );
324 delete pNewItem;
327 /* -----------------------------21.02.00 11:26--------------------------------
329 ---------------------------------------------------------------------------*/
330 void SfxItemPropertySet::setPropertyValue( const OUString &rName,
331 const Any& aVal,
332 SfxItemSet& rSet ) const
333 throw(RuntimeException,
334 IllegalArgumentException,
335 UnknownPropertyException)
337 const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
338 if ( !pEntry )
340 throw UnknownPropertyException();
342 setPropertyValue(*pEntry, aVal, rSet);
344 /* -----------------------------21.02.00 11:26--------------------------------
346 ---------------------------------------------------------------------------*/
347 PropertyState SfxItemPropertySet::getPropertyState(const SfxItemPropertySimpleEntry& rEntry, const SfxItemSet& rSet) const
348 throw()
350 PropertyState eRet = PropertyState_DIRECT_VALUE;
351 USHORT nWhich = rEntry.nWID;
353 // item state holen
354 SfxItemState eState = rSet.GetItemState( nWhich, FALSE );
355 // item-Wert als UnoAny zurueckgeben
356 if(eState == SFX_ITEM_DEFAULT)
357 eRet = PropertyState_DEFAULT_VALUE;
358 else if(eState < SFX_ITEM_DEFAULT)
359 eRet = PropertyState_AMBIGUOUS_VALUE;
360 return eRet;
362 PropertyState SfxItemPropertySet::getPropertyState(
363 const OUString& rName, const SfxItemSet& rSet) const
364 throw(UnknownPropertyException)
366 PropertyState eRet = PropertyState_DIRECT_VALUE;
368 // which-id ermitteln
369 const SfxItemPropertySimpleEntry* pEntry = m_aMap.getByName( rName );
370 if( !pEntry || !pEntry->nWID )
372 throw UnknownPropertyException();
374 USHORT nWhich = pEntry->nWID;
376 // item holen
377 const SfxPoolItem* pItem = 0;
378 SfxItemState eState = rSet.GetItemState( nWhich, FALSE, &pItem );
379 if(!pItem && nWhich != rSet.GetPool()->GetSlotId(nWhich))
380 pItem = &rSet.GetPool()->GetDefaultItem(nWhich);
381 // item-Wert als UnoAny zurueckgeben
382 if(eState == SFX_ITEM_DEFAULT)
383 eRet = PropertyState_DEFAULT_VALUE;
384 else if(eState < SFX_ITEM_DEFAULT)
385 eRet = PropertyState_AMBIGUOUS_VALUE;
386 return eRet;
388 /* -----------------------------21.02.00 11:26--------------------------------
390 ---------------------------------------------------------------------------*/
391 Reference<XPropertySetInfo>
392 SfxItemPropertySet::getPropertySetInfo() const
394 if( !m_xInfo.is() )
395 m_xInfo = new SfxItemPropertySetInfo( &m_aMap );
396 return m_xInfo;
398 /*-- 16.02.2009 13:49:25---------------------------------------------------
400 -----------------------------------------------------------------------*/
401 struct SfxItemPropertySetInfo_Impl
403 SfxItemPropertyMap* m_pOwnMap;
405 /*-- 16.02.2009 13:49:24---------------------------------------------------
407 -----------------------------------------------------------------------*/
408 SfxItemPropertySetInfo::SfxItemPropertySetInfo(const SfxItemPropertyMap *pMap ) :
409 m_pImpl( new SfxItemPropertySetInfo_Impl )
411 m_pImpl->m_pOwnMap = new SfxItemPropertyMap( pMap );
413 /*-- 16.02.2009 13:49:25---------------------------------------------------
415 -----------------------------------------------------------------------*/
416 SfxItemPropertySetInfo::SfxItemPropertySetInfo(const SfxItemPropertyMapEntry *pEntries ) :
417 m_pImpl( new SfxItemPropertySetInfo_Impl )
419 m_pImpl->m_pOwnMap = new SfxItemPropertyMap( pEntries );
421 /* -----------------------------21.02.00 11:09--------------------------------
423 ---------------------------------------------------------------------------*/
424 Sequence< Property > SAL_CALL
425 SfxItemPropertySetInfo::getProperties( )
426 throw(RuntimeException)
428 return m_pImpl->m_pOwnMap->getProperties();
430 /*-- 16.02.2009 13:49:27---------------------------------------------------
432 -----------------------------------------------------------------------*/
433 const SfxItemPropertyMap* SfxItemPropertySetInfo::getMap() const
435 return m_pImpl->m_pOwnMap;
438 /*-- 16.02.2009 12:43:36---------------------------------------------------
440 -----------------------------------------------------------------------*/
441 SfxItemPropertySetInfo::~SfxItemPropertySetInfo()
443 delete m_pImpl->m_pOwnMap;
444 delete m_pImpl;
446 /* -----------------------------21.02.00 11:27--------------------------------
448 ---------------------------------------------------------------------------*/
449 Property SAL_CALL
450 SfxItemPropertySetInfo::getPropertyByName( const ::rtl::OUString& rName )
451 throw(UnknownPropertyException, RuntimeException)
453 return m_pImpl->m_pOwnMap->getPropertyByName( rName );
455 /* -----------------------------21.02.00 11:28--------------------------------
457 ---------------------------------------------------------------------------*/
458 sal_Bool SAL_CALL
459 SfxItemPropertySetInfo::hasPropertyByName( const ::rtl::OUString& rName )
460 throw(RuntimeException)
462 return m_pImpl->m_pOwnMap->hasPropertyByName( rName );
464 /* -----------------------------21.02.00 12:03--------------------------------
466 ---------------------------------------------------------------------------*/
467 SfxExtItemPropertySetInfo::SfxExtItemPropertySetInfo(
468 const SfxItemPropertyMapEntry *pMap,
469 const Sequence<Property>& rPropSeq ) :
470 aExtMap( pMap )
472 aExtMap.mergeProperties( rPropSeq );
474 /*-- 16.02.2009 12:06:49---------------------------------------------------
476 -----------------------------------------------------------------------*/
477 SfxExtItemPropertySetInfo::~SfxExtItemPropertySetInfo()
480 /* -----------------------------21.02.00 12:03--------------------------------
482 ---------------------------------------------------------------------------*/
483 Sequence< Property > SAL_CALL
484 SfxExtItemPropertySetInfo::getProperties( ) throw(RuntimeException)
486 return aExtMap.getProperties();
488 /* -----------------------------21.02.00 12:03--------------------------------
490 ---------------------------------------------------------------------------*/
491 Property SAL_CALL
492 SfxExtItemPropertySetInfo::getPropertyByName( const OUString& rPropertyName )
493 throw(UnknownPropertyException, RuntimeException)
495 return aExtMap.getPropertyByName( rPropertyName );
497 /* -----------------------------21.02.00 12:03--------------------------------
499 ---------------------------------------------------------------------------*/
500 sal_Bool SAL_CALL
501 SfxExtItemPropertySetInfo::hasPropertyByName( const OUString& rPropertyName )
502 throw(RuntimeException)
504 return aExtMap.hasPropertyByName( rPropertyName );