Line data Source code
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"
22 : #include <comphelper/anytostring.hxx>
23 : #include <com/sun/star/script/ArrayWrapper.hpp>
24 : #include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
25 : #include <ooo/vba/msforms/fmMultiSelect.hpp>
26 :
27 : using namespace com::sun::star;
28 : using namespace ooo::vba;
29 :
30 0 : const static OUString TEXT( "Text" );
31 0 : const static OUString SELECTEDITEMS( "SelectedItems" );
32 0 : const static OUString ITEMS( "StringItemList" );
33 :
34 :
35 0 : 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 )
36 : : ListBoxImpl_BASE(xParent, xContext, xControl, xModel, pGeomHelper)
37 0 : , m_nIndex(0)
38 : {
39 0 : mpListHelper.reset( new ListControlHelper( m_xProps ) );
40 0 : }
41 :
42 : // Attributes
43 : void SAL_CALL
44 0 : ScVbaListBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException, std::exception)
45 : {
46 0 : sal_Int32 nIndex = 0;
47 0 : _value >>= nIndex;
48 0 : uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
49 0 : xPropVal->setValue( uno::makeAny( sal_True ) );
50 0 : }
51 :
52 : uno::Any SAL_CALL
53 0 : ScVbaListBox::getListIndex() throw (uno::RuntimeException, std::exception)
54 : {
55 0 : uno::Sequence< sal_Int16 > sSelection;
56 0 : m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
57 0 : if ( sSelection.getLength() == 0 )
58 0 : return uno::Any( sal_Int32( -1 ) );
59 0 : return uno::Any( sSelection[ 0 ] );
60 : }
61 :
62 : uno::Any SAL_CALL
63 0 : ScVbaListBox::getValue() throw (uno::RuntimeException, std::exception)
64 : {
65 0 : uno::Sequence< sal_Int16 > sSelection;
66 0 : uno::Sequence< OUString > sItems;
67 0 : m_xProps->getPropertyValue( SELECTEDITEMS ) >>= sSelection;
68 0 : m_xProps->getPropertyValue( ITEMS ) >>= sItems;
69 0 : if( getMultiSelect() )
70 0 : throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
71 0 : uno::Any aRet;
72 0 : if ( sSelection.getLength() )
73 0 : aRet = uno::makeAny( sItems[ sSelection[ 0 ] ] );
74 0 : return aRet;
75 : }
76 :
77 : void SAL_CALL
78 0 : ScVbaListBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException, std::exception)
79 : {
80 0 : if( getMultiSelect() )
81 : {
82 0 : throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
83 : }
84 0 : OUString sValue = getAnyAsString( _value );
85 0 : uno::Sequence< OUString > sList;
86 0 : m_xProps->getPropertyValue( ITEMS ) >>= sList;
87 0 : sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
88 0 : sal_Int16 nValue = -1;
89 0 : sal_Int16 i = 0;
90 0 : for( i = 0; i < nLength; i++ )
91 : {
92 0 : if( sList[i].equals( sValue ) )
93 : {
94 0 : nValue = i;
95 0 : break;
96 : }
97 : }
98 0 : if( nValue == -1 )
99 0 : throw uno::RuntimeException( "Attribute use invalid." , uno::Reference< uno::XInterface >() );
100 :
101 0 : uno::Sequence< sal_Int16 > nSelectedIndices(1);
102 0 : uno::Sequence< sal_Int16 > nOldSelectedIndices;
103 0 : m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nOldSelectedIndices;
104 0 : nSelectedIndices[ 0 ] = nValue;
105 0 : m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nSelectedIndices ) );
106 0 : if ( nSelectedIndices != nOldSelectedIndices )
107 0 : fireClickEvent();
108 0 : }
109 :
110 : OUString SAL_CALL
111 0 : ScVbaListBox::getText() throw (uno::RuntimeException, std::exception)
112 : {
113 0 : OUString result;
114 0 : getValue() >>= result;
115 0 : return result;
116 : }
117 :
118 : void SAL_CALL
119 0 : ScVbaListBox::setText( const OUString& _text ) throw (uno::RuntimeException, std::exception)
120 : {
121 0 : setValue( uno::makeAny( _text ) ); // seems the same
122 0 : }
123 :
124 : sal_Int32 SAL_CALL
125 0 : ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException, std::exception)
126 : {
127 0 : sal_Bool bMultiSelect = sal_False;
128 0 : m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
129 :
130 0 : return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
131 : }
132 :
133 : void SAL_CALL
134 0 : ScVbaListBox::setMultiSelect( sal_Int32 _multiselect ) throw (css::uno::RuntimeException, std::exception)
135 : {
136 0 : sal_Bool bBoolVal = false;
137 0 : switch ( _multiselect )
138 : {
139 : case msforms::fmMultiSelect::fmMultiSelectMulti:
140 : case msforms::fmMultiSelect::fmMultiSelectExtended:
141 0 : bBoolVal = sal_True;
142 0 : break;
143 : case msforms::fmMultiSelect::fmMultiSelectSingle:
144 0 : bBoolVal = sal_False;
145 0 : break;
146 : default:
147 0 : throw lang::IllegalArgumentException();
148 : break;
149 : }
150 0 : m_xProps->setPropertyValue( "MultiSelection" , uno::makeAny( bBoolVal ) );
151 0 : }
152 :
153 :
154 : css::uno::Any SAL_CALL
155 0 : ScVbaListBox::Selected( sal_Int32 index ) throw (css::uno::RuntimeException, std::exception)
156 : {
157 0 : uno::Sequence< OUString > sList;
158 0 : m_xProps->getPropertyValue( ITEMS ) >>= sList;
159 0 : sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
160 : // no choice but to do a horror cast as internally
161 : // the indices are but sal_Int16
162 0 : sal_Int16 nIndex = static_cast< sal_Int16 >( index );
163 0 : if( nIndex < 0 || nIndex >= nLength )
164 0 : throw uno::RuntimeException( "Error Number." , uno::Reference< uno::XInterface >() );
165 0 : m_nIndex = nIndex;
166 0 : return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
167 : }
168 :
169 : // Methods
170 : void SAL_CALL
171 0 : ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException, std::exception)
172 : {
173 0 : mpListHelper->AddItem( pvargItem, pvargIndex );
174 0 : }
175 :
176 : void SAL_CALL
177 0 : ScVbaListBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
178 : {
179 0 : mpListHelper->removeItem( index );
180 0 : }
181 :
182 : void SAL_CALL
183 0 : ScVbaListBox::Clear( ) throw (uno::RuntimeException, std::exception)
184 : {
185 0 : mpListHelper->Clear();
186 0 : }
187 :
188 : // this is called when something like the following vba code is used
189 : // to set the selected state of particular entries in the Listbox
190 : // ListBox1.Selected( 3 ) = false
191 : //PropListener
192 : void
193 0 : ScVbaListBox::setValueEvent( const uno::Any& value )
194 : {
195 0 : sal_Bool bValue = sal_False;
196 0 : if( !(value >>= bValue) )
197 0 : throw uno::RuntimeException( "Invalid type\n. need boolean." , uno::Reference< uno::XInterface >() );
198 0 : uno::Sequence< sal_Int16 > nList;
199 0 : m_xProps->getPropertyValue( SELECTEDITEMS ) >>= nList;
200 0 : sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
201 0 : sal_Int16 nIndex = m_nIndex;
202 0 : for( sal_Int16 i = 0; i < nLength; i++ )
203 : {
204 0 : if( nList[i] == nIndex )
205 : {
206 0 : if( bValue )
207 0 : return;
208 : else
209 : {
210 0 : for( ; i < nLength - 1; i++ )
211 : {
212 0 : nList[i] = nList[i + 1];
213 : }
214 0 : nList.realloc( nLength - 1 );
215 : //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
216 0 : fireClickEvent();
217 0 : m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
218 0 : return;
219 : }
220 : }
221 : }
222 0 : if( bValue )
223 : {
224 0 : if( getMultiSelect() )
225 : {
226 0 : nList.realloc( nLength + 1 );
227 0 : nList[nLength] = nIndex;
228 : }
229 : else
230 : {
231 0 : nList.realloc( 1 );
232 0 : nList[0] = nIndex;
233 : }
234 : //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
235 0 : fireClickEvent();
236 0 : m_xProps->setPropertyValue( SELECTEDITEMS, uno::makeAny( nList ) );
237 0 : }
238 : }
239 :
240 : // this is called when something like the following vba code is used
241 : // to determine the selected state of particular entries in the Listbox
242 : // msgbox ListBox1.Selected( 3 )
243 :
244 : css::uno::Any
245 0 : ScVbaListBox::getValueEvent()
246 : {
247 0 : uno::Sequence< sal_Int16 > nList;
248 0 : m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
249 0 : sal_Int32 nLength = nList.getLength();
250 0 : sal_Int32 nIndex = m_nIndex;
251 :
252 0 : for( sal_Int32 i = 0; i < nLength; i++ )
253 : {
254 0 : if( nList[i] == nIndex )
255 0 : return uno::makeAny( sal_True );
256 : }
257 :
258 0 : return uno::makeAny( sal_False );
259 : }
260 :
261 : void SAL_CALL
262 0 : ScVbaListBox::setRowSource( const OUString& _rowsource ) throw (uno::RuntimeException, std::exception)
263 : {
264 0 : ScVbaControl::setRowSource( _rowsource );
265 0 : mpListHelper->setRowSource( _rowsource );
266 0 : }
267 :
268 : sal_Int32 SAL_CALL
269 0 : ScVbaListBox::getListCount() throw (uno::RuntimeException, std::exception)
270 : {
271 0 : return mpListHelper->getListCount();
272 : }
273 :
274 : uno::Any SAL_CALL
275 0 : ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException, std::exception)
276 : {
277 0 : return mpListHelper->List( pvargIndex, pvarColumn );
278 : }
279 :
280 0 : uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont() throw (uno::RuntimeException, std::exception)
281 : {
282 0 : return new VbaNewFont( this, mxContext, m_xProps );
283 : }
284 :
285 : OUString
286 0 : ScVbaListBox::getServiceImplName()
287 : {
288 0 : return OUString("ScVbaListBox");
289 : }
290 :
291 : uno::Sequence< OUString >
292 0 : ScVbaListBox::getServiceNames()
293 : {
294 0 : static uno::Sequence< OUString > aServiceNames;
295 0 : if ( aServiceNames.getLength() == 0 )
296 : {
297 0 : aServiceNames.realloc( 1 );
298 0 : aServiceNames[ 0 ] = "ooo.vba.msforms.ScVbaListBox";
299 : }
300 0 : return aServiceNames;
301 0 : }
302 :
303 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|