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>
27 struct SfxAllEnumValue_Impl
33 class SfxAllEnumValueArr
: public std::vector
<SfxAllEnumValue_Impl
> {};
36 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which
, sal_uInt16 nVal
):
37 SfxAllEnumItem_Base(which
, nVal
)
42 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which
):
43 SfxAllEnumItem_Base(which
, 0)
47 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem
&rCopy
):
48 SfxAllEnumItem_Base(rCopy
)
51 pValues
.reset( new SfxAllEnumValueArr(*rCopy
.pValues
) );
54 SfxAllEnumItem::~SfxAllEnumItem()
58 sal_uInt16
SfxAllEnumItem::GetValueCount() const
60 return pValues
? pValues
->size() : 0;
63 OUString
const & SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos
) const
65 assert(pValues
&& nPos
< pValues
->size());
66 return (*pValues
)[nPos
].aText
;
69 sal_uInt16
SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos
) const
71 assert(pValues
&& nPos
< pValues
->size());
72 return (*pValues
)[nPos
].nValue
;
75 SfxPoolItem
* SfxAllEnumItem::Clone( SfxItemPool
* ) const
77 return new SfxAllEnumItem(*this);
81 * In contrast to @see GetPosByValue(sal_uInt16) const
82 * this internal method returns the position the value would be for non-present values.
84 std::size_t SfxAllEnumItem::GetPosByValue_( sal_uInt16 nVal
) const
89 //FIXME: Optimisation: use binary search or SortArray
91 for ( nPos
= 0; nPos
< pValues
->size(); ++nPos
)
92 if ( (*pValues
)[nPos
].nValue
>= nVal
)
97 sal_uInt16
SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue
) const
99 if ( !pValues
|| pValues
->empty() )
102 sal_uInt16 nCount
= GetValueCount();
103 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
104 if (GetValueByPos(i
) == nValue
)
109 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue
, const OUString
&rValue
)
111 SfxAllEnumValue_Impl aVal
;
112 aVal
.nValue
= nValue
;
115 pValues
.reset( new SfxAllEnumValueArr
);
116 else if ( GetPosByValue( nValue
) != USHRT_MAX
)
117 // remove when exists
118 RemoveValue( nValue
);
120 pValues
->insert(pValues
->begin() + GetPosByValue_(nValue
), aVal
); // FIXME: Duplicates?
123 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue
)
125 SfxAllEnumValue_Impl aVal
;
126 aVal
.nValue
= nValue
;
127 aVal
.aText
= OUString::number(nValue
);
129 pValues
.reset( new SfxAllEnumValueArr
);
131 pValues
->insert(pValues
->begin() + GetPosByValue_(nValue
), aVal
); // FIXME: Duplicates?
134 void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue
)
136 sal_uInt16 nPos
= GetPosByValue(nValue
);
137 assert(nPos
!= USHRT_MAX
);
138 pValues
->erase( pValues
->begin() + nPos
);
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */