1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 #ifndef GCC
27 #endif
28
29 #include <tools/string.hxx>
30
31 #define _SVSTDARR_USHORTS
32 #include <svl/svstdarr.hxx>
33 #include <svl/svarray.hxx>
34 #include <svl/aeitem.hxx>
35
36 // STATIC DATA -----------------------------------------------------------
37
38 DBG_NAME(SfxAllEnumItem)
39
40 TYPEINIT1_AUTOFACTORY(SfxAllEnumItem, SfxEnumItem)
41
42 // -----------------------------------------------------------------------
43
44 struct SfxAllEnumValue_Impl
45 {
46 sal_uInt16 nValue;
47 XubString aText;
48 };
49
50 SV_DECL_PTRARR_DEL(SfxAllEnumValueArr, SfxAllEnumValue_Impl*, 0, 8)
SV_IMPL_PTRARR(SfxAllEnumValueArr,SfxAllEnumValue_Impl *)51 SV_IMPL_PTRARR(SfxAllEnumValueArr, SfxAllEnumValue_Impl*)
52
53 // -----------------------------------------------------------------------
54
55 SfxAllEnumItem::SfxAllEnumItem() :
56 SfxEnumItem(),
57 pValues( 0 ),
58 pDisabledValues( 0 )
59 {
60 }
61
SfxAllEnumItem(sal_uInt16 which,sal_uInt16 nVal,const XubString & rText)62 SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, sal_uInt16 nVal, const XubString &rText ):
63 SfxEnumItem(which, nVal),
64 pValues( 0 ),
65 pDisabledValues( 0 )
66 {
67 DBG_CTOR(SfxAllEnumItem, 0);
68 InsertValue( nVal, rText );
69 }
70
71 // -----------------------------------------------------------------------
72
SfxAllEnumItem(sal_uInt16 which,sal_uInt16 nVal)73 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which, sal_uInt16 nVal):
74 SfxEnumItem(which, nVal),
75 pValues( 0 ),
76 pDisabledValues( 0 )
77 {
78 DBG_CTOR(SfxAllEnumItem, 0);
79 InsertValue( nVal );
80 }
81
82 // -----------------------------------------------------------------------
83
SfxAllEnumItem(sal_uInt16 which,SvStream & rStream)84 SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, SvStream &rStream ):
85 SfxEnumItem(which, rStream),
86 pValues( 0 ),
87 pDisabledValues( 0 )
88 {
89 DBG_CTOR(SfxAllEnumItem, 0);
90 InsertValue( GetValue() );
91 }
92
93 // -----------------------------------------------------------------------
94
95
SfxAllEnumItem(sal_uInt16 which)96 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which):
97 SfxEnumItem(which, 0),
98 pValues( 0 ),
99 pDisabledValues( 0 )
100 {
101 DBG_CTOR(SfxAllEnumItem, 0);
102 }
103
104
105 // -----------------------------------------------------------------------
106
SfxAllEnumItem(const SfxAllEnumItem & rCopy)107 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
108 SfxEnumItem(rCopy),
109 pValues(0),
110 pDisabledValues( 0 )
111 {
112 DBG_CTOR(SfxAllEnumItem, 0);
113 if ( !rCopy.pValues )
114 return;
115
116 pValues = new SfxAllEnumValueArr;
117
118 for ( sal_uInt16 nPos = 0; nPos < rCopy.pValues->Count(); ++nPos )
119 {
120 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
121 pVal->nValue = rCopy.pValues->GetObject(nPos)->nValue;
122 pVal->aText = rCopy.pValues->GetObject(nPos)->aText;
123 const SfxAllEnumValue_Impl *pTemp = pVal;
124 pValues->Insert( pTemp, nPos );
125 }
126
127 if( rCopy.pDisabledValues )
128 {
129 pDisabledValues = new SvUShorts;
130 for ( sal_uInt16 nPos = 0; nPos < rCopy.pDisabledValues->Count(); ++nPos )
131 {
132 pDisabledValues->Insert( rCopy.pDisabledValues->GetObject(nPos),
133 nPos );
134 }
135 }
136 }
137
138 // -----------------------------------------------------------------------
139
~SfxAllEnumItem()140 SfxAllEnumItem::~SfxAllEnumItem()
141 {
142 DBG_DTOR(SfxAllEnumItem, 0);
143 delete pValues;
144 delete pDisabledValues;
145 }
146
147 // -----------------------------------------------------------------------
148
GetValueCount() const149 sal_uInt16 SfxAllEnumItem::GetValueCount() const
150 {
151 DBG_CHKTHIS(SfxAllEnumItem, 0);
152 return pValues ? pValues->Count() : 0;
153 }
154
155 // -----------------------------------------------------------------------
156
GetValueTextByPos(sal_uInt16 nPos) const157 XubString SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos ) const
158 {
159 DBG_CHKTHIS(SfxAllEnumItem, 0);
160 DBG_ASSERT( pValues && nPos < pValues->Count(), "enum overflow" );
161 return pValues->GetObject(nPos)->aText;
162 }
163
164 // -----------------------------------------------------------------------
165
GetValueByPos(sal_uInt16 nPos) const166 sal_uInt16 SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos ) const
167 {
168 DBG_CHKTHIS(SfxAllEnumItem, 0);
169 DBG_ASSERT( pValues && nPos < pValues->Count(), "enum overflow" );
170 return pValues->GetObject(nPos)->nValue;
171 }
172
173 // -----------------------------------------------------------------------
174
Clone(SfxItemPool *) const175 SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
176 {
177 DBG_CHKTHIS(SfxAllEnumItem, 0);
178 return new SfxAllEnumItem(*this);
179 }
180
181 // -----------------------------------------------------------------------
182
Create(SvStream & rStream,sal_uInt16) const183 SfxPoolItem* SfxAllEnumItem::Create( SvStream & rStream, sal_uInt16 ) const
184 {
185 DBG_CHKTHIS(SfxAllEnumItem, 0);
186 return new SfxAllEnumItem( Which(), rStream );
187 }
188
189
190 // -----------------------------------------------------------------------
191
_GetPosByValue(sal_uInt16 nVal) const192 sal_uInt16 SfxAllEnumItem::_GetPosByValue( sal_uInt16 nVal ) const
193
194 /* [Beschreibung]
195
196 Im Ggs. zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const> liefert
197 diese interne Methode bei nicht vorhandenen Values die Position,
198 an der der Wert liegen w"urde.
199 */
200
201 {
202 DBG_CHKTHIS(SfxAllEnumItem, 0);
203
204 if ( !pValues )
205 return 0;
206
207 //!O: binaere Suche oder SortArray verwenden
208 sal_uInt16 nPos;
209 for ( nPos = 0; nPos < pValues->Count(); ++nPos )
210 if ( pValues->GetObject(nPos)->nValue >= nVal )
211 return nPos;
212 return nPos;
213 }
214
215 // -----------------------------------------------------------------------
216
GetPosByValue(sal_uInt16 nValue) const217 sal_uInt16 SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue ) const
218
219 /* [Beschreibung]
220
221 Liefert im Gegensatz zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const>
222 immer nValue zur"uck, solange nicht mindestens ein Wert mit einer der
223 Methoden <SfxAllEnumItem::InsertValue()> eingef"ugt wurde.
224 */
225
226 {
227 DBG_CHKTHIS(SfxAllEnumItem, 0);
228
229 if ( !pValues || !pValues->Count() )
230 return nValue;
231
232 return SfxEnumItem::GetPosByValue( nValue );
233 }
234
235 // -----------------------------------------------------------------------
236
InsertValue(sal_uInt16 nValue,const XubString & rValue)237 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue, const XubString &rValue )
238 {
239 DBG_CHKTHIS(SfxAllEnumItem, 0);
240 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
241 pVal->nValue = nValue;
242 pVal->aText = rValue;
243 const SfxAllEnumValue_Impl *pTemp = pVal;
244 if ( !pValues )
245 pValues = new SfxAllEnumValueArr;
246 else if ( GetPosByValue( nValue ) != USHRT_MAX )
247 // remove when exists
248 RemoveValue( nValue );
249 // then insert
250 pValues->Insert( pTemp, _GetPosByValue(nValue) ); //! doppelte?!
251 }
252
253 // -----------------------------------------------------------------------
254
InsertValue(sal_uInt16 nValue)255 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue )
256 {
257 DBG_CHKTHIS(SfxAllEnumItem, 0);
258 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
259 pVal->nValue = nValue;
260 pVal->aText = XubString::CreateFromInt32( nValue );
261 const SfxAllEnumValue_Impl *pTemp = pVal;
262 if ( !pValues )
263 pValues = new SfxAllEnumValueArr;
264
265 pValues->Insert( pTemp, _GetPosByValue(nValue) ); //! doppelte?!
266 }
267
DisableValue(sal_uInt16 nValue)268 void SfxAllEnumItem::DisableValue( sal_uInt16 nValue )
269 {
270 DBG_CHKTHIS(SfxAllEnumItem, 0);
271 if ( !pDisabledValues )
272 pDisabledValues = new SvUShorts;
273
274 pDisabledValues->Insert( nValue, pDisabledValues->Count() );
275 }
276
IsEnabled(sal_uInt16 nValue) const277 sal_Bool SfxAllEnumItem::IsEnabled( sal_uInt16 nValue ) const
278 {
279 if ( pDisabledValues )
280 {
281 for ( sal_uInt16 i=0; i<pDisabledValues->Count(); i++ )
282 if ( (*pDisabledValues)[i] == nValue )
283 return sal_False;
284 }
285
286 return sal_True;
287 }
288
289 // -----------------------------------------------------------------------
290
RemoveValue(sal_uInt16 nValue)291 void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue )
292 {
293 DBG_CHKTHIS(SfxAllEnumItem, 0);
294 sal_uInt16 nPos = GetPosByValue(nValue);
295 DBG_ASSERT( nPos != USHRT_MAX, "removing value not in enum" );
296 pValues->Remove( nPos );
297 }
298
299 // -----------------------------------------------------------------------
300
301
RemoveAllValues()302 void SfxAllEnumItem::RemoveAllValues()
303 {
304 DBG_CHKTHIS(SfxAllEnumItem, 0);
305 if ( pValues )
306 pValues->DeleteAndDestroy( 0, pValues->Count() );
307 }
308
309
310
311