LibreOffice Module sw (master) 1
vbacontentcontrollistentries.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
15namespace
16{
17class ContentControlListEntriesEnumWrapper : public EnumerationHelper_BASE
18{
19 uno::Reference<container::XIndexAccess> mxIndexAccess;
20 sal_Int32 mnIndex;
21
22public:
23 explicit ContentControlListEntriesEnumWrapper(
24 uno::Reference<container::XIndexAccess> xIndexAccess)
25 : mxIndexAccess(xIndexAccess)
26 , mnIndex(0)
27 {
28 }
29
30 virtual sal_Bool SAL_CALL hasMoreElements() override
31 {
32 return (mnIndex < mxIndexAccess->getCount());
33 }
34
35 virtual uno::Any SAL_CALL nextElement() override
36 {
37 if (mnIndex < mxIndexAccess->getCount())
38 {
39 return mxIndexAccess->getByIndex(mnIndex++);
40 }
41 throw container::NoSuchElementException();
42 }
43};
44
45class ContentControlListEntryCollectionHelper
46 : public ::cppu::WeakImplHelper<container::XIndexAccess, container::XEnumerationAccess>
47{
48private:
49 uno::Reference<XHelperInterface> mxParent;
50 uno::Reference<uno::XComponentContext> mxContext;
51 std::shared_ptr<SwContentControl> m_pCC;
52
53public:
55 ContentControlListEntryCollectionHelper(uno::Reference<ov::XHelperInterface> xParent,
56 uno::Reference<uno::XComponentContext> xContext,
57 std::shared_ptr<SwContentControl> pCC)
58 : mxParent(xParent)
59 , mxContext(xContext)
60 , m_pCC(pCC)
61 {
62 }
63
64 sal_Int32 SAL_CALL getCount() override { return m_pCC->GetListItems().size(); }
65
66 uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
67 {
68 if (Index < 0 || Index >= getCount())
69 throw lang::IndexOutOfBoundsException();
70
71 return uno::Any(uno::Reference<word::XContentControlListEntry>(
72 new SwVbaContentControlListEntry(mxParent, mxContext, m_pCC, Index)));
73 }
74
75 uno::Type SAL_CALL getElementType() override
76 {
78 }
79
80 sal_Bool SAL_CALL hasElements() override { return getCount() != 0; }
81
82 // XEnumerationAccess
83 uno::Reference<container::XEnumeration> SAL_CALL createEnumeration() override
84 {
85 return new ContentControlListEntriesEnumWrapper(this);
86 }
87};
88}
89
95 const uno::Reference<XHelperInterface>& xParent,
96 const uno::Reference<uno::XComponentContext>& xContext, std::shared_ptr<SwContentControl> pCC)
98 xParent, xContext,
99 uno::Reference<container::XIndexAccess>(
100 new ContentControlListEntryCollectionHelper(xParent, xContext, pCC)))
101 , m_pCC(pCC)
102{
103}
104
105uno::Reference<word::XContentControlListEntry>
106SwVbaContentControlListEntries::Add(const OUString& rName, const uno::Any& rValue,
107 const uno::Any& rIndex)
108{
109 // No duplicate Names allowed in VBA
110 for (auto& rListItem : m_pCC->GetListItems())
111 {
112 if (rListItem.ToString() == rName)
113 return uno::Reference<word::XContentControlListEntry>();
114 }
115
116 sal_Int32 nZIndex = SAL_MAX_INT32;
117 rIndex >>= nZIndex;
118 // rIndex is 1-based, nZIndex is 0-based. If rIndex is not given, then add as the last choice.
119 assert(nZIndex > 0);
120 --nZIndex;
121 nZIndex = std::min(static_cast<size_t>(nZIndex), m_pCC->GetListItems().size());
122
123 OUString sValue;
124 rValue >>= sValue;
125 if (m_pCC->AddListItem(nZIndex, rName, sValue))
126 {
127 return uno::Reference<word::XContentControlListEntry>(
129 }
130
131 return uno::Reference<word::XContentControlListEntry>();
132}
133
134void SwVbaContentControlListEntries::Clear() { m_pCC->ClearListItems(); }
135
136sal_Int32 SwVbaContentControlListEntries::getCount() { return m_pCC->GetListItems().size(); }
137
138// XEnumerationAccess
140{
142}
143
144uno::Reference<container::XEnumeration> SwVbaContentControlListEntries::createEnumeration()
145{
146 return new ContentControlListEntriesEnumWrapper(m_xIndexAccess);
147}
148
149// SwVbaContentControlListEntries_BASE
151{
152 return aSource;
153}
154
156{
157 return "SwVbaContentControlListEntries";
158}
159
161{
162 static uno::Sequence<OUString> const sNames{ "ooo.vba.word.ContentControlListEntries" };
163 return sNames;
164}
165
166/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
unotools::WeakReference< AnimationNode > mxParent
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::WeakReference< ov::XHelperInterface > mxParent
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
std::shared_ptr< SwContentControl > m_pCC
css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
SwVbaContentControlListEntries(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, std::shared_ptr< SwContentControl > pCC)
DropDownLists and ComboBoxes contain a list of name/value pairs to choose from.
css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
css::uno::Reference< ooo::vba::word::XContentControlListEntry > SAL_CALL Add(const OUString &rName, const css::uno::Any &rValue, const css::uno::Any &rIndex) override
css::uno::Sequence< OUString > getServiceNames() override
css::uno::Type SAL_CALL getElementType() override
css::uno::Type const & get()
uno::Reference< uno::XComponentContext > mxContext
Reference
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
sal_uInt32 mnIndex
#define SAL_MAX_INT32
unsigned char sal_Bool
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE