merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / items / aeitem.cxx
blob36e06d1d77865c73f131a6e6dcd6f3c700790c35
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: aeitem.cxx,v $
10 * $Revision: 1.11 $
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"
33 #ifndef GCC
34 #endif
36 #include <tools/string.hxx>
38 #define _SVSTDARR_USHORTS
39 #include <svtools/svstdarr.hxx>
40 #include <svtools/svarray.hxx>
41 #include <svtools/aeitem.hxx>
43 // STATIC DATA -----------------------------------------------------------
45 DBG_NAME(SfxAllEnumItem)
47 TYPEINIT1_AUTOFACTORY(SfxAllEnumItem, SfxEnumItem)
49 // -----------------------------------------------------------------------
51 struct SfxAllEnumValue_Impl
53 USHORT nValue;
54 XubString aText;
57 SV_DECL_PTRARR_DEL(SfxAllEnumValueArr, SfxAllEnumValue_Impl*, 0, 8)
58 SV_IMPL_PTRARR(SfxAllEnumValueArr, SfxAllEnumValue_Impl*)
60 // -----------------------------------------------------------------------
62 SfxAllEnumItem::SfxAllEnumItem() :
63 SfxEnumItem(),
64 pValues( 0 ),
65 pDisabledValues( 0 )
69 SfxAllEnumItem::SfxAllEnumItem( USHORT which, USHORT nVal, const XubString &rText ):
70 SfxEnumItem(which, nVal),
71 pValues( 0 ),
72 pDisabledValues( 0 )
74 DBG_CTOR(SfxAllEnumItem, 0);
75 InsertValue( nVal, rText );
78 // -----------------------------------------------------------------------
80 SfxAllEnumItem::SfxAllEnumItem(USHORT which, USHORT nVal):
81 SfxEnumItem(which, nVal),
82 pValues( 0 ),
83 pDisabledValues( 0 )
85 DBG_CTOR(SfxAllEnumItem, 0);
86 InsertValue( nVal );
89 // -----------------------------------------------------------------------
91 SfxAllEnumItem::SfxAllEnumItem( USHORT which, SvStream &rStream ):
92 SfxEnumItem(which, rStream),
93 pValues( 0 ),
94 pDisabledValues( 0 )
96 DBG_CTOR(SfxAllEnumItem, 0);
97 InsertValue( GetValue() );
100 // -----------------------------------------------------------------------
103 SfxAllEnumItem::SfxAllEnumItem(USHORT which):
104 SfxEnumItem(which, 0),
105 pValues( 0 ),
106 pDisabledValues( 0 )
108 DBG_CTOR(SfxAllEnumItem, 0);
112 // -----------------------------------------------------------------------
114 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
115 SfxEnumItem(rCopy),
116 pValues(0),
117 pDisabledValues( 0 )
119 DBG_CTOR(SfxAllEnumItem, 0);
120 if ( !rCopy.pValues )
121 return;
123 pValues = new SfxAllEnumValueArr;
125 for ( USHORT nPos = 0; nPos < rCopy.pValues->Count(); ++nPos )
127 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
128 pVal->nValue = rCopy.pValues->GetObject(nPos)->nValue;
129 pVal->aText = rCopy.pValues->GetObject(nPos)->aText;
130 const SfxAllEnumValue_Impl *pTemp = pVal;
131 pValues->Insert( pTemp, nPos );
134 if( rCopy.pDisabledValues )
136 pDisabledValues = new SvUShorts;
137 for ( USHORT nPos = 0; nPos < rCopy.pDisabledValues->Count(); ++nPos )
139 pDisabledValues->Insert( rCopy.pDisabledValues->GetObject(nPos),
140 nPos );
145 // -----------------------------------------------------------------------
147 SfxAllEnumItem::~SfxAllEnumItem()
149 DBG_DTOR(SfxAllEnumItem, 0);
150 delete pValues;
151 delete pDisabledValues;
154 // -----------------------------------------------------------------------
156 USHORT SfxAllEnumItem::GetValueCount() const
158 DBG_CHKTHIS(SfxAllEnumItem, 0);
159 return pValues ? pValues->Count() : 0;
162 // -----------------------------------------------------------------------
164 XubString SfxAllEnumItem::GetValueTextByPos( USHORT nPos ) const
166 DBG_CHKTHIS(SfxAllEnumItem, 0);
167 DBG_ASSERT( pValues && nPos < pValues->Count(), "enum overflow" );
168 return pValues->GetObject(nPos)->aText;
171 // -----------------------------------------------------------------------
173 USHORT SfxAllEnumItem::GetValueByPos( USHORT nPos ) const
175 DBG_CHKTHIS(SfxAllEnumItem, 0);
176 DBG_ASSERT( pValues && nPos < pValues->Count(), "enum overflow" );
177 return pValues->GetObject(nPos)->nValue;
180 // -----------------------------------------------------------------------
182 SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
184 DBG_CHKTHIS(SfxAllEnumItem, 0);
185 return new SfxAllEnumItem(*this);
188 // -----------------------------------------------------------------------
190 SfxPoolItem* SfxAllEnumItem::Create( SvStream & rStream, USHORT ) const
192 DBG_CHKTHIS(SfxAllEnumItem, 0);
193 return new SfxAllEnumItem( Which(), rStream );
197 // -----------------------------------------------------------------------
199 USHORT SfxAllEnumItem::_GetPosByValue( USHORT nVal ) const
201 /* [Beschreibung]
203 Im Ggs. zu <SfxEnumItemInterface::GetPosByValue(USHORT)const> liefert
204 diese interne Methode bei nicht vorhandenen Values die Position,
205 an der der Wert liegen w"urde.
209 DBG_CHKTHIS(SfxAllEnumItem, 0);
211 if ( !pValues )
212 return 0;
214 //!O: binaere Suche oder SortArray verwenden
215 USHORT nPos;
216 for ( nPos = 0; nPos < pValues->Count(); ++nPos )
217 if ( pValues->GetObject(nPos)->nValue >= nVal )
218 return nPos;
219 return nPos;
222 // -----------------------------------------------------------------------
224 USHORT SfxAllEnumItem::GetPosByValue( USHORT nValue ) const
226 /* [Beschreibung]
228 Liefert im Gegensatz zu <SfxEnumItemInterface::GetPosByValue(USHORT)const>
229 immer nValue zur"uck, solange nicht mindestens ein Wert mit einer der
230 Methoden <SfxAllEnumItem::InsertValue()> eingef"ugt wurde.
234 DBG_CHKTHIS(SfxAllEnumItem, 0);
236 if ( !pValues || !pValues->Count() )
237 return nValue;
239 return SfxEnumItem::GetPosByValue( nValue );
242 // -----------------------------------------------------------------------
244 void SfxAllEnumItem::InsertValue( USHORT nValue, const XubString &rValue )
246 DBG_CHKTHIS(SfxAllEnumItem, 0);
247 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
248 pVal->nValue = nValue;
249 pVal->aText = rValue;
250 const SfxAllEnumValue_Impl *pTemp = pVal;
251 if ( !pValues )
252 pValues = new SfxAllEnumValueArr;
253 else if ( GetPosByValue( nValue ) != USHRT_MAX )
254 // remove when exists
255 RemoveValue( nValue );
256 // then insert
257 pValues->Insert( pTemp, _GetPosByValue(nValue) ); //! doppelte?!
260 // -----------------------------------------------------------------------
262 void SfxAllEnumItem::InsertValue( USHORT nValue )
264 DBG_CHKTHIS(SfxAllEnumItem, 0);
265 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
266 pVal->nValue = nValue;
267 pVal->aText = XubString::CreateFromInt32( nValue );
268 const SfxAllEnumValue_Impl *pTemp = pVal;
269 if ( !pValues )
270 pValues = new SfxAllEnumValueArr;
272 pValues->Insert( pTemp, _GetPosByValue(nValue) ); //! doppelte?!
275 void SfxAllEnumItem::DisableValue( USHORT nValue )
277 DBG_CHKTHIS(SfxAllEnumItem, 0);
278 if ( !pDisabledValues )
279 pDisabledValues = new SvUShorts;
281 pDisabledValues->Insert( nValue, pDisabledValues->Count() );
284 BOOL SfxAllEnumItem::IsEnabled( USHORT nValue ) const
286 if ( pDisabledValues )
288 for ( USHORT i=0; i<pDisabledValues->Count(); i++ )
289 if ( (*pDisabledValues)[i] == nValue )
290 return FALSE;
293 return TRUE;
296 // -----------------------------------------------------------------------
298 void SfxAllEnumItem::RemoveValue( USHORT nValue )
300 DBG_CHKTHIS(SfxAllEnumItem, 0);
301 USHORT nPos = GetPosByValue(nValue);
302 DBG_ASSERT( nPos != USHRT_MAX, "removing value not in enum" );
303 pValues->Remove( nPos );
306 // -----------------------------------------------------------------------
309 void SfxAllEnumItem::RemoveAllValues()
311 DBG_CHKTHIS(SfxAllEnumItem, 0);
312 if ( pValues )
313 pValues->DeleteAndDestroy( 0, pValues->Count() );