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 class ListPropListener
: public PropListener
30 uno::Reference
< beans::XPropertySet
> m_xProps
;
31 uno::Any m_pvargIndex
;
32 uno::Any m_pvarColumn
;
35 ListPropListener( const uno::Reference
< beans::XPropertySet
>& xProps
, const uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
);
36 virtual ~ListPropListener() { };
37 virtual void setValueEvent( const css::uno::Any
& value
) SAL_OVERRIDE
;
38 virtual css::uno::Any
getValueEvent() SAL_OVERRIDE
;
41 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
)
45 void ListPropListener::setValueEvent( const uno::Any
& value
)
47 if( m_pvargIndex
.hasValue() || m_pvarColumn
.hasValue() )
48 throw uno::RuntimeException( "Bad argument" );
50 m_xProps
->setPropertyValue( "StringItemList", value
);
53 uno::Any
ListPropListener::getValueEvent()
55 uno::Sequence
< OUString
> sList
;
56 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
57 sal_Int16 nLength
= static_cast< sal_Int16
>( sList
.getLength() );
59 if ( m_pvargIndex
.hasValue() )
61 sal_Int16 nIndex
= -1;
62 m_pvargIndex
>>= nIndex
;
63 if( nIndex
< 0 || nIndex
>= nLength
)
64 throw uno::RuntimeException( "Bad row Index" );
65 aRet
<<= sList
[ nIndex
];
67 else if ( m_pvarColumn
.hasValue() ) // pvarColumn on its own would be bad
68 throw uno::RuntimeException( "Bad column Index" );
69 else // List() ( e.g. no args )
71 uno::Sequence
< uno::Sequence
< OUString
> > sReturnArray( nLength
);
72 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
74 sReturnArray
[ i
].realloc( 10 );
75 sReturnArray
[ i
][ 0 ] = sList
[ i
];
77 aRet
= uno::makeAny( sReturnArray
);
83 ListControlHelper::AddItem( const uno::Any
& pvargItem
, const uno::Any
& pvargIndex
) throw (uno::RuntimeException
)
85 if ( pvargItem
.hasValue() )
87 uno::Sequence
< OUString
> sList
;
88 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
90 sal_Int32 nIndex
= sList
.getLength();
92 if ( pvargIndex
.hasValue() )
93 pvargIndex
>>= nIndex
;
95 OUString sString
= getAnyAsString( pvargItem
);
97 // if no index specified or item is to be appended to end of
98 // list just realloc the array and set the last item
99 if ( nIndex
== sList
.getLength() )
101 sal_Int32 nOldSize
= sList
.getLength();
102 sList
.realloc( nOldSize
+ 1 );
103 sList
[ nOldSize
] = sString
;
107 // just copy those elements above the one to be inserted
108 std::vector
< OUString
> sVec
;
109 // reserve just the amount we need to copy
110 sVec
.reserve( sList
.getLength() - nIndex
);
112 // point at first element to copy
113 OUString
* pString
= sList
.getArray() + nIndex
;
114 const OUString
* pEndString
= sList
.getArray() + sList
.getLength();
115 // insert the new element
116 sVec
.push_back( sString
);
118 for ( ; pString
!= pEndString
; ++pString
)
119 sVec
.push_back( *pString
);
121 sList
.realloc( sList
.getLength() + 1 );
123 // point at first element to be overwritten
124 pString
= sList
.getArray() + nIndex
;
125 pEndString
= sList
.getArray() + sList
.getLength();
126 std::vector
< OUString
>::iterator it
= sVec
.begin();
127 for ( ; pString
!= pEndString
; ++pString
, ++it
)
132 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( sList
) );
138 ListControlHelper::removeItem( const uno::Any
& index
) throw (uno::RuntimeException
)
140 sal_Int32 nIndex
= 0;
142 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 )
155 for( sal_Int32 i
= nIndex
; i
< ( sList
.getLength()-1 ); i
++ )
157 sList
[i
] = sList
[i
+1];
159 sList
.realloc( sList
.getLength() - 1 );
162 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( sList
) );
167 ListControlHelper::Clear( ) throw (uno::RuntimeException
)
169 // urk, setValue doesn't seem to work !!
170 //setValue( uno::makeAny( sal_Int16() ) );
171 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( uno::Sequence
< OUString
>() ) );
175 ListControlHelper::setRowSource( const OUString
& _rowsource
) throw (uno::RuntimeException
)
177 if ( _rowsource
.isEmpty() )
182 ListControlHelper::getListCount() throw (uno::RuntimeException
)
184 uno::Sequence
< OUString
> sList
;
185 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
186 return sList
.getLength();
190 ListControlHelper::List( const ::uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
) throw (uno::RuntimeException
)
192 return uno::makeAny( uno::Reference
< XPropValue
> ( new ScVbaPropValue( new ListPropListener( m_xProps
, pvargIndex
, pvarColumn
) ) ) );
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */