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