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