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 "vbalistcontrolhelper.hxx"
21 #include <vector>
22 #include <vbahelper/vbapropvalue.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <comphelper/sequence.hxx>
25
26 using namespace com::sun::star;
27 using namespace ooo::vba;
28
29 class ListPropListener : public PropListener
30 {
31 private:
32 uno::Reference< beans::XPropertySet > m_xProps;
33 uno::Any const m_pvargIndex;
34 uno::Any const m_pvarColumn;
35
36 public:
37 ListPropListener( const uno::Reference< beans::XPropertySet >& xProps, const uno::Any& pvargIndex, const uno::Any& pvarColumn );
~ListPropListener()38 virtual ~ListPropListener() { };
39 virtual void setValueEvent( const css::uno::Any& value ) override;
40 virtual css::uno::Any getValueEvent() override;
41 };
42
ListPropListener(const uno::Reference<beans::XPropertySet> & xProps,const uno::Any & pvargIndex,const uno::Any & pvarColumn)43 ListPropListener::ListPropListener( const uno::Reference< beans::XPropertySet >& xProps, const uno::Any& pvargIndex, const uno::Any& pvarColumn ) : m_xProps( xProps ), m_pvargIndex( pvargIndex ), m_pvarColumn( pvarColumn )
44 {
45 }
46
setValueEvent(const uno::Any & value)47 void ListPropListener::setValueEvent( const uno::Any& value )
48 {
49 if( m_pvargIndex.hasValue() || m_pvarColumn.hasValue() )
50 throw uno::RuntimeException( "Bad argument" );
51
52 m_xProps->setPropertyValue( "StringItemList", value );
53 }
54
getValueEvent()55 uno::Any ListPropListener::getValueEvent()
56 {
57 uno::Sequence< OUString > sList;
58 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
59 sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
60 uno::Any aRet;
61 if ( m_pvargIndex.hasValue() )
62 {
63 sal_Int16 nIndex = -1;
64 m_pvargIndex >>= nIndex;
65 if( nIndex < 0 || nIndex >= nLength )
66 throw uno::RuntimeException( "Bad row Index" );
67 aRet <<= sList[ nIndex ];
68 }
69 else if ( m_pvarColumn.hasValue() ) // pvarColumn on its own would be bad
70 throw uno::RuntimeException( "Bad column Index" );
71 else // List() ( e.g. no args )
72 {
73 uno::Sequence< uno::Sequence< OUString > > sReturnArray( nLength );
74 for ( sal_Int32 i = 0; i < nLength; ++i )
75 {
76 sReturnArray[ i ].realloc( 10 );
77 sReturnArray[ i ][ 0 ] = sList[ i ];
78 }
79 aRet <<= sReturnArray;
80 }
81 return aRet;
82 }
83
84 void
AddItem(const uno::Any & pvargItem,const uno::Any & pvargIndex)85 ListControlHelper::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex )
86 {
87 if ( pvargItem.hasValue() )
88 {
89 uno::Sequence< OUString > sList;
90 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
91
92 sal_Int32 nIndex = sList.getLength();
93
94 if ( pvargIndex.hasValue() )
95 pvargIndex >>= nIndex;
96
97 OUString sString = getAnyAsString( pvargItem );
98
99 // if no index specified or item is to be appended to end of
100 // list just realloc the array and set the last item
101 if ( nIndex == sList.getLength() )
102 {
103 sal_Int32 nOldSize = sList.getLength();
104 sList.realloc( nOldSize + 1 );
105 sList[ nOldSize ] = sString;
106 }
107 else
108 {
109 // just copy those elements above the one to be inserted
110 std::vector< OUString > sVec;
111 // reserve just the amount we need to copy
112 sVec.reserve( sList.getLength() - nIndex + 1);
113
114 // insert the new element
115 sVec.push_back( sString );
116
117 // point at first element to copy
118 std::copy(std::next(sList.begin(), nIndex), sList.end(), std::back_inserter(sVec));
119
120 sList.realloc( sList.getLength() + 1 );
121
122 // point at first element to be overwritten
123 std::copy(sVec.begin(), sVec.end(), std::next(sList.begin(), nIndex));
124 }
125
126 m_xProps->setPropertyValue( "StringItemList", uno::makeAny( sList ) );
127 }
128 }
129
130 void
removeItem(const uno::Any & index)131 ListControlHelper::removeItem( const uno::Any& index )
132 {
133 sal_Int32 nIndex = 0;
134 // for int index
135 if ( index >>= nIndex )
136 {
137 uno::Sequence< OUString > sList;
138 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
139 if( nIndex < 0 || nIndex > ( sList.getLength() - 1 ) )
140 throw uno::RuntimeException( "Invalid index" , uno::Reference< uno::XInterface > () );
141 if( sList.hasElements() )
142 {
143 if( sList.getLength() == 1 )
144 {
145 Clear();
146 return;
147 }
148
149 comphelper::removeElementAt(sList, nIndex);
150 }
151
152 m_xProps->setPropertyValue( "StringItemList", uno::makeAny( sList ) );
153 }
154 }
155
156 void
Clear()157 ListControlHelper::Clear( )
158 {
159 // urk, setValue doesn't seem to work !!
160 //setValue( uno::makeAny( sal_Int16() ) );
161 m_xProps->setPropertyValue( "StringItemList", uno::makeAny( uno::Sequence< OUString >() ) );
162 }
163
164 void
setRowSource(const OUString & _rowsource)165 ListControlHelper::setRowSource( const OUString& _rowsource )
166 {
167 if ( _rowsource.isEmpty() )
168 Clear();
169 }
170
171 sal_Int32
getListCount()172 ListControlHelper::getListCount()
173 {
174 uno::Sequence< OUString > sList;
175 m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
176 return sList.getLength();
177 }
178
179 uno::Any
List(const::uno::Any & pvargIndex,const uno::Any & pvarColumn)180 ListControlHelper::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn )
181 {
182 return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( new ListPropListener( m_xProps, pvargIndex, pvarColumn ) ) ) );
183 }
184
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
186