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/sequence.hxx>
23 #include <ooo/vba/msforms/fmMultiSelect.hpp>
24 
25 using namespace com::sun::star;
26 using namespace ooo::vba;
27 
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)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     , m_nIndex(0)
31 {
32     mpListHelper.reset( new ListControlHelper( m_xProps ) );
33 }
34 
35 // Attributes
36 void SAL_CALL
setListIndex(const uno::Any & _value)37 ScVbaListBox::setListIndex( const uno::Any& _value )
38 {
39     sal_Int32 nIndex = 0;
40     _value >>= nIndex;
41     uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
42     xPropVal->setValue( uno::makeAny( true ) );
43 }
44 
45 uno::Any SAL_CALL
getListIndex()46 ScVbaListBox::getListIndex()
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 
55 uno::Any SAL_CALL
getValue()56 ScVbaListBox::getValue()
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 
70 void SAL_CALL
setValue(const uno::Any & _value)71 ScVbaListBox::setValue( const uno::Any& _value )
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::makeAny( nSelectedIndices ) );
88     if ( nSelectedIndices != nOldSelectedIndices )
89         fireClickEvent();
90 }
91 
92 OUString SAL_CALL
getText()93 ScVbaListBox::getText()
94 {
95     OUString result;
96     getValue() >>= result;
97     return result;
98 }
99 
100 void SAL_CALL
setText(const OUString & _text)101 ScVbaListBox::setText( const OUString& _text )
102 {
103     setValue( uno::makeAny( _text ) ); // seems the same
104 }
105 
106 sal_Int32 SAL_CALL
getMultiSelect()107 ScVbaListBox::getMultiSelect()
108 {
109     bool bMultiSelect = false;
110     m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
111 
112     return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
113 }
114 
115 void SAL_CALL
setMultiSelect(sal_Int32 _multiselect)116 ScVbaListBox::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::makeAny( bBoolVal ) );
133 }
134 
135 
136 css::uno::Any SAL_CALL
Selected(sal_Int32 index)137 ScVbaListBox::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." );
147     m_nIndex = nIndex;
148     return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
149 }
150 
151 // Methods
152 void SAL_CALL
AddItem(const uno::Any & pvargItem,const uno::Any & pvargIndex)153 ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex )
154 {
155     mpListHelper->AddItem( pvargItem, pvargIndex );
156 }
157 
158 void SAL_CALL
removeItem(const uno::Any & index)159 ScVbaListBox::removeItem( const uno::Any& index )
160 {
161     mpListHelper->removeItem( index );
162 }
163 
164 void SAL_CALL
Clear()165 ScVbaListBox::Clear(  )
166 {
167     mpListHelper->Clear();
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
174 void
setValueEvent(const uno::Any & value)175 ScVbaListBox::setValueEvent( const uno::Any& value )
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                 for( ; i < nLength - 1; i++ )
191                 {
192                     nList[i] = nList[i + 1];
193                 }
194                 nList.realloc( nLength - 1 );
195                 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
196                 fireClickEvent();
197                 m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nList ) );
198             }
199             return;
200         }
201     }
202     if( !bValue )
203         return;
204 
205     if( getMultiSelect() )
206     {
207         nList.realloc( nLength + 1 );
208         nList[nLength] = nIndex;
209     }
210     else
211     {
212         nList.realloc( 1 );
213         nList[0] = nIndex;
214     }
215     //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
216     fireClickEvent();
217     m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( 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 
224 css::uno::Any
getValueEvent()225 ScVbaListBox::getValueEvent()
226 {
227     uno::Sequence< sal_Int16 > nList;
228     m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
229     sal_Int32 nIndex = m_nIndex;
230     bool bRet = std::find(nList.begin(), nList.end(), nIndex) != nList.end();
231 
232     return uno::makeAny( bRet );
233 }
234 
235 void SAL_CALL
setRowSource(const OUString & _rowsource)236 ScVbaListBox::setRowSource( const OUString& _rowsource )
237 {
238     ScVbaControl::setRowSource( _rowsource );
239     mpListHelper->setRowSource( _rowsource );
240 }
241 
242 sal_Int32 SAL_CALL
getListCount()243 ScVbaListBox::getListCount()
244 {
245     return mpListHelper->getListCount();
246 }
247 
248 uno::Any SAL_CALL
List(const::uno::Any & pvargIndex,const uno::Any & pvarColumn)249 ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn )
250 {
251     return mpListHelper->List( pvargIndex, pvarColumn );
252 }
253 
getFont()254 uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont()
255 {
256     return new VbaNewFont( m_xProps );
257 }
258 
259 OUString
getServiceImplName()260 ScVbaListBox::getServiceImplName()
261 {
262     return "ScVbaListBox";
263 }
264 
265 uno::Sequence< OUString >
getServiceNames()266 ScVbaListBox::getServiceNames()
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: */
276