bump product version to 6.3.0.0.beta1
[LibreOffice.git] / vbahelper / source / msforms / vbalistbox.cxx
blob13c87844019337407c1238461abe6e31305eaac5
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 <com/sun/star/form/validation/XValidatableFormComponent.hpp>
23 #include <ooo/vba/msforms/fmMultiSelect.hpp>
25 using namespace com::sun::star;
26 using namespace ooo::vba;
28 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, std::unique_ptr<ov::AbstractGeometryAttributes> pGeomHelper )
29 : ListBoxImpl_BASE(xParent, xContext, xControl, xModel, std::move(pGeomHelper))
30 , m_nIndex(0)
32 mpListHelper.reset( new ListControlHelper( m_xProps ) );
35 // Attributes
36 void SAL_CALL
37 ScVbaListBox::setListIndex( const uno::Any& _value )
39 sal_Int32 nIndex = 0;
40 _value >>= nIndex;
41 uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
42 xPropVal->setValue( uno::makeAny( true ) );
45 uno::Any SAL_CALL
46 ScVbaListBox::getListIndex()
48 uno::Sequence< sal_Int16 > sSelection;
49 m_xProps->getPropertyValue( "SelectedItems" ) >>= sSelection;
50 if ( !sSelection.hasElements() )
51 return uno::Any( sal_Int32( -1 ) );
52 return uno::Any( sSelection[ 0 ] );
55 uno::Any SAL_CALL
56 ScVbaListBox::getValue()
58 uno::Sequence< sal_Int16 > sSelection;
59 uno::Sequence< OUString > sItems;
60 m_xProps->getPropertyValue( "SelectedItems" ) >>= sSelection;
61 m_xProps->getPropertyValue( "StringItemList" ) >>= sItems;
62 if( getMultiSelect() )
63 throw uno::RuntimeException( "Attribute use invalid." );
64 uno::Any aRet;
65 if ( sSelection.hasElements() )
66 aRet <<= sItems[ sSelection[ 0 ] ];
67 return aRet;
70 void SAL_CALL
71 ScVbaListBox::setValue( const uno::Any& _value )
73 if( getMultiSelect() )
75 throw uno::RuntimeException( "Attribute use invalid." );
77 OUString sValue = getAnyAsString( _value );
78 uno::Sequence< OUString > sList;
79 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
80 sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
81 sal_Int16 nValue = -1;
82 sal_Int16 i = 0;
83 for( i = 0; i < nLength; i++ )
85 if( sList[i] == sValue )
87 nValue = i;
88 break;
91 if( nValue == -1 )
92 throw uno::RuntimeException( "Attribute use invalid." );
94 uno::Sequence< sal_Int16 > nSelectedIndices(1);
95 uno::Sequence< sal_Int16 > nOldSelectedIndices;
96 m_xProps->getPropertyValue( "SelectedItems" ) >>= nOldSelectedIndices;
97 nSelectedIndices[ 0 ] = nValue;
98 m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nSelectedIndices ) );
99 if ( nSelectedIndices != nOldSelectedIndices )
100 fireClickEvent();
103 OUString SAL_CALL
104 ScVbaListBox::getText()
106 OUString result;
107 getValue() >>= result;
108 return result;
111 void SAL_CALL
112 ScVbaListBox::setText( const OUString& _text )
114 setValue( uno::makeAny( _text ) ); // seems the same
117 sal_Int32 SAL_CALL
118 ScVbaListBox::getMultiSelect()
120 bool bMultiSelect = false;
121 m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
123 return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
126 void SAL_CALL
127 ScVbaListBox::setMultiSelect( sal_Int32 _multiselect )
129 bool bBoolVal = false;
130 switch ( _multiselect )
132 case msforms::fmMultiSelect::fmMultiSelectMulti:
133 case msforms::fmMultiSelect::fmMultiSelectExtended:
134 bBoolVal = true;
135 break;
136 case msforms::fmMultiSelect::fmMultiSelectSingle:
137 bBoolVal = false;
138 break;
139 default:
140 throw lang::IllegalArgumentException();
141 break;
143 m_xProps->setPropertyValue( "MultiSelection" , uno::makeAny( bBoolVal ) );
147 css::uno::Any SAL_CALL
148 ScVbaListBox::Selected( sal_Int32 index )
150 uno::Sequence< OUString > sList;
151 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
152 sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
153 // no choice but to do a horror cast as internally
154 // the indices are but sal_Int16
155 sal_Int16 nIndex = static_cast< sal_Int16 >( index );
156 if( nIndex < 0 || nIndex >= nLength )
157 throw uno::RuntimeException( "Error Number." );
158 m_nIndex = nIndex;
159 return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
162 // Methods
163 void SAL_CALL
164 ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex )
166 mpListHelper->AddItem( pvargItem, pvargIndex );
169 void SAL_CALL
170 ScVbaListBox::removeItem( const uno::Any& index )
172 mpListHelper->removeItem( index );
175 void SAL_CALL
176 ScVbaListBox::Clear( )
178 mpListHelper->Clear();
181 // this is called when something like the following vba code is used
182 // to set the selected state of particular entries in the Listbox
183 // ListBox1.Selected( 3 ) = false
184 //PropListener
185 void
186 ScVbaListBox::setValueEvent( const uno::Any& value )
188 bool bValue = false;
189 if( !(value >>= bValue) )
190 throw uno::RuntimeException( "Invalid type. need boolean." );
191 uno::Sequence< sal_Int16 > nList;
192 m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
193 sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
194 sal_Int16 nIndex = m_nIndex;
195 for( sal_Int16 i = 0; i < nLength; i++ )
197 if( nList[i] == nIndex )
199 if( bValue )
200 return;
201 else
203 for( ; i < nLength - 1; i++ )
205 nList[i] = nList[i + 1];
207 nList.realloc( nLength - 1 );
208 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
209 fireClickEvent();
210 m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nList ) );
211 return;
215 if( bValue )
217 if( getMultiSelect() )
219 nList.realloc( nLength + 1 );
220 nList[nLength] = nIndex;
222 else
224 nList.realloc( 1 );
225 nList[0] = nIndex;
227 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
228 fireClickEvent();
229 m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nList ) );
233 // this is called when something like the following vba code is used
234 // to determine the selected state of particular entries in the Listbox
235 // msgbox ListBox1.Selected( 3 )
237 css::uno::Any
238 ScVbaListBox::getValueEvent()
240 uno::Sequence< sal_Int16 > nList;
241 m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
242 sal_Int32 nLength = nList.getLength();
243 sal_Int32 nIndex = m_nIndex;
245 for( sal_Int32 i = 0; i < nLength; i++ )
247 if( nList[i] == nIndex )
248 return uno::makeAny( true );
251 return uno::makeAny( false );
254 void SAL_CALL
255 ScVbaListBox::setRowSource( const OUString& _rowsource )
257 ScVbaControl::setRowSource( _rowsource );
258 mpListHelper->setRowSource( _rowsource );
261 sal_Int32 SAL_CALL
262 ScVbaListBox::getListCount()
264 return mpListHelper->getListCount();
267 uno::Any SAL_CALL
268 ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn )
270 return mpListHelper->List( pvargIndex, pvarColumn );
273 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont()
275 return new VbaNewFont( m_xProps );
278 OUString
279 ScVbaListBox::getServiceImplName()
281 return OUString("ScVbaListBox");
284 uno::Sequence< OUString >
285 ScVbaListBox::getServiceNames()
287 static uno::Sequence< OUString > const aServiceNames
289 "ooo.vba.msforms.ScVbaListBox"
291 return aServiceNames;
294 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */