Update ooo320-m1
[ooovba.git] / sw / source / core / fields / flddropdown.cxx
blob357e1c842efe50148702dfa29e1b390e3af26676
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: flddropdown.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 #include <flddropdown.hxx>
36 #ifndef INCLUDED_ALGORITHM
37 #include <algorithm>
38 #define INCLUDED_ALGORITHM
39 #endif
40 #include <svtools/poolitem.hxx>
42 #ifndef _UNOFLDMID_H
43 #include <unofldmid.h>
44 #endif
45 #include <unoprnms.hxx>
47 using namespace com::sun::star;
49 using rtl::OUString;
50 using std::vector;
52 static String aEmptyString;
54 SwDropDownFieldType::SwDropDownFieldType()
55 : SwFieldType(RES_DROPDOWN)
59 SwDropDownFieldType::~SwDropDownFieldType()
63 SwFieldType * SwDropDownFieldType::Copy() const
65 return new SwDropDownFieldType;
68 SwDropDownField::SwDropDownField(SwFieldType * pTyp)
69 : SwField(pTyp, 0, LANGUAGE_SYSTEM)
73 SwDropDownField::SwDropDownField(const SwDropDownField & rSrc)
74 : SwField(rSrc.GetTyp(), rSrc.GetFormat(), rSrc.GetLanguage()),
75 aValues(rSrc.aValues), aSelectedItem(rSrc.aSelectedItem),
76 aName(rSrc.aName), aHelp(rSrc.aHelp), aToolTip(rSrc.aToolTip)
80 SwDropDownField::~SwDropDownField()
84 String SwDropDownField::Expand() const
86 String sSelect = GetSelectedItem();
87 if(!sSelect.Len())
89 vector<String>::const_iterator aIt = aValues.begin();
90 if ( aIt != aValues.end())
91 sSelect = *aIt;
93 //if still no list value is available a default text of 10 spaces is to be set
94 if(!sSelect.Len())
95 sSelect.AppendAscii ( RTL_CONSTASCII_STRINGPARAM (" "));
96 return sSelect;
99 SwField * SwDropDownField::Copy() const
101 return new SwDropDownField(*this);
104 const String & SwDropDownField::GetPar1() const
106 return GetSelectedItem();
109 String SwDropDownField::GetPar2() const
111 return GetName();
114 void SwDropDownField::SetPar1(const String & rStr)
116 SetSelectedItem(rStr);
119 void SwDropDownField::SetPar2(const String & rName)
121 SetName(rName);
124 void SwDropDownField::SetItems(const vector<String> & rItems)
126 aValues = rItems;
127 aSelectedItem = aEmptyString;
130 void SwDropDownField::SetItems(const uno::Sequence<OUString> & rItems)
132 aValues.clear();
134 sal_Int32 aCount = rItems.getLength();
135 for (int i = 0; i < aCount; i++)
136 aValues.push_back(rItems[i]);
138 aSelectedItem = aEmptyString;
141 uno::Sequence<OUString> SwDropDownField::GetItemSequence() const
143 uno::Sequence<OUString> aSeq( aValues.size() );
144 OUString* pSeq = aSeq.getArray();
145 int i = 0;
146 vector<String>::const_iterator aIt;
148 for (aIt = aValues.begin(); aIt != aValues.end(); aIt++)
150 pSeq[i] = rtl::OUString(*aIt);
152 i++;
155 return aSeq;
158 const String & SwDropDownField::GetSelectedItem() const
160 return aSelectedItem;
163 const String & SwDropDownField::GetName() const
165 return aName;
168 const String & SwDropDownField::GetHelp() const
170 return aHelp;
173 const String & SwDropDownField::GetToolTip() const
175 return aToolTip;
178 BOOL SwDropDownField::SetSelectedItem(const String & rItem)
180 vector<String>::const_iterator aIt =
181 std::find(aValues.begin(), aValues.end(), rItem);
183 if (aIt != aValues.end())
184 aSelectedItem = *aIt;
185 else
186 aSelectedItem = String();
188 return (aIt != aValues.end());
191 void SwDropDownField::SetName(const String & rName)
193 aName = rName;
196 void SwDropDownField::SetHelp(const String & rHelp)
198 aHelp = rHelp;
201 void SwDropDownField::SetToolTip(const String & rToolTip)
203 aToolTip = rToolTip;
206 BOOL SwDropDownField::QueryValue(::uno::Any &rVal, USHORT nWhich) const
208 nWhich &= ~CONVERT_TWIPS;
209 switch( nWhich )
211 case FIELD_PROP_PAR1:
212 rVal <<= rtl::OUString(GetSelectedItem());
213 break;
214 case FIELD_PROP_PAR2:
215 rVal <<= rtl::OUString(GetName());
216 break;
217 case FIELD_PROP_PAR3:
218 rVal <<= rtl::OUString(GetHelp());
219 break;
220 case FIELD_PROP_PAR4:
221 rVal <<= rtl::OUString(GetToolTip());
222 break;
223 case FIELD_PROP_STRINGS:
224 rVal <<= GetItemSequence();
226 break;
228 default:
229 DBG_ERROR("illegal property");
231 return sal_True;
234 BOOL SwDropDownField::PutValue(const uno::Any &rVal,
235 USHORT nWhich)
237 switch( nWhich )
239 case FIELD_PROP_PAR1:
241 String aTmpStr;
242 ::GetString( rVal, aTmpStr );
244 SetSelectedItem(aTmpStr);
246 break;
248 case FIELD_PROP_PAR2:
250 String aTmpStr;
251 ::GetString( rVal, aTmpStr );
253 SetName(aTmpStr);
255 break;
257 case FIELD_PROP_PAR3:
259 String aTmpStr;
260 ::GetString( rVal, aTmpStr );
262 SetHelp(aTmpStr);
264 break;
266 case FIELD_PROP_PAR4:
268 String aTmpStr;
269 ::GetString( rVal, aTmpStr );
271 SetToolTip(aTmpStr);
273 break;
275 case FIELD_PROP_STRINGS:
277 uno::Sequence<OUString> aSeq;
278 rVal >>= aSeq;
279 SetItems(aSeq);
281 break;
283 default:
284 DBG_ERROR("illegal property");
286 return sal_True;