Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #include "vbaworksheets.hxx"
29 : :
30 : : #include <sfx2/dispatch.hxx>
31 : : #include <sfx2/app.hxx>
32 : : #include <sfx2/bindings.hxx>
33 : : #include <sfx2/request.hxx>
34 : : #include <sfx2/viewfrm.hxx>
35 : : #include <sfx2/itemwrapper.hxx>
36 : : #include <svl/itemset.hxx>
37 : : #include <svl/eitem.hxx>
38 : :
39 : : #include <comphelper/processfactory.hxx>
40 : : #include <cppuhelper/implbase3.hxx>
41 : :
42 : : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
43 : : #include <com/sun/star/container/XEnumerationAccess.hpp>
44 : : #include <com/sun/star/sheet/XSpreadsheetView.hpp>
45 : : #include <com/sun/star/container/XNamed.hpp>
46 : : #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
47 : : #include <com/sun/star/beans/XPropertySet.hpp>
48 : :
49 : : #include <ooo/vba/excel/XApplication.hpp>
50 : : #include <tools/string.hxx>
51 : : #include "tabvwsh.hxx"
52 : :
53 : : #include "vbaglobals.hxx"
54 : : #include "vbaworksheet.hxx"
55 : : #include "vbaworkbook.hxx"
56 : : #include "unonames.hxx"
57 : : #include "markdata.hxx"
58 : :
59 : : #include <vector>
60 : : #include "prevwsh.hxx"
61 : : #include "preview.hxx"
62 : : using namespace ::ooo::vba;
63 : : using namespace ::com::sun::star;
64 : :
65 : :
66 : : typedef ::cppu::WeakImplHelper1< container::XEnumeration > SheetEnumeration_BASE;
67 : : typedef ::cppu::WeakImplHelper3< container::XNameAccess, container::XIndexAccess, container::XEnumerationAccess > SheetCollectionHelper_BASE;
68 : : // a map ( or hashmap ) wont do as we need also to preserve the order
69 : : // (as added ) of the items
70 : : typedef std::vector< uno::Reference< sheet::XSpreadsheet > > SheetMap;
71 : :
72 : :
73 : : // #FIXME #TODO the implementation of the Sheets collections sucks,
74 : : // e.g. there is no support for tracking sheets added/removed from the collection
75 : :
76 : : uno::Reference< uno::XInterface >
77 : 22 : lcl_getModulAsUnoObject( const uno::Reference< sheet::XSpreadsheet >& xSheet, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException )
78 : : {
79 : 22 : uno::Reference< uno::XInterface > xRet;
80 [ - + ]: 22 : if ( !xSheet.is() )
81 [ # # ]: 0 : throw uno::RuntimeException();
82 [ + - ]: 22 : uno::Reference< beans::XPropertySet > xProps( xSheet, uno::UNO_QUERY_THROW );
83 : 22 : rtl::OUString sName;
84 [ + - ][ + - ]: 22 : xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_CODENAME ) ) ) >>= sName;
[ + - ]
85 : :
86 [ + - ]: 22 : ScDocShell* pShell = excel::getDocShell( xModel );
87 : :
88 [ + - ]: 22 : if ( pShell )
89 [ + - ][ + - ]: 22 : xRet = getUnoDocModule( sName, pShell );
[ + - ][ + - ]
90 : 22 : return xRet;
91 : : }
92 : :
93 [ # # ]: 0 : class WorkSheetsEnumeration : public SheetEnumeration_BASE
94 : : {
95 : : SheetMap mSheetMap;
96 : : SheetMap::iterator mIt;
97 : : public:
98 [ # # ]: 0 : WorkSheetsEnumeration( const SheetMap& sMap ) : mSheetMap( sMap ), mIt( mSheetMap.begin() ) {}
99 : 0 : virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException)
100 : : {
101 [ # # ]: 0 : return ( mIt != mSheetMap.end() );
102 : : }
103 : 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
104 : : {
105 [ # # ][ # # ]: 0 : if ( !hasMoreElements() )
106 [ # # ]: 0 : throw container::NoSuchElementException();
107 [ # # ]: 0 : uno::Reference< sheet::XSpreadsheet > xSheet( *mIt++ );
108 [ # # ]: 0 : return uno::makeAny( xSheet ) ;
109 : : }
110 : : };
111 : :
112 [ # # ]: 0 : class SheetCollectionHelper : public SheetCollectionHelper_BASE
113 : : {
114 : : SheetMap mSheetMap;
115 : : SheetMap::iterator cachePos;
116 : : public:
117 [ # # ]: 0 : SheetCollectionHelper( const SheetMap& sMap ) : mSheetMap( sMap ), cachePos(mSheetMap.begin()) {}
118 : : // XElementAccess
119 : 0 : virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException) { return sheet::XSpreadsheet::static_type(0); }
120 : 0 : virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException) { return ( !mSheetMap.empty() ); }
121 : : // XNameAcess
122 : 0 : virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
123 : : {
124 [ # # ]: 0 : if ( !hasByName(aName) )
125 [ # # ]: 0 : throw container::NoSuchElementException();
126 : 0 : return uno::makeAny( *cachePos );
127 : : }
128 : 0 : virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException)
129 : : {
130 [ # # ]: 0 : uno::Sequence< rtl::OUString > sNames( mSheetMap.size() );
131 [ # # ]: 0 : rtl::OUString* pString = sNames.getArray();
132 : 0 : SheetMap::iterator it = mSheetMap.begin();
133 : 0 : SheetMap::iterator it_end = mSheetMap.end();
134 : :
135 [ # # ][ # # ]: 0 : for ( ; it != it_end; ++it, ++pString )
136 : : {
137 [ # # ]: 0 : uno::Reference< container::XNamed > xName( *it, uno::UNO_QUERY_THROW );
138 [ # # ][ # # ]: 0 : *pString = xName->getName();
139 : 0 : }
140 : 0 : return sNames;
141 : : }
142 : 0 : virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
143 : : {
144 : 0 : cachePos = mSheetMap.begin();
145 : 0 : SheetMap::iterator it_end = mSheetMap.end();
146 [ # # ][ # # ]: 0 : for ( ; cachePos != it_end; ++cachePos )
147 : : {
148 [ # # ]: 0 : uno::Reference< container::XNamed > xName( *cachePos, uno::UNO_QUERY_THROW );
149 [ # # ][ # # ]: 0 : if ( aName.equals( xName->getName() ) )
[ # # ]
150 : : break;
151 [ # # ]: 0 : }
152 [ # # ]: 0 : return ( cachePos != it_end );
153 : : }
154 : :
155 : : // XElementAccess
156 : 0 : virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException) { return mSheetMap.size(); }
157 : 0 : virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
158 : : {
159 [ # # ][ # # ]: 0 : if ( Index < 0 || Index >= getCount() )
[ # # ]
160 [ # # ]: 0 : throw lang::IndexOutOfBoundsException();
161 : :
162 : 0 : return uno::makeAny( mSheetMap[ Index ] );
163 : :
164 : : }
165 : : // XEnumerationAccess
166 : 0 : virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException)
167 : : {
168 [ # # ][ # # ]: 0 : return new WorkSheetsEnumeration( mSheetMap );
169 : : }
170 : : };
171 : :
172 [ # # ]: 0 : class SheetsEnumeration : public EnumerationHelperImpl
173 : : {
174 : : uno::Reference< frame::XModel > m_xModel;
175 : : public:
176 : 0 : SheetsEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_xModel( xModel ) {}
177 : :
178 : 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
179 : : {
180 [ # # ][ # # ]: 0 : uno::Reference< sheet::XSpreadsheet > xSheet( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
[ # # ]
181 [ # # ]: 0 : uno::Reference< uno::XInterface > xIf = lcl_getModulAsUnoObject( xSheet, m_xModel );
182 : 0 : uno::Any aRet;
183 [ # # ]: 0 : if ( !xIf.is() )
184 : : {
185 : : // if the Sheet is in a document created by the api unfortunately ( at the
186 : : // moment, it actually wont have the special Document modules
187 [ # # ][ # # ]: 0 : uno::Reference< excel::XWorksheet > xNewSheet( new ScVbaWorksheet( m_xParent, m_xContext, xSheet, m_xModel ) );
[ # # ][ # # ]
188 [ # # ]: 0 : aRet <<= xNewSheet;
189 : : }
190 : : else
191 [ # # ]: 0 : aRet <<= xIf;
192 : 0 : return aRet;
193 : : }
194 : :
195 : : };
196 : :
197 [ + - ]: 22 : ScVbaWorksheets::ScVbaWorksheets( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< container::XIndexAccess >& xSheets, const uno::Reference< frame::XModel >& xModel ): ScVbaWorksheets_BASE( xParent, xContext, xSheets ), mxModel( xModel ), m_xSheets( uno::Reference< sheet::XSpreadsheets >( xSheets, uno::UNO_QUERY ) )
198 : : {
199 : 22 : }
200 : :
201 [ # # ]: 0 : ScVbaWorksheets::ScVbaWorksheets( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< container::XEnumerationAccess >& xEnumAccess, const uno::Reference< frame::XModel >& xModel ): ScVbaWorksheets_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( xEnumAccess, uno::UNO_QUERY ) ), mxModel(xModel)
202 : : {
203 : 0 : }
204 : :
205 : : // XEnumerationAccess
206 : : uno::Type
207 : 0 : ScVbaWorksheets::getElementType() throw (uno::RuntimeException)
208 : : {
209 : 0 : return excel::XWorksheet::static_type(0);
210 : : }
211 : :
212 : : uno::Reference< container::XEnumeration >
213 : 0 : ScVbaWorksheets::createEnumeration() throw (uno::RuntimeException)
214 : : {
215 [ # # ]: 0 : if ( !m_xSheets.is() )
216 : : {
217 [ # # ]: 0 : uno::Reference< container::XEnumerationAccess > xAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
218 [ # # ][ # # ]: 0 : return xAccess->createEnumeration();
219 : : }
220 [ # # ]: 0 : uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xSheets, uno::UNO_QUERY_THROW );
221 [ # # ][ # # ]: 0 : return new SheetsEnumeration( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
[ # # ][ # # ]
[ # # ][ # # ]
222 : : }
223 : :
224 : : uno::Any
225 : 22 : ScVbaWorksheets::createCollectionObject( const uno::Any& aSource )
226 : : {
227 [ + - ]: 22 : uno::Reference< sheet::XSpreadsheet > xSheet( aSource, uno::UNO_QUERY );
228 [ + - ]: 22 : uno::Reference< XInterface > xIf = lcl_getModulAsUnoObject( xSheet, mxModel );
229 : 22 : uno::Any aRet;
230 [ - + ]: 22 : if ( !xIf.is() )
231 : : {
232 : : // if the Sheet is in a document created by the api unfortunately ( at the
233 : : // moment, it actually wont have the special Document modules
234 [ # # ][ # # ]: 0 : uno::Reference< excel::XWorksheet > xNewSheet( new ScVbaWorksheet( getParent(), mxContext, xSheet, mxModel ) );
[ # # ][ # # ]
235 [ # # ]: 0 : aRet <<= xNewSheet;
236 : : }
237 : : else
238 [ + - ]: 22 : aRet <<= xIf;
239 : 22 : return aRet;
240 : : }
241 : :
242 : : // XWorksheets
243 : : uno::Any
244 : 0 : ScVbaWorksheets::Add( const uno::Any& Before, const uno::Any& After,
245 : : const uno::Any& Count, const uno::Any& Type ) throw (uno::RuntimeException)
246 : : {
247 [ # # ]: 0 : if ( isSelectedSheets() )
248 : 0 : return uno::Any(); // or should we throw?
249 : :
250 : 0 : rtl::OUString aStringSheet;
251 : 0 : sal_Bool bBefore(sal_True);
252 : 0 : SCTAB nSheetIndex = 0;
253 : 0 : SCTAB nNewSheets = 1, nType = 0;
254 : 0 : Count >>= nNewSheets;
255 : 0 : Type >>= nType;
256 : 0 : SCTAB nCount = 0;
257 : :
258 : 0 : uno::Reference< excel::XWorksheet > xBeforeAfterSheet;
259 : :
260 [ # # ]: 0 : if ( Before.hasValue() )
261 : : {
262 [ # # ][ # # ]: 0 : if ( Before >>= xBeforeAfterSheet )
263 [ # # ][ # # ]: 0 : aStringSheet = xBeforeAfterSheet->getName();
264 : : else
265 : 0 : Before >>= aStringSheet;
266 : : }
267 : :
268 [ # # ][ # # ]: 0 : if (aStringSheet.isEmpty() && After.hasValue() )
[ # # ]
269 : : {
270 [ # # ][ # # ]: 0 : if ( After >>= xBeforeAfterSheet )
271 [ # # ][ # # ]: 0 : aStringSheet = xBeforeAfterSheet->getName();
272 : : else
273 : 0 : After >>= aStringSheet;
274 : 0 : bBefore = false;
275 : : }
276 [ # # ]: 0 : if (aStringSheet.isEmpty())
277 : : {
278 [ # # ][ # # ]: 0 : uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
279 [ # # ][ # # ]: 0 : aStringSheet = xApplication->getActiveWorkbook()->getActiveSheet()->getName();
[ # # ][ # # ]
[ # # ][ # # ]
280 : 0 : bBefore = sal_True;
281 : : }
282 [ # # ][ # # ]: 0 : nCount = static_cast< SCTAB >( m_xIndexAccess->getCount() );
283 [ # # ]: 0 : for (SCTAB i=0; i < nCount; i++)
284 : : {
285 [ # # ][ # # ]: 0 : uno::Reference< sheet::XSpreadsheet > xSheet(m_xIndexAccess->getByIndex(i), uno::UNO_QUERY);
[ # # ]
286 [ # # ]: 0 : uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
287 [ # # ][ # # ]: 0 : if (xNamed->getName() == aStringSheet)
[ # # ]
288 : : {
289 : 0 : nSheetIndex = i;
290 : : break;
291 : : }
292 [ # # ][ # # ]: 0 : }
293 : :
294 [ # # ]: 0 : if(!bBefore)
295 : 0 : nSheetIndex++;
296 : :
297 : 0 : SCTAB nSheetName = nCount + 1L;
298 [ # # ]: 0 : String aStringBase( RTL_CONSTASCII_USTRINGPARAM("Sheet") );
299 : 0 : uno::Any result;
300 [ # # ]: 0 : for (SCTAB i=0; i < nNewSheets; i++, nSheetName++)
301 : : {
302 [ # # ]: 0 : String aStringName = aStringBase;
303 [ # # ][ # # ]: 0 : aStringName += String::CreateFromInt32(nSheetName);
[ # # ]
304 [ # # ][ # # ]: 0 : while (m_xNameAccess->hasByName(aStringName))
[ # # ][ # # ]
305 : : {
306 : 0 : nSheetName++;
307 [ # # ]: 0 : aStringName = aStringBase;
308 [ # # ][ # # ]: 0 : aStringName += String::CreateFromInt32(nSheetName);
[ # # ]
309 : : }
310 [ # # ][ # # ]: 0 : m_xSheets->insertNewByName(aStringName, nSheetIndex + i);
[ # # ]
311 [ # # ][ # # ]: 0 : result = getItemByStringIndex( aStringName );
312 [ # # ]: 0 : }
313 [ # # ]: 0 : uno::Reference< excel::XWorksheet > xNewSheet( result, uno::UNO_QUERY );
314 [ # # ]: 0 : if ( xNewSheet.is() )
315 [ # # ][ # # ]: 0 : xNewSheet->Activate();
316 [ # # ]: 0 : return result;
317 : : }
318 : :
319 : : void
320 : 0 : ScVbaWorksheets::Delete() throw (uno::RuntimeException)
321 : : {
322 : : // #TODO #INVESTIGATE
323 : : // mmm this method could be trouble if the underlying
324 : : // uno objects ( the m_xIndexAccess etc ) aren't aware of the
325 : : // contents that are deleted
326 : 0 : sal_Int32 nElems = getCount();
327 [ # # ]: 0 : for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem )
328 : : {
329 [ # # ][ # # ]: 0 : uno::Reference< excel::XWorksheet > xSheet( Item( uno::makeAny( nItem ), uno::Any() ), uno::UNO_QUERY_THROW );
[ # # ]
330 [ # # ][ # # ]: 0 : xSheet->Delete();
331 : 0 : }
332 : 0 : }
333 : :
334 : : bool
335 : 0 : ScVbaWorksheets::isSelectedSheets()
336 : : {
337 : 0 : return !m_xSheets.is();
338 : : }
339 : :
340 : : void SAL_CALL
341 : 0 : ScVbaWorksheets::PrintOut( const uno::Any& From, const uno::Any& To, const uno::Any& Copies, const uno::Any& Preview, const uno::Any& ActivePrinter, const uno::Any& PrintToFile, const uno::Any& Collate, const uno::Any& PrToFileName ) throw (uno::RuntimeException)
342 : : {
343 : 0 : sal_Int32 nTo = 0;
344 : 0 : sal_Int32 nFrom = 0;
345 : 0 : sal_Int16 nCopies = 1;
346 : 0 : sal_Bool bCollate = false;
347 : 0 : sal_Bool bSelection = false;
348 : 0 : From >>= nFrom;
349 : 0 : To >>= nTo;
350 : 0 : Copies >>= nCopies;
351 [ # # ]: 0 : if ( nCopies > 1 ) // Collate only useful when more that 1 copy
352 : 0 : Collate >>= bCollate;
353 : :
354 [ # # ][ # # ]: 0 : if ( !( nFrom || nTo ) )
355 [ # # ]: 0 : if ( isSelectedSheets() )
356 : 0 : bSelection = sal_True;
357 : :
358 [ # # ][ # # ]: 0 : PrintOutHelper( excel::getBestViewShell( mxModel ), From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, bSelection );
359 : 0 : }
360 : :
361 : : uno::Any SAL_CALL
362 : 0 : ScVbaWorksheets::getVisible() throw (uno::RuntimeException)
363 : : {
364 : 0 : sal_Bool bVisible = sal_True;
365 [ # # ][ # # ]: 0 : uno::Reference< container::XEnumeration > xEnum( createEnumeration(), uno::UNO_QUERY_THROW );
366 [ # # ][ # # ]: 0 : while ( xEnum->hasMoreElements() )
[ # # ]
367 : : {
368 [ # # ][ # # ]: 0 : uno::Reference< excel::XWorksheet > xSheet( xEnum->nextElement(), uno::UNO_QUERY_THROW );
[ # # ]
369 [ # # ][ # # ]: 0 : if ( xSheet->getVisible() == false )
[ # # ]
370 : : {
371 : 0 : bVisible = false;
372 : : break;
373 : : }
374 [ # # ]: 0 : }
375 [ # # ]: 0 : return uno::makeAny( bVisible );
376 : : }
377 : :
378 : : void SAL_CALL
379 : 0 : ScVbaWorksheets::setVisible( const uno::Any& _visible ) throw (uno::RuntimeException)
380 : : {
381 : 0 : sal_Bool bState = false;
382 [ # # ]: 0 : if ( _visible >>= bState )
383 : : {
384 [ # # ][ # # ]: 0 : uno::Reference< container::XEnumeration > xEnum( createEnumeration(), uno::UNO_QUERY_THROW );
385 [ # # ][ # # ]: 0 : while ( xEnum->hasMoreElements() )
[ # # ]
386 : : {
387 [ # # ][ # # ]: 0 : uno::Reference< excel::XWorksheet > xSheet( xEnum->nextElement(), uno::UNO_QUERY_THROW );
[ # # ]
388 [ # # ][ # # ]: 0 : xSheet->setVisible( bState );
389 : 0 : }
390 : : }
391 : : else
392 : : throw uno::RuntimeException( rtl::OUString(
393 [ # # ][ # # ]: 0 : RTL_CONSTASCII_USTRINGPARAM( "Visible property doesn't support non boolean #FIXME" ) ), uno::Reference< uno::XInterface >() );
394 : 0 : }
395 : :
396 : : void SAL_CALL
397 : 0 : ScVbaWorksheets::Select( const uno::Any& Replace ) throw (uno::RuntimeException)
398 : : {
399 [ # # ]: 0 : ScTabViewShell* pViewShell = excel::getBestViewShell( mxModel );
400 [ # # ]: 0 : if ( !pViewShell )
401 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Cannot obtain view shell" ) ), uno::Reference< uno::XInterface >() );
402 : :
403 [ # # ]: 0 : ScMarkData& rMarkData = pViewShell->GetViewData()->GetMarkData();
404 : 0 : sal_Bool bReplace = sal_True;
405 : 0 : Replace >>= bReplace;
406 : : // Replace is defaulted to True, meanining this current collection
407 : : // becomes the Selection, if it were false then the current selection would
408 : : // be extended
409 : 0 : bool bSelectSingle = bReplace;
410 [ # # ]: 0 : sal_Int32 nElems = getCount();
411 [ # # ]: 0 : for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem )
412 : : {
413 [ # # ][ # # ]: 0 : uno::Reference< excel::XWorksheet > xSheet( Item( uno::makeAny( nItem ), uno::Any() ), uno::UNO_QUERY_THROW );
[ # # ]
414 [ # # ]: 0 : ScVbaWorksheet* pSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xSheet );
415 [ # # ]: 0 : if ( bSelectSingle )
416 : : {
417 [ # # ][ # # ]: 0 : rMarkData.SelectOneTable( static_cast< SCTAB >( pSheet->getSheetID() ) );
418 : 0 : bSelectSingle = false;
419 : : }
420 : : else
421 [ # # ][ # # ]: 0 : rMarkData.SelectTable( static_cast< SCTAB >( pSheet->getSheetID() ), sal_True );
422 : 0 : }
423 : :
424 : :
425 : 0 : }
426 : :
427 : : void SAL_CALL
428 : 0 : ScVbaWorksheets::Copy ( const uno::Any& Before, const uno::Any& After) throw (css::uno::RuntimeException)
429 : : {
430 : 0 : uno::Reference<excel::XWorksheet> xSheet;
431 [ # # ]: 0 : sal_Int32 nElems = getCount();
432 : 0 : bool bAfter = After.hasValue();
433 [ # # ]: 0 : std::vector< uno::Reference< excel::XWorksheet > > Sheets;
434 : 0 : sal_Int32 nItem = 0;
435 : :
436 [ # # ]: 0 : for ( nItem = 1; nItem <= nElems; ++nItem)
437 : : {
438 [ # # ][ # # ]: 0 : uno::Reference<excel::XWorksheet> xWorksheet(Item( uno::makeAny( nItem ), uno::Any() ), uno::UNO_QUERY_THROW );
[ # # ]
439 [ # # ]: 0 : Sheets.push_back(xWorksheet);
440 : 0 : }
441 [ # # ][ # # ]: 0 : bool bNewDoc = (!(Before >>= xSheet) && !(After >>=xSheet)&& !(Before.hasValue()) && !(After.hasValue()));
[ # # ][ # # ]
[ # # ][ # # ]
442 : :
443 : 0 : uno::Reference< excel::XWorksheet > xSrcSheet;
444 [ # # ]: 0 : if ( bNewDoc )
445 : : {
446 : 0 : bAfter = true;
447 [ # # ][ # # ]: 0 : xSrcSheet = Sheets.at(0);
448 [ # # ]: 0 : ScVbaWorksheet* pSrcSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xSrcSheet );
449 [ # # ][ # # ]: 0 : xSheet = pSrcSheet->createSheetCopyInNewDoc(xSrcSheet->getName());
[ # # ][ # # ]
450 : 0 : nItem = 1;
451 : : }
452 : : else
453 : : {
454 : 0 : nItem=0;
455 : : }
456 : :
457 [ # # ]: 0 : for (; nItem < nElems; ++nItem )
458 : : {
459 [ # # ]: 0 : xSrcSheet = Sheets[nItem];
460 [ # # ]: 0 : ScVbaWorksheet* pSrcSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xSrcSheet );
461 [ # # ]: 0 : if ( bAfter )
462 [ # # ][ # # ]: 0 : xSheet = pSrcSheet->createSheetCopy(xSheet, bAfter);
463 : : else
464 [ # # ]: 0 : pSrcSheet->createSheetCopy(xSheet, bAfter);
465 : 0 : }
466 : 0 : }
467 : :
468 : : //ScVbaCollectionBaseImpl
469 : : uno::Any SAL_CALL
470 : 22 : ScVbaWorksheets::Item( const uno::Any& Index, const uno::Any& Index2 ) throw (uno::RuntimeException)
471 : : {
472 [ - + ]: 22 : if ( Index.getValueTypeClass() == uno::TypeClass_SEQUENCE )
473 : : {
474 [ # # ]: 0 : uno::Reference< script::XTypeConverter > xConverter = getTypeConverter(mxContext);
475 : 0 : uno::Any aConverted;
476 [ # # ][ # # ]: 0 : aConverted = xConverter->convertTo( Index, getCppuType((uno::Sequence< uno::Any >*)0) );
[ # # ]
477 [ # # ]: 0 : SheetMap mSheets;
478 [ # # ]: 0 : uno::Sequence< uno::Any > sIndices;
479 [ # # ]: 0 : aConverted >>= sIndices;
480 : 0 : sal_Int32 nElems = sIndices.getLength();
481 [ # # ]: 0 : for( sal_Int32 index = 0; index < nElems; ++index )
482 : : {
483 [ # # ][ # # ]: 0 : uno::Reference< excel::XWorksheet > xWorkSheet( ScVbaWorksheets_BASE::Item( sIndices[ index ], Index2 ), uno::UNO_QUERY_THROW );
[ # # ]
484 [ # # ]: 0 : ScVbaWorksheet* pWorkSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xWorkSheet );
485 [ # # ][ # # ]: 0 : uno::Reference< sheet::XSpreadsheet > xSheet( pWorkSheet->getSheet() , uno::UNO_QUERY_THROW );
486 [ # # ]: 0 : uno::Reference< container::XNamed > xName( xSheet, uno::UNO_QUERY_THROW );
487 [ # # ]: 0 : mSheets.push_back( xSheet );
488 : 0 : }
489 [ # # ][ # # ]: 0 : uno::Reference< container::XIndexAccess > xIndexAccess = new SheetCollectionHelper( mSheets );
[ # # ]
490 [ # # ][ # # ]: 0 : uno::Reference< XCollection > xSelectedSheets( new ScVbaWorksheets( this->getParent(), mxContext, xIndexAccess, mxModel ) );
[ # # ][ # # ]
491 [ # # ][ # # ]: 0 : return uno::makeAny( xSelectedSheets );
492 : : }
493 : 22 : return ScVbaWorksheets_BASE::Item( Index, Index2 );
494 : : }
495 : :
496 : : uno::Any
497 : 22 : ScVbaWorksheets::getItemByStringIndex( const rtl::OUString& sIndex ) throw (uno::RuntimeException)
498 : : {
499 : 22 : return ScVbaWorksheets_BASE::getItemByStringIndex( sIndex );
500 : : }
501 : :
502 : : rtl::OUString
503 : 0 : ScVbaWorksheets::getServiceImplName()
504 : : {
505 : 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ScVbaWorksheets"));
506 : : }
507 : :
508 : : css::uno::Sequence<rtl::OUString>
509 : 0 : ScVbaWorksheets::getServiceNames()
510 : : {
511 [ # # ][ # # ]: 0 : static uno::Sequence< rtl::OUString > sNames;
[ # # ][ # # ]
512 [ # # ]: 0 : if ( sNames.getLength() == 0 )
513 : : {
514 : 0 : sNames.realloc( 1 );
515 [ # # ]: 0 : sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Worksheets") );
516 : : }
517 : 0 : return sNames;
518 : : }
519 : :
520 : 0 : bool ScVbaWorksheets::nameExists( uno::Reference <sheet::XSpreadsheetDocument>& xSpreadDoc, const ::rtl::OUString & name, SCTAB& nTab ) throw ( lang::IllegalArgumentException )
521 : : {
522 [ # # ]: 0 : if (!xSpreadDoc.is())
523 [ # # ][ # # ]: 0 : throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nameExists() xSpreadDoc is null" ) ), uno::Reference< uno::XInterface >(), 1 );
524 [ # # ][ # # ]: 0 : uno::Reference <container::XIndexAccess> xIndex( xSpreadDoc->getSheets(), uno::UNO_QUERY );
[ # # ]
525 [ # # ]: 0 : if ( xIndex.is() )
526 : : {
527 [ # # ][ # # ]: 0 : SCTAB nCount = static_cast< SCTAB >( xIndex->getCount() );
528 [ # # ]: 0 : for (SCTAB i=0; i < nCount; i++)
529 : : {
530 [ # # ][ # # ]: 0 : uno::Reference< container::XNamed > xNamed( xIndex->getByIndex(i), uno::UNO_QUERY_THROW );
[ # # ]
531 [ # # ][ # # ]: 0 : if (xNamed->getName() == name)
[ # # ]
532 : : {
533 : 0 : nTab = i;
534 : 0 : return true;
535 : : }
536 [ # # ]: 0 : }
537 : : }
538 : 0 : return false;
539 : : }
540 : :
541 : 0 : void ScVbaWorksheets::PrintPreview( const css::uno::Any& /*EnableChanges*/ ) throw (css::uno::RuntimeException)
542 : : {
543 : : // need test, print preview current active sheet
544 : : // !! TODO !! get view shell from controller
545 : 0 : ScTabViewShell* pViewShell = excel::getBestViewShell( mxModel );
546 : 0 : SfxViewFrame* pViewFrame = NULL;
547 [ # # ]: 0 : if ( pViewShell )
548 : 0 : pViewFrame = pViewShell->GetViewFrame();
549 [ # # ]: 0 : if ( pViewFrame )
550 : : {
551 [ # # ]: 0 : if ( !pViewFrame->GetFrame().IsInPlace() )
552 : : {
553 : 0 : dispatchExecute( pViewShell, SID_VIEWSHELL1 );
554 [ # # ][ # # ]: 0 : SfxViewShell* pShell = SfxViewShell::Get( pViewFrame->GetFrame().GetFrameInterface()->getController() );
[ # # ]
555 : :
556 [ # # ]: 0 : if ( pShell->ISA( ScPreviewShell ) )
557 : : {
558 : 0 : ScPreviewShell* pPrvShell = static_cast< ScPreviewShell* >( pShell );
559 [ # # ]: 0 : ScPreview* pPrvView = pPrvShell->GetPreview();
560 [ # # ]: 0 : ScMarkData aMarkData;
561 [ # # ]: 0 : sal_Int32 nElems = getCount();
562 [ # # ]: 0 : for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem )
563 : : {
564 [ # # ][ # # ]: 0 : uno::Reference< excel::XWorksheet > xSheet( Item( uno::makeAny( nItem ), uno::Any() ), uno::UNO_QUERY_THROW );
[ # # ]
565 [ # # ]: 0 : ScVbaWorksheet* pSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xSheet );
566 [ # # ]: 0 : if ( pSheet )
567 [ # # ][ # # ]: 0 : aMarkData.SelectTable(static_cast< SCTAB >( pSheet->getSheetID() ), true );
568 : 0 : }
569 : : // save old selection, setting the selectedtabs in the preview
570 : : // can affect the current selection when preview has been
571 : : // closed
572 [ # # ][ # # ]: 0 : ScMarkData::MarkedTabsType aOldTabs = pPrvView->GetSelectedTabs();
573 [ # # ]: 0 : pPrvView->SetSelectedTabs( aMarkData );
574 : : // force update
575 [ # # ]: 0 : pPrvView->DataChanged();
576 : : // set sensible first page
577 [ # # ]: 0 : long nPage = pPrvView->GetFirstPage( 1 );
578 [ # # ]: 0 : pPrvView->SetPageNo( nPage );
579 [ # # ]: 0 : WaitUntilPreviewIsClosed( pViewFrame );
580 : : // restore old tab selection
581 [ # # ]: 0 : pViewShell = excel::getBestViewShell( mxModel );
582 [ # # ][ # # ]: 0 : pViewShell->GetViewData()->GetMarkData().SetSelectedTabs(aOldTabs);
[ # # ]
583 : : }
584 : : }
585 : : }
586 : :
587 : 0 : }
588 : :
589 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|