LibreOffice Module sw (master)  1
flddropdown.cxx
Go to the documentation of this file.
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 
24 #include <svl/poolitem.hxx>
25 #include <comphelper/sequence.hxx>
26 
27 #include <unofldmid.h>
28 
29 using namespace com::sun::star;
30 
31 using std::vector;
32 
35 {
36 }
37 
39 {
40 }
41 
42 std::unique_ptr<SwFieldType> SwDropDownFieldType::Copy() const
43 {
44  return std::make_unique<SwDropDownFieldType>();
45 }
46 
48  : SwField(pTyp, 0, LANGUAGE_SYSTEM)
49 {
50 }
51 
53  : SwField(rSrc.GetTyp(), rSrc.GetFormat(), rSrc.GetLanguage()),
54  aValues(rSrc.aValues), aSelectedItem(rSrc.aSelectedItem),
55  aName(rSrc.aName), aHelp(rSrc.aHelp), aToolTip(rSrc.aToolTip)
56 {
57 }
58 
60 {
61 }
62 
63 OUString SwDropDownField::ExpandImpl(SwRootFrame const*const) const
64 {
65  OUString sSelect = GetSelectedItem();
66  if (sSelect.isEmpty())
67  {
68  vector<OUString>::const_iterator aIt = aValues.begin();
69  if ( aIt != aValues.end())
70  sSelect = *aIt;
71  }
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;
76 }
77 
78 std::unique_ptr<SwField> SwDropDownField::Copy() const
79 {
80  return std::make_unique<SwDropDownField>(*this);
81 }
82 
83 OUString SwDropDownField::GetPar1() const
84 {
85  return GetSelectedItem();
86 }
87 
88 OUString SwDropDownField::GetPar2() const
89 {
90  return GetName();
91 }
92 
93 void SwDropDownField::SetPar1(const OUString & rStr)
94 {
95  SetSelectedItem(rStr);
96 }
97 
98 void SwDropDownField::SetPar2(const OUString & rName)
99 {
100  SetName(rName);
101 }
102 
103 void SwDropDownField::SetItems(const vector<OUString> & rItems)
104 {
105  aValues = rItems;
106  aSelectedItem.clear();
107 }
108 
109 void SwDropDownField::SetItems(const uno::Sequence<OUString> & rItems)
110 {
111  aValues.clear();
112 
114 
115  aSelectedItem.clear();
116 }
117 
118 uno::Sequence<OUString> SwDropDownField::GetItemSequence() const
119 {
121 }
122 
123 
124 void SwDropDownField::SetSelectedItem(const OUString & rItem)
125 {
126  vector<OUString>::const_iterator aIt =
127  std::find(aValues.begin(), aValues.end(), rItem);
128 
129  if (aIt != aValues.end())
130  aSelectedItem = *aIt;
131  else
132  aSelectedItem.clear();
133 }
134 
135 void SwDropDownField::SetName(const OUString & rName)
136 {
137  aName = rName;
138 }
139 
140 void SwDropDownField::SetHelp(const OUString & rHelp)
141 {
142  aHelp = rHelp;
143 }
144 
145 void SwDropDownField::SetToolTip(const OUString & rToolTip)
146 {
147  aToolTip = rToolTip;
148 }
149 
150 bool SwDropDownField::QueryValue(::uno::Any &rVal, sal_uInt16 nWhich) const
151 {
152  nWhich &= ~CONVERT_TWIPS;
153  switch( nWhich )
154  {
155  case FIELD_PROP_PAR1:
156  rVal <<= aSelectedItem;
157  break;
158  case FIELD_PROP_PAR2:
159  rVal <<= aName;
160  break;
161  case FIELD_PROP_PAR3:
162  rVal <<= aHelp;
163  break;
164  case FIELD_PROP_PAR4:
165  rVal <<= aToolTip;
166  break;
167  case FIELD_PROP_STRINGS:
168  rVal <<= GetItemSequence();
169  break;
170 
171  default:
172  assert(false);
173  }
174  return true;
175 }
176 
178  sal_uInt16 nWhich)
179 {
180  switch( nWhich )
181  {
182  case FIELD_PROP_PAR1:
183  {
184  OUString aTmpStr;
185  rVal >>= aTmpStr;
186 
187  SetSelectedItem(aTmpStr);
188  }
189  break;
190 
191  case FIELD_PROP_PAR2:
192  rVal >>= aName;
193  break;
194 
195  case FIELD_PROP_PAR3:
196  rVal >>= aHelp;
197  break;
198 
199  case FIELD_PROP_PAR4:
200  rVal >>= aToolTip;
201  break;
202 
203  case FIELD_PROP_STRINGS:
204  {
205  uno::Sequence<OUString> aSeq;
206  rVal >>= aSeq;
207  SetItems(aSeq);
208  }
209  break;
210 
211  default:
212  assert(false);
213  }
214  return true;
215 }
216 
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetToolTip(const OUString &rToolTip)
Sets the tool tip of the field.
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:234
OUString aName
the name of the field
Definition: flddropdown.hxx:73
virtual void SetPar1(const OUString &rStr) override
Sets the selected value.
Definition: flddropdown.cxx:93
virtual OUString GetPar1() const override
Returns the selected value.
Definition: flddropdown.cxx:83
#define FIELD_PROP_STRINGS
Definition: unofldmid.h:42
Base class of all fields.
Definition: fldbas.hxx:280
LanguageType GetLanguage(SfxItemSet const &aSet, sal_uInt16 nLangWhichId)
Definition: langhelper.cxx:402
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Expands the field.
Definition: flddropdown.cxx:63
const OUString & GetName() const
Returns the name of the field.
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
virtual void SetPar2(const OUString &rStr) override
Sets the name of the field.
Definition: flddropdown.cxx:98
DstType sequenceToContainer(const css::uno::Sequence< SrcType > &i_Sequence)
OUString aSelectedItem
the selected item
Definition: flddropdown.hxx:68
const OUString & GetSelectedItem() const
Returns the selected item.
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt16 nWhichId) const override
API: Gets a property value from the dropdown field.
void SetSelectedItem(const OUString &rItem)
Sets the selected item.
std::vector< OUString > aValues
the possible values (aka items) of the dropdown box
Definition: flddropdown.hxx:63
virtual std::unique_ptr< SwField > Copy() const override
Creates a copy of this field.
Definition: flddropdown.cxx:78
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt16 nWhichId) override
API: Sets a property value on the dropdown field.
#define LANGUAGE_SYSTEM
SwFieldIds
Definition: fldbas.hxx:38
OUString aHelp
help text
Definition: flddropdown.hxx:78
OUString aToolTip
tool tip string
Definition: flddropdown.hxx:83
#define FIELD_PROP_PAR3
Definition: unofldmid.h:25
Dropdown field.
Definition: flddropdown.hxx:58
void SetName(const OUString &rName)
Sets the name of the field.
SwDropDownField(SwFieldType *pTyp)
Constructor.
Definition: flddropdown.cxx:47
SwDropDownFieldType()
Constructor.
Definition: flddropdown.cxx:33
virtual OUString GetPar2() const override
Returns the name of the field.
Definition: flddropdown.cxx:88
#define FIELD_PROP_PAR1
Definition: unofldmid.h:23
virtual ~SwDropDownFieldType() override
Destructor.
Definition: flddropdown.cxx:38
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
OUString aName
virtual ~SwDropDownField() override
Destructor.
Definition: flddropdown.cxx:59
Sequence< sal_Int8 > aSeq
virtual std::unique_ptr< SwFieldType > Copy() const override
Create a copy of this field type.
Definition: flddropdown.cxx:42
void SetHelp(const OUString &rHelp)
Sets the help text of the field.
#define FIELD_PROP_PAR4
Definition: unofldmid.h:36
void SetItems(const std::vector< OUString > &rItems)
Sets the items of the dropdown box.
css::uno::Sequence< OUString > GetItemSequence() const
Returns the items of the dropdown box.
#define CONVERT_TWIPS