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>
24 using namespace com::sun::star
;
25 using namespace ooo::vba
;
27 const static OUString
ITEMS( "StringItemList" );
29 class ListPropListener
: public PropListener
32 uno::Reference
< beans::XPropertySet
> m_xProps
;
33 uno::Any m_pvargIndex
;
34 uno::Any m_pvarColumn
;
37 ListPropListener( const uno::Reference
< beans::XPropertySet
>& xProps
, const uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
);
38 virtual ~ListPropListener() { };
39 virtual void setValueEvent( const css::uno::Any
& value
);
40 virtual css::uno::Any
getValueEvent();
43 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
)
47 void ListPropListener::setValueEvent( const uno::Any
& value
)
49 if( m_pvargIndex
.hasValue() || m_pvarColumn
.hasValue() )
50 throw uno::RuntimeException( "Bad argument" , uno::Reference
< uno::XInterface
>() );
52 m_xProps
->setPropertyValue( ITEMS
, value
);
55 uno::Any
ListPropListener::getValueEvent()
57 uno::Sequence
< OUString
> sList
;
58 m_xProps
->getPropertyValue( ITEMS
) >>= sList
;
59 sal_Int16 nLength
= static_cast< sal_Int16
>( sList
.getLength() );
61 if ( m_pvargIndex
.hasValue() )
63 sal_Int16 nIndex
= -1;
64 m_pvargIndex
>>= nIndex
;
65 if( nIndex
< 0 || nIndex
>= nLength
)
66 throw uno::RuntimeException( "Bad row Index" , uno::Reference
< uno::XInterface
>() );
67 aRet
<<= sList
[ nIndex
];
69 else if ( m_pvarColumn
.hasValue() ) // pvarColumn on its own would be bad
70 throw uno::RuntimeException( "Bad column Index" , uno::Reference
< uno::XInterface
>() );
71 else // List() ( e.g. no args )
73 uno::Sequence
< uno::Sequence
< OUString
> > sReturnArray( nLength
);
74 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
76 sReturnArray
[ i
].realloc( 10 );
77 sReturnArray
[ i
][ 0 ] = sList
[ i
];
79 aRet
= uno::makeAny( sReturnArray
);
85 ListControlHelper::AddItem( const uno::Any
& pvargItem
, const uno::Any
& pvargIndex
) throw (uno::RuntimeException
)
87 if ( pvargItem
.hasValue() )
89 uno::Sequence
< OUString
> sList
;
90 m_xProps
->getPropertyValue( ITEMS
) >>= sList
;
92 sal_Int32 nIndex
= sList
.getLength();
94 if ( pvargIndex
.hasValue() )
95 pvargIndex
>>= nIndex
;
97 OUString sString
= getAnyAsString( pvargItem
);
99 // if no index specified or item is to be appended to end of
100 // list just realloc the array and set the last item
101 if ( nIndex
== sList
.getLength() )
103 sal_Int32 nOldSize
= sList
.getLength();
104 sList
.realloc( nOldSize
+ 1 );
105 sList
[ nOldSize
] = sString
;
109 // just copy those elements above the one to be inserted
110 std::vector
< OUString
> sVec
;
111 // reserve just the amount we need to copy
112 sVec
.reserve( sList
.getLength() - nIndex
);
114 // point at first element to copy
115 OUString
* pString
= sList
.getArray() + nIndex
;
116 const OUString
* pEndString
= sList
.getArray() + sList
.getLength();
117 // insert the new element
118 sVec
.push_back( sString
);
120 for ( ; pString
!= pEndString
; ++pString
)
121 sVec
.push_back( *pString
);
123 sList
.realloc( sList
.getLength() + 1 );
125 // point at first element to be overwritten
126 pString
= sList
.getArray() + nIndex
;
127 pEndString
= sList
.getArray() + sList
.getLength();
128 std::vector
< OUString
>::iterator it
= sVec
.begin();
129 for ( ; pString
!= pEndString
; ++pString
, ++it
)
134 m_xProps
->setPropertyValue( ITEMS
, uno::makeAny( sList
) );
140 ListControlHelper::removeItem( const uno::Any
& index
) throw (uno::RuntimeException
)
142 sal_Int32 nIndex
= 0;
144 if ( index
>>= nIndex
)
146 uno::Sequence
< OUString
> sList
;
147 m_xProps
->getPropertyValue( ITEMS
) >>= sList
;
148 if( nIndex
< 0 || nIndex
> ( sList
.getLength() - 1 ) )
149 throw uno::RuntimeException( "Invalid index" , uno::Reference
< uno::XInterface
> () );
150 if( sList
.hasElements() )
152 if( sList
.getLength() == 1 )
157 for( sal_Int32 i
= nIndex
; i
< ( sList
.getLength()-1 ); i
++ )
159 sList
[i
] = sList
[i
+1];
161 sList
.realloc( sList
.getLength() - 1 );
164 m_xProps
->setPropertyValue( ITEMS
, uno::makeAny( sList
) );
169 ListControlHelper::Clear( ) throw (uno::RuntimeException
)
171 // urk, setValue doesn't seem to work !!
172 //setValue( uno::makeAny( sal_Int16() ) );
173 m_xProps
->setPropertyValue( ITEMS
, uno::makeAny( uno::Sequence
< OUString
>() ) );
177 ListControlHelper::setRowSource( const OUString
& _rowsource
) throw (uno::RuntimeException
)
179 if ( _rowsource
.isEmpty() )
184 ListControlHelper::getListCount() throw (uno::RuntimeException
)
186 uno::Sequence
< OUString
> sList
;
187 m_xProps
->getPropertyValue( ITEMS
) >>= sList
;
188 return sList
.getLength();
192 ListControlHelper::List( const ::uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
) throw (uno::RuntimeException
)
194 return uno::makeAny( uno::Reference
< XPropValue
> ( new ScVbaPropValue( new ListPropListener( m_xProps
, pvargIndex
, pvarColumn
) ) ) );
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */