Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <flddropdown.hxx>
21 :
22 : #include <algorithm>
23 : #include <svl/poolitem.hxx>
24 :
25 : #include <unofldmid.h>
26 : #include <unoprnms.hxx>
27 :
28 : using namespace com::sun::star;
29 :
30 : using std::vector;
31 :
32 33 : static String aEmptyString;
33 :
34 898 : SwDropDownFieldType::SwDropDownFieldType()
35 898 : : SwFieldType(RES_DROPDOWN)
36 : {
37 898 : }
38 :
39 1790 : SwDropDownFieldType::~SwDropDownFieldType()
40 : {
41 1790 : }
42 :
43 0 : SwFieldType * SwDropDownFieldType::Copy() const
44 : {
45 0 : return new SwDropDownFieldType;
46 : }
47 :
48 0 : SwDropDownField::SwDropDownField(SwFieldType * pTyp)
49 0 : : SwField(pTyp, 0, LANGUAGE_SYSTEM)
50 : {
51 0 : }
52 :
53 0 : SwDropDownField::SwDropDownField(const SwDropDownField & rSrc)
54 0 : : SwField(rSrc.GetTyp(), rSrc.GetFormat(), rSrc.GetLanguage()),
55 : aValues(rSrc.aValues), aSelectedItem(rSrc.aSelectedItem),
56 0 : aName(rSrc.aName), aHelp(rSrc.aHelp), aToolTip(rSrc.aToolTip)
57 : {
58 0 : }
59 :
60 0 : SwDropDownField::~SwDropDownField()
61 : {
62 0 : }
63 :
64 0 : String SwDropDownField::Expand() const
65 : {
66 0 : String sSelect = GetSelectedItem();
67 0 : if(!sSelect.Len())
68 : {
69 0 : vector<OUString>::const_iterator aIt = aValues.begin();
70 0 : if ( aIt != aValues.end())
71 0 : sSelect = *aIt;
72 : }
73 : // if still no list value is available a default text of 10 spaces is to be set
74 0 : if(!sSelect.Len())
75 0 : sSelect.AppendAscii ( RTL_CONSTASCII_STRINGPARAM (" "));
76 0 : return sSelect;
77 : }
78 :
79 0 : SwField * SwDropDownField::Copy() const
80 : {
81 0 : return new SwDropDownField(*this);
82 : }
83 :
84 0 : const OUString & SwDropDownField::GetPar1() const
85 : {
86 0 : return GetSelectedItem();
87 : }
88 :
89 0 : OUString SwDropDownField::GetPar2() const
90 : {
91 0 : return GetName();
92 : }
93 :
94 0 : void SwDropDownField::SetPar1(const OUString & rStr)
95 : {
96 0 : SetSelectedItem(rStr);
97 0 : }
98 :
99 0 : void SwDropDownField::SetPar2(const OUString & rName)
100 : {
101 0 : SetName(rName);
102 0 : }
103 :
104 0 : void SwDropDownField::SetItems(const vector<OUString> & rItems)
105 : {
106 0 : aValues = rItems;
107 0 : aSelectedItem = aEmptyString;
108 0 : }
109 :
110 0 : void SwDropDownField::SetItems(const uno::Sequence<OUString> & rItems)
111 : {
112 0 : aValues.clear();
113 :
114 0 : sal_Int32 aCount = rItems.getLength();
115 0 : for (int i = 0; i < aCount; i++)
116 0 : aValues.push_back(rItems[i]);
117 :
118 0 : aSelectedItem = aEmptyString;
119 0 : }
120 :
121 0 : uno::Sequence<OUString> SwDropDownField::GetItemSequence() const
122 : {
123 0 : uno::Sequence<OUString> aSeq( aValues.size() );
124 0 : OUString* pSeq = aSeq.getArray();
125 0 : int i = 0;
126 0 : vector<OUString>::const_iterator aIt;
127 :
128 0 : for (aIt = aValues.begin(); aIt != aValues.end(); ++aIt)
129 : {
130 0 : pSeq[i] = *aIt;
131 0 : i++;
132 : }
133 :
134 0 : return aSeq;
135 : }
136 :
137 0 : const OUString & SwDropDownField::GetSelectedItem() const
138 : {
139 0 : return aSelectedItem;
140 : }
141 :
142 0 : const OUString & SwDropDownField::GetName() const
143 : {
144 0 : return aName;
145 : }
146 :
147 0 : const OUString & SwDropDownField::GetHelp() const
148 : {
149 0 : return aHelp;
150 : }
151 :
152 0 : const OUString & SwDropDownField::GetToolTip() const
153 : {
154 0 : return aToolTip;
155 : }
156 :
157 0 : sal_Bool SwDropDownField::SetSelectedItem(const OUString & rItem)
158 : {
159 : vector<OUString>::const_iterator aIt =
160 0 : std::find(aValues.begin(), aValues.end(), rItem);
161 :
162 0 : if (aIt != aValues.end())
163 0 : aSelectedItem = *aIt;
164 : else
165 0 : aSelectedItem = OUString();
166 :
167 0 : return (aIt != aValues.end());
168 : }
169 :
170 0 : void SwDropDownField::SetName(const OUString & rName)
171 : {
172 0 : aName = rName;
173 0 : }
174 :
175 0 : void SwDropDownField::SetHelp(const OUString & rHelp)
176 : {
177 0 : aHelp = rHelp;
178 0 : }
179 :
180 0 : void SwDropDownField::SetToolTip(const OUString & rToolTip)
181 : {
182 0 : aToolTip = rToolTip;
183 0 : }
184 :
185 0 : bool SwDropDownField::QueryValue(::uno::Any &rVal, sal_uInt16 nWhich) const
186 : {
187 0 : nWhich &= ~CONVERT_TWIPS;
188 0 : switch( nWhich )
189 : {
190 : case FIELD_PROP_PAR1:
191 0 : rVal <<= GetSelectedItem();
192 0 : break;
193 : case FIELD_PROP_PAR2:
194 0 : rVal <<= GetName();
195 0 : break;
196 : case FIELD_PROP_PAR3:
197 0 : rVal <<= GetHelp();
198 0 : break;
199 : case FIELD_PROP_PAR4:
200 0 : rVal <<= GetToolTip();
201 0 : break;
202 : case FIELD_PROP_STRINGS:
203 0 : rVal <<= GetItemSequence();
204 :
205 0 : break;
206 :
207 : default:
208 : OSL_FAIL("illegal property");
209 : }
210 0 : return true;
211 : }
212 :
213 0 : bool SwDropDownField::PutValue(const uno::Any &rVal,
214 : sal_uInt16 nWhich)
215 : {
216 0 : switch( nWhich )
217 : {
218 : case FIELD_PROP_PAR1:
219 : {
220 0 : OUString aTmpStr;
221 0 : rVal >>= aTmpStr;
222 :
223 0 : SetSelectedItem(aTmpStr);
224 : }
225 0 : break;
226 :
227 : case FIELD_PROP_PAR2:
228 : {
229 0 : OUString aTmpStr;
230 0 : rVal >>= aTmpStr;
231 :
232 0 : SetName(aTmpStr);
233 : }
234 0 : break;
235 :
236 : case FIELD_PROP_PAR3:
237 : {
238 0 : OUString aTmpStr;
239 0 : rVal >>= aTmpStr;
240 :
241 0 : SetHelp(aTmpStr);
242 : }
243 0 : break;
244 :
245 : case FIELD_PROP_PAR4:
246 : {
247 0 : OUString aTmpStr;
248 0 : rVal >>= aTmpStr;
249 :
250 0 : SetToolTip(aTmpStr);
251 : }
252 0 : break;
253 :
254 : case FIELD_PROP_STRINGS:
255 : {
256 0 : uno::Sequence<OUString> aSeq;
257 0 : rVal >>= aSeq;
258 0 : SetItems(aSeq);
259 : }
260 0 : break;
261 :
262 : default:
263 : OSL_FAIL("illegal property");
264 : }
265 0 : return true;
266 99 : }
267 :
268 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|