xref: /AOO41X/main/vbahelper/source/msforms/vbalistbox.cxx (revision e6ed5fbc51cf474df369618b58e945b84a21a167)
1*e6ed5fbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
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
10cdf0e10cSrcweir  *
11*e6ed5fbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
20*e6ed5fbcSAndrew Rist  *************************************************************/
21*e6ed5fbcSAndrew Rist 
22*e6ed5fbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "vbalistbox.hxx"
25cdf0e10cSrcweir #include "vbanewfont.hxx"
26cdf0e10cSrcweir #include <comphelper/anytostring.hxx>
27cdf0e10cSrcweir #include <com/sun/star/script/ArrayWrapper.hpp>
28cdf0e10cSrcweir #include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir using namespace com::sun::star;
31cdf0e10cSrcweir using namespace ooo::vba;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir const static rtl::OUString TEXT( RTL_CONSTASCII_USTRINGPARAM("Text") );
34cdf0e10cSrcweir const static rtl::OUString SELECTEDITEMS( RTL_CONSTASCII_USTRINGPARAM("SelectedItems") );
35cdf0e10cSrcweir const static rtl::OUString ITEMS( RTL_CONSTASCII_USTRINGPARAM("StringItemList") );
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
ScVbaListBox(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<css::uno::XInterface> & xControl,const uno::Reference<frame::XModel> & xModel,AbstractGeometryAttributes * pGeomHelper)38cdf0e10cSrcweir ScVbaListBox::ScVbaListBox( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< css::uno::XInterface >& xControl, const uno::Reference< frame::XModel >& xModel, AbstractGeometryAttributes* pGeomHelper ) : ListBoxImpl_BASE( xParent, xContext, xControl, xModel, pGeomHelper )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     mpListHelper.reset( new ListControlHelper( m_xProps ) );
41cdf0e10cSrcweir }
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // Attributes
44cdf0e10cSrcweir void SAL_CALL
setListIndex(const uno::Any & _value)45cdf0e10cSrcweir ScVbaListBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException)
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     sal_Int32 nIndex = 0;
48cdf0e10cSrcweir     _value >>= nIndex;
49cdf0e10cSrcweir     uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
50cdf0e10cSrcweir     xPropVal->setValue( uno::makeAny( sal_True ) );
51cdf0e10cSrcweir }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir uno::Any SAL_CALL
getListIndex()54cdf0e10cSrcweir ScVbaListBox::getListIndex() throw (uno::RuntimeException)
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     uno::Sequence< sal_Int16 > sSelection;
57cdf0e10cSrcweir     m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
58cdf0e10cSrcweir     if ( sSelection.getLength() == 0 )
59cdf0e10cSrcweir         return uno::Any( sal_Int32( -1 ) );
60cdf0e10cSrcweir     return uno::Any( sSelection[ 0 ] );
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir uno::Any SAL_CALL
getValue()64cdf0e10cSrcweir ScVbaListBox::getValue() throw (uno::RuntimeException)
65cdf0e10cSrcweir {
66cdf0e10cSrcweir     uno::Sequence< sal_Int16 > sSelection;
67cdf0e10cSrcweir     uno::Sequence< rtl::OUString > sItems;
68cdf0e10cSrcweir     m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
69cdf0e10cSrcweir     m_xProps->getPropertyValue( ITEMS ) >>= sItems;
70cdf0e10cSrcweir     if( getMultiSelect() )
71cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii(
72cdf0e10cSrcweir                     "Attribute use invalid." ), uno::Reference< uno::XInterface >() );
73cdf0e10cSrcweir     uno::Any aRet;
74cdf0e10cSrcweir     if ( sSelection.getLength() )
75cdf0e10cSrcweir         aRet = uno::makeAny( sItems[ sSelection[ 0 ] ] );
76cdf0e10cSrcweir     return aRet;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir void SAL_CALL
setValue(const uno::Any & _value)80cdf0e10cSrcweir ScVbaListBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     if( getMultiSelect() )
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii(
85cdf0e10cSrcweir                     "Attribute use invalid." ), uno::Reference< uno::XInterface >() );
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir     rtl::OUString sValue = getAnyAsString( _value );
88cdf0e10cSrcweir     uno::Sequence< rtl::OUString > sList;
89cdf0e10cSrcweir     m_xProps->getPropertyValue( ITEMS ) >>= sList;
90cdf0e10cSrcweir     uno::Sequence< sal_Int16 > nList;
91cdf0e10cSrcweir     sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
92cdf0e10cSrcweir     sal_Int16 nValue = -1;
93cdf0e10cSrcweir     sal_Int16 i = 0;
94cdf0e10cSrcweir     for( i = 0; i < nLength; i++ )
95cdf0e10cSrcweir     {
96cdf0e10cSrcweir         if( sList[i].equals( sValue ) )
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             nValue = i;
99cdf0e10cSrcweir             break;
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir     if( nValue == -1 )
103cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii(
104cdf0e10cSrcweir                     "Attribute use invalid." ), uno::Reference< uno::XInterface >() );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     uno::Sequence< sal_Int16 > nSelectedIndices(1);
107cdf0e10cSrcweir     nSelectedIndices[ 0 ] = nValue;
108cdf0e10cSrcweir     m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nSelectedIndices ) );
109cdf0e10cSrcweir     m_xProps->setPropertyValue( TEXT, uno::makeAny( sValue ) );
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getText()113cdf0e10cSrcweir ScVbaListBox::getText() throw (uno::RuntimeException)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	rtl::OUString result;
116cdf0e10cSrcweir 	getValue() >>= result;
117cdf0e10cSrcweir 	return result;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir void SAL_CALL
setText(const::rtl::OUString & _text)121cdf0e10cSrcweir ScVbaListBox::setText( const ::rtl::OUString& _text ) throw (uno::RuntimeException)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	setValue( uno::makeAny( _text ) ); // seems the same
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir sal_Bool SAL_CALL
getMultiSelect()127cdf0e10cSrcweir ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     sal_Bool bMultiSelect = sal_False;
130cdf0e10cSrcweir     m_xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MultiSelection" ) ) ) >>= bMultiSelect;
131cdf0e10cSrcweir     return bMultiSelect;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir void SAL_CALL
setMultiSelect(sal_Bool _multiselect)135cdf0e10cSrcweir ScVbaListBox::setMultiSelect( sal_Bool _multiselect ) throw (css::uno::RuntimeException)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     m_xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MultiSelection" ) ), uno::makeAny( _multiselect ) );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir css::uno::Any SAL_CALL
Selected(sal_Int32 index)141cdf0e10cSrcweir ScVbaListBox::Selected( sal_Int32 index ) throw (css::uno::RuntimeException)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     uno::Sequence< rtl::OUString > sList;
144cdf0e10cSrcweir     m_xProps->getPropertyValue( ITEMS ) >>= sList;
145cdf0e10cSrcweir     sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
146cdf0e10cSrcweir     // no choice but to do a horror cast as internally
147cdf0e10cSrcweir     // the indices are but sal_Int16
148cdf0e10cSrcweir     sal_Int16 nIndex = static_cast< sal_Int16 >( index );
149cdf0e10cSrcweir     if( nIndex < 0 || nIndex >= nLength )
150cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii(
151cdf0e10cSrcweir                     "Error Number." ), uno::Reference< uno::XInterface >() );
152cdf0e10cSrcweir     m_nIndex = nIndex;
153cdf0e10cSrcweir     return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir // Methods
157cdf0e10cSrcweir void SAL_CALL
AddItem(const uno::Any & pvargItem,const uno::Any & pvargIndex)158cdf0e10cSrcweir ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	mpListHelper->AddItem( pvargItem, pvargIndex );
161cdf0e10cSrcweir 		}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir void SAL_CALL
removeItem(const uno::Any & index)164cdf0e10cSrcweir ScVbaListBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException)
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	mpListHelper->removeItem( index );
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir void SAL_CALL
Clear()170cdf0e10cSrcweir ScVbaListBox::Clear(  ) throw (uno::RuntimeException)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir 	mpListHelper->Clear();
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir // this is called when something like the following vba code is used
176cdf0e10cSrcweir // to set the selected state of particular entries in the Listbox
177cdf0e10cSrcweir // ListBox1.Selected( 3 ) = false
178cdf0e10cSrcweir //PropListener
179cdf0e10cSrcweir void
setValueEvent(const uno::Any & value)180cdf0e10cSrcweir ScVbaListBox::setValueEvent( const uno::Any& value )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir     sal_Bool bValue = sal_False;
183cdf0e10cSrcweir     if( !(value >>= bValue) )
184cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString::createFromAscii(
185cdf0e10cSrcweir                     "Invalid type\n. need boolean." ), uno::Reference< uno::XInterface >() );
186cdf0e10cSrcweir     uno::Sequence< sal_Int16 > nList;
187cdf0e10cSrcweir     m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nList;
188cdf0e10cSrcweir     sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
189cdf0e10cSrcweir     sal_Int16 nIndex = m_nIndex;
190cdf0e10cSrcweir     for( sal_Int16 i = 0; i < nLength; i++ )
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         if( nList[i] == nIndex )
193cdf0e10cSrcweir         {
194cdf0e10cSrcweir             if( bValue )
195cdf0e10cSrcweir                 return;
196cdf0e10cSrcweir             else
197cdf0e10cSrcweir             {
198cdf0e10cSrcweir                 for( ; i < nLength - 1; i++ )
199cdf0e10cSrcweir                 {
200cdf0e10cSrcweir                     nList[i] = nList[i + 1];
201cdf0e10cSrcweir                 }
202cdf0e10cSrcweir                 nList.realloc( nLength - 1 );
203cdf0e10cSrcweir                 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
204cdf0e10cSrcweir 		m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
205cdf0e10cSrcweir                 return;
206cdf0e10cSrcweir             }
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir     if( bValue )
210cdf0e10cSrcweir     {
211cdf0e10cSrcweir         if( getMultiSelect() )
212cdf0e10cSrcweir         {
213cdf0e10cSrcweir             nList.realloc( nLength + 1 );
214cdf0e10cSrcweir             nList[nLength] = nIndex;
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir         else
217cdf0e10cSrcweir         {
218cdf0e10cSrcweir             nList.realloc( 1 );
219cdf0e10cSrcweir             nList[0] = nIndex;
220cdf0e10cSrcweir         }
221cdf0e10cSrcweir         //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
222cdf0e10cSrcweir         m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir // this is called when something like the following vba code is used
227cdf0e10cSrcweir // to determine the selected state of particular entries in the Listbox
228cdf0e10cSrcweir // msgbox ListBox1.Selected( 3 )
229cdf0e10cSrcweir 
230cdf0e10cSrcweir css::uno::Any
getValueEvent()231cdf0e10cSrcweir ScVbaListBox::getValueEvent()
232cdf0e10cSrcweir {
233cdf0e10cSrcweir     uno::Sequence< sal_Int16 > nList;
234cdf0e10cSrcweir     m_xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SelectedItems" ) ) ) >>= nList;
235cdf0e10cSrcweir     sal_Int32 nLength = nList.getLength();
236cdf0e10cSrcweir     sal_Int32 nIndex = m_nIndex;
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     for( sal_Int32 i = 0; i < nLength; i++ )
239cdf0e10cSrcweir     {
240cdf0e10cSrcweir         if( nList[i] == nIndex )
241cdf0e10cSrcweir             return uno::makeAny( sal_True );
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     return uno::makeAny( sal_False );
245cdf0e10cSrcweir }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir void SAL_CALL
setRowSource(const rtl::OUString & _rowsource)248cdf0e10cSrcweir ScVbaListBox::setRowSource( const rtl::OUString& _rowsource ) throw (uno::RuntimeException)
249cdf0e10cSrcweir {
250cdf0e10cSrcweir 	ScVbaControl::setRowSource( _rowsource );
251cdf0e10cSrcweir 	mpListHelper->setRowSource( _rowsource );
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir sal_Int32 SAL_CALL
getListCount()255cdf0e10cSrcweir ScVbaListBox::getListCount() throw (uno::RuntimeException)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir 	return mpListHelper->getListCount();
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir uno::Any SAL_CALL
List(const::uno::Any & pvargIndex,const uno::Any & pvarColumn)261cdf0e10cSrcweir ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException)
262cdf0e10cSrcweir {
263cdf0e10cSrcweir 	return mpListHelper->List( pvargIndex, pvarColumn );
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
getFont()266cdf0e10cSrcweir uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont() throw (uno::RuntimeException)
267cdf0e10cSrcweir {
268cdf0e10cSrcweir     return new VbaNewFont( this, mxContext, m_xProps );
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir rtl::OUString&
getServiceImplName()272cdf0e10cSrcweir ScVbaListBox::getServiceImplName()
273cdf0e10cSrcweir {
274cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaListBox") );
275cdf0e10cSrcweir 	return sImplName;
276cdf0e10cSrcweir }
277cdf0e10cSrcweir 
278cdf0e10cSrcweir uno::Sequence< rtl::OUString >
getServiceNames()279cdf0e10cSrcweir ScVbaListBox::getServiceNames()
280cdf0e10cSrcweir {
281cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
282cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
283cdf0e10cSrcweir 	{
284cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
285cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.msforms.ScVbaListBox" ) );
286cdf0e10cSrcweir 	}
287cdf0e10cSrcweir 	return aServiceNames;
288cdf0e10cSrcweir }
289