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 <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
) );
41 ScVbaListBox::setListIndex( const uno::Any
& _value
) throw (uno::RuntimeException
)
45 uno::Reference
< XPropValue
> xPropVal( Selected( nIndex
), uno::UNO_QUERY_THROW
);
46 xPropVal
->setValue( uno::makeAny( sal_True
) );
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 ] );
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
>() );
69 if ( sSelection
.getLength() )
70 aRet
= uno::makeAny( sItems
[ sSelection
[ 0 ] ] );
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;
88 for( i
= 0; i
< nLength
; i
++ )
90 if( sList
[i
].equals( sValue
) )
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
)
106 m_xProps
->setPropertyValue( TEXT
, uno::makeAny( sValue
) );
110 ScVbaListBox::getText() throw (uno::RuntimeException
)
113 getValue() >>= result
;
118 ScVbaListBox::setText( const OUString
& _text
) throw (uno::RuntimeException
)
120 setValue( uno::makeAny( _text
) ); // seems the same
124 ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException
)
126 sal_Bool bMultiSelect
= sal_False
;
127 m_xProps
->getPropertyValue( "MultiSelection" ) >>= bMultiSelect
;
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
>() );
150 return uno::makeAny( uno::Reference
< XPropValue
> ( new ScVbaPropValue( this ) ) );
155 ScVbaListBox::AddItem( const uno::Any
& pvargItem
, const uno::Any
& pvargIndex
) throw (uno::RuntimeException
)
157 mpListHelper
->AddItem( pvargItem
, pvargIndex
);
161 ScVbaListBox::removeItem( const uno::Any
& index
) throw (uno::RuntimeException
)
163 mpListHelper
->removeItem( index
);
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
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
)
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 ) );
201 m_xProps
->setPropertyValue( SELECTEDITEMS
, uno::makeAny( nList
) );
208 if( getMultiSelect() )
210 nList
.realloc( nLength
+ 1 );
211 nList
[nLength
] = nIndex
;
218 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
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 )
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
);
246 ScVbaListBox::setRowSource( const OUString
& _rowsource
) throw (uno::RuntimeException
)
248 ScVbaControl::setRowSource( _rowsource
);
249 mpListHelper
->setRowSource( _rowsource
);
253 ScVbaListBox::getListCount() throw (uno::RuntimeException
)
255 return mpListHelper
->getListCount();
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
);
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: */