android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / vba / vbacontentcontrollistentry.cxx
blob73f5e9d0a2cf054d72ed474e8f8c1fc7a04c1ae2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include "vbacontentcontrollistentry.hxx"
12 using namespace ::ooo::vba;
13 using namespace ::com::sun::star;
15 SwVbaContentControlListEntry::SwVbaContentControlListEntry(
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)
25 SwVbaContentControlListEntry::~SwVbaContentControlListEntry() {}
27 sal_Int32 SwVbaContentControlListEntry::getIndex() { return m_nZIndex + 1; }
29 void SwVbaContentControlListEntry::setIndex(sal_Int32 nSet)
31 if (nSet < 1 || static_cast<size_t>(nSet) == m_nZIndex + 1)
32 return;
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();
44 OUString SwVbaContentControlListEntry::getText()
46 assert(m_nZIndex < m_pCC->GetListItems().size());
47 const SwContentControlListItem& rListItem = m_pCC->GetListItems()[m_nZIndex];
48 return rListItem.ToString();
51 void SwVbaContentControlListEntry::setText(const OUString& rSet)
53 std::vector<SwContentControlListItem> vListItems = m_pCC->GetListItems();
54 assert(m_nZIndex < vListItems.size());
56 // prevent duplicates
57 for (size_t i = 0; i < vListItems.size(); ++i)
59 if (vListItems[i].ToString() == rSet)
60 return;
63 const std::optional<size_t> oSel(m_pCC->GetSelectedListItem(/*bCheckDocModel=*/true));
64 const bool bNeedsInvalidation = m_pCC->GetDropDown() && oSel && *oSel == m_nZIndex;
66 vListItems[m_nZIndex].m_aDisplayText = rSet;
67 m_pCC->SetListItems(vListItems);
69 if (bNeedsInvalidation)
71 m_pCC->SetSelectedListItem(m_nZIndex);
72 if (m_pCC->GetTextAttr())
73 m_pCC->GetTextAttr()->Invalidate();
77 OUString SwVbaContentControlListEntry::getValue()
79 assert(m_nZIndex < m_pCC->GetListItems().size());
80 const SwContentControlListItem& rListItem = m_pCC->GetListItems()[m_nZIndex];
82 return rListItem.m_aValue;
85 void SwVbaContentControlListEntry::setValue(const OUString& rSet)
87 assert(m_nZIndex < m_pCC->GetListItems().size());
88 std::vector<SwContentControlListItem> vListItems = m_pCC->GetListItems();
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();
94 vListItems[m_nZIndex].m_aValue = rSet;
95 m_pCC->SetListItems(vListItems);
98 void SwVbaContentControlListEntry::Delete() { m_pCC->DeleteListItem(m_nZIndex); }
100 void SwVbaContentControlListEntry::MoveDown()
102 // if already at last position, can't move down
103 if (m_nZIndex >= m_pCC->GetListItems().size() - 1)
104 return;
106 const std::optional<size_t> oSelected = m_pCC->GetSelectedListItem(/*bCheckDocModel=*/false);
107 if (oSelected)
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);
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;
120 void SwVbaContentControlListEntry::MoveUp()
122 // if already at position 0, can't move up
123 if (!m_nZIndex || m_nZIndex >= m_pCC->GetListItems().size())
124 return;
126 const std::optional<size_t> oSelected = m_pCC->GetSelectedListItem(/*bCheckDocModel=*/false);
127 if (oSelected)
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);
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;
140 void SwVbaContentControlListEntry::Select()
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();
149 // XHelperInterface
150 OUString SwVbaContentControlListEntry::getServiceImplName()
152 return "SwVbaContentControlListEntry";
155 uno::Sequence<OUString> SwVbaContentControlListEntry::getServiceNames()
157 static uno::Sequence<OUString> const aServiceNames{ "ooo.vba.word.ContentControlListEntry" };
158 return aServiceNames;
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */