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>
25 using namespace com::sun::star
;
26 using namespace ooo::vba
;
28 class ListPropListener
: public PropListener
31 uno::Reference
< beans::XPropertySet
> m_xProps
;
32 uno::Any
const m_pvargIndex
;
33 uno::Any
const m_pvarColumn
;
36 ListPropListener( const uno::Reference
< beans::XPropertySet
>& xProps
, const uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
);
37 virtual ~ListPropListener() { };
38 virtual void setValueEvent( const css::uno::Any
& value
) override
;
39 virtual css::uno::Any
getValueEvent() override
;
42 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
)
46 void ListPropListener::setValueEvent( const uno::Any
& value
)
48 if( m_pvargIndex
.hasValue() || m_pvarColumn
.hasValue() )
49 throw uno::RuntimeException( "Bad argument" );
51 m_xProps
->setPropertyValue( "StringItemList", value
);
54 uno::Any
ListPropListener::getValueEvent()
56 uno::Sequence
< OUString
> sList
;
57 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
58 sal_Int16 nLength
= static_cast< sal_Int16
>( sList
.getLength() );
60 if ( m_pvargIndex
.hasValue() )
62 sal_Int16 nIndex
= -1;
63 m_pvargIndex
>>= nIndex
;
64 if( nIndex
< 0 || nIndex
>= nLength
)
65 throw uno::RuntimeException( "Bad row Index" );
66 aRet
<<= sList
[ nIndex
];
68 else if ( m_pvarColumn
.hasValue() ) // pvarColumn on its own would be bad
69 throw uno::RuntimeException( "Bad column Index" );
70 else // List() ( e.g. no args )
72 uno::Sequence
< uno::Sequence
< OUString
> > sReturnArray( nLength
);
73 for ( sal_Int32 i
= 0; i
< nLength
; ++i
)
75 sReturnArray
[ i
].realloc( 10 );
76 sReturnArray
[ i
][ 0 ] = sList
[ i
];
78 aRet
<<= sReturnArray
;
84 ListControlHelper::AddItem( const uno::Any
& pvargItem
, const uno::Any
& pvargIndex
)
86 if ( pvargItem
.hasValue() )
88 uno::Sequence
< OUString
> sList
;
89 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
91 sal_Int32 nIndex
= sList
.getLength();
93 if ( pvargIndex
.hasValue() )
94 pvargIndex
>>= nIndex
;
96 OUString sString
= getAnyAsString( pvargItem
);
98 // if no index specified or item is to be appended to end of
99 // list just realloc the array and set the last item
100 if ( nIndex
== sList
.getLength() )
102 sal_Int32 nOldSize
= sList
.getLength();
103 sList
.realloc( nOldSize
+ 1 );
104 sList
[ nOldSize
] = sString
;
108 // just copy those elements above the one to be inserted
109 std::vector
< OUString
> sVec
;
110 // reserve just the amount we need to copy
111 sVec
.reserve( sList
.getLength() - nIndex
);
113 // point at first element to copy
114 OUString
* pString
= sList
.getArray() + nIndex
;
115 const OUString
* pEndString
= sList
.getArray() + sList
.getLength();
116 // insert the new element
117 sVec
.push_back( sString
);
119 for ( ; pString
!= pEndString
; ++pString
)
120 sVec
.push_back( *pString
);
122 sList
.realloc( sList
.getLength() + 1 );
124 // point at first element to be overwritten
125 pString
= sList
.getArray() + nIndex
;
126 pEndString
= sList
.getArray() + sList
.getLength();
127 std::vector
< OUString
>::iterator it
= sVec
.begin();
128 for ( ; pString
!= pEndString
; ++pString
, ++it
)
133 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( sList
) );
139 ListControlHelper::removeItem( const uno::Any
& index
)
141 sal_Int32 nIndex
= 0;
143 if ( index
>>= nIndex
)
145 uno::Sequence
< OUString
> sList
;
146 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
147 if( nIndex
< 0 || nIndex
> ( sList
.getLength() - 1 ) )
148 throw uno::RuntimeException( "Invalid index" , uno::Reference
< uno::XInterface
> () );
149 if( sList
.hasElements() )
151 if( sList
.getLength() == 1 )
156 for( sal_Int32 i
= nIndex
; i
< ( sList
.getLength()-1 ); i
++ )
158 sList
[i
] = sList
[i
+1];
160 sList
.realloc( sList
.getLength() - 1 );
163 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( sList
) );
168 ListControlHelper::Clear( )
170 // urk, setValue doesn't seem to work !!
171 //setValue( uno::makeAny( sal_Int16() ) );
172 m_xProps
->setPropertyValue( "StringItemList", uno::makeAny( uno::Sequence
< OUString
>() ) );
176 ListControlHelper::setRowSource( const OUString
& _rowsource
)
178 if ( _rowsource
.isEmpty() )
183 ListControlHelper::getListCount()
185 uno::Sequence
< OUString
> sList
;
186 m_xProps
->getPropertyValue( "StringItemList" ) >>= sList
;
187 return sList
.getLength();
191 ListControlHelper::List( const ::uno::Any
& pvargIndex
, const uno::Any
& pvarColumn
)
193 return uno::makeAny( uno::Reference
< XPropValue
> ( new ScVbaPropValue( new ListPropListener( m_xProps
, pvargIndex
, pvarColumn
) ) ) );
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */