xref: /aoo41x/main/svl/source/items/aeitem.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svl.hxx"
30 #ifndef GCC
31 #endif
32 
33 #include <tools/string.hxx>
34 
35 #define _SVSTDARR_USHORTS
36 #include <svl/svstdarr.hxx>
37 #include <svl/svarray.hxx>
38 #include <svl/aeitem.hxx>
39 
40 // STATIC DATA -----------------------------------------------------------
41 
42 DBG_NAME(SfxAllEnumItem)
43 
44 TYPEINIT1_AUTOFACTORY(SfxAllEnumItem, SfxEnumItem)
45 
46 // -----------------------------------------------------------------------
47 
48 struct SfxAllEnumValue_Impl
49 {
50 	sal_uInt16 nValue;
51 	XubString aText;
52 };
53 
54 SV_DECL_PTRARR_DEL(SfxAllEnumValueArr, SfxAllEnumValue_Impl*, 0, 8)
55 SV_IMPL_PTRARR(SfxAllEnumValueArr, SfxAllEnumValue_Impl*)
56 
57 // -----------------------------------------------------------------------
58 
59 SfxAllEnumItem::SfxAllEnumItem() :
60     SfxEnumItem(),
61     pValues( 0 ),
62     pDisabledValues( 0 )
63 {
64 }
65 
66 SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, sal_uInt16 nVal, const XubString &rText ):
67 	SfxEnumItem(which, nVal),
68 	pValues( 0 ),
69 	pDisabledValues( 0 )
70 {
71 	DBG_CTOR(SfxAllEnumItem, 0);
72 	InsertValue( nVal, rText );
73 }
74 
75 // -----------------------------------------------------------------------
76 
77 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which, sal_uInt16 nVal):
78 	SfxEnumItem(which, nVal),
79 	pValues( 0 ),
80 	pDisabledValues( 0 )
81 {
82 	DBG_CTOR(SfxAllEnumItem, 0);
83 	InsertValue( nVal );
84 }
85 
86 // -----------------------------------------------------------------------
87 
88 SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, SvStream &rStream ):
89 	SfxEnumItem(which, rStream),
90 	pValues( 0 ),
91 	pDisabledValues( 0 )
92 {
93 	DBG_CTOR(SfxAllEnumItem, 0);
94 	InsertValue( GetValue() );
95 }
96 
97 // -----------------------------------------------------------------------
98 
99 
100 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which):
101 	SfxEnumItem(which, 0),
102 	pValues( 0 ),
103 	pDisabledValues( 0 )
104 {
105 	DBG_CTOR(SfxAllEnumItem, 0);
106 }
107 
108 
109 // -----------------------------------------------------------------------
110 
111 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
112 	SfxEnumItem(rCopy),
113 	pValues(0),
114 	pDisabledValues( 0 )
115 {
116 	DBG_CTOR(SfxAllEnumItem, 0);
117 	if ( !rCopy.pValues )
118 		return;
119 
120 	pValues = new SfxAllEnumValueArr;
121 
122 	for ( sal_uInt16 nPos = 0; nPos < rCopy.pValues->Count(); ++nPos )
123 	{
124 		SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
125 		pVal->nValue = rCopy.pValues->GetObject(nPos)->nValue;
126 		pVal->aText = rCopy.pValues->GetObject(nPos)->aText;
127 		const SfxAllEnumValue_Impl *pTemp = pVal;
128 		pValues->Insert( pTemp, nPos );
129 	}
130 
131 	if( rCopy.pDisabledValues )
132 	{
133 		pDisabledValues = new SvUShorts;
134 		for ( sal_uInt16 nPos = 0; nPos < rCopy.pDisabledValues->Count(); ++nPos )
135 		{
136 			pDisabledValues->Insert( rCopy.pDisabledValues->GetObject(nPos),
137 									 nPos );
138 		}
139 	}
140 }
141 
142 // -----------------------------------------------------------------------
143 
144 SfxAllEnumItem::~SfxAllEnumItem()
145 {
146 	DBG_DTOR(SfxAllEnumItem, 0);
147 	delete pValues;
148 	delete pDisabledValues;
149 }
150 
151 // -----------------------------------------------------------------------
152 
153 sal_uInt16 SfxAllEnumItem::GetValueCount() const
154 {
155 	DBG_CHKTHIS(SfxAllEnumItem, 0);
156 	return pValues ? pValues->Count() : 0;
157 }
158 
159 // -----------------------------------------------------------------------
160 
161 XubString SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos ) const
162 {
163 	DBG_CHKTHIS(SfxAllEnumItem, 0);
164 	DBG_ASSERT( pValues && nPos < pValues->Count(), "enum overflow" );
165 	return pValues->GetObject(nPos)->aText;
166 }
167 
168 // -----------------------------------------------------------------------
169 
170 sal_uInt16 SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos ) const
171 {
172 	DBG_CHKTHIS(SfxAllEnumItem, 0);
173 	DBG_ASSERT( pValues && nPos < pValues->Count(), "enum overflow" );
174 	return pValues->GetObject(nPos)->nValue;
175 }
176 
177 // -----------------------------------------------------------------------
178 
179 SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
180 {
181 	DBG_CHKTHIS(SfxAllEnumItem, 0);
182 	return new SfxAllEnumItem(*this);
183 }
184 
185 // -----------------------------------------------------------------------
186 
187 SfxPoolItem* SfxAllEnumItem::Create( SvStream & rStream, sal_uInt16 ) const
188 {
189 	DBG_CHKTHIS(SfxAllEnumItem, 0);
190 	return new SfxAllEnumItem( Which(), rStream );
191 }
192 
193 
194 // -----------------------------------------------------------------------
195 
196 sal_uInt16 SfxAllEnumItem::_GetPosByValue( sal_uInt16 nVal ) const
197 
198 /*	[Beschreibung]
199 
200 	Im Ggs. zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const> liefert
201 	diese interne Methode bei nicht vorhandenen Values die Position,
202 	an der der Wert liegen w"urde.
203 */
204 
205 {
206 	DBG_CHKTHIS(SfxAllEnumItem, 0);
207 
208 	if ( !pValues )
209 		return 0;
210 
211 	//!O: binaere Suche oder SortArray verwenden
212 	sal_uInt16 nPos;
213 	for ( nPos = 0; nPos < pValues->Count(); ++nPos )
214 		if ( pValues->GetObject(nPos)->nValue >= nVal )
215 			return nPos;
216 	return nPos;
217 }
218 
219 // -----------------------------------------------------------------------
220 
221 sal_uInt16 SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue ) const
222 
223 /*  [Beschreibung]
224 
225 	Liefert im Gegensatz zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const>
226 	immer nValue zur"uck, solange nicht mindestens ein Wert mit einer der
227 	Methoden <SfxAllEnumItem::InsertValue()> eingef"ugt wurde.
228 */
229 
230 {
231 	DBG_CHKTHIS(SfxAllEnumItem, 0);
232 
233 	if ( !pValues || !pValues->Count() )
234 		return nValue;
235 
236 	return SfxEnumItem::GetPosByValue( nValue );
237 }
238 
239 // -----------------------------------------------------------------------
240 
241 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue, const XubString &rValue )
242 {
243 	DBG_CHKTHIS(SfxAllEnumItem, 0);
244 	SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
245 	pVal->nValue = nValue;
246 	pVal->aText = rValue;
247 	const SfxAllEnumValue_Impl *pTemp = pVal;
248 	if ( !pValues )
249 		pValues = new SfxAllEnumValueArr;
250 	else if ( GetPosByValue( nValue ) != USHRT_MAX )
251 		// remove when exists
252 		RemoveValue( nValue );
253 	// then insert
254 	pValues->Insert( pTemp, _GetPosByValue(nValue) ); //! doppelte?!
255 }
256 
257 // -----------------------------------------------------------------------
258 
259 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue )
260 {
261 	DBG_CHKTHIS(SfxAllEnumItem, 0);
262 	SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
263 	pVal->nValue = nValue;
264 	pVal->aText = XubString::CreateFromInt32( nValue );
265 	const SfxAllEnumValue_Impl *pTemp = pVal;
266 	if ( !pValues )
267 		pValues = new SfxAllEnumValueArr;
268 
269 	pValues->Insert( pTemp, _GetPosByValue(nValue) ); //! doppelte?!
270 }
271 
272 void SfxAllEnumItem::DisableValue( sal_uInt16 nValue )
273 {
274 	DBG_CHKTHIS(SfxAllEnumItem, 0);
275 	if ( !pDisabledValues )
276 		pDisabledValues = new SvUShorts;
277 
278 	pDisabledValues->Insert( nValue, pDisabledValues->Count() );
279 }
280 
281 sal_Bool SfxAllEnumItem::IsEnabled( sal_uInt16 nValue ) const
282 {
283 	if ( pDisabledValues )
284 	{
285 		for ( sal_uInt16 i=0; i<pDisabledValues->Count(); i++ )
286 			if ( (*pDisabledValues)[i] == nValue )
287 				return sal_False;
288 	}
289 
290 	return sal_True;
291 }
292 
293 // -----------------------------------------------------------------------
294 
295 void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue )
296 {
297 	DBG_CHKTHIS(SfxAllEnumItem, 0);
298 	sal_uInt16 nPos = GetPosByValue(nValue);
299 	DBG_ASSERT( nPos != USHRT_MAX, "removing value not in enum" );
300 	pValues->Remove( nPos );
301 }
302 
303 // -----------------------------------------------------------------------
304 
305 
306 void SfxAllEnumItem::RemoveAllValues()
307 {
308 	DBG_CHKTHIS(SfxAllEnumItem, 0);
309 	if ( pValues )
310 		pValues->DeleteAndDestroy( 0, pValues->Count() );
311 }
312 
313 
314 
315