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