Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / vbahelper / source / msforms / vbalistbox.cxx
blobba16007786a59c9a5d95e6a3d02b6e543e4d261c
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/sequence.hxx>
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 , maListHelper( m_xProps )
31 , m_nIndex(0)
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::Any( true ) );
45 uno::Any SAL_CALL
46 ScVbaListBox::getListIndex()
48 uno::Sequence< sal_Int16 > sSelection;
49 m_xProps->getPropertyValue( u"SelectedItems"_ustr ) >>= 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( u"SelectedItems"_ustr ) >>= sSelection;
61 m_xProps->getPropertyValue( u"StringItemList"_ustr ) >>= sItems;
62 if( getMultiSelect() )
63 throw uno::RuntimeException( u"Attribute use invalid."_ustr );
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( u"Attribute use invalid."_ustr );
77 OUString sValue = getAnyAsString( _value );
78 uno::Sequence< OUString > sList;
79 m_xProps->getPropertyValue( u"StringItemList"_ustr ) >>= sList;
80 sal_Int16 nValue = static_cast<sal_Int16>(comphelper::findValue(sList, sValue));
81 if( nValue == -1 )
82 throw uno::RuntimeException( u"Attribute use invalid."_ustr );
84 uno::Sequence< sal_Int16 > nSelectedIndices { nValue };
85 uno::Sequence< sal_Int16 > nOldSelectedIndices;
86 m_xProps->getPropertyValue( u"SelectedItems"_ustr ) >>= nOldSelectedIndices;
87 m_xProps->setPropertyValue( u"SelectedItems"_ustr, uno::Any( nSelectedIndices ) );
88 if ( nSelectedIndices != nOldSelectedIndices )
89 fireClickEvent();
92 OUString SAL_CALL
93 ScVbaListBox::getText()
95 OUString result;
96 getValue() >>= result;
97 return result;
100 void SAL_CALL
101 ScVbaListBox::setText( const OUString& _text )
103 setValue( uno::Any( _text ) ); // seems the same
106 sal_Int32 SAL_CALL
107 ScVbaListBox::getMultiSelect()
109 bool bMultiSelect = false;
110 m_xProps->getPropertyValue( u"MultiSelection"_ustr ) >>= bMultiSelect;
112 return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
115 void SAL_CALL
116 ScVbaListBox::setMultiSelect( sal_Int32 _multiselect )
118 bool bBoolVal = false;
119 switch ( _multiselect )
121 case msforms::fmMultiSelect::fmMultiSelectMulti:
122 case msforms::fmMultiSelect::fmMultiSelectExtended:
123 bBoolVal = true;
124 break;
125 case msforms::fmMultiSelect::fmMultiSelectSingle:
126 bBoolVal = false;
127 break;
128 default:
129 throw lang::IllegalArgumentException();
130 break;
132 m_xProps->setPropertyValue( u"MultiSelection"_ustr , uno::Any( bBoolVal ) );
136 css::uno::Any SAL_CALL
137 ScVbaListBox::Selected( sal_Int32 index )
139 uno::Sequence< OUString > sList;
140 m_xProps->getPropertyValue( u"StringItemList"_ustr ) >>= sList;
141 sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
142 // no choice but to do a horror cast as internally
143 // the indices are but sal_Int16
144 sal_Int16 nIndex = static_cast< sal_Int16 >( index );
145 if( nIndex < 0 || nIndex >= nLength )
146 throw uno::RuntimeException( u"Error Number."_ustr );
147 m_nIndex = nIndex;
148 return uno::Any( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
151 // Methods
152 void SAL_CALL
153 ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex )
155 maListHelper.AddItem( pvargItem, pvargIndex );
158 void SAL_CALL
159 ScVbaListBox::removeItem( const uno::Any& index )
161 maListHelper.removeItem( index );
164 void SAL_CALL
165 ScVbaListBox::Clear( )
167 maListHelper.Clear();
170 // this is called when something like the following vba code is used
171 // to set the selected state of particular entries in the Listbox
172 // ListBox1.Selected( 3 ) = false
173 //PropListener
174 void
175 ScVbaListBox::setValueEvent( const uno::Any& value )
177 bool bValue = false;
178 if( !(value >>= bValue) )
179 throw uno::RuntimeException( u"Invalid type. need boolean."_ustr );
180 uno::Sequence< sal_Int16 > nList;
181 m_xProps->getPropertyValue( u"SelectedItems"_ustr ) >>= nList;
182 sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
183 sal_Int16 nIndex = m_nIndex;
184 for( sal_Int16 i = 0; i < nLength; i++ )
186 if( nList[i] == nIndex )
188 if( !bValue )
190 auto pList = nList.getArray();
191 for( ; i < nLength - 1; i++ )
193 pList[i] = nList[i + 1];
195 nList.realloc( nLength - 1 );
196 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
197 fireClickEvent();
198 m_xProps->setPropertyValue( u"SelectedItems"_ustr, uno::Any( nList ) );
200 return;
203 if( !bValue )
204 return;
206 if( getMultiSelect() )
208 nList.realloc( nLength + 1 );
209 nList.getArray()[nLength] = nIndex;
211 else
213 nList = { nIndex };
215 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
216 fireClickEvent();
217 m_xProps->setPropertyValue( u"SelectedItems"_ustr, uno::Any( nList ) );
220 // this is called when something like the following vba code is used
221 // to determine the selected state of particular entries in the Listbox
222 // msgbox ListBox1.Selected( 3 )
224 css::uno::Any
225 ScVbaListBox::getValueEvent()
227 uno::Sequence< sal_Int16 > nList;
228 m_xProps->getPropertyValue( u"SelectedItems"_ustr ) >>= nList;
229 sal_Int32 nIndex = m_nIndex;
230 bool bRet = std::find(std::cbegin(nList), std::cend(nList), nIndex) != std::cend(nList);
232 return uno::Any( bRet );
235 void SAL_CALL
236 ScVbaListBox::setRowSource( const OUString& _rowsource )
238 ScVbaControl::setRowSource( _rowsource );
239 maListHelper.setRowSource( _rowsource );
242 sal_Int32 SAL_CALL
243 ScVbaListBox::getListCount()
245 return maListHelper.getListCount();
248 uno::Any SAL_CALL
249 ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn )
251 return maListHelper.List( pvargIndex, pvarColumn );
254 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont()
256 return new VbaNewFont( m_xProps );
259 OUString
260 ScVbaListBox::getServiceImplName()
262 return u"ScVbaListBox"_ustr;
265 uno::Sequence< OUString >
266 ScVbaListBox::getServiceNames()
268 static uno::Sequence< OUString > const aServiceNames
270 u"ooo.vba.msforms.ScVbaListBox"_ustr
272 return aServiceNames;
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */