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: aeitem.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"
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
57 SV_DECL_PTRARR_DEL(SfxAllEnumValueArr
, SfxAllEnumValue_Impl
*, 0, 8)
58 SV_IMPL_PTRARR(SfxAllEnumValueArr
, SfxAllEnumValue_Impl
*)
60 // -----------------------------------------------------------------------
62 SfxAllEnumItem::SfxAllEnumItem() :
69 SfxAllEnumItem::SfxAllEnumItem( USHORT which
, USHORT nVal
, const XubString
&rText
):
70 SfxEnumItem(which
, nVal
),
74 DBG_CTOR(SfxAllEnumItem
, 0);
75 InsertValue( nVal
, rText
);
78 // -----------------------------------------------------------------------
80 SfxAllEnumItem::SfxAllEnumItem(USHORT which
, USHORT nVal
):
81 SfxEnumItem(which
, nVal
),
85 DBG_CTOR(SfxAllEnumItem
, 0);
89 // -----------------------------------------------------------------------
91 SfxAllEnumItem::SfxAllEnumItem( USHORT which
, SvStream
&rStream
):
92 SfxEnumItem(which
, rStream
),
96 DBG_CTOR(SfxAllEnumItem
, 0);
97 InsertValue( GetValue() );
100 // -----------------------------------------------------------------------
103 SfxAllEnumItem::SfxAllEnumItem(USHORT which
):
104 SfxEnumItem(which
, 0),
108 DBG_CTOR(SfxAllEnumItem
, 0);
112 // -----------------------------------------------------------------------
114 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem
&rCopy
):
119 DBG_CTOR(SfxAllEnumItem
, 0);
120 if ( !rCopy
.pValues
)
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
),
145 // -----------------------------------------------------------------------
147 SfxAllEnumItem::~SfxAllEnumItem()
149 DBG_DTOR(SfxAllEnumItem
, 0);
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
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);
214 //!O: binaere Suche oder SortArray verwenden
216 for ( nPos
= 0; nPos
< pValues
->Count(); ++nPos
)
217 if ( pValues
->GetObject(nPos
)->nValue
>= nVal
)
222 // -----------------------------------------------------------------------
224 USHORT
SfxAllEnumItem::GetPosByValue( USHORT nValue
) const
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() )
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
;
252 pValues
= new SfxAllEnumValueArr
;
253 else if ( GetPosByValue( nValue
) != USHRT_MAX
)
254 // remove when exists
255 RemoveValue( nValue
);
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
;
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
)
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);
313 pValues
->DeleteAndDestroy( 0, pValues
->Count() );