Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / vbahelper / source / msforms / vbalistbox.cxx
blob483985ea26cd6448ca25f499d7f7192cdad4e81c
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>
26 using namespace com::sun::star;
27 using namespace ooo::vba;
29 const static OUString TEXT( "Text" );
30 const static OUString SELECTEDITEMS( "SelectedItems" );
31 const static OUString ITEMS( "StringItemList" );
34 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 )
36 mpListHelper.reset( new ListControlHelper( m_xProps ) );
39 // Attributes
40 void SAL_CALL
41 ScVbaListBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException)
43 sal_Int32 nIndex = 0;
44 _value >>= nIndex;
45 uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
46 xPropVal->setValue( uno::makeAny( sal_True ) );
49 uno::Any SAL_CALL
50 ScVbaListBox::getListIndex() throw (uno::RuntimeException)
52 uno::Sequence< sal_Int16 > sSelection;
53 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
54 if ( sSelection.getLength() == 0 )
55 return uno::Any( sal_Int32( -1 ) );
56 return uno::Any( sSelection[ 0 ] );
59 uno::Any SAL_CALL
60 ScVbaListBox::getValue() throw (uno::RuntimeException)
62 uno::Sequence< sal_Int16 > sSelection;
63 uno::Sequence< OUString > sItems;
64 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
65 m_xProps->getPropertyValue( ITEMS ) >>= sItems;
66 if( getMultiSelect() )
67 throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
68 uno::Any aRet;
69 if ( sSelection.getLength() )
70 aRet = uno::makeAny( sItems[ sSelection[ 0 ] ] );
71 return aRet;
74 void SAL_CALL
75 ScVbaListBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException)
77 if( getMultiSelect() )
79 throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
81 OUString sValue = getAnyAsString( _value );
82 uno::Sequence< OUString > sList;
83 m_xProps->getPropertyValue( ITEMS ) >>= sList;
84 uno::Sequence< sal_Int16 > nList;
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();
106 m_xProps->setPropertyValue( TEXT, uno::makeAny( sValue ) );
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_Bool SAL_CALL
124 ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException)
126 sal_Bool bMultiSelect = sal_False;
127 m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
128 return bMultiSelect;
131 void SAL_CALL
132 ScVbaListBox::setMultiSelect( sal_Bool _multiselect ) throw (css::uno::RuntimeException)
134 m_xProps->setPropertyValue( "MultiSelection" , uno::makeAny( _multiselect ) );
138 css::uno::Any SAL_CALL
139 ScVbaListBox::Selected( sal_Int32 index ) throw (css::uno::RuntimeException)
141 uno::Sequence< OUString > sList;
142 m_xProps->getPropertyValue( ITEMS ) >>= sList;
143 sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
144 // no choice but to do a horror cast as internally
145 // the indices are but sal_Int16
146 sal_Int16 nIndex = static_cast< sal_Int16 >( index );
147 if( nIndex < 0 || nIndex >= nLength )
148 throw uno::RuntimeException( "Error Number." , uno::Reference< uno::XInterface >() );
149 m_nIndex = nIndex;
150 return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
153 // Methods
154 void SAL_CALL
155 ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException)
157 mpListHelper->AddItem( pvargItem, pvargIndex );
160 void SAL_CALL
161 ScVbaListBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException)
163 mpListHelper->removeItem( index );
166 void SAL_CALL
167 ScVbaListBox::Clear( ) throw (uno::RuntimeException)
169 mpListHelper->Clear();
172 // this is called when something like the following vba code is used
173 // to set the selected state of particular entries in the Listbox
174 // ListBox1.Selected( 3 ) = false
175 //PropListener
176 void
177 ScVbaListBox::setValueEvent( const uno::Any& value )
179 sal_Bool bValue = sal_False;
180 if( !(value >>= bValue) )
181 throw uno::RuntimeException( "Invalid type\n. need boolean." , uno::Reference< uno::XInterface >() );
182 uno::Sequence< sal_Int16 > nList;
183 m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nList;
184 sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
185 sal_Int16 nIndex = m_nIndex;
186 for( sal_Int16 i = 0; i < nLength; i++ )
188 if( nList[i] == nIndex )
190 if( bValue )
191 return;
192 else
194 for( ; i < nLength - 1; i++ )
196 nList[i] = nList[i + 1];
198 nList.realloc( nLength - 1 );
199 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
200 fireClickEvent();
201 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
202 return;
206 if( bValue )
208 if( getMultiSelect() )
210 nList.realloc( nLength + 1 );
211 nList[nLength] = nIndex;
213 else
215 nList.realloc( 1 );
216 nList[0] = nIndex;
218 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
219 fireClickEvent();
220 m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
224 // this is called when something like the following vba code is used
225 // to determine the selected state of particular entries in the Listbox
226 // msgbox ListBox1.Selected( 3 )
228 css::uno::Any
229 ScVbaListBox::getValueEvent()
231 uno::Sequence< sal_Int16 > nList;
232 m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
233 sal_Int32 nLength = nList.getLength();
234 sal_Int32 nIndex = m_nIndex;
236 for( sal_Int32 i = 0; i < nLength; i++ )
238 if( nList[i] == nIndex )
239 return uno::makeAny( sal_True );
242 return uno::makeAny( sal_False );
245 void SAL_CALL
246 ScVbaListBox::setRowSource( const OUString& _rowsource ) throw (uno::RuntimeException)
248 ScVbaControl::setRowSource( _rowsource );
249 mpListHelper->setRowSource( _rowsource );
252 sal_Int32 SAL_CALL
253 ScVbaListBox::getListCount() throw (uno::RuntimeException)
255 return mpListHelper->getListCount();
258 uno::Any SAL_CALL
259 ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException)
261 return mpListHelper->List( pvargIndex, pvarColumn );
264 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont() throw (uno::RuntimeException)
266 return new VbaNewFont( this, mxContext, m_xProps );
269 OUString
270 ScVbaListBox::getServiceImplName()
272 return OUString("ScVbaListBox");
275 uno::Sequence< OUString >
276 ScVbaListBox::getServiceNames()
278 static uno::Sequence< OUString > aServiceNames;
279 if ( aServiceNames.getLength() == 0 )
281 aServiceNames.realloc( 1 );
282 aServiceNames[ 0 ] = "ooo.vba.msforms.ScVbaListBox";
284 return aServiceNames;
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */