xref: /trunk/main/vbahelper/source/msforms/vbalistcontrolhelper.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*e6ed5fbcSAndrew Rist /**************************************************************
2*e6ed5fbcSAndrew Rist  *
3*e6ed5fbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e6ed5fbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e6ed5fbcSAndrew Rist  * distributed with this work for additional information
6*e6ed5fbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e6ed5fbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e6ed5fbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*e6ed5fbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e6ed5fbcSAndrew Rist  *
11*e6ed5fbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e6ed5fbcSAndrew Rist  *
13*e6ed5fbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e6ed5fbcSAndrew Rist  * software distributed under the License is distributed on an
15*e6ed5fbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e6ed5fbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*e6ed5fbcSAndrew Rist  * specific language governing permissions and limitations
18*e6ed5fbcSAndrew Rist  * under the License.
19*e6ed5fbcSAndrew Rist  *
20*e6ed5fbcSAndrew Rist  *************************************************************/
21*e6ed5fbcSAndrew Rist 
22cdf0e10cSrcweir #include <vbalistcontrolhelper.hxx>
23cdf0e10cSrcweir #include <vector>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir using namespace com::sun::star;
26cdf0e10cSrcweir using namespace ooo::vba;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir const static rtl::OUString ITEMS( RTL_CONSTASCII_USTRINGPARAM("StringItemList") );
29cdf0e10cSrcweir 
30cdf0e10cSrcweir void SAL_CALL
AddItem(const uno::Any & pvargItem,const uno::Any & pvargIndex)31cdf0e10cSrcweir ListControlHelper::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException)
32cdf0e10cSrcweir {
33cdf0e10cSrcweir     if ( pvargItem.hasValue()  )
34cdf0e10cSrcweir     {
35cdf0e10cSrcweir         uno::Sequence< rtl::OUString > sList;
36cdf0e10cSrcweir         m_xProps->getPropertyValue( ITEMS ) >>= sList;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir         sal_Int32 nIndex = sList.getLength();
39cdf0e10cSrcweir 
40cdf0e10cSrcweir         if ( pvargIndex.hasValue() )
41cdf0e10cSrcweir             pvargIndex >>= nIndex;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir         rtl::OUString sString = getAnyAsString( pvargItem );
44cdf0e10cSrcweir 
45cdf0e10cSrcweir         // if no index specified or item is to be appended to end of
46cdf0e10cSrcweir         // list just realloc the array and set the last item
47cdf0e10cSrcweir         if ( nIndex  == sList.getLength() )
48cdf0e10cSrcweir         {
49cdf0e10cSrcweir             sal_Int32 nOldSize = sList.getLength();
50cdf0e10cSrcweir             sList.realloc( nOldSize + 1 );
51cdf0e10cSrcweir             sList[ nOldSize ] = sString;
52cdf0e10cSrcweir         }
53cdf0e10cSrcweir         else
54cdf0e10cSrcweir         {
55cdf0e10cSrcweir             // just copy those elements above the one to be inserted
56cdf0e10cSrcweir             std::vector< rtl::OUString > sVec;
57cdf0e10cSrcweir             // reserve just the amount we need to copy
58cdf0e10cSrcweir             sVec.reserve( sList.getLength() - nIndex );
59cdf0e10cSrcweir 
60cdf0e10cSrcweir             // point at first element to copy
61cdf0e10cSrcweir             rtl::OUString* pString = sList.getArray() + nIndex;
62cdf0e10cSrcweir             const rtl::OUString* pEndString = sList.getArray() + sList.getLength();
63cdf0e10cSrcweir             // insert the new element
64cdf0e10cSrcweir             sVec.push_back( sString );
65cdf0e10cSrcweir             // copy elements
66cdf0e10cSrcweir             for ( ; pString != pEndString; ++pString )
67cdf0e10cSrcweir                 sVec.push_back( *pString );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir             sList.realloc(  sList.getLength() + 1 );
70cdf0e10cSrcweir 
71cdf0e10cSrcweir             // point at first element to be overwritten
72cdf0e10cSrcweir             pString = sList.getArray() + nIndex;
73cdf0e10cSrcweir             pEndString = sList.getArray() + sList.getLength();
74cdf0e10cSrcweir             std::vector< rtl::OUString >::iterator it = sVec.begin();
75cdf0e10cSrcweir             for ( ; pString != pEndString; ++pString, ++it)
76cdf0e10cSrcweir                 *pString = *it;
77cdf0e10cSrcweir             //
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         m_xProps->setPropertyValue( ITEMS, uno::makeAny( sList ) );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir void SAL_CALL
removeItem(const uno::Any & index)86cdf0e10cSrcweir ListControlHelper::removeItem( const uno::Any& index ) throw (uno::RuntimeException)
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     sal_Int32 nIndex = 0;
89cdf0e10cSrcweir     // for int index
90cdf0e10cSrcweir     if ( index >>= nIndex  )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         uno::Sequence< rtl::OUString > sList;
93cdf0e10cSrcweir         m_xProps->getPropertyValue( ITEMS ) >>= sList;
94cdf0e10cSrcweir         if( nIndex < 0 || nIndex > ( sList.getLength() - 1 ) )
95cdf0e10cSrcweir             throw uno::RuntimeException( rtl::OUString::createFromAscii( "Invalid index" ), uno::Reference< uno::XInterface > () );
96cdf0e10cSrcweir         if( sList.hasElements() )
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             if( sList.getLength() == 1 )
99cdf0e10cSrcweir             {
100cdf0e10cSrcweir                 Clear();
101cdf0e10cSrcweir                 return;
102cdf0e10cSrcweir             }
103cdf0e10cSrcweir             for( sal_Int32 i = nIndex; i < ( sList.getLength()-1 ); i++ )
104cdf0e10cSrcweir             {
105cdf0e10cSrcweir                 sList[i] = sList[i+1];
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir             sList.realloc(  sList.getLength() - 1 );
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         m_xProps->setPropertyValue( ITEMS, uno::makeAny( sList ) );
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir void SAL_CALL
Clear()115cdf0e10cSrcweir ListControlHelper::Clear(  ) throw (uno::RuntimeException)
116cdf0e10cSrcweir {
117cdf0e10cSrcweir     // urk, setValue doesn't seem to work !!
118cdf0e10cSrcweir     //setValue( uno::makeAny( sal_Int16() ) );
119cdf0e10cSrcweir     m_xProps->setPropertyValue( ITEMS, uno::makeAny( uno::Sequence< rtl::OUString >() ) );
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir void SAL_CALL
setRowSource(const rtl::OUString & _rowsource)123cdf0e10cSrcweir ListControlHelper::setRowSource( const rtl::OUString& _rowsource ) throw (uno::RuntimeException)
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     if ( _rowsource.getLength() == 0 )
126cdf0e10cSrcweir         Clear();
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir sal_Int32 SAL_CALL
getListCount()130cdf0e10cSrcweir ListControlHelper::getListCount() throw (uno::RuntimeException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     uno::Sequence< rtl::OUString > sList;
133cdf0e10cSrcweir     m_xProps->getPropertyValue( ITEMS ) >>= sList;
134cdf0e10cSrcweir     return sList.getLength();
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir uno::Any SAL_CALL
List(const::uno::Any & pvargIndex,const uno::Any & pvarColumn)138cdf0e10cSrcweir ListControlHelper::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException)
139cdf0e10cSrcweir {
140cdf0e10cSrcweir     uno::Sequence< rtl::OUString > sList;
141cdf0e10cSrcweir     m_xProps->getPropertyValue( ITEMS ) >>= sList;
142cdf0e10cSrcweir     sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
143cdf0e10cSrcweir     uno::Any aRet;
144cdf0e10cSrcweir     if ( pvargIndex.hasValue() )
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         sal_Int16 nIndex = -1;
147cdf0e10cSrcweir         pvargIndex >>= nIndex;
148cdf0e10cSrcweir         if( nIndex < 0 || nIndex >= nLength )
149cdf0e10cSrcweir             throw uno::RuntimeException( rtl::OUString::createFromAscii(
150cdf0e10cSrcweir                     "Bad row Index" ), uno::Reference< uno::XInterface >() );
151cdf0e10cSrcweir         aRet <<= sList[ nIndex ];
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir     else if ( pvarColumn.hasValue() ) // pvarColumn on its own would be bad
154cdf0e10cSrcweir             throw uno::RuntimeException( rtl::OUString::createFromAscii(
155cdf0e10cSrcweir                     "Bad column Index" ), uno::Reference< uno::XInterface >() );
156cdf0e10cSrcweir     else // List() ( e.g. no args )
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         uno::Sequence< uno::Sequence< rtl::OUString > > sReturnArray( nLength );
159cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < nLength; ++i )
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             sReturnArray[ i ].realloc( 10 );
162cdf0e10cSrcweir             sReturnArray[ i ][ 0 ] = sList[ i ];
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir         aRet = uno::makeAny( sReturnArray );
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir     return aRet;
167cdf0e10cSrcweir }
168