Bump version to 4.1-6
[LibreOffice.git] / vbahelper / source / msforms / vbalistbox.cxx
blob0f9c8b7b4a361c08f2d251f8bcdbcfeac4e98788
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 uno::Sequence< sal_Int16 > nList;
86 sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
87 sal_Int16 nValue = -1;
88 sal_Int16 i = 0;
89 for( i = 0; i < nLength; i++ )
91 if( sList[i].equals( sValue ) )
93 nValue = i;
94 break;
97 if( nValue == -1 )
98 throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
100 uno::Sequence< sal_Int16 > nSelectedIndices(1);
101 uno::Sequence< sal_Int16 > nOldSelectedIndices;
102 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nOldSelectedIndices;
103 nSelectedIndices[ 0 ] = nValue;
104 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nSelectedIndices ) );
105 if ( nSelectedIndices != nOldSelectedIndices )
106 fireClickEvent();
109 OUString SAL_CALL
110 ScVbaListBox::getText() throw (uno::RuntimeException)
112 OUString result;
113 getValue() >>= result;
114 return result;
117 void SAL_CALL
118 ScVbaListBox::setText( const OUString& _text ) throw (uno::RuntimeException)
120 setValue( uno::makeAny( _text ) ); // seems the same
123 sal_Int32 SAL_CALL
124 ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException)
126 sal_Bool bMultiSelect = sal_False;
127 m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
129 return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
132 void SAL_CALL
133 ScVbaListBox::setMultiSelect( sal_Int32 _multiselect ) throw (css::uno::RuntimeException)
135 sal_Bool bBoolVal = false;
136 switch ( _multiselect )
138 case msforms::fmMultiSelect::fmMultiSelectMulti:
139 case msforms::fmMultiSelect::fmMultiSelectExtended:
140 bBoolVal = sal_True;
141 break;
142 case msforms::fmMultiSelect::fmMultiSelectSingle:
143 bBoolVal = sal_False;
144 break;
145 default:
146 throw lang::IllegalArgumentException();
147 break;
149 m_xProps->setPropertyValue( "MultiSelection" , uno::makeAny( bBoolVal ) );
153 css::uno::Any SAL_CALL
154 ScVbaListBox::Selected( sal_Int32 index ) throw (css::uno::RuntimeException)
156 uno::Sequence< OUString > sList;
157 m_xProps->getPropertyValue( ITEMS ) >>= sList;
158 sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
159 // no choice but to do a horror cast as internally
160 // the indices are but sal_Int16
161 sal_Int16 nIndex = static_cast< sal_Int16 >( index );
162 if( nIndex < 0 || nIndex >= nLength )
163 throw uno::RuntimeException( "Error Number." , uno::Reference< uno::XInterface >() );
164 m_nIndex = nIndex;
165 return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
168 // Methods
169 void SAL_CALL
170 ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException)
172 mpListHelper->AddItem( pvargItem, pvargIndex );
175 void SAL_CALL
176 ScVbaListBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException)
178 mpListHelper->removeItem( index );
181 void SAL_CALL
182 ScVbaListBox::Clear( ) throw (uno::RuntimeException)
184 mpListHelper->Clear();
187 // this is called when something like the following vba code is used
188 // to set the selected state of particular entries in the Listbox
189 // ListBox1.Selected( 3 ) = false
190 //PropListener
191 void
192 ScVbaListBox::setValueEvent( const uno::Any& value )
194 sal_Bool bValue = sal_False;
195 if( !(value >>= bValue) )
196 throw uno::RuntimeException( "Invalid type\n. need boolean." , uno::Reference< uno::XInterface >() );
197 uno::Sequence< sal_Int16 > nList;
198 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nList;
199 sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
200 sal_Int16 nIndex = m_nIndex;
201 for( sal_Int16 i = 0; i < nLength; i++ )
203 if( nList[i] == nIndex )
205 if( bValue )
206 return;
207 else
209 for( ; i < nLength - 1; i++ )
211 nList[i] = nList[i + 1];
213 nList.realloc( nLength - 1 );
214 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
215 fireClickEvent();
216 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
217 return;
221 if( bValue )
223 if( getMultiSelect() )
225 nList.realloc( nLength + 1 );
226 nList[nLength] = nIndex;
228 else
230 nList.realloc( 1 );
231 nList[0] = nIndex;
233 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
234 fireClickEvent();
235 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
239 // this is called when something like the following vba code is used
240 // to determine the selected state of particular entries in the Listbox
241 // msgbox ListBox1.Selected( 3 )
243 css::uno::Any
244 ScVbaListBox::getValueEvent()
246 uno::Sequence< sal_Int16 > nList;
247 m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
248 sal_Int32 nLength = nList.getLength();
249 sal_Int32 nIndex = m_nIndex;
251 for( sal_Int32 i = 0; i < nLength; i++ )
253 if( nList[i] == nIndex )
254 return uno::makeAny( sal_True );
257 return uno::makeAny( sal_False );
260 void SAL_CALL
261 ScVbaListBox::setRowSource( const OUString& _rowsource ) throw (uno::RuntimeException)
263 ScVbaControl::setRowSource( _rowsource );
264 mpListHelper->setRowSource( _rowsource );
267 sal_Int32 SAL_CALL
268 ScVbaListBox::getListCount() throw (uno::RuntimeException)
270 return mpListHelper->getListCount();
273 uno::Any SAL_CALL
274 ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException)
276 return mpListHelper->List( pvargIndex, pvarColumn );
279 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont() throw (uno::RuntimeException)
281 return new VbaNewFont( this, mxContext, m_xProps );
284 OUString
285 ScVbaListBox::getServiceImplName()
287 return OUString("ScVbaListBox");
290 uno::Sequence< OUString >
291 ScVbaListBox::getServiceNames()
293 static uno::Sequence< OUString > aServiceNames;
294 if ( aServiceNames.getLength() == 0 )
296 aServiceNames.realloc( 1 );
297 aServiceNames[ 0 ] = "ooo.vba.msforms.ScVbaListBox";
299 return aServiceNames;
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */