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