1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "vbalistcontrolhelper.hxx"
23 #include <vbahelper/vbapropvalue.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <comphelper/sequence.hxx>
27 using namespace com::sun::star
;
28 using namespace ooo::vba
;
32 class ListPropListener
: public PropListener
35 uno::Reference
< beans::XPropertySet
> m_xProps
;
36 uno::Any m_pvargIndex
;
37 uno::Any m_pvarColumn
;
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
;
48 ListPropListener::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
))
52 void ListPropListener::setValueEvent( const uno::Any
& value
)
54 if( m_pvargIndex
.hasValue() || m_pvarColumn
.hasValue() )
55 throw uno::RuntimeException( "Bad argument" );
57 m_xProps
->setPropertyValue( "StringItemList", value
);
60 uno::Any
ListPropListener::getValueEvent()
62 uno::Sequence
< OUString
> sList
;
63 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
64 sal_Int16 nLength
= static_cast< sal_Int16
>( sList
.getLength() );
66 if ( m_pvargIndex
.hasValue() )
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
];
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 )
78 uno::Sequence
< uno::Sequence
< OUString
> > sReturnArray( nLength
);
79 auto pReturnArray
= sReturnArray
.getArray();
80 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
82 pReturnArray
[ i
].realloc( 10 );
83 pReturnArray
[ i
].getArray()[ 0 ] = sList
[ i
];
85 aRet
<<= sReturnArray
;
91 ListControlHelper::AddItem( const uno::Any
& pvargItem
, const uno::Any
& pvargIndex
)
93 if ( !pvargItem
.hasValue() )
96 uno::Sequence
< OUString
> sList
;
97 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
99 sal_Int32 nIndex
= sList
.getLength();
101 if ( pvargIndex
.hasValue() )
102 pvargIndex
>>= nIndex
;
104 OUString sString
= getAnyAsString( pvargItem
);
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() )
110 sal_Int32 nOldSize
= sList
.getLength();
111 sList
.realloc( nOldSize
+ 1 );
112 sList
.getArray()[ nOldSize
] = sString
;
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);
121 // insert the new element
122 sVec
.push_back( sString
);
124 // point at first element to copy
125 sVec
.insert( sVec
.end(), std::next(std::cbegin(sList
), nIndex
), std::cend(sList
) );
127 sList
.realloc( sList
.getLength() + 1 );
129 // point at first element to be overwritten
130 std::copy(sVec
.begin(), sVec
.end(), std::next(sList
.getArray(), nIndex
));
133 m_xProps
->setPropertyValue( "StringItemList", uno::Any( sList
) );
137 ListControlHelper::removeItem( const uno::Any
& index
)
139 sal_Int32 nIndex
= 0;
141 if ( !(index
>>= nIndex
) )
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() )
150 if( sList
.getLength() == 1 )
156 comphelper::removeElementAt(sList
, nIndex
);
159 m_xProps
->setPropertyValue( "StringItemList", uno::Any( sList
) );
163 ListControlHelper::Clear( )
165 // urk, setValue doesn't seem to work !!
166 //setValue( uno::makeAny( sal_Int16() ) );
167 m_xProps
->setPropertyValue( "StringItemList", uno::Any( uno::Sequence
< OUString
>() ) );
171 ListControlHelper::setRowSource( std::u16string_view _rowsource
)
173 if ( _rowsource
.empty() )
178 ListControlHelper::getListCount()
180 uno::Sequence
< OUString
> sList
;
181 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
182 return sList
.getLength();
186 ListControlHelper::List( const ::uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
)
188 return uno::Any( uno::Reference
< XPropValue
> ( new ScVbaPropValue( new ListPropListener( m_xProps
, pvargIndex
, pvarColumn
) ) ) );
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */