Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <tools/string.hxx>
22 :
23 : #include <svl/aeitem.hxx>
24 : #include <vector>
25 :
26 : // STATIC DATA -----------------------------------------------------------
27 :
28 : DBG_NAME(SfxAllEnumItem)
29 :
30 387 : TYPEINIT1_AUTOFACTORY(SfxAllEnumItem, SfxEnumItem)
31 :
32 : // -----------------------------------------------------------------------
33 :
34 0 : struct SfxAllEnumValue_Impl
35 : {
36 : sal_uInt16 nValue;
37 : rtl::OUString aText;
38 : };
39 :
40 0 : class SfxAllEnumValueArr : public std::vector<SfxAllEnumValue_Impl*>
41 : {
42 : public:
43 0 : ~SfxAllEnumValueArr()
44 0 : {
45 0 : for( const_iterator it = begin(); it != end(); ++it )
46 0 : delete *it;
47 0 : }
48 : };
49 :
50 : // -----------------------------------------------------------------------
51 :
52 0 : SfxAllEnumItem::SfxAllEnumItem() :
53 : SfxEnumItem(),
54 : pValues( 0 ),
55 0 : pDisabledValues( 0 )
56 : {
57 0 : }
58 :
59 : // -----------------------------------------------------------------------
60 :
61 0 : SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which, sal_uInt16 nVal):
62 : SfxEnumItem(which, nVal),
63 : pValues( 0 ),
64 0 : pDisabledValues( 0 )
65 : {
66 : DBG_CTOR(SfxAllEnumItem, 0);
67 0 : InsertValue( nVal );
68 0 : }
69 :
70 : // -----------------------------------------------------------------------
71 :
72 0 : SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, SvStream &rStream ):
73 : SfxEnumItem(which, rStream),
74 : pValues( 0 ),
75 0 : pDisabledValues( 0 )
76 : {
77 : DBG_CTOR(SfxAllEnumItem, 0);
78 0 : InsertValue( GetValue() );
79 0 : }
80 :
81 : // -----------------------------------------------------------------------
82 :
83 :
84 195 : SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which):
85 : SfxEnumItem(which, 0),
86 : pValues( 0 ),
87 195 : pDisabledValues( 0 )
88 : {
89 : DBG_CTOR(SfxAllEnumItem, 0);
90 195 : }
91 :
92 :
93 : // -----------------------------------------------------------------------
94 :
95 0 : SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
96 : SfxEnumItem(rCopy),
97 : pValues(0),
98 0 : pDisabledValues( 0 )
99 : {
100 : DBG_CTOR(SfxAllEnumItem, 0);
101 0 : if ( !rCopy.pValues )
102 0 : return;
103 :
104 0 : pValues = new SfxAllEnumValueArr;
105 :
106 0 : for ( sal_uInt16 nPos = 0; nPos < rCopy.pValues->size(); ++nPos )
107 : {
108 0 : SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
109 0 : pVal->nValue = (*rCopy.pValues)[nPos]->nValue;
110 0 : pVal->aText = (*rCopy.pValues)[nPos]->aText;
111 0 : pValues->insert( pValues->begin() + nPos, pVal );
112 : }
113 :
114 0 : if( rCopy.pDisabledValues )
115 : {
116 0 : pDisabledValues = new std::vector<sal_uInt16>( *(rCopy.pDisabledValues) );
117 : }
118 : }
119 :
120 : // -----------------------------------------------------------------------
121 :
122 327 : SfxAllEnumItem::~SfxAllEnumItem()
123 : {
124 : DBG_DTOR(SfxAllEnumItem, 0);
125 109 : delete pValues;
126 109 : delete pDisabledValues;
127 218 : }
128 :
129 : // -----------------------------------------------------------------------
130 :
131 0 : sal_uInt16 SfxAllEnumItem::GetValueCount() const
132 : {
133 : DBG_CHKTHIS(SfxAllEnumItem, 0);
134 0 : return pValues ? pValues->size() : 0;
135 : }
136 :
137 : // -----------------------------------------------------------------------
138 :
139 0 : rtl::OUString SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos ) const
140 : {
141 : DBG_CHKTHIS(SfxAllEnumItem, 0);
142 : DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
143 0 : return (*pValues)[nPos]->aText;
144 : }
145 :
146 : // -----------------------------------------------------------------------
147 :
148 0 : sal_uInt16 SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos ) const
149 : {
150 : DBG_CHKTHIS(SfxAllEnumItem, 0);
151 : DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
152 0 : return (*pValues)[nPos]->nValue;
153 : }
154 :
155 : // -----------------------------------------------------------------------
156 :
157 0 : SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
158 : {
159 : DBG_CHKTHIS(SfxAllEnumItem, 0);
160 0 : return new SfxAllEnumItem(*this);
161 : }
162 :
163 : // -----------------------------------------------------------------------
164 :
165 0 : SfxPoolItem* SfxAllEnumItem::Create( SvStream & rStream, sal_uInt16 ) const
166 : {
167 : DBG_CHKTHIS(SfxAllEnumItem, 0);
168 0 : return new SfxAllEnumItem( Which(), rStream );
169 : }
170 :
171 :
172 : // -----------------------------------------------------------------------
173 :
174 0 : sal_uInt16 SfxAllEnumItem::_GetPosByValue( sal_uInt16 nVal ) const
175 :
176 : /* [Beschreibung]
177 :
178 : Im Ggs. zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const> liefert
179 : diese interne Methode bei nicht vorhandenen Values die Position,
180 : an der der Wert liegen w"urde.
181 : */
182 :
183 : {
184 : DBG_CHKTHIS(SfxAllEnumItem, 0);
185 :
186 0 : if ( !pValues )
187 0 : return 0;
188 :
189 : //!O: binaere Suche oder SortArray verwenden
190 : sal_uInt16 nPos;
191 0 : for ( nPos = 0; nPos < pValues->size(); ++nPos )
192 0 : if ( (*pValues)[nPos]->nValue >= nVal )
193 0 : return nPos;
194 0 : return nPos;
195 : }
196 :
197 : // -----------------------------------------------------------------------
198 :
199 0 : sal_uInt16 SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue ) const
200 :
201 : /* [Beschreibung]
202 :
203 : Liefert im Gegensatz zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const>
204 : immer nValue zur"uck, solange nicht mindestens ein Wert mit einer der
205 : Methoden <SfxAllEnumItem::InsertValue()> eingef"ugt wurde.
206 : */
207 :
208 : {
209 : DBG_CHKTHIS(SfxAllEnumItem, 0);
210 :
211 0 : if ( !pValues || pValues->empty() )
212 0 : return nValue;
213 :
214 0 : return SfxEnumItem::GetPosByValue( nValue );
215 : }
216 :
217 : // -----------------------------------------------------------------------
218 :
219 0 : void SfxAllEnumItem::InsertValue( sal_uInt16 nValue, const rtl::OUString &rValue )
220 : {
221 : DBG_CHKTHIS(SfxAllEnumItem, 0);
222 0 : SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
223 0 : pVal->nValue = nValue;
224 0 : pVal->aText = rValue;
225 0 : if ( !pValues )
226 0 : pValues = new SfxAllEnumValueArr;
227 0 : else if ( GetPosByValue( nValue ) != USHRT_MAX )
228 : // remove when exists
229 0 : RemoveValue( nValue );
230 : // then insert
231 0 : pValues->insert( pValues->begin() + _GetPosByValue(nValue), pVal ); //! doppelte?!
232 0 : }
233 :
234 : // -----------------------------------------------------------------------
235 :
236 0 : void SfxAllEnumItem::InsertValue( sal_uInt16 nValue )
237 : {
238 : DBG_CHKTHIS(SfxAllEnumItem, 0);
239 0 : SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
240 0 : pVal->nValue = nValue;
241 0 : pVal->aText = rtl::OUString::valueOf(static_cast<sal_Int32>(nValue));
242 0 : if ( !pValues )
243 0 : pValues = new SfxAllEnumValueArr;
244 :
245 0 : pValues->insert( pValues->begin() + _GetPosByValue(nValue), pVal ); //! doppelte?!
246 0 : }
247 :
248 0 : void SfxAllEnumItem::DisableValue( sal_uInt16 nValue )
249 : {
250 : DBG_CHKTHIS(SfxAllEnumItem, 0);
251 0 : if ( !pDisabledValues )
252 0 : pDisabledValues = new std::vector<sal_uInt16>;
253 :
254 0 : pDisabledValues->push_back( nValue );
255 0 : }
256 :
257 0 : sal_Bool SfxAllEnumItem::IsEnabled( sal_uInt16 nValue ) const
258 : {
259 0 : if ( pDisabledValues )
260 : {
261 0 : for ( size_t i=0; i<pDisabledValues->size(); i++ )
262 0 : if ( (*pDisabledValues)[i] == nValue )
263 0 : return sal_False;
264 : }
265 :
266 0 : return sal_True;
267 : }
268 :
269 : // -----------------------------------------------------------------------
270 :
271 0 : void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue )
272 : {
273 : DBG_CHKTHIS(SfxAllEnumItem, 0);
274 0 : sal_uInt16 nPos = GetPosByValue(nValue);
275 : DBG_ASSERT( nPos != USHRT_MAX, "removing value not in enum" );
276 0 : pValues->erase( pValues->begin() + nPos );
277 0 : }
278 :
279 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|