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"
22 #include <vbahelper/vbapropvalue.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <comphelper/sequence.hxx>
26 using namespace com::sun::star
;
27 using namespace ooo::vba
;
31 class ListPropListener
: public PropListener
34 uno::Reference
< beans::XPropertySet
> m_xProps
;
35 uno::Any m_pvargIndex
;
36 uno::Any m_pvarColumn
;
39 ListPropListener( const uno::Reference
< beans::XPropertySet
>& xProps
, const uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
);
40 virtual ~ListPropListener() { };
41 virtual void setValueEvent( const css::uno::Any
& value
) override
;
42 virtual css::uno::Any
getValueEvent() override
;
47 ListPropListener::ListPropListener( const uno::Reference
< beans::XPropertySet
>& xProps
, const uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
) : m_xProps( xProps
), m_pvargIndex( pvargIndex
), m_pvarColumn( pvarColumn
)
51 void ListPropListener::setValueEvent( const uno::Any
& value
)
53 if( m_pvargIndex
.hasValue() || m_pvarColumn
.hasValue() )
54 throw uno::RuntimeException( "Bad argument" );
56 m_xProps
->setPropertyValue( "StringItemList", value
);
59 uno::Any
ListPropListener::getValueEvent()
61 uno::Sequence
< OUString
> sList
;
62 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
63 sal_Int16 nLength
= static_cast< sal_Int16
>( sList
.getLength() );
65 if ( m_pvargIndex
.hasValue() )
67 sal_Int16 nIndex
= -1;
68 m_pvargIndex
>>= nIndex
;
69 if( nIndex
< 0 || nIndex
>= nLength
)
70 throw uno::RuntimeException( "Bad row Index" );
71 aRet
<<= sList
[ nIndex
];
73 else if ( m_pvarColumn
.hasValue() ) // pvarColumn on its own would be bad
74 throw uno::RuntimeException( "Bad column Index" );
75 else // List() ( e.g. no args )
77 uno::Sequence
< uno::Sequence
< OUString
> > sReturnArray( nLength
);
78 auto pReturnArray
= sReturnArray
.getArray();
79 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
81 pReturnArray
[ i
].realloc( 10 );
82 pReturnArray
[ i
].getArray()[ 0 ] = sList
[ i
];
84 aRet
<<= sReturnArray
;
90 ListControlHelper::AddItem( const uno::Any
& pvargItem
, const uno::Any
& pvargIndex
)
92 if ( !pvargItem
.hasValue() )
95 uno::Sequence
< OUString
> sList
;
96 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
98 sal_Int32 nIndex
= sList
.getLength();
100 if ( pvargIndex
.hasValue() )
101 pvargIndex
>>= nIndex
;
103 OUString sString
= getAnyAsString( pvargItem
);
105 // if no index specified or item is to be appended to end of
106 // list just realloc the array and set the last item
107 if ( nIndex
== sList
.getLength() )
109 sal_Int32 nOldSize
= sList
.getLength();
110 sList
.realloc( nOldSize
+ 1 );
111 sList
.getArray()[ nOldSize
] = sString
;
115 // just copy those elements above the one to be inserted
116 std::vector
< OUString
> sVec
;
117 // reserve just the amount we need to copy
118 sVec
.reserve( sList
.getLength() - nIndex
+ 1);
120 // insert the new element
121 sVec
.push_back( sString
);
123 // point at first element to copy
124 sVec
.insert( sVec
.end(), std::next(std::cbegin(sList
), nIndex
), std::cend(sList
) );
126 sList
.realloc( sList
.getLength() + 1 );
128 // point at first element to be overwritten
129 std::copy(sVec
.begin(), sVec
.end(), std::next(sList
.getArray(), nIndex
));
132 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( sList
) );
136 ListControlHelper::removeItem( const uno::Any
& index
)
138 sal_Int32 nIndex
= 0;
140 if ( !(index
>>= nIndex
) )
143 uno::Sequence
< OUString
> sList
;
144 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
145 if( nIndex
< 0 || nIndex
> ( sList
.getLength() - 1 ) )
146 throw uno::RuntimeException( "Invalid index" , uno::Reference
< uno::XInterface
> () );
147 if( sList
.hasElements() )
149 if( sList
.getLength() == 1 )
155 comphelper::removeElementAt(sList
, nIndex
);
158 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( sList
) );
162 ListControlHelper::Clear( )
164 // urk, setValue doesn't seem to work !!
165 //setValue( uno::makeAny( sal_Int16() ) );
166 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( uno::Sequence
< OUString
>() ) );
170 ListControlHelper::setRowSource( std::u16string_view _rowsource
)
172 if ( _rowsource
.empty() )
177 ListControlHelper::getListCount()
179 uno::Sequence
< OUString
> sList
;
180 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
181 return sList
.getLength();
185 ListControlHelper::List( const ::uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
)
187 return uno::makeAny( uno::Reference
< XPropValue
> ( new ScVbaPropValue( new ListPropListener( m_xProps
, pvargIndex
, pvarColumn
) ) ) );
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */