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 #include <unoprnms.hxx>
29 
30 using namespace com::sun::star;
31 
32 using std::vector;
33 
36 {
37 }
38 
40 {
41 }
42 
43 std::unique_ptr<SwFieldType> SwDropDownFieldType::Copy() const
44 {
45  return std::make_unique<SwDropDownFieldType>();
46 }
47 
49  : SwField(pTyp, 0, LANGUAGE_SYSTEM)
50 {
51 }
52 
54  : SwField(rSrc.GetTyp(), rSrc.GetFormat(), rSrc.GetLanguage()),
55  aValues(rSrc.aValues), aSelectedItem(rSrc.aSelectedItem),
56  aName(rSrc.aName), aHelp(rSrc.aHelp), aToolTip(rSrc.aToolTip)
57 {
58 }
59 
61 {
62 }
63 
64 OUString SwDropDownField::ExpandImpl(SwRootFrame const*const) const
65 {
66  OUString sSelect = GetSelectedItem();
67  if (sSelect.isEmpty())
68  {
69  vector<OUString>::const_iterator aIt = aValues.begin();
70  if ( aIt != aValues.end())
71  sSelect = *aIt;
72  }
73  // if still no list value is available a default text of 10 spaces is to be set
74  if (sSelect.isEmpty())
75  sSelect = " ";
76  return sSelect;
77 }
78 
79 std::unique_ptr<SwField> SwDropDownField::Copy() const
80 {
81  return std::make_unique<SwDropDownField>(*this);
82 }
83 
84 OUString SwDropDownField::GetPar1() const
85 {
86  return GetSelectedItem();
87 }
88 
89 OUString SwDropDownField::GetPar2() const
90 {
91  return GetName();
92 }
93 
94 void SwDropDownField::SetPar1(const OUString & rStr)
95 {
96  SetSelectedItem(rStr);
97 }
98 
99 void SwDropDownField::SetPar2(const OUString & rName)
100 {
101  SetName(rName);
102 }
103 
104 void SwDropDownField::SetItems(const vector<OUString> & rItems)
105 {
106  aValues = rItems;
107  aSelectedItem.clear();
108 }
109 
110 void SwDropDownField::SetItems(const uno::Sequence<OUString> & rItems)
111 {
112  aValues.clear();
113 
115 
116  aSelectedItem.clear();
117 }
118 
119 uno::Sequence<OUString> SwDropDownField::GetItemSequence() const
120 {
122 }
123 
124 
125 void SwDropDownField::SetSelectedItem(const OUString & rItem)
126 {
127  vector<OUString>::const_iterator aIt =
128  std::find(aValues.begin(), aValues.end(), rItem);
129 
130  if (aIt != aValues.end())
131  aSelectedItem = *aIt;
132  else
133  aSelectedItem.clear();
134 }
135 
136 void SwDropDownField::SetName(const OUString & rName)
137 {
138  aName = rName;
139 }
140 
141 void SwDropDownField::SetHelp(const OUString & rHelp)
142 {
143  aHelp = rHelp;
144 }
145 
146 void SwDropDownField::SetToolTip(const OUString & rToolTip)
147 {
148  aToolTip = rToolTip;
149 }
150 
151 bool SwDropDownField::QueryValue(::uno::Any &rVal, sal_uInt16 nWhich) const
152 {
153  nWhich &= ~CONVERT_TWIPS;
154  switch( nWhich )
155  {
156  case FIELD_PROP_PAR1:
157  rVal <<= aSelectedItem;
158  break;
159  case FIELD_PROP_PAR2:
160  rVal <<= aName;
161  break;
162  case FIELD_PROP_PAR3:
163  rVal <<= aHelp;
164  break;
165  case FIELD_PROP_PAR4:
166  rVal <<= aToolTip;
167  break;
168  case FIELD_PROP_STRINGS:
169  rVal <<= GetItemSequence();
170  break;
171 
172  default:
173  assert(false);
174  }
175  return true;
176 }
177 
179  sal_uInt16 nWhich)
180 {
181  switch( nWhich )
182  {
183  case FIELD_PROP_PAR1:
184  {
185  OUString aTmpStr;
186  rVal >>= aTmpStr;
187 
188  SetSelectedItem(aTmpStr);
189  }
190  break;
191 
192  case FIELD_PROP_PAR2:
193  rVal >>= aName;
194  break;
195 
196  case FIELD_PROP_PAR3:
197  rVal >>= aHelp;
198  break;
199 
200  case FIELD_PROP_PAR4:
201  rVal >>= aToolTip;
202  break;
203 
204  case FIELD_PROP_STRINGS:
205  {
206  uno::Sequence<OUString> aSeq;
207  rVal >>= aSeq;
208  SetItems(aSeq);
209  }
210  break;
211 
212  default:
213  assert(false);
214  }
215  return true;
216 }
217 
218 /* 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:233
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:94
virtual OUString GetPar1() const override
Returns the selected value.
Definition: flddropdown.cxx:84
#define FIELD_PROP_STRINGS
Definition: unofldmid.h:42
Base class of all fields.
Definition: fldbas.hxx:279
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:64
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:99
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:79
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:48
SwDropDownFieldType()
Constructor.
Definition: flddropdown.cxx:34
virtual OUString GetPar2() const override
Returns the name of the field.
Definition: flddropdown.cxx:89
#define FIELD_PROP_PAR1
Definition: unofldmid.h:23
virtual ~SwDropDownFieldType() override
Destructor.
Definition: flddropdown.cxx:39
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
virtual ~SwDropDownField() override
Destructor.
Definition: flddropdown.cxx:60
virtual std::unique_ptr< SwFieldType > Copy() const override
Create a copy of this field type.
Definition: flddropdown.cxx:43
OString const aName
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