update dev300-m58
[ooovba.git] / vbahelper / source / msforms / vbalistbox.cxx
blob175328f107f74465df780a6b506f22fb978c55a6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbalistbox.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
32 #include "vbalistbox.hxx"
33 #include <vector>
34 #include <comphelper/anytostring.hxx>
35 #include <com/sun/star/script/ArrayWrapper.hpp>
37 using namespace com::sun::star;
38 using namespace ooo::vba;
40 const static rtl::OUString TEXT( RTL_CONSTASCII_USTRINGPARAM("Text") );
41 const static rtl::OUString SELECTEDITEMS( RTL_CONSTASCII_USTRINGPARAM("SelectedItems") );
42 const static rtl::OUString ITEMS( RTL_CONSTASCII_USTRINGPARAM("StringItemList") );
45 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 )
47 mpListHelper.reset( new ListControlHelper( m_xProps ) );
50 // Attributes
51 void SAL_CALL
52 ScVbaListBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException)
54 sal_Int32 nIndex = 0;
55 _value >>= nIndex;
56 uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
57 xPropVal->setValue( uno::makeAny( sal_True ) );
60 uno::Any SAL_CALL
61 ScVbaListBox::getListIndex() throw (uno::RuntimeException)
63 uno::Sequence< sal_Int16 > sSelection;
64 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
65 if ( sSelection.getLength() == 0 )
66 return uno::Any( sal_Int32( -1 ) );
67 return uno::Any( sSelection[ 0 ] );
70 uno::Any SAL_CALL
71 ScVbaListBox::getValue() throw (uno::RuntimeException)
73 uno::Sequence< sal_Int16 > sSelection;
74 uno::Sequence< rtl::OUString > sItems;
75 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
76 m_xProps->getPropertyValue( ITEMS ) >>= sItems;
77 if( getMultiSelect() )
78 throw uno::RuntimeException( rtl::OUString::createFromAscii(
79 "Attribute use invalid." ), uno::Reference< uno::XInterface >() );
80 uno::Any aRet;
81 if ( sSelection.getLength() )
82 aRet = uno::makeAny( sItems[ sSelection[ 0 ] ] );
83 return aRet;
86 void SAL_CALL
87 ScVbaListBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException)
89 if( getMultiSelect() )
91 throw uno::RuntimeException( rtl::OUString::createFromAscii(
92 "Attribute use invalid." ), uno::Reference< uno::XInterface >() );
94 rtl::OUString sValue = getAnyAsString( _value );
95 uno::Sequence< rtl::OUString > sList;
96 m_xProps->getPropertyValue( ITEMS ) >>= sList;
97 uno::Sequence< sal_Int16 > nList;
98 sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
99 sal_Int16 nValue = -1;
100 sal_Int16 i = 0;
101 for( i = 0; i < nLength; i++ )
103 if( sList[i].equals( sValue ) )
105 nValue = i;
106 break;
109 if( nValue == -1 )
110 throw uno::RuntimeException( rtl::OUString::createFromAscii(
111 "Attribute use invalid." ), uno::Reference< uno::XInterface >() );
113 uno::Sequence< sal_Int16 > nSelectedIndices(1);
114 nSelectedIndices[ 0 ] = nValue;
115 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nSelectedIndices ) );
116 //m_xProps->setPropertyValue( TEXT, uno::makeAny( sValue ) ); //liuchen 2009-8-12 solve the problem that ListBox.Text and ListBox.Value cannot be set
119 ::rtl::OUString SAL_CALL
120 ScVbaListBox::getText() throw (uno::RuntimeException)
122 rtl::OUString result;
123 getValue() >>= result;
124 return result;
127 void SAL_CALL
128 ScVbaListBox::setText( const ::rtl::OUString& _text ) throw (uno::RuntimeException)
130 setValue( uno::makeAny( _text ) ); // seems the same
133 sal_Int32 SAL_CALL
134 ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException)
136 sal_Bool bMultiSelect = sal_False;
137 m_xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MultiSelection" ) ) ) >>= bMultiSelect;
138 return bMultiSelect ? 1 : 0 ;
141 void SAL_CALL
142 ScVbaListBox::setMultiSelect( sal_Int32 _multiselect ) throw (css::uno::RuntimeException)
144 sal_Bool bMultiSelect = _multiselect == 1 ? 1 : 0;
145 m_xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MultiSelection" ) ), uno::makeAny( bMultiSelect ) );
149 css::uno::Any SAL_CALL
150 ScVbaListBox::Selected( sal_Int32 index ) throw (css::uno::RuntimeException)
152 uno::Sequence< rtl::OUString > sList;
153 m_xProps->getPropertyValue( ITEMS ) >>= sList;
154 sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
155 // no choice but to do a horror cast as internally
156 // the indices are but sal_Int16
157 sal_Int16 nIndex = static_cast< sal_Int16 >( index );
158 if( nIndex < 0 || nIndex >= nLength )
159 throw uno::RuntimeException( rtl::OUString::createFromAscii(
160 "Error Number." ), uno::Reference< uno::XInterface >() );
161 m_nIndex = nIndex;
162 return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
165 // Methods
166 void SAL_CALL
167 ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException)
169 mpListHelper->AddItem( pvargItem, pvargIndex );
172 void SAL_CALL
173 ScVbaListBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException)
175 mpListHelper->removeItem( index );
178 void SAL_CALL
179 ScVbaListBox::Clear( ) throw (uno::RuntimeException)
181 mpListHelper->Clear();
184 // this is called when something like the following vba code is used
185 // to set the selected state of particular entries in the Listbox
186 // ListBox1.Selected( 3 ) = false
187 //PropListener
188 void
189 ScVbaListBox::setValueEvent( const uno::Any& value )
191 sal_Bool bValue = sal_False;
192 if( !(value >>= bValue) )
193 throw uno::RuntimeException( rtl::OUString::createFromAscii(
194 "Invalid type\n. need boolean." ), uno::Reference< uno::XInterface >() );
195 uno::Sequence< sal_Int16 > nList;
196 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nList;
197 sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
198 sal_Int16 nIndex = m_nIndex;
199 for( sal_Int16 i = 0; i < nLength; i++ )
201 if( nList[i] == nIndex )
203 if( bValue )
204 return;
205 else
207 for( ; i < nLength - 1; i++ )
209 nList[i] = nList[i + 1];
211 nList.realloc( nLength - 1 );
212 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
213 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
214 return;
218 if( bValue )
220 if( getMultiSelect() )
222 nList.realloc( nLength + 1 );
223 nList[nLength] = nIndex;
225 else
227 nList.realloc( 1 );
228 nList[0] = nIndex;
230 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
231 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
235 // this is called when something like the following vba code is used
236 // to determine the selected state of particular entries in the Listbox
237 // msgbox ListBox1.Selected( 3 )
239 css::uno::Any
240 ScVbaListBox::getValueEvent()
242 uno::Sequence< sal_Int16 > nList;
243 m_xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SelectedItems" ) ) ) >>= nList;
244 sal_Int32 nLength = nList.getLength();
245 sal_Int32 nIndex = m_nIndex;
247 for( sal_Int32 i = 0; i < nLength; i++ )
249 if( nList[i] == nIndex )
250 return uno::makeAny( sal_True );
253 return uno::makeAny( sal_False );
256 void SAL_CALL
257 ScVbaListBox::setRowSource( const rtl::OUString& _rowsource ) throw (uno::RuntimeException)
259 ScVbaControl::setRowSource( _rowsource );
260 mpListHelper->setRowSource( _rowsource );
263 sal_Int32 SAL_CALL
264 ScVbaListBox::getListCount() throw (uno::RuntimeException)
266 return mpListHelper->getListCount();
269 uno::Any SAL_CALL
270 ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException)
272 return mpListHelper->List( pvargIndex, pvarColumn );
275 rtl::OUString&
276 ScVbaListBox::getServiceImplName()
278 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaListBox") );
279 return sImplName;
282 uno::Sequence< rtl::OUString >
283 ScVbaListBox::getServiceNames()
285 static uno::Sequence< rtl::OUString > aServiceNames;
286 if ( aServiceNames.getLength() == 0 )
288 aServiceNames.realloc( 1 );
289 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.msforms.ScVbaListBox" ) );
291 return aServiceNames;