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