bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / items / aeitem.cxx
blob09356f2041d45755bf4561d07cc9285d0b7cbdfc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/ustring.hxx>
21 #include <svl/aeitem.hxx>
22 #include <boost/noncopyable.hpp>
23 #include <boost/ptr_container/ptr_vector.hpp>
25 TYPEINIT1_AUTOFACTORY(SfxAllEnumItem, SfxEnumItem)
29 struct SfxAllEnumValue_Impl
31 sal_uInt16 nValue;
32 OUString aText;
35 class SfxAllEnumValueArr : boost::noncopyable
37 public:
38 const SfxAllEnumValue_Impl &operator[](size_t i) const {
39 return mValues[i];
42 bool empty() const {
43 return mValues.empty();
46 void Insert(sal_uInt16 n, SfxAllEnumValue_Impl *value) {
47 mValues.insert(mValues.begin() + n, value);
50 void Erase(sal_uInt16 n) {
51 mValues.erase(mValues.begin() + n);
54 size_t size() const {
55 return mValues.size();
58 private:
59 boost::ptr_vector<SfxAllEnumValue_Impl> mValues;
64 SfxAllEnumItem::SfxAllEnumItem() :
65 SfxEnumItem(),
66 pValues( 0 ),
67 pDisabledValues( 0 )
73 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which, sal_uInt16 nVal):
74 SfxEnumItem(which, nVal),
75 pValues( 0 ),
76 pDisabledValues( 0 )
78 InsertValue( nVal );
83 SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, SvStream &rStream ):
84 SfxEnumItem(which, rStream),
85 pValues( 0 ),
86 pDisabledValues( 0 )
88 InsertValue( GetValue() );
94 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which):
95 SfxEnumItem(which, 0),
96 pValues( 0 ),
97 pDisabledValues( 0 )
104 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
105 SfxEnumItem(rCopy),
106 pValues(0),
107 pDisabledValues( 0 )
109 if ( !rCopy.pValues )
110 return;
112 pValues = new SfxAllEnumValueArr;
114 for ( sal_uInt16 nPos = 0; nPos < rCopy.pValues->size(); ++nPos )
116 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
117 pVal->nValue = (*rCopy.pValues)[nPos].nValue;
118 pVal->aText = (*rCopy.pValues)[nPos].aText;
119 pValues->Insert( nPos, pVal );
122 if( rCopy.pDisabledValues )
124 pDisabledValues = new std::vector<sal_uInt16>( *(rCopy.pDisabledValues) );
130 SfxAllEnumItem::~SfxAllEnumItem()
132 delete pValues;
133 delete pDisabledValues;
138 sal_uInt16 SfxAllEnumItem::GetValueCount() const
140 return pValues ? pValues->size() : 0;
145 OUString SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos ) const
147 DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
148 return (*pValues)[nPos].aText;
153 sal_uInt16 SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos ) const
155 DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
156 return (*pValues)[nPos].nValue;
161 SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
163 return new SfxAllEnumItem(*this);
168 SfxPoolItem* SfxAllEnumItem::Create( SvStream & rStream, sal_uInt16 ) const
170 return new SfxAllEnumItem( Which(), rStream );
176 * In contrast to @see SfxEnumItemInterface::GetPosByValue(sal_uInt16) const
177 * this internal method returns the position the value would be for non-present values.
179 sal_uInt16 SfxAllEnumItem::_GetPosByValue( sal_uInt16 nVal ) const
181 if ( !pValues )
182 return 0;
184 //FIXME: Optimisation: use binary search or SortArray
185 sal_uInt16 nPos;
186 for ( nPos = 0; nPos < pValues->size(); ++nPos )
187 if ( (*pValues)[nPos].nValue >= nVal )
188 return nPos;
189 return nPos;
194 * In contrast to @see SfxEnumItemInterface::GetPosByValue(sal_uInt16) const
195 * this method always returns nValue, as long as not at least one value has
196 * been inserted using the SfxAllEnumItem::InsertValue() methods
199 sal_uInt16 SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue ) const
201 if ( !pValues || pValues->empty() )
202 return nValue;
204 return SfxEnumItem::GetPosByValue( nValue );
209 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue, const OUString &rValue )
211 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
212 pVal->nValue = nValue;
213 pVal->aText = rValue;
214 if ( !pValues )
215 pValues = new SfxAllEnumValueArr;
216 else if ( GetPosByValue( nValue ) != USHRT_MAX )
217 // remove when exists
218 RemoveValue( nValue );
219 // then insert
220 pValues->Insert( _GetPosByValue(nValue), pVal ); // FIXME: Duplicates?
225 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue )
227 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
228 pVal->nValue = nValue;
229 pVal->aText = OUString::number(nValue);
230 if ( !pValues )
231 pValues = new SfxAllEnumValueArr;
233 pValues->Insert( _GetPosByValue(nValue), pVal ); // FIXME: Duplicates?
236 void SfxAllEnumItem::DisableValue( sal_uInt16 nValue )
238 if ( !pDisabledValues )
239 pDisabledValues = new std::vector<sal_uInt16>;
241 pDisabledValues->push_back( nValue );
244 bool SfxAllEnumItem::IsEnabled( sal_uInt16 nValue ) const
246 if ( pDisabledValues )
248 for ( size_t i=0; i<pDisabledValues->size(); i++ )
249 if ( (*pDisabledValues)[i] == nValue )
250 return false;
253 return true;
258 void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue )
260 sal_uInt16 nPos = GetPosByValue(nValue);
261 DBG_ASSERT( nPos != USHRT_MAX, "removing value not in enum" );
262 pValues->Erase( nPos );
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */