1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
35 class SfxAllEnumValueArr
: boost::noncopyable
38 const SfxAllEnumValue_Impl
&operator[](size_t i
) 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
);
55 return mValues
.size();
59 boost::ptr_vector
<SfxAllEnumValue_Impl
> mValues
;
64 SfxAllEnumItem::SfxAllEnumItem() :
73 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which
, sal_uInt16 nVal
):
74 SfxEnumItem(which
, nVal
),
83 SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which
, SvStream
&rStream
):
84 SfxEnumItem(which
, rStream
),
88 InsertValue( GetValue() );
94 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which
):
95 SfxEnumItem(which
, 0),
104 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem
&rCopy
):
109 if ( !rCopy
.pValues
)
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()
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
184 //FIXME: Optimisation: use binary search or SortArray
186 for ( nPos
= 0; nPos
< pValues
->size(); ++nPos
)
187 if ( (*pValues
)[nPos
].nValue
>= nVal
)
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() )
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
;
215 pValues
= new SfxAllEnumValueArr
;
216 else if ( GetPosByValue( nValue
) != USHRT_MAX
)
217 // remove when exists
218 RemoveValue( nValue
);
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
);
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
)
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: */