LibreOffice Module sw (master) 1
vbacontentcontrollistentry.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
11
12using namespace ::ooo::vba;
13using namespace ::com::sun::star;
14
16 const uno::Reference<ooo::vba::XHelperInterface>& rParent,
17 const uno::Reference<uno::XComponentContext>& rContext, std::shared_ptr<SwContentControl> pCC,
18 size_t nZIndex)
19 : SwVbaContentControlListEntry_BASE(rParent, rContext)
20 , m_pCC(pCC)
21 , m_nZIndex(nZIndex)
22{
23}
24
26
28
30{
31 if (nSet < 1 || static_cast<size_t>(nSet) == m_nZIndex + 1)
32 return;
33
34 // Given a one-based index to set to
35 size_t nIndex = std::min(static_cast<size_t>(nSet), m_pCC->GetListItems().size());
36 // change to zero-based index
37 --nIndex;
38 while (nIndex < m_nZIndex)
39 MoveUp();
40 while (m_nZIndex < nIndex)
41 MoveDown();
42}
43
45{
46 assert(m_nZIndex < m_pCC->GetListItems().size());
47 const SwContentControlListItem& rListItem = m_pCC->GetListItems()[m_nZIndex];
48 return rListItem.ToString();
49}
50
51void SwVbaContentControlListEntry::setText(const OUString& rSet)
52{
53 std::vector<SwContentControlListItem> vListItems = m_pCC->GetListItems();
54 assert(m_nZIndex < vListItems.size());
55
56 // prevent duplicates
57 for (size_t i = 0; i < vListItems.size(); ++i)
58 {
59 if (vListItems[i].ToString() == rSet)
60 return;
61 }
62
63 const std::optional<size_t> oSel(m_pCC->GetSelectedListItem(/*bCheckDocModel=*/true));
64 const bool bNeedsInvalidation = m_pCC->GetDropDown() && oSel && *oSel == m_nZIndex;
65
66 vListItems[m_nZIndex].m_aDisplayText = rSet;
67 m_pCC->SetListItems(vListItems);
68
69 if (bNeedsInvalidation)
70 {
71 m_pCC->SetSelectedListItem(m_nZIndex);
72 if (m_pCC->GetTextAttr())
73 m_pCC->GetTextAttr()->Invalidate();
74 }
75}
76
78{
79 assert(m_nZIndex < m_pCC->GetListItems().size());
80 const SwContentControlListItem& rListItem = m_pCC->GetListItems()[m_nZIndex];
81
82 return rListItem.m_aValue;
83}
84
86{
87 assert(m_nZIndex < m_pCC->GetListItems().size());
88 std::vector<SwContentControlListItem> vListItems = m_pCC->GetListItems();
89
90 // LO may pull the display text from Value. Ensure changing Value doesn't alter display text.
91 if (vListItems[m_nZIndex].m_aDisplayText.isEmpty())
92 vListItems[m_nZIndex].m_aDisplayText = vListItems[m_nZIndex].ToString();
93
94 vListItems[m_nZIndex].m_aValue = rSet;
95 m_pCC->SetListItems(vListItems);
96}
97
99
101{
102 // if already at last position, can't move down
103 if (m_nZIndex >= m_pCC->GetListItems().size() - 1)
104 return;
105
106 const std::optional<size_t> oSelected = m_pCC->GetSelectedListItem(/*bCheckDocModel=*/false);
107 if (oSelected)
108 {
109 if (*oSelected == m_nZIndex)
110 m_pCC->SetSelectedListItem(m_nZIndex + 1);
111 else if (*oSelected == m_nZIndex + 1)
112 m_pCC->SetSelectedListItem(*oSelected - 1);
113 }
114 std::vector<SwContentControlListItem> vListItems = m_pCC->GetListItems();
115 std::swap(vListItems[m_nZIndex], vListItems[m_nZIndex + 1]);
116 m_pCC->SetListItems(vListItems);
117 ++m_nZIndex;
118}
119
121{
122 // if already at position 0, can't move up
123 if (!m_nZIndex || m_nZIndex >= m_pCC->GetListItems().size())
124 return;
125
126 const std::optional<size_t> oSelected = m_pCC->GetSelectedListItem(/*bCheckDocModel=*/false);
127 if (oSelected)
128 {
129 if (*oSelected == m_nZIndex)
130 m_pCC->SetSelectedListItem(m_nZIndex - 1);
131 else if (*oSelected == m_nZIndex - 1)
132 m_pCC->SetSelectedListItem(*oSelected + 1);
133 }
134 std::vector<SwContentControlListItem> vListItems = m_pCC->GetListItems();
135 std::swap(vListItems[m_nZIndex], vListItems[m_nZIndex - 1]);
136 m_pCC->SetListItems(vListItems);
137 --m_nZIndex;
138}
139
141{
142 assert(m_nZIndex < m_pCC->GetListItems().size());
143 m_pCC->SetSelectedListItem(m_nZIndex);
144 m_pCC->SetShowingPlaceHolder(false);
145 if (m_pCC->GetTextAttr())
146 m_pCC->GetTextAttr()->Invalidate();
147}
148
149// XHelperInterface
151{
152 return "SwVbaContentControlListEntry";
153}
154
156{
157 static uno::Sequence<OUString> const aServiceNames{ "ooo.vba.word.ContentControlListEntry" };
158 return aServiceNames;
159}
160
161/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Represents one list item in a content control dropdown list.
OUString m_aValue
This must not be empty.
const OUString & ToString() const
void SAL_CALL setText(const OUString &sSet) override
css::uno::Sequence< OUString > getServiceNames() override
sal_Int32 SAL_CALL getIndex() override
OUString SAL_CALL getText() override
void SAL_CALL setIndex(sal_Int32 nSet) override
OUString SAL_CALL getValue() override
SwVbaContentControlListEntry(const css::uno::Reference< ooo::vba::XHelperInterface > &rParent, const css::uno::Reference< css::uno::XComponentContext > &rContext, std::shared_ptr< SwContentControl > pCC, size_t nZIndex)
std::shared_ptr< SwContentControl > m_pCC
void SAL_CALL setValue(const OUString &sSet) override
Sequence< OUString > aServiceNames
sal_Int32 nIndex
size
int i
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
static SfxItemSet & rSet