LibreOffice Module vbahelper (master) 1
vbalistcontrolhelper.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
21#include <utility>
22#include <vector>
24#include <com/sun/star/beans/XPropertySet.hpp>
26
27using namespace com::sun::star;
28using namespace ooo::vba;
29
30namespace {
31
32class ListPropListener : public PropListener
33{
34private:
35 uno::Reference< beans::XPropertySet > m_xProps;
36 uno::Any m_pvargIndex;
37 uno::Any m_pvarColumn;
38
39public:
40 ListPropListener( uno::Reference< beans::XPropertySet > xProps, uno::Any pvargIndex, uno::Any varColumn );
41 virtual ~ListPropListener() { };
42 virtual void setValueEvent( const css::uno::Any& value ) override;
43 virtual css::uno::Any getValueEvent() override;
44};
45
46}
47
48ListPropListener::ListPropListener( uno::Reference< beans::XPropertySet > xProps, uno::Any vargIndex, uno::Any varColumn ) : m_xProps(std::move( xProps )), m_pvargIndex(std::move( vargIndex )), m_pvarColumn(std::move( varColumn ))
49{
50}
51
52void ListPropListener::setValueEvent( const uno::Any& value )
53{
54 if( m_pvargIndex.hasValue() || m_pvarColumn.hasValue() )
55 throw uno::RuntimeException( "Bad argument" );
56
57 m_xProps->setPropertyValue( "StringItemList", value );
58}
59
60uno::Any ListPropListener::getValueEvent()
61{
62 uno::Sequence< OUString > sList;
63 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
64 sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
65 uno::Any aRet;
66 if ( m_pvargIndex.hasValue() )
67 {
68 sal_Int16 nIndex = -1;
69 m_pvargIndex >>= nIndex;
70 if( nIndex < 0 || nIndex >= nLength )
71 throw uno::RuntimeException( "Bad row Index" );
72 aRet <<= sList[ nIndex ];
73 }
74 else if ( m_pvarColumn.hasValue() ) // pvarColumn on its own would be bad
75 throw uno::RuntimeException( "Bad column Index" );
76 else // List() ( e.g. no args )
77 {
78 uno::Sequence< uno::Sequence< OUString > > sReturnArray( nLength );
79 auto pReturnArray = sReturnArray.getArray();
80 for ( sal_Int32 i = 0; i < nLength; ++i )
81 {
82 pReturnArray[ i ].realloc( 10 );
83 pReturnArray[ i ].getArray()[ 0 ] = sList[ i ];
84 }
85 aRet <<= sReturnArray;
86 }
87 return aRet;
88}
89
90void
91ListControlHelper::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex )
92{
93 if ( !pvargItem.hasValue() )
94 return;
95
96 uno::Sequence< OUString > sList;
97 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
98
99 sal_Int32 nIndex = sList.getLength();
100
101 if ( pvargIndex.hasValue() )
102 pvargIndex >>= nIndex;
103
104 OUString sString = getAnyAsString( pvargItem );
105
106 // if no index specified or item is to be appended to end of
107 // list just realloc the array and set the last item
108 if ( nIndex == sList.getLength() )
109 {
110 sal_Int32 nOldSize = sList.getLength();
111 sList.realloc( nOldSize + 1 );
112 sList.getArray()[ nOldSize ] = sString;
113 }
114 else
115 {
116 // just copy those elements above the one to be inserted
117 std::vector< OUString > sVec;
118 // reserve just the amount we need to copy
119 sVec.reserve( sList.getLength() - nIndex + 1);
120
121 // insert the new element
122 sVec.push_back( sString );
123
124 // point at first element to copy
125 sVec.insert( sVec.end(), std::next(std::cbegin(sList), nIndex), std::cend(sList) );
126
127 sList.realloc( sList.getLength() + 1 );
128
129 // point at first element to be overwritten
130 std::copy(sVec.begin(), sVec.end(), std::next(sList.getArray(), nIndex));
131 }
132
133 m_xProps->setPropertyValue( "StringItemList", uno::Any( sList ) );
134}
135
136void
138{
139 sal_Int32 nIndex = 0;
140 // for int index
141 if ( !(index >>= nIndex) )
142 return;
143
144 uno::Sequence< OUString > sList;
145 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
146 if( nIndex < 0 || nIndex > ( sList.getLength() - 1 ) )
147 throw uno::RuntimeException( "Invalid index" , uno::Reference< uno::XInterface > () );
148 if( sList.hasElements() )
149 {
150 if( sList.getLength() == 1 )
151 {
152 Clear();
153 return;
154 }
155
157 }
158
159 m_xProps->setPropertyValue( "StringItemList", uno::Any( sList ) );
160}
161
162void
164{
165 // urk, setValue doesn't seem to work !!
166 //setValue( uno::makeAny( sal_Int16() ) );
167 m_xProps->setPropertyValue( "StringItemList", uno::Any( uno::Sequence< OUString >() ) );
168}
169
170void
171ListControlHelper::setRowSource( std::u16string_view _rowsource )
172{
173 if ( _rowsource.empty() )
174 Clear();
175}
176
177sal_Int32
179{
180 uno::Sequence< OUString > sList;
181 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
182 return sList.getLength();
183}
184
186ListControlHelper::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn )
187{
188 return uno::Any( uno::Reference< XPropValue > ( new ScVbaPropValue( new ListPropListener( m_xProps, pvargIndex, pvarColumn ) ) ) );
189}
190
191/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XPropertySet > m_xProps
void removeItem(const css::uno::Any &index)
void setRowSource(std::u16string_view _rowsource)
css::uno::Reference< css::beans::XPropertySet > m_xProps
css::uno::Any List(const css::uno::Any &pvargIndex, const css::uno::Any &pvarColumn)
void AddItem(const css::uno::Any &pvargItem, const css::uno::Any &pvargIndex)
virtual css::uno::Any getValueEvent()=0
virtual void setValueEvent(const css::uno::Any &value)=0
sal_Int32 nIndex
void removeElementAt(css::uno::Sequence< T > &_rSeq, sal_Int32 _nPos)
int i
index
OUString getAnyAsString(const uno::Any &pvargItem)
Definition: vbahelper.cxx:495
bool hasValue()
sal_Int32 nLength