1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: slstitm.cxx,v $
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
54 SfxImpStringList() { nRefCount
= 1; }
56 void Sort( BOOL bAscending
, List
* );
59 //------------------------------------------------------------------------
61 SfxImpStringList::~SfxImpStringList()
63 DBG_ASSERT(nRefCount
!=0xffff,"ImpList already deleted");
64 String
* pStr
= (String
*)aList
.First();
68 pStr
= (String
*)aList
.Next();
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();
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
); //@@@
96 if( eCompare
== COMPARE_LESS
)
99 else if( eCompare
== COMPARE_GREATER
)
105 aList
.Replace( pStr1
, nCur
+ 1 );
106 aList
.Replace( pStr2
, nCur
);
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() :
127 //------------------------------------------------------------------------
129 SfxStringListItem::SfxStringListItem( USHORT which
, const List
* pList
) :
130 SfxPoolItem( which
),
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
),
157 rStream
>> nEntryCount
;
160 pImp
= new SfxImpStringList
;
164 for( i
=0; i
< nEntryCount
; i
++ )
167 readByteString(rStream
, *pStr
);
168 pImp
->aList
.Insert( pStr
, LIST_APPEND
);
172 //------------------------------------------------------------------------
174 SfxStringListItem::SfxStringListItem( const SfxStringListItem
& rItem
) :
175 SfxPoolItem( rItem
),
182 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
187 //------------------------------------------------------------------------
189 SfxStringListItem::~SfxStringListItem()
193 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
194 if( pImp
->nRefCount
> 1 )
201 //------------------------------------------------------------------------
203 List
* SfxStringListItem::GetList()
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
)
225 //------------------------------------------------------------------------
227 SfxItemPresentation
SfxStringListItem::GetPresentation
229 SfxItemPresentation
/*ePresentation*/,
230 SfxMapUnit
/*eCoreMetric*/,
231 SfxMapUnit
/*ePresentationMetric*/,
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 );
247 return new SfxStringListItem( Which(), &(pImp->aList) );
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
271 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
273 long nCount
= pImp
->aList
.Count();
278 for( i
=0; i
< nCount
; i
++ )
280 pStr
= (String
*)(pImp
->aList
.GetObject( i
));
281 writeByteString(rStream
, *pStr
);
287 //------------------------------------------------------------------------
289 void SfxStringListItem::SetString( const XubString
& rStr
)
291 DBG_ASSERT(GetRefCount()==0,"SetString:RefCount!=0");
293 if ( pImp
&& (pImp
->nRefCount
== 1) )
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
);
308 if ( nDelimPos
== STRING_NOTFOUND
)
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()
333 DBG_ASSERT(pImp
->nRefCount
!=0xffff,"ImpList not valid");
334 XubString
* pStr
= (XubString
*)(pImp
->aList
.First());
338 pStr
= (XubString
*)(pImp
->aList
.Next());
343 aStr
.ConvertLineEnd();
347 //------------------------------------------------------------------------
351 int SfxStringListItem::IsPoolable() const
358 //------------------------------------------------------------------------
360 void SfxStringListItem::Sort( BOOL bAscending
, List
* pParallelList
)
362 DBG_ASSERT(GetRefCount()==0,"Sort:RefCount!=0");
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) )
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 //----------------------------------------------------------------------------
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
);
408 DBG_ERROR( "SfxStringListItem::PutValue - Wrong type!" );
412 //----------------------------------------------------------------------------
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
);