1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <flddropdown.hxx>
28 
29 #ifndef INCLUDED_ALGORITHM
30 #include <algorithm>
31 #define INCLUDED_ALGORITHM
32 #endif
33 #include <svl/poolitem.hxx>
34 
35 #ifndef _UNOFLDMID_H
36 #include <unofldmid.h>
37 #endif
38 #include <unoprnms.hxx>
39 
40 using namespace com::sun::star;
41 
42 using rtl::OUString;
43 using std::vector;
44 
45 static String aEmptyString;
46 
SwDropDownFieldType()47 SwDropDownFieldType::SwDropDownFieldType()
48     : SwFieldType(RES_DROPDOWN)
49 {
50 }
51 
~SwDropDownFieldType()52 SwDropDownFieldType::~SwDropDownFieldType()
53 {
54 }
55 
Copy() const56 SwFieldType * SwDropDownFieldType::Copy() const
57 {
58     return new SwDropDownFieldType;
59 }
60 
SwDropDownField(SwFieldType * pTyp)61 SwDropDownField::SwDropDownField(SwFieldType * pTyp)
62     : SwField(pTyp, 0, LANGUAGE_SYSTEM)
63 {
64 }
65 
SwDropDownField(const SwDropDownField & rSrc)66 SwDropDownField::SwDropDownField(const SwDropDownField & rSrc)
67     : SwField(rSrc.GetTyp(), rSrc.GetFormat(), rSrc.GetLanguage()),
68       aValues(rSrc.aValues), aSelectedItem(rSrc.aSelectedItem),
69       aName(rSrc.aName), aHelp(rSrc.aHelp), aToolTip(rSrc.aToolTip)
70 {
71 }
72 
~SwDropDownField()73 SwDropDownField::~SwDropDownField()
74 {
75 }
76 
Expand() const77 String SwDropDownField::Expand() const
78 {
79     String sSelect = GetSelectedItem();
80     if(!sSelect.Len())
81     {
82         vector<String>::const_iterator aIt = aValues.begin();
83         if ( aIt != aValues.end())
84             sSelect = *aIt;
85     }
86     //if still no list value is available a default text of 10 spaces is to be set
87     if(!sSelect.Len())
88         sSelect.AppendAscii ( RTL_CONSTASCII_STRINGPARAM ("          "));
89     return sSelect;
90 }
91 
Copy() const92 SwField * SwDropDownField::Copy() const
93 {
94     return new SwDropDownField(*this);
95 }
96 
GetPar1() const97 const String & SwDropDownField::GetPar1() const
98 {
99     return GetSelectedItem();
100 }
101 
GetPar2() const102 String SwDropDownField::GetPar2() const
103 {
104     return GetName();
105 }
106 
SetPar1(const String & rStr)107 void SwDropDownField::SetPar1(const String & rStr)
108 {
109     SetSelectedItem(rStr);
110 }
111 
SetPar2(const String & rName)112 void SwDropDownField::SetPar2(const String & rName)
113 {
114     SetName(rName);
115 }
116 
SetItems(const vector<String> & rItems)117 void SwDropDownField::SetItems(const vector<String> & rItems)
118 {
119     aValues = rItems;
120     aSelectedItem = aEmptyString;
121 }
122 
SetItems(const uno::Sequence<OUString> & rItems)123 void SwDropDownField::SetItems(const uno::Sequence<OUString> & rItems)
124 {
125     aValues.clear();
126 
127     sal_Int32 aCount = rItems.getLength();
128     for (int i = 0; i < aCount; i++)
129         aValues.push_back(rItems[i]);
130 
131     aSelectedItem = aEmptyString;
132 }
133 
GetItemSequence() const134 uno::Sequence<OUString> SwDropDownField::GetItemSequence() const
135 {
136     uno::Sequence<OUString> aSeq( aValues.size() );
137     OUString* pSeq = aSeq.getArray();
138     int i = 0;
139     vector<String>::const_iterator aIt;
140 
141     for (aIt = aValues.begin(); aIt != aValues.end(); aIt++)
142     {
143         pSeq[i] = rtl::OUString(*aIt);
144 
145         i++;
146     }
147 
148     return aSeq;
149 }
150 
GetSelectedItem() const151 const String & SwDropDownField::GetSelectedItem() const
152 {
153     return aSelectedItem;
154 }
155 
GetName() const156 const String & SwDropDownField::GetName() const
157 {
158     return aName;
159 }
160 
GetHelp() const161 const String & SwDropDownField::GetHelp() const
162 {
163     return aHelp;
164 }
165 
GetToolTip() const166 const String & SwDropDownField::GetToolTip() const
167 {
168     return aToolTip;
169 }
170 
SetSelectedItem(const String & rItem)171 sal_Bool SwDropDownField::SetSelectedItem(const String & rItem)
172 {
173     vector<String>::const_iterator aIt =
174         std::find(aValues.begin(), aValues.end(), rItem);
175 
176     if (aIt != aValues.end())
177         aSelectedItem = *aIt;
178     else
179         aSelectedItem = String();
180 
181     return (aIt != aValues.end());
182 }
183 
SetName(const String & rName)184 void SwDropDownField::SetName(const String & rName)
185 {
186     aName = rName;
187 }
188 
SetHelp(const String & rHelp)189 void SwDropDownField::SetHelp(const String & rHelp)
190 {
191     aHelp = rHelp;
192 }
193 
SetToolTip(const String & rToolTip)194 void SwDropDownField::SetToolTip(const String & rToolTip)
195 {
196     aToolTip = rToolTip;
197 }
198 
QueryValue(::uno::Any & rVal,sal_uInt16 nWhich) const199 sal_Bool SwDropDownField::QueryValue(::uno::Any &rVal, sal_uInt16 nWhich) const
200 {
201     nWhich &= ~CONVERT_TWIPS;
202     switch( nWhich )
203     {
204     case FIELD_PROP_PAR1:
205         rVal <<= rtl::OUString(GetSelectedItem());
206         break;
207     case FIELD_PROP_PAR2:
208         rVal <<= rtl::OUString(GetName());
209         break;
210     case FIELD_PROP_PAR3:
211         rVal <<= rtl::OUString(GetHelp());
212         break;
213     case FIELD_PROP_PAR4:
214         rVal <<= rtl::OUString(GetToolTip());
215         break;
216     case FIELD_PROP_STRINGS:
217         rVal <<= GetItemSequence();
218 
219         break;
220 
221     default:
222         DBG_ERROR("illegal property");
223     }
224     return sal_True;
225 }
226 
PutValue(const uno::Any & rVal,sal_uInt16 nWhich)227 sal_Bool SwDropDownField::PutValue(const uno::Any &rVal,
228                                sal_uInt16 nWhich)
229 {
230     switch( nWhich )
231     {
232     case FIELD_PROP_PAR1:
233         {
234             String aTmpStr;
235             ::GetString( rVal, aTmpStr );
236 
237             SetSelectedItem(aTmpStr);
238         }
239         break;
240 
241     case FIELD_PROP_PAR2:
242         {
243             String aTmpStr;
244             ::GetString( rVal, aTmpStr );
245 
246             SetName(aTmpStr);
247         }
248         break;
249 
250     case FIELD_PROP_PAR3:
251         {
252             String aTmpStr;
253             ::GetString( rVal, aTmpStr );
254 
255             SetHelp(aTmpStr);
256         }
257         break;
258 
259     case FIELD_PROP_PAR4:
260         {
261             String aTmpStr;
262             ::GetString( rVal, aTmpStr );
263 
264             SetToolTip(aTmpStr);
265         }
266         break;
267 
268     case FIELD_PROP_STRINGS:
269         {
270             uno::Sequence<OUString> aSeq;
271             rVal >>= aSeq;
272             SetItems(aSeq);
273         }
274         break;
275 
276     default:
277         DBG_ERROR("illegal property");
278     }
279     return sal_True;
280 }
281