ITEM: Refactor ItemType
[LibreOffice.git] / sw / source / core / fields / flddropdown.cxx
blob57106978e10f8e3cd45c57597799b38bb456e164
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 <flddropdown.hxx>
22 #include <algorithm>
24 #include <svl/poolitem.hxx>
25 #include <comphelper/sequence.hxx>
27 #include <unofldmid.h>
29 using namespace com::sun::star;
31 using std::vector;
33 SwDropDownFieldType::SwDropDownFieldType()
34 : SwFieldType(SwFieldIds::Dropdown)
38 SwDropDownFieldType::~SwDropDownFieldType()
42 std::unique_ptr<SwFieldType> SwDropDownFieldType::Copy() const
44 return std::make_unique<SwDropDownFieldType>();
47 SwDropDownField::SwDropDownField(SwFieldType * pTyp)
48 : SwField(pTyp, 0, LANGUAGE_SYSTEM)
52 SwDropDownField::SwDropDownField(const SwDropDownField & rSrc)
53 : SwField(rSrc.GetTyp(), rSrc.GetFormat(), rSrc.GetLanguage()),
54 m_aValues(rSrc.m_aValues), m_aSelectedItem(rSrc.m_aSelectedItem),
55 m_aName(rSrc.m_aName), m_aHelp(rSrc.m_aHelp), m_aToolTip(rSrc.m_aToolTip)
59 SwDropDownField::~SwDropDownField()
63 OUString SwDropDownField::ExpandImpl(SwRootFrame const*const) const
65 OUString sSelect = GetSelectedItem();
66 if (sSelect.isEmpty())
68 vector<OUString>::const_iterator aIt = m_aValues.begin();
69 if ( aIt != m_aValues.end())
70 sSelect = *aIt;
72 // if still no list value is available a default text of 10 spaces is to be set
73 if (sSelect.isEmpty())
74 sSelect = " ";
75 return sSelect;
78 std::unique_ptr<SwField> SwDropDownField::Copy() const
80 return std::make_unique<SwDropDownField>(*this);
83 OUString SwDropDownField::GetPar1() const
85 return GetSelectedItem();
88 OUString SwDropDownField::GetPar2() const
90 return GetName();
93 void SwDropDownField::SetPar1(const OUString & rStr)
95 SetSelectedItem(rStr);
98 void SwDropDownField::SetPar2(const OUString & rName)
100 SetName(rName);
103 void SwDropDownField::SetItems(vector<OUString> && rItems)
105 m_aValues = std::move(rItems);
106 m_aSelectedItem.clear();
109 void SwDropDownField::SetItems(const uno::Sequence<OUString> & rItems)
111 m_aValues.clear();
113 comphelper::sequenceToContainer(m_aValues, rItems);
115 m_aSelectedItem.clear();
118 uno::Sequence<OUString> SwDropDownField::GetItemSequence() const
120 return comphelper::containerToSequence(m_aValues);
124 void SwDropDownField::SetSelectedItem(const OUString & rItem)
126 vector<OUString>::const_iterator aIt =
127 std::find(m_aValues.begin(), m_aValues.end(), rItem);
129 if (aIt != m_aValues.end())
130 m_aSelectedItem = *aIt;
131 else
132 m_aSelectedItem.clear();
135 void SwDropDownField::SetName(const OUString & rName)
137 m_aName = rName;
140 void SwDropDownField::SetHelp(const OUString & rHelp)
142 m_aHelp = rHelp;
145 void SwDropDownField::SetToolTip(const OUString & rToolTip)
147 m_aToolTip = rToolTip;
150 bool SwDropDownField::QueryValue(::uno::Any &rVal, sal_uInt16 nWhich) const
152 nWhich &= ~CONVERT_TWIPS;
153 switch( nWhich )
155 case FIELD_PROP_PAR1:
156 rVal <<= m_aSelectedItem;
157 break;
158 case FIELD_PROP_PAR2:
159 rVal <<= m_aName;
160 break;
161 case FIELD_PROP_PAR3:
162 rVal <<= m_aHelp;
163 break;
164 case FIELD_PROP_PAR4:
165 rVal <<= m_aToolTip;
166 break;
167 case FIELD_PROP_STRINGS:
168 rVal <<= GetItemSequence();
169 break;
171 default:
172 assert(false);
174 return true;
177 bool SwDropDownField::PutValue(const uno::Any &rVal,
178 sal_uInt16 nWhich)
180 switch( nWhich )
182 case FIELD_PROP_PAR1:
184 OUString aTmpStr;
185 rVal >>= aTmpStr;
187 SetSelectedItem(aTmpStr);
189 break;
191 case FIELD_PROP_PAR2:
192 rVal >>= m_aName;
193 break;
195 case FIELD_PROP_PAR3:
196 rVal >>= m_aHelp;
197 break;
199 case FIELD_PROP_PAR4:
200 rVal >>= m_aToolTip;
201 break;
203 case FIELD_PROP_STRINGS:
205 uno::Sequence<OUString> aSeq;
206 rVal >>= aSeq;
207 SetItems(aSeq);
209 break;
211 default:
212 assert(false);
214 return true;
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */