1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 #include <flddropdown.hxx>
32 
33 #ifndef INCLUDED_ALGORITHM
34 #include <algorithm>
35 #define INCLUDED_ALGORITHM
36 #endif
37 #include <svl/poolitem.hxx>
38 
39 #ifndef _UNOFLDMID_H
40 #include <unofldmid.h>
41 #endif
42 #include <unoprnms.hxx>
43 
44 using namespace com::sun::star;
45 
46 using rtl::OUString;
47 using std::vector;
48 
49 static String aEmptyString;
50 
51 SwDropDownFieldType::SwDropDownFieldType()
52     : SwFieldType(RES_DROPDOWN)
53 {
54 }
55 
56 SwDropDownFieldType::~SwDropDownFieldType()
57 {
58 }
59 
60 SwFieldType * SwDropDownFieldType::Copy() const
61 {
62     return new SwDropDownFieldType;
63 }
64 
65 SwDropDownField::SwDropDownField(SwFieldType * pTyp)
66     : SwField(pTyp, 0, LANGUAGE_SYSTEM)
67 {
68 }
69 
70 SwDropDownField::SwDropDownField(const SwDropDownField & rSrc)
71     : SwField(rSrc.GetTyp(), rSrc.GetFormat(), rSrc.GetLanguage()),
72       aValues(rSrc.aValues), aSelectedItem(rSrc.aSelectedItem),
73       aName(rSrc.aName), aHelp(rSrc.aHelp), aToolTip(rSrc.aToolTip)
74 {
75 }
76 
77 SwDropDownField::~SwDropDownField()
78 {
79 }
80 
81 String SwDropDownField::Expand() const
82 {
83     String sSelect = GetSelectedItem();
84     if(!sSelect.Len())
85     {
86         vector<String>::const_iterator aIt = aValues.begin();
87         if ( aIt != aValues.end())
88             sSelect = *aIt;
89     }
90     //if still no list value is available a default text of 10 spaces is to be set
91     if(!sSelect.Len())
92         sSelect.AppendAscii ( RTL_CONSTASCII_STRINGPARAM ("          "));
93     return sSelect;
94 }
95 
96 SwField * SwDropDownField::Copy() const
97 {
98     return new SwDropDownField(*this);
99 }
100 
101 const String & SwDropDownField::GetPar1() const
102 {
103     return GetSelectedItem();
104 }
105 
106 String SwDropDownField::GetPar2() const
107 {
108     return GetName();
109 }
110 
111 void SwDropDownField::SetPar1(const String & rStr)
112 {
113     SetSelectedItem(rStr);
114 }
115 
116 void SwDropDownField::SetPar2(const String & rName)
117 {
118     SetName(rName);
119 }
120 
121 void SwDropDownField::SetItems(const vector<String> & rItems)
122 {
123     aValues = rItems;
124     aSelectedItem = aEmptyString;
125 }
126 
127 void SwDropDownField::SetItems(const uno::Sequence<OUString> & rItems)
128 {
129     aValues.clear();
130 
131     sal_Int32 aCount = rItems.getLength();
132     for (int i = 0; i < aCount; i++)
133         aValues.push_back(rItems[i]);
134 
135     aSelectedItem = aEmptyString;
136 }
137 
138 uno::Sequence<OUString> SwDropDownField::GetItemSequence() const
139 {
140     uno::Sequence<OUString> aSeq( aValues.size() );
141     OUString* pSeq = aSeq.getArray();
142     int i = 0;
143     vector<String>::const_iterator aIt;
144 
145     for (aIt = aValues.begin(); aIt != aValues.end(); aIt++)
146     {
147         pSeq[i] = rtl::OUString(*aIt);
148 
149         i++;
150     }
151 
152     return aSeq;
153 }
154 
155 const String & SwDropDownField::GetSelectedItem() const
156 {
157     return aSelectedItem;
158 }
159 
160 const String & SwDropDownField::GetName() const
161 {
162     return aName;
163 }
164 
165 const String & SwDropDownField::GetHelp() const
166 {
167     return aHelp;
168 }
169 
170 const String & SwDropDownField::GetToolTip() const
171 {
172     return aToolTip;
173 }
174 
175 sal_Bool SwDropDownField::SetSelectedItem(const String & rItem)
176 {
177     vector<String>::const_iterator aIt =
178         std::find(aValues.begin(), aValues.end(), rItem);
179 
180     if (aIt != aValues.end())
181         aSelectedItem = *aIt;
182     else
183         aSelectedItem = String();
184 
185     return (aIt != aValues.end());
186 }
187 
188 void SwDropDownField::SetName(const String & rName)
189 {
190     aName = rName;
191 }
192 
193 void SwDropDownField::SetHelp(const String & rHelp)
194 {
195     aHelp = rHelp;
196 }
197 
198 void SwDropDownField::SetToolTip(const String & rToolTip)
199 {
200     aToolTip = rToolTip;
201 }
202 
203 sal_Bool SwDropDownField::QueryValue(::uno::Any &rVal, sal_uInt16 nWhich) const
204 {
205     nWhich &= ~CONVERT_TWIPS;
206     switch( nWhich )
207     {
208     case FIELD_PROP_PAR1:
209         rVal <<= rtl::OUString(GetSelectedItem());
210         break;
211     case FIELD_PROP_PAR2:
212         rVal <<= rtl::OUString(GetName());
213         break;
214     case FIELD_PROP_PAR3:
215         rVal <<= rtl::OUString(GetHelp());
216         break;
217     case FIELD_PROP_PAR4:
218         rVal <<= rtl::OUString(GetToolTip());
219         break;
220     case FIELD_PROP_STRINGS:
221         rVal <<= GetItemSequence();
222 
223         break;
224 
225     default:
226         DBG_ERROR("illegal property");
227     }
228     return sal_True;
229 }
230 
231 sal_Bool SwDropDownField::PutValue(const uno::Any &rVal,
232                                sal_uInt16 nWhich)
233 {
234     switch( nWhich )
235     {
236     case FIELD_PROP_PAR1:
237         {
238             String aTmpStr;
239             ::GetString( rVal, aTmpStr );
240 
241             SetSelectedItem(aTmpStr);
242         }
243         break;
244 
245     case FIELD_PROP_PAR2:
246         {
247             String aTmpStr;
248             ::GetString( rVal, aTmpStr );
249 
250             SetName(aTmpStr);
251         }
252         break;
253 
254     case FIELD_PROP_PAR3:
255         {
256             String aTmpStr;
257             ::GetString( rVal, aTmpStr );
258 
259             SetHelp(aTmpStr);
260         }
261         break;
262 
263     case FIELD_PROP_PAR4:
264         {
265             String aTmpStr;
266             ::GetString( rVal, aTmpStr );
267 
268             SetToolTip(aTmpStr);
269         }
270         break;
271 
272     case FIELD_PROP_STRINGS:
273         {
274             uno::Sequence<OUString> aSeq;
275             rVal >>= aSeq;
276             SetItems(aSeq);
277         }
278         break;
279 
280     default:
281         DBG_ERROR("illegal property");
282     }
283     return sal_True;
284 }
285