merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / items1 / slstitm.cxx
blob4a7b6a066439c0f2dba9f1c44755712fe76507cc
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: slstitm.cxx,v $
10 * $Revision: 1.7 $
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/slstitm.hxx>
35 #include <svtools/poolitem.hxx>
36 #include <com/sun/star/uno/Any.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
38 #include <tools/stream.hxx>
40 // STATIC DATA -----------------------------------------------------------
42 DBG_NAME(SfxStringListItem)
44 // -----------------------------------------------------------------------
46 TYPEINIT1_AUTOFACTORY(SfxStringListItem, SfxPoolItem);
48 class SfxImpStringList
50 public:
51 USHORT nRefCount;
52 List aList;
54 SfxImpStringList() { nRefCount = 1; }
55 ~SfxImpStringList();
56 void Sort( BOOL bAscending, List* );
59 //------------------------------------------------------------------------
61 SfxImpStringList::~SfxImpStringList()
63 DBG_ASSERT(nRefCount!=0xffff,"ImpList already deleted");
64 String* pStr = (String*)aList.First();
65 while( pStr )
67 delete pStr;
68 pStr = (String*)aList.Next();
70 nRefCount = 0xffff;
73 //------------------------------------------------------------------------
75 void SfxImpStringList::Sort( BOOL bAscending, List* pParallelList )
77 DBG_ASSERT(!pParallelList || pParallelList->Count() >= aList.Count(),"Sort:ParallelList too small");
78 ULONG nCount = aList.Count();
79 if( nCount > 1 )
81 nCount -= 2;
82 // Bubble Dir Einen
83 BOOL bSwapped = TRUE;
84 while( bSwapped )
86 bSwapped = FALSE;
87 for( ULONG nCur = 0; nCur <= nCount; nCur++ )
89 String* pStr1 = (String*)aList.GetObject( nCur );
90 String* pStr2 = (String*)aList.GetObject( nCur+1 );
91 // COMPARE_GREATER => pStr2 ist groesser als pStr1
92 StringCompare eCompare = pStr1->CompareIgnoreCaseToAscii( *pStr2 ); //@@@
93 BOOL bSwap = FALSE;
94 if( bAscending )
96 if( eCompare == COMPARE_LESS )
97 bSwap = TRUE;
99 else if( eCompare == COMPARE_GREATER )
100 bSwap = TRUE;
102 if( bSwap )
104 bSwapped = TRUE;
105 aList.Replace( pStr1, nCur + 1 );
106 aList.Replace( pStr2, nCur );
107 if( pParallelList )
109 void* p1 = pParallelList->GetObject( nCur );
110 void* p2 = pParallelList->GetObject( nCur + 1 );
111 pParallelList->Replace( p1, nCur + 1 );
112 pParallelList->Replace( p2, nCur );
120 // class SfxStringListItem -----------------------------------------------
122 SfxStringListItem::SfxStringListItem() :
123 pImp(NULL)
127 //------------------------------------------------------------------------
129 SfxStringListItem::SfxStringListItem( USHORT which, const List* pList ) :
130 SfxPoolItem( which ),
131 pImp(NULL)
133 // PB: das Putten einer leeren Liste funktionierte nicht,
134 // deshalb habe ich hier die Abfrage nach dem Count auskommentiert
135 if( pList /*!!! && pList->Count() */ )
137 pImp = new SfxImpStringList;
139 long i, nCount = pList->Count();
140 String *pStr1, *pStr2;
141 for( i=0; i < nCount; i++ )
143 pStr1 = (String*)pList->GetObject(i);
144 pStr2 = new String( *pStr1 );
145 pImp->aList.Insert( pStr2, LIST_APPEND );
150 //------------------------------------------------------------------------
152 SfxStringListItem::SfxStringListItem( USHORT which, SvStream& rStream ) :
153 SfxPoolItem( which ),
154 pImp(NULL)
156 long nEntryCount;
157 rStream >> nEntryCount;
159 if( nEntryCount )
160 pImp = new SfxImpStringList;
162 long i;
163 String* pStr;
164 for( i=0; i < nEntryCount; i++ )
166 pStr = new String;
167 readByteString(rStream, *pStr);
168 pImp->aList.Insert( pStr, LIST_APPEND );
172 //------------------------------------------------------------------------
174 SfxStringListItem::SfxStringListItem( const SfxStringListItem& rItem ) :
175 SfxPoolItem( rItem ),
176 pImp(NULL)
178 pImp = rItem.pImp;
180 if( pImp )
182 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
183 pImp->nRefCount++;
187 //------------------------------------------------------------------------
189 SfxStringListItem::~SfxStringListItem()
191 if( pImp )
193 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
194 if( pImp->nRefCount > 1 )
195 pImp->nRefCount--;
196 else
197 delete pImp;
201 //------------------------------------------------------------------------
203 List* SfxStringListItem::GetList()
205 if( !pImp )
206 pImp = new SfxImpStringList;
207 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
208 return &(pImp->aList);
211 //------------------------------------------------------------------------
213 int SfxStringListItem::operator==( const SfxPoolItem& rItem ) const
215 DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal type" );
217 SfxStringListItem* pItem = (SfxStringListItem*)&rItem;
219 if( pImp == pItem->pImp )
220 return TRUE;
221 else
222 return FALSE;
225 //------------------------------------------------------------------------
227 SfxItemPresentation SfxStringListItem::GetPresentation
229 SfxItemPresentation /*ePresentation*/,
230 SfxMapUnit /*eCoreMetric*/,
231 SfxMapUnit /*ePresentationMetric*/,
232 XubString& rText,
233 const IntlWrapper *
234 ) const
236 rText.AssignAscii(RTL_CONSTASCII_STRINGPARAM("(List)"));
237 return SFX_ITEM_PRESENTATION_NONE;
240 //------------------------------------------------------------------------
242 SfxPoolItem* SfxStringListItem::Clone( SfxItemPool *) const
244 return new SfxStringListItem( *this );
246 if( pImp )
247 return new SfxStringListItem( Which(), &(pImp->aList) );
248 else
249 return new SfxStringListItem( Which(), NULL );
254 //------------------------------------------------------------------------
256 SfxPoolItem* SfxStringListItem::Create( SvStream & rStream, USHORT ) const
258 return new SfxStringListItem( Which(), rStream );
261 //------------------------------------------------------------------------
263 SvStream& SfxStringListItem::Store( SvStream & rStream, USHORT ) const
265 if( !pImp )
267 rStream << 0L;
268 return rStream;
271 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
273 long nCount = pImp->aList.Count();
274 rStream << nCount;
276 long i;
277 String* pStr;
278 for( i=0; i < nCount; i++ )
280 pStr = (String*)(pImp->aList.GetObject( i ));
281 writeByteString(rStream, *pStr);
284 return rStream;
287 //------------------------------------------------------------------------
289 void SfxStringListItem::SetString( const XubString& rStr )
291 DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
293 if ( pImp && (pImp->nRefCount == 1) )
294 delete pImp;
295 else
296 if( pImp )
297 pImp->nRefCount--;
298 pImp = new SfxImpStringList;
300 xub_StrLen nStart = 0;
301 xub_StrLen nDelimPos;
302 XubString aStr(rStr);
303 aStr.ConvertLineEnd(LINEEND_CR);
306 nDelimPos = aStr.Search( _CR, nStart );
307 xub_StrLen nLen;
308 if ( nDelimPos == STRING_NOTFOUND )
309 nLen = 0xffff;
310 else
311 nLen = nDelimPos - nStart;
313 XubString* pStr = new XubString(aStr.Copy(nStart, nLen));
314 // String gehoert der Liste
315 pImp->aList.Insert( pStr, LIST_APPEND );
317 nStart += nLen + 1 ; // delimiter ueberspringen
318 } while( nDelimPos != STRING_NOTFOUND );
320 // Kein Leerstring am Ende
321 if( pImp->aList.Last() &&
322 !((XubString*)pImp->aList.Last())->Len() )
323 delete (XubString*)pImp->aList.Remove( pImp->aList.Count()-1 );
326 //------------------------------------------------------------------------
328 XubString SfxStringListItem::GetString()
330 XubString aStr;
331 if ( pImp )
333 DBG_ASSERT(pImp->nRefCount!=0xffff,"ImpList not valid");
334 XubString* pStr = (XubString*)(pImp->aList.First());
335 while( pStr )
337 aStr += *pStr;
338 pStr = (XubString*)(pImp->aList.Next());
339 if ( pStr )
340 aStr += '\r';
343 aStr.ConvertLineEnd();
344 return aStr;
347 //------------------------------------------------------------------------
349 #ifndef TF_POOLABLE
351 int SfxStringListItem::IsPoolable() const
353 return FALSE;
356 #endif
358 //------------------------------------------------------------------------
360 void SfxStringListItem::Sort( BOOL bAscending, List* pParallelList )
362 DBG_ASSERT(GetRefCount()==0,"Sort:RefCount!=0");
363 if( pImp )
364 pImp->Sort( bAscending, pParallelList );
367 //----------------------------------------------------------------------------
368 void SfxStringListItem::SetStringList( const com::sun::star::uno::Sequence< rtl::OUString >& rList )
370 DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
372 if ( pImp && (pImp->nRefCount == 1) )
373 delete pImp;
374 else
375 if( pImp )
376 pImp->nRefCount--;
377 pImp = new SfxImpStringList;
379 for ( sal_Int32 n = 0; n < rList.getLength(); n++ )
381 XubString* pStr = new XubString( rList[n] );
382 // String gehoert der Liste
383 pImp->aList.Insert( pStr, LIST_APPEND );
387 //----------------------------------------------------------------------------
388 void SfxStringListItem::GetStringList( com::sun::star::uno::Sequence< rtl::OUString >& rList ) const
390 long nCount = pImp->aList.Count();
392 rList.realloc( nCount );
393 for( long i=0; i < nCount; i++ )
394 rList[i] = *(String*)(pImp->aList.GetObject( i ));
397 //----------------------------------------------------------------------------
398 // virtual
399 BOOL SfxStringListItem::PutValue( const com::sun::star::uno::Any& rVal,BYTE )
401 com::sun::star::uno::Sequence< rtl::OUString > aValue;
402 if ( rVal >>= aValue )
404 SetStringList( aValue );
405 return TRUE;
408 DBG_ERROR( "SfxStringListItem::PutValue - Wrong type!" );
409 return FALSE;
412 //----------------------------------------------------------------------------
413 // virtual
414 BOOL SfxStringListItem::QueryValue( com::sun::star::uno::Any& rVal,BYTE ) const
416 // GetString() is not const!!!
417 SfxStringListItem* pThis = const_cast< SfxStringListItem * >( this );
419 com::sun::star::uno::Sequence< rtl::OUString > aStringList;
420 pThis->GetStringList( aStringList );
421 rVal = ::com::sun::star::uno::makeAny( aStringList );
422 return TRUE;