Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / vbahelper / source / msforms / vbalistbox.cxx
blob41ccb0af21c20f9eb921854ac017ccc30779101d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "vbalistbox.hxx"
21 #include "vbanewfont.hxx"
22 #include <comphelper/anytostring.hxx>
23 #include <com/sun/star/script/ArrayWrapper.hpp>
24 #include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
25 #include <ooo/vba/msforms/fmMultiSelect.hpp>
27 using namespace com::sun::star;
28 using namespace ooo::vba;
30 const static OUString TEXT( "Text" );
31 const static OUString SELECTEDITEMS( "SelectedItems" );
32 const static OUString ITEMS( "StringItemList" );
35 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 )
37 mpListHelper.reset( new ListControlHelper( m_xProps ) );
40 // Attributes
41 void SAL_CALL
42 ScVbaListBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException)
44 sal_Int32 nIndex = 0;
45 _value >>= nIndex;
46 uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
47 xPropVal->setValue( uno::makeAny( sal_True ) );
50 uno::Any SAL_CALL
51 ScVbaListBox::getListIndex() throw (uno::RuntimeException)
53 uno::Sequence< sal_Int16 > sSelection;
54 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
55 if ( sSelection.getLength() == 0 )
56 return uno::Any( sal_Int32( -1 ) );
57 return uno::Any( sSelection[ 0 ] );
60 uno::Any SAL_CALL
61 ScVbaListBox::getValue() throw (uno::RuntimeException)
63 uno::Sequence< sal_Int16 > sSelection;
64 uno::Sequence< OUString > sItems;
65 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
66 m_xProps->getPropertyValue( ITEMS ) >>= sItems;
67 if( getMultiSelect() )
68 throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
69 uno::Any aRet;
70 if ( sSelection.getLength() )
71 aRet = uno::makeAny( sItems[ sSelection[ 0 ] ] );
72 return aRet;
75 void SAL_CALL
76 ScVbaListBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException)
78 if( getMultiSelect() )
80 throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
82 OUString sValue = getAnyAsString( _value );
83 uno::Sequence< OUString > sList;
84 m_xProps->getPropertyValue( ITEMS ) >>= sList;
85 sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
86 sal_Int16 nValue = -1;
87 sal_Int16 i = 0;
88 for( i = 0; i < nLength; i++ )
90 if( sList[i].equals( sValue ) )
92 nValue = i;
93 break;
96 if( nValue == -1 )
97 throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
99 uno::Sequence< sal_Int16 > nSelectedIndices(1);
100 uno::Sequence< sal_Int16 > nOldSelectedIndices;
101 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nOldSelectedIndices;
102 nSelectedIndices[ 0 ] = nValue;
103 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nSelectedIndices ) );
104 if ( nSelectedIndices != nOldSelectedIndices )
105 fireClickEvent();
108 OUString SAL_CALL
109 ScVbaListBox::getText() throw (uno::RuntimeException)
111 OUString result;
112 getValue() >>= result;
113 return result;
116 void SAL_CALL
117 ScVbaListBox::setText( const OUString& _text ) throw (uno::RuntimeException)
119 setValue( uno::makeAny( _text ) ); // seems the same
122 sal_Int32 SAL_CALL
123 ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException)
125 sal_Bool bMultiSelect = sal_False;
126 m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
128 return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
131 void SAL_CALL
132 ScVbaListBox::setMultiSelect( sal_Int32 _multiselect ) throw (css::uno::RuntimeException)
134 sal_Bool bBoolVal = false;
135 switch ( _multiselect )
137 case msforms::fmMultiSelect::fmMultiSelectMulti:
138 case msforms::fmMultiSelect::fmMultiSelectExtended:
139 bBoolVal = sal_True;
140 break;
141 case msforms::fmMultiSelect::fmMultiSelectSingle:
142 bBoolVal = sal_False;
143 break;
144 default:
145 throw lang::IllegalArgumentException();
146 break;
148 m_xProps->setPropertyValue( "MultiSelection" , uno::makeAny( bBoolVal ) );
152 css::uno::Any SAL_CALL
153 ScVbaListBox::Selected( sal_Int32 index ) throw (css::uno::RuntimeException)
155 uno::Sequence< OUString > sList;
156 m_xProps->getPropertyValue( ITEMS ) >>= sList;
157 sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
158 // no choice but to do a horror cast as internally
159 // the indices are but sal_Int16
160 sal_Int16 nIndex = static_cast< sal_Int16 >( index );
161 if( nIndex < 0 || nIndex >= nLength )
162 throw uno::RuntimeException( "Error Number." , uno::Reference< uno::XInterface >() );
163 m_nIndex = nIndex;
164 return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
167 // Methods
168 void SAL_CALL
169 ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException)
171 mpListHelper->AddItem( pvargItem, pvargIndex );
174 void SAL_CALL
175 ScVbaListBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException)
177 mpListHelper->removeItem( index );
180 void SAL_CALL
181 ScVbaListBox::Clear( ) throw (uno::RuntimeException)
183 mpListHelper->Clear();
186 // this is called when something like the following vba code is used
187 // to set the selected state of particular entries in the Listbox
188 // ListBox1.Selected( 3 ) = false
189 //PropListener
190 void
191 ScVbaListBox::setValueEvent( const uno::Any& value )
193 sal_Bool bValue = sal_False;
194 if( !(value >>= bValue) )
195 throw uno::RuntimeException( "Invalid type\n. need boolean." , uno::Reference< uno::XInterface >() );
196 uno::Sequence< sal_Int16 > nList;
197 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nList;
198 sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
199 sal_Int16 nIndex = m_nIndex;
200 for( sal_Int16 i = 0; i < nLength; i++ )
202 if( nList[i] == nIndex )
204 if( bValue )
205 return;
206 else
208 for( ; i < nLength - 1; i++ )
210 nList[i] = nList[i + 1];
212 nList.realloc( nLength - 1 );
213 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
214 fireClickEvent();
215 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
216 return;
220 if( bValue )
222 if( getMultiSelect() )
224 nList.realloc( nLength + 1 );
225 nList[nLength] = nIndex;
227 else
229 nList.realloc( 1 );
230 nList[0] = nIndex;
232 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
233 fireClickEvent();
234 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
238 // this is called when something like the following vba code is used
239 // to determine the selected state of particular entries in the Listbox
240 // msgbox ListBox1.Selected( 3 )
242 css::uno::Any
243 ScVbaListBox::getValueEvent()
245 uno::Sequence< sal_Int16 > nList;
246 m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
247 sal_Int32 nLength = nList.getLength();
248 sal_Int32 nIndex = m_nIndex;
250 for( sal_Int32 i = 0; i < nLength; i++ )
252 if( nList[i] == nIndex )
253 return uno::makeAny( sal_True );
256 return uno::makeAny( sal_False );
259 void SAL_CALL
260 ScVbaListBox::setRowSource( const OUString& _rowsource ) throw (uno::RuntimeException)
262 ScVbaControl::setRowSource( _rowsource );
263 mpListHelper->setRowSource( _rowsource );
266 sal_Int32 SAL_CALL
267 ScVbaListBox::getListCount() throw (uno::RuntimeException)
269 return mpListHelper->getListCount();
272 uno::Any SAL_CALL
273 ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException)
275 return mpListHelper->List( pvargIndex, pvarColumn );
278 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont() throw (uno::RuntimeException)
280 return new VbaNewFont( this, mxContext, m_xProps );
283 OUString
284 ScVbaListBox::getServiceImplName()
286 return OUString("ScVbaListBox");
289 uno::Sequence< OUString >
290 ScVbaListBox::getServiceNames()
292 static uno::Sequence< OUString > aServiceNames;
293 if ( aServiceNames.getLength() == 0 )
295 aServiceNames.realloc( 1 );
296 aServiceNames[ 0 ] = "ooo.vba.msforms.ScVbaListBox";
298 return aServiceNames;
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */