bump product version to 5.0.4.1
[LibreOffice.git] / vbahelper / source / msforms / vbalistbox.cxx
blob38effb8ecddf15c99c706ea0be11c9a68bd37771
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 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 )
31 : ListBoxImpl_BASE(xParent, xContext, xControl, xModel, pGeomHelper)
32 , m_nIndex(0)
34 mpListHelper.reset( new ListControlHelper( m_xProps ) );
37 // Attributes
38 void SAL_CALL
39 ScVbaListBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException, std::exception)
41 sal_Int32 nIndex = 0;
42 _value >>= nIndex;
43 uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
44 xPropVal->setValue( uno::makeAny( sal_True ) );
47 uno::Any SAL_CALL
48 ScVbaListBox::getListIndex() throw (uno::RuntimeException, std::exception)
50 uno::Sequence< sal_Int16 > sSelection;
51 m_xProps->getPropertyValue( "SelectedItems" ) >>= sSelection;
52 if ( sSelection.getLength() == 0 )
53 return uno::Any( sal_Int32( -1 ) );
54 return uno::Any( sSelection[ 0 ] );
57 uno::Any SAL_CALL
58 ScVbaListBox::getValue() throw (uno::RuntimeException, std::exception)
60 uno::Sequence< sal_Int16 > sSelection;
61 uno::Sequence< OUString > sItems;
62 m_xProps->getPropertyValue( "SelectedItems" ) >>= sSelection;
63 m_xProps->getPropertyValue( "StringItemList" ) >>= sItems;
64 if( getMultiSelect() )
65 throw uno::RuntimeException( "Attribute use invalid." );
66 uno::Any aRet;
67 if ( sSelection.getLength() )
68 aRet = uno::makeAny( sItems[ sSelection[ 0 ] ] );
69 return aRet;
72 void SAL_CALL
73 ScVbaListBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException, std::exception)
75 if( getMultiSelect() )
77 throw uno::RuntimeException( "Attribute use invalid." );
79 OUString sValue = getAnyAsString( _value );
80 uno::Sequence< OUString > sList;
81 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
82 sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
83 sal_Int16 nValue = -1;
84 sal_Int16 i = 0;
85 for( i = 0; i < nLength; i++ )
87 if( sList[i].equals( sValue ) )
89 nValue = i;
90 break;
93 if( nValue == -1 )
94 throw uno::RuntimeException( "Attribute use invalid." );
96 uno::Sequence< sal_Int16 > nSelectedIndices(1);
97 uno::Sequence< sal_Int16 > nOldSelectedIndices;
98 m_xProps->getPropertyValue( "SelectedItems" ) >>= nOldSelectedIndices;
99 nSelectedIndices[ 0 ] = nValue;
100 m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nSelectedIndices ) );
101 if ( nSelectedIndices != nOldSelectedIndices )
102 fireClickEvent();
105 OUString SAL_CALL
106 ScVbaListBox::getText() throw (uno::RuntimeException, std::exception)
108 OUString result;
109 getValue() >>= result;
110 return result;
113 void SAL_CALL
114 ScVbaListBox::setText( const OUString& _text ) throw (uno::RuntimeException, std::exception)
116 setValue( uno::makeAny( _text ) ); // seems the same
119 sal_Int32 SAL_CALL
120 ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException, std::exception)
122 bool bMultiSelect = false;
123 m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
125 return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
128 void SAL_CALL
129 ScVbaListBox::setMultiSelect( sal_Int32 _multiselect ) throw (css::uno::RuntimeException, std::exception)
131 bool bBoolVal = false;
132 switch ( _multiselect )
134 case msforms::fmMultiSelect::fmMultiSelectMulti:
135 case msforms::fmMultiSelect::fmMultiSelectExtended:
136 bBoolVal = true;
137 break;
138 case msforms::fmMultiSelect::fmMultiSelectSingle:
139 bBoolVal = false;
140 break;
141 default:
142 throw lang::IllegalArgumentException();
143 break;
145 m_xProps->setPropertyValue( "MultiSelection" , uno::makeAny( bBoolVal ) );
149 css::uno::Any SAL_CALL
150 ScVbaListBox::Selected( sal_Int32 index ) throw (css::uno::RuntimeException, std::exception)
152 uno::Sequence< OUString > sList;
153 m_xProps->getPropertyValue( "StringItemList" ) >>= 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( "Error Number." );
160 m_nIndex = nIndex;
161 return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
164 // Methods
165 void SAL_CALL
166 ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException, std::exception)
168 mpListHelper->AddItem( pvargItem, pvargIndex );
171 void SAL_CALL
172 ScVbaListBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
174 mpListHelper->removeItem( index );
177 void SAL_CALL
178 ScVbaListBox::Clear( ) throw (uno::RuntimeException, std::exception)
180 mpListHelper->Clear();
183 // this is called when something like the following vba code is used
184 // to set the selected state of particular entries in the Listbox
185 // ListBox1.Selected( 3 ) = false
186 //PropListener
187 void
188 ScVbaListBox::setValueEvent( const uno::Any& value )
190 bool bValue = false;
191 if( !(value >>= bValue) )
192 throw uno::RuntimeException( "Invalid type. need boolean." );
193 uno::Sequence< sal_Int16 > nList;
194 m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
195 sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
196 sal_Int16 nIndex = m_nIndex;
197 for( sal_Int16 i = 0; i < nLength; i++ )
199 if( nList[i] == nIndex )
201 if( bValue )
202 return;
203 else
205 for( ; i < nLength - 1; i++ )
207 nList[i] = nList[i + 1];
209 nList.realloc( nLength - 1 );
210 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
211 fireClickEvent();
212 m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nList ) );
213 return;
217 if( bValue )
219 if( getMultiSelect() )
221 nList.realloc( nLength + 1 );
222 nList[nLength] = nIndex;
224 else
226 nList.realloc( 1 );
227 nList[0] = nIndex;
229 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
230 fireClickEvent();
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( "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 OUString& _rowsource ) throw (uno::RuntimeException, std::exception)
259 ScVbaControl::setRowSource( _rowsource );
260 mpListHelper->setRowSource( _rowsource );
263 sal_Int32 SAL_CALL
264 ScVbaListBox::getListCount() throw (uno::RuntimeException, std::exception)
266 return mpListHelper->getListCount();
269 uno::Any SAL_CALL
270 ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException, std::exception)
272 return mpListHelper->List( pvargIndex, pvarColumn );
275 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont() throw (uno::RuntimeException, std::exception)
277 return new VbaNewFont( m_xProps );
280 OUString
281 ScVbaListBox::getServiceImplName()
283 return OUString("ScVbaListBox");
286 uno::Sequence< OUString >
287 ScVbaListBox::getServiceNames()
289 static uno::Sequence< OUString > aServiceNames;
290 if ( aServiceNames.getLength() == 0 )
292 aServiceNames.realloc( 1 );
293 aServiceNames[ 0 ] = "ooo.vba.msforms.ScVbaListBox";
295 return aServiceNames;
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */