LibreOffice Module vbahelper (master) 1
vbalistbox.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "vbalistbox.hxx"
21#include "vbanewfont.hxx"
23#include <ooo/vba/msforms/fmMultiSelect.hpp>
24
25using namespace com::sun::star;
26using namespace ooo::vba;
27
28ScVbaListBox::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)
32{
33}
34
35// Attributes
36void SAL_CALL
38{
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 ) );
43}
44
45uno::Any SAL_CALL
47{
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 ] );
53}
54
55uno::Any SAL_CALL
57{
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;
68}
69
70void SAL_CALL
72{
73 if( getMultiSelect() )
74 {
75 throw uno::RuntimeException( "Attribute use invalid." );
76 }
77 OUString sValue = getAnyAsString( _value );
78 uno::Sequence< OUString > sList;
79 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
80 sal_Int16 nValue = static_cast<sal_Int16>(comphelper::findValue(sList, sValue));
81 if( nValue == -1 )
82 throw uno::RuntimeException( "Attribute use invalid." );
83
84 uno::Sequence< sal_Int16 > nSelectedIndices { nValue };
85 uno::Sequence< sal_Int16 > nOldSelectedIndices;
86 m_xProps->getPropertyValue( "SelectedItems" ) >>= nOldSelectedIndices;
87 m_xProps->setPropertyValue( "SelectedItems", uno::Any( nSelectedIndices ) );
88 if ( nSelectedIndices != nOldSelectedIndices )
89 fireClickEvent();
90}
91
92OUString SAL_CALL
94{
95 OUString result;
96 getValue() >>= result;
97 return result;
98}
99
100void SAL_CALL
101ScVbaListBox::setText( const OUString& _text )
102{
103 setValue( uno::Any( _text ) ); // seems the same
104}
105
106sal_Int32 SAL_CALL
108{
109 bool bMultiSelect = false;
110 m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
111
112 return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
113}
114
115void SAL_CALL
116ScVbaListBox::setMultiSelect( sal_Int32 _multiselect )
117{
118 bool bBoolVal = false;
119 switch ( _multiselect )
120 {
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;
131 }
132 m_xProps->setPropertyValue( "MultiSelection" , uno::Any( bBoolVal ) );
133}
134
135
136css::uno::Any SAL_CALL
137ScVbaListBox::Selected( sal_Int32 index )
138{
139 uno::Sequence< OUString > sList;
140 m_xProps->getPropertyValue( "StringItemList" ) >>= 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( "Error Number." );
148 return uno::Any( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
149}
150
151// Methods
152void SAL_CALL
153ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex )
154{
155 maListHelper.AddItem( pvargItem, pvargIndex );
156}
157
158void SAL_CALL
160{
162}
163
164void SAL_CALL
166{
168}
169
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
174void
176{
177 bool bValue = false;
178 if( !(value >>= bValue) )
179 throw uno::RuntimeException( "Invalid type. need boolean." );
180 uno::Sequence< sal_Int16 > nList;
181 m_xProps->getPropertyValue( "SelectedItems" ) >>= 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++ )
185 {
186 if( nList[i] == nIndex )
187 {
188 if( !bValue )
189 {
190 auto pList = nList.getArray();
191 for( ; i < nLength - 1; i++ )
192 {
193 pList[i] = nList[i + 1];
194 }
195 nList.realloc( nLength - 1 );
196 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
197 fireClickEvent();
198 m_xProps->setPropertyValue( "SelectedItems", uno::Any( nList ) );
199 }
200 return;
201 }
202 }
203 if( !bValue )
204 return;
205
206 if( getMultiSelect() )
207 {
208 nList.realloc( nLength + 1 );
209 nList.getArray()[nLength] = nIndex;
210 }
211 else
212 {
213 nList = { nIndex };
214 }
215 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
216 fireClickEvent();
217 m_xProps->setPropertyValue( "SelectedItems", uno::Any( nList ) );
218}
219
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 )
223
224css::uno::Any
226{
227 uno::Sequence< sal_Int16 > nList;
228 m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
229 sal_Int32 nIndex = m_nIndex;
230 bool bRet = std::find(std::cbegin(nList), std::cend(nList), nIndex) != std::cend(nList);
231
232 return uno::Any( bRet );
233}
234
235void SAL_CALL
236ScVbaListBox::setRowSource( const OUString& _rowsource )
237{
238 ScVbaControl::setRowSource( _rowsource );
239 maListHelper.setRowSource( _rowsource );
240}
241
242sal_Int32 SAL_CALL
244{
245 return maListHelper.getListCount();
246}
247
248uno::Any SAL_CALL
249ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn )
250{
251 return maListHelper.List( pvargIndex, pvarColumn );
252}
253
254uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont()
255{
256 return new VbaNewFont( m_xProps );
257}
258
259OUString
261{
262 return "ScVbaListBox";
263}
264
265uno::Sequence< OUString >
267{
268 static uno::Sequence< OUString > const aServiceNames
269 {
270 "ooo.vba.msforms.ScVbaListBox"
271 };
272 return aServiceNames;
273}
274
275/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XPropertySet > m_xProps
void removeItem(const css::uno::Any &index)
void setRowSource(std::u16string_view _rowsource)
css::uno::Any List(const css::uno::Any &pvargIndex, const css::uno::Any &pvarColumn)
void AddItem(const css::uno::Any &pvargItem, const css::uno::Any &pvargIndex)
virtual void SAL_CALL setRowSource(const OUString &_rowsource) override
Definition: vbacontrol.cxx:388
virtual sal_Int32 SAL_CALL getMultiSelect() override
Definition: vbalistbox.cxx:107
virtual void SAL_CALL setValue(const css::uno::Any &_value) override
Definition: vbalistbox.cxx:71
virtual void SAL_CALL Clear() override
Definition: vbalistbox.cxx:165
virtual OUString SAL_CALL getText() override
Definition: vbalistbox.cxx:93
ScVbaListBox(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::uno::XInterface > &xControl, const css::uno::Reference< css::frame::XModel > &xModel, std::unique_ptr< ov::AbstractGeometryAttributes > pGeomHelper)
Definition: vbalistbox.cxx:28
virtual css::uno::Any SAL_CALL getValue() override
Definition: vbalistbox.cxx:56
virtual css::uno::Any SAL_CALL List(const css::uno::Any &pvargIndex, const css::uno::Any &pvarColumn) override
Definition: vbalistbox.cxx:249
virtual css::uno::Any getValueEvent() override
Definition: vbalistbox.cxx:225
virtual css::uno::Reference< ov::msforms::XNewFont > SAL_CALL getFont() override
Definition: vbalistbox.cxx:254
virtual css::uno::Any SAL_CALL getListIndex() override
Definition: vbalistbox.cxx:46
virtual css::uno::Any SAL_CALL Selected(::sal_Int32 index) override
Definition: vbalistbox.cxx:137
ListControlHelper maListHelper
Definition: vbalistbox.hxx:38
virtual void setValueEvent(const css::uno::Any &value) override
Definition: vbalistbox.cxx:175
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbalistbox.cxx:266
virtual void SAL_CALL setRowSource(const OUString &_rowsource) override
Definition: vbalistbox.cxx:236
virtual void SAL_CALL setMultiSelect(sal_Int32 _multiselect) override
Definition: vbalistbox.cxx:116
virtual void SAL_CALL setText(const OUString &_text) override
Definition: vbalistbox.cxx:101
sal_Int16 m_nIndex
Definition: vbalistbox.hxx:40
virtual ::sal_Int32 SAL_CALL getListCount() override
Definition: vbalistbox.cxx:243
virtual OUString getServiceImplName() override
Definition: vbalistbox.cxx:260
virtual void SAL_CALL removeItem(const css::uno::Any &index) override
Definition: vbalistbox.cxx:159
virtual void SAL_CALL AddItem(const css::uno::Any &pvargItem, const css::uno::Any &pvargIndex) override
Definition: vbalistbox.cxx:153
virtual void SAL_CALL setListIndex(const css::uno::Any &_value) override
Definition: vbalistbox.cxx:37
Any value
Sequence< OUString > aServiceNames
sal_Int16 nValue
sal_Int32 nIndex
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
int i
index
OUString getAnyAsString(const uno::Any &pvargItem)
Definition: vbahelper.cxx:495
Reference< XModel > xModel
Any result
cppu::ImplInheritanceHelper< ScVbaControl, ov::msforms::XListBox, css::script::XDefaultProperty > ListBoxImpl_BASE
Definition: vbalistbox.hxx:34
sal_Int32 nLength