1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
))
32 mpListHelper
.reset( new ListControlHelper( m_xProps
) );
37 ScVbaListBox::setListIndex( const uno::Any
& _value
)
41 uno::Reference
< XPropValue
> xPropVal( Selected( nIndex
), uno::UNO_QUERY_THROW
);
42 xPropVal
->setValue( uno::makeAny( true ) );
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 ] );
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." );
65 if ( sSelection
.hasElements() )
66 aRet
<<= sItems
[ sSelection
[ 0 ] ];
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;
83 for( i
= 0; i
< nLength
; i
++ )
85 if( sList
[i
] == sValue
)
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
)
104 ScVbaListBox::getText()
107 getValue() >>= result
;
112 ScVbaListBox::setText( const OUString
& _text
)
114 setValue( uno::makeAny( _text
) ); // seems the same
118 ScVbaListBox::getMultiSelect()
120 bool bMultiSelect
= false;
121 m_xProps
->getPropertyValue( "MultiSelection" ) >>= bMultiSelect
;
123 return bMultiSelect
? msforms::fmMultiSelect::fmMultiSelectMulti
: msforms::fmMultiSelect::fmMultiSelectSingle
;
127 ScVbaListBox::setMultiSelect( sal_Int32 _multiselect
)
129 bool bBoolVal
= false;
130 switch ( _multiselect
)
132 case msforms::fmMultiSelect::fmMultiSelectMulti
:
133 case msforms::fmMultiSelect::fmMultiSelectExtended
:
136 case msforms::fmMultiSelect::fmMultiSelectSingle
:
140 throw lang::IllegalArgumentException();
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." );
159 return uno::makeAny( uno::Reference
< XPropValue
> ( new ScVbaPropValue( this ) ) );
164 ScVbaListBox::AddItem( const uno::Any
& pvargItem
, const uno::Any
& pvargIndex
)
166 mpListHelper
->AddItem( pvargItem
, pvargIndex
);
170 ScVbaListBox::removeItem( const uno::Any
& index
)
172 mpListHelper
->removeItem( index
);
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
186 ScVbaListBox::setValueEvent( const uno::Any
& value
)
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
)
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 ) );
210 m_xProps
->setPropertyValue( "SelectedItems", uno::makeAny( nList
) );
217 if( getMultiSelect() )
219 nList
.realloc( nLength
+ 1 );
220 nList
[nLength
] = nIndex
;
227 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
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 )
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 );
255 ScVbaListBox::setRowSource( const OUString
& _rowsource
)
257 ScVbaControl::setRowSource( _rowsource
);
258 mpListHelper
->setRowSource( _rowsource
);
262 ScVbaListBox::getListCount()
264 return mpListHelper
->getListCount();
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
);
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: */